2 Copyright (C) 1994-2021 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 /* Pointer to the .debug_rnglists section loaded into memory. */
134 bfd_byte
*dwarf_rnglists_buffer
;
136 /* Length of the loaded .debug_rnglists section. */
137 bfd_size_type dwarf_rnglists_size
;
139 /* A list of all previously read comp_units. */
140 struct comp_unit
*all_comp_units
;
142 /* Last comp unit in list above. */
143 struct comp_unit
*last_comp_unit
;
145 /* Line table at line_offset zero. */
146 struct line_info_table
*line_table
;
148 /* Hash table to map offsets to decoded abbrevs. */
149 htab_t abbrev_offsets
;
154 /* Names of the debug sections. */
155 const struct dwarf_debug_section
*debug_sections
;
157 /* Per-file stuff. */
158 struct dwarf2_debug_file f
, alt
;
160 /* Pointer to the original bfd for which debug was loaded. This is what
161 we use to compare and so check that the cached debug data is still
162 valid - it saves having to possibly dereference the gnu_debuglink each
166 /* If the most recent call to bfd_find_nearest_line was given an
167 address in an inlined function, preserve a pointer into the
168 calling chain for subsequent calls to bfd_find_inliner_info to
170 struct funcinfo
*inliner_chain
;
172 /* Section VMAs at the time the stash was built. */
174 /* Number of sections in the SEC_VMA table. */
175 unsigned int sec_vma_count
;
177 /* Number of sections whose VMA we must adjust. */
178 int adjusted_section_count
;
180 /* Array of sections with adjusted VMA. */
181 struct adjusted_section
*adjusted_sections
;
183 /* Number of times find_line is called. This is used in
184 the heuristic for enabling the info hash tables. */
187 #define STASH_INFO_HASH_TRIGGER 100
189 /* Hash table mapping symbol names to function infos. */
190 struct info_hash_table
*funcinfo_hash_table
;
192 /* Hash table mapping symbol names to variable infos. */
193 struct info_hash_table
*varinfo_hash_table
;
195 /* Head of comp_unit list in the last hash table update. */
196 struct comp_unit
*hash_units_head
;
198 /* Status of info hash. */
199 int info_hash_status
;
200 #define STASH_INFO_HASH_OFF 0
201 #define STASH_INFO_HASH_ON 1
202 #define STASH_INFO_HASH_DISABLED 2
204 /* True if we opened bfd_ptr. */
205 bool close_on_cleanup
;
215 /* A minimal decoding of DWARF2 compilation units. We only decode
216 what's needed to get to the line number information. */
220 /* Chain the previously read compilation units. */
221 struct comp_unit
*next_unit
;
223 /* Likewise, chain the compilation unit read after this one.
224 The comp units are stored in reversed reading order. */
225 struct comp_unit
*prev_unit
;
227 /* Keep the bfd convenient (for memory allocation). */
230 /* The lowest and highest addresses contained in this compilation
231 unit as specified in the compilation unit header. */
232 struct arange arange
;
234 /* The DW_AT_name attribute (for error messages). */
237 /* The abbrev hash table. */
238 struct abbrev_info
**abbrevs
;
240 /* DW_AT_language. */
243 /* Note that an error was found by comp_unit_find_nearest_line. */
246 /* The DW_AT_comp_dir attribute. */
249 /* TRUE if there is a line number table associated with this comp. unit. */
252 /* Pointer to the current comp_unit so that we can find a given entry
254 bfd_byte
*info_ptr_unit
;
256 /* The offset into .debug_line of the line number table. */
257 unsigned long line_offset
;
259 /* Pointer to the first child die for the comp unit. */
260 bfd_byte
*first_child_die_ptr
;
262 /* The end of the comp unit. */
265 /* The decoded line number, NULL if not yet decoded. */
266 struct line_info_table
*line_table
;
268 /* A list of the functions found in this comp. unit. */
269 struct funcinfo
*function_table
;
271 /* A table of function information references searchable by address. */
272 struct lookup_funcinfo
*lookup_funcinfo_table
;
274 /* Number of functions in the function_table and sorted_function_table. */
275 bfd_size_type number_of_functions
;
277 /* A list of the variables found in this comp. unit. */
278 struct varinfo
*variable_table
;
280 /* Pointers to dwarf2_debug structures. */
281 struct dwarf2_debug
*stash
;
282 struct dwarf2_debug_file
*file
;
284 /* DWARF format version for this unit - from unit header. */
287 /* Address size for this unit - from unit header. */
288 unsigned char addr_size
;
290 /* Offset size for this unit - from unit header. */
291 unsigned char offset_size
;
293 /* Base address for this unit - from DW_AT_low_pc attribute of
294 DW_TAG_compile_unit DIE */
295 bfd_vma base_address
;
297 /* TRUE if symbols are cached in hash table for faster lookup by name. */
301 /* This data structure holds the information of an abbrev. */
304 unsigned int number
; /* Number identifying abbrev. */
305 enum dwarf_tag tag
; /* DWARF tag. */
306 bool has_children
; /* TRUE if the abbrev has children. */
307 unsigned int num_attrs
; /* Number of attributes. */
308 struct attr_abbrev
* attrs
; /* An array of attribute descriptions. */
309 struct abbrev_info
* next
; /* Next in chain. */
314 enum dwarf_attribute name
;
315 enum dwarf_form form
;
316 bfd_vma implicit_const
;
319 /* Map of uncompressed DWARF debug section name to compressed one. It
320 is terminated by NULL uncompressed_name. */
322 const struct dwarf_debug_section dwarf_debug_sections
[] =
324 { ".debug_abbrev", ".zdebug_abbrev" },
325 { ".debug_aranges", ".zdebug_aranges" },
326 { ".debug_frame", ".zdebug_frame" },
327 { ".debug_info", ".zdebug_info" },
328 { ".debug_info", ".zdebug_info" },
329 { ".debug_line", ".zdebug_line" },
330 { ".debug_loc", ".zdebug_loc" },
331 { ".debug_macinfo", ".zdebug_macinfo" },
332 { ".debug_macro", ".zdebug_macro" },
333 { ".debug_pubnames", ".zdebug_pubnames" },
334 { ".debug_pubtypes", ".zdebug_pubtypes" },
335 { ".debug_ranges", ".zdebug_ranges" },
336 { ".debug_rnglists", ".zdebug_rnglist" },
337 { ".debug_static_func", ".zdebug_static_func" },
338 { ".debug_static_vars", ".zdebug_static_vars" },
339 { ".debug_str", ".zdebug_str", },
340 { ".debug_str", ".zdebug_str", },
341 { ".debug_line_str", ".zdebug_line_str", },
342 { ".debug_types", ".zdebug_types" },
343 /* GNU DWARF 1 extensions */
344 { ".debug_sfnames", ".zdebug_sfnames" },
345 { ".debug_srcinfo", ".zebug_srcinfo" },
346 /* SGI/MIPS DWARF 2 extensions */
347 { ".debug_funcnames", ".zdebug_funcnames" },
348 { ".debug_typenames", ".zdebug_typenames" },
349 { ".debug_varnames", ".zdebug_varnames" },
350 { ".debug_weaknames", ".zdebug_weaknames" },
354 /* NB/ Numbers in this enum must match up with indices
355 into the dwarf_debug_sections[] array above. */
356 enum dwarf_debug_section_enum
386 /* A static assertion. */
387 extern int dwarf_debug_section_assert
[ARRAY_SIZE (dwarf_debug_sections
)
388 == debug_max
+ 1 ? 1 : -1];
390 #ifndef ABBREV_HASH_SIZE
391 #define ABBREV_HASH_SIZE 121
393 #ifndef ATTR_ALLOC_CHUNK
394 #define ATTR_ALLOC_CHUNK 4
397 /* Variable and function hash tables. This is used to speed up look-up
398 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
399 In order to share code between variable and function infos, we use
400 a list of untyped pointer for all variable/function info associated with
401 a symbol. We waste a bit of memory for list with one node but that
402 simplifies the code. */
404 struct info_list_node
406 struct info_list_node
*next
;
410 /* Info hash entry. */
411 struct info_hash_entry
413 struct bfd_hash_entry root
;
414 struct info_list_node
*head
;
417 struct info_hash_table
419 struct bfd_hash_table base
;
422 /* Function to create a new entry in info hash table. */
424 static struct bfd_hash_entry
*
425 info_hash_table_newfunc (struct bfd_hash_entry
*entry
,
426 struct bfd_hash_table
*table
,
429 struct info_hash_entry
*ret
= (struct info_hash_entry
*) entry
;
431 /* Allocate the structure if it has not already been allocated by a
435 ret
= (struct info_hash_entry
*) bfd_hash_allocate (table
,
441 /* Call the allocation method of the base class. */
442 ret
= ((struct info_hash_entry
*)
443 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
445 /* Initialize the local fields here. */
449 return (struct bfd_hash_entry
*) ret
;
452 /* Function to create a new info hash table. It returns a pointer to the
453 newly created table or NULL if there is any error. We need abfd
454 solely for memory allocation. */
456 static struct info_hash_table
*
457 create_info_hash_table (bfd
*abfd
)
459 struct info_hash_table
*hash_table
;
461 hash_table
= ((struct info_hash_table
*)
462 bfd_alloc (abfd
, sizeof (struct info_hash_table
)));
466 if (!bfd_hash_table_init (&hash_table
->base
, info_hash_table_newfunc
,
467 sizeof (struct info_hash_entry
)))
469 bfd_release (abfd
, hash_table
);
476 /* Insert an info entry into an info hash table. We do not check of
477 duplicate entries. Also, the caller need to guarantee that the
478 right type of info in inserted as info is passed as a void* pointer.
479 This function returns true if there is no error. */
482 insert_info_hash_table (struct info_hash_table
*hash_table
,
487 struct info_hash_entry
*entry
;
488 struct info_list_node
*node
;
490 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
,
495 node
= (struct info_list_node
*) bfd_hash_allocate (&hash_table
->base
,
501 node
->next
= entry
->head
;
507 /* Look up an info entry list from an info hash table. Return NULL
510 static struct info_list_node
*
511 lookup_info_hash_table (struct info_hash_table
*hash_table
, const char *key
)
513 struct info_hash_entry
*entry
;
515 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
, key
,
517 return entry
? entry
->head
: NULL
;
520 /* Read a section into its appropriate place in the dwarf2_debug
521 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
522 not NULL, use bfd_simple_get_relocated_section_contents to read the
523 section contents, otherwise use bfd_get_section_contents. Fail if
524 the located section does not contain at least OFFSET bytes. */
527 read_section (bfd
* abfd
,
528 const struct dwarf_debug_section
*sec
,
531 bfd_byte
** section_buffer
,
532 bfd_size_type
* section_size
)
534 const char *section_name
= sec
->uncompressed_name
;
535 bfd_byte
*contents
= *section_buffer
;
537 /* The section may have already been read. */
538 if (contents
== NULL
)
544 msec
= bfd_get_section_by_name (abfd
, section_name
);
547 section_name
= sec
->compressed_name
;
548 msec
= bfd_get_section_by_name (abfd
, section_name
);
552 _bfd_error_handler (_("DWARF error: can't find %s section."),
553 sec
->uncompressed_name
);
554 bfd_set_error (bfd_error_bad_value
);
558 amt
= bfd_get_section_limit_octets (abfd
, msec
);
559 filesize
= bfd_get_file_size (abfd
);
563 _bfd_error_handler (_("DWARF error: section %s is larger than its filesize! (0x%lx vs 0x%lx)"),
564 section_name
, (long) amt
, (long) filesize
);
565 bfd_set_error (bfd_error_bad_value
);
569 /* Paranoia - alloc one extra so that we can make sure a string
570 section is NUL terminated. */
574 /* Paranoia - this should never happen. */
575 bfd_set_error (bfd_error_no_memory
);
578 contents
= (bfd_byte
*) bfd_malloc (amt
);
579 if (contents
== NULL
)
582 ? !bfd_simple_get_relocated_section_contents (abfd
, msec
, contents
,
584 : !bfd_get_section_contents (abfd
, msec
, contents
, 0, *section_size
))
589 contents
[*section_size
] = 0;
590 *section_buffer
= contents
;
593 /* It is possible to get a bad value for the offset into the section
594 that the client wants. Validate it here to avoid trouble later. */
595 if (offset
!= 0 && offset
>= *section_size
)
597 /* xgettext: c-format */
598 _bfd_error_handler (_("DWARF error: offset (%" PRIu64
")"
599 " greater than or equal to %s size (%" PRIu64
")"),
600 (uint64_t) offset
, section_name
,
601 (uint64_t) *section_size
);
602 bfd_set_error (bfd_error_bad_value
);
609 /* Read dwarf information from a buffer. */
611 static inline uint64_t
612 read_n_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
, int n
)
614 bfd_byte
*buf
= *ptr
;
621 return bfd_get (n
* 8, abfd
, buf
);
625 read_1_byte (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
627 return read_n_bytes (abfd
, ptr
, end
, 1);
631 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
**ptr
, bfd_byte
*end
)
633 bfd_byte
*buf
= *ptr
;
640 return bfd_get_signed_8 (abfd
, buf
);
644 read_2_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
646 return read_n_bytes (abfd
, ptr
, end
, 2);
650 read_3_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
652 unsigned int val
= read_1_byte (abfd
, ptr
, end
);
654 val
|= read_1_byte (abfd
, ptr
, end
);
656 val
|= read_1_byte (abfd
, ptr
, end
);
657 if (bfd_little_endian (abfd
))
658 val
= (((val
>> 16) & 0xff)
660 | ((val
& 0xff) << 16));
665 read_4_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
667 return read_n_bytes (abfd
, ptr
, end
, 4);
671 read_8_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
673 return read_n_bytes (abfd
, ptr
, end
, 8);
676 static struct dwarf_block
*
677 read_blk (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
, size_t size
)
679 bfd_byte
*buf
= *ptr
;
680 struct dwarf_block
*block
;
682 block
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (*block
));
686 if (size
> (size_t) (end
- buf
))
701 /* Scans a NUL terminated string starting at *PTR, returning a pointer to it.
702 Bytes at or beyond BUF_END will not be read. Returns NULL if the
703 terminator is not found or if the string is empty. *PTR is
704 incremented over the bytes scanned, including the terminator. */
707 read_string (bfd_byte
**ptr
,
710 bfd_byte
*buf
= *ptr
;
713 while (buf
< buf_end
)
726 /* Reads an offset from *PTR and then locates the string at this offset
727 inside the debug string section. Returns a pointer to the string.
728 Increments *PTR by the number of bytes read for the offset. This
729 value is set even if the function fails. Bytes at or beyond
730 BUF_END will not be read. Returns NULL if there was a problem, or
731 if the string is empty. Does not check for NUL termination of the
735 read_indirect_string (struct comp_unit
*unit
,
740 struct dwarf2_debug
*stash
= unit
->stash
;
741 struct dwarf2_debug_file
*file
= unit
->file
;
744 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
750 if (unit
->offset_size
== 4)
751 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
753 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
755 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
757 &file
->dwarf_str_buffer
, &file
->dwarf_str_size
))
760 str
= (char *) file
->dwarf_str_buffer
+ offset
;
766 /* Like read_indirect_string but from .debug_line_str section. */
769 read_indirect_line_string (struct comp_unit
*unit
,
774 struct dwarf2_debug
*stash
= unit
->stash
;
775 struct dwarf2_debug_file
*file
= unit
->file
;
778 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
784 if (unit
->offset_size
== 4)
785 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
787 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
789 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_line_str
],
791 &file
->dwarf_line_str_buffer
,
792 &file
->dwarf_line_str_size
))
795 str
= (char *) file
->dwarf_line_str_buffer
+ offset
;
801 /* Like read_indirect_string but uses a .debug_str located in
802 an alternate file pointed to by the .gnu_debugaltlink section.
803 Used to impement DW_FORM_GNU_strp_alt. */
806 read_alt_indirect_string (struct comp_unit
*unit
,
811 struct dwarf2_debug
*stash
= unit
->stash
;
814 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
820 if (unit
->offset_size
== 4)
821 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
823 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
825 if (stash
->alt
.bfd_ptr
== NULL
)
828 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
830 if (debug_filename
== NULL
)
833 debug_bfd
= bfd_openr (debug_filename
, NULL
);
834 free (debug_filename
);
835 if (debug_bfd
== NULL
)
836 /* FIXME: Should we report our failure to follow the debuglink ? */
839 if (!bfd_check_format (debug_bfd
, bfd_object
))
841 bfd_close (debug_bfd
);
844 stash
->alt
.bfd_ptr
= debug_bfd
;
847 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
848 stash
->debug_sections
+ debug_str_alt
,
849 stash
->alt
.syms
, offset
,
850 &stash
->alt
.dwarf_str_buffer
,
851 &stash
->alt
.dwarf_str_size
))
854 str
= (char *) stash
->alt
.dwarf_str_buffer
+ offset
;
861 /* Resolve an alternate reference from UNIT at OFFSET.
862 Returns a pointer into the loaded alternate CU upon success
863 or NULL upon failure. */
866 read_alt_indirect_ref (struct comp_unit
* unit
,
869 struct dwarf2_debug
*stash
= unit
->stash
;
871 if (stash
->alt
.bfd_ptr
== NULL
)
874 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
876 if (debug_filename
== NULL
)
879 debug_bfd
= bfd_openr (debug_filename
, NULL
);
880 free (debug_filename
);
881 if (debug_bfd
== NULL
)
882 /* FIXME: Should we report our failure to follow the debuglink ? */
885 if (!bfd_check_format (debug_bfd
, bfd_object
))
887 bfd_close (debug_bfd
);
890 stash
->alt
.bfd_ptr
= debug_bfd
;
893 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
894 stash
->debug_sections
+ debug_info_alt
,
895 stash
->alt
.syms
, offset
,
896 &stash
->alt
.dwarf_info_buffer
,
897 &stash
->alt
.dwarf_info_size
))
900 return stash
->alt
.dwarf_info_buffer
+ offset
;
904 read_address (struct comp_unit
*unit
, bfd_byte
**ptr
, bfd_byte
*buf_end
)
906 bfd_byte
*buf
= *ptr
;
909 if (bfd_get_flavour (unit
->abfd
) == bfd_target_elf_flavour
)
910 signed_vma
= get_elf_backend_data (unit
->abfd
)->sign_extend_vma
;
912 if (unit
->addr_size
> (size_t) (buf_end
- buf
)
919 *ptr
= buf
+ unit
->addr_size
;
922 switch (unit
->addr_size
)
925 return bfd_get_signed_64 (unit
->abfd
, buf
);
927 return bfd_get_signed_32 (unit
->abfd
, buf
);
929 return bfd_get_signed_16 (unit
->abfd
, buf
);
936 switch (unit
->addr_size
)
939 return bfd_get_64 (unit
->abfd
, buf
);
941 return bfd_get_32 (unit
->abfd
, buf
);
943 return bfd_get_16 (unit
->abfd
, buf
);
950 /* Lookup an abbrev_info structure in the abbrev hash table. */
952 static struct abbrev_info
*
953 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
955 unsigned int hash_number
;
956 struct abbrev_info
*abbrev
;
958 hash_number
= number
% ABBREV_HASH_SIZE
;
959 abbrev
= abbrevs
[hash_number
];
963 if (abbrev
->number
== number
)
966 abbrev
= abbrev
->next
;
972 /* We keep a hash table to map .debug_abbrev section offsets to the
973 array of abbrevs, so that compilation units using the same set of
974 abbrevs do not waste memory. */
976 struct abbrev_offset_entry
979 struct abbrev_info
**abbrevs
;
983 hash_abbrev (const void *p
)
985 const struct abbrev_offset_entry
*ent
= p
;
986 return htab_hash_pointer ((void *) ent
->offset
);
990 eq_abbrev (const void *pa
, const void *pb
)
992 const struct abbrev_offset_entry
*a
= pa
;
993 const struct abbrev_offset_entry
*b
= pb
;
994 return a
->offset
== b
->offset
;
1000 struct abbrev_offset_entry
*ent
= p
;
1001 struct abbrev_info
**abbrevs
= ent
->abbrevs
;
1004 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1006 struct abbrev_info
*abbrev
= abbrevs
[i
];
1010 free (abbrev
->attrs
);
1011 abbrev
= abbrev
->next
;
1017 /* In DWARF version 2, the description of the debugging information is
1018 stored in a separate .debug_abbrev section. Before we read any
1019 dies from a section we read in all abbreviations and install them
1022 static struct abbrev_info
**
1023 read_abbrevs (bfd
*abfd
, bfd_uint64_t offset
, struct dwarf2_debug
*stash
,
1024 struct dwarf2_debug_file
*file
)
1026 struct abbrev_info
**abbrevs
;
1027 bfd_byte
*abbrev_ptr
;
1028 bfd_byte
*abbrev_end
;
1029 struct abbrev_info
*cur_abbrev
;
1030 unsigned int abbrev_number
, abbrev_name
;
1031 unsigned int abbrev_form
, hash_number
;
1034 struct abbrev_offset_entry ent
= { offset
, NULL
};
1036 if (ent
.offset
!= offset
)
1039 slot
= htab_find_slot (file
->abbrev_offsets
, &ent
, INSERT
);
1043 return ((struct abbrev_offset_entry
*) (*slot
))->abbrevs
;
1045 if (! read_section (abfd
, &stash
->debug_sections
[debug_abbrev
],
1047 &file
->dwarf_abbrev_buffer
,
1048 &file
->dwarf_abbrev_size
))
1051 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
1052 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
1053 if (abbrevs
== NULL
)
1056 abbrev_ptr
= file
->dwarf_abbrev_buffer
+ offset
;
1057 abbrev_end
= file
->dwarf_abbrev_buffer
+ file
->dwarf_abbrev_size
;
1058 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1061 /* Loop until we reach an abbrev number of 0. */
1062 while (abbrev_number
)
1064 amt
= sizeof (struct abbrev_info
);
1065 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
1066 if (cur_abbrev
== NULL
)
1069 /* Read in abbrev header. */
1070 cur_abbrev
->number
= abbrev_number
;
1071 cur_abbrev
->tag
= (enum dwarf_tag
)
1072 _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1074 cur_abbrev
->has_children
= read_1_byte (abfd
, &abbrev_ptr
, abbrev_end
);
1076 /* Now read in declarations. */
1079 /* Initialize it just to avoid a GCC false warning. */
1080 bfd_vma implicit_const
= -1;
1082 abbrev_name
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1084 abbrev_form
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1086 if (abbrev_form
== DW_FORM_implicit_const
)
1087 implicit_const
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1089 if (abbrev_name
== 0)
1092 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
1094 struct attr_abbrev
*tmp
;
1096 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
1097 amt
*= sizeof (struct attr_abbrev
);
1098 tmp
= (struct attr_abbrev
*) bfd_realloc (cur_abbrev
->attrs
, amt
);
1101 cur_abbrev
->attrs
= tmp
;
1104 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
1105 = (enum dwarf_attribute
) abbrev_name
;
1106 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].form
1107 = (enum dwarf_form
) abbrev_form
;
1108 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].implicit_const
1110 ++cur_abbrev
->num_attrs
;
1113 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
1114 cur_abbrev
->next
= abbrevs
[hash_number
];
1115 abbrevs
[hash_number
] = cur_abbrev
;
1117 /* Get next abbreviation.
1118 Under Irix6 the abbreviations for a compilation unit are not
1119 always properly terminated with an abbrev number of 0.
1120 Exit loop if we encounter an abbreviation which we have
1121 already read (which means we are about to read the abbreviations
1122 for the next compile unit) or if the end of the abbreviation
1123 table is reached. */
1124 if ((size_t) (abbrev_ptr
- file
->dwarf_abbrev_buffer
)
1125 >= file
->dwarf_abbrev_size
)
1127 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1129 if (lookup_abbrev (abbrev_number
, abbrevs
) != NULL
)
1133 *slot
= bfd_malloc (sizeof ent
);
1136 ent
.abbrevs
= abbrevs
;
1137 memcpy (*slot
, &ent
, sizeof ent
);
1141 if (abbrevs
!= NULL
)
1145 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1147 struct abbrev_info
*abbrev
= abbrevs
[i
];
1151 free (abbrev
->attrs
);
1152 abbrev
= abbrev
->next
;
1160 /* Returns true if the form is one which has a string value. */
1163 is_str_attr (enum dwarf_form form
)
1165 return (form
== DW_FORM_string
1166 || form
== DW_FORM_strp
1167 || form
== DW_FORM_strx
1168 || form
== DW_FORM_strx1
1169 || form
== DW_FORM_strx2
1170 || form
== DW_FORM_strx3
1171 || form
== DW_FORM_strx4
1172 || form
== DW_FORM_line_strp
1173 || form
== DW_FORM_GNU_strp_alt
);
1177 read_indexed_string (bfd_uint64_t idx ATTRIBUTE_UNUSED
,
1178 struct comp_unit
* unit ATTRIBUTE_UNUSED
)
1180 /* FIXME: Add support for indexed strings. */
1181 return "<indexed strings not yet supported>";
1184 /* Read and fill in the value of attribute ATTR as described by FORM.
1185 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1186 Returns an updated INFO_PTR taking into account the amount of data read. */
1189 read_attribute_value (struct attribute
* attr
,
1191 bfd_vma implicit_const
,
1192 struct comp_unit
* unit
,
1193 bfd_byte
* info_ptr
,
1194 bfd_byte
* info_ptr_end
)
1196 bfd
*abfd
= unit
->abfd
;
1199 if (info_ptr
>= info_ptr_end
&& form
!= DW_FORM_flag_present
)
1201 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1202 bfd_set_error (bfd_error_bad_value
);
1206 attr
->form
= (enum dwarf_form
) form
;
1210 case DW_FORM_flag_present
:
1213 case DW_FORM_ref_addr
:
1214 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1216 if (unit
->version
>= 3)
1218 if (unit
->offset_size
== 4)
1219 attr
->u
.val
= read_4_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1221 attr
->u
.val
= read_8_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1226 attr
->u
.val
= read_address (unit
, &info_ptr
, info_ptr_end
);
1228 case DW_FORM_GNU_ref_alt
:
1229 case DW_FORM_sec_offset
:
1230 if (unit
->offset_size
== 4)
1231 attr
->u
.val
= read_4_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1233 attr
->u
.val
= read_8_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1235 case DW_FORM_block2
:
1236 amt
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1237 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1238 if (attr
->u
.blk
== NULL
)
1241 case DW_FORM_block4
:
1242 amt
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1243 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1244 if (attr
->u
.blk
== NULL
)
1250 case DW_FORM_addrx1
:
1251 attr
->u
.val
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1255 attr
->u
.val
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1257 case DW_FORM_addrx3
:
1258 attr
->u
.val
= read_3_bytes (abfd
, &info_ptr
, info_ptr_end
);
1262 case DW_FORM_addrx4
:
1263 attr
->u
.val
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1267 case DW_FORM_ref_sig8
:
1268 attr
->u
.val
= read_8_bytes (abfd
, &info_ptr
, info_ptr_end
);
1270 case DW_FORM_string
:
1271 attr
->u
.str
= read_string (&info_ptr
, info_ptr_end
);
1274 attr
->u
.str
= read_indirect_string (unit
, &info_ptr
, info_ptr_end
);
1276 case DW_FORM_line_strp
:
1277 attr
->u
.str
= read_indirect_line_string (unit
, &info_ptr
, info_ptr_end
);
1279 case DW_FORM_GNU_strp_alt
:
1280 attr
->u
.str
= read_alt_indirect_string (unit
, &info_ptr
, info_ptr_end
);
1283 attr
->u
.val
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1284 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1287 attr
->u
.val
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1288 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1291 attr
->u
.val
= read_3_bytes (abfd
, &info_ptr
, info_ptr_end
);
1292 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1295 attr
->u
.val
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1296 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1299 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1300 false, info_ptr_end
);
1301 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1303 case DW_FORM_exprloc
:
1305 amt
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1306 false, info_ptr_end
);
1307 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1308 if (attr
->u
.blk
== NULL
)
1311 case DW_FORM_block1
:
1312 amt
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1313 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1314 if (attr
->u
.blk
== NULL
)
1318 attr
->u
.sval
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1319 true, info_ptr_end
);
1321 case DW_FORM_ref_udata
:
1324 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1325 false, info_ptr_end
);
1327 case DW_FORM_indirect
:
1328 form
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1329 false, info_ptr_end
);
1330 if (form
== DW_FORM_implicit_const
)
1331 implicit_const
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1332 true, info_ptr_end
);
1333 info_ptr
= read_attribute_value (attr
, form
, implicit_const
, unit
,
1334 info_ptr
, info_ptr_end
);
1336 case DW_FORM_implicit_const
:
1337 attr
->form
= DW_FORM_sdata
;
1338 attr
->u
.sval
= implicit_const
;
1340 case DW_FORM_data16
:
1341 /* This is really a "constant", but there is no way to store that
1342 so pretend it is a 16 byte block instead. */
1343 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, 16);
1344 if (attr
->u
.blk
== NULL
)
1349 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1351 bfd_set_error (bfd_error_bad_value
);
1357 /* Read an attribute described by an abbreviated attribute. */
1360 read_attribute (struct attribute
* attr
,
1361 struct attr_abbrev
* abbrev
,
1362 struct comp_unit
* unit
,
1363 bfd_byte
* info_ptr
,
1364 bfd_byte
* info_ptr_end
)
1366 attr
->name
= abbrev
->name
;
1367 info_ptr
= read_attribute_value (attr
, abbrev
->form
, abbrev
->implicit_const
,
1368 unit
, info_ptr
, info_ptr_end
);
1372 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1376 non_mangled (int lang
)
1386 case DW_LANG_Cobol74
:
1387 case DW_LANG_Cobol85
:
1388 case DW_LANG_Fortran77
:
1389 case DW_LANG_Pascal83
:
1399 /* Source line information table routines. */
1401 #define FILE_ALLOC_CHUNK 5
1402 #define DIR_ALLOC_CHUNK 5
1406 struct line_info
* prev_line
;
1410 unsigned int column
;
1411 unsigned int discriminator
;
1412 unsigned char op_index
;
1413 unsigned char end_sequence
; /* End of (sequential) code sequence. */
1424 struct line_sequence
1427 struct line_sequence
* prev_sequence
;
1428 struct line_info
* last_line
; /* Largest VMA. */
1429 struct line_info
** line_info_lookup
;
1430 bfd_size_type num_lines
;
1433 struct line_info_table
1436 unsigned int num_files
;
1437 unsigned int num_dirs
;
1438 unsigned int num_sequences
;
1441 struct fileinfo
* files
;
1442 struct line_sequence
* sequences
;
1443 struct line_info
* lcl_head
; /* Local head; used in 'add_line_info'. */
1446 /* Remember some information about each function. If the function is
1447 inlined (DW_TAG_inlined_subroutine) it may have two additional
1448 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1449 source code location where this function was inlined. */
1453 /* Pointer to previous function in list of all functions. */
1454 struct funcinfo
* prev_func
;
1455 /* Pointer to function one scope higher. */
1456 struct funcinfo
* caller_func
;
1457 /* Source location file name where caller_func inlines this func. */
1459 /* Source location file name. */
1461 /* Source location line number where caller_func inlines this func. */
1463 /* Source location line number. */
1468 struct arange arange
;
1469 /* Where the symbol is defined. */
1471 /* The offset of the funcinfo from the start of the unit. */
1472 bfd_uint64_t unit_offset
;
1475 struct lookup_funcinfo
1477 /* Function information corresponding to this lookup table entry. */
1478 struct funcinfo
* funcinfo
;
1480 /* The lowest address for this specific function. */
1483 /* The highest address of this function before the lookup table is sorted.
1484 The highest address of all prior functions after the lookup table is
1485 sorted, which is used for binary search. */
1487 /* Index of this function, used to ensure qsort is stable. */
1493 /* Pointer to previous variable in list of all variables. */
1494 struct varinfo
*prev_var
;
1495 /* The offset of the varinfo from the start of the unit. */
1496 bfd_uint64_t unit_offset
;
1497 /* Source location file name. */
1499 /* Source location line number. */
1501 /* The type of this variable. */
1503 /* The name of the variable, if it has one. */
1505 /* The address of the variable. */
1507 /* Where the symbol is defined. */
1509 /* Is this a stack variable? */
1513 /* Return TRUE if NEW_LINE should sort after LINE. */
1516 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
1518 return (new_line
->address
> line
->address
1519 || (new_line
->address
== line
->address
1520 && new_line
->op_index
> line
->op_index
));
1524 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1525 that the list is sorted. Note that the line_info list is sorted from
1526 highest to lowest VMA (with possible duplicates); that is,
1527 line_info->prev_line always accesses an equal or smaller VMA. */
1530 add_line_info (struct line_info_table
*table
,
1532 unsigned char op_index
,
1535 unsigned int column
,
1536 unsigned int discriminator
,
1539 size_t amt
= sizeof (struct line_info
);
1540 struct line_sequence
* seq
= table
->sequences
;
1541 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
1546 /* Set member data of 'info'. */
1547 info
->prev_line
= NULL
;
1548 info
->address
= address
;
1549 info
->op_index
= op_index
;
1551 info
->column
= column
;
1552 info
->discriminator
= discriminator
;
1553 info
->end_sequence
= end_sequence
;
1555 if (filename
&& filename
[0])
1557 info
->filename
= (char *) bfd_alloc (table
->abfd
, strlen (filename
) + 1);
1558 if (info
->filename
== NULL
)
1560 strcpy (info
->filename
, filename
);
1563 info
->filename
= NULL
;
1565 /* Find the correct location for 'info'. Normally we will receive
1566 new line_info data 1) in order and 2) with increasing VMAs.
1567 However some compilers break the rules (cf. decode_line_info) and
1568 so we include some heuristics for quickly finding the correct
1569 location for 'info'. In particular, these heuristics optimize for
1570 the common case in which the VMA sequence that we receive is a
1571 list of locally sorted VMAs such as
1572 p...z a...j (where a < j < p < z)
1574 Note: table->lcl_head is used to head an *actual* or *possible*
1575 sub-sequence within the list (such as a...j) that is not directly
1576 headed by table->last_line
1578 Note: we may receive duplicate entries from 'decode_line_info'. */
1581 && seq
->last_line
->address
== address
1582 && seq
->last_line
->op_index
== op_index
1583 && seq
->last_line
->end_sequence
== end_sequence
)
1585 /* We only keep the last entry with the same address and end
1586 sequence. See PR ld/4986. */
1587 if (table
->lcl_head
== seq
->last_line
)
1588 table
->lcl_head
= info
;
1589 info
->prev_line
= seq
->last_line
->prev_line
;
1590 seq
->last_line
= info
;
1592 else if (!seq
|| seq
->last_line
->end_sequence
)
1594 /* Start a new line sequence. */
1595 amt
= sizeof (struct line_sequence
);
1596 seq
= (struct line_sequence
*) bfd_malloc (amt
);
1599 seq
->low_pc
= address
;
1600 seq
->prev_sequence
= table
->sequences
;
1601 seq
->last_line
= info
;
1602 table
->lcl_head
= info
;
1603 table
->sequences
= seq
;
1604 table
->num_sequences
++;
1606 else if (info
->end_sequence
1607 || new_line_sorts_after (info
, seq
->last_line
))
1609 /* Normal case: add 'info' to the beginning of the current sequence. */
1610 info
->prev_line
= seq
->last_line
;
1611 seq
->last_line
= info
;
1613 /* lcl_head: initialize to head a *possible* sequence at the end. */
1614 if (!table
->lcl_head
)
1615 table
->lcl_head
= info
;
1617 else if (!new_line_sorts_after (info
, table
->lcl_head
)
1618 && (!table
->lcl_head
->prev_line
1619 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
1621 /* Abnormal but easy: lcl_head is the head of 'info'. */
1622 info
->prev_line
= table
->lcl_head
->prev_line
;
1623 table
->lcl_head
->prev_line
= info
;
1627 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1628 are valid heads for 'info'. Reset 'lcl_head'. */
1629 struct line_info
* li2
= seq
->last_line
; /* Always non-NULL. */
1630 struct line_info
* li1
= li2
->prev_line
;
1634 if (!new_line_sorts_after (info
, li2
)
1635 && new_line_sorts_after (info
, li1
))
1638 li2
= li1
; /* always non-NULL */
1639 li1
= li1
->prev_line
;
1641 table
->lcl_head
= li2
;
1642 info
->prev_line
= table
->lcl_head
->prev_line
;
1643 table
->lcl_head
->prev_line
= info
;
1644 if (address
< seq
->low_pc
)
1645 seq
->low_pc
= address
;
1650 /* Extract a fully qualified filename from a line info table.
1651 The returned string has been malloc'ed and it is the caller's
1652 responsibility to free it. */
1655 concat_filename (struct line_info_table
*table
, unsigned int file
)
1659 if (table
== NULL
|| file
- 1 >= table
->num_files
)
1661 /* FILE == 0 means unknown. */
1664 (_("DWARF error: mangled line number section (bad file number)"));
1665 return strdup ("<unknown>");
1668 filename
= table
->files
[file
- 1].name
;
1669 if (filename
== NULL
)
1670 return strdup ("<unknown>");
1672 if (!IS_ABSOLUTE_PATH (filename
))
1674 char *dir_name
= NULL
;
1675 char *subdir_name
= NULL
;
1679 if (table
->files
[file
- 1].dir
1680 /* PR 17512: file: 0317e960. */
1681 && table
->files
[file
- 1].dir
<= table
->num_dirs
1682 /* PR 17512: file: 7f3d2e4b. */
1683 && table
->dirs
!= NULL
)
1684 subdir_name
= table
->dirs
[table
->files
[file
- 1].dir
- 1];
1686 if (!subdir_name
|| !IS_ABSOLUTE_PATH (subdir_name
))
1687 dir_name
= table
->comp_dir
;
1691 dir_name
= subdir_name
;
1696 return strdup (filename
);
1698 len
= strlen (dir_name
) + strlen (filename
) + 2;
1702 len
+= strlen (subdir_name
) + 1;
1703 name
= (char *) bfd_malloc (len
);
1705 sprintf (name
, "%s/%s/%s", dir_name
, subdir_name
, filename
);
1709 name
= (char *) bfd_malloc (len
);
1711 sprintf (name
, "%s/%s", dir_name
, filename
);
1717 return strdup (filename
);
1721 arange_add (const struct comp_unit
*unit
, struct arange
*first_arange
,
1722 bfd_vma low_pc
, bfd_vma high_pc
)
1724 struct arange
*arange
;
1726 /* Ignore empty ranges. */
1727 if (low_pc
== high_pc
)
1730 /* If the first arange is empty, use it. */
1731 if (first_arange
->high
== 0)
1733 first_arange
->low
= low_pc
;
1734 first_arange
->high
= high_pc
;
1738 /* Next see if we can cheaply extend an existing range. */
1739 arange
= first_arange
;
1742 if (low_pc
== arange
->high
)
1744 arange
->high
= high_pc
;
1747 if (high_pc
== arange
->low
)
1749 arange
->low
= low_pc
;
1752 arange
= arange
->next
;
1756 /* Need to allocate a new arange and insert it into the arange list.
1757 Order isn't significant, so just insert after the first arange. */
1758 arange
= (struct arange
*) bfd_alloc (unit
->abfd
, sizeof (*arange
));
1761 arange
->low
= low_pc
;
1762 arange
->high
= high_pc
;
1763 arange
->next
= first_arange
->next
;
1764 first_arange
->next
= arange
;
1768 /* Compare function for line sequences. */
1771 compare_sequences (const void* a
, const void* b
)
1773 const struct line_sequence
* seq1
= a
;
1774 const struct line_sequence
* seq2
= b
;
1776 /* Sort by low_pc as the primary key. */
1777 if (seq1
->low_pc
< seq2
->low_pc
)
1779 if (seq1
->low_pc
> seq2
->low_pc
)
1782 /* If low_pc values are equal, sort in reverse order of
1783 high_pc, so that the largest region comes first. */
1784 if (seq1
->last_line
->address
< seq2
->last_line
->address
)
1786 if (seq1
->last_line
->address
> seq2
->last_line
->address
)
1789 if (seq1
->last_line
->op_index
< seq2
->last_line
->op_index
)
1791 if (seq1
->last_line
->op_index
> seq2
->last_line
->op_index
)
1794 /* num_lines is initially an index, to make the sort stable. */
1795 if (seq1
->num_lines
< seq2
->num_lines
)
1797 if (seq1
->num_lines
> seq2
->num_lines
)
1802 /* Construct the line information table for quick lookup. */
1805 build_line_info_table (struct line_info_table
* table
,
1806 struct line_sequence
* seq
)
1809 struct line_info
**line_info_lookup
;
1810 struct line_info
*each_line
;
1811 unsigned int num_lines
;
1812 unsigned int line_index
;
1814 if (seq
->line_info_lookup
!= NULL
)
1817 /* Count the number of line information entries. We could do this while
1818 scanning the debug information, but some entries may be added via
1819 lcl_head without having a sequence handy to increment the number of
1822 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
1825 seq
->num_lines
= num_lines
;
1829 /* Allocate space for the line information lookup table. */
1830 amt
= sizeof (struct line_info
*) * num_lines
;
1831 line_info_lookup
= (struct line_info
**) bfd_alloc (table
->abfd
, amt
);
1832 seq
->line_info_lookup
= line_info_lookup
;
1833 if (line_info_lookup
== NULL
)
1836 /* Create the line information lookup table. */
1837 line_index
= num_lines
;
1838 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
1839 line_info_lookup
[--line_index
] = each_line
;
1841 BFD_ASSERT (line_index
== 0);
1845 /* Sort the line sequences for quick lookup. */
1848 sort_line_sequences (struct line_info_table
* table
)
1851 struct line_sequence
*sequences
;
1852 struct line_sequence
*seq
;
1854 unsigned int num_sequences
= table
->num_sequences
;
1855 bfd_vma last_high_pc
;
1857 if (num_sequences
== 0)
1860 /* Allocate space for an array of sequences. */
1861 amt
= sizeof (struct line_sequence
) * num_sequences
;
1862 sequences
= (struct line_sequence
*) bfd_alloc (table
->abfd
, amt
);
1863 if (sequences
== NULL
)
1866 /* Copy the linked list into the array, freeing the original nodes. */
1867 seq
= table
->sequences
;
1868 for (n
= 0; n
< num_sequences
; n
++)
1870 struct line_sequence
* last_seq
= seq
;
1873 sequences
[n
].low_pc
= seq
->low_pc
;
1874 sequences
[n
].prev_sequence
= NULL
;
1875 sequences
[n
].last_line
= seq
->last_line
;
1876 sequences
[n
].line_info_lookup
= NULL
;
1877 sequences
[n
].num_lines
= n
;
1878 seq
= seq
->prev_sequence
;
1881 BFD_ASSERT (seq
== NULL
);
1883 qsort (sequences
, n
, sizeof (struct line_sequence
), compare_sequences
);
1885 /* Make the list binary-searchable by trimming overlapping entries
1886 and removing nested entries. */
1888 last_high_pc
= sequences
[0].last_line
->address
;
1889 for (n
= 1; n
< table
->num_sequences
; n
++)
1891 if (sequences
[n
].low_pc
< last_high_pc
)
1893 if (sequences
[n
].last_line
->address
<= last_high_pc
)
1894 /* Skip nested entries. */
1897 /* Trim overlapping entries. */
1898 sequences
[n
].low_pc
= last_high_pc
;
1900 last_high_pc
= sequences
[n
].last_line
->address
;
1901 if (n
> num_sequences
)
1903 /* Close up the gap. */
1904 sequences
[num_sequences
].low_pc
= sequences
[n
].low_pc
;
1905 sequences
[num_sequences
].last_line
= sequences
[n
].last_line
;
1910 table
->sequences
= sequences
;
1911 table
->num_sequences
= num_sequences
;
1915 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
1918 line_info_add_include_dir (struct line_info_table
*table
, char *cur_dir
)
1920 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1925 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1926 amt
*= sizeof (char *);
1928 tmp
= (char **) bfd_realloc (table
->dirs
, amt
);
1934 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1939 line_info_add_include_dir_stub (struct line_info_table
*table
, char *cur_dir
,
1940 unsigned int dir ATTRIBUTE_UNUSED
,
1941 unsigned int xtime ATTRIBUTE_UNUSED
,
1942 unsigned int size ATTRIBUTE_UNUSED
)
1944 return line_info_add_include_dir (table
, cur_dir
);
1947 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
1950 line_info_add_file_name (struct line_info_table
*table
, char *cur_file
,
1951 unsigned int dir
, unsigned int xtime
,
1954 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1956 struct fileinfo
*tmp
;
1959 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1960 amt
*= sizeof (struct fileinfo
);
1962 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1968 table
->files
[table
->num_files
].name
= cur_file
;
1969 table
->files
[table
->num_files
].dir
= dir
;
1970 table
->files
[table
->num_files
].time
= xtime
;
1971 table
->files
[table
->num_files
].size
= size
;
1976 /* Read directory or file name entry format, starting with byte of
1977 format count entries, ULEB128 pairs of entry formats, ULEB128 of
1978 entries count and the entries themselves in the described entry
1982 read_formatted_entries (struct comp_unit
*unit
, bfd_byte
**bufp
,
1983 bfd_byte
*buf_end
, struct line_info_table
*table
,
1984 bool (*callback
) (struct line_info_table
*table
,
1990 bfd
*abfd
= unit
->abfd
;
1991 bfd_byte format_count
, formati
;
1992 bfd_vma data_count
, datai
;
1993 bfd_byte
*buf
= *bufp
;
1994 bfd_byte
*format_header_data
;
1996 format_count
= read_1_byte (abfd
, &buf
, buf_end
);
1997 format_header_data
= buf
;
1998 for (formati
= 0; formati
< format_count
; formati
++)
2000 _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2001 _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2004 data_count
= _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2005 if (format_count
== 0 && data_count
!= 0)
2007 _bfd_error_handler (_("DWARF error: zero format count"));
2008 bfd_set_error (bfd_error_bad_value
);
2012 /* PR 22210. Paranoia check. Don't bother running the loop
2013 if we know that we are going to run out of buffer. */
2014 if (data_count
> (bfd_vma
) (buf_end
- buf
))
2017 (_("DWARF error: data count (%" PRIx64
") larger than buffer size"),
2018 (uint64_t) data_count
);
2019 bfd_set_error (bfd_error_bad_value
);
2023 for (datai
= 0; datai
< data_count
; datai
++)
2025 bfd_byte
*format
= format_header_data
;
2028 memset (&fe
, 0, sizeof fe
);
2029 for (formati
= 0; formati
< format_count
; formati
++)
2031 bfd_vma content_type
, form
;
2033 char **stringp
= &string_trash
;
2034 unsigned int uint_trash
, *uintp
= &uint_trash
;
2035 struct attribute attr
;
2037 content_type
= _bfd_safe_read_leb128 (abfd
, &format
, false, buf_end
);
2038 switch (content_type
)
2043 case DW_LNCT_directory_index
:
2046 case DW_LNCT_timestamp
:
2056 (_("DWARF error: unknown format content type %" PRIu64
),
2057 (uint64_t) content_type
);
2058 bfd_set_error (bfd_error_bad_value
);
2062 form
= _bfd_safe_read_leb128 (abfd
, &format
, false, buf_end
);
2063 buf
= read_attribute_value (&attr
, form
, 0, unit
, buf
, buf_end
);
2068 case DW_FORM_string
:
2069 case DW_FORM_line_strp
:
2070 *stringp
= attr
.u
.str
;
2078 *uintp
= attr
.u
.val
;
2081 case DW_FORM_data16
:
2082 /* MD5 data is in the attr.blk, but we are ignoring those. */
2087 /* Skip the first "zero entry", which is the compilation dir/file. */
2089 if (!callback (table
, fe
.name
, fe
.dir
, fe
.time
, fe
.size
))
2097 /* Decode the line number information for UNIT. */
2099 static struct line_info_table
*
2100 decode_line_info (struct comp_unit
*unit
)
2102 bfd
*abfd
= unit
->abfd
;
2103 struct dwarf2_debug
*stash
= unit
->stash
;
2104 struct dwarf2_debug_file
*file
= unit
->file
;
2105 struct line_info_table
* table
;
2108 struct line_head lh
;
2109 unsigned int i
, offset_size
;
2110 char *cur_file
, *cur_dir
;
2111 unsigned char op_code
, extended_op
, adj_opcode
;
2112 unsigned int exop_len
;
2115 if (unit
->line_offset
== 0 && file
->line_table
)
2116 return file
->line_table
;
2118 if (! read_section (abfd
, &stash
->debug_sections
[debug_line
],
2119 file
->syms
, unit
->line_offset
,
2120 &file
->dwarf_line_buffer
, &file
->dwarf_line_size
))
2123 if (file
->dwarf_line_size
< 16)
2126 (_("DWARF error: line info section is too small (%" PRId64
")"),
2127 (int64_t) file
->dwarf_line_size
);
2128 bfd_set_error (bfd_error_bad_value
);
2131 line_ptr
= file
->dwarf_line_buffer
+ unit
->line_offset
;
2132 line_end
= file
->dwarf_line_buffer
+ file
->dwarf_line_size
;
2134 /* Read in the prologue. */
2135 lh
.total_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2137 if (lh
.total_length
== 0xffffffff)
2139 lh
.total_length
= read_8_bytes (abfd
, &line_ptr
, line_end
);
2142 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
2144 /* Handle (non-standard) 64-bit DWARF2 formats. */
2145 lh
.total_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2149 if (lh
.total_length
> (size_t) (line_end
- line_ptr
))
2152 /* xgettext: c-format */
2153 (_("DWARF error: line info data is bigger (%#" PRIx64
")"
2154 " than the space remaining in the section (%#lx)"),
2155 (uint64_t) lh
.total_length
, (unsigned long) (line_end
- line_ptr
));
2156 bfd_set_error (bfd_error_bad_value
);
2160 line_end
= line_ptr
+ lh
.total_length
;
2162 lh
.version
= read_2_bytes (abfd
, &line_ptr
, line_end
);
2163 if (lh
.version
< 2 || lh
.version
> 5)
2166 (_("DWARF error: unhandled .debug_line version %d"), lh
.version
);
2167 bfd_set_error (bfd_error_bad_value
);
2171 if (line_ptr
+ offset_size
+ (lh
.version
>= 5 ? 8 : (lh
.version
>= 4 ? 6 : 5))
2175 (_("DWARF error: ran out of room reading prologue"));
2176 bfd_set_error (bfd_error_bad_value
);
2180 if (lh
.version
>= 5)
2182 unsigned int segment_selector_size
;
2184 /* Skip address size. */
2185 read_1_byte (abfd
, &line_ptr
, line_end
);
2187 segment_selector_size
= read_1_byte (abfd
, &line_ptr
, line_end
);
2188 if (segment_selector_size
!= 0)
2191 (_("DWARF error: line info unsupported segment selector size %u"),
2192 segment_selector_size
);
2193 bfd_set_error (bfd_error_bad_value
);
2198 if (offset_size
== 4)
2199 lh
.prologue_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2201 lh
.prologue_length
= read_8_bytes (abfd
, &line_ptr
, line_end
);
2203 lh
.minimum_instruction_length
= read_1_byte (abfd
, &line_ptr
, line_end
);
2205 if (lh
.version
>= 4)
2206 lh
.maximum_ops_per_insn
= read_1_byte (abfd
, &line_ptr
, line_end
);
2208 lh
.maximum_ops_per_insn
= 1;
2210 if (lh
.maximum_ops_per_insn
== 0)
2213 (_("DWARF error: invalid maximum operations per instruction"));
2214 bfd_set_error (bfd_error_bad_value
);
2218 lh
.default_is_stmt
= read_1_byte (abfd
, &line_ptr
, line_end
);
2219 lh
.line_base
= read_1_signed_byte (abfd
, &line_ptr
, line_end
);
2220 lh
.line_range
= read_1_byte (abfd
, &line_ptr
, line_end
);
2221 lh
.opcode_base
= read_1_byte (abfd
, &line_ptr
, line_end
);
2223 if (line_ptr
+ (lh
.opcode_base
- 1) >= line_end
)
2225 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2226 bfd_set_error (bfd_error_bad_value
);
2230 amt
= lh
.opcode_base
* sizeof (unsigned char);
2231 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
2233 lh
.standard_opcode_lengths
[0] = 1;
2235 for (i
= 1; i
< lh
.opcode_base
; ++i
)
2236 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, &line_ptr
, line_end
);
2238 amt
= sizeof (struct line_info_table
);
2239 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
2243 table
->comp_dir
= unit
->comp_dir
;
2245 table
->num_files
= 0;
2246 table
->files
= NULL
;
2248 table
->num_dirs
= 0;
2251 table
->num_sequences
= 0;
2252 table
->sequences
= NULL
;
2254 table
->lcl_head
= NULL
;
2256 if (lh
.version
>= 5)
2258 /* Read directory table. */
2259 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2260 line_info_add_include_dir_stub
))
2263 /* Read file name table. */
2264 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2265 line_info_add_file_name
))
2270 /* Read directory table. */
2271 while ((cur_dir
= read_string (&line_ptr
, line_end
)) != NULL
)
2273 if (!line_info_add_include_dir (table
, cur_dir
))
2277 /* Read file name table. */
2278 while ((cur_file
= read_string (&line_ptr
, line_end
)) != NULL
)
2280 unsigned int dir
, xtime
, size
;
2282 dir
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2283 xtime
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2284 size
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2286 if (!line_info_add_file_name (table
, cur_file
, dir
, xtime
, size
))
2291 /* Read the statement sequences until there's nothing left. */
2292 while (line_ptr
< line_end
)
2294 /* State machine registers. */
2295 bfd_vma address
= 0;
2296 unsigned char op_index
= 0;
2297 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
2298 unsigned int line
= 1;
2299 unsigned int column
= 0;
2300 unsigned int discriminator
= 0;
2301 int is_stmt
= lh
.default_is_stmt
;
2302 int end_sequence
= 0;
2303 unsigned int dir
, xtime
, size
;
2304 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2305 compilers generate address sequences that are wildly out of
2306 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2307 for ia64-Linux). Thus, to determine the low and high
2308 address, we must compare on every DW_LNS_copy, etc. */
2309 bfd_vma low_pc
= (bfd_vma
) -1;
2310 bfd_vma high_pc
= 0;
2312 /* Decode the table. */
2313 while (!end_sequence
&& line_ptr
< line_end
)
2315 op_code
= read_1_byte (abfd
, &line_ptr
, line_end
);
2317 if (op_code
>= lh
.opcode_base
)
2319 /* Special operand. */
2320 adj_opcode
= op_code
- lh
.opcode_base
;
2321 if (lh
.line_range
== 0)
2323 if (lh
.maximum_ops_per_insn
== 1)
2324 address
+= (adj_opcode
/ lh
.line_range
2325 * lh
.minimum_instruction_length
);
2328 address
+= ((op_index
+ adj_opcode
/ lh
.line_range
)
2329 / lh
.maximum_ops_per_insn
2330 * lh
.minimum_instruction_length
);
2331 op_index
= ((op_index
+ adj_opcode
/ lh
.line_range
)
2332 % lh
.maximum_ops_per_insn
);
2334 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
2335 /* Append row to matrix using current values. */
2336 if (!add_line_info (table
, address
, op_index
, filename
,
2337 line
, column
, discriminator
, 0))
2340 if (address
< low_pc
)
2342 if (address
> high_pc
)
2345 else switch (op_code
)
2347 case DW_LNS_extended_op
:
2348 exop_len
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2350 extended_op
= read_1_byte (abfd
, &line_ptr
, line_end
);
2352 switch (extended_op
)
2354 case DW_LNE_end_sequence
:
2356 if (!add_line_info (table
, address
, op_index
, filename
, line
,
2357 column
, discriminator
, end_sequence
))
2360 if (address
< low_pc
)
2362 if (address
> high_pc
)
2364 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
2367 case DW_LNE_set_address
:
2368 address
= read_address (unit
, &line_ptr
, line_end
);
2371 case DW_LNE_define_file
:
2372 cur_file
= read_string (&line_ptr
, line_end
);
2373 dir
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2375 xtime
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2377 size
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2379 if (!line_info_add_file_name (table
, cur_file
, dir
,
2383 case DW_LNE_set_discriminator
:
2385 _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2388 case DW_LNE_HP_source_file_correlation
:
2389 line_ptr
+= exop_len
- 1;
2393 (_("DWARF error: mangled line number section"));
2394 bfd_set_error (bfd_error_bad_value
);
2401 if (!add_line_info (table
, address
, op_index
,
2402 filename
, line
, column
, discriminator
, 0))
2405 if (address
< low_pc
)
2407 if (address
> high_pc
)
2410 case DW_LNS_advance_pc
:
2411 if (lh
.maximum_ops_per_insn
== 1)
2412 address
+= (lh
.minimum_instruction_length
2413 * _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2417 bfd_vma adjust
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2419 address
= ((op_index
+ adjust
) / lh
.maximum_ops_per_insn
2420 * lh
.minimum_instruction_length
);
2421 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2424 case DW_LNS_advance_line
:
2425 line
+= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2428 case DW_LNS_set_file
:
2430 unsigned int filenum
;
2432 /* The file and directory tables are 0
2433 based, the references are 1 based. */
2434 filenum
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2437 filename
= concat_filename (table
, filenum
);
2440 case DW_LNS_set_column
:
2441 column
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2444 case DW_LNS_negate_stmt
:
2445 is_stmt
= (!is_stmt
);
2447 case DW_LNS_set_basic_block
:
2449 case DW_LNS_const_add_pc
:
2450 if (lh
.line_range
== 0)
2452 if (lh
.maximum_ops_per_insn
== 1)
2453 address
+= (lh
.minimum_instruction_length
2454 * ((255 - lh
.opcode_base
) / lh
.line_range
));
2457 bfd_vma adjust
= ((255 - lh
.opcode_base
) / lh
.line_range
);
2458 address
+= (lh
.minimum_instruction_length
2459 * ((op_index
+ adjust
)
2460 / lh
.maximum_ops_per_insn
));
2461 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2464 case DW_LNS_fixed_advance_pc
:
2465 address
+= read_2_bytes (abfd
, &line_ptr
, line_end
);
2469 /* Unknown standard opcode, ignore it. */
2470 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
2471 (void) _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2480 if (unit
->line_offset
== 0)
2481 file
->line_table
= table
;
2482 if (sort_line_sequences (table
))
2486 while (table
->sequences
!= NULL
)
2488 struct line_sequence
* seq
= table
->sequences
;
2489 table
->sequences
= table
->sequences
->prev_sequence
;
2492 free (table
->files
);
2497 /* If ADDR is within TABLE set the output parameters and return the
2498 range of addresses covered by the entry used to fill them out.
2499 Otherwise set * FILENAME_PTR to NULL and return 0.
2500 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2501 are pointers to the objects to be filled in. */
2504 lookup_address_in_line_info_table (struct line_info_table
*table
,
2506 const char **filename_ptr
,
2507 unsigned int *linenumber_ptr
,
2508 unsigned int *discriminator_ptr
)
2510 struct line_sequence
*seq
= NULL
;
2511 struct line_info
*info
;
2514 /* Binary search the array of sequences. */
2516 high
= table
->num_sequences
;
2519 mid
= (low
+ high
) / 2;
2520 seq
= &table
->sequences
[mid
];
2521 if (addr
< seq
->low_pc
)
2523 else if (addr
>= seq
->last_line
->address
)
2529 /* Check for a valid sequence. */
2530 if (!seq
|| addr
< seq
->low_pc
|| addr
>= seq
->last_line
->address
)
2533 if (!build_line_info_table (table
, seq
))
2536 /* Binary search the array of line information. */
2538 high
= seq
->num_lines
;
2542 mid
= (low
+ high
) / 2;
2543 info
= seq
->line_info_lookup
[mid
];
2544 if (addr
< info
->address
)
2546 else if (addr
>= seq
->line_info_lookup
[mid
+ 1]->address
)
2552 /* Check for a valid line information entry. */
2554 && addr
>= info
->address
2555 && addr
< seq
->line_info_lookup
[mid
+ 1]->address
2556 && !(info
->end_sequence
|| info
== seq
->last_line
))
2558 *filename_ptr
= info
->filename
;
2559 *linenumber_ptr
= info
->line
;
2560 if (discriminator_ptr
)
2561 *discriminator_ptr
= info
->discriminator
;
2562 return seq
->last_line
->address
- seq
->low_pc
;
2566 *filename_ptr
= NULL
;
2570 /* Read in the .debug_ranges section for future reference. */
2573 read_debug_ranges (struct comp_unit
* unit
)
2575 struct dwarf2_debug
*stash
= unit
->stash
;
2576 struct dwarf2_debug_file
*file
= unit
->file
;
2578 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_ranges
],
2580 &file
->dwarf_ranges_buffer
, &file
->dwarf_ranges_size
);
2583 /* Read in the .debug_rnglists section for future reference. */
2586 read_debug_rnglists (struct comp_unit
* unit
)
2588 struct dwarf2_debug
*stash
= unit
->stash
;
2589 struct dwarf2_debug_file
*file
= unit
->file
;
2591 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_rnglists
],
2593 &file
->dwarf_rnglists_buffer
, &file
->dwarf_rnglists_size
);
2596 /* Function table functions. */
2599 compare_lookup_funcinfos (const void * a
, const void * b
)
2601 const struct lookup_funcinfo
* lookup1
= a
;
2602 const struct lookup_funcinfo
* lookup2
= b
;
2604 if (lookup1
->low_addr
< lookup2
->low_addr
)
2606 if (lookup1
->low_addr
> lookup2
->low_addr
)
2608 if (lookup1
->high_addr
< lookup2
->high_addr
)
2610 if (lookup1
->high_addr
> lookup2
->high_addr
)
2613 if (lookup1
->idx
< lookup2
->idx
)
2615 if (lookup1
->idx
> lookup2
->idx
)
2621 build_lookup_funcinfo_table (struct comp_unit
* unit
)
2623 struct lookup_funcinfo
*lookup_funcinfo_table
= unit
->lookup_funcinfo_table
;
2624 unsigned int number_of_functions
= unit
->number_of_functions
;
2625 struct funcinfo
*each
;
2626 struct lookup_funcinfo
*entry
;
2628 struct arange
*range
;
2629 bfd_vma low_addr
, high_addr
;
2631 if (lookup_funcinfo_table
|| number_of_functions
== 0)
2634 /* Create the function info lookup table. */
2635 lookup_funcinfo_table
= (struct lookup_funcinfo
*)
2636 bfd_malloc (number_of_functions
* sizeof (struct lookup_funcinfo
));
2637 if (lookup_funcinfo_table
== NULL
)
2640 /* Populate the function info lookup table. */
2641 func_index
= number_of_functions
;
2642 for (each
= unit
->function_table
; each
; each
= each
->prev_func
)
2644 entry
= &lookup_funcinfo_table
[--func_index
];
2645 entry
->funcinfo
= each
;
2646 entry
->idx
= func_index
;
2648 /* Calculate the lowest and highest address for this function entry. */
2649 low_addr
= entry
->funcinfo
->arange
.low
;
2650 high_addr
= entry
->funcinfo
->arange
.high
;
2652 for (range
= entry
->funcinfo
->arange
.next
; range
; range
= range
->next
)
2654 if (range
->low
< low_addr
)
2655 low_addr
= range
->low
;
2656 if (range
->high
> high_addr
)
2657 high_addr
= range
->high
;
2660 entry
->low_addr
= low_addr
;
2661 entry
->high_addr
= high_addr
;
2664 BFD_ASSERT (func_index
== 0);
2666 /* Sort the function by address. */
2667 qsort (lookup_funcinfo_table
,
2668 number_of_functions
,
2669 sizeof (struct lookup_funcinfo
),
2670 compare_lookup_funcinfos
);
2672 /* Calculate the high watermark for each function in the lookup table. */
2673 high_addr
= lookup_funcinfo_table
[0].high_addr
;
2674 for (func_index
= 1; func_index
< number_of_functions
; func_index
++)
2676 entry
= &lookup_funcinfo_table
[func_index
];
2677 if (entry
->high_addr
> high_addr
)
2678 high_addr
= entry
->high_addr
;
2680 entry
->high_addr
= high_addr
;
2683 unit
->lookup_funcinfo_table
= lookup_funcinfo_table
;
2687 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
2688 TRUE. Note that we need to find the function that has the smallest range
2689 that contains ADDR, to handle inlined functions without depending upon
2690 them being ordered in TABLE by increasing range. */
2693 lookup_address_in_function_table (struct comp_unit
*unit
,
2695 struct funcinfo
**function_ptr
)
2697 unsigned int number_of_functions
= unit
->number_of_functions
;
2698 struct lookup_funcinfo
* lookup_funcinfo
= NULL
;
2699 struct funcinfo
* funcinfo
= NULL
;
2700 struct funcinfo
* best_fit
= NULL
;
2701 bfd_vma best_fit_len
= 0;
2702 bfd_size_type low
, high
, mid
, first
;
2703 struct arange
*arange
;
2705 if (number_of_functions
== 0)
2708 if (!build_lookup_funcinfo_table (unit
))
2711 if (unit
->lookup_funcinfo_table
[number_of_functions
- 1].high_addr
< addr
)
2714 /* Find the first function in the lookup table which may contain the
2715 specified address. */
2717 high
= number_of_functions
;
2721 mid
= (low
+ high
) / 2;
2722 lookup_funcinfo
= &unit
->lookup_funcinfo_table
[mid
];
2723 if (addr
< lookup_funcinfo
->low_addr
)
2725 else if (addr
>= lookup_funcinfo
->high_addr
)
2731 /* Find the 'best' match for the address. The prior algorithm defined the
2732 best match as the function with the smallest address range containing
2733 the specified address. This definition should probably be changed to the
2734 innermost inline routine containing the address, but right now we want
2735 to get the same results we did before. */
2736 while (first
< number_of_functions
)
2738 if (addr
< unit
->lookup_funcinfo_table
[first
].low_addr
)
2740 funcinfo
= unit
->lookup_funcinfo_table
[first
].funcinfo
;
2742 for (arange
= &funcinfo
->arange
; arange
; arange
= arange
->next
)
2744 if (addr
< arange
->low
|| addr
>= arange
->high
)
2748 || arange
->high
- arange
->low
< best_fit_len
2749 /* The following comparison is designed to return the same
2750 match as the previous algorithm for routines which have the
2751 same best fit length. */
2752 || (arange
->high
- arange
->low
== best_fit_len
2753 && funcinfo
> best_fit
))
2755 best_fit
= funcinfo
;
2756 best_fit_len
= arange
->high
- arange
->low
;
2766 *function_ptr
= best_fit
;
2770 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2771 and LINENUMBER_PTR, and return TRUE. */
2774 lookup_symbol_in_function_table (struct comp_unit
*unit
,
2777 const char **filename_ptr
,
2778 unsigned int *linenumber_ptr
)
2780 struct funcinfo
* each_func
;
2781 struct funcinfo
* best_fit
= NULL
;
2782 bfd_vma best_fit_len
= 0;
2783 struct arange
*arange
;
2784 const char *name
= bfd_asymbol_name (sym
);
2785 asection
*sec
= bfd_asymbol_section (sym
);
2787 for (each_func
= unit
->function_table
;
2789 each_func
= each_func
->prev_func
)
2791 for (arange
= &each_func
->arange
;
2793 arange
= arange
->next
)
2795 if ((!each_func
->sec
|| each_func
->sec
== sec
)
2796 && addr
>= arange
->low
2797 && addr
< arange
->high
2799 && strcmp (name
, each_func
->name
) == 0
2801 || arange
->high
- arange
->low
< best_fit_len
))
2803 best_fit
= each_func
;
2804 best_fit_len
= arange
->high
- arange
->low
;
2811 best_fit
->sec
= sec
;
2812 *filename_ptr
= best_fit
->file
;
2813 *linenumber_ptr
= best_fit
->line
;
2820 /* Variable table functions. */
2822 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
2823 LINENUMBER_PTR, and return TRUE. */
2826 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
2829 const char **filename_ptr
,
2830 unsigned int *linenumber_ptr
)
2832 const char *name
= bfd_asymbol_name (sym
);
2833 asection
*sec
= bfd_asymbol_section (sym
);
2834 struct varinfo
* each
;
2836 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
2838 && each
->file
!= NULL
2839 && each
->name
!= NULL
2840 && each
->addr
== addr
2841 && (!each
->sec
|| each
->sec
== sec
)
2842 && strcmp (name
, each
->name
) == 0)
2848 *filename_ptr
= each
->file
;
2849 *linenumber_ptr
= each
->line
;
2856 static struct comp_unit
*stash_comp_unit (struct dwarf2_debug
*,
2857 struct dwarf2_debug_file
*);
2858 static bool comp_unit_maybe_decode_line_info (struct comp_unit
*);
2861 find_abstract_instance (struct comp_unit
*unit
,
2862 struct attribute
*attr_ptr
,
2863 unsigned int recur_count
,
2866 char **filename_ptr
,
2867 int *linenumber_ptr
)
2869 bfd
*abfd
= unit
->abfd
;
2870 bfd_byte
*info_ptr
= NULL
;
2871 bfd_byte
*info_ptr_end
;
2872 unsigned int abbrev_number
, i
;
2873 struct abbrev_info
*abbrev
;
2874 bfd_uint64_t die_ref
= attr_ptr
->u
.val
;
2875 struct attribute attr
;
2876 const char *name
= NULL
;
2878 if (recur_count
== 100)
2881 (_("DWARF error: abstract instance recursion detected"));
2882 bfd_set_error (bfd_error_bad_value
);
2886 /* DW_FORM_ref_addr can reference an entry in a different CU. It
2887 is an offset from the .debug_info section, not the current CU. */
2888 if (attr_ptr
->form
== DW_FORM_ref_addr
)
2890 /* We only support DW_FORM_ref_addr within the same file, so
2891 any relocations should be resolved already. Check this by
2892 testing for a zero die_ref; There can't be a valid reference
2893 to the header of a .debug_info section.
2894 DW_FORM_ref_addr is an offset relative to .debug_info.
2895 Normally when using the GNU linker this is accomplished by
2896 emitting a symbolic reference to a label, because .debug_info
2897 sections are linked at zero. When there are multiple section
2898 groups containing .debug_info, as there might be in a
2899 relocatable object file, it would be reasonable to assume that
2900 a symbolic reference to a label in any .debug_info section
2901 might be used. Since we lay out multiple .debug_info
2902 sections at non-zero VMAs (see place_sections), and read
2903 them contiguously into dwarf_info_buffer, that means the
2904 reference is relative to dwarf_info_buffer. */
2907 info_ptr
= unit
->file
->dwarf_info_buffer
;
2908 info_ptr_end
= info_ptr
+ unit
->file
->dwarf_info_size
;
2909 total
= info_ptr_end
- info_ptr
;
2912 else if (die_ref
>= total
)
2915 (_("DWARF error: invalid abstract instance DIE ref"));
2916 bfd_set_error (bfd_error_bad_value
);
2919 info_ptr
+= die_ref
;
2921 else if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
2923 bool first_time
= unit
->stash
->alt
.dwarf_info_buffer
== NULL
;
2925 info_ptr
= read_alt_indirect_ref (unit
, die_ref
);
2927 unit
->stash
->alt
.info_ptr
= unit
->stash
->alt
.dwarf_info_buffer
;
2928 if (info_ptr
== NULL
)
2931 (_("DWARF error: unable to read alt ref %" PRIu64
),
2932 (uint64_t) die_ref
);
2933 bfd_set_error (bfd_error_bad_value
);
2936 info_ptr_end
= (unit
->stash
->alt
.dwarf_info_buffer
2937 + unit
->stash
->alt
.dwarf_info_size
);
2938 if (unit
->stash
->alt
.all_comp_units
)
2939 unit
= unit
->stash
->alt
.all_comp_units
;
2942 if (attr_ptr
->form
== DW_FORM_ref_addr
2943 || attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
2945 /* Now find the CU containing this pointer. */
2946 if (info_ptr
>= unit
->info_ptr_unit
&& info_ptr
< unit
->end_ptr
)
2947 info_ptr_end
= unit
->end_ptr
;
2950 /* Check other CUs to see if they contain the abbrev. */
2951 struct comp_unit
*u
;
2953 for (u
= unit
->prev_unit
; u
!= NULL
; u
= u
->prev_unit
)
2954 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
2958 for (u
= unit
->next_unit
; u
!= NULL
; u
= u
->next_unit
)
2959 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
2962 if (attr_ptr
->form
== DW_FORM_ref_addr
)
2965 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->f
);
2968 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
2973 if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
2976 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->alt
);
2979 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
2987 (_("DWARF error: unable to locate abstract instance DIE ref %"
2988 PRIu64
), (uint64_t) die_ref
);
2989 bfd_set_error (bfd_error_bad_value
);
2993 info_ptr_end
= unit
->end_ptr
;
2998 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
2999 DW_FORM_ref_udata. These are all references relative to the
3000 start of the current CU. */
3003 info_ptr
= unit
->info_ptr_unit
;
3004 info_ptr_end
= unit
->end_ptr
;
3005 total
= info_ptr_end
- info_ptr
;
3006 if (!die_ref
|| die_ref
>= total
)
3009 (_("DWARF error: invalid abstract instance DIE ref"));
3010 bfd_set_error (bfd_error_bad_value
);
3013 info_ptr
+= die_ref
;
3016 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3017 false, info_ptr_end
);
3020 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3024 (_("DWARF error: could not find abbrev number %u"), abbrev_number
);
3025 bfd_set_error (bfd_error_bad_value
);
3030 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3032 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
,
3033 info_ptr
, info_ptr_end
);
3034 if (info_ptr
== NULL
)
3039 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3041 if (name
== NULL
&& is_str_attr (attr
.form
))
3044 if (non_mangled (unit
->lang
))
3048 case DW_AT_specification
:
3049 if (!find_abstract_instance (unit
, &attr
, recur_count
+ 1,
3051 filename_ptr
, linenumber_ptr
))
3054 case DW_AT_linkage_name
:
3055 case DW_AT_MIPS_linkage_name
:
3056 /* PR 16949: Corrupt debug info can place
3057 non-string forms into these attributes. */
3058 if (is_str_attr (attr
.form
))
3064 case DW_AT_decl_file
:
3065 if (!comp_unit_maybe_decode_line_info (unit
))
3067 *filename_ptr
= concat_filename (unit
->line_table
,
3070 case DW_AT_decl_line
:
3071 *linenumber_ptr
= attr
.u
.val
;
3084 read_ranges (struct comp_unit
*unit
, struct arange
*arange
,
3085 bfd_uint64_t offset
)
3087 bfd_byte
*ranges_ptr
;
3088 bfd_byte
*ranges_end
;
3089 bfd_vma base_address
= unit
->base_address
;
3091 if (! unit
->file
->dwarf_ranges_buffer
)
3093 if (! read_debug_ranges (unit
))
3097 ranges_ptr
= unit
->file
->dwarf_ranges_buffer
+ offset
;
3098 if (ranges_ptr
< unit
->file
->dwarf_ranges_buffer
)
3100 ranges_end
= unit
->file
->dwarf_ranges_buffer
+ unit
->file
->dwarf_ranges_size
;
3101 if (ranges_ptr
>= ranges_end
)
3109 /* PR 17512: file: 62cada7d. */
3110 if (2u * unit
->addr_size
> (size_t) (ranges_end
- ranges_ptr
))
3113 low_pc
= read_address (unit
, &ranges_ptr
, ranges_end
);
3114 high_pc
= read_address (unit
, &ranges_ptr
, ranges_end
);
3116 if (low_pc
== 0 && high_pc
== 0)
3118 if (low_pc
== -1UL && high_pc
!= -1UL)
3119 base_address
= high_pc
;
3122 if (!arange_add (unit
, arange
,
3123 base_address
+ low_pc
, base_address
+ high_pc
))
3131 read_rnglists (struct comp_unit
*unit
, struct arange
*arange
,
3132 bfd_uint64_t offset
)
3136 bfd_vma base_address
= unit
->base_address
;
3139 bfd
*abfd
= unit
->abfd
;
3141 if (! unit
->file
->dwarf_rnglists_buffer
)
3143 if (! read_debug_rnglists (unit
))
3147 rngs_ptr
= unit
->file
->dwarf_rnglists_buffer
+ offset
;
3148 if (rngs_ptr
< unit
->file
->dwarf_rnglists_buffer
)
3150 rngs_end
= unit
->file
->dwarf_rnglists_buffer
;
3151 rngs_end
+= unit
->file
->dwarf_rnglists_size
;
3155 enum dwarf_range_list_entry rlet
;
3157 if (rngs_ptr
>= rngs_end
)
3160 rlet
= read_1_byte (abfd
, &rngs_ptr
, rngs_end
);
3164 case DW_RLE_end_of_list
:
3167 case DW_RLE_base_address
:
3168 if (unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3170 base_address
= read_address (unit
, &rngs_ptr
, rngs_end
);
3173 case DW_RLE_start_length
:
3174 if (unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3176 low_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3178 high_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3182 case DW_RLE_offset_pair
:
3183 low_pc
= base_address
;
3184 low_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3186 high_pc
= base_address
;
3187 high_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3191 case DW_RLE_start_end
:
3192 if (2u * unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3194 low_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3195 high_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3198 /* TODO x-variants need .debug_addr support used for split-dwarf. */
3199 case DW_RLE_base_addressx
:
3200 case DW_RLE_startx_endx
:
3201 case DW_RLE_startx_length
:
3206 if (!arange_add (unit
, arange
, low_pc
, high_pc
))
3212 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
,
3213 bfd_uint64_t offset
)
3215 if (unit
->version
<= 4)
3216 return read_ranges (unit
, arange
, offset
);
3218 return read_rnglists (unit
, arange
, offset
);
3221 static struct funcinfo
*
3222 lookup_func_by_offset (bfd_uint64_t offset
, struct funcinfo
* table
)
3224 for (; table
!= NULL
; table
= table
->prev_func
)
3225 if (table
->unit_offset
== offset
)
3230 static struct varinfo
*
3231 lookup_var_by_offset (bfd_uint64_t offset
, struct varinfo
* table
)
3235 if (table
->unit_offset
== offset
)
3237 table
= table
->prev_var
;
3244 /* DWARF2 Compilation unit functions. */
3246 /* Scan over each die in a comp. unit looking for functions to add
3247 to the function table and variables to the variable table. */
3250 scan_unit_for_symbols (struct comp_unit
*unit
)
3252 bfd
*abfd
= unit
->abfd
;
3253 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
3254 bfd_byte
*info_ptr_end
= unit
->end_ptr
;
3255 int nesting_level
= 0;
3256 struct nest_funcinfo
3258 struct funcinfo
*func
;
3260 int nested_funcs_size
;
3262 /* Maintain a stack of in-scope functions and inlined functions, which we
3263 can use to set the caller_func field. */
3264 nested_funcs_size
= 32;
3265 nested_funcs
= (struct nest_funcinfo
*)
3266 bfd_malloc (nested_funcs_size
* sizeof (*nested_funcs
));
3267 if (nested_funcs
== NULL
)
3269 nested_funcs
[nesting_level
].func
= 0;
3271 /* PR 27484: We must scan the DIEs twice. The first time we look for
3272 function and variable tags and accumulate them into their respective
3273 tables. The second time through we process the attributes of the
3274 functions/variables and augment the table entries. */
3275 while (nesting_level
>= 0)
3277 unsigned int abbrev_number
, i
;
3278 struct abbrev_info
*abbrev
;
3279 struct funcinfo
*func
;
3280 struct varinfo
*var
;
3281 bfd_uint64_t current_offset
;
3283 /* PR 17512: file: 9f405d9d. */
3284 if (info_ptr
>= info_ptr_end
)
3287 current_offset
= info_ptr
- unit
->info_ptr_unit
;
3288 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3289 false, info_ptr_end
);
3290 if (abbrev_number
== 0)
3296 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3299 static unsigned int previous_failed_abbrev
= -1U;
3301 /* Avoid multiple reports of the same missing abbrev. */
3302 if (abbrev_number
!= previous_failed_abbrev
)
3305 (_("DWARF error: could not find abbrev number %u"),
3307 previous_failed_abbrev
= abbrev_number
;
3309 bfd_set_error (bfd_error_bad_value
);
3313 if (abbrev
->tag
== DW_TAG_subprogram
3314 || abbrev
->tag
== DW_TAG_entry_point
3315 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3317 size_t amt
= sizeof (struct funcinfo
);
3320 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
3323 func
->tag
= abbrev
->tag
;
3324 func
->prev_func
= unit
->function_table
;
3325 func
->unit_offset
= current_offset
;
3326 unit
->function_table
= func
;
3327 unit
->number_of_functions
++;
3328 BFD_ASSERT (!unit
->cached
);
3330 if (func
->tag
== DW_TAG_inlined_subroutine
)
3331 for (i
= nesting_level
; i
-- != 0; )
3332 if (nested_funcs
[i
].func
)
3334 func
->caller_func
= nested_funcs
[i
].func
;
3337 nested_funcs
[nesting_level
].func
= func
;
3342 if (abbrev
->tag
== DW_TAG_variable
3343 || abbrev
->tag
== DW_TAG_member
)
3345 size_t amt
= sizeof (struct varinfo
);
3347 var
= (struct varinfo
*) bfd_zalloc (abfd
, amt
);
3350 var
->tag
= abbrev
->tag
;
3352 var
->prev_var
= unit
->variable_table
;
3353 unit
->variable_table
= var
;
3354 var
->unit_offset
= current_offset
;
3355 /* PR 18205: Missing debug information can cause this
3356 var to be attached to an already cached unit. */
3361 /* No inline function in scope at this nesting level. */
3362 nested_funcs
[nesting_level
].func
= 0;
3365 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3367 struct attribute attr
;
3369 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
3370 unit
, info_ptr
, info_ptr_end
);
3371 if (info_ptr
== NULL
)
3375 if (abbrev
->has_children
)
3379 if (nesting_level
>= nested_funcs_size
)
3381 struct nest_funcinfo
*tmp
;
3383 nested_funcs_size
*= 2;
3384 tmp
= (struct nest_funcinfo
*)
3385 bfd_realloc (nested_funcs
,
3386 nested_funcs_size
* sizeof (*nested_funcs
));
3391 nested_funcs
[nesting_level
].func
= 0;
3395 /* This is the second pass over the abbrevs. */
3396 info_ptr
= unit
->first_child_die_ptr
;
3399 while (nesting_level
>= 0)
3401 unsigned int abbrev_number
, i
;
3402 struct abbrev_info
*abbrev
;
3403 struct attribute attr
;
3404 struct funcinfo
*func
;
3405 struct varinfo
*var
;
3407 bfd_vma high_pc
= 0;
3408 bool high_pc_relative
= false;
3409 bfd_uint64_t current_offset
;
3411 /* PR 17512: file: 9f405d9d. */
3412 if (info_ptr
>= info_ptr_end
)
3415 current_offset
= info_ptr
- unit
->info_ptr_unit
;
3416 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3417 false, info_ptr_end
);
3418 if (! abbrev_number
)
3424 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3425 /* This should have been handled above. */
3426 BFD_ASSERT (abbrev
!= NULL
);
3430 if (abbrev
->tag
== DW_TAG_subprogram
3431 || abbrev
->tag
== DW_TAG_entry_point
3432 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3434 func
= lookup_func_by_offset (current_offset
, unit
->function_table
);
3438 else if (abbrev
->tag
== DW_TAG_variable
3439 || abbrev
->tag
== DW_TAG_member
)
3441 var
= lookup_var_by_offset (current_offset
, unit
->variable_table
);
3446 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3448 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
3449 unit
, info_ptr
, info_ptr_end
);
3450 if (info_ptr
== NULL
)
3457 case DW_AT_call_file
:
3458 func
->caller_file
= concat_filename (unit
->line_table
,
3462 case DW_AT_call_line
:
3463 func
->caller_line
= attr
.u
.val
;
3466 case DW_AT_abstract_origin
:
3467 case DW_AT_specification
:
3468 if (!find_abstract_instance (unit
, &attr
, 0,
3477 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3479 if (func
->name
== NULL
&& is_str_attr (attr
.form
))
3481 func
->name
= attr
.u
.str
;
3482 if (non_mangled (unit
->lang
))
3483 func
->is_linkage
= true;
3487 case DW_AT_linkage_name
:
3488 case DW_AT_MIPS_linkage_name
:
3489 /* PR 16949: Corrupt debug info can place
3490 non-string forms into these attributes. */
3491 if (is_str_attr (attr
.form
))
3493 func
->name
= attr
.u
.str
;
3494 func
->is_linkage
= true;
3499 low_pc
= attr
.u
.val
;
3503 high_pc
= attr
.u
.val
;
3504 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
3508 if (!read_rangelist (unit
, &func
->arange
, attr
.u
.val
))
3512 case DW_AT_decl_file
:
3513 func
->file
= concat_filename (unit
->line_table
,
3517 case DW_AT_decl_line
:
3518 func
->line
= attr
.u
.val
;
3529 case DW_AT_specification
:
3532 struct varinfo
* spec_var
;
3534 spec_var
= lookup_var_by_offset (attr
.u
.val
,
3535 unit
->variable_table
);
3536 if (spec_var
== NULL
)
3538 _bfd_error_handler (_("DWARF error: could not find "
3539 "variable specification "
3541 (unsigned long) attr
.u
.val
);
3545 if (var
->name
== NULL
)
3546 var
->name
= spec_var
->name
;
3547 if (var
->file
== NULL
&& spec_var
->file
!= NULL
)
3548 var
->file
= strdup (spec_var
->file
);
3550 var
->line
= spec_var
->line
;
3551 if (var
->sec
== NULL
)
3552 var
->sec
= spec_var
->sec
;
3557 if (is_str_attr (attr
.form
))
3558 var
->name
= attr
.u
.str
;
3561 case DW_AT_decl_file
:
3562 var
->file
= concat_filename (unit
->line_table
,
3566 case DW_AT_decl_line
:
3567 var
->line
= attr
.u
.val
;
3570 case DW_AT_external
:
3571 if (attr
.u
.val
!= 0)
3575 case DW_AT_location
:
3579 case DW_FORM_block1
:
3580 case DW_FORM_block2
:
3581 case DW_FORM_block4
:
3582 case DW_FORM_exprloc
:
3583 if (attr
.u
.blk
->data
!= NULL
3584 && *attr
.u
.blk
->data
== DW_OP_addr
)
3588 /* Verify that DW_OP_addr is the only opcode in the
3589 location, in which case the block size will be 1
3590 plus the address size. */
3591 /* ??? For TLS variables, gcc can emit
3592 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
3593 which we don't handle here yet. */
3594 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
3595 var
->addr
= bfd_get (unit
->addr_size
* 8,
3597 attr
.u
.blk
->data
+ 1);
3612 if (abbrev
->has_children
)
3615 if (high_pc_relative
)
3618 if (func
&& high_pc
!= 0)
3620 if (!arange_add (unit
, &func
->arange
, low_pc
, high_pc
))
3625 free (nested_funcs
);
3629 free (nested_funcs
);
3633 /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
3634 includes the compilation unit header that proceeds the DIE's, but
3635 does not include the length field that precedes each compilation
3636 unit header. END_PTR points one past the end of this comp unit.
3637 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
3639 This routine does not read the whole compilation unit; only enough
3640 to get to the line number information for the compilation unit. */
3642 static struct comp_unit
*
3643 parse_comp_unit (struct dwarf2_debug
*stash
,
3644 struct dwarf2_debug_file
*file
,
3646 bfd_vma unit_length
,
3647 bfd_byte
*info_ptr_unit
,
3648 unsigned int offset_size
)
3650 struct comp_unit
* unit
;
3651 unsigned int version
;
3652 bfd_uint64_t abbrev_offset
= 0;
3653 /* Initialize it just to avoid a GCC false warning. */
3654 unsigned int addr_size
= -1;
3655 struct abbrev_info
** abbrevs
;
3656 unsigned int abbrev_number
, i
;
3657 struct abbrev_info
*abbrev
;
3658 struct attribute attr
;
3659 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
3662 bfd_vma high_pc
= 0;
3663 bfd
*abfd
= file
->bfd_ptr
;
3664 bool high_pc_relative
= false;
3665 enum dwarf_unit_type unit_type
;
3667 version
= read_2_bytes (abfd
, &info_ptr
, end_ptr
);
3668 if (version
< 2 || version
> 5)
3670 /* PR 19872: A version number of 0 probably means that there is padding
3671 at the end of the .debug_info section. Gold puts it there when
3672 performing an incremental link, for example. So do not generate
3673 an error, just return a NULL. */
3677 (_("DWARF error: found dwarf version '%u', this reader"
3678 " only handles version 2, 3, 4 and 5 information"), version
);
3679 bfd_set_error (bfd_error_bad_value
);
3685 unit_type
= DW_UT_compile
;
3688 unit_type
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
3689 addr_size
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
3692 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
3693 if (offset_size
== 4)
3694 abbrev_offset
= read_4_bytes (abfd
, &info_ptr
, end_ptr
);
3696 abbrev_offset
= read_8_bytes (abfd
, &info_ptr
, end_ptr
);
3699 addr_size
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
3701 if (unit_type
== DW_UT_type
)
3703 /* Skip type signature. */
3706 /* Skip type offset. */
3707 info_ptr
+= offset_size
;
3710 if (addr_size
> sizeof (bfd_vma
))
3713 /* xgettext: c-format */
3714 (_("DWARF error: found address size '%u', this reader"
3715 " can not handle sizes greater than '%u'"),
3717 (unsigned int) sizeof (bfd_vma
));
3718 bfd_set_error (bfd_error_bad_value
);
3722 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
3725 ("DWARF error: found address size '%u', this reader"
3726 " can only handle address sizes '2', '4' and '8'", addr_size
);
3727 bfd_set_error (bfd_error_bad_value
);
3731 /* Read the abbrevs for this compilation unit into a table. */
3732 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
, file
);
3736 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3738 if (! abbrev_number
)
3740 /* PR 19872: An abbrev number of 0 probably means that there is padding
3741 at the end of the .debug_abbrev section. Gold puts it there when
3742 performing an incremental link, for example. So do not generate
3743 an error, just return a NULL. */
3747 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
3750 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
3752 bfd_set_error (bfd_error_bad_value
);
3756 amt
= sizeof (struct comp_unit
);
3757 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
3761 unit
->version
= version
;
3762 unit
->addr_size
= addr_size
;
3763 unit
->offset_size
= offset_size
;
3764 unit
->abbrevs
= abbrevs
;
3765 unit
->end_ptr
= end_ptr
;
3766 unit
->stash
= stash
;
3768 unit
->info_ptr_unit
= info_ptr_unit
;
3770 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3772 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
, end_ptr
);
3773 if (info_ptr
== NULL
)
3776 /* Store the data if it is of an attribute we want to keep in a
3777 partial symbol table. */
3780 case DW_AT_stmt_list
:
3782 unit
->line_offset
= attr
.u
.val
;
3786 if (is_str_attr (attr
.form
))
3787 unit
->name
= attr
.u
.str
;
3791 low_pc
= attr
.u
.val
;
3792 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
3793 this is the base address to use when reading location
3794 lists or range lists. */
3795 if (abbrev
->tag
== DW_TAG_compile_unit
)
3796 unit
->base_address
= low_pc
;
3800 high_pc
= attr
.u
.val
;
3801 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
3805 if (!read_rangelist (unit
, &unit
->arange
, attr
.u
.val
))
3809 case DW_AT_comp_dir
:
3811 char *comp_dir
= attr
.u
.str
;
3813 /* PR 17512: file: 1fe726be. */
3814 if (! is_str_attr (attr
.form
))
3817 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
3823 /* Irix 6.2 native cc prepends <machine>.: to the compilation
3824 directory, get rid of it. */
3825 char *cp
= strchr (comp_dir
, ':');
3827 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
3830 unit
->comp_dir
= comp_dir
;
3834 case DW_AT_language
:
3835 unit
->lang
= attr
.u
.val
;
3842 if (high_pc_relative
)
3846 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
3850 unit
->first_child_die_ptr
= info_ptr
;
3854 /* Return TRUE if UNIT may contain the address given by ADDR. When
3855 there are functions written entirely with inline asm statements, the
3856 range info in the compilation unit header may not be correct. We
3857 need to consult the line info table to see if a compilation unit
3858 really contains the given address. */
3861 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
3863 struct arange
*arange
;
3868 arange
= &unit
->arange
;
3871 if (addr
>= arange
->low
&& addr
< arange
->high
)
3873 arange
= arange
->next
;
3880 /* If UNIT contains ADDR, set the output parameters to the values for
3881 the line containing ADDR. The output parameters, FILENAME_PTR,
3882 FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects
3885 Returns the range of addresses covered by the entry that was used
3886 to fill in *LINENUMBER_PTR or 0 if it was not filled in. */
3889 comp_unit_find_nearest_line (struct comp_unit
*unit
,
3891 const char **filename_ptr
,
3892 struct funcinfo
**function_ptr
,
3893 unsigned int *linenumber_ptr
,
3894 unsigned int *discriminator_ptr
)
3898 if (!comp_unit_maybe_decode_line_info (unit
))
3901 *function_ptr
= NULL
;
3902 func_p
= lookup_address_in_function_table (unit
, addr
, function_ptr
);
3903 if (func_p
&& (*function_ptr
)->tag
== DW_TAG_inlined_subroutine
)
3904 unit
->stash
->inliner_chain
= *function_ptr
;
3906 return lookup_address_in_line_info_table (unit
->line_table
, addr
,
3912 /* Check to see if line info is already decoded in a comp_unit.
3913 If not, decode it. Returns TRUE if no errors were encountered;
3917 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
)
3922 if (! unit
->line_table
)
3924 if (! unit
->stmtlist
)
3930 unit
->line_table
= decode_line_info (unit
);
3932 if (! unit
->line_table
)
3938 if (unit
->first_child_die_ptr
< unit
->end_ptr
3939 && ! scan_unit_for_symbols (unit
))
3949 /* If UNIT contains SYM at ADDR, set the output parameters to the
3950 values for the line containing SYM. The output parameters,
3951 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
3954 Return TRUE if UNIT contains SYM, and no errors were encountered;
3958 comp_unit_find_line (struct comp_unit
*unit
,
3961 const char **filename_ptr
,
3962 unsigned int *linenumber_ptr
)
3964 if (!comp_unit_maybe_decode_line_info (unit
))
3967 if (sym
->flags
& BSF_FUNCTION
)
3968 return lookup_symbol_in_function_table (unit
, sym
, addr
,
3972 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
3977 static struct funcinfo
*
3978 reverse_funcinfo_list (struct funcinfo
*head
)
3980 struct funcinfo
*rhead
;
3981 struct funcinfo
*temp
;
3983 for (rhead
= NULL
; head
; head
= temp
)
3985 temp
= head
->prev_func
;
3986 head
->prev_func
= rhead
;
3992 static struct varinfo
*
3993 reverse_varinfo_list (struct varinfo
*head
)
3995 struct varinfo
*rhead
;
3996 struct varinfo
*temp
;
3998 for (rhead
= NULL
; head
; head
= temp
)
4000 temp
= head
->prev_var
;
4001 head
->prev_var
= rhead
;
4007 /* Extract all interesting funcinfos and varinfos of a compilation
4008 unit into hash tables for faster lookup. Returns TRUE if no
4009 errors were enountered; FALSE otherwise. */
4012 comp_unit_hash_info (struct dwarf2_debug
*stash
,
4013 struct comp_unit
*unit
,
4014 struct info_hash_table
*funcinfo_hash_table
,
4015 struct info_hash_table
*varinfo_hash_table
)
4017 struct funcinfo
* each_func
;
4018 struct varinfo
* each_var
;
4021 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
4023 if (!comp_unit_maybe_decode_line_info (unit
))
4026 BFD_ASSERT (!unit
->cached
);
4028 /* To preserve the original search order, we went to visit the function
4029 infos in the reversed order of the list. However, making the list
4030 bi-directional use quite a bit of extra memory. So we reverse
4031 the list first, traverse the list in the now reversed order and
4032 finally reverse the list again to get back the original order. */
4033 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4034 for (each_func
= unit
->function_table
;
4036 each_func
= each_func
->prev_func
)
4038 /* Skip nameless functions. */
4039 if (each_func
->name
)
4040 /* There is no need to copy name string into hash table as
4041 name string is either in the dwarf string buffer or
4042 info in the stash. */
4043 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
4044 (void*) each_func
, false);
4046 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4050 /* We do the same for variable infos. */
4051 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4052 for (each_var
= unit
->variable_table
;
4054 each_var
= each_var
->prev_var
)
4056 /* Skip stack vars and vars with no files or names. */
4057 if (! each_var
->stack
4058 && each_var
->file
!= NULL
4059 && each_var
->name
!= NULL
)
4060 /* There is no need to copy name string into hash table as
4061 name string is either in the dwarf string buffer or
4062 info in the stash. */
4063 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
4064 (void*) each_var
, false);
4067 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4068 unit
->cached
= true;
4072 /* Locate a section in a BFD containing debugging info. The search starts
4073 from the section after AFTER_SEC, or from the first section in the BFD if
4074 AFTER_SEC is NULL. The search works by examining the names of the
4075 sections. There are three permissiable names. The first two are given
4076 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
4077 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
4078 This is a variation on the .debug_info section which has a checksum
4079 describing the contents appended onto the name. This allows the linker to
4080 identify and discard duplicate debugging sections for different
4081 compilation units. */
4082 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
4085 find_debug_info (bfd
*abfd
, const struct dwarf_debug_section
*debug_sections
,
4086 asection
*after_sec
)
4091 if (after_sec
== NULL
)
4093 look
= debug_sections
[debug_info
].uncompressed_name
;
4094 msec
= bfd_get_section_by_name (abfd
, look
);
4098 look
= debug_sections
[debug_info
].compressed_name
;
4099 msec
= bfd_get_section_by_name (abfd
, look
);
4103 for (msec
= abfd
->sections
; msec
!= NULL
; msec
= msec
->next
)
4104 if (startswith (msec
->name
, GNU_LINKONCE_INFO
))
4110 for (msec
= after_sec
->next
; msec
!= NULL
; msec
= msec
->next
)
4112 look
= debug_sections
[debug_info
].uncompressed_name
;
4113 if (strcmp (msec
->name
, look
) == 0)
4116 look
= debug_sections
[debug_info
].compressed_name
;
4117 if (look
!= NULL
&& strcmp (msec
->name
, look
) == 0)
4120 if (startswith (msec
->name
, GNU_LINKONCE_INFO
))
4127 /* Transfer VMAs from object file to separate debug file. */
4130 set_debug_vma (bfd
*orig_bfd
, bfd
*debug_bfd
)
4134 for (s
= orig_bfd
->sections
, d
= debug_bfd
->sections
;
4135 s
!= NULL
&& d
!= NULL
;
4136 s
= s
->next
, d
= d
->next
)
4138 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4140 /* ??? Assumes 1-1 correspondence between sections in the
4142 if (strcmp (s
->name
, d
->name
) == 0)
4144 d
->output_section
= s
->output_section
;
4145 d
->output_offset
= s
->output_offset
;
4151 /* If the dwarf2 info was found in a separate debug file, return the
4152 debug file section corresponding to the section in the original file
4153 and the debug file symbols. */
4156 _bfd_dwarf2_stash_syms (struct dwarf2_debug
*stash
, bfd
*abfd
,
4157 asection
**sec
, asymbol
***syms
)
4159 if (stash
->f
.bfd_ptr
!= abfd
)
4165 *syms
= stash
->f
.syms
;
4169 for (s
= abfd
->sections
, d
= stash
->f
.bfd_ptr
->sections
;
4170 s
!= NULL
&& d
!= NULL
;
4171 s
= s
->next
, d
= d
->next
)
4173 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4176 && strcmp (s
->name
, d
->name
) == 0)
4179 *syms
= stash
->f
.syms
;
4186 /* Unset vmas for adjusted sections in STASH. */
4189 unset_sections (struct dwarf2_debug
*stash
)
4192 struct adjusted_section
*p
;
4194 i
= stash
->adjusted_section_count
;
4195 p
= stash
->adjusted_sections
;
4196 for (; i
> 0; i
--, p
++)
4197 p
->section
->vma
= 0;
4200 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4201 relocatable object file. VMAs are normally all zero in relocatable
4202 object files, so if we want to distinguish locations in sections by
4203 address we need to set VMAs so the sections do not overlap. We
4204 also set VMA on .debug_info so that when we have multiple
4205 .debug_info sections (or the linkonce variant) they also do not
4206 overlap. The multiple .debug_info sections make up a single
4207 logical section. ??? We should probably do the same for other
4211 place_sections (bfd
*orig_bfd
, struct dwarf2_debug
*stash
)
4214 struct adjusted_section
*p
;
4216 const char *debug_info_name
;
4218 if (stash
->adjusted_section_count
!= 0)
4220 i
= stash
->adjusted_section_count
;
4221 p
= stash
->adjusted_sections
;
4222 for (; i
> 0; i
--, p
++)
4223 p
->section
->vma
= p
->adj_vma
;
4227 debug_info_name
= stash
->debug_sections
[debug_info
].uncompressed_name
;
4234 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4238 if ((sect
->output_section
!= NULL
4239 && sect
->output_section
!= sect
4240 && (sect
->flags
& SEC_DEBUGGING
) == 0)
4244 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
4245 || startswith (sect
->name
, GNU_LINKONCE_INFO
));
4247 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
4253 if (abfd
== stash
->f
.bfd_ptr
)
4255 abfd
= stash
->f
.bfd_ptr
;
4259 stash
->adjusted_section_count
= -1;
4262 bfd_vma last_vma
= 0, last_dwarf
= 0;
4263 size_t amt
= i
* sizeof (struct adjusted_section
);
4265 p
= (struct adjusted_section
*) bfd_malloc (amt
);
4269 stash
->adjusted_sections
= p
;
4270 stash
->adjusted_section_count
= i
;
4277 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4282 if ((sect
->output_section
!= NULL
4283 && sect
->output_section
!= sect
4284 && (sect
->flags
& SEC_DEBUGGING
) == 0)
4288 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
4289 || startswith (sect
->name
, GNU_LINKONCE_INFO
));
4291 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
4295 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
4299 BFD_ASSERT (sect
->alignment_power
== 0);
4300 sect
->vma
= last_dwarf
;
4305 /* Align the new address to the current section
4307 last_vma
= ((last_vma
4308 + ~(-((bfd_vma
) 1 << sect
->alignment_power
)))
4309 & (-((bfd_vma
) 1 << sect
->alignment_power
)));
4310 sect
->vma
= last_vma
;
4315 p
->adj_vma
= sect
->vma
;
4318 if (abfd
== stash
->f
.bfd_ptr
)
4320 abfd
= stash
->f
.bfd_ptr
;
4324 if (orig_bfd
!= stash
->f
.bfd_ptr
)
4325 set_debug_vma (orig_bfd
, stash
->f
.bfd_ptr
);
4330 /* Look up a funcinfo by name using the given info hash table. If found,
4331 also update the locations pointed to by filename_ptr and linenumber_ptr.
4333 This function returns TRUE if a funcinfo that matches the given symbol
4334 and address is found with any error; otherwise it returns FALSE. */
4337 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
4340 const char **filename_ptr
,
4341 unsigned int *linenumber_ptr
)
4343 struct funcinfo
* each_func
;
4344 struct funcinfo
* best_fit
= NULL
;
4345 bfd_vma best_fit_len
= 0;
4346 struct info_list_node
*node
;
4347 struct arange
*arange
;
4348 const char *name
= bfd_asymbol_name (sym
);
4349 asection
*sec
= bfd_asymbol_section (sym
);
4351 for (node
= lookup_info_hash_table (hash_table
, name
);
4355 each_func
= (struct funcinfo
*) node
->info
;
4356 for (arange
= &each_func
->arange
;
4358 arange
= arange
->next
)
4360 if ((!each_func
->sec
|| each_func
->sec
== sec
)
4361 && addr
>= arange
->low
4362 && addr
< arange
->high
4364 || arange
->high
- arange
->low
< best_fit_len
))
4366 best_fit
= each_func
;
4367 best_fit_len
= arange
->high
- arange
->low
;
4374 best_fit
->sec
= sec
;
4375 *filename_ptr
= best_fit
->file
;
4376 *linenumber_ptr
= best_fit
->line
;
4383 /* Look up a varinfo by name using the given info hash table. If found,
4384 also update the locations pointed to by filename_ptr and linenumber_ptr.
4386 This function returns TRUE if a varinfo that matches the given symbol
4387 and address is found with any error; otherwise it returns FALSE. */
4390 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
4393 const char **filename_ptr
,
4394 unsigned int *linenumber_ptr
)
4396 const char *name
= bfd_asymbol_name (sym
);
4397 asection
*sec
= bfd_asymbol_section (sym
);
4398 struct varinfo
* each
;
4399 struct info_list_node
*node
;
4401 for (node
= lookup_info_hash_table (hash_table
, name
);
4405 each
= (struct varinfo
*) node
->info
;
4406 if (each
->addr
== addr
4407 && (!each
->sec
|| each
->sec
== sec
))
4410 *filename_ptr
= each
->file
;
4411 *linenumber_ptr
= each
->line
;
4419 /* Update the funcinfo and varinfo info hash tables if they are
4420 not up to date. Returns TRUE if there is no error; otherwise
4421 returns FALSE and disable the info hash tables. */
4424 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
4426 struct comp_unit
*each
;
4428 /* Exit if hash tables are up-to-date. */
4429 if (stash
->f
.all_comp_units
== stash
->hash_units_head
)
4432 if (stash
->hash_units_head
)
4433 each
= stash
->hash_units_head
->prev_unit
;
4435 each
= stash
->f
.last_comp_unit
;
4439 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
4440 stash
->varinfo_hash_table
))
4442 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
4445 each
= each
->prev_unit
;
4448 stash
->hash_units_head
= stash
->f
.all_comp_units
;
4452 /* Check consistency of info hash tables. This is for debugging only. */
4454 static void ATTRIBUTE_UNUSED
4455 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
4457 struct comp_unit
*each_unit
;
4458 struct funcinfo
*each_func
;
4459 struct varinfo
*each_var
;
4460 struct info_list_node
*node
;
4463 for (each_unit
= stash
->f
.all_comp_units
;
4465 each_unit
= each_unit
->next_unit
)
4467 for (each_func
= each_unit
->function_table
;
4469 each_func
= each_func
->prev_func
)
4471 if (!each_func
->name
)
4473 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
4477 while (node
&& !found
)
4479 found
= node
->info
== each_func
;
4485 for (each_var
= each_unit
->variable_table
;
4487 each_var
= each_var
->prev_var
)
4489 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
4491 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
4495 while (node
&& !found
)
4497 found
= node
->info
== each_var
;
4505 /* Check to see if we want to enable the info hash tables, which consume
4506 quite a bit of memory. Currently we only check the number times
4507 bfd_dwarf2_find_line is called. In the future, we may also want to
4508 take the number of symbols into account. */
4511 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
4513 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
4515 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
4518 /* FIXME: Maybe we should check the reduce_memory_overheads
4519 and optimize fields in the bfd_link_info structure ? */
4521 /* Create hash tables. */
4522 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
4523 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
4524 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
4526 /* Turn off info hashes if any allocation above fails. */
4527 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
4530 /* We need a forced update so that the info hash tables will
4531 be created even though there is no compilation unit. That
4532 happens if STASH_INFO_HASH_TRIGGER is 0. */
4533 if (stash_maybe_update_info_hash_tables (stash
))
4534 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
4537 /* Find the file and line associated with a symbol and address using the
4538 info hash tables of a stash. If there is a match, the function returns
4539 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
4540 otherwise it returns FALSE. */
4543 stash_find_line_fast (struct dwarf2_debug
*stash
,
4546 const char **filename_ptr
,
4547 unsigned int *linenumber_ptr
)
4549 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
4551 if (sym
->flags
& BSF_FUNCTION
)
4552 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
4553 filename_ptr
, linenumber_ptr
);
4554 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
4555 filename_ptr
, linenumber_ptr
);
4558 /* Save current section VMAs. */
4561 save_section_vma (const bfd
*abfd
, struct dwarf2_debug
*stash
)
4566 if (abfd
->section_count
== 0)
4568 stash
->sec_vma
= bfd_malloc (sizeof (*stash
->sec_vma
) * abfd
->section_count
);
4569 if (stash
->sec_vma
== NULL
)
4571 stash
->sec_vma_count
= abfd
->section_count
;
4572 for (i
= 0, s
= abfd
->sections
;
4573 s
!= NULL
&& i
< abfd
->section_count
;
4576 if (s
->output_section
!= NULL
)
4577 stash
->sec_vma
[i
] = s
->output_section
->vma
+ s
->output_offset
;
4579 stash
->sec_vma
[i
] = s
->vma
;
4584 /* Compare current section VMAs against those at the time the stash
4585 was created. If find_nearest_line is used in linker warnings or
4586 errors early in the link process, the debug info stash will be
4587 invalid for later calls. This is because we relocate debug info
4588 sections, so the stashed section contents depend on symbol values,
4589 which in turn depend on section VMAs. */
4592 section_vma_same (const bfd
*abfd
, const struct dwarf2_debug
*stash
)
4597 /* PR 24334: If the number of sections in ABFD has changed between
4598 when the stash was created and now, then we cannot trust the
4599 stashed vma information. */
4600 if (abfd
->section_count
!= stash
->sec_vma_count
)
4603 for (i
= 0, s
= abfd
->sections
;
4604 s
!= NULL
&& i
< abfd
->section_count
;
4609 if (s
->output_section
!= NULL
)
4610 vma
= s
->output_section
->vma
+ s
->output_offset
;
4613 if (vma
!= stash
->sec_vma
[i
])
4619 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
4620 If DEBUG_BFD is not specified, we read debug information from ABFD
4621 or its gnu_debuglink. The results will be stored in PINFO.
4622 The function returns TRUE iff debug information is ready. */
4625 _bfd_dwarf2_slurp_debug_info (bfd
*abfd
, bfd
*debug_bfd
,
4626 const struct dwarf_debug_section
*debug_sections
,
4631 size_t amt
= sizeof (struct dwarf2_debug
);
4632 bfd_size_type total_size
;
4634 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
4638 if (stash
->orig_bfd
== abfd
4639 && section_vma_same (abfd
, stash
))
4641 /* Check that we did previously find some debug information
4642 before attempting to make use of it. */
4643 if (stash
->f
.bfd_ptr
!= NULL
)
4645 if (do_place
&& !place_sections (abfd
, stash
))
4652 _bfd_dwarf2_cleanup_debug_info (abfd
, pinfo
);
4653 memset (stash
, 0, amt
);
4657 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
4661 stash
->orig_bfd
= abfd
;
4662 stash
->debug_sections
= debug_sections
;
4663 stash
->f
.syms
= symbols
;
4664 if (!save_section_vma (abfd
, stash
))
4667 stash
->f
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
4668 del_abbrev
, calloc
, free
);
4669 if (!stash
->f
.abbrev_offsets
)
4672 stash
->alt
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
4673 del_abbrev
, calloc
, free
);
4674 if (!stash
->alt
.abbrev_offsets
)
4679 if (debug_bfd
== NULL
)
4682 msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4683 if (msec
== NULL
&& abfd
== debug_bfd
)
4685 char * debug_filename
;
4687 debug_filename
= bfd_follow_build_id_debuglink (abfd
, DEBUGDIR
);
4688 if (debug_filename
== NULL
)
4689 debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
4691 if (debug_filename
== NULL
)
4692 /* No dwarf2 info, and no gnu_debuglink to follow.
4693 Note that at this point the stash has been allocated, but
4694 contains zeros. This lets future calls to this function
4695 fail more quickly. */
4698 debug_bfd
= bfd_openr (debug_filename
, NULL
);
4699 free (debug_filename
);
4700 if (debug_bfd
== NULL
)
4701 /* FIXME: Should we report our failure to follow the debuglink ? */
4704 /* Set BFD_DECOMPRESS to decompress debug sections. */
4705 debug_bfd
->flags
|= BFD_DECOMPRESS
;
4706 if (!bfd_check_format (debug_bfd
, bfd_object
)
4707 || (msec
= find_debug_info (debug_bfd
,
4708 debug_sections
, NULL
)) == NULL
4709 || !bfd_generic_link_read_symbols (debug_bfd
))
4711 bfd_close (debug_bfd
);
4715 symbols
= bfd_get_outsymbols (debug_bfd
);
4716 stash
->f
.syms
= symbols
;
4717 stash
->close_on_cleanup
= true;
4719 stash
->f
.bfd_ptr
= debug_bfd
;
4722 && !place_sections (abfd
, stash
))
4725 /* There can be more than one DWARF2 info section in a BFD these
4726 days. First handle the easy case when there's only one. If
4727 there's more than one, try case two: none of the sections is
4728 compressed. In that case, read them all in and produce one
4729 large stash. We do this in two passes - in the first pass we
4730 just accumulate the section sizes, and in the second pass we
4731 read in the section's contents. (The allows us to avoid
4732 reallocing the data as we add sections to the stash.) If
4733 some or all sections are compressed, then do things the slow
4734 way, with a bunch of reallocs. */
4736 if (! find_debug_info (debug_bfd
, debug_sections
, msec
))
4738 /* Case 1: only one info section. */
4739 total_size
= msec
->size
;
4740 if (! read_section (debug_bfd
, &stash
->debug_sections
[debug_info
],
4742 &stash
->f
.dwarf_info_buffer
, &total_size
))
4747 /* Case 2: multiple sections. */
4748 for (total_size
= 0;
4750 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
4752 /* Catch PR25070 testcase overflowing size calculation here. */
4753 if (total_size
+ msec
->size
< total_size
4754 || total_size
+ msec
->size
< msec
->size
)
4756 bfd_set_error (bfd_error_no_memory
);
4759 total_size
+= msec
->size
;
4762 stash
->f
.dwarf_info_buffer
= (bfd_byte
*) bfd_malloc (total_size
);
4763 if (stash
->f
.dwarf_info_buffer
== NULL
)
4767 for (msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4769 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
4777 if (!(bfd_simple_get_relocated_section_contents
4778 (debug_bfd
, msec
, stash
->f
.dwarf_info_buffer
+ total_size
,
4786 stash
->f
.info_ptr
= stash
->f
.dwarf_info_buffer
;
4787 stash
->f
.dwarf_info_size
= total_size
;
4791 /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
4793 static struct comp_unit
*
4794 stash_comp_unit (struct dwarf2_debug
*stash
, struct dwarf2_debug_file
*file
)
4796 bfd_size_type length
;
4797 unsigned int offset_size
;
4798 bfd_byte
*info_ptr_unit
= file
->info_ptr
;
4799 bfd_byte
*info_ptr_end
= file
->dwarf_info_buffer
+ file
->dwarf_info_size
;
4801 if (file
->info_ptr
>= info_ptr_end
)
4804 length
= read_4_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
4805 /* A 0xffffff length is the DWARF3 way of indicating
4806 we use 64-bit offsets, instead of 32-bit offsets. */
4807 if (length
== 0xffffffff)
4810 length
= read_8_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
4812 /* A zero length is the IRIX way of indicating 64-bit offsets,
4813 mostly because the 64-bit length will generally fit in 32
4814 bits, and the endianness helps. */
4815 else if (length
== 0)
4818 length
= read_4_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
4820 /* In the absence of the hints above, we assume 32-bit DWARF2
4821 offsets even for targets with 64-bit addresses, because:
4822 a) most of the time these targets will not have generated
4823 more than 2Gb of debug info and so will not need 64-bit
4826 b) if they do use 64-bit offsets but they are not using
4827 the size hints that are tested for above then they are
4828 not conforming to the DWARF3 standard anyway. */
4833 && length
<= (size_t) (info_ptr_end
- file
->info_ptr
))
4835 struct comp_unit
*each
= parse_comp_unit (stash
, file
,
4836 file
->info_ptr
, length
,
4837 info_ptr_unit
, offset_size
);
4840 if (file
->all_comp_units
)
4841 file
->all_comp_units
->prev_unit
= each
;
4843 file
->last_comp_unit
= each
;
4845 each
->next_unit
= file
->all_comp_units
;
4846 file
->all_comp_units
= each
;
4848 file
->info_ptr
+= length
;
4853 /* Don't trust any of the DWARF info after a corrupted length or
4855 file
->info_ptr
= info_ptr_end
;
4859 /* Hash function for an asymbol. */
4862 hash_asymbol (const void *sym
)
4864 const asymbol
*asym
= sym
;
4865 return htab_hash_string (asym
->name
);
4868 /* Equality function for asymbols. */
4871 eq_asymbol (const void *a
, const void *b
)
4873 const asymbol
*sa
= a
;
4874 const asymbol
*sb
= b
;
4875 return strcmp (sa
->name
, sb
->name
) == 0;
4878 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
4879 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
4880 symbol in SYMBOLS and return the difference between the low_pc and
4881 the symbol's address. Returns 0 if no suitable symbol could be found. */
4884 _bfd_dwarf2_find_symbol_bias (asymbol
** symbols
, void ** pinfo
)
4886 struct dwarf2_debug
*stash
;
4887 struct comp_unit
* unit
;
4889 bfd_signed_vma result
= 0;
4892 stash
= (struct dwarf2_debug
*) *pinfo
;
4894 if (stash
== NULL
|| symbols
== NULL
)
4897 sym_hash
= htab_create_alloc (10, hash_asymbol
, eq_asymbol
,
4898 NULL
, xcalloc
, free
);
4899 for (psym
= symbols
; * psym
!= NULL
; psym
++)
4901 asymbol
* sym
= * psym
;
4903 if (sym
->flags
& BSF_FUNCTION
&& sym
->section
!= NULL
)
4905 void **slot
= htab_find_slot (sym_hash
, sym
, INSERT
);
4910 for (unit
= stash
->f
.all_comp_units
; unit
; unit
= unit
->next_unit
)
4912 struct funcinfo
* func
;
4914 comp_unit_maybe_decode_line_info (unit
);
4916 for (func
= unit
->function_table
; func
!= NULL
; func
= func
->prev_func
)
4917 if (func
->name
&& func
->arange
.low
)
4919 asymbol search
, *sym
;
4921 /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
4923 search
.name
= func
->name
;
4924 sym
= htab_find (sym_hash
, &search
);
4927 result
= ((bfd_signed_vma
) func
->arange
.low
) -
4928 ((bfd_signed_vma
) (sym
->value
+ sym
->section
->vma
));
4935 htab_delete (sym_hash
);
4939 /* Find the source code location of SYMBOL. If SYMBOL is NULL
4940 then find the nearest source code location corresponding to
4941 the address SECTION + OFFSET.
4942 Returns 1 if the line is found without error and fills in
4943 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
4944 NULL the FUNCTIONNAME_PTR is also filled in.
4945 Returns 2 if partial information from _bfd_elf_find_function is
4946 returned (function and maybe file) by looking at symbols. DWARF2
4947 info is present but not regarding the requested code location.
4948 Returns 0 otherwise.
4949 SYMBOLS contains the symbol table for ABFD.
4950 DEBUG_SECTIONS contains the name of the dwarf debug sections. */
4953 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
4958 const char **filename_ptr
,
4959 const char **functionname_ptr
,
4960 unsigned int *linenumber_ptr
,
4961 unsigned int *discriminator_ptr
,
4962 const struct dwarf_debug_section
*debug_sections
,
4965 /* Read each compilation unit from the section .debug_info, and check
4966 to see if it contains the address we are searching for. If yes,
4967 lookup the address, and return the line number info. If no, go
4968 on to the next compilation unit.
4970 We keep a list of all the previously read compilation units, and
4971 a pointer to the next un-read compilation unit. Check the
4972 previously read units before reading more. */
4973 struct dwarf2_debug
*stash
;
4974 /* What address are we looking for? */
4976 struct comp_unit
* each
;
4977 struct funcinfo
*function
= NULL
;
4981 *filename_ptr
= NULL
;
4982 if (functionname_ptr
!= NULL
)
4983 *functionname_ptr
= NULL
;
4984 *linenumber_ptr
= 0;
4985 if (discriminator_ptr
)
4986 *discriminator_ptr
= 0;
4988 if (! _bfd_dwarf2_slurp_debug_info (abfd
, NULL
, debug_sections
,
4990 (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0))
4993 stash
= (struct dwarf2_debug
*) *pinfo
;
4995 do_line
= symbol
!= NULL
;
4998 BFD_ASSERT (section
== NULL
&& offset
== 0 && functionname_ptr
== NULL
);
4999 section
= bfd_asymbol_section (symbol
);
5000 addr
= symbol
->value
;
5004 BFD_ASSERT (section
!= NULL
&& functionname_ptr
!= NULL
);
5007 /* If we have no SYMBOL but the section we're looking at is not a
5008 code section, then take a look through the list of symbols to see
5009 if we have a symbol at the address we're looking for. If we do
5010 then use this to look up line information. This will allow us to
5011 give file and line results for data symbols. We exclude code
5012 symbols here, if we look up a function symbol and then look up the
5013 line information we'll actually return the line number for the
5014 opening '{' rather than the function definition line. This is
5015 because looking up by symbol uses the line table, in which the
5016 first line for a function is usually the opening '{', while
5017 looking up the function by section + offset uses the
5018 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
5019 which will be the line of the function name. */
5020 if (symbols
!= NULL
&& (section
->flags
& SEC_CODE
) == 0)
5024 for (tmp
= symbols
; (*tmp
) != NULL
; ++tmp
)
5025 if ((*tmp
)->the_bfd
== abfd
5026 && (*tmp
)->section
== section
5027 && (*tmp
)->value
== offset
5028 && ((*tmp
)->flags
& BSF_SECTION_SYM
) == 0)
5032 /* For local symbols, keep going in the hope we find a
5034 if ((symbol
->flags
& BSF_GLOBAL
) != 0)
5040 if (section
->output_section
)
5041 addr
+= section
->output_section
->vma
+ section
->output_offset
;
5043 addr
+= section
->vma
;
5045 /* A null info_ptr indicates that there is no dwarf2 info
5046 (or that an error occured while setting up the stash). */
5047 if (! stash
->f
.info_ptr
)
5050 stash
->inliner_chain
= NULL
;
5052 /* Check the previously read comp. units first. */
5055 /* The info hash tables use quite a bit of memory. We may not want to
5056 always use them. We use some heuristics to decide if and when to
5058 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
5059 stash_maybe_enable_info_hash_tables (abfd
, stash
);
5061 /* Keep info hash table up to date if they are available. Note that we
5062 may disable the hash tables if there is any error duing update. */
5063 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
5064 stash_maybe_update_info_hash_tables (stash
);
5066 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
5068 found
= stash_find_line_fast (stash
, symbol
, addr
, filename_ptr
,
5075 /* Check the previously read comp. units first. */
5076 for (each
= stash
->f
.all_comp_units
; each
; each
= each
->next_unit
)
5077 if ((symbol
->flags
& BSF_FUNCTION
) == 0
5078 || each
->arange
.high
== 0
5079 || comp_unit_contains_address (each
, addr
))
5081 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
5090 bfd_vma min_range
= (bfd_vma
) -1;
5091 const char * local_filename
= NULL
;
5092 struct funcinfo
*local_function
= NULL
;
5093 unsigned int local_linenumber
= 0;
5094 unsigned int local_discriminator
= 0;
5096 for (each
= stash
->f
.all_comp_units
; each
; each
= each
->next_unit
)
5098 bfd_vma range
= (bfd_vma
) -1;
5100 found
= ((each
->arange
.high
== 0
5101 || comp_unit_contains_address (each
, addr
))
5102 && (range
= (comp_unit_find_nearest_line
5103 (each
, addr
, &local_filename
,
5104 &local_function
, &local_linenumber
,
5105 &local_discriminator
))) != 0);
5108 /* PRs 15935 15994: Bogus debug information may have provided us
5109 with an erroneous match. We attempt to counter this by
5110 selecting the match that has the smallest address range
5111 associated with it. (We are assuming that corrupt debug info
5112 will tend to result in extra large address ranges rather than
5113 extra small ranges).
5115 This does mean that we scan through all of the CUs associated
5116 with the bfd each time this function is called. But this does
5117 have the benefit of producing consistent results every time the
5118 function is called. */
5119 if (range
<= min_range
)
5121 if (filename_ptr
&& local_filename
)
5122 * filename_ptr
= local_filename
;
5124 function
= local_function
;
5125 if (discriminator_ptr
&& local_discriminator
)
5126 * discriminator_ptr
= local_discriminator
;
5127 if (local_linenumber
)
5128 * linenumber_ptr
= local_linenumber
;
5134 if (* linenumber_ptr
)
5141 /* Read each remaining comp. units checking each as they are read. */
5142 while ((each
= stash_comp_unit (stash
, &stash
->f
)) != NULL
)
5144 /* DW_AT_low_pc and DW_AT_high_pc are optional for
5145 compilation units. If we don't have them (i.e.,
5146 unit->high == 0), we need to consult the line info table
5147 to see if a compilation unit contains the given
5150 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
5151 || each
->arange
.high
== 0
5152 || comp_unit_contains_address (each
, addr
))
5153 && comp_unit_find_line (each
, symbol
, addr
,
5154 filename_ptr
, linenumber_ptr
));
5156 found
= ((each
->arange
.high
== 0
5157 || comp_unit_contains_address (each
, addr
))
5158 && comp_unit_find_nearest_line (each
, addr
,
5162 discriminator_ptr
) != 0);
5169 if (functionname_ptr
&& function
&& function
->is_linkage
)
5170 *functionname_ptr
= function
->name
;
5171 else if (functionname_ptr
5172 && (!*functionname_ptr
5173 || (function
&& !function
->is_linkage
)))
5176 asymbol
**syms
= symbols
;
5177 asection
*sec
= section
;
5179 _bfd_dwarf2_stash_syms (stash
, abfd
, &sec
, &syms
);
5180 fun
= _bfd_elf_find_function (abfd
, syms
, sec
, offset
,
5181 *filename_ptr
? NULL
: filename_ptr
,
5184 if (!found
&& fun
!= NULL
)
5187 if (function
&& !function
->is_linkage
)
5191 sec_vma
= section
->vma
;
5192 if (section
->output_section
!= NULL
)
5193 sec_vma
= section
->output_section
->vma
+ section
->output_offset
;
5195 && fun
->value
+ sec_vma
== function
->arange
.low
)
5196 function
->name
= *functionname_ptr
;
5197 /* Even if we didn't find a linkage name, say that we have
5198 to stop a repeated search of symbols. */
5199 function
->is_linkage
= true;
5203 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
5204 unset_sections (stash
);
5210 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
5211 const char **filename_ptr
,
5212 const char **functionname_ptr
,
5213 unsigned int *linenumber_ptr
,
5216 struct dwarf2_debug
*stash
;
5218 stash
= (struct dwarf2_debug
*) *pinfo
;
5221 struct funcinfo
*func
= stash
->inliner_chain
;
5223 if (func
&& func
->caller_func
)
5225 *filename_ptr
= func
->caller_file
;
5226 *functionname_ptr
= func
->caller_func
->name
;
5227 *linenumber_ptr
= func
->caller_line
;
5228 stash
->inliner_chain
= func
->caller_func
;
5237 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
, void **pinfo
)
5239 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
5240 struct comp_unit
*each
;
5241 struct dwarf2_debug_file
*file
;
5243 if (abfd
== NULL
|| stash
== NULL
)
5246 if (stash
->varinfo_hash_table
)
5247 bfd_hash_table_free (&stash
->varinfo_hash_table
->base
);
5248 if (stash
->funcinfo_hash_table
)
5249 bfd_hash_table_free (&stash
->funcinfo_hash_table
->base
);
5254 for (each
= file
->all_comp_units
; each
; each
= each
->next_unit
)
5256 struct funcinfo
*function_table
= each
->function_table
;
5257 struct varinfo
*variable_table
= each
->variable_table
;
5259 if (each
->line_table
&& each
->line_table
!= file
->line_table
)
5261 free (each
->line_table
->files
);
5262 free (each
->line_table
->dirs
);
5265 free (each
->lookup_funcinfo_table
);
5266 each
->lookup_funcinfo_table
= NULL
;
5268 while (function_table
)
5270 free (function_table
->file
);
5271 function_table
->file
= NULL
;
5272 free (function_table
->caller_file
);
5273 function_table
->caller_file
= NULL
;
5274 function_table
= function_table
->prev_func
;
5277 while (variable_table
)
5279 free (variable_table
->file
);
5280 variable_table
->file
= NULL
;
5281 variable_table
= variable_table
->prev_var
;
5285 if (file
->line_table
)
5287 free (file
->line_table
->files
);
5288 free (file
->line_table
->dirs
);
5290 htab_delete (file
->abbrev_offsets
);
5292 free (file
->dwarf_line_str_buffer
);
5293 free (file
->dwarf_str_buffer
);
5294 free (file
->dwarf_ranges_buffer
);
5295 free (file
->dwarf_line_buffer
);
5296 free (file
->dwarf_abbrev_buffer
);
5297 free (file
->dwarf_info_buffer
);
5298 if (file
== &stash
->alt
)
5302 free (stash
->sec_vma
);
5303 free (stash
->adjusted_sections
);
5304 if (stash
->close_on_cleanup
)
5305 bfd_close (stash
->f
.bfd_ptr
);
5306 if (stash
->alt
.bfd_ptr
)
5307 bfd_close (stash
->alt
.bfd_ptr
);
5310 /* Find the function to a particular section and offset,
5311 for error reporting. */
5314 _bfd_elf_find_function (bfd
*abfd
,
5318 const char **filename_ptr
,
5319 const char **functionname_ptr
)
5321 struct elf_find_function_cache
5323 asection
*last_section
;
5325 const char *filename
;
5326 bfd_size_type func_size
;
5329 if (symbols
== NULL
)
5332 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
5335 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
5338 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
5339 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
5343 if (cache
->last_section
!= section
5344 || cache
->func
== NULL
5345 || offset
< cache
->func
->value
5346 || offset
>= cache
->func
->value
+ cache
->func_size
)
5351 /* ??? Given multiple file symbols, it is impossible to reliably
5352 choose the right file name for global symbols. File symbols are
5353 local symbols, and thus all file symbols must sort before any
5354 global symbols. The ELF spec may be interpreted to say that a
5355 file symbol must sort before other local symbols, but currently
5356 ld -r doesn't do this. So, for ld -r output, it is possible to
5357 make a better choice of file name for local symbols by ignoring
5358 file symbols appearing after a given local symbol. */
5359 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
5360 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5364 state
= nothing_seen
;
5365 cache
->filename
= NULL
;
5367 cache
->func_size
= 0;
5368 cache
->last_section
= section
;
5370 for (p
= symbols
; *p
!= NULL
; p
++)
5376 if ((sym
->flags
& BSF_FILE
) != 0)
5379 if (state
== symbol_seen
)
5380 state
= file_after_symbol_seen
;
5384 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
5386 && code_off
<= offset
5387 && (code_off
> low_func
5388 || (code_off
== low_func
5389 && size
> cache
->func_size
)))
5392 cache
->func_size
= size
;
5393 cache
->filename
= NULL
;
5394 low_func
= code_off
;
5396 && ((sym
->flags
& BSF_LOCAL
) != 0
5397 || state
!= file_after_symbol_seen
))
5398 cache
->filename
= bfd_asymbol_name (file
);
5400 if (state
== nothing_seen
)
5401 state
= symbol_seen
;
5405 if (cache
->func
== NULL
)
5409 *filename_ptr
= cache
->filename
;
5410 if (functionname_ptr
)
5411 *functionname_ptr
= bfd_asymbol_name (cache
->func
);