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. */
42 unsigned int total_length
;
43 unsigned short version
;
44 unsigned int prologue_length
;
45 unsigned char minimum_instruction_length
;
46 unsigned char default_is_stmt
;
48 unsigned char line_range
;
49 unsigned char opcode_base
;
50 unsigned char *standard_opcode_lengths
;
53 /* Attributes have a name and a value. */
57 enum dwarf_attribute name
;
62 struct dwarf_block
*blk
;
70 /* Get at parts of an attribute structure. */
72 #define DW_STRING(attr) ((attr)->u.str)
73 #define DW_UNSND(attr) ((attr)->u.unsnd)
74 #define DW_BLOCK(attr) ((attr)->u.blk)
75 #define DW_SND(attr) ((attr)->u.snd)
76 #define DW_ADDR(attr) ((attr)->u.addr)
78 /* Blocks are a bunch of untyped bytes. */
87 /* A list of all previously read comp_units. */
88 struct comp_unit
* all_comp_units
;
90 /* The next unread compilation unit within the .debug_info section.
91 Zero indicates that the .debug_info section has not been loaded
95 /* Pointer to the end of the .debug_info section memory buffer. */
98 /* Pointer to the .debug_abbrev section loaded into memory. */
99 char* dwarf_abbrev_buffer
;
101 /* Length of the loaded .debug_abbrev section. */
102 unsigned long dwarf_abbrev_size
;
104 /* Buffer for decode_line_info. */
105 char *dwarf_line_buffer
;
107 /* Length of the loaded .debug_line section. */
108 unsigned long dwarf_line_size
;
118 /* A minimal decoding of DWARF2 compilation units. We only decode
119 what's needed to get to the line number information. */
123 /* Chain the previously read compilation units. */
124 struct comp_unit
* next_unit
;
126 /* Keep the bdf convenient (for memory allocation). */
129 /* The lowest and higest addresses contained in this compilation
130 unit as specified in the compilation unit header. */
131 struct arange arange
;
133 /* The DW_AT_name attribute (for error messages). */
136 /* The abbrev hash table. */
137 struct abbrev_info
** abbrevs
;
139 /* Note that an error was found by comp_unit_find_nearest_line. */
142 /* The DW_AT_comp_dir attribute. */
145 /* True if there is a line number table associated with this comp. unit. */
148 /* The offset into .debug_line of the line number table. */
149 unsigned long line_offset
;
151 /* Pointer to the first child die for the comp unit. */
152 char *first_child_die_ptr
;
154 /* The end of the comp unit. */
157 /* The decoded line number, NULL if not yet decoded. */
158 struct line_info_table
* line_table
;
160 /* A list of the functions found in this comp. unit. */
161 struct funcinfo
* function_table
;
163 /* Address size for this unit - from unit header. */
164 unsigned char addr_size
;
168 The following function up to the END VERBATIM mark are
169 copied directly from dwarf2read.c. */
171 /* Read dwarf information from a buffer. */
174 read_1_byte (abfd
, buf
)
175 bfd
*abfd ATTRIBUTE_UNUSED
;
178 return bfd_get_8 (abfd
, (bfd_byte
*) buf
);
182 read_1_signed_byte (abfd
, buf
)
183 bfd
*abfd ATTRIBUTE_UNUSED
;
186 return bfd_get_signed_8 (abfd
, (bfd_byte
*) buf
);
190 read_2_bytes (abfd
, buf
)
194 return bfd_get_16 (abfd
, (bfd_byte
*) buf
);
197 #if 0 /* This is not used. */
200 read_2_signed_bytes (abfd
, buf
)
204 return bfd_get_signed_16 (abfd
, (bfd_byte
*) buf
);
210 read_4_bytes (abfd
, buf
)
214 return bfd_get_32 (abfd
, (bfd_byte
*) buf
);
217 #if 0 /* This is not used. */
220 read_4_signed_bytes (abfd
, buf
)
224 return bfd_get_signed_32 (abfd
, (bfd_byte
*) buf
);
230 read_8_bytes (abfd
, buf
)
234 return bfd_get_64 (abfd
, (bfd_byte
*) buf
);
238 read_n_bytes (abfd
, buf
, size
)
239 bfd
*abfd ATTRIBUTE_UNUSED
;
241 unsigned int size ATTRIBUTE_UNUSED
;
243 /* If the size of a host char is 8 bits, we can return a pointer
244 to the buffer, otherwise we have to copy the data to a buffer
245 allocated on the temporary obstack. */
250 read_string (abfd
, buf
, bytes_read_ptr
)
251 bfd
*abfd ATTRIBUTE_UNUSED
;
253 unsigned int *bytes_read_ptr
;
255 /* If the size of a host char is 8 bits, we can return a pointer
256 to the string, otherwise we have to copy the string to a buffer
257 allocated on the temporary obstack. */
264 *bytes_read_ptr
= strlen (buf
) + 1;
269 read_unsigned_leb128 (abfd
, buf
, bytes_read_ptr
)
270 bfd
*abfd ATTRIBUTE_UNUSED
;
272 unsigned int *bytes_read_ptr
;
275 unsigned int num_read
;
285 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
288 result
|= ((byte
& 0x7f) << shift
);
293 * bytes_read_ptr
= num_read
;
299 read_signed_leb128 (abfd
, buf
, bytes_read_ptr
)
300 bfd
*abfd ATTRIBUTE_UNUSED
;
302 unsigned int * bytes_read_ptr
;
315 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
318 result
|= ((byte
& 0x7f) << shift
);
323 if ((shift
< 32) && (byte
& 0x40))
324 result
|= -(1 << shift
);
326 * bytes_read_ptr
= num_read
;
334 read_address (unit
, buf
)
335 struct comp_unit
* unit
;
338 switch (unit
->addr_size
)
341 return bfd_get_64 (unit
->abfd
, (bfd_byte
*) buf
);
343 return bfd_get_32 (unit
->abfd
, (bfd_byte
*) buf
);
345 return bfd_get_16 (unit
->abfd
, (bfd_byte
*) buf
);
351 /* This data structure holds the information of an abbrev. */
354 unsigned int number
; /* Number identifying abbrev. */
355 enum dwarf_tag tag
; /* DWARF tag. */
356 int has_children
; /* Boolean. */
357 unsigned int num_attrs
; /* Number of attributes. */
358 struct attr_abbrev
*attrs
; /* An array of attribute descriptions. */
359 struct abbrev_info
*next
; /* Next in chain. */
364 enum dwarf_attribute name
;
365 enum dwarf_form form
;
368 #ifndef ABBREV_HASH_SIZE
369 #define ABBREV_HASH_SIZE 121
371 #ifndef ATTR_ALLOC_CHUNK
372 #define ATTR_ALLOC_CHUNK 4
375 /* Lookup an abbrev_info structure in the abbrev hash table. */
377 static struct abbrev_info
*
378 lookup_abbrev (number
,abbrevs
)
380 struct abbrev_info
**abbrevs
;
382 unsigned int hash_number
;
383 struct abbrev_info
*abbrev
;
385 hash_number
= number
% ABBREV_HASH_SIZE
;
386 abbrev
= abbrevs
[hash_number
];
390 if (abbrev
->number
== number
)
393 abbrev
= abbrev
->next
;
399 /* In DWARF version 2, the description of the debugging information is
400 stored in a separate .debug_abbrev section. Before we read any
401 dies from a section we read in all abbreviations and install them
404 static struct abbrev_info
**
405 read_abbrevs (abfd
, offset
, stash
)
408 struct dwarf2_debug
*stash
;
410 struct abbrev_info
**abbrevs
;
412 struct abbrev_info
*cur_abbrev
;
413 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
414 unsigned int abbrev_form
, hash_number
;
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
= msec
->_raw_size
;
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
;
473 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
475 cur_abbrev
->attrs
= (struct attr_abbrev
*)
476 bfd_realloc (cur_abbrev
->attrs
,
477 (cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
)
478 * sizeof (struct attr_abbrev
));
479 if (! cur_abbrev
->attrs
)
483 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
= abbrev_name
;
484 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
++].form
= abbrev_form
;
485 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
486 abbrev_ptr
+= bytes_read
;
487 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
488 abbrev_ptr
+= bytes_read
;
491 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
492 cur_abbrev
->next
= abbrevs
[hash_number
];
493 abbrevs
[hash_number
] = cur_abbrev
;
495 /* Get next abbreviation.
496 Under Irix6 the abbreviations for a compilation unit are not
497 always properly terminated with an abbrev number of 0.
498 Exit loop if we encounter an abbreviation which we have
499 already read (which means we are about to read the abbreviations
500 for the next compile unit) or if the end of the abbreviation
502 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
503 >= stash
->dwarf_abbrev_size
)
505 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
506 abbrev_ptr
+= bytes_read
;
507 if (lookup_abbrev (abbrev_number
,abbrevs
) != NULL
)
514 /* Read an attribute described by an abbreviated attribute. */
517 read_attribute (attr
, abbrev
, unit
, info_ptr
)
518 struct attribute
*attr
;
519 struct attr_abbrev
*abbrev
;
520 struct comp_unit
*unit
;
523 bfd
*abfd
= unit
->abfd
;
524 unsigned int bytes_read
;
525 struct dwarf_block
*blk
;
527 attr
->name
= abbrev
->name
;
528 attr
->form
= abbrev
->form
;
530 switch (abbrev
->form
)
533 case DW_FORM_ref_addr
:
534 DW_ADDR (attr
) = read_address (unit
, info_ptr
);
535 info_ptr
+= unit
->addr_size
;
538 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
539 blk
->size
= read_2_bytes (abfd
, info_ptr
);
541 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
542 info_ptr
+= blk
->size
;
543 DW_BLOCK (attr
) = blk
;
546 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
547 blk
->size
= read_4_bytes (abfd
, info_ptr
);
549 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
550 info_ptr
+= blk
->size
;
551 DW_BLOCK (attr
) = blk
;
554 DW_UNSND (attr
) = read_2_bytes (abfd
, info_ptr
);
558 DW_UNSND (attr
) = read_4_bytes (abfd
, info_ptr
);
562 DW_UNSND (attr
) = read_8_bytes (abfd
, info_ptr
);
566 DW_STRING (attr
) = read_string (abfd
, info_ptr
, &bytes_read
);
567 info_ptr
+= bytes_read
;
570 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
571 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
572 info_ptr
+= bytes_read
;
573 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
574 info_ptr
+= blk
->size
;
575 DW_BLOCK (attr
) = blk
;
578 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
579 blk
->size
= read_1_byte (abfd
, info_ptr
);
581 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
582 info_ptr
+= blk
->size
;
583 DW_BLOCK (attr
) = blk
;
586 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
590 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
594 DW_SND (attr
) = read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
595 info_ptr
+= bytes_read
;
598 DW_UNSND (attr
) = read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
599 info_ptr
+= bytes_read
;
602 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
606 DW_UNSND (attr
) = read_2_bytes (abfd
, info_ptr
);
610 DW_UNSND (attr
) = read_4_bytes (abfd
, info_ptr
);
614 DW_UNSND (attr
) = read_8_bytes (abfd
, info_ptr
);
617 case DW_FORM_ref_udata
:
618 DW_UNSND (attr
) = read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
619 info_ptr
+= bytes_read
;
622 case DW_FORM_indirect
:
624 (*_bfd_error_handler
) (_("Dwarf Error: Invalid or unhandled FORM value: %d."),
626 bfd_set_error (bfd_error_bad_value
);
631 /* Source line information table routines. */
633 #define FILE_ALLOC_CHUNK 5
634 #define DIR_ALLOC_CHUNK 5
638 struct line_info
* prev_line
;
643 int end_sequence
; /* End of (sequential) code sequence. */
654 struct line_info_table
657 unsigned int num_files
;
658 unsigned int num_dirs
;
661 struct fileinfo
* files
;
662 struct line_info
* last_line
;
666 add_line_info (table
, address
, filename
, line
, column
, end_sequence
)
667 struct line_info_table
* table
;
674 struct line_info
* info
= (struct line_info
*)
675 bfd_alloc (table
->abfd
, sizeof (struct line_info
));
677 info
->prev_line
= table
->last_line
;
678 table
->last_line
= info
;
680 info
->address
= address
;
681 info
->filename
= filename
;
683 info
->column
= column
;
684 info
->end_sequence
= end_sequence
;
688 concat_filename (table
, file
)
689 struct line_info_table
* table
;
694 if (file
- 1 >= table
->num_files
)
696 (*_bfd_error_handler
)
697 (_("Dwarf Error: mangled line number section (bad file number)."));
701 filename
= table
->files
[file
- 1].name
;
702 if (IS_ABSOLUTE_PATH(filename
))
707 char* dirname
= (table
->files
[file
- 1].dir
708 ? table
->dirs
[table
->files
[file
- 1].dir
- 1]
710 return (char*) concat (dirname
, "/", filename
, NULL
);
715 arange_add (unit
, low_pc
, high_pc
)
716 struct comp_unit
*unit
;
720 struct arange
*arange
;
722 /* First see if we can cheaply extend an existing range. */
723 arange
= &unit
->arange
;
727 if (low_pc
== arange
->high
)
729 arange
->high
= high_pc
;
732 if (high_pc
== arange
->low
)
734 arange
->low
= low_pc
;
737 arange
= arange
->next
;
741 if (unit
->arange
.high
== 0)
743 /* This is the first address range: store it in unit->arange. */
744 unit
->arange
.next
= 0;
745 unit
->arange
.low
= low_pc
;
746 unit
->arange
.high
= high_pc
;
750 /* Need to allocate a new arange and insert it into the arange list. */
751 arange
= bfd_zalloc (unit
->abfd
, sizeof (*arange
));
752 arange
->low
= low_pc
;
753 arange
->high
= high_pc
;
755 arange
->next
= unit
->arange
.next
;
756 unit
->arange
.next
= arange
;
759 /* Decode the line number information for UNIT. */
761 static struct line_info_table
*
762 decode_line_info (unit
, stash
)
763 struct comp_unit
*unit
;
764 struct dwarf2_debug
*stash
;
766 bfd
*abfd
= unit
->abfd
;
767 struct line_info_table
* table
;
771 unsigned int i
, bytes_read
;
772 char *cur_file
, *cur_dir
;
773 unsigned char op_code
, extended_op
, adj_opcode
;
775 if (! stash
->dwarf_line_buffer
)
779 msec
= bfd_get_section_by_name (abfd
, ".debug_line");
782 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_line section."));
783 bfd_set_error (bfd_error_bad_value
);
787 stash
->dwarf_line_size
= msec
->_raw_size
;
788 stash
->dwarf_line_buffer
= (char *) bfd_alloc (abfd
, stash
->dwarf_line_size
);
789 if (! stash
->dwarf_line_buffer
)
792 if (! bfd_get_section_contents (abfd
, msec
,
793 stash
->dwarf_line_buffer
, 0,
794 stash
->dwarf_line_size
))
797 /* FIXME: We ought to apply the relocs against this section before
801 /* Since we are using un-relocated data, it is possible to get a bad value
802 for the line_offset. Validate it here so that we won't get a segfault
804 if (unit
->line_offset
>= stash
->dwarf_line_size
)
806 (*_bfd_error_handler
) (_("Dwarf Error: Line offset (%u) bigger than line size (%u)."),
807 unit
->line_offset
, stash
->dwarf_line_size
);
808 bfd_set_error (bfd_error_bad_value
);
812 table
= (struct line_info_table
*) bfd_alloc (abfd
,
813 sizeof (struct line_info_table
));
815 table
->comp_dir
= unit
->comp_dir
;
817 table
->num_files
= 0;
824 table
->last_line
= NULL
;
826 line_ptr
= stash
->dwarf_line_buffer
+ unit
->line_offset
;
828 /* Read in the prologue. */
829 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
831 line_end
= line_ptr
+ lh
.total_length
;
832 lh
.version
= read_2_bytes (abfd
, line_ptr
);
834 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
);
836 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
);
838 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
);
840 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
);
842 lh
.line_range
= read_1_byte (abfd
, line_ptr
);
844 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
);
846 lh
.standard_opcode_lengths
= (unsigned char *)
847 bfd_alloc (abfd
, lh
.opcode_base
* sizeof (unsigned char));
849 lh
.standard_opcode_lengths
[0] = 1;
851 for (i
= 1; i
< lh
.opcode_base
; ++i
)
853 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
);
857 /* Read directory table. */
858 while ((cur_dir
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
860 line_ptr
+= bytes_read
;
862 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
864 table
->dirs
= (char **)
865 bfd_realloc (table
->dirs
,
866 (table
->num_dirs
+ DIR_ALLOC_CHUNK
) * sizeof (char *));
871 table
->dirs
[table
->num_dirs
++] = cur_dir
;
874 line_ptr
+= bytes_read
;
876 /* Read file name table. */
877 while ((cur_file
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
879 line_ptr
+= bytes_read
;
881 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
883 table
->files
= (struct fileinfo
*)
884 bfd_realloc (table
->files
,
885 (table
->num_files
+ FILE_ALLOC_CHUNK
)
886 * sizeof (struct fileinfo
));
891 table
->files
[table
->num_files
].name
= cur_file
;
892 table
->files
[table
->num_files
].dir
=
893 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
894 line_ptr
+= bytes_read
;
895 table
->files
[table
->num_files
].time
=
896 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
897 line_ptr
+= bytes_read
;
898 table
->files
[table
->num_files
].size
=
899 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
900 line_ptr
+= bytes_read
;
904 line_ptr
+= bytes_read
;
906 /* Read the statement sequences until there's nothing left. */
907 while (line_ptr
< line_end
)
909 /* State machine registers. */
911 char* filename
= concat_filename (table
, 1);
912 unsigned int line
= 1;
913 unsigned int column
= 0;
914 int is_stmt
= lh
.default_is_stmt
;
916 int end_sequence
= 0, need_low_pc
= 1;
919 /* Decode the table. */
920 while (! end_sequence
)
922 op_code
= read_1_byte (abfd
, line_ptr
);
927 case DW_LNS_extended_op
:
928 line_ptr
+= 1; /* Ignore length. */
929 extended_op
= read_1_byte (abfd
, line_ptr
);
933 case DW_LNE_end_sequence
:
935 add_line_info (table
, address
, filename
, line
, column
,
942 arange_add (unit
, low_pc
, address
);
944 case DW_LNE_set_address
:
945 address
= read_address (unit
, line_ptr
);
946 line_ptr
+= unit
->addr_size
;
948 case DW_LNE_define_file
:
949 cur_file
= read_string (abfd
, line_ptr
, &bytes_read
);
950 line_ptr
+= bytes_read
;
951 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
953 table
->files
= (struct fileinfo
*)
954 bfd_realloc (table
->files
,
955 (table
->num_files
+ FILE_ALLOC_CHUNK
)
956 * sizeof (struct fileinfo
));
960 table
->files
[table
->num_files
].name
= cur_file
;
961 table
->files
[table
->num_files
].dir
=
962 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
963 line_ptr
+= bytes_read
;
964 table
->files
[table
->num_files
].time
=
965 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
966 line_ptr
+= bytes_read
;
967 table
->files
[table
->num_files
].size
=
968 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
969 line_ptr
+= bytes_read
;
973 (*_bfd_error_handler
) (_("Dwarf Error: mangled line number section."));
974 bfd_set_error (bfd_error_bad_value
);
979 add_line_info (table
, address
, filename
, line
, column
, 0);
987 case DW_LNS_advance_pc
:
988 address
+= lh
.minimum_instruction_length
989 * read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
990 line_ptr
+= bytes_read
;
992 case DW_LNS_advance_line
:
993 line
+= read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
994 line_ptr
+= bytes_read
;
996 case DW_LNS_set_file
:
1000 /* The file and directory tables are 0 based, the references
1002 file
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1003 line_ptr
+= bytes_read
;
1004 filename
= concat_filename (table
, file
);
1007 case DW_LNS_set_column
:
1008 column
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1009 line_ptr
+= bytes_read
;
1011 case DW_LNS_negate_stmt
:
1012 is_stmt
= (!is_stmt
);
1014 case DW_LNS_set_basic_block
:
1017 case DW_LNS_const_add_pc
:
1018 address
+= lh
.minimum_instruction_length
1019 * ((255 - lh
.opcode_base
) / lh
.line_range
);
1021 case DW_LNS_fixed_advance_pc
:
1022 address
+= read_2_bytes (abfd
, line_ptr
);
1025 default: /* Special operand. */
1026 adj_opcode
= op_code
- lh
.opcode_base
;
1027 address
+= (adj_opcode
/ lh
.line_range
)
1028 * lh
.minimum_instruction_length
;
1029 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
1030 /* Append row to matrix using current values. */
1031 add_line_info (table
, address
, filename
, line
, column
, 0);
1045 /* If ADDR is within TABLE set the output parameters and return true,
1046 otherwise return false. The output parameters, FILENAME_PTR and
1047 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1050 lookup_address_in_line_info_table (table
,
1054 struct line_info_table
* table
;
1056 const char **filename_ptr
;
1057 unsigned int *linenumber_ptr
;
1059 struct line_info
* next_line
= table
->last_line
;
1060 struct line_info
* each_line
;
1065 each_line
= next_line
->prev_line
;
1067 while (each_line
&& next_line
)
1069 if (!each_line
->end_sequence
1070 && addr
>= each_line
->address
&& addr
< next_line
->address
)
1072 *filename_ptr
= each_line
->filename
;
1073 *linenumber_ptr
= each_line
->line
;
1076 next_line
= each_line
;
1077 each_line
= each_line
->prev_line
;
1083 /* Function table functions. */
1087 struct funcinfo
*prev_func
;
1093 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
1096 lookup_address_in_function_table (table
,
1099 struct funcinfo
* table
;
1101 const char **functionname_ptr
;
1103 struct funcinfo
* each_func
;
1105 for (each_func
= table
;
1107 each_func
= each_func
->prev_func
)
1109 if (addr
>= each_func
->low
&& addr
< each_func
->high
)
1111 *functionname_ptr
= each_func
->name
;
1119 /* DWARF2 Compilation unit functions. */
1121 /* Scan over each die in a comp. unit looking for functions to add
1122 to the function table. */
1125 scan_unit_for_functions (unit
)
1126 struct comp_unit
*unit
;
1128 bfd
*abfd
= unit
->abfd
;
1129 char *info_ptr
= unit
->first_child_die_ptr
;
1130 int nesting_level
= 1;
1132 while (nesting_level
)
1134 unsigned int abbrev_number
, bytes_read
, i
;
1135 struct abbrev_info
*abbrev
;
1136 struct attribute attr
;
1137 struct funcinfo
*func
;
1140 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1141 info_ptr
+= bytes_read
;
1143 if (! abbrev_number
)
1149 abbrev
= lookup_abbrev (abbrev_number
,unit
->abbrevs
);
1152 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %d."),
1154 bfd_set_error (bfd_error_bad_value
);
1158 if (abbrev
->tag
== DW_TAG_subprogram
)
1160 func
= (struct funcinfo
*) bfd_zalloc (abfd
, sizeof (struct funcinfo
));
1161 func
->prev_func
= unit
->function_table
;
1162 unit
->function_table
= func
;
1167 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1169 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1177 name
= DW_STRING (&attr
);
1179 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1180 if (func
->name
== NULL
)
1181 func
->name
= DW_STRING (&attr
);
1184 case DW_AT_MIPS_linkage_name
:
1185 func
->name
= DW_STRING (&attr
);
1189 func
->low
= DW_ADDR (&attr
);
1193 func
->high
= DW_ADDR (&attr
);
1205 name
= DW_STRING (&attr
);
1214 if (abbrev
->has_children
)
1221 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1222 includes the compilation unit header that proceeds the DIE's, but
1223 does not include the length field that preceeds each compilation
1224 unit header. END_PTR points one past the end of this comp unit.
1225 If ABBREV_LENGTH is 0, then the length of the abbreviation offset
1226 is assumed to be four bytes. Otherwise, it it is the size given.
1228 This routine does not read the whole compilation unit; only enough
1229 to get to the line number information for the compilation unit. */
1231 static struct comp_unit
*
1232 parse_comp_unit (abfd
, stash
, unit_length
, abbrev_length
)
1234 struct dwarf2_debug
*stash
;
1235 bfd_vma unit_length
;
1236 unsigned int abbrev_length
;
1238 struct comp_unit
* unit
;
1240 unsigned short version
;
1241 unsigned int abbrev_offset
= 0;
1242 unsigned char addr_size
;
1243 struct abbrev_info
** abbrevs
;
1245 unsigned int abbrev_number
, bytes_read
, i
;
1246 struct abbrev_info
*abbrev
;
1247 struct attribute attr
;
1249 char *info_ptr
= stash
->info_ptr
;
1250 char *end_ptr
= info_ptr
+ unit_length
;
1252 version
= read_2_bytes (abfd
, info_ptr
);
1254 BFD_ASSERT (abbrev_length
== 0
1255 || abbrev_length
== 4
1256 || abbrev_length
== 8);
1257 if (abbrev_length
== 0 || abbrev_length
== 4)
1258 abbrev_offset
= read_4_bytes (abfd
, info_ptr
);
1259 else if (abbrev_length
== 8)
1260 abbrev_offset
= read_8_bytes (abfd
, info_ptr
);
1261 info_ptr
+= abbrev_length
;
1262 addr_size
= read_1_byte (abfd
, info_ptr
);
1267 (*_bfd_error_handler
) (_("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information."), version
);
1268 bfd_set_error (bfd_error_bad_value
);
1272 if (addr_size
> sizeof (bfd_vma
))
1274 (*_bfd_error_handler
) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1277 bfd_set_error (bfd_error_bad_value
);
1281 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
1283 (*_bfd_error_handler
) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size
);
1284 bfd_set_error (bfd_error_bad_value
);
1288 /* Read the abbrevs for this compilation unit into a table. */
1289 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
);
1293 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1294 info_ptr
+= bytes_read
;
1295 if (! abbrev_number
)
1297 (*_bfd_error_handler
) (_("Dwarf Error: Bad abbrev number: %d."),
1299 bfd_set_error (bfd_error_bad_value
);
1303 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
1306 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %d."),
1308 bfd_set_error (bfd_error_bad_value
);
1312 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, sizeof (struct comp_unit
));
1314 unit
->addr_size
= addr_size
;
1315 unit
->abbrevs
= abbrevs
;
1316 unit
->end_ptr
= end_ptr
;
1318 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1320 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1322 /* Store the data if it is of an attribute we want to keep in a
1323 partial symbol table. */
1326 case DW_AT_stmt_list
:
1328 unit
->line_offset
= DW_UNSND (&attr
);
1332 unit
->name
= DW_STRING (&attr
);
1336 unit
->arange
.low
= DW_ADDR (&attr
);
1340 unit
->arange
.high
= DW_ADDR (&attr
);
1343 case DW_AT_comp_dir
:
1345 char* comp_dir
= DW_STRING (&attr
);
1348 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1349 directory, get rid of it. */
1350 char *cp
= (char*) strchr (comp_dir
, ':');
1352 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
1355 unit
->comp_dir
= comp_dir
;
1364 unit
->first_child_die_ptr
= info_ptr
;
1368 /* Return true if UNIT contains the address given by ADDR. */
1371 comp_unit_contains_address (unit
, addr
)
1372 struct comp_unit
* unit
;
1375 struct arange
*arange
;
1380 arange
= &unit
->arange
;
1383 if (addr
>= arange
->low
&& addr
< arange
->high
)
1385 arange
= arange
->next
;
1392 /* If UNIT contains ADDR, set the output parameters to the values for
1393 the line containing ADDR. The output parameters, FILENAME_PTR,
1394 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1397 Return true of UNIT contains ADDR, and no errors were encountered;
1401 comp_unit_find_nearest_line (unit
, addr
,
1402 filename_ptr
, functionname_ptr
, linenumber_ptr
,
1404 struct comp_unit
* unit
;
1406 const char **filename_ptr
;
1407 const char **functionname_ptr
;
1408 unsigned int *linenumber_ptr
;
1409 struct dwarf2_debug
*stash
;
1417 if (! unit
->line_table
)
1419 if (! unit
->stmtlist
)
1425 unit
->line_table
= decode_line_info (unit
, stash
);
1427 if (! unit
->line_table
)
1433 if (! scan_unit_for_functions (unit
))
1440 line_p
= lookup_address_in_line_info_table (unit
->line_table
,
1444 func_p
= lookup_address_in_function_table (unit
->function_table
,
1447 return line_p
|| func_p
;
1450 /* Locate a section in a BFD containing debugging info. The search starts from the
1451 section after AFTER_SEC, or from the first section in the BFD if AFTER_SEC is
1452 NULL. The search works by examining the names of the sections. There are two
1453 permissiable names. The first is .debug_info. This is the standard DWARF2 name.
1454 The second is a prefix .gnu.linkonce.wi. This is a variation on the .debug_info
1455 section which has a checksum describing the contents appended onto the name. This
1456 allows the linker to identify and discard duplicate debugging sections for
1457 different compilation units. */
1458 #define DWARF2_DEBUG_INFO ".debug_info"
1459 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1462 find_debug_info (abfd
, after_sec
)
1464 asection
* after_sec
;
1469 msec
= after_sec
->next
;
1471 msec
= abfd
->sections
;
1475 if (strcmp (msec
->name
, DWARF2_DEBUG_INFO
) == 0)
1478 if (strncmp (msec
->name
, GNU_LINKONCE_INFO
, strlen (GNU_LINKONCE_INFO
)) == 0)
1487 /* The DWARF2 version of find_nearest line. Return true if the line
1488 is found without error. ADDR_SIZE is the number of bytes in the
1489 initial .debug_info length field and in the abbreviation offset.
1490 You may use zero to indicate that the default value should be
1494 _bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
1495 filename_ptr
, functionname_ptr
,
1500 asymbol
**symbols ATTRIBUTE_UNUSED
;
1502 const char **filename_ptr
;
1503 const char **functionname_ptr
;
1504 unsigned int *linenumber_ptr
;
1505 unsigned int addr_size
;
1508 /* Read each compilation unit from the section .debug_info, and check
1509 to see if it contains the address we are searching for. If yes,
1510 lookup the address, and return the line number info. If no, go
1511 on to the next compilation unit.
1513 We keep a list of all the previously read compilation units, and
1514 a pointer to the next un-read compilation unit. Check the
1515 previously read units before reading more. */
1516 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
1518 /* What address are we looking for? */
1519 bfd_vma addr
= offset
+ section
->vma
;
1521 struct comp_unit
* each
;
1523 *filename_ptr
= NULL
;
1524 *functionname_ptr
= NULL
;
1525 *linenumber_ptr
= 0;
1527 /* The DWARF2 spec says that the initial length field, and the
1528 offset of the abbreviation table, should both be 4-byte values.
1529 However, some compilers do things differently. */
1532 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
1536 unsigned long total_size
;
1540 (struct dwarf2_debug
*) bfd_zalloc (abfd
, sizeof (struct dwarf2_debug
));
1544 *pinfo
= (PTR
) stash
;
1546 msec
= find_debug_info (abfd
, NULL
);
1548 /* No dwarf2 info. Note that at this point the stash
1549 has been allocated, but contains zeros, this lets
1550 future calls to this function fail quicker. */
1553 /* There can be more than one DWARF2 info section in a BFD these days.
1554 Read them all in and produce one large stash. We do this in two
1555 passes - in the first pass we just accumulate the section sizes.
1556 In the second pass we read in the section's contents. The allows
1557 us to avoid reallocing the data as we add sections to the stash. */
1558 for (total_size
= 0; msec
; msec
= find_debug_info (abfd
, msec
))
1559 total_size
+= msec
->_raw_size
;
1561 stash
->info_ptr
= (char *) bfd_alloc (abfd
, total_size
);
1562 if (stash
->info_ptr
== NULL
)
1565 stash
->info_ptr_end
= stash
->info_ptr
;
1567 for (msec
= find_debug_info (abfd
, NULL
);
1569 msec
= find_debug_info (abfd
, msec
))
1572 unsigned long start
;
1574 size
= msec
->_raw_size
;
1578 start
= stash
->info_ptr_end
- stash
->info_ptr
;
1580 if (! bfd_get_section_contents (abfd
, msec
, stash
->info_ptr
+ start
, 0, size
))
1583 stash
->info_ptr_end
= stash
->info_ptr
+ start
+ size
;
1586 BFD_ASSERT (stash
->info_ptr_end
= stash
->info_ptr
+ total_size
);
1589 /* FIXME: There is a problem with the contents of the
1590 .debug_info section. The 'low' and 'high' addresses of the
1591 comp_units are computed by relocs against symbols in the
1592 .text segment. We need these addresses in order to determine
1593 the nearest line number, and so we have to resolve the
1594 relocs. There is a similar problem when the .debug_line
1595 section is processed as well (e.g., there may be relocs
1596 against the operand of the DW_LNE_set_address operator).
1598 Unfortunately getting hold of the reloc information is hard...
1600 For now, this means that disassembling object files (as
1601 opposed to fully executables) does not always work as well as
1604 /* A null info_ptr indicates that there is no dwarf2 info
1605 (or that an error occured while setting up the stash). */
1606 if (! stash
->info_ptr
)
1609 /* Check the previously read comp. units first. */
1610 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
1611 if (comp_unit_contains_address (each
, addr
))
1612 return comp_unit_find_nearest_line (each
, addr
, filename_ptr
,
1613 functionname_ptr
, linenumber_ptr
,
1616 /* Read each remaining comp. units checking each as they are read. */
1617 while (stash
->info_ptr
< stash
->info_ptr_end
)
1619 struct comp_unit
* each
;
1624 length
= read_4_bytes (abfd
, stash
->info_ptr
);
1626 length
= read_8_bytes (abfd
, stash
->info_ptr
);
1627 stash
->info_ptr
+= addr_size
;
1631 each
= parse_comp_unit (abfd
, stash
, length
, addr_size
);
1632 stash
->info_ptr
+= length
;
1636 each
->next_unit
= stash
->all_comp_units
;
1637 stash
->all_comp_units
= each
;
1639 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1640 compilation units. If we don't have them (i.e.,
1641 unit->high == 0), we need to consult the line info
1642 table to see if a compilation unit contains the given
1644 if (each
->arange
.high
> 0)
1646 if (comp_unit_contains_address (each
, addr
))
1647 return comp_unit_find_nearest_line (each
, addr
,
1655 found
= comp_unit_find_nearest_line (each
, addr
,