2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
8 From the dwarf2read.c header:
9 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10 Inc. with support from Florida State University (under contract
11 with the Ada Joint Program Office), and Silicon Graphics, Inc.
12 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14 support in dwarfread.c
16 This file is part of BFD.
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or (at
21 your option) any later version.
23 This program is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
34 #include "libiberty.h"
37 #include "elf/dwarf2.h"
39 /* The data in the .debug_line statement prologue looks like this. */
44 unsigned short version
;
45 unsigned int prologue_length
;
46 unsigned char minimum_instruction_length
;
47 unsigned char default_is_stmt
;
49 unsigned char line_range
;
50 unsigned char opcode_base
;
51 unsigned char *standard_opcode_lengths
;
54 /* Attributes have a name and a value. */
58 enum dwarf_attribute name
;
63 struct dwarf_block
*blk
;
71 /* Get at parts of an attribute structure. */
73 #define DW_STRING(attr) ((attr)->u.str)
74 #define DW_UNSND(attr) ((attr)->u.unsnd)
75 #define DW_BLOCK(attr) ((attr)->u.blk)
76 #define DW_SND(attr) ((attr)->u.snd)
77 #define DW_ADDR(attr) ((attr)->u.addr)
79 /* Blocks are a bunch of untyped bytes. */
88 /* A list of all previously read comp_units. */
89 struct comp_unit
* all_comp_units
;
91 /* The next unread compilation unit within the .debug_info section.
92 Zero indicates that the .debug_info section has not been loaded
96 /* Pointer to the end of the .debug_info section memory buffer. */
99 /* Pointer to the section and address of the beginning of the
104 /* Pointer to the symbol table. */
107 /* Pointer to the .debug_abbrev section loaded into memory. */
108 char* dwarf_abbrev_buffer
;
110 /* Length of the loaded .debug_abbrev section. */
111 unsigned long dwarf_abbrev_size
;
113 /* Buffer for decode_line_info. */
114 char *dwarf_line_buffer
;
116 /* Length of the loaded .debug_line section. */
117 unsigned long dwarf_line_size
;
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
*, unsigned int, 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 boolean lookup_address_in_line_info_table
243 PARAMS ((struct line_info_table
*, bfd_vma
, const char **, unsigned int *));
244 static boolean lookup_address_in_function_table
245 PARAMS ((struct funcinfo
*, bfd_vma
, const char **));
246 static boolean scan_unit_for_functions
PARAMS ((struct comp_unit
*));
247 static bfd_vma find_rela_addend
248 PARAMS ((bfd
*, asection
*, bfd_size_type
, asymbol
**));
249 static struct comp_unit
*parse_comp_unit
250 PARAMS ((bfd
*, struct dwarf2_debug
*, bfd_vma
, unsigned int));
251 static boolean comp_unit_contains_address
252 PARAMS ((struct comp_unit
*, bfd_vma
));
253 static boolean comp_unit_find_nearest_line
254 PARAMS ((struct comp_unit
*, bfd_vma
, const char **, const char **,
255 unsigned int *, struct dwarf2_debug
*));
256 static asection
*find_debug_info
PARAMS ((bfd
*, asection
*));
259 The following function up to the END VERBATIM mark are
260 copied directly from dwarf2read.c. */
262 /* Read dwarf information from a buffer. */
265 read_1_byte (abfd
, buf
)
266 bfd
*abfd ATTRIBUTE_UNUSED
;
269 return bfd_get_8 (abfd
, (bfd_byte
*) buf
);
273 read_1_signed_byte (abfd
, buf
)
274 bfd
*abfd ATTRIBUTE_UNUSED
;
277 return bfd_get_signed_8 (abfd
, (bfd_byte
*) buf
);
281 read_2_bytes (abfd
, buf
)
285 return bfd_get_16 (abfd
, (bfd_byte
*) buf
);
288 #if 0 /* This is not used. */
291 read_2_signed_bytes (abfd
, buf
)
295 return bfd_get_signed_16 (abfd
, (bfd_byte
*) buf
);
301 read_4_bytes (abfd
, buf
)
305 return bfd_get_32 (abfd
, (bfd_byte
*) buf
);
308 #if 0 /* This is not used. */
311 read_4_signed_bytes (abfd
, buf
)
315 return bfd_get_signed_32 (abfd
, (bfd_byte
*) buf
);
321 read_8_bytes (abfd
, buf
)
325 return bfd_get_64 (abfd
, (bfd_byte
*) buf
);
329 read_n_bytes (abfd
, buf
, size
)
330 bfd
*abfd ATTRIBUTE_UNUSED
;
332 unsigned int size ATTRIBUTE_UNUSED
;
334 /* If the size of a host char is 8 bits, we can return a pointer
335 to the buffer, otherwise we have to copy the data to a buffer
336 allocated on the temporary obstack. */
341 read_string (abfd
, buf
, bytes_read_ptr
)
342 bfd
*abfd ATTRIBUTE_UNUSED
;
344 unsigned int *bytes_read_ptr
;
346 /* Return a pointer to the embedded string. */
353 *bytes_read_ptr
= strlen (buf
) + 1;
358 read_indirect_string (unit
, buf
, bytes_read_ptr
)
359 struct comp_unit
* unit
;
361 unsigned int *bytes_read_ptr
;
364 struct dwarf2_debug
*stash
= unit
->stash
;
366 if (unit
->offset_size
== 4)
367 offset
= read_4_bytes (unit
->abfd
, buf
);
369 offset
= read_8_bytes (unit
->abfd
, buf
);
370 *bytes_read_ptr
= unit
->offset_size
;
372 if (! stash
->dwarf_str_buffer
)
375 bfd
*abfd
= unit
->abfd
;
377 msec
= bfd_get_section_by_name (abfd
, ".debug_str");
380 (*_bfd_error_handler
)
381 (_("Dwarf Error: Can't find .debug_str section."));
382 bfd_set_error (bfd_error_bad_value
);
386 stash
->dwarf_str_size
= msec
->_raw_size
;
387 stash
->dwarf_str_buffer
= (char*) bfd_alloc (abfd
, msec
->_raw_size
);
388 if (! stash
->dwarf_abbrev_buffer
)
391 if (! bfd_get_section_contents (abfd
, msec
, stash
->dwarf_str_buffer
,
392 (bfd_vma
) 0, msec
->_raw_size
))
396 if (offset
>= stash
->dwarf_str_size
)
398 (*_bfd_error_handler
) (_("Dwarf Error: DW_FORM_strp offset (%u) greater than or equal to .debug_str size (%u)."),
399 offset
, stash
->dwarf_str_size
);
400 bfd_set_error (bfd_error_bad_value
);
404 buf
= stash
->dwarf_str_buffer
+ offset
;
411 read_unsigned_leb128 (abfd
, buf
, bytes_read_ptr
)
412 bfd
*abfd ATTRIBUTE_UNUSED
;
414 unsigned int *bytes_read_ptr
;
417 unsigned int num_read
;
427 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
430 result
|= ((byte
& 0x7f) << shift
);
435 * bytes_read_ptr
= num_read
;
441 read_signed_leb128 (abfd
, buf
, bytes_read_ptr
)
442 bfd
*abfd ATTRIBUTE_UNUSED
;
444 unsigned int * bytes_read_ptr
;
457 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
460 result
|= ((byte
& 0x7f) << shift
);
465 if ((shift
< 32) && (byte
& 0x40))
466 result
|= -(1 << shift
);
468 * bytes_read_ptr
= num_read
;
476 read_address (unit
, buf
)
477 struct comp_unit
* unit
;
480 switch (unit
->addr_size
)
483 return bfd_get_64 (unit
->abfd
, (bfd_byte
*) buf
);
485 return bfd_get_32 (unit
->abfd
, (bfd_byte
*) buf
);
487 return bfd_get_16 (unit
->abfd
, (bfd_byte
*) buf
);
493 /* Lookup an abbrev_info structure in the abbrev hash table. */
495 static struct abbrev_info
*
496 lookup_abbrev (number
,abbrevs
)
498 struct abbrev_info
**abbrevs
;
500 unsigned int hash_number
;
501 struct abbrev_info
*abbrev
;
503 hash_number
= number
% ABBREV_HASH_SIZE
;
504 abbrev
= abbrevs
[hash_number
];
508 if (abbrev
->number
== number
)
511 abbrev
= abbrev
->next
;
517 /* In DWARF version 2, the description of the debugging information is
518 stored in a separate .debug_abbrev section. Before we read any
519 dies from a section we read in all abbreviations and install them
522 static struct abbrev_info
**
523 read_abbrevs (abfd
, offset
, stash
)
526 struct dwarf2_debug
*stash
;
528 struct abbrev_info
**abbrevs
;
530 struct abbrev_info
*cur_abbrev
;
531 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
532 unsigned int abbrev_form
, hash_number
;
535 if (! stash
->dwarf_abbrev_buffer
)
539 msec
= bfd_get_section_by_name (abfd
, ".debug_abbrev");
542 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_abbrev section."));
543 bfd_set_error (bfd_error_bad_value
);
547 stash
->dwarf_abbrev_size
= msec
->_raw_size
;
548 stash
->dwarf_abbrev_buffer
= (char*) bfd_alloc (abfd
, msec
->_raw_size
);
549 if (! stash
->dwarf_abbrev_buffer
)
552 if (! bfd_get_section_contents (abfd
, msec
, stash
->dwarf_abbrev_buffer
,
553 (bfd_vma
) 0, msec
->_raw_size
))
557 if (offset
>= stash
->dwarf_abbrev_size
)
559 (*_bfd_error_handler
) (_("Dwarf Error: Abbrev offset (%u) greater than or equal to abbrev size (%u)."),
560 offset
, stash
->dwarf_abbrev_size
);
561 bfd_set_error (bfd_error_bad_value
);
565 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
566 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
568 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
569 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
570 abbrev_ptr
+= bytes_read
;
572 /* Loop until we reach an abbrev number of 0. */
573 while (abbrev_number
)
575 amt
= sizeof (struct abbrev_info
);
576 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
578 /* Read in abbrev header. */
579 cur_abbrev
->number
= abbrev_number
;
580 cur_abbrev
->tag
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
581 abbrev_ptr
+= bytes_read
;
582 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
);
585 /* Now read in declarations. */
586 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
587 abbrev_ptr
+= bytes_read
;
588 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
589 abbrev_ptr
+= bytes_read
;
593 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
595 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
596 amt
*= sizeof (struct attr_abbrev
);
597 cur_abbrev
->attrs
= ((struct attr_abbrev
*)
598 bfd_realloc (cur_abbrev
->attrs
, amt
));
599 if (! cur_abbrev
->attrs
)
603 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
= abbrev_name
;
604 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
++].form
= abbrev_form
;
605 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
606 abbrev_ptr
+= bytes_read
;
607 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
608 abbrev_ptr
+= bytes_read
;
611 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
612 cur_abbrev
->next
= abbrevs
[hash_number
];
613 abbrevs
[hash_number
] = cur_abbrev
;
615 /* Get next abbreviation.
616 Under Irix6 the abbreviations for a compilation unit are not
617 always properly terminated with an abbrev number of 0.
618 Exit loop if we encounter an abbreviation which we have
619 already read (which means we are about to read the abbreviations
620 for the next compile unit) or if the end of the abbreviation
622 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
623 >= stash
->dwarf_abbrev_size
)
625 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
626 abbrev_ptr
+= bytes_read
;
627 if (lookup_abbrev (abbrev_number
,abbrevs
) != NULL
)
634 /* Read an attribute value described by an attribute form. */
637 read_attribute_value (attr
, form
, unit
, info_ptr
)
638 struct attribute
*attr
;
640 struct comp_unit
*unit
;
643 bfd
*abfd
= unit
->abfd
;
644 unsigned int bytes_read
;
645 struct dwarf_block
*blk
;
653 /* FIXME: DWARF3 draft sais DW_FORM_ref_addr is offset_size. */
654 case DW_FORM_ref_addr
:
655 DW_ADDR (attr
) = read_address (unit
, info_ptr
);
656 info_ptr
+= unit
->addr_size
;
659 amt
= sizeof (struct dwarf_block
);
660 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
661 blk
->size
= read_2_bytes (abfd
, info_ptr
);
663 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
664 info_ptr
+= blk
->size
;
665 DW_BLOCK (attr
) = blk
;
668 amt
= sizeof (struct dwarf_block
);
669 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
670 blk
->size
= read_4_bytes (abfd
, info_ptr
);
672 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
673 info_ptr
+= blk
->size
;
674 DW_BLOCK (attr
) = blk
;
677 DW_UNSND (attr
) = read_2_bytes (abfd
, info_ptr
);
681 DW_UNSND (attr
) = read_4_bytes (abfd
, info_ptr
);
685 DW_UNSND (attr
) = read_8_bytes (abfd
, info_ptr
);
689 DW_STRING (attr
) = read_string (abfd
, info_ptr
, &bytes_read
);
690 info_ptr
+= bytes_read
;
693 DW_STRING (attr
) = read_indirect_string (unit
, info_ptr
, &bytes_read
);
694 info_ptr
+= bytes_read
;
697 amt
= sizeof (struct dwarf_block
);
698 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
699 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
700 info_ptr
+= bytes_read
;
701 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
702 info_ptr
+= blk
->size
;
703 DW_BLOCK (attr
) = blk
;
706 amt
= sizeof (struct dwarf_block
);
707 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
708 blk
->size
= read_1_byte (abfd
, info_ptr
);
710 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
711 info_ptr
+= blk
->size
;
712 DW_BLOCK (attr
) = blk
;
715 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
719 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
723 DW_SND (attr
) = read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
724 info_ptr
+= bytes_read
;
727 DW_UNSND (attr
) = read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
728 info_ptr
+= bytes_read
;
731 DW_UNSND (attr
) = read_1_byte (abfd
, info_ptr
);
735 DW_UNSND (attr
) = read_2_bytes (abfd
, info_ptr
);
739 DW_UNSND (attr
) = read_4_bytes (abfd
, info_ptr
);
743 DW_UNSND (attr
) = read_8_bytes (abfd
, info_ptr
);
746 case DW_FORM_ref_udata
:
747 DW_UNSND (attr
) = read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
748 info_ptr
+= bytes_read
;
750 case DW_FORM_indirect
:
751 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
752 info_ptr
+= bytes_read
;
753 info_ptr
= read_attribute_value (attr
, form
, unit
, info_ptr
);
756 (*_bfd_error_handler
) (_("Dwarf Error: Invalid or unhandled FORM value: %d."),
758 bfd_set_error (bfd_error_bad_value
);
763 /* Read an attribute described by an abbreviated attribute. */
766 read_attribute (attr
, abbrev
, unit
, info_ptr
)
767 struct attribute
*attr
;
768 struct attr_abbrev
*abbrev
;
769 struct comp_unit
*unit
;
772 attr
->name
= abbrev
->name
;
773 info_ptr
= read_attribute_value (attr
, abbrev
->form
, unit
, info_ptr
);
777 /* Source line information table routines. */
779 #define FILE_ALLOC_CHUNK 5
780 #define DIR_ALLOC_CHUNK 5
784 struct line_info
* prev_line
;
789 int end_sequence
; /* End of (sequential) code sequence. */
800 struct line_info_table
803 unsigned int num_files
;
804 unsigned int num_dirs
;
807 struct fileinfo
* files
;
808 struct line_info
* last_line
;
812 add_line_info (table
, address
, filename
, line
, column
, end_sequence
)
813 struct line_info_table
* table
;
820 bfd_size_type amt
= sizeof (struct line_info
);
821 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
823 info
->prev_line
= table
->last_line
;
824 table
->last_line
= info
;
826 info
->address
= address
;
827 info
->filename
= filename
;
829 info
->column
= column
;
830 info
->end_sequence
= end_sequence
;
834 concat_filename (table
, file
)
835 struct line_info_table
* table
;
840 if (file
- 1 >= table
->num_files
)
842 (*_bfd_error_handler
)
843 (_("Dwarf Error: mangled line number section (bad file number)."));
847 filename
= table
->files
[file
- 1].name
;
848 if (IS_ABSOLUTE_PATH(filename
))
853 char* dirname
= (table
->files
[file
- 1].dir
854 ? table
->dirs
[table
->files
[file
- 1].dir
- 1]
856 return (char*) concat (dirname
, "/", filename
, NULL
);
861 arange_add (unit
, low_pc
, high_pc
)
862 struct comp_unit
*unit
;
866 struct arange
*arange
;
868 /* First see if we can cheaply extend an existing range. */
869 arange
= &unit
->arange
;
873 if (low_pc
== arange
->high
)
875 arange
->high
= high_pc
;
878 if (high_pc
== arange
->low
)
880 arange
->low
= low_pc
;
883 arange
= arange
->next
;
887 if (unit
->arange
.high
== 0)
889 /* This is the first address range: store it in unit->arange. */
890 unit
->arange
.next
= 0;
891 unit
->arange
.low
= low_pc
;
892 unit
->arange
.high
= high_pc
;
896 /* Need to allocate a new arange and insert it into the arange list. */
897 arange
= bfd_zalloc (unit
->abfd
, (bfd_size_type
) sizeof (*arange
));
898 arange
->low
= low_pc
;
899 arange
->high
= high_pc
;
901 arange
->next
= unit
->arange
.next
;
902 unit
->arange
.next
= arange
;
905 /* Decode the line number information for UNIT. */
907 static struct line_info_table
*
908 decode_line_info (unit
, stash
)
909 struct comp_unit
*unit
;
910 struct dwarf2_debug
*stash
;
912 bfd
*abfd
= unit
->abfd
;
913 struct line_info_table
* table
;
917 unsigned int i
, bytes_read
, offset_size
;
918 char *cur_file
, *cur_dir
;
919 unsigned char op_code
, extended_op
, adj_opcode
;
922 if (! stash
->dwarf_line_buffer
)
926 msec
= bfd_get_section_by_name (abfd
, ".debug_line");
929 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_line section."));
930 bfd_set_error (bfd_error_bad_value
);
934 stash
->dwarf_line_size
= msec
->_raw_size
;
935 stash
->dwarf_line_buffer
= (char *) bfd_alloc (abfd
, msec
->_raw_size
);
936 if (! stash
->dwarf_line_buffer
)
939 if (! bfd_get_section_contents (abfd
, msec
, stash
->dwarf_line_buffer
,
940 (bfd_vma
) 0, msec
->_raw_size
))
943 /* FIXME: We ought to apply the relocs against this section before
947 /* Since we are using un-relocated data, it is possible to get a bad value
948 for the line_offset. Validate it here so that we won't get a segfault
950 if (unit
->line_offset
>= stash
->dwarf_line_size
)
952 (*_bfd_error_handler
) (_("Dwarf Error: Line offset (%u) greater than or equal to line size (%u)."),
953 unit
->line_offset
, stash
->dwarf_line_size
);
954 bfd_set_error (bfd_error_bad_value
);
958 amt
= sizeof (struct line_info_table
);
959 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
961 table
->comp_dir
= unit
->comp_dir
;
963 table
->num_files
= 0;
970 table
->last_line
= NULL
;
972 line_ptr
= stash
->dwarf_line_buffer
+ unit
->line_offset
;
974 /* Read in the prologue. */
975 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
978 if (lh
.total_length
== 0xffffffff)
980 lh
.total_length
= read_8_bytes (abfd
, line_ptr
);
984 line_end
= line_ptr
+ lh
.total_length
;
985 lh
.version
= read_2_bytes (abfd
, line_ptr
);
987 if (offset_size
== 4)
988 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
);
990 lh
.prologue_length
= read_8_bytes (abfd
, line_ptr
);
991 line_ptr
+= offset_size
;
992 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
);
994 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
);
996 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
);
998 lh
.line_range
= read_1_byte (abfd
, line_ptr
);
1000 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
);
1002 amt
= lh
.opcode_base
* sizeof (unsigned char);
1003 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
1005 lh
.standard_opcode_lengths
[0] = 1;
1007 for (i
= 1; i
< lh
.opcode_base
; ++i
)
1009 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
);
1013 /* Read directory table. */
1014 while ((cur_dir
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1016 line_ptr
+= bytes_read
;
1018 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1020 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1021 amt
*= sizeof (char *);
1022 table
->dirs
= (char **) bfd_realloc (table
->dirs
, amt
);
1027 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1030 line_ptr
+= bytes_read
;
1032 /* Read file name table. */
1033 while ((cur_file
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1035 line_ptr
+= bytes_read
;
1037 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1039 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1040 amt
*= sizeof (struct fileinfo
);
1041 table
->files
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1046 table
->files
[table
->num_files
].name
= cur_file
;
1047 table
->files
[table
->num_files
].dir
=
1048 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1049 line_ptr
+= bytes_read
;
1050 table
->files
[table
->num_files
].time
=
1051 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1052 line_ptr
+= bytes_read
;
1053 table
->files
[table
->num_files
].size
=
1054 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1055 line_ptr
+= bytes_read
;
1059 line_ptr
+= bytes_read
;
1061 /* Read the statement sequences until there's nothing left. */
1062 while (line_ptr
< line_end
)
1064 /* State machine registers. */
1065 bfd_vma address
= 0;
1066 char* filename
= concat_filename (table
, 1);
1067 unsigned int line
= 1;
1068 unsigned int column
= 0;
1069 int is_stmt
= lh
.default_is_stmt
;
1070 int basic_block
= 0;
1071 int end_sequence
= 0, need_low_pc
= 1;
1074 /* Decode the table. */
1075 while (! end_sequence
)
1077 op_code
= read_1_byte (abfd
, line_ptr
);
1080 if (op_code
>= lh
.opcode_base
)
1081 { /* Special operand. */
1082 adj_opcode
= op_code
- lh
.opcode_base
;
1083 address
+= (adj_opcode
/ lh
.line_range
)
1084 * lh
.minimum_instruction_length
;
1085 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
1086 /* Append row to matrix using current values. */
1087 add_line_info (table
, address
, filename
, line
, column
, 0);
1095 else switch (op_code
)
1097 case DW_LNS_extended_op
:
1098 line_ptr
+= 1; /* Ignore length. */
1099 extended_op
= read_1_byte (abfd
, line_ptr
);
1101 switch (extended_op
)
1103 case DW_LNE_end_sequence
:
1105 add_line_info (table
, address
, filename
, line
, column
,
1112 arange_add (unit
, low_pc
, address
);
1114 case DW_LNE_set_address
:
1115 address
= read_address (unit
, line_ptr
);
1116 line_ptr
+= unit
->addr_size
;
1118 case DW_LNE_define_file
:
1119 cur_file
= read_string (abfd
, line_ptr
, &bytes_read
);
1120 line_ptr
+= bytes_read
;
1121 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1123 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1124 amt
*= sizeof (struct fileinfo
);
1126 (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1130 table
->files
[table
->num_files
].name
= cur_file
;
1131 table
->files
[table
->num_files
].dir
=
1132 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1133 line_ptr
+= bytes_read
;
1134 table
->files
[table
->num_files
].time
=
1135 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1136 line_ptr
+= bytes_read
;
1137 table
->files
[table
->num_files
].size
=
1138 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1139 line_ptr
+= bytes_read
;
1143 (*_bfd_error_handler
) (_("Dwarf Error: mangled line number section."));
1144 bfd_set_error (bfd_error_bad_value
);
1149 add_line_info (table
, address
, filename
, line
, column
, 0);
1157 case DW_LNS_advance_pc
:
1158 address
+= lh
.minimum_instruction_length
1159 * read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1160 line_ptr
+= bytes_read
;
1162 case DW_LNS_advance_line
:
1163 line
+= read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
1164 line_ptr
+= bytes_read
;
1166 case DW_LNS_set_file
:
1170 /* The file and directory tables are 0 based, the references
1172 file
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1173 line_ptr
+= bytes_read
;
1174 filename
= concat_filename (table
, file
);
1177 case DW_LNS_set_column
:
1178 column
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1179 line_ptr
+= bytes_read
;
1181 case DW_LNS_negate_stmt
:
1182 is_stmt
= (!is_stmt
);
1184 case DW_LNS_set_basic_block
:
1187 case DW_LNS_const_add_pc
:
1188 address
+= lh
.minimum_instruction_length
1189 * ((255 - lh
.opcode_base
) / lh
.line_range
);
1191 case DW_LNS_fixed_advance_pc
:
1192 address
+= read_2_bytes (abfd
, line_ptr
);
1196 { /* Unknown standard opcode, ignore it. */
1198 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
1200 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1201 line_ptr
+= bytes_read
;
1211 /* If ADDR is within TABLE set the output parameters and return true,
1212 otherwise return false. The output parameters, FILENAME_PTR and
1213 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1216 lookup_address_in_line_info_table (table
,
1220 struct line_info_table
* table
;
1222 const char **filename_ptr
;
1223 unsigned int *linenumber_ptr
;
1225 struct line_info
* next_line
= table
->last_line
;
1226 struct line_info
* each_line
;
1231 each_line
= next_line
->prev_line
;
1233 while (each_line
&& next_line
)
1235 if (!each_line
->end_sequence
1236 && addr
>= each_line
->address
&& addr
< next_line
->address
)
1238 *filename_ptr
= each_line
->filename
;
1239 *linenumber_ptr
= each_line
->line
;
1242 next_line
= each_line
;
1243 each_line
= each_line
->prev_line
;
1249 /* Function table functions. */
1253 struct funcinfo
*prev_func
;
1259 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
1262 lookup_address_in_function_table (table
,
1265 struct funcinfo
* table
;
1267 const char **functionname_ptr
;
1269 struct funcinfo
* each_func
;
1271 for (each_func
= table
;
1273 each_func
= each_func
->prev_func
)
1275 if (addr
>= each_func
->low
&& addr
< each_func
->high
)
1277 *functionname_ptr
= each_func
->name
;
1285 /* DWARF2 Compilation unit functions. */
1287 /* Scan over each die in a comp. unit looking for functions to add
1288 to the function table. */
1291 scan_unit_for_functions (unit
)
1292 struct comp_unit
*unit
;
1294 bfd
*abfd
= unit
->abfd
;
1295 char *info_ptr
= unit
->first_child_die_ptr
;
1296 int nesting_level
= 1;
1298 while (nesting_level
)
1300 unsigned int abbrev_number
, bytes_read
, i
;
1301 struct abbrev_info
*abbrev
;
1302 struct attribute attr
;
1303 struct funcinfo
*func
;
1306 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1307 info_ptr
+= bytes_read
;
1309 if (! abbrev_number
)
1315 abbrev
= lookup_abbrev (abbrev_number
,unit
->abbrevs
);
1318 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %d."),
1320 bfd_set_error (bfd_error_bad_value
);
1324 if (abbrev
->tag
== DW_TAG_subprogram
)
1326 bfd_size_type amt
= sizeof (struct funcinfo
);
1327 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
1328 func
->prev_func
= unit
->function_table
;
1329 unit
->function_table
= func
;
1334 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1336 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1344 name
= DW_STRING (&attr
);
1346 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1347 if (func
->name
== NULL
)
1348 func
->name
= DW_STRING (&attr
);
1351 case DW_AT_MIPS_linkage_name
:
1352 func
->name
= DW_STRING (&attr
);
1356 func
->low
= DW_ADDR (&attr
);
1360 func
->high
= DW_ADDR (&attr
);
1372 name
= DW_STRING (&attr
);
1381 if (abbrev
->has_children
)
1388 /* Look for a RELA relocation to be applied on OFFSET of section SEC,
1389 and return the addend if such a relocation is found. Since this is
1390 only used to find relocations referring to the .debug_abbrev
1391 section, we make sure the relocation refers to this section, but
1392 this is not strictly necessary, and it can probably be safely
1393 removed if needed. However, it is important to note that this
1394 function only returns the addend, it doesn't serve the purpose of
1395 applying a generic relocation.
1397 If no suitable relocation is found, or if it is not a real RELA
1398 relocation, this function returns 0. */
1401 find_rela_addend (abfd
, sec
, offset
, syms
)
1404 bfd_size_type offset
;
1407 long reloc_size
= bfd_get_reloc_upper_bound (abfd
, sec
);
1408 arelent
**relocs
= NULL
;
1409 long reloc_count
, relc
;
1411 if (reloc_size
<= 0)
1414 relocs
= (arelent
**) bfd_malloc ((bfd_size_type
) reloc_size
);
1418 reloc_count
= bfd_canonicalize_reloc (abfd
, sec
, relocs
, syms
);
1420 if (reloc_count
<= 0)
1426 for (relc
= 0; relc
< reloc_count
; relc
++)
1427 if (relocs
[relc
]->address
== offset
1428 && (*relocs
[relc
]->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
1429 && strcmp ((*relocs
[relc
]->sym_ptr_ptr
)->name
,
1430 ".debug_abbrev") == 0)
1432 bfd_vma addend
= (relocs
[relc
]->howto
->partial_inplace
1433 ? 0 : relocs
[relc
]->addend
);
1442 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1443 includes the compilation unit header that proceeds the DIE's, but
1444 does not include the length field that preceeds each compilation
1445 unit header. END_PTR points one past the end of this comp unit.
1446 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1448 This routine does not read the whole compilation unit; only enough
1449 to get to the line number information for the compilation unit. */
1451 static struct comp_unit
*
1452 parse_comp_unit (abfd
, stash
, unit_length
, offset_size
)
1454 struct dwarf2_debug
*stash
;
1455 bfd_vma unit_length
;
1456 unsigned int offset_size
;
1458 struct comp_unit
* unit
;
1460 unsigned short version
;
1461 unsigned int abbrev_offset
= 0;
1462 unsigned char addr_size
;
1463 struct abbrev_info
** abbrevs
;
1465 unsigned int abbrev_number
, bytes_read
, i
;
1466 struct abbrev_info
*abbrev
;
1467 struct attribute attr
;
1469 char *info_ptr
= stash
->info_ptr
;
1470 char *end_ptr
= info_ptr
+ unit_length
;
1474 version
= read_2_bytes (abfd
, info_ptr
);
1476 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
1477 if (offset_size
== 4)
1478 abbrev_offset
= read_4_bytes (abfd
, info_ptr
);
1480 abbrev_offset
= read_8_bytes (abfd
, info_ptr
);
1481 /* The abbrev offset is generally a relocation pointing to
1482 .debug_abbrev+offset. On RELA targets, we have to find the
1483 relocation and extract the addend to obtain the actual
1484 abbrev_offset, so do it here. */
1485 off
= info_ptr
- stash
->sec_info_ptr
;
1486 abbrev_offset
+= find_rela_addend (abfd
, stash
->sec
, off
, stash
->syms
);
1487 info_ptr
+= offset_size
;
1488 addr_size
= read_1_byte (abfd
, info_ptr
);
1493 (*_bfd_error_handler
) (_("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information."), version
);
1494 bfd_set_error (bfd_error_bad_value
);
1498 if (addr_size
> sizeof (bfd_vma
))
1500 (*_bfd_error_handler
) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1503 bfd_set_error (bfd_error_bad_value
);
1507 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
1509 (*_bfd_error_handler
) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size
);
1510 bfd_set_error (bfd_error_bad_value
);
1514 /* Read the abbrevs for this compilation unit into a table. */
1515 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
);
1519 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1520 info_ptr
+= bytes_read
;
1521 if (! abbrev_number
)
1523 (*_bfd_error_handler
) (_("Dwarf Error: Bad abbrev number: %d."),
1525 bfd_set_error (bfd_error_bad_value
);
1529 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
1532 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %d."),
1534 bfd_set_error (bfd_error_bad_value
);
1538 amt
= sizeof (struct comp_unit
);
1539 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
1541 unit
->addr_size
= addr_size
;
1542 unit
->offset_size
= offset_size
;
1543 unit
->abbrevs
= abbrevs
;
1544 unit
->end_ptr
= end_ptr
;
1545 unit
->stash
= stash
;
1547 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1549 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1551 /* Store the data if it is of an attribute we want to keep in a
1552 partial symbol table. */
1555 case DW_AT_stmt_list
:
1557 unit
->line_offset
= DW_UNSND (&attr
);
1561 unit
->name
= DW_STRING (&attr
);
1565 unit
->arange
.low
= DW_ADDR (&attr
);
1569 unit
->arange
.high
= DW_ADDR (&attr
);
1572 case DW_AT_comp_dir
:
1574 char* comp_dir
= DW_STRING (&attr
);
1577 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1578 directory, get rid of it. */
1579 char *cp
= (char*) strchr (comp_dir
, ':');
1581 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
1584 unit
->comp_dir
= comp_dir
;
1593 unit
->first_child_die_ptr
= info_ptr
;
1597 /* Return true if UNIT contains the address given by ADDR. */
1600 comp_unit_contains_address (unit
, addr
)
1601 struct comp_unit
* unit
;
1604 struct arange
*arange
;
1609 arange
= &unit
->arange
;
1612 if (addr
>= arange
->low
&& addr
< arange
->high
)
1614 arange
= arange
->next
;
1621 /* If UNIT contains ADDR, set the output parameters to the values for
1622 the line containing ADDR. The output parameters, FILENAME_PTR,
1623 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1626 Return true of UNIT contains ADDR, and no errors were encountered;
1630 comp_unit_find_nearest_line (unit
, addr
,
1631 filename_ptr
, functionname_ptr
, linenumber_ptr
,
1633 struct comp_unit
* unit
;
1635 const char **filename_ptr
;
1636 const char **functionname_ptr
;
1637 unsigned int *linenumber_ptr
;
1638 struct dwarf2_debug
*stash
;
1646 if (! unit
->line_table
)
1648 if (! unit
->stmtlist
)
1654 unit
->line_table
= decode_line_info (unit
, stash
);
1656 if (! unit
->line_table
)
1662 if (unit
->first_child_die_ptr
< unit
->end_ptr
1663 && ! scan_unit_for_functions (unit
))
1670 line_p
= lookup_address_in_line_info_table (unit
->line_table
,
1674 func_p
= lookup_address_in_function_table (unit
->function_table
,
1677 return line_p
|| func_p
;
1680 /* Locate a section in a BFD containing debugging info. The search starts from the
1681 section after AFTER_SEC, or from the first section in the BFD if AFTER_SEC is
1682 NULL. The search works by examining the names of the sections. There are two
1683 permissiable names. The first is .debug_info. This is the standard DWARF2 name.
1684 The second is a prefix .gnu.linkonce.wi. This is a variation on the .debug_info
1685 section which has a checksum describing the contents appended onto the name. This
1686 allows the linker to identify and discard duplicate debugging sections for
1687 different compilation units. */
1688 #define DWARF2_DEBUG_INFO ".debug_info"
1689 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1692 find_debug_info (abfd
, after_sec
)
1694 asection
* after_sec
;
1699 msec
= after_sec
->next
;
1701 msec
= abfd
->sections
;
1705 if (strcmp (msec
->name
, DWARF2_DEBUG_INFO
) == 0)
1708 if (strncmp (msec
->name
, GNU_LINKONCE_INFO
, strlen (GNU_LINKONCE_INFO
)) == 0)
1717 /* The DWARF2 version of find_nearest line. Return true if the line
1718 is found without error. ADDR_SIZE is the number of bytes in the
1719 initial .debug_info length field and in the abbreviation offset.
1720 You may use zero to indicate that the default value should be
1724 _bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
1725 filename_ptr
, functionname_ptr
,
1732 const char **filename_ptr
;
1733 const char **functionname_ptr
;
1734 unsigned int *linenumber_ptr
;
1735 unsigned int addr_size
;
1738 /* Read each compilation unit from the section .debug_info, and check
1739 to see if it contains the address we are searching for. If yes,
1740 lookup the address, and return the line number info. If no, go
1741 on to the next compilation unit.
1743 We keep a list of all the previously read compilation units, and
1744 a pointer to the next un-read compilation unit. Check the
1745 previously read units before reading more. */
1746 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
1748 /* What address are we looking for? */
1749 bfd_vma addr
= offset
+ section
->vma
;
1751 struct comp_unit
* each
;
1753 *filename_ptr
= NULL
;
1754 *functionname_ptr
= NULL
;
1755 *linenumber_ptr
= 0;
1757 /* The DWARF2 spec says that the initial length field, and the
1758 offset of the abbreviation table, should both be 4-byte values.
1759 However, some compilers do things differently. */
1762 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
1766 bfd_size_type total_size
;
1768 bfd_size_type amt
= sizeof (struct dwarf2_debug
);
1770 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
1774 *pinfo
= (PTR
) stash
;
1776 msec
= find_debug_info (abfd
, NULL
);
1778 /* No dwarf2 info. Note that at this point the stash
1779 has been allocated, but contains zeros, this lets
1780 future calls to this function fail quicker. */
1783 /* There can be more than one DWARF2 info section in a BFD these days.
1784 Read them all in and produce one large stash. We do this in two
1785 passes - in the first pass we just accumulate the section sizes.
1786 In the second pass we read in the section's contents. The allows
1787 us to avoid reallocing the data as we add sections to the stash. */
1788 for (total_size
= 0; msec
; msec
= find_debug_info (abfd
, msec
))
1789 total_size
+= msec
->_raw_size
;
1791 stash
->info_ptr
= (char *) bfd_alloc (abfd
, total_size
);
1792 if (stash
->info_ptr
== NULL
)
1795 stash
->info_ptr_end
= stash
->info_ptr
;
1797 for (msec
= find_debug_info (abfd
, NULL
);
1799 msec
= find_debug_info (abfd
, msec
))
1802 bfd_size_type start
;
1804 size
= msec
->_raw_size
;
1808 start
= stash
->info_ptr_end
- stash
->info_ptr
;
1810 if (! bfd_get_section_contents (abfd
, msec
, stash
->info_ptr
+ start
,
1814 stash
->info_ptr_end
= stash
->info_ptr
+ start
+ size
;
1817 BFD_ASSERT (stash
->info_ptr_end
== stash
->info_ptr
+ total_size
);
1819 stash
->sec
= find_debug_info (abfd
, NULL
);
1820 stash
->sec_info_ptr
= stash
->info_ptr
;
1821 stash
->syms
= symbols
;
1824 /* FIXME: There is a problem with the contents of the
1825 .debug_info section. The 'low' and 'high' addresses of the
1826 comp_units are computed by relocs against symbols in the
1827 .text segment. We need these addresses in order to determine
1828 the nearest line number, and so we have to resolve the
1829 relocs. There is a similar problem when the .debug_line
1830 section is processed as well (e.g., there may be relocs
1831 against the operand of the DW_LNE_set_address operator).
1833 Unfortunately getting hold of the reloc information is hard...
1835 For now, this means that disassembling object files (as
1836 opposed to fully executables) does not always work as well as
1839 /* A null info_ptr indicates that there is no dwarf2 info
1840 (or that an error occured while setting up the stash). */
1841 if (! stash
->info_ptr
)
1844 /* Check the previously read comp. units first. */
1845 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
1846 if (comp_unit_contains_address (each
, addr
))
1847 return comp_unit_find_nearest_line (each
, addr
, filename_ptr
,
1848 functionname_ptr
, linenumber_ptr
,
1851 /* Read each remaining comp. units checking each as they are read. */
1852 while (stash
->info_ptr
< stash
->info_ptr_end
)
1856 unsigned int offset_size
= addr_size
;
1860 length
= read_4_bytes (abfd
, stash
->info_ptr
);
1861 if (length
== 0xffffffff)
1864 length
= read_8_bytes (abfd
, stash
->info_ptr
+ 4);
1865 stash
->info_ptr
+= 8;
1869 length
= read_8_bytes (abfd
, stash
->info_ptr
);
1870 stash
->info_ptr
+= addr_size
;
1874 each
= parse_comp_unit (abfd
, stash
, length
, offset_size
);
1875 stash
->info_ptr
+= length
;
1877 if ((bfd_vma
) (stash
->info_ptr
- stash
->sec_info_ptr
)
1878 == stash
->sec
->_raw_size
)
1880 stash
->sec
= find_debug_info (abfd
, stash
->sec
);
1881 stash
->sec_info_ptr
= stash
->info_ptr
;
1886 each
->next_unit
= stash
->all_comp_units
;
1887 stash
->all_comp_units
= each
;
1889 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1890 compilation units. If we don't have them (i.e.,
1891 unit->high == 0), we need to consult the line info
1892 table to see if a compilation unit contains the given
1894 if (each
->arange
.high
> 0)
1896 if (comp_unit_contains_address (each
, addr
))
1897 return comp_unit_find_nearest_line (each
, addr
,
1905 found
= comp_unit_find_nearest_line (each
, addr
,