2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009, 2010 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 3 of the License, or (at
21 your option) any later version.
23 This program is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
31 MA 02110-1301, USA. */
35 #include "libiberty.h"
40 /* The data in the .debug_line statement prologue looks like this. */
45 unsigned short version
;
46 bfd_vma prologue_length
;
47 unsigned char minimum_instruction_length
;
48 unsigned char default_is_stmt
;
50 unsigned char line_range
;
51 unsigned char opcode_base
;
52 unsigned char *standard_opcode_lengths
;
55 /* Attributes have a name and a value. */
59 enum dwarf_attribute name
;
64 struct dwarf_block
*blk
;
71 /* Blocks are a bunch of untyped bytes. */
78 struct adjusted_section
86 /* A list of all previously read comp_units. */
87 struct comp_unit
*all_comp_units
;
89 /* Last comp unit in list above. */
90 struct comp_unit
*last_comp_unit
;
92 /* The next unread compilation unit within the .debug_info section.
93 Zero indicates that the .debug_info section has not been loaded
97 /* Pointer to the end of the .debug_info section memory buffer. */
98 bfd_byte
*info_ptr_end
;
100 /* Pointer to the bfd, section and address of the beginning of the
101 section. The bfd might be different than expected because of
102 gnu_debuglink sections. */
105 bfd_byte
*sec_info_ptr
;
107 /* A pointer to the memory block allocated for info_ptr. Neither
108 info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
109 beginning of the malloc block. This is used only to free the
111 bfd_byte
*info_ptr_memory
;
113 /* Pointer to the symbol table. */
116 /* Pointer to the .debug_abbrev section loaded into memory. */
117 bfd_byte
*dwarf_abbrev_buffer
;
119 /* Length of the loaded .debug_abbrev section. */
120 bfd_size_type dwarf_abbrev_size
;
122 /* Buffer for decode_line_info. */
123 bfd_byte
*dwarf_line_buffer
;
125 /* Length of the loaded .debug_line section. */
126 bfd_size_type dwarf_line_size
;
128 /* Pointer to the .debug_str section loaded into memory. */
129 bfd_byte
*dwarf_str_buffer
;
131 /* Length of the loaded .debug_str section. */
132 bfd_size_type dwarf_str_size
;
134 /* Pointer to the .debug_ranges section loaded into memory. */
135 bfd_byte
*dwarf_ranges_buffer
;
137 /* Length of the loaded .debug_ranges section. */
138 bfd_size_type dwarf_ranges_size
;
140 /* If the most recent call to bfd_find_nearest_line was given an
141 address in an inlined function, preserve a pointer into the
142 calling chain for subsequent calls to bfd_find_inliner_info to
144 struct funcinfo
*inliner_chain
;
146 /* Number of sections whose VMA we must adjust. */
147 unsigned int adjusted_section_count
;
149 /* Array of sections with adjusted VMA. */
150 struct adjusted_section
*adjusted_sections
;
152 /* Number of times find_line is called. This is used in
153 the heuristic for enabling the info hash tables. */
156 #define STASH_INFO_HASH_TRIGGER 100
158 /* Hash table mapping symbol names to function infos. */
159 struct info_hash_table
*funcinfo_hash_table
;
161 /* Hash table mapping symbol names to variable infos. */
162 struct info_hash_table
*varinfo_hash_table
;
164 /* Head of comp_unit list in the last hash table update. */
165 struct comp_unit
*hash_units_head
;
167 /* Status of info hash. */
168 int info_hash_status
;
169 #define STASH_INFO_HASH_OFF 0
170 #define STASH_INFO_HASH_ON 1
171 #define STASH_INFO_HASH_DISABLED 2
181 /* A minimal decoding of DWARF2 compilation units. We only decode
182 what's needed to get to the line number information. */
186 /* Chain the previously read compilation units. */
187 struct comp_unit
*next_unit
;
189 /* Likewise, chain the compilation unit read after this one.
190 The comp units are stored in reversed reading order. */
191 struct comp_unit
*prev_unit
;
193 /* Keep the bfd convenient (for memory allocation). */
196 /* The lowest and highest addresses contained in this compilation
197 unit as specified in the compilation unit header. */
198 struct arange arange
;
200 /* The DW_AT_name attribute (for error messages). */
203 /* The abbrev hash table. */
204 struct abbrev_info
**abbrevs
;
206 /* Note that an error was found by comp_unit_find_nearest_line. */
209 /* The DW_AT_comp_dir attribute. */
212 /* TRUE if there is a line number table associated with this comp. unit. */
215 /* Pointer to the current comp_unit so that we can find a given entry
217 bfd_byte
*info_ptr_unit
;
219 /* Pointer to the start of the debug section, for DW_FORM_ref_addr. */
220 bfd_byte
*sec_info_ptr
;
222 /* The offset into .debug_line of the line number table. */
223 unsigned long line_offset
;
225 /* Pointer to the first child die for the comp unit. */
226 bfd_byte
*first_child_die_ptr
;
228 /* The end of the comp unit. */
231 /* The decoded line number, NULL if not yet decoded. */
232 struct line_info_table
*line_table
;
234 /* A list of the functions found in this comp. unit. */
235 struct funcinfo
*function_table
;
237 /* A list of the variables found in this comp. unit. */
238 struct varinfo
*variable_table
;
240 /* Pointer to dwarf2_debug structure. */
241 struct dwarf2_debug
*stash
;
243 /* DWARF format version for this unit - from unit header. */
246 /* Address size for this unit - from unit header. */
247 unsigned char addr_size
;
249 /* Offset size for this unit - from unit header. */
250 unsigned char offset_size
;
252 /* Base address for this unit - from DW_AT_low_pc attribute of
253 DW_TAG_compile_unit DIE */
254 bfd_vma base_address
;
256 /* TRUE if symbols are cached in hash table for faster lookup by name. */
260 /* This data structure holds the information of an abbrev. */
263 unsigned int number
; /* Number identifying abbrev. */
264 enum dwarf_tag tag
; /* DWARF tag. */
265 int has_children
; /* Boolean. */
266 unsigned int num_attrs
; /* Number of attributes. */
267 struct attr_abbrev
*attrs
; /* An array of attribute descriptions. */
268 struct abbrev_info
*next
; /* Next in chain. */
273 enum dwarf_attribute name
;
274 enum dwarf_form form
;
277 #ifndef ABBREV_HASH_SIZE
278 #define ABBREV_HASH_SIZE 121
280 #ifndef ATTR_ALLOC_CHUNK
281 #define ATTR_ALLOC_CHUNK 4
284 /* Variable and function hash tables. This is used to speed up look-up
285 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
286 In order to share code between variable and function infos, we use
287 a list of untyped pointer for all variable/function info associated with
288 a symbol. We waste a bit of memory for list with one node but that
289 simplifies the code. */
291 struct info_list_node
293 struct info_list_node
*next
;
297 /* Info hash entry. */
298 struct info_hash_entry
300 struct bfd_hash_entry root
;
301 struct info_list_node
*head
;
304 struct info_hash_table
306 struct bfd_hash_table base
;
309 /* Function to create a new entry in info hash table. */
311 static struct bfd_hash_entry
*
312 info_hash_table_newfunc (struct bfd_hash_entry
*entry
,
313 struct bfd_hash_table
*table
,
316 struct info_hash_entry
*ret
= (struct info_hash_entry
*) entry
;
318 /* Allocate the structure if it has not already been allocated by a
322 ret
= (struct info_hash_entry
*) bfd_hash_allocate (table
,
328 /* Call the allocation method of the base class. */
329 ret
= ((struct info_hash_entry
*)
330 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
332 /* Initialize the local fields here. */
336 return (struct bfd_hash_entry
*) ret
;
339 /* Function to create a new info hash table. It returns a pointer to the
340 newly created table or NULL if there is any error. We need abfd
341 solely for memory allocation. */
343 static struct info_hash_table
*
344 create_info_hash_table (bfd
*abfd
)
346 struct info_hash_table
*hash_table
;
348 hash_table
= (struct info_hash_table
*)
349 bfd_alloc (abfd
, sizeof (struct info_hash_table
));
353 if (!bfd_hash_table_init (&hash_table
->base
, info_hash_table_newfunc
,
354 sizeof (struct info_hash_entry
)))
356 bfd_release (abfd
, hash_table
);
363 /* Insert an info entry into an info hash table. We do not check of
364 duplicate entries. Also, the caller need to guarantee that the
365 right type of info in inserted as info is passed as a void* pointer.
366 This function returns true if there is no error. */
369 insert_info_hash_table (struct info_hash_table
*hash_table
,
374 struct info_hash_entry
*entry
;
375 struct info_list_node
*node
;
377 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
,
382 node
= (struct info_list_node
*) bfd_hash_allocate (&hash_table
->base
,
388 node
->next
= entry
->head
;
394 /* Look up an info entry list from an info hash table. Return NULL
397 static struct info_list_node
*
398 lookup_info_hash_table (struct info_hash_table
*hash_table
, const char *key
)
400 struct info_hash_entry
*entry
;
402 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
, key
,
404 return entry
? entry
->head
: NULL
;
407 /* Read a section into its appropriate place in the dwarf2_debug
408 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
409 not NULL, use bfd_simple_get_relocated_section_contents to read the
410 section contents, otherwise use bfd_get_section_contents. Fail if
411 the located section does not contain at least OFFSET bytes. */
414 read_section (bfd
* abfd
,
415 const char * section_name
,
416 const char * compressed_section_name
,
419 bfd_byte
** section_buffer
,
420 bfd_size_type
* section_size
)
423 bfd_boolean section_is_compressed
= FALSE
;
425 /* read_section is a noop if the section has already been read. */
426 if (!*section_buffer
)
428 msec
= bfd_get_section_by_name (abfd
, section_name
);
429 if (! msec
&& compressed_section_name
)
431 msec
= bfd_get_section_by_name (abfd
, compressed_section_name
);
432 section_is_compressed
= TRUE
;
436 (*_bfd_error_handler
) (_("Dwarf Error: Can't find %s section."), section_name
);
437 bfd_set_error (bfd_error_bad_value
);
441 *section_size
= msec
->rawsize
? msec
->rawsize
: msec
->size
;
445 = bfd_simple_get_relocated_section_contents (abfd
, msec
, NULL
, syms
);
446 if (! *section_buffer
)
451 *section_buffer
= (bfd_byte
*) bfd_malloc (*section_size
);
452 if (! *section_buffer
)
454 if (! bfd_get_section_contents (abfd
, msec
, *section_buffer
,
459 if (section_is_compressed
)
461 if (! bfd_uncompress_section_contents (section_buffer
, section_size
))
463 (*_bfd_error_handler
) (_("Dwarf Error: unable to decompress %s section."), compressed_section_name
);
464 bfd_set_error (bfd_error_bad_value
);
470 /* It is possible to get a bad value for the offset into the section
471 that the client wants. Validate it here to avoid trouble later. */
472 if (offset
!= 0 && offset
>= *section_size
)
474 (*_bfd_error_handler
) (_("Dwarf Error: Offset (%lu) greater than or equal to %s size (%lu)."),
475 (long) offset
, section_name
, *section_size
);
476 bfd_set_error (bfd_error_bad_value
);
484 The following function up to the END VERBATIM mark are
485 copied directly from dwarf2read.c. */
487 /* Read dwarf information from a buffer. */
490 read_1_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
)
492 return bfd_get_8 (abfd
, buf
);
496 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
)
498 return bfd_get_signed_8 (abfd
, buf
);
502 read_2_bytes (bfd
*abfd
, bfd_byte
*buf
)
504 return bfd_get_16 (abfd
, buf
);
508 read_4_bytes (bfd
*abfd
, bfd_byte
*buf
)
510 return bfd_get_32 (abfd
, buf
);
514 read_8_bytes (bfd
*abfd
, bfd_byte
*buf
)
516 return bfd_get_64 (abfd
, buf
);
520 read_n_bytes (bfd
*abfd ATTRIBUTE_UNUSED
,
522 unsigned int size ATTRIBUTE_UNUSED
)
528 read_string (bfd
*abfd ATTRIBUTE_UNUSED
,
530 unsigned int *bytes_read_ptr
)
532 /* Return a pointer to the embedded string. */
533 char *str
= (char *) buf
;
541 *bytes_read_ptr
= strlen (str
) + 1;
548 read_indirect_string (struct comp_unit
* unit
,
550 unsigned int * bytes_read_ptr
)
553 struct dwarf2_debug
*stash
= unit
->stash
;
556 if (unit
->offset_size
== 4)
557 offset
= read_4_bytes (unit
->abfd
, buf
);
559 offset
= read_8_bytes (unit
->abfd
, buf
);
561 *bytes_read_ptr
= unit
->offset_size
;
563 if (! read_section (unit
->abfd
, ".debug_str", ".zdebug_str",
565 &stash
->dwarf_str_buffer
, &stash
->dwarf_str_size
))
568 str
= (char *) stash
->dwarf_str_buffer
+ offset
;
575 read_address (struct comp_unit
*unit
, bfd_byte
*buf
)
577 int signed_vma
= get_elf_backend_data (unit
->abfd
)->sign_extend_vma
;
581 switch (unit
->addr_size
)
584 return bfd_get_signed_64 (unit
->abfd
, buf
);
586 return bfd_get_signed_32 (unit
->abfd
, buf
);
588 return bfd_get_signed_16 (unit
->abfd
, buf
);
595 switch (unit
->addr_size
)
598 return bfd_get_64 (unit
->abfd
, buf
);
600 return bfd_get_32 (unit
->abfd
, buf
);
602 return bfd_get_16 (unit
->abfd
, buf
);
609 /* Lookup an abbrev_info structure in the abbrev hash table. */
611 static struct abbrev_info
*
612 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
614 unsigned int hash_number
;
615 struct abbrev_info
*abbrev
;
617 hash_number
= number
% ABBREV_HASH_SIZE
;
618 abbrev
= abbrevs
[hash_number
];
622 if (abbrev
->number
== number
)
625 abbrev
= abbrev
->next
;
631 /* In DWARF version 2, the description of the debugging information is
632 stored in a separate .debug_abbrev section. Before we read any
633 dies from a section we read in all abbreviations and install them
636 static struct abbrev_info
**
637 read_abbrevs (bfd
*abfd
, bfd_uint64_t offset
, struct dwarf2_debug
*stash
)
639 struct abbrev_info
**abbrevs
;
640 bfd_byte
*abbrev_ptr
;
641 struct abbrev_info
*cur_abbrev
;
642 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
643 unsigned int abbrev_form
, hash_number
;
646 if (! read_section (abfd
, ".debug_abbrev", ".zdebug_abbrev",
648 &stash
->dwarf_abbrev_buffer
, &stash
->dwarf_abbrev_size
))
651 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
652 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
656 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
657 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
658 abbrev_ptr
+= bytes_read
;
660 /* Loop until we reach an abbrev number of 0. */
661 while (abbrev_number
)
663 amt
= sizeof (struct abbrev_info
);
664 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
665 if (cur_abbrev
== NULL
)
668 /* Read in abbrev header. */
669 cur_abbrev
->number
= abbrev_number
;
670 cur_abbrev
->tag
= (enum dwarf_tag
)
671 read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
672 abbrev_ptr
+= bytes_read
;
673 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
);
676 /* Now read in declarations. */
677 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
678 abbrev_ptr
+= bytes_read
;
679 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
680 abbrev_ptr
+= bytes_read
;
684 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
686 struct attr_abbrev
*tmp
;
688 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
689 amt
*= sizeof (struct attr_abbrev
);
690 tmp
= (struct attr_abbrev
*) bfd_realloc (cur_abbrev
->attrs
, amt
);
695 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
697 struct abbrev_info
*abbrev
= abbrevs
[i
];
701 free (abbrev
->attrs
);
702 abbrev
= abbrev
->next
;
707 cur_abbrev
->attrs
= tmp
;
710 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
711 = (enum dwarf_attribute
) abbrev_name
;
712 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
++].form
713 = (enum dwarf_form
) abbrev_form
;
714 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
715 abbrev_ptr
+= bytes_read
;
716 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
717 abbrev_ptr
+= bytes_read
;
720 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
721 cur_abbrev
->next
= abbrevs
[hash_number
];
722 abbrevs
[hash_number
] = cur_abbrev
;
724 /* Get next abbreviation.
725 Under Irix6 the abbreviations for a compilation unit are not
726 always properly terminated with an abbrev number of 0.
727 Exit loop if we encounter an abbreviation which we have
728 already read (which means we are about to read the abbreviations
729 for the next compile unit) or if the end of the abbreviation
731 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
732 >= stash
->dwarf_abbrev_size
)
734 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
735 abbrev_ptr
+= bytes_read
;
736 if (lookup_abbrev (abbrev_number
,abbrevs
) != NULL
)
743 /* Read an attribute value described by an attribute form. */
746 read_attribute_value (struct attribute
*attr
,
748 struct comp_unit
*unit
,
751 bfd
*abfd
= unit
->abfd
;
752 unsigned int bytes_read
;
753 struct dwarf_block
*blk
;
756 attr
->form
= (enum dwarf_form
) form
;
760 case DW_FORM_ref_addr
:
761 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
763 if (unit
->version
== 3 || unit
->version
== 4)
765 if (unit
->offset_size
== 4)
766 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
);
768 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
);
769 info_ptr
+= unit
->offset_size
;
774 attr
->u
.val
= read_address (unit
, info_ptr
);
775 info_ptr
+= unit
->addr_size
;
777 case DW_FORM_sec_offset
:
778 if (unit
->offset_size
== 4)
779 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
);
781 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
);
782 info_ptr
+= unit
->offset_size
;
785 amt
= sizeof (struct dwarf_block
);
786 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
789 blk
->size
= read_2_bytes (abfd
, info_ptr
);
791 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
792 info_ptr
+= blk
->size
;
796 amt
= sizeof (struct dwarf_block
);
797 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
800 blk
->size
= read_4_bytes (abfd
, info_ptr
);
802 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
803 info_ptr
+= blk
->size
;
807 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
);
811 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
);
815 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
819 attr
->u
.str
= read_string (abfd
, info_ptr
, &bytes_read
);
820 info_ptr
+= bytes_read
;
823 attr
->u
.str
= read_indirect_string (unit
, info_ptr
, &bytes_read
);
824 info_ptr
+= bytes_read
;
826 case DW_FORM_exprloc
:
828 amt
= sizeof (struct dwarf_block
);
829 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
832 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
833 info_ptr
+= bytes_read
;
834 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
835 info_ptr
+= blk
->size
;
839 amt
= sizeof (struct dwarf_block
);
840 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
843 blk
->size
= read_1_byte (abfd
, info_ptr
);
845 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
846 info_ptr
+= blk
->size
;
850 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
854 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
857 case DW_FORM_flag_present
:
861 attr
->u
.sval
= read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
862 info_ptr
+= bytes_read
;
865 attr
->u
.val
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
866 info_ptr
+= bytes_read
;
869 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
873 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
);
877 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
);
881 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
884 case DW_FORM_ref_sig8
:
885 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
888 case DW_FORM_ref_udata
:
889 attr
->u
.val
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
890 info_ptr
+= bytes_read
;
892 case DW_FORM_indirect
:
893 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
894 info_ptr
+= bytes_read
;
895 info_ptr
= read_attribute_value (attr
, form
, unit
, info_ptr
);
898 (*_bfd_error_handler
) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
900 bfd_set_error (bfd_error_bad_value
);
906 /* Read an attribute described by an abbreviated attribute. */
909 read_attribute (struct attribute
*attr
,
910 struct attr_abbrev
*abbrev
,
911 struct comp_unit
*unit
,
914 attr
->name
= abbrev
->name
;
915 info_ptr
= read_attribute_value (attr
, abbrev
->form
, unit
, info_ptr
);
919 /* Source line information table routines. */
921 #define FILE_ALLOC_CHUNK 5
922 #define DIR_ALLOC_CHUNK 5
926 struct line_info
* prev_line
;
931 int end_sequence
; /* End of (sequential) code sequence. */
945 struct line_sequence
* prev_sequence
;
946 struct line_info
* last_line
; /* Largest VMA. */
949 struct line_info_table
952 unsigned int num_files
;
953 unsigned int num_dirs
;
954 unsigned int num_sequences
;
957 struct fileinfo
* files
;
958 struct line_sequence
* sequences
;
959 struct line_info
* lcl_head
; /* Local head; used in 'add_line_info'. */
962 /* Remember some information about each function. If the function is
963 inlined (DW_TAG_inlined_subroutine) it may have two additional
964 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
965 source code location where this function was inlined. */
969 struct funcinfo
*prev_func
; /* Pointer to previous function in list of all functions */
970 struct funcinfo
*caller_func
; /* Pointer to function one scope higher */
971 char *caller_file
; /* Source location file name where caller_func inlines this func */
972 int caller_line
; /* Source location line number where caller_func inlines this func */
973 char *file
; /* Source location file name */
974 int line
; /* Source location line number */
977 struct arange arange
;
978 asection
*sec
; /* Where the symbol is defined */
983 /* Pointer to previous variable in list of all variables */
984 struct varinfo
*prev_var
;
985 /* Source location file name */
987 /* Source location line number */
992 /* Where the symbol is defined */
994 /* Is this a stack variable? */
995 unsigned int stack
: 1;
998 /* Return TRUE if NEW_LINE should sort after LINE. */
1000 static inline bfd_boolean
1001 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
1003 return (new_line
->address
> line
->address
1004 || (new_line
->address
== line
->address
1005 && new_line
->end_sequence
< line
->end_sequence
));
1009 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1010 that the list is sorted. Note that the line_info list is sorted from
1011 highest to lowest VMA (with possible duplicates); that is,
1012 line_info->prev_line always accesses an equal or smaller VMA. */
1015 add_line_info (struct line_info_table
*table
,
1019 unsigned int column
,
1022 bfd_size_type amt
= sizeof (struct line_info
);
1023 struct line_sequence
* seq
= table
->sequences
;
1024 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
1029 /* Set member data of 'info'. */
1030 info
->address
= address
;
1032 info
->column
= column
;
1033 info
->end_sequence
= end_sequence
;
1035 if (filename
&& filename
[0])
1037 info
->filename
= (char *) bfd_alloc (table
->abfd
, strlen (filename
) + 1);
1038 if (info
->filename
== NULL
)
1040 strcpy (info
->filename
, filename
);
1043 info
->filename
= NULL
;
1045 /* Find the correct location for 'info'. Normally we will receive
1046 new line_info data 1) in order and 2) with increasing VMAs.
1047 However some compilers break the rules (cf. decode_line_info) and
1048 so we include some heuristics for quickly finding the correct
1049 location for 'info'. In particular, these heuristics optimize for
1050 the common case in which the VMA sequence that we receive is a
1051 list of locally sorted VMAs such as
1052 p...z a...j (where a < j < p < z)
1054 Note: table->lcl_head is used to head an *actual* or *possible*
1055 sub-sequence within the list (such as a...j) that is not directly
1056 headed by table->last_line
1058 Note: we may receive duplicate entries from 'decode_line_info'. */
1061 && seq
->last_line
->address
== address
1062 && seq
->last_line
->end_sequence
== end_sequence
)
1064 /* We only keep the last entry with the same address and end
1065 sequence. See PR ld/4986. */
1066 if (table
->lcl_head
== seq
->last_line
)
1067 table
->lcl_head
= info
;
1068 info
->prev_line
= seq
->last_line
->prev_line
;
1069 seq
->last_line
= info
;
1071 else if (!seq
|| seq
->last_line
->end_sequence
)
1073 /* Start a new line sequence. */
1074 amt
= sizeof (struct line_sequence
);
1075 seq
= (struct line_sequence
*) bfd_malloc (amt
);
1078 seq
->low_pc
= address
;
1079 seq
->prev_sequence
= table
->sequences
;
1080 seq
->last_line
= info
;
1081 table
->lcl_head
= info
;
1082 table
->sequences
= seq
;
1083 table
->num_sequences
++;
1085 else if (new_line_sorts_after (info
, seq
->last_line
))
1087 /* Normal case: add 'info' to the beginning of the current sequence. */
1088 info
->prev_line
= seq
->last_line
;
1089 seq
->last_line
= info
;
1091 /* lcl_head: initialize to head a *possible* sequence at the end. */
1092 if (!table
->lcl_head
)
1093 table
->lcl_head
= info
;
1095 else if (!new_line_sorts_after (info
, table
->lcl_head
)
1096 && (!table
->lcl_head
->prev_line
1097 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
1099 /* Abnormal but easy: lcl_head is the head of 'info'. */
1100 info
->prev_line
= table
->lcl_head
->prev_line
;
1101 table
->lcl_head
->prev_line
= info
;
1105 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1106 are valid heads for 'info'. Reset 'lcl_head'. */
1107 struct line_info
* li2
= seq
->last_line
; /* Always non-NULL. */
1108 struct line_info
* li1
= li2
->prev_line
;
1112 if (!new_line_sorts_after (info
, li2
)
1113 && new_line_sorts_after (info
, li1
))
1116 li2
= li1
; /* always non-NULL */
1117 li1
= li1
->prev_line
;
1119 table
->lcl_head
= li2
;
1120 info
->prev_line
= table
->lcl_head
->prev_line
;
1121 table
->lcl_head
->prev_line
= info
;
1122 if (address
< seq
->low_pc
)
1123 seq
->low_pc
= address
;
1128 /* Extract a fully qualified filename from a line info table.
1129 The returned string has been malloc'ed and it is the caller's
1130 responsibility to free it. */
1133 concat_filename (struct line_info_table
*table
, unsigned int file
)
1137 if (file
- 1 >= table
->num_files
)
1139 /* FILE == 0 means unknown. */
1141 (*_bfd_error_handler
)
1142 (_("Dwarf Error: mangled line number section (bad file number)."));
1143 return strdup ("<unknown>");
1146 filename
= table
->files
[file
- 1].name
;
1148 if (!IS_ABSOLUTE_PATH (filename
))
1150 char *dir_name
= NULL
;
1151 char *subdir_name
= NULL
;
1155 if (table
->files
[file
- 1].dir
)
1156 subdir_name
= table
->dirs
[table
->files
[file
- 1].dir
- 1];
1158 if (!subdir_name
|| !IS_ABSOLUTE_PATH (subdir_name
))
1159 dir_name
= table
->comp_dir
;
1163 dir_name
= subdir_name
;
1168 return strdup (filename
);
1170 len
= strlen (dir_name
) + strlen (filename
) + 2;
1174 len
+= strlen (subdir_name
) + 1;
1175 name
= (char *) bfd_malloc (len
);
1177 sprintf (name
, "%s/%s/%s", dir_name
, subdir_name
, filename
);
1181 name
= (char *) bfd_malloc (len
);
1183 sprintf (name
, "%s/%s", dir_name
, filename
);
1189 return strdup (filename
);
1193 arange_add (bfd
*abfd
, struct arange
*first_arange
,
1194 bfd_vma low_pc
, bfd_vma high_pc
)
1196 struct arange
*arange
;
1198 /* If the first arange is empty, use it. */
1199 if (first_arange
->high
== 0)
1201 first_arange
->low
= low_pc
;
1202 first_arange
->high
= high_pc
;
1206 /* Next see if we can cheaply extend an existing range. */
1207 arange
= first_arange
;
1210 if (low_pc
== arange
->high
)
1212 arange
->high
= high_pc
;
1215 if (high_pc
== arange
->low
)
1217 arange
->low
= low_pc
;
1220 arange
= arange
->next
;
1224 /* Need to allocate a new arange and insert it into the arange list.
1225 Order isn't significant, so just insert after the first arange. */
1226 arange
= (struct arange
*) bfd_zalloc (abfd
, sizeof (*arange
));
1229 arange
->low
= low_pc
;
1230 arange
->high
= high_pc
;
1231 arange
->next
= first_arange
->next
;
1232 first_arange
->next
= arange
;
1236 /* Compare function for line sequences. */
1239 compare_sequences (const void* a
, const void* b
)
1241 const struct line_sequence
* seq1
= a
;
1242 const struct line_sequence
* seq2
= b
;
1244 /* Sort by low_pc as the primary key. */
1245 if (seq1
->low_pc
< seq2
->low_pc
)
1247 if (seq1
->low_pc
> seq2
->low_pc
)
1250 /* If low_pc values are equal, sort in reverse order of
1251 high_pc, so that the largest region comes first. */
1252 if (seq1
->last_line
->address
< seq2
->last_line
->address
)
1254 if (seq1
->last_line
->address
> seq2
->last_line
->address
)
1260 /* Sort the line sequences for quick lookup. */
1263 sort_line_sequences (struct line_info_table
* table
)
1266 struct line_sequence
* sequences
;
1267 struct line_sequence
* seq
;
1269 unsigned int num_sequences
= table
->num_sequences
;
1270 bfd_vma last_high_pc
;
1272 if (num_sequences
== 0)
1275 /* Allocate space for an array of sequences. */
1276 amt
= sizeof (struct line_sequence
) * num_sequences
;
1277 sequences
= (struct line_sequence
*) bfd_alloc (table
->abfd
, amt
);
1278 if (sequences
== NULL
)
1281 /* Copy the linked list into the array, freeing the original nodes. */
1282 seq
= table
->sequences
;
1283 for (n
= 0; n
< num_sequences
; n
++)
1285 struct line_sequence
* last_seq
= seq
;
1288 sequences
[n
].low_pc
= seq
->low_pc
;
1289 sequences
[n
].prev_sequence
= NULL
;
1290 sequences
[n
].last_line
= seq
->last_line
;
1291 seq
= seq
->prev_sequence
;
1294 BFD_ASSERT (seq
== NULL
);
1296 qsort (sequences
, n
, sizeof (struct line_sequence
), compare_sequences
);
1298 /* Make the list binary-searchable by trimming overlapping entries
1299 and removing nested entries. */
1301 last_high_pc
= sequences
[0].last_line
->address
;
1302 for (n
= 1; n
< table
->num_sequences
; n
++)
1304 if (sequences
[n
].low_pc
< last_high_pc
)
1306 if (sequences
[n
].last_line
->address
<= last_high_pc
)
1307 /* Skip nested entries. */
1310 /* Trim overlapping entries. */
1311 sequences
[n
].low_pc
= last_high_pc
;
1313 last_high_pc
= sequences
[n
].last_line
->address
;
1314 if (n
> num_sequences
)
1316 /* Close up the gap. */
1317 sequences
[num_sequences
].low_pc
= sequences
[n
].low_pc
;
1318 sequences
[num_sequences
].last_line
= sequences
[n
].last_line
;
1323 table
->sequences
= sequences
;
1324 table
->num_sequences
= num_sequences
;
1328 /* Decode the line number information for UNIT. */
1330 static struct line_info_table
*
1331 decode_line_info (struct comp_unit
*unit
, struct dwarf2_debug
*stash
)
1333 bfd
*abfd
= unit
->abfd
;
1334 struct line_info_table
* table
;
1337 struct line_head lh
;
1338 unsigned int i
, bytes_read
, offset_size
;
1339 char *cur_file
, *cur_dir
;
1340 unsigned char op_code
, extended_op
, adj_opcode
;
1343 if (! read_section (abfd
, ".debug_line", ".zdebug_line",
1344 stash
->syms
, unit
->line_offset
,
1345 &stash
->dwarf_line_buffer
, &stash
->dwarf_line_size
))
1348 amt
= sizeof (struct line_info_table
);
1349 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
1353 table
->comp_dir
= unit
->comp_dir
;
1355 table
->num_files
= 0;
1356 table
->files
= NULL
;
1358 table
->num_dirs
= 0;
1361 table
->num_sequences
= 0;
1362 table
->sequences
= NULL
;
1364 table
->lcl_head
= NULL
;
1366 line_ptr
= stash
->dwarf_line_buffer
+ unit
->line_offset
;
1368 /* Read in the prologue. */
1369 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
1372 if (lh
.total_length
== 0xffffffff)
1374 lh
.total_length
= read_8_bytes (abfd
, line_ptr
);
1378 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
1380 /* Handle (non-standard) 64-bit DWARF2 formats. */
1381 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
1385 line_end
= line_ptr
+ lh
.total_length
;
1386 lh
.version
= read_2_bytes (abfd
, line_ptr
);
1388 if (offset_size
== 4)
1389 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
);
1391 lh
.prologue_length
= read_8_bytes (abfd
, line_ptr
);
1392 line_ptr
+= offset_size
;
1393 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
);
1395 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
);
1397 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
);
1399 lh
.line_range
= read_1_byte (abfd
, line_ptr
);
1401 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
);
1403 amt
= lh
.opcode_base
* sizeof (unsigned char);
1404 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
1406 lh
.standard_opcode_lengths
[0] = 1;
1408 for (i
= 1; i
< lh
.opcode_base
; ++i
)
1410 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
);
1414 /* Read directory table. */
1415 while ((cur_dir
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1417 line_ptr
+= bytes_read
;
1419 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1423 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1424 amt
*= sizeof (char *);
1426 tmp
= (char **) bfd_realloc (table
->dirs
, amt
);
1432 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1435 line_ptr
+= bytes_read
;
1437 /* Read file name table. */
1438 while ((cur_file
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1440 line_ptr
+= bytes_read
;
1442 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1444 struct fileinfo
*tmp
;
1446 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1447 amt
*= sizeof (struct fileinfo
);
1449 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1455 table
->files
[table
->num_files
].name
= cur_file
;
1456 table
->files
[table
->num_files
].dir
=
1457 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1458 line_ptr
+= bytes_read
;
1459 table
->files
[table
->num_files
].time
=
1460 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1461 line_ptr
+= bytes_read
;
1462 table
->files
[table
->num_files
].size
=
1463 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1464 line_ptr
+= bytes_read
;
1468 line_ptr
+= bytes_read
;
1470 /* Read the statement sequences until there's nothing left. */
1471 while (line_ptr
< line_end
)
1473 /* State machine registers. */
1474 bfd_vma address
= 0;
1475 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
1476 unsigned int line
= 1;
1477 unsigned int column
= 0;
1478 int is_stmt
= lh
.default_is_stmt
;
1479 int end_sequence
= 0;
1480 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1481 compilers generate address sequences that are wildly out of
1482 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1483 for ia64-Linux). Thus, to determine the low and high
1484 address, we must compare on every DW_LNS_copy, etc. */
1485 bfd_vma low_pc
= (bfd_vma
) -1;
1486 bfd_vma high_pc
= 0;
1488 /* Decode the table. */
1489 while (! end_sequence
)
1491 op_code
= read_1_byte (abfd
, line_ptr
);
1494 if (op_code
>= lh
.opcode_base
)
1496 /* Special operand. */
1497 adj_opcode
= op_code
- lh
.opcode_base
;
1498 address
+= (adj_opcode
/ lh
.line_range
)
1499 * lh
.minimum_instruction_length
;
1500 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
1501 /* Append row to matrix using current values. */
1502 if (!add_line_info (table
, address
, filename
, line
, column
, 0))
1504 if (address
< low_pc
)
1506 if (address
> high_pc
)
1509 else switch (op_code
)
1511 case DW_LNS_extended_op
:
1512 /* Ignore length. */
1514 extended_op
= read_1_byte (abfd
, line_ptr
);
1517 switch (extended_op
)
1519 case DW_LNE_end_sequence
:
1521 if (!add_line_info (table
, address
, filename
, line
, column
,
1524 if (address
< low_pc
)
1526 if (address
> high_pc
)
1528 if (!arange_add (unit
->abfd
, &unit
->arange
, low_pc
, high_pc
))
1531 case DW_LNE_set_address
:
1532 address
= read_address (unit
, line_ptr
);
1533 line_ptr
+= unit
->addr_size
;
1535 case DW_LNE_define_file
:
1536 cur_file
= read_string (abfd
, line_ptr
, &bytes_read
);
1537 line_ptr
+= bytes_read
;
1538 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1540 struct fileinfo
*tmp
;
1542 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1543 amt
*= sizeof (struct fileinfo
);
1544 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1549 table
->files
[table
->num_files
].name
= cur_file
;
1550 table
->files
[table
->num_files
].dir
=
1551 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1552 line_ptr
+= bytes_read
;
1553 table
->files
[table
->num_files
].time
=
1554 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1555 line_ptr
+= bytes_read
;
1556 table
->files
[table
->num_files
].size
=
1557 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1558 line_ptr
+= bytes_read
;
1561 case DW_LNE_set_discriminator
:
1562 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1563 line_ptr
+= bytes_read
;
1566 (*_bfd_error_handler
) (_("Dwarf Error: mangled line number section."));
1567 bfd_set_error (bfd_error_bad_value
);
1569 if (filename
!= NULL
)
1575 if (!add_line_info (table
, address
, filename
, line
, column
, 0))
1577 if (address
< low_pc
)
1579 if (address
> high_pc
)
1582 case DW_LNS_advance_pc
:
1583 address
+= lh
.minimum_instruction_length
1584 * read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1585 line_ptr
+= bytes_read
;
1587 case DW_LNS_advance_line
:
1588 line
+= read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
1589 line_ptr
+= bytes_read
;
1591 case DW_LNS_set_file
:
1595 /* The file and directory tables are 0
1596 based, the references are 1 based. */
1597 file
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1598 line_ptr
+= bytes_read
;
1601 filename
= concat_filename (table
, file
);
1604 case DW_LNS_set_column
:
1605 column
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1606 line_ptr
+= bytes_read
;
1608 case DW_LNS_negate_stmt
:
1609 is_stmt
= (!is_stmt
);
1611 case DW_LNS_set_basic_block
:
1613 case DW_LNS_const_add_pc
:
1614 address
+= lh
.minimum_instruction_length
1615 * ((255 - lh
.opcode_base
) / lh
.line_range
);
1617 case DW_LNS_fixed_advance_pc
:
1618 address
+= read_2_bytes (abfd
, line_ptr
);
1622 /* Unknown standard opcode, ignore it. */
1623 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
1625 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1626 line_ptr
+= bytes_read
;
1636 if (sort_line_sequences (table
))
1640 if (table
->sequences
!= NULL
)
1641 free (table
->sequences
);
1642 if (table
->files
!= NULL
)
1643 free (table
->files
);
1644 if (table
->dirs
!= NULL
)
1649 /* If ADDR is within TABLE set the output parameters and return TRUE,
1650 otherwise return FALSE. The output parameters, FILENAME_PTR and
1651 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1654 lookup_address_in_line_info_table (struct line_info_table
*table
,
1656 const char **filename_ptr
,
1657 unsigned int *linenumber_ptr
)
1659 struct line_sequence
*seq
= NULL
;
1660 struct line_info
*each_line
;
1663 /* Binary search the array of sequences. */
1665 high
= table
->num_sequences
;
1668 mid
= (low
+ high
) / 2;
1669 seq
= &table
->sequences
[mid
];
1670 if (addr
< seq
->low_pc
)
1672 else if (addr
>= seq
->last_line
->address
)
1678 if (seq
&& addr
>= seq
->low_pc
&& addr
< seq
->last_line
->address
)
1680 /* Note: seq->last_line should be a descendingly sorted list. */
1681 for (each_line
= seq
->last_line
;
1683 each_line
= each_line
->prev_line
)
1684 if (addr
>= each_line
->address
)
1688 && !(each_line
->end_sequence
|| each_line
== seq
->last_line
))
1690 *filename_ptr
= each_line
->filename
;
1691 *linenumber_ptr
= each_line
->line
;
1696 *filename_ptr
= NULL
;
1700 /* Read in the .debug_ranges section for future reference. */
1703 read_debug_ranges (struct comp_unit
*unit
)
1705 struct dwarf2_debug
*stash
= unit
->stash
;
1706 return read_section (unit
->abfd
, ".debug_ranges", ".zdebug_ranges",
1708 &stash
->dwarf_ranges_buffer
, &stash
->dwarf_ranges_size
);
1711 /* Function table functions. */
1713 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1714 Note that we need to find the function that has the smallest
1715 range that contains ADDR, to handle inlined functions without
1716 depending upon them being ordered in TABLE by increasing range. */
1719 lookup_address_in_function_table (struct comp_unit
*unit
,
1721 struct funcinfo
**function_ptr
,
1722 const char **functionname_ptr
)
1724 struct funcinfo
* each_func
;
1725 struct funcinfo
* best_fit
= NULL
;
1726 struct arange
*arange
;
1728 for (each_func
= unit
->function_table
;
1730 each_func
= each_func
->prev_func
)
1732 for (arange
= &each_func
->arange
;
1734 arange
= arange
->next
)
1736 if (addr
>= arange
->low
&& addr
< arange
->high
)
1739 ((arange
->high
- arange
->low
) < (best_fit
->arange
.high
- best_fit
->arange
.low
)))
1740 best_fit
= each_func
;
1747 *functionname_ptr
= best_fit
->name
;
1748 *function_ptr
= best_fit
;
1757 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1758 and LINENUMBER_PTR, and return TRUE. */
1761 lookup_symbol_in_function_table (struct comp_unit
*unit
,
1764 const char **filename_ptr
,
1765 unsigned int *linenumber_ptr
)
1767 struct funcinfo
* each_func
;
1768 struct funcinfo
* best_fit
= NULL
;
1769 struct arange
*arange
;
1770 const char *name
= bfd_asymbol_name (sym
);
1771 asection
*sec
= bfd_get_section (sym
);
1773 for (each_func
= unit
->function_table
;
1775 each_func
= each_func
->prev_func
)
1777 for (arange
= &each_func
->arange
;
1779 arange
= arange
->next
)
1781 if ((!each_func
->sec
|| each_func
->sec
== sec
)
1782 && addr
>= arange
->low
1783 && addr
< arange
->high
1785 && strcmp (name
, each_func
->name
) == 0
1787 || ((arange
->high
- arange
->low
)
1788 < (best_fit
->arange
.high
- best_fit
->arange
.low
))))
1789 best_fit
= each_func
;
1795 best_fit
->sec
= sec
;
1796 *filename_ptr
= best_fit
->file
;
1797 *linenumber_ptr
= best_fit
->line
;
1804 /* Variable table functions. */
1806 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1807 LINENUMBER_PTR, and return TRUE. */
1810 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
1813 const char **filename_ptr
,
1814 unsigned int *linenumber_ptr
)
1816 const char *name
= bfd_asymbol_name (sym
);
1817 asection
*sec
= bfd_get_section (sym
);
1818 struct varinfo
* each
;
1820 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
1821 if (each
->stack
== 0
1822 && each
->file
!= NULL
1823 && each
->name
!= NULL
1824 && each
->addr
== addr
1825 && (!each
->sec
|| each
->sec
== sec
)
1826 && strcmp (name
, each
->name
) == 0)
1832 *filename_ptr
= each
->file
;
1833 *linenumber_ptr
= each
->line
;
1841 find_abstract_instance_name (struct comp_unit
*unit
,
1842 struct attribute
*attr_ptr
)
1844 bfd
*abfd
= unit
->abfd
;
1846 unsigned int abbrev_number
, bytes_read
, i
;
1847 struct abbrev_info
*abbrev
;
1848 bfd_uint64_t die_ref
= attr_ptr
->u
.val
;
1849 struct attribute attr
;
1852 /* DW_FORM_ref_addr can reference an entry in a different CU. It
1853 is an offset from the .debug_info section, not the current CU. */
1854 if (attr_ptr
->form
== DW_FORM_ref_addr
)
1856 /* We only support DW_FORM_ref_addr within the same file, so
1857 any relocations should be resolved already. */
1861 info_ptr
= unit
->sec_info_ptr
+ die_ref
;
1864 info_ptr
= unit
->info_ptr_unit
+ die_ref
;
1865 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1866 info_ptr
+= bytes_read
;
1870 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
1873 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
1875 bfd_set_error (bfd_error_bad_value
);
1879 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1881 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
,
1883 if (info_ptr
== NULL
)
1888 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1892 case DW_AT_specification
:
1893 name
= find_abstract_instance_name (unit
, &attr
);
1895 case DW_AT_MIPS_linkage_name
:
1908 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
,
1909 bfd_uint64_t offset
)
1911 bfd_byte
*ranges_ptr
;
1912 bfd_vma base_address
= unit
->base_address
;
1914 if (! unit
->stash
->dwarf_ranges_buffer
)
1916 if (! read_debug_ranges (unit
))
1919 ranges_ptr
= unit
->stash
->dwarf_ranges_buffer
+ offset
;
1926 low_pc
= read_address (unit
, ranges_ptr
);
1927 ranges_ptr
+= unit
->addr_size
;
1928 high_pc
= read_address (unit
, ranges_ptr
);
1929 ranges_ptr
+= unit
->addr_size
;
1931 if (low_pc
== 0 && high_pc
== 0)
1933 if (low_pc
== -1UL && high_pc
!= -1UL)
1934 base_address
= high_pc
;
1937 if (!arange_add (unit
->abfd
, arange
,
1938 base_address
+ low_pc
, base_address
+ high_pc
))
1945 /* DWARF2 Compilation unit functions. */
1947 /* Scan over each die in a comp. unit looking for functions to add
1948 to the function table and variables to the variable table. */
1951 scan_unit_for_symbols (struct comp_unit
*unit
)
1953 bfd
*abfd
= unit
->abfd
;
1954 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
1955 int nesting_level
= 1;
1956 struct funcinfo
**nested_funcs
;
1957 int nested_funcs_size
;
1959 /* Maintain a stack of in-scope functions and inlined functions, which we
1960 can use to set the caller_func field. */
1961 nested_funcs_size
= 32;
1962 nested_funcs
= (struct funcinfo
**)
1963 bfd_malloc (nested_funcs_size
* sizeof (struct funcinfo
*));
1964 if (nested_funcs
== NULL
)
1966 nested_funcs
[nesting_level
] = 0;
1968 while (nesting_level
)
1970 unsigned int abbrev_number
, bytes_read
, i
;
1971 struct abbrev_info
*abbrev
;
1972 struct attribute attr
;
1973 struct funcinfo
*func
;
1974 struct varinfo
*var
;
1976 bfd_vma high_pc
= 0;
1978 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1979 info_ptr
+= bytes_read
;
1981 if (! abbrev_number
)
1987 abbrev
= lookup_abbrev (abbrev_number
,unit
->abbrevs
);
1990 (*_bfd_error_handler
)
1991 (_("Dwarf Error: Could not find abbrev number %u."),
1993 bfd_set_error (bfd_error_bad_value
);
1998 if (abbrev
->tag
== DW_TAG_subprogram
1999 || abbrev
->tag
== DW_TAG_entry_point
2000 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
2002 bfd_size_type amt
= sizeof (struct funcinfo
);
2003 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
2006 func
->tag
= abbrev
->tag
;
2007 func
->prev_func
= unit
->function_table
;
2008 unit
->function_table
= func
;
2009 BFD_ASSERT (!unit
->cached
);
2011 if (func
->tag
== DW_TAG_inlined_subroutine
)
2012 for (i
= nesting_level
- 1; i
>= 1; i
--)
2013 if (nested_funcs
[i
])
2015 func
->caller_func
= nested_funcs
[i
];
2018 nested_funcs
[nesting_level
] = func
;
2023 if (abbrev
->tag
== DW_TAG_variable
)
2025 bfd_size_type amt
= sizeof (struct varinfo
);
2026 var
= (struct varinfo
*) bfd_zalloc (abfd
, amt
);
2029 var
->tag
= abbrev
->tag
;
2031 var
->prev_var
= unit
->variable_table
;
2032 unit
->variable_table
= var
;
2033 BFD_ASSERT (!unit
->cached
);
2036 /* No inline function in scope at this nesting level. */
2037 nested_funcs
[nesting_level
] = 0;
2040 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2042 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
2043 if (info_ptr
== NULL
)
2050 case DW_AT_call_file
:
2051 func
->caller_file
= concat_filename (unit
->line_table
,
2055 case DW_AT_call_line
:
2056 func
->caller_line
= attr
.u
.val
;
2059 case DW_AT_abstract_origin
:
2060 func
->name
= find_abstract_instance_name (unit
, &attr
);
2064 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
2065 if (func
->name
== NULL
)
2066 func
->name
= attr
.u
.str
;
2069 case DW_AT_MIPS_linkage_name
:
2070 func
->name
= attr
.u
.str
;
2074 low_pc
= attr
.u
.val
;
2078 high_pc
= attr
.u
.val
;
2082 if (!read_rangelist (unit
, &func
->arange
, attr
.u
.val
))
2086 case DW_AT_decl_file
:
2087 func
->file
= concat_filename (unit
->line_table
,
2091 case DW_AT_decl_line
:
2092 func
->line
= attr
.u
.val
;
2104 var
->name
= attr
.u
.str
;
2107 case DW_AT_decl_file
:
2108 var
->file
= concat_filename (unit
->line_table
,
2112 case DW_AT_decl_line
:
2113 var
->line
= attr
.u
.val
;
2116 case DW_AT_external
:
2117 if (attr
.u
.val
!= 0)
2121 case DW_AT_location
:
2125 case DW_FORM_block1
:
2126 case DW_FORM_block2
:
2127 case DW_FORM_block4
:
2128 case DW_FORM_exprloc
:
2129 if (*attr
.u
.blk
->data
== DW_OP_addr
)
2133 /* Verify that DW_OP_addr is the only opcode in the
2134 location, in which case the block size will be 1
2135 plus the address size. */
2136 /* ??? For TLS variables, gcc can emit
2137 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
2138 which we don't handle here yet. */
2139 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
2140 var
->addr
= bfd_get (unit
->addr_size
* 8,
2142 attr
.u
.blk
->data
+ 1);
2157 if (func
&& high_pc
!= 0)
2159 if (!arange_add (unit
->abfd
, &func
->arange
, low_pc
, high_pc
))
2163 if (abbrev
->has_children
)
2167 if (nesting_level
>= nested_funcs_size
)
2169 struct funcinfo
**tmp
;
2171 nested_funcs_size
*= 2;
2172 tmp
= (struct funcinfo
**)
2173 bfd_realloc (nested_funcs
,
2174 (nested_funcs_size
* sizeof (struct funcinfo
*)));
2179 nested_funcs
[nesting_level
] = 0;
2183 free (nested_funcs
);
2187 free (nested_funcs
);
2191 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
2192 includes the compilation unit header that proceeds the DIE's, but
2193 does not include the length field that precedes each compilation
2194 unit header. END_PTR points one past the end of this comp unit.
2195 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
2197 This routine does not read the whole compilation unit; only enough
2198 to get to the line number information for the compilation unit. */
2200 static struct comp_unit
*
2201 parse_comp_unit (struct dwarf2_debug
*stash
,
2202 bfd_vma unit_length
,
2203 bfd_byte
*info_ptr_unit
,
2204 unsigned int offset_size
)
2206 struct comp_unit
* unit
;
2207 unsigned int version
;
2208 bfd_uint64_t abbrev_offset
= 0;
2209 unsigned int addr_size
;
2210 struct abbrev_info
** abbrevs
;
2211 unsigned int abbrev_number
, bytes_read
, i
;
2212 struct abbrev_info
*abbrev
;
2213 struct attribute attr
;
2214 bfd_byte
*info_ptr
= stash
->info_ptr
;
2215 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
2218 bfd_vma high_pc
= 0;
2219 bfd
*abfd
= stash
->bfd_ptr
;
2221 version
= read_2_bytes (abfd
, info_ptr
);
2223 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
2224 if (offset_size
== 4)
2225 abbrev_offset
= read_4_bytes (abfd
, info_ptr
);
2227 abbrev_offset
= read_8_bytes (abfd
, info_ptr
);
2228 info_ptr
+= offset_size
;
2229 addr_size
= read_1_byte (abfd
, info_ptr
);
2232 if (version
!= 2 && version
!= 3 && version
!= 4)
2234 (*_bfd_error_handler
) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2, 3 and 4 information."), version
);
2235 bfd_set_error (bfd_error_bad_value
);
2239 if (addr_size
> sizeof (bfd_vma
))
2241 (*_bfd_error_handler
) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
2243 (unsigned int) sizeof (bfd_vma
));
2244 bfd_set_error (bfd_error_bad_value
);
2248 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
2250 (*_bfd_error_handler
) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size
);
2251 bfd_set_error (bfd_error_bad_value
);
2255 /* Read the abbrevs for this compilation unit into a table. */
2256 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
);
2260 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
2261 info_ptr
+= bytes_read
;
2262 if (! abbrev_number
)
2264 (*_bfd_error_handler
) (_("Dwarf Error: Bad abbrev number: %u."),
2266 bfd_set_error (bfd_error_bad_value
);
2270 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
2273 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
2275 bfd_set_error (bfd_error_bad_value
);
2279 amt
= sizeof (struct comp_unit
);
2280 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
2284 unit
->version
= version
;
2285 unit
->addr_size
= addr_size
;
2286 unit
->offset_size
= offset_size
;
2287 unit
->abbrevs
= abbrevs
;
2288 unit
->end_ptr
= end_ptr
;
2289 unit
->stash
= stash
;
2290 unit
->info_ptr_unit
= info_ptr_unit
;
2291 unit
->sec_info_ptr
= stash
->sec_info_ptr
;
2293 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2295 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
2296 if (info_ptr
== NULL
)
2299 /* Store the data if it is of an attribute we want to keep in a
2300 partial symbol table. */
2303 case DW_AT_stmt_list
:
2305 unit
->line_offset
= attr
.u
.val
;
2309 unit
->name
= attr
.u
.str
;
2313 low_pc
= attr
.u
.val
;
2314 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2315 this is the base address to use when reading location
2316 lists or range lists. */
2317 unit
->base_address
= low_pc
;
2321 high_pc
= attr
.u
.val
;
2325 if (!read_rangelist (unit
, &unit
->arange
, attr
.u
.val
))
2329 case DW_AT_comp_dir
:
2331 char *comp_dir
= attr
.u
.str
;
2334 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2335 directory, get rid of it. */
2336 char *cp
= strchr (comp_dir
, ':');
2338 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
2341 unit
->comp_dir
= comp_dir
;
2351 if (!arange_add (unit
->abfd
, &unit
->arange
, low_pc
, high_pc
))
2355 unit
->first_child_die_ptr
= info_ptr
;
2359 /* Return TRUE if UNIT may contain the address given by ADDR. When
2360 there are functions written entirely with inline asm statements, the
2361 range info in the compilation unit header may not be correct. We
2362 need to consult the line info table to see if a compilation unit
2363 really contains the given address. */
2366 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
2368 struct arange
*arange
;
2373 arange
= &unit
->arange
;
2376 if (addr
>= arange
->low
&& addr
< arange
->high
)
2378 arange
= arange
->next
;
2385 /* If UNIT contains ADDR, set the output parameters to the values for
2386 the line containing ADDR. The output parameters, FILENAME_PTR,
2387 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2390 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2394 comp_unit_find_nearest_line (struct comp_unit
*unit
,
2396 const char **filename_ptr
,
2397 const char **functionname_ptr
,
2398 unsigned int *linenumber_ptr
,
2399 struct dwarf2_debug
*stash
)
2403 struct funcinfo
*function
;
2408 if (! unit
->line_table
)
2410 if (! unit
->stmtlist
)
2416 unit
->line_table
= decode_line_info (unit
, stash
);
2418 if (! unit
->line_table
)
2424 if (unit
->first_child_die_ptr
< unit
->end_ptr
2425 && ! scan_unit_for_symbols (unit
))
2433 func_p
= lookup_address_in_function_table (unit
, addr
,
2434 &function
, functionname_ptr
);
2435 if (func_p
&& (function
->tag
== DW_TAG_inlined_subroutine
))
2436 stash
->inliner_chain
= function
;
2437 line_p
= lookup_address_in_line_info_table (unit
->line_table
, addr
,
2440 return line_p
|| func_p
;
2443 /* Check to see if line info is already decoded in a comp_unit.
2444 If not, decode it. Returns TRUE if no errors were encountered;
2448 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
,
2449 struct dwarf2_debug
*stash
)
2454 if (! unit
->line_table
)
2456 if (! unit
->stmtlist
)
2462 unit
->line_table
= decode_line_info (unit
, stash
);
2464 if (! unit
->line_table
)
2470 if (unit
->first_child_die_ptr
< unit
->end_ptr
2471 && ! scan_unit_for_symbols (unit
))
2481 /* If UNIT contains SYM at ADDR, set the output parameters to the
2482 values for the line containing SYM. The output parameters,
2483 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2486 Return TRUE if UNIT contains SYM, and no errors were encountered;
2490 comp_unit_find_line (struct comp_unit
*unit
,
2493 const char **filename_ptr
,
2494 unsigned int *linenumber_ptr
,
2495 struct dwarf2_debug
*stash
)
2497 if (!comp_unit_maybe_decode_line_info (unit
, stash
))
2500 if (sym
->flags
& BSF_FUNCTION
)
2501 return lookup_symbol_in_function_table (unit
, sym
, addr
,
2505 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
2510 static struct funcinfo
*
2511 reverse_funcinfo_list (struct funcinfo
*head
)
2513 struct funcinfo
*rhead
;
2514 struct funcinfo
*temp
;
2516 for (rhead
= NULL
; head
; head
= temp
)
2518 temp
= head
->prev_func
;
2519 head
->prev_func
= rhead
;
2525 static struct varinfo
*
2526 reverse_varinfo_list (struct varinfo
*head
)
2528 struct varinfo
*rhead
;
2529 struct varinfo
*temp
;
2531 for (rhead
= NULL
; head
; head
= temp
)
2533 temp
= head
->prev_var
;
2534 head
->prev_var
= rhead
;
2540 /* Extract all interesting funcinfos and varinfos of a compilation
2541 unit into hash tables for faster lookup. Returns TRUE if no
2542 errors were enountered; FALSE otherwise. */
2545 comp_unit_hash_info (struct dwarf2_debug
*stash
,
2546 struct comp_unit
*unit
,
2547 struct info_hash_table
*funcinfo_hash_table
,
2548 struct info_hash_table
*varinfo_hash_table
)
2550 struct funcinfo
* each_func
;
2551 struct varinfo
* each_var
;
2552 bfd_boolean okay
= TRUE
;
2554 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
2556 if (!comp_unit_maybe_decode_line_info (unit
, stash
))
2559 BFD_ASSERT (!unit
->cached
);
2561 /* To preserve the original search order, we went to visit the function
2562 infos in the reversed order of the list. However, making the list
2563 bi-directional use quite a bit of extra memory. So we reverse
2564 the list first, traverse the list in the now reversed order and
2565 finally reverse the list again to get back the original order. */
2566 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
2567 for (each_func
= unit
->function_table
;
2569 each_func
= each_func
->prev_func
)
2571 /* Skip nameless functions. */
2572 if (each_func
->name
)
2573 /* There is no need to copy name string into hash table as
2574 name string is either in the dwarf string buffer or
2575 info in the stash. */
2576 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
2577 (void*) each_func
, FALSE
);
2579 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
2583 /* We do the same for variable infos. */
2584 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
2585 for (each_var
= unit
->variable_table
;
2587 each_var
= each_var
->prev_var
)
2589 /* Skip stack vars and vars with no files or names. */
2590 if (each_var
->stack
== 0
2591 && each_var
->file
!= NULL
2592 && each_var
->name
!= NULL
)
2593 /* There is no need to copy name string into hash table as
2594 name string is either in the dwarf string buffer or
2595 info in the stash. */
2596 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
2597 (void*) each_var
, FALSE
);
2600 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
2601 unit
->cached
= TRUE
;
2605 /* Locate a section in a BFD containing debugging info. The search starts
2606 from the section after AFTER_SEC, or from the first section in the BFD if
2607 AFTER_SEC is NULL. The search works by examining the names of the
2608 sections. There are two permissiable names. The first is .debug_info.
2609 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2610 This is a variation on the .debug_info section which has a checksum
2611 describing the contents appended onto the name. This allows the linker to
2612 identify and discard duplicate debugging sections for different
2613 compilation units. */
2614 #define DWARF2_DEBUG_INFO ".debug_info"
2615 #define DWARF2_COMPRESSED_DEBUG_INFO ".zdebug_info"
2616 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2619 find_debug_info (bfd
*abfd
, asection
*after_sec
)
2623 msec
= after_sec
!= NULL
? after_sec
->next
: abfd
->sections
;
2627 if (strcmp (msec
->name
, DWARF2_DEBUG_INFO
) == 0)
2630 if (strcmp (msec
->name
, DWARF2_COMPRESSED_DEBUG_INFO
) == 0)
2633 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
2642 /* Unset vmas for adjusted sections in STASH. */
2645 unset_sections (struct dwarf2_debug
*stash
)
2648 struct adjusted_section
*p
;
2650 i
= stash
->adjusted_section_count
;
2651 p
= stash
->adjusted_sections
;
2652 for (; i
> 0; i
--, p
++)
2653 p
->section
->vma
= 0;
2656 /* Set unique VMAs for loadable and DWARF sections in ABFD and save
2657 VMAs in STASH for unset_sections. */
2660 place_sections (bfd
*abfd
, struct dwarf2_debug
*stash
)
2662 struct adjusted_section
*p
;
2665 if (stash
->adjusted_section_count
!= 0)
2667 i
= stash
->adjusted_section_count
;
2668 p
= stash
->adjusted_sections
;
2669 for (; i
> 0; i
--, p
++)
2670 p
->section
->vma
= p
->adj_vma
;
2675 bfd_vma last_vma
= 0, last_dwarf
= 0;
2679 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
2687 /* We need to adjust the VMAs of any .debug_info sections.
2688 Skip compressed ones, since no relocations could target
2689 them - they should not appear in object files anyway. */
2690 if (strcmp (sect
->name
, DWARF2_DEBUG_INFO
) == 0)
2692 else if (CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
))
2697 if (!is_debug_info
&& (sect
->flags
& SEC_LOAD
) == 0)
2700 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
2707 amt
= i
* sizeof (struct adjusted_section
);
2708 p
= (struct adjusted_section
*) bfd_zalloc (abfd
, amt
);
2712 stash
->adjusted_sections
= p
;
2713 stash
->adjusted_section_count
= i
;
2715 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
2723 /* We need to adjust the VMAs of any .debug_info sections.
2724 Skip compressed ones, since no relocations could target
2725 them - they should not appear in object files anyway. */
2726 if (strcmp (sect
->name
, DWARF2_DEBUG_INFO
) == 0)
2728 else if (CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
))
2733 if (!is_debug_info
&& (sect
->flags
& SEC_LOAD
) == 0)
2736 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
2743 BFD_ASSERT (sect
->alignment_power
== 0);
2744 sect
->vma
= last_dwarf
;
2747 else if (last_vma
!= 0)
2749 /* Align the new address to the current section
2751 last_vma
= ((last_vma
2752 + ~((bfd_vma
) -1 << sect
->alignment_power
))
2753 & ((bfd_vma
) -1 << sect
->alignment_power
));
2754 sect
->vma
= last_vma
;
2755 last_vma
+= sect
->vma
+ sz
;
2758 last_vma
+= sect
->vma
+ sz
;
2760 p
->adj_vma
= sect
->vma
;
2769 /* Look up a funcinfo by name using the given info hash table. If found,
2770 also update the locations pointed to by filename_ptr and linenumber_ptr.
2772 This function returns TRUE if a funcinfo that matches the given symbol
2773 and address is found with any error; otherwise it returns FALSE. */
2776 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
2779 const char **filename_ptr
,
2780 unsigned int *linenumber_ptr
)
2782 struct funcinfo
* each_func
;
2783 struct funcinfo
* best_fit
= NULL
;
2784 struct info_list_node
*node
;
2785 struct arange
*arange
;
2786 const char *name
= bfd_asymbol_name (sym
);
2787 asection
*sec
= bfd_get_section (sym
);
2789 for (node
= lookup_info_hash_table (hash_table
, name
);
2793 each_func
= (struct funcinfo
*) node
->info
;
2794 for (arange
= &each_func
->arange
;
2796 arange
= arange
->next
)
2798 if ((!each_func
->sec
|| each_func
->sec
== sec
)
2799 && addr
>= arange
->low
2800 && addr
< arange
->high
2802 || ((arange
->high
- arange
->low
)
2803 < (best_fit
->arange
.high
- best_fit
->arange
.low
))))
2804 best_fit
= each_func
;
2810 best_fit
->sec
= sec
;
2811 *filename_ptr
= best_fit
->file
;
2812 *linenumber_ptr
= best_fit
->line
;
2819 /* Look up a varinfo by name using the given info hash table. If found,
2820 also update the locations pointed to by filename_ptr and linenumber_ptr.
2822 This function returns TRUE if a varinfo that matches the given symbol
2823 and address is found with any error; otherwise it returns FALSE. */
2826 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
2829 const char **filename_ptr
,
2830 unsigned int *linenumber_ptr
)
2832 const char *name
= bfd_asymbol_name (sym
);
2833 asection
*sec
= bfd_get_section (sym
);
2834 struct varinfo
* each
;
2835 struct info_list_node
*node
;
2837 for (node
= lookup_info_hash_table (hash_table
, name
);
2841 each
= (struct varinfo
*) node
->info
;
2842 if (each
->addr
== addr
2843 && (!each
->sec
|| each
->sec
== sec
))
2846 *filename_ptr
= each
->file
;
2847 *linenumber_ptr
= each
->line
;
2855 /* Update the funcinfo and varinfo info hash tables if they are
2856 not up to date. Returns TRUE if there is no error; otherwise
2857 returns FALSE and disable the info hash tables. */
2860 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
2862 struct comp_unit
*each
;
2864 /* Exit if hash tables are up-to-date. */
2865 if (stash
->all_comp_units
== stash
->hash_units_head
)
2868 if (stash
->hash_units_head
)
2869 each
= stash
->hash_units_head
->prev_unit
;
2871 each
= stash
->last_comp_unit
;
2875 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
2876 stash
->varinfo_hash_table
))
2878 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
2881 each
= each
->prev_unit
;
2884 stash
->hash_units_head
= stash
->all_comp_units
;
2888 /* Check consistency of info hash tables. This is for debugging only. */
2890 static void ATTRIBUTE_UNUSED
2891 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
2893 struct comp_unit
*each_unit
;
2894 struct funcinfo
*each_func
;
2895 struct varinfo
*each_var
;
2896 struct info_list_node
*node
;
2899 for (each_unit
= stash
->all_comp_units
;
2901 each_unit
= each_unit
->next_unit
)
2903 for (each_func
= each_unit
->function_table
;
2905 each_func
= each_func
->prev_func
)
2907 if (!each_func
->name
)
2909 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
2913 while (node
&& !found
)
2915 found
= node
->info
== each_func
;
2921 for (each_var
= each_unit
->variable_table
;
2923 each_var
= each_var
->prev_var
)
2925 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
2927 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
2931 while (node
&& !found
)
2933 found
= node
->info
== each_var
;
2941 /* Check to see if we want to enable the info hash tables, which consume
2942 quite a bit of memory. Currently we only check the number times
2943 bfd_dwarf2_find_line is called. In the future, we may also want to
2944 take the number of symbols into account. */
2947 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
2949 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
2951 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
2954 /* FIXME: Maybe we should check the reduce_memory_overheads
2955 and optimize fields in the bfd_link_info structure ? */
2957 /* Create hash tables. */
2958 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
2959 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
2960 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
2962 /* Turn off info hashes if any allocation above fails. */
2963 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
2966 /* We need a forced update so that the info hash tables will
2967 be created even though there is no compilation unit. That
2968 happens if STASH_INFO_HASH_TRIGGER is 0. */
2969 stash_maybe_update_info_hash_tables (stash
);
2970 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
2973 /* Find the file and line associated with a symbol and address using the
2974 info hash tables of a stash. If there is a match, the function returns
2975 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
2976 otherwise it returns FALSE. */
2979 stash_find_line_fast (struct dwarf2_debug
*stash
,
2982 const char **filename_ptr
,
2983 unsigned int *linenumber_ptr
)
2985 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
2987 if (sym
->flags
& BSF_FUNCTION
)
2988 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
2989 filename_ptr
, linenumber_ptr
);
2990 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
2991 filename_ptr
, linenumber_ptr
);
2994 /* Find the source code location of SYMBOL. If SYMBOL is NULL
2995 then find the nearest source code location corresponding to
2996 the address SECTION + OFFSET.
2997 Returns TRUE if the line is found without error and fills in
2998 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
2999 NULL the FUNCTIONNAME_PTR is also filled in.
3000 SYMBOLS contains the symbol table for ABFD.
3001 ADDR_SIZE is the number of bytes in the initial .debug_info length
3002 field and in the abbreviation offset, or zero to indicate that the
3003 default value should be used. */
3006 find_line (bfd
*abfd
,
3011 const char **filename_ptr
,
3012 const char **functionname_ptr
,
3013 unsigned int *linenumber_ptr
,
3014 unsigned int addr_size
,
3017 /* Read each compilation unit from the section .debug_info, and check
3018 to see if it contains the address we are searching for. If yes,
3019 lookup the address, and return the line number info. If no, go
3020 on to the next compilation unit.
3022 We keep a list of all the previously read compilation units, and
3023 a pointer to the next un-read compilation unit. Check the
3024 previously read units before reading more. */
3025 struct dwarf2_debug
*stash
;
3026 /* What address are we looking for? */
3028 struct comp_unit
* each
;
3029 bfd_vma found
= FALSE
;
3030 bfd_boolean do_line
;
3032 stash
= (struct dwarf2_debug
*) *pinfo
;
3036 bfd_size_type amt
= sizeof (struct dwarf2_debug
);
3038 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
3043 /* In a relocatable file, 2 functions may have the same address.
3044 We change the section vma so that they won't overlap. */
3045 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
3047 if (! place_sections (abfd
, stash
))
3051 do_line
= (section
== NULL
3053 && functionname_ptr
== NULL
3057 addr
= symbol
->value
;
3058 section
= bfd_get_section (symbol
);
3060 else if (section
!= NULL
3061 && functionname_ptr
!= NULL
3067 if (section
->output_section
)
3068 addr
+= section
->output_section
->vma
+ section
->output_offset
;
3070 addr
+= section
->vma
;
3071 *filename_ptr
= NULL
;
3073 *functionname_ptr
= NULL
;
3074 *linenumber_ptr
= 0;
3079 bfd_size_type total_size
;
3084 msec
= find_debug_info (abfd
, NULL
);
3087 char * debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
3089 if (debug_filename
== NULL
)
3090 /* No dwarf2 info, and no gnu_debuglink to follow.
3091 Note that at this point the stash has been allocated, but
3092 contains zeros. This lets future calls to this function
3093 fail more quickly. */
3096 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
3097 || ! bfd_check_format (debug_bfd
, bfd_object
)
3098 || (msec
= find_debug_info (debug_bfd
, NULL
)) == NULL
)
3101 bfd_close (debug_bfd
);
3102 /* FIXME: Should we report our failure to follow the debuglink ? */
3103 free (debug_filename
);
3110 /* There can be more than one DWARF2 info section in a BFD these
3111 days. First handle the easy case when there's only one. If
3112 there's more than one, try case two: none of the sections is
3113 compressed. In that case, read them all in and produce one
3114 large stash. We do this in two passes - in the first pass we
3115 just accumulate the section sizes, and in the second pass we
3116 read in the section's contents. (The allows us to avoid
3117 reallocing the data as we add sections to the stash.) If
3118 some or all sections are compressed, then do things the slow
3119 way, with a bunch of reallocs. */
3121 if (! find_debug_info (debug_bfd
, msec
))
3123 /* Case 1: only one info section. */
3124 total_size
= msec
->size
;
3125 if (! read_section (debug_bfd
, ".debug_info", ".zdebug_info",
3127 &stash
->info_ptr_memory
, &total_size
))
3132 int all_uncompressed
= 1;
3133 for (total_size
= 0; msec
; msec
= find_debug_info (debug_bfd
, msec
))
3135 total_size
+= msec
->size
;
3136 if (strcmp (msec
->name
, DWARF2_COMPRESSED_DEBUG_INFO
) == 0)
3137 all_uncompressed
= 0;
3139 if (all_uncompressed
)
3141 /* Case 2: multiple sections, but none is compressed. */
3142 stash
->info_ptr_memory
= (bfd_byte
*) bfd_malloc (total_size
);
3143 if (stash
->info_ptr_memory
== NULL
)
3147 for (msec
= find_debug_info (debug_bfd
, NULL
);
3149 msec
= find_debug_info (debug_bfd
, msec
))
3157 if (!(bfd_simple_get_relocated_section_contents
3158 (debug_bfd
, msec
, stash
->info_ptr_memory
+ total_size
,
3167 /* Case 3: multiple sections, some or all compressed. */
3168 stash
->info_ptr_memory
= NULL
;
3170 for (msec
= find_debug_info (debug_bfd
, NULL
);
3172 msec
= find_debug_info (debug_bfd
, msec
))
3174 bfd_size_type size
= msec
->size
;
3175 bfd_byte
*buffer
, *tmp
;
3180 buffer
= (bfd_simple_get_relocated_section_contents
3181 (debug_bfd
, msec
, NULL
, symbols
));
3185 if (strcmp (msec
->name
, DWARF2_COMPRESSED_DEBUG_INFO
) == 0)
3187 if (! bfd_uncompress_section_contents (&buffer
, &size
))
3193 tmp
= (bfd_byte
*) bfd_realloc (stash
->info_ptr_memory
,
3200 stash
->info_ptr_memory
= tmp
;
3201 memcpy (stash
->info_ptr_memory
+ total_size
, buffer
, size
);
3208 stash
->info_ptr
= stash
->info_ptr_memory
;
3209 stash
->info_ptr_end
= stash
->info_ptr
+ total_size
;
3210 stash
->sec
= find_debug_info (debug_bfd
, NULL
);
3211 stash
->sec_info_ptr
= stash
->info_ptr
;
3212 stash
->syms
= symbols
;
3213 stash
->bfd_ptr
= debug_bfd
;
3216 /* A null info_ptr indicates that there is no dwarf2 info
3217 (or that an error occured while setting up the stash). */
3218 if (! stash
->info_ptr
)
3221 stash
->inliner_chain
= NULL
;
3223 /* Check the previously read comp. units first. */
3226 /* The info hash tables use quite a bit of memory. We may not want to
3227 always use them. We use some heuristics to decide if and when to
3229 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
3230 stash_maybe_enable_info_hash_tables (abfd
, stash
);
3232 /* Keep info hash table up to date if they are available. Note that we
3233 may disable the hash tables if there is any error duing update. */
3234 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
3235 stash_maybe_update_info_hash_tables (stash
);
3237 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
3239 found
= stash_find_line_fast (stash
, symbol
, addr
, filename_ptr
,
3246 /* Check the previously read comp. units first. */
3247 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
3248 if ((symbol
->flags
& BSF_FUNCTION
) == 0
3249 || comp_unit_contains_address (each
, addr
))
3251 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
3252 linenumber_ptr
, stash
);
3260 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
3262 found
= (comp_unit_contains_address (each
, addr
)
3263 && comp_unit_find_nearest_line (each
, addr
,
3273 /* The DWARF2 spec says that the initial length field, and the
3274 offset of the abbreviation table, should both be 4-byte values.
3275 However, some compilers do things differently. */
3278 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
3280 /* Read each remaining comp. units checking each as they are read. */
3281 while (stash
->info_ptr
< stash
->info_ptr_end
)
3284 unsigned int offset_size
= addr_size
;
3285 bfd_byte
*info_ptr_unit
= stash
->info_ptr
;
3287 length
= read_4_bytes (stash
->bfd_ptr
, stash
->info_ptr
);
3288 /* A 0xffffff length is the DWARF3 way of indicating
3289 we use 64-bit offsets, instead of 32-bit offsets. */
3290 if (length
== 0xffffffff)
3293 length
= read_8_bytes (stash
->bfd_ptr
, stash
->info_ptr
+ 4);
3294 stash
->info_ptr
+= 12;
3296 /* A zero length is the IRIX way of indicating 64-bit offsets,
3297 mostly because the 64-bit length will generally fit in 32
3298 bits, and the endianness helps. */
3299 else if (length
== 0)
3302 length
= read_4_bytes (stash
->bfd_ptr
, stash
->info_ptr
+ 4);
3303 stash
->info_ptr
+= 8;
3305 /* In the absence of the hints above, we assume 32-bit DWARF2
3306 offsets even for targets with 64-bit addresses, because:
3307 a) most of the time these targets will not have generated
3308 more than 2Gb of debug info and so will not need 64-bit
3311 b) if they do use 64-bit offsets but they are not using
3312 the size hints that are tested for above then they are
3313 not conforming to the DWARF3 standard anyway. */
3314 else if (addr_size
== 8)
3317 stash
->info_ptr
+= 4;
3320 stash
->info_ptr
+= 4;
3324 each
= parse_comp_unit (stash
, length
, info_ptr_unit
,
3327 /* The dwarf information is damaged, don't trust it any
3330 stash
->info_ptr
+= length
;
3332 if (stash
->all_comp_units
)
3333 stash
->all_comp_units
->prev_unit
= each
;
3335 stash
->last_comp_unit
= each
;
3337 each
->next_unit
= stash
->all_comp_units
;
3338 stash
->all_comp_units
= each
;
3340 /* DW_AT_low_pc and DW_AT_high_pc are optional for
3341 compilation units. If we don't have them (i.e.,
3342 unit->high == 0), we need to consult the line info table
3343 to see if a compilation unit contains the given
3346 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
3347 || each
->arange
.high
== 0
3348 || comp_unit_contains_address (each
, addr
))
3349 && comp_unit_find_line (each
, symbol
, addr
,
3354 found
= ((each
->arange
.high
== 0
3355 || comp_unit_contains_address (each
, addr
))
3356 && comp_unit_find_nearest_line (each
, addr
,
3362 if ((bfd_vma
) (stash
->info_ptr
- stash
->sec_info_ptr
)
3363 == stash
->sec
->size
)
3365 stash
->sec
= find_debug_info (stash
->bfd_ptr
, stash
->sec
);
3366 stash
->sec_info_ptr
= stash
->info_ptr
;
3375 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
3376 unset_sections (stash
);
3381 /* The DWARF2 version of find_nearest_line.
3382 Return TRUE if the line is found without error. */
3385 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
3389 const char **filename_ptr
,
3390 const char **functionname_ptr
,
3391 unsigned int *linenumber_ptr
,
3392 unsigned int addr_size
,
3395 return find_line (abfd
, section
, offset
, NULL
, symbols
, filename_ptr
,
3396 functionname_ptr
, linenumber_ptr
, addr_size
,
3400 /* The DWARF2 version of find_line.
3401 Return TRUE if the line is found without error. */
3404 _bfd_dwarf2_find_line (bfd
*abfd
,
3407 const char **filename_ptr
,
3408 unsigned int *linenumber_ptr
,
3409 unsigned int addr_size
,
3412 return find_line (abfd
, NULL
, 0, symbol
, symbols
, filename_ptr
,
3413 NULL
, linenumber_ptr
, addr_size
,
3418 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
3419 const char **filename_ptr
,
3420 const char **functionname_ptr
,
3421 unsigned int *linenumber_ptr
,
3424 struct dwarf2_debug
*stash
;
3426 stash
= (struct dwarf2_debug
*) *pinfo
;
3429 struct funcinfo
*func
= stash
->inliner_chain
;
3431 if (func
&& func
->caller_func
)
3433 *filename_ptr
= func
->caller_file
;
3434 *functionname_ptr
= func
->caller_func
->name
;
3435 *linenumber_ptr
= func
->caller_line
;
3436 stash
->inliner_chain
= func
->caller_func
;
3445 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
)
3447 struct comp_unit
*each
;
3448 struct dwarf2_debug
*stash
;
3450 if (abfd
== NULL
|| elf_tdata (abfd
) == NULL
)
3453 stash
= (struct dwarf2_debug
*) elf_tdata (abfd
)->dwarf2_find_line_info
;
3458 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
3460 struct abbrev_info
**abbrevs
= each
->abbrevs
;
3461 struct funcinfo
*function_table
= each
->function_table
;
3462 struct varinfo
*variable_table
= each
->variable_table
;
3465 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
3467 struct abbrev_info
*abbrev
= abbrevs
[i
];
3471 free (abbrev
->attrs
);
3472 abbrev
= abbrev
->next
;
3476 if (each
->line_table
)
3478 free (each
->line_table
->dirs
);
3479 free (each
->line_table
->files
);
3482 while (function_table
)
3484 if (function_table
->file
)
3486 free (function_table
->file
);
3487 function_table
->file
= NULL
;
3490 if (function_table
->caller_file
)
3492 free (function_table
->caller_file
);
3493 function_table
->caller_file
= NULL
;
3495 function_table
= function_table
->prev_func
;
3498 while (variable_table
)
3500 if (variable_table
->file
)
3502 free (variable_table
->file
);
3503 variable_table
->file
= NULL
;
3506 variable_table
= variable_table
->prev_var
;
3510 if (stash
->dwarf_abbrev_buffer
)
3511 free (stash
->dwarf_abbrev_buffer
);
3512 if (stash
->dwarf_line_buffer
)
3513 free (stash
->dwarf_line_buffer
);
3514 if (stash
->dwarf_str_buffer
)
3515 free (stash
->dwarf_str_buffer
);
3516 if (stash
->dwarf_ranges_buffer
)
3517 free (stash
->dwarf_ranges_buffer
);
3518 if (stash
->info_ptr_memory
)
3519 free (stash
->info_ptr_memory
);