2 Copyright (C) 1994-2018 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
;
529 bfd_byte
*contents
= *section_buffer
;
531 /* The section may have already been read. */
532 if (contents
== NULL
)
534 msec
= bfd_get_section_by_name (abfd
, section_name
);
537 section_name
= sec
->compressed_name
;
538 if (section_name
!= NULL
)
539 msec
= bfd_get_section_by_name (abfd
, section_name
);
543 _bfd_error_handler (_("DWARF error: can't find %s section."),
544 sec
->uncompressed_name
);
545 bfd_set_error (bfd_error_bad_value
);
549 *section_size
= msec
->rawsize
? msec
->rawsize
: msec
->size
;
550 /* Paranoia - alloc one extra so that we can make sure a string
551 section is NUL terminated. */
552 contents
= (bfd_byte
*) bfd_malloc (*section_size
+ 1);
553 if (contents
== NULL
)
556 ? !bfd_simple_get_relocated_section_contents (abfd
, msec
, contents
,
558 : !bfd_get_section_contents (abfd
, msec
, contents
, 0, *section_size
))
563 contents
[*section_size
] = 0;
564 *section_buffer
= contents
;
567 /* It is possible to get a bad value for the offset into the section
568 that the client wants. Validate it here to avoid trouble later. */
569 if (offset
!= 0 && offset
>= *section_size
)
571 /* xgettext: c-format */
572 _bfd_error_handler (_("DWARF error: offset (%" PRIu64
")"
573 " greater than or equal to %s size (%" PRIu64
")"),
574 (uint64_t) offset
, section_name
,
575 (uint64_t) *section_size
);
576 bfd_set_error (bfd_error_bad_value
);
583 /* Read dwarf information from a buffer. */
586 read_1_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
, bfd_byte
*end
)
590 return bfd_get_8 (abfd
, buf
);
594 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
, bfd_byte
*end
)
598 return bfd_get_signed_8 (abfd
, buf
);
602 read_2_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
606 return bfd_get_16 (abfd
, buf
);
610 read_4_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
614 return bfd_get_32 (abfd
, buf
);
618 read_8_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
622 return bfd_get_64 (abfd
, buf
);
626 read_n_bytes (bfd_byte
* buf
,
628 struct dwarf_block
* block
)
630 unsigned int size
= block
->size
;
631 bfd_byte
* block_end
= buf
+ size
;
633 if (block_end
> end
|| block_end
< buf
)
646 /* Scans a NUL terminated string starting at BUF, returning a pointer to it.
647 Returns the number of characters in the string, *including* the NUL byte,
648 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
649 at or beyond BUF_END will not be read. Returns NULL if there was a
650 problem, or if the string is empty. */
653 read_string (bfd
* abfd ATTRIBUTE_UNUSED
,
656 unsigned int * bytes_read_ptr
)
662 * bytes_read_ptr
= 0;
668 * bytes_read_ptr
= 1;
672 while (buf
< buf_end
)
675 * bytes_read_ptr
= buf
- str
;
679 * bytes_read_ptr
= buf
- str
;
683 /* Reads an offset from BUF and then locates the string at this offset
684 inside the debug string section. Returns a pointer to the string.
685 Returns the number of bytes read from BUF, *not* the length of the string,
686 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
687 at or beyond BUF_END will not be read from BUF. Returns NULL if there was
688 a problem, or if the string is empty. Does not check for NUL termination
692 read_indirect_string (struct comp_unit
* unit
,
695 unsigned int * bytes_read_ptr
)
698 struct dwarf2_debug
*stash
= unit
->stash
;
701 if (buf
+ unit
->offset_size
> buf_end
)
703 * bytes_read_ptr
= 0;
707 if (unit
->offset_size
== 4)
708 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
710 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
712 *bytes_read_ptr
= unit
->offset_size
;
714 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
716 &stash
->dwarf_str_buffer
, &stash
->dwarf_str_size
))
719 if (offset
>= stash
->dwarf_str_size
)
721 str
= (char *) stash
->dwarf_str_buffer
+ offset
;
727 /* Like read_indirect_string but from .debug_line_str section. */
730 read_indirect_line_string (struct comp_unit
* unit
,
733 unsigned int * bytes_read_ptr
)
736 struct dwarf2_debug
*stash
= unit
->stash
;
739 if (buf
+ unit
->offset_size
> buf_end
)
741 * bytes_read_ptr
= 0;
745 if (unit
->offset_size
== 4)
746 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
748 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
750 *bytes_read_ptr
= unit
->offset_size
;
752 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_line_str
],
754 &stash
->dwarf_line_str_buffer
,
755 &stash
->dwarf_line_str_size
))
758 if (offset
>= stash
->dwarf_line_str_size
)
760 str
= (char *) stash
->dwarf_line_str_buffer
+ offset
;
766 /* Like read_indirect_string but uses a .debug_str located in
767 an alternate file pointed to by the .gnu_debugaltlink section.
768 Used to impement DW_FORM_GNU_strp_alt. */
771 read_alt_indirect_string (struct comp_unit
* unit
,
774 unsigned int * bytes_read_ptr
)
777 struct dwarf2_debug
*stash
= unit
->stash
;
780 if (buf
+ unit
->offset_size
> buf_end
)
782 * bytes_read_ptr
= 0;
786 if (unit
->offset_size
== 4)
787 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
789 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
791 *bytes_read_ptr
= unit
->offset_size
;
793 if (stash
->alt_bfd_ptr
== NULL
)
796 char * debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
798 if (debug_filename
== NULL
)
801 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
802 || ! bfd_check_format (debug_bfd
, bfd_object
))
805 bfd_close (debug_bfd
);
807 /* FIXME: Should we report our failure to follow the debuglink ? */
808 free (debug_filename
);
811 stash
->alt_bfd_ptr
= debug_bfd
;
814 if (! read_section (unit
->stash
->alt_bfd_ptr
,
815 stash
->debug_sections
+ debug_str_alt
,
816 NULL
, /* FIXME: Do we need to load alternate symbols ? */
818 &stash
->alt_dwarf_str_buffer
,
819 &stash
->alt_dwarf_str_size
))
822 if (offset
>= stash
->alt_dwarf_str_size
)
824 str
= (char *) stash
->alt_dwarf_str_buffer
+ offset
;
831 /* Resolve an alternate reference from UNIT at OFFSET.
832 Returns a pointer into the loaded alternate CU upon success
833 or NULL upon failure. */
836 read_alt_indirect_ref (struct comp_unit
* unit
,
839 struct dwarf2_debug
*stash
= unit
->stash
;
841 if (stash
->alt_bfd_ptr
== NULL
)
844 char * debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
846 if (debug_filename
== NULL
)
849 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
850 || ! bfd_check_format (debug_bfd
, bfd_object
))
853 bfd_close (debug_bfd
);
855 /* FIXME: Should we report our failure to follow the debuglink ? */
856 free (debug_filename
);
859 stash
->alt_bfd_ptr
= debug_bfd
;
862 if (! read_section (unit
->stash
->alt_bfd_ptr
,
863 stash
->debug_sections
+ debug_info_alt
,
864 NULL
, /* FIXME: Do we need to load alternate symbols ? */
866 &stash
->alt_dwarf_info_buffer
,
867 &stash
->alt_dwarf_info_size
))
870 if (offset
>= stash
->alt_dwarf_info_size
)
872 return stash
->alt_dwarf_info_buffer
+ offset
;
876 read_address (struct comp_unit
*unit
, bfd_byte
*buf
, bfd_byte
* buf_end
)
880 if (bfd_get_flavour (unit
->abfd
) == bfd_target_elf_flavour
)
881 signed_vma
= get_elf_backend_data (unit
->abfd
)->sign_extend_vma
;
883 if (buf
+ unit
->addr_size
> buf_end
)
888 switch (unit
->addr_size
)
891 return bfd_get_signed_64 (unit
->abfd
, buf
);
893 return bfd_get_signed_32 (unit
->abfd
, buf
);
895 return bfd_get_signed_16 (unit
->abfd
, buf
);
902 switch (unit
->addr_size
)
905 return bfd_get_64 (unit
->abfd
, buf
);
907 return bfd_get_32 (unit
->abfd
, buf
);
909 return bfd_get_16 (unit
->abfd
, buf
);
916 /* Lookup an abbrev_info structure in the abbrev hash table. */
918 static struct abbrev_info
*
919 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
921 unsigned int hash_number
;
922 struct abbrev_info
*abbrev
;
924 hash_number
= number
% ABBREV_HASH_SIZE
;
925 abbrev
= abbrevs
[hash_number
];
929 if (abbrev
->number
== number
)
932 abbrev
= abbrev
->next
;
938 /* In DWARF version 2, the description of the debugging information is
939 stored in a separate .debug_abbrev section. Before we read any
940 dies from a section we read in all abbreviations and install them
943 static struct abbrev_info
**
944 read_abbrevs (bfd
*abfd
, bfd_uint64_t offset
, struct dwarf2_debug
*stash
)
946 struct abbrev_info
**abbrevs
;
947 bfd_byte
*abbrev_ptr
;
948 bfd_byte
*abbrev_end
;
949 struct abbrev_info
*cur_abbrev
;
950 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
951 unsigned int abbrev_form
, hash_number
;
954 if (! read_section (abfd
, &stash
->debug_sections
[debug_abbrev
],
956 &stash
->dwarf_abbrev_buffer
, &stash
->dwarf_abbrev_size
))
959 if (offset
>= stash
->dwarf_abbrev_size
)
962 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
963 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
967 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
968 abbrev_end
= stash
->dwarf_abbrev_buffer
+ stash
->dwarf_abbrev_size
;
969 abbrev_number
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
971 abbrev_ptr
+= bytes_read
;
973 /* Loop until we reach an abbrev number of 0. */
974 while (abbrev_number
)
976 amt
= sizeof (struct abbrev_info
);
977 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
978 if (cur_abbrev
== NULL
)
981 /* Read in abbrev header. */
982 cur_abbrev
->number
= abbrev_number
;
983 cur_abbrev
->tag
= (enum dwarf_tag
)
984 _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
986 abbrev_ptr
+= bytes_read
;
987 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
, abbrev_end
);
990 /* Now read in declarations. */
993 /* Initialize it just to avoid a GCC false warning. */
994 bfd_vma implicit_const
= -1;
996 abbrev_name
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
998 abbrev_ptr
+= bytes_read
;
999 abbrev_form
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1001 abbrev_ptr
+= bytes_read
;
1002 if (abbrev_form
== DW_FORM_implicit_const
)
1004 implicit_const
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
,
1007 abbrev_ptr
+= bytes_read
;
1010 if (abbrev_name
== 0)
1013 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
1015 struct attr_abbrev
*tmp
;
1017 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
1018 amt
*= sizeof (struct attr_abbrev
);
1019 tmp
= (struct attr_abbrev
*) bfd_realloc (cur_abbrev
->attrs
, amt
);
1024 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1026 struct abbrev_info
*abbrev
= abbrevs
[i
];
1030 free (abbrev
->attrs
);
1031 abbrev
= abbrev
->next
;
1036 cur_abbrev
->attrs
= tmp
;
1039 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
1040 = (enum dwarf_attribute
) abbrev_name
;
1041 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].form
1042 = (enum dwarf_form
) abbrev_form
;
1043 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].implicit_const
1045 ++cur_abbrev
->num_attrs
;
1048 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
1049 cur_abbrev
->next
= abbrevs
[hash_number
];
1050 abbrevs
[hash_number
] = cur_abbrev
;
1052 /* Get next abbreviation.
1053 Under Irix6 the abbreviations for a compilation unit are not
1054 always properly terminated with an abbrev number of 0.
1055 Exit loop if we encounter an abbreviation which we have
1056 already read (which means we are about to read the abbreviations
1057 for the next compile unit) or if the end of the abbreviation
1058 table is reached. */
1059 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
1060 >= stash
->dwarf_abbrev_size
)
1062 abbrev_number
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
,
1063 &bytes_read
, FALSE
, abbrev_end
);
1064 abbrev_ptr
+= bytes_read
;
1065 if (lookup_abbrev (abbrev_number
, abbrevs
) != NULL
)
1072 /* Returns true if the form is one which has a string value. */
1074 static inline bfd_boolean
1075 is_str_attr (enum dwarf_form form
)
1077 return (form
== DW_FORM_string
|| form
== DW_FORM_strp
1078 || form
== DW_FORM_line_strp
|| form
== DW_FORM_GNU_strp_alt
);
1081 /* Read and fill in the value of attribute ATTR as described by FORM.
1082 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1083 Returns an updated INFO_PTR taking into account the amount of data read. */
1086 read_attribute_value (struct attribute
* attr
,
1088 bfd_vma implicit_const
,
1089 struct comp_unit
* unit
,
1090 bfd_byte
* info_ptr
,
1091 bfd_byte
* info_ptr_end
)
1093 bfd
*abfd
= unit
->abfd
;
1094 unsigned int bytes_read
;
1095 struct dwarf_block
*blk
;
1098 if (info_ptr
>= info_ptr_end
&& form
!= DW_FORM_flag_present
)
1100 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1101 bfd_set_error (bfd_error_bad_value
);
1105 attr
->form
= (enum dwarf_form
) form
;
1109 case DW_FORM_ref_addr
:
1110 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1112 if (unit
->version
== 3 || unit
->version
== 4)
1114 if (unit
->offset_size
== 4)
1115 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1117 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1118 info_ptr
+= unit
->offset_size
;
1123 attr
->u
.val
= read_address (unit
, info_ptr
, info_ptr_end
);
1124 info_ptr
+= unit
->addr_size
;
1126 case DW_FORM_GNU_ref_alt
:
1127 case DW_FORM_sec_offset
:
1128 if (unit
->offset_size
== 4)
1129 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1131 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1132 info_ptr
+= unit
->offset_size
;
1134 case DW_FORM_block2
:
1135 amt
= sizeof (struct dwarf_block
);
1136 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1139 blk
->size
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1141 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1144 case DW_FORM_block4
:
1145 amt
= sizeof (struct dwarf_block
);
1146 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1149 blk
->size
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1151 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1155 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1159 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1163 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1166 case DW_FORM_string
:
1167 attr
->u
.str
= read_string (abfd
, info_ptr
, info_ptr_end
, &bytes_read
);
1168 info_ptr
+= bytes_read
;
1171 attr
->u
.str
= read_indirect_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1172 info_ptr
+= bytes_read
;
1174 case DW_FORM_line_strp
:
1175 attr
->u
.str
= read_indirect_line_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1176 info_ptr
+= bytes_read
;
1178 case DW_FORM_GNU_strp_alt
:
1179 attr
->u
.str
= read_alt_indirect_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1180 info_ptr
+= bytes_read
;
1182 case DW_FORM_exprloc
:
1184 amt
= sizeof (struct dwarf_block
);
1185 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1188 blk
->size
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1189 FALSE
, info_ptr_end
);
1190 info_ptr
+= bytes_read
;
1191 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1194 case DW_FORM_block1
:
1195 amt
= sizeof (struct dwarf_block
);
1196 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1199 blk
->size
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1201 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1205 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1209 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1212 case DW_FORM_flag_present
:
1216 attr
->u
.sval
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1217 TRUE
, info_ptr_end
);
1218 info_ptr
+= bytes_read
;
1221 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1222 FALSE
, info_ptr_end
);
1223 info_ptr
+= bytes_read
;
1226 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1230 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1234 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1238 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1241 case DW_FORM_ref_sig8
:
1242 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1245 case DW_FORM_ref_udata
:
1246 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1247 FALSE
, info_ptr_end
);
1248 info_ptr
+= bytes_read
;
1250 case DW_FORM_indirect
:
1251 form
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1252 FALSE
, info_ptr_end
);
1253 info_ptr
+= bytes_read
;
1254 if (form
== DW_FORM_implicit_const
)
1256 implicit_const
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1257 TRUE
, info_ptr_end
);
1258 info_ptr
+= bytes_read
;
1260 info_ptr
= read_attribute_value (attr
, form
, implicit_const
, unit
,
1261 info_ptr
, info_ptr_end
);
1263 case DW_FORM_implicit_const
:
1264 attr
->form
= DW_FORM_sdata
;
1265 attr
->u
.sval
= implicit_const
;
1268 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1270 bfd_set_error (bfd_error_bad_value
);
1276 /* Read an attribute described by an abbreviated attribute. */
1279 read_attribute (struct attribute
* attr
,
1280 struct attr_abbrev
* abbrev
,
1281 struct comp_unit
* unit
,
1282 bfd_byte
* info_ptr
,
1283 bfd_byte
* info_ptr_end
)
1285 attr
->name
= abbrev
->name
;
1286 info_ptr
= read_attribute_value (attr
, abbrev
->form
, abbrev
->implicit_const
,
1287 unit
, info_ptr
, info_ptr_end
);
1291 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1295 non_mangled (int lang
)
1305 case DW_LANG_Cobol74
:
1306 case DW_LANG_Cobol85
:
1307 case DW_LANG_Fortran77
:
1308 case DW_LANG_Pascal83
:
1318 /* Source line information table routines. */
1320 #define FILE_ALLOC_CHUNK 5
1321 #define DIR_ALLOC_CHUNK 5
1325 struct line_info
* prev_line
;
1329 unsigned int column
;
1330 unsigned int discriminator
;
1331 unsigned char op_index
;
1332 unsigned char end_sequence
; /* End of (sequential) code sequence. */
1343 struct line_sequence
1346 struct line_sequence
* prev_sequence
;
1347 struct line_info
* last_line
; /* Largest VMA. */
1348 struct line_info
** line_info_lookup
;
1349 bfd_size_type num_lines
;
1352 struct line_info_table
1355 unsigned int num_files
;
1356 unsigned int num_dirs
;
1357 unsigned int num_sequences
;
1360 struct fileinfo
* files
;
1361 struct line_sequence
* sequences
;
1362 struct line_info
* lcl_head
; /* Local head; used in 'add_line_info'. */
1365 /* Remember some information about each function. If the function is
1366 inlined (DW_TAG_inlined_subroutine) it may have two additional
1367 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1368 source code location where this function was inlined. */
1372 /* Pointer to previous function in list of all functions. */
1373 struct funcinfo
* prev_func
;
1374 /* Pointer to function one scope higher. */
1375 struct funcinfo
* caller_func
;
1376 /* Source location file name where caller_func inlines this func. */
1378 /* Source location file name. */
1380 /* Source location line number where caller_func inlines this func. */
1382 /* Source location line number. */
1385 bfd_boolean is_linkage
;
1387 struct arange arange
;
1388 /* Where the symbol is defined. */
1392 struct lookup_funcinfo
1394 /* Function information corresponding to this lookup table entry. */
1395 struct funcinfo
* funcinfo
;
1397 /* The lowest address for this specific function. */
1400 /* The highest address of this function before the lookup table is sorted.
1401 The highest address of all prior functions after the lookup table is
1402 sorted, which is used for binary search. */
1408 /* Pointer to previous variable in list of all variables */
1409 struct varinfo
*prev_var
;
1410 /* Source location file name */
1412 /* Source location line number */
1417 /* Where the symbol is defined */
1419 /* Is this a stack variable? */
1420 unsigned int stack
: 1;
1423 /* Return TRUE if NEW_LINE should sort after LINE. */
1425 static inline bfd_boolean
1426 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
1428 return (new_line
->address
> line
->address
1429 || (new_line
->address
== line
->address
1430 && new_line
->op_index
> line
->op_index
));
1434 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1435 that the list is sorted. Note that the line_info list is sorted from
1436 highest to lowest VMA (with possible duplicates); that is,
1437 line_info->prev_line always accesses an equal or smaller VMA. */
1440 add_line_info (struct line_info_table
*table
,
1442 unsigned char op_index
,
1445 unsigned int column
,
1446 unsigned int discriminator
,
1449 bfd_size_type amt
= sizeof (struct line_info
);
1450 struct line_sequence
* seq
= table
->sequences
;
1451 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
1456 /* Set member data of 'info'. */
1457 info
->prev_line
= NULL
;
1458 info
->address
= address
;
1459 info
->op_index
= op_index
;
1461 info
->column
= column
;
1462 info
->discriminator
= discriminator
;
1463 info
->end_sequence
= end_sequence
;
1465 if (filename
&& filename
[0])
1467 info
->filename
= (char *) bfd_alloc (table
->abfd
, strlen (filename
) + 1);
1468 if (info
->filename
== NULL
)
1470 strcpy (info
->filename
, filename
);
1473 info
->filename
= NULL
;
1475 /* Find the correct location for 'info'. Normally we will receive
1476 new line_info data 1) in order and 2) with increasing VMAs.
1477 However some compilers break the rules (cf. decode_line_info) and
1478 so we include some heuristics for quickly finding the correct
1479 location for 'info'. In particular, these heuristics optimize for
1480 the common case in which the VMA sequence that we receive is a
1481 list of locally sorted VMAs such as
1482 p...z a...j (where a < j < p < z)
1484 Note: table->lcl_head is used to head an *actual* or *possible*
1485 sub-sequence within the list (such as a...j) that is not directly
1486 headed by table->last_line
1488 Note: we may receive duplicate entries from 'decode_line_info'. */
1491 && seq
->last_line
->address
== address
1492 && seq
->last_line
->op_index
== op_index
1493 && seq
->last_line
->end_sequence
== end_sequence
)
1495 /* We only keep the last entry with the same address and end
1496 sequence. See PR ld/4986. */
1497 if (table
->lcl_head
== seq
->last_line
)
1498 table
->lcl_head
= info
;
1499 info
->prev_line
= seq
->last_line
->prev_line
;
1500 seq
->last_line
= info
;
1502 else if (!seq
|| seq
->last_line
->end_sequence
)
1504 /* Start a new line sequence. */
1505 amt
= sizeof (struct line_sequence
);
1506 seq
= (struct line_sequence
*) bfd_malloc (amt
);
1509 seq
->low_pc
= address
;
1510 seq
->prev_sequence
= table
->sequences
;
1511 seq
->last_line
= info
;
1512 table
->lcl_head
= info
;
1513 table
->sequences
= seq
;
1514 table
->num_sequences
++;
1516 else if (info
->end_sequence
1517 || new_line_sorts_after (info
, seq
->last_line
))
1519 /* Normal case: add 'info' to the beginning of the current sequence. */
1520 info
->prev_line
= seq
->last_line
;
1521 seq
->last_line
= info
;
1523 /* lcl_head: initialize to head a *possible* sequence at the end. */
1524 if (!table
->lcl_head
)
1525 table
->lcl_head
= info
;
1527 else if (!new_line_sorts_after (info
, table
->lcl_head
)
1528 && (!table
->lcl_head
->prev_line
1529 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
1531 /* Abnormal but easy: lcl_head is the head of 'info'. */
1532 info
->prev_line
= table
->lcl_head
->prev_line
;
1533 table
->lcl_head
->prev_line
= info
;
1537 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1538 are valid heads for 'info'. Reset 'lcl_head'. */
1539 struct line_info
* li2
= seq
->last_line
; /* Always non-NULL. */
1540 struct line_info
* li1
= li2
->prev_line
;
1544 if (!new_line_sorts_after (info
, li2
)
1545 && new_line_sorts_after (info
, li1
))
1548 li2
= li1
; /* always non-NULL */
1549 li1
= li1
->prev_line
;
1551 table
->lcl_head
= li2
;
1552 info
->prev_line
= table
->lcl_head
->prev_line
;
1553 table
->lcl_head
->prev_line
= info
;
1554 if (address
< seq
->low_pc
)
1555 seq
->low_pc
= address
;
1560 /* Extract a fully qualified filename from a line info table.
1561 The returned string has been malloc'ed and it is the caller's
1562 responsibility to free it. */
1565 concat_filename (struct line_info_table
*table
, unsigned int file
)
1569 if (table
== NULL
|| file
- 1 >= table
->num_files
)
1571 /* FILE == 0 means unknown. */
1574 (_("DWARF error: mangled line number section (bad file number)"));
1575 return strdup ("<unknown>");
1578 filename
= table
->files
[file
- 1].name
;
1579 if (filename
== NULL
)
1580 return strdup ("<unknown>");
1582 if (!IS_ABSOLUTE_PATH (filename
))
1584 char *dir_name
= NULL
;
1585 char *subdir_name
= NULL
;
1589 if (table
->files
[file
- 1].dir
1590 /* PR 17512: file: 0317e960. */
1591 && table
->files
[file
- 1].dir
<= table
->num_dirs
1592 /* PR 17512: file: 7f3d2e4b. */
1593 && table
->dirs
!= NULL
)
1594 subdir_name
= table
->dirs
[table
->files
[file
- 1].dir
- 1];
1596 if (!subdir_name
|| !IS_ABSOLUTE_PATH (subdir_name
))
1597 dir_name
= table
->comp_dir
;
1601 dir_name
= subdir_name
;
1606 return strdup (filename
);
1608 len
= strlen (dir_name
) + strlen (filename
) + 2;
1612 len
+= strlen (subdir_name
) + 1;
1613 name
= (char *) bfd_malloc (len
);
1615 sprintf (name
, "%s/%s/%s", dir_name
, subdir_name
, filename
);
1619 name
= (char *) bfd_malloc (len
);
1621 sprintf (name
, "%s/%s", dir_name
, filename
);
1627 return strdup (filename
);
1631 arange_add (const struct comp_unit
*unit
, struct arange
*first_arange
,
1632 bfd_vma low_pc
, bfd_vma high_pc
)
1634 struct arange
*arange
;
1636 /* Ignore empty ranges. */
1637 if (low_pc
== high_pc
)
1640 /* If the first arange is empty, use it. */
1641 if (first_arange
->high
== 0)
1643 first_arange
->low
= low_pc
;
1644 first_arange
->high
= high_pc
;
1648 /* Next see if we can cheaply extend an existing range. */
1649 arange
= first_arange
;
1652 if (low_pc
== arange
->high
)
1654 arange
->high
= high_pc
;
1657 if (high_pc
== arange
->low
)
1659 arange
->low
= low_pc
;
1662 arange
= arange
->next
;
1666 /* Need to allocate a new arange and insert it into the arange list.
1667 Order isn't significant, so just insert after the first arange. */
1668 arange
= (struct arange
*) bfd_alloc (unit
->abfd
, sizeof (*arange
));
1671 arange
->low
= low_pc
;
1672 arange
->high
= high_pc
;
1673 arange
->next
= first_arange
->next
;
1674 first_arange
->next
= arange
;
1678 /* Compare function for line sequences. */
1681 compare_sequences (const void* a
, const void* b
)
1683 const struct line_sequence
* seq1
= a
;
1684 const struct line_sequence
* seq2
= b
;
1686 /* Sort by low_pc as the primary key. */
1687 if (seq1
->low_pc
< seq2
->low_pc
)
1689 if (seq1
->low_pc
> seq2
->low_pc
)
1692 /* If low_pc values are equal, sort in reverse order of
1693 high_pc, so that the largest region comes first. */
1694 if (seq1
->last_line
->address
< seq2
->last_line
->address
)
1696 if (seq1
->last_line
->address
> seq2
->last_line
->address
)
1699 if (seq1
->last_line
->op_index
< seq2
->last_line
->op_index
)
1701 if (seq1
->last_line
->op_index
> seq2
->last_line
->op_index
)
1707 /* Construct the line information table for quick lookup. */
1710 build_line_info_table (struct line_info_table
* table
,
1711 struct line_sequence
* seq
)
1714 struct line_info
** line_info_lookup
;
1715 struct line_info
* each_line
;
1716 unsigned int num_lines
;
1717 unsigned int line_index
;
1719 if (seq
->line_info_lookup
!= NULL
)
1722 /* Count the number of line information entries. We could do this while
1723 scanning the debug information, but some entries may be added via
1724 lcl_head without having a sequence handy to increment the number of
1727 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
1733 /* Allocate space for the line information lookup table. */
1734 amt
= sizeof (struct line_info
*) * num_lines
;
1735 line_info_lookup
= (struct line_info
**) bfd_alloc (table
->abfd
, amt
);
1736 if (line_info_lookup
== NULL
)
1739 /* Create the line information lookup table. */
1740 line_index
= num_lines
;
1741 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
1742 line_info_lookup
[--line_index
] = each_line
;
1744 BFD_ASSERT (line_index
== 0);
1746 seq
->num_lines
= num_lines
;
1747 seq
->line_info_lookup
= line_info_lookup
;
1752 /* Sort the line sequences for quick lookup. */
1755 sort_line_sequences (struct line_info_table
* table
)
1758 struct line_sequence
* sequences
;
1759 struct line_sequence
* seq
;
1761 unsigned int num_sequences
= table
->num_sequences
;
1762 bfd_vma last_high_pc
;
1764 if (num_sequences
== 0)
1767 /* Allocate space for an array of sequences. */
1768 amt
= sizeof (struct line_sequence
) * num_sequences
;
1769 sequences
= (struct line_sequence
*) bfd_alloc (table
->abfd
, amt
);
1770 if (sequences
== NULL
)
1773 /* Copy the linked list into the array, freeing the original nodes. */
1774 seq
= table
->sequences
;
1775 for (n
= 0; n
< num_sequences
; n
++)
1777 struct line_sequence
* last_seq
= seq
;
1780 sequences
[n
].low_pc
= seq
->low_pc
;
1781 sequences
[n
].prev_sequence
= NULL
;
1782 sequences
[n
].last_line
= seq
->last_line
;
1783 sequences
[n
].line_info_lookup
= NULL
;
1784 sequences
[n
].num_lines
= 0;
1785 seq
= seq
->prev_sequence
;
1788 BFD_ASSERT (seq
== NULL
);
1790 qsort (sequences
, n
, sizeof (struct line_sequence
), compare_sequences
);
1792 /* Make the list binary-searchable by trimming overlapping entries
1793 and removing nested entries. */
1795 last_high_pc
= sequences
[0].last_line
->address
;
1796 for (n
= 1; n
< table
->num_sequences
; n
++)
1798 if (sequences
[n
].low_pc
< last_high_pc
)
1800 if (sequences
[n
].last_line
->address
<= last_high_pc
)
1801 /* Skip nested entries. */
1804 /* Trim overlapping entries. */
1805 sequences
[n
].low_pc
= last_high_pc
;
1807 last_high_pc
= sequences
[n
].last_line
->address
;
1808 if (n
> num_sequences
)
1810 /* Close up the gap. */
1811 sequences
[num_sequences
].low_pc
= sequences
[n
].low_pc
;
1812 sequences
[num_sequences
].last_line
= sequences
[n
].last_line
;
1817 table
->sequences
= sequences
;
1818 table
->num_sequences
= num_sequences
;
1822 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
1825 line_info_add_include_dir (struct line_info_table
*table
, char *cur_dir
)
1827 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1832 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1833 amt
*= sizeof (char *);
1835 tmp
= (char **) bfd_realloc (table
->dirs
, amt
);
1841 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1846 line_info_add_include_dir_stub (struct line_info_table
*table
, char *cur_dir
,
1847 unsigned int dir ATTRIBUTE_UNUSED
,
1848 unsigned int xtime ATTRIBUTE_UNUSED
,
1849 unsigned int size ATTRIBUTE_UNUSED
)
1851 return line_info_add_include_dir (table
, cur_dir
);
1854 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
1857 line_info_add_file_name (struct line_info_table
*table
, char *cur_file
,
1858 unsigned int dir
, unsigned int xtime
,
1861 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1863 struct fileinfo
*tmp
;
1866 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1867 amt
*= sizeof (struct fileinfo
);
1869 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1875 table
->files
[table
->num_files
].name
= cur_file
;
1876 table
->files
[table
->num_files
].dir
= dir
;
1877 table
->files
[table
->num_files
].time
= xtime
;
1878 table
->files
[table
->num_files
].size
= size
;
1883 /* Read directory or file name entry format, starting with byte of
1884 format count entries, ULEB128 pairs of entry formats, ULEB128 of
1885 entries count and the entries themselves in the described entry
1889 read_formatted_entries (struct comp_unit
*unit
, bfd_byte
**bufp
,
1890 bfd_byte
*buf_end
, struct line_info_table
*table
,
1891 bfd_boolean (*callback
) (struct line_info_table
*table
,
1897 bfd
*abfd
= unit
->abfd
;
1898 bfd_byte format_count
, formati
;
1899 bfd_vma data_count
, datai
;
1900 bfd_byte
*buf
= *bufp
;
1901 bfd_byte
*format_header_data
;
1902 unsigned int bytes_read
;
1904 format_count
= read_1_byte (abfd
, buf
, buf_end
);
1906 format_header_data
= buf
;
1907 for (formati
= 0; formati
< format_count
; formati
++)
1909 _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
1911 _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
1915 data_count
= _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
1917 if (format_count
== 0 && data_count
!= 0)
1919 _bfd_error_handler (_("DWARF error: zero format count"));
1920 bfd_set_error (bfd_error_bad_value
);
1924 /* PR 22210. Paranoia check. Don't bother running the loop
1925 if we know that we are going to run out of buffer. */
1926 if (data_count
> (bfd_vma
) (buf_end
- buf
))
1929 (_("DWARF error: data count (%" PRIx64
") larger than buffer size"),
1930 (uint64_t) data_count
);
1931 bfd_set_error (bfd_error_bad_value
);
1935 for (datai
= 0; datai
< data_count
; datai
++)
1937 bfd_byte
*format
= format_header_data
;
1940 memset (&fe
, 0, sizeof fe
);
1941 for (formati
= 0; formati
< format_count
; formati
++)
1943 bfd_vma content_type
, form
;
1945 char **stringp
= &string_trash
;
1946 unsigned int uint_trash
, *uintp
= &uint_trash
;
1947 struct attribute attr
;
1949 content_type
= _bfd_safe_read_leb128 (abfd
, format
, &bytes_read
,
1951 format
+= bytes_read
;
1952 switch (content_type
)
1957 case DW_LNCT_directory_index
:
1960 case DW_LNCT_timestamp
:
1970 (_("DWARF error: unknown format content type %" PRIu64
),
1971 (uint64_t) content_type
);
1972 bfd_set_error (bfd_error_bad_value
);
1976 form
= _bfd_safe_read_leb128 (abfd
, format
, &bytes_read
, FALSE
,
1978 format
+= bytes_read
;
1980 buf
= read_attribute_value (&attr
, form
, 0, unit
, buf
, buf_end
);
1985 case DW_FORM_string
:
1986 case DW_FORM_line_strp
:
1987 *stringp
= attr
.u
.str
;
1995 *uintp
= attr
.u
.val
;
2000 if (!callback (table
, fe
.name
, fe
.dir
, fe
.time
, fe
.size
))
2008 /* Decode the line number information for UNIT. */
2010 static struct line_info_table
*
2011 decode_line_info (struct comp_unit
*unit
, struct dwarf2_debug
*stash
)
2013 bfd
*abfd
= unit
->abfd
;
2014 struct line_info_table
* table
;
2017 struct line_head lh
;
2018 unsigned int i
, bytes_read
, offset_size
;
2019 char *cur_file
, *cur_dir
;
2020 unsigned char op_code
, extended_op
, adj_opcode
;
2021 unsigned int exop_len
;
2024 if (! read_section (abfd
, &stash
->debug_sections
[debug_line
],
2025 stash
->syms
, unit
->line_offset
,
2026 &stash
->dwarf_line_buffer
, &stash
->dwarf_line_size
))
2029 amt
= sizeof (struct line_info_table
);
2030 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
2034 table
->comp_dir
= unit
->comp_dir
;
2036 table
->num_files
= 0;
2037 table
->files
= NULL
;
2039 table
->num_dirs
= 0;
2042 table
->num_sequences
= 0;
2043 table
->sequences
= NULL
;
2045 table
->lcl_head
= NULL
;
2047 if (stash
->dwarf_line_size
< 16)
2050 (_("DWARF error: line info section is too small (%" PRId64
")"),
2051 (int64_t) stash
->dwarf_line_size
);
2052 bfd_set_error (bfd_error_bad_value
);
2055 line_ptr
= stash
->dwarf_line_buffer
+ unit
->line_offset
;
2056 line_end
= stash
->dwarf_line_buffer
+ stash
->dwarf_line_size
;
2058 /* Read in the prologue. */
2059 lh
.total_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2062 if (lh
.total_length
== 0xffffffff)
2064 lh
.total_length
= read_8_bytes (abfd
, line_ptr
, line_end
);
2068 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
2070 /* Handle (non-standard) 64-bit DWARF2 formats. */
2071 lh
.total_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2076 if (lh
.total_length
> (size_t) (line_end
- line_ptr
))
2079 /* xgettext: c-format */
2080 (_("DWARF error: line info data is bigger (%#" PRIx64
")"
2081 " than the space remaining in the section (%#lx)"),
2082 (uint64_t) lh
.total_length
, (unsigned long) (line_end
- line_ptr
));
2083 bfd_set_error (bfd_error_bad_value
);
2087 line_end
= line_ptr
+ lh
.total_length
;
2089 lh
.version
= read_2_bytes (abfd
, line_ptr
, line_end
);
2090 if (lh
.version
< 2 || lh
.version
> 5)
2093 (_("DWARF error: unhandled .debug_line version %d"), lh
.version
);
2094 bfd_set_error (bfd_error_bad_value
);
2099 if (line_ptr
+ offset_size
+ (lh
.version
>= 5 ? 8 : (lh
.version
>= 4 ? 6 : 5))
2103 (_("DWARF error: ran out of room reading prologue"));
2104 bfd_set_error (bfd_error_bad_value
);
2108 if (lh
.version
>= 5)
2110 unsigned int segment_selector_size
;
2112 /* Skip address size. */
2113 read_1_byte (abfd
, line_ptr
, line_end
);
2116 segment_selector_size
= read_1_byte (abfd
, line_ptr
, line_end
);
2118 if (segment_selector_size
!= 0)
2121 (_("DWARF error: line info unsupported segment selector size %u"),
2122 segment_selector_size
);
2123 bfd_set_error (bfd_error_bad_value
);
2128 if (offset_size
== 4)
2129 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2131 lh
.prologue_length
= read_8_bytes (abfd
, line_ptr
, line_end
);
2132 line_ptr
+= offset_size
;
2134 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
, line_end
);
2137 if (lh
.version
>= 4)
2139 lh
.maximum_ops_per_insn
= read_1_byte (abfd
, line_ptr
, line_end
);
2143 lh
.maximum_ops_per_insn
= 1;
2145 if (lh
.maximum_ops_per_insn
== 0)
2148 (_("DWARF error: invalid maximum operations per instruction"));
2149 bfd_set_error (bfd_error_bad_value
);
2153 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
, line_end
);
2156 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
, line_end
);
2159 lh
.line_range
= read_1_byte (abfd
, line_ptr
, line_end
);
2162 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
, line_end
);
2165 if (line_ptr
+ (lh
.opcode_base
- 1) >= line_end
)
2167 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2168 bfd_set_error (bfd_error_bad_value
);
2172 amt
= lh
.opcode_base
* sizeof (unsigned char);
2173 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
2175 lh
.standard_opcode_lengths
[0] = 1;
2177 for (i
= 1; i
< lh
.opcode_base
; ++i
)
2179 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
, line_end
);
2183 if (lh
.version
>= 5)
2185 /* Read directory table. */
2186 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2187 line_info_add_include_dir_stub
))
2190 /* Read file name table. */
2191 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2192 line_info_add_file_name
))
2197 /* Read directory table. */
2198 while ((cur_dir
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
)) != NULL
)
2200 line_ptr
+= bytes_read
;
2202 if (!line_info_add_include_dir (table
, cur_dir
))
2206 line_ptr
+= bytes_read
;
2208 /* Read file name table. */
2209 while ((cur_file
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
)) != NULL
)
2211 unsigned int dir
, xtime
, size
;
2213 line_ptr
+= bytes_read
;
2215 dir
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2216 line_ptr
+= bytes_read
;
2217 xtime
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2218 line_ptr
+= bytes_read
;
2219 size
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2220 line_ptr
+= bytes_read
;
2222 if (!line_info_add_file_name (table
, cur_file
, dir
, xtime
, size
))
2226 line_ptr
+= bytes_read
;
2229 /* Read the statement sequences until there's nothing left. */
2230 while (line_ptr
< line_end
)
2232 /* State machine registers. */
2233 bfd_vma address
= 0;
2234 unsigned char op_index
= 0;
2235 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
2236 unsigned int line
= 1;
2237 unsigned int column
= 0;
2238 unsigned int discriminator
= 0;
2239 int is_stmt
= lh
.default_is_stmt
;
2240 int end_sequence
= 0;
2241 unsigned int dir
, xtime
, size
;
2242 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2243 compilers generate address sequences that are wildly out of
2244 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2245 for ia64-Linux). Thus, to determine the low and high
2246 address, we must compare on every DW_LNS_copy, etc. */
2247 bfd_vma low_pc
= (bfd_vma
) -1;
2248 bfd_vma high_pc
= 0;
2250 /* Decode the table. */
2251 while (!end_sequence
&& line_ptr
< line_end
)
2253 op_code
= read_1_byte (abfd
, line_ptr
, line_end
);
2256 if (op_code
>= lh
.opcode_base
)
2258 /* Special operand. */
2259 adj_opcode
= op_code
- lh
.opcode_base
;
2260 if (lh
.line_range
== 0)
2262 if (lh
.maximum_ops_per_insn
== 1)
2263 address
+= (adj_opcode
/ lh
.line_range
2264 * lh
.minimum_instruction_length
);
2267 address
+= ((op_index
+ adj_opcode
/ lh
.line_range
)
2268 / lh
.maximum_ops_per_insn
2269 * lh
.minimum_instruction_length
);
2270 op_index
= ((op_index
+ adj_opcode
/ lh
.line_range
)
2271 % lh
.maximum_ops_per_insn
);
2273 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
2274 /* Append row to matrix using current values. */
2275 if (!add_line_info (table
, address
, op_index
, filename
,
2276 line
, column
, discriminator
, 0))
2279 if (address
< low_pc
)
2281 if (address
> high_pc
)
2284 else switch (op_code
)
2286 case DW_LNS_extended_op
:
2287 exop_len
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2289 line_ptr
+= bytes_read
;
2290 extended_op
= read_1_byte (abfd
, line_ptr
, line_end
);
2293 switch (extended_op
)
2295 case DW_LNE_end_sequence
:
2297 if (!add_line_info (table
, address
, op_index
, filename
, line
,
2298 column
, discriminator
, end_sequence
))
2301 if (address
< low_pc
)
2303 if (address
> high_pc
)
2305 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
2308 case DW_LNE_set_address
:
2309 address
= read_address (unit
, line_ptr
, line_end
);
2311 line_ptr
+= unit
->addr_size
;
2313 case DW_LNE_define_file
:
2314 cur_file
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
);
2315 line_ptr
+= bytes_read
;
2316 dir
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2318 line_ptr
+= bytes_read
;
2319 xtime
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2321 line_ptr
+= bytes_read
;
2322 size
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2324 line_ptr
+= bytes_read
;
2325 if (!line_info_add_file_name (table
, cur_file
, dir
,
2329 case DW_LNE_set_discriminator
:
2331 _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2333 line_ptr
+= bytes_read
;
2335 case DW_LNE_HP_source_file_correlation
:
2336 line_ptr
+= exop_len
- 1;
2340 (_("DWARF error: mangled line number section"));
2341 bfd_set_error (bfd_error_bad_value
);
2343 if (filename
!= NULL
)
2349 if (!add_line_info (table
, address
, op_index
,
2350 filename
, line
, column
, discriminator
, 0))
2353 if (address
< low_pc
)
2355 if (address
> high_pc
)
2358 case DW_LNS_advance_pc
:
2359 if (lh
.maximum_ops_per_insn
== 1)
2360 address
+= (lh
.minimum_instruction_length
2361 * _bfd_safe_read_leb128 (abfd
, line_ptr
,
2366 bfd_vma adjust
= _bfd_safe_read_leb128 (abfd
, line_ptr
,
2369 address
= ((op_index
+ adjust
) / lh
.maximum_ops_per_insn
2370 * lh
.minimum_instruction_length
);
2371 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2373 line_ptr
+= bytes_read
;
2375 case DW_LNS_advance_line
:
2376 line
+= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2378 line_ptr
+= bytes_read
;
2380 case DW_LNS_set_file
:
2384 /* The file and directory tables are 0
2385 based, the references are 1 based. */
2386 file
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2388 line_ptr
+= bytes_read
;
2391 filename
= concat_filename (table
, file
);
2394 case DW_LNS_set_column
:
2395 column
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2397 line_ptr
+= bytes_read
;
2399 case DW_LNS_negate_stmt
:
2400 is_stmt
= (!is_stmt
);
2402 case DW_LNS_set_basic_block
:
2404 case DW_LNS_const_add_pc
:
2405 if (lh
.line_range
== 0)
2407 if (lh
.maximum_ops_per_insn
== 1)
2408 address
+= (lh
.minimum_instruction_length
2409 * ((255 - lh
.opcode_base
) / lh
.line_range
));
2412 bfd_vma adjust
= ((255 - lh
.opcode_base
) / lh
.line_range
);
2413 address
+= (lh
.minimum_instruction_length
2414 * ((op_index
+ adjust
)
2415 / lh
.maximum_ops_per_insn
));
2416 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2419 case DW_LNS_fixed_advance_pc
:
2420 address
+= read_2_bytes (abfd
, line_ptr
, line_end
);
2425 /* Unknown standard opcode, ignore it. */
2426 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
2428 (void) _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2430 line_ptr
+= bytes_read
;
2440 if (sort_line_sequences (table
))
2444 while (table
->sequences
!= NULL
)
2446 struct line_sequence
* seq
= table
->sequences
;
2447 table
->sequences
= table
->sequences
->prev_sequence
;
2450 if (table
->files
!= NULL
)
2451 free (table
->files
);
2452 if (table
->dirs
!= NULL
)
2457 /* If ADDR is within TABLE set the output parameters and return the
2458 range of addresses covered by the entry used to fill them out.
2459 Otherwise set * FILENAME_PTR to NULL and return 0.
2460 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2461 are pointers to the objects to be filled in. */
2464 lookup_address_in_line_info_table (struct line_info_table
*table
,
2466 const char **filename_ptr
,
2467 unsigned int *linenumber_ptr
,
2468 unsigned int *discriminator_ptr
)
2470 struct line_sequence
*seq
= NULL
;
2471 struct line_info
*info
;
2474 /* Binary search the array of sequences. */
2476 high
= table
->num_sequences
;
2479 mid
= (low
+ high
) / 2;
2480 seq
= &table
->sequences
[mid
];
2481 if (addr
< seq
->low_pc
)
2483 else if (addr
>= seq
->last_line
->address
)
2489 /* Check for a valid sequence. */
2490 if (!seq
|| addr
< seq
->low_pc
|| addr
>= seq
->last_line
->address
)
2493 if (!build_line_info_table (table
, seq
))
2496 /* Binary search the array of line information. */
2498 high
= seq
->num_lines
;
2502 mid
= (low
+ high
) / 2;
2503 info
= seq
->line_info_lookup
[mid
];
2504 if (addr
< info
->address
)
2506 else if (addr
>= seq
->line_info_lookup
[mid
+ 1]->address
)
2512 /* Check for a valid line information entry. */
2514 && addr
>= info
->address
2515 && addr
< seq
->line_info_lookup
[mid
+ 1]->address
2516 && !(info
->end_sequence
|| info
== seq
->last_line
))
2518 *filename_ptr
= info
->filename
;
2519 *linenumber_ptr
= info
->line
;
2520 if (discriminator_ptr
)
2521 *discriminator_ptr
= info
->discriminator
;
2522 return seq
->last_line
->address
- seq
->low_pc
;
2526 *filename_ptr
= NULL
;
2530 /* Read in the .debug_ranges section for future reference. */
2533 read_debug_ranges (struct comp_unit
* unit
)
2535 struct dwarf2_debug
* stash
= unit
->stash
;
2537 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_ranges
],
2539 &stash
->dwarf_ranges_buffer
,
2540 &stash
->dwarf_ranges_size
);
2543 /* Function table functions. */
2546 compare_lookup_funcinfos (const void * a
, const void * b
)
2548 const struct lookup_funcinfo
* lookup1
= a
;
2549 const struct lookup_funcinfo
* lookup2
= b
;
2551 if (lookup1
->low_addr
< lookup2
->low_addr
)
2553 if (lookup1
->low_addr
> lookup2
->low_addr
)
2555 if (lookup1
->high_addr
< lookup2
->high_addr
)
2557 if (lookup1
->high_addr
> lookup2
->high_addr
)
2564 build_lookup_funcinfo_table (struct comp_unit
* unit
)
2566 struct lookup_funcinfo
*lookup_funcinfo_table
= unit
->lookup_funcinfo_table
;
2567 unsigned int number_of_functions
= unit
->number_of_functions
;
2568 struct funcinfo
*each
;
2569 struct lookup_funcinfo
*entry
;
2571 struct arange
*range
;
2572 bfd_vma low_addr
, high_addr
;
2574 if (lookup_funcinfo_table
|| number_of_functions
== 0)
2577 /* Create the function info lookup table. */
2578 lookup_funcinfo_table
= (struct lookup_funcinfo
*)
2579 bfd_malloc (number_of_functions
* sizeof (struct lookup_funcinfo
));
2580 if (lookup_funcinfo_table
== NULL
)
2583 /* Populate the function info lookup table. */
2584 func_index
= number_of_functions
;
2585 for (each
= unit
->function_table
; each
; each
= each
->prev_func
)
2587 entry
= &lookup_funcinfo_table
[--func_index
];
2588 entry
->funcinfo
= each
;
2590 /* Calculate the lowest and highest address for this function entry. */
2591 low_addr
= entry
->funcinfo
->arange
.low
;
2592 high_addr
= entry
->funcinfo
->arange
.high
;
2594 for (range
= entry
->funcinfo
->arange
.next
; range
; range
= range
->next
)
2596 if (range
->low
< low_addr
)
2597 low_addr
= range
->low
;
2598 if (range
->high
> high_addr
)
2599 high_addr
= range
->high
;
2602 entry
->low_addr
= low_addr
;
2603 entry
->high_addr
= high_addr
;
2606 BFD_ASSERT (func_index
== 0);
2608 /* Sort the function by address. */
2609 qsort (lookup_funcinfo_table
,
2610 number_of_functions
,
2611 sizeof (struct lookup_funcinfo
),
2612 compare_lookup_funcinfos
);
2614 /* Calculate the high watermark for each function in the lookup table. */
2615 high_addr
= lookup_funcinfo_table
[0].high_addr
;
2616 for (func_index
= 1; func_index
< number_of_functions
; func_index
++)
2618 entry
= &lookup_funcinfo_table
[func_index
];
2619 if (entry
->high_addr
> high_addr
)
2620 high_addr
= entry
->high_addr
;
2622 entry
->high_addr
= high_addr
;
2625 unit
->lookup_funcinfo_table
= lookup_funcinfo_table
;
2629 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
2630 TRUE. Note that we need to find the function that has the smallest range
2631 that contains ADDR, to handle inlined functions without depending upon
2632 them being ordered in TABLE by increasing range. */
2635 lookup_address_in_function_table (struct comp_unit
*unit
,
2637 struct funcinfo
**function_ptr
)
2639 unsigned int number_of_functions
= unit
->number_of_functions
;
2640 struct lookup_funcinfo
* lookup_funcinfo
= NULL
;
2641 struct funcinfo
* funcinfo
= NULL
;
2642 struct funcinfo
* best_fit
= NULL
;
2643 bfd_vma best_fit_len
= 0;
2644 bfd_size_type low
, high
, mid
, first
;
2645 struct arange
*arange
;
2647 if (number_of_functions
== 0)
2650 if (!build_lookup_funcinfo_table (unit
))
2653 if (unit
->lookup_funcinfo_table
[number_of_functions
- 1].high_addr
< addr
)
2656 /* Find the first function in the lookup table which may contain the
2657 specified address. */
2659 high
= number_of_functions
;
2663 mid
= (low
+ high
) / 2;
2664 lookup_funcinfo
= &unit
->lookup_funcinfo_table
[mid
];
2665 if (addr
< lookup_funcinfo
->low_addr
)
2667 else if (addr
>= lookup_funcinfo
->high_addr
)
2673 /* Find the 'best' match for the address. The prior algorithm defined the
2674 best match as the function with the smallest address range containing
2675 the specified address. This definition should probably be changed to the
2676 innermost inline routine containing the address, but right now we want
2677 to get the same results we did before. */
2678 while (first
< number_of_functions
)
2680 if (addr
< unit
->lookup_funcinfo_table
[first
].low_addr
)
2682 funcinfo
= unit
->lookup_funcinfo_table
[first
].funcinfo
;
2684 for (arange
= &funcinfo
->arange
; arange
; arange
= arange
->next
)
2686 if (addr
< arange
->low
|| addr
>= arange
->high
)
2690 || arange
->high
- arange
->low
< best_fit_len
2691 /* The following comparison is designed to return the same
2692 match as the previous algorithm for routines which have the
2693 same best fit length. */
2694 || (arange
->high
- arange
->low
== best_fit_len
2695 && funcinfo
> best_fit
))
2697 best_fit
= funcinfo
;
2698 best_fit_len
= arange
->high
- arange
->low
;
2708 *function_ptr
= best_fit
;
2712 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2713 and LINENUMBER_PTR, and return TRUE. */
2716 lookup_symbol_in_function_table (struct comp_unit
*unit
,
2719 const char **filename_ptr
,
2720 unsigned int *linenumber_ptr
)
2722 struct funcinfo
* each_func
;
2723 struct funcinfo
* best_fit
= NULL
;
2724 bfd_vma best_fit_len
= 0;
2725 struct arange
*arange
;
2726 const char *name
= bfd_asymbol_name (sym
);
2727 asection
*sec
= bfd_get_section (sym
);
2729 for (each_func
= unit
->function_table
;
2731 each_func
= each_func
->prev_func
)
2733 for (arange
= &each_func
->arange
;
2735 arange
= arange
->next
)
2737 if ((!each_func
->sec
|| each_func
->sec
== sec
)
2738 && addr
>= arange
->low
2739 && addr
< arange
->high
2741 && strcmp (name
, each_func
->name
) == 0
2743 || arange
->high
- arange
->low
< best_fit_len
))
2745 best_fit
= each_func
;
2746 best_fit_len
= arange
->high
- arange
->low
;
2753 best_fit
->sec
= sec
;
2754 *filename_ptr
= best_fit
->file
;
2755 *linenumber_ptr
= best_fit
->line
;
2762 /* Variable table functions. */
2764 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
2765 LINENUMBER_PTR, and return TRUE. */
2768 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
2771 const char **filename_ptr
,
2772 unsigned int *linenumber_ptr
)
2774 const char *name
= bfd_asymbol_name (sym
);
2775 asection
*sec
= bfd_get_section (sym
);
2776 struct varinfo
* each
;
2778 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
2779 if (each
->stack
== 0
2780 && each
->file
!= NULL
2781 && each
->name
!= NULL
2782 && each
->addr
== addr
2783 && (!each
->sec
|| each
->sec
== sec
)
2784 && strcmp (name
, each
->name
) == 0)
2790 *filename_ptr
= each
->file
;
2791 *linenumber_ptr
= each
->line
;
2799 find_abstract_instance (struct comp_unit
* unit
,
2800 bfd_byte
* orig_info_ptr
,
2801 struct attribute
* attr_ptr
,
2802 const char ** pname
,
2803 bfd_boolean
* is_linkage
,
2804 char ** filename_ptr
,
2805 int * linenumber_ptr
)
2807 bfd
*abfd
= unit
->abfd
;
2809 bfd_byte
*info_ptr_end
;
2810 unsigned int abbrev_number
, bytes_read
, i
;
2811 struct abbrev_info
*abbrev
;
2812 bfd_uint64_t die_ref
= attr_ptr
->u
.val
;
2813 struct attribute attr
;
2814 const char *name
= NULL
;
2816 /* DW_FORM_ref_addr can reference an entry in a different CU. It
2817 is an offset from the .debug_info section, not the current CU. */
2818 if (attr_ptr
->form
== DW_FORM_ref_addr
)
2820 /* We only support DW_FORM_ref_addr within the same file, so
2821 any relocations should be resolved already. Check this by
2822 testing for a zero die_ref; There can't be a valid reference
2823 to the header of a .debug_info section.
2824 DW_FORM_ref_addr is an offset relative to .debug_info.
2825 Normally when using the GNU linker this is accomplished by
2826 emitting a symbolic reference to a label, because .debug_info
2827 sections are linked at zero. When there are multiple section
2828 groups containing .debug_info, as there might be in a
2829 relocatable object file, it would be reasonable to assume that
2830 a symbolic reference to a label in any .debug_info section
2831 might be used. Since we lay out multiple .debug_info
2832 sections at non-zero VMAs (see place_sections), and read
2833 them contiguously into stash->info_ptr_memory, that means
2834 the reference is relative to stash->info_ptr_memory. */
2837 info_ptr
= unit
->stash
->info_ptr_memory
;
2838 info_ptr_end
= unit
->stash
->info_ptr_end
;
2839 total
= info_ptr_end
- info_ptr
;
2840 if (!die_ref
|| die_ref
>= total
)
2843 (_("DWARF error: invalid abstract instance DIE ref"));
2844 bfd_set_error (bfd_error_bad_value
);
2847 info_ptr
+= die_ref
;
2849 /* Now find the CU containing this pointer. */
2850 if (info_ptr
>= unit
->info_ptr_unit
&& info_ptr
< unit
->end_ptr
)
2851 info_ptr_end
= unit
->end_ptr
;
2854 /* Check other CUs to see if they contain the abbrev. */
2855 struct comp_unit
* u
;
2857 for (u
= unit
->prev_unit
; u
!= NULL
; u
= u
->prev_unit
)
2858 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
2862 for (u
= unit
->next_unit
; u
!= NULL
; u
= u
->next_unit
)
2863 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
2869 info_ptr_end
= unit
->end_ptr
;
2871 /* else FIXME: What do we do now ? */
2874 else if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
2876 info_ptr
= read_alt_indirect_ref (unit
, die_ref
);
2877 if (info_ptr
== NULL
)
2880 (_("DWARF error: unable to read alt ref %" PRIu64
),
2881 (uint64_t) die_ref
);
2882 bfd_set_error (bfd_error_bad_value
);
2885 info_ptr_end
= (unit
->stash
->alt_dwarf_info_buffer
2886 + unit
->stash
->alt_dwarf_info_size
);
2888 /* FIXME: Do we need to locate the correct CU, in a similar
2889 fashion to the code in the DW_FORM_ref_addr case above ? */
2893 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
2894 DW_FORM_ref_udata. These are all references relative to the
2895 start of the current CU. */
2898 info_ptr
= unit
->info_ptr_unit
;
2899 info_ptr_end
= unit
->end_ptr
;
2900 total
= info_ptr_end
- info_ptr
;
2901 if (!die_ref
|| die_ref
>= total
)
2904 (_("DWARF error: invalid abstract instance DIE ref"));
2905 bfd_set_error (bfd_error_bad_value
);
2908 info_ptr
+= die_ref
;
2911 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
2912 FALSE
, info_ptr_end
);
2913 info_ptr
+= bytes_read
;
2917 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
2921 (_("DWARF error: could not find abbrev number %u"), abbrev_number
);
2922 bfd_set_error (bfd_error_bad_value
);
2927 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2929 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
,
2930 info_ptr
, info_ptr_end
);
2931 if (info_ptr
== NULL
)
2933 /* It doesn't ever make sense for DW_AT_specification to
2934 refer to the same DIE. Stop simple recursion. */
2935 if (info_ptr
== orig_info_ptr
)
2938 (_("DWARF error: abstract instance recursion detected"));
2939 bfd_set_error (bfd_error_bad_value
);
2945 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2947 if (name
== NULL
&& is_str_attr (attr
.form
))
2950 if (non_mangled (unit
->lang
))
2954 case DW_AT_specification
:
2955 if (!find_abstract_instance (unit
, info_ptr
, &attr
,
2957 filename_ptr
, linenumber_ptr
))
2960 case DW_AT_linkage_name
:
2961 case DW_AT_MIPS_linkage_name
:
2962 /* PR 16949: Corrupt debug info can place
2963 non-string forms into these attributes. */
2964 if (is_str_attr (attr
.form
))
2970 case DW_AT_decl_file
:
2971 *filename_ptr
= concat_filename (unit
->line_table
,
2974 case DW_AT_decl_line
:
2975 *linenumber_ptr
= attr
.u
.val
;
2988 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
,
2989 bfd_uint64_t offset
)
2991 bfd_byte
*ranges_ptr
;
2992 bfd_byte
*ranges_end
;
2993 bfd_vma base_address
= unit
->base_address
;
2995 if (! unit
->stash
->dwarf_ranges_buffer
)
2997 if (! read_debug_ranges (unit
))
3001 ranges_ptr
= unit
->stash
->dwarf_ranges_buffer
+ offset
;
3002 if (ranges_ptr
< unit
->stash
->dwarf_ranges_buffer
)
3004 ranges_end
= unit
->stash
->dwarf_ranges_buffer
+ unit
->stash
->dwarf_ranges_size
;
3011 /* PR 17512: file: 62cada7d. */
3012 if (ranges_ptr
+ 2 * unit
->addr_size
> ranges_end
)
3015 low_pc
= read_address (unit
, ranges_ptr
, ranges_end
);
3016 ranges_ptr
+= unit
->addr_size
;
3017 high_pc
= read_address (unit
, ranges_ptr
, ranges_end
);
3018 ranges_ptr
+= unit
->addr_size
;
3020 if (low_pc
== 0 && high_pc
== 0)
3022 if (low_pc
== -1UL && high_pc
!= -1UL)
3023 base_address
= high_pc
;
3026 if (!arange_add (unit
, arange
,
3027 base_address
+ low_pc
, base_address
+ high_pc
))
3034 /* DWARF2 Compilation unit functions. */
3036 /* Scan over each die in a comp. unit looking for functions to add
3037 to the function table and variables to the variable table. */
3040 scan_unit_for_symbols (struct comp_unit
*unit
)
3042 bfd
*abfd
= unit
->abfd
;
3043 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
3044 bfd_byte
*info_ptr_end
= unit
->stash
->info_ptr_end
;
3045 int nesting_level
= 0;
3046 struct nest_funcinfo
{
3047 struct funcinfo
*func
;
3049 int nested_funcs_size
;
3051 /* Maintain a stack of in-scope functions and inlined functions, which we
3052 can use to set the caller_func field. */
3053 nested_funcs_size
= 32;
3054 nested_funcs
= (struct nest_funcinfo
*)
3055 bfd_malloc (nested_funcs_size
* sizeof (*nested_funcs
));
3056 if (nested_funcs
== NULL
)
3058 nested_funcs
[nesting_level
].func
= 0;
3060 while (nesting_level
>= 0)
3062 unsigned int abbrev_number
, bytes_read
, i
;
3063 struct abbrev_info
*abbrev
;
3064 struct attribute attr
;
3065 struct funcinfo
*func
;
3066 struct varinfo
*var
;
3068 bfd_vma high_pc
= 0;
3069 bfd_boolean high_pc_relative
= FALSE
;
3071 /* PR 17512: file: 9f405d9d. */
3072 if (info_ptr
>= info_ptr_end
)
3075 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
3076 FALSE
, info_ptr_end
);
3077 info_ptr
+= bytes_read
;
3079 if (! abbrev_number
)
3085 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3088 static unsigned int previous_failed_abbrev
= -1U;
3090 /* Avoid multiple reports of the same missing abbrev. */
3091 if (abbrev_number
!= previous_failed_abbrev
)
3094 (_("DWARF error: could not find abbrev number %u"),
3096 previous_failed_abbrev
= abbrev_number
;
3098 bfd_set_error (bfd_error_bad_value
);
3103 if (abbrev
->tag
== DW_TAG_subprogram
3104 || abbrev
->tag
== DW_TAG_entry_point
3105 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3107 bfd_size_type amt
= sizeof (struct funcinfo
);
3108 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
3111 func
->tag
= abbrev
->tag
;
3112 func
->prev_func
= unit
->function_table
;
3113 unit
->function_table
= func
;
3114 unit
->number_of_functions
++;
3115 BFD_ASSERT (!unit
->cached
);
3117 if (func
->tag
== DW_TAG_inlined_subroutine
)
3118 for (i
= nesting_level
; i
-- != 0; )
3119 if (nested_funcs
[i
].func
)
3121 func
->caller_func
= nested_funcs
[i
].func
;
3124 nested_funcs
[nesting_level
].func
= func
;
3129 if (abbrev
->tag
== DW_TAG_variable
)
3131 bfd_size_type amt
= sizeof (struct varinfo
);
3132 var
= (struct varinfo
*) bfd_zalloc (abfd
, amt
);
3135 var
->tag
= abbrev
->tag
;
3137 var
->prev_var
= unit
->variable_table
;
3138 unit
->variable_table
= var
;
3139 /* PR 18205: Missing debug information can cause this
3140 var to be attached to an already cached unit. */
3143 /* No inline function in scope at this nesting level. */
3144 nested_funcs
[nesting_level
].func
= 0;
3147 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3149 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
3150 unit
, info_ptr
, info_ptr_end
);
3151 if (info_ptr
== NULL
)
3158 case DW_AT_call_file
:
3159 func
->caller_file
= concat_filename (unit
->line_table
,
3163 case DW_AT_call_line
:
3164 func
->caller_line
= attr
.u
.val
;
3167 case DW_AT_abstract_origin
:
3168 case DW_AT_specification
:
3169 if (!find_abstract_instance (unit
, info_ptr
, &attr
,
3178 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3180 if (func
->name
== NULL
&& is_str_attr (attr
.form
))
3182 func
->name
= attr
.u
.str
;
3183 if (non_mangled (unit
->lang
))
3184 func
->is_linkage
= TRUE
;
3188 case DW_AT_linkage_name
:
3189 case DW_AT_MIPS_linkage_name
:
3190 /* PR 16949: Corrupt debug info can place
3191 non-string forms into these attributes. */
3192 if (is_str_attr (attr
.form
))
3194 func
->name
= attr
.u
.str
;
3195 func
->is_linkage
= TRUE
;
3200 low_pc
= attr
.u
.val
;
3204 high_pc
= attr
.u
.val
;
3205 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
3209 if (!read_rangelist (unit
, &func
->arange
, attr
.u
.val
))
3213 case DW_AT_decl_file
:
3214 func
->file
= concat_filename (unit
->line_table
,
3218 case DW_AT_decl_line
:
3219 func
->line
= attr
.u
.val
;
3231 if (is_str_attr (attr
.form
))
3232 var
->name
= attr
.u
.str
;
3235 case DW_AT_decl_file
:
3236 var
->file
= concat_filename (unit
->line_table
,
3240 case DW_AT_decl_line
:
3241 var
->line
= attr
.u
.val
;
3244 case DW_AT_external
:
3245 if (attr
.u
.val
!= 0)
3249 case DW_AT_location
:
3253 case DW_FORM_block1
:
3254 case DW_FORM_block2
:
3255 case DW_FORM_block4
:
3256 case DW_FORM_exprloc
:
3257 if (attr
.u
.blk
->data
!= NULL
3258 && *attr
.u
.blk
->data
== DW_OP_addr
)
3262 /* Verify that DW_OP_addr is the only opcode in the
3263 location, in which case the block size will be 1
3264 plus the address size. */
3265 /* ??? For TLS variables, gcc can emit
3266 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
3267 which we don't handle here yet. */
3268 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
3269 var
->addr
= bfd_get (unit
->addr_size
* 8,
3271 attr
.u
.blk
->data
+ 1);
3286 if (high_pc_relative
)
3289 if (func
&& high_pc
!= 0)
3291 if (!arange_add (unit
, &func
->arange
, low_pc
, high_pc
))
3295 if (abbrev
->has_children
)
3299 if (nesting_level
>= nested_funcs_size
)
3301 struct nest_funcinfo
*tmp
;
3303 nested_funcs_size
*= 2;
3304 tmp
= (struct nest_funcinfo
*)
3305 bfd_realloc (nested_funcs
,
3306 nested_funcs_size
* sizeof (*nested_funcs
));
3311 nested_funcs
[nesting_level
].func
= 0;
3315 free (nested_funcs
);
3319 free (nested_funcs
);
3323 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
3324 includes the compilation unit header that proceeds the DIE's, but
3325 does not include the length field that precedes each compilation
3326 unit header. END_PTR points one past the end of this comp unit.
3327 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
3329 This routine does not read the whole compilation unit; only enough
3330 to get to the line number information for the compilation unit. */
3332 static struct comp_unit
*
3333 parse_comp_unit (struct dwarf2_debug
*stash
,
3334 bfd_vma unit_length
,
3335 bfd_byte
*info_ptr_unit
,
3336 unsigned int offset_size
)
3338 struct comp_unit
* unit
;
3339 unsigned int version
;
3340 bfd_uint64_t abbrev_offset
= 0;
3341 /* Initialize it just to avoid a GCC false warning. */
3342 unsigned int addr_size
= -1;
3343 struct abbrev_info
** abbrevs
;
3344 unsigned int abbrev_number
, bytes_read
, i
;
3345 struct abbrev_info
*abbrev
;
3346 struct attribute attr
;
3347 bfd_byte
*info_ptr
= stash
->info_ptr
;
3348 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
3351 bfd_vma high_pc
= 0;
3352 bfd
*abfd
= stash
->bfd_ptr
;
3353 bfd_boolean high_pc_relative
= FALSE
;
3354 enum dwarf_unit_type unit_type
;
3356 version
= read_2_bytes (abfd
, info_ptr
, end_ptr
);
3358 if (version
< 2 || version
> 5)
3360 /* PR 19872: A version number of 0 probably means that there is padding
3361 at the end of the .debug_info section. Gold puts it there when
3362 performing an incremental link, for example. So do not generate
3363 an error, just return a NULL. */
3367 (_("DWARF error: found dwarf version '%u', this reader"
3368 " only handles version 2, 3, 4 and 5 information"), version
);
3369 bfd_set_error (bfd_error_bad_value
);
3375 unit_type
= DW_UT_compile
;
3378 unit_type
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3381 addr_size
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3385 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
3386 if (offset_size
== 4)
3387 abbrev_offset
= read_4_bytes (abfd
, info_ptr
, end_ptr
);
3389 abbrev_offset
= read_8_bytes (abfd
, info_ptr
, end_ptr
);
3390 info_ptr
+= offset_size
;
3394 addr_size
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3398 if (unit_type
== DW_UT_type
)
3400 /* Skip type signature. */
3403 /* Skip type offset. */
3404 info_ptr
+= offset_size
;
3407 if (addr_size
> sizeof (bfd_vma
))
3410 /* xgettext: c-format */
3411 (_("DWARF error: found address size '%u', this reader"
3412 " can not handle sizes greater than '%u'"),
3414 (unsigned int) sizeof (bfd_vma
));
3415 bfd_set_error (bfd_error_bad_value
);
3419 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
3422 ("DWARF error: found address size '%u', this reader"
3423 " can only handle address sizes '2', '4' and '8'", addr_size
);
3424 bfd_set_error (bfd_error_bad_value
);
3428 /* Read the abbrevs for this compilation unit into a table. */
3429 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
);
3433 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
3435 info_ptr
+= bytes_read
;
3436 if (! abbrev_number
)
3438 /* PR 19872: An abbrev number of 0 probably means that there is padding
3439 at the end of the .debug_abbrev section. Gold puts it there when
3440 performing an incremental link, for example. So do not generate
3441 an error, just return a NULL. */
3445 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
3448 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
3450 bfd_set_error (bfd_error_bad_value
);
3454 amt
= sizeof (struct comp_unit
);
3455 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
3459 unit
->version
= version
;
3460 unit
->addr_size
= addr_size
;
3461 unit
->offset_size
= offset_size
;
3462 unit
->abbrevs
= abbrevs
;
3463 unit
->end_ptr
= end_ptr
;
3464 unit
->stash
= stash
;
3465 unit
->info_ptr_unit
= info_ptr_unit
;
3467 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3469 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
, end_ptr
);
3470 if (info_ptr
== NULL
)
3473 /* Store the data if it is of an attribute we want to keep in a
3474 partial symbol table. */
3477 case DW_AT_stmt_list
:
3479 unit
->line_offset
= attr
.u
.val
;
3483 if (is_str_attr (attr
.form
))
3484 unit
->name
= attr
.u
.str
;
3488 low_pc
= attr
.u
.val
;
3489 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
3490 this is the base address to use when reading location
3491 lists or range lists. */
3492 if (abbrev
->tag
== DW_TAG_compile_unit
)
3493 unit
->base_address
= low_pc
;
3497 high_pc
= attr
.u
.val
;
3498 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
3502 if (!read_rangelist (unit
, &unit
->arange
, attr
.u
.val
))
3506 case DW_AT_comp_dir
:
3508 char *comp_dir
= attr
.u
.str
;
3510 /* PR 17512: file: 1fe726be. */
3511 if (! is_str_attr (attr
.form
))
3514 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
3520 /* Irix 6.2 native cc prepends <machine>.: to the compilation
3521 directory, get rid of it. */
3522 char *cp
= strchr (comp_dir
, ':');
3524 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
3527 unit
->comp_dir
= comp_dir
;
3531 case DW_AT_language
:
3532 unit
->lang
= attr
.u
.val
;
3539 if (high_pc_relative
)
3543 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
3547 unit
->first_child_die_ptr
= info_ptr
;
3551 /* Return TRUE if UNIT may contain the address given by ADDR. When
3552 there are functions written entirely with inline asm statements, the
3553 range info in the compilation unit header may not be correct. We
3554 need to consult the line info table to see if a compilation unit
3555 really contains the given address. */
3558 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
3560 struct arange
*arange
;
3565 arange
= &unit
->arange
;
3568 if (addr
>= arange
->low
&& addr
< arange
->high
)
3570 arange
= arange
->next
;
3577 /* If UNIT contains ADDR, set the output parameters to the values for
3578 the line containing ADDR. The output parameters, FILENAME_PTR,
3579 FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects
3582 Returns the range of addresses covered by the entry that was used
3583 to fill in *LINENUMBER_PTR or 0 if it was not filled in. */
3586 comp_unit_find_nearest_line (struct comp_unit
*unit
,
3588 const char **filename_ptr
,
3589 struct funcinfo
**function_ptr
,
3590 unsigned int *linenumber_ptr
,
3591 unsigned int *discriminator_ptr
,
3592 struct dwarf2_debug
*stash
)
3599 if (! unit
->line_table
)
3601 if (! unit
->stmtlist
)
3607 unit
->line_table
= decode_line_info (unit
, stash
);
3609 if (! unit
->line_table
)
3615 if (unit
->first_child_die_ptr
< unit
->end_ptr
3616 && ! scan_unit_for_symbols (unit
))
3623 *function_ptr
= NULL
;
3624 func_p
= lookup_address_in_function_table (unit
, addr
, function_ptr
);
3625 if (func_p
&& (*function_ptr
)->tag
== DW_TAG_inlined_subroutine
)
3626 stash
->inliner_chain
= *function_ptr
;
3628 return lookup_address_in_line_info_table (unit
->line_table
, addr
,
3634 /* Check to see if line info is already decoded in a comp_unit.
3635 If not, decode it. Returns TRUE if no errors were encountered;
3639 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
,
3640 struct dwarf2_debug
*stash
)
3645 if (! unit
->line_table
)
3647 if (! unit
->stmtlist
)
3653 unit
->line_table
= decode_line_info (unit
, stash
);
3655 if (! unit
->line_table
)
3661 if (unit
->first_child_die_ptr
< unit
->end_ptr
3662 && ! scan_unit_for_symbols (unit
))
3672 /* If UNIT contains SYM at ADDR, set the output parameters to the
3673 values for the line containing SYM. The output parameters,
3674 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
3677 Return TRUE if UNIT contains SYM, and no errors were encountered;
3681 comp_unit_find_line (struct comp_unit
*unit
,
3684 const char **filename_ptr
,
3685 unsigned int *linenumber_ptr
,
3686 struct dwarf2_debug
*stash
)
3688 if (!comp_unit_maybe_decode_line_info (unit
, stash
))
3691 if (sym
->flags
& BSF_FUNCTION
)
3692 return lookup_symbol_in_function_table (unit
, sym
, addr
,
3696 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
3701 static struct funcinfo
*
3702 reverse_funcinfo_list (struct funcinfo
*head
)
3704 struct funcinfo
*rhead
;
3705 struct funcinfo
*temp
;
3707 for (rhead
= NULL
; head
; head
= temp
)
3709 temp
= head
->prev_func
;
3710 head
->prev_func
= rhead
;
3716 static struct varinfo
*
3717 reverse_varinfo_list (struct varinfo
*head
)
3719 struct varinfo
*rhead
;
3720 struct varinfo
*temp
;
3722 for (rhead
= NULL
; head
; head
= temp
)
3724 temp
= head
->prev_var
;
3725 head
->prev_var
= rhead
;
3731 /* Extract all interesting funcinfos and varinfos of a compilation
3732 unit into hash tables for faster lookup. Returns TRUE if no
3733 errors were enountered; FALSE otherwise. */
3736 comp_unit_hash_info (struct dwarf2_debug
*stash
,
3737 struct comp_unit
*unit
,
3738 struct info_hash_table
*funcinfo_hash_table
,
3739 struct info_hash_table
*varinfo_hash_table
)
3741 struct funcinfo
* each_func
;
3742 struct varinfo
* each_var
;
3743 bfd_boolean okay
= TRUE
;
3745 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
3747 if (!comp_unit_maybe_decode_line_info (unit
, stash
))
3750 BFD_ASSERT (!unit
->cached
);
3752 /* To preserve the original search order, we went to visit the function
3753 infos in the reversed order of the list. However, making the list
3754 bi-directional use quite a bit of extra memory. So we reverse
3755 the list first, traverse the list in the now reversed order and
3756 finally reverse the list again to get back the original order. */
3757 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
3758 for (each_func
= unit
->function_table
;
3760 each_func
= each_func
->prev_func
)
3762 /* Skip nameless functions. */
3763 if (each_func
->name
)
3764 /* There is no need to copy name string into hash table as
3765 name string is either in the dwarf string buffer or
3766 info in the stash. */
3767 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
3768 (void*) each_func
, FALSE
);
3770 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
3774 /* We do the same for variable infos. */
3775 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
3776 for (each_var
= unit
->variable_table
;
3778 each_var
= each_var
->prev_var
)
3780 /* Skip stack vars and vars with no files or names. */
3781 if (each_var
->stack
== 0
3782 && each_var
->file
!= NULL
3783 && each_var
->name
!= NULL
)
3784 /* There is no need to copy name string into hash table as
3785 name string is either in the dwarf string buffer or
3786 info in the stash. */
3787 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
3788 (void*) each_var
, FALSE
);
3791 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
3792 unit
->cached
= TRUE
;
3796 /* Locate a section in a BFD containing debugging info. The search starts
3797 from the section after AFTER_SEC, or from the first section in the BFD if
3798 AFTER_SEC is NULL. The search works by examining the names of the
3799 sections. There are three permissiable names. The first two are given
3800 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
3801 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
3802 This is a variation on the .debug_info section which has a checksum
3803 describing the contents appended onto the name. This allows the linker to
3804 identify and discard duplicate debugging sections for different
3805 compilation units. */
3806 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
3809 find_debug_info (bfd
*abfd
, const struct dwarf_debug_section
*debug_sections
,
3810 asection
*after_sec
)
3815 if (after_sec
== NULL
)
3817 look
= debug_sections
[debug_info
].uncompressed_name
;
3818 msec
= bfd_get_section_by_name (abfd
, look
);
3822 look
= debug_sections
[debug_info
].compressed_name
;
3825 msec
= bfd_get_section_by_name (abfd
, look
);
3830 for (msec
= abfd
->sections
; msec
!= NULL
; msec
= msec
->next
)
3831 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
3837 for (msec
= after_sec
->next
; msec
!= NULL
; msec
= msec
->next
)
3839 look
= debug_sections
[debug_info
].uncompressed_name
;
3840 if (strcmp (msec
->name
, look
) == 0)
3843 look
= debug_sections
[debug_info
].compressed_name
;
3844 if (look
!= NULL
&& strcmp (msec
->name
, look
) == 0)
3847 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
3854 /* Transfer VMAs from object file to separate debug file. */
3857 set_debug_vma (bfd
*orig_bfd
, bfd
*debug_bfd
)
3861 for (s
= orig_bfd
->sections
, d
= debug_bfd
->sections
;
3862 s
!= NULL
&& d
!= NULL
;
3863 s
= s
->next
, d
= d
->next
)
3865 if ((d
->flags
& SEC_DEBUGGING
) != 0)
3867 /* ??? Assumes 1-1 correspondence between sections in the
3869 if (strcmp (s
->name
, d
->name
) == 0)
3871 d
->output_section
= s
->output_section
;
3872 d
->output_offset
= s
->output_offset
;
3878 /* Unset vmas for adjusted sections in STASH. */
3881 unset_sections (struct dwarf2_debug
*stash
)
3884 struct adjusted_section
*p
;
3886 i
= stash
->adjusted_section_count
;
3887 p
= stash
->adjusted_sections
;
3888 for (; i
> 0; i
--, p
++)
3889 p
->section
->vma
= 0;
3892 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
3893 relocatable object file. VMAs are normally all zero in relocatable
3894 object files, so if we want to distinguish locations in sections by
3895 address we need to set VMAs so the sections do not overlap. We
3896 also set VMA on .debug_info so that when we have multiple
3897 .debug_info sections (or the linkonce variant) they also do not
3898 overlap. The multiple .debug_info sections make up a single
3899 logical section. ??? We should probably do the same for other
3903 place_sections (bfd
*orig_bfd
, struct dwarf2_debug
*stash
)
3906 struct adjusted_section
*p
;
3908 const char *debug_info_name
;
3910 if (stash
->adjusted_section_count
!= 0)
3912 i
= stash
->adjusted_section_count
;
3913 p
= stash
->adjusted_sections
;
3914 for (; i
> 0; i
--, p
++)
3915 p
->section
->vma
= p
->adj_vma
;
3919 debug_info_name
= stash
->debug_sections
[debug_info
].uncompressed_name
;
3926 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
3930 if ((sect
->output_section
!= NULL
3931 && sect
->output_section
!= sect
3932 && (sect
->flags
& SEC_DEBUGGING
) == 0)
3936 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
3937 || CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
));
3939 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
3945 if (abfd
== stash
->bfd_ptr
)
3947 abfd
= stash
->bfd_ptr
;
3951 stash
->adjusted_section_count
= -1;
3954 bfd_vma last_vma
= 0, last_dwarf
= 0;
3955 bfd_size_type amt
= i
* sizeof (struct adjusted_section
);
3957 p
= (struct adjusted_section
*) bfd_malloc (amt
);
3961 stash
->adjusted_sections
= p
;
3962 stash
->adjusted_section_count
= i
;
3969 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
3974 if ((sect
->output_section
!= NULL
3975 && sect
->output_section
!= sect
3976 && (sect
->flags
& SEC_DEBUGGING
) == 0)
3980 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
3981 || CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
));
3983 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
3987 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
3991 BFD_ASSERT (sect
->alignment_power
== 0);
3992 sect
->vma
= last_dwarf
;
3997 /* Align the new address to the current section
3999 last_vma
= ((last_vma
4000 + ~(-((bfd_vma
) 1 << sect
->alignment_power
)))
4001 & (-((bfd_vma
) 1 << sect
->alignment_power
)));
4002 sect
->vma
= last_vma
;
4007 p
->adj_vma
= sect
->vma
;
4010 if (abfd
== stash
->bfd_ptr
)
4012 abfd
= stash
->bfd_ptr
;
4016 if (orig_bfd
!= stash
->bfd_ptr
)
4017 set_debug_vma (orig_bfd
, stash
->bfd_ptr
);
4022 /* Look up a funcinfo by name using the given info hash table. If found,
4023 also update the locations pointed to by filename_ptr and linenumber_ptr.
4025 This function returns TRUE if a funcinfo that matches the given symbol
4026 and address is found with any error; otherwise it returns FALSE. */
4029 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
4032 const char **filename_ptr
,
4033 unsigned int *linenumber_ptr
)
4035 struct funcinfo
* each_func
;
4036 struct funcinfo
* best_fit
= NULL
;
4037 bfd_vma best_fit_len
= 0;
4038 struct info_list_node
*node
;
4039 struct arange
*arange
;
4040 const char *name
= bfd_asymbol_name (sym
);
4041 asection
*sec
= bfd_get_section (sym
);
4043 for (node
= lookup_info_hash_table (hash_table
, name
);
4047 each_func
= (struct funcinfo
*) node
->info
;
4048 for (arange
= &each_func
->arange
;
4050 arange
= arange
->next
)
4052 if ((!each_func
->sec
|| each_func
->sec
== sec
)
4053 && addr
>= arange
->low
4054 && addr
< arange
->high
4056 || arange
->high
- arange
->low
< best_fit_len
))
4058 best_fit
= each_func
;
4059 best_fit_len
= arange
->high
- arange
->low
;
4066 best_fit
->sec
= sec
;
4067 *filename_ptr
= best_fit
->file
;
4068 *linenumber_ptr
= best_fit
->line
;
4075 /* Look up a varinfo by name using the given info hash table. If found,
4076 also update the locations pointed to by filename_ptr and linenumber_ptr.
4078 This function returns TRUE if a varinfo that matches the given symbol
4079 and address is found with any error; otherwise it returns FALSE. */
4082 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
4085 const char **filename_ptr
,
4086 unsigned int *linenumber_ptr
)
4088 const char *name
= bfd_asymbol_name (sym
);
4089 asection
*sec
= bfd_get_section (sym
);
4090 struct varinfo
* each
;
4091 struct info_list_node
*node
;
4093 for (node
= lookup_info_hash_table (hash_table
, name
);
4097 each
= (struct varinfo
*) node
->info
;
4098 if (each
->addr
== addr
4099 && (!each
->sec
|| each
->sec
== sec
))
4102 *filename_ptr
= each
->file
;
4103 *linenumber_ptr
= each
->line
;
4111 /* Update the funcinfo and varinfo info hash tables if they are
4112 not up to date. Returns TRUE if there is no error; otherwise
4113 returns FALSE and disable the info hash tables. */
4116 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
4118 struct comp_unit
*each
;
4120 /* Exit if hash tables are up-to-date. */
4121 if (stash
->all_comp_units
== stash
->hash_units_head
)
4124 if (stash
->hash_units_head
)
4125 each
= stash
->hash_units_head
->prev_unit
;
4127 each
= stash
->last_comp_unit
;
4131 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
4132 stash
->varinfo_hash_table
))
4134 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
4137 each
= each
->prev_unit
;
4140 stash
->hash_units_head
= stash
->all_comp_units
;
4144 /* Check consistency of info hash tables. This is for debugging only. */
4146 static void ATTRIBUTE_UNUSED
4147 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
4149 struct comp_unit
*each_unit
;
4150 struct funcinfo
*each_func
;
4151 struct varinfo
*each_var
;
4152 struct info_list_node
*node
;
4155 for (each_unit
= stash
->all_comp_units
;
4157 each_unit
= each_unit
->next_unit
)
4159 for (each_func
= each_unit
->function_table
;
4161 each_func
= each_func
->prev_func
)
4163 if (!each_func
->name
)
4165 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
4169 while (node
&& !found
)
4171 found
= node
->info
== each_func
;
4177 for (each_var
= each_unit
->variable_table
;
4179 each_var
= each_var
->prev_var
)
4181 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
4183 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
4187 while (node
&& !found
)
4189 found
= node
->info
== each_var
;
4197 /* Check to see if we want to enable the info hash tables, which consume
4198 quite a bit of memory. Currently we only check the number times
4199 bfd_dwarf2_find_line is called. In the future, we may also want to
4200 take the number of symbols into account. */
4203 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
4205 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
4207 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
4210 /* FIXME: Maybe we should check the reduce_memory_overheads
4211 and optimize fields in the bfd_link_info structure ? */
4213 /* Create hash tables. */
4214 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
4215 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
4216 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
4218 /* Turn off info hashes if any allocation above fails. */
4219 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
4222 /* We need a forced update so that the info hash tables will
4223 be created even though there is no compilation unit. That
4224 happens if STASH_INFO_HASH_TRIGGER is 0. */
4225 stash_maybe_update_info_hash_tables (stash
);
4226 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
4229 /* Find the file and line associated with a symbol and address using the
4230 info hash tables of a stash. If there is a match, the function returns
4231 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
4232 otherwise it returns FALSE. */
4235 stash_find_line_fast (struct dwarf2_debug
*stash
,
4238 const char **filename_ptr
,
4239 unsigned int *linenumber_ptr
)
4241 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
4243 if (sym
->flags
& BSF_FUNCTION
)
4244 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
4245 filename_ptr
, linenumber_ptr
);
4246 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
4247 filename_ptr
, linenumber_ptr
);
4250 /* Save current section VMAs. */
4253 save_section_vma (const bfd
*abfd
, struct dwarf2_debug
*stash
)
4258 if (abfd
->section_count
== 0)
4260 stash
->sec_vma
= bfd_malloc (sizeof (*stash
->sec_vma
) * abfd
->section_count
);
4261 if (stash
->sec_vma
== NULL
)
4263 for (i
= 0, s
= abfd
->sections
; i
< abfd
->section_count
; i
++, s
= s
->next
)
4265 if (s
->output_section
!= NULL
)
4266 stash
->sec_vma
[i
] = s
->output_section
->vma
+ s
->output_offset
;
4268 stash
->sec_vma
[i
] = s
->vma
;
4273 /* Compare current section VMAs against those at the time the stash
4274 was created. If find_nearest_line is used in linker warnings or
4275 errors early in the link process, the debug info stash will be
4276 invalid for later calls. This is because we relocate debug info
4277 sections, so the stashed section contents depend on symbol values,
4278 which in turn depend on section VMAs. */
4281 section_vma_same (const bfd
*abfd
, const struct dwarf2_debug
*stash
)
4286 for (i
= 0, s
= abfd
->sections
; i
< abfd
->section_count
; i
++, s
= s
->next
)
4290 if (s
->output_section
!= NULL
)
4291 vma
= s
->output_section
->vma
+ s
->output_offset
;
4294 if (vma
!= stash
->sec_vma
[i
])
4300 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
4301 If DEBUG_BFD is not specified, we read debug information from ABFD
4302 or its gnu_debuglink. The results will be stored in PINFO.
4303 The function returns TRUE iff debug information is ready. */
4306 _bfd_dwarf2_slurp_debug_info (bfd
*abfd
, bfd
*debug_bfd
,
4307 const struct dwarf_debug_section
*debug_sections
,
4310 bfd_boolean do_place
)
4312 bfd_size_type amt
= sizeof (struct dwarf2_debug
);
4313 bfd_size_type total_size
;
4315 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
4319 if (stash
->orig_bfd
== abfd
4320 && section_vma_same (abfd
, stash
))
4322 /* Check that we did previously find some debug information
4323 before attempting to make use of it. */
4324 if (stash
->bfd_ptr
!= NULL
)
4326 if (do_place
&& !place_sections (abfd
, stash
))
4333 _bfd_dwarf2_cleanup_debug_info (abfd
, pinfo
);
4334 memset (stash
, 0, amt
);
4338 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
4342 stash
->orig_bfd
= abfd
;
4343 stash
->debug_sections
= debug_sections
;
4344 stash
->syms
= symbols
;
4345 if (!save_section_vma (abfd
, stash
))
4350 if (debug_bfd
== NULL
)
4353 msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4354 if (msec
== NULL
&& abfd
== debug_bfd
)
4356 char * debug_filename
;
4358 debug_filename
= bfd_follow_build_id_debuglink (abfd
, DEBUGDIR
);
4359 if (debug_filename
== NULL
)
4360 debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
4362 if (debug_filename
== NULL
)
4363 /* No dwarf2 info, and no gnu_debuglink to follow.
4364 Note that at this point the stash has been allocated, but
4365 contains zeros. This lets future calls to this function
4366 fail more quickly. */
4369 /* Set BFD_DECOMPRESS to decompress debug sections. */
4370 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
4371 || !(debug_bfd
->flags
|= BFD_DECOMPRESS
,
4372 bfd_check_format (debug_bfd
, bfd_object
))
4373 || (msec
= find_debug_info (debug_bfd
,
4374 debug_sections
, NULL
)) == NULL
4375 || !bfd_generic_link_read_symbols (debug_bfd
))
4378 bfd_close (debug_bfd
);
4379 /* FIXME: Should we report our failure to follow the debuglink ? */
4380 free (debug_filename
);
4384 symbols
= bfd_get_outsymbols (debug_bfd
);
4385 stash
->syms
= symbols
;
4386 stash
->close_on_cleanup
= TRUE
;
4388 stash
->bfd_ptr
= debug_bfd
;
4391 && !place_sections (abfd
, stash
))
4394 /* There can be more than one DWARF2 info section in a BFD these
4395 days. First handle the easy case when there's only one. If
4396 there's more than one, try case two: none of the sections is
4397 compressed. In that case, read them all in and produce one
4398 large stash. We do this in two passes - in the first pass we
4399 just accumulate the section sizes, and in the second pass we
4400 read in the section's contents. (The allows us to avoid
4401 reallocing the data as we add sections to the stash.) If
4402 some or all sections are compressed, then do things the slow
4403 way, with a bunch of reallocs. */
4405 if (! find_debug_info (debug_bfd
, debug_sections
, msec
))
4407 /* Case 1: only one info section. */
4408 total_size
= msec
->size
;
4409 if (! read_section (debug_bfd
, &stash
->debug_sections
[debug_info
],
4411 &stash
->info_ptr_memory
, &total_size
))
4416 /* Case 2: multiple sections. */
4417 for (total_size
= 0;
4419 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
4420 total_size
+= msec
->size
;
4422 stash
->info_ptr_memory
= (bfd_byte
*) bfd_malloc (total_size
);
4423 if (stash
->info_ptr_memory
== NULL
)
4427 for (msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4429 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
4437 if (!(bfd_simple_get_relocated_section_contents
4438 (debug_bfd
, msec
, stash
->info_ptr_memory
+ total_size
,
4446 stash
->info_ptr
= stash
->info_ptr_memory
;
4447 stash
->info_ptr_end
= stash
->info_ptr
+ total_size
;
4448 stash
->sec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4449 stash
->sec_info_ptr
= stash
->info_ptr
;
4453 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
4454 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
4455 symbol in SYMBOLS and return the difference between the low_pc and
4456 the symbol's address. Returns 0 if no suitable symbol could be found. */
4459 _bfd_dwarf2_find_symbol_bias (asymbol
** symbols
, void ** pinfo
)
4461 struct dwarf2_debug
*stash
;
4462 struct comp_unit
* unit
;
4464 stash
= (struct dwarf2_debug
*) *pinfo
;
4469 for (unit
= stash
->all_comp_units
; unit
; unit
= unit
->next_unit
)
4471 struct funcinfo
* func
;
4473 if (unit
->function_table
== NULL
)
4475 if (unit
->line_table
== NULL
)
4476 unit
->line_table
= decode_line_info (unit
, stash
);
4477 if (unit
->line_table
!= NULL
)
4478 scan_unit_for_symbols (unit
);
4481 for (func
= unit
->function_table
; func
!= NULL
; func
= func
->prev_func
)
4482 if (func
->name
&& func
->arange
.low
)
4486 /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
4488 for (psym
= symbols
; * psym
!= NULL
; psym
++)
4490 asymbol
* sym
= * psym
;
4492 if (sym
->flags
& BSF_FUNCTION
4493 && sym
->section
!= NULL
4494 && strcmp (sym
->name
, func
->name
) == 0)
4495 return ((bfd_signed_vma
) func
->arange
.low
) -
4496 ((bfd_signed_vma
) (sym
->value
+ sym
->section
->vma
));
4504 /* Find the source code location of SYMBOL. If SYMBOL is NULL
4505 then find the nearest source code location corresponding to
4506 the address SECTION + OFFSET.
4507 Returns TRUE if the line is found without error and fills in
4508 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
4509 NULL the FUNCTIONNAME_PTR is also filled in.
4510 SYMBOLS contains the symbol table for ABFD.
4511 DEBUG_SECTIONS contains the name of the dwarf debug sections.
4512 ADDR_SIZE is the number of bytes in the initial .debug_info length
4513 field and in the abbreviation offset, or zero to indicate that the
4514 default value should be used. */
4517 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
4522 const char **filename_ptr
,
4523 const char **functionname_ptr
,
4524 unsigned int *linenumber_ptr
,
4525 unsigned int *discriminator_ptr
,
4526 const struct dwarf_debug_section
*debug_sections
,
4527 unsigned int addr_size
,
4530 /* Read each compilation unit from the section .debug_info, and check
4531 to see if it contains the address we are searching for. If yes,
4532 lookup the address, and return the line number info. If no, go
4533 on to the next compilation unit.
4535 We keep a list of all the previously read compilation units, and
4536 a pointer to the next un-read compilation unit. Check the
4537 previously read units before reading more. */
4538 struct dwarf2_debug
*stash
;
4539 /* What address are we looking for? */
4541 struct comp_unit
* each
;
4542 struct funcinfo
*function
= NULL
;
4543 bfd_boolean found
= FALSE
;
4544 bfd_boolean do_line
;
4546 *filename_ptr
= NULL
;
4547 if (functionname_ptr
!= NULL
)
4548 *functionname_ptr
= NULL
;
4549 *linenumber_ptr
= 0;
4550 if (discriminator_ptr
)
4551 *discriminator_ptr
= 0;
4553 if (! _bfd_dwarf2_slurp_debug_info (abfd
, NULL
, debug_sections
,
4555 (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0))
4558 stash
= (struct dwarf2_debug
*) *pinfo
;
4560 do_line
= symbol
!= NULL
;
4563 BFD_ASSERT (section
== NULL
&& offset
== 0 && functionname_ptr
== NULL
);
4564 section
= bfd_get_section (symbol
);
4565 addr
= symbol
->value
;
4569 BFD_ASSERT (section
!= NULL
&& functionname_ptr
!= NULL
);
4572 /* If we have no SYMBOL but the section we're looking at is not a
4573 code section, then take a look through the list of symbols to see
4574 if we have a symbol at the address we're looking for. If we do
4575 then use this to look up line information. This will allow us to
4576 give file and line results for data symbols. We exclude code
4577 symbols here, if we look up a function symbol and then look up the
4578 line information we'll actually return the line number for the
4579 opening '{' rather than the function definition line. This is
4580 because looking up by symbol uses the line table, in which the
4581 first line for a function is usually the opening '{', while
4582 looking up the function by section + offset uses the
4583 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
4584 which will be the line of the function name. */
4585 if (symbols
!= NULL
&& (section
->flags
& SEC_CODE
) == 0)
4589 for (tmp
= symbols
; (*tmp
) != NULL
; ++tmp
)
4590 if ((*tmp
)->the_bfd
== abfd
4591 && (*tmp
)->section
== section
4592 && (*tmp
)->value
== offset
4593 && ((*tmp
)->flags
& BSF_SECTION_SYM
) == 0)
4597 /* For local symbols, keep going in the hope we find a
4599 if ((symbol
->flags
& BSF_GLOBAL
) != 0)
4605 if (section
->output_section
)
4606 addr
+= section
->output_section
->vma
+ section
->output_offset
;
4608 addr
+= section
->vma
;
4610 /* A null info_ptr indicates that there is no dwarf2 info
4611 (or that an error occured while setting up the stash). */
4612 if (! stash
->info_ptr
)
4615 stash
->inliner_chain
= NULL
;
4617 /* Check the previously read comp. units first. */
4620 /* The info hash tables use quite a bit of memory. We may not want to
4621 always use them. We use some heuristics to decide if and when to
4623 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
4624 stash_maybe_enable_info_hash_tables (abfd
, stash
);
4626 /* Keep info hash table up to date if they are available. Note that we
4627 may disable the hash tables if there is any error duing update. */
4628 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
4629 stash_maybe_update_info_hash_tables (stash
);
4631 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
4633 found
= stash_find_line_fast (stash
, symbol
, addr
, filename_ptr
,
4640 /* Check the previously read comp. units first. */
4641 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
4642 if ((symbol
->flags
& BSF_FUNCTION
) == 0
4643 || each
->arange
.high
== 0
4644 || comp_unit_contains_address (each
, addr
))
4646 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
4647 linenumber_ptr
, stash
);
4655 bfd_vma min_range
= (bfd_vma
) -1;
4656 const char * local_filename
= NULL
;
4657 struct funcinfo
*local_function
= NULL
;
4658 unsigned int local_linenumber
= 0;
4659 unsigned int local_discriminator
= 0;
4661 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
4663 bfd_vma range
= (bfd_vma
) -1;
4665 found
= ((each
->arange
.high
== 0
4666 || comp_unit_contains_address (each
, addr
))
4667 && (range
= comp_unit_find_nearest_line (each
, addr
,
4671 & local_discriminator
,
4675 /* PRs 15935 15994: Bogus debug information may have provided us
4676 with an erroneous match. We attempt to counter this by
4677 selecting the match that has the smallest address range
4678 associated with it. (We are assuming that corrupt debug info
4679 will tend to result in extra large address ranges rather than
4680 extra small ranges).
4682 This does mean that we scan through all of the CUs associated
4683 with the bfd each time this function is called. But this does
4684 have the benefit of producing consistent results every time the
4685 function is called. */
4686 if (range
<= min_range
)
4688 if (filename_ptr
&& local_filename
)
4689 * filename_ptr
= local_filename
;
4691 function
= local_function
;
4692 if (discriminator_ptr
&& local_discriminator
)
4693 * discriminator_ptr
= local_discriminator
;
4694 if (local_linenumber
)
4695 * linenumber_ptr
= local_linenumber
;
4701 if (* linenumber_ptr
)
4708 /* The DWARF2 spec says that the initial length field, and the
4709 offset of the abbreviation table, should both be 4-byte values.
4710 However, some compilers do things differently. */
4713 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
4715 /* Read each remaining comp. units checking each as they are read. */
4716 while (stash
->info_ptr
< stash
->info_ptr_end
)
4719 unsigned int offset_size
= addr_size
;
4720 bfd_byte
*info_ptr_unit
= stash
->info_ptr
;
4722 length
= read_4_bytes (stash
->bfd_ptr
, stash
->info_ptr
, stash
->info_ptr_end
);
4723 /* A 0xffffff length is the DWARF3 way of indicating
4724 we use 64-bit offsets, instead of 32-bit offsets. */
4725 if (length
== 0xffffffff)
4728 length
= read_8_bytes (stash
->bfd_ptr
, stash
->info_ptr
+ 4, stash
->info_ptr_end
);
4729 stash
->info_ptr
+= 12;
4731 /* A zero length is the IRIX way of indicating 64-bit offsets,
4732 mostly because the 64-bit length will generally fit in 32
4733 bits, and the endianness helps. */
4734 else if (length
== 0)
4737 length
= read_4_bytes (stash
->bfd_ptr
, stash
->info_ptr
+ 4, stash
->info_ptr_end
);
4738 stash
->info_ptr
+= 8;
4740 /* In the absence of the hints above, we assume 32-bit DWARF2
4741 offsets even for targets with 64-bit addresses, because:
4742 a) most of the time these targets will not have generated
4743 more than 2Gb of debug info and so will not need 64-bit
4746 b) if they do use 64-bit offsets but they are not using
4747 the size hints that are tested for above then they are
4748 not conforming to the DWARF3 standard anyway. */
4749 else if (addr_size
== 8)
4752 stash
->info_ptr
+= 4;
4755 stash
->info_ptr
+= 4;
4762 if (stash
->info_ptr
+ length
> stash
->info_ptr_end
)
4765 each
= parse_comp_unit (stash
, length
, info_ptr_unit
,
4768 /* The dwarf information is damaged, don't trust it any
4772 new_ptr
= stash
->info_ptr
+ length
;
4773 /* PR 17512: file: 1500698c. */
4774 if (new_ptr
< stash
->info_ptr
)
4776 /* A corrupt length value - do not trust the info any more. */
4781 stash
->info_ptr
= new_ptr
;
4783 if (stash
->all_comp_units
)
4784 stash
->all_comp_units
->prev_unit
= each
;
4786 stash
->last_comp_unit
= each
;
4788 each
->next_unit
= stash
->all_comp_units
;
4789 stash
->all_comp_units
= each
;
4791 /* DW_AT_low_pc and DW_AT_high_pc are optional for
4792 compilation units. If we don't have them (i.e.,
4793 unit->high == 0), we need to consult the line info table
4794 to see if a compilation unit contains the given
4797 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
4798 || each
->arange
.high
== 0
4799 || comp_unit_contains_address (each
, addr
))
4800 && comp_unit_find_line (each
, symbol
, addr
,
4805 found
= ((each
->arange
.high
== 0
4806 || comp_unit_contains_address (each
, addr
))
4807 && comp_unit_find_nearest_line (each
, addr
,
4814 if ((bfd_vma
) (stash
->info_ptr
- stash
->sec_info_ptr
)
4815 == stash
->sec
->size
)
4817 stash
->sec
= find_debug_info (stash
->bfd_ptr
, debug_sections
,
4819 stash
->sec_info_ptr
= stash
->info_ptr
;
4830 if (!function
->is_linkage
)
4835 fun
= _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
4836 *filename_ptr
? NULL
: filename_ptr
,
4838 sec_vma
= section
->vma
;
4839 if (section
->output_section
!= NULL
)
4840 sec_vma
= section
->output_section
->vma
+ section
->output_offset
;
4842 && fun
->value
+ sec_vma
== function
->arange
.low
)
4843 function
->name
= *functionname_ptr
;
4844 /* Even if we didn't find a linkage name, say that we have
4845 to stop a repeated search of symbols. */
4846 function
->is_linkage
= TRUE
;
4848 *functionname_ptr
= function
->name
;
4850 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
4851 unset_sections (stash
);
4857 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
4858 const char **filename_ptr
,
4859 const char **functionname_ptr
,
4860 unsigned int *linenumber_ptr
,
4863 struct dwarf2_debug
*stash
;
4865 stash
= (struct dwarf2_debug
*) *pinfo
;
4868 struct funcinfo
*func
= stash
->inliner_chain
;
4870 if (func
&& func
->caller_func
)
4872 *filename_ptr
= func
->caller_file
;
4873 *functionname_ptr
= func
->caller_func
->name
;
4874 *linenumber_ptr
= func
->caller_line
;
4875 stash
->inliner_chain
= func
->caller_func
;
4884 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
, void **pinfo
)
4886 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
4887 struct comp_unit
*each
;
4889 if (abfd
== NULL
|| stash
== NULL
)
4892 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
4894 struct abbrev_info
**abbrevs
= each
->abbrevs
;
4895 struct funcinfo
*function_table
= each
->function_table
;
4896 struct varinfo
*variable_table
= each
->variable_table
;
4899 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
4901 struct abbrev_info
*abbrev
= abbrevs
[i
];
4905 free (abbrev
->attrs
);
4906 abbrev
= abbrev
->next
;
4910 if (each
->line_table
)
4912 free (each
->line_table
->dirs
);
4913 free (each
->line_table
->files
);
4916 while (function_table
)
4918 if (function_table
->file
)
4920 free (function_table
->file
);
4921 function_table
->file
= NULL
;
4924 if (function_table
->caller_file
)
4926 free (function_table
->caller_file
);
4927 function_table
->caller_file
= NULL
;
4929 function_table
= function_table
->prev_func
;
4932 if (each
->lookup_funcinfo_table
)
4934 free (each
->lookup_funcinfo_table
);
4935 each
->lookup_funcinfo_table
= NULL
;
4938 while (variable_table
)
4940 if (variable_table
->file
)
4942 free (variable_table
->file
);
4943 variable_table
->file
= NULL
;
4946 variable_table
= variable_table
->prev_var
;
4950 if (stash
->funcinfo_hash_table
)
4951 bfd_hash_table_free (&stash
->funcinfo_hash_table
->base
);
4952 if (stash
->varinfo_hash_table
)
4953 bfd_hash_table_free (&stash
->varinfo_hash_table
->base
);
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
);