2 Copyright 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
4 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
7 From the dwarf2read.c header:
8 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
9 Inc. with support from Florida State University (under contract
10 with the Ada Joint Program Office), and Silicon Graphics, Inc.
11 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
12 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
13 support in dwarfread.c
15 This file is part of BFD.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or (at
20 your option) any later version.
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33 #include "libiberty.h"
36 #include "elf/dwarf2.h"
38 /* The data in the .debug_line statement prologue looks like this. */
41 unsigned int total_length
;
42 unsigned short version
;
43 unsigned int prologue_length
;
44 unsigned char minimum_instruction_length
;
45 unsigned char default_is_stmt
;
47 unsigned char line_range
;
48 unsigned char opcode_base
;
49 unsigned char *standard_opcode_lengths
;
52 /* Attributes have a name and a value */
55 enum dwarf_attribute name
;
60 struct dwarf_block
*blk
;
68 /* Get at parts of an attribute structure */
70 #define DW_STRING(attr) ((attr)->u.str)
71 #define DW_UNSND(attr) ((attr)->u.unsnd)
72 #define DW_BLOCK(attr) ((attr)->u.blk)
73 #define DW_SND(attr) ((attr)->u.snd)
74 #define DW_ADDR(attr) ((attr)->u.addr)
76 /* Blocks are a bunch of untyped bytes. */
86 /* A list of all previously read comp_units. */
87 struct comp_unit
* all_comp_units
;
89 /* The next unread compilation unit within the .debug_info section.
90 Zero indicates that the .debug_info section has not been loaded
94 /* Pointer to the end of the .debug_info section memory buffer. */
97 /* Pointer to the .debug_abbrev section loaded into memory. */
98 char* dwarf_abbrev_buffer
;
100 /* Length of the loaded .debug_abbrev section. */
101 unsigned long dwarf_abbrev_size
;
106 The following function up to the END VERBATUM mark are
107 copied directly from dwarf2read.c. */
109 /* read dwarf information from a buffer */
112 read_1_byte (abfd
, buf
)
116 return bfd_get_8 (abfd
, (bfd_byte
*) buf
);
120 read_1_signed_byte (abfd
, buf
)
124 return bfd_get_signed_8 (abfd
, (bfd_byte
*) buf
);
128 read_2_bytes (abfd
, buf
)
132 return bfd_get_16 (abfd
, (bfd_byte
*) buf
);
137 /* This is not used. */
140 read_2_signed_bytes (abfd
, buf
)
144 return bfd_get_signed_16 (abfd
, (bfd_byte
*) buf
);
150 read_4_bytes (abfd
, buf
)
154 return bfd_get_32 (abfd
, (bfd_byte
*) buf
);
159 /* This is not used. */
162 read_4_signed_bytes (abfd
, buf
)
166 return bfd_get_signed_32 (abfd
, (bfd_byte
*) buf
);
172 read_8_bytes (abfd
, buf
)
176 return bfd_get_64 (abfd
, (bfd_byte
*) buf
);
180 read_n_bytes (abfd
, buf
, size
)
185 /* If the size of a host char is 8 bits, we can return a pointer
186 to the buffer, otherwise we have to copy the data to a buffer
187 allocated on the temporary obstack. */
192 read_string (abfd
, buf
, bytes_read_ptr
)
195 unsigned int *bytes_read_ptr
;
197 /* If the size of a host char is 8 bits, we can return a pointer
198 to the string, otherwise we have to copy the string to a buffer
199 allocated on the temporary obstack. */
205 *bytes_read_ptr
= strlen (buf
) + 1;
210 read_unsigned_leb128 (abfd
, buf
, bytes_read_ptr
)
213 unsigned int *bytes_read_ptr
;
215 unsigned int result
, num_read
;
225 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
228 result
|= ((byte
& 127) << shift
);
229 if ((byte
& 128) == 0)
235 *bytes_read_ptr
= num_read
;
240 read_signed_leb128 (abfd
, buf
, bytes_read_ptr
)
243 unsigned int *bytes_read_ptr
;
246 int i
, shift
, size
, num_read
;
256 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
259 result
|= ((byte
& 127) << shift
);
261 if ((byte
& 128) == 0)
266 if ((shift
< size
) && (byte
& 0x40))
268 result
|= -(1 << shift
);
270 *bytes_read_ptr
= num_read
;
278 read_address (abfd
, buf
)
284 if (sizeof(retval
) == 4)
286 retval
= bfd_get_32 (abfd
, (bfd_byte
*) buf
);
288 retval
= bfd_get_64 (abfd
, (bfd_byte
*) buf
);
297 /* This data structure holds the information of an abbrev. */
300 unsigned int number
; /* number identifying abbrev */
301 enum dwarf_tag tag
; /* dwarf tag */
302 int has_children
; /* boolean */
303 unsigned int num_attrs
; /* number of attributes */
304 struct attr_abbrev
*attrs
; /* an array of attribute descriptions */
305 struct abbrev_info
*next
; /* next in chain */
310 enum dwarf_attribute name
;
311 enum dwarf_form form
;
314 #ifndef ABBREV_HASH_SIZE
315 #define ABBREV_HASH_SIZE 121
317 #ifndef ATTR_ALLOC_CHUNK
318 #define ATTR_ALLOC_CHUNK 4
321 /* Lookup an abbrev_info structure in the abbrev hash table. */
323 static struct abbrev_info
*
324 lookup_abbrev (number
,abbrevs
)
326 struct abbrev_info
**abbrevs
;
328 unsigned int hash_number
;
329 struct abbrev_info
*abbrev
;
331 hash_number
= number
% ABBREV_HASH_SIZE
;
332 abbrev
= abbrevs
[hash_number
];
336 if (abbrev
->number
== number
)
339 abbrev
= abbrev
->next
;
344 /* In DWARF version 2, the description of the debugging information is
345 stored in a separate .debug_abbrev section. Before we read any
346 dies from a section we read in all abbreviations and install them
349 static struct abbrev_info
**
350 read_abbrevs (abfd
, offset
)
354 struct abbrev_info
**abbrevs
;
356 struct abbrev_info
*cur_abbrev
;
357 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
358 unsigned int abbrev_form
, hash_number
;
359 struct dwarf2_debug
*stash
;
361 stash
= elf_tdata(abfd
)->dwarf2_find_line_info
;
363 if (! stash
->dwarf_abbrev_buffer
)
367 msec
= bfd_get_section_by_name (abfd
, ".debug_abbrev");
370 (*_bfd_error_handler
) ("Dwarf Error: Can't find .debug_abbrev section.");
371 bfd_set_error (bfd_error_bad_value
);
375 stash
->dwarf_abbrev_size
= bfd_get_section_size_before_reloc (msec
);
376 stash
->dwarf_abbrev_buffer
= (unsigned char*) bfd_alloc (abfd
, stash
->dwarf_abbrev_size
);
377 if (! stash
->dwarf_abbrev_buffer
)
380 if (! bfd_get_section_contents (abfd
, msec
,
381 stash
->dwarf_abbrev_buffer
, 0,
382 stash
->dwarf_abbrev_size
))
386 if (offset
> stash
->dwarf_abbrev_size
)
388 (*_bfd_error_handler
) ("Dwarf Error: Abbrev offset (%u) bigger than abbrev size (%u).",
389 offset
, stash
->dwarf_abbrev_size
);
390 bfd_set_error (bfd_error_bad_value
);
394 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, sizeof(struct abbrev_info
*) * ABBREV_HASH_SIZE
);
396 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
397 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
398 abbrev_ptr
+= bytes_read
;
400 /* loop until we reach an abbrev number of 0 */
401 while (abbrev_number
)
403 cur_abbrev
= (struct abbrev_info
*)bfd_zalloc (abfd
, sizeof (struct abbrev_info
));
405 /* read in abbrev header */
406 cur_abbrev
->number
= abbrev_number
;
407 cur_abbrev
->tag
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
408 abbrev_ptr
+= bytes_read
;
409 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
);
412 /* now read in declarations */
413 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
414 abbrev_ptr
+= bytes_read
;
415 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
416 abbrev_ptr
+= bytes_read
;
419 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
421 cur_abbrev
->attrs
= (struct attr_abbrev
*)
422 bfd_realloc (cur_abbrev
->attrs
,
423 (cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
)
424 * sizeof (struct attr_abbrev
));
425 if (! cur_abbrev
->attrs
)
428 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
= abbrev_name
;
429 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
++].form
= abbrev_form
;
430 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
431 abbrev_ptr
+= bytes_read
;
432 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
433 abbrev_ptr
+= bytes_read
;
436 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
437 cur_abbrev
->next
= abbrevs
[hash_number
];
438 abbrevs
[hash_number
] = cur_abbrev
;
440 /* Get next abbreviation.
441 Under Irix6 the abbreviations for a compilation unit are not
442 always properly terminated with an abbrev number of 0.
443 Exit loop if we encounter an abbreviation which we have
444 already read (which means we are about to read the abbreviations
445 for the next compile unit) or if the end of the abbreviation
447 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
448 >= stash
->dwarf_abbrev_size
)
450 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
451 abbrev_ptr
+= bytes_read
;
452 if (lookup_abbrev (abbrev_number
,abbrevs
) != NULL
)
459 /* Read an attribute described by an abbreviated attribute. */
462 read_attribute (attr
, abbrev
, abfd
, info_ptr
)
463 struct attribute
*attr
;
464 struct attr_abbrev
*abbrev
;
468 unsigned int bytes_read
;
469 struct dwarf_block
*blk
;
471 attr
->name
= abbrev
->name
;
472 attr
->form
= abbrev
->form
;
473 switch (abbrev
->form
)
476 case DW_FORM_ref_addr
:
477 DW_ADDR (attr
) = read_address (abfd
, info_ptr
);
478 info_ptr
+= sizeof(bfd_vma
);
481 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
482 blk
->size
= read_2_bytes (abfd
, info_ptr
);
484 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
485 info_ptr
+= blk
->size
;
486 DW_BLOCK (attr
) = blk
;
489 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
490 blk
->size
= read_4_bytes (abfd
, info_ptr
);
492 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
493 info_ptr
+= blk
->size
;
494 DW_BLOCK (attr
) = blk
;
497 DW_UNSND (attr
) = read_2_bytes (abfd
, info_ptr
);
501 DW_UNSND (attr
) = read_4_bytes (abfd
, info_ptr
);
505 DW_UNSND (attr
) = read_8_bytes (abfd
, info_ptr
);
509 DW_STRING (attr
) = read_string (abfd
, info_ptr
, &bytes_read
);
510 info_ptr
+= bytes_read
;
513 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
514 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
515 info_ptr
+= bytes_read
;
516 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
517 info_ptr
+= blk
->size
;
518 DW_BLOCK (attr
) = blk
;
521 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
522 blk
->size
= read_1_byte (abfd
, info_ptr
);
524 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
525 info_ptr
+= blk
->size
;
526 DW_BLOCK (attr
) = blk
;
529 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
533 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
537 DW_SND (attr
) = read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
538 info_ptr
+= bytes_read
;
541 DW_UNSND (attr
) = read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
542 info_ptr
+= bytes_read
;
545 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
549 DW_UNSND (attr
) = read_2_bytes (abfd
, info_ptr
);
553 DW_UNSND (attr
) = read_4_bytes (abfd
, info_ptr
);
556 case DW_FORM_ref_udata
:
557 DW_UNSND (attr
) = read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
558 info_ptr
+= bytes_read
;
561 case DW_FORM_indirect
:
563 (*_bfd_error_handler
) ("Dwarf Error: Invalid or unhandled FORM value: %d.",
565 bfd_set_error (bfd_error_bad_value
);
571 /* Source line information table routines. */
573 #define FILE_ALLOC_CHUNK 5
574 #define DIR_ALLOC_CHUNK 5
577 struct line_info
* prev_line
;
592 struct line_info_table
{
595 unsigned int num_files
;
596 unsigned int num_dirs
;
600 struct fileinfo
* files
;
601 struct line_info
* last_line
;
605 add_line_info (table
, address
, filename
, line
, column
)
606 struct line_info_table
* table
;
612 struct line_info
* info
= (struct line_info
*)
613 bfd_alloc (table
->abfd
, sizeof (struct line_info
));
615 info
->prev_line
= table
->last_line
;
616 table
->last_line
= info
;
618 info
->address
= address
;
619 info
->filename
= filename
;
621 info
->column
= column
;
625 concat_filename (table
, file
)
626 struct line_info_table
* table
;
629 char* filename
= table
->files
[file
- 1].name
;
630 if (*filename
== '/')
635 char* dirname
= (table
->files
[file
- 1].dir
636 ? table
->dirs
[table
->files
[file
- 1].dir
- 1]
638 return (char*) concat (dirname
, "/", filename
, NULL
);
642 /* Decode the line number information for the compilation unit whose
643 line number info is at OFFSET in the .debug_line section.
644 The compilation directory of the file is passed in COMP_DIR. */
646 static struct line_info_table
*
647 decode_line_info (abfd
, offset
, comp_dir
)
652 static char* dwarf_line_buffer
= 0;
654 struct line_info_table
* table
;
659 unsigned int i
, bytes_read
;
660 char *cur_file
, *cur_dir
;
661 unsigned char op_code
, extended_op
, adj_opcode
;
663 if (! dwarf_line_buffer
)
668 msec
= bfd_get_section_by_name (abfd
, ".debug_line");
671 (*_bfd_error_handler
) ("Dwarf Error: Can't find .debug_line section.");
672 bfd_set_error (bfd_error_bad_value
);
676 size
= bfd_get_section_size_before_reloc (msec
);
677 dwarf_line_buffer
= (unsigned char*) bfd_alloc (abfd
, size
);
678 if (! dwarf_line_buffer
)
681 if (! bfd_get_section_contents (abfd
, msec
,
682 dwarf_line_buffer
, 0,
687 table
= (struct line_info_table
*) bfd_alloc (abfd
,
688 sizeof (struct line_info_table
));
690 table
->comp_dir
= comp_dir
;
692 table
->num_files
= 0;
698 line_ptr
= dwarf_line_buffer
+ offset
;
700 /* read in the prologue */
701 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
703 line_end
= line_ptr
+ lh
.total_length
;
704 lh
.version
= read_2_bytes (abfd
, line_ptr
);
706 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
);
708 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
);
710 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
);
712 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
);
714 lh
.line_range
= read_1_byte (abfd
, line_ptr
);
716 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
);
718 lh
.standard_opcode_lengths
= (unsigned char *)
719 bfd_alloc (abfd
, lh
.opcode_base
* sizeof (unsigned char));
721 lh
.standard_opcode_lengths
[0] = 1;
722 for (i
= 1; i
< lh
.opcode_base
; ++i
)
724 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
);
728 /* Read directory table */
729 while ((cur_dir
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
731 line_ptr
+= bytes_read
;
732 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
734 table
->dirs
= (char **)
735 bfd_realloc (table
->dirs
,
736 (table
->num_dirs
+ DIR_ALLOC_CHUNK
) * sizeof (char *));
740 table
->dirs
[table
->num_dirs
++] = cur_dir
;
742 line_ptr
+= bytes_read
;
744 /* Read file name table */
745 while ((cur_file
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
747 line_ptr
+= bytes_read
;
748 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
750 table
->files
= (struct fileinfo
*)
751 bfd_realloc (table
->files
,
752 (table
->num_files
+ FILE_ALLOC_CHUNK
)
753 * sizeof (struct fileinfo
));
757 table
->files
[table
->num_files
].name
= cur_file
;
758 table
->files
[table
->num_files
].dir
=
759 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
760 line_ptr
+= bytes_read
;
761 table
->files
[table
->num_files
].time
=
762 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
763 line_ptr
+= bytes_read
;
764 table
->files
[table
->num_files
].size
=
765 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
766 line_ptr
+= bytes_read
;
769 line_ptr
+= bytes_read
;
771 /* Read the statement sequences until there's nothing left. */
772 while (line_ptr
< line_end
)
774 /* state machine registers */
776 char* filename
= concat_filename (table
, 1);
777 unsigned int line
= 1;
778 unsigned int column
= 0;
779 int is_stmt
= lh
.default_is_stmt
;
781 int end_sequence
= 0;
783 /* Decode the table. */
784 while (! end_sequence
)
786 op_code
= read_1_byte (abfd
, line_ptr
);
790 case DW_LNS_extended_op
:
791 line_ptr
+= 1; /* ignore length */
792 extended_op
= read_1_byte (abfd
, line_ptr
);
796 case DW_LNE_end_sequence
:
798 add_line_info (table
, address
, filename
, line
, column
);
800 case DW_LNE_set_address
:
801 address
= read_address (abfd
, line_ptr
);
802 address
&= 0xffffffff;
803 line_ptr
+= sizeof (bfd_vma
);
805 case DW_LNE_define_file
:
806 cur_file
= read_string (abfd
, line_ptr
, &bytes_read
);
807 line_ptr
+= bytes_read
;
808 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
810 table
->files
= (struct fileinfo
*)
811 bfd_realloc (table
->files
,
812 (table
->num_files
+ FILE_ALLOC_CHUNK
)
813 * sizeof (struct fileinfo
));
817 table
->files
[table
->num_files
].name
= cur_file
;
818 table
->files
[table
->num_files
].dir
=
819 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
820 line_ptr
+= bytes_read
;
821 table
->files
[table
->num_files
].time
=
822 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
823 line_ptr
+= bytes_read
;
824 table
->files
[table
->num_files
].size
=
825 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
826 line_ptr
+= bytes_read
;
830 (*_bfd_error_handler
) ("Dwarf Error: mangled line number section.");
831 bfd_set_error (bfd_error_bad_value
);
836 add_line_info (table
, address
, filename
, line
, column
);
839 case DW_LNS_advance_pc
:
840 address
+= lh
.minimum_instruction_length
841 * read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
842 line_ptr
+= bytes_read
;
844 case DW_LNS_advance_line
:
845 line
+= read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
846 line_ptr
+= bytes_read
;
848 case DW_LNS_set_file
:
852 /* The file and directory tables are 0 based, the references
854 file
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
855 line_ptr
+= bytes_read
;
856 filename
= concat_filename (table
, file
);
859 case DW_LNS_set_column
:
860 column
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
861 line_ptr
+= bytes_read
;
863 case DW_LNS_negate_stmt
:
864 is_stmt
= (!is_stmt
);
866 case DW_LNS_set_basic_block
:
869 case DW_LNS_const_add_pc
:
870 address
+= (255 - lh
.opcode_base
) / lh
.line_range
;
872 case DW_LNS_fixed_advance_pc
:
873 address
+= read_2_bytes (abfd
, line_ptr
);
876 default: /* special operand */
877 adj_opcode
= op_code
- lh
.opcode_base
;
878 address
+= (adj_opcode
/ lh
.line_range
)
879 * lh
.minimum_instruction_length
;
880 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
881 /* append row to matrix using current values */
882 add_line_info (table
, address
, filename
, line
, column
);
892 /* If ADDR is within TABLE set the output parameters and return true,
893 otherwise return false. The output parameters, FILENAME_PTR and
894 LINENUMBER_PTR, are pointers to the objects to be filled in. */
897 lookup_address_in_line_info_table (table
,
901 struct line_info_table
* table
;
903 const char **filename_ptr
;
904 unsigned int *linenumber_ptr
;
906 struct line_info
* each_line
;
907 struct line_info
* next_line
;
909 for (next_line
= 0, each_line
= table
->last_line
;
911 next_line
= each_line
, each_line
= each_line
->prev_line
)
913 if (addr
>= each_line
->address
915 || addr
< next_line
->address
))
917 *filename_ptr
= each_line
->filename
;
918 *linenumber_ptr
= each_line
->line
;
929 /* Function table functions. */
932 struct funcinfo
*prev_func
;
940 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
943 lookup_address_in_function_table (table
,
946 struct funcinfo
* table
;
948 const char **functionname_ptr
;
950 struct funcinfo
* each_func
;
952 for (each_func
= table
;
954 each_func
= each_func
->prev_func
)
956 if (addr
>= (each_func
->low
& 0xffffffff)
957 && addr
< (each_func
->high
& 0xffffffff))
959 *functionname_ptr
= each_func
->name
;
970 /* DWARF2 Compilation unit functions. */
973 /* A minimal decoding of DWARF2 compilation units. We only decode
974 what's needed to get to the line number information. */
978 /* Chain the previously read compilation units. */
979 struct comp_unit
* next_unit
;
981 /* Keep the bdf convenient (for memory allocation). */
984 /* The lowest and higest addresses contained in this compilation
985 unit as specified in the compilation unit header. */
989 /* The DW_AT_name attribute (for error messages). */
992 /* The abbrev hash table. */
993 struct abbrev_info
** abbrevs
;
995 /* Note that an error was found by comp_unit_find_nearest_line. */
998 /* The DW_AT_comp_dir attribute */
1001 /* True if there is a line number table associated with this comp. unit. */
1004 /* The offset into .debug_line of the line number table. */
1005 unsigned long line_offset
;
1007 /* Pointer to the first child die for the comp unit. */
1008 char *first_child_die_ptr
;
1010 /* The end of the comp unit. */
1013 /* The decoded line number, NULL if not yet decoded. */
1014 struct line_info_table
* line_table
;
1016 /* A list of the functions found in this comp. unit. */
1017 struct funcinfo
* function_table
;
1023 /* Scan over each die in a comp. unit looking for functions to add
1024 to the function table. */
1027 scan_unit_for_functions (unit
)
1028 struct comp_unit
*unit
;
1030 bfd
*abfd
= unit
->abfd
;
1031 char *info_ptr
= unit
->first_child_die_ptr
;
1032 int nesting_level
= 1;
1034 while (nesting_level
)
1036 unsigned int abbrev_number
, bytes_read
, i
;
1037 struct abbrev_info
*abbrev
;
1038 struct attribute attr
;
1039 struct funcinfo
*func
;
1042 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1043 info_ptr
+= bytes_read
;
1045 if (! abbrev_number
)
1051 abbrev
= lookup_abbrev (abbrev_number
,unit
->abbrevs
);
1054 (*_bfd_error_handler
) ("Dwarf Error: Could not find abbrev number %d.",
1056 bfd_set_error (bfd_error_bad_value
);
1060 if (abbrev
->tag
== DW_TAG_subprogram
)
1062 func
= (struct funcinfo
*) bfd_zalloc (abfd
, sizeof (struct funcinfo
));
1063 func
->prev_func
= unit
->function_table
;
1064 unit
->function_table
= func
;
1069 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1071 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], abfd
, info_ptr
);
1079 name
= DW_STRING (&attr
);
1081 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1082 if (func
->name
== NULL
)
1083 func
->name
= DW_STRING (&attr
);
1086 case DW_AT_MIPS_linkage_name
:
1087 func
->name
= DW_STRING (&attr
);
1091 func
->low
= DW_ADDR (&attr
);
1095 func
->high
= DW_ADDR (&attr
);
1107 name
= DW_STRING (&attr
);
1116 if (abbrev
->has_children
)
1128 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This includes
1129 the compilation unit header that proceeds the DIE's, but does not
1130 include the length field that preceeds each compilation unit header.
1131 END_PTR points one past the end of this comp unit.
1133 This routine does not read the whole compilation unit; only enough
1134 to get to the line number information for the compilation unit.
1137 static struct comp_unit
*
1138 parse_comp_unit (abfd
, info_ptr
, end_ptr
)
1143 struct comp_unit
* unit
;
1145 unsigned short version
;
1146 unsigned int abbrev_offset
;
1147 unsigned char addr_size
;
1148 struct abbrev_info
** abbrevs
;
1150 unsigned int abbrev_number
, bytes_read
, i
;
1151 struct abbrev_info
*abbrev
;
1152 struct attribute attr
;
1154 version
= read_2_bytes (abfd
, info_ptr
);
1156 abbrev_offset
= read_4_bytes (abfd
, info_ptr
);
1158 addr_size
= read_1_byte (abfd
, info_ptr
);
1163 (*_bfd_error_handler
) ("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information.", version
);
1164 bfd_set_error (bfd_error_bad_value
);
1168 if (addr_size
!= sizeof (bfd_vma
))
1170 (*_bfd_error_handler
) ("Dwarf Error: found address size '%u', this readers only handles address size '%u'.",
1173 bfd_set_error (bfd_error_bad_value
);
1177 /* Read the abbrevs for this compilation unit into a table */
1178 abbrevs
= read_abbrevs (abfd
, abbrev_offset
);
1182 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1183 info_ptr
+= bytes_read
;
1184 if (! abbrev_number
)
1186 (*_bfd_error_handler
) ("Dwarf Error: Bad abbrev number: %d.",
1188 bfd_set_error (bfd_error_bad_value
);
1192 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
1195 (*_bfd_error_handler
) ("Dwarf Error: Could not find abbrev number %d.",
1197 bfd_set_error (bfd_error_bad_value
);
1201 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, sizeof (struct comp_unit
));
1203 unit
->abbrevs
= abbrevs
;
1204 unit
->end_ptr
= end_ptr
;
1206 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1208 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], abfd
, info_ptr
);
1210 /* Store the data if it is of an attribute we want to keep in a
1211 partial symbol table. */
1214 case DW_AT_stmt_list
:
1216 unit
->line_offset
= DW_UNSND (&attr
);
1220 unit
->name
= DW_STRING (&attr
);
1224 unit
->low
= DW_ADDR (&attr
);
1228 unit
->high
= DW_ADDR (&attr
);
1231 case DW_AT_comp_dir
:
1233 char* comp_dir
= DW_STRING (&attr
);
1236 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1237 directory, get rid of it. */
1238 char *cp
= (char*) strchr (comp_dir
, ':');
1240 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
1243 unit
->comp_dir
= comp_dir
;
1252 unit
->first_child_die_ptr
= info_ptr
;
1260 /* Return true if UNIT contains the address given by ADDR. */
1263 comp_unit_contains_address (unit
, addr
)
1264 struct comp_unit
* unit
;
1267 return ! unit
->error
1268 && ( addr
>= (unit
->low
& 0xffffffff)
1269 && addr
<= (unit
->high
& 0xffffffff));
1273 /* If UNIT contains ADDR, set the output parameters to the values for
1274 the line containing ADDR. The output parameters, FILENAME_PTR,
1275 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1278 Return true of UNIT contains ADDR, and no errors were encountered;
1282 comp_unit_find_nearest_line (unit
, addr
,
1283 filename_ptr
, functionname_ptr
, linenumber_ptr
)
1284 struct comp_unit
* unit
;
1286 const char **filename_ptr
;
1287 const char **functionname_ptr
;
1288 unsigned int *linenumber_ptr
;
1296 if (! unit
->line_table
)
1298 if (! unit
->stmtlist
)
1304 unit
->line_table
= decode_line_info (unit
->abfd
,
1308 if (! unit
->line_table
)
1314 if (! scan_unit_for_functions (unit
))
1321 line_p
= lookup_address_in_line_info_table (unit
->line_table
,
1325 func_p
= lookup_address_in_function_table (unit
->function_table
,
1328 return line_p
|| func_p
;
1331 /* The DWARF2 version of find_nearest line.
1332 Return true if the line is found without error. */
1335 _bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
1336 filename_ptr
, functionname_ptr
, linenumber_ptr
)
1341 const char **filename_ptr
;
1342 const char **functionname_ptr
;
1343 unsigned int *linenumber_ptr
;
1345 /* Read each compilation unit from the section .debug_info, and check
1346 to see if it contains the address we are searching for. If yes,
1347 lookup the address, and return the line number info. If no, go
1348 on to the next compilation unit.
1350 We keep a list of all the previously read compilation units, and
1351 a pointer to the next un-read compilation unit. Check the
1352 previously read units before reading more.
1355 struct dwarf2_debug
*stash
= elf_tdata (abfd
)->dwarf2_find_line_info
;
1357 /* What address are we looking for? */
1358 bfd_vma addr
= offset
+ section
->vma
;
1360 struct comp_unit
* each
;
1362 *filename_ptr
= NULL
;
1363 *functionname_ptr
= NULL
;
1364 *linenumber_ptr
= 0;
1371 stash
= elf_tdata (abfd
)->dwarf2_find_line_info
=
1372 (struct dwarf2_debug
*) bfd_zalloc (abfd
, sizeof (struct dwarf2_debug
));
1377 msec
= bfd_get_section_by_name (abfd
, ".debug_info");
1380 /* No dwarf2 info. Note that at this point the stash
1381 has been allocated, but contains zeros, this lets
1382 future calls to this function fail quicker. */
1386 size
= bfd_get_section_size_before_reloc (msec
);
1387 stash
->info_ptr
= (unsigned char*) bfd_alloc (abfd
, size
);
1389 if (! stash
->info_ptr
)
1392 if (! bfd_get_section_contents (abfd
, msec
, stash
->info_ptr
, 0, size
))
1394 stash
->info_ptr
= 0;
1398 stash
->info_ptr_end
= stash
->info_ptr
+ size
;
1402 /* A null info_ptr indicates that there is no dwarf2 info
1403 (or that an error occured while setting up the stash). */
1405 if (! stash
->info_ptr
)
1410 /* Check the previously read comp. units first. */
1412 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
1414 if (comp_unit_contains_address (each
, addr
))
1415 return comp_unit_find_nearest_line (each
, addr
,
1422 /* Read each remaining comp. units checking each as they are read. */
1423 while (stash
->info_ptr
< stash
->info_ptr_end
)
1425 struct comp_unit
* each
;
1426 unsigned int length
;
1428 length
= read_4_bytes (abfd
, stash
->info_ptr
);
1429 stash
->info_ptr
+= 4;
1433 each
= parse_comp_unit (abfd
, stash
->info_ptr
,
1434 stash
->info_ptr
+ length
);
1435 stash
->info_ptr
+= length
;
1439 each
->next_unit
= stash
->all_comp_units
;
1440 stash
->all_comp_units
= each
;
1442 if (comp_unit_contains_address (each
, addr
))
1443 return comp_unit_find_nearest_line (each
, addr
,