2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
8 From the dwarf2read.c header:
9 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10 Inc. with support from Florida State University (under contract
11 with the Ada Joint Program Office), and Silicon Graphics, Inc.
12 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14 support in dwarfread.c
16 This file is part of BFD.
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or (at
21 your option) any later version.
23 This program is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 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. */
43 unsigned int total_length;
44 unsigned short version;
45 unsigned int 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;
71 /* Get at parts of an attribute structure. */
73 #define DW_STRING(attr) ((attr)->u.str)
74 #define DW_UNSND(attr) ((attr)->u.unsnd)
75 #define DW_BLOCK(attr) ((attr)->u.blk)
76 #define DW_SND(attr) ((attr)->u.snd)
77 #define DW_ADDR(attr) ((attr)->u.addr)
79 /* Blocks are a bunch of untyped bytes. */
88 /* A list of all previously read comp_units. */
89 struct comp_unit* all_comp_units;
91 /* The next unread compilation unit within the .debug_info section.
92 Zero indicates that the .debug_info section has not been loaded
96 /* Pointer to the end of the .debug_info section memory buffer. */
99 /* Pointer to the section and address of the beginning of the
104 /* Pointer to the symbol table. */
107 /* Pointer to the .debug_abbrev section loaded into memory. */
108 char* dwarf_abbrev_buffer;
110 /* Length of the loaded .debug_abbrev section. */
111 unsigned long dwarf_abbrev_size;
113 /* Buffer for decode_line_info. */
114 char *dwarf_line_buffer;
116 /* Length of the loaded .debug_line section. */
117 unsigned long dwarf_line_size;
127 /* A minimal decoding of DWARF2 compilation units. We only decode
128 what's needed to get to the line number information. */
132 /* Chain the previously read compilation units. */
133 struct comp_unit* next_unit;
135 /* Keep the bdf convenient (for memory allocation). */
138 /* The lowest and higest addresses contained in this compilation
139 unit as specified in the compilation unit header. */
140 struct arange arange;
142 /* The DW_AT_name attribute (for error messages). */
145 /* The abbrev hash table. */
146 struct abbrev_info** abbrevs;
148 /* Note that an error was found by comp_unit_find_nearest_line. */
151 /* The DW_AT_comp_dir attribute. */
154 /* True if there is a line number table associated with this comp. unit. */
157 /* The offset into .debug_line of the line number table. */
158 unsigned long line_offset;
160 /* Pointer to the first child die for the comp unit. */
161 char *first_child_die_ptr;
163 /* The end of the comp unit. */
166 /* The decoded line number, NULL if not yet decoded. */
167 struct line_info_table* line_table;
169 /* A list of the functions found in this comp. unit. */
170 struct funcinfo* function_table;
172 /* Address size for this unit - from unit header. */
173 unsigned char addr_size;
176 /* This data structure holds the information of an abbrev. */
179 unsigned int number; /* Number identifying abbrev. */
180 enum dwarf_tag tag; /* DWARF tag. */
181 int has_children; /* Boolean. */
182 unsigned int num_attrs; /* Number of attributes. */
183 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
184 struct abbrev_info *next; /* Next in chain. */
189 enum dwarf_attribute name;
190 enum dwarf_form form;
193 #ifndef ABBREV_HASH_SIZE
194 #define ABBREV_HASH_SIZE 121
196 #ifndef ATTR_ALLOC_CHUNK
197 #define ATTR_ALLOC_CHUNK 4
200 static unsigned int read_1_byte PARAMS ((bfd *, char *));
201 static int read_1_signed_byte PARAMS ((bfd *, char *));
202 static unsigned int read_2_bytes PARAMS ((bfd *, char *));
203 static unsigned int read_4_bytes PARAMS ((bfd *, char *));
204 static unsigned int read_8_bytes PARAMS ((bfd *, char *));
205 static char *read_n_bytes PARAMS ((bfd *, char *, unsigned int));
206 static char *read_string PARAMS ((bfd *, char *, unsigned int *));
207 static unsigned int read_unsigned_leb128
208 PARAMS ((bfd *, char *, unsigned int *));
209 static int read_signed_leb128
210 PARAMS ((bfd *, char *, unsigned int *));
211 static bfd_vma read_address PARAMS ((struct comp_unit *, char *));
212 static struct abbrev_info *lookup_abbrev
213 PARAMS ((unsigned int, struct abbrev_info **));
214 static struct abbrev_info **read_abbrevs
215 PARAMS ((bfd *, unsigned int, struct dwarf2_debug *));
216 static char *read_attribute
217 PARAMS ((struct attribute *, struct attr_abbrev *,
218 struct comp_unit *, char *));
219 static char *read_attribute_value
220 PARAMS ((struct attribute *, unsigned,
221 struct comp_unit *, char *));
222 static void add_line_info
223 PARAMS ((struct line_info_table *, bfd_vma, char *,
224 unsigned int, unsigned int, int));
225 static char *concat_filename PARAMS ((struct line_info_table *, unsigned int));
226 static void arange_add PARAMS ((struct comp_unit *, bfd_vma, bfd_vma));
227 static struct line_info_table *decode_line_info
228 PARAMS ((struct comp_unit *, struct dwarf2_debug *));
229 static boolean lookup_address_in_line_info_table
230 PARAMS ((struct line_info_table *, bfd_vma, const char **, unsigned int *));
231 static boolean lookup_address_in_function_table
232 PARAMS ((struct funcinfo *, bfd_vma, const char **));
233 static boolean scan_unit_for_functions PARAMS ((struct comp_unit *));
234 static bfd_vma find_rela_addend
235 PARAMS ((bfd *, asection *, bfd_size_type, asymbol**));
236 static struct comp_unit *parse_comp_unit
237 PARAMS ((bfd *, struct dwarf2_debug *, bfd_vma, unsigned int));
238 static boolean comp_unit_contains_address
239 PARAMS ((struct comp_unit *, bfd_vma));
240 static boolean comp_unit_find_nearest_line
241 PARAMS ((struct comp_unit *, bfd_vma, const char **, const char **,
242 unsigned int *, struct dwarf2_debug *));
243 static asection *find_debug_info PARAMS ((bfd *, asection *));
246 The following function up to the END VERBATIM mark are
247 copied directly from dwarf2read.c. */
249 /* Read dwarf information from a buffer. */
252 read_1_byte (abfd, buf)
253 bfd *abfd ATTRIBUTE_UNUSED;
256 return bfd_get_8 (abfd, (bfd_byte *) buf);
260 read_1_signed_byte (abfd, buf)
261 bfd *abfd ATTRIBUTE_UNUSED;
264 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
268 read_2_bytes (abfd, buf)
272 return bfd_get_16 (abfd, (bfd_byte *) buf);
275 #if 0 /* This is not used. */
278 read_2_signed_bytes (abfd, buf)
282 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
288 read_4_bytes (abfd, buf)
292 return bfd_get_32 (abfd, (bfd_byte *) buf);
295 #if 0 /* This is not used. */
298 read_4_signed_bytes (abfd, buf)
302 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
308 read_8_bytes (abfd, buf)
312 return bfd_get_64 (abfd, (bfd_byte *) buf);
316 read_n_bytes (abfd, buf, size)
317 bfd *abfd ATTRIBUTE_UNUSED;
319 unsigned int size ATTRIBUTE_UNUSED;
321 /* If the size of a host char is 8 bits, we can return a pointer
322 to the buffer, otherwise we have to copy the data to a buffer
323 allocated on the temporary obstack. */
328 read_string (abfd, buf, bytes_read_ptr)
329 bfd *abfd ATTRIBUTE_UNUSED;
331 unsigned int *bytes_read_ptr;
333 /* If the size of a host char is 8 bits, we can return a pointer
334 to the string, otherwise we have to copy the string to a buffer
335 allocated on the temporary obstack. */
342 *bytes_read_ptr = strlen (buf) + 1;
347 read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
348 bfd *abfd ATTRIBUTE_UNUSED;
350 unsigned int *bytes_read_ptr;
353 unsigned int num_read;
363 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
366 result |= ((byte & 0x7f) << shift);
371 * bytes_read_ptr = num_read;
377 read_signed_leb128 (abfd, buf, bytes_read_ptr)
378 bfd *abfd ATTRIBUTE_UNUSED;
380 unsigned int * bytes_read_ptr;
393 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
396 result |= ((byte & 0x7f) << shift);
401 if ((shift < 32) && (byte & 0x40))
402 result |= -(1 << shift);
404 * bytes_read_ptr = num_read;
412 read_address (unit, buf)
413 struct comp_unit* unit;
416 switch (unit->addr_size)
419 return bfd_get_64 (unit->abfd, (bfd_byte *) buf);
421 return bfd_get_32 (unit->abfd, (bfd_byte *) buf);
423 return bfd_get_16 (unit->abfd, (bfd_byte *) buf);
429 /* Lookup an abbrev_info structure in the abbrev hash table. */
431 static struct abbrev_info *
432 lookup_abbrev (number,abbrevs)
434 struct abbrev_info **abbrevs;
436 unsigned int hash_number;
437 struct abbrev_info *abbrev;
439 hash_number = number % ABBREV_HASH_SIZE;
440 abbrev = abbrevs[hash_number];
444 if (abbrev->number == number)
447 abbrev = abbrev->next;
453 /* In DWARF version 2, the description of the debugging information is
454 stored in a separate .debug_abbrev section. Before we read any
455 dies from a section we read in all abbreviations and install them
458 static struct abbrev_info**
459 read_abbrevs (abfd, offset, stash)
462 struct dwarf2_debug *stash;
464 struct abbrev_info **abbrevs;
466 struct abbrev_info *cur_abbrev;
467 unsigned int abbrev_number, bytes_read, abbrev_name;
468 unsigned int abbrev_form, hash_number;
471 if (! stash->dwarf_abbrev_buffer)
475 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
478 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
479 bfd_set_error (bfd_error_bad_value);
483 stash->dwarf_abbrev_size = msec->_raw_size;
484 stash->dwarf_abbrev_buffer = (char*) bfd_alloc (abfd, msec->_raw_size);
485 if (! stash->dwarf_abbrev_buffer)
488 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_abbrev_buffer,
489 (bfd_vma) 0, msec->_raw_size))
493 if (offset >= stash->dwarf_abbrev_size)
495 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%u) greater than or equal to abbrev size (%u)."),
496 offset, stash->dwarf_abbrev_size );
497 bfd_set_error (bfd_error_bad_value);
501 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
502 abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, amt);
504 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
505 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
506 abbrev_ptr += bytes_read;
508 /* Loop until we reach an abbrev number of 0. */
509 while (abbrev_number)
511 amt = sizeof (struct abbrev_info);
512 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
514 /* Read in abbrev header. */
515 cur_abbrev->number = abbrev_number;
516 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
517 abbrev_ptr += bytes_read;
518 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
521 /* Now read in declarations. */
522 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
523 abbrev_ptr += bytes_read;
524 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
525 abbrev_ptr += bytes_read;
529 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
531 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
532 amt *= sizeof (struct attr_abbrev);
533 cur_abbrev->attrs = ((struct attr_abbrev *)
534 bfd_realloc (cur_abbrev->attrs, amt));
535 if (! cur_abbrev->attrs)
539 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
540 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
541 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
542 abbrev_ptr += bytes_read;
543 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
544 abbrev_ptr += bytes_read;
547 hash_number = abbrev_number % ABBREV_HASH_SIZE;
548 cur_abbrev->next = abbrevs[hash_number];
549 abbrevs[hash_number] = cur_abbrev;
551 /* Get next abbreviation.
552 Under Irix6 the abbreviations for a compilation unit are not
553 always properly terminated with an abbrev number of 0.
554 Exit loop if we encounter an abbreviation which we have
555 already read (which means we are about to read the abbreviations
556 for the next compile unit) or if the end of the abbreviation
558 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
559 >= stash->dwarf_abbrev_size)
561 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
562 abbrev_ptr += bytes_read;
563 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
570 /* Read an attribute value described by an attribute form. */
573 read_attribute_value (attr, form, unit, info_ptr)
574 struct attribute *attr;
576 struct comp_unit *unit;
579 bfd *abfd = unit->abfd;
580 unsigned int bytes_read;
581 struct dwarf_block *blk;
589 case DW_FORM_ref_addr:
590 DW_ADDR (attr) = read_address (unit, info_ptr);
591 info_ptr += unit->addr_size;
594 amt = sizeof (struct dwarf_block);
595 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
596 blk->size = read_2_bytes (abfd, info_ptr);
598 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
599 info_ptr += blk->size;
600 DW_BLOCK (attr) = blk;
603 amt = sizeof (struct dwarf_block);
604 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
605 blk->size = read_4_bytes (abfd, info_ptr);
607 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
608 info_ptr += blk->size;
609 DW_BLOCK (attr) = blk;
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);
624 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
625 info_ptr += bytes_read;
628 amt = sizeof (struct dwarf_block);
629 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
630 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
631 info_ptr += bytes_read;
632 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
633 info_ptr += blk->size;
634 DW_BLOCK (attr) = blk;
637 amt = sizeof (struct dwarf_block);
638 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
639 blk->size = read_1_byte (abfd, info_ptr);
641 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
642 info_ptr += blk->size;
643 DW_BLOCK (attr) = blk;
646 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
650 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
654 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
655 info_ptr += bytes_read;
658 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
659 info_ptr += bytes_read;
662 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
666 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
670 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
674 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
677 case DW_FORM_ref_udata:
678 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
679 info_ptr += bytes_read;
681 case DW_FORM_indirect:
682 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
683 info_ptr += bytes_read;
684 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
688 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %d."),
690 bfd_set_error (bfd_error_bad_value);
695 /* Read an attribute described by an abbreviated attribute. */
698 read_attribute (attr, abbrev, unit, info_ptr)
699 struct attribute *attr;
700 struct attr_abbrev *abbrev;
701 struct comp_unit *unit;
704 attr->name = abbrev->name;
705 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
709 /* Source line information table routines. */
711 #define FILE_ALLOC_CHUNK 5
712 #define DIR_ALLOC_CHUNK 5
716 struct line_info* prev_line;
721 int end_sequence; /* End of (sequential) code sequence. */
732 struct line_info_table
735 unsigned int num_files;
736 unsigned int num_dirs;
739 struct fileinfo* files;
740 struct line_info* last_line;
744 add_line_info (table, address, filename, line, column, end_sequence)
745 struct line_info_table* table;
752 bfd_size_type amt = sizeof (struct line_info);
753 struct line_info* info = (struct line_info*) bfd_alloc (table->abfd, amt);
755 info->prev_line = table->last_line;
756 table->last_line = info;
758 info->address = address;
759 info->filename = filename;
761 info->column = column;
762 info->end_sequence = end_sequence;
766 concat_filename (table, file)
767 struct line_info_table* table;
772 if (file - 1 >= table->num_files)
774 (*_bfd_error_handler)
775 (_("Dwarf Error: mangled line number section (bad file number)."));
779 filename = table->files[file - 1].name;
780 if (IS_ABSOLUTE_PATH(filename))
785 char* dirname = (table->files[file - 1].dir
786 ? table->dirs[table->files[file - 1].dir - 1]
788 return (char*) concat (dirname, "/", filename, NULL);
793 arange_add (unit, low_pc, high_pc)
794 struct comp_unit *unit;
798 struct arange *arange;
800 /* First see if we can cheaply extend an existing range. */
801 arange = &unit->arange;
805 if (low_pc == arange->high)
807 arange->high = high_pc;
810 if (high_pc == arange->low)
812 arange->low = low_pc;
815 arange = arange->next;
819 if (unit->arange.high == 0)
821 /* This is the first address range: store it in unit->arange. */
822 unit->arange.next = 0;
823 unit->arange.low = low_pc;
824 unit->arange.high = high_pc;
828 /* Need to allocate a new arange and insert it into the arange list. */
829 arange = bfd_zalloc (unit->abfd, (bfd_size_type) sizeof (*arange));
830 arange->low = low_pc;
831 arange->high = high_pc;
833 arange->next = unit->arange.next;
834 unit->arange.next = arange;
837 /* Decode the line number information for UNIT. */
839 static struct line_info_table*
840 decode_line_info (unit, stash)
841 struct comp_unit *unit;
842 struct dwarf2_debug *stash;
844 bfd *abfd = unit->abfd;
845 struct line_info_table* table;
849 unsigned int i, bytes_read;
850 char *cur_file, *cur_dir;
851 unsigned char op_code, extended_op, adj_opcode;
854 if (! stash->dwarf_line_buffer)
858 msec = bfd_get_section_by_name (abfd, ".debug_line");
861 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
862 bfd_set_error (bfd_error_bad_value);
866 stash->dwarf_line_size = msec->_raw_size;
867 stash->dwarf_line_buffer = (char *) bfd_alloc (abfd, msec->_raw_size);
868 if (! stash->dwarf_line_buffer)
871 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_line_buffer,
872 (bfd_vma) 0, msec->_raw_size))
875 /* FIXME: We ought to apply the relocs against this section before
879 /* Since we are using un-relocated data, it is possible to get a bad value
880 for the line_offset. Validate it here so that we won't get a segfault
882 if (unit->line_offset >= stash->dwarf_line_size)
884 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%u) greater than or equal to line size (%u)."),
885 unit->line_offset, stash->dwarf_line_size);
886 bfd_set_error (bfd_error_bad_value);
890 amt = sizeof (struct line_info_table);
891 table = (struct line_info_table*) bfd_alloc (abfd, amt);
893 table->comp_dir = unit->comp_dir;
895 table->num_files = 0;
902 table->last_line = 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);
909 line_end = line_ptr + lh.total_length;
910 lh.version = read_2_bytes (abfd, line_ptr);
912 lh.prologue_length = read_4_bytes (abfd, line_ptr);
914 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
916 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
918 lh.line_base = read_1_signed_byte (abfd, line_ptr);
920 lh.line_range = read_1_byte (abfd, line_ptr);
922 lh.opcode_base = read_1_byte (abfd, line_ptr);
924 amt = lh.opcode_base * sizeof (unsigned char);
925 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
927 lh.standard_opcode_lengths[0] = 1;
929 for (i = 1; i < lh.opcode_base; ++i)
931 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
935 /* Read directory table. */
936 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
938 line_ptr += bytes_read;
940 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
942 amt = table->num_dirs + DIR_ALLOC_CHUNK;
943 amt *= sizeof (char *);
944 table->dirs = (char **) bfd_realloc (table->dirs, amt);
949 table->dirs[table->num_dirs++] = cur_dir;
952 line_ptr += bytes_read;
954 /* Read file name table. */
955 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
957 line_ptr += bytes_read;
959 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
961 amt = table->num_files + FILE_ALLOC_CHUNK;
962 amt *= sizeof (struct fileinfo);
963 table->files = (struct fileinfo *) bfd_realloc (table->files, amt);
968 table->files[table->num_files].name = cur_file;
969 table->files[table->num_files].dir =
970 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
971 line_ptr += bytes_read;
972 table->files[table->num_files].time =
973 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
974 line_ptr += bytes_read;
975 table->files[table->num_files].size =
976 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
977 line_ptr += bytes_read;
981 line_ptr += bytes_read;
983 /* Read the statement sequences until there's nothing left. */
984 while (line_ptr < line_end)
986 /* State machine registers. */
988 char* filename = concat_filename (table, 1);
989 unsigned int line = 1;
990 unsigned int column = 0;
991 int is_stmt = lh.default_is_stmt;
993 int end_sequence = 0, need_low_pc = 1;
996 /* Decode the table. */
997 while (! end_sequence)
999 op_code = read_1_byte (abfd, line_ptr);
1002 if (op_code >= lh.opcode_base)
1003 { /* Special operand. */
1004 adj_opcode = op_code - lh.opcode_base;
1005 address += (adj_opcode / lh.line_range)
1006 * lh.minimum_instruction_length;
1007 line += lh.line_base + (adj_opcode % lh.line_range);
1008 /* Append row to matrix using current values. */
1009 add_line_info (table, address, filename, line, column, 0);
1017 else switch (op_code)
1019 case DW_LNS_extended_op:
1020 line_ptr += 1; /* Ignore length. */
1021 extended_op = read_1_byte (abfd, line_ptr);
1023 switch (extended_op)
1025 case DW_LNE_end_sequence:
1027 add_line_info (table, address, filename, line, column,
1034 arange_add (unit, low_pc, address);
1036 case DW_LNE_set_address:
1037 address = read_address (unit, line_ptr);
1038 line_ptr += unit->addr_size;
1040 case DW_LNE_define_file:
1041 cur_file = read_string (abfd, line_ptr, &bytes_read);
1042 line_ptr += bytes_read;
1043 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1045 amt = table->num_files + FILE_ALLOC_CHUNK;
1046 amt *= sizeof (struct fileinfo);
1048 (struct fileinfo *) bfd_realloc (table->files, amt);
1052 table->files[table->num_files].name = cur_file;
1053 table->files[table->num_files].dir =
1054 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1055 line_ptr += bytes_read;
1056 table->files[table->num_files].time =
1057 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1058 line_ptr += bytes_read;
1059 table->files[table->num_files].size =
1060 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1061 line_ptr += bytes_read;
1065 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1066 bfd_set_error (bfd_error_bad_value);
1071 add_line_info (table, address, filename, line, column, 0);
1079 case DW_LNS_advance_pc:
1080 address += lh.minimum_instruction_length
1081 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1082 line_ptr += bytes_read;
1084 case DW_LNS_advance_line:
1085 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1086 line_ptr += bytes_read;
1088 case DW_LNS_set_file:
1092 /* The file and directory tables are 0 based, the references
1094 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1095 line_ptr += bytes_read;
1096 filename = concat_filename (table, file);
1099 case DW_LNS_set_column:
1100 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1101 line_ptr += bytes_read;
1103 case DW_LNS_negate_stmt:
1104 is_stmt = (!is_stmt);
1106 case DW_LNS_set_basic_block:
1109 case DW_LNS_const_add_pc:
1110 address += lh.minimum_instruction_length
1111 * ((255 - lh.opcode_base) / lh.line_range);
1113 case DW_LNS_fixed_advance_pc:
1114 address += read_2_bytes (abfd, line_ptr);
1118 { /* Unknown standard opcode, ignore it. */
1120 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1122 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1123 line_ptr += bytes_read;
1133 /* If ADDR is within TABLE set the output parameters and return true,
1134 otherwise return false. The output parameters, FILENAME_PTR and
1135 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1138 lookup_address_in_line_info_table (table,
1142 struct line_info_table* table;
1144 const char **filename_ptr;
1145 unsigned int *linenumber_ptr;
1147 struct line_info* next_line = table->last_line;
1148 struct line_info* each_line;
1153 each_line = next_line->prev_line;
1155 while (each_line && next_line)
1157 if (!each_line->end_sequence
1158 && addr >= each_line->address && addr < next_line->address)
1160 *filename_ptr = each_line->filename;
1161 *linenumber_ptr = each_line->line;
1164 next_line = each_line;
1165 each_line = each_line->prev_line;
1171 /* Function table functions. */
1175 struct funcinfo *prev_func;
1181 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
1184 lookup_address_in_function_table (table,
1187 struct funcinfo* table;
1189 const char **functionname_ptr;
1191 struct funcinfo* each_func;
1193 for (each_func = table;
1195 each_func = each_func->prev_func)
1197 if (addr >= each_func->low && addr < each_func->high)
1199 *functionname_ptr = each_func->name;
1207 /* DWARF2 Compilation unit functions. */
1209 /* Scan over each die in a comp. unit looking for functions to add
1210 to the function table. */
1213 scan_unit_for_functions (unit)
1214 struct comp_unit *unit;
1216 bfd *abfd = unit->abfd;
1217 char *info_ptr = unit->first_child_die_ptr;
1218 int nesting_level = 1;
1220 while (nesting_level)
1222 unsigned int abbrev_number, bytes_read, i;
1223 struct abbrev_info *abbrev;
1224 struct attribute attr;
1225 struct funcinfo *func;
1228 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1229 info_ptr += bytes_read;
1231 if (! abbrev_number)
1237 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1240 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1242 bfd_set_error (bfd_error_bad_value);
1246 if (abbrev->tag == DW_TAG_subprogram)
1248 bfd_size_type amt = sizeof (struct funcinfo);
1249 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
1250 func->prev_func = unit->function_table;
1251 unit->function_table = func;
1256 for (i = 0; i < abbrev->num_attrs; ++i)
1258 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1266 name = DW_STRING (&attr);
1268 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1269 if (func->name == NULL)
1270 func->name = DW_STRING (&attr);
1273 case DW_AT_MIPS_linkage_name:
1274 func->name = DW_STRING (&attr);
1278 func->low = DW_ADDR (&attr);
1282 func->high = DW_ADDR (&attr);
1294 name = DW_STRING (&attr);
1303 if (abbrev->has_children)
1310 /* Look for a RELA relocation to be applied on OFFSET of section SEC,
1311 and return the addend if such a relocation is found. Since this is
1312 only used to find relocations referring to the .debug_abbrev
1313 section, we make sure the relocation refers to this section, but
1314 this is not strictly necessary, and it can probably be safely
1315 removed if needed. However, it is important to note that this
1316 function only returns the addend, it doesn't serve the purpose of
1317 applying a generic relocation.
1319 If no suitable relocation is found, or if it is not a real RELA
1320 relocation, this function returns 0. */
1323 find_rela_addend (abfd, sec, offset, syms)
1326 bfd_size_type offset;
1329 long reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
1330 arelent **relocs = NULL;
1331 long reloc_count, relc;
1333 if (reloc_size <= 0)
1336 relocs = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
1340 reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, syms);
1342 if (reloc_count <= 0)
1348 for (relc = 0; relc < reloc_count; relc++)
1349 if (relocs[relc]->address == offset
1350 && (*relocs[relc]->sym_ptr_ptr)->flags & BSF_SECTION_SYM
1351 && strcmp ((*relocs[relc]->sym_ptr_ptr)->name,
1352 ".debug_abbrev") == 0)
1354 bfd_vma addend = (relocs[relc]->howto->partial_inplace
1355 ? 0 : relocs[relc]->addend);
1364 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1365 includes the compilation unit header that proceeds the DIE's, but
1366 does not include the length field that preceeds each compilation
1367 unit header. END_PTR points one past the end of this comp unit.
1368 If ABBREV_LENGTH is 0, then the length of the abbreviation offset
1369 is assumed to be four bytes. Otherwise, it it is the size given.
1371 This routine does not read the whole compilation unit; only enough
1372 to get to the line number information for the compilation unit. */
1374 static struct comp_unit *
1375 parse_comp_unit (abfd, stash, unit_length, abbrev_length)
1377 struct dwarf2_debug *stash;
1378 bfd_vma unit_length;
1379 unsigned int abbrev_length;
1381 struct comp_unit* unit;
1383 unsigned short version;
1384 unsigned int abbrev_offset = 0;
1385 unsigned char addr_size;
1386 struct abbrev_info** abbrevs;
1388 unsigned int abbrev_number, bytes_read, i;
1389 struct abbrev_info *abbrev;
1390 struct attribute attr;
1392 char *info_ptr = stash->info_ptr;
1393 char *end_ptr = info_ptr + unit_length;
1397 version = read_2_bytes (abfd, info_ptr);
1399 BFD_ASSERT (abbrev_length == 0
1400 || abbrev_length == 4
1401 || abbrev_length == 8);
1402 if (abbrev_length == 0 || abbrev_length == 4)
1403 abbrev_offset = read_4_bytes (abfd, info_ptr);
1404 else if (abbrev_length == 8)
1405 abbrev_offset = read_8_bytes (abfd, info_ptr);
1406 /* The abbrev offset is generally a relocation pointing to
1407 .debug_abbrev+offset. On RELA targets, we have to find the
1408 relocation and extract the addend to obtain the actual
1409 abbrev_offset, so do it here. */
1410 off = info_ptr - stash->sec_info_ptr;
1411 abbrev_offset += find_rela_addend (abfd, stash->sec, off, stash->syms);
1412 info_ptr += abbrev_length;
1413 addr_size = read_1_byte (abfd, info_ptr);
1418 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information."), version );
1419 bfd_set_error (bfd_error_bad_value);
1423 if (addr_size > sizeof (bfd_vma))
1425 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1428 bfd_set_error (bfd_error_bad_value);
1432 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1434 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size );
1435 bfd_set_error (bfd_error_bad_value);
1439 /* Read the abbrevs for this compilation unit into a table. */
1440 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1444 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1445 info_ptr += bytes_read;
1446 if (! abbrev_number)
1448 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %d."),
1450 bfd_set_error (bfd_error_bad_value);
1454 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1457 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1459 bfd_set_error (bfd_error_bad_value);
1463 amt = sizeof (struct comp_unit);
1464 unit = (struct comp_unit*) bfd_zalloc (abfd, amt);
1466 unit->addr_size = addr_size;
1467 unit->abbrevs = abbrevs;
1468 unit->end_ptr = end_ptr;
1470 for (i = 0; i < abbrev->num_attrs; ++i)
1472 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1474 /* Store the data if it is of an attribute we want to keep in a
1475 partial symbol table. */
1478 case DW_AT_stmt_list:
1480 unit->line_offset = DW_UNSND (&attr);
1484 unit->name = DW_STRING (&attr);
1488 unit->arange.low = DW_ADDR (&attr);
1492 unit->arange.high = DW_ADDR (&attr);
1495 case DW_AT_comp_dir:
1497 char* comp_dir = DW_STRING (&attr);
1500 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1501 directory, get rid of it. */
1502 char *cp = (char*) strchr (comp_dir, ':');
1504 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1507 unit->comp_dir = comp_dir;
1516 unit->first_child_die_ptr = info_ptr;
1520 /* Return true if UNIT contains the address given by ADDR. */
1523 comp_unit_contains_address (unit, addr)
1524 struct comp_unit* unit;
1527 struct arange *arange;
1532 arange = &unit->arange;
1535 if (addr >= arange->low && addr < arange->high)
1537 arange = arange->next;
1544 /* If UNIT contains ADDR, set the output parameters to the values for
1545 the line containing ADDR. The output parameters, FILENAME_PTR,
1546 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1549 Return true of UNIT contains ADDR, and no errors were encountered;
1553 comp_unit_find_nearest_line (unit, addr,
1554 filename_ptr, functionname_ptr, linenumber_ptr,
1556 struct comp_unit* unit;
1558 const char **filename_ptr;
1559 const char **functionname_ptr;
1560 unsigned int *linenumber_ptr;
1561 struct dwarf2_debug *stash;
1569 if (! unit->line_table)
1571 if (! unit->stmtlist)
1577 unit->line_table = decode_line_info (unit, stash);
1579 if (! unit->line_table)
1585 if (unit->first_child_die_ptr < unit->end_ptr
1586 && ! scan_unit_for_functions (unit))
1593 line_p = lookup_address_in_line_info_table (unit->line_table,
1597 func_p = lookup_address_in_function_table (unit->function_table,
1600 return line_p || func_p;
1603 /* Locate a section in a BFD containing debugging info. The search starts from the
1604 section after AFTER_SEC, or from the first section in the BFD if AFTER_SEC is
1605 NULL. The search works by examining the names of the sections. There are two
1606 permissiable names. The first is .debug_info. This is the standard DWARF2 name.
1607 The second is a prefix .gnu.linkonce.wi. This is a variation on the .debug_info
1608 section which has a checksum describing the contents appended onto the name. This
1609 allows the linker to identify and discard duplicate debugging sections for
1610 different compilation units. */
1611 #define DWARF2_DEBUG_INFO ".debug_info"
1612 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1615 find_debug_info (abfd, after_sec)
1617 asection * after_sec;
1622 msec = after_sec->next;
1624 msec = abfd->sections;
1628 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1631 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1640 /* The DWARF2 version of find_nearest line. Return true if the line
1641 is found without error. ADDR_SIZE is the number of bytes in the
1642 initial .debug_info length field and in the abbreviation offset.
1643 You may use zero to indicate that the default value should be
1647 _bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
1648 filename_ptr, functionname_ptr,
1655 const char **filename_ptr;
1656 const char **functionname_ptr;
1657 unsigned int *linenumber_ptr;
1658 unsigned int addr_size;
1661 /* Read each compilation unit from the section .debug_info, and check
1662 to see if it contains the address we are searching for. If yes,
1663 lookup the address, and return the line number info. If no, go
1664 on to the next compilation unit.
1666 We keep a list of all the previously read compilation units, and
1667 a pointer to the next un-read compilation unit. Check the
1668 previously read units before reading more. */
1669 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
1671 /* What address are we looking for? */
1672 bfd_vma addr = offset + section->vma;
1674 struct comp_unit* each;
1676 *filename_ptr = NULL;
1677 *functionname_ptr = NULL;
1678 *linenumber_ptr = 0;
1680 /* The DWARF2 spec says that the initial length field, and the
1681 offset of the abbreviation table, should both be 4-byte values.
1682 However, some compilers do things differently. */
1685 BFD_ASSERT (addr_size == 4 || addr_size == 8);
1689 bfd_size_type total_size;
1691 bfd_size_type amt = sizeof (struct dwarf2_debug);
1693 stash = (struct dwarf2_debug*) bfd_zalloc (abfd, amt);
1697 *pinfo = (PTR) stash;
1699 msec = find_debug_info (abfd, NULL);
1701 /* No dwarf2 info. Note that at this point the stash
1702 has been allocated, but contains zeros, this lets
1703 future calls to this function fail quicker. */
1706 /* There can be more than one DWARF2 info section in a BFD these days.
1707 Read them all in and produce one large stash. We do this in two
1708 passes - in the first pass we just accumulate the section sizes.
1709 In the second pass we read in the section's contents. The allows
1710 us to avoid reallocing the data as we add sections to the stash. */
1711 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1712 total_size += msec->_raw_size;
1714 stash->info_ptr = (char *) bfd_alloc (abfd, total_size);
1715 if (stash->info_ptr == NULL)
1718 stash->info_ptr_end = stash->info_ptr;
1720 for (msec = find_debug_info (abfd, NULL);
1722 msec = find_debug_info (abfd, msec))
1725 bfd_size_type start;
1727 size = msec->_raw_size;
1731 start = stash->info_ptr_end - stash->info_ptr;
1733 if (! bfd_get_section_contents (abfd, msec, stash->info_ptr + start,
1737 stash->info_ptr_end = stash->info_ptr + start + size;
1740 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1742 stash->sec = find_debug_info (abfd, NULL);
1743 stash->sec_info_ptr = stash->info_ptr;
1744 stash->syms = symbols;
1747 /* FIXME: There is a problem with the contents of the
1748 .debug_info section. The 'low' and 'high' addresses of the
1749 comp_units are computed by relocs against symbols in the
1750 .text segment. We need these addresses in order to determine
1751 the nearest line number, and so we have to resolve the
1752 relocs. There is a similar problem when the .debug_line
1753 section is processed as well (e.g., there may be relocs
1754 against the operand of the DW_LNE_set_address operator).
1756 Unfortunately getting hold of the reloc information is hard...
1758 For now, this means that disassembling object files (as
1759 opposed to fully executables) does not always work as well as
1762 /* A null info_ptr indicates that there is no dwarf2 info
1763 (or that an error occured while setting up the stash). */
1764 if (! stash->info_ptr)
1767 /* Check the previously read comp. units first. */
1768 for (each = stash->all_comp_units; each; each = each->next_unit)
1769 if (comp_unit_contains_address (each, addr))
1770 return comp_unit_find_nearest_line (each, addr, filename_ptr,
1771 functionname_ptr, linenumber_ptr,
1774 /* Read each remaining comp. units checking each as they are read. */
1775 while (stash->info_ptr < stash->info_ptr_end)
1781 length = read_4_bytes (abfd, stash->info_ptr);
1783 length = read_8_bytes (abfd, stash->info_ptr);
1784 stash->info_ptr += addr_size;
1788 each = parse_comp_unit (abfd, stash, length, addr_size);
1789 stash->info_ptr += length;
1791 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
1792 == stash->sec->_raw_size)
1794 stash->sec = find_debug_info (abfd, stash->sec);
1795 stash->sec_info_ptr = stash->info_ptr;
1800 each->next_unit = stash->all_comp_units;
1801 stash->all_comp_units = each;
1803 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1804 compilation units. If we don't have them (i.e.,
1805 unit->high == 0), we need to consult the line info
1806 table to see if a compilation unit contains the given
1808 if (each->arange.high > 0)
1810 if (comp_unit_contains_address (each, addr))
1811 return comp_unit_find_nearest_line (each, addr,
1819 found = comp_unit_find_nearest_line (each, addr,