2 Copyright (C) 1994-2014 Free Software Foundation, Inc.
4 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
7 From the dwarf2read.c header:
8 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
9 Inc. with support from Florida State University (under contract
10 with the Ada Joint Program Office), and Silicon Graphics, Inc.
11 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
12 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
13 support in dwarfread.c
15 This file is part of BFD.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 3 of the License, or (at
20 your option) any later version.
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
30 MA 02110-1301, USA. */
34 #include "libiberty.h"
39 /* The data in the .debug_line statement prologue looks like this. */
44 unsigned short version
;
45 bfd_vma prologue_length
;
46 unsigned char minimum_instruction_length
;
47 unsigned char maximum_ops_per_insn
;
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 /* Names of the debug sections. */
93 const struct dwarf_debug_section
*debug_sections
;
95 /* The next unread compilation unit within the .debug_info section.
96 Zero indicates that the .debug_info section has not been loaded
100 /* Pointer to the end of the .debug_info section memory buffer. */
101 bfd_byte
*info_ptr_end
;
103 /* Pointer to the bfd, section and address of the beginning of the
104 section. The bfd might be different than expected because of
105 gnu_debuglink sections. */
108 bfd_byte
*sec_info_ptr
;
110 /* Support for alternate debug info sections created by the DWZ utility:
111 This includes a pointer to an alternate bfd which contains *extra*,
112 possibly duplicate debug sections, and pointers to the loaded
113 .debug_str and .debug_info sections from this bfd. */
115 bfd_byte
* alt_dwarf_str_buffer
;
116 bfd_size_type alt_dwarf_str_size
;
117 bfd_byte
* alt_dwarf_info_buffer
;
118 bfd_size_type alt_dwarf_info_size
;
120 /* A pointer to the memory block allocated for info_ptr. Neither
121 info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
122 beginning of the malloc block. This is used only to free the
124 bfd_byte
*info_ptr_memory
;
126 /* Pointer to the symbol table. */
129 /* Pointer to the .debug_abbrev section loaded into memory. */
130 bfd_byte
*dwarf_abbrev_buffer
;
132 /* Length of the loaded .debug_abbrev section. */
133 bfd_size_type dwarf_abbrev_size
;
135 /* Buffer for decode_line_info. */
136 bfd_byte
*dwarf_line_buffer
;
138 /* Length of the loaded .debug_line section. */
139 bfd_size_type dwarf_line_size
;
141 /* Pointer to the .debug_str section loaded into memory. */
142 bfd_byte
*dwarf_str_buffer
;
144 /* Length of the loaded .debug_str section. */
145 bfd_size_type dwarf_str_size
;
147 /* Pointer to the .debug_ranges section loaded into memory. */
148 bfd_byte
*dwarf_ranges_buffer
;
150 /* Length of the loaded .debug_ranges section. */
151 bfd_size_type dwarf_ranges_size
;
153 /* If the most recent call to bfd_find_nearest_line was given an
154 address in an inlined function, preserve a pointer into the
155 calling chain for subsequent calls to bfd_find_inliner_info to
157 struct funcinfo
*inliner_chain
;
159 /* Number of sections whose VMA we must adjust. */
160 unsigned int adjusted_section_count
;
162 /* Array of sections with adjusted VMA. */
163 struct adjusted_section
*adjusted_sections
;
165 /* Number of times find_line is called. This is used in
166 the heuristic for enabling the info hash tables. */
169 #define STASH_INFO_HASH_TRIGGER 100
171 /* Hash table mapping symbol names to function infos. */
172 struct info_hash_table
*funcinfo_hash_table
;
174 /* Hash table mapping symbol names to variable infos. */
175 struct info_hash_table
*varinfo_hash_table
;
177 /* Head of comp_unit list in the last hash table update. */
178 struct comp_unit
*hash_units_head
;
180 /* Status of info hash. */
181 int info_hash_status
;
182 #define STASH_INFO_HASH_OFF 0
183 #define STASH_INFO_HASH_ON 1
184 #define STASH_INFO_HASH_DISABLED 2
186 /* True if we opened bfd_ptr. */
187 bfd_boolean close_on_cleanup
;
197 /* A minimal decoding of DWARF2 compilation units. We only decode
198 what's needed to get to the line number information. */
202 /* Chain the previously read compilation units. */
203 struct comp_unit
*next_unit
;
205 /* Likewise, chain the compilation unit read after this one.
206 The comp units are stored in reversed reading order. */
207 struct comp_unit
*prev_unit
;
209 /* Keep the bfd convenient (for memory allocation). */
212 /* The lowest and highest addresses contained in this compilation
213 unit as specified in the compilation unit header. */
214 struct arange arange
;
216 /* The DW_AT_name attribute (for error messages). */
219 /* The abbrev hash table. */
220 struct abbrev_info
**abbrevs
;
222 /* Note that an error was found by comp_unit_find_nearest_line. */
225 /* The DW_AT_comp_dir attribute. */
228 /* TRUE if there is a line number table associated with this comp. unit. */
231 /* Pointer to the current comp_unit so that we can find a given entry
233 bfd_byte
*info_ptr_unit
;
235 /* Pointer to the start of the debug section, for DW_FORM_ref_addr. */
236 bfd_byte
*sec_info_ptr
;
238 /* The offset into .debug_line of the line number table. */
239 unsigned long line_offset
;
241 /* Pointer to the first child die for the comp unit. */
242 bfd_byte
*first_child_die_ptr
;
244 /* The end of the comp unit. */
247 /* The decoded line number, NULL if not yet decoded. */
248 struct line_info_table
*line_table
;
250 /* A list of the functions found in this comp. unit. */
251 struct funcinfo
*function_table
;
253 /* A list of the variables found in this comp. unit. */
254 struct varinfo
*variable_table
;
256 /* Pointer to dwarf2_debug structure. */
257 struct dwarf2_debug
*stash
;
259 /* DWARF format version for this unit - from unit header. */
262 /* Address size for this unit - from unit header. */
263 unsigned char addr_size
;
265 /* Offset size for this unit - from unit header. */
266 unsigned char offset_size
;
268 /* Base address for this unit - from DW_AT_low_pc attribute of
269 DW_TAG_compile_unit DIE */
270 bfd_vma base_address
;
272 /* TRUE if symbols are cached in hash table for faster lookup by name. */
276 /* This data structure holds the information of an abbrev. */
279 unsigned int number
; /* Number identifying abbrev. */
280 enum dwarf_tag tag
; /* DWARF tag. */
281 int has_children
; /* Boolean. */
282 unsigned int num_attrs
; /* Number of attributes. */
283 struct attr_abbrev
*attrs
; /* An array of attribute descriptions. */
284 struct abbrev_info
*next
; /* Next in chain. */
289 enum dwarf_attribute name
;
290 enum dwarf_form form
;
293 /* Map of uncompressed DWARF debug section name to compressed one. It
294 is terminated by NULL uncompressed_name. */
296 const struct dwarf_debug_section dwarf_debug_sections
[] =
298 { ".debug_abbrev", ".zdebug_abbrev" },
299 { ".debug_aranges", ".zdebug_aranges" },
300 { ".debug_frame", ".zdebug_frame" },
301 { ".debug_info", ".zdebug_info" },
302 { ".debug_info", ".zdebug_info" },
303 { ".debug_line", ".zdebug_line" },
304 { ".debug_loc", ".zdebug_loc" },
305 { ".debug_macinfo", ".zdebug_macinfo" },
306 { ".debug_macro", ".zdebug_macro" },
307 { ".debug_pubnames", ".zdebug_pubnames" },
308 { ".debug_pubtypes", ".zdebug_pubtypes" },
309 { ".debug_ranges", ".zdebug_ranges" },
310 { ".debug_static_func", ".zdebug_static_func" },
311 { ".debug_static_vars", ".zdebug_static_vars" },
312 { ".debug_str", ".zdebug_str", },
313 { ".debug_str", ".zdebug_str", },
314 { ".debug_types", ".zdebug_types" },
315 /* GNU DWARF 1 extensions */
316 { ".debug_sfnames", ".zdebug_sfnames" },
317 { ".debug_srcinfo", ".zebug_srcinfo" },
318 /* SGI/MIPS DWARF 2 extensions */
319 { ".debug_funcnames", ".zdebug_funcnames" },
320 { ".debug_typenames", ".zdebug_typenames" },
321 { ".debug_varnames", ".zdebug_varnames" },
322 { ".debug_weaknames", ".zdebug_weaknames" },
326 /* NB/ Numbers in this enum must match up with indicies
327 into the dwarf_debug_sections[] array above. */
328 enum dwarf_debug_section_enum
355 #ifndef ABBREV_HASH_SIZE
356 #define ABBREV_HASH_SIZE 121
358 #ifndef ATTR_ALLOC_CHUNK
359 #define ATTR_ALLOC_CHUNK 4
362 /* Variable and function hash tables. This is used to speed up look-up
363 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
364 In order to share code between variable and function infos, we use
365 a list of untyped pointer for all variable/function info associated with
366 a symbol. We waste a bit of memory for list with one node but that
367 simplifies the code. */
369 struct info_list_node
371 struct info_list_node
*next
;
375 /* Info hash entry. */
376 struct info_hash_entry
378 struct bfd_hash_entry root
;
379 struct info_list_node
*head
;
382 struct info_hash_table
384 struct bfd_hash_table base
;
387 /* Function to create a new entry in info hash table. */
389 static struct bfd_hash_entry
*
390 info_hash_table_newfunc (struct bfd_hash_entry
*entry
,
391 struct bfd_hash_table
*table
,
394 struct info_hash_entry
*ret
= (struct info_hash_entry
*) entry
;
396 /* Allocate the structure if it has not already been allocated by a
400 ret
= (struct info_hash_entry
*) bfd_hash_allocate (table
,
406 /* Call the allocation method of the base class. */
407 ret
= ((struct info_hash_entry
*)
408 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
410 /* Initialize the local fields here. */
414 return (struct bfd_hash_entry
*) ret
;
417 /* Function to create a new info hash table. It returns a pointer to the
418 newly created table or NULL if there is any error. We need abfd
419 solely for memory allocation. */
421 static struct info_hash_table
*
422 create_info_hash_table (bfd
*abfd
)
424 struct info_hash_table
*hash_table
;
426 hash_table
= ((struct info_hash_table
*)
427 bfd_alloc (abfd
, sizeof (struct info_hash_table
)));
431 if (!bfd_hash_table_init (&hash_table
->base
, info_hash_table_newfunc
,
432 sizeof (struct info_hash_entry
)))
434 bfd_release (abfd
, hash_table
);
441 /* Insert an info entry into an info hash table. We do not check of
442 duplicate entries. Also, the caller need to guarantee that the
443 right type of info in inserted as info is passed as a void* pointer.
444 This function returns true if there is no error. */
447 insert_info_hash_table (struct info_hash_table
*hash_table
,
452 struct info_hash_entry
*entry
;
453 struct info_list_node
*node
;
455 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
,
460 node
= (struct info_list_node
*) bfd_hash_allocate (&hash_table
->base
,
466 node
->next
= entry
->head
;
472 /* Look up an info entry list from an info hash table. Return NULL
475 static struct info_list_node
*
476 lookup_info_hash_table (struct info_hash_table
*hash_table
, const char *key
)
478 struct info_hash_entry
*entry
;
480 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
, key
,
482 return entry
? entry
->head
: NULL
;
485 /* Read a section into its appropriate place in the dwarf2_debug
486 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
487 not NULL, use bfd_simple_get_relocated_section_contents to read the
488 section contents, otherwise use bfd_get_section_contents. Fail if
489 the located section does not contain at least OFFSET bytes. */
492 read_section (bfd
* abfd
,
493 const struct dwarf_debug_section
*sec
,
496 bfd_byte
** section_buffer
,
497 bfd_size_type
* section_size
)
500 const char *section_name
= sec
->uncompressed_name
;
502 /* The section may have already been read. */
503 if (*section_buffer
== NULL
)
505 msec
= bfd_get_section_by_name (abfd
, section_name
);
508 section_name
= sec
->compressed_name
;
509 if (section_name
!= NULL
)
510 msec
= bfd_get_section_by_name (abfd
, section_name
);
514 (*_bfd_error_handler
) (_("Dwarf Error: Can't find %s section."),
515 sec
->uncompressed_name
);
516 bfd_set_error (bfd_error_bad_value
);
520 *section_size
= msec
->rawsize
? msec
->rawsize
: msec
->size
;
524 = bfd_simple_get_relocated_section_contents (abfd
, msec
, NULL
, syms
);
525 if (! *section_buffer
)
530 *section_buffer
= (bfd_byte
*) bfd_malloc (*section_size
);
531 if (! *section_buffer
)
533 if (! bfd_get_section_contents (abfd
, msec
, *section_buffer
,
539 /* It is possible to get a bad value for the offset into the section
540 that the client wants. Validate it here to avoid trouble later. */
541 if (offset
!= 0 && offset
>= *section_size
)
543 (*_bfd_error_handler
) (_("Dwarf Error: Offset (%lu)"
544 " greater than or equal to %s size (%lu)."),
545 (long) offset
, section_name
, *section_size
);
546 bfd_set_error (bfd_error_bad_value
);
554 The following function up to the END VERBATIM mark are
555 copied directly from dwarf2read.c. */
557 /* Read dwarf information from a buffer. */
560 read_1_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
)
562 return bfd_get_8 (abfd
, buf
);
566 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
)
568 return bfd_get_signed_8 (abfd
, buf
);
572 read_2_bytes (bfd
*abfd
, bfd_byte
*buf
)
574 return bfd_get_16 (abfd
, buf
);
578 read_4_bytes (bfd
*abfd
, bfd_byte
*buf
)
580 return bfd_get_32 (abfd
, buf
);
584 read_8_bytes (bfd
*abfd
, bfd_byte
*buf
)
586 return bfd_get_64 (abfd
, buf
);
590 read_n_bytes (bfd
*abfd ATTRIBUTE_UNUSED
,
592 unsigned int size ATTRIBUTE_UNUSED
)
598 read_string (bfd
*abfd ATTRIBUTE_UNUSED
,
600 unsigned int *bytes_read_ptr
)
602 /* Return a pointer to the embedded string. */
603 char *str
= (char *) buf
;
611 *bytes_read_ptr
= strlen (str
) + 1;
618 read_indirect_string (struct comp_unit
* unit
,
620 unsigned int * bytes_read_ptr
)
623 struct dwarf2_debug
*stash
= unit
->stash
;
626 if (unit
->offset_size
== 4)
627 offset
= read_4_bytes (unit
->abfd
, buf
);
629 offset
= read_8_bytes (unit
->abfd
, buf
);
631 *bytes_read_ptr
= unit
->offset_size
;
633 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
635 &stash
->dwarf_str_buffer
, &stash
->dwarf_str_size
))
638 str
= (char *) stash
->dwarf_str_buffer
+ offset
;
644 /* Like read_indirect_string but uses a .debug_str located in
645 an alternate filepointed to by the .gnu_debuglink section.
646 Used to impement DW_FORM_GNU_strp_alt. */
649 read_alt_indirect_string (struct comp_unit
* unit
,
651 unsigned int * bytes_read_ptr
)
654 struct dwarf2_debug
*stash
= unit
->stash
;
657 if (unit
->offset_size
== 4)
658 offset
= read_4_bytes (unit
->abfd
, buf
);
660 offset
= read_8_bytes (unit
->abfd
, buf
);
662 *bytes_read_ptr
= unit
->offset_size
;
664 if (stash
->alt_bfd_ptr
== NULL
)
667 char * debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
669 if (debug_filename
== NULL
)
672 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
673 || ! bfd_check_format (debug_bfd
, bfd_object
))
676 bfd_close (debug_bfd
);
678 /* FIXME: Should we report our failure to follow the debuglink ? */
679 free (debug_filename
);
682 stash
->alt_bfd_ptr
= debug_bfd
;
685 if (! read_section (unit
->stash
->alt_bfd_ptr
,
686 stash
->debug_sections
+ debug_str_alt
,
687 NULL
, /* FIXME: Do we need to load alternate symbols ? */
689 &stash
->alt_dwarf_str_buffer
,
690 &stash
->alt_dwarf_str_size
))
693 str
= (char *) stash
->alt_dwarf_str_buffer
+ offset
;
700 /* Resolve an alternate reference from UNIT at OFFSET.
701 Returns a pointer into the loaded alternate CU upon success
702 or NULL upon failure. */
705 read_alt_indirect_ref (struct comp_unit
* unit
,
708 struct dwarf2_debug
*stash
= unit
->stash
;
710 if (stash
->alt_bfd_ptr
== NULL
)
713 char * debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
715 if (debug_filename
== NULL
)
718 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
719 || ! bfd_check_format (debug_bfd
, bfd_object
))
722 bfd_close (debug_bfd
);
724 /* FIXME: Should we report our failure to follow the debuglink ? */
725 free (debug_filename
);
728 stash
->alt_bfd_ptr
= debug_bfd
;
731 if (! read_section (unit
->stash
->alt_bfd_ptr
,
732 stash
->debug_sections
+ debug_info_alt
,
733 NULL
, /* FIXME: Do we need to load alternate symbols ? */
735 &stash
->alt_dwarf_info_buffer
,
736 &stash
->alt_dwarf_info_size
))
739 return stash
->alt_dwarf_info_buffer
+ offset
;
743 read_address (struct comp_unit
*unit
, bfd_byte
*buf
)
745 int signed_vma
= get_elf_backend_data (unit
->abfd
)->sign_extend_vma
;
749 switch (unit
->addr_size
)
752 return bfd_get_signed_64 (unit
->abfd
, buf
);
754 return bfd_get_signed_32 (unit
->abfd
, buf
);
756 return bfd_get_signed_16 (unit
->abfd
, buf
);
763 switch (unit
->addr_size
)
766 return bfd_get_64 (unit
->abfd
, buf
);
768 return bfd_get_32 (unit
->abfd
, buf
);
770 return bfd_get_16 (unit
->abfd
, buf
);
777 /* Lookup an abbrev_info structure in the abbrev hash table. */
779 static struct abbrev_info
*
780 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
782 unsigned int hash_number
;
783 struct abbrev_info
*abbrev
;
785 hash_number
= number
% ABBREV_HASH_SIZE
;
786 abbrev
= abbrevs
[hash_number
];
790 if (abbrev
->number
== number
)
793 abbrev
= abbrev
->next
;
799 /* In DWARF version 2, the description of the debugging information is
800 stored in a separate .debug_abbrev section. Before we read any
801 dies from a section we read in all abbreviations and install them
804 static struct abbrev_info
**
805 read_abbrevs (bfd
*abfd
, bfd_uint64_t offset
, struct dwarf2_debug
*stash
)
807 struct abbrev_info
**abbrevs
;
808 bfd_byte
*abbrev_ptr
;
809 struct abbrev_info
*cur_abbrev
;
810 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
811 unsigned int abbrev_form
, hash_number
;
814 if (! read_section (abfd
, &stash
->debug_sections
[debug_abbrev
],
816 &stash
->dwarf_abbrev_buffer
, &stash
->dwarf_abbrev_size
))
819 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
820 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
824 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
825 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
826 abbrev_ptr
+= bytes_read
;
828 /* Loop until we reach an abbrev number of 0. */
829 while (abbrev_number
)
831 amt
= sizeof (struct abbrev_info
);
832 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
833 if (cur_abbrev
== NULL
)
836 /* Read in abbrev header. */
837 cur_abbrev
->number
= abbrev_number
;
838 cur_abbrev
->tag
= (enum dwarf_tag
)
839 read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
840 abbrev_ptr
+= bytes_read
;
841 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
);
844 /* Now read in declarations. */
845 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
846 abbrev_ptr
+= bytes_read
;
847 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
848 abbrev_ptr
+= bytes_read
;
852 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
854 struct attr_abbrev
*tmp
;
856 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
857 amt
*= sizeof (struct attr_abbrev
);
858 tmp
= (struct attr_abbrev
*) bfd_realloc (cur_abbrev
->attrs
, amt
);
863 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
865 struct abbrev_info
*abbrev
= abbrevs
[i
];
869 free (abbrev
->attrs
);
870 abbrev
= abbrev
->next
;
875 cur_abbrev
->attrs
= tmp
;
878 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
879 = (enum dwarf_attribute
) abbrev_name
;
880 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
++].form
881 = (enum dwarf_form
) abbrev_form
;
882 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
883 abbrev_ptr
+= bytes_read
;
884 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
885 abbrev_ptr
+= bytes_read
;
888 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
889 cur_abbrev
->next
= abbrevs
[hash_number
];
890 abbrevs
[hash_number
] = cur_abbrev
;
892 /* Get next abbreviation.
893 Under Irix6 the abbreviations for a compilation unit are not
894 always properly terminated with an abbrev number of 0.
895 Exit loop if we encounter an abbreviation which we have
896 already read (which means we are about to read the abbreviations
897 for the next compile unit) or if the end of the abbreviation
899 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
900 >= stash
->dwarf_abbrev_size
)
902 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
903 abbrev_ptr
+= bytes_read
;
904 if (lookup_abbrev (abbrev_number
,abbrevs
) != NULL
)
911 /* Read an attribute value described by an attribute form. */
914 read_attribute_value (struct attribute
*attr
,
916 struct comp_unit
*unit
,
919 bfd
*abfd
= unit
->abfd
;
920 unsigned int bytes_read
;
921 struct dwarf_block
*blk
;
924 attr
->form
= (enum dwarf_form
) form
;
928 case DW_FORM_ref_addr
:
929 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
931 if (unit
->version
== 3 || unit
->version
== 4)
933 if (unit
->offset_size
== 4)
934 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
);
936 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
);
937 info_ptr
+= unit
->offset_size
;
942 attr
->u
.val
= read_address (unit
, info_ptr
);
943 info_ptr
+= unit
->addr_size
;
945 case DW_FORM_GNU_ref_alt
:
946 case DW_FORM_sec_offset
:
947 if (unit
->offset_size
== 4)
948 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
);
950 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
);
951 info_ptr
+= unit
->offset_size
;
954 amt
= sizeof (struct dwarf_block
);
955 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
958 blk
->size
= read_2_bytes (abfd
, info_ptr
);
960 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
961 info_ptr
+= blk
->size
;
965 amt
= sizeof (struct dwarf_block
);
966 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
969 blk
->size
= read_4_bytes (abfd
, info_ptr
);
971 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
972 info_ptr
+= blk
->size
;
976 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
);
980 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
);
984 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
988 attr
->u
.str
= read_string (abfd
, info_ptr
, &bytes_read
);
989 info_ptr
+= bytes_read
;
992 attr
->u
.str
= read_indirect_string (unit
, info_ptr
, &bytes_read
);
993 info_ptr
+= bytes_read
;
995 case DW_FORM_GNU_strp_alt
:
996 attr
->u
.str
= read_alt_indirect_string (unit
, info_ptr
, &bytes_read
);
997 info_ptr
+= bytes_read
;
999 case DW_FORM_exprloc
:
1001 amt
= sizeof (struct dwarf_block
);
1002 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1005 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1006 info_ptr
+= bytes_read
;
1007 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
1008 info_ptr
+= blk
->size
;
1011 case DW_FORM_block1
:
1012 amt
= sizeof (struct dwarf_block
);
1013 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1016 blk
->size
= read_1_byte (abfd
, info_ptr
);
1018 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
1019 info_ptr
+= blk
->size
;
1023 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
1027 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
1030 case DW_FORM_flag_present
:
1034 attr
->u
.sval
= read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
1035 info_ptr
+= bytes_read
;
1038 attr
->u
.val
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1039 info_ptr
+= bytes_read
;
1042 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
1046 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
);
1050 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
);
1054 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
1057 case DW_FORM_ref_sig8
:
1058 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
1061 case DW_FORM_ref_udata
:
1062 attr
->u
.val
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1063 info_ptr
+= bytes_read
;
1065 case DW_FORM_indirect
:
1066 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1067 info_ptr
+= bytes_read
;
1068 info_ptr
= read_attribute_value (attr
, form
, unit
, info_ptr
);
1071 (*_bfd_error_handler
) (_("Dwarf Error: Invalid or unhandled FORM value: %#x."),
1073 bfd_set_error (bfd_error_bad_value
);
1079 /* Read an attribute described by an abbreviated attribute. */
1082 read_attribute (struct attribute
*attr
,
1083 struct attr_abbrev
*abbrev
,
1084 struct comp_unit
*unit
,
1087 attr
->name
= abbrev
->name
;
1088 info_ptr
= read_attribute_value (attr
, abbrev
->form
, unit
, info_ptr
);
1092 /* Source line information table routines. */
1094 #define FILE_ALLOC_CHUNK 5
1095 #define DIR_ALLOC_CHUNK 5
1099 struct line_info
* prev_line
;
1103 unsigned int column
;
1104 unsigned int discriminator
;
1105 unsigned char op_index
;
1106 unsigned char end_sequence
; /* End of (sequential) code sequence. */
1117 struct line_sequence
1120 struct line_sequence
* prev_sequence
;
1121 struct line_info
* last_line
; /* Largest VMA. */
1124 struct line_info_table
1127 unsigned int num_files
;
1128 unsigned int num_dirs
;
1129 unsigned int num_sequences
;
1132 struct fileinfo
* files
;
1133 struct line_sequence
* sequences
;
1134 struct line_info
* lcl_head
; /* Local head; used in 'add_line_info'. */
1137 /* Remember some information about each function. If the function is
1138 inlined (DW_TAG_inlined_subroutine) it may have two additional
1139 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1140 source code location where this function was inlined. */
1144 /* Pointer to previous function in list of all functions. */
1145 struct funcinfo
*prev_func
;
1146 /* Pointer to function one scope higher. */
1147 struct funcinfo
*caller_func
;
1148 /* Source location file name where caller_func inlines this func. */
1150 /* Source location line number where caller_func inlines this func. */
1152 /* Source location file name. */
1154 /* Source location line number. */
1158 struct arange arange
;
1159 /* Where the symbol is defined. */
1165 /* Pointer to previous variable in list of all variables */
1166 struct varinfo
*prev_var
;
1167 /* Source location file name */
1169 /* Source location line number */
1174 /* Where the symbol is defined */
1176 /* Is this a stack variable? */
1177 unsigned int stack
: 1;
1180 /* Return TRUE if NEW_LINE should sort after LINE. */
1182 static inline bfd_boolean
1183 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
1185 return (new_line
->address
> line
->address
1186 || (new_line
->address
== line
->address
1187 && (new_line
->op_index
> line
->op_index
1188 || (new_line
->op_index
== line
->op_index
1189 && new_line
->end_sequence
< line
->end_sequence
))));
1193 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1194 that the list is sorted. Note that the line_info list is sorted from
1195 highest to lowest VMA (with possible duplicates); that is,
1196 line_info->prev_line always accesses an equal or smaller VMA. */
1199 add_line_info (struct line_info_table
*table
,
1201 unsigned char op_index
,
1204 unsigned int column
,
1205 unsigned int discriminator
,
1208 bfd_size_type amt
= sizeof (struct line_info
);
1209 struct line_sequence
* seq
= table
->sequences
;
1210 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
1215 /* Set member data of 'info'. */
1216 info
->prev_line
= NULL
;
1217 info
->address
= address
;
1218 info
->op_index
= op_index
;
1220 info
->column
= column
;
1221 info
->discriminator
= discriminator
;
1222 info
->end_sequence
= end_sequence
;
1224 if (filename
&& filename
[0])
1226 info
->filename
= (char *) bfd_alloc (table
->abfd
, strlen (filename
) + 1);
1227 if (info
->filename
== NULL
)
1229 strcpy (info
->filename
, filename
);
1232 info
->filename
= NULL
;
1234 /* Find the correct location for 'info'. Normally we will receive
1235 new line_info data 1) in order and 2) with increasing VMAs.
1236 However some compilers break the rules (cf. decode_line_info) and
1237 so we include some heuristics for quickly finding the correct
1238 location for 'info'. In particular, these heuristics optimize for
1239 the common case in which the VMA sequence that we receive is a
1240 list of locally sorted VMAs such as
1241 p...z a...j (where a < j < p < z)
1243 Note: table->lcl_head is used to head an *actual* or *possible*
1244 sub-sequence within the list (such as a...j) that is not directly
1245 headed by table->last_line
1247 Note: we may receive duplicate entries from 'decode_line_info'. */
1250 && seq
->last_line
->address
== address
1251 && seq
->last_line
->op_index
== op_index
1252 && seq
->last_line
->end_sequence
== end_sequence
)
1254 /* We only keep the last entry with the same address and end
1255 sequence. See PR ld/4986. */
1256 if (table
->lcl_head
== seq
->last_line
)
1257 table
->lcl_head
= info
;
1258 info
->prev_line
= seq
->last_line
->prev_line
;
1259 seq
->last_line
= info
;
1261 else if (!seq
|| seq
->last_line
->end_sequence
)
1263 /* Start a new line sequence. */
1264 amt
= sizeof (struct line_sequence
);
1265 seq
= (struct line_sequence
*) bfd_malloc (amt
);
1268 seq
->low_pc
= address
;
1269 seq
->prev_sequence
= table
->sequences
;
1270 seq
->last_line
= info
;
1271 table
->lcl_head
= info
;
1272 table
->sequences
= seq
;
1273 table
->num_sequences
++;
1275 else if (new_line_sorts_after (info
, seq
->last_line
))
1277 /* Normal case: add 'info' to the beginning of the current sequence. */
1278 info
->prev_line
= seq
->last_line
;
1279 seq
->last_line
= info
;
1281 /* lcl_head: initialize to head a *possible* sequence at the end. */
1282 if (!table
->lcl_head
)
1283 table
->lcl_head
= info
;
1285 else if (!new_line_sorts_after (info
, table
->lcl_head
)
1286 && (!table
->lcl_head
->prev_line
1287 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
1289 /* Abnormal but easy: lcl_head is the head of 'info'. */
1290 info
->prev_line
= table
->lcl_head
->prev_line
;
1291 table
->lcl_head
->prev_line
= info
;
1295 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1296 are valid heads for 'info'. Reset 'lcl_head'. */
1297 struct line_info
* li2
= seq
->last_line
; /* Always non-NULL. */
1298 struct line_info
* li1
= li2
->prev_line
;
1302 if (!new_line_sorts_after (info
, li2
)
1303 && new_line_sorts_after (info
, li1
))
1306 li2
= li1
; /* always non-NULL */
1307 li1
= li1
->prev_line
;
1309 table
->lcl_head
= li2
;
1310 info
->prev_line
= table
->lcl_head
->prev_line
;
1311 table
->lcl_head
->prev_line
= info
;
1312 if (address
< seq
->low_pc
)
1313 seq
->low_pc
= address
;
1318 /* Extract a fully qualified filename from a line info table.
1319 The returned string has been malloc'ed and it is the caller's
1320 responsibility to free it. */
1323 concat_filename (struct line_info_table
*table
, unsigned int file
)
1327 if (file
- 1 >= table
->num_files
)
1329 /* FILE == 0 means unknown. */
1331 (*_bfd_error_handler
)
1332 (_("Dwarf Error: mangled line number section (bad file number)."));
1333 return strdup ("<unknown>");
1336 filename
= table
->files
[file
- 1].name
;
1338 if (!IS_ABSOLUTE_PATH (filename
))
1340 char *dir_name
= NULL
;
1341 char *subdir_name
= NULL
;
1345 if (table
->files
[file
- 1].dir
)
1346 subdir_name
= table
->dirs
[table
->files
[file
- 1].dir
- 1];
1348 if (!subdir_name
|| !IS_ABSOLUTE_PATH (subdir_name
))
1349 dir_name
= table
->comp_dir
;
1353 dir_name
= subdir_name
;
1358 return strdup (filename
);
1360 len
= strlen (dir_name
) + strlen (filename
) + 2;
1364 len
+= strlen (subdir_name
) + 1;
1365 name
= (char *) bfd_malloc (len
);
1367 sprintf (name
, "%s/%s/%s", dir_name
, subdir_name
, filename
);
1371 name
= (char *) bfd_malloc (len
);
1373 sprintf (name
, "%s/%s", dir_name
, filename
);
1379 return strdup (filename
);
1383 arange_add (const struct comp_unit
*unit
, struct arange
*first_arange
,
1384 bfd_vma low_pc
, bfd_vma high_pc
)
1386 struct arange
*arange
;
1388 /* Ignore empty ranges. */
1389 if (low_pc
== high_pc
)
1392 /* If the first arange is empty, use it. */
1393 if (first_arange
->high
== 0)
1395 first_arange
->low
= low_pc
;
1396 first_arange
->high
= high_pc
;
1400 /* Next see if we can cheaply extend an existing range. */
1401 arange
= first_arange
;
1404 if (low_pc
== arange
->high
)
1406 arange
->high
= high_pc
;
1409 if (high_pc
== arange
->low
)
1411 arange
->low
= low_pc
;
1414 arange
= arange
->next
;
1418 /* Need to allocate a new arange and insert it into the arange list.
1419 Order isn't significant, so just insert after the first arange. */
1420 arange
= (struct arange
*) bfd_alloc (unit
->abfd
, sizeof (*arange
));
1423 arange
->low
= low_pc
;
1424 arange
->high
= high_pc
;
1425 arange
->next
= first_arange
->next
;
1426 first_arange
->next
= arange
;
1430 /* Compare function for line sequences. */
1433 compare_sequences (const void* a
, const void* b
)
1435 const struct line_sequence
* seq1
= a
;
1436 const struct line_sequence
* seq2
= b
;
1438 /* Sort by low_pc as the primary key. */
1439 if (seq1
->low_pc
< seq2
->low_pc
)
1441 if (seq1
->low_pc
> seq2
->low_pc
)
1444 /* If low_pc values are equal, sort in reverse order of
1445 high_pc, so that the largest region comes first. */
1446 if (seq1
->last_line
->address
< seq2
->last_line
->address
)
1448 if (seq1
->last_line
->address
> seq2
->last_line
->address
)
1451 if (seq1
->last_line
->op_index
< seq2
->last_line
->op_index
)
1453 if (seq1
->last_line
->op_index
> seq2
->last_line
->op_index
)
1459 /* Sort the line sequences for quick lookup. */
1462 sort_line_sequences (struct line_info_table
* table
)
1465 struct line_sequence
* sequences
;
1466 struct line_sequence
* seq
;
1468 unsigned int num_sequences
= table
->num_sequences
;
1469 bfd_vma last_high_pc
;
1471 if (num_sequences
== 0)
1474 /* Allocate space for an array of sequences. */
1475 amt
= sizeof (struct line_sequence
) * num_sequences
;
1476 sequences
= (struct line_sequence
*) bfd_alloc (table
->abfd
, amt
);
1477 if (sequences
== NULL
)
1480 /* Copy the linked list into the array, freeing the original nodes. */
1481 seq
= table
->sequences
;
1482 for (n
= 0; n
< num_sequences
; n
++)
1484 struct line_sequence
* last_seq
= seq
;
1487 sequences
[n
].low_pc
= seq
->low_pc
;
1488 sequences
[n
].prev_sequence
= NULL
;
1489 sequences
[n
].last_line
= seq
->last_line
;
1490 seq
= seq
->prev_sequence
;
1493 BFD_ASSERT (seq
== NULL
);
1495 qsort (sequences
, n
, sizeof (struct line_sequence
), compare_sequences
);
1497 /* Make the list binary-searchable by trimming overlapping entries
1498 and removing nested entries. */
1500 last_high_pc
= sequences
[0].last_line
->address
;
1501 for (n
= 1; n
< table
->num_sequences
; n
++)
1503 if (sequences
[n
].low_pc
< last_high_pc
)
1505 if (sequences
[n
].last_line
->address
<= last_high_pc
)
1506 /* Skip nested entries. */
1509 /* Trim overlapping entries. */
1510 sequences
[n
].low_pc
= last_high_pc
;
1512 last_high_pc
= sequences
[n
].last_line
->address
;
1513 if (n
> num_sequences
)
1515 /* Close up the gap. */
1516 sequences
[num_sequences
].low_pc
= sequences
[n
].low_pc
;
1517 sequences
[num_sequences
].last_line
= sequences
[n
].last_line
;
1522 table
->sequences
= sequences
;
1523 table
->num_sequences
= num_sequences
;
1527 /* Decode the line number information for UNIT. */
1529 static struct line_info_table
*
1530 decode_line_info (struct comp_unit
*unit
, struct dwarf2_debug
*stash
)
1532 bfd
*abfd
= unit
->abfd
;
1533 struct line_info_table
* table
;
1536 struct line_head lh
;
1537 unsigned int i
, bytes_read
, offset_size
;
1538 char *cur_file
, *cur_dir
;
1539 unsigned char op_code
, extended_op
, adj_opcode
;
1540 unsigned int exop_len
;
1543 if (! read_section (abfd
, &stash
->debug_sections
[debug_line
],
1544 stash
->syms
, unit
->line_offset
,
1545 &stash
->dwarf_line_buffer
, &stash
->dwarf_line_size
))
1548 amt
= sizeof (struct line_info_table
);
1549 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
1553 table
->comp_dir
= unit
->comp_dir
;
1555 table
->num_files
= 0;
1556 table
->files
= NULL
;
1558 table
->num_dirs
= 0;
1561 table
->num_sequences
= 0;
1562 table
->sequences
= NULL
;
1564 table
->lcl_head
= NULL
;
1566 line_ptr
= stash
->dwarf_line_buffer
+ unit
->line_offset
;
1568 /* Read in the prologue. */
1569 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
1572 if (lh
.total_length
== 0xffffffff)
1574 lh
.total_length
= read_8_bytes (abfd
, line_ptr
);
1578 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
1580 /* Handle (non-standard) 64-bit DWARF2 formats. */
1581 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
1585 line_end
= line_ptr
+ lh
.total_length
;
1586 lh
.version
= read_2_bytes (abfd
, line_ptr
);
1587 if (lh
.version
< 2 || lh
.version
> 4)
1589 (*_bfd_error_handler
)
1590 (_("Dwarf Error: Unhandled .debug_line version %d."), lh
.version
);
1591 bfd_set_error (bfd_error_bad_value
);
1595 if (offset_size
== 4)
1596 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
);
1598 lh
.prologue_length
= read_8_bytes (abfd
, line_ptr
);
1599 line_ptr
+= offset_size
;
1600 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
);
1602 if (lh
.version
>= 4)
1604 lh
.maximum_ops_per_insn
= read_1_byte (abfd
, line_ptr
);
1608 lh
.maximum_ops_per_insn
= 1;
1609 if (lh
.maximum_ops_per_insn
== 0)
1611 (*_bfd_error_handler
)
1612 (_("Dwarf Error: Invalid maximum operations per instruction."));
1613 bfd_set_error (bfd_error_bad_value
);
1616 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
);
1618 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
);
1620 lh
.line_range
= read_1_byte (abfd
, line_ptr
);
1622 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
);
1624 amt
= lh
.opcode_base
* sizeof (unsigned char);
1625 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
1627 lh
.standard_opcode_lengths
[0] = 1;
1629 for (i
= 1; i
< lh
.opcode_base
; ++i
)
1631 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
);
1635 /* Read directory table. */
1636 while ((cur_dir
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1638 line_ptr
+= bytes_read
;
1640 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1644 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1645 amt
*= sizeof (char *);
1647 tmp
= (char **) bfd_realloc (table
->dirs
, amt
);
1653 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1656 line_ptr
+= bytes_read
;
1658 /* Read file name table. */
1659 while ((cur_file
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1661 line_ptr
+= bytes_read
;
1663 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1665 struct fileinfo
*tmp
;
1667 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1668 amt
*= sizeof (struct fileinfo
);
1670 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1676 table
->files
[table
->num_files
].name
= cur_file
;
1677 table
->files
[table
->num_files
].dir
=
1678 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1679 line_ptr
+= bytes_read
;
1680 table
->files
[table
->num_files
].time
=
1681 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1682 line_ptr
+= bytes_read
;
1683 table
->files
[table
->num_files
].size
=
1684 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1685 line_ptr
+= bytes_read
;
1689 line_ptr
+= bytes_read
;
1691 /* Read the statement sequences until there's nothing left. */
1692 while (line_ptr
< line_end
)
1694 /* State machine registers. */
1695 bfd_vma address
= 0;
1696 unsigned char op_index
= 0;
1697 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
1698 unsigned int line
= 1;
1699 unsigned int column
= 0;
1700 unsigned int discriminator
= 0;
1701 int is_stmt
= lh
.default_is_stmt
;
1702 int end_sequence
= 0;
1703 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1704 compilers generate address sequences that are wildly out of
1705 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1706 for ia64-Linux). Thus, to determine the low and high
1707 address, we must compare on every DW_LNS_copy, etc. */
1708 bfd_vma low_pc
= (bfd_vma
) -1;
1709 bfd_vma high_pc
= 0;
1711 /* Decode the table. */
1712 while (! end_sequence
)
1714 op_code
= read_1_byte (abfd
, line_ptr
);
1717 if (op_code
>= lh
.opcode_base
)
1719 /* Special operand. */
1720 adj_opcode
= op_code
- lh
.opcode_base
;
1721 if (lh
.maximum_ops_per_insn
== 1)
1722 address
+= (adj_opcode
/ lh
.line_range
1723 * lh
.minimum_instruction_length
);
1726 address
+= ((op_index
+ adj_opcode
/ lh
.line_range
)
1727 / lh
.maximum_ops_per_insn
1728 * lh
.minimum_instruction_length
);
1729 op_index
= ((op_index
+ adj_opcode
/ lh
.line_range
)
1730 % lh
.maximum_ops_per_insn
);
1732 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
1733 /* Append row to matrix using current values. */
1734 if (!add_line_info (table
, address
, op_index
, filename
,
1735 line
, column
, discriminator
, 0))
1738 if (address
< low_pc
)
1740 if (address
> high_pc
)
1743 else switch (op_code
)
1745 case DW_LNS_extended_op
:
1746 exop_len
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1747 line_ptr
+= bytes_read
;
1748 extended_op
= read_1_byte (abfd
, line_ptr
);
1751 switch (extended_op
)
1753 case DW_LNE_end_sequence
:
1755 if (!add_line_info (table
, address
, op_index
, filename
, line
,
1756 column
, discriminator
, end_sequence
))
1759 if (address
< low_pc
)
1761 if (address
> high_pc
)
1763 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
1766 case DW_LNE_set_address
:
1767 address
= read_address (unit
, line_ptr
);
1769 line_ptr
+= unit
->addr_size
;
1771 case DW_LNE_define_file
:
1772 cur_file
= read_string (abfd
, line_ptr
, &bytes_read
);
1773 line_ptr
+= bytes_read
;
1774 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1776 struct fileinfo
*tmp
;
1778 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1779 amt
*= sizeof (struct fileinfo
);
1780 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1785 table
->files
[table
->num_files
].name
= cur_file
;
1786 table
->files
[table
->num_files
].dir
=
1787 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1788 line_ptr
+= bytes_read
;
1789 table
->files
[table
->num_files
].time
=
1790 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1791 line_ptr
+= bytes_read
;
1792 table
->files
[table
->num_files
].size
=
1793 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1794 line_ptr
+= bytes_read
;
1797 case DW_LNE_set_discriminator
:
1799 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1800 line_ptr
+= bytes_read
;
1802 case DW_LNE_HP_source_file_correlation
:
1803 line_ptr
+= exop_len
- 1;
1806 (*_bfd_error_handler
)
1807 (_("Dwarf Error: mangled line number section."));
1808 bfd_set_error (bfd_error_bad_value
);
1810 if (filename
!= NULL
)
1816 if (!add_line_info (table
, address
, op_index
,
1817 filename
, line
, column
, discriminator
, 0))
1820 if (address
< low_pc
)
1822 if (address
> high_pc
)
1825 case DW_LNS_advance_pc
:
1826 if (lh
.maximum_ops_per_insn
== 1)
1827 address
+= (lh
.minimum_instruction_length
1828 * read_unsigned_leb128 (abfd
, line_ptr
,
1832 bfd_vma adjust
= read_unsigned_leb128 (abfd
, line_ptr
,
1834 address
= ((op_index
+ adjust
) / lh
.maximum_ops_per_insn
1835 * lh
.minimum_instruction_length
);
1836 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
1838 line_ptr
+= bytes_read
;
1840 case DW_LNS_advance_line
:
1841 line
+= read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
1842 line_ptr
+= bytes_read
;
1844 case DW_LNS_set_file
:
1848 /* The file and directory tables are 0
1849 based, the references are 1 based. */
1850 file
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1851 line_ptr
+= bytes_read
;
1854 filename
= concat_filename (table
, file
);
1857 case DW_LNS_set_column
:
1858 column
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1859 line_ptr
+= bytes_read
;
1861 case DW_LNS_negate_stmt
:
1862 is_stmt
= (!is_stmt
);
1864 case DW_LNS_set_basic_block
:
1866 case DW_LNS_const_add_pc
:
1867 if (lh
.maximum_ops_per_insn
== 1)
1868 address
+= (lh
.minimum_instruction_length
1869 * ((255 - lh
.opcode_base
) / lh
.line_range
));
1872 bfd_vma adjust
= ((255 - lh
.opcode_base
) / lh
.line_range
);
1873 address
+= (lh
.minimum_instruction_length
1874 * ((op_index
+ adjust
)
1875 / lh
.maximum_ops_per_insn
));
1876 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
1879 case DW_LNS_fixed_advance_pc
:
1880 address
+= read_2_bytes (abfd
, line_ptr
);
1885 /* Unknown standard opcode, ignore it. */
1886 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
1888 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1889 line_ptr
+= bytes_read
;
1899 if (sort_line_sequences (table
))
1903 if (table
->sequences
!= NULL
)
1904 free (table
->sequences
);
1905 if (table
->files
!= NULL
)
1906 free (table
->files
);
1907 if (table
->dirs
!= NULL
)
1912 /* If ADDR is within TABLE set the output parameters and return the
1913 range of addresses covered by the entry used to fill them out.
1914 Otherwise set * FILENAME_PTR to NULL and return 0.
1915 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
1916 are pointers to the objects to be filled in. */
1919 lookup_address_in_line_info_table (struct line_info_table
*table
,
1921 const char **filename_ptr
,
1922 unsigned int *linenumber_ptr
,
1923 unsigned int *discriminator_ptr
)
1925 struct line_sequence
*seq
= NULL
;
1926 struct line_info
*each_line
;
1929 /* Binary search the array of sequences. */
1931 high
= table
->num_sequences
;
1934 mid
= (low
+ high
) / 2;
1935 seq
= &table
->sequences
[mid
];
1936 if (addr
< seq
->low_pc
)
1938 else if (addr
>= seq
->last_line
->address
)
1944 if (seq
&& addr
>= seq
->low_pc
&& addr
< seq
->last_line
->address
)
1946 /* Note: seq->last_line should be a descendingly sorted list. */
1947 for (each_line
= seq
->last_line
;
1949 each_line
= each_line
->prev_line
)
1950 if (addr
>= each_line
->address
)
1954 && !(each_line
->end_sequence
|| each_line
== seq
->last_line
))
1956 *filename_ptr
= each_line
->filename
;
1957 *linenumber_ptr
= each_line
->line
;
1958 if (discriminator_ptr
)
1959 *discriminator_ptr
= each_line
->discriminator
;
1960 return seq
->last_line
->address
- seq
->low_pc
;
1964 *filename_ptr
= NULL
;
1968 /* Read in the .debug_ranges section for future reference. */
1971 read_debug_ranges (struct comp_unit
*unit
)
1973 struct dwarf2_debug
*stash
= unit
->stash
;
1974 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_ranges
],
1976 &stash
->dwarf_ranges_buffer
, &stash
->dwarf_ranges_size
);
1979 /* Function table functions. */
1981 /* If ADDR is within UNIT's function tables, set FUNCTIONNAME_PTR, and return
1982 TRUE. Note that we need to find the function that has the smallest range
1983 that contains ADDR, to handle inlined functions without depending upon
1984 them being ordered in TABLE by increasing range. */
1987 lookup_address_in_function_table (struct comp_unit
*unit
,
1989 struct funcinfo
**function_ptr
,
1990 const char **functionname_ptr
)
1992 struct funcinfo
* each_func
;
1993 struct funcinfo
* best_fit
= NULL
;
1994 struct arange
*arange
;
1996 for (each_func
= unit
->function_table
;
1998 each_func
= each_func
->prev_func
)
2000 for (arange
= &each_func
->arange
;
2002 arange
= arange
->next
)
2004 if (addr
>= arange
->low
&& addr
< arange
->high
)
2007 || (arange
->high
- arange
->low
2008 < best_fit
->arange
.high
- best_fit
->arange
.low
))
2009 best_fit
= each_func
;
2016 *functionname_ptr
= best_fit
->name
;
2017 *function_ptr
= best_fit
;
2026 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2027 and LINENUMBER_PTR, and return TRUE. */
2030 lookup_symbol_in_function_table (struct comp_unit
*unit
,
2033 const char **filename_ptr
,
2034 unsigned int *linenumber_ptr
)
2036 struct funcinfo
* each_func
;
2037 struct funcinfo
* best_fit
= NULL
;
2038 struct arange
*arange
;
2039 const char *name
= bfd_asymbol_name (sym
);
2040 asection
*sec
= bfd_get_section (sym
);
2042 for (each_func
= unit
->function_table
;
2044 each_func
= each_func
->prev_func
)
2046 for (arange
= &each_func
->arange
;
2048 arange
= arange
->next
)
2050 if ((!each_func
->sec
|| each_func
->sec
== sec
)
2051 && addr
>= arange
->low
2052 && addr
< arange
->high
2054 && strcmp (name
, each_func
->name
) == 0
2056 || (arange
->high
- arange
->low
2057 < best_fit
->arange
.high
- best_fit
->arange
.low
)))
2058 best_fit
= each_func
;
2064 best_fit
->sec
= sec
;
2065 *filename_ptr
= best_fit
->file
;
2066 *linenumber_ptr
= best_fit
->line
;
2073 /* Variable table functions. */
2075 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
2076 LINENUMBER_PTR, and return TRUE. */
2079 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
2082 const char **filename_ptr
,
2083 unsigned int *linenumber_ptr
)
2085 const char *name
= bfd_asymbol_name (sym
);
2086 asection
*sec
= bfd_get_section (sym
);
2087 struct varinfo
* each
;
2089 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
2090 if (each
->stack
== 0
2091 && each
->file
!= NULL
2092 && each
->name
!= NULL
2093 && each
->addr
== addr
2094 && (!each
->sec
|| each
->sec
== sec
)
2095 && strcmp (name
, each
->name
) == 0)
2101 *filename_ptr
= each
->file
;
2102 *linenumber_ptr
= each
->line
;
2110 find_abstract_instance_name (struct comp_unit
*unit
,
2111 struct attribute
*attr_ptr
)
2113 bfd
*abfd
= unit
->abfd
;
2115 unsigned int abbrev_number
, bytes_read
, i
;
2116 struct abbrev_info
*abbrev
;
2117 bfd_uint64_t die_ref
= attr_ptr
->u
.val
;
2118 struct attribute attr
;
2121 /* DW_FORM_ref_addr can reference an entry in a different CU. It
2122 is an offset from the .debug_info section, not the current CU. */
2123 if (attr_ptr
->form
== DW_FORM_ref_addr
)
2125 /* We only support DW_FORM_ref_addr within the same file, so
2126 any relocations should be resolved already. */
2130 info_ptr
= unit
->sec_info_ptr
+ die_ref
;
2132 /* Now find the CU containing this pointer. */
2133 if (info_ptr
>= unit
->info_ptr_unit
&& info_ptr
< unit
->end_ptr
)
2137 /* Check other CUs to see if they contain the abbrev. */
2138 struct comp_unit
* u
;
2140 for (u
= unit
->prev_unit
; u
!= NULL
; u
= u
->prev_unit
)
2141 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
2145 for (u
= unit
->next_unit
; u
!= NULL
; u
= u
->next_unit
)
2146 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
2151 /* else FIXME: What do we do now ? */
2154 else if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
2156 info_ptr
= read_alt_indirect_ref (unit
, die_ref
);
2157 if (info_ptr
== NULL
)
2159 (*_bfd_error_handler
)
2160 (_("Dwarf Error: Unable to read alt ref %u."), die_ref
);
2161 bfd_set_error (bfd_error_bad_value
);
2164 /* FIXME: Do we need to locate the correct CU, in a similar
2165 fashion to the code in the DW_FORM_ref_addr case above ? */
2168 info_ptr
= unit
->info_ptr_unit
+ die_ref
;
2170 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
2171 info_ptr
+= bytes_read
;
2175 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
2178 (*_bfd_error_handler
)
2179 (_("Dwarf Error: Could not find abbrev number %u."), abbrev_number
);
2180 bfd_set_error (bfd_error_bad_value
);
2184 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2186 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
,
2188 if (info_ptr
== NULL
)
2193 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2198 case DW_AT_specification
:
2199 name
= find_abstract_instance_name (unit
, &attr
);
2201 case DW_AT_linkage_name
:
2202 case DW_AT_MIPS_linkage_name
:
2215 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
,
2216 bfd_uint64_t offset
)
2218 bfd_byte
*ranges_ptr
;
2219 bfd_vma base_address
= unit
->base_address
;
2221 if (! unit
->stash
->dwarf_ranges_buffer
)
2223 if (! read_debug_ranges (unit
))
2226 ranges_ptr
= unit
->stash
->dwarf_ranges_buffer
+ offset
;
2233 low_pc
= read_address (unit
, ranges_ptr
);
2234 ranges_ptr
+= unit
->addr_size
;
2235 high_pc
= read_address (unit
, ranges_ptr
);
2236 ranges_ptr
+= unit
->addr_size
;
2238 if (low_pc
== 0 && high_pc
== 0)
2240 if (low_pc
== -1UL && high_pc
!= -1UL)
2241 base_address
= high_pc
;
2244 if (!arange_add (unit
, arange
,
2245 base_address
+ low_pc
, base_address
+ high_pc
))
2252 /* DWARF2 Compilation unit functions. */
2254 /* Scan over each die in a comp. unit looking for functions to add
2255 to the function table and variables to the variable table. */
2258 scan_unit_for_symbols (struct comp_unit
*unit
)
2260 bfd
*abfd
= unit
->abfd
;
2261 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
2262 int nesting_level
= 1;
2263 struct funcinfo
**nested_funcs
;
2264 int nested_funcs_size
;
2266 /* Maintain a stack of in-scope functions and inlined functions, which we
2267 can use to set the caller_func field. */
2268 nested_funcs_size
= 32;
2269 nested_funcs
= (struct funcinfo
**)
2270 bfd_malloc (nested_funcs_size
* sizeof (struct funcinfo
*));
2271 if (nested_funcs
== NULL
)
2273 nested_funcs
[nesting_level
] = 0;
2275 while (nesting_level
)
2277 unsigned int abbrev_number
, bytes_read
, i
;
2278 struct abbrev_info
*abbrev
;
2279 struct attribute attr
;
2280 struct funcinfo
*func
;
2281 struct varinfo
*var
;
2283 bfd_vma high_pc
= 0;
2284 bfd_boolean high_pc_relative
= FALSE
;
2286 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
2287 info_ptr
+= bytes_read
;
2289 if (! abbrev_number
)
2295 abbrev
= lookup_abbrev (abbrev_number
,unit
->abbrevs
);
2298 (*_bfd_error_handler
)
2299 (_("Dwarf Error: Could not find abbrev number %u."),
2301 bfd_set_error (bfd_error_bad_value
);
2306 if (abbrev
->tag
== DW_TAG_subprogram
2307 || abbrev
->tag
== DW_TAG_entry_point
2308 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
2310 bfd_size_type amt
= sizeof (struct funcinfo
);
2311 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
2314 func
->tag
= abbrev
->tag
;
2315 func
->prev_func
= unit
->function_table
;
2316 unit
->function_table
= func
;
2317 BFD_ASSERT (!unit
->cached
);
2319 if (func
->tag
== DW_TAG_inlined_subroutine
)
2320 for (i
= nesting_level
- 1; i
>= 1; i
--)
2321 if (nested_funcs
[i
])
2323 func
->caller_func
= nested_funcs
[i
];
2326 nested_funcs
[nesting_level
] = func
;
2331 if (abbrev
->tag
== DW_TAG_variable
)
2333 bfd_size_type amt
= sizeof (struct varinfo
);
2334 var
= (struct varinfo
*) bfd_zalloc (abfd
, amt
);
2337 var
->tag
= abbrev
->tag
;
2339 var
->prev_var
= unit
->variable_table
;
2340 unit
->variable_table
= var
;
2341 BFD_ASSERT (!unit
->cached
);
2344 /* No inline function in scope at this nesting level. */
2345 nested_funcs
[nesting_level
] = 0;
2348 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2350 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
2351 if (info_ptr
== NULL
)
2358 case DW_AT_call_file
:
2359 func
->caller_file
= concat_filename (unit
->line_table
,
2363 case DW_AT_call_line
:
2364 func
->caller_line
= attr
.u
.val
;
2367 case DW_AT_abstract_origin
:
2368 case DW_AT_specification
:
2369 func
->name
= find_abstract_instance_name (unit
, &attr
);
2373 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2375 if (func
->name
== NULL
)
2376 func
->name
= attr
.u
.str
;
2379 case DW_AT_linkage_name
:
2380 case DW_AT_MIPS_linkage_name
:
2381 func
->name
= attr
.u
.str
;
2385 low_pc
= attr
.u
.val
;
2389 high_pc
= attr
.u
.val
;
2390 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
2394 if (!read_rangelist (unit
, &func
->arange
, attr
.u
.val
))
2398 case DW_AT_decl_file
:
2399 func
->file
= concat_filename (unit
->line_table
,
2403 case DW_AT_decl_line
:
2404 func
->line
= attr
.u
.val
;
2416 var
->name
= attr
.u
.str
;
2419 case DW_AT_decl_file
:
2420 var
->file
= concat_filename (unit
->line_table
,
2424 case DW_AT_decl_line
:
2425 var
->line
= attr
.u
.val
;
2428 case DW_AT_external
:
2429 if (attr
.u
.val
!= 0)
2433 case DW_AT_location
:
2437 case DW_FORM_block1
:
2438 case DW_FORM_block2
:
2439 case DW_FORM_block4
:
2440 case DW_FORM_exprloc
:
2441 if (*attr
.u
.blk
->data
== DW_OP_addr
)
2445 /* Verify that DW_OP_addr is the only opcode in the
2446 location, in which case the block size will be 1
2447 plus the address size. */
2448 /* ??? For TLS variables, gcc can emit
2449 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
2450 which we don't handle here yet. */
2451 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
2452 var
->addr
= bfd_get (unit
->addr_size
* 8,
2454 attr
.u
.blk
->data
+ 1);
2469 if (high_pc_relative
)
2472 if (func
&& high_pc
!= 0)
2474 if (!arange_add (unit
, &func
->arange
, low_pc
, high_pc
))
2478 if (abbrev
->has_children
)
2482 if (nesting_level
>= nested_funcs_size
)
2484 struct funcinfo
**tmp
;
2486 nested_funcs_size
*= 2;
2487 tmp
= (struct funcinfo
**)
2488 bfd_realloc (nested_funcs
,
2489 nested_funcs_size
* sizeof (struct funcinfo
*));
2494 nested_funcs
[nesting_level
] = 0;
2498 free (nested_funcs
);
2502 free (nested_funcs
);
2506 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
2507 includes the compilation unit header that proceeds the DIE's, but
2508 does not include the length field that precedes each compilation
2509 unit header. END_PTR points one past the end of this comp unit.
2510 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
2512 This routine does not read the whole compilation unit; only enough
2513 to get to the line number information for the compilation unit. */
2515 static struct comp_unit
*
2516 parse_comp_unit (struct dwarf2_debug
*stash
,
2517 bfd_vma unit_length
,
2518 bfd_byte
*info_ptr_unit
,
2519 unsigned int offset_size
)
2521 struct comp_unit
* unit
;
2522 unsigned int version
;
2523 bfd_uint64_t abbrev_offset
= 0;
2524 unsigned int addr_size
;
2525 struct abbrev_info
** abbrevs
;
2526 unsigned int abbrev_number
, bytes_read
, i
;
2527 struct abbrev_info
*abbrev
;
2528 struct attribute attr
;
2529 bfd_byte
*info_ptr
= stash
->info_ptr
;
2530 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
2533 bfd_vma high_pc
= 0;
2534 bfd
*abfd
= stash
->bfd_ptr
;
2535 bfd_boolean high_pc_relative
= FALSE
;
2537 version
= read_2_bytes (abfd
, info_ptr
);
2539 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
2540 if (offset_size
== 4)
2541 abbrev_offset
= read_4_bytes (abfd
, info_ptr
);
2543 abbrev_offset
= read_8_bytes (abfd
, info_ptr
);
2544 info_ptr
+= offset_size
;
2545 addr_size
= read_1_byte (abfd
, info_ptr
);
2548 if (version
!= 2 && version
!= 3 && version
!= 4)
2550 (*_bfd_error_handler
)
2551 (_("Dwarf Error: found dwarf version '%u', this reader"
2552 " only handles version 2, 3 and 4 information."), version
);
2553 bfd_set_error (bfd_error_bad_value
);
2557 if (addr_size
> sizeof (bfd_vma
))
2559 (*_bfd_error_handler
)
2560 (_("Dwarf Error: found address size '%u', this reader"
2561 " can not handle sizes greater than '%u'."),
2563 (unsigned int) sizeof (bfd_vma
));
2564 bfd_set_error (bfd_error_bad_value
);
2568 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
2570 (*_bfd_error_handler
)
2571 ("Dwarf Error: found address size '%u', this reader"
2572 " can only handle address sizes '2', '4' and '8'.", addr_size
);
2573 bfd_set_error (bfd_error_bad_value
);
2577 /* Read the abbrevs for this compilation unit into a table. */
2578 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
);
2582 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
2583 info_ptr
+= bytes_read
;
2584 if (! abbrev_number
)
2586 (*_bfd_error_handler
) (_("Dwarf Error: Bad abbrev number: %u."),
2588 bfd_set_error (bfd_error_bad_value
);
2592 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
2595 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
2597 bfd_set_error (bfd_error_bad_value
);
2601 amt
= sizeof (struct comp_unit
);
2602 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
2606 unit
->version
= version
;
2607 unit
->addr_size
= addr_size
;
2608 unit
->offset_size
= offset_size
;
2609 unit
->abbrevs
= abbrevs
;
2610 unit
->end_ptr
= end_ptr
;
2611 unit
->stash
= stash
;
2612 unit
->info_ptr_unit
= info_ptr_unit
;
2613 unit
->sec_info_ptr
= stash
->sec_info_ptr
;
2615 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2617 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
2618 if (info_ptr
== NULL
)
2621 /* Store the data if it is of an attribute we want to keep in a
2622 partial symbol table. */
2625 case DW_AT_stmt_list
:
2627 unit
->line_offset
= attr
.u
.val
;
2631 unit
->name
= attr
.u
.str
;
2635 low_pc
= attr
.u
.val
;
2636 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2637 this is the base address to use when reading location
2638 lists or range lists. */
2639 if (abbrev
->tag
== DW_TAG_compile_unit
)
2640 unit
->base_address
= low_pc
;
2644 high_pc
= attr
.u
.val
;
2645 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
2649 if (!read_rangelist (unit
, &unit
->arange
, attr
.u
.val
))
2653 case DW_AT_comp_dir
:
2655 char *comp_dir
= attr
.u
.str
;
2658 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2659 directory, get rid of it. */
2660 char *cp
= strchr (comp_dir
, ':');
2662 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
2665 unit
->comp_dir
= comp_dir
;
2673 if (high_pc_relative
)
2677 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
2681 unit
->first_child_die_ptr
= info_ptr
;
2685 /* Return TRUE if UNIT may contain the address given by ADDR. When
2686 there are functions written entirely with inline asm statements, the
2687 range info in the compilation unit header may not be correct. We
2688 need to consult the line info table to see if a compilation unit
2689 really contains the given address. */
2692 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
2694 struct arange
*arange
;
2699 arange
= &unit
->arange
;
2702 if (addr
>= arange
->low
&& addr
< arange
->high
)
2704 arange
= arange
->next
;
2711 /* If UNIT contains ADDR, set the output parameters to the values for
2712 the line containing ADDR. The output parameters, FILENAME_PTR,
2713 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2716 Returns the range of addresses covered by the entry that was used
2717 to fill in *LINENUMBER_PTR or 0 if it was not filled in. */
2720 comp_unit_find_nearest_line (struct comp_unit
*unit
,
2722 const char **filename_ptr
,
2723 const char **functionname_ptr
,
2724 unsigned int *linenumber_ptr
,
2725 unsigned int *discriminator_ptr
,
2726 struct dwarf2_debug
*stash
)
2729 struct funcinfo
*function
;
2734 if (! unit
->line_table
)
2736 if (! unit
->stmtlist
)
2742 unit
->line_table
= decode_line_info (unit
, stash
);
2744 if (! unit
->line_table
)
2750 if (unit
->first_child_die_ptr
< unit
->end_ptr
2751 && ! scan_unit_for_symbols (unit
))
2759 func_p
= lookup_address_in_function_table (unit
, addr
,
2760 &function
, functionname_ptr
);
2761 if (func_p
&& (function
->tag
== DW_TAG_inlined_subroutine
))
2762 stash
->inliner_chain
= function
;
2764 return lookup_address_in_line_info_table (unit
->line_table
, addr
,
2770 /* Check to see if line info is already decoded in a comp_unit.
2771 If not, decode it. Returns TRUE if no errors were encountered;
2775 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
,
2776 struct dwarf2_debug
*stash
)
2781 if (! unit
->line_table
)
2783 if (! unit
->stmtlist
)
2789 unit
->line_table
= decode_line_info (unit
, stash
);
2791 if (! unit
->line_table
)
2797 if (unit
->first_child_die_ptr
< unit
->end_ptr
2798 && ! scan_unit_for_symbols (unit
))
2808 /* If UNIT contains SYM at ADDR, set the output parameters to the
2809 values for the line containing SYM. The output parameters,
2810 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2813 Return TRUE if UNIT contains SYM, and no errors were encountered;
2817 comp_unit_find_line (struct comp_unit
*unit
,
2820 const char **filename_ptr
,
2821 unsigned int *linenumber_ptr
,
2822 struct dwarf2_debug
*stash
)
2824 if (!comp_unit_maybe_decode_line_info (unit
, stash
))
2827 if (sym
->flags
& BSF_FUNCTION
)
2828 return lookup_symbol_in_function_table (unit
, sym
, addr
,
2832 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
2837 static struct funcinfo
*
2838 reverse_funcinfo_list (struct funcinfo
*head
)
2840 struct funcinfo
*rhead
;
2841 struct funcinfo
*temp
;
2843 for (rhead
= NULL
; head
; head
= temp
)
2845 temp
= head
->prev_func
;
2846 head
->prev_func
= rhead
;
2852 static struct varinfo
*
2853 reverse_varinfo_list (struct varinfo
*head
)
2855 struct varinfo
*rhead
;
2856 struct varinfo
*temp
;
2858 for (rhead
= NULL
; head
; head
= temp
)
2860 temp
= head
->prev_var
;
2861 head
->prev_var
= rhead
;
2867 /* Extract all interesting funcinfos and varinfos of a compilation
2868 unit into hash tables for faster lookup. Returns TRUE if no
2869 errors were enountered; FALSE otherwise. */
2872 comp_unit_hash_info (struct dwarf2_debug
*stash
,
2873 struct comp_unit
*unit
,
2874 struct info_hash_table
*funcinfo_hash_table
,
2875 struct info_hash_table
*varinfo_hash_table
)
2877 struct funcinfo
* each_func
;
2878 struct varinfo
* each_var
;
2879 bfd_boolean okay
= TRUE
;
2881 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
2883 if (!comp_unit_maybe_decode_line_info (unit
, stash
))
2886 BFD_ASSERT (!unit
->cached
);
2888 /* To preserve the original search order, we went to visit the function
2889 infos in the reversed order of the list. However, making the list
2890 bi-directional use quite a bit of extra memory. So we reverse
2891 the list first, traverse the list in the now reversed order and
2892 finally reverse the list again to get back the original order. */
2893 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
2894 for (each_func
= unit
->function_table
;
2896 each_func
= each_func
->prev_func
)
2898 /* Skip nameless functions. */
2899 if (each_func
->name
)
2900 /* There is no need to copy name string into hash table as
2901 name string is either in the dwarf string buffer or
2902 info in the stash. */
2903 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
2904 (void*) each_func
, FALSE
);
2906 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
2910 /* We do the same for variable infos. */
2911 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
2912 for (each_var
= unit
->variable_table
;
2914 each_var
= each_var
->prev_var
)
2916 /* Skip stack vars and vars with no files or names. */
2917 if (each_var
->stack
== 0
2918 && each_var
->file
!= NULL
2919 && each_var
->name
!= NULL
)
2920 /* There is no need to copy name string into hash table as
2921 name string is either in the dwarf string buffer or
2922 info in the stash. */
2923 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
2924 (void*) each_var
, FALSE
);
2927 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
2928 unit
->cached
= TRUE
;
2932 /* Locate a section in a BFD containing debugging info. The search starts
2933 from the section after AFTER_SEC, or from the first section in the BFD if
2934 AFTER_SEC is NULL. The search works by examining the names of the
2935 sections. There are three permissiable names. The first two are given
2936 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
2937 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
2938 This is a variation on the .debug_info section which has a checksum
2939 describing the contents appended onto the name. This allows the linker to
2940 identify and discard duplicate debugging sections for different
2941 compilation units. */
2942 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2945 find_debug_info (bfd
*abfd
, const struct dwarf_debug_section
*debug_sections
,
2946 asection
*after_sec
)
2951 if (after_sec
== NULL
)
2953 look
= debug_sections
[debug_info
].uncompressed_name
;
2954 msec
= bfd_get_section_by_name (abfd
, look
);
2958 look
= debug_sections
[debug_info
].compressed_name
;
2961 msec
= bfd_get_section_by_name (abfd
, look
);
2966 for (msec
= abfd
->sections
; msec
!= NULL
; msec
= msec
->next
)
2967 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
2973 for (msec
= after_sec
->next
; msec
!= NULL
; msec
= msec
->next
)
2975 look
= debug_sections
[debug_info
].uncompressed_name
;
2976 if (strcmp (msec
->name
, look
) == 0)
2979 look
= debug_sections
[debug_info
].compressed_name
;
2980 if (look
!= NULL
&& strcmp (msec
->name
, look
) == 0)
2983 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
2990 /* Unset vmas for adjusted sections in STASH. */
2993 unset_sections (struct dwarf2_debug
*stash
)
2996 struct adjusted_section
*p
;
2998 i
= stash
->adjusted_section_count
;
2999 p
= stash
->adjusted_sections
;
3000 for (; i
> 0; i
--, p
++)
3001 p
->section
->vma
= 0;
3004 /* Set unique VMAs for loadable and DWARF sections in ABFD and save
3005 VMAs in STASH for unset_sections. */
3008 place_sections (bfd
*abfd
, struct dwarf2_debug
*stash
)
3010 struct adjusted_section
*p
;
3013 if (stash
->adjusted_section_count
!= 0)
3015 i
= stash
->adjusted_section_count
;
3016 p
= stash
->adjusted_sections
;
3017 for (; i
> 0; i
--, p
++)
3018 p
->section
->vma
= p
->adj_vma
;
3023 bfd_vma last_vma
= 0, last_dwarf
= 0;
3025 const char *debug_info_name
;
3027 debug_info_name
= stash
->debug_sections
[debug_info
].uncompressed_name
;
3029 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
3037 /* We need to adjust the VMAs of any .debug_info sections.
3038 Skip compressed ones, since no relocations could target
3039 them - they should not appear in object files anyway. */
3040 if (strcmp (sect
->name
, debug_info_name
) == 0)
3042 else if (CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
))
3047 if (!is_debug_info
&& (sect
->flags
& SEC_LOAD
) == 0)
3050 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
3057 amt
= i
* sizeof (struct adjusted_section
);
3058 p
= (struct adjusted_section
*) bfd_alloc (abfd
, amt
);
3062 stash
->adjusted_sections
= p
;
3063 stash
->adjusted_section_count
= i
;
3065 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
3073 /* We need to adjust the VMAs of any .debug_info sections.
3074 Skip compressed ones, since no relocations could target
3075 them - they should not appear in object files anyway. */
3076 if (strcmp (sect
->name
, debug_info_name
) == 0)
3078 else if (CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
))
3083 if (!is_debug_info
&& (sect
->flags
& SEC_LOAD
) == 0)
3086 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
3093 BFD_ASSERT (sect
->alignment_power
== 0);
3094 sect
->vma
= last_dwarf
;
3097 else if (last_vma
!= 0)
3099 /* Align the new address to the current section
3101 last_vma
= ((last_vma
3102 + ~((bfd_vma
) -1 << sect
->alignment_power
))
3103 & ((bfd_vma
) -1 << sect
->alignment_power
));
3104 sect
->vma
= last_vma
;
3105 last_vma
+= sect
->vma
+ sz
;
3108 last_vma
+= sect
->vma
+ sz
;
3110 p
->adj_vma
= sect
->vma
;
3119 /* Look up a funcinfo by name using the given info hash table. If found,
3120 also update the locations pointed to by filename_ptr and linenumber_ptr.
3122 This function returns TRUE if a funcinfo that matches the given symbol
3123 and address is found with any error; otherwise it returns FALSE. */
3126 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
3129 const char **filename_ptr
,
3130 unsigned int *linenumber_ptr
)
3132 struct funcinfo
* each_func
;
3133 struct funcinfo
* best_fit
= NULL
;
3134 struct info_list_node
*node
;
3135 struct arange
*arange
;
3136 const char *name
= bfd_asymbol_name (sym
);
3137 asection
*sec
= bfd_get_section (sym
);
3139 for (node
= lookup_info_hash_table (hash_table
, name
);
3143 each_func
= (struct funcinfo
*) node
->info
;
3144 for (arange
= &each_func
->arange
;
3146 arange
= arange
->next
)
3148 if ((!each_func
->sec
|| each_func
->sec
== sec
)
3149 && addr
>= arange
->low
3150 && addr
< arange
->high
3152 || (arange
->high
- arange
->low
3153 < best_fit
->arange
.high
- best_fit
->arange
.low
)))
3154 best_fit
= each_func
;
3160 best_fit
->sec
= sec
;
3161 *filename_ptr
= best_fit
->file
;
3162 *linenumber_ptr
= best_fit
->line
;
3169 /* Look up a varinfo by name using the given info hash table. If found,
3170 also update the locations pointed to by filename_ptr and linenumber_ptr.
3172 This function returns TRUE if a varinfo that matches the given symbol
3173 and address is found with any error; otherwise it returns FALSE. */
3176 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
3179 const char **filename_ptr
,
3180 unsigned int *linenumber_ptr
)
3182 const char *name
= bfd_asymbol_name (sym
);
3183 asection
*sec
= bfd_get_section (sym
);
3184 struct varinfo
* each
;
3185 struct info_list_node
*node
;
3187 for (node
= lookup_info_hash_table (hash_table
, name
);
3191 each
= (struct varinfo
*) node
->info
;
3192 if (each
->addr
== addr
3193 && (!each
->sec
|| each
->sec
== sec
))
3196 *filename_ptr
= each
->file
;
3197 *linenumber_ptr
= each
->line
;
3205 /* Update the funcinfo and varinfo info hash tables if they are
3206 not up to date. Returns TRUE if there is no error; otherwise
3207 returns FALSE and disable the info hash tables. */
3210 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
3212 struct comp_unit
*each
;
3214 /* Exit if hash tables are up-to-date. */
3215 if (stash
->all_comp_units
== stash
->hash_units_head
)
3218 if (stash
->hash_units_head
)
3219 each
= stash
->hash_units_head
->prev_unit
;
3221 each
= stash
->last_comp_unit
;
3225 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
3226 stash
->varinfo_hash_table
))
3228 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
3231 each
= each
->prev_unit
;
3234 stash
->hash_units_head
= stash
->all_comp_units
;
3238 /* Check consistency of info hash tables. This is for debugging only. */
3240 static void ATTRIBUTE_UNUSED
3241 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
3243 struct comp_unit
*each_unit
;
3244 struct funcinfo
*each_func
;
3245 struct varinfo
*each_var
;
3246 struct info_list_node
*node
;
3249 for (each_unit
= stash
->all_comp_units
;
3251 each_unit
= each_unit
->next_unit
)
3253 for (each_func
= each_unit
->function_table
;
3255 each_func
= each_func
->prev_func
)
3257 if (!each_func
->name
)
3259 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
3263 while (node
&& !found
)
3265 found
= node
->info
== each_func
;
3271 for (each_var
= each_unit
->variable_table
;
3273 each_var
= each_var
->prev_var
)
3275 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
3277 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
3281 while (node
&& !found
)
3283 found
= node
->info
== each_var
;
3291 /* Check to see if we want to enable the info hash tables, which consume
3292 quite a bit of memory. Currently we only check the number times
3293 bfd_dwarf2_find_line is called. In the future, we may also want to
3294 take the number of symbols into account. */
3297 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
3299 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
3301 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
3304 /* FIXME: Maybe we should check the reduce_memory_overheads
3305 and optimize fields in the bfd_link_info structure ? */
3307 /* Create hash tables. */
3308 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
3309 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
3310 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
3312 /* Turn off info hashes if any allocation above fails. */
3313 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
3316 /* We need a forced update so that the info hash tables will
3317 be created even though there is no compilation unit. That
3318 happens if STASH_INFO_HASH_TRIGGER is 0. */
3319 stash_maybe_update_info_hash_tables (stash
);
3320 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
3323 /* Find the file and line associated with a symbol and address using the
3324 info hash tables of a stash. If there is a match, the function returns
3325 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
3326 otherwise it returns FALSE. */
3329 stash_find_line_fast (struct dwarf2_debug
*stash
,
3332 const char **filename_ptr
,
3333 unsigned int *linenumber_ptr
)
3335 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
3337 if (sym
->flags
& BSF_FUNCTION
)
3338 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
3339 filename_ptr
, linenumber_ptr
);
3340 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
3341 filename_ptr
, linenumber_ptr
);
3344 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
3345 If DEBUG_BFD is not specified, we read debug information from ABFD
3346 or its gnu_debuglink. The results will be stored in PINFO.
3347 The function returns TRUE iff debug information is ready. */
3350 _bfd_dwarf2_slurp_debug_info (bfd
*abfd
, bfd
*debug_bfd
,
3351 const struct dwarf_debug_section
*debug_sections
,
3355 bfd_size_type amt
= sizeof (struct dwarf2_debug
);
3356 bfd_size_type total_size
;
3358 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
3363 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
3366 stash
->debug_sections
= debug_sections
;
3367 stash
->syms
= symbols
;
3371 if (debug_bfd
== NULL
)
3374 msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
3375 if (msec
== NULL
&& abfd
== debug_bfd
)
3377 char * debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
3379 if (debug_filename
== NULL
)
3380 /* No dwarf2 info, and no gnu_debuglink to follow.
3381 Note that at this point the stash has been allocated, but
3382 contains zeros. This lets future calls to this function
3383 fail more quickly. */
3386 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
3387 || ! bfd_check_format (debug_bfd
, bfd_object
)
3388 || (msec
= find_debug_info (debug_bfd
,
3389 debug_sections
, NULL
)) == NULL
)
3392 bfd_close (debug_bfd
);
3393 /* FIXME: Should we report our failure to follow the debuglink ? */
3394 free (debug_filename
);
3397 stash
->close_on_cleanup
= TRUE
;
3399 stash
->bfd_ptr
= debug_bfd
;
3401 /* There can be more than one DWARF2 info section in a BFD these
3402 days. First handle the easy case when there's only one. If
3403 there's more than one, try case two: none of the sections is
3404 compressed. In that case, read them all in and produce one
3405 large stash. We do this in two passes - in the first pass we
3406 just accumulate the section sizes, and in the second pass we
3407 read in the section's contents. (The allows us to avoid
3408 reallocing the data as we add sections to the stash.) If
3409 some or all sections are compressed, then do things the slow
3410 way, with a bunch of reallocs. */
3412 if (! find_debug_info (debug_bfd
, debug_sections
, msec
))
3414 /* Case 1: only one info section. */
3415 total_size
= msec
->size
;
3416 if (! read_section (debug_bfd
, &stash
->debug_sections
[debug_info
],
3418 &stash
->info_ptr_memory
, &total_size
))
3423 /* Case 2: multiple sections. */
3424 for (total_size
= 0;
3426 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
3427 total_size
+= msec
->size
;
3429 stash
->info_ptr_memory
= (bfd_byte
*) bfd_malloc (total_size
);
3430 if (stash
->info_ptr_memory
== NULL
)
3434 for (msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
3436 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
3444 if (!(bfd_simple_get_relocated_section_contents
3445 (debug_bfd
, msec
, stash
->info_ptr_memory
+ total_size
,
3453 stash
->info_ptr
= stash
->info_ptr_memory
;
3454 stash
->info_ptr_end
= stash
->info_ptr
+ total_size
;
3455 stash
->sec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
3456 stash
->sec_info_ptr
= stash
->info_ptr
;
3460 /* Find the source code location of SYMBOL. If SYMBOL is NULL
3461 then find the nearest source code location corresponding to
3462 the address SECTION + OFFSET.
3463 Returns TRUE if the line is found without error and fills in
3464 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
3465 NULL the FUNCTIONNAME_PTR is also filled in.
3466 SYMBOLS contains the symbol table for ABFD.
3467 DEBUG_SECTIONS contains the name of the dwarf debug sections.
3468 ADDR_SIZE is the number of bytes in the initial .debug_info length
3469 field and in the abbreviation offset, or zero to indicate that the
3470 default value should be used. */
3473 find_line (bfd
*abfd
,
3474 const struct dwarf_debug_section
*debug_sections
,
3479 const char **filename_ptr
,
3480 const char **functionname_ptr
,
3481 unsigned int *linenumber_ptr
,
3482 unsigned int *discriminator_ptr
,
3483 unsigned int addr_size
,
3486 /* Read each compilation unit from the section .debug_info, and check
3487 to see if it contains the address we are searching for. If yes,
3488 lookup the address, and return the line number info. If no, go
3489 on to the next compilation unit.
3491 We keep a list of all the previously read compilation units, and
3492 a pointer to the next un-read compilation unit. Check the
3493 previously read units before reading more. */
3494 struct dwarf2_debug
*stash
;
3495 /* What address are we looking for? */
3497 struct comp_unit
* each
;
3498 bfd_boolean found
= FALSE
;
3499 bfd_boolean do_line
;
3501 *filename_ptr
= NULL
;
3502 if (functionname_ptr
!= NULL
)
3503 *functionname_ptr
= NULL
;
3504 *linenumber_ptr
= 0;
3505 if (discriminator_ptr
)
3506 *discriminator_ptr
= 0;
3508 if (! _bfd_dwarf2_slurp_debug_info (abfd
, NULL
,
3509 debug_sections
, symbols
, pinfo
))
3512 stash
= (struct dwarf2_debug
*) *pinfo
;
3514 /* In a relocatable file, 2 functions may have the same address.
3515 We change the section vma so that they won't overlap. */
3516 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
3518 if (! place_sections (abfd
, stash
))
3522 do_line
= (section
== NULL
3524 && functionname_ptr
== NULL
3528 addr
= symbol
->value
;
3529 section
= bfd_get_section (symbol
);
3531 else if (section
!= NULL
3532 && functionname_ptr
!= NULL
3538 if (section
->output_section
)
3539 addr
+= section
->output_section
->vma
+ section
->output_offset
;
3541 addr
+= section
->vma
;
3543 /* A null info_ptr indicates that there is no dwarf2 info
3544 (or that an error occured while setting up the stash). */
3545 if (! stash
->info_ptr
)
3548 stash
->inliner_chain
= NULL
;
3550 /* Check the previously read comp. units first. */
3553 /* The info hash tables use quite a bit of memory. We may not want to
3554 always use them. We use some heuristics to decide if and when to
3556 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
3557 stash_maybe_enable_info_hash_tables (abfd
, stash
);
3559 /* Keep info hash table up to date if they are available. Note that we
3560 may disable the hash tables if there is any error duing update. */
3561 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
3562 stash_maybe_update_info_hash_tables (stash
);
3564 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
3566 found
= stash_find_line_fast (stash
, symbol
, addr
, filename_ptr
,
3573 /* Check the previously read comp. units first. */
3574 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
3575 if ((symbol
->flags
& BSF_FUNCTION
) == 0
3576 || each
->arange
.high
== 0
3577 || comp_unit_contains_address (each
, addr
))
3579 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
3580 linenumber_ptr
, stash
);
3588 bfd_vma min_range
= (bfd_vma
) -1;
3589 const char * local_filename
= NULL
;
3590 const char * local_functionname
= NULL
;
3591 unsigned int local_linenumber
= 0;
3592 unsigned int local_discriminator
= 0;
3594 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
3596 bfd_vma range
= (bfd_vma
) -1;
3598 found
= ((each
->arange
.high
== 0
3599 || comp_unit_contains_address (each
, addr
))
3600 && (range
= comp_unit_find_nearest_line (each
, addr
,
3602 & local_functionname
,
3604 & local_discriminator
,
3608 /* PRs 15935 15994: Bogus debug information may have provided us
3609 with an erroneous match. We attempt to counter this by
3610 selecting the match that has the smallest address range
3611 associated with it. (We are assuming that corrupt debug info
3612 will tend to result in extra large address ranges rather than
3613 extra small ranges).
3615 This does mean that we scan through all of the CUs associated
3616 with the bfd each time this function is called. But this does
3617 have the benefit of producing consistent results every time the
3618 function is called. */
3619 if (range
<= min_range
)
3621 if (filename_ptr
&& local_filename
)
3622 * filename_ptr
= local_filename
;
3623 if (functionname_ptr
&& local_functionname
)
3624 * functionname_ptr
= local_functionname
;
3625 if (discriminator_ptr
&& local_discriminator
)
3626 * discriminator_ptr
= local_discriminator
;
3627 if (local_linenumber
)
3628 * linenumber_ptr
= local_linenumber
;
3634 if (* linenumber_ptr
)
3641 /* The DWARF2 spec says that the initial length field, and the
3642 offset of the abbreviation table, should both be 4-byte values.
3643 However, some compilers do things differently. */
3646 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
3648 /* Read each remaining comp. units checking each as they are read. */
3649 while (stash
->info_ptr
< stash
->info_ptr_end
)
3652 unsigned int offset_size
= addr_size
;
3653 bfd_byte
*info_ptr_unit
= stash
->info_ptr
;
3655 length
= read_4_bytes (stash
->bfd_ptr
, stash
->info_ptr
);
3656 /* A 0xffffff length is the DWARF3 way of indicating
3657 we use 64-bit offsets, instead of 32-bit offsets. */
3658 if (length
== 0xffffffff)
3661 length
= read_8_bytes (stash
->bfd_ptr
, stash
->info_ptr
+ 4);
3662 stash
->info_ptr
+= 12;
3664 /* A zero length is the IRIX way of indicating 64-bit offsets,
3665 mostly because the 64-bit length will generally fit in 32
3666 bits, and the endianness helps. */
3667 else if (length
== 0)
3670 length
= read_4_bytes (stash
->bfd_ptr
, stash
->info_ptr
+ 4);
3671 stash
->info_ptr
+= 8;
3673 /* In the absence of the hints above, we assume 32-bit DWARF2
3674 offsets even for targets with 64-bit addresses, because:
3675 a) most of the time these targets will not have generated
3676 more than 2Gb of debug info and so will not need 64-bit
3679 b) if they do use 64-bit offsets but they are not using
3680 the size hints that are tested for above then they are
3681 not conforming to the DWARF3 standard anyway. */
3682 else if (addr_size
== 8)
3685 stash
->info_ptr
+= 4;
3688 stash
->info_ptr
+= 4;
3692 each
= parse_comp_unit (stash
, length
, info_ptr_unit
,
3695 /* The dwarf information is damaged, don't trust it any
3698 stash
->info_ptr
+= length
;
3700 if (stash
->all_comp_units
)
3701 stash
->all_comp_units
->prev_unit
= each
;
3703 stash
->last_comp_unit
= each
;
3705 each
->next_unit
= stash
->all_comp_units
;
3706 stash
->all_comp_units
= each
;
3708 /* DW_AT_low_pc and DW_AT_high_pc are optional for
3709 compilation units. If we don't have them (i.e.,
3710 unit->high == 0), we need to consult the line info table
3711 to see if a compilation unit contains the given
3714 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
3715 || each
->arange
.high
== 0
3716 || comp_unit_contains_address (each
, addr
))
3717 && comp_unit_find_line (each
, symbol
, addr
,
3722 found
= ((each
->arange
.high
== 0
3723 || comp_unit_contains_address (each
, addr
))
3724 && comp_unit_find_nearest_line (each
, addr
,
3731 if ((bfd_vma
) (stash
->info_ptr
- stash
->sec_info_ptr
)
3732 == stash
->sec
->size
)
3734 stash
->sec
= find_debug_info (stash
->bfd_ptr
, debug_sections
,
3736 stash
->sec_info_ptr
= stash
->info_ptr
;
3745 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
3746 unset_sections (stash
);
3751 /* The DWARF2 version of find_nearest_line.
3752 Return TRUE if the line is found without error. */
3755 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
3756 const struct dwarf_debug_section
*debug_sections
,
3760 const char **filename_ptr
,
3761 const char **functionname_ptr
,
3762 unsigned int *linenumber_ptr
,
3763 unsigned int *discriminator_ptr
,
3764 unsigned int addr_size
,
3767 return find_line (abfd
, debug_sections
, section
, offset
, NULL
, symbols
,
3768 filename_ptr
, functionname_ptr
, linenumber_ptr
,
3769 discriminator_ptr
, addr_size
, pinfo
);
3772 /* The DWARF2 version of find_line.
3773 Return TRUE if the line is found without error. */
3776 _bfd_dwarf2_find_line (bfd
*abfd
,
3779 const char **filename_ptr
,
3780 unsigned int *linenumber_ptr
,
3781 unsigned int *discriminator_ptr
,
3782 unsigned int addr_size
,
3785 return find_line (abfd
, dwarf_debug_sections
, NULL
, 0, symbol
, symbols
,
3786 filename_ptr
, NULL
, linenumber_ptr
, discriminator_ptr
,
3791 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
3792 const char **filename_ptr
,
3793 const char **functionname_ptr
,
3794 unsigned int *linenumber_ptr
,
3797 struct dwarf2_debug
*stash
;
3799 stash
= (struct dwarf2_debug
*) *pinfo
;
3802 struct funcinfo
*func
= stash
->inliner_chain
;
3804 if (func
&& func
->caller_func
)
3806 *filename_ptr
= func
->caller_file
;
3807 *functionname_ptr
= func
->caller_func
->name
;
3808 *linenumber_ptr
= func
->caller_line
;
3809 stash
->inliner_chain
= func
->caller_func
;
3818 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
, void **pinfo
)
3820 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
3821 struct comp_unit
*each
;
3823 if (abfd
== NULL
|| stash
== NULL
)
3826 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
3828 struct abbrev_info
**abbrevs
= each
->abbrevs
;
3829 struct funcinfo
*function_table
= each
->function_table
;
3830 struct varinfo
*variable_table
= each
->variable_table
;
3833 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
3835 struct abbrev_info
*abbrev
= abbrevs
[i
];
3839 free (abbrev
->attrs
);
3840 abbrev
= abbrev
->next
;
3844 if (each
->line_table
)
3846 free (each
->line_table
->dirs
);
3847 free (each
->line_table
->files
);
3850 while (function_table
)
3852 if (function_table
->file
)
3854 free (function_table
->file
);
3855 function_table
->file
= NULL
;
3858 if (function_table
->caller_file
)
3860 free (function_table
->caller_file
);
3861 function_table
->caller_file
= NULL
;
3863 function_table
= function_table
->prev_func
;
3866 while (variable_table
)
3868 if (variable_table
->file
)
3870 free (variable_table
->file
);
3871 variable_table
->file
= NULL
;
3874 variable_table
= variable_table
->prev_var
;
3878 if (stash
->dwarf_abbrev_buffer
)
3879 free (stash
->dwarf_abbrev_buffer
);
3880 if (stash
->dwarf_line_buffer
)
3881 free (stash
->dwarf_line_buffer
);
3882 if (stash
->dwarf_str_buffer
)
3883 free (stash
->dwarf_str_buffer
);
3884 if (stash
->dwarf_ranges_buffer
)
3885 free (stash
->dwarf_ranges_buffer
);
3886 if (stash
->info_ptr_memory
)
3887 free (stash
->info_ptr_memory
);
3888 if (stash
->close_on_cleanup
)
3889 bfd_close (stash
->bfd_ptr
);
3890 if (stash
->alt_dwarf_str_buffer
)
3891 free (stash
->alt_dwarf_str_buffer
);
3892 if (stash
->alt_dwarf_info_buffer
)
3893 free (stash
->alt_dwarf_info_buffer
);
3894 if (stash
->alt_bfd_ptr
)
3895 bfd_close (stash
->alt_bfd_ptr
);