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
);
654 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
655 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
656 abbrev_ptr
+= bytes_read
;
658 /* Loop until we reach an abbrev number of 0. */
659 while (abbrev_number
)
661 amt
= sizeof (struct abbrev_info
);
662 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
664 /* Read in abbrev header. */
665 cur_abbrev
->number
= abbrev_number
;
666 cur_abbrev
->tag
= (enum dwarf_tag
)
667 read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
668 abbrev_ptr
+= bytes_read
;
669 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
);
672 /* Now read in declarations. */
673 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
674 abbrev_ptr
+= bytes_read
;
675 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
676 abbrev_ptr
+= bytes_read
;
680 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
682 struct attr_abbrev
*tmp
;
684 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
685 amt
*= sizeof (struct attr_abbrev
);
686 tmp
= (struct attr_abbrev
*) bfd_realloc (cur_abbrev
->attrs
, amt
);
691 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
693 struct abbrev_info
*abbrev
= abbrevs
[i
];
697 free (abbrev
->attrs
);
698 abbrev
= abbrev
->next
;
703 cur_abbrev
->attrs
= tmp
;
706 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
707 = (enum dwarf_attribute
) abbrev_name
;
708 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
++].form
709 = (enum dwarf_form
) abbrev_form
;
710 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
711 abbrev_ptr
+= bytes_read
;
712 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
713 abbrev_ptr
+= bytes_read
;
716 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
717 cur_abbrev
->next
= abbrevs
[hash_number
];
718 abbrevs
[hash_number
] = cur_abbrev
;
720 /* Get next abbreviation.
721 Under Irix6 the abbreviations for a compilation unit are not
722 always properly terminated with an abbrev number of 0.
723 Exit loop if we encounter an abbreviation which we have
724 already read (which means we are about to read the abbreviations
725 for the next compile unit) or if the end of the abbreviation
727 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
728 >= stash
->dwarf_abbrev_size
)
730 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
731 abbrev_ptr
+= bytes_read
;
732 if (lookup_abbrev (abbrev_number
,abbrevs
) != NULL
)
739 /* Read an attribute value described by an attribute form. */
742 read_attribute_value (struct attribute
*attr
,
744 struct comp_unit
*unit
,
747 bfd
*abfd
= unit
->abfd
;
748 unsigned int bytes_read
;
749 struct dwarf_block
*blk
;
752 attr
->form
= (enum dwarf_form
) form
;
756 case DW_FORM_ref_addr
:
757 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
759 if (unit
->version
== 3)
761 if (unit
->offset_size
== 4)
762 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
);
764 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
);
765 info_ptr
+= unit
->offset_size
;
770 attr
->u
.val
= read_address (unit
, info_ptr
);
771 info_ptr
+= unit
->addr_size
;
774 amt
= sizeof (struct dwarf_block
);
775 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
776 blk
->size
= read_2_bytes (abfd
, info_ptr
);
778 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
779 info_ptr
+= blk
->size
;
783 amt
= sizeof (struct dwarf_block
);
784 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
785 blk
->size
= read_4_bytes (abfd
, info_ptr
);
787 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
788 info_ptr
+= blk
->size
;
792 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
);
796 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
);
800 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
804 attr
->u
.str
= read_string (abfd
, info_ptr
, &bytes_read
);
805 info_ptr
+= bytes_read
;
808 attr
->u
.str
= read_indirect_string (unit
, info_ptr
, &bytes_read
);
809 info_ptr
+= bytes_read
;
812 amt
= sizeof (struct dwarf_block
);
813 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
814 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
815 info_ptr
+= bytes_read
;
816 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
817 info_ptr
+= blk
->size
;
821 amt
= sizeof (struct dwarf_block
);
822 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
823 blk
->size
= read_1_byte (abfd
, info_ptr
);
825 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
826 info_ptr
+= blk
->size
;
830 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
834 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
838 attr
->u
.sval
= read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
839 info_ptr
+= bytes_read
;
842 attr
->u
.val
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
843 info_ptr
+= bytes_read
;
846 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
850 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
);
854 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
);
858 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
861 case DW_FORM_ref_sig8
:
862 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
865 case DW_FORM_ref_udata
:
866 attr
->u
.val
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
867 info_ptr
+= bytes_read
;
869 case DW_FORM_indirect
:
870 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
871 info_ptr
+= bytes_read
;
872 info_ptr
= read_attribute_value (attr
, form
, unit
, info_ptr
);
875 (*_bfd_error_handler
) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
877 bfd_set_error (bfd_error_bad_value
);
882 /* Read an attribute described by an abbreviated attribute. */
885 read_attribute (struct attribute
*attr
,
886 struct attr_abbrev
*abbrev
,
887 struct comp_unit
*unit
,
890 attr
->name
= abbrev
->name
;
891 info_ptr
= read_attribute_value (attr
, abbrev
->form
, unit
, info_ptr
);
895 /* Source line information table routines. */
897 #define FILE_ALLOC_CHUNK 5
898 #define DIR_ALLOC_CHUNK 5
902 struct line_info
* prev_line
;
907 int end_sequence
; /* End of (sequential) code sequence. */
921 struct line_sequence
* prev_sequence
;
922 struct line_info
* last_line
; /* Largest VMA. */
925 struct line_info_table
928 unsigned int num_files
;
929 unsigned int num_dirs
;
930 unsigned int num_sequences
;
933 struct fileinfo
* files
;
934 struct line_sequence
* sequences
;
935 struct line_info
* lcl_head
; /* Local head; used in 'add_line_info'. */
938 /* Remember some information about each function. If the function is
939 inlined (DW_TAG_inlined_subroutine) it may have two additional
940 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
941 source code location where this function was inlined. */
945 struct funcinfo
*prev_func
; /* Pointer to previous function in list of all functions */
946 struct funcinfo
*caller_func
; /* Pointer to function one scope higher */
947 char *caller_file
; /* Source location file name where caller_func inlines this func */
948 int caller_line
; /* Source location line number where caller_func inlines this func */
949 char *file
; /* Source location file name */
950 int line
; /* Source location line number */
953 struct arange arange
;
954 asection
*sec
; /* Where the symbol is defined */
959 /* Pointer to previous variable in list of all variables */
960 struct varinfo
*prev_var
;
961 /* Source location file name */
963 /* Source location line number */
968 /* Where the symbol is defined */
970 /* Is this a stack variable? */
971 unsigned int stack
: 1;
974 /* Return TRUE if NEW_LINE should sort after LINE. */
976 static inline bfd_boolean
977 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
979 return (new_line
->address
> line
->address
980 || (new_line
->address
== line
->address
981 && new_line
->end_sequence
< line
->end_sequence
));
985 /* Adds a new entry to the line_info list in the line_info_table, ensuring
986 that the list is sorted. Note that the line_info list is sorted from
987 highest to lowest VMA (with possible duplicates); that is,
988 line_info->prev_line always accesses an equal or smaller VMA. */
991 add_line_info (struct line_info_table
*table
,
998 bfd_size_type amt
= sizeof (struct line_info
);
999 struct line_sequence
* seq
= table
->sequences
;
1000 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
1002 /* Set member data of 'info'. */
1003 info
->address
= address
;
1005 info
->column
= column
;
1006 info
->end_sequence
= end_sequence
;
1008 if (filename
&& filename
[0])
1010 info
->filename
= (char *) bfd_alloc (table
->abfd
, strlen (filename
) + 1);
1012 strcpy (info
->filename
, filename
);
1015 info
->filename
= NULL
;
1017 /* Find the correct location for 'info'. Normally we will receive
1018 new line_info data 1) in order and 2) with increasing VMAs.
1019 However some compilers break the rules (cf. decode_line_info) and
1020 so we include some heuristics for quickly finding the correct
1021 location for 'info'. In particular, these heuristics optimize for
1022 the common case in which the VMA sequence that we receive is a
1023 list of locally sorted VMAs such as
1024 p...z a...j (where a < j < p < z)
1026 Note: table->lcl_head is used to head an *actual* or *possible*
1027 sub-sequence within the list (such as a...j) that is not directly
1028 headed by table->last_line
1030 Note: we may receive duplicate entries from 'decode_line_info'. */
1033 && seq
->last_line
->address
== address
1034 && seq
->last_line
->end_sequence
== end_sequence
)
1036 /* We only keep the last entry with the same address and end
1037 sequence. See PR ld/4986. */
1038 if (table
->lcl_head
== seq
->last_line
)
1039 table
->lcl_head
= info
;
1040 info
->prev_line
= seq
->last_line
->prev_line
;
1041 seq
->last_line
= info
;
1043 else if (!seq
|| seq
->last_line
->end_sequence
)
1045 /* Start a new line sequence. */
1046 amt
= sizeof (struct line_sequence
);
1047 seq
= (struct line_sequence
*) bfd_malloc (amt
);
1048 seq
->low_pc
= address
;
1049 seq
->prev_sequence
= table
->sequences
;
1050 seq
->last_line
= info
;
1051 table
->lcl_head
= info
;
1052 table
->sequences
= seq
;
1053 table
->num_sequences
++;
1055 else if (new_line_sorts_after (info
, seq
->last_line
))
1057 /* Normal case: add 'info' to the beginning of the current sequence. */
1058 info
->prev_line
= seq
->last_line
;
1059 seq
->last_line
= info
;
1061 /* lcl_head: initialize to head a *possible* sequence at the end. */
1062 if (!table
->lcl_head
)
1063 table
->lcl_head
= info
;
1065 else if (!new_line_sorts_after (info
, table
->lcl_head
)
1066 && (!table
->lcl_head
->prev_line
1067 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
1069 /* Abnormal but easy: lcl_head is the head of 'info'. */
1070 info
->prev_line
= table
->lcl_head
->prev_line
;
1071 table
->lcl_head
->prev_line
= info
;
1075 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1076 are valid heads for 'info'. Reset 'lcl_head'. */
1077 struct line_info
* li2
= seq
->last_line
; /* Always non-NULL. */
1078 struct line_info
* li1
= li2
->prev_line
;
1082 if (!new_line_sorts_after (info
, li2
)
1083 && new_line_sorts_after (info
, li1
))
1086 li2
= li1
; /* always non-NULL */
1087 li1
= li1
->prev_line
;
1089 table
->lcl_head
= li2
;
1090 info
->prev_line
= table
->lcl_head
->prev_line
;
1091 table
->lcl_head
->prev_line
= info
;
1092 if (address
< seq
->low_pc
)
1093 seq
->low_pc
= address
;
1097 /* Extract a fully qualified filename from a line info table.
1098 The returned string has been malloc'ed and it is the caller's
1099 responsibility to free it. */
1102 concat_filename (struct line_info_table
*table
, unsigned int file
)
1106 if (file
- 1 >= table
->num_files
)
1108 /* FILE == 0 means unknown. */
1110 (*_bfd_error_handler
)
1111 (_("Dwarf Error: mangled line number section (bad file number)."));
1112 return strdup ("<unknown>");
1115 filename
= table
->files
[file
- 1].name
;
1117 if (!IS_ABSOLUTE_PATH (filename
))
1119 char *dir_name
= NULL
;
1120 char *subdir_name
= NULL
;
1124 if (table
->files
[file
- 1].dir
)
1125 subdir_name
= table
->dirs
[table
->files
[file
- 1].dir
- 1];
1127 if (!subdir_name
|| !IS_ABSOLUTE_PATH (subdir_name
))
1128 dir_name
= table
->comp_dir
;
1132 dir_name
= subdir_name
;
1137 return strdup (filename
);
1139 len
= strlen (dir_name
) + strlen (filename
) + 2;
1143 len
+= strlen (subdir_name
) + 1;
1144 name
= (char *) bfd_malloc (len
);
1146 sprintf (name
, "%s/%s/%s", dir_name
, subdir_name
, filename
);
1150 name
= (char *) bfd_malloc (len
);
1152 sprintf (name
, "%s/%s", dir_name
, filename
);
1158 return strdup (filename
);
1162 arange_add (bfd
*abfd
, struct arange
*first_arange
, bfd_vma low_pc
, bfd_vma high_pc
)
1164 struct arange
*arange
;
1166 /* If the first arange is empty, use it. */
1167 if (first_arange
->high
== 0)
1169 first_arange
->low
= low_pc
;
1170 first_arange
->high
= high_pc
;
1174 /* Next see if we can cheaply extend an existing range. */
1175 arange
= first_arange
;
1178 if (low_pc
== arange
->high
)
1180 arange
->high
= high_pc
;
1183 if (high_pc
== arange
->low
)
1185 arange
->low
= low_pc
;
1188 arange
= arange
->next
;
1192 /* Need to allocate a new arange and insert it into the arange list.
1193 Order isn't significant, so just insert after the first arange. */
1194 arange
= (struct arange
*) bfd_zalloc (abfd
, sizeof (*arange
));
1195 arange
->low
= low_pc
;
1196 arange
->high
= high_pc
;
1197 arange
->next
= first_arange
->next
;
1198 first_arange
->next
= arange
;
1201 /* Compare function for line sequences. */
1204 compare_sequences (const void* a
, const void* b
)
1206 const struct line_sequence
* seq1
= a
;
1207 const struct line_sequence
* seq2
= b
;
1209 /* Sort by low_pc as the primary key. */
1210 if (seq1
->low_pc
< seq2
->low_pc
)
1212 if (seq1
->low_pc
> seq2
->low_pc
)
1215 /* If low_pc values are equal, sort in reverse order of
1216 high_pc, so that the largest region comes first. */
1217 if (seq1
->last_line
->address
< seq2
->last_line
->address
)
1219 if (seq1
->last_line
->address
> seq2
->last_line
->address
)
1225 /* Sort the line sequences for quick lookup. */
1228 sort_line_sequences (struct line_info_table
* table
)
1231 struct line_sequence
* sequences
;
1232 struct line_sequence
* seq
;
1234 unsigned int num_sequences
= table
->num_sequences
;
1235 bfd_vma last_high_pc
;
1237 if (num_sequences
== 0)
1240 /* Allocate space for an array of sequences. */
1241 amt
= sizeof (struct line_sequence
) * num_sequences
;
1242 sequences
= (struct line_sequence
*) bfd_alloc (table
->abfd
, amt
);
1244 /* Copy the linked list into the array, freeing the original nodes. */
1245 seq
= table
->sequences
;
1246 for (n
= 0; n
< num_sequences
; n
++)
1248 struct line_sequence
* last_seq
= seq
;
1251 sequences
[n
].low_pc
= seq
->low_pc
;
1252 sequences
[n
].prev_sequence
= NULL
;
1253 sequences
[n
].last_line
= seq
->last_line
;
1254 seq
= seq
->prev_sequence
;
1257 BFD_ASSERT (seq
== NULL
);
1259 qsort (sequences
, n
, sizeof (struct line_sequence
), compare_sequences
);
1261 /* Make the list binary-searchable by trimming overlapping entries
1262 and removing nested entries. */
1264 last_high_pc
= sequences
[0].last_line
->address
;
1265 for (n
= 1; n
< table
->num_sequences
; n
++)
1267 if (sequences
[n
].low_pc
< last_high_pc
)
1269 if (sequences
[n
].last_line
->address
<= last_high_pc
)
1270 /* Skip nested entries. */
1273 /* Trim overlapping entries. */
1274 sequences
[n
].low_pc
= last_high_pc
;
1276 last_high_pc
= sequences
[n
].last_line
->address
;
1277 if (n
> num_sequences
)
1279 /* Close up the gap. */
1280 sequences
[num_sequences
].low_pc
= sequences
[n
].low_pc
;
1281 sequences
[num_sequences
].last_line
= sequences
[n
].last_line
;
1286 table
->sequences
= sequences
;
1287 table
->num_sequences
= num_sequences
;
1290 /* Decode the line number information for UNIT. */
1292 static struct line_info_table
*
1293 decode_line_info (struct comp_unit
*unit
, struct dwarf2_debug
*stash
)
1295 bfd
*abfd
= unit
->abfd
;
1296 struct line_info_table
* table
;
1299 struct line_head lh
;
1300 unsigned int i
, bytes_read
, offset_size
;
1301 char *cur_file
, *cur_dir
;
1302 unsigned char op_code
, extended_op
, adj_opcode
;
1305 if (! read_section (abfd
, ".debug_line", ".zdebug_line",
1306 stash
->syms
, unit
->line_offset
,
1307 &stash
->dwarf_line_buffer
, &stash
->dwarf_line_size
))
1310 amt
= sizeof (struct line_info_table
);
1311 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
1313 table
->comp_dir
= unit
->comp_dir
;
1315 table
->num_files
= 0;
1316 table
->files
= NULL
;
1318 table
->num_dirs
= 0;
1321 table
->num_sequences
= 0;
1322 table
->sequences
= NULL
;
1324 table
->lcl_head
= NULL
;
1326 line_ptr
= stash
->dwarf_line_buffer
+ unit
->line_offset
;
1328 /* Read in the prologue. */
1329 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
1332 if (lh
.total_length
== 0xffffffff)
1334 lh
.total_length
= read_8_bytes (abfd
, line_ptr
);
1338 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
1340 /* Handle (non-standard) 64-bit DWARF2 formats. */
1341 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
1345 line_end
= line_ptr
+ lh
.total_length
;
1346 lh
.version
= read_2_bytes (abfd
, line_ptr
);
1348 if (offset_size
== 4)
1349 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
);
1351 lh
.prologue_length
= read_8_bytes (abfd
, line_ptr
);
1352 line_ptr
+= offset_size
;
1353 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
);
1355 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
);
1357 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
);
1359 lh
.line_range
= read_1_byte (abfd
, line_ptr
);
1361 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
);
1363 amt
= lh
.opcode_base
* sizeof (unsigned char);
1364 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
1366 lh
.standard_opcode_lengths
[0] = 1;
1368 for (i
= 1; i
< lh
.opcode_base
; ++i
)
1370 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
);
1374 /* Read directory table. */
1375 while ((cur_dir
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1377 line_ptr
+= bytes_read
;
1379 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1383 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1384 amt
*= sizeof (char *);
1386 tmp
= (char **) bfd_realloc (table
->dirs
, amt
);
1395 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1398 line_ptr
+= bytes_read
;
1400 /* Read file name table. */
1401 while ((cur_file
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1403 line_ptr
+= bytes_read
;
1405 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1407 struct fileinfo
*tmp
;
1409 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1410 amt
*= sizeof (struct fileinfo
);
1412 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1415 free (table
->files
);
1422 table
->files
[table
->num_files
].name
= cur_file
;
1423 table
->files
[table
->num_files
].dir
=
1424 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1425 line_ptr
+= bytes_read
;
1426 table
->files
[table
->num_files
].time
=
1427 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1428 line_ptr
+= bytes_read
;
1429 table
->files
[table
->num_files
].size
=
1430 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1431 line_ptr
+= bytes_read
;
1435 line_ptr
+= bytes_read
;
1437 /* Read the statement sequences until there's nothing left. */
1438 while (line_ptr
< line_end
)
1440 /* State machine registers. */
1441 bfd_vma address
= 0;
1442 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
1443 unsigned int line
= 1;
1444 unsigned int column
= 0;
1445 int is_stmt
= lh
.default_is_stmt
;
1446 int end_sequence
= 0;
1447 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1448 compilers generate address sequences that are wildly out of
1449 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1450 for ia64-Linux). Thus, to determine the low and high
1451 address, we must compare on every DW_LNS_copy, etc. */
1452 bfd_vma low_pc
= (bfd_vma
) -1;
1453 bfd_vma high_pc
= 0;
1455 /* Decode the table. */
1456 while (! end_sequence
)
1458 op_code
= read_1_byte (abfd
, line_ptr
);
1461 if (op_code
>= lh
.opcode_base
)
1463 /* Special operand. */
1464 adj_opcode
= op_code
- lh
.opcode_base
;
1465 address
+= (adj_opcode
/ lh
.line_range
)
1466 * lh
.minimum_instruction_length
;
1467 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
1468 /* Append row to matrix using current values. */
1469 add_line_info (table
, address
, filename
, line
, column
, 0);
1470 if (address
< low_pc
)
1472 if (address
> high_pc
)
1475 else switch (op_code
)
1477 case DW_LNS_extended_op
:
1478 /* Ignore length. */
1480 extended_op
= read_1_byte (abfd
, line_ptr
);
1483 switch (extended_op
)
1485 case DW_LNE_end_sequence
:
1487 add_line_info (table
, address
, filename
, line
, column
,
1489 if (address
< low_pc
)
1491 if (address
> high_pc
)
1493 arange_add (unit
->abfd
, &unit
->arange
, low_pc
, high_pc
);
1495 case DW_LNE_set_address
:
1496 address
= read_address (unit
, line_ptr
);
1497 line_ptr
+= unit
->addr_size
;
1499 case DW_LNE_define_file
:
1500 cur_file
= read_string (abfd
, line_ptr
, &bytes_read
);
1501 line_ptr
+= bytes_read
;
1502 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1504 struct fileinfo
*tmp
;
1506 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1507 amt
*= sizeof (struct fileinfo
);
1508 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1511 free (table
->files
);
1518 table
->files
[table
->num_files
].name
= cur_file
;
1519 table
->files
[table
->num_files
].dir
=
1520 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1521 line_ptr
+= bytes_read
;
1522 table
->files
[table
->num_files
].time
=
1523 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1524 line_ptr
+= bytes_read
;
1525 table
->files
[table
->num_files
].size
=
1526 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1527 line_ptr
+= bytes_read
;
1530 case DW_LNE_set_discriminator
:
1531 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1532 line_ptr
+= bytes_read
;
1535 (*_bfd_error_handler
) (_("Dwarf Error: mangled line number section."));
1536 bfd_set_error (bfd_error_bad_value
);
1538 free (table
->files
);
1544 add_line_info (table
, address
, filename
, line
, column
, 0);
1545 if (address
< low_pc
)
1547 if (address
> high_pc
)
1550 case DW_LNS_advance_pc
:
1551 address
+= lh
.minimum_instruction_length
1552 * read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1553 line_ptr
+= bytes_read
;
1555 case DW_LNS_advance_line
:
1556 line
+= read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
1557 line_ptr
+= bytes_read
;
1559 case DW_LNS_set_file
:
1563 /* The file and directory tables are 0
1564 based, the references are 1 based. */
1565 file
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1566 line_ptr
+= bytes_read
;
1569 filename
= concat_filename (table
, file
);
1572 case DW_LNS_set_column
:
1573 column
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1574 line_ptr
+= bytes_read
;
1576 case DW_LNS_negate_stmt
:
1577 is_stmt
= (!is_stmt
);
1579 case DW_LNS_set_basic_block
:
1581 case DW_LNS_const_add_pc
:
1582 address
+= lh
.minimum_instruction_length
1583 * ((255 - lh
.opcode_base
) / lh
.line_range
);
1585 case DW_LNS_fixed_advance_pc
:
1586 address
+= read_2_bytes (abfd
, line_ptr
);
1590 /* Unknown standard opcode, ignore it. */
1591 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
1593 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1594 line_ptr
+= bytes_read
;
1604 sort_line_sequences (table
);
1609 /* If ADDR is within TABLE set the output parameters and return TRUE,
1610 otherwise return FALSE. The output parameters, FILENAME_PTR and
1611 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1614 lookup_address_in_line_info_table (struct line_info_table
*table
,
1616 const char **filename_ptr
,
1617 unsigned int *linenumber_ptr
)
1619 struct line_sequence
*seq
= NULL
;
1620 struct line_info
*each_line
;
1623 /* Binary search the array of sequences. */
1625 high
= table
->num_sequences
;
1628 mid
= (low
+ high
) / 2;
1629 seq
= &table
->sequences
[mid
];
1630 if (addr
< seq
->low_pc
)
1632 else if (addr
>= seq
->last_line
->address
)
1638 if (seq
&& addr
>= seq
->low_pc
&& addr
< seq
->last_line
->address
)
1640 /* Note: seq->last_line should be a descendingly sorted list. */
1641 for (each_line
= seq
->last_line
;
1643 each_line
= each_line
->prev_line
)
1644 if (addr
>= each_line
->address
)
1648 && !(each_line
->end_sequence
|| each_line
== seq
->last_line
))
1650 *filename_ptr
= each_line
->filename
;
1651 *linenumber_ptr
= each_line
->line
;
1656 *filename_ptr
= NULL
;
1660 /* Read in the .debug_ranges section for future reference. */
1663 read_debug_ranges (struct comp_unit
*unit
)
1665 struct dwarf2_debug
*stash
= unit
->stash
;
1666 return read_section (unit
->abfd
, ".debug_ranges", ".zdebug_ranges",
1668 &stash
->dwarf_ranges_buffer
, &stash
->dwarf_ranges_size
);
1671 /* Function table functions. */
1673 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1674 Note that we need to find the function that has the smallest
1675 range that contains ADDR, to handle inlined functions without
1676 depending upon them being ordered in TABLE by increasing range. */
1679 lookup_address_in_function_table (struct comp_unit
*unit
,
1681 struct funcinfo
**function_ptr
,
1682 const char **functionname_ptr
)
1684 struct funcinfo
* each_func
;
1685 struct funcinfo
* best_fit
= NULL
;
1686 struct arange
*arange
;
1688 for (each_func
= unit
->function_table
;
1690 each_func
= each_func
->prev_func
)
1692 for (arange
= &each_func
->arange
;
1694 arange
= arange
->next
)
1696 if (addr
>= arange
->low
&& addr
< arange
->high
)
1699 ((arange
->high
- arange
->low
) < (best_fit
->arange
.high
- best_fit
->arange
.low
)))
1700 best_fit
= each_func
;
1707 *functionname_ptr
= best_fit
->name
;
1708 *function_ptr
= best_fit
;
1717 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1718 and LINENUMBER_PTR, and return TRUE. */
1721 lookup_symbol_in_function_table (struct comp_unit
*unit
,
1724 const char **filename_ptr
,
1725 unsigned int *linenumber_ptr
)
1727 struct funcinfo
* each_func
;
1728 struct funcinfo
* best_fit
= NULL
;
1729 struct arange
*arange
;
1730 const char *name
= bfd_asymbol_name (sym
);
1731 asection
*sec
= bfd_get_section (sym
);
1733 for (each_func
= unit
->function_table
;
1735 each_func
= each_func
->prev_func
)
1737 for (arange
= &each_func
->arange
;
1739 arange
= arange
->next
)
1741 if ((!each_func
->sec
|| each_func
->sec
== sec
)
1742 && addr
>= arange
->low
1743 && addr
< arange
->high
1745 && strcmp (name
, each_func
->name
) == 0
1747 || ((arange
->high
- arange
->low
)
1748 < (best_fit
->arange
.high
- best_fit
->arange
.low
))))
1749 best_fit
= each_func
;
1755 best_fit
->sec
= sec
;
1756 *filename_ptr
= best_fit
->file
;
1757 *linenumber_ptr
= best_fit
->line
;
1764 /* Variable table functions. */
1766 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1767 LINENUMBER_PTR, and return TRUE. */
1770 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
1773 const char **filename_ptr
,
1774 unsigned int *linenumber_ptr
)
1776 const char *name
= bfd_asymbol_name (sym
);
1777 asection
*sec
= bfd_get_section (sym
);
1778 struct varinfo
* each
;
1780 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
1781 if (each
->stack
== 0
1782 && each
->file
!= NULL
1783 && each
->name
!= NULL
1784 && each
->addr
== addr
1785 && (!each
->sec
|| each
->sec
== sec
)
1786 && strcmp (name
, each
->name
) == 0)
1792 *filename_ptr
= each
->file
;
1793 *linenumber_ptr
= each
->line
;
1801 find_abstract_instance_name (struct comp_unit
*unit
,
1802 struct attribute
*attr_ptr
)
1804 bfd
*abfd
= unit
->abfd
;
1806 unsigned int abbrev_number
, bytes_read
, i
;
1807 struct abbrev_info
*abbrev
;
1808 bfd_uint64_t die_ref
= attr_ptr
->u
.val
;
1809 struct attribute attr
;
1812 /* DW_FORM_ref_addr can reference an entry in a different CU. It
1813 is an offset from the .debug_info section, not the current CU. */
1814 if (attr_ptr
->form
== DW_FORM_ref_addr
)
1816 /* We only support DW_FORM_ref_addr within the same file, so
1817 any relocations should be resolved already. */
1821 info_ptr
= unit
->sec_info_ptr
+ die_ref
;
1824 info_ptr
= unit
->info_ptr_unit
+ die_ref
;
1825 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1826 info_ptr
+= bytes_read
;
1830 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
1833 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
1835 bfd_set_error (bfd_error_bad_value
);
1839 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1841 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1845 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1849 case DW_AT_specification
:
1850 name
= find_abstract_instance_name (unit
, &attr
);
1852 case DW_AT_MIPS_linkage_name
:
1865 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
, bfd_uint64_t offset
)
1867 bfd_byte
*ranges_ptr
;
1868 bfd_vma base_address
= unit
->base_address
;
1870 if (! unit
->stash
->dwarf_ranges_buffer
)
1872 if (! read_debug_ranges (unit
))
1875 ranges_ptr
= unit
->stash
->dwarf_ranges_buffer
+ offset
;
1882 low_pc
= read_address (unit
, ranges_ptr
);
1883 ranges_ptr
+= unit
->addr_size
;
1884 high_pc
= read_address (unit
, ranges_ptr
);
1885 ranges_ptr
+= unit
->addr_size
;
1887 if (low_pc
== 0 && high_pc
== 0)
1889 if (low_pc
== -1UL && high_pc
!= -1UL)
1890 base_address
= high_pc
;
1892 arange_add (unit
->abfd
, arange
, base_address
+ low_pc
, base_address
+ high_pc
);
1896 /* DWARF2 Compilation unit functions. */
1898 /* Scan over each die in a comp. unit looking for functions to add
1899 to the function table and variables to the variable table. */
1902 scan_unit_for_symbols (struct comp_unit
*unit
)
1904 bfd
*abfd
= unit
->abfd
;
1905 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
1906 int nesting_level
= 1;
1907 struct funcinfo
**nested_funcs
;
1908 int nested_funcs_size
;
1910 /* Maintain a stack of in-scope functions and inlined functions, which we
1911 can use to set the caller_func field. */
1912 nested_funcs_size
= 32;
1913 nested_funcs
= (struct funcinfo
**)
1914 bfd_malloc (nested_funcs_size
* sizeof (struct funcinfo
*));
1915 if (nested_funcs
== NULL
)
1917 nested_funcs
[nesting_level
] = 0;
1919 while (nesting_level
)
1921 unsigned int abbrev_number
, bytes_read
, i
;
1922 struct abbrev_info
*abbrev
;
1923 struct attribute attr
;
1924 struct funcinfo
*func
;
1925 struct varinfo
*var
;
1927 bfd_vma high_pc
= 0;
1929 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1930 info_ptr
+= bytes_read
;
1932 if (! abbrev_number
)
1938 abbrev
= lookup_abbrev (abbrev_number
,unit
->abbrevs
);
1941 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
1943 bfd_set_error (bfd_error_bad_value
);
1944 free (nested_funcs
);
1949 if (abbrev
->tag
== DW_TAG_subprogram
1950 || abbrev
->tag
== DW_TAG_entry_point
1951 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
1953 bfd_size_type amt
= sizeof (struct funcinfo
);
1954 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
1955 func
->tag
= abbrev
->tag
;
1956 func
->prev_func
= unit
->function_table
;
1957 unit
->function_table
= func
;
1958 BFD_ASSERT (!unit
->cached
);
1960 if (func
->tag
== DW_TAG_inlined_subroutine
)
1961 for (i
= nesting_level
- 1; i
>= 1; i
--)
1962 if (nested_funcs
[i
])
1964 func
->caller_func
= nested_funcs
[i
];
1967 nested_funcs
[nesting_level
] = func
;
1972 if (abbrev
->tag
== DW_TAG_variable
)
1974 bfd_size_type amt
= sizeof (struct varinfo
);
1975 var
= (struct varinfo
*) bfd_zalloc (abfd
, amt
);
1976 var
->tag
= abbrev
->tag
;
1978 var
->prev_var
= unit
->variable_table
;
1979 unit
->variable_table
= var
;
1980 BFD_ASSERT (!unit
->cached
);
1983 /* No inline function in scope at this nesting level. */
1984 nested_funcs
[nesting_level
] = 0;
1987 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1989 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1995 case DW_AT_call_file
:
1996 func
->caller_file
= concat_filename (unit
->line_table
, attr
.u
.val
);
1999 case DW_AT_call_line
:
2000 func
->caller_line
= attr
.u
.val
;
2003 case DW_AT_abstract_origin
:
2004 func
->name
= find_abstract_instance_name (unit
, &attr
);
2008 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
2009 if (func
->name
== NULL
)
2010 func
->name
= attr
.u
.str
;
2013 case DW_AT_MIPS_linkage_name
:
2014 func
->name
= attr
.u
.str
;
2018 low_pc
= attr
.u
.val
;
2022 high_pc
= attr
.u
.val
;
2026 read_rangelist (unit
, &func
->arange
, attr
.u
.val
);
2029 case DW_AT_decl_file
:
2030 func
->file
= concat_filename (unit
->line_table
,
2034 case DW_AT_decl_line
:
2035 func
->line
= attr
.u
.val
;
2047 var
->name
= attr
.u
.str
;
2050 case DW_AT_decl_file
:
2051 var
->file
= concat_filename (unit
->line_table
,
2055 case DW_AT_decl_line
:
2056 var
->line
= attr
.u
.val
;
2059 case DW_AT_external
:
2060 if (attr
.u
.val
!= 0)
2064 case DW_AT_location
:
2068 case DW_FORM_block1
:
2069 case DW_FORM_block2
:
2070 case DW_FORM_block4
:
2071 if (*attr
.u
.blk
->data
== DW_OP_addr
)
2075 /* Verify that DW_OP_addr is the only opcode in the
2076 location, in which case the block size will be 1
2077 plus the address size. */
2078 /* ??? For TLS variables, gcc can emit
2079 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
2080 which we don't handle here yet. */
2081 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
2082 var
->addr
= bfd_get (unit
->addr_size
* 8,
2084 attr
.u
.blk
->data
+ 1);
2099 if (func
&& high_pc
!= 0)
2101 arange_add (unit
->abfd
, &func
->arange
, low_pc
, high_pc
);
2104 if (abbrev
->has_children
)
2108 if (nesting_level
>= nested_funcs_size
)
2110 struct funcinfo
**tmp
;
2112 nested_funcs_size
*= 2;
2113 tmp
= (struct funcinfo
**)
2114 bfd_realloc (nested_funcs
,
2115 (nested_funcs_size
* sizeof (struct funcinfo
*)));
2118 free (nested_funcs
);
2123 nested_funcs
[nesting_level
] = 0;
2127 free (nested_funcs
);
2131 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
2132 includes the compilation unit header that proceeds the DIE's, but
2133 does not include the length field that precedes each compilation
2134 unit header. END_PTR points one past the end of this comp unit.
2135 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
2137 This routine does not read the whole compilation unit; only enough
2138 to get to the line number information for the compilation unit. */
2140 static struct comp_unit
*
2141 parse_comp_unit (struct dwarf2_debug
*stash
,
2142 bfd_vma unit_length
,
2143 bfd_byte
*info_ptr_unit
,
2144 unsigned int offset_size
)
2146 struct comp_unit
* unit
;
2147 unsigned int version
;
2148 bfd_uint64_t abbrev_offset
= 0;
2149 unsigned int addr_size
;
2150 struct abbrev_info
** abbrevs
;
2151 unsigned int abbrev_number
, bytes_read
, i
;
2152 struct abbrev_info
*abbrev
;
2153 struct attribute attr
;
2154 bfd_byte
*info_ptr
= stash
->info_ptr
;
2155 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
2158 bfd_vma high_pc
= 0;
2159 bfd
*abfd
= stash
->bfd_ptr
;
2161 version
= read_2_bytes (abfd
, info_ptr
);
2163 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
2164 if (offset_size
== 4)
2165 abbrev_offset
= read_4_bytes (abfd
, info_ptr
);
2167 abbrev_offset
= read_8_bytes (abfd
, info_ptr
);
2168 info_ptr
+= offset_size
;
2169 addr_size
= read_1_byte (abfd
, info_ptr
);
2172 if (version
!= 2 && version
!= 3)
2174 (*_bfd_error_handler
) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 and 3 information."), version
);
2175 bfd_set_error (bfd_error_bad_value
);
2179 if (addr_size
> sizeof (bfd_vma
))
2181 (*_bfd_error_handler
) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
2183 (unsigned int) sizeof (bfd_vma
));
2184 bfd_set_error (bfd_error_bad_value
);
2188 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
2190 (*_bfd_error_handler
) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size
);
2191 bfd_set_error (bfd_error_bad_value
);
2195 /* Read the abbrevs for this compilation unit into a table. */
2196 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
);
2200 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
2201 info_ptr
+= bytes_read
;
2202 if (! abbrev_number
)
2204 (*_bfd_error_handler
) (_("Dwarf Error: Bad abbrev number: %u."),
2206 bfd_set_error (bfd_error_bad_value
);
2210 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
2213 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
2215 bfd_set_error (bfd_error_bad_value
);
2219 amt
= sizeof (struct comp_unit
);
2220 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
2222 unit
->version
= version
;
2223 unit
->addr_size
= addr_size
;
2224 unit
->offset_size
= offset_size
;
2225 unit
->abbrevs
= abbrevs
;
2226 unit
->end_ptr
= end_ptr
;
2227 unit
->stash
= stash
;
2228 unit
->info_ptr_unit
= info_ptr_unit
;
2229 unit
->sec_info_ptr
= stash
->sec_info_ptr
;
2231 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2233 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
2235 /* Store the data if it is of an attribute we want to keep in a
2236 partial symbol table. */
2239 case DW_AT_stmt_list
:
2241 unit
->line_offset
= attr
.u
.val
;
2245 unit
->name
= attr
.u
.str
;
2249 low_pc
= attr
.u
.val
;
2250 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2251 this is the base address to use when reading location
2252 lists or range lists. */
2253 unit
->base_address
= low_pc
;
2257 high_pc
= attr
.u
.val
;
2261 read_rangelist (unit
, &unit
->arange
, attr
.u
.val
);
2264 case DW_AT_comp_dir
:
2266 char *comp_dir
= attr
.u
.str
;
2269 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2270 directory, get rid of it. */
2271 char *cp
= strchr (comp_dir
, ':');
2273 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
2276 unit
->comp_dir
= comp_dir
;
2286 arange_add (unit
->abfd
, &unit
->arange
, low_pc
, high_pc
);
2289 unit
->first_child_die_ptr
= info_ptr
;
2293 /* Return TRUE if UNIT may contain the address given by ADDR. When
2294 there are functions written entirely with inline asm statements, the
2295 range info in the compilation unit header may not be correct. We
2296 need to consult the line info table to see if a compilation unit
2297 really contains the given address. */
2300 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
2302 struct arange
*arange
;
2307 arange
= &unit
->arange
;
2310 if (addr
>= arange
->low
&& addr
< arange
->high
)
2312 arange
= arange
->next
;
2319 /* If UNIT contains ADDR, set the output parameters to the values for
2320 the line containing ADDR. The output parameters, FILENAME_PTR,
2321 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2324 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2328 comp_unit_find_nearest_line (struct comp_unit
*unit
,
2330 const char **filename_ptr
,
2331 const char **functionname_ptr
,
2332 unsigned int *linenumber_ptr
,
2333 struct dwarf2_debug
*stash
)
2337 struct funcinfo
*function
;
2342 if (! unit
->line_table
)
2344 if (! unit
->stmtlist
)
2350 unit
->line_table
= decode_line_info (unit
, stash
);
2352 if (! unit
->line_table
)
2358 if (unit
->first_child_die_ptr
< unit
->end_ptr
2359 && ! scan_unit_for_symbols (unit
))
2367 func_p
= lookup_address_in_function_table (unit
, addr
,
2368 &function
, functionname_ptr
);
2369 if (func_p
&& (function
->tag
== DW_TAG_inlined_subroutine
))
2370 stash
->inliner_chain
= function
;
2371 line_p
= lookup_address_in_line_info_table (unit
->line_table
, addr
,
2374 return line_p
|| func_p
;
2377 /* Check to see if line info is already decoded in a comp_unit.
2378 If not, decode it. Returns TRUE if no errors were encountered;
2382 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
,
2383 struct dwarf2_debug
*stash
)
2388 if (! unit
->line_table
)
2390 if (! unit
->stmtlist
)
2396 unit
->line_table
= decode_line_info (unit
, stash
);
2398 if (! unit
->line_table
)
2404 if (unit
->first_child_die_ptr
< unit
->end_ptr
2405 && ! scan_unit_for_symbols (unit
))
2415 /* If UNIT contains SYM at ADDR, set the output parameters to the
2416 values for the line containing SYM. The output parameters,
2417 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2420 Return TRUE if UNIT contains SYM, and no errors were encountered;
2424 comp_unit_find_line (struct comp_unit
*unit
,
2427 const char **filename_ptr
,
2428 unsigned int *linenumber_ptr
,
2429 struct dwarf2_debug
*stash
)
2431 if (!comp_unit_maybe_decode_line_info (unit
, stash
))
2434 if (sym
->flags
& BSF_FUNCTION
)
2435 return lookup_symbol_in_function_table (unit
, sym
, addr
,
2439 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
2444 static struct funcinfo
*
2445 reverse_funcinfo_list (struct funcinfo
*head
)
2447 struct funcinfo
*rhead
;
2448 struct funcinfo
*temp
;
2450 for (rhead
= NULL
; head
; head
= temp
)
2452 temp
= head
->prev_func
;
2453 head
->prev_func
= rhead
;
2459 static struct varinfo
*
2460 reverse_varinfo_list (struct varinfo
*head
)
2462 struct varinfo
*rhead
;
2463 struct varinfo
*temp
;
2465 for (rhead
= NULL
; head
; head
= temp
)
2467 temp
= head
->prev_var
;
2468 head
->prev_var
= rhead
;
2474 /* Extract all interesting funcinfos and varinfos of a compilation
2475 unit into hash tables for faster lookup. Returns TRUE if no
2476 errors were enountered; FALSE otherwise. */
2479 comp_unit_hash_info (struct dwarf2_debug
*stash
,
2480 struct comp_unit
*unit
,
2481 struct info_hash_table
*funcinfo_hash_table
,
2482 struct info_hash_table
*varinfo_hash_table
)
2484 struct funcinfo
* each_func
;
2485 struct varinfo
* each_var
;
2486 bfd_boolean okay
= TRUE
;
2488 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
2490 if (!comp_unit_maybe_decode_line_info (unit
, stash
))
2493 BFD_ASSERT (!unit
->cached
);
2495 /* To preserve the original search order, we went to visit the function
2496 infos in the reversed order of the list. However, making the list
2497 bi-directional use quite a bit of extra memory. So we reverse
2498 the list first, traverse the list in the now reversed order and
2499 finally reverse the list again to get back the original order. */
2500 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
2501 for (each_func
= unit
->function_table
;
2503 each_func
= each_func
->prev_func
)
2505 /* Skip nameless functions. */
2506 if (each_func
->name
)
2507 /* There is no need to copy name string into hash table as
2508 name string is either in the dwarf string buffer or
2509 info in the stash. */
2510 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
2511 (void*) each_func
, FALSE
);
2513 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
2517 /* We do the same for variable infos. */
2518 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
2519 for (each_var
= unit
->variable_table
;
2521 each_var
= each_var
->prev_var
)
2523 /* Skip stack vars and vars with no files or names. */
2524 if (each_var
->stack
== 0
2525 && each_var
->file
!= NULL
2526 && each_var
->name
!= NULL
)
2527 /* There is no need to copy name string into hash table as
2528 name string is either in the dwarf string buffer or
2529 info in the stash. */
2530 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
2531 (void*) each_var
, FALSE
);
2534 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
2535 unit
->cached
= TRUE
;
2539 /* Locate a section in a BFD containing debugging info. The search starts
2540 from the section after AFTER_SEC, or from the first section in the BFD if
2541 AFTER_SEC is NULL. The search works by examining the names of the
2542 sections. There are two permissiable names. The first is .debug_info.
2543 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2544 This is a variation on the .debug_info section which has a checksum
2545 describing the contents appended onto the name. This allows the linker to
2546 identify and discard duplicate debugging sections for different
2547 compilation units. */
2548 #define DWARF2_DEBUG_INFO ".debug_info"
2549 #define DWARF2_COMPRESSED_DEBUG_INFO ".zdebug_info"
2550 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2553 find_debug_info (bfd
*abfd
, asection
*after_sec
)
2557 msec
= after_sec
!= NULL
? after_sec
->next
: abfd
->sections
;
2561 if (strcmp (msec
->name
, DWARF2_DEBUG_INFO
) == 0)
2564 if (strcmp (msec
->name
, DWARF2_COMPRESSED_DEBUG_INFO
) == 0)
2567 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
2576 /* Unset vmas for adjusted sections in STASH. */
2579 unset_sections (struct dwarf2_debug
*stash
)
2582 struct adjusted_section
*p
;
2584 i
= stash
->adjusted_section_count
;
2585 p
= stash
->adjusted_sections
;
2586 for (; i
> 0; i
--, p
++)
2587 p
->section
->vma
= 0;
2590 /* Set unique VMAs for loadable and DWARF sections in ABFD and save
2591 VMAs in STASH for unset_sections. */
2594 place_sections (bfd
*abfd
, struct dwarf2_debug
*stash
)
2596 struct adjusted_section
*p
;
2599 if (stash
->adjusted_section_count
!= 0)
2601 i
= stash
->adjusted_section_count
;
2602 p
= stash
->adjusted_sections
;
2603 for (; i
> 0; i
--, p
++)
2604 p
->section
->vma
= p
->adj_vma
;
2609 bfd_vma last_vma
= 0, last_dwarf
= 0;
2613 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
2621 /* We need to adjust the VMAs of any .debug_info sections.
2622 Skip compressed ones, since no relocations could target
2623 them - they should not appear in object files anyway. */
2624 if (strcmp (sect
->name
, DWARF2_DEBUG_INFO
) == 0)
2626 else if (CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
))
2631 if (!is_debug_info
&& (sect
->flags
& SEC_LOAD
) == 0)
2634 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
2641 amt
= i
* sizeof (struct adjusted_section
);
2642 p
= (struct adjusted_section
*) bfd_zalloc (abfd
, amt
);
2646 stash
->adjusted_sections
= p
;
2647 stash
->adjusted_section_count
= i
;
2649 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
2657 /* We need to adjust the VMAs of any .debug_info sections.
2658 Skip compressed ones, since no relocations could target
2659 them - they should not appear in object files anyway. */
2660 if (strcmp (sect
->name
, DWARF2_DEBUG_INFO
) == 0)
2662 else if (CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
))
2667 if (!is_debug_info
&& (sect
->flags
& SEC_LOAD
) == 0)
2670 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
2677 BFD_ASSERT (sect
->alignment_power
== 0);
2678 sect
->vma
= last_dwarf
;
2681 else if (last_vma
!= 0)
2683 /* Align the new address to the current section
2685 last_vma
= ((last_vma
2686 + ~((bfd_vma
) -1 << sect
->alignment_power
))
2687 & ((bfd_vma
) -1 << sect
->alignment_power
));
2688 sect
->vma
= last_vma
;
2689 last_vma
+= sect
->vma
+ sz
;
2692 last_vma
+= sect
->vma
+ sz
;
2694 p
->adj_vma
= sect
->vma
;
2703 /* Look up a funcinfo by name using the given info hash table. If found,
2704 also update the locations pointed to by filename_ptr and linenumber_ptr.
2706 This function returns TRUE if a funcinfo that matches the given symbol
2707 and address is found with any error; otherwise it returns FALSE. */
2710 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
2713 const char **filename_ptr
,
2714 unsigned int *linenumber_ptr
)
2716 struct funcinfo
* each_func
;
2717 struct funcinfo
* best_fit
= NULL
;
2718 struct info_list_node
*node
;
2719 struct arange
*arange
;
2720 const char *name
= bfd_asymbol_name (sym
);
2721 asection
*sec
= bfd_get_section (sym
);
2723 for (node
= lookup_info_hash_table (hash_table
, name
);
2727 each_func
= (struct funcinfo
*) node
->info
;
2728 for (arange
= &each_func
->arange
;
2730 arange
= arange
->next
)
2732 if ((!each_func
->sec
|| each_func
->sec
== sec
)
2733 && addr
>= arange
->low
2734 && addr
< arange
->high
2736 || ((arange
->high
- arange
->low
)
2737 < (best_fit
->arange
.high
- best_fit
->arange
.low
))))
2738 best_fit
= each_func
;
2744 best_fit
->sec
= sec
;
2745 *filename_ptr
= best_fit
->file
;
2746 *linenumber_ptr
= best_fit
->line
;
2753 /* Look up a varinfo by name using the given info hash table. If found,
2754 also update the locations pointed to by filename_ptr and linenumber_ptr.
2756 This function returns TRUE if a varinfo that matches the given symbol
2757 and address is found with any error; otherwise it returns FALSE. */
2760 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
2763 const char **filename_ptr
,
2764 unsigned int *linenumber_ptr
)
2766 const char *name
= bfd_asymbol_name (sym
);
2767 asection
*sec
= bfd_get_section (sym
);
2768 struct varinfo
* each
;
2769 struct info_list_node
*node
;
2771 for (node
= lookup_info_hash_table (hash_table
, name
);
2775 each
= (struct varinfo
*) node
->info
;
2776 if (each
->addr
== addr
2777 && (!each
->sec
|| each
->sec
== sec
))
2780 *filename_ptr
= each
->file
;
2781 *linenumber_ptr
= each
->line
;
2789 /* Update the funcinfo and varinfo info hash tables if they are
2790 not up to date. Returns TRUE if there is no error; otherwise
2791 returns FALSE and disable the info hash tables. */
2794 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
2796 struct comp_unit
*each
;
2798 /* Exit if hash tables are up-to-date. */
2799 if (stash
->all_comp_units
== stash
->hash_units_head
)
2802 if (stash
->hash_units_head
)
2803 each
= stash
->hash_units_head
->prev_unit
;
2805 each
= stash
->last_comp_unit
;
2809 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
2810 stash
->varinfo_hash_table
))
2812 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
2815 each
= each
->prev_unit
;
2818 stash
->hash_units_head
= stash
->all_comp_units
;
2822 /* Check consistency of info hash tables. This is for debugging only. */
2824 static void ATTRIBUTE_UNUSED
2825 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
2827 struct comp_unit
*each_unit
;
2828 struct funcinfo
*each_func
;
2829 struct varinfo
*each_var
;
2830 struct info_list_node
*node
;
2833 for (each_unit
= stash
->all_comp_units
;
2835 each_unit
= each_unit
->next_unit
)
2837 for (each_func
= each_unit
->function_table
;
2839 each_func
= each_func
->prev_func
)
2841 if (!each_func
->name
)
2843 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
2847 while (node
&& !found
)
2849 found
= node
->info
== each_func
;
2855 for (each_var
= each_unit
->variable_table
;
2857 each_var
= each_var
->prev_var
)
2859 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
2861 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
2865 while (node
&& !found
)
2867 found
= node
->info
== each_var
;
2875 /* Check to see if we want to enable the info hash tables, which consume
2876 quite a bit of memory. Currently we only check the number times
2877 bfd_dwarf2_find_line is called. In the future, we may also want to
2878 take the number of symbols into account. */
2881 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
2883 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
2885 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
2888 /* FIXME: Maybe we should check the reduce_memory_overheads
2889 and optimize fields in the bfd_link_info structure ? */
2891 /* Create hash tables. */
2892 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
2893 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
2894 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
2896 /* Turn off info hashes if any allocation above fails. */
2897 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
2900 /* We need a forced update so that the info hash tables will
2901 be created even though there is no compilation unit. That
2902 happens if STASH_INFO_HASH_TRIGGER is 0. */
2903 stash_maybe_update_info_hash_tables (stash
);
2904 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
2907 /* Find the file and line associated with a symbol and address using the
2908 info hash tables of a stash. If there is a match, the function returns
2909 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
2910 otherwise it returns FALSE. */
2913 stash_find_line_fast (struct dwarf2_debug
*stash
,
2916 const char **filename_ptr
,
2917 unsigned int *linenumber_ptr
)
2919 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
2921 if (sym
->flags
& BSF_FUNCTION
)
2922 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
2923 filename_ptr
, linenumber_ptr
);
2924 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
2925 filename_ptr
, linenumber_ptr
);
2928 /* Find the source code location of SYMBOL. If SYMBOL is NULL
2929 then find the nearest source code location corresponding to
2930 the address SECTION + OFFSET.
2931 Returns TRUE if the line is found without error and fills in
2932 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
2933 NULL the FUNCTIONNAME_PTR is also filled in.
2934 SYMBOLS contains the symbol table for ABFD.
2935 ADDR_SIZE is the number of bytes in the initial .debug_info length
2936 field and in the abbreviation offset, or zero to indicate that the
2937 default value should be used. */
2940 find_line (bfd
*abfd
,
2945 const char **filename_ptr
,
2946 const char **functionname_ptr
,
2947 unsigned int *linenumber_ptr
,
2948 unsigned int addr_size
,
2951 /* Read each compilation unit from the section .debug_info, and check
2952 to see if it contains the address we are searching for. If yes,
2953 lookup the address, and return the line number info. If no, go
2954 on to the next compilation unit.
2956 We keep a list of all the previously read compilation units, and
2957 a pointer to the next un-read compilation unit. Check the
2958 previously read units before reading more. */
2959 struct dwarf2_debug
*stash
;
2960 /* What address are we looking for? */
2962 struct comp_unit
* each
;
2963 bfd_vma found
= FALSE
;
2964 bfd_boolean do_line
;
2966 stash
= (struct dwarf2_debug
*) *pinfo
;
2970 bfd_size_type amt
= sizeof (struct dwarf2_debug
);
2972 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
2977 /* In a relocatable file, 2 functions may have the same address.
2978 We change the section vma so that they won't overlap. */
2979 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
2981 if (! place_sections (abfd
, stash
))
2985 do_line
= (section
== NULL
2987 && functionname_ptr
== NULL
2991 addr
= symbol
->value
;
2992 section
= bfd_get_section (symbol
);
2994 else if (section
!= NULL
2995 && functionname_ptr
!= NULL
3001 if (section
->output_section
)
3002 addr
+= section
->output_section
->vma
+ section
->output_offset
;
3004 addr
+= section
->vma
;
3005 *filename_ptr
= NULL
;
3007 *functionname_ptr
= NULL
;
3008 *linenumber_ptr
= 0;
3013 bfd_size_type total_size
;
3018 msec
= find_debug_info (abfd
, NULL
);
3021 char * debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
3023 if (debug_filename
== NULL
)
3024 /* No dwarf2 info, and no gnu_debuglink to follow.
3025 Note that at this point the stash has been allocated, but
3026 contains zeros. This lets future calls to this function
3027 fail more quickly. */
3030 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
3031 || ! bfd_check_format (debug_bfd
, bfd_object
)
3032 || (msec
= find_debug_info (debug_bfd
, NULL
)) == NULL
)
3035 bfd_close (debug_bfd
);
3036 /* FIXME: Should we report our failure to follow the debuglink ? */
3037 free (debug_filename
);
3044 /* There can be more than one DWARF2 info section in a BFD these
3045 days. First handle the easy case when there's only one. If
3046 there's more than one, try case two: none of the sections is
3047 compressed. In that case, read them all in and produce one
3048 large stash. We do this in two passes - in the first pass we
3049 just accumulate the section sizes, and in the second pass we
3050 read in the section's contents. (The allows us to avoid
3051 reallocing the data as we add sections to the stash.) If
3052 some or all sections are compressed, then do things the slow
3053 way, with a bunch of reallocs. */
3055 if (! find_debug_info (debug_bfd
, msec
))
3057 /* Case 1: only one info section. */
3058 total_size
= msec
->size
;
3059 if (! read_section (debug_bfd
, ".debug_info", ".zdebug_info",
3061 &stash
->info_ptr_memory
, &total_size
))
3066 int all_uncompressed
= 1;
3067 for (total_size
= 0; msec
; msec
= find_debug_info (debug_bfd
, msec
))
3069 total_size
+= msec
->size
;
3070 if (strcmp (msec
->name
, DWARF2_COMPRESSED_DEBUG_INFO
) == 0)
3071 all_uncompressed
= 0;
3073 if (all_uncompressed
)
3075 /* Case 2: multiple sections, but none is compressed. */
3076 stash
->info_ptr_memory
= (bfd_byte
*) bfd_malloc (total_size
);
3077 if (stash
->info_ptr_memory
== NULL
)
3081 for (msec
= find_debug_info (debug_bfd
, NULL
);
3083 msec
= find_debug_info (debug_bfd
, msec
))
3091 if (!(bfd_simple_get_relocated_section_contents
3092 (debug_bfd
, msec
, stash
->info_ptr_memory
+ total_size
,
3101 /* Case 3: multiple sections, some or all compressed. */
3102 stash
->info_ptr_memory
= NULL
;
3104 for (msec
= find_debug_info (debug_bfd
, NULL
);
3106 msec
= find_debug_info (debug_bfd
, msec
))
3108 bfd_size_type size
= msec
->size
;
3114 buffer
= (bfd_simple_get_relocated_section_contents
3115 (debug_bfd
, msec
, NULL
, symbols
));
3119 if (strcmp (msec
->name
, DWARF2_COMPRESSED_DEBUG_INFO
) == 0)
3121 if (! bfd_uncompress_section_contents (&buffer
, &size
))
3127 stash
->info_ptr_memory
= (bfd_byte
*)
3128 bfd_realloc (stash
->info_ptr_memory
, total_size
+ size
);
3129 memcpy (stash
->info_ptr_memory
+ total_size
, buffer
, size
);
3136 stash
->info_ptr
= stash
->info_ptr_memory
;
3137 stash
->info_ptr_end
= stash
->info_ptr
+ total_size
;
3138 stash
->sec
= find_debug_info (debug_bfd
, NULL
);
3139 stash
->sec_info_ptr
= stash
->info_ptr
;
3140 stash
->syms
= symbols
;
3141 stash
->bfd_ptr
= debug_bfd
;
3144 /* A null info_ptr indicates that there is no dwarf2 info
3145 (or that an error occured while setting up the stash). */
3146 if (! stash
->info_ptr
)
3149 stash
->inliner_chain
= NULL
;
3151 /* Check the previously read comp. units first. */
3154 /* The info hash tables use quite a bit of memory. We may not want to
3155 always use them. We use some heuristics to decide if and when to
3157 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
3158 stash_maybe_enable_info_hash_tables (abfd
, stash
);
3160 /* Keep info hash table up to date if they are available. Note that we
3161 may disable the hash tables if there is any error duing update. */
3162 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
3163 stash_maybe_update_info_hash_tables (stash
);
3165 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
3167 found
= stash_find_line_fast (stash
, symbol
, addr
, filename_ptr
,
3174 /* Check the previously read comp. units first. */
3175 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
3176 if ((symbol
->flags
& BSF_FUNCTION
) == 0
3177 || comp_unit_contains_address (each
, addr
))
3179 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
3180 linenumber_ptr
, stash
);
3188 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
3190 found
= (comp_unit_contains_address (each
, addr
)
3191 && comp_unit_find_nearest_line (each
, addr
,
3201 /* The DWARF2 spec says that the initial length field, and the
3202 offset of the abbreviation table, should both be 4-byte values.
3203 However, some compilers do things differently. */
3206 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
3208 /* Read each remaining comp. units checking each as they are read. */
3209 while (stash
->info_ptr
< stash
->info_ptr_end
)
3212 unsigned int offset_size
= addr_size
;
3213 bfd_byte
*info_ptr_unit
= stash
->info_ptr
;
3215 length
= read_4_bytes (stash
->bfd_ptr
, stash
->info_ptr
);
3216 /* A 0xffffff length is the DWARF3 way of indicating
3217 we use 64-bit offsets, instead of 32-bit offsets. */
3218 if (length
== 0xffffffff)
3221 length
= read_8_bytes (stash
->bfd_ptr
, stash
->info_ptr
+ 4);
3222 stash
->info_ptr
+= 12;
3224 /* A zero length is the IRIX way of indicating 64-bit offsets,
3225 mostly because the 64-bit length will generally fit in 32
3226 bits, and the endianness helps. */
3227 else if (length
== 0)
3230 length
= read_4_bytes (stash
->bfd_ptr
, stash
->info_ptr
+ 4);
3231 stash
->info_ptr
+= 8;
3233 /* In the absence of the hints above, we assume 32-bit DWARF2
3234 offsets even for targets with 64-bit addresses, because:
3235 a) most of the time these targets will not have generated
3236 more than 2Gb of debug info and so will not need 64-bit
3239 b) if they do use 64-bit offsets but they are not using
3240 the size hints that are tested for above then they are
3241 not conforming to the DWARF3 standard anyway. */
3242 else if (addr_size
== 8)
3245 stash
->info_ptr
+= 4;
3248 stash
->info_ptr
+= 4;
3252 each
= parse_comp_unit (stash
, length
, info_ptr_unit
,
3255 /* The dwarf information is damaged, don't trust it any
3258 stash
->info_ptr
+= length
;
3260 if (stash
->all_comp_units
)
3261 stash
->all_comp_units
->prev_unit
= each
;
3263 stash
->last_comp_unit
= each
;
3265 each
->next_unit
= stash
->all_comp_units
;
3266 stash
->all_comp_units
= each
;
3268 /* DW_AT_low_pc and DW_AT_high_pc are optional for
3269 compilation units. If we don't have them (i.e.,
3270 unit->high == 0), we need to consult the line info table
3271 to see if a compilation unit contains the given
3274 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
3275 || each
->arange
.high
== 0
3276 || comp_unit_contains_address (each
, addr
))
3277 && comp_unit_find_line (each
, symbol
, addr
,
3282 found
= ((each
->arange
.high
== 0
3283 || comp_unit_contains_address (each
, addr
))
3284 && comp_unit_find_nearest_line (each
, addr
,
3290 if ((bfd_vma
) (stash
->info_ptr
- stash
->sec_info_ptr
)
3291 == stash
->sec
->size
)
3293 stash
->sec
= find_debug_info (stash
->bfd_ptr
, stash
->sec
);
3294 stash
->sec_info_ptr
= stash
->info_ptr
;
3303 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
3304 unset_sections (stash
);
3309 /* The DWARF2 version of find_nearest_line.
3310 Return TRUE if the line is found without error. */
3313 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
3317 const char **filename_ptr
,
3318 const char **functionname_ptr
,
3319 unsigned int *linenumber_ptr
,
3320 unsigned int addr_size
,
3323 return find_line (abfd
, section
, offset
, NULL
, symbols
, filename_ptr
,
3324 functionname_ptr
, linenumber_ptr
, addr_size
,
3328 /* The DWARF2 version of find_line.
3329 Return TRUE if the line is found without error. */
3332 _bfd_dwarf2_find_line (bfd
*abfd
,
3335 const char **filename_ptr
,
3336 unsigned int *linenumber_ptr
,
3337 unsigned int addr_size
,
3340 return find_line (abfd
, NULL
, 0, symbol
, symbols
, filename_ptr
,
3341 NULL
, linenumber_ptr
, addr_size
,
3346 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
3347 const char **filename_ptr
,
3348 const char **functionname_ptr
,
3349 unsigned int *linenumber_ptr
,
3352 struct dwarf2_debug
*stash
;
3354 stash
= (struct dwarf2_debug
*) *pinfo
;
3357 struct funcinfo
*func
= stash
->inliner_chain
;
3359 if (func
&& func
->caller_func
)
3361 *filename_ptr
= func
->caller_file
;
3362 *functionname_ptr
= func
->caller_func
->name
;
3363 *linenumber_ptr
= func
->caller_line
;
3364 stash
->inliner_chain
= func
->caller_func
;
3373 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
)
3375 struct comp_unit
*each
;
3376 struct dwarf2_debug
*stash
;
3378 if (abfd
== NULL
|| elf_tdata (abfd
) == NULL
)
3381 stash
= (struct dwarf2_debug
*) elf_tdata (abfd
)->dwarf2_find_line_info
;
3386 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
3388 struct abbrev_info
**abbrevs
= each
->abbrevs
;
3389 struct funcinfo
*function_table
= each
->function_table
;
3390 struct varinfo
*variable_table
= each
->variable_table
;
3393 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
3395 struct abbrev_info
*abbrev
= abbrevs
[i
];
3399 free (abbrev
->attrs
);
3400 abbrev
= abbrev
->next
;
3404 if (each
->line_table
)
3406 free (each
->line_table
->dirs
);
3407 free (each
->line_table
->files
);
3410 while (function_table
)
3412 if (function_table
->file
)
3414 free (function_table
->file
);
3415 function_table
->file
= NULL
;
3418 if (function_table
->caller_file
)
3420 free (function_table
->caller_file
);
3421 function_table
->caller_file
= NULL
;
3423 function_table
= function_table
->prev_func
;
3426 while (variable_table
)
3428 if (variable_table
->file
)
3430 free (variable_table
->file
);
3431 variable_table
->file
= NULL
;
3434 variable_table
= variable_table
->prev_var
;
3438 if (stash
->dwarf_abbrev_buffer
)
3439 free (stash
->dwarf_abbrev_buffer
);
3440 if (stash
->dwarf_line_buffer
)
3441 free (stash
->dwarf_line_buffer
);
3442 if (stash
->dwarf_str_buffer
)
3443 free (stash
->dwarf_str_buffer
);
3444 if (stash
->dwarf_ranges_buffer
)
3445 free (stash
->dwarf_ranges_buffer
);
3446 if (stash
->info_ptr_memory
)
3447 free (stash
->info_ptr_memory
);