2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007 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"
38 #include "elf/dwarf2.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 loadable_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 /* Pointer to the symbol table. */
110 /* Pointer to the .debug_abbrev section loaded into memory. */
111 bfd_byte
*dwarf_abbrev_buffer
;
113 /* Length of the loaded .debug_abbrev section. */
114 unsigned long dwarf_abbrev_size
;
116 /* Buffer for decode_line_info. */
117 bfd_byte
*dwarf_line_buffer
;
119 /* Length of the loaded .debug_line section. */
120 unsigned long dwarf_line_size
;
122 /* Pointer to the .debug_str section loaded into memory. */
123 bfd_byte
*dwarf_str_buffer
;
125 /* Length of the loaded .debug_str section. */
126 unsigned long dwarf_str_size
;
128 /* Pointer to the .debug_ranges section loaded into memory. */
129 bfd_byte
*dwarf_ranges_buffer
;
131 /* Length of the loaded .debug_ranges section. */
132 unsigned long dwarf_ranges_size
;
134 /* If the most recent call to bfd_find_nearest_line was given an
135 address in an inlined function, preserve a pointer into the
136 calling chain for subsequent calls to bfd_find_inliner_info to
138 struct funcinfo
*inliner_chain
;
140 /* Number of loadable sections. */
141 unsigned int loadable_section_count
;
143 /* Array of loadable sections. */
144 struct loadable_section
*loadable_sections
;
146 /* Number of times find_line is called. This is used in
147 the heuristic for enabling the info hash tables. */
150 #define STASH_INFO_HASH_TRIGGER 100
152 /* Hash table mapping symbol names to function infos. */
153 struct info_hash_table
*funcinfo_hash_table
;
155 /* Hash table mapping symbol names to variable infos. */
156 struct info_hash_table
*varinfo_hash_table
;
158 /* Head of comp_unit list in the last hash table update. */
159 struct comp_unit
*hash_units_head
;
161 /* Status of info hash. */
162 int info_hash_status
;
163 #define STASH_INFO_HASH_OFF 0
164 #define STASH_INFO_HASH_ON 1
165 #define STASH_INFO_HASH_DISABLED 2
175 /* A minimal decoding of DWARF2 compilation units. We only decode
176 what's needed to get to the line number information. */
180 /* Chain the previously read compilation units. */
181 struct comp_unit
*next_unit
;
183 /* Likewise, chain the compilation unit read after this one.
184 The comp units are stored in reversed reading order. */
185 struct comp_unit
*prev_unit
;
187 /* Keep the bfd convenient (for memory allocation). */
190 /* The lowest and highest addresses contained in this compilation
191 unit as specified in the compilation unit header. */
192 struct arange arange
;
194 /* The DW_AT_name attribute (for error messages). */
197 /* The abbrev hash table. */
198 struct abbrev_info
**abbrevs
;
200 /* Note that an error was found by comp_unit_find_nearest_line. */
203 /* The DW_AT_comp_dir attribute. */
206 /* TRUE if there is a line number table associated with this comp. unit. */
209 /* Pointer to the current comp_unit so that we can find a given entry
211 bfd_byte
*info_ptr_unit
;
213 /* The offset into .debug_line of the line number table. */
214 unsigned long line_offset
;
216 /* Pointer to the first child die for the comp unit. */
217 bfd_byte
*first_child_die_ptr
;
219 /* The end of the comp unit. */
222 /* The decoded line number, NULL if not yet decoded. */
223 struct line_info_table
*line_table
;
225 /* A list of the functions found in this comp. unit. */
226 struct funcinfo
*function_table
;
228 /* A list of the variables found in this comp. unit. */
229 struct varinfo
*variable_table
;
231 /* Pointer to dwarf2_debug structure. */
232 struct dwarf2_debug
*stash
;
234 /* Address size for this unit - from unit header. */
235 unsigned char addr_size
;
237 /* Offset size for this unit - from unit header. */
238 unsigned char offset_size
;
240 /* Base address for this unit - from DW_AT_low_pc attribute of
241 DW_TAG_compile_unit DIE */
242 bfd_vma base_address
;
244 /* TRUE if symbols are cached in hash table for faster lookup by name. */
248 /* This data structure holds the information of an abbrev. */
251 unsigned int number
; /* Number identifying abbrev. */
252 enum dwarf_tag tag
; /* DWARF tag. */
253 int has_children
; /* Boolean. */
254 unsigned int num_attrs
; /* Number of attributes. */
255 struct attr_abbrev
*attrs
; /* An array of attribute descriptions. */
256 struct abbrev_info
*next
; /* Next in chain. */
261 enum dwarf_attribute name
;
262 enum dwarf_form form
;
265 #ifndef ABBREV_HASH_SIZE
266 #define ABBREV_HASH_SIZE 121
268 #ifndef ATTR_ALLOC_CHUNK
269 #define ATTR_ALLOC_CHUNK 4
272 /* Variable and function hash tables. This is used to speed up look-up
273 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
274 In order to share code between variable and function infos, we use
275 a list of untyped pointer for all variable/function info associated with
276 a symbol. We waste a bit of memory for list with one node but that
277 simplifies the code. */
279 struct info_list_node
281 struct info_list_node
*next
;
285 /* Info hash entry. */
286 struct info_hash_entry
288 struct bfd_hash_entry root
;
289 struct info_list_node
*head
;
292 struct info_hash_table
294 struct bfd_hash_table base
;
297 /* Function to create a new entry in info hash table. */
299 static struct bfd_hash_entry
*
300 info_hash_table_newfunc (struct bfd_hash_entry
*entry
,
301 struct bfd_hash_table
*table
,
304 struct info_hash_entry
*ret
= (struct info_hash_entry
*) entry
;
306 /* Allocate the structure if it has not already been allocated by a
310 ret
= bfd_hash_allocate (table
, sizeof (* ret
));
315 /* Call the allocation method of the base class. */
316 ret
= ((struct info_hash_entry
*)
317 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
319 /* Initialize the local fields here. */
323 return (struct bfd_hash_entry
*) ret
;
326 /* Function to create a new info hash table. It returns a pointer to the
327 newly created table or NULL if there is any error. We need abfd
328 solely for memory allocation. */
330 static struct info_hash_table
*
331 create_info_hash_table (bfd
*abfd
)
333 struct info_hash_table
*hash_table
;
335 hash_table
= bfd_alloc (abfd
, sizeof (struct info_hash_table
));
339 if (!bfd_hash_table_init (&hash_table
->base
, info_hash_table_newfunc
,
340 sizeof (struct info_hash_entry
)))
342 bfd_release (abfd
, hash_table
);
349 /* Insert an info entry into an info hash table. We do not check of
350 duplicate entries. Also, the caller need to guarantee that the
351 right type of info in inserted as info is passed as a void* pointer.
352 This function returns true if there is no error. */
355 insert_info_hash_table (struct info_hash_table
*hash_table
,
360 struct info_hash_entry
*entry
;
361 struct info_list_node
*node
;
363 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
,
368 node
= bfd_hash_allocate (&hash_table
->base
, sizeof (*node
));
373 node
->next
= entry
->head
;
379 /* Look up an info entry list from an info hash table. Return NULL
382 static struct info_list_node
*
383 lookup_info_hash_table (struct info_hash_table
*hash_table
, const char *key
)
385 struct info_hash_entry
*entry
;
387 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
, key
,
389 return entry
? entry
->head
: NULL
;
393 The following function up to the END VERBATIM mark are
394 copied directly from dwarf2read.c. */
396 /* Read dwarf information from a buffer. */
399 read_1_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
)
401 return bfd_get_8 (abfd
, buf
);
405 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
)
407 return bfd_get_signed_8 (abfd
, buf
);
411 read_2_bytes (bfd
*abfd
, bfd_byte
*buf
)
413 return bfd_get_16 (abfd
, buf
);
417 read_4_bytes (bfd
*abfd
, bfd_byte
*buf
)
419 return bfd_get_32 (abfd
, buf
);
423 read_8_bytes (bfd
*abfd
, bfd_byte
*buf
)
425 return bfd_get_64 (abfd
, buf
);
429 read_n_bytes (bfd
*abfd ATTRIBUTE_UNUSED
,
431 unsigned int size ATTRIBUTE_UNUSED
)
433 /* If the size of a host char is 8 bits, we can return a pointer
434 to the buffer, otherwise we have to copy the data to a buffer
435 allocated on the temporary obstack. */
440 read_string (bfd
*abfd ATTRIBUTE_UNUSED
,
442 unsigned int *bytes_read_ptr
)
444 /* Return a pointer to the embedded string. */
445 char *str
= (char *) buf
;
452 *bytes_read_ptr
= strlen (str
) + 1;
457 read_indirect_string (struct comp_unit
* unit
,
459 unsigned int *bytes_read_ptr
)
462 struct dwarf2_debug
*stash
= unit
->stash
;
465 if (unit
->offset_size
== 4)
466 offset
= read_4_bytes (unit
->abfd
, buf
);
468 offset
= read_8_bytes (unit
->abfd
, buf
);
469 *bytes_read_ptr
= unit
->offset_size
;
471 if (! stash
->dwarf_str_buffer
)
474 bfd
*abfd
= unit
->abfd
;
477 msec
= bfd_get_section_by_name (abfd
, ".debug_str");
480 (*_bfd_error_handler
)
481 (_("Dwarf Error: Can't find .debug_str section."));
482 bfd_set_error (bfd_error_bad_value
);
486 sz
= msec
->rawsize
? msec
->rawsize
: msec
->size
;
487 stash
->dwarf_str_size
= sz
;
488 stash
->dwarf_str_buffer
= bfd_alloc (abfd
, sz
);
489 if (! stash
->dwarf_str_buffer
)
492 if (! bfd_get_section_contents (abfd
, msec
, stash
->dwarf_str_buffer
,
497 if (offset
>= stash
->dwarf_str_size
)
499 (*_bfd_error_handler
) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
500 (unsigned long) offset
, stash
->dwarf_str_size
);
501 bfd_set_error (bfd_error_bad_value
);
505 str
= (char *) stash
->dwarf_str_buffer
+ offset
;
514 read_address (struct comp_unit
*unit
, bfd_byte
*buf
)
516 int signed_vma
= get_elf_backend_data (unit
->abfd
)->sign_extend_vma
;
520 switch (unit
->addr_size
)
523 return bfd_get_signed_64 (unit
->abfd
, buf
);
525 return bfd_get_signed_32 (unit
->abfd
, buf
);
527 return bfd_get_signed_16 (unit
->abfd
, buf
);
534 switch (unit
->addr_size
)
537 return bfd_get_64 (unit
->abfd
, buf
);
539 return bfd_get_32 (unit
->abfd
, buf
);
541 return bfd_get_16 (unit
->abfd
, buf
);
548 /* Lookup an abbrev_info structure in the abbrev hash table. */
550 static struct abbrev_info
*
551 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
553 unsigned int hash_number
;
554 struct abbrev_info
*abbrev
;
556 hash_number
= number
% ABBREV_HASH_SIZE
;
557 abbrev
= abbrevs
[hash_number
];
561 if (abbrev
->number
== number
)
564 abbrev
= abbrev
->next
;
570 /* In DWARF version 2, the description of the debugging information is
571 stored in a separate .debug_abbrev section. Before we read any
572 dies from a section we read in all abbreviations and install them
575 static struct abbrev_info
**
576 read_abbrevs (bfd
*abfd
, bfd_uint64_t offset
, struct dwarf2_debug
*stash
)
578 struct abbrev_info
**abbrevs
;
579 bfd_byte
*abbrev_ptr
;
580 struct abbrev_info
*cur_abbrev
;
581 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
582 unsigned int abbrev_form
, hash_number
;
585 if (! stash
->dwarf_abbrev_buffer
)
589 msec
= bfd_get_section_by_name (abfd
, ".debug_abbrev");
592 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_abbrev section."));
593 bfd_set_error (bfd_error_bad_value
);
597 stash
->dwarf_abbrev_size
= msec
->size
;
598 stash
->dwarf_abbrev_buffer
599 = bfd_simple_get_relocated_section_contents (abfd
, msec
, NULL
,
601 if (! stash
->dwarf_abbrev_buffer
)
605 if (offset
>= stash
->dwarf_abbrev_size
)
607 (*_bfd_error_handler
) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
608 (unsigned long) offset
, stash
->dwarf_abbrev_size
);
609 bfd_set_error (bfd_error_bad_value
);
613 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
614 abbrevs
= bfd_zalloc (abfd
, amt
);
616 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
617 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
618 abbrev_ptr
+= bytes_read
;
620 /* Loop until we reach an abbrev number of 0. */
621 while (abbrev_number
)
623 amt
= sizeof (struct abbrev_info
);
624 cur_abbrev
= bfd_zalloc (abfd
, amt
);
626 /* Read in abbrev header. */
627 cur_abbrev
->number
= abbrev_number
;
628 cur_abbrev
->tag
= (enum dwarf_tag
)
629 read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
630 abbrev_ptr
+= bytes_read
;
631 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
);
634 /* Now read in declarations. */
635 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
636 abbrev_ptr
+= bytes_read
;
637 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
638 abbrev_ptr
+= bytes_read
;
642 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
644 struct attr_abbrev
*tmp
;
646 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
647 amt
*= sizeof (struct attr_abbrev
);
648 tmp
= bfd_realloc (cur_abbrev
->attrs
, amt
);
653 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
655 struct abbrev_info
*abbrev
= abbrevs
[i
];
659 free (abbrev
->attrs
);
660 abbrev
= abbrev
->next
;
665 cur_abbrev
->attrs
= tmp
;
668 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
669 = (enum dwarf_attribute
) abbrev_name
;
670 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
++].form
671 = (enum dwarf_form
) abbrev_form
;
672 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
673 abbrev_ptr
+= bytes_read
;
674 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
675 abbrev_ptr
+= bytes_read
;
678 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
679 cur_abbrev
->next
= abbrevs
[hash_number
];
680 abbrevs
[hash_number
] = cur_abbrev
;
682 /* Get next abbreviation.
683 Under Irix6 the abbreviations for a compilation unit are not
684 always properly terminated with an abbrev number of 0.
685 Exit loop if we encounter an abbreviation which we have
686 already read (which means we are about to read the abbreviations
687 for the next compile unit) or if the end of the abbreviation
689 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
690 >= stash
->dwarf_abbrev_size
)
692 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
693 abbrev_ptr
+= bytes_read
;
694 if (lookup_abbrev (abbrev_number
,abbrevs
) != NULL
)
701 /* Read an attribute value described by an attribute form. */
704 read_attribute_value (struct attribute
*attr
,
706 struct comp_unit
*unit
,
709 bfd
*abfd
= unit
->abfd
;
710 unsigned int bytes_read
;
711 struct dwarf_block
*blk
;
714 attr
->form
= (enum dwarf_form
) form
;
719 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
720 case DW_FORM_ref_addr
:
721 attr
->u
.val
= read_address (unit
, info_ptr
);
722 info_ptr
+= unit
->addr_size
;
725 amt
= sizeof (struct dwarf_block
);
726 blk
= bfd_alloc (abfd
, amt
);
727 blk
->size
= read_2_bytes (abfd
, info_ptr
);
729 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
730 info_ptr
+= blk
->size
;
734 amt
= sizeof (struct dwarf_block
);
735 blk
= bfd_alloc (abfd
, amt
);
736 blk
->size
= read_4_bytes (abfd
, info_ptr
);
738 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
739 info_ptr
+= blk
->size
;
743 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
);
747 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
);
751 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
755 attr
->u
.str
= read_string (abfd
, info_ptr
, &bytes_read
);
756 info_ptr
+= bytes_read
;
759 attr
->u
.str
= read_indirect_string (unit
, info_ptr
, &bytes_read
);
760 info_ptr
+= bytes_read
;
763 amt
= sizeof (struct dwarf_block
);
764 blk
= bfd_alloc (abfd
, amt
);
765 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
766 info_ptr
+= bytes_read
;
767 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
768 info_ptr
+= blk
->size
;
772 amt
= sizeof (struct dwarf_block
);
773 blk
= bfd_alloc (abfd
, amt
);
774 blk
->size
= read_1_byte (abfd
, info_ptr
);
776 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
777 info_ptr
+= blk
->size
;
781 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
785 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
789 attr
->u
.sval
= read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
790 info_ptr
+= bytes_read
;
793 attr
->u
.val
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
794 info_ptr
+= bytes_read
;
797 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
801 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
);
805 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
);
809 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
812 case DW_FORM_ref_udata
:
813 attr
->u
.val
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
814 info_ptr
+= bytes_read
;
816 case DW_FORM_indirect
:
817 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
818 info_ptr
+= bytes_read
;
819 info_ptr
= read_attribute_value (attr
, form
, unit
, info_ptr
);
822 (*_bfd_error_handler
) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
824 bfd_set_error (bfd_error_bad_value
);
829 /* Read an attribute described by an abbreviated attribute. */
832 read_attribute (struct attribute
*attr
,
833 struct attr_abbrev
*abbrev
,
834 struct comp_unit
*unit
,
837 attr
->name
= abbrev
->name
;
838 info_ptr
= read_attribute_value (attr
, abbrev
->form
, unit
, info_ptr
);
842 /* Source line information table routines. */
844 #define FILE_ALLOC_CHUNK 5
845 #define DIR_ALLOC_CHUNK 5
849 struct line_info
* prev_line
;
854 int end_sequence
; /* End of (sequential) code sequence. */
865 struct line_info_table
868 unsigned int num_files
;
869 unsigned int num_dirs
;
872 struct fileinfo
* files
;
873 struct line_info
* last_line
; /* largest VMA */
874 struct line_info
* lcl_head
; /* local head; used in 'add_line_info' */
877 /* Remember some information about each function. If the function is
878 inlined (DW_TAG_inlined_subroutine) it may have two additional
879 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
880 source code location where this function was inlined. */
884 struct funcinfo
*prev_func
; /* Pointer to previous function in list of all functions */
885 struct funcinfo
*caller_func
; /* Pointer to function one scope higher */
886 char *caller_file
; /* Source location file name where caller_func inlines this func */
887 int caller_line
; /* Source location line number where caller_func inlines this func */
888 char *file
; /* Source location file name */
889 int line
; /* Source location line number */
892 struct arange arange
;
893 asection
*sec
; /* Where the symbol is defined */
898 /* Pointer to previous variable in list of all variables */
899 struct varinfo
*prev_var
;
900 /* Source location file name */
902 /* Source location line number */
907 /* Where the symbol is defined */
909 /* Is this a stack variable? */
910 unsigned int stack
: 1;
913 /* Return TRUE if NEW_LINE should sort after LINE. */
915 static inline bfd_boolean
916 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
918 return (new_line
->address
> line
->address
919 || (new_line
->address
== line
->address
920 && new_line
->end_sequence
< line
->end_sequence
));
924 /* Adds a new entry to the line_info list in the line_info_table, ensuring
925 that the list is sorted. Note that the line_info list is sorted from
926 highest to lowest VMA (with possible duplicates); that is,
927 line_info->prev_line always accesses an equal or smaller VMA. */
930 add_line_info (struct line_info_table
*table
,
937 bfd_size_type amt
= sizeof (struct line_info
);
938 struct line_info
* info
= bfd_alloc (table
->abfd
, amt
);
940 /* Set member data of 'info'. */
941 info
->address
= address
;
943 info
->column
= column
;
944 info
->end_sequence
= end_sequence
;
946 if (filename
&& filename
[0])
948 info
->filename
= bfd_alloc (table
->abfd
, strlen (filename
) + 1);
950 strcpy (info
->filename
, filename
);
953 info
->filename
= NULL
;
955 /* Find the correct location for 'info'. Normally we will receive
956 new line_info data 1) in order and 2) with increasing VMAs.
957 However some compilers break the rules (cf. decode_line_info) and
958 so we include some heuristics for quickly finding the correct
959 location for 'info'. In particular, these heuristics optimize for
960 the common case in which the VMA sequence that we receive is a
961 list of locally sorted VMAs such as
962 p...z a...j (where a < j < p < z)
964 Note: table->lcl_head is used to head an *actual* or *possible*
965 sequence within the list (such as a...j) that is not directly
966 headed by table->last_line
968 Note: we may receive duplicate entries from 'decode_line_info'. */
970 if (!table
->last_line
971 || new_line_sorts_after (info
, table
->last_line
))
973 /* Normal case: add 'info' to the beginning of the list */
974 info
->prev_line
= table
->last_line
;
975 table
->last_line
= info
;
977 /* lcl_head: initialize to head a *possible* sequence at the end. */
978 if (!table
->lcl_head
)
979 table
->lcl_head
= info
;
981 else if (!new_line_sorts_after (info
, table
->lcl_head
)
982 && (!table
->lcl_head
->prev_line
983 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
985 /* Abnormal but easy: lcl_head is the head of 'info'. */
986 info
->prev_line
= table
->lcl_head
->prev_line
;
987 table
->lcl_head
->prev_line
= info
;
991 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
992 heads for 'info'. Reset 'lcl_head'. */
993 struct line_info
* li2
= table
->last_line
; /* always non-NULL */
994 struct line_info
* li1
= li2
->prev_line
;
998 if (!new_line_sorts_after (info
, li2
)
999 && new_line_sorts_after (info
, li1
))
1002 li2
= li1
; /* always non-NULL */
1003 li1
= li1
->prev_line
;
1005 table
->lcl_head
= li2
;
1006 info
->prev_line
= table
->lcl_head
->prev_line
;
1007 table
->lcl_head
->prev_line
= info
;
1011 /* Extract a fully qualified filename from a line info table.
1012 The returned string has been malloc'ed and it is the caller's
1013 responsibility to free it. */
1016 concat_filename (struct line_info_table
*table
, unsigned int file
)
1020 if (file
- 1 >= table
->num_files
)
1022 /* FILE == 0 means unknown. */
1024 (*_bfd_error_handler
)
1025 (_("Dwarf Error: mangled line number section (bad file number)."));
1026 return strdup ("<unknown>");
1029 filename
= table
->files
[file
- 1].name
;
1031 if (!IS_ABSOLUTE_PATH (filename
))
1033 char *dirname
= NULL
;
1034 char *subdirname
= NULL
;
1038 if (table
->files
[file
- 1].dir
)
1039 subdirname
= table
->dirs
[table
->files
[file
- 1].dir
- 1];
1041 if (!subdirname
|| !IS_ABSOLUTE_PATH (subdirname
))
1042 dirname
= table
->comp_dir
;
1046 dirname
= subdirname
;
1051 return strdup (filename
);
1053 len
= strlen (dirname
) + strlen (filename
) + 2;
1057 len
+= strlen (subdirname
) + 1;
1058 name
= bfd_malloc (len
);
1060 sprintf (name
, "%s/%s/%s", dirname
, subdirname
, filename
);
1064 name
= bfd_malloc (len
);
1066 sprintf (name
, "%s/%s", dirname
, filename
);
1072 return strdup (filename
);
1076 arange_add (bfd
*abfd
, struct arange
*first_arange
, bfd_vma low_pc
, bfd_vma high_pc
)
1078 struct arange
*arange
;
1080 /* If the first arange is empty, use it. */
1081 if (first_arange
->high
== 0)
1083 first_arange
->low
= low_pc
;
1084 first_arange
->high
= high_pc
;
1088 /* Next see if we can cheaply extend an existing range. */
1089 arange
= first_arange
;
1092 if (low_pc
== arange
->high
)
1094 arange
->high
= high_pc
;
1097 if (high_pc
== arange
->low
)
1099 arange
->low
= low_pc
;
1102 arange
= arange
->next
;
1106 /* Need to allocate a new arange and insert it into the arange list.
1107 Order isn't significant, so just insert after the first arange. */
1108 arange
= bfd_zalloc (abfd
, sizeof (*arange
));
1109 arange
->low
= low_pc
;
1110 arange
->high
= high_pc
;
1111 arange
->next
= first_arange
->next
;
1112 first_arange
->next
= arange
;
1115 /* Decode the line number information for UNIT. */
1117 static struct line_info_table
*
1118 decode_line_info (struct comp_unit
*unit
, struct dwarf2_debug
*stash
)
1120 bfd
*abfd
= unit
->abfd
;
1121 struct line_info_table
* table
;
1124 struct line_head lh
;
1125 unsigned int i
, bytes_read
, offset_size
;
1126 char *cur_file
, *cur_dir
;
1127 unsigned char op_code
, extended_op
, adj_opcode
;
1130 if (! stash
->dwarf_line_buffer
)
1134 msec
= bfd_get_section_by_name (abfd
, ".debug_line");
1137 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_line section."));
1138 bfd_set_error (bfd_error_bad_value
);
1142 stash
->dwarf_line_size
= msec
->size
;
1143 stash
->dwarf_line_buffer
1144 = bfd_simple_get_relocated_section_contents (abfd
, msec
, NULL
,
1146 if (! stash
->dwarf_line_buffer
)
1150 /* It is possible to get a bad value for the line_offset. Validate
1151 it here so that we won't get a segfault below. */
1152 if (unit
->line_offset
>= stash
->dwarf_line_size
)
1154 (*_bfd_error_handler
) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
1155 unit
->line_offset
, stash
->dwarf_line_size
);
1156 bfd_set_error (bfd_error_bad_value
);
1160 amt
= sizeof (struct line_info_table
);
1161 table
= bfd_alloc (abfd
, amt
);
1163 table
->comp_dir
= unit
->comp_dir
;
1165 table
->num_files
= 0;
1166 table
->files
= NULL
;
1168 table
->num_dirs
= 0;
1171 table
->files
= NULL
;
1172 table
->last_line
= NULL
;
1173 table
->lcl_head
= NULL
;
1175 line_ptr
= stash
->dwarf_line_buffer
+ unit
->line_offset
;
1177 /* Read in the prologue. */
1178 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
1181 if (lh
.total_length
== 0xffffffff)
1183 lh
.total_length
= read_8_bytes (abfd
, line_ptr
);
1187 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
1189 /* Handle (non-standard) 64-bit DWARF2 formats. */
1190 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
1194 line_end
= line_ptr
+ lh
.total_length
;
1195 lh
.version
= read_2_bytes (abfd
, line_ptr
);
1197 if (offset_size
== 4)
1198 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
);
1200 lh
.prologue_length
= read_8_bytes (abfd
, line_ptr
);
1201 line_ptr
+= offset_size
;
1202 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
);
1204 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
);
1206 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
);
1208 lh
.line_range
= read_1_byte (abfd
, line_ptr
);
1210 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
);
1212 amt
= lh
.opcode_base
* sizeof (unsigned char);
1213 lh
.standard_opcode_lengths
= bfd_alloc (abfd
, amt
);
1215 lh
.standard_opcode_lengths
[0] = 1;
1217 for (i
= 1; i
< lh
.opcode_base
; ++i
)
1219 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
);
1223 /* Read directory table. */
1224 while ((cur_dir
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1226 line_ptr
+= bytes_read
;
1228 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1232 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1233 amt
*= sizeof (char *);
1235 tmp
= bfd_realloc (table
->dirs
, amt
);
1244 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1247 line_ptr
+= bytes_read
;
1249 /* Read file name table. */
1250 while ((cur_file
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1252 line_ptr
+= bytes_read
;
1254 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1256 struct fileinfo
*tmp
;
1258 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1259 amt
*= sizeof (struct fileinfo
);
1261 tmp
= bfd_realloc (table
->files
, amt
);
1264 free (table
->files
);
1271 table
->files
[table
->num_files
].name
= cur_file
;
1272 table
->files
[table
->num_files
].dir
=
1273 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1274 line_ptr
+= bytes_read
;
1275 table
->files
[table
->num_files
].time
=
1276 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1277 line_ptr
+= bytes_read
;
1278 table
->files
[table
->num_files
].size
=
1279 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1280 line_ptr
+= bytes_read
;
1284 line_ptr
+= bytes_read
;
1286 /* Read the statement sequences until there's nothing left. */
1287 while (line_ptr
< line_end
)
1289 /* State machine registers. */
1290 bfd_vma address
= 0;
1291 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
1292 unsigned int line
= 1;
1293 unsigned int column
= 0;
1294 int is_stmt
= lh
.default_is_stmt
;
1295 int end_sequence
= 0;
1296 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1297 compilers generate address sequences that are wildly out of
1298 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1299 for ia64-Linux). Thus, to determine the low and high
1300 address, we must compare on every DW_LNS_copy, etc. */
1301 bfd_vma low_pc
= (bfd_vma
) -1;
1302 bfd_vma high_pc
= 0;
1304 /* Decode the table. */
1305 while (! end_sequence
)
1307 op_code
= read_1_byte (abfd
, line_ptr
);
1310 if (op_code
>= lh
.opcode_base
)
1312 /* Special operand. */
1313 adj_opcode
= op_code
- lh
.opcode_base
;
1314 address
+= (adj_opcode
/ lh
.line_range
)
1315 * lh
.minimum_instruction_length
;
1316 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
1317 /* Append row to matrix using current values. */
1318 add_line_info (table
, address
, filename
, line
, column
, 0);
1319 if (address
< low_pc
)
1321 if (address
> high_pc
)
1324 else switch (op_code
)
1326 case DW_LNS_extended_op
:
1327 /* Ignore length. */
1329 extended_op
= read_1_byte (abfd
, line_ptr
);
1332 switch (extended_op
)
1334 case DW_LNE_end_sequence
:
1336 add_line_info (table
, address
, filename
, line
, column
,
1338 if (address
< low_pc
)
1340 if (address
> high_pc
)
1342 arange_add (unit
->abfd
, &unit
->arange
, low_pc
, high_pc
);
1344 case DW_LNE_set_address
:
1345 address
= read_address (unit
, line_ptr
);
1346 line_ptr
+= unit
->addr_size
;
1348 case DW_LNE_define_file
:
1349 cur_file
= read_string (abfd
, line_ptr
, &bytes_read
);
1350 line_ptr
+= bytes_read
;
1351 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1353 struct fileinfo
*tmp
;
1355 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1356 amt
*= sizeof (struct fileinfo
);
1357 tmp
= bfd_realloc (table
->files
, amt
);
1360 free (table
->files
);
1367 table
->files
[table
->num_files
].name
= cur_file
;
1368 table
->files
[table
->num_files
].dir
=
1369 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1370 line_ptr
+= bytes_read
;
1371 table
->files
[table
->num_files
].time
=
1372 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1373 line_ptr
+= bytes_read
;
1374 table
->files
[table
->num_files
].size
=
1375 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1376 line_ptr
+= bytes_read
;
1380 (*_bfd_error_handler
) (_("Dwarf Error: mangled line number section."));
1381 bfd_set_error (bfd_error_bad_value
);
1383 free (table
->files
);
1389 add_line_info (table
, address
, filename
, line
, column
, 0);
1390 if (address
< low_pc
)
1392 if (address
> high_pc
)
1395 case DW_LNS_advance_pc
:
1396 address
+= lh
.minimum_instruction_length
1397 * read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1398 line_ptr
+= bytes_read
;
1400 case DW_LNS_advance_line
:
1401 line
+= read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
1402 line_ptr
+= bytes_read
;
1404 case DW_LNS_set_file
:
1408 /* The file and directory tables are 0
1409 based, the references are 1 based. */
1410 file
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1411 line_ptr
+= bytes_read
;
1414 filename
= concat_filename (table
, file
);
1417 case DW_LNS_set_column
:
1418 column
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1419 line_ptr
+= bytes_read
;
1421 case DW_LNS_negate_stmt
:
1422 is_stmt
= (!is_stmt
);
1424 case DW_LNS_set_basic_block
:
1426 case DW_LNS_const_add_pc
:
1427 address
+= lh
.minimum_instruction_length
1428 * ((255 - lh
.opcode_base
) / lh
.line_range
);
1430 case DW_LNS_fixed_advance_pc
:
1431 address
+= read_2_bytes (abfd
, line_ptr
);
1438 /* Unknown standard opcode, ignore it. */
1439 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
1441 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1442 line_ptr
+= bytes_read
;
1455 /* If ADDR is within TABLE set the output parameters and return TRUE,
1456 otherwise return FALSE. The output parameters, FILENAME_PTR and
1457 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1460 lookup_address_in_line_info_table (struct line_info_table
*table
,
1462 struct funcinfo
*function
,
1463 const char **filename_ptr
,
1464 unsigned int *linenumber_ptr
)
1466 /* Note: table->last_line should be a descendingly sorted list. */
1467 struct line_info
* next_line
= table
->last_line
;
1468 struct line_info
* each_line
= NULL
;
1469 *filename_ptr
= NULL
;
1474 each_line
= next_line
->prev_line
;
1476 /* Check for large addresses */
1477 if (addr
> next_line
->address
)
1478 each_line
= NULL
; /* ensure we skip over the normal case */
1480 /* Normal case: search the list; save */
1481 while (each_line
&& next_line
)
1483 /* If we have an address match, save this info. This allows us
1484 to return as good as results as possible for strange debugging
1486 bfd_boolean addr_match
= FALSE
;
1487 if (each_line
->address
<= addr
&& addr
< next_line
->address
)
1491 /* If this line appears to span functions, and addr is in the
1492 later function, return the first line of that function instead
1493 of the last line of the earlier one. This check is for GCC
1494 2.95, which emits the first line number for a function late. */
1496 if (function
!= NULL
)
1499 struct arange
*arange
;
1501 /* Find the lowest address in the function's range list */
1502 lowest_pc
= function
->arange
.low
;
1503 for (arange
= &function
->arange
;
1505 arange
= arange
->next
)
1507 if (function
->arange
.low
< lowest_pc
)
1508 lowest_pc
= function
->arange
.low
;
1510 /* Check for spanning function and set outgoing line info */
1511 if (addr
>= lowest_pc
1512 && each_line
->address
< lowest_pc
1513 && next_line
->address
> lowest_pc
)
1515 *filename_ptr
= next_line
->filename
;
1516 *linenumber_ptr
= next_line
->line
;
1520 *filename_ptr
= each_line
->filename
;
1521 *linenumber_ptr
= each_line
->line
;
1526 *filename_ptr
= each_line
->filename
;
1527 *linenumber_ptr
= each_line
->line
;
1531 if (addr_match
&& !each_line
->end_sequence
)
1532 return TRUE
; /* we have definitely found what we want */
1534 next_line
= each_line
;
1535 each_line
= each_line
->prev_line
;
1538 /* At this point each_line is NULL but next_line is not. If we found
1539 a candidate end-of-sequence point in the loop above, we can return
1540 that (compatibility with a bug in the Intel compiler); otherwise,
1541 assuming that we found the containing function for this address in
1542 this compilation unit, return the first line we have a number for
1543 (compatibility with GCC 2.95). */
1544 if (*filename_ptr
== NULL
&& function
!= NULL
)
1546 *filename_ptr
= next_line
->filename
;
1547 *linenumber_ptr
= next_line
->line
;
1554 /* Read in the .debug_ranges section for future reference */
1557 read_debug_ranges (struct comp_unit
*unit
)
1559 struct dwarf2_debug
*stash
= unit
->stash
;
1560 if (! stash
->dwarf_ranges_buffer
)
1562 bfd
*abfd
= unit
->abfd
;
1565 msec
= bfd_get_section_by_name (abfd
, ".debug_ranges");
1568 (*_bfd_error_handler
) (_("Dwarf Error: Can't find .debug_ranges section."));
1569 bfd_set_error (bfd_error_bad_value
);
1573 stash
->dwarf_ranges_size
= msec
->size
;
1574 stash
->dwarf_ranges_buffer
1575 = bfd_simple_get_relocated_section_contents (abfd
, msec
, NULL
,
1577 if (! stash
->dwarf_ranges_buffer
)
1583 /* Function table functions. */
1585 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1586 Note that we need to find the function that has the smallest
1587 range that contains ADDR, to handle inlined functions without
1588 depending upon them being ordered in TABLE by increasing range. */
1591 lookup_address_in_function_table (struct comp_unit
*unit
,
1593 struct funcinfo
**function_ptr
,
1594 const char **functionname_ptr
)
1596 struct funcinfo
* each_func
;
1597 struct funcinfo
* best_fit
= NULL
;
1598 struct arange
*arange
;
1600 for (each_func
= unit
->function_table
;
1602 each_func
= each_func
->prev_func
)
1604 for (arange
= &each_func
->arange
;
1606 arange
= arange
->next
)
1608 if (addr
>= arange
->low
&& addr
< arange
->high
)
1611 ((arange
->high
- arange
->low
) < (best_fit
->arange
.high
- best_fit
->arange
.low
)))
1612 best_fit
= each_func
;
1619 *functionname_ptr
= best_fit
->name
;
1620 *function_ptr
= best_fit
;
1629 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1630 and LINENUMBER_PTR, and return TRUE. */
1633 lookup_symbol_in_function_table (struct comp_unit
*unit
,
1636 const char **filename_ptr
,
1637 unsigned int *linenumber_ptr
)
1639 struct funcinfo
* each_func
;
1640 struct funcinfo
* best_fit
= NULL
;
1641 struct arange
*arange
;
1642 const char *name
= bfd_asymbol_name (sym
);
1643 asection
*sec
= bfd_get_section (sym
);
1645 for (each_func
= unit
->function_table
;
1647 each_func
= each_func
->prev_func
)
1649 for (arange
= &each_func
->arange
;
1651 arange
= arange
->next
)
1653 if ((!each_func
->sec
|| each_func
->sec
== sec
)
1654 && addr
>= arange
->low
1655 && addr
< arange
->high
1657 && strcmp (name
, each_func
->name
) == 0
1659 || ((arange
->high
- arange
->low
)
1660 < (best_fit
->arange
.high
- best_fit
->arange
.low
))))
1661 best_fit
= each_func
;
1667 best_fit
->sec
= sec
;
1668 *filename_ptr
= best_fit
->file
;
1669 *linenumber_ptr
= best_fit
->line
;
1676 /* Variable table functions. */
1678 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1679 LINENUMBER_PTR, and return TRUE. */
1682 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
1685 const char **filename_ptr
,
1686 unsigned int *linenumber_ptr
)
1688 const char *name
= bfd_asymbol_name (sym
);
1689 asection
*sec
= bfd_get_section (sym
);
1690 struct varinfo
* each
;
1692 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
1693 if (each
->stack
== 0
1694 && each
->file
!= NULL
1695 && each
->name
!= NULL
1696 && each
->addr
== addr
1697 && (!each
->sec
|| each
->sec
== sec
)
1698 && strcmp (name
, each
->name
) == 0)
1704 *filename_ptr
= each
->file
;
1705 *linenumber_ptr
= each
->line
;
1713 find_abstract_instance_name (struct comp_unit
*unit
, bfd_uint64_t die_ref
)
1715 bfd
*abfd
= unit
->abfd
;
1717 unsigned int abbrev_number
, bytes_read
, i
;
1718 struct abbrev_info
*abbrev
;
1719 struct attribute attr
;
1722 info_ptr
= unit
->info_ptr_unit
+ die_ref
;
1723 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1724 info_ptr
+= bytes_read
;
1728 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
1731 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
1733 bfd_set_error (bfd_error_bad_value
);
1737 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1739 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1743 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1747 case DW_AT_specification
:
1748 name
= find_abstract_instance_name (unit
, attr
.u
.val
);
1750 case DW_AT_MIPS_linkage_name
:
1763 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
, bfd_uint64_t offset
)
1765 bfd_byte
*ranges_ptr
;
1766 bfd_vma base_address
= unit
->base_address
;
1768 if (! unit
->stash
->dwarf_ranges_buffer
)
1770 if (! read_debug_ranges (unit
))
1773 ranges_ptr
= unit
->stash
->dwarf_ranges_buffer
+ offset
;
1780 if (unit
->addr_size
== 4)
1782 low_pc
= read_4_bytes (unit
->abfd
, ranges_ptr
);
1784 high_pc
= read_4_bytes (unit
->abfd
, ranges_ptr
);
1789 low_pc
= read_8_bytes (unit
->abfd
, ranges_ptr
);
1791 high_pc
= read_8_bytes (unit
->abfd
, ranges_ptr
);
1794 if (low_pc
== 0 && high_pc
== 0)
1796 if (low_pc
== -1UL && high_pc
!= -1UL)
1797 base_address
= high_pc
;
1799 arange_add (unit
->abfd
, arange
, base_address
+ low_pc
, base_address
+ high_pc
);
1803 /* DWARF2 Compilation unit functions. */
1805 /* Scan over each die in a comp. unit looking for functions to add
1806 to the function table and variables to the variable table. */
1809 scan_unit_for_symbols (struct comp_unit
*unit
)
1811 bfd
*abfd
= unit
->abfd
;
1812 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
1813 int nesting_level
= 1;
1814 struct funcinfo
**nested_funcs
;
1815 int nested_funcs_size
;
1817 /* Maintain a stack of in-scope functions and inlined functions, which we
1818 can use to set the caller_func field. */
1819 nested_funcs_size
= 32;
1820 nested_funcs
= bfd_malloc (nested_funcs_size
* sizeof (struct funcinfo
*));
1821 if (nested_funcs
== NULL
)
1823 nested_funcs
[nesting_level
] = 0;
1825 while (nesting_level
)
1827 unsigned int abbrev_number
, bytes_read
, i
;
1828 struct abbrev_info
*abbrev
;
1829 struct attribute attr
;
1830 struct funcinfo
*func
;
1831 struct varinfo
*var
;
1833 bfd_vma high_pc
= 0;
1835 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1836 info_ptr
+= bytes_read
;
1838 if (! abbrev_number
)
1844 abbrev
= lookup_abbrev (abbrev_number
,unit
->abbrevs
);
1847 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
1849 bfd_set_error (bfd_error_bad_value
);
1850 free (nested_funcs
);
1855 if (abbrev
->tag
== DW_TAG_subprogram
1856 || abbrev
->tag
== DW_TAG_entry_point
1857 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
1859 bfd_size_type amt
= sizeof (struct funcinfo
);
1860 func
= bfd_zalloc (abfd
, amt
);
1861 func
->tag
= abbrev
->tag
;
1862 func
->prev_func
= unit
->function_table
;
1863 unit
->function_table
= func
;
1864 BFD_ASSERT (!unit
->cached
);
1866 if (func
->tag
== DW_TAG_inlined_subroutine
)
1867 for (i
= nesting_level
- 1; i
>= 1; i
--)
1868 if (nested_funcs
[i
])
1870 func
->caller_func
= nested_funcs
[i
];
1873 nested_funcs
[nesting_level
] = func
;
1878 if (abbrev
->tag
== DW_TAG_variable
)
1880 bfd_size_type amt
= sizeof (struct varinfo
);
1881 var
= bfd_zalloc (abfd
, amt
);
1882 var
->tag
= abbrev
->tag
;
1884 var
->prev_var
= unit
->variable_table
;
1885 unit
->variable_table
= var
;
1886 BFD_ASSERT (!unit
->cached
);
1889 /* No inline function in scope at this nesting level. */
1890 nested_funcs
[nesting_level
] = 0;
1893 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1895 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
1901 case DW_AT_call_file
:
1902 func
->caller_file
= concat_filename (unit
->line_table
, attr
.u
.val
);
1905 case DW_AT_call_line
:
1906 func
->caller_line
= attr
.u
.val
;
1909 case DW_AT_abstract_origin
:
1910 func
->name
= find_abstract_instance_name (unit
, attr
.u
.val
);
1914 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1915 if (func
->name
== NULL
)
1916 func
->name
= attr
.u
.str
;
1919 case DW_AT_MIPS_linkage_name
:
1920 func
->name
= attr
.u
.str
;
1924 low_pc
= attr
.u
.val
;
1928 high_pc
= attr
.u
.val
;
1932 read_rangelist (unit
, &func
->arange
, attr
.u
.val
);
1935 case DW_AT_decl_file
:
1936 func
->file
= concat_filename (unit
->line_table
,
1940 case DW_AT_decl_line
:
1941 func
->line
= attr
.u
.val
;
1953 var
->name
= attr
.u
.str
;
1956 case DW_AT_decl_file
:
1957 var
->file
= concat_filename (unit
->line_table
,
1961 case DW_AT_decl_line
:
1962 var
->line
= attr
.u
.val
;
1965 case DW_AT_external
:
1966 if (attr
.u
.val
!= 0)
1970 case DW_AT_location
:
1974 case DW_FORM_block1
:
1975 case DW_FORM_block2
:
1976 case DW_FORM_block4
:
1977 if (*attr
.u
.blk
->data
== DW_OP_addr
)
1981 /* Verify that DW_OP_addr is the only opcode in the
1982 location, in which case the block size will be 1
1983 plus the address size. */
1984 /* ??? For TLS variables, gcc can emit
1985 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
1986 which we don't handle here yet. */
1987 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
1988 var
->addr
= bfd_get (unit
->addr_size
* 8,
1990 attr
.u
.blk
->data
+ 1);
2005 if (func
&& high_pc
!= 0)
2007 arange_add (unit
->abfd
, &func
->arange
, low_pc
, high_pc
);
2010 if (abbrev
->has_children
)
2014 if (nesting_level
>= nested_funcs_size
)
2016 struct funcinfo
**tmp
;
2018 nested_funcs_size
*= 2;
2019 tmp
= bfd_realloc (nested_funcs
,
2021 * sizeof (struct funcinfo
*)));
2024 free (nested_funcs
);
2029 nested_funcs
[nesting_level
] = 0;
2033 free (nested_funcs
);
2037 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
2038 includes the compilation unit header that proceeds the DIE's, but
2039 does not include the length field that precedes each compilation
2040 unit header. END_PTR points one past the end of this comp unit.
2041 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
2043 This routine does not read the whole compilation unit; only enough
2044 to get to the line number information for the compilation unit. */
2046 static struct comp_unit
*
2047 parse_comp_unit (struct dwarf2_debug
*stash
,
2048 bfd_vma unit_length
,
2049 bfd_byte
*info_ptr_unit
,
2050 unsigned int offset_size
)
2052 struct comp_unit
* unit
;
2053 unsigned int version
;
2054 bfd_uint64_t abbrev_offset
= 0;
2055 unsigned int addr_size
;
2056 struct abbrev_info
** abbrevs
;
2057 unsigned int abbrev_number
, bytes_read
, i
;
2058 struct abbrev_info
*abbrev
;
2059 struct attribute attr
;
2060 bfd_byte
*info_ptr
= stash
->info_ptr
;
2061 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
2064 bfd_vma high_pc
= 0;
2065 bfd
*abfd
= stash
->bfd
;
2067 version
= read_2_bytes (abfd
, info_ptr
);
2069 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
2070 if (offset_size
== 4)
2071 abbrev_offset
= read_4_bytes (abfd
, info_ptr
);
2073 abbrev_offset
= read_8_bytes (abfd
, info_ptr
);
2074 info_ptr
+= offset_size
;
2075 addr_size
= read_1_byte (abfd
, info_ptr
);
2080 (*_bfd_error_handler
) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version
);
2081 bfd_set_error (bfd_error_bad_value
);
2085 if (addr_size
> sizeof (bfd_vma
))
2087 (*_bfd_error_handler
) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
2089 (unsigned int) sizeof (bfd_vma
));
2090 bfd_set_error (bfd_error_bad_value
);
2094 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
2096 (*_bfd_error_handler
) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size
);
2097 bfd_set_error (bfd_error_bad_value
);
2101 /* Read the abbrevs for this compilation unit into a table. */
2102 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
);
2106 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
2107 info_ptr
+= bytes_read
;
2108 if (! abbrev_number
)
2110 (*_bfd_error_handler
) (_("Dwarf Error: Bad abbrev number: %u."),
2112 bfd_set_error (bfd_error_bad_value
);
2116 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
2119 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
2121 bfd_set_error (bfd_error_bad_value
);
2125 amt
= sizeof (struct comp_unit
);
2126 unit
= bfd_zalloc (abfd
, amt
);
2128 unit
->addr_size
= addr_size
;
2129 unit
->offset_size
= offset_size
;
2130 unit
->abbrevs
= abbrevs
;
2131 unit
->end_ptr
= end_ptr
;
2132 unit
->stash
= stash
;
2133 unit
->info_ptr_unit
= info_ptr_unit
;
2135 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2137 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
2139 /* Store the data if it is of an attribute we want to keep in a
2140 partial symbol table. */
2143 case DW_AT_stmt_list
:
2145 unit
->line_offset
= attr
.u
.val
;
2149 unit
->name
= attr
.u
.str
;
2153 low_pc
= attr
.u
.val
;
2154 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2155 this is the base address to use when reading location
2156 lists or range lists. */
2157 unit
->base_address
= low_pc
;
2161 high_pc
= attr
.u
.val
;
2165 read_rangelist (unit
, &unit
->arange
, attr
.u
.val
);
2168 case DW_AT_comp_dir
:
2170 char *comp_dir
= attr
.u
.str
;
2173 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2174 directory, get rid of it. */
2175 char *cp
= strchr (comp_dir
, ':');
2177 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
2180 unit
->comp_dir
= comp_dir
;
2190 arange_add (unit
->abfd
, &unit
->arange
, low_pc
, high_pc
);
2193 unit
->first_child_die_ptr
= info_ptr
;
2197 /* Return TRUE if UNIT may contain the address given by ADDR. When
2198 there are functions written entirely with inline asm statements, the
2199 range info in the compilation unit header may not be correct. We
2200 need to consult the line info table to see if a compilation unit
2201 really contains the given address. */
2204 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
2206 struct arange
*arange
;
2211 arange
= &unit
->arange
;
2214 if (addr
>= arange
->low
&& addr
< arange
->high
)
2216 arange
= arange
->next
;
2223 /* If UNIT contains ADDR, set the output parameters to the values for
2224 the line containing ADDR. The output parameters, FILENAME_PTR,
2225 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2228 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2232 comp_unit_find_nearest_line (struct comp_unit
*unit
,
2234 const char **filename_ptr
,
2235 const char **functionname_ptr
,
2236 unsigned int *linenumber_ptr
,
2237 struct dwarf2_debug
*stash
)
2241 struct funcinfo
*function
;
2246 if (! unit
->line_table
)
2248 if (! unit
->stmtlist
)
2254 unit
->line_table
= decode_line_info (unit
, stash
);
2256 if (! unit
->line_table
)
2262 if (unit
->first_child_die_ptr
< unit
->end_ptr
2263 && ! scan_unit_for_symbols (unit
))
2271 func_p
= lookup_address_in_function_table (unit
, addr
,
2272 &function
, functionname_ptr
);
2273 if (func_p
&& (function
->tag
== DW_TAG_inlined_subroutine
))
2274 stash
->inliner_chain
= function
;
2275 line_p
= lookup_address_in_line_info_table (unit
->line_table
, addr
,
2276 function
, filename_ptr
,
2278 return line_p
|| func_p
;
2281 /* Check to see if line info is already decoded in a comp_unit.
2282 If not, decode it. Returns TRUE if no errors were encountered;
2286 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
,
2287 struct dwarf2_debug
*stash
)
2292 if (! unit
->line_table
)
2294 if (! unit
->stmtlist
)
2300 unit
->line_table
= decode_line_info (unit
, stash
);
2302 if (! unit
->line_table
)
2308 if (unit
->first_child_die_ptr
< unit
->end_ptr
2309 && ! scan_unit_for_symbols (unit
))
2319 /* If UNIT contains SYM at ADDR, set the output parameters to the
2320 values for the line containing SYM. The output parameters,
2321 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2324 Return TRUE if UNIT contains SYM, and no errors were encountered;
2328 comp_unit_find_line (struct comp_unit
*unit
,
2331 const char **filename_ptr
,
2332 unsigned int *linenumber_ptr
,
2333 struct dwarf2_debug
*stash
)
2335 if (!comp_unit_maybe_decode_line_info (unit
, stash
))
2338 if (sym
->flags
& BSF_FUNCTION
)
2339 return lookup_symbol_in_function_table (unit
, sym
, addr
,
2343 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
2348 static struct funcinfo
*
2349 reverse_funcinfo_list (struct funcinfo
*head
)
2351 struct funcinfo
*rhead
;
2352 struct funcinfo
*temp
;
2354 for (rhead
= NULL
; head
; head
= temp
)
2356 temp
= head
->prev_func
;
2357 head
->prev_func
= rhead
;
2363 static struct varinfo
*
2364 reverse_varinfo_list (struct varinfo
*head
)
2366 struct varinfo
*rhead
;
2367 struct varinfo
*temp
;
2369 for (rhead
= NULL
; head
; head
= temp
)
2371 temp
= head
->prev_var
;
2372 head
->prev_var
= rhead
;
2378 /* Extract all interesting funcinfos and varinfos of a compilation
2379 unit into hash tables for faster lookup. Returns TRUE if no
2380 errors were enountered; FALSE otherwise. */
2383 comp_unit_hash_info (struct dwarf2_debug
*stash
,
2384 struct comp_unit
*unit
,
2385 struct info_hash_table
*funcinfo_hash_table
,
2386 struct info_hash_table
*varinfo_hash_table
)
2388 struct funcinfo
* each_func
;
2389 struct varinfo
* each_var
;
2390 bfd_boolean okay
= TRUE
;
2392 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
2394 if (!comp_unit_maybe_decode_line_info (unit
, stash
))
2397 BFD_ASSERT (!unit
->cached
);
2399 /* To preserve the original search order, we went to visit the function
2400 infos in the reversed order of the list. However, making the list
2401 bi-directional use quite a bit of extra memory. So we reverse
2402 the list first, traverse the list in the now reversed order and
2403 finally reverse the list again to get back the original order. */
2404 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
2405 for (each_func
= unit
->function_table
;
2407 each_func
= each_func
->prev_func
)
2409 /* Skip nameless functions. */
2410 if (each_func
->name
)
2411 /* There is no need to copy name string into hash table as
2412 name string is either in the dwarf string buffer or
2413 info in the stash. */
2414 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
2415 (void*) each_func
, FALSE
);
2417 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
2421 /* We do the same for variable infos. */
2422 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
2423 for (each_var
= unit
->variable_table
;
2425 each_var
= each_var
->prev_var
)
2427 /* Skip stack vars and vars with no files or names. */
2428 if (each_var
->stack
== 0
2429 && each_var
->file
!= NULL
2430 && each_var
->name
!= NULL
)
2431 /* There is no need to copy name string into hash table as
2432 name string is either in the dwarf string buffer or
2433 info in the stash. */
2434 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
2435 (void*) each_var
, FALSE
);
2438 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
2439 unit
->cached
= TRUE
;
2443 /* Locate a section in a BFD containing debugging info. The search starts
2444 from the section after AFTER_SEC, or from the first section in the BFD if
2445 AFTER_SEC is NULL. The search works by examining the names of the
2446 sections. There are two permissiable names. The first is .debug_info.
2447 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2448 This is a variation on the .debug_info section which has a checksum
2449 describing the contents appended onto the name. This allows the linker to
2450 identify and discard duplicate debugging sections for different
2451 compilation units. */
2452 #define DWARF2_DEBUG_INFO ".debug_info"
2453 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2456 find_debug_info (bfd
*abfd
, asection
*after_sec
)
2460 msec
= after_sec
!= NULL
? after_sec
->next
: abfd
->sections
;
2464 if (strcmp (msec
->name
, DWARF2_DEBUG_INFO
) == 0)
2467 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
2476 /* Unset vmas for loadable sections in STASH. */
2479 unset_sections (struct dwarf2_debug
*stash
)
2482 struct loadable_section
*p
;
2484 i
= stash
->loadable_section_count
;
2485 p
= stash
->loadable_sections
;
2486 for (; i
> 0; i
--, p
++)
2487 p
->section
->vma
= 0;
2490 /* Set unique vmas for loadable sections in ABFD and save vmas in
2491 STASH for unset_sections. */
2494 place_sections (bfd
*abfd
, struct dwarf2_debug
*stash
)
2496 struct loadable_section
*p
;
2499 if (stash
->loadable_section_count
!= 0)
2501 i
= stash
->loadable_section_count
;
2502 p
= stash
->loadable_sections
;
2503 for (; i
> 0; i
--, p
++)
2504 p
->section
->vma
= p
->adj_vma
;
2509 bfd_vma last_vma
= 0;
2511 struct loadable_section
*p
;
2514 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
2518 if (sect
->vma
!= 0 || (sect
->flags
& SEC_LOAD
) == 0)
2521 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
2528 amt
= i
* sizeof (struct loadable_section
);
2529 p
= (struct loadable_section
*) bfd_zalloc (abfd
, amt
);
2533 stash
->loadable_sections
= p
;
2534 stash
->loadable_section_count
= i
;
2536 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
2540 if (sect
->vma
!= 0 || (sect
->flags
& SEC_LOAD
) == 0)
2543 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
2550 /* Align the new address to the current section
2552 last_vma
= ((last_vma
2553 + ~((bfd_vma
) -1 << sect
->alignment_power
))
2554 & ((bfd_vma
) -1 << sect
->alignment_power
));
2555 sect
->vma
= last_vma
;
2557 p
->adj_vma
= sect
->vma
;
2558 last_vma
+= sect
->vma
+ sz
;
2567 /* Look up a funcinfo by name using the given info hash table. If found,
2568 also update the locations pointed to by filename_ptr and linenumber_ptr.
2570 This function returns TRUE if a funcinfo that matches the given symbol
2571 and address is found with any error; otherwise it returns FALSE. */
2574 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
2577 const char **filename_ptr
,
2578 unsigned int *linenumber_ptr
)
2580 struct funcinfo
* each_func
;
2581 struct funcinfo
* best_fit
= NULL
;
2582 struct info_list_node
*node
;
2583 struct arange
*arange
;
2584 const char *name
= bfd_asymbol_name (sym
);
2585 asection
*sec
= bfd_get_section (sym
);
2587 for (node
= lookup_info_hash_table (hash_table
, name
);
2591 each_func
= node
->info
;
2592 for (arange
= &each_func
->arange
;
2594 arange
= arange
->next
)
2596 if ((!each_func
->sec
|| each_func
->sec
== sec
)
2597 && addr
>= arange
->low
2598 && addr
< arange
->high
2600 || ((arange
->high
- arange
->low
)
2601 < (best_fit
->arange
.high
- best_fit
->arange
.low
))))
2602 best_fit
= each_func
;
2608 best_fit
->sec
= sec
;
2609 *filename_ptr
= best_fit
->file
;
2610 *linenumber_ptr
= best_fit
->line
;
2617 /* Look up a varinfo by name using the given info hash table. If found,
2618 also update the locations pointed to by filename_ptr and linenumber_ptr.
2620 This function returns TRUE if a varinfo that matches the given symbol
2621 and address is found with any error; otherwise it returns FALSE. */
2624 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
2627 const char **filename_ptr
,
2628 unsigned int *linenumber_ptr
)
2630 const char *name
= bfd_asymbol_name (sym
);
2631 asection
*sec
= bfd_get_section (sym
);
2632 struct varinfo
* each
;
2633 struct info_list_node
*node
;
2635 for (node
= lookup_info_hash_table (hash_table
, name
);
2640 if (each
->addr
== addr
2641 && (!each
->sec
|| each
->sec
== sec
))
2644 *filename_ptr
= each
->file
;
2645 *linenumber_ptr
= each
->line
;
2653 /* Update the funcinfo and varinfo info hash tables if they are
2654 not up to date. Returns TRUE if there is no error; otherwise
2655 returns FALSE and disable the info hash tables. */
2658 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
2660 struct comp_unit
*each
;
2662 /* Exit if hash tables are up-to-date. */
2663 if (stash
->all_comp_units
== stash
->hash_units_head
)
2666 if (stash
->hash_units_head
)
2667 each
= stash
->hash_units_head
->prev_unit
;
2669 each
= stash
->last_comp_unit
;
2673 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
2674 stash
->varinfo_hash_table
))
2676 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
2679 each
= each
->prev_unit
;
2682 stash
->hash_units_head
= stash
->all_comp_units
;
2686 /* Check consistency of info hash tables. This is for debugging only. */
2688 static void ATTRIBUTE_UNUSED
2689 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
2691 struct comp_unit
*each_unit
;
2692 struct funcinfo
*each_func
;
2693 struct varinfo
*each_var
;
2694 struct info_list_node
*node
;
2697 for (each_unit
= stash
->all_comp_units
;
2699 each_unit
= each_unit
->next_unit
)
2701 for (each_func
= each_unit
->function_table
;
2703 each_func
= each_func
->prev_func
)
2705 if (!each_func
->name
)
2707 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
2711 while (node
&& !found
)
2713 found
= node
->info
== each_func
;
2719 for (each_var
= each_unit
->variable_table
;
2721 each_var
= each_var
->prev_var
)
2723 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
2725 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
2729 while (node
&& !found
)
2731 found
= node
->info
== each_var
;
2739 /* Check to see if we want to enable the info hash tables, which consume
2740 quite a bit of memory. Currently we only check the number times
2741 bfd_dwarf2_find_line is called. In the future, we may also want to
2742 take the number of symbols into account. */
2745 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
2747 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
2749 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
2752 /* FIXME: Maybe we should check the reduce_memory_overheads
2753 and optimize fields in the bfd_link_info structure ? */
2755 /* Create hash tables. */
2756 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
2757 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
2758 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
2760 /* Turn off info hashes if any allocation above fails. */
2761 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
2764 /* We need a forced update so that the info hash tables will
2765 be created even though there is no compilation unit. That
2766 happens if STASH_INFO_HASH_TRIGGER is 0. */
2767 stash_maybe_update_info_hash_tables (stash
);
2768 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
2771 /* Find the file and line associated with a symbol and address using the
2772 info hash tables of a stash. If there is a match, the function returns
2773 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
2774 otherwise it returns FALSE. */
2777 stash_find_line_fast (struct dwarf2_debug
*stash
,
2780 const char **filename_ptr
,
2781 unsigned int *linenumber_ptr
)
2783 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
2785 if (sym
->flags
& BSF_FUNCTION
)
2786 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
2787 filename_ptr
, linenumber_ptr
);
2788 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
2789 filename_ptr
, linenumber_ptr
);
2792 /* Find the source code location of SYMBOL. If SYMBOL is NULL
2793 then find the nearest source code location corresponding to
2794 the address SECTION + OFFSET.
2795 Returns TRUE if the line is found without error and fills in
2796 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
2797 NULL the FUNCTIONNAME_PTR is also filled in.
2798 SYMBOLS contains the symbol table for ABFD.
2799 ADDR_SIZE is the number of bytes in the initial .debug_info length
2800 field and in the abbreviation offset, or zero to indicate that the
2801 default value should be used. */
2804 find_line (bfd
*abfd
,
2809 const char **filename_ptr
,
2810 const char **functionname_ptr
,
2811 unsigned int *linenumber_ptr
,
2812 unsigned int addr_size
,
2815 /* Read each compilation unit from the section .debug_info, and check
2816 to see if it contains the address we are searching for. If yes,
2817 lookup the address, and return the line number info. If no, go
2818 on to the next compilation unit.
2820 We keep a list of all the previously read compilation units, and
2821 a pointer to the next un-read compilation unit. Check the
2822 previously read units before reading more. */
2823 struct dwarf2_debug
*stash
;
2824 /* What address are we looking for? */
2826 struct comp_unit
* each
;
2827 bfd_vma found
= FALSE
;
2828 bfd_boolean do_line
;
2834 bfd_size_type amt
= sizeof (struct dwarf2_debug
);
2836 stash
= bfd_zalloc (abfd
, amt
);
2841 /* In a relocatable file, 2 functions may have the same address.
2842 We change the section vma so that they won't overlap. */
2843 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
2845 if (! place_sections (abfd
, stash
))
2849 do_line
= (section
== NULL
2851 && functionname_ptr
== NULL
2855 addr
= symbol
->value
;
2856 section
= bfd_get_section (symbol
);
2858 else if (section
!= NULL
2859 && functionname_ptr
!= NULL
2865 if (section
->output_section
)
2866 addr
+= section
->output_section
->vma
+ section
->output_offset
;
2868 addr
+= section
->vma
;
2869 *filename_ptr
= NULL
;
2871 *functionname_ptr
= NULL
;
2872 *linenumber_ptr
= 0;
2877 bfd_size_type total_size
;
2882 msec
= find_debug_info (abfd
, NULL
);
2885 char * debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
2887 if (debug_filename
== NULL
)
2888 /* No dwarf2 info, and no gnu_debuglink to follow.
2889 Note that at this point the stash has been allocated, but
2890 contains zeros. This lets future calls to this function
2891 fail more quickly. */
2894 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
2895 || ! bfd_check_format (debug_bfd
, bfd_object
)
2896 || (msec
= find_debug_info (debug_bfd
, NULL
)) == NULL
)
2899 bfd_close (debug_bfd
);
2900 /* FIXME: Should we report our failure to follow the debuglink ? */
2901 free (debug_filename
);
2908 /* There can be more than one DWARF2 info section in a BFD these days.
2909 Read them all in and produce one large stash. We do this in two
2910 passes - in the first pass we just accumulate the section sizes.
2911 In the second pass we read in the section's contents. The allows
2912 us to avoid reallocing the data as we add sections to the stash. */
2913 for (total_size
= 0; msec
; msec
= find_debug_info (debug_bfd
, msec
))
2914 total_size
+= msec
->size
;
2916 stash
->info_ptr
= bfd_alloc (debug_bfd
, total_size
);
2917 if (stash
->info_ptr
== NULL
)
2920 stash
->info_ptr_end
= stash
->info_ptr
;
2922 for (msec
= find_debug_info (debug_bfd
, NULL
);
2924 msec
= find_debug_info (debug_bfd
, msec
))
2927 bfd_size_type start
;
2933 start
= stash
->info_ptr_end
- stash
->info_ptr
;
2935 if ((bfd_simple_get_relocated_section_contents
2936 (debug_bfd
, msec
, stash
->info_ptr
+ start
, symbols
)) == NULL
)
2939 stash
->info_ptr_end
= stash
->info_ptr
+ start
+ size
;
2942 BFD_ASSERT (stash
->info_ptr_end
== stash
->info_ptr
+ total_size
);
2944 stash
->sec
= find_debug_info (debug_bfd
, NULL
);
2945 stash
->sec_info_ptr
= stash
->info_ptr
;
2946 stash
->syms
= symbols
;
2947 stash
->bfd
= debug_bfd
;
2950 /* A null info_ptr indicates that there is no dwarf2 info
2951 (or that an error occured while setting up the stash). */
2952 if (! stash
->info_ptr
)
2955 stash
->inliner_chain
= NULL
;
2957 /* Check the previously read comp. units first. */
2960 /* The info hash tables use quite a bit of memory. We may not want to
2961 always use them. We use some heuristics to decide if and when to
2963 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
2964 stash_maybe_enable_info_hash_tables (abfd
, stash
);
2966 /* Keep info hash table up to date if they are available. Note that we
2967 may disable the hash tables if there is any error duing update. */
2968 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
2969 stash_maybe_update_info_hash_tables (stash
);
2971 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
2973 found
= stash_find_line_fast (stash
, symbol
, addr
, filename_ptr
,
2980 /* Check the previously read comp. units first. */
2981 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
2982 if ((symbol
->flags
& BSF_FUNCTION
) == 0
2983 || comp_unit_contains_address (each
, addr
))
2985 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
2986 linenumber_ptr
, stash
);
2994 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
2996 found
= (comp_unit_contains_address (each
, addr
)
2997 && comp_unit_find_nearest_line (each
, addr
,
3007 /* The DWARF2 spec says that the initial length field, and the
3008 offset of the abbreviation table, should both be 4-byte values.
3009 However, some compilers do things differently. */
3012 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
3014 /* Read each remaining comp. units checking each as they are read. */
3015 while (stash
->info_ptr
< stash
->info_ptr_end
)
3018 unsigned int offset_size
= addr_size
;
3019 bfd_byte
*info_ptr_unit
= stash
->info_ptr
;
3021 length
= read_4_bytes (stash
->bfd
, stash
->info_ptr
);
3022 /* A 0xffffff length is the DWARF3 way of indicating
3023 we use 64-bit offsets, instead of 32-bit offsets. */
3024 if (length
== 0xffffffff)
3027 length
= read_8_bytes (stash
->bfd
, stash
->info_ptr
+ 4);
3028 stash
->info_ptr
+= 12;
3030 /* A zero length is the IRIX way of indicating 64-bit offsets,
3031 mostly because the 64-bit length will generally fit in 32
3032 bits, and the endianness helps. */
3033 else if (length
== 0)
3036 length
= read_4_bytes (stash
->bfd
, stash
->info_ptr
+ 4);
3037 stash
->info_ptr
+= 8;
3039 /* In the absence of the hints above, we assume 32-bit DWARF2
3040 offsets even for targets with 64-bit addresses, because:
3041 a) most of the time these targets will not have generated
3042 more than 2Gb of debug info and so will not need 64-bit
3045 b) if they do use 64-bit offsets but they are not using
3046 the size hints that are tested for above then they are
3047 not conforming to the DWARF3 standard anyway. */
3048 else if (addr_size
== 8)
3051 stash
->info_ptr
+= 4;
3054 stash
->info_ptr
+= 4;
3058 each
= parse_comp_unit (stash
, length
, info_ptr_unit
,
3060 stash
->info_ptr
+= length
;
3062 if ((bfd_vma
) (stash
->info_ptr
- stash
->sec_info_ptr
)
3063 == stash
->sec
->size
)
3065 stash
->sec
= find_debug_info (stash
->bfd
, stash
->sec
);
3066 stash
->sec_info_ptr
= stash
->info_ptr
;
3071 if (stash
->all_comp_units
)
3072 stash
->all_comp_units
->prev_unit
= each
;
3074 stash
->last_comp_unit
= each
;
3076 each
->next_unit
= stash
->all_comp_units
;
3077 stash
->all_comp_units
= each
;
3079 /* DW_AT_low_pc and DW_AT_high_pc are optional for
3080 compilation units. If we don't have them (i.e.,
3081 unit->high == 0), we need to consult the line info
3082 table to see if a compilation unit contains the given
3085 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
3086 || each
->arange
.high
== 0
3087 || comp_unit_contains_address (each
, addr
))
3088 && comp_unit_find_line (each
, symbol
, addr
,
3093 found
= ((each
->arange
.high
== 0
3094 || comp_unit_contains_address (each
, addr
))
3095 && comp_unit_find_nearest_line (each
, addr
,
3107 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
3108 unset_sections (stash
);
3113 /* The DWARF2 version of find_nearest_line.
3114 Return TRUE if the line is found without error. */
3117 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
3121 const char **filename_ptr
,
3122 const char **functionname_ptr
,
3123 unsigned int *linenumber_ptr
,
3124 unsigned int addr_size
,
3127 return find_line (abfd
, section
, offset
, NULL
, symbols
, filename_ptr
,
3128 functionname_ptr
, linenumber_ptr
, addr_size
,
3132 /* The DWARF2 version of find_line.
3133 Return TRUE if the line is found without error. */
3136 _bfd_dwarf2_find_line (bfd
*abfd
,
3139 const char **filename_ptr
,
3140 unsigned int *linenumber_ptr
,
3141 unsigned int addr_size
,
3144 return find_line (abfd
, NULL
, 0, symbol
, symbols
, filename_ptr
,
3145 NULL
, linenumber_ptr
, addr_size
,
3150 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
3151 const char **filename_ptr
,
3152 const char **functionname_ptr
,
3153 unsigned int *linenumber_ptr
,
3156 struct dwarf2_debug
*stash
;
3161 struct funcinfo
*func
= stash
->inliner_chain
;
3163 if (func
&& func
->caller_func
)
3165 *filename_ptr
= func
->caller_file
;
3166 *functionname_ptr
= func
->caller_func
->name
;
3167 *linenumber_ptr
= func
->caller_line
;
3168 stash
->inliner_chain
= func
->caller_func
;
3177 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
)
3179 struct comp_unit
*each
;
3180 struct dwarf2_debug
*stash
;
3182 if (abfd
== NULL
|| elf_tdata (abfd
) == NULL
)
3185 stash
= elf_tdata (abfd
)->dwarf2_find_line_info
;
3190 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
3192 struct abbrev_info
**abbrevs
= each
->abbrevs
;
3195 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
3197 struct abbrev_info
*abbrev
= abbrevs
[i
];
3201 free (abbrev
->attrs
);
3202 abbrev
= abbrev
->next
;
3206 if (each
->line_table
)
3208 free (each
->line_table
->dirs
);
3209 free (each
->line_table
->files
);
3213 free (stash
->dwarf_abbrev_buffer
);
3214 free (stash
->dwarf_line_buffer
);
3215 free (stash
->dwarf_ranges_buffer
);