2 Copyright (C) 1994-2017 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 original bfd for which debug was loaded. This is what
104 we use to compare and so check that the cached debug data is still
105 valid - it saves having to possibly dereference the gnu_debuglink each
109 /* Pointer to the bfd, section and address of the beginning of the
110 section. The bfd might be different than expected because of
111 gnu_debuglink sections. */
114 bfd_byte
*sec_info_ptr
;
116 /* Support for alternate debug info sections created by the DWZ utility:
117 This includes a pointer to an alternate bfd which contains *extra*,
118 possibly duplicate debug sections, and pointers to the loaded
119 .debug_str and .debug_info sections from this bfd. */
121 bfd_byte
* alt_dwarf_str_buffer
;
122 bfd_size_type alt_dwarf_str_size
;
123 bfd_byte
* alt_dwarf_info_buffer
;
124 bfd_size_type alt_dwarf_info_size
;
126 /* A pointer to the memory block allocated for info_ptr. Neither
127 info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
128 beginning of the malloc block. */
129 bfd_byte
*info_ptr_memory
;
131 /* Pointer to the symbol table. */
134 /* Pointer to the .debug_abbrev section loaded into memory. */
135 bfd_byte
*dwarf_abbrev_buffer
;
137 /* Length of the loaded .debug_abbrev section. */
138 bfd_size_type dwarf_abbrev_size
;
140 /* Buffer for decode_line_info. */
141 bfd_byte
*dwarf_line_buffer
;
143 /* Length of the loaded .debug_line section. */
144 bfd_size_type dwarf_line_size
;
146 /* Pointer to the .debug_str section loaded into memory. */
147 bfd_byte
*dwarf_str_buffer
;
149 /* Length of the loaded .debug_str section. */
150 bfd_size_type dwarf_str_size
;
152 /* Pointer to the .debug_line_str section loaded into memory. */
153 bfd_byte
*dwarf_line_str_buffer
;
155 /* Length of the loaded .debug_line_str section. */
156 bfd_size_type dwarf_line_str_size
;
158 /* Pointer to the .debug_ranges section loaded into memory. */
159 bfd_byte
*dwarf_ranges_buffer
;
161 /* Length of the loaded .debug_ranges section. */
162 bfd_size_type dwarf_ranges_size
;
164 /* If the most recent call to bfd_find_nearest_line was given an
165 address in an inlined function, preserve a pointer into the
166 calling chain for subsequent calls to bfd_find_inliner_info to
168 struct funcinfo
*inliner_chain
;
170 /* Section VMAs at the time the stash was built. */
173 /* Number of sections whose VMA we must adjust. */
174 int adjusted_section_count
;
176 /* Array of sections with adjusted VMA. */
177 struct adjusted_section
*adjusted_sections
;
179 /* Number of times find_line is called. This is used in
180 the heuristic for enabling the info hash tables. */
183 #define STASH_INFO_HASH_TRIGGER 100
185 /* Hash table mapping symbol names to function infos. */
186 struct info_hash_table
*funcinfo_hash_table
;
188 /* Hash table mapping symbol names to variable infos. */
189 struct info_hash_table
*varinfo_hash_table
;
191 /* Head of comp_unit list in the last hash table update. */
192 struct comp_unit
*hash_units_head
;
194 /* Status of info hash. */
195 int info_hash_status
;
196 #define STASH_INFO_HASH_OFF 0
197 #define STASH_INFO_HASH_ON 1
198 #define STASH_INFO_HASH_DISABLED 2
200 /* True if we opened bfd_ptr. */
201 bfd_boolean close_on_cleanup
;
211 /* A minimal decoding of DWARF2 compilation units. We only decode
212 what's needed to get to the line number information. */
216 /* Chain the previously read compilation units. */
217 struct comp_unit
*next_unit
;
219 /* Likewise, chain the compilation unit read after this one.
220 The comp units are stored in reversed reading order. */
221 struct comp_unit
*prev_unit
;
223 /* Keep the bfd convenient (for memory allocation). */
226 /* The lowest and highest addresses contained in this compilation
227 unit as specified in the compilation unit header. */
228 struct arange arange
;
230 /* The DW_AT_name attribute (for error messages). */
233 /* The abbrev hash table. */
234 struct abbrev_info
**abbrevs
;
236 /* DW_AT_language. */
239 /* Note that an error was found by comp_unit_find_nearest_line. */
242 /* The DW_AT_comp_dir attribute. */
245 /* TRUE if there is a line number table associated with this comp. unit. */
248 /* Pointer to the current comp_unit so that we can find a given entry
250 bfd_byte
*info_ptr_unit
;
252 /* The offset into .debug_line of the line number table. */
253 unsigned long line_offset
;
255 /* Pointer to the first child die for the comp unit. */
256 bfd_byte
*first_child_die_ptr
;
258 /* The end of the comp unit. */
261 /* The decoded line number, NULL if not yet decoded. */
262 struct line_info_table
*line_table
;
264 /* A list of the functions found in this comp. unit. */
265 struct funcinfo
*function_table
;
267 /* A table of function information references searchable by address. */
268 struct lookup_funcinfo
*lookup_funcinfo_table
;
270 /* Number of functions in the function_table and sorted_function_table. */
271 bfd_size_type number_of_functions
;
273 /* A list of the variables found in this comp. unit. */
274 struct varinfo
*variable_table
;
276 /* Pointer to dwarf2_debug structure. */
277 struct dwarf2_debug
*stash
;
279 /* DWARF format version for this unit - from unit header. */
282 /* Address size for this unit - from unit header. */
283 unsigned char addr_size
;
285 /* Offset size for this unit - from unit header. */
286 unsigned char offset_size
;
288 /* Base address for this unit - from DW_AT_low_pc attribute of
289 DW_TAG_compile_unit DIE */
290 bfd_vma base_address
;
292 /* TRUE if symbols are cached in hash table for faster lookup by name. */
296 /* This data structure holds the information of an abbrev. */
299 unsigned int number
; /* Number identifying abbrev. */
300 enum dwarf_tag tag
; /* DWARF tag. */
301 int has_children
; /* Boolean. */
302 unsigned int num_attrs
; /* Number of attributes. */
303 struct attr_abbrev
*attrs
; /* An array of attribute descriptions. */
304 struct abbrev_info
*next
; /* Next in chain. */
309 enum dwarf_attribute name
;
310 enum dwarf_form form
;
311 bfd_vma implicit_const
;
314 /* Map of uncompressed DWARF debug section name to compressed one. It
315 is terminated by NULL uncompressed_name. */
317 const struct dwarf_debug_section dwarf_debug_sections
[] =
319 { ".debug_abbrev", ".zdebug_abbrev" },
320 { ".debug_aranges", ".zdebug_aranges" },
321 { ".debug_frame", ".zdebug_frame" },
322 { ".debug_info", ".zdebug_info" },
323 { ".debug_info", ".zdebug_info" },
324 { ".debug_line", ".zdebug_line" },
325 { ".debug_loc", ".zdebug_loc" },
326 { ".debug_macinfo", ".zdebug_macinfo" },
327 { ".debug_macro", ".zdebug_macro" },
328 { ".debug_pubnames", ".zdebug_pubnames" },
329 { ".debug_pubtypes", ".zdebug_pubtypes" },
330 { ".debug_ranges", ".zdebug_ranges" },
331 { ".debug_static_func", ".zdebug_static_func" },
332 { ".debug_static_vars", ".zdebug_static_vars" },
333 { ".debug_str", ".zdebug_str", },
334 { ".debug_str", ".zdebug_str", },
335 { ".debug_line_str", ".zdebug_line_str", },
336 { ".debug_types", ".zdebug_types" },
337 /* GNU DWARF 1 extensions */
338 { ".debug_sfnames", ".zdebug_sfnames" },
339 { ".debug_srcinfo", ".zebug_srcinfo" },
340 /* SGI/MIPS DWARF 2 extensions */
341 { ".debug_funcnames", ".zdebug_funcnames" },
342 { ".debug_typenames", ".zdebug_typenames" },
343 { ".debug_varnames", ".zdebug_varnames" },
344 { ".debug_weaknames", ".zdebug_weaknames" },
348 /* NB/ Numbers in this enum must match up with indicies
349 into the dwarf_debug_sections[] array above. */
350 enum dwarf_debug_section_enum
379 /* A static assertion. */
380 extern int dwarf_debug_section_assert
[ARRAY_SIZE (dwarf_debug_sections
)
381 == debug_max
+ 1 ? 1 : -1];
383 #ifndef ABBREV_HASH_SIZE
384 #define ABBREV_HASH_SIZE 121
386 #ifndef ATTR_ALLOC_CHUNK
387 #define ATTR_ALLOC_CHUNK 4
390 /* Variable and function hash tables. This is used to speed up look-up
391 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
392 In order to share code between variable and function infos, we use
393 a list of untyped pointer for all variable/function info associated with
394 a symbol. We waste a bit of memory for list with one node but that
395 simplifies the code. */
397 struct info_list_node
399 struct info_list_node
*next
;
403 /* Info hash entry. */
404 struct info_hash_entry
406 struct bfd_hash_entry root
;
407 struct info_list_node
*head
;
410 struct info_hash_table
412 struct bfd_hash_table base
;
415 /* Function to create a new entry in info hash table. */
417 static struct bfd_hash_entry
*
418 info_hash_table_newfunc (struct bfd_hash_entry
*entry
,
419 struct bfd_hash_table
*table
,
422 struct info_hash_entry
*ret
= (struct info_hash_entry
*) entry
;
424 /* Allocate the structure if it has not already been allocated by a
428 ret
= (struct info_hash_entry
*) bfd_hash_allocate (table
,
434 /* Call the allocation method of the base class. */
435 ret
= ((struct info_hash_entry
*)
436 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
438 /* Initialize the local fields here. */
442 return (struct bfd_hash_entry
*) ret
;
445 /* Function to create a new info hash table. It returns a pointer to the
446 newly created table or NULL if there is any error. We need abfd
447 solely for memory allocation. */
449 static struct info_hash_table
*
450 create_info_hash_table (bfd
*abfd
)
452 struct info_hash_table
*hash_table
;
454 hash_table
= ((struct info_hash_table
*)
455 bfd_alloc (abfd
, sizeof (struct info_hash_table
)));
459 if (!bfd_hash_table_init (&hash_table
->base
, info_hash_table_newfunc
,
460 sizeof (struct info_hash_entry
)))
462 bfd_release (abfd
, hash_table
);
469 /* Insert an info entry into an info hash table. We do not check of
470 duplicate entries. Also, the caller need to guarantee that the
471 right type of info in inserted as info is passed as a void* pointer.
472 This function returns true if there is no error. */
475 insert_info_hash_table (struct info_hash_table
*hash_table
,
480 struct info_hash_entry
*entry
;
481 struct info_list_node
*node
;
483 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
,
488 node
= (struct info_list_node
*) bfd_hash_allocate (&hash_table
->base
,
494 node
->next
= entry
->head
;
500 /* Look up an info entry list from an info hash table. Return NULL
503 static struct info_list_node
*
504 lookup_info_hash_table (struct info_hash_table
*hash_table
, const char *key
)
506 struct info_hash_entry
*entry
;
508 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
, key
,
510 return entry
? entry
->head
: NULL
;
513 /* Read a section into its appropriate place in the dwarf2_debug
514 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
515 not NULL, use bfd_simple_get_relocated_section_contents to read the
516 section contents, otherwise use bfd_get_section_contents. Fail if
517 the located section does not contain at least OFFSET bytes. */
520 read_section (bfd
* abfd
,
521 const struct dwarf_debug_section
*sec
,
524 bfd_byte
** section_buffer
,
525 bfd_size_type
* section_size
)
528 const char *section_name
= sec
->uncompressed_name
;
530 /* The section may have already been read. */
531 if (*section_buffer
== NULL
)
533 msec
= bfd_get_section_by_name (abfd
, section_name
);
536 section_name
= sec
->compressed_name
;
537 if (section_name
!= NULL
)
538 msec
= bfd_get_section_by_name (abfd
, section_name
);
542 _bfd_error_handler (_("Dwarf Error: Can't find %s section."),
543 sec
->uncompressed_name
);
544 bfd_set_error (bfd_error_bad_value
);
548 *section_size
= msec
->rawsize
? msec
->rawsize
: msec
->size
;
552 = bfd_simple_get_relocated_section_contents (abfd
, msec
, NULL
, syms
);
553 if (! *section_buffer
)
558 *section_buffer
= (bfd_byte
*) bfd_malloc (*section_size
);
559 if (! *section_buffer
)
561 if (! bfd_get_section_contents (abfd
, msec
, *section_buffer
,
566 /* Paranoia - if we are reading in a string section, make sure that it
567 is NUL terminated. This is to prevent string functions from running
568 off the end of the buffer. Note - knowing the size of the buffer is
569 not enough as some functions, eg strchr, do not have a range limited
572 FIXME: We ought to use a flag in the dwarf_debug_sections[] table to
573 determine the nature of a debug section, rather than checking the
574 section name as we do here. */
575 if (*section_size
> 0
576 && (*section_buffer
)[*section_size
- 1] != 0
577 && (strstr (section_name
, "_str") || strstr (section_name
, "names")))
579 bfd_byte
* new_buffer
= malloc (*section_size
+ 1);
581 _bfd_error_handler (_("warning: dwarf string section '%s' is not NUL terminated"),
583 memcpy (new_buffer
, *section_buffer
, *section_size
);
584 new_buffer
[*section_size
] = 0;
585 free (*section_buffer
);
586 *section_buffer
= new_buffer
;
590 /* It is possible to get a bad value for the offset into the section
591 that the client wants. Validate it here to avoid trouble later. */
592 if (offset
!= 0 && offset
>= *section_size
)
594 /* xgettext: c-format */
595 _bfd_error_handler (_("Dwarf Error: Offset (%llu)"
596 " greater than or equal to %s size (%Lu)."),
597 (long long) offset
, section_name
, *section_size
);
598 bfd_set_error (bfd_error_bad_value
);
605 /* Read dwarf information from a buffer. */
608 read_1_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
, bfd_byte
*end
)
612 return bfd_get_8 (abfd
, buf
);
616 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
, bfd_byte
*end
)
620 return bfd_get_signed_8 (abfd
, buf
);
624 read_2_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
628 return bfd_get_16 (abfd
, buf
);
632 read_4_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
636 return bfd_get_32 (abfd
, buf
);
640 read_8_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
644 return bfd_get_64 (abfd
, buf
);
648 read_n_bytes (bfd
*abfd ATTRIBUTE_UNUSED
,
651 unsigned int size ATTRIBUTE_UNUSED
)
653 if (buf
+ size
> end
)
658 /* Scans a NUL terminated string starting at BUF, returning a pointer to it.
659 Returns the number of characters in the string, *including* the NUL byte,
660 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
661 at or beyond BUF_END will not be read. Returns NULL if there was a
662 problem, or if the string is empty. */
665 read_string (bfd
* abfd ATTRIBUTE_UNUSED
,
668 unsigned int * bytes_read_ptr
)
674 * bytes_read_ptr
= 0;
680 * bytes_read_ptr
= 1;
684 while (buf
< buf_end
)
687 * bytes_read_ptr
= buf
- str
;
691 * bytes_read_ptr
= buf
- str
;
695 /* Reads an offset from BUF and then locates the string at this offset
696 inside the debug string section. Returns a pointer to the string.
697 Returns the number of bytes read from BUF, *not* the length of the string,
698 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
699 at or beyond BUF_END will not be read from BUF. Returns NULL if there was
700 a problem, or if the string is empty. Does not check for NUL termination
704 read_indirect_string (struct comp_unit
* unit
,
707 unsigned int * bytes_read_ptr
)
710 struct dwarf2_debug
*stash
= unit
->stash
;
713 if (buf
+ unit
->offset_size
> buf_end
)
715 * bytes_read_ptr
= 0;
719 if (unit
->offset_size
== 4)
720 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
722 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
724 *bytes_read_ptr
= unit
->offset_size
;
726 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
728 &stash
->dwarf_str_buffer
, &stash
->dwarf_str_size
))
731 if (offset
>= stash
->dwarf_str_size
)
733 str
= (char *) stash
->dwarf_str_buffer
+ offset
;
739 /* Like read_indirect_string but from .debug_line_str section. */
742 read_indirect_line_string (struct comp_unit
* unit
,
745 unsigned int * bytes_read_ptr
)
748 struct dwarf2_debug
*stash
= unit
->stash
;
751 if (buf
+ unit
->offset_size
> buf_end
)
753 * bytes_read_ptr
= 0;
757 if (unit
->offset_size
== 4)
758 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
760 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
762 *bytes_read_ptr
= unit
->offset_size
;
764 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_line_str
],
766 &stash
->dwarf_line_str_buffer
,
767 &stash
->dwarf_line_str_size
))
770 if (offset
>= stash
->dwarf_line_str_size
)
772 str
= (char *) stash
->dwarf_line_str_buffer
+ offset
;
778 /* Like read_indirect_string but uses a .debug_str located in
779 an alternate file pointed to by the .gnu_debugaltlink section.
780 Used to impement DW_FORM_GNU_strp_alt. */
783 read_alt_indirect_string (struct comp_unit
* unit
,
786 unsigned int * bytes_read_ptr
)
789 struct dwarf2_debug
*stash
= unit
->stash
;
792 if (buf
+ unit
->offset_size
> buf_end
)
794 * bytes_read_ptr
= 0;
798 if (unit
->offset_size
== 4)
799 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
801 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
803 *bytes_read_ptr
= unit
->offset_size
;
805 if (stash
->alt_bfd_ptr
== NULL
)
808 char * debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
810 if (debug_filename
== NULL
)
813 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
814 || ! bfd_check_format (debug_bfd
, bfd_object
))
817 bfd_close (debug_bfd
);
819 /* FIXME: Should we report our failure to follow the debuglink ? */
820 free (debug_filename
);
823 stash
->alt_bfd_ptr
= debug_bfd
;
826 if (! read_section (unit
->stash
->alt_bfd_ptr
,
827 stash
->debug_sections
+ debug_str_alt
,
828 NULL
, /* FIXME: Do we need to load alternate symbols ? */
830 &stash
->alt_dwarf_str_buffer
,
831 &stash
->alt_dwarf_str_size
))
834 if (offset
>= stash
->alt_dwarf_str_size
)
836 str
= (char *) stash
->alt_dwarf_str_buffer
+ offset
;
843 /* Resolve an alternate reference from UNIT at OFFSET.
844 Returns a pointer into the loaded alternate CU upon success
845 or NULL upon failure. */
848 read_alt_indirect_ref (struct comp_unit
* unit
,
851 struct dwarf2_debug
*stash
= unit
->stash
;
853 if (stash
->alt_bfd_ptr
== NULL
)
856 char * debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
858 if (debug_filename
== NULL
)
861 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
862 || ! bfd_check_format (debug_bfd
, bfd_object
))
865 bfd_close (debug_bfd
);
867 /* FIXME: Should we report our failure to follow the debuglink ? */
868 free (debug_filename
);
871 stash
->alt_bfd_ptr
= debug_bfd
;
874 if (! read_section (unit
->stash
->alt_bfd_ptr
,
875 stash
->debug_sections
+ debug_info_alt
,
876 NULL
, /* FIXME: Do we need to load alternate symbols ? */
878 &stash
->alt_dwarf_info_buffer
,
879 &stash
->alt_dwarf_info_size
))
882 if (offset
>= stash
->alt_dwarf_info_size
)
884 return stash
->alt_dwarf_info_buffer
+ offset
;
888 read_address (struct comp_unit
*unit
, bfd_byte
*buf
, bfd_byte
* buf_end
)
892 if (bfd_get_flavour (unit
->abfd
) == bfd_target_elf_flavour
)
893 signed_vma
= get_elf_backend_data (unit
->abfd
)->sign_extend_vma
;
895 if (buf
+ unit
->addr_size
> buf_end
)
900 switch (unit
->addr_size
)
903 return bfd_get_signed_64 (unit
->abfd
, buf
);
905 return bfd_get_signed_32 (unit
->abfd
, buf
);
907 return bfd_get_signed_16 (unit
->abfd
, buf
);
914 switch (unit
->addr_size
)
917 return bfd_get_64 (unit
->abfd
, buf
);
919 return bfd_get_32 (unit
->abfd
, buf
);
921 return bfd_get_16 (unit
->abfd
, buf
);
928 /* Lookup an abbrev_info structure in the abbrev hash table. */
930 static struct abbrev_info
*
931 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
933 unsigned int hash_number
;
934 struct abbrev_info
*abbrev
;
936 hash_number
= number
% ABBREV_HASH_SIZE
;
937 abbrev
= abbrevs
[hash_number
];
941 if (abbrev
->number
== number
)
944 abbrev
= abbrev
->next
;
950 /* In DWARF version 2, the description of the debugging information is
951 stored in a separate .debug_abbrev section. Before we read any
952 dies from a section we read in all abbreviations and install them
955 static struct abbrev_info
**
956 read_abbrevs (bfd
*abfd
, bfd_uint64_t offset
, struct dwarf2_debug
*stash
)
958 struct abbrev_info
**abbrevs
;
959 bfd_byte
*abbrev_ptr
;
960 bfd_byte
*abbrev_end
;
961 struct abbrev_info
*cur_abbrev
;
962 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
963 unsigned int abbrev_form
, hash_number
;
966 if (! read_section (abfd
, &stash
->debug_sections
[debug_abbrev
],
968 &stash
->dwarf_abbrev_buffer
, &stash
->dwarf_abbrev_size
))
971 if (offset
>= stash
->dwarf_abbrev_size
)
974 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
975 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
979 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
980 abbrev_end
= stash
->dwarf_abbrev_buffer
+ stash
->dwarf_abbrev_size
;
981 abbrev_number
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
983 abbrev_ptr
+= bytes_read
;
985 /* Loop until we reach an abbrev number of 0. */
986 while (abbrev_number
)
988 amt
= sizeof (struct abbrev_info
);
989 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
990 if (cur_abbrev
== NULL
)
993 /* Read in abbrev header. */
994 cur_abbrev
->number
= abbrev_number
;
995 cur_abbrev
->tag
= (enum dwarf_tag
)
996 _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
998 abbrev_ptr
+= bytes_read
;
999 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
, abbrev_end
);
1002 /* Now read in declarations. */
1005 /* Initialize it just to avoid a GCC false warning. */
1006 bfd_vma implicit_const
= -1;
1008 abbrev_name
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1010 abbrev_ptr
+= bytes_read
;
1011 abbrev_form
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1013 abbrev_ptr
+= bytes_read
;
1014 if (abbrev_form
== DW_FORM_implicit_const
)
1016 implicit_const
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
,
1019 abbrev_ptr
+= bytes_read
;
1022 if (abbrev_name
== 0)
1025 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
1027 struct attr_abbrev
*tmp
;
1029 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
1030 amt
*= sizeof (struct attr_abbrev
);
1031 tmp
= (struct attr_abbrev
*) bfd_realloc (cur_abbrev
->attrs
, amt
);
1036 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1038 struct abbrev_info
*abbrev
= abbrevs
[i
];
1042 free (abbrev
->attrs
);
1043 abbrev
= abbrev
->next
;
1048 cur_abbrev
->attrs
= tmp
;
1051 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
1052 = (enum dwarf_attribute
) abbrev_name
;
1053 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].form
1054 = (enum dwarf_form
) abbrev_form
;
1055 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].implicit_const
1057 ++cur_abbrev
->num_attrs
;
1060 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
1061 cur_abbrev
->next
= abbrevs
[hash_number
];
1062 abbrevs
[hash_number
] = cur_abbrev
;
1064 /* Get next abbreviation.
1065 Under Irix6 the abbreviations for a compilation unit are not
1066 always properly terminated with an abbrev number of 0.
1067 Exit loop if we encounter an abbreviation which we have
1068 already read (which means we are about to read the abbreviations
1069 for the next compile unit) or if the end of the abbreviation
1070 table is reached. */
1071 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
1072 >= stash
->dwarf_abbrev_size
)
1074 abbrev_number
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
,
1075 &bytes_read
, FALSE
, abbrev_end
);
1076 abbrev_ptr
+= bytes_read
;
1077 if (lookup_abbrev (abbrev_number
, abbrevs
) != NULL
)
1084 /* Returns true if the form is one which has a string value. */
1086 static inline bfd_boolean
1087 is_str_attr (enum dwarf_form form
)
1089 return (form
== DW_FORM_string
|| form
== DW_FORM_strp
1090 || form
== DW_FORM_line_strp
|| form
== DW_FORM_GNU_strp_alt
);
1093 /* Read and fill in the value of attribute ATTR as described by FORM.
1094 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1095 Returns an updated INFO_PTR taking into account the amount of data read. */
1098 read_attribute_value (struct attribute
* attr
,
1100 bfd_vma implicit_const
,
1101 struct comp_unit
* unit
,
1102 bfd_byte
* info_ptr
,
1103 bfd_byte
* info_ptr_end
)
1105 bfd
*abfd
= unit
->abfd
;
1106 unsigned int bytes_read
;
1107 struct dwarf_block
*blk
;
1110 if (info_ptr
>= info_ptr_end
&& form
!= DW_FORM_flag_present
)
1112 _bfd_error_handler (_("Dwarf Error: Info pointer extends beyond end of attributes"));
1113 bfd_set_error (bfd_error_bad_value
);
1117 attr
->form
= (enum dwarf_form
) form
;
1121 case DW_FORM_ref_addr
:
1122 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1124 if (unit
->version
== 3 || unit
->version
== 4)
1126 if (unit
->offset_size
== 4)
1127 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1129 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1130 info_ptr
+= unit
->offset_size
;
1135 attr
->u
.val
= read_address (unit
, info_ptr
, info_ptr_end
);
1136 info_ptr
+= unit
->addr_size
;
1138 case DW_FORM_GNU_ref_alt
:
1139 case DW_FORM_sec_offset
:
1140 if (unit
->offset_size
== 4)
1141 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1143 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1144 info_ptr
+= unit
->offset_size
;
1146 case DW_FORM_block2
:
1147 amt
= sizeof (struct dwarf_block
);
1148 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1151 blk
->size
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1153 blk
->data
= read_n_bytes (abfd
, info_ptr
, info_ptr_end
, blk
->size
);
1154 info_ptr
+= blk
->size
;
1157 case DW_FORM_block4
:
1158 amt
= sizeof (struct dwarf_block
);
1159 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1162 blk
->size
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1164 blk
->data
= read_n_bytes (abfd
, info_ptr
, info_ptr_end
, blk
->size
);
1165 info_ptr
+= blk
->size
;
1169 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1173 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1177 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1180 case DW_FORM_string
:
1181 attr
->u
.str
= read_string (abfd
, info_ptr
, info_ptr_end
, &bytes_read
);
1182 info_ptr
+= bytes_read
;
1185 attr
->u
.str
= read_indirect_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1186 info_ptr
+= bytes_read
;
1188 case DW_FORM_line_strp
:
1189 attr
->u
.str
= read_indirect_line_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1190 info_ptr
+= bytes_read
;
1192 case DW_FORM_GNU_strp_alt
:
1193 attr
->u
.str
= read_alt_indirect_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1194 info_ptr
+= bytes_read
;
1196 case DW_FORM_exprloc
:
1198 amt
= sizeof (struct dwarf_block
);
1199 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1202 blk
->size
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1203 FALSE
, info_ptr_end
);
1204 info_ptr
+= bytes_read
;
1205 blk
->data
= read_n_bytes (abfd
, info_ptr
, info_ptr_end
, blk
->size
);
1206 info_ptr
+= blk
->size
;
1209 case DW_FORM_block1
:
1210 amt
= sizeof (struct dwarf_block
);
1211 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1214 blk
->size
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1216 blk
->data
= read_n_bytes (abfd
, info_ptr
, info_ptr_end
, blk
->size
);
1217 info_ptr
+= blk
->size
;
1221 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1225 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1228 case DW_FORM_flag_present
:
1232 attr
->u
.sval
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1233 TRUE
, info_ptr_end
);
1234 info_ptr
+= bytes_read
;
1237 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1238 FALSE
, info_ptr_end
);
1239 info_ptr
+= bytes_read
;
1242 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1246 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1250 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1254 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1257 case DW_FORM_ref_sig8
:
1258 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1261 case DW_FORM_ref_udata
:
1262 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1263 FALSE
, info_ptr_end
);
1264 info_ptr
+= bytes_read
;
1266 case DW_FORM_indirect
:
1267 form
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1268 FALSE
, info_ptr_end
);
1269 info_ptr
+= bytes_read
;
1270 if (form
== DW_FORM_implicit_const
)
1272 implicit_const
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1273 TRUE
, info_ptr_end
);
1274 info_ptr
+= bytes_read
;
1276 info_ptr
= read_attribute_value (attr
, form
, implicit_const
, unit
,
1277 info_ptr
, info_ptr_end
);
1279 case DW_FORM_implicit_const
:
1280 attr
->form
= DW_FORM_sdata
;
1281 attr
->u
.sval
= implicit_const
;
1284 _bfd_error_handler (_("Dwarf Error: Invalid or unhandled FORM value: %#x."),
1286 bfd_set_error (bfd_error_bad_value
);
1292 /* Read an attribute described by an abbreviated attribute. */
1295 read_attribute (struct attribute
* attr
,
1296 struct attr_abbrev
* abbrev
,
1297 struct comp_unit
* unit
,
1298 bfd_byte
* info_ptr
,
1299 bfd_byte
* info_ptr_end
)
1301 attr
->name
= abbrev
->name
;
1302 info_ptr
= read_attribute_value (attr
, abbrev
->form
, abbrev
->implicit_const
,
1303 unit
, info_ptr
, info_ptr_end
);
1307 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1311 non_mangled (int lang
)
1321 case DW_LANG_Cobol74
:
1322 case DW_LANG_Cobol85
:
1323 case DW_LANG_Fortran77
:
1324 case DW_LANG_Pascal83
:
1334 /* Source line information table routines. */
1336 #define FILE_ALLOC_CHUNK 5
1337 #define DIR_ALLOC_CHUNK 5
1341 struct line_info
* prev_line
;
1345 unsigned int column
;
1346 unsigned int discriminator
;
1347 unsigned char op_index
;
1348 unsigned char end_sequence
; /* End of (sequential) code sequence. */
1359 struct line_sequence
1362 struct line_sequence
* prev_sequence
;
1363 struct line_info
* last_line
; /* Largest VMA. */
1364 struct line_info
** line_info_lookup
;
1365 bfd_size_type num_lines
;
1368 struct line_info_table
1371 unsigned int num_files
;
1372 unsigned int num_dirs
;
1373 unsigned int num_sequences
;
1376 struct fileinfo
* files
;
1377 struct line_sequence
* sequences
;
1378 struct line_info
* lcl_head
; /* Local head; used in 'add_line_info'. */
1381 /* Remember some information about each function. If the function is
1382 inlined (DW_TAG_inlined_subroutine) it may have two additional
1383 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1384 source code location where this function was inlined. */
1388 /* Pointer to previous function in list of all functions. */
1389 struct funcinfo
* prev_func
;
1390 /* Pointer to function one scope higher. */
1391 struct funcinfo
* caller_func
;
1392 /* Source location file name where caller_func inlines this func. */
1394 /* Source location file name. */
1396 /* Source location line number where caller_func inlines this func. */
1398 /* Source location line number. */
1401 bfd_boolean is_linkage
;
1403 struct arange arange
;
1404 /* Where the symbol is defined. */
1408 struct lookup_funcinfo
1410 /* Function information corresponding to this lookup table entry. */
1411 struct funcinfo
* funcinfo
;
1413 /* The lowest address for this specific function. */
1416 /* The highest address of this function before the lookup table is sorted.
1417 The highest address of all prior functions after the lookup table is
1418 sorted, which is used for binary search. */
1424 /* Pointer to previous variable in list of all variables */
1425 struct varinfo
*prev_var
;
1426 /* Source location file name */
1428 /* Source location line number */
1433 /* Where the symbol is defined */
1435 /* Is this a stack variable? */
1436 unsigned int stack
: 1;
1439 /* Return TRUE if NEW_LINE should sort after LINE. */
1441 static inline bfd_boolean
1442 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
1444 return (new_line
->address
> line
->address
1445 || (new_line
->address
== line
->address
1446 && (new_line
->op_index
> line
->op_index
1447 || (new_line
->op_index
== line
->op_index
1448 && new_line
->end_sequence
< line
->end_sequence
))));
1452 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1453 that the list is sorted. Note that the line_info list is sorted from
1454 highest to lowest VMA (with possible duplicates); that is,
1455 line_info->prev_line always accesses an equal or smaller VMA. */
1458 add_line_info (struct line_info_table
*table
,
1460 unsigned char op_index
,
1463 unsigned int column
,
1464 unsigned int discriminator
,
1467 bfd_size_type amt
= sizeof (struct line_info
);
1468 struct line_sequence
* seq
= table
->sequences
;
1469 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
1474 /* Set member data of 'info'. */
1475 info
->prev_line
= NULL
;
1476 info
->address
= address
;
1477 info
->op_index
= op_index
;
1479 info
->column
= column
;
1480 info
->discriminator
= discriminator
;
1481 info
->end_sequence
= end_sequence
;
1483 if (filename
&& filename
[0])
1485 info
->filename
= (char *) bfd_alloc (table
->abfd
, strlen (filename
) + 1);
1486 if (info
->filename
== NULL
)
1488 strcpy (info
->filename
, filename
);
1491 info
->filename
= NULL
;
1493 /* Find the correct location for 'info'. Normally we will receive
1494 new line_info data 1) in order and 2) with increasing VMAs.
1495 However some compilers break the rules (cf. decode_line_info) and
1496 so we include some heuristics for quickly finding the correct
1497 location for 'info'. In particular, these heuristics optimize for
1498 the common case in which the VMA sequence that we receive is a
1499 list of locally sorted VMAs such as
1500 p...z a...j (where a < j < p < z)
1502 Note: table->lcl_head is used to head an *actual* or *possible*
1503 sub-sequence within the list (such as a...j) that is not directly
1504 headed by table->last_line
1506 Note: we may receive duplicate entries from 'decode_line_info'. */
1509 && seq
->last_line
->address
== address
1510 && seq
->last_line
->op_index
== op_index
1511 && seq
->last_line
->end_sequence
== end_sequence
)
1513 /* We only keep the last entry with the same address and end
1514 sequence. See PR ld/4986. */
1515 if (table
->lcl_head
== seq
->last_line
)
1516 table
->lcl_head
= info
;
1517 info
->prev_line
= seq
->last_line
->prev_line
;
1518 seq
->last_line
= info
;
1520 else if (!seq
|| seq
->last_line
->end_sequence
)
1522 /* Start a new line sequence. */
1523 amt
= sizeof (struct line_sequence
);
1524 seq
= (struct line_sequence
*) bfd_malloc (amt
);
1527 seq
->low_pc
= address
;
1528 seq
->prev_sequence
= table
->sequences
;
1529 seq
->last_line
= info
;
1530 table
->lcl_head
= info
;
1531 table
->sequences
= seq
;
1532 table
->num_sequences
++;
1534 else if (new_line_sorts_after (info
, seq
->last_line
))
1536 /* Normal case: add 'info' to the beginning of the current sequence. */
1537 info
->prev_line
= seq
->last_line
;
1538 seq
->last_line
= info
;
1540 /* lcl_head: initialize to head a *possible* sequence at the end. */
1541 if (!table
->lcl_head
)
1542 table
->lcl_head
= info
;
1544 else if (!new_line_sorts_after (info
, table
->lcl_head
)
1545 && (!table
->lcl_head
->prev_line
1546 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
1548 /* Abnormal but easy: lcl_head is the head of 'info'. */
1549 info
->prev_line
= table
->lcl_head
->prev_line
;
1550 table
->lcl_head
->prev_line
= info
;
1554 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1555 are valid heads for 'info'. Reset 'lcl_head'. */
1556 struct line_info
* li2
= seq
->last_line
; /* Always non-NULL. */
1557 struct line_info
* li1
= li2
->prev_line
;
1561 if (!new_line_sorts_after (info
, li2
)
1562 && new_line_sorts_after (info
, li1
))
1565 li2
= li1
; /* always non-NULL */
1566 li1
= li1
->prev_line
;
1568 table
->lcl_head
= li2
;
1569 info
->prev_line
= table
->lcl_head
->prev_line
;
1570 table
->lcl_head
->prev_line
= info
;
1571 if (address
< seq
->low_pc
)
1572 seq
->low_pc
= address
;
1577 /* Extract a fully qualified filename from a line info table.
1578 The returned string has been malloc'ed and it is the caller's
1579 responsibility to free it. */
1582 concat_filename (struct line_info_table
*table
, unsigned int file
)
1586 if (file
- 1 >= table
->num_files
)
1588 /* FILE == 0 means unknown. */
1591 (_("Dwarf Error: mangled line number section (bad file number)."));
1592 return strdup ("<unknown>");
1595 filename
= table
->files
[file
- 1].name
;
1596 if (filename
== NULL
)
1597 return strdup ("<unknown>");
1599 if (!IS_ABSOLUTE_PATH (filename
))
1601 char *dir_name
= NULL
;
1602 char *subdir_name
= NULL
;
1606 if (table
->files
[file
- 1].dir
1607 /* PR 17512: file: 0317e960. */
1608 && table
->files
[file
- 1].dir
<= table
->num_dirs
1609 /* PR 17512: file: 7f3d2e4b. */
1610 && table
->dirs
!= NULL
)
1611 subdir_name
= table
->dirs
[table
->files
[file
- 1].dir
- 1];
1613 if (!subdir_name
|| !IS_ABSOLUTE_PATH (subdir_name
))
1614 dir_name
= table
->comp_dir
;
1618 dir_name
= subdir_name
;
1623 return strdup (filename
);
1625 len
= strlen (dir_name
) + strlen (filename
) + 2;
1629 len
+= strlen (subdir_name
) + 1;
1630 name
= (char *) bfd_malloc (len
);
1632 sprintf (name
, "%s/%s/%s", dir_name
, subdir_name
, filename
);
1636 name
= (char *) bfd_malloc (len
);
1638 sprintf (name
, "%s/%s", dir_name
, filename
);
1644 return strdup (filename
);
1648 arange_add (const struct comp_unit
*unit
, struct arange
*first_arange
,
1649 bfd_vma low_pc
, bfd_vma high_pc
)
1651 struct arange
*arange
;
1653 /* Ignore empty ranges. */
1654 if (low_pc
== high_pc
)
1657 /* If the first arange is empty, use it. */
1658 if (first_arange
->high
== 0)
1660 first_arange
->low
= low_pc
;
1661 first_arange
->high
= high_pc
;
1665 /* Next see if we can cheaply extend an existing range. */
1666 arange
= first_arange
;
1669 if (low_pc
== arange
->high
)
1671 arange
->high
= high_pc
;
1674 if (high_pc
== arange
->low
)
1676 arange
->low
= low_pc
;
1679 arange
= arange
->next
;
1683 /* Need to allocate a new arange and insert it into the arange list.
1684 Order isn't significant, so just insert after the first arange. */
1685 arange
= (struct arange
*) bfd_alloc (unit
->abfd
, sizeof (*arange
));
1688 arange
->low
= low_pc
;
1689 arange
->high
= high_pc
;
1690 arange
->next
= first_arange
->next
;
1691 first_arange
->next
= arange
;
1695 /* Compare function for line sequences. */
1698 compare_sequences (const void* a
, const void* b
)
1700 const struct line_sequence
* seq1
= a
;
1701 const struct line_sequence
* seq2
= b
;
1703 /* Sort by low_pc as the primary key. */
1704 if (seq1
->low_pc
< seq2
->low_pc
)
1706 if (seq1
->low_pc
> seq2
->low_pc
)
1709 /* If low_pc values are equal, sort in reverse order of
1710 high_pc, so that the largest region comes first. */
1711 if (seq1
->last_line
->address
< seq2
->last_line
->address
)
1713 if (seq1
->last_line
->address
> seq2
->last_line
->address
)
1716 if (seq1
->last_line
->op_index
< seq2
->last_line
->op_index
)
1718 if (seq1
->last_line
->op_index
> seq2
->last_line
->op_index
)
1724 /* Construct the line information table for quick lookup. */
1727 build_line_info_table (struct line_info_table
* table
,
1728 struct line_sequence
* seq
)
1731 struct line_info
** line_info_lookup
;
1732 struct line_info
* each_line
;
1733 unsigned int num_lines
;
1734 unsigned int line_index
;
1736 if (seq
->line_info_lookup
!= NULL
)
1739 /* Count the number of line information entries. We could do this while
1740 scanning the debug information, but some entries may be added via
1741 lcl_head without having a sequence handy to increment the number of
1744 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
1750 /* Allocate space for the line information lookup table. */
1751 amt
= sizeof (struct line_info
*) * num_lines
;
1752 line_info_lookup
= (struct line_info
**) bfd_alloc (table
->abfd
, amt
);
1753 if (line_info_lookup
== NULL
)
1756 /* Create the line information lookup table. */
1757 line_index
= num_lines
;
1758 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
1759 line_info_lookup
[--line_index
] = each_line
;
1761 BFD_ASSERT (line_index
== 0);
1763 seq
->num_lines
= num_lines
;
1764 seq
->line_info_lookup
= line_info_lookup
;
1769 /* Sort the line sequences for quick lookup. */
1772 sort_line_sequences (struct line_info_table
* table
)
1775 struct line_sequence
* sequences
;
1776 struct line_sequence
* seq
;
1778 unsigned int num_sequences
= table
->num_sequences
;
1779 bfd_vma last_high_pc
;
1781 if (num_sequences
== 0)
1784 /* Allocate space for an array of sequences. */
1785 amt
= sizeof (struct line_sequence
) * num_sequences
;
1786 sequences
= (struct line_sequence
*) bfd_alloc (table
->abfd
, amt
);
1787 if (sequences
== NULL
)
1790 /* Copy the linked list into the array, freeing the original nodes. */
1791 seq
= table
->sequences
;
1792 for (n
= 0; n
< num_sequences
; n
++)
1794 struct line_sequence
* last_seq
= seq
;
1797 sequences
[n
].low_pc
= seq
->low_pc
;
1798 sequences
[n
].prev_sequence
= NULL
;
1799 sequences
[n
].last_line
= seq
->last_line
;
1800 sequences
[n
].line_info_lookup
= NULL
;
1801 sequences
[n
].num_lines
= 0;
1802 seq
= seq
->prev_sequence
;
1805 BFD_ASSERT (seq
== NULL
);
1807 qsort (sequences
, n
, sizeof (struct line_sequence
), compare_sequences
);
1809 /* Make the list binary-searchable by trimming overlapping entries
1810 and removing nested entries. */
1812 last_high_pc
= sequences
[0].last_line
->address
;
1813 for (n
= 1; n
< table
->num_sequences
; n
++)
1815 if (sequences
[n
].low_pc
< last_high_pc
)
1817 if (sequences
[n
].last_line
->address
<= last_high_pc
)
1818 /* Skip nested entries. */
1821 /* Trim overlapping entries. */
1822 sequences
[n
].low_pc
= last_high_pc
;
1824 last_high_pc
= sequences
[n
].last_line
->address
;
1825 if (n
> num_sequences
)
1827 /* Close up the gap. */
1828 sequences
[num_sequences
].low_pc
= sequences
[n
].low_pc
;
1829 sequences
[num_sequences
].last_line
= sequences
[n
].last_line
;
1834 table
->sequences
= sequences
;
1835 table
->num_sequences
= num_sequences
;
1839 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
1842 line_info_add_include_dir (struct line_info_table
*table
, char *cur_dir
)
1844 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1849 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1850 amt
*= sizeof (char *);
1852 tmp
= (char **) bfd_realloc (table
->dirs
, amt
);
1858 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1863 line_info_add_include_dir_stub (struct line_info_table
*table
, char *cur_dir
,
1864 unsigned int dir ATTRIBUTE_UNUSED
,
1865 unsigned int xtime ATTRIBUTE_UNUSED
,
1866 unsigned int size ATTRIBUTE_UNUSED
)
1868 return line_info_add_include_dir (table
, cur_dir
);
1871 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
1874 line_info_add_file_name (struct line_info_table
*table
, char *cur_file
,
1875 unsigned int dir
, unsigned int xtime
,
1878 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1880 struct fileinfo
*tmp
;
1883 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1884 amt
*= sizeof (struct fileinfo
);
1886 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1892 table
->files
[table
->num_files
].name
= cur_file
;
1893 table
->files
[table
->num_files
].dir
= dir
;
1894 table
->files
[table
->num_files
].time
= xtime
;
1895 table
->files
[table
->num_files
].size
= size
;
1900 /* Read directory or file name entry format, starting with byte of
1901 format count entries, ULEB128 pairs of entry formats, ULEB128 of
1902 entries count and the entries themselves in the described entry
1906 read_formatted_entries (struct comp_unit
*unit
, bfd_byte
**bufp
,
1907 bfd_byte
*buf_end
, struct line_info_table
*table
,
1908 bfd_boolean (*callback
) (struct line_info_table
*table
,
1914 bfd
*abfd
= unit
->abfd
;
1915 bfd_byte format_count
, formati
;
1916 bfd_vma data_count
, datai
;
1917 bfd_byte
*buf
= *bufp
;
1918 bfd_byte
*format_header_data
;
1919 unsigned int bytes_read
;
1921 format_count
= read_1_byte (abfd
, buf
, buf_end
);
1923 format_header_data
= buf
;
1924 for (formati
= 0; formati
< format_count
; formati
++)
1926 _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
1928 _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
1932 data_count
= _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
1934 if (format_count
== 0 && data_count
!= 0)
1936 _bfd_error_handler (_("Dwarf Error: Zero format count."));
1937 bfd_set_error (bfd_error_bad_value
);
1941 /* PR 22210. Paranoia check. Don't bother running the loop
1942 if we know that we are going to run out of buffer. */
1943 if (data_count
> (bfd_vma
) (buf_end
- buf
))
1945 _bfd_error_handler (_("Dwarf Error: data count (%Lx) larger than buffer size."),
1947 bfd_set_error (bfd_error_bad_value
);
1951 for (datai
= 0; datai
< data_count
; datai
++)
1953 bfd_byte
*format
= format_header_data
;
1956 memset (&fe
, 0, sizeof fe
);
1957 for (formati
= 0; formati
< format_count
; formati
++)
1959 bfd_vma content_type
, form
;
1961 char **stringp
= &string_trash
;
1962 unsigned int uint_trash
, *uintp
= &uint_trash
;
1963 struct attribute attr
;
1965 content_type
= _bfd_safe_read_leb128 (abfd
, format
, &bytes_read
,
1967 format
+= bytes_read
;
1968 switch (content_type
)
1973 case DW_LNCT_directory_index
:
1976 case DW_LNCT_timestamp
:
1986 (_("Dwarf Error: Unknown format content type %Lu."),
1988 bfd_set_error (bfd_error_bad_value
);
1992 form
= _bfd_safe_read_leb128 (abfd
, format
, &bytes_read
, FALSE
,
1994 format
+= bytes_read
;
1996 buf
= read_attribute_value (&attr
, form
, 0, unit
, buf
, buf_end
);
2001 case DW_FORM_string
:
2002 case DW_FORM_line_strp
:
2003 *stringp
= attr
.u
.str
;
2011 *uintp
= attr
.u
.val
;
2016 if (!callback (table
, fe
.name
, fe
.dir
, fe
.time
, fe
.size
))
2024 /* Decode the line number information for UNIT. */
2026 static struct line_info_table
*
2027 decode_line_info (struct comp_unit
*unit
, struct dwarf2_debug
*stash
)
2029 bfd
*abfd
= unit
->abfd
;
2030 struct line_info_table
* table
;
2033 struct line_head lh
;
2034 unsigned int i
, bytes_read
, offset_size
;
2035 char *cur_file
, *cur_dir
;
2036 unsigned char op_code
, extended_op
, adj_opcode
;
2037 unsigned int exop_len
;
2040 if (! read_section (abfd
, &stash
->debug_sections
[debug_line
],
2041 stash
->syms
, unit
->line_offset
,
2042 &stash
->dwarf_line_buffer
, &stash
->dwarf_line_size
))
2045 amt
= sizeof (struct line_info_table
);
2046 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
2050 table
->comp_dir
= unit
->comp_dir
;
2052 table
->num_files
= 0;
2053 table
->files
= NULL
;
2055 table
->num_dirs
= 0;
2058 table
->num_sequences
= 0;
2059 table
->sequences
= NULL
;
2061 table
->lcl_head
= NULL
;
2063 if (stash
->dwarf_line_size
< 16)
2066 (_("Dwarf Error: Line info section is too small (%Ld)"),
2067 stash
->dwarf_line_size
);
2068 bfd_set_error (bfd_error_bad_value
);
2071 line_ptr
= stash
->dwarf_line_buffer
+ unit
->line_offset
;
2072 line_end
= stash
->dwarf_line_buffer
+ stash
->dwarf_line_size
;
2074 /* Read in the prologue. */
2075 lh
.total_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2078 if (lh
.total_length
== 0xffffffff)
2080 lh
.total_length
= read_8_bytes (abfd
, line_ptr
, line_end
);
2084 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
2086 /* Handle (non-standard) 64-bit DWARF2 formats. */
2087 lh
.total_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2092 if (lh
.total_length
> (size_t) (line_end
- line_ptr
))
2095 /* xgettext: c-format */
2096 (_("Dwarf Error: Line info data is bigger (%#Lx)"
2097 " than the space remaining in the section (%#lx)"),
2098 lh
.total_length
, (unsigned long) (line_end
- line_ptr
));
2099 bfd_set_error (bfd_error_bad_value
);
2103 line_end
= line_ptr
+ lh
.total_length
;
2105 lh
.version
= read_2_bytes (abfd
, line_ptr
, line_end
);
2106 if (lh
.version
< 2 || lh
.version
> 5)
2109 (_("Dwarf Error: Unhandled .debug_line version %d."), lh
.version
);
2110 bfd_set_error (bfd_error_bad_value
);
2115 if (line_ptr
+ offset_size
+ (lh
.version
>= 5 ? 8 : (lh
.version
>= 4 ? 6 : 5))
2119 (_("Dwarf Error: Ran out of room reading prologue"));
2120 bfd_set_error (bfd_error_bad_value
);
2124 if (lh
.version
>= 5)
2126 unsigned int segment_selector_size
;
2128 /* Skip address size. */
2129 read_1_byte (abfd
, line_ptr
, line_end
);
2132 segment_selector_size
= read_1_byte (abfd
, line_ptr
, line_end
);
2134 if (segment_selector_size
!= 0)
2137 (_("Dwarf Error: Line info unsupported segment selector size %u."),
2138 segment_selector_size
);
2139 bfd_set_error (bfd_error_bad_value
);
2144 if (offset_size
== 4)
2145 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2147 lh
.prologue_length
= read_8_bytes (abfd
, line_ptr
, line_end
);
2148 line_ptr
+= offset_size
;
2150 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
, line_end
);
2153 if (lh
.version
>= 4)
2155 lh
.maximum_ops_per_insn
= read_1_byte (abfd
, line_ptr
, line_end
);
2159 lh
.maximum_ops_per_insn
= 1;
2161 if (lh
.maximum_ops_per_insn
== 0)
2164 (_("Dwarf Error: Invalid maximum operations per instruction."));
2165 bfd_set_error (bfd_error_bad_value
);
2169 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
, line_end
);
2172 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
, line_end
);
2175 lh
.line_range
= read_1_byte (abfd
, line_ptr
, line_end
);
2178 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
, line_end
);
2181 if (line_ptr
+ (lh
.opcode_base
- 1) >= line_end
)
2183 _bfd_error_handler (_("Dwarf Error: Ran out of room reading opcodes"));
2184 bfd_set_error (bfd_error_bad_value
);
2188 amt
= lh
.opcode_base
* sizeof (unsigned char);
2189 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
2191 lh
.standard_opcode_lengths
[0] = 1;
2193 for (i
= 1; i
< lh
.opcode_base
; ++i
)
2195 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
, line_end
);
2199 if (lh
.version
>= 5)
2201 /* Read directory table. */
2202 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2203 line_info_add_include_dir_stub
))
2206 /* Read file name table. */
2207 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2208 line_info_add_file_name
))
2213 /* Read directory table. */
2214 while ((cur_dir
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
)) != NULL
)
2216 line_ptr
+= bytes_read
;
2218 if (!line_info_add_include_dir (table
, cur_dir
))
2222 line_ptr
+= bytes_read
;
2224 /* Read file name table. */
2225 while ((cur_file
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
)) != NULL
)
2227 unsigned int dir
, xtime
, size
;
2229 line_ptr
+= bytes_read
;
2231 dir
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2232 line_ptr
+= bytes_read
;
2233 xtime
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2234 line_ptr
+= bytes_read
;
2235 size
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2236 line_ptr
+= bytes_read
;
2238 if (!line_info_add_file_name (table
, cur_file
, dir
, xtime
, size
))
2242 line_ptr
+= bytes_read
;
2245 /* Read the statement sequences until there's nothing left. */
2246 while (line_ptr
< line_end
)
2248 /* State machine registers. */
2249 bfd_vma address
= 0;
2250 unsigned char op_index
= 0;
2251 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
2252 unsigned int line
= 1;
2253 unsigned int column
= 0;
2254 unsigned int discriminator
= 0;
2255 int is_stmt
= lh
.default_is_stmt
;
2256 int end_sequence
= 0;
2257 unsigned int dir
, xtime
, size
;
2258 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2259 compilers generate address sequences that are wildly out of
2260 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2261 for ia64-Linux). Thus, to determine the low and high
2262 address, we must compare on every DW_LNS_copy, etc. */
2263 bfd_vma low_pc
= (bfd_vma
) -1;
2264 bfd_vma high_pc
= 0;
2266 /* Decode the table. */
2267 while (!end_sequence
&& line_ptr
< line_end
)
2269 op_code
= read_1_byte (abfd
, line_ptr
, line_end
);
2272 if (op_code
>= lh
.opcode_base
)
2274 /* Special operand. */
2275 adj_opcode
= op_code
- lh
.opcode_base
;
2276 if (lh
.line_range
== 0)
2278 if (lh
.maximum_ops_per_insn
== 1)
2279 address
+= (adj_opcode
/ lh
.line_range
2280 * lh
.minimum_instruction_length
);
2283 address
+= ((op_index
+ adj_opcode
/ lh
.line_range
)
2284 / lh
.maximum_ops_per_insn
2285 * lh
.minimum_instruction_length
);
2286 op_index
= ((op_index
+ adj_opcode
/ lh
.line_range
)
2287 % lh
.maximum_ops_per_insn
);
2289 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
2290 /* Append row to matrix using current values. */
2291 if (!add_line_info (table
, address
, op_index
, filename
,
2292 line
, column
, discriminator
, 0))
2295 if (address
< low_pc
)
2297 if (address
> high_pc
)
2300 else switch (op_code
)
2302 case DW_LNS_extended_op
:
2303 exop_len
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2305 line_ptr
+= bytes_read
;
2306 extended_op
= read_1_byte (abfd
, line_ptr
, line_end
);
2309 switch (extended_op
)
2311 case DW_LNE_end_sequence
:
2313 if (!add_line_info (table
, address
, op_index
, filename
, line
,
2314 column
, discriminator
, end_sequence
))
2317 if (address
< low_pc
)
2319 if (address
> high_pc
)
2321 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
2324 case DW_LNE_set_address
:
2325 address
= read_address (unit
, line_ptr
, line_end
);
2327 line_ptr
+= unit
->addr_size
;
2329 case DW_LNE_define_file
:
2330 cur_file
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
);
2331 line_ptr
+= bytes_read
;
2332 dir
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2334 line_ptr
+= bytes_read
;
2335 xtime
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2337 line_ptr
+= bytes_read
;
2338 size
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2340 line_ptr
+= bytes_read
;
2341 if (!line_info_add_file_name (table
, cur_file
, dir
,
2345 case DW_LNE_set_discriminator
:
2347 _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2349 line_ptr
+= bytes_read
;
2351 case DW_LNE_HP_source_file_correlation
:
2352 line_ptr
+= exop_len
- 1;
2356 (_("Dwarf Error: mangled line number section."));
2357 bfd_set_error (bfd_error_bad_value
);
2359 if (filename
!= NULL
)
2365 if (!add_line_info (table
, address
, op_index
,
2366 filename
, line
, column
, discriminator
, 0))
2369 if (address
< low_pc
)
2371 if (address
> high_pc
)
2374 case DW_LNS_advance_pc
:
2375 if (lh
.maximum_ops_per_insn
== 1)
2376 address
+= (lh
.minimum_instruction_length
2377 * _bfd_safe_read_leb128 (abfd
, line_ptr
,
2382 bfd_vma adjust
= _bfd_safe_read_leb128 (abfd
, line_ptr
,
2385 address
= ((op_index
+ adjust
) / lh
.maximum_ops_per_insn
2386 * lh
.minimum_instruction_length
);
2387 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2389 line_ptr
+= bytes_read
;
2391 case DW_LNS_advance_line
:
2392 line
+= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2394 line_ptr
+= bytes_read
;
2396 case DW_LNS_set_file
:
2400 /* The file and directory tables are 0
2401 based, the references are 1 based. */
2402 file
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2404 line_ptr
+= bytes_read
;
2407 filename
= concat_filename (table
, file
);
2410 case DW_LNS_set_column
:
2411 column
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2413 line_ptr
+= bytes_read
;
2415 case DW_LNS_negate_stmt
:
2416 is_stmt
= (!is_stmt
);
2418 case DW_LNS_set_basic_block
:
2420 case DW_LNS_const_add_pc
:
2421 if (lh
.line_range
== 0)
2423 if (lh
.maximum_ops_per_insn
== 1)
2424 address
+= (lh
.minimum_instruction_length
2425 * ((255 - lh
.opcode_base
) / lh
.line_range
));
2428 bfd_vma adjust
= ((255 - lh
.opcode_base
) / lh
.line_range
);
2429 address
+= (lh
.minimum_instruction_length
2430 * ((op_index
+ adjust
)
2431 / lh
.maximum_ops_per_insn
));
2432 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2435 case DW_LNS_fixed_advance_pc
:
2436 address
+= read_2_bytes (abfd
, line_ptr
, line_end
);
2441 /* Unknown standard opcode, ignore it. */
2442 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
2444 (void) _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2446 line_ptr
+= bytes_read
;
2456 if (sort_line_sequences (table
))
2460 while (table
->sequences
!= NULL
)
2462 struct line_sequence
* seq
= table
->sequences
;
2463 table
->sequences
= table
->sequences
->prev_sequence
;
2466 if (table
->files
!= NULL
)
2467 free (table
->files
);
2468 if (table
->dirs
!= NULL
)
2473 /* If ADDR is within TABLE set the output parameters and return the
2474 range of addresses covered by the entry used to fill them out.
2475 Otherwise set * FILENAME_PTR to NULL and return 0.
2476 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2477 are pointers to the objects to be filled in. */
2480 lookup_address_in_line_info_table (struct line_info_table
*table
,
2482 const char **filename_ptr
,
2483 unsigned int *linenumber_ptr
,
2484 unsigned int *discriminator_ptr
)
2486 struct line_sequence
*seq
= NULL
;
2487 struct line_info
*info
;
2490 /* Binary search the array of sequences. */
2492 high
= table
->num_sequences
;
2495 mid
= (low
+ high
) / 2;
2496 seq
= &table
->sequences
[mid
];
2497 if (addr
< seq
->low_pc
)
2499 else if (addr
>= seq
->last_line
->address
)
2505 /* Check for a valid sequence. */
2506 if (!seq
|| addr
< seq
->low_pc
|| addr
>= seq
->last_line
->address
)
2509 if (!build_line_info_table (table
, seq
))
2512 /* Binary search the array of line information. */
2514 high
= seq
->num_lines
;
2518 mid
= (low
+ high
) / 2;
2519 info
= seq
->line_info_lookup
[mid
];
2520 if (addr
< info
->address
)
2522 else if (addr
>= seq
->line_info_lookup
[mid
+ 1]->address
)
2528 /* Check for a valid line information entry. */
2530 && addr
>= info
->address
2531 && addr
< seq
->line_info_lookup
[mid
+ 1]->address
2532 && !(info
->end_sequence
|| info
== seq
->last_line
))
2534 *filename_ptr
= info
->filename
;
2535 *linenumber_ptr
= info
->line
;
2536 if (discriminator_ptr
)
2537 *discriminator_ptr
= info
->discriminator
;
2538 return seq
->last_line
->address
- seq
->low_pc
;
2542 *filename_ptr
= NULL
;
2546 /* Read in the .debug_ranges section for future reference. */
2549 read_debug_ranges (struct comp_unit
* unit
)
2551 struct dwarf2_debug
* stash
= unit
->stash
;
2553 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_ranges
],
2555 &stash
->dwarf_ranges_buffer
,
2556 &stash
->dwarf_ranges_size
);
2559 /* Function table functions. */
2562 compare_lookup_funcinfos (const void * a
, const void * b
)
2564 const struct lookup_funcinfo
* lookup1
= a
;
2565 const struct lookup_funcinfo
* lookup2
= b
;
2567 if (lookup1
->low_addr
< lookup2
->low_addr
)
2569 if (lookup1
->low_addr
> lookup2
->low_addr
)
2571 if (lookup1
->high_addr
< lookup2
->high_addr
)
2573 if (lookup1
->high_addr
> lookup2
->high_addr
)
2580 build_lookup_funcinfo_table (struct comp_unit
* unit
)
2582 struct lookup_funcinfo
*lookup_funcinfo_table
= unit
->lookup_funcinfo_table
;
2583 unsigned int number_of_functions
= unit
->number_of_functions
;
2584 struct funcinfo
*each
;
2585 struct lookup_funcinfo
*entry
;
2587 struct arange
*range
;
2588 bfd_vma low_addr
, high_addr
;
2590 if (lookup_funcinfo_table
|| number_of_functions
== 0)
2593 /* Create the function info lookup table. */
2594 lookup_funcinfo_table
= (struct lookup_funcinfo
*)
2595 bfd_malloc (number_of_functions
* sizeof (struct lookup_funcinfo
));
2596 if (lookup_funcinfo_table
== NULL
)
2599 /* Populate the function info lookup table. */
2600 func_index
= number_of_functions
;
2601 for (each
= unit
->function_table
; each
; each
= each
->prev_func
)
2603 entry
= &lookup_funcinfo_table
[--func_index
];
2604 entry
->funcinfo
= each
;
2606 /* Calculate the lowest and highest address for this function entry. */
2607 low_addr
= entry
->funcinfo
->arange
.low
;
2608 high_addr
= entry
->funcinfo
->arange
.high
;
2610 for (range
= entry
->funcinfo
->arange
.next
; range
; range
= range
->next
)
2612 if (range
->low
< low_addr
)
2613 low_addr
= range
->low
;
2614 if (range
->high
> high_addr
)
2615 high_addr
= range
->high
;
2618 entry
->low_addr
= low_addr
;
2619 entry
->high_addr
= high_addr
;
2622 BFD_ASSERT (func_index
== 0);
2624 /* Sort the function by address. */
2625 qsort (lookup_funcinfo_table
,
2626 number_of_functions
,
2627 sizeof (struct lookup_funcinfo
),
2628 compare_lookup_funcinfos
);
2630 /* Calculate the high watermark for each function in the lookup table. */
2631 high_addr
= lookup_funcinfo_table
[0].high_addr
;
2632 for (func_index
= 1; func_index
< number_of_functions
; func_index
++)
2634 entry
= &lookup_funcinfo_table
[func_index
];
2635 if (entry
->high_addr
> high_addr
)
2636 high_addr
= entry
->high_addr
;
2638 entry
->high_addr
= high_addr
;
2641 unit
->lookup_funcinfo_table
= lookup_funcinfo_table
;
2645 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
2646 TRUE. Note that we need to find the function that has the smallest range
2647 that contains ADDR, to handle inlined functions without depending upon
2648 them being ordered in TABLE by increasing range. */
2651 lookup_address_in_function_table (struct comp_unit
*unit
,
2653 struct funcinfo
**function_ptr
)
2655 unsigned int number_of_functions
= unit
->number_of_functions
;
2656 struct lookup_funcinfo
* lookup_funcinfo
= NULL
;
2657 struct funcinfo
* funcinfo
= NULL
;
2658 struct funcinfo
* best_fit
= NULL
;
2659 bfd_vma best_fit_len
= 0;
2660 bfd_size_type low
, high
, mid
, first
;
2661 struct arange
*arange
;
2663 if (number_of_functions
== 0)
2666 if (!build_lookup_funcinfo_table (unit
))
2669 if (unit
->lookup_funcinfo_table
[number_of_functions
- 1].high_addr
< addr
)
2672 /* Find the first function in the lookup table which may contain the
2673 specified address. */
2675 high
= number_of_functions
;
2679 mid
= (low
+ high
) / 2;
2680 lookup_funcinfo
= &unit
->lookup_funcinfo_table
[mid
];
2681 if (addr
< lookup_funcinfo
->low_addr
)
2683 else if (addr
>= lookup_funcinfo
->high_addr
)
2689 /* Find the 'best' match for the address. The prior algorithm defined the
2690 best match as the function with the smallest address range containing
2691 the specified address. This definition should probably be changed to the
2692 innermost inline routine containing the address, but right now we want
2693 to get the same results we did before. */
2694 while (first
< number_of_functions
)
2696 if (addr
< unit
->lookup_funcinfo_table
[first
].low_addr
)
2698 funcinfo
= unit
->lookup_funcinfo_table
[first
].funcinfo
;
2700 for (arange
= &funcinfo
->arange
; arange
; arange
= arange
->next
)
2702 if (addr
< arange
->low
|| addr
>= arange
->high
)
2706 || arange
->high
- arange
->low
< best_fit_len
2707 /* The following comparison is designed to return the same
2708 match as the previous algorithm for routines which have the
2709 same best fit length. */
2710 || (arange
->high
- arange
->low
== best_fit_len
2711 && funcinfo
> best_fit
))
2713 best_fit
= funcinfo
;
2714 best_fit_len
= arange
->high
- arange
->low
;
2724 *function_ptr
= best_fit
;
2728 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2729 and LINENUMBER_PTR, and return TRUE. */
2732 lookup_symbol_in_function_table (struct comp_unit
*unit
,
2735 const char **filename_ptr
,
2736 unsigned int *linenumber_ptr
)
2738 struct funcinfo
* each_func
;
2739 struct funcinfo
* best_fit
= NULL
;
2740 bfd_vma best_fit_len
= 0;
2741 struct arange
*arange
;
2742 const char *name
= bfd_asymbol_name (sym
);
2743 asection
*sec
= bfd_get_section (sym
);
2745 for (each_func
= unit
->function_table
;
2747 each_func
= each_func
->prev_func
)
2749 for (arange
= &each_func
->arange
;
2751 arange
= arange
->next
)
2753 if ((!each_func
->sec
|| each_func
->sec
== sec
)
2754 && addr
>= arange
->low
2755 && addr
< arange
->high
2757 && strcmp (name
, each_func
->name
) == 0
2759 || arange
->high
- arange
->low
< best_fit_len
))
2761 best_fit
= each_func
;
2762 best_fit_len
= arange
->high
- arange
->low
;
2769 best_fit
->sec
= sec
;
2770 *filename_ptr
= best_fit
->file
;
2771 *linenumber_ptr
= best_fit
->line
;
2778 /* Variable table functions. */
2780 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
2781 LINENUMBER_PTR, and return TRUE. */
2784 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
2787 const char **filename_ptr
,
2788 unsigned int *linenumber_ptr
)
2790 const char *name
= bfd_asymbol_name (sym
);
2791 asection
*sec
= bfd_get_section (sym
);
2792 struct varinfo
* each
;
2794 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
2795 if (each
->stack
== 0
2796 && each
->file
!= NULL
2797 && each
->name
!= NULL
2798 && each
->addr
== addr
2799 && (!each
->sec
|| each
->sec
== sec
)
2800 && strcmp (name
, each
->name
) == 0)
2806 *filename_ptr
= each
->file
;
2807 *linenumber_ptr
= each
->line
;
2815 find_abstract_instance_name (struct comp_unit
*unit
,
2816 bfd_byte
*orig_info_ptr
,
2817 struct attribute
*attr_ptr
,
2819 bfd_boolean
*is_linkage
)
2821 bfd
*abfd
= unit
->abfd
;
2823 bfd_byte
*info_ptr_end
;
2824 unsigned int abbrev_number
, bytes_read
, i
;
2825 struct abbrev_info
*abbrev
;
2826 bfd_uint64_t die_ref
= attr_ptr
->u
.val
;
2827 struct attribute attr
;
2828 const char *name
= NULL
;
2830 /* DW_FORM_ref_addr can reference an entry in a different CU. It
2831 is an offset from the .debug_info section, not the current CU. */
2832 if (attr_ptr
->form
== DW_FORM_ref_addr
)
2834 /* We only support DW_FORM_ref_addr within the same file, so
2835 any relocations should be resolved already. Check this by
2836 testing for a zero die_ref; There can't be a valid reference
2837 to the header of a .debug_info section.
2838 DW_FORM_ref_addr is an offset relative to .debug_info.
2839 Normally when using the GNU linker this is accomplished by
2840 emitting a symbolic reference to a label, because .debug_info
2841 sections are linked at zero. When there are multiple section
2842 groups containing .debug_info, as there might be in a
2843 relocatable object file, it would be reasonable to assume that
2844 a symbolic reference to a label in any .debug_info section
2845 might be used. Since we lay out multiple .debug_info
2846 sections at non-zero VMAs (see place_sections), and read
2847 them contiguously into stash->info_ptr_memory, that means
2848 the reference is relative to stash->info_ptr_memory. */
2851 info_ptr
= unit
->stash
->info_ptr_memory
;
2852 info_ptr_end
= unit
->stash
->info_ptr_end
;
2853 total
= info_ptr_end
- info_ptr
;
2854 if (!die_ref
|| die_ref
>= total
)
2857 (_("Dwarf Error: Invalid abstract instance DIE ref."));
2858 bfd_set_error (bfd_error_bad_value
);
2861 info_ptr
+= die_ref
;
2863 /* Now find the CU containing this pointer. */
2864 if (info_ptr
>= unit
->info_ptr_unit
&& info_ptr
< unit
->end_ptr
)
2865 info_ptr_end
= unit
->end_ptr
;
2868 /* Check other CUs to see if they contain the abbrev. */
2869 struct comp_unit
* u
;
2871 for (u
= unit
->prev_unit
; u
!= NULL
; u
= u
->prev_unit
)
2872 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
2876 for (u
= unit
->next_unit
; u
!= NULL
; u
= u
->next_unit
)
2877 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
2883 info_ptr_end
= unit
->end_ptr
;
2885 /* else FIXME: What do we do now ? */
2888 else if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
2890 info_ptr
= read_alt_indirect_ref (unit
, die_ref
);
2891 if (info_ptr
== NULL
)
2894 (_("Dwarf Error: Unable to read alt ref %llu."),
2895 (long long) die_ref
);
2896 bfd_set_error (bfd_error_bad_value
);
2899 info_ptr_end
= (unit
->stash
->alt_dwarf_info_buffer
2900 + unit
->stash
->alt_dwarf_info_size
);
2902 /* FIXME: Do we need to locate the correct CU, in a similar
2903 fashion to the code in the DW_FORM_ref_addr case above ? */
2907 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
2908 DW_FORM_ref_udata. These are all references relative to the
2909 start of the current CU. */
2912 info_ptr
= unit
->info_ptr_unit
;
2913 info_ptr_end
= unit
->end_ptr
;
2914 total
= info_ptr_end
- info_ptr
;
2915 if (!die_ref
|| die_ref
>= total
)
2918 (_("Dwarf Error: Invalid abstract instance DIE ref."));
2919 bfd_set_error (bfd_error_bad_value
);
2922 info_ptr
+= die_ref
;
2925 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
2926 FALSE
, info_ptr_end
);
2927 info_ptr
+= bytes_read
;
2931 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
2935 (_("Dwarf Error: Could not find abbrev number %u."), abbrev_number
);
2936 bfd_set_error (bfd_error_bad_value
);
2941 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2943 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
,
2944 info_ptr
, info_ptr_end
);
2945 if (info_ptr
== NULL
)
2947 /* It doesn't ever make sense for DW_AT_specification to
2948 refer to the same DIE. Stop simple recursion. */
2949 if (info_ptr
== orig_info_ptr
)
2952 (_("Dwarf Error: Abstract instance recursion detected."));
2953 bfd_set_error (bfd_error_bad_value
);
2959 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2961 if (name
== NULL
&& is_str_attr (attr
.form
))
2964 if (non_mangled (unit
->lang
))
2968 case DW_AT_specification
:
2969 if (!find_abstract_instance_name (unit
, info_ptr
, &attr
,
2973 case DW_AT_linkage_name
:
2974 case DW_AT_MIPS_linkage_name
:
2975 /* PR 16949: Corrupt debug info can place
2976 non-string forms into these attributes. */
2977 if (is_str_attr (attr
.form
))
2994 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
,
2995 bfd_uint64_t offset
)
2997 bfd_byte
*ranges_ptr
;
2998 bfd_byte
*ranges_end
;
2999 bfd_vma base_address
= unit
->base_address
;
3001 if (! unit
->stash
->dwarf_ranges_buffer
)
3003 if (! read_debug_ranges (unit
))
3007 ranges_ptr
= unit
->stash
->dwarf_ranges_buffer
+ offset
;
3008 if (ranges_ptr
< unit
->stash
->dwarf_ranges_buffer
)
3010 ranges_end
= unit
->stash
->dwarf_ranges_buffer
+ unit
->stash
->dwarf_ranges_size
;
3017 /* PR 17512: file: 62cada7d. */
3018 if (ranges_ptr
+ 2 * unit
->addr_size
> ranges_end
)
3021 low_pc
= read_address (unit
, ranges_ptr
, ranges_end
);
3022 ranges_ptr
+= unit
->addr_size
;
3023 high_pc
= read_address (unit
, ranges_ptr
, ranges_end
);
3024 ranges_ptr
+= unit
->addr_size
;
3026 if (low_pc
== 0 && high_pc
== 0)
3028 if (low_pc
== -1UL && high_pc
!= -1UL)
3029 base_address
= high_pc
;
3032 if (!arange_add (unit
, arange
,
3033 base_address
+ low_pc
, base_address
+ high_pc
))
3040 /* DWARF2 Compilation unit functions. */
3042 /* Scan over each die in a comp. unit looking for functions to add
3043 to the function table and variables to the variable table. */
3046 scan_unit_for_symbols (struct comp_unit
*unit
)
3048 bfd
*abfd
= unit
->abfd
;
3049 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
3050 bfd_byte
*info_ptr_end
= unit
->stash
->info_ptr_end
;
3051 int nesting_level
= 0;
3052 struct nest_funcinfo
{
3053 struct funcinfo
*func
;
3055 int nested_funcs_size
;
3057 /* Maintain a stack of in-scope functions and inlined functions, which we
3058 can use to set the caller_func field. */
3059 nested_funcs_size
= 32;
3060 nested_funcs
= (struct nest_funcinfo
*)
3061 bfd_malloc (nested_funcs_size
* sizeof (*nested_funcs
));
3062 if (nested_funcs
== NULL
)
3064 nested_funcs
[nesting_level
].func
= 0;
3066 while (nesting_level
>= 0)
3068 unsigned int abbrev_number
, bytes_read
, i
;
3069 struct abbrev_info
*abbrev
;
3070 struct attribute attr
;
3071 struct funcinfo
*func
;
3072 struct varinfo
*var
;
3074 bfd_vma high_pc
= 0;
3075 bfd_boolean high_pc_relative
= FALSE
;
3077 /* PR 17512: file: 9f405d9d. */
3078 if (info_ptr
>= info_ptr_end
)
3081 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
3082 FALSE
, info_ptr_end
);
3083 info_ptr
+= bytes_read
;
3085 if (! abbrev_number
)
3091 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3094 static unsigned int previous_failed_abbrev
= -1U;
3096 /* Avoid multiple reports of the same missing abbrev. */
3097 if (abbrev_number
!= previous_failed_abbrev
)
3100 (_("Dwarf Error: Could not find abbrev number %u."),
3102 previous_failed_abbrev
= abbrev_number
;
3104 bfd_set_error (bfd_error_bad_value
);
3109 if (abbrev
->tag
== DW_TAG_subprogram
3110 || abbrev
->tag
== DW_TAG_entry_point
3111 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3113 bfd_size_type amt
= sizeof (struct funcinfo
);
3114 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
3117 func
->tag
= abbrev
->tag
;
3118 func
->prev_func
= unit
->function_table
;
3119 unit
->function_table
= func
;
3120 unit
->number_of_functions
++;
3121 BFD_ASSERT (!unit
->cached
);
3123 if (func
->tag
== DW_TAG_inlined_subroutine
)
3124 for (i
= nesting_level
; i
-- != 0; )
3125 if (nested_funcs
[i
].func
)
3127 func
->caller_func
= nested_funcs
[i
].func
;
3130 nested_funcs
[nesting_level
].func
= func
;
3135 if (abbrev
->tag
== DW_TAG_variable
)
3137 bfd_size_type amt
= sizeof (struct varinfo
);
3138 var
= (struct varinfo
*) bfd_zalloc (abfd
, amt
);
3141 var
->tag
= abbrev
->tag
;
3143 var
->prev_var
= unit
->variable_table
;
3144 unit
->variable_table
= var
;
3145 /* PR 18205: Missing debug information can cause this
3146 var to be attached to an already cached unit. */
3149 /* No inline function in scope at this nesting level. */
3150 nested_funcs
[nesting_level
].func
= 0;
3153 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3155 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
3156 unit
, info_ptr
, info_ptr_end
);
3157 if (info_ptr
== NULL
)
3164 case DW_AT_call_file
:
3165 func
->caller_file
= concat_filename (unit
->line_table
,
3169 case DW_AT_call_line
:
3170 func
->caller_line
= attr
.u
.val
;
3173 case DW_AT_abstract_origin
:
3174 case DW_AT_specification
:
3175 if (!find_abstract_instance_name (unit
, info_ptr
, &attr
,
3182 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3184 if (func
->name
== NULL
&& is_str_attr (attr
.form
))
3186 func
->name
= attr
.u
.str
;
3187 if (non_mangled (unit
->lang
))
3188 func
->is_linkage
= TRUE
;
3192 case DW_AT_linkage_name
:
3193 case DW_AT_MIPS_linkage_name
:
3194 /* PR 16949: Corrupt debug info can place
3195 non-string forms into these attributes. */
3196 if (is_str_attr (attr
.form
))
3198 func
->name
= attr
.u
.str
;
3199 func
->is_linkage
= TRUE
;
3204 low_pc
= attr
.u
.val
;
3208 high_pc
= attr
.u
.val
;
3209 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
3213 if (!read_rangelist (unit
, &func
->arange
, attr
.u
.val
))
3217 case DW_AT_decl_file
:
3218 func
->file
= concat_filename (unit
->line_table
,
3222 case DW_AT_decl_line
:
3223 func
->line
= attr
.u
.val
;
3235 if (is_str_attr (attr
.form
))
3236 var
->name
= attr
.u
.str
;
3239 case DW_AT_decl_file
:
3240 var
->file
= concat_filename (unit
->line_table
,
3244 case DW_AT_decl_line
:
3245 var
->line
= attr
.u
.val
;
3248 case DW_AT_external
:
3249 if (attr
.u
.val
!= 0)
3253 case DW_AT_location
:
3257 case DW_FORM_block1
:
3258 case DW_FORM_block2
:
3259 case DW_FORM_block4
:
3260 case DW_FORM_exprloc
:
3261 if (attr
.u
.blk
->data
!= NULL
3262 && *attr
.u
.blk
->data
== DW_OP_addr
)
3266 /* Verify that DW_OP_addr is the only opcode in the
3267 location, in which case the block size will be 1
3268 plus the address size. */
3269 /* ??? For TLS variables, gcc can emit
3270 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
3271 which we don't handle here yet. */
3272 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
3273 var
->addr
= bfd_get (unit
->addr_size
* 8,
3275 attr
.u
.blk
->data
+ 1);
3290 if (high_pc_relative
)
3293 if (func
&& high_pc
!= 0)
3295 if (!arange_add (unit
, &func
->arange
, low_pc
, high_pc
))
3299 if (abbrev
->has_children
)
3303 if (nesting_level
>= nested_funcs_size
)
3305 struct nest_funcinfo
*tmp
;
3307 nested_funcs_size
*= 2;
3308 tmp
= (struct nest_funcinfo
*)
3309 bfd_realloc (nested_funcs
,
3310 nested_funcs_size
* sizeof (*nested_funcs
));
3315 nested_funcs
[nesting_level
].func
= 0;
3319 free (nested_funcs
);
3323 free (nested_funcs
);
3327 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
3328 includes the compilation unit header that proceeds the DIE's, but
3329 does not include the length field that precedes each compilation
3330 unit header. END_PTR points one past the end of this comp unit.
3331 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
3333 This routine does not read the whole compilation unit; only enough
3334 to get to the line number information for the compilation unit. */
3336 static struct comp_unit
*
3337 parse_comp_unit (struct dwarf2_debug
*stash
,
3338 bfd_vma unit_length
,
3339 bfd_byte
*info_ptr_unit
,
3340 unsigned int offset_size
)
3342 struct comp_unit
* unit
;
3343 unsigned int version
;
3344 bfd_uint64_t abbrev_offset
= 0;
3345 /* Initialize it just to avoid a GCC false warning. */
3346 unsigned int addr_size
= -1;
3347 struct abbrev_info
** abbrevs
;
3348 unsigned int abbrev_number
, bytes_read
, i
;
3349 struct abbrev_info
*abbrev
;
3350 struct attribute attr
;
3351 bfd_byte
*info_ptr
= stash
->info_ptr
;
3352 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
3355 bfd_vma high_pc
= 0;
3356 bfd
*abfd
= stash
->bfd_ptr
;
3357 bfd_boolean high_pc_relative
= FALSE
;
3358 enum dwarf_unit_type unit_type
;
3360 version
= read_2_bytes (abfd
, info_ptr
, end_ptr
);
3362 if (version
< 2 || version
> 5)
3364 /* PR 19872: A version number of 0 probably means that there is padding
3365 at the end of the .debug_info section. Gold puts it there when
3366 performing an incremental link, for example. So do not generate
3367 an error, just return a NULL. */
3371 (_("Dwarf Error: found dwarf version '%u', this reader"
3372 " only handles version 2, 3, 4 and 5 information."), version
);
3373 bfd_set_error (bfd_error_bad_value
);
3379 unit_type
= DW_UT_compile
;
3382 unit_type
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3385 addr_size
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3389 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
3390 if (offset_size
== 4)
3391 abbrev_offset
= read_4_bytes (abfd
, info_ptr
, end_ptr
);
3393 abbrev_offset
= read_8_bytes (abfd
, info_ptr
, end_ptr
);
3394 info_ptr
+= offset_size
;
3398 addr_size
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3402 if (unit_type
== DW_UT_type
)
3404 /* Skip type signature. */
3407 /* Skip type offset. */
3408 info_ptr
+= offset_size
;
3411 if (addr_size
> sizeof (bfd_vma
))
3414 /* xgettext: c-format */
3415 (_("Dwarf Error: found address size '%u', this reader"
3416 " can not handle sizes greater than '%u'."),
3418 (unsigned int) sizeof (bfd_vma
));
3419 bfd_set_error (bfd_error_bad_value
);
3423 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
3426 ("Dwarf Error: found address size '%u', this reader"
3427 " can only handle address sizes '2', '4' and '8'.", addr_size
);
3428 bfd_set_error (bfd_error_bad_value
);
3432 /* Read the abbrevs for this compilation unit into a table. */
3433 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
);
3437 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
3439 info_ptr
+= bytes_read
;
3440 if (! abbrev_number
)
3442 /* PR 19872: An abbrev number of 0 probably means that there is padding
3443 at the end of the .debug_abbrev section. Gold puts it there when
3444 performing an incremental link, for example. So do not generate
3445 an error, just return a NULL. */
3449 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
3452 _bfd_error_handler (_("Dwarf Error: Could not find abbrev number %u."),
3454 bfd_set_error (bfd_error_bad_value
);
3458 amt
= sizeof (struct comp_unit
);
3459 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
3463 unit
->version
= version
;
3464 unit
->addr_size
= addr_size
;
3465 unit
->offset_size
= offset_size
;
3466 unit
->abbrevs
= abbrevs
;
3467 unit
->end_ptr
= end_ptr
;
3468 unit
->stash
= stash
;
3469 unit
->info_ptr_unit
= info_ptr_unit
;
3471 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3473 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
, end_ptr
);
3474 if (info_ptr
== NULL
)
3477 /* Store the data if it is of an attribute we want to keep in a
3478 partial symbol table. */
3481 case DW_AT_stmt_list
:
3483 unit
->line_offset
= attr
.u
.val
;
3487 if (is_str_attr (attr
.form
))
3488 unit
->name
= attr
.u
.str
;
3492 low_pc
= attr
.u
.val
;
3493 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
3494 this is the base address to use when reading location
3495 lists or range lists. */
3496 if (abbrev
->tag
== DW_TAG_compile_unit
)
3497 unit
->base_address
= low_pc
;
3501 high_pc
= attr
.u
.val
;
3502 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
3506 if (!read_rangelist (unit
, &unit
->arange
, attr
.u
.val
))
3510 case DW_AT_comp_dir
:
3512 char *comp_dir
= attr
.u
.str
;
3514 /* PR 17512: file: 1fe726be. */
3515 if (! is_str_attr (attr
.form
))
3518 (_("Dwarf Error: DW_AT_comp_dir attribute encountered with a non-string form."));
3524 /* Irix 6.2 native cc prepends <machine>.: to the compilation
3525 directory, get rid of it. */
3526 char *cp
= strchr (comp_dir
, ':');
3528 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
3531 unit
->comp_dir
= comp_dir
;
3535 case DW_AT_language
:
3536 unit
->lang
= attr
.u
.val
;
3543 if (high_pc_relative
)
3547 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
3551 unit
->first_child_die_ptr
= info_ptr
;
3555 /* Return TRUE if UNIT may contain the address given by ADDR. When
3556 there are functions written entirely with inline asm statements, the
3557 range info in the compilation unit header may not be correct. We
3558 need to consult the line info table to see if a compilation unit
3559 really contains the given address. */
3562 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
3564 struct arange
*arange
;
3569 arange
= &unit
->arange
;
3572 if (addr
>= arange
->low
&& addr
< arange
->high
)
3574 arange
= arange
->next
;
3581 /* If UNIT contains ADDR, set the output parameters to the values for
3582 the line containing ADDR. The output parameters, FILENAME_PTR,
3583 FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects
3586 Returns the range of addresses covered by the entry that was used
3587 to fill in *LINENUMBER_PTR or 0 if it was not filled in. */
3590 comp_unit_find_nearest_line (struct comp_unit
*unit
,
3592 const char **filename_ptr
,
3593 struct funcinfo
**function_ptr
,
3594 unsigned int *linenumber_ptr
,
3595 unsigned int *discriminator_ptr
,
3596 struct dwarf2_debug
*stash
)
3603 if (! unit
->line_table
)
3605 if (! unit
->stmtlist
)
3611 unit
->line_table
= decode_line_info (unit
, stash
);
3613 if (! unit
->line_table
)
3619 if (unit
->first_child_die_ptr
< unit
->end_ptr
3620 && ! scan_unit_for_symbols (unit
))
3627 *function_ptr
= NULL
;
3628 func_p
= lookup_address_in_function_table (unit
, addr
, function_ptr
);
3629 if (func_p
&& (*function_ptr
)->tag
== DW_TAG_inlined_subroutine
)
3630 stash
->inliner_chain
= *function_ptr
;
3632 return lookup_address_in_line_info_table (unit
->line_table
, addr
,
3638 /* Check to see if line info is already decoded in a comp_unit.
3639 If not, decode it. Returns TRUE if no errors were encountered;
3643 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
,
3644 struct dwarf2_debug
*stash
)
3649 if (! unit
->line_table
)
3651 if (! unit
->stmtlist
)
3657 unit
->line_table
= decode_line_info (unit
, stash
);
3659 if (! unit
->line_table
)
3665 if (unit
->first_child_die_ptr
< unit
->end_ptr
3666 && ! scan_unit_for_symbols (unit
))
3676 /* If UNIT contains SYM at ADDR, set the output parameters to the
3677 values for the line containing SYM. The output parameters,
3678 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
3681 Return TRUE if UNIT contains SYM, and no errors were encountered;
3685 comp_unit_find_line (struct comp_unit
*unit
,
3688 const char **filename_ptr
,
3689 unsigned int *linenumber_ptr
,
3690 struct dwarf2_debug
*stash
)
3692 if (!comp_unit_maybe_decode_line_info (unit
, stash
))
3695 if (sym
->flags
& BSF_FUNCTION
)
3696 return lookup_symbol_in_function_table (unit
, sym
, addr
,
3700 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
3705 static struct funcinfo
*
3706 reverse_funcinfo_list (struct funcinfo
*head
)
3708 struct funcinfo
*rhead
;
3709 struct funcinfo
*temp
;
3711 for (rhead
= NULL
; head
; head
= temp
)
3713 temp
= head
->prev_func
;
3714 head
->prev_func
= rhead
;
3720 static struct varinfo
*
3721 reverse_varinfo_list (struct varinfo
*head
)
3723 struct varinfo
*rhead
;
3724 struct varinfo
*temp
;
3726 for (rhead
= NULL
; head
; head
= temp
)
3728 temp
= head
->prev_var
;
3729 head
->prev_var
= rhead
;
3735 /* Extract all interesting funcinfos and varinfos of a compilation
3736 unit into hash tables for faster lookup. Returns TRUE if no
3737 errors were enountered; FALSE otherwise. */
3740 comp_unit_hash_info (struct dwarf2_debug
*stash
,
3741 struct comp_unit
*unit
,
3742 struct info_hash_table
*funcinfo_hash_table
,
3743 struct info_hash_table
*varinfo_hash_table
)
3745 struct funcinfo
* each_func
;
3746 struct varinfo
* each_var
;
3747 bfd_boolean okay
= TRUE
;
3749 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
3751 if (!comp_unit_maybe_decode_line_info (unit
, stash
))
3754 BFD_ASSERT (!unit
->cached
);
3756 /* To preserve the original search order, we went to visit the function
3757 infos in the reversed order of the list. However, making the list
3758 bi-directional use quite a bit of extra memory. So we reverse
3759 the list first, traverse the list in the now reversed order and
3760 finally reverse the list again to get back the original order. */
3761 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
3762 for (each_func
= unit
->function_table
;
3764 each_func
= each_func
->prev_func
)
3766 /* Skip nameless functions. */
3767 if (each_func
->name
)
3768 /* There is no need to copy name string into hash table as
3769 name string is either in the dwarf string buffer or
3770 info in the stash. */
3771 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
3772 (void*) each_func
, FALSE
);
3774 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
3778 /* We do the same for variable infos. */
3779 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
3780 for (each_var
= unit
->variable_table
;
3782 each_var
= each_var
->prev_var
)
3784 /* Skip stack vars and vars with no files or names. */
3785 if (each_var
->stack
== 0
3786 && each_var
->file
!= NULL
3787 && each_var
->name
!= NULL
)
3788 /* There is no need to copy name string into hash table as
3789 name string is either in the dwarf string buffer or
3790 info in the stash. */
3791 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
3792 (void*) each_var
, FALSE
);
3795 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
3796 unit
->cached
= TRUE
;
3800 /* Locate a section in a BFD containing debugging info. The search starts
3801 from the section after AFTER_SEC, or from the first section in the BFD if
3802 AFTER_SEC is NULL. The search works by examining the names of the
3803 sections. There are three permissiable names. The first two are given
3804 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
3805 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
3806 This is a variation on the .debug_info section which has a checksum
3807 describing the contents appended onto the name. This allows the linker to
3808 identify and discard duplicate debugging sections for different
3809 compilation units. */
3810 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
3813 find_debug_info (bfd
*abfd
, const struct dwarf_debug_section
*debug_sections
,
3814 asection
*after_sec
)
3819 if (after_sec
== NULL
)
3821 look
= debug_sections
[debug_info
].uncompressed_name
;
3822 msec
= bfd_get_section_by_name (abfd
, look
);
3826 look
= debug_sections
[debug_info
].compressed_name
;
3829 msec
= bfd_get_section_by_name (abfd
, look
);
3834 for (msec
= abfd
->sections
; msec
!= NULL
; msec
= msec
->next
)
3835 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
3841 for (msec
= after_sec
->next
; msec
!= NULL
; msec
= msec
->next
)
3843 look
= debug_sections
[debug_info
].uncompressed_name
;
3844 if (strcmp (msec
->name
, look
) == 0)
3847 look
= debug_sections
[debug_info
].compressed_name
;
3848 if (look
!= NULL
&& strcmp (msec
->name
, look
) == 0)
3851 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
3858 /* Transfer VMAs from object file to separate debug file. */
3861 set_debug_vma (bfd
*orig_bfd
, bfd
*debug_bfd
)
3865 for (s
= orig_bfd
->sections
, d
= debug_bfd
->sections
;
3866 s
!= NULL
&& d
!= NULL
;
3867 s
= s
->next
, d
= d
->next
)
3869 if ((d
->flags
& SEC_DEBUGGING
) != 0)
3871 /* ??? Assumes 1-1 correspondence between sections in the
3873 if (strcmp (s
->name
, d
->name
) == 0)
3875 d
->output_section
= s
->output_section
;
3876 d
->output_offset
= s
->output_offset
;
3882 /* Unset vmas for adjusted sections in STASH. */
3885 unset_sections (struct dwarf2_debug
*stash
)
3888 struct adjusted_section
*p
;
3890 i
= stash
->adjusted_section_count
;
3891 p
= stash
->adjusted_sections
;
3892 for (; i
> 0; i
--, p
++)
3893 p
->section
->vma
= 0;
3896 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
3897 relocatable object file. VMAs are normally all zero in relocatable
3898 object files, so if we want to distinguish locations in sections by
3899 address we need to set VMAs so the sections do not overlap. We
3900 also set VMA on .debug_info so that when we have multiple
3901 .debug_info sections (or the linkonce variant) they also do not
3902 overlap. The multiple .debug_info sections make up a single
3903 logical section. ??? We should probably do the same for other
3907 place_sections (bfd
*orig_bfd
, struct dwarf2_debug
*stash
)
3910 struct adjusted_section
*p
;
3912 const char *debug_info_name
;
3914 if (stash
->adjusted_section_count
!= 0)
3916 i
= stash
->adjusted_section_count
;
3917 p
= stash
->adjusted_sections
;
3918 for (; i
> 0; i
--, p
++)
3919 p
->section
->vma
= p
->adj_vma
;
3923 debug_info_name
= stash
->debug_sections
[debug_info
].uncompressed_name
;
3930 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
3934 if ((sect
->output_section
!= NULL
3935 && sect
->output_section
!= sect
3936 && (sect
->flags
& SEC_DEBUGGING
) == 0)
3940 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
3941 || CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
));
3943 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
3949 if (abfd
== stash
->bfd_ptr
)
3951 abfd
= stash
->bfd_ptr
;
3955 stash
->adjusted_section_count
= -1;
3958 bfd_vma last_vma
= 0, last_dwarf
= 0;
3959 bfd_size_type amt
= i
* sizeof (struct adjusted_section
);
3961 p
= (struct adjusted_section
*) bfd_malloc (amt
);
3965 stash
->adjusted_sections
= p
;
3966 stash
->adjusted_section_count
= i
;
3973 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
3978 if ((sect
->output_section
!= NULL
3979 && sect
->output_section
!= sect
3980 && (sect
->flags
& SEC_DEBUGGING
) == 0)
3984 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
3985 || CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
));
3987 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
3991 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
3995 BFD_ASSERT (sect
->alignment_power
== 0);
3996 sect
->vma
= last_dwarf
;
4001 /* Align the new address to the current section
4003 last_vma
= ((last_vma
4004 + ~(-((bfd_vma
) 1 << sect
->alignment_power
)))
4005 & (-((bfd_vma
) 1 << sect
->alignment_power
)));
4006 sect
->vma
= last_vma
;
4011 p
->adj_vma
= sect
->vma
;
4014 if (abfd
== stash
->bfd_ptr
)
4016 abfd
= stash
->bfd_ptr
;
4020 if (orig_bfd
!= stash
->bfd_ptr
)
4021 set_debug_vma (orig_bfd
, stash
->bfd_ptr
);
4026 /* Look up a funcinfo by name using the given info hash table. If found,
4027 also update the locations pointed to by filename_ptr and linenumber_ptr.
4029 This function returns TRUE if a funcinfo that matches the given symbol
4030 and address is found with any error; otherwise it returns FALSE. */
4033 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
4036 const char **filename_ptr
,
4037 unsigned int *linenumber_ptr
)
4039 struct funcinfo
* each_func
;
4040 struct funcinfo
* best_fit
= NULL
;
4041 bfd_vma best_fit_len
= 0;
4042 struct info_list_node
*node
;
4043 struct arange
*arange
;
4044 const char *name
= bfd_asymbol_name (sym
);
4045 asection
*sec
= bfd_get_section (sym
);
4047 for (node
= lookup_info_hash_table (hash_table
, name
);
4051 each_func
= (struct funcinfo
*) node
->info
;
4052 for (arange
= &each_func
->arange
;
4054 arange
= arange
->next
)
4056 if ((!each_func
->sec
|| each_func
->sec
== sec
)
4057 && addr
>= arange
->low
4058 && addr
< arange
->high
4060 || arange
->high
- arange
->low
< best_fit_len
))
4062 best_fit
= each_func
;
4063 best_fit_len
= arange
->high
- arange
->low
;
4070 best_fit
->sec
= sec
;
4071 *filename_ptr
= best_fit
->file
;
4072 *linenumber_ptr
= best_fit
->line
;
4079 /* Look up a varinfo by name using the given info hash table. If found,
4080 also update the locations pointed to by filename_ptr and linenumber_ptr.
4082 This function returns TRUE if a varinfo that matches the given symbol
4083 and address is found with any error; otherwise it returns FALSE. */
4086 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
4089 const char **filename_ptr
,
4090 unsigned int *linenumber_ptr
)
4092 const char *name
= bfd_asymbol_name (sym
);
4093 asection
*sec
= bfd_get_section (sym
);
4094 struct varinfo
* each
;
4095 struct info_list_node
*node
;
4097 for (node
= lookup_info_hash_table (hash_table
, name
);
4101 each
= (struct varinfo
*) node
->info
;
4102 if (each
->addr
== addr
4103 && (!each
->sec
|| each
->sec
== sec
))
4106 *filename_ptr
= each
->file
;
4107 *linenumber_ptr
= each
->line
;
4115 /* Update the funcinfo and varinfo info hash tables if they are
4116 not up to date. Returns TRUE if there is no error; otherwise
4117 returns FALSE and disable the info hash tables. */
4120 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
4122 struct comp_unit
*each
;
4124 /* Exit if hash tables are up-to-date. */
4125 if (stash
->all_comp_units
== stash
->hash_units_head
)
4128 if (stash
->hash_units_head
)
4129 each
= stash
->hash_units_head
->prev_unit
;
4131 each
= stash
->last_comp_unit
;
4135 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
4136 stash
->varinfo_hash_table
))
4138 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
4141 each
= each
->prev_unit
;
4144 stash
->hash_units_head
= stash
->all_comp_units
;
4148 /* Check consistency of info hash tables. This is for debugging only. */
4150 static void ATTRIBUTE_UNUSED
4151 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
4153 struct comp_unit
*each_unit
;
4154 struct funcinfo
*each_func
;
4155 struct varinfo
*each_var
;
4156 struct info_list_node
*node
;
4159 for (each_unit
= stash
->all_comp_units
;
4161 each_unit
= each_unit
->next_unit
)
4163 for (each_func
= each_unit
->function_table
;
4165 each_func
= each_func
->prev_func
)
4167 if (!each_func
->name
)
4169 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
4173 while (node
&& !found
)
4175 found
= node
->info
== each_func
;
4181 for (each_var
= each_unit
->variable_table
;
4183 each_var
= each_var
->prev_var
)
4185 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
4187 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
4191 while (node
&& !found
)
4193 found
= node
->info
== each_var
;
4201 /* Check to see if we want to enable the info hash tables, which consume
4202 quite a bit of memory. Currently we only check the number times
4203 bfd_dwarf2_find_line is called. In the future, we may also want to
4204 take the number of symbols into account. */
4207 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
4209 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
4211 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
4214 /* FIXME: Maybe we should check the reduce_memory_overheads
4215 and optimize fields in the bfd_link_info structure ? */
4217 /* Create hash tables. */
4218 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
4219 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
4220 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
4222 /* Turn off info hashes if any allocation above fails. */
4223 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
4226 /* We need a forced update so that the info hash tables will
4227 be created even though there is no compilation unit. That
4228 happens if STASH_INFO_HASH_TRIGGER is 0. */
4229 stash_maybe_update_info_hash_tables (stash
);
4230 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
4233 /* Find the file and line associated with a symbol and address using the
4234 info hash tables of a stash. If there is a match, the function returns
4235 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
4236 otherwise it returns FALSE. */
4239 stash_find_line_fast (struct dwarf2_debug
*stash
,
4242 const char **filename_ptr
,
4243 unsigned int *linenumber_ptr
)
4245 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
4247 if (sym
->flags
& BSF_FUNCTION
)
4248 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
4249 filename_ptr
, linenumber_ptr
);
4250 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
4251 filename_ptr
, linenumber_ptr
);
4254 /* Save current section VMAs. */
4257 save_section_vma (const bfd
*abfd
, struct dwarf2_debug
*stash
)
4262 if (abfd
->section_count
== 0)
4264 stash
->sec_vma
= bfd_malloc (sizeof (*stash
->sec_vma
) * abfd
->section_count
);
4265 if (stash
->sec_vma
== NULL
)
4267 for (i
= 0, s
= abfd
->sections
; i
< abfd
->section_count
; i
++, s
= s
->next
)
4269 if (s
->output_section
!= NULL
)
4270 stash
->sec_vma
[i
] = s
->output_section
->vma
+ s
->output_offset
;
4272 stash
->sec_vma
[i
] = s
->vma
;
4277 /* Compare current section VMAs against those at the time the stash
4278 was created. If find_nearest_line is used in linker warnings or
4279 errors early in the link process, the debug info stash will be
4280 invalid for later calls. This is because we relocate debug info
4281 sections, so the stashed section contents depend on symbol values,
4282 which in turn depend on section VMAs. */
4285 section_vma_same (const bfd
*abfd
, const struct dwarf2_debug
*stash
)
4290 for (i
= 0, s
= abfd
->sections
; i
< abfd
->section_count
; i
++, s
= s
->next
)
4294 if (s
->output_section
!= NULL
)
4295 vma
= s
->output_section
->vma
+ s
->output_offset
;
4298 if (vma
!= stash
->sec_vma
[i
])
4304 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
4305 If DEBUG_BFD is not specified, we read debug information from ABFD
4306 or its gnu_debuglink. The results will be stored in PINFO.
4307 The function returns TRUE iff debug information is ready. */
4310 _bfd_dwarf2_slurp_debug_info (bfd
*abfd
, bfd
*debug_bfd
,
4311 const struct dwarf_debug_section
*debug_sections
,
4314 bfd_boolean do_place
)
4316 bfd_size_type amt
= sizeof (struct dwarf2_debug
);
4317 bfd_size_type total_size
;
4319 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
4323 if (stash
->orig_bfd
== abfd
4324 && section_vma_same (abfd
, stash
))
4326 /* Check that we did previously find some debug information
4327 before attempting to make use of it. */
4328 if (stash
->bfd_ptr
!= NULL
)
4330 if (do_place
&& !place_sections (abfd
, stash
))
4337 _bfd_dwarf2_cleanup_debug_info (abfd
, pinfo
);
4338 memset (stash
, 0, amt
);
4342 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
4346 stash
->orig_bfd
= abfd
;
4347 stash
->debug_sections
= debug_sections
;
4348 stash
->syms
= symbols
;
4349 if (!save_section_vma (abfd
, stash
))
4354 if (debug_bfd
== NULL
)
4357 msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4358 if (msec
== NULL
&& abfd
== debug_bfd
)
4360 char * debug_filename
;
4362 debug_filename
= bfd_follow_build_id_debuglink (abfd
, DEBUGDIR
);
4363 if (debug_filename
== NULL
)
4364 debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
4366 if (debug_filename
== NULL
)
4367 /* No dwarf2 info, and no gnu_debuglink to follow.
4368 Note that at this point the stash has been allocated, but
4369 contains zeros. This lets future calls to this function
4370 fail more quickly. */
4373 /* Set BFD_DECOMPRESS to decompress debug sections. */
4374 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
4375 || !(debug_bfd
->flags
|= BFD_DECOMPRESS
,
4376 bfd_check_format (debug_bfd
, bfd_object
))
4377 || (msec
= find_debug_info (debug_bfd
,
4378 debug_sections
, NULL
)) == NULL
4379 || !bfd_generic_link_read_symbols (debug_bfd
))
4382 bfd_close (debug_bfd
);
4383 /* FIXME: Should we report our failure to follow the debuglink ? */
4384 free (debug_filename
);
4388 symbols
= bfd_get_outsymbols (debug_bfd
);
4389 stash
->syms
= symbols
;
4390 stash
->close_on_cleanup
= TRUE
;
4392 stash
->bfd_ptr
= debug_bfd
;
4395 && !place_sections (abfd
, stash
))
4398 /* There can be more than one DWARF2 info section in a BFD these
4399 days. First handle the easy case when there's only one. If
4400 there's more than one, try case two: none of the sections is
4401 compressed. In that case, read them all in and produce one
4402 large stash. We do this in two passes - in the first pass we
4403 just accumulate the section sizes, and in the second pass we
4404 read in the section's contents. (The allows us to avoid
4405 reallocing the data as we add sections to the stash.) If
4406 some or all sections are compressed, then do things the slow
4407 way, with a bunch of reallocs. */
4409 if (! find_debug_info (debug_bfd
, debug_sections
, msec
))
4411 /* Case 1: only one info section. */
4412 total_size
= msec
->size
;
4413 if (! read_section (debug_bfd
, &stash
->debug_sections
[debug_info
],
4415 &stash
->info_ptr_memory
, &total_size
))
4420 /* Case 2: multiple sections. */
4421 for (total_size
= 0;
4423 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
4424 total_size
+= msec
->size
;
4426 stash
->info_ptr_memory
= (bfd_byte
*) bfd_malloc (total_size
);
4427 if (stash
->info_ptr_memory
== NULL
)
4431 for (msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4433 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
4441 if (!(bfd_simple_get_relocated_section_contents
4442 (debug_bfd
, msec
, stash
->info_ptr_memory
+ total_size
,
4450 stash
->info_ptr
= stash
->info_ptr_memory
;
4451 stash
->info_ptr_end
= stash
->info_ptr
+ total_size
;
4452 stash
->sec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4453 stash
->sec_info_ptr
= stash
->info_ptr
;
4457 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
4458 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
4459 symbol in SYMBOLS and return the difference between the low_pc and
4460 the symbol's address. Returns 0 if no suitable symbol could be found. */
4463 _bfd_dwarf2_find_symbol_bias (asymbol
** symbols
, void ** pinfo
)
4465 struct dwarf2_debug
*stash
;
4466 struct comp_unit
* unit
;
4468 stash
= (struct dwarf2_debug
*) *pinfo
;
4473 for (unit
= stash
->all_comp_units
; unit
; unit
= unit
->next_unit
)
4475 struct funcinfo
* func
;
4477 if (unit
->function_table
== NULL
)
4479 if (unit
->line_table
== NULL
)
4480 unit
->line_table
= decode_line_info (unit
, stash
);
4481 if (unit
->line_table
!= NULL
)
4482 scan_unit_for_symbols (unit
);
4485 for (func
= unit
->function_table
; func
!= NULL
; func
= func
->prev_func
)
4486 if (func
->name
&& func
->arange
.low
)
4490 /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
4492 for (psym
= symbols
; * psym
!= NULL
; psym
++)
4494 asymbol
* sym
= * psym
;
4496 if (sym
->flags
& BSF_FUNCTION
4497 && sym
->section
!= NULL
4498 && strcmp (sym
->name
, func
->name
) == 0)
4499 return ((bfd_signed_vma
) func
->arange
.low
) -
4500 ((bfd_signed_vma
) (sym
->value
+ sym
->section
->vma
));
4508 /* Find the source code location of SYMBOL. If SYMBOL is NULL
4509 then find the nearest source code location corresponding to
4510 the address SECTION + OFFSET.
4511 Returns TRUE if the line is found without error and fills in
4512 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
4513 NULL the FUNCTIONNAME_PTR is also filled in.
4514 SYMBOLS contains the symbol table for ABFD.
4515 DEBUG_SECTIONS contains the name of the dwarf debug sections.
4516 ADDR_SIZE is the number of bytes in the initial .debug_info length
4517 field and in the abbreviation offset, or zero to indicate that the
4518 default value should be used. */
4521 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
4526 const char **filename_ptr
,
4527 const char **functionname_ptr
,
4528 unsigned int *linenumber_ptr
,
4529 unsigned int *discriminator_ptr
,
4530 const struct dwarf_debug_section
*debug_sections
,
4531 unsigned int addr_size
,
4534 /* Read each compilation unit from the section .debug_info, and check
4535 to see if it contains the address we are searching for. If yes,
4536 lookup the address, and return the line number info. If no, go
4537 on to the next compilation unit.
4539 We keep a list of all the previously read compilation units, and
4540 a pointer to the next un-read compilation unit. Check the
4541 previously read units before reading more. */
4542 struct dwarf2_debug
*stash
;
4543 /* What address are we looking for? */
4545 struct comp_unit
* each
;
4546 struct funcinfo
*function
= NULL
;
4547 bfd_boolean found
= FALSE
;
4548 bfd_boolean do_line
;
4550 *filename_ptr
= NULL
;
4551 if (functionname_ptr
!= NULL
)
4552 *functionname_ptr
= NULL
;
4553 *linenumber_ptr
= 0;
4554 if (discriminator_ptr
)
4555 *discriminator_ptr
= 0;
4557 if (! _bfd_dwarf2_slurp_debug_info (abfd
, NULL
, debug_sections
,
4559 (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0))
4562 stash
= (struct dwarf2_debug
*) *pinfo
;
4564 do_line
= symbol
!= NULL
;
4567 BFD_ASSERT (section
== NULL
&& offset
== 0 && functionname_ptr
== NULL
);
4568 section
= bfd_get_section (symbol
);
4569 addr
= symbol
->value
;
4573 BFD_ASSERT (section
!= NULL
&& functionname_ptr
!= NULL
);
4576 /* If we have no SYMBOL but the section we're looking at is not a
4577 code section, then take a look through the list of symbols to see
4578 if we have a symbol at the address we're looking for. If we do
4579 then use this to look up line information. This will allow us to
4580 give file and line results for data symbols. We exclude code
4581 symbols here, if we look up a function symbol and then look up the
4582 line information we'll actually return the line number for the
4583 opening '{' rather than the function definition line. This is
4584 because looking up by symbol uses the line table, in which the
4585 first line for a function is usually the opening '{', while
4586 looking up the function by section + offset uses the
4587 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
4588 which will be the line of the function name. */
4589 if (symbols
!= NULL
&& (section
->flags
& SEC_CODE
) == 0)
4593 for (tmp
= symbols
; (*tmp
) != NULL
; ++tmp
)
4594 if ((*tmp
)->the_bfd
== abfd
4595 && (*tmp
)->section
== section
4596 && (*tmp
)->value
== offset
4597 && ((*tmp
)->flags
& BSF_SECTION_SYM
) == 0)
4601 /* For local symbols, keep going in the hope we find a
4603 if ((symbol
->flags
& BSF_GLOBAL
) != 0)
4609 if (section
->output_section
)
4610 addr
+= section
->output_section
->vma
+ section
->output_offset
;
4612 addr
+= section
->vma
;
4614 /* A null info_ptr indicates that there is no dwarf2 info
4615 (or that an error occured while setting up the stash). */
4616 if (! stash
->info_ptr
)
4619 stash
->inliner_chain
= NULL
;
4621 /* Check the previously read comp. units first. */
4624 /* The info hash tables use quite a bit of memory. We may not want to
4625 always use them. We use some heuristics to decide if and when to
4627 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
4628 stash_maybe_enable_info_hash_tables (abfd
, stash
);
4630 /* Keep info hash table up to date if they are available. Note that we
4631 may disable the hash tables if there is any error duing update. */
4632 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
4633 stash_maybe_update_info_hash_tables (stash
);
4635 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
4637 found
= stash_find_line_fast (stash
, symbol
, addr
, filename_ptr
,
4644 /* Check the previously read comp. units first. */
4645 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
4646 if ((symbol
->flags
& BSF_FUNCTION
) == 0
4647 || each
->arange
.high
== 0
4648 || comp_unit_contains_address (each
, addr
))
4650 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
4651 linenumber_ptr
, stash
);
4659 bfd_vma min_range
= (bfd_vma
) -1;
4660 const char * local_filename
= NULL
;
4661 struct funcinfo
*local_function
= NULL
;
4662 unsigned int local_linenumber
= 0;
4663 unsigned int local_discriminator
= 0;
4665 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
4667 bfd_vma range
= (bfd_vma
) -1;
4669 found
= ((each
->arange
.high
== 0
4670 || comp_unit_contains_address (each
, addr
))
4671 && (range
= comp_unit_find_nearest_line (each
, addr
,
4675 & local_discriminator
,
4679 /* PRs 15935 15994: Bogus debug information may have provided us
4680 with an erroneous match. We attempt to counter this by
4681 selecting the match that has the smallest address range
4682 associated with it. (We are assuming that corrupt debug info
4683 will tend to result in extra large address ranges rather than
4684 extra small ranges).
4686 This does mean that we scan through all of the CUs associated
4687 with the bfd each time this function is called. But this does
4688 have the benefit of producing consistent results every time the
4689 function is called. */
4690 if (range
<= min_range
)
4692 if (filename_ptr
&& local_filename
)
4693 * filename_ptr
= local_filename
;
4695 function
= local_function
;
4696 if (discriminator_ptr
&& local_discriminator
)
4697 * discriminator_ptr
= local_discriminator
;
4698 if (local_linenumber
)
4699 * linenumber_ptr
= local_linenumber
;
4705 if (* linenumber_ptr
)
4712 /* The DWARF2 spec says that the initial length field, and the
4713 offset of the abbreviation table, should both be 4-byte values.
4714 However, some compilers do things differently. */
4717 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
4719 /* Read each remaining comp. units checking each as they are read. */
4720 while (stash
->info_ptr
< stash
->info_ptr_end
)
4723 unsigned int offset_size
= addr_size
;
4724 bfd_byte
*info_ptr_unit
= stash
->info_ptr
;
4726 length
= read_4_bytes (stash
->bfd_ptr
, stash
->info_ptr
, stash
->info_ptr_end
);
4727 /* A 0xffffff length is the DWARF3 way of indicating
4728 we use 64-bit offsets, instead of 32-bit offsets. */
4729 if (length
== 0xffffffff)
4732 length
= read_8_bytes (stash
->bfd_ptr
, stash
->info_ptr
+ 4, stash
->info_ptr_end
);
4733 stash
->info_ptr
+= 12;
4735 /* A zero length is the IRIX way of indicating 64-bit offsets,
4736 mostly because the 64-bit length will generally fit in 32
4737 bits, and the endianness helps. */
4738 else if (length
== 0)
4741 length
= read_4_bytes (stash
->bfd_ptr
, stash
->info_ptr
+ 4, stash
->info_ptr_end
);
4742 stash
->info_ptr
+= 8;
4744 /* In the absence of the hints above, we assume 32-bit DWARF2
4745 offsets even for targets with 64-bit addresses, because:
4746 a) most of the time these targets will not have generated
4747 more than 2Gb of debug info and so will not need 64-bit
4750 b) if they do use 64-bit offsets but they are not using
4751 the size hints that are tested for above then they are
4752 not conforming to the DWARF3 standard anyway. */
4753 else if (addr_size
== 8)
4756 stash
->info_ptr
+= 4;
4759 stash
->info_ptr
+= 4;
4766 if (stash
->info_ptr
+ length
> stash
->info_ptr_end
)
4769 each
= parse_comp_unit (stash
, length
, info_ptr_unit
,
4772 /* The dwarf information is damaged, don't trust it any
4776 new_ptr
= stash
->info_ptr
+ length
;
4777 /* PR 17512: file: 1500698c. */
4778 if (new_ptr
< stash
->info_ptr
)
4780 /* A corrupt length value - do not trust the info any more. */
4785 stash
->info_ptr
= new_ptr
;
4787 if (stash
->all_comp_units
)
4788 stash
->all_comp_units
->prev_unit
= each
;
4790 stash
->last_comp_unit
= each
;
4792 each
->next_unit
= stash
->all_comp_units
;
4793 stash
->all_comp_units
= each
;
4795 /* DW_AT_low_pc and DW_AT_high_pc are optional for
4796 compilation units. If we don't have them (i.e.,
4797 unit->high == 0), we need to consult the line info table
4798 to see if a compilation unit contains the given
4801 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
4802 || each
->arange
.high
== 0
4803 || comp_unit_contains_address (each
, addr
))
4804 && comp_unit_find_line (each
, symbol
, addr
,
4809 found
= ((each
->arange
.high
== 0
4810 || comp_unit_contains_address (each
, addr
))
4811 && comp_unit_find_nearest_line (each
, addr
,
4818 if ((bfd_vma
) (stash
->info_ptr
- stash
->sec_info_ptr
)
4819 == stash
->sec
->size
)
4821 stash
->sec
= find_debug_info (stash
->bfd_ptr
, debug_sections
,
4823 stash
->sec_info_ptr
= stash
->info_ptr
;
4834 if (!function
->is_linkage
)
4839 fun
= _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
4840 *filename_ptr
? NULL
: filename_ptr
,
4842 sec_vma
= section
->vma
;
4843 if (section
->output_section
!= NULL
)
4844 sec_vma
= section
->output_section
->vma
+ section
->output_offset
;
4846 && fun
->value
+ sec_vma
== function
->arange
.low
)
4847 function
->name
= *functionname_ptr
;
4848 /* Even if we didn't find a linkage name, say that we have
4849 to stop a repeated search of symbols. */
4850 function
->is_linkage
= TRUE
;
4852 *functionname_ptr
= function
->name
;
4854 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
4855 unset_sections (stash
);
4861 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
4862 const char **filename_ptr
,
4863 const char **functionname_ptr
,
4864 unsigned int *linenumber_ptr
,
4867 struct dwarf2_debug
*stash
;
4869 stash
= (struct dwarf2_debug
*) *pinfo
;
4872 struct funcinfo
*func
= stash
->inliner_chain
;
4874 if (func
&& func
->caller_func
)
4876 *filename_ptr
= func
->caller_file
;
4877 *functionname_ptr
= func
->caller_func
->name
;
4878 *linenumber_ptr
= func
->caller_line
;
4879 stash
->inliner_chain
= func
->caller_func
;
4888 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
, void **pinfo
)
4890 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
4891 struct comp_unit
*each
;
4893 if (abfd
== NULL
|| stash
== NULL
)
4896 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
4898 struct abbrev_info
**abbrevs
= each
->abbrevs
;
4899 struct funcinfo
*function_table
= each
->function_table
;
4900 struct varinfo
*variable_table
= each
->variable_table
;
4903 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
4905 struct abbrev_info
*abbrev
= abbrevs
[i
];
4909 free (abbrev
->attrs
);
4910 abbrev
= abbrev
->next
;
4914 if (each
->line_table
)
4916 free (each
->line_table
->dirs
);
4917 free (each
->line_table
->files
);
4920 while (function_table
)
4922 if (function_table
->file
)
4924 free (function_table
->file
);
4925 function_table
->file
= NULL
;
4928 if (function_table
->caller_file
)
4930 free (function_table
->caller_file
);
4931 function_table
->caller_file
= NULL
;
4933 function_table
= function_table
->prev_func
;
4936 if (each
->lookup_funcinfo_table
)
4938 free (each
->lookup_funcinfo_table
);
4939 each
->lookup_funcinfo_table
= NULL
;
4942 while (variable_table
)
4944 if (variable_table
->file
)
4946 free (variable_table
->file
);
4947 variable_table
->file
= NULL
;
4950 variable_table
= variable_table
->prev_var
;
4954 if (stash
->dwarf_abbrev_buffer
)
4955 free (stash
->dwarf_abbrev_buffer
);
4956 if (stash
->dwarf_line_buffer
)
4957 free (stash
->dwarf_line_buffer
);
4958 if (stash
->dwarf_str_buffer
)
4959 free (stash
->dwarf_str_buffer
);
4960 if (stash
->dwarf_line_str_buffer
)
4961 free (stash
->dwarf_line_str_buffer
);
4962 if (stash
->dwarf_ranges_buffer
)
4963 free (stash
->dwarf_ranges_buffer
);
4964 if (stash
->info_ptr_memory
)
4965 free (stash
->info_ptr_memory
);
4966 if (stash
->close_on_cleanup
)
4967 bfd_close (stash
->bfd_ptr
);
4968 if (stash
->alt_dwarf_str_buffer
)
4969 free (stash
->alt_dwarf_str_buffer
);
4970 if (stash
->alt_dwarf_info_buffer
)
4971 free (stash
->alt_dwarf_info_buffer
);
4973 free (stash
->sec_vma
);
4974 if (stash
->adjusted_sections
)
4975 free (stash
->adjusted_sections
);
4976 if (stash
->alt_bfd_ptr
)
4977 bfd_close (stash
->alt_bfd_ptr
);
4980 /* Find the function to a particular section and offset,
4981 for error reporting. */
4984 _bfd_elf_find_function (bfd
*abfd
,
4988 const char **filename_ptr
,
4989 const char **functionname_ptr
)
4991 struct elf_find_function_cache
4993 asection
*last_section
;
4995 const char *filename
;
4996 bfd_size_type func_size
;
4999 if (symbols
== NULL
)
5002 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
5005 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
5008 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
5009 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
5013 if (cache
->last_section
!= section
5014 || cache
->func
== NULL
5015 || offset
< cache
->func
->value
5016 || offset
>= cache
->func
->value
+ cache
->func_size
)
5021 /* ??? Given multiple file symbols, it is impossible to reliably
5022 choose the right file name for global symbols. File symbols are
5023 local symbols, and thus all file symbols must sort before any
5024 global symbols. The ELF spec may be interpreted to say that a
5025 file symbol must sort before other local symbols, but currently
5026 ld -r doesn't do this. So, for ld -r output, it is possible to
5027 make a better choice of file name for local symbols by ignoring
5028 file symbols appearing after a given local symbol. */
5029 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
5030 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5034 state
= nothing_seen
;
5035 cache
->filename
= NULL
;
5037 cache
->func_size
= 0;
5038 cache
->last_section
= section
;
5040 for (p
= symbols
; *p
!= NULL
; p
++)
5046 if ((sym
->flags
& BSF_FILE
) != 0)
5049 if (state
== symbol_seen
)
5050 state
= file_after_symbol_seen
;
5054 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
5056 && code_off
<= offset
5057 && (code_off
> low_func
5058 || (code_off
== low_func
5059 && size
> cache
->func_size
)))
5062 cache
->func_size
= size
;
5063 cache
->filename
= NULL
;
5064 low_func
= code_off
;
5066 && ((sym
->flags
& BSF_LOCAL
) != 0
5067 || state
!= file_after_symbol_seen
))
5068 cache
->filename
= bfd_asymbol_name (file
);
5070 if (state
== nothing_seen
)
5071 state
= symbol_seen
;
5075 if (cache
->func
== NULL
)
5079 *filename_ptr
= cache
->filename
;
5080 if (functionname_ptr
)
5081 *functionname_ptr
= bfd_asymbol_name (cache
->func
);