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
)
409 struct abbrev_info
**abbrevs
;
411 struct abbrev_info
*cur_abbrev
;
412 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
413 unsigned int abbrev_form
, hash_number
;
414 struct dwarf2_debug
*stash
;
416 stash
= elf_tdata(abfd
)->dwarf2_find_line_info
;
418 if (! stash
->dwarf_abbrev_buffer
)
422 msec
= bfd_get_section_by_name (abfd
, ".debug_abbrev");
425 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_abbrev section."));
426 bfd_set_error (bfd_error_bad_value
);
430 stash
->dwarf_abbrev_size
= msec
->_raw_size
;
431 stash
->dwarf_abbrev_buffer
= (char*) bfd_alloc (abfd
, stash
->dwarf_abbrev_size
);
432 if (! stash
->dwarf_abbrev_buffer
)
435 if (! bfd_get_section_contents (abfd
, msec
,
436 stash
->dwarf_abbrev_buffer
, 0,
437 stash
->dwarf_abbrev_size
))
441 if (offset
> stash
->dwarf_abbrev_size
)
443 (*_bfd_error_handler
) (_("Dwarf Error: Abbrev offset (%u) bigger than abbrev size (%u)."),
444 offset
, stash
->dwarf_abbrev_size
);
445 bfd_set_error (bfd_error_bad_value
);
449 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, sizeof(struct abbrev_info
*) * ABBREV_HASH_SIZE
);
451 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
452 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
453 abbrev_ptr
+= bytes_read
;
455 /* Loop until we reach an abbrev number of 0. */
456 while (abbrev_number
)
458 cur_abbrev
= (struct abbrev_info
*)bfd_zalloc (abfd
, sizeof (struct abbrev_info
));
460 /* Read in abbrev header. */
461 cur_abbrev
->number
= abbrev_number
;
462 cur_abbrev
->tag
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
463 abbrev_ptr
+= bytes_read
;
464 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
);
467 /* Now read in declarations. */
468 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
469 abbrev_ptr
+= bytes_read
;
470 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
471 abbrev_ptr
+= bytes_read
;
475 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
477 cur_abbrev
->attrs
= (struct attr_abbrev
*)
478 bfd_realloc (cur_abbrev
->attrs
,
479 (cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
)
480 * sizeof (struct attr_abbrev
));
481 if (! cur_abbrev
->attrs
)
485 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
= abbrev_name
;
486 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
++].form
= abbrev_form
;
487 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
488 abbrev_ptr
+= bytes_read
;
489 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
490 abbrev_ptr
+= bytes_read
;
493 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
494 cur_abbrev
->next
= abbrevs
[hash_number
];
495 abbrevs
[hash_number
] = cur_abbrev
;
497 /* Get next abbreviation.
498 Under Irix6 the abbreviations for a compilation unit are not
499 always properly terminated with an abbrev number of 0.
500 Exit loop if we encounter an abbreviation which we have
501 already read (which means we are about to read the abbreviations
502 for the next compile unit) or if the end of the abbreviation
504 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
505 >= stash
->dwarf_abbrev_size
)
507 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
508 abbrev_ptr
+= bytes_read
;
509 if (lookup_abbrev (abbrev_number
,abbrevs
) != NULL
)
516 /* Read an attribute described by an abbreviated attribute. */
519 read_attribute (attr
, abbrev
, unit
, info_ptr
)
520 struct attribute
*attr
;
521 struct attr_abbrev
*abbrev
;
522 struct comp_unit
*unit
;
525 bfd
*abfd
= unit
->abfd
;
526 unsigned int bytes_read
;
527 struct dwarf_block
*blk
;
529 attr
->name
= abbrev
->name
;
530 attr
->form
= abbrev
->form
;
532 switch (abbrev
->form
)
535 case DW_FORM_ref_addr
:
536 DW_ADDR (attr
) = read_address (unit
, info_ptr
);
537 info_ptr
+= unit
->addr_size
;
540 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
541 blk
->size
= read_2_bytes (abfd
, info_ptr
);
543 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
544 info_ptr
+= blk
->size
;
545 DW_BLOCK (attr
) = blk
;
548 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
549 blk
->size
= read_4_bytes (abfd
, info_ptr
);
551 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
552 info_ptr
+= blk
->size
;
553 DW_BLOCK (attr
) = blk
;
556 DW_UNSND (attr
) = read_2_bytes (abfd
, info_ptr
);
560 DW_UNSND (attr
) = read_4_bytes (abfd
, info_ptr
);
564 DW_UNSND (attr
) = read_8_bytes (abfd
, info_ptr
);
568 DW_STRING (attr
) = read_string (abfd
, info_ptr
, &bytes_read
);
569 info_ptr
+= bytes_read
;
572 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
573 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
574 info_ptr
+= bytes_read
;
575 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
576 info_ptr
+= blk
->size
;
577 DW_BLOCK (attr
) = blk
;
580 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
581 blk
->size
= read_1_byte (abfd
, info_ptr
);
583 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
584 info_ptr
+= blk
->size
;
585 DW_BLOCK (attr
) = blk
;
588 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
592 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
596 DW_SND (attr
) = read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
597 info_ptr
+= bytes_read
;
600 DW_UNSND (attr
) = read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
601 info_ptr
+= bytes_read
;
604 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
608 DW_UNSND (attr
) = read_2_bytes (abfd
, info_ptr
);
612 DW_UNSND (attr
) = read_4_bytes (abfd
, info_ptr
);
616 DW_UNSND (attr
) = read_8_bytes (abfd
, info_ptr
);
619 case DW_FORM_ref_udata
:
620 DW_UNSND (attr
) = read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
621 info_ptr
+= bytes_read
;
624 case DW_FORM_indirect
:
626 (*_bfd_error_handler
) (_("Dwarf Error: Invalid or unhandled FORM value: %d."),
628 bfd_set_error (bfd_error_bad_value
);
633 /* Source line information table routines. */
635 #define FILE_ALLOC_CHUNK 5
636 #define DIR_ALLOC_CHUNK 5
640 struct line_info
* prev_line
;
645 int end_sequence
; /* End of (sequential) code sequence. */
656 struct line_info_table
659 unsigned int num_files
;
660 unsigned int num_dirs
;
663 struct fileinfo
* files
;
664 struct line_info
* last_line
;
668 add_line_info (table
, address
, filename
, line
, column
, end_sequence
)
669 struct line_info_table
* table
;
676 struct line_info
* info
= (struct line_info
*)
677 bfd_alloc (table
->abfd
, sizeof (struct line_info
));
679 info
->prev_line
= table
->last_line
;
680 table
->last_line
= info
;
682 info
->address
= address
;
683 info
->filename
= filename
;
685 info
->column
= column
;
686 info
->end_sequence
= end_sequence
;
690 concat_filename (table
, file
)
691 struct line_info_table
* table
;
696 if (file
- 1 >= table
->num_files
)
698 (*_bfd_error_handler
)
699 (_("Dwarf Error: mangled line number section (bad file number)."));
703 filename
= table
->files
[file
- 1].name
;
704 if (*filename
== '/')
709 char* dirname
= (table
->files
[file
- 1].dir
710 ? table
->dirs
[table
->files
[file
- 1].dir
- 1]
712 return (char*) concat (dirname
, "/", filename
, NULL
);
717 arange_add (unit
, low_pc
, high_pc
)
718 struct comp_unit
*unit
;
722 struct arange
*arange
;
724 /* First see if we can cheaply extend an existing range. */
725 arange
= &unit
->arange
;
729 if (low_pc
== arange
->high
)
731 arange
->high
= high_pc
;
734 if (high_pc
== arange
->low
)
736 arange
->low
= low_pc
;
739 arange
= arange
->next
;
743 if (unit
->arange
.high
== 0)
745 /* This is the first address range: store it in unit->arange. */
746 unit
->arange
.next
= 0;
747 unit
->arange
.low
= low_pc
;
748 unit
->arange
.high
= high_pc
;
752 /* Need to allocate a new arange and insert it into the arange list. */
753 arange
= bfd_zalloc (unit
->abfd
, sizeof (*arange
));
754 arange
->low
= low_pc
;
755 arange
->high
= high_pc
;
757 arange
->next
= unit
->arange
.next
;
758 unit
->arange
.next
= arange
;
761 /* Decode the line number information for UNIT. */
763 static struct line_info_table
*
764 decode_line_info (unit
)
765 struct comp_unit
*unit
;
767 bfd
*abfd
= unit
->abfd
;
768 struct dwarf2_debug
*stash
;
769 struct line_info_table
* table
;
773 unsigned int i
, bytes_read
;
774 char *cur_file
, *cur_dir
;
775 unsigned char op_code
, extended_op
, adj_opcode
;
777 stash
= elf_tdata (abfd
)->dwarf2_find_line_info
;
779 if (! stash
->dwarf_line_buffer
)
783 msec
= bfd_get_section_by_name (abfd
, ".debug_line");
786 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_line section."));
787 bfd_set_error (bfd_error_bad_value
);
791 stash
->dwarf_line_size
= msec
->_raw_size
;
792 stash
->dwarf_line_buffer
= (char *) bfd_alloc (abfd
, stash
->dwarf_line_size
);
793 if (! stash
->dwarf_line_buffer
)
796 if (! bfd_get_section_contents (abfd
, msec
,
797 stash
->dwarf_line_buffer
, 0,
798 stash
->dwarf_line_size
))
801 /* FIXME: We ought to apply the relocs against this section before
805 /* Since we are using un-relocated data, it is possible to get a bad value
806 for the line_offset. Validate it here so that we won't get a segfault
808 if (unit
->line_offset
>= stash
->dwarf_line_size
)
810 (*_bfd_error_handler
) (_("Dwarf Error: Line offset (%u) bigger than line size (%u)."),
811 unit
->line_offset
, stash
->dwarf_line_size
);
812 bfd_set_error (bfd_error_bad_value
);
816 table
= (struct line_info_table
*) bfd_alloc (abfd
,
817 sizeof (struct line_info_table
));
819 table
->comp_dir
= unit
->comp_dir
;
821 table
->num_files
= 0;
828 table
->last_line
= NULL
;
830 line_ptr
= stash
->dwarf_line_buffer
+ unit
->line_offset
;
832 /* Read in the prologue. */
833 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
835 line_end
= line_ptr
+ lh
.total_length
;
836 lh
.version
= read_2_bytes (abfd
, line_ptr
);
838 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
);
840 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
);
842 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
);
844 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
);
846 lh
.line_range
= read_1_byte (abfd
, line_ptr
);
848 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
);
850 lh
.standard_opcode_lengths
= (unsigned char *)
851 bfd_alloc (abfd
, lh
.opcode_base
* sizeof (unsigned char));
853 lh
.standard_opcode_lengths
[0] = 1;
855 for (i
= 1; i
< lh
.opcode_base
; ++i
)
857 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
);
861 /* Read directory table. */
862 while ((cur_dir
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
864 line_ptr
+= bytes_read
;
866 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
868 table
->dirs
= (char **)
869 bfd_realloc (table
->dirs
,
870 (table
->num_dirs
+ DIR_ALLOC_CHUNK
) * sizeof (char *));
875 table
->dirs
[table
->num_dirs
++] = cur_dir
;
878 line_ptr
+= bytes_read
;
880 /* Read file name table. */
881 while ((cur_file
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
883 line_ptr
+= bytes_read
;
885 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
887 table
->files
= (struct fileinfo
*)
888 bfd_realloc (table
->files
,
889 (table
->num_files
+ FILE_ALLOC_CHUNK
)
890 * sizeof (struct fileinfo
));
895 table
->files
[table
->num_files
].name
= cur_file
;
896 table
->files
[table
->num_files
].dir
=
897 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
898 line_ptr
+= bytes_read
;
899 table
->files
[table
->num_files
].time
=
900 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
901 line_ptr
+= bytes_read
;
902 table
->files
[table
->num_files
].size
=
903 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
904 line_ptr
+= bytes_read
;
908 line_ptr
+= bytes_read
;
910 /* Read the statement sequences until there's nothing left. */
911 while (line_ptr
< line_end
)
913 /* State machine registers. */
915 char* filename
= concat_filename (table
, 1);
916 unsigned int line
= 1;
917 unsigned int column
= 0;
918 int is_stmt
= lh
.default_is_stmt
;
920 int end_sequence
= 0, need_low_pc
= 1;
923 /* Decode the table. */
924 while (! end_sequence
)
926 op_code
= read_1_byte (abfd
, line_ptr
);
931 case DW_LNS_extended_op
:
932 line_ptr
+= 1; /* Ignore length. */
933 extended_op
= read_1_byte (abfd
, line_ptr
);
937 case DW_LNE_end_sequence
:
939 add_line_info (table
, address
, filename
, line
, column
,
946 arange_add (unit
, low_pc
, address
);
948 case DW_LNE_set_address
:
949 address
= read_address (unit
, line_ptr
);
950 line_ptr
+= unit
->addr_size
;
952 case DW_LNE_define_file
:
953 cur_file
= read_string (abfd
, line_ptr
, &bytes_read
);
954 line_ptr
+= bytes_read
;
955 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
957 table
->files
= (struct fileinfo
*)
958 bfd_realloc (table
->files
,
959 (table
->num_files
+ FILE_ALLOC_CHUNK
)
960 * sizeof (struct fileinfo
));
964 table
->files
[table
->num_files
].name
= cur_file
;
965 table
->files
[table
->num_files
].dir
=
966 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
967 line_ptr
+= bytes_read
;
968 table
->files
[table
->num_files
].time
=
969 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
970 line_ptr
+= bytes_read
;
971 table
->files
[table
->num_files
].size
=
972 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
973 line_ptr
+= bytes_read
;
977 (*_bfd_error_handler
) (_("Dwarf Error: mangled line number section."));
978 bfd_set_error (bfd_error_bad_value
);
983 add_line_info (table
, address
, filename
, line
, column
, 0);
991 case DW_LNS_advance_pc
:
992 address
+= lh
.minimum_instruction_length
993 * read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
994 line_ptr
+= bytes_read
;
996 case DW_LNS_advance_line
:
997 line
+= read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
998 line_ptr
+= bytes_read
;
1000 case DW_LNS_set_file
:
1004 /* The file and directory tables are 0 based, the references
1006 file
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1007 line_ptr
+= bytes_read
;
1008 filename
= concat_filename (table
, file
);
1011 case DW_LNS_set_column
:
1012 column
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1013 line_ptr
+= bytes_read
;
1015 case DW_LNS_negate_stmt
:
1016 is_stmt
= (!is_stmt
);
1018 case DW_LNS_set_basic_block
:
1021 case DW_LNS_const_add_pc
:
1022 address
+= lh
.minimum_instruction_length
1023 * ((255 - lh
.opcode_base
) / lh
.line_range
);
1025 case DW_LNS_fixed_advance_pc
:
1026 address
+= read_2_bytes (abfd
, line_ptr
);
1029 default: /* Special operand. */
1030 adj_opcode
= op_code
- lh
.opcode_base
;
1031 address
+= (adj_opcode
/ lh
.line_range
)
1032 * lh
.minimum_instruction_length
;
1033 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
1034 /* Append row to matrix using current values. */
1035 add_line_info (table
, address
, filename
, line
, column
, 0);
1049 /* If ADDR is within TABLE set the output parameters and return true,
1050 otherwise return false. The output parameters, FILENAME_PTR and
1051 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1054 lookup_address_in_line_info_table (table
,
1058 struct line_info_table
* table
;
1060 const char **filename_ptr
;
1061 unsigned int *linenumber_ptr
;
1063 struct line_info
* next_line
= table
->last_line
;
1064 struct line_info
* each_line
;
1069 each_line
= next_line
->prev_line
;
1071 while (each_line
&& next_line
)
1073 if (!each_line
->end_sequence
1074 && addr
>= each_line
->address
&& addr
< next_line
->address
)
1076 *filename_ptr
= each_line
->filename
;
1077 *linenumber_ptr
= each_line
->line
;
1080 next_line
= each_line
;
1081 each_line
= each_line
->prev_line
;
1087 /* Function table functions. */
1091 struct funcinfo
*prev_func
;
1097 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
1100 lookup_address_in_function_table (table
,
1103 struct funcinfo
* table
;
1105 const char **functionname_ptr
;
1107 struct funcinfo
* each_func
;
1109 for (each_func
= table
;
1111 each_func
= each_func
->prev_func
)
1113 if (addr
>= each_func
->low
&& addr
< each_func
->high
)
1115 *functionname_ptr
= each_func
->name
;
1123 /* DWARF2 Compilation unit functions. */
1125 /* Scan over each die in a comp. unit looking for functions to add
1126 to the function table. */
1129 scan_unit_for_functions (unit
)
1130 struct comp_unit
*unit
;
1132 bfd
*abfd
= unit
->abfd
;
1133 char *info_ptr
= unit
->first_child_die_ptr
;
1134 int nesting_level
= 1;
1136 while (nesting_level
)
1138 unsigned int abbrev_number
, bytes_read
, i
;
1139 struct abbrev_info
*abbrev
;
1140 struct attribute attr
;
1141 struct funcinfo
*func
;
1144 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1145 info_ptr
+= bytes_read
;
1147 if (! abbrev_number
)
1153 abbrev
= lookup_abbrev (abbrev_number
,unit
->abbrevs
);
1156 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %d."),
1158 bfd_set_error (bfd_error_bad_value
);
1162 if (abbrev
->tag
== DW_TAG_subprogram
)
1164 func
= (struct funcinfo
*) bfd_zalloc (abfd
, sizeof (struct funcinfo
));
1165 func
->prev_func
= unit
->function_table
;
1166 unit
->function_table
= func
;
1171 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1173 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1181 name
= DW_STRING (&attr
);
1183 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1184 if (func
->name
== NULL
)
1185 func
->name
= DW_STRING (&attr
);
1188 case DW_AT_MIPS_linkage_name
:
1189 func
->name
= DW_STRING (&attr
);
1193 func
->low
= DW_ADDR (&attr
);
1197 func
->high
= DW_ADDR (&attr
);
1209 name
= DW_STRING (&attr
);
1218 if (abbrev
->has_children
)
1225 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1226 includes the compilation unit header that proceeds the DIE's, but
1227 does not include the length field that preceeds each compilation
1228 unit header. END_PTR points one past the end of this comp unit.
1229 If ABBREV_LENGTH is 0, then the length of the abbreviation offset
1230 is assumed to be four bytes. Otherwise, it it is the size given.
1232 This routine does not read the whole compilation unit; only enough
1233 to get to the line number information for the compilation unit. */
1235 static struct comp_unit
*
1236 parse_comp_unit (abfd
, info_ptr
, end_ptr
, abbrev_length
)
1240 unsigned int abbrev_length
;
1242 struct comp_unit
* unit
;
1244 unsigned short version
;
1245 unsigned int abbrev_offset
= 0;
1246 unsigned char addr_size
;
1247 struct abbrev_info
** abbrevs
;
1249 unsigned int abbrev_number
, bytes_read
, i
;
1250 struct abbrev_info
*abbrev
;
1251 struct attribute attr
;
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
);
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
)
1404 struct comp_unit
* unit
;
1406 const char **filename_ptr
;
1407 const char **functionname_ptr
;
1408 unsigned int *linenumber_ptr
;
1416 if (! unit
->line_table
)
1418 if (! unit
->stmtlist
)
1424 unit
->line_table
= decode_line_info (unit
);
1426 if (! unit
->line_table
)
1432 if (! scan_unit_for_functions (unit
))
1439 line_p
= lookup_address_in_line_info_table (unit
->line_table
,
1443 func_p
= lookup_address_in_function_table (unit
->function_table
,
1446 return line_p
|| func_p
;
1449 /* Locate a section in a BFD containing debugging info. The search starts from the
1450 section after AFTER_SEC, or from the first section in the BFD if AFTER_SEC is
1451 NULL. The search works by examining the names of the sections. There are two
1452 permissiable names. The first is .debug_info. This is the standard DWARF2 name.
1453 The second is a prefix .gnu.linkonce.wi. This is a variation on the .debug_info
1454 section which has a checksum describing the contents appended onto the name. This
1455 allows the linker to identify and discard duplicate debugging sections for
1456 different compilation units. */
1457 #define DWARF2_DEBUG_INFO ".debug_info"
1458 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1461 find_debug_info (abfd
, after_sec
)
1463 asection
* after_sec
;
1468 msec
= after_sec
->next
;
1470 msec
= abfd
->sections
;
1474 if (strcmp (msec
->name
, DWARF2_DEBUG_INFO
) == 0)
1477 if (strncmp (msec
->name
, GNU_LINKONCE_INFO
, strlen (GNU_LINKONCE_INFO
)) == 0)
1486 /* The DWARF2 version of find_nearest line. Return true if the line
1487 is found without error. ADDR_SIZE is the number of bytes in the
1488 initial .debug_info length field and in the abbreviation offset.
1489 You may use zero to indicate that the default value should be
1493 _bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
1494 filename_ptr
, functionname_ptr
,
1499 asymbol
**symbols ATTRIBUTE_UNUSED
;
1501 const char **filename_ptr
;
1502 const char **functionname_ptr
;
1503 unsigned int *linenumber_ptr
;
1504 unsigned int addr_size
;
1506 /* Read each compilation unit from the section .debug_info, and check
1507 to see if it contains the address we are searching for. If yes,
1508 lookup the address, and return the line number info. If no, go
1509 on to the next compilation unit.
1511 We keep a list of all the previously read compilation units, and
1512 a pointer to the next un-read compilation unit. Check the
1513 previously read units before reading more. */
1514 struct dwarf2_debug
*stash
= elf_tdata (abfd
)->dwarf2_find_line_info
;
1516 /* What address are we looking for? */
1517 bfd_vma addr
= offset
+ section
->vma
;
1519 struct comp_unit
* each
;
1521 *filename_ptr
= NULL
;
1522 *functionname_ptr
= NULL
;
1523 *linenumber_ptr
= 0;
1525 /* The DWARF2 spec says that the initial length field, and the
1526 offset of the abbreviation table, should both be 4-byte values.
1527 However, some compilers do things differently. */
1530 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
1534 unsigned long total_size
;
1537 stash
= elf_tdata (abfd
)->dwarf2_find_line_info
=
1538 (struct dwarf2_debug
*) bfd_zalloc (abfd
, sizeof (struct dwarf2_debug
));
1542 msec
= find_debug_info (abfd
, NULL
);
1544 /* No dwarf2 info. Note that at this point the stash
1545 has been allocated, but contains zeros, this lets
1546 future calls to this function fail quicker. */
1549 /* There can be more than one DWARF2 info section in a BFD these days.
1550 Read them all in and produce one large stash. We do this in two
1551 passes - in the first pass we just accumulate the section sizes.
1552 In the second pass we read in the section's contents. The allows
1553 us to avoid reallocing the data as we add sections to the stash. */
1554 for (total_size
= 0; msec
; msec
= find_debug_info (abfd
, msec
))
1555 total_size
+= msec
->_raw_size
;
1557 stash
->info_ptr
= (char *) bfd_alloc (abfd
, total_size
);
1558 if (stash
->info_ptr
== NULL
)
1561 stash
->info_ptr_end
= stash
->info_ptr
;
1563 for (msec
= find_debug_info (abfd
, NULL
);
1565 msec
= find_debug_info (abfd
, msec
))
1568 unsigned long start
;
1570 size
= msec
->_raw_size
;
1574 start
= stash
->info_ptr_end
- stash
->info_ptr
;
1576 if (! bfd_get_section_contents (abfd
, msec
, stash
->info_ptr
+ start
, 0, size
))
1579 stash
->info_ptr_end
= stash
->info_ptr
+ start
+ size
;
1582 BFD_ASSERT (stash
->info_ptr_end
= stash
->info_ptr
+ total_size
);
1585 /* FIXME: There is a problem with the contents of the
1586 .debug_info section. The 'low' and 'high' addresses of the
1587 comp_units are computed by relocs against symbols in the
1588 .text segment. We need these addresses in order to determine
1589 the nearest line number, and so we have to resolve the
1590 relocs. There is a similar problem when the .debug_line
1591 section is processed as well (e.g., there may be relocs
1592 against the operand of the DW_LNE_set_address operator).
1594 Unfortunately getting hold of the reloc information is hard...
1596 For now, this means that disassembling object files (as
1597 opposed to fully executables) does not always work as well as
1600 /* A null info_ptr indicates that there is no dwarf2 info
1601 (or that an error occured while setting up the stash). */
1602 if (! stash
->info_ptr
)
1605 /* Check the previously read comp. units first. */
1606 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
1607 if (comp_unit_contains_address (each
, addr
))
1608 return comp_unit_find_nearest_line (each
, addr
, filename_ptr
,
1609 functionname_ptr
, linenumber_ptr
);
1611 /* Read each remaining comp. units checking each as they are read. */
1612 while (stash
->info_ptr
< stash
->info_ptr_end
)
1614 struct comp_unit
* each
;
1619 length
= read_4_bytes (abfd
, stash
->info_ptr
);
1621 length
= read_8_bytes (abfd
, stash
->info_ptr
);
1622 stash
->info_ptr
+= addr_size
;
1626 each
= parse_comp_unit (abfd
, stash
->info_ptr
,
1627 stash
->info_ptr
+ length
,
1629 stash
->info_ptr
+= length
;
1633 each
->next_unit
= stash
->all_comp_units
;
1634 stash
->all_comp_units
= each
;
1636 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1637 compilation units. If we don't have them (i.e.,
1638 unit->high == 0), we need to consult the line info
1639 table to see if a compilation unit contains the given
1641 if (each
->arange
.high
> 0)
1643 if (comp_unit_contains_address (each
, addr
))
1644 return comp_unit_find_nearest_line (each
, addr
,
1651 found
= comp_unit_find_nearest_line (each
, addr
,