2 Copyright (C) 1994-2022 Free Software Foundation, Inc.
4 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
7 From the dwarf2read.c header:
8 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
9 Inc. with support from Florida State University (under contract
10 with the Ada Joint Program Office), and Silicon Graphics, Inc.
11 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
12 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
13 support in dwarfread.c
15 This file is part of BFD.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 3 of the License, or (at
20 your option) any later version.
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
30 MA 02110-1301, USA. */
34 #include "libiberty.h"
40 /* The data in the .debug_line statement prologue looks like this. */
45 unsigned short version
;
46 bfd_vma prologue_length
;
47 unsigned char minimum_instruction_length
;
48 unsigned char maximum_ops_per_insn
;
49 unsigned char default_is_stmt
;
51 unsigned char line_range
;
52 unsigned char opcode_base
;
53 unsigned char *standard_opcode_lengths
;
56 /* Attributes have a name and a value. */
60 enum dwarf_attribute name
;
65 struct dwarf_block
*blk
;
72 /* Blocks are a bunch of untyped bytes. */
79 struct adjusted_section
85 /* A trie to map quickly from address range to compilation unit.
87 This is a fairly standard radix-256 trie, used to quickly locate which
88 compilation unit any given address belongs to. Given that each compilation
89 unit may register hundreds of very small and unaligned ranges (which may
90 potentially overlap, due to inlining and other concerns), and a large
91 program may end up containing hundreds of thousands of such ranges, we cannot
92 scan through them linearly without undue slowdown.
94 We use a hybrid trie to avoid memory explosion: There are two types of trie
95 nodes, leaves and interior nodes. (Almost all nodes are leaves, so they
96 take up the bulk of the memory usage.) Leaves contain a simple array of
97 ranges (high/low address) and which compilation unit contains those ranges,
98 and when we get to a leaf, we scan through it linearly. Interior nodes
99 contain pointers to 256 other nodes, keyed by the next byte of the address.
100 So for a 64-bit address like 0x1234567abcd, we would start at the root and go
101 down child[0x00]->child[0x00]->child[0x01]->child[0x23]->child[0x45] etc.,
102 until we hit a leaf. (Nodes are, in general, leaves until they exceed the
103 default allocation of 16 elements, at which point they are converted to
104 interior node if possible.) This gives us near-constant lookup times;
105 the only thing that can be costly is if there are lots of overlapping ranges
106 within a single 256-byte segment of the binary, in which case we have to
107 scan through them all to find the best match.
109 For a binary with few ranges, we will in practice only have a single leaf
110 node at the root, containing a simple array. Thus, the scheme is efficient
111 for both small and large binaries.
114 /* Experiments have shown 16 to be a memory-efficient default leaf size.
115 The only case where a leaf will hold more memory than this, is at the
116 bottomost level (covering 256 bytes in the binary), where we'll expand
117 the leaf to be able to hold more ranges if needed.
119 #define TRIE_LEAF_SIZE 16
121 /* All trie_node pointers will really be trie_leaf or trie_interior,
122 but they have this common head. */
125 /* If zero, we are an interior node.
126 Otherwise, how many ranges we have room for in this leaf. */
127 unsigned int num_room_in_leaf
;
132 struct trie_node head
;
133 unsigned int num_stored_in_leaf
;
135 struct comp_unit
*unit
;
136 bfd_vma low_pc
, high_pc
;
137 } ranges
[TRIE_LEAF_SIZE
];
142 struct trie_node head
;
143 struct trie_node
*children
[256];
146 static struct trie_node
*alloc_trie_leaf (bfd
*abfd
)
148 struct trie_leaf
*leaf
= bfd_zalloc (abfd
, sizeof (struct trie_leaf
));
151 leaf
->head
.num_room_in_leaf
= TRIE_LEAF_SIZE
;
155 struct dwarf2_debug_file
157 /* The actual bfd from which debug info was loaded. Might be
158 different to orig_bfd because of gnu_debuglink sections. */
161 /* Pointer to the symbol table. */
164 /* The current info pointer for the .debug_info section being parsed. */
167 /* A pointer to the memory block allocated for .debug_info sections. */
168 bfd_byte
*dwarf_info_buffer
;
170 /* Length of the loaded .debug_info sections. */
171 bfd_size_type dwarf_info_size
;
173 /* Pointer to the .debug_abbrev section loaded into memory. */
174 bfd_byte
*dwarf_abbrev_buffer
;
176 /* Length of the loaded .debug_abbrev section. */
177 bfd_size_type dwarf_abbrev_size
;
179 /* Buffer for decode_line_info. */
180 bfd_byte
*dwarf_line_buffer
;
182 /* Length of the loaded .debug_line section. */
183 bfd_size_type dwarf_line_size
;
185 /* Pointer to the .debug_str section loaded into memory. */
186 bfd_byte
*dwarf_str_buffer
;
188 /* Length of the loaded .debug_str section. */
189 bfd_size_type dwarf_str_size
;
191 /* Pointer to the .debug_str_offsets section loaded into memory. */
192 bfd_byte
*dwarf_str_offsets_buffer
;
194 /* Length of the loaded .debug_str_offsets section. */
195 bfd_size_type dwarf_str_offsets_size
;
197 /* Pointer to the .debug_addr section loaded into memory. */
198 bfd_byte
*dwarf_addr_buffer
;
200 /* Length of the loaded .debug_addr section. */
201 bfd_size_type dwarf_addr_size
;
203 /* Pointer to the .debug_line_str section loaded into memory. */
204 bfd_byte
*dwarf_line_str_buffer
;
206 /* Length of the loaded .debug_line_str section. */
207 bfd_size_type dwarf_line_str_size
;
209 /* Pointer to the .debug_ranges section loaded into memory. */
210 bfd_byte
*dwarf_ranges_buffer
;
212 /* Length of the loaded .debug_ranges section. */
213 bfd_size_type dwarf_ranges_size
;
215 /* Pointer to the .debug_rnglists section loaded into memory. */
216 bfd_byte
*dwarf_rnglists_buffer
;
218 /* Length of the loaded .debug_rnglists section. */
219 bfd_size_type dwarf_rnglists_size
;
221 /* A list of all previously read comp_units. */
222 struct comp_unit
*all_comp_units
;
224 /* A list of all previously read comp_units with no ranges (yet). */
225 struct comp_unit
*all_comp_units_without_ranges
;
227 /* Last comp unit in list above. */
228 struct comp_unit
*last_comp_unit
;
230 /* Line table at line_offset zero. */
231 struct line_info_table
*line_table
;
233 /* Hash table to map offsets to decoded abbrevs. */
234 htab_t abbrev_offsets
;
236 /* Root of a trie to map addresses to compilation units. */
237 struct trie_node
*trie_root
;
242 /* Names of the debug sections. */
243 const struct dwarf_debug_section
*debug_sections
;
245 /* Per-file stuff. */
246 struct dwarf2_debug_file f
, alt
;
248 /* Pointer to the original bfd for which debug was loaded. This is what
249 we use to compare and so check that the cached debug data is still
250 valid - it saves having to possibly dereference the gnu_debuglink each
254 /* If the most recent call to bfd_find_nearest_line was given an
255 address in an inlined function, preserve a pointer into the
256 calling chain for subsequent calls to bfd_find_inliner_info to
258 struct funcinfo
*inliner_chain
;
260 /* Section VMAs at the time the stash was built. */
262 /* Number of sections in the SEC_VMA table. */
263 unsigned int sec_vma_count
;
265 /* Number of sections whose VMA we must adjust. */
266 int adjusted_section_count
;
268 /* Array of sections with adjusted VMA. */
269 struct adjusted_section
*adjusted_sections
;
271 /* Number of times find_line is called. This is used in
272 the heuristic for enabling the info hash tables. */
275 #define STASH_INFO_HASH_TRIGGER 100
277 /* Hash table mapping symbol names to function infos. */
278 struct info_hash_table
*funcinfo_hash_table
;
280 /* Hash table mapping symbol names to variable infos. */
281 struct info_hash_table
*varinfo_hash_table
;
283 /* Head of comp_unit list in the last hash table update. */
284 struct comp_unit
*hash_units_head
;
286 /* Status of info hash. */
287 int info_hash_status
;
288 #define STASH_INFO_HASH_OFF 0
289 #define STASH_INFO_HASH_ON 1
290 #define STASH_INFO_HASH_DISABLED 2
292 /* True if we opened bfd_ptr. */
293 bool close_on_cleanup
;
303 /* A minimal decoding of DWARF2 compilation units. We only decode
304 what's needed to get to the line number information. */
308 /* Chain the previously read compilation units. */
309 struct comp_unit
*next_unit
;
311 /* Chain the previously read compilation units that have no ranges yet.
312 We scan these separately when we have a trie over the ranges.
313 Unused if arange.high != 0. */
314 struct comp_unit
*next_unit_without_ranges
;
316 /* Likewise, chain the compilation unit read after this one.
317 The comp units are stored in reversed reading order. */
318 struct comp_unit
*prev_unit
;
320 /* Keep the bfd convenient (for memory allocation). */
323 /* The lowest and highest addresses contained in this compilation
324 unit as specified in the compilation unit header. */
325 struct arange arange
;
327 /* The DW_AT_name attribute (for error messages). */
330 /* The abbrev hash table. */
331 struct abbrev_info
**abbrevs
;
333 /* DW_AT_language. */
336 /* Note that an error was found by comp_unit_find_nearest_line. */
339 /* The DW_AT_comp_dir attribute. */
342 /* TRUE if there is a line number table associated with this comp. unit. */
345 /* Pointer to the current comp_unit so that we can find a given entry
347 bfd_byte
*info_ptr_unit
;
349 /* The offset into .debug_line of the line number table. */
350 unsigned long line_offset
;
352 /* Pointer to the first child die for the comp unit. */
353 bfd_byte
*first_child_die_ptr
;
355 /* The end of the comp unit. */
358 /* The decoded line number, NULL if not yet decoded. */
359 struct line_info_table
*line_table
;
361 /* A list of the functions found in this comp. unit. */
362 struct funcinfo
*function_table
;
364 /* A table of function information references searchable by address. */
365 struct lookup_funcinfo
*lookup_funcinfo_table
;
367 /* Number of functions in the function_table and sorted_function_table. */
368 bfd_size_type number_of_functions
;
370 /* A list of the variables found in this comp. unit. */
371 struct varinfo
*variable_table
;
373 /* Pointers to dwarf2_debug structures. */
374 struct dwarf2_debug
*stash
;
375 struct dwarf2_debug_file
*file
;
377 /* DWARF format version for this unit - from unit header. */
380 /* Address size for this unit - from unit header. */
381 unsigned char addr_size
;
383 /* Offset size for this unit - from unit header. */
384 unsigned char offset_size
;
386 /* Base address for this unit - from DW_AT_low_pc attribute of
387 DW_TAG_compile_unit DIE */
388 bfd_vma base_address
;
390 /* TRUE if symbols are cached in hash table for faster lookup by name. */
393 /* Used when iterating over trie leaves to know which units we have
394 already seen in this iteration. */
397 /* Base address of debug_addr section. */
398 size_t dwarf_addr_offset
;
400 /* Base address of string offset table. */
401 size_t dwarf_str_offset
;
404 /* This data structure holds the information of an abbrev. */
407 unsigned int number
; /* Number identifying abbrev. */
408 enum dwarf_tag tag
; /* DWARF tag. */
409 bool has_children
; /* TRUE if the abbrev has children. */
410 unsigned int num_attrs
; /* Number of attributes. */
411 struct attr_abbrev
* attrs
; /* An array of attribute descriptions. */
412 struct abbrev_info
* next
; /* Next in chain. */
417 enum dwarf_attribute name
;
418 enum dwarf_form form
;
419 bfd_vma implicit_const
;
422 /* Map of uncompressed DWARF debug section name to compressed one. It
423 is terminated by NULL uncompressed_name. */
425 const struct dwarf_debug_section dwarf_debug_sections
[] =
427 { ".debug_abbrev", ".zdebug_abbrev" },
428 { ".debug_aranges", ".zdebug_aranges" },
429 { ".debug_frame", ".zdebug_frame" },
430 { ".debug_info", ".zdebug_info" },
431 { ".debug_info", ".zdebug_info" },
432 { ".debug_line", ".zdebug_line" },
433 { ".debug_loc", ".zdebug_loc" },
434 { ".debug_macinfo", ".zdebug_macinfo" },
435 { ".debug_macro", ".zdebug_macro" },
436 { ".debug_pubnames", ".zdebug_pubnames" },
437 { ".debug_pubtypes", ".zdebug_pubtypes" },
438 { ".debug_ranges", ".zdebug_ranges" },
439 { ".debug_rnglists", ".zdebug_rnglist" },
440 { ".debug_static_func", ".zdebug_static_func" },
441 { ".debug_static_vars", ".zdebug_static_vars" },
442 { ".debug_str", ".zdebug_str", },
443 { ".debug_str", ".zdebug_str", },
444 { ".debug_str_offsets", ".zdebug_str_offsets", },
445 { ".debug_addr", ".zdebug_addr", },
446 { ".debug_line_str", ".zdebug_line_str", },
447 { ".debug_types", ".zdebug_types" },
448 /* GNU DWARF 1 extensions */
449 { ".debug_sfnames", ".zdebug_sfnames" },
450 { ".debug_srcinfo", ".zebug_srcinfo" },
451 /* SGI/MIPS DWARF 2 extensions */
452 { ".debug_funcnames", ".zdebug_funcnames" },
453 { ".debug_typenames", ".zdebug_typenames" },
454 { ".debug_varnames", ".zdebug_varnames" },
455 { ".debug_weaknames", ".zdebug_weaknames" },
459 /* NB/ Numbers in this enum must match up with indices
460 into the dwarf_debug_sections[] array above. */
461 enum dwarf_debug_section_enum
493 /* A static assertion. */
494 extern int dwarf_debug_section_assert
[ARRAY_SIZE (dwarf_debug_sections
)
495 == debug_max
+ 1 ? 1 : -1];
497 #ifndef ABBREV_HASH_SIZE
498 #define ABBREV_HASH_SIZE 121
500 #ifndef ATTR_ALLOC_CHUNK
501 #define ATTR_ALLOC_CHUNK 4
504 /* Variable and function hash tables. This is used to speed up look-up
505 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
506 In order to share code between variable and function infos, we use
507 a list of untyped pointer for all variable/function info associated with
508 a symbol. We waste a bit of memory for list with one node but that
509 simplifies the code. */
511 struct info_list_node
513 struct info_list_node
*next
;
517 /* Info hash entry. */
518 struct info_hash_entry
520 struct bfd_hash_entry root
;
521 struct info_list_node
*head
;
524 struct info_hash_table
526 struct bfd_hash_table base
;
529 /* Function to create a new entry in info hash table. */
531 static struct bfd_hash_entry
*
532 info_hash_table_newfunc (struct bfd_hash_entry
*entry
,
533 struct bfd_hash_table
*table
,
536 struct info_hash_entry
*ret
= (struct info_hash_entry
*) entry
;
538 /* Allocate the structure if it has not already been allocated by a
542 ret
= (struct info_hash_entry
*) bfd_hash_allocate (table
,
548 /* Call the allocation method of the base class. */
549 ret
= ((struct info_hash_entry
*)
550 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
552 /* Initialize the local fields here. */
556 return (struct bfd_hash_entry
*) ret
;
559 /* Function to create a new info hash table. It returns a pointer to the
560 newly created table or NULL if there is any error. We need abfd
561 solely for memory allocation. */
563 static struct info_hash_table
*
564 create_info_hash_table (bfd
*abfd
)
566 struct info_hash_table
*hash_table
;
568 hash_table
= ((struct info_hash_table
*)
569 bfd_alloc (abfd
, sizeof (struct info_hash_table
)));
573 if (!bfd_hash_table_init (&hash_table
->base
, info_hash_table_newfunc
,
574 sizeof (struct info_hash_entry
)))
576 bfd_release (abfd
, hash_table
);
583 /* Insert an info entry into an info hash table. We do not check of
584 duplicate entries. Also, the caller need to guarantee that the
585 right type of info in inserted as info is passed as a void* pointer.
586 This function returns true if there is no error. */
589 insert_info_hash_table (struct info_hash_table
*hash_table
,
594 struct info_hash_entry
*entry
;
595 struct info_list_node
*node
;
597 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
,
602 node
= (struct info_list_node
*) bfd_hash_allocate (&hash_table
->base
,
608 node
->next
= entry
->head
;
614 /* Look up an info entry list from an info hash table. Return NULL
617 static struct info_list_node
*
618 lookup_info_hash_table (struct info_hash_table
*hash_table
, const char *key
)
620 struct info_hash_entry
*entry
;
622 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
, key
,
624 return entry
? entry
->head
: NULL
;
627 /* Read a section into its appropriate place in the dwarf2_debug
628 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
629 not NULL, use bfd_simple_get_relocated_section_contents to read the
630 section contents, otherwise use bfd_get_section_contents. Fail if
631 the located section does not contain at least OFFSET bytes. */
634 read_section (bfd
*abfd
,
635 const struct dwarf_debug_section
*sec
,
638 bfd_byte
**section_buffer
,
639 bfd_size_type
*section_size
)
641 const char *section_name
= sec
->uncompressed_name
;
642 bfd_byte
*contents
= *section_buffer
;
644 /* The section may have already been read. */
645 if (contents
== NULL
)
651 msec
= bfd_get_section_by_name (abfd
, section_name
);
654 section_name
= sec
->compressed_name
;
655 msec
= bfd_get_section_by_name (abfd
, section_name
);
659 _bfd_error_handler (_("DWARF error: can't find %s section."),
660 sec
->uncompressed_name
);
661 bfd_set_error (bfd_error_bad_value
);
665 amt
= bfd_get_section_limit_octets (abfd
, msec
);
666 filesize
= bfd_get_file_size (abfd
);
667 /* PR 28834: A compressed debug section could well decompress to a size
668 larger than the file, so we choose an arbitrary modifier of 10x in
669 the test below. If this ever turns out to be insufficient, it can
670 be changed by a future update. */
671 if (amt
>= filesize
* 10)
674 _bfd_error_handler (_("DWARF error: section %s is larger than 10x its filesize! (0x%lx vs 0x%lx)"),
675 section_name
, (long) amt
, (long) filesize
);
676 bfd_set_error (bfd_error_bad_value
);
680 /* Paranoia - alloc one extra so that we can make sure a string
681 section is NUL terminated. */
685 /* Paranoia - this should never happen. */
686 bfd_set_error (bfd_error_no_memory
);
689 contents
= (bfd_byte
*) bfd_malloc (amt
);
690 if (contents
== NULL
)
693 ? !bfd_simple_get_relocated_section_contents (abfd
, msec
, contents
,
695 : !bfd_get_section_contents (abfd
, msec
, contents
, 0, *section_size
))
700 contents
[*section_size
] = 0;
701 *section_buffer
= contents
;
704 /* It is possible to get a bad value for the offset into the section
705 that the client wants. Validate it here to avoid trouble later. */
706 if (offset
!= 0 && offset
>= *section_size
)
708 /* xgettext: c-format */
709 _bfd_error_handler (_("DWARF error: offset (%" PRIu64
")"
710 " greater than or equal to %s size (%" PRIu64
")"),
711 (uint64_t) offset
, section_name
,
712 (uint64_t) *section_size
);
713 bfd_set_error (bfd_error_bad_value
);
720 /* Read dwarf information from a buffer. */
722 static inline uint64_t
723 read_n_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
, int n
)
725 bfd_byte
*buf
= *ptr
;
732 return bfd_get (n
* 8, abfd
, buf
);
736 read_1_byte (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
738 return read_n_bytes (abfd
, ptr
, end
, 1);
742 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
**ptr
, bfd_byte
*end
)
744 bfd_byte
*buf
= *ptr
;
751 return bfd_get_signed_8 (abfd
, buf
);
755 read_2_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
757 return read_n_bytes (abfd
, ptr
, end
, 2);
761 read_3_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
763 unsigned int val
= read_1_byte (abfd
, ptr
, end
);
765 val
|= read_1_byte (abfd
, ptr
, end
);
767 val
|= read_1_byte (abfd
, ptr
, end
);
768 if (bfd_little_endian (abfd
))
769 val
= (((val
>> 16) & 0xff)
771 | ((val
& 0xff) << 16));
776 read_4_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
778 return read_n_bytes (abfd
, ptr
, end
, 4);
782 read_8_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
784 return read_n_bytes (abfd
, ptr
, end
, 8);
787 static struct dwarf_block
*
788 read_blk (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
, size_t size
)
790 bfd_byte
*buf
= *ptr
;
791 struct dwarf_block
*block
;
793 block
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (*block
));
797 if (size
> (size_t) (end
- buf
))
812 /* Scans a NUL terminated string starting at *PTR, returning a pointer to it.
813 Bytes at or beyond BUF_END will not be read. Returns NULL if the
814 terminator is not found or if the string is empty. *PTR is
815 incremented over the bytes scanned, including the terminator. */
818 read_string (bfd_byte
**ptr
,
821 bfd_byte
*buf
= *ptr
;
824 while (buf
< buf_end
)
837 /* Reads an offset from *PTR and then locates the string at this offset
838 inside the debug string section. Returns a pointer to the string.
839 Increments *PTR by the number of bytes read for the offset. This
840 value is set even if the function fails. Bytes at or beyond
841 BUF_END will not be read. Returns NULL if there was a problem, or
842 if the string is empty. Does not check for NUL termination of the
846 read_indirect_string (struct comp_unit
*unit
,
851 struct dwarf2_debug
*stash
= unit
->stash
;
852 struct dwarf2_debug_file
*file
= unit
->file
;
855 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
861 if (unit
->offset_size
== 4)
862 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
864 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
866 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
868 &file
->dwarf_str_buffer
, &file
->dwarf_str_size
))
871 str
= (char *) file
->dwarf_str_buffer
+ offset
;
877 /* Like read_indirect_string but from .debug_line_str section. */
880 read_indirect_line_string (struct comp_unit
*unit
,
885 struct dwarf2_debug
*stash
= unit
->stash
;
886 struct dwarf2_debug_file
*file
= unit
->file
;
889 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
895 if (unit
->offset_size
== 4)
896 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
898 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
900 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_line_str
],
902 &file
->dwarf_line_str_buffer
,
903 &file
->dwarf_line_str_size
))
906 str
= (char *) file
->dwarf_line_str_buffer
+ offset
;
912 /* Like read_indirect_string but uses a .debug_str located in
913 an alternate file pointed to by the .gnu_debugaltlink section.
914 Used to impement DW_FORM_GNU_strp_alt. */
917 read_alt_indirect_string (struct comp_unit
*unit
,
922 struct dwarf2_debug
*stash
= unit
->stash
;
925 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
931 if (unit
->offset_size
== 4)
932 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
934 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
936 if (stash
->alt
.bfd_ptr
== NULL
)
939 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
941 if (debug_filename
== NULL
)
944 debug_bfd
= bfd_openr (debug_filename
, NULL
);
945 free (debug_filename
);
946 if (debug_bfd
== NULL
)
947 /* FIXME: Should we report our failure to follow the debuglink ? */
950 if (!bfd_check_format (debug_bfd
, bfd_object
))
952 bfd_close (debug_bfd
);
955 stash
->alt
.bfd_ptr
= debug_bfd
;
958 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
959 stash
->debug_sections
+ debug_str_alt
,
960 stash
->alt
.syms
, offset
,
961 &stash
->alt
.dwarf_str_buffer
,
962 &stash
->alt
.dwarf_str_size
))
965 str
= (char *) stash
->alt
.dwarf_str_buffer
+ offset
;
972 /* Resolve an alternate reference from UNIT at OFFSET.
973 Returns a pointer into the loaded alternate CU upon success
974 or NULL upon failure. */
977 read_alt_indirect_ref (struct comp_unit
*unit
, uint64_t offset
)
979 struct dwarf2_debug
*stash
= unit
->stash
;
981 if (stash
->alt
.bfd_ptr
== NULL
)
984 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
986 if (debug_filename
== NULL
)
989 debug_bfd
= bfd_openr (debug_filename
, NULL
);
990 free (debug_filename
);
991 if (debug_bfd
== NULL
)
992 /* FIXME: Should we report our failure to follow the debuglink ? */
995 if (!bfd_check_format (debug_bfd
, bfd_object
))
997 bfd_close (debug_bfd
);
1000 stash
->alt
.bfd_ptr
= debug_bfd
;
1003 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
1004 stash
->debug_sections
+ debug_info_alt
,
1005 stash
->alt
.syms
, offset
,
1006 &stash
->alt
.dwarf_info_buffer
,
1007 &stash
->alt
.dwarf_info_size
))
1010 return stash
->alt
.dwarf_info_buffer
+ offset
;
1014 read_address (struct comp_unit
*unit
, bfd_byte
**ptr
, bfd_byte
*buf_end
)
1016 bfd_byte
*buf
= *ptr
;
1019 if (bfd_get_flavour (unit
->abfd
) == bfd_target_elf_flavour
)
1020 signed_vma
= get_elf_backend_data (unit
->abfd
)->sign_extend_vma
;
1022 if (unit
->addr_size
> (size_t) (buf_end
- buf
))
1028 *ptr
= buf
+ unit
->addr_size
;
1031 switch (unit
->addr_size
)
1034 return bfd_get_signed_64 (unit
->abfd
, buf
);
1036 return bfd_get_signed_32 (unit
->abfd
, buf
);
1038 return bfd_get_signed_16 (unit
->abfd
, buf
);
1045 switch (unit
->addr_size
)
1048 return bfd_get_64 (unit
->abfd
, buf
);
1050 return bfd_get_32 (unit
->abfd
, buf
);
1052 return bfd_get_16 (unit
->abfd
, buf
);
1059 /* Lookup an abbrev_info structure in the abbrev hash table. */
1061 static struct abbrev_info
*
1062 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
1064 unsigned int hash_number
;
1065 struct abbrev_info
*abbrev
;
1067 hash_number
= number
% ABBREV_HASH_SIZE
;
1068 abbrev
= abbrevs
[hash_number
];
1072 if (abbrev
->number
== number
)
1075 abbrev
= abbrev
->next
;
1081 /* We keep a hash table to map .debug_abbrev section offsets to the
1082 array of abbrevs, so that compilation units using the same set of
1083 abbrevs do not waste memory. */
1085 struct abbrev_offset_entry
1088 struct abbrev_info
**abbrevs
;
1092 hash_abbrev (const void *p
)
1094 const struct abbrev_offset_entry
*ent
= p
;
1095 return htab_hash_pointer ((void *) ent
->offset
);
1099 eq_abbrev (const void *pa
, const void *pb
)
1101 const struct abbrev_offset_entry
*a
= pa
;
1102 const struct abbrev_offset_entry
*b
= pb
;
1103 return a
->offset
== b
->offset
;
1107 del_abbrev (void *p
)
1109 struct abbrev_offset_entry
*ent
= p
;
1110 struct abbrev_info
**abbrevs
= ent
->abbrevs
;
1113 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1115 struct abbrev_info
*abbrev
= abbrevs
[i
];
1119 free (abbrev
->attrs
);
1120 abbrev
= abbrev
->next
;
1126 /* In DWARF version 2, the description of the debugging information is
1127 stored in a separate .debug_abbrev section. Before we read any
1128 dies from a section we read in all abbreviations and install them
1131 static struct abbrev_info
**
1132 read_abbrevs (bfd
*abfd
, uint64_t offset
, struct dwarf2_debug
*stash
,
1133 struct dwarf2_debug_file
*file
)
1135 struct abbrev_info
**abbrevs
;
1136 bfd_byte
*abbrev_ptr
;
1137 bfd_byte
*abbrev_end
;
1138 struct abbrev_info
*cur_abbrev
;
1139 unsigned int abbrev_number
, abbrev_name
;
1140 unsigned int abbrev_form
, hash_number
;
1143 struct abbrev_offset_entry ent
= { offset
, NULL
};
1145 if (ent
.offset
!= offset
)
1148 slot
= htab_find_slot (file
->abbrev_offsets
, &ent
, INSERT
);
1152 return ((struct abbrev_offset_entry
*) (*slot
))->abbrevs
;
1154 if (! read_section (abfd
, &stash
->debug_sections
[debug_abbrev
],
1156 &file
->dwarf_abbrev_buffer
,
1157 &file
->dwarf_abbrev_size
))
1160 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
1161 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
1162 if (abbrevs
== NULL
)
1165 abbrev_ptr
= file
->dwarf_abbrev_buffer
+ offset
;
1166 abbrev_end
= file
->dwarf_abbrev_buffer
+ file
->dwarf_abbrev_size
;
1167 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1170 /* Loop until we reach an abbrev number of 0. */
1171 while (abbrev_number
)
1173 amt
= sizeof (struct abbrev_info
);
1174 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
1175 if (cur_abbrev
== NULL
)
1178 /* Read in abbrev header. */
1179 cur_abbrev
->number
= abbrev_number
;
1180 cur_abbrev
->tag
= (enum dwarf_tag
)
1181 _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1183 cur_abbrev
->has_children
= read_1_byte (abfd
, &abbrev_ptr
, abbrev_end
);
1185 /* Now read in declarations. */
1188 /* Initialize it just to avoid a GCC false warning. */
1189 bfd_vma implicit_const
= -1;
1191 abbrev_name
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1193 abbrev_form
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1195 if (abbrev_form
== DW_FORM_implicit_const
)
1196 implicit_const
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1198 if (abbrev_name
== 0)
1201 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
1203 struct attr_abbrev
*tmp
;
1205 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
1206 amt
*= sizeof (struct attr_abbrev
);
1207 tmp
= (struct attr_abbrev
*) bfd_realloc (cur_abbrev
->attrs
, amt
);
1210 cur_abbrev
->attrs
= tmp
;
1213 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
1214 = (enum dwarf_attribute
) abbrev_name
;
1215 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].form
1216 = (enum dwarf_form
) abbrev_form
;
1217 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].implicit_const
1219 ++cur_abbrev
->num_attrs
;
1222 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
1223 cur_abbrev
->next
= abbrevs
[hash_number
];
1224 abbrevs
[hash_number
] = cur_abbrev
;
1226 /* Get next abbreviation.
1227 Under Irix6 the abbreviations for a compilation unit are not
1228 always properly terminated with an abbrev number of 0.
1229 Exit loop if we encounter an abbreviation which we have
1230 already read (which means we are about to read the abbreviations
1231 for the next compile unit) or if the end of the abbreviation
1232 table is reached. */
1233 if ((size_t) (abbrev_ptr
- file
->dwarf_abbrev_buffer
)
1234 >= file
->dwarf_abbrev_size
)
1236 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1238 if (lookup_abbrev (abbrev_number
, abbrevs
) != NULL
)
1242 *slot
= bfd_malloc (sizeof ent
);
1245 ent
.abbrevs
= abbrevs
;
1246 memcpy (*slot
, &ent
, sizeof ent
);
1250 if (abbrevs
!= NULL
)
1254 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1256 struct abbrev_info
*abbrev
= abbrevs
[i
];
1260 free (abbrev
->attrs
);
1261 abbrev
= abbrev
->next
;
1269 /* Returns true if the form is one which has a string value. */
1272 is_str_form (const struct attribute
*attr
)
1276 case DW_FORM_string
:
1283 case DW_FORM_line_strp
:
1284 case DW_FORM_GNU_strp_alt
:
1292 /* Returns true if the form is one which has an integer value. */
1295 is_int_form (const struct attribute
*attr
)
1307 case DW_FORM_ref_addr
:
1312 case DW_FORM_ref_udata
:
1313 case DW_FORM_sec_offset
:
1314 case DW_FORM_flag_present
:
1315 case DW_FORM_ref_sig8
:
1317 case DW_FORM_implicit_const
:
1318 case DW_FORM_addrx1
:
1319 case DW_FORM_addrx2
:
1320 case DW_FORM_addrx3
:
1321 case DW_FORM_addrx4
:
1322 case DW_FORM_GNU_ref_alt
:
1330 /* Returns true if the form is strx[1-4]. */
1333 is_strx_form (enum dwarf_form form
)
1335 return (form
== DW_FORM_strx
1336 || form
== DW_FORM_strx1
1337 || form
== DW_FORM_strx2
1338 || form
== DW_FORM_strx3
1339 || form
== DW_FORM_strx4
);
1342 /* Return true if the form is addrx[1-4]. */
1345 is_addrx_form (enum dwarf_form form
)
1347 return (form
== DW_FORM_addrx
1348 || form
== DW_FORM_addrx1
1349 || form
== DW_FORM_addrx2
1350 || form
== DW_FORM_addrx3
1351 || form
== DW_FORM_addrx4
);
1354 /* Returns the address in .debug_addr section using DW_AT_addr_base.
1355 Used to implement DW_FORM_addrx*. */
1357 read_indexed_address (uint64_t idx
, struct comp_unit
*unit
)
1359 struct dwarf2_debug
*stash
= unit
->stash
;
1360 struct dwarf2_debug_file
*file
= unit
->file
;
1367 if (!read_section (unit
->abfd
, &stash
->debug_sections
[debug_addr
],
1369 &file
->dwarf_addr_buffer
, &file
->dwarf_addr_size
))
1372 if (_bfd_mul_overflow (idx
, unit
->offset_size
, &offset
))
1375 offset
+= unit
->dwarf_addr_offset
;
1376 if (offset
< unit
->dwarf_addr_offset
1377 || offset
> file
->dwarf_addr_size
1378 || file
->dwarf_addr_size
- offset
< unit
->offset_size
)
1381 info_ptr
= file
->dwarf_addr_buffer
+ offset
;
1383 if (unit
->offset_size
== 4)
1384 return bfd_get_32 (unit
->abfd
, info_ptr
);
1385 else if (unit
->offset_size
== 8)
1386 return bfd_get_64 (unit
->abfd
, info_ptr
);
1391 /* Returns the string using DW_AT_str_offsets_base.
1392 Used to implement DW_FORM_strx*. */
1394 read_indexed_string (uint64_t idx
, struct comp_unit
*unit
)
1396 struct dwarf2_debug
*stash
= unit
->stash
;
1397 struct dwarf2_debug_file
*file
= unit
->file
;
1399 uint64_t str_offset
;
1405 if (!read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
1407 &file
->dwarf_str_buffer
, &file
->dwarf_str_size
))
1410 if (!read_section (unit
->abfd
, &stash
->debug_sections
[debug_str_offsets
],
1412 &file
->dwarf_str_offsets_buffer
,
1413 &file
->dwarf_str_offsets_size
))
1416 if (_bfd_mul_overflow (idx
, unit
->offset_size
, &offset
))
1419 offset
+= unit
->dwarf_str_offset
;
1420 if (offset
< unit
->dwarf_str_offset
1421 || offset
> file
->dwarf_str_offsets_size
1422 || file
->dwarf_str_offsets_size
- offset
< unit
->offset_size
)
1425 info_ptr
= file
->dwarf_str_offsets_buffer
+ offset
;
1427 if (unit
->offset_size
== 4)
1428 str_offset
= bfd_get_32 (unit
->abfd
, info_ptr
);
1429 else if (unit
->offset_size
== 8)
1430 str_offset
= bfd_get_64 (unit
->abfd
, info_ptr
);
1434 if (str_offset
>= file
->dwarf_str_size
)
1436 return (const char *) file
->dwarf_str_buffer
+ str_offset
;
1439 /* Read and fill in the value of attribute ATTR as described by FORM.
1440 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1441 Returns an updated INFO_PTR taking into account the amount of data read. */
1444 read_attribute_value (struct attribute
* attr
,
1446 bfd_vma implicit_const
,
1447 struct comp_unit
* unit
,
1448 bfd_byte
* info_ptr
,
1449 bfd_byte
* info_ptr_end
)
1451 bfd
*abfd
= unit
->abfd
;
1454 if (info_ptr
>= info_ptr_end
&& form
!= DW_FORM_flag_present
)
1456 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1457 bfd_set_error (bfd_error_bad_value
);
1461 attr
->form
= (enum dwarf_form
) form
;
1465 case DW_FORM_flag_present
:
1468 case DW_FORM_ref_addr
:
1469 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1471 if (unit
->version
>= 3)
1473 if (unit
->offset_size
== 4)
1474 attr
->u
.val
= read_4_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1476 attr
->u
.val
= read_8_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1481 attr
->u
.val
= read_address (unit
, &info_ptr
, info_ptr_end
);
1483 case DW_FORM_GNU_ref_alt
:
1484 case DW_FORM_sec_offset
:
1485 if (unit
->offset_size
== 4)
1486 attr
->u
.val
= read_4_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1488 attr
->u
.val
= read_8_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1490 case DW_FORM_block2
:
1491 amt
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1492 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1493 if (attr
->u
.blk
== NULL
)
1496 case DW_FORM_block4
:
1497 amt
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1498 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1499 if (attr
->u
.blk
== NULL
)
1505 attr
->u
.val
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1507 case DW_FORM_addrx1
:
1508 attr
->u
.val
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1509 /* dwarf_addr_offset value 0 indicates the attribute DW_AT_addr_base
1511 if (unit
->dwarf_addr_offset
!= 0)
1512 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
1516 attr
->u
.val
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1518 case DW_FORM_addrx2
:
1519 attr
->u
.val
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1520 if (unit
->dwarf_addr_offset
!= 0)
1521 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
1523 case DW_FORM_addrx3
:
1524 attr
->u
.val
= read_3_bytes (abfd
, &info_ptr
, info_ptr_end
);
1525 if (unit
->dwarf_addr_offset
!= 0)
1526 attr
->u
.val
= read_indexed_address(attr
->u
.val
, unit
);
1530 attr
->u
.val
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1532 case DW_FORM_addrx4
:
1533 attr
->u
.val
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1534 if (unit
->dwarf_addr_offset
!= 0)
1535 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
1539 case DW_FORM_ref_sig8
:
1540 attr
->u
.val
= read_8_bytes (abfd
, &info_ptr
, info_ptr_end
);
1542 case DW_FORM_string
:
1543 attr
->u
.str
= read_string (&info_ptr
, info_ptr_end
);
1546 attr
->u
.str
= read_indirect_string (unit
, &info_ptr
, info_ptr_end
);
1548 case DW_FORM_line_strp
:
1549 attr
->u
.str
= read_indirect_line_string (unit
, &info_ptr
, info_ptr_end
);
1551 case DW_FORM_GNU_strp_alt
:
1552 attr
->u
.str
= read_alt_indirect_string (unit
, &info_ptr
, info_ptr_end
);
1555 attr
->u
.val
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1556 /* dwarf_str_offset value 0 indicates the attribute DW_AT_str_offsets_base
1558 if (unit
->dwarf_str_offset
!= 0)
1559 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1564 attr
->u
.val
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1565 if (unit
->dwarf_str_offset
!= 0)
1566 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1571 attr
->u
.val
= read_3_bytes (abfd
, &info_ptr
, info_ptr_end
);
1572 if (unit
->dwarf_str_offset
!= 0)
1573 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1578 attr
->u
.val
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1579 if (unit
->dwarf_str_offset
!= 0)
1580 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1585 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1586 false, info_ptr_end
);
1587 if (unit
->dwarf_str_offset
!= 0)
1588 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1592 case DW_FORM_exprloc
:
1594 amt
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1595 false, info_ptr_end
);
1596 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1597 if (attr
->u
.blk
== NULL
)
1600 case DW_FORM_block1
:
1601 amt
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1602 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1603 if (attr
->u
.blk
== NULL
)
1607 attr
->u
.sval
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1608 true, info_ptr_end
);
1611 case DW_FORM_rnglistx
:
1612 case DW_FORM_loclistx
:
1613 /* FIXME: Add support for these forms! */
1615 case DW_FORM_ref_udata
:
1617 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1618 false, info_ptr_end
);
1621 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1622 false, info_ptr_end
);
1623 if (unit
->dwarf_addr_offset
!= 0)
1624 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
1626 case DW_FORM_indirect
:
1627 form
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1628 false, info_ptr_end
);
1629 if (form
== DW_FORM_implicit_const
)
1630 implicit_const
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1631 true, info_ptr_end
);
1632 info_ptr
= read_attribute_value (attr
, form
, implicit_const
, unit
,
1633 info_ptr
, info_ptr_end
);
1635 case DW_FORM_implicit_const
:
1636 attr
->form
= DW_FORM_sdata
;
1637 attr
->u
.sval
= implicit_const
;
1639 case DW_FORM_data16
:
1640 /* This is really a "constant", but there is no way to store that
1641 so pretend it is a 16 byte block instead. */
1642 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, 16);
1643 if (attr
->u
.blk
== NULL
)
1648 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1650 bfd_set_error (bfd_error_bad_value
);
1656 /* Read an attribute described by an abbreviated attribute. */
1659 read_attribute (struct attribute
* attr
,
1660 struct attr_abbrev
* abbrev
,
1661 struct comp_unit
* unit
,
1662 bfd_byte
* info_ptr
,
1663 bfd_byte
* info_ptr_end
)
1665 attr
->name
= abbrev
->name
;
1666 info_ptr
= read_attribute_value (attr
, abbrev
->form
, abbrev
->implicit_const
,
1667 unit
, info_ptr
, info_ptr_end
);
1671 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1675 non_mangled (int lang
)
1685 case DW_LANG_Cobol74
:
1686 case DW_LANG_Cobol85
:
1687 case DW_LANG_Fortran77
:
1688 case DW_LANG_Pascal83
:
1694 case DW_LANG_Mips_Assembler
:
1699 /* Source line information table routines. */
1701 #define FILE_ALLOC_CHUNK 5
1702 #define DIR_ALLOC_CHUNK 5
1706 struct line_info
* prev_line
;
1710 unsigned int column
;
1711 unsigned int discriminator
;
1712 unsigned char op_index
;
1713 unsigned char end_sequence
; /* End of (sequential) code sequence. */
1724 struct line_sequence
1727 struct line_sequence
* prev_sequence
;
1728 struct line_info
* last_line
; /* Largest VMA. */
1729 struct line_info
** line_info_lookup
;
1730 bfd_size_type num_lines
;
1733 struct line_info_table
1736 unsigned int num_files
;
1737 unsigned int num_dirs
;
1738 unsigned int num_sequences
;
1741 struct fileinfo
* files
;
1742 struct line_sequence
* sequences
;
1743 struct line_info
* lcl_head
; /* Local head; used in 'add_line_info'. */
1746 /* Remember some information about each function. If the function is
1747 inlined (DW_TAG_inlined_subroutine) it may have two additional
1748 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1749 source code location where this function was inlined. */
1753 /* Pointer to previous function in list of all functions. */
1754 struct funcinfo
*prev_func
;
1755 /* Pointer to function one scope higher. */
1756 struct funcinfo
*caller_func
;
1757 /* Source location file name where caller_func inlines this func. */
1759 /* Source location file name. */
1761 /* Source location line number where caller_func inlines this func. */
1763 /* Source location line number. */
1768 struct arange arange
;
1769 /* Where the symbol is defined. */
1771 /* The offset of the funcinfo from the start of the unit. */
1772 uint64_t unit_offset
;
1775 struct lookup_funcinfo
1777 /* Function information corresponding to this lookup table entry. */
1778 struct funcinfo
*funcinfo
;
1780 /* The lowest address for this specific function. */
1783 /* The highest address of this function before the lookup table is sorted.
1784 The highest address of all prior functions after the lookup table is
1785 sorted, which is used for binary search. */
1787 /* Index of this function, used to ensure qsort is stable. */
1793 /* Pointer to previous variable in list of all variables. */
1794 struct varinfo
*prev_var
;
1795 /* The offset of the varinfo from the start of the unit. */
1796 uint64_t unit_offset
;
1797 /* Source location file name. */
1799 /* Source location line number. */
1801 /* The type of this variable. */
1803 /* The name of the variable, if it has one. */
1805 /* The address of the variable. */
1807 /* Where the symbol is defined. */
1809 /* Is this a stack variable? */
1813 /* Return TRUE if NEW_LINE should sort after LINE. */
1816 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
1818 return (new_line
->address
> line
->address
1819 || (new_line
->address
== line
->address
1820 && new_line
->op_index
> line
->op_index
));
1824 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1825 that the list is sorted. Note that the line_info list is sorted from
1826 highest to lowest VMA (with possible duplicates); that is,
1827 line_info->prev_line always accesses an equal or smaller VMA. */
1830 add_line_info (struct line_info_table
*table
,
1832 unsigned char op_index
,
1835 unsigned int column
,
1836 unsigned int discriminator
,
1839 size_t amt
= sizeof (struct line_info
);
1840 struct line_sequence
* seq
= table
->sequences
;
1841 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
1846 /* Set member data of 'info'. */
1847 info
->prev_line
= NULL
;
1848 info
->address
= address
;
1849 info
->op_index
= op_index
;
1851 info
->column
= column
;
1852 info
->discriminator
= discriminator
;
1853 info
->end_sequence
= end_sequence
;
1855 if (filename
&& filename
[0])
1857 info
->filename
= (char *) bfd_alloc (table
->abfd
, strlen (filename
) + 1);
1858 if (info
->filename
== NULL
)
1860 strcpy (info
->filename
, filename
);
1863 info
->filename
= NULL
;
1865 /* Find the correct location for 'info'. Normally we will receive
1866 new line_info data 1) in order and 2) with increasing VMAs.
1867 However some compilers break the rules (cf. decode_line_info) and
1868 so we include some heuristics for quickly finding the correct
1869 location for 'info'. In particular, these heuristics optimize for
1870 the common case in which the VMA sequence that we receive is a
1871 list of locally sorted VMAs such as
1872 p...z a...j (where a < j < p < z)
1874 Note: table->lcl_head is used to head an *actual* or *possible*
1875 sub-sequence within the list (such as a...j) that is not directly
1876 headed by table->last_line
1878 Note: we may receive duplicate entries from 'decode_line_info'. */
1881 && seq
->last_line
->address
== address
1882 && seq
->last_line
->op_index
== op_index
1883 && seq
->last_line
->end_sequence
== end_sequence
)
1885 /* We only keep the last entry with the same address and end
1886 sequence. See PR ld/4986. */
1887 if (table
->lcl_head
== seq
->last_line
)
1888 table
->lcl_head
= info
;
1889 info
->prev_line
= seq
->last_line
->prev_line
;
1890 seq
->last_line
= info
;
1892 else if (!seq
|| seq
->last_line
->end_sequence
)
1894 /* Start a new line sequence. */
1895 amt
= sizeof (struct line_sequence
);
1896 seq
= (struct line_sequence
*) bfd_malloc (amt
);
1899 seq
->low_pc
= address
;
1900 seq
->prev_sequence
= table
->sequences
;
1901 seq
->last_line
= info
;
1902 table
->lcl_head
= info
;
1903 table
->sequences
= seq
;
1904 table
->num_sequences
++;
1906 else if (info
->end_sequence
1907 || new_line_sorts_after (info
, seq
->last_line
))
1909 /* Normal case: add 'info' to the beginning of the current sequence. */
1910 info
->prev_line
= seq
->last_line
;
1911 seq
->last_line
= info
;
1913 /* lcl_head: initialize to head a *possible* sequence at the end. */
1914 if (!table
->lcl_head
)
1915 table
->lcl_head
= info
;
1917 else if (!new_line_sorts_after (info
, table
->lcl_head
)
1918 && (!table
->lcl_head
->prev_line
1919 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
1921 /* Abnormal but easy: lcl_head is the head of 'info'. */
1922 info
->prev_line
= table
->lcl_head
->prev_line
;
1923 table
->lcl_head
->prev_line
= info
;
1927 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1928 are valid heads for 'info'. Reset 'lcl_head'. */
1929 struct line_info
* li2
= seq
->last_line
; /* Always non-NULL. */
1930 struct line_info
* li1
= li2
->prev_line
;
1934 if (!new_line_sorts_after (info
, li2
)
1935 && new_line_sorts_after (info
, li1
))
1938 li2
= li1
; /* always non-NULL */
1939 li1
= li1
->prev_line
;
1941 table
->lcl_head
= li2
;
1942 info
->prev_line
= table
->lcl_head
->prev_line
;
1943 table
->lcl_head
->prev_line
= info
;
1944 if (address
< seq
->low_pc
)
1945 seq
->low_pc
= address
;
1950 /* Extract a fully qualified filename from a line info table.
1951 The returned string has been malloc'ed and it is the caller's
1952 responsibility to free it. */
1955 concat_filename (struct line_info_table
*table
, unsigned int file
)
1959 if (table
== NULL
|| file
- 1 >= table
->num_files
)
1961 /* FILE == 0 means unknown. */
1964 (_("DWARF error: mangled line number section (bad file number)"));
1965 return strdup ("<unknown>");
1968 filename
= table
->files
[file
- 1].name
;
1969 if (filename
== NULL
)
1970 return strdup ("<unknown>");
1972 if (!IS_ABSOLUTE_PATH (filename
))
1974 char *dir_name
= NULL
;
1975 char *subdir_name
= NULL
;
1979 if (table
->files
[file
- 1].dir
1980 /* PR 17512: file: 0317e960. */
1981 && table
->files
[file
- 1].dir
<= table
->num_dirs
1982 /* PR 17512: file: 7f3d2e4b. */
1983 && table
->dirs
!= NULL
)
1984 subdir_name
= table
->dirs
[table
->files
[file
- 1].dir
- 1];
1986 if (!subdir_name
|| !IS_ABSOLUTE_PATH (subdir_name
))
1987 dir_name
= table
->comp_dir
;
1991 dir_name
= subdir_name
;
1996 return strdup (filename
);
1998 len
= strlen (dir_name
) + strlen (filename
) + 2;
2002 len
+= strlen (subdir_name
) + 1;
2003 name
= (char *) bfd_malloc (len
);
2005 sprintf (name
, "%s/%s/%s", dir_name
, subdir_name
, filename
);
2009 name
= (char *) bfd_malloc (len
);
2011 sprintf (name
, "%s/%s", dir_name
, filename
);
2017 return strdup (filename
);
2020 /* Number of bits in a bfd_vma. */
2021 #define VMA_BITS (8 * sizeof (bfd_vma))
2023 /* Check whether [low1, high1) can be combined with [low2, high2),
2024 i.e., they touch or overlap. */
2025 static bool ranges_overlap (bfd_vma low1
,
2030 if (low1
== low2
|| high1
== high2
)
2033 /* Sort so that low1 is below low2. */
2047 /* We touch iff low2 == high1.
2048 We overlap iff low2 is within [low1, high1). */
2049 return low2
<= high1
;
2052 /* Insert an address range in the trie mapping addresses to compilation units.
2053 Will return the new trie node (usually the same as is being sent in, but
2054 in case of a leaf-to-interior conversion, or expansion of a leaf, it may be
2055 different), or NULL on failure.
2057 static struct trie_node
*insert_arange_in_trie(bfd
*abfd
,
2058 struct trie_node
*trie
,
2060 unsigned int trie_pc_bits
,
2061 struct comp_unit
*unit
,
2065 bfd_vma clamped_low_pc
, clamped_high_pc
;
2066 int ch
, from_ch
, to_ch
;
2067 bool is_full_leaf
= false;
2069 /* See if we can extend any of the existing ranges. This merging
2070 isn't perfect (if merging opens up the possibility of merging two existing
2071 ranges, we won't find them), but it takes the majority of the cases. */
2072 if (trie
->num_room_in_leaf
> 0)
2074 struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
2077 for (i
= 0; i
< leaf
->num_stored_in_leaf
; ++i
)
2079 if (leaf
->ranges
[i
].unit
== unit
2080 && ranges_overlap (low_pc
, high_pc
,
2081 leaf
->ranges
[i
].low_pc
,
2082 leaf
->ranges
[i
].high_pc
))
2084 if (low_pc
< leaf
->ranges
[i
].low_pc
)
2085 leaf
->ranges
[i
].low_pc
= low_pc
;
2086 if (high_pc
> leaf
->ranges
[i
].high_pc
)
2087 leaf
->ranges
[i
].high_pc
= high_pc
;
2092 is_full_leaf
= leaf
->num_stored_in_leaf
== trie
->num_room_in_leaf
;
2095 /* If we're a leaf with no more room and we're _not_ at the bottom,
2096 convert to an interior node. */
2097 if (is_full_leaf
&& trie_pc_bits
< VMA_BITS
)
2099 const struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
2102 trie
= bfd_zalloc (abfd
, sizeof (struct trie_interior
));
2105 is_full_leaf
= false;
2107 /* TODO: If we wanted to save a little more memory at the cost of
2108 complexity, we could have reused the old leaf node as one of the
2109 children of the new interior node, instead of throwing it away. */
2110 for (i
= 0; i
< leaf
->num_stored_in_leaf
; ++i
)
2112 if (!insert_arange_in_trie (abfd
, trie
, trie_pc
, trie_pc_bits
,
2113 leaf
->ranges
[i
].unit
, leaf
->ranges
[i
].low_pc
,
2114 leaf
->ranges
[i
].high_pc
))
2119 /* If we're a leaf with no more room and we _are_ at the bottom,
2120 we have no choice but to just make it larger. */
2123 const struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
2124 unsigned int new_room_in_leaf
= trie
->num_room_in_leaf
* 2;
2125 struct trie_leaf
*new_leaf
;
2126 size_t amt
= (sizeof (struct trie_leaf
)
2127 + ((new_room_in_leaf
- TRIE_LEAF_SIZE
)
2128 * sizeof (leaf
->ranges
[0])));
2129 new_leaf
= bfd_zalloc (abfd
, amt
);
2130 new_leaf
->head
.num_room_in_leaf
= new_room_in_leaf
;
2131 new_leaf
->num_stored_in_leaf
= leaf
->num_stored_in_leaf
;
2133 memcpy (new_leaf
->ranges
,
2135 leaf
->num_stored_in_leaf
* sizeof (leaf
->ranges
[0]));
2136 trie
= &new_leaf
->head
;
2137 is_full_leaf
= false;
2139 /* Now the insert below will go through. */
2142 /* If we're a leaf (now with room), we can just insert at the end. */
2143 if (trie
->num_room_in_leaf
> 0)
2145 struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
2147 unsigned int i
= leaf
->num_stored_in_leaf
++;
2148 leaf
->ranges
[i
].unit
= unit
;
2149 leaf
->ranges
[i
].low_pc
= low_pc
;
2150 leaf
->ranges
[i
].high_pc
= high_pc
;
2154 /* Now we are definitely an interior node, so recurse into all
2155 the relevant buckets. */
2157 /* Clamp the range to the current trie bucket. */
2158 clamped_low_pc
= low_pc
;
2159 clamped_high_pc
= high_pc
;
2160 if (trie_pc_bits
> 0)
2162 bfd_vma bucket_high_pc
=
2163 trie_pc
+ ((bfd_vma
) -1 >> trie_pc_bits
); /* Inclusive. */
2164 if (clamped_low_pc
< trie_pc
)
2165 clamped_low_pc
= trie_pc
;
2166 if (clamped_high_pc
> bucket_high_pc
)
2167 clamped_high_pc
= bucket_high_pc
;
2170 /* Insert the ranges in all buckets that it spans. */
2171 from_ch
= (clamped_low_pc
>> (VMA_BITS
- trie_pc_bits
- 8)) & 0xff;
2172 to_ch
= ((clamped_high_pc
- 1) >> (VMA_BITS
- trie_pc_bits
- 8)) & 0xff;
2173 for (ch
= from_ch
; ch
<= to_ch
; ++ch
)
2175 struct trie_interior
*interior
= (struct trie_interior
*) trie
;
2176 struct trie_node
*child
= interior
->children
[ch
];
2180 child
= alloc_trie_leaf (abfd
);
2184 bfd_vma bucket
= (bfd_vma
) ch
<< (VMA_BITS
- trie_pc_bits
- 8);
2185 child
= insert_arange_in_trie (abfd
,
2195 interior
->children
[ch
] = child
;
2203 arange_add (struct comp_unit
*unit
, struct arange
*first_arange
,
2204 struct trie_node
**trie_root
, bfd_vma low_pc
, bfd_vma high_pc
)
2206 struct arange
*arange
;
2208 /* Ignore empty ranges. */
2209 if (low_pc
== high_pc
)
2212 if (trie_root
!= NULL
)
2214 *trie_root
= insert_arange_in_trie (unit
->file
->bfd_ptr
,
2221 if (*trie_root
== NULL
)
2225 /* If the first arange is empty, use it. */
2226 if (first_arange
->high
== 0)
2228 first_arange
->low
= low_pc
;
2229 first_arange
->high
= high_pc
;
2233 /* Next see if we can cheaply extend an existing range. */
2234 arange
= first_arange
;
2237 if (low_pc
== arange
->high
)
2239 arange
->high
= high_pc
;
2242 if (high_pc
== arange
->low
)
2244 arange
->low
= low_pc
;
2247 arange
= arange
->next
;
2251 /* Need to allocate a new arange and insert it into the arange list.
2252 Order isn't significant, so just insert after the first arange. */
2253 arange
= (struct arange
*) bfd_alloc (unit
->abfd
, sizeof (*arange
));
2256 arange
->low
= low_pc
;
2257 arange
->high
= high_pc
;
2258 arange
->next
= first_arange
->next
;
2259 first_arange
->next
= arange
;
2263 /* Compare function for line sequences. */
2266 compare_sequences (const void* a
, const void* b
)
2268 const struct line_sequence
* seq1
= a
;
2269 const struct line_sequence
* seq2
= b
;
2271 /* Sort by low_pc as the primary key. */
2272 if (seq1
->low_pc
< seq2
->low_pc
)
2274 if (seq1
->low_pc
> seq2
->low_pc
)
2277 /* If low_pc values are equal, sort in reverse order of
2278 high_pc, so that the largest region comes first. */
2279 if (seq1
->last_line
->address
< seq2
->last_line
->address
)
2281 if (seq1
->last_line
->address
> seq2
->last_line
->address
)
2284 if (seq1
->last_line
->op_index
< seq2
->last_line
->op_index
)
2286 if (seq1
->last_line
->op_index
> seq2
->last_line
->op_index
)
2289 /* num_lines is initially an index, to make the sort stable. */
2290 if (seq1
->num_lines
< seq2
->num_lines
)
2292 if (seq1
->num_lines
> seq2
->num_lines
)
2297 /* Construct the line information table for quick lookup. */
2300 build_line_info_table (struct line_info_table
* table
,
2301 struct line_sequence
* seq
)
2304 struct line_info
**line_info_lookup
;
2305 struct line_info
*each_line
;
2306 unsigned int num_lines
;
2307 unsigned int line_index
;
2309 if (seq
->line_info_lookup
!= NULL
)
2312 /* Count the number of line information entries. We could do this while
2313 scanning the debug information, but some entries may be added via
2314 lcl_head without having a sequence handy to increment the number of
2317 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
2320 seq
->num_lines
= num_lines
;
2324 /* Allocate space for the line information lookup table. */
2325 amt
= sizeof (struct line_info
*) * num_lines
;
2326 line_info_lookup
= (struct line_info
**) bfd_alloc (table
->abfd
, amt
);
2327 seq
->line_info_lookup
= line_info_lookup
;
2328 if (line_info_lookup
== NULL
)
2331 /* Create the line information lookup table. */
2332 line_index
= num_lines
;
2333 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
2334 line_info_lookup
[--line_index
] = each_line
;
2336 BFD_ASSERT (line_index
== 0);
2340 /* Sort the line sequences for quick lookup. */
2343 sort_line_sequences (struct line_info_table
* table
)
2346 struct line_sequence
*sequences
;
2347 struct line_sequence
*seq
;
2349 unsigned int num_sequences
= table
->num_sequences
;
2350 bfd_vma last_high_pc
;
2352 if (num_sequences
== 0)
2355 /* Allocate space for an array of sequences. */
2356 amt
= sizeof (struct line_sequence
) * num_sequences
;
2357 sequences
= (struct line_sequence
*) bfd_alloc (table
->abfd
, amt
);
2358 if (sequences
== NULL
)
2361 /* Copy the linked list into the array, freeing the original nodes. */
2362 seq
= table
->sequences
;
2363 for (n
= 0; n
< num_sequences
; n
++)
2365 struct line_sequence
* last_seq
= seq
;
2368 sequences
[n
].low_pc
= seq
->low_pc
;
2369 sequences
[n
].prev_sequence
= NULL
;
2370 sequences
[n
].last_line
= seq
->last_line
;
2371 sequences
[n
].line_info_lookup
= NULL
;
2372 sequences
[n
].num_lines
= n
;
2373 seq
= seq
->prev_sequence
;
2376 BFD_ASSERT (seq
== NULL
);
2378 qsort (sequences
, n
, sizeof (struct line_sequence
), compare_sequences
);
2380 /* Make the list binary-searchable by trimming overlapping entries
2381 and removing nested entries. */
2383 last_high_pc
= sequences
[0].last_line
->address
;
2384 for (n
= 1; n
< table
->num_sequences
; n
++)
2386 if (sequences
[n
].low_pc
< last_high_pc
)
2388 if (sequences
[n
].last_line
->address
<= last_high_pc
)
2389 /* Skip nested entries. */
2392 /* Trim overlapping entries. */
2393 sequences
[n
].low_pc
= last_high_pc
;
2395 last_high_pc
= sequences
[n
].last_line
->address
;
2396 if (n
> num_sequences
)
2398 /* Close up the gap. */
2399 sequences
[num_sequences
].low_pc
= sequences
[n
].low_pc
;
2400 sequences
[num_sequences
].last_line
= sequences
[n
].last_line
;
2405 table
->sequences
= sequences
;
2406 table
->num_sequences
= num_sequences
;
2410 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
2413 line_info_add_include_dir (struct line_info_table
*table
, char *cur_dir
)
2415 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
2420 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
2421 amt
*= sizeof (char *);
2423 tmp
= (char **) bfd_realloc (table
->dirs
, amt
);
2429 table
->dirs
[table
->num_dirs
++] = cur_dir
;
2434 line_info_add_include_dir_stub (struct line_info_table
*table
, char *cur_dir
,
2435 unsigned int dir ATTRIBUTE_UNUSED
,
2436 unsigned int xtime ATTRIBUTE_UNUSED
,
2437 unsigned int size ATTRIBUTE_UNUSED
)
2439 return line_info_add_include_dir (table
, cur_dir
);
2442 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
2445 line_info_add_file_name (struct line_info_table
*table
, char *cur_file
,
2446 unsigned int dir
, unsigned int xtime
,
2449 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
2451 struct fileinfo
*tmp
;
2454 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
2455 amt
*= sizeof (struct fileinfo
);
2457 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
2463 table
->files
[table
->num_files
].name
= cur_file
;
2464 table
->files
[table
->num_files
].dir
= dir
;
2465 table
->files
[table
->num_files
].time
= xtime
;
2466 table
->files
[table
->num_files
].size
= size
;
2471 /* Read directory or file name entry format, starting with byte of
2472 format count entries, ULEB128 pairs of entry formats, ULEB128 of
2473 entries count and the entries themselves in the described entry
2477 read_formatted_entries (struct comp_unit
*unit
, bfd_byte
**bufp
,
2478 bfd_byte
*buf_end
, struct line_info_table
*table
,
2479 bool (*callback
) (struct line_info_table
*table
,
2485 bfd
*abfd
= unit
->abfd
;
2486 bfd_byte format_count
, formati
;
2487 bfd_vma data_count
, datai
;
2488 bfd_byte
*buf
= *bufp
;
2489 bfd_byte
*format_header_data
;
2491 format_count
= read_1_byte (abfd
, &buf
, buf_end
);
2492 format_header_data
= buf
;
2493 for (formati
= 0; formati
< format_count
; formati
++)
2495 _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2496 _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2499 data_count
= _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2500 if (format_count
== 0 && data_count
!= 0)
2502 _bfd_error_handler (_("DWARF error: zero format count"));
2503 bfd_set_error (bfd_error_bad_value
);
2507 /* PR 22210. Paranoia check. Don't bother running the loop
2508 if we know that we are going to run out of buffer. */
2509 if (data_count
> (bfd_vma
) (buf_end
- buf
))
2512 (_("DWARF error: data count (%" PRIx64
") larger than buffer size"),
2513 (uint64_t) data_count
);
2514 bfd_set_error (bfd_error_bad_value
);
2518 for (datai
= 0; datai
< data_count
; datai
++)
2520 bfd_byte
*format
= format_header_data
;
2523 memset (&fe
, 0, sizeof fe
);
2524 for (formati
= 0; formati
< format_count
; formati
++)
2526 bfd_vma content_type
, form
;
2528 char **stringp
= &string_trash
;
2529 unsigned int uint_trash
, *uintp
= &uint_trash
;
2530 struct attribute attr
;
2532 content_type
= _bfd_safe_read_leb128 (abfd
, &format
, false, buf_end
);
2533 switch (content_type
)
2538 case DW_LNCT_directory_index
:
2541 case DW_LNCT_timestamp
:
2551 (_("DWARF error: unknown format content type %" PRIu64
),
2552 (uint64_t) content_type
);
2553 bfd_set_error (bfd_error_bad_value
);
2557 form
= _bfd_safe_read_leb128 (abfd
, &format
, false, buf_end
);
2558 buf
= read_attribute_value (&attr
, form
, 0, unit
, buf
, buf_end
);
2563 case DW_FORM_string
:
2564 case DW_FORM_line_strp
:
2570 *stringp
= attr
.u
.str
;
2578 *uintp
= attr
.u
.val
;
2581 case DW_FORM_data16
:
2582 /* MD5 data is in the attr.blk, but we are ignoring those. */
2587 /* Skip the first "zero entry", which is the compilation dir/file. */
2589 if (!callback (table
, fe
.name
, fe
.dir
, fe
.time
, fe
.size
))
2597 /* Decode the line number information for UNIT. */
2599 static struct line_info_table
*
2600 decode_line_info (struct comp_unit
*unit
)
2602 bfd
*abfd
= unit
->abfd
;
2603 struct dwarf2_debug
*stash
= unit
->stash
;
2604 struct dwarf2_debug_file
*file
= unit
->file
;
2605 struct line_info_table
* table
;
2608 struct line_head lh
;
2609 unsigned int i
, offset_size
;
2610 char *cur_file
, *cur_dir
;
2611 unsigned char op_code
, extended_op
, adj_opcode
;
2612 unsigned int exop_len
;
2615 if (unit
->line_offset
== 0 && file
->line_table
)
2616 return file
->line_table
;
2618 if (! read_section (abfd
, &stash
->debug_sections
[debug_line
],
2619 file
->syms
, unit
->line_offset
,
2620 &file
->dwarf_line_buffer
, &file
->dwarf_line_size
))
2623 if (file
->dwarf_line_size
< 16)
2626 (_("DWARF error: line info section is too small (%" PRId64
")"),
2627 (int64_t) file
->dwarf_line_size
);
2628 bfd_set_error (bfd_error_bad_value
);
2631 line_ptr
= file
->dwarf_line_buffer
+ unit
->line_offset
;
2632 line_end
= file
->dwarf_line_buffer
+ file
->dwarf_line_size
;
2634 /* Read in the prologue. */
2635 lh
.total_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2637 if (lh
.total_length
== 0xffffffff)
2639 lh
.total_length
= read_8_bytes (abfd
, &line_ptr
, line_end
);
2642 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
2644 /* Handle (non-standard) 64-bit DWARF2 formats. */
2645 lh
.total_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2649 if (lh
.total_length
> (size_t) (line_end
- line_ptr
))
2652 /* xgettext: c-format */
2653 (_("DWARF error: line info data is bigger (%#" PRIx64
")"
2654 " than the space remaining in the section (%#lx)"),
2655 (uint64_t) lh
.total_length
, (unsigned long) (line_end
- line_ptr
));
2656 bfd_set_error (bfd_error_bad_value
);
2660 line_end
= line_ptr
+ lh
.total_length
;
2662 lh
.version
= read_2_bytes (abfd
, &line_ptr
, line_end
);
2663 if (lh
.version
< 2 || lh
.version
> 5)
2666 (_("DWARF error: unhandled .debug_line version %d"), lh
.version
);
2667 bfd_set_error (bfd_error_bad_value
);
2671 if (line_ptr
+ offset_size
+ (lh
.version
>= 5 ? 8 : (lh
.version
>= 4 ? 6 : 5))
2675 (_("DWARF error: ran out of room reading prologue"));
2676 bfd_set_error (bfd_error_bad_value
);
2680 if (lh
.version
>= 5)
2682 unsigned int segment_selector_size
;
2684 /* Skip address size. */
2685 read_1_byte (abfd
, &line_ptr
, line_end
);
2687 segment_selector_size
= read_1_byte (abfd
, &line_ptr
, line_end
);
2688 if (segment_selector_size
!= 0)
2691 (_("DWARF error: line info unsupported segment selector size %u"),
2692 segment_selector_size
);
2693 bfd_set_error (bfd_error_bad_value
);
2698 if (offset_size
== 4)
2699 lh
.prologue_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2701 lh
.prologue_length
= read_8_bytes (abfd
, &line_ptr
, line_end
);
2703 lh
.minimum_instruction_length
= read_1_byte (abfd
, &line_ptr
, line_end
);
2705 if (lh
.version
>= 4)
2706 lh
.maximum_ops_per_insn
= read_1_byte (abfd
, &line_ptr
, line_end
);
2708 lh
.maximum_ops_per_insn
= 1;
2710 if (lh
.maximum_ops_per_insn
== 0)
2713 (_("DWARF error: invalid maximum operations per instruction"));
2714 bfd_set_error (bfd_error_bad_value
);
2718 lh
.default_is_stmt
= read_1_byte (abfd
, &line_ptr
, line_end
);
2719 lh
.line_base
= read_1_signed_byte (abfd
, &line_ptr
, line_end
);
2720 lh
.line_range
= read_1_byte (abfd
, &line_ptr
, line_end
);
2721 lh
.opcode_base
= read_1_byte (abfd
, &line_ptr
, line_end
);
2723 if (line_ptr
+ (lh
.opcode_base
- 1) >= line_end
)
2725 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2726 bfd_set_error (bfd_error_bad_value
);
2730 amt
= lh
.opcode_base
* sizeof (unsigned char);
2731 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
2733 lh
.standard_opcode_lengths
[0] = 1;
2735 for (i
= 1; i
< lh
.opcode_base
; ++i
)
2736 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, &line_ptr
, line_end
);
2738 amt
= sizeof (struct line_info_table
);
2739 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
2743 table
->comp_dir
= unit
->comp_dir
;
2745 table
->num_files
= 0;
2746 table
->files
= NULL
;
2748 table
->num_dirs
= 0;
2751 table
->num_sequences
= 0;
2752 table
->sequences
= NULL
;
2754 table
->lcl_head
= NULL
;
2756 if (lh
.version
>= 5)
2758 /* Read directory table. */
2759 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2760 line_info_add_include_dir_stub
))
2763 /* Read file name table. */
2764 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2765 line_info_add_file_name
))
2770 /* Read directory table. */
2771 while ((cur_dir
= read_string (&line_ptr
, line_end
)) != NULL
)
2773 if (!line_info_add_include_dir (table
, cur_dir
))
2777 /* Read file name table. */
2778 while ((cur_file
= read_string (&line_ptr
, line_end
)) != NULL
)
2780 unsigned int dir
, xtime
, size
;
2782 dir
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2783 xtime
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2784 size
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2786 if (!line_info_add_file_name (table
, cur_file
, dir
, xtime
, size
))
2791 /* Read the statement sequences until there's nothing left. */
2792 while (line_ptr
< line_end
)
2794 /* State machine registers. */
2795 bfd_vma address
= 0;
2796 unsigned char op_index
= 0;
2797 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
2798 unsigned int line
= 1;
2799 unsigned int column
= 0;
2800 unsigned int discriminator
= 0;
2801 int is_stmt
= lh
.default_is_stmt
;
2802 int end_sequence
= 0;
2803 unsigned int dir
, xtime
, size
;
2804 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2805 compilers generate address sequences that are wildly out of
2806 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2807 for ia64-Linux). Thus, to determine the low and high
2808 address, we must compare on every DW_LNS_copy, etc. */
2809 bfd_vma low_pc
= (bfd_vma
) -1;
2810 bfd_vma high_pc
= 0;
2812 /* Decode the table. */
2813 while (!end_sequence
&& line_ptr
< line_end
)
2815 op_code
= read_1_byte (abfd
, &line_ptr
, line_end
);
2817 if (op_code
>= lh
.opcode_base
)
2819 /* Special operand. */
2820 adj_opcode
= op_code
- lh
.opcode_base
;
2821 if (lh
.line_range
== 0)
2823 if (lh
.maximum_ops_per_insn
== 1)
2824 address
+= (adj_opcode
/ lh
.line_range
2825 * lh
.minimum_instruction_length
);
2828 address
+= ((op_index
+ adj_opcode
/ lh
.line_range
)
2829 / lh
.maximum_ops_per_insn
2830 * lh
.minimum_instruction_length
);
2831 op_index
= ((op_index
+ adj_opcode
/ lh
.line_range
)
2832 % lh
.maximum_ops_per_insn
);
2834 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
2835 /* Append row to matrix using current values. */
2836 if (!add_line_info (table
, address
, op_index
, filename
,
2837 line
, column
, discriminator
, 0))
2840 if (address
< low_pc
)
2842 if (address
> high_pc
)
2845 else switch (op_code
)
2847 case DW_LNS_extended_op
:
2848 exop_len
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2850 extended_op
= read_1_byte (abfd
, &line_ptr
, line_end
);
2852 switch (extended_op
)
2854 case DW_LNE_end_sequence
:
2856 if (!add_line_info (table
, address
, op_index
, filename
, line
,
2857 column
, discriminator
, end_sequence
))
2860 if (address
< low_pc
)
2862 if (address
> high_pc
)
2864 if (!arange_add (unit
, &unit
->arange
, &unit
->file
->trie_root
,
2868 case DW_LNE_set_address
:
2869 address
= read_address (unit
, &line_ptr
, line_end
);
2872 case DW_LNE_define_file
:
2873 cur_file
= read_string (&line_ptr
, line_end
);
2874 dir
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2876 xtime
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2878 size
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2880 if (!line_info_add_file_name (table
, cur_file
, dir
,
2884 case DW_LNE_set_discriminator
:
2885 discriminator
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2888 case DW_LNE_HP_source_file_correlation
:
2889 line_ptr
+= exop_len
- 1;
2893 (_("DWARF error: mangled line number section"));
2894 bfd_set_error (bfd_error_bad_value
);
2901 if (!add_line_info (table
, address
, op_index
,
2902 filename
, line
, column
, discriminator
, 0))
2905 if (address
< low_pc
)
2907 if (address
> high_pc
)
2910 case DW_LNS_advance_pc
:
2911 if (lh
.maximum_ops_per_insn
== 1)
2912 address
+= (lh
.minimum_instruction_length
2913 * _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2917 bfd_vma adjust
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2919 address
= ((op_index
+ adjust
) / lh
.maximum_ops_per_insn
2920 * lh
.minimum_instruction_length
);
2921 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2924 case DW_LNS_advance_line
:
2925 line
+= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2928 case DW_LNS_set_file
:
2930 unsigned int filenum
;
2932 /* The file and directory tables are 0
2933 based, the references are 1 based. */
2934 filenum
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2937 filename
= concat_filename (table
, filenum
);
2940 case DW_LNS_set_column
:
2941 column
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2944 case DW_LNS_negate_stmt
:
2945 is_stmt
= (!is_stmt
);
2947 case DW_LNS_set_basic_block
:
2949 case DW_LNS_const_add_pc
:
2950 if (lh
.line_range
== 0)
2952 if (lh
.maximum_ops_per_insn
== 1)
2953 address
+= (lh
.minimum_instruction_length
2954 * ((255 - lh
.opcode_base
) / lh
.line_range
));
2957 bfd_vma adjust
= ((255 - lh
.opcode_base
) / lh
.line_range
);
2958 address
+= (lh
.minimum_instruction_length
2959 * ((op_index
+ adjust
)
2960 / lh
.maximum_ops_per_insn
));
2961 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2964 case DW_LNS_fixed_advance_pc
:
2965 address
+= read_2_bytes (abfd
, &line_ptr
, line_end
);
2969 /* Unknown standard opcode, ignore it. */
2970 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
2971 (void) _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2980 if (unit
->line_offset
== 0)
2981 file
->line_table
= table
;
2982 if (sort_line_sequences (table
))
2986 while (table
->sequences
!= NULL
)
2988 struct line_sequence
* seq
= table
->sequences
;
2989 table
->sequences
= table
->sequences
->prev_sequence
;
2992 free (table
->files
);
2997 /* If ADDR is within TABLE set the output parameters and return TRUE,
2998 otherwise set *FILENAME_PTR to NULL and return FALSE.
2999 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
3000 are pointers to the objects to be filled in. */
3003 lookup_address_in_line_info_table (struct line_info_table
*table
,
3005 const char **filename_ptr
,
3006 unsigned int *linenumber_ptr
,
3007 unsigned int *discriminator_ptr
)
3009 struct line_sequence
*seq
= NULL
;
3010 struct line_info
*info
;
3013 /* Binary search the array of sequences. */
3015 high
= table
->num_sequences
;
3018 mid
= (low
+ high
) / 2;
3019 seq
= &table
->sequences
[mid
];
3020 if (addr
< seq
->low_pc
)
3022 else if (addr
>= seq
->last_line
->address
)
3028 /* Check for a valid sequence. */
3029 if (!seq
|| addr
< seq
->low_pc
|| addr
>= seq
->last_line
->address
)
3032 if (!build_line_info_table (table
, seq
))
3035 /* Binary search the array of line information. */
3037 high
= seq
->num_lines
;
3041 mid
= (low
+ high
) / 2;
3042 info
= seq
->line_info_lookup
[mid
];
3043 if (addr
< info
->address
)
3045 else if (addr
>= seq
->line_info_lookup
[mid
+ 1]->address
)
3051 /* Check for a valid line information entry. */
3053 && addr
>= info
->address
3054 && addr
< seq
->line_info_lookup
[mid
+ 1]->address
3055 && !(info
->end_sequence
|| info
== seq
->last_line
))
3057 *filename_ptr
= info
->filename
;
3058 *linenumber_ptr
= info
->line
;
3059 if (discriminator_ptr
)
3060 *discriminator_ptr
= info
->discriminator
;
3065 *filename_ptr
= NULL
;
3069 /* Read in the .debug_ranges section for future reference. */
3072 read_debug_ranges (struct comp_unit
* unit
)
3074 struct dwarf2_debug
*stash
= unit
->stash
;
3075 struct dwarf2_debug_file
*file
= unit
->file
;
3077 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_ranges
],
3079 &file
->dwarf_ranges_buffer
, &file
->dwarf_ranges_size
);
3082 /* Read in the .debug_rnglists section for future reference. */
3085 read_debug_rnglists (struct comp_unit
* unit
)
3087 struct dwarf2_debug
*stash
= unit
->stash
;
3088 struct dwarf2_debug_file
*file
= unit
->file
;
3090 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_rnglists
],
3092 &file
->dwarf_rnglists_buffer
, &file
->dwarf_rnglists_size
);
3095 /* Function table functions. */
3098 compare_lookup_funcinfos (const void * a
, const void * b
)
3100 const struct lookup_funcinfo
* lookup1
= a
;
3101 const struct lookup_funcinfo
* lookup2
= b
;
3103 if (lookup1
->low_addr
< lookup2
->low_addr
)
3105 if (lookup1
->low_addr
> lookup2
->low_addr
)
3107 if (lookup1
->high_addr
< lookup2
->high_addr
)
3109 if (lookup1
->high_addr
> lookup2
->high_addr
)
3112 if (lookup1
->idx
< lookup2
->idx
)
3114 if (lookup1
->idx
> lookup2
->idx
)
3120 build_lookup_funcinfo_table (struct comp_unit
* unit
)
3122 struct lookup_funcinfo
*lookup_funcinfo_table
= unit
->lookup_funcinfo_table
;
3123 unsigned int number_of_functions
= unit
->number_of_functions
;
3124 struct funcinfo
*each
;
3125 struct lookup_funcinfo
*entry
;
3127 struct arange
*range
;
3128 bfd_vma low_addr
, high_addr
;
3130 if (lookup_funcinfo_table
|| number_of_functions
== 0)
3133 /* Create the function info lookup table. */
3134 lookup_funcinfo_table
= (struct lookup_funcinfo
*)
3135 bfd_malloc (number_of_functions
* sizeof (struct lookup_funcinfo
));
3136 if (lookup_funcinfo_table
== NULL
)
3139 /* Populate the function info lookup table. */
3140 func_index
= number_of_functions
;
3141 for (each
= unit
->function_table
; each
; each
= each
->prev_func
)
3143 entry
= &lookup_funcinfo_table
[--func_index
];
3144 entry
->funcinfo
= each
;
3145 entry
->idx
= func_index
;
3147 /* Calculate the lowest and highest address for this function entry. */
3148 low_addr
= entry
->funcinfo
->arange
.low
;
3149 high_addr
= entry
->funcinfo
->arange
.high
;
3151 for (range
= entry
->funcinfo
->arange
.next
; range
; range
= range
->next
)
3153 if (range
->low
< low_addr
)
3154 low_addr
= range
->low
;
3155 if (range
->high
> high_addr
)
3156 high_addr
= range
->high
;
3159 entry
->low_addr
= low_addr
;
3160 entry
->high_addr
= high_addr
;
3163 BFD_ASSERT (func_index
== 0);
3165 /* Sort the function by address. */
3166 qsort (lookup_funcinfo_table
,
3167 number_of_functions
,
3168 sizeof (struct lookup_funcinfo
),
3169 compare_lookup_funcinfos
);
3171 /* Calculate the high watermark for each function in the lookup table. */
3172 high_addr
= lookup_funcinfo_table
[0].high_addr
;
3173 for (func_index
= 1; func_index
< number_of_functions
; func_index
++)
3175 entry
= &lookup_funcinfo_table
[func_index
];
3176 if (entry
->high_addr
> high_addr
)
3177 high_addr
= entry
->high_addr
;
3179 entry
->high_addr
= high_addr
;
3182 unit
->lookup_funcinfo_table
= lookup_funcinfo_table
;
3186 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
3187 TRUE. Note that we need to find the function that has the smallest range
3188 that contains ADDR, to handle inlined functions without depending upon
3189 them being ordered in TABLE by increasing range. */
3192 lookup_address_in_function_table (struct comp_unit
*unit
,
3194 struct funcinfo
**function_ptr
)
3196 unsigned int number_of_functions
= unit
->number_of_functions
;
3197 struct lookup_funcinfo
* lookup_funcinfo
= NULL
;
3198 struct funcinfo
* funcinfo
= NULL
;
3199 struct funcinfo
* best_fit
= NULL
;
3200 bfd_vma best_fit_len
= 0;
3201 bfd_size_type low
, high
, mid
, first
;
3202 struct arange
*arange
;
3204 if (number_of_functions
== 0)
3207 if (!build_lookup_funcinfo_table (unit
))
3210 if (unit
->lookup_funcinfo_table
[number_of_functions
- 1].high_addr
< addr
)
3213 /* Find the first function in the lookup table which may contain the
3214 specified address. */
3216 high
= number_of_functions
;
3220 mid
= (low
+ high
) / 2;
3221 lookup_funcinfo
= &unit
->lookup_funcinfo_table
[mid
];
3222 if (addr
< lookup_funcinfo
->low_addr
)
3224 else if (addr
>= lookup_funcinfo
->high_addr
)
3230 /* Find the 'best' match for the address. The prior algorithm defined the
3231 best match as the function with the smallest address range containing
3232 the specified address. This definition should probably be changed to the
3233 innermost inline routine containing the address, but right now we want
3234 to get the same results we did before. */
3235 while (first
< number_of_functions
)
3237 if (addr
< unit
->lookup_funcinfo_table
[first
].low_addr
)
3239 funcinfo
= unit
->lookup_funcinfo_table
[first
].funcinfo
;
3241 for (arange
= &funcinfo
->arange
; arange
; arange
= arange
->next
)
3243 if (addr
< arange
->low
|| addr
>= arange
->high
)
3247 || arange
->high
- arange
->low
< best_fit_len
3248 /* The following comparison is designed to return the same
3249 match as the previous algorithm for routines which have the
3250 same best fit length. */
3251 || (arange
->high
- arange
->low
== best_fit_len
3252 && funcinfo
> best_fit
))
3254 best_fit
= funcinfo
;
3255 best_fit_len
= arange
->high
- arange
->low
;
3265 *function_ptr
= best_fit
;
3269 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
3270 and LINENUMBER_PTR, and return TRUE. */
3273 lookup_symbol_in_function_table (struct comp_unit
*unit
,
3276 const char **filename_ptr
,
3277 unsigned int *linenumber_ptr
)
3279 struct funcinfo
* each_func
;
3280 struct funcinfo
* best_fit
= NULL
;
3281 bfd_vma best_fit_len
= 0;
3282 struct arange
*arange
;
3283 const char *name
= bfd_asymbol_name (sym
);
3284 asection
*sec
= bfd_asymbol_section (sym
);
3286 for (each_func
= unit
->function_table
;
3288 each_func
= each_func
->prev_func
)
3290 for (arange
= &each_func
->arange
;
3292 arange
= arange
->next
)
3294 if ((!each_func
->sec
|| each_func
->sec
== sec
)
3295 && addr
>= arange
->low
3296 && addr
< arange
->high
3298 && strcmp (name
, each_func
->name
) == 0
3300 || arange
->high
- arange
->low
< best_fit_len
))
3302 best_fit
= each_func
;
3303 best_fit_len
= arange
->high
- arange
->low
;
3310 best_fit
->sec
= sec
;
3311 *filename_ptr
= best_fit
->file
;
3312 *linenumber_ptr
= best_fit
->line
;
3319 /* Variable table functions. */
3321 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
3322 LINENUMBER_PTR, and return TRUE. */
3325 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
3328 const char **filename_ptr
,
3329 unsigned int *linenumber_ptr
)
3331 const char *name
= bfd_asymbol_name (sym
);
3332 asection
*sec
= bfd_asymbol_section (sym
);
3333 struct varinfo
* each
;
3335 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
3337 && each
->file
!= NULL
3338 && each
->name
!= NULL
3339 && each
->addr
== addr
3340 && (!each
->sec
|| each
->sec
== sec
)
3341 && strcmp (name
, each
->name
) == 0)
3347 *filename_ptr
= each
->file
;
3348 *linenumber_ptr
= each
->line
;
3355 static struct comp_unit
*stash_comp_unit (struct dwarf2_debug
*,
3356 struct dwarf2_debug_file
*);
3357 static bool comp_unit_maybe_decode_line_info (struct comp_unit
*);
3360 find_abstract_instance (struct comp_unit
*unit
,
3361 struct attribute
*attr_ptr
,
3362 unsigned int recur_count
,
3365 char **filename_ptr
,
3366 int *linenumber_ptr
)
3368 bfd
*abfd
= unit
->abfd
;
3369 bfd_byte
*info_ptr
= NULL
;
3370 bfd_byte
*info_ptr_end
;
3371 unsigned int abbrev_number
, i
;
3372 struct abbrev_info
*abbrev
;
3373 uint64_t die_ref
= attr_ptr
->u
.val
;
3374 struct attribute attr
;
3375 const char *name
= NULL
;
3377 if (recur_count
== 100)
3380 (_("DWARF error: abstract instance recursion detected"));
3381 bfd_set_error (bfd_error_bad_value
);
3385 /* DW_FORM_ref_addr can reference an entry in a different CU. It
3386 is an offset from the .debug_info section, not the current CU. */
3387 if (attr_ptr
->form
== DW_FORM_ref_addr
)
3389 /* We only support DW_FORM_ref_addr within the same file, so
3390 any relocations should be resolved already. Check this by
3391 testing for a zero die_ref; There can't be a valid reference
3392 to the header of a .debug_info section.
3393 DW_FORM_ref_addr is an offset relative to .debug_info.
3394 Normally when using the GNU linker this is accomplished by
3395 emitting a symbolic reference to a label, because .debug_info
3396 sections are linked at zero. When there are multiple section
3397 groups containing .debug_info, as there might be in a
3398 relocatable object file, it would be reasonable to assume that
3399 a symbolic reference to a label in any .debug_info section
3400 might be used. Since we lay out multiple .debug_info
3401 sections at non-zero VMAs (see place_sections), and read
3402 them contiguously into dwarf_info_buffer, that means the
3403 reference is relative to dwarf_info_buffer. */
3406 info_ptr
= unit
->file
->dwarf_info_buffer
;
3407 info_ptr_end
= info_ptr
+ unit
->file
->dwarf_info_size
;
3408 total
= info_ptr_end
- info_ptr
;
3411 else if (die_ref
>= total
)
3414 (_("DWARF error: invalid abstract instance DIE ref"));
3415 bfd_set_error (bfd_error_bad_value
);
3418 info_ptr
+= die_ref
;
3420 else if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3422 bool first_time
= unit
->stash
->alt
.dwarf_info_buffer
== NULL
;
3424 info_ptr
= read_alt_indirect_ref (unit
, die_ref
);
3426 unit
->stash
->alt
.info_ptr
= unit
->stash
->alt
.dwarf_info_buffer
;
3427 if (info_ptr
== NULL
)
3430 (_("DWARF error: unable to read alt ref %" PRIu64
),
3431 (uint64_t) die_ref
);
3432 bfd_set_error (bfd_error_bad_value
);
3435 info_ptr_end
= (unit
->stash
->alt
.dwarf_info_buffer
3436 + unit
->stash
->alt
.dwarf_info_size
);
3437 if (unit
->stash
->alt
.all_comp_units
)
3438 unit
= unit
->stash
->alt
.all_comp_units
;
3441 if (attr_ptr
->form
== DW_FORM_ref_addr
3442 || attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3444 /* Now find the CU containing this pointer. */
3445 if (info_ptr
>= unit
->info_ptr_unit
&& info_ptr
< unit
->end_ptr
)
3446 info_ptr_end
= unit
->end_ptr
;
3449 /* Check other CUs to see if they contain the abbrev. */
3450 struct comp_unit
*u
;
3452 for (u
= unit
->prev_unit
; u
!= NULL
; u
= u
->prev_unit
)
3453 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3457 for (u
= unit
->next_unit
; u
!= NULL
; u
= u
->next_unit
)
3458 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3461 if (attr_ptr
->form
== DW_FORM_ref_addr
)
3464 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->f
);
3467 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3472 if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3475 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->alt
);
3478 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3486 (_("DWARF error: unable to locate abstract instance DIE ref %"
3487 PRIu64
), (uint64_t) die_ref
);
3488 bfd_set_error (bfd_error_bad_value
);
3492 info_ptr_end
= unit
->end_ptr
;
3497 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
3498 DW_FORM_ref_udata. These are all references relative to the
3499 start of the current CU. */
3502 info_ptr
= unit
->info_ptr_unit
;
3503 info_ptr_end
= unit
->end_ptr
;
3504 total
= info_ptr_end
- info_ptr
;
3505 if (!die_ref
|| die_ref
>= total
)
3508 (_("DWARF error: invalid abstract instance DIE ref"));
3509 bfd_set_error (bfd_error_bad_value
);
3512 info_ptr
+= die_ref
;
3515 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3516 false, info_ptr_end
);
3519 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3523 (_("DWARF error: could not find abbrev number %u"), abbrev_number
);
3524 bfd_set_error (bfd_error_bad_value
);
3529 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3531 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
,
3532 info_ptr
, info_ptr_end
);
3533 if (info_ptr
== NULL
)
3538 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3540 if (name
== NULL
&& is_str_form (&attr
))
3543 if (non_mangled (unit
->lang
))
3547 case DW_AT_specification
:
3548 if (is_int_form (&attr
)
3549 && !find_abstract_instance (unit
, &attr
, recur_count
+ 1,
3551 filename_ptr
, linenumber_ptr
))
3554 case DW_AT_linkage_name
:
3555 case DW_AT_MIPS_linkage_name
:
3556 /* PR 16949: Corrupt debug info can place
3557 non-string forms into these attributes. */
3558 if (is_str_form (&attr
))
3564 case DW_AT_decl_file
:
3565 if (!comp_unit_maybe_decode_line_info (unit
))
3567 if (is_int_form (&attr
))
3568 *filename_ptr
= concat_filename (unit
->line_table
,
3571 case DW_AT_decl_line
:
3572 if (is_int_form (&attr
))
3573 *linenumber_ptr
= attr
.u
.val
;
3586 read_ranges (struct comp_unit
*unit
, struct arange
*arange
,
3587 struct trie_node
**trie_root
, uint64_t offset
)
3589 bfd_byte
*ranges_ptr
;
3590 bfd_byte
*ranges_end
;
3591 bfd_vma base_address
= unit
->base_address
;
3593 if (! unit
->file
->dwarf_ranges_buffer
)
3595 if (! read_debug_ranges (unit
))
3599 if (offset
> unit
->file
->dwarf_ranges_size
)
3601 ranges_ptr
= unit
->file
->dwarf_ranges_buffer
+ offset
;
3602 ranges_end
= unit
->file
->dwarf_ranges_buffer
+ unit
->file
->dwarf_ranges_size
;
3609 /* PR 17512: file: 62cada7d. */
3610 if (2u * unit
->addr_size
> (size_t) (ranges_end
- ranges_ptr
))
3613 low_pc
= read_address (unit
, &ranges_ptr
, ranges_end
);
3614 high_pc
= read_address (unit
, &ranges_ptr
, ranges_end
);
3616 if (low_pc
== 0 && high_pc
== 0)
3618 if (low_pc
== -1UL && high_pc
!= -1UL)
3619 base_address
= high_pc
;
3622 if (!arange_add (unit
, arange
, trie_root
,
3623 base_address
+ low_pc
, base_address
+ high_pc
))
3631 read_rnglists (struct comp_unit
*unit
, struct arange
*arange
,
3632 struct trie_node
**trie_root
, uint64_t offset
)
3636 bfd_vma base_address
= unit
->base_address
;
3639 bfd
*abfd
= unit
->abfd
;
3641 if (! unit
->file
->dwarf_rnglists_buffer
)
3643 if (! read_debug_rnglists (unit
))
3647 rngs_ptr
= unit
->file
->dwarf_rnglists_buffer
+ offset
;
3648 if (rngs_ptr
< unit
->file
->dwarf_rnglists_buffer
)
3650 rngs_end
= unit
->file
->dwarf_rnglists_buffer
;
3651 rngs_end
+= unit
->file
->dwarf_rnglists_size
;
3655 enum dwarf_range_list_entry rlet
;
3657 if (rngs_ptr
>= rngs_end
)
3660 rlet
= read_1_byte (abfd
, &rngs_ptr
, rngs_end
);
3664 case DW_RLE_end_of_list
:
3667 case DW_RLE_base_address
:
3668 if (unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3670 base_address
= read_address (unit
, &rngs_ptr
, rngs_end
);
3673 case DW_RLE_start_length
:
3674 if (unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3676 low_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3678 high_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3682 case DW_RLE_offset_pair
:
3683 low_pc
= base_address
;
3684 low_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3686 high_pc
= base_address
;
3687 high_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3691 case DW_RLE_start_end
:
3692 if (2u * unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3694 low_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3695 high_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3698 /* TODO x-variants need .debug_addr support used for split-dwarf. */
3699 case DW_RLE_base_addressx
:
3700 case DW_RLE_startx_endx
:
3701 case DW_RLE_startx_length
:
3706 if (!arange_add (unit
, arange
, trie_root
, low_pc
, high_pc
))
3712 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
,
3713 struct trie_node
**trie_root
, uint64_t offset
)
3715 if (unit
->version
<= 4)
3716 return read_ranges (unit
, arange
, trie_root
, offset
);
3718 return read_rnglists (unit
, arange
, trie_root
, offset
);
3721 static struct funcinfo
*
3722 lookup_func_by_offset (uint64_t offset
, struct funcinfo
* table
)
3724 for (; table
!= NULL
; table
= table
->prev_func
)
3725 if (table
->unit_offset
== offset
)
3730 static struct varinfo
*
3731 lookup_var_by_offset (uint64_t offset
, struct varinfo
* table
)
3735 if (table
->unit_offset
== offset
)
3737 table
= table
->prev_var
;
3744 /* DWARF2 Compilation unit functions. */
3746 static struct funcinfo
*
3747 reverse_funcinfo_list (struct funcinfo
*head
)
3749 struct funcinfo
*rhead
;
3750 struct funcinfo
*temp
;
3752 for (rhead
= NULL
; head
; head
= temp
)
3754 temp
= head
->prev_func
;
3755 head
->prev_func
= rhead
;
3761 static struct varinfo
*
3762 reverse_varinfo_list (struct varinfo
*head
)
3764 struct varinfo
*rhead
;
3765 struct varinfo
*temp
;
3767 for (rhead
= NULL
; head
; head
= temp
)
3769 temp
= head
->prev_var
;
3770 head
->prev_var
= rhead
;
3776 /* Scan over each die in a comp. unit looking for functions to add
3777 to the function table and variables to the variable table. */
3780 scan_unit_for_symbols (struct comp_unit
*unit
)
3782 bfd
*abfd
= unit
->abfd
;
3783 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
3784 bfd_byte
*info_ptr_end
= unit
->end_ptr
;
3785 int nesting_level
= 0;
3786 struct nest_funcinfo
3788 struct funcinfo
*func
;
3790 int nested_funcs_size
;
3791 struct funcinfo
*last_func
;
3792 struct varinfo
*last_var
;
3794 /* Maintain a stack of in-scope functions and inlined functions, which we
3795 can use to set the caller_func field. */
3796 nested_funcs_size
= 32;
3797 nested_funcs
= (struct nest_funcinfo
*)
3798 bfd_malloc (nested_funcs_size
* sizeof (*nested_funcs
));
3799 if (nested_funcs
== NULL
)
3801 nested_funcs
[nesting_level
].func
= 0;
3803 /* PR 27484: We must scan the DIEs twice. The first time we look for
3804 function and variable tags and accumulate them into their respective
3805 tables. The second time through we process the attributes of the
3806 functions/variables and augment the table entries. */
3807 while (nesting_level
>= 0)
3809 unsigned int abbrev_number
, i
;
3810 struct abbrev_info
*abbrev
;
3811 struct funcinfo
*func
;
3812 struct varinfo
*var
;
3813 uint64_t current_offset
;
3815 /* PR 17512: file: 9f405d9d. */
3816 if (info_ptr
>= info_ptr_end
)
3819 current_offset
= info_ptr
- unit
->info_ptr_unit
;
3820 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3821 false, info_ptr_end
);
3822 if (abbrev_number
== 0)
3828 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3831 static unsigned int previous_failed_abbrev
= -1U;
3833 /* Avoid multiple reports of the same missing abbrev. */
3834 if (abbrev_number
!= previous_failed_abbrev
)
3837 (_("DWARF error: could not find abbrev number %u"),
3839 previous_failed_abbrev
= abbrev_number
;
3841 bfd_set_error (bfd_error_bad_value
);
3845 if (abbrev
->tag
== DW_TAG_subprogram
3846 || abbrev
->tag
== DW_TAG_entry_point
3847 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3849 size_t amt
= sizeof (struct funcinfo
);
3852 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
3855 func
->tag
= abbrev
->tag
;
3856 func
->prev_func
= unit
->function_table
;
3857 func
->unit_offset
= current_offset
;
3858 unit
->function_table
= func
;
3859 unit
->number_of_functions
++;
3860 BFD_ASSERT (!unit
->cached
);
3862 if (func
->tag
== DW_TAG_inlined_subroutine
)
3863 for (i
= nesting_level
; i
-- != 0; )
3864 if (nested_funcs
[i
].func
)
3866 func
->caller_func
= nested_funcs
[i
].func
;
3869 nested_funcs
[nesting_level
].func
= func
;
3874 if (abbrev
->tag
== DW_TAG_variable
3875 || abbrev
->tag
== DW_TAG_member
)
3877 size_t amt
= sizeof (struct varinfo
);
3879 var
= (struct varinfo
*) bfd_zalloc (abfd
, amt
);
3882 var
->tag
= abbrev
->tag
;
3884 var
->prev_var
= unit
->variable_table
;
3885 unit
->variable_table
= var
;
3886 var
->unit_offset
= current_offset
;
3887 /* PR 18205: Missing debug information can cause this
3888 var to be attached to an already cached unit. */
3893 /* No inline function in scope at this nesting level. */
3894 nested_funcs
[nesting_level
].func
= 0;
3897 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3899 struct attribute attr
;
3901 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
3902 unit
, info_ptr
, info_ptr_end
);
3903 if (info_ptr
== NULL
)
3907 if (abbrev
->has_children
)
3911 if (nesting_level
>= nested_funcs_size
)
3913 struct nest_funcinfo
*tmp
;
3915 nested_funcs_size
*= 2;
3916 tmp
= (struct nest_funcinfo
*)
3917 bfd_realloc (nested_funcs
,
3918 nested_funcs_size
* sizeof (*nested_funcs
));
3923 nested_funcs
[nesting_level
].func
= 0;
3927 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
3928 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
3930 /* This is the second pass over the abbrevs. */
3931 info_ptr
= unit
->first_child_die_ptr
;
3937 while (nesting_level
>= 0)
3939 unsigned int abbrev_number
, i
;
3940 struct abbrev_info
*abbrev
;
3941 struct attribute attr
;
3942 struct funcinfo
*func
;
3943 struct varinfo
*var
;
3945 bfd_vma high_pc
= 0;
3946 bool high_pc_relative
= false;
3947 uint64_t current_offset
;
3949 /* PR 17512: file: 9f405d9d. */
3950 if (info_ptr
>= info_ptr_end
)
3953 current_offset
= info_ptr
- unit
->info_ptr_unit
;
3954 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3955 false, info_ptr_end
);
3956 if (! abbrev_number
)
3962 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3963 /* This should have been handled above. */
3964 BFD_ASSERT (abbrev
!= NULL
);
3968 if (abbrev
->tag
== DW_TAG_subprogram
3969 || abbrev
->tag
== DW_TAG_entry_point
3970 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3973 && last_func
->prev_func
3974 && last_func
->prev_func
->unit_offset
== current_offset
)
3975 func
= last_func
->prev_func
;
3977 func
= lookup_func_by_offset (current_offset
, unit
->function_table
);
3984 else if (abbrev
->tag
== DW_TAG_variable
3985 || abbrev
->tag
== DW_TAG_member
)
3988 && last_var
->prev_var
3989 && last_var
->prev_var
->unit_offset
== current_offset
)
3990 var
= last_var
->prev_var
;
3992 var
= lookup_var_by_offset (current_offset
, unit
->variable_table
);
4000 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
4002 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
4003 unit
, info_ptr
, info_ptr_end
);
4004 if (info_ptr
== NULL
)
4011 case DW_AT_call_file
:
4012 if (is_int_form (&attr
))
4013 func
->caller_file
= concat_filename (unit
->line_table
,
4017 case DW_AT_call_line
:
4018 if (is_int_form (&attr
))
4019 func
->caller_line
= attr
.u
.val
;
4022 case DW_AT_abstract_origin
:
4023 case DW_AT_specification
:
4024 if (is_int_form (&attr
)
4025 && !find_abstract_instance (unit
, &attr
, 0,
4034 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
4036 if (func
->name
== NULL
&& is_str_form (&attr
))
4038 func
->name
= attr
.u
.str
;
4039 if (non_mangled (unit
->lang
))
4040 func
->is_linkage
= true;
4044 case DW_AT_linkage_name
:
4045 case DW_AT_MIPS_linkage_name
:
4046 /* PR 16949: Corrupt debug info can place
4047 non-string forms into these attributes. */
4048 if (is_str_form (&attr
))
4050 func
->name
= attr
.u
.str
;
4051 func
->is_linkage
= true;
4056 if (is_int_form (&attr
))
4057 low_pc
= attr
.u
.val
;
4061 if (is_int_form (&attr
))
4063 high_pc
= attr
.u
.val
;
4064 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
4069 if (is_int_form (&attr
)
4070 && !read_rangelist (unit
, &func
->arange
,
4071 &unit
->file
->trie_root
, attr
.u
.val
))
4075 case DW_AT_decl_file
:
4076 if (is_int_form (&attr
))
4077 func
->file
= concat_filename (unit
->line_table
,
4081 case DW_AT_decl_line
:
4082 if (is_int_form (&attr
))
4083 func
->line
= attr
.u
.val
;
4094 case DW_AT_specification
:
4095 if (is_int_form (&attr
) && attr
.u
.val
)
4098 if (!find_abstract_instance (unit
, &attr
, 0,
4104 _bfd_error_handler (_("DWARF error: could not find "
4105 "variable specification "
4107 (unsigned long) attr
.u
.val
);
4114 if (is_str_form (&attr
))
4115 var
->name
= attr
.u
.str
;
4118 case DW_AT_decl_file
:
4119 if (is_int_form (&attr
))
4120 var
->file
= concat_filename (unit
->line_table
,
4124 case DW_AT_decl_line
:
4125 if (is_int_form (&attr
))
4126 var
->line
= attr
.u
.val
;
4129 case DW_AT_external
:
4130 if (is_int_form (&attr
) && attr
.u
.val
!= 0)
4134 case DW_AT_location
:
4138 case DW_FORM_block1
:
4139 case DW_FORM_block2
:
4140 case DW_FORM_block4
:
4141 case DW_FORM_exprloc
:
4142 if (attr
.u
.blk
->data
!= NULL
4143 && *attr
.u
.blk
->data
== DW_OP_addr
)
4147 /* Verify that DW_OP_addr is the only opcode in the
4148 location, in which case the block size will be 1
4149 plus the address size. */
4150 /* ??? For TLS variables, gcc can emit
4151 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
4152 which we don't handle here yet. */
4153 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
4154 var
->addr
= bfd_get (unit
->addr_size
* 8,
4156 attr
.u
.blk
->data
+ 1);
4171 if (abbrev
->has_children
)
4174 if (high_pc_relative
)
4177 if (func
&& high_pc
!= 0)
4179 if (!arange_add (unit
, &func
->arange
, &unit
->file
->trie_root
,
4185 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4186 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4188 free (nested_funcs
);
4192 free (nested_funcs
);
4196 /* Read the attributes of the form strx and addrx. */
4199 reread_attribute (struct comp_unit
*unit
,
4200 struct attribute
*attr
,
4203 bool *high_pc_relative
,
4206 if (is_strx_form (attr
->form
))
4207 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
4208 if (is_addrx_form (attr
->form
))
4209 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
4213 case DW_AT_stmt_list
:
4215 unit
->line_offset
= attr
->u
.val
;
4219 if (is_str_form (attr
))
4220 unit
->name
= attr
->u
.str
;
4224 *low_pc
= attr
->u
.val
;
4226 unit
->base_address
= *low_pc
;
4230 *high_pc
= attr
->u
.val
;
4231 *high_pc_relative
= attr
->form
!= DW_FORM_addr
;
4235 if (!read_rangelist (unit
, &unit
->arange
,
4236 &unit
->file
->trie_root
, attr
->u
.val
))
4240 case DW_AT_comp_dir
:
4242 char *comp_dir
= attr
->u
.str
;
4244 if (!is_str_form (attr
))
4247 (_("DWARF error: DW_AT_comp_dir attribute encountered "
4248 "with a non-string form"));
4254 char *cp
= strchr (comp_dir
, ':');
4256 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
4259 unit
->comp_dir
= comp_dir
;
4263 case DW_AT_language
:
4264 unit
->lang
= attr
->u
.val
;
4270 /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
4271 includes the compilation unit header that proceeds the DIE's, but
4272 does not include the length field that precedes each compilation
4273 unit header. END_PTR points one past the end of this comp unit.
4274 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
4276 This routine does not read the whole compilation unit; only enough
4277 to get to the line number information for the compilation unit. */
4279 static struct comp_unit
*
4280 parse_comp_unit (struct dwarf2_debug
*stash
,
4281 struct dwarf2_debug_file
*file
,
4283 bfd_vma unit_length
,
4284 bfd_byte
*info_ptr_unit
,
4285 unsigned int offset_size
)
4287 struct comp_unit
* unit
;
4288 unsigned int version
;
4289 uint64_t abbrev_offset
= 0;
4290 /* Initialize it just to avoid a GCC false warning. */
4291 unsigned int addr_size
= -1;
4292 struct abbrev_info
** abbrevs
;
4293 unsigned int abbrev_number
, i
;
4294 struct abbrev_info
*abbrev
;
4295 struct attribute attr
;
4296 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
4299 bfd_vma high_pc
= 0;
4300 bfd
*abfd
= file
->bfd_ptr
;
4301 bool high_pc_relative
= false;
4302 enum dwarf_unit_type unit_type
;
4303 struct attribute
*str_addrp
= NULL
;
4304 size_t str_count
= 0;
4305 size_t str_alloc
= 0;
4306 bool compunit_flag
= false;
4308 version
= read_2_bytes (abfd
, &info_ptr
, end_ptr
);
4309 if (version
< 2 || version
> 5)
4311 /* PR 19872: A version number of 0 probably means that there is padding
4312 at the end of the .debug_info section. Gold puts it there when
4313 performing an incremental link, for example. So do not generate
4314 an error, just return a NULL. */
4318 (_("DWARF error: found dwarf version '%u', this reader"
4319 " only handles version 2, 3, 4 and 5 information"), version
);
4320 bfd_set_error (bfd_error_bad_value
);
4326 unit_type
= DW_UT_compile
;
4329 unit_type
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
4330 addr_size
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
4333 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
4334 if (offset_size
== 4)
4335 abbrev_offset
= read_4_bytes (abfd
, &info_ptr
, end_ptr
);
4337 abbrev_offset
= read_8_bytes (abfd
, &info_ptr
, end_ptr
);
4340 addr_size
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
4342 if (unit_type
== DW_UT_type
)
4344 /* Skip type signature. */
4347 /* Skip type offset. */
4348 info_ptr
+= offset_size
;
4351 if (addr_size
> sizeof (bfd_vma
))
4354 /* xgettext: c-format */
4355 (_("DWARF error: found address size '%u', this reader"
4356 " can not handle sizes greater than '%u'"),
4358 (unsigned int) sizeof (bfd_vma
));
4359 bfd_set_error (bfd_error_bad_value
);
4363 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
4366 ("DWARF error: found address size '%u', this reader"
4367 " can only handle address sizes '2', '4' and '8'", addr_size
);
4368 bfd_set_error (bfd_error_bad_value
);
4372 /* Read the abbrevs for this compilation unit into a table. */
4373 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
, file
);
4377 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
4379 if (! abbrev_number
)
4381 /* PR 19872: An abbrev number of 0 probably means that there is padding
4382 at the end of the .debug_abbrev section. Gold puts it there when
4383 performing an incremental link, for example. So do not generate
4384 an error, just return a NULL. */
4388 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
4391 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
4393 bfd_set_error (bfd_error_bad_value
);
4397 amt
= sizeof (struct comp_unit
);
4398 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
4402 unit
->version
= version
;
4403 unit
->addr_size
= addr_size
;
4404 unit
->offset_size
= offset_size
;
4405 unit
->abbrevs
= abbrevs
;
4406 unit
->end_ptr
= end_ptr
;
4407 unit
->stash
= stash
;
4409 unit
->info_ptr_unit
= info_ptr_unit
;
4411 if (abbrev
->tag
== DW_TAG_compile_unit
)
4412 compunit_flag
= true;
4414 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
4416 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
, end_ptr
);
4417 if (info_ptr
== NULL
)
4420 /* Identify attributes of the form strx* and addrx* which come before
4421 DW_AT_str_offsets_base and DW_AT_addr_base respectively in the CU.
4422 Store the attributes in an array and process them later. */
4423 if ((unit
->dwarf_str_offset
== 0 && is_strx_form (attr
.form
))
4424 || (unit
->dwarf_addr_offset
== 0 && is_addrx_form (attr
.form
)))
4426 if (str_count
<= str_alloc
)
4428 str_alloc
= 2 * str_alloc
+ 200;
4429 str_addrp
= bfd_realloc (str_addrp
,
4430 str_alloc
* sizeof (*str_addrp
));
4431 if (str_addrp
== NULL
)
4434 str_addrp
[str_count
] = attr
;
4439 /* Store the data if it is of an attribute we want to keep in a
4440 partial symbol table. */
4443 case DW_AT_stmt_list
:
4444 if (is_int_form (&attr
))
4447 unit
->line_offset
= attr
.u
.val
;
4452 if (is_str_form (&attr
))
4453 unit
->name
= attr
.u
.str
;
4457 if (is_int_form (&attr
))
4459 low_pc
= attr
.u
.val
;
4460 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
4461 this is the base address to use when reading location
4462 lists or range lists. */
4464 unit
->base_address
= low_pc
;
4469 if (is_int_form (&attr
))
4471 high_pc
= attr
.u
.val
;
4472 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
4477 if (is_int_form (&attr
)
4478 && !read_rangelist (unit
, &unit
->arange
,
4479 &unit
->file
->trie_root
, attr
.u
.val
))
4483 case DW_AT_comp_dir
:
4485 char *comp_dir
= attr
.u
.str
;
4487 /* PR 17512: file: 1fe726be. */
4488 if (!is_str_form (&attr
))
4491 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
4497 /* Irix 6.2 native cc prepends <machine>.: to the compilation
4498 directory, get rid of it. */
4499 char *cp
= strchr (comp_dir
, ':');
4501 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
4504 unit
->comp_dir
= comp_dir
;
4508 case DW_AT_language
:
4509 if (is_int_form (&attr
))
4510 unit
->lang
= attr
.u
.val
;
4513 case DW_AT_addr_base
:
4514 unit
->dwarf_addr_offset
= attr
.u
.val
;
4517 case DW_AT_str_offsets_base
:
4518 unit
->dwarf_str_offset
= attr
.u
.val
;
4526 for (i
= 0; i
< str_count
; ++i
)
4527 reread_attribute (unit
, &str_addrp
[i
], &low_pc
, &high_pc
,
4528 &high_pc_relative
, compunit_flag
);
4530 if (high_pc_relative
)
4534 if (!arange_add (unit
, &unit
->arange
, &unit
->file
->trie_root
,
4539 unit
->first_child_die_ptr
= info_ptr
;
4550 /* Return TRUE if UNIT may contain the address given by ADDR. When
4551 there are functions written entirely with inline asm statements, the
4552 range info in the compilation unit header may not be correct. We
4553 need to consult the line info table to see if a compilation unit
4554 really contains the given address. */
4557 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
4559 struct arange
*arange
;
4564 arange
= &unit
->arange
;
4567 if (addr
>= arange
->low
&& addr
< arange
->high
)
4569 arange
= arange
->next
;
4576 /* If UNIT contains ADDR, set the output parameters to the values for
4577 the line containing ADDR and return TRUE. Otherwise return FALSE.
4578 The output parameters, FILENAME_PTR, FUNCTION_PTR, and
4579 LINENUMBER_PTR, are pointers to the objects to be filled in. */
4582 comp_unit_find_nearest_line (struct comp_unit
*unit
,
4584 const char **filename_ptr
,
4585 struct funcinfo
**function_ptr
,
4586 unsigned int *linenumber_ptr
,
4587 unsigned int *discriminator_ptr
)
4589 bool line_p
, func_p
;
4591 if (!comp_unit_maybe_decode_line_info (unit
))
4594 *function_ptr
= NULL
;
4595 func_p
= lookup_address_in_function_table (unit
, addr
, function_ptr
);
4596 if (func_p
&& (*function_ptr
)->tag
== DW_TAG_inlined_subroutine
)
4597 unit
->stash
->inliner_chain
= *function_ptr
;
4599 line_p
= lookup_address_in_line_info_table (unit
->line_table
, addr
,
4603 return line_p
|| func_p
;
4606 /* Check to see if line info is already decoded in a comp_unit.
4607 If not, decode it. Returns TRUE if no errors were encountered;
4611 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
)
4616 if (! unit
->line_table
)
4618 if (! unit
->stmtlist
)
4624 unit
->line_table
= decode_line_info (unit
);
4626 if (! unit
->line_table
)
4632 if (unit
->first_child_die_ptr
< unit
->end_ptr
4633 && ! scan_unit_for_symbols (unit
))
4643 /* If UNIT contains SYM at ADDR, set the output parameters to the
4644 values for the line containing SYM. The output parameters,
4645 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
4648 Return TRUE if UNIT contains SYM, and no errors were encountered;
4652 comp_unit_find_line (struct comp_unit
*unit
,
4655 const char **filename_ptr
,
4656 unsigned int *linenumber_ptr
)
4658 if (!comp_unit_maybe_decode_line_info (unit
))
4661 if (sym
->flags
& BSF_FUNCTION
)
4662 return lookup_symbol_in_function_table (unit
, sym
, addr
,
4666 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
4671 /* Extract all interesting funcinfos and varinfos of a compilation
4672 unit into hash tables for faster lookup. Returns TRUE if no
4673 errors were enountered; FALSE otherwise. */
4676 comp_unit_hash_info (struct dwarf2_debug
*stash
,
4677 struct comp_unit
*unit
,
4678 struct info_hash_table
*funcinfo_hash_table
,
4679 struct info_hash_table
*varinfo_hash_table
)
4681 struct funcinfo
* each_func
;
4682 struct varinfo
* each_var
;
4685 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
4687 if (!comp_unit_maybe_decode_line_info (unit
))
4690 BFD_ASSERT (!unit
->cached
);
4692 /* To preserve the original search order, we went to visit the function
4693 infos in the reversed order of the list. However, making the list
4694 bi-directional use quite a bit of extra memory. So we reverse
4695 the list first, traverse the list in the now reversed order and
4696 finally reverse the list again to get back the original order. */
4697 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4698 for (each_func
= unit
->function_table
;
4700 each_func
= each_func
->prev_func
)
4702 /* Skip nameless functions. */
4703 if (each_func
->name
)
4704 /* There is no need to copy name string into hash table as
4705 name string is either in the dwarf string buffer or
4706 info in the stash. */
4707 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
4708 (void*) each_func
, false);
4710 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4714 /* We do the same for variable infos. */
4715 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4716 for (each_var
= unit
->variable_table
;
4718 each_var
= each_var
->prev_var
)
4720 /* Skip stack vars and vars with no files or names. */
4721 if (! each_var
->stack
4722 && each_var
->file
!= NULL
4723 && each_var
->name
!= NULL
)
4724 /* There is no need to copy name string into hash table as
4725 name string is either in the dwarf string buffer or
4726 info in the stash. */
4727 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
4728 (void*) each_var
, false);
4731 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4732 unit
->cached
= true;
4736 /* Locate a section in a BFD containing debugging info. The search starts
4737 from the section after AFTER_SEC, or from the first section in the BFD if
4738 AFTER_SEC is NULL. The search works by examining the names of the
4739 sections. There are three permissiable names. The first two are given
4740 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
4741 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
4742 This is a variation on the .debug_info section which has a checksum
4743 describing the contents appended onto the name. This allows the linker to
4744 identify and discard duplicate debugging sections for different
4745 compilation units. */
4746 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
4749 find_debug_info (bfd
*abfd
, const struct dwarf_debug_section
*debug_sections
,
4750 asection
*after_sec
)
4755 if (after_sec
== NULL
)
4757 look
= debug_sections
[debug_info
].uncompressed_name
;
4758 msec
= bfd_get_section_by_name (abfd
, look
);
4762 look
= debug_sections
[debug_info
].compressed_name
;
4763 msec
= bfd_get_section_by_name (abfd
, look
);
4767 for (msec
= abfd
->sections
; msec
!= NULL
; msec
= msec
->next
)
4768 if (startswith (msec
->name
, GNU_LINKONCE_INFO
))
4774 for (msec
= after_sec
->next
; msec
!= NULL
; msec
= msec
->next
)
4776 look
= debug_sections
[debug_info
].uncompressed_name
;
4777 if (strcmp (msec
->name
, look
) == 0)
4780 look
= debug_sections
[debug_info
].compressed_name
;
4781 if (look
!= NULL
&& strcmp (msec
->name
, look
) == 0)
4784 if (startswith (msec
->name
, GNU_LINKONCE_INFO
))
4791 /* Transfer VMAs from object file to separate debug file. */
4794 set_debug_vma (bfd
*orig_bfd
, bfd
*debug_bfd
)
4798 for (s
= orig_bfd
->sections
, d
= debug_bfd
->sections
;
4799 s
!= NULL
&& d
!= NULL
;
4800 s
= s
->next
, d
= d
->next
)
4802 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4804 /* ??? Assumes 1-1 correspondence between sections in the
4806 if (strcmp (s
->name
, d
->name
) == 0)
4808 d
->output_section
= s
->output_section
;
4809 d
->output_offset
= s
->output_offset
;
4815 /* If the dwarf2 info was found in a separate debug file, return the
4816 debug file section corresponding to the section in the original file
4817 and the debug file symbols. */
4820 _bfd_dwarf2_stash_syms (struct dwarf2_debug
*stash
, bfd
*abfd
,
4821 asection
**sec
, asymbol
***syms
)
4823 if (stash
->f
.bfd_ptr
!= abfd
)
4829 *syms
= stash
->f
.syms
;
4833 for (s
= abfd
->sections
, d
= stash
->f
.bfd_ptr
->sections
;
4834 s
!= NULL
&& d
!= NULL
;
4835 s
= s
->next
, d
= d
->next
)
4837 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4840 && strcmp (s
->name
, d
->name
) == 0)
4843 *syms
= stash
->f
.syms
;
4850 /* Unset vmas for adjusted sections in STASH. */
4853 unset_sections (struct dwarf2_debug
*stash
)
4856 struct adjusted_section
*p
;
4858 i
= stash
->adjusted_section_count
;
4859 p
= stash
->adjusted_sections
;
4860 for (; i
> 0; i
--, p
++)
4861 p
->section
->vma
= 0;
4864 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4865 relocatable object file. VMAs are normally all zero in relocatable
4866 object files, so if we want to distinguish locations in sections by
4867 address we need to set VMAs so the sections do not overlap. We
4868 also set VMA on .debug_info so that when we have multiple
4869 .debug_info sections (or the linkonce variant) they also do not
4870 overlap. The multiple .debug_info sections make up a single
4871 logical section. ??? We should probably do the same for other
4875 place_sections (bfd
*orig_bfd
, struct dwarf2_debug
*stash
)
4878 struct adjusted_section
*p
;
4880 const char *debug_info_name
;
4882 if (stash
->adjusted_section_count
!= 0)
4884 i
= stash
->adjusted_section_count
;
4885 p
= stash
->adjusted_sections
;
4886 for (; i
> 0; i
--, p
++)
4887 p
->section
->vma
= p
->adj_vma
;
4891 debug_info_name
= stash
->debug_sections
[debug_info
].uncompressed_name
;
4898 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4902 if ((sect
->output_section
!= NULL
4903 && sect
->output_section
!= sect
4904 && (sect
->flags
& SEC_DEBUGGING
) == 0)
4908 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
4909 || startswith (sect
->name
, GNU_LINKONCE_INFO
));
4911 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
4917 if (abfd
== stash
->f
.bfd_ptr
)
4919 abfd
= stash
->f
.bfd_ptr
;
4923 stash
->adjusted_section_count
= -1;
4926 bfd_vma last_vma
= 0, last_dwarf
= 0;
4927 size_t amt
= i
* sizeof (struct adjusted_section
);
4929 p
= (struct adjusted_section
*) bfd_malloc (amt
);
4933 stash
->adjusted_sections
= p
;
4934 stash
->adjusted_section_count
= i
;
4941 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4946 if ((sect
->output_section
!= NULL
4947 && sect
->output_section
!= sect
4948 && (sect
->flags
& SEC_DEBUGGING
) == 0)
4952 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
4953 || startswith (sect
->name
, GNU_LINKONCE_INFO
));
4955 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
4959 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
4963 BFD_ASSERT (sect
->alignment_power
== 0);
4964 sect
->vma
= last_dwarf
;
4969 /* Align the new address to the current section
4971 last_vma
= ((last_vma
4972 + ~(-((bfd_vma
) 1 << sect
->alignment_power
)))
4973 & (-((bfd_vma
) 1 << sect
->alignment_power
)));
4974 sect
->vma
= last_vma
;
4979 p
->adj_vma
= sect
->vma
;
4982 if (abfd
== stash
->f
.bfd_ptr
)
4984 abfd
= stash
->f
.bfd_ptr
;
4988 if (orig_bfd
!= stash
->f
.bfd_ptr
)
4989 set_debug_vma (orig_bfd
, stash
->f
.bfd_ptr
);
4994 /* Look up a funcinfo by name using the given info hash table. If found,
4995 also update the locations pointed to by filename_ptr and linenumber_ptr.
4997 This function returns TRUE if a funcinfo that matches the given symbol
4998 and address is found with any error; otherwise it returns FALSE. */
5001 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
5004 const char **filename_ptr
,
5005 unsigned int *linenumber_ptr
)
5007 struct funcinfo
* each_func
;
5008 struct funcinfo
* best_fit
= NULL
;
5009 bfd_vma best_fit_len
= 0;
5010 struct info_list_node
*node
;
5011 struct arange
*arange
;
5012 const char *name
= bfd_asymbol_name (sym
);
5013 asection
*sec
= bfd_asymbol_section (sym
);
5015 for (node
= lookup_info_hash_table (hash_table
, name
);
5019 each_func
= (struct funcinfo
*) node
->info
;
5020 for (arange
= &each_func
->arange
;
5022 arange
= arange
->next
)
5024 if ((!each_func
->sec
|| each_func
->sec
== sec
)
5025 && addr
>= arange
->low
5026 && addr
< arange
->high
5028 || arange
->high
- arange
->low
< best_fit_len
))
5030 best_fit
= each_func
;
5031 best_fit_len
= arange
->high
- arange
->low
;
5038 best_fit
->sec
= sec
;
5039 *filename_ptr
= best_fit
->file
;
5040 *linenumber_ptr
= best_fit
->line
;
5047 /* Look up a varinfo by name using the given info hash table. If found,
5048 also update the locations pointed to by filename_ptr and linenumber_ptr.
5050 This function returns TRUE if a varinfo that matches the given symbol
5051 and address is found with any error; otherwise it returns FALSE. */
5054 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
5057 const char **filename_ptr
,
5058 unsigned int *linenumber_ptr
)
5060 const char *name
= bfd_asymbol_name (sym
);
5061 asection
*sec
= bfd_asymbol_section (sym
);
5062 struct varinfo
* each
;
5063 struct info_list_node
*node
;
5065 for (node
= lookup_info_hash_table (hash_table
, name
);
5069 each
= (struct varinfo
*) node
->info
;
5070 if (each
->addr
== addr
5071 && (!each
->sec
|| each
->sec
== sec
))
5074 *filename_ptr
= each
->file
;
5075 *linenumber_ptr
= each
->line
;
5083 /* Update the funcinfo and varinfo info hash tables if they are
5084 not up to date. Returns TRUE if there is no error; otherwise
5085 returns FALSE and disable the info hash tables. */
5088 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
5090 struct comp_unit
*each
;
5092 /* Exit if hash tables are up-to-date. */
5093 if (stash
->f
.all_comp_units
== stash
->hash_units_head
)
5096 if (stash
->hash_units_head
)
5097 each
= stash
->hash_units_head
->prev_unit
;
5099 each
= stash
->f
.last_comp_unit
;
5103 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
5104 stash
->varinfo_hash_table
))
5106 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
5109 each
= each
->prev_unit
;
5112 stash
->hash_units_head
= stash
->f
.all_comp_units
;
5116 /* Check consistency of info hash tables. This is for debugging only. */
5118 static void ATTRIBUTE_UNUSED
5119 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
5121 struct comp_unit
*each_unit
;
5122 struct funcinfo
*each_func
;
5123 struct varinfo
*each_var
;
5124 struct info_list_node
*node
;
5127 for (each_unit
= stash
->f
.all_comp_units
;
5129 each_unit
= each_unit
->next_unit
)
5131 for (each_func
= each_unit
->function_table
;
5133 each_func
= each_func
->prev_func
)
5135 if (!each_func
->name
)
5137 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
5141 while (node
&& !found
)
5143 found
= node
->info
== each_func
;
5149 for (each_var
= each_unit
->variable_table
;
5151 each_var
= each_var
->prev_var
)
5153 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
5155 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
5159 while (node
&& !found
)
5161 found
= node
->info
== each_var
;
5169 /* Check to see if we want to enable the info hash tables, which consume
5170 quite a bit of memory. Currently we only check the number times
5171 bfd_dwarf2_find_line is called. In the future, we may also want to
5172 take the number of symbols into account. */
5175 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
5177 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
5179 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
5182 /* FIXME: Maybe we should check the reduce_memory_overheads
5183 and optimize fields in the bfd_link_info structure ? */
5185 /* Create hash tables. */
5186 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
5187 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
5188 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
5190 /* Turn off info hashes if any allocation above fails. */
5191 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
5194 /* We need a forced update so that the info hash tables will
5195 be created even though there is no compilation unit. That
5196 happens if STASH_INFO_HASH_TRIGGER is 0. */
5197 if (stash_maybe_update_info_hash_tables (stash
))
5198 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
5201 /* Find the file and line associated with a symbol and address using the
5202 info hash tables of a stash. If there is a match, the function returns
5203 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
5204 otherwise it returns FALSE. */
5207 stash_find_line_fast (struct dwarf2_debug
*stash
,
5210 const char **filename_ptr
,
5211 unsigned int *linenumber_ptr
)
5213 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
5215 if (sym
->flags
& BSF_FUNCTION
)
5216 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
5217 filename_ptr
, linenumber_ptr
);
5218 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
5219 filename_ptr
, linenumber_ptr
);
5222 /* Save current section VMAs. */
5225 save_section_vma (const bfd
*abfd
, struct dwarf2_debug
*stash
)
5230 if (abfd
->section_count
== 0)
5232 stash
->sec_vma
= bfd_malloc (sizeof (*stash
->sec_vma
) * abfd
->section_count
);
5233 if (stash
->sec_vma
== NULL
)
5235 stash
->sec_vma_count
= abfd
->section_count
;
5236 for (i
= 0, s
= abfd
->sections
;
5237 s
!= NULL
&& i
< abfd
->section_count
;
5240 if (s
->output_section
!= NULL
)
5241 stash
->sec_vma
[i
] = s
->output_section
->vma
+ s
->output_offset
;
5243 stash
->sec_vma
[i
] = s
->vma
;
5248 /* Compare current section VMAs against those at the time the stash
5249 was created. If find_nearest_line is used in linker warnings or
5250 errors early in the link process, the debug info stash will be
5251 invalid for later calls. This is because we relocate debug info
5252 sections, so the stashed section contents depend on symbol values,
5253 which in turn depend on section VMAs. */
5256 section_vma_same (const bfd
*abfd
, const struct dwarf2_debug
*stash
)
5261 /* PR 24334: If the number of sections in ABFD has changed between
5262 when the stash was created and now, then we cannot trust the
5263 stashed vma information. */
5264 if (abfd
->section_count
!= stash
->sec_vma_count
)
5267 for (i
= 0, s
= abfd
->sections
;
5268 s
!= NULL
&& i
< abfd
->section_count
;
5273 if (s
->output_section
!= NULL
)
5274 vma
= s
->output_section
->vma
+ s
->output_offset
;
5277 if (vma
!= stash
->sec_vma
[i
])
5283 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
5284 If DEBUG_BFD is not specified, we read debug information from ABFD
5285 or its gnu_debuglink. The results will be stored in PINFO.
5286 The function returns TRUE iff debug information is ready. */
5289 _bfd_dwarf2_slurp_debug_info (bfd
*abfd
, bfd
*debug_bfd
,
5290 const struct dwarf_debug_section
*debug_sections
,
5295 size_t amt
= sizeof (struct dwarf2_debug
);
5296 bfd_size_type total_size
;
5298 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
5302 if (stash
->orig_bfd
== abfd
5303 && section_vma_same (abfd
, stash
))
5305 /* Check that we did previously find some debug information
5306 before attempting to make use of it. */
5307 if (stash
->f
.bfd_ptr
!= NULL
)
5309 if (do_place
&& !place_sections (abfd
, stash
))
5316 _bfd_dwarf2_cleanup_debug_info (abfd
, pinfo
);
5317 memset (stash
, 0, amt
);
5321 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
5325 stash
->orig_bfd
= abfd
;
5326 stash
->debug_sections
= debug_sections
;
5327 stash
->f
.syms
= symbols
;
5328 if (!save_section_vma (abfd
, stash
))
5331 stash
->f
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
5332 del_abbrev
, calloc
, free
);
5333 if (!stash
->f
.abbrev_offsets
)
5336 stash
->alt
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
5337 del_abbrev
, calloc
, free
);
5338 if (!stash
->alt
.abbrev_offsets
)
5341 stash
->f
.trie_root
= alloc_trie_leaf (abfd
);
5342 if (!stash
->f
.trie_root
)
5345 stash
->alt
.trie_root
= alloc_trie_leaf (abfd
);
5346 if (!stash
->alt
.trie_root
)
5351 if (debug_bfd
== NULL
)
5354 msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
5355 if (msec
== NULL
&& abfd
== debug_bfd
)
5357 char * debug_filename
;
5359 debug_filename
= bfd_follow_build_id_debuglink (abfd
, DEBUGDIR
);
5360 if (debug_filename
== NULL
)
5361 debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
5363 if (debug_filename
== NULL
)
5364 /* No dwarf2 info, and no gnu_debuglink to follow.
5365 Note that at this point the stash has been allocated, but
5366 contains zeros. This lets future calls to this function
5367 fail more quickly. */
5370 debug_bfd
= bfd_openr (debug_filename
, NULL
);
5371 free (debug_filename
);
5372 if (debug_bfd
== NULL
)
5373 /* FIXME: Should we report our failure to follow the debuglink ? */
5376 /* Set BFD_DECOMPRESS to decompress debug sections. */
5377 debug_bfd
->flags
|= BFD_DECOMPRESS
;
5378 if (!bfd_check_format (debug_bfd
, bfd_object
)
5379 || (msec
= find_debug_info (debug_bfd
,
5380 debug_sections
, NULL
)) == NULL
5381 || !bfd_generic_link_read_symbols (debug_bfd
))
5383 bfd_close (debug_bfd
);
5387 symbols
= bfd_get_outsymbols (debug_bfd
);
5388 stash
->f
.syms
= symbols
;
5389 stash
->close_on_cleanup
= true;
5391 stash
->f
.bfd_ptr
= debug_bfd
;
5394 && !place_sections (abfd
, stash
))
5397 /* There can be more than one DWARF2 info section in a BFD these
5398 days. First handle the easy case when there's only one. If
5399 there's more than one, try case two: none of the sections is
5400 compressed. In that case, read them all in and produce one
5401 large stash. We do this in two passes - in the first pass we
5402 just accumulate the section sizes, and in the second pass we
5403 read in the section's contents. (The allows us to avoid
5404 reallocing the data as we add sections to the stash.) If
5405 some or all sections are compressed, then do things the slow
5406 way, with a bunch of reallocs. */
5408 if (! find_debug_info (debug_bfd
, debug_sections
, msec
))
5410 /* Case 1: only one info section. */
5411 total_size
= msec
->size
;
5412 if (! read_section (debug_bfd
, &stash
->debug_sections
[debug_info
],
5414 &stash
->f
.dwarf_info_buffer
, &total_size
))
5419 /* Case 2: multiple sections. */
5420 for (total_size
= 0;
5422 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
5424 /* Catch PR25070 testcase overflowing size calculation here. */
5425 if (total_size
+ msec
->size
< total_size
5426 || total_size
+ msec
->size
< msec
->size
)
5428 bfd_set_error (bfd_error_no_memory
);
5431 total_size
+= msec
->size
;
5434 stash
->f
.dwarf_info_buffer
= (bfd_byte
*) bfd_malloc (total_size
);
5435 if (stash
->f
.dwarf_info_buffer
== NULL
)
5439 for (msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
5441 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
5449 if (!(bfd_simple_get_relocated_section_contents
5450 (debug_bfd
, msec
, stash
->f
.dwarf_info_buffer
+ total_size
,
5458 stash
->f
.info_ptr
= stash
->f
.dwarf_info_buffer
;
5459 stash
->f
.dwarf_info_size
= total_size
;
5463 /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
5465 static struct comp_unit
*
5466 stash_comp_unit (struct dwarf2_debug
*stash
, struct dwarf2_debug_file
*file
)
5468 bfd_size_type length
;
5469 unsigned int offset_size
;
5470 bfd_byte
*info_ptr_unit
= file
->info_ptr
;
5471 bfd_byte
*info_ptr_end
= file
->dwarf_info_buffer
+ file
->dwarf_info_size
;
5473 if (file
->info_ptr
>= info_ptr_end
)
5476 length
= read_4_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
5477 /* A 0xffffff length is the DWARF3 way of indicating
5478 we use 64-bit offsets, instead of 32-bit offsets. */
5479 if (length
== 0xffffffff)
5482 length
= read_8_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
5484 /* A zero length is the IRIX way of indicating 64-bit offsets,
5485 mostly because the 64-bit length will generally fit in 32
5486 bits, and the endianness helps. */
5487 else if (length
== 0)
5490 length
= read_4_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
5492 /* In the absence of the hints above, we assume 32-bit DWARF2
5493 offsets even for targets with 64-bit addresses, because:
5494 a) most of the time these targets will not have generated
5495 more than 2Gb of debug info and so will not need 64-bit
5498 b) if they do use 64-bit offsets but they are not using
5499 the size hints that are tested for above then they are
5500 not conforming to the DWARF3 standard anyway. */
5505 && length
<= (size_t) (info_ptr_end
- file
->info_ptr
))
5507 struct comp_unit
*each
= parse_comp_unit (stash
, file
,
5508 file
->info_ptr
, length
,
5509 info_ptr_unit
, offset_size
);
5512 if (file
->all_comp_units
)
5513 file
->all_comp_units
->prev_unit
= each
;
5515 file
->last_comp_unit
= each
;
5517 each
->next_unit
= file
->all_comp_units
;
5518 file
->all_comp_units
= each
;
5520 if (each
->arange
.high
== 0)
5522 each
->next_unit_without_ranges
= file
->all_comp_units_without_ranges
;
5523 file
->all_comp_units_without_ranges
= each
->next_unit_without_ranges
;
5526 file
->info_ptr
+= length
;
5531 /* Don't trust any of the DWARF info after a corrupted length or
5533 file
->info_ptr
= info_ptr_end
;
5537 /* Hash function for an asymbol. */
5540 hash_asymbol (const void *sym
)
5542 const asymbol
*asym
= sym
;
5543 return htab_hash_string (asym
->name
);
5546 /* Equality function for asymbols. */
5549 eq_asymbol (const void *a
, const void *b
)
5551 const asymbol
*sa
= a
;
5552 const asymbol
*sb
= b
;
5553 return strcmp (sa
->name
, sb
->name
) == 0;
5556 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
5557 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
5558 symbol in SYMBOLS and return the difference between the low_pc and
5559 the symbol's address. Returns 0 if no suitable symbol could be found. */
5562 _bfd_dwarf2_find_symbol_bias (asymbol
** symbols
, void ** pinfo
)
5564 struct dwarf2_debug
*stash
;
5565 struct comp_unit
* unit
;
5567 bfd_signed_vma result
= 0;
5570 stash
= (struct dwarf2_debug
*) *pinfo
;
5572 if (stash
== NULL
|| symbols
== NULL
)
5575 sym_hash
= htab_create_alloc (10, hash_asymbol
, eq_asymbol
,
5576 NULL
, xcalloc
, free
);
5577 for (psym
= symbols
; * psym
!= NULL
; psym
++)
5579 asymbol
* sym
= * psym
;
5581 if (sym
->flags
& BSF_FUNCTION
&& sym
->section
!= NULL
)
5583 void **slot
= htab_find_slot (sym_hash
, sym
, INSERT
);
5588 for (unit
= stash
->f
.all_comp_units
; unit
; unit
= unit
->next_unit
)
5590 struct funcinfo
* func
;
5592 comp_unit_maybe_decode_line_info (unit
);
5594 for (func
= unit
->function_table
; func
!= NULL
; func
= func
->prev_func
)
5595 if (func
->name
&& func
->arange
.low
)
5597 asymbol search
, *sym
;
5599 /* FIXME: Do we need to scan the aranges looking for the
5602 search
.name
= func
->name
;
5603 sym
= htab_find (sym_hash
, &search
);
5606 result
= func
->arange
.low
- (sym
->value
+ sym
->section
->vma
);
5613 htab_delete (sym_hash
);
5617 /* Find the source code location of SYMBOL. If SYMBOL is NULL
5618 then find the nearest source code location corresponding to
5619 the address SECTION + OFFSET.
5620 Returns 1 if the line is found without error and fills in
5621 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
5622 NULL the FUNCTIONNAME_PTR is also filled in.
5623 Returns 2 if partial information from _bfd_elf_find_function is
5624 returned (function and maybe file) by looking at symbols. DWARF2
5625 info is present but not regarding the requested code location.
5626 Returns 0 otherwise.
5627 SYMBOLS contains the symbol table for ABFD.
5628 DEBUG_SECTIONS contains the name of the dwarf debug sections. */
5631 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
5636 const char **filename_ptr
,
5637 const char **functionname_ptr
,
5638 unsigned int *linenumber_ptr
,
5639 unsigned int *discriminator_ptr
,
5640 const struct dwarf_debug_section
*debug_sections
,
5643 /* Read each compilation unit from the section .debug_info, and check
5644 to see if it contains the address we are searching for. If yes,
5645 lookup the address, and return the line number info. If no, go
5646 on to the next compilation unit.
5648 We keep a list of all the previously read compilation units, and
5649 a pointer to the next un-read compilation unit. Check the
5650 previously read units before reading more. */
5651 struct dwarf2_debug
*stash
;
5652 /* What address are we looking for? */
5654 struct comp_unit
* each
;
5655 struct funcinfo
*function
= NULL
;
5659 *filename_ptr
= NULL
;
5660 if (functionname_ptr
!= NULL
)
5661 *functionname_ptr
= NULL
;
5662 *linenumber_ptr
= 0;
5663 if (discriminator_ptr
)
5664 *discriminator_ptr
= 0;
5666 if (! _bfd_dwarf2_slurp_debug_info (abfd
, NULL
, debug_sections
,
5668 (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0))
5671 stash
= (struct dwarf2_debug
*) *pinfo
;
5673 do_line
= symbol
!= NULL
;
5676 BFD_ASSERT (section
== NULL
&& offset
== 0 && functionname_ptr
== NULL
);
5677 section
= bfd_asymbol_section (symbol
);
5678 addr
= symbol
->value
;
5682 BFD_ASSERT (section
!= NULL
&& functionname_ptr
!= NULL
);
5685 /* If we have no SYMBOL but the section we're looking at is not a
5686 code section, then take a look through the list of symbols to see
5687 if we have a symbol at the address we're looking for. If we do
5688 then use this to look up line information. This will allow us to
5689 give file and line results for data symbols. We exclude code
5690 symbols here, if we look up a function symbol and then look up the
5691 line information we'll actually return the line number for the
5692 opening '{' rather than the function definition line. This is
5693 because looking up by symbol uses the line table, in which the
5694 first line for a function is usually the opening '{', while
5695 looking up the function by section + offset uses the
5696 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
5697 which will be the line of the function name. */
5698 if (symbols
!= NULL
&& (section
->flags
& SEC_CODE
) == 0)
5702 for (tmp
= symbols
; (*tmp
) != NULL
; ++tmp
)
5703 if ((*tmp
)->the_bfd
== abfd
5704 && (*tmp
)->section
== section
5705 && (*tmp
)->value
== offset
5706 && ((*tmp
)->flags
& BSF_SECTION_SYM
) == 0)
5710 /* For local symbols, keep going in the hope we find a
5712 if ((symbol
->flags
& BSF_GLOBAL
) != 0)
5718 if (section
->output_section
)
5719 addr
+= section
->output_section
->vma
+ section
->output_offset
;
5721 addr
+= section
->vma
;
5723 /* A null info_ptr indicates that there is no dwarf2 info
5724 (or that an error occured while setting up the stash). */
5725 if (! stash
->f
.info_ptr
)
5728 stash
->inliner_chain
= NULL
;
5730 /* Check the previously read comp. units first. */
5733 /* The info hash tables use quite a bit of memory. We may not want to
5734 always use them. We use some heuristics to decide if and when to
5736 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
5737 stash_maybe_enable_info_hash_tables (abfd
, stash
);
5739 /* Keep info hash table up to date if they are available. Note that we
5740 may disable the hash tables if there is any error duing update. */
5741 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
5742 stash_maybe_update_info_hash_tables (stash
);
5744 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
5746 found
= stash_find_line_fast (stash
, symbol
, addr
, filename_ptr
,
5753 /* Check the previously read comp. units first. */
5754 for (each
= stash
->f
.all_comp_units
; each
; each
= each
->next_unit
)
5755 if ((symbol
->flags
& BSF_FUNCTION
) == 0
5756 || each
->arange
.high
== 0
5757 || comp_unit_contains_address (each
, addr
))
5759 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
5768 struct trie_node
*trie
= stash
->f
.trie_root
;
5769 unsigned int bits
= VMA_BITS
- 8;
5770 struct comp_unit
**prev_each
;
5772 /* Traverse interior nodes until we get to a leaf. */
5773 while (trie
&& trie
->num_room_in_leaf
== 0)
5775 int ch
= (addr
>> bits
) & 0xff;
5776 trie
= ((struct trie_interior
*) trie
)->children
[ch
];
5782 const struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
5785 for (i
= 0; i
< leaf
->num_stored_in_leaf
; ++i
)
5786 leaf
->ranges
[i
].unit
->mark
= false;
5788 for (i
= 0; i
< leaf
->num_stored_in_leaf
; ++i
)
5790 struct comp_unit
*unit
= leaf
->ranges
[i
].unit
;
5792 || addr
< leaf
->ranges
[i
].low_pc
5793 || addr
>= leaf
->ranges
[i
].high_pc
)
5797 found
= comp_unit_find_nearest_line (unit
, addr
,
5807 /* Also scan through all compilation units without any ranges,
5808 taking them out of the list if they have acquired any since
5810 prev_each
= &stash
->f
.all_comp_units_without_ranges
;
5811 for (each
= *prev_each
; each
; each
= each
->next_unit_without_ranges
)
5813 if (each
->arange
.high
!= 0)
5815 *prev_each
= each
->next_unit_without_ranges
;
5819 found
= comp_unit_find_nearest_line (each
, addr
,
5826 prev_each
= &each
->next_unit_without_ranges
;
5830 /* Read each remaining comp. units checking each as they are read. */
5831 while ((each
= stash_comp_unit (stash
, &stash
->f
)) != NULL
)
5833 /* DW_AT_low_pc and DW_AT_high_pc are optional for
5834 compilation units. If we don't have them (i.e.,
5835 unit->high == 0), we need to consult the line info table
5836 to see if a compilation unit contains the given
5839 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
5840 || each
->arange
.high
== 0
5841 || comp_unit_contains_address (each
, addr
))
5842 && comp_unit_find_line (each
, symbol
, addr
,
5843 filename_ptr
, linenumber_ptr
));
5845 found
= ((each
->arange
.high
== 0
5846 || comp_unit_contains_address (each
, addr
))
5847 && comp_unit_find_nearest_line (each
, addr
,
5851 discriminator_ptr
));
5858 if (functionname_ptr
&& function
&& function
->is_linkage
)
5860 *functionname_ptr
= function
->name
;
5864 else if (functionname_ptr
5865 && (!*functionname_ptr
5866 || (function
&& !function
->is_linkage
)))
5869 asymbol
**syms
= symbols
;
5870 asection
*sec
= section
;
5872 _bfd_dwarf2_stash_syms (stash
, abfd
, &sec
, &syms
);
5873 fun
= _bfd_elf_find_function (abfd
, syms
, sec
, offset
,
5874 *filename_ptr
? NULL
: filename_ptr
,
5877 if (!found
&& fun
!= NULL
)
5880 if (function
&& !function
->is_linkage
)
5884 sec_vma
= section
->vma
;
5885 if (section
->output_section
!= NULL
)
5886 sec_vma
= section
->output_section
->vma
+ section
->output_offset
;
5888 *functionname_ptr
= function
->name
;
5889 else if (fun
->value
+ sec_vma
== function
->arange
.low
)
5890 function
->name
= *functionname_ptr
;
5891 /* Even if we didn't find a linkage name, say that we have
5892 to stop a repeated search of symbols. */
5893 function
->is_linkage
= true;
5897 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
5898 unset_sections (stash
);
5904 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
5905 const char **filename_ptr
,
5906 const char **functionname_ptr
,
5907 unsigned int *linenumber_ptr
,
5910 struct dwarf2_debug
*stash
;
5912 stash
= (struct dwarf2_debug
*) *pinfo
;
5915 struct funcinfo
*func
= stash
->inliner_chain
;
5917 if (func
&& func
->caller_func
)
5919 *filename_ptr
= func
->caller_file
;
5920 *functionname_ptr
= func
->caller_func
->name
;
5921 *linenumber_ptr
= func
->caller_line
;
5922 stash
->inliner_chain
= func
->caller_func
;
5931 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
, void **pinfo
)
5933 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
5934 struct comp_unit
*each
;
5935 struct dwarf2_debug_file
*file
;
5937 if (abfd
== NULL
|| stash
== NULL
)
5940 if (stash
->varinfo_hash_table
)
5941 bfd_hash_table_free (&stash
->varinfo_hash_table
->base
);
5942 if (stash
->funcinfo_hash_table
)
5943 bfd_hash_table_free (&stash
->funcinfo_hash_table
->base
);
5948 for (each
= file
->all_comp_units
; each
; each
= each
->next_unit
)
5950 struct funcinfo
*function_table
= each
->function_table
;
5951 struct varinfo
*variable_table
= each
->variable_table
;
5953 if (each
->line_table
&& each
->line_table
!= file
->line_table
)
5955 free (each
->line_table
->files
);
5956 free (each
->line_table
->dirs
);
5959 free (each
->lookup_funcinfo_table
);
5960 each
->lookup_funcinfo_table
= NULL
;
5962 while (function_table
)
5964 free (function_table
->file
);
5965 function_table
->file
= NULL
;
5966 free (function_table
->caller_file
);
5967 function_table
->caller_file
= NULL
;
5968 function_table
= function_table
->prev_func
;
5971 while (variable_table
)
5973 free (variable_table
->file
);
5974 variable_table
->file
= NULL
;
5975 variable_table
= variable_table
->prev_var
;
5979 if (file
->line_table
)
5981 free (file
->line_table
->files
);
5982 free (file
->line_table
->dirs
);
5984 htab_delete (file
->abbrev_offsets
);
5986 free (file
->dwarf_line_str_buffer
);
5987 free (file
->dwarf_str_buffer
);
5988 free (file
->dwarf_ranges_buffer
);
5989 free (file
->dwarf_line_buffer
);
5990 free (file
->dwarf_abbrev_buffer
);
5991 free (file
->dwarf_info_buffer
);
5992 if (file
== &stash
->alt
)
5996 free (stash
->sec_vma
);
5997 free (stash
->adjusted_sections
);
5998 if (stash
->close_on_cleanup
)
5999 bfd_close (stash
->f
.bfd_ptr
);
6000 if (stash
->alt
.bfd_ptr
)
6001 bfd_close (stash
->alt
.bfd_ptr
);
6004 /* Find the function to a particular section and offset,
6005 for error reporting. */
6008 _bfd_elf_find_function (bfd
*abfd
,
6012 const char **filename_ptr
,
6013 const char **functionname_ptr
)
6015 struct elf_find_function_cache
6017 asection
*last_section
;
6019 const char *filename
;
6020 bfd_size_type func_size
;
6023 if (symbols
== NULL
)
6026 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6029 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
6032 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
6033 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
6037 if (cache
->last_section
!= section
6038 || cache
->func
== NULL
6039 || offset
< cache
->func
->value
6040 || offset
>= cache
->func
->value
+ cache
->func_size
)
6045 /* ??? Given multiple file symbols, it is impossible to reliably
6046 choose the right file name for global symbols. File symbols are
6047 local symbols, and thus all file symbols must sort before any
6048 global symbols. The ELF spec may be interpreted to say that a
6049 file symbol must sort before other local symbols, but currently
6050 ld -r doesn't do this. So, for ld -r output, it is possible to
6051 make a better choice of file name for local symbols by ignoring
6052 file symbols appearing after a given local symbol. */
6053 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
6054 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6058 state
= nothing_seen
;
6059 cache
->filename
= NULL
;
6061 cache
->func_size
= 0;
6062 cache
->last_section
= section
;
6064 for (p
= symbols
; *p
!= NULL
; p
++)
6070 if ((sym
->flags
& BSF_FILE
) != 0)
6073 if (state
== symbol_seen
)
6074 state
= file_after_symbol_seen
;
6078 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
6080 && code_off
<= offset
6081 && (code_off
> low_func
6082 || (code_off
== low_func
6083 && size
> cache
->func_size
)))
6086 cache
->func_size
= size
;
6087 cache
->filename
= NULL
;
6088 low_func
= code_off
;
6090 && ((sym
->flags
& BSF_LOCAL
) != 0
6091 || state
!= file_after_symbol_seen
))
6092 cache
->filename
= bfd_asymbol_name (file
);
6094 if (state
== nothing_seen
)
6095 state
= symbol_seen
;
6099 if (cache
->func
== NULL
)
6103 *filename_ptr
= cache
->filename
;
6104 if (functionname_ptr
)
6105 *functionname_ptr
= bfd_asymbol_name (cache
->func
);