2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
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. */
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
;
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
;
119 /* Pointer to the .debug_str section loaded into memory. */
120 char* dwarf_str_buffer
;
122 /* Length of the loaded .debug_str section. */
123 unsigned long dwarf_str_size
;
133 /* A minimal decoding of DWARF2 compilation units. We only decode
134 what's needed to get to the line number information. */
138 /* Chain the previously read compilation units. */
139 struct comp_unit
* next_unit
;
141 /* Keep the bdf convenient (for memory allocation). */
144 /* The lowest and higest addresses contained in this compilation
145 unit as specified in the compilation unit header. */
146 struct arange arange
;
148 /* The DW_AT_name attribute (for error messages). */
151 /* The abbrev hash table. */
152 struct abbrev_info
** abbrevs
;
154 /* Note that an error was found by comp_unit_find_nearest_line. */
157 /* The DW_AT_comp_dir attribute. */
160 /* TRUE if there is a line number table associated with this comp. unit. */
163 /* The offset into .debug_line of the line number table. */
164 unsigned long line_offset
;
166 /* Pointer to the first child die for the comp unit. */
167 char *first_child_die_ptr
;
169 /* The end of the comp unit. */
172 /* The decoded line number, NULL if not yet decoded. */
173 struct line_info_table
* line_table
;
175 /* A list of the functions found in this comp. unit. */
176 struct funcinfo
* function_table
;
178 /* Pointer to dwarf2_debug structure. */
179 struct dwarf2_debug
*stash
;
181 /* Address size for this unit - from unit header. */
182 unsigned char addr_size
;
184 /* Offset size for this unit - from unit header. */
185 unsigned char offset_size
;
188 /* This data structure holds the information of an abbrev. */
191 unsigned int number
; /* Number identifying abbrev. */
192 enum dwarf_tag tag
; /* DWARF tag. */
193 int has_children
; /* Boolean. */
194 unsigned int num_attrs
; /* Number of attributes. */
195 struct attr_abbrev
*attrs
; /* An array of attribute descriptions. */
196 struct abbrev_info
*next
; /* Next in chain. */
201 enum dwarf_attribute name
;
202 enum dwarf_form form
;
205 #ifndef ABBREV_HASH_SIZE
206 #define ABBREV_HASH_SIZE 121
208 #ifndef ATTR_ALLOC_CHUNK
209 #define ATTR_ALLOC_CHUNK 4
212 static unsigned int read_1_byte
PARAMS ((bfd
*, char *));
213 static int read_1_signed_byte
PARAMS ((bfd
*, char *));
214 static unsigned int read_2_bytes
PARAMS ((bfd
*, char *));
215 static unsigned int read_4_bytes
PARAMS ((bfd
*, char *));
216 static bfd_vma read_8_bytes
PARAMS ((bfd
*, char *));
217 static char *read_n_bytes
PARAMS ((bfd
*, char *, unsigned int));
218 static char *read_string
PARAMS ((bfd
*, char *, unsigned int *));
219 static char *read_indirect_string
PARAMS ((struct comp_unit
*, char *, unsigned int *));
220 static unsigned int read_unsigned_leb128
221 PARAMS ((bfd
*, char *, unsigned int *));
222 static int read_signed_leb128
223 PARAMS ((bfd
*, char *, unsigned int *));
224 static bfd_vma read_address
PARAMS ((struct comp_unit
*, char *));
225 static struct abbrev_info
*lookup_abbrev
226 PARAMS ((unsigned int, struct abbrev_info
**));
227 static struct abbrev_info
**read_abbrevs
228 PARAMS ((bfd
*, bfd_vma
, struct dwarf2_debug
*));
229 static char *read_attribute
230 PARAMS ((struct attribute
*, struct attr_abbrev
*,
231 struct comp_unit
*, char *));
232 static char *read_attribute_value
233 PARAMS ((struct attribute
*, unsigned,
234 struct comp_unit
*, char *));
235 static void add_line_info
236 PARAMS ((struct line_info_table
*, bfd_vma
, char *,
237 unsigned int, unsigned int, int));
238 static char *concat_filename
PARAMS ((struct line_info_table
*, unsigned int));
239 static void arange_add
PARAMS ((struct comp_unit
*, bfd_vma
, bfd_vma
));
240 static struct line_info_table
*decode_line_info
241 PARAMS ((struct comp_unit
*, struct dwarf2_debug
*));
242 static bfd_boolean lookup_address_in_line_info_table
243 PARAMS ((struct line_info_table
*, bfd_vma
, struct funcinfo
*,
244 const char **, unsigned int *));
245 static bfd_boolean lookup_address_in_function_table
246 PARAMS ((struct funcinfo
*, bfd_vma
, struct funcinfo
**, const char **));
247 static bfd_boolean scan_unit_for_functions
PARAMS ((struct comp_unit
*));
248 static bfd_vma find_rela_addend
249 PARAMS ((bfd
*, asection
*, bfd_size_type
, asymbol
**));
250 static struct comp_unit
*parse_comp_unit
251 PARAMS ((bfd
*, struct dwarf2_debug
*, bfd_vma
, unsigned int));
252 static bfd_boolean comp_unit_contains_address
253 PARAMS ((struct comp_unit
*, bfd_vma
));
254 static bfd_boolean comp_unit_find_nearest_line
255 PARAMS ((struct comp_unit
*, bfd_vma
, const char **, const char **,
256 unsigned int *, struct dwarf2_debug
*));
257 static asection
*find_debug_info
PARAMS ((bfd
*, asection
*));
260 The following function up to the END VERBATIM mark are
261 copied directly from dwarf2read.c. */
263 /* Read dwarf information from a buffer. */
266 read_1_byte (abfd
, buf
)
267 bfd
*abfd ATTRIBUTE_UNUSED
;
270 return bfd_get_8 (abfd
, (bfd_byte
*) buf
);
274 read_1_signed_byte (abfd
, buf
)
275 bfd
*abfd ATTRIBUTE_UNUSED
;
278 return bfd_get_signed_8 (abfd
, (bfd_byte
*) buf
);
282 read_2_bytes (abfd
, buf
)
286 return bfd_get_16 (abfd
, (bfd_byte
*) buf
);
289 #if 0 /* This is not used. */
292 read_2_signed_bytes (abfd
, buf
)
296 return bfd_get_signed_16 (abfd
, (bfd_byte
*) buf
);
302 read_4_bytes (abfd
, buf
)
306 return bfd_get_32 (abfd
, (bfd_byte
*) buf
);
309 #if 0 /* This is not used. */
312 read_4_signed_bytes (abfd
, buf
)
316 return bfd_get_signed_32 (abfd
, (bfd_byte
*) buf
);
322 read_8_bytes (abfd
, buf
)
326 return bfd_get_64 (abfd
, (bfd_byte
*) buf
);
330 read_n_bytes (abfd
, buf
, size
)
331 bfd
*abfd ATTRIBUTE_UNUSED
;
333 unsigned int size ATTRIBUTE_UNUSED
;
335 /* If the size of a host char is 8 bits, we can return a pointer
336 to the buffer, otherwise we have to copy the data to a buffer
337 allocated on the temporary obstack. */
342 read_string (abfd
, buf
, bytes_read_ptr
)
343 bfd
*abfd ATTRIBUTE_UNUSED
;
345 unsigned int *bytes_read_ptr
;
347 /* Return a pointer to the embedded string. */
354 *bytes_read_ptr
= strlen (buf
) + 1;
359 read_indirect_string (unit
, buf
, bytes_read_ptr
)
360 struct comp_unit
* unit
;
362 unsigned int *bytes_read_ptr
;
365 struct dwarf2_debug
*stash
= unit
->stash
;
367 if (unit
->offset_size
== 4)
368 offset
= read_4_bytes (unit
->abfd
, buf
);
370 offset
= read_8_bytes (unit
->abfd
, buf
);
371 *bytes_read_ptr
= unit
->offset_size
;
373 if (! stash
->dwarf_str_buffer
)
376 bfd
*abfd
= unit
->abfd
;
378 msec
= bfd_get_section_by_name (abfd
, ".debug_str");
381 (*_bfd_error_handler
)
382 (_("Dwarf Error: Can't find .debug_str section."));
383 bfd_set_error (bfd_error_bad_value
);
387 stash
->dwarf_str_size
= msec
->_raw_size
;
388 stash
->dwarf_str_buffer
= (char*) bfd_alloc (abfd
, msec
->_raw_size
);
389 if (! stash
->dwarf_abbrev_buffer
)
392 if (! bfd_get_section_contents (abfd
, msec
, stash
->dwarf_str_buffer
,
393 (bfd_vma
) 0, msec
->_raw_size
))
397 if (offset
>= stash
->dwarf_str_size
)
399 (*_bfd_error_handler
) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
400 (unsigned long) offset
, stash
->dwarf_str_size
);
401 bfd_set_error (bfd_error_bad_value
);
405 buf
= stash
->dwarf_str_buffer
+ offset
;
412 read_unsigned_leb128 (abfd
, buf
, bytes_read_ptr
)
413 bfd
*abfd ATTRIBUTE_UNUSED
;
415 unsigned int *bytes_read_ptr
;
418 unsigned int num_read
;
428 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
431 result
|= ((byte
& 0x7f) << shift
);
436 * bytes_read_ptr
= num_read
;
442 read_signed_leb128 (abfd
, buf
, bytes_read_ptr
)
443 bfd
*abfd ATTRIBUTE_UNUSED
;
445 unsigned int * bytes_read_ptr
;
458 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
461 result
|= ((byte
& 0x7f) << shift
);
466 if ((shift
< 32) && (byte
& 0x40))
467 result
|= -(1 << shift
);
469 * bytes_read_ptr
= num_read
;
477 read_address (unit
, buf
)
478 struct comp_unit
* unit
;
481 switch (unit
->addr_size
)
484 return bfd_get_64 (unit
->abfd
, (bfd_byte
*) buf
);
486 return bfd_get_32 (unit
->abfd
, (bfd_byte
*) buf
);
488 return bfd_get_16 (unit
->abfd
, (bfd_byte
*) buf
);
494 /* Lookup an abbrev_info structure in the abbrev hash table. */
496 static struct abbrev_info
*
497 lookup_abbrev (number
,abbrevs
)
499 struct abbrev_info
**abbrevs
;
501 unsigned int hash_number
;
502 struct abbrev_info
*abbrev
;
504 hash_number
= number
% ABBREV_HASH_SIZE
;
505 abbrev
= abbrevs
[hash_number
];
509 if (abbrev
->number
== number
)
512 abbrev
= abbrev
->next
;
518 /* In DWARF version 2, the description of the debugging information is
519 stored in a separate .debug_abbrev section. Before we read any
520 dies from a section we read in all abbreviations and install them
523 static struct abbrev_info
**
524 read_abbrevs (abfd
, offset
, stash
)
527 struct dwarf2_debug
*stash
;
529 struct abbrev_info
**abbrevs
;
531 struct abbrev_info
*cur_abbrev
;
532 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
533 unsigned int abbrev_form
, hash_number
;
536 if (! stash
->dwarf_abbrev_buffer
)
540 msec
= bfd_get_section_by_name (abfd
, ".debug_abbrev");
543 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_abbrev section."));
544 bfd_set_error (bfd_error_bad_value
);
548 stash
->dwarf_abbrev_size
= msec
->_raw_size
;
549 stash
->dwarf_abbrev_buffer
= (char*) bfd_alloc (abfd
, msec
->_raw_size
);
550 if (! stash
->dwarf_abbrev_buffer
)
553 if (! bfd_get_section_contents (abfd
, msec
, stash
->dwarf_abbrev_buffer
,
554 (bfd_vma
) 0, msec
->_raw_size
))
558 if (offset
>= stash
->dwarf_abbrev_size
)
560 (*_bfd_error_handler
) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
561 (unsigned long) offset
, stash
->dwarf_abbrev_size
);
562 bfd_set_error (bfd_error_bad_value
);
566 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
567 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
569 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
570 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
571 abbrev_ptr
+= bytes_read
;
573 /* Loop until we reach an abbrev number of 0. */
574 while (abbrev_number
)
576 amt
= sizeof (struct abbrev_info
);
577 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
579 /* Read in abbrev header. */
580 cur_abbrev
->number
= abbrev_number
;
581 cur_abbrev
->tag
= (enum dwarf_tag
)
582 read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
583 abbrev_ptr
+= bytes_read
;
584 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
);
587 /* Now read in declarations. */
588 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
589 abbrev_ptr
+= bytes_read
;
590 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
591 abbrev_ptr
+= bytes_read
;
595 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
597 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
598 amt
*= sizeof (struct attr_abbrev
);
599 cur_abbrev
->attrs
= ((struct attr_abbrev
*)
600 bfd_realloc (cur_abbrev
->attrs
, amt
));
601 if (! cur_abbrev
->attrs
)
605 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
606 = (enum dwarf_attribute
) abbrev_name
;
607 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
++].form
608 = (enum dwarf_form
) abbrev_form
;
609 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
610 abbrev_ptr
+= bytes_read
;
611 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
612 abbrev_ptr
+= bytes_read
;
615 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
616 cur_abbrev
->next
= abbrevs
[hash_number
];
617 abbrevs
[hash_number
] = cur_abbrev
;
619 /* Get next abbreviation.
620 Under Irix6 the abbreviations for a compilation unit are not
621 always properly terminated with an abbrev number of 0.
622 Exit loop if we encounter an abbreviation which we have
623 already read (which means we are about to read the abbreviations
624 for the next compile unit) or if the end of the abbreviation
626 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
627 >= stash
->dwarf_abbrev_size
)
629 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
630 abbrev_ptr
+= bytes_read
;
631 if (lookup_abbrev (abbrev_number
,abbrevs
) != NULL
)
638 /* Read an attribute value described by an attribute form. */
641 read_attribute_value (attr
, form
, unit
, info_ptr
)
642 struct attribute
*attr
;
644 struct comp_unit
*unit
;
647 bfd
*abfd
= unit
->abfd
;
648 unsigned int bytes_read
;
649 struct dwarf_block
*blk
;
652 attr
->form
= (enum dwarf_form
) form
;
657 /* FIXME: DWARF3 draft sais DW_FORM_ref_addr is offset_size. */
658 case DW_FORM_ref_addr
:
659 DW_ADDR (attr
) = read_address (unit
, info_ptr
);
660 info_ptr
+= unit
->addr_size
;
663 amt
= sizeof (struct dwarf_block
);
664 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
665 blk
->size
= read_2_bytes (abfd
, info_ptr
);
667 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
668 info_ptr
+= blk
->size
;
669 DW_BLOCK (attr
) = blk
;
672 amt
= sizeof (struct dwarf_block
);
673 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
674 blk
->size
= read_4_bytes (abfd
, info_ptr
);
676 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
677 info_ptr
+= blk
->size
;
678 DW_BLOCK (attr
) = blk
;
681 DW_UNSND (attr
) = read_2_bytes (abfd
, info_ptr
);
685 DW_UNSND (attr
) = read_4_bytes (abfd
, info_ptr
);
689 DW_UNSND (attr
) = read_8_bytes (abfd
, info_ptr
);
693 DW_STRING (attr
) = read_string (abfd
, info_ptr
, &bytes_read
);
694 info_ptr
+= bytes_read
;
697 DW_STRING (attr
) = read_indirect_string (unit
, info_ptr
, &bytes_read
);
698 info_ptr
+= bytes_read
;
701 amt
= sizeof (struct dwarf_block
);
702 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
703 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
704 info_ptr
+= bytes_read
;
705 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
706 info_ptr
+= blk
->size
;
707 DW_BLOCK (attr
) = blk
;
710 amt
= sizeof (struct dwarf_block
);
711 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
712 blk
->size
= read_1_byte (abfd
, info_ptr
);
714 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
715 info_ptr
+= blk
->size
;
716 DW_BLOCK (attr
) = blk
;
719 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
723 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
727 DW_SND (attr
) = read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
728 info_ptr
+= bytes_read
;
731 DW_UNSND (attr
) = read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
732 info_ptr
+= bytes_read
;
735 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
739 DW_UNSND (attr
) = read_2_bytes (abfd
, info_ptr
);
743 DW_UNSND (attr
) = read_4_bytes (abfd
, info_ptr
);
747 DW_UNSND (attr
) = read_8_bytes (abfd
, info_ptr
);
750 case DW_FORM_ref_udata
:
751 DW_UNSND (attr
) = read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
752 info_ptr
+= bytes_read
;
754 case DW_FORM_indirect
:
755 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
756 info_ptr
+= bytes_read
;
757 info_ptr
= read_attribute_value (attr
, form
, unit
, info_ptr
);
760 (*_bfd_error_handler
) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
762 bfd_set_error (bfd_error_bad_value
);
767 /* Read an attribute described by an abbreviated attribute. */
770 read_attribute (attr
, abbrev
, unit
, info_ptr
)
771 struct attribute
*attr
;
772 struct attr_abbrev
*abbrev
;
773 struct comp_unit
*unit
;
776 attr
->name
= abbrev
->name
;
777 info_ptr
= read_attribute_value (attr
, abbrev
->form
, unit
, info_ptr
);
781 /* Source line information table routines. */
783 #define FILE_ALLOC_CHUNK 5
784 #define DIR_ALLOC_CHUNK 5
788 struct line_info
* prev_line
;
793 int end_sequence
; /* End of (sequential) code sequence. */
804 struct line_info_table
807 unsigned int num_files
;
808 unsigned int num_dirs
;
811 struct fileinfo
* files
;
812 struct line_info
* last_line
; /* largest VMA */
813 struct line_info
* lcl_head
; /* local head; used in 'add_line_info' */
818 struct funcinfo
*prev_func
;
824 /* add_line_info: adds a new entry to the line_info list in the
825 line_info_table, ensuring that the list is sorted. Note that the
826 line_info list is sorted from highest to lowest VMA (with possible
827 duplicates); that is, line_info->prev_line always accesses an equal
830 add_line_info (table
, address
, filename
, line
, column
, end_sequence
)
831 struct line_info_table
* table
;
838 bfd_size_type amt
= sizeof (struct line_info
);
839 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
841 /* Find the correct location for 'info'. Normally we will receive
842 new line_info data 1) in order and 2) with increasing VMAs.
843 However some compilers break the rules (cf. decode_line_info) and
844 so we include some heuristics for quickly finding the correct
845 location for 'info'. In particular, these heuristics optimize for
846 the common case in which the VMA sequence that we receive is a
847 list of locally sorted VMAs such as
848 p...z a...j (where a < j < p < z)
850 Note: table->lcl_head is used to head an *actual* or *possible*
851 sequence within the list (such as a...j) that is not directly
852 headed by table->last_line
854 Note: we may receive duplicate entries from 'decode_line_info'. */
857 if (!table
->last_line
858 || address
>= table
->last_line
->address
)
860 /* Normal case: add 'info' to the beginning of the list */
861 info
->prev_line
= table
->last_line
;
862 table
->last_line
= info
;
864 /* lcl_head: initialize to head a *possible* sequence at the end. */
865 if (!table
->lcl_head
)
866 table
->lcl_head
= info
;
869 else if (!table
->lcl_head
->prev_line
870 && table
->lcl_head
->address
> address
)
872 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
873 list and 2) the head of 'info'. */
874 info
->prev_line
= NULL
;
875 table
->lcl_head
->prev_line
= info
;
878 else if (table
->lcl_head
->prev_line
879 && table
->lcl_head
->address
> address
880 && address
>= table
->lcl_head
->prev_line
->address
)
882 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
883 list and 2) the head of 'info'. */
884 info
->prev_line
= table
->lcl_head
->prev_line
;
885 table
->lcl_head
->prev_line
= info
;
890 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
891 heads for 'info'. Reset 'lcl_head' and repeat. */
892 struct line_info
* li2
= table
->last_line
; /* always non-NULL */
893 struct line_info
* li1
= li2
->prev_line
;
897 if (li2
->address
> address
&& address
>= li1
->address
)
900 li2
= li1
; /* always non-NULL */
901 li1
= li1
->prev_line
;
903 table
->lcl_head
= li2
;
906 /* Set member data of 'info'. */
907 info
->address
= address
;
908 info
->filename
= filename
;
910 info
->column
= column
;
911 info
->end_sequence
= end_sequence
;
915 concat_filename (table
, file
)
916 struct line_info_table
* table
;
921 if (file
- 1 >= table
->num_files
)
923 (*_bfd_error_handler
)
924 (_("Dwarf Error: mangled line number section (bad file number)."));
928 filename
= table
->files
[file
- 1].name
;
929 if (IS_ABSOLUTE_PATH(filename
))
933 char* dirname
= (table
->files
[file
- 1].dir
934 ? table
->dirs
[table
->files
[file
- 1].dir
- 1]
937 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown. The
938 best we can do is return the filename part. */
942 return (char*) concat (dirname
, "/", filename
, NULL
);
947 arange_add (unit
, low_pc
, high_pc
)
948 struct comp_unit
*unit
;
952 struct arange
*arange
;
954 /* First see if we can cheaply extend an existing range. */
955 arange
= &unit
->arange
;
959 if (low_pc
== arange
->high
)
961 arange
->high
= high_pc
;
964 if (high_pc
== arange
->low
)
966 arange
->low
= low_pc
;
969 arange
= arange
->next
;
973 if (unit
->arange
.high
== 0)
975 /* This is the first address range: store it in unit->arange. */
976 unit
->arange
.next
= 0;
977 unit
->arange
.low
= low_pc
;
978 unit
->arange
.high
= high_pc
;
982 /* Need to allocate a new arange and insert it into the arange list. */
983 arange
= (struct arange
*)
984 bfd_zalloc (unit
->abfd
, (bfd_size_type
) sizeof (*arange
));
985 arange
->low
= low_pc
;
986 arange
->high
= high_pc
;
988 arange
->next
= unit
->arange
.next
;
989 unit
->arange
.next
= arange
;
992 /* Decode the line number information for UNIT. */
994 static struct line_info_table
*
995 decode_line_info (unit
, stash
)
996 struct comp_unit
*unit
;
997 struct dwarf2_debug
*stash
;
999 bfd
*abfd
= unit
->abfd
;
1000 struct line_info_table
* table
;
1003 struct line_head lh
;
1004 unsigned int i
, bytes_read
, offset_size
;
1005 char *cur_file
, *cur_dir
;
1006 unsigned char op_code
, extended_op
, adj_opcode
;
1009 if (! stash
->dwarf_line_buffer
)
1013 msec
= bfd_get_section_by_name (abfd
, ".debug_line");
1016 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_line section."));
1017 bfd_set_error (bfd_error_bad_value
);
1021 stash
->dwarf_line_size
= msec
->_raw_size
;
1022 stash
->dwarf_line_buffer
= (char *) bfd_alloc (abfd
, msec
->_raw_size
);
1023 if (! stash
->dwarf_line_buffer
)
1026 if (! bfd_get_section_contents (abfd
, msec
, stash
->dwarf_line_buffer
,
1027 (bfd_vma
) 0, msec
->_raw_size
))
1030 /* FIXME: We ought to apply the relocs against this section before
1034 /* Since we are using un-relocated data, it is possible to get a bad value
1035 for the line_offset. Validate it here so that we won't get a segfault
1037 if (unit
->line_offset
>= stash
->dwarf_line_size
)
1039 (*_bfd_error_handler
) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
1040 unit
->line_offset
, stash
->dwarf_line_size
);
1041 bfd_set_error (bfd_error_bad_value
);
1045 amt
= sizeof (struct line_info_table
);
1046 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
1048 table
->comp_dir
= unit
->comp_dir
;
1050 table
->num_files
= 0;
1051 table
->files
= NULL
;
1053 table
->num_dirs
= 0;
1056 table
->files
= NULL
;
1057 table
->last_line
= NULL
;
1058 table
->lcl_head
= NULL
;
1060 line_ptr
= stash
->dwarf_line_buffer
+ unit
->line_offset
;
1062 /* Read in the prologue. */
1063 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
1066 if (lh
.total_length
== 0xffffffff)
1068 lh
.total_length
= read_8_bytes (abfd
, line_ptr
);
1072 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
1074 /* Handle (non-standard) 64-bit DWARF2 formats. */
1075 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
1079 line_end
= line_ptr
+ lh
.total_length
;
1080 lh
.version
= read_2_bytes (abfd
, line_ptr
);
1082 if (offset_size
== 4)
1083 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
);
1085 lh
.prologue_length
= read_8_bytes (abfd
, line_ptr
);
1086 line_ptr
+= offset_size
;
1087 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
);
1089 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
);
1091 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
);
1093 lh
.line_range
= read_1_byte (abfd
, line_ptr
);
1095 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
);
1097 amt
= lh
.opcode_base
* sizeof (unsigned char);
1098 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
1100 lh
.standard_opcode_lengths
[0] = 1;
1102 for (i
= 1; i
< lh
.opcode_base
; ++i
)
1104 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
);
1108 /* Read directory table. */
1109 while ((cur_dir
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1111 line_ptr
+= bytes_read
;
1113 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1115 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1116 amt
*= sizeof (char *);
1117 table
->dirs
= (char **) bfd_realloc (table
->dirs
, amt
);
1122 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1125 line_ptr
+= bytes_read
;
1127 /* Read file name table. */
1128 while ((cur_file
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1130 line_ptr
+= bytes_read
;
1132 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1134 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1135 amt
*= sizeof (struct fileinfo
);
1136 table
->files
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1141 table
->files
[table
->num_files
].name
= cur_file
;
1142 table
->files
[table
->num_files
].dir
=
1143 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1144 line_ptr
+= bytes_read
;
1145 table
->files
[table
->num_files
].time
=
1146 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1147 line_ptr
+= bytes_read
;
1148 table
->files
[table
->num_files
].size
=
1149 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1150 line_ptr
+= bytes_read
;
1154 line_ptr
+= bytes_read
;
1156 /* Read the statement sequences until there's nothing left. */
1157 while (line_ptr
< line_end
)
1159 /* State machine registers. */
1160 bfd_vma address
= 0;
1161 char * filename
= concat_filename (table
, 1);
1162 unsigned int line
= 1;
1163 unsigned int column
= 0;
1164 int is_stmt
= lh
.default_is_stmt
;
1165 int basic_block
= 0;
1166 int end_sequence
= 0;
1167 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1168 compilers generate address sequences that are wildly out of
1169 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1170 for ia64-Linux). Thus, to determine the low and high
1171 address, we must compare on every DW_LNS_copy, etc. */
1173 bfd_vma high_pc
= 0;
1175 /* Decode the table. */
1176 while (! end_sequence
)
1178 op_code
= read_1_byte (abfd
, line_ptr
);
1181 if (op_code
>= lh
.opcode_base
)
1183 /* Special operand. */
1184 adj_opcode
= op_code
- lh
.opcode_base
;
1185 address
+= (adj_opcode
/ lh
.line_range
)
1186 * lh
.minimum_instruction_length
;
1187 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
1188 /* Append row to matrix using current values. */
1189 add_line_info (table
, address
, filename
, line
, column
, 0);
1191 if (low_pc
== 0 || address
< low_pc
)
1193 if (address
> high_pc
)
1196 else switch (op_code
)
1198 case DW_LNS_extended_op
:
1199 /* Ignore length. */
1201 extended_op
= read_1_byte (abfd
, line_ptr
);
1204 switch (extended_op
)
1206 case DW_LNE_end_sequence
:
1208 add_line_info (table
, address
, filename
, line
, column
,
1210 if (low_pc
== 0 || address
< low_pc
)
1212 if (address
> high_pc
)
1214 arange_add (unit
, low_pc
, high_pc
);
1216 case DW_LNE_set_address
:
1217 address
= read_address (unit
, line_ptr
);
1218 line_ptr
+= unit
->addr_size
;
1220 case DW_LNE_define_file
:
1221 cur_file
= read_string (abfd
, line_ptr
, &bytes_read
);
1222 line_ptr
+= bytes_read
;
1223 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1225 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1226 amt
*= sizeof (struct fileinfo
);
1228 (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1232 table
->files
[table
->num_files
].name
= cur_file
;
1233 table
->files
[table
->num_files
].dir
=
1234 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1235 line_ptr
+= bytes_read
;
1236 table
->files
[table
->num_files
].time
=
1237 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1238 line_ptr
+= bytes_read
;
1239 table
->files
[table
->num_files
].size
=
1240 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1241 line_ptr
+= bytes_read
;
1245 (*_bfd_error_handler
) (_("Dwarf Error: mangled line number section."));
1246 bfd_set_error (bfd_error_bad_value
);
1251 add_line_info (table
, address
, filename
, line
, column
, 0);
1253 if (low_pc
== 0 || address
< low_pc
)
1255 if (address
> high_pc
)
1258 case DW_LNS_advance_pc
:
1259 address
+= lh
.minimum_instruction_length
1260 * read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1261 line_ptr
+= bytes_read
;
1263 case DW_LNS_advance_line
:
1264 line
+= read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
1265 line_ptr
+= bytes_read
;
1267 case DW_LNS_set_file
:
1271 /* The file and directory tables are 0
1272 based, the references are 1 based. */
1273 file
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1274 line_ptr
+= bytes_read
;
1275 filename
= concat_filename (table
, file
);
1278 case DW_LNS_set_column
:
1279 column
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1280 line_ptr
+= bytes_read
;
1282 case DW_LNS_negate_stmt
:
1283 is_stmt
= (!is_stmt
);
1285 case DW_LNS_set_basic_block
:
1288 case DW_LNS_const_add_pc
:
1289 address
+= lh
.minimum_instruction_length
1290 * ((255 - lh
.opcode_base
) / lh
.line_range
);
1292 case DW_LNS_fixed_advance_pc
:
1293 address
+= read_2_bytes (abfd
, line_ptr
);
1299 /* Unknown standard opcode, ignore it. */
1300 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
1302 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1303 line_ptr
+= bytes_read
;
1313 /* If ADDR is within TABLE set the output parameters and return TRUE,
1314 otherwise return FALSE. The output parameters, FILENAME_PTR and
1315 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1318 lookup_address_in_line_info_table (table
, addr
, function
, filename_ptr
,
1320 struct line_info_table
* table
;
1322 struct funcinfo
*function
;
1323 const char **filename_ptr
;
1324 unsigned int *linenumber_ptr
;
1326 /* Note: table->last_line should be a descendingly sorted list. */
1327 struct line_info
* next_line
= table
->last_line
;
1328 struct line_info
* each_line
= NULL
;
1329 *filename_ptr
= NULL
;
1334 each_line
= next_line
->prev_line
;
1336 /* Check for large addresses */
1337 if (addr
> next_line
->address
)
1338 each_line
= NULL
; /* ensure we skip over the normal case */
1340 /* Normal case: search the list; save */
1341 while (each_line
&& next_line
)
1343 /* If we have an address match, save this info. This allows us
1344 to return as good as results as possible for strange debugging
1346 bfd_boolean addr_match
= FALSE
;
1347 if (each_line
->address
<= addr
&& addr
<= next_line
->address
)
1351 /* If this line appears to span functions, and addr is in the
1352 later function, return the first line of that function instead
1353 of the last line of the earlier one. This check is for GCC
1354 2.95, which emits the first line number for a function late. */
1355 if (function
!= NULL
1356 && each_line
->address
< function
->low
1357 && next_line
->address
> function
->low
)
1359 *filename_ptr
= next_line
->filename
;
1360 *linenumber_ptr
= next_line
->line
;
1364 *filename_ptr
= each_line
->filename
;
1365 *linenumber_ptr
= each_line
->line
;
1369 if (addr_match
&& !each_line
->end_sequence
)
1370 return TRUE
; /* we have definitely found what we want */
1372 next_line
= each_line
;
1373 each_line
= each_line
->prev_line
;
1376 /* At this point each_line is NULL but next_line is not. If we found
1377 a candidate end-of-sequence point in the loop above, we can return
1378 that (compatibility with a bug in the Intel compiler); otherwise,
1379 assuming that we found the containing function for this address in
1380 this compilation unit, return the first line we have a number for
1381 (compatibility with GCC 2.95). */
1382 if (*filename_ptr
== NULL
&& function
!= NULL
)
1384 *filename_ptr
= next_line
->filename
;
1385 *linenumber_ptr
= next_line
->line
;
1392 /* Function table functions. */
1394 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE. */
1397 lookup_address_in_function_table (table
, addr
, function_ptr
,
1399 struct funcinfo
* table
;
1401 struct funcinfo
** function_ptr
;
1402 const char **functionname_ptr
;
1404 struct funcinfo
* each_func
;
1406 for (each_func
= table
;
1408 each_func
= each_func
->prev_func
)
1410 if (addr
>= each_func
->low
&& addr
< each_func
->high
)
1412 *functionname_ptr
= each_func
->name
;
1413 *function_ptr
= each_func
;
1421 /* DWARF2 Compilation unit functions. */
1423 /* Scan over each die in a comp. unit looking for functions to add
1424 to the function table. */
1427 scan_unit_for_functions (unit
)
1428 struct comp_unit
*unit
;
1430 bfd
*abfd
= unit
->abfd
;
1431 char *info_ptr
= unit
->first_child_die_ptr
;
1432 int nesting_level
= 1;
1434 while (nesting_level
)
1436 unsigned int abbrev_number
, bytes_read
, i
;
1437 struct abbrev_info
*abbrev
;
1438 struct attribute attr
;
1439 struct funcinfo
*func
;
1442 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1443 info_ptr
+= bytes_read
;
1445 if (! abbrev_number
)
1451 abbrev
= lookup_abbrev (abbrev_number
,unit
->abbrevs
);
1454 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
1456 bfd_set_error (bfd_error_bad_value
);
1460 if (abbrev
->tag
== DW_TAG_subprogram
)
1462 bfd_size_type amt
= sizeof (struct funcinfo
);
1463 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
1464 func
->prev_func
= unit
->function_table
;
1465 unit
->function_table
= func
;
1470 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1472 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1480 name
= DW_STRING (&attr
);
1482 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1483 if (func
->name
== NULL
)
1484 func
->name
= DW_STRING (&attr
);
1487 case DW_AT_MIPS_linkage_name
:
1488 func
->name
= DW_STRING (&attr
);
1492 func
->low
= DW_ADDR (&attr
);
1496 func
->high
= DW_ADDR (&attr
);
1508 name
= DW_STRING (&attr
);
1517 if (abbrev
->has_children
)
1524 /* Look for a RELA relocation to be applied on OFFSET of section SEC,
1525 and return the addend if such a relocation is found. Since this is
1526 only used to find relocations referring to the .debug_abbrev
1527 section, we make sure the relocation refers to this section, but
1528 this is not strictly necessary, and it can probably be safely
1529 removed if needed. However, it is important to note that this
1530 function only returns the addend, it doesn't serve the purpose of
1531 applying a generic relocation.
1533 If no suitable relocation is found, or if it is not a real RELA
1534 relocation, this function returns 0. */
1537 find_rela_addend (abfd
, sec
, offset
, syms
)
1540 bfd_size_type offset
;
1543 long reloc_size
= bfd_get_reloc_upper_bound (abfd
, sec
);
1544 arelent
**relocs
= NULL
;
1545 long reloc_count
, relc
;
1547 if (reloc_size
<= 0)
1550 relocs
= (arelent
**) bfd_malloc ((bfd_size_type
) reloc_size
);
1554 reloc_count
= bfd_canonicalize_reloc (abfd
, sec
, relocs
, syms
);
1556 if (reloc_count
<= 0)
1562 for (relc
= 0; relc
< reloc_count
; relc
++)
1563 if (relocs
[relc
]->address
== offset
1564 && (*relocs
[relc
]->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
1565 && strcmp ((*relocs
[relc
]->sym_ptr_ptr
)->name
,
1566 ".debug_abbrev") == 0)
1568 bfd_vma addend
= (relocs
[relc
]->howto
->partial_inplace
1569 ? 0 : relocs
[relc
]->addend
);
1578 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1579 includes the compilation unit header that proceeds the DIE's, but
1580 does not include the length field that preceeds each compilation
1581 unit header. END_PTR points one past the end of this comp unit.
1582 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1584 This routine does not read the whole compilation unit; only enough
1585 to get to the line number information for the compilation unit. */
1587 static struct comp_unit
*
1588 parse_comp_unit (abfd
, stash
, unit_length
, offset_size
)
1590 struct dwarf2_debug
*stash
;
1591 bfd_vma unit_length
;
1592 unsigned int offset_size
;
1594 struct comp_unit
* unit
;
1595 unsigned int version
;
1596 bfd_vma abbrev_offset
= 0;
1597 unsigned int addr_size
;
1598 struct abbrev_info
** abbrevs
;
1599 unsigned int abbrev_number
, bytes_read
, i
;
1600 struct abbrev_info
*abbrev
;
1601 struct attribute attr
;
1602 char *info_ptr
= stash
->info_ptr
;
1603 char *end_ptr
= info_ptr
+ unit_length
;
1607 version
= read_2_bytes (abfd
, info_ptr
);
1609 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
1610 if (offset_size
== 4)
1611 abbrev_offset
= read_4_bytes (abfd
, info_ptr
);
1613 abbrev_offset
= read_8_bytes (abfd
, info_ptr
);
1614 /* The abbrev offset is generally a relocation pointing to
1615 .debug_abbrev+offset. On RELA targets, we have to find the
1616 relocation and extract the addend to obtain the actual
1617 abbrev_offset, so do it here. */
1618 off
= info_ptr
- stash
->sec_info_ptr
;
1619 abbrev_offset
+= find_rela_addend (abfd
, stash
->sec
, off
, stash
->syms
);
1620 info_ptr
+= offset_size
;
1621 addr_size
= read_1_byte (abfd
, info_ptr
);
1626 (*_bfd_error_handler
) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version
);
1627 bfd_set_error (bfd_error_bad_value
);
1631 if (addr_size
> sizeof (bfd_vma
))
1633 (*_bfd_error_handler
) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1635 (unsigned int) sizeof (bfd_vma
));
1636 bfd_set_error (bfd_error_bad_value
);
1640 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
1642 (*_bfd_error_handler
) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size
);
1643 bfd_set_error (bfd_error_bad_value
);
1647 /* Read the abbrevs for this compilation unit into a table. */
1648 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
);
1652 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1653 info_ptr
+= bytes_read
;
1654 if (! abbrev_number
)
1656 (*_bfd_error_handler
) (_("Dwarf Error: Bad abbrev number: %u."),
1658 bfd_set_error (bfd_error_bad_value
);
1662 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
1665 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
1667 bfd_set_error (bfd_error_bad_value
);
1671 amt
= sizeof (struct comp_unit
);
1672 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
1674 unit
->addr_size
= addr_size
;
1675 unit
->offset_size
= offset_size
;
1676 unit
->abbrevs
= abbrevs
;
1677 unit
->end_ptr
= end_ptr
;
1678 unit
->stash
= stash
;
1680 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1682 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1684 /* Store the data if it is of an attribute we want to keep in a
1685 partial symbol table. */
1688 case DW_AT_stmt_list
:
1690 unit
->line_offset
= DW_UNSND (&attr
);
1694 unit
->name
= DW_STRING (&attr
);
1698 unit
->arange
.low
= DW_ADDR (&attr
);
1702 unit
->arange
.high
= DW_ADDR (&attr
);
1705 case DW_AT_comp_dir
:
1707 char* comp_dir
= DW_STRING (&attr
);
1710 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1711 directory, get rid of it. */
1712 char *cp
= (char*) strchr (comp_dir
, ':');
1714 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
1717 unit
->comp_dir
= comp_dir
;
1726 unit
->first_child_die_ptr
= info_ptr
;
1730 /* Return TRUE if UNIT contains the address given by ADDR. */
1733 comp_unit_contains_address (unit
, addr
)
1734 struct comp_unit
* unit
;
1737 struct arange
*arange
;
1742 arange
= &unit
->arange
;
1745 if (addr
>= arange
->low
&& addr
< arange
->high
)
1747 arange
= arange
->next
;
1754 /* If UNIT contains ADDR, set the output parameters to the values for
1755 the line containing ADDR. The output parameters, FILENAME_PTR,
1756 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1759 Return TRUE of UNIT contains ADDR, and no errors were encountered;
1763 comp_unit_find_nearest_line (unit
, addr
, filename_ptr
, functionname_ptr
,
1764 linenumber_ptr
, stash
)
1765 struct comp_unit
* unit
;
1767 const char **filename_ptr
;
1768 const char **functionname_ptr
;
1769 unsigned int *linenumber_ptr
;
1770 struct dwarf2_debug
*stash
;
1774 struct funcinfo
*function
;
1779 if (! unit
->line_table
)
1781 if (! unit
->stmtlist
)
1787 unit
->line_table
= decode_line_info (unit
, stash
);
1789 if (! unit
->line_table
)
1795 if (unit
->first_child_die_ptr
< unit
->end_ptr
1796 && ! scan_unit_for_functions (unit
))
1804 func_p
= lookup_address_in_function_table (unit
->function_table
, addr
,
1805 &function
, functionname_ptr
);
1806 line_p
= lookup_address_in_line_info_table (unit
->line_table
, addr
,
1807 function
, filename_ptr
,
1809 return line_p
|| func_p
;
1812 /* Locate a section in a BFD containing debugging info. The search starts
1813 from the section after AFTER_SEC, or from the first section in the BFD if
1814 AFTER_SEC is NULL. The search works by examining the names of the
1815 sections. There are two permissiable names. The first is .debug_info.
1816 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
1817 This is a variation on the .debug_info section which has a checksum
1818 describing the contents appended onto the name. This allows the linker to
1819 identify and discard duplicate debugging sections for different
1820 compilation units. */
1821 #define DWARF2_DEBUG_INFO ".debug_info"
1822 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1825 find_debug_info (abfd
, after_sec
)
1827 asection
* after_sec
;
1832 msec
= after_sec
->next
;
1834 msec
= abfd
->sections
;
1838 if (strcmp (msec
->name
, DWARF2_DEBUG_INFO
) == 0)
1841 if (strncmp (msec
->name
, GNU_LINKONCE_INFO
, strlen (GNU_LINKONCE_INFO
)) == 0)
1850 /* The DWARF2 version of find_nearest line. Return TRUE if the line
1851 is found without error. ADDR_SIZE is the number of bytes in the
1852 initial .debug_info length field and in the abbreviation offset.
1853 You may use zero to indicate that the default value should be
1857 _bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
1858 filename_ptr
, functionname_ptr
,
1859 linenumber_ptr
, addr_size
, pinfo
)
1864 const char **filename_ptr
;
1865 const char **functionname_ptr
;
1866 unsigned int *linenumber_ptr
;
1867 unsigned int addr_size
;
1870 /* Read each compilation unit from the section .debug_info, and check
1871 to see if it contains the address we are searching for. If yes,
1872 lookup the address, and return the line number info. If no, go
1873 on to the next compilation unit.
1875 We keep a list of all the previously read compilation units, and
1876 a pointer to the next un-read compilation unit. Check the
1877 previously read units before reading more. */
1878 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
1880 /* What address are we looking for? */
1881 bfd_vma addr
= offset
+ section
->vma
;
1883 struct comp_unit
* each
;
1885 *filename_ptr
= NULL
;
1886 *functionname_ptr
= NULL
;
1887 *linenumber_ptr
= 0;
1889 /* The DWARF2 spec says that the initial length field, and the
1890 offset of the abbreviation table, should both be 4-byte values.
1891 However, some compilers do things differently. */
1894 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
1898 bfd_size_type total_size
;
1900 bfd_size_type amt
= sizeof (struct dwarf2_debug
);
1902 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
1906 *pinfo
= (PTR
) stash
;
1908 msec
= find_debug_info (abfd
, NULL
);
1910 /* No dwarf2 info. Note that at this point the stash
1911 has been allocated, but contains zeros, this lets
1912 future calls to this function fail quicker. */
1915 /* There can be more than one DWARF2 info section in a BFD these days.
1916 Read them all in and produce one large stash. We do this in two
1917 passes - in the first pass we just accumulate the section sizes.
1918 In the second pass we read in the section's contents. The allows
1919 us to avoid reallocing the data as we add sections to the stash. */
1920 for (total_size
= 0; msec
; msec
= find_debug_info (abfd
, msec
))
1921 total_size
+= msec
->_raw_size
;
1923 stash
->info_ptr
= (char *) bfd_alloc (abfd
, total_size
);
1924 if (stash
->info_ptr
== NULL
)
1927 stash
->info_ptr_end
= stash
->info_ptr
;
1929 for (msec
= find_debug_info (abfd
, NULL
);
1931 msec
= find_debug_info (abfd
, msec
))
1934 bfd_size_type start
;
1936 size
= msec
->_raw_size
;
1940 start
= stash
->info_ptr_end
- stash
->info_ptr
;
1942 if (! bfd_get_section_contents (abfd
, msec
, stash
->info_ptr
+ start
,
1946 stash
->info_ptr_end
= stash
->info_ptr
+ start
+ size
;
1949 BFD_ASSERT (stash
->info_ptr_end
== stash
->info_ptr
+ total_size
);
1951 stash
->sec
= find_debug_info (abfd
, NULL
);
1952 stash
->sec_info_ptr
= stash
->info_ptr
;
1953 stash
->syms
= symbols
;
1956 /* FIXME: There is a problem with the contents of the
1957 .debug_info section. The 'low' and 'high' addresses of the
1958 comp_units are computed by relocs against symbols in the
1959 .text segment. We need these addresses in order to determine
1960 the nearest line number, and so we have to resolve the
1961 relocs. There is a similar problem when the .debug_line
1962 section is processed as well (e.g., there may be relocs
1963 against the operand of the DW_LNE_set_address operator).
1965 Unfortunately getting hold of the reloc information is hard...
1967 For now, this means that disassembling object files (as
1968 opposed to fully executables) does not always work as well as
1971 /* A null info_ptr indicates that there is no dwarf2 info
1972 (or that an error occured while setting up the stash). */
1973 if (! stash
->info_ptr
)
1976 /* Check the previously read comp. units first. */
1977 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
1978 if (comp_unit_contains_address (each
, addr
))
1979 return comp_unit_find_nearest_line (each
, addr
, filename_ptr
,
1980 functionname_ptr
, linenumber_ptr
,
1983 /* Read each remaining comp. units checking each as they are read. */
1984 while (stash
->info_ptr
< stash
->info_ptr_end
)
1988 unsigned int offset_size
= addr_size
;
1992 length
= read_4_bytes (abfd
, stash
->info_ptr
);
1993 if (length
== 0xffffffff)
1996 length
= read_8_bytes (abfd
, stash
->info_ptr
+ 4);
1997 stash
->info_ptr
+= 8;
1999 else if (length
== 0)
2001 /* Handle (non-standard) 64-bit DWARF2 formats. */
2003 length
= read_4_bytes (abfd
, stash
->info_ptr
+ 4);
2004 stash
->info_ptr
+= 4;
2008 length
= read_8_bytes (abfd
, stash
->info_ptr
);
2009 stash
->info_ptr
+= addr_size
;
2013 each
= parse_comp_unit (abfd
, stash
, length
, offset_size
);
2014 stash
->info_ptr
+= length
;
2016 if ((bfd_vma
) (stash
->info_ptr
- stash
->sec_info_ptr
)
2017 == stash
->sec
->_raw_size
)
2019 stash
->sec
= find_debug_info (abfd
, stash
->sec
);
2020 stash
->sec_info_ptr
= stash
->info_ptr
;
2025 each
->next_unit
= stash
->all_comp_units
;
2026 stash
->all_comp_units
= each
;
2028 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2029 compilation units. If we don't have them (i.e.,
2030 unit->high == 0), we need to consult the line info
2031 table to see if a compilation unit contains the given
2033 if (each
->arange
.high
> 0)
2035 if (comp_unit_contains_address (each
, addr
))
2036 return comp_unit_find_nearest_line (each
, addr
,
2044 found
= comp_unit_find_nearest_line (each
, addr
,