2 Copyright (C) 1994-2020 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"
40 /* The data in the .debug_line statement prologue looks like this. */
45 unsigned short version
;
46 bfd_vma prologue_length
;
47 unsigned char minimum_instruction_length
;
48 unsigned char maximum_ops_per_insn
;
49 unsigned char default_is_stmt
;
51 unsigned char line_range
;
52 unsigned char opcode_base
;
53 unsigned char *standard_opcode_lengths
;
56 /* Attributes have a name and a value. */
60 enum dwarf_attribute name
;
65 struct dwarf_block
*blk
;
72 /* Blocks are a bunch of untyped bytes. */
79 struct adjusted_section
85 struct dwarf2_debug_file
87 /* The actual bfd from which debug info was loaded. Might be
88 different to orig_bfd because of gnu_debuglink sections. */
91 /* Pointer to the symbol table. */
94 /* The current info pointer for the .debug_info section being parsed. */
97 /* A pointer to the memory block allocated for .debug_info sections. */
98 bfd_byte
*dwarf_info_buffer
;
100 /* Length of the loaded .debug_info sections. */
101 bfd_size_type dwarf_info_size
;
103 /* Pointer to the .debug_abbrev section loaded into memory. */
104 bfd_byte
*dwarf_abbrev_buffer
;
106 /* Length of the loaded .debug_abbrev section. */
107 bfd_size_type dwarf_abbrev_size
;
109 /* Buffer for decode_line_info. */
110 bfd_byte
*dwarf_line_buffer
;
112 /* Length of the loaded .debug_line section. */
113 bfd_size_type dwarf_line_size
;
115 /* Pointer to the .debug_str section loaded into memory. */
116 bfd_byte
*dwarf_str_buffer
;
118 /* Length of the loaded .debug_str section. */
119 bfd_size_type dwarf_str_size
;
121 /* Pointer to the .debug_line_str section loaded into memory. */
122 bfd_byte
*dwarf_line_str_buffer
;
124 /* Length of the loaded .debug_line_str section. */
125 bfd_size_type dwarf_line_str_size
;
127 /* Pointer to the .debug_ranges section loaded into memory. */
128 bfd_byte
*dwarf_ranges_buffer
;
130 /* Length of the loaded .debug_ranges section. */
131 bfd_size_type dwarf_ranges_size
;
133 /* A list of all previously read comp_units. */
134 struct comp_unit
*all_comp_units
;
136 /* Last comp unit in list above. */
137 struct comp_unit
*last_comp_unit
;
139 /* Line table at line_offset zero. */
140 struct line_info_table
*line_table
;
142 /* Hash table to map offsets to decoded abbrevs. */
143 htab_t abbrev_offsets
;
148 /* Names of the debug sections. */
149 const struct dwarf_debug_section
*debug_sections
;
151 /* Per-file stuff. */
152 struct dwarf2_debug_file f
, alt
;
154 /* Pointer to the original bfd for which debug was loaded. This is what
155 we use to compare and so check that the cached debug data is still
156 valid - it saves having to possibly dereference the gnu_debuglink each
160 /* If the most recent call to bfd_find_nearest_line was given an
161 address in an inlined function, preserve a pointer into the
162 calling chain for subsequent calls to bfd_find_inliner_info to
164 struct funcinfo
*inliner_chain
;
166 /* Section VMAs at the time the stash was built. */
168 /* Number of sections in the SEC_VMA table. */
169 unsigned int sec_vma_count
;
171 /* Number of sections whose VMA we must adjust. */
172 int adjusted_section_count
;
174 /* Array of sections with adjusted VMA. */
175 struct adjusted_section
*adjusted_sections
;
177 /* Number of times find_line is called. This is used in
178 the heuristic for enabling the info hash tables. */
181 #define STASH_INFO_HASH_TRIGGER 100
183 /* Hash table mapping symbol names to function infos. */
184 struct info_hash_table
*funcinfo_hash_table
;
186 /* Hash table mapping symbol names to variable infos. */
187 struct info_hash_table
*varinfo_hash_table
;
189 /* Head of comp_unit list in the last hash table update. */
190 struct comp_unit
*hash_units_head
;
192 /* Status of info hash. */
193 int info_hash_status
;
194 #define STASH_INFO_HASH_OFF 0
195 #define STASH_INFO_HASH_ON 1
196 #define STASH_INFO_HASH_DISABLED 2
198 /* True if we opened bfd_ptr. */
199 bfd_boolean close_on_cleanup
;
209 /* A minimal decoding of DWARF2 compilation units. We only decode
210 what's needed to get to the line number information. */
214 /* Chain the previously read compilation units. */
215 struct comp_unit
*next_unit
;
217 /* Likewise, chain the compilation unit read after this one.
218 The comp units are stored in reversed reading order. */
219 struct comp_unit
*prev_unit
;
221 /* Keep the bfd convenient (for memory allocation). */
224 /* The lowest and highest addresses contained in this compilation
225 unit as specified in the compilation unit header. */
226 struct arange arange
;
228 /* The DW_AT_name attribute (for error messages). */
231 /* The abbrev hash table. */
232 struct abbrev_info
**abbrevs
;
234 /* DW_AT_language. */
237 /* Note that an error was found by comp_unit_find_nearest_line. */
240 /* The DW_AT_comp_dir attribute. */
243 /* TRUE if there is a line number table associated with this comp. unit. */
246 /* Pointer to the current comp_unit so that we can find a given entry
248 bfd_byte
*info_ptr_unit
;
250 /* The offset into .debug_line of the line number table. */
251 unsigned long line_offset
;
253 /* Pointer to the first child die for the comp unit. */
254 bfd_byte
*first_child_die_ptr
;
256 /* The end of the comp unit. */
259 /* The decoded line number, NULL if not yet decoded. */
260 struct line_info_table
*line_table
;
262 /* A list of the functions found in this comp. unit. */
263 struct funcinfo
*function_table
;
265 /* A table of function information references searchable by address. */
266 struct lookup_funcinfo
*lookup_funcinfo_table
;
268 /* Number of functions in the function_table and sorted_function_table. */
269 bfd_size_type number_of_functions
;
271 /* A list of the variables found in this comp. unit. */
272 struct varinfo
*variable_table
;
274 /* Pointers to dwarf2_debug structures. */
275 struct dwarf2_debug
*stash
;
276 struct dwarf2_debug_file
*file
;
278 /* DWARF format version for this unit - from unit header. */
281 /* Address size for this unit - from unit header. */
282 unsigned char addr_size
;
284 /* Offset size for this unit - from unit header. */
285 unsigned char offset_size
;
287 /* Base address for this unit - from DW_AT_low_pc attribute of
288 DW_TAG_compile_unit DIE */
289 bfd_vma base_address
;
291 /* TRUE if symbols are cached in hash table for faster lookup by name. */
295 /* This data structure holds the information of an abbrev. */
298 unsigned int number
; /* Number identifying abbrev. */
299 enum dwarf_tag tag
; /* DWARF tag. */
300 bfd_boolean has_children
; /* TRUE if the abbrev has children. */
301 unsigned int num_attrs
; /* Number of attributes. */
302 struct attr_abbrev
* attrs
; /* An array of attribute descriptions. */
303 struct abbrev_info
* next
; /* Next in chain. */
308 enum dwarf_attribute name
;
309 enum dwarf_form form
;
310 bfd_vma implicit_const
;
313 /* Map of uncompressed DWARF debug section name to compressed one. It
314 is terminated by NULL uncompressed_name. */
316 const struct dwarf_debug_section dwarf_debug_sections
[] =
318 { ".debug_abbrev", ".zdebug_abbrev" },
319 { ".debug_aranges", ".zdebug_aranges" },
320 { ".debug_frame", ".zdebug_frame" },
321 { ".debug_info", ".zdebug_info" },
322 { ".debug_info", ".zdebug_info" },
323 { ".debug_line", ".zdebug_line" },
324 { ".debug_loc", ".zdebug_loc" },
325 { ".debug_macinfo", ".zdebug_macinfo" },
326 { ".debug_macro", ".zdebug_macro" },
327 { ".debug_pubnames", ".zdebug_pubnames" },
328 { ".debug_pubtypes", ".zdebug_pubtypes" },
329 { ".debug_ranges", ".zdebug_ranges" },
330 { ".debug_static_func", ".zdebug_static_func" },
331 { ".debug_static_vars", ".zdebug_static_vars" },
332 { ".debug_str", ".zdebug_str", },
333 { ".debug_str", ".zdebug_str", },
334 { ".debug_line_str", ".zdebug_line_str", },
335 { ".debug_types", ".zdebug_types" },
336 /* GNU DWARF 1 extensions */
337 { ".debug_sfnames", ".zdebug_sfnames" },
338 { ".debug_srcinfo", ".zebug_srcinfo" },
339 /* SGI/MIPS DWARF 2 extensions */
340 { ".debug_funcnames", ".zdebug_funcnames" },
341 { ".debug_typenames", ".zdebug_typenames" },
342 { ".debug_varnames", ".zdebug_varnames" },
343 { ".debug_weaknames", ".zdebug_weaknames" },
347 /* NB/ Numbers in this enum must match up with indices
348 into the dwarf_debug_sections[] array above. */
349 enum dwarf_debug_section_enum
378 /* A static assertion. */
379 extern int dwarf_debug_section_assert
[ARRAY_SIZE (dwarf_debug_sections
)
380 == debug_max
+ 1 ? 1 : -1];
382 #ifndef ABBREV_HASH_SIZE
383 #define ABBREV_HASH_SIZE 121
385 #ifndef ATTR_ALLOC_CHUNK
386 #define ATTR_ALLOC_CHUNK 4
389 /* Variable and function hash tables. This is used to speed up look-up
390 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
391 In order to share code between variable and function infos, we use
392 a list of untyped pointer for all variable/function info associated with
393 a symbol. We waste a bit of memory for list with one node but that
394 simplifies the code. */
396 struct info_list_node
398 struct info_list_node
*next
;
402 /* Info hash entry. */
403 struct info_hash_entry
405 struct bfd_hash_entry root
;
406 struct info_list_node
*head
;
409 struct info_hash_table
411 struct bfd_hash_table base
;
414 /* Function to create a new entry in info hash table. */
416 static struct bfd_hash_entry
*
417 info_hash_table_newfunc (struct bfd_hash_entry
*entry
,
418 struct bfd_hash_table
*table
,
421 struct info_hash_entry
*ret
= (struct info_hash_entry
*) entry
;
423 /* Allocate the structure if it has not already been allocated by a
427 ret
= (struct info_hash_entry
*) bfd_hash_allocate (table
,
433 /* Call the allocation method of the base class. */
434 ret
= ((struct info_hash_entry
*)
435 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
437 /* Initialize the local fields here. */
441 return (struct bfd_hash_entry
*) ret
;
444 /* Function to create a new info hash table. It returns a pointer to the
445 newly created table or NULL if there is any error. We need abfd
446 solely for memory allocation. */
448 static struct info_hash_table
*
449 create_info_hash_table (bfd
*abfd
)
451 struct info_hash_table
*hash_table
;
453 hash_table
= ((struct info_hash_table
*)
454 bfd_alloc (abfd
, sizeof (struct info_hash_table
)));
458 if (!bfd_hash_table_init (&hash_table
->base
, info_hash_table_newfunc
,
459 sizeof (struct info_hash_entry
)))
461 bfd_release (abfd
, hash_table
);
468 /* Insert an info entry into an info hash table. We do not check of
469 duplicate entries. Also, the caller need to guarantee that the
470 right type of info in inserted as info is passed as a void* pointer.
471 This function returns true if there is no error. */
474 insert_info_hash_table (struct info_hash_table
*hash_table
,
479 struct info_hash_entry
*entry
;
480 struct info_list_node
*node
;
482 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
,
487 node
= (struct info_list_node
*) bfd_hash_allocate (&hash_table
->base
,
493 node
->next
= entry
->head
;
499 /* Look up an info entry list from an info hash table. Return NULL
502 static struct info_list_node
*
503 lookup_info_hash_table (struct info_hash_table
*hash_table
, const char *key
)
505 struct info_hash_entry
*entry
;
507 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
, key
,
509 return entry
? entry
->head
: NULL
;
512 /* Read a section into its appropriate place in the dwarf2_debug
513 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
514 not NULL, use bfd_simple_get_relocated_section_contents to read the
515 section contents, otherwise use bfd_get_section_contents. Fail if
516 the located section does not contain at least OFFSET bytes. */
519 read_section (bfd
* abfd
,
520 const struct dwarf_debug_section
*sec
,
523 bfd_byte
** section_buffer
,
524 bfd_size_type
* section_size
)
527 const char *section_name
= sec
->uncompressed_name
;
528 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 amt
= *section_size
+ 1;
555 bfd_set_error (bfd_error_no_memory
);
558 contents
= (bfd_byte
*) bfd_malloc (amt
);
559 if (contents
== NULL
)
562 ? !bfd_simple_get_relocated_section_contents (abfd
, msec
, contents
,
564 : !bfd_get_section_contents (abfd
, msec
, contents
, 0, *section_size
))
569 contents
[*section_size
] = 0;
570 *section_buffer
= contents
;
573 /* It is possible to get a bad value for the offset into the section
574 that the client wants. Validate it here to avoid trouble later. */
575 if (offset
!= 0 && offset
>= *section_size
)
577 /* xgettext: c-format */
578 _bfd_error_handler (_("DWARF error: offset (%" PRIu64
")"
579 " greater than or equal to %s size (%" PRIu64
")"),
580 (uint64_t) offset
, section_name
,
581 (uint64_t) *section_size
);
582 bfd_set_error (bfd_error_bad_value
);
589 /* Read dwarf information from a buffer. */
592 read_1_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
, bfd_byte
*end
)
596 return bfd_get_8 (abfd
, buf
);
600 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
, bfd_byte
*end
)
604 return bfd_get_signed_8 (abfd
, buf
);
608 read_2_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
612 return bfd_get_16 (abfd
, buf
);
616 read_4_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
620 return bfd_get_32 (abfd
, buf
);
624 read_8_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
628 return bfd_get_64 (abfd
, buf
);
632 read_n_bytes (bfd_byte
* buf
,
634 struct dwarf_block
* block
)
636 unsigned int size
= block
->size
;
637 bfd_byte
* block_end
= buf
+ size
;
639 if (block_end
> end
|| block_end
< buf
)
652 /* Scans a NUL terminated string starting at BUF, returning a pointer to it.
653 Returns the number of characters in the string, *including* the NUL byte,
654 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
655 at or beyond BUF_END will not be read. Returns NULL if there was a
656 problem, or if the string is empty. */
659 read_string (bfd
* abfd ATTRIBUTE_UNUSED
,
662 unsigned int * bytes_read_ptr
)
668 * bytes_read_ptr
= 0;
674 * bytes_read_ptr
= 1;
678 while (buf
< buf_end
)
681 * bytes_read_ptr
= buf
- str
;
685 * bytes_read_ptr
= buf
- str
;
689 /* Reads an offset from BUF and then locates the string at this offset
690 inside the debug string section. Returns a pointer to the string.
691 Returns the number of bytes read from BUF, *not* the length of the string,
692 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
693 at or beyond BUF_END will not be read from BUF. Returns NULL if there was
694 a problem, or if the string is empty. Does not check for NUL termination
698 read_indirect_string (struct comp_unit
* unit
,
701 unsigned int * bytes_read_ptr
)
704 struct dwarf2_debug
*stash
= unit
->stash
;
705 struct dwarf2_debug_file
*file
= unit
->file
;
708 if (buf
+ unit
->offset_size
> buf_end
)
710 * bytes_read_ptr
= 0;
714 if (unit
->offset_size
== 4)
715 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
717 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
719 *bytes_read_ptr
= unit
->offset_size
;
721 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
723 &file
->dwarf_str_buffer
, &file
->dwarf_str_size
))
726 str
= (char *) file
->dwarf_str_buffer
+ offset
;
732 /* Like read_indirect_string but from .debug_line_str section. */
735 read_indirect_line_string (struct comp_unit
* unit
,
738 unsigned int * bytes_read_ptr
)
741 struct dwarf2_debug
*stash
= unit
->stash
;
742 struct dwarf2_debug_file
*file
= unit
->file
;
745 if (buf
+ unit
->offset_size
> buf_end
)
747 * bytes_read_ptr
= 0;
751 if (unit
->offset_size
== 4)
752 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
754 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
756 *bytes_read_ptr
= unit
->offset_size
;
758 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_line_str
],
760 &file
->dwarf_line_str_buffer
,
761 &file
->dwarf_line_str_size
))
764 str
= (char *) file
->dwarf_line_str_buffer
+ offset
;
770 /* Like read_indirect_string but uses a .debug_str located in
771 an alternate file pointed to by the .gnu_debugaltlink section.
772 Used to impement DW_FORM_GNU_strp_alt. */
775 read_alt_indirect_string (struct comp_unit
* unit
,
778 unsigned int * bytes_read_ptr
)
781 struct dwarf2_debug
*stash
= unit
->stash
;
784 if (buf
+ unit
->offset_size
> buf_end
)
786 * bytes_read_ptr
= 0;
790 if (unit
->offset_size
== 4)
791 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
793 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
795 *bytes_read_ptr
= unit
->offset_size
;
797 if (stash
->alt
.bfd_ptr
== NULL
)
800 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
802 if (debug_filename
== NULL
)
805 debug_bfd
= bfd_openr (debug_filename
, NULL
);
806 free (debug_filename
);
807 if (debug_bfd
== NULL
)
808 /* FIXME: Should we report our failure to follow the debuglink ? */
811 if (!bfd_check_format (debug_bfd
, bfd_object
))
813 bfd_close (debug_bfd
);
816 stash
->alt
.bfd_ptr
= debug_bfd
;
819 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
820 stash
->debug_sections
+ debug_str_alt
,
821 stash
->alt
.syms
, offset
,
822 &stash
->alt
.dwarf_str_buffer
,
823 &stash
->alt
.dwarf_str_size
))
826 str
= (char *) stash
->alt
.dwarf_str_buffer
+ offset
;
833 /* Resolve an alternate reference from UNIT at OFFSET.
834 Returns a pointer into the loaded alternate CU upon success
835 or NULL upon failure. */
838 read_alt_indirect_ref (struct comp_unit
* unit
,
841 struct dwarf2_debug
*stash
= unit
->stash
;
843 if (stash
->alt
.bfd_ptr
== NULL
)
846 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
848 if (debug_filename
== NULL
)
851 debug_bfd
= bfd_openr (debug_filename
, NULL
);
852 free (debug_filename
);
853 if (debug_bfd
== NULL
)
854 /* FIXME: Should we report our failure to follow the debuglink ? */
857 if (!bfd_check_format (debug_bfd
, bfd_object
))
859 bfd_close (debug_bfd
);
862 stash
->alt
.bfd_ptr
= debug_bfd
;
865 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
866 stash
->debug_sections
+ debug_info_alt
,
867 stash
->alt
.syms
, offset
,
868 &stash
->alt
.dwarf_info_buffer
,
869 &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 /* We keep a hash table to map .debug_abbrev section offsets to the
939 array of abbrevs, so that compilation units using the same set of
940 abbrevs do not waste memory. */
942 struct abbrev_offset_entry
945 struct abbrev_info
**abbrevs
;
949 hash_abbrev (const void *p
)
951 const struct abbrev_offset_entry
*ent
= p
;
952 return htab_hash_pointer ((void *) ent
->offset
);
956 eq_abbrev (const void *pa
, const void *pb
)
958 const struct abbrev_offset_entry
*a
= pa
;
959 const struct abbrev_offset_entry
*b
= pb
;
960 return a
->offset
== b
->offset
;
966 struct abbrev_offset_entry
*ent
= p
;
967 struct abbrev_info
**abbrevs
= ent
->abbrevs
;
970 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
972 struct abbrev_info
*abbrev
= abbrevs
[i
];
976 free (abbrev
->attrs
);
977 abbrev
= abbrev
->next
;
983 /* In DWARF version 2, the description of the debugging information is
984 stored in a separate .debug_abbrev section. Before we read any
985 dies from a section we read in all abbreviations and install them
988 static struct abbrev_info
**
989 read_abbrevs (bfd
*abfd
, bfd_uint64_t offset
, struct dwarf2_debug
*stash
,
990 struct dwarf2_debug_file
*file
)
992 struct abbrev_info
**abbrevs
;
993 bfd_byte
*abbrev_ptr
;
994 bfd_byte
*abbrev_end
;
995 struct abbrev_info
*cur_abbrev
;
996 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
997 unsigned int abbrev_form
, hash_number
;
1000 struct abbrev_offset_entry ent
= { offset
, NULL
};
1002 if (ent
.offset
!= offset
)
1005 slot
= htab_find_slot (file
->abbrev_offsets
, &ent
, INSERT
);
1009 return ((struct abbrev_offset_entry
*) (*slot
))->abbrevs
;
1011 if (! read_section (abfd
, &stash
->debug_sections
[debug_abbrev
],
1013 &file
->dwarf_abbrev_buffer
,
1014 &file
->dwarf_abbrev_size
))
1017 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
1018 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
1019 if (abbrevs
== NULL
)
1022 abbrev_ptr
= file
->dwarf_abbrev_buffer
+ offset
;
1023 abbrev_end
= file
->dwarf_abbrev_buffer
+ file
->dwarf_abbrev_size
;
1024 abbrev_number
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1026 abbrev_ptr
+= bytes_read
;
1028 /* Loop until we reach an abbrev number of 0. */
1029 while (abbrev_number
)
1031 amt
= sizeof (struct abbrev_info
);
1032 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
1033 if (cur_abbrev
== NULL
)
1036 /* Read in abbrev header. */
1037 cur_abbrev
->number
= abbrev_number
;
1038 cur_abbrev
->tag
= (enum dwarf_tag
)
1039 _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1041 abbrev_ptr
+= bytes_read
;
1042 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
, abbrev_end
);
1045 /* Now read in declarations. */
1048 /* Initialize it just to avoid a GCC false warning. */
1049 bfd_vma implicit_const
= -1;
1051 abbrev_name
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1053 abbrev_ptr
+= bytes_read
;
1054 abbrev_form
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1056 abbrev_ptr
+= bytes_read
;
1057 if (abbrev_form
== DW_FORM_implicit_const
)
1059 implicit_const
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
,
1062 abbrev_ptr
+= bytes_read
;
1065 if (abbrev_name
== 0)
1068 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
1070 struct attr_abbrev
*tmp
;
1072 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
1073 amt
*= sizeof (struct attr_abbrev
);
1074 tmp
= (struct attr_abbrev
*) bfd_realloc (cur_abbrev
->attrs
, amt
);
1077 cur_abbrev
->attrs
= tmp
;
1080 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
1081 = (enum dwarf_attribute
) abbrev_name
;
1082 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].form
1083 = (enum dwarf_form
) abbrev_form
;
1084 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].implicit_const
1086 ++cur_abbrev
->num_attrs
;
1089 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
1090 cur_abbrev
->next
= abbrevs
[hash_number
];
1091 abbrevs
[hash_number
] = cur_abbrev
;
1093 /* Get next abbreviation.
1094 Under Irix6 the abbreviations for a compilation unit are not
1095 always properly terminated with an abbrev number of 0.
1096 Exit loop if we encounter an abbreviation which we have
1097 already read (which means we are about to read the abbreviations
1098 for the next compile unit) or if the end of the abbreviation
1099 table is reached. */
1100 if ((size_t) (abbrev_ptr
- file
->dwarf_abbrev_buffer
)
1101 >= file
->dwarf_abbrev_size
)
1103 abbrev_number
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
,
1104 &bytes_read
, FALSE
, abbrev_end
);
1105 abbrev_ptr
+= bytes_read
;
1106 if (lookup_abbrev (abbrev_number
, abbrevs
) != NULL
)
1110 *slot
= bfd_malloc (sizeof ent
);
1113 ent
.abbrevs
= abbrevs
;
1114 memcpy (*slot
, &ent
, sizeof ent
);
1118 if (abbrevs
!= NULL
)
1122 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1124 struct abbrev_info
*abbrev
= abbrevs
[i
];
1128 free (abbrev
->attrs
);
1129 abbrev
= abbrev
->next
;
1137 /* Returns true if the form is one which has a string value. */
1139 static inline bfd_boolean
1140 is_str_attr (enum dwarf_form form
)
1142 return (form
== DW_FORM_string
|| form
== DW_FORM_strp
1143 || form
== DW_FORM_line_strp
|| form
== DW_FORM_GNU_strp_alt
);
1146 /* Read and fill in the value of attribute ATTR as described by FORM.
1147 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1148 Returns an updated INFO_PTR taking into account the amount of data read. */
1151 read_attribute_value (struct attribute
* attr
,
1153 bfd_vma implicit_const
,
1154 struct comp_unit
* unit
,
1155 bfd_byte
* info_ptr
,
1156 bfd_byte
* info_ptr_end
)
1158 bfd
*abfd
= unit
->abfd
;
1159 unsigned int bytes_read
;
1160 struct dwarf_block
*blk
;
1163 if (info_ptr
>= info_ptr_end
&& form
!= DW_FORM_flag_present
)
1165 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1166 bfd_set_error (bfd_error_bad_value
);
1170 attr
->form
= (enum dwarf_form
) form
;
1174 case DW_FORM_ref_addr
:
1175 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1177 if (unit
->version
== 3 || unit
->version
== 4)
1179 if (unit
->offset_size
== 4)
1180 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1182 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1183 info_ptr
+= unit
->offset_size
;
1188 attr
->u
.val
= read_address (unit
, info_ptr
, info_ptr_end
);
1189 info_ptr
+= unit
->addr_size
;
1191 case DW_FORM_GNU_ref_alt
:
1192 case DW_FORM_sec_offset
:
1193 if (unit
->offset_size
== 4)
1194 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1196 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1197 info_ptr
+= unit
->offset_size
;
1199 case DW_FORM_block2
:
1200 amt
= sizeof (struct dwarf_block
);
1201 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1204 blk
->size
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1206 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1209 case DW_FORM_block4
:
1210 amt
= sizeof (struct dwarf_block
);
1211 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1214 blk
->size
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1216 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1220 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1224 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1228 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1231 case DW_FORM_string
:
1232 attr
->u
.str
= read_string (abfd
, info_ptr
, info_ptr_end
, &bytes_read
);
1233 info_ptr
+= bytes_read
;
1236 attr
->u
.str
= read_indirect_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1237 info_ptr
+= bytes_read
;
1239 case DW_FORM_line_strp
:
1240 attr
->u
.str
= read_indirect_line_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1241 info_ptr
+= bytes_read
;
1243 case DW_FORM_GNU_strp_alt
:
1244 attr
->u
.str
= read_alt_indirect_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1245 info_ptr
+= bytes_read
;
1247 case DW_FORM_exprloc
:
1249 amt
= sizeof (struct dwarf_block
);
1250 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1253 blk
->size
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1254 FALSE
, info_ptr_end
);
1255 info_ptr
+= bytes_read
;
1256 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1259 case DW_FORM_block1
:
1260 amt
= sizeof (struct dwarf_block
);
1261 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1264 blk
->size
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1266 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1270 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1274 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1277 case DW_FORM_flag_present
:
1281 attr
->u
.sval
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1282 TRUE
, info_ptr_end
);
1283 info_ptr
+= bytes_read
;
1286 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1287 FALSE
, info_ptr_end
);
1288 info_ptr
+= bytes_read
;
1291 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1295 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1299 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1303 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1306 case DW_FORM_ref_sig8
:
1307 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1310 case DW_FORM_ref_udata
:
1311 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1312 FALSE
, info_ptr_end
);
1313 info_ptr
+= bytes_read
;
1315 case DW_FORM_indirect
:
1316 form
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1317 FALSE
, info_ptr_end
);
1318 info_ptr
+= bytes_read
;
1319 if (form
== DW_FORM_implicit_const
)
1321 implicit_const
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1322 TRUE
, info_ptr_end
);
1323 info_ptr
+= bytes_read
;
1325 info_ptr
= read_attribute_value (attr
, form
, implicit_const
, unit
,
1326 info_ptr
, info_ptr_end
);
1328 case DW_FORM_implicit_const
:
1329 attr
->form
= DW_FORM_sdata
;
1330 attr
->u
.sval
= implicit_const
;
1333 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1335 bfd_set_error (bfd_error_bad_value
);
1341 /* Read an attribute described by an abbreviated attribute. */
1344 read_attribute (struct attribute
* attr
,
1345 struct attr_abbrev
* abbrev
,
1346 struct comp_unit
* unit
,
1347 bfd_byte
* info_ptr
,
1348 bfd_byte
* info_ptr_end
)
1350 attr
->name
= abbrev
->name
;
1351 info_ptr
= read_attribute_value (attr
, abbrev
->form
, abbrev
->implicit_const
,
1352 unit
, info_ptr
, info_ptr_end
);
1356 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1360 non_mangled (int lang
)
1370 case DW_LANG_Cobol74
:
1371 case DW_LANG_Cobol85
:
1372 case DW_LANG_Fortran77
:
1373 case DW_LANG_Pascal83
:
1383 /* Source line information table routines. */
1385 #define FILE_ALLOC_CHUNK 5
1386 #define DIR_ALLOC_CHUNK 5
1390 struct line_info
* prev_line
;
1394 unsigned int column
;
1395 unsigned int discriminator
;
1396 unsigned char op_index
;
1397 unsigned char end_sequence
; /* End of (sequential) code sequence. */
1408 struct line_sequence
1411 struct line_sequence
* prev_sequence
;
1412 struct line_info
* last_line
; /* Largest VMA. */
1413 struct line_info
** line_info_lookup
;
1414 bfd_size_type num_lines
;
1417 struct line_info_table
1420 unsigned int num_files
;
1421 unsigned int num_dirs
;
1422 unsigned int num_sequences
;
1425 struct fileinfo
* files
;
1426 struct line_sequence
* sequences
;
1427 struct line_info
* lcl_head
; /* Local head; used in 'add_line_info'. */
1430 /* Remember some information about each function. If the function is
1431 inlined (DW_TAG_inlined_subroutine) it may have two additional
1432 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1433 source code location where this function was inlined. */
1437 /* Pointer to previous function in list of all functions. */
1438 struct funcinfo
* prev_func
;
1439 /* Pointer to function one scope higher. */
1440 struct funcinfo
* caller_func
;
1441 /* Source location file name where caller_func inlines this func. */
1443 /* Source location file name. */
1445 /* Source location line number where caller_func inlines this func. */
1447 /* Source location line number. */
1450 bfd_boolean is_linkage
;
1452 struct arange arange
;
1453 /* Where the symbol is defined. */
1457 struct lookup_funcinfo
1459 /* Function information corresponding to this lookup table entry. */
1460 struct funcinfo
* funcinfo
;
1462 /* The lowest address for this specific function. */
1465 /* The highest address of this function before the lookup table is sorted.
1466 The highest address of all prior functions after the lookup table is
1467 sorted, which is used for binary search. */
1469 /* Index of this function, used to ensure qsort is stable. */
1475 /* Pointer to previous variable in list of all variables. */
1476 struct varinfo
*prev_var
;
1477 /* The offset of the varinfo from the start of the unit. */
1478 bfd_uint64_t unit_offset
;
1479 /* Source location file name. */
1481 /* Source location line number. */
1483 /* The type of this variable. */
1485 /* The name of the variable, if it has one. */
1487 /* The address of the variable. */
1489 /* Where the symbol is defined. */
1491 /* Is this a stack variable? */
1495 /* Return TRUE if NEW_LINE should sort after LINE. */
1497 static inline bfd_boolean
1498 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
1500 return (new_line
->address
> line
->address
1501 || (new_line
->address
== line
->address
1502 && new_line
->op_index
> line
->op_index
));
1506 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1507 that the list is sorted. Note that the line_info list is sorted from
1508 highest to lowest VMA (with possible duplicates); that is,
1509 line_info->prev_line always accesses an equal or smaller VMA. */
1512 add_line_info (struct line_info_table
*table
,
1514 unsigned char op_index
,
1517 unsigned int column
,
1518 unsigned int discriminator
,
1521 size_t amt
= sizeof (struct line_info
);
1522 struct line_sequence
* seq
= table
->sequences
;
1523 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
1528 /* Set member data of 'info'. */
1529 info
->prev_line
= NULL
;
1530 info
->address
= address
;
1531 info
->op_index
= op_index
;
1533 info
->column
= column
;
1534 info
->discriminator
= discriminator
;
1535 info
->end_sequence
= end_sequence
;
1537 if (filename
&& filename
[0])
1539 info
->filename
= (char *) bfd_alloc (table
->abfd
, strlen (filename
) + 1);
1540 if (info
->filename
== NULL
)
1542 strcpy (info
->filename
, filename
);
1545 info
->filename
= NULL
;
1547 /* Find the correct location for 'info'. Normally we will receive
1548 new line_info data 1) in order and 2) with increasing VMAs.
1549 However some compilers break the rules (cf. decode_line_info) and
1550 so we include some heuristics for quickly finding the correct
1551 location for 'info'. In particular, these heuristics optimize for
1552 the common case in which the VMA sequence that we receive is a
1553 list of locally sorted VMAs such as
1554 p...z a...j (where a < j < p < z)
1556 Note: table->lcl_head is used to head an *actual* or *possible*
1557 sub-sequence within the list (such as a...j) that is not directly
1558 headed by table->last_line
1560 Note: we may receive duplicate entries from 'decode_line_info'. */
1563 && seq
->last_line
->address
== address
1564 && seq
->last_line
->op_index
== op_index
1565 && seq
->last_line
->end_sequence
== end_sequence
)
1567 /* We only keep the last entry with the same address and end
1568 sequence. See PR ld/4986. */
1569 if (table
->lcl_head
== seq
->last_line
)
1570 table
->lcl_head
= info
;
1571 info
->prev_line
= seq
->last_line
->prev_line
;
1572 seq
->last_line
= info
;
1574 else if (!seq
|| seq
->last_line
->end_sequence
)
1576 /* Start a new line sequence. */
1577 amt
= sizeof (struct line_sequence
);
1578 seq
= (struct line_sequence
*) bfd_malloc (amt
);
1581 seq
->low_pc
= address
;
1582 seq
->prev_sequence
= table
->sequences
;
1583 seq
->last_line
= info
;
1584 table
->lcl_head
= info
;
1585 table
->sequences
= seq
;
1586 table
->num_sequences
++;
1588 else if (info
->end_sequence
1589 || new_line_sorts_after (info
, seq
->last_line
))
1591 /* Normal case: add 'info' to the beginning of the current sequence. */
1592 info
->prev_line
= seq
->last_line
;
1593 seq
->last_line
= info
;
1595 /* lcl_head: initialize to head a *possible* sequence at the end. */
1596 if (!table
->lcl_head
)
1597 table
->lcl_head
= info
;
1599 else if (!new_line_sorts_after (info
, table
->lcl_head
)
1600 && (!table
->lcl_head
->prev_line
1601 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
1603 /* Abnormal but easy: lcl_head is the head of 'info'. */
1604 info
->prev_line
= table
->lcl_head
->prev_line
;
1605 table
->lcl_head
->prev_line
= info
;
1609 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1610 are valid heads for 'info'. Reset 'lcl_head'. */
1611 struct line_info
* li2
= seq
->last_line
; /* Always non-NULL. */
1612 struct line_info
* li1
= li2
->prev_line
;
1616 if (!new_line_sorts_after (info
, li2
)
1617 && new_line_sorts_after (info
, li1
))
1620 li2
= li1
; /* always non-NULL */
1621 li1
= li1
->prev_line
;
1623 table
->lcl_head
= li2
;
1624 info
->prev_line
= table
->lcl_head
->prev_line
;
1625 table
->lcl_head
->prev_line
= info
;
1626 if (address
< seq
->low_pc
)
1627 seq
->low_pc
= address
;
1632 /* Extract a fully qualified filename from a line info table.
1633 The returned string has been malloc'ed and it is the caller's
1634 responsibility to free it. */
1637 concat_filename (struct line_info_table
*table
, unsigned int file
)
1641 if (table
== NULL
|| file
- 1 >= table
->num_files
)
1643 /* FILE == 0 means unknown. */
1646 (_("DWARF error: mangled line number section (bad file number)"));
1647 return strdup ("<unknown>");
1650 filename
= table
->files
[file
- 1].name
;
1651 if (filename
== NULL
)
1652 return strdup ("<unknown>");
1654 if (!IS_ABSOLUTE_PATH (filename
))
1656 char *dir_name
= NULL
;
1657 char *subdir_name
= NULL
;
1661 if (table
->files
[file
- 1].dir
1662 /* PR 17512: file: 0317e960. */
1663 && table
->files
[file
- 1].dir
<= table
->num_dirs
1664 /* PR 17512: file: 7f3d2e4b. */
1665 && table
->dirs
!= NULL
)
1666 subdir_name
= table
->dirs
[table
->files
[file
- 1].dir
- 1];
1668 if (!subdir_name
|| !IS_ABSOLUTE_PATH (subdir_name
))
1669 dir_name
= table
->comp_dir
;
1673 dir_name
= subdir_name
;
1678 return strdup (filename
);
1680 len
= strlen (dir_name
) + strlen (filename
) + 2;
1684 len
+= strlen (subdir_name
) + 1;
1685 name
= (char *) bfd_malloc (len
);
1687 sprintf (name
, "%s/%s/%s", dir_name
, subdir_name
, filename
);
1691 name
= (char *) bfd_malloc (len
);
1693 sprintf (name
, "%s/%s", dir_name
, filename
);
1699 return strdup (filename
);
1703 arange_add (const struct comp_unit
*unit
, struct arange
*first_arange
,
1704 bfd_vma low_pc
, bfd_vma high_pc
)
1706 struct arange
*arange
;
1708 /* Ignore empty ranges. */
1709 if (low_pc
== high_pc
)
1712 /* If the first arange is empty, use it. */
1713 if (first_arange
->high
== 0)
1715 first_arange
->low
= low_pc
;
1716 first_arange
->high
= high_pc
;
1720 /* Next see if we can cheaply extend an existing range. */
1721 arange
= first_arange
;
1724 if (low_pc
== arange
->high
)
1726 arange
->high
= high_pc
;
1729 if (high_pc
== arange
->low
)
1731 arange
->low
= low_pc
;
1734 arange
= arange
->next
;
1738 /* Need to allocate a new arange and insert it into the arange list.
1739 Order isn't significant, so just insert after the first arange. */
1740 arange
= (struct arange
*) bfd_alloc (unit
->abfd
, sizeof (*arange
));
1743 arange
->low
= low_pc
;
1744 arange
->high
= high_pc
;
1745 arange
->next
= first_arange
->next
;
1746 first_arange
->next
= arange
;
1750 /* Compare function for line sequences. */
1753 compare_sequences (const void* a
, const void* b
)
1755 const struct line_sequence
* seq1
= a
;
1756 const struct line_sequence
* seq2
= b
;
1758 /* Sort by low_pc as the primary key. */
1759 if (seq1
->low_pc
< seq2
->low_pc
)
1761 if (seq1
->low_pc
> seq2
->low_pc
)
1764 /* If low_pc values are equal, sort in reverse order of
1765 high_pc, so that the largest region comes first. */
1766 if (seq1
->last_line
->address
< seq2
->last_line
->address
)
1768 if (seq1
->last_line
->address
> seq2
->last_line
->address
)
1771 if (seq1
->last_line
->op_index
< seq2
->last_line
->op_index
)
1773 if (seq1
->last_line
->op_index
> seq2
->last_line
->op_index
)
1776 /* num_lines is initially an index, to make the sort stable. */
1777 if (seq1
->num_lines
< seq2
->num_lines
)
1779 if (seq1
->num_lines
> seq2
->num_lines
)
1784 /* Construct the line information table for quick lookup. */
1787 build_line_info_table (struct line_info_table
* table
,
1788 struct line_sequence
* seq
)
1791 struct line_info
**line_info_lookup
;
1792 struct line_info
*each_line
;
1793 unsigned int num_lines
;
1794 unsigned int line_index
;
1796 if (seq
->line_info_lookup
!= NULL
)
1799 /* Count the number of line information entries. We could do this while
1800 scanning the debug information, but some entries may be added via
1801 lcl_head without having a sequence handy to increment the number of
1804 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
1807 seq
->num_lines
= num_lines
;
1811 /* Allocate space for the line information lookup table. */
1812 amt
= sizeof (struct line_info
*) * num_lines
;
1813 line_info_lookup
= (struct line_info
**) bfd_alloc (table
->abfd
, amt
);
1814 seq
->line_info_lookup
= line_info_lookup
;
1815 if (line_info_lookup
== NULL
)
1818 /* Create the line information lookup table. */
1819 line_index
= num_lines
;
1820 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
1821 line_info_lookup
[--line_index
] = each_line
;
1823 BFD_ASSERT (line_index
== 0);
1827 /* Sort the line sequences for quick lookup. */
1830 sort_line_sequences (struct line_info_table
* table
)
1833 struct line_sequence
*sequences
;
1834 struct line_sequence
*seq
;
1836 unsigned int num_sequences
= table
->num_sequences
;
1837 bfd_vma last_high_pc
;
1839 if (num_sequences
== 0)
1842 /* Allocate space for an array of sequences. */
1843 amt
= sizeof (struct line_sequence
) * num_sequences
;
1844 sequences
= (struct line_sequence
*) bfd_alloc (table
->abfd
, amt
);
1845 if (sequences
== NULL
)
1848 /* Copy the linked list into the array, freeing the original nodes. */
1849 seq
= table
->sequences
;
1850 for (n
= 0; n
< num_sequences
; n
++)
1852 struct line_sequence
* last_seq
= seq
;
1855 sequences
[n
].low_pc
= seq
->low_pc
;
1856 sequences
[n
].prev_sequence
= NULL
;
1857 sequences
[n
].last_line
= seq
->last_line
;
1858 sequences
[n
].line_info_lookup
= NULL
;
1859 sequences
[n
].num_lines
= n
;
1860 seq
= seq
->prev_sequence
;
1863 BFD_ASSERT (seq
== NULL
);
1865 qsort (sequences
, n
, sizeof (struct line_sequence
), compare_sequences
);
1867 /* Make the list binary-searchable by trimming overlapping entries
1868 and removing nested entries. */
1870 last_high_pc
= sequences
[0].last_line
->address
;
1871 for (n
= 1; n
< table
->num_sequences
; n
++)
1873 if (sequences
[n
].low_pc
< last_high_pc
)
1875 if (sequences
[n
].last_line
->address
<= last_high_pc
)
1876 /* Skip nested entries. */
1879 /* Trim overlapping entries. */
1880 sequences
[n
].low_pc
= last_high_pc
;
1882 last_high_pc
= sequences
[n
].last_line
->address
;
1883 if (n
> num_sequences
)
1885 /* Close up the gap. */
1886 sequences
[num_sequences
].low_pc
= sequences
[n
].low_pc
;
1887 sequences
[num_sequences
].last_line
= sequences
[n
].last_line
;
1892 table
->sequences
= sequences
;
1893 table
->num_sequences
= num_sequences
;
1897 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
1900 line_info_add_include_dir (struct line_info_table
*table
, char *cur_dir
)
1902 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1907 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1908 amt
*= sizeof (char *);
1910 tmp
= (char **) bfd_realloc (table
->dirs
, amt
);
1916 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1921 line_info_add_include_dir_stub (struct line_info_table
*table
, char *cur_dir
,
1922 unsigned int dir ATTRIBUTE_UNUSED
,
1923 unsigned int xtime ATTRIBUTE_UNUSED
,
1924 unsigned int size ATTRIBUTE_UNUSED
)
1926 return line_info_add_include_dir (table
, cur_dir
);
1929 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
1932 line_info_add_file_name (struct line_info_table
*table
, char *cur_file
,
1933 unsigned int dir
, unsigned int xtime
,
1936 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1938 struct fileinfo
*tmp
;
1941 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1942 amt
*= sizeof (struct fileinfo
);
1944 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1950 table
->files
[table
->num_files
].name
= cur_file
;
1951 table
->files
[table
->num_files
].dir
= dir
;
1952 table
->files
[table
->num_files
].time
= xtime
;
1953 table
->files
[table
->num_files
].size
= size
;
1958 /* Read directory or file name entry format, starting with byte of
1959 format count entries, ULEB128 pairs of entry formats, ULEB128 of
1960 entries count and the entries themselves in the described entry
1964 read_formatted_entries (struct comp_unit
*unit
, bfd_byte
**bufp
,
1965 bfd_byte
*buf_end
, struct line_info_table
*table
,
1966 bfd_boolean (*callback
) (struct line_info_table
*table
,
1972 bfd
*abfd
= unit
->abfd
;
1973 bfd_byte format_count
, formati
;
1974 bfd_vma data_count
, datai
;
1975 bfd_byte
*buf
= *bufp
;
1976 bfd_byte
*format_header_data
;
1977 unsigned int bytes_read
;
1979 format_count
= read_1_byte (abfd
, buf
, buf_end
);
1981 format_header_data
= buf
;
1982 for (formati
= 0; formati
< format_count
; formati
++)
1984 _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
1986 _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
1990 data_count
= _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
1992 if (format_count
== 0 && data_count
!= 0)
1994 _bfd_error_handler (_("DWARF error: zero format count"));
1995 bfd_set_error (bfd_error_bad_value
);
1999 /* PR 22210. Paranoia check. Don't bother running the loop
2000 if we know that we are going to run out of buffer. */
2001 if (data_count
> (bfd_vma
) (buf_end
- buf
))
2004 (_("DWARF error: data count (%" PRIx64
") larger than buffer size"),
2005 (uint64_t) data_count
);
2006 bfd_set_error (bfd_error_bad_value
);
2010 for (datai
= 0; datai
< data_count
; datai
++)
2012 bfd_byte
*format
= format_header_data
;
2015 memset (&fe
, 0, sizeof fe
);
2016 for (formati
= 0; formati
< format_count
; formati
++)
2018 bfd_vma content_type
, form
;
2020 char **stringp
= &string_trash
;
2021 unsigned int uint_trash
, *uintp
= &uint_trash
;
2022 struct attribute attr
;
2024 content_type
= _bfd_safe_read_leb128 (abfd
, format
, &bytes_read
,
2026 format
+= bytes_read
;
2027 switch (content_type
)
2032 case DW_LNCT_directory_index
:
2035 case DW_LNCT_timestamp
:
2045 (_("DWARF error: unknown format content type %" PRIu64
),
2046 (uint64_t) content_type
);
2047 bfd_set_error (bfd_error_bad_value
);
2051 form
= _bfd_safe_read_leb128 (abfd
, format
, &bytes_read
, FALSE
,
2053 format
+= bytes_read
;
2055 buf
= read_attribute_value (&attr
, form
, 0, unit
, buf
, buf_end
);
2060 case DW_FORM_string
:
2061 case DW_FORM_line_strp
:
2062 *stringp
= attr
.u
.str
;
2070 *uintp
= attr
.u
.val
;
2075 if (!callback (table
, fe
.name
, fe
.dir
, fe
.time
, fe
.size
))
2083 /* Decode the line number information for UNIT. */
2085 static struct line_info_table
*
2086 decode_line_info (struct comp_unit
*unit
)
2088 bfd
*abfd
= unit
->abfd
;
2089 struct dwarf2_debug
*stash
= unit
->stash
;
2090 struct dwarf2_debug_file
*file
= unit
->file
;
2091 struct line_info_table
* table
;
2094 struct line_head lh
;
2095 unsigned int i
, bytes_read
, offset_size
;
2096 char *cur_file
, *cur_dir
;
2097 unsigned char op_code
, extended_op
, adj_opcode
;
2098 unsigned int exop_len
;
2101 if (unit
->line_offset
== 0 && file
->line_table
)
2102 return file
->line_table
;
2104 if (! read_section (abfd
, &stash
->debug_sections
[debug_line
],
2105 file
->syms
, unit
->line_offset
,
2106 &file
->dwarf_line_buffer
, &file
->dwarf_line_size
))
2109 if (file
->dwarf_line_size
< 16)
2112 (_("DWARF error: line info section is too small (%" PRId64
")"),
2113 (int64_t) file
->dwarf_line_size
);
2114 bfd_set_error (bfd_error_bad_value
);
2117 line_ptr
= file
->dwarf_line_buffer
+ unit
->line_offset
;
2118 line_end
= file
->dwarf_line_buffer
+ file
->dwarf_line_size
;
2120 /* Read in the prologue. */
2121 lh
.total_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2124 if (lh
.total_length
== 0xffffffff)
2126 lh
.total_length
= read_8_bytes (abfd
, line_ptr
, line_end
);
2130 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
2132 /* Handle (non-standard) 64-bit DWARF2 formats. */
2133 lh
.total_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2138 if (lh
.total_length
> (size_t) (line_end
- line_ptr
))
2141 /* xgettext: c-format */
2142 (_("DWARF error: line info data is bigger (%#" PRIx64
")"
2143 " than the space remaining in the section (%#lx)"),
2144 (uint64_t) lh
.total_length
, (unsigned long) (line_end
- line_ptr
));
2145 bfd_set_error (bfd_error_bad_value
);
2149 line_end
= line_ptr
+ lh
.total_length
;
2151 lh
.version
= read_2_bytes (abfd
, line_ptr
, line_end
);
2152 if (lh
.version
< 2 || lh
.version
> 5)
2155 (_("DWARF error: unhandled .debug_line version %d"), lh
.version
);
2156 bfd_set_error (bfd_error_bad_value
);
2161 if (line_ptr
+ offset_size
+ (lh
.version
>= 5 ? 8 : (lh
.version
>= 4 ? 6 : 5))
2165 (_("DWARF error: ran out of room reading prologue"));
2166 bfd_set_error (bfd_error_bad_value
);
2170 if (lh
.version
>= 5)
2172 unsigned int segment_selector_size
;
2174 /* Skip address size. */
2175 read_1_byte (abfd
, line_ptr
, line_end
);
2178 segment_selector_size
= read_1_byte (abfd
, line_ptr
, line_end
);
2180 if (segment_selector_size
!= 0)
2183 (_("DWARF error: line info unsupported segment selector size %u"),
2184 segment_selector_size
);
2185 bfd_set_error (bfd_error_bad_value
);
2190 if (offset_size
== 4)
2191 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2193 lh
.prologue_length
= read_8_bytes (abfd
, line_ptr
, line_end
);
2194 line_ptr
+= offset_size
;
2196 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
, line_end
);
2199 if (lh
.version
>= 4)
2201 lh
.maximum_ops_per_insn
= read_1_byte (abfd
, line_ptr
, line_end
);
2205 lh
.maximum_ops_per_insn
= 1;
2207 if (lh
.maximum_ops_per_insn
== 0)
2210 (_("DWARF error: invalid maximum operations per instruction"));
2211 bfd_set_error (bfd_error_bad_value
);
2215 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
, line_end
);
2218 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
, line_end
);
2221 lh
.line_range
= read_1_byte (abfd
, line_ptr
, line_end
);
2224 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
, line_end
);
2227 if (line_ptr
+ (lh
.opcode_base
- 1) >= line_end
)
2229 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2230 bfd_set_error (bfd_error_bad_value
);
2234 amt
= lh
.opcode_base
* sizeof (unsigned char);
2235 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
2237 lh
.standard_opcode_lengths
[0] = 1;
2239 for (i
= 1; i
< lh
.opcode_base
; ++i
)
2241 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
, line_end
);
2245 amt
= sizeof (struct line_info_table
);
2246 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
2250 table
->comp_dir
= unit
->comp_dir
;
2252 table
->num_files
= 0;
2253 table
->files
= NULL
;
2255 table
->num_dirs
= 0;
2258 table
->num_sequences
= 0;
2259 table
->sequences
= NULL
;
2261 table
->lcl_head
= NULL
;
2263 if (lh
.version
>= 5)
2265 /* Read directory table. */
2266 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2267 line_info_add_include_dir_stub
))
2270 /* Read file name table. */
2271 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2272 line_info_add_file_name
))
2277 /* Read directory table. */
2278 while ((cur_dir
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
)) != NULL
)
2280 line_ptr
+= bytes_read
;
2282 if (!line_info_add_include_dir (table
, cur_dir
))
2286 line_ptr
+= bytes_read
;
2288 /* Read file name table. */
2289 while ((cur_file
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
)) != NULL
)
2291 unsigned int dir
, xtime
, size
;
2293 line_ptr
+= bytes_read
;
2295 dir
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2296 line_ptr
+= bytes_read
;
2297 xtime
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2298 line_ptr
+= bytes_read
;
2299 size
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2300 line_ptr
+= bytes_read
;
2302 if (!line_info_add_file_name (table
, cur_file
, dir
, xtime
, size
))
2306 line_ptr
+= bytes_read
;
2309 /* Read the statement sequences until there's nothing left. */
2310 while (line_ptr
< line_end
)
2312 /* State machine registers. */
2313 bfd_vma address
= 0;
2314 unsigned char op_index
= 0;
2315 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
2316 unsigned int line
= 1;
2317 unsigned int column
= 0;
2318 unsigned int discriminator
= 0;
2319 int is_stmt
= lh
.default_is_stmt
;
2320 int end_sequence
= 0;
2321 unsigned int dir
, xtime
, size
;
2322 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2323 compilers generate address sequences that are wildly out of
2324 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2325 for ia64-Linux). Thus, to determine the low and high
2326 address, we must compare on every DW_LNS_copy, etc. */
2327 bfd_vma low_pc
= (bfd_vma
) -1;
2328 bfd_vma high_pc
= 0;
2330 /* Decode the table. */
2331 while (!end_sequence
&& line_ptr
< line_end
)
2333 op_code
= read_1_byte (abfd
, line_ptr
, line_end
);
2336 if (op_code
>= lh
.opcode_base
)
2338 /* Special operand. */
2339 adj_opcode
= op_code
- lh
.opcode_base
;
2340 if (lh
.line_range
== 0)
2342 if (lh
.maximum_ops_per_insn
== 1)
2343 address
+= (adj_opcode
/ lh
.line_range
2344 * lh
.minimum_instruction_length
);
2347 address
+= ((op_index
+ adj_opcode
/ lh
.line_range
)
2348 / lh
.maximum_ops_per_insn
2349 * lh
.minimum_instruction_length
);
2350 op_index
= ((op_index
+ adj_opcode
/ lh
.line_range
)
2351 % lh
.maximum_ops_per_insn
);
2353 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
2354 /* Append row to matrix using current values. */
2355 if (!add_line_info (table
, address
, op_index
, filename
,
2356 line
, column
, discriminator
, 0))
2359 if (address
< low_pc
)
2361 if (address
> high_pc
)
2364 else switch (op_code
)
2366 case DW_LNS_extended_op
:
2367 exop_len
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2369 line_ptr
+= bytes_read
;
2370 extended_op
= read_1_byte (abfd
, line_ptr
, line_end
);
2373 switch (extended_op
)
2375 case DW_LNE_end_sequence
:
2377 if (!add_line_info (table
, address
, op_index
, filename
, line
,
2378 column
, discriminator
, end_sequence
))
2381 if (address
< low_pc
)
2383 if (address
> high_pc
)
2385 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
2388 case DW_LNE_set_address
:
2389 address
= read_address (unit
, line_ptr
, line_end
);
2391 line_ptr
+= unit
->addr_size
;
2393 case DW_LNE_define_file
:
2394 cur_file
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
);
2395 line_ptr
+= bytes_read
;
2396 dir
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2398 line_ptr
+= bytes_read
;
2399 xtime
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2401 line_ptr
+= bytes_read
;
2402 size
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2404 line_ptr
+= bytes_read
;
2405 if (!line_info_add_file_name (table
, cur_file
, dir
,
2409 case DW_LNE_set_discriminator
:
2411 _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2413 line_ptr
+= bytes_read
;
2415 case DW_LNE_HP_source_file_correlation
:
2416 line_ptr
+= exop_len
- 1;
2420 (_("DWARF error: mangled line number section"));
2421 bfd_set_error (bfd_error_bad_value
);
2423 if (filename
!= NULL
)
2429 if (!add_line_info (table
, address
, op_index
,
2430 filename
, line
, column
, discriminator
, 0))
2433 if (address
< low_pc
)
2435 if (address
> high_pc
)
2438 case DW_LNS_advance_pc
:
2439 if (lh
.maximum_ops_per_insn
== 1)
2440 address
+= (lh
.minimum_instruction_length
2441 * _bfd_safe_read_leb128 (abfd
, line_ptr
,
2446 bfd_vma adjust
= _bfd_safe_read_leb128 (abfd
, line_ptr
,
2449 address
= ((op_index
+ adjust
) / lh
.maximum_ops_per_insn
2450 * lh
.minimum_instruction_length
);
2451 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2453 line_ptr
+= bytes_read
;
2455 case DW_LNS_advance_line
:
2456 line
+= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2458 line_ptr
+= bytes_read
;
2460 case DW_LNS_set_file
:
2462 unsigned int filenum
;
2464 /* The file and directory tables are 0
2465 based, the references are 1 based. */
2466 filenum
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2468 line_ptr
+= bytes_read
;
2471 filename
= concat_filename (table
, filenum
);
2474 case DW_LNS_set_column
:
2475 column
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2477 line_ptr
+= bytes_read
;
2479 case DW_LNS_negate_stmt
:
2480 is_stmt
= (!is_stmt
);
2482 case DW_LNS_set_basic_block
:
2484 case DW_LNS_const_add_pc
:
2485 if (lh
.line_range
== 0)
2487 if (lh
.maximum_ops_per_insn
== 1)
2488 address
+= (lh
.minimum_instruction_length
2489 * ((255 - lh
.opcode_base
) / lh
.line_range
));
2492 bfd_vma adjust
= ((255 - lh
.opcode_base
) / lh
.line_range
);
2493 address
+= (lh
.minimum_instruction_length
2494 * ((op_index
+ adjust
)
2495 / lh
.maximum_ops_per_insn
));
2496 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2499 case DW_LNS_fixed_advance_pc
:
2500 address
+= read_2_bytes (abfd
, line_ptr
, line_end
);
2505 /* Unknown standard opcode, ignore it. */
2506 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
2508 (void) _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2510 line_ptr
+= bytes_read
;
2520 if (unit
->line_offset
== 0)
2521 file
->line_table
= table
;
2522 if (sort_line_sequences (table
))
2526 while (table
->sequences
!= NULL
)
2528 struct line_sequence
* seq
= table
->sequences
;
2529 table
->sequences
= table
->sequences
->prev_sequence
;
2532 if (table
->files
!= NULL
)
2533 free (table
->files
);
2534 if (table
->dirs
!= NULL
)
2539 /* If ADDR is within TABLE set the output parameters and return the
2540 range of addresses covered by the entry used to fill them out.
2541 Otherwise set * FILENAME_PTR to NULL and return 0.
2542 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2543 are pointers to the objects to be filled in. */
2546 lookup_address_in_line_info_table (struct line_info_table
*table
,
2548 const char **filename_ptr
,
2549 unsigned int *linenumber_ptr
,
2550 unsigned int *discriminator_ptr
)
2552 struct line_sequence
*seq
= NULL
;
2553 struct line_info
*info
;
2556 /* Binary search the array of sequences. */
2558 high
= table
->num_sequences
;
2561 mid
= (low
+ high
) / 2;
2562 seq
= &table
->sequences
[mid
];
2563 if (addr
< seq
->low_pc
)
2565 else if (addr
>= seq
->last_line
->address
)
2571 /* Check for a valid sequence. */
2572 if (!seq
|| addr
< seq
->low_pc
|| addr
>= seq
->last_line
->address
)
2575 if (!build_line_info_table (table
, seq
))
2578 /* Binary search the array of line information. */
2580 high
= seq
->num_lines
;
2584 mid
= (low
+ high
) / 2;
2585 info
= seq
->line_info_lookup
[mid
];
2586 if (addr
< info
->address
)
2588 else if (addr
>= seq
->line_info_lookup
[mid
+ 1]->address
)
2594 /* Check for a valid line information entry. */
2596 && addr
>= info
->address
2597 && addr
< seq
->line_info_lookup
[mid
+ 1]->address
2598 && !(info
->end_sequence
|| info
== seq
->last_line
))
2600 *filename_ptr
= info
->filename
;
2601 *linenumber_ptr
= info
->line
;
2602 if (discriminator_ptr
)
2603 *discriminator_ptr
= info
->discriminator
;
2604 return seq
->last_line
->address
- seq
->low_pc
;
2608 *filename_ptr
= NULL
;
2612 /* Read in the .debug_ranges section for future reference. */
2615 read_debug_ranges (struct comp_unit
* unit
)
2617 struct dwarf2_debug
*stash
= unit
->stash
;
2618 struct dwarf2_debug_file
*file
= unit
->file
;
2620 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_ranges
],
2622 &file
->dwarf_ranges_buffer
, &file
->dwarf_ranges_size
);
2625 /* Function table functions. */
2628 compare_lookup_funcinfos (const void * a
, const void * b
)
2630 const struct lookup_funcinfo
* lookup1
= a
;
2631 const struct lookup_funcinfo
* lookup2
= b
;
2633 if (lookup1
->low_addr
< lookup2
->low_addr
)
2635 if (lookup1
->low_addr
> lookup2
->low_addr
)
2637 if (lookup1
->high_addr
< lookup2
->high_addr
)
2639 if (lookup1
->high_addr
> lookup2
->high_addr
)
2642 if (lookup1
->idx
< lookup2
->idx
)
2644 if (lookup1
->idx
> lookup2
->idx
)
2650 build_lookup_funcinfo_table (struct comp_unit
* unit
)
2652 struct lookup_funcinfo
*lookup_funcinfo_table
= unit
->lookup_funcinfo_table
;
2653 unsigned int number_of_functions
= unit
->number_of_functions
;
2654 struct funcinfo
*each
;
2655 struct lookup_funcinfo
*entry
;
2657 struct arange
*range
;
2658 bfd_vma low_addr
, high_addr
;
2660 if (lookup_funcinfo_table
|| number_of_functions
== 0)
2663 /* Create the function info lookup table. */
2664 lookup_funcinfo_table
= (struct lookup_funcinfo
*)
2665 bfd_malloc (number_of_functions
* sizeof (struct lookup_funcinfo
));
2666 if (lookup_funcinfo_table
== NULL
)
2669 /* Populate the function info lookup table. */
2670 func_index
= number_of_functions
;
2671 for (each
= unit
->function_table
; each
; each
= each
->prev_func
)
2673 entry
= &lookup_funcinfo_table
[--func_index
];
2674 entry
->funcinfo
= each
;
2675 entry
->idx
= func_index
;
2677 /* Calculate the lowest and highest address for this function entry. */
2678 low_addr
= entry
->funcinfo
->arange
.low
;
2679 high_addr
= entry
->funcinfo
->arange
.high
;
2681 for (range
= entry
->funcinfo
->arange
.next
; range
; range
= range
->next
)
2683 if (range
->low
< low_addr
)
2684 low_addr
= range
->low
;
2685 if (range
->high
> high_addr
)
2686 high_addr
= range
->high
;
2689 entry
->low_addr
= low_addr
;
2690 entry
->high_addr
= high_addr
;
2693 BFD_ASSERT (func_index
== 0);
2695 /* Sort the function by address. */
2696 qsort (lookup_funcinfo_table
,
2697 number_of_functions
,
2698 sizeof (struct lookup_funcinfo
),
2699 compare_lookup_funcinfos
);
2701 /* Calculate the high watermark for each function in the lookup table. */
2702 high_addr
= lookup_funcinfo_table
[0].high_addr
;
2703 for (func_index
= 1; func_index
< number_of_functions
; func_index
++)
2705 entry
= &lookup_funcinfo_table
[func_index
];
2706 if (entry
->high_addr
> high_addr
)
2707 high_addr
= entry
->high_addr
;
2709 entry
->high_addr
= high_addr
;
2712 unit
->lookup_funcinfo_table
= lookup_funcinfo_table
;
2716 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
2717 TRUE. Note that we need to find the function that has the smallest range
2718 that contains ADDR, to handle inlined functions without depending upon
2719 them being ordered in TABLE by increasing range. */
2722 lookup_address_in_function_table (struct comp_unit
*unit
,
2724 struct funcinfo
**function_ptr
)
2726 unsigned int number_of_functions
= unit
->number_of_functions
;
2727 struct lookup_funcinfo
* lookup_funcinfo
= NULL
;
2728 struct funcinfo
* funcinfo
= NULL
;
2729 struct funcinfo
* best_fit
= NULL
;
2730 bfd_vma best_fit_len
= 0;
2731 bfd_size_type low
, high
, mid
, first
;
2732 struct arange
*arange
;
2734 if (number_of_functions
== 0)
2737 if (!build_lookup_funcinfo_table (unit
))
2740 if (unit
->lookup_funcinfo_table
[number_of_functions
- 1].high_addr
< addr
)
2743 /* Find the first function in the lookup table which may contain the
2744 specified address. */
2746 high
= number_of_functions
;
2750 mid
= (low
+ high
) / 2;
2751 lookup_funcinfo
= &unit
->lookup_funcinfo_table
[mid
];
2752 if (addr
< lookup_funcinfo
->low_addr
)
2754 else if (addr
>= lookup_funcinfo
->high_addr
)
2760 /* Find the 'best' match for the address. The prior algorithm defined the
2761 best match as the function with the smallest address range containing
2762 the specified address. This definition should probably be changed to the
2763 innermost inline routine containing the address, but right now we want
2764 to get the same results we did before. */
2765 while (first
< number_of_functions
)
2767 if (addr
< unit
->lookup_funcinfo_table
[first
].low_addr
)
2769 funcinfo
= unit
->lookup_funcinfo_table
[first
].funcinfo
;
2771 for (arange
= &funcinfo
->arange
; arange
; arange
= arange
->next
)
2773 if (addr
< arange
->low
|| addr
>= arange
->high
)
2777 || arange
->high
- arange
->low
< best_fit_len
2778 /* The following comparison is designed to return the same
2779 match as the previous algorithm for routines which have the
2780 same best fit length. */
2781 || (arange
->high
- arange
->low
== best_fit_len
2782 && funcinfo
> best_fit
))
2784 best_fit
= funcinfo
;
2785 best_fit_len
= arange
->high
- arange
->low
;
2795 *function_ptr
= best_fit
;
2799 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2800 and LINENUMBER_PTR, and return TRUE. */
2803 lookup_symbol_in_function_table (struct comp_unit
*unit
,
2806 const char **filename_ptr
,
2807 unsigned int *linenumber_ptr
)
2809 struct funcinfo
* each_func
;
2810 struct funcinfo
* best_fit
= NULL
;
2811 bfd_vma best_fit_len
= 0;
2812 struct arange
*arange
;
2813 const char *name
= bfd_asymbol_name (sym
);
2814 asection
*sec
= bfd_asymbol_section (sym
);
2816 for (each_func
= unit
->function_table
;
2818 each_func
= each_func
->prev_func
)
2820 for (arange
= &each_func
->arange
;
2822 arange
= arange
->next
)
2824 if ((!each_func
->sec
|| each_func
->sec
== sec
)
2825 && addr
>= arange
->low
2826 && addr
< arange
->high
2828 && strcmp (name
, each_func
->name
) == 0
2830 || arange
->high
- arange
->low
< best_fit_len
))
2832 best_fit
= each_func
;
2833 best_fit_len
= arange
->high
- arange
->low
;
2840 best_fit
->sec
= sec
;
2841 *filename_ptr
= best_fit
->file
;
2842 *linenumber_ptr
= best_fit
->line
;
2849 /* Variable table functions. */
2851 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
2852 LINENUMBER_PTR, and return TRUE. */
2855 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
2858 const char **filename_ptr
,
2859 unsigned int *linenumber_ptr
)
2861 const char *name
= bfd_asymbol_name (sym
);
2862 asection
*sec
= bfd_asymbol_section (sym
);
2863 struct varinfo
* each
;
2865 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
2867 && each
->file
!= NULL
2868 && each
->name
!= NULL
2869 && each
->addr
== addr
2870 && (!each
->sec
|| each
->sec
== sec
)
2871 && strcmp (name
, each
->name
) == 0)
2877 *filename_ptr
= each
->file
;
2878 *linenumber_ptr
= each
->line
;
2885 static struct comp_unit
*stash_comp_unit (struct dwarf2_debug
*,
2886 struct dwarf2_debug_file
*);
2887 static bfd_boolean
comp_unit_maybe_decode_line_info (struct comp_unit
*);
2890 find_abstract_instance (struct comp_unit
*unit
,
2891 struct attribute
*attr_ptr
,
2892 unsigned int recur_count
,
2894 bfd_boolean
*is_linkage
,
2895 char **filename_ptr
,
2896 int *linenumber_ptr
)
2898 bfd
*abfd
= unit
->abfd
;
2899 bfd_byte
*info_ptr
= NULL
;
2900 bfd_byte
*info_ptr_end
;
2901 unsigned int abbrev_number
, bytes_read
, i
;
2902 struct abbrev_info
*abbrev
;
2903 bfd_uint64_t die_ref
= attr_ptr
->u
.val
;
2904 struct attribute attr
;
2905 const char *name
= NULL
;
2907 if (recur_count
== 100)
2910 (_("DWARF error: abstract instance recursion detected"));
2911 bfd_set_error (bfd_error_bad_value
);
2915 /* DW_FORM_ref_addr can reference an entry in a different CU. It
2916 is an offset from the .debug_info section, not the current CU. */
2917 if (attr_ptr
->form
== DW_FORM_ref_addr
)
2919 /* We only support DW_FORM_ref_addr within the same file, so
2920 any relocations should be resolved already. Check this by
2921 testing for a zero die_ref; There can't be a valid reference
2922 to the header of a .debug_info section.
2923 DW_FORM_ref_addr is an offset relative to .debug_info.
2924 Normally when using the GNU linker this is accomplished by
2925 emitting a symbolic reference to a label, because .debug_info
2926 sections are linked at zero. When there are multiple section
2927 groups containing .debug_info, as there might be in a
2928 relocatable object file, it would be reasonable to assume that
2929 a symbolic reference to a label in any .debug_info section
2930 might be used. Since we lay out multiple .debug_info
2931 sections at non-zero VMAs (see place_sections), and read
2932 them contiguously into dwarf_info_buffer, that means the
2933 reference is relative to dwarf_info_buffer. */
2936 info_ptr
= unit
->file
->dwarf_info_buffer
;
2937 info_ptr_end
= info_ptr
+ unit
->file
->dwarf_info_size
;
2938 total
= info_ptr_end
- info_ptr
;
2941 else if (die_ref
>= total
)
2944 (_("DWARF error: invalid abstract instance DIE ref"));
2945 bfd_set_error (bfd_error_bad_value
);
2948 info_ptr
+= die_ref
;
2950 else if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
2952 bfd_boolean first_time
= unit
->stash
->alt
.dwarf_info_buffer
== NULL
;
2954 info_ptr
= read_alt_indirect_ref (unit
, die_ref
);
2956 unit
->stash
->alt
.info_ptr
= unit
->stash
->alt
.dwarf_info_buffer
;
2957 if (info_ptr
== NULL
)
2960 (_("DWARF error: unable to read alt ref %" PRIu64
),
2961 (uint64_t) die_ref
);
2962 bfd_set_error (bfd_error_bad_value
);
2965 info_ptr_end
= (unit
->stash
->alt
.dwarf_info_buffer
2966 + unit
->stash
->alt
.dwarf_info_size
);
2967 if (unit
->stash
->alt
.all_comp_units
)
2968 unit
= unit
->stash
->alt
.all_comp_units
;
2971 if (attr_ptr
->form
== DW_FORM_ref_addr
2972 || attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
2974 /* Now find the CU containing this pointer. */
2975 if (info_ptr
>= unit
->info_ptr_unit
&& info_ptr
< unit
->end_ptr
)
2976 info_ptr_end
= unit
->end_ptr
;
2979 /* Check other CUs to see if they contain the abbrev. */
2980 struct comp_unit
*u
;
2982 for (u
= unit
->prev_unit
; u
!= NULL
; u
= u
->prev_unit
)
2983 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
2987 for (u
= unit
->next_unit
; u
!= NULL
; u
= u
->next_unit
)
2988 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
2991 if (attr_ptr
->form
== DW_FORM_ref_addr
)
2994 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->f
);
2997 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3002 if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3005 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->alt
);
3008 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3016 (_("DWARF error: unable to locate abstract instance DIE ref %"
3017 PRIu64
), (uint64_t) die_ref
);
3018 bfd_set_error (bfd_error_bad_value
);
3022 info_ptr_end
= unit
->end_ptr
;
3027 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
3028 DW_FORM_ref_udata. These are all references relative to the
3029 start of the current CU. */
3032 info_ptr
= unit
->info_ptr_unit
;
3033 info_ptr_end
= unit
->end_ptr
;
3034 total
= info_ptr_end
- info_ptr
;
3035 if (!die_ref
|| die_ref
>= total
)
3038 (_("DWARF error: invalid abstract instance DIE ref"));
3039 bfd_set_error (bfd_error_bad_value
);
3042 info_ptr
+= die_ref
;
3045 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
3046 FALSE
, info_ptr_end
);
3047 info_ptr
+= bytes_read
;
3051 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3055 (_("DWARF error: could not find abbrev number %u"), abbrev_number
);
3056 bfd_set_error (bfd_error_bad_value
);
3061 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3063 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
,
3064 info_ptr
, info_ptr_end
);
3065 if (info_ptr
== NULL
)
3070 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3072 if (name
== NULL
&& is_str_attr (attr
.form
))
3075 if (non_mangled (unit
->lang
))
3079 case DW_AT_specification
:
3080 if (!find_abstract_instance (unit
, &attr
, recur_count
+ 1,
3082 filename_ptr
, linenumber_ptr
))
3085 case DW_AT_linkage_name
:
3086 case DW_AT_MIPS_linkage_name
:
3087 /* PR 16949: Corrupt debug info can place
3088 non-string forms into these attributes. */
3089 if (is_str_attr (attr
.form
))
3095 case DW_AT_decl_file
:
3096 if (!comp_unit_maybe_decode_line_info (unit
))
3098 *filename_ptr
= concat_filename (unit
->line_table
,
3101 case DW_AT_decl_line
:
3102 *linenumber_ptr
= attr
.u
.val
;
3115 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
,
3116 bfd_uint64_t offset
)
3118 bfd_byte
*ranges_ptr
;
3119 bfd_byte
*ranges_end
;
3120 bfd_vma base_address
= unit
->base_address
;
3122 if (! unit
->file
->dwarf_ranges_buffer
)
3124 if (! read_debug_ranges (unit
))
3128 ranges_ptr
= unit
->file
->dwarf_ranges_buffer
+ offset
;
3129 if (ranges_ptr
< unit
->file
->dwarf_ranges_buffer
)
3131 ranges_end
= unit
->file
->dwarf_ranges_buffer
+ unit
->file
->dwarf_ranges_size
;
3138 /* PR 17512: file: 62cada7d. */
3139 if (ranges_ptr
+ 2 * unit
->addr_size
> ranges_end
)
3142 low_pc
= read_address (unit
, ranges_ptr
, ranges_end
);
3143 ranges_ptr
+= unit
->addr_size
;
3144 high_pc
= read_address (unit
, ranges_ptr
, ranges_end
);
3145 ranges_ptr
+= unit
->addr_size
;
3147 if (low_pc
== 0 && high_pc
== 0)
3149 if (low_pc
== -1UL && high_pc
!= -1UL)
3150 base_address
= high_pc
;
3153 if (!arange_add (unit
, arange
,
3154 base_address
+ low_pc
, base_address
+ high_pc
))
3161 static struct varinfo
*
3162 lookup_var_by_offset (bfd_uint64_t offset
, struct varinfo
* table
)
3166 if (table
->unit_offset
== offset
)
3168 table
= table
->prev_var
;
3175 /* DWARF2 Compilation unit functions. */
3177 /* Scan over each die in a comp. unit looking for functions to add
3178 to the function table and variables to the variable table. */
3181 scan_unit_for_symbols (struct comp_unit
*unit
)
3183 bfd
*abfd
= unit
->abfd
;
3184 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
3185 bfd_byte
*info_ptr_end
= unit
->end_ptr
;
3186 int nesting_level
= 0;
3187 struct nest_funcinfo
{
3188 struct funcinfo
*func
;
3190 int nested_funcs_size
;
3192 /* Maintain a stack of in-scope functions and inlined functions, which we
3193 can use to set the caller_func field. */
3194 nested_funcs_size
= 32;
3195 nested_funcs
= (struct nest_funcinfo
*)
3196 bfd_malloc (nested_funcs_size
* sizeof (*nested_funcs
));
3197 if (nested_funcs
== NULL
)
3199 nested_funcs
[nesting_level
].func
= 0;
3201 while (nesting_level
>= 0)
3203 unsigned int abbrev_number
, bytes_read
, i
;
3204 struct abbrev_info
*abbrev
;
3205 struct attribute attr
;
3206 struct funcinfo
*func
;
3207 struct varinfo
*var
;
3209 bfd_vma high_pc
= 0;
3210 bfd_boolean high_pc_relative
= FALSE
;
3211 bfd_uint64_t current_offset
;
3213 /* PR 17512: file: 9f405d9d. */
3214 if (info_ptr
>= info_ptr_end
)
3217 current_offset
= info_ptr
- unit
->info_ptr_unit
;
3218 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
3219 FALSE
, info_ptr_end
);
3220 info_ptr
+= bytes_read
;
3222 if (! abbrev_number
)
3228 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3231 static unsigned int previous_failed_abbrev
= -1U;
3233 /* Avoid multiple reports of the same missing abbrev. */
3234 if (abbrev_number
!= previous_failed_abbrev
)
3237 (_("DWARF error: could not find abbrev number %u"),
3239 previous_failed_abbrev
= abbrev_number
;
3241 bfd_set_error (bfd_error_bad_value
);
3245 if (abbrev
->tag
== DW_TAG_subprogram
3246 || abbrev
->tag
== DW_TAG_entry_point
3247 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3249 size_t amt
= sizeof (struct funcinfo
);
3252 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
3255 func
->tag
= abbrev
->tag
;
3256 func
->prev_func
= unit
->function_table
;
3257 unit
->function_table
= func
;
3258 unit
->number_of_functions
++;
3259 BFD_ASSERT (!unit
->cached
);
3261 if (func
->tag
== DW_TAG_inlined_subroutine
)
3262 for (i
= nesting_level
; i
-- != 0; )
3263 if (nested_funcs
[i
].func
)
3265 func
->caller_func
= nested_funcs
[i
].func
;
3268 nested_funcs
[nesting_level
].func
= func
;
3273 if (abbrev
->tag
== DW_TAG_variable
)
3275 size_t amt
= sizeof (struct varinfo
);
3276 var
= (struct varinfo
*) bfd_zalloc (abfd
, amt
);
3279 var
->tag
= abbrev
->tag
;
3281 var
->prev_var
= unit
->variable_table
;
3282 unit
->variable_table
= var
;
3283 var
->unit_offset
= current_offset
;
3284 /* PR 18205: Missing debug information can cause this
3285 var to be attached to an already cached unit. */
3290 /* No inline function in scope at this nesting level. */
3291 nested_funcs
[nesting_level
].func
= 0;
3294 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3296 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
3297 unit
, info_ptr
, info_ptr_end
);
3298 if (info_ptr
== NULL
)
3305 case DW_AT_call_file
:
3306 func
->caller_file
= concat_filename (unit
->line_table
,
3310 case DW_AT_call_line
:
3311 func
->caller_line
= attr
.u
.val
;
3314 case DW_AT_abstract_origin
:
3315 case DW_AT_specification
:
3316 if (!find_abstract_instance (unit
, &attr
, 0,
3325 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3327 if (func
->name
== NULL
&& is_str_attr (attr
.form
))
3329 func
->name
= attr
.u
.str
;
3330 if (non_mangled (unit
->lang
))
3331 func
->is_linkage
= TRUE
;
3335 case DW_AT_linkage_name
:
3336 case DW_AT_MIPS_linkage_name
:
3337 /* PR 16949: Corrupt debug info can place
3338 non-string forms into these attributes. */
3339 if (is_str_attr (attr
.form
))
3341 func
->name
= attr
.u
.str
;
3342 func
->is_linkage
= TRUE
;
3347 low_pc
= attr
.u
.val
;
3351 high_pc
= attr
.u
.val
;
3352 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
3356 if (!read_rangelist (unit
, &func
->arange
, attr
.u
.val
))
3360 case DW_AT_decl_file
:
3361 func
->file
= concat_filename (unit
->line_table
,
3365 case DW_AT_decl_line
:
3366 func
->line
= attr
.u
.val
;
3377 case DW_AT_specification
:
3380 struct varinfo
* spec_var
;
3382 spec_var
= lookup_var_by_offset (attr
.u
.val
, unit
->variable_table
);
3383 if (spec_var
== NULL
)
3386 (_("DWARF error: could not find variable specification at offset %lx"),
3387 (unsigned long) attr
.u
.val
);
3391 if (var
->name
== NULL
)
3392 var
->name
= spec_var
->name
;
3393 if (var
->file
== NULL
)
3394 var
->file
= strdup (spec_var
->file
);
3396 var
->line
= spec_var
->line
;
3397 if (var
->sec
== NULL
)
3398 var
->sec
= spec_var
->sec
;
3403 if (is_str_attr (attr
.form
))
3404 var
->name
= attr
.u
.str
;
3407 case DW_AT_decl_file
:
3408 var
->file
= concat_filename (unit
->line_table
,
3412 case DW_AT_decl_line
:
3413 var
->line
= attr
.u
.val
;
3416 case DW_AT_external
:
3417 if (attr
.u
.val
!= 0)
3421 case DW_AT_location
:
3425 case DW_FORM_block1
:
3426 case DW_FORM_block2
:
3427 case DW_FORM_block4
:
3428 case DW_FORM_exprloc
:
3429 if (attr
.u
.blk
->data
!= NULL
3430 && *attr
.u
.blk
->data
== DW_OP_addr
)
3434 /* Verify that DW_OP_addr is the only opcode in the
3435 location, in which case the block size will be 1
3436 plus the address size. */
3437 /* ??? For TLS variables, gcc can emit
3438 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
3439 which we don't handle here yet. */
3440 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
3441 var
->addr
= bfd_get (unit
->addr_size
* 8,
3443 attr
.u
.blk
->data
+ 1);
3458 if (high_pc_relative
)
3461 if (func
&& high_pc
!= 0)
3463 if (!arange_add (unit
, &func
->arange
, low_pc
, high_pc
))
3467 if (abbrev
->has_children
)
3471 if (nesting_level
>= nested_funcs_size
)
3473 struct nest_funcinfo
*tmp
;
3475 nested_funcs_size
*= 2;
3476 tmp
= (struct nest_funcinfo
*)
3477 bfd_realloc (nested_funcs
,
3478 nested_funcs_size
* sizeof (*nested_funcs
));
3483 nested_funcs
[nesting_level
].func
= 0;
3487 free (nested_funcs
);
3491 free (nested_funcs
);
3495 /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
3496 includes the compilation unit header that proceeds the DIE's, but
3497 does not include the length field that precedes each compilation
3498 unit header. END_PTR points one past the end of this comp unit.
3499 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
3501 This routine does not read the whole compilation unit; only enough
3502 to get to the line number information for the compilation unit. */
3504 static struct comp_unit
*
3505 parse_comp_unit (struct dwarf2_debug
*stash
,
3506 struct dwarf2_debug_file
*file
,
3508 bfd_vma unit_length
,
3509 bfd_byte
*info_ptr_unit
,
3510 unsigned int offset_size
)
3512 struct comp_unit
* unit
;
3513 unsigned int version
;
3514 bfd_uint64_t abbrev_offset
= 0;
3515 /* Initialize it just to avoid a GCC false warning. */
3516 unsigned int addr_size
= -1;
3517 struct abbrev_info
** abbrevs
;
3518 unsigned int abbrev_number
, bytes_read
, i
;
3519 struct abbrev_info
*abbrev
;
3520 struct attribute attr
;
3521 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
3524 bfd_vma high_pc
= 0;
3525 bfd
*abfd
= file
->bfd_ptr
;
3526 bfd_boolean high_pc_relative
= FALSE
;
3527 enum dwarf_unit_type unit_type
;
3529 version
= read_2_bytes (abfd
, info_ptr
, end_ptr
);
3531 if (version
< 2 || version
> 5)
3533 /* PR 19872: A version number of 0 probably means that there is padding
3534 at the end of the .debug_info section. Gold puts it there when
3535 performing an incremental link, for example. So do not generate
3536 an error, just return a NULL. */
3540 (_("DWARF error: found dwarf version '%u', this reader"
3541 " only handles version 2, 3, 4 and 5 information"), version
);
3542 bfd_set_error (bfd_error_bad_value
);
3548 unit_type
= DW_UT_compile
;
3551 unit_type
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3554 addr_size
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3558 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
3559 if (offset_size
== 4)
3560 abbrev_offset
= read_4_bytes (abfd
, info_ptr
, end_ptr
);
3562 abbrev_offset
= read_8_bytes (abfd
, info_ptr
, end_ptr
);
3563 info_ptr
+= offset_size
;
3567 addr_size
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3571 if (unit_type
== DW_UT_type
)
3573 /* Skip type signature. */
3576 /* Skip type offset. */
3577 info_ptr
+= offset_size
;
3580 if (addr_size
> sizeof (bfd_vma
))
3583 /* xgettext: c-format */
3584 (_("DWARF error: found address size '%u', this reader"
3585 " can not handle sizes greater than '%u'"),
3587 (unsigned int) sizeof (bfd_vma
));
3588 bfd_set_error (bfd_error_bad_value
);
3592 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
3595 ("DWARF error: found address size '%u', this reader"
3596 " can only handle address sizes '2', '4' and '8'", addr_size
);
3597 bfd_set_error (bfd_error_bad_value
);
3601 /* Read the abbrevs for this compilation unit into a table. */
3602 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
, file
);
3606 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
3608 info_ptr
+= bytes_read
;
3609 if (! abbrev_number
)
3611 /* PR 19872: An abbrev number of 0 probably means that there is padding
3612 at the end of the .debug_abbrev section. Gold puts it there when
3613 performing an incremental link, for example. So do not generate
3614 an error, just return a NULL. */
3618 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
3621 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
3623 bfd_set_error (bfd_error_bad_value
);
3627 amt
= sizeof (struct comp_unit
);
3628 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
3632 unit
->version
= version
;
3633 unit
->addr_size
= addr_size
;
3634 unit
->offset_size
= offset_size
;
3635 unit
->abbrevs
= abbrevs
;
3636 unit
->end_ptr
= end_ptr
;
3637 unit
->stash
= stash
;
3639 unit
->info_ptr_unit
= info_ptr_unit
;
3641 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3643 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
, end_ptr
);
3644 if (info_ptr
== NULL
)
3647 /* Store the data if it is of an attribute we want to keep in a
3648 partial symbol table. */
3651 case DW_AT_stmt_list
:
3653 unit
->line_offset
= attr
.u
.val
;
3657 if (is_str_attr (attr
.form
))
3658 unit
->name
= attr
.u
.str
;
3662 low_pc
= attr
.u
.val
;
3663 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
3664 this is the base address to use when reading location
3665 lists or range lists. */
3666 if (abbrev
->tag
== DW_TAG_compile_unit
)
3667 unit
->base_address
= low_pc
;
3671 high_pc
= attr
.u
.val
;
3672 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
3676 if (!read_rangelist (unit
, &unit
->arange
, attr
.u
.val
))
3680 case DW_AT_comp_dir
:
3682 char *comp_dir
= attr
.u
.str
;
3684 /* PR 17512: file: 1fe726be. */
3685 if (! is_str_attr (attr
.form
))
3688 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
3694 /* Irix 6.2 native cc prepends <machine>.: to the compilation
3695 directory, get rid of it. */
3696 char *cp
= strchr (comp_dir
, ':');
3698 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
3701 unit
->comp_dir
= comp_dir
;
3705 case DW_AT_language
:
3706 unit
->lang
= attr
.u
.val
;
3713 if (high_pc_relative
)
3717 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
3721 unit
->first_child_die_ptr
= info_ptr
;
3725 /* Return TRUE if UNIT may contain the address given by ADDR. When
3726 there are functions written entirely with inline asm statements, the
3727 range info in the compilation unit header may not be correct. We
3728 need to consult the line info table to see if a compilation unit
3729 really contains the given address. */
3732 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
3734 struct arange
*arange
;
3739 arange
= &unit
->arange
;
3742 if (addr
>= arange
->low
&& addr
< arange
->high
)
3744 arange
= arange
->next
;
3751 /* If UNIT contains ADDR, set the output parameters to the values for
3752 the line containing ADDR. The output parameters, FILENAME_PTR,
3753 FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects
3756 Returns the range of addresses covered by the entry that was used
3757 to fill in *LINENUMBER_PTR or 0 if it was not filled in. */
3760 comp_unit_find_nearest_line (struct comp_unit
*unit
,
3762 const char **filename_ptr
,
3763 struct funcinfo
**function_ptr
,
3764 unsigned int *linenumber_ptr
,
3765 unsigned int *discriminator_ptr
)
3769 if (!comp_unit_maybe_decode_line_info (unit
))
3772 *function_ptr
= NULL
;
3773 func_p
= lookup_address_in_function_table (unit
, addr
, function_ptr
);
3774 if (func_p
&& (*function_ptr
)->tag
== DW_TAG_inlined_subroutine
)
3775 unit
->stash
->inliner_chain
= *function_ptr
;
3777 return lookup_address_in_line_info_table (unit
->line_table
, addr
,
3783 /* Check to see if line info is already decoded in a comp_unit.
3784 If not, decode it. Returns TRUE if no errors were encountered;
3788 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
)
3793 if (! unit
->line_table
)
3795 if (! unit
->stmtlist
)
3801 unit
->line_table
= decode_line_info (unit
);
3803 if (! unit
->line_table
)
3809 if (unit
->first_child_die_ptr
< unit
->end_ptr
3810 && ! scan_unit_for_symbols (unit
))
3820 /* If UNIT contains SYM at ADDR, set the output parameters to the
3821 values for the line containing SYM. The output parameters,
3822 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
3825 Return TRUE if UNIT contains SYM, and no errors were encountered;
3829 comp_unit_find_line (struct comp_unit
*unit
,
3832 const char **filename_ptr
,
3833 unsigned int *linenumber_ptr
)
3835 if (!comp_unit_maybe_decode_line_info (unit
))
3838 if (sym
->flags
& BSF_FUNCTION
)
3839 return lookup_symbol_in_function_table (unit
, sym
, addr
,
3843 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
3848 static struct funcinfo
*
3849 reverse_funcinfo_list (struct funcinfo
*head
)
3851 struct funcinfo
*rhead
;
3852 struct funcinfo
*temp
;
3854 for (rhead
= NULL
; head
; head
= temp
)
3856 temp
= head
->prev_func
;
3857 head
->prev_func
= rhead
;
3863 static struct varinfo
*
3864 reverse_varinfo_list (struct varinfo
*head
)
3866 struct varinfo
*rhead
;
3867 struct varinfo
*temp
;
3869 for (rhead
= NULL
; head
; head
= temp
)
3871 temp
= head
->prev_var
;
3872 head
->prev_var
= rhead
;
3878 /* Extract all interesting funcinfos and varinfos of a compilation
3879 unit into hash tables for faster lookup. Returns TRUE if no
3880 errors were enountered; FALSE otherwise. */
3883 comp_unit_hash_info (struct dwarf2_debug
*stash
,
3884 struct comp_unit
*unit
,
3885 struct info_hash_table
*funcinfo_hash_table
,
3886 struct info_hash_table
*varinfo_hash_table
)
3888 struct funcinfo
* each_func
;
3889 struct varinfo
* each_var
;
3890 bfd_boolean okay
= TRUE
;
3892 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
3894 if (!comp_unit_maybe_decode_line_info (unit
))
3897 BFD_ASSERT (!unit
->cached
);
3899 /* To preserve the original search order, we went to visit the function
3900 infos in the reversed order of the list. However, making the list
3901 bi-directional use quite a bit of extra memory. So we reverse
3902 the list first, traverse the list in the now reversed order and
3903 finally reverse the list again to get back the original order. */
3904 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
3905 for (each_func
= unit
->function_table
;
3907 each_func
= each_func
->prev_func
)
3909 /* Skip nameless functions. */
3910 if (each_func
->name
)
3911 /* There is no need to copy name string into hash table as
3912 name string is either in the dwarf string buffer or
3913 info in the stash. */
3914 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
3915 (void*) each_func
, FALSE
);
3917 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
3921 /* We do the same for variable infos. */
3922 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
3923 for (each_var
= unit
->variable_table
;
3925 each_var
= each_var
->prev_var
)
3927 /* Skip stack vars and vars with no files or names. */
3928 if (! each_var
->stack
3929 && each_var
->file
!= NULL
3930 && each_var
->name
!= NULL
)
3931 /* There is no need to copy name string into hash table as
3932 name string is either in the dwarf string buffer or
3933 info in the stash. */
3934 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
3935 (void*) each_var
, FALSE
);
3938 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
3939 unit
->cached
= TRUE
;
3943 /* Locate a section in a BFD containing debugging info. The search starts
3944 from the section after AFTER_SEC, or from the first section in the BFD if
3945 AFTER_SEC is NULL. The search works by examining the names of the
3946 sections. There are three permissiable names. The first two are given
3947 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
3948 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
3949 This is a variation on the .debug_info section which has a checksum
3950 describing the contents appended onto the name. This allows the linker to
3951 identify and discard duplicate debugging sections for different
3952 compilation units. */
3953 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
3956 find_debug_info (bfd
*abfd
, const struct dwarf_debug_section
*debug_sections
,
3957 asection
*after_sec
)
3962 if (after_sec
== NULL
)
3964 look
= debug_sections
[debug_info
].uncompressed_name
;
3965 msec
= bfd_get_section_by_name (abfd
, look
);
3969 look
= debug_sections
[debug_info
].compressed_name
;
3972 msec
= bfd_get_section_by_name (abfd
, look
);
3977 for (msec
= abfd
->sections
; msec
!= NULL
; msec
= msec
->next
)
3978 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
3984 for (msec
= after_sec
->next
; msec
!= NULL
; msec
= msec
->next
)
3986 look
= debug_sections
[debug_info
].uncompressed_name
;
3987 if (strcmp (msec
->name
, look
) == 0)
3990 look
= debug_sections
[debug_info
].compressed_name
;
3991 if (look
!= NULL
&& strcmp (msec
->name
, look
) == 0)
3994 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
4001 /* Transfer VMAs from object file to separate debug file. */
4004 set_debug_vma (bfd
*orig_bfd
, bfd
*debug_bfd
)
4008 for (s
= orig_bfd
->sections
, d
= debug_bfd
->sections
;
4009 s
!= NULL
&& d
!= NULL
;
4010 s
= s
->next
, d
= d
->next
)
4012 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4014 /* ??? Assumes 1-1 correspondence between sections in the
4016 if (strcmp (s
->name
, d
->name
) == 0)
4018 d
->output_section
= s
->output_section
;
4019 d
->output_offset
= s
->output_offset
;
4025 /* If the dwarf2 info was found in a separate debug file, return the
4026 debug file section corresponding to the section in the original file
4027 and the debug file symbols. */
4030 _bfd_dwarf2_stash_syms (struct dwarf2_debug
*stash
, bfd
*abfd
,
4031 asection
**sec
, asymbol
***syms
)
4033 if (stash
->f
.bfd_ptr
!= abfd
)
4039 *syms
= stash
->f
.syms
;
4043 for (s
= abfd
->sections
, d
= stash
->f
.bfd_ptr
->sections
;
4044 s
!= NULL
&& d
!= NULL
;
4045 s
= s
->next
, d
= d
->next
)
4047 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4050 && strcmp (s
->name
, d
->name
) == 0)
4053 *syms
= stash
->f
.syms
;
4060 /* Unset vmas for adjusted sections in STASH. */
4063 unset_sections (struct dwarf2_debug
*stash
)
4066 struct adjusted_section
*p
;
4068 i
= stash
->adjusted_section_count
;
4069 p
= stash
->adjusted_sections
;
4070 for (; i
> 0; i
--, p
++)
4071 p
->section
->vma
= 0;
4074 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4075 relocatable object file. VMAs are normally all zero in relocatable
4076 object files, so if we want to distinguish locations in sections by
4077 address we need to set VMAs so the sections do not overlap. We
4078 also set VMA on .debug_info so that when we have multiple
4079 .debug_info sections (or the linkonce variant) they also do not
4080 overlap. The multiple .debug_info sections make up a single
4081 logical section. ??? We should probably do the same for other
4085 place_sections (bfd
*orig_bfd
, struct dwarf2_debug
*stash
)
4088 struct adjusted_section
*p
;
4090 const char *debug_info_name
;
4092 if (stash
->adjusted_section_count
!= 0)
4094 i
= stash
->adjusted_section_count
;
4095 p
= stash
->adjusted_sections
;
4096 for (; i
> 0; i
--, p
++)
4097 p
->section
->vma
= p
->adj_vma
;
4101 debug_info_name
= stash
->debug_sections
[debug_info
].uncompressed_name
;
4108 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4112 if ((sect
->output_section
!= NULL
4113 && sect
->output_section
!= sect
4114 && (sect
->flags
& SEC_DEBUGGING
) == 0)
4118 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
4119 || CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
));
4121 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
4127 if (abfd
== stash
->f
.bfd_ptr
)
4129 abfd
= stash
->f
.bfd_ptr
;
4133 stash
->adjusted_section_count
= -1;
4136 bfd_vma last_vma
= 0, last_dwarf
= 0;
4137 size_t amt
= i
* sizeof (struct adjusted_section
);
4139 p
= (struct adjusted_section
*) bfd_malloc (amt
);
4143 stash
->adjusted_sections
= p
;
4144 stash
->adjusted_section_count
= i
;
4151 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4156 if ((sect
->output_section
!= NULL
4157 && sect
->output_section
!= sect
4158 && (sect
->flags
& SEC_DEBUGGING
) == 0)
4162 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
4163 || CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
));
4165 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
4169 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
4173 BFD_ASSERT (sect
->alignment_power
== 0);
4174 sect
->vma
= last_dwarf
;
4179 /* Align the new address to the current section
4181 last_vma
= ((last_vma
4182 + ~(-((bfd_vma
) 1 << sect
->alignment_power
)))
4183 & (-((bfd_vma
) 1 << sect
->alignment_power
)));
4184 sect
->vma
= last_vma
;
4189 p
->adj_vma
= sect
->vma
;
4192 if (abfd
== stash
->f
.bfd_ptr
)
4194 abfd
= stash
->f
.bfd_ptr
;
4198 if (orig_bfd
!= stash
->f
.bfd_ptr
)
4199 set_debug_vma (orig_bfd
, stash
->f
.bfd_ptr
);
4204 /* Look up a funcinfo by name using the given info hash table. If found,
4205 also update the locations pointed to by filename_ptr and linenumber_ptr.
4207 This function returns TRUE if a funcinfo that matches the given symbol
4208 and address is found with any error; otherwise it returns FALSE. */
4211 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
4214 const char **filename_ptr
,
4215 unsigned int *linenumber_ptr
)
4217 struct funcinfo
* each_func
;
4218 struct funcinfo
* best_fit
= NULL
;
4219 bfd_vma best_fit_len
= 0;
4220 struct info_list_node
*node
;
4221 struct arange
*arange
;
4222 const char *name
= bfd_asymbol_name (sym
);
4223 asection
*sec
= bfd_asymbol_section (sym
);
4225 for (node
= lookup_info_hash_table (hash_table
, name
);
4229 each_func
= (struct funcinfo
*) node
->info
;
4230 for (arange
= &each_func
->arange
;
4232 arange
= arange
->next
)
4234 if ((!each_func
->sec
|| each_func
->sec
== sec
)
4235 && addr
>= arange
->low
4236 && addr
< arange
->high
4238 || arange
->high
- arange
->low
< best_fit_len
))
4240 best_fit
= each_func
;
4241 best_fit_len
= arange
->high
- arange
->low
;
4248 best_fit
->sec
= sec
;
4249 *filename_ptr
= best_fit
->file
;
4250 *linenumber_ptr
= best_fit
->line
;
4257 /* Look up a varinfo by name using the given info hash table. If found,
4258 also update the locations pointed to by filename_ptr and linenumber_ptr.
4260 This function returns TRUE if a varinfo that matches the given symbol
4261 and address is found with any error; otherwise it returns FALSE. */
4264 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
4267 const char **filename_ptr
,
4268 unsigned int *linenumber_ptr
)
4270 const char *name
= bfd_asymbol_name (sym
);
4271 asection
*sec
= bfd_asymbol_section (sym
);
4272 struct varinfo
* each
;
4273 struct info_list_node
*node
;
4275 for (node
= lookup_info_hash_table (hash_table
, name
);
4279 each
= (struct varinfo
*) node
->info
;
4280 if (each
->addr
== addr
4281 && (!each
->sec
|| each
->sec
== sec
))
4284 *filename_ptr
= each
->file
;
4285 *linenumber_ptr
= each
->line
;
4293 /* Update the funcinfo and varinfo info hash tables if they are
4294 not up to date. Returns TRUE if there is no error; otherwise
4295 returns FALSE and disable the info hash tables. */
4298 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
4300 struct comp_unit
*each
;
4302 /* Exit if hash tables are up-to-date. */
4303 if (stash
->f
.all_comp_units
== stash
->hash_units_head
)
4306 if (stash
->hash_units_head
)
4307 each
= stash
->hash_units_head
->prev_unit
;
4309 each
= stash
->f
.last_comp_unit
;
4313 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
4314 stash
->varinfo_hash_table
))
4316 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
4319 each
= each
->prev_unit
;
4322 stash
->hash_units_head
= stash
->f
.all_comp_units
;
4326 /* Check consistency of info hash tables. This is for debugging only. */
4328 static void ATTRIBUTE_UNUSED
4329 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
4331 struct comp_unit
*each_unit
;
4332 struct funcinfo
*each_func
;
4333 struct varinfo
*each_var
;
4334 struct info_list_node
*node
;
4337 for (each_unit
= stash
->f
.all_comp_units
;
4339 each_unit
= each_unit
->next_unit
)
4341 for (each_func
= each_unit
->function_table
;
4343 each_func
= each_func
->prev_func
)
4345 if (!each_func
->name
)
4347 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
4351 while (node
&& !found
)
4353 found
= node
->info
== each_func
;
4359 for (each_var
= each_unit
->variable_table
;
4361 each_var
= each_var
->prev_var
)
4363 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
4365 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
4369 while (node
&& !found
)
4371 found
= node
->info
== each_var
;
4379 /* Check to see if we want to enable the info hash tables, which consume
4380 quite a bit of memory. Currently we only check the number times
4381 bfd_dwarf2_find_line is called. In the future, we may also want to
4382 take the number of symbols into account. */
4385 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
4387 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
4389 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
4392 /* FIXME: Maybe we should check the reduce_memory_overheads
4393 and optimize fields in the bfd_link_info structure ? */
4395 /* Create hash tables. */
4396 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
4397 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
4398 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
4400 /* Turn off info hashes if any allocation above fails. */
4401 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
4404 /* We need a forced update so that the info hash tables will
4405 be created even though there is no compilation unit. That
4406 happens if STASH_INFO_HASH_TRIGGER is 0. */
4407 if (stash_maybe_update_info_hash_tables (stash
))
4408 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
4411 /* Find the file and line associated with a symbol and address using the
4412 info hash tables of a stash. If there is a match, the function returns
4413 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
4414 otherwise it returns FALSE. */
4417 stash_find_line_fast (struct dwarf2_debug
*stash
,
4420 const char **filename_ptr
,
4421 unsigned int *linenumber_ptr
)
4423 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
4425 if (sym
->flags
& BSF_FUNCTION
)
4426 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
4427 filename_ptr
, linenumber_ptr
);
4428 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
4429 filename_ptr
, linenumber_ptr
);
4432 /* Save current section VMAs. */
4435 save_section_vma (const bfd
*abfd
, struct dwarf2_debug
*stash
)
4440 if (abfd
->section_count
== 0)
4442 stash
->sec_vma
= bfd_malloc (sizeof (*stash
->sec_vma
) * abfd
->section_count
);
4443 if (stash
->sec_vma
== NULL
)
4445 stash
->sec_vma_count
= abfd
->section_count
;
4446 for (i
= 0, s
= abfd
->sections
;
4447 s
!= NULL
&& i
< abfd
->section_count
;
4450 if (s
->output_section
!= NULL
)
4451 stash
->sec_vma
[i
] = s
->output_section
->vma
+ s
->output_offset
;
4453 stash
->sec_vma
[i
] = s
->vma
;
4458 /* Compare current section VMAs against those at the time the stash
4459 was created. If find_nearest_line is used in linker warnings or
4460 errors early in the link process, the debug info stash will be
4461 invalid for later calls. This is because we relocate debug info
4462 sections, so the stashed section contents depend on symbol values,
4463 which in turn depend on section VMAs. */
4466 section_vma_same (const bfd
*abfd
, const struct dwarf2_debug
*stash
)
4471 /* PR 24334: If the number of sections in ABFD has changed between
4472 when the stash was created and now, then we cannot trust the
4473 stashed vma information. */
4474 if (abfd
->section_count
!= stash
->sec_vma_count
)
4477 for (i
= 0, s
= abfd
->sections
;
4478 s
!= NULL
&& i
< abfd
->section_count
;
4483 if (s
->output_section
!= NULL
)
4484 vma
= s
->output_section
->vma
+ s
->output_offset
;
4487 if (vma
!= stash
->sec_vma
[i
])
4493 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
4494 If DEBUG_BFD is not specified, we read debug information from ABFD
4495 or its gnu_debuglink. The results will be stored in PINFO.
4496 The function returns TRUE iff debug information is ready. */
4499 _bfd_dwarf2_slurp_debug_info (bfd
*abfd
, bfd
*debug_bfd
,
4500 const struct dwarf_debug_section
*debug_sections
,
4503 bfd_boolean do_place
)
4505 size_t amt
= sizeof (struct dwarf2_debug
);
4506 bfd_size_type total_size
;
4508 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
4512 if (stash
->orig_bfd
== abfd
4513 && section_vma_same (abfd
, stash
))
4515 /* Check that we did previously find some debug information
4516 before attempting to make use of it. */
4517 if (stash
->f
.bfd_ptr
!= NULL
)
4519 if (do_place
&& !place_sections (abfd
, stash
))
4526 _bfd_dwarf2_cleanup_debug_info (abfd
, pinfo
);
4527 memset (stash
, 0, amt
);
4531 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
4535 stash
->orig_bfd
= abfd
;
4536 stash
->debug_sections
= debug_sections
;
4537 stash
->f
.syms
= symbols
;
4538 if (!save_section_vma (abfd
, stash
))
4541 stash
->f
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
4542 del_abbrev
, calloc
, free
);
4543 if (!stash
->f
.abbrev_offsets
)
4546 stash
->alt
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
4547 del_abbrev
, calloc
, free
);
4548 if (!stash
->alt
.abbrev_offsets
)
4553 if (debug_bfd
== NULL
)
4556 msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4557 if (msec
== NULL
&& abfd
== debug_bfd
)
4559 char * debug_filename
;
4561 debug_filename
= bfd_follow_build_id_debuglink (abfd
, DEBUGDIR
);
4562 if (debug_filename
== NULL
)
4563 debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
4565 if (debug_filename
== NULL
)
4566 /* No dwarf2 info, and no gnu_debuglink to follow.
4567 Note that at this point the stash has been allocated, but
4568 contains zeros. This lets future calls to this function
4569 fail more quickly. */
4572 debug_bfd
= bfd_openr (debug_filename
, NULL
);
4573 free (debug_filename
);
4574 if (debug_bfd
== NULL
)
4575 /* FIXME: Should we report our failure to follow the debuglink ? */
4578 /* Set BFD_DECOMPRESS to decompress debug sections. */
4579 debug_bfd
->flags
|= BFD_DECOMPRESS
;
4580 if (!bfd_check_format (debug_bfd
, bfd_object
)
4581 || (msec
= find_debug_info (debug_bfd
,
4582 debug_sections
, NULL
)) == NULL
4583 || !bfd_generic_link_read_symbols (debug_bfd
))
4585 bfd_close (debug_bfd
);
4589 symbols
= bfd_get_outsymbols (debug_bfd
);
4590 stash
->f
.syms
= symbols
;
4591 stash
->close_on_cleanup
= TRUE
;
4593 stash
->f
.bfd_ptr
= debug_bfd
;
4596 && !place_sections (abfd
, stash
))
4599 /* There can be more than one DWARF2 info section in a BFD these
4600 days. First handle the easy case when there's only one. If
4601 there's more than one, try case two: none of the sections is
4602 compressed. In that case, read them all in and produce one
4603 large stash. We do this in two passes - in the first pass we
4604 just accumulate the section sizes, and in the second pass we
4605 read in the section's contents. (The allows us to avoid
4606 reallocing the data as we add sections to the stash.) If
4607 some or all sections are compressed, then do things the slow
4608 way, with a bunch of reallocs. */
4610 if (! find_debug_info (debug_bfd
, debug_sections
, msec
))
4612 /* Case 1: only one info section. */
4613 total_size
= msec
->size
;
4614 if (! read_section (debug_bfd
, &stash
->debug_sections
[debug_info
],
4616 &stash
->f
.dwarf_info_buffer
, &total_size
))
4621 /* Case 2: multiple sections. */
4622 for (total_size
= 0;
4624 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
4626 /* Catch PR25070 testcase overflowing size calculation here. */
4627 if (total_size
+ msec
->size
< total_size
4628 || total_size
+ msec
->size
< msec
->size
)
4630 bfd_set_error (bfd_error_no_memory
);
4633 total_size
+= msec
->size
;
4636 stash
->f
.dwarf_info_buffer
= (bfd_byte
*) bfd_malloc (total_size
);
4637 if (stash
->f
.dwarf_info_buffer
== NULL
)
4641 for (msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4643 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
4651 if (!(bfd_simple_get_relocated_section_contents
4652 (debug_bfd
, msec
, stash
->f
.dwarf_info_buffer
+ total_size
,
4660 stash
->f
.info_ptr
= stash
->f
.dwarf_info_buffer
;
4661 stash
->f
.dwarf_info_size
= total_size
;
4665 /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
4667 static struct comp_unit
*
4668 stash_comp_unit (struct dwarf2_debug
*stash
, struct dwarf2_debug_file
*file
)
4670 bfd_size_type length
;
4671 unsigned int offset_size
;
4672 bfd_byte
*info_ptr_unit
= file
->info_ptr
;
4673 bfd_byte
*info_ptr_end
= file
->dwarf_info_buffer
+ file
->dwarf_info_size
;
4675 if (file
->info_ptr
>= info_ptr_end
)
4678 length
= read_4_bytes (file
->bfd_ptr
, file
->info_ptr
, info_ptr_end
);
4679 /* A 0xffffff length is the DWARF3 way of indicating
4680 we use 64-bit offsets, instead of 32-bit offsets. */
4681 if (length
== 0xffffffff)
4684 length
= read_8_bytes (file
->bfd_ptr
, file
->info_ptr
+ 4,
4686 file
->info_ptr
+= 12;
4688 /* A zero length is the IRIX way of indicating 64-bit offsets,
4689 mostly because the 64-bit length will generally fit in 32
4690 bits, and the endianness helps. */
4691 else if (length
== 0)
4694 length
= read_4_bytes (file
->bfd_ptr
, file
->info_ptr
+ 4,
4696 file
->info_ptr
+= 8;
4698 /* In the absence of the hints above, we assume 32-bit DWARF2
4699 offsets even for targets with 64-bit addresses, because:
4700 a) most of the time these targets will not have generated
4701 more than 2Gb of debug info and so will not need 64-bit
4704 b) if they do use 64-bit offsets but they are not using
4705 the size hints that are tested for above then they are
4706 not conforming to the DWARF3 standard anyway. */
4710 file
->info_ptr
+= 4;
4714 && file
->info_ptr
+ length
<= info_ptr_end
4715 && file
->info_ptr
+ length
> file
->info_ptr
)
4717 struct comp_unit
*each
= parse_comp_unit (stash
, file
,
4718 file
->info_ptr
, length
,
4719 info_ptr_unit
, offset_size
);
4722 if (file
->all_comp_units
)
4723 file
->all_comp_units
->prev_unit
= each
;
4725 file
->last_comp_unit
= each
;
4727 each
->next_unit
= file
->all_comp_units
;
4728 file
->all_comp_units
= each
;
4730 file
->info_ptr
+= length
;
4735 /* Don't trust any of the DWARF info after a corrupted length or
4737 file
->info_ptr
= info_ptr_end
;
4741 /* Hash function for an asymbol. */
4744 hash_asymbol (const void *sym
)
4746 const asymbol
*asym
= sym
;
4747 return htab_hash_string (asym
->name
);
4750 /* Equality function for asymbols. */
4753 eq_asymbol (const void *a
, const void *b
)
4755 const asymbol
*sa
= a
;
4756 const asymbol
*sb
= b
;
4757 return strcmp (sa
->name
, sb
->name
) == 0;
4760 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
4761 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
4762 symbol in SYMBOLS and return the difference between the low_pc and
4763 the symbol's address. Returns 0 if no suitable symbol could be found. */
4766 _bfd_dwarf2_find_symbol_bias (asymbol
** symbols
, void ** pinfo
)
4768 struct dwarf2_debug
*stash
;
4769 struct comp_unit
* unit
;
4771 bfd_signed_vma result
= 0;
4774 stash
= (struct dwarf2_debug
*) *pinfo
;
4776 if (stash
== NULL
|| symbols
== NULL
)
4779 sym_hash
= htab_create_alloc (10, hash_asymbol
, eq_asymbol
,
4780 NULL
, xcalloc
, free
);
4781 for (psym
= symbols
; * psym
!= NULL
; psym
++)
4783 asymbol
* sym
= * psym
;
4785 if (sym
->flags
& BSF_FUNCTION
&& sym
->section
!= NULL
)
4787 void **slot
= htab_find_slot (sym_hash
, sym
, INSERT
);
4792 for (unit
= stash
->f
.all_comp_units
; unit
; unit
= unit
->next_unit
)
4794 struct funcinfo
* func
;
4796 comp_unit_maybe_decode_line_info (unit
);
4798 for (func
= unit
->function_table
; func
!= NULL
; func
= func
->prev_func
)
4799 if (func
->name
&& func
->arange
.low
)
4801 asymbol search
, *sym
;
4803 /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
4805 search
.name
= func
->name
;
4806 sym
= htab_find (sym_hash
, &search
);
4809 result
= ((bfd_signed_vma
) func
->arange
.low
) -
4810 ((bfd_signed_vma
) (sym
->value
+ sym
->section
->vma
));
4817 htab_delete (sym_hash
);
4821 /* Find the source code location of SYMBOL. If SYMBOL is NULL
4822 then find the nearest source code location corresponding to
4823 the address SECTION + OFFSET.
4824 Returns 1 if the line is found without error and fills in
4825 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
4826 NULL the FUNCTIONNAME_PTR is also filled in.
4827 Returns 2 if partial information from _bfd_elf_find_function is
4828 returned (function and maybe file) by looking at symbols. DWARF2
4829 info is present but not regarding the requested code location.
4830 Returns 0 otherwise.
4831 SYMBOLS contains the symbol table for ABFD.
4832 DEBUG_SECTIONS contains the name of the dwarf debug sections. */
4835 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
4840 const char **filename_ptr
,
4841 const char **functionname_ptr
,
4842 unsigned int *linenumber_ptr
,
4843 unsigned int *discriminator_ptr
,
4844 const struct dwarf_debug_section
*debug_sections
,
4847 /* Read each compilation unit from the section .debug_info, and check
4848 to see if it contains the address we are searching for. If yes,
4849 lookup the address, and return the line number info. If no, go
4850 on to the next compilation unit.
4852 We keep a list of all the previously read compilation units, and
4853 a pointer to the next un-read compilation unit. Check the
4854 previously read units before reading more. */
4855 struct dwarf2_debug
*stash
;
4856 /* What address are we looking for? */
4858 struct comp_unit
* each
;
4859 struct funcinfo
*function
= NULL
;
4861 bfd_boolean do_line
;
4863 *filename_ptr
= NULL
;
4864 if (functionname_ptr
!= NULL
)
4865 *functionname_ptr
= NULL
;
4866 *linenumber_ptr
= 0;
4867 if (discriminator_ptr
)
4868 *discriminator_ptr
= 0;
4870 if (! _bfd_dwarf2_slurp_debug_info (abfd
, NULL
, debug_sections
,
4872 (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0))
4875 stash
= (struct dwarf2_debug
*) *pinfo
;
4877 do_line
= symbol
!= NULL
;
4880 BFD_ASSERT (section
== NULL
&& offset
== 0 && functionname_ptr
== NULL
);
4881 section
= bfd_asymbol_section (symbol
);
4882 addr
= symbol
->value
;
4886 BFD_ASSERT (section
!= NULL
&& functionname_ptr
!= NULL
);
4889 /* If we have no SYMBOL but the section we're looking at is not a
4890 code section, then take a look through the list of symbols to see
4891 if we have a symbol at the address we're looking for. If we do
4892 then use this to look up line information. This will allow us to
4893 give file and line results for data symbols. We exclude code
4894 symbols here, if we look up a function symbol and then look up the
4895 line information we'll actually return the line number for the
4896 opening '{' rather than the function definition line. This is
4897 because looking up by symbol uses the line table, in which the
4898 first line for a function is usually the opening '{', while
4899 looking up the function by section + offset uses the
4900 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
4901 which will be the line of the function name. */
4902 if (symbols
!= NULL
&& (section
->flags
& SEC_CODE
) == 0)
4906 for (tmp
= symbols
; (*tmp
) != NULL
; ++tmp
)
4907 if ((*tmp
)->the_bfd
== abfd
4908 && (*tmp
)->section
== section
4909 && (*tmp
)->value
== offset
4910 && ((*tmp
)->flags
& BSF_SECTION_SYM
) == 0)
4914 /* For local symbols, keep going in the hope we find a
4916 if ((symbol
->flags
& BSF_GLOBAL
) != 0)
4922 if (section
->output_section
)
4923 addr
+= section
->output_section
->vma
+ section
->output_offset
;
4925 addr
+= section
->vma
;
4927 /* A null info_ptr indicates that there is no dwarf2 info
4928 (or that an error occured while setting up the stash). */
4929 if (! stash
->f
.info_ptr
)
4932 stash
->inliner_chain
= NULL
;
4934 /* Check the previously read comp. units first. */
4937 /* The info hash tables use quite a bit of memory. We may not want to
4938 always use them. We use some heuristics to decide if and when to
4940 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
4941 stash_maybe_enable_info_hash_tables (abfd
, stash
);
4943 /* Keep info hash table up to date if they are available. Note that we
4944 may disable the hash tables if there is any error duing update. */
4945 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
4946 stash_maybe_update_info_hash_tables (stash
);
4948 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
4950 found
= stash_find_line_fast (stash
, symbol
, addr
, filename_ptr
,
4957 /* Check the previously read comp. units first. */
4958 for (each
= stash
->f
.all_comp_units
; each
; each
= each
->next_unit
)
4959 if ((symbol
->flags
& BSF_FUNCTION
) == 0
4960 || each
->arange
.high
== 0
4961 || comp_unit_contains_address (each
, addr
))
4963 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
4972 bfd_vma min_range
= (bfd_vma
) -1;
4973 const char * local_filename
= NULL
;
4974 struct funcinfo
*local_function
= NULL
;
4975 unsigned int local_linenumber
= 0;
4976 unsigned int local_discriminator
= 0;
4978 for (each
= stash
->f
.all_comp_units
; each
; each
= each
->next_unit
)
4980 bfd_vma range
= (bfd_vma
) -1;
4982 found
= ((each
->arange
.high
== 0
4983 || comp_unit_contains_address (each
, addr
))
4984 && (range
= (comp_unit_find_nearest_line
4985 (each
, addr
, &local_filename
,
4986 &local_function
, &local_linenumber
,
4987 &local_discriminator
))) != 0);
4990 /* PRs 15935 15994: Bogus debug information may have provided us
4991 with an erroneous match. We attempt to counter this by
4992 selecting the match that has the smallest address range
4993 associated with it. (We are assuming that corrupt debug info
4994 will tend to result in extra large address ranges rather than
4995 extra small ranges).
4997 This does mean that we scan through all of the CUs associated
4998 with the bfd each time this function is called. But this does
4999 have the benefit of producing consistent results every time the
5000 function is called. */
5001 if (range
<= min_range
)
5003 if (filename_ptr
&& local_filename
)
5004 * filename_ptr
= local_filename
;
5006 function
= local_function
;
5007 if (discriminator_ptr
&& local_discriminator
)
5008 * discriminator_ptr
= local_discriminator
;
5009 if (local_linenumber
)
5010 * linenumber_ptr
= local_linenumber
;
5016 if (* linenumber_ptr
)
5023 /* Read each remaining comp. units checking each as they are read. */
5024 while ((each
= stash_comp_unit (stash
, &stash
->f
)) != NULL
)
5026 /* DW_AT_low_pc and DW_AT_high_pc are optional for
5027 compilation units. If we don't have them (i.e.,
5028 unit->high == 0), we need to consult the line info table
5029 to see if a compilation unit contains the given
5032 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
5033 || each
->arange
.high
== 0
5034 || comp_unit_contains_address (each
, addr
))
5035 && comp_unit_find_line (each
, symbol
, addr
,
5036 filename_ptr
, linenumber_ptr
));
5038 found
= ((each
->arange
.high
== 0
5039 || comp_unit_contains_address (each
, addr
))
5040 && comp_unit_find_nearest_line (each
, addr
,
5044 discriminator_ptr
) != 0);
5051 if (functionname_ptr
&& function
&& function
->is_linkage
)
5052 *functionname_ptr
= function
->name
;
5053 else if (functionname_ptr
5054 && (!*functionname_ptr
5055 || (function
&& !function
->is_linkage
)))
5058 asymbol
**syms
= symbols
;
5059 asection
*sec
= section
;
5061 _bfd_dwarf2_stash_syms (stash
, abfd
, &sec
, &syms
);
5062 fun
= _bfd_elf_find_function (abfd
, syms
, sec
, offset
,
5063 *filename_ptr
? NULL
: filename_ptr
,
5066 if (!found
&& fun
!= NULL
)
5069 if (function
&& !function
->is_linkage
)
5073 sec_vma
= section
->vma
;
5074 if (section
->output_section
!= NULL
)
5075 sec_vma
= section
->output_section
->vma
+ section
->output_offset
;
5077 && fun
->value
+ sec_vma
== function
->arange
.low
)
5078 function
->name
= *functionname_ptr
;
5079 /* Even if we didn't find a linkage name, say that we have
5080 to stop a repeated search of symbols. */
5081 function
->is_linkage
= TRUE
;
5085 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
5086 unset_sections (stash
);
5092 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
5093 const char **filename_ptr
,
5094 const char **functionname_ptr
,
5095 unsigned int *linenumber_ptr
,
5098 struct dwarf2_debug
*stash
;
5100 stash
= (struct dwarf2_debug
*) *pinfo
;
5103 struct funcinfo
*func
= stash
->inliner_chain
;
5105 if (func
&& func
->caller_func
)
5107 *filename_ptr
= func
->caller_file
;
5108 *functionname_ptr
= func
->caller_func
->name
;
5109 *linenumber_ptr
= func
->caller_line
;
5110 stash
->inliner_chain
= func
->caller_func
;
5119 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
, void **pinfo
)
5121 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
5122 struct comp_unit
*each
;
5123 struct dwarf2_debug_file
*file
;
5125 if (abfd
== NULL
|| stash
== NULL
)
5128 if (stash
->varinfo_hash_table
)
5129 bfd_hash_table_free (&stash
->varinfo_hash_table
->base
);
5130 if (stash
->funcinfo_hash_table
)
5131 bfd_hash_table_free (&stash
->funcinfo_hash_table
->base
);
5136 for (each
= file
->all_comp_units
; each
; each
= each
->next_unit
)
5138 struct funcinfo
*function_table
= each
->function_table
;
5139 struct varinfo
*variable_table
= each
->variable_table
;
5141 if (each
->line_table
&& each
->line_table
!= file
->line_table
)
5143 free (each
->line_table
->files
);
5144 free (each
->line_table
->dirs
);
5147 if (each
->lookup_funcinfo_table
)
5149 free (each
->lookup_funcinfo_table
);
5150 each
->lookup_funcinfo_table
= NULL
;
5153 while (function_table
)
5155 if (function_table
->file
)
5157 free (function_table
->file
);
5158 function_table
->file
= NULL
;
5160 if (function_table
->caller_file
)
5162 free (function_table
->caller_file
);
5163 function_table
->caller_file
= NULL
;
5165 function_table
= function_table
->prev_func
;
5168 while (variable_table
)
5170 if (variable_table
->file
)
5172 free (variable_table
->file
);
5173 variable_table
->file
= NULL
;
5175 variable_table
= variable_table
->prev_var
;
5179 if (file
->line_table
)
5181 free (file
->line_table
->files
);
5182 free (file
->line_table
->dirs
);
5184 htab_delete (file
->abbrev_offsets
);
5186 free (file
->dwarf_line_str_buffer
);
5187 free (file
->dwarf_str_buffer
);
5188 free (file
->dwarf_ranges_buffer
);
5189 free (file
->dwarf_line_buffer
);
5190 free (file
->dwarf_abbrev_buffer
);
5191 free (file
->dwarf_info_buffer
);
5192 if (file
== &stash
->alt
)
5196 free (stash
->sec_vma
);
5197 free (stash
->adjusted_sections
);
5198 if (stash
->close_on_cleanup
)
5199 bfd_close (stash
->f
.bfd_ptr
);
5200 if (stash
->alt
.bfd_ptr
)
5201 bfd_close (stash
->alt
.bfd_ptr
);
5204 /* Find the function to a particular section and offset,
5205 for error reporting. */
5208 _bfd_elf_find_function (bfd
*abfd
,
5212 const char **filename_ptr
,
5213 const char **functionname_ptr
)
5215 struct elf_find_function_cache
5217 asection
*last_section
;
5219 const char *filename
;
5220 bfd_size_type func_size
;
5223 if (symbols
== NULL
)
5226 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
5229 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
5232 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
5233 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
5237 if (cache
->last_section
!= section
5238 || cache
->func
== NULL
5239 || offset
< cache
->func
->value
5240 || offset
>= cache
->func
->value
+ cache
->func_size
)
5245 /* ??? Given multiple file symbols, it is impossible to reliably
5246 choose the right file name for global symbols. File symbols are
5247 local symbols, and thus all file symbols must sort before any
5248 global symbols. The ELF spec may be interpreted to say that a
5249 file symbol must sort before other local symbols, but currently
5250 ld -r doesn't do this. So, for ld -r output, it is possible to
5251 make a better choice of file name for local symbols by ignoring
5252 file symbols appearing after a given local symbol. */
5253 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
5254 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5258 state
= nothing_seen
;
5259 cache
->filename
= NULL
;
5261 cache
->func_size
= 0;
5262 cache
->last_section
= section
;
5264 for (p
= symbols
; *p
!= NULL
; p
++)
5270 if ((sym
->flags
& BSF_FILE
) != 0)
5273 if (state
== symbol_seen
)
5274 state
= file_after_symbol_seen
;
5278 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
5280 && code_off
<= offset
5281 && (code_off
> low_func
5282 || (code_off
== low_func
5283 && size
> cache
->func_size
)))
5286 cache
->func_size
= size
;
5287 cache
->filename
= NULL
;
5288 low_func
= code_off
;
5290 && ((sym
->flags
& BSF_LOCAL
) != 0
5291 || state
!= file_after_symbol_seen
))
5292 cache
->filename
= bfd_asymbol_name (file
);
5294 if (state
== nothing_seen
)
5295 state
= symbol_seen
;
5299 if (cache
->func
== NULL
)
5303 *filename_ptr
= cache
->filename
;
5304 if (functionname_ptr
)
5305 *functionname_ptr
= bfd_asymbol_name (cache
->func
);