2 Copyright (C) 1994-2022 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
);
560 /* PR 28834: A compressed debug section could well decompress to a size
561 larger than the file, so we choose an arbitrary modifier of 10x in
562 the test below. If this ever turns out to be insufficient, it can
563 be changed by a future update. */
564 if (amt
>= filesize
* 10)
567 _bfd_error_handler (_("DWARF error: section %s is larger than 10x its filesize! (0x%lx vs 0x%lx)"),
568 section_name
, (long) amt
, (long) filesize
);
569 bfd_set_error (bfd_error_bad_value
);
573 /* Paranoia - alloc one extra so that we can make sure a string
574 section is NUL terminated. */
578 /* Paranoia - this should never happen. */
579 bfd_set_error (bfd_error_no_memory
);
582 contents
= (bfd_byte
*) bfd_malloc (amt
);
583 if (contents
== NULL
)
586 ? !bfd_simple_get_relocated_section_contents (abfd
, msec
, contents
,
588 : !bfd_get_section_contents (abfd
, msec
, contents
, 0, *section_size
))
593 contents
[*section_size
] = 0;
594 *section_buffer
= contents
;
597 /* It is possible to get a bad value for the offset into the section
598 that the client wants. Validate it here to avoid trouble later. */
599 if (offset
!= 0 && offset
>= *section_size
)
601 /* xgettext: c-format */
602 _bfd_error_handler (_("DWARF error: offset (%" PRIu64
")"
603 " greater than or equal to %s size (%" PRIu64
")"),
604 (uint64_t) offset
, section_name
,
605 (uint64_t) *section_size
);
606 bfd_set_error (bfd_error_bad_value
);
613 /* Read dwarf information from a buffer. */
615 static inline uint64_t
616 read_n_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
, int n
)
618 bfd_byte
*buf
= *ptr
;
625 return bfd_get (n
* 8, abfd
, buf
);
629 read_1_byte (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
631 return read_n_bytes (abfd
, ptr
, end
, 1);
635 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
**ptr
, bfd_byte
*end
)
637 bfd_byte
*buf
= *ptr
;
644 return bfd_get_signed_8 (abfd
, buf
);
648 read_2_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
650 return read_n_bytes (abfd
, ptr
, end
, 2);
654 read_3_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
656 unsigned int val
= read_1_byte (abfd
, ptr
, end
);
658 val
|= read_1_byte (abfd
, ptr
, end
);
660 val
|= read_1_byte (abfd
, ptr
, end
);
661 if (bfd_little_endian (abfd
))
662 val
= (((val
>> 16) & 0xff)
664 | ((val
& 0xff) << 16));
669 read_4_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
671 return read_n_bytes (abfd
, ptr
, end
, 4);
675 read_8_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
677 return read_n_bytes (abfd
, ptr
, end
, 8);
680 static struct dwarf_block
*
681 read_blk (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
, size_t size
)
683 bfd_byte
*buf
= *ptr
;
684 struct dwarf_block
*block
;
686 block
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (*block
));
690 if (size
> (size_t) (end
- buf
))
705 /* Scans a NUL terminated string starting at *PTR, returning a pointer to it.
706 Bytes at or beyond BUF_END will not be read. Returns NULL if the
707 terminator is not found or if the string is empty. *PTR is
708 incremented over the bytes scanned, including the terminator. */
711 read_string (bfd_byte
**ptr
,
714 bfd_byte
*buf
= *ptr
;
717 while (buf
< buf_end
)
730 /* Reads an offset from *PTR and then locates the string at this offset
731 inside the debug string section. Returns a pointer to the string.
732 Increments *PTR by the number of bytes read for the offset. This
733 value is set even if the function fails. Bytes at or beyond
734 BUF_END will not be read. Returns NULL if there was a problem, or
735 if the string is empty. Does not check for NUL termination of the
739 read_indirect_string (struct comp_unit
*unit
,
744 struct dwarf2_debug
*stash
= unit
->stash
;
745 struct dwarf2_debug_file
*file
= unit
->file
;
748 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
754 if (unit
->offset_size
== 4)
755 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
757 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
759 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
761 &file
->dwarf_str_buffer
, &file
->dwarf_str_size
))
764 str
= (char *) file
->dwarf_str_buffer
+ offset
;
770 /* Like read_indirect_string but from .debug_line_str section. */
773 read_indirect_line_string (struct comp_unit
*unit
,
778 struct dwarf2_debug
*stash
= unit
->stash
;
779 struct dwarf2_debug_file
*file
= unit
->file
;
782 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
788 if (unit
->offset_size
== 4)
789 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
791 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
793 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_line_str
],
795 &file
->dwarf_line_str_buffer
,
796 &file
->dwarf_line_str_size
))
799 str
= (char *) file
->dwarf_line_str_buffer
+ offset
;
805 /* Like read_indirect_string but uses a .debug_str located in
806 an alternate file pointed to by the .gnu_debugaltlink section.
807 Used to impement DW_FORM_GNU_strp_alt. */
810 read_alt_indirect_string (struct comp_unit
*unit
,
815 struct dwarf2_debug
*stash
= unit
->stash
;
818 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
824 if (unit
->offset_size
== 4)
825 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
827 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
829 if (stash
->alt
.bfd_ptr
== NULL
)
832 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
834 if (debug_filename
== NULL
)
837 debug_bfd
= bfd_openr (debug_filename
, NULL
);
838 free (debug_filename
);
839 if (debug_bfd
== NULL
)
840 /* FIXME: Should we report our failure to follow the debuglink ? */
843 if (!bfd_check_format (debug_bfd
, bfd_object
))
845 bfd_close (debug_bfd
);
848 stash
->alt
.bfd_ptr
= debug_bfd
;
851 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
852 stash
->debug_sections
+ debug_str_alt
,
853 stash
->alt
.syms
, offset
,
854 &stash
->alt
.dwarf_str_buffer
,
855 &stash
->alt
.dwarf_str_size
))
858 str
= (char *) stash
->alt
.dwarf_str_buffer
+ offset
;
865 /* Resolve an alternate reference from UNIT at OFFSET.
866 Returns a pointer into the loaded alternate CU upon success
867 or NULL upon failure. */
870 read_alt_indirect_ref (struct comp_unit
* unit
,
873 struct dwarf2_debug
*stash
= unit
->stash
;
875 if (stash
->alt
.bfd_ptr
== NULL
)
878 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
880 if (debug_filename
== NULL
)
883 debug_bfd
= bfd_openr (debug_filename
, NULL
);
884 free (debug_filename
);
885 if (debug_bfd
== NULL
)
886 /* FIXME: Should we report our failure to follow the debuglink ? */
889 if (!bfd_check_format (debug_bfd
, bfd_object
))
891 bfd_close (debug_bfd
);
894 stash
->alt
.bfd_ptr
= debug_bfd
;
897 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
898 stash
->debug_sections
+ debug_info_alt
,
899 stash
->alt
.syms
, offset
,
900 &stash
->alt
.dwarf_info_buffer
,
901 &stash
->alt
.dwarf_info_size
))
904 return stash
->alt
.dwarf_info_buffer
+ offset
;
908 read_address (struct comp_unit
*unit
, bfd_byte
**ptr
, bfd_byte
*buf_end
)
910 bfd_byte
*buf
= *ptr
;
913 if (bfd_get_flavour (unit
->abfd
) == bfd_target_elf_flavour
)
914 signed_vma
= get_elf_backend_data (unit
->abfd
)->sign_extend_vma
;
916 if (unit
->addr_size
> (size_t) (buf_end
- buf
))
922 *ptr
= buf
+ unit
->addr_size
;
925 switch (unit
->addr_size
)
928 return bfd_get_signed_64 (unit
->abfd
, buf
);
930 return bfd_get_signed_32 (unit
->abfd
, buf
);
932 return bfd_get_signed_16 (unit
->abfd
, buf
);
939 switch (unit
->addr_size
)
942 return bfd_get_64 (unit
->abfd
, buf
);
944 return bfd_get_32 (unit
->abfd
, buf
);
946 return bfd_get_16 (unit
->abfd
, buf
);
953 /* Lookup an abbrev_info structure in the abbrev hash table. */
955 static struct abbrev_info
*
956 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
958 unsigned int hash_number
;
959 struct abbrev_info
*abbrev
;
961 hash_number
= number
% ABBREV_HASH_SIZE
;
962 abbrev
= abbrevs
[hash_number
];
966 if (abbrev
->number
== number
)
969 abbrev
= abbrev
->next
;
975 /* We keep a hash table to map .debug_abbrev section offsets to the
976 array of abbrevs, so that compilation units using the same set of
977 abbrevs do not waste memory. */
979 struct abbrev_offset_entry
982 struct abbrev_info
**abbrevs
;
986 hash_abbrev (const void *p
)
988 const struct abbrev_offset_entry
*ent
= p
;
989 return htab_hash_pointer ((void *) ent
->offset
);
993 eq_abbrev (const void *pa
, const void *pb
)
995 const struct abbrev_offset_entry
*a
= pa
;
996 const struct abbrev_offset_entry
*b
= pb
;
997 return a
->offset
== b
->offset
;
1001 del_abbrev (void *p
)
1003 struct abbrev_offset_entry
*ent
= p
;
1004 struct abbrev_info
**abbrevs
= ent
->abbrevs
;
1007 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1009 struct abbrev_info
*abbrev
= abbrevs
[i
];
1013 free (abbrev
->attrs
);
1014 abbrev
= abbrev
->next
;
1020 /* In DWARF version 2, the description of the debugging information is
1021 stored in a separate .debug_abbrev section. Before we read any
1022 dies from a section we read in all abbreviations and install them
1025 static struct abbrev_info
**
1026 read_abbrevs (bfd
*abfd
, bfd_uint64_t offset
, struct dwarf2_debug
*stash
,
1027 struct dwarf2_debug_file
*file
)
1029 struct abbrev_info
**abbrevs
;
1030 bfd_byte
*abbrev_ptr
;
1031 bfd_byte
*abbrev_end
;
1032 struct abbrev_info
*cur_abbrev
;
1033 unsigned int abbrev_number
, abbrev_name
;
1034 unsigned int abbrev_form
, hash_number
;
1037 struct abbrev_offset_entry ent
= { offset
, NULL
};
1039 if (ent
.offset
!= offset
)
1042 slot
= htab_find_slot (file
->abbrev_offsets
, &ent
, INSERT
);
1046 return ((struct abbrev_offset_entry
*) (*slot
))->abbrevs
;
1048 if (! read_section (abfd
, &stash
->debug_sections
[debug_abbrev
],
1050 &file
->dwarf_abbrev_buffer
,
1051 &file
->dwarf_abbrev_size
))
1054 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
1055 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
1056 if (abbrevs
== NULL
)
1059 abbrev_ptr
= file
->dwarf_abbrev_buffer
+ offset
;
1060 abbrev_end
= file
->dwarf_abbrev_buffer
+ file
->dwarf_abbrev_size
;
1061 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1064 /* Loop until we reach an abbrev number of 0. */
1065 while (abbrev_number
)
1067 amt
= sizeof (struct abbrev_info
);
1068 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
1069 if (cur_abbrev
== NULL
)
1072 /* Read in abbrev header. */
1073 cur_abbrev
->number
= abbrev_number
;
1074 cur_abbrev
->tag
= (enum dwarf_tag
)
1075 _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1077 cur_abbrev
->has_children
= read_1_byte (abfd
, &abbrev_ptr
, abbrev_end
);
1079 /* Now read in declarations. */
1082 /* Initialize it just to avoid a GCC false warning. */
1083 bfd_vma implicit_const
= -1;
1085 abbrev_name
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1087 abbrev_form
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1089 if (abbrev_form
== DW_FORM_implicit_const
)
1090 implicit_const
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1092 if (abbrev_name
== 0)
1095 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
1097 struct attr_abbrev
*tmp
;
1099 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
1100 amt
*= sizeof (struct attr_abbrev
);
1101 tmp
= (struct attr_abbrev
*) bfd_realloc (cur_abbrev
->attrs
, amt
);
1104 cur_abbrev
->attrs
= tmp
;
1107 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
1108 = (enum dwarf_attribute
) abbrev_name
;
1109 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].form
1110 = (enum dwarf_form
) abbrev_form
;
1111 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].implicit_const
1113 ++cur_abbrev
->num_attrs
;
1116 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
1117 cur_abbrev
->next
= abbrevs
[hash_number
];
1118 abbrevs
[hash_number
] = cur_abbrev
;
1120 /* Get next abbreviation.
1121 Under Irix6 the abbreviations for a compilation unit are not
1122 always properly terminated with an abbrev number of 0.
1123 Exit loop if we encounter an abbreviation which we have
1124 already read (which means we are about to read the abbreviations
1125 for the next compile unit) or if the end of the abbreviation
1126 table is reached. */
1127 if ((size_t) (abbrev_ptr
- file
->dwarf_abbrev_buffer
)
1128 >= file
->dwarf_abbrev_size
)
1130 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1132 if (lookup_abbrev (abbrev_number
, abbrevs
) != NULL
)
1136 *slot
= bfd_malloc (sizeof ent
);
1139 ent
.abbrevs
= abbrevs
;
1140 memcpy (*slot
, &ent
, sizeof ent
);
1144 if (abbrevs
!= NULL
)
1148 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1150 struct abbrev_info
*abbrev
= abbrevs
[i
];
1154 free (abbrev
->attrs
);
1155 abbrev
= abbrev
->next
;
1163 /* Returns true if the form is one which has a string value. */
1166 is_str_form (const struct attribute
*attr
)
1170 case DW_FORM_string
:
1177 case DW_FORM_line_strp
:
1178 case DW_FORM_GNU_strp_alt
:
1186 /* Returns true if the form is one which has an integer value. */
1189 is_int_form (const struct attribute
*attr
)
1201 case DW_FORM_ref_addr
:
1206 case DW_FORM_ref_udata
:
1207 case DW_FORM_sec_offset
:
1208 case DW_FORM_flag_present
:
1209 case DW_FORM_ref_sig8
:
1211 case DW_FORM_implicit_const
:
1212 case DW_FORM_addrx1
:
1213 case DW_FORM_addrx2
:
1214 case DW_FORM_addrx3
:
1215 case DW_FORM_addrx4
:
1216 case DW_FORM_GNU_ref_alt
:
1225 read_indexed_string (bfd_uint64_t idx ATTRIBUTE_UNUSED
,
1226 struct comp_unit
* unit ATTRIBUTE_UNUSED
)
1228 /* FIXME: Add support for indexed strings. */
1229 return "<indexed strings not yet supported>";
1232 /* Read and fill in the value of attribute ATTR as described by FORM.
1233 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1234 Returns an updated INFO_PTR taking into account the amount of data read. */
1237 read_attribute_value (struct attribute
* attr
,
1239 bfd_vma implicit_const
,
1240 struct comp_unit
* unit
,
1241 bfd_byte
* info_ptr
,
1242 bfd_byte
* info_ptr_end
)
1244 bfd
*abfd
= unit
->abfd
;
1247 if (info_ptr
>= info_ptr_end
&& form
!= DW_FORM_flag_present
)
1249 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1250 bfd_set_error (bfd_error_bad_value
);
1254 attr
->form
= (enum dwarf_form
) form
;
1258 case DW_FORM_flag_present
:
1261 case DW_FORM_ref_addr
:
1262 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1264 if (unit
->version
>= 3)
1266 if (unit
->offset_size
== 4)
1267 attr
->u
.val
= read_4_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1269 attr
->u
.val
= read_8_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1274 attr
->u
.val
= read_address (unit
, &info_ptr
, info_ptr_end
);
1276 case DW_FORM_GNU_ref_alt
:
1277 case DW_FORM_sec_offset
:
1278 if (unit
->offset_size
== 4)
1279 attr
->u
.val
= read_4_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1281 attr
->u
.val
= read_8_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1283 case DW_FORM_block2
:
1284 amt
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1285 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1286 if (attr
->u
.blk
== NULL
)
1289 case DW_FORM_block4
:
1290 amt
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1291 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1292 if (attr
->u
.blk
== NULL
)
1298 case DW_FORM_addrx1
:
1299 attr
->u
.val
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1302 case DW_FORM_addrx2
:
1304 attr
->u
.val
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1306 case DW_FORM_addrx3
:
1307 attr
->u
.val
= read_3_bytes (abfd
, &info_ptr
, info_ptr_end
);
1311 case DW_FORM_addrx4
:
1312 attr
->u
.val
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1316 case DW_FORM_ref_sig8
:
1317 attr
->u
.val
= read_8_bytes (abfd
, &info_ptr
, info_ptr_end
);
1319 case DW_FORM_string
:
1320 attr
->u
.str
= read_string (&info_ptr
, info_ptr_end
);
1323 attr
->u
.str
= read_indirect_string (unit
, &info_ptr
, info_ptr_end
);
1325 case DW_FORM_line_strp
:
1326 attr
->u
.str
= read_indirect_line_string (unit
, &info_ptr
, info_ptr_end
);
1328 case DW_FORM_GNU_strp_alt
:
1329 attr
->u
.str
= read_alt_indirect_string (unit
, &info_ptr
, info_ptr_end
);
1332 attr
->u
.val
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1333 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1336 attr
->u
.val
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1337 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1340 attr
->u
.val
= read_3_bytes (abfd
, &info_ptr
, info_ptr_end
);
1341 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1344 attr
->u
.val
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1345 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1348 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1349 false, info_ptr_end
);
1350 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1352 case DW_FORM_exprloc
:
1354 amt
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1355 false, info_ptr_end
);
1356 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1357 if (attr
->u
.blk
== NULL
)
1360 case DW_FORM_block1
:
1361 amt
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1362 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1363 if (attr
->u
.blk
== NULL
)
1367 attr
->u
.sval
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1368 true, info_ptr_end
);
1370 case DW_FORM_ref_udata
:
1373 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1374 false, info_ptr_end
);
1376 case DW_FORM_indirect
:
1377 form
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1378 false, info_ptr_end
);
1379 if (form
== DW_FORM_implicit_const
)
1380 implicit_const
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1381 true, info_ptr_end
);
1382 info_ptr
= read_attribute_value (attr
, form
, implicit_const
, unit
,
1383 info_ptr
, info_ptr_end
);
1385 case DW_FORM_implicit_const
:
1386 attr
->form
= DW_FORM_sdata
;
1387 attr
->u
.sval
= implicit_const
;
1389 case DW_FORM_data16
:
1390 /* This is really a "constant", but there is no way to store that
1391 so pretend it is a 16 byte block instead. */
1392 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, 16);
1393 if (attr
->u
.blk
== NULL
)
1398 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1400 bfd_set_error (bfd_error_bad_value
);
1406 /* Read an attribute described by an abbreviated attribute. */
1409 read_attribute (struct attribute
* attr
,
1410 struct attr_abbrev
* abbrev
,
1411 struct comp_unit
* unit
,
1412 bfd_byte
* info_ptr
,
1413 bfd_byte
* info_ptr_end
)
1415 attr
->name
= abbrev
->name
;
1416 info_ptr
= read_attribute_value (attr
, abbrev
->form
, abbrev
->implicit_const
,
1417 unit
, info_ptr
, info_ptr_end
);
1421 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1425 non_mangled (int lang
)
1435 case DW_LANG_Cobol74
:
1436 case DW_LANG_Cobol85
:
1437 case DW_LANG_Fortran77
:
1438 case DW_LANG_Pascal83
:
1444 case DW_LANG_Mips_Assembler
:
1449 /* Source line information table routines. */
1451 #define FILE_ALLOC_CHUNK 5
1452 #define DIR_ALLOC_CHUNK 5
1456 struct line_info
* prev_line
;
1460 unsigned int column
;
1461 unsigned int discriminator
;
1462 unsigned char op_index
;
1463 unsigned char end_sequence
; /* End of (sequential) code sequence. */
1474 struct line_sequence
1477 struct line_sequence
* prev_sequence
;
1478 struct line_info
* last_line
; /* Largest VMA. */
1479 struct line_info
** line_info_lookup
;
1480 bfd_size_type num_lines
;
1483 struct line_info_table
1486 unsigned int num_files
;
1487 unsigned int num_dirs
;
1488 unsigned int num_sequences
;
1491 struct fileinfo
* files
;
1492 struct line_sequence
* sequences
;
1493 struct line_info
* lcl_head
; /* Local head; used in 'add_line_info'. */
1496 /* Remember some information about each function. If the function is
1497 inlined (DW_TAG_inlined_subroutine) it may have two additional
1498 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1499 source code location where this function was inlined. */
1503 /* Pointer to previous function in list of all functions. */
1504 struct funcinfo
* prev_func
;
1505 /* Pointer to function one scope higher. */
1506 struct funcinfo
* caller_func
;
1507 /* Source location file name where caller_func inlines this func. */
1509 /* Source location file name. */
1511 /* Source location line number where caller_func inlines this func. */
1513 /* Source location line number. */
1518 struct arange arange
;
1519 /* Where the symbol is defined. */
1521 /* The offset of the funcinfo from the start of the unit. */
1522 bfd_uint64_t unit_offset
;
1525 struct lookup_funcinfo
1527 /* Function information corresponding to this lookup table entry. */
1528 struct funcinfo
* funcinfo
;
1530 /* The lowest address for this specific function. */
1533 /* The highest address of this function before the lookup table is sorted.
1534 The highest address of all prior functions after the lookup table is
1535 sorted, which is used for binary search. */
1537 /* Index of this function, used to ensure qsort is stable. */
1543 /* Pointer to previous variable in list of all variables. */
1544 struct varinfo
*prev_var
;
1545 /* The offset of the varinfo from the start of the unit. */
1546 bfd_uint64_t unit_offset
;
1547 /* Source location file name. */
1549 /* Source location line number. */
1551 /* The type of this variable. */
1553 /* The name of the variable, if it has one. */
1555 /* The address of the variable. */
1557 /* Where the symbol is defined. */
1559 /* Is this a stack variable? */
1563 /* Return TRUE if NEW_LINE should sort after LINE. */
1566 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
1568 return (new_line
->address
> line
->address
1569 || (new_line
->address
== line
->address
1570 && new_line
->op_index
> line
->op_index
));
1574 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1575 that the list is sorted. Note that the line_info list is sorted from
1576 highest to lowest VMA (with possible duplicates); that is,
1577 line_info->prev_line always accesses an equal or smaller VMA. */
1580 add_line_info (struct line_info_table
*table
,
1582 unsigned char op_index
,
1585 unsigned int column
,
1586 unsigned int discriminator
,
1589 size_t amt
= sizeof (struct line_info
);
1590 struct line_sequence
* seq
= table
->sequences
;
1591 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
1596 /* Set member data of 'info'. */
1597 info
->prev_line
= NULL
;
1598 info
->address
= address
;
1599 info
->op_index
= op_index
;
1601 info
->column
= column
;
1602 info
->discriminator
= discriminator
;
1603 info
->end_sequence
= end_sequence
;
1605 if (filename
&& filename
[0])
1607 info
->filename
= (char *) bfd_alloc (table
->abfd
, strlen (filename
) + 1);
1608 if (info
->filename
== NULL
)
1610 strcpy (info
->filename
, filename
);
1613 info
->filename
= NULL
;
1615 /* Find the correct location for 'info'. Normally we will receive
1616 new line_info data 1) in order and 2) with increasing VMAs.
1617 However some compilers break the rules (cf. decode_line_info) and
1618 so we include some heuristics for quickly finding the correct
1619 location for 'info'. In particular, these heuristics optimize for
1620 the common case in which the VMA sequence that we receive is a
1621 list of locally sorted VMAs such as
1622 p...z a...j (where a < j < p < z)
1624 Note: table->lcl_head is used to head an *actual* or *possible*
1625 sub-sequence within the list (such as a...j) that is not directly
1626 headed by table->last_line
1628 Note: we may receive duplicate entries from 'decode_line_info'. */
1631 && seq
->last_line
->address
== address
1632 && seq
->last_line
->op_index
== op_index
1633 && seq
->last_line
->end_sequence
== end_sequence
)
1635 /* We only keep the last entry with the same address and end
1636 sequence. See PR ld/4986. */
1637 if (table
->lcl_head
== seq
->last_line
)
1638 table
->lcl_head
= info
;
1639 info
->prev_line
= seq
->last_line
->prev_line
;
1640 seq
->last_line
= info
;
1642 else if (!seq
|| seq
->last_line
->end_sequence
)
1644 /* Start a new line sequence. */
1645 amt
= sizeof (struct line_sequence
);
1646 seq
= (struct line_sequence
*) bfd_malloc (amt
);
1649 seq
->low_pc
= address
;
1650 seq
->prev_sequence
= table
->sequences
;
1651 seq
->last_line
= info
;
1652 table
->lcl_head
= info
;
1653 table
->sequences
= seq
;
1654 table
->num_sequences
++;
1656 else if (info
->end_sequence
1657 || new_line_sorts_after (info
, seq
->last_line
))
1659 /* Normal case: add 'info' to the beginning of the current sequence. */
1660 info
->prev_line
= seq
->last_line
;
1661 seq
->last_line
= info
;
1663 /* lcl_head: initialize to head a *possible* sequence at the end. */
1664 if (!table
->lcl_head
)
1665 table
->lcl_head
= info
;
1667 else if (!new_line_sorts_after (info
, table
->lcl_head
)
1668 && (!table
->lcl_head
->prev_line
1669 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
1671 /* Abnormal but easy: lcl_head is the head of 'info'. */
1672 info
->prev_line
= table
->lcl_head
->prev_line
;
1673 table
->lcl_head
->prev_line
= info
;
1677 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1678 are valid heads for 'info'. Reset 'lcl_head'. */
1679 struct line_info
* li2
= seq
->last_line
; /* Always non-NULL. */
1680 struct line_info
* li1
= li2
->prev_line
;
1684 if (!new_line_sorts_after (info
, li2
)
1685 && new_line_sorts_after (info
, li1
))
1688 li2
= li1
; /* always non-NULL */
1689 li1
= li1
->prev_line
;
1691 table
->lcl_head
= li2
;
1692 info
->prev_line
= table
->lcl_head
->prev_line
;
1693 table
->lcl_head
->prev_line
= info
;
1694 if (address
< seq
->low_pc
)
1695 seq
->low_pc
= address
;
1700 /* Extract a fully qualified filename from a line info table.
1701 The returned string has been malloc'ed and it is the caller's
1702 responsibility to free it. */
1705 concat_filename (struct line_info_table
*table
, unsigned int file
)
1709 if (table
== NULL
|| file
- 1 >= table
->num_files
)
1711 /* FILE == 0 means unknown. */
1714 (_("DWARF error: mangled line number section (bad file number)"));
1715 return strdup ("<unknown>");
1718 filename
= table
->files
[file
- 1].name
;
1719 if (filename
== NULL
)
1720 return strdup ("<unknown>");
1722 if (!IS_ABSOLUTE_PATH (filename
))
1724 char *dir_name
= NULL
;
1725 char *subdir_name
= NULL
;
1729 if (table
->files
[file
- 1].dir
1730 /* PR 17512: file: 0317e960. */
1731 && table
->files
[file
- 1].dir
<= table
->num_dirs
1732 /* PR 17512: file: 7f3d2e4b. */
1733 && table
->dirs
!= NULL
)
1734 subdir_name
= table
->dirs
[table
->files
[file
- 1].dir
- 1];
1736 if (!subdir_name
|| !IS_ABSOLUTE_PATH (subdir_name
))
1737 dir_name
= table
->comp_dir
;
1741 dir_name
= subdir_name
;
1746 return strdup (filename
);
1748 len
= strlen (dir_name
) + strlen (filename
) + 2;
1752 len
+= strlen (subdir_name
) + 1;
1753 name
= (char *) bfd_malloc (len
);
1755 sprintf (name
, "%s/%s/%s", dir_name
, subdir_name
, filename
);
1759 name
= (char *) bfd_malloc (len
);
1761 sprintf (name
, "%s/%s", dir_name
, filename
);
1767 return strdup (filename
);
1771 arange_add (const struct comp_unit
*unit
, struct arange
*first_arange
,
1772 bfd_vma low_pc
, bfd_vma high_pc
)
1774 struct arange
*arange
;
1776 /* Ignore empty ranges. */
1777 if (low_pc
== high_pc
)
1780 /* If the first arange is empty, use it. */
1781 if (first_arange
->high
== 0)
1783 first_arange
->low
= low_pc
;
1784 first_arange
->high
= high_pc
;
1788 /* Next see if we can cheaply extend an existing range. */
1789 arange
= first_arange
;
1792 if (low_pc
== arange
->high
)
1794 arange
->high
= high_pc
;
1797 if (high_pc
== arange
->low
)
1799 arange
->low
= low_pc
;
1802 arange
= arange
->next
;
1806 /* Need to allocate a new arange and insert it into the arange list.
1807 Order isn't significant, so just insert after the first arange. */
1808 arange
= (struct arange
*) bfd_alloc (unit
->abfd
, sizeof (*arange
));
1811 arange
->low
= low_pc
;
1812 arange
->high
= high_pc
;
1813 arange
->next
= first_arange
->next
;
1814 first_arange
->next
= arange
;
1818 /* Compare function for line sequences. */
1821 compare_sequences (const void* a
, const void* b
)
1823 const struct line_sequence
* seq1
= a
;
1824 const struct line_sequence
* seq2
= b
;
1826 /* Sort by low_pc as the primary key. */
1827 if (seq1
->low_pc
< seq2
->low_pc
)
1829 if (seq1
->low_pc
> seq2
->low_pc
)
1832 /* If low_pc values are equal, sort in reverse order of
1833 high_pc, so that the largest region comes first. */
1834 if (seq1
->last_line
->address
< seq2
->last_line
->address
)
1836 if (seq1
->last_line
->address
> seq2
->last_line
->address
)
1839 if (seq1
->last_line
->op_index
< seq2
->last_line
->op_index
)
1841 if (seq1
->last_line
->op_index
> seq2
->last_line
->op_index
)
1844 /* num_lines is initially an index, to make the sort stable. */
1845 if (seq1
->num_lines
< seq2
->num_lines
)
1847 if (seq1
->num_lines
> seq2
->num_lines
)
1852 /* Construct the line information table for quick lookup. */
1855 build_line_info_table (struct line_info_table
* table
,
1856 struct line_sequence
* seq
)
1859 struct line_info
**line_info_lookup
;
1860 struct line_info
*each_line
;
1861 unsigned int num_lines
;
1862 unsigned int line_index
;
1864 if (seq
->line_info_lookup
!= NULL
)
1867 /* Count the number of line information entries. We could do this while
1868 scanning the debug information, but some entries may be added via
1869 lcl_head without having a sequence handy to increment the number of
1872 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
1875 seq
->num_lines
= num_lines
;
1879 /* Allocate space for the line information lookup table. */
1880 amt
= sizeof (struct line_info
*) * num_lines
;
1881 line_info_lookup
= (struct line_info
**) bfd_alloc (table
->abfd
, amt
);
1882 seq
->line_info_lookup
= line_info_lookup
;
1883 if (line_info_lookup
== NULL
)
1886 /* Create the line information lookup table. */
1887 line_index
= num_lines
;
1888 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
1889 line_info_lookup
[--line_index
] = each_line
;
1891 BFD_ASSERT (line_index
== 0);
1895 /* Sort the line sequences for quick lookup. */
1898 sort_line_sequences (struct line_info_table
* table
)
1901 struct line_sequence
*sequences
;
1902 struct line_sequence
*seq
;
1904 unsigned int num_sequences
= table
->num_sequences
;
1905 bfd_vma last_high_pc
;
1907 if (num_sequences
== 0)
1910 /* Allocate space for an array of sequences. */
1911 amt
= sizeof (struct line_sequence
) * num_sequences
;
1912 sequences
= (struct line_sequence
*) bfd_alloc (table
->abfd
, amt
);
1913 if (sequences
== NULL
)
1916 /* Copy the linked list into the array, freeing the original nodes. */
1917 seq
= table
->sequences
;
1918 for (n
= 0; n
< num_sequences
; n
++)
1920 struct line_sequence
* last_seq
= seq
;
1923 sequences
[n
].low_pc
= seq
->low_pc
;
1924 sequences
[n
].prev_sequence
= NULL
;
1925 sequences
[n
].last_line
= seq
->last_line
;
1926 sequences
[n
].line_info_lookup
= NULL
;
1927 sequences
[n
].num_lines
= n
;
1928 seq
= seq
->prev_sequence
;
1931 BFD_ASSERT (seq
== NULL
);
1933 qsort (sequences
, n
, sizeof (struct line_sequence
), compare_sequences
);
1935 /* Make the list binary-searchable by trimming overlapping entries
1936 and removing nested entries. */
1938 last_high_pc
= sequences
[0].last_line
->address
;
1939 for (n
= 1; n
< table
->num_sequences
; n
++)
1941 if (sequences
[n
].low_pc
< last_high_pc
)
1943 if (sequences
[n
].last_line
->address
<= last_high_pc
)
1944 /* Skip nested entries. */
1947 /* Trim overlapping entries. */
1948 sequences
[n
].low_pc
= last_high_pc
;
1950 last_high_pc
= sequences
[n
].last_line
->address
;
1951 if (n
> num_sequences
)
1953 /* Close up the gap. */
1954 sequences
[num_sequences
].low_pc
= sequences
[n
].low_pc
;
1955 sequences
[num_sequences
].last_line
= sequences
[n
].last_line
;
1960 table
->sequences
= sequences
;
1961 table
->num_sequences
= num_sequences
;
1965 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
1968 line_info_add_include_dir (struct line_info_table
*table
, char *cur_dir
)
1970 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1975 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1976 amt
*= sizeof (char *);
1978 tmp
= (char **) bfd_realloc (table
->dirs
, amt
);
1984 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1989 line_info_add_include_dir_stub (struct line_info_table
*table
, char *cur_dir
,
1990 unsigned int dir ATTRIBUTE_UNUSED
,
1991 unsigned int xtime ATTRIBUTE_UNUSED
,
1992 unsigned int size ATTRIBUTE_UNUSED
)
1994 return line_info_add_include_dir (table
, cur_dir
);
1997 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
2000 line_info_add_file_name (struct line_info_table
*table
, char *cur_file
,
2001 unsigned int dir
, unsigned int xtime
,
2004 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
2006 struct fileinfo
*tmp
;
2009 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
2010 amt
*= sizeof (struct fileinfo
);
2012 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
2018 table
->files
[table
->num_files
].name
= cur_file
;
2019 table
->files
[table
->num_files
].dir
= dir
;
2020 table
->files
[table
->num_files
].time
= xtime
;
2021 table
->files
[table
->num_files
].size
= size
;
2026 /* Read directory or file name entry format, starting with byte of
2027 format count entries, ULEB128 pairs of entry formats, ULEB128 of
2028 entries count and the entries themselves in the described entry
2032 read_formatted_entries (struct comp_unit
*unit
, bfd_byte
**bufp
,
2033 bfd_byte
*buf_end
, struct line_info_table
*table
,
2034 bool (*callback
) (struct line_info_table
*table
,
2040 bfd
*abfd
= unit
->abfd
;
2041 bfd_byte format_count
, formati
;
2042 bfd_vma data_count
, datai
;
2043 bfd_byte
*buf
= *bufp
;
2044 bfd_byte
*format_header_data
;
2046 format_count
= read_1_byte (abfd
, &buf
, buf_end
);
2047 format_header_data
= buf
;
2048 for (formati
= 0; formati
< format_count
; formati
++)
2050 _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2051 _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2054 data_count
= _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2055 if (format_count
== 0 && data_count
!= 0)
2057 _bfd_error_handler (_("DWARF error: zero format count"));
2058 bfd_set_error (bfd_error_bad_value
);
2062 /* PR 22210. Paranoia check. Don't bother running the loop
2063 if we know that we are going to run out of buffer. */
2064 if (data_count
> (bfd_vma
) (buf_end
- buf
))
2067 (_("DWARF error: data count (%" PRIx64
") larger than buffer size"),
2068 (uint64_t) data_count
);
2069 bfd_set_error (bfd_error_bad_value
);
2073 for (datai
= 0; datai
< data_count
; datai
++)
2075 bfd_byte
*format
= format_header_data
;
2078 memset (&fe
, 0, sizeof fe
);
2079 for (formati
= 0; formati
< format_count
; formati
++)
2081 bfd_vma content_type
, form
;
2083 char **stringp
= &string_trash
;
2084 unsigned int uint_trash
, *uintp
= &uint_trash
;
2085 struct attribute attr
;
2087 content_type
= _bfd_safe_read_leb128 (abfd
, &format
, false, buf_end
);
2088 switch (content_type
)
2093 case DW_LNCT_directory_index
:
2096 case DW_LNCT_timestamp
:
2106 (_("DWARF error: unknown format content type %" PRIu64
),
2107 (uint64_t) content_type
);
2108 bfd_set_error (bfd_error_bad_value
);
2112 form
= _bfd_safe_read_leb128 (abfd
, &format
, false, buf_end
);
2113 buf
= read_attribute_value (&attr
, form
, 0, unit
, buf
, buf_end
);
2118 case DW_FORM_string
:
2119 case DW_FORM_line_strp
:
2120 *stringp
= attr
.u
.str
;
2128 *uintp
= attr
.u
.val
;
2131 case DW_FORM_data16
:
2132 /* MD5 data is in the attr.blk, but we are ignoring those. */
2137 /* Skip the first "zero entry", which is the compilation dir/file. */
2139 if (!callback (table
, fe
.name
, fe
.dir
, fe
.time
, fe
.size
))
2147 /* Decode the line number information for UNIT. */
2149 static struct line_info_table
*
2150 decode_line_info (struct comp_unit
*unit
)
2152 bfd
*abfd
= unit
->abfd
;
2153 struct dwarf2_debug
*stash
= unit
->stash
;
2154 struct dwarf2_debug_file
*file
= unit
->file
;
2155 struct line_info_table
* table
;
2158 struct line_head lh
;
2159 unsigned int i
, offset_size
;
2160 char *cur_file
, *cur_dir
;
2161 unsigned char op_code
, extended_op
, adj_opcode
;
2162 unsigned int exop_len
;
2165 if (unit
->line_offset
== 0 && file
->line_table
)
2166 return file
->line_table
;
2168 if (! read_section (abfd
, &stash
->debug_sections
[debug_line
],
2169 file
->syms
, unit
->line_offset
,
2170 &file
->dwarf_line_buffer
, &file
->dwarf_line_size
))
2173 if (file
->dwarf_line_size
< 16)
2176 (_("DWARF error: line info section is too small (%" PRId64
")"),
2177 (int64_t) file
->dwarf_line_size
);
2178 bfd_set_error (bfd_error_bad_value
);
2181 line_ptr
= file
->dwarf_line_buffer
+ unit
->line_offset
;
2182 line_end
= file
->dwarf_line_buffer
+ file
->dwarf_line_size
;
2184 /* Read in the prologue. */
2185 lh
.total_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2187 if (lh
.total_length
== 0xffffffff)
2189 lh
.total_length
= read_8_bytes (abfd
, &line_ptr
, line_end
);
2192 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
2194 /* Handle (non-standard) 64-bit DWARF2 formats. */
2195 lh
.total_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2199 if (lh
.total_length
> (size_t) (line_end
- line_ptr
))
2202 /* xgettext: c-format */
2203 (_("DWARF error: line info data is bigger (%#" PRIx64
")"
2204 " than the space remaining in the section (%#lx)"),
2205 (uint64_t) lh
.total_length
, (unsigned long) (line_end
- line_ptr
));
2206 bfd_set_error (bfd_error_bad_value
);
2210 line_end
= line_ptr
+ lh
.total_length
;
2212 lh
.version
= read_2_bytes (abfd
, &line_ptr
, line_end
);
2213 if (lh
.version
< 2 || lh
.version
> 5)
2216 (_("DWARF error: unhandled .debug_line version %d"), lh
.version
);
2217 bfd_set_error (bfd_error_bad_value
);
2221 if (line_ptr
+ offset_size
+ (lh
.version
>= 5 ? 8 : (lh
.version
>= 4 ? 6 : 5))
2225 (_("DWARF error: ran out of room reading prologue"));
2226 bfd_set_error (bfd_error_bad_value
);
2230 if (lh
.version
>= 5)
2232 unsigned int segment_selector_size
;
2234 /* Skip address size. */
2235 read_1_byte (abfd
, &line_ptr
, line_end
);
2237 segment_selector_size
= read_1_byte (abfd
, &line_ptr
, line_end
);
2238 if (segment_selector_size
!= 0)
2241 (_("DWARF error: line info unsupported segment selector size %u"),
2242 segment_selector_size
);
2243 bfd_set_error (bfd_error_bad_value
);
2248 if (offset_size
== 4)
2249 lh
.prologue_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2251 lh
.prologue_length
= read_8_bytes (abfd
, &line_ptr
, line_end
);
2253 lh
.minimum_instruction_length
= read_1_byte (abfd
, &line_ptr
, line_end
);
2255 if (lh
.version
>= 4)
2256 lh
.maximum_ops_per_insn
= read_1_byte (abfd
, &line_ptr
, line_end
);
2258 lh
.maximum_ops_per_insn
= 1;
2260 if (lh
.maximum_ops_per_insn
== 0)
2263 (_("DWARF error: invalid maximum operations per instruction"));
2264 bfd_set_error (bfd_error_bad_value
);
2268 lh
.default_is_stmt
= read_1_byte (abfd
, &line_ptr
, line_end
);
2269 lh
.line_base
= read_1_signed_byte (abfd
, &line_ptr
, line_end
);
2270 lh
.line_range
= read_1_byte (abfd
, &line_ptr
, line_end
);
2271 lh
.opcode_base
= read_1_byte (abfd
, &line_ptr
, line_end
);
2273 if (line_ptr
+ (lh
.opcode_base
- 1) >= line_end
)
2275 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2276 bfd_set_error (bfd_error_bad_value
);
2280 amt
= lh
.opcode_base
* sizeof (unsigned char);
2281 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
2283 lh
.standard_opcode_lengths
[0] = 1;
2285 for (i
= 1; i
< lh
.opcode_base
; ++i
)
2286 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, &line_ptr
, line_end
);
2288 amt
= sizeof (struct line_info_table
);
2289 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
2293 table
->comp_dir
= unit
->comp_dir
;
2295 table
->num_files
= 0;
2296 table
->files
= NULL
;
2298 table
->num_dirs
= 0;
2301 table
->num_sequences
= 0;
2302 table
->sequences
= NULL
;
2304 table
->lcl_head
= NULL
;
2306 if (lh
.version
>= 5)
2308 /* Read directory table. */
2309 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2310 line_info_add_include_dir_stub
))
2313 /* Read file name table. */
2314 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2315 line_info_add_file_name
))
2320 /* Read directory table. */
2321 while ((cur_dir
= read_string (&line_ptr
, line_end
)) != NULL
)
2323 if (!line_info_add_include_dir (table
, cur_dir
))
2327 /* Read file name table. */
2328 while ((cur_file
= read_string (&line_ptr
, line_end
)) != NULL
)
2330 unsigned int dir
, xtime
, size
;
2332 dir
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2333 xtime
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2334 size
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2336 if (!line_info_add_file_name (table
, cur_file
, dir
, xtime
, size
))
2341 /* Read the statement sequences until there's nothing left. */
2342 while (line_ptr
< line_end
)
2344 /* State machine registers. */
2345 bfd_vma address
= 0;
2346 unsigned char op_index
= 0;
2347 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
2348 unsigned int line
= 1;
2349 unsigned int column
= 0;
2350 unsigned int discriminator
= 0;
2351 int is_stmt
= lh
.default_is_stmt
;
2352 int end_sequence
= 0;
2353 unsigned int dir
, xtime
, size
;
2354 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2355 compilers generate address sequences that are wildly out of
2356 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2357 for ia64-Linux). Thus, to determine the low and high
2358 address, we must compare on every DW_LNS_copy, etc. */
2359 bfd_vma low_pc
= (bfd_vma
) -1;
2360 bfd_vma high_pc
= 0;
2362 /* Decode the table. */
2363 while (!end_sequence
&& line_ptr
< line_end
)
2365 op_code
= read_1_byte (abfd
, &line_ptr
, line_end
);
2367 if (op_code
>= lh
.opcode_base
)
2369 /* Special operand. */
2370 adj_opcode
= op_code
- lh
.opcode_base
;
2371 if (lh
.line_range
== 0)
2373 if (lh
.maximum_ops_per_insn
== 1)
2374 address
+= (adj_opcode
/ lh
.line_range
2375 * lh
.minimum_instruction_length
);
2378 address
+= ((op_index
+ adj_opcode
/ lh
.line_range
)
2379 / lh
.maximum_ops_per_insn
2380 * lh
.minimum_instruction_length
);
2381 op_index
= ((op_index
+ adj_opcode
/ lh
.line_range
)
2382 % lh
.maximum_ops_per_insn
);
2384 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
2385 /* Append row to matrix using current values. */
2386 if (!add_line_info (table
, address
, op_index
, filename
,
2387 line
, column
, discriminator
, 0))
2390 if (address
< low_pc
)
2392 if (address
> high_pc
)
2395 else switch (op_code
)
2397 case DW_LNS_extended_op
:
2398 exop_len
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2400 extended_op
= read_1_byte (abfd
, &line_ptr
, line_end
);
2402 switch (extended_op
)
2404 case DW_LNE_end_sequence
:
2406 if (!add_line_info (table
, address
, op_index
, filename
, line
,
2407 column
, discriminator
, end_sequence
))
2410 if (address
< low_pc
)
2412 if (address
> high_pc
)
2414 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
2417 case DW_LNE_set_address
:
2418 address
= read_address (unit
, &line_ptr
, line_end
);
2421 case DW_LNE_define_file
:
2422 cur_file
= read_string (&line_ptr
, line_end
);
2423 dir
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2425 xtime
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2427 size
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2429 if (!line_info_add_file_name (table
, cur_file
, dir
,
2433 case DW_LNE_set_discriminator
:
2435 _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2438 case DW_LNE_HP_source_file_correlation
:
2439 line_ptr
+= exop_len
- 1;
2443 (_("DWARF error: mangled line number section"));
2444 bfd_set_error (bfd_error_bad_value
);
2451 if (!add_line_info (table
, address
, op_index
,
2452 filename
, line
, column
, discriminator
, 0))
2455 if (address
< low_pc
)
2457 if (address
> high_pc
)
2460 case DW_LNS_advance_pc
:
2461 if (lh
.maximum_ops_per_insn
== 1)
2462 address
+= (lh
.minimum_instruction_length
2463 * _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2467 bfd_vma adjust
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2469 address
= ((op_index
+ adjust
) / lh
.maximum_ops_per_insn
2470 * lh
.minimum_instruction_length
);
2471 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2474 case DW_LNS_advance_line
:
2475 line
+= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2478 case DW_LNS_set_file
:
2480 unsigned int filenum
;
2482 /* The file and directory tables are 0
2483 based, the references are 1 based. */
2484 filenum
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2487 filename
= concat_filename (table
, filenum
);
2490 case DW_LNS_set_column
:
2491 column
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2494 case DW_LNS_negate_stmt
:
2495 is_stmt
= (!is_stmt
);
2497 case DW_LNS_set_basic_block
:
2499 case DW_LNS_const_add_pc
:
2500 if (lh
.line_range
== 0)
2502 if (lh
.maximum_ops_per_insn
== 1)
2503 address
+= (lh
.minimum_instruction_length
2504 * ((255 - lh
.opcode_base
) / lh
.line_range
));
2507 bfd_vma adjust
= ((255 - lh
.opcode_base
) / lh
.line_range
);
2508 address
+= (lh
.minimum_instruction_length
2509 * ((op_index
+ adjust
)
2510 / lh
.maximum_ops_per_insn
));
2511 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2514 case DW_LNS_fixed_advance_pc
:
2515 address
+= read_2_bytes (abfd
, &line_ptr
, line_end
);
2519 /* Unknown standard opcode, ignore it. */
2520 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
2521 (void) _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2530 if (unit
->line_offset
== 0)
2531 file
->line_table
= table
;
2532 if (sort_line_sequences (table
))
2536 while (table
->sequences
!= NULL
)
2538 struct line_sequence
* seq
= table
->sequences
;
2539 table
->sequences
= table
->sequences
->prev_sequence
;
2542 free (table
->files
);
2547 /* If ADDR is within TABLE set the output parameters and return TRUE,
2548 otherwise set *FILENAME_PTR to NULL and return FALSE.
2549 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2550 are pointers to the objects to be filled in. */
2553 lookup_address_in_line_info_table (struct line_info_table
*table
,
2555 const char **filename_ptr
,
2556 unsigned int *linenumber_ptr
,
2557 unsigned int *discriminator_ptr
)
2559 struct line_sequence
*seq
= NULL
;
2560 struct line_info
*info
;
2563 /* Binary search the array of sequences. */
2565 high
= table
->num_sequences
;
2568 mid
= (low
+ high
) / 2;
2569 seq
= &table
->sequences
[mid
];
2570 if (addr
< seq
->low_pc
)
2572 else if (addr
>= seq
->last_line
->address
)
2578 /* Check for a valid sequence. */
2579 if (!seq
|| addr
< seq
->low_pc
|| addr
>= seq
->last_line
->address
)
2582 if (!build_line_info_table (table
, seq
))
2585 /* Binary search the array of line information. */
2587 high
= seq
->num_lines
;
2591 mid
= (low
+ high
) / 2;
2592 info
= seq
->line_info_lookup
[mid
];
2593 if (addr
< info
->address
)
2595 else if (addr
>= seq
->line_info_lookup
[mid
+ 1]->address
)
2601 /* Check for a valid line information entry. */
2603 && addr
>= info
->address
2604 && addr
< seq
->line_info_lookup
[mid
+ 1]->address
2605 && !(info
->end_sequence
|| info
== seq
->last_line
))
2607 *filename_ptr
= info
->filename
;
2608 *linenumber_ptr
= info
->line
;
2609 if (discriminator_ptr
)
2610 *discriminator_ptr
= info
->discriminator
;
2615 *filename_ptr
= NULL
;
2619 /* Read in the .debug_ranges section for future reference. */
2622 read_debug_ranges (struct comp_unit
* unit
)
2624 struct dwarf2_debug
*stash
= unit
->stash
;
2625 struct dwarf2_debug_file
*file
= unit
->file
;
2627 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_ranges
],
2629 &file
->dwarf_ranges_buffer
, &file
->dwarf_ranges_size
);
2632 /* Read in the .debug_rnglists section for future reference. */
2635 read_debug_rnglists (struct comp_unit
* unit
)
2637 struct dwarf2_debug
*stash
= unit
->stash
;
2638 struct dwarf2_debug_file
*file
= unit
->file
;
2640 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_rnglists
],
2642 &file
->dwarf_rnglists_buffer
, &file
->dwarf_rnglists_size
);
2645 /* Function table functions. */
2648 compare_lookup_funcinfos (const void * a
, const void * b
)
2650 const struct lookup_funcinfo
* lookup1
= a
;
2651 const struct lookup_funcinfo
* lookup2
= b
;
2653 if (lookup1
->low_addr
< lookup2
->low_addr
)
2655 if (lookup1
->low_addr
> lookup2
->low_addr
)
2657 if (lookup1
->high_addr
< lookup2
->high_addr
)
2659 if (lookup1
->high_addr
> lookup2
->high_addr
)
2662 if (lookup1
->idx
< lookup2
->idx
)
2664 if (lookup1
->idx
> lookup2
->idx
)
2670 build_lookup_funcinfo_table (struct comp_unit
* unit
)
2672 struct lookup_funcinfo
*lookup_funcinfo_table
= unit
->lookup_funcinfo_table
;
2673 unsigned int number_of_functions
= unit
->number_of_functions
;
2674 struct funcinfo
*each
;
2675 struct lookup_funcinfo
*entry
;
2677 struct arange
*range
;
2678 bfd_vma low_addr
, high_addr
;
2680 if (lookup_funcinfo_table
|| number_of_functions
== 0)
2683 /* Create the function info lookup table. */
2684 lookup_funcinfo_table
= (struct lookup_funcinfo
*)
2685 bfd_malloc (number_of_functions
* sizeof (struct lookup_funcinfo
));
2686 if (lookup_funcinfo_table
== NULL
)
2689 /* Populate the function info lookup table. */
2690 func_index
= number_of_functions
;
2691 for (each
= unit
->function_table
; each
; each
= each
->prev_func
)
2693 entry
= &lookup_funcinfo_table
[--func_index
];
2694 entry
->funcinfo
= each
;
2695 entry
->idx
= func_index
;
2697 /* Calculate the lowest and highest address for this function entry. */
2698 low_addr
= entry
->funcinfo
->arange
.low
;
2699 high_addr
= entry
->funcinfo
->arange
.high
;
2701 for (range
= entry
->funcinfo
->arange
.next
; range
; range
= range
->next
)
2703 if (range
->low
< low_addr
)
2704 low_addr
= range
->low
;
2705 if (range
->high
> high_addr
)
2706 high_addr
= range
->high
;
2709 entry
->low_addr
= low_addr
;
2710 entry
->high_addr
= high_addr
;
2713 BFD_ASSERT (func_index
== 0);
2715 /* Sort the function by address. */
2716 qsort (lookup_funcinfo_table
,
2717 number_of_functions
,
2718 sizeof (struct lookup_funcinfo
),
2719 compare_lookup_funcinfos
);
2721 /* Calculate the high watermark for each function in the lookup table. */
2722 high_addr
= lookup_funcinfo_table
[0].high_addr
;
2723 for (func_index
= 1; func_index
< number_of_functions
; func_index
++)
2725 entry
= &lookup_funcinfo_table
[func_index
];
2726 if (entry
->high_addr
> high_addr
)
2727 high_addr
= entry
->high_addr
;
2729 entry
->high_addr
= high_addr
;
2732 unit
->lookup_funcinfo_table
= lookup_funcinfo_table
;
2736 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
2737 TRUE. Note that we need to find the function that has the smallest range
2738 that contains ADDR, to handle inlined functions without depending upon
2739 them being ordered in TABLE by increasing range. */
2742 lookup_address_in_function_table (struct comp_unit
*unit
,
2744 struct funcinfo
**function_ptr
)
2746 unsigned int number_of_functions
= unit
->number_of_functions
;
2747 struct lookup_funcinfo
* lookup_funcinfo
= NULL
;
2748 struct funcinfo
* funcinfo
= NULL
;
2749 struct funcinfo
* best_fit
= NULL
;
2750 bfd_vma best_fit_len
= 0;
2751 bfd_size_type low
, high
, mid
, first
;
2752 struct arange
*arange
;
2754 if (number_of_functions
== 0)
2757 if (!build_lookup_funcinfo_table (unit
))
2760 if (unit
->lookup_funcinfo_table
[number_of_functions
- 1].high_addr
< addr
)
2763 /* Find the first function in the lookup table which may contain the
2764 specified address. */
2766 high
= number_of_functions
;
2770 mid
= (low
+ high
) / 2;
2771 lookup_funcinfo
= &unit
->lookup_funcinfo_table
[mid
];
2772 if (addr
< lookup_funcinfo
->low_addr
)
2774 else if (addr
>= lookup_funcinfo
->high_addr
)
2780 /* Find the 'best' match for the address. The prior algorithm defined the
2781 best match as the function with the smallest address range containing
2782 the specified address. This definition should probably be changed to the
2783 innermost inline routine containing the address, but right now we want
2784 to get the same results we did before. */
2785 while (first
< number_of_functions
)
2787 if (addr
< unit
->lookup_funcinfo_table
[first
].low_addr
)
2789 funcinfo
= unit
->lookup_funcinfo_table
[first
].funcinfo
;
2791 for (arange
= &funcinfo
->arange
; arange
; arange
= arange
->next
)
2793 if (addr
< arange
->low
|| addr
>= arange
->high
)
2797 || arange
->high
- arange
->low
< best_fit_len
2798 /* The following comparison is designed to return the same
2799 match as the previous algorithm for routines which have the
2800 same best fit length. */
2801 || (arange
->high
- arange
->low
== best_fit_len
2802 && funcinfo
> best_fit
))
2804 best_fit
= funcinfo
;
2805 best_fit_len
= arange
->high
- arange
->low
;
2815 *function_ptr
= best_fit
;
2819 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2820 and LINENUMBER_PTR, and return TRUE. */
2823 lookup_symbol_in_function_table (struct comp_unit
*unit
,
2826 const char **filename_ptr
,
2827 unsigned int *linenumber_ptr
)
2829 struct funcinfo
* each_func
;
2830 struct funcinfo
* best_fit
= NULL
;
2831 bfd_vma best_fit_len
= 0;
2832 struct arange
*arange
;
2833 const char *name
= bfd_asymbol_name (sym
);
2834 asection
*sec
= bfd_asymbol_section (sym
);
2836 for (each_func
= unit
->function_table
;
2838 each_func
= each_func
->prev_func
)
2840 for (arange
= &each_func
->arange
;
2842 arange
= arange
->next
)
2844 if ((!each_func
->sec
|| each_func
->sec
== sec
)
2845 && addr
>= arange
->low
2846 && addr
< arange
->high
2848 && strcmp (name
, each_func
->name
) == 0
2850 || arange
->high
- arange
->low
< best_fit_len
))
2852 best_fit
= each_func
;
2853 best_fit_len
= arange
->high
- arange
->low
;
2860 best_fit
->sec
= sec
;
2861 *filename_ptr
= best_fit
->file
;
2862 *linenumber_ptr
= best_fit
->line
;
2869 /* Variable table functions. */
2871 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
2872 LINENUMBER_PTR, and return TRUE. */
2875 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
2878 const char **filename_ptr
,
2879 unsigned int *linenumber_ptr
)
2881 const char *name
= bfd_asymbol_name (sym
);
2882 asection
*sec
= bfd_asymbol_section (sym
);
2883 struct varinfo
* each
;
2885 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
2887 && each
->file
!= NULL
2888 && each
->name
!= NULL
2889 && each
->addr
== addr
2890 && (!each
->sec
|| each
->sec
== sec
)
2891 && strcmp (name
, each
->name
) == 0)
2897 *filename_ptr
= each
->file
;
2898 *linenumber_ptr
= each
->line
;
2905 static struct comp_unit
*stash_comp_unit (struct dwarf2_debug
*,
2906 struct dwarf2_debug_file
*);
2907 static bool comp_unit_maybe_decode_line_info (struct comp_unit
*);
2910 find_abstract_instance (struct comp_unit
*unit
,
2911 struct attribute
*attr_ptr
,
2912 unsigned int recur_count
,
2915 char **filename_ptr
,
2916 int *linenumber_ptr
)
2918 bfd
*abfd
= unit
->abfd
;
2919 bfd_byte
*info_ptr
= NULL
;
2920 bfd_byte
*info_ptr_end
;
2921 unsigned int abbrev_number
, i
;
2922 struct abbrev_info
*abbrev
;
2923 bfd_uint64_t die_ref
= attr_ptr
->u
.val
;
2924 struct attribute attr
;
2925 const char *name
= NULL
;
2927 if (recur_count
== 100)
2930 (_("DWARF error: abstract instance recursion detected"));
2931 bfd_set_error (bfd_error_bad_value
);
2935 /* DW_FORM_ref_addr can reference an entry in a different CU. It
2936 is an offset from the .debug_info section, not the current CU. */
2937 if (attr_ptr
->form
== DW_FORM_ref_addr
)
2939 /* We only support DW_FORM_ref_addr within the same file, so
2940 any relocations should be resolved already. Check this by
2941 testing for a zero die_ref; There can't be a valid reference
2942 to the header of a .debug_info section.
2943 DW_FORM_ref_addr is an offset relative to .debug_info.
2944 Normally when using the GNU linker this is accomplished by
2945 emitting a symbolic reference to a label, because .debug_info
2946 sections are linked at zero. When there are multiple section
2947 groups containing .debug_info, as there might be in a
2948 relocatable object file, it would be reasonable to assume that
2949 a symbolic reference to a label in any .debug_info section
2950 might be used. Since we lay out multiple .debug_info
2951 sections at non-zero VMAs (see place_sections), and read
2952 them contiguously into dwarf_info_buffer, that means the
2953 reference is relative to dwarf_info_buffer. */
2956 info_ptr
= unit
->file
->dwarf_info_buffer
;
2957 info_ptr_end
= info_ptr
+ unit
->file
->dwarf_info_size
;
2958 total
= info_ptr_end
- info_ptr
;
2961 else if (die_ref
>= total
)
2964 (_("DWARF error: invalid abstract instance DIE ref"));
2965 bfd_set_error (bfd_error_bad_value
);
2968 info_ptr
+= die_ref
;
2970 else if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
2972 bool first_time
= unit
->stash
->alt
.dwarf_info_buffer
== NULL
;
2974 info_ptr
= read_alt_indirect_ref (unit
, die_ref
);
2976 unit
->stash
->alt
.info_ptr
= unit
->stash
->alt
.dwarf_info_buffer
;
2977 if (info_ptr
== NULL
)
2980 (_("DWARF error: unable to read alt ref %" PRIu64
),
2981 (uint64_t) die_ref
);
2982 bfd_set_error (bfd_error_bad_value
);
2985 info_ptr_end
= (unit
->stash
->alt
.dwarf_info_buffer
2986 + unit
->stash
->alt
.dwarf_info_size
);
2987 if (unit
->stash
->alt
.all_comp_units
)
2988 unit
= unit
->stash
->alt
.all_comp_units
;
2991 if (attr_ptr
->form
== DW_FORM_ref_addr
2992 || attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
2994 /* Now find the CU containing this pointer. */
2995 if (info_ptr
>= unit
->info_ptr_unit
&& info_ptr
< unit
->end_ptr
)
2996 info_ptr_end
= unit
->end_ptr
;
2999 /* Check other CUs to see if they contain the abbrev. */
3000 struct comp_unit
*u
;
3002 for (u
= unit
->prev_unit
; u
!= NULL
; u
= u
->prev_unit
)
3003 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3007 for (u
= unit
->next_unit
; u
!= NULL
; u
= u
->next_unit
)
3008 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3011 if (attr_ptr
->form
== DW_FORM_ref_addr
)
3014 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->f
);
3017 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3022 if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3025 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->alt
);
3028 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3036 (_("DWARF error: unable to locate abstract instance DIE ref %"
3037 PRIu64
), (uint64_t) die_ref
);
3038 bfd_set_error (bfd_error_bad_value
);
3042 info_ptr_end
= unit
->end_ptr
;
3047 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
3048 DW_FORM_ref_udata. These are all references relative to the
3049 start of the current CU. */
3052 info_ptr
= unit
->info_ptr_unit
;
3053 info_ptr_end
= unit
->end_ptr
;
3054 total
= info_ptr_end
- info_ptr
;
3055 if (!die_ref
|| die_ref
>= total
)
3058 (_("DWARF error: invalid abstract instance DIE ref"));
3059 bfd_set_error (bfd_error_bad_value
);
3062 info_ptr
+= die_ref
;
3065 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3066 false, info_ptr_end
);
3069 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3073 (_("DWARF error: could not find abbrev number %u"), abbrev_number
);
3074 bfd_set_error (bfd_error_bad_value
);
3079 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3081 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
,
3082 info_ptr
, info_ptr_end
);
3083 if (info_ptr
== NULL
)
3088 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3090 if (name
== NULL
&& is_str_form (&attr
))
3093 if (non_mangled (unit
->lang
))
3097 case DW_AT_specification
:
3098 if (is_int_form (&attr
)
3099 && !find_abstract_instance (unit
, &attr
, recur_count
+ 1,
3101 filename_ptr
, linenumber_ptr
))
3104 case DW_AT_linkage_name
:
3105 case DW_AT_MIPS_linkage_name
:
3106 /* PR 16949: Corrupt debug info can place
3107 non-string forms into these attributes. */
3108 if (is_str_form (&attr
))
3114 case DW_AT_decl_file
:
3115 if (!comp_unit_maybe_decode_line_info (unit
))
3117 if (is_int_form (&attr
))
3118 *filename_ptr
= concat_filename (unit
->line_table
,
3121 case DW_AT_decl_line
:
3122 if (is_int_form (&attr
))
3123 *linenumber_ptr
= attr
.u
.val
;
3136 read_ranges (struct comp_unit
*unit
, struct arange
*arange
,
3137 bfd_uint64_t offset
)
3139 bfd_byte
*ranges_ptr
;
3140 bfd_byte
*ranges_end
;
3141 bfd_vma base_address
= unit
->base_address
;
3143 if (! unit
->file
->dwarf_ranges_buffer
)
3145 if (! read_debug_ranges (unit
))
3149 if (offset
> unit
->file
->dwarf_ranges_size
)
3151 ranges_ptr
= unit
->file
->dwarf_ranges_buffer
+ offset
;
3152 ranges_end
= unit
->file
->dwarf_ranges_buffer
+ unit
->file
->dwarf_ranges_size
;
3159 /* PR 17512: file: 62cada7d. */
3160 if (2u * unit
->addr_size
> (size_t) (ranges_end
- ranges_ptr
))
3163 low_pc
= read_address (unit
, &ranges_ptr
, ranges_end
);
3164 high_pc
= read_address (unit
, &ranges_ptr
, ranges_end
);
3166 if (low_pc
== 0 && high_pc
== 0)
3168 if (low_pc
== -1UL && high_pc
!= -1UL)
3169 base_address
= high_pc
;
3172 if (!arange_add (unit
, arange
,
3173 base_address
+ low_pc
, base_address
+ high_pc
))
3181 read_rnglists (struct comp_unit
*unit
, struct arange
*arange
,
3182 bfd_uint64_t offset
)
3186 bfd_vma base_address
= unit
->base_address
;
3189 bfd
*abfd
= unit
->abfd
;
3191 if (! unit
->file
->dwarf_rnglists_buffer
)
3193 if (! read_debug_rnglists (unit
))
3197 rngs_ptr
= unit
->file
->dwarf_rnglists_buffer
+ offset
;
3198 if (rngs_ptr
< unit
->file
->dwarf_rnglists_buffer
)
3200 rngs_end
= unit
->file
->dwarf_rnglists_buffer
;
3201 rngs_end
+= unit
->file
->dwarf_rnglists_size
;
3205 enum dwarf_range_list_entry rlet
;
3207 if (rngs_ptr
>= rngs_end
)
3210 rlet
= read_1_byte (abfd
, &rngs_ptr
, rngs_end
);
3214 case DW_RLE_end_of_list
:
3217 case DW_RLE_base_address
:
3218 if (unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3220 base_address
= read_address (unit
, &rngs_ptr
, rngs_end
);
3223 case DW_RLE_start_length
:
3224 if (unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3226 low_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3228 high_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3232 case DW_RLE_offset_pair
:
3233 low_pc
= base_address
;
3234 low_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3236 high_pc
= base_address
;
3237 high_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3241 case DW_RLE_start_end
:
3242 if (2u * unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3244 low_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3245 high_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3248 /* TODO x-variants need .debug_addr support used for split-dwarf. */
3249 case DW_RLE_base_addressx
:
3250 case DW_RLE_startx_endx
:
3251 case DW_RLE_startx_length
:
3256 if (!arange_add (unit
, arange
, low_pc
, high_pc
))
3262 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
,
3263 bfd_uint64_t offset
)
3265 if (unit
->version
<= 4)
3266 return read_ranges (unit
, arange
, offset
);
3268 return read_rnglists (unit
, arange
, offset
);
3271 static struct funcinfo
*
3272 lookup_func_by_offset (bfd_uint64_t offset
, struct funcinfo
* table
)
3274 for (; table
!= NULL
; table
= table
->prev_func
)
3275 if (table
->unit_offset
== offset
)
3280 static struct varinfo
*
3281 lookup_var_by_offset (bfd_uint64_t offset
, struct varinfo
* table
)
3285 if (table
->unit_offset
== offset
)
3287 table
= table
->prev_var
;
3294 /* DWARF2 Compilation unit functions. */
3296 static struct funcinfo
*
3297 reverse_funcinfo_list (struct funcinfo
*head
)
3299 struct funcinfo
*rhead
;
3300 struct funcinfo
*temp
;
3302 for (rhead
= NULL
; head
; head
= temp
)
3304 temp
= head
->prev_func
;
3305 head
->prev_func
= rhead
;
3311 static struct varinfo
*
3312 reverse_varinfo_list (struct varinfo
*head
)
3314 struct varinfo
*rhead
;
3315 struct varinfo
*temp
;
3317 for (rhead
= NULL
; head
; head
= temp
)
3319 temp
= head
->prev_var
;
3320 head
->prev_var
= rhead
;
3326 /* Scan over each die in a comp. unit looking for functions to add
3327 to the function table and variables to the variable table. */
3330 scan_unit_for_symbols (struct comp_unit
*unit
)
3332 bfd
*abfd
= unit
->abfd
;
3333 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
3334 bfd_byte
*info_ptr_end
= unit
->end_ptr
;
3335 int nesting_level
= 0;
3336 struct nest_funcinfo
3338 struct funcinfo
*func
;
3340 int nested_funcs_size
;
3341 struct funcinfo
*last_func
;
3342 struct varinfo
*last_var
;
3344 /* Maintain a stack of in-scope functions and inlined functions, which we
3345 can use to set the caller_func field. */
3346 nested_funcs_size
= 32;
3347 nested_funcs
= (struct nest_funcinfo
*)
3348 bfd_malloc (nested_funcs_size
* sizeof (*nested_funcs
));
3349 if (nested_funcs
== NULL
)
3351 nested_funcs
[nesting_level
].func
= 0;
3353 /* PR 27484: We must scan the DIEs twice. The first time we look for
3354 function and variable tags and accumulate them into their respective
3355 tables. The second time through we process the attributes of the
3356 functions/variables and augment the table entries. */
3357 while (nesting_level
>= 0)
3359 unsigned int abbrev_number
, i
;
3360 struct abbrev_info
*abbrev
;
3361 struct funcinfo
*func
;
3362 struct varinfo
*var
;
3363 bfd_uint64_t current_offset
;
3365 /* PR 17512: file: 9f405d9d. */
3366 if (info_ptr
>= info_ptr_end
)
3369 current_offset
= info_ptr
- unit
->info_ptr_unit
;
3370 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3371 false, info_ptr_end
);
3372 if (abbrev_number
== 0)
3378 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3381 static unsigned int previous_failed_abbrev
= -1U;
3383 /* Avoid multiple reports of the same missing abbrev. */
3384 if (abbrev_number
!= previous_failed_abbrev
)
3387 (_("DWARF error: could not find abbrev number %u"),
3389 previous_failed_abbrev
= abbrev_number
;
3391 bfd_set_error (bfd_error_bad_value
);
3395 if (abbrev
->tag
== DW_TAG_subprogram
3396 || abbrev
->tag
== DW_TAG_entry_point
3397 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3399 size_t amt
= sizeof (struct funcinfo
);
3402 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
3405 func
->tag
= abbrev
->tag
;
3406 func
->prev_func
= unit
->function_table
;
3407 func
->unit_offset
= current_offset
;
3408 unit
->function_table
= func
;
3409 unit
->number_of_functions
++;
3410 BFD_ASSERT (!unit
->cached
);
3412 if (func
->tag
== DW_TAG_inlined_subroutine
)
3413 for (i
= nesting_level
; i
-- != 0; )
3414 if (nested_funcs
[i
].func
)
3416 func
->caller_func
= nested_funcs
[i
].func
;
3419 nested_funcs
[nesting_level
].func
= func
;
3424 if (abbrev
->tag
== DW_TAG_variable
3425 || abbrev
->tag
== DW_TAG_member
)
3427 size_t amt
= sizeof (struct varinfo
);
3429 var
= (struct varinfo
*) bfd_zalloc (abfd
, amt
);
3432 var
->tag
= abbrev
->tag
;
3434 var
->prev_var
= unit
->variable_table
;
3435 unit
->variable_table
= var
;
3436 var
->unit_offset
= current_offset
;
3437 /* PR 18205: Missing debug information can cause this
3438 var to be attached to an already cached unit. */
3443 /* No inline function in scope at this nesting level. */
3444 nested_funcs
[nesting_level
].func
= 0;
3447 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3449 struct attribute attr
;
3451 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
3452 unit
, info_ptr
, info_ptr_end
);
3453 if (info_ptr
== NULL
)
3457 if (abbrev
->has_children
)
3461 if (nesting_level
>= nested_funcs_size
)
3463 struct nest_funcinfo
*tmp
;
3465 nested_funcs_size
*= 2;
3466 tmp
= (struct nest_funcinfo
*)
3467 bfd_realloc (nested_funcs
,
3468 nested_funcs_size
* sizeof (*nested_funcs
));
3473 nested_funcs
[nesting_level
].func
= 0;
3477 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
3478 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
3480 /* This is the second pass over the abbrevs. */
3481 info_ptr
= unit
->first_child_die_ptr
;
3487 while (nesting_level
>= 0)
3489 unsigned int abbrev_number
, i
;
3490 struct abbrev_info
*abbrev
;
3491 struct attribute attr
;
3492 struct funcinfo
*func
;
3493 struct varinfo
*var
;
3495 bfd_vma high_pc
= 0;
3496 bool high_pc_relative
= false;
3497 bfd_uint64_t current_offset
;
3499 /* PR 17512: file: 9f405d9d. */
3500 if (info_ptr
>= info_ptr_end
)
3503 current_offset
= info_ptr
- unit
->info_ptr_unit
;
3504 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3505 false, info_ptr_end
);
3506 if (! abbrev_number
)
3512 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3513 /* This should have been handled above. */
3514 BFD_ASSERT (abbrev
!= NULL
);
3518 if (abbrev
->tag
== DW_TAG_subprogram
3519 || abbrev
->tag
== DW_TAG_entry_point
3520 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3523 && last_func
->prev_func
3524 && last_func
->prev_func
->unit_offset
== current_offset
)
3525 func
= last_func
->prev_func
;
3527 func
= lookup_func_by_offset (current_offset
, unit
->function_table
);
3534 else if (abbrev
->tag
== DW_TAG_variable
3535 || abbrev
->tag
== DW_TAG_member
)
3538 && last_var
->prev_var
3539 && last_var
->prev_var
->unit_offset
== current_offset
)
3540 var
= last_var
->prev_var
;
3542 var
= lookup_var_by_offset (current_offset
, unit
->variable_table
);
3550 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3552 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
3553 unit
, info_ptr
, info_ptr_end
);
3554 if (info_ptr
== NULL
)
3561 case DW_AT_call_file
:
3562 if (is_int_form (&attr
))
3563 func
->caller_file
= concat_filename (unit
->line_table
,
3567 case DW_AT_call_line
:
3568 if (is_int_form (&attr
))
3569 func
->caller_line
= attr
.u
.val
;
3572 case DW_AT_abstract_origin
:
3573 case DW_AT_specification
:
3574 if (is_int_form (&attr
)
3575 && !find_abstract_instance (unit
, &attr
, 0,
3584 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3586 if (func
->name
== NULL
&& is_str_form (&attr
))
3588 func
->name
= attr
.u
.str
;
3589 if (non_mangled (unit
->lang
))
3590 func
->is_linkage
= true;
3594 case DW_AT_linkage_name
:
3595 case DW_AT_MIPS_linkage_name
:
3596 /* PR 16949: Corrupt debug info can place
3597 non-string forms into these attributes. */
3598 if (is_str_form (&attr
))
3600 func
->name
= attr
.u
.str
;
3601 func
->is_linkage
= true;
3606 if (is_int_form (&attr
))
3607 low_pc
= attr
.u
.val
;
3611 if (is_int_form (&attr
))
3613 high_pc
= attr
.u
.val
;
3614 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
3619 if (is_int_form (&attr
)
3620 && !read_rangelist (unit
, &func
->arange
, attr
.u
.val
))
3624 case DW_AT_decl_file
:
3625 if (is_int_form (&attr
))
3626 func
->file
= concat_filename (unit
->line_table
,
3630 case DW_AT_decl_line
:
3631 if (is_int_form (&attr
))
3632 func
->line
= attr
.u
.val
;
3643 case DW_AT_specification
:
3644 if (is_int_form (&attr
) && attr
.u
.val
)
3646 struct varinfo
* spec_var
;
3648 spec_var
= lookup_var_by_offset (attr
.u
.val
,
3649 unit
->variable_table
);
3650 if (spec_var
== NULL
)
3652 _bfd_error_handler (_("DWARF error: could not find "
3653 "variable specification "
3655 (unsigned long) attr
.u
.val
);
3659 if (var
->name
== NULL
)
3660 var
->name
= spec_var
->name
;
3661 if (var
->file
== NULL
&& spec_var
->file
!= NULL
)
3662 var
->file
= strdup (spec_var
->file
);
3664 var
->line
= spec_var
->line
;
3665 if (var
->sec
== NULL
)
3666 var
->sec
= spec_var
->sec
;
3671 if (is_str_form (&attr
))
3672 var
->name
= attr
.u
.str
;
3675 case DW_AT_decl_file
:
3676 if (is_int_form (&attr
))
3677 var
->file
= concat_filename (unit
->line_table
,
3681 case DW_AT_decl_line
:
3682 if (is_int_form (&attr
))
3683 var
->line
= attr
.u
.val
;
3686 case DW_AT_external
:
3687 if (is_int_form (&attr
) && attr
.u
.val
!= 0)
3691 case DW_AT_location
:
3695 case DW_FORM_block1
:
3696 case DW_FORM_block2
:
3697 case DW_FORM_block4
:
3698 case DW_FORM_exprloc
:
3699 if (attr
.u
.blk
->data
!= NULL
3700 && *attr
.u
.blk
->data
== DW_OP_addr
)
3704 /* Verify that DW_OP_addr is the only opcode in the
3705 location, in which case the block size will be 1
3706 plus the address size. */
3707 /* ??? For TLS variables, gcc can emit
3708 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
3709 which we don't handle here yet. */
3710 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
3711 var
->addr
= bfd_get (unit
->addr_size
* 8,
3713 attr
.u
.blk
->data
+ 1);
3728 if (abbrev
->has_children
)
3731 if (high_pc_relative
)
3734 if (func
&& high_pc
!= 0)
3736 if (!arange_add (unit
, &func
->arange
, low_pc
, high_pc
))
3741 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
3742 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
3744 free (nested_funcs
);
3748 free (nested_funcs
);
3752 /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
3753 includes the compilation unit header that proceeds the DIE's, but
3754 does not include the length field that precedes each compilation
3755 unit header. END_PTR points one past the end of this comp unit.
3756 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
3758 This routine does not read the whole compilation unit; only enough
3759 to get to the line number information for the compilation unit. */
3761 static struct comp_unit
*
3762 parse_comp_unit (struct dwarf2_debug
*stash
,
3763 struct dwarf2_debug_file
*file
,
3765 bfd_vma unit_length
,
3766 bfd_byte
*info_ptr_unit
,
3767 unsigned int offset_size
)
3769 struct comp_unit
* unit
;
3770 unsigned int version
;
3771 bfd_uint64_t abbrev_offset
= 0;
3772 /* Initialize it just to avoid a GCC false warning. */
3773 unsigned int addr_size
= -1;
3774 struct abbrev_info
** abbrevs
;
3775 unsigned int abbrev_number
, i
;
3776 struct abbrev_info
*abbrev
;
3777 struct attribute attr
;
3778 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
3781 bfd_vma high_pc
= 0;
3782 bfd
*abfd
= file
->bfd_ptr
;
3783 bool high_pc_relative
= false;
3784 enum dwarf_unit_type unit_type
;
3786 version
= read_2_bytes (abfd
, &info_ptr
, end_ptr
);
3787 if (version
< 2 || version
> 5)
3789 /* PR 19872: A version number of 0 probably means that there is padding
3790 at the end of the .debug_info section. Gold puts it there when
3791 performing an incremental link, for example. So do not generate
3792 an error, just return a NULL. */
3796 (_("DWARF error: found dwarf version '%u', this reader"
3797 " only handles version 2, 3, 4 and 5 information"), version
);
3798 bfd_set_error (bfd_error_bad_value
);
3804 unit_type
= DW_UT_compile
;
3807 unit_type
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
3808 addr_size
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
3811 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
3812 if (offset_size
== 4)
3813 abbrev_offset
= read_4_bytes (abfd
, &info_ptr
, end_ptr
);
3815 abbrev_offset
= read_8_bytes (abfd
, &info_ptr
, end_ptr
);
3818 addr_size
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
3820 if (unit_type
== DW_UT_type
)
3822 /* Skip type signature. */
3825 /* Skip type offset. */
3826 info_ptr
+= offset_size
;
3829 if (addr_size
> sizeof (bfd_vma
))
3832 /* xgettext: c-format */
3833 (_("DWARF error: found address size '%u', this reader"
3834 " can not handle sizes greater than '%u'"),
3836 (unsigned int) sizeof (bfd_vma
));
3837 bfd_set_error (bfd_error_bad_value
);
3841 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
3844 ("DWARF error: found address size '%u', this reader"
3845 " can only handle address sizes '2', '4' and '8'", addr_size
);
3846 bfd_set_error (bfd_error_bad_value
);
3850 /* Read the abbrevs for this compilation unit into a table. */
3851 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
, file
);
3855 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3857 if (! abbrev_number
)
3859 /* PR 19872: An abbrev number of 0 probably means that there is padding
3860 at the end of the .debug_abbrev section. Gold puts it there when
3861 performing an incremental link, for example. So do not generate
3862 an error, just return a NULL. */
3866 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
3869 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
3871 bfd_set_error (bfd_error_bad_value
);
3875 amt
= sizeof (struct comp_unit
);
3876 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
3880 unit
->version
= version
;
3881 unit
->addr_size
= addr_size
;
3882 unit
->offset_size
= offset_size
;
3883 unit
->abbrevs
= abbrevs
;
3884 unit
->end_ptr
= end_ptr
;
3885 unit
->stash
= stash
;
3887 unit
->info_ptr_unit
= info_ptr_unit
;
3889 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3891 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
, end_ptr
);
3892 if (info_ptr
== NULL
)
3895 /* Store the data if it is of an attribute we want to keep in a
3896 partial symbol table. */
3899 case DW_AT_stmt_list
:
3900 if (is_int_form (&attr
))
3903 unit
->line_offset
= attr
.u
.val
;
3908 if (is_str_form (&attr
))
3909 unit
->name
= attr
.u
.str
;
3913 if (is_int_form (&attr
))
3915 low_pc
= attr
.u
.val
;
3916 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
3917 this is the base address to use when reading location
3918 lists or range lists. */
3919 if (abbrev
->tag
== DW_TAG_compile_unit
)
3920 unit
->base_address
= low_pc
;
3925 if (is_int_form (&attr
))
3927 high_pc
= attr
.u
.val
;
3928 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
3933 if (is_int_form (&attr
)
3934 && !read_rangelist (unit
, &unit
->arange
, attr
.u
.val
))
3938 case DW_AT_comp_dir
:
3940 char *comp_dir
= attr
.u
.str
;
3942 /* PR 17512: file: 1fe726be. */
3943 if (!is_str_form (&attr
))
3946 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
3952 /* Irix 6.2 native cc prepends <machine>.: to the compilation
3953 directory, get rid of it. */
3954 char *cp
= strchr (comp_dir
, ':');
3956 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
3959 unit
->comp_dir
= comp_dir
;
3963 case DW_AT_language
:
3964 if (is_int_form (&attr
))
3965 unit
->lang
= attr
.u
.val
;
3972 if (high_pc_relative
)
3976 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
3980 unit
->first_child_die_ptr
= info_ptr
;
3984 /* Return TRUE if UNIT may contain the address given by ADDR. When
3985 there are functions written entirely with inline asm statements, the
3986 range info in the compilation unit header may not be correct. We
3987 need to consult the line info table to see if a compilation unit
3988 really contains the given address. */
3991 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
3993 struct arange
*arange
;
3998 arange
= &unit
->arange
;
4001 if (addr
>= arange
->low
&& addr
< arange
->high
)
4003 arange
= arange
->next
;
4010 /* If UNIT contains ADDR, set the output parameters to the values for
4011 the line containing ADDR and return TRUE. Otherwise return FALSE.
4012 The output parameters, FILENAME_PTR, FUNCTION_PTR, and
4013 LINENUMBER_PTR, are pointers to the objects to be filled in. */
4016 comp_unit_find_nearest_line (struct comp_unit
*unit
,
4018 const char **filename_ptr
,
4019 struct funcinfo
**function_ptr
,
4020 unsigned int *linenumber_ptr
,
4021 unsigned int *discriminator_ptr
)
4023 bool line_p
, func_p
;
4025 if (!comp_unit_maybe_decode_line_info (unit
))
4028 *function_ptr
= NULL
;
4029 func_p
= lookup_address_in_function_table (unit
, addr
, function_ptr
);
4030 if (func_p
&& (*function_ptr
)->tag
== DW_TAG_inlined_subroutine
)
4031 unit
->stash
->inliner_chain
= *function_ptr
;
4033 line_p
= lookup_address_in_line_info_table (unit
->line_table
, addr
,
4037 return line_p
|| func_p
;
4040 /* Check to see if line info is already decoded in a comp_unit.
4041 If not, decode it. Returns TRUE if no errors were encountered;
4045 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
)
4050 if (! unit
->line_table
)
4052 if (! unit
->stmtlist
)
4058 unit
->line_table
= decode_line_info (unit
);
4060 if (! unit
->line_table
)
4066 if (unit
->first_child_die_ptr
< unit
->end_ptr
4067 && ! scan_unit_for_symbols (unit
))
4077 /* If UNIT contains SYM at ADDR, set the output parameters to the
4078 values for the line containing SYM. The output parameters,
4079 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
4082 Return TRUE if UNIT contains SYM, and no errors were encountered;
4086 comp_unit_find_line (struct comp_unit
*unit
,
4089 const char **filename_ptr
,
4090 unsigned int *linenumber_ptr
)
4092 if (!comp_unit_maybe_decode_line_info (unit
))
4095 if (sym
->flags
& BSF_FUNCTION
)
4096 return lookup_symbol_in_function_table (unit
, sym
, addr
,
4100 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
4105 /* Extract all interesting funcinfos and varinfos of a compilation
4106 unit into hash tables for faster lookup. Returns TRUE if no
4107 errors were enountered; FALSE otherwise. */
4110 comp_unit_hash_info (struct dwarf2_debug
*stash
,
4111 struct comp_unit
*unit
,
4112 struct info_hash_table
*funcinfo_hash_table
,
4113 struct info_hash_table
*varinfo_hash_table
)
4115 struct funcinfo
* each_func
;
4116 struct varinfo
* each_var
;
4119 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
4121 if (!comp_unit_maybe_decode_line_info (unit
))
4124 BFD_ASSERT (!unit
->cached
);
4126 /* To preserve the original search order, we went to visit the function
4127 infos in the reversed order of the list. However, making the list
4128 bi-directional use quite a bit of extra memory. So we reverse
4129 the list first, traverse the list in the now reversed order and
4130 finally reverse the list again to get back the original order. */
4131 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4132 for (each_func
= unit
->function_table
;
4134 each_func
= each_func
->prev_func
)
4136 /* Skip nameless functions. */
4137 if (each_func
->name
)
4138 /* There is no need to copy name string into hash table as
4139 name string is either in the dwarf string buffer or
4140 info in the stash. */
4141 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
4142 (void*) each_func
, false);
4144 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4148 /* We do the same for variable infos. */
4149 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4150 for (each_var
= unit
->variable_table
;
4152 each_var
= each_var
->prev_var
)
4154 /* Skip stack vars and vars with no files or names. */
4155 if (! each_var
->stack
4156 && each_var
->file
!= NULL
4157 && each_var
->name
!= NULL
)
4158 /* There is no need to copy name string into hash table as
4159 name string is either in the dwarf string buffer or
4160 info in the stash. */
4161 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
4162 (void*) each_var
, false);
4165 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4166 unit
->cached
= true;
4170 /* Locate a section in a BFD containing debugging info. The search starts
4171 from the section after AFTER_SEC, or from the first section in the BFD if
4172 AFTER_SEC is NULL. The search works by examining the names of the
4173 sections. There are three permissiable names. The first two are given
4174 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
4175 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
4176 This is a variation on the .debug_info section which has a checksum
4177 describing the contents appended onto the name. This allows the linker to
4178 identify and discard duplicate debugging sections for different
4179 compilation units. */
4180 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
4183 find_debug_info (bfd
*abfd
, const struct dwarf_debug_section
*debug_sections
,
4184 asection
*after_sec
)
4189 if (after_sec
== NULL
)
4191 look
= debug_sections
[debug_info
].uncompressed_name
;
4192 msec
= bfd_get_section_by_name (abfd
, look
);
4196 look
= debug_sections
[debug_info
].compressed_name
;
4197 msec
= bfd_get_section_by_name (abfd
, look
);
4201 for (msec
= abfd
->sections
; msec
!= NULL
; msec
= msec
->next
)
4202 if (startswith (msec
->name
, GNU_LINKONCE_INFO
))
4208 for (msec
= after_sec
->next
; msec
!= NULL
; msec
= msec
->next
)
4210 look
= debug_sections
[debug_info
].uncompressed_name
;
4211 if (strcmp (msec
->name
, look
) == 0)
4214 look
= debug_sections
[debug_info
].compressed_name
;
4215 if (look
!= NULL
&& strcmp (msec
->name
, look
) == 0)
4218 if (startswith (msec
->name
, GNU_LINKONCE_INFO
))
4225 /* Transfer VMAs from object file to separate debug file. */
4228 set_debug_vma (bfd
*orig_bfd
, bfd
*debug_bfd
)
4232 for (s
= orig_bfd
->sections
, d
= debug_bfd
->sections
;
4233 s
!= NULL
&& d
!= NULL
;
4234 s
= s
->next
, d
= d
->next
)
4236 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4238 /* ??? Assumes 1-1 correspondence between sections in the
4240 if (strcmp (s
->name
, d
->name
) == 0)
4242 d
->output_section
= s
->output_section
;
4243 d
->output_offset
= s
->output_offset
;
4249 /* If the dwarf2 info was found in a separate debug file, return the
4250 debug file section corresponding to the section in the original file
4251 and the debug file symbols. */
4254 _bfd_dwarf2_stash_syms (struct dwarf2_debug
*stash
, bfd
*abfd
,
4255 asection
**sec
, asymbol
***syms
)
4257 if (stash
->f
.bfd_ptr
!= abfd
)
4263 *syms
= stash
->f
.syms
;
4267 for (s
= abfd
->sections
, d
= stash
->f
.bfd_ptr
->sections
;
4268 s
!= NULL
&& d
!= NULL
;
4269 s
= s
->next
, d
= d
->next
)
4271 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4274 && strcmp (s
->name
, d
->name
) == 0)
4277 *syms
= stash
->f
.syms
;
4284 /* Unset vmas for adjusted sections in STASH. */
4287 unset_sections (struct dwarf2_debug
*stash
)
4290 struct adjusted_section
*p
;
4292 i
= stash
->adjusted_section_count
;
4293 p
= stash
->adjusted_sections
;
4294 for (; i
> 0; i
--, p
++)
4295 p
->section
->vma
= 0;
4298 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4299 relocatable object file. VMAs are normally all zero in relocatable
4300 object files, so if we want to distinguish locations in sections by
4301 address we need to set VMAs so the sections do not overlap. We
4302 also set VMA on .debug_info so that when we have multiple
4303 .debug_info sections (or the linkonce variant) they also do not
4304 overlap. The multiple .debug_info sections make up a single
4305 logical section. ??? We should probably do the same for other
4309 place_sections (bfd
*orig_bfd
, struct dwarf2_debug
*stash
)
4312 struct adjusted_section
*p
;
4314 const char *debug_info_name
;
4316 if (stash
->adjusted_section_count
!= 0)
4318 i
= stash
->adjusted_section_count
;
4319 p
= stash
->adjusted_sections
;
4320 for (; i
> 0; i
--, p
++)
4321 p
->section
->vma
= p
->adj_vma
;
4325 debug_info_name
= stash
->debug_sections
[debug_info
].uncompressed_name
;
4332 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4336 if ((sect
->output_section
!= NULL
4337 && sect
->output_section
!= sect
4338 && (sect
->flags
& SEC_DEBUGGING
) == 0)
4342 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
4343 || startswith (sect
->name
, GNU_LINKONCE_INFO
));
4345 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
4351 if (abfd
== stash
->f
.bfd_ptr
)
4353 abfd
= stash
->f
.bfd_ptr
;
4357 stash
->adjusted_section_count
= -1;
4360 bfd_vma last_vma
= 0, last_dwarf
= 0;
4361 size_t amt
= i
* sizeof (struct adjusted_section
);
4363 p
= (struct adjusted_section
*) bfd_malloc (amt
);
4367 stash
->adjusted_sections
= p
;
4368 stash
->adjusted_section_count
= i
;
4375 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4380 if ((sect
->output_section
!= NULL
4381 && sect
->output_section
!= sect
4382 && (sect
->flags
& SEC_DEBUGGING
) == 0)
4386 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
4387 || startswith (sect
->name
, GNU_LINKONCE_INFO
));
4389 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
4393 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
4397 BFD_ASSERT (sect
->alignment_power
== 0);
4398 sect
->vma
= last_dwarf
;
4403 /* Align the new address to the current section
4405 last_vma
= ((last_vma
4406 + ~(-((bfd_vma
) 1 << sect
->alignment_power
)))
4407 & (-((bfd_vma
) 1 << sect
->alignment_power
)));
4408 sect
->vma
= last_vma
;
4413 p
->adj_vma
= sect
->vma
;
4416 if (abfd
== stash
->f
.bfd_ptr
)
4418 abfd
= stash
->f
.bfd_ptr
;
4422 if (orig_bfd
!= stash
->f
.bfd_ptr
)
4423 set_debug_vma (orig_bfd
, stash
->f
.bfd_ptr
);
4428 /* Look up a funcinfo by name using the given info hash table. If found,
4429 also update the locations pointed to by filename_ptr and linenumber_ptr.
4431 This function returns TRUE if a funcinfo that matches the given symbol
4432 and address is found with any error; otherwise it returns FALSE. */
4435 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
4438 const char **filename_ptr
,
4439 unsigned int *linenumber_ptr
)
4441 struct funcinfo
* each_func
;
4442 struct funcinfo
* best_fit
= NULL
;
4443 bfd_vma best_fit_len
= 0;
4444 struct info_list_node
*node
;
4445 struct arange
*arange
;
4446 const char *name
= bfd_asymbol_name (sym
);
4447 asection
*sec
= bfd_asymbol_section (sym
);
4449 for (node
= lookup_info_hash_table (hash_table
, name
);
4453 each_func
= (struct funcinfo
*) node
->info
;
4454 for (arange
= &each_func
->arange
;
4456 arange
= arange
->next
)
4458 if ((!each_func
->sec
|| each_func
->sec
== sec
)
4459 && addr
>= arange
->low
4460 && addr
< arange
->high
4462 || arange
->high
- arange
->low
< best_fit_len
))
4464 best_fit
= each_func
;
4465 best_fit_len
= arange
->high
- arange
->low
;
4472 best_fit
->sec
= sec
;
4473 *filename_ptr
= best_fit
->file
;
4474 *linenumber_ptr
= best_fit
->line
;
4481 /* Look up a varinfo by name using the given info hash table. If found,
4482 also update the locations pointed to by filename_ptr and linenumber_ptr.
4484 This function returns TRUE if a varinfo that matches the given symbol
4485 and address is found with any error; otherwise it returns FALSE. */
4488 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
4491 const char **filename_ptr
,
4492 unsigned int *linenumber_ptr
)
4494 const char *name
= bfd_asymbol_name (sym
);
4495 asection
*sec
= bfd_asymbol_section (sym
);
4496 struct varinfo
* each
;
4497 struct info_list_node
*node
;
4499 for (node
= lookup_info_hash_table (hash_table
, name
);
4503 each
= (struct varinfo
*) node
->info
;
4504 if (each
->addr
== addr
4505 && (!each
->sec
|| each
->sec
== sec
))
4508 *filename_ptr
= each
->file
;
4509 *linenumber_ptr
= each
->line
;
4517 /* Update the funcinfo and varinfo info hash tables if they are
4518 not up to date. Returns TRUE if there is no error; otherwise
4519 returns FALSE and disable the info hash tables. */
4522 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
4524 struct comp_unit
*each
;
4526 /* Exit if hash tables are up-to-date. */
4527 if (stash
->f
.all_comp_units
== stash
->hash_units_head
)
4530 if (stash
->hash_units_head
)
4531 each
= stash
->hash_units_head
->prev_unit
;
4533 each
= stash
->f
.last_comp_unit
;
4537 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
4538 stash
->varinfo_hash_table
))
4540 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
4543 each
= each
->prev_unit
;
4546 stash
->hash_units_head
= stash
->f
.all_comp_units
;
4550 /* Check consistency of info hash tables. This is for debugging only. */
4552 static void ATTRIBUTE_UNUSED
4553 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
4555 struct comp_unit
*each_unit
;
4556 struct funcinfo
*each_func
;
4557 struct varinfo
*each_var
;
4558 struct info_list_node
*node
;
4561 for (each_unit
= stash
->f
.all_comp_units
;
4563 each_unit
= each_unit
->next_unit
)
4565 for (each_func
= each_unit
->function_table
;
4567 each_func
= each_func
->prev_func
)
4569 if (!each_func
->name
)
4571 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
4575 while (node
&& !found
)
4577 found
= node
->info
== each_func
;
4583 for (each_var
= each_unit
->variable_table
;
4585 each_var
= each_var
->prev_var
)
4587 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
4589 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
4593 while (node
&& !found
)
4595 found
= node
->info
== each_var
;
4603 /* Check to see if we want to enable the info hash tables, which consume
4604 quite a bit of memory. Currently we only check the number times
4605 bfd_dwarf2_find_line is called. In the future, we may also want to
4606 take the number of symbols into account. */
4609 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
4611 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
4613 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
4616 /* FIXME: Maybe we should check the reduce_memory_overheads
4617 and optimize fields in the bfd_link_info structure ? */
4619 /* Create hash tables. */
4620 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
4621 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
4622 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
4624 /* Turn off info hashes if any allocation above fails. */
4625 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
4628 /* We need a forced update so that the info hash tables will
4629 be created even though there is no compilation unit. That
4630 happens if STASH_INFO_HASH_TRIGGER is 0. */
4631 if (stash_maybe_update_info_hash_tables (stash
))
4632 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
4635 /* Find the file and line associated with a symbol and address using the
4636 info hash tables of a stash. If there is a match, the function returns
4637 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
4638 otherwise it returns FALSE. */
4641 stash_find_line_fast (struct dwarf2_debug
*stash
,
4644 const char **filename_ptr
,
4645 unsigned int *linenumber_ptr
)
4647 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
4649 if (sym
->flags
& BSF_FUNCTION
)
4650 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
4651 filename_ptr
, linenumber_ptr
);
4652 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
4653 filename_ptr
, linenumber_ptr
);
4656 /* Save current section VMAs. */
4659 save_section_vma (const bfd
*abfd
, struct dwarf2_debug
*stash
)
4664 if (abfd
->section_count
== 0)
4666 stash
->sec_vma
= bfd_malloc (sizeof (*stash
->sec_vma
) * abfd
->section_count
);
4667 if (stash
->sec_vma
== NULL
)
4669 stash
->sec_vma_count
= abfd
->section_count
;
4670 for (i
= 0, s
= abfd
->sections
;
4671 s
!= NULL
&& i
< abfd
->section_count
;
4674 if (s
->output_section
!= NULL
)
4675 stash
->sec_vma
[i
] = s
->output_section
->vma
+ s
->output_offset
;
4677 stash
->sec_vma
[i
] = s
->vma
;
4682 /* Compare current section VMAs against those at the time the stash
4683 was created. If find_nearest_line is used in linker warnings or
4684 errors early in the link process, the debug info stash will be
4685 invalid for later calls. This is because we relocate debug info
4686 sections, so the stashed section contents depend on symbol values,
4687 which in turn depend on section VMAs. */
4690 section_vma_same (const bfd
*abfd
, const struct dwarf2_debug
*stash
)
4695 /* PR 24334: If the number of sections in ABFD has changed between
4696 when the stash was created and now, then we cannot trust the
4697 stashed vma information. */
4698 if (abfd
->section_count
!= stash
->sec_vma_count
)
4701 for (i
= 0, s
= abfd
->sections
;
4702 s
!= NULL
&& i
< abfd
->section_count
;
4707 if (s
->output_section
!= NULL
)
4708 vma
= s
->output_section
->vma
+ s
->output_offset
;
4711 if (vma
!= stash
->sec_vma
[i
])
4717 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
4718 If DEBUG_BFD is not specified, we read debug information from ABFD
4719 or its gnu_debuglink. The results will be stored in PINFO.
4720 The function returns TRUE iff debug information is ready. */
4723 _bfd_dwarf2_slurp_debug_info (bfd
*abfd
, bfd
*debug_bfd
,
4724 const struct dwarf_debug_section
*debug_sections
,
4729 size_t amt
= sizeof (struct dwarf2_debug
);
4730 bfd_size_type total_size
;
4732 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
4736 if (stash
->orig_bfd
== abfd
4737 && section_vma_same (abfd
, stash
))
4739 /* Check that we did previously find some debug information
4740 before attempting to make use of it. */
4741 if (stash
->f
.bfd_ptr
!= NULL
)
4743 if (do_place
&& !place_sections (abfd
, stash
))
4750 _bfd_dwarf2_cleanup_debug_info (abfd
, pinfo
);
4751 memset (stash
, 0, amt
);
4755 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
4759 stash
->orig_bfd
= abfd
;
4760 stash
->debug_sections
= debug_sections
;
4761 stash
->f
.syms
= symbols
;
4762 if (!save_section_vma (abfd
, stash
))
4765 stash
->f
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
4766 del_abbrev
, calloc
, free
);
4767 if (!stash
->f
.abbrev_offsets
)
4770 stash
->alt
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
4771 del_abbrev
, calloc
, free
);
4772 if (!stash
->alt
.abbrev_offsets
)
4777 if (debug_bfd
== NULL
)
4780 msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4781 if (msec
== NULL
&& abfd
== debug_bfd
)
4783 char * debug_filename
;
4785 debug_filename
= bfd_follow_build_id_debuglink (abfd
, DEBUGDIR
);
4786 if (debug_filename
== NULL
)
4787 debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
4789 if (debug_filename
== NULL
)
4790 /* No dwarf2 info, and no gnu_debuglink to follow.
4791 Note that at this point the stash has been allocated, but
4792 contains zeros. This lets future calls to this function
4793 fail more quickly. */
4796 debug_bfd
= bfd_openr (debug_filename
, NULL
);
4797 free (debug_filename
);
4798 if (debug_bfd
== NULL
)
4799 /* FIXME: Should we report our failure to follow the debuglink ? */
4802 /* Set BFD_DECOMPRESS to decompress debug sections. */
4803 debug_bfd
->flags
|= BFD_DECOMPRESS
;
4804 if (!bfd_check_format (debug_bfd
, bfd_object
)
4805 || (msec
= find_debug_info (debug_bfd
,
4806 debug_sections
, NULL
)) == NULL
4807 || !bfd_generic_link_read_symbols (debug_bfd
))
4809 bfd_close (debug_bfd
);
4813 symbols
= bfd_get_outsymbols (debug_bfd
);
4814 stash
->f
.syms
= symbols
;
4815 stash
->close_on_cleanup
= true;
4817 stash
->f
.bfd_ptr
= debug_bfd
;
4820 && !place_sections (abfd
, stash
))
4823 /* There can be more than one DWARF2 info section in a BFD these
4824 days. First handle the easy case when there's only one. If
4825 there's more than one, try case two: none of the sections is
4826 compressed. In that case, read them all in and produce one
4827 large stash. We do this in two passes - in the first pass we
4828 just accumulate the section sizes, and in the second pass we
4829 read in the section's contents. (The allows us to avoid
4830 reallocing the data as we add sections to the stash.) If
4831 some or all sections are compressed, then do things the slow
4832 way, with a bunch of reallocs. */
4834 if (! find_debug_info (debug_bfd
, debug_sections
, msec
))
4836 /* Case 1: only one info section. */
4837 total_size
= msec
->size
;
4838 if (! read_section (debug_bfd
, &stash
->debug_sections
[debug_info
],
4840 &stash
->f
.dwarf_info_buffer
, &total_size
))
4845 /* Case 2: multiple sections. */
4846 for (total_size
= 0;
4848 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
4850 /* Catch PR25070 testcase overflowing size calculation here. */
4851 if (total_size
+ msec
->size
< total_size
4852 || total_size
+ msec
->size
< msec
->size
)
4854 bfd_set_error (bfd_error_no_memory
);
4857 total_size
+= msec
->size
;
4860 stash
->f
.dwarf_info_buffer
= (bfd_byte
*) bfd_malloc (total_size
);
4861 if (stash
->f
.dwarf_info_buffer
== NULL
)
4865 for (msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4867 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
4875 if (!(bfd_simple_get_relocated_section_contents
4876 (debug_bfd
, msec
, stash
->f
.dwarf_info_buffer
+ total_size
,
4884 stash
->f
.info_ptr
= stash
->f
.dwarf_info_buffer
;
4885 stash
->f
.dwarf_info_size
= total_size
;
4889 /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
4891 static struct comp_unit
*
4892 stash_comp_unit (struct dwarf2_debug
*stash
, struct dwarf2_debug_file
*file
)
4894 bfd_size_type length
;
4895 unsigned int offset_size
;
4896 bfd_byte
*info_ptr_unit
= file
->info_ptr
;
4897 bfd_byte
*info_ptr_end
= file
->dwarf_info_buffer
+ file
->dwarf_info_size
;
4899 if (file
->info_ptr
>= info_ptr_end
)
4902 length
= read_4_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
4903 /* A 0xffffff length is the DWARF3 way of indicating
4904 we use 64-bit offsets, instead of 32-bit offsets. */
4905 if (length
== 0xffffffff)
4908 length
= read_8_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
4910 /* A zero length is the IRIX way of indicating 64-bit offsets,
4911 mostly because the 64-bit length will generally fit in 32
4912 bits, and the endianness helps. */
4913 else if (length
== 0)
4916 length
= read_4_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
4918 /* In the absence of the hints above, we assume 32-bit DWARF2
4919 offsets even for targets with 64-bit addresses, because:
4920 a) most of the time these targets will not have generated
4921 more than 2Gb of debug info and so will not need 64-bit
4924 b) if they do use 64-bit offsets but they are not using
4925 the size hints that are tested for above then they are
4926 not conforming to the DWARF3 standard anyway. */
4931 && length
<= (size_t) (info_ptr_end
- file
->info_ptr
))
4933 struct comp_unit
*each
= parse_comp_unit (stash
, file
,
4934 file
->info_ptr
, length
,
4935 info_ptr_unit
, offset_size
);
4938 if (file
->all_comp_units
)
4939 file
->all_comp_units
->prev_unit
= each
;
4941 file
->last_comp_unit
= each
;
4943 each
->next_unit
= file
->all_comp_units
;
4944 file
->all_comp_units
= each
;
4946 file
->info_ptr
+= length
;
4951 /* Don't trust any of the DWARF info after a corrupted length or
4953 file
->info_ptr
= info_ptr_end
;
4957 /* Hash function for an asymbol. */
4960 hash_asymbol (const void *sym
)
4962 const asymbol
*asym
= sym
;
4963 return htab_hash_string (asym
->name
);
4966 /* Equality function for asymbols. */
4969 eq_asymbol (const void *a
, const void *b
)
4971 const asymbol
*sa
= a
;
4972 const asymbol
*sb
= b
;
4973 return strcmp (sa
->name
, sb
->name
) == 0;
4976 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
4977 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
4978 symbol in SYMBOLS and return the difference between the low_pc and
4979 the symbol's address. Returns 0 if no suitable symbol could be found. */
4982 _bfd_dwarf2_find_symbol_bias (asymbol
** symbols
, void ** pinfo
)
4984 struct dwarf2_debug
*stash
;
4985 struct comp_unit
* unit
;
4987 bfd_signed_vma result
= 0;
4990 stash
= (struct dwarf2_debug
*) *pinfo
;
4992 if (stash
== NULL
|| symbols
== NULL
)
4995 sym_hash
= htab_create_alloc (10, hash_asymbol
, eq_asymbol
,
4996 NULL
, xcalloc
, free
);
4997 for (psym
= symbols
; * psym
!= NULL
; psym
++)
4999 asymbol
* sym
= * psym
;
5001 if (sym
->flags
& BSF_FUNCTION
&& sym
->section
!= NULL
)
5003 void **slot
= htab_find_slot (sym_hash
, sym
, INSERT
);
5008 for (unit
= stash
->f
.all_comp_units
; unit
; unit
= unit
->next_unit
)
5010 struct funcinfo
* func
;
5012 comp_unit_maybe_decode_line_info (unit
);
5014 for (func
= unit
->function_table
; func
!= NULL
; func
= func
->prev_func
)
5015 if (func
->name
&& func
->arange
.low
)
5017 asymbol search
, *sym
;
5019 /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
5021 search
.name
= func
->name
;
5022 sym
= htab_find (sym_hash
, &search
);
5025 result
= ((bfd_signed_vma
) func
->arange
.low
) -
5026 ((bfd_signed_vma
) (sym
->value
+ sym
->section
->vma
));
5033 htab_delete (sym_hash
);
5037 /* Find the source code location of SYMBOL. If SYMBOL is NULL
5038 then find the nearest source code location corresponding to
5039 the address SECTION + OFFSET.
5040 Returns 1 if the line is found without error and fills in
5041 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
5042 NULL the FUNCTIONNAME_PTR is also filled in.
5043 Returns 2 if partial information from _bfd_elf_find_function is
5044 returned (function and maybe file) by looking at symbols. DWARF2
5045 info is present but not regarding the requested code location.
5046 Returns 0 otherwise.
5047 SYMBOLS contains the symbol table for ABFD.
5048 DEBUG_SECTIONS contains the name of the dwarf debug sections. */
5051 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
5056 const char **filename_ptr
,
5057 const char **functionname_ptr
,
5058 unsigned int *linenumber_ptr
,
5059 unsigned int *discriminator_ptr
,
5060 const struct dwarf_debug_section
*debug_sections
,
5063 /* Read each compilation unit from the section .debug_info, and check
5064 to see if it contains the address we are searching for. If yes,
5065 lookup the address, and return the line number info. If no, go
5066 on to the next compilation unit.
5068 We keep a list of all the previously read compilation units, and
5069 a pointer to the next un-read compilation unit. Check the
5070 previously read units before reading more. */
5071 struct dwarf2_debug
*stash
;
5072 /* What address are we looking for? */
5074 struct comp_unit
* each
;
5075 struct funcinfo
*function
= NULL
;
5079 *filename_ptr
= NULL
;
5080 if (functionname_ptr
!= NULL
)
5081 *functionname_ptr
= NULL
;
5082 *linenumber_ptr
= 0;
5083 if (discriminator_ptr
)
5084 *discriminator_ptr
= 0;
5086 if (! _bfd_dwarf2_slurp_debug_info (abfd
, NULL
, debug_sections
,
5088 (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0))
5091 stash
= (struct dwarf2_debug
*) *pinfo
;
5093 do_line
= symbol
!= NULL
;
5096 BFD_ASSERT (section
== NULL
&& offset
== 0 && functionname_ptr
== NULL
);
5097 section
= bfd_asymbol_section (symbol
);
5098 addr
= symbol
->value
;
5102 BFD_ASSERT (section
!= NULL
&& functionname_ptr
!= NULL
);
5105 /* If we have no SYMBOL but the section we're looking at is not a
5106 code section, then take a look through the list of symbols to see
5107 if we have a symbol at the address we're looking for. If we do
5108 then use this to look up line information. This will allow us to
5109 give file and line results for data symbols. We exclude code
5110 symbols here, if we look up a function symbol and then look up the
5111 line information we'll actually return the line number for the
5112 opening '{' rather than the function definition line. This is
5113 because looking up by symbol uses the line table, in which the
5114 first line for a function is usually the opening '{', while
5115 looking up the function by section + offset uses the
5116 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
5117 which will be the line of the function name. */
5118 if (symbols
!= NULL
&& (section
->flags
& SEC_CODE
) == 0)
5122 for (tmp
= symbols
; (*tmp
) != NULL
; ++tmp
)
5123 if ((*tmp
)->the_bfd
== abfd
5124 && (*tmp
)->section
== section
5125 && (*tmp
)->value
== offset
5126 && ((*tmp
)->flags
& BSF_SECTION_SYM
) == 0)
5130 /* For local symbols, keep going in the hope we find a
5132 if ((symbol
->flags
& BSF_GLOBAL
) != 0)
5138 if (section
->output_section
)
5139 addr
+= section
->output_section
->vma
+ section
->output_offset
;
5141 addr
+= section
->vma
;
5143 /* A null info_ptr indicates that there is no dwarf2 info
5144 (or that an error occured while setting up the stash). */
5145 if (! stash
->f
.info_ptr
)
5148 stash
->inliner_chain
= NULL
;
5150 /* Check the previously read comp. units first. */
5153 /* The info hash tables use quite a bit of memory. We may not want to
5154 always use them. We use some heuristics to decide if and when to
5156 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
5157 stash_maybe_enable_info_hash_tables (abfd
, stash
);
5159 /* Keep info hash table up to date if they are available. Note that we
5160 may disable the hash tables if there is any error duing update. */
5161 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
5162 stash_maybe_update_info_hash_tables (stash
);
5164 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
5166 found
= stash_find_line_fast (stash
, symbol
, addr
, filename_ptr
,
5173 /* Check the previously read comp. units first. */
5174 for (each
= stash
->f
.all_comp_units
; each
; each
= each
->next_unit
)
5175 if ((symbol
->flags
& BSF_FUNCTION
) == 0
5176 || each
->arange
.high
== 0
5177 || comp_unit_contains_address (each
, addr
))
5179 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
5188 for (each
= stash
->f
.all_comp_units
; each
; each
= each
->next_unit
)
5190 found
= ((each
->arange
.high
== 0
5191 || comp_unit_contains_address (each
, addr
))
5192 && comp_unit_find_nearest_line (each
, addr
,
5196 discriminator_ptr
));
5202 /* Read each remaining comp. units checking each as they are read. */
5203 while ((each
= stash_comp_unit (stash
, &stash
->f
)) != NULL
)
5205 /* DW_AT_low_pc and DW_AT_high_pc are optional for
5206 compilation units. If we don't have them (i.e.,
5207 unit->high == 0), we need to consult the line info table
5208 to see if a compilation unit contains the given
5211 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
5212 || each
->arange
.high
== 0
5213 || comp_unit_contains_address (each
, addr
))
5214 && comp_unit_find_line (each
, symbol
, addr
,
5215 filename_ptr
, linenumber_ptr
));
5217 found
= ((each
->arange
.high
== 0
5218 || comp_unit_contains_address (each
, addr
))
5219 && comp_unit_find_nearest_line (each
, addr
,
5223 discriminator_ptr
));
5230 if (functionname_ptr
&& function
&& function
->is_linkage
)
5232 *functionname_ptr
= function
->name
;
5236 else if (functionname_ptr
5237 && (!*functionname_ptr
5238 || (function
&& !function
->is_linkage
)))
5241 asymbol
**syms
= symbols
;
5242 asection
*sec
= section
;
5244 _bfd_dwarf2_stash_syms (stash
, abfd
, &sec
, &syms
);
5245 fun
= _bfd_elf_find_function (abfd
, syms
, sec
, offset
,
5246 *filename_ptr
? NULL
: filename_ptr
,
5249 if (!found
&& fun
!= NULL
)
5252 if (function
&& !function
->is_linkage
)
5256 sec_vma
= section
->vma
;
5257 if (section
->output_section
!= NULL
)
5258 sec_vma
= section
->output_section
->vma
+ section
->output_offset
;
5260 *functionname_ptr
= function
->name
;
5261 else if (fun
->value
+ sec_vma
== function
->arange
.low
)
5262 function
->name
= *functionname_ptr
;
5263 /* Even if we didn't find a linkage name, say that we have
5264 to stop a repeated search of symbols. */
5265 function
->is_linkage
= true;
5269 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
5270 unset_sections (stash
);
5276 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
5277 const char **filename_ptr
,
5278 const char **functionname_ptr
,
5279 unsigned int *linenumber_ptr
,
5282 struct dwarf2_debug
*stash
;
5284 stash
= (struct dwarf2_debug
*) *pinfo
;
5287 struct funcinfo
*func
= stash
->inliner_chain
;
5289 if (func
&& func
->caller_func
)
5291 *filename_ptr
= func
->caller_file
;
5292 *functionname_ptr
= func
->caller_func
->name
;
5293 *linenumber_ptr
= func
->caller_line
;
5294 stash
->inliner_chain
= func
->caller_func
;
5303 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
, void **pinfo
)
5305 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
5306 struct comp_unit
*each
;
5307 struct dwarf2_debug_file
*file
;
5309 if (abfd
== NULL
|| stash
== NULL
)
5312 if (stash
->varinfo_hash_table
)
5313 bfd_hash_table_free (&stash
->varinfo_hash_table
->base
);
5314 if (stash
->funcinfo_hash_table
)
5315 bfd_hash_table_free (&stash
->funcinfo_hash_table
->base
);
5320 for (each
= file
->all_comp_units
; each
; each
= each
->next_unit
)
5322 struct funcinfo
*function_table
= each
->function_table
;
5323 struct varinfo
*variable_table
= each
->variable_table
;
5325 if (each
->line_table
&& each
->line_table
!= file
->line_table
)
5327 free (each
->line_table
->files
);
5328 free (each
->line_table
->dirs
);
5331 free (each
->lookup_funcinfo_table
);
5332 each
->lookup_funcinfo_table
= NULL
;
5334 while (function_table
)
5336 free (function_table
->file
);
5337 function_table
->file
= NULL
;
5338 free (function_table
->caller_file
);
5339 function_table
->caller_file
= NULL
;
5340 function_table
= function_table
->prev_func
;
5343 while (variable_table
)
5345 free (variable_table
->file
);
5346 variable_table
->file
= NULL
;
5347 variable_table
= variable_table
->prev_var
;
5351 if (file
->line_table
)
5353 free (file
->line_table
->files
);
5354 free (file
->line_table
->dirs
);
5356 htab_delete (file
->abbrev_offsets
);
5358 free (file
->dwarf_line_str_buffer
);
5359 free (file
->dwarf_str_buffer
);
5360 free (file
->dwarf_ranges_buffer
);
5361 free (file
->dwarf_line_buffer
);
5362 free (file
->dwarf_abbrev_buffer
);
5363 free (file
->dwarf_info_buffer
);
5364 if (file
== &stash
->alt
)
5368 free (stash
->sec_vma
);
5369 free (stash
->adjusted_sections
);
5370 if (stash
->close_on_cleanup
)
5371 bfd_close (stash
->f
.bfd_ptr
);
5372 if (stash
->alt
.bfd_ptr
)
5373 bfd_close (stash
->alt
.bfd_ptr
);
5376 /* Find the function to a particular section and offset,
5377 for error reporting. */
5380 _bfd_elf_find_function (bfd
*abfd
,
5384 const char **filename_ptr
,
5385 const char **functionname_ptr
)
5387 struct elf_find_function_cache
5389 asection
*last_section
;
5391 const char *filename
;
5392 bfd_size_type func_size
;
5395 if (symbols
== NULL
)
5398 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
5401 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
5404 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
5405 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
5409 if (cache
->last_section
!= section
5410 || cache
->func
== NULL
5411 || offset
< cache
->func
->value
5412 || offset
>= cache
->func
->value
+ cache
->func_size
)
5417 /* ??? Given multiple file symbols, it is impossible to reliably
5418 choose the right file name for global symbols. File symbols are
5419 local symbols, and thus all file symbols must sort before any
5420 global symbols. The ELF spec may be interpreted to say that a
5421 file symbol must sort before other local symbols, but currently
5422 ld -r doesn't do this. So, for ld -r output, it is possible to
5423 make a better choice of file name for local symbols by ignoring
5424 file symbols appearing after a given local symbol. */
5425 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
5426 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5430 state
= nothing_seen
;
5431 cache
->filename
= NULL
;
5433 cache
->func_size
= 0;
5434 cache
->last_section
= section
;
5436 for (p
= symbols
; *p
!= NULL
; p
++)
5442 if ((sym
->flags
& BSF_FILE
) != 0)
5445 if (state
== symbol_seen
)
5446 state
= file_after_symbol_seen
;
5450 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
5452 && code_off
<= offset
5453 && (code_off
> low_func
5454 || (code_off
== low_func
5455 && size
> cache
->func_size
)))
5458 cache
->func_size
= size
;
5459 cache
->filename
= NULL
;
5460 low_func
= code_off
;
5462 && ((sym
->flags
& BSF_LOCAL
) != 0
5463 || state
!= file_after_symbol_seen
))
5464 cache
->filename
= bfd_asymbol_name (file
);
5466 if (state
== nothing_seen
)
5467 state
= symbol_seen
;
5471 if (cache
->func
== NULL
)
5475 *filename_ptr
= cache
->filename
;
5476 if (functionname_ptr
)
5477 *functionname_ptr
= bfd_asymbol_name (cache
->func
);