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
=
149 bfd_zalloc (abfd
, sizeof (struct trie_leaf
));
152 leaf
->head
.num_room_in_leaf
= TRIE_LEAF_SIZE
;
156 struct dwarf2_debug_file
158 /* The actual bfd from which debug info was loaded. Might be
159 different to orig_bfd because of gnu_debuglink sections. */
162 /* Pointer to the symbol table. */
165 /* The current info pointer for the .debug_info section being parsed. */
168 /* A pointer to the memory block allocated for .debug_info sections. */
169 bfd_byte
*dwarf_info_buffer
;
171 /* Length of the loaded .debug_info sections. */
172 bfd_size_type dwarf_info_size
;
174 /* Pointer to the .debug_abbrev section loaded into memory. */
175 bfd_byte
*dwarf_abbrev_buffer
;
177 /* Length of the loaded .debug_abbrev section. */
178 bfd_size_type dwarf_abbrev_size
;
180 /* Buffer for decode_line_info. */
181 bfd_byte
*dwarf_line_buffer
;
183 /* Length of the loaded .debug_line section. */
184 bfd_size_type dwarf_line_size
;
186 /* Pointer to the .debug_str section loaded into memory. */
187 bfd_byte
*dwarf_str_buffer
;
189 /* Length of the loaded .debug_str section. */
190 bfd_size_type dwarf_str_size
;
192 /* Pointer to the .debug_str_offsets section loaded into memory. */
193 bfd_byte
*dwarf_str_offsets_buffer
;
195 /* Length of the loaded .debug_str_offsets section. */
196 bfd_size_type dwarf_str_offsets_size
;
198 /* Pointer to the .debug_addr section loaded into memory. */
199 bfd_byte
*dwarf_addr_buffer
;
201 /* Length of the loaded .debug_addr section. */
202 bfd_size_type dwarf_addr_size
;
204 /* Pointer to the .debug_line_str section loaded into memory. */
205 bfd_byte
*dwarf_line_str_buffer
;
207 /* Length of the loaded .debug_line_str section. */
208 bfd_size_type dwarf_line_str_size
;
210 /* Pointer to the .debug_ranges section loaded into memory. */
211 bfd_byte
*dwarf_ranges_buffer
;
213 /* Length of the loaded .debug_ranges section. */
214 bfd_size_type dwarf_ranges_size
;
216 /* Pointer to the .debug_rnglists section loaded into memory. */
217 bfd_byte
*dwarf_rnglists_buffer
;
219 /* Length of the loaded .debug_rnglists section. */
220 bfd_size_type dwarf_rnglists_size
;
222 /* A list of all previously read comp_units. */
223 struct comp_unit
*all_comp_units
;
225 /* A list of all previously read comp_units with no ranges (yet). */
226 struct comp_unit
*all_comp_units_without_ranges
;
228 /* Last comp unit in list above. */
229 struct comp_unit
*last_comp_unit
;
231 /* Line table at line_offset zero. */
232 struct line_info_table
*line_table
;
234 /* Hash table to map offsets to decoded abbrevs. */
235 htab_t abbrev_offsets
;
237 /* Root of a trie to map addresses to compilation units. */
238 struct trie_node
*trie_root
;
243 /* Names of the debug sections. */
244 const struct dwarf_debug_section
*debug_sections
;
246 /* Per-file stuff. */
247 struct dwarf2_debug_file f
, alt
;
249 /* Pointer to the original bfd for which debug was loaded. This is what
250 we use to compare and so check that the cached debug data is still
251 valid - it saves having to possibly dereference the gnu_debuglink each
255 /* If the most recent call to bfd_find_nearest_line was given an
256 address in an inlined function, preserve a pointer into the
257 calling chain for subsequent calls to bfd_find_inliner_info to
259 struct funcinfo
*inliner_chain
;
261 /* Section VMAs at the time the stash was built. */
263 /* Number of sections in the SEC_VMA table. */
264 unsigned int sec_vma_count
;
266 /* Number of sections whose VMA we must adjust. */
267 int adjusted_section_count
;
269 /* Array of sections with adjusted VMA. */
270 struct adjusted_section
*adjusted_sections
;
272 /* Number of times find_line is called. This is used in
273 the heuristic for enabling the info hash tables. */
276 #define STASH_INFO_HASH_TRIGGER 100
278 /* Hash table mapping symbol names to function infos. */
279 struct info_hash_table
*funcinfo_hash_table
;
281 /* Hash table mapping symbol names to variable infos. */
282 struct info_hash_table
*varinfo_hash_table
;
284 /* Head of comp_unit list in the last hash table update. */
285 struct comp_unit
*hash_units_head
;
287 /* Status of info hash. */
288 int info_hash_status
;
289 #define STASH_INFO_HASH_OFF 0
290 #define STASH_INFO_HASH_ON 1
291 #define STASH_INFO_HASH_DISABLED 2
293 /* True if we opened bfd_ptr. */
294 bool close_on_cleanup
;
304 /* A minimal decoding of DWARF2 compilation units. We only decode
305 what's needed to get to the line number information. */
309 /* Chain the previously read compilation units. */
310 struct comp_unit
*next_unit
;
312 /* Chain the previously read compilation units that have no ranges yet.
313 We scan these separately when we have a trie over the ranges.
314 Unused if arange.high != 0. */
315 struct comp_unit
*next_unit_without_ranges
;
317 /* Likewise, chain the compilation unit read after this one.
318 The comp units are stored in reversed reading order. */
319 struct comp_unit
*prev_unit
;
321 /* Keep the bfd convenient (for memory allocation). */
324 /* The lowest and highest addresses contained in this compilation
325 unit as specified in the compilation unit header. */
326 struct arange arange
;
328 /* The DW_AT_name attribute (for error messages). */
331 /* The abbrev hash table. */
332 struct abbrev_info
**abbrevs
;
334 /* DW_AT_language. */
337 /* Note that an error was found by comp_unit_find_nearest_line. */
340 /* The DW_AT_comp_dir attribute. */
343 /* TRUE if there is a line number table associated with this comp. unit. */
346 /* Pointer to the current comp_unit so that we can find a given entry
348 bfd_byte
*info_ptr_unit
;
350 /* The offset into .debug_line of the line number table. */
351 unsigned long line_offset
;
353 /* Pointer to the first child die for the comp unit. */
354 bfd_byte
*first_child_die_ptr
;
356 /* The end of the comp unit. */
359 /* The decoded line number, NULL if not yet decoded. */
360 struct line_info_table
*line_table
;
362 /* A list of the functions found in this comp. unit. */
363 struct funcinfo
*function_table
;
365 /* A table of function information references searchable by address. */
366 struct lookup_funcinfo
*lookup_funcinfo_table
;
368 /* Number of functions in the function_table and sorted_function_table. */
369 bfd_size_type number_of_functions
;
371 /* A list of the variables found in this comp. unit. */
372 struct varinfo
*variable_table
;
374 /* Pointers to dwarf2_debug structures. */
375 struct dwarf2_debug
*stash
;
376 struct dwarf2_debug_file
*file
;
378 /* DWARF format version for this unit - from unit header. */
381 /* Address size for this unit - from unit header. */
382 unsigned char addr_size
;
384 /* Offset size for this unit - from unit header. */
385 unsigned char offset_size
;
387 /* Base address for this unit - from DW_AT_low_pc attribute of
388 DW_TAG_compile_unit DIE */
389 bfd_vma base_address
;
391 /* TRUE if symbols are cached in hash table for faster lookup by name. */
394 /* Used when iterating over trie leaves to know which units we have
395 already seen in this iteration. */
398 /* Base address of debug_addr section. */
399 size_t dwarf_addr_offset
;
401 /* Base address of string offset table. */
402 size_t dwarf_str_offset
;
405 /* This data structure holds the information of an abbrev. */
408 unsigned int number
; /* Number identifying abbrev. */
409 enum dwarf_tag tag
; /* DWARF tag. */
410 bool has_children
; /* TRUE if the abbrev has children. */
411 unsigned int num_attrs
; /* Number of attributes. */
412 struct attr_abbrev
* attrs
; /* An array of attribute descriptions. */
413 struct abbrev_info
* next
; /* Next in chain. */
418 enum dwarf_attribute name
;
419 enum dwarf_form form
;
420 bfd_vma implicit_const
;
423 /* Map of uncompressed DWARF debug section name to compressed one. It
424 is terminated by NULL uncompressed_name. */
426 const struct dwarf_debug_section dwarf_debug_sections
[] =
428 { ".debug_abbrev", ".zdebug_abbrev" },
429 { ".debug_aranges", ".zdebug_aranges" },
430 { ".debug_frame", ".zdebug_frame" },
431 { ".debug_info", ".zdebug_info" },
432 { ".debug_info", ".zdebug_info" },
433 { ".debug_line", ".zdebug_line" },
434 { ".debug_loc", ".zdebug_loc" },
435 { ".debug_macinfo", ".zdebug_macinfo" },
436 { ".debug_macro", ".zdebug_macro" },
437 { ".debug_pubnames", ".zdebug_pubnames" },
438 { ".debug_pubtypes", ".zdebug_pubtypes" },
439 { ".debug_ranges", ".zdebug_ranges" },
440 { ".debug_rnglists", ".zdebug_rnglist" },
441 { ".debug_static_func", ".zdebug_static_func" },
442 { ".debug_static_vars", ".zdebug_static_vars" },
443 { ".debug_str", ".zdebug_str", },
444 { ".debug_str", ".zdebug_str", },
445 { ".debug_str_offsets", ".zdebug_str_offsets", },
446 { ".debug_addr", ".zdebug_addr", },
447 { ".debug_line_str", ".zdebug_line_str", },
448 { ".debug_types", ".zdebug_types" },
449 /* GNU DWARF 1 extensions */
450 { ".debug_sfnames", ".zdebug_sfnames" },
451 { ".debug_srcinfo", ".zebug_srcinfo" },
452 /* SGI/MIPS DWARF 2 extensions */
453 { ".debug_funcnames", ".zdebug_funcnames" },
454 { ".debug_typenames", ".zdebug_typenames" },
455 { ".debug_varnames", ".zdebug_varnames" },
456 { ".debug_weaknames", ".zdebug_weaknames" },
460 /* NB/ Numbers in this enum must match up with indices
461 into the dwarf_debug_sections[] array above. */
462 enum dwarf_debug_section_enum
494 /* A static assertion. */
495 extern int dwarf_debug_section_assert
[ARRAY_SIZE (dwarf_debug_sections
)
496 == debug_max
+ 1 ? 1 : -1];
498 #ifndef ABBREV_HASH_SIZE
499 #define ABBREV_HASH_SIZE 121
501 #ifndef ATTR_ALLOC_CHUNK
502 #define ATTR_ALLOC_CHUNK 4
505 /* Variable and function hash tables. This is used to speed up look-up
506 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
507 In order to share code between variable and function infos, we use
508 a list of untyped pointer for all variable/function info associated with
509 a symbol. We waste a bit of memory for list with one node but that
510 simplifies the code. */
512 struct info_list_node
514 struct info_list_node
*next
;
518 /* Info hash entry. */
519 struct info_hash_entry
521 struct bfd_hash_entry root
;
522 struct info_list_node
*head
;
525 struct info_hash_table
527 struct bfd_hash_table base
;
530 /* Function to create a new entry in info hash table. */
532 static struct bfd_hash_entry
*
533 info_hash_table_newfunc (struct bfd_hash_entry
*entry
,
534 struct bfd_hash_table
*table
,
537 struct info_hash_entry
*ret
= (struct info_hash_entry
*) entry
;
539 /* Allocate the structure if it has not already been allocated by a
543 ret
= (struct info_hash_entry
*) bfd_hash_allocate (table
,
549 /* Call the allocation method of the base class. */
550 ret
= ((struct info_hash_entry
*)
551 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
553 /* Initialize the local fields here. */
557 return (struct bfd_hash_entry
*) ret
;
560 /* Function to create a new info hash table. It returns a pointer to the
561 newly created table or NULL if there is any error. We need abfd
562 solely for memory allocation. */
564 static struct info_hash_table
*
565 create_info_hash_table (bfd
*abfd
)
567 struct info_hash_table
*hash_table
;
569 hash_table
= ((struct info_hash_table
*)
570 bfd_alloc (abfd
, sizeof (struct info_hash_table
)));
574 if (!bfd_hash_table_init (&hash_table
->base
, info_hash_table_newfunc
,
575 sizeof (struct info_hash_entry
)))
577 bfd_release (abfd
, hash_table
);
584 /* Insert an info entry into an info hash table. We do not check of
585 duplicate entries. Also, the caller need to guarantee that the
586 right type of info in inserted as info is passed as a void* pointer.
587 This function returns true if there is no error. */
590 insert_info_hash_table (struct info_hash_table
*hash_table
,
595 struct info_hash_entry
*entry
;
596 struct info_list_node
*node
;
598 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
,
603 node
= (struct info_list_node
*) bfd_hash_allocate (&hash_table
->base
,
609 node
->next
= entry
->head
;
615 /* Look up an info entry list from an info hash table. Return NULL
618 static struct info_list_node
*
619 lookup_info_hash_table (struct info_hash_table
*hash_table
, const char *key
)
621 struct info_hash_entry
*entry
;
623 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
, key
,
625 return entry
? entry
->head
: NULL
;
628 /* Read a section into its appropriate place in the dwarf2_debug
629 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
630 not NULL, use bfd_simple_get_relocated_section_contents to read the
631 section contents, otherwise use bfd_get_section_contents. Fail if
632 the located section does not contain at least OFFSET bytes. */
635 read_section (bfd
*abfd
,
636 const struct dwarf_debug_section
*sec
,
639 bfd_byte
**section_buffer
,
640 bfd_size_type
*section_size
)
642 const char *section_name
= sec
->uncompressed_name
;
643 bfd_byte
*contents
= *section_buffer
;
645 /* The section may have already been read. */
646 if (contents
== NULL
)
652 msec
= bfd_get_section_by_name (abfd
, section_name
);
655 section_name
= sec
->compressed_name
;
656 msec
= bfd_get_section_by_name (abfd
, section_name
);
660 _bfd_error_handler (_("DWARF error: can't find %s section."),
661 sec
->uncompressed_name
);
662 bfd_set_error (bfd_error_bad_value
);
666 amt
= bfd_get_section_limit_octets (abfd
, msec
);
667 filesize
= bfd_get_file_size (abfd
);
668 /* PR 28834: A compressed debug section could well decompress to a size
669 larger than the file, so we choose an arbitrary modifier of 10x in
670 the test below. If this ever turns out to be insufficient, it can
671 be changed by a future update. */
672 if (amt
>= filesize
* 10)
675 _bfd_error_handler (_("DWARF error: section %s is larger than 10x its filesize! (0x%lx vs 0x%lx)"),
676 section_name
, (long) amt
, (long) filesize
);
677 bfd_set_error (bfd_error_bad_value
);
681 /* Paranoia - alloc one extra so that we can make sure a string
682 section is NUL terminated. */
686 /* Paranoia - this should never happen. */
687 bfd_set_error (bfd_error_no_memory
);
690 contents
= (bfd_byte
*) bfd_malloc (amt
);
691 if (contents
== NULL
)
694 ? !bfd_simple_get_relocated_section_contents (abfd
, msec
, contents
,
696 : !bfd_get_section_contents (abfd
, msec
, contents
, 0, *section_size
))
701 contents
[*section_size
] = 0;
702 *section_buffer
= contents
;
705 /* It is possible to get a bad value for the offset into the section
706 that the client wants. Validate it here to avoid trouble later. */
707 if (offset
!= 0 && offset
>= *section_size
)
709 /* xgettext: c-format */
710 _bfd_error_handler (_("DWARF error: offset (%" PRIu64
")"
711 " greater than or equal to %s size (%" PRIu64
")"),
712 (uint64_t) offset
, section_name
,
713 (uint64_t) *section_size
);
714 bfd_set_error (bfd_error_bad_value
);
721 /* Read dwarf information from a buffer. */
723 static inline uint64_t
724 read_n_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
, int n
)
726 bfd_byte
*buf
= *ptr
;
733 return bfd_get (n
* 8, abfd
, buf
);
737 read_1_byte (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
739 return read_n_bytes (abfd
, ptr
, end
, 1);
743 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
**ptr
, bfd_byte
*end
)
745 bfd_byte
*buf
= *ptr
;
752 return bfd_get_signed_8 (abfd
, buf
);
756 read_2_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
758 return read_n_bytes (abfd
, ptr
, end
, 2);
762 read_3_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
764 unsigned int val
= read_1_byte (abfd
, ptr
, end
);
766 val
|= read_1_byte (abfd
, ptr
, end
);
768 val
|= read_1_byte (abfd
, ptr
, end
);
769 if (bfd_little_endian (abfd
))
770 val
= (((val
>> 16) & 0xff)
772 | ((val
& 0xff) << 16));
777 read_4_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
779 return read_n_bytes (abfd
, ptr
, end
, 4);
783 read_8_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
785 return read_n_bytes (abfd
, ptr
, end
, 8);
788 static struct dwarf_block
*
789 read_blk (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
, size_t size
)
791 bfd_byte
*buf
= *ptr
;
792 struct dwarf_block
*block
;
794 block
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (*block
));
798 if (size
> (size_t) (end
- buf
))
813 /* Scans a NUL terminated string starting at *PTR, returning a pointer to it.
814 Bytes at or beyond BUF_END will not be read. Returns NULL if the
815 terminator is not found or if the string is empty. *PTR is
816 incremented over the bytes scanned, including the terminator. */
819 read_string (bfd_byte
**ptr
,
822 bfd_byte
*buf
= *ptr
;
825 while (buf
< buf_end
)
838 /* Reads an offset from *PTR and then locates the string at this offset
839 inside the debug string section. Returns a pointer to the string.
840 Increments *PTR by the number of bytes read for the offset. This
841 value is set even if the function fails. Bytes at or beyond
842 BUF_END will not be read. Returns NULL if there was a problem, or
843 if the string is empty. Does not check for NUL termination of the
847 read_indirect_string (struct comp_unit
*unit
,
852 struct dwarf2_debug
*stash
= unit
->stash
;
853 struct dwarf2_debug_file
*file
= unit
->file
;
856 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
862 if (unit
->offset_size
== 4)
863 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
865 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
867 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
869 &file
->dwarf_str_buffer
, &file
->dwarf_str_size
))
872 str
= (char *) file
->dwarf_str_buffer
+ offset
;
878 /* Like read_indirect_string but from .debug_line_str section. */
881 read_indirect_line_string (struct comp_unit
*unit
,
886 struct dwarf2_debug
*stash
= unit
->stash
;
887 struct dwarf2_debug_file
*file
= unit
->file
;
890 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
896 if (unit
->offset_size
== 4)
897 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
899 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
901 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_line_str
],
903 &file
->dwarf_line_str_buffer
,
904 &file
->dwarf_line_str_size
))
907 str
= (char *) file
->dwarf_line_str_buffer
+ offset
;
913 /* Like read_indirect_string but uses a .debug_str located in
914 an alternate file pointed to by the .gnu_debugaltlink section.
915 Used to impement DW_FORM_GNU_strp_alt. */
918 read_alt_indirect_string (struct comp_unit
*unit
,
923 struct dwarf2_debug
*stash
= unit
->stash
;
926 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
932 if (unit
->offset_size
== 4)
933 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
935 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
937 if (stash
->alt
.bfd_ptr
== NULL
)
940 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
942 if (debug_filename
== NULL
)
945 debug_bfd
= bfd_openr (debug_filename
, NULL
);
946 free (debug_filename
);
947 if (debug_bfd
== NULL
)
948 /* FIXME: Should we report our failure to follow the debuglink ? */
951 if (!bfd_check_format (debug_bfd
, bfd_object
))
953 bfd_close (debug_bfd
);
956 stash
->alt
.bfd_ptr
= debug_bfd
;
959 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
960 stash
->debug_sections
+ debug_str_alt
,
961 stash
->alt
.syms
, offset
,
962 &stash
->alt
.dwarf_str_buffer
,
963 &stash
->alt
.dwarf_str_size
))
966 str
= (char *) stash
->alt
.dwarf_str_buffer
+ offset
;
973 /* Resolve an alternate reference from UNIT at OFFSET.
974 Returns a pointer into the loaded alternate CU upon success
975 or NULL upon failure. */
978 read_alt_indirect_ref (struct comp_unit
*unit
, uint64_t offset
)
980 struct dwarf2_debug
*stash
= unit
->stash
;
982 if (stash
->alt
.bfd_ptr
== NULL
)
985 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
987 if (debug_filename
== NULL
)
990 debug_bfd
= bfd_openr (debug_filename
, NULL
);
991 free (debug_filename
);
992 if (debug_bfd
== NULL
)
993 /* FIXME: Should we report our failure to follow the debuglink ? */
996 if (!bfd_check_format (debug_bfd
, bfd_object
))
998 bfd_close (debug_bfd
);
1001 stash
->alt
.bfd_ptr
= debug_bfd
;
1004 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
1005 stash
->debug_sections
+ debug_info_alt
,
1006 stash
->alt
.syms
, offset
,
1007 &stash
->alt
.dwarf_info_buffer
,
1008 &stash
->alt
.dwarf_info_size
))
1011 return stash
->alt
.dwarf_info_buffer
+ offset
;
1015 read_address (struct comp_unit
*unit
, bfd_byte
**ptr
, bfd_byte
*buf_end
)
1017 bfd_byte
*buf
= *ptr
;
1020 if (bfd_get_flavour (unit
->abfd
) == bfd_target_elf_flavour
)
1021 signed_vma
= get_elf_backend_data (unit
->abfd
)->sign_extend_vma
;
1023 if (unit
->addr_size
> (size_t) (buf_end
- buf
))
1029 *ptr
= buf
+ unit
->addr_size
;
1032 switch (unit
->addr_size
)
1035 return bfd_get_signed_64 (unit
->abfd
, buf
);
1037 return bfd_get_signed_32 (unit
->abfd
, buf
);
1039 return bfd_get_signed_16 (unit
->abfd
, buf
);
1046 switch (unit
->addr_size
)
1049 return bfd_get_64 (unit
->abfd
, buf
);
1051 return bfd_get_32 (unit
->abfd
, buf
);
1053 return bfd_get_16 (unit
->abfd
, buf
);
1060 /* Lookup an abbrev_info structure in the abbrev hash table. */
1062 static struct abbrev_info
*
1063 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
1065 unsigned int hash_number
;
1066 struct abbrev_info
*abbrev
;
1068 hash_number
= number
% ABBREV_HASH_SIZE
;
1069 abbrev
= abbrevs
[hash_number
];
1073 if (abbrev
->number
== number
)
1076 abbrev
= abbrev
->next
;
1082 /* We keep a hash table to map .debug_abbrev section offsets to the
1083 array of abbrevs, so that compilation units using the same set of
1084 abbrevs do not waste memory. */
1086 struct abbrev_offset_entry
1089 struct abbrev_info
**abbrevs
;
1093 hash_abbrev (const void *p
)
1095 const struct abbrev_offset_entry
*ent
= p
;
1096 return htab_hash_pointer ((void *) ent
->offset
);
1100 eq_abbrev (const void *pa
, const void *pb
)
1102 const struct abbrev_offset_entry
*a
= pa
;
1103 const struct abbrev_offset_entry
*b
= pb
;
1104 return a
->offset
== b
->offset
;
1108 del_abbrev (void *p
)
1110 struct abbrev_offset_entry
*ent
= p
;
1111 struct abbrev_info
**abbrevs
= ent
->abbrevs
;
1114 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1116 struct abbrev_info
*abbrev
= abbrevs
[i
];
1120 free (abbrev
->attrs
);
1121 abbrev
= abbrev
->next
;
1127 /* In DWARF version 2, the description of the debugging information is
1128 stored in a separate .debug_abbrev section. Before we read any
1129 dies from a section we read in all abbreviations and install them
1132 static struct abbrev_info
**
1133 read_abbrevs (bfd
*abfd
, uint64_t offset
, struct dwarf2_debug
*stash
,
1134 struct dwarf2_debug_file
*file
)
1136 struct abbrev_info
**abbrevs
;
1137 bfd_byte
*abbrev_ptr
;
1138 bfd_byte
*abbrev_end
;
1139 struct abbrev_info
*cur_abbrev
;
1140 unsigned int abbrev_number
, abbrev_name
;
1141 unsigned int abbrev_form
, hash_number
;
1144 struct abbrev_offset_entry ent
= { offset
, NULL
};
1146 if (ent
.offset
!= offset
)
1149 slot
= htab_find_slot (file
->abbrev_offsets
, &ent
, INSERT
);
1153 return ((struct abbrev_offset_entry
*) (*slot
))->abbrevs
;
1155 if (! read_section (abfd
, &stash
->debug_sections
[debug_abbrev
],
1157 &file
->dwarf_abbrev_buffer
,
1158 &file
->dwarf_abbrev_size
))
1161 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
1162 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
1163 if (abbrevs
== NULL
)
1166 abbrev_ptr
= file
->dwarf_abbrev_buffer
+ offset
;
1167 abbrev_end
= file
->dwarf_abbrev_buffer
+ file
->dwarf_abbrev_size
;
1168 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1171 /* Loop until we reach an abbrev number of 0. */
1172 while (abbrev_number
)
1174 amt
= sizeof (struct abbrev_info
);
1175 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
1176 if (cur_abbrev
== NULL
)
1179 /* Read in abbrev header. */
1180 cur_abbrev
->number
= abbrev_number
;
1181 cur_abbrev
->tag
= (enum dwarf_tag
)
1182 _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1184 cur_abbrev
->has_children
= read_1_byte (abfd
, &abbrev_ptr
, abbrev_end
);
1186 /* Now read in declarations. */
1189 /* Initialize it just to avoid a GCC false warning. */
1190 bfd_vma implicit_const
= -1;
1192 abbrev_name
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1194 abbrev_form
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1196 if (abbrev_form
== DW_FORM_implicit_const
)
1197 implicit_const
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1199 if (abbrev_name
== 0)
1202 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
1204 struct attr_abbrev
*tmp
;
1206 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
1207 amt
*= sizeof (struct attr_abbrev
);
1208 tmp
= (struct attr_abbrev
*) bfd_realloc (cur_abbrev
->attrs
, amt
);
1211 cur_abbrev
->attrs
= tmp
;
1214 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
1215 = (enum dwarf_attribute
) abbrev_name
;
1216 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].form
1217 = (enum dwarf_form
) abbrev_form
;
1218 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].implicit_const
1220 ++cur_abbrev
->num_attrs
;
1223 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
1224 cur_abbrev
->next
= abbrevs
[hash_number
];
1225 abbrevs
[hash_number
] = cur_abbrev
;
1227 /* Get next abbreviation.
1228 Under Irix6 the abbreviations for a compilation unit are not
1229 always properly terminated with an abbrev number of 0.
1230 Exit loop if we encounter an abbreviation which we have
1231 already read (which means we are about to read the abbreviations
1232 for the next compile unit) or if the end of the abbreviation
1233 table is reached. */
1234 if ((size_t) (abbrev_ptr
- file
->dwarf_abbrev_buffer
)
1235 >= file
->dwarf_abbrev_size
)
1237 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1239 if (lookup_abbrev (abbrev_number
, abbrevs
) != NULL
)
1243 *slot
= bfd_malloc (sizeof ent
);
1246 ent
.abbrevs
= abbrevs
;
1247 memcpy (*slot
, &ent
, sizeof ent
);
1251 if (abbrevs
!= NULL
)
1255 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1257 struct abbrev_info
*abbrev
= abbrevs
[i
];
1261 free (abbrev
->attrs
);
1262 abbrev
= abbrev
->next
;
1270 /* Returns true if the form is one which has a string value. */
1273 is_str_form (const struct attribute
*attr
)
1277 case DW_FORM_string
:
1284 case DW_FORM_line_strp
:
1285 case DW_FORM_GNU_strp_alt
:
1293 /* Returns true if the form is one which has an integer value. */
1296 is_int_form (const struct attribute
*attr
)
1308 case DW_FORM_ref_addr
:
1313 case DW_FORM_ref_udata
:
1314 case DW_FORM_sec_offset
:
1315 case DW_FORM_flag_present
:
1316 case DW_FORM_ref_sig8
:
1318 case DW_FORM_implicit_const
:
1319 case DW_FORM_addrx1
:
1320 case DW_FORM_addrx2
:
1321 case DW_FORM_addrx3
:
1322 case DW_FORM_addrx4
:
1323 case DW_FORM_GNU_ref_alt
:
1331 /* Returns true if the form is strx[1-4]. */
1334 is_strx_form (enum dwarf_form form
)
1336 return (form
== DW_FORM_strx
1337 || form
== DW_FORM_strx1
1338 || form
== DW_FORM_strx2
1339 || form
== DW_FORM_strx3
1340 || form
== DW_FORM_strx4
);
1343 /* Return true if the form is addrx[1-4]. */
1346 is_addrx_form (enum dwarf_form form
)
1348 return (form
== DW_FORM_addrx
1349 || form
== DW_FORM_addrx1
1350 || form
== DW_FORM_addrx2
1351 || form
== DW_FORM_addrx3
1352 || form
== DW_FORM_addrx4
);
1355 /* Returns the address in .debug_addr section using DW_AT_addr_base.
1356 Used to implement DW_FORM_addrx*. */
1358 read_indexed_address (uint64_t idx
, struct comp_unit
*unit
)
1360 struct dwarf2_debug
*stash
= unit
->stash
;
1361 struct dwarf2_debug_file
*file
= unit
->file
;
1362 size_t addr_base
= unit
->dwarf_addr_offset
;
1368 if (!read_section (unit
->abfd
, &stash
->debug_sections
[debug_addr
],
1370 &file
->dwarf_addr_buffer
, &file
->dwarf_addr_size
))
1373 info_ptr
= file
->dwarf_addr_buffer
+ addr_base
+ idx
* unit
->offset_size
;
1375 if (unit
->offset_size
== 4)
1376 return bfd_get_32 (unit
->abfd
, info_ptr
);
1378 return bfd_get_64 (unit
->abfd
, info_ptr
);
1381 /* Returns the string using DW_AT_str_offsets_base.
1382 Used to implement DW_FORM_strx*. */
1384 read_indexed_string (uint64_t idx
, struct comp_unit
*unit
)
1386 struct dwarf2_debug
*stash
= unit
->stash
;
1387 struct dwarf2_debug_file
*file
= unit
->file
;
1389 unsigned long str_offset
;
1394 if (!read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
1396 &file
->dwarf_str_buffer
, &file
->dwarf_str_size
))
1399 if (!read_section (unit
->abfd
, &stash
->debug_sections
[debug_str_offsets
],
1401 &file
->dwarf_str_offsets_buffer
,
1402 &file
->dwarf_str_offsets_size
))
1405 info_ptr
= (file
->dwarf_str_offsets_buffer
1406 + unit
->dwarf_str_offset
1407 + idx
* unit
->offset_size
);
1409 if (unit
->offset_size
== 4)
1410 str_offset
= bfd_get_32 (unit
->abfd
, info_ptr
);
1412 str_offset
= bfd_get_64 (unit
->abfd
, info_ptr
);
1414 return (const char *) file
->dwarf_str_buffer
+ str_offset
;
1417 /* Read and fill in the value of attribute ATTR as described by FORM.
1418 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1419 Returns an updated INFO_PTR taking into account the amount of data read. */
1422 read_attribute_value (struct attribute
* attr
,
1424 bfd_vma implicit_const
,
1425 struct comp_unit
* unit
,
1426 bfd_byte
* info_ptr
,
1427 bfd_byte
* info_ptr_end
)
1429 bfd
*abfd
= unit
->abfd
;
1432 if (info_ptr
>= info_ptr_end
&& form
!= DW_FORM_flag_present
)
1434 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1435 bfd_set_error (bfd_error_bad_value
);
1439 attr
->form
= (enum dwarf_form
) form
;
1443 case DW_FORM_flag_present
:
1446 case DW_FORM_ref_addr
:
1447 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1449 if (unit
->version
>= 3)
1451 if (unit
->offset_size
== 4)
1452 attr
->u
.val
= read_4_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1454 attr
->u
.val
= read_8_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1459 attr
->u
.val
= read_address (unit
, &info_ptr
, info_ptr_end
);
1461 case DW_FORM_GNU_ref_alt
:
1462 case DW_FORM_sec_offset
:
1463 if (unit
->offset_size
== 4)
1464 attr
->u
.val
= read_4_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1466 attr
->u
.val
= read_8_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1468 case DW_FORM_block2
:
1469 amt
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1470 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1471 if (attr
->u
.blk
== NULL
)
1474 case DW_FORM_block4
:
1475 amt
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1476 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1477 if (attr
->u
.blk
== NULL
)
1483 attr
->u
.val
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1485 case DW_FORM_addrx1
:
1486 attr
->u
.val
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1487 /* dwarf_addr_offset value 0 indicates the attribute DW_AT_addr_base
1489 if (unit
->dwarf_addr_offset
!= 0)
1490 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
1494 attr
->u
.val
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1496 case DW_FORM_addrx2
:
1497 attr
->u
.val
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1498 if (unit
->dwarf_addr_offset
!= 0)
1499 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
1501 case DW_FORM_addrx3
:
1502 attr
->u
.val
= read_3_bytes (abfd
, &info_ptr
, info_ptr_end
);
1503 if (unit
->dwarf_addr_offset
!= 0)
1504 attr
->u
.val
= read_indexed_address(attr
->u
.val
, unit
);
1508 attr
->u
.val
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1510 case DW_FORM_addrx4
:
1511 attr
->u
.val
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1512 if (unit
->dwarf_addr_offset
!= 0)
1513 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
1517 case DW_FORM_ref_sig8
:
1518 attr
->u
.val
= read_8_bytes (abfd
, &info_ptr
, info_ptr_end
);
1520 case DW_FORM_string
:
1521 attr
->u
.str
= read_string (&info_ptr
, info_ptr_end
);
1524 attr
->u
.str
= read_indirect_string (unit
, &info_ptr
, info_ptr_end
);
1526 case DW_FORM_line_strp
:
1527 attr
->u
.str
= read_indirect_line_string (unit
, &info_ptr
, info_ptr_end
);
1529 case DW_FORM_GNU_strp_alt
:
1530 attr
->u
.str
= read_alt_indirect_string (unit
, &info_ptr
, info_ptr_end
);
1533 attr
->u
.val
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1534 /* dwarf_str_offset value 0 indicates the attribute DW_AT_str_offsets_base
1536 if (unit
->dwarf_str_offset
!= 0)
1537 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1540 attr
->u
.val
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1541 if (unit
->dwarf_str_offset
!= 0)
1542 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1545 attr
->u
.val
= read_3_bytes (abfd
, &info_ptr
, info_ptr_end
);
1546 if (unit
->dwarf_str_offset
!= 0)
1547 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1550 attr
->u
.val
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1551 if (unit
->dwarf_str_offset
!= 0)
1552 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1555 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1556 false, info_ptr_end
);
1557 if (unit
->dwarf_str_offset
!= 0)
1558 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1560 case DW_FORM_exprloc
:
1562 amt
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1563 false, info_ptr_end
);
1564 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1565 if (attr
->u
.blk
== NULL
)
1568 case DW_FORM_block1
:
1569 amt
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1570 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1571 if (attr
->u
.blk
== NULL
)
1575 attr
->u
.sval
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1576 true, info_ptr_end
);
1578 case DW_FORM_ref_udata
:
1580 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1581 false, info_ptr_end
);
1584 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1585 false, info_ptr_end
);
1586 if (unit
->dwarf_addr_offset
!= 0)
1587 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
1589 case DW_FORM_indirect
:
1590 form
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1591 false, info_ptr_end
);
1592 if (form
== DW_FORM_implicit_const
)
1593 implicit_const
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1594 true, info_ptr_end
);
1595 info_ptr
= read_attribute_value (attr
, form
, implicit_const
, unit
,
1596 info_ptr
, info_ptr_end
);
1598 case DW_FORM_implicit_const
:
1599 attr
->form
= DW_FORM_sdata
;
1600 attr
->u
.sval
= implicit_const
;
1602 case DW_FORM_data16
:
1603 /* This is really a "constant", but there is no way to store that
1604 so pretend it is a 16 byte block instead. */
1605 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, 16);
1606 if (attr
->u
.blk
== NULL
)
1611 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1613 bfd_set_error (bfd_error_bad_value
);
1619 /* Read an attribute described by an abbreviated attribute. */
1622 read_attribute (struct attribute
* attr
,
1623 struct attr_abbrev
* abbrev
,
1624 struct comp_unit
* unit
,
1625 bfd_byte
* info_ptr
,
1626 bfd_byte
* info_ptr_end
)
1628 attr
->name
= abbrev
->name
;
1629 info_ptr
= read_attribute_value (attr
, abbrev
->form
, abbrev
->implicit_const
,
1630 unit
, info_ptr
, info_ptr_end
);
1634 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1638 non_mangled (int lang
)
1648 case DW_LANG_Cobol74
:
1649 case DW_LANG_Cobol85
:
1650 case DW_LANG_Fortran77
:
1651 case DW_LANG_Pascal83
:
1657 case DW_LANG_Mips_Assembler
:
1662 /* Source line information table routines. */
1664 #define FILE_ALLOC_CHUNK 5
1665 #define DIR_ALLOC_CHUNK 5
1669 struct line_info
* prev_line
;
1673 unsigned int column
;
1674 unsigned int discriminator
;
1675 unsigned char op_index
;
1676 unsigned char end_sequence
; /* End of (sequential) code sequence. */
1687 struct line_sequence
1690 struct line_sequence
* prev_sequence
;
1691 struct line_info
* last_line
; /* Largest VMA. */
1692 struct line_info
** line_info_lookup
;
1693 bfd_size_type num_lines
;
1696 struct line_info_table
1699 unsigned int num_files
;
1700 unsigned int num_dirs
;
1701 unsigned int num_sequences
;
1704 struct fileinfo
* files
;
1705 struct line_sequence
* sequences
;
1706 struct line_info
* lcl_head
; /* Local head; used in 'add_line_info'. */
1709 /* Remember some information about each function. If the function is
1710 inlined (DW_TAG_inlined_subroutine) it may have two additional
1711 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1712 source code location where this function was inlined. */
1716 /* Pointer to previous function in list of all functions. */
1717 struct funcinfo
*prev_func
;
1718 /* Pointer to function one scope higher. */
1719 struct funcinfo
*caller_func
;
1720 /* Source location file name where caller_func inlines this func. */
1722 /* Source location file name. */
1724 /* Source location line number where caller_func inlines this func. */
1726 /* Source location line number. */
1731 struct arange arange
;
1732 /* Where the symbol is defined. */
1734 /* The offset of the funcinfo from the start of the unit. */
1735 uint64_t unit_offset
;
1738 struct lookup_funcinfo
1740 /* Function information corresponding to this lookup table entry. */
1741 struct funcinfo
*funcinfo
;
1743 /* The lowest address for this specific function. */
1746 /* The highest address of this function before the lookup table is sorted.
1747 The highest address of all prior functions after the lookup table is
1748 sorted, which is used for binary search. */
1750 /* Index of this function, used to ensure qsort is stable. */
1756 /* Pointer to previous variable in list of all variables. */
1757 struct varinfo
*prev_var
;
1758 /* The offset of the varinfo from the start of the unit. */
1759 uint64_t unit_offset
;
1760 /* Source location file name. */
1762 /* Source location line number. */
1764 /* The type of this variable. */
1766 /* The name of the variable, if it has one. */
1768 /* The address of the variable. */
1770 /* Where the symbol is defined. */
1772 /* Is this a stack variable? */
1776 /* Return TRUE if NEW_LINE should sort after LINE. */
1779 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
1781 return (new_line
->address
> line
->address
1782 || (new_line
->address
== line
->address
1783 && new_line
->op_index
> line
->op_index
));
1787 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1788 that the list is sorted. Note that the line_info list is sorted from
1789 highest to lowest VMA (with possible duplicates); that is,
1790 line_info->prev_line always accesses an equal or smaller VMA. */
1793 add_line_info (struct line_info_table
*table
,
1795 unsigned char op_index
,
1798 unsigned int column
,
1799 unsigned int discriminator
,
1802 size_t amt
= sizeof (struct line_info
);
1803 struct line_sequence
* seq
= table
->sequences
;
1804 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
1809 /* Set member data of 'info'. */
1810 info
->prev_line
= NULL
;
1811 info
->address
= address
;
1812 info
->op_index
= op_index
;
1814 info
->column
= column
;
1815 info
->discriminator
= discriminator
;
1816 info
->end_sequence
= end_sequence
;
1818 if (filename
&& filename
[0])
1820 info
->filename
= (char *) bfd_alloc (table
->abfd
, strlen (filename
) + 1);
1821 if (info
->filename
== NULL
)
1823 strcpy (info
->filename
, filename
);
1826 info
->filename
= NULL
;
1828 /* Find the correct location for 'info'. Normally we will receive
1829 new line_info data 1) in order and 2) with increasing VMAs.
1830 However some compilers break the rules (cf. decode_line_info) and
1831 so we include some heuristics for quickly finding the correct
1832 location for 'info'. In particular, these heuristics optimize for
1833 the common case in which the VMA sequence that we receive is a
1834 list of locally sorted VMAs such as
1835 p...z a...j (where a < j < p < z)
1837 Note: table->lcl_head is used to head an *actual* or *possible*
1838 sub-sequence within the list (such as a...j) that is not directly
1839 headed by table->last_line
1841 Note: we may receive duplicate entries from 'decode_line_info'. */
1844 && seq
->last_line
->address
== address
1845 && seq
->last_line
->op_index
== op_index
1846 && seq
->last_line
->end_sequence
== end_sequence
)
1848 /* We only keep the last entry with the same address and end
1849 sequence. See PR ld/4986. */
1850 if (table
->lcl_head
== seq
->last_line
)
1851 table
->lcl_head
= info
;
1852 info
->prev_line
= seq
->last_line
->prev_line
;
1853 seq
->last_line
= info
;
1855 else if (!seq
|| seq
->last_line
->end_sequence
)
1857 /* Start a new line sequence. */
1858 amt
= sizeof (struct line_sequence
);
1859 seq
= (struct line_sequence
*) bfd_malloc (amt
);
1862 seq
->low_pc
= address
;
1863 seq
->prev_sequence
= table
->sequences
;
1864 seq
->last_line
= info
;
1865 table
->lcl_head
= info
;
1866 table
->sequences
= seq
;
1867 table
->num_sequences
++;
1869 else if (info
->end_sequence
1870 || new_line_sorts_after (info
, seq
->last_line
))
1872 /* Normal case: add 'info' to the beginning of the current sequence. */
1873 info
->prev_line
= seq
->last_line
;
1874 seq
->last_line
= info
;
1876 /* lcl_head: initialize to head a *possible* sequence at the end. */
1877 if (!table
->lcl_head
)
1878 table
->lcl_head
= info
;
1880 else if (!new_line_sorts_after (info
, table
->lcl_head
)
1881 && (!table
->lcl_head
->prev_line
1882 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
1884 /* Abnormal but easy: lcl_head is the head of 'info'. */
1885 info
->prev_line
= table
->lcl_head
->prev_line
;
1886 table
->lcl_head
->prev_line
= info
;
1890 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1891 are valid heads for 'info'. Reset 'lcl_head'. */
1892 struct line_info
* li2
= seq
->last_line
; /* Always non-NULL. */
1893 struct line_info
* li1
= li2
->prev_line
;
1897 if (!new_line_sorts_after (info
, li2
)
1898 && new_line_sorts_after (info
, li1
))
1901 li2
= li1
; /* always non-NULL */
1902 li1
= li1
->prev_line
;
1904 table
->lcl_head
= li2
;
1905 info
->prev_line
= table
->lcl_head
->prev_line
;
1906 table
->lcl_head
->prev_line
= info
;
1907 if (address
< seq
->low_pc
)
1908 seq
->low_pc
= address
;
1913 /* Extract a fully qualified filename from a line info table.
1914 The returned string has been malloc'ed and it is the caller's
1915 responsibility to free it. */
1918 concat_filename (struct line_info_table
*table
, unsigned int file
)
1922 if (table
== NULL
|| file
- 1 >= table
->num_files
)
1924 /* FILE == 0 means unknown. */
1927 (_("DWARF error: mangled line number section (bad file number)"));
1928 return strdup ("<unknown>");
1931 filename
= table
->files
[file
- 1].name
;
1932 if (filename
== NULL
)
1933 return strdup ("<unknown>");
1935 if (!IS_ABSOLUTE_PATH (filename
))
1937 char *dir_name
= NULL
;
1938 char *subdir_name
= NULL
;
1942 if (table
->files
[file
- 1].dir
1943 /* PR 17512: file: 0317e960. */
1944 && table
->files
[file
- 1].dir
<= table
->num_dirs
1945 /* PR 17512: file: 7f3d2e4b. */
1946 && table
->dirs
!= NULL
)
1947 subdir_name
= table
->dirs
[table
->files
[file
- 1].dir
- 1];
1949 if (!subdir_name
|| !IS_ABSOLUTE_PATH (subdir_name
))
1950 dir_name
= table
->comp_dir
;
1954 dir_name
= subdir_name
;
1959 return strdup (filename
);
1961 len
= strlen (dir_name
) + strlen (filename
) + 2;
1965 len
+= strlen (subdir_name
) + 1;
1966 name
= (char *) bfd_malloc (len
);
1968 sprintf (name
, "%s/%s/%s", dir_name
, subdir_name
, filename
);
1972 name
= (char *) bfd_malloc (len
);
1974 sprintf (name
, "%s/%s", dir_name
, filename
);
1980 return strdup (filename
);
1983 /* Number of bits in a bfd_vma. */
1984 #define VMA_BITS (8 * sizeof (bfd_vma))
1986 /* Check whether [low1, high1) can be combined with [low2, high2),
1987 i.e., they touch or overlap. */
1988 static bool ranges_overlap (bfd_vma low1
,
1993 if (low1
== low2
|| high1
== high2
)
1996 /* Sort so that low1 is below low2. */
2010 /* We touch iff low2 == high1.
2011 We overlap iff low2 is within [low1, high1). */
2012 return (low2
<= high1
);
2015 /* Insert an address range in the trie mapping addresses to compilation units.
2016 Will return the new trie node (usually the same as is being sent in, but
2017 in case of a leaf-to-interior conversion, or expansion of a leaf, it may be
2018 different), or NULL on failure.
2020 static struct trie_node
*insert_arange_in_trie(bfd
*abfd
,
2021 struct trie_node
*trie
,
2023 unsigned int trie_pc_bits
,
2024 struct comp_unit
*unit
,
2028 bfd_vma clamped_low_pc
, clamped_high_pc
;
2029 int ch
, from_ch
, to_ch
;
2030 bool is_full_leaf
= false;
2032 /* See if we can extend any of the existing ranges. This merging
2033 isn't perfect (if merging opens up the possibility of merging two existing
2034 ranges, we won't find them), but it takes the majority of the cases. */
2035 if (trie
->num_room_in_leaf
> 0)
2037 struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
2040 for (i
= 0; i
< leaf
->num_stored_in_leaf
; ++i
)
2042 if (leaf
->ranges
[i
].unit
== unit
&&
2043 ranges_overlap(low_pc
, high_pc
,
2044 leaf
->ranges
[i
].low_pc
, leaf
->ranges
[i
].high_pc
))
2046 if (low_pc
< leaf
->ranges
[i
].low_pc
)
2047 leaf
->ranges
[i
].low_pc
= low_pc
;
2048 if (high_pc
> leaf
->ranges
[i
].high_pc
)
2049 leaf
->ranges
[i
].high_pc
= high_pc
;
2054 is_full_leaf
= leaf
->num_stored_in_leaf
== trie
->num_room_in_leaf
;
2057 /* If we're a leaf with no more room and we're _not_ at the bottom,
2058 convert to an interior node. */
2059 if (is_full_leaf
&& trie_pc_bits
< VMA_BITS
)
2061 const struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
2064 trie
= bfd_zalloc (abfd
, sizeof (struct trie_interior
));
2067 is_full_leaf
= false;
2069 /* TODO: If we wanted to save a little more memory at the cost of
2070 complexity, we could have reused the old leaf node as one of the
2071 children of the new interior node, instead of throwing it away. */
2072 for (i
= 0; i
< leaf
->num_stored_in_leaf
; ++i
)
2074 if (!insert_arange_in_trie (abfd
, trie
, trie_pc
, trie_pc_bits
,
2075 leaf
->ranges
[i
].unit
, leaf
->ranges
[i
].low_pc
,
2076 leaf
->ranges
[i
].high_pc
))
2081 /* If we're a leaf with no more room and we _are_ at the bottom,
2082 we have no choice but to just make it larger. */
2085 const struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
2086 unsigned int new_room_in_leaf
= trie
->num_room_in_leaf
* 2;
2087 struct trie_leaf
*new_leaf
;
2089 new_leaf
= bfd_zalloc (abfd
,
2090 sizeof (struct trie_leaf
) +
2091 (new_room_in_leaf
- TRIE_LEAF_SIZE
) * sizeof (leaf
->ranges
[0]));
2092 new_leaf
->head
.num_room_in_leaf
= new_room_in_leaf
;
2093 new_leaf
->num_stored_in_leaf
= leaf
->num_stored_in_leaf
;
2095 memcpy (new_leaf
->ranges
,
2097 leaf
->num_stored_in_leaf
* sizeof (leaf
->ranges
[0]));
2098 trie
= &new_leaf
->head
;
2099 is_full_leaf
= false;
2101 /* Now the insert below will go through. */
2104 /* If we're a leaf (now with room), we can just insert at the end. */
2105 if (trie
->num_room_in_leaf
> 0)
2107 struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
2109 unsigned int i
= leaf
->num_stored_in_leaf
++;
2110 leaf
->ranges
[i
].unit
= unit
;
2111 leaf
->ranges
[i
].low_pc
= low_pc
;
2112 leaf
->ranges
[i
].high_pc
= high_pc
;
2116 /* Now we are definitely an interior node, so recurse into all
2117 the relevant buckets. */
2119 /* Clamp the range to the current trie bucket. */
2120 clamped_low_pc
= low_pc
;
2121 clamped_high_pc
= high_pc
;
2122 if (trie_pc_bits
> 0)
2124 bfd_vma bucket_high_pc
=
2125 trie_pc
+ ((bfd_vma
)-1 >> trie_pc_bits
); /* Inclusive. */
2126 if (clamped_low_pc
< trie_pc
)
2127 clamped_low_pc
= trie_pc
;
2128 if (clamped_high_pc
> bucket_high_pc
)
2129 clamped_high_pc
= bucket_high_pc
;
2132 /* Insert the ranges in all buckets that it spans. */
2133 from_ch
= (clamped_low_pc
>> (VMA_BITS
- trie_pc_bits
- 8)) & 0xff;
2134 to_ch
= ((clamped_high_pc
- 1) >> (VMA_BITS
- trie_pc_bits
- 8)) & 0xff;
2135 for (ch
= from_ch
; ch
<= to_ch
; ++ch
)
2137 struct trie_interior
*interior
= (struct trie_interior
*) trie
;
2138 struct trie_node
*child
= interior
->children
[ch
];
2142 child
= alloc_trie_leaf (abfd
);
2146 child
= insert_arange_in_trie (abfd
,
2148 trie_pc
+ ((bfd_vma
)ch
<< (VMA_BITS
- trie_pc_bits
- 8)),
2156 interior
->children
[ch
] = child
;
2164 arange_add (struct comp_unit
*unit
, struct arange
*first_arange
,
2165 struct trie_node
**trie_root
, bfd_vma low_pc
, bfd_vma high_pc
)
2167 struct arange
*arange
;
2169 /* Ignore empty ranges. */
2170 if (low_pc
== high_pc
)
2173 if (trie_root
!= NULL
)
2175 *trie_root
= insert_arange_in_trie (unit
->file
->bfd_ptr
,
2182 if (*trie_root
== NULL
)
2186 /* If the first arange is empty, use it. */
2187 if (first_arange
->high
== 0)
2189 first_arange
->low
= low_pc
;
2190 first_arange
->high
= high_pc
;
2194 /* Next see if we can cheaply extend an existing range. */
2195 arange
= first_arange
;
2198 if (low_pc
== arange
->high
)
2200 arange
->high
= high_pc
;
2203 if (high_pc
== arange
->low
)
2205 arange
->low
= low_pc
;
2208 arange
= arange
->next
;
2212 /* Need to allocate a new arange and insert it into the arange list.
2213 Order isn't significant, so just insert after the first arange. */
2214 arange
= (struct arange
*) bfd_alloc (unit
->abfd
, sizeof (*arange
));
2217 arange
->low
= low_pc
;
2218 arange
->high
= high_pc
;
2219 arange
->next
= first_arange
->next
;
2220 first_arange
->next
= arange
;
2224 /* Compare function for line sequences. */
2227 compare_sequences (const void* a
, const void* b
)
2229 const struct line_sequence
* seq1
= a
;
2230 const struct line_sequence
* seq2
= b
;
2232 /* Sort by low_pc as the primary key. */
2233 if (seq1
->low_pc
< seq2
->low_pc
)
2235 if (seq1
->low_pc
> seq2
->low_pc
)
2238 /* If low_pc values are equal, sort in reverse order of
2239 high_pc, so that the largest region comes first. */
2240 if (seq1
->last_line
->address
< seq2
->last_line
->address
)
2242 if (seq1
->last_line
->address
> seq2
->last_line
->address
)
2245 if (seq1
->last_line
->op_index
< seq2
->last_line
->op_index
)
2247 if (seq1
->last_line
->op_index
> seq2
->last_line
->op_index
)
2250 /* num_lines is initially an index, to make the sort stable. */
2251 if (seq1
->num_lines
< seq2
->num_lines
)
2253 if (seq1
->num_lines
> seq2
->num_lines
)
2258 /* Construct the line information table for quick lookup. */
2261 build_line_info_table (struct line_info_table
* table
,
2262 struct line_sequence
* seq
)
2265 struct line_info
**line_info_lookup
;
2266 struct line_info
*each_line
;
2267 unsigned int num_lines
;
2268 unsigned int line_index
;
2270 if (seq
->line_info_lookup
!= NULL
)
2273 /* Count the number of line information entries. We could do this while
2274 scanning the debug information, but some entries may be added via
2275 lcl_head without having a sequence handy to increment the number of
2278 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
2281 seq
->num_lines
= num_lines
;
2285 /* Allocate space for the line information lookup table. */
2286 amt
= sizeof (struct line_info
*) * num_lines
;
2287 line_info_lookup
= (struct line_info
**) bfd_alloc (table
->abfd
, amt
);
2288 seq
->line_info_lookup
= line_info_lookup
;
2289 if (line_info_lookup
== NULL
)
2292 /* Create the line information lookup table. */
2293 line_index
= num_lines
;
2294 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
2295 line_info_lookup
[--line_index
] = each_line
;
2297 BFD_ASSERT (line_index
== 0);
2301 /* Sort the line sequences for quick lookup. */
2304 sort_line_sequences (struct line_info_table
* table
)
2307 struct line_sequence
*sequences
;
2308 struct line_sequence
*seq
;
2310 unsigned int num_sequences
= table
->num_sequences
;
2311 bfd_vma last_high_pc
;
2313 if (num_sequences
== 0)
2316 /* Allocate space for an array of sequences. */
2317 amt
= sizeof (struct line_sequence
) * num_sequences
;
2318 sequences
= (struct line_sequence
*) bfd_alloc (table
->abfd
, amt
);
2319 if (sequences
== NULL
)
2322 /* Copy the linked list into the array, freeing the original nodes. */
2323 seq
= table
->sequences
;
2324 for (n
= 0; n
< num_sequences
; n
++)
2326 struct line_sequence
* last_seq
= seq
;
2329 sequences
[n
].low_pc
= seq
->low_pc
;
2330 sequences
[n
].prev_sequence
= NULL
;
2331 sequences
[n
].last_line
= seq
->last_line
;
2332 sequences
[n
].line_info_lookup
= NULL
;
2333 sequences
[n
].num_lines
= n
;
2334 seq
= seq
->prev_sequence
;
2337 BFD_ASSERT (seq
== NULL
);
2339 qsort (sequences
, n
, sizeof (struct line_sequence
), compare_sequences
);
2341 /* Make the list binary-searchable by trimming overlapping entries
2342 and removing nested entries. */
2344 last_high_pc
= sequences
[0].last_line
->address
;
2345 for (n
= 1; n
< table
->num_sequences
; n
++)
2347 if (sequences
[n
].low_pc
< last_high_pc
)
2349 if (sequences
[n
].last_line
->address
<= last_high_pc
)
2350 /* Skip nested entries. */
2353 /* Trim overlapping entries. */
2354 sequences
[n
].low_pc
= last_high_pc
;
2356 last_high_pc
= sequences
[n
].last_line
->address
;
2357 if (n
> num_sequences
)
2359 /* Close up the gap. */
2360 sequences
[num_sequences
].low_pc
= sequences
[n
].low_pc
;
2361 sequences
[num_sequences
].last_line
= sequences
[n
].last_line
;
2366 table
->sequences
= sequences
;
2367 table
->num_sequences
= num_sequences
;
2371 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
2374 line_info_add_include_dir (struct line_info_table
*table
, char *cur_dir
)
2376 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
2381 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
2382 amt
*= sizeof (char *);
2384 tmp
= (char **) bfd_realloc (table
->dirs
, amt
);
2390 table
->dirs
[table
->num_dirs
++] = cur_dir
;
2395 line_info_add_include_dir_stub (struct line_info_table
*table
, char *cur_dir
,
2396 unsigned int dir ATTRIBUTE_UNUSED
,
2397 unsigned int xtime ATTRIBUTE_UNUSED
,
2398 unsigned int size ATTRIBUTE_UNUSED
)
2400 return line_info_add_include_dir (table
, cur_dir
);
2403 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
2406 line_info_add_file_name (struct line_info_table
*table
, char *cur_file
,
2407 unsigned int dir
, unsigned int xtime
,
2410 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
2412 struct fileinfo
*tmp
;
2415 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
2416 amt
*= sizeof (struct fileinfo
);
2418 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
2424 table
->files
[table
->num_files
].name
= cur_file
;
2425 table
->files
[table
->num_files
].dir
= dir
;
2426 table
->files
[table
->num_files
].time
= xtime
;
2427 table
->files
[table
->num_files
].size
= size
;
2432 /* Read directory or file name entry format, starting with byte of
2433 format count entries, ULEB128 pairs of entry formats, ULEB128 of
2434 entries count and the entries themselves in the described entry
2438 read_formatted_entries (struct comp_unit
*unit
, bfd_byte
**bufp
,
2439 bfd_byte
*buf_end
, struct line_info_table
*table
,
2440 bool (*callback
) (struct line_info_table
*table
,
2446 bfd
*abfd
= unit
->abfd
;
2447 bfd_byte format_count
, formati
;
2448 bfd_vma data_count
, datai
;
2449 bfd_byte
*buf
= *bufp
;
2450 bfd_byte
*format_header_data
;
2452 format_count
= read_1_byte (abfd
, &buf
, buf_end
);
2453 format_header_data
= buf
;
2454 for (formati
= 0; formati
< format_count
; formati
++)
2456 _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2457 _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2460 data_count
= _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2461 if (format_count
== 0 && data_count
!= 0)
2463 _bfd_error_handler (_("DWARF error: zero format count"));
2464 bfd_set_error (bfd_error_bad_value
);
2468 /* PR 22210. Paranoia check. Don't bother running the loop
2469 if we know that we are going to run out of buffer. */
2470 if (data_count
> (bfd_vma
) (buf_end
- buf
))
2473 (_("DWARF error: data count (%" PRIx64
") larger than buffer size"),
2474 (uint64_t) data_count
);
2475 bfd_set_error (bfd_error_bad_value
);
2479 for (datai
= 0; datai
< data_count
; datai
++)
2481 bfd_byte
*format
= format_header_data
;
2484 memset (&fe
, 0, sizeof fe
);
2485 for (formati
= 0; formati
< format_count
; formati
++)
2487 bfd_vma content_type
, form
;
2489 char **stringp
= &string_trash
;
2490 unsigned int uint_trash
, *uintp
= &uint_trash
;
2491 struct attribute attr
;
2493 content_type
= _bfd_safe_read_leb128 (abfd
, &format
, false, buf_end
);
2494 switch (content_type
)
2499 case DW_LNCT_directory_index
:
2502 case DW_LNCT_timestamp
:
2512 (_("DWARF error: unknown format content type %" PRIu64
),
2513 (uint64_t) content_type
);
2514 bfd_set_error (bfd_error_bad_value
);
2518 form
= _bfd_safe_read_leb128 (abfd
, &format
, false, buf_end
);
2519 buf
= read_attribute_value (&attr
, form
, 0, unit
, buf
, buf_end
);
2524 case DW_FORM_string
:
2525 case DW_FORM_line_strp
:
2531 *stringp
= attr
.u
.str
;
2539 *uintp
= attr
.u
.val
;
2542 case DW_FORM_data16
:
2543 /* MD5 data is in the attr.blk, but we are ignoring those. */
2548 /* Skip the first "zero entry", which is the compilation dir/file. */
2550 if (!callback (table
, fe
.name
, fe
.dir
, fe
.time
, fe
.size
))
2558 /* Decode the line number information for UNIT. */
2560 static struct line_info_table
*
2561 decode_line_info (struct comp_unit
*unit
)
2563 bfd
*abfd
= unit
->abfd
;
2564 struct dwarf2_debug
*stash
= unit
->stash
;
2565 struct dwarf2_debug_file
*file
= unit
->file
;
2566 struct line_info_table
* table
;
2569 struct line_head lh
;
2570 unsigned int i
, offset_size
;
2571 char *cur_file
, *cur_dir
;
2572 unsigned char op_code
, extended_op
, adj_opcode
;
2573 unsigned int exop_len
;
2576 if (unit
->line_offset
== 0 && file
->line_table
)
2577 return file
->line_table
;
2579 if (! read_section (abfd
, &stash
->debug_sections
[debug_line
],
2580 file
->syms
, unit
->line_offset
,
2581 &file
->dwarf_line_buffer
, &file
->dwarf_line_size
))
2584 if (file
->dwarf_line_size
< 16)
2587 (_("DWARF error: line info section is too small (%" PRId64
")"),
2588 (int64_t) file
->dwarf_line_size
);
2589 bfd_set_error (bfd_error_bad_value
);
2592 line_ptr
= file
->dwarf_line_buffer
+ unit
->line_offset
;
2593 line_end
= file
->dwarf_line_buffer
+ file
->dwarf_line_size
;
2595 /* Read in the prologue. */
2596 lh
.total_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2598 if (lh
.total_length
== 0xffffffff)
2600 lh
.total_length
= read_8_bytes (abfd
, &line_ptr
, line_end
);
2603 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
2605 /* Handle (non-standard) 64-bit DWARF2 formats. */
2606 lh
.total_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2610 if (lh
.total_length
> (size_t) (line_end
- line_ptr
))
2613 /* xgettext: c-format */
2614 (_("DWARF error: line info data is bigger (%#" PRIx64
")"
2615 " than the space remaining in the section (%#lx)"),
2616 (uint64_t) lh
.total_length
, (unsigned long) (line_end
- line_ptr
));
2617 bfd_set_error (bfd_error_bad_value
);
2621 line_end
= line_ptr
+ lh
.total_length
;
2623 lh
.version
= read_2_bytes (abfd
, &line_ptr
, line_end
);
2624 if (lh
.version
< 2 || lh
.version
> 5)
2627 (_("DWARF error: unhandled .debug_line version %d"), lh
.version
);
2628 bfd_set_error (bfd_error_bad_value
);
2632 if (line_ptr
+ offset_size
+ (lh
.version
>= 5 ? 8 : (lh
.version
>= 4 ? 6 : 5))
2636 (_("DWARF error: ran out of room reading prologue"));
2637 bfd_set_error (bfd_error_bad_value
);
2641 if (lh
.version
>= 5)
2643 unsigned int segment_selector_size
;
2645 /* Skip address size. */
2646 read_1_byte (abfd
, &line_ptr
, line_end
);
2648 segment_selector_size
= read_1_byte (abfd
, &line_ptr
, line_end
);
2649 if (segment_selector_size
!= 0)
2652 (_("DWARF error: line info unsupported segment selector size %u"),
2653 segment_selector_size
);
2654 bfd_set_error (bfd_error_bad_value
);
2659 if (offset_size
== 4)
2660 lh
.prologue_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2662 lh
.prologue_length
= read_8_bytes (abfd
, &line_ptr
, line_end
);
2664 lh
.minimum_instruction_length
= read_1_byte (abfd
, &line_ptr
, line_end
);
2666 if (lh
.version
>= 4)
2667 lh
.maximum_ops_per_insn
= read_1_byte (abfd
, &line_ptr
, line_end
);
2669 lh
.maximum_ops_per_insn
= 1;
2671 if (lh
.maximum_ops_per_insn
== 0)
2674 (_("DWARF error: invalid maximum operations per instruction"));
2675 bfd_set_error (bfd_error_bad_value
);
2679 lh
.default_is_stmt
= read_1_byte (abfd
, &line_ptr
, line_end
);
2680 lh
.line_base
= read_1_signed_byte (abfd
, &line_ptr
, line_end
);
2681 lh
.line_range
= read_1_byte (abfd
, &line_ptr
, line_end
);
2682 lh
.opcode_base
= read_1_byte (abfd
, &line_ptr
, line_end
);
2684 if (line_ptr
+ (lh
.opcode_base
- 1) >= line_end
)
2686 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2687 bfd_set_error (bfd_error_bad_value
);
2691 amt
= lh
.opcode_base
* sizeof (unsigned char);
2692 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
2694 lh
.standard_opcode_lengths
[0] = 1;
2696 for (i
= 1; i
< lh
.opcode_base
; ++i
)
2697 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, &line_ptr
, line_end
);
2699 amt
= sizeof (struct line_info_table
);
2700 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
2704 table
->comp_dir
= unit
->comp_dir
;
2706 table
->num_files
= 0;
2707 table
->files
= NULL
;
2709 table
->num_dirs
= 0;
2712 table
->num_sequences
= 0;
2713 table
->sequences
= NULL
;
2715 table
->lcl_head
= NULL
;
2717 if (lh
.version
>= 5)
2719 /* Read directory table. */
2720 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2721 line_info_add_include_dir_stub
))
2724 /* Read file name table. */
2725 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2726 line_info_add_file_name
))
2731 /* Read directory table. */
2732 while ((cur_dir
= read_string (&line_ptr
, line_end
)) != NULL
)
2734 if (!line_info_add_include_dir (table
, cur_dir
))
2738 /* Read file name table. */
2739 while ((cur_file
= read_string (&line_ptr
, line_end
)) != NULL
)
2741 unsigned int dir
, xtime
, size
;
2743 dir
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2744 xtime
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2745 size
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2747 if (!line_info_add_file_name (table
, cur_file
, dir
, xtime
, size
))
2752 /* Read the statement sequences until there's nothing left. */
2753 while (line_ptr
< line_end
)
2755 /* State machine registers. */
2756 bfd_vma address
= 0;
2757 unsigned char op_index
= 0;
2758 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
2759 unsigned int line
= 1;
2760 unsigned int column
= 0;
2761 unsigned int discriminator
= 0;
2762 int is_stmt
= lh
.default_is_stmt
;
2763 int end_sequence
= 0;
2764 unsigned int dir
, xtime
, size
;
2765 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2766 compilers generate address sequences that are wildly out of
2767 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2768 for ia64-Linux). Thus, to determine the low and high
2769 address, we must compare on every DW_LNS_copy, etc. */
2770 bfd_vma low_pc
= (bfd_vma
) -1;
2771 bfd_vma high_pc
= 0;
2773 /* Decode the table. */
2774 while (!end_sequence
&& line_ptr
< line_end
)
2776 op_code
= read_1_byte (abfd
, &line_ptr
, line_end
);
2778 if (op_code
>= lh
.opcode_base
)
2780 /* Special operand. */
2781 adj_opcode
= op_code
- lh
.opcode_base
;
2782 if (lh
.line_range
== 0)
2784 if (lh
.maximum_ops_per_insn
== 1)
2785 address
+= (adj_opcode
/ lh
.line_range
2786 * lh
.minimum_instruction_length
);
2789 address
+= ((op_index
+ adj_opcode
/ lh
.line_range
)
2790 / lh
.maximum_ops_per_insn
2791 * lh
.minimum_instruction_length
);
2792 op_index
= ((op_index
+ adj_opcode
/ lh
.line_range
)
2793 % lh
.maximum_ops_per_insn
);
2795 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
2796 /* Append row to matrix using current values. */
2797 if (!add_line_info (table
, address
, op_index
, filename
,
2798 line
, column
, discriminator
, 0))
2801 if (address
< low_pc
)
2803 if (address
> high_pc
)
2806 else switch (op_code
)
2808 case DW_LNS_extended_op
:
2809 exop_len
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2811 extended_op
= read_1_byte (abfd
, &line_ptr
, line_end
);
2813 switch (extended_op
)
2815 case DW_LNE_end_sequence
:
2817 if (!add_line_info (table
, address
, op_index
, filename
, line
,
2818 column
, discriminator
, end_sequence
))
2821 if (address
< low_pc
)
2823 if (address
> high_pc
)
2825 if (!arange_add (unit
, &unit
->arange
, &unit
->file
->trie_root
,
2829 case DW_LNE_set_address
:
2830 address
= read_address (unit
, &line_ptr
, line_end
);
2833 case DW_LNE_define_file
:
2834 cur_file
= read_string (&line_ptr
, line_end
);
2835 dir
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2837 xtime
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2839 size
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2841 if (!line_info_add_file_name (table
, cur_file
, dir
,
2845 case DW_LNE_set_discriminator
:
2847 _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2850 case DW_LNE_HP_source_file_correlation
:
2851 line_ptr
+= exop_len
- 1;
2855 (_("DWARF error: mangled line number section"));
2856 bfd_set_error (bfd_error_bad_value
);
2863 if (!add_line_info (table
, address
, op_index
,
2864 filename
, line
, column
, discriminator
, 0))
2867 if (address
< low_pc
)
2869 if (address
> high_pc
)
2872 case DW_LNS_advance_pc
:
2873 if (lh
.maximum_ops_per_insn
== 1)
2874 address
+= (lh
.minimum_instruction_length
2875 * _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2879 bfd_vma adjust
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2881 address
= ((op_index
+ adjust
) / lh
.maximum_ops_per_insn
2882 * lh
.minimum_instruction_length
);
2883 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2886 case DW_LNS_advance_line
:
2887 line
+= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2890 case DW_LNS_set_file
:
2892 unsigned int filenum
;
2894 /* The file and directory tables are 0
2895 based, the references are 1 based. */
2896 filenum
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2899 filename
= concat_filename (table
, filenum
);
2902 case DW_LNS_set_column
:
2903 column
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2906 case DW_LNS_negate_stmt
:
2907 is_stmt
= (!is_stmt
);
2909 case DW_LNS_set_basic_block
:
2911 case DW_LNS_const_add_pc
:
2912 if (lh
.line_range
== 0)
2914 if (lh
.maximum_ops_per_insn
== 1)
2915 address
+= (lh
.minimum_instruction_length
2916 * ((255 - lh
.opcode_base
) / lh
.line_range
));
2919 bfd_vma adjust
= ((255 - lh
.opcode_base
) / lh
.line_range
);
2920 address
+= (lh
.minimum_instruction_length
2921 * ((op_index
+ adjust
)
2922 / lh
.maximum_ops_per_insn
));
2923 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2926 case DW_LNS_fixed_advance_pc
:
2927 address
+= read_2_bytes (abfd
, &line_ptr
, line_end
);
2931 /* Unknown standard opcode, ignore it. */
2932 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
2933 (void) _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2942 if (unit
->line_offset
== 0)
2943 file
->line_table
= table
;
2944 if (sort_line_sequences (table
))
2948 while (table
->sequences
!= NULL
)
2950 struct line_sequence
* seq
= table
->sequences
;
2951 table
->sequences
= table
->sequences
->prev_sequence
;
2954 free (table
->files
);
2959 /* If ADDR is within TABLE set the output parameters and return TRUE,
2960 otherwise set *FILENAME_PTR to NULL and return FALSE.
2961 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2962 are pointers to the objects to be filled in. */
2965 lookup_address_in_line_info_table (struct line_info_table
*table
,
2967 const char **filename_ptr
,
2968 unsigned int *linenumber_ptr
,
2969 unsigned int *discriminator_ptr
)
2971 struct line_sequence
*seq
= NULL
;
2972 struct line_info
*info
;
2975 /* Binary search the array of sequences. */
2977 high
= table
->num_sequences
;
2980 mid
= (low
+ high
) / 2;
2981 seq
= &table
->sequences
[mid
];
2982 if (addr
< seq
->low_pc
)
2984 else if (addr
>= seq
->last_line
->address
)
2990 /* Check for a valid sequence. */
2991 if (!seq
|| addr
< seq
->low_pc
|| addr
>= seq
->last_line
->address
)
2994 if (!build_line_info_table (table
, seq
))
2997 /* Binary search the array of line information. */
2999 high
= seq
->num_lines
;
3003 mid
= (low
+ high
) / 2;
3004 info
= seq
->line_info_lookup
[mid
];
3005 if (addr
< info
->address
)
3007 else if (addr
>= seq
->line_info_lookup
[mid
+ 1]->address
)
3013 /* Check for a valid line information entry. */
3015 && addr
>= info
->address
3016 && addr
< seq
->line_info_lookup
[mid
+ 1]->address
3017 && !(info
->end_sequence
|| info
== seq
->last_line
))
3019 *filename_ptr
= info
->filename
;
3020 *linenumber_ptr
= info
->line
;
3021 if (discriminator_ptr
)
3022 *discriminator_ptr
= info
->discriminator
;
3027 *filename_ptr
= NULL
;
3031 /* Read in the .debug_ranges section for future reference. */
3034 read_debug_ranges (struct comp_unit
* unit
)
3036 struct dwarf2_debug
*stash
= unit
->stash
;
3037 struct dwarf2_debug_file
*file
= unit
->file
;
3039 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_ranges
],
3041 &file
->dwarf_ranges_buffer
, &file
->dwarf_ranges_size
);
3044 /* Read in the .debug_rnglists section for future reference. */
3047 read_debug_rnglists (struct comp_unit
* unit
)
3049 struct dwarf2_debug
*stash
= unit
->stash
;
3050 struct dwarf2_debug_file
*file
= unit
->file
;
3052 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_rnglists
],
3054 &file
->dwarf_rnglists_buffer
, &file
->dwarf_rnglists_size
);
3057 /* Function table functions. */
3060 compare_lookup_funcinfos (const void * a
, const void * b
)
3062 const struct lookup_funcinfo
* lookup1
= a
;
3063 const struct lookup_funcinfo
* lookup2
= b
;
3065 if (lookup1
->low_addr
< lookup2
->low_addr
)
3067 if (lookup1
->low_addr
> lookup2
->low_addr
)
3069 if (lookup1
->high_addr
< lookup2
->high_addr
)
3071 if (lookup1
->high_addr
> lookup2
->high_addr
)
3074 if (lookup1
->idx
< lookup2
->idx
)
3076 if (lookup1
->idx
> lookup2
->idx
)
3082 build_lookup_funcinfo_table (struct comp_unit
* unit
)
3084 struct lookup_funcinfo
*lookup_funcinfo_table
= unit
->lookup_funcinfo_table
;
3085 unsigned int number_of_functions
= unit
->number_of_functions
;
3086 struct funcinfo
*each
;
3087 struct lookup_funcinfo
*entry
;
3089 struct arange
*range
;
3090 bfd_vma low_addr
, high_addr
;
3092 if (lookup_funcinfo_table
|| number_of_functions
== 0)
3095 /* Create the function info lookup table. */
3096 lookup_funcinfo_table
= (struct lookup_funcinfo
*)
3097 bfd_malloc (number_of_functions
* sizeof (struct lookup_funcinfo
));
3098 if (lookup_funcinfo_table
== NULL
)
3101 /* Populate the function info lookup table. */
3102 func_index
= number_of_functions
;
3103 for (each
= unit
->function_table
; each
; each
= each
->prev_func
)
3105 entry
= &lookup_funcinfo_table
[--func_index
];
3106 entry
->funcinfo
= each
;
3107 entry
->idx
= func_index
;
3109 /* Calculate the lowest and highest address for this function entry. */
3110 low_addr
= entry
->funcinfo
->arange
.low
;
3111 high_addr
= entry
->funcinfo
->arange
.high
;
3113 for (range
= entry
->funcinfo
->arange
.next
; range
; range
= range
->next
)
3115 if (range
->low
< low_addr
)
3116 low_addr
= range
->low
;
3117 if (range
->high
> high_addr
)
3118 high_addr
= range
->high
;
3121 entry
->low_addr
= low_addr
;
3122 entry
->high_addr
= high_addr
;
3125 BFD_ASSERT (func_index
== 0);
3127 /* Sort the function by address. */
3128 qsort (lookup_funcinfo_table
,
3129 number_of_functions
,
3130 sizeof (struct lookup_funcinfo
),
3131 compare_lookup_funcinfos
);
3133 /* Calculate the high watermark for each function in the lookup table. */
3134 high_addr
= lookup_funcinfo_table
[0].high_addr
;
3135 for (func_index
= 1; func_index
< number_of_functions
; func_index
++)
3137 entry
= &lookup_funcinfo_table
[func_index
];
3138 if (entry
->high_addr
> high_addr
)
3139 high_addr
= entry
->high_addr
;
3141 entry
->high_addr
= high_addr
;
3144 unit
->lookup_funcinfo_table
= lookup_funcinfo_table
;
3148 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
3149 TRUE. Note that we need to find the function that has the smallest range
3150 that contains ADDR, to handle inlined functions without depending upon
3151 them being ordered in TABLE by increasing range. */
3154 lookup_address_in_function_table (struct comp_unit
*unit
,
3156 struct funcinfo
**function_ptr
)
3158 unsigned int number_of_functions
= unit
->number_of_functions
;
3159 struct lookup_funcinfo
* lookup_funcinfo
= NULL
;
3160 struct funcinfo
* funcinfo
= NULL
;
3161 struct funcinfo
* best_fit
= NULL
;
3162 bfd_vma best_fit_len
= 0;
3163 bfd_size_type low
, high
, mid
, first
;
3164 struct arange
*arange
;
3166 if (number_of_functions
== 0)
3169 if (!build_lookup_funcinfo_table (unit
))
3172 if (unit
->lookup_funcinfo_table
[number_of_functions
- 1].high_addr
< addr
)
3175 /* Find the first function in the lookup table which may contain the
3176 specified address. */
3178 high
= number_of_functions
;
3182 mid
= (low
+ high
) / 2;
3183 lookup_funcinfo
= &unit
->lookup_funcinfo_table
[mid
];
3184 if (addr
< lookup_funcinfo
->low_addr
)
3186 else if (addr
>= lookup_funcinfo
->high_addr
)
3192 /* Find the 'best' match for the address. The prior algorithm defined the
3193 best match as the function with the smallest address range containing
3194 the specified address. This definition should probably be changed to the
3195 innermost inline routine containing the address, but right now we want
3196 to get the same results we did before. */
3197 while (first
< number_of_functions
)
3199 if (addr
< unit
->lookup_funcinfo_table
[first
].low_addr
)
3201 funcinfo
= unit
->lookup_funcinfo_table
[first
].funcinfo
;
3203 for (arange
= &funcinfo
->arange
; arange
; arange
= arange
->next
)
3205 if (addr
< arange
->low
|| addr
>= arange
->high
)
3209 || arange
->high
- arange
->low
< best_fit_len
3210 /* The following comparison is designed to return the same
3211 match as the previous algorithm for routines which have the
3212 same best fit length. */
3213 || (arange
->high
- arange
->low
== best_fit_len
3214 && funcinfo
> best_fit
))
3216 best_fit
= funcinfo
;
3217 best_fit_len
= arange
->high
- arange
->low
;
3227 *function_ptr
= best_fit
;
3231 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
3232 and LINENUMBER_PTR, and return TRUE. */
3235 lookup_symbol_in_function_table (struct comp_unit
*unit
,
3238 const char **filename_ptr
,
3239 unsigned int *linenumber_ptr
)
3241 struct funcinfo
* each_func
;
3242 struct funcinfo
* best_fit
= NULL
;
3243 bfd_vma best_fit_len
= 0;
3244 struct arange
*arange
;
3245 const char *name
= bfd_asymbol_name (sym
);
3246 asection
*sec
= bfd_asymbol_section (sym
);
3248 for (each_func
= unit
->function_table
;
3250 each_func
= each_func
->prev_func
)
3252 for (arange
= &each_func
->arange
;
3254 arange
= arange
->next
)
3256 if ((!each_func
->sec
|| each_func
->sec
== sec
)
3257 && addr
>= arange
->low
3258 && addr
< arange
->high
3260 && strcmp (name
, each_func
->name
) == 0
3262 || arange
->high
- arange
->low
< best_fit_len
))
3264 best_fit
= each_func
;
3265 best_fit_len
= arange
->high
- arange
->low
;
3272 best_fit
->sec
= sec
;
3273 *filename_ptr
= best_fit
->file
;
3274 *linenumber_ptr
= best_fit
->line
;
3281 /* Variable table functions. */
3283 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
3284 LINENUMBER_PTR, and return TRUE. */
3287 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
3290 const char **filename_ptr
,
3291 unsigned int *linenumber_ptr
)
3293 const char *name
= bfd_asymbol_name (sym
);
3294 asection
*sec
= bfd_asymbol_section (sym
);
3295 struct varinfo
* each
;
3297 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
3299 && each
->file
!= NULL
3300 && each
->name
!= NULL
3301 && each
->addr
== addr
3302 && (!each
->sec
|| each
->sec
== sec
)
3303 && strcmp (name
, each
->name
) == 0)
3309 *filename_ptr
= each
->file
;
3310 *linenumber_ptr
= each
->line
;
3317 static struct comp_unit
*stash_comp_unit (struct dwarf2_debug
*,
3318 struct dwarf2_debug_file
*);
3319 static bool comp_unit_maybe_decode_line_info (struct comp_unit
*);
3322 find_abstract_instance (struct comp_unit
*unit
,
3323 struct attribute
*attr_ptr
,
3324 unsigned int recur_count
,
3327 char **filename_ptr
,
3328 int *linenumber_ptr
)
3330 bfd
*abfd
= unit
->abfd
;
3331 bfd_byte
*info_ptr
= NULL
;
3332 bfd_byte
*info_ptr_end
;
3333 unsigned int abbrev_number
, i
;
3334 struct abbrev_info
*abbrev
;
3335 uint64_t die_ref
= attr_ptr
->u
.val
;
3336 struct attribute attr
;
3337 const char *name
= NULL
;
3339 if (recur_count
== 100)
3342 (_("DWARF error: abstract instance recursion detected"));
3343 bfd_set_error (bfd_error_bad_value
);
3347 /* DW_FORM_ref_addr can reference an entry in a different CU. It
3348 is an offset from the .debug_info section, not the current CU. */
3349 if (attr_ptr
->form
== DW_FORM_ref_addr
)
3351 /* We only support DW_FORM_ref_addr within the same file, so
3352 any relocations should be resolved already. Check this by
3353 testing for a zero die_ref; There can't be a valid reference
3354 to the header of a .debug_info section.
3355 DW_FORM_ref_addr is an offset relative to .debug_info.
3356 Normally when using the GNU linker this is accomplished by
3357 emitting a symbolic reference to a label, because .debug_info
3358 sections are linked at zero. When there are multiple section
3359 groups containing .debug_info, as there might be in a
3360 relocatable object file, it would be reasonable to assume that
3361 a symbolic reference to a label in any .debug_info section
3362 might be used. Since we lay out multiple .debug_info
3363 sections at non-zero VMAs (see place_sections), and read
3364 them contiguously into dwarf_info_buffer, that means the
3365 reference is relative to dwarf_info_buffer. */
3368 info_ptr
= unit
->file
->dwarf_info_buffer
;
3369 info_ptr_end
= info_ptr
+ unit
->file
->dwarf_info_size
;
3370 total
= info_ptr_end
- info_ptr
;
3373 else if (die_ref
>= total
)
3376 (_("DWARF error: invalid abstract instance DIE ref"));
3377 bfd_set_error (bfd_error_bad_value
);
3380 info_ptr
+= die_ref
;
3382 else if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3384 bool first_time
= unit
->stash
->alt
.dwarf_info_buffer
== NULL
;
3386 info_ptr
= read_alt_indirect_ref (unit
, die_ref
);
3388 unit
->stash
->alt
.info_ptr
= unit
->stash
->alt
.dwarf_info_buffer
;
3389 if (info_ptr
== NULL
)
3392 (_("DWARF error: unable to read alt ref %" PRIu64
),
3393 (uint64_t) die_ref
);
3394 bfd_set_error (bfd_error_bad_value
);
3397 info_ptr_end
= (unit
->stash
->alt
.dwarf_info_buffer
3398 + unit
->stash
->alt
.dwarf_info_size
);
3399 if (unit
->stash
->alt
.all_comp_units
)
3400 unit
= unit
->stash
->alt
.all_comp_units
;
3403 if (attr_ptr
->form
== DW_FORM_ref_addr
3404 || attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3406 /* Now find the CU containing this pointer. */
3407 if (info_ptr
>= unit
->info_ptr_unit
&& info_ptr
< unit
->end_ptr
)
3408 info_ptr_end
= unit
->end_ptr
;
3411 /* Check other CUs to see if they contain the abbrev. */
3412 struct comp_unit
*u
;
3414 for (u
= unit
->prev_unit
; u
!= NULL
; u
= u
->prev_unit
)
3415 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3419 for (u
= unit
->next_unit
; u
!= NULL
; u
= u
->next_unit
)
3420 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3423 if (attr_ptr
->form
== DW_FORM_ref_addr
)
3426 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->f
);
3429 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3434 if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3437 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->alt
);
3440 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3448 (_("DWARF error: unable to locate abstract instance DIE ref %"
3449 PRIu64
), (uint64_t) die_ref
);
3450 bfd_set_error (bfd_error_bad_value
);
3454 info_ptr_end
= unit
->end_ptr
;
3459 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
3460 DW_FORM_ref_udata. These are all references relative to the
3461 start of the current CU. */
3464 info_ptr
= unit
->info_ptr_unit
;
3465 info_ptr_end
= unit
->end_ptr
;
3466 total
= info_ptr_end
- info_ptr
;
3467 if (!die_ref
|| die_ref
>= total
)
3470 (_("DWARF error: invalid abstract instance DIE ref"));
3471 bfd_set_error (bfd_error_bad_value
);
3474 info_ptr
+= die_ref
;
3477 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3478 false, info_ptr_end
);
3481 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3485 (_("DWARF error: could not find abbrev number %u"), abbrev_number
);
3486 bfd_set_error (bfd_error_bad_value
);
3491 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3493 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
,
3494 info_ptr
, info_ptr_end
);
3495 if (info_ptr
== NULL
)
3500 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3502 if (name
== NULL
&& is_str_form (&attr
))
3505 if (non_mangled (unit
->lang
))
3509 case DW_AT_specification
:
3510 if (is_int_form (&attr
)
3511 && !find_abstract_instance (unit
, &attr
, recur_count
+ 1,
3513 filename_ptr
, linenumber_ptr
))
3516 case DW_AT_linkage_name
:
3517 case DW_AT_MIPS_linkage_name
:
3518 /* PR 16949: Corrupt debug info can place
3519 non-string forms into these attributes. */
3520 if (is_str_form (&attr
))
3526 case DW_AT_decl_file
:
3527 if (!comp_unit_maybe_decode_line_info (unit
))
3529 if (is_int_form (&attr
))
3530 *filename_ptr
= concat_filename (unit
->line_table
,
3533 case DW_AT_decl_line
:
3534 if (is_int_form (&attr
))
3535 *linenumber_ptr
= attr
.u
.val
;
3548 read_ranges (struct comp_unit
*unit
, struct arange
*arange
,
3549 struct trie_node
**trie_root
, uint64_t offset
)
3551 bfd_byte
*ranges_ptr
;
3552 bfd_byte
*ranges_end
;
3553 bfd_vma base_address
= unit
->base_address
;
3555 if (! unit
->file
->dwarf_ranges_buffer
)
3557 if (! read_debug_ranges (unit
))
3561 if (offset
> unit
->file
->dwarf_ranges_size
)
3563 ranges_ptr
= unit
->file
->dwarf_ranges_buffer
+ offset
;
3564 ranges_end
= unit
->file
->dwarf_ranges_buffer
+ unit
->file
->dwarf_ranges_size
;
3571 /* PR 17512: file: 62cada7d. */
3572 if (2u * unit
->addr_size
> (size_t) (ranges_end
- ranges_ptr
))
3575 low_pc
= read_address (unit
, &ranges_ptr
, ranges_end
);
3576 high_pc
= read_address (unit
, &ranges_ptr
, ranges_end
);
3578 if (low_pc
== 0 && high_pc
== 0)
3580 if (low_pc
== -1UL && high_pc
!= -1UL)
3581 base_address
= high_pc
;
3584 if (!arange_add (unit
, arange
, trie_root
,
3585 base_address
+ low_pc
, base_address
+ high_pc
))
3593 read_rnglists (struct comp_unit
*unit
, struct arange
*arange
,
3594 struct trie_node
**trie_root
, uint64_t offset
)
3598 bfd_vma base_address
= unit
->base_address
;
3601 bfd
*abfd
= unit
->abfd
;
3603 if (! unit
->file
->dwarf_rnglists_buffer
)
3605 if (! read_debug_rnglists (unit
))
3609 rngs_ptr
= unit
->file
->dwarf_rnglists_buffer
+ offset
;
3610 if (rngs_ptr
< unit
->file
->dwarf_rnglists_buffer
)
3612 rngs_end
= unit
->file
->dwarf_rnglists_buffer
;
3613 rngs_end
+= unit
->file
->dwarf_rnglists_size
;
3617 enum dwarf_range_list_entry rlet
;
3619 if (rngs_ptr
>= rngs_end
)
3622 rlet
= read_1_byte (abfd
, &rngs_ptr
, rngs_end
);
3626 case DW_RLE_end_of_list
:
3629 case DW_RLE_base_address
:
3630 if (unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3632 base_address
= read_address (unit
, &rngs_ptr
, rngs_end
);
3635 case DW_RLE_start_length
:
3636 if (unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3638 low_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3640 high_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3644 case DW_RLE_offset_pair
:
3645 low_pc
= base_address
;
3646 low_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3648 high_pc
= base_address
;
3649 high_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3653 case DW_RLE_start_end
:
3654 if (2u * unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3656 low_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3657 high_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3660 /* TODO x-variants need .debug_addr support used for split-dwarf. */
3661 case DW_RLE_base_addressx
:
3662 case DW_RLE_startx_endx
:
3663 case DW_RLE_startx_length
:
3668 if (!arange_add (unit
, arange
, trie_root
, low_pc
, high_pc
))
3674 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
,
3675 struct trie_node
**trie_root
, uint64_t offset
)
3677 if (unit
->version
<= 4)
3678 return read_ranges (unit
, arange
, trie_root
, offset
);
3680 return read_rnglists (unit
, arange
, trie_root
, offset
);
3683 static struct funcinfo
*
3684 lookup_func_by_offset (uint64_t offset
, struct funcinfo
* table
)
3686 for (; table
!= NULL
; table
= table
->prev_func
)
3687 if (table
->unit_offset
== offset
)
3692 static struct varinfo
*
3693 lookup_var_by_offset (uint64_t offset
, struct varinfo
* table
)
3697 if (table
->unit_offset
== offset
)
3699 table
= table
->prev_var
;
3706 /* DWARF2 Compilation unit functions. */
3708 static struct funcinfo
*
3709 reverse_funcinfo_list (struct funcinfo
*head
)
3711 struct funcinfo
*rhead
;
3712 struct funcinfo
*temp
;
3714 for (rhead
= NULL
; head
; head
= temp
)
3716 temp
= head
->prev_func
;
3717 head
->prev_func
= rhead
;
3723 static struct varinfo
*
3724 reverse_varinfo_list (struct varinfo
*head
)
3726 struct varinfo
*rhead
;
3727 struct varinfo
*temp
;
3729 for (rhead
= NULL
; head
; head
= temp
)
3731 temp
= head
->prev_var
;
3732 head
->prev_var
= rhead
;
3738 /* Scan over each die in a comp. unit looking for functions to add
3739 to the function table and variables to the variable table. */
3742 scan_unit_for_symbols (struct comp_unit
*unit
)
3744 bfd
*abfd
= unit
->abfd
;
3745 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
3746 bfd_byte
*info_ptr_end
= unit
->end_ptr
;
3747 int nesting_level
= 0;
3748 struct nest_funcinfo
3750 struct funcinfo
*func
;
3752 int nested_funcs_size
;
3753 struct funcinfo
*last_func
;
3754 struct varinfo
*last_var
;
3756 /* Maintain a stack of in-scope functions and inlined functions, which we
3757 can use to set the caller_func field. */
3758 nested_funcs_size
= 32;
3759 nested_funcs
= (struct nest_funcinfo
*)
3760 bfd_malloc (nested_funcs_size
* sizeof (*nested_funcs
));
3761 if (nested_funcs
== NULL
)
3763 nested_funcs
[nesting_level
].func
= 0;
3765 /* PR 27484: We must scan the DIEs twice. The first time we look for
3766 function and variable tags and accumulate them into their respective
3767 tables. The second time through we process the attributes of the
3768 functions/variables and augment the table entries. */
3769 while (nesting_level
>= 0)
3771 unsigned int abbrev_number
, i
;
3772 struct abbrev_info
*abbrev
;
3773 struct funcinfo
*func
;
3774 struct varinfo
*var
;
3775 uint64_t current_offset
;
3777 /* PR 17512: file: 9f405d9d. */
3778 if (info_ptr
>= info_ptr_end
)
3781 current_offset
= info_ptr
- unit
->info_ptr_unit
;
3782 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3783 false, info_ptr_end
);
3784 if (abbrev_number
== 0)
3790 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3793 static unsigned int previous_failed_abbrev
= -1U;
3795 /* Avoid multiple reports of the same missing abbrev. */
3796 if (abbrev_number
!= previous_failed_abbrev
)
3799 (_("DWARF error: could not find abbrev number %u"),
3801 previous_failed_abbrev
= abbrev_number
;
3803 bfd_set_error (bfd_error_bad_value
);
3807 if (abbrev
->tag
== DW_TAG_subprogram
3808 || abbrev
->tag
== DW_TAG_entry_point
3809 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3811 size_t amt
= sizeof (struct funcinfo
);
3814 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
3817 func
->tag
= abbrev
->tag
;
3818 func
->prev_func
= unit
->function_table
;
3819 func
->unit_offset
= current_offset
;
3820 unit
->function_table
= func
;
3821 unit
->number_of_functions
++;
3822 BFD_ASSERT (!unit
->cached
);
3824 if (func
->tag
== DW_TAG_inlined_subroutine
)
3825 for (i
= nesting_level
; i
-- != 0; )
3826 if (nested_funcs
[i
].func
)
3828 func
->caller_func
= nested_funcs
[i
].func
;
3831 nested_funcs
[nesting_level
].func
= func
;
3836 if (abbrev
->tag
== DW_TAG_variable
3837 || abbrev
->tag
== DW_TAG_member
)
3839 size_t amt
= sizeof (struct varinfo
);
3841 var
= (struct varinfo
*) bfd_zalloc (abfd
, amt
);
3844 var
->tag
= abbrev
->tag
;
3846 var
->prev_var
= unit
->variable_table
;
3847 unit
->variable_table
= var
;
3848 var
->unit_offset
= current_offset
;
3849 /* PR 18205: Missing debug information can cause this
3850 var to be attached to an already cached unit. */
3855 /* No inline function in scope at this nesting level. */
3856 nested_funcs
[nesting_level
].func
= 0;
3859 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3861 struct attribute attr
;
3863 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
3864 unit
, info_ptr
, info_ptr_end
);
3865 if (info_ptr
== NULL
)
3869 if (abbrev
->has_children
)
3873 if (nesting_level
>= nested_funcs_size
)
3875 struct nest_funcinfo
*tmp
;
3877 nested_funcs_size
*= 2;
3878 tmp
= (struct nest_funcinfo
*)
3879 bfd_realloc (nested_funcs
,
3880 nested_funcs_size
* sizeof (*nested_funcs
));
3885 nested_funcs
[nesting_level
].func
= 0;
3889 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
3890 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
3892 /* This is the second pass over the abbrevs. */
3893 info_ptr
= unit
->first_child_die_ptr
;
3899 while (nesting_level
>= 0)
3901 unsigned int abbrev_number
, i
;
3902 struct abbrev_info
*abbrev
;
3903 struct attribute attr
;
3904 struct funcinfo
*func
;
3905 struct varinfo
*var
;
3907 bfd_vma high_pc
= 0;
3908 bool high_pc_relative
= false;
3909 uint64_t current_offset
;
3911 /* PR 17512: file: 9f405d9d. */
3912 if (info_ptr
>= info_ptr_end
)
3915 current_offset
= info_ptr
- unit
->info_ptr_unit
;
3916 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3917 false, info_ptr_end
);
3918 if (! abbrev_number
)
3924 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3925 /* This should have been handled above. */
3926 BFD_ASSERT (abbrev
!= NULL
);
3930 if (abbrev
->tag
== DW_TAG_subprogram
3931 || abbrev
->tag
== DW_TAG_entry_point
3932 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3935 && last_func
->prev_func
3936 && last_func
->prev_func
->unit_offset
== current_offset
)
3937 func
= last_func
->prev_func
;
3939 func
= lookup_func_by_offset (current_offset
, unit
->function_table
);
3946 else if (abbrev
->tag
== DW_TAG_variable
3947 || abbrev
->tag
== DW_TAG_member
)
3950 && last_var
->prev_var
3951 && last_var
->prev_var
->unit_offset
== current_offset
)
3952 var
= last_var
->prev_var
;
3954 var
= lookup_var_by_offset (current_offset
, unit
->variable_table
);
3962 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3964 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
3965 unit
, info_ptr
, info_ptr_end
);
3966 if (info_ptr
== NULL
)
3973 case DW_AT_call_file
:
3974 if (is_int_form (&attr
))
3975 func
->caller_file
= concat_filename (unit
->line_table
,
3979 case DW_AT_call_line
:
3980 if (is_int_form (&attr
))
3981 func
->caller_line
= attr
.u
.val
;
3984 case DW_AT_abstract_origin
:
3985 case DW_AT_specification
:
3986 if (is_int_form (&attr
)
3987 && !find_abstract_instance (unit
, &attr
, 0,
3996 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3998 if (func
->name
== NULL
&& is_str_form (&attr
))
4000 func
->name
= attr
.u
.str
;
4001 if (non_mangled (unit
->lang
))
4002 func
->is_linkage
= true;
4006 case DW_AT_linkage_name
:
4007 case DW_AT_MIPS_linkage_name
:
4008 /* PR 16949: Corrupt debug info can place
4009 non-string forms into these attributes. */
4010 if (is_str_form (&attr
))
4012 func
->name
= attr
.u
.str
;
4013 func
->is_linkage
= true;
4018 if (is_int_form (&attr
))
4019 low_pc
= attr
.u
.val
;
4023 if (is_int_form (&attr
))
4025 high_pc
= attr
.u
.val
;
4026 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
4031 if (is_int_form (&attr
)
4032 && !read_rangelist (unit
, &func
->arange
,
4033 &unit
->file
->trie_root
, attr
.u
.val
))
4037 case DW_AT_decl_file
:
4038 if (is_int_form (&attr
))
4039 func
->file
= concat_filename (unit
->line_table
,
4043 case DW_AT_decl_line
:
4044 if (is_int_form (&attr
))
4045 func
->line
= attr
.u
.val
;
4056 case DW_AT_specification
:
4057 if (is_int_form (&attr
) && attr
.u
.val
)
4059 struct varinfo
* spec_var
;
4061 spec_var
= lookup_var_by_offset (attr
.u
.val
,
4062 unit
->variable_table
);
4063 if (spec_var
== NULL
)
4065 _bfd_error_handler (_("DWARF error: could not find "
4066 "variable specification "
4068 (unsigned long) attr
.u
.val
);
4072 if (var
->name
== NULL
)
4073 var
->name
= spec_var
->name
;
4074 if (var
->file
== NULL
&& spec_var
->file
!= NULL
)
4075 var
->file
= strdup (spec_var
->file
);
4077 var
->line
= spec_var
->line
;
4078 if (var
->sec
== NULL
)
4079 var
->sec
= spec_var
->sec
;
4084 if (is_str_form (&attr
))
4085 var
->name
= attr
.u
.str
;
4088 case DW_AT_decl_file
:
4089 if (is_int_form (&attr
))
4090 var
->file
= concat_filename (unit
->line_table
,
4094 case DW_AT_decl_line
:
4095 if (is_int_form (&attr
))
4096 var
->line
= attr
.u
.val
;
4099 case DW_AT_external
:
4100 if (is_int_form (&attr
) && attr
.u
.val
!= 0)
4104 case DW_AT_location
:
4108 case DW_FORM_block1
:
4109 case DW_FORM_block2
:
4110 case DW_FORM_block4
:
4111 case DW_FORM_exprloc
:
4112 if (attr
.u
.blk
->data
!= NULL
4113 && *attr
.u
.blk
->data
== DW_OP_addr
)
4117 /* Verify that DW_OP_addr is the only opcode in the
4118 location, in which case the block size will be 1
4119 plus the address size. */
4120 /* ??? For TLS variables, gcc can emit
4121 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
4122 which we don't handle here yet. */
4123 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
4124 var
->addr
= bfd_get (unit
->addr_size
* 8,
4126 attr
.u
.blk
->data
+ 1);
4141 if (abbrev
->has_children
)
4144 if (high_pc_relative
)
4147 if (func
&& high_pc
!= 0)
4149 if (!arange_add (unit
, &func
->arange
, &unit
->file
->trie_root
,
4155 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4156 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4158 free (nested_funcs
);
4162 free (nested_funcs
);
4166 /* Read the attributes of the form strx and addrx. */
4169 reread_attribute (struct comp_unit
*unit
,
4170 struct attribute
*attr
,
4173 bool *high_pc_relative
,
4176 if (is_strx_form (attr
->form
))
4177 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
4178 if (is_addrx_form (attr
->form
))
4179 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
4183 case DW_AT_stmt_list
:
4185 unit
->line_offset
= attr
->u
.val
;
4189 if (is_str_form (attr
))
4190 unit
->name
= attr
->u
.str
;
4194 *low_pc
= attr
->u
.val
;
4196 unit
->base_address
= *low_pc
;
4200 *high_pc
= attr
->u
.val
;
4201 *high_pc_relative
= attr
->form
!= DW_FORM_addr
;
4205 if (!read_rangelist (unit
, &unit
->arange
,
4206 &unit
->file
->trie_root
, attr
->u
.val
))
4210 case DW_AT_comp_dir
:
4212 char *comp_dir
= attr
->u
.str
;
4214 if (!is_str_form (attr
))
4217 (_("DWARF error: DW_AT_comp_dir attribute encountered "
4218 "with a non-string form"));
4224 char *cp
= strchr (comp_dir
, ':');
4226 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
4229 unit
->comp_dir
= comp_dir
;
4233 case DW_AT_language
:
4234 unit
->lang
= attr
->u
.val
;
4240 /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
4241 includes the compilation unit header that proceeds the DIE's, but
4242 does not include the length field that precedes each compilation
4243 unit header. END_PTR points one past the end of this comp unit.
4244 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
4246 This routine does not read the whole compilation unit; only enough
4247 to get to the line number information for the compilation unit. */
4249 static struct comp_unit
*
4250 parse_comp_unit (struct dwarf2_debug
*stash
,
4251 struct dwarf2_debug_file
*file
,
4253 bfd_vma unit_length
,
4254 bfd_byte
*info_ptr_unit
,
4255 unsigned int offset_size
)
4257 struct comp_unit
* unit
;
4258 unsigned int version
;
4259 uint64_t abbrev_offset
= 0;
4260 /* Initialize it just to avoid a GCC false warning. */
4261 unsigned int addr_size
= -1;
4262 struct abbrev_info
** abbrevs
;
4263 unsigned int abbrev_number
, i
;
4264 struct abbrev_info
*abbrev
;
4265 struct attribute attr
;
4266 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
4269 bfd_vma high_pc
= 0;
4270 bfd
*abfd
= file
->bfd_ptr
;
4271 bool high_pc_relative
= false;
4272 enum dwarf_unit_type unit_type
;
4273 struct attribute
*str_addrp
= NULL
;
4274 size_t str_count
= 0;
4275 size_t str_alloc
= 0;
4276 bool compunit_flag
= false;
4278 version
= read_2_bytes (abfd
, &info_ptr
, end_ptr
);
4279 if (version
< 2 || version
> 5)
4281 /* PR 19872: A version number of 0 probably means that there is padding
4282 at the end of the .debug_info section. Gold puts it there when
4283 performing an incremental link, for example. So do not generate
4284 an error, just return a NULL. */
4288 (_("DWARF error: found dwarf version '%u', this reader"
4289 " only handles version 2, 3, 4 and 5 information"), version
);
4290 bfd_set_error (bfd_error_bad_value
);
4296 unit_type
= DW_UT_compile
;
4299 unit_type
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
4300 addr_size
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
4303 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
4304 if (offset_size
== 4)
4305 abbrev_offset
= read_4_bytes (abfd
, &info_ptr
, end_ptr
);
4307 abbrev_offset
= read_8_bytes (abfd
, &info_ptr
, end_ptr
);
4310 addr_size
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
4312 if (unit_type
== DW_UT_type
)
4314 /* Skip type signature. */
4317 /* Skip type offset. */
4318 info_ptr
+= offset_size
;
4321 if (addr_size
> sizeof (bfd_vma
))
4324 /* xgettext: c-format */
4325 (_("DWARF error: found address size '%u', this reader"
4326 " can not handle sizes greater than '%u'"),
4328 (unsigned int) sizeof (bfd_vma
));
4329 bfd_set_error (bfd_error_bad_value
);
4333 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
4336 ("DWARF error: found address size '%u', this reader"
4337 " can only handle address sizes '2', '4' and '8'", addr_size
);
4338 bfd_set_error (bfd_error_bad_value
);
4342 /* Read the abbrevs for this compilation unit into a table. */
4343 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
, file
);
4347 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
4349 if (! abbrev_number
)
4351 /* PR 19872: An abbrev number of 0 probably means that there is padding
4352 at the end of the .debug_abbrev section. Gold puts it there when
4353 performing an incremental link, for example. So do not generate
4354 an error, just return a NULL. */
4358 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
4361 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
4363 bfd_set_error (bfd_error_bad_value
);
4367 amt
= sizeof (struct comp_unit
);
4368 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
4372 unit
->version
= version
;
4373 unit
->addr_size
= addr_size
;
4374 unit
->offset_size
= offset_size
;
4375 unit
->abbrevs
= abbrevs
;
4376 unit
->end_ptr
= end_ptr
;
4377 unit
->stash
= stash
;
4379 unit
->info_ptr_unit
= info_ptr_unit
;
4381 if (abbrev
->tag
== DW_TAG_compile_unit
)
4382 compunit_flag
= true;
4384 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
4386 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
, end_ptr
);
4387 if (info_ptr
== NULL
)
4390 /* Identify attributes of the form strx* and addrx* which come before
4391 DW_AT_str_offsets_base and DW_AT_addr_base respectively in the CU.
4392 Store the attributes in an array and process them later. */
4393 if ((unit
->dwarf_str_offset
== 0 && is_strx_form (attr
.form
))
4394 || (unit
->dwarf_addr_offset
== 0 && is_addrx_form (attr
.form
)))
4396 if (str_count
<= str_alloc
)
4398 str_alloc
= 2 * str_alloc
+ 200;
4399 str_addrp
= bfd_realloc (str_addrp
,
4400 str_alloc
* sizeof (*str_addrp
));
4401 if (str_addrp
== NULL
)
4404 str_addrp
[str_count
] = attr
;
4409 /* Store the data if it is of an attribute we want to keep in a
4410 partial symbol table. */
4413 case DW_AT_stmt_list
:
4414 if (is_int_form (&attr
))
4417 unit
->line_offset
= attr
.u
.val
;
4422 if (is_str_form (&attr
))
4423 unit
->name
= attr
.u
.str
;
4427 if (is_int_form (&attr
))
4429 low_pc
= attr
.u
.val
;
4430 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
4431 this is the base address to use when reading location
4432 lists or range lists. */
4434 unit
->base_address
= low_pc
;
4439 if (is_int_form (&attr
))
4441 high_pc
= attr
.u
.val
;
4442 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
4447 if (is_int_form (&attr
)
4448 && !read_rangelist (unit
, &unit
->arange
,
4449 &unit
->file
->trie_root
, attr
.u
.val
))
4453 case DW_AT_comp_dir
:
4455 char *comp_dir
= attr
.u
.str
;
4457 /* PR 17512: file: 1fe726be. */
4458 if (!is_str_form (&attr
))
4461 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
4467 /* Irix 6.2 native cc prepends <machine>.: to the compilation
4468 directory, get rid of it. */
4469 char *cp
= strchr (comp_dir
, ':');
4471 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
4474 unit
->comp_dir
= comp_dir
;
4478 case DW_AT_language
:
4479 if (is_int_form (&attr
))
4480 unit
->lang
= attr
.u
.val
;
4483 case DW_AT_addr_base
:
4484 unit
->dwarf_addr_offset
= attr
.u
.val
;
4487 case DW_AT_str_offsets_base
:
4488 unit
->dwarf_str_offset
= attr
.u
.val
;
4496 for (i
= 0; i
< str_count
; ++i
)
4497 reread_attribute (unit
, &str_addrp
[i
], &low_pc
, &high_pc
,
4498 &high_pc_relative
, compunit_flag
);
4500 if (high_pc_relative
)
4504 if (!arange_add (unit
, &unit
->arange
, &unit
->file
->trie_root
,
4509 unit
->first_child_die_ptr
= info_ptr
;
4519 /* Return TRUE if UNIT may contain the address given by ADDR. When
4520 there are functions written entirely with inline asm statements, the
4521 range info in the compilation unit header may not be correct. We
4522 need to consult the line info table to see if a compilation unit
4523 really contains the given address. */
4526 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
4528 struct arange
*arange
;
4533 arange
= &unit
->arange
;
4536 if (addr
>= arange
->low
&& addr
< arange
->high
)
4538 arange
= arange
->next
;
4545 /* If UNIT contains ADDR, set the output parameters to the values for
4546 the line containing ADDR and return TRUE. Otherwise return FALSE.
4547 The output parameters, FILENAME_PTR, FUNCTION_PTR, and
4548 LINENUMBER_PTR, are pointers to the objects to be filled in. */
4551 comp_unit_find_nearest_line (struct comp_unit
*unit
,
4553 const char **filename_ptr
,
4554 struct funcinfo
**function_ptr
,
4555 unsigned int *linenumber_ptr
,
4556 unsigned int *discriminator_ptr
)
4558 bool line_p
, func_p
;
4560 if (!comp_unit_maybe_decode_line_info (unit
))
4563 *function_ptr
= NULL
;
4564 func_p
= lookup_address_in_function_table (unit
, addr
, function_ptr
);
4565 if (func_p
&& (*function_ptr
)->tag
== DW_TAG_inlined_subroutine
)
4566 unit
->stash
->inliner_chain
= *function_ptr
;
4568 line_p
= lookup_address_in_line_info_table (unit
->line_table
, addr
,
4572 return line_p
|| func_p
;
4575 /* Check to see if line info is already decoded in a comp_unit.
4576 If not, decode it. Returns TRUE if no errors were encountered;
4580 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
)
4585 if (! unit
->line_table
)
4587 if (! unit
->stmtlist
)
4593 unit
->line_table
= decode_line_info (unit
);
4595 if (! unit
->line_table
)
4601 if (unit
->first_child_die_ptr
< unit
->end_ptr
4602 && ! scan_unit_for_symbols (unit
))
4612 /* If UNIT contains SYM at ADDR, set the output parameters to the
4613 values for the line containing SYM. The output parameters,
4614 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
4617 Return TRUE if UNIT contains SYM, and no errors were encountered;
4621 comp_unit_find_line (struct comp_unit
*unit
,
4624 const char **filename_ptr
,
4625 unsigned int *linenumber_ptr
)
4627 if (!comp_unit_maybe_decode_line_info (unit
))
4630 if (sym
->flags
& BSF_FUNCTION
)
4631 return lookup_symbol_in_function_table (unit
, sym
, addr
,
4635 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
4640 /* Extract all interesting funcinfos and varinfos of a compilation
4641 unit into hash tables for faster lookup. Returns TRUE if no
4642 errors were enountered; FALSE otherwise. */
4645 comp_unit_hash_info (struct dwarf2_debug
*stash
,
4646 struct comp_unit
*unit
,
4647 struct info_hash_table
*funcinfo_hash_table
,
4648 struct info_hash_table
*varinfo_hash_table
)
4650 struct funcinfo
* each_func
;
4651 struct varinfo
* each_var
;
4654 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
4656 if (!comp_unit_maybe_decode_line_info (unit
))
4659 BFD_ASSERT (!unit
->cached
);
4661 /* To preserve the original search order, we went to visit the function
4662 infos in the reversed order of the list. However, making the list
4663 bi-directional use quite a bit of extra memory. So we reverse
4664 the list first, traverse the list in the now reversed order and
4665 finally reverse the list again to get back the original order. */
4666 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4667 for (each_func
= unit
->function_table
;
4669 each_func
= each_func
->prev_func
)
4671 /* Skip nameless functions. */
4672 if (each_func
->name
)
4673 /* There is no need to copy name string into hash table as
4674 name string is either in the dwarf string buffer or
4675 info in the stash. */
4676 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
4677 (void*) each_func
, false);
4679 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4683 /* We do the same for variable infos. */
4684 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4685 for (each_var
= unit
->variable_table
;
4687 each_var
= each_var
->prev_var
)
4689 /* Skip stack vars and vars with no files or names. */
4690 if (! each_var
->stack
4691 && each_var
->file
!= NULL
4692 && each_var
->name
!= NULL
)
4693 /* There is no need to copy name string into hash table as
4694 name string is either in the dwarf string buffer or
4695 info in the stash. */
4696 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
4697 (void*) each_var
, false);
4700 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4701 unit
->cached
= true;
4705 /* Locate a section in a BFD containing debugging info. The search starts
4706 from the section after AFTER_SEC, or from the first section in the BFD if
4707 AFTER_SEC is NULL. The search works by examining the names of the
4708 sections. There are three permissiable names. The first two are given
4709 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
4710 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
4711 This is a variation on the .debug_info section which has a checksum
4712 describing the contents appended onto the name. This allows the linker to
4713 identify and discard duplicate debugging sections for different
4714 compilation units. */
4715 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
4718 find_debug_info (bfd
*abfd
, const struct dwarf_debug_section
*debug_sections
,
4719 asection
*after_sec
)
4724 if (after_sec
== NULL
)
4726 look
= debug_sections
[debug_info
].uncompressed_name
;
4727 msec
= bfd_get_section_by_name (abfd
, look
);
4731 look
= debug_sections
[debug_info
].compressed_name
;
4732 msec
= bfd_get_section_by_name (abfd
, look
);
4736 for (msec
= abfd
->sections
; msec
!= NULL
; msec
= msec
->next
)
4737 if (startswith (msec
->name
, GNU_LINKONCE_INFO
))
4743 for (msec
= after_sec
->next
; msec
!= NULL
; msec
= msec
->next
)
4745 look
= debug_sections
[debug_info
].uncompressed_name
;
4746 if (strcmp (msec
->name
, look
) == 0)
4749 look
= debug_sections
[debug_info
].compressed_name
;
4750 if (look
!= NULL
&& strcmp (msec
->name
, look
) == 0)
4753 if (startswith (msec
->name
, GNU_LINKONCE_INFO
))
4760 /* Transfer VMAs from object file to separate debug file. */
4763 set_debug_vma (bfd
*orig_bfd
, bfd
*debug_bfd
)
4767 for (s
= orig_bfd
->sections
, d
= debug_bfd
->sections
;
4768 s
!= NULL
&& d
!= NULL
;
4769 s
= s
->next
, d
= d
->next
)
4771 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4773 /* ??? Assumes 1-1 correspondence between sections in the
4775 if (strcmp (s
->name
, d
->name
) == 0)
4777 d
->output_section
= s
->output_section
;
4778 d
->output_offset
= s
->output_offset
;
4784 /* If the dwarf2 info was found in a separate debug file, return the
4785 debug file section corresponding to the section in the original file
4786 and the debug file symbols. */
4789 _bfd_dwarf2_stash_syms (struct dwarf2_debug
*stash
, bfd
*abfd
,
4790 asection
**sec
, asymbol
***syms
)
4792 if (stash
->f
.bfd_ptr
!= abfd
)
4798 *syms
= stash
->f
.syms
;
4802 for (s
= abfd
->sections
, d
= stash
->f
.bfd_ptr
->sections
;
4803 s
!= NULL
&& d
!= NULL
;
4804 s
= s
->next
, d
= d
->next
)
4806 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4809 && strcmp (s
->name
, d
->name
) == 0)
4812 *syms
= stash
->f
.syms
;
4819 /* Unset vmas for adjusted sections in STASH. */
4822 unset_sections (struct dwarf2_debug
*stash
)
4825 struct adjusted_section
*p
;
4827 i
= stash
->adjusted_section_count
;
4828 p
= stash
->adjusted_sections
;
4829 for (; i
> 0; i
--, p
++)
4830 p
->section
->vma
= 0;
4833 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4834 relocatable object file. VMAs are normally all zero in relocatable
4835 object files, so if we want to distinguish locations in sections by
4836 address we need to set VMAs so the sections do not overlap. We
4837 also set VMA on .debug_info so that when we have multiple
4838 .debug_info sections (or the linkonce variant) they also do not
4839 overlap. The multiple .debug_info sections make up a single
4840 logical section. ??? We should probably do the same for other
4844 place_sections (bfd
*orig_bfd
, struct dwarf2_debug
*stash
)
4847 struct adjusted_section
*p
;
4849 const char *debug_info_name
;
4851 if (stash
->adjusted_section_count
!= 0)
4853 i
= stash
->adjusted_section_count
;
4854 p
= stash
->adjusted_sections
;
4855 for (; i
> 0; i
--, p
++)
4856 p
->section
->vma
= p
->adj_vma
;
4860 debug_info_name
= stash
->debug_sections
[debug_info
].uncompressed_name
;
4867 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4871 if ((sect
->output_section
!= NULL
4872 && sect
->output_section
!= sect
4873 && (sect
->flags
& SEC_DEBUGGING
) == 0)
4877 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
4878 || startswith (sect
->name
, GNU_LINKONCE_INFO
));
4880 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
4886 if (abfd
== stash
->f
.bfd_ptr
)
4888 abfd
= stash
->f
.bfd_ptr
;
4892 stash
->adjusted_section_count
= -1;
4895 bfd_vma last_vma
= 0, last_dwarf
= 0;
4896 size_t amt
= i
* sizeof (struct adjusted_section
);
4898 p
= (struct adjusted_section
*) bfd_malloc (amt
);
4902 stash
->adjusted_sections
= p
;
4903 stash
->adjusted_section_count
= i
;
4910 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4915 if ((sect
->output_section
!= NULL
4916 && sect
->output_section
!= sect
4917 && (sect
->flags
& SEC_DEBUGGING
) == 0)
4921 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
4922 || startswith (sect
->name
, GNU_LINKONCE_INFO
));
4924 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
4928 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
4932 BFD_ASSERT (sect
->alignment_power
== 0);
4933 sect
->vma
= last_dwarf
;
4938 /* Align the new address to the current section
4940 last_vma
= ((last_vma
4941 + ~(-((bfd_vma
) 1 << sect
->alignment_power
)))
4942 & (-((bfd_vma
) 1 << sect
->alignment_power
)));
4943 sect
->vma
= last_vma
;
4948 p
->adj_vma
= sect
->vma
;
4951 if (abfd
== stash
->f
.bfd_ptr
)
4953 abfd
= stash
->f
.bfd_ptr
;
4957 if (orig_bfd
!= stash
->f
.bfd_ptr
)
4958 set_debug_vma (orig_bfd
, stash
->f
.bfd_ptr
);
4963 /* Look up a funcinfo by name using the given info hash table. If found,
4964 also update the locations pointed to by filename_ptr and linenumber_ptr.
4966 This function returns TRUE if a funcinfo that matches the given symbol
4967 and address is found with any error; otherwise it returns FALSE. */
4970 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
4973 const char **filename_ptr
,
4974 unsigned int *linenumber_ptr
)
4976 struct funcinfo
* each_func
;
4977 struct funcinfo
* best_fit
= NULL
;
4978 bfd_vma best_fit_len
= 0;
4979 struct info_list_node
*node
;
4980 struct arange
*arange
;
4981 const char *name
= bfd_asymbol_name (sym
);
4982 asection
*sec
= bfd_asymbol_section (sym
);
4984 for (node
= lookup_info_hash_table (hash_table
, name
);
4988 each_func
= (struct funcinfo
*) node
->info
;
4989 for (arange
= &each_func
->arange
;
4991 arange
= arange
->next
)
4993 if ((!each_func
->sec
|| each_func
->sec
== sec
)
4994 && addr
>= arange
->low
4995 && addr
< arange
->high
4997 || arange
->high
- arange
->low
< best_fit_len
))
4999 best_fit
= each_func
;
5000 best_fit_len
= arange
->high
- arange
->low
;
5007 best_fit
->sec
= sec
;
5008 *filename_ptr
= best_fit
->file
;
5009 *linenumber_ptr
= best_fit
->line
;
5016 /* Look up a varinfo by name using the given info hash table. If found,
5017 also update the locations pointed to by filename_ptr and linenumber_ptr.
5019 This function returns TRUE if a varinfo that matches the given symbol
5020 and address is found with any error; otherwise it returns FALSE. */
5023 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
5026 const char **filename_ptr
,
5027 unsigned int *linenumber_ptr
)
5029 const char *name
= bfd_asymbol_name (sym
);
5030 asection
*sec
= bfd_asymbol_section (sym
);
5031 struct varinfo
* each
;
5032 struct info_list_node
*node
;
5034 for (node
= lookup_info_hash_table (hash_table
, name
);
5038 each
= (struct varinfo
*) node
->info
;
5039 if (each
->addr
== addr
5040 && (!each
->sec
|| each
->sec
== sec
))
5043 *filename_ptr
= each
->file
;
5044 *linenumber_ptr
= each
->line
;
5052 /* Update the funcinfo and varinfo info hash tables if they are
5053 not up to date. Returns TRUE if there is no error; otherwise
5054 returns FALSE and disable the info hash tables. */
5057 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
5059 struct comp_unit
*each
;
5061 /* Exit if hash tables are up-to-date. */
5062 if (stash
->f
.all_comp_units
== stash
->hash_units_head
)
5065 if (stash
->hash_units_head
)
5066 each
= stash
->hash_units_head
->prev_unit
;
5068 each
= stash
->f
.last_comp_unit
;
5072 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
5073 stash
->varinfo_hash_table
))
5075 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
5078 each
= each
->prev_unit
;
5081 stash
->hash_units_head
= stash
->f
.all_comp_units
;
5085 /* Check consistency of info hash tables. This is for debugging only. */
5087 static void ATTRIBUTE_UNUSED
5088 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
5090 struct comp_unit
*each_unit
;
5091 struct funcinfo
*each_func
;
5092 struct varinfo
*each_var
;
5093 struct info_list_node
*node
;
5096 for (each_unit
= stash
->f
.all_comp_units
;
5098 each_unit
= each_unit
->next_unit
)
5100 for (each_func
= each_unit
->function_table
;
5102 each_func
= each_func
->prev_func
)
5104 if (!each_func
->name
)
5106 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
5110 while (node
&& !found
)
5112 found
= node
->info
== each_func
;
5118 for (each_var
= each_unit
->variable_table
;
5120 each_var
= each_var
->prev_var
)
5122 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
5124 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
5128 while (node
&& !found
)
5130 found
= node
->info
== each_var
;
5138 /* Check to see if we want to enable the info hash tables, which consume
5139 quite a bit of memory. Currently we only check the number times
5140 bfd_dwarf2_find_line is called. In the future, we may also want to
5141 take the number of symbols into account. */
5144 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
5146 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
5148 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
5151 /* FIXME: Maybe we should check the reduce_memory_overheads
5152 and optimize fields in the bfd_link_info structure ? */
5154 /* Create hash tables. */
5155 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
5156 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
5157 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
5159 /* Turn off info hashes if any allocation above fails. */
5160 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
5163 /* We need a forced update so that the info hash tables will
5164 be created even though there is no compilation unit. That
5165 happens if STASH_INFO_HASH_TRIGGER is 0. */
5166 if (stash_maybe_update_info_hash_tables (stash
))
5167 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
5170 /* Find the file and line associated with a symbol and address using the
5171 info hash tables of a stash. If there is a match, the function returns
5172 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
5173 otherwise it returns FALSE. */
5176 stash_find_line_fast (struct dwarf2_debug
*stash
,
5179 const char **filename_ptr
,
5180 unsigned int *linenumber_ptr
)
5182 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
5184 if (sym
->flags
& BSF_FUNCTION
)
5185 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
5186 filename_ptr
, linenumber_ptr
);
5187 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
5188 filename_ptr
, linenumber_ptr
);
5191 /* Save current section VMAs. */
5194 save_section_vma (const bfd
*abfd
, struct dwarf2_debug
*stash
)
5199 if (abfd
->section_count
== 0)
5201 stash
->sec_vma
= bfd_malloc (sizeof (*stash
->sec_vma
) * abfd
->section_count
);
5202 if (stash
->sec_vma
== NULL
)
5204 stash
->sec_vma_count
= abfd
->section_count
;
5205 for (i
= 0, s
= abfd
->sections
;
5206 s
!= NULL
&& i
< abfd
->section_count
;
5209 if (s
->output_section
!= NULL
)
5210 stash
->sec_vma
[i
] = s
->output_section
->vma
+ s
->output_offset
;
5212 stash
->sec_vma
[i
] = s
->vma
;
5217 /* Compare current section VMAs against those at the time the stash
5218 was created. If find_nearest_line is used in linker warnings or
5219 errors early in the link process, the debug info stash will be
5220 invalid for later calls. This is because we relocate debug info
5221 sections, so the stashed section contents depend on symbol values,
5222 which in turn depend on section VMAs. */
5225 section_vma_same (const bfd
*abfd
, const struct dwarf2_debug
*stash
)
5230 /* PR 24334: If the number of sections in ABFD has changed between
5231 when the stash was created and now, then we cannot trust the
5232 stashed vma information. */
5233 if (abfd
->section_count
!= stash
->sec_vma_count
)
5236 for (i
= 0, s
= abfd
->sections
;
5237 s
!= NULL
&& i
< abfd
->section_count
;
5242 if (s
->output_section
!= NULL
)
5243 vma
= s
->output_section
->vma
+ s
->output_offset
;
5246 if (vma
!= stash
->sec_vma
[i
])
5252 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
5253 If DEBUG_BFD is not specified, we read debug information from ABFD
5254 or its gnu_debuglink. The results will be stored in PINFO.
5255 The function returns TRUE iff debug information is ready. */
5258 _bfd_dwarf2_slurp_debug_info (bfd
*abfd
, bfd
*debug_bfd
,
5259 const struct dwarf_debug_section
*debug_sections
,
5264 size_t amt
= sizeof (struct dwarf2_debug
);
5265 bfd_size_type total_size
;
5267 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
5271 if (stash
->orig_bfd
== abfd
5272 && section_vma_same (abfd
, stash
))
5274 /* Check that we did previously find some debug information
5275 before attempting to make use of it. */
5276 if (stash
->f
.bfd_ptr
!= NULL
)
5278 if (do_place
&& !place_sections (abfd
, stash
))
5285 _bfd_dwarf2_cleanup_debug_info (abfd
, pinfo
);
5286 memset (stash
, 0, amt
);
5290 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
5294 stash
->orig_bfd
= abfd
;
5295 stash
->debug_sections
= debug_sections
;
5296 stash
->f
.syms
= symbols
;
5297 if (!save_section_vma (abfd
, stash
))
5300 stash
->f
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
5301 del_abbrev
, calloc
, free
);
5302 if (!stash
->f
.abbrev_offsets
)
5305 stash
->alt
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
5306 del_abbrev
, calloc
, free
);
5307 if (!stash
->alt
.abbrev_offsets
)
5310 stash
->f
.trie_root
= alloc_trie_leaf (abfd
);
5311 if (!stash
->f
.trie_root
)
5314 stash
->alt
.trie_root
= alloc_trie_leaf (abfd
);
5315 if (!stash
->alt
.trie_root
)
5320 if (debug_bfd
== NULL
)
5323 msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
5324 if (msec
== NULL
&& abfd
== debug_bfd
)
5326 char * debug_filename
;
5328 debug_filename
= bfd_follow_build_id_debuglink (abfd
, DEBUGDIR
);
5329 if (debug_filename
== NULL
)
5330 debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
5332 if (debug_filename
== NULL
)
5333 /* No dwarf2 info, and no gnu_debuglink to follow.
5334 Note that at this point the stash has been allocated, but
5335 contains zeros. This lets future calls to this function
5336 fail more quickly. */
5339 debug_bfd
= bfd_openr (debug_filename
, NULL
);
5340 free (debug_filename
);
5341 if (debug_bfd
== NULL
)
5342 /* FIXME: Should we report our failure to follow the debuglink ? */
5345 /* Set BFD_DECOMPRESS to decompress debug sections. */
5346 debug_bfd
->flags
|= BFD_DECOMPRESS
;
5347 if (!bfd_check_format (debug_bfd
, bfd_object
)
5348 || (msec
= find_debug_info (debug_bfd
,
5349 debug_sections
, NULL
)) == NULL
5350 || !bfd_generic_link_read_symbols (debug_bfd
))
5352 bfd_close (debug_bfd
);
5356 symbols
= bfd_get_outsymbols (debug_bfd
);
5357 stash
->f
.syms
= symbols
;
5358 stash
->close_on_cleanup
= true;
5360 stash
->f
.bfd_ptr
= debug_bfd
;
5363 && !place_sections (abfd
, stash
))
5366 /* There can be more than one DWARF2 info section in a BFD these
5367 days. First handle the easy case when there's only one. If
5368 there's more than one, try case two: none of the sections is
5369 compressed. In that case, read them all in and produce one
5370 large stash. We do this in two passes - in the first pass we
5371 just accumulate the section sizes, and in the second pass we
5372 read in the section's contents. (The allows us to avoid
5373 reallocing the data as we add sections to the stash.) If
5374 some or all sections are compressed, then do things the slow
5375 way, with a bunch of reallocs. */
5377 if (! find_debug_info (debug_bfd
, debug_sections
, msec
))
5379 /* Case 1: only one info section. */
5380 total_size
= msec
->size
;
5381 if (! read_section (debug_bfd
, &stash
->debug_sections
[debug_info
],
5383 &stash
->f
.dwarf_info_buffer
, &total_size
))
5388 /* Case 2: multiple sections. */
5389 for (total_size
= 0;
5391 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
5393 /* Catch PR25070 testcase overflowing size calculation here. */
5394 if (total_size
+ msec
->size
< total_size
5395 || total_size
+ msec
->size
< msec
->size
)
5397 bfd_set_error (bfd_error_no_memory
);
5400 total_size
+= msec
->size
;
5403 stash
->f
.dwarf_info_buffer
= (bfd_byte
*) bfd_malloc (total_size
);
5404 if (stash
->f
.dwarf_info_buffer
== NULL
)
5408 for (msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
5410 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
5418 if (!(bfd_simple_get_relocated_section_contents
5419 (debug_bfd
, msec
, stash
->f
.dwarf_info_buffer
+ total_size
,
5427 stash
->f
.info_ptr
= stash
->f
.dwarf_info_buffer
;
5428 stash
->f
.dwarf_info_size
= total_size
;
5432 /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
5434 static struct comp_unit
*
5435 stash_comp_unit (struct dwarf2_debug
*stash
, struct dwarf2_debug_file
*file
)
5437 bfd_size_type length
;
5438 unsigned int offset_size
;
5439 bfd_byte
*info_ptr_unit
= file
->info_ptr
;
5440 bfd_byte
*info_ptr_end
= file
->dwarf_info_buffer
+ file
->dwarf_info_size
;
5442 if (file
->info_ptr
>= info_ptr_end
)
5445 length
= read_4_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
5446 /* A 0xffffff length is the DWARF3 way of indicating
5447 we use 64-bit offsets, instead of 32-bit offsets. */
5448 if (length
== 0xffffffff)
5451 length
= read_8_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
5453 /* A zero length is the IRIX way of indicating 64-bit offsets,
5454 mostly because the 64-bit length will generally fit in 32
5455 bits, and the endianness helps. */
5456 else if (length
== 0)
5459 length
= read_4_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
5461 /* In the absence of the hints above, we assume 32-bit DWARF2
5462 offsets even for targets with 64-bit addresses, because:
5463 a) most of the time these targets will not have generated
5464 more than 2Gb of debug info and so will not need 64-bit
5467 b) if they do use 64-bit offsets but they are not using
5468 the size hints that are tested for above then they are
5469 not conforming to the DWARF3 standard anyway. */
5474 && length
<= (size_t) (info_ptr_end
- file
->info_ptr
))
5476 struct comp_unit
*each
= parse_comp_unit (stash
, file
,
5477 file
->info_ptr
, length
,
5478 info_ptr_unit
, offset_size
);
5481 if (file
->all_comp_units
)
5482 file
->all_comp_units
->prev_unit
= each
;
5484 file
->last_comp_unit
= each
;
5486 each
->next_unit
= file
->all_comp_units
;
5487 file
->all_comp_units
= each
;
5489 if (each
->arange
.high
== 0)
5491 each
->next_unit_without_ranges
= file
->all_comp_units_without_ranges
;
5492 file
->all_comp_units_without_ranges
= each
->next_unit_without_ranges
;
5495 file
->info_ptr
+= length
;
5500 /* Don't trust any of the DWARF info after a corrupted length or
5502 file
->info_ptr
= info_ptr_end
;
5506 /* Hash function for an asymbol. */
5509 hash_asymbol (const void *sym
)
5511 const asymbol
*asym
= sym
;
5512 return htab_hash_string (asym
->name
);
5515 /* Equality function for asymbols. */
5518 eq_asymbol (const void *a
, const void *b
)
5520 const asymbol
*sa
= a
;
5521 const asymbol
*sb
= b
;
5522 return strcmp (sa
->name
, sb
->name
) == 0;
5525 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
5526 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
5527 symbol in SYMBOLS and return the difference between the low_pc and
5528 the symbol's address. Returns 0 if no suitable symbol could be found. */
5531 _bfd_dwarf2_find_symbol_bias (asymbol
** symbols
, void ** pinfo
)
5533 struct dwarf2_debug
*stash
;
5534 struct comp_unit
* unit
;
5536 bfd_signed_vma result
= 0;
5539 stash
= (struct dwarf2_debug
*) *pinfo
;
5541 if (stash
== NULL
|| symbols
== NULL
)
5544 sym_hash
= htab_create_alloc (10, hash_asymbol
, eq_asymbol
,
5545 NULL
, xcalloc
, free
);
5546 for (psym
= symbols
; * psym
!= NULL
; psym
++)
5548 asymbol
* sym
= * psym
;
5550 if (sym
->flags
& BSF_FUNCTION
&& sym
->section
!= NULL
)
5552 void **slot
= htab_find_slot (sym_hash
, sym
, INSERT
);
5557 for (unit
= stash
->f
.all_comp_units
; unit
; unit
= unit
->next_unit
)
5559 struct funcinfo
* func
;
5561 comp_unit_maybe_decode_line_info (unit
);
5563 for (func
= unit
->function_table
; func
!= NULL
; func
= func
->prev_func
)
5564 if (func
->name
&& func
->arange
.low
)
5566 asymbol search
, *sym
;
5568 /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
5570 search
.name
= func
->name
;
5571 sym
= htab_find (sym_hash
, &search
);
5574 result
= ((bfd_signed_vma
) func
->arange
.low
) -
5575 ((bfd_signed_vma
) (sym
->value
+ sym
->section
->vma
));
5582 htab_delete (sym_hash
);
5586 /* Find the source code location of SYMBOL. If SYMBOL is NULL
5587 then find the nearest source code location corresponding to
5588 the address SECTION + OFFSET.
5589 Returns 1 if the line is found without error and fills in
5590 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
5591 NULL the FUNCTIONNAME_PTR is also filled in.
5592 Returns 2 if partial information from _bfd_elf_find_function is
5593 returned (function and maybe file) by looking at symbols. DWARF2
5594 info is present but not regarding the requested code location.
5595 Returns 0 otherwise.
5596 SYMBOLS contains the symbol table for ABFD.
5597 DEBUG_SECTIONS contains the name of the dwarf debug sections. */
5600 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
5605 const char **filename_ptr
,
5606 const char **functionname_ptr
,
5607 unsigned int *linenumber_ptr
,
5608 unsigned int *discriminator_ptr
,
5609 const struct dwarf_debug_section
*debug_sections
,
5612 /* Read each compilation unit from the section .debug_info, and check
5613 to see if it contains the address we are searching for. If yes,
5614 lookup the address, and return the line number info. If no, go
5615 on to the next compilation unit.
5617 We keep a list of all the previously read compilation units, and
5618 a pointer to the next un-read compilation unit. Check the
5619 previously read units before reading more. */
5620 struct dwarf2_debug
*stash
;
5621 /* What address are we looking for? */
5623 struct comp_unit
* each
;
5624 struct funcinfo
*function
= NULL
;
5628 *filename_ptr
= NULL
;
5629 if (functionname_ptr
!= NULL
)
5630 *functionname_ptr
= NULL
;
5631 *linenumber_ptr
= 0;
5632 if (discriminator_ptr
)
5633 *discriminator_ptr
= 0;
5635 if (! _bfd_dwarf2_slurp_debug_info (abfd
, NULL
, debug_sections
,
5637 (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0))
5640 stash
= (struct dwarf2_debug
*) *pinfo
;
5642 do_line
= symbol
!= NULL
;
5645 BFD_ASSERT (section
== NULL
&& offset
== 0 && functionname_ptr
== NULL
);
5646 section
= bfd_asymbol_section (symbol
);
5647 addr
= symbol
->value
;
5651 BFD_ASSERT (section
!= NULL
&& functionname_ptr
!= NULL
);
5654 /* If we have no SYMBOL but the section we're looking at is not a
5655 code section, then take a look through the list of symbols to see
5656 if we have a symbol at the address we're looking for. If we do
5657 then use this to look up line information. This will allow us to
5658 give file and line results for data symbols. We exclude code
5659 symbols here, if we look up a function symbol and then look up the
5660 line information we'll actually return the line number for the
5661 opening '{' rather than the function definition line. This is
5662 because looking up by symbol uses the line table, in which the
5663 first line for a function is usually the opening '{', while
5664 looking up the function by section + offset uses the
5665 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
5666 which will be the line of the function name. */
5667 if (symbols
!= NULL
&& (section
->flags
& SEC_CODE
) == 0)
5671 for (tmp
= symbols
; (*tmp
) != NULL
; ++tmp
)
5672 if ((*tmp
)->the_bfd
== abfd
5673 && (*tmp
)->section
== section
5674 && (*tmp
)->value
== offset
5675 && ((*tmp
)->flags
& BSF_SECTION_SYM
) == 0)
5679 /* For local symbols, keep going in the hope we find a
5681 if ((symbol
->flags
& BSF_GLOBAL
) != 0)
5687 if (section
->output_section
)
5688 addr
+= section
->output_section
->vma
+ section
->output_offset
;
5690 addr
+= section
->vma
;
5692 /* A null info_ptr indicates that there is no dwarf2 info
5693 (or that an error occured while setting up the stash). */
5694 if (! stash
->f
.info_ptr
)
5697 stash
->inliner_chain
= NULL
;
5699 /* Check the previously read comp. units first. */
5702 /* The info hash tables use quite a bit of memory. We may not want to
5703 always use them. We use some heuristics to decide if and when to
5705 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
5706 stash_maybe_enable_info_hash_tables (abfd
, stash
);
5708 /* Keep info hash table up to date if they are available. Note that we
5709 may disable the hash tables if there is any error duing update. */
5710 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
5711 stash_maybe_update_info_hash_tables (stash
);
5713 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
5715 found
= stash_find_line_fast (stash
, symbol
, addr
, filename_ptr
,
5722 /* Check the previously read comp. units first. */
5723 for (each
= stash
->f
.all_comp_units
; each
; each
= each
->next_unit
)
5724 if ((symbol
->flags
& BSF_FUNCTION
) == 0
5725 || each
->arange
.high
== 0
5726 || comp_unit_contains_address (each
, addr
))
5728 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
5737 struct trie_node
*trie
= stash
->f
.trie_root
;
5738 unsigned int bits
= VMA_BITS
- 8;
5739 struct comp_unit
**prev_each
;
5741 /* Traverse interior nodes until we get to a leaf. */
5742 while (trie
&& trie
->num_room_in_leaf
== 0)
5744 int ch
= (addr
>> bits
) & 0xff;
5745 trie
= ((struct trie_interior
*) trie
)->children
[ch
];
5751 const struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
5754 for (i
= 0; i
< leaf
->num_stored_in_leaf
; ++i
)
5756 leaf
->ranges
[i
].unit
->mark
= false;
5759 for (i
= 0; i
< leaf
->num_stored_in_leaf
; ++i
)
5761 struct comp_unit
*unit
= leaf
->ranges
[i
].unit
;
5763 addr
< leaf
->ranges
[i
].low_pc
||
5764 addr
>= leaf
->ranges
[i
].high_pc
)
5768 found
= comp_unit_find_nearest_line (unit
, addr
,
5778 /* Also scan through all compilation units without any ranges,
5779 taking them out of the list if they have acquired any since
5781 prev_each
= &stash
->f
.all_comp_units_without_ranges
;
5782 for (each
= *prev_each
; each
; each
= each
->next_unit_without_ranges
)
5784 if (each
->arange
.high
!= 0)
5786 *prev_each
= each
->next_unit_without_ranges
;
5790 found
= comp_unit_find_nearest_line (each
, addr
,
5797 prev_each
= &each
->next_unit_without_ranges
;
5801 /* Read each remaining comp. units checking each as they are read. */
5802 while ((each
= stash_comp_unit (stash
, &stash
->f
)) != NULL
)
5804 /* DW_AT_low_pc and DW_AT_high_pc are optional for
5805 compilation units. If we don't have them (i.e.,
5806 unit->high == 0), we need to consult the line info table
5807 to see if a compilation unit contains the given
5810 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
5811 || each
->arange
.high
== 0
5812 || comp_unit_contains_address (each
, addr
))
5813 && comp_unit_find_line (each
, symbol
, addr
,
5814 filename_ptr
, linenumber_ptr
));
5816 found
= ((each
->arange
.high
== 0
5817 || comp_unit_contains_address (each
, addr
))
5818 && comp_unit_find_nearest_line (each
, addr
,
5822 discriminator_ptr
));
5829 if (functionname_ptr
&& function
&& function
->is_linkage
)
5831 *functionname_ptr
= function
->name
;
5835 else if (functionname_ptr
5836 && (!*functionname_ptr
5837 || (function
&& !function
->is_linkage
)))
5840 asymbol
**syms
= symbols
;
5841 asection
*sec
= section
;
5843 _bfd_dwarf2_stash_syms (stash
, abfd
, &sec
, &syms
);
5844 fun
= _bfd_elf_find_function (abfd
, syms
, sec
, offset
,
5845 *filename_ptr
? NULL
: filename_ptr
,
5848 if (!found
&& fun
!= NULL
)
5851 if (function
&& !function
->is_linkage
)
5855 sec_vma
= section
->vma
;
5856 if (section
->output_section
!= NULL
)
5857 sec_vma
= section
->output_section
->vma
+ section
->output_offset
;
5859 *functionname_ptr
= function
->name
;
5860 else if (fun
->value
+ sec_vma
== function
->arange
.low
)
5861 function
->name
= *functionname_ptr
;
5862 /* Even if we didn't find a linkage name, say that we have
5863 to stop a repeated search of symbols. */
5864 function
->is_linkage
= true;
5868 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
5869 unset_sections (stash
);
5875 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
5876 const char **filename_ptr
,
5877 const char **functionname_ptr
,
5878 unsigned int *linenumber_ptr
,
5881 struct dwarf2_debug
*stash
;
5883 stash
= (struct dwarf2_debug
*) *pinfo
;
5886 struct funcinfo
*func
= stash
->inliner_chain
;
5888 if (func
&& func
->caller_func
)
5890 *filename_ptr
= func
->caller_file
;
5891 *functionname_ptr
= func
->caller_func
->name
;
5892 *linenumber_ptr
= func
->caller_line
;
5893 stash
->inliner_chain
= func
->caller_func
;
5902 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
, void **pinfo
)
5904 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
5905 struct comp_unit
*each
;
5906 struct dwarf2_debug_file
*file
;
5908 if (abfd
== NULL
|| stash
== NULL
)
5911 if (stash
->varinfo_hash_table
)
5912 bfd_hash_table_free (&stash
->varinfo_hash_table
->base
);
5913 if (stash
->funcinfo_hash_table
)
5914 bfd_hash_table_free (&stash
->funcinfo_hash_table
->base
);
5919 for (each
= file
->all_comp_units
; each
; each
= each
->next_unit
)
5921 struct funcinfo
*function_table
= each
->function_table
;
5922 struct varinfo
*variable_table
= each
->variable_table
;
5924 if (each
->line_table
&& each
->line_table
!= file
->line_table
)
5926 free (each
->line_table
->files
);
5927 free (each
->line_table
->dirs
);
5930 free (each
->lookup_funcinfo_table
);
5931 each
->lookup_funcinfo_table
= NULL
;
5933 while (function_table
)
5935 free (function_table
->file
);
5936 function_table
->file
= NULL
;
5937 free (function_table
->caller_file
);
5938 function_table
->caller_file
= NULL
;
5939 function_table
= function_table
->prev_func
;
5942 while (variable_table
)
5944 free (variable_table
->file
);
5945 variable_table
->file
= NULL
;
5946 variable_table
= variable_table
->prev_var
;
5950 if (file
->line_table
)
5952 free (file
->line_table
->files
);
5953 free (file
->line_table
->dirs
);
5955 htab_delete (file
->abbrev_offsets
);
5957 free (file
->dwarf_line_str_buffer
);
5958 free (file
->dwarf_str_buffer
);
5959 free (file
->dwarf_ranges_buffer
);
5960 free (file
->dwarf_line_buffer
);
5961 free (file
->dwarf_abbrev_buffer
);
5962 free (file
->dwarf_info_buffer
);
5963 if (file
== &stash
->alt
)
5967 free (stash
->sec_vma
);
5968 free (stash
->adjusted_sections
);
5969 if (stash
->close_on_cleanup
)
5970 bfd_close (stash
->f
.bfd_ptr
);
5971 if (stash
->alt
.bfd_ptr
)
5972 bfd_close (stash
->alt
.bfd_ptr
);
5975 /* Find the function to a particular section and offset,
5976 for error reporting. */
5979 _bfd_elf_find_function (bfd
*abfd
,
5983 const char **filename_ptr
,
5984 const char **functionname_ptr
)
5986 struct elf_find_function_cache
5988 asection
*last_section
;
5990 const char *filename
;
5991 bfd_size_type func_size
;
5994 if (symbols
== NULL
)
5997 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6000 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
6003 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
6004 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
6008 if (cache
->last_section
!= section
6009 || cache
->func
== NULL
6010 || offset
< cache
->func
->value
6011 || offset
>= cache
->func
->value
+ cache
->func_size
)
6016 /* ??? Given multiple file symbols, it is impossible to reliably
6017 choose the right file name for global symbols. File symbols are
6018 local symbols, and thus all file symbols must sort before any
6019 global symbols. The ELF spec may be interpreted to say that a
6020 file symbol must sort before other local symbols, but currently
6021 ld -r doesn't do this. So, for ld -r output, it is possible to
6022 make a better choice of file name for local symbols by ignoring
6023 file symbols appearing after a given local symbol. */
6024 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
6025 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6029 state
= nothing_seen
;
6030 cache
->filename
= NULL
;
6032 cache
->func_size
= 0;
6033 cache
->last_section
= section
;
6035 for (p
= symbols
; *p
!= NULL
; p
++)
6041 if ((sym
->flags
& BSF_FILE
) != 0)
6044 if (state
== symbol_seen
)
6045 state
= file_after_symbol_seen
;
6049 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
6051 && code_off
<= offset
6052 && (code_off
> low_func
6053 || (code_off
== low_func
6054 && size
> cache
->func_size
)))
6057 cache
->func_size
= size
;
6058 cache
->filename
= NULL
;
6059 low_func
= code_off
;
6061 && ((sym
->flags
& BSF_LOCAL
) != 0
6062 || state
!= file_after_symbol_seen
))
6063 cache
->filename
= bfd_asymbol_name (file
);
6065 if (state
== nothing_seen
)
6066 state
= symbol_seen
;
6070 if (cache
->func
== NULL
)
6074 *filename_ptr
= cache
->filename
;
6075 if (functionname_ptr
)
6076 *functionname_ptr
= bfd_asymbol_name (cache
->func
);