2 Copyright 1994, 1995, 1996, 1997, 1998 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;
109 /* A minimal decoding of DWARF2 compilation units. We only decode
110 what's needed to get to the line number information. */
114 /* Chain the previously read compilation units. */
115 struct comp_unit* next_unit;
117 /* Keep the bdf convenient (for memory allocation). */
120 /* The lowest and higest addresses contained in this compilation
121 unit as specified in the compilation unit header. */
125 /* The DW_AT_name attribute (for error messages). */
128 /* The abbrev hash table. */
129 struct abbrev_info** abbrevs;
131 /* Note that an error was found by comp_unit_find_nearest_line. */
134 /* The DW_AT_comp_dir attribute */
137 /* True if there is a line number table associated with this comp. unit. */
140 /* The offset into .debug_line of the line number table. */
141 unsigned long line_offset;
143 /* Pointer to the first child die for the comp unit. */
144 char *first_child_die_ptr;
146 /* The end of the comp unit. */
149 /* The decoded line number, NULL if not yet decoded. */
150 struct line_info_table* line_table;
152 /* A list of the functions found in this comp. unit. */
153 struct funcinfo* function_table;
155 /* Address size for this unit - from unit header */
156 unsigned char addr_size;
162 The following function up to the END VERBATIM mark are
163 copied directly from dwarf2read.c. */
165 /* read dwarf information from a buffer */
168 read_1_byte (abfd, buf)
172 return bfd_get_8 (abfd, (bfd_byte *) buf);
176 read_1_signed_byte (abfd, buf)
180 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
184 read_2_bytes (abfd, buf)
188 return bfd_get_16 (abfd, (bfd_byte *) buf);
193 /* This is not used. */
196 read_2_signed_bytes (abfd, buf)
200 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
206 read_4_bytes (abfd, buf)
210 return bfd_get_32 (abfd, (bfd_byte *) buf);
215 /* This is not used. */
218 read_4_signed_bytes (abfd, buf)
222 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
228 read_8_bytes (abfd, buf)
232 return bfd_get_64 (abfd, (bfd_byte *) buf);
236 read_n_bytes (abfd, buf, size)
241 /* If the size of a host char is 8 bits, we can return a pointer
242 to the buffer, otherwise we have to copy the data to a buffer
243 allocated on the temporary obstack. */
248 read_string (abfd, buf, bytes_read_ptr)
251 unsigned int *bytes_read_ptr;
253 /* If the size of a host char is 8 bits, we can return a pointer
254 to the string, otherwise we have to copy the string to a buffer
255 allocated on the temporary obstack. */
261 *bytes_read_ptr = strlen (buf) + 1;
266 read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
269 unsigned int *bytes_read_ptr;
272 unsigned int num_read;
282 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
285 result |= ((byte & 0x7f) << shift);
290 * bytes_read_ptr = num_read;
296 read_signed_leb128 (abfd, buf, bytes_read_ptr)
299 unsigned int * bytes_read_ptr;
312 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
315 result |= ((byte & 0x7f) << shift);
320 if ((shift < 32) && (byte & 0x40))
321 result |= -(1 << shift);
323 * bytes_read_ptr = num_read;
331 read_address (unit, buf)
332 struct comp_unit* unit;
337 if (unit->addr_size == 4)
339 retval = bfd_get_32 (unit->abfd, (bfd_byte *) buf);
341 retval = bfd_get_64 (unit->abfd, (bfd_byte *) buf);
350 /* This data structure holds the information of an abbrev. */
353 unsigned int number; /* number identifying abbrev */
354 enum dwarf_tag tag; /* dwarf tag */
355 int has_children; /* boolean */
356 unsigned int num_attrs; /* number of attributes */
357 struct attr_abbrev *attrs; /* an array of attribute descriptions */
358 struct abbrev_info *next; /* next in chain */
363 enum dwarf_attribute name;
364 enum dwarf_form form;
367 #ifndef ABBREV_HASH_SIZE
368 #define ABBREV_HASH_SIZE 121
370 #ifndef ATTR_ALLOC_CHUNK
371 #define ATTR_ALLOC_CHUNK 4
374 /* Lookup an abbrev_info structure in the abbrev hash table. */
376 static struct abbrev_info *
377 lookup_abbrev (number,abbrevs)
379 struct abbrev_info **abbrevs;
381 unsigned int hash_number;
382 struct abbrev_info *abbrev;
384 hash_number = number % ABBREV_HASH_SIZE;
385 abbrev = abbrevs[hash_number];
389 if (abbrev->number == number)
392 abbrev = abbrev->next;
397 /* In DWARF version 2, the description of the debugging information is
398 stored in a separate .debug_abbrev section. Before we read any
399 dies from a section we read in all abbreviations and install them
402 static struct abbrev_info**
403 read_abbrevs (abfd, offset)
407 struct abbrev_info **abbrevs;
409 struct abbrev_info *cur_abbrev;
410 unsigned int abbrev_number, bytes_read, abbrev_name;
411 unsigned int abbrev_form, hash_number;
412 struct dwarf2_debug *stash;
414 stash = elf_tdata(abfd)->dwarf2_find_line_info;
416 if (! stash->dwarf_abbrev_buffer)
420 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
423 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
424 bfd_set_error (bfd_error_bad_value);
428 stash->dwarf_abbrev_size = bfd_get_section_size_before_reloc (msec);
429 stash->dwarf_abbrev_buffer = (char*) bfd_alloc (abfd, stash->dwarf_abbrev_size);
430 if (! stash->dwarf_abbrev_buffer)
433 if (! bfd_get_section_contents (abfd, msec,
434 stash->dwarf_abbrev_buffer, 0,
435 stash->dwarf_abbrev_size))
439 if (offset > stash->dwarf_abbrev_size)
441 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%u) bigger than abbrev size (%u)."),
442 offset, stash->dwarf_abbrev_size );
443 bfd_set_error (bfd_error_bad_value);
447 abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, sizeof(struct abbrev_info*) * ABBREV_HASH_SIZE);
449 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
450 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
451 abbrev_ptr += bytes_read;
453 /* loop until we reach an abbrev number of 0 */
454 while (abbrev_number)
456 cur_abbrev = (struct abbrev_info*)bfd_zalloc (abfd, sizeof (struct abbrev_info));
458 /* read in abbrev header */
459 cur_abbrev->number = abbrev_number;
460 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
461 abbrev_ptr += bytes_read;
462 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
465 /* now read in declarations */
466 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
467 abbrev_ptr += bytes_read;
468 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
469 abbrev_ptr += bytes_read;
472 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
474 cur_abbrev->attrs = (struct attr_abbrev *)
475 bfd_realloc (cur_abbrev->attrs,
476 (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)
477 * sizeof (struct attr_abbrev));
478 if (! cur_abbrev->attrs)
481 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
482 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
483 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
484 abbrev_ptr += bytes_read;
485 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
486 abbrev_ptr += bytes_read;
489 hash_number = abbrev_number % ABBREV_HASH_SIZE;
490 cur_abbrev->next = abbrevs[hash_number];
491 abbrevs[hash_number] = cur_abbrev;
493 /* Get next abbreviation.
494 Under Irix6 the abbreviations for a compilation unit are not
495 always properly terminated with an abbrev number of 0.
496 Exit loop if we encounter an abbreviation which we have
497 already read (which means we are about to read the abbreviations
498 for the next compile unit) or if the end of the abbreviation
500 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
501 >= stash->dwarf_abbrev_size)
503 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
504 abbrev_ptr += bytes_read;
505 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
512 /* Read an attribute described by an abbreviated attribute. */
515 read_attribute (attr, abbrev, unit, info_ptr)
516 struct attribute *attr;
517 struct attr_abbrev *abbrev;
518 struct comp_unit *unit;
521 bfd *abfd = unit->abfd;
522 unsigned int bytes_read;
523 struct dwarf_block *blk;
525 attr->name = abbrev->name;
526 attr->form = abbrev->form;
527 switch (abbrev->form)
530 case DW_FORM_ref_addr:
531 DW_ADDR (attr) = read_address (unit, info_ptr);
532 info_ptr += unit->addr_size;
535 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
536 blk->size = read_2_bytes (abfd, info_ptr);
538 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
539 info_ptr += blk->size;
540 DW_BLOCK (attr) = blk;
543 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
544 blk->size = read_4_bytes (abfd, info_ptr);
546 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
547 info_ptr += blk->size;
548 DW_BLOCK (attr) = blk;
551 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
555 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
559 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
563 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
564 info_ptr += bytes_read;
567 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
568 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
569 info_ptr += bytes_read;
570 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
571 info_ptr += blk->size;
572 DW_BLOCK (attr) = blk;
575 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
576 blk->size = read_1_byte (abfd, info_ptr);
578 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
579 info_ptr += blk->size;
580 DW_BLOCK (attr) = blk;
583 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
587 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
591 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
592 info_ptr += bytes_read;
595 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
596 info_ptr += bytes_read;
599 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
603 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
607 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
610 case DW_FORM_ref_udata:
611 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
612 info_ptr += bytes_read;
615 case DW_FORM_indirect:
617 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %d."),
619 bfd_set_error (bfd_error_bad_value);
625 /* Source line information table routines. */
627 #define FILE_ALLOC_CHUNK 5
628 #define DIR_ALLOC_CHUNK 5
631 struct line_info* prev_line;
637 int end_sequence; /* end of (sequential) code sequence */
647 struct line_info_table {
650 unsigned int num_files;
651 unsigned int num_dirs;
655 struct fileinfo* files;
656 struct line_info* last_line;
660 add_line_info (table, address, filename, line, column, end_sequence)
661 struct line_info_table* table;
668 struct line_info* info = (struct line_info*)
669 bfd_alloc (table->abfd, sizeof (struct line_info));
671 info->prev_line = table->last_line;
672 table->last_line = info;
674 info->address = address;
675 info->filename = filename;
677 info->column = column;
678 info->end_sequence = end_sequence;
682 concat_filename (table, file)
683 struct line_info_table* table;
688 if (file - 1 >= table->num_files)
690 (*_bfd_error_handler) (_("Dwarf Error: mangled line number "
691 "section (bad file number)."));
695 filename = table->files[file - 1].name;
696 if (*filename == '/')
701 char* dirname = (table->files[file - 1].dir
702 ? table->dirs[table->files[file - 1].dir - 1]
704 return (char*) concat (dirname, "/", filename, NULL);
708 /* Decode the line number information for UNIT. */
710 static struct line_info_table*
711 decode_line_info (unit)
712 struct comp_unit *unit;
714 bfd *abfd = unit->abfd;
716 struct dwarf2_debug *stash;
718 struct line_info_table* table;
723 unsigned int i, bytes_read;
724 char *cur_file, *cur_dir;
725 unsigned char op_code, extended_op, adj_opcode;
727 /* This optimization unfortunately does not work well on programs
728 that have multiple sections containing text (such as Linux which
729 uses .text, and .text.init, for example. */
730 bfd_vma hi_pc = 0, lo_pc = ~ (bfd_vma) 0;
733 stash = elf_tdata (abfd)->dwarf2_find_line_info;
735 if (! stash->dwarf_line_buffer)
740 msec = bfd_get_section_by_name (abfd, ".debug_line");
743 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
744 bfd_set_error (bfd_error_bad_value);
748 size = bfd_get_section_size_before_reloc (msec);
749 stash->dwarf_line_buffer = (char *) bfd_alloc (abfd, size);
750 if (! dwarf_line_buffer)
753 if (! bfd_get_section_contents (abfd, msec,
754 dwarf_line_buffer, 0,
758 /* FIXME: We ought to apply the relocs against this section before
762 table = (struct line_info_table*) bfd_alloc (abfd,
763 sizeof (struct line_info_table));
765 table->comp_dir = unit->comp_dir;
767 table->num_files = 0;
774 table->last_line = NULL;
776 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
778 /* read in the prologue */
779 lh.total_length = read_4_bytes (abfd, line_ptr);
781 line_end = line_ptr + lh.total_length;
782 lh.version = read_2_bytes (abfd, line_ptr);
784 lh.prologue_length = read_4_bytes (abfd, line_ptr);
786 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
788 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
790 lh.line_base = read_1_signed_byte (abfd, line_ptr);
792 lh.line_range = read_1_byte (abfd, line_ptr);
794 lh.opcode_base = read_1_byte (abfd, line_ptr);
796 lh.standard_opcode_lengths = (unsigned char *)
797 bfd_alloc (abfd, lh.opcode_base * sizeof (unsigned char));
799 lh.standard_opcode_lengths[0] = 1;
800 for (i = 1; i < lh.opcode_base; ++i)
802 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
806 /* Read directory table */
807 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
809 line_ptr += bytes_read;
810 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
812 table->dirs = (char **)
813 bfd_realloc (table->dirs,
814 (table->num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *));
818 table->dirs[table->num_dirs++] = cur_dir;
820 line_ptr += bytes_read;
822 /* Read file name table */
823 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
825 line_ptr += bytes_read;
826 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
828 table->files = (struct fileinfo *)
829 bfd_realloc (table->files,
830 (table->num_files + FILE_ALLOC_CHUNK)
831 * sizeof (struct fileinfo));
835 table->files[table->num_files].name = cur_file;
836 table->files[table->num_files].dir =
837 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
838 line_ptr += bytes_read;
839 table->files[table->num_files].time =
840 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
841 line_ptr += bytes_read;
842 table->files[table->num_files].size =
843 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
844 line_ptr += bytes_read;
847 line_ptr += bytes_read;
849 /* Read the statement sequences until there's nothing left. */
850 while (line_ptr < line_end)
852 /* state machine registers */
854 char* filename = concat_filename (table, 1);
855 unsigned int line = 1;
856 unsigned int column = 0;
857 int is_stmt = lh.default_is_stmt;
859 int end_sequence = 0;
861 /* Decode the table. */
862 while (! end_sequence)
864 op_code = read_1_byte (abfd, line_ptr);
868 case DW_LNS_extended_op:
869 line_ptr += 1; /* ignore length */
870 extended_op = read_1_byte (abfd, line_ptr);
874 case DW_LNE_end_sequence:
876 add_line_info (table, address, filename, line, column, end_sequence);
878 case DW_LNE_set_address:
879 address = read_address (unit, line_ptr);
880 line_ptr += unit->addr_size;
882 case DW_LNE_define_file:
883 cur_file = read_string (abfd, line_ptr, &bytes_read);
884 line_ptr += bytes_read;
885 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
887 table->files = (struct fileinfo *)
888 bfd_realloc (table->files,
889 (table->num_files + FILE_ALLOC_CHUNK)
890 * sizeof (struct fileinfo));
894 table->files[table->num_files].name = cur_file;
895 table->files[table->num_files].dir =
896 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
897 line_ptr += bytes_read;
898 table->files[table->num_files].time =
899 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
900 line_ptr += bytes_read;
901 table->files[table->num_files].size =
902 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
903 line_ptr += bytes_read;
907 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
908 bfd_set_error (bfd_error_bad_value);
913 add_line_info (table, address, filename, line, column, 0);
916 case DW_LNS_advance_pc:
917 address += lh.minimum_instruction_length
918 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
919 line_ptr += bytes_read;
921 case DW_LNS_advance_line:
922 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
923 line_ptr += bytes_read;
925 case DW_LNS_set_file:
929 /* The file and directory tables are 0 based, the references
931 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
932 line_ptr += bytes_read;
933 filename = concat_filename (table, file);
936 case DW_LNS_set_column:
937 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
938 line_ptr += bytes_read;
940 case DW_LNS_negate_stmt:
941 is_stmt = (!is_stmt);
943 case DW_LNS_set_basic_block:
946 case DW_LNS_const_add_pc:
947 address += lh.minimum_instruction_length
948 * ((255 - lh.opcode_base) / lh.line_range);
950 case DW_LNS_fixed_advance_pc:
951 address += read_2_bytes (abfd, line_ptr);
954 default: /* special operand */
955 adj_opcode = op_code - lh.opcode_base;
956 address += (adj_opcode / lh.line_range)
957 * lh.minimum_instruction_length;
958 line += lh.line_base + (adj_opcode % lh.line_range);
959 /* append row to matrix using current values */
960 add_line_info (table, address, filename, line, column, 0);
975 if (unit->high == 0 && hi_pc != 0)
986 /* If ADDR is within TABLE set the output parameters and return true,
987 otherwise return false. The output parameters, FILENAME_PTR and
988 LINENUMBER_PTR, are pointers to the objects to be filled in. */
991 lookup_address_in_line_info_table (table,
995 struct line_info_table* table;
997 const char **filename_ptr;
998 unsigned int *linenumber_ptr;
1000 struct line_info* next_line = table->last_line;
1001 struct line_info* each_line;
1006 each_line = next_line->prev_line;
1008 while (each_line && next_line)
1010 if (!each_line->end_sequence
1011 && addr >= each_line->address && addr < next_line->address)
1013 *filename_ptr = each_line->filename;
1014 *linenumber_ptr = each_line->line;
1017 next_line = each_line;
1018 each_line = each_line->prev_line;
1027 /* Function table functions. */
1030 struct funcinfo *prev_func;
1038 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
1041 lookup_address_in_function_table (table,
1044 struct funcinfo* table;
1046 const char **functionname_ptr;
1048 struct funcinfo* each_func;
1050 for (each_func = table;
1052 each_func = each_func->prev_func)
1054 if (addr >= each_func->low && addr < each_func->high)
1056 *functionname_ptr = each_func->name;
1067 /* DWARF2 Compilation unit functions. */
1070 /* Scan over each die in a comp. unit looking for functions to add
1071 to the function table. */
1074 scan_unit_for_functions (unit)
1075 struct comp_unit *unit;
1077 bfd *abfd = unit->abfd;
1078 char *info_ptr = unit->first_child_die_ptr;
1079 int nesting_level = 1;
1081 while (nesting_level)
1083 unsigned int abbrev_number, bytes_read, i;
1084 struct abbrev_info *abbrev;
1085 struct attribute attr;
1086 struct funcinfo *func;
1089 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1090 info_ptr += bytes_read;
1092 if (! abbrev_number)
1098 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1101 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1103 bfd_set_error (bfd_error_bad_value);
1107 if (abbrev->tag == DW_TAG_subprogram)
1109 func = (struct funcinfo*) bfd_zalloc (abfd, sizeof (struct funcinfo));
1110 func->prev_func = unit->function_table;
1111 unit->function_table = func;
1116 for (i = 0; i < abbrev->num_attrs; ++i)
1118 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1126 name = DW_STRING (&attr);
1128 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1129 if (func->name == NULL)
1130 func->name = DW_STRING (&attr);
1133 case DW_AT_MIPS_linkage_name:
1134 func->name = DW_STRING (&attr);
1138 func->low = DW_ADDR (&attr);
1142 func->high = DW_ADDR (&attr);
1154 name = DW_STRING (&attr);
1163 if (abbrev->has_children)
1175 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This includes
1176 the compilation unit header that proceeds the DIE's, but does not
1177 include the length field that preceeds each compilation unit header.
1178 END_PTR points one past the end of this comp unit.
1180 This routine does not read the whole compilation unit; only enough
1181 to get to the line number information for the compilation unit. */
1183 static struct comp_unit *
1184 parse_comp_unit (abfd, info_ptr, end_ptr)
1189 struct comp_unit* unit;
1191 unsigned short version;
1192 unsigned int abbrev_offset;
1193 unsigned char addr_size;
1194 struct abbrev_info** abbrevs;
1196 unsigned int abbrev_number, bytes_read, i;
1197 struct abbrev_info *abbrev;
1198 struct attribute attr;
1200 version = read_2_bytes (abfd, info_ptr);
1202 abbrev_offset = read_4_bytes (abfd, info_ptr);
1204 addr_size = read_1_byte (abfd, info_ptr);
1209 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information."), version );
1210 bfd_set_error (bfd_error_bad_value);
1214 if (addr_size > sizeof (bfd_vma))
1216 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1219 bfd_set_error (bfd_error_bad_value);
1223 if (addr_size != 4 && addr_size != 8)
1225 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '4' and '8'.", addr_size );
1226 bfd_set_error (bfd_error_bad_value);
1230 /* Read the abbrevs for this compilation unit into a table */
1231 abbrevs = read_abbrevs (abfd, abbrev_offset);
1235 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1236 info_ptr += bytes_read;
1237 if (! abbrev_number)
1239 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %d."),
1241 bfd_set_error (bfd_error_bad_value);
1245 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1248 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1250 bfd_set_error (bfd_error_bad_value);
1254 unit = (struct comp_unit*) bfd_zalloc (abfd, sizeof (struct comp_unit));
1256 unit->addr_size = addr_size;
1257 unit->abbrevs = abbrevs;
1258 unit->end_ptr = end_ptr;
1260 for (i = 0; i < abbrev->num_attrs; ++i)
1262 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1264 /* Store the data if it is of an attribute we want to keep in a
1265 partial symbol table. */
1268 case DW_AT_stmt_list:
1270 unit->line_offset = DW_UNSND (&attr);
1274 unit->name = DW_STRING (&attr);
1278 unit->low = DW_ADDR (&attr);
1282 unit->high = DW_ADDR (&attr);
1285 case DW_AT_comp_dir:
1287 char* comp_dir = DW_STRING (&attr);
1290 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1291 directory, get rid of it. */
1292 char *cp = (char*) strchr (comp_dir, ':');
1294 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1297 unit->comp_dir = comp_dir;
1306 unit->first_child_die_ptr = info_ptr;
1314 /* Return true if UNIT contains the address given by ADDR. */
1317 comp_unit_contains_address (unit, addr)
1318 struct comp_unit* unit;
1321 return ! unit->error
1322 && (addr >= unit->low && addr <= unit->high);
1326 /* If UNIT contains ADDR, set the output parameters to the values for
1327 the line containing ADDR. The output parameters, FILENAME_PTR,
1328 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1331 Return true of UNIT contains ADDR, and no errors were encountered;
1335 comp_unit_find_nearest_line (unit, addr,
1336 filename_ptr, functionname_ptr, linenumber_ptr)
1337 struct comp_unit* unit;
1339 const char **filename_ptr;
1340 const char **functionname_ptr;
1341 unsigned int *linenumber_ptr;
1349 if (! unit->line_table)
1351 if (! unit->stmtlist)
1357 unit->line_table = decode_line_info (unit);
1359 if (! unit->line_table)
1365 if (! scan_unit_for_functions (unit))
1372 line_p = lookup_address_in_line_info_table (unit->line_table,
1376 func_p = lookup_address_in_function_table (unit->function_table,
1379 return line_p || func_p;
1382 /* The DWARF2 version of find_nearest line.
1383 Return true if the line is found without error. */
1386 _bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
1387 filename_ptr, functionname_ptr, linenumber_ptr)
1392 const char **filename_ptr;
1393 const char **functionname_ptr;
1394 unsigned int *linenumber_ptr;
1396 /* Read each compilation unit from the section .debug_info, and check
1397 to see if it contains the address we are searching for. If yes,
1398 lookup the address, and return the line number info. If no, go
1399 on to the next compilation unit.
1401 We keep a list of all the previously read compilation units, and
1402 a pointer to the next un-read compilation unit. Check the
1403 previously read units before reading more.
1406 struct dwarf2_debug *stash = elf_tdata (abfd)->dwarf2_find_line_info;
1408 /* What address are we looking for? */
1409 bfd_vma addr = offset + section->vma;
1411 struct comp_unit* each;
1415 *filename_ptr = NULL;
1416 *functionname_ptr = NULL;
1417 *linenumber_ptr = 0;
1424 stash = elf_tdata (abfd)->dwarf2_find_line_info =
1425 (struct dwarf2_debug*) bfd_zalloc (abfd, sizeof (struct dwarf2_debug));
1430 msec = bfd_get_section_by_name (abfd, ".debug_info");
1433 /* No dwarf2 info. Note that at this point the stash
1434 has been allocated, but contains zeros, this lets
1435 future calls to this function fail quicker. */
1439 size = bfd_get_section_size_before_reloc (msec);
1443 stash->info_ptr = (char *) bfd_alloc (abfd, size);
1445 if (! stash->info_ptr)
1448 if (! bfd_get_section_contents (abfd, msec, stash->info_ptr, 0, size))
1450 stash->info_ptr = 0;
1454 stash->info_ptr_end = stash->info_ptr + size;
1456 /* FIXME: There is a problem with the contents of the
1457 .debug_info section. The 'low' and 'high' addresses of the
1458 comp_units are computed by relocs against symbols in the
1459 .text segment. We need these addresses in order to determine
1460 the nearest line number, and so we have to resolve the
1461 relocs. There is a similar problem when the .debug_line
1462 section is processed as well (e.g., there may be relocs
1463 against the operand of the DW_LNE_set_address operator).
1465 Unfortunately getting hold of the reloc information is hard...
1467 For now, this means that disassembling object files (as
1468 opposed to fully executables) does not always work as well as
1472 /* A null info_ptr indicates that there is no dwarf2 info
1473 (or that an error occured while setting up the stash). */
1475 if (! stash->info_ptr)
1478 /* Check the previously read comp. units first. */
1480 for (each = stash->all_comp_units; each; each = each->next_unit)
1484 if (comp_unit_contains_address (each, addr))
1485 return comp_unit_find_nearest_line (each, addr,
1492 found = comp_unit_find_nearest_line (each, addr,
1501 /* Read each remaining comp. units checking each as they are read. */
1502 while (stash->info_ptr < stash->info_ptr_end)
1504 struct comp_unit* each;
1505 unsigned int length;
1507 length = read_4_bytes (abfd, stash->info_ptr);
1508 stash->info_ptr += 4;
1512 each = parse_comp_unit (abfd, stash->info_ptr,
1513 stash->info_ptr + length);
1514 stash->info_ptr += length;
1518 each->next_unit = stash->all_comp_units;
1519 stash->all_comp_units = each;
1521 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1522 compilation units. If we don't have them (i.e.,
1523 unit->high == 0), we need to consult the line info
1524 table to see if a compilation unit contains the given
1528 if (comp_unit_contains_address (each, addr))
1529 return comp_unit_find_nearest_line (each, addr,
1536 found = comp_unit_find_nearest_line (each, addr,