2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
8 From the dwarf2read.c header:
9 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10 Inc. with support from Florida State University (under contract
11 with the Ada Joint Program Office), and Silicon Graphics, Inc.
12 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14 support in dwarfread.c
16 This file is part of BFD.
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or (at
21 your option) any later version.
23 This program is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
34 #include "libiberty.h"
37 #include "elf/dwarf2.h"
39 /* The data in the .debug_line statement prologue looks like this. */
43 unsigned int total_length
;
44 unsigned short version
;
45 unsigned int prologue_length
;
46 unsigned char minimum_instruction_length
;
47 unsigned char default_is_stmt
;
49 unsigned char line_range
;
50 unsigned char opcode_base
;
51 unsigned char *standard_opcode_lengths
;
54 /* Attributes have a name and a value. */
58 enum dwarf_attribute name
;
63 struct dwarf_block
*blk
;
71 /* Get at parts of an attribute structure. */
73 #define DW_STRING(attr) ((attr)->u.str)
74 #define DW_UNSND(attr) ((attr)->u.unsnd)
75 #define DW_BLOCK(attr) ((attr)->u.blk)
76 #define DW_SND(attr) ((attr)->u.snd)
77 #define DW_ADDR(attr) ((attr)->u.addr)
79 /* Blocks are a bunch of untyped bytes. */
88 /* A list of all previously read comp_units. */
89 struct comp_unit
* all_comp_units
;
91 /* The next unread compilation unit within the .debug_info section.
92 Zero indicates that the .debug_info section has not been loaded
96 /* Pointer to the end of the .debug_info section memory buffer. */
99 /* Pointer to the section and address of the beginning of the
104 /* Pointer to the symbol table. */
107 /* Pointer to the .debug_abbrev section loaded into memory. */
108 char* dwarf_abbrev_buffer
;
110 /* Length of the loaded .debug_abbrev section. */
111 unsigned long dwarf_abbrev_size
;
113 /* Buffer for decode_line_info. */
114 char *dwarf_line_buffer
;
116 /* Length of the loaded .debug_line section. */
117 unsigned long dwarf_line_size
;
127 /* A minimal decoding of DWARF2 compilation units. We only decode
128 what's needed to get to the line number information. */
132 /* Chain the previously read compilation units. */
133 struct comp_unit
* next_unit
;
135 /* Keep the bdf convenient (for memory allocation). */
138 /* The lowest and higest addresses contained in this compilation
139 unit as specified in the compilation unit header. */
140 struct arange arange
;
142 /* The DW_AT_name attribute (for error messages). */
145 /* The abbrev hash table. */
146 struct abbrev_info
** abbrevs
;
148 /* Note that an error was found by comp_unit_find_nearest_line. */
151 /* The DW_AT_comp_dir attribute. */
154 /* True if there is a line number table associated with this comp. unit. */
157 /* The offset into .debug_line of the line number table. */
158 unsigned long line_offset
;
160 /* Pointer to the first child die for the comp unit. */
161 char *first_child_die_ptr
;
163 /* The end of the comp unit. */
166 /* The decoded line number, NULL if not yet decoded. */
167 struct line_info_table
* line_table
;
169 /* A list of the functions found in this comp. unit. */
170 struct funcinfo
* function_table
;
172 /* Address size for this unit - from unit header. */
173 unsigned char addr_size
;
177 The following function up to the END VERBATIM mark are
178 copied directly from dwarf2read.c. */
180 /* Read dwarf information from a buffer. */
183 read_1_byte (abfd
, buf
)
184 bfd
*abfd ATTRIBUTE_UNUSED
;
187 return bfd_get_8 (abfd
, (bfd_byte
*) buf
);
191 read_1_signed_byte (abfd
, buf
)
192 bfd
*abfd ATTRIBUTE_UNUSED
;
195 return bfd_get_signed_8 (abfd
, (bfd_byte
*) buf
);
199 read_2_bytes (abfd
, buf
)
203 return bfd_get_16 (abfd
, (bfd_byte
*) buf
);
206 #if 0 /* This is not used. */
209 read_2_signed_bytes (abfd
, buf
)
213 return bfd_get_signed_16 (abfd
, (bfd_byte
*) buf
);
219 read_4_bytes (abfd
, buf
)
223 return bfd_get_32 (abfd
, (bfd_byte
*) buf
);
226 #if 0 /* This is not used. */
229 read_4_signed_bytes (abfd
, buf
)
233 return bfd_get_signed_32 (abfd
, (bfd_byte
*) buf
);
239 read_8_bytes (abfd
, buf
)
243 return bfd_get_64 (abfd
, (bfd_byte
*) buf
);
247 read_n_bytes (abfd
, buf
, size
)
248 bfd
*abfd ATTRIBUTE_UNUSED
;
250 unsigned int size ATTRIBUTE_UNUSED
;
252 /* If the size of a host char is 8 bits, we can return a pointer
253 to the buffer, otherwise we have to copy the data to a buffer
254 allocated on the temporary obstack. */
259 read_string (abfd
, buf
, bytes_read_ptr
)
260 bfd
*abfd ATTRIBUTE_UNUSED
;
262 unsigned int *bytes_read_ptr
;
264 /* If the size of a host char is 8 bits, we can return a pointer
265 to the string, otherwise we have to copy the string to a buffer
266 allocated on the temporary obstack. */
273 *bytes_read_ptr
= strlen (buf
) + 1;
278 read_unsigned_leb128 (abfd
, buf
, bytes_read_ptr
)
279 bfd
*abfd ATTRIBUTE_UNUSED
;
281 unsigned int *bytes_read_ptr
;
284 unsigned int num_read
;
294 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
297 result
|= ((byte
& 0x7f) << shift
);
302 * bytes_read_ptr
= num_read
;
308 read_signed_leb128 (abfd
, buf
, bytes_read_ptr
)
309 bfd
*abfd ATTRIBUTE_UNUSED
;
311 unsigned int * bytes_read_ptr
;
324 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
327 result
|= ((byte
& 0x7f) << shift
);
332 if ((shift
< 32) && (byte
& 0x40))
333 result
|= -(1 << shift
);
335 * bytes_read_ptr
= num_read
;
343 read_address (unit
, buf
)
344 struct comp_unit
* unit
;
347 switch (unit
->addr_size
)
350 return bfd_get_64 (unit
->abfd
, (bfd_byte
*) buf
);
352 return bfd_get_32 (unit
->abfd
, (bfd_byte
*) buf
);
354 return bfd_get_16 (unit
->abfd
, (bfd_byte
*) buf
);
360 /* This data structure holds the information of an abbrev. */
363 unsigned int number
; /* Number identifying abbrev. */
364 enum dwarf_tag tag
; /* DWARF tag. */
365 int has_children
; /* Boolean. */
366 unsigned int num_attrs
; /* Number of attributes. */
367 struct attr_abbrev
*attrs
; /* An array of attribute descriptions. */
368 struct abbrev_info
*next
; /* Next in chain. */
373 enum dwarf_attribute name
;
374 enum dwarf_form form
;
377 #ifndef ABBREV_HASH_SIZE
378 #define ABBREV_HASH_SIZE 121
380 #ifndef ATTR_ALLOC_CHUNK
381 #define ATTR_ALLOC_CHUNK 4
384 /* Lookup an abbrev_info structure in the abbrev hash table. */
386 static struct abbrev_info
*
387 lookup_abbrev (number
,abbrevs
)
389 struct abbrev_info
**abbrevs
;
391 unsigned int hash_number
;
392 struct abbrev_info
*abbrev
;
394 hash_number
= number
% ABBREV_HASH_SIZE
;
395 abbrev
= abbrevs
[hash_number
];
399 if (abbrev
->number
== number
)
402 abbrev
= abbrev
->next
;
408 /* In DWARF version 2, the description of the debugging information is
409 stored in a separate .debug_abbrev section. Before we read any
410 dies from a section we read in all abbreviations and install them
413 static struct abbrev_info
**
414 read_abbrevs (abfd
, offset
, stash
)
417 struct dwarf2_debug
*stash
;
419 struct abbrev_info
**abbrevs
;
421 struct abbrev_info
*cur_abbrev
;
422 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
423 unsigned int abbrev_form
, hash_number
;
425 if (! stash
->dwarf_abbrev_buffer
)
429 msec
= bfd_get_section_by_name (abfd
, ".debug_abbrev");
432 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_abbrev section."));
433 bfd_set_error (bfd_error_bad_value
);
437 stash
->dwarf_abbrev_size
= msec
->_raw_size
;
438 stash
->dwarf_abbrev_buffer
= (char*) bfd_alloc (abfd
, stash
->dwarf_abbrev_size
);
439 if (! stash
->dwarf_abbrev_buffer
)
442 if (! bfd_get_section_contents (abfd
, msec
,
443 stash
->dwarf_abbrev_buffer
, 0,
444 stash
->dwarf_abbrev_size
))
448 if (offset
>= stash
->dwarf_abbrev_size
)
450 (*_bfd_error_handler
) (_("Dwarf Error: Abbrev offset (%u) greater than or equal to abbrev size (%u)."),
451 offset
, stash
->dwarf_abbrev_size
);
452 bfd_set_error (bfd_error_bad_value
);
456 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
);
458 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
459 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
460 abbrev_ptr
+= bytes_read
;
462 /* Loop until we reach an abbrev number of 0. */
463 while (abbrev_number
)
465 cur_abbrev
= (struct abbrev_info
*)bfd_zalloc (abfd
, sizeof (struct abbrev_info
));
467 /* Read in abbrev header. */
468 cur_abbrev
->number
= abbrev_number
;
469 cur_abbrev
->tag
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
470 abbrev_ptr
+= bytes_read
;
471 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
);
474 /* Now read in declarations. */
475 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
476 abbrev_ptr
+= bytes_read
;
477 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
478 abbrev_ptr
+= bytes_read
;
482 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
484 cur_abbrev
->attrs
= (struct attr_abbrev
*)
485 bfd_realloc (cur_abbrev
->attrs
,
486 (cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
)
487 * sizeof (struct attr_abbrev
));
488 if (! cur_abbrev
->attrs
)
492 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
= abbrev_name
;
493 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
++].form
= abbrev_form
;
494 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
495 abbrev_ptr
+= bytes_read
;
496 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
497 abbrev_ptr
+= bytes_read
;
500 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
501 cur_abbrev
->next
= abbrevs
[hash_number
];
502 abbrevs
[hash_number
] = cur_abbrev
;
504 /* Get next abbreviation.
505 Under Irix6 the abbreviations for a compilation unit are not
506 always properly terminated with an abbrev number of 0.
507 Exit loop if we encounter an abbreviation which we have
508 already read (which means we are about to read the abbreviations
509 for the next compile unit) or if the end of the abbreviation
511 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
512 >= stash
->dwarf_abbrev_size
)
514 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
515 abbrev_ptr
+= bytes_read
;
516 if (lookup_abbrev (abbrev_number
,abbrevs
) != NULL
)
523 /* Read an attribute described by an abbreviated attribute. */
526 read_attribute (attr
, abbrev
, unit
, info_ptr
)
527 struct attribute
*attr
;
528 struct attr_abbrev
*abbrev
;
529 struct comp_unit
*unit
;
532 bfd
*abfd
= unit
->abfd
;
533 unsigned int bytes_read
;
534 struct dwarf_block
*blk
;
536 attr
->name
= abbrev
->name
;
537 attr
->form
= abbrev
->form
;
539 switch (abbrev
->form
)
542 case DW_FORM_ref_addr
:
543 DW_ADDR (attr
) = read_address (unit
, info_ptr
);
544 info_ptr
+= unit
->addr_size
;
547 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
548 blk
->size
= read_2_bytes (abfd
, info_ptr
);
550 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
551 info_ptr
+= blk
->size
;
552 DW_BLOCK (attr
) = blk
;
555 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
556 blk
->size
= read_4_bytes (abfd
, info_ptr
);
558 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
559 info_ptr
+= blk
->size
;
560 DW_BLOCK (attr
) = blk
;
563 DW_UNSND (attr
) = read_2_bytes (abfd
, info_ptr
);
567 DW_UNSND (attr
) = read_4_bytes (abfd
, info_ptr
);
571 DW_UNSND (attr
) = read_8_bytes (abfd
, info_ptr
);
575 DW_STRING (attr
) = read_string (abfd
, info_ptr
, &bytes_read
);
576 info_ptr
+= bytes_read
;
579 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
580 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
581 info_ptr
+= bytes_read
;
582 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
583 info_ptr
+= blk
->size
;
584 DW_BLOCK (attr
) = blk
;
587 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (struct dwarf_block
));
588 blk
->size
= read_1_byte (abfd
, info_ptr
);
590 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
591 info_ptr
+= blk
->size
;
592 DW_BLOCK (attr
) = blk
;
595 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
599 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
603 DW_SND (attr
) = read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
604 info_ptr
+= bytes_read
;
607 DW_UNSND (attr
) = read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
608 info_ptr
+= bytes_read
;
611 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
615 DW_UNSND (attr
) = read_2_bytes (abfd
, info_ptr
);
619 DW_UNSND (attr
) = read_4_bytes (abfd
, info_ptr
);
623 DW_UNSND (attr
) = read_8_bytes (abfd
, info_ptr
);
626 case DW_FORM_ref_udata
:
627 DW_UNSND (attr
) = read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
628 info_ptr
+= bytes_read
;
631 case DW_FORM_indirect
:
633 (*_bfd_error_handler
) (_("Dwarf Error: Invalid or unhandled FORM value: %d."),
635 bfd_set_error (bfd_error_bad_value
);
640 /* Source line information table routines. */
642 #define FILE_ALLOC_CHUNK 5
643 #define DIR_ALLOC_CHUNK 5
647 struct line_info
* prev_line
;
652 int end_sequence
; /* End of (sequential) code sequence. */
663 struct line_info_table
666 unsigned int num_files
;
667 unsigned int num_dirs
;
670 struct fileinfo
* files
;
671 struct line_info
* last_line
;
675 add_line_info (table
, address
, filename
, line
, column
, end_sequence
)
676 struct line_info_table
* table
;
683 struct line_info
* info
= (struct line_info
*)
684 bfd_alloc (table
->abfd
, sizeof (struct line_info
));
686 info
->prev_line
= table
->last_line
;
687 table
->last_line
= info
;
689 info
->address
= address
;
690 info
->filename
= filename
;
692 info
->column
= column
;
693 info
->end_sequence
= end_sequence
;
697 concat_filename (table
, file
)
698 struct line_info_table
* table
;
703 if (file
- 1 >= table
->num_files
)
705 (*_bfd_error_handler
)
706 (_("Dwarf Error: mangled line number section (bad file number)."));
710 filename
= table
->files
[file
- 1].name
;
711 if (IS_ABSOLUTE_PATH(filename
))
716 char* dirname
= (table
->files
[file
- 1].dir
717 ? table
->dirs
[table
->files
[file
- 1].dir
- 1]
719 return (char*) concat (dirname
, "/", filename
, NULL
);
724 arange_add (unit
, low_pc
, high_pc
)
725 struct comp_unit
*unit
;
729 struct arange
*arange
;
731 /* First see if we can cheaply extend an existing range. */
732 arange
= &unit
->arange
;
736 if (low_pc
== arange
->high
)
738 arange
->high
= high_pc
;
741 if (high_pc
== arange
->low
)
743 arange
->low
= low_pc
;
746 arange
= arange
->next
;
750 if (unit
->arange
.high
== 0)
752 /* This is the first address range: store it in unit->arange. */
753 unit
->arange
.next
= 0;
754 unit
->arange
.low
= low_pc
;
755 unit
->arange
.high
= high_pc
;
759 /* Need to allocate a new arange and insert it into the arange list. */
760 arange
= bfd_zalloc (unit
->abfd
, sizeof (*arange
));
761 arange
->low
= low_pc
;
762 arange
->high
= high_pc
;
764 arange
->next
= unit
->arange
.next
;
765 unit
->arange
.next
= arange
;
768 /* Decode the line number information for UNIT. */
770 static struct line_info_table
*
771 decode_line_info (unit
, stash
)
772 struct comp_unit
*unit
;
773 struct dwarf2_debug
*stash
;
775 bfd
*abfd
= unit
->abfd
;
776 struct line_info_table
* table
;
780 unsigned int i
, bytes_read
;
781 char *cur_file
, *cur_dir
;
782 unsigned char op_code
, extended_op
, adj_opcode
;
784 if (! stash
->dwarf_line_buffer
)
788 msec
= bfd_get_section_by_name (abfd
, ".debug_line");
791 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_line section."));
792 bfd_set_error (bfd_error_bad_value
);
796 stash
->dwarf_line_size
= msec
->_raw_size
;
797 stash
->dwarf_line_buffer
= (char *) bfd_alloc (abfd
, stash
->dwarf_line_size
);
798 if (! stash
->dwarf_line_buffer
)
801 if (! bfd_get_section_contents (abfd
, msec
,
802 stash
->dwarf_line_buffer
, 0,
803 stash
->dwarf_line_size
))
806 /* FIXME: We ought to apply the relocs against this section before
810 /* Since we are using un-relocated data, it is possible to get a bad value
811 for the line_offset. Validate it here so that we won't get a segfault
813 if (unit
->line_offset
>= stash
->dwarf_line_size
)
815 (*_bfd_error_handler
) (_("Dwarf Error: Line offset (%u) greater than or equal to line size (%u)."),
816 unit
->line_offset
, stash
->dwarf_line_size
);
817 bfd_set_error (bfd_error_bad_value
);
821 table
= (struct line_info_table
*) bfd_alloc (abfd
,
822 sizeof (struct line_info_table
));
824 table
->comp_dir
= unit
->comp_dir
;
826 table
->num_files
= 0;
833 table
->last_line
= NULL
;
835 line_ptr
= stash
->dwarf_line_buffer
+ unit
->line_offset
;
837 /* Read in the prologue. */
838 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
840 line_end
= line_ptr
+ lh
.total_length
;
841 lh
.version
= read_2_bytes (abfd
, line_ptr
);
843 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
);
845 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
);
847 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
);
849 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
);
851 lh
.line_range
= read_1_byte (abfd
, line_ptr
);
853 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
);
855 lh
.standard_opcode_lengths
= (unsigned char *)
856 bfd_alloc (abfd
, lh
.opcode_base
* sizeof (unsigned char));
858 lh
.standard_opcode_lengths
[0] = 1;
860 for (i
= 1; i
< lh
.opcode_base
; ++i
)
862 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
);
866 /* Read directory table. */
867 while ((cur_dir
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
869 line_ptr
+= bytes_read
;
871 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
873 table
->dirs
= (char **)
874 bfd_realloc (table
->dirs
,
875 (table
->num_dirs
+ DIR_ALLOC_CHUNK
) * sizeof (char *));
880 table
->dirs
[table
->num_dirs
++] = cur_dir
;
883 line_ptr
+= bytes_read
;
885 /* Read file name table. */
886 while ((cur_file
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
888 line_ptr
+= bytes_read
;
890 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
892 table
->files
= (struct fileinfo
*)
893 bfd_realloc (table
->files
,
894 (table
->num_files
+ FILE_ALLOC_CHUNK
)
895 * sizeof (struct fileinfo
));
900 table
->files
[table
->num_files
].name
= cur_file
;
901 table
->files
[table
->num_files
].dir
=
902 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
903 line_ptr
+= bytes_read
;
904 table
->files
[table
->num_files
].time
=
905 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
906 line_ptr
+= bytes_read
;
907 table
->files
[table
->num_files
].size
=
908 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
909 line_ptr
+= bytes_read
;
913 line_ptr
+= bytes_read
;
915 /* Read the statement sequences until there's nothing left. */
916 while (line_ptr
< line_end
)
918 /* State machine registers. */
920 char* filename
= concat_filename (table
, 1);
921 unsigned int line
= 1;
922 unsigned int column
= 0;
923 int is_stmt
= lh
.default_is_stmt
;
925 int end_sequence
= 0, need_low_pc
= 1;
928 /* Decode the table. */
929 while (! end_sequence
)
931 op_code
= read_1_byte (abfd
, line_ptr
);
936 case DW_LNS_extended_op
:
937 line_ptr
+= 1; /* Ignore length. */
938 extended_op
= read_1_byte (abfd
, line_ptr
);
942 case DW_LNE_end_sequence
:
944 add_line_info (table
, address
, filename
, line
, column
,
951 arange_add (unit
, low_pc
, address
);
953 case DW_LNE_set_address
:
954 address
= read_address (unit
, line_ptr
);
955 line_ptr
+= unit
->addr_size
;
957 case DW_LNE_define_file
:
958 cur_file
= read_string (abfd
, line_ptr
, &bytes_read
);
959 line_ptr
+= bytes_read
;
960 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
962 table
->files
= (struct fileinfo
*)
963 bfd_realloc (table
->files
,
964 (table
->num_files
+ FILE_ALLOC_CHUNK
)
965 * sizeof (struct fileinfo
));
969 table
->files
[table
->num_files
].name
= cur_file
;
970 table
->files
[table
->num_files
].dir
=
971 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
972 line_ptr
+= bytes_read
;
973 table
->files
[table
->num_files
].time
=
974 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
975 line_ptr
+= bytes_read
;
976 table
->files
[table
->num_files
].size
=
977 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
978 line_ptr
+= bytes_read
;
982 (*_bfd_error_handler
) (_("Dwarf Error: mangled line number section."));
983 bfd_set_error (bfd_error_bad_value
);
988 add_line_info (table
, address
, filename
, line
, column
, 0);
996 case DW_LNS_advance_pc
:
997 address
+= lh
.minimum_instruction_length
998 * read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
999 line_ptr
+= bytes_read
;
1001 case DW_LNS_advance_line
:
1002 line
+= read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
1003 line_ptr
+= bytes_read
;
1005 case DW_LNS_set_file
:
1009 /* The file and directory tables are 0 based, the references
1011 file
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1012 line_ptr
+= bytes_read
;
1013 filename
= concat_filename (table
, file
);
1016 case DW_LNS_set_column
:
1017 column
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1018 line_ptr
+= bytes_read
;
1020 case DW_LNS_negate_stmt
:
1021 is_stmt
= (!is_stmt
);
1023 case DW_LNS_set_basic_block
:
1026 case DW_LNS_const_add_pc
:
1027 address
+= lh
.minimum_instruction_length
1028 * ((255 - lh
.opcode_base
) / lh
.line_range
);
1030 case DW_LNS_fixed_advance_pc
:
1031 address
+= read_2_bytes (abfd
, line_ptr
);
1034 default: /* Special operand. */
1035 adj_opcode
= op_code
- lh
.opcode_base
;
1036 address
+= (adj_opcode
/ lh
.line_range
)
1037 * lh
.minimum_instruction_length
;
1038 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
1039 /* Append row to matrix using current values. */
1040 add_line_info (table
, address
, filename
, line
, column
, 0);
1054 /* If ADDR is within TABLE set the output parameters and return true,
1055 otherwise return false. The output parameters, FILENAME_PTR and
1056 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1059 lookup_address_in_line_info_table (table
,
1063 struct line_info_table
* table
;
1065 const char **filename_ptr
;
1066 unsigned int *linenumber_ptr
;
1068 struct line_info
* next_line
= table
->last_line
;
1069 struct line_info
* each_line
;
1074 each_line
= next_line
->prev_line
;
1076 while (each_line
&& next_line
)
1078 if (!each_line
->end_sequence
1079 && addr
>= each_line
->address
&& addr
< next_line
->address
)
1081 *filename_ptr
= each_line
->filename
;
1082 *linenumber_ptr
= each_line
->line
;
1085 next_line
= each_line
;
1086 each_line
= each_line
->prev_line
;
1092 /* Function table functions. */
1096 struct funcinfo
*prev_func
;
1102 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
1105 lookup_address_in_function_table (table
,
1108 struct funcinfo
* table
;
1110 const char **functionname_ptr
;
1112 struct funcinfo
* each_func
;
1114 for (each_func
= table
;
1116 each_func
= each_func
->prev_func
)
1118 if (addr
>= each_func
->low
&& addr
< each_func
->high
)
1120 *functionname_ptr
= each_func
->name
;
1128 /* DWARF2 Compilation unit functions. */
1130 /* Scan over each die in a comp. unit looking for functions to add
1131 to the function table. */
1134 scan_unit_for_functions (unit
)
1135 struct comp_unit
*unit
;
1137 bfd
*abfd
= unit
->abfd
;
1138 char *info_ptr
= unit
->first_child_die_ptr
;
1139 int nesting_level
= 1;
1141 while (nesting_level
)
1143 unsigned int abbrev_number
, bytes_read
, i
;
1144 struct abbrev_info
*abbrev
;
1145 struct attribute attr
;
1146 struct funcinfo
*func
;
1149 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1150 info_ptr
+= bytes_read
;
1152 if (! abbrev_number
)
1158 abbrev
= lookup_abbrev (abbrev_number
,unit
->abbrevs
);
1161 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %d."),
1163 bfd_set_error (bfd_error_bad_value
);
1167 if (abbrev
->tag
== DW_TAG_subprogram
)
1169 func
= (struct funcinfo
*) bfd_zalloc (abfd
, sizeof (struct funcinfo
));
1170 func
->prev_func
= unit
->function_table
;
1171 unit
->function_table
= func
;
1176 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1178 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1186 name
= DW_STRING (&attr
);
1188 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1189 if (func
->name
== NULL
)
1190 func
->name
= DW_STRING (&attr
);
1193 case DW_AT_MIPS_linkage_name
:
1194 func
->name
= DW_STRING (&attr
);
1198 func
->low
= DW_ADDR (&attr
);
1202 func
->high
= DW_ADDR (&attr
);
1214 name
= DW_STRING (&attr
);
1223 if (abbrev
->has_children
)
1230 /* Look for a RELA relocation to be applied on OFFSET of section SEC,
1231 and return the addend if such a relocation is found. Since this is
1232 only used to find relocations referring to the .debug_abbrev
1233 section, we make sure the relocation refers to this section, but
1234 this is not strictly necessary, and it can probably be safely
1235 removed if needed. However, it is important to note that this
1236 function only returns the addend, it doesn't serve the purpose of
1237 applying a generic relocation.
1239 If no suitable relocation is found, or if it is not a real RELA
1240 relocation, this function returns 0. */
1243 find_rela_addend (abfd
, sec
, offset
, syms
)
1246 bfd_size_type offset
;
1249 long reloc_size
= bfd_get_reloc_upper_bound (abfd
, sec
);
1250 arelent
**relocs
= NULL
;
1251 long reloc_count
, relc
;
1253 if (reloc_size
<= 0)
1256 relocs
= (arelent
**) bfd_malloc ((size_t) reloc_size
);
1260 reloc_count
= bfd_canonicalize_reloc (abfd
, sec
, relocs
, syms
);
1262 if (reloc_count
<= 0)
1268 for (relc
= 0; relc
< reloc_count
; relc
++)
1269 if (relocs
[relc
]->address
== offset
1270 && (*relocs
[relc
]->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
1271 && strcmp ((*relocs
[relc
]->sym_ptr_ptr
)->name
,
1272 ".debug_abbrev") == 0)
1274 bfd_vma addend
= (relocs
[relc
]->howto
->partial_inplace
1275 ? 0 : relocs
[relc
]->addend
);
1284 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1285 includes the compilation unit header that proceeds the DIE's, but
1286 does not include the length field that preceeds each compilation
1287 unit header. END_PTR points one past the end of this comp unit.
1288 If ABBREV_LENGTH is 0, then the length of the abbreviation offset
1289 is assumed to be four bytes. Otherwise, it it is the size given.
1291 This routine does not read the whole compilation unit; only enough
1292 to get to the line number information for the compilation unit. */
1294 static struct comp_unit
*
1295 parse_comp_unit (abfd
, stash
, unit_length
, abbrev_length
)
1297 struct dwarf2_debug
*stash
;
1298 bfd_vma unit_length
;
1299 unsigned int abbrev_length
;
1301 struct comp_unit
* unit
;
1303 unsigned short version
;
1304 unsigned int abbrev_offset
= 0;
1305 unsigned char addr_size
;
1306 struct abbrev_info
** abbrevs
;
1308 unsigned int abbrev_number
, bytes_read
, i
;
1309 struct abbrev_info
*abbrev
;
1310 struct attribute attr
;
1312 char *info_ptr
= stash
->info_ptr
;
1313 char *end_ptr
= info_ptr
+ unit_length
;
1315 version
= read_2_bytes (abfd
, info_ptr
);
1317 BFD_ASSERT (abbrev_length
== 0
1318 || abbrev_length
== 4
1319 || abbrev_length
== 8);
1320 if (abbrev_length
== 0 || abbrev_length
== 4)
1321 abbrev_offset
= read_4_bytes (abfd
, info_ptr
);
1322 else if (abbrev_length
== 8)
1323 abbrev_offset
= read_8_bytes (abfd
, info_ptr
);
1324 /* The abbrev offset is generally a relocation pointing to
1325 .debug_abbrev+offset. On RELA targets, we have to find the
1326 relocation and extract the addend to obtain the actual
1327 abbrev_offset, so do it here. */
1328 abbrev_offset
+= find_rela_addend (abfd
, stash
->sec
,
1329 info_ptr
- stash
->sec_info_ptr
,
1331 info_ptr
+= abbrev_length
;
1332 addr_size
= read_1_byte (abfd
, info_ptr
);
1337 (*_bfd_error_handler
) (_("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information."), version
);
1338 bfd_set_error (bfd_error_bad_value
);
1342 if (addr_size
> sizeof (bfd_vma
))
1344 (*_bfd_error_handler
) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1347 bfd_set_error (bfd_error_bad_value
);
1351 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
1353 (*_bfd_error_handler
) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size
);
1354 bfd_set_error (bfd_error_bad_value
);
1358 /* Read the abbrevs for this compilation unit into a table. */
1359 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
);
1363 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1364 info_ptr
+= bytes_read
;
1365 if (! abbrev_number
)
1367 (*_bfd_error_handler
) (_("Dwarf Error: Bad abbrev number: %d."),
1369 bfd_set_error (bfd_error_bad_value
);
1373 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
1376 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %d."),
1378 bfd_set_error (bfd_error_bad_value
);
1382 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, sizeof (struct comp_unit
));
1384 unit
->addr_size
= addr_size
;
1385 unit
->abbrevs
= abbrevs
;
1386 unit
->end_ptr
= end_ptr
;
1388 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1390 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1392 /* Store the data if it is of an attribute we want to keep in a
1393 partial symbol table. */
1396 case DW_AT_stmt_list
:
1398 unit
->line_offset
= DW_UNSND (&attr
);
1402 unit
->name
= DW_STRING (&attr
);
1406 unit
->arange
.low
= DW_ADDR (&attr
);
1410 unit
->arange
.high
= DW_ADDR (&attr
);
1413 case DW_AT_comp_dir
:
1415 char* comp_dir
= DW_STRING (&attr
);
1418 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1419 directory, get rid of it. */
1420 char *cp
= (char*) strchr (comp_dir
, ':');
1422 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
1425 unit
->comp_dir
= comp_dir
;
1434 unit
->first_child_die_ptr
= info_ptr
;
1438 /* Return true if UNIT contains the address given by ADDR. */
1441 comp_unit_contains_address (unit
, addr
)
1442 struct comp_unit
* unit
;
1445 struct arange
*arange
;
1450 arange
= &unit
->arange
;
1453 if (addr
>= arange
->low
&& addr
< arange
->high
)
1455 arange
= arange
->next
;
1462 /* If UNIT contains ADDR, set the output parameters to the values for
1463 the line containing ADDR. The output parameters, FILENAME_PTR,
1464 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1467 Return true of UNIT contains ADDR, and no errors were encountered;
1471 comp_unit_find_nearest_line (unit
, addr
,
1472 filename_ptr
, functionname_ptr
, linenumber_ptr
,
1474 struct comp_unit
* unit
;
1476 const char **filename_ptr
;
1477 const char **functionname_ptr
;
1478 unsigned int *linenumber_ptr
;
1479 struct dwarf2_debug
*stash
;
1487 if (! unit
->line_table
)
1489 if (! unit
->stmtlist
)
1495 unit
->line_table
= decode_line_info (unit
, stash
);
1497 if (! unit
->line_table
)
1503 if (! scan_unit_for_functions (unit
))
1510 line_p
= lookup_address_in_line_info_table (unit
->line_table
,
1514 func_p
= lookup_address_in_function_table (unit
->function_table
,
1517 return line_p
|| func_p
;
1520 /* Locate a section in a BFD containing debugging info. The search starts from the
1521 section after AFTER_SEC, or from the first section in the BFD if AFTER_SEC is
1522 NULL. The search works by examining the names of the sections. There are two
1523 permissiable names. The first is .debug_info. This is the standard DWARF2 name.
1524 The second is a prefix .gnu.linkonce.wi. This is a variation on the .debug_info
1525 section which has a checksum describing the contents appended onto the name. This
1526 allows the linker to identify and discard duplicate debugging sections for
1527 different compilation units. */
1528 #define DWARF2_DEBUG_INFO ".debug_info"
1529 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1532 find_debug_info (abfd
, after_sec
)
1534 asection
* after_sec
;
1539 msec
= after_sec
->next
;
1541 msec
= abfd
->sections
;
1545 if (strcmp (msec
->name
, DWARF2_DEBUG_INFO
) == 0)
1548 if (strncmp (msec
->name
, GNU_LINKONCE_INFO
, strlen (GNU_LINKONCE_INFO
)) == 0)
1557 /* The DWARF2 version of find_nearest line. Return true if the line
1558 is found without error. ADDR_SIZE is the number of bytes in the
1559 initial .debug_info length field and in the abbreviation offset.
1560 You may use zero to indicate that the default value should be
1564 _bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
1565 filename_ptr
, functionname_ptr
,
1572 const char **filename_ptr
;
1573 const char **functionname_ptr
;
1574 unsigned int *linenumber_ptr
;
1575 unsigned int addr_size
;
1578 /* Read each compilation unit from the section .debug_info, and check
1579 to see if it contains the address we are searching for. If yes,
1580 lookup the address, and return the line number info. If no, go
1581 on to the next compilation unit.
1583 We keep a list of all the previously read compilation units, and
1584 a pointer to the next un-read compilation unit. Check the
1585 previously read units before reading more. */
1586 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
1588 /* What address are we looking for? */
1589 bfd_vma addr
= offset
+ section
->vma
;
1591 struct comp_unit
* each
;
1593 *filename_ptr
= NULL
;
1594 *functionname_ptr
= NULL
;
1595 *linenumber_ptr
= 0;
1597 /* The DWARF2 spec says that the initial length field, and the
1598 offset of the abbreviation table, should both be 4-byte values.
1599 However, some compilers do things differently. */
1602 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
1606 unsigned long total_size
;
1610 (struct dwarf2_debug
*) bfd_zalloc (abfd
, sizeof (struct dwarf2_debug
));
1614 *pinfo
= (PTR
) stash
;
1616 msec
= find_debug_info (abfd
, NULL
);
1618 /* No dwarf2 info. Note that at this point the stash
1619 has been allocated, but contains zeros, this lets
1620 future calls to this function fail quicker. */
1623 /* There can be more than one DWARF2 info section in a BFD these days.
1624 Read them all in and produce one large stash. We do this in two
1625 passes - in the first pass we just accumulate the section sizes.
1626 In the second pass we read in the section's contents. The allows
1627 us to avoid reallocing the data as we add sections to the stash. */
1628 for (total_size
= 0; msec
; msec
= find_debug_info (abfd
, msec
))
1629 total_size
+= msec
->_raw_size
;
1631 stash
->info_ptr
= (char *) bfd_alloc (abfd
, total_size
);
1632 if (stash
->info_ptr
== NULL
)
1635 stash
->info_ptr_end
= stash
->info_ptr
;
1637 for (msec
= find_debug_info (abfd
, NULL
);
1639 msec
= find_debug_info (abfd
, msec
))
1642 unsigned long start
;
1644 size
= msec
->_raw_size
;
1648 start
= stash
->info_ptr_end
- stash
->info_ptr
;
1650 if (! bfd_get_section_contents (abfd
, msec
, stash
->info_ptr
+ start
, 0, size
))
1653 stash
->info_ptr_end
= stash
->info_ptr
+ start
+ size
;
1656 BFD_ASSERT (stash
->info_ptr_end
== stash
->info_ptr
+ total_size
);
1658 stash
->sec
= find_debug_info (abfd
, NULL
);
1659 stash
->sec_info_ptr
= stash
->info_ptr
;
1660 stash
->syms
= symbols
;
1663 /* FIXME: There is a problem with the contents of the
1664 .debug_info section. The 'low' and 'high' addresses of the
1665 comp_units are computed by relocs against symbols in the
1666 .text segment. We need these addresses in order to determine
1667 the nearest line number, and so we have to resolve the
1668 relocs. There is a similar problem when the .debug_line
1669 section is processed as well (e.g., there may be relocs
1670 against the operand of the DW_LNE_set_address operator).
1672 Unfortunately getting hold of the reloc information is hard...
1674 For now, this means that disassembling object files (as
1675 opposed to fully executables) does not always work as well as
1678 /* A null info_ptr indicates that there is no dwarf2 info
1679 (or that an error occured while setting up the stash). */
1680 if (! stash
->info_ptr
)
1683 /* Check the previously read comp. units first. */
1684 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
1685 if (comp_unit_contains_address (each
, addr
))
1686 return comp_unit_find_nearest_line (each
, addr
, filename_ptr
,
1687 functionname_ptr
, linenumber_ptr
,
1690 /* Read each remaining comp. units checking each as they are read. */
1691 while (stash
->info_ptr
< stash
->info_ptr_end
)
1693 struct comp_unit
* each
;
1698 length
= read_4_bytes (abfd
, stash
->info_ptr
);
1700 length
= read_8_bytes (abfd
, stash
->info_ptr
);
1701 stash
->info_ptr
+= addr_size
;
1705 each
= parse_comp_unit (abfd
, stash
, length
, addr_size
);
1706 stash
->info_ptr
+= length
;
1708 if ((bfd_vma
) (stash
->info_ptr
- stash
->sec_info_ptr
)
1709 == stash
->sec
->_raw_size
)
1711 stash
->sec
= find_debug_info (abfd
, stash
->sec
);
1712 stash
->sec_info_ptr
= stash
->info_ptr
;
1717 each
->next_unit
= stash
->all_comp_units
;
1718 stash
->all_comp_units
= each
;
1720 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1721 compilation units. If we don't have them (i.e.,
1722 unit->high == 0), we need to consult the line info
1723 table to see if a compilation unit contains the given
1725 if (each
->arange
.high
> 0)
1727 if (comp_unit_contains_address (each
, addr
))
1728 return comp_unit_find_nearest_line (each
, addr
,
1736 found
= comp_unit_find_nearest_line (each
, addr
,