2 Copyright (C) 1994-2023 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 #include "splay-tree.h"
42 /* The data in the .debug_line statement prologue looks like this. */
47 unsigned short version
;
48 bfd_vma prologue_length
;
49 unsigned char minimum_instruction_length
;
50 unsigned char maximum_ops_per_insn
;
51 unsigned char default_is_stmt
;
53 unsigned char line_range
;
54 unsigned char opcode_base
;
55 unsigned char *standard_opcode_lengths
;
58 /* Attributes have a name and a value. */
62 enum dwarf_attribute name
;
67 struct dwarf_block
*blk
;
74 /* Blocks are a bunch of untyped bytes. */
81 struct adjusted_section
88 /* A trie to map quickly from address range to compilation unit.
90 This is a fairly standard radix-256 trie, used to quickly locate which
91 compilation unit any given address belongs to. Given that each compilation
92 unit may register hundreds of very small and unaligned ranges (which may
93 potentially overlap, due to inlining and other concerns), and a large
94 program may end up containing hundreds of thousands of such ranges, we cannot
95 scan through them linearly without undue slowdown.
97 We use a hybrid trie to avoid memory explosion: There are two types of trie
98 nodes, leaves and interior nodes. (Almost all nodes are leaves, so they
99 take up the bulk of the memory usage.) Leaves contain a simple array of
100 ranges (high/low address) and which compilation unit contains those ranges,
101 and when we get to a leaf, we scan through it linearly. Interior nodes
102 contain pointers to 256 other nodes, keyed by the next byte of the address.
103 So for a 64-bit address like 0x1234567abcd, we would start at the root and go
104 down child[0x00]->child[0x00]->child[0x01]->child[0x23]->child[0x45] etc.,
105 until we hit a leaf. (Nodes are, in general, leaves until they exceed the
106 default allocation of 16 elements, at which point they are converted to
107 interior node if possible.) This gives us near-constant lookup times;
108 the only thing that can be costly is if there are lots of overlapping ranges
109 within a single 256-byte segment of the binary, in which case we have to
110 scan through them all to find the best match.
112 For a binary with few ranges, we will in practice only have a single leaf
113 node at the root, containing a simple array. Thus, the scheme is efficient
114 for both small and large binaries.
117 /* Experiments have shown 16 to be a memory-efficient default leaf size.
118 The only case where a leaf will hold more memory than this, is at the
119 bottomost level (covering 256 bytes in the binary), where we'll expand
120 the leaf to be able to hold more ranges if needed.
122 #define TRIE_LEAF_SIZE 16
124 /* All trie_node pointers will really be trie_leaf or trie_interior,
125 but they have this common head. */
128 /* If zero, we are an interior node.
129 Otherwise, how many ranges we have room for in this leaf. */
130 unsigned int num_room_in_leaf
;
135 struct trie_node head
;
136 unsigned int num_stored_in_leaf
;
138 struct comp_unit
*unit
;
139 bfd_vma low_pc
, high_pc
;
140 } ranges
[TRIE_LEAF_SIZE
];
145 struct trie_node head
;
146 struct trie_node
*children
[256];
149 static struct trie_node
*alloc_trie_leaf (bfd
*abfd
)
151 struct trie_leaf
*leaf
= bfd_zalloc (abfd
, sizeof (struct trie_leaf
));
154 leaf
->head
.num_room_in_leaf
= TRIE_LEAF_SIZE
;
164 /* Return true if address range do intersect. */
167 addr_range_intersects (struct addr_range
*r1
, struct addr_range
*r2
)
169 return (r1
->start
<= r2
->start
&& r2
->start
< r1
->end
)
170 || (r1
->start
<= (r2
->end
- 1) && (r2
->end
- 1) < r1
->end
);
173 /* Compare function for splay tree of addr_ranges. */
176 splay_tree_compare_addr_range (splay_tree_key xa
, splay_tree_key xb
)
178 struct addr_range
*r1
= (struct addr_range
*) xa
;
179 struct addr_range
*r2
= (struct addr_range
*) xb
;
181 if (addr_range_intersects (r1
, r2
) || addr_range_intersects (r2
, r1
))
183 else if (r1
->end
<= r2
->start
)
189 /* Splay tree release function for keys (addr_range). */
192 splay_tree_free_addr_range (splay_tree_key key
)
194 free ((struct addr_range
*)key
);
197 struct dwarf2_debug_file
199 /* The actual bfd from which debug info was loaded. Might be
200 different to orig_bfd because of gnu_debuglink sections. */
203 /* Pointer to the symbol table. */
206 /* The current info pointer for the .debug_info section being parsed. */
209 /* A pointer to the memory block allocated for .debug_info sections. */
210 bfd_byte
*dwarf_info_buffer
;
212 /* Length of the loaded .debug_info sections. */
213 bfd_size_type dwarf_info_size
;
215 /* Pointer to the .debug_abbrev section loaded into memory. */
216 bfd_byte
*dwarf_abbrev_buffer
;
218 /* Length of the loaded .debug_abbrev section. */
219 bfd_size_type dwarf_abbrev_size
;
221 /* Buffer for decode_line_info. */
222 bfd_byte
*dwarf_line_buffer
;
224 /* Length of the loaded .debug_line section. */
225 bfd_size_type dwarf_line_size
;
227 /* Pointer to the .debug_str section loaded into memory. */
228 bfd_byte
*dwarf_str_buffer
;
230 /* Length of the loaded .debug_str section. */
231 bfd_size_type dwarf_str_size
;
233 /* Pointer to the .debug_str_offsets section loaded into memory. */
234 bfd_byte
*dwarf_str_offsets_buffer
;
236 /* Length of the loaded .debug_str_offsets section. */
237 bfd_size_type dwarf_str_offsets_size
;
239 /* Pointer to the .debug_addr section loaded into memory. */
240 bfd_byte
*dwarf_addr_buffer
;
242 /* Length of the loaded .debug_addr section. */
243 bfd_size_type dwarf_addr_size
;
245 /* Pointer to the .debug_line_str section loaded into memory. */
246 bfd_byte
*dwarf_line_str_buffer
;
248 /* Length of the loaded .debug_line_str section. */
249 bfd_size_type dwarf_line_str_size
;
251 /* Pointer to the .debug_ranges section loaded into memory. */
252 bfd_byte
*dwarf_ranges_buffer
;
254 /* Length of the loaded .debug_ranges section. */
255 bfd_size_type dwarf_ranges_size
;
257 /* Pointer to the .debug_rnglists section loaded into memory. */
258 bfd_byte
*dwarf_rnglists_buffer
;
260 /* Length of the loaded .debug_rnglists section. */
261 bfd_size_type dwarf_rnglists_size
;
263 /* A list of all previously read comp_units. */
264 struct comp_unit
*all_comp_units
;
266 /* A list of all previously read comp_units with no ranges (yet). */
267 struct comp_unit
*all_comp_units_without_ranges
;
269 /* Last comp unit in list above. */
270 struct comp_unit
*last_comp_unit
;
272 /* Line table at line_offset zero. */
273 struct line_info_table
*line_table
;
275 /* Hash table to map offsets to decoded abbrevs. */
276 htab_t abbrev_offsets
;
278 /* Root of a trie to map addresses to compilation units. */
279 struct trie_node
*trie_root
;
281 /* Splay tree to map info_ptr address to compilation units. */
282 splay_tree comp_unit_tree
;
287 /* Names of the debug sections. */
288 const struct dwarf_debug_section
*debug_sections
;
290 /* Per-file stuff. */
291 struct dwarf2_debug_file f
, alt
;
293 /* Pointer to the original bfd for which debug was loaded. This is what
294 we use to compare and so check that the cached debug data is still
295 valid - it saves having to possibly dereference the gnu_debuglink each
299 /* If the most recent call to bfd_find_nearest_line was given an
300 address in an inlined function, preserve a pointer into the
301 calling chain for subsequent calls to bfd_find_inliner_info to
303 struct funcinfo
*inliner_chain
;
305 /* Section VMAs at the time the stash was built. */
307 /* Number of sections in the SEC_VMA table. */
308 unsigned int sec_vma_count
;
310 /* Number of sections whose VMA we must adjust. */
311 int adjusted_section_count
;
313 /* Array of sections with adjusted VMA. */
314 struct adjusted_section
*adjusted_sections
;
316 /* Number of times find_line is called. This is used in
317 the heuristic for enabling the info hash tables. */
320 #define STASH_INFO_HASH_TRIGGER 100
322 /* Hash table mapping symbol names to function infos. */
323 struct info_hash_table
*funcinfo_hash_table
;
325 /* Hash table mapping symbol names to variable infos. */
326 struct info_hash_table
*varinfo_hash_table
;
328 /* Head of comp_unit list in the last hash table update. */
329 struct comp_unit
*hash_units_head
;
331 /* Status of info hash. */
332 int info_hash_status
;
333 #define STASH_INFO_HASH_OFF 0
334 #define STASH_INFO_HASH_ON 1
335 #define STASH_INFO_HASH_DISABLED 2
337 /* True if we opened bfd_ptr. */
338 bool close_on_cleanup
;
348 /* A minimal decoding of DWARF2 compilation units. We only decode
349 what's needed to get to the line number information. */
353 /* Chain the previously read compilation units. */
354 struct comp_unit
*next_unit
;
356 /* Chain the previously read compilation units that have no ranges yet.
357 We scan these separately when we have a trie over the ranges.
358 Unused if arange.high != 0. */
359 struct comp_unit
*next_unit_without_ranges
;
361 /* Likewise, chain the compilation unit read after this one.
362 The comp units are stored in reversed reading order. */
363 struct comp_unit
*prev_unit
;
365 /* Keep the bfd convenient (for memory allocation). */
368 /* The lowest and highest addresses contained in this compilation
369 unit as specified in the compilation unit header. */
370 struct arange arange
;
372 /* The DW_AT_name attribute (for error messages). */
375 /* The abbrev hash table. */
376 struct abbrev_info
**abbrevs
;
378 /* DW_AT_language. */
381 /* Note that an error was found by comp_unit_find_nearest_line. */
384 /* The DW_AT_comp_dir attribute. */
387 /* TRUE if there is a line number table associated with this comp. unit. */
390 /* Pointer to the current comp_unit so that we can find a given entry
392 bfd_byte
*info_ptr_unit
;
394 /* The offset into .debug_line of the line number table. */
395 unsigned long line_offset
;
397 /* Pointer to the first child die for the comp unit. */
398 bfd_byte
*first_child_die_ptr
;
400 /* The end of the comp unit. */
403 /* The decoded line number, NULL if not yet decoded. */
404 struct line_info_table
*line_table
;
406 /* A list of the functions found in this comp. unit. */
407 struct funcinfo
*function_table
;
409 /* A table of function information references searchable by address. */
410 struct lookup_funcinfo
*lookup_funcinfo_table
;
412 /* Number of functions in the function_table and sorted_function_table. */
413 bfd_size_type number_of_functions
;
415 /* A list of the variables found in this comp. unit. */
416 struct varinfo
*variable_table
;
418 /* Pointers to dwarf2_debug structures. */
419 struct dwarf2_debug
*stash
;
420 struct dwarf2_debug_file
*file
;
422 /* DWARF format version for this unit - from unit header. */
425 /* Address size for this unit - from unit header. */
426 unsigned char addr_size
;
428 /* Offset size for this unit - from unit header. */
429 unsigned char offset_size
;
431 /* Base address for this unit - from DW_AT_low_pc attribute of
432 DW_TAG_compile_unit DIE */
433 bfd_vma base_address
;
435 /* TRUE if symbols are cached in hash table for faster lookup by name. */
438 /* Used when iterating over trie leaves to know which units we have
439 already seen in this iteration. */
442 /* Base address of debug_addr section. */
443 size_t dwarf_addr_offset
;
445 /* Base address of string offset table. */
446 size_t dwarf_str_offset
;
449 /* This data structure holds the information of an abbrev. */
452 unsigned int number
; /* Number identifying abbrev. */
453 enum dwarf_tag tag
; /* DWARF tag. */
454 bool has_children
; /* TRUE if the abbrev has children. */
455 unsigned int num_attrs
; /* Number of attributes. */
456 struct attr_abbrev
* attrs
; /* An array of attribute descriptions. */
457 struct abbrev_info
* next
; /* Next in chain. */
462 enum dwarf_attribute name
;
463 enum dwarf_form form
;
464 bfd_vma implicit_const
;
467 /* Map of uncompressed DWARF debug section name to compressed one. It
468 is terminated by NULL uncompressed_name. */
470 const struct dwarf_debug_section dwarf_debug_sections
[] =
472 { ".debug_abbrev", ".zdebug_abbrev" },
473 { ".debug_aranges", ".zdebug_aranges" },
474 { ".debug_frame", ".zdebug_frame" },
475 { ".debug_info", ".zdebug_info" },
476 { ".debug_info", ".zdebug_info" },
477 { ".debug_line", ".zdebug_line" },
478 { ".debug_loc", ".zdebug_loc" },
479 { ".debug_macinfo", ".zdebug_macinfo" },
480 { ".debug_macro", ".zdebug_macro" },
481 { ".debug_pubnames", ".zdebug_pubnames" },
482 { ".debug_pubtypes", ".zdebug_pubtypes" },
483 { ".debug_ranges", ".zdebug_ranges" },
484 { ".debug_rnglists", ".zdebug_rnglist" },
485 { ".debug_static_func", ".zdebug_static_func" },
486 { ".debug_static_vars", ".zdebug_static_vars" },
487 { ".debug_str", ".zdebug_str", },
488 { ".debug_str", ".zdebug_str", },
489 { ".debug_str_offsets", ".zdebug_str_offsets", },
490 { ".debug_addr", ".zdebug_addr", },
491 { ".debug_line_str", ".zdebug_line_str", },
492 { ".debug_types", ".zdebug_types" },
493 /* GNU DWARF 1 extensions */
494 { ".debug_sfnames", ".zdebug_sfnames" },
495 { ".debug_srcinfo", ".zebug_srcinfo" },
496 /* SGI/MIPS DWARF 2 extensions */
497 { ".debug_funcnames", ".zdebug_funcnames" },
498 { ".debug_typenames", ".zdebug_typenames" },
499 { ".debug_varnames", ".zdebug_varnames" },
500 { ".debug_weaknames", ".zdebug_weaknames" },
504 /* NB/ Numbers in this enum must match up with indices
505 into the dwarf_debug_sections[] array above. */
506 enum dwarf_debug_section_enum
538 /* A static assertion. */
539 extern int dwarf_debug_section_assert
[ARRAY_SIZE (dwarf_debug_sections
)
540 == debug_max
+ 1 ? 1 : -1];
542 #ifndef ABBREV_HASH_SIZE
543 #define ABBREV_HASH_SIZE 121
545 #ifndef ATTR_ALLOC_CHUNK
546 #define ATTR_ALLOC_CHUNK 4
549 /* Variable and function hash tables. This is used to speed up look-up
550 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
551 In order to share code between variable and function infos, we use
552 a list of untyped pointer for all variable/function info associated with
553 a symbol. We waste a bit of memory for list with one node but that
554 simplifies the code. */
556 struct info_list_node
558 struct info_list_node
*next
;
562 /* Info hash entry. */
563 struct info_hash_entry
565 struct bfd_hash_entry root
;
566 struct info_list_node
*head
;
569 struct info_hash_table
571 struct bfd_hash_table base
;
574 /* Function to create a new entry in info hash table. */
576 static struct bfd_hash_entry
*
577 info_hash_table_newfunc (struct bfd_hash_entry
*entry
,
578 struct bfd_hash_table
*table
,
581 struct info_hash_entry
*ret
= (struct info_hash_entry
*) entry
;
583 /* Allocate the structure if it has not already been allocated by a
587 ret
= (struct info_hash_entry
*) bfd_hash_allocate (table
,
593 /* Call the allocation method of the base class. */
594 ret
= ((struct info_hash_entry
*)
595 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
597 /* Initialize the local fields here. */
601 return (struct bfd_hash_entry
*) ret
;
604 /* Function to create a new info hash table. It returns a pointer to the
605 newly created table or NULL if there is any error. We need abfd
606 solely for memory allocation. */
608 static struct info_hash_table
*
609 create_info_hash_table (bfd
*abfd
)
611 struct info_hash_table
*hash_table
;
613 hash_table
= ((struct info_hash_table
*)
614 bfd_alloc (abfd
, sizeof (struct info_hash_table
)));
618 if (!bfd_hash_table_init (&hash_table
->base
, info_hash_table_newfunc
,
619 sizeof (struct info_hash_entry
)))
621 bfd_release (abfd
, hash_table
);
628 /* Insert an info entry into an info hash table. We do not check of
629 duplicate entries. Also, the caller need to guarantee that the
630 right type of info in inserted as info is passed as a void* pointer.
631 This function returns true if there is no error. */
634 insert_info_hash_table (struct info_hash_table
*hash_table
,
639 struct info_hash_entry
*entry
;
640 struct info_list_node
*node
;
642 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
,
647 node
= (struct info_list_node
*) bfd_hash_allocate (&hash_table
->base
,
653 node
->next
= entry
->head
;
659 /* Look up an info entry list from an info hash table. Return NULL
662 static struct info_list_node
*
663 lookup_info_hash_table (struct info_hash_table
*hash_table
, const char *key
)
665 struct info_hash_entry
*entry
;
667 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
, key
,
669 return entry
? entry
->head
: NULL
;
672 /* Read a section into its appropriate place in the dwarf2_debug
673 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
674 not NULL, use bfd_simple_get_relocated_section_contents to read the
675 section contents, otherwise use bfd_get_section_contents. Fail if
676 the located section does not contain at least OFFSET bytes. */
679 read_section (bfd
*abfd
,
680 const struct dwarf_debug_section
*sec
,
683 bfd_byte
**section_buffer
,
684 bfd_size_type
*section_size
)
686 const char *section_name
= sec
->uncompressed_name
;
687 bfd_byte
*contents
= *section_buffer
;
689 /* The section may have already been read. */
690 if (contents
== NULL
)
695 msec
= bfd_get_section_by_name (abfd
, section_name
);
698 section_name
= sec
->compressed_name
;
699 msec
= bfd_get_section_by_name (abfd
, section_name
);
703 _bfd_error_handler (_("DWARF error: can't find %s section."),
704 sec
->uncompressed_name
);
705 bfd_set_error (bfd_error_bad_value
);
709 if ((msec
->flags
& SEC_HAS_CONTENTS
) == 0)
711 _bfd_error_handler (_("DWARF error: section %s has no contents"),
713 bfd_set_error (bfd_error_no_contents
);
717 if (_bfd_section_size_insane (abfd
, msec
))
720 _bfd_error_handler (_("DWARF error: section %s is too big"),
724 amt
= bfd_get_section_limit_octets (abfd
, msec
);
726 /* Paranoia - alloc one extra so that we can make sure a string
727 section is NUL terminated. */
731 /* Paranoia - this should never happen. */
732 bfd_set_error (bfd_error_no_memory
);
735 contents
= (bfd_byte
*) bfd_malloc (amt
);
736 if (contents
== NULL
)
739 ? !bfd_simple_get_relocated_section_contents (abfd
, msec
, contents
,
741 : !bfd_get_section_contents (abfd
, msec
, contents
, 0, *section_size
))
746 contents
[*section_size
] = 0;
747 *section_buffer
= contents
;
750 /* It is possible to get a bad value for the offset into the section
751 that the client wants. Validate it here to avoid trouble later. */
752 if (offset
!= 0 && offset
>= *section_size
)
754 /* xgettext: c-format */
755 _bfd_error_handler (_("DWARF error: offset (%" PRIu64
")"
756 " greater than or equal to %s size (%" PRIu64
")"),
757 (uint64_t) offset
, section_name
,
758 (uint64_t) *section_size
);
759 bfd_set_error (bfd_error_bad_value
);
766 /* Read dwarf information from a buffer. */
768 static inline uint64_t
769 read_n_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
, int n
)
771 bfd_byte
*buf
= *ptr
;
778 return bfd_get (n
* 8, abfd
, buf
);
782 read_1_byte (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
784 return read_n_bytes (abfd
, ptr
, end
, 1);
788 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
**ptr
, bfd_byte
*end
)
790 bfd_byte
*buf
= *ptr
;
797 return bfd_get_signed_8 (abfd
, buf
);
801 read_2_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
803 return read_n_bytes (abfd
, ptr
, end
, 2);
807 read_3_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
809 unsigned int val
= read_1_byte (abfd
, ptr
, end
);
811 val
|= read_1_byte (abfd
, ptr
, end
);
813 val
|= read_1_byte (abfd
, ptr
, end
);
814 if (bfd_little_endian (abfd
))
815 val
= (((val
>> 16) & 0xff)
817 | ((val
& 0xff) << 16));
822 read_4_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
824 return read_n_bytes (abfd
, ptr
, end
, 4);
828 read_8_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
830 return read_n_bytes (abfd
, ptr
, end
, 8);
833 static struct dwarf_block
*
834 read_blk (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
, size_t size
)
836 bfd_byte
*buf
= *ptr
;
837 struct dwarf_block
*block
;
839 block
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (*block
));
843 if (size
> (size_t) (end
- buf
))
858 /* Scans a NUL terminated string starting at *PTR, returning a pointer to it.
859 Bytes at or beyond BUF_END will not be read. Returns NULL if the
860 terminator is not found or if the string is empty. *PTR is
861 incremented over the bytes scanned, including the terminator. */
864 read_string (bfd_byte
**ptr
,
867 bfd_byte
*buf
= *ptr
;
870 while (buf
< buf_end
)
883 /* Reads an offset from *PTR and then locates the string at this offset
884 inside the debug string section. Returns a pointer to the string.
885 Increments *PTR by the number of bytes read for the offset. This
886 value is set even if the function fails. Bytes at or beyond
887 BUF_END will not be read. Returns NULL if there was a problem, or
888 if the string is empty. Does not check for NUL termination of the
892 read_indirect_string (struct comp_unit
*unit
,
897 struct dwarf2_debug
*stash
= unit
->stash
;
898 struct dwarf2_debug_file
*file
= unit
->file
;
901 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
907 if (unit
->offset_size
== 4)
908 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
910 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
912 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
914 &file
->dwarf_str_buffer
, &file
->dwarf_str_size
))
917 str
= (char *) file
->dwarf_str_buffer
+ offset
;
923 /* Like read_indirect_string but from .debug_line_str section. */
926 read_indirect_line_string (struct comp_unit
*unit
,
931 struct dwarf2_debug
*stash
= unit
->stash
;
932 struct dwarf2_debug_file
*file
= unit
->file
;
935 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
941 if (unit
->offset_size
== 4)
942 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
944 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
946 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_line_str
],
948 &file
->dwarf_line_str_buffer
,
949 &file
->dwarf_line_str_size
))
952 str
= (char *) file
->dwarf_line_str_buffer
+ offset
;
958 /* Like read_indirect_string but uses a .debug_str located in
959 an alternate file pointed to by the .gnu_debugaltlink section.
960 Used to impement DW_FORM_GNU_strp_alt. */
963 read_alt_indirect_string (struct comp_unit
*unit
,
968 struct dwarf2_debug
*stash
= unit
->stash
;
971 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
977 if (unit
->offset_size
== 4)
978 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
980 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
982 if (stash
->alt
.bfd_ptr
== NULL
)
985 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
987 if (debug_filename
== NULL
)
990 debug_bfd
= bfd_openr (debug_filename
, NULL
);
991 free (debug_filename
);
992 if (debug_bfd
== NULL
)
993 /* FIXME: Should we report our failure to follow the debuglink ? */
996 if (!bfd_check_format (debug_bfd
, bfd_object
))
998 bfd_close (debug_bfd
);
1001 stash
->alt
.bfd_ptr
= debug_bfd
;
1004 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
1005 stash
->debug_sections
+ debug_str_alt
,
1006 stash
->alt
.syms
, offset
,
1007 &stash
->alt
.dwarf_str_buffer
,
1008 &stash
->alt
.dwarf_str_size
))
1011 str
= (char *) stash
->alt
.dwarf_str_buffer
+ offset
;
1018 /* Resolve an alternate reference from UNIT at OFFSET.
1019 Returns a pointer into the loaded alternate CU upon success
1020 or NULL upon failure. */
1023 read_alt_indirect_ref (struct comp_unit
*unit
, uint64_t offset
)
1025 struct dwarf2_debug
*stash
= unit
->stash
;
1027 if (stash
->alt
.bfd_ptr
== NULL
)
1030 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
1032 if (debug_filename
== NULL
)
1035 debug_bfd
= bfd_openr (debug_filename
, NULL
);
1036 free (debug_filename
);
1037 if (debug_bfd
== NULL
)
1038 /* FIXME: Should we report our failure to follow the debuglink ? */
1041 if (!bfd_check_format (debug_bfd
, bfd_object
))
1043 bfd_close (debug_bfd
);
1046 stash
->alt
.bfd_ptr
= debug_bfd
;
1049 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
1050 stash
->debug_sections
+ debug_info_alt
,
1051 stash
->alt
.syms
, offset
,
1052 &stash
->alt
.dwarf_info_buffer
,
1053 &stash
->alt
.dwarf_info_size
))
1056 return stash
->alt
.dwarf_info_buffer
+ offset
;
1060 read_address (struct comp_unit
*unit
, bfd_byte
**ptr
, bfd_byte
*buf_end
)
1062 bfd_byte
*buf
= *ptr
;
1065 if (bfd_get_flavour (unit
->abfd
) == bfd_target_elf_flavour
)
1066 signed_vma
= get_elf_backend_data (unit
->abfd
)->sign_extend_vma
;
1068 if (unit
->addr_size
> (size_t) (buf_end
- buf
))
1074 *ptr
= buf
+ unit
->addr_size
;
1077 switch (unit
->addr_size
)
1080 return bfd_get_signed_64 (unit
->abfd
, buf
);
1082 return bfd_get_signed_32 (unit
->abfd
, buf
);
1084 return bfd_get_signed_16 (unit
->abfd
, buf
);
1091 switch (unit
->addr_size
)
1094 return bfd_get_64 (unit
->abfd
, buf
);
1096 return bfd_get_32 (unit
->abfd
, buf
);
1098 return bfd_get_16 (unit
->abfd
, buf
);
1105 /* Lookup an abbrev_info structure in the abbrev hash table. */
1107 static struct abbrev_info
*
1108 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
1110 unsigned int hash_number
;
1111 struct abbrev_info
*abbrev
;
1113 hash_number
= number
% ABBREV_HASH_SIZE
;
1114 abbrev
= abbrevs
[hash_number
];
1118 if (abbrev
->number
== number
)
1121 abbrev
= abbrev
->next
;
1127 /* We keep a hash table to map .debug_abbrev section offsets to the
1128 array of abbrevs, so that compilation units using the same set of
1129 abbrevs do not waste memory. */
1131 struct abbrev_offset_entry
1134 struct abbrev_info
**abbrevs
;
1138 hash_abbrev (const void *p
)
1140 const struct abbrev_offset_entry
*ent
= p
;
1141 return htab_hash_pointer ((void *) ent
->offset
);
1145 eq_abbrev (const void *pa
, const void *pb
)
1147 const struct abbrev_offset_entry
*a
= pa
;
1148 const struct abbrev_offset_entry
*b
= pb
;
1149 return a
->offset
== b
->offset
;
1153 del_abbrev (void *p
)
1155 struct abbrev_offset_entry
*ent
= p
;
1156 struct abbrev_info
**abbrevs
= ent
->abbrevs
;
1159 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1161 struct abbrev_info
*abbrev
= abbrevs
[i
];
1165 free (abbrev
->attrs
);
1166 abbrev
= abbrev
->next
;
1172 /* In DWARF version 2, the description of the debugging information is
1173 stored in a separate .debug_abbrev section. Before we read any
1174 dies from a section we read in all abbreviations and install them
1177 static struct abbrev_info
**
1178 read_abbrevs (bfd
*abfd
, uint64_t offset
, struct dwarf2_debug
*stash
,
1179 struct dwarf2_debug_file
*file
)
1181 struct abbrev_info
**abbrevs
;
1182 bfd_byte
*abbrev_ptr
;
1183 bfd_byte
*abbrev_end
;
1184 struct abbrev_info
*cur_abbrev
;
1185 unsigned int abbrev_number
, abbrev_name
;
1186 unsigned int abbrev_form
, hash_number
;
1189 struct abbrev_offset_entry ent
= { offset
, NULL
};
1191 if (ent
.offset
!= offset
)
1194 slot
= htab_find_slot (file
->abbrev_offsets
, &ent
, INSERT
);
1198 return ((struct abbrev_offset_entry
*) (*slot
))->abbrevs
;
1200 if (! read_section (abfd
, &stash
->debug_sections
[debug_abbrev
],
1202 &file
->dwarf_abbrev_buffer
,
1203 &file
->dwarf_abbrev_size
))
1206 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
1207 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
1208 if (abbrevs
== NULL
)
1211 abbrev_ptr
= file
->dwarf_abbrev_buffer
+ offset
;
1212 abbrev_end
= file
->dwarf_abbrev_buffer
+ file
->dwarf_abbrev_size
;
1213 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1216 /* Loop until we reach an abbrev number of 0. */
1217 while (abbrev_number
)
1219 amt
= sizeof (struct abbrev_info
);
1220 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
1221 if (cur_abbrev
== NULL
)
1224 /* Read in abbrev header. */
1225 cur_abbrev
->number
= abbrev_number
;
1226 cur_abbrev
->tag
= (enum dwarf_tag
)
1227 _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1229 cur_abbrev
->has_children
= read_1_byte (abfd
, &abbrev_ptr
, abbrev_end
);
1231 /* Now read in declarations. */
1234 /* Initialize it just to avoid a GCC false warning. */
1235 bfd_vma implicit_const
= -1;
1237 abbrev_name
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1239 abbrev_form
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1241 if (abbrev_form
== DW_FORM_implicit_const
)
1242 implicit_const
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1244 if (abbrev_name
== 0)
1247 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
1249 struct attr_abbrev
*tmp
;
1251 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
1252 amt
*= sizeof (struct attr_abbrev
);
1253 tmp
= (struct attr_abbrev
*) bfd_realloc (cur_abbrev
->attrs
, amt
);
1256 cur_abbrev
->attrs
= tmp
;
1259 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
1260 = (enum dwarf_attribute
) abbrev_name
;
1261 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].form
1262 = (enum dwarf_form
) abbrev_form
;
1263 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].implicit_const
1265 ++cur_abbrev
->num_attrs
;
1268 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
1269 cur_abbrev
->next
= abbrevs
[hash_number
];
1270 abbrevs
[hash_number
] = cur_abbrev
;
1272 /* Get next abbreviation.
1273 Under Irix6 the abbreviations for a compilation unit are not
1274 always properly terminated with an abbrev number of 0.
1275 Exit loop if we encounter an abbreviation which we have
1276 already read (which means we are about to read the abbreviations
1277 for the next compile unit) or if the end of the abbreviation
1278 table is reached. */
1279 if ((size_t) (abbrev_ptr
- file
->dwarf_abbrev_buffer
)
1280 >= file
->dwarf_abbrev_size
)
1282 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1284 if (lookup_abbrev (abbrev_number
, abbrevs
) != NULL
)
1288 *slot
= bfd_malloc (sizeof ent
);
1291 ent
.abbrevs
= abbrevs
;
1292 memcpy (*slot
, &ent
, sizeof ent
);
1296 if (abbrevs
!= NULL
)
1300 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1302 struct abbrev_info
*abbrev
= abbrevs
[i
];
1306 free (abbrev
->attrs
);
1307 abbrev
= abbrev
->next
;
1315 /* Returns true if the form is one which has a string value. */
1318 is_str_form (const struct attribute
*attr
)
1322 case DW_FORM_string
:
1329 case DW_FORM_line_strp
:
1330 case DW_FORM_GNU_strp_alt
:
1338 /* Returns true if the form is one which has an integer value. */
1341 is_int_form (const struct attribute
*attr
)
1353 case DW_FORM_ref_addr
:
1358 case DW_FORM_ref_udata
:
1359 case DW_FORM_sec_offset
:
1360 case DW_FORM_flag_present
:
1361 case DW_FORM_ref_sig8
:
1363 case DW_FORM_implicit_const
:
1364 case DW_FORM_addrx1
:
1365 case DW_FORM_addrx2
:
1366 case DW_FORM_addrx3
:
1367 case DW_FORM_addrx4
:
1368 case DW_FORM_GNU_ref_alt
:
1376 /* Returns true if the form is strx[1-4]. */
1379 is_strx_form (enum dwarf_form form
)
1381 return (form
== DW_FORM_strx
1382 || form
== DW_FORM_strx1
1383 || form
== DW_FORM_strx2
1384 || form
== DW_FORM_strx3
1385 || form
== DW_FORM_strx4
);
1388 /* Return true if the form is addrx[1-4]. */
1391 is_addrx_form (enum dwarf_form form
)
1393 return (form
== DW_FORM_addrx
1394 || form
== DW_FORM_addrx1
1395 || form
== DW_FORM_addrx2
1396 || form
== DW_FORM_addrx3
1397 || form
== DW_FORM_addrx4
);
1400 /* Returns the address in .debug_addr section using DW_AT_addr_base.
1401 Used to implement DW_FORM_addrx*. */
1403 read_indexed_address (uint64_t idx
, struct comp_unit
*unit
)
1405 struct dwarf2_debug
*stash
= unit
->stash
;
1406 struct dwarf2_debug_file
*file
= unit
->file
;
1413 if (!read_section (unit
->abfd
, &stash
->debug_sections
[debug_addr
],
1415 &file
->dwarf_addr_buffer
, &file
->dwarf_addr_size
))
1418 if (_bfd_mul_overflow (idx
, unit
->addr_size
, &offset
))
1421 offset
+= unit
->dwarf_addr_offset
;
1422 if (offset
< unit
->dwarf_addr_offset
1423 || offset
> file
->dwarf_addr_size
1424 || file
->dwarf_addr_size
- offset
< unit
->addr_size
)
1427 info_ptr
= file
->dwarf_addr_buffer
+ offset
;
1429 if (unit
->addr_size
== 4)
1430 return bfd_get_32 (unit
->abfd
, info_ptr
);
1431 else if (unit
->addr_size
== 8)
1432 return bfd_get_64 (unit
->abfd
, info_ptr
);
1437 /* Returns the string using DW_AT_str_offsets_base.
1438 Used to implement DW_FORM_strx*. */
1440 read_indexed_string (uint64_t idx
, struct comp_unit
*unit
)
1442 struct dwarf2_debug
*stash
= unit
->stash
;
1443 struct dwarf2_debug_file
*file
= unit
->file
;
1445 uint64_t str_offset
;
1451 if (!read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
1453 &file
->dwarf_str_buffer
, &file
->dwarf_str_size
))
1456 if (!read_section (unit
->abfd
, &stash
->debug_sections
[debug_str_offsets
],
1458 &file
->dwarf_str_offsets_buffer
,
1459 &file
->dwarf_str_offsets_size
))
1462 if (_bfd_mul_overflow (idx
, unit
->offset_size
, &offset
))
1465 offset
+= unit
->dwarf_str_offset
;
1466 if (offset
< unit
->dwarf_str_offset
1467 || offset
> file
->dwarf_str_offsets_size
1468 || file
->dwarf_str_offsets_size
- offset
< unit
->offset_size
)
1471 info_ptr
= file
->dwarf_str_offsets_buffer
+ offset
;
1473 if (unit
->offset_size
== 4)
1474 str_offset
= bfd_get_32 (unit
->abfd
, info_ptr
);
1475 else if (unit
->offset_size
== 8)
1476 str_offset
= bfd_get_64 (unit
->abfd
, info_ptr
);
1480 if (str_offset
>= file
->dwarf_str_size
)
1482 return (const char *) file
->dwarf_str_buffer
+ str_offset
;
1485 /* Read and fill in the value of attribute ATTR as described by FORM.
1486 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1487 Returns an updated INFO_PTR taking into account the amount of data read. */
1490 read_attribute_value (struct attribute
* attr
,
1492 bfd_vma implicit_const
,
1493 struct comp_unit
* unit
,
1494 bfd_byte
* info_ptr
,
1495 bfd_byte
* info_ptr_end
)
1497 bfd
*abfd
= unit
->abfd
;
1500 if (info_ptr
>= info_ptr_end
&& form
!= DW_FORM_flag_present
)
1502 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1503 bfd_set_error (bfd_error_bad_value
);
1507 attr
->form
= (enum dwarf_form
) form
;
1511 case DW_FORM_flag_present
:
1514 case DW_FORM_ref_addr
:
1515 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1517 if (unit
->version
>= 3)
1519 if (unit
->offset_size
== 4)
1520 attr
->u
.val
= read_4_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1522 attr
->u
.val
= read_8_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1527 attr
->u
.val
= read_address (unit
, &info_ptr
, info_ptr_end
);
1529 case DW_FORM_GNU_ref_alt
:
1530 case DW_FORM_sec_offset
:
1531 if (unit
->offset_size
== 4)
1532 attr
->u
.val
= read_4_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1534 attr
->u
.val
= read_8_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1536 case DW_FORM_block2
:
1537 amt
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1538 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1539 if (attr
->u
.blk
== NULL
)
1542 case DW_FORM_block4
:
1543 amt
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1544 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1545 if (attr
->u
.blk
== NULL
)
1551 attr
->u
.val
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1553 case DW_FORM_addrx1
:
1554 attr
->u
.val
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1555 /* dwarf_addr_offset value 0 indicates the attribute DW_AT_addr_base
1557 if (unit
->dwarf_addr_offset
!= 0)
1558 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
1562 attr
->u
.val
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1564 case DW_FORM_addrx2
:
1565 attr
->u
.val
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1566 if (unit
->dwarf_addr_offset
!= 0)
1567 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
1569 case DW_FORM_addrx3
:
1570 attr
->u
.val
= read_3_bytes (abfd
, &info_ptr
, info_ptr_end
);
1571 if (unit
->dwarf_addr_offset
!= 0)
1572 attr
->u
.val
= read_indexed_address(attr
->u
.val
, unit
);
1576 attr
->u
.val
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1578 case DW_FORM_addrx4
:
1579 attr
->u
.val
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1580 if (unit
->dwarf_addr_offset
!= 0)
1581 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
1585 case DW_FORM_ref_sig8
:
1586 attr
->u
.val
= read_8_bytes (abfd
, &info_ptr
, info_ptr_end
);
1588 case DW_FORM_string
:
1589 attr
->u
.str
= read_string (&info_ptr
, info_ptr_end
);
1592 attr
->u
.str
= read_indirect_string (unit
, &info_ptr
, info_ptr_end
);
1594 case DW_FORM_line_strp
:
1595 attr
->u
.str
= read_indirect_line_string (unit
, &info_ptr
, info_ptr_end
);
1597 case DW_FORM_GNU_strp_alt
:
1598 attr
->u
.str
= read_alt_indirect_string (unit
, &info_ptr
, info_ptr_end
);
1601 attr
->u
.val
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1602 /* dwarf_str_offset value 0 indicates the attribute DW_AT_str_offsets_base
1604 if (unit
->dwarf_str_offset
!= 0)
1605 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1610 attr
->u
.val
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1611 if (unit
->dwarf_str_offset
!= 0)
1612 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1617 attr
->u
.val
= read_3_bytes (abfd
, &info_ptr
, info_ptr_end
);
1618 if (unit
->dwarf_str_offset
!= 0)
1619 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1624 attr
->u
.val
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1625 if (unit
->dwarf_str_offset
!= 0)
1626 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1631 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1632 false, info_ptr_end
);
1633 if (unit
->dwarf_str_offset
!= 0)
1634 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1638 case DW_FORM_exprloc
:
1640 amt
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1641 false, info_ptr_end
);
1642 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1643 if (attr
->u
.blk
== NULL
)
1646 case DW_FORM_block1
:
1647 amt
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1648 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1649 if (attr
->u
.blk
== NULL
)
1653 attr
->u
.sval
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1654 true, info_ptr_end
);
1657 case DW_FORM_rnglistx
:
1658 case DW_FORM_loclistx
:
1659 /* FIXME: Add support for these forms! */
1661 case DW_FORM_ref_udata
:
1663 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1664 false, info_ptr_end
);
1667 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1668 false, info_ptr_end
);
1669 if (unit
->dwarf_addr_offset
!= 0)
1670 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
1672 case DW_FORM_indirect
:
1673 form
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1674 false, info_ptr_end
);
1675 if (form
== DW_FORM_implicit_const
)
1676 implicit_const
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1677 true, info_ptr_end
);
1678 info_ptr
= read_attribute_value (attr
, form
, implicit_const
, unit
,
1679 info_ptr
, info_ptr_end
);
1681 case DW_FORM_implicit_const
:
1682 attr
->form
= DW_FORM_sdata
;
1683 attr
->u
.sval
= implicit_const
;
1685 case DW_FORM_data16
:
1686 /* This is really a "constant", but there is no way to store that
1687 so pretend it is a 16 byte block instead. */
1688 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, 16);
1689 if (attr
->u
.blk
== NULL
)
1694 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1696 bfd_set_error (bfd_error_bad_value
);
1702 /* Read an attribute described by an abbreviated attribute. */
1705 read_attribute (struct attribute
* attr
,
1706 struct attr_abbrev
* abbrev
,
1707 struct comp_unit
* unit
,
1708 bfd_byte
* info_ptr
,
1709 bfd_byte
* info_ptr_end
)
1711 attr
->name
= abbrev
->name
;
1712 info_ptr
= read_attribute_value (attr
, abbrev
->form
, abbrev
->implicit_const
,
1713 unit
, info_ptr
, info_ptr_end
);
1717 /* Return mangling style given LANG. */
1720 mangle_style (int lang
)
1728 case DW_LANG_C_plus_plus
:
1729 case DW_LANG_C_plus_plus_03
:
1730 case DW_LANG_C_plus_plus_11
:
1731 case DW_LANG_C_plus_plus_14
:
1741 case DW_LANG_Rust_old
:
1749 case DW_LANG_Cobol74
:
1750 case DW_LANG_Cobol85
:
1751 case DW_LANG_Fortran77
:
1752 case DW_LANG_Pascal83
:
1757 case DW_LANG_Mips_Assembler
:
1759 case DW_LANG_HP_Basic91
:
1760 case DW_LANG_HP_IMacro
:
1761 case DW_LANG_HP_Assembler
:
1766 /* Source line information table routines. */
1768 #define FILE_ALLOC_CHUNK 5
1769 #define DIR_ALLOC_CHUNK 5
1773 struct line_info
* prev_line
;
1777 unsigned int column
;
1778 unsigned int discriminator
;
1779 unsigned char op_index
;
1780 unsigned char end_sequence
; /* End of (sequential) code sequence. */
1791 struct line_sequence
1794 struct line_sequence
* prev_sequence
;
1795 struct line_info
* last_line
; /* Largest VMA. */
1796 struct line_info
** line_info_lookup
;
1797 bfd_size_type num_lines
;
1800 struct line_info_table
1803 unsigned int num_files
;
1804 unsigned int num_dirs
;
1805 unsigned int num_sequences
;
1806 bool use_dir_and_file_0
;
1809 struct fileinfo
* files
;
1810 struct line_sequence
* sequences
;
1811 struct line_info
* lcl_head
; /* Local head; used in 'add_line_info'. */
1814 /* Remember some information about each function. If the function is
1815 inlined (DW_TAG_inlined_subroutine) it may have two additional
1816 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1817 source code location where this function was inlined. */
1821 /* Pointer to previous function in list of all functions. */
1822 struct funcinfo
*prev_func
;
1823 /* Pointer to function one scope higher. */
1824 struct funcinfo
*caller_func
;
1825 /* Source location file name where caller_func inlines this func. */
1827 /* Source location file name. */
1829 /* Source location line number where caller_func inlines this func. */
1831 /* Source location line number. */
1836 struct arange arange
;
1837 /* The offset of the funcinfo from the start of the unit. */
1838 uint64_t unit_offset
;
1841 struct lookup_funcinfo
1843 /* Function information corresponding to this lookup table entry. */
1844 struct funcinfo
*funcinfo
;
1846 /* The lowest address for this specific function. */
1849 /* The highest address of this function before the lookup table is sorted.
1850 The highest address of all prior functions after the lookup table is
1851 sorted, which is used for binary search. */
1853 /* Index of this function, used to ensure qsort is stable. */
1859 /* Pointer to previous variable in list of all variables. */
1860 struct varinfo
*prev_var
;
1861 /* The offset of the varinfo from the start of the unit. */
1862 uint64_t unit_offset
;
1863 /* Source location file name. */
1865 /* Source location line number. */
1867 /* The type of this variable. */
1869 /* The name of the variable, if it has one. */
1871 /* The address of the variable. */
1873 /* Is this a stack variable? */
1877 /* Return TRUE if NEW_LINE should sort after LINE. */
1880 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
1882 return (new_line
->address
> line
->address
1883 || (new_line
->address
== line
->address
1884 && new_line
->op_index
> line
->op_index
));
1888 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1889 that the list is sorted. Note that the line_info list is sorted from
1890 highest to lowest VMA (with possible duplicates); that is,
1891 line_info->prev_line always accesses an equal or smaller VMA. */
1894 add_line_info (struct line_info_table
*table
,
1896 unsigned char op_index
,
1899 unsigned int column
,
1900 unsigned int discriminator
,
1903 size_t amt
= sizeof (struct line_info
);
1904 struct line_sequence
* seq
= table
->sequences
;
1905 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
1910 /* Set member data of 'info'. */
1911 info
->prev_line
= NULL
;
1912 info
->address
= address
;
1913 info
->op_index
= op_index
;
1915 info
->column
= column
;
1916 info
->discriminator
= discriminator
;
1917 info
->end_sequence
= end_sequence
;
1919 if (filename
&& filename
[0])
1921 info
->filename
= (char *) bfd_alloc (table
->abfd
, strlen (filename
) + 1);
1922 if (info
->filename
== NULL
)
1924 strcpy (info
->filename
, filename
);
1927 info
->filename
= NULL
;
1929 /* Find the correct location for 'info'. Normally we will receive
1930 new line_info data 1) in order and 2) with increasing VMAs.
1931 However some compilers break the rules (cf. decode_line_info) and
1932 so we include some heuristics for quickly finding the correct
1933 location for 'info'. In particular, these heuristics optimize for
1934 the common case in which the VMA sequence that we receive is a
1935 list of locally sorted VMAs such as
1936 p...z a...j (where a < j < p < z)
1938 Note: table->lcl_head is used to head an *actual* or *possible*
1939 sub-sequence within the list (such as a...j) that is not directly
1940 headed by table->last_line
1942 Note: we may receive duplicate entries from 'decode_line_info'. */
1945 && seq
->last_line
->address
== address
1946 && seq
->last_line
->op_index
== op_index
1947 && seq
->last_line
->end_sequence
== end_sequence
)
1949 /* We only keep the last entry with the same address and end
1950 sequence. See PR ld/4986. */
1951 if (table
->lcl_head
== seq
->last_line
)
1952 table
->lcl_head
= info
;
1953 info
->prev_line
= seq
->last_line
->prev_line
;
1954 seq
->last_line
= info
;
1956 else if (!seq
|| seq
->last_line
->end_sequence
)
1958 /* Start a new line sequence. */
1959 amt
= sizeof (struct line_sequence
);
1960 seq
= (struct line_sequence
*) bfd_malloc (amt
);
1963 seq
->low_pc
= address
;
1964 seq
->prev_sequence
= table
->sequences
;
1965 seq
->last_line
= info
;
1966 table
->lcl_head
= info
;
1967 table
->sequences
= seq
;
1968 table
->num_sequences
++;
1970 else if (info
->end_sequence
1971 || new_line_sorts_after (info
, seq
->last_line
))
1973 /* Normal case: add 'info' to the beginning of the current sequence. */
1974 info
->prev_line
= seq
->last_line
;
1975 seq
->last_line
= info
;
1977 /* lcl_head: initialize to head a *possible* sequence at the end. */
1978 if (!table
->lcl_head
)
1979 table
->lcl_head
= info
;
1981 else if (!new_line_sorts_after (info
, table
->lcl_head
)
1982 && (!table
->lcl_head
->prev_line
1983 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
1985 /* Abnormal but easy: lcl_head is the head of 'info'. */
1986 info
->prev_line
= table
->lcl_head
->prev_line
;
1987 table
->lcl_head
->prev_line
= info
;
1991 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1992 are valid heads for 'info'. Reset 'lcl_head'. */
1993 struct line_info
* li2
= seq
->last_line
; /* Always non-NULL. */
1994 struct line_info
* li1
= li2
->prev_line
;
1998 if (!new_line_sorts_after (info
, li2
)
1999 && new_line_sorts_after (info
, li1
))
2002 li2
= li1
; /* always non-NULL */
2003 li1
= li1
->prev_line
;
2005 table
->lcl_head
= li2
;
2006 info
->prev_line
= table
->lcl_head
->prev_line
;
2007 table
->lcl_head
->prev_line
= info
;
2008 if (address
< seq
->low_pc
)
2009 seq
->low_pc
= address
;
2014 /* Extract a fully qualified filename from a line info table.
2015 The returned string has been malloc'ed and it is the caller's
2016 responsibility to free it. */
2019 concat_filename (struct line_info_table
*table
, unsigned int file
)
2023 /* Pre DWARF-5 entry 0 in the directory and filename tables was not used.
2024 So in order to save space in the tables used here the info for, eg
2025 directory 1 is stored in slot 0 of the directory table, directory 2
2026 in slot 1 and so on.
2028 Starting with DWARF-5 the 0'th entry is used so there is a one to one
2029 mapping between DWARF slots and internal table entries. */
2030 if (! table
->use_dir_and_file_0
)
2032 /* Pre DWARF-5, FILE == 0 means unknown. */
2034 return strdup ("<unknown>");
2038 if (table
== NULL
|| file
>= table
->num_files
)
2041 (_("DWARF error: mangled line number section (bad file number)"));
2042 return strdup ("<unknown>");
2045 filename
= table
->files
[file
].name
;
2047 if (filename
== NULL
)
2048 return strdup ("<unknown>");
2050 if (!IS_ABSOLUTE_PATH (filename
))
2052 char *dir_name
= NULL
;
2053 char *subdir_name
= NULL
;
2056 unsigned int dir
= table
->files
[file
].dir
;
2058 if (!table
->use_dir_and_file_0
)
2060 /* Wrapping from 0 to -1u above gives the intended result with
2061 the test below of leaving subdir_name NULL for pre-DWARF5 dir
2063 /* PR 17512: file: 0317e960, file: 7f3d2e4b. */
2064 if (dir
< table
->num_dirs
)
2065 subdir_name
= table
->dirs
[dir
];
2067 if (!subdir_name
|| !IS_ABSOLUTE_PATH (subdir_name
))
2068 dir_name
= table
->comp_dir
;
2072 dir_name
= subdir_name
;
2077 return strdup (filename
);
2079 len
= strlen (dir_name
) + strlen (filename
) + 2;
2083 len
+= strlen (subdir_name
) + 1;
2084 name
= (char *) bfd_malloc (len
);
2086 sprintf (name
, "%s/%s/%s", dir_name
, subdir_name
, filename
);
2090 name
= (char *) bfd_malloc (len
);
2092 sprintf (name
, "%s/%s", dir_name
, filename
);
2098 return strdup (filename
);
2101 /* Number of bits in a bfd_vma. */
2102 #define VMA_BITS (8 * sizeof (bfd_vma))
2104 /* Check whether [low1, high1) can be combined with [low2, high2),
2105 i.e., they touch or overlap. */
2108 ranges_overlap (bfd_vma low1
,
2113 if (low1
== low2
|| high1
== high2
)
2116 /* Sort so that low1 is below low2. */
2130 /* We touch iff low2 == high1.
2131 We overlap iff low2 is within [low1, high1). */
2132 return low2
<= high1
;
2135 /* Insert an address range in the trie mapping addresses to compilation units.
2136 Will return the new trie node (usually the same as is being sent in, but
2137 in case of a leaf-to-interior conversion, or expansion of a leaf, it may be
2138 different), or NULL on failure. */
2140 static struct trie_node
*
2141 insert_arange_in_trie (bfd
*abfd
,
2142 struct trie_node
*trie
,
2144 unsigned int trie_pc_bits
,
2145 struct comp_unit
*unit
,
2149 bfd_vma clamped_low_pc
, clamped_high_pc
;
2150 int ch
, from_ch
, to_ch
;
2151 bool is_full_leaf
= false;
2153 /* See if we can extend any of the existing ranges. This merging
2154 isn't perfect (if merging opens up the possibility of merging two existing
2155 ranges, we won't find them), but it takes the majority of the cases. */
2156 if (trie
->num_room_in_leaf
> 0)
2158 struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
2161 for (i
= 0; i
< leaf
->num_stored_in_leaf
; ++i
)
2163 if (leaf
->ranges
[i
].unit
== unit
2164 && ranges_overlap (low_pc
, high_pc
,
2165 leaf
->ranges
[i
].low_pc
,
2166 leaf
->ranges
[i
].high_pc
))
2168 if (low_pc
< leaf
->ranges
[i
].low_pc
)
2169 leaf
->ranges
[i
].low_pc
= low_pc
;
2170 if (high_pc
> leaf
->ranges
[i
].high_pc
)
2171 leaf
->ranges
[i
].high_pc
= high_pc
;
2176 is_full_leaf
= leaf
->num_stored_in_leaf
== trie
->num_room_in_leaf
;
2179 /* If we're a leaf with no more room and we're _not_ at the bottom,
2180 convert to an interior node. */
2181 if (is_full_leaf
&& trie_pc_bits
< VMA_BITS
)
2183 const struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
2186 trie
= bfd_zalloc (abfd
, sizeof (struct trie_interior
));
2189 is_full_leaf
= false;
2191 /* TODO: If we wanted to save a little more memory at the cost of
2192 complexity, we could have reused the old leaf node as one of the
2193 children of the new interior node, instead of throwing it away. */
2194 for (i
= 0; i
< leaf
->num_stored_in_leaf
; ++i
)
2196 if (!insert_arange_in_trie (abfd
, trie
, trie_pc
, trie_pc_bits
,
2197 leaf
->ranges
[i
].unit
, leaf
->ranges
[i
].low_pc
,
2198 leaf
->ranges
[i
].high_pc
))
2203 /* If we're a leaf with no more room and we _are_ at the bottom,
2204 we have no choice but to just make it larger. */
2207 const struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
2208 unsigned int new_room_in_leaf
= trie
->num_room_in_leaf
* 2;
2209 struct trie_leaf
*new_leaf
;
2210 size_t amt
= (sizeof (struct trie_leaf
)
2211 + ((new_room_in_leaf
- TRIE_LEAF_SIZE
)
2212 * sizeof (leaf
->ranges
[0])));
2213 new_leaf
= bfd_zalloc (abfd
, amt
);
2214 new_leaf
->head
.num_room_in_leaf
= new_room_in_leaf
;
2215 new_leaf
->num_stored_in_leaf
= leaf
->num_stored_in_leaf
;
2217 memcpy (new_leaf
->ranges
,
2219 leaf
->num_stored_in_leaf
* sizeof (leaf
->ranges
[0]));
2220 trie
= &new_leaf
->head
;
2221 is_full_leaf
= false;
2223 /* Now the insert below will go through. */
2226 /* If we're a leaf (now with room), we can just insert at the end. */
2227 if (trie
->num_room_in_leaf
> 0)
2229 struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
2231 unsigned int i
= leaf
->num_stored_in_leaf
++;
2232 leaf
->ranges
[i
].unit
= unit
;
2233 leaf
->ranges
[i
].low_pc
= low_pc
;
2234 leaf
->ranges
[i
].high_pc
= high_pc
;
2238 /* Now we are definitely an interior node, so recurse into all
2239 the relevant buckets. */
2241 /* Clamp the range to the current trie bucket. */
2242 clamped_low_pc
= low_pc
;
2243 clamped_high_pc
= high_pc
;
2244 if (trie_pc_bits
> 0)
2246 bfd_vma bucket_high_pc
=
2247 trie_pc
+ ((bfd_vma
) -1 >> trie_pc_bits
); /* Inclusive. */
2248 if (clamped_low_pc
< trie_pc
)
2249 clamped_low_pc
= trie_pc
;
2250 if (clamped_high_pc
> bucket_high_pc
)
2251 clamped_high_pc
= bucket_high_pc
;
2254 /* Insert the ranges in all buckets that it spans. */
2255 from_ch
= (clamped_low_pc
>> (VMA_BITS
- trie_pc_bits
- 8)) & 0xff;
2256 to_ch
= ((clamped_high_pc
- 1) >> (VMA_BITS
- trie_pc_bits
- 8)) & 0xff;
2257 for (ch
= from_ch
; ch
<= to_ch
; ++ch
)
2259 struct trie_interior
*interior
= (struct trie_interior
*) trie
;
2260 struct trie_node
*child
= interior
->children
[ch
];
2264 child
= alloc_trie_leaf (abfd
);
2268 bfd_vma bucket
= (bfd_vma
) ch
<< (VMA_BITS
- trie_pc_bits
- 8);
2269 child
= insert_arange_in_trie (abfd
,
2279 interior
->children
[ch
] = child
;
2286 arange_add (struct comp_unit
*unit
, struct arange
*first_arange
,
2287 struct trie_node
**trie_root
, bfd_vma low_pc
, bfd_vma high_pc
)
2289 struct arange
*arange
;
2291 /* Ignore empty ranges. */
2292 if (low_pc
== high_pc
)
2295 if (trie_root
!= NULL
)
2297 *trie_root
= insert_arange_in_trie (unit
->file
->bfd_ptr
,
2304 if (*trie_root
== NULL
)
2308 /* If the first arange is empty, use it. */
2309 if (first_arange
->high
== 0)
2311 first_arange
->low
= low_pc
;
2312 first_arange
->high
= high_pc
;
2316 /* Next see if we can cheaply extend an existing range. */
2317 arange
= first_arange
;
2320 if (low_pc
== arange
->high
)
2322 arange
->high
= high_pc
;
2325 if (high_pc
== arange
->low
)
2327 arange
->low
= low_pc
;
2330 arange
= arange
->next
;
2334 /* Need to allocate a new arange and insert it into the arange list.
2335 Order isn't significant, so just insert after the first arange. */
2336 arange
= (struct arange
*) bfd_alloc (unit
->abfd
, sizeof (*arange
));
2339 arange
->low
= low_pc
;
2340 arange
->high
= high_pc
;
2341 arange
->next
= first_arange
->next
;
2342 first_arange
->next
= arange
;
2346 /* Compare function for line sequences. */
2349 compare_sequences (const void* a
, const void* b
)
2351 const struct line_sequence
* seq1
= a
;
2352 const struct line_sequence
* seq2
= b
;
2354 /* Sort by low_pc as the primary key. */
2355 if (seq1
->low_pc
< seq2
->low_pc
)
2357 if (seq1
->low_pc
> seq2
->low_pc
)
2360 /* If low_pc values are equal, sort in reverse order of
2361 high_pc, so that the largest region comes first. */
2362 if (seq1
->last_line
->address
< seq2
->last_line
->address
)
2364 if (seq1
->last_line
->address
> seq2
->last_line
->address
)
2367 if (seq1
->last_line
->op_index
< seq2
->last_line
->op_index
)
2369 if (seq1
->last_line
->op_index
> seq2
->last_line
->op_index
)
2372 /* num_lines is initially an index, to make the sort stable. */
2373 if (seq1
->num_lines
< seq2
->num_lines
)
2375 if (seq1
->num_lines
> seq2
->num_lines
)
2380 /* Construct the line information table for quick lookup. */
2383 build_line_info_table (struct line_info_table
* table
,
2384 struct line_sequence
* seq
)
2387 struct line_info
**line_info_lookup
;
2388 struct line_info
*each_line
;
2389 unsigned int num_lines
;
2390 unsigned int line_index
;
2392 if (seq
->line_info_lookup
!= NULL
)
2395 /* Count the number of line information entries. We could do this while
2396 scanning the debug information, but some entries may be added via
2397 lcl_head without having a sequence handy to increment the number of
2400 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
2403 seq
->num_lines
= num_lines
;
2407 /* Allocate space for the line information lookup table. */
2408 amt
= sizeof (struct line_info
*) * num_lines
;
2409 line_info_lookup
= (struct line_info
**) bfd_alloc (table
->abfd
, amt
);
2410 seq
->line_info_lookup
= line_info_lookup
;
2411 if (line_info_lookup
== NULL
)
2414 /* Create the line information lookup table. */
2415 line_index
= num_lines
;
2416 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
2417 line_info_lookup
[--line_index
] = each_line
;
2419 BFD_ASSERT (line_index
== 0);
2423 /* Sort the line sequences for quick lookup. */
2426 sort_line_sequences (struct line_info_table
* table
)
2429 struct line_sequence
*sequences
;
2430 struct line_sequence
*seq
;
2432 unsigned int num_sequences
= table
->num_sequences
;
2433 bfd_vma last_high_pc
;
2435 if (num_sequences
== 0)
2438 /* Allocate space for an array of sequences. */
2439 amt
= sizeof (struct line_sequence
) * num_sequences
;
2440 sequences
= (struct line_sequence
*) bfd_alloc (table
->abfd
, amt
);
2441 if (sequences
== NULL
)
2444 /* Copy the linked list into the array, freeing the original nodes. */
2445 seq
= table
->sequences
;
2446 for (n
= 0; n
< num_sequences
; n
++)
2448 struct line_sequence
* last_seq
= seq
;
2451 sequences
[n
].low_pc
= seq
->low_pc
;
2452 sequences
[n
].prev_sequence
= NULL
;
2453 sequences
[n
].last_line
= seq
->last_line
;
2454 sequences
[n
].line_info_lookup
= NULL
;
2455 sequences
[n
].num_lines
= n
;
2456 seq
= seq
->prev_sequence
;
2459 BFD_ASSERT (seq
== NULL
);
2461 qsort (sequences
, n
, sizeof (struct line_sequence
), compare_sequences
);
2463 /* Make the list binary-searchable by trimming overlapping entries
2464 and removing nested entries. */
2466 last_high_pc
= sequences
[0].last_line
->address
;
2467 for (n
= 1; n
< table
->num_sequences
; n
++)
2469 if (sequences
[n
].low_pc
< last_high_pc
)
2471 if (sequences
[n
].last_line
->address
<= last_high_pc
)
2472 /* Skip nested entries. */
2475 /* Trim overlapping entries. */
2476 sequences
[n
].low_pc
= last_high_pc
;
2478 last_high_pc
= sequences
[n
].last_line
->address
;
2479 if (n
> num_sequences
)
2481 /* Close up the gap. */
2482 sequences
[num_sequences
].low_pc
= sequences
[n
].low_pc
;
2483 sequences
[num_sequences
].last_line
= sequences
[n
].last_line
;
2488 table
->sequences
= sequences
;
2489 table
->num_sequences
= num_sequences
;
2493 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
2496 line_info_add_include_dir (struct line_info_table
*table
, char *cur_dir
)
2498 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
2503 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
2504 amt
*= sizeof (char *);
2506 tmp
= (char **) bfd_realloc (table
->dirs
, amt
);
2512 table
->dirs
[table
->num_dirs
++] = cur_dir
;
2517 line_info_add_include_dir_stub (struct line_info_table
*table
, char *cur_dir
,
2518 unsigned int dir ATTRIBUTE_UNUSED
,
2519 unsigned int xtime ATTRIBUTE_UNUSED
,
2520 unsigned int size ATTRIBUTE_UNUSED
)
2522 return line_info_add_include_dir (table
, cur_dir
);
2525 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
2528 line_info_add_file_name (struct line_info_table
*table
, char *cur_file
,
2529 unsigned int dir
, unsigned int xtime
,
2532 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
2534 struct fileinfo
*tmp
;
2537 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
2538 amt
*= sizeof (struct fileinfo
);
2540 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
2546 table
->files
[table
->num_files
].name
= cur_file
;
2547 table
->files
[table
->num_files
].dir
= dir
;
2548 table
->files
[table
->num_files
].time
= xtime
;
2549 table
->files
[table
->num_files
].size
= size
;
2554 /* Read directory or file name entry format, starting with byte of
2555 format count entries, ULEB128 pairs of entry formats, ULEB128 of
2556 entries count and the entries themselves in the described entry
2560 read_formatted_entries (struct comp_unit
*unit
, bfd_byte
**bufp
,
2561 bfd_byte
*buf_end
, struct line_info_table
*table
,
2562 bool (*callback
) (struct line_info_table
*table
,
2568 bfd
*abfd
= unit
->abfd
;
2569 bfd_byte format_count
, formati
;
2570 bfd_vma data_count
, datai
;
2571 bfd_byte
*buf
= *bufp
;
2572 bfd_byte
*format_header_data
;
2574 format_count
= read_1_byte (abfd
, &buf
, buf_end
);
2575 format_header_data
= buf
;
2576 for (formati
= 0; formati
< format_count
; formati
++)
2578 _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2579 _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2582 data_count
= _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2583 if (format_count
== 0 && data_count
!= 0)
2585 _bfd_error_handler (_("DWARF error: zero format count"));
2586 bfd_set_error (bfd_error_bad_value
);
2590 /* PR 22210. Paranoia check. Don't bother running the loop
2591 if we know that we are going to run out of buffer. */
2592 if (data_count
> (bfd_vma
) (buf_end
- buf
))
2595 (_("DWARF error: data count (%" PRIx64
") larger than buffer size"),
2596 (uint64_t) data_count
);
2597 bfd_set_error (bfd_error_bad_value
);
2601 for (datai
= 0; datai
< data_count
; datai
++)
2603 bfd_byte
*format
= format_header_data
;
2606 memset (&fe
, 0, sizeof fe
);
2607 for (formati
= 0; formati
< format_count
; formati
++)
2609 bfd_vma content_type
, form
;
2611 char **stringp
= &string_trash
;
2612 unsigned int uint_trash
, *uintp
= &uint_trash
;
2613 struct attribute attr
;
2615 content_type
= _bfd_safe_read_leb128 (abfd
, &format
, false, buf_end
);
2616 switch (content_type
)
2621 case DW_LNCT_directory_index
:
2624 case DW_LNCT_timestamp
:
2634 (_("DWARF error: unknown format content type %" PRIu64
),
2635 (uint64_t) content_type
);
2636 bfd_set_error (bfd_error_bad_value
);
2640 form
= _bfd_safe_read_leb128 (abfd
, &format
, false, buf_end
);
2641 buf
= read_attribute_value (&attr
, form
, 0, unit
, buf
, buf_end
);
2646 case DW_FORM_string
:
2647 case DW_FORM_line_strp
:
2653 *stringp
= attr
.u
.str
;
2661 *uintp
= attr
.u
.val
;
2664 case DW_FORM_data16
:
2665 /* MD5 data is in the attr.blk, but we are ignoring those. */
2670 if (!callback (table
, fe
.name
, fe
.dir
, fe
.time
, fe
.size
))
2678 /* Decode the line number information for UNIT. */
2680 static struct line_info_table
*
2681 decode_line_info (struct comp_unit
*unit
)
2683 bfd
*abfd
= unit
->abfd
;
2684 struct dwarf2_debug
*stash
= unit
->stash
;
2685 struct dwarf2_debug_file
*file
= unit
->file
;
2686 struct line_info_table
* table
;
2689 struct line_head lh
;
2690 unsigned int i
, offset_size
;
2691 char *cur_file
, *cur_dir
;
2692 unsigned char op_code
, extended_op
, adj_opcode
;
2693 unsigned int exop_len
;
2696 if (unit
->line_offset
== 0 && file
->line_table
)
2697 return file
->line_table
;
2699 if (! read_section (abfd
, &stash
->debug_sections
[debug_line
],
2700 file
->syms
, unit
->line_offset
,
2701 &file
->dwarf_line_buffer
, &file
->dwarf_line_size
))
2704 if (file
->dwarf_line_size
< 16)
2707 (_("DWARF error: line info section is too small (%" PRId64
")"),
2708 (int64_t) file
->dwarf_line_size
);
2709 bfd_set_error (bfd_error_bad_value
);
2712 line_ptr
= file
->dwarf_line_buffer
+ unit
->line_offset
;
2713 line_end
= file
->dwarf_line_buffer
+ file
->dwarf_line_size
;
2715 /* Read in the prologue. */
2716 lh
.total_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2718 if (lh
.total_length
== 0xffffffff)
2720 lh
.total_length
= read_8_bytes (abfd
, &line_ptr
, line_end
);
2723 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
2725 /* Handle (non-standard) 64-bit DWARF2 formats. */
2726 lh
.total_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2730 if (lh
.total_length
> (size_t) (line_end
- line_ptr
))
2733 /* xgettext: c-format */
2734 (_("DWARF error: line info data is bigger (%#" PRIx64
")"
2735 " than the space remaining in the section (%#lx)"),
2736 (uint64_t) lh
.total_length
, (unsigned long) (line_end
- line_ptr
));
2737 bfd_set_error (bfd_error_bad_value
);
2741 line_end
= line_ptr
+ lh
.total_length
;
2743 lh
.version
= read_2_bytes (abfd
, &line_ptr
, line_end
);
2744 if (lh
.version
< 2 || lh
.version
> 5)
2747 (_("DWARF error: unhandled .debug_line version %d"), lh
.version
);
2748 bfd_set_error (bfd_error_bad_value
);
2752 if (line_ptr
+ offset_size
+ (lh
.version
>= 5 ? 8 : (lh
.version
>= 4 ? 6 : 5))
2756 (_("DWARF error: ran out of room reading prologue"));
2757 bfd_set_error (bfd_error_bad_value
);
2761 if (lh
.version
>= 5)
2763 unsigned int segment_selector_size
;
2765 /* Skip address size. */
2766 read_1_byte (abfd
, &line_ptr
, line_end
);
2768 segment_selector_size
= read_1_byte (abfd
, &line_ptr
, line_end
);
2769 if (segment_selector_size
!= 0)
2772 (_("DWARF error: line info unsupported segment selector size %u"),
2773 segment_selector_size
);
2774 bfd_set_error (bfd_error_bad_value
);
2779 if (offset_size
== 4)
2780 lh
.prologue_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2782 lh
.prologue_length
= read_8_bytes (abfd
, &line_ptr
, line_end
);
2784 lh
.minimum_instruction_length
= read_1_byte (abfd
, &line_ptr
, line_end
);
2786 if (lh
.version
>= 4)
2787 lh
.maximum_ops_per_insn
= read_1_byte (abfd
, &line_ptr
, line_end
);
2789 lh
.maximum_ops_per_insn
= 1;
2791 if (lh
.maximum_ops_per_insn
== 0)
2794 (_("DWARF error: invalid maximum operations per instruction"));
2795 bfd_set_error (bfd_error_bad_value
);
2799 lh
.default_is_stmt
= read_1_byte (abfd
, &line_ptr
, line_end
);
2800 lh
.line_base
= read_1_signed_byte (abfd
, &line_ptr
, line_end
);
2801 lh
.line_range
= read_1_byte (abfd
, &line_ptr
, line_end
);
2802 lh
.opcode_base
= read_1_byte (abfd
, &line_ptr
, line_end
);
2804 if (line_ptr
+ (lh
.opcode_base
- 1) >= line_end
)
2806 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2807 bfd_set_error (bfd_error_bad_value
);
2811 amt
= lh
.opcode_base
* sizeof (unsigned char);
2812 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
2814 lh
.standard_opcode_lengths
[0] = 1;
2816 for (i
= 1; i
< lh
.opcode_base
; ++i
)
2817 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, &line_ptr
, line_end
);
2819 amt
= sizeof (struct line_info_table
);
2820 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
2824 table
->comp_dir
= unit
->comp_dir
;
2826 table
->num_files
= 0;
2827 table
->files
= NULL
;
2829 table
->num_dirs
= 0;
2832 table
->num_sequences
= 0;
2833 table
->sequences
= NULL
;
2835 table
->lcl_head
= NULL
;
2837 if (lh
.version
>= 5)
2839 /* Read directory table. */
2840 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2841 line_info_add_include_dir_stub
))
2844 /* Read file name table. */
2845 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2846 line_info_add_file_name
))
2848 table
->use_dir_and_file_0
= true;
2852 /* Read directory table. */
2853 while ((cur_dir
= read_string (&line_ptr
, line_end
)) != NULL
)
2855 if (!line_info_add_include_dir (table
, cur_dir
))
2859 /* Read file name table. */
2860 while ((cur_file
= read_string (&line_ptr
, line_end
)) != NULL
)
2862 unsigned int dir
, xtime
, size
;
2864 dir
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2865 xtime
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2866 size
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2868 if (!line_info_add_file_name (table
, cur_file
, dir
, xtime
, size
))
2871 table
->use_dir_and_file_0
= false;
2874 /* Read the statement sequences until there's nothing left. */
2875 while (line_ptr
< line_end
)
2877 /* State machine registers. */
2878 bfd_vma address
= 0;
2879 unsigned char op_index
= 0;
2880 char * filename
= NULL
;
2881 unsigned int line
= 1;
2882 unsigned int column
= 0;
2883 unsigned int discriminator
= 0;
2884 int is_stmt
= lh
.default_is_stmt
;
2885 int end_sequence
= 0;
2886 unsigned int dir
, xtime
, size
;
2887 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2888 compilers generate address sequences that are wildly out of
2889 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2890 for ia64-Linux). Thus, to determine the low and high
2891 address, we must compare on every DW_LNS_copy, etc. */
2892 bfd_vma low_pc
= (bfd_vma
) -1;
2893 bfd_vma high_pc
= 0;
2895 if (table
->num_files
)
2897 if (table
->use_dir_and_file_0
)
2898 filename
= concat_filename (table
, 0);
2900 filename
= concat_filename (table
, 1);
2903 /* Decode the table. */
2904 while (!end_sequence
&& line_ptr
< line_end
)
2906 op_code
= read_1_byte (abfd
, &line_ptr
, line_end
);
2908 if (op_code
>= lh
.opcode_base
)
2910 /* Special operand. */
2911 adj_opcode
= op_code
- lh
.opcode_base
;
2912 if (lh
.line_range
== 0)
2914 if (lh
.maximum_ops_per_insn
== 1)
2915 address
+= (adj_opcode
/ lh
.line_range
2916 * lh
.minimum_instruction_length
);
2919 address
+= ((op_index
+ adj_opcode
/ lh
.line_range
)
2920 / lh
.maximum_ops_per_insn
2921 * lh
.minimum_instruction_length
);
2922 op_index
= ((op_index
+ adj_opcode
/ lh
.line_range
)
2923 % lh
.maximum_ops_per_insn
);
2925 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
2926 /* Append row to matrix using current values. */
2927 if (!add_line_info (table
, address
, op_index
, filename
,
2928 line
, column
, discriminator
, 0))
2931 if (address
< low_pc
)
2933 if (address
> high_pc
)
2936 else switch (op_code
)
2938 case DW_LNS_extended_op
:
2939 exop_len
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2941 extended_op
= read_1_byte (abfd
, &line_ptr
, line_end
);
2943 switch (extended_op
)
2945 case DW_LNE_end_sequence
:
2947 if (!add_line_info (table
, address
, op_index
, filename
, line
,
2948 column
, discriminator
, end_sequence
))
2951 if (address
< low_pc
)
2953 if (address
> high_pc
)
2955 if (!arange_add (unit
, &unit
->arange
, &unit
->file
->trie_root
,
2959 case DW_LNE_set_address
:
2960 address
= read_address (unit
, &line_ptr
, line_end
);
2963 case DW_LNE_define_file
:
2964 cur_file
= read_string (&line_ptr
, line_end
);
2965 dir
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2967 xtime
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2969 size
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2971 if (!line_info_add_file_name (table
, cur_file
, dir
,
2975 case DW_LNE_set_discriminator
:
2976 discriminator
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2979 case DW_LNE_HP_source_file_correlation
:
2980 line_ptr
+= exop_len
- 1;
2984 (_("DWARF error: mangled line number section"));
2985 bfd_set_error (bfd_error_bad_value
);
2992 if (!add_line_info (table
, address
, op_index
,
2993 filename
, line
, column
, discriminator
, 0))
2996 if (address
< low_pc
)
2998 if (address
> high_pc
)
3001 case DW_LNS_advance_pc
:
3002 if (lh
.maximum_ops_per_insn
== 1)
3003 address
+= (lh
.minimum_instruction_length
3004 * _bfd_safe_read_leb128 (abfd
, &line_ptr
,
3008 bfd_vma adjust
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
3010 address
= ((op_index
+ adjust
) / lh
.maximum_ops_per_insn
3011 * lh
.minimum_instruction_length
);
3012 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
3015 case DW_LNS_advance_line
:
3016 line
+= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
3019 case DW_LNS_set_file
:
3021 unsigned int filenum
;
3023 /* The file and directory tables are 0
3024 based, the references are 1 based. */
3025 filenum
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
3028 filename
= concat_filename (table
, filenum
);
3031 case DW_LNS_set_column
:
3032 column
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
3035 case DW_LNS_negate_stmt
:
3036 is_stmt
= (!is_stmt
);
3038 case DW_LNS_set_basic_block
:
3040 case DW_LNS_const_add_pc
:
3041 if (lh
.line_range
== 0)
3043 if (lh
.maximum_ops_per_insn
== 1)
3044 address
+= (lh
.minimum_instruction_length
3045 * ((255 - lh
.opcode_base
) / lh
.line_range
));
3048 bfd_vma adjust
= ((255 - lh
.opcode_base
) / lh
.line_range
);
3049 address
+= (lh
.minimum_instruction_length
3050 * ((op_index
+ adjust
)
3051 / lh
.maximum_ops_per_insn
));
3052 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
3055 case DW_LNS_fixed_advance_pc
:
3056 address
+= read_2_bytes (abfd
, &line_ptr
, line_end
);
3060 /* Unknown standard opcode, ignore it. */
3061 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
3062 (void) _bfd_safe_read_leb128 (abfd
, &line_ptr
,
3071 if (unit
->line_offset
== 0)
3072 file
->line_table
= table
;
3073 if (sort_line_sequences (table
))
3077 while (table
->sequences
!= NULL
)
3079 struct line_sequence
* seq
= table
->sequences
;
3080 table
->sequences
= table
->sequences
->prev_sequence
;
3083 free (table
->files
);
3088 /* If ADDR is within TABLE set the output parameters and return TRUE,
3089 otherwise set *FILENAME_PTR to NULL and return FALSE.
3090 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
3091 are pointers to the objects to be filled in. */
3094 lookup_address_in_line_info_table (struct line_info_table
*table
,
3096 const char **filename_ptr
,
3097 unsigned int *linenumber_ptr
,
3098 unsigned int *discriminator_ptr
)
3100 struct line_sequence
*seq
= NULL
;
3101 struct line_info
*info
;
3104 /* Binary search the array of sequences. */
3106 high
= table
->num_sequences
;
3109 mid
= (low
+ high
) / 2;
3110 seq
= &table
->sequences
[mid
];
3111 if (addr
< seq
->low_pc
)
3113 else if (addr
>= seq
->last_line
->address
)
3119 /* Check for a valid sequence. */
3120 if (!seq
|| addr
< seq
->low_pc
|| addr
>= seq
->last_line
->address
)
3123 if (!build_line_info_table (table
, seq
))
3126 /* Binary search the array of line information. */
3128 high
= seq
->num_lines
;
3132 mid
= (low
+ high
) / 2;
3133 info
= seq
->line_info_lookup
[mid
];
3134 if (addr
< info
->address
)
3136 else if (addr
>= seq
->line_info_lookup
[mid
+ 1]->address
)
3142 /* Check for a valid line information entry. */
3144 && addr
>= info
->address
3145 && addr
< seq
->line_info_lookup
[mid
+ 1]->address
3146 && !(info
->end_sequence
|| info
== seq
->last_line
))
3148 *filename_ptr
= info
->filename
;
3149 *linenumber_ptr
= info
->line
;
3150 if (discriminator_ptr
)
3151 *discriminator_ptr
= info
->discriminator
;
3156 *filename_ptr
= NULL
;
3160 /* Read in the .debug_ranges section for future reference. */
3163 read_debug_ranges (struct comp_unit
* unit
)
3165 struct dwarf2_debug
*stash
= unit
->stash
;
3166 struct dwarf2_debug_file
*file
= unit
->file
;
3168 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_ranges
],
3170 &file
->dwarf_ranges_buffer
, &file
->dwarf_ranges_size
);
3173 /* Read in the .debug_rnglists section for future reference. */
3176 read_debug_rnglists (struct comp_unit
* unit
)
3178 struct dwarf2_debug
*stash
= unit
->stash
;
3179 struct dwarf2_debug_file
*file
= unit
->file
;
3181 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_rnglists
],
3183 &file
->dwarf_rnglists_buffer
, &file
->dwarf_rnglists_size
);
3186 /* Function table functions. */
3189 compare_lookup_funcinfos (const void * a
, const void * b
)
3191 const struct lookup_funcinfo
* lookup1
= a
;
3192 const struct lookup_funcinfo
* lookup2
= b
;
3194 if (lookup1
->low_addr
< lookup2
->low_addr
)
3196 if (lookup1
->low_addr
> lookup2
->low_addr
)
3198 if (lookup1
->high_addr
< lookup2
->high_addr
)
3200 if (lookup1
->high_addr
> lookup2
->high_addr
)
3203 if (lookup1
->idx
< lookup2
->idx
)
3205 if (lookup1
->idx
> lookup2
->idx
)
3211 build_lookup_funcinfo_table (struct comp_unit
* unit
)
3213 struct lookup_funcinfo
*lookup_funcinfo_table
= unit
->lookup_funcinfo_table
;
3214 unsigned int number_of_functions
= unit
->number_of_functions
;
3215 struct funcinfo
*each
;
3216 struct lookup_funcinfo
*entry
;
3218 struct arange
*range
;
3219 bfd_vma low_addr
, high_addr
;
3221 if (lookup_funcinfo_table
|| number_of_functions
== 0)
3224 /* Create the function info lookup table. */
3225 lookup_funcinfo_table
= (struct lookup_funcinfo
*)
3226 bfd_malloc (number_of_functions
* sizeof (struct lookup_funcinfo
));
3227 if (lookup_funcinfo_table
== NULL
)
3230 /* Populate the function info lookup table. */
3231 func_index
= number_of_functions
;
3232 for (each
= unit
->function_table
; each
; each
= each
->prev_func
)
3234 entry
= &lookup_funcinfo_table
[--func_index
];
3235 entry
->funcinfo
= each
;
3236 entry
->idx
= func_index
;
3238 /* Calculate the lowest and highest address for this function entry. */
3239 low_addr
= entry
->funcinfo
->arange
.low
;
3240 high_addr
= entry
->funcinfo
->arange
.high
;
3242 for (range
= entry
->funcinfo
->arange
.next
; range
; range
= range
->next
)
3244 if (range
->low
< low_addr
)
3245 low_addr
= range
->low
;
3246 if (range
->high
> high_addr
)
3247 high_addr
= range
->high
;
3250 entry
->low_addr
= low_addr
;
3251 entry
->high_addr
= high_addr
;
3254 BFD_ASSERT (func_index
== 0);
3256 /* Sort the function by address. */
3257 qsort (lookup_funcinfo_table
,
3258 number_of_functions
,
3259 sizeof (struct lookup_funcinfo
),
3260 compare_lookup_funcinfos
);
3262 /* Calculate the high watermark for each function in the lookup table. */
3263 high_addr
= lookup_funcinfo_table
[0].high_addr
;
3264 for (func_index
= 1; func_index
< number_of_functions
; func_index
++)
3266 entry
= &lookup_funcinfo_table
[func_index
];
3267 if (entry
->high_addr
> high_addr
)
3268 high_addr
= entry
->high_addr
;
3270 entry
->high_addr
= high_addr
;
3273 unit
->lookup_funcinfo_table
= lookup_funcinfo_table
;
3277 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
3278 TRUE. Note that we need to find the function that has the smallest range
3279 that contains ADDR, to handle inlined functions without depending upon
3280 them being ordered in TABLE by increasing range. */
3283 lookup_address_in_function_table (struct comp_unit
*unit
,
3285 struct funcinfo
**function_ptr
)
3287 unsigned int number_of_functions
= unit
->number_of_functions
;
3288 struct lookup_funcinfo
* lookup_funcinfo
= NULL
;
3289 struct funcinfo
* funcinfo
= NULL
;
3290 struct funcinfo
* best_fit
= NULL
;
3291 bfd_vma best_fit_len
= (bfd_vma
) -1;
3292 bfd_size_type low
, high
, mid
, first
;
3293 struct arange
*arange
;
3295 if (number_of_functions
== 0)
3298 if (!build_lookup_funcinfo_table (unit
))
3301 if (unit
->lookup_funcinfo_table
[number_of_functions
- 1].high_addr
< addr
)
3304 /* Find the first function in the lookup table which may contain the
3305 specified address. */
3307 high
= number_of_functions
;
3311 mid
= (low
+ high
) / 2;
3312 lookup_funcinfo
= &unit
->lookup_funcinfo_table
[mid
];
3313 if (addr
< lookup_funcinfo
->low_addr
)
3315 else if (addr
>= lookup_funcinfo
->high_addr
)
3321 /* Find the 'best' match for the address. The prior algorithm defined the
3322 best match as the function with the smallest address range containing
3323 the specified address. This definition should probably be changed to the
3324 innermost inline routine containing the address, but right now we want
3325 to get the same results we did before. */
3326 while (first
< number_of_functions
)
3328 if (addr
< unit
->lookup_funcinfo_table
[first
].low_addr
)
3330 funcinfo
= unit
->lookup_funcinfo_table
[first
].funcinfo
;
3332 for (arange
= &funcinfo
->arange
; arange
; arange
= arange
->next
)
3334 if (addr
< arange
->low
|| addr
>= arange
->high
)
3337 if (arange
->high
- arange
->low
< best_fit_len
3338 /* The following comparison is designed to return the same
3339 match as the previous algorithm for routines which have the
3340 same best fit length. */
3341 || (arange
->high
- arange
->low
== best_fit_len
3342 && funcinfo
> best_fit
))
3344 best_fit
= funcinfo
;
3345 best_fit_len
= arange
->high
- arange
->low
;
3355 *function_ptr
= best_fit
;
3359 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
3360 and LINENUMBER_PTR, and return TRUE. */
3363 lookup_symbol_in_function_table (struct comp_unit
*unit
,
3366 const char **filename_ptr
,
3367 unsigned int *linenumber_ptr
)
3369 struct funcinfo
* each
;
3370 struct funcinfo
* best_fit
= NULL
;
3371 bfd_vma best_fit_len
= (bfd_vma
) -1;
3372 struct arange
*arange
;
3373 const char *name
= bfd_asymbol_name (sym
);
3375 for (each
= unit
->function_table
; each
; each
= each
->prev_func
)
3376 for (arange
= &each
->arange
; arange
; arange
= arange
->next
)
3377 if (addr
>= arange
->low
3378 && addr
< arange
->high
3379 && arange
->high
- arange
->low
< best_fit_len
3382 && strstr (name
, each
->name
) != NULL
)
3385 best_fit_len
= arange
->high
- arange
->low
;
3390 *filename_ptr
= best_fit
->file
;
3391 *linenumber_ptr
= best_fit
->line
;
3398 /* Variable table functions. */
3400 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
3401 LINENUMBER_PTR, and return TRUE. */
3404 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
3407 const char **filename_ptr
,
3408 unsigned int *linenumber_ptr
)
3410 struct varinfo
* each
;
3411 const char *name
= bfd_asymbol_name (sym
);
3413 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
3414 if (each
->addr
== addr
3416 && each
->file
!= NULL
3417 && each
->name
!= NULL
3418 && strstr (name
, each
->name
) != NULL
)
3423 *filename_ptr
= each
->file
;
3424 *linenumber_ptr
= each
->line
;
3431 static struct comp_unit
*stash_comp_unit (struct dwarf2_debug
*,
3432 struct dwarf2_debug_file
*);
3433 static bool comp_unit_maybe_decode_line_info (struct comp_unit
*);
3436 find_abstract_instance (struct comp_unit
*unit
,
3437 struct attribute
*attr_ptr
,
3438 unsigned int recur_count
,
3441 char **filename_ptr
,
3442 int *linenumber_ptr
)
3444 bfd
*abfd
= unit
->abfd
;
3445 bfd_byte
*info_ptr
= NULL
;
3446 bfd_byte
*info_ptr_end
;
3447 unsigned int abbrev_number
, i
;
3448 struct abbrev_info
*abbrev
;
3449 uint64_t die_ref
= attr_ptr
->u
.val
;
3450 struct attribute attr
;
3452 if (recur_count
== 100)
3455 (_("DWARF error: abstract instance recursion detected"));
3456 bfd_set_error (bfd_error_bad_value
);
3460 /* DW_FORM_ref_addr can reference an entry in a different CU. It
3461 is an offset from the .debug_info section, not the current CU. */
3462 if (attr_ptr
->form
== DW_FORM_ref_addr
)
3464 /* We only support DW_FORM_ref_addr within the same file, so
3465 any relocations should be resolved already. Check this by
3466 testing for a zero die_ref; There can't be a valid reference
3467 to the header of a .debug_info section.
3468 DW_FORM_ref_addr is an offset relative to .debug_info.
3469 Normally when using the GNU linker this is accomplished by
3470 emitting a symbolic reference to a label, because .debug_info
3471 sections are linked at zero. When there are multiple section
3472 groups containing .debug_info, as there might be in a
3473 relocatable object file, it would be reasonable to assume that
3474 a symbolic reference to a label in any .debug_info section
3475 might be used. Since we lay out multiple .debug_info
3476 sections at non-zero VMAs (see place_sections), and read
3477 them contiguously into dwarf_info_buffer, that means the
3478 reference is relative to dwarf_info_buffer. */
3481 info_ptr
= unit
->file
->dwarf_info_buffer
;
3482 info_ptr_end
= info_ptr
+ unit
->file
->dwarf_info_size
;
3483 total
= info_ptr_end
- info_ptr
;
3486 else if (die_ref
>= total
)
3489 (_("DWARF error: invalid abstract instance DIE ref"));
3490 bfd_set_error (bfd_error_bad_value
);
3493 info_ptr
+= die_ref
;
3495 else if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3497 bool first_time
= unit
->stash
->alt
.dwarf_info_buffer
== NULL
;
3499 info_ptr
= read_alt_indirect_ref (unit
, die_ref
);
3501 unit
->stash
->alt
.info_ptr
= unit
->stash
->alt
.dwarf_info_buffer
;
3502 if (info_ptr
== NULL
)
3505 (_("DWARF error: unable to read alt ref %" PRIu64
),
3506 (uint64_t) die_ref
);
3507 bfd_set_error (bfd_error_bad_value
);
3510 info_ptr_end
= (unit
->stash
->alt
.dwarf_info_buffer
3511 + unit
->stash
->alt
.dwarf_info_size
);
3512 if (unit
->stash
->alt
.all_comp_units
)
3513 unit
= unit
->stash
->alt
.all_comp_units
;
3516 if (attr_ptr
->form
== DW_FORM_ref_addr
3517 || attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3519 /* Now find the CU containing this pointer. */
3520 if (info_ptr
>= unit
->info_ptr_unit
&& info_ptr
< unit
->end_ptr
)
3521 info_ptr_end
= unit
->end_ptr
;
3524 /* Check other CUs to see if they contain the abbrev. */
3525 struct comp_unit
*u
= NULL
;
3526 struct addr_range range
= { info_ptr
, info_ptr
};
3527 splay_tree_node v
= splay_tree_lookup (unit
->file
->comp_unit_tree
,
3528 (splay_tree_key
)&range
);
3530 u
= (struct comp_unit
*)v
->value
;
3532 if (attr_ptr
->form
== DW_FORM_ref_addr
)
3535 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->f
);
3538 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3543 if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3546 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->alt
);
3549 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3557 (_("DWARF error: unable to locate abstract instance DIE ref %"
3558 PRIu64
), (uint64_t) die_ref
);
3559 bfd_set_error (bfd_error_bad_value
);
3563 info_ptr_end
= unit
->end_ptr
;
3568 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
3569 DW_FORM_ref_udata. These are all references relative to the
3570 start of the current CU. */
3573 info_ptr
= unit
->info_ptr_unit
;
3574 info_ptr_end
= unit
->end_ptr
;
3575 total
= info_ptr_end
- info_ptr
;
3576 if (!die_ref
|| die_ref
>= total
)
3579 (_("DWARF error: invalid abstract instance DIE ref"));
3580 bfd_set_error (bfd_error_bad_value
);
3583 info_ptr
+= die_ref
;
3586 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3587 false, info_ptr_end
);
3590 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3594 (_("DWARF error: could not find abbrev number %u"), abbrev_number
);
3595 bfd_set_error (bfd_error_bad_value
);
3600 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3602 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
,
3603 info_ptr
, info_ptr_end
);
3604 if (info_ptr
== NULL
)
3609 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3611 if (*pname
== NULL
&& is_str_form (&attr
))
3613 *pname
= attr
.u
.str
;
3614 if (mangle_style (unit
->lang
) == 0)
3618 case DW_AT_specification
:
3619 if (is_int_form (&attr
)
3620 && !find_abstract_instance (unit
, &attr
, recur_count
+ 1,
3622 filename_ptr
, linenumber_ptr
))
3625 case DW_AT_linkage_name
:
3626 case DW_AT_MIPS_linkage_name
:
3627 /* PR 16949: Corrupt debug info can place
3628 non-string forms into these attributes. */
3629 if (is_str_form (&attr
))
3631 *pname
= attr
.u
.str
;
3635 case DW_AT_decl_file
:
3636 if (!comp_unit_maybe_decode_line_info (unit
))
3638 if (is_int_form (&attr
))
3640 free (*filename_ptr
);
3641 *filename_ptr
= concat_filename (unit
->line_table
,
3645 case DW_AT_decl_line
:
3646 if (is_int_form (&attr
))
3647 *linenumber_ptr
= attr
.u
.val
;
3659 read_ranges (struct comp_unit
*unit
, struct arange
*arange
,
3660 struct trie_node
**trie_root
, uint64_t offset
)
3662 bfd_byte
*ranges_ptr
;
3663 bfd_byte
*ranges_end
;
3664 bfd_vma base_address
= unit
->base_address
;
3666 if (! unit
->file
->dwarf_ranges_buffer
)
3668 if (! read_debug_ranges (unit
))
3672 if (offset
> unit
->file
->dwarf_ranges_size
)
3674 ranges_ptr
= unit
->file
->dwarf_ranges_buffer
+ offset
;
3675 ranges_end
= unit
->file
->dwarf_ranges_buffer
+ unit
->file
->dwarf_ranges_size
;
3682 /* PR 17512: file: 62cada7d. */
3683 if (2u * unit
->addr_size
> (size_t) (ranges_end
- ranges_ptr
))
3686 low_pc
= read_address (unit
, &ranges_ptr
, ranges_end
);
3687 high_pc
= read_address (unit
, &ranges_ptr
, ranges_end
);
3689 if (low_pc
== 0 && high_pc
== 0)
3691 if (low_pc
== -1UL && high_pc
!= -1UL)
3692 base_address
= high_pc
;
3695 if (!arange_add (unit
, arange
, trie_root
,
3696 base_address
+ low_pc
, base_address
+ high_pc
))
3704 read_rnglists (struct comp_unit
*unit
, struct arange
*arange
,
3705 struct trie_node
**trie_root
, uint64_t offset
)
3709 bfd_vma base_address
= unit
->base_address
;
3712 bfd
*abfd
= unit
->abfd
;
3714 if (! unit
->file
->dwarf_rnglists_buffer
)
3716 if (! read_debug_rnglists (unit
))
3720 rngs_ptr
= unit
->file
->dwarf_rnglists_buffer
+ offset
;
3721 if (rngs_ptr
< unit
->file
->dwarf_rnglists_buffer
)
3723 rngs_end
= unit
->file
->dwarf_rnglists_buffer
;
3724 rngs_end
+= unit
->file
->dwarf_rnglists_size
;
3728 enum dwarf_range_list_entry rlet
;
3730 if (rngs_ptr
>= rngs_end
)
3733 rlet
= read_1_byte (abfd
, &rngs_ptr
, rngs_end
);
3737 case DW_RLE_end_of_list
:
3740 case DW_RLE_base_address
:
3741 if (unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3743 base_address
= read_address (unit
, &rngs_ptr
, rngs_end
);
3746 case DW_RLE_start_length
:
3747 if (unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3749 low_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3751 high_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3755 case DW_RLE_offset_pair
:
3756 low_pc
= base_address
;
3757 low_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3759 high_pc
= base_address
;
3760 high_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3764 case DW_RLE_start_end
:
3765 if (2u * unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3767 low_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3768 high_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3771 /* TODO x-variants need .debug_addr support used for split-dwarf. */
3772 case DW_RLE_base_addressx
:
3773 case DW_RLE_startx_endx
:
3774 case DW_RLE_startx_length
:
3779 if (!arange_add (unit
, arange
, trie_root
, low_pc
, high_pc
))
3785 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
,
3786 struct trie_node
**trie_root
, uint64_t offset
)
3788 if (unit
->version
<= 4)
3789 return read_ranges (unit
, arange
, trie_root
, offset
);
3791 return read_rnglists (unit
, arange
, trie_root
, offset
);
3794 static struct funcinfo
*
3795 lookup_func_by_offset (uint64_t offset
, struct funcinfo
* table
)
3797 for (; table
!= NULL
; table
= table
->prev_func
)
3798 if (table
->unit_offset
== offset
)
3803 static struct varinfo
*
3804 lookup_var_by_offset (uint64_t offset
, struct varinfo
* table
)
3808 if (table
->unit_offset
== offset
)
3810 table
= table
->prev_var
;
3817 /* DWARF2 Compilation unit functions. */
3819 static struct funcinfo
*
3820 reverse_funcinfo_list (struct funcinfo
*head
)
3822 struct funcinfo
*rhead
;
3823 struct funcinfo
*temp
;
3825 for (rhead
= NULL
; head
; head
= temp
)
3827 temp
= head
->prev_func
;
3828 head
->prev_func
= rhead
;
3834 static struct varinfo
*
3835 reverse_varinfo_list (struct varinfo
*head
)
3837 struct varinfo
*rhead
;
3838 struct varinfo
*temp
;
3840 for (rhead
= NULL
; head
; head
= temp
)
3842 temp
= head
->prev_var
;
3843 head
->prev_var
= rhead
;
3849 /* Scan over each die in a comp. unit looking for functions to add
3850 to the function table and variables to the variable table. */
3853 scan_unit_for_symbols (struct comp_unit
*unit
)
3855 bfd
*abfd
= unit
->abfd
;
3856 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
3857 bfd_byte
*info_ptr_end
= unit
->end_ptr
;
3858 int nesting_level
= 0;
3859 struct nest_funcinfo
3861 struct funcinfo
*func
;
3863 int nested_funcs_size
;
3864 struct funcinfo
*last_func
;
3865 struct varinfo
*last_var
;
3867 /* Maintain a stack of in-scope functions and inlined functions, which we
3868 can use to set the caller_func field. */
3869 nested_funcs_size
= 32;
3870 nested_funcs
= (struct nest_funcinfo
*)
3871 bfd_malloc (nested_funcs_size
* sizeof (*nested_funcs
));
3872 if (nested_funcs
== NULL
)
3874 nested_funcs
[nesting_level
].func
= 0;
3876 /* PR 27484: We must scan the DIEs twice. The first time we look for
3877 function and variable tags and accumulate them into their respective
3878 tables. The second time through we process the attributes of the
3879 functions/variables and augment the table entries. */
3880 while (nesting_level
>= 0)
3882 unsigned int abbrev_number
, i
;
3883 struct abbrev_info
*abbrev
;
3884 struct funcinfo
*func
;
3885 struct varinfo
*var
;
3886 uint64_t current_offset
;
3888 /* PR 17512: file: 9f405d9d. */
3889 if (info_ptr
>= info_ptr_end
)
3892 current_offset
= info_ptr
- unit
->info_ptr_unit
;
3893 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3894 false, info_ptr_end
);
3895 if (abbrev_number
== 0)
3901 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3904 static unsigned int previous_failed_abbrev
= -1U;
3906 /* Avoid multiple reports of the same missing abbrev. */
3907 if (abbrev_number
!= previous_failed_abbrev
)
3910 (_("DWARF error: could not find abbrev number %u"),
3912 previous_failed_abbrev
= abbrev_number
;
3914 bfd_set_error (bfd_error_bad_value
);
3918 if (abbrev
->tag
== DW_TAG_subprogram
3919 || abbrev
->tag
== DW_TAG_entry_point
3920 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3922 size_t amt
= sizeof (struct funcinfo
);
3925 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
3928 func
->tag
= abbrev
->tag
;
3929 func
->prev_func
= unit
->function_table
;
3930 func
->unit_offset
= current_offset
;
3931 unit
->function_table
= func
;
3932 unit
->number_of_functions
++;
3933 BFD_ASSERT (!unit
->cached
);
3935 if (func
->tag
== DW_TAG_inlined_subroutine
)
3936 for (i
= nesting_level
; i
-- != 0; )
3937 if (nested_funcs
[i
].func
)
3939 func
->caller_func
= nested_funcs
[i
].func
;
3942 nested_funcs
[nesting_level
].func
= func
;
3947 if (abbrev
->tag
== DW_TAG_variable
3948 || abbrev
->tag
== DW_TAG_member
)
3950 size_t amt
= sizeof (struct varinfo
);
3952 var
= (struct varinfo
*) bfd_zalloc (abfd
, amt
);
3955 var
->tag
= abbrev
->tag
;
3957 var
->prev_var
= unit
->variable_table
;
3958 unit
->variable_table
= var
;
3959 var
->unit_offset
= current_offset
;
3960 /* PR 18205: Missing debug information can cause this
3961 var to be attached to an already cached unit. */
3966 /* No inline function in scope at this nesting level. */
3967 nested_funcs
[nesting_level
].func
= 0;
3970 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3972 struct attribute attr
;
3974 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
3975 unit
, info_ptr
, info_ptr_end
);
3976 if (info_ptr
== NULL
)
3980 if (abbrev
->has_children
)
3984 if (nesting_level
>= nested_funcs_size
)
3986 struct nest_funcinfo
*tmp
;
3988 nested_funcs_size
*= 2;
3989 tmp
= (struct nest_funcinfo
*)
3990 bfd_realloc (nested_funcs
,
3991 nested_funcs_size
* sizeof (*nested_funcs
));
3996 nested_funcs
[nesting_level
].func
= 0;
4000 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4001 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4003 /* This is the second pass over the abbrevs. */
4004 info_ptr
= unit
->first_child_die_ptr
;
4010 while (nesting_level
>= 0)
4012 unsigned int abbrev_number
, i
;
4013 struct abbrev_info
*abbrev
;
4014 struct attribute attr
;
4015 struct funcinfo
*func
;
4016 struct varinfo
*var
;
4018 bfd_vma high_pc
= 0;
4019 bool high_pc_relative
= false;
4020 uint64_t current_offset
;
4022 /* PR 17512: file: 9f405d9d. */
4023 if (info_ptr
>= info_ptr_end
)
4026 current_offset
= info_ptr
- unit
->info_ptr_unit
;
4027 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
4028 false, info_ptr_end
);
4029 if (! abbrev_number
)
4035 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
4036 /* This should have been handled above. */
4037 BFD_ASSERT (abbrev
!= NULL
);
4041 if (abbrev
->tag
== DW_TAG_subprogram
4042 || abbrev
->tag
== DW_TAG_entry_point
4043 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
4046 && last_func
->prev_func
4047 && last_func
->prev_func
->unit_offset
== current_offset
)
4048 func
= last_func
->prev_func
;
4050 func
= lookup_func_by_offset (current_offset
, unit
->function_table
);
4057 else if (abbrev
->tag
== DW_TAG_variable
4058 || abbrev
->tag
== DW_TAG_member
)
4061 && last_var
->prev_var
4062 && last_var
->prev_var
->unit_offset
== current_offset
)
4063 var
= last_var
->prev_var
;
4065 var
= lookup_var_by_offset (current_offset
, unit
->variable_table
);
4073 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
4075 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
4076 unit
, info_ptr
, info_ptr_end
);
4077 if (info_ptr
== NULL
)
4084 case DW_AT_call_file
:
4085 if (is_int_form (&attr
))
4086 func
->caller_file
= concat_filename (unit
->line_table
,
4090 case DW_AT_call_line
:
4091 if (is_int_form (&attr
))
4092 func
->caller_line
= attr
.u
.val
;
4095 case DW_AT_abstract_origin
:
4096 case DW_AT_specification
:
4097 if (is_int_form (&attr
)
4098 && !find_abstract_instance (unit
, &attr
, 0,
4107 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
4109 if (func
->name
== NULL
&& is_str_form (&attr
))
4111 func
->name
= attr
.u
.str
;
4112 if (mangle_style (unit
->lang
) == 0)
4113 func
->is_linkage
= true;
4117 case DW_AT_linkage_name
:
4118 case DW_AT_MIPS_linkage_name
:
4119 /* PR 16949: Corrupt debug info can place
4120 non-string forms into these attributes. */
4121 if (is_str_form (&attr
))
4123 func
->name
= attr
.u
.str
;
4124 func
->is_linkage
= true;
4129 if (is_int_form (&attr
))
4130 low_pc
= attr
.u
.val
;
4134 if (is_int_form (&attr
))
4136 high_pc
= attr
.u
.val
;
4137 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
4142 if (is_int_form (&attr
)
4143 && !read_rangelist (unit
, &func
->arange
,
4144 &unit
->file
->trie_root
, attr
.u
.val
))
4148 case DW_AT_decl_file
:
4149 if (is_int_form (&attr
))
4152 func
->file
= concat_filename (unit
->line_table
,
4157 case DW_AT_decl_line
:
4158 if (is_int_form (&attr
))
4159 func
->line
= attr
.u
.val
;
4170 case DW_AT_specification
:
4171 if (is_int_form (&attr
) && attr
.u
.val
)
4174 if (!find_abstract_instance (unit
, &attr
, 0,
4180 _bfd_error_handler (_("DWARF error: could not find "
4181 "variable specification "
4183 (unsigned long) attr
.u
.val
);
4190 if (is_str_form (&attr
))
4191 var
->name
= attr
.u
.str
;
4194 case DW_AT_decl_file
:
4195 if (is_int_form (&attr
))
4198 var
->file
= concat_filename (unit
->line_table
,
4203 case DW_AT_decl_line
:
4204 if (is_int_form (&attr
))
4205 var
->line
= attr
.u
.val
;
4208 case DW_AT_external
:
4209 if (is_int_form (&attr
) && attr
.u
.val
!= 0)
4213 case DW_AT_location
:
4217 case DW_FORM_block1
:
4218 case DW_FORM_block2
:
4219 case DW_FORM_block4
:
4220 case DW_FORM_exprloc
:
4221 if (attr
.u
.blk
->data
!= NULL
4222 && *attr
.u
.blk
->data
== DW_OP_addr
)
4226 /* Verify that DW_OP_addr is the only opcode in the
4227 location, in which case the block size will be 1
4228 plus the address size. */
4229 /* ??? For TLS variables, gcc can emit
4230 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
4231 which we don't handle here yet. */
4232 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
4233 var
->addr
= bfd_get (unit
->addr_size
* 8,
4235 attr
.u
.blk
->data
+ 1);
4250 if (abbrev
->has_children
)
4253 if (high_pc_relative
)
4256 if (func
&& high_pc
!= 0)
4258 if (!arange_add (unit
, &func
->arange
, &unit
->file
->trie_root
,
4264 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4265 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4267 free (nested_funcs
);
4271 free (nested_funcs
);
4275 /* Read the attributes of the form strx and addrx. */
4278 reread_attribute (struct comp_unit
*unit
,
4279 struct attribute
*attr
,
4282 bool *high_pc_relative
,
4285 if (is_strx_form (attr
->form
))
4286 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
4287 if (is_addrx_form (attr
->form
))
4288 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
4292 case DW_AT_stmt_list
:
4294 unit
->line_offset
= attr
->u
.val
;
4298 if (is_str_form (attr
))
4299 unit
->name
= attr
->u
.str
;
4303 *low_pc
= attr
->u
.val
;
4305 unit
->base_address
= *low_pc
;
4309 *high_pc
= attr
->u
.val
;
4310 *high_pc_relative
= attr
->form
!= DW_FORM_addr
;
4314 if (!read_rangelist (unit
, &unit
->arange
,
4315 &unit
->file
->trie_root
, attr
->u
.val
))
4319 case DW_AT_comp_dir
:
4321 char *comp_dir
= attr
->u
.str
;
4323 if (!is_str_form (attr
))
4326 (_("DWARF error: DW_AT_comp_dir attribute encountered "
4327 "with a non-string form"));
4333 char *cp
= strchr (comp_dir
, ':');
4335 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
4338 unit
->comp_dir
= comp_dir
;
4342 case DW_AT_language
:
4343 unit
->lang
= attr
->u
.val
;
4349 /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
4350 includes the compilation unit header that proceeds the DIE's, but
4351 does not include the length field that precedes each compilation
4352 unit header. END_PTR points one past the end of this comp unit.
4353 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
4355 This routine does not read the whole compilation unit; only enough
4356 to get to the line number information for the compilation unit. */
4358 static struct comp_unit
*
4359 parse_comp_unit (struct dwarf2_debug
*stash
,
4360 struct dwarf2_debug_file
*file
,
4362 bfd_vma unit_length
,
4363 bfd_byte
*info_ptr_unit
,
4364 unsigned int offset_size
)
4366 struct comp_unit
* unit
;
4367 unsigned int version
;
4368 uint64_t abbrev_offset
= 0;
4369 /* Initialize it just to avoid a GCC false warning. */
4370 unsigned int addr_size
= -1;
4371 struct abbrev_info
** abbrevs
;
4372 unsigned int abbrev_number
, i
;
4373 struct abbrev_info
*abbrev
;
4374 struct attribute attr
;
4375 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
4378 bfd_vma high_pc
= 0;
4379 bfd
*abfd
= file
->bfd_ptr
;
4380 bool high_pc_relative
= false;
4381 enum dwarf_unit_type unit_type
;
4382 struct attribute
*str_addrp
= NULL
;
4383 size_t str_count
= 0;
4384 size_t str_alloc
= 0;
4385 bool compunit_flag
= false;
4387 version
= read_2_bytes (abfd
, &info_ptr
, end_ptr
);
4388 if (version
< 2 || version
> 5)
4390 /* PR 19872: A version number of 0 probably means that there is padding
4391 at the end of the .debug_info section. Gold puts it there when
4392 performing an incremental link, for example. So do not generate
4393 an error, just return a NULL. */
4397 (_("DWARF error: found dwarf version '%u', this reader"
4398 " only handles version 2, 3, 4 and 5 information"), version
);
4399 bfd_set_error (bfd_error_bad_value
);
4405 unit_type
= DW_UT_compile
;
4408 unit_type
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
4409 addr_size
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
4412 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
4413 if (offset_size
== 4)
4414 abbrev_offset
= read_4_bytes (abfd
, &info_ptr
, end_ptr
);
4416 abbrev_offset
= read_8_bytes (abfd
, &info_ptr
, end_ptr
);
4419 addr_size
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
4424 /* Skip type signature. */
4427 /* Skip type offset. */
4428 info_ptr
+= offset_size
;
4431 case DW_UT_skeleton
:
4432 /* Skip DWO_id field. */
4440 if (addr_size
> sizeof (bfd_vma
))
4443 /* xgettext: c-format */
4444 (_("DWARF error: found address size '%u', this reader"
4445 " can not handle sizes greater than '%u'"),
4447 (unsigned int) sizeof (bfd_vma
));
4448 bfd_set_error (bfd_error_bad_value
);
4452 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
4455 ("DWARF error: found address size '%u', this reader"
4456 " can only handle address sizes '2', '4' and '8'", addr_size
);
4457 bfd_set_error (bfd_error_bad_value
);
4461 /* Read the abbrevs for this compilation unit into a table. */
4462 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
, file
);
4466 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
4468 if (! abbrev_number
)
4470 /* PR 19872: An abbrev number of 0 probably means that there is padding
4471 at the end of the .debug_abbrev section. Gold puts it there when
4472 performing an incremental link, for example. So do not generate
4473 an error, just return a NULL. */
4477 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
4480 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
4482 bfd_set_error (bfd_error_bad_value
);
4486 amt
= sizeof (struct comp_unit
);
4487 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
4491 unit
->version
= version
;
4492 unit
->addr_size
= addr_size
;
4493 unit
->offset_size
= offset_size
;
4494 unit
->abbrevs
= abbrevs
;
4495 unit
->end_ptr
= end_ptr
;
4496 unit
->stash
= stash
;
4498 unit
->info_ptr_unit
= info_ptr_unit
;
4500 if (abbrev
->tag
== DW_TAG_compile_unit
)
4501 compunit_flag
= true;
4503 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
4505 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
, end_ptr
);
4506 if (info_ptr
== NULL
)
4509 /* Identify attributes of the form strx* and addrx* which come before
4510 DW_AT_str_offsets_base and DW_AT_addr_base respectively in the CU.
4511 Store the attributes in an array and process them later. */
4512 if ((unit
->dwarf_str_offset
== 0 && is_strx_form (attr
.form
))
4513 || (unit
->dwarf_addr_offset
== 0 && is_addrx_form (attr
.form
)))
4515 if (str_count
<= str_alloc
)
4517 str_alloc
= 2 * str_alloc
+ 200;
4518 str_addrp
= bfd_realloc (str_addrp
,
4519 str_alloc
* sizeof (*str_addrp
));
4520 if (str_addrp
== NULL
)
4523 str_addrp
[str_count
] = attr
;
4528 /* Store the data if it is of an attribute we want to keep in a
4529 partial symbol table. */
4532 case DW_AT_stmt_list
:
4533 if (is_int_form (&attr
))
4536 unit
->line_offset
= attr
.u
.val
;
4541 if (is_str_form (&attr
))
4542 unit
->name
= attr
.u
.str
;
4546 if (is_int_form (&attr
))
4548 low_pc
= attr
.u
.val
;
4549 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
4550 this is the base address to use when reading location
4551 lists or range lists. */
4553 unit
->base_address
= low_pc
;
4558 if (is_int_form (&attr
))
4560 high_pc
= attr
.u
.val
;
4561 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
4566 if (is_int_form (&attr
)
4567 && !read_rangelist (unit
, &unit
->arange
,
4568 &unit
->file
->trie_root
, attr
.u
.val
))
4572 case DW_AT_comp_dir
:
4574 char *comp_dir
= attr
.u
.str
;
4576 /* PR 17512: file: 1fe726be. */
4577 if (!is_str_form (&attr
))
4580 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
4586 /* Irix 6.2 native cc prepends <machine>.: to the compilation
4587 directory, get rid of it. */
4588 char *cp
= strchr (comp_dir
, ':');
4590 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
4593 unit
->comp_dir
= comp_dir
;
4597 case DW_AT_language
:
4598 if (is_int_form (&attr
))
4599 unit
->lang
= attr
.u
.val
;
4602 case DW_AT_addr_base
:
4603 unit
->dwarf_addr_offset
= attr
.u
.val
;
4606 case DW_AT_str_offsets_base
:
4607 unit
->dwarf_str_offset
= attr
.u
.val
;
4615 for (i
= 0; i
< str_count
; ++i
)
4616 reread_attribute (unit
, &str_addrp
[i
], &low_pc
, &high_pc
,
4617 &high_pc_relative
, compunit_flag
);
4619 if (high_pc_relative
)
4623 if (!arange_add (unit
, &unit
->arange
, &unit
->file
->trie_root
,
4628 unit
->first_child_die_ptr
= info_ptr
;
4639 /* Return TRUE if UNIT may contain the address given by ADDR. When
4640 there are functions written entirely with inline asm statements, the
4641 range info in the compilation unit header may not be correct. We
4642 need to consult the line info table to see if a compilation unit
4643 really contains the given address. */
4646 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
4648 struct arange
*arange
;
4653 arange
= &unit
->arange
;
4656 if (addr
>= arange
->low
&& addr
< arange
->high
)
4658 arange
= arange
->next
;
4665 /* If UNIT contains ADDR, set the output parameters to the values for
4666 the line containing ADDR and return TRUE. Otherwise return FALSE.
4667 The output parameters, FILENAME_PTR, FUNCTION_PTR, and
4668 LINENUMBER_PTR, are pointers to the objects to be filled in. */
4671 comp_unit_find_nearest_line (struct comp_unit
*unit
,
4673 const char **filename_ptr
,
4674 struct funcinfo
**function_ptr
,
4675 unsigned int *linenumber_ptr
,
4676 unsigned int *discriminator_ptr
)
4678 bool line_p
, func_p
;
4680 if (!comp_unit_maybe_decode_line_info (unit
))
4683 *function_ptr
= NULL
;
4684 func_p
= lookup_address_in_function_table (unit
, addr
, function_ptr
);
4685 if (func_p
&& (*function_ptr
)->tag
== DW_TAG_inlined_subroutine
)
4686 unit
->stash
->inliner_chain
= *function_ptr
;
4688 line_p
= lookup_address_in_line_info_table (unit
->line_table
, addr
,
4692 return line_p
|| func_p
;
4695 /* Check to see if line info is already decoded in a comp_unit.
4696 If not, decode it. Returns TRUE if no errors were encountered;
4700 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
)
4705 if (! unit
->line_table
)
4707 if (! unit
->stmtlist
)
4713 unit
->line_table
= decode_line_info (unit
);
4715 if (! unit
->line_table
)
4721 if (unit
->first_child_die_ptr
< unit
->end_ptr
4722 && ! scan_unit_for_symbols (unit
))
4732 /* If UNIT contains SYM at ADDR, set the output parameters to the
4733 values for the line containing SYM. The output parameters,
4734 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
4737 Return TRUE if UNIT contains SYM, and no errors were encountered;
4741 comp_unit_find_line (struct comp_unit
*unit
,
4744 const char **filename_ptr
,
4745 unsigned int *linenumber_ptr
)
4747 if (!comp_unit_maybe_decode_line_info (unit
))
4750 if (sym
->flags
& BSF_FUNCTION
)
4751 return lookup_symbol_in_function_table (unit
, sym
, addr
,
4755 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
4760 /* Extract all interesting funcinfos and varinfos of a compilation
4761 unit into hash tables for faster lookup. Returns TRUE if no
4762 errors were enountered; FALSE otherwise. */
4765 comp_unit_hash_info (struct dwarf2_debug
*stash
,
4766 struct comp_unit
*unit
,
4767 struct info_hash_table
*funcinfo_hash_table
,
4768 struct info_hash_table
*varinfo_hash_table
)
4770 struct funcinfo
* each_func
;
4771 struct varinfo
* each_var
;
4774 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
4776 if (!comp_unit_maybe_decode_line_info (unit
))
4779 BFD_ASSERT (!unit
->cached
);
4781 /* To preserve the original search order, we went to visit the function
4782 infos in the reversed order of the list. However, making the list
4783 bi-directional use quite a bit of extra memory. So we reverse
4784 the list first, traverse the list in the now reversed order and
4785 finally reverse the list again to get back the original order. */
4786 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4787 for (each_func
= unit
->function_table
;
4789 each_func
= each_func
->prev_func
)
4791 /* Skip nameless functions. */
4792 if (each_func
->name
)
4793 /* There is no need to copy name string into hash table as
4794 name string is either in the dwarf string buffer or
4795 info in the stash. */
4796 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
4797 (void*) each_func
, false);
4799 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4803 /* We do the same for variable infos. */
4804 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4805 for (each_var
= unit
->variable_table
;
4807 each_var
= each_var
->prev_var
)
4809 /* Skip stack vars and vars with no files or names. */
4810 if (! each_var
->stack
4811 && each_var
->file
!= NULL
4812 && each_var
->name
!= NULL
)
4813 /* There is no need to copy name string into hash table as
4814 name string is either in the dwarf string buffer or
4815 info in the stash. */
4816 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
4817 (void*) each_var
, false);
4820 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4821 unit
->cached
= true;
4825 /* Locate a section in a BFD containing debugging info. The search starts
4826 from the section after AFTER_SEC, or from the first section in the BFD if
4827 AFTER_SEC is NULL. The search works by examining the names of the
4828 sections. There are three permissiable names. The first two are given
4829 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
4830 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
4831 This is a variation on the .debug_info section which has a checksum
4832 describing the contents appended onto the name. This allows the linker to
4833 identify and discard duplicate debugging sections for different
4834 compilation units. */
4835 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
4838 find_debug_info (bfd
*abfd
, const struct dwarf_debug_section
*debug_sections
,
4839 asection
*after_sec
)
4844 if (after_sec
== NULL
)
4846 look
= debug_sections
[debug_info
].uncompressed_name
;
4847 msec
= bfd_get_section_by_name (abfd
, look
);
4848 /* Testing SEC_HAS_CONTENTS is an anti-fuzzer measure. Of
4849 course debug sections always have contents. */
4850 if (msec
!= NULL
&& (msec
->flags
& SEC_HAS_CONTENTS
) != 0)
4853 look
= debug_sections
[debug_info
].compressed_name
;
4854 msec
= bfd_get_section_by_name (abfd
, look
);
4855 if (msec
!= NULL
&& (msec
->flags
& SEC_HAS_CONTENTS
) != 0)
4858 for (msec
= abfd
->sections
; msec
!= NULL
; msec
= msec
->next
)
4859 if ((msec
->flags
& SEC_HAS_CONTENTS
) != 0
4860 && startswith (msec
->name
, GNU_LINKONCE_INFO
))
4866 for (msec
= after_sec
->next
; msec
!= NULL
; msec
= msec
->next
)
4868 if ((msec
->flags
& SEC_HAS_CONTENTS
) == 0)
4871 look
= debug_sections
[debug_info
].uncompressed_name
;
4872 if (strcmp (msec
->name
, look
) == 0)
4875 look
= debug_sections
[debug_info
].compressed_name
;
4876 if (look
!= NULL
&& strcmp (msec
->name
, look
) == 0)
4879 if (startswith (msec
->name
, GNU_LINKONCE_INFO
))
4886 /* Transfer VMAs from object file to separate debug file. */
4889 set_debug_vma (bfd
*orig_bfd
, bfd
*debug_bfd
)
4893 for (s
= orig_bfd
->sections
, d
= debug_bfd
->sections
;
4894 s
!= NULL
&& d
!= NULL
;
4895 s
= s
->next
, d
= d
->next
)
4897 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4899 /* ??? Assumes 1-1 correspondence between sections in the
4901 if (strcmp (s
->name
, d
->name
) == 0)
4903 d
->output_section
= s
->output_section
;
4904 d
->output_offset
= s
->output_offset
;
4910 /* If the dwarf2 info was found in a separate debug file, return the
4911 debug file section corresponding to the section in the original file
4912 and the debug file symbols. */
4915 _bfd_dwarf2_stash_syms (struct dwarf2_debug
*stash
, bfd
*abfd
,
4916 asection
**sec
, asymbol
***syms
)
4918 if (stash
->f
.bfd_ptr
!= abfd
)
4924 *syms
= stash
->f
.syms
;
4928 for (s
= abfd
->sections
, d
= stash
->f
.bfd_ptr
->sections
;
4929 s
!= NULL
&& d
!= NULL
;
4930 s
= s
->next
, d
= d
->next
)
4932 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4935 && strcmp (s
->name
, d
->name
) == 0)
4938 *syms
= stash
->f
.syms
;
4945 /* Unset vmas for adjusted sections in STASH. */
4948 unset_sections (struct dwarf2_debug
*stash
)
4951 struct adjusted_section
*p
;
4953 i
= stash
->adjusted_section_count
;
4954 p
= stash
->adjusted_sections
;
4955 for (; i
> 0; i
--, p
++)
4956 p
->section
->vma
= p
->orig_vma
;
4959 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4960 relocatable object file. VMAs are normally all zero in relocatable
4961 object files, so if we want to distinguish locations in sections by
4962 address we need to set VMAs so the sections do not overlap. We
4963 also set VMA on .debug_info so that when we have multiple
4964 .debug_info sections (or the linkonce variant) they also do not
4965 overlap. The multiple .debug_info sections make up a single
4966 logical section. ??? We should probably do the same for other
4970 place_sections (bfd
*orig_bfd
, struct dwarf2_debug
*stash
)
4973 struct adjusted_section
*p
;
4975 const char *debug_info_name
;
4977 if (stash
->adjusted_section_count
!= 0)
4979 i
= stash
->adjusted_section_count
;
4980 p
= stash
->adjusted_sections
;
4981 for (; i
> 0; i
--, p
++)
4982 p
->section
->vma
= p
->adj_vma
;
4986 debug_info_name
= stash
->debug_sections
[debug_info
].uncompressed_name
;
4993 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4997 if (sect
->output_section
!= NULL
4998 && sect
->output_section
!= sect
4999 && (sect
->flags
& SEC_DEBUGGING
) == 0)
5002 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
5003 || startswith (sect
->name
, GNU_LINKONCE_INFO
));
5005 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
5011 if (abfd
== stash
->f
.bfd_ptr
)
5013 abfd
= stash
->f
.bfd_ptr
;
5017 stash
->adjusted_section_count
= -1;
5020 bfd_vma last_vma
= 0, last_dwarf
= 0;
5021 size_t amt
= i
* sizeof (struct adjusted_section
);
5023 p
= (struct adjusted_section
*) bfd_malloc (amt
);
5027 stash
->adjusted_sections
= p
;
5028 stash
->adjusted_section_count
= i
;
5035 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
5040 if (sect
->output_section
!= NULL
5041 && sect
->output_section
!= sect
5042 && (sect
->flags
& SEC_DEBUGGING
) == 0)
5045 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
5046 || startswith (sect
->name
, GNU_LINKONCE_INFO
));
5048 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
5052 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
5055 p
->orig_vma
= sect
->vma
;
5057 bfd_vma
*v
= is_debug_info
? &last_dwarf
: &last_vma
;
5058 /* Align the new address to the current section
5060 bfd_vma mask
= -(bfd_vma
) 1 << sect
->alignment_power
;
5061 *v
= (*v
+ ~mask
) & mask
;
5065 p
->adj_vma
= sect
->vma
;
5068 if (abfd
== stash
->f
.bfd_ptr
)
5070 abfd
= stash
->f
.bfd_ptr
;
5074 if (orig_bfd
!= stash
->f
.bfd_ptr
)
5075 set_debug_vma (orig_bfd
, stash
->f
.bfd_ptr
);
5080 /* Look up a funcinfo by name using the given info hash table. If found,
5081 also update the locations pointed to by filename_ptr and linenumber_ptr.
5083 This function returns TRUE if a funcinfo that matches the given symbol
5084 and address is found with any error; otherwise it returns FALSE. */
5087 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
5090 const char **filename_ptr
,
5091 unsigned int *linenumber_ptr
)
5093 struct funcinfo
* each_func
;
5094 struct funcinfo
* best_fit
= NULL
;
5095 bfd_vma best_fit_len
= (bfd_vma
) -1;
5096 struct info_list_node
*node
;
5097 struct arange
*arange
;
5098 const char *name
= bfd_asymbol_name (sym
);
5100 for (node
= lookup_info_hash_table (hash_table
, name
);
5104 each_func
= (struct funcinfo
*) node
->info
;
5105 for (arange
= &each_func
->arange
;
5107 arange
= arange
->next
)
5109 if (addr
>= arange
->low
5110 && addr
< arange
->high
5111 && arange
->high
- arange
->low
< best_fit_len
)
5113 best_fit
= each_func
;
5114 best_fit_len
= arange
->high
- arange
->low
;
5121 *filename_ptr
= best_fit
->file
;
5122 *linenumber_ptr
= best_fit
->line
;
5129 /* Look up a varinfo by name using the given info hash table. If found,
5130 also update the locations pointed to by filename_ptr and linenumber_ptr.
5132 This function returns TRUE if a varinfo that matches the given symbol
5133 and address is found with any error; otherwise it returns FALSE. */
5136 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
5139 const char **filename_ptr
,
5140 unsigned int *linenumber_ptr
)
5142 struct varinfo
* each
;
5143 struct info_list_node
*node
;
5144 const char *name
= bfd_asymbol_name (sym
);
5146 for (node
= lookup_info_hash_table (hash_table
, name
);
5150 each
= (struct varinfo
*) node
->info
;
5151 if (each
->addr
== addr
)
5153 *filename_ptr
= each
->file
;
5154 *linenumber_ptr
= each
->line
;
5162 /* Update the funcinfo and varinfo info hash tables if they are
5163 not up to date. Returns TRUE if there is no error; otherwise
5164 returns FALSE and disable the info hash tables. */
5167 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
5169 struct comp_unit
*each
;
5171 /* Exit if hash tables are up-to-date. */
5172 if (stash
->f
.all_comp_units
== stash
->hash_units_head
)
5175 if (stash
->hash_units_head
)
5176 each
= stash
->hash_units_head
->prev_unit
;
5178 each
= stash
->f
.last_comp_unit
;
5182 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
5183 stash
->varinfo_hash_table
))
5185 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
5188 each
= each
->prev_unit
;
5191 stash
->hash_units_head
= stash
->f
.all_comp_units
;
5195 /* Check consistency of info hash tables. This is for debugging only. */
5197 static void ATTRIBUTE_UNUSED
5198 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
5200 struct comp_unit
*each_unit
;
5201 struct funcinfo
*each_func
;
5202 struct varinfo
*each_var
;
5203 struct info_list_node
*node
;
5206 for (each_unit
= stash
->f
.all_comp_units
;
5208 each_unit
= each_unit
->next_unit
)
5210 for (each_func
= each_unit
->function_table
;
5212 each_func
= each_func
->prev_func
)
5214 if (!each_func
->name
)
5216 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
5220 while (node
&& !found
)
5222 found
= node
->info
== each_func
;
5228 for (each_var
= each_unit
->variable_table
;
5230 each_var
= each_var
->prev_var
)
5232 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
5234 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
5238 while (node
&& !found
)
5240 found
= node
->info
== each_var
;
5248 /* Check to see if we want to enable the info hash tables, which consume
5249 quite a bit of memory. Currently we only check the number times
5250 bfd_dwarf2_find_line is called. In the future, we may also want to
5251 take the number of symbols into account. */
5254 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
5256 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
5258 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
5261 /* FIXME: Maybe we should check the reduce_memory_overheads
5262 and optimize fields in the bfd_link_info structure ? */
5264 /* Create hash tables. */
5265 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
5266 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
5267 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
5269 /* Turn off info hashes if any allocation above fails. */
5270 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
5273 /* We need a forced update so that the info hash tables will
5274 be created even though there is no compilation unit. That
5275 happens if STASH_INFO_HASH_TRIGGER is 0. */
5276 if (stash_maybe_update_info_hash_tables (stash
))
5277 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
5280 /* Find the file and line associated with a symbol and address using the
5281 info hash tables of a stash. If there is a match, the function returns
5282 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
5283 otherwise it returns FALSE. */
5286 stash_find_line_fast (struct dwarf2_debug
*stash
,
5289 const char **filename_ptr
,
5290 unsigned int *linenumber_ptr
)
5292 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
5294 if (sym
->flags
& BSF_FUNCTION
)
5295 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
5296 filename_ptr
, linenumber_ptr
);
5297 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
5298 filename_ptr
, linenumber_ptr
);
5301 /* Save current section VMAs. */
5304 save_section_vma (const bfd
*abfd
, struct dwarf2_debug
*stash
)
5309 if (abfd
->section_count
== 0)
5311 stash
->sec_vma
= bfd_malloc (sizeof (*stash
->sec_vma
) * abfd
->section_count
);
5312 if (stash
->sec_vma
== NULL
)
5314 stash
->sec_vma_count
= abfd
->section_count
;
5315 for (i
= 0, s
= abfd
->sections
;
5316 s
!= NULL
&& i
< abfd
->section_count
;
5319 if (s
->output_section
!= NULL
)
5320 stash
->sec_vma
[i
] = s
->output_section
->vma
+ s
->output_offset
;
5322 stash
->sec_vma
[i
] = s
->vma
;
5327 /* Compare current section VMAs against those at the time the stash
5328 was created. If find_nearest_line is used in linker warnings or
5329 errors early in the link process, the debug info stash will be
5330 invalid for later calls. This is because we relocate debug info
5331 sections, so the stashed section contents depend on symbol values,
5332 which in turn depend on section VMAs. */
5335 section_vma_same (const bfd
*abfd
, const struct dwarf2_debug
*stash
)
5340 /* PR 24334: If the number of sections in ABFD has changed between
5341 when the stash was created and now, then we cannot trust the
5342 stashed vma information. */
5343 if (abfd
->section_count
!= stash
->sec_vma_count
)
5346 for (i
= 0, s
= abfd
->sections
;
5347 s
!= NULL
&& i
< abfd
->section_count
;
5352 if (s
->output_section
!= NULL
)
5353 vma
= s
->output_section
->vma
+ s
->output_offset
;
5356 if (vma
!= stash
->sec_vma
[i
])
5362 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
5363 If DEBUG_BFD is not specified, we read debug information from ABFD
5364 or its gnu_debuglink. The results will be stored in PINFO.
5365 The function returns TRUE iff debug information is ready. */
5368 _bfd_dwarf2_slurp_debug_info (bfd
*abfd
, bfd
*debug_bfd
,
5369 const struct dwarf_debug_section
*debug_sections
,
5374 bfd_size_type total_size
;
5376 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
5380 if (stash
->orig_bfd
== abfd
5381 && section_vma_same (abfd
, stash
))
5383 /* Check that we did previously find some debug information
5384 before attempting to make use of it. */
5385 if (stash
->f
.dwarf_info_size
!= 0)
5387 if (do_place
&& !place_sections (abfd
, stash
))
5394 _bfd_dwarf2_cleanup_debug_info (abfd
, pinfo
);
5395 memset (stash
, 0, sizeof (*stash
));
5399 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, sizeof (*stash
));
5404 stash
->orig_bfd
= abfd
;
5405 stash
->debug_sections
= debug_sections
;
5406 stash
->f
.syms
= symbols
;
5407 if (!save_section_vma (abfd
, stash
))
5410 stash
->f
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
5411 del_abbrev
, calloc
, free
);
5412 if (!stash
->f
.abbrev_offsets
)
5415 stash
->alt
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
5416 del_abbrev
, calloc
, free
);
5417 if (!stash
->alt
.abbrev_offsets
)
5420 stash
->f
.trie_root
= alloc_trie_leaf (abfd
);
5421 if (!stash
->f
.trie_root
)
5424 stash
->alt
.trie_root
= alloc_trie_leaf (abfd
);
5425 if (!stash
->alt
.trie_root
)
5428 if (debug_bfd
== NULL
)
5431 msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
5432 if (msec
== NULL
&& abfd
== debug_bfd
)
5434 char * debug_filename
;
5436 debug_filename
= bfd_follow_build_id_debuglink (abfd
, DEBUGDIR
);
5437 if (debug_filename
== NULL
)
5438 debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
5440 if (debug_filename
== NULL
)
5441 /* No dwarf2 info, and no gnu_debuglink to follow.
5442 Note that at this point the stash has been allocated, but
5443 contains zeros. This lets future calls to this function
5444 fail more quickly. */
5447 debug_bfd
= bfd_openr (debug_filename
, NULL
);
5448 free (debug_filename
);
5449 if (debug_bfd
== NULL
)
5450 /* FIXME: Should we report our failure to follow the debuglink ? */
5453 /* Set BFD_DECOMPRESS to decompress debug sections. */
5454 debug_bfd
->flags
|= BFD_DECOMPRESS
;
5455 if (!bfd_check_format (debug_bfd
, bfd_object
)
5456 || (msec
= find_debug_info (debug_bfd
,
5457 debug_sections
, NULL
)) == NULL
5458 || !bfd_generic_link_read_symbols (debug_bfd
))
5460 bfd_close (debug_bfd
);
5464 symbols
= bfd_get_outsymbols (debug_bfd
);
5465 stash
->f
.syms
= symbols
;
5466 stash
->close_on_cleanup
= true;
5468 stash
->f
.bfd_ptr
= debug_bfd
;
5471 && !place_sections (abfd
, stash
))
5474 /* There can be more than one DWARF2 info section in a BFD these
5475 days. First handle the easy case when there's only one. If
5476 there's more than one, try case two: read them all in and produce
5477 one large stash. We do this in two passes - in the first pass we
5478 just accumulate the section sizes, and in the second pass we
5479 read in the section's contents. (The allows us to avoid
5480 reallocing the data as we add sections to the stash.) */
5482 if (! find_debug_info (debug_bfd
, debug_sections
, msec
))
5484 /* Case 1: only one info section. */
5485 total_size
= msec
->size
;
5486 if (! read_section (debug_bfd
, &stash
->debug_sections
[debug_info
],
5488 &stash
->f
.dwarf_info_buffer
, &total_size
))
5493 /* Case 2: multiple sections. */
5494 for (total_size
= 0;
5496 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
5498 if (_bfd_section_size_insane (debug_bfd
, msec
))
5500 /* Catch PR25070 testcase overflowing size calculation here. */
5501 if (total_size
+ msec
->size
< total_size
)
5503 bfd_set_error (bfd_error_no_memory
);
5506 total_size
+= msec
->size
;
5509 stash
->f
.dwarf_info_buffer
= (bfd_byte
*) bfd_malloc (total_size
);
5510 if (stash
->f
.dwarf_info_buffer
== NULL
)
5514 for (msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
5516 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
5524 if (!(bfd_simple_get_relocated_section_contents
5525 (debug_bfd
, msec
, stash
->f
.dwarf_info_buffer
+ total_size
,
5533 stash
->f
.info_ptr
= stash
->f
.dwarf_info_buffer
;
5534 stash
->f
.dwarf_info_size
= total_size
;
5538 unset_sections (stash
);
5542 /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
5544 static struct comp_unit
*
5545 stash_comp_unit (struct dwarf2_debug
*stash
, struct dwarf2_debug_file
*file
)
5547 bfd_size_type length
;
5548 unsigned int offset_size
;
5549 bfd_byte
*info_ptr_unit
= file
->info_ptr
;
5550 bfd_byte
*info_ptr_end
= file
->dwarf_info_buffer
+ file
->dwarf_info_size
;
5552 if (file
->info_ptr
>= info_ptr_end
)
5555 length
= read_4_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
5556 /* A 0xffffff length is the DWARF3 way of indicating
5557 we use 64-bit offsets, instead of 32-bit offsets. */
5558 if (length
== 0xffffffff)
5561 length
= read_8_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
5563 /* A zero length is the IRIX way of indicating 64-bit offsets,
5564 mostly because the 64-bit length will generally fit in 32
5565 bits, and the endianness helps. */
5566 else if (length
== 0)
5569 length
= read_4_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
5571 /* In the absence of the hints above, we assume 32-bit DWARF2
5572 offsets even for targets with 64-bit addresses, because:
5573 a) most of the time these targets will not have generated
5574 more than 2Gb of debug info and so will not need 64-bit
5577 b) if they do use 64-bit offsets but they are not using
5578 the size hints that are tested for above then they are
5579 not conforming to the DWARF3 standard anyway. */
5584 && length
<= (size_t) (info_ptr_end
- file
->info_ptr
))
5586 struct comp_unit
*each
= parse_comp_unit (stash
, file
,
5587 file
->info_ptr
, length
,
5588 info_ptr_unit
, offset_size
);
5591 if (file
->comp_unit_tree
== NULL
)
5592 file
->comp_unit_tree
5593 = splay_tree_new (splay_tree_compare_addr_range
,
5594 splay_tree_free_addr_range
, NULL
);
5596 struct addr_range
*r
5597 = (struct addr_range
*)bfd_malloc (sizeof (struct addr_range
));
5598 r
->start
= each
->info_ptr_unit
;
5599 r
->end
= each
->end_ptr
;
5600 splay_tree_node v
= splay_tree_lookup (file
->comp_unit_tree
,
5602 if (v
!= NULL
|| r
->end
<= r
->start
)
5604 splay_tree_insert (file
->comp_unit_tree
, (splay_tree_key
)r
,
5605 (splay_tree_value
)each
);
5607 if (file
->all_comp_units
)
5608 file
->all_comp_units
->prev_unit
= each
;
5610 file
->last_comp_unit
= each
;
5612 each
->next_unit
= file
->all_comp_units
;
5613 file
->all_comp_units
= each
;
5615 if (each
->arange
.high
== 0)
5617 each
->next_unit_without_ranges
= file
->all_comp_units_without_ranges
;
5618 file
->all_comp_units_without_ranges
= each
->next_unit_without_ranges
;
5621 file
->info_ptr
+= length
;
5626 /* Don't trust any of the DWARF info after a corrupted length or
5628 file
->info_ptr
= info_ptr_end
;
5632 /* Hash function for an asymbol. */
5635 hash_asymbol (const void *sym
)
5637 const asymbol
*asym
= sym
;
5638 return htab_hash_string (asym
->name
);
5641 /* Equality function for asymbols. */
5644 eq_asymbol (const void *a
, const void *b
)
5646 const asymbol
*sa
= a
;
5647 const asymbol
*sb
= b
;
5648 return strcmp (sa
->name
, sb
->name
) == 0;
5651 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
5652 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
5653 symbol in SYMBOLS and return the difference between the low_pc and
5654 the symbol's address. Returns 0 if no suitable symbol could be found. */
5657 _bfd_dwarf2_find_symbol_bias (asymbol
** symbols
, void ** pinfo
)
5659 struct dwarf2_debug
*stash
;
5660 struct comp_unit
* unit
;
5662 bfd_signed_vma result
= 0;
5665 stash
= (struct dwarf2_debug
*) *pinfo
;
5667 if (stash
== NULL
|| symbols
== NULL
)
5670 sym_hash
= htab_create_alloc (10, hash_asymbol
, eq_asymbol
,
5671 NULL
, xcalloc
, free
);
5672 for (psym
= symbols
; * psym
!= NULL
; psym
++)
5674 asymbol
* sym
= * psym
;
5676 if (sym
->flags
& BSF_FUNCTION
&& sym
->section
!= NULL
)
5678 void **slot
= htab_find_slot (sym_hash
, sym
, INSERT
);
5683 for (unit
= stash
->f
.all_comp_units
; unit
; unit
= unit
->next_unit
)
5685 struct funcinfo
* func
;
5687 comp_unit_maybe_decode_line_info (unit
);
5689 for (func
= unit
->function_table
; func
!= NULL
; func
= func
->prev_func
)
5690 if (func
->name
&& func
->arange
.low
)
5692 asymbol search
, *sym
;
5694 /* FIXME: Do we need to scan the aranges looking for the
5697 search
.name
= func
->name
;
5698 sym
= htab_find (sym_hash
, &search
);
5701 result
= func
->arange
.low
- (sym
->value
+ sym
->section
->vma
);
5708 htab_delete (sym_hash
);
5712 /* See _bfd_dwarf2_find_nearest_line_with_alt. */
5715 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
5720 const char **filename_ptr
,
5721 const char **functionname_ptr
,
5722 unsigned int *linenumber_ptr
,
5723 unsigned int *discriminator_ptr
,
5724 const struct dwarf_debug_section
*debug_sections
,
5727 return _bfd_dwarf2_find_nearest_line_with_alt
5728 (abfd
, NULL
, symbols
, symbol
, section
, offset
, filename_ptr
,
5729 functionname_ptr
, linenumber_ptr
, discriminator_ptr
, debug_sections
,
5733 /* Find the source code location of SYMBOL. If SYMBOL is NULL
5734 then find the nearest source code location corresponding to
5735 the address SECTION + OFFSET.
5736 Returns 1 if the line is found without error and fills in
5737 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
5738 NULL the FUNCTIONNAME_PTR is also filled in.
5739 Returns 2 if partial information from _bfd_elf_find_function is
5740 returned (function and maybe file) by looking at symbols. DWARF2
5741 info is present but not regarding the requested code location.
5742 Returns 0 otherwise.
5743 SYMBOLS contains the symbol table for ABFD.
5744 DEBUG_SECTIONS contains the name of the dwarf debug sections.
5745 If ALT_FILENAME is given, attempt to open the file and use it
5746 as the .gnu_debugaltlink file. Otherwise this file will be
5747 searched for when needed. */
5750 _bfd_dwarf2_find_nearest_line_with_alt
5752 const char *alt_filename
,
5757 const char **filename_ptr
,
5758 const char **functionname_ptr
,
5759 unsigned int *linenumber_ptr
,
5760 unsigned int *discriminator_ptr
,
5761 const struct dwarf_debug_section
*debug_sections
,
5764 /* Read each compilation unit from the section .debug_info, and check
5765 to see if it contains the address we are searching for. If yes,
5766 lookup the address, and return the line number info. If no, go
5767 on to the next compilation unit.
5769 We keep a list of all the previously read compilation units, and
5770 a pointer to the next un-read compilation unit. Check the
5771 previously read units before reading more. */
5772 struct dwarf2_debug
*stash
;
5773 /* What address are we looking for? */
5775 struct comp_unit
* each
;
5776 struct funcinfo
*function
= NULL
;
5780 *filename_ptr
= NULL
;
5781 if (functionname_ptr
!= NULL
)
5782 *functionname_ptr
= NULL
;
5783 *linenumber_ptr
= 0;
5784 if (discriminator_ptr
)
5785 *discriminator_ptr
= 0;
5787 if (! _bfd_dwarf2_slurp_debug_info (abfd
, NULL
, debug_sections
,
5789 (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0))
5792 stash
= (struct dwarf2_debug
*) *pinfo
;
5794 if (stash
->alt
.bfd_ptr
== NULL
&& alt_filename
!= NULL
)
5796 bfd
*alt_bfd
= bfd_openr (alt_filename
, NULL
);
5798 if (alt_bfd
== NULL
)
5799 /* bfd_openr will have set the bfd_error. */
5801 if (!bfd_check_format (alt_bfd
, bfd_object
))
5803 bfd_set_error (bfd_error_wrong_format
);
5804 bfd_close (alt_bfd
);
5808 stash
->alt
.bfd_ptr
= alt_bfd
;
5811 do_line
= symbol
!= NULL
;
5814 BFD_ASSERT (section
== NULL
&& offset
== 0 && functionname_ptr
== NULL
);
5815 section
= bfd_asymbol_section (symbol
);
5816 addr
= symbol
->value
;
5820 BFD_ASSERT (section
!= NULL
&& functionname_ptr
!= NULL
);
5823 /* If we have no SYMBOL but the section we're looking at is not a
5824 code section, then take a look through the list of symbols to see
5825 if we have a symbol at the address we're looking for. If we do
5826 then use this to look up line information. This will allow us to
5827 give file and line results for data symbols. We exclude code
5828 symbols here, if we look up a function symbol and then look up the
5829 line information we'll actually return the line number for the
5830 opening '{' rather than the function definition line. This is
5831 because looking up by symbol uses the line table, in which the
5832 first line for a function is usually the opening '{', while
5833 looking up the function by section + offset uses the
5834 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
5835 which will be the line of the function name. */
5836 if (symbols
!= NULL
&& (section
->flags
& SEC_CODE
) == 0)
5840 for (tmp
= symbols
; (*tmp
) != NULL
; ++tmp
)
5841 if ((*tmp
)->the_bfd
== abfd
5842 && (*tmp
)->section
== section
5843 && (*tmp
)->value
== offset
5844 && ((*tmp
)->flags
& BSF_SECTION_SYM
) == 0)
5848 /* For local symbols, keep going in the hope we find a
5850 if ((symbol
->flags
& BSF_GLOBAL
) != 0)
5856 if (section
->output_section
)
5857 addr
+= section
->output_section
->vma
+ section
->output_offset
;
5859 addr
+= section
->vma
;
5861 /* A null info_ptr indicates that there is no dwarf2 info
5862 (or that an error occured while setting up the stash). */
5863 if (! stash
->f
.info_ptr
)
5866 stash
->inliner_chain
= NULL
;
5868 /* Check the previously read comp. units first. */
5871 /* The info hash tables use quite a bit of memory. We may not want to
5872 always use them. We use some heuristics to decide if and when to
5874 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
5875 stash_maybe_enable_info_hash_tables (abfd
, stash
);
5877 /* Keep info hash table up to date if they are available. Note that we
5878 may disable the hash tables if there is any error duing update. */
5879 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
5880 stash_maybe_update_info_hash_tables (stash
);
5882 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
5884 found
= stash_find_line_fast (stash
, symbol
, addr
,
5885 filename_ptr
, linenumber_ptr
);
5890 /* Check the previously read comp. units first. */
5891 for (each
= stash
->f
.all_comp_units
; each
; each
= each
->next_unit
)
5892 if ((symbol
->flags
& BSF_FUNCTION
) == 0
5893 || each
->arange
.high
== 0
5894 || comp_unit_contains_address (each
, addr
))
5896 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
5904 struct trie_node
*trie
= stash
->f
.trie_root
;
5905 unsigned int bits
= VMA_BITS
- 8;
5906 struct comp_unit
**prev_each
;
5908 /* Traverse interior nodes until we get to a leaf. */
5909 while (trie
&& trie
->num_room_in_leaf
== 0)
5911 int ch
= (addr
>> bits
) & 0xff;
5912 trie
= ((struct trie_interior
*) trie
)->children
[ch
];
5918 const struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
5921 for (i
= 0; i
< leaf
->num_stored_in_leaf
; ++i
)
5922 leaf
->ranges
[i
].unit
->mark
= false;
5924 for (i
= 0; i
< leaf
->num_stored_in_leaf
; ++i
)
5926 struct comp_unit
*unit
= leaf
->ranges
[i
].unit
;
5928 || addr
< leaf
->ranges
[i
].low_pc
5929 || addr
>= leaf
->ranges
[i
].high_pc
)
5933 found
= comp_unit_find_nearest_line (unit
, addr
,
5943 /* Also scan through all compilation units without any ranges,
5944 taking them out of the list if they have acquired any since
5946 prev_each
= &stash
->f
.all_comp_units_without_ranges
;
5947 for (each
= *prev_each
; each
; each
= each
->next_unit_without_ranges
)
5949 if (each
->arange
.high
!= 0)
5951 *prev_each
= each
->next_unit_without_ranges
;
5955 found
= comp_unit_find_nearest_line (each
, addr
,
5962 prev_each
= &each
->next_unit_without_ranges
;
5966 /* Read each remaining comp. units checking each as they are read. */
5967 while ((each
= stash_comp_unit (stash
, &stash
->f
)) != NULL
)
5969 /* DW_AT_low_pc and DW_AT_high_pc are optional for
5970 compilation units. If we don't have them (i.e.,
5971 unit->high == 0), we need to consult the line info table
5972 to see if a compilation unit contains the given
5975 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
5976 || each
->arange
.high
== 0
5977 || comp_unit_contains_address (each
, addr
))
5978 && comp_unit_find_line (each
, symbol
, addr
,
5979 filename_ptr
, linenumber_ptr
));
5981 found
= ((each
->arange
.high
== 0
5982 || comp_unit_contains_address (each
, addr
))
5983 && comp_unit_find_nearest_line (each
, addr
,
5987 discriminator_ptr
));
5994 if (functionname_ptr
&& function
&& function
->is_linkage
)
5996 *functionname_ptr
= function
->name
;
6000 else if (functionname_ptr
6001 && (!*functionname_ptr
6002 || (function
&& !function
->is_linkage
)))
6005 asymbol
**syms
= symbols
;
6006 asection
*sec
= section
;
6008 _bfd_dwarf2_stash_syms (stash
, abfd
, &sec
, &syms
);
6009 fun
= _bfd_elf_find_function (abfd
, syms
, sec
, offset
,
6010 *filename_ptr
? NULL
: filename_ptr
,
6013 if (!found
&& fun
!= NULL
)
6016 if (function
&& !function
->is_linkage
)
6020 sec_vma
= section
->vma
;
6021 if (section
->output_section
!= NULL
)
6022 sec_vma
= section
->output_section
->vma
+ section
->output_offset
;
6024 *functionname_ptr
= function
->name
;
6025 else if (fun
->value
+ sec_vma
== function
->arange
.low
)
6026 function
->name
= *functionname_ptr
;
6027 /* Even if we didn't find a linkage name, say that we have
6028 to stop a repeated search of symbols. */
6029 function
->is_linkage
= true;
6033 unset_sections (stash
);
6039 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
6040 const char **filename_ptr
,
6041 const char **functionname_ptr
,
6042 unsigned int *linenumber_ptr
,
6045 struct dwarf2_debug
*stash
;
6047 stash
= (struct dwarf2_debug
*) *pinfo
;
6050 struct funcinfo
*func
= stash
->inliner_chain
;
6052 if (func
&& func
->caller_func
)
6054 *filename_ptr
= func
->caller_file
;
6055 *functionname_ptr
= func
->caller_func
->name
;
6056 *linenumber_ptr
= func
->caller_line
;
6057 stash
->inliner_chain
= func
->caller_func
;
6066 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
, void **pinfo
)
6068 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
6069 struct comp_unit
*each
;
6070 struct dwarf2_debug_file
*file
;
6072 if (abfd
== NULL
|| stash
== NULL
)
6075 if (stash
->varinfo_hash_table
)
6076 bfd_hash_table_free (&stash
->varinfo_hash_table
->base
);
6077 if (stash
->funcinfo_hash_table
)
6078 bfd_hash_table_free (&stash
->funcinfo_hash_table
->base
);
6083 for (each
= file
->all_comp_units
; each
; each
= each
->next_unit
)
6085 struct funcinfo
*function_table
= each
->function_table
;
6086 struct varinfo
*variable_table
= each
->variable_table
;
6088 if (each
->line_table
&& each
->line_table
!= file
->line_table
)
6090 free (each
->line_table
->files
);
6091 free (each
->line_table
->dirs
);
6094 free (each
->lookup_funcinfo_table
);
6095 each
->lookup_funcinfo_table
= NULL
;
6097 while (function_table
)
6099 free (function_table
->file
);
6100 function_table
->file
= NULL
;
6101 free (function_table
->caller_file
);
6102 function_table
->caller_file
= NULL
;
6103 function_table
= function_table
->prev_func
;
6106 while (variable_table
)
6108 free (variable_table
->file
);
6109 variable_table
->file
= NULL
;
6110 variable_table
= variable_table
->prev_var
;
6114 if (file
->line_table
)
6116 free (file
->line_table
->files
);
6117 free (file
->line_table
->dirs
);
6119 htab_delete (file
->abbrev_offsets
);
6120 if (file
->comp_unit_tree
!= NULL
)
6121 splay_tree_delete (file
->comp_unit_tree
);
6123 free (file
->dwarf_line_str_buffer
);
6124 free (file
->dwarf_str_buffer
);
6125 free (file
->dwarf_ranges_buffer
);
6126 free (file
->dwarf_line_buffer
);
6127 free (file
->dwarf_abbrev_buffer
);
6128 free (file
->dwarf_info_buffer
);
6129 if (file
== &stash
->alt
)
6133 free (stash
->sec_vma
);
6134 free (stash
->adjusted_sections
);
6135 if (stash
->close_on_cleanup
)
6136 bfd_close (stash
->f
.bfd_ptr
);
6137 if (stash
->alt
.bfd_ptr
)
6138 bfd_close (stash
->alt
.bfd_ptr
);
6141 /* Find the function to a particular section and offset,
6142 for error reporting. */
6145 _bfd_elf_find_function (bfd
*abfd
,
6149 const char **filename_ptr
,
6150 const char **functionname_ptr
)
6152 struct elf_find_function_cache
6154 asection
*last_section
;
6156 const char *filename
;
6157 bfd_size_type func_size
;
6160 if (symbols
== NULL
)
6163 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6166 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
6169 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
6170 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
6174 if (cache
->last_section
!= section
6175 || cache
->func
== NULL
6176 || offset
< cache
->func
->value
6177 || offset
>= cache
->func
->value
+ cache
->func_size
)
6182 /* ??? Given multiple file symbols, it is impossible to reliably
6183 choose the right file name for global symbols. File symbols are
6184 local symbols, and thus all file symbols must sort before any
6185 global symbols. The ELF spec may be interpreted to say that a
6186 file symbol must sort before other local symbols, but currently
6187 ld -r doesn't do this. So, for ld -r output, it is possible to
6188 make a better choice of file name for local symbols by ignoring
6189 file symbols appearing after a given local symbol. */
6190 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
6191 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6195 state
= nothing_seen
;
6196 cache
->filename
= NULL
;
6198 cache
->func_size
= 0;
6199 cache
->last_section
= section
;
6201 for (p
= symbols
; *p
!= NULL
; p
++)
6207 if ((sym
->flags
& BSF_FILE
) != 0)
6210 if (state
== symbol_seen
)
6211 state
= file_after_symbol_seen
;
6215 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
6217 && code_off
<= offset
6218 && (code_off
> low_func
6219 || (code_off
== low_func
6220 && size
> cache
->func_size
)))
6223 cache
->func_size
= size
;
6224 cache
->filename
= NULL
;
6225 low_func
= code_off
;
6227 && ((sym
->flags
& BSF_LOCAL
) != 0
6228 || state
!= file_after_symbol_seen
))
6229 cache
->filename
= bfd_asymbol_name (file
);
6231 if (state
== nothing_seen
)
6232 state
= symbol_seen
;
6236 if (cache
->func
== NULL
)
6240 *filename_ptr
= cache
->filename
;
6241 if (functionname_ptr
)
6242 *functionname_ptr
= bfd_asymbol_name (cache
->func
);