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 /* A minimal decoding of DWARF2 compilation units. We only decode
107 what's needed to get to the line number information. */
111 /* Chain the previously read compilation units. */
112 struct comp_unit
* next_unit
;
114 /* Keep the bdf convenient (for memory allocation). */
117 /* The lowest and higest addresses contained in this compilation
118 unit as specified in the compilation unit header. */
122 /* The DW_AT_name attribute (for error messages). */
125 /* The abbrev hash table. */
126 struct abbrev_info
** abbrevs
;
128 /* Note that an error was found by comp_unit_find_nearest_line. */
131 /* The DW_AT_comp_dir attribute */
134 /* True if there is a line number table associated with this comp. unit. */
137 /* The offset into .debug_line of the line number table. */
138 unsigned long line_offset
;
140 /* Pointer to the first child die for the comp unit. */
141 char *first_child_die_ptr
;
143 /* The end of the comp unit. */
146 /* The decoded line number, NULL if not yet decoded. */
147 struct line_info_table
* line_table
;
149 /* A list of the functions found in this comp. unit. */
150 struct funcinfo
* function_table
;
152 /* Address size for this unit - from unit header */
153 unsigned char addr_size
;
159 The following function up to the END VERBATIM mark are
160 copied directly from dwarf2read.c. */
162 /* read dwarf information from a buffer */
165 read_1_byte (abfd
, buf
)
169 return bfd_get_8 (abfd
, (bfd_byte
*) buf
);
173 read_1_signed_byte (abfd
, buf
)
177 return bfd_get_signed_8 (abfd
, (bfd_byte
*) buf
);
181 read_2_bytes (abfd
, buf
)
185 return bfd_get_16 (abfd
, (bfd_byte
*) buf
);
190 /* This is not used. */
193 read_2_signed_bytes (abfd
, buf
)
197 return bfd_get_signed_16 (abfd
, (bfd_byte
*) buf
);
203 read_4_bytes (abfd
, buf
)
207 return bfd_get_32 (abfd
, (bfd_byte
*) buf
);
212 /* This is not used. */
215 read_4_signed_bytes (abfd
, buf
)
219 return bfd_get_signed_32 (abfd
, (bfd_byte
*) buf
);
225 read_8_bytes (abfd
, buf
)
229 return bfd_get_64 (abfd
, (bfd_byte
*) buf
);
233 read_n_bytes (abfd
, buf
, size
)
238 /* If the size of a host char is 8 bits, we can return a pointer
239 to the buffer, otherwise we have to copy the data to a buffer
240 allocated on the temporary obstack. */
245 read_string (abfd
, buf
, bytes_read_ptr
)
248 unsigned int *bytes_read_ptr
;
250 /* If the size of a host char is 8 bits, we can return a pointer
251 to the string, otherwise we have to copy the string to a buffer
252 allocated on the temporary obstack. */
258 *bytes_read_ptr
= strlen (buf
) + 1;
263 read_unsigned_leb128 (abfd
, buf
, bytes_read_ptr
)
266 unsigned int *bytes_read_ptr
;
269 unsigned int num_read
;
279 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
282 result
|= ((byte
& 0x7f) << shift
);
287 * bytes_read_ptr
= num_read
;
293 read_signed_leb128 (abfd
, buf
, bytes_read_ptr
)
296 unsigned int * bytes_read_ptr
;
309 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
312 result
|= ((byte
& 0x7f) << shift
);
317 if ((shift
< 32) && (byte
& 0x40))
318 result
|= -(1 << shift
);
320 * bytes_read_ptr
= num_read
;
328 read_address (unit
, buf
)
329 struct comp_unit
* unit
;
334 if (unit
->addr_size
== 4)
336 retval
= bfd_get_32 (unit
->abfd
, (bfd_byte
*) buf
);
338 retval
= bfd_get_64 (unit
->abfd
, (bfd_byte
*) buf
);
347 /* This data structure holds the information of an abbrev. */
350 unsigned int number
; /* number identifying abbrev */
351 enum dwarf_tag tag
; /* dwarf tag */
352 int has_children
; /* boolean */
353 unsigned int num_attrs
; /* number of attributes */
354 struct attr_abbrev
*attrs
; /* an array of attribute descriptions */
355 struct abbrev_info
*next
; /* next in chain */
360 enum dwarf_attribute name
;
361 enum dwarf_form form
;
364 #ifndef ABBREV_HASH_SIZE
365 #define ABBREV_HASH_SIZE 121
367 #ifndef ATTR_ALLOC_CHUNK
368 #define ATTR_ALLOC_CHUNK 4
371 /* Lookup an abbrev_info structure in the abbrev hash table. */
373 static struct abbrev_info
*
374 lookup_abbrev (number
,abbrevs
)
376 struct abbrev_info
**abbrevs
;
378 unsigned int hash_number
;
379 struct abbrev_info
*abbrev
;
381 hash_number
= number
% ABBREV_HASH_SIZE
;
382 abbrev
= abbrevs
[hash_number
];
386 if (abbrev
->number
== number
)
389 abbrev
= abbrev
->next
;
394 /* In DWARF version 2, the description of the debugging information is
395 stored in a separate .debug_abbrev section. Before we read any
396 dies from a section we read in all abbreviations and install them
399 static struct abbrev_info
**
400 read_abbrevs (abfd
, offset
)
404 struct abbrev_info
**abbrevs
;
406 struct abbrev_info
*cur_abbrev
;
407 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
408 unsigned int abbrev_form
, hash_number
;
409 struct dwarf2_debug
*stash
;
411 stash
= elf_tdata(abfd
)->dwarf2_find_line_info
;
413 if (! stash
->dwarf_abbrev_buffer
)
417 msec
= bfd_get_section_by_name (abfd
, ".debug_abbrev");
420 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_abbrev section."));
421 bfd_set_error (bfd_error_bad_value
);
425 stash
->dwarf_abbrev_size
= bfd_get_section_size_before_reloc (msec
);
426 stash
->dwarf_abbrev_buffer
= (char*) bfd_alloc (abfd
, stash
->dwarf_abbrev_size
);
427 if (! stash
->dwarf_abbrev_buffer
)
430 if (! bfd_get_section_contents (abfd
, msec
,
431 stash
->dwarf_abbrev_buffer
, 0,
432 stash
->dwarf_abbrev_size
))
436 if (offset
> stash
->dwarf_abbrev_size
)
438 (*_bfd_error_handler
) (_("Dwarf Error: Abbrev offset (%u) bigger than abbrev size (%u)."),
439 offset
, stash
->dwarf_abbrev_size
);
440 bfd_set_error (bfd_error_bad_value
);
444 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, sizeof(struct abbrev_info
*) * ABBREV_HASH_SIZE
);
446 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
447 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
448 abbrev_ptr
+= bytes_read
;
450 /* loop until we reach an abbrev number of 0 */
451 while (abbrev_number
)
453 cur_abbrev
= (struct abbrev_info
*)bfd_zalloc (abfd
, sizeof (struct abbrev_info
));
455 /* read in abbrev header */
456 cur_abbrev
->number
= abbrev_number
;
457 cur_abbrev
->tag
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
458 abbrev_ptr
+= bytes_read
;
459 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
);
462 /* now read in declarations */
463 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
464 abbrev_ptr
+= bytes_read
;
465 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
466 abbrev_ptr
+= bytes_read
;
469 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
471 cur_abbrev
->attrs
= (struct attr_abbrev
*)
472 bfd_realloc (cur_abbrev
->attrs
,
473 (cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
)
474 * sizeof (struct attr_abbrev
));
475 if (! cur_abbrev
->attrs
)
478 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
= abbrev_name
;
479 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
++].form
= abbrev_form
;
480 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
481 abbrev_ptr
+= bytes_read
;
482 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
483 abbrev_ptr
+= bytes_read
;
486 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
487 cur_abbrev
->next
= abbrevs
[hash_number
];
488 abbrevs
[hash_number
] = cur_abbrev
;
490 /* Get next abbreviation.
491 Under Irix6 the abbreviations for a compilation unit are not
492 always properly terminated with an abbrev number of 0.
493 Exit loop if we encounter an abbreviation which we have
494 already read (which means we are about to read the abbreviations
495 for the next compile unit) or if the end of the abbreviation
497 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
498 >= stash
->dwarf_abbrev_size
)
500 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
501 abbrev_ptr
+= bytes_read
;
502 if (lookup_abbrev (abbrev_number
,abbrevs
) != NULL
)
509 /* Read an attribute described by an abbreviated attribute. */
512 read_attribute (attr
, abbrev
, unit
, info_ptr
)
513 struct attribute
*attr
;
514 struct attr_abbrev
*abbrev
;
515 struct comp_unit
*unit
;
518 bfd
*abfd
= unit
->abfd
;
519 unsigned int bytes_read
;
520 struct dwarf_block
*blk
;
522 attr
->name
= abbrev
->name
;
523 attr
->form
= abbrev
->form
;
524 switch (abbrev
->form
)
527 case DW_FORM_ref_addr
:
528 DW_ADDR (attr
) = read_address (unit
, info_ptr
);
529 info_ptr
+= unit
->addr_size
;
532 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
533 blk
->size
= read_2_bytes (abfd
, info_ptr
);
535 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
536 info_ptr
+= blk
->size
;
537 DW_BLOCK (attr
) = blk
;
540 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
541 blk
->size
= read_4_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 DW_UNSND (attr
) = read_2_bytes (abfd
, info_ptr
);
552 DW_UNSND (attr
) = read_4_bytes (abfd
, info_ptr
);
556 DW_UNSND (attr
) = read_8_bytes (abfd
, info_ptr
);
560 DW_STRING (attr
) = read_string (abfd
, info_ptr
, &bytes_read
);
561 info_ptr
+= bytes_read
;
564 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
565 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
566 info_ptr
+= bytes_read
;
567 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
568 info_ptr
+= blk
->size
;
569 DW_BLOCK (attr
) = blk
;
572 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
573 blk
->size
= read_1_byte (abfd
, info_ptr
);
575 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
576 info_ptr
+= blk
->size
;
577 DW_BLOCK (attr
) = blk
;
580 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
584 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
588 DW_SND (attr
) = read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
589 info_ptr
+= bytes_read
;
592 DW_UNSND (attr
) = read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
593 info_ptr
+= bytes_read
;
596 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
600 DW_UNSND (attr
) = read_2_bytes (abfd
, info_ptr
);
604 DW_UNSND (attr
) = read_4_bytes (abfd
, info_ptr
);
607 case DW_FORM_ref_udata
:
608 DW_UNSND (attr
) = read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
609 info_ptr
+= bytes_read
;
612 case DW_FORM_indirect
:
614 (*_bfd_error_handler
) (_("Dwarf Error: Invalid or unhandled FORM value: %d."),
616 bfd_set_error (bfd_error_bad_value
);
622 /* Source line information table routines. */
624 #define FILE_ALLOC_CHUNK 5
625 #define DIR_ALLOC_CHUNK 5
628 struct line_info
* prev_line
;
634 int end_sequence
; /* end of (sequential) code sequence */
644 struct line_info_table
{
647 unsigned int num_files
;
648 unsigned int num_dirs
;
652 struct fileinfo
* files
;
653 struct line_info
* last_line
;
657 add_line_info (table
, address
, filename
, line
, column
, end_sequence
)
658 struct line_info_table
* table
;
665 struct line_info
* info
= (struct line_info
*)
666 bfd_alloc (table
->abfd
, sizeof (struct line_info
));
668 info
->prev_line
= table
->last_line
;
669 table
->last_line
= info
;
671 info
->address
= address
;
672 info
->filename
= filename
;
674 info
->column
= column
;
675 info
->end_sequence
= end_sequence
;
679 concat_filename (table
, file
)
680 struct line_info_table
* table
;
685 if (file
- 1 >= table
->num_files
)
687 (*_bfd_error_handler
) (_("Dwarf Error: mangled line number "
688 "section (bad file number)."));
692 filename
= table
->files
[file
- 1].name
;
693 if (*filename
== '/')
698 char* dirname
= (table
->files
[file
- 1].dir
699 ? table
->dirs
[table
->files
[file
- 1].dir
- 1]
701 return (char*) concat (dirname
, "/", filename
, NULL
);
705 /* Decode the line number information for UNIT. */
707 static struct line_info_table
*
708 decode_line_info (unit
)
709 struct comp_unit
*unit
;
711 bfd
*abfd
= unit
->abfd
;
713 static char* dwarf_line_buffer
= 0;
715 struct line_info_table
* table
;
720 unsigned int i
, bytes_read
;
721 char *cur_file
, *cur_dir
;
722 unsigned char op_code
, extended_op
, adj_opcode
;
724 /* This optimization unfortunately does not work well on programs
725 that have multiple sections containing text (such as Linux which
726 uses .text, and .text.init, for example. */
727 bfd_vma hi_pc
= 0, lo_pc
= ~ (bfd_vma
) 0;
730 if (! dwarf_line_buffer
)
735 msec
= bfd_get_section_by_name (abfd
, ".debug_line");
738 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_line section."));
739 bfd_set_error (bfd_error_bad_value
);
743 size
= bfd_get_section_size_before_reloc (msec
);
744 dwarf_line_buffer
= (char*) bfd_alloc (abfd
, size
);
745 if (! dwarf_line_buffer
)
748 if (! bfd_get_section_contents (abfd
, msec
,
749 dwarf_line_buffer
, 0,
753 /* FIXME: We ought to apply the relocs against this section before
757 table
= (struct line_info_table
*) bfd_alloc (abfd
,
758 sizeof (struct line_info_table
));
760 table
->comp_dir
= unit
->comp_dir
;
762 table
->num_files
= 0;
769 table
->last_line
= NULL
;
771 line_ptr
= dwarf_line_buffer
+ unit
->line_offset
;
773 /* read in the prologue */
774 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
776 line_end
= line_ptr
+ lh
.total_length
;
777 lh
.version
= read_2_bytes (abfd
, line_ptr
);
779 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
);
781 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
);
783 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
);
785 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
);
787 lh
.line_range
= read_1_byte (abfd
, line_ptr
);
789 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
);
791 lh
.standard_opcode_lengths
= (unsigned char *)
792 bfd_alloc (abfd
, lh
.opcode_base
* sizeof (unsigned char));
794 lh
.standard_opcode_lengths
[0] = 1;
795 for (i
= 1; i
< lh
.opcode_base
; ++i
)
797 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
);
801 /* Read directory table */
802 while ((cur_dir
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
804 line_ptr
+= bytes_read
;
805 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
807 table
->dirs
= (char **)
808 bfd_realloc (table
->dirs
,
809 (table
->num_dirs
+ DIR_ALLOC_CHUNK
) * sizeof (char *));
813 table
->dirs
[table
->num_dirs
++] = cur_dir
;
815 line_ptr
+= bytes_read
;
817 /* Read file name table */
818 while ((cur_file
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
820 line_ptr
+= bytes_read
;
821 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
823 table
->files
= (struct fileinfo
*)
824 bfd_realloc (table
->files
,
825 (table
->num_files
+ FILE_ALLOC_CHUNK
)
826 * sizeof (struct fileinfo
));
830 table
->files
[table
->num_files
].name
= cur_file
;
831 table
->files
[table
->num_files
].dir
=
832 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
833 line_ptr
+= bytes_read
;
834 table
->files
[table
->num_files
].time
=
835 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
836 line_ptr
+= bytes_read
;
837 table
->files
[table
->num_files
].size
=
838 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
839 line_ptr
+= bytes_read
;
842 line_ptr
+= bytes_read
;
844 /* Read the statement sequences until there's nothing left. */
845 while (line_ptr
< line_end
)
847 /* state machine registers */
849 char* filename
= concat_filename (table
, 1);
850 unsigned int line
= 1;
851 unsigned int column
= 0;
852 int is_stmt
= lh
.default_is_stmt
;
854 int end_sequence
= 0;
855 boolean first_time
= true;
857 /* Decode the table. */
858 while (! end_sequence
)
860 op_code
= read_1_byte (abfd
, line_ptr
);
864 case DW_LNS_extended_op
:
865 line_ptr
+= 1; /* ignore length */
866 extended_op
= read_1_byte (abfd
, line_ptr
);
870 case DW_LNE_end_sequence
:
872 add_line_info (table
, address
, filename
, line
, column
, end_sequence
);
874 case DW_LNE_set_address
:
875 address
= read_address (unit
, line_ptr
);
876 line_ptr
+= unit
->addr_size
;
878 case DW_LNE_define_file
:
879 cur_file
= read_string (abfd
, line_ptr
, &bytes_read
);
880 line_ptr
+= bytes_read
;
881 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
883 table
->files
= (struct fileinfo
*)
884 bfd_realloc (table
->files
,
885 (table
->num_files
+ FILE_ALLOC_CHUNK
)
886 * sizeof (struct fileinfo
));
890 table
->files
[table
->num_files
].name
= cur_file
;
891 table
->files
[table
->num_files
].dir
=
892 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
893 line_ptr
+= bytes_read
;
894 table
->files
[table
->num_files
].time
=
895 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
896 line_ptr
+= bytes_read
;
897 table
->files
[table
->num_files
].size
=
898 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
899 line_ptr
+= bytes_read
;
903 (*_bfd_error_handler
) (_("Dwarf Error: mangled line number section."));
904 bfd_set_error (bfd_error_bad_value
);
909 add_line_info (table
, address
, filename
, line
, column
, 0);
912 case DW_LNS_advance_pc
:
913 address
+= lh
.minimum_instruction_length
914 * read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
915 line_ptr
+= bytes_read
;
917 case DW_LNS_advance_line
:
918 line
+= read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
919 line_ptr
+= bytes_read
;
921 case DW_LNS_set_file
:
925 /* The file and directory tables are 0 based, the references
927 file
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
928 line_ptr
+= bytes_read
;
929 filename
= concat_filename (table
, file
);
932 case DW_LNS_set_column
:
933 column
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
934 line_ptr
+= bytes_read
;
936 case DW_LNS_negate_stmt
:
937 is_stmt
= (!is_stmt
);
939 case DW_LNS_set_basic_block
:
942 case DW_LNS_const_add_pc
:
943 address
+= lh
.minimum_instruction_length
944 * ((255 - lh
.opcode_base
) / lh
.line_range
);
946 case DW_LNS_fixed_advance_pc
:
947 address
+= read_2_bytes (abfd
, line_ptr
);
950 default: /* special operand */
951 adj_opcode
= op_code
- lh
.opcode_base
;
952 address
+= (adj_opcode
/ lh
.line_range
)
953 * lh
.minimum_instruction_length
;
954 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
955 /* append row to matrix using current values */
956 add_line_info (table
, address
, filename
, line
, column
, 0);
971 if (unit
->high
== 0 && hi_pc
!= 0)
982 /* If ADDR is within TABLE set the output parameters and return true,
983 otherwise return false. The output parameters, FILENAME_PTR and
984 LINENUMBER_PTR, are pointers to the objects to be filled in. */
987 lookup_address_in_line_info_table (table
,
991 struct line_info_table
* table
;
993 const char **filename_ptr
;
994 unsigned int *linenumber_ptr
;
996 struct line_info
* next_line
= table
->last_line
;
997 struct line_info
* each_line
;
1002 each_line
= next_line
->prev_line
;
1004 while (each_line
&& next_line
)
1006 if (!each_line
->end_sequence
1007 && addr
>= each_line
->address
&& addr
< next_line
->address
)
1009 *filename_ptr
= each_line
->filename
;
1010 *linenumber_ptr
= each_line
->line
;
1013 next_line
= each_line
;
1014 each_line
= each_line
->prev_line
;
1023 /* Function table functions. */
1026 struct funcinfo
*prev_func
;
1034 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
1037 lookup_address_in_function_table (table
,
1040 struct funcinfo
* table
;
1042 const char **functionname_ptr
;
1044 struct funcinfo
* each_func
;
1046 for (each_func
= table
;
1048 each_func
= each_func
->prev_func
)
1050 if (addr
>= each_func
->low
&& addr
< each_func
->high
)
1052 *functionname_ptr
= each_func
->name
;
1063 /* DWARF2 Compilation unit functions. */
1066 /* Scan over each die in a comp. unit looking for functions to add
1067 to the function table. */
1070 scan_unit_for_functions (unit
)
1071 struct comp_unit
*unit
;
1073 bfd
*abfd
= unit
->abfd
;
1074 char *info_ptr
= unit
->first_child_die_ptr
;
1075 int nesting_level
= 1;
1077 while (nesting_level
)
1079 unsigned int abbrev_number
, bytes_read
, i
;
1080 struct abbrev_info
*abbrev
;
1081 struct attribute attr
;
1082 struct funcinfo
*func
;
1085 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1086 info_ptr
+= bytes_read
;
1088 if (! abbrev_number
)
1094 abbrev
= lookup_abbrev (abbrev_number
,unit
->abbrevs
);
1097 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %d."),
1099 bfd_set_error (bfd_error_bad_value
);
1103 if (abbrev
->tag
== DW_TAG_subprogram
)
1105 func
= (struct funcinfo
*) bfd_zalloc (abfd
, sizeof (struct funcinfo
));
1106 func
->prev_func
= unit
->function_table
;
1107 unit
->function_table
= func
;
1112 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1114 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1122 name
= DW_STRING (&attr
);
1124 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1125 if (func
->name
== NULL
)
1126 func
->name
= DW_STRING (&attr
);
1129 case DW_AT_MIPS_linkage_name
:
1130 func
->name
= DW_STRING (&attr
);
1134 func
->low
= DW_ADDR (&attr
);
1138 func
->high
= DW_ADDR (&attr
);
1150 name
= DW_STRING (&attr
);
1159 if (abbrev
->has_children
)
1171 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This includes
1172 the compilation unit header that proceeds the DIE's, but does not
1173 include the length field that preceeds each compilation unit header.
1174 END_PTR points one past the end of this comp unit.
1176 This routine does not read the whole compilation unit; only enough
1177 to get to the line number information for the compilation unit. */
1179 static struct comp_unit
*
1180 parse_comp_unit (abfd
, info_ptr
, end_ptr
)
1185 struct comp_unit
* unit
;
1187 unsigned short version
;
1188 unsigned int abbrev_offset
;
1189 unsigned char addr_size
;
1190 struct abbrev_info
** abbrevs
;
1192 unsigned int abbrev_number
, bytes_read
, i
;
1193 struct abbrev_info
*abbrev
;
1194 struct attribute attr
;
1196 version
= read_2_bytes (abfd
, info_ptr
);
1198 abbrev_offset
= read_4_bytes (abfd
, info_ptr
);
1200 addr_size
= read_1_byte (abfd
, info_ptr
);
1205 (*_bfd_error_handler
) (_("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information."), version
);
1206 bfd_set_error (bfd_error_bad_value
);
1210 if (addr_size
> sizeof (bfd_vma
))
1212 (*_bfd_error_handler
) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1215 bfd_set_error (bfd_error_bad_value
);
1219 if (addr_size
!= 4 && addr_size
!= 8)
1221 (*_bfd_error_handler
) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '4' and '8'.", addr_size
);
1222 bfd_set_error (bfd_error_bad_value
);
1226 /* Read the abbrevs for this compilation unit into a table */
1227 abbrevs
= read_abbrevs (abfd
, abbrev_offset
);
1231 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1232 info_ptr
+= bytes_read
;
1233 if (! abbrev_number
)
1235 (*_bfd_error_handler
) (_("Dwarf Error: Bad abbrev number: %d."),
1237 bfd_set_error (bfd_error_bad_value
);
1241 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
1244 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %d."),
1246 bfd_set_error (bfd_error_bad_value
);
1250 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, sizeof (struct comp_unit
));
1252 unit
->addr_size
= addr_size
;
1253 unit
->abbrevs
= abbrevs
;
1254 unit
->end_ptr
= end_ptr
;
1256 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1258 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1260 /* Store the data if it is of an attribute we want to keep in a
1261 partial symbol table. */
1264 case DW_AT_stmt_list
:
1266 unit
->line_offset
= DW_UNSND (&attr
);
1270 unit
->name
= DW_STRING (&attr
);
1274 unit
->low
= DW_ADDR (&attr
);
1278 unit
->high
= DW_ADDR (&attr
);
1281 case DW_AT_comp_dir
:
1283 char* comp_dir
= DW_STRING (&attr
);
1286 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1287 directory, get rid of it. */
1288 char *cp
= (char*) strchr (comp_dir
, ':');
1290 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
1293 unit
->comp_dir
= comp_dir
;
1302 unit
->first_child_die_ptr
= info_ptr
;
1310 /* Return true if UNIT contains the address given by ADDR. */
1313 comp_unit_contains_address (unit
, addr
)
1314 struct comp_unit
* unit
;
1317 return ! unit
->error
1318 && (addr
>= unit
->low
&& addr
<= unit
->high
);
1322 /* If UNIT contains ADDR, set the output parameters to the values for
1323 the line containing ADDR. The output parameters, FILENAME_PTR,
1324 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1327 Return true of UNIT contains ADDR, and no errors were encountered;
1331 comp_unit_find_nearest_line (unit
, addr
,
1332 filename_ptr
, functionname_ptr
, linenumber_ptr
)
1333 struct comp_unit
* unit
;
1335 const char **filename_ptr
;
1336 const char **functionname_ptr
;
1337 unsigned int *linenumber_ptr
;
1345 if (! unit
->line_table
)
1347 if (! unit
->stmtlist
)
1353 unit
->line_table
= decode_line_info (unit
);
1355 if (! unit
->line_table
)
1361 if (! scan_unit_for_functions (unit
))
1368 line_p
= lookup_address_in_line_info_table (unit
->line_table
,
1372 func_p
= lookup_address_in_function_table (unit
->function_table
,
1375 return line_p
|| func_p
;
1378 /* The DWARF2 version of find_nearest line.
1379 Return true if the line is found without error. */
1382 _bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
1383 filename_ptr
, functionname_ptr
, linenumber_ptr
)
1388 const char **filename_ptr
;
1389 const char **functionname_ptr
;
1390 unsigned int *linenumber_ptr
;
1392 /* Read each compilation unit from the section .debug_info, and check
1393 to see if it contains the address we are searching for. If yes,
1394 lookup the address, and return the line number info. If no, go
1395 on to the next compilation unit.
1397 We keep a list of all the previously read compilation units, and
1398 a pointer to the next un-read compilation unit. Check the
1399 previously read units before reading more.
1402 struct dwarf2_debug
*stash
= elf_tdata (abfd
)->dwarf2_find_line_info
;
1404 /* What address are we looking for? */
1405 bfd_vma addr
= offset
+ section
->vma
;
1407 struct comp_unit
* each
;
1411 *filename_ptr
= NULL
;
1412 *functionname_ptr
= NULL
;
1413 *linenumber_ptr
= 0;
1420 stash
= elf_tdata (abfd
)->dwarf2_find_line_info
=
1421 (struct dwarf2_debug
*) bfd_zalloc (abfd
, sizeof (struct dwarf2_debug
));
1426 msec
= bfd_get_section_by_name (abfd
, ".debug_info");
1429 /* No dwarf2 info. Note that at this point the stash
1430 has been allocated, but contains zeros, this lets
1431 future calls to this function fail quicker. */
1435 size
= bfd_get_section_size_before_reloc (msec
);
1439 stash
->info_ptr
= (char *) bfd_alloc (abfd
, size
);
1441 if (! stash
->info_ptr
)
1444 if (! bfd_get_section_contents (abfd
, msec
, stash
->info_ptr
, 0, size
))
1446 stash
->info_ptr
= 0;
1450 stash
->info_ptr_end
= stash
->info_ptr
+ size
;
1452 /* FIXME: There is a problem with the contents of the
1453 .debug_info section. The 'low' and 'high' addresses of the
1454 comp_units are computed by relocs against symbols in the
1455 .text segment. We need these addresses in order to determine
1456 the nearest line number, and so we have to resolve the
1457 relocs. There is a similar problem when the .debug_line
1458 section is processed as well (e.g., there may be relocs
1459 against the operand of the DW_LNE_set_address operator).
1461 Unfortunately getting hold of the reloc information is hard...
1463 For now, this means that disassembling object files (as
1464 opposed to fully executables) does not always work as well as
1468 /* A null info_ptr indicates that there is no dwarf2 info
1469 (or that an error occured while setting up the stash). */
1471 if (! stash
->info_ptr
)
1474 /* Check the previously read comp. units first. */
1476 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
1480 if (comp_unit_contains_address (each
, addr
))
1481 return comp_unit_find_nearest_line (each
, addr
,
1488 found
= comp_unit_find_nearest_line (each
, addr
,
1497 /* Read each remaining comp. units checking each as they are read. */
1498 while (stash
->info_ptr
< stash
->info_ptr_end
)
1500 struct comp_unit
* each
;
1501 unsigned int length
;
1503 length
= read_4_bytes (abfd
, stash
->info_ptr
);
1504 stash
->info_ptr
+= 4;
1508 each
= parse_comp_unit (abfd
, stash
->info_ptr
,
1509 stash
->info_ptr
+ length
);
1510 stash
->info_ptr
+= length
;
1514 each
->next_unit
= stash
->all_comp_units
;
1515 stash
->all_comp_units
= each
;
1517 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1518 compilation units. If we don't have them (i.e.,
1519 unit->high == 0), we need to consult the line info
1520 table to see if a compilation unit contains the given
1524 if (comp_unit_contains_address (each
, addr
))
1525 return comp_unit_find_nearest_line (each
, addr
,
1532 found
= comp_unit_find_nearest_line (each
, addr
,