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 .debug_abbrev section loaded into memory. */
100 char* dwarf_abbrev_buffer
;
102 /* Length of the loaded .debug_abbrev section. */
103 unsigned long dwarf_abbrev_size
;
105 /* Buffer for decode_line_info. */
106 char *dwarf_line_buffer
;
108 /* Length of the loaded .debug_line section. */
109 unsigned long dwarf_line_size
;
119 /* A minimal decoding of DWARF2 compilation units. We only decode
120 what's needed to get to the line number information. */
124 /* Chain the previously read compilation units. */
125 struct comp_unit
* next_unit
;
127 /* Keep the bdf convenient (for memory allocation). */
130 /* The lowest and higest addresses contained in this compilation
131 unit as specified in the compilation unit header. */
132 struct arange arange
;
134 /* The DW_AT_name attribute (for error messages). */
137 /* The abbrev hash table. */
138 struct abbrev_info
** abbrevs
;
140 /* Note that an error was found by comp_unit_find_nearest_line. */
143 /* The DW_AT_comp_dir attribute. */
146 /* True if there is a line number table associated with this comp. unit. */
149 /* The offset into .debug_line of the line number table. */
150 unsigned long line_offset
;
152 /* Pointer to the first child die for the comp unit. */
153 char *first_child_die_ptr
;
155 /* The end of the comp unit. */
158 /* The decoded line number, NULL if not yet decoded. */
159 struct line_info_table
* line_table
;
161 /* A list of the functions found in this comp. unit. */
162 struct funcinfo
* function_table
;
164 /* Address size for this unit - from unit header. */
165 unsigned char addr_size
;
169 The following function up to the END VERBATIM mark are
170 copied directly from dwarf2read.c. */
172 /* Read dwarf information from a buffer. */
175 read_1_byte (abfd
, buf
)
176 bfd
*abfd ATTRIBUTE_UNUSED
;
179 return bfd_get_8 (abfd
, (bfd_byte
*) buf
);
183 read_1_signed_byte (abfd
, buf
)
184 bfd
*abfd ATTRIBUTE_UNUSED
;
187 return bfd_get_signed_8 (abfd
, (bfd_byte
*) buf
);
191 read_2_bytes (abfd
, buf
)
195 return bfd_get_16 (abfd
, (bfd_byte
*) buf
);
198 #if 0 /* This is not used. */
201 read_2_signed_bytes (abfd
, buf
)
205 return bfd_get_signed_16 (abfd
, (bfd_byte
*) buf
);
211 read_4_bytes (abfd
, buf
)
215 return bfd_get_32 (abfd
, (bfd_byte
*) buf
);
218 #if 0 /* This is not used. */
221 read_4_signed_bytes (abfd
, buf
)
225 return bfd_get_signed_32 (abfd
, (bfd_byte
*) buf
);
231 read_8_bytes (abfd
, buf
)
235 return bfd_get_64 (abfd
, (bfd_byte
*) buf
);
239 read_n_bytes (abfd
, buf
, size
)
240 bfd
*abfd ATTRIBUTE_UNUSED
;
242 unsigned int size ATTRIBUTE_UNUSED
;
244 /* If the size of a host char is 8 bits, we can return a pointer
245 to the buffer, otherwise we have to copy the data to a buffer
246 allocated on the temporary obstack. */
251 read_string (abfd
, buf
, bytes_read_ptr
)
252 bfd
*abfd ATTRIBUTE_UNUSED
;
254 unsigned int *bytes_read_ptr
;
256 /* If the size of a host char is 8 bits, we can return a pointer
257 to the string, otherwise we have to copy the string to a buffer
258 allocated on the temporary obstack. */
265 *bytes_read_ptr
= strlen (buf
) + 1;
270 read_unsigned_leb128 (abfd
, buf
, bytes_read_ptr
)
271 bfd
*abfd ATTRIBUTE_UNUSED
;
273 unsigned int *bytes_read_ptr
;
276 unsigned int num_read
;
286 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
289 result
|= ((byte
& 0x7f) << shift
);
294 * bytes_read_ptr
= num_read
;
300 read_signed_leb128 (abfd
, buf
, bytes_read_ptr
)
301 bfd
*abfd ATTRIBUTE_UNUSED
;
303 unsigned int * bytes_read_ptr
;
316 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
319 result
|= ((byte
& 0x7f) << shift
);
324 if ((shift
< 32) && (byte
& 0x40))
325 result
|= -(1 << shift
);
327 * bytes_read_ptr
= num_read
;
335 read_address (unit
, buf
)
336 struct comp_unit
* unit
;
339 switch (unit
->addr_size
)
342 return bfd_get_64 (unit
->abfd
, (bfd_byte
*) buf
);
344 return bfd_get_32 (unit
->abfd
, (bfd_byte
*) buf
);
346 return bfd_get_16 (unit
->abfd
, (bfd_byte
*) buf
);
352 /* This data structure holds the information of an abbrev. */
355 unsigned int number
; /* Number identifying abbrev. */
356 enum dwarf_tag tag
; /* DWARF tag. */
357 int has_children
; /* Boolean. */
358 unsigned int num_attrs
; /* Number of attributes. */
359 struct attr_abbrev
*attrs
; /* An array of attribute descriptions. */
360 struct abbrev_info
*next
; /* Next in chain. */
365 enum dwarf_attribute name
;
366 enum dwarf_form form
;
369 #ifndef ABBREV_HASH_SIZE
370 #define ABBREV_HASH_SIZE 121
372 #ifndef ATTR_ALLOC_CHUNK
373 #define ATTR_ALLOC_CHUNK 4
376 /* Lookup an abbrev_info structure in the abbrev hash table. */
378 static struct abbrev_info
*
379 lookup_abbrev (number
,abbrevs
)
381 struct abbrev_info
**abbrevs
;
383 unsigned int hash_number
;
384 struct abbrev_info
*abbrev
;
386 hash_number
= number
% ABBREV_HASH_SIZE
;
387 abbrev
= abbrevs
[hash_number
];
391 if (abbrev
->number
== number
)
394 abbrev
= abbrev
->next
;
400 /* In DWARF version 2, the description of the debugging information is
401 stored in a separate .debug_abbrev section. Before we read any
402 dies from a section we read in all abbreviations and install them
405 static struct abbrev_info
**
406 read_abbrevs (abfd
, offset
, stash
)
409 struct dwarf2_debug
*stash
;
411 struct abbrev_info
**abbrevs
;
413 struct abbrev_info
*cur_abbrev
;
414 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
415 unsigned int abbrev_form
, hash_number
;
417 if (! stash
->dwarf_abbrev_buffer
)
421 msec
= bfd_get_section_by_name (abfd
, ".debug_abbrev");
424 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_abbrev section."));
425 bfd_set_error (bfd_error_bad_value
);
429 stash
->dwarf_abbrev_size
= msec
->_raw_size
;
430 stash
->dwarf_abbrev_buffer
= (char*) bfd_alloc (abfd
, stash
->dwarf_abbrev_size
);
431 if (! stash
->dwarf_abbrev_buffer
)
434 if (! bfd_get_section_contents (abfd
, msec
,
435 stash
->dwarf_abbrev_buffer
, 0,
436 stash
->dwarf_abbrev_size
))
440 if (offset
>= stash
->dwarf_abbrev_size
)
442 (*_bfd_error_handler
) (_("Dwarf Error: Abbrev offset (%u) greater than or equal to abbrev size (%u)."),
443 offset
, stash
->dwarf_abbrev_size
);
444 bfd_set_error (bfd_error_bad_value
);
448 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
);
450 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
451 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
452 abbrev_ptr
+= bytes_read
;
454 /* Loop until we reach an abbrev number of 0. */
455 while (abbrev_number
)
457 cur_abbrev
= (struct abbrev_info
*)bfd_zalloc (abfd
, sizeof (struct abbrev_info
));
459 /* Read in abbrev header. */
460 cur_abbrev
->number
= abbrev_number
;
461 cur_abbrev
->tag
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
462 abbrev_ptr
+= bytes_read
;
463 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
);
466 /* Now read in declarations. */
467 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
468 abbrev_ptr
+= bytes_read
;
469 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
470 abbrev_ptr
+= bytes_read
;
474 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
476 cur_abbrev
->attrs
= (struct attr_abbrev
*)
477 bfd_realloc (cur_abbrev
->attrs
,
478 (cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
)
479 * sizeof (struct attr_abbrev
));
480 if (! cur_abbrev
->attrs
)
484 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
= abbrev_name
;
485 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
++].form
= abbrev_form
;
486 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
487 abbrev_ptr
+= bytes_read
;
488 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
489 abbrev_ptr
+= bytes_read
;
492 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
493 cur_abbrev
->next
= abbrevs
[hash_number
];
494 abbrevs
[hash_number
] = cur_abbrev
;
496 /* Get next abbreviation.
497 Under Irix6 the abbreviations for a compilation unit are not
498 always properly terminated with an abbrev number of 0.
499 Exit loop if we encounter an abbreviation which we have
500 already read (which means we are about to read the abbreviations
501 for the next compile unit) or if the end of the abbreviation
503 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
504 >= stash
->dwarf_abbrev_size
)
506 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
507 abbrev_ptr
+= bytes_read
;
508 if (lookup_abbrev (abbrev_number
,abbrevs
) != NULL
)
515 /* Read an attribute described by an abbreviated attribute. */
518 read_attribute (attr
, abbrev
, unit
, info_ptr
)
519 struct attribute
*attr
;
520 struct attr_abbrev
*abbrev
;
521 struct comp_unit
*unit
;
524 bfd
*abfd
= unit
->abfd
;
525 unsigned int bytes_read
;
526 struct dwarf_block
*blk
;
528 attr
->name
= abbrev
->name
;
529 attr
->form
= abbrev
->form
;
531 switch (abbrev
->form
)
534 case DW_FORM_ref_addr
:
535 DW_ADDR (attr
) = read_address (unit
, info_ptr
);
536 info_ptr
+= unit
->addr_size
;
539 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
540 blk
->size
= read_2_bytes (abfd
, info_ptr
);
542 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
543 info_ptr
+= blk
->size
;
544 DW_BLOCK (attr
) = blk
;
547 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
548 blk
->size
= read_4_bytes (abfd
, info_ptr
);
550 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
551 info_ptr
+= blk
->size
;
552 DW_BLOCK (attr
) = blk
;
555 DW_UNSND (attr
) = read_2_bytes (abfd
, info_ptr
);
559 DW_UNSND (attr
) = read_4_bytes (abfd
, info_ptr
);
563 DW_UNSND (attr
) = read_8_bytes (abfd
, info_ptr
);
567 DW_STRING (attr
) = read_string (abfd
, info_ptr
, &bytes_read
);
568 info_ptr
+= bytes_read
;
571 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
572 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
573 info_ptr
+= bytes_read
;
574 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
575 info_ptr
+= blk
->size
;
576 DW_BLOCK (attr
) = blk
;
579 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
580 blk
->size
= read_1_byte (abfd
, info_ptr
);
582 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
583 info_ptr
+= blk
->size
;
584 DW_BLOCK (attr
) = blk
;
587 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
591 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
595 DW_SND (attr
) = read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
596 info_ptr
+= bytes_read
;
599 DW_UNSND (attr
) = read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
600 info_ptr
+= bytes_read
;
603 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
607 DW_UNSND (attr
) = read_2_bytes (abfd
, info_ptr
);
611 DW_UNSND (attr
) = read_4_bytes (abfd
, info_ptr
);
615 DW_UNSND (attr
) = read_8_bytes (abfd
, info_ptr
);
618 case DW_FORM_ref_udata
:
619 DW_UNSND (attr
) = read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
620 info_ptr
+= bytes_read
;
623 case DW_FORM_indirect
:
625 (*_bfd_error_handler
) (_("Dwarf Error: Invalid or unhandled FORM value: %d."),
627 bfd_set_error (bfd_error_bad_value
);
632 /* Source line information table routines. */
634 #define FILE_ALLOC_CHUNK 5
635 #define DIR_ALLOC_CHUNK 5
639 struct line_info
* prev_line
;
644 int end_sequence
; /* End of (sequential) code sequence. */
655 struct line_info_table
658 unsigned int num_files
;
659 unsigned int num_dirs
;
662 struct fileinfo
* files
;
663 struct line_info
* last_line
;
667 add_line_info (table
, address
, filename
, line
, column
, end_sequence
)
668 struct line_info_table
* table
;
675 struct line_info
* info
= (struct line_info
*)
676 bfd_alloc (table
->abfd
, sizeof (struct line_info
));
678 info
->prev_line
= table
->last_line
;
679 table
->last_line
= info
;
681 info
->address
= address
;
682 info
->filename
= filename
;
684 info
->column
= column
;
685 info
->end_sequence
= end_sequence
;
689 concat_filename (table
, file
)
690 struct line_info_table
* table
;
695 if (file
- 1 >= table
->num_files
)
697 (*_bfd_error_handler
)
698 (_("Dwarf Error: mangled line number section (bad file number)."));
702 filename
= table
->files
[file
- 1].name
;
703 if (IS_ABSOLUTE_PATH(filename
))
708 char* dirname
= (table
->files
[file
- 1].dir
709 ? table
->dirs
[table
->files
[file
- 1].dir
- 1]
711 return (char*) concat (dirname
, "/", filename
, NULL
);
716 arange_add (unit
, low_pc
, high_pc
)
717 struct comp_unit
*unit
;
721 struct arange
*arange
;
723 /* First see if we can cheaply extend an existing range. */
724 arange
= &unit
->arange
;
728 if (low_pc
== arange
->high
)
730 arange
->high
= high_pc
;
733 if (high_pc
== arange
->low
)
735 arange
->low
= low_pc
;
738 arange
= arange
->next
;
742 if (unit
->arange
.high
== 0)
744 /* This is the first address range: store it in unit->arange. */
745 unit
->arange
.next
= 0;
746 unit
->arange
.low
= low_pc
;
747 unit
->arange
.high
= high_pc
;
751 /* Need to allocate a new arange and insert it into the arange list. */
752 arange
= bfd_zalloc (unit
->abfd
, sizeof (*arange
));
753 arange
->low
= low_pc
;
754 arange
->high
= high_pc
;
756 arange
->next
= unit
->arange
.next
;
757 unit
->arange
.next
= arange
;
760 /* Decode the line number information for UNIT. */
762 static struct line_info_table
*
763 decode_line_info (unit
, stash
)
764 struct comp_unit
*unit
;
765 struct dwarf2_debug
*stash
;
767 bfd
*abfd
= unit
->abfd
;
768 struct line_info_table
* table
;
772 unsigned int i
, bytes_read
;
773 char *cur_file
, *cur_dir
;
774 unsigned char op_code
, extended_op
, adj_opcode
;
776 if (! stash
->dwarf_line_buffer
)
780 msec
= bfd_get_section_by_name (abfd
, ".debug_line");
783 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_line section."));
784 bfd_set_error (bfd_error_bad_value
);
788 stash
->dwarf_line_size
= msec
->_raw_size
;
789 stash
->dwarf_line_buffer
= (char *) bfd_alloc (abfd
, stash
->dwarf_line_size
);
790 if (! stash
->dwarf_line_buffer
)
793 if (! bfd_get_section_contents (abfd
, msec
,
794 stash
->dwarf_line_buffer
, 0,
795 stash
->dwarf_line_size
))
798 /* FIXME: We ought to apply the relocs against this section before
802 /* Since we are using un-relocated data, it is possible to get a bad value
803 for the line_offset. Validate it here so that we won't get a segfault
805 if (unit
->line_offset
>= stash
->dwarf_line_size
)
807 (*_bfd_error_handler
) (_("Dwarf Error: Line offset (%u) greater than or equal to line size (%u)."),
808 unit
->line_offset
, stash
->dwarf_line_size
);
809 bfd_set_error (bfd_error_bad_value
);
813 table
= (struct line_info_table
*) bfd_alloc (abfd
,
814 sizeof (struct line_info_table
));
816 table
->comp_dir
= unit
->comp_dir
;
818 table
->num_files
= 0;
825 table
->last_line
= NULL
;
827 line_ptr
= stash
->dwarf_line_buffer
+ unit
->line_offset
;
829 /* Read in the prologue. */
830 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
832 line_end
= line_ptr
+ lh
.total_length
;
833 lh
.version
= read_2_bytes (abfd
, line_ptr
);
835 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
);
837 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
);
839 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
);
841 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
);
843 lh
.line_range
= read_1_byte (abfd
, line_ptr
);
845 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
);
847 lh
.standard_opcode_lengths
= (unsigned char *)
848 bfd_alloc (abfd
, lh
.opcode_base
* sizeof (unsigned char));
850 lh
.standard_opcode_lengths
[0] = 1;
852 for (i
= 1; i
< lh
.opcode_base
; ++i
)
854 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
);
858 /* Read directory table. */
859 while ((cur_dir
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
861 line_ptr
+= bytes_read
;
863 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
865 table
->dirs
= (char **)
866 bfd_realloc (table
->dirs
,
867 (table
->num_dirs
+ DIR_ALLOC_CHUNK
) * sizeof (char *));
872 table
->dirs
[table
->num_dirs
++] = cur_dir
;
875 line_ptr
+= bytes_read
;
877 /* Read file name table. */
878 while ((cur_file
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
880 line_ptr
+= bytes_read
;
882 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
884 table
->files
= (struct fileinfo
*)
885 bfd_realloc (table
->files
,
886 (table
->num_files
+ FILE_ALLOC_CHUNK
)
887 * sizeof (struct fileinfo
));
892 table
->files
[table
->num_files
].name
= cur_file
;
893 table
->files
[table
->num_files
].dir
=
894 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
895 line_ptr
+= bytes_read
;
896 table
->files
[table
->num_files
].time
=
897 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
898 line_ptr
+= bytes_read
;
899 table
->files
[table
->num_files
].size
=
900 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
901 line_ptr
+= bytes_read
;
905 line_ptr
+= bytes_read
;
907 /* Read the statement sequences until there's nothing left. */
908 while (line_ptr
< line_end
)
910 /* State machine registers. */
912 char* filename
= concat_filename (table
, 1);
913 unsigned int line
= 1;
914 unsigned int column
= 0;
915 int is_stmt
= lh
.default_is_stmt
;
917 int end_sequence
= 0, need_low_pc
= 1;
920 /* Decode the table. */
921 while (! end_sequence
)
923 op_code
= read_1_byte (abfd
, line_ptr
);
928 case DW_LNS_extended_op
:
929 line_ptr
+= 1; /* Ignore length. */
930 extended_op
= read_1_byte (abfd
, line_ptr
);
934 case DW_LNE_end_sequence
:
936 add_line_info (table
, address
, filename
, line
, column
,
943 arange_add (unit
, low_pc
, address
);
945 case DW_LNE_set_address
:
946 address
= read_address (unit
, line_ptr
);
947 line_ptr
+= unit
->addr_size
;
949 case DW_LNE_define_file
:
950 cur_file
= read_string (abfd
, line_ptr
, &bytes_read
);
951 line_ptr
+= bytes_read
;
952 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
954 table
->files
= (struct fileinfo
*)
955 bfd_realloc (table
->files
,
956 (table
->num_files
+ FILE_ALLOC_CHUNK
)
957 * sizeof (struct fileinfo
));
961 table
->files
[table
->num_files
].name
= cur_file
;
962 table
->files
[table
->num_files
].dir
=
963 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
964 line_ptr
+= bytes_read
;
965 table
->files
[table
->num_files
].time
=
966 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
967 line_ptr
+= bytes_read
;
968 table
->files
[table
->num_files
].size
=
969 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
970 line_ptr
+= bytes_read
;
974 (*_bfd_error_handler
) (_("Dwarf Error: mangled line number section."));
975 bfd_set_error (bfd_error_bad_value
);
980 add_line_info (table
, address
, filename
, line
, column
, 0);
988 case DW_LNS_advance_pc
:
989 address
+= lh
.minimum_instruction_length
990 * read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
991 line_ptr
+= bytes_read
;
993 case DW_LNS_advance_line
:
994 line
+= read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
995 line_ptr
+= bytes_read
;
997 case DW_LNS_set_file
:
1001 /* The file and directory tables are 0 based, the references
1003 file
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1004 line_ptr
+= bytes_read
;
1005 filename
= concat_filename (table
, file
);
1008 case DW_LNS_set_column
:
1009 column
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1010 line_ptr
+= bytes_read
;
1012 case DW_LNS_negate_stmt
:
1013 is_stmt
= (!is_stmt
);
1015 case DW_LNS_set_basic_block
:
1018 case DW_LNS_const_add_pc
:
1019 address
+= lh
.minimum_instruction_length
1020 * ((255 - lh
.opcode_base
) / lh
.line_range
);
1022 case DW_LNS_fixed_advance_pc
:
1023 address
+= read_2_bytes (abfd
, line_ptr
);
1026 default: /* Special operand. */
1027 adj_opcode
= op_code
- lh
.opcode_base
;
1028 address
+= (adj_opcode
/ lh
.line_range
)
1029 * lh
.minimum_instruction_length
;
1030 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
1031 /* Append row to matrix using current values. */
1032 add_line_info (table
, address
, filename
, line
, column
, 0);
1046 /* If ADDR is within TABLE set the output parameters and return true,
1047 otherwise return false. The output parameters, FILENAME_PTR and
1048 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1051 lookup_address_in_line_info_table (table
,
1055 struct line_info_table
* table
;
1057 const char **filename_ptr
;
1058 unsigned int *linenumber_ptr
;
1060 struct line_info
* next_line
= table
->last_line
;
1061 struct line_info
* each_line
;
1066 each_line
= next_line
->prev_line
;
1068 while (each_line
&& next_line
)
1070 if (!each_line
->end_sequence
1071 && addr
>= each_line
->address
&& addr
< next_line
->address
)
1073 *filename_ptr
= each_line
->filename
;
1074 *linenumber_ptr
= each_line
->line
;
1077 next_line
= each_line
;
1078 each_line
= each_line
->prev_line
;
1084 /* Function table functions. */
1088 struct funcinfo
*prev_func
;
1094 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
1097 lookup_address_in_function_table (table
,
1100 struct funcinfo
* table
;
1102 const char **functionname_ptr
;
1104 struct funcinfo
* each_func
;
1106 for (each_func
= table
;
1108 each_func
= each_func
->prev_func
)
1110 if (addr
>= each_func
->low
&& addr
< each_func
->high
)
1112 *functionname_ptr
= each_func
->name
;
1120 /* DWARF2 Compilation unit functions. */
1122 /* Scan over each die in a comp. unit looking for functions to add
1123 to the function table. */
1126 scan_unit_for_functions (unit
)
1127 struct comp_unit
*unit
;
1129 bfd
*abfd
= unit
->abfd
;
1130 char *info_ptr
= unit
->first_child_die_ptr
;
1131 int nesting_level
= 1;
1133 while (nesting_level
)
1135 unsigned int abbrev_number
, bytes_read
, i
;
1136 struct abbrev_info
*abbrev
;
1137 struct attribute attr
;
1138 struct funcinfo
*func
;
1141 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1142 info_ptr
+= bytes_read
;
1144 if (! abbrev_number
)
1150 abbrev
= lookup_abbrev (abbrev_number
,unit
->abbrevs
);
1153 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %d."),
1155 bfd_set_error (bfd_error_bad_value
);
1159 if (abbrev
->tag
== DW_TAG_subprogram
)
1161 func
= (struct funcinfo
*) bfd_zalloc (abfd
, sizeof (struct funcinfo
));
1162 func
->prev_func
= unit
->function_table
;
1163 unit
->function_table
= func
;
1168 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1170 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1178 name
= DW_STRING (&attr
);
1180 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1181 if (func
->name
== NULL
)
1182 func
->name
= DW_STRING (&attr
);
1185 case DW_AT_MIPS_linkage_name
:
1186 func
->name
= DW_STRING (&attr
);
1190 func
->low
= DW_ADDR (&attr
);
1194 func
->high
= DW_ADDR (&attr
);
1206 name
= DW_STRING (&attr
);
1215 if (abbrev
->has_children
)
1222 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1223 includes the compilation unit header that proceeds the DIE's, but
1224 does not include the length field that preceeds each compilation
1225 unit header. END_PTR points one past the end of this comp unit.
1226 If ABBREV_LENGTH is 0, then the length of the abbreviation offset
1227 is assumed to be four bytes. Otherwise, it it is the size given.
1229 This routine does not read the whole compilation unit; only enough
1230 to get to the line number information for the compilation unit. */
1232 static struct comp_unit
*
1233 parse_comp_unit (abfd
, stash
, unit_length
, abbrev_length
)
1235 struct dwarf2_debug
*stash
;
1236 bfd_vma unit_length
;
1237 unsigned int abbrev_length
;
1239 struct comp_unit
* unit
;
1241 unsigned short version
;
1242 unsigned int abbrev_offset
= 0;
1243 unsigned char addr_size
;
1244 struct abbrev_info
** abbrevs
;
1246 unsigned int abbrev_number
, bytes_read
, i
;
1247 struct abbrev_info
*abbrev
;
1248 struct attribute attr
;
1250 char *info_ptr
= stash
->info_ptr
;
1251 char *end_ptr
= info_ptr
+ unit_length
;
1253 version
= read_2_bytes (abfd
, info_ptr
);
1255 BFD_ASSERT (abbrev_length
== 0
1256 || abbrev_length
== 4
1257 || abbrev_length
== 8);
1258 if (abbrev_length
== 0 || abbrev_length
== 4)
1259 abbrev_offset
= read_4_bytes (abfd
, info_ptr
);
1260 else if (abbrev_length
== 8)
1261 abbrev_offset
= read_8_bytes (abfd
, info_ptr
);
1262 info_ptr
+= abbrev_length
;
1263 addr_size
= read_1_byte (abfd
, info_ptr
);
1268 (*_bfd_error_handler
) (_("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information."), version
);
1269 bfd_set_error (bfd_error_bad_value
);
1273 if (addr_size
> sizeof (bfd_vma
))
1275 (*_bfd_error_handler
) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1278 bfd_set_error (bfd_error_bad_value
);
1282 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
1284 (*_bfd_error_handler
) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size
);
1285 bfd_set_error (bfd_error_bad_value
);
1289 /* Read the abbrevs for this compilation unit into a table. */
1290 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
);
1294 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1295 info_ptr
+= bytes_read
;
1296 if (! abbrev_number
)
1298 (*_bfd_error_handler
) (_("Dwarf Error: Bad abbrev number: %d."),
1300 bfd_set_error (bfd_error_bad_value
);
1304 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
1307 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %d."),
1309 bfd_set_error (bfd_error_bad_value
);
1313 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, sizeof (struct comp_unit
));
1315 unit
->addr_size
= addr_size
;
1316 unit
->abbrevs
= abbrevs
;
1317 unit
->end_ptr
= end_ptr
;
1319 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1321 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1323 /* Store the data if it is of an attribute we want to keep in a
1324 partial symbol table. */
1327 case DW_AT_stmt_list
:
1329 unit
->line_offset
= DW_UNSND (&attr
);
1333 unit
->name
= DW_STRING (&attr
);
1337 unit
->arange
.low
= DW_ADDR (&attr
);
1341 unit
->arange
.high
= DW_ADDR (&attr
);
1344 case DW_AT_comp_dir
:
1346 char* comp_dir
= DW_STRING (&attr
);
1349 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1350 directory, get rid of it. */
1351 char *cp
= (char*) strchr (comp_dir
, ':');
1353 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
1356 unit
->comp_dir
= comp_dir
;
1365 unit
->first_child_die_ptr
= info_ptr
;
1369 /* Return true if UNIT contains the address given by ADDR. */
1372 comp_unit_contains_address (unit
, addr
)
1373 struct comp_unit
* unit
;
1376 struct arange
*arange
;
1381 arange
= &unit
->arange
;
1384 if (addr
>= arange
->low
&& addr
< arange
->high
)
1386 arange
= arange
->next
;
1393 /* If UNIT contains ADDR, set the output parameters to the values for
1394 the line containing ADDR. The output parameters, FILENAME_PTR,
1395 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1398 Return true of UNIT contains ADDR, and no errors were encountered;
1402 comp_unit_find_nearest_line (unit
, addr
,
1403 filename_ptr
, functionname_ptr
, linenumber_ptr
,
1405 struct comp_unit
* unit
;
1407 const char **filename_ptr
;
1408 const char **functionname_ptr
;
1409 unsigned int *linenumber_ptr
;
1410 struct dwarf2_debug
*stash
;
1418 if (! unit
->line_table
)
1420 if (! unit
->stmtlist
)
1426 unit
->line_table
= decode_line_info (unit
, stash
);
1428 if (! unit
->line_table
)
1434 if (! scan_unit_for_functions (unit
))
1441 line_p
= lookup_address_in_line_info_table (unit
->line_table
,
1445 func_p
= lookup_address_in_function_table (unit
->function_table
,
1448 return line_p
|| func_p
;
1451 /* Locate a section in a BFD containing debugging info. The search starts from the
1452 section after AFTER_SEC, or from the first section in the BFD if AFTER_SEC is
1453 NULL. The search works by examining the names of the sections. There are two
1454 permissiable names. The first is .debug_info. This is the standard DWARF2 name.
1455 The second is a prefix .gnu.linkonce.wi. This is a variation on the .debug_info
1456 section which has a checksum describing the contents appended onto the name. This
1457 allows the linker to identify and discard duplicate debugging sections for
1458 different compilation units. */
1459 #define DWARF2_DEBUG_INFO ".debug_info"
1460 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1463 find_debug_info (abfd
, after_sec
)
1465 asection
* after_sec
;
1470 msec
= after_sec
->next
;
1472 msec
= abfd
->sections
;
1476 if (strcmp (msec
->name
, DWARF2_DEBUG_INFO
) == 0)
1479 if (strncmp (msec
->name
, GNU_LINKONCE_INFO
, strlen (GNU_LINKONCE_INFO
)) == 0)
1488 /* The DWARF2 version of find_nearest line. Return true if the line
1489 is found without error. ADDR_SIZE is the number of bytes in the
1490 initial .debug_info length field and in the abbreviation offset.
1491 You may use zero to indicate that the default value should be
1495 _bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
1496 filename_ptr
, functionname_ptr
,
1501 asymbol
**symbols ATTRIBUTE_UNUSED
;
1503 const char **filename_ptr
;
1504 const char **functionname_ptr
;
1505 unsigned int *linenumber_ptr
;
1506 unsigned int addr_size
;
1509 /* Read each compilation unit from the section .debug_info, and check
1510 to see if it contains the address we are searching for. If yes,
1511 lookup the address, and return the line number info. If no, go
1512 on to the next compilation unit.
1514 We keep a list of all the previously read compilation units, and
1515 a pointer to the next un-read compilation unit. Check the
1516 previously read units before reading more. */
1517 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
1519 /* What address are we looking for? */
1520 bfd_vma addr
= offset
+ section
->vma
;
1522 struct comp_unit
* each
;
1524 *filename_ptr
= NULL
;
1525 *functionname_ptr
= NULL
;
1526 *linenumber_ptr
= 0;
1528 /* The DWARF2 spec says that the initial length field, and the
1529 offset of the abbreviation table, should both be 4-byte values.
1530 However, some compilers do things differently. */
1533 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
1537 unsigned long total_size
;
1541 (struct dwarf2_debug
*) bfd_zalloc (abfd
, sizeof (struct dwarf2_debug
));
1545 *pinfo
= (PTR
) stash
;
1547 msec
= find_debug_info (abfd
, NULL
);
1549 /* No dwarf2 info. Note that at this point the stash
1550 has been allocated, but contains zeros, this lets
1551 future calls to this function fail quicker. */
1554 /* There can be more than one DWARF2 info section in a BFD these days.
1555 Read them all in and produce one large stash. We do this in two
1556 passes - in the first pass we just accumulate the section sizes.
1557 In the second pass we read in the section's contents. The allows
1558 us to avoid reallocing the data as we add sections to the stash. */
1559 for (total_size
= 0; msec
; msec
= find_debug_info (abfd
, msec
))
1560 total_size
+= msec
->_raw_size
;
1562 stash
->info_ptr
= (char *) bfd_alloc (abfd
, total_size
);
1563 if (stash
->info_ptr
== NULL
)
1566 stash
->info_ptr_end
= stash
->info_ptr
;
1568 for (msec
= find_debug_info (abfd
, NULL
);
1570 msec
= find_debug_info (abfd
, msec
))
1573 unsigned long start
;
1575 size
= msec
->_raw_size
;
1579 start
= stash
->info_ptr_end
- stash
->info_ptr
;
1581 if (! bfd_get_section_contents (abfd
, msec
, stash
->info_ptr
+ start
, 0, size
))
1584 stash
->info_ptr_end
= stash
->info_ptr
+ start
+ size
;
1587 BFD_ASSERT (stash
->info_ptr_end
= stash
->info_ptr
+ total_size
);
1590 /* FIXME: There is a problem with the contents of the
1591 .debug_info section. The 'low' and 'high' addresses of the
1592 comp_units are computed by relocs against symbols in the
1593 .text segment. We need these addresses in order to determine
1594 the nearest line number, and so we have to resolve the
1595 relocs. There is a similar problem when the .debug_line
1596 section is processed as well (e.g., there may be relocs
1597 against the operand of the DW_LNE_set_address operator).
1599 Unfortunately getting hold of the reloc information is hard...
1601 For now, this means that disassembling object files (as
1602 opposed to fully executables) does not always work as well as
1605 /* A null info_ptr indicates that there is no dwarf2 info
1606 (or that an error occured while setting up the stash). */
1607 if (! stash
->info_ptr
)
1610 /* Check the previously read comp. units first. */
1611 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
1612 if (comp_unit_contains_address (each
, addr
))
1613 return comp_unit_find_nearest_line (each
, addr
, filename_ptr
,
1614 functionname_ptr
, linenumber_ptr
,
1617 /* Read each remaining comp. units checking each as they are read. */
1618 while (stash
->info_ptr
< stash
->info_ptr_end
)
1620 struct comp_unit
* each
;
1625 length
= read_4_bytes (abfd
, stash
->info_ptr
);
1627 length
= read_8_bytes (abfd
, stash
->info_ptr
);
1628 stash
->info_ptr
+= addr_size
;
1632 each
= parse_comp_unit (abfd
, stash
, length
, addr_size
);
1633 stash
->info_ptr
+= length
;
1637 each
->next_unit
= stash
->all_comp_units
;
1638 stash
->all_comp_units
= each
;
1640 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1641 compilation units. If we don't have them (i.e.,
1642 unit->high == 0), we need to consult the line info
1643 table to see if a compilation unit contains the given
1645 if (each
->arange
.high
> 0)
1647 if (comp_unit_contains_address (each
, addr
))
1648 return comp_unit_find_nearest_line (each
, addr
,
1656 found
= comp_unit_find_nearest_line (each
, addr
,