2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
34 #include "libiberty.h"
37 #include "elf/dwarf2.h"
39 /* The data in the .debug_line statement prologue looks like this. */
44 unsigned short version
;
45 bfd_vma 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
;
70 /* Blocks are a bunch of untyped bytes. */
79 /* A list of all previously read comp_units. */
80 struct comp_unit
*all_comp_units
;
82 /* The next unread compilation unit within the .debug_info section.
83 Zero indicates that the .debug_info section has not been loaded
87 /* Pointer to the end of the .debug_info section memory buffer. */
88 bfd_byte
*info_ptr_end
;
90 /* Pointer to the section and address of the beginning of the
93 bfd_byte
*sec_info_ptr
;
95 /* Pointer to the symbol table. */
98 /* Pointer to the .debug_abbrev section loaded into memory. */
99 bfd_byte
*dwarf_abbrev_buffer
;
101 /* Length of the loaded .debug_abbrev section. */
102 unsigned long dwarf_abbrev_size
;
104 /* Buffer for decode_line_info. */
105 bfd_byte
*dwarf_line_buffer
;
107 /* Length of the loaded .debug_line section. */
108 unsigned long dwarf_line_size
;
110 /* Pointer to the .debug_str section loaded into memory. */
111 bfd_byte
*dwarf_str_buffer
;
113 /* Length of the loaded .debug_str section. */
114 unsigned long dwarf_str_size
;
116 /* Pointer to the .debug_ranges section loaded into memory. */
117 bfd_byte
*dwarf_ranges_buffer
;
119 /* Length of the loaded .debug_ranges section. */
120 unsigned long dwarf_ranges_size
;
122 /* If the most recent call to bfd_find_nearest_line was given an
123 address in an inlined function, preserve a pointer into the
124 calling chain for subsequent calls to bfd_find_inliner_info to
126 struct funcinfo
*inliner_chain
;
136 /* A minimal decoding of DWARF2 compilation units. We only decode
137 what's needed to get to the line number information. */
141 /* Chain the previously read compilation units. */
142 struct comp_unit
*next_unit
;
144 /* Keep the bfd convenient (for memory allocation). */
147 /* The lowest and highest addresses contained in this compilation
148 unit as specified in the compilation unit header. */
149 struct arange arange
;
151 /* The DW_AT_name attribute (for error messages). */
154 /* The abbrev hash table. */
155 struct abbrev_info
**abbrevs
;
157 /* Note that an error was found by comp_unit_find_nearest_line. */
160 /* The DW_AT_comp_dir attribute. */
163 /* TRUE if there is a line number table associated with this comp. unit. */
166 /* Pointer to the current comp_unit so that we can find a given entry
168 bfd_byte
*info_ptr_unit
;
170 /* The offset into .debug_line of the line number table. */
171 unsigned long line_offset
;
173 /* Pointer to the first child die for the comp unit. */
174 bfd_byte
*first_child_die_ptr
;
176 /* The end of the comp unit. */
179 /* The decoded line number, NULL if not yet decoded. */
180 struct line_info_table
*line_table
;
182 /* A list of the functions found in this comp. unit. */
183 struct funcinfo
*function_table
;
185 /* A list of the variables found in this comp. unit. */
186 struct varinfo
*variable_table
;
188 /* Pointer to dwarf2_debug structure. */
189 struct dwarf2_debug
*stash
;
191 /* Address size for this unit - from unit header. */
192 unsigned char addr_size
;
194 /* Offset size for this unit - from unit header. */
195 unsigned char offset_size
;
197 /* Base address for this unit - from DW_AT_low_pc attribute of
198 DW_TAG_compile_unit DIE */
199 bfd_vma base_address
;
202 /* This data structure holds the information of an abbrev. */
205 unsigned int number
; /* Number identifying abbrev. */
206 enum dwarf_tag tag
; /* DWARF tag. */
207 int has_children
; /* Boolean. */
208 unsigned int num_attrs
; /* Number of attributes. */
209 struct attr_abbrev
*attrs
; /* An array of attribute descriptions. */
210 struct abbrev_info
*next
; /* Next in chain. */
215 enum dwarf_attribute name
;
216 enum dwarf_form form
;
219 #ifndef ABBREV_HASH_SIZE
220 #define ABBREV_HASH_SIZE 121
222 #ifndef ATTR_ALLOC_CHUNK
223 #define ATTR_ALLOC_CHUNK 4
227 The following function up to the END VERBATIM mark are
228 copied directly from dwarf2read.c. */
230 /* Read dwarf information from a buffer. */
233 read_1_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
)
235 return bfd_get_8 (abfd
, buf
);
239 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
)
241 return bfd_get_signed_8 (abfd
, buf
);
245 read_2_bytes (bfd
*abfd
, bfd_byte
*buf
)
247 return bfd_get_16 (abfd
, buf
);
251 read_4_bytes (bfd
*abfd
, bfd_byte
*buf
)
253 return bfd_get_32 (abfd
, buf
);
257 read_8_bytes (bfd
*abfd
, bfd_byte
*buf
)
259 return bfd_get_64 (abfd
, buf
);
263 read_n_bytes (bfd
*abfd ATTRIBUTE_UNUSED
,
265 unsigned int size ATTRIBUTE_UNUSED
)
267 /* If the size of a host char is 8 bits, we can return a pointer
268 to the buffer, otherwise we have to copy the data to a buffer
269 allocated on the temporary obstack. */
274 read_string (bfd
*abfd ATTRIBUTE_UNUSED
,
276 unsigned int *bytes_read_ptr
)
278 /* Return a pointer to the embedded string. */
279 char *str
= (char *) buf
;
286 *bytes_read_ptr
= strlen (str
) + 1;
291 read_indirect_string (struct comp_unit
* unit
,
293 unsigned int *bytes_read_ptr
)
296 struct dwarf2_debug
*stash
= unit
->stash
;
299 if (unit
->offset_size
== 4)
300 offset
= read_4_bytes (unit
->abfd
, buf
);
302 offset
= read_8_bytes (unit
->abfd
, buf
);
303 *bytes_read_ptr
= unit
->offset_size
;
305 if (! stash
->dwarf_str_buffer
)
308 bfd
*abfd
= unit
->abfd
;
311 msec
= bfd_get_section_by_name (abfd
, ".debug_str");
314 (*_bfd_error_handler
)
315 (_("Dwarf Error: Can't find .debug_str section."));
316 bfd_set_error (bfd_error_bad_value
);
320 sz
= msec
->rawsize
? msec
->rawsize
: msec
->size
;
321 stash
->dwarf_str_size
= sz
;
322 stash
->dwarf_str_buffer
= bfd_alloc (abfd
, sz
);
323 if (! stash
->dwarf_str_buffer
)
326 if (! bfd_get_section_contents (abfd
, msec
, stash
->dwarf_str_buffer
,
331 if (offset
>= stash
->dwarf_str_size
)
333 (*_bfd_error_handler
) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
334 (unsigned long) offset
, stash
->dwarf_str_size
);
335 bfd_set_error (bfd_error_bad_value
);
339 str
= (char *) stash
->dwarf_str_buffer
+ offset
;
348 read_address (struct comp_unit
*unit
, bfd_byte
*buf
)
350 switch (unit
->addr_size
)
353 return bfd_get_64 (unit
->abfd
, buf
);
355 return bfd_get_32 (unit
->abfd
, buf
);
357 return bfd_get_16 (unit
->abfd
, buf
);
363 /* Lookup an abbrev_info structure in the abbrev hash table. */
365 static struct abbrev_info
*
366 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
368 unsigned int hash_number
;
369 struct abbrev_info
*abbrev
;
371 hash_number
= number
% ABBREV_HASH_SIZE
;
372 abbrev
= abbrevs
[hash_number
];
376 if (abbrev
->number
== number
)
379 abbrev
= abbrev
->next
;
385 /* In DWARF version 2, the description of the debugging information is
386 stored in a separate .debug_abbrev section. Before we read any
387 dies from a section we read in all abbreviations and install them
390 static struct abbrev_info
**
391 read_abbrevs (bfd
*abfd
, bfd_uint64_t offset
, struct dwarf2_debug
*stash
)
393 struct abbrev_info
**abbrevs
;
394 bfd_byte
*abbrev_ptr
;
395 struct abbrev_info
*cur_abbrev
;
396 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
397 unsigned int abbrev_form
, hash_number
;
400 if (! stash
->dwarf_abbrev_buffer
)
404 msec
= bfd_get_section_by_name (abfd
, ".debug_abbrev");
407 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_abbrev section."));
408 bfd_set_error (bfd_error_bad_value
);
412 stash
->dwarf_abbrev_size
= msec
->size
;
413 stash
->dwarf_abbrev_buffer
414 = bfd_simple_get_relocated_section_contents (abfd
, msec
, NULL
,
416 if (! stash
->dwarf_abbrev_buffer
)
420 if (offset
>= stash
->dwarf_abbrev_size
)
422 (*_bfd_error_handler
) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
423 (unsigned long) offset
, stash
->dwarf_abbrev_size
);
424 bfd_set_error (bfd_error_bad_value
);
428 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
429 abbrevs
= bfd_zalloc (abfd
, amt
);
431 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
432 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
433 abbrev_ptr
+= bytes_read
;
435 /* Loop until we reach an abbrev number of 0. */
436 while (abbrev_number
)
438 amt
= sizeof (struct abbrev_info
);
439 cur_abbrev
= bfd_zalloc (abfd
, amt
);
441 /* Read in abbrev header. */
442 cur_abbrev
->number
= abbrev_number
;
443 cur_abbrev
->tag
= (enum dwarf_tag
)
444 read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
445 abbrev_ptr
+= bytes_read
;
446 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
);
449 /* Now read in declarations. */
450 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
451 abbrev_ptr
+= bytes_read
;
452 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
453 abbrev_ptr
+= bytes_read
;
457 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
459 struct attr_abbrev
*tmp
;
461 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
462 amt
*= sizeof (struct attr_abbrev
);
463 tmp
= bfd_realloc (cur_abbrev
->attrs
, amt
);
468 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
470 struct abbrev_info
*abbrev
= abbrevs
[i
];
474 free (abbrev
->attrs
);
475 abbrev
= abbrev
->next
;
480 cur_abbrev
->attrs
= tmp
;
483 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
484 = (enum dwarf_attribute
) abbrev_name
;
485 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
++].form
486 = (enum dwarf_form
) abbrev_form
;
487 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
488 abbrev_ptr
+= bytes_read
;
489 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
490 abbrev_ptr
+= bytes_read
;
493 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
494 cur_abbrev
->next
= abbrevs
[hash_number
];
495 abbrevs
[hash_number
] = cur_abbrev
;
497 /* Get next abbreviation.
498 Under Irix6 the abbreviations for a compilation unit are not
499 always properly terminated with an abbrev number of 0.
500 Exit loop if we encounter an abbreviation which we have
501 already read (which means we are about to read the abbreviations
502 for the next compile unit) or if the end of the abbreviation
504 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
505 >= stash
->dwarf_abbrev_size
)
507 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
508 abbrev_ptr
+= bytes_read
;
509 if (lookup_abbrev (abbrev_number
,abbrevs
) != NULL
)
516 /* Read an attribute value described by an attribute form. */
519 read_attribute_value (struct attribute
*attr
,
521 struct comp_unit
*unit
,
524 bfd
*abfd
= unit
->abfd
;
525 unsigned int bytes_read
;
526 struct dwarf_block
*blk
;
529 attr
->form
= (enum dwarf_form
) form
;
534 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
535 case DW_FORM_ref_addr
:
536 attr
->u
.val
= read_address (unit
, info_ptr
);
537 info_ptr
+= unit
->addr_size
;
540 amt
= sizeof (struct dwarf_block
);
541 blk
= bfd_alloc (abfd
, amt
);
542 blk
->size
= read_2_bytes (abfd
, info_ptr
);
544 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
545 info_ptr
+= blk
->size
;
549 amt
= sizeof (struct dwarf_block
);
550 blk
= bfd_alloc (abfd
, amt
);
551 blk
->size
= read_4_bytes (abfd
, info_ptr
);
553 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
554 info_ptr
+= blk
->size
;
558 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
);
562 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
);
566 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
570 attr
->u
.str
= read_string (abfd
, info_ptr
, &bytes_read
);
571 info_ptr
+= bytes_read
;
574 attr
->u
.str
= read_indirect_string (unit
, info_ptr
, &bytes_read
);
575 info_ptr
+= bytes_read
;
578 amt
= sizeof (struct dwarf_block
);
579 blk
= bfd_alloc (abfd
, amt
);
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
;
587 amt
= sizeof (struct dwarf_block
);
588 blk
= bfd_alloc (abfd
, amt
);
589 blk
->size
= read_1_byte (abfd
, info_ptr
);
591 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
592 info_ptr
+= blk
->size
;
596 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
600 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
604 attr
->u
.sval
= read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
605 info_ptr
+= bytes_read
;
608 attr
->u
.val
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
609 info_ptr
+= bytes_read
;
612 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
616 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
);
620 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
);
624 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
627 case DW_FORM_ref_udata
:
628 attr
->u
.val
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
629 info_ptr
+= bytes_read
;
631 case DW_FORM_indirect
:
632 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
633 info_ptr
+= bytes_read
;
634 info_ptr
= read_attribute_value (attr
, form
, unit
, info_ptr
);
637 (*_bfd_error_handler
) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
639 bfd_set_error (bfd_error_bad_value
);
644 /* Read an attribute described by an abbreviated attribute. */
647 read_attribute (struct attribute
*attr
,
648 struct attr_abbrev
*abbrev
,
649 struct comp_unit
*unit
,
652 attr
->name
= abbrev
->name
;
653 info_ptr
= read_attribute_value (attr
, abbrev
->form
, unit
, info_ptr
);
657 /* Source line information table routines. */
659 #define FILE_ALLOC_CHUNK 5
660 #define DIR_ALLOC_CHUNK 5
664 struct line_info
* prev_line
;
669 int end_sequence
; /* End of (sequential) code sequence. */
680 struct line_info_table
683 unsigned int num_files
;
684 unsigned int num_dirs
;
687 struct fileinfo
* files
;
688 struct line_info
* last_line
; /* largest VMA */
689 struct line_info
* lcl_head
; /* local head; used in 'add_line_info' */
692 /* Remember some information about each function. If the function is
693 inlined (DW_TAG_inlined_subroutine) it may have two additional
694 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
695 source code location where this function was inlined. */
699 struct funcinfo
*prev_func
; /* Pointer to previous function in list of all functions */
700 struct funcinfo
*caller_func
; /* Pointer to function one scope higher */
701 char *caller_file
; /* Source location file name where caller_func inlines this func */
702 int caller_line
; /* Source location line number where caller_func inlines this func */
703 char *file
; /* Source location file name */
704 int line
; /* Source location line number */
708 struct arange arange
;
709 asection
*sec
; /* Where the symbol is defined */
714 /* Pointer to previous variable in list of all variables */
715 struct varinfo
*prev_var
;
716 /* Source location file name */
718 /* Source location line number */
722 /* Where the symbol is defined */
724 /* Is this a stack variable? */
725 unsigned int stack
: 1;
728 /* Adds a new entry to the line_info list in the line_info_table, ensuring
729 that the list is sorted. Note that the line_info list is sorted from
730 highest to lowest VMA (with possible duplicates); that is,
731 line_info->prev_line always accesses an equal or smaller VMA. */
734 add_line_info (struct line_info_table
*table
,
741 bfd_size_type amt
= sizeof (struct line_info
);
742 struct line_info
* info
= bfd_alloc (table
->abfd
, amt
);
744 /* Find the correct location for 'info'. Normally we will receive
745 new line_info data 1) in order and 2) with increasing VMAs.
746 However some compilers break the rules (cf. decode_line_info) and
747 so we include some heuristics for quickly finding the correct
748 location for 'info'. In particular, these heuristics optimize for
749 the common case in which the VMA sequence that we receive is a
750 list of locally sorted VMAs such as
751 p...z a...j (where a < j < p < z)
753 Note: table->lcl_head is used to head an *actual* or *possible*
754 sequence within the list (such as a...j) that is not directly
755 headed by table->last_line
757 Note: we may receive duplicate entries from 'decode_line_info'. */
760 if (!table
->last_line
761 || address
>= table
->last_line
->address
)
763 /* Normal case: add 'info' to the beginning of the list */
764 info
->prev_line
= table
->last_line
;
765 table
->last_line
= info
;
767 /* lcl_head: initialize to head a *possible* sequence at the end. */
768 if (!table
->lcl_head
)
769 table
->lcl_head
= info
;
772 else if (!table
->lcl_head
->prev_line
773 && table
->lcl_head
->address
> address
)
775 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
776 list and 2) the head of 'info'. */
777 info
->prev_line
= NULL
;
778 table
->lcl_head
->prev_line
= info
;
781 else if (table
->lcl_head
->prev_line
782 && table
->lcl_head
->address
> address
783 && address
>= table
->lcl_head
->prev_line
->address
)
785 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
786 list and 2) the head of 'info'. */
787 info
->prev_line
= table
->lcl_head
->prev_line
;
788 table
->lcl_head
->prev_line
= info
;
793 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
794 heads for 'info'. Reset 'lcl_head' and repeat. */
795 struct line_info
* li2
= table
->last_line
; /* always non-NULL */
796 struct line_info
* li1
= li2
->prev_line
;
800 if (li2
->address
> address
&& address
>= li1
->address
)
803 li2
= li1
; /* always non-NULL */
804 li1
= li1
->prev_line
;
806 table
->lcl_head
= li2
;
809 /* Set member data of 'info'. */
810 info
->address
= address
;
812 info
->column
= column
;
813 info
->end_sequence
= end_sequence
;
815 if (filename
&& filename
[0])
817 info
->filename
= bfd_alloc (table
->abfd
, strlen (filename
) + 1);
819 strcpy (info
->filename
, filename
);
822 info
->filename
= NULL
;
825 /* Extract a fully qualified filename from a line info table.
826 The returned string has been malloc'ed and it is the caller's
827 responsibility to free it. */
830 concat_filename (struct line_info_table
*table
, unsigned int file
)
834 if (file
- 1 >= table
->num_files
)
836 (*_bfd_error_handler
)
837 (_("Dwarf Error: mangled line number section (bad file number)."));
838 return strdup ("<unknown>");
841 filename
= table
->files
[file
- 1].name
;
843 if (! IS_ABSOLUTE_PATH (filename
))
845 char *dirname
= (table
->files
[file
- 1].dir
846 ? table
->dirs
[table
->files
[file
- 1].dir
- 1]
849 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
850 The best we can do is return the filename part. */
853 unsigned int len
= strlen (dirname
) + strlen (filename
) + 2;
856 name
= bfd_malloc (len
);
858 sprintf (name
, "%s/%s", dirname
, filename
);
863 return strdup (filename
);
867 arange_add (bfd
*abfd
, struct arange
*first_arange
, bfd_vma low_pc
, bfd_vma high_pc
)
869 struct arange
*arange
;
871 /* If the first arange is empty, use it. */
872 if (first_arange
->high
== 0)
874 first_arange
->low
= low_pc
;
875 first_arange
->high
= high_pc
;
879 /* Next see if we can cheaply extend an existing range. */
880 arange
= first_arange
;
883 if (low_pc
== arange
->high
)
885 arange
->high
= high_pc
;
888 if (high_pc
== arange
->low
)
890 arange
->low
= low_pc
;
893 arange
= arange
->next
;
897 /* Need to allocate a new arange and insert it into the arange list.
898 Order isn't significant, so just insert after the first arange. */
899 arange
= bfd_zalloc (abfd
, sizeof (*arange
));
900 arange
->low
= low_pc
;
901 arange
->high
= high_pc
;
902 arange
->next
= first_arange
->next
;
903 first_arange
->next
= arange
;
906 /* Decode the line number information for UNIT. */
908 static struct line_info_table
*
909 decode_line_info (struct comp_unit
*unit
, struct dwarf2_debug
*stash
)
911 bfd
*abfd
= unit
->abfd
;
912 struct line_info_table
* table
;
916 unsigned int i
, bytes_read
, offset_size
;
917 char *cur_file
, *cur_dir
;
918 unsigned char op_code
, extended_op
, adj_opcode
;
921 if (! stash
->dwarf_line_buffer
)
925 msec
= bfd_get_section_by_name (abfd
, ".debug_line");
928 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_line section."));
929 bfd_set_error (bfd_error_bad_value
);
933 stash
->dwarf_line_size
= msec
->size
;
934 stash
->dwarf_line_buffer
935 = bfd_simple_get_relocated_section_contents (abfd
, msec
, NULL
,
937 if (! stash
->dwarf_line_buffer
)
941 /* It is possible to get a bad value for the line_offset. Validate
942 it here so that we won't get a segfault below. */
943 if (unit
->line_offset
>= stash
->dwarf_line_size
)
945 (*_bfd_error_handler
) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
946 unit
->line_offset
, stash
->dwarf_line_size
);
947 bfd_set_error (bfd_error_bad_value
);
951 amt
= sizeof (struct line_info_table
);
952 table
= bfd_alloc (abfd
, amt
);
954 table
->comp_dir
= unit
->comp_dir
;
956 table
->num_files
= 0;
963 table
->last_line
= NULL
;
964 table
->lcl_head
= NULL
;
966 line_ptr
= stash
->dwarf_line_buffer
+ unit
->line_offset
;
968 /* Read in the prologue. */
969 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
972 if (lh
.total_length
== 0xffffffff)
974 lh
.total_length
= read_8_bytes (abfd
, line_ptr
);
978 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
980 /* Handle (non-standard) 64-bit DWARF2 formats. */
981 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
985 line_end
= line_ptr
+ lh
.total_length
;
986 lh
.version
= read_2_bytes (abfd
, line_ptr
);
988 if (offset_size
== 4)
989 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
);
991 lh
.prologue_length
= read_8_bytes (abfd
, line_ptr
);
992 line_ptr
+= offset_size
;
993 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
);
995 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
);
997 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
);
999 lh
.line_range
= read_1_byte (abfd
, line_ptr
);
1001 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
);
1003 amt
= lh
.opcode_base
* sizeof (unsigned char);
1004 lh
.standard_opcode_lengths
= bfd_alloc (abfd
, amt
);
1006 lh
.standard_opcode_lengths
[0] = 1;
1008 for (i
= 1; i
< lh
.opcode_base
; ++i
)
1010 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
);
1014 /* Read directory table. */
1015 while ((cur_dir
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1017 line_ptr
+= bytes_read
;
1019 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1023 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1024 amt
*= sizeof (char *);
1026 tmp
= bfd_realloc (table
->dirs
, amt
);
1035 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1038 line_ptr
+= bytes_read
;
1040 /* Read file name table. */
1041 while ((cur_file
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1043 line_ptr
+= bytes_read
;
1045 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1047 struct fileinfo
*tmp
;
1049 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1050 amt
*= sizeof (struct fileinfo
);
1052 tmp
= bfd_realloc (table
->files
, amt
);
1055 free (table
->files
);
1062 table
->files
[table
->num_files
].name
= cur_file
;
1063 table
->files
[table
->num_files
].dir
=
1064 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1065 line_ptr
+= bytes_read
;
1066 table
->files
[table
->num_files
].time
=
1067 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1068 line_ptr
+= bytes_read
;
1069 table
->files
[table
->num_files
].size
=
1070 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1071 line_ptr
+= bytes_read
;
1075 line_ptr
+= bytes_read
;
1077 /* Read the statement sequences until there's nothing left. */
1078 while (line_ptr
< line_end
)
1080 /* State machine registers. */
1081 bfd_vma address
= 0;
1082 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
1083 unsigned int line
= 1;
1084 unsigned int column
= 0;
1085 int is_stmt
= lh
.default_is_stmt
;
1086 int basic_block
= 0;
1087 int end_sequence
= 0;
1088 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1089 compilers generate address sequences that are wildly out of
1090 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1091 for ia64-Linux). Thus, to determine the low and high
1092 address, we must compare on every DW_LNS_copy, etc. */
1094 bfd_vma high_pc
= 0;
1095 bfd_boolean low_pc_set
= FALSE
;
1097 /* Decode the table. */
1098 while (! end_sequence
)
1100 op_code
= read_1_byte (abfd
, line_ptr
);
1103 if (op_code
>= lh
.opcode_base
)
1105 /* Special operand. */
1106 adj_opcode
= op_code
- lh
.opcode_base
;
1107 address
+= (adj_opcode
/ lh
.line_range
)
1108 * lh
.minimum_instruction_length
;
1109 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
1110 /* Append row to matrix using current values. */
1111 add_line_info (table
, address
, filename
, line
, column
, 0);
1113 if (!low_pc_set
|| address
< low_pc
)
1118 if (address
> high_pc
)
1121 else switch (op_code
)
1123 case DW_LNS_extended_op
:
1124 /* Ignore length. */
1126 extended_op
= read_1_byte (abfd
, line_ptr
);
1129 switch (extended_op
)
1131 case DW_LNE_end_sequence
:
1133 add_line_info (table
, address
, filename
, line
, column
,
1135 if (!low_pc_set
|| address
< low_pc
)
1140 if (address
> high_pc
)
1142 arange_add (unit
->abfd
, &unit
->arange
, low_pc
, high_pc
);
1144 case DW_LNE_set_address
:
1145 address
= read_address (unit
, line_ptr
);
1146 line_ptr
+= unit
->addr_size
;
1148 case DW_LNE_define_file
:
1149 cur_file
= read_string (abfd
, line_ptr
, &bytes_read
);
1150 line_ptr
+= bytes_read
;
1151 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1153 struct fileinfo
*tmp
;
1155 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1156 amt
*= sizeof (struct fileinfo
);
1157 tmp
= bfd_realloc (table
->files
, amt
);
1160 free (table
->files
);
1167 table
->files
[table
->num_files
].name
= cur_file
;
1168 table
->files
[table
->num_files
].dir
=
1169 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1170 line_ptr
+= bytes_read
;
1171 table
->files
[table
->num_files
].time
=
1172 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1173 line_ptr
+= bytes_read
;
1174 table
->files
[table
->num_files
].size
=
1175 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1176 line_ptr
+= bytes_read
;
1180 (*_bfd_error_handler
) (_("Dwarf Error: mangled line number section."));
1181 bfd_set_error (bfd_error_bad_value
);
1183 free (table
->files
);
1189 add_line_info (table
, address
, filename
, line
, column
, 0);
1191 if (!low_pc_set
|| address
< low_pc
)
1196 if (address
> high_pc
)
1199 case DW_LNS_advance_pc
:
1200 address
+= lh
.minimum_instruction_length
1201 * read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1202 line_ptr
+= bytes_read
;
1204 case DW_LNS_advance_line
:
1205 line
+= read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
1206 line_ptr
+= bytes_read
;
1208 case DW_LNS_set_file
:
1212 /* The file and directory tables are 0
1213 based, the references are 1 based. */
1214 file
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1215 line_ptr
+= bytes_read
;
1218 filename
= concat_filename (table
, file
);
1221 case DW_LNS_set_column
:
1222 column
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1223 line_ptr
+= bytes_read
;
1225 case DW_LNS_negate_stmt
:
1226 is_stmt
= (!is_stmt
);
1228 case DW_LNS_set_basic_block
:
1231 case DW_LNS_const_add_pc
:
1232 address
+= lh
.minimum_instruction_length
1233 * ((255 - lh
.opcode_base
) / lh
.line_range
);
1235 case DW_LNS_fixed_advance_pc
:
1236 address
+= read_2_bytes (abfd
, line_ptr
);
1243 /* Unknown standard opcode, ignore it. */
1244 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
1246 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1247 line_ptr
+= bytes_read
;
1260 /* If ADDR is within TABLE set the output parameters and return TRUE,
1261 otherwise return FALSE. The output parameters, FILENAME_PTR and
1262 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1265 lookup_address_in_line_info_table (struct line_info_table
*table
,
1267 struct funcinfo
*function
,
1268 const char **filename_ptr
,
1269 unsigned int *linenumber_ptr
)
1271 /* Note: table->last_line should be a descendingly sorted list. */
1272 struct line_info
* next_line
= table
->last_line
;
1273 struct line_info
* each_line
= NULL
;
1274 *filename_ptr
= NULL
;
1279 each_line
= next_line
->prev_line
;
1281 /* Check for large addresses */
1282 if (addr
> next_line
->address
)
1283 each_line
= NULL
; /* ensure we skip over the normal case */
1285 /* Normal case: search the list; save */
1286 while (each_line
&& next_line
)
1288 /* If we have an address match, save this info. This allows us
1289 to return as good as results as possible for strange debugging
1291 bfd_boolean addr_match
= FALSE
;
1292 if (each_line
->address
<= addr
&& addr
< next_line
->address
)
1296 /* If this line appears to span functions, and addr is in the
1297 later function, return the first line of that function instead
1298 of the last line of the earlier one. This check is for GCC
1299 2.95, which emits the first line number for a function late. */
1301 if (function
!= NULL
)
1304 struct arange
*arange
;
1306 /* Find the lowest address in the function's range list */
1307 lowest_pc
= function
->arange
.low
;
1308 for (arange
= &function
->arange
;
1310 arange
= arange
->next
)
1312 if (function
->arange
.low
< lowest_pc
)
1313 lowest_pc
= function
->arange
.low
;
1315 /* Check for spanning function and set outgoing line info */
1316 if (addr
>= lowest_pc
1317 && each_line
->address
< lowest_pc
1318 && next_line
->address
> lowest_pc
)
1320 *filename_ptr
= next_line
->filename
;
1321 *linenumber_ptr
= next_line
->line
;
1325 *filename_ptr
= each_line
->filename
;
1326 *linenumber_ptr
= each_line
->line
;
1331 if (addr_match
&& !each_line
->end_sequence
)
1332 return TRUE
; /* we have definitely found what we want */
1334 next_line
= each_line
;
1335 each_line
= each_line
->prev_line
;
1338 /* At this point each_line is NULL but next_line is not. If we found
1339 a candidate end-of-sequence point in the loop above, we can return
1340 that (compatibility with a bug in the Intel compiler); otherwise,
1341 assuming that we found the containing function for this address in
1342 this compilation unit, return the first line we have a number for
1343 (compatibility with GCC 2.95). */
1344 if (*filename_ptr
== NULL
&& function
!= NULL
)
1346 *filename_ptr
= next_line
->filename
;
1347 *linenumber_ptr
= next_line
->line
;
1354 /* Read in the .debug_ranges section for future reference */
1357 read_debug_ranges (struct comp_unit
*unit
)
1359 struct dwarf2_debug
*stash
= unit
->stash
;
1360 if (! stash
->dwarf_ranges_buffer
)
1362 bfd
*abfd
= unit
->abfd
;
1365 msec
= bfd_get_section_by_name (abfd
, ".debug_ranges");
1368 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_ranges section."));
1369 bfd_set_error (bfd_error_bad_value
);
1373 stash
->dwarf_ranges_size
= msec
->size
;
1374 stash
->dwarf_ranges_buffer
1375 = bfd_simple_get_relocated_section_contents (abfd
, msec
, NULL
,
1377 if (! stash
->dwarf_ranges_buffer
)
1383 /* Function table functions. */
1385 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1386 Note that we need to find the function that has the smallest
1387 range that contains ADDR, to handle inlined functions without
1388 depending upon them being ordered in TABLE by increasing range. */
1391 lookup_address_in_function_table (struct comp_unit
*unit
,
1393 struct funcinfo
**function_ptr
,
1394 const char **functionname_ptr
)
1396 struct funcinfo
* each_func
;
1397 struct funcinfo
* best_fit
= NULL
;
1398 struct arange
*arange
;
1400 for (each_func
= unit
->function_table
;
1402 each_func
= each_func
->prev_func
)
1404 for (arange
= &each_func
->arange
;
1406 arange
= arange
->next
)
1408 if (addr
>= arange
->low
&& addr
< arange
->high
)
1411 ((arange
->high
- arange
->low
) < (best_fit
->arange
.high
- best_fit
->arange
.low
)))
1412 best_fit
= each_func
;
1419 struct funcinfo
* curr_func
= best_fit
;
1421 *functionname_ptr
= best_fit
->name
;
1422 *function_ptr
= best_fit
;
1424 /* If we found a match and it is a function that was inlined,
1425 traverse the function list looking for the function at the
1426 next higher scope and save a pointer to it for future use.
1427 Note that because of the way the DWARF info is generated, and
1428 the way we build the function list, the first function at the
1429 next higher level is the one we want. */
1431 for (each_func
= best_fit
-> prev_func
;
1432 each_func
&& (curr_func
->tag
== DW_TAG_inlined_subroutine
);
1433 each_func
= each_func
->prev_func
)
1435 if (each_func
->nesting_level
< curr_func
->nesting_level
)
1437 curr_func
->caller_func
= each_func
;
1438 curr_func
= each_func
;
1449 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1450 and LINENUMBER_PTR, and return TRUE. */
1453 lookup_symbol_in_function_table (struct comp_unit
*unit
,
1456 const char **filename_ptr
,
1457 unsigned int *linenumber_ptr
)
1459 struct funcinfo
* each_func
;
1460 struct funcinfo
* best_fit
= NULL
;
1461 struct arange
*arange
;
1462 const char *name
= bfd_asymbol_name (sym
);
1463 asection
*sec
= bfd_get_section (sym
);
1465 for (each_func
= unit
->function_table
;
1467 each_func
= each_func
->prev_func
)
1469 for (arange
= &each_func
->arange
;
1471 arange
= arange
->next
)
1473 if ((!each_func
->sec
|| each_func
->sec
== sec
)
1474 && addr
>= arange
->low
1475 && addr
< arange
->high
1476 && strcmp (name
, each_func
->name
) == 0
1478 || ((arange
->high
- arange
->low
)
1479 < (best_fit
->arange
.high
- best_fit
->arange
.low
))))
1480 best_fit
= each_func
;
1486 best_fit
->sec
= sec
;
1487 *filename_ptr
= best_fit
->file
;
1488 *linenumber_ptr
= best_fit
->line
;
1495 /* Variable table functions. */
1497 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1498 LINENUMBER_PTR, and return TRUE. */
1501 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
1503 const char **filename_ptr
,
1504 unsigned int *linenumber_ptr
)
1506 const char *name
= bfd_asymbol_name (sym
);
1507 asection
*sec
= bfd_get_section (sym
);
1508 struct varinfo
* each
;
1510 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
1511 if (each
->stack
== 0
1512 && (!each
->sec
|| each
->sec
== sec
)
1513 && strcmp (name
, each
->name
) == 0)
1519 *filename_ptr
= each
->file
;
1520 *linenumber_ptr
= each
->line
;
1528 find_abstract_instance_name (struct comp_unit
*unit
, bfd_uint64_t die_ref
)
1530 bfd
*abfd
= unit
->abfd
;
1532 unsigned int abbrev_number
, bytes_read
, i
;
1533 struct abbrev_info
*abbrev
;
1534 struct attribute attr
;
1537 info_ptr
= unit
->info_ptr_unit
+ die_ref
;
1538 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1539 info_ptr
+= bytes_read
;
1543 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
1546 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
1548 bfd_set_error (bfd_error_bad_value
);
1552 for (i
= 0; i
< abbrev
->num_attrs
&& !name
; ++i
)
1554 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1560 case DW_AT_specification
:
1561 name
= find_abstract_instance_name (unit
, attr
.u
.val
);
1573 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
, bfd_uint64_t offset
)
1575 bfd_byte
*ranges_ptr
;
1576 bfd_vma base_address
= unit
->base_address
;
1578 if (! unit
->stash
->dwarf_ranges_buffer
)
1580 if (! read_debug_ranges (unit
))
1583 ranges_ptr
= unit
->stash
->dwarf_ranges_buffer
+ offset
;
1590 if (unit
->offset_size
== 4)
1592 low_pc
= read_4_bytes (unit
->abfd
, ranges_ptr
);
1594 high_pc
= read_4_bytes (unit
->abfd
, ranges_ptr
);
1599 low_pc
= read_8_bytes (unit
->abfd
, ranges_ptr
);
1601 high_pc
= read_8_bytes (unit
->abfd
, ranges_ptr
);
1604 if (low_pc
== 0 && high_pc
== 0)
1606 if (low_pc
== -1UL && high_pc
!= -1UL)
1607 base_address
= high_pc
;
1609 arange_add (unit
->abfd
, arange
, base_address
+ low_pc
, base_address
+ high_pc
);
1613 /* DWARF2 Compilation unit functions. */
1615 /* Scan over each die in a comp. unit looking for functions to add
1616 to the function table and variables to the variable table. */
1619 scan_unit_for_symbols (struct comp_unit
*unit
)
1621 bfd
*abfd
= unit
->abfd
;
1622 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
1623 int nesting_level
= 1;
1625 while (nesting_level
)
1627 unsigned int abbrev_number
, bytes_read
, i
;
1628 struct abbrev_info
*abbrev
;
1629 struct attribute attr
;
1630 struct funcinfo
*func
;
1631 struct varinfo
*var
;
1633 bfd_vma high_pc
= 0;
1635 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1636 info_ptr
+= bytes_read
;
1638 if (! abbrev_number
)
1644 abbrev
= lookup_abbrev (abbrev_number
,unit
->abbrevs
);
1647 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
1649 bfd_set_error (bfd_error_bad_value
);
1654 if (abbrev
->tag
== DW_TAG_subprogram
1655 || abbrev
->tag
== DW_TAG_entry_point
1656 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
1658 bfd_size_type amt
= sizeof (struct funcinfo
);
1659 func
= bfd_zalloc (abfd
, amt
);
1660 func
->tag
= abbrev
->tag
;
1661 func
->nesting_level
= nesting_level
;
1662 func
->prev_func
= unit
->function_table
;
1663 unit
->function_table
= func
;
1668 if (abbrev
->tag
== DW_TAG_variable
)
1670 bfd_size_type amt
= sizeof (struct varinfo
);
1671 var
= bfd_zalloc (abfd
, amt
);
1672 var
->tag
= abbrev
->tag
;
1674 var
->prev_var
= unit
->variable_table
;
1675 unit
->variable_table
= var
;
1679 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1681 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1687 case DW_AT_call_file
:
1688 func
->caller_file
= concat_filename (unit
->line_table
, attr
.u
.val
);
1691 case DW_AT_call_line
:
1692 func
->caller_line
= attr
.u
.val
;
1695 case DW_AT_abstract_origin
:
1696 func
->name
= find_abstract_instance_name (unit
, attr
.u
.val
);
1700 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1701 if (func
->name
== NULL
)
1702 func
->name
= attr
.u
.str
;
1705 case DW_AT_MIPS_linkage_name
:
1706 func
->name
= attr
.u
.str
;
1710 low_pc
= attr
.u
.val
;
1714 high_pc
= attr
.u
.val
;
1718 read_rangelist (unit
, &func
->arange
, attr
.u
.val
);
1721 case DW_AT_decl_file
:
1722 func
->file
= concat_filename (unit
->line_table
,
1726 case DW_AT_decl_line
:
1727 func
->line
= attr
.u
.val
;
1739 var
->name
= attr
.u
.str
;
1742 case DW_AT_decl_file
:
1743 var
->file
= concat_filename (unit
->line_table
,
1747 case DW_AT_decl_line
:
1748 var
->line
= attr
.u
.val
;
1751 case DW_AT_external
:
1752 if (attr
.u
.val
!= 0)
1756 case DW_AT_location
:
1762 case DW_FORM_block1
:
1763 case DW_FORM_block2
:
1764 case DW_FORM_block4
:
1765 if (*attr
.u
.blk
->data
== DW_OP_addr
)
1781 if (func
&& high_pc
!= 0)
1783 arange_add (unit
->abfd
, &func
->arange
, low_pc
, high_pc
);
1786 if (abbrev
->has_children
)
1793 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1794 includes the compilation unit header that proceeds the DIE's, but
1795 does not include the length field that precedes each compilation
1796 unit header. END_PTR points one past the end of this comp unit.
1797 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1799 This routine does not read the whole compilation unit; only enough
1800 to get to the line number information for the compilation unit. */
1802 static struct comp_unit
*
1803 parse_comp_unit (bfd
*abfd
,
1804 struct dwarf2_debug
*stash
,
1805 bfd_vma unit_length
,
1806 bfd_byte
*info_ptr_unit
,
1807 unsigned int offset_size
)
1809 struct comp_unit
* unit
;
1810 unsigned int version
;
1811 bfd_uint64_t abbrev_offset
= 0;
1812 unsigned int addr_size
;
1813 struct abbrev_info
** abbrevs
;
1814 unsigned int abbrev_number
, bytes_read
, i
;
1815 struct abbrev_info
*abbrev
;
1816 struct attribute attr
;
1817 bfd_byte
*info_ptr
= stash
->info_ptr
;
1818 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
1821 bfd_vma high_pc
= 0;
1823 version
= read_2_bytes (abfd
, info_ptr
);
1825 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
1826 if (offset_size
== 4)
1827 abbrev_offset
= read_4_bytes (abfd
, info_ptr
);
1829 abbrev_offset
= read_8_bytes (abfd
, info_ptr
);
1830 info_ptr
+= offset_size
;
1831 addr_size
= read_1_byte (abfd
, info_ptr
);
1836 (*_bfd_error_handler
) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version
);
1837 bfd_set_error (bfd_error_bad_value
);
1841 if (addr_size
> sizeof (bfd_vma
))
1843 (*_bfd_error_handler
) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1845 (unsigned int) sizeof (bfd_vma
));
1846 bfd_set_error (bfd_error_bad_value
);
1850 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
1852 (*_bfd_error_handler
) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size
);
1853 bfd_set_error (bfd_error_bad_value
);
1857 /* Read the abbrevs for this compilation unit into a table. */
1858 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
);
1862 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1863 info_ptr
+= bytes_read
;
1864 if (! abbrev_number
)
1866 (*_bfd_error_handler
) (_("Dwarf Error: Bad abbrev number: %u."),
1868 bfd_set_error (bfd_error_bad_value
);
1872 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
1875 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
1877 bfd_set_error (bfd_error_bad_value
);
1881 amt
= sizeof (struct comp_unit
);
1882 unit
= bfd_zalloc (abfd
, amt
);
1884 unit
->addr_size
= addr_size
;
1885 unit
->offset_size
= offset_size
;
1886 unit
->abbrevs
= abbrevs
;
1887 unit
->end_ptr
= end_ptr
;
1888 unit
->stash
= stash
;
1889 unit
->info_ptr_unit
= info_ptr_unit
;
1891 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1893 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1895 /* Store the data if it is of an attribute we want to keep in a
1896 partial symbol table. */
1899 case DW_AT_stmt_list
:
1901 unit
->line_offset
= attr
.u
.val
;
1905 unit
->name
= attr
.u
.str
;
1909 low_pc
= attr
.u
.val
;
1910 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
1911 this is the base address to use when reading location
1912 lists or range lists. */
1913 unit
->base_address
= low_pc
;
1917 high_pc
= attr
.u
.val
;
1921 read_rangelist (unit
, &unit
->arange
, attr
.u
.val
);
1924 case DW_AT_comp_dir
:
1926 char *comp_dir
= attr
.u
.str
;
1929 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1930 directory, get rid of it. */
1931 char *cp
= strchr (comp_dir
, ':');
1933 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
1936 unit
->comp_dir
= comp_dir
;
1946 arange_add (unit
->abfd
, &unit
->arange
, low_pc
, high_pc
);
1949 unit
->first_child_die_ptr
= info_ptr
;
1953 /* Return TRUE if UNIT contains the address given by ADDR. */
1956 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
1958 struct arange
*arange
;
1963 arange
= &unit
->arange
;
1966 if (addr
>= arange
->low
&& addr
< arange
->high
)
1968 arange
= arange
->next
;
1975 /* If UNIT contains ADDR, set the output parameters to the values for
1976 the line containing ADDR. The output parameters, FILENAME_PTR,
1977 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1980 Return TRUE if UNIT contains ADDR, and no errors were encountered;
1984 comp_unit_find_nearest_line (struct comp_unit
*unit
,
1986 const char **filename_ptr
,
1987 const char **functionname_ptr
,
1988 unsigned int *linenumber_ptr
,
1989 struct dwarf2_debug
*stash
)
1993 struct funcinfo
*function
;
1998 if (! unit
->line_table
)
2000 if (! unit
->stmtlist
)
2006 unit
->line_table
= decode_line_info (unit
, stash
);
2008 if (! unit
->line_table
)
2014 if (unit
->first_child_die_ptr
< unit
->end_ptr
2015 && ! scan_unit_for_symbols (unit
))
2023 func_p
= lookup_address_in_function_table (unit
, addr
,
2024 &function
, functionname_ptr
);
2025 if (func_p
&& (function
->tag
== DW_TAG_inlined_subroutine
))
2026 stash
->inliner_chain
= function
;
2027 line_p
= lookup_address_in_line_info_table (unit
->line_table
, addr
,
2028 function
, filename_ptr
,
2030 return line_p
|| func_p
;
2033 /* If UNIT contains SYM at ADDR, set the output parameters to the
2034 values for the line containing SYM. The output parameters,
2035 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2038 Return TRUE if UNIT contains SYM, and no errors were encountered;
2042 comp_unit_find_line (struct comp_unit
*unit
,
2045 const char **filename_ptr
,
2046 unsigned int *linenumber_ptr
,
2047 struct dwarf2_debug
*stash
)
2052 if (! unit
->line_table
)
2054 if (! unit
->stmtlist
)
2060 unit
->line_table
= decode_line_info (unit
, stash
);
2062 if (! unit
->line_table
)
2068 if (unit
->first_child_die_ptr
< unit
->end_ptr
2069 && ! scan_unit_for_symbols (unit
))
2076 if (sym
->flags
& BSF_FUNCTION
)
2077 return lookup_symbol_in_function_table (unit
, sym
, addr
,
2081 return lookup_symbol_in_variable_table (unit
, sym
, filename_ptr
,
2085 /* Locate a section in a BFD containing debugging info. The search starts
2086 from the section after AFTER_SEC, or from the first section in the BFD if
2087 AFTER_SEC is NULL. The search works by examining the names of the
2088 sections. There are two permissiable names. The first is .debug_info.
2089 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2090 This is a variation on the .debug_info section which has a checksum
2091 describing the contents appended onto the name. This allows the linker to
2092 identify and discard duplicate debugging sections for different
2093 compilation units. */
2094 #define DWARF2_DEBUG_INFO ".debug_info"
2095 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2098 find_debug_info (bfd
*abfd
, asection
*after_sec
)
2103 msec
= after_sec
->next
;
2105 msec
= abfd
->sections
;
2109 if (strcmp (msec
->name
, DWARF2_DEBUG_INFO
) == 0)
2112 if (strncmp (msec
->name
, GNU_LINKONCE_INFO
, strlen (GNU_LINKONCE_INFO
)) == 0)
2121 /* The DWARF2 version of find_nearest_line. Return TRUE if the line
2122 is found without error. ADDR_SIZE is the number of bytes in the
2123 initial .debug_info length field and in the abbreviation offset.
2124 You may use zero to indicate that the default value should be
2128 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
2132 const char **filename_ptr
,
2133 const char **functionname_ptr
,
2134 unsigned int *linenumber_ptr
,
2135 unsigned int addr_size
,
2138 /* Read each compilation unit from the section .debug_info, and check
2139 to see if it contains the address we are searching for. If yes,
2140 lookup the address, and return the line number info. If no, go
2141 on to the next compilation unit.
2143 We keep a list of all the previously read compilation units, and
2144 a pointer to the next un-read compilation unit. Check the
2145 previously read units before reading more. */
2146 struct dwarf2_debug
*stash
;
2148 /* What address are we looking for? */
2151 struct comp_unit
* each
;
2155 if (section
->output_section
)
2156 addr
+= section
->output_section
->vma
+ section
->output_offset
;
2158 addr
+= section
->vma
;
2159 *filename_ptr
= NULL
;
2160 *functionname_ptr
= NULL
;
2161 *linenumber_ptr
= 0;
2163 /* The DWARF2 spec says that the initial length field, and the
2164 offset of the abbreviation table, should both be 4-byte values.
2165 However, some compilers do things differently. */
2168 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
2172 bfd_size_type total_size
;
2174 bfd_size_type amt
= sizeof (struct dwarf2_debug
);
2176 stash
= bfd_zalloc (abfd
, amt
);
2182 msec
= find_debug_info (abfd
, NULL
);
2184 /* No dwarf2 info. Note that at this point the stash
2185 has been allocated, but contains zeros, this lets
2186 future calls to this function fail quicker. */
2189 /* There can be more than one DWARF2 info section in a BFD these days.
2190 Read them all in and produce one large stash. We do this in two
2191 passes - in the first pass we just accumulate the section sizes.
2192 In the second pass we read in the section's contents. The allows
2193 us to avoid reallocing the data as we add sections to the stash. */
2194 for (total_size
= 0; msec
; msec
= find_debug_info (abfd
, msec
))
2195 total_size
+= msec
->size
;
2197 stash
->info_ptr
= bfd_alloc (abfd
, total_size
);
2198 if (stash
->info_ptr
== NULL
)
2201 stash
->info_ptr_end
= stash
->info_ptr
;
2203 for (msec
= find_debug_info (abfd
, NULL
);
2205 msec
= find_debug_info (abfd
, msec
))
2208 bfd_size_type start
;
2214 start
= stash
->info_ptr_end
- stash
->info_ptr
;
2216 if ((bfd_simple_get_relocated_section_contents
2217 (abfd
, msec
, stash
->info_ptr
+ start
, symbols
)) == NULL
)
2220 stash
->info_ptr_end
= stash
->info_ptr
+ start
+ size
;
2223 BFD_ASSERT (stash
->info_ptr_end
== stash
->info_ptr
+ total_size
);
2225 stash
->sec
= find_debug_info (abfd
, NULL
);
2226 stash
->sec_info_ptr
= stash
->info_ptr
;
2227 stash
->syms
= symbols
;
2230 /* A null info_ptr indicates that there is no dwarf2 info
2231 (or that an error occured while setting up the stash). */
2232 if (! stash
->info_ptr
)
2235 stash
->inliner_chain
= NULL
;
2237 /* Check the previously read comp. units first. */
2238 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
2239 if (comp_unit_contains_address (each
, addr
))
2240 return comp_unit_find_nearest_line (each
, addr
, filename_ptr
,
2241 functionname_ptr
, linenumber_ptr
,
2244 /* Read each remaining comp. units checking each as they are read. */
2245 while (stash
->info_ptr
< stash
->info_ptr_end
)
2249 unsigned int offset_size
= addr_size
;
2250 bfd_byte
*info_ptr_unit
= stash
->info_ptr
;
2252 length
= read_4_bytes (abfd
, stash
->info_ptr
);
2253 /* A 0xffffff length is the DWARF3 way of indicating we use
2254 64-bit offsets, instead of 32-bit offsets. */
2255 if (length
== 0xffffffff)
2258 length
= read_8_bytes (abfd
, stash
->info_ptr
+ 4);
2259 stash
->info_ptr
+= 12;
2261 /* A zero length is the IRIX way of indicating 64-bit offsets,
2262 mostly because the 64-bit length will generally fit in 32
2263 bits, and the endianness helps. */
2264 else if (length
== 0)
2267 length
= read_4_bytes (abfd
, stash
->info_ptr
+ 4);
2268 stash
->info_ptr
+= 8;
2270 /* In the absence of the hints above, we assume addr_size-sized
2271 offsets, for backward-compatibility with pre-DWARF3 64-bit
2273 else if (addr_size
== 8)
2275 length
= read_8_bytes (abfd
, stash
->info_ptr
);
2276 stash
->info_ptr
+= 8;
2279 stash
->info_ptr
+= 4;
2283 each
= parse_comp_unit (abfd
, stash
, length
, info_ptr_unit
,
2285 stash
->info_ptr
+= length
;
2287 if ((bfd_vma
) (stash
->info_ptr
- stash
->sec_info_ptr
)
2288 == stash
->sec
->size
)
2290 stash
->sec
= find_debug_info (abfd
, stash
->sec
);
2291 stash
->sec_info_ptr
= stash
->info_ptr
;
2296 each
->next_unit
= stash
->all_comp_units
;
2297 stash
->all_comp_units
= each
;
2299 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2300 compilation units. If we don't have them (i.e.,
2301 unit->high == 0), we need to consult the line info
2302 table to see if a compilation unit contains the given
2304 if (each
->arange
.high
> 0)
2306 if (comp_unit_contains_address (each
, addr
))
2307 return comp_unit_find_nearest_line (each
, addr
,
2315 found
= comp_unit_find_nearest_line (each
, addr
,
2330 /* The DWARF2 version of find_line. Return TRUE if the line is found
2334 _bfd_dwarf2_find_line (bfd
*abfd
,
2337 const char **filename_ptr
,
2338 unsigned int *linenumber_ptr
,
2339 unsigned int addr_size
,
2342 /* Read each compilation unit from the section .debug_info, and check
2343 to see if it contains the address we are searching for. If yes,
2344 lookup the address, and return the line number info. If no, go
2345 on to the next compilation unit.
2347 We keep a list of all the previously read compilation units, and
2348 a pointer to the next un-read compilation unit. Check the
2349 previously read units before reading more. */
2350 struct dwarf2_debug
*stash
;
2352 /* What address are we looking for? */
2355 struct comp_unit
* each
;
2361 section
= bfd_get_section (symbol
);
2363 addr
= symbol
->value
;
2364 if (section
->output_section
)
2365 addr
+= section
->output_section
->vma
+ section
->output_offset
;
2367 addr
+= section
->vma
;
2369 *filename_ptr
= NULL
;
2371 *filename_ptr
= NULL
;
2372 *linenumber_ptr
= 0;
2376 bfd_size_type total_size
;
2378 bfd_size_type amt
= sizeof (struct dwarf2_debug
);
2380 stash
= bfd_zalloc (abfd
, amt
);
2386 msec
= find_debug_info (abfd
, NULL
);
2388 /* No dwarf2 info. Note that at this point the stash
2389 has been allocated, but contains zeros, this lets
2390 future calls to this function fail quicker. */
2393 /* There can be more than one DWARF2 info section in a BFD these days.
2394 Read them all in and produce one large stash. We do this in two
2395 passes - in the first pass we just accumulate the section sizes.
2396 In the second pass we read in the section's contents. The allows
2397 us to avoid reallocing the data as we add sections to the stash. */
2398 for (total_size
= 0; msec
; msec
= find_debug_info (abfd
, msec
))
2399 total_size
+= msec
->size
;
2401 stash
->info_ptr
= bfd_alloc (abfd
, total_size
);
2402 if (stash
->info_ptr
== NULL
)
2405 stash
->info_ptr_end
= stash
->info_ptr
;
2407 for (msec
= find_debug_info (abfd
, NULL
);
2409 msec
= find_debug_info (abfd
, msec
))
2412 bfd_size_type start
;
2418 start
= stash
->info_ptr_end
- stash
->info_ptr
;
2420 if ((bfd_simple_get_relocated_section_contents
2421 (abfd
, msec
, stash
->info_ptr
+ start
, symbols
)) == NULL
)
2424 stash
->info_ptr_end
= stash
->info_ptr
+ start
+ size
;
2427 BFD_ASSERT (stash
->info_ptr_end
== stash
->info_ptr
+ total_size
);
2429 stash
->sec
= find_debug_info (abfd
, NULL
);
2430 stash
->sec_info_ptr
= stash
->info_ptr
;
2431 stash
->syms
= symbols
;
2434 /* A null info_ptr indicates that there is no dwarf2 info
2435 (or that an error occured while setting up the stash). */
2436 if (! stash
->info_ptr
)
2439 stash
->inliner_chain
= NULL
;
2441 /* Check the previously read comp. units first. */
2442 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
2443 if ((symbol
->flags
& BSF_FUNCTION
) == 0
2444 || comp_unit_contains_address (each
, addr
))
2446 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
2447 linenumber_ptr
, stash
);
2452 /* The DWARF2 spec says that the initial length field, and the
2453 offset of the abbreviation table, should both be 4-byte values.
2454 However, some compilers do things differently. */
2457 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
2459 /* Read each remaining comp. units checking each as they are read. */
2460 while (stash
->info_ptr
< stash
->info_ptr_end
)
2463 unsigned int offset_size
= addr_size
;
2464 bfd_byte
*info_ptr_unit
= stash
->info_ptr
;
2466 length
= read_4_bytes (abfd
, stash
->info_ptr
);
2467 /* A 0xffffff length is the DWARF3 way of indicating we use
2468 64-bit offsets, instead of 32-bit offsets. */
2469 if (length
== 0xffffffff)
2472 length
= read_8_bytes (abfd
, stash
->info_ptr
+ 4);
2473 stash
->info_ptr
+= 12;
2475 /* A zero length is the IRIX way of indicating 64-bit offsets,
2476 mostly because the 64-bit length will generally fit in 32
2477 bits, and the endianness helps. */
2478 else if (length
== 0)
2481 length
= read_4_bytes (abfd
, stash
->info_ptr
+ 4);
2482 stash
->info_ptr
+= 8;
2484 /* In the absence of the hints above, we assume addr_size-sized
2485 offsets, for backward-compatibility with pre-DWARF3 64-bit
2487 else if (addr_size
== 8)
2489 length
= read_8_bytes (abfd
, stash
->info_ptr
);
2490 stash
->info_ptr
+= 8;
2493 stash
->info_ptr
+= 4;
2497 each
= parse_comp_unit (abfd
, stash
, length
, info_ptr_unit
,
2499 stash
->info_ptr
+= length
;
2501 if ((bfd_vma
) (stash
->info_ptr
- stash
->sec_info_ptr
)
2502 == stash
->sec
->size
)
2504 stash
->sec
= find_debug_info (abfd
, stash
->sec
);
2505 stash
->sec_info_ptr
= stash
->info_ptr
;
2510 each
->next_unit
= stash
->all_comp_units
;
2511 stash
->all_comp_units
= each
;
2513 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2514 compilation units. If we don't have them (i.e.,
2515 unit->high == 0), we need to consult the line info
2516 table to see if a compilation unit contains the given
2518 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
2519 || each
->arange
.high
<= 0
2520 || comp_unit_contains_address (each
, addr
))
2521 && comp_unit_find_line (each
, symbol
, addr
,
2535 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
2536 const char **filename_ptr
,
2537 const char **functionname_ptr
,
2538 unsigned int *linenumber_ptr
,
2541 struct dwarf2_debug
*stash
;
2546 struct funcinfo
*func
= stash
->inliner_chain
;
2547 if (func
&& func
->caller_func
)
2549 *filename_ptr
= func
->caller_file
;
2550 *functionname_ptr
= func
->caller_func
->name
;
2551 *linenumber_ptr
= func
->caller_line
;
2552 stash
->inliner_chain
= func
->caller_func
;
2561 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
)
2563 struct comp_unit
*each
;
2564 struct dwarf2_debug
*stash
;
2566 if (abfd
== NULL
|| elf_tdata (abfd
) == NULL
)
2569 stash
= elf_tdata (abfd
)->dwarf2_find_line_info
;
2574 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
2576 struct abbrev_info
**abbrevs
= each
->abbrevs
;
2579 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
2581 struct abbrev_info
*abbrev
= abbrevs
[i
];
2585 free (abbrev
->attrs
);
2586 abbrev
= abbrev
->next
;
2590 if (each
->line_table
)
2592 free (each
->line_table
->dirs
);
2593 free (each
->line_table
->files
);
2597 free (stash
->dwarf_abbrev_buffer
);
2598 free (stash
->dwarf_line_buffer
);
2599 free (stash
->dwarf_ranges_buffer
);