2 Copyright (C) 1994-2022 Free Software Foundation, Inc.
4 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
7 From the dwarf2read.c header:
8 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
9 Inc. with support from Florida State University (under contract
10 with the Ada Joint Program Office), and Silicon Graphics, Inc.
11 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
12 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
13 support in dwarfread.c
15 This file is part of BFD.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 3 of the License, or (at
20 your option) any later version.
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
30 MA 02110-1301, USA. */
34 #include "libiberty.h"
40 /* The data in the .debug_line statement prologue looks like this. */
45 unsigned short version
;
46 bfd_vma prologue_length
;
47 unsigned char minimum_instruction_length
;
48 unsigned char maximum_ops_per_insn
;
49 unsigned char default_is_stmt
;
51 unsigned char line_range
;
52 unsigned char opcode_base
;
53 unsigned char *standard_opcode_lengths
;
56 /* Attributes have a name and a value. */
60 enum dwarf_attribute name
;
65 struct dwarf_block
*blk
;
72 /* Blocks are a bunch of untyped bytes. */
79 struct adjusted_section
85 /* A trie to map quickly from address range to compilation unit.
87 This is a fairly standard radix-256 trie, used to quickly locate which
88 compilation unit any given address belongs to. Given that each compilation
89 unit may register hundreds of very small and unaligned ranges (which may
90 potentially overlap, due to inlining and other concerns), and a large
91 program may end up containing hundreds of thousands of such ranges, we cannot
92 scan through them linearly without undue slowdown.
94 We use a hybrid trie to avoid memory explosion: There are two types of trie
95 nodes, leaves and interior nodes. (Almost all nodes are leaves, so they
96 take up the bulk of the memory usage.) Leaves contain a simple array of
97 ranges (high/low address) and which compilation unit contains those ranges,
98 and when we get to a leaf, we scan through it linearly. Interior nodes
99 contain pointers to 256 other nodes, keyed by the next byte of the address.
100 So for a 64-bit address like 0x1234567abcd, we would start at the root and go
101 down child[0x00]->child[0x00]->child[0x01]->child[0x23]->child[0x45] etc.,
102 until we hit a leaf. (Nodes are, in general, leaves until they exceed the
103 default allocation of 16 elements, at which point they are converted to
104 interior node if possible.) This gives us near-constant lookup times;
105 the only thing that can be costly is if there are lots of overlapping ranges
106 within a single 256-byte segment of the binary, in which case we have to
107 scan through them all to find the best match.
109 For a binary with few ranges, we will in practice only have a single leaf
110 node at the root, containing a simple array. Thus, the scheme is efficient
111 for both small and large binaries.
114 /* Experiments have shown 16 to be a memory-efficient default leaf size.
115 The only case where a leaf will hold more memory than this, is at the
116 bottomost level (covering 256 bytes in the binary), where we'll expand
117 the leaf to be able to hold more ranges if needed.
119 #define TRIE_LEAF_SIZE 16
121 /* All trie_node pointers will really be trie_leaf or trie_interior,
122 but they have this common head. */
125 /* If zero, we are an interior node.
126 Otherwise, how many ranges we have room for in this leaf. */
127 unsigned int num_room_in_leaf
;
132 struct trie_node head
;
133 unsigned int num_stored_in_leaf
;
135 struct comp_unit
*unit
;
136 bfd_vma low_pc
, high_pc
;
137 } ranges
[TRIE_LEAF_SIZE
];
142 struct trie_node head
;
143 struct trie_node
*children
[256];
146 static struct trie_node
*alloc_trie_leaf (bfd
*abfd
)
148 struct trie_leaf
*leaf
= bfd_zalloc (abfd
, sizeof (struct trie_leaf
));
151 leaf
->head
.num_room_in_leaf
= TRIE_LEAF_SIZE
;
155 struct dwarf2_debug_file
157 /* The actual bfd from which debug info was loaded. Might be
158 different to orig_bfd because of gnu_debuglink sections. */
161 /* Pointer to the symbol table. */
164 /* The current info pointer for the .debug_info section being parsed. */
167 /* A pointer to the memory block allocated for .debug_info sections. */
168 bfd_byte
*dwarf_info_buffer
;
170 /* Length of the loaded .debug_info sections. */
171 bfd_size_type dwarf_info_size
;
173 /* Pointer to the .debug_abbrev section loaded into memory. */
174 bfd_byte
*dwarf_abbrev_buffer
;
176 /* Length of the loaded .debug_abbrev section. */
177 bfd_size_type dwarf_abbrev_size
;
179 /* Buffer for decode_line_info. */
180 bfd_byte
*dwarf_line_buffer
;
182 /* Length of the loaded .debug_line section. */
183 bfd_size_type dwarf_line_size
;
185 /* Pointer to the .debug_str section loaded into memory. */
186 bfd_byte
*dwarf_str_buffer
;
188 /* Length of the loaded .debug_str section. */
189 bfd_size_type dwarf_str_size
;
191 /* Pointer to the .debug_str_offsets section loaded into memory. */
192 bfd_byte
*dwarf_str_offsets_buffer
;
194 /* Length of the loaded .debug_str_offsets section. */
195 bfd_size_type dwarf_str_offsets_size
;
197 /* Pointer to the .debug_addr section loaded into memory. */
198 bfd_byte
*dwarf_addr_buffer
;
200 /* Length of the loaded .debug_addr section. */
201 bfd_size_type dwarf_addr_size
;
203 /* Pointer to the .debug_line_str section loaded into memory. */
204 bfd_byte
*dwarf_line_str_buffer
;
206 /* Length of the loaded .debug_line_str section. */
207 bfd_size_type dwarf_line_str_size
;
209 /* Pointer to the .debug_ranges section loaded into memory. */
210 bfd_byte
*dwarf_ranges_buffer
;
212 /* Length of the loaded .debug_ranges section. */
213 bfd_size_type dwarf_ranges_size
;
215 /* Pointer to the .debug_rnglists section loaded into memory. */
216 bfd_byte
*dwarf_rnglists_buffer
;
218 /* Length of the loaded .debug_rnglists section. */
219 bfd_size_type dwarf_rnglists_size
;
221 /* A list of all previously read comp_units. */
222 struct comp_unit
*all_comp_units
;
224 /* A list of all previously read comp_units with no ranges (yet). */
225 struct comp_unit
*all_comp_units_without_ranges
;
227 /* Last comp unit in list above. */
228 struct comp_unit
*last_comp_unit
;
230 /* Line table at line_offset zero. */
231 struct line_info_table
*line_table
;
233 /* Hash table to map offsets to decoded abbrevs. */
234 htab_t abbrev_offsets
;
236 /* Root of a trie to map addresses to compilation units. */
237 struct trie_node
*trie_root
;
242 /* Names of the debug sections. */
243 const struct dwarf_debug_section
*debug_sections
;
245 /* Per-file stuff. */
246 struct dwarf2_debug_file f
, alt
;
248 /* Pointer to the original bfd for which debug was loaded. This is what
249 we use to compare and so check that the cached debug data is still
250 valid - it saves having to possibly dereference the gnu_debuglink each
254 /* If the most recent call to bfd_find_nearest_line was given an
255 address in an inlined function, preserve a pointer into the
256 calling chain for subsequent calls to bfd_find_inliner_info to
258 struct funcinfo
*inliner_chain
;
260 /* Section VMAs at the time the stash was built. */
262 /* Number of sections in the SEC_VMA table. */
263 unsigned int sec_vma_count
;
265 /* Number of sections whose VMA we must adjust. */
266 int adjusted_section_count
;
268 /* Array of sections with adjusted VMA. */
269 struct adjusted_section
*adjusted_sections
;
271 /* Number of times find_line is called. This is used in
272 the heuristic for enabling the info hash tables. */
275 #define STASH_INFO_HASH_TRIGGER 100
277 /* Hash table mapping symbol names to function infos. */
278 struct info_hash_table
*funcinfo_hash_table
;
280 /* Hash table mapping symbol names to variable infos. */
281 struct info_hash_table
*varinfo_hash_table
;
283 /* Head of comp_unit list in the last hash table update. */
284 struct comp_unit
*hash_units_head
;
286 /* Status of info hash. */
287 int info_hash_status
;
288 #define STASH_INFO_HASH_OFF 0
289 #define STASH_INFO_HASH_ON 1
290 #define STASH_INFO_HASH_DISABLED 2
292 /* True if we opened bfd_ptr. */
293 bool close_on_cleanup
;
303 /* A minimal decoding of DWARF2 compilation units. We only decode
304 what's needed to get to the line number information. */
308 /* Chain the previously read compilation units. */
309 struct comp_unit
*next_unit
;
311 /* Chain the previously read compilation units that have no ranges yet.
312 We scan these separately when we have a trie over the ranges.
313 Unused if arange.high != 0. */
314 struct comp_unit
*next_unit_without_ranges
;
316 /* Likewise, chain the compilation unit read after this one.
317 The comp units are stored in reversed reading order. */
318 struct comp_unit
*prev_unit
;
320 /* Keep the bfd convenient (for memory allocation). */
323 /* The lowest and highest addresses contained in this compilation
324 unit as specified in the compilation unit header. */
325 struct arange arange
;
327 /* The DW_AT_name attribute (for error messages). */
330 /* The abbrev hash table. */
331 struct abbrev_info
**abbrevs
;
333 /* DW_AT_language. */
336 /* Note that an error was found by comp_unit_find_nearest_line. */
339 /* The DW_AT_comp_dir attribute. */
342 /* TRUE if there is a line number table associated with this comp. unit. */
345 /* Pointer to the current comp_unit so that we can find a given entry
347 bfd_byte
*info_ptr_unit
;
349 /* The offset into .debug_line of the line number table. */
350 unsigned long line_offset
;
352 /* Pointer to the first child die for the comp unit. */
353 bfd_byte
*first_child_die_ptr
;
355 /* The end of the comp unit. */
358 /* The decoded line number, NULL if not yet decoded. */
359 struct line_info_table
*line_table
;
361 /* A list of the functions found in this comp. unit. */
362 struct funcinfo
*function_table
;
364 /* A table of function information references searchable by address. */
365 struct lookup_funcinfo
*lookup_funcinfo_table
;
367 /* Number of functions in the function_table and sorted_function_table. */
368 bfd_size_type number_of_functions
;
370 /* A list of the variables found in this comp. unit. */
371 struct varinfo
*variable_table
;
373 /* Pointers to dwarf2_debug structures. */
374 struct dwarf2_debug
*stash
;
375 struct dwarf2_debug_file
*file
;
377 /* DWARF format version for this unit - from unit header. */
380 /* Address size for this unit - from unit header. */
381 unsigned char addr_size
;
383 /* Offset size for this unit - from unit header. */
384 unsigned char offset_size
;
386 /* Base address for this unit - from DW_AT_low_pc attribute of
387 DW_TAG_compile_unit DIE */
388 bfd_vma base_address
;
390 /* TRUE if symbols are cached in hash table for faster lookup by name. */
393 /* Used when iterating over trie leaves to know which units we have
394 already seen in this iteration. */
397 /* Base address of debug_addr section. */
398 size_t dwarf_addr_offset
;
400 /* Base address of string offset table. */
401 size_t dwarf_str_offset
;
404 /* This data structure holds the information of an abbrev. */
407 unsigned int number
; /* Number identifying abbrev. */
408 enum dwarf_tag tag
; /* DWARF tag. */
409 bool has_children
; /* TRUE if the abbrev has children. */
410 unsigned int num_attrs
; /* Number of attributes. */
411 struct attr_abbrev
* attrs
; /* An array of attribute descriptions. */
412 struct abbrev_info
* next
; /* Next in chain. */
417 enum dwarf_attribute name
;
418 enum dwarf_form form
;
419 bfd_vma implicit_const
;
422 /* Map of uncompressed DWARF debug section name to compressed one. It
423 is terminated by NULL uncompressed_name. */
425 const struct dwarf_debug_section dwarf_debug_sections
[] =
427 { ".debug_abbrev", ".zdebug_abbrev" },
428 { ".debug_aranges", ".zdebug_aranges" },
429 { ".debug_frame", ".zdebug_frame" },
430 { ".debug_info", ".zdebug_info" },
431 { ".debug_info", ".zdebug_info" },
432 { ".debug_line", ".zdebug_line" },
433 { ".debug_loc", ".zdebug_loc" },
434 { ".debug_macinfo", ".zdebug_macinfo" },
435 { ".debug_macro", ".zdebug_macro" },
436 { ".debug_pubnames", ".zdebug_pubnames" },
437 { ".debug_pubtypes", ".zdebug_pubtypes" },
438 { ".debug_ranges", ".zdebug_ranges" },
439 { ".debug_rnglists", ".zdebug_rnglist" },
440 { ".debug_static_func", ".zdebug_static_func" },
441 { ".debug_static_vars", ".zdebug_static_vars" },
442 { ".debug_str", ".zdebug_str", },
443 { ".debug_str", ".zdebug_str", },
444 { ".debug_str_offsets", ".zdebug_str_offsets", },
445 { ".debug_addr", ".zdebug_addr", },
446 { ".debug_line_str", ".zdebug_line_str", },
447 { ".debug_types", ".zdebug_types" },
448 /* GNU DWARF 1 extensions */
449 { ".debug_sfnames", ".zdebug_sfnames" },
450 { ".debug_srcinfo", ".zebug_srcinfo" },
451 /* SGI/MIPS DWARF 2 extensions */
452 { ".debug_funcnames", ".zdebug_funcnames" },
453 { ".debug_typenames", ".zdebug_typenames" },
454 { ".debug_varnames", ".zdebug_varnames" },
455 { ".debug_weaknames", ".zdebug_weaknames" },
459 /* NB/ Numbers in this enum must match up with indices
460 into the dwarf_debug_sections[] array above. */
461 enum dwarf_debug_section_enum
493 /* A static assertion. */
494 extern int dwarf_debug_section_assert
[ARRAY_SIZE (dwarf_debug_sections
)
495 == debug_max
+ 1 ? 1 : -1];
497 #ifndef ABBREV_HASH_SIZE
498 #define ABBREV_HASH_SIZE 121
500 #ifndef ATTR_ALLOC_CHUNK
501 #define ATTR_ALLOC_CHUNK 4
504 /* Variable and function hash tables. This is used to speed up look-up
505 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
506 In order to share code between variable and function infos, we use
507 a list of untyped pointer for all variable/function info associated with
508 a symbol. We waste a bit of memory for list with one node but that
509 simplifies the code. */
511 struct info_list_node
513 struct info_list_node
*next
;
517 /* Info hash entry. */
518 struct info_hash_entry
520 struct bfd_hash_entry root
;
521 struct info_list_node
*head
;
524 struct info_hash_table
526 struct bfd_hash_table base
;
529 /* Function to create a new entry in info hash table. */
531 static struct bfd_hash_entry
*
532 info_hash_table_newfunc (struct bfd_hash_entry
*entry
,
533 struct bfd_hash_table
*table
,
536 struct info_hash_entry
*ret
= (struct info_hash_entry
*) entry
;
538 /* Allocate the structure if it has not already been allocated by a
542 ret
= (struct info_hash_entry
*) bfd_hash_allocate (table
,
548 /* Call the allocation method of the base class. */
549 ret
= ((struct info_hash_entry
*)
550 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
552 /* Initialize the local fields here. */
556 return (struct bfd_hash_entry
*) ret
;
559 /* Function to create a new info hash table. It returns a pointer to the
560 newly created table or NULL if there is any error. We need abfd
561 solely for memory allocation. */
563 static struct info_hash_table
*
564 create_info_hash_table (bfd
*abfd
)
566 struct info_hash_table
*hash_table
;
568 hash_table
= ((struct info_hash_table
*)
569 bfd_alloc (abfd
, sizeof (struct info_hash_table
)));
573 if (!bfd_hash_table_init (&hash_table
->base
, info_hash_table_newfunc
,
574 sizeof (struct info_hash_entry
)))
576 bfd_release (abfd
, hash_table
);
583 /* Insert an info entry into an info hash table. We do not check of
584 duplicate entries. Also, the caller need to guarantee that the
585 right type of info in inserted as info is passed as a void* pointer.
586 This function returns true if there is no error. */
589 insert_info_hash_table (struct info_hash_table
*hash_table
,
594 struct info_hash_entry
*entry
;
595 struct info_list_node
*node
;
597 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
,
602 node
= (struct info_list_node
*) bfd_hash_allocate (&hash_table
->base
,
608 node
->next
= entry
->head
;
614 /* Look up an info entry list from an info hash table. Return NULL
617 static struct info_list_node
*
618 lookup_info_hash_table (struct info_hash_table
*hash_table
, const char *key
)
620 struct info_hash_entry
*entry
;
622 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
, key
,
624 return entry
? entry
->head
: NULL
;
627 /* Read a section into its appropriate place in the dwarf2_debug
628 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
629 not NULL, use bfd_simple_get_relocated_section_contents to read the
630 section contents, otherwise use bfd_get_section_contents. Fail if
631 the located section does not contain at least OFFSET bytes. */
634 read_section (bfd
*abfd
,
635 const struct dwarf_debug_section
*sec
,
638 bfd_byte
**section_buffer
,
639 bfd_size_type
*section_size
)
641 const char *section_name
= sec
->uncompressed_name
;
642 bfd_byte
*contents
= *section_buffer
;
644 /* The section may have already been read. */
645 if (contents
== NULL
)
651 msec
= bfd_get_section_by_name (abfd
, section_name
);
654 section_name
= sec
->compressed_name
;
655 msec
= bfd_get_section_by_name (abfd
, section_name
);
659 _bfd_error_handler (_("DWARF error: can't find %s section."),
660 sec
->uncompressed_name
);
661 bfd_set_error (bfd_error_bad_value
);
665 amt
= bfd_get_section_limit_octets (abfd
, msec
);
666 filesize
= bfd_get_file_size (abfd
);
667 /* PR 28834: A compressed debug section could well decompress to a size
668 larger than the file, so we choose an arbitrary modifier of 10x in
669 the test below. If this ever turns out to be insufficient, it can
670 be changed by a future update. */
671 if (amt
>= filesize
* 10)
674 _bfd_error_handler (_("DWARF error: section %s is larger than 10x its filesize! (0x%lx vs 0x%lx)"),
675 section_name
, (long) amt
, (long) filesize
);
676 bfd_set_error (bfd_error_bad_value
);
680 /* Paranoia - alloc one extra so that we can make sure a string
681 section is NUL terminated. */
685 /* Paranoia - this should never happen. */
686 bfd_set_error (bfd_error_no_memory
);
689 contents
= (bfd_byte
*) bfd_malloc (amt
);
690 if (contents
== NULL
)
693 ? !bfd_simple_get_relocated_section_contents (abfd
, msec
, contents
,
695 : !bfd_get_section_contents (abfd
, msec
, contents
, 0, *section_size
))
700 contents
[*section_size
] = 0;
701 *section_buffer
= contents
;
704 /* It is possible to get a bad value for the offset into the section
705 that the client wants. Validate it here to avoid trouble later. */
706 if (offset
!= 0 && offset
>= *section_size
)
708 /* xgettext: c-format */
709 _bfd_error_handler (_("DWARF error: offset (%" PRIu64
")"
710 " greater than or equal to %s size (%" PRIu64
")"),
711 (uint64_t) offset
, section_name
,
712 (uint64_t) *section_size
);
713 bfd_set_error (bfd_error_bad_value
);
720 /* Read dwarf information from a buffer. */
722 static inline uint64_t
723 read_n_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
, int n
)
725 bfd_byte
*buf
= *ptr
;
732 return bfd_get (n
* 8, abfd
, buf
);
736 read_1_byte (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
738 return read_n_bytes (abfd
, ptr
, end
, 1);
742 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
**ptr
, bfd_byte
*end
)
744 bfd_byte
*buf
= *ptr
;
751 return bfd_get_signed_8 (abfd
, buf
);
755 read_2_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
757 return read_n_bytes (abfd
, ptr
, end
, 2);
761 read_3_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
763 unsigned int val
= read_1_byte (abfd
, ptr
, end
);
765 val
|= read_1_byte (abfd
, ptr
, end
);
767 val
|= read_1_byte (abfd
, ptr
, end
);
768 if (bfd_little_endian (abfd
))
769 val
= (((val
>> 16) & 0xff)
771 | ((val
& 0xff) << 16));
776 read_4_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
778 return read_n_bytes (abfd
, ptr
, end
, 4);
782 read_8_bytes (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
)
784 return read_n_bytes (abfd
, ptr
, end
, 8);
787 static struct dwarf_block
*
788 read_blk (bfd
*abfd
, bfd_byte
**ptr
, bfd_byte
*end
, size_t size
)
790 bfd_byte
*buf
= *ptr
;
791 struct dwarf_block
*block
;
793 block
= (struct dwarf_block
*) bfd_alloc (abfd
, sizeof (*block
));
797 if (size
> (size_t) (end
- buf
))
812 /* Scans a NUL terminated string starting at *PTR, returning a pointer to it.
813 Bytes at or beyond BUF_END will not be read. Returns NULL if the
814 terminator is not found or if the string is empty. *PTR is
815 incremented over the bytes scanned, including the terminator. */
818 read_string (bfd_byte
**ptr
,
821 bfd_byte
*buf
= *ptr
;
824 while (buf
< buf_end
)
837 /* Reads an offset from *PTR and then locates the string at this offset
838 inside the debug string section. Returns a pointer to the string.
839 Increments *PTR by the number of bytes read for the offset. This
840 value is set even if the function fails. Bytes at or beyond
841 BUF_END will not be read. Returns NULL if there was a problem, or
842 if the string is empty. Does not check for NUL termination of the
846 read_indirect_string (struct comp_unit
*unit
,
851 struct dwarf2_debug
*stash
= unit
->stash
;
852 struct dwarf2_debug_file
*file
= unit
->file
;
855 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
861 if (unit
->offset_size
== 4)
862 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
864 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
866 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
868 &file
->dwarf_str_buffer
, &file
->dwarf_str_size
))
871 str
= (char *) file
->dwarf_str_buffer
+ offset
;
877 /* Like read_indirect_string but from .debug_line_str section. */
880 read_indirect_line_string (struct comp_unit
*unit
,
885 struct dwarf2_debug
*stash
= unit
->stash
;
886 struct dwarf2_debug_file
*file
= unit
->file
;
889 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
895 if (unit
->offset_size
== 4)
896 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
898 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
900 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_line_str
],
902 &file
->dwarf_line_str_buffer
,
903 &file
->dwarf_line_str_size
))
906 str
= (char *) file
->dwarf_line_str_buffer
+ offset
;
912 /* Like read_indirect_string but uses a .debug_str located in
913 an alternate file pointed to by the .gnu_debugaltlink section.
914 Used to impement DW_FORM_GNU_strp_alt. */
917 read_alt_indirect_string (struct comp_unit
*unit
,
922 struct dwarf2_debug
*stash
= unit
->stash
;
925 if (unit
->offset_size
> (size_t) (buf_end
- *ptr
))
931 if (unit
->offset_size
== 4)
932 offset
= read_4_bytes (unit
->abfd
, ptr
, buf_end
);
934 offset
= read_8_bytes (unit
->abfd
, ptr
, buf_end
);
936 if (stash
->alt
.bfd_ptr
== NULL
)
939 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
941 if (debug_filename
== NULL
)
944 debug_bfd
= bfd_openr (debug_filename
, NULL
);
945 free (debug_filename
);
946 if (debug_bfd
== NULL
)
947 /* FIXME: Should we report our failure to follow the debuglink ? */
950 if (!bfd_check_format (debug_bfd
, bfd_object
))
952 bfd_close (debug_bfd
);
955 stash
->alt
.bfd_ptr
= debug_bfd
;
958 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
959 stash
->debug_sections
+ debug_str_alt
,
960 stash
->alt
.syms
, offset
,
961 &stash
->alt
.dwarf_str_buffer
,
962 &stash
->alt
.dwarf_str_size
))
965 str
= (char *) stash
->alt
.dwarf_str_buffer
+ offset
;
972 /* Resolve an alternate reference from UNIT at OFFSET.
973 Returns a pointer into the loaded alternate CU upon success
974 or NULL upon failure. */
977 read_alt_indirect_ref (struct comp_unit
*unit
, uint64_t offset
)
979 struct dwarf2_debug
*stash
= unit
->stash
;
981 if (stash
->alt
.bfd_ptr
== NULL
)
984 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
986 if (debug_filename
== NULL
)
989 debug_bfd
= bfd_openr (debug_filename
, NULL
);
990 free (debug_filename
);
991 if (debug_bfd
== NULL
)
992 /* FIXME: Should we report our failure to follow the debuglink ? */
995 if (!bfd_check_format (debug_bfd
, bfd_object
))
997 bfd_close (debug_bfd
);
1000 stash
->alt
.bfd_ptr
= debug_bfd
;
1003 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
1004 stash
->debug_sections
+ debug_info_alt
,
1005 stash
->alt
.syms
, offset
,
1006 &stash
->alt
.dwarf_info_buffer
,
1007 &stash
->alt
.dwarf_info_size
))
1010 return stash
->alt
.dwarf_info_buffer
+ offset
;
1014 read_address (struct comp_unit
*unit
, bfd_byte
**ptr
, bfd_byte
*buf_end
)
1016 bfd_byte
*buf
= *ptr
;
1019 if (bfd_get_flavour (unit
->abfd
) == bfd_target_elf_flavour
)
1020 signed_vma
= get_elf_backend_data (unit
->abfd
)->sign_extend_vma
;
1022 if (unit
->addr_size
> (size_t) (buf_end
- buf
))
1028 *ptr
= buf
+ unit
->addr_size
;
1031 switch (unit
->addr_size
)
1034 return bfd_get_signed_64 (unit
->abfd
, buf
);
1036 return bfd_get_signed_32 (unit
->abfd
, buf
);
1038 return bfd_get_signed_16 (unit
->abfd
, buf
);
1045 switch (unit
->addr_size
)
1048 return bfd_get_64 (unit
->abfd
, buf
);
1050 return bfd_get_32 (unit
->abfd
, buf
);
1052 return bfd_get_16 (unit
->abfd
, buf
);
1059 /* Lookup an abbrev_info structure in the abbrev hash table. */
1061 static struct abbrev_info
*
1062 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
1064 unsigned int hash_number
;
1065 struct abbrev_info
*abbrev
;
1067 hash_number
= number
% ABBREV_HASH_SIZE
;
1068 abbrev
= abbrevs
[hash_number
];
1072 if (abbrev
->number
== number
)
1075 abbrev
= abbrev
->next
;
1081 /* We keep a hash table to map .debug_abbrev section offsets to the
1082 array of abbrevs, so that compilation units using the same set of
1083 abbrevs do not waste memory. */
1085 struct abbrev_offset_entry
1088 struct abbrev_info
**abbrevs
;
1092 hash_abbrev (const void *p
)
1094 const struct abbrev_offset_entry
*ent
= p
;
1095 return htab_hash_pointer ((void *) ent
->offset
);
1099 eq_abbrev (const void *pa
, const void *pb
)
1101 const struct abbrev_offset_entry
*a
= pa
;
1102 const struct abbrev_offset_entry
*b
= pb
;
1103 return a
->offset
== b
->offset
;
1107 del_abbrev (void *p
)
1109 struct abbrev_offset_entry
*ent
= p
;
1110 struct abbrev_info
**abbrevs
= ent
->abbrevs
;
1113 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1115 struct abbrev_info
*abbrev
= abbrevs
[i
];
1119 free (abbrev
->attrs
);
1120 abbrev
= abbrev
->next
;
1126 /* In DWARF version 2, the description of the debugging information is
1127 stored in a separate .debug_abbrev section. Before we read any
1128 dies from a section we read in all abbreviations and install them
1131 static struct abbrev_info
**
1132 read_abbrevs (bfd
*abfd
, uint64_t offset
, struct dwarf2_debug
*stash
,
1133 struct dwarf2_debug_file
*file
)
1135 struct abbrev_info
**abbrevs
;
1136 bfd_byte
*abbrev_ptr
;
1137 bfd_byte
*abbrev_end
;
1138 struct abbrev_info
*cur_abbrev
;
1139 unsigned int abbrev_number
, abbrev_name
;
1140 unsigned int abbrev_form
, hash_number
;
1143 struct abbrev_offset_entry ent
= { offset
, NULL
};
1145 if (ent
.offset
!= offset
)
1148 slot
= htab_find_slot (file
->abbrev_offsets
, &ent
, INSERT
);
1152 return ((struct abbrev_offset_entry
*) (*slot
))->abbrevs
;
1154 if (! read_section (abfd
, &stash
->debug_sections
[debug_abbrev
],
1156 &file
->dwarf_abbrev_buffer
,
1157 &file
->dwarf_abbrev_size
))
1160 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
1161 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
1162 if (abbrevs
== NULL
)
1165 abbrev_ptr
= file
->dwarf_abbrev_buffer
+ offset
;
1166 abbrev_end
= file
->dwarf_abbrev_buffer
+ file
->dwarf_abbrev_size
;
1167 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1170 /* Loop until we reach an abbrev number of 0. */
1171 while (abbrev_number
)
1173 amt
= sizeof (struct abbrev_info
);
1174 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
1175 if (cur_abbrev
== NULL
)
1178 /* Read in abbrev header. */
1179 cur_abbrev
->number
= abbrev_number
;
1180 cur_abbrev
->tag
= (enum dwarf_tag
)
1181 _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1183 cur_abbrev
->has_children
= read_1_byte (abfd
, &abbrev_ptr
, abbrev_end
);
1185 /* Now read in declarations. */
1188 /* Initialize it just to avoid a GCC false warning. */
1189 bfd_vma implicit_const
= -1;
1191 abbrev_name
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1193 abbrev_form
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1195 if (abbrev_form
== DW_FORM_implicit_const
)
1196 implicit_const
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1198 if (abbrev_name
== 0)
1201 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
1203 struct attr_abbrev
*tmp
;
1205 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
1206 amt
*= sizeof (struct attr_abbrev
);
1207 tmp
= (struct attr_abbrev
*) bfd_realloc (cur_abbrev
->attrs
, amt
);
1210 cur_abbrev
->attrs
= tmp
;
1213 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
1214 = (enum dwarf_attribute
) abbrev_name
;
1215 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].form
1216 = (enum dwarf_form
) abbrev_form
;
1217 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].implicit_const
1219 ++cur_abbrev
->num_attrs
;
1222 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
1223 cur_abbrev
->next
= abbrevs
[hash_number
];
1224 abbrevs
[hash_number
] = cur_abbrev
;
1226 /* Get next abbreviation.
1227 Under Irix6 the abbreviations for a compilation unit are not
1228 always properly terminated with an abbrev number of 0.
1229 Exit loop if we encounter an abbreviation which we have
1230 already read (which means we are about to read the abbreviations
1231 for the next compile unit) or if the end of the abbreviation
1232 table is reached. */
1233 if ((size_t) (abbrev_ptr
- file
->dwarf_abbrev_buffer
)
1234 >= file
->dwarf_abbrev_size
)
1236 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &abbrev_ptr
,
1238 if (lookup_abbrev (abbrev_number
, abbrevs
) != NULL
)
1242 *slot
= bfd_malloc (sizeof ent
);
1245 ent
.abbrevs
= abbrevs
;
1246 memcpy (*slot
, &ent
, sizeof ent
);
1250 if (abbrevs
!= NULL
)
1254 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1256 struct abbrev_info
*abbrev
= abbrevs
[i
];
1260 free (abbrev
->attrs
);
1261 abbrev
= abbrev
->next
;
1269 /* Returns true if the form is one which has a string value. */
1272 is_str_form (const struct attribute
*attr
)
1276 case DW_FORM_string
:
1283 case DW_FORM_line_strp
:
1284 case DW_FORM_GNU_strp_alt
:
1292 /* Returns true if the form is one which has an integer value. */
1295 is_int_form (const struct attribute
*attr
)
1307 case DW_FORM_ref_addr
:
1312 case DW_FORM_ref_udata
:
1313 case DW_FORM_sec_offset
:
1314 case DW_FORM_flag_present
:
1315 case DW_FORM_ref_sig8
:
1317 case DW_FORM_implicit_const
:
1318 case DW_FORM_addrx1
:
1319 case DW_FORM_addrx2
:
1320 case DW_FORM_addrx3
:
1321 case DW_FORM_addrx4
:
1322 case DW_FORM_GNU_ref_alt
:
1330 /* Returns true if the form is strx[1-4]. */
1333 is_strx_form (enum dwarf_form form
)
1335 return (form
== DW_FORM_strx
1336 || form
== DW_FORM_strx1
1337 || form
== DW_FORM_strx2
1338 || form
== DW_FORM_strx3
1339 || form
== DW_FORM_strx4
);
1342 /* Return true if the form is addrx[1-4]. */
1345 is_addrx_form (enum dwarf_form form
)
1347 return (form
== DW_FORM_addrx
1348 || form
== DW_FORM_addrx1
1349 || form
== DW_FORM_addrx2
1350 || form
== DW_FORM_addrx3
1351 || form
== DW_FORM_addrx4
);
1354 /* Returns the address in .debug_addr section using DW_AT_addr_base.
1355 Used to implement DW_FORM_addrx*. */
1357 read_indexed_address (uint64_t idx
, struct comp_unit
*unit
)
1359 struct dwarf2_debug
*stash
= unit
->stash
;
1360 struct dwarf2_debug_file
*file
= unit
->file
;
1367 if (!read_section (unit
->abfd
, &stash
->debug_sections
[debug_addr
],
1369 &file
->dwarf_addr_buffer
, &file
->dwarf_addr_size
))
1372 if (_bfd_mul_overflow (idx
, unit
->offset_size
, &offset
))
1375 offset
+= unit
->dwarf_addr_offset
;
1376 if (offset
< unit
->dwarf_addr_offset
1377 || offset
> file
->dwarf_addr_size
1378 || file
->dwarf_addr_size
- offset
< unit
->offset_size
)
1381 info_ptr
= file
->dwarf_addr_buffer
+ offset
;
1383 if (unit
->offset_size
== 4)
1384 return bfd_get_32 (unit
->abfd
, info_ptr
);
1385 else if (unit
->offset_size
== 8)
1386 return bfd_get_64 (unit
->abfd
, info_ptr
);
1391 /* Returns the string using DW_AT_str_offsets_base.
1392 Used to implement DW_FORM_strx*. */
1394 read_indexed_string (uint64_t idx
, struct comp_unit
*unit
)
1396 struct dwarf2_debug
*stash
= unit
->stash
;
1397 struct dwarf2_debug_file
*file
= unit
->file
;
1399 uint64_t str_offset
;
1405 if (!read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
1407 &file
->dwarf_str_buffer
, &file
->dwarf_str_size
))
1410 if (!read_section (unit
->abfd
, &stash
->debug_sections
[debug_str_offsets
],
1412 &file
->dwarf_str_offsets_buffer
,
1413 &file
->dwarf_str_offsets_size
))
1416 if (_bfd_mul_overflow (idx
, unit
->offset_size
, &offset
))
1419 offset
+= unit
->dwarf_str_offset
;
1420 if (offset
< unit
->dwarf_str_offset
1421 || offset
> file
->dwarf_str_offsets_size
1422 || file
->dwarf_str_offsets_size
- offset
< unit
->offset_size
)
1425 info_ptr
= file
->dwarf_str_offsets_buffer
+ offset
;
1427 if (unit
->offset_size
== 4)
1428 str_offset
= bfd_get_32 (unit
->abfd
, info_ptr
);
1429 else if (unit
->offset_size
== 8)
1430 str_offset
= bfd_get_64 (unit
->abfd
, info_ptr
);
1434 if (str_offset
>= file
->dwarf_str_size
)
1436 return (const char *) file
->dwarf_str_buffer
+ str_offset
;
1439 /* Read and fill in the value of attribute ATTR as described by FORM.
1440 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1441 Returns an updated INFO_PTR taking into account the amount of data read. */
1444 read_attribute_value (struct attribute
* attr
,
1446 bfd_vma implicit_const
,
1447 struct comp_unit
* unit
,
1448 bfd_byte
* info_ptr
,
1449 bfd_byte
* info_ptr_end
)
1451 bfd
*abfd
= unit
->abfd
;
1454 if (info_ptr
>= info_ptr_end
&& form
!= DW_FORM_flag_present
)
1456 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1457 bfd_set_error (bfd_error_bad_value
);
1461 attr
->form
= (enum dwarf_form
) form
;
1465 case DW_FORM_flag_present
:
1468 case DW_FORM_ref_addr
:
1469 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1471 if (unit
->version
>= 3)
1473 if (unit
->offset_size
== 4)
1474 attr
->u
.val
= read_4_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1476 attr
->u
.val
= read_8_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1481 attr
->u
.val
= read_address (unit
, &info_ptr
, info_ptr_end
);
1483 case DW_FORM_GNU_ref_alt
:
1484 case DW_FORM_sec_offset
:
1485 if (unit
->offset_size
== 4)
1486 attr
->u
.val
= read_4_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1488 attr
->u
.val
= read_8_bytes (unit
->abfd
, &info_ptr
, info_ptr_end
);
1490 case DW_FORM_block2
:
1491 amt
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1492 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1493 if (attr
->u
.blk
== NULL
)
1496 case DW_FORM_block4
:
1497 amt
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1498 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1499 if (attr
->u
.blk
== NULL
)
1505 attr
->u
.val
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1507 case DW_FORM_addrx1
:
1508 attr
->u
.val
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1509 /* dwarf_addr_offset value 0 indicates the attribute DW_AT_addr_base
1511 if (unit
->dwarf_addr_offset
!= 0)
1512 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
1516 attr
->u
.val
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1518 case DW_FORM_addrx2
:
1519 attr
->u
.val
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1520 if (unit
->dwarf_addr_offset
!= 0)
1521 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
1523 case DW_FORM_addrx3
:
1524 attr
->u
.val
= read_3_bytes (abfd
, &info_ptr
, info_ptr_end
);
1525 if (unit
->dwarf_addr_offset
!= 0)
1526 attr
->u
.val
= read_indexed_address(attr
->u
.val
, unit
);
1530 attr
->u
.val
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1532 case DW_FORM_addrx4
:
1533 attr
->u
.val
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1534 if (unit
->dwarf_addr_offset
!= 0)
1535 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
1539 case DW_FORM_ref_sig8
:
1540 attr
->u
.val
= read_8_bytes (abfd
, &info_ptr
, info_ptr_end
);
1542 case DW_FORM_string
:
1543 attr
->u
.str
= read_string (&info_ptr
, info_ptr_end
);
1546 attr
->u
.str
= read_indirect_string (unit
, &info_ptr
, info_ptr_end
);
1548 case DW_FORM_line_strp
:
1549 attr
->u
.str
= read_indirect_line_string (unit
, &info_ptr
, info_ptr_end
);
1551 case DW_FORM_GNU_strp_alt
:
1552 attr
->u
.str
= read_alt_indirect_string (unit
, &info_ptr
, info_ptr_end
);
1555 attr
->u
.val
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1556 /* dwarf_str_offset value 0 indicates the attribute DW_AT_str_offsets_base
1558 if (unit
->dwarf_str_offset
!= 0)
1559 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1564 attr
->u
.val
= read_2_bytes (abfd
, &info_ptr
, info_ptr_end
);
1565 if (unit
->dwarf_str_offset
!= 0)
1566 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1571 attr
->u
.val
= read_3_bytes (abfd
, &info_ptr
, info_ptr_end
);
1572 if (unit
->dwarf_str_offset
!= 0)
1573 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1578 attr
->u
.val
= read_4_bytes (abfd
, &info_ptr
, info_ptr_end
);
1579 if (unit
->dwarf_str_offset
!= 0)
1580 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1585 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1586 false, info_ptr_end
);
1587 if (unit
->dwarf_str_offset
!= 0)
1588 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
1592 case DW_FORM_exprloc
:
1594 amt
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1595 false, info_ptr_end
);
1596 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1597 if (attr
->u
.blk
== NULL
)
1600 case DW_FORM_block1
:
1601 amt
= read_1_byte (abfd
, &info_ptr
, info_ptr_end
);
1602 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, amt
);
1603 if (attr
->u
.blk
== NULL
)
1607 attr
->u
.sval
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1608 true, info_ptr_end
);
1610 case DW_FORM_ref_udata
:
1612 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1613 false, info_ptr_end
);
1616 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1617 false, info_ptr_end
);
1618 if (unit
->dwarf_addr_offset
!= 0)
1619 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
1621 case DW_FORM_indirect
:
1622 form
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1623 false, info_ptr_end
);
1624 if (form
== DW_FORM_implicit_const
)
1625 implicit_const
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
1626 true, info_ptr_end
);
1627 info_ptr
= read_attribute_value (attr
, form
, implicit_const
, unit
,
1628 info_ptr
, info_ptr_end
);
1630 case DW_FORM_implicit_const
:
1631 attr
->form
= DW_FORM_sdata
;
1632 attr
->u
.sval
= implicit_const
;
1634 case DW_FORM_data16
:
1635 /* This is really a "constant", but there is no way to store that
1636 so pretend it is a 16 byte block instead. */
1637 attr
->u
.blk
= read_blk (abfd
, &info_ptr
, info_ptr_end
, 16);
1638 if (attr
->u
.blk
== NULL
)
1643 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1645 bfd_set_error (bfd_error_bad_value
);
1651 /* Read an attribute described by an abbreviated attribute. */
1654 read_attribute (struct attribute
* attr
,
1655 struct attr_abbrev
* abbrev
,
1656 struct comp_unit
* unit
,
1657 bfd_byte
* info_ptr
,
1658 bfd_byte
* info_ptr_end
)
1660 attr
->name
= abbrev
->name
;
1661 info_ptr
= read_attribute_value (attr
, abbrev
->form
, abbrev
->implicit_const
,
1662 unit
, info_ptr
, info_ptr_end
);
1666 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1670 non_mangled (int lang
)
1680 case DW_LANG_Cobol74
:
1681 case DW_LANG_Cobol85
:
1682 case DW_LANG_Fortran77
:
1683 case DW_LANG_Pascal83
:
1689 case DW_LANG_Mips_Assembler
:
1694 /* Source line information table routines. */
1696 #define FILE_ALLOC_CHUNK 5
1697 #define DIR_ALLOC_CHUNK 5
1701 struct line_info
* prev_line
;
1705 unsigned int column
;
1706 unsigned int discriminator
;
1707 unsigned char op_index
;
1708 unsigned char end_sequence
; /* End of (sequential) code sequence. */
1719 struct line_sequence
1722 struct line_sequence
* prev_sequence
;
1723 struct line_info
* last_line
; /* Largest VMA. */
1724 struct line_info
** line_info_lookup
;
1725 bfd_size_type num_lines
;
1728 struct line_info_table
1731 unsigned int num_files
;
1732 unsigned int num_dirs
;
1733 unsigned int num_sequences
;
1736 struct fileinfo
* files
;
1737 struct line_sequence
* sequences
;
1738 struct line_info
* lcl_head
; /* Local head; used in 'add_line_info'. */
1741 /* Remember some information about each function. If the function is
1742 inlined (DW_TAG_inlined_subroutine) it may have two additional
1743 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1744 source code location where this function was inlined. */
1748 /* Pointer to previous function in list of all functions. */
1749 struct funcinfo
*prev_func
;
1750 /* Pointer to function one scope higher. */
1751 struct funcinfo
*caller_func
;
1752 /* Source location file name where caller_func inlines this func. */
1754 /* Source location file name. */
1756 /* Source location line number where caller_func inlines this func. */
1758 /* Source location line number. */
1763 struct arange arange
;
1764 /* Where the symbol is defined. */
1766 /* The offset of the funcinfo from the start of the unit. */
1767 uint64_t unit_offset
;
1770 struct lookup_funcinfo
1772 /* Function information corresponding to this lookup table entry. */
1773 struct funcinfo
*funcinfo
;
1775 /* The lowest address for this specific function. */
1778 /* The highest address of this function before the lookup table is sorted.
1779 The highest address of all prior functions after the lookup table is
1780 sorted, which is used for binary search. */
1782 /* Index of this function, used to ensure qsort is stable. */
1788 /* Pointer to previous variable in list of all variables. */
1789 struct varinfo
*prev_var
;
1790 /* The offset of the varinfo from the start of the unit. */
1791 uint64_t unit_offset
;
1792 /* Source location file name. */
1794 /* Source location line number. */
1796 /* The type of this variable. */
1798 /* The name of the variable, if it has one. */
1800 /* The address of the variable. */
1802 /* Where the symbol is defined. */
1804 /* Is this a stack variable? */
1808 /* Return TRUE if NEW_LINE should sort after LINE. */
1811 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
1813 return (new_line
->address
> line
->address
1814 || (new_line
->address
== line
->address
1815 && new_line
->op_index
> line
->op_index
));
1819 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1820 that the list is sorted. Note that the line_info list is sorted from
1821 highest to lowest VMA (with possible duplicates); that is,
1822 line_info->prev_line always accesses an equal or smaller VMA. */
1825 add_line_info (struct line_info_table
*table
,
1827 unsigned char op_index
,
1830 unsigned int column
,
1831 unsigned int discriminator
,
1834 size_t amt
= sizeof (struct line_info
);
1835 struct line_sequence
* seq
= table
->sequences
;
1836 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
1841 /* Set member data of 'info'. */
1842 info
->prev_line
= NULL
;
1843 info
->address
= address
;
1844 info
->op_index
= op_index
;
1846 info
->column
= column
;
1847 info
->discriminator
= discriminator
;
1848 info
->end_sequence
= end_sequence
;
1850 if (filename
&& filename
[0])
1852 info
->filename
= (char *) bfd_alloc (table
->abfd
, strlen (filename
) + 1);
1853 if (info
->filename
== NULL
)
1855 strcpy (info
->filename
, filename
);
1858 info
->filename
= NULL
;
1860 /* Find the correct location for 'info'. Normally we will receive
1861 new line_info data 1) in order and 2) with increasing VMAs.
1862 However some compilers break the rules (cf. decode_line_info) and
1863 so we include some heuristics for quickly finding the correct
1864 location for 'info'. In particular, these heuristics optimize for
1865 the common case in which the VMA sequence that we receive is a
1866 list of locally sorted VMAs such as
1867 p...z a...j (where a < j < p < z)
1869 Note: table->lcl_head is used to head an *actual* or *possible*
1870 sub-sequence within the list (such as a...j) that is not directly
1871 headed by table->last_line
1873 Note: we may receive duplicate entries from 'decode_line_info'. */
1876 && seq
->last_line
->address
== address
1877 && seq
->last_line
->op_index
== op_index
1878 && seq
->last_line
->end_sequence
== end_sequence
)
1880 /* We only keep the last entry with the same address and end
1881 sequence. See PR ld/4986. */
1882 if (table
->lcl_head
== seq
->last_line
)
1883 table
->lcl_head
= info
;
1884 info
->prev_line
= seq
->last_line
->prev_line
;
1885 seq
->last_line
= info
;
1887 else if (!seq
|| seq
->last_line
->end_sequence
)
1889 /* Start a new line sequence. */
1890 amt
= sizeof (struct line_sequence
);
1891 seq
= (struct line_sequence
*) bfd_malloc (amt
);
1894 seq
->low_pc
= address
;
1895 seq
->prev_sequence
= table
->sequences
;
1896 seq
->last_line
= info
;
1897 table
->lcl_head
= info
;
1898 table
->sequences
= seq
;
1899 table
->num_sequences
++;
1901 else if (info
->end_sequence
1902 || new_line_sorts_after (info
, seq
->last_line
))
1904 /* Normal case: add 'info' to the beginning of the current sequence. */
1905 info
->prev_line
= seq
->last_line
;
1906 seq
->last_line
= info
;
1908 /* lcl_head: initialize to head a *possible* sequence at the end. */
1909 if (!table
->lcl_head
)
1910 table
->lcl_head
= info
;
1912 else if (!new_line_sorts_after (info
, table
->lcl_head
)
1913 && (!table
->lcl_head
->prev_line
1914 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
1916 /* Abnormal but easy: lcl_head is the head of 'info'. */
1917 info
->prev_line
= table
->lcl_head
->prev_line
;
1918 table
->lcl_head
->prev_line
= info
;
1922 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1923 are valid heads for 'info'. Reset 'lcl_head'. */
1924 struct line_info
* li2
= seq
->last_line
; /* Always non-NULL. */
1925 struct line_info
* li1
= li2
->prev_line
;
1929 if (!new_line_sorts_after (info
, li2
)
1930 && new_line_sorts_after (info
, li1
))
1933 li2
= li1
; /* always non-NULL */
1934 li1
= li1
->prev_line
;
1936 table
->lcl_head
= li2
;
1937 info
->prev_line
= table
->lcl_head
->prev_line
;
1938 table
->lcl_head
->prev_line
= info
;
1939 if (address
< seq
->low_pc
)
1940 seq
->low_pc
= address
;
1945 /* Extract a fully qualified filename from a line info table.
1946 The returned string has been malloc'ed and it is the caller's
1947 responsibility to free it. */
1950 concat_filename (struct line_info_table
*table
, unsigned int file
)
1954 if (table
== NULL
|| file
- 1 >= table
->num_files
)
1956 /* FILE == 0 means unknown. */
1959 (_("DWARF error: mangled line number section (bad file number)"));
1960 return strdup ("<unknown>");
1963 filename
= table
->files
[file
- 1].name
;
1964 if (filename
== NULL
)
1965 return strdup ("<unknown>");
1967 if (!IS_ABSOLUTE_PATH (filename
))
1969 char *dir_name
= NULL
;
1970 char *subdir_name
= NULL
;
1974 if (table
->files
[file
- 1].dir
1975 /* PR 17512: file: 0317e960. */
1976 && table
->files
[file
- 1].dir
<= table
->num_dirs
1977 /* PR 17512: file: 7f3d2e4b. */
1978 && table
->dirs
!= NULL
)
1979 subdir_name
= table
->dirs
[table
->files
[file
- 1].dir
- 1];
1981 if (!subdir_name
|| !IS_ABSOLUTE_PATH (subdir_name
))
1982 dir_name
= table
->comp_dir
;
1986 dir_name
= subdir_name
;
1991 return strdup (filename
);
1993 len
= strlen (dir_name
) + strlen (filename
) + 2;
1997 len
+= strlen (subdir_name
) + 1;
1998 name
= (char *) bfd_malloc (len
);
2000 sprintf (name
, "%s/%s/%s", dir_name
, subdir_name
, filename
);
2004 name
= (char *) bfd_malloc (len
);
2006 sprintf (name
, "%s/%s", dir_name
, filename
);
2012 return strdup (filename
);
2015 /* Number of bits in a bfd_vma. */
2016 #define VMA_BITS (8 * sizeof (bfd_vma))
2018 /* Check whether [low1, high1) can be combined with [low2, high2),
2019 i.e., they touch or overlap. */
2020 static bool ranges_overlap (bfd_vma low1
,
2025 if (low1
== low2
|| high1
== high2
)
2028 /* Sort so that low1 is below low2. */
2042 /* We touch iff low2 == high1.
2043 We overlap iff low2 is within [low1, high1). */
2044 return low2
<= high1
;
2047 /* Insert an address range in the trie mapping addresses to compilation units.
2048 Will return the new trie node (usually the same as is being sent in, but
2049 in case of a leaf-to-interior conversion, or expansion of a leaf, it may be
2050 different), or NULL on failure.
2052 static struct trie_node
*insert_arange_in_trie(bfd
*abfd
,
2053 struct trie_node
*trie
,
2055 unsigned int trie_pc_bits
,
2056 struct comp_unit
*unit
,
2060 bfd_vma clamped_low_pc
, clamped_high_pc
;
2061 int ch
, from_ch
, to_ch
;
2062 bool is_full_leaf
= false;
2064 /* See if we can extend any of the existing ranges. This merging
2065 isn't perfect (if merging opens up the possibility of merging two existing
2066 ranges, we won't find them), but it takes the majority of the cases. */
2067 if (trie
->num_room_in_leaf
> 0)
2069 struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
2072 for (i
= 0; i
< leaf
->num_stored_in_leaf
; ++i
)
2074 if (leaf
->ranges
[i
].unit
== unit
2075 && ranges_overlap (low_pc
, high_pc
,
2076 leaf
->ranges
[i
].low_pc
,
2077 leaf
->ranges
[i
].high_pc
))
2079 if (low_pc
< leaf
->ranges
[i
].low_pc
)
2080 leaf
->ranges
[i
].low_pc
= low_pc
;
2081 if (high_pc
> leaf
->ranges
[i
].high_pc
)
2082 leaf
->ranges
[i
].high_pc
= high_pc
;
2087 is_full_leaf
= leaf
->num_stored_in_leaf
== trie
->num_room_in_leaf
;
2090 /* If we're a leaf with no more room and we're _not_ at the bottom,
2091 convert to an interior node. */
2092 if (is_full_leaf
&& trie_pc_bits
< VMA_BITS
)
2094 const struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
2097 trie
= bfd_zalloc (abfd
, sizeof (struct trie_interior
));
2100 is_full_leaf
= false;
2102 /* TODO: If we wanted to save a little more memory at the cost of
2103 complexity, we could have reused the old leaf node as one of the
2104 children of the new interior node, instead of throwing it away. */
2105 for (i
= 0; i
< leaf
->num_stored_in_leaf
; ++i
)
2107 if (!insert_arange_in_trie (abfd
, trie
, trie_pc
, trie_pc_bits
,
2108 leaf
->ranges
[i
].unit
, leaf
->ranges
[i
].low_pc
,
2109 leaf
->ranges
[i
].high_pc
))
2114 /* If we're a leaf with no more room and we _are_ at the bottom,
2115 we have no choice but to just make it larger. */
2118 const struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
2119 unsigned int new_room_in_leaf
= trie
->num_room_in_leaf
* 2;
2120 struct trie_leaf
*new_leaf
;
2121 size_t amt
= (sizeof (struct trie_leaf
)
2122 + ((new_room_in_leaf
- TRIE_LEAF_SIZE
)
2123 * sizeof (leaf
->ranges
[0])));
2124 new_leaf
= bfd_zalloc (abfd
, amt
);
2125 new_leaf
->head
.num_room_in_leaf
= new_room_in_leaf
;
2126 new_leaf
->num_stored_in_leaf
= leaf
->num_stored_in_leaf
;
2128 memcpy (new_leaf
->ranges
,
2130 leaf
->num_stored_in_leaf
* sizeof (leaf
->ranges
[0]));
2131 trie
= &new_leaf
->head
;
2132 is_full_leaf
= false;
2134 /* Now the insert below will go through. */
2137 /* If we're a leaf (now with room), we can just insert at the end. */
2138 if (trie
->num_room_in_leaf
> 0)
2140 struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
2142 unsigned int i
= leaf
->num_stored_in_leaf
++;
2143 leaf
->ranges
[i
].unit
= unit
;
2144 leaf
->ranges
[i
].low_pc
= low_pc
;
2145 leaf
->ranges
[i
].high_pc
= high_pc
;
2149 /* Now we are definitely an interior node, so recurse into all
2150 the relevant buckets. */
2152 /* Clamp the range to the current trie bucket. */
2153 clamped_low_pc
= low_pc
;
2154 clamped_high_pc
= high_pc
;
2155 if (trie_pc_bits
> 0)
2157 bfd_vma bucket_high_pc
=
2158 trie_pc
+ ((bfd_vma
) -1 >> trie_pc_bits
); /* Inclusive. */
2159 if (clamped_low_pc
< trie_pc
)
2160 clamped_low_pc
= trie_pc
;
2161 if (clamped_high_pc
> bucket_high_pc
)
2162 clamped_high_pc
= bucket_high_pc
;
2165 /* Insert the ranges in all buckets that it spans. */
2166 from_ch
= (clamped_low_pc
>> (VMA_BITS
- trie_pc_bits
- 8)) & 0xff;
2167 to_ch
= ((clamped_high_pc
- 1) >> (VMA_BITS
- trie_pc_bits
- 8)) & 0xff;
2168 for (ch
= from_ch
; ch
<= to_ch
; ++ch
)
2170 struct trie_interior
*interior
= (struct trie_interior
*) trie
;
2171 struct trie_node
*child
= interior
->children
[ch
];
2175 child
= alloc_trie_leaf (abfd
);
2179 bfd_vma bucket
= (bfd_vma
) ch
<< (VMA_BITS
- trie_pc_bits
- 8);
2180 child
= insert_arange_in_trie (abfd
,
2190 interior
->children
[ch
] = child
;
2198 arange_add (struct comp_unit
*unit
, struct arange
*first_arange
,
2199 struct trie_node
**trie_root
, bfd_vma low_pc
, bfd_vma high_pc
)
2201 struct arange
*arange
;
2203 /* Ignore empty ranges. */
2204 if (low_pc
== high_pc
)
2207 if (trie_root
!= NULL
)
2209 *trie_root
= insert_arange_in_trie (unit
->file
->bfd_ptr
,
2216 if (*trie_root
== NULL
)
2220 /* If the first arange is empty, use it. */
2221 if (first_arange
->high
== 0)
2223 first_arange
->low
= low_pc
;
2224 first_arange
->high
= high_pc
;
2228 /* Next see if we can cheaply extend an existing range. */
2229 arange
= first_arange
;
2232 if (low_pc
== arange
->high
)
2234 arange
->high
= high_pc
;
2237 if (high_pc
== arange
->low
)
2239 arange
->low
= low_pc
;
2242 arange
= arange
->next
;
2246 /* Need to allocate a new arange and insert it into the arange list.
2247 Order isn't significant, so just insert after the first arange. */
2248 arange
= (struct arange
*) bfd_alloc (unit
->abfd
, sizeof (*arange
));
2251 arange
->low
= low_pc
;
2252 arange
->high
= high_pc
;
2253 arange
->next
= first_arange
->next
;
2254 first_arange
->next
= arange
;
2258 /* Compare function for line sequences. */
2261 compare_sequences (const void* a
, const void* b
)
2263 const struct line_sequence
* seq1
= a
;
2264 const struct line_sequence
* seq2
= b
;
2266 /* Sort by low_pc as the primary key. */
2267 if (seq1
->low_pc
< seq2
->low_pc
)
2269 if (seq1
->low_pc
> seq2
->low_pc
)
2272 /* If low_pc values are equal, sort in reverse order of
2273 high_pc, so that the largest region comes first. */
2274 if (seq1
->last_line
->address
< seq2
->last_line
->address
)
2276 if (seq1
->last_line
->address
> seq2
->last_line
->address
)
2279 if (seq1
->last_line
->op_index
< seq2
->last_line
->op_index
)
2281 if (seq1
->last_line
->op_index
> seq2
->last_line
->op_index
)
2284 /* num_lines is initially an index, to make the sort stable. */
2285 if (seq1
->num_lines
< seq2
->num_lines
)
2287 if (seq1
->num_lines
> seq2
->num_lines
)
2292 /* Construct the line information table for quick lookup. */
2295 build_line_info_table (struct line_info_table
* table
,
2296 struct line_sequence
* seq
)
2299 struct line_info
**line_info_lookup
;
2300 struct line_info
*each_line
;
2301 unsigned int num_lines
;
2302 unsigned int line_index
;
2304 if (seq
->line_info_lookup
!= NULL
)
2307 /* Count the number of line information entries. We could do this while
2308 scanning the debug information, but some entries may be added via
2309 lcl_head without having a sequence handy to increment the number of
2312 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
2315 seq
->num_lines
= num_lines
;
2319 /* Allocate space for the line information lookup table. */
2320 amt
= sizeof (struct line_info
*) * num_lines
;
2321 line_info_lookup
= (struct line_info
**) bfd_alloc (table
->abfd
, amt
);
2322 seq
->line_info_lookup
= line_info_lookup
;
2323 if (line_info_lookup
== NULL
)
2326 /* Create the line information lookup table. */
2327 line_index
= num_lines
;
2328 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
2329 line_info_lookup
[--line_index
] = each_line
;
2331 BFD_ASSERT (line_index
== 0);
2335 /* Sort the line sequences for quick lookup. */
2338 sort_line_sequences (struct line_info_table
* table
)
2341 struct line_sequence
*sequences
;
2342 struct line_sequence
*seq
;
2344 unsigned int num_sequences
= table
->num_sequences
;
2345 bfd_vma last_high_pc
;
2347 if (num_sequences
== 0)
2350 /* Allocate space for an array of sequences. */
2351 amt
= sizeof (struct line_sequence
) * num_sequences
;
2352 sequences
= (struct line_sequence
*) bfd_alloc (table
->abfd
, amt
);
2353 if (sequences
== NULL
)
2356 /* Copy the linked list into the array, freeing the original nodes. */
2357 seq
= table
->sequences
;
2358 for (n
= 0; n
< num_sequences
; n
++)
2360 struct line_sequence
* last_seq
= seq
;
2363 sequences
[n
].low_pc
= seq
->low_pc
;
2364 sequences
[n
].prev_sequence
= NULL
;
2365 sequences
[n
].last_line
= seq
->last_line
;
2366 sequences
[n
].line_info_lookup
= NULL
;
2367 sequences
[n
].num_lines
= n
;
2368 seq
= seq
->prev_sequence
;
2371 BFD_ASSERT (seq
== NULL
);
2373 qsort (sequences
, n
, sizeof (struct line_sequence
), compare_sequences
);
2375 /* Make the list binary-searchable by trimming overlapping entries
2376 and removing nested entries. */
2378 last_high_pc
= sequences
[0].last_line
->address
;
2379 for (n
= 1; n
< table
->num_sequences
; n
++)
2381 if (sequences
[n
].low_pc
< last_high_pc
)
2383 if (sequences
[n
].last_line
->address
<= last_high_pc
)
2384 /* Skip nested entries. */
2387 /* Trim overlapping entries. */
2388 sequences
[n
].low_pc
= last_high_pc
;
2390 last_high_pc
= sequences
[n
].last_line
->address
;
2391 if (n
> num_sequences
)
2393 /* Close up the gap. */
2394 sequences
[num_sequences
].low_pc
= sequences
[n
].low_pc
;
2395 sequences
[num_sequences
].last_line
= sequences
[n
].last_line
;
2400 table
->sequences
= sequences
;
2401 table
->num_sequences
= num_sequences
;
2405 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
2408 line_info_add_include_dir (struct line_info_table
*table
, char *cur_dir
)
2410 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
2415 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
2416 amt
*= sizeof (char *);
2418 tmp
= (char **) bfd_realloc (table
->dirs
, amt
);
2424 table
->dirs
[table
->num_dirs
++] = cur_dir
;
2429 line_info_add_include_dir_stub (struct line_info_table
*table
, char *cur_dir
,
2430 unsigned int dir ATTRIBUTE_UNUSED
,
2431 unsigned int xtime ATTRIBUTE_UNUSED
,
2432 unsigned int size ATTRIBUTE_UNUSED
)
2434 return line_info_add_include_dir (table
, cur_dir
);
2437 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
2440 line_info_add_file_name (struct line_info_table
*table
, char *cur_file
,
2441 unsigned int dir
, unsigned int xtime
,
2444 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
2446 struct fileinfo
*tmp
;
2449 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
2450 amt
*= sizeof (struct fileinfo
);
2452 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
2458 table
->files
[table
->num_files
].name
= cur_file
;
2459 table
->files
[table
->num_files
].dir
= dir
;
2460 table
->files
[table
->num_files
].time
= xtime
;
2461 table
->files
[table
->num_files
].size
= size
;
2466 /* Read directory or file name entry format, starting with byte of
2467 format count entries, ULEB128 pairs of entry formats, ULEB128 of
2468 entries count and the entries themselves in the described entry
2472 read_formatted_entries (struct comp_unit
*unit
, bfd_byte
**bufp
,
2473 bfd_byte
*buf_end
, struct line_info_table
*table
,
2474 bool (*callback
) (struct line_info_table
*table
,
2480 bfd
*abfd
= unit
->abfd
;
2481 bfd_byte format_count
, formati
;
2482 bfd_vma data_count
, datai
;
2483 bfd_byte
*buf
= *bufp
;
2484 bfd_byte
*format_header_data
;
2486 format_count
= read_1_byte (abfd
, &buf
, buf_end
);
2487 format_header_data
= buf
;
2488 for (formati
= 0; formati
< format_count
; formati
++)
2490 _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2491 _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2494 data_count
= _bfd_safe_read_leb128 (abfd
, &buf
, false, buf_end
);
2495 if (format_count
== 0 && data_count
!= 0)
2497 _bfd_error_handler (_("DWARF error: zero format count"));
2498 bfd_set_error (bfd_error_bad_value
);
2502 /* PR 22210. Paranoia check. Don't bother running the loop
2503 if we know that we are going to run out of buffer. */
2504 if (data_count
> (bfd_vma
) (buf_end
- buf
))
2507 (_("DWARF error: data count (%" PRIx64
") larger than buffer size"),
2508 (uint64_t) data_count
);
2509 bfd_set_error (bfd_error_bad_value
);
2513 for (datai
= 0; datai
< data_count
; datai
++)
2515 bfd_byte
*format
= format_header_data
;
2518 memset (&fe
, 0, sizeof fe
);
2519 for (formati
= 0; formati
< format_count
; formati
++)
2521 bfd_vma content_type
, form
;
2523 char **stringp
= &string_trash
;
2524 unsigned int uint_trash
, *uintp
= &uint_trash
;
2525 struct attribute attr
;
2527 content_type
= _bfd_safe_read_leb128 (abfd
, &format
, false, buf_end
);
2528 switch (content_type
)
2533 case DW_LNCT_directory_index
:
2536 case DW_LNCT_timestamp
:
2546 (_("DWARF error: unknown format content type %" PRIu64
),
2547 (uint64_t) content_type
);
2548 bfd_set_error (bfd_error_bad_value
);
2552 form
= _bfd_safe_read_leb128 (abfd
, &format
, false, buf_end
);
2553 buf
= read_attribute_value (&attr
, form
, 0, unit
, buf
, buf_end
);
2558 case DW_FORM_string
:
2559 case DW_FORM_line_strp
:
2565 *stringp
= attr
.u
.str
;
2573 *uintp
= attr
.u
.val
;
2576 case DW_FORM_data16
:
2577 /* MD5 data is in the attr.blk, but we are ignoring those. */
2582 /* Skip the first "zero entry", which is the compilation dir/file. */
2584 if (!callback (table
, fe
.name
, fe
.dir
, fe
.time
, fe
.size
))
2592 /* Decode the line number information for UNIT. */
2594 static struct line_info_table
*
2595 decode_line_info (struct comp_unit
*unit
)
2597 bfd
*abfd
= unit
->abfd
;
2598 struct dwarf2_debug
*stash
= unit
->stash
;
2599 struct dwarf2_debug_file
*file
= unit
->file
;
2600 struct line_info_table
* table
;
2603 struct line_head lh
;
2604 unsigned int i
, offset_size
;
2605 char *cur_file
, *cur_dir
;
2606 unsigned char op_code
, extended_op
, adj_opcode
;
2607 unsigned int exop_len
;
2610 if (unit
->line_offset
== 0 && file
->line_table
)
2611 return file
->line_table
;
2613 if (! read_section (abfd
, &stash
->debug_sections
[debug_line
],
2614 file
->syms
, unit
->line_offset
,
2615 &file
->dwarf_line_buffer
, &file
->dwarf_line_size
))
2618 if (file
->dwarf_line_size
< 16)
2621 (_("DWARF error: line info section is too small (%" PRId64
")"),
2622 (int64_t) file
->dwarf_line_size
);
2623 bfd_set_error (bfd_error_bad_value
);
2626 line_ptr
= file
->dwarf_line_buffer
+ unit
->line_offset
;
2627 line_end
= file
->dwarf_line_buffer
+ file
->dwarf_line_size
;
2629 /* Read in the prologue. */
2630 lh
.total_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2632 if (lh
.total_length
== 0xffffffff)
2634 lh
.total_length
= read_8_bytes (abfd
, &line_ptr
, line_end
);
2637 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
2639 /* Handle (non-standard) 64-bit DWARF2 formats. */
2640 lh
.total_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2644 if (lh
.total_length
> (size_t) (line_end
- line_ptr
))
2647 /* xgettext: c-format */
2648 (_("DWARF error: line info data is bigger (%#" PRIx64
")"
2649 " than the space remaining in the section (%#lx)"),
2650 (uint64_t) lh
.total_length
, (unsigned long) (line_end
- line_ptr
));
2651 bfd_set_error (bfd_error_bad_value
);
2655 line_end
= line_ptr
+ lh
.total_length
;
2657 lh
.version
= read_2_bytes (abfd
, &line_ptr
, line_end
);
2658 if (lh
.version
< 2 || lh
.version
> 5)
2661 (_("DWARF error: unhandled .debug_line version %d"), lh
.version
);
2662 bfd_set_error (bfd_error_bad_value
);
2666 if (line_ptr
+ offset_size
+ (lh
.version
>= 5 ? 8 : (lh
.version
>= 4 ? 6 : 5))
2670 (_("DWARF error: ran out of room reading prologue"));
2671 bfd_set_error (bfd_error_bad_value
);
2675 if (lh
.version
>= 5)
2677 unsigned int segment_selector_size
;
2679 /* Skip address size. */
2680 read_1_byte (abfd
, &line_ptr
, line_end
);
2682 segment_selector_size
= read_1_byte (abfd
, &line_ptr
, line_end
);
2683 if (segment_selector_size
!= 0)
2686 (_("DWARF error: line info unsupported segment selector size %u"),
2687 segment_selector_size
);
2688 bfd_set_error (bfd_error_bad_value
);
2693 if (offset_size
== 4)
2694 lh
.prologue_length
= read_4_bytes (abfd
, &line_ptr
, line_end
);
2696 lh
.prologue_length
= read_8_bytes (abfd
, &line_ptr
, line_end
);
2698 lh
.minimum_instruction_length
= read_1_byte (abfd
, &line_ptr
, line_end
);
2700 if (lh
.version
>= 4)
2701 lh
.maximum_ops_per_insn
= read_1_byte (abfd
, &line_ptr
, line_end
);
2703 lh
.maximum_ops_per_insn
= 1;
2705 if (lh
.maximum_ops_per_insn
== 0)
2708 (_("DWARF error: invalid maximum operations per instruction"));
2709 bfd_set_error (bfd_error_bad_value
);
2713 lh
.default_is_stmt
= read_1_byte (abfd
, &line_ptr
, line_end
);
2714 lh
.line_base
= read_1_signed_byte (abfd
, &line_ptr
, line_end
);
2715 lh
.line_range
= read_1_byte (abfd
, &line_ptr
, line_end
);
2716 lh
.opcode_base
= read_1_byte (abfd
, &line_ptr
, line_end
);
2718 if (line_ptr
+ (lh
.opcode_base
- 1) >= line_end
)
2720 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2721 bfd_set_error (bfd_error_bad_value
);
2725 amt
= lh
.opcode_base
* sizeof (unsigned char);
2726 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
2728 lh
.standard_opcode_lengths
[0] = 1;
2730 for (i
= 1; i
< lh
.opcode_base
; ++i
)
2731 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, &line_ptr
, line_end
);
2733 amt
= sizeof (struct line_info_table
);
2734 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
2738 table
->comp_dir
= unit
->comp_dir
;
2740 table
->num_files
= 0;
2741 table
->files
= NULL
;
2743 table
->num_dirs
= 0;
2746 table
->num_sequences
= 0;
2747 table
->sequences
= NULL
;
2749 table
->lcl_head
= NULL
;
2751 if (lh
.version
>= 5)
2753 /* Read directory table. */
2754 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2755 line_info_add_include_dir_stub
))
2758 /* Read file name table. */
2759 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2760 line_info_add_file_name
))
2765 /* Read directory table. */
2766 while ((cur_dir
= read_string (&line_ptr
, line_end
)) != NULL
)
2768 if (!line_info_add_include_dir (table
, cur_dir
))
2772 /* Read file name table. */
2773 while ((cur_file
= read_string (&line_ptr
, line_end
)) != NULL
)
2775 unsigned int dir
, xtime
, size
;
2777 dir
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2778 xtime
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2779 size
= _bfd_safe_read_leb128 (abfd
, &line_ptr
, false, line_end
);
2781 if (!line_info_add_file_name (table
, cur_file
, dir
, xtime
, size
))
2786 /* Read the statement sequences until there's nothing left. */
2787 while (line_ptr
< line_end
)
2789 /* State machine registers. */
2790 bfd_vma address
= 0;
2791 unsigned char op_index
= 0;
2792 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
2793 unsigned int line
= 1;
2794 unsigned int column
= 0;
2795 unsigned int discriminator
= 0;
2796 int is_stmt
= lh
.default_is_stmt
;
2797 int end_sequence
= 0;
2798 unsigned int dir
, xtime
, size
;
2799 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2800 compilers generate address sequences that are wildly out of
2801 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2802 for ia64-Linux). Thus, to determine the low and high
2803 address, we must compare on every DW_LNS_copy, etc. */
2804 bfd_vma low_pc
= (bfd_vma
) -1;
2805 bfd_vma high_pc
= 0;
2807 /* Decode the table. */
2808 while (!end_sequence
&& line_ptr
< line_end
)
2810 op_code
= read_1_byte (abfd
, &line_ptr
, line_end
);
2812 if (op_code
>= lh
.opcode_base
)
2814 /* Special operand. */
2815 adj_opcode
= op_code
- lh
.opcode_base
;
2816 if (lh
.line_range
== 0)
2818 if (lh
.maximum_ops_per_insn
== 1)
2819 address
+= (adj_opcode
/ lh
.line_range
2820 * lh
.minimum_instruction_length
);
2823 address
+= ((op_index
+ adj_opcode
/ lh
.line_range
)
2824 / lh
.maximum_ops_per_insn
2825 * lh
.minimum_instruction_length
);
2826 op_index
= ((op_index
+ adj_opcode
/ lh
.line_range
)
2827 % lh
.maximum_ops_per_insn
);
2829 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
2830 /* Append row to matrix using current values. */
2831 if (!add_line_info (table
, address
, op_index
, filename
,
2832 line
, column
, discriminator
, 0))
2835 if (address
< low_pc
)
2837 if (address
> high_pc
)
2840 else switch (op_code
)
2842 case DW_LNS_extended_op
:
2843 exop_len
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2845 extended_op
= read_1_byte (abfd
, &line_ptr
, line_end
);
2847 switch (extended_op
)
2849 case DW_LNE_end_sequence
:
2851 if (!add_line_info (table
, address
, op_index
, filename
, line
,
2852 column
, discriminator
, end_sequence
))
2855 if (address
< low_pc
)
2857 if (address
> high_pc
)
2859 if (!arange_add (unit
, &unit
->arange
, &unit
->file
->trie_root
,
2863 case DW_LNE_set_address
:
2864 address
= read_address (unit
, &line_ptr
, line_end
);
2867 case DW_LNE_define_file
:
2868 cur_file
= read_string (&line_ptr
, line_end
);
2869 dir
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2871 xtime
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2873 size
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2875 if (!line_info_add_file_name (table
, cur_file
, dir
,
2879 case DW_LNE_set_discriminator
:
2880 discriminator
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2883 case DW_LNE_HP_source_file_correlation
:
2884 line_ptr
+= exop_len
- 1;
2888 (_("DWARF error: mangled line number section"));
2889 bfd_set_error (bfd_error_bad_value
);
2896 if (!add_line_info (table
, address
, op_index
,
2897 filename
, line
, column
, discriminator
, 0))
2900 if (address
< low_pc
)
2902 if (address
> high_pc
)
2905 case DW_LNS_advance_pc
:
2906 if (lh
.maximum_ops_per_insn
== 1)
2907 address
+= (lh
.minimum_instruction_length
2908 * _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2912 bfd_vma adjust
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2914 address
= ((op_index
+ adjust
) / lh
.maximum_ops_per_insn
2915 * lh
.minimum_instruction_length
);
2916 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2919 case DW_LNS_advance_line
:
2920 line
+= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2923 case DW_LNS_set_file
:
2925 unsigned int filenum
;
2927 /* The file and directory tables are 0
2928 based, the references are 1 based. */
2929 filenum
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2932 filename
= concat_filename (table
, filenum
);
2935 case DW_LNS_set_column
:
2936 column
= _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2939 case DW_LNS_negate_stmt
:
2940 is_stmt
= (!is_stmt
);
2942 case DW_LNS_set_basic_block
:
2944 case DW_LNS_const_add_pc
:
2945 if (lh
.line_range
== 0)
2947 if (lh
.maximum_ops_per_insn
== 1)
2948 address
+= (lh
.minimum_instruction_length
2949 * ((255 - lh
.opcode_base
) / lh
.line_range
));
2952 bfd_vma adjust
= ((255 - lh
.opcode_base
) / lh
.line_range
);
2953 address
+= (lh
.minimum_instruction_length
2954 * ((op_index
+ adjust
)
2955 / lh
.maximum_ops_per_insn
));
2956 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2959 case DW_LNS_fixed_advance_pc
:
2960 address
+= read_2_bytes (abfd
, &line_ptr
, line_end
);
2964 /* Unknown standard opcode, ignore it. */
2965 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
2966 (void) _bfd_safe_read_leb128 (abfd
, &line_ptr
,
2975 if (unit
->line_offset
== 0)
2976 file
->line_table
= table
;
2977 if (sort_line_sequences (table
))
2981 while (table
->sequences
!= NULL
)
2983 struct line_sequence
* seq
= table
->sequences
;
2984 table
->sequences
= table
->sequences
->prev_sequence
;
2987 free (table
->files
);
2992 /* If ADDR is within TABLE set the output parameters and return TRUE,
2993 otherwise set *FILENAME_PTR to NULL and return FALSE.
2994 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2995 are pointers to the objects to be filled in. */
2998 lookup_address_in_line_info_table (struct line_info_table
*table
,
3000 const char **filename_ptr
,
3001 unsigned int *linenumber_ptr
,
3002 unsigned int *discriminator_ptr
)
3004 struct line_sequence
*seq
= NULL
;
3005 struct line_info
*info
;
3008 /* Binary search the array of sequences. */
3010 high
= table
->num_sequences
;
3013 mid
= (low
+ high
) / 2;
3014 seq
= &table
->sequences
[mid
];
3015 if (addr
< seq
->low_pc
)
3017 else if (addr
>= seq
->last_line
->address
)
3023 /* Check for a valid sequence. */
3024 if (!seq
|| addr
< seq
->low_pc
|| addr
>= seq
->last_line
->address
)
3027 if (!build_line_info_table (table
, seq
))
3030 /* Binary search the array of line information. */
3032 high
= seq
->num_lines
;
3036 mid
= (low
+ high
) / 2;
3037 info
= seq
->line_info_lookup
[mid
];
3038 if (addr
< info
->address
)
3040 else if (addr
>= seq
->line_info_lookup
[mid
+ 1]->address
)
3046 /* Check for a valid line information entry. */
3048 && addr
>= info
->address
3049 && addr
< seq
->line_info_lookup
[mid
+ 1]->address
3050 && !(info
->end_sequence
|| info
== seq
->last_line
))
3052 *filename_ptr
= info
->filename
;
3053 *linenumber_ptr
= info
->line
;
3054 if (discriminator_ptr
)
3055 *discriminator_ptr
= info
->discriminator
;
3060 *filename_ptr
= NULL
;
3064 /* Read in the .debug_ranges section for future reference. */
3067 read_debug_ranges (struct comp_unit
* unit
)
3069 struct dwarf2_debug
*stash
= unit
->stash
;
3070 struct dwarf2_debug_file
*file
= unit
->file
;
3072 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_ranges
],
3074 &file
->dwarf_ranges_buffer
, &file
->dwarf_ranges_size
);
3077 /* Read in the .debug_rnglists section for future reference. */
3080 read_debug_rnglists (struct comp_unit
* unit
)
3082 struct dwarf2_debug
*stash
= unit
->stash
;
3083 struct dwarf2_debug_file
*file
= unit
->file
;
3085 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_rnglists
],
3087 &file
->dwarf_rnglists_buffer
, &file
->dwarf_rnglists_size
);
3090 /* Function table functions. */
3093 compare_lookup_funcinfos (const void * a
, const void * b
)
3095 const struct lookup_funcinfo
* lookup1
= a
;
3096 const struct lookup_funcinfo
* lookup2
= b
;
3098 if (lookup1
->low_addr
< lookup2
->low_addr
)
3100 if (lookup1
->low_addr
> lookup2
->low_addr
)
3102 if (lookup1
->high_addr
< lookup2
->high_addr
)
3104 if (lookup1
->high_addr
> lookup2
->high_addr
)
3107 if (lookup1
->idx
< lookup2
->idx
)
3109 if (lookup1
->idx
> lookup2
->idx
)
3115 build_lookup_funcinfo_table (struct comp_unit
* unit
)
3117 struct lookup_funcinfo
*lookup_funcinfo_table
= unit
->lookup_funcinfo_table
;
3118 unsigned int number_of_functions
= unit
->number_of_functions
;
3119 struct funcinfo
*each
;
3120 struct lookup_funcinfo
*entry
;
3122 struct arange
*range
;
3123 bfd_vma low_addr
, high_addr
;
3125 if (lookup_funcinfo_table
|| number_of_functions
== 0)
3128 /* Create the function info lookup table. */
3129 lookup_funcinfo_table
= (struct lookup_funcinfo
*)
3130 bfd_malloc (number_of_functions
* sizeof (struct lookup_funcinfo
));
3131 if (lookup_funcinfo_table
== NULL
)
3134 /* Populate the function info lookup table. */
3135 func_index
= number_of_functions
;
3136 for (each
= unit
->function_table
; each
; each
= each
->prev_func
)
3138 entry
= &lookup_funcinfo_table
[--func_index
];
3139 entry
->funcinfo
= each
;
3140 entry
->idx
= func_index
;
3142 /* Calculate the lowest and highest address for this function entry. */
3143 low_addr
= entry
->funcinfo
->arange
.low
;
3144 high_addr
= entry
->funcinfo
->arange
.high
;
3146 for (range
= entry
->funcinfo
->arange
.next
; range
; range
= range
->next
)
3148 if (range
->low
< low_addr
)
3149 low_addr
= range
->low
;
3150 if (range
->high
> high_addr
)
3151 high_addr
= range
->high
;
3154 entry
->low_addr
= low_addr
;
3155 entry
->high_addr
= high_addr
;
3158 BFD_ASSERT (func_index
== 0);
3160 /* Sort the function by address. */
3161 qsort (lookup_funcinfo_table
,
3162 number_of_functions
,
3163 sizeof (struct lookup_funcinfo
),
3164 compare_lookup_funcinfos
);
3166 /* Calculate the high watermark for each function in the lookup table. */
3167 high_addr
= lookup_funcinfo_table
[0].high_addr
;
3168 for (func_index
= 1; func_index
< number_of_functions
; func_index
++)
3170 entry
= &lookup_funcinfo_table
[func_index
];
3171 if (entry
->high_addr
> high_addr
)
3172 high_addr
= entry
->high_addr
;
3174 entry
->high_addr
= high_addr
;
3177 unit
->lookup_funcinfo_table
= lookup_funcinfo_table
;
3181 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
3182 TRUE. Note that we need to find the function that has the smallest range
3183 that contains ADDR, to handle inlined functions without depending upon
3184 them being ordered in TABLE by increasing range. */
3187 lookup_address_in_function_table (struct comp_unit
*unit
,
3189 struct funcinfo
**function_ptr
)
3191 unsigned int number_of_functions
= unit
->number_of_functions
;
3192 struct lookup_funcinfo
* lookup_funcinfo
= NULL
;
3193 struct funcinfo
* funcinfo
= NULL
;
3194 struct funcinfo
* best_fit
= NULL
;
3195 bfd_vma best_fit_len
= 0;
3196 bfd_size_type low
, high
, mid
, first
;
3197 struct arange
*arange
;
3199 if (number_of_functions
== 0)
3202 if (!build_lookup_funcinfo_table (unit
))
3205 if (unit
->lookup_funcinfo_table
[number_of_functions
- 1].high_addr
< addr
)
3208 /* Find the first function in the lookup table which may contain the
3209 specified address. */
3211 high
= number_of_functions
;
3215 mid
= (low
+ high
) / 2;
3216 lookup_funcinfo
= &unit
->lookup_funcinfo_table
[mid
];
3217 if (addr
< lookup_funcinfo
->low_addr
)
3219 else if (addr
>= lookup_funcinfo
->high_addr
)
3225 /* Find the 'best' match for the address. The prior algorithm defined the
3226 best match as the function with the smallest address range containing
3227 the specified address. This definition should probably be changed to the
3228 innermost inline routine containing the address, but right now we want
3229 to get the same results we did before. */
3230 while (first
< number_of_functions
)
3232 if (addr
< unit
->lookup_funcinfo_table
[first
].low_addr
)
3234 funcinfo
= unit
->lookup_funcinfo_table
[first
].funcinfo
;
3236 for (arange
= &funcinfo
->arange
; arange
; arange
= arange
->next
)
3238 if (addr
< arange
->low
|| addr
>= arange
->high
)
3242 || arange
->high
- arange
->low
< best_fit_len
3243 /* The following comparison is designed to return the same
3244 match as the previous algorithm for routines which have the
3245 same best fit length. */
3246 || (arange
->high
- arange
->low
== best_fit_len
3247 && funcinfo
> best_fit
))
3249 best_fit
= funcinfo
;
3250 best_fit_len
= arange
->high
- arange
->low
;
3260 *function_ptr
= best_fit
;
3264 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
3265 and LINENUMBER_PTR, and return TRUE. */
3268 lookup_symbol_in_function_table (struct comp_unit
*unit
,
3271 const char **filename_ptr
,
3272 unsigned int *linenumber_ptr
)
3274 struct funcinfo
* each_func
;
3275 struct funcinfo
* best_fit
= NULL
;
3276 bfd_vma best_fit_len
= 0;
3277 struct arange
*arange
;
3278 const char *name
= bfd_asymbol_name (sym
);
3279 asection
*sec
= bfd_asymbol_section (sym
);
3281 for (each_func
= unit
->function_table
;
3283 each_func
= each_func
->prev_func
)
3285 for (arange
= &each_func
->arange
;
3287 arange
= arange
->next
)
3289 if ((!each_func
->sec
|| each_func
->sec
== sec
)
3290 && addr
>= arange
->low
3291 && addr
< arange
->high
3293 && strcmp (name
, each_func
->name
) == 0
3295 || arange
->high
- arange
->low
< best_fit_len
))
3297 best_fit
= each_func
;
3298 best_fit_len
= arange
->high
- arange
->low
;
3305 best_fit
->sec
= sec
;
3306 *filename_ptr
= best_fit
->file
;
3307 *linenumber_ptr
= best_fit
->line
;
3314 /* Variable table functions. */
3316 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
3317 LINENUMBER_PTR, and return TRUE. */
3320 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
3323 const char **filename_ptr
,
3324 unsigned int *linenumber_ptr
)
3326 const char *name
= bfd_asymbol_name (sym
);
3327 asection
*sec
= bfd_asymbol_section (sym
);
3328 struct varinfo
* each
;
3330 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
3332 && each
->file
!= NULL
3333 && each
->name
!= NULL
3334 && each
->addr
== addr
3335 && (!each
->sec
|| each
->sec
== sec
)
3336 && strcmp (name
, each
->name
) == 0)
3342 *filename_ptr
= each
->file
;
3343 *linenumber_ptr
= each
->line
;
3350 static struct comp_unit
*stash_comp_unit (struct dwarf2_debug
*,
3351 struct dwarf2_debug_file
*);
3352 static bool comp_unit_maybe_decode_line_info (struct comp_unit
*);
3355 find_abstract_instance (struct comp_unit
*unit
,
3356 struct attribute
*attr_ptr
,
3357 unsigned int recur_count
,
3360 char **filename_ptr
,
3361 int *linenumber_ptr
)
3363 bfd
*abfd
= unit
->abfd
;
3364 bfd_byte
*info_ptr
= NULL
;
3365 bfd_byte
*info_ptr_end
;
3366 unsigned int abbrev_number
, i
;
3367 struct abbrev_info
*abbrev
;
3368 uint64_t die_ref
= attr_ptr
->u
.val
;
3369 struct attribute attr
;
3370 const char *name
= NULL
;
3372 if (recur_count
== 100)
3375 (_("DWARF error: abstract instance recursion detected"));
3376 bfd_set_error (bfd_error_bad_value
);
3380 /* DW_FORM_ref_addr can reference an entry in a different CU. It
3381 is an offset from the .debug_info section, not the current CU. */
3382 if (attr_ptr
->form
== DW_FORM_ref_addr
)
3384 /* We only support DW_FORM_ref_addr within the same file, so
3385 any relocations should be resolved already. Check this by
3386 testing for a zero die_ref; There can't be a valid reference
3387 to the header of a .debug_info section.
3388 DW_FORM_ref_addr is an offset relative to .debug_info.
3389 Normally when using the GNU linker this is accomplished by
3390 emitting a symbolic reference to a label, because .debug_info
3391 sections are linked at zero. When there are multiple section
3392 groups containing .debug_info, as there might be in a
3393 relocatable object file, it would be reasonable to assume that
3394 a symbolic reference to a label in any .debug_info section
3395 might be used. Since we lay out multiple .debug_info
3396 sections at non-zero VMAs (see place_sections), and read
3397 them contiguously into dwarf_info_buffer, that means the
3398 reference is relative to dwarf_info_buffer. */
3401 info_ptr
= unit
->file
->dwarf_info_buffer
;
3402 info_ptr_end
= info_ptr
+ unit
->file
->dwarf_info_size
;
3403 total
= info_ptr_end
- info_ptr
;
3406 else if (die_ref
>= total
)
3409 (_("DWARF error: invalid abstract instance DIE ref"));
3410 bfd_set_error (bfd_error_bad_value
);
3413 info_ptr
+= die_ref
;
3415 else if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3417 bool first_time
= unit
->stash
->alt
.dwarf_info_buffer
== NULL
;
3419 info_ptr
= read_alt_indirect_ref (unit
, die_ref
);
3421 unit
->stash
->alt
.info_ptr
= unit
->stash
->alt
.dwarf_info_buffer
;
3422 if (info_ptr
== NULL
)
3425 (_("DWARF error: unable to read alt ref %" PRIu64
),
3426 (uint64_t) die_ref
);
3427 bfd_set_error (bfd_error_bad_value
);
3430 info_ptr_end
= (unit
->stash
->alt
.dwarf_info_buffer
3431 + unit
->stash
->alt
.dwarf_info_size
);
3432 if (unit
->stash
->alt
.all_comp_units
)
3433 unit
= unit
->stash
->alt
.all_comp_units
;
3436 if (attr_ptr
->form
== DW_FORM_ref_addr
3437 || attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3439 /* Now find the CU containing this pointer. */
3440 if (info_ptr
>= unit
->info_ptr_unit
&& info_ptr
< unit
->end_ptr
)
3441 info_ptr_end
= unit
->end_ptr
;
3444 /* Check other CUs to see if they contain the abbrev. */
3445 struct comp_unit
*u
;
3447 for (u
= unit
->prev_unit
; u
!= NULL
; u
= u
->prev_unit
)
3448 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3452 for (u
= unit
->next_unit
; u
!= NULL
; u
= u
->next_unit
)
3453 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3456 if (attr_ptr
->form
== DW_FORM_ref_addr
)
3459 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->f
);
3462 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3467 if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3470 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->alt
);
3473 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3481 (_("DWARF error: unable to locate abstract instance DIE ref %"
3482 PRIu64
), (uint64_t) die_ref
);
3483 bfd_set_error (bfd_error_bad_value
);
3487 info_ptr_end
= unit
->end_ptr
;
3492 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
3493 DW_FORM_ref_udata. These are all references relative to the
3494 start of the current CU. */
3497 info_ptr
= unit
->info_ptr_unit
;
3498 info_ptr_end
= unit
->end_ptr
;
3499 total
= info_ptr_end
- info_ptr
;
3500 if (!die_ref
|| die_ref
>= total
)
3503 (_("DWARF error: invalid abstract instance DIE ref"));
3504 bfd_set_error (bfd_error_bad_value
);
3507 info_ptr
+= die_ref
;
3510 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3511 false, info_ptr_end
);
3514 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3518 (_("DWARF error: could not find abbrev number %u"), abbrev_number
);
3519 bfd_set_error (bfd_error_bad_value
);
3524 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3526 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
,
3527 info_ptr
, info_ptr_end
);
3528 if (info_ptr
== NULL
)
3533 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3535 if (name
== NULL
&& is_str_form (&attr
))
3538 if (non_mangled (unit
->lang
))
3542 case DW_AT_specification
:
3543 if (is_int_form (&attr
)
3544 && !find_abstract_instance (unit
, &attr
, recur_count
+ 1,
3546 filename_ptr
, linenumber_ptr
))
3549 case DW_AT_linkage_name
:
3550 case DW_AT_MIPS_linkage_name
:
3551 /* PR 16949: Corrupt debug info can place
3552 non-string forms into these attributes. */
3553 if (is_str_form (&attr
))
3559 case DW_AT_decl_file
:
3560 if (!comp_unit_maybe_decode_line_info (unit
))
3562 if (is_int_form (&attr
))
3563 *filename_ptr
= concat_filename (unit
->line_table
,
3566 case DW_AT_decl_line
:
3567 if (is_int_form (&attr
))
3568 *linenumber_ptr
= attr
.u
.val
;
3581 read_ranges (struct comp_unit
*unit
, struct arange
*arange
,
3582 struct trie_node
**trie_root
, uint64_t offset
)
3584 bfd_byte
*ranges_ptr
;
3585 bfd_byte
*ranges_end
;
3586 bfd_vma base_address
= unit
->base_address
;
3588 if (! unit
->file
->dwarf_ranges_buffer
)
3590 if (! read_debug_ranges (unit
))
3594 if (offset
> unit
->file
->dwarf_ranges_size
)
3596 ranges_ptr
= unit
->file
->dwarf_ranges_buffer
+ offset
;
3597 ranges_end
= unit
->file
->dwarf_ranges_buffer
+ unit
->file
->dwarf_ranges_size
;
3604 /* PR 17512: file: 62cada7d. */
3605 if (2u * unit
->addr_size
> (size_t) (ranges_end
- ranges_ptr
))
3608 low_pc
= read_address (unit
, &ranges_ptr
, ranges_end
);
3609 high_pc
= read_address (unit
, &ranges_ptr
, ranges_end
);
3611 if (low_pc
== 0 && high_pc
== 0)
3613 if (low_pc
== -1UL && high_pc
!= -1UL)
3614 base_address
= high_pc
;
3617 if (!arange_add (unit
, arange
, trie_root
,
3618 base_address
+ low_pc
, base_address
+ high_pc
))
3626 read_rnglists (struct comp_unit
*unit
, struct arange
*arange
,
3627 struct trie_node
**trie_root
, uint64_t offset
)
3631 bfd_vma base_address
= unit
->base_address
;
3634 bfd
*abfd
= unit
->abfd
;
3636 if (! unit
->file
->dwarf_rnglists_buffer
)
3638 if (! read_debug_rnglists (unit
))
3642 rngs_ptr
= unit
->file
->dwarf_rnglists_buffer
+ offset
;
3643 if (rngs_ptr
< unit
->file
->dwarf_rnglists_buffer
)
3645 rngs_end
= unit
->file
->dwarf_rnglists_buffer
;
3646 rngs_end
+= unit
->file
->dwarf_rnglists_size
;
3650 enum dwarf_range_list_entry rlet
;
3652 if (rngs_ptr
>= rngs_end
)
3655 rlet
= read_1_byte (abfd
, &rngs_ptr
, rngs_end
);
3659 case DW_RLE_end_of_list
:
3662 case DW_RLE_base_address
:
3663 if (unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3665 base_address
= read_address (unit
, &rngs_ptr
, rngs_end
);
3668 case DW_RLE_start_length
:
3669 if (unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3671 low_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3673 high_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3677 case DW_RLE_offset_pair
:
3678 low_pc
= base_address
;
3679 low_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3681 high_pc
= base_address
;
3682 high_pc
+= _bfd_safe_read_leb128 (abfd
, &rngs_ptr
,
3686 case DW_RLE_start_end
:
3687 if (2u * unit
->addr_size
> (size_t) (rngs_end
- rngs_ptr
))
3689 low_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3690 high_pc
= read_address (unit
, &rngs_ptr
, rngs_end
);
3693 /* TODO x-variants need .debug_addr support used for split-dwarf. */
3694 case DW_RLE_base_addressx
:
3695 case DW_RLE_startx_endx
:
3696 case DW_RLE_startx_length
:
3701 if (!arange_add (unit
, arange
, trie_root
, low_pc
, high_pc
))
3707 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
,
3708 struct trie_node
**trie_root
, uint64_t offset
)
3710 if (unit
->version
<= 4)
3711 return read_ranges (unit
, arange
, trie_root
, offset
);
3713 return read_rnglists (unit
, arange
, trie_root
, offset
);
3716 static struct funcinfo
*
3717 lookup_func_by_offset (uint64_t offset
, struct funcinfo
* table
)
3719 for (; table
!= NULL
; table
= table
->prev_func
)
3720 if (table
->unit_offset
== offset
)
3725 static struct varinfo
*
3726 lookup_var_by_offset (uint64_t offset
, struct varinfo
* table
)
3730 if (table
->unit_offset
== offset
)
3732 table
= table
->prev_var
;
3739 /* DWARF2 Compilation unit functions. */
3741 static struct funcinfo
*
3742 reverse_funcinfo_list (struct funcinfo
*head
)
3744 struct funcinfo
*rhead
;
3745 struct funcinfo
*temp
;
3747 for (rhead
= NULL
; head
; head
= temp
)
3749 temp
= head
->prev_func
;
3750 head
->prev_func
= rhead
;
3756 static struct varinfo
*
3757 reverse_varinfo_list (struct varinfo
*head
)
3759 struct varinfo
*rhead
;
3760 struct varinfo
*temp
;
3762 for (rhead
= NULL
; head
; head
= temp
)
3764 temp
= head
->prev_var
;
3765 head
->prev_var
= rhead
;
3771 /* Scan over each die in a comp. unit looking for functions to add
3772 to the function table and variables to the variable table. */
3775 scan_unit_for_symbols (struct comp_unit
*unit
)
3777 bfd
*abfd
= unit
->abfd
;
3778 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
3779 bfd_byte
*info_ptr_end
= unit
->end_ptr
;
3780 int nesting_level
= 0;
3781 struct nest_funcinfo
3783 struct funcinfo
*func
;
3785 int nested_funcs_size
;
3786 struct funcinfo
*last_func
;
3787 struct varinfo
*last_var
;
3789 /* Maintain a stack of in-scope functions and inlined functions, which we
3790 can use to set the caller_func field. */
3791 nested_funcs_size
= 32;
3792 nested_funcs
= (struct nest_funcinfo
*)
3793 bfd_malloc (nested_funcs_size
* sizeof (*nested_funcs
));
3794 if (nested_funcs
== NULL
)
3796 nested_funcs
[nesting_level
].func
= 0;
3798 /* PR 27484: We must scan the DIEs twice. The first time we look for
3799 function and variable tags and accumulate them into their respective
3800 tables. The second time through we process the attributes of the
3801 functions/variables and augment the table entries. */
3802 while (nesting_level
>= 0)
3804 unsigned int abbrev_number
, i
;
3805 struct abbrev_info
*abbrev
;
3806 struct funcinfo
*func
;
3807 struct varinfo
*var
;
3808 uint64_t current_offset
;
3810 /* PR 17512: file: 9f405d9d. */
3811 if (info_ptr
>= info_ptr_end
)
3814 current_offset
= info_ptr
- unit
->info_ptr_unit
;
3815 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3816 false, info_ptr_end
);
3817 if (abbrev_number
== 0)
3823 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3826 static unsigned int previous_failed_abbrev
= -1U;
3828 /* Avoid multiple reports of the same missing abbrev. */
3829 if (abbrev_number
!= previous_failed_abbrev
)
3832 (_("DWARF error: could not find abbrev number %u"),
3834 previous_failed_abbrev
= abbrev_number
;
3836 bfd_set_error (bfd_error_bad_value
);
3840 if (abbrev
->tag
== DW_TAG_subprogram
3841 || abbrev
->tag
== DW_TAG_entry_point
3842 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3844 size_t amt
= sizeof (struct funcinfo
);
3847 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
3850 func
->tag
= abbrev
->tag
;
3851 func
->prev_func
= unit
->function_table
;
3852 func
->unit_offset
= current_offset
;
3853 unit
->function_table
= func
;
3854 unit
->number_of_functions
++;
3855 BFD_ASSERT (!unit
->cached
);
3857 if (func
->tag
== DW_TAG_inlined_subroutine
)
3858 for (i
= nesting_level
; i
-- != 0; )
3859 if (nested_funcs
[i
].func
)
3861 func
->caller_func
= nested_funcs
[i
].func
;
3864 nested_funcs
[nesting_level
].func
= func
;
3869 if (abbrev
->tag
== DW_TAG_variable
3870 || abbrev
->tag
== DW_TAG_member
)
3872 size_t amt
= sizeof (struct varinfo
);
3874 var
= (struct varinfo
*) bfd_zalloc (abfd
, amt
);
3877 var
->tag
= abbrev
->tag
;
3879 var
->prev_var
= unit
->variable_table
;
3880 unit
->variable_table
= var
;
3881 var
->unit_offset
= current_offset
;
3882 /* PR 18205: Missing debug information can cause this
3883 var to be attached to an already cached unit. */
3888 /* No inline function in scope at this nesting level. */
3889 nested_funcs
[nesting_level
].func
= 0;
3892 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3894 struct attribute attr
;
3896 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
3897 unit
, info_ptr
, info_ptr_end
);
3898 if (info_ptr
== NULL
)
3902 if (abbrev
->has_children
)
3906 if (nesting_level
>= nested_funcs_size
)
3908 struct nest_funcinfo
*tmp
;
3910 nested_funcs_size
*= 2;
3911 tmp
= (struct nest_funcinfo
*)
3912 bfd_realloc (nested_funcs
,
3913 nested_funcs_size
* sizeof (*nested_funcs
));
3918 nested_funcs
[nesting_level
].func
= 0;
3922 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
3923 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
3925 /* This is the second pass over the abbrevs. */
3926 info_ptr
= unit
->first_child_die_ptr
;
3932 while (nesting_level
>= 0)
3934 unsigned int abbrev_number
, i
;
3935 struct abbrev_info
*abbrev
;
3936 struct attribute attr
;
3937 struct funcinfo
*func
;
3938 struct varinfo
*var
;
3940 bfd_vma high_pc
= 0;
3941 bool high_pc_relative
= false;
3942 uint64_t current_offset
;
3944 /* PR 17512: file: 9f405d9d. */
3945 if (info_ptr
>= info_ptr_end
)
3948 current_offset
= info_ptr
- unit
->info_ptr_unit
;
3949 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
3950 false, info_ptr_end
);
3951 if (! abbrev_number
)
3957 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3958 /* This should have been handled above. */
3959 BFD_ASSERT (abbrev
!= NULL
);
3963 if (abbrev
->tag
== DW_TAG_subprogram
3964 || abbrev
->tag
== DW_TAG_entry_point
3965 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3968 && last_func
->prev_func
3969 && last_func
->prev_func
->unit_offset
== current_offset
)
3970 func
= last_func
->prev_func
;
3972 func
= lookup_func_by_offset (current_offset
, unit
->function_table
);
3979 else if (abbrev
->tag
== DW_TAG_variable
3980 || abbrev
->tag
== DW_TAG_member
)
3983 && last_var
->prev_var
3984 && last_var
->prev_var
->unit_offset
== current_offset
)
3985 var
= last_var
->prev_var
;
3987 var
= lookup_var_by_offset (current_offset
, unit
->variable_table
);
3995 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3997 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
3998 unit
, info_ptr
, info_ptr_end
);
3999 if (info_ptr
== NULL
)
4006 case DW_AT_call_file
:
4007 if (is_int_form (&attr
))
4008 func
->caller_file
= concat_filename (unit
->line_table
,
4012 case DW_AT_call_line
:
4013 if (is_int_form (&attr
))
4014 func
->caller_line
= attr
.u
.val
;
4017 case DW_AT_abstract_origin
:
4018 case DW_AT_specification
:
4019 if (is_int_form (&attr
)
4020 && !find_abstract_instance (unit
, &attr
, 0,
4029 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
4031 if (func
->name
== NULL
&& is_str_form (&attr
))
4033 func
->name
= attr
.u
.str
;
4034 if (non_mangled (unit
->lang
))
4035 func
->is_linkage
= true;
4039 case DW_AT_linkage_name
:
4040 case DW_AT_MIPS_linkage_name
:
4041 /* PR 16949: Corrupt debug info can place
4042 non-string forms into these attributes. */
4043 if (is_str_form (&attr
))
4045 func
->name
= attr
.u
.str
;
4046 func
->is_linkage
= true;
4051 if (is_int_form (&attr
))
4052 low_pc
= attr
.u
.val
;
4056 if (is_int_form (&attr
))
4058 high_pc
= attr
.u
.val
;
4059 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
4064 if (is_int_form (&attr
)
4065 && !read_rangelist (unit
, &func
->arange
,
4066 &unit
->file
->trie_root
, attr
.u
.val
))
4070 case DW_AT_decl_file
:
4071 if (is_int_form (&attr
))
4072 func
->file
= concat_filename (unit
->line_table
,
4076 case DW_AT_decl_line
:
4077 if (is_int_form (&attr
))
4078 func
->line
= attr
.u
.val
;
4089 case DW_AT_specification
:
4090 if (is_int_form (&attr
) && attr
.u
.val
)
4092 struct varinfo
* spec_var
;
4094 spec_var
= lookup_var_by_offset (attr
.u
.val
,
4095 unit
->variable_table
);
4096 if (spec_var
== NULL
)
4098 _bfd_error_handler (_("DWARF error: could not find "
4099 "variable specification "
4101 (unsigned long) attr
.u
.val
);
4105 if (var
->name
== NULL
)
4106 var
->name
= spec_var
->name
;
4107 if (var
->file
== NULL
&& spec_var
->file
!= NULL
)
4108 var
->file
= strdup (spec_var
->file
);
4110 var
->line
= spec_var
->line
;
4111 if (var
->sec
== NULL
)
4112 var
->sec
= spec_var
->sec
;
4117 if (is_str_form (&attr
))
4118 var
->name
= attr
.u
.str
;
4121 case DW_AT_decl_file
:
4122 if (is_int_form (&attr
))
4123 var
->file
= concat_filename (unit
->line_table
,
4127 case DW_AT_decl_line
:
4128 if (is_int_form (&attr
))
4129 var
->line
= attr
.u
.val
;
4132 case DW_AT_external
:
4133 if (is_int_form (&attr
) && attr
.u
.val
!= 0)
4137 case DW_AT_location
:
4141 case DW_FORM_block1
:
4142 case DW_FORM_block2
:
4143 case DW_FORM_block4
:
4144 case DW_FORM_exprloc
:
4145 if (attr
.u
.blk
->data
!= NULL
4146 && *attr
.u
.blk
->data
== DW_OP_addr
)
4150 /* Verify that DW_OP_addr is the only opcode in the
4151 location, in which case the block size will be 1
4152 plus the address size. */
4153 /* ??? For TLS variables, gcc can emit
4154 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
4155 which we don't handle here yet. */
4156 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
4157 var
->addr
= bfd_get (unit
->addr_size
* 8,
4159 attr
.u
.blk
->data
+ 1);
4174 if (abbrev
->has_children
)
4177 if (high_pc_relative
)
4180 if (func
&& high_pc
!= 0)
4182 if (!arange_add (unit
, &func
->arange
, &unit
->file
->trie_root
,
4188 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4189 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4191 free (nested_funcs
);
4195 free (nested_funcs
);
4199 /* Read the attributes of the form strx and addrx. */
4202 reread_attribute (struct comp_unit
*unit
,
4203 struct attribute
*attr
,
4206 bool *high_pc_relative
,
4209 if (is_strx_form (attr
->form
))
4210 attr
->u
.str
= (char *) read_indexed_string (attr
->u
.val
, unit
);
4211 if (is_addrx_form (attr
->form
))
4212 attr
->u
.val
= read_indexed_address (attr
->u
.val
, unit
);
4216 case DW_AT_stmt_list
:
4218 unit
->line_offset
= attr
->u
.val
;
4222 if (is_str_form (attr
))
4223 unit
->name
= attr
->u
.str
;
4227 *low_pc
= attr
->u
.val
;
4229 unit
->base_address
= *low_pc
;
4233 *high_pc
= attr
->u
.val
;
4234 *high_pc_relative
= attr
->form
!= DW_FORM_addr
;
4238 if (!read_rangelist (unit
, &unit
->arange
,
4239 &unit
->file
->trie_root
, attr
->u
.val
))
4243 case DW_AT_comp_dir
:
4245 char *comp_dir
= attr
->u
.str
;
4247 if (!is_str_form (attr
))
4250 (_("DWARF error: DW_AT_comp_dir attribute encountered "
4251 "with a non-string form"));
4257 char *cp
= strchr (comp_dir
, ':');
4259 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
4262 unit
->comp_dir
= comp_dir
;
4266 case DW_AT_language
:
4267 unit
->lang
= attr
->u
.val
;
4273 /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
4274 includes the compilation unit header that proceeds the DIE's, but
4275 does not include the length field that precedes each compilation
4276 unit header. END_PTR points one past the end of this comp unit.
4277 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
4279 This routine does not read the whole compilation unit; only enough
4280 to get to the line number information for the compilation unit. */
4282 static struct comp_unit
*
4283 parse_comp_unit (struct dwarf2_debug
*stash
,
4284 struct dwarf2_debug_file
*file
,
4286 bfd_vma unit_length
,
4287 bfd_byte
*info_ptr_unit
,
4288 unsigned int offset_size
)
4290 struct comp_unit
* unit
;
4291 unsigned int version
;
4292 uint64_t abbrev_offset
= 0;
4293 /* Initialize it just to avoid a GCC false warning. */
4294 unsigned int addr_size
= -1;
4295 struct abbrev_info
** abbrevs
;
4296 unsigned int abbrev_number
, i
;
4297 struct abbrev_info
*abbrev
;
4298 struct attribute attr
;
4299 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
4302 bfd_vma high_pc
= 0;
4303 bfd
*abfd
= file
->bfd_ptr
;
4304 bool high_pc_relative
= false;
4305 enum dwarf_unit_type unit_type
;
4306 struct attribute
*str_addrp
= NULL
;
4307 size_t str_count
= 0;
4308 size_t str_alloc
= 0;
4309 bool compunit_flag
= false;
4311 version
= read_2_bytes (abfd
, &info_ptr
, end_ptr
);
4312 if (version
< 2 || version
> 5)
4314 /* PR 19872: A version number of 0 probably means that there is padding
4315 at the end of the .debug_info section. Gold puts it there when
4316 performing an incremental link, for example. So do not generate
4317 an error, just return a NULL. */
4321 (_("DWARF error: found dwarf version '%u', this reader"
4322 " only handles version 2, 3, 4 and 5 information"), version
);
4323 bfd_set_error (bfd_error_bad_value
);
4329 unit_type
= DW_UT_compile
;
4332 unit_type
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
4333 addr_size
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
4336 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
4337 if (offset_size
== 4)
4338 abbrev_offset
= read_4_bytes (abfd
, &info_ptr
, end_ptr
);
4340 abbrev_offset
= read_8_bytes (abfd
, &info_ptr
, end_ptr
);
4343 addr_size
= read_1_byte (abfd
, &info_ptr
, end_ptr
);
4345 if (unit_type
== DW_UT_type
)
4347 /* Skip type signature. */
4350 /* Skip type offset. */
4351 info_ptr
+= offset_size
;
4354 if (addr_size
> sizeof (bfd_vma
))
4357 /* xgettext: c-format */
4358 (_("DWARF error: found address size '%u', this reader"
4359 " can not handle sizes greater than '%u'"),
4361 (unsigned int) sizeof (bfd_vma
));
4362 bfd_set_error (bfd_error_bad_value
);
4366 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
4369 ("DWARF error: found address size '%u', this reader"
4370 " can only handle address sizes '2', '4' and '8'", addr_size
);
4371 bfd_set_error (bfd_error_bad_value
);
4375 /* Read the abbrevs for this compilation unit into a table. */
4376 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
, file
);
4380 abbrev_number
= _bfd_safe_read_leb128 (abfd
, &info_ptr
,
4382 if (! abbrev_number
)
4384 /* PR 19872: An abbrev number of 0 probably means that there is padding
4385 at the end of the .debug_abbrev section. Gold puts it there when
4386 performing an incremental link, for example. So do not generate
4387 an error, just return a NULL. */
4391 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
4394 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
4396 bfd_set_error (bfd_error_bad_value
);
4400 amt
= sizeof (struct comp_unit
);
4401 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
4405 unit
->version
= version
;
4406 unit
->addr_size
= addr_size
;
4407 unit
->offset_size
= offset_size
;
4408 unit
->abbrevs
= abbrevs
;
4409 unit
->end_ptr
= end_ptr
;
4410 unit
->stash
= stash
;
4412 unit
->info_ptr_unit
= info_ptr_unit
;
4414 if (abbrev
->tag
== DW_TAG_compile_unit
)
4415 compunit_flag
= true;
4417 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
4419 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
, end_ptr
);
4420 if (info_ptr
== NULL
)
4423 /* Identify attributes of the form strx* and addrx* which come before
4424 DW_AT_str_offsets_base and DW_AT_addr_base respectively in the CU.
4425 Store the attributes in an array and process them later. */
4426 if ((unit
->dwarf_str_offset
== 0 && is_strx_form (attr
.form
))
4427 || (unit
->dwarf_addr_offset
== 0 && is_addrx_form (attr
.form
)))
4429 if (str_count
<= str_alloc
)
4431 str_alloc
= 2 * str_alloc
+ 200;
4432 str_addrp
= bfd_realloc (str_addrp
,
4433 str_alloc
* sizeof (*str_addrp
));
4434 if (str_addrp
== NULL
)
4437 str_addrp
[str_count
] = attr
;
4442 /* Store the data if it is of an attribute we want to keep in a
4443 partial symbol table. */
4446 case DW_AT_stmt_list
:
4447 if (is_int_form (&attr
))
4450 unit
->line_offset
= attr
.u
.val
;
4455 if (is_str_form (&attr
))
4456 unit
->name
= attr
.u
.str
;
4460 if (is_int_form (&attr
))
4462 low_pc
= attr
.u
.val
;
4463 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
4464 this is the base address to use when reading location
4465 lists or range lists. */
4467 unit
->base_address
= low_pc
;
4472 if (is_int_form (&attr
))
4474 high_pc
= attr
.u
.val
;
4475 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
4480 if (is_int_form (&attr
)
4481 && !read_rangelist (unit
, &unit
->arange
,
4482 &unit
->file
->trie_root
, attr
.u
.val
))
4486 case DW_AT_comp_dir
:
4488 char *comp_dir
= attr
.u
.str
;
4490 /* PR 17512: file: 1fe726be. */
4491 if (!is_str_form (&attr
))
4494 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
4500 /* Irix 6.2 native cc prepends <machine>.: to the compilation
4501 directory, get rid of it. */
4502 char *cp
= strchr (comp_dir
, ':');
4504 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
4507 unit
->comp_dir
= comp_dir
;
4511 case DW_AT_language
:
4512 if (is_int_form (&attr
))
4513 unit
->lang
= attr
.u
.val
;
4516 case DW_AT_addr_base
:
4517 unit
->dwarf_addr_offset
= attr
.u
.val
;
4520 case DW_AT_str_offsets_base
:
4521 unit
->dwarf_str_offset
= attr
.u
.val
;
4529 for (i
= 0; i
< str_count
; ++i
)
4530 reread_attribute (unit
, &str_addrp
[i
], &low_pc
, &high_pc
,
4531 &high_pc_relative
, compunit_flag
);
4533 if (high_pc_relative
)
4537 if (!arange_add (unit
, &unit
->arange
, &unit
->file
->trie_root
,
4542 unit
->first_child_die_ptr
= info_ptr
;
4553 /* Return TRUE if UNIT may contain the address given by ADDR. When
4554 there are functions written entirely with inline asm statements, the
4555 range info in the compilation unit header may not be correct. We
4556 need to consult the line info table to see if a compilation unit
4557 really contains the given address. */
4560 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
4562 struct arange
*arange
;
4567 arange
= &unit
->arange
;
4570 if (addr
>= arange
->low
&& addr
< arange
->high
)
4572 arange
= arange
->next
;
4579 /* If UNIT contains ADDR, set the output parameters to the values for
4580 the line containing ADDR and return TRUE. Otherwise return FALSE.
4581 The output parameters, FILENAME_PTR, FUNCTION_PTR, and
4582 LINENUMBER_PTR, are pointers to the objects to be filled in. */
4585 comp_unit_find_nearest_line (struct comp_unit
*unit
,
4587 const char **filename_ptr
,
4588 struct funcinfo
**function_ptr
,
4589 unsigned int *linenumber_ptr
,
4590 unsigned int *discriminator_ptr
)
4592 bool line_p
, func_p
;
4594 if (!comp_unit_maybe_decode_line_info (unit
))
4597 *function_ptr
= NULL
;
4598 func_p
= lookup_address_in_function_table (unit
, addr
, function_ptr
);
4599 if (func_p
&& (*function_ptr
)->tag
== DW_TAG_inlined_subroutine
)
4600 unit
->stash
->inliner_chain
= *function_ptr
;
4602 line_p
= lookup_address_in_line_info_table (unit
->line_table
, addr
,
4606 return line_p
|| func_p
;
4609 /* Check to see if line info is already decoded in a comp_unit.
4610 If not, decode it. Returns TRUE if no errors were encountered;
4614 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
)
4619 if (! unit
->line_table
)
4621 if (! unit
->stmtlist
)
4627 unit
->line_table
= decode_line_info (unit
);
4629 if (! unit
->line_table
)
4635 if (unit
->first_child_die_ptr
< unit
->end_ptr
4636 && ! scan_unit_for_symbols (unit
))
4646 /* If UNIT contains SYM at ADDR, set the output parameters to the
4647 values for the line containing SYM. The output parameters,
4648 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
4651 Return TRUE if UNIT contains SYM, and no errors were encountered;
4655 comp_unit_find_line (struct comp_unit
*unit
,
4658 const char **filename_ptr
,
4659 unsigned int *linenumber_ptr
)
4661 if (!comp_unit_maybe_decode_line_info (unit
))
4664 if (sym
->flags
& BSF_FUNCTION
)
4665 return lookup_symbol_in_function_table (unit
, sym
, addr
,
4669 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
4674 /* Extract all interesting funcinfos and varinfos of a compilation
4675 unit into hash tables for faster lookup. Returns TRUE if no
4676 errors were enountered; FALSE otherwise. */
4679 comp_unit_hash_info (struct dwarf2_debug
*stash
,
4680 struct comp_unit
*unit
,
4681 struct info_hash_table
*funcinfo_hash_table
,
4682 struct info_hash_table
*varinfo_hash_table
)
4684 struct funcinfo
* each_func
;
4685 struct varinfo
* each_var
;
4688 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
4690 if (!comp_unit_maybe_decode_line_info (unit
))
4693 BFD_ASSERT (!unit
->cached
);
4695 /* To preserve the original search order, we went to visit the function
4696 infos in the reversed order of the list. However, making the list
4697 bi-directional use quite a bit of extra memory. So we reverse
4698 the list first, traverse the list in the now reversed order and
4699 finally reverse the list again to get back the original order. */
4700 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4701 for (each_func
= unit
->function_table
;
4703 each_func
= each_func
->prev_func
)
4705 /* Skip nameless functions. */
4706 if (each_func
->name
)
4707 /* There is no need to copy name string into hash table as
4708 name string is either in the dwarf string buffer or
4709 info in the stash. */
4710 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
4711 (void*) each_func
, false);
4713 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4717 /* We do the same for variable infos. */
4718 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4719 for (each_var
= unit
->variable_table
;
4721 each_var
= each_var
->prev_var
)
4723 /* Skip stack vars and vars with no files or names. */
4724 if (! each_var
->stack
4725 && each_var
->file
!= NULL
4726 && each_var
->name
!= NULL
)
4727 /* There is no need to copy name string into hash table as
4728 name string is either in the dwarf string buffer or
4729 info in the stash. */
4730 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
4731 (void*) each_var
, false);
4734 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4735 unit
->cached
= true;
4739 /* Locate a section in a BFD containing debugging info. The search starts
4740 from the section after AFTER_SEC, or from the first section in the BFD if
4741 AFTER_SEC is NULL. The search works by examining the names of the
4742 sections. There are three permissiable names. The first two are given
4743 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
4744 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
4745 This is a variation on the .debug_info section which has a checksum
4746 describing the contents appended onto the name. This allows the linker to
4747 identify and discard duplicate debugging sections for different
4748 compilation units. */
4749 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
4752 find_debug_info (bfd
*abfd
, const struct dwarf_debug_section
*debug_sections
,
4753 asection
*after_sec
)
4758 if (after_sec
== NULL
)
4760 look
= debug_sections
[debug_info
].uncompressed_name
;
4761 msec
= bfd_get_section_by_name (abfd
, look
);
4765 look
= debug_sections
[debug_info
].compressed_name
;
4766 msec
= bfd_get_section_by_name (abfd
, look
);
4770 for (msec
= abfd
->sections
; msec
!= NULL
; msec
= msec
->next
)
4771 if (startswith (msec
->name
, GNU_LINKONCE_INFO
))
4777 for (msec
= after_sec
->next
; msec
!= NULL
; msec
= msec
->next
)
4779 look
= debug_sections
[debug_info
].uncompressed_name
;
4780 if (strcmp (msec
->name
, look
) == 0)
4783 look
= debug_sections
[debug_info
].compressed_name
;
4784 if (look
!= NULL
&& strcmp (msec
->name
, look
) == 0)
4787 if (startswith (msec
->name
, GNU_LINKONCE_INFO
))
4794 /* Transfer VMAs from object file to separate debug file. */
4797 set_debug_vma (bfd
*orig_bfd
, bfd
*debug_bfd
)
4801 for (s
= orig_bfd
->sections
, d
= debug_bfd
->sections
;
4802 s
!= NULL
&& d
!= NULL
;
4803 s
= s
->next
, d
= d
->next
)
4805 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4807 /* ??? Assumes 1-1 correspondence between sections in the
4809 if (strcmp (s
->name
, d
->name
) == 0)
4811 d
->output_section
= s
->output_section
;
4812 d
->output_offset
= s
->output_offset
;
4818 /* If the dwarf2 info was found in a separate debug file, return the
4819 debug file section corresponding to the section in the original file
4820 and the debug file symbols. */
4823 _bfd_dwarf2_stash_syms (struct dwarf2_debug
*stash
, bfd
*abfd
,
4824 asection
**sec
, asymbol
***syms
)
4826 if (stash
->f
.bfd_ptr
!= abfd
)
4832 *syms
= stash
->f
.syms
;
4836 for (s
= abfd
->sections
, d
= stash
->f
.bfd_ptr
->sections
;
4837 s
!= NULL
&& d
!= NULL
;
4838 s
= s
->next
, d
= d
->next
)
4840 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4843 && strcmp (s
->name
, d
->name
) == 0)
4846 *syms
= stash
->f
.syms
;
4853 /* Unset vmas for adjusted sections in STASH. */
4856 unset_sections (struct dwarf2_debug
*stash
)
4859 struct adjusted_section
*p
;
4861 i
= stash
->adjusted_section_count
;
4862 p
= stash
->adjusted_sections
;
4863 for (; i
> 0; i
--, p
++)
4864 p
->section
->vma
= 0;
4867 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4868 relocatable object file. VMAs are normally all zero in relocatable
4869 object files, so if we want to distinguish locations in sections by
4870 address we need to set VMAs so the sections do not overlap. We
4871 also set VMA on .debug_info so that when we have multiple
4872 .debug_info sections (or the linkonce variant) they also do not
4873 overlap. The multiple .debug_info sections make up a single
4874 logical section. ??? We should probably do the same for other
4878 place_sections (bfd
*orig_bfd
, struct dwarf2_debug
*stash
)
4881 struct adjusted_section
*p
;
4883 const char *debug_info_name
;
4885 if (stash
->adjusted_section_count
!= 0)
4887 i
= stash
->adjusted_section_count
;
4888 p
= stash
->adjusted_sections
;
4889 for (; i
> 0; i
--, p
++)
4890 p
->section
->vma
= p
->adj_vma
;
4894 debug_info_name
= stash
->debug_sections
[debug_info
].uncompressed_name
;
4901 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4905 if ((sect
->output_section
!= NULL
4906 && sect
->output_section
!= sect
4907 && (sect
->flags
& SEC_DEBUGGING
) == 0)
4911 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
4912 || startswith (sect
->name
, GNU_LINKONCE_INFO
));
4914 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
4920 if (abfd
== stash
->f
.bfd_ptr
)
4922 abfd
= stash
->f
.bfd_ptr
;
4926 stash
->adjusted_section_count
= -1;
4929 bfd_vma last_vma
= 0, last_dwarf
= 0;
4930 size_t amt
= i
* sizeof (struct adjusted_section
);
4932 p
= (struct adjusted_section
*) bfd_malloc (amt
);
4936 stash
->adjusted_sections
= p
;
4937 stash
->adjusted_section_count
= i
;
4944 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4949 if ((sect
->output_section
!= NULL
4950 && sect
->output_section
!= sect
4951 && (sect
->flags
& SEC_DEBUGGING
) == 0)
4955 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
4956 || startswith (sect
->name
, GNU_LINKONCE_INFO
));
4958 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
4962 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
4966 BFD_ASSERT (sect
->alignment_power
== 0);
4967 sect
->vma
= last_dwarf
;
4972 /* Align the new address to the current section
4974 last_vma
= ((last_vma
4975 + ~(-((bfd_vma
) 1 << sect
->alignment_power
)))
4976 & (-((bfd_vma
) 1 << sect
->alignment_power
)));
4977 sect
->vma
= last_vma
;
4982 p
->adj_vma
= sect
->vma
;
4985 if (abfd
== stash
->f
.bfd_ptr
)
4987 abfd
= stash
->f
.bfd_ptr
;
4991 if (orig_bfd
!= stash
->f
.bfd_ptr
)
4992 set_debug_vma (orig_bfd
, stash
->f
.bfd_ptr
);
4997 /* Look up a funcinfo by name using the given info hash table. If found,
4998 also update the locations pointed to by filename_ptr and linenumber_ptr.
5000 This function returns TRUE if a funcinfo that matches the given symbol
5001 and address is found with any error; otherwise it returns FALSE. */
5004 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
5007 const char **filename_ptr
,
5008 unsigned int *linenumber_ptr
)
5010 struct funcinfo
* each_func
;
5011 struct funcinfo
* best_fit
= NULL
;
5012 bfd_vma best_fit_len
= 0;
5013 struct info_list_node
*node
;
5014 struct arange
*arange
;
5015 const char *name
= bfd_asymbol_name (sym
);
5016 asection
*sec
= bfd_asymbol_section (sym
);
5018 for (node
= lookup_info_hash_table (hash_table
, name
);
5022 each_func
= (struct funcinfo
*) node
->info
;
5023 for (arange
= &each_func
->arange
;
5025 arange
= arange
->next
)
5027 if ((!each_func
->sec
|| each_func
->sec
== sec
)
5028 && addr
>= arange
->low
5029 && addr
< arange
->high
5031 || arange
->high
- arange
->low
< best_fit_len
))
5033 best_fit
= each_func
;
5034 best_fit_len
= arange
->high
- arange
->low
;
5041 best_fit
->sec
= sec
;
5042 *filename_ptr
= best_fit
->file
;
5043 *linenumber_ptr
= best_fit
->line
;
5050 /* Look up a varinfo by name using the given info hash table. If found,
5051 also update the locations pointed to by filename_ptr and linenumber_ptr.
5053 This function returns TRUE if a varinfo that matches the given symbol
5054 and address is found with any error; otherwise it returns FALSE. */
5057 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
5060 const char **filename_ptr
,
5061 unsigned int *linenumber_ptr
)
5063 const char *name
= bfd_asymbol_name (sym
);
5064 asection
*sec
= bfd_asymbol_section (sym
);
5065 struct varinfo
* each
;
5066 struct info_list_node
*node
;
5068 for (node
= lookup_info_hash_table (hash_table
, name
);
5072 each
= (struct varinfo
*) node
->info
;
5073 if (each
->addr
== addr
5074 && (!each
->sec
|| each
->sec
== sec
))
5077 *filename_ptr
= each
->file
;
5078 *linenumber_ptr
= each
->line
;
5086 /* Update the funcinfo and varinfo info hash tables if they are
5087 not up to date. Returns TRUE if there is no error; otherwise
5088 returns FALSE and disable the info hash tables. */
5091 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
5093 struct comp_unit
*each
;
5095 /* Exit if hash tables are up-to-date. */
5096 if (stash
->f
.all_comp_units
== stash
->hash_units_head
)
5099 if (stash
->hash_units_head
)
5100 each
= stash
->hash_units_head
->prev_unit
;
5102 each
= stash
->f
.last_comp_unit
;
5106 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
5107 stash
->varinfo_hash_table
))
5109 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
5112 each
= each
->prev_unit
;
5115 stash
->hash_units_head
= stash
->f
.all_comp_units
;
5119 /* Check consistency of info hash tables. This is for debugging only. */
5121 static void ATTRIBUTE_UNUSED
5122 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
5124 struct comp_unit
*each_unit
;
5125 struct funcinfo
*each_func
;
5126 struct varinfo
*each_var
;
5127 struct info_list_node
*node
;
5130 for (each_unit
= stash
->f
.all_comp_units
;
5132 each_unit
= each_unit
->next_unit
)
5134 for (each_func
= each_unit
->function_table
;
5136 each_func
= each_func
->prev_func
)
5138 if (!each_func
->name
)
5140 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
5144 while (node
&& !found
)
5146 found
= node
->info
== each_func
;
5152 for (each_var
= each_unit
->variable_table
;
5154 each_var
= each_var
->prev_var
)
5156 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
5158 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
5162 while (node
&& !found
)
5164 found
= node
->info
== each_var
;
5172 /* Check to see if we want to enable the info hash tables, which consume
5173 quite a bit of memory. Currently we only check the number times
5174 bfd_dwarf2_find_line is called. In the future, we may also want to
5175 take the number of symbols into account. */
5178 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
5180 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
5182 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
5185 /* FIXME: Maybe we should check the reduce_memory_overheads
5186 and optimize fields in the bfd_link_info structure ? */
5188 /* Create hash tables. */
5189 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
5190 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
5191 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
5193 /* Turn off info hashes if any allocation above fails. */
5194 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
5197 /* We need a forced update so that the info hash tables will
5198 be created even though there is no compilation unit. That
5199 happens if STASH_INFO_HASH_TRIGGER is 0. */
5200 if (stash_maybe_update_info_hash_tables (stash
))
5201 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
5204 /* Find the file and line associated with a symbol and address using the
5205 info hash tables of a stash. If there is a match, the function returns
5206 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
5207 otherwise it returns FALSE. */
5210 stash_find_line_fast (struct dwarf2_debug
*stash
,
5213 const char **filename_ptr
,
5214 unsigned int *linenumber_ptr
)
5216 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
5218 if (sym
->flags
& BSF_FUNCTION
)
5219 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
5220 filename_ptr
, linenumber_ptr
);
5221 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
5222 filename_ptr
, linenumber_ptr
);
5225 /* Save current section VMAs. */
5228 save_section_vma (const bfd
*abfd
, struct dwarf2_debug
*stash
)
5233 if (abfd
->section_count
== 0)
5235 stash
->sec_vma
= bfd_malloc (sizeof (*stash
->sec_vma
) * abfd
->section_count
);
5236 if (stash
->sec_vma
== NULL
)
5238 stash
->sec_vma_count
= abfd
->section_count
;
5239 for (i
= 0, s
= abfd
->sections
;
5240 s
!= NULL
&& i
< abfd
->section_count
;
5243 if (s
->output_section
!= NULL
)
5244 stash
->sec_vma
[i
] = s
->output_section
->vma
+ s
->output_offset
;
5246 stash
->sec_vma
[i
] = s
->vma
;
5251 /* Compare current section VMAs against those at the time the stash
5252 was created. If find_nearest_line is used in linker warnings or
5253 errors early in the link process, the debug info stash will be
5254 invalid for later calls. This is because we relocate debug info
5255 sections, so the stashed section contents depend on symbol values,
5256 which in turn depend on section VMAs. */
5259 section_vma_same (const bfd
*abfd
, const struct dwarf2_debug
*stash
)
5264 /* PR 24334: If the number of sections in ABFD has changed between
5265 when the stash was created and now, then we cannot trust the
5266 stashed vma information. */
5267 if (abfd
->section_count
!= stash
->sec_vma_count
)
5270 for (i
= 0, s
= abfd
->sections
;
5271 s
!= NULL
&& i
< abfd
->section_count
;
5276 if (s
->output_section
!= NULL
)
5277 vma
= s
->output_section
->vma
+ s
->output_offset
;
5280 if (vma
!= stash
->sec_vma
[i
])
5286 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
5287 If DEBUG_BFD is not specified, we read debug information from ABFD
5288 or its gnu_debuglink. The results will be stored in PINFO.
5289 The function returns TRUE iff debug information is ready. */
5292 _bfd_dwarf2_slurp_debug_info (bfd
*abfd
, bfd
*debug_bfd
,
5293 const struct dwarf_debug_section
*debug_sections
,
5298 size_t amt
= sizeof (struct dwarf2_debug
);
5299 bfd_size_type total_size
;
5301 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
5305 if (stash
->orig_bfd
== abfd
5306 && section_vma_same (abfd
, stash
))
5308 /* Check that we did previously find some debug information
5309 before attempting to make use of it. */
5310 if (stash
->f
.bfd_ptr
!= NULL
)
5312 if (do_place
&& !place_sections (abfd
, stash
))
5319 _bfd_dwarf2_cleanup_debug_info (abfd
, pinfo
);
5320 memset (stash
, 0, amt
);
5324 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
5328 stash
->orig_bfd
= abfd
;
5329 stash
->debug_sections
= debug_sections
;
5330 stash
->f
.syms
= symbols
;
5331 if (!save_section_vma (abfd
, stash
))
5334 stash
->f
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
5335 del_abbrev
, calloc
, free
);
5336 if (!stash
->f
.abbrev_offsets
)
5339 stash
->alt
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
5340 del_abbrev
, calloc
, free
);
5341 if (!stash
->alt
.abbrev_offsets
)
5344 stash
->f
.trie_root
= alloc_trie_leaf (abfd
);
5345 if (!stash
->f
.trie_root
)
5348 stash
->alt
.trie_root
= alloc_trie_leaf (abfd
);
5349 if (!stash
->alt
.trie_root
)
5354 if (debug_bfd
== NULL
)
5357 msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
5358 if (msec
== NULL
&& abfd
== debug_bfd
)
5360 char * debug_filename
;
5362 debug_filename
= bfd_follow_build_id_debuglink (abfd
, DEBUGDIR
);
5363 if (debug_filename
== NULL
)
5364 debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
5366 if (debug_filename
== NULL
)
5367 /* No dwarf2 info, and no gnu_debuglink to follow.
5368 Note that at this point the stash has been allocated, but
5369 contains zeros. This lets future calls to this function
5370 fail more quickly. */
5373 debug_bfd
= bfd_openr (debug_filename
, NULL
);
5374 free (debug_filename
);
5375 if (debug_bfd
== NULL
)
5376 /* FIXME: Should we report our failure to follow the debuglink ? */
5379 /* Set BFD_DECOMPRESS to decompress debug sections. */
5380 debug_bfd
->flags
|= BFD_DECOMPRESS
;
5381 if (!bfd_check_format (debug_bfd
, bfd_object
)
5382 || (msec
= find_debug_info (debug_bfd
,
5383 debug_sections
, NULL
)) == NULL
5384 || !bfd_generic_link_read_symbols (debug_bfd
))
5386 bfd_close (debug_bfd
);
5390 symbols
= bfd_get_outsymbols (debug_bfd
);
5391 stash
->f
.syms
= symbols
;
5392 stash
->close_on_cleanup
= true;
5394 stash
->f
.bfd_ptr
= debug_bfd
;
5397 && !place_sections (abfd
, stash
))
5400 /* There can be more than one DWARF2 info section in a BFD these
5401 days. First handle the easy case when there's only one. If
5402 there's more than one, try case two: none of the sections is
5403 compressed. In that case, read them all in and produce one
5404 large stash. We do this in two passes - in the first pass we
5405 just accumulate the section sizes, and in the second pass we
5406 read in the section's contents. (The allows us to avoid
5407 reallocing the data as we add sections to the stash.) If
5408 some or all sections are compressed, then do things the slow
5409 way, with a bunch of reallocs. */
5411 if (! find_debug_info (debug_bfd
, debug_sections
, msec
))
5413 /* Case 1: only one info section. */
5414 total_size
= msec
->size
;
5415 if (! read_section (debug_bfd
, &stash
->debug_sections
[debug_info
],
5417 &stash
->f
.dwarf_info_buffer
, &total_size
))
5422 /* Case 2: multiple sections. */
5423 for (total_size
= 0;
5425 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
5427 /* Catch PR25070 testcase overflowing size calculation here. */
5428 if (total_size
+ msec
->size
< total_size
5429 || total_size
+ msec
->size
< msec
->size
)
5431 bfd_set_error (bfd_error_no_memory
);
5434 total_size
+= msec
->size
;
5437 stash
->f
.dwarf_info_buffer
= (bfd_byte
*) bfd_malloc (total_size
);
5438 if (stash
->f
.dwarf_info_buffer
== NULL
)
5442 for (msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
5444 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
5452 if (!(bfd_simple_get_relocated_section_contents
5453 (debug_bfd
, msec
, stash
->f
.dwarf_info_buffer
+ total_size
,
5461 stash
->f
.info_ptr
= stash
->f
.dwarf_info_buffer
;
5462 stash
->f
.dwarf_info_size
= total_size
;
5466 /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
5468 static struct comp_unit
*
5469 stash_comp_unit (struct dwarf2_debug
*stash
, struct dwarf2_debug_file
*file
)
5471 bfd_size_type length
;
5472 unsigned int offset_size
;
5473 bfd_byte
*info_ptr_unit
= file
->info_ptr
;
5474 bfd_byte
*info_ptr_end
= file
->dwarf_info_buffer
+ file
->dwarf_info_size
;
5476 if (file
->info_ptr
>= info_ptr_end
)
5479 length
= read_4_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
5480 /* A 0xffffff length is the DWARF3 way of indicating
5481 we use 64-bit offsets, instead of 32-bit offsets. */
5482 if (length
== 0xffffffff)
5485 length
= read_8_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
5487 /* A zero length is the IRIX way of indicating 64-bit offsets,
5488 mostly because the 64-bit length will generally fit in 32
5489 bits, and the endianness helps. */
5490 else if (length
== 0)
5493 length
= read_4_bytes (file
->bfd_ptr
, &file
->info_ptr
, info_ptr_end
);
5495 /* In the absence of the hints above, we assume 32-bit DWARF2
5496 offsets even for targets with 64-bit addresses, because:
5497 a) most of the time these targets will not have generated
5498 more than 2Gb of debug info and so will not need 64-bit
5501 b) if they do use 64-bit offsets but they are not using
5502 the size hints that are tested for above then they are
5503 not conforming to the DWARF3 standard anyway. */
5508 && length
<= (size_t) (info_ptr_end
- file
->info_ptr
))
5510 struct comp_unit
*each
= parse_comp_unit (stash
, file
,
5511 file
->info_ptr
, length
,
5512 info_ptr_unit
, offset_size
);
5515 if (file
->all_comp_units
)
5516 file
->all_comp_units
->prev_unit
= each
;
5518 file
->last_comp_unit
= each
;
5520 each
->next_unit
= file
->all_comp_units
;
5521 file
->all_comp_units
= each
;
5523 if (each
->arange
.high
== 0)
5525 each
->next_unit_without_ranges
= file
->all_comp_units_without_ranges
;
5526 file
->all_comp_units_without_ranges
= each
->next_unit_without_ranges
;
5529 file
->info_ptr
+= length
;
5534 /* Don't trust any of the DWARF info after a corrupted length or
5536 file
->info_ptr
= info_ptr_end
;
5540 /* Hash function for an asymbol. */
5543 hash_asymbol (const void *sym
)
5545 const asymbol
*asym
= sym
;
5546 return htab_hash_string (asym
->name
);
5549 /* Equality function for asymbols. */
5552 eq_asymbol (const void *a
, const void *b
)
5554 const asymbol
*sa
= a
;
5555 const asymbol
*sb
= b
;
5556 return strcmp (sa
->name
, sb
->name
) == 0;
5559 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
5560 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
5561 symbol in SYMBOLS and return the difference between the low_pc and
5562 the symbol's address. Returns 0 if no suitable symbol could be found. */
5565 _bfd_dwarf2_find_symbol_bias (asymbol
** symbols
, void ** pinfo
)
5567 struct dwarf2_debug
*stash
;
5568 struct comp_unit
* unit
;
5570 bfd_signed_vma result
= 0;
5573 stash
= (struct dwarf2_debug
*) *pinfo
;
5575 if (stash
== NULL
|| symbols
== NULL
)
5578 sym_hash
= htab_create_alloc (10, hash_asymbol
, eq_asymbol
,
5579 NULL
, xcalloc
, free
);
5580 for (psym
= symbols
; * psym
!= NULL
; psym
++)
5582 asymbol
* sym
= * psym
;
5584 if (sym
->flags
& BSF_FUNCTION
&& sym
->section
!= NULL
)
5586 void **slot
= htab_find_slot (sym_hash
, sym
, INSERT
);
5591 for (unit
= stash
->f
.all_comp_units
; unit
; unit
= unit
->next_unit
)
5593 struct funcinfo
* func
;
5595 comp_unit_maybe_decode_line_info (unit
);
5597 for (func
= unit
->function_table
; func
!= NULL
; func
= func
->prev_func
)
5598 if (func
->name
&& func
->arange
.low
)
5600 asymbol search
, *sym
;
5602 /* FIXME: Do we need to scan the aranges looking for the
5605 search
.name
= func
->name
;
5606 sym
= htab_find (sym_hash
, &search
);
5609 result
= func
->arange
.low
- (sym
->value
+ sym
->section
->vma
);
5616 htab_delete (sym_hash
);
5620 /* Find the source code location of SYMBOL. If SYMBOL is NULL
5621 then find the nearest source code location corresponding to
5622 the address SECTION + OFFSET.
5623 Returns 1 if the line is found without error and fills in
5624 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
5625 NULL the FUNCTIONNAME_PTR is also filled in.
5626 Returns 2 if partial information from _bfd_elf_find_function is
5627 returned (function and maybe file) by looking at symbols. DWARF2
5628 info is present but not regarding the requested code location.
5629 Returns 0 otherwise.
5630 SYMBOLS contains the symbol table for ABFD.
5631 DEBUG_SECTIONS contains the name of the dwarf debug sections. */
5634 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
5639 const char **filename_ptr
,
5640 const char **functionname_ptr
,
5641 unsigned int *linenumber_ptr
,
5642 unsigned int *discriminator_ptr
,
5643 const struct dwarf_debug_section
*debug_sections
,
5646 /* Read each compilation unit from the section .debug_info, and check
5647 to see if it contains the address we are searching for. If yes,
5648 lookup the address, and return the line number info. If no, go
5649 on to the next compilation unit.
5651 We keep a list of all the previously read compilation units, and
5652 a pointer to the next un-read compilation unit. Check the
5653 previously read units before reading more. */
5654 struct dwarf2_debug
*stash
;
5655 /* What address are we looking for? */
5657 struct comp_unit
* each
;
5658 struct funcinfo
*function
= NULL
;
5662 *filename_ptr
= NULL
;
5663 if (functionname_ptr
!= NULL
)
5664 *functionname_ptr
= NULL
;
5665 *linenumber_ptr
= 0;
5666 if (discriminator_ptr
)
5667 *discriminator_ptr
= 0;
5669 if (! _bfd_dwarf2_slurp_debug_info (abfd
, NULL
, debug_sections
,
5671 (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0))
5674 stash
= (struct dwarf2_debug
*) *pinfo
;
5676 do_line
= symbol
!= NULL
;
5679 BFD_ASSERT (section
== NULL
&& offset
== 0 && functionname_ptr
== NULL
);
5680 section
= bfd_asymbol_section (symbol
);
5681 addr
= symbol
->value
;
5685 BFD_ASSERT (section
!= NULL
&& functionname_ptr
!= NULL
);
5688 /* If we have no SYMBOL but the section we're looking at is not a
5689 code section, then take a look through the list of symbols to see
5690 if we have a symbol at the address we're looking for. If we do
5691 then use this to look up line information. This will allow us to
5692 give file and line results for data symbols. We exclude code
5693 symbols here, if we look up a function symbol and then look up the
5694 line information we'll actually return the line number for the
5695 opening '{' rather than the function definition line. This is
5696 because looking up by symbol uses the line table, in which the
5697 first line for a function is usually the opening '{', while
5698 looking up the function by section + offset uses the
5699 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
5700 which will be the line of the function name. */
5701 if (symbols
!= NULL
&& (section
->flags
& SEC_CODE
) == 0)
5705 for (tmp
= symbols
; (*tmp
) != NULL
; ++tmp
)
5706 if ((*tmp
)->the_bfd
== abfd
5707 && (*tmp
)->section
== section
5708 && (*tmp
)->value
== offset
5709 && ((*tmp
)->flags
& BSF_SECTION_SYM
) == 0)
5713 /* For local symbols, keep going in the hope we find a
5715 if ((symbol
->flags
& BSF_GLOBAL
) != 0)
5721 if (section
->output_section
)
5722 addr
+= section
->output_section
->vma
+ section
->output_offset
;
5724 addr
+= section
->vma
;
5726 /* A null info_ptr indicates that there is no dwarf2 info
5727 (or that an error occured while setting up the stash). */
5728 if (! stash
->f
.info_ptr
)
5731 stash
->inliner_chain
= NULL
;
5733 /* Check the previously read comp. units first. */
5736 /* The info hash tables use quite a bit of memory. We may not want to
5737 always use them. We use some heuristics to decide if and when to
5739 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
5740 stash_maybe_enable_info_hash_tables (abfd
, stash
);
5742 /* Keep info hash table up to date if they are available. Note that we
5743 may disable the hash tables if there is any error duing update. */
5744 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
5745 stash_maybe_update_info_hash_tables (stash
);
5747 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
5749 found
= stash_find_line_fast (stash
, symbol
, addr
, filename_ptr
,
5756 /* Check the previously read comp. units first. */
5757 for (each
= stash
->f
.all_comp_units
; each
; each
= each
->next_unit
)
5758 if ((symbol
->flags
& BSF_FUNCTION
) == 0
5759 || each
->arange
.high
== 0
5760 || comp_unit_contains_address (each
, addr
))
5762 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
5771 struct trie_node
*trie
= stash
->f
.trie_root
;
5772 unsigned int bits
= VMA_BITS
- 8;
5773 struct comp_unit
**prev_each
;
5775 /* Traverse interior nodes until we get to a leaf. */
5776 while (trie
&& trie
->num_room_in_leaf
== 0)
5778 int ch
= (addr
>> bits
) & 0xff;
5779 trie
= ((struct trie_interior
*) trie
)->children
[ch
];
5785 const struct trie_leaf
*leaf
= (struct trie_leaf
*) trie
;
5788 for (i
= 0; i
< leaf
->num_stored_in_leaf
; ++i
)
5789 leaf
->ranges
[i
].unit
->mark
= false;
5791 for (i
= 0; i
< leaf
->num_stored_in_leaf
; ++i
)
5793 struct comp_unit
*unit
= leaf
->ranges
[i
].unit
;
5795 || addr
< leaf
->ranges
[i
].low_pc
5796 || addr
>= leaf
->ranges
[i
].high_pc
)
5800 found
= comp_unit_find_nearest_line (unit
, addr
,
5810 /* Also scan through all compilation units without any ranges,
5811 taking them out of the list if they have acquired any since
5813 prev_each
= &stash
->f
.all_comp_units_without_ranges
;
5814 for (each
= *prev_each
; each
; each
= each
->next_unit_without_ranges
)
5816 if (each
->arange
.high
!= 0)
5818 *prev_each
= each
->next_unit_without_ranges
;
5822 found
= comp_unit_find_nearest_line (each
, addr
,
5829 prev_each
= &each
->next_unit_without_ranges
;
5833 /* Read each remaining comp. units checking each as they are read. */
5834 while ((each
= stash_comp_unit (stash
, &stash
->f
)) != NULL
)
5836 /* DW_AT_low_pc and DW_AT_high_pc are optional for
5837 compilation units. If we don't have them (i.e.,
5838 unit->high == 0), we need to consult the line info table
5839 to see if a compilation unit contains the given
5842 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
5843 || each
->arange
.high
== 0
5844 || comp_unit_contains_address (each
, addr
))
5845 && comp_unit_find_line (each
, symbol
, addr
,
5846 filename_ptr
, linenumber_ptr
));
5848 found
= ((each
->arange
.high
== 0
5849 || comp_unit_contains_address (each
, addr
))
5850 && comp_unit_find_nearest_line (each
, addr
,
5854 discriminator_ptr
));
5861 if (functionname_ptr
&& function
&& function
->is_linkage
)
5863 *functionname_ptr
= function
->name
;
5867 else if (functionname_ptr
5868 && (!*functionname_ptr
5869 || (function
&& !function
->is_linkage
)))
5872 asymbol
**syms
= symbols
;
5873 asection
*sec
= section
;
5875 _bfd_dwarf2_stash_syms (stash
, abfd
, &sec
, &syms
);
5876 fun
= _bfd_elf_find_function (abfd
, syms
, sec
, offset
,
5877 *filename_ptr
? NULL
: filename_ptr
,
5880 if (!found
&& fun
!= NULL
)
5883 if (function
&& !function
->is_linkage
)
5887 sec_vma
= section
->vma
;
5888 if (section
->output_section
!= NULL
)
5889 sec_vma
= section
->output_section
->vma
+ section
->output_offset
;
5891 *functionname_ptr
= function
->name
;
5892 else if (fun
->value
+ sec_vma
== function
->arange
.low
)
5893 function
->name
= *functionname_ptr
;
5894 /* Even if we didn't find a linkage name, say that we have
5895 to stop a repeated search of symbols. */
5896 function
->is_linkage
= true;
5900 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
5901 unset_sections (stash
);
5907 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
5908 const char **filename_ptr
,
5909 const char **functionname_ptr
,
5910 unsigned int *linenumber_ptr
,
5913 struct dwarf2_debug
*stash
;
5915 stash
= (struct dwarf2_debug
*) *pinfo
;
5918 struct funcinfo
*func
= stash
->inliner_chain
;
5920 if (func
&& func
->caller_func
)
5922 *filename_ptr
= func
->caller_file
;
5923 *functionname_ptr
= func
->caller_func
->name
;
5924 *linenumber_ptr
= func
->caller_line
;
5925 stash
->inliner_chain
= func
->caller_func
;
5934 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
, void **pinfo
)
5936 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
5937 struct comp_unit
*each
;
5938 struct dwarf2_debug_file
*file
;
5940 if (abfd
== NULL
|| stash
== NULL
)
5943 if (stash
->varinfo_hash_table
)
5944 bfd_hash_table_free (&stash
->varinfo_hash_table
->base
);
5945 if (stash
->funcinfo_hash_table
)
5946 bfd_hash_table_free (&stash
->funcinfo_hash_table
->base
);
5951 for (each
= file
->all_comp_units
; each
; each
= each
->next_unit
)
5953 struct funcinfo
*function_table
= each
->function_table
;
5954 struct varinfo
*variable_table
= each
->variable_table
;
5956 if (each
->line_table
&& each
->line_table
!= file
->line_table
)
5958 free (each
->line_table
->files
);
5959 free (each
->line_table
->dirs
);
5962 free (each
->lookup_funcinfo_table
);
5963 each
->lookup_funcinfo_table
= NULL
;
5965 while (function_table
)
5967 free (function_table
->file
);
5968 function_table
->file
= NULL
;
5969 free (function_table
->caller_file
);
5970 function_table
->caller_file
= NULL
;
5971 function_table
= function_table
->prev_func
;
5974 while (variable_table
)
5976 free (variable_table
->file
);
5977 variable_table
->file
= NULL
;
5978 variable_table
= variable_table
->prev_var
;
5982 if (file
->line_table
)
5984 free (file
->line_table
->files
);
5985 free (file
->line_table
->dirs
);
5987 htab_delete (file
->abbrev_offsets
);
5989 free (file
->dwarf_line_str_buffer
);
5990 free (file
->dwarf_str_buffer
);
5991 free (file
->dwarf_ranges_buffer
);
5992 free (file
->dwarf_line_buffer
);
5993 free (file
->dwarf_abbrev_buffer
);
5994 free (file
->dwarf_info_buffer
);
5995 if (file
== &stash
->alt
)
5999 free (stash
->sec_vma
);
6000 free (stash
->adjusted_sections
);
6001 if (stash
->close_on_cleanup
)
6002 bfd_close (stash
->f
.bfd_ptr
);
6003 if (stash
->alt
.bfd_ptr
)
6004 bfd_close (stash
->alt
.bfd_ptr
);
6007 /* Find the function to a particular section and offset,
6008 for error reporting. */
6011 _bfd_elf_find_function (bfd
*abfd
,
6015 const char **filename_ptr
,
6016 const char **functionname_ptr
)
6018 struct elf_find_function_cache
6020 asection
*last_section
;
6022 const char *filename
;
6023 bfd_size_type func_size
;
6026 if (symbols
== NULL
)
6029 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6032 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
6035 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
6036 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
6040 if (cache
->last_section
!= section
6041 || cache
->func
== NULL
6042 || offset
< cache
->func
->value
6043 || offset
>= cache
->func
->value
+ cache
->func_size
)
6048 /* ??? Given multiple file symbols, it is impossible to reliably
6049 choose the right file name for global symbols. File symbols are
6050 local symbols, and thus all file symbols must sort before any
6051 global symbols. The ELF spec may be interpreted to say that a
6052 file symbol must sort before other local symbols, but currently
6053 ld -r doesn't do this. So, for ld -r output, it is possible to
6054 make a better choice of file name for local symbols by ignoring
6055 file symbols appearing after a given local symbol. */
6056 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
6057 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6061 state
= nothing_seen
;
6062 cache
->filename
= NULL
;
6064 cache
->func_size
= 0;
6065 cache
->last_section
= section
;
6067 for (p
= symbols
; *p
!= NULL
; p
++)
6073 if ((sym
->flags
& BSF_FILE
) != 0)
6076 if (state
== symbol_seen
)
6077 state
= file_after_symbol_seen
;
6081 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
6083 && code_off
<= offset
6084 && (code_off
> low_func
6085 || (code_off
== low_func
6086 && size
> cache
->func_size
)))
6089 cache
->func_size
= size
;
6090 cache
->filename
= NULL
;
6091 low_func
= code_off
;
6093 && ((sym
->flags
& BSF_LOCAL
) != 0
6094 || state
!= file_after_symbol_seen
))
6095 cache
->filename
= bfd_asymbol_name (file
);
6097 if (state
== nothing_seen
)
6098 state
= symbol_seen
;
6102 if (cache
->func
== NULL
)
6106 *filename_ptr
= cache
->filename
;
6107 if (functionname_ptr
)
6108 *functionname_ptr
= bfd_asymbol_name (cache
->func
);