2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004 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
;
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 /* Preserve the original value of info_ptr for the current
88 comp_unit so we can find a given entry by its reference. */
91 /* Pointer to the end of the .debug_info section memory buffer. */
94 /* Pointer to the section and address of the beginning of the
99 /* Pointer to the symbol table. */
102 /* Pointer to the .debug_abbrev section loaded into memory. */
103 char* dwarf_abbrev_buffer
;
105 /* Length of the loaded .debug_abbrev section. */
106 unsigned long dwarf_abbrev_size
;
108 /* Buffer for decode_line_info. */
109 char *dwarf_line_buffer
;
111 /* Length of the loaded .debug_line section. */
112 unsigned long dwarf_line_size
;
114 /* Pointer to the .debug_str section loaded into memory. */
115 char* dwarf_str_buffer
;
117 /* Length of the loaded .debug_str section. */
118 unsigned long dwarf_str_size
;
128 /* A minimal decoding of DWARF2 compilation units. We only decode
129 what's needed to get to the line number information. */
133 /* Chain the previously read compilation units. */
134 struct comp_unit
* next_unit
;
136 /* Keep the bdf convenient (for memory allocation). */
139 /* The lowest and higest addresses contained in this compilation
140 unit as specified in the compilation unit header. */
141 struct arange arange
;
143 /* The DW_AT_name attribute (for error messages). */
146 /* The abbrev hash table. */
147 struct abbrev_info
** abbrevs
;
149 /* Note that an error was found by comp_unit_find_nearest_line. */
152 /* The DW_AT_comp_dir attribute. */
155 /* TRUE if there is a line number table associated with this comp. unit. */
158 /* The offset into .debug_line of the line number table. */
159 unsigned long line_offset
;
161 /* Pointer to the first child die for the comp unit. */
162 char *first_child_die_ptr
;
164 /* The end of the comp unit. */
167 /* The decoded line number, NULL if not yet decoded. */
168 struct line_info_table
* line_table
;
170 /* A list of the functions found in this comp. unit. */
171 struct funcinfo
* function_table
;
173 /* Pointer to dwarf2_debug structure. */
174 struct dwarf2_debug
*stash
;
176 /* Address size for this unit - from unit header. */
177 unsigned char addr_size
;
179 /* Offset size for this unit - from unit header. */
180 unsigned char offset_size
;
183 /* This data structure holds the information of an abbrev. */
186 unsigned int number
; /* Number identifying abbrev. */
187 enum dwarf_tag tag
; /* DWARF tag. */
188 int has_children
; /* Boolean. */
189 unsigned int num_attrs
; /* Number of attributes. */
190 struct attr_abbrev
*attrs
; /* An array of attribute descriptions. */
191 struct abbrev_info
*next
; /* Next in chain. */
196 enum dwarf_attribute name
;
197 enum dwarf_form form
;
200 #ifndef ABBREV_HASH_SIZE
201 #define ABBREV_HASH_SIZE 121
203 #ifndef ATTR_ALLOC_CHUNK
204 #define ATTR_ALLOC_CHUNK 4
208 The following function up to the END VERBATIM mark are
209 copied directly from dwarf2read.c. */
211 /* Read dwarf information from a buffer. */
214 read_1_byte (bfd
*abfd ATTRIBUTE_UNUSED
, char *buf
)
216 return bfd_get_8 (abfd
, buf
);
220 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, char *buf
)
222 return bfd_get_signed_8 (abfd
, buf
);
226 read_2_bytes (bfd
*abfd
, char *buf
)
228 return bfd_get_16 (abfd
, buf
);
232 read_4_bytes (bfd
*abfd
, char *buf
)
234 return bfd_get_32 (abfd
, buf
);
238 read_8_bytes (bfd
*abfd
, char *buf
)
240 return bfd_get_64 (abfd
, buf
);
244 read_n_bytes (bfd
*abfd ATTRIBUTE_UNUSED
,
246 unsigned int size ATTRIBUTE_UNUSED
)
248 /* If the size of a host char is 8 bits, we can return a pointer
249 to the buffer, otherwise we have to copy the data to a buffer
250 allocated on the temporary obstack. */
255 read_string (bfd
*abfd ATTRIBUTE_UNUSED
,
257 unsigned int *bytes_read_ptr
)
259 /* Return a pointer to the embedded string. */
266 *bytes_read_ptr
= strlen (buf
) + 1;
271 read_indirect_string (struct comp_unit
* unit
,
273 unsigned int *bytes_read_ptr
)
276 struct dwarf2_debug
*stash
= unit
->stash
;
278 if (unit
->offset_size
== 4)
279 offset
= read_4_bytes (unit
->abfd
, buf
);
281 offset
= read_8_bytes (unit
->abfd
, buf
);
282 *bytes_read_ptr
= unit
->offset_size
;
284 if (! stash
->dwarf_str_buffer
)
287 bfd
*abfd
= unit
->abfd
;
290 msec
= bfd_get_section_by_name (abfd
, ".debug_str");
293 (*_bfd_error_handler
)
294 (_("Dwarf Error: Can't find .debug_str section."));
295 bfd_set_error (bfd_error_bad_value
);
299 sz
= msec
->rawsize
? msec
->rawsize
: msec
->size
;
300 stash
->dwarf_str_size
= sz
;
301 stash
->dwarf_str_buffer
= bfd_alloc (abfd
, sz
);
302 if (! stash
->dwarf_abbrev_buffer
)
305 if (! bfd_get_section_contents (abfd
, msec
, stash
->dwarf_str_buffer
,
310 if (offset
>= stash
->dwarf_str_size
)
312 (*_bfd_error_handler
) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
313 (unsigned long) offset
, stash
->dwarf_str_size
);
314 bfd_set_error (bfd_error_bad_value
);
318 buf
= stash
->dwarf_str_buffer
+ offset
;
325 read_unsigned_leb128 (bfd
*abfd ATTRIBUTE_UNUSED
,
327 unsigned int *bytes_read_ptr
)
330 unsigned int num_read
;
340 byte
= bfd_get_8 (abfd
, buf
);
343 result
|= ((byte
& 0x7f) << shift
);
348 * bytes_read_ptr
= num_read
;
354 read_signed_leb128 (bfd
*abfd ATTRIBUTE_UNUSED
,
356 unsigned int * bytes_read_ptr
)
369 byte
= bfd_get_8 (abfd
, buf
);
372 result
|= ((byte
& 0x7f) << shift
);
377 if ((shift
< 32) && (byte
& 0x40))
378 result
|= -(1 << shift
);
380 * bytes_read_ptr
= num_read
;
388 read_address (struct comp_unit
*unit
, char *buf
)
390 switch (unit
->addr_size
)
393 return bfd_get_64 (unit
->abfd
, buf
);
395 return bfd_get_32 (unit
->abfd
, buf
);
397 return bfd_get_16 (unit
->abfd
, buf
);
403 /* Lookup an abbrev_info structure in the abbrev hash table. */
405 static struct abbrev_info
*
406 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
408 unsigned int hash_number
;
409 struct abbrev_info
*abbrev
;
411 hash_number
= number
% ABBREV_HASH_SIZE
;
412 abbrev
= abbrevs
[hash_number
];
416 if (abbrev
->number
== number
)
419 abbrev
= abbrev
->next
;
425 /* In DWARF version 2, the description of the debugging information is
426 stored in a separate .debug_abbrev section. Before we read any
427 dies from a section we read in all abbreviations and install them
430 static struct abbrev_info
**
431 read_abbrevs (bfd
*abfd
, bfd_uint64_t offset
, struct dwarf2_debug
*stash
)
433 struct abbrev_info
**abbrevs
;
435 struct abbrev_info
*cur_abbrev
;
436 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
437 unsigned int abbrev_form
, hash_number
;
440 if (! stash
->dwarf_abbrev_buffer
)
444 msec
= bfd_get_section_by_name (abfd
, ".debug_abbrev");
447 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_abbrev section."));
448 bfd_set_error (bfd_error_bad_value
);
452 stash
->dwarf_abbrev_size
= msec
->size
;
453 stash
->dwarf_abbrev_buffer
454 = bfd_simple_get_relocated_section_contents (abfd
, msec
, NULL
,
456 if (! stash
->dwarf_abbrev_buffer
)
460 if (offset
>= stash
->dwarf_abbrev_size
)
462 (*_bfd_error_handler
) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
463 (unsigned long) offset
, stash
->dwarf_abbrev_size
);
464 bfd_set_error (bfd_error_bad_value
);
468 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
469 abbrevs
= bfd_zalloc (abfd
, amt
);
471 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
472 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
473 abbrev_ptr
+= bytes_read
;
475 /* Loop until we reach an abbrev number of 0. */
476 while (abbrev_number
)
478 amt
= sizeof (struct abbrev_info
);
479 cur_abbrev
= bfd_zalloc (abfd
, amt
);
481 /* Read in abbrev header. */
482 cur_abbrev
->number
= abbrev_number
;
483 cur_abbrev
->tag
= (enum dwarf_tag
)
484 read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
485 abbrev_ptr
+= bytes_read
;
486 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
);
489 /* Now read in declarations. */
490 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
491 abbrev_ptr
+= bytes_read
;
492 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
493 abbrev_ptr
+= bytes_read
;
497 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
499 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
500 amt
*= sizeof (struct attr_abbrev
);
501 cur_abbrev
->attrs
= bfd_realloc (cur_abbrev
->attrs
, amt
);
502 if (! cur_abbrev
->attrs
)
506 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
507 = (enum dwarf_attribute
) abbrev_name
;
508 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
++].form
509 = (enum dwarf_form
) abbrev_form
;
510 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
511 abbrev_ptr
+= bytes_read
;
512 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
513 abbrev_ptr
+= bytes_read
;
516 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
517 cur_abbrev
->next
= abbrevs
[hash_number
];
518 abbrevs
[hash_number
] = cur_abbrev
;
520 /* Get next abbreviation.
521 Under Irix6 the abbreviations for a compilation unit are not
522 always properly terminated with an abbrev number of 0.
523 Exit loop if we encounter an abbreviation which we have
524 already read (which means we are about to read the abbreviations
525 for the next compile unit) or if the end of the abbreviation
527 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
528 >= stash
->dwarf_abbrev_size
)
530 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
531 abbrev_ptr
+= bytes_read
;
532 if (lookup_abbrev (abbrev_number
,abbrevs
) != NULL
)
539 /* Read an attribute value described by an attribute form. */
542 read_attribute_value (struct attribute
*attr
,
544 struct comp_unit
*unit
,
547 bfd
*abfd
= unit
->abfd
;
548 unsigned int bytes_read
;
549 struct dwarf_block
*blk
;
552 attr
->form
= (enum dwarf_form
) form
;
557 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
558 case DW_FORM_ref_addr
:
559 attr
->u
.val
= read_address (unit
, info_ptr
);
560 info_ptr
+= unit
->addr_size
;
563 amt
= sizeof (struct dwarf_block
);
564 blk
= bfd_alloc (abfd
, amt
);
565 blk
->size
= read_2_bytes (abfd
, info_ptr
);
567 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
568 info_ptr
+= blk
->size
;
572 amt
= sizeof (struct dwarf_block
);
573 blk
= bfd_alloc (abfd
, amt
);
574 blk
->size
= read_4_bytes (abfd
, info_ptr
);
576 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
577 info_ptr
+= blk
->size
;
581 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
);
585 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
);
589 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
593 attr
->u
.str
= read_string (abfd
, info_ptr
, &bytes_read
);
594 info_ptr
+= bytes_read
;
597 attr
->u
.str
= read_indirect_string (unit
, info_ptr
, &bytes_read
);
598 info_ptr
+= bytes_read
;
601 amt
= sizeof (struct dwarf_block
);
602 blk
= bfd_alloc (abfd
, amt
);
603 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
604 info_ptr
+= bytes_read
;
605 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
606 info_ptr
+= blk
->size
;
610 amt
= sizeof (struct dwarf_block
);
611 blk
= bfd_alloc (abfd
, amt
);
612 blk
->size
= read_1_byte (abfd
, info_ptr
);
614 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
615 info_ptr
+= blk
->size
;
619 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
623 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
627 attr
->u
.sval
= read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
628 info_ptr
+= bytes_read
;
631 attr
->u
.val
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
632 info_ptr
+= bytes_read
;
635 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
639 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
);
643 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
);
647 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
650 case DW_FORM_ref_udata
:
651 attr
->u
.val
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
652 info_ptr
+= bytes_read
;
654 case DW_FORM_indirect
:
655 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
656 info_ptr
+= bytes_read
;
657 info_ptr
= read_attribute_value (attr
, form
, unit
, info_ptr
);
660 (*_bfd_error_handler
) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
662 bfd_set_error (bfd_error_bad_value
);
667 /* Read an attribute described by an abbreviated attribute. */
670 read_attribute (struct attribute
*attr
,
671 struct attr_abbrev
*abbrev
,
672 struct comp_unit
*unit
,
675 attr
->name
= abbrev
->name
;
676 info_ptr
= read_attribute_value (attr
, abbrev
->form
, unit
, info_ptr
);
680 /* Source line information table routines. */
682 #define FILE_ALLOC_CHUNK 5
683 #define DIR_ALLOC_CHUNK 5
687 struct line_info
* prev_line
;
692 int end_sequence
; /* End of (sequential) code sequence. */
703 struct line_info_table
706 unsigned int num_files
;
707 unsigned int num_dirs
;
710 struct fileinfo
* files
;
711 struct line_info
* last_line
; /* largest VMA */
712 struct line_info
* lcl_head
; /* local head; used in 'add_line_info' */
717 struct funcinfo
*prev_func
;
723 /* Adds a new entry to the line_info list in the line_info_table, ensuring
724 that the list is sorted. Note that the line_info list is sorted from
725 highest to lowest VMA (with possible duplicates); that is,
726 line_info->prev_line always accesses an equal or smaller VMA. */
729 add_line_info (struct line_info_table
*table
,
736 bfd_size_type amt
= sizeof (struct line_info
);
737 struct line_info
* info
= bfd_alloc (table
->abfd
, amt
);
739 /* Find the correct location for 'info'. Normally we will receive
740 new line_info data 1) in order and 2) with increasing VMAs.
741 However some compilers break the rules (cf. decode_line_info) and
742 so we include some heuristics for quickly finding the correct
743 location for 'info'. In particular, these heuristics optimize for
744 the common case in which the VMA sequence that we receive is a
745 list of locally sorted VMAs such as
746 p...z a...j (where a < j < p < z)
748 Note: table->lcl_head is used to head an *actual* or *possible*
749 sequence within the list (such as a...j) that is not directly
750 headed by table->last_line
752 Note: we may receive duplicate entries from 'decode_line_info'. */
755 if (!table
->last_line
756 || address
>= table
->last_line
->address
)
758 /* Normal case: add 'info' to the beginning of the list */
759 info
->prev_line
= table
->last_line
;
760 table
->last_line
= info
;
762 /* lcl_head: initialize to head a *possible* sequence at the end. */
763 if (!table
->lcl_head
)
764 table
->lcl_head
= info
;
767 else if (!table
->lcl_head
->prev_line
768 && table
->lcl_head
->address
> address
)
770 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
771 list and 2) the head of 'info'. */
772 info
->prev_line
= NULL
;
773 table
->lcl_head
->prev_line
= info
;
776 else if (table
->lcl_head
->prev_line
777 && table
->lcl_head
->address
> address
778 && address
>= table
->lcl_head
->prev_line
->address
)
780 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
781 list and 2) the head of 'info'. */
782 info
->prev_line
= table
->lcl_head
->prev_line
;
783 table
->lcl_head
->prev_line
= info
;
788 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
789 heads for 'info'. Reset 'lcl_head' and repeat. */
790 struct line_info
* li2
= table
->last_line
; /* always non-NULL */
791 struct line_info
* li1
= li2
->prev_line
;
795 if (li2
->address
> address
&& address
>= li1
->address
)
798 li2
= li1
; /* always non-NULL */
799 li1
= li1
->prev_line
;
801 table
->lcl_head
= li2
;
804 /* Set member data of 'info'. */
805 info
->address
= address
;
807 info
->column
= column
;
808 info
->end_sequence
= end_sequence
;
810 if (filename
&& filename
[0])
812 info
->filename
= bfd_alloc (table
->abfd
, strlen (filename
) + 1);
814 strcpy (info
->filename
, filename
);
817 info
->filename
= NULL
;
820 /* Extract a fully qualified filename from a line info table.
821 The returned string has been malloc'ed and it is the caller's
822 responsibility to free it. */
825 concat_filename (struct line_info_table
*table
, unsigned int file
)
829 if (file
- 1 >= table
->num_files
)
831 (*_bfd_error_handler
)
832 (_("Dwarf Error: mangled line number section (bad file number)."));
833 return strdup ("<unknown>");
836 filename
= table
->files
[file
- 1].name
;
838 if (! IS_ABSOLUTE_PATH (filename
))
840 char* dirname
= (table
->files
[file
- 1].dir
841 ? table
->dirs
[table
->files
[file
- 1].dir
- 1]
844 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
845 The best we can do is return the filename part. */
848 unsigned int len
= strlen (dirname
) + strlen (filename
) + 2;
851 name
= bfd_malloc (len
);
853 sprintf (name
, "%s/%s", dirname
, filename
);
858 return strdup (filename
);
862 arange_add (struct comp_unit
*unit
, bfd_vma low_pc
, bfd_vma high_pc
)
864 struct arange
*arange
;
866 /* First see if we can cheaply extend an existing range. */
867 arange
= &unit
->arange
;
871 if (low_pc
== arange
->high
)
873 arange
->high
= high_pc
;
876 if (high_pc
== arange
->low
)
878 arange
->low
= low_pc
;
881 arange
= arange
->next
;
885 if (unit
->arange
.high
== 0)
887 /* This is the first address range: store it in unit->arange. */
888 unit
->arange
.next
= 0;
889 unit
->arange
.low
= low_pc
;
890 unit
->arange
.high
= high_pc
;
894 /* Need to allocate a new arange and insert it into the arange list. */
895 arange
= bfd_zalloc (unit
->abfd
, sizeof (*arange
));
896 arange
->low
= low_pc
;
897 arange
->high
= high_pc
;
899 arange
->next
= unit
->arange
.next
;
900 unit
->arange
.next
= arange
;
903 /* Decode the line number information for UNIT. */
905 static struct line_info_table
*
906 decode_line_info (struct comp_unit
*unit
, struct dwarf2_debug
*stash
)
908 bfd
*abfd
= unit
->abfd
;
909 struct line_info_table
* table
;
913 unsigned int i
, bytes_read
, offset_size
;
914 char *cur_file
, *cur_dir
;
915 unsigned char op_code
, extended_op
, adj_opcode
;
918 if (! stash
->dwarf_line_buffer
)
922 msec
= bfd_get_section_by_name (abfd
, ".debug_line");
925 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_line section."));
926 bfd_set_error (bfd_error_bad_value
);
930 stash
->dwarf_line_size
= msec
->size
;
931 stash
->dwarf_line_buffer
932 = bfd_simple_get_relocated_section_contents (abfd
, msec
, NULL
,
934 if (! stash
->dwarf_line_buffer
)
938 /* It is possible to get a bad value for the line_offset. Validate
939 it here so that we won't get a segfault below. */
940 if (unit
->line_offset
>= stash
->dwarf_line_size
)
942 (*_bfd_error_handler
) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
943 unit
->line_offset
, stash
->dwarf_line_size
);
944 bfd_set_error (bfd_error_bad_value
);
948 amt
= sizeof (struct line_info_table
);
949 table
= bfd_alloc (abfd
, amt
);
951 table
->comp_dir
= unit
->comp_dir
;
953 table
->num_files
= 0;
960 table
->last_line
= NULL
;
961 table
->lcl_head
= NULL
;
963 line_ptr
= stash
->dwarf_line_buffer
+ unit
->line_offset
;
965 /* Read in the prologue. */
966 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
969 if (lh
.total_length
== 0xffffffff)
971 lh
.total_length
= read_8_bytes (abfd
, line_ptr
);
975 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
977 /* Handle (non-standard) 64-bit DWARF2 formats. */
978 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
982 line_end
= line_ptr
+ lh
.total_length
;
983 lh
.version
= read_2_bytes (abfd
, line_ptr
);
985 if (offset_size
== 4)
986 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
);
988 lh
.prologue_length
= read_8_bytes (abfd
, line_ptr
);
989 line_ptr
+= offset_size
;
990 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
);
992 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
);
994 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
);
996 lh
.line_range
= read_1_byte (abfd
, line_ptr
);
998 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
);
1000 amt
= lh
.opcode_base
* sizeof (unsigned char);
1001 lh
.standard_opcode_lengths
= bfd_alloc (abfd
, amt
);
1003 lh
.standard_opcode_lengths
[0] = 1;
1005 for (i
= 1; i
< lh
.opcode_base
; ++i
)
1007 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
);
1011 /* Read directory table. */
1012 while ((cur_dir
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1014 line_ptr
+= bytes_read
;
1016 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1018 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1019 amt
*= sizeof (char *);
1020 table
->dirs
= bfd_realloc (table
->dirs
, amt
);
1025 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1028 line_ptr
+= bytes_read
;
1030 /* Read file name table. */
1031 while ((cur_file
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1033 line_ptr
+= bytes_read
;
1035 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1037 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1038 amt
*= sizeof (struct fileinfo
);
1039 table
->files
= bfd_realloc (table
->files
, amt
);
1044 table
->files
[table
->num_files
].name
= cur_file
;
1045 table
->files
[table
->num_files
].dir
=
1046 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1047 line_ptr
+= bytes_read
;
1048 table
->files
[table
->num_files
].time
=
1049 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1050 line_ptr
+= bytes_read
;
1051 table
->files
[table
->num_files
].size
=
1052 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1053 line_ptr
+= bytes_read
;
1057 line_ptr
+= bytes_read
;
1059 /* Read the statement sequences until there's nothing left. */
1060 while (line_ptr
< line_end
)
1062 /* State machine registers. */
1063 bfd_vma address
= 0;
1064 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
1065 unsigned int line
= 1;
1066 unsigned int column
= 0;
1067 int is_stmt
= lh
.default_is_stmt
;
1068 int basic_block
= 0;
1069 int end_sequence
= 0;
1070 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1071 compilers generate address sequences that are wildly out of
1072 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1073 for ia64-Linux). Thus, to determine the low and high
1074 address, we must compare on every DW_LNS_copy, etc. */
1076 bfd_vma high_pc
= 0;
1078 /* Decode the table. */
1079 while (! end_sequence
)
1081 op_code
= read_1_byte (abfd
, line_ptr
);
1084 if (op_code
>= lh
.opcode_base
)
1086 /* Special operand. */
1087 adj_opcode
= op_code
- lh
.opcode_base
;
1088 address
+= (adj_opcode
/ lh
.line_range
)
1089 * lh
.minimum_instruction_length
;
1090 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
1091 /* Append row to matrix using current values. */
1092 add_line_info (table
, address
, filename
, line
, column
, 0);
1094 if (low_pc
== 0 || address
< low_pc
)
1096 if (address
> high_pc
)
1099 else switch (op_code
)
1101 case DW_LNS_extended_op
:
1102 /* Ignore length. */
1104 extended_op
= read_1_byte (abfd
, line_ptr
);
1107 switch (extended_op
)
1109 case DW_LNE_end_sequence
:
1111 add_line_info (table
, address
, filename
, line
, column
,
1113 if (low_pc
== 0 || address
< low_pc
)
1115 if (address
> high_pc
)
1117 arange_add (unit
, low_pc
, high_pc
);
1119 case DW_LNE_set_address
:
1120 address
= read_address (unit
, line_ptr
);
1121 line_ptr
+= unit
->addr_size
;
1123 case DW_LNE_define_file
:
1124 cur_file
= read_string (abfd
, line_ptr
, &bytes_read
);
1125 line_ptr
+= bytes_read
;
1126 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1128 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1129 amt
*= sizeof (struct fileinfo
);
1130 table
->files
= bfd_realloc (table
->files
, amt
);
1134 table
->files
[table
->num_files
].name
= cur_file
;
1135 table
->files
[table
->num_files
].dir
=
1136 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1137 line_ptr
+= bytes_read
;
1138 table
->files
[table
->num_files
].time
=
1139 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1140 line_ptr
+= bytes_read
;
1141 table
->files
[table
->num_files
].size
=
1142 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1143 line_ptr
+= bytes_read
;
1147 (*_bfd_error_handler
) (_("Dwarf Error: mangled line number section."));
1148 bfd_set_error (bfd_error_bad_value
);
1153 add_line_info (table
, address
, filename
, line
, column
, 0);
1155 if (low_pc
== 0 || address
< low_pc
)
1157 if (address
> high_pc
)
1160 case DW_LNS_advance_pc
:
1161 address
+= lh
.minimum_instruction_length
1162 * read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1163 line_ptr
+= bytes_read
;
1165 case DW_LNS_advance_line
:
1166 line
+= read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
1167 line_ptr
+= bytes_read
;
1169 case DW_LNS_set_file
:
1173 /* The file and directory tables are 0
1174 based, the references are 1 based. */
1175 file
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1176 line_ptr
+= bytes_read
;
1179 filename
= concat_filename (table
, file
);
1182 case DW_LNS_set_column
:
1183 column
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1184 line_ptr
+= bytes_read
;
1186 case DW_LNS_negate_stmt
:
1187 is_stmt
= (!is_stmt
);
1189 case DW_LNS_set_basic_block
:
1192 case DW_LNS_const_add_pc
:
1193 address
+= lh
.minimum_instruction_length
1194 * ((255 - lh
.opcode_base
) / lh
.line_range
);
1196 case DW_LNS_fixed_advance_pc
:
1197 address
+= read_2_bytes (abfd
, line_ptr
);
1204 /* Unknown standard opcode, ignore it. */
1205 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
1207 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1208 line_ptr
+= bytes_read
;
1221 /* If ADDR is within TABLE set the output parameters and return TRUE,
1222 otherwise return FALSE. The output parameters, FILENAME_PTR and
1223 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1226 lookup_address_in_line_info_table (struct line_info_table
*table
,
1228 struct funcinfo
*function
,
1229 const char **filename_ptr
,
1230 unsigned int *linenumber_ptr
)
1232 /* Note: table->last_line should be a descendingly sorted list. */
1233 struct line_info
* next_line
= table
->last_line
;
1234 struct line_info
* each_line
= NULL
;
1235 *filename_ptr
= NULL
;
1240 each_line
= next_line
->prev_line
;
1242 /* Check for large addresses */
1243 if (addr
> next_line
->address
)
1244 each_line
= NULL
; /* ensure we skip over the normal case */
1246 /* Normal case: search the list; save */
1247 while (each_line
&& next_line
)
1249 /* If we have an address match, save this info. This allows us
1250 to return as good as results as possible for strange debugging
1252 bfd_boolean addr_match
= FALSE
;
1253 if (each_line
->address
<= addr
&& addr
<= next_line
->address
)
1257 /* If this line appears to span functions, and addr is in the
1258 later function, return the first line of that function instead
1259 of the last line of the earlier one. This check is for GCC
1260 2.95, which emits the first line number for a function late. */
1261 if (function
!= NULL
1262 && each_line
->address
< function
->low
1263 && next_line
->address
> function
->low
)
1265 *filename_ptr
= next_line
->filename
;
1266 *linenumber_ptr
= next_line
->line
;
1270 *filename_ptr
= each_line
->filename
;
1271 *linenumber_ptr
= each_line
->line
;
1275 if (addr_match
&& !each_line
->end_sequence
)
1276 return TRUE
; /* we have definitely found what we want */
1278 next_line
= each_line
;
1279 each_line
= each_line
->prev_line
;
1282 /* At this point each_line is NULL but next_line is not. If we found
1283 a candidate end-of-sequence point in the loop above, we can return
1284 that (compatibility with a bug in the Intel compiler); otherwise,
1285 assuming that we found the containing function for this address in
1286 this compilation unit, return the first line we have a number for
1287 (compatibility with GCC 2.95). */
1288 if (*filename_ptr
== NULL
&& function
!= NULL
)
1290 *filename_ptr
= next_line
->filename
;
1291 *linenumber_ptr
= next_line
->line
;
1298 /* Function table functions. */
1300 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE. */
1303 lookup_address_in_function_table (struct funcinfo
*table
,
1305 struct funcinfo
**function_ptr
,
1306 const char **functionname_ptr
)
1308 struct funcinfo
* each_func
;
1310 for (each_func
= table
;
1312 each_func
= each_func
->prev_func
)
1314 if (addr
>= each_func
->low
&& addr
< each_func
->high
)
1316 *functionname_ptr
= each_func
->name
;
1317 *function_ptr
= each_func
;
1326 find_abstract_instance_name (struct comp_unit
*unit
, bfd_uint64_t die_ref
)
1328 bfd
*abfd
= unit
->abfd
;
1330 unsigned int abbrev_number
, bytes_read
, i
;
1331 struct abbrev_info
*abbrev
;
1332 struct attribute attr
;
1335 info_ptr
= unit
->stash
->info_ptr_unit
+ die_ref
;
1336 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1337 info_ptr
+= bytes_read
;
1341 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
1344 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
1346 bfd_set_error (bfd_error_bad_value
);
1350 for (i
= 0; i
< abbrev
->num_attrs
&& !name
; ++i
)
1352 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1353 if (attr
.name
== DW_AT_name
)
1361 /* DWARF2 Compilation unit functions. */
1363 /* Scan over each die in a comp. unit looking for functions to add
1364 to the function table. */
1367 scan_unit_for_functions (struct comp_unit
*unit
)
1369 bfd
*abfd
= unit
->abfd
;
1370 char *info_ptr
= unit
->first_child_die_ptr
;
1371 int nesting_level
= 1;
1373 while (nesting_level
)
1375 unsigned int abbrev_number
, bytes_read
, i
;
1376 struct abbrev_info
*abbrev
;
1377 struct attribute attr
;
1378 struct funcinfo
*func
;
1381 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1382 info_ptr
+= bytes_read
;
1384 if (! abbrev_number
)
1390 abbrev
= lookup_abbrev (abbrev_number
,unit
->abbrevs
);
1393 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
1395 bfd_set_error (bfd_error_bad_value
);
1399 if (abbrev
->tag
== DW_TAG_subprogram
1400 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
1402 bfd_size_type amt
= sizeof (struct funcinfo
);
1403 func
= bfd_zalloc (abfd
, amt
);
1404 func
->prev_func
= unit
->function_table
;
1405 unit
->function_table
= func
;
1410 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1412 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1418 case DW_AT_abstract_origin
:
1419 func
->name
= find_abstract_instance_name (unit
, attr
.u
.val
);
1426 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1427 if (func
->name
== NULL
)
1428 func
->name
= attr
.u
.str
;
1431 case DW_AT_MIPS_linkage_name
:
1432 func
->name
= attr
.u
.str
;
1436 func
->low
= attr
.u
.val
;
1440 func
->high
= attr
.u
.val
;
1461 if (abbrev
->has_children
)
1468 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1469 includes the compilation unit header that proceeds the DIE's, but
1470 does not include the length field that precedes each compilation
1471 unit header. END_PTR points one past the end of this comp unit.
1472 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1474 This routine does not read the whole compilation unit; only enough
1475 to get to the line number information for the compilation unit. */
1477 static struct comp_unit
*
1478 parse_comp_unit (bfd
*abfd
,
1479 struct dwarf2_debug
*stash
,
1480 bfd_vma unit_length
,
1481 unsigned int offset_size
)
1483 struct comp_unit
* unit
;
1484 unsigned int version
;
1485 bfd_uint64_t abbrev_offset
= 0;
1486 unsigned int addr_size
;
1487 struct abbrev_info
** abbrevs
;
1488 unsigned int abbrev_number
, bytes_read
, i
;
1489 struct abbrev_info
*abbrev
;
1490 struct attribute attr
;
1491 char *info_ptr
= stash
->info_ptr
;
1492 char *end_ptr
= info_ptr
+ unit_length
;
1495 version
= read_2_bytes (abfd
, info_ptr
);
1497 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
1498 if (offset_size
== 4)
1499 abbrev_offset
= read_4_bytes (abfd
, info_ptr
);
1501 abbrev_offset
= read_8_bytes (abfd
, info_ptr
);
1502 info_ptr
+= offset_size
;
1503 addr_size
= read_1_byte (abfd
, info_ptr
);
1508 (*_bfd_error_handler
) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version
);
1509 bfd_set_error (bfd_error_bad_value
);
1513 if (addr_size
> sizeof (bfd_vma
))
1515 (*_bfd_error_handler
) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1517 (unsigned int) sizeof (bfd_vma
));
1518 bfd_set_error (bfd_error_bad_value
);
1522 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
1524 (*_bfd_error_handler
) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size
);
1525 bfd_set_error (bfd_error_bad_value
);
1529 /* Read the abbrevs for this compilation unit into a table. */
1530 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
);
1534 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1535 info_ptr
+= bytes_read
;
1536 if (! abbrev_number
)
1538 (*_bfd_error_handler
) (_("Dwarf Error: Bad abbrev number: %u."),
1540 bfd_set_error (bfd_error_bad_value
);
1544 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
1547 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
1549 bfd_set_error (bfd_error_bad_value
);
1553 amt
= sizeof (struct comp_unit
);
1554 unit
= bfd_zalloc (abfd
, amt
);
1556 unit
->addr_size
= addr_size
;
1557 unit
->offset_size
= offset_size
;
1558 unit
->abbrevs
= abbrevs
;
1559 unit
->end_ptr
= end_ptr
;
1560 unit
->stash
= stash
;
1562 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1564 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1566 /* Store the data if it is of an attribute we want to keep in a
1567 partial symbol table. */
1570 case DW_AT_stmt_list
:
1572 unit
->line_offset
= attr
.u
.val
;
1576 unit
->name
= attr
.u
.str
;
1580 unit
->arange
.low
= attr
.u
.val
;
1584 unit
->arange
.high
= attr
.u
.val
;
1587 case DW_AT_comp_dir
:
1589 char* comp_dir
= attr
.u
.str
;
1592 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1593 directory, get rid of it. */
1594 char *cp
= strchr (comp_dir
, ':');
1596 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
1599 unit
->comp_dir
= comp_dir
;
1608 unit
->first_child_die_ptr
= info_ptr
;
1612 /* Return TRUE if UNIT contains the address given by ADDR. */
1615 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
1617 struct arange
*arange
;
1622 arange
= &unit
->arange
;
1625 if (addr
>= arange
->low
&& addr
< arange
->high
)
1627 arange
= arange
->next
;
1634 /* If UNIT contains ADDR, set the output parameters to the values for
1635 the line containing ADDR. The output parameters, FILENAME_PTR,
1636 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1639 Return TRUE if UNIT contains ADDR, and no errors were encountered;
1643 comp_unit_find_nearest_line (struct comp_unit
*unit
,
1645 const char **filename_ptr
,
1646 const char **functionname_ptr
,
1647 unsigned int *linenumber_ptr
,
1648 struct dwarf2_debug
*stash
)
1652 struct funcinfo
*function
;
1657 if (! unit
->line_table
)
1659 if (! unit
->stmtlist
)
1665 unit
->line_table
= decode_line_info (unit
, stash
);
1667 if (! unit
->line_table
)
1673 if (unit
->first_child_die_ptr
< unit
->end_ptr
1674 && ! scan_unit_for_functions (unit
))
1682 func_p
= lookup_address_in_function_table (unit
->function_table
, addr
,
1683 &function
, functionname_ptr
);
1684 line_p
= lookup_address_in_line_info_table (unit
->line_table
, addr
,
1685 function
, filename_ptr
,
1687 return line_p
|| func_p
;
1690 /* Locate a section in a BFD containing debugging info. The search starts
1691 from the section after AFTER_SEC, or from the first section in the BFD if
1692 AFTER_SEC is NULL. The search works by examining the names of the
1693 sections. There are two permissiable names. The first is .debug_info.
1694 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
1695 This is a variation on the .debug_info section which has a checksum
1696 describing the contents appended onto the name. This allows the linker to
1697 identify and discard duplicate debugging sections for different
1698 compilation units. */
1699 #define DWARF2_DEBUG_INFO ".debug_info"
1700 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1703 find_debug_info (bfd
*abfd
, asection
*after_sec
)
1708 msec
= after_sec
->next
;
1710 msec
= abfd
->sections
;
1714 if (strcmp (msec
->name
, DWARF2_DEBUG_INFO
) == 0)
1717 if (strncmp (msec
->name
, GNU_LINKONCE_INFO
, strlen (GNU_LINKONCE_INFO
)) == 0)
1726 /* The DWARF2 version of find_nearest_line. Return TRUE if the line
1727 is found without error. ADDR_SIZE is the number of bytes in the
1728 initial .debug_info length field and in the abbreviation offset.
1729 You may use zero to indicate that the default value should be
1733 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
1737 const char **filename_ptr
,
1738 const char **functionname_ptr
,
1739 unsigned int *linenumber_ptr
,
1740 unsigned int addr_size
,
1743 /* Read each compilation unit from the section .debug_info, and check
1744 to see if it contains the address we are searching for. If yes,
1745 lookup the address, and return the line number info. If no, go
1746 on to the next compilation unit.
1748 We keep a list of all the previously read compilation units, and
1749 a pointer to the next un-read compilation unit. Check the
1750 previously read units before reading more. */
1751 struct dwarf2_debug
*stash
;
1753 /* What address are we looking for? */
1756 struct comp_unit
* each
;
1760 if (section
->output_section
)
1761 addr
+= section
->output_section
->vma
+ section
->output_offset
;
1763 addr
+= section
->vma
;
1764 *filename_ptr
= NULL
;
1765 *functionname_ptr
= NULL
;
1766 *linenumber_ptr
= 0;
1768 /* The DWARF2 spec says that the initial length field, and the
1769 offset of the abbreviation table, should both be 4-byte values.
1770 However, some compilers do things differently. */
1773 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
1777 bfd_size_type total_size
;
1779 bfd_size_type amt
= sizeof (struct dwarf2_debug
);
1781 stash
= bfd_zalloc (abfd
, amt
);
1787 msec
= find_debug_info (abfd
, NULL
);
1789 /* No dwarf2 info. Note that at this point the stash
1790 has been allocated, but contains zeros, this lets
1791 future calls to this function fail quicker. */
1794 /* There can be more than one DWARF2 info section in a BFD these days.
1795 Read them all in and produce one large stash. We do this in two
1796 passes - in the first pass we just accumulate the section sizes.
1797 In the second pass we read in the section's contents. The allows
1798 us to avoid reallocing the data as we add sections to the stash. */
1799 for (total_size
= 0; msec
; msec
= find_debug_info (abfd
, msec
))
1800 total_size
+= msec
->size
;
1802 stash
->info_ptr
= bfd_alloc (abfd
, total_size
);
1803 if (stash
->info_ptr
== NULL
)
1806 stash
->info_ptr_unit
= stash
->info_ptr
;
1807 stash
->info_ptr_end
= stash
->info_ptr
;
1809 for (msec
= find_debug_info (abfd
, NULL
);
1811 msec
= find_debug_info (abfd
, msec
))
1814 bfd_size_type start
;
1820 start
= stash
->info_ptr_end
- stash
->info_ptr
;
1822 if ((bfd_simple_get_relocated_section_contents
1823 (abfd
, msec
, stash
->info_ptr
+ start
, symbols
)) == NULL
)
1826 stash
->info_ptr_end
= stash
->info_ptr
+ start
+ size
;
1829 BFD_ASSERT (stash
->info_ptr_end
== stash
->info_ptr
+ total_size
);
1831 stash
->sec
= find_debug_info (abfd
, NULL
);
1832 stash
->sec_info_ptr
= stash
->info_ptr
;
1833 stash
->syms
= symbols
;
1836 /* A null info_ptr indicates that there is no dwarf2 info
1837 (or that an error occured while setting up the stash). */
1838 if (! stash
->info_ptr
)
1841 /* Check the previously read comp. units first. */
1842 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
1843 if (comp_unit_contains_address (each
, addr
))
1844 return comp_unit_find_nearest_line (each
, addr
, filename_ptr
,
1845 functionname_ptr
, linenumber_ptr
,
1848 /* Read each remaining comp. units checking each as they are read. */
1849 while (stash
->info_ptr
< stash
->info_ptr_end
)
1853 unsigned int offset_size
= addr_size
;
1855 length
= read_4_bytes (abfd
, stash
->info_ptr
);
1856 /* A 0xffffff length is the DWARF3 way of indicating we use
1857 64-bit offsets, instead of 32-bit offsets. */
1858 if (length
== 0xffffffff)
1861 length
= read_8_bytes (abfd
, stash
->info_ptr
+ 4);
1862 stash
->info_ptr
+= 12;
1864 /* A zero length is the IRIX way of indicating 64-bit offsets,
1865 mostly because the 64-bit length will generally fit in 32
1866 bits, and the endianness helps. */
1867 else if (length
== 0)
1870 length
= read_4_bytes (abfd
, stash
->info_ptr
+ 4);
1871 stash
->info_ptr
+= 8;
1873 /* In the absence of the hints above, we assume addr_size-sized
1874 offsets, for backward-compatibility with pre-DWARF3 64-bit
1876 else if (addr_size
== 8)
1878 length
= read_8_bytes (abfd
, stash
->info_ptr
);
1879 stash
->info_ptr
+= 8;
1882 stash
->info_ptr
+= 4;
1886 each
= parse_comp_unit (abfd
, stash
, length
, offset_size
);
1887 stash
->info_ptr
+= length
;
1889 if ((bfd_vma
) (stash
->info_ptr
- stash
->sec_info_ptr
)
1890 == stash
->sec
->size
)
1892 stash
->sec
= find_debug_info (abfd
, stash
->sec
);
1893 stash
->sec_info_ptr
= stash
->info_ptr
;
1898 each
->next_unit
= stash
->all_comp_units
;
1899 stash
->all_comp_units
= each
;
1901 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1902 compilation units. If we don't have them (i.e.,
1903 unit->high == 0), we need to consult the line info
1904 table to see if a compilation unit contains the given
1906 if (each
->arange
.high
> 0)
1908 if (comp_unit_contains_address (each
, addr
))
1909 return comp_unit_find_nearest_line (each
, addr
,
1917 found
= comp_unit_find_nearest_line (each
, addr
,