2 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
7 From the dwarf2read.c header:
8 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
9 Inc. with support from Florida State University (under contract
10 with the Ada Joint Program Office), and Silicon Graphics, Inc.
11 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
12 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
13 support in dwarfread.c
15 This file is part of BFD.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 3 of the License, or (at
20 your option) any later version.
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
30 MA 02110-1301, USA. */
34 #include "libiberty.h"
40 /* The data in the .debug_line statement prologue looks like this. */
45 unsigned short version
;
46 bfd_vma prologue_length
;
47 unsigned char minimum_instruction_length
;
48 unsigned char maximum_ops_per_insn
;
49 unsigned char default_is_stmt
;
51 unsigned char line_range
;
52 unsigned char opcode_base
;
53 unsigned char *standard_opcode_lengths
;
56 /* Attributes have a name and a value. */
60 enum dwarf_attribute name
;
65 struct dwarf_block
*blk
;
72 /* Blocks are a bunch of untyped bytes. */
79 struct adjusted_section
85 struct dwarf2_debug_file
87 /* The actual bfd from which debug info was loaded. Might be
88 different to orig_bfd because of gnu_debuglink sections. */
91 /* Pointer to the symbol table. */
94 /* The current info pointer for the .debug_info section being parsed. */
97 /* A pointer to the memory block allocated for .debug_info sections. */
98 bfd_byte
*dwarf_info_buffer
;
100 /* Length of the loaded .debug_info sections. */
101 bfd_size_type dwarf_info_size
;
103 /* Pointer to the .debug_abbrev section loaded into memory. */
104 bfd_byte
*dwarf_abbrev_buffer
;
106 /* Length of the loaded .debug_abbrev section. */
107 bfd_size_type dwarf_abbrev_size
;
109 /* Buffer for decode_line_info. */
110 bfd_byte
*dwarf_line_buffer
;
112 /* Length of the loaded .debug_line section. */
113 bfd_size_type dwarf_line_size
;
115 /* Pointer to the .debug_str section loaded into memory. */
116 bfd_byte
*dwarf_str_buffer
;
118 /* Length of the loaded .debug_str section. */
119 bfd_size_type dwarf_str_size
;
121 /* Pointer to the .debug_line_str section loaded into memory. */
122 bfd_byte
*dwarf_line_str_buffer
;
124 /* Length of the loaded .debug_line_str section. */
125 bfd_size_type dwarf_line_str_size
;
127 /* Pointer to the .debug_ranges section loaded into memory. */
128 bfd_byte
*dwarf_ranges_buffer
;
130 /* Length of the loaded .debug_ranges section. */
131 bfd_size_type dwarf_ranges_size
;
133 /* Pointer to the .debug_rnglists section loaded into memory. */
134 bfd_byte
*dwarf_rnglists_buffer
;
136 /* Length of the loaded .debug_rnglists section. */
137 bfd_size_type dwarf_rnglists_size
;
139 /* A list of all previously read comp_units. */
140 struct comp_unit
*all_comp_units
;
142 /* Last comp unit in list above. */
143 struct comp_unit
*last_comp_unit
;
145 /* Line table at line_offset zero. */
146 struct line_info_table
*line_table
;
148 /* Hash table to map offsets to decoded abbrevs. */
149 htab_t abbrev_offsets
;
154 /* Names of the debug sections. */
155 const struct dwarf_debug_section
*debug_sections
;
157 /* Per-file stuff. */
158 struct dwarf2_debug_file f
, alt
;
160 /* Pointer to the original bfd for which debug was loaded. This is what
161 we use to compare and so check that the cached debug data is still
162 valid - it saves having to possibly dereference the gnu_debuglink each
166 /* If the most recent call to bfd_find_nearest_line was given an
167 address in an inlined function, preserve a pointer into the
168 calling chain for subsequent calls to bfd_find_inliner_info to
170 struct funcinfo
*inliner_chain
;
172 /* Section VMAs at the time the stash was built. */
174 /* Number of sections in the SEC_VMA table. */
175 unsigned int sec_vma_count
;
177 /* Number of sections whose VMA we must adjust. */
178 int adjusted_section_count
;
180 /* Array of sections with adjusted VMA. */
181 struct adjusted_section
*adjusted_sections
;
183 /* Number of times find_line is called. This is used in
184 the heuristic for enabling the info hash tables. */
187 #define STASH_INFO_HASH_TRIGGER 100
189 /* Hash table mapping symbol names to function infos. */
190 struct info_hash_table
*funcinfo_hash_table
;
192 /* Hash table mapping symbol names to variable infos. */
193 struct info_hash_table
*varinfo_hash_table
;
195 /* Head of comp_unit list in the last hash table update. */
196 struct comp_unit
*hash_units_head
;
198 /* Status of info hash. */
199 int info_hash_status
;
200 #define STASH_INFO_HASH_OFF 0
201 #define STASH_INFO_HASH_ON 1
202 #define STASH_INFO_HASH_DISABLED 2
204 /* True if we opened bfd_ptr. */
205 bfd_boolean close_on_cleanup
;
215 /* A minimal decoding of DWARF2 compilation units. We only decode
216 what's needed to get to the line number information. */
220 /* Chain the previously read compilation units. */
221 struct comp_unit
*next_unit
;
223 /* Likewise, chain the compilation unit read after this one.
224 The comp units are stored in reversed reading order. */
225 struct comp_unit
*prev_unit
;
227 /* Keep the bfd convenient (for memory allocation). */
230 /* The lowest and highest addresses contained in this compilation
231 unit as specified in the compilation unit header. */
232 struct arange arange
;
234 /* The DW_AT_name attribute (for error messages). */
237 /* The abbrev hash table. */
238 struct abbrev_info
**abbrevs
;
240 /* DW_AT_language. */
243 /* Note that an error was found by comp_unit_find_nearest_line. */
246 /* The DW_AT_comp_dir attribute. */
249 /* TRUE if there is a line number table associated with this comp. unit. */
252 /* Pointer to the current comp_unit so that we can find a given entry
254 bfd_byte
*info_ptr_unit
;
256 /* The offset into .debug_line of the line number table. */
257 unsigned long line_offset
;
259 /* Pointer to the first child die for the comp unit. */
260 bfd_byte
*first_child_die_ptr
;
262 /* The end of the comp unit. */
265 /* The decoded line number, NULL if not yet decoded. */
266 struct line_info_table
*line_table
;
268 /* A list of the functions found in this comp. unit. */
269 struct funcinfo
*function_table
;
271 /* A table of function information references searchable by address. */
272 struct lookup_funcinfo
*lookup_funcinfo_table
;
274 /* Number of functions in the function_table and sorted_function_table. */
275 bfd_size_type number_of_functions
;
277 /* A list of the variables found in this comp. unit. */
278 struct varinfo
*variable_table
;
280 /* Pointers to dwarf2_debug structures. */
281 struct dwarf2_debug
*stash
;
282 struct dwarf2_debug_file
*file
;
284 /* DWARF format version for this unit - from unit header. */
287 /* Address size for this unit - from unit header. */
288 unsigned char addr_size
;
290 /* Offset size for this unit - from unit header. */
291 unsigned char offset_size
;
293 /* Base address for this unit - from DW_AT_low_pc attribute of
294 DW_TAG_compile_unit DIE */
295 bfd_vma base_address
;
297 /* TRUE if symbols are cached in hash table for faster lookup by name. */
301 /* This data structure holds the information of an abbrev. */
304 unsigned int number
; /* Number identifying abbrev. */
305 enum dwarf_tag tag
; /* DWARF tag. */
306 bfd_boolean has_children
; /* TRUE if the abbrev has children. */
307 unsigned int num_attrs
; /* Number of attributes. */
308 struct attr_abbrev
* attrs
; /* An array of attribute descriptions. */
309 struct abbrev_info
* next
; /* Next in chain. */
314 enum dwarf_attribute name
;
315 enum dwarf_form form
;
316 bfd_vma implicit_const
;
319 /* Map of uncompressed DWARF debug section name to compressed one. It
320 is terminated by NULL uncompressed_name. */
322 const struct dwarf_debug_section dwarf_debug_sections
[] =
324 { ".debug_abbrev", ".zdebug_abbrev" },
325 { ".debug_aranges", ".zdebug_aranges" },
326 { ".debug_frame", ".zdebug_frame" },
327 { ".debug_info", ".zdebug_info" },
328 { ".debug_info", ".zdebug_info" },
329 { ".debug_line", ".zdebug_line" },
330 { ".debug_loc", ".zdebug_loc" },
331 { ".debug_macinfo", ".zdebug_macinfo" },
332 { ".debug_macro", ".zdebug_macro" },
333 { ".debug_pubnames", ".zdebug_pubnames" },
334 { ".debug_pubtypes", ".zdebug_pubtypes" },
335 { ".debug_ranges", ".zdebug_ranges" },
336 { ".debug_rnglists", ".zdebug_rnglist" },
337 { ".debug_static_func", ".zdebug_static_func" },
338 { ".debug_static_vars", ".zdebug_static_vars" },
339 { ".debug_str", ".zdebug_str", },
340 { ".debug_str", ".zdebug_str", },
341 { ".debug_line_str", ".zdebug_line_str", },
342 { ".debug_types", ".zdebug_types" },
343 /* GNU DWARF 1 extensions */
344 { ".debug_sfnames", ".zdebug_sfnames" },
345 { ".debug_srcinfo", ".zebug_srcinfo" },
346 /* SGI/MIPS DWARF 2 extensions */
347 { ".debug_funcnames", ".zdebug_funcnames" },
348 { ".debug_typenames", ".zdebug_typenames" },
349 { ".debug_varnames", ".zdebug_varnames" },
350 { ".debug_weaknames", ".zdebug_weaknames" },
354 /* NB/ Numbers in this enum must match up with indices
355 into the dwarf_debug_sections[] array above. */
356 enum dwarf_debug_section_enum
386 /* A static assertion. */
387 extern int dwarf_debug_section_assert
[ARRAY_SIZE (dwarf_debug_sections
)
388 == debug_max
+ 1 ? 1 : -1];
390 #ifndef ABBREV_HASH_SIZE
391 #define ABBREV_HASH_SIZE 121
393 #ifndef ATTR_ALLOC_CHUNK
394 #define ATTR_ALLOC_CHUNK 4
397 /* Variable and function hash tables. This is used to speed up look-up
398 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
399 In order to share code between variable and function infos, we use
400 a list of untyped pointer for all variable/function info associated with
401 a symbol. We waste a bit of memory for list with one node but that
402 simplifies the code. */
404 struct info_list_node
406 struct info_list_node
*next
;
410 /* Info hash entry. */
411 struct info_hash_entry
413 struct bfd_hash_entry root
;
414 struct info_list_node
*head
;
417 struct info_hash_table
419 struct bfd_hash_table base
;
422 /* Function to create a new entry in info hash table. */
424 static struct bfd_hash_entry
*
425 info_hash_table_newfunc (struct bfd_hash_entry
*entry
,
426 struct bfd_hash_table
*table
,
429 struct info_hash_entry
*ret
= (struct info_hash_entry
*) entry
;
431 /* Allocate the structure if it has not already been allocated by a
435 ret
= (struct info_hash_entry
*) bfd_hash_allocate (table
,
441 /* Call the allocation method of the base class. */
442 ret
= ((struct info_hash_entry
*)
443 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
445 /* Initialize the local fields here. */
449 return (struct bfd_hash_entry
*) ret
;
452 /* Function to create a new info hash table. It returns a pointer to the
453 newly created table or NULL if there is any error. We need abfd
454 solely for memory allocation. */
456 static struct info_hash_table
*
457 create_info_hash_table (bfd
*abfd
)
459 struct info_hash_table
*hash_table
;
461 hash_table
= ((struct info_hash_table
*)
462 bfd_alloc (abfd
, sizeof (struct info_hash_table
)));
466 if (!bfd_hash_table_init (&hash_table
->base
, info_hash_table_newfunc
,
467 sizeof (struct info_hash_entry
)))
469 bfd_release (abfd
, hash_table
);
476 /* Insert an info entry into an info hash table. We do not check of
477 duplicate entries. Also, the caller need to guarantee that the
478 right type of info in inserted as info is passed as a void* pointer.
479 This function returns true if there is no error. */
482 insert_info_hash_table (struct info_hash_table
*hash_table
,
487 struct info_hash_entry
*entry
;
488 struct info_list_node
*node
;
490 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
,
495 node
= (struct info_list_node
*) bfd_hash_allocate (&hash_table
->base
,
501 node
->next
= entry
->head
;
507 /* Look up an info entry list from an info hash table. Return NULL
510 static struct info_list_node
*
511 lookup_info_hash_table (struct info_hash_table
*hash_table
, const char *key
)
513 struct info_hash_entry
*entry
;
515 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
, key
,
517 return entry
? entry
->head
: NULL
;
520 /* Read a section into its appropriate place in the dwarf2_debug
521 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
522 not NULL, use bfd_simple_get_relocated_section_contents to read the
523 section contents, otherwise use bfd_get_section_contents. Fail if
524 the located section does not contain at least OFFSET bytes. */
527 read_section (bfd
* abfd
,
528 const struct dwarf_debug_section
*sec
,
531 bfd_byte
** section_buffer
,
532 bfd_size_type
* section_size
)
534 const char *section_name
= sec
->uncompressed_name
;
535 bfd_byte
*contents
= *section_buffer
;
537 /* The section may have already been read. */
538 if (contents
== NULL
)
544 msec
= bfd_get_section_by_name (abfd
, section_name
);
547 section_name
= sec
->compressed_name
;
548 if (section_name
!= NULL
)
549 msec
= bfd_get_section_by_name (abfd
, section_name
);
553 _bfd_error_handler (_("DWARF error: can't find %s section."),
554 sec
->uncompressed_name
);
555 bfd_set_error (bfd_error_bad_value
);
559 amt
= bfd_get_section_limit_octets (abfd
, msec
);
560 filesize
= bfd_get_file_size (abfd
);
564 _bfd_error_handler (_("DWARF error: section %s is larger than its filesize! (0x%lx vs 0x%lx)"),
565 section_name
, (long) amt
, (long) filesize
);
566 bfd_set_error (bfd_error_bad_value
);
570 /* Paranoia - alloc one extra so that we can make sure a string
571 section is NUL terminated. */
575 /* Paranoia - this should never happen. */
576 bfd_set_error (bfd_error_no_memory
);
579 contents
= (bfd_byte
*) bfd_malloc (amt
);
580 if (contents
== NULL
)
583 ? !bfd_simple_get_relocated_section_contents (abfd
, msec
, contents
,
585 : !bfd_get_section_contents (abfd
, msec
, contents
, 0, *section_size
))
590 contents
[*section_size
] = 0;
591 *section_buffer
= contents
;
594 /* It is possible to get a bad value for the offset into the section
595 that the client wants. Validate it here to avoid trouble later. */
596 if (offset
!= 0 && offset
>= *section_size
)
598 /* xgettext: c-format */
599 _bfd_error_handler (_("DWARF error: offset (%" PRIu64
")"
600 " greater than or equal to %s size (%" PRIu64
")"),
601 (uint64_t) offset
, section_name
,
602 (uint64_t) *section_size
);
603 bfd_set_error (bfd_error_bad_value
);
610 /* Read dwarf information from a buffer. */
613 read_1_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
, bfd_byte
*end
)
617 return bfd_get_8 (abfd
, buf
);
621 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
, bfd_byte
*end
)
625 return bfd_get_signed_8 (abfd
, buf
);
629 read_2_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
633 return bfd_get_16 (abfd
, buf
);
637 read_4_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
641 return bfd_get_32 (abfd
, buf
);
645 read_8_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
649 return bfd_get_64 (abfd
, buf
);
653 read_n_bytes (bfd_byte
* buf
,
655 struct dwarf_block
* block
)
657 unsigned int size
= block
->size
;
658 bfd_byte
* block_end
= buf
+ size
;
660 if (block_end
> end
|| block_end
< buf
)
673 /* Scans a NUL terminated string starting at BUF, returning a pointer to it.
674 Returns the number of characters in the string, *including* the NUL byte,
675 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
676 at or beyond BUF_END will not be read. Returns NULL if there was a
677 problem, or if the string is empty. */
680 read_string (bfd
* abfd ATTRIBUTE_UNUSED
,
683 unsigned int * bytes_read_ptr
)
689 * bytes_read_ptr
= 0;
695 * bytes_read_ptr
= 1;
699 while (buf
< buf_end
)
702 * bytes_read_ptr
= buf
- str
;
706 * bytes_read_ptr
= buf
- str
;
710 /* Reads an offset from BUF and then locates the string at this offset
711 inside the debug string section. Returns a pointer to the string.
712 Returns the number of bytes read from BUF, *not* the length of the string,
713 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
714 at or beyond BUF_END will not be read from BUF. Returns NULL if there was
715 a problem, or if the string is empty. Does not check for NUL termination
719 read_indirect_string (struct comp_unit
* unit
,
722 unsigned int * bytes_read_ptr
)
725 struct dwarf2_debug
*stash
= unit
->stash
;
726 struct dwarf2_debug_file
*file
= unit
->file
;
729 if (buf
+ unit
->offset_size
> buf_end
)
731 * bytes_read_ptr
= 0;
735 if (unit
->offset_size
== 4)
736 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
738 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
740 *bytes_read_ptr
= unit
->offset_size
;
742 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
744 &file
->dwarf_str_buffer
, &file
->dwarf_str_size
))
747 str
= (char *) file
->dwarf_str_buffer
+ offset
;
753 /* Like read_indirect_string but from .debug_line_str section. */
756 read_indirect_line_string (struct comp_unit
* unit
,
759 unsigned int * bytes_read_ptr
)
762 struct dwarf2_debug
*stash
= unit
->stash
;
763 struct dwarf2_debug_file
*file
= unit
->file
;
766 if (buf
+ unit
->offset_size
> buf_end
)
768 * bytes_read_ptr
= 0;
772 if (unit
->offset_size
== 4)
773 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
775 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
777 *bytes_read_ptr
= unit
->offset_size
;
779 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_line_str
],
781 &file
->dwarf_line_str_buffer
,
782 &file
->dwarf_line_str_size
))
785 str
= (char *) file
->dwarf_line_str_buffer
+ offset
;
791 /* Like read_indirect_string but uses a .debug_str located in
792 an alternate file pointed to by the .gnu_debugaltlink section.
793 Used to impement DW_FORM_GNU_strp_alt. */
796 read_alt_indirect_string (struct comp_unit
* unit
,
799 unsigned int * bytes_read_ptr
)
802 struct dwarf2_debug
*stash
= unit
->stash
;
805 if (buf
+ unit
->offset_size
> buf_end
)
807 * bytes_read_ptr
= 0;
811 if (unit
->offset_size
== 4)
812 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
814 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
816 *bytes_read_ptr
= unit
->offset_size
;
818 if (stash
->alt
.bfd_ptr
== NULL
)
821 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
823 if (debug_filename
== NULL
)
826 debug_bfd
= bfd_openr (debug_filename
, NULL
);
827 free (debug_filename
);
828 if (debug_bfd
== NULL
)
829 /* FIXME: Should we report our failure to follow the debuglink ? */
832 if (!bfd_check_format (debug_bfd
, bfd_object
))
834 bfd_close (debug_bfd
);
837 stash
->alt
.bfd_ptr
= debug_bfd
;
840 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
841 stash
->debug_sections
+ debug_str_alt
,
842 stash
->alt
.syms
, offset
,
843 &stash
->alt
.dwarf_str_buffer
,
844 &stash
->alt
.dwarf_str_size
))
847 str
= (char *) stash
->alt
.dwarf_str_buffer
+ offset
;
854 /* Resolve an alternate reference from UNIT at OFFSET.
855 Returns a pointer into the loaded alternate CU upon success
856 or NULL upon failure. */
859 read_alt_indirect_ref (struct comp_unit
* unit
,
862 struct dwarf2_debug
*stash
= unit
->stash
;
864 if (stash
->alt
.bfd_ptr
== NULL
)
867 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
869 if (debug_filename
== NULL
)
872 debug_bfd
= bfd_openr (debug_filename
, NULL
);
873 free (debug_filename
);
874 if (debug_bfd
== NULL
)
875 /* FIXME: Should we report our failure to follow the debuglink ? */
878 if (!bfd_check_format (debug_bfd
, bfd_object
))
880 bfd_close (debug_bfd
);
883 stash
->alt
.bfd_ptr
= debug_bfd
;
886 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
887 stash
->debug_sections
+ debug_info_alt
,
888 stash
->alt
.syms
, offset
,
889 &stash
->alt
.dwarf_info_buffer
,
890 &stash
->alt
.dwarf_info_size
))
893 return stash
->alt
.dwarf_info_buffer
+ offset
;
897 read_address (struct comp_unit
*unit
, bfd_byte
*buf
, bfd_byte
* buf_end
)
901 if (bfd_get_flavour (unit
->abfd
) == bfd_target_elf_flavour
)
902 signed_vma
= get_elf_backend_data (unit
->abfd
)->sign_extend_vma
;
904 if (buf
+ unit
->addr_size
> buf_end
)
909 switch (unit
->addr_size
)
912 return bfd_get_signed_64 (unit
->abfd
, buf
);
914 return bfd_get_signed_32 (unit
->abfd
, buf
);
916 return bfd_get_signed_16 (unit
->abfd
, buf
);
923 switch (unit
->addr_size
)
926 return bfd_get_64 (unit
->abfd
, buf
);
928 return bfd_get_32 (unit
->abfd
, buf
);
930 return bfd_get_16 (unit
->abfd
, buf
);
937 /* Lookup an abbrev_info structure in the abbrev hash table. */
939 static struct abbrev_info
*
940 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
942 unsigned int hash_number
;
943 struct abbrev_info
*abbrev
;
945 hash_number
= number
% ABBREV_HASH_SIZE
;
946 abbrev
= abbrevs
[hash_number
];
950 if (abbrev
->number
== number
)
953 abbrev
= abbrev
->next
;
959 /* We keep a hash table to map .debug_abbrev section offsets to the
960 array of abbrevs, so that compilation units using the same set of
961 abbrevs do not waste memory. */
963 struct abbrev_offset_entry
966 struct abbrev_info
**abbrevs
;
970 hash_abbrev (const void *p
)
972 const struct abbrev_offset_entry
*ent
= p
;
973 return htab_hash_pointer ((void *) ent
->offset
);
977 eq_abbrev (const void *pa
, const void *pb
)
979 const struct abbrev_offset_entry
*a
= pa
;
980 const struct abbrev_offset_entry
*b
= pb
;
981 return a
->offset
== b
->offset
;
987 struct abbrev_offset_entry
*ent
= p
;
988 struct abbrev_info
**abbrevs
= ent
->abbrevs
;
991 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
993 struct abbrev_info
*abbrev
= abbrevs
[i
];
997 free (abbrev
->attrs
);
998 abbrev
= abbrev
->next
;
1004 /* In DWARF version 2, the description of the debugging information is
1005 stored in a separate .debug_abbrev section. Before we read any
1006 dies from a section we read in all abbreviations and install them
1009 static struct abbrev_info
**
1010 read_abbrevs (bfd
*abfd
, bfd_uint64_t offset
, struct dwarf2_debug
*stash
,
1011 struct dwarf2_debug_file
*file
)
1013 struct abbrev_info
**abbrevs
;
1014 bfd_byte
*abbrev_ptr
;
1015 bfd_byte
*abbrev_end
;
1016 struct abbrev_info
*cur_abbrev
;
1017 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
1018 unsigned int abbrev_form
, hash_number
;
1021 struct abbrev_offset_entry ent
= { offset
, NULL
};
1023 if (ent
.offset
!= offset
)
1026 slot
= htab_find_slot (file
->abbrev_offsets
, &ent
, INSERT
);
1030 return ((struct abbrev_offset_entry
*) (*slot
))->abbrevs
;
1032 if (! read_section (abfd
, &stash
->debug_sections
[debug_abbrev
],
1034 &file
->dwarf_abbrev_buffer
,
1035 &file
->dwarf_abbrev_size
))
1038 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
1039 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
1040 if (abbrevs
== NULL
)
1043 abbrev_ptr
= file
->dwarf_abbrev_buffer
+ offset
;
1044 abbrev_end
= file
->dwarf_abbrev_buffer
+ file
->dwarf_abbrev_size
;
1045 abbrev_number
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1047 abbrev_ptr
+= bytes_read
;
1049 /* Loop until we reach an abbrev number of 0. */
1050 while (abbrev_number
)
1052 amt
= sizeof (struct abbrev_info
);
1053 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
1054 if (cur_abbrev
== NULL
)
1057 /* Read in abbrev header. */
1058 cur_abbrev
->number
= abbrev_number
;
1059 cur_abbrev
->tag
= (enum dwarf_tag
)
1060 _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1062 abbrev_ptr
+= bytes_read
;
1063 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
, abbrev_end
);
1066 /* Now read in declarations. */
1069 /* Initialize it just to avoid a GCC false warning. */
1070 bfd_vma implicit_const
= -1;
1072 abbrev_name
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1074 abbrev_ptr
+= bytes_read
;
1075 abbrev_form
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1077 abbrev_ptr
+= bytes_read
;
1078 if (abbrev_form
== DW_FORM_implicit_const
)
1080 implicit_const
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
,
1083 abbrev_ptr
+= bytes_read
;
1086 if (abbrev_name
== 0)
1089 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
1091 struct attr_abbrev
*tmp
;
1093 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
1094 amt
*= sizeof (struct attr_abbrev
);
1095 tmp
= (struct attr_abbrev
*) bfd_realloc (cur_abbrev
->attrs
, amt
);
1098 cur_abbrev
->attrs
= tmp
;
1101 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
1102 = (enum dwarf_attribute
) abbrev_name
;
1103 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].form
1104 = (enum dwarf_form
) abbrev_form
;
1105 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].implicit_const
1107 ++cur_abbrev
->num_attrs
;
1110 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
1111 cur_abbrev
->next
= abbrevs
[hash_number
];
1112 abbrevs
[hash_number
] = cur_abbrev
;
1114 /* Get next abbreviation.
1115 Under Irix6 the abbreviations for a compilation unit are not
1116 always properly terminated with an abbrev number of 0.
1117 Exit loop if we encounter an abbreviation which we have
1118 already read (which means we are about to read the abbreviations
1119 for the next compile unit) or if the end of the abbreviation
1120 table is reached. */
1121 if ((size_t) (abbrev_ptr
- file
->dwarf_abbrev_buffer
)
1122 >= file
->dwarf_abbrev_size
)
1124 abbrev_number
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
,
1125 &bytes_read
, FALSE
, abbrev_end
);
1126 abbrev_ptr
+= bytes_read
;
1127 if (lookup_abbrev (abbrev_number
, abbrevs
) != NULL
)
1131 *slot
= bfd_malloc (sizeof ent
);
1134 ent
.abbrevs
= abbrevs
;
1135 memcpy (*slot
, &ent
, sizeof ent
);
1139 if (abbrevs
!= NULL
)
1143 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1145 struct abbrev_info
*abbrev
= abbrevs
[i
];
1149 free (abbrev
->attrs
);
1150 abbrev
= abbrev
->next
;
1158 /* Returns true if the form is one which has a string value. */
1160 static inline bfd_boolean
1161 is_str_attr (enum dwarf_form form
)
1163 return (form
== DW_FORM_string
|| form
== DW_FORM_strp
1164 || form
== DW_FORM_line_strp
|| form
== DW_FORM_GNU_strp_alt
);
1167 /* Read and fill in the value of attribute ATTR as described by FORM.
1168 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1169 Returns an updated INFO_PTR taking into account the amount of data read. */
1172 read_attribute_value (struct attribute
* attr
,
1174 bfd_vma implicit_const
,
1175 struct comp_unit
* unit
,
1176 bfd_byte
* info_ptr
,
1177 bfd_byte
* info_ptr_end
)
1179 bfd
*abfd
= unit
->abfd
;
1180 unsigned int bytes_read
;
1181 struct dwarf_block
*blk
;
1184 if (info_ptr
>= info_ptr_end
&& form
!= DW_FORM_flag_present
)
1186 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1187 bfd_set_error (bfd_error_bad_value
);
1191 attr
->form
= (enum dwarf_form
) form
;
1195 case DW_FORM_ref_addr
:
1196 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1198 if (unit
->version
== 3 || unit
->version
== 4)
1200 if (unit
->offset_size
== 4)
1201 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1203 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1204 info_ptr
+= unit
->offset_size
;
1209 attr
->u
.val
= read_address (unit
, info_ptr
, info_ptr_end
);
1210 info_ptr
+= unit
->addr_size
;
1212 case DW_FORM_GNU_ref_alt
:
1213 case DW_FORM_sec_offset
:
1214 if (unit
->offset_size
== 4)
1215 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1217 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1218 info_ptr
+= unit
->offset_size
;
1220 case DW_FORM_block2
:
1221 amt
= sizeof (struct dwarf_block
);
1222 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1225 blk
->size
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1227 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1230 case DW_FORM_block4
:
1231 amt
= sizeof (struct dwarf_block
);
1232 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1235 blk
->size
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1237 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1241 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1245 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1249 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1252 case DW_FORM_string
:
1253 attr
->u
.str
= read_string (abfd
, info_ptr
, info_ptr_end
, &bytes_read
);
1254 info_ptr
+= bytes_read
;
1257 attr
->u
.str
= read_indirect_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1258 info_ptr
+= bytes_read
;
1260 case DW_FORM_line_strp
:
1261 attr
->u
.str
= read_indirect_line_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1262 info_ptr
+= bytes_read
;
1264 case DW_FORM_GNU_strp_alt
:
1265 attr
->u
.str
= read_alt_indirect_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1266 info_ptr
+= bytes_read
;
1268 case DW_FORM_exprloc
:
1270 amt
= sizeof (struct dwarf_block
);
1271 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1274 blk
->size
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1275 FALSE
, info_ptr_end
);
1276 info_ptr
+= bytes_read
;
1277 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1280 case DW_FORM_block1
:
1281 amt
= sizeof (struct dwarf_block
);
1282 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1285 blk
->size
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1287 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1291 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1295 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1298 case DW_FORM_flag_present
:
1302 attr
->u
.sval
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1303 TRUE
, info_ptr_end
);
1304 info_ptr
+= bytes_read
;
1307 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1308 FALSE
, info_ptr_end
);
1309 info_ptr
+= bytes_read
;
1312 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1316 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1320 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1324 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1327 case DW_FORM_ref_sig8
:
1328 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1331 case DW_FORM_ref_udata
:
1332 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1333 FALSE
, info_ptr_end
);
1334 info_ptr
+= bytes_read
;
1336 case DW_FORM_indirect
:
1337 form
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1338 FALSE
, info_ptr_end
);
1339 info_ptr
+= bytes_read
;
1340 if (form
== DW_FORM_implicit_const
)
1342 implicit_const
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1343 TRUE
, info_ptr_end
);
1344 info_ptr
+= bytes_read
;
1346 info_ptr
= read_attribute_value (attr
, form
, implicit_const
, unit
,
1347 info_ptr
, info_ptr_end
);
1349 case DW_FORM_implicit_const
:
1350 attr
->form
= DW_FORM_sdata
;
1351 attr
->u
.sval
= implicit_const
;
1353 case DW_FORM_data16
:
1354 /* This is really a "constant", but there is no way to store that
1355 so pretend it is a 16 byte block instead. */
1356 amt
= sizeof (struct dwarf_block
);
1357 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1361 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1365 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1367 bfd_set_error (bfd_error_bad_value
);
1373 /* Read an attribute described by an abbreviated attribute. */
1376 read_attribute (struct attribute
* attr
,
1377 struct attr_abbrev
* abbrev
,
1378 struct comp_unit
* unit
,
1379 bfd_byte
* info_ptr
,
1380 bfd_byte
* info_ptr_end
)
1382 attr
->name
= abbrev
->name
;
1383 info_ptr
= read_attribute_value (attr
, abbrev
->form
, abbrev
->implicit_const
,
1384 unit
, info_ptr
, info_ptr_end
);
1388 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1392 non_mangled (int lang
)
1402 case DW_LANG_Cobol74
:
1403 case DW_LANG_Cobol85
:
1404 case DW_LANG_Fortran77
:
1405 case DW_LANG_Pascal83
:
1415 /* Source line information table routines. */
1417 #define FILE_ALLOC_CHUNK 5
1418 #define DIR_ALLOC_CHUNK 5
1422 struct line_info
* prev_line
;
1426 unsigned int column
;
1427 unsigned int discriminator
;
1428 unsigned char op_index
;
1429 unsigned char end_sequence
; /* End of (sequential) code sequence. */
1440 struct line_sequence
1443 struct line_sequence
* prev_sequence
;
1444 struct line_info
* last_line
; /* Largest VMA. */
1445 struct line_info
** line_info_lookup
;
1446 bfd_size_type num_lines
;
1449 struct line_info_table
1452 unsigned int num_files
;
1453 unsigned int num_dirs
;
1454 unsigned int num_sequences
;
1457 struct fileinfo
* files
;
1458 struct line_sequence
* sequences
;
1459 struct line_info
* lcl_head
; /* Local head; used in 'add_line_info'. */
1462 /* Remember some information about each function. If the function is
1463 inlined (DW_TAG_inlined_subroutine) it may have two additional
1464 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1465 source code location where this function was inlined. */
1469 /* Pointer to previous function in list of all functions. */
1470 struct funcinfo
* prev_func
;
1471 /* Pointer to function one scope higher. */
1472 struct funcinfo
* caller_func
;
1473 /* Source location file name where caller_func inlines this func. */
1475 /* Source location file name. */
1477 /* Source location line number where caller_func inlines this func. */
1479 /* Source location line number. */
1482 bfd_boolean is_linkage
;
1484 struct arange arange
;
1485 /* Where the symbol is defined. */
1489 struct lookup_funcinfo
1491 /* Function information corresponding to this lookup table entry. */
1492 struct funcinfo
* funcinfo
;
1494 /* The lowest address for this specific function. */
1497 /* The highest address of this function before the lookup table is sorted.
1498 The highest address of all prior functions after the lookup table is
1499 sorted, which is used for binary search. */
1501 /* Index of this function, used to ensure qsort is stable. */
1507 /* Pointer to previous variable in list of all variables. */
1508 struct varinfo
*prev_var
;
1509 /* The offset of the varinfo from the start of the unit. */
1510 bfd_uint64_t unit_offset
;
1511 /* Source location file name. */
1513 /* Source location line number. */
1515 /* The type of this variable. */
1517 /* The name of the variable, if it has one. */
1519 /* The address of the variable. */
1521 /* Where the symbol is defined. */
1523 /* Is this a stack variable? */
1527 /* Return TRUE if NEW_LINE should sort after LINE. */
1529 static inline bfd_boolean
1530 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
1532 return (new_line
->address
> line
->address
1533 || (new_line
->address
== line
->address
1534 && new_line
->op_index
> line
->op_index
));
1538 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1539 that the list is sorted. Note that the line_info list is sorted from
1540 highest to lowest VMA (with possible duplicates); that is,
1541 line_info->prev_line always accesses an equal or smaller VMA. */
1544 add_line_info (struct line_info_table
*table
,
1546 unsigned char op_index
,
1549 unsigned int column
,
1550 unsigned int discriminator
,
1553 size_t amt
= sizeof (struct line_info
);
1554 struct line_sequence
* seq
= table
->sequences
;
1555 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
1560 /* Set member data of 'info'. */
1561 info
->prev_line
= NULL
;
1562 info
->address
= address
;
1563 info
->op_index
= op_index
;
1565 info
->column
= column
;
1566 info
->discriminator
= discriminator
;
1567 info
->end_sequence
= end_sequence
;
1569 if (filename
&& filename
[0])
1571 info
->filename
= (char *) bfd_alloc (table
->abfd
, strlen (filename
) + 1);
1572 if (info
->filename
== NULL
)
1574 strcpy (info
->filename
, filename
);
1577 info
->filename
= NULL
;
1579 /* Find the correct location for 'info'. Normally we will receive
1580 new line_info data 1) in order and 2) with increasing VMAs.
1581 However some compilers break the rules (cf. decode_line_info) and
1582 so we include some heuristics for quickly finding the correct
1583 location for 'info'. In particular, these heuristics optimize for
1584 the common case in which the VMA sequence that we receive is a
1585 list of locally sorted VMAs such as
1586 p...z a...j (where a < j < p < z)
1588 Note: table->lcl_head is used to head an *actual* or *possible*
1589 sub-sequence within the list (such as a...j) that is not directly
1590 headed by table->last_line
1592 Note: we may receive duplicate entries from 'decode_line_info'. */
1595 && seq
->last_line
->address
== address
1596 && seq
->last_line
->op_index
== op_index
1597 && seq
->last_line
->end_sequence
== end_sequence
)
1599 /* We only keep the last entry with the same address and end
1600 sequence. See PR ld/4986. */
1601 if (table
->lcl_head
== seq
->last_line
)
1602 table
->lcl_head
= info
;
1603 info
->prev_line
= seq
->last_line
->prev_line
;
1604 seq
->last_line
= info
;
1606 else if (!seq
|| seq
->last_line
->end_sequence
)
1608 /* Start a new line sequence. */
1609 amt
= sizeof (struct line_sequence
);
1610 seq
= (struct line_sequence
*) bfd_malloc (amt
);
1613 seq
->low_pc
= address
;
1614 seq
->prev_sequence
= table
->sequences
;
1615 seq
->last_line
= info
;
1616 table
->lcl_head
= info
;
1617 table
->sequences
= seq
;
1618 table
->num_sequences
++;
1620 else if (info
->end_sequence
1621 || new_line_sorts_after (info
, seq
->last_line
))
1623 /* Normal case: add 'info' to the beginning of the current sequence. */
1624 info
->prev_line
= seq
->last_line
;
1625 seq
->last_line
= info
;
1627 /* lcl_head: initialize to head a *possible* sequence at the end. */
1628 if (!table
->lcl_head
)
1629 table
->lcl_head
= info
;
1631 else if (!new_line_sorts_after (info
, table
->lcl_head
)
1632 && (!table
->lcl_head
->prev_line
1633 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
1635 /* Abnormal but easy: lcl_head is the head of 'info'. */
1636 info
->prev_line
= table
->lcl_head
->prev_line
;
1637 table
->lcl_head
->prev_line
= info
;
1641 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1642 are valid heads for 'info'. Reset 'lcl_head'. */
1643 struct line_info
* li2
= seq
->last_line
; /* Always non-NULL. */
1644 struct line_info
* li1
= li2
->prev_line
;
1648 if (!new_line_sorts_after (info
, li2
)
1649 && new_line_sorts_after (info
, li1
))
1652 li2
= li1
; /* always non-NULL */
1653 li1
= li1
->prev_line
;
1655 table
->lcl_head
= li2
;
1656 info
->prev_line
= table
->lcl_head
->prev_line
;
1657 table
->lcl_head
->prev_line
= info
;
1658 if (address
< seq
->low_pc
)
1659 seq
->low_pc
= address
;
1664 /* Extract a fully qualified filename from a line info table.
1665 The returned string has been malloc'ed and it is the caller's
1666 responsibility to free it. */
1669 concat_filename (struct line_info_table
*table
, unsigned int file
)
1673 if (table
== NULL
|| file
- 1 >= table
->num_files
)
1675 /* FILE == 0 means unknown. */
1678 (_("DWARF error: mangled line number section (bad file number)"));
1679 return strdup ("<unknown>");
1682 filename
= table
->files
[file
- 1].name
;
1683 if (filename
== NULL
)
1684 return strdup ("<unknown>");
1686 if (!IS_ABSOLUTE_PATH (filename
))
1688 char *dir_name
= NULL
;
1689 char *subdir_name
= NULL
;
1693 if (table
->files
[file
- 1].dir
1694 /* PR 17512: file: 0317e960. */
1695 && table
->files
[file
- 1].dir
<= table
->num_dirs
1696 /* PR 17512: file: 7f3d2e4b. */
1697 && table
->dirs
!= NULL
)
1698 subdir_name
= table
->dirs
[table
->files
[file
- 1].dir
- 1];
1700 if (!subdir_name
|| !IS_ABSOLUTE_PATH (subdir_name
))
1701 dir_name
= table
->comp_dir
;
1705 dir_name
= subdir_name
;
1710 return strdup (filename
);
1712 len
= strlen (dir_name
) + strlen (filename
) + 2;
1716 len
+= strlen (subdir_name
) + 1;
1717 name
= (char *) bfd_malloc (len
);
1719 sprintf (name
, "%s/%s/%s", dir_name
, subdir_name
, filename
);
1723 name
= (char *) bfd_malloc (len
);
1725 sprintf (name
, "%s/%s", dir_name
, filename
);
1731 return strdup (filename
);
1735 arange_add (const struct comp_unit
*unit
, struct arange
*first_arange
,
1736 bfd_vma low_pc
, bfd_vma high_pc
)
1738 struct arange
*arange
;
1740 /* Ignore empty ranges. */
1741 if (low_pc
== high_pc
)
1744 /* If the first arange is empty, use it. */
1745 if (first_arange
->high
== 0)
1747 first_arange
->low
= low_pc
;
1748 first_arange
->high
= high_pc
;
1752 /* Next see if we can cheaply extend an existing range. */
1753 arange
= first_arange
;
1756 if (low_pc
== arange
->high
)
1758 arange
->high
= high_pc
;
1761 if (high_pc
== arange
->low
)
1763 arange
->low
= low_pc
;
1766 arange
= arange
->next
;
1770 /* Need to allocate a new arange and insert it into the arange list.
1771 Order isn't significant, so just insert after the first arange. */
1772 arange
= (struct arange
*) bfd_alloc (unit
->abfd
, sizeof (*arange
));
1775 arange
->low
= low_pc
;
1776 arange
->high
= high_pc
;
1777 arange
->next
= first_arange
->next
;
1778 first_arange
->next
= arange
;
1782 /* Compare function for line sequences. */
1785 compare_sequences (const void* a
, const void* b
)
1787 const struct line_sequence
* seq1
= a
;
1788 const struct line_sequence
* seq2
= b
;
1790 /* Sort by low_pc as the primary key. */
1791 if (seq1
->low_pc
< seq2
->low_pc
)
1793 if (seq1
->low_pc
> seq2
->low_pc
)
1796 /* If low_pc values are equal, sort in reverse order of
1797 high_pc, so that the largest region comes first. */
1798 if (seq1
->last_line
->address
< seq2
->last_line
->address
)
1800 if (seq1
->last_line
->address
> seq2
->last_line
->address
)
1803 if (seq1
->last_line
->op_index
< seq2
->last_line
->op_index
)
1805 if (seq1
->last_line
->op_index
> seq2
->last_line
->op_index
)
1808 /* num_lines is initially an index, to make the sort stable. */
1809 if (seq1
->num_lines
< seq2
->num_lines
)
1811 if (seq1
->num_lines
> seq2
->num_lines
)
1816 /* Construct the line information table for quick lookup. */
1819 build_line_info_table (struct line_info_table
* table
,
1820 struct line_sequence
* seq
)
1823 struct line_info
**line_info_lookup
;
1824 struct line_info
*each_line
;
1825 unsigned int num_lines
;
1826 unsigned int line_index
;
1828 if (seq
->line_info_lookup
!= NULL
)
1831 /* Count the number of line information entries. We could do this while
1832 scanning the debug information, but some entries may be added via
1833 lcl_head without having a sequence handy to increment the number of
1836 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
1839 seq
->num_lines
= num_lines
;
1843 /* Allocate space for the line information lookup table. */
1844 amt
= sizeof (struct line_info
*) * num_lines
;
1845 line_info_lookup
= (struct line_info
**) bfd_alloc (table
->abfd
, amt
);
1846 seq
->line_info_lookup
= line_info_lookup
;
1847 if (line_info_lookup
== NULL
)
1850 /* Create the line information lookup table. */
1851 line_index
= num_lines
;
1852 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
1853 line_info_lookup
[--line_index
] = each_line
;
1855 BFD_ASSERT (line_index
== 0);
1859 /* Sort the line sequences for quick lookup. */
1862 sort_line_sequences (struct line_info_table
* table
)
1865 struct line_sequence
*sequences
;
1866 struct line_sequence
*seq
;
1868 unsigned int num_sequences
= table
->num_sequences
;
1869 bfd_vma last_high_pc
;
1871 if (num_sequences
== 0)
1874 /* Allocate space for an array of sequences. */
1875 amt
= sizeof (struct line_sequence
) * num_sequences
;
1876 sequences
= (struct line_sequence
*) bfd_alloc (table
->abfd
, amt
);
1877 if (sequences
== NULL
)
1880 /* Copy the linked list into the array, freeing the original nodes. */
1881 seq
= table
->sequences
;
1882 for (n
= 0; n
< num_sequences
; n
++)
1884 struct line_sequence
* last_seq
= seq
;
1887 sequences
[n
].low_pc
= seq
->low_pc
;
1888 sequences
[n
].prev_sequence
= NULL
;
1889 sequences
[n
].last_line
= seq
->last_line
;
1890 sequences
[n
].line_info_lookup
= NULL
;
1891 sequences
[n
].num_lines
= n
;
1892 seq
= seq
->prev_sequence
;
1895 BFD_ASSERT (seq
== NULL
);
1897 qsort (sequences
, n
, sizeof (struct line_sequence
), compare_sequences
);
1899 /* Make the list binary-searchable by trimming overlapping entries
1900 and removing nested entries. */
1902 last_high_pc
= sequences
[0].last_line
->address
;
1903 for (n
= 1; n
< table
->num_sequences
; n
++)
1905 if (sequences
[n
].low_pc
< last_high_pc
)
1907 if (sequences
[n
].last_line
->address
<= last_high_pc
)
1908 /* Skip nested entries. */
1911 /* Trim overlapping entries. */
1912 sequences
[n
].low_pc
= last_high_pc
;
1914 last_high_pc
= sequences
[n
].last_line
->address
;
1915 if (n
> num_sequences
)
1917 /* Close up the gap. */
1918 sequences
[num_sequences
].low_pc
= sequences
[n
].low_pc
;
1919 sequences
[num_sequences
].last_line
= sequences
[n
].last_line
;
1924 table
->sequences
= sequences
;
1925 table
->num_sequences
= num_sequences
;
1929 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
1932 line_info_add_include_dir (struct line_info_table
*table
, char *cur_dir
)
1934 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1939 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1940 amt
*= sizeof (char *);
1942 tmp
= (char **) bfd_realloc (table
->dirs
, amt
);
1948 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1953 line_info_add_include_dir_stub (struct line_info_table
*table
, char *cur_dir
,
1954 unsigned int dir ATTRIBUTE_UNUSED
,
1955 unsigned int xtime ATTRIBUTE_UNUSED
,
1956 unsigned int size ATTRIBUTE_UNUSED
)
1958 return line_info_add_include_dir (table
, cur_dir
);
1961 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
1964 line_info_add_file_name (struct line_info_table
*table
, char *cur_file
,
1965 unsigned int dir
, unsigned int xtime
,
1968 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1970 struct fileinfo
*tmp
;
1973 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1974 amt
*= sizeof (struct fileinfo
);
1976 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1982 table
->files
[table
->num_files
].name
= cur_file
;
1983 table
->files
[table
->num_files
].dir
= dir
;
1984 table
->files
[table
->num_files
].time
= xtime
;
1985 table
->files
[table
->num_files
].size
= size
;
1990 /* Read directory or file name entry format, starting with byte of
1991 format count entries, ULEB128 pairs of entry formats, ULEB128 of
1992 entries count and the entries themselves in the described entry
1996 read_formatted_entries (struct comp_unit
*unit
, bfd_byte
**bufp
,
1997 bfd_byte
*buf_end
, struct line_info_table
*table
,
1998 bfd_boolean (*callback
) (struct line_info_table
*table
,
2004 bfd
*abfd
= unit
->abfd
;
2005 bfd_byte format_count
, formati
;
2006 bfd_vma data_count
, datai
;
2007 bfd_byte
*buf
= *bufp
;
2008 bfd_byte
*format_header_data
;
2009 unsigned int bytes_read
;
2011 format_count
= read_1_byte (abfd
, buf
, buf_end
);
2013 format_header_data
= buf
;
2014 for (formati
= 0; formati
< format_count
; formati
++)
2016 _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
2018 _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
2022 data_count
= _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
2024 if (format_count
== 0 && data_count
!= 0)
2026 _bfd_error_handler (_("DWARF error: zero format count"));
2027 bfd_set_error (bfd_error_bad_value
);
2031 /* PR 22210. Paranoia check. Don't bother running the loop
2032 if we know that we are going to run out of buffer. */
2033 if (data_count
> (bfd_vma
) (buf_end
- buf
))
2036 (_("DWARF error: data count (%" PRIx64
") larger than buffer size"),
2037 (uint64_t) data_count
);
2038 bfd_set_error (bfd_error_bad_value
);
2042 for (datai
= 0; datai
< data_count
; datai
++)
2044 bfd_byte
*format
= format_header_data
;
2047 memset (&fe
, 0, sizeof fe
);
2048 for (formati
= 0; formati
< format_count
; formati
++)
2050 bfd_vma content_type
, form
;
2052 char **stringp
= &string_trash
;
2053 unsigned int uint_trash
, *uintp
= &uint_trash
;
2054 struct attribute attr
;
2056 content_type
= _bfd_safe_read_leb128 (abfd
, format
, &bytes_read
,
2058 format
+= bytes_read
;
2059 switch (content_type
)
2064 case DW_LNCT_directory_index
:
2067 case DW_LNCT_timestamp
:
2077 (_("DWARF error: unknown format content type %" PRIu64
),
2078 (uint64_t) content_type
);
2079 bfd_set_error (bfd_error_bad_value
);
2083 form
= _bfd_safe_read_leb128 (abfd
, format
, &bytes_read
, FALSE
,
2085 format
+= bytes_read
;
2087 buf
= read_attribute_value (&attr
, form
, 0, unit
, buf
, buf_end
);
2092 case DW_FORM_string
:
2093 case DW_FORM_line_strp
:
2094 *stringp
= attr
.u
.str
;
2102 *uintp
= attr
.u
.val
;
2105 case DW_FORM_data16
:
2106 /* MD5 data is in the attr.blk, but we are ignoring those. */
2111 /* Skip the first "zero entry", which is the compilation dir/file. */
2113 if (!callback (table
, fe
.name
, fe
.dir
, fe
.time
, fe
.size
))
2121 /* Decode the line number information for UNIT. */
2123 static struct line_info_table
*
2124 decode_line_info (struct comp_unit
*unit
)
2126 bfd
*abfd
= unit
->abfd
;
2127 struct dwarf2_debug
*stash
= unit
->stash
;
2128 struct dwarf2_debug_file
*file
= unit
->file
;
2129 struct line_info_table
* table
;
2132 struct line_head lh
;
2133 unsigned int i
, bytes_read
, offset_size
;
2134 char *cur_file
, *cur_dir
;
2135 unsigned char op_code
, extended_op
, adj_opcode
;
2136 unsigned int exop_len
;
2139 if (unit
->line_offset
== 0 && file
->line_table
)
2140 return file
->line_table
;
2142 if (! read_section (abfd
, &stash
->debug_sections
[debug_line
],
2143 file
->syms
, unit
->line_offset
,
2144 &file
->dwarf_line_buffer
, &file
->dwarf_line_size
))
2147 if (file
->dwarf_line_size
< 16)
2150 (_("DWARF error: line info section is too small (%" PRId64
")"),
2151 (int64_t) file
->dwarf_line_size
);
2152 bfd_set_error (bfd_error_bad_value
);
2155 line_ptr
= file
->dwarf_line_buffer
+ unit
->line_offset
;
2156 line_end
= file
->dwarf_line_buffer
+ file
->dwarf_line_size
;
2158 /* Read in the prologue. */
2159 lh
.total_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2162 if (lh
.total_length
== 0xffffffff)
2164 lh
.total_length
= read_8_bytes (abfd
, line_ptr
, line_end
);
2168 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
2170 /* Handle (non-standard) 64-bit DWARF2 formats. */
2171 lh
.total_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2176 if (lh
.total_length
> (size_t) (line_end
- line_ptr
))
2179 /* xgettext: c-format */
2180 (_("DWARF error: line info data is bigger (%#" PRIx64
")"
2181 " than the space remaining in the section (%#lx)"),
2182 (uint64_t) lh
.total_length
, (unsigned long) (line_end
- line_ptr
));
2183 bfd_set_error (bfd_error_bad_value
);
2187 line_end
= line_ptr
+ lh
.total_length
;
2189 lh
.version
= read_2_bytes (abfd
, line_ptr
, line_end
);
2190 if (lh
.version
< 2 || lh
.version
> 5)
2193 (_("DWARF error: unhandled .debug_line version %d"), lh
.version
);
2194 bfd_set_error (bfd_error_bad_value
);
2199 if (line_ptr
+ offset_size
+ (lh
.version
>= 5 ? 8 : (lh
.version
>= 4 ? 6 : 5))
2203 (_("DWARF error: ran out of room reading prologue"));
2204 bfd_set_error (bfd_error_bad_value
);
2208 if (lh
.version
>= 5)
2210 unsigned int segment_selector_size
;
2212 /* Skip address size. */
2213 read_1_byte (abfd
, line_ptr
, line_end
);
2216 segment_selector_size
= read_1_byte (abfd
, line_ptr
, line_end
);
2218 if (segment_selector_size
!= 0)
2221 (_("DWARF error: line info unsupported segment selector size %u"),
2222 segment_selector_size
);
2223 bfd_set_error (bfd_error_bad_value
);
2228 if (offset_size
== 4)
2229 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2231 lh
.prologue_length
= read_8_bytes (abfd
, line_ptr
, line_end
);
2232 line_ptr
+= offset_size
;
2234 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
, line_end
);
2237 if (lh
.version
>= 4)
2239 lh
.maximum_ops_per_insn
= read_1_byte (abfd
, line_ptr
, line_end
);
2243 lh
.maximum_ops_per_insn
= 1;
2245 if (lh
.maximum_ops_per_insn
== 0)
2248 (_("DWARF error: invalid maximum operations per instruction"));
2249 bfd_set_error (bfd_error_bad_value
);
2253 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
, line_end
);
2256 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
, line_end
);
2259 lh
.line_range
= read_1_byte (abfd
, line_ptr
, line_end
);
2262 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
, line_end
);
2265 if (line_ptr
+ (lh
.opcode_base
- 1) >= line_end
)
2267 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2268 bfd_set_error (bfd_error_bad_value
);
2272 amt
= lh
.opcode_base
* sizeof (unsigned char);
2273 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
2275 lh
.standard_opcode_lengths
[0] = 1;
2277 for (i
= 1; i
< lh
.opcode_base
; ++i
)
2279 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
, line_end
);
2283 amt
= sizeof (struct line_info_table
);
2284 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
2288 table
->comp_dir
= unit
->comp_dir
;
2290 table
->num_files
= 0;
2291 table
->files
= NULL
;
2293 table
->num_dirs
= 0;
2296 table
->num_sequences
= 0;
2297 table
->sequences
= NULL
;
2299 table
->lcl_head
= NULL
;
2301 if (lh
.version
>= 5)
2303 /* Read directory table. */
2304 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2305 line_info_add_include_dir_stub
))
2308 /* Read file name table. */
2309 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2310 line_info_add_file_name
))
2315 /* Read directory table. */
2316 while ((cur_dir
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
)) != NULL
)
2318 line_ptr
+= bytes_read
;
2320 if (!line_info_add_include_dir (table
, cur_dir
))
2324 line_ptr
+= bytes_read
;
2326 /* Read file name table. */
2327 while ((cur_file
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
)) != NULL
)
2329 unsigned int dir
, xtime
, size
;
2331 line_ptr
+= bytes_read
;
2333 dir
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2334 line_ptr
+= bytes_read
;
2335 xtime
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2336 line_ptr
+= bytes_read
;
2337 size
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2338 line_ptr
+= bytes_read
;
2340 if (!line_info_add_file_name (table
, cur_file
, dir
, xtime
, size
))
2344 line_ptr
+= bytes_read
;
2347 /* Read the statement sequences until there's nothing left. */
2348 while (line_ptr
< line_end
)
2350 /* State machine registers. */
2351 bfd_vma address
= 0;
2352 unsigned char op_index
= 0;
2353 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
2354 unsigned int line
= 1;
2355 unsigned int column
= 0;
2356 unsigned int discriminator
= 0;
2357 int is_stmt
= lh
.default_is_stmt
;
2358 int end_sequence
= 0;
2359 unsigned int dir
, xtime
, size
;
2360 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2361 compilers generate address sequences that are wildly out of
2362 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2363 for ia64-Linux). Thus, to determine the low and high
2364 address, we must compare on every DW_LNS_copy, etc. */
2365 bfd_vma low_pc
= (bfd_vma
) -1;
2366 bfd_vma high_pc
= 0;
2368 /* Decode the table. */
2369 while (!end_sequence
&& line_ptr
< line_end
)
2371 op_code
= read_1_byte (abfd
, line_ptr
, line_end
);
2374 if (op_code
>= lh
.opcode_base
)
2376 /* Special operand. */
2377 adj_opcode
= op_code
- lh
.opcode_base
;
2378 if (lh
.line_range
== 0)
2380 if (lh
.maximum_ops_per_insn
== 1)
2381 address
+= (adj_opcode
/ lh
.line_range
2382 * lh
.minimum_instruction_length
);
2385 address
+= ((op_index
+ adj_opcode
/ lh
.line_range
)
2386 / lh
.maximum_ops_per_insn
2387 * lh
.minimum_instruction_length
);
2388 op_index
= ((op_index
+ adj_opcode
/ lh
.line_range
)
2389 % lh
.maximum_ops_per_insn
);
2391 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
2392 /* Append row to matrix using current values. */
2393 if (!add_line_info (table
, address
, op_index
, filename
,
2394 line
, column
, discriminator
, 0))
2397 if (address
< low_pc
)
2399 if (address
> high_pc
)
2402 else switch (op_code
)
2404 case DW_LNS_extended_op
:
2405 exop_len
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2407 line_ptr
+= bytes_read
;
2408 extended_op
= read_1_byte (abfd
, line_ptr
, line_end
);
2411 switch (extended_op
)
2413 case DW_LNE_end_sequence
:
2415 if (!add_line_info (table
, address
, op_index
, filename
, line
,
2416 column
, discriminator
, end_sequence
))
2419 if (address
< low_pc
)
2421 if (address
> high_pc
)
2423 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
2426 case DW_LNE_set_address
:
2427 address
= read_address (unit
, line_ptr
, line_end
);
2429 line_ptr
+= unit
->addr_size
;
2431 case DW_LNE_define_file
:
2432 cur_file
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
);
2433 line_ptr
+= bytes_read
;
2434 dir
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2436 line_ptr
+= bytes_read
;
2437 xtime
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2439 line_ptr
+= bytes_read
;
2440 size
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2442 line_ptr
+= bytes_read
;
2443 if (!line_info_add_file_name (table
, cur_file
, dir
,
2447 case DW_LNE_set_discriminator
:
2449 _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2451 line_ptr
+= bytes_read
;
2453 case DW_LNE_HP_source_file_correlation
:
2454 line_ptr
+= exop_len
- 1;
2458 (_("DWARF error: mangled line number section"));
2459 bfd_set_error (bfd_error_bad_value
);
2466 if (!add_line_info (table
, address
, op_index
,
2467 filename
, line
, column
, discriminator
, 0))
2470 if (address
< low_pc
)
2472 if (address
> high_pc
)
2475 case DW_LNS_advance_pc
:
2476 if (lh
.maximum_ops_per_insn
== 1)
2477 address
+= (lh
.minimum_instruction_length
2478 * _bfd_safe_read_leb128 (abfd
, line_ptr
,
2483 bfd_vma adjust
= _bfd_safe_read_leb128 (abfd
, line_ptr
,
2486 address
= ((op_index
+ adjust
) / lh
.maximum_ops_per_insn
2487 * lh
.minimum_instruction_length
);
2488 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2490 line_ptr
+= bytes_read
;
2492 case DW_LNS_advance_line
:
2493 line
+= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2495 line_ptr
+= bytes_read
;
2497 case DW_LNS_set_file
:
2499 unsigned int filenum
;
2501 /* The file and directory tables are 0
2502 based, the references are 1 based. */
2503 filenum
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2505 line_ptr
+= bytes_read
;
2507 filename
= concat_filename (table
, filenum
);
2510 case DW_LNS_set_column
:
2511 column
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2513 line_ptr
+= bytes_read
;
2515 case DW_LNS_negate_stmt
:
2516 is_stmt
= (!is_stmt
);
2518 case DW_LNS_set_basic_block
:
2520 case DW_LNS_const_add_pc
:
2521 if (lh
.line_range
== 0)
2523 if (lh
.maximum_ops_per_insn
== 1)
2524 address
+= (lh
.minimum_instruction_length
2525 * ((255 - lh
.opcode_base
) / lh
.line_range
));
2528 bfd_vma adjust
= ((255 - lh
.opcode_base
) / lh
.line_range
);
2529 address
+= (lh
.minimum_instruction_length
2530 * ((op_index
+ adjust
)
2531 / lh
.maximum_ops_per_insn
));
2532 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2535 case DW_LNS_fixed_advance_pc
:
2536 address
+= read_2_bytes (abfd
, line_ptr
, line_end
);
2541 /* Unknown standard opcode, ignore it. */
2542 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
2544 (void) _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2546 line_ptr
+= bytes_read
;
2555 if (unit
->line_offset
== 0)
2556 file
->line_table
= table
;
2557 if (sort_line_sequences (table
))
2561 while (table
->sequences
!= NULL
)
2563 struct line_sequence
* seq
= table
->sequences
;
2564 table
->sequences
= table
->sequences
->prev_sequence
;
2567 free (table
->files
);
2572 /* If ADDR is within TABLE set the output parameters and return the
2573 range of addresses covered by the entry used to fill them out.
2574 Otherwise set * FILENAME_PTR to NULL and return 0.
2575 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2576 are pointers to the objects to be filled in. */
2579 lookup_address_in_line_info_table (struct line_info_table
*table
,
2581 const char **filename_ptr
,
2582 unsigned int *linenumber_ptr
,
2583 unsigned int *discriminator_ptr
)
2585 struct line_sequence
*seq
= NULL
;
2586 struct line_info
*info
;
2589 /* Binary search the array of sequences. */
2591 high
= table
->num_sequences
;
2594 mid
= (low
+ high
) / 2;
2595 seq
= &table
->sequences
[mid
];
2596 if (addr
< seq
->low_pc
)
2598 else if (addr
>= seq
->last_line
->address
)
2604 /* Check for a valid sequence. */
2605 if (!seq
|| addr
< seq
->low_pc
|| addr
>= seq
->last_line
->address
)
2608 if (!build_line_info_table (table
, seq
))
2611 /* Binary search the array of line information. */
2613 high
= seq
->num_lines
;
2617 mid
= (low
+ high
) / 2;
2618 info
= seq
->line_info_lookup
[mid
];
2619 if (addr
< info
->address
)
2621 else if (addr
>= seq
->line_info_lookup
[mid
+ 1]->address
)
2627 /* Check for a valid line information entry. */
2629 && addr
>= info
->address
2630 && addr
< seq
->line_info_lookup
[mid
+ 1]->address
2631 && !(info
->end_sequence
|| info
== seq
->last_line
))
2633 *filename_ptr
= info
->filename
;
2634 *linenumber_ptr
= info
->line
;
2635 if (discriminator_ptr
)
2636 *discriminator_ptr
= info
->discriminator
;
2637 return seq
->last_line
->address
- seq
->low_pc
;
2641 *filename_ptr
= NULL
;
2645 /* Read in the .debug_ranges section for future reference. */
2648 read_debug_ranges (struct comp_unit
* unit
)
2650 struct dwarf2_debug
*stash
= unit
->stash
;
2651 struct dwarf2_debug_file
*file
= unit
->file
;
2653 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_ranges
],
2655 &file
->dwarf_ranges_buffer
, &file
->dwarf_ranges_size
);
2658 /* Read in the .debug_rnglists section for future reference. */
2661 read_debug_rnglists (struct comp_unit
* unit
)
2663 struct dwarf2_debug
*stash
= unit
->stash
;
2664 struct dwarf2_debug_file
*file
= unit
->file
;
2666 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_rnglists
],
2668 &file
->dwarf_rnglists_buffer
, &file
->dwarf_rnglists_size
);
2671 /* Function table functions. */
2674 compare_lookup_funcinfos (const void * a
, const void * b
)
2676 const struct lookup_funcinfo
* lookup1
= a
;
2677 const struct lookup_funcinfo
* lookup2
= b
;
2679 if (lookup1
->low_addr
< lookup2
->low_addr
)
2681 if (lookup1
->low_addr
> lookup2
->low_addr
)
2683 if (lookup1
->high_addr
< lookup2
->high_addr
)
2685 if (lookup1
->high_addr
> lookup2
->high_addr
)
2688 if (lookup1
->idx
< lookup2
->idx
)
2690 if (lookup1
->idx
> lookup2
->idx
)
2696 build_lookup_funcinfo_table (struct comp_unit
* unit
)
2698 struct lookup_funcinfo
*lookup_funcinfo_table
= unit
->lookup_funcinfo_table
;
2699 unsigned int number_of_functions
= unit
->number_of_functions
;
2700 struct funcinfo
*each
;
2701 struct lookup_funcinfo
*entry
;
2703 struct arange
*range
;
2704 bfd_vma low_addr
, high_addr
;
2706 if (lookup_funcinfo_table
|| number_of_functions
== 0)
2709 /* Create the function info lookup table. */
2710 lookup_funcinfo_table
= (struct lookup_funcinfo
*)
2711 bfd_malloc (number_of_functions
* sizeof (struct lookup_funcinfo
));
2712 if (lookup_funcinfo_table
== NULL
)
2715 /* Populate the function info lookup table. */
2716 func_index
= number_of_functions
;
2717 for (each
= unit
->function_table
; each
; each
= each
->prev_func
)
2719 entry
= &lookup_funcinfo_table
[--func_index
];
2720 entry
->funcinfo
= each
;
2721 entry
->idx
= func_index
;
2723 /* Calculate the lowest and highest address for this function entry. */
2724 low_addr
= entry
->funcinfo
->arange
.low
;
2725 high_addr
= entry
->funcinfo
->arange
.high
;
2727 for (range
= entry
->funcinfo
->arange
.next
; range
; range
= range
->next
)
2729 if (range
->low
< low_addr
)
2730 low_addr
= range
->low
;
2731 if (range
->high
> high_addr
)
2732 high_addr
= range
->high
;
2735 entry
->low_addr
= low_addr
;
2736 entry
->high_addr
= high_addr
;
2739 BFD_ASSERT (func_index
== 0);
2741 /* Sort the function by address. */
2742 qsort (lookup_funcinfo_table
,
2743 number_of_functions
,
2744 sizeof (struct lookup_funcinfo
),
2745 compare_lookup_funcinfos
);
2747 /* Calculate the high watermark for each function in the lookup table. */
2748 high_addr
= lookup_funcinfo_table
[0].high_addr
;
2749 for (func_index
= 1; func_index
< number_of_functions
; func_index
++)
2751 entry
= &lookup_funcinfo_table
[func_index
];
2752 if (entry
->high_addr
> high_addr
)
2753 high_addr
= entry
->high_addr
;
2755 entry
->high_addr
= high_addr
;
2758 unit
->lookup_funcinfo_table
= lookup_funcinfo_table
;
2762 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
2763 TRUE. Note that we need to find the function that has the smallest range
2764 that contains ADDR, to handle inlined functions without depending upon
2765 them being ordered in TABLE by increasing range. */
2768 lookup_address_in_function_table (struct comp_unit
*unit
,
2770 struct funcinfo
**function_ptr
)
2772 unsigned int number_of_functions
= unit
->number_of_functions
;
2773 struct lookup_funcinfo
* lookup_funcinfo
= NULL
;
2774 struct funcinfo
* funcinfo
= NULL
;
2775 struct funcinfo
* best_fit
= NULL
;
2776 bfd_vma best_fit_len
= 0;
2777 bfd_size_type low
, high
, mid
, first
;
2778 struct arange
*arange
;
2780 if (number_of_functions
== 0)
2783 if (!build_lookup_funcinfo_table (unit
))
2786 if (unit
->lookup_funcinfo_table
[number_of_functions
- 1].high_addr
< addr
)
2789 /* Find the first function in the lookup table which may contain the
2790 specified address. */
2792 high
= number_of_functions
;
2796 mid
= (low
+ high
) / 2;
2797 lookup_funcinfo
= &unit
->lookup_funcinfo_table
[mid
];
2798 if (addr
< lookup_funcinfo
->low_addr
)
2800 else if (addr
>= lookup_funcinfo
->high_addr
)
2806 /* Find the 'best' match for the address. The prior algorithm defined the
2807 best match as the function with the smallest address range containing
2808 the specified address. This definition should probably be changed to the
2809 innermost inline routine containing the address, but right now we want
2810 to get the same results we did before. */
2811 while (first
< number_of_functions
)
2813 if (addr
< unit
->lookup_funcinfo_table
[first
].low_addr
)
2815 funcinfo
= unit
->lookup_funcinfo_table
[first
].funcinfo
;
2817 for (arange
= &funcinfo
->arange
; arange
; arange
= arange
->next
)
2819 if (addr
< arange
->low
|| addr
>= arange
->high
)
2823 || arange
->high
- arange
->low
< best_fit_len
2824 /* The following comparison is designed to return the same
2825 match as the previous algorithm for routines which have the
2826 same best fit length. */
2827 || (arange
->high
- arange
->low
== best_fit_len
2828 && funcinfo
> best_fit
))
2830 best_fit
= funcinfo
;
2831 best_fit_len
= arange
->high
- arange
->low
;
2841 *function_ptr
= best_fit
;
2845 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2846 and LINENUMBER_PTR, and return TRUE. */
2849 lookup_symbol_in_function_table (struct comp_unit
*unit
,
2852 const char **filename_ptr
,
2853 unsigned int *linenumber_ptr
)
2855 struct funcinfo
* each_func
;
2856 struct funcinfo
* best_fit
= NULL
;
2857 bfd_vma best_fit_len
= 0;
2858 struct arange
*arange
;
2859 const char *name
= bfd_asymbol_name (sym
);
2860 asection
*sec
= bfd_asymbol_section (sym
);
2862 for (each_func
= unit
->function_table
;
2864 each_func
= each_func
->prev_func
)
2866 for (arange
= &each_func
->arange
;
2868 arange
= arange
->next
)
2870 if ((!each_func
->sec
|| each_func
->sec
== sec
)
2871 && addr
>= arange
->low
2872 && addr
< arange
->high
2874 && strcmp (name
, each_func
->name
) == 0
2876 || arange
->high
- arange
->low
< best_fit_len
))
2878 best_fit
= each_func
;
2879 best_fit_len
= arange
->high
- arange
->low
;
2886 best_fit
->sec
= sec
;
2887 *filename_ptr
= best_fit
->file
;
2888 *linenumber_ptr
= best_fit
->line
;
2895 /* Variable table functions. */
2897 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
2898 LINENUMBER_PTR, and return TRUE. */
2901 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
2904 const char **filename_ptr
,
2905 unsigned int *linenumber_ptr
)
2907 const char *name
= bfd_asymbol_name (sym
);
2908 asection
*sec
= bfd_asymbol_section (sym
);
2909 struct varinfo
* each
;
2911 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
2913 && each
->file
!= NULL
2914 && each
->name
!= NULL
2915 && each
->addr
== addr
2916 && (!each
->sec
|| each
->sec
== sec
)
2917 && strcmp (name
, each
->name
) == 0)
2923 *filename_ptr
= each
->file
;
2924 *linenumber_ptr
= each
->line
;
2931 static struct comp_unit
*stash_comp_unit (struct dwarf2_debug
*,
2932 struct dwarf2_debug_file
*);
2933 static bfd_boolean
comp_unit_maybe_decode_line_info (struct comp_unit
*);
2936 find_abstract_instance (struct comp_unit
*unit
,
2937 struct attribute
*attr_ptr
,
2938 unsigned int recur_count
,
2940 bfd_boolean
*is_linkage
,
2941 char **filename_ptr
,
2942 int *linenumber_ptr
)
2944 bfd
*abfd
= unit
->abfd
;
2945 bfd_byte
*info_ptr
= NULL
;
2946 bfd_byte
*info_ptr_end
;
2947 unsigned int abbrev_number
, bytes_read
, i
;
2948 struct abbrev_info
*abbrev
;
2949 bfd_uint64_t die_ref
= attr_ptr
->u
.val
;
2950 struct attribute attr
;
2951 const char *name
= NULL
;
2953 if (recur_count
== 100)
2956 (_("DWARF error: abstract instance recursion detected"));
2957 bfd_set_error (bfd_error_bad_value
);
2961 /* DW_FORM_ref_addr can reference an entry in a different CU. It
2962 is an offset from the .debug_info section, not the current CU. */
2963 if (attr_ptr
->form
== DW_FORM_ref_addr
)
2965 /* We only support DW_FORM_ref_addr within the same file, so
2966 any relocations should be resolved already. Check this by
2967 testing for a zero die_ref; There can't be a valid reference
2968 to the header of a .debug_info section.
2969 DW_FORM_ref_addr is an offset relative to .debug_info.
2970 Normally when using the GNU linker this is accomplished by
2971 emitting a symbolic reference to a label, because .debug_info
2972 sections are linked at zero. When there are multiple section
2973 groups containing .debug_info, as there might be in a
2974 relocatable object file, it would be reasonable to assume that
2975 a symbolic reference to a label in any .debug_info section
2976 might be used. Since we lay out multiple .debug_info
2977 sections at non-zero VMAs (see place_sections), and read
2978 them contiguously into dwarf_info_buffer, that means the
2979 reference is relative to dwarf_info_buffer. */
2982 info_ptr
= unit
->file
->dwarf_info_buffer
;
2983 info_ptr_end
= info_ptr
+ unit
->file
->dwarf_info_size
;
2984 total
= info_ptr_end
- info_ptr
;
2987 else if (die_ref
>= total
)
2990 (_("DWARF error: invalid abstract instance DIE ref"));
2991 bfd_set_error (bfd_error_bad_value
);
2994 info_ptr
+= die_ref
;
2996 else if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
2998 bfd_boolean first_time
= unit
->stash
->alt
.dwarf_info_buffer
== NULL
;
3000 info_ptr
= read_alt_indirect_ref (unit
, die_ref
);
3002 unit
->stash
->alt
.info_ptr
= unit
->stash
->alt
.dwarf_info_buffer
;
3003 if (info_ptr
== NULL
)
3006 (_("DWARF error: unable to read alt ref %" PRIu64
),
3007 (uint64_t) die_ref
);
3008 bfd_set_error (bfd_error_bad_value
);
3011 info_ptr_end
= (unit
->stash
->alt
.dwarf_info_buffer
3012 + unit
->stash
->alt
.dwarf_info_size
);
3013 if (unit
->stash
->alt
.all_comp_units
)
3014 unit
= unit
->stash
->alt
.all_comp_units
;
3017 if (attr_ptr
->form
== DW_FORM_ref_addr
3018 || attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3020 /* Now find the CU containing this pointer. */
3021 if (info_ptr
>= unit
->info_ptr_unit
&& info_ptr
< unit
->end_ptr
)
3022 info_ptr_end
= unit
->end_ptr
;
3025 /* Check other CUs to see if they contain the abbrev. */
3026 struct comp_unit
*u
;
3028 for (u
= unit
->prev_unit
; u
!= NULL
; u
= u
->prev_unit
)
3029 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3033 for (u
= unit
->next_unit
; u
!= NULL
; u
= u
->next_unit
)
3034 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3037 if (attr_ptr
->form
== DW_FORM_ref_addr
)
3040 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->f
);
3043 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3048 if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3051 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->alt
);
3054 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3062 (_("DWARF error: unable to locate abstract instance DIE ref %"
3063 PRIu64
), (uint64_t) die_ref
);
3064 bfd_set_error (bfd_error_bad_value
);
3068 info_ptr_end
= unit
->end_ptr
;
3073 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
3074 DW_FORM_ref_udata. These are all references relative to the
3075 start of the current CU. */
3078 info_ptr
= unit
->info_ptr_unit
;
3079 info_ptr_end
= unit
->end_ptr
;
3080 total
= info_ptr_end
- info_ptr
;
3081 if (!die_ref
|| die_ref
>= total
)
3084 (_("DWARF error: invalid abstract instance DIE ref"));
3085 bfd_set_error (bfd_error_bad_value
);
3088 info_ptr
+= die_ref
;
3091 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
3092 FALSE
, info_ptr_end
);
3093 info_ptr
+= bytes_read
;
3097 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3101 (_("DWARF error: could not find abbrev number %u"), abbrev_number
);
3102 bfd_set_error (bfd_error_bad_value
);
3107 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3109 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
,
3110 info_ptr
, info_ptr_end
);
3111 if (info_ptr
== NULL
)
3116 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3118 if (name
== NULL
&& is_str_attr (attr
.form
))
3121 if (non_mangled (unit
->lang
))
3125 case DW_AT_specification
:
3126 if (!find_abstract_instance (unit
, &attr
, recur_count
+ 1,
3128 filename_ptr
, linenumber_ptr
))
3131 case DW_AT_linkage_name
:
3132 case DW_AT_MIPS_linkage_name
:
3133 /* PR 16949: Corrupt debug info can place
3134 non-string forms into these attributes. */
3135 if (is_str_attr (attr
.form
))
3141 case DW_AT_decl_file
:
3142 if (!comp_unit_maybe_decode_line_info (unit
))
3144 *filename_ptr
= concat_filename (unit
->line_table
,
3147 case DW_AT_decl_line
:
3148 *linenumber_ptr
= attr
.u
.val
;
3161 read_ranges (struct comp_unit
*unit
, struct arange
*arange
,
3162 bfd_uint64_t offset
)
3164 bfd_byte
*ranges_ptr
;
3165 bfd_byte
*ranges_end
;
3166 bfd_vma base_address
= unit
->base_address
;
3168 if (! unit
->file
->dwarf_ranges_buffer
)
3170 if (! read_debug_ranges (unit
))
3174 ranges_ptr
= unit
->file
->dwarf_ranges_buffer
+ offset
;
3175 if (ranges_ptr
< unit
->file
->dwarf_ranges_buffer
)
3177 ranges_end
= unit
->file
->dwarf_ranges_buffer
+ unit
->file
->dwarf_ranges_size
;
3184 /* PR 17512: file: 62cada7d. */
3185 if (ranges_ptr
+ 2 * unit
->addr_size
> ranges_end
)
3188 low_pc
= read_address (unit
, ranges_ptr
, ranges_end
);
3189 ranges_ptr
+= unit
->addr_size
;
3190 high_pc
= read_address (unit
, ranges_ptr
, ranges_end
);
3191 ranges_ptr
+= unit
->addr_size
;
3193 if (low_pc
== 0 && high_pc
== 0)
3195 if (low_pc
== -1UL && high_pc
!= -1UL)
3196 base_address
= high_pc
;
3199 if (!arange_add (unit
, arange
,
3200 base_address
+ low_pc
, base_address
+ high_pc
))
3208 read_rnglists (struct comp_unit
*unit
, struct arange
*arange
,
3209 bfd_uint64_t offset
)
3213 bfd_vma base_address
= unit
->base_address
;
3216 bfd
*abfd
= unit
->abfd
;
3218 if (! unit
->file
->dwarf_rnglists_buffer
)
3220 if (! read_debug_rnglists (unit
))
3224 rngs_ptr
= unit
->file
->dwarf_rnglists_buffer
+ offset
;
3225 if (rngs_ptr
< unit
->file
->dwarf_rnglists_buffer
)
3227 rngs_end
= unit
->file
->dwarf_rnglists_buffer
;
3228 rngs_end
+= unit
->file
->dwarf_rnglists_size
;
3232 enum dwarf_range_list_entry rlet
;
3233 unsigned int bytes_read
;
3235 if (rngs_ptr
+ 1 > rngs_end
)
3238 rlet
= read_1_byte (abfd
, rngs_ptr
, rngs_end
);
3243 case DW_RLE_end_of_list
:
3246 case DW_RLE_base_address
:
3247 if (rngs_ptr
+ unit
->addr_size
> rngs_end
)
3249 base_address
= read_address (unit
, rngs_ptr
, rngs_end
);
3250 rngs_ptr
+= unit
->addr_size
;
3253 case DW_RLE_start_length
:
3254 if (rngs_ptr
+ unit
->addr_size
> rngs_end
)
3256 low_pc
= read_address (unit
, rngs_ptr
, rngs_end
);
3257 rngs_ptr
+= unit
->addr_size
;
3259 high_pc
+= _bfd_safe_read_leb128 (abfd
, rngs_ptr
, &bytes_read
,
3261 rngs_ptr
+= bytes_read
;
3264 case DW_RLE_offset_pair
:
3265 low_pc
= base_address
;
3266 low_pc
+= _bfd_safe_read_leb128 (abfd
, rngs_ptr
, &bytes_read
,
3268 high_pc
= base_address
;
3269 high_pc
+= _bfd_safe_read_leb128 (abfd
, rngs_ptr
, &bytes_read
,
3273 case DW_RLE_start_end
:
3274 if (rngs_ptr
+ 2 * unit
->addr_size
> rngs_end
)
3276 low_pc
= read_address (unit
, rngs_ptr
, rngs_end
);
3277 rngs_ptr
+= unit
->addr_size
;
3278 high_pc
= read_address (unit
, rngs_ptr
, rngs_end
);
3279 rngs_ptr
+= unit
->addr_size
;
3282 /* TODO x-variants need .debug_addr support used for split-dwarf. */
3283 case DW_RLE_base_addressx
:
3284 case DW_RLE_startx_endx
:
3285 case DW_RLE_startx_length
:
3290 if ((low_pc
== 0 && high_pc
== 0) || low_pc
== high_pc
)
3293 if (!arange_add (unit
, arange
, low_pc
, high_pc
))
3299 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
,
3300 bfd_uint64_t offset
)
3302 if (unit
->version
<= 4)
3303 return read_ranges (unit
, arange
, offset
);
3305 return read_rnglists (unit
, arange
, offset
);
3308 static struct varinfo
*
3309 lookup_var_by_offset (bfd_uint64_t offset
, struct varinfo
* table
)
3313 if (table
->unit_offset
== offset
)
3315 table
= table
->prev_var
;
3322 /* DWARF2 Compilation unit functions. */
3324 /* Scan over each die in a comp. unit looking for functions to add
3325 to the function table and variables to the variable table. */
3328 scan_unit_for_symbols (struct comp_unit
*unit
)
3330 bfd
*abfd
= unit
->abfd
;
3331 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
3332 bfd_byte
*info_ptr_end
= unit
->end_ptr
;
3333 int nesting_level
= 0;
3334 struct nest_funcinfo
{
3335 struct funcinfo
*func
;
3337 int nested_funcs_size
;
3339 /* Maintain a stack of in-scope functions and inlined functions, which we
3340 can use to set the caller_func field. */
3341 nested_funcs_size
= 32;
3342 nested_funcs
= (struct nest_funcinfo
*)
3343 bfd_malloc (nested_funcs_size
* sizeof (*nested_funcs
));
3344 if (nested_funcs
== NULL
)
3346 nested_funcs
[nesting_level
].func
= 0;
3348 while (nesting_level
>= 0)
3350 unsigned int abbrev_number
, bytes_read
, i
;
3351 struct abbrev_info
*abbrev
;
3352 struct attribute attr
;
3353 struct funcinfo
*func
;
3354 struct varinfo
*var
;
3356 bfd_vma high_pc
= 0;
3357 bfd_boolean high_pc_relative
= FALSE
;
3358 bfd_uint64_t current_offset
;
3360 /* PR 17512: file: 9f405d9d. */
3361 if (info_ptr
>= info_ptr_end
)
3364 current_offset
= info_ptr
- unit
->info_ptr_unit
;
3365 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
3366 FALSE
, info_ptr_end
);
3367 info_ptr
+= bytes_read
;
3369 if (! abbrev_number
)
3375 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3378 static unsigned int previous_failed_abbrev
= -1U;
3380 /* Avoid multiple reports of the same missing abbrev. */
3381 if (abbrev_number
!= previous_failed_abbrev
)
3384 (_("DWARF error: could not find abbrev number %u"),
3386 previous_failed_abbrev
= abbrev_number
;
3388 bfd_set_error (bfd_error_bad_value
);
3392 if (abbrev
->tag
== DW_TAG_subprogram
3393 || abbrev
->tag
== DW_TAG_entry_point
3394 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3396 size_t amt
= sizeof (struct funcinfo
);
3399 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
3402 func
->tag
= abbrev
->tag
;
3403 func
->prev_func
= unit
->function_table
;
3404 unit
->function_table
= func
;
3405 unit
->number_of_functions
++;
3406 BFD_ASSERT (!unit
->cached
);
3408 if (func
->tag
== DW_TAG_inlined_subroutine
)
3409 for (i
= nesting_level
; i
-- != 0; )
3410 if (nested_funcs
[i
].func
)
3412 func
->caller_func
= nested_funcs
[i
].func
;
3415 nested_funcs
[nesting_level
].func
= func
;
3420 if (abbrev
->tag
== DW_TAG_variable
3421 || abbrev
->tag
== DW_TAG_member
)
3423 size_t amt
= sizeof (struct varinfo
);
3424 var
= (struct varinfo
*) bfd_zalloc (abfd
, amt
);
3427 var
->tag
= abbrev
->tag
;
3429 var
->prev_var
= unit
->variable_table
;
3430 unit
->variable_table
= var
;
3431 var
->unit_offset
= current_offset
;
3432 /* PR 18205: Missing debug information can cause this
3433 var to be attached to an already cached unit. */
3438 /* No inline function in scope at this nesting level. */
3439 nested_funcs
[nesting_level
].func
= 0;
3442 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3444 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
3445 unit
, info_ptr
, info_ptr_end
);
3446 if (info_ptr
== NULL
)
3453 case DW_AT_call_file
:
3454 func
->caller_file
= concat_filename (unit
->line_table
,
3458 case DW_AT_call_line
:
3459 func
->caller_line
= attr
.u
.val
;
3462 case DW_AT_abstract_origin
:
3463 case DW_AT_specification
:
3464 if (!find_abstract_instance (unit
, &attr
, 0,
3473 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3475 if (func
->name
== NULL
&& is_str_attr (attr
.form
))
3477 func
->name
= attr
.u
.str
;
3478 if (non_mangled (unit
->lang
))
3479 func
->is_linkage
= TRUE
;
3483 case DW_AT_linkage_name
:
3484 case DW_AT_MIPS_linkage_name
:
3485 /* PR 16949: Corrupt debug info can place
3486 non-string forms into these attributes. */
3487 if (is_str_attr (attr
.form
))
3489 func
->name
= attr
.u
.str
;
3490 func
->is_linkage
= TRUE
;
3495 low_pc
= attr
.u
.val
;
3499 high_pc
= attr
.u
.val
;
3500 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
3504 if (!read_rangelist (unit
, &func
->arange
, attr
.u
.val
))
3508 case DW_AT_decl_file
:
3509 func
->file
= concat_filename (unit
->line_table
,
3513 case DW_AT_decl_line
:
3514 func
->line
= attr
.u
.val
;
3525 case DW_AT_specification
:
3528 struct varinfo
* spec_var
;
3530 spec_var
= lookup_var_by_offset (attr
.u
.val
,
3531 unit
->variable_table
);
3532 if (spec_var
== NULL
)
3534 _bfd_error_handler (_("DWARF error: could not find "
3535 "variable specification "
3537 (unsigned long) attr
.u
.val
);
3541 if (var
->name
== NULL
)
3542 var
->name
= spec_var
->name
;
3543 if (var
->file
== NULL
&& spec_var
->file
!= NULL
)
3544 var
->file
= strdup (spec_var
->file
);
3546 var
->line
= spec_var
->line
;
3547 if (var
->sec
== NULL
)
3548 var
->sec
= spec_var
->sec
;
3553 if (is_str_attr (attr
.form
))
3554 var
->name
= attr
.u
.str
;
3557 case DW_AT_decl_file
:
3558 var
->file
= concat_filename (unit
->line_table
,
3562 case DW_AT_decl_line
:
3563 var
->line
= attr
.u
.val
;
3566 case DW_AT_external
:
3567 if (attr
.u
.val
!= 0)
3571 case DW_AT_location
:
3575 case DW_FORM_block1
:
3576 case DW_FORM_block2
:
3577 case DW_FORM_block4
:
3578 case DW_FORM_exprloc
:
3579 if (attr
.u
.blk
->data
!= NULL
3580 && *attr
.u
.blk
->data
== DW_OP_addr
)
3584 /* Verify that DW_OP_addr is the only opcode in the
3585 location, in which case the block size will be 1
3586 plus the address size. */
3587 /* ??? For TLS variables, gcc can emit
3588 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
3589 which we don't handle here yet. */
3590 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
3591 var
->addr
= bfd_get (unit
->addr_size
* 8,
3593 attr
.u
.blk
->data
+ 1);
3608 if (high_pc_relative
)
3611 if (func
&& high_pc
!= 0)
3613 if (!arange_add (unit
, &func
->arange
, low_pc
, high_pc
))
3617 if (abbrev
->has_children
)
3621 if (nesting_level
>= nested_funcs_size
)
3623 struct nest_funcinfo
*tmp
;
3625 nested_funcs_size
*= 2;
3626 tmp
= (struct nest_funcinfo
*)
3627 bfd_realloc (nested_funcs
,
3628 nested_funcs_size
* sizeof (*nested_funcs
));
3633 nested_funcs
[nesting_level
].func
= 0;
3637 free (nested_funcs
);
3641 free (nested_funcs
);
3645 /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
3646 includes the compilation unit header that proceeds the DIE's, but
3647 does not include the length field that precedes each compilation
3648 unit header. END_PTR points one past the end of this comp unit.
3649 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
3651 This routine does not read the whole compilation unit; only enough
3652 to get to the line number information for the compilation unit. */
3654 static struct comp_unit
*
3655 parse_comp_unit (struct dwarf2_debug
*stash
,
3656 struct dwarf2_debug_file
*file
,
3658 bfd_vma unit_length
,
3659 bfd_byte
*info_ptr_unit
,
3660 unsigned int offset_size
)
3662 struct comp_unit
* unit
;
3663 unsigned int version
;
3664 bfd_uint64_t abbrev_offset
= 0;
3665 /* Initialize it just to avoid a GCC false warning. */
3666 unsigned int addr_size
= -1;
3667 struct abbrev_info
** abbrevs
;
3668 unsigned int abbrev_number
, bytes_read
, i
;
3669 struct abbrev_info
*abbrev
;
3670 struct attribute attr
;
3671 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
3674 bfd_vma high_pc
= 0;
3675 bfd
*abfd
= file
->bfd_ptr
;
3676 bfd_boolean high_pc_relative
= FALSE
;
3677 enum dwarf_unit_type unit_type
;
3679 version
= read_2_bytes (abfd
, info_ptr
, end_ptr
);
3681 if (version
< 2 || version
> 5)
3683 /* PR 19872: A version number of 0 probably means that there is padding
3684 at the end of the .debug_info section. Gold puts it there when
3685 performing an incremental link, for example. So do not generate
3686 an error, just return a NULL. */
3690 (_("DWARF error: found dwarf version '%u', this reader"
3691 " only handles version 2, 3, 4 and 5 information"), version
);
3692 bfd_set_error (bfd_error_bad_value
);
3698 unit_type
= DW_UT_compile
;
3701 unit_type
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3704 addr_size
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3708 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
3709 if (offset_size
== 4)
3710 abbrev_offset
= read_4_bytes (abfd
, info_ptr
, end_ptr
);
3712 abbrev_offset
= read_8_bytes (abfd
, info_ptr
, end_ptr
);
3713 info_ptr
+= offset_size
;
3717 addr_size
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3721 if (unit_type
== DW_UT_type
)
3723 /* Skip type signature. */
3726 /* Skip type offset. */
3727 info_ptr
+= offset_size
;
3730 if (addr_size
> sizeof (bfd_vma
))
3733 /* xgettext: c-format */
3734 (_("DWARF error: found address size '%u', this reader"
3735 " can not handle sizes greater than '%u'"),
3737 (unsigned int) sizeof (bfd_vma
));
3738 bfd_set_error (bfd_error_bad_value
);
3742 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
3745 ("DWARF error: found address size '%u', this reader"
3746 " can only handle address sizes '2', '4' and '8'", addr_size
);
3747 bfd_set_error (bfd_error_bad_value
);
3751 /* Read the abbrevs for this compilation unit into a table. */
3752 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
, file
);
3756 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
3758 info_ptr
+= bytes_read
;
3759 if (! abbrev_number
)
3761 /* PR 19872: An abbrev number of 0 probably means that there is padding
3762 at the end of the .debug_abbrev section. Gold puts it there when
3763 performing an incremental link, for example. So do not generate
3764 an error, just return a NULL. */
3768 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
3771 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
3773 bfd_set_error (bfd_error_bad_value
);
3777 amt
= sizeof (struct comp_unit
);
3778 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
3782 unit
->version
= version
;
3783 unit
->addr_size
= addr_size
;
3784 unit
->offset_size
= offset_size
;
3785 unit
->abbrevs
= abbrevs
;
3786 unit
->end_ptr
= end_ptr
;
3787 unit
->stash
= stash
;
3789 unit
->info_ptr_unit
= info_ptr_unit
;
3791 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3793 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
, end_ptr
);
3794 if (info_ptr
== NULL
)
3797 /* Store the data if it is of an attribute we want to keep in a
3798 partial symbol table. */
3801 case DW_AT_stmt_list
:
3803 unit
->line_offset
= attr
.u
.val
;
3807 if (is_str_attr (attr
.form
))
3808 unit
->name
= attr
.u
.str
;
3812 low_pc
= attr
.u
.val
;
3813 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
3814 this is the base address to use when reading location
3815 lists or range lists. */
3816 if (abbrev
->tag
== DW_TAG_compile_unit
)
3817 unit
->base_address
= low_pc
;
3821 high_pc
= attr
.u
.val
;
3822 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
3826 if (!read_rangelist (unit
, &unit
->arange
, attr
.u
.val
))
3830 case DW_AT_comp_dir
:
3832 char *comp_dir
= attr
.u
.str
;
3834 /* PR 17512: file: 1fe726be. */
3835 if (! is_str_attr (attr
.form
))
3838 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
3844 /* Irix 6.2 native cc prepends <machine>.: to the compilation
3845 directory, get rid of it. */
3846 char *cp
= strchr (comp_dir
, ':');
3848 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
3851 unit
->comp_dir
= comp_dir
;
3855 case DW_AT_language
:
3856 unit
->lang
= attr
.u
.val
;
3863 if (high_pc_relative
)
3867 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
3871 unit
->first_child_die_ptr
= info_ptr
;
3875 /* Return TRUE if UNIT may contain the address given by ADDR. When
3876 there are functions written entirely with inline asm statements, the
3877 range info in the compilation unit header may not be correct. We
3878 need to consult the line info table to see if a compilation unit
3879 really contains the given address. */
3882 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
3884 struct arange
*arange
;
3889 arange
= &unit
->arange
;
3892 if (addr
>= arange
->low
&& addr
< arange
->high
)
3894 arange
= arange
->next
;
3901 /* If UNIT contains ADDR, set the output parameters to the values for
3902 the line containing ADDR. The output parameters, FILENAME_PTR,
3903 FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects
3906 Returns the range of addresses covered by the entry that was used
3907 to fill in *LINENUMBER_PTR or 0 if it was not filled in. */
3910 comp_unit_find_nearest_line (struct comp_unit
*unit
,
3912 const char **filename_ptr
,
3913 struct funcinfo
**function_ptr
,
3914 unsigned int *linenumber_ptr
,
3915 unsigned int *discriminator_ptr
)
3919 if (!comp_unit_maybe_decode_line_info (unit
))
3922 *function_ptr
= NULL
;
3923 func_p
= lookup_address_in_function_table (unit
, addr
, function_ptr
);
3924 if (func_p
&& (*function_ptr
)->tag
== DW_TAG_inlined_subroutine
)
3925 unit
->stash
->inliner_chain
= *function_ptr
;
3927 return lookup_address_in_line_info_table (unit
->line_table
, addr
,
3933 /* Check to see if line info is already decoded in a comp_unit.
3934 If not, decode it. Returns TRUE if no errors were encountered;
3938 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
)
3943 if (! unit
->line_table
)
3945 if (! unit
->stmtlist
)
3951 unit
->line_table
= decode_line_info (unit
);
3953 if (! unit
->line_table
)
3959 if (unit
->first_child_die_ptr
< unit
->end_ptr
3960 && ! scan_unit_for_symbols (unit
))
3970 /* If UNIT contains SYM at ADDR, set the output parameters to the
3971 values for the line containing SYM. The output parameters,
3972 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
3975 Return TRUE if UNIT contains SYM, and no errors were encountered;
3979 comp_unit_find_line (struct comp_unit
*unit
,
3982 const char **filename_ptr
,
3983 unsigned int *linenumber_ptr
)
3985 if (!comp_unit_maybe_decode_line_info (unit
))
3988 if (sym
->flags
& BSF_FUNCTION
)
3989 return lookup_symbol_in_function_table (unit
, sym
, addr
,
3993 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
3998 static struct funcinfo
*
3999 reverse_funcinfo_list (struct funcinfo
*head
)
4001 struct funcinfo
*rhead
;
4002 struct funcinfo
*temp
;
4004 for (rhead
= NULL
; head
; head
= temp
)
4006 temp
= head
->prev_func
;
4007 head
->prev_func
= rhead
;
4013 static struct varinfo
*
4014 reverse_varinfo_list (struct varinfo
*head
)
4016 struct varinfo
*rhead
;
4017 struct varinfo
*temp
;
4019 for (rhead
= NULL
; head
; head
= temp
)
4021 temp
= head
->prev_var
;
4022 head
->prev_var
= rhead
;
4028 /* Extract all interesting funcinfos and varinfos of a compilation
4029 unit into hash tables for faster lookup. Returns TRUE if no
4030 errors were enountered; FALSE otherwise. */
4033 comp_unit_hash_info (struct dwarf2_debug
*stash
,
4034 struct comp_unit
*unit
,
4035 struct info_hash_table
*funcinfo_hash_table
,
4036 struct info_hash_table
*varinfo_hash_table
)
4038 struct funcinfo
* each_func
;
4039 struct varinfo
* each_var
;
4040 bfd_boolean okay
= TRUE
;
4042 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
4044 if (!comp_unit_maybe_decode_line_info (unit
))
4047 BFD_ASSERT (!unit
->cached
);
4049 /* To preserve the original search order, we went to visit the function
4050 infos in the reversed order of the list. However, making the list
4051 bi-directional use quite a bit of extra memory. So we reverse
4052 the list first, traverse the list in the now reversed order and
4053 finally reverse the list again to get back the original order. */
4054 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4055 for (each_func
= unit
->function_table
;
4057 each_func
= each_func
->prev_func
)
4059 /* Skip nameless functions. */
4060 if (each_func
->name
)
4061 /* There is no need to copy name string into hash table as
4062 name string is either in the dwarf string buffer or
4063 info in the stash. */
4064 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
4065 (void*) each_func
, FALSE
);
4067 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4071 /* We do the same for variable infos. */
4072 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4073 for (each_var
= unit
->variable_table
;
4075 each_var
= each_var
->prev_var
)
4077 /* Skip stack vars and vars with no files or names. */
4078 if (! each_var
->stack
4079 && each_var
->file
!= NULL
4080 && each_var
->name
!= NULL
)
4081 /* There is no need to copy name string into hash table as
4082 name string is either in the dwarf string buffer or
4083 info in the stash. */
4084 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
4085 (void*) each_var
, FALSE
);
4088 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4089 unit
->cached
= TRUE
;
4093 /* Locate a section in a BFD containing debugging info. The search starts
4094 from the section after AFTER_SEC, or from the first section in the BFD if
4095 AFTER_SEC is NULL. The search works by examining the names of the
4096 sections. There are three permissiable names. The first two are given
4097 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
4098 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
4099 This is a variation on the .debug_info section which has a checksum
4100 describing the contents appended onto the name. This allows the linker to
4101 identify and discard duplicate debugging sections for different
4102 compilation units. */
4103 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
4106 find_debug_info (bfd
*abfd
, const struct dwarf_debug_section
*debug_sections
,
4107 asection
*after_sec
)
4112 if (after_sec
== NULL
)
4114 look
= debug_sections
[debug_info
].uncompressed_name
;
4115 msec
= bfd_get_section_by_name (abfd
, look
);
4119 look
= debug_sections
[debug_info
].compressed_name
;
4122 msec
= bfd_get_section_by_name (abfd
, look
);
4127 for (msec
= abfd
->sections
; msec
!= NULL
; msec
= msec
->next
)
4128 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
4134 for (msec
= after_sec
->next
; msec
!= NULL
; msec
= msec
->next
)
4136 look
= debug_sections
[debug_info
].uncompressed_name
;
4137 if (strcmp (msec
->name
, look
) == 0)
4140 look
= debug_sections
[debug_info
].compressed_name
;
4141 if (look
!= NULL
&& strcmp (msec
->name
, look
) == 0)
4144 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
4151 /* Transfer VMAs from object file to separate debug file. */
4154 set_debug_vma (bfd
*orig_bfd
, bfd
*debug_bfd
)
4158 for (s
= orig_bfd
->sections
, d
= debug_bfd
->sections
;
4159 s
!= NULL
&& d
!= NULL
;
4160 s
= s
->next
, d
= d
->next
)
4162 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4164 /* ??? Assumes 1-1 correspondence between sections in the
4166 if (strcmp (s
->name
, d
->name
) == 0)
4168 d
->output_section
= s
->output_section
;
4169 d
->output_offset
= s
->output_offset
;
4175 /* If the dwarf2 info was found in a separate debug file, return the
4176 debug file section corresponding to the section in the original file
4177 and the debug file symbols. */
4180 _bfd_dwarf2_stash_syms (struct dwarf2_debug
*stash
, bfd
*abfd
,
4181 asection
**sec
, asymbol
***syms
)
4183 if (stash
->f
.bfd_ptr
!= abfd
)
4189 *syms
= stash
->f
.syms
;
4193 for (s
= abfd
->sections
, d
= stash
->f
.bfd_ptr
->sections
;
4194 s
!= NULL
&& d
!= NULL
;
4195 s
= s
->next
, d
= d
->next
)
4197 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4200 && strcmp (s
->name
, d
->name
) == 0)
4203 *syms
= stash
->f
.syms
;
4210 /* Unset vmas for adjusted sections in STASH. */
4213 unset_sections (struct dwarf2_debug
*stash
)
4216 struct adjusted_section
*p
;
4218 i
= stash
->adjusted_section_count
;
4219 p
= stash
->adjusted_sections
;
4220 for (; i
> 0; i
--, p
++)
4221 p
->section
->vma
= 0;
4224 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4225 relocatable object file. VMAs are normally all zero in relocatable
4226 object files, so if we want to distinguish locations in sections by
4227 address we need to set VMAs so the sections do not overlap. We
4228 also set VMA on .debug_info so that when we have multiple
4229 .debug_info sections (or the linkonce variant) they also do not
4230 overlap. The multiple .debug_info sections make up a single
4231 logical section. ??? We should probably do the same for other
4235 place_sections (bfd
*orig_bfd
, struct dwarf2_debug
*stash
)
4238 struct adjusted_section
*p
;
4240 const char *debug_info_name
;
4242 if (stash
->adjusted_section_count
!= 0)
4244 i
= stash
->adjusted_section_count
;
4245 p
= stash
->adjusted_sections
;
4246 for (; i
> 0; i
--, p
++)
4247 p
->section
->vma
= p
->adj_vma
;
4251 debug_info_name
= stash
->debug_sections
[debug_info
].uncompressed_name
;
4258 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4262 if ((sect
->output_section
!= NULL
4263 && sect
->output_section
!= sect
4264 && (sect
->flags
& SEC_DEBUGGING
) == 0)
4268 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
4269 || CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
));
4271 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
4277 if (abfd
== stash
->f
.bfd_ptr
)
4279 abfd
= stash
->f
.bfd_ptr
;
4283 stash
->adjusted_section_count
= -1;
4286 bfd_vma last_vma
= 0, last_dwarf
= 0;
4287 size_t amt
= i
* sizeof (struct adjusted_section
);
4289 p
= (struct adjusted_section
*) bfd_malloc (amt
);
4293 stash
->adjusted_sections
= p
;
4294 stash
->adjusted_section_count
= i
;
4301 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4306 if ((sect
->output_section
!= NULL
4307 && sect
->output_section
!= sect
4308 && (sect
->flags
& SEC_DEBUGGING
) == 0)
4312 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
4313 || CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
));
4315 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
4319 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
4323 BFD_ASSERT (sect
->alignment_power
== 0);
4324 sect
->vma
= last_dwarf
;
4329 /* Align the new address to the current section
4331 last_vma
= ((last_vma
4332 + ~(-((bfd_vma
) 1 << sect
->alignment_power
)))
4333 & (-((bfd_vma
) 1 << sect
->alignment_power
)));
4334 sect
->vma
= last_vma
;
4339 p
->adj_vma
= sect
->vma
;
4342 if (abfd
== stash
->f
.bfd_ptr
)
4344 abfd
= stash
->f
.bfd_ptr
;
4348 if (orig_bfd
!= stash
->f
.bfd_ptr
)
4349 set_debug_vma (orig_bfd
, stash
->f
.bfd_ptr
);
4354 /* Look up a funcinfo by name using the given info hash table. If found,
4355 also update the locations pointed to by filename_ptr and linenumber_ptr.
4357 This function returns TRUE if a funcinfo that matches the given symbol
4358 and address is found with any error; otherwise it returns FALSE. */
4361 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
4364 const char **filename_ptr
,
4365 unsigned int *linenumber_ptr
)
4367 struct funcinfo
* each_func
;
4368 struct funcinfo
* best_fit
= NULL
;
4369 bfd_vma best_fit_len
= 0;
4370 struct info_list_node
*node
;
4371 struct arange
*arange
;
4372 const char *name
= bfd_asymbol_name (sym
);
4373 asection
*sec
= bfd_asymbol_section (sym
);
4375 for (node
= lookup_info_hash_table (hash_table
, name
);
4379 each_func
= (struct funcinfo
*) node
->info
;
4380 for (arange
= &each_func
->arange
;
4382 arange
= arange
->next
)
4384 if ((!each_func
->sec
|| each_func
->sec
== sec
)
4385 && addr
>= arange
->low
4386 && addr
< arange
->high
4388 || arange
->high
- arange
->low
< best_fit_len
))
4390 best_fit
= each_func
;
4391 best_fit_len
= arange
->high
- arange
->low
;
4398 best_fit
->sec
= sec
;
4399 *filename_ptr
= best_fit
->file
;
4400 *linenumber_ptr
= best_fit
->line
;
4407 /* Look up a varinfo by name using the given info hash table. If found,
4408 also update the locations pointed to by filename_ptr and linenumber_ptr.
4410 This function returns TRUE if a varinfo that matches the given symbol
4411 and address is found with any error; otherwise it returns FALSE. */
4414 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
4417 const char **filename_ptr
,
4418 unsigned int *linenumber_ptr
)
4420 const char *name
= bfd_asymbol_name (sym
);
4421 asection
*sec
= bfd_asymbol_section (sym
);
4422 struct varinfo
* each
;
4423 struct info_list_node
*node
;
4425 for (node
= lookup_info_hash_table (hash_table
, name
);
4429 each
= (struct varinfo
*) node
->info
;
4430 if (each
->addr
== addr
4431 && (!each
->sec
|| each
->sec
== sec
))
4434 *filename_ptr
= each
->file
;
4435 *linenumber_ptr
= each
->line
;
4443 /* Update the funcinfo and varinfo info hash tables if they are
4444 not up to date. Returns TRUE if there is no error; otherwise
4445 returns FALSE and disable the info hash tables. */
4448 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
4450 struct comp_unit
*each
;
4452 /* Exit if hash tables are up-to-date. */
4453 if (stash
->f
.all_comp_units
== stash
->hash_units_head
)
4456 if (stash
->hash_units_head
)
4457 each
= stash
->hash_units_head
->prev_unit
;
4459 each
= stash
->f
.last_comp_unit
;
4463 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
4464 stash
->varinfo_hash_table
))
4466 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
4469 each
= each
->prev_unit
;
4472 stash
->hash_units_head
= stash
->f
.all_comp_units
;
4476 /* Check consistency of info hash tables. This is for debugging only. */
4478 static void ATTRIBUTE_UNUSED
4479 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
4481 struct comp_unit
*each_unit
;
4482 struct funcinfo
*each_func
;
4483 struct varinfo
*each_var
;
4484 struct info_list_node
*node
;
4487 for (each_unit
= stash
->f
.all_comp_units
;
4489 each_unit
= each_unit
->next_unit
)
4491 for (each_func
= each_unit
->function_table
;
4493 each_func
= each_func
->prev_func
)
4495 if (!each_func
->name
)
4497 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
4501 while (node
&& !found
)
4503 found
= node
->info
== each_func
;
4509 for (each_var
= each_unit
->variable_table
;
4511 each_var
= each_var
->prev_var
)
4513 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
4515 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
4519 while (node
&& !found
)
4521 found
= node
->info
== each_var
;
4529 /* Check to see if we want to enable the info hash tables, which consume
4530 quite a bit of memory. Currently we only check the number times
4531 bfd_dwarf2_find_line is called. In the future, we may also want to
4532 take the number of symbols into account. */
4535 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
4537 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
4539 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
4542 /* FIXME: Maybe we should check the reduce_memory_overheads
4543 and optimize fields in the bfd_link_info structure ? */
4545 /* Create hash tables. */
4546 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
4547 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
4548 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
4550 /* Turn off info hashes if any allocation above fails. */
4551 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
4554 /* We need a forced update so that the info hash tables will
4555 be created even though there is no compilation unit. That
4556 happens if STASH_INFO_HASH_TRIGGER is 0. */
4557 if (stash_maybe_update_info_hash_tables (stash
))
4558 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
4561 /* Find the file and line associated with a symbol and address using the
4562 info hash tables of a stash. If there is a match, the function returns
4563 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
4564 otherwise it returns FALSE. */
4567 stash_find_line_fast (struct dwarf2_debug
*stash
,
4570 const char **filename_ptr
,
4571 unsigned int *linenumber_ptr
)
4573 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
4575 if (sym
->flags
& BSF_FUNCTION
)
4576 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
4577 filename_ptr
, linenumber_ptr
);
4578 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
4579 filename_ptr
, linenumber_ptr
);
4582 /* Save current section VMAs. */
4585 save_section_vma (const bfd
*abfd
, struct dwarf2_debug
*stash
)
4590 if (abfd
->section_count
== 0)
4592 stash
->sec_vma
= bfd_malloc (sizeof (*stash
->sec_vma
) * abfd
->section_count
);
4593 if (stash
->sec_vma
== NULL
)
4595 stash
->sec_vma_count
= abfd
->section_count
;
4596 for (i
= 0, s
= abfd
->sections
;
4597 s
!= NULL
&& i
< abfd
->section_count
;
4600 if (s
->output_section
!= NULL
)
4601 stash
->sec_vma
[i
] = s
->output_section
->vma
+ s
->output_offset
;
4603 stash
->sec_vma
[i
] = s
->vma
;
4608 /* Compare current section VMAs against those at the time the stash
4609 was created. If find_nearest_line is used in linker warnings or
4610 errors early in the link process, the debug info stash will be
4611 invalid for later calls. This is because we relocate debug info
4612 sections, so the stashed section contents depend on symbol values,
4613 which in turn depend on section VMAs. */
4616 section_vma_same (const bfd
*abfd
, const struct dwarf2_debug
*stash
)
4621 /* PR 24334: If the number of sections in ABFD has changed between
4622 when the stash was created and now, then we cannot trust the
4623 stashed vma information. */
4624 if (abfd
->section_count
!= stash
->sec_vma_count
)
4627 for (i
= 0, s
= abfd
->sections
;
4628 s
!= NULL
&& i
< abfd
->section_count
;
4633 if (s
->output_section
!= NULL
)
4634 vma
= s
->output_section
->vma
+ s
->output_offset
;
4637 if (vma
!= stash
->sec_vma
[i
])
4643 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
4644 If DEBUG_BFD is not specified, we read debug information from ABFD
4645 or its gnu_debuglink. The results will be stored in PINFO.
4646 The function returns TRUE iff debug information is ready. */
4649 _bfd_dwarf2_slurp_debug_info (bfd
*abfd
, bfd
*debug_bfd
,
4650 const struct dwarf_debug_section
*debug_sections
,
4653 bfd_boolean do_place
)
4655 size_t amt
= sizeof (struct dwarf2_debug
);
4656 bfd_size_type total_size
;
4658 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
4662 if (stash
->orig_bfd
== abfd
4663 && section_vma_same (abfd
, stash
))
4665 /* Check that we did previously find some debug information
4666 before attempting to make use of it. */
4667 if (stash
->f
.bfd_ptr
!= NULL
)
4669 if (do_place
&& !place_sections (abfd
, stash
))
4676 _bfd_dwarf2_cleanup_debug_info (abfd
, pinfo
);
4677 memset (stash
, 0, amt
);
4681 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
4685 stash
->orig_bfd
= abfd
;
4686 stash
->debug_sections
= debug_sections
;
4687 stash
->f
.syms
= symbols
;
4688 if (!save_section_vma (abfd
, stash
))
4691 stash
->f
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
4692 del_abbrev
, calloc
, free
);
4693 if (!stash
->f
.abbrev_offsets
)
4696 stash
->alt
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
4697 del_abbrev
, calloc
, free
);
4698 if (!stash
->alt
.abbrev_offsets
)
4703 if (debug_bfd
== NULL
)
4706 msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4707 if (msec
== NULL
&& abfd
== debug_bfd
)
4709 char * debug_filename
;
4711 debug_filename
= bfd_follow_build_id_debuglink (abfd
, DEBUGDIR
);
4712 if (debug_filename
== NULL
)
4713 debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
4715 if (debug_filename
== NULL
)
4716 /* No dwarf2 info, and no gnu_debuglink to follow.
4717 Note that at this point the stash has been allocated, but
4718 contains zeros. This lets future calls to this function
4719 fail more quickly. */
4722 debug_bfd
= bfd_openr (debug_filename
, NULL
);
4723 free (debug_filename
);
4724 if (debug_bfd
== NULL
)
4725 /* FIXME: Should we report our failure to follow the debuglink ? */
4728 /* Set BFD_DECOMPRESS to decompress debug sections. */
4729 debug_bfd
->flags
|= BFD_DECOMPRESS
;
4730 if (!bfd_check_format (debug_bfd
, bfd_object
)
4731 || (msec
= find_debug_info (debug_bfd
,
4732 debug_sections
, NULL
)) == NULL
4733 || !bfd_generic_link_read_symbols (debug_bfd
))
4735 bfd_close (debug_bfd
);
4739 symbols
= bfd_get_outsymbols (debug_bfd
);
4740 stash
->f
.syms
= symbols
;
4741 stash
->close_on_cleanup
= TRUE
;
4743 stash
->f
.bfd_ptr
= debug_bfd
;
4746 && !place_sections (abfd
, stash
))
4749 /* There can be more than one DWARF2 info section in a BFD these
4750 days. First handle the easy case when there's only one. If
4751 there's more than one, try case two: none of the sections is
4752 compressed. In that case, read them all in and produce one
4753 large stash. We do this in two passes - in the first pass we
4754 just accumulate the section sizes, and in the second pass we
4755 read in the section's contents. (The allows us to avoid
4756 reallocing the data as we add sections to the stash.) If
4757 some or all sections are compressed, then do things the slow
4758 way, with a bunch of reallocs. */
4760 if (! find_debug_info (debug_bfd
, debug_sections
, msec
))
4762 /* Case 1: only one info section. */
4763 total_size
= msec
->size
;
4764 if (! read_section (debug_bfd
, &stash
->debug_sections
[debug_info
],
4766 &stash
->f
.dwarf_info_buffer
, &total_size
))
4771 /* Case 2: multiple sections. */
4772 for (total_size
= 0;
4774 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
4776 /* Catch PR25070 testcase overflowing size calculation here. */
4777 if (total_size
+ msec
->size
< total_size
4778 || total_size
+ msec
->size
< msec
->size
)
4780 bfd_set_error (bfd_error_no_memory
);
4783 total_size
+= msec
->size
;
4786 stash
->f
.dwarf_info_buffer
= (bfd_byte
*) bfd_malloc (total_size
);
4787 if (stash
->f
.dwarf_info_buffer
== NULL
)
4791 for (msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4793 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
4801 if (!(bfd_simple_get_relocated_section_contents
4802 (debug_bfd
, msec
, stash
->f
.dwarf_info_buffer
+ total_size
,
4810 stash
->f
.info_ptr
= stash
->f
.dwarf_info_buffer
;
4811 stash
->f
.dwarf_info_size
= total_size
;
4815 /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
4817 static struct comp_unit
*
4818 stash_comp_unit (struct dwarf2_debug
*stash
, struct dwarf2_debug_file
*file
)
4820 bfd_size_type length
;
4821 unsigned int offset_size
;
4822 bfd_byte
*info_ptr_unit
= file
->info_ptr
;
4823 bfd_byte
*info_ptr_end
= file
->dwarf_info_buffer
+ file
->dwarf_info_size
;
4825 if (file
->info_ptr
>= info_ptr_end
)
4828 length
= read_4_bytes (file
->bfd_ptr
, file
->info_ptr
, info_ptr_end
);
4829 /* A 0xffffff length is the DWARF3 way of indicating
4830 we use 64-bit offsets, instead of 32-bit offsets. */
4831 if (length
== 0xffffffff)
4834 length
= read_8_bytes (file
->bfd_ptr
, file
->info_ptr
+ 4,
4836 file
->info_ptr
+= 12;
4838 /* A zero length is the IRIX way of indicating 64-bit offsets,
4839 mostly because the 64-bit length will generally fit in 32
4840 bits, and the endianness helps. */
4841 else if (length
== 0)
4844 length
= read_4_bytes (file
->bfd_ptr
, file
->info_ptr
+ 4,
4846 file
->info_ptr
+= 8;
4848 /* In the absence of the hints above, we assume 32-bit DWARF2
4849 offsets even for targets with 64-bit addresses, because:
4850 a) most of the time these targets will not have generated
4851 more than 2Gb of debug info and so will not need 64-bit
4854 b) if they do use 64-bit offsets but they are not using
4855 the size hints that are tested for above then they are
4856 not conforming to the DWARF3 standard anyway. */
4860 file
->info_ptr
+= 4;
4864 && file
->info_ptr
+ length
<= info_ptr_end
4865 && file
->info_ptr
+ length
> file
->info_ptr
)
4867 struct comp_unit
*each
= parse_comp_unit (stash
, file
,
4868 file
->info_ptr
, length
,
4869 info_ptr_unit
, offset_size
);
4872 if (file
->all_comp_units
)
4873 file
->all_comp_units
->prev_unit
= each
;
4875 file
->last_comp_unit
= each
;
4877 each
->next_unit
= file
->all_comp_units
;
4878 file
->all_comp_units
= each
;
4880 file
->info_ptr
+= length
;
4885 /* Don't trust any of the DWARF info after a corrupted length or
4887 file
->info_ptr
= info_ptr_end
;
4891 /* Hash function for an asymbol. */
4894 hash_asymbol (const void *sym
)
4896 const asymbol
*asym
= sym
;
4897 return htab_hash_string (asym
->name
);
4900 /* Equality function for asymbols. */
4903 eq_asymbol (const void *a
, const void *b
)
4905 const asymbol
*sa
= a
;
4906 const asymbol
*sb
= b
;
4907 return strcmp (sa
->name
, sb
->name
) == 0;
4910 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
4911 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
4912 symbol in SYMBOLS and return the difference between the low_pc and
4913 the symbol's address. Returns 0 if no suitable symbol could be found. */
4916 _bfd_dwarf2_find_symbol_bias (asymbol
** symbols
, void ** pinfo
)
4918 struct dwarf2_debug
*stash
;
4919 struct comp_unit
* unit
;
4921 bfd_signed_vma result
= 0;
4924 stash
= (struct dwarf2_debug
*) *pinfo
;
4926 if (stash
== NULL
|| symbols
== NULL
)
4929 sym_hash
= htab_create_alloc (10, hash_asymbol
, eq_asymbol
,
4930 NULL
, xcalloc
, free
);
4931 for (psym
= symbols
; * psym
!= NULL
; psym
++)
4933 asymbol
* sym
= * psym
;
4935 if (sym
->flags
& BSF_FUNCTION
&& sym
->section
!= NULL
)
4937 void **slot
= htab_find_slot (sym_hash
, sym
, INSERT
);
4942 for (unit
= stash
->f
.all_comp_units
; unit
; unit
= unit
->next_unit
)
4944 struct funcinfo
* func
;
4946 comp_unit_maybe_decode_line_info (unit
);
4948 for (func
= unit
->function_table
; func
!= NULL
; func
= func
->prev_func
)
4949 if (func
->name
&& func
->arange
.low
)
4951 asymbol search
, *sym
;
4953 /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
4955 search
.name
= func
->name
;
4956 sym
= htab_find (sym_hash
, &search
);
4959 result
= ((bfd_signed_vma
) func
->arange
.low
) -
4960 ((bfd_signed_vma
) (sym
->value
+ sym
->section
->vma
));
4967 htab_delete (sym_hash
);
4971 /* Find the source code location of SYMBOL. If SYMBOL is NULL
4972 then find the nearest source code location corresponding to
4973 the address SECTION + OFFSET.
4974 Returns 1 if the line is found without error and fills in
4975 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
4976 NULL the FUNCTIONNAME_PTR is also filled in.
4977 Returns 2 if partial information from _bfd_elf_find_function is
4978 returned (function and maybe file) by looking at symbols. DWARF2
4979 info is present but not regarding the requested code location.
4980 Returns 0 otherwise.
4981 SYMBOLS contains the symbol table for ABFD.
4982 DEBUG_SECTIONS contains the name of the dwarf debug sections. */
4985 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
4990 const char **filename_ptr
,
4991 const char **functionname_ptr
,
4992 unsigned int *linenumber_ptr
,
4993 unsigned int *discriminator_ptr
,
4994 const struct dwarf_debug_section
*debug_sections
,
4997 /* Read each compilation unit from the section .debug_info, and check
4998 to see if it contains the address we are searching for. If yes,
4999 lookup the address, and return the line number info. If no, go
5000 on to the next compilation unit.
5002 We keep a list of all the previously read compilation units, and
5003 a pointer to the next un-read compilation unit. Check the
5004 previously read units before reading more. */
5005 struct dwarf2_debug
*stash
;
5006 /* What address are we looking for? */
5008 struct comp_unit
* each
;
5009 struct funcinfo
*function
= NULL
;
5011 bfd_boolean do_line
;
5013 *filename_ptr
= NULL
;
5014 if (functionname_ptr
!= NULL
)
5015 *functionname_ptr
= NULL
;
5016 *linenumber_ptr
= 0;
5017 if (discriminator_ptr
)
5018 *discriminator_ptr
= 0;
5020 if (! _bfd_dwarf2_slurp_debug_info (abfd
, NULL
, debug_sections
,
5022 (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0))
5025 stash
= (struct dwarf2_debug
*) *pinfo
;
5027 do_line
= symbol
!= NULL
;
5030 BFD_ASSERT (section
== NULL
&& offset
== 0 && functionname_ptr
== NULL
);
5031 section
= bfd_asymbol_section (symbol
);
5032 addr
= symbol
->value
;
5036 BFD_ASSERT (section
!= NULL
&& functionname_ptr
!= NULL
);
5039 /* If we have no SYMBOL but the section we're looking at is not a
5040 code section, then take a look through the list of symbols to see
5041 if we have a symbol at the address we're looking for. If we do
5042 then use this to look up line information. This will allow us to
5043 give file and line results for data symbols. We exclude code
5044 symbols here, if we look up a function symbol and then look up the
5045 line information we'll actually return the line number for the
5046 opening '{' rather than the function definition line. This is
5047 because looking up by symbol uses the line table, in which the
5048 first line for a function is usually the opening '{', while
5049 looking up the function by section + offset uses the
5050 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
5051 which will be the line of the function name. */
5052 if (symbols
!= NULL
&& (section
->flags
& SEC_CODE
) == 0)
5056 for (tmp
= symbols
; (*tmp
) != NULL
; ++tmp
)
5057 if ((*tmp
)->the_bfd
== abfd
5058 && (*tmp
)->section
== section
5059 && (*tmp
)->value
== offset
5060 && ((*tmp
)->flags
& BSF_SECTION_SYM
) == 0)
5064 /* For local symbols, keep going in the hope we find a
5066 if ((symbol
->flags
& BSF_GLOBAL
) != 0)
5072 if (section
->output_section
)
5073 addr
+= section
->output_section
->vma
+ section
->output_offset
;
5075 addr
+= section
->vma
;
5077 /* A null info_ptr indicates that there is no dwarf2 info
5078 (or that an error occured while setting up the stash). */
5079 if (! stash
->f
.info_ptr
)
5082 stash
->inliner_chain
= NULL
;
5084 /* Check the previously read comp. units first. */
5087 /* The info hash tables use quite a bit of memory. We may not want to
5088 always use them. We use some heuristics to decide if and when to
5090 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
5091 stash_maybe_enable_info_hash_tables (abfd
, stash
);
5093 /* Keep info hash table up to date if they are available. Note that we
5094 may disable the hash tables if there is any error duing update. */
5095 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
5096 stash_maybe_update_info_hash_tables (stash
);
5098 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
5100 found
= stash_find_line_fast (stash
, symbol
, addr
, filename_ptr
,
5107 /* Check the previously read comp. units first. */
5108 for (each
= stash
->f
.all_comp_units
; each
; each
= each
->next_unit
)
5109 if ((symbol
->flags
& BSF_FUNCTION
) == 0
5110 || each
->arange
.high
== 0
5111 || comp_unit_contains_address (each
, addr
))
5113 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
5122 bfd_vma min_range
= (bfd_vma
) -1;
5123 const char * local_filename
= NULL
;
5124 struct funcinfo
*local_function
= NULL
;
5125 unsigned int local_linenumber
= 0;
5126 unsigned int local_discriminator
= 0;
5128 for (each
= stash
->f
.all_comp_units
; each
; each
= each
->next_unit
)
5130 bfd_vma range
= (bfd_vma
) -1;
5132 found
= ((each
->arange
.high
== 0
5133 || comp_unit_contains_address (each
, addr
))
5134 && (range
= (comp_unit_find_nearest_line
5135 (each
, addr
, &local_filename
,
5136 &local_function
, &local_linenumber
,
5137 &local_discriminator
))) != 0);
5140 /* PRs 15935 15994: Bogus debug information may have provided us
5141 with an erroneous match. We attempt to counter this by
5142 selecting the match that has the smallest address range
5143 associated with it. (We are assuming that corrupt debug info
5144 will tend to result in extra large address ranges rather than
5145 extra small ranges).
5147 This does mean that we scan through all of the CUs associated
5148 with the bfd each time this function is called. But this does
5149 have the benefit of producing consistent results every time the
5150 function is called. */
5151 if (range
<= min_range
)
5153 if (filename_ptr
&& local_filename
)
5154 * filename_ptr
= local_filename
;
5156 function
= local_function
;
5157 if (discriminator_ptr
&& local_discriminator
)
5158 * discriminator_ptr
= local_discriminator
;
5159 if (local_linenumber
)
5160 * linenumber_ptr
= local_linenumber
;
5166 if (* linenumber_ptr
)
5173 /* Read each remaining comp. units checking each as they are read. */
5174 while ((each
= stash_comp_unit (stash
, &stash
->f
)) != NULL
)
5176 /* DW_AT_low_pc and DW_AT_high_pc are optional for
5177 compilation units. If we don't have them (i.e.,
5178 unit->high == 0), we need to consult the line info table
5179 to see if a compilation unit contains the given
5182 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
5183 || each
->arange
.high
== 0
5184 || comp_unit_contains_address (each
, addr
))
5185 && comp_unit_find_line (each
, symbol
, addr
,
5186 filename_ptr
, linenumber_ptr
));
5188 found
= ((each
->arange
.high
== 0
5189 || comp_unit_contains_address (each
, addr
))
5190 && comp_unit_find_nearest_line (each
, addr
,
5194 discriminator_ptr
) != 0);
5201 if (functionname_ptr
&& function
&& function
->is_linkage
)
5202 *functionname_ptr
= function
->name
;
5203 else if (functionname_ptr
5204 && (!*functionname_ptr
5205 || (function
&& !function
->is_linkage
)))
5208 asymbol
**syms
= symbols
;
5209 asection
*sec
= section
;
5211 _bfd_dwarf2_stash_syms (stash
, abfd
, &sec
, &syms
);
5212 fun
= _bfd_elf_find_function (abfd
, syms
, sec
, offset
,
5213 *filename_ptr
? NULL
: filename_ptr
,
5216 if (!found
&& fun
!= NULL
)
5219 if (function
&& !function
->is_linkage
)
5223 sec_vma
= section
->vma
;
5224 if (section
->output_section
!= NULL
)
5225 sec_vma
= section
->output_section
->vma
+ section
->output_offset
;
5227 && fun
->value
+ sec_vma
== function
->arange
.low
)
5228 function
->name
= *functionname_ptr
;
5229 /* Even if we didn't find a linkage name, say that we have
5230 to stop a repeated search of symbols. */
5231 function
->is_linkage
= TRUE
;
5235 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
5236 unset_sections (stash
);
5242 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
5243 const char **filename_ptr
,
5244 const char **functionname_ptr
,
5245 unsigned int *linenumber_ptr
,
5248 struct dwarf2_debug
*stash
;
5250 stash
= (struct dwarf2_debug
*) *pinfo
;
5253 struct funcinfo
*func
= stash
->inliner_chain
;
5255 if (func
&& func
->caller_func
)
5257 *filename_ptr
= func
->caller_file
;
5258 *functionname_ptr
= func
->caller_func
->name
;
5259 *linenumber_ptr
= func
->caller_line
;
5260 stash
->inliner_chain
= func
->caller_func
;
5269 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
, void **pinfo
)
5271 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
5272 struct comp_unit
*each
;
5273 struct dwarf2_debug_file
*file
;
5275 if (abfd
== NULL
|| stash
== NULL
)
5278 if (stash
->varinfo_hash_table
)
5279 bfd_hash_table_free (&stash
->varinfo_hash_table
->base
);
5280 if (stash
->funcinfo_hash_table
)
5281 bfd_hash_table_free (&stash
->funcinfo_hash_table
->base
);
5286 for (each
= file
->all_comp_units
; each
; each
= each
->next_unit
)
5288 struct funcinfo
*function_table
= each
->function_table
;
5289 struct varinfo
*variable_table
= each
->variable_table
;
5291 if (each
->line_table
&& each
->line_table
!= file
->line_table
)
5293 free (each
->line_table
->files
);
5294 free (each
->line_table
->dirs
);
5297 free (each
->lookup_funcinfo_table
);
5298 each
->lookup_funcinfo_table
= NULL
;
5300 while (function_table
)
5302 free (function_table
->file
);
5303 function_table
->file
= NULL
;
5304 free (function_table
->caller_file
);
5305 function_table
->caller_file
= NULL
;
5306 function_table
= function_table
->prev_func
;
5309 while (variable_table
)
5311 free (variable_table
->file
);
5312 variable_table
->file
= NULL
;
5313 variable_table
= variable_table
->prev_var
;
5317 if (file
->line_table
)
5319 free (file
->line_table
->files
);
5320 free (file
->line_table
->dirs
);
5322 htab_delete (file
->abbrev_offsets
);
5324 free (file
->dwarf_line_str_buffer
);
5325 free (file
->dwarf_str_buffer
);
5326 free (file
->dwarf_ranges_buffer
);
5327 free (file
->dwarf_line_buffer
);
5328 free (file
->dwarf_abbrev_buffer
);
5329 free (file
->dwarf_info_buffer
);
5330 if (file
== &stash
->alt
)
5334 free (stash
->sec_vma
);
5335 free (stash
->adjusted_sections
);
5336 if (stash
->close_on_cleanup
)
5337 bfd_close (stash
->f
.bfd_ptr
);
5338 if (stash
->alt
.bfd_ptr
)
5339 bfd_close (stash
->alt
.bfd_ptr
);
5342 /* Find the function to a particular section and offset,
5343 for error reporting. */
5346 _bfd_elf_find_function (bfd
*abfd
,
5350 const char **filename_ptr
,
5351 const char **functionname_ptr
)
5353 struct elf_find_function_cache
5355 asection
*last_section
;
5357 const char *filename
;
5358 bfd_size_type func_size
;
5361 if (symbols
== NULL
)
5364 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
5367 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
5370 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
5371 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
5375 if (cache
->last_section
!= section
5376 || cache
->func
== NULL
5377 || offset
< cache
->func
->value
5378 || offset
>= cache
->func
->value
+ cache
->func_size
)
5383 /* ??? Given multiple file symbols, it is impossible to reliably
5384 choose the right file name for global symbols. File symbols are
5385 local symbols, and thus all file symbols must sort before any
5386 global symbols. The ELF spec may be interpreted to say that a
5387 file symbol must sort before other local symbols, but currently
5388 ld -r doesn't do this. So, for ld -r output, it is possible to
5389 make a better choice of file name for local symbols by ignoring
5390 file symbols appearing after a given local symbol. */
5391 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
5392 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5396 state
= nothing_seen
;
5397 cache
->filename
= NULL
;
5399 cache
->func_size
= 0;
5400 cache
->last_section
= section
;
5402 for (p
= symbols
; *p
!= NULL
; p
++)
5408 if ((sym
->flags
& BSF_FILE
) != 0)
5411 if (state
== symbol_seen
)
5412 state
= file_after_symbol_seen
;
5416 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
5418 && code_off
<= offset
5419 && (code_off
> low_func
5420 || (code_off
== low_func
5421 && size
> cache
->func_size
)))
5424 cache
->func_size
= size
;
5425 cache
->filename
= NULL
;
5426 low_func
= code_off
;
5428 && ((sym
->flags
& BSF_LOCAL
) != 0
5429 || state
!= file_after_symbol_seen
))
5430 cache
->filename
= bfd_asymbol_name (file
);
5432 if (state
== nothing_seen
)
5433 state
= symbol_seen
;
5437 if (cache
->func
== NULL
)
5441 *filename_ptr
= cache
->filename
;
5442 if (functionname_ptr
)
5443 *functionname_ptr
= bfd_asymbol_name (cache
->func
);