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
)
535 const char *section_name
= sec
->uncompressed_name
;
536 bfd_byte
*contents
= *section_buffer
;
539 /* The section may have already been read. */
540 if (contents
== NULL
)
542 msec
= bfd_get_section_by_name (abfd
, section_name
);
545 section_name
= sec
->compressed_name
;
546 if (section_name
!= NULL
)
547 msec
= bfd_get_section_by_name (abfd
, section_name
);
551 _bfd_error_handler (_("DWARF error: can't find %s section."),
552 sec
->uncompressed_name
);
553 bfd_set_error (bfd_error_bad_value
);
557 *section_size
= msec
->rawsize
? msec
->rawsize
: msec
->size
;
558 /* Paranoia - alloc one extra so that we can make sure a string
559 section is NUL terminated. */
560 amt
= *section_size
+ 1;
563 bfd_set_error (bfd_error_no_memory
);
566 contents
= (bfd_byte
*) bfd_malloc (amt
);
567 if (contents
== NULL
)
570 ? !bfd_simple_get_relocated_section_contents (abfd
, msec
, contents
,
572 : !bfd_get_section_contents (abfd
, msec
, contents
, 0, *section_size
))
577 contents
[*section_size
] = 0;
578 *section_buffer
= contents
;
581 /* It is possible to get a bad value for the offset into the section
582 that the client wants. Validate it here to avoid trouble later. */
583 if (offset
!= 0 && offset
>= *section_size
)
585 /* xgettext: c-format */
586 _bfd_error_handler (_("DWARF error: offset (%" PRIu64
")"
587 " greater than or equal to %s size (%" PRIu64
")"),
588 (uint64_t) offset
, section_name
,
589 (uint64_t) *section_size
);
590 bfd_set_error (bfd_error_bad_value
);
597 /* Read dwarf information from a buffer. */
600 read_1_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
, bfd_byte
*end
)
604 return bfd_get_8 (abfd
, buf
);
608 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
, bfd_byte
*end
)
612 return bfd_get_signed_8 (abfd
, buf
);
616 read_2_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
620 return bfd_get_16 (abfd
, buf
);
624 read_4_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
628 return bfd_get_32 (abfd
, buf
);
632 read_8_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
636 return bfd_get_64 (abfd
, buf
);
640 read_n_bytes (bfd_byte
* buf
,
642 struct dwarf_block
* block
)
644 unsigned int size
= block
->size
;
645 bfd_byte
* block_end
= buf
+ size
;
647 if (block_end
> end
|| block_end
< buf
)
660 /* Scans a NUL terminated string starting at BUF, returning a pointer to it.
661 Returns the number of characters in the string, *including* the NUL byte,
662 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
663 at or beyond BUF_END will not be read. Returns NULL if there was a
664 problem, or if the string is empty. */
667 read_string (bfd
* abfd ATTRIBUTE_UNUSED
,
670 unsigned int * bytes_read_ptr
)
676 * bytes_read_ptr
= 0;
682 * bytes_read_ptr
= 1;
686 while (buf
< buf_end
)
689 * bytes_read_ptr
= buf
- str
;
693 * bytes_read_ptr
= buf
- str
;
697 /* Reads an offset from BUF and then locates the string at this offset
698 inside the debug string section. Returns a pointer to the string.
699 Returns the number of bytes read from BUF, *not* the length of the string,
700 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
701 at or beyond BUF_END will not be read from BUF. Returns NULL if there was
702 a problem, or if the string is empty. Does not check for NUL termination
706 read_indirect_string (struct comp_unit
* unit
,
709 unsigned int * bytes_read_ptr
)
712 struct dwarf2_debug
*stash
= unit
->stash
;
713 struct dwarf2_debug_file
*file
= unit
->file
;
716 if (buf
+ unit
->offset_size
> buf_end
)
718 * bytes_read_ptr
= 0;
722 if (unit
->offset_size
== 4)
723 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
725 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
727 *bytes_read_ptr
= unit
->offset_size
;
729 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
731 &file
->dwarf_str_buffer
, &file
->dwarf_str_size
))
734 str
= (char *) file
->dwarf_str_buffer
+ offset
;
740 /* Like read_indirect_string but from .debug_line_str section. */
743 read_indirect_line_string (struct comp_unit
* unit
,
746 unsigned int * bytes_read_ptr
)
749 struct dwarf2_debug
*stash
= unit
->stash
;
750 struct dwarf2_debug_file
*file
= unit
->file
;
753 if (buf
+ unit
->offset_size
> buf_end
)
755 * bytes_read_ptr
= 0;
759 if (unit
->offset_size
== 4)
760 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
762 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
764 *bytes_read_ptr
= unit
->offset_size
;
766 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_line_str
],
768 &file
->dwarf_line_str_buffer
,
769 &file
->dwarf_line_str_size
))
772 str
= (char *) file
->dwarf_line_str_buffer
+ offset
;
778 /* Like read_indirect_string but uses a .debug_str located in
779 an alternate file pointed to by the .gnu_debugaltlink section.
780 Used to impement DW_FORM_GNU_strp_alt. */
783 read_alt_indirect_string (struct comp_unit
* unit
,
786 unsigned int * bytes_read_ptr
)
789 struct dwarf2_debug
*stash
= unit
->stash
;
792 if (buf
+ unit
->offset_size
> buf_end
)
794 * bytes_read_ptr
= 0;
798 if (unit
->offset_size
== 4)
799 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
801 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
803 *bytes_read_ptr
= unit
->offset_size
;
805 if (stash
->alt
.bfd_ptr
== NULL
)
808 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
810 if (debug_filename
== NULL
)
813 debug_bfd
= bfd_openr (debug_filename
, NULL
);
814 free (debug_filename
);
815 if (debug_bfd
== NULL
)
816 /* FIXME: Should we report our failure to follow the debuglink ? */
819 if (!bfd_check_format (debug_bfd
, bfd_object
))
821 bfd_close (debug_bfd
);
824 stash
->alt
.bfd_ptr
= debug_bfd
;
827 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
828 stash
->debug_sections
+ debug_str_alt
,
829 stash
->alt
.syms
, offset
,
830 &stash
->alt
.dwarf_str_buffer
,
831 &stash
->alt
.dwarf_str_size
))
834 str
= (char *) stash
->alt
.dwarf_str_buffer
+ offset
;
841 /* Resolve an alternate reference from UNIT at OFFSET.
842 Returns a pointer into the loaded alternate CU upon success
843 or NULL upon failure. */
846 read_alt_indirect_ref (struct comp_unit
* unit
,
849 struct dwarf2_debug
*stash
= unit
->stash
;
851 if (stash
->alt
.bfd_ptr
== NULL
)
854 char *debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
856 if (debug_filename
== NULL
)
859 debug_bfd
= bfd_openr (debug_filename
, NULL
);
860 free (debug_filename
);
861 if (debug_bfd
== NULL
)
862 /* FIXME: Should we report our failure to follow the debuglink ? */
865 if (!bfd_check_format (debug_bfd
, bfd_object
))
867 bfd_close (debug_bfd
);
870 stash
->alt
.bfd_ptr
= debug_bfd
;
873 if (! read_section (unit
->stash
->alt
.bfd_ptr
,
874 stash
->debug_sections
+ debug_info_alt
,
875 stash
->alt
.syms
, offset
,
876 &stash
->alt
.dwarf_info_buffer
,
877 &stash
->alt
.dwarf_info_size
))
880 return stash
->alt
.dwarf_info_buffer
+ offset
;
884 read_address (struct comp_unit
*unit
, bfd_byte
*buf
, bfd_byte
* buf_end
)
888 if (bfd_get_flavour (unit
->abfd
) == bfd_target_elf_flavour
)
889 signed_vma
= get_elf_backend_data (unit
->abfd
)->sign_extend_vma
;
891 if (buf
+ unit
->addr_size
> buf_end
)
896 switch (unit
->addr_size
)
899 return bfd_get_signed_64 (unit
->abfd
, buf
);
901 return bfd_get_signed_32 (unit
->abfd
, buf
);
903 return bfd_get_signed_16 (unit
->abfd
, buf
);
910 switch (unit
->addr_size
)
913 return bfd_get_64 (unit
->abfd
, buf
);
915 return bfd_get_32 (unit
->abfd
, buf
);
917 return bfd_get_16 (unit
->abfd
, buf
);
924 /* Lookup an abbrev_info structure in the abbrev hash table. */
926 static struct abbrev_info
*
927 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
929 unsigned int hash_number
;
930 struct abbrev_info
*abbrev
;
932 hash_number
= number
% ABBREV_HASH_SIZE
;
933 abbrev
= abbrevs
[hash_number
];
937 if (abbrev
->number
== number
)
940 abbrev
= abbrev
->next
;
946 /* We keep a hash table to map .debug_abbrev section offsets to the
947 array of abbrevs, so that compilation units using the same set of
948 abbrevs do not waste memory. */
950 struct abbrev_offset_entry
953 struct abbrev_info
**abbrevs
;
957 hash_abbrev (const void *p
)
959 const struct abbrev_offset_entry
*ent
= p
;
960 return htab_hash_pointer ((void *) ent
->offset
);
964 eq_abbrev (const void *pa
, const void *pb
)
966 const struct abbrev_offset_entry
*a
= pa
;
967 const struct abbrev_offset_entry
*b
= pb
;
968 return a
->offset
== b
->offset
;
974 struct abbrev_offset_entry
*ent
= p
;
975 struct abbrev_info
**abbrevs
= ent
->abbrevs
;
978 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
980 struct abbrev_info
*abbrev
= abbrevs
[i
];
984 free (abbrev
->attrs
);
985 abbrev
= abbrev
->next
;
991 /* In DWARF version 2, the description of the debugging information is
992 stored in a separate .debug_abbrev section. Before we read any
993 dies from a section we read in all abbreviations and install them
996 static struct abbrev_info
**
997 read_abbrevs (bfd
*abfd
, bfd_uint64_t offset
, struct dwarf2_debug
*stash
,
998 struct dwarf2_debug_file
*file
)
1000 struct abbrev_info
**abbrevs
;
1001 bfd_byte
*abbrev_ptr
;
1002 bfd_byte
*abbrev_end
;
1003 struct abbrev_info
*cur_abbrev
;
1004 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
1005 unsigned int abbrev_form
, hash_number
;
1008 struct abbrev_offset_entry ent
= { offset
, NULL
};
1010 if (ent
.offset
!= offset
)
1013 slot
= htab_find_slot (file
->abbrev_offsets
, &ent
, INSERT
);
1017 return ((struct abbrev_offset_entry
*) (*slot
))->abbrevs
;
1019 if (! read_section (abfd
, &stash
->debug_sections
[debug_abbrev
],
1021 &file
->dwarf_abbrev_buffer
,
1022 &file
->dwarf_abbrev_size
))
1025 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
1026 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
1027 if (abbrevs
== NULL
)
1030 abbrev_ptr
= file
->dwarf_abbrev_buffer
+ offset
;
1031 abbrev_end
= file
->dwarf_abbrev_buffer
+ file
->dwarf_abbrev_size
;
1032 abbrev_number
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1034 abbrev_ptr
+= bytes_read
;
1036 /* Loop until we reach an abbrev number of 0. */
1037 while (abbrev_number
)
1039 amt
= sizeof (struct abbrev_info
);
1040 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
1041 if (cur_abbrev
== NULL
)
1044 /* Read in abbrev header. */
1045 cur_abbrev
->number
= abbrev_number
;
1046 cur_abbrev
->tag
= (enum dwarf_tag
)
1047 _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1049 abbrev_ptr
+= bytes_read
;
1050 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
, abbrev_end
);
1053 /* Now read in declarations. */
1056 /* Initialize it just to avoid a GCC false warning. */
1057 bfd_vma implicit_const
= -1;
1059 abbrev_name
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1061 abbrev_ptr
+= bytes_read
;
1062 abbrev_form
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1064 abbrev_ptr
+= bytes_read
;
1065 if (abbrev_form
== DW_FORM_implicit_const
)
1067 implicit_const
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
,
1070 abbrev_ptr
+= bytes_read
;
1073 if (abbrev_name
== 0)
1076 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
1078 struct attr_abbrev
*tmp
;
1080 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
1081 amt
*= sizeof (struct attr_abbrev
);
1082 tmp
= (struct attr_abbrev
*) bfd_realloc (cur_abbrev
->attrs
, amt
);
1085 cur_abbrev
->attrs
= tmp
;
1088 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
1089 = (enum dwarf_attribute
) abbrev_name
;
1090 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].form
1091 = (enum dwarf_form
) abbrev_form
;
1092 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].implicit_const
1094 ++cur_abbrev
->num_attrs
;
1097 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
1098 cur_abbrev
->next
= abbrevs
[hash_number
];
1099 abbrevs
[hash_number
] = cur_abbrev
;
1101 /* Get next abbreviation.
1102 Under Irix6 the abbreviations for a compilation unit are not
1103 always properly terminated with an abbrev number of 0.
1104 Exit loop if we encounter an abbreviation which we have
1105 already read (which means we are about to read the abbreviations
1106 for the next compile unit) or if the end of the abbreviation
1107 table is reached. */
1108 if ((size_t) (abbrev_ptr
- file
->dwarf_abbrev_buffer
)
1109 >= file
->dwarf_abbrev_size
)
1111 abbrev_number
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
,
1112 &bytes_read
, FALSE
, abbrev_end
);
1113 abbrev_ptr
+= bytes_read
;
1114 if (lookup_abbrev (abbrev_number
, abbrevs
) != NULL
)
1118 *slot
= bfd_malloc (sizeof ent
);
1121 ent
.abbrevs
= abbrevs
;
1122 memcpy (*slot
, &ent
, sizeof ent
);
1126 if (abbrevs
!= NULL
)
1130 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1132 struct abbrev_info
*abbrev
= abbrevs
[i
];
1136 free (abbrev
->attrs
);
1137 abbrev
= abbrev
->next
;
1145 /* Returns true if the form is one which has a string value. */
1147 static inline bfd_boolean
1148 is_str_attr (enum dwarf_form form
)
1150 return (form
== DW_FORM_string
|| form
== DW_FORM_strp
1151 || form
== DW_FORM_line_strp
|| form
== DW_FORM_GNU_strp_alt
);
1154 /* Read and fill in the value of attribute ATTR as described by FORM.
1155 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1156 Returns an updated INFO_PTR taking into account the amount of data read. */
1159 read_attribute_value (struct attribute
* attr
,
1161 bfd_vma implicit_const
,
1162 struct comp_unit
* unit
,
1163 bfd_byte
* info_ptr
,
1164 bfd_byte
* info_ptr_end
)
1166 bfd
*abfd
= unit
->abfd
;
1167 unsigned int bytes_read
;
1168 struct dwarf_block
*blk
;
1171 if (info_ptr
>= info_ptr_end
&& form
!= DW_FORM_flag_present
)
1173 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1174 bfd_set_error (bfd_error_bad_value
);
1178 attr
->form
= (enum dwarf_form
) form
;
1182 case DW_FORM_ref_addr
:
1183 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1185 if (unit
->version
== 3 || unit
->version
== 4)
1187 if (unit
->offset_size
== 4)
1188 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1190 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1191 info_ptr
+= unit
->offset_size
;
1196 attr
->u
.val
= read_address (unit
, info_ptr
, info_ptr_end
);
1197 info_ptr
+= unit
->addr_size
;
1199 case DW_FORM_GNU_ref_alt
:
1200 case DW_FORM_sec_offset
:
1201 if (unit
->offset_size
== 4)
1202 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1204 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1205 info_ptr
+= unit
->offset_size
;
1207 case DW_FORM_block2
:
1208 amt
= sizeof (struct dwarf_block
);
1209 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1212 blk
->size
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1214 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1217 case DW_FORM_block4
:
1218 amt
= sizeof (struct dwarf_block
);
1219 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1222 blk
->size
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1224 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1228 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1232 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1236 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1239 case DW_FORM_string
:
1240 attr
->u
.str
= read_string (abfd
, info_ptr
, info_ptr_end
, &bytes_read
);
1241 info_ptr
+= bytes_read
;
1244 attr
->u
.str
= read_indirect_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1245 info_ptr
+= bytes_read
;
1247 case DW_FORM_line_strp
:
1248 attr
->u
.str
= read_indirect_line_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1249 info_ptr
+= bytes_read
;
1251 case DW_FORM_GNU_strp_alt
:
1252 attr
->u
.str
= read_alt_indirect_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1253 info_ptr
+= bytes_read
;
1255 case DW_FORM_exprloc
:
1257 amt
= sizeof (struct dwarf_block
);
1258 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1261 blk
->size
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1262 FALSE
, info_ptr_end
);
1263 info_ptr
+= bytes_read
;
1264 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1267 case DW_FORM_block1
:
1268 amt
= sizeof (struct dwarf_block
);
1269 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1272 blk
->size
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1274 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1278 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1282 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1285 case DW_FORM_flag_present
:
1289 attr
->u
.sval
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1290 TRUE
, info_ptr_end
);
1291 info_ptr
+= bytes_read
;
1294 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1295 FALSE
, info_ptr_end
);
1296 info_ptr
+= bytes_read
;
1299 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1303 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1307 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1311 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1314 case DW_FORM_ref_sig8
:
1315 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1318 case DW_FORM_ref_udata
:
1319 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1320 FALSE
, info_ptr_end
);
1321 info_ptr
+= bytes_read
;
1323 case DW_FORM_indirect
:
1324 form
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1325 FALSE
, info_ptr_end
);
1326 info_ptr
+= bytes_read
;
1327 if (form
== DW_FORM_implicit_const
)
1329 implicit_const
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1330 TRUE
, info_ptr_end
);
1331 info_ptr
+= bytes_read
;
1333 info_ptr
= read_attribute_value (attr
, form
, implicit_const
, unit
,
1334 info_ptr
, info_ptr_end
);
1336 case DW_FORM_implicit_const
:
1337 attr
->form
= DW_FORM_sdata
;
1338 attr
->u
.sval
= implicit_const
;
1340 case DW_FORM_data16
:
1341 /* This is really a "constant", but there is no way to store that
1342 so pretend it is a 16 byte block instead. */
1343 amt
= sizeof (struct dwarf_block
);
1344 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1348 info_ptr
= read_n_bytes (info_ptr
, info_ptr_end
, blk
);
1352 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1354 bfd_set_error (bfd_error_bad_value
);
1360 /* Read an attribute described by an abbreviated attribute. */
1363 read_attribute (struct attribute
* attr
,
1364 struct attr_abbrev
* abbrev
,
1365 struct comp_unit
* unit
,
1366 bfd_byte
* info_ptr
,
1367 bfd_byte
* info_ptr_end
)
1369 attr
->name
= abbrev
->name
;
1370 info_ptr
= read_attribute_value (attr
, abbrev
->form
, abbrev
->implicit_const
,
1371 unit
, info_ptr
, info_ptr_end
);
1375 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1379 non_mangled (int lang
)
1389 case DW_LANG_Cobol74
:
1390 case DW_LANG_Cobol85
:
1391 case DW_LANG_Fortran77
:
1392 case DW_LANG_Pascal83
:
1402 /* Source line information table routines. */
1404 #define FILE_ALLOC_CHUNK 5
1405 #define DIR_ALLOC_CHUNK 5
1409 struct line_info
* prev_line
;
1413 unsigned int column
;
1414 unsigned int discriminator
;
1415 unsigned char op_index
;
1416 unsigned char end_sequence
; /* End of (sequential) code sequence. */
1427 struct line_sequence
1430 struct line_sequence
* prev_sequence
;
1431 struct line_info
* last_line
; /* Largest VMA. */
1432 struct line_info
** line_info_lookup
;
1433 bfd_size_type num_lines
;
1436 struct line_info_table
1439 unsigned int num_files
;
1440 unsigned int num_dirs
;
1441 unsigned int num_sequences
;
1444 struct fileinfo
* files
;
1445 struct line_sequence
* sequences
;
1446 struct line_info
* lcl_head
; /* Local head; used in 'add_line_info'. */
1449 /* Remember some information about each function. If the function is
1450 inlined (DW_TAG_inlined_subroutine) it may have two additional
1451 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1452 source code location where this function was inlined. */
1456 /* Pointer to previous function in list of all functions. */
1457 struct funcinfo
* prev_func
;
1458 /* Pointer to function one scope higher. */
1459 struct funcinfo
* caller_func
;
1460 /* Source location file name where caller_func inlines this func. */
1462 /* Source location file name. */
1464 /* Source location line number where caller_func inlines this func. */
1466 /* Source location line number. */
1469 bfd_boolean is_linkage
;
1471 struct arange arange
;
1472 /* Where the symbol is defined. */
1476 struct lookup_funcinfo
1478 /* Function information corresponding to this lookup table entry. */
1479 struct funcinfo
* funcinfo
;
1481 /* The lowest address for this specific function. */
1484 /* The highest address of this function before the lookup table is sorted.
1485 The highest address of all prior functions after the lookup table is
1486 sorted, which is used for binary search. */
1488 /* Index of this function, used to ensure qsort is stable. */
1494 /* Pointer to previous variable in list of all variables. */
1495 struct varinfo
*prev_var
;
1496 /* The offset of the varinfo from the start of the unit. */
1497 bfd_uint64_t unit_offset
;
1498 /* Source location file name. */
1500 /* Source location line number. */
1502 /* The type of this variable. */
1504 /* The name of the variable, if it has one. */
1506 /* The address of the variable. */
1508 /* Where the symbol is defined. */
1510 /* Is this a stack variable? */
1514 /* Return TRUE if NEW_LINE should sort after LINE. */
1516 static inline bfd_boolean
1517 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
1519 return (new_line
->address
> line
->address
1520 || (new_line
->address
== line
->address
1521 && new_line
->op_index
> line
->op_index
));
1525 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1526 that the list is sorted. Note that the line_info list is sorted from
1527 highest to lowest VMA (with possible duplicates); that is,
1528 line_info->prev_line always accesses an equal or smaller VMA. */
1531 add_line_info (struct line_info_table
*table
,
1533 unsigned char op_index
,
1536 unsigned int column
,
1537 unsigned int discriminator
,
1540 size_t amt
= sizeof (struct line_info
);
1541 struct line_sequence
* seq
= table
->sequences
;
1542 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
1547 /* Set member data of 'info'. */
1548 info
->prev_line
= NULL
;
1549 info
->address
= address
;
1550 info
->op_index
= op_index
;
1552 info
->column
= column
;
1553 info
->discriminator
= discriminator
;
1554 info
->end_sequence
= end_sequence
;
1556 if (filename
&& filename
[0])
1558 info
->filename
= (char *) bfd_alloc (table
->abfd
, strlen (filename
) + 1);
1559 if (info
->filename
== NULL
)
1561 strcpy (info
->filename
, filename
);
1564 info
->filename
= NULL
;
1566 /* Find the correct location for 'info'. Normally we will receive
1567 new line_info data 1) in order and 2) with increasing VMAs.
1568 However some compilers break the rules (cf. decode_line_info) and
1569 so we include some heuristics for quickly finding the correct
1570 location for 'info'. In particular, these heuristics optimize for
1571 the common case in which the VMA sequence that we receive is a
1572 list of locally sorted VMAs such as
1573 p...z a...j (where a < j < p < z)
1575 Note: table->lcl_head is used to head an *actual* or *possible*
1576 sub-sequence within the list (such as a...j) that is not directly
1577 headed by table->last_line
1579 Note: we may receive duplicate entries from 'decode_line_info'. */
1582 && seq
->last_line
->address
== address
1583 && seq
->last_line
->op_index
== op_index
1584 && seq
->last_line
->end_sequence
== end_sequence
)
1586 /* We only keep the last entry with the same address and end
1587 sequence. See PR ld/4986. */
1588 if (table
->lcl_head
== seq
->last_line
)
1589 table
->lcl_head
= info
;
1590 info
->prev_line
= seq
->last_line
->prev_line
;
1591 seq
->last_line
= info
;
1593 else if (!seq
|| seq
->last_line
->end_sequence
)
1595 /* Start a new line sequence. */
1596 amt
= sizeof (struct line_sequence
);
1597 seq
= (struct line_sequence
*) bfd_malloc (amt
);
1600 seq
->low_pc
= address
;
1601 seq
->prev_sequence
= table
->sequences
;
1602 seq
->last_line
= info
;
1603 table
->lcl_head
= info
;
1604 table
->sequences
= seq
;
1605 table
->num_sequences
++;
1607 else if (info
->end_sequence
1608 || new_line_sorts_after (info
, seq
->last_line
))
1610 /* Normal case: add 'info' to the beginning of the current sequence. */
1611 info
->prev_line
= seq
->last_line
;
1612 seq
->last_line
= info
;
1614 /* lcl_head: initialize to head a *possible* sequence at the end. */
1615 if (!table
->lcl_head
)
1616 table
->lcl_head
= info
;
1618 else if (!new_line_sorts_after (info
, table
->lcl_head
)
1619 && (!table
->lcl_head
->prev_line
1620 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
1622 /* Abnormal but easy: lcl_head is the head of 'info'. */
1623 info
->prev_line
= table
->lcl_head
->prev_line
;
1624 table
->lcl_head
->prev_line
= info
;
1628 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1629 are valid heads for 'info'. Reset 'lcl_head'. */
1630 struct line_info
* li2
= seq
->last_line
; /* Always non-NULL. */
1631 struct line_info
* li1
= li2
->prev_line
;
1635 if (!new_line_sorts_after (info
, li2
)
1636 && new_line_sorts_after (info
, li1
))
1639 li2
= li1
; /* always non-NULL */
1640 li1
= li1
->prev_line
;
1642 table
->lcl_head
= li2
;
1643 info
->prev_line
= table
->lcl_head
->prev_line
;
1644 table
->lcl_head
->prev_line
= info
;
1645 if (address
< seq
->low_pc
)
1646 seq
->low_pc
= address
;
1651 /* Extract a fully qualified filename from a line info table.
1652 The returned string has been malloc'ed and it is the caller's
1653 responsibility to free it. */
1656 concat_filename (struct line_info_table
*table
, unsigned int file
)
1660 if (table
== NULL
|| file
- 1 >= table
->num_files
)
1662 /* FILE == 0 means unknown. */
1665 (_("DWARF error: mangled line number section (bad file number)"));
1666 return strdup ("<unknown>");
1669 filename
= table
->files
[file
- 1].name
;
1670 if (filename
== NULL
)
1671 return strdup ("<unknown>");
1673 if (!IS_ABSOLUTE_PATH (filename
))
1675 char *dir_name
= NULL
;
1676 char *subdir_name
= NULL
;
1680 if (table
->files
[file
- 1].dir
1681 /* PR 17512: file: 0317e960. */
1682 && table
->files
[file
- 1].dir
<= table
->num_dirs
1683 /* PR 17512: file: 7f3d2e4b. */
1684 && table
->dirs
!= NULL
)
1685 subdir_name
= table
->dirs
[table
->files
[file
- 1].dir
- 1];
1687 if (!subdir_name
|| !IS_ABSOLUTE_PATH (subdir_name
))
1688 dir_name
= table
->comp_dir
;
1692 dir_name
= subdir_name
;
1697 return strdup (filename
);
1699 len
= strlen (dir_name
) + strlen (filename
) + 2;
1703 len
+= strlen (subdir_name
) + 1;
1704 name
= (char *) bfd_malloc (len
);
1706 sprintf (name
, "%s/%s/%s", dir_name
, subdir_name
, filename
);
1710 name
= (char *) bfd_malloc (len
);
1712 sprintf (name
, "%s/%s", dir_name
, filename
);
1718 return strdup (filename
);
1722 arange_add (const struct comp_unit
*unit
, struct arange
*first_arange
,
1723 bfd_vma low_pc
, bfd_vma high_pc
)
1725 struct arange
*arange
;
1727 /* Ignore empty ranges. */
1728 if (low_pc
== high_pc
)
1731 /* If the first arange is empty, use it. */
1732 if (first_arange
->high
== 0)
1734 first_arange
->low
= low_pc
;
1735 first_arange
->high
= high_pc
;
1739 /* Next see if we can cheaply extend an existing range. */
1740 arange
= first_arange
;
1743 if (low_pc
== arange
->high
)
1745 arange
->high
= high_pc
;
1748 if (high_pc
== arange
->low
)
1750 arange
->low
= low_pc
;
1753 arange
= arange
->next
;
1757 /* Need to allocate a new arange and insert it into the arange list.
1758 Order isn't significant, so just insert after the first arange. */
1759 arange
= (struct arange
*) bfd_alloc (unit
->abfd
, sizeof (*arange
));
1762 arange
->low
= low_pc
;
1763 arange
->high
= high_pc
;
1764 arange
->next
= first_arange
->next
;
1765 first_arange
->next
= arange
;
1769 /* Compare function for line sequences. */
1772 compare_sequences (const void* a
, const void* b
)
1774 const struct line_sequence
* seq1
= a
;
1775 const struct line_sequence
* seq2
= b
;
1777 /* Sort by low_pc as the primary key. */
1778 if (seq1
->low_pc
< seq2
->low_pc
)
1780 if (seq1
->low_pc
> seq2
->low_pc
)
1783 /* If low_pc values are equal, sort in reverse order of
1784 high_pc, so that the largest region comes first. */
1785 if (seq1
->last_line
->address
< seq2
->last_line
->address
)
1787 if (seq1
->last_line
->address
> seq2
->last_line
->address
)
1790 if (seq1
->last_line
->op_index
< seq2
->last_line
->op_index
)
1792 if (seq1
->last_line
->op_index
> seq2
->last_line
->op_index
)
1795 /* num_lines is initially an index, to make the sort stable. */
1796 if (seq1
->num_lines
< seq2
->num_lines
)
1798 if (seq1
->num_lines
> seq2
->num_lines
)
1803 /* Construct the line information table for quick lookup. */
1806 build_line_info_table (struct line_info_table
* table
,
1807 struct line_sequence
* seq
)
1810 struct line_info
**line_info_lookup
;
1811 struct line_info
*each_line
;
1812 unsigned int num_lines
;
1813 unsigned int line_index
;
1815 if (seq
->line_info_lookup
!= NULL
)
1818 /* Count the number of line information entries. We could do this while
1819 scanning the debug information, but some entries may be added via
1820 lcl_head without having a sequence handy to increment the number of
1823 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
1826 seq
->num_lines
= num_lines
;
1830 /* Allocate space for the line information lookup table. */
1831 amt
= sizeof (struct line_info
*) * num_lines
;
1832 line_info_lookup
= (struct line_info
**) bfd_alloc (table
->abfd
, amt
);
1833 seq
->line_info_lookup
= line_info_lookup
;
1834 if (line_info_lookup
== NULL
)
1837 /* Create the line information lookup table. */
1838 line_index
= num_lines
;
1839 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
1840 line_info_lookup
[--line_index
] = each_line
;
1842 BFD_ASSERT (line_index
== 0);
1846 /* Sort the line sequences for quick lookup. */
1849 sort_line_sequences (struct line_info_table
* table
)
1852 struct line_sequence
*sequences
;
1853 struct line_sequence
*seq
;
1855 unsigned int num_sequences
= table
->num_sequences
;
1856 bfd_vma last_high_pc
;
1858 if (num_sequences
== 0)
1861 /* Allocate space for an array of sequences. */
1862 amt
= sizeof (struct line_sequence
) * num_sequences
;
1863 sequences
= (struct line_sequence
*) bfd_alloc (table
->abfd
, amt
);
1864 if (sequences
== NULL
)
1867 /* Copy the linked list into the array, freeing the original nodes. */
1868 seq
= table
->sequences
;
1869 for (n
= 0; n
< num_sequences
; n
++)
1871 struct line_sequence
* last_seq
= seq
;
1874 sequences
[n
].low_pc
= seq
->low_pc
;
1875 sequences
[n
].prev_sequence
= NULL
;
1876 sequences
[n
].last_line
= seq
->last_line
;
1877 sequences
[n
].line_info_lookup
= NULL
;
1878 sequences
[n
].num_lines
= n
;
1879 seq
= seq
->prev_sequence
;
1882 BFD_ASSERT (seq
== NULL
);
1884 qsort (sequences
, n
, sizeof (struct line_sequence
), compare_sequences
);
1886 /* Make the list binary-searchable by trimming overlapping entries
1887 and removing nested entries. */
1889 last_high_pc
= sequences
[0].last_line
->address
;
1890 for (n
= 1; n
< table
->num_sequences
; n
++)
1892 if (sequences
[n
].low_pc
< last_high_pc
)
1894 if (sequences
[n
].last_line
->address
<= last_high_pc
)
1895 /* Skip nested entries. */
1898 /* Trim overlapping entries. */
1899 sequences
[n
].low_pc
= last_high_pc
;
1901 last_high_pc
= sequences
[n
].last_line
->address
;
1902 if (n
> num_sequences
)
1904 /* Close up the gap. */
1905 sequences
[num_sequences
].low_pc
= sequences
[n
].low_pc
;
1906 sequences
[num_sequences
].last_line
= sequences
[n
].last_line
;
1911 table
->sequences
= sequences
;
1912 table
->num_sequences
= num_sequences
;
1916 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
1919 line_info_add_include_dir (struct line_info_table
*table
, char *cur_dir
)
1921 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1926 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1927 amt
*= sizeof (char *);
1929 tmp
= (char **) bfd_realloc (table
->dirs
, amt
);
1935 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1940 line_info_add_include_dir_stub (struct line_info_table
*table
, char *cur_dir
,
1941 unsigned int dir ATTRIBUTE_UNUSED
,
1942 unsigned int xtime ATTRIBUTE_UNUSED
,
1943 unsigned int size ATTRIBUTE_UNUSED
)
1945 return line_info_add_include_dir (table
, cur_dir
);
1948 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
1951 line_info_add_file_name (struct line_info_table
*table
, char *cur_file
,
1952 unsigned int dir
, unsigned int xtime
,
1955 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1957 struct fileinfo
*tmp
;
1960 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1961 amt
*= sizeof (struct fileinfo
);
1963 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1969 table
->files
[table
->num_files
].name
= cur_file
;
1970 table
->files
[table
->num_files
].dir
= dir
;
1971 table
->files
[table
->num_files
].time
= xtime
;
1972 table
->files
[table
->num_files
].size
= size
;
1977 /* Read directory or file name entry format, starting with byte of
1978 format count entries, ULEB128 pairs of entry formats, ULEB128 of
1979 entries count and the entries themselves in the described entry
1983 read_formatted_entries (struct comp_unit
*unit
, bfd_byte
**bufp
,
1984 bfd_byte
*buf_end
, struct line_info_table
*table
,
1985 bfd_boolean (*callback
) (struct line_info_table
*table
,
1991 bfd
*abfd
= unit
->abfd
;
1992 bfd_byte format_count
, formati
;
1993 bfd_vma data_count
, datai
;
1994 bfd_byte
*buf
= *bufp
;
1995 bfd_byte
*format_header_data
;
1996 unsigned int bytes_read
;
1998 format_count
= read_1_byte (abfd
, buf
, buf_end
);
2000 format_header_data
= buf
;
2001 for (formati
= 0; formati
< format_count
; formati
++)
2003 _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
2005 _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
2009 data_count
= _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
2011 if (format_count
== 0 && data_count
!= 0)
2013 _bfd_error_handler (_("DWARF error: zero format count"));
2014 bfd_set_error (bfd_error_bad_value
);
2018 /* PR 22210. Paranoia check. Don't bother running the loop
2019 if we know that we are going to run out of buffer. */
2020 if (data_count
> (bfd_vma
) (buf_end
- buf
))
2023 (_("DWARF error: data count (%" PRIx64
") larger than buffer size"),
2024 (uint64_t) data_count
);
2025 bfd_set_error (bfd_error_bad_value
);
2029 for (datai
= 0; datai
< data_count
; datai
++)
2031 bfd_byte
*format
= format_header_data
;
2034 memset (&fe
, 0, sizeof fe
);
2035 for (formati
= 0; formati
< format_count
; formati
++)
2037 bfd_vma content_type
, form
;
2039 char **stringp
= &string_trash
;
2040 unsigned int uint_trash
, *uintp
= &uint_trash
;
2041 struct attribute attr
;
2043 content_type
= _bfd_safe_read_leb128 (abfd
, format
, &bytes_read
,
2045 format
+= bytes_read
;
2046 switch (content_type
)
2051 case DW_LNCT_directory_index
:
2054 case DW_LNCT_timestamp
:
2064 (_("DWARF error: unknown format content type %" PRIu64
),
2065 (uint64_t) content_type
);
2066 bfd_set_error (bfd_error_bad_value
);
2070 form
= _bfd_safe_read_leb128 (abfd
, format
, &bytes_read
, FALSE
,
2072 format
+= bytes_read
;
2074 buf
= read_attribute_value (&attr
, form
, 0, unit
, buf
, buf_end
);
2079 case DW_FORM_string
:
2080 case DW_FORM_line_strp
:
2081 *stringp
= attr
.u
.str
;
2089 *uintp
= attr
.u
.val
;
2092 case DW_FORM_data16
:
2093 /* MD5 data is in the attr.blk, but we are ignoring those. */
2098 /* Skip the first "zero entry", which is the compilation dir/file. */
2100 if (!callback (table
, fe
.name
, fe
.dir
, fe
.time
, fe
.size
))
2108 /* Decode the line number information for UNIT. */
2110 static struct line_info_table
*
2111 decode_line_info (struct comp_unit
*unit
)
2113 bfd
*abfd
= unit
->abfd
;
2114 struct dwarf2_debug
*stash
= unit
->stash
;
2115 struct dwarf2_debug_file
*file
= unit
->file
;
2116 struct line_info_table
* table
;
2119 struct line_head lh
;
2120 unsigned int i
, bytes_read
, offset_size
;
2121 char *cur_file
, *cur_dir
;
2122 unsigned char op_code
, extended_op
, adj_opcode
;
2123 unsigned int exop_len
;
2126 if (unit
->line_offset
== 0 && file
->line_table
)
2127 return file
->line_table
;
2129 if (! read_section (abfd
, &stash
->debug_sections
[debug_line
],
2130 file
->syms
, unit
->line_offset
,
2131 &file
->dwarf_line_buffer
, &file
->dwarf_line_size
))
2134 if (file
->dwarf_line_size
< 16)
2137 (_("DWARF error: line info section is too small (%" PRId64
")"),
2138 (int64_t) file
->dwarf_line_size
);
2139 bfd_set_error (bfd_error_bad_value
);
2142 line_ptr
= file
->dwarf_line_buffer
+ unit
->line_offset
;
2143 line_end
= file
->dwarf_line_buffer
+ file
->dwarf_line_size
;
2145 /* Read in the prologue. */
2146 lh
.total_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2149 if (lh
.total_length
== 0xffffffff)
2151 lh
.total_length
= read_8_bytes (abfd
, line_ptr
, line_end
);
2155 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
2157 /* Handle (non-standard) 64-bit DWARF2 formats. */
2158 lh
.total_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2163 if (lh
.total_length
> (size_t) (line_end
- line_ptr
))
2166 /* xgettext: c-format */
2167 (_("DWARF error: line info data is bigger (%#" PRIx64
")"
2168 " than the space remaining in the section (%#lx)"),
2169 (uint64_t) lh
.total_length
, (unsigned long) (line_end
- line_ptr
));
2170 bfd_set_error (bfd_error_bad_value
);
2174 line_end
= line_ptr
+ lh
.total_length
;
2176 lh
.version
= read_2_bytes (abfd
, line_ptr
, line_end
);
2177 if (lh
.version
< 2 || lh
.version
> 5)
2180 (_("DWARF error: unhandled .debug_line version %d"), lh
.version
);
2181 bfd_set_error (bfd_error_bad_value
);
2186 if (line_ptr
+ offset_size
+ (lh
.version
>= 5 ? 8 : (lh
.version
>= 4 ? 6 : 5))
2190 (_("DWARF error: ran out of room reading prologue"));
2191 bfd_set_error (bfd_error_bad_value
);
2195 if (lh
.version
>= 5)
2197 unsigned int segment_selector_size
;
2199 /* Skip address size. */
2200 read_1_byte (abfd
, line_ptr
, line_end
);
2203 segment_selector_size
= read_1_byte (abfd
, line_ptr
, line_end
);
2205 if (segment_selector_size
!= 0)
2208 (_("DWARF error: line info unsupported segment selector size %u"),
2209 segment_selector_size
);
2210 bfd_set_error (bfd_error_bad_value
);
2215 if (offset_size
== 4)
2216 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2218 lh
.prologue_length
= read_8_bytes (abfd
, line_ptr
, line_end
);
2219 line_ptr
+= offset_size
;
2221 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
, line_end
);
2224 if (lh
.version
>= 4)
2226 lh
.maximum_ops_per_insn
= read_1_byte (abfd
, line_ptr
, line_end
);
2230 lh
.maximum_ops_per_insn
= 1;
2232 if (lh
.maximum_ops_per_insn
== 0)
2235 (_("DWARF error: invalid maximum operations per instruction"));
2236 bfd_set_error (bfd_error_bad_value
);
2240 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
, line_end
);
2243 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
, line_end
);
2246 lh
.line_range
= read_1_byte (abfd
, line_ptr
, line_end
);
2249 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
, line_end
);
2252 if (line_ptr
+ (lh
.opcode_base
- 1) >= line_end
)
2254 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2255 bfd_set_error (bfd_error_bad_value
);
2259 amt
= lh
.opcode_base
* sizeof (unsigned char);
2260 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
2262 lh
.standard_opcode_lengths
[0] = 1;
2264 for (i
= 1; i
< lh
.opcode_base
; ++i
)
2266 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
, line_end
);
2270 amt
= sizeof (struct line_info_table
);
2271 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
2275 table
->comp_dir
= unit
->comp_dir
;
2277 table
->num_files
= 0;
2278 table
->files
= NULL
;
2280 table
->num_dirs
= 0;
2283 table
->num_sequences
= 0;
2284 table
->sequences
= NULL
;
2286 table
->lcl_head
= NULL
;
2288 if (lh
.version
>= 5)
2290 /* Read directory table. */
2291 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2292 line_info_add_include_dir_stub
))
2295 /* Read file name table. */
2296 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2297 line_info_add_file_name
))
2302 /* Read directory table. */
2303 while ((cur_dir
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
)) != NULL
)
2305 line_ptr
+= bytes_read
;
2307 if (!line_info_add_include_dir (table
, cur_dir
))
2311 line_ptr
+= bytes_read
;
2313 /* Read file name table. */
2314 while ((cur_file
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
)) != NULL
)
2316 unsigned int dir
, xtime
, size
;
2318 line_ptr
+= bytes_read
;
2320 dir
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2321 line_ptr
+= bytes_read
;
2322 xtime
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2323 line_ptr
+= bytes_read
;
2324 size
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2325 line_ptr
+= bytes_read
;
2327 if (!line_info_add_file_name (table
, cur_file
, dir
, xtime
, size
))
2331 line_ptr
+= bytes_read
;
2334 /* Read the statement sequences until there's nothing left. */
2335 while (line_ptr
< line_end
)
2337 /* State machine registers. */
2338 bfd_vma address
= 0;
2339 unsigned char op_index
= 0;
2340 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
2341 unsigned int line
= 1;
2342 unsigned int column
= 0;
2343 unsigned int discriminator
= 0;
2344 int is_stmt
= lh
.default_is_stmt
;
2345 int end_sequence
= 0;
2346 unsigned int dir
, xtime
, size
;
2347 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2348 compilers generate address sequences that are wildly out of
2349 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2350 for ia64-Linux). Thus, to determine the low and high
2351 address, we must compare on every DW_LNS_copy, etc. */
2352 bfd_vma low_pc
= (bfd_vma
) -1;
2353 bfd_vma high_pc
= 0;
2355 /* Decode the table. */
2356 while (!end_sequence
&& line_ptr
< line_end
)
2358 op_code
= read_1_byte (abfd
, line_ptr
, line_end
);
2361 if (op_code
>= lh
.opcode_base
)
2363 /* Special operand. */
2364 adj_opcode
= op_code
- lh
.opcode_base
;
2365 if (lh
.line_range
== 0)
2367 if (lh
.maximum_ops_per_insn
== 1)
2368 address
+= (adj_opcode
/ lh
.line_range
2369 * lh
.minimum_instruction_length
);
2372 address
+= ((op_index
+ adj_opcode
/ lh
.line_range
)
2373 / lh
.maximum_ops_per_insn
2374 * lh
.minimum_instruction_length
);
2375 op_index
= ((op_index
+ adj_opcode
/ lh
.line_range
)
2376 % lh
.maximum_ops_per_insn
);
2378 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
2379 /* Append row to matrix using current values. */
2380 if (!add_line_info (table
, address
, op_index
, filename
,
2381 line
, column
, discriminator
, 0))
2384 if (address
< low_pc
)
2386 if (address
> high_pc
)
2389 else switch (op_code
)
2391 case DW_LNS_extended_op
:
2392 exop_len
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2394 line_ptr
+= bytes_read
;
2395 extended_op
= read_1_byte (abfd
, line_ptr
, line_end
);
2398 switch (extended_op
)
2400 case DW_LNE_end_sequence
:
2402 if (!add_line_info (table
, address
, op_index
, filename
, line
,
2403 column
, discriminator
, end_sequence
))
2406 if (address
< low_pc
)
2408 if (address
> high_pc
)
2410 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
2413 case DW_LNE_set_address
:
2414 address
= read_address (unit
, line_ptr
, line_end
);
2416 line_ptr
+= unit
->addr_size
;
2418 case DW_LNE_define_file
:
2419 cur_file
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
);
2420 line_ptr
+= bytes_read
;
2421 dir
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2423 line_ptr
+= bytes_read
;
2424 xtime
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2426 line_ptr
+= bytes_read
;
2427 size
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2429 line_ptr
+= bytes_read
;
2430 if (!line_info_add_file_name (table
, cur_file
, dir
,
2434 case DW_LNE_set_discriminator
:
2436 _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2438 line_ptr
+= bytes_read
;
2440 case DW_LNE_HP_source_file_correlation
:
2441 line_ptr
+= exop_len
- 1;
2445 (_("DWARF error: mangled line number section"));
2446 bfd_set_error (bfd_error_bad_value
);
2453 if (!add_line_info (table
, address
, op_index
,
2454 filename
, line
, column
, discriminator
, 0))
2457 if (address
< low_pc
)
2459 if (address
> high_pc
)
2462 case DW_LNS_advance_pc
:
2463 if (lh
.maximum_ops_per_insn
== 1)
2464 address
+= (lh
.minimum_instruction_length
2465 * _bfd_safe_read_leb128 (abfd
, line_ptr
,
2470 bfd_vma adjust
= _bfd_safe_read_leb128 (abfd
, line_ptr
,
2473 address
= ((op_index
+ adjust
) / lh
.maximum_ops_per_insn
2474 * lh
.minimum_instruction_length
);
2475 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2477 line_ptr
+= bytes_read
;
2479 case DW_LNS_advance_line
:
2480 line
+= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2482 line_ptr
+= bytes_read
;
2484 case DW_LNS_set_file
:
2486 unsigned int filenum
;
2488 /* The file and directory tables are 0
2489 based, the references are 1 based. */
2490 filenum
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2492 line_ptr
+= bytes_read
;
2494 filename
= concat_filename (table
, filenum
);
2497 case DW_LNS_set_column
:
2498 column
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2500 line_ptr
+= bytes_read
;
2502 case DW_LNS_negate_stmt
:
2503 is_stmt
= (!is_stmt
);
2505 case DW_LNS_set_basic_block
:
2507 case DW_LNS_const_add_pc
:
2508 if (lh
.line_range
== 0)
2510 if (lh
.maximum_ops_per_insn
== 1)
2511 address
+= (lh
.minimum_instruction_length
2512 * ((255 - lh
.opcode_base
) / lh
.line_range
));
2515 bfd_vma adjust
= ((255 - lh
.opcode_base
) / lh
.line_range
);
2516 address
+= (lh
.minimum_instruction_length
2517 * ((op_index
+ adjust
)
2518 / lh
.maximum_ops_per_insn
));
2519 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2522 case DW_LNS_fixed_advance_pc
:
2523 address
+= read_2_bytes (abfd
, line_ptr
, line_end
);
2528 /* Unknown standard opcode, ignore it. */
2529 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
2531 (void) _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2533 line_ptr
+= bytes_read
;
2542 if (unit
->line_offset
== 0)
2543 file
->line_table
= table
;
2544 if (sort_line_sequences (table
))
2548 while (table
->sequences
!= NULL
)
2550 struct line_sequence
* seq
= table
->sequences
;
2551 table
->sequences
= table
->sequences
->prev_sequence
;
2554 free (table
->files
);
2559 /* If ADDR is within TABLE set the output parameters and return the
2560 range of addresses covered by the entry used to fill them out.
2561 Otherwise set * FILENAME_PTR to NULL and return 0.
2562 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2563 are pointers to the objects to be filled in. */
2566 lookup_address_in_line_info_table (struct line_info_table
*table
,
2568 const char **filename_ptr
,
2569 unsigned int *linenumber_ptr
,
2570 unsigned int *discriminator_ptr
)
2572 struct line_sequence
*seq
= NULL
;
2573 struct line_info
*info
;
2576 /* Binary search the array of sequences. */
2578 high
= table
->num_sequences
;
2581 mid
= (low
+ high
) / 2;
2582 seq
= &table
->sequences
[mid
];
2583 if (addr
< seq
->low_pc
)
2585 else if (addr
>= seq
->last_line
->address
)
2591 /* Check for a valid sequence. */
2592 if (!seq
|| addr
< seq
->low_pc
|| addr
>= seq
->last_line
->address
)
2595 if (!build_line_info_table (table
, seq
))
2598 /* Binary search the array of line information. */
2600 high
= seq
->num_lines
;
2604 mid
= (low
+ high
) / 2;
2605 info
= seq
->line_info_lookup
[mid
];
2606 if (addr
< info
->address
)
2608 else if (addr
>= seq
->line_info_lookup
[mid
+ 1]->address
)
2614 /* Check for a valid line information entry. */
2616 && addr
>= info
->address
2617 && addr
< seq
->line_info_lookup
[mid
+ 1]->address
2618 && !(info
->end_sequence
|| info
== seq
->last_line
))
2620 *filename_ptr
= info
->filename
;
2621 *linenumber_ptr
= info
->line
;
2622 if (discriminator_ptr
)
2623 *discriminator_ptr
= info
->discriminator
;
2624 return seq
->last_line
->address
- seq
->low_pc
;
2628 *filename_ptr
= NULL
;
2632 /* Read in the .debug_ranges section for future reference. */
2635 read_debug_ranges (struct comp_unit
* unit
)
2637 struct dwarf2_debug
*stash
= unit
->stash
;
2638 struct dwarf2_debug_file
*file
= unit
->file
;
2640 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_ranges
],
2642 &file
->dwarf_ranges_buffer
, &file
->dwarf_ranges_size
);
2645 /* Read in the .debug_rnglists section for future reference. */
2648 read_debug_rnglists (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_rnglists
],
2655 &file
->dwarf_rnglists_buffer
, &file
->dwarf_rnglists_size
);
2658 /* Function table functions. */
2661 compare_lookup_funcinfos (const void * a
, const void * b
)
2663 const struct lookup_funcinfo
* lookup1
= a
;
2664 const struct lookup_funcinfo
* lookup2
= b
;
2666 if (lookup1
->low_addr
< lookup2
->low_addr
)
2668 if (lookup1
->low_addr
> lookup2
->low_addr
)
2670 if (lookup1
->high_addr
< lookup2
->high_addr
)
2672 if (lookup1
->high_addr
> lookup2
->high_addr
)
2675 if (lookup1
->idx
< lookup2
->idx
)
2677 if (lookup1
->idx
> lookup2
->idx
)
2683 build_lookup_funcinfo_table (struct comp_unit
* unit
)
2685 struct lookup_funcinfo
*lookup_funcinfo_table
= unit
->lookup_funcinfo_table
;
2686 unsigned int number_of_functions
= unit
->number_of_functions
;
2687 struct funcinfo
*each
;
2688 struct lookup_funcinfo
*entry
;
2690 struct arange
*range
;
2691 bfd_vma low_addr
, high_addr
;
2693 if (lookup_funcinfo_table
|| number_of_functions
== 0)
2696 /* Create the function info lookup table. */
2697 lookup_funcinfo_table
= (struct lookup_funcinfo
*)
2698 bfd_malloc (number_of_functions
* sizeof (struct lookup_funcinfo
));
2699 if (lookup_funcinfo_table
== NULL
)
2702 /* Populate the function info lookup table. */
2703 func_index
= number_of_functions
;
2704 for (each
= unit
->function_table
; each
; each
= each
->prev_func
)
2706 entry
= &lookup_funcinfo_table
[--func_index
];
2707 entry
->funcinfo
= each
;
2708 entry
->idx
= func_index
;
2710 /* Calculate the lowest and highest address for this function entry. */
2711 low_addr
= entry
->funcinfo
->arange
.low
;
2712 high_addr
= entry
->funcinfo
->arange
.high
;
2714 for (range
= entry
->funcinfo
->arange
.next
; range
; range
= range
->next
)
2716 if (range
->low
< low_addr
)
2717 low_addr
= range
->low
;
2718 if (range
->high
> high_addr
)
2719 high_addr
= range
->high
;
2722 entry
->low_addr
= low_addr
;
2723 entry
->high_addr
= high_addr
;
2726 BFD_ASSERT (func_index
== 0);
2728 /* Sort the function by address. */
2729 qsort (lookup_funcinfo_table
,
2730 number_of_functions
,
2731 sizeof (struct lookup_funcinfo
),
2732 compare_lookup_funcinfos
);
2734 /* Calculate the high watermark for each function in the lookup table. */
2735 high_addr
= lookup_funcinfo_table
[0].high_addr
;
2736 for (func_index
= 1; func_index
< number_of_functions
; func_index
++)
2738 entry
= &lookup_funcinfo_table
[func_index
];
2739 if (entry
->high_addr
> high_addr
)
2740 high_addr
= entry
->high_addr
;
2742 entry
->high_addr
= high_addr
;
2745 unit
->lookup_funcinfo_table
= lookup_funcinfo_table
;
2749 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
2750 TRUE. Note that we need to find the function that has the smallest range
2751 that contains ADDR, to handle inlined functions without depending upon
2752 them being ordered in TABLE by increasing range. */
2755 lookup_address_in_function_table (struct comp_unit
*unit
,
2757 struct funcinfo
**function_ptr
)
2759 unsigned int number_of_functions
= unit
->number_of_functions
;
2760 struct lookup_funcinfo
* lookup_funcinfo
= NULL
;
2761 struct funcinfo
* funcinfo
= NULL
;
2762 struct funcinfo
* best_fit
= NULL
;
2763 bfd_vma best_fit_len
= 0;
2764 bfd_size_type low
, high
, mid
, first
;
2765 struct arange
*arange
;
2767 if (number_of_functions
== 0)
2770 if (!build_lookup_funcinfo_table (unit
))
2773 if (unit
->lookup_funcinfo_table
[number_of_functions
- 1].high_addr
< addr
)
2776 /* Find the first function in the lookup table which may contain the
2777 specified address. */
2779 high
= number_of_functions
;
2783 mid
= (low
+ high
) / 2;
2784 lookup_funcinfo
= &unit
->lookup_funcinfo_table
[mid
];
2785 if (addr
< lookup_funcinfo
->low_addr
)
2787 else if (addr
>= lookup_funcinfo
->high_addr
)
2793 /* Find the 'best' match for the address. The prior algorithm defined the
2794 best match as the function with the smallest address range containing
2795 the specified address. This definition should probably be changed to the
2796 innermost inline routine containing the address, but right now we want
2797 to get the same results we did before. */
2798 while (first
< number_of_functions
)
2800 if (addr
< unit
->lookup_funcinfo_table
[first
].low_addr
)
2802 funcinfo
= unit
->lookup_funcinfo_table
[first
].funcinfo
;
2804 for (arange
= &funcinfo
->arange
; arange
; arange
= arange
->next
)
2806 if (addr
< arange
->low
|| addr
>= arange
->high
)
2810 || arange
->high
- arange
->low
< best_fit_len
2811 /* The following comparison is designed to return the same
2812 match as the previous algorithm for routines which have the
2813 same best fit length. */
2814 || (arange
->high
- arange
->low
== best_fit_len
2815 && funcinfo
> best_fit
))
2817 best_fit
= funcinfo
;
2818 best_fit_len
= arange
->high
- arange
->low
;
2828 *function_ptr
= best_fit
;
2832 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2833 and LINENUMBER_PTR, and return TRUE. */
2836 lookup_symbol_in_function_table (struct comp_unit
*unit
,
2839 const char **filename_ptr
,
2840 unsigned int *linenumber_ptr
)
2842 struct funcinfo
* each_func
;
2843 struct funcinfo
* best_fit
= NULL
;
2844 bfd_vma best_fit_len
= 0;
2845 struct arange
*arange
;
2846 const char *name
= bfd_asymbol_name (sym
);
2847 asection
*sec
= bfd_asymbol_section (sym
);
2849 for (each_func
= unit
->function_table
;
2851 each_func
= each_func
->prev_func
)
2853 for (arange
= &each_func
->arange
;
2855 arange
= arange
->next
)
2857 if ((!each_func
->sec
|| each_func
->sec
== sec
)
2858 && addr
>= arange
->low
2859 && addr
< arange
->high
2861 && strcmp (name
, each_func
->name
) == 0
2863 || arange
->high
- arange
->low
< best_fit_len
))
2865 best_fit
= each_func
;
2866 best_fit_len
= arange
->high
- arange
->low
;
2873 best_fit
->sec
= sec
;
2874 *filename_ptr
= best_fit
->file
;
2875 *linenumber_ptr
= best_fit
->line
;
2882 /* Variable table functions. */
2884 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
2885 LINENUMBER_PTR, and return TRUE. */
2888 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
2891 const char **filename_ptr
,
2892 unsigned int *linenumber_ptr
)
2894 const char *name
= bfd_asymbol_name (sym
);
2895 asection
*sec
= bfd_asymbol_section (sym
);
2896 struct varinfo
* each
;
2898 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
2900 && each
->file
!= NULL
2901 && each
->name
!= NULL
2902 && each
->addr
== addr
2903 && (!each
->sec
|| each
->sec
== sec
)
2904 && strcmp (name
, each
->name
) == 0)
2910 *filename_ptr
= each
->file
;
2911 *linenumber_ptr
= each
->line
;
2918 static struct comp_unit
*stash_comp_unit (struct dwarf2_debug
*,
2919 struct dwarf2_debug_file
*);
2920 static bfd_boolean
comp_unit_maybe_decode_line_info (struct comp_unit
*);
2923 find_abstract_instance (struct comp_unit
*unit
,
2924 struct attribute
*attr_ptr
,
2925 unsigned int recur_count
,
2927 bfd_boolean
*is_linkage
,
2928 char **filename_ptr
,
2929 int *linenumber_ptr
)
2931 bfd
*abfd
= unit
->abfd
;
2932 bfd_byte
*info_ptr
= NULL
;
2933 bfd_byte
*info_ptr_end
;
2934 unsigned int abbrev_number
, bytes_read
, i
;
2935 struct abbrev_info
*abbrev
;
2936 bfd_uint64_t die_ref
= attr_ptr
->u
.val
;
2937 struct attribute attr
;
2938 const char *name
= NULL
;
2940 if (recur_count
== 100)
2943 (_("DWARF error: abstract instance recursion detected"));
2944 bfd_set_error (bfd_error_bad_value
);
2948 /* DW_FORM_ref_addr can reference an entry in a different CU. It
2949 is an offset from the .debug_info section, not the current CU. */
2950 if (attr_ptr
->form
== DW_FORM_ref_addr
)
2952 /* We only support DW_FORM_ref_addr within the same file, so
2953 any relocations should be resolved already. Check this by
2954 testing for a zero die_ref; There can't be a valid reference
2955 to the header of a .debug_info section.
2956 DW_FORM_ref_addr is an offset relative to .debug_info.
2957 Normally when using the GNU linker this is accomplished by
2958 emitting a symbolic reference to a label, because .debug_info
2959 sections are linked at zero. When there are multiple section
2960 groups containing .debug_info, as there might be in a
2961 relocatable object file, it would be reasonable to assume that
2962 a symbolic reference to a label in any .debug_info section
2963 might be used. Since we lay out multiple .debug_info
2964 sections at non-zero VMAs (see place_sections), and read
2965 them contiguously into dwarf_info_buffer, that means the
2966 reference is relative to dwarf_info_buffer. */
2969 info_ptr
= unit
->file
->dwarf_info_buffer
;
2970 info_ptr_end
= info_ptr
+ unit
->file
->dwarf_info_size
;
2971 total
= info_ptr_end
- info_ptr
;
2974 else if (die_ref
>= total
)
2977 (_("DWARF error: invalid abstract instance DIE ref"));
2978 bfd_set_error (bfd_error_bad_value
);
2981 info_ptr
+= die_ref
;
2983 else if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
2985 bfd_boolean first_time
= unit
->stash
->alt
.dwarf_info_buffer
== NULL
;
2987 info_ptr
= read_alt_indirect_ref (unit
, die_ref
);
2989 unit
->stash
->alt
.info_ptr
= unit
->stash
->alt
.dwarf_info_buffer
;
2990 if (info_ptr
== NULL
)
2993 (_("DWARF error: unable to read alt ref %" PRIu64
),
2994 (uint64_t) die_ref
);
2995 bfd_set_error (bfd_error_bad_value
);
2998 info_ptr_end
= (unit
->stash
->alt
.dwarf_info_buffer
2999 + unit
->stash
->alt
.dwarf_info_size
);
3000 if (unit
->stash
->alt
.all_comp_units
)
3001 unit
= unit
->stash
->alt
.all_comp_units
;
3004 if (attr_ptr
->form
== DW_FORM_ref_addr
3005 || attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3007 /* Now find the CU containing this pointer. */
3008 if (info_ptr
>= unit
->info_ptr_unit
&& info_ptr
< unit
->end_ptr
)
3009 info_ptr_end
= unit
->end_ptr
;
3012 /* Check other CUs to see if they contain the abbrev. */
3013 struct comp_unit
*u
;
3015 for (u
= unit
->prev_unit
; u
!= NULL
; u
= u
->prev_unit
)
3016 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3020 for (u
= unit
->next_unit
; u
!= NULL
; u
= u
->next_unit
)
3021 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3024 if (attr_ptr
->form
== DW_FORM_ref_addr
)
3027 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->f
);
3030 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3035 if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
3038 u
= stash_comp_unit (unit
->stash
, &unit
->stash
->alt
);
3041 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
3049 (_("DWARF error: unable to locate abstract instance DIE ref %"
3050 PRIu64
), (uint64_t) die_ref
);
3051 bfd_set_error (bfd_error_bad_value
);
3055 info_ptr_end
= unit
->end_ptr
;
3060 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
3061 DW_FORM_ref_udata. These are all references relative to the
3062 start of the current CU. */
3065 info_ptr
= unit
->info_ptr_unit
;
3066 info_ptr_end
= unit
->end_ptr
;
3067 total
= info_ptr_end
- info_ptr
;
3068 if (!die_ref
|| die_ref
>= total
)
3071 (_("DWARF error: invalid abstract instance DIE ref"));
3072 bfd_set_error (bfd_error_bad_value
);
3075 info_ptr
+= die_ref
;
3078 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
3079 FALSE
, info_ptr_end
);
3080 info_ptr
+= bytes_read
;
3084 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3088 (_("DWARF error: could not find abbrev number %u"), abbrev_number
);
3089 bfd_set_error (bfd_error_bad_value
);
3094 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3096 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
,
3097 info_ptr
, info_ptr_end
);
3098 if (info_ptr
== NULL
)
3103 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3105 if (name
== NULL
&& is_str_attr (attr
.form
))
3108 if (non_mangled (unit
->lang
))
3112 case DW_AT_specification
:
3113 if (!find_abstract_instance (unit
, &attr
, recur_count
+ 1,
3115 filename_ptr
, linenumber_ptr
))
3118 case DW_AT_linkage_name
:
3119 case DW_AT_MIPS_linkage_name
:
3120 /* PR 16949: Corrupt debug info can place
3121 non-string forms into these attributes. */
3122 if (is_str_attr (attr
.form
))
3128 case DW_AT_decl_file
:
3129 if (!comp_unit_maybe_decode_line_info (unit
))
3131 *filename_ptr
= concat_filename (unit
->line_table
,
3134 case DW_AT_decl_line
:
3135 *linenumber_ptr
= attr
.u
.val
;
3148 read_ranges (struct comp_unit
*unit
, struct arange
*arange
,
3149 bfd_uint64_t offset
)
3151 bfd_byte
*ranges_ptr
;
3152 bfd_byte
*ranges_end
;
3153 bfd_vma base_address
= unit
->base_address
;
3155 if (! unit
->file
->dwarf_ranges_buffer
)
3157 if (! read_debug_ranges (unit
))
3161 ranges_ptr
= unit
->file
->dwarf_ranges_buffer
+ offset
;
3162 if (ranges_ptr
< unit
->file
->dwarf_ranges_buffer
)
3164 ranges_end
= unit
->file
->dwarf_ranges_buffer
+ unit
->file
->dwarf_ranges_size
;
3171 /* PR 17512: file: 62cada7d. */
3172 if (ranges_ptr
+ 2 * unit
->addr_size
> ranges_end
)
3175 low_pc
= read_address (unit
, ranges_ptr
, ranges_end
);
3176 ranges_ptr
+= unit
->addr_size
;
3177 high_pc
= read_address (unit
, ranges_ptr
, ranges_end
);
3178 ranges_ptr
+= unit
->addr_size
;
3180 if (low_pc
== 0 && high_pc
== 0)
3182 if (low_pc
== -1UL && high_pc
!= -1UL)
3183 base_address
= high_pc
;
3186 if (!arange_add (unit
, arange
,
3187 base_address
+ low_pc
, base_address
+ high_pc
))
3195 read_rnglists (struct comp_unit
*unit
, struct arange
*arange
,
3196 bfd_uint64_t offset
)
3200 bfd_vma base_address
= unit
->base_address
;
3203 bfd
*abfd
= unit
->abfd
;
3205 if (! unit
->file
->dwarf_rnglists_buffer
)
3207 if (! read_debug_rnglists (unit
))
3211 rngs_ptr
= unit
->file
->dwarf_rnglists_buffer
+ offset
;
3212 if (rngs_ptr
< unit
->file
->dwarf_rnglists_buffer
)
3214 rngs_end
= unit
->file
->dwarf_rnglists_buffer
;
3215 rngs_end
+= unit
->file
->dwarf_rnglists_size
;
3219 enum dwarf_range_list_entry rlet
;
3220 unsigned int bytes_read
;
3222 if (rngs_ptr
+ 1 > rngs_end
)
3225 rlet
= read_1_byte (abfd
, rngs_ptr
, rngs_end
);
3230 case DW_RLE_end_of_list
:
3233 case DW_RLE_base_address
:
3234 if (rngs_ptr
+ unit
->addr_size
> rngs_end
)
3236 base_address
= read_address (unit
, rngs_ptr
, rngs_end
);
3237 rngs_ptr
+= unit
->addr_size
;
3240 case DW_RLE_start_length
:
3241 if (rngs_ptr
+ unit
->addr_size
> rngs_end
)
3243 low_pc
= read_address (unit
, rngs_ptr
, rngs_end
);
3244 rngs_ptr
+= unit
->addr_size
;
3246 high_pc
+= _bfd_safe_read_leb128 (abfd
, rngs_ptr
, &bytes_read
,
3248 rngs_ptr
+= bytes_read
;
3251 case DW_RLE_offset_pair
:
3252 low_pc
= base_address
;
3253 low_pc
+= _bfd_safe_read_leb128 (abfd
, rngs_ptr
, &bytes_read
,
3255 high_pc
= base_address
;
3256 high_pc
+= _bfd_safe_read_leb128 (abfd
, rngs_ptr
, &bytes_read
,
3260 case DW_RLE_start_end
:
3261 if (rngs_ptr
+ 2 * unit
->addr_size
> rngs_end
)
3263 low_pc
= read_address (unit
, rngs_ptr
, rngs_end
);
3264 rngs_ptr
+= unit
->addr_size
;
3265 high_pc
= read_address (unit
, rngs_ptr
, rngs_end
);
3266 rngs_ptr
+= unit
->addr_size
;
3269 /* TODO x-variants need .debug_addr support used for split-dwarf. */
3270 case DW_RLE_base_addressx
:
3271 case DW_RLE_startx_endx
:
3272 case DW_RLE_startx_length
:
3277 if ((low_pc
== 0 && high_pc
== 0) || low_pc
== high_pc
)
3280 if (!arange_add (unit
, arange
, low_pc
, high_pc
))
3286 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
,
3287 bfd_uint64_t offset
)
3289 if (unit
->version
<= 4)
3290 return read_ranges (unit
, arange
, offset
);
3292 return read_rnglists (unit
, arange
, offset
);
3295 static struct varinfo
*
3296 lookup_var_by_offset (bfd_uint64_t offset
, struct varinfo
* table
)
3300 if (table
->unit_offset
== offset
)
3302 table
= table
->prev_var
;
3309 /* DWARF2 Compilation unit functions. */
3311 /* Scan over each die in a comp. unit looking for functions to add
3312 to the function table and variables to the variable table. */
3315 scan_unit_for_symbols (struct comp_unit
*unit
)
3317 bfd
*abfd
= unit
->abfd
;
3318 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
3319 bfd_byte
*info_ptr_end
= unit
->end_ptr
;
3320 int nesting_level
= 0;
3321 struct nest_funcinfo
{
3322 struct funcinfo
*func
;
3324 int nested_funcs_size
;
3326 /* Maintain a stack of in-scope functions and inlined functions, which we
3327 can use to set the caller_func field. */
3328 nested_funcs_size
= 32;
3329 nested_funcs
= (struct nest_funcinfo
*)
3330 bfd_malloc (nested_funcs_size
* sizeof (*nested_funcs
));
3331 if (nested_funcs
== NULL
)
3333 nested_funcs
[nesting_level
].func
= 0;
3335 while (nesting_level
>= 0)
3337 unsigned int abbrev_number
, bytes_read
, i
;
3338 struct abbrev_info
*abbrev
;
3339 struct attribute attr
;
3340 struct funcinfo
*func
;
3341 struct varinfo
*var
;
3343 bfd_vma high_pc
= 0;
3344 bfd_boolean high_pc_relative
= FALSE
;
3345 bfd_uint64_t current_offset
;
3347 /* PR 17512: file: 9f405d9d. */
3348 if (info_ptr
>= info_ptr_end
)
3351 current_offset
= info_ptr
- unit
->info_ptr_unit
;
3352 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
3353 FALSE
, info_ptr_end
);
3354 info_ptr
+= bytes_read
;
3356 if (! abbrev_number
)
3362 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3365 static unsigned int previous_failed_abbrev
= -1U;
3367 /* Avoid multiple reports of the same missing abbrev. */
3368 if (abbrev_number
!= previous_failed_abbrev
)
3371 (_("DWARF error: could not find abbrev number %u"),
3373 previous_failed_abbrev
= abbrev_number
;
3375 bfd_set_error (bfd_error_bad_value
);
3379 if (abbrev
->tag
== DW_TAG_subprogram
3380 || abbrev
->tag
== DW_TAG_entry_point
3381 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3383 size_t amt
= sizeof (struct funcinfo
);
3386 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
3389 func
->tag
= abbrev
->tag
;
3390 func
->prev_func
= unit
->function_table
;
3391 unit
->function_table
= func
;
3392 unit
->number_of_functions
++;
3393 BFD_ASSERT (!unit
->cached
);
3395 if (func
->tag
== DW_TAG_inlined_subroutine
)
3396 for (i
= nesting_level
; i
-- != 0; )
3397 if (nested_funcs
[i
].func
)
3399 func
->caller_func
= nested_funcs
[i
].func
;
3402 nested_funcs
[nesting_level
].func
= func
;
3407 if (abbrev
->tag
== DW_TAG_variable
3408 || abbrev
->tag
== DW_TAG_member
)
3410 size_t amt
= sizeof (struct varinfo
);
3411 var
= (struct varinfo
*) bfd_zalloc (abfd
, amt
);
3414 var
->tag
= abbrev
->tag
;
3416 var
->prev_var
= unit
->variable_table
;
3417 unit
->variable_table
= var
;
3418 var
->unit_offset
= current_offset
;
3419 /* PR 18205: Missing debug information can cause this
3420 var to be attached to an already cached unit. */
3425 /* No inline function in scope at this nesting level. */
3426 nested_funcs
[nesting_level
].func
= 0;
3429 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3431 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
3432 unit
, info_ptr
, info_ptr_end
);
3433 if (info_ptr
== NULL
)
3440 case DW_AT_call_file
:
3441 func
->caller_file
= concat_filename (unit
->line_table
,
3445 case DW_AT_call_line
:
3446 func
->caller_line
= attr
.u
.val
;
3449 case DW_AT_abstract_origin
:
3450 case DW_AT_specification
:
3451 if (!find_abstract_instance (unit
, &attr
, 0,
3460 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3462 if (func
->name
== NULL
&& is_str_attr (attr
.form
))
3464 func
->name
= attr
.u
.str
;
3465 if (non_mangled (unit
->lang
))
3466 func
->is_linkage
= TRUE
;
3470 case DW_AT_linkage_name
:
3471 case DW_AT_MIPS_linkage_name
:
3472 /* PR 16949: Corrupt debug info can place
3473 non-string forms into these attributes. */
3474 if (is_str_attr (attr
.form
))
3476 func
->name
= attr
.u
.str
;
3477 func
->is_linkage
= TRUE
;
3482 low_pc
= attr
.u
.val
;
3486 high_pc
= attr
.u
.val
;
3487 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
3491 if (!read_rangelist (unit
, &func
->arange
, attr
.u
.val
))
3495 case DW_AT_decl_file
:
3496 func
->file
= concat_filename (unit
->line_table
,
3500 case DW_AT_decl_line
:
3501 func
->line
= attr
.u
.val
;
3512 case DW_AT_specification
:
3515 struct varinfo
* spec_var
;
3517 spec_var
= lookup_var_by_offset (attr
.u
.val
,
3518 unit
->variable_table
);
3519 if (spec_var
== NULL
)
3521 _bfd_error_handler (_("DWARF error: could not find "
3522 "variable specification "
3524 (unsigned long) attr
.u
.val
);
3528 if (var
->name
== NULL
)
3529 var
->name
= spec_var
->name
;
3530 if (var
->file
== NULL
&& spec_var
->file
!= NULL
)
3531 var
->file
= strdup (spec_var
->file
);
3533 var
->line
= spec_var
->line
;
3534 if (var
->sec
== NULL
)
3535 var
->sec
= spec_var
->sec
;
3540 if (is_str_attr (attr
.form
))
3541 var
->name
= attr
.u
.str
;
3544 case DW_AT_decl_file
:
3545 var
->file
= concat_filename (unit
->line_table
,
3549 case DW_AT_decl_line
:
3550 var
->line
= attr
.u
.val
;
3553 case DW_AT_external
:
3554 if (attr
.u
.val
!= 0)
3558 case DW_AT_location
:
3562 case DW_FORM_block1
:
3563 case DW_FORM_block2
:
3564 case DW_FORM_block4
:
3565 case DW_FORM_exprloc
:
3566 if (attr
.u
.blk
->data
!= NULL
3567 && *attr
.u
.blk
->data
== DW_OP_addr
)
3571 /* Verify that DW_OP_addr is the only opcode in the
3572 location, in which case the block size will be 1
3573 plus the address size. */
3574 /* ??? For TLS variables, gcc can emit
3575 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
3576 which we don't handle here yet. */
3577 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
3578 var
->addr
= bfd_get (unit
->addr_size
* 8,
3580 attr
.u
.blk
->data
+ 1);
3595 if (high_pc_relative
)
3598 if (func
&& high_pc
!= 0)
3600 if (!arange_add (unit
, &func
->arange
, low_pc
, high_pc
))
3604 if (abbrev
->has_children
)
3608 if (nesting_level
>= nested_funcs_size
)
3610 struct nest_funcinfo
*tmp
;
3612 nested_funcs_size
*= 2;
3613 tmp
= (struct nest_funcinfo
*)
3614 bfd_realloc (nested_funcs
,
3615 nested_funcs_size
* sizeof (*nested_funcs
));
3620 nested_funcs
[nesting_level
].func
= 0;
3624 free (nested_funcs
);
3628 free (nested_funcs
);
3632 /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
3633 includes the compilation unit header that proceeds the DIE's, but
3634 does not include the length field that precedes each compilation
3635 unit header. END_PTR points one past the end of this comp unit.
3636 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
3638 This routine does not read the whole compilation unit; only enough
3639 to get to the line number information for the compilation unit. */
3641 static struct comp_unit
*
3642 parse_comp_unit (struct dwarf2_debug
*stash
,
3643 struct dwarf2_debug_file
*file
,
3645 bfd_vma unit_length
,
3646 bfd_byte
*info_ptr_unit
,
3647 unsigned int offset_size
)
3649 struct comp_unit
* unit
;
3650 unsigned int version
;
3651 bfd_uint64_t abbrev_offset
= 0;
3652 /* Initialize it just to avoid a GCC false warning. */
3653 unsigned int addr_size
= -1;
3654 struct abbrev_info
** abbrevs
;
3655 unsigned int abbrev_number
, bytes_read
, i
;
3656 struct abbrev_info
*abbrev
;
3657 struct attribute attr
;
3658 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
3661 bfd_vma high_pc
= 0;
3662 bfd
*abfd
= file
->bfd_ptr
;
3663 bfd_boolean high_pc_relative
= FALSE
;
3664 enum dwarf_unit_type unit_type
;
3666 version
= read_2_bytes (abfd
, info_ptr
, end_ptr
);
3668 if (version
< 2 || version
> 5)
3670 /* PR 19872: A version number of 0 probably means that there is padding
3671 at the end of the .debug_info section. Gold puts it there when
3672 performing an incremental link, for example. So do not generate
3673 an error, just return a NULL. */
3677 (_("DWARF error: found dwarf version '%u', this reader"
3678 " only handles version 2, 3, 4 and 5 information"), version
);
3679 bfd_set_error (bfd_error_bad_value
);
3685 unit_type
= DW_UT_compile
;
3688 unit_type
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3691 addr_size
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3695 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
3696 if (offset_size
== 4)
3697 abbrev_offset
= read_4_bytes (abfd
, info_ptr
, end_ptr
);
3699 abbrev_offset
= read_8_bytes (abfd
, info_ptr
, end_ptr
);
3700 info_ptr
+= offset_size
;
3704 addr_size
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3708 if (unit_type
== DW_UT_type
)
3710 /* Skip type signature. */
3713 /* Skip type offset. */
3714 info_ptr
+= offset_size
;
3717 if (addr_size
> sizeof (bfd_vma
))
3720 /* xgettext: c-format */
3721 (_("DWARF error: found address size '%u', this reader"
3722 " can not handle sizes greater than '%u'"),
3724 (unsigned int) sizeof (bfd_vma
));
3725 bfd_set_error (bfd_error_bad_value
);
3729 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
3732 ("DWARF error: found address size '%u', this reader"
3733 " can only handle address sizes '2', '4' and '8'", addr_size
);
3734 bfd_set_error (bfd_error_bad_value
);
3738 /* Read the abbrevs for this compilation unit into a table. */
3739 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
, file
);
3743 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
3745 info_ptr
+= bytes_read
;
3746 if (! abbrev_number
)
3748 /* PR 19872: An abbrev number of 0 probably means that there is padding
3749 at the end of the .debug_abbrev section. Gold puts it there when
3750 performing an incremental link, for example. So do not generate
3751 an error, just return a NULL. */
3755 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
3758 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
3760 bfd_set_error (bfd_error_bad_value
);
3764 amt
= sizeof (struct comp_unit
);
3765 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
3769 unit
->version
= version
;
3770 unit
->addr_size
= addr_size
;
3771 unit
->offset_size
= offset_size
;
3772 unit
->abbrevs
= abbrevs
;
3773 unit
->end_ptr
= end_ptr
;
3774 unit
->stash
= stash
;
3776 unit
->info_ptr_unit
= info_ptr_unit
;
3778 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3780 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
, end_ptr
);
3781 if (info_ptr
== NULL
)
3784 /* Store the data if it is of an attribute we want to keep in a
3785 partial symbol table. */
3788 case DW_AT_stmt_list
:
3790 unit
->line_offset
= attr
.u
.val
;
3794 if (is_str_attr (attr
.form
))
3795 unit
->name
= attr
.u
.str
;
3799 low_pc
= attr
.u
.val
;
3800 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
3801 this is the base address to use when reading location
3802 lists or range lists. */
3803 if (abbrev
->tag
== DW_TAG_compile_unit
)
3804 unit
->base_address
= low_pc
;
3808 high_pc
= attr
.u
.val
;
3809 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
3813 if (!read_rangelist (unit
, &unit
->arange
, attr
.u
.val
))
3817 case DW_AT_comp_dir
:
3819 char *comp_dir
= attr
.u
.str
;
3821 /* PR 17512: file: 1fe726be. */
3822 if (! is_str_attr (attr
.form
))
3825 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
3831 /* Irix 6.2 native cc prepends <machine>.: to the compilation
3832 directory, get rid of it. */
3833 char *cp
= strchr (comp_dir
, ':');
3835 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
3838 unit
->comp_dir
= comp_dir
;
3842 case DW_AT_language
:
3843 unit
->lang
= attr
.u
.val
;
3850 if (high_pc_relative
)
3854 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
3858 unit
->first_child_die_ptr
= info_ptr
;
3862 /* Return TRUE if UNIT may contain the address given by ADDR. When
3863 there are functions written entirely with inline asm statements, the
3864 range info in the compilation unit header may not be correct. We
3865 need to consult the line info table to see if a compilation unit
3866 really contains the given address. */
3869 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
3871 struct arange
*arange
;
3876 arange
= &unit
->arange
;
3879 if (addr
>= arange
->low
&& addr
< arange
->high
)
3881 arange
= arange
->next
;
3888 /* If UNIT contains ADDR, set the output parameters to the values for
3889 the line containing ADDR. The output parameters, FILENAME_PTR,
3890 FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects
3893 Returns the range of addresses covered by the entry that was used
3894 to fill in *LINENUMBER_PTR or 0 if it was not filled in. */
3897 comp_unit_find_nearest_line (struct comp_unit
*unit
,
3899 const char **filename_ptr
,
3900 struct funcinfo
**function_ptr
,
3901 unsigned int *linenumber_ptr
,
3902 unsigned int *discriminator_ptr
)
3906 if (!comp_unit_maybe_decode_line_info (unit
))
3909 *function_ptr
= NULL
;
3910 func_p
= lookup_address_in_function_table (unit
, addr
, function_ptr
);
3911 if (func_p
&& (*function_ptr
)->tag
== DW_TAG_inlined_subroutine
)
3912 unit
->stash
->inliner_chain
= *function_ptr
;
3914 return lookup_address_in_line_info_table (unit
->line_table
, addr
,
3920 /* Check to see if line info is already decoded in a comp_unit.
3921 If not, decode it. Returns TRUE if no errors were encountered;
3925 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
)
3930 if (! unit
->line_table
)
3932 if (! unit
->stmtlist
)
3938 unit
->line_table
= decode_line_info (unit
);
3940 if (! unit
->line_table
)
3946 if (unit
->first_child_die_ptr
< unit
->end_ptr
3947 && ! scan_unit_for_symbols (unit
))
3957 /* If UNIT contains SYM at ADDR, set the output parameters to the
3958 values for the line containing SYM. The output parameters,
3959 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
3962 Return TRUE if UNIT contains SYM, and no errors were encountered;
3966 comp_unit_find_line (struct comp_unit
*unit
,
3969 const char **filename_ptr
,
3970 unsigned int *linenumber_ptr
)
3972 if (!comp_unit_maybe_decode_line_info (unit
))
3975 if (sym
->flags
& BSF_FUNCTION
)
3976 return lookup_symbol_in_function_table (unit
, sym
, addr
,
3980 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
3985 static struct funcinfo
*
3986 reverse_funcinfo_list (struct funcinfo
*head
)
3988 struct funcinfo
*rhead
;
3989 struct funcinfo
*temp
;
3991 for (rhead
= NULL
; head
; head
= temp
)
3993 temp
= head
->prev_func
;
3994 head
->prev_func
= rhead
;
4000 static struct varinfo
*
4001 reverse_varinfo_list (struct varinfo
*head
)
4003 struct varinfo
*rhead
;
4004 struct varinfo
*temp
;
4006 for (rhead
= NULL
; head
; head
= temp
)
4008 temp
= head
->prev_var
;
4009 head
->prev_var
= rhead
;
4015 /* Extract all interesting funcinfos and varinfos of a compilation
4016 unit into hash tables for faster lookup. Returns TRUE if no
4017 errors were enountered; FALSE otherwise. */
4020 comp_unit_hash_info (struct dwarf2_debug
*stash
,
4021 struct comp_unit
*unit
,
4022 struct info_hash_table
*funcinfo_hash_table
,
4023 struct info_hash_table
*varinfo_hash_table
)
4025 struct funcinfo
* each_func
;
4026 struct varinfo
* each_var
;
4027 bfd_boolean okay
= TRUE
;
4029 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
4031 if (!comp_unit_maybe_decode_line_info (unit
))
4034 BFD_ASSERT (!unit
->cached
);
4036 /* To preserve the original search order, we went to visit the function
4037 infos in the reversed order of the list. However, making the list
4038 bi-directional use quite a bit of extra memory. So we reverse
4039 the list first, traverse the list in the now reversed order and
4040 finally reverse the list again to get back the original order. */
4041 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4042 for (each_func
= unit
->function_table
;
4044 each_func
= each_func
->prev_func
)
4046 /* Skip nameless functions. */
4047 if (each_func
->name
)
4048 /* There is no need to copy name string into hash table as
4049 name string is either in the dwarf string buffer or
4050 info in the stash. */
4051 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
4052 (void*) each_func
, FALSE
);
4054 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
4058 /* We do the same for variable infos. */
4059 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4060 for (each_var
= unit
->variable_table
;
4062 each_var
= each_var
->prev_var
)
4064 /* Skip stack vars and vars with no files or names. */
4065 if (! each_var
->stack
4066 && each_var
->file
!= NULL
4067 && each_var
->name
!= NULL
)
4068 /* There is no need to copy name string into hash table as
4069 name string is either in the dwarf string buffer or
4070 info in the stash. */
4071 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
4072 (void*) each_var
, FALSE
);
4075 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
4076 unit
->cached
= TRUE
;
4080 /* Locate a section in a BFD containing debugging info. The search starts
4081 from the section after AFTER_SEC, or from the first section in the BFD if
4082 AFTER_SEC is NULL. The search works by examining the names of the
4083 sections. There are three permissiable names. The first two are given
4084 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
4085 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
4086 This is a variation on the .debug_info section which has a checksum
4087 describing the contents appended onto the name. This allows the linker to
4088 identify and discard duplicate debugging sections for different
4089 compilation units. */
4090 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
4093 find_debug_info (bfd
*abfd
, const struct dwarf_debug_section
*debug_sections
,
4094 asection
*after_sec
)
4099 if (after_sec
== NULL
)
4101 look
= debug_sections
[debug_info
].uncompressed_name
;
4102 msec
= bfd_get_section_by_name (abfd
, look
);
4106 look
= debug_sections
[debug_info
].compressed_name
;
4109 msec
= bfd_get_section_by_name (abfd
, look
);
4114 for (msec
= abfd
->sections
; msec
!= NULL
; msec
= msec
->next
)
4115 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
4121 for (msec
= after_sec
->next
; msec
!= NULL
; msec
= msec
->next
)
4123 look
= debug_sections
[debug_info
].uncompressed_name
;
4124 if (strcmp (msec
->name
, look
) == 0)
4127 look
= debug_sections
[debug_info
].compressed_name
;
4128 if (look
!= NULL
&& strcmp (msec
->name
, look
) == 0)
4131 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
4138 /* Transfer VMAs from object file to separate debug file. */
4141 set_debug_vma (bfd
*orig_bfd
, bfd
*debug_bfd
)
4145 for (s
= orig_bfd
->sections
, d
= debug_bfd
->sections
;
4146 s
!= NULL
&& d
!= NULL
;
4147 s
= s
->next
, d
= d
->next
)
4149 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4151 /* ??? Assumes 1-1 correspondence between sections in the
4153 if (strcmp (s
->name
, d
->name
) == 0)
4155 d
->output_section
= s
->output_section
;
4156 d
->output_offset
= s
->output_offset
;
4162 /* If the dwarf2 info was found in a separate debug file, return the
4163 debug file section corresponding to the section in the original file
4164 and the debug file symbols. */
4167 _bfd_dwarf2_stash_syms (struct dwarf2_debug
*stash
, bfd
*abfd
,
4168 asection
**sec
, asymbol
***syms
)
4170 if (stash
->f
.bfd_ptr
!= abfd
)
4176 *syms
= stash
->f
.syms
;
4180 for (s
= abfd
->sections
, d
= stash
->f
.bfd_ptr
->sections
;
4181 s
!= NULL
&& d
!= NULL
;
4182 s
= s
->next
, d
= d
->next
)
4184 if ((d
->flags
& SEC_DEBUGGING
) != 0)
4187 && strcmp (s
->name
, d
->name
) == 0)
4190 *syms
= stash
->f
.syms
;
4197 /* Unset vmas for adjusted sections in STASH. */
4200 unset_sections (struct dwarf2_debug
*stash
)
4203 struct adjusted_section
*p
;
4205 i
= stash
->adjusted_section_count
;
4206 p
= stash
->adjusted_sections
;
4207 for (; i
> 0; i
--, p
++)
4208 p
->section
->vma
= 0;
4211 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4212 relocatable object file. VMAs are normally all zero in relocatable
4213 object files, so if we want to distinguish locations in sections by
4214 address we need to set VMAs so the sections do not overlap. We
4215 also set VMA on .debug_info so that when we have multiple
4216 .debug_info sections (or the linkonce variant) they also do not
4217 overlap. The multiple .debug_info sections make up a single
4218 logical section. ??? We should probably do the same for other
4222 place_sections (bfd
*orig_bfd
, struct dwarf2_debug
*stash
)
4225 struct adjusted_section
*p
;
4227 const char *debug_info_name
;
4229 if (stash
->adjusted_section_count
!= 0)
4231 i
= stash
->adjusted_section_count
;
4232 p
= stash
->adjusted_sections
;
4233 for (; i
> 0; i
--, p
++)
4234 p
->section
->vma
= p
->adj_vma
;
4238 debug_info_name
= stash
->debug_sections
[debug_info
].uncompressed_name
;
4245 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4249 if ((sect
->output_section
!= NULL
4250 && sect
->output_section
!= sect
4251 && (sect
->flags
& SEC_DEBUGGING
) == 0)
4255 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
4256 || CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
));
4258 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
4264 if (abfd
== stash
->f
.bfd_ptr
)
4266 abfd
= stash
->f
.bfd_ptr
;
4270 stash
->adjusted_section_count
= -1;
4273 bfd_vma last_vma
= 0, last_dwarf
= 0;
4274 size_t amt
= i
* sizeof (struct adjusted_section
);
4276 p
= (struct adjusted_section
*) bfd_malloc (amt
);
4280 stash
->adjusted_sections
= p
;
4281 stash
->adjusted_section_count
= i
;
4288 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
4293 if ((sect
->output_section
!= NULL
4294 && sect
->output_section
!= sect
4295 && (sect
->flags
& SEC_DEBUGGING
) == 0)
4299 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
4300 || CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
));
4302 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
4306 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
4310 BFD_ASSERT (sect
->alignment_power
== 0);
4311 sect
->vma
= last_dwarf
;
4316 /* Align the new address to the current section
4318 last_vma
= ((last_vma
4319 + ~(-((bfd_vma
) 1 << sect
->alignment_power
)))
4320 & (-((bfd_vma
) 1 << sect
->alignment_power
)));
4321 sect
->vma
= last_vma
;
4326 p
->adj_vma
= sect
->vma
;
4329 if (abfd
== stash
->f
.bfd_ptr
)
4331 abfd
= stash
->f
.bfd_ptr
;
4335 if (orig_bfd
!= stash
->f
.bfd_ptr
)
4336 set_debug_vma (orig_bfd
, stash
->f
.bfd_ptr
);
4341 /* Look up a funcinfo by name using the given info hash table. If found,
4342 also update the locations pointed to by filename_ptr and linenumber_ptr.
4344 This function returns TRUE if a funcinfo that matches the given symbol
4345 and address is found with any error; otherwise it returns FALSE. */
4348 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
4351 const char **filename_ptr
,
4352 unsigned int *linenumber_ptr
)
4354 struct funcinfo
* each_func
;
4355 struct funcinfo
* best_fit
= NULL
;
4356 bfd_vma best_fit_len
= 0;
4357 struct info_list_node
*node
;
4358 struct arange
*arange
;
4359 const char *name
= bfd_asymbol_name (sym
);
4360 asection
*sec
= bfd_asymbol_section (sym
);
4362 for (node
= lookup_info_hash_table (hash_table
, name
);
4366 each_func
= (struct funcinfo
*) node
->info
;
4367 for (arange
= &each_func
->arange
;
4369 arange
= arange
->next
)
4371 if ((!each_func
->sec
|| each_func
->sec
== sec
)
4372 && addr
>= arange
->low
4373 && addr
< arange
->high
4375 || arange
->high
- arange
->low
< best_fit_len
))
4377 best_fit
= each_func
;
4378 best_fit_len
= arange
->high
- arange
->low
;
4385 best_fit
->sec
= sec
;
4386 *filename_ptr
= best_fit
->file
;
4387 *linenumber_ptr
= best_fit
->line
;
4394 /* Look up a varinfo by name using the given info hash table. If found,
4395 also update the locations pointed to by filename_ptr and linenumber_ptr.
4397 This function returns TRUE if a varinfo that matches the given symbol
4398 and address is found with any error; otherwise it returns FALSE. */
4401 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
4404 const char **filename_ptr
,
4405 unsigned int *linenumber_ptr
)
4407 const char *name
= bfd_asymbol_name (sym
);
4408 asection
*sec
= bfd_asymbol_section (sym
);
4409 struct varinfo
* each
;
4410 struct info_list_node
*node
;
4412 for (node
= lookup_info_hash_table (hash_table
, name
);
4416 each
= (struct varinfo
*) node
->info
;
4417 if (each
->addr
== addr
4418 && (!each
->sec
|| each
->sec
== sec
))
4421 *filename_ptr
= each
->file
;
4422 *linenumber_ptr
= each
->line
;
4430 /* Update the funcinfo and varinfo info hash tables if they are
4431 not up to date. Returns TRUE if there is no error; otherwise
4432 returns FALSE and disable the info hash tables. */
4435 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
4437 struct comp_unit
*each
;
4439 /* Exit if hash tables are up-to-date. */
4440 if (stash
->f
.all_comp_units
== stash
->hash_units_head
)
4443 if (stash
->hash_units_head
)
4444 each
= stash
->hash_units_head
->prev_unit
;
4446 each
= stash
->f
.last_comp_unit
;
4450 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
4451 stash
->varinfo_hash_table
))
4453 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
4456 each
= each
->prev_unit
;
4459 stash
->hash_units_head
= stash
->f
.all_comp_units
;
4463 /* Check consistency of info hash tables. This is for debugging only. */
4465 static void ATTRIBUTE_UNUSED
4466 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
4468 struct comp_unit
*each_unit
;
4469 struct funcinfo
*each_func
;
4470 struct varinfo
*each_var
;
4471 struct info_list_node
*node
;
4474 for (each_unit
= stash
->f
.all_comp_units
;
4476 each_unit
= each_unit
->next_unit
)
4478 for (each_func
= each_unit
->function_table
;
4480 each_func
= each_func
->prev_func
)
4482 if (!each_func
->name
)
4484 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
4488 while (node
&& !found
)
4490 found
= node
->info
== each_func
;
4496 for (each_var
= each_unit
->variable_table
;
4498 each_var
= each_var
->prev_var
)
4500 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
4502 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
4506 while (node
&& !found
)
4508 found
= node
->info
== each_var
;
4516 /* Check to see if we want to enable the info hash tables, which consume
4517 quite a bit of memory. Currently we only check the number times
4518 bfd_dwarf2_find_line is called. In the future, we may also want to
4519 take the number of symbols into account. */
4522 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
4524 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
4526 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
4529 /* FIXME: Maybe we should check the reduce_memory_overheads
4530 and optimize fields in the bfd_link_info structure ? */
4532 /* Create hash tables. */
4533 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
4534 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
4535 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
4537 /* Turn off info hashes if any allocation above fails. */
4538 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
4541 /* We need a forced update so that the info hash tables will
4542 be created even though there is no compilation unit. That
4543 happens if STASH_INFO_HASH_TRIGGER is 0. */
4544 if (stash_maybe_update_info_hash_tables (stash
))
4545 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
4548 /* Find the file and line associated with a symbol and address using the
4549 info hash tables of a stash. If there is a match, the function returns
4550 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
4551 otherwise it returns FALSE. */
4554 stash_find_line_fast (struct dwarf2_debug
*stash
,
4557 const char **filename_ptr
,
4558 unsigned int *linenumber_ptr
)
4560 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
4562 if (sym
->flags
& BSF_FUNCTION
)
4563 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
4564 filename_ptr
, linenumber_ptr
);
4565 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
4566 filename_ptr
, linenumber_ptr
);
4569 /* Save current section VMAs. */
4572 save_section_vma (const bfd
*abfd
, struct dwarf2_debug
*stash
)
4577 if (abfd
->section_count
== 0)
4579 stash
->sec_vma
= bfd_malloc (sizeof (*stash
->sec_vma
) * abfd
->section_count
);
4580 if (stash
->sec_vma
== NULL
)
4582 stash
->sec_vma_count
= abfd
->section_count
;
4583 for (i
= 0, s
= abfd
->sections
;
4584 s
!= NULL
&& i
< abfd
->section_count
;
4587 if (s
->output_section
!= NULL
)
4588 stash
->sec_vma
[i
] = s
->output_section
->vma
+ s
->output_offset
;
4590 stash
->sec_vma
[i
] = s
->vma
;
4595 /* Compare current section VMAs against those at the time the stash
4596 was created. If find_nearest_line is used in linker warnings or
4597 errors early in the link process, the debug info stash will be
4598 invalid for later calls. This is because we relocate debug info
4599 sections, so the stashed section contents depend on symbol values,
4600 which in turn depend on section VMAs. */
4603 section_vma_same (const bfd
*abfd
, const struct dwarf2_debug
*stash
)
4608 /* PR 24334: If the number of sections in ABFD has changed between
4609 when the stash was created and now, then we cannot trust the
4610 stashed vma information. */
4611 if (abfd
->section_count
!= stash
->sec_vma_count
)
4614 for (i
= 0, s
= abfd
->sections
;
4615 s
!= NULL
&& i
< abfd
->section_count
;
4620 if (s
->output_section
!= NULL
)
4621 vma
= s
->output_section
->vma
+ s
->output_offset
;
4624 if (vma
!= stash
->sec_vma
[i
])
4630 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
4631 If DEBUG_BFD is not specified, we read debug information from ABFD
4632 or its gnu_debuglink. The results will be stored in PINFO.
4633 The function returns TRUE iff debug information is ready. */
4636 _bfd_dwarf2_slurp_debug_info (bfd
*abfd
, bfd
*debug_bfd
,
4637 const struct dwarf_debug_section
*debug_sections
,
4640 bfd_boolean do_place
)
4642 size_t amt
= sizeof (struct dwarf2_debug
);
4643 bfd_size_type total_size
;
4645 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
4649 if (stash
->orig_bfd
== abfd
4650 && section_vma_same (abfd
, stash
))
4652 /* Check that we did previously find some debug information
4653 before attempting to make use of it. */
4654 if (stash
->f
.bfd_ptr
!= NULL
)
4656 if (do_place
&& !place_sections (abfd
, stash
))
4663 _bfd_dwarf2_cleanup_debug_info (abfd
, pinfo
);
4664 memset (stash
, 0, amt
);
4668 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
4672 stash
->orig_bfd
= abfd
;
4673 stash
->debug_sections
= debug_sections
;
4674 stash
->f
.syms
= symbols
;
4675 if (!save_section_vma (abfd
, stash
))
4678 stash
->f
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
4679 del_abbrev
, calloc
, free
);
4680 if (!stash
->f
.abbrev_offsets
)
4683 stash
->alt
.abbrev_offsets
= htab_create_alloc (10, hash_abbrev
, eq_abbrev
,
4684 del_abbrev
, calloc
, free
);
4685 if (!stash
->alt
.abbrev_offsets
)
4690 if (debug_bfd
== NULL
)
4693 msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4694 if (msec
== NULL
&& abfd
== debug_bfd
)
4696 char * debug_filename
;
4698 debug_filename
= bfd_follow_build_id_debuglink (abfd
, DEBUGDIR
);
4699 if (debug_filename
== NULL
)
4700 debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
4702 if (debug_filename
== NULL
)
4703 /* No dwarf2 info, and no gnu_debuglink to follow.
4704 Note that at this point the stash has been allocated, but
4705 contains zeros. This lets future calls to this function
4706 fail more quickly. */
4709 debug_bfd
= bfd_openr (debug_filename
, NULL
);
4710 free (debug_filename
);
4711 if (debug_bfd
== NULL
)
4712 /* FIXME: Should we report our failure to follow the debuglink ? */
4715 /* Set BFD_DECOMPRESS to decompress debug sections. */
4716 debug_bfd
->flags
|= BFD_DECOMPRESS
;
4717 if (!bfd_check_format (debug_bfd
, bfd_object
)
4718 || (msec
= find_debug_info (debug_bfd
,
4719 debug_sections
, NULL
)) == NULL
4720 || !bfd_generic_link_read_symbols (debug_bfd
))
4722 bfd_close (debug_bfd
);
4726 symbols
= bfd_get_outsymbols (debug_bfd
);
4727 stash
->f
.syms
= symbols
;
4728 stash
->close_on_cleanup
= TRUE
;
4730 stash
->f
.bfd_ptr
= debug_bfd
;
4733 && !place_sections (abfd
, stash
))
4736 /* There can be more than one DWARF2 info section in a BFD these
4737 days. First handle the easy case when there's only one. If
4738 there's more than one, try case two: none of the sections is
4739 compressed. In that case, read them all in and produce one
4740 large stash. We do this in two passes - in the first pass we
4741 just accumulate the section sizes, and in the second pass we
4742 read in the section's contents. (The allows us to avoid
4743 reallocing the data as we add sections to the stash.) If
4744 some or all sections are compressed, then do things the slow
4745 way, with a bunch of reallocs. */
4747 if (! find_debug_info (debug_bfd
, debug_sections
, msec
))
4749 /* Case 1: only one info section. */
4750 total_size
= msec
->size
;
4751 if (! read_section (debug_bfd
, &stash
->debug_sections
[debug_info
],
4753 &stash
->f
.dwarf_info_buffer
, &total_size
))
4758 /* Case 2: multiple sections. */
4759 for (total_size
= 0;
4761 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
4763 /* Catch PR25070 testcase overflowing size calculation here. */
4764 if (total_size
+ msec
->size
< total_size
4765 || total_size
+ msec
->size
< msec
->size
)
4767 bfd_set_error (bfd_error_no_memory
);
4770 total_size
+= msec
->size
;
4773 stash
->f
.dwarf_info_buffer
= (bfd_byte
*) bfd_malloc (total_size
);
4774 if (stash
->f
.dwarf_info_buffer
== NULL
)
4778 for (msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4780 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
4788 if (!(bfd_simple_get_relocated_section_contents
4789 (debug_bfd
, msec
, stash
->f
.dwarf_info_buffer
+ total_size
,
4797 stash
->f
.info_ptr
= stash
->f
.dwarf_info_buffer
;
4798 stash
->f
.dwarf_info_size
= total_size
;
4802 /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
4804 static struct comp_unit
*
4805 stash_comp_unit (struct dwarf2_debug
*stash
, struct dwarf2_debug_file
*file
)
4807 bfd_size_type length
;
4808 unsigned int offset_size
;
4809 bfd_byte
*info_ptr_unit
= file
->info_ptr
;
4810 bfd_byte
*info_ptr_end
= file
->dwarf_info_buffer
+ file
->dwarf_info_size
;
4812 if (file
->info_ptr
>= info_ptr_end
)
4815 length
= read_4_bytes (file
->bfd_ptr
, file
->info_ptr
, info_ptr_end
);
4816 /* A 0xffffff length is the DWARF3 way of indicating
4817 we use 64-bit offsets, instead of 32-bit offsets. */
4818 if (length
== 0xffffffff)
4821 length
= read_8_bytes (file
->bfd_ptr
, file
->info_ptr
+ 4,
4823 file
->info_ptr
+= 12;
4825 /* A zero length is the IRIX way of indicating 64-bit offsets,
4826 mostly because the 64-bit length will generally fit in 32
4827 bits, and the endianness helps. */
4828 else if (length
== 0)
4831 length
= read_4_bytes (file
->bfd_ptr
, file
->info_ptr
+ 4,
4833 file
->info_ptr
+= 8;
4835 /* In the absence of the hints above, we assume 32-bit DWARF2
4836 offsets even for targets with 64-bit addresses, because:
4837 a) most of the time these targets will not have generated
4838 more than 2Gb of debug info and so will not need 64-bit
4841 b) if they do use 64-bit offsets but they are not using
4842 the size hints that are tested for above then they are
4843 not conforming to the DWARF3 standard anyway. */
4847 file
->info_ptr
+= 4;
4851 && file
->info_ptr
+ length
<= info_ptr_end
4852 && file
->info_ptr
+ length
> file
->info_ptr
)
4854 struct comp_unit
*each
= parse_comp_unit (stash
, file
,
4855 file
->info_ptr
, length
,
4856 info_ptr_unit
, offset_size
);
4859 if (file
->all_comp_units
)
4860 file
->all_comp_units
->prev_unit
= each
;
4862 file
->last_comp_unit
= each
;
4864 each
->next_unit
= file
->all_comp_units
;
4865 file
->all_comp_units
= each
;
4867 file
->info_ptr
+= length
;
4872 /* Don't trust any of the DWARF info after a corrupted length or
4874 file
->info_ptr
= info_ptr_end
;
4878 /* Hash function for an asymbol. */
4881 hash_asymbol (const void *sym
)
4883 const asymbol
*asym
= sym
;
4884 return htab_hash_string (asym
->name
);
4887 /* Equality function for asymbols. */
4890 eq_asymbol (const void *a
, const void *b
)
4892 const asymbol
*sa
= a
;
4893 const asymbol
*sb
= b
;
4894 return strcmp (sa
->name
, sb
->name
) == 0;
4897 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
4898 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
4899 symbol in SYMBOLS and return the difference between the low_pc and
4900 the symbol's address. Returns 0 if no suitable symbol could be found. */
4903 _bfd_dwarf2_find_symbol_bias (asymbol
** symbols
, void ** pinfo
)
4905 struct dwarf2_debug
*stash
;
4906 struct comp_unit
* unit
;
4908 bfd_signed_vma result
= 0;
4911 stash
= (struct dwarf2_debug
*) *pinfo
;
4913 if (stash
== NULL
|| symbols
== NULL
)
4916 sym_hash
= htab_create_alloc (10, hash_asymbol
, eq_asymbol
,
4917 NULL
, xcalloc
, free
);
4918 for (psym
= symbols
; * psym
!= NULL
; psym
++)
4920 asymbol
* sym
= * psym
;
4922 if (sym
->flags
& BSF_FUNCTION
&& sym
->section
!= NULL
)
4924 void **slot
= htab_find_slot (sym_hash
, sym
, INSERT
);
4929 for (unit
= stash
->f
.all_comp_units
; unit
; unit
= unit
->next_unit
)
4931 struct funcinfo
* func
;
4933 comp_unit_maybe_decode_line_info (unit
);
4935 for (func
= unit
->function_table
; func
!= NULL
; func
= func
->prev_func
)
4936 if (func
->name
&& func
->arange
.low
)
4938 asymbol search
, *sym
;
4940 /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
4942 search
.name
= func
->name
;
4943 sym
= htab_find (sym_hash
, &search
);
4946 result
= ((bfd_signed_vma
) func
->arange
.low
) -
4947 ((bfd_signed_vma
) (sym
->value
+ sym
->section
->vma
));
4954 htab_delete (sym_hash
);
4958 /* Find the source code location of SYMBOL. If SYMBOL is NULL
4959 then find the nearest source code location corresponding to
4960 the address SECTION + OFFSET.
4961 Returns 1 if the line is found without error and fills in
4962 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
4963 NULL the FUNCTIONNAME_PTR is also filled in.
4964 Returns 2 if partial information from _bfd_elf_find_function is
4965 returned (function and maybe file) by looking at symbols. DWARF2
4966 info is present but not regarding the requested code location.
4967 Returns 0 otherwise.
4968 SYMBOLS contains the symbol table for ABFD.
4969 DEBUG_SECTIONS contains the name of the dwarf debug sections. */
4972 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
4977 const char **filename_ptr
,
4978 const char **functionname_ptr
,
4979 unsigned int *linenumber_ptr
,
4980 unsigned int *discriminator_ptr
,
4981 const struct dwarf_debug_section
*debug_sections
,
4984 /* Read each compilation unit from the section .debug_info, and check
4985 to see if it contains the address we are searching for. If yes,
4986 lookup the address, and return the line number info. If no, go
4987 on to the next compilation unit.
4989 We keep a list of all the previously read compilation units, and
4990 a pointer to the next un-read compilation unit. Check the
4991 previously read units before reading more. */
4992 struct dwarf2_debug
*stash
;
4993 /* What address are we looking for? */
4995 struct comp_unit
* each
;
4996 struct funcinfo
*function
= NULL
;
4998 bfd_boolean do_line
;
5000 *filename_ptr
= NULL
;
5001 if (functionname_ptr
!= NULL
)
5002 *functionname_ptr
= NULL
;
5003 *linenumber_ptr
= 0;
5004 if (discriminator_ptr
)
5005 *discriminator_ptr
= 0;
5007 if (! _bfd_dwarf2_slurp_debug_info (abfd
, NULL
, debug_sections
,
5009 (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0))
5012 stash
= (struct dwarf2_debug
*) *pinfo
;
5014 do_line
= symbol
!= NULL
;
5017 BFD_ASSERT (section
== NULL
&& offset
== 0 && functionname_ptr
== NULL
);
5018 section
= bfd_asymbol_section (symbol
);
5019 addr
= symbol
->value
;
5023 BFD_ASSERT (section
!= NULL
&& functionname_ptr
!= NULL
);
5026 /* If we have no SYMBOL but the section we're looking at is not a
5027 code section, then take a look through the list of symbols to see
5028 if we have a symbol at the address we're looking for. If we do
5029 then use this to look up line information. This will allow us to
5030 give file and line results for data symbols. We exclude code
5031 symbols here, if we look up a function symbol and then look up the
5032 line information we'll actually return the line number for the
5033 opening '{' rather than the function definition line. This is
5034 because looking up by symbol uses the line table, in which the
5035 first line for a function is usually the opening '{', while
5036 looking up the function by section + offset uses the
5037 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
5038 which will be the line of the function name. */
5039 if (symbols
!= NULL
&& (section
->flags
& SEC_CODE
) == 0)
5043 for (tmp
= symbols
; (*tmp
) != NULL
; ++tmp
)
5044 if ((*tmp
)->the_bfd
== abfd
5045 && (*tmp
)->section
== section
5046 && (*tmp
)->value
== offset
5047 && ((*tmp
)->flags
& BSF_SECTION_SYM
) == 0)
5051 /* For local symbols, keep going in the hope we find a
5053 if ((symbol
->flags
& BSF_GLOBAL
) != 0)
5059 if (section
->output_section
)
5060 addr
+= section
->output_section
->vma
+ section
->output_offset
;
5062 addr
+= section
->vma
;
5064 /* A null info_ptr indicates that there is no dwarf2 info
5065 (or that an error occured while setting up the stash). */
5066 if (! stash
->f
.info_ptr
)
5069 stash
->inliner_chain
= NULL
;
5071 /* Check the previously read comp. units first. */
5074 /* The info hash tables use quite a bit of memory. We may not want to
5075 always use them. We use some heuristics to decide if and when to
5077 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
5078 stash_maybe_enable_info_hash_tables (abfd
, stash
);
5080 /* Keep info hash table up to date if they are available. Note that we
5081 may disable the hash tables if there is any error duing update. */
5082 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
5083 stash_maybe_update_info_hash_tables (stash
);
5085 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
5087 found
= stash_find_line_fast (stash
, symbol
, addr
, filename_ptr
,
5094 /* Check the previously read comp. units first. */
5095 for (each
= stash
->f
.all_comp_units
; each
; each
= each
->next_unit
)
5096 if ((symbol
->flags
& BSF_FUNCTION
) == 0
5097 || each
->arange
.high
== 0
5098 || comp_unit_contains_address (each
, addr
))
5100 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
5109 bfd_vma min_range
= (bfd_vma
) -1;
5110 const char * local_filename
= NULL
;
5111 struct funcinfo
*local_function
= NULL
;
5112 unsigned int local_linenumber
= 0;
5113 unsigned int local_discriminator
= 0;
5115 for (each
= stash
->f
.all_comp_units
; each
; each
= each
->next_unit
)
5117 bfd_vma range
= (bfd_vma
) -1;
5119 found
= ((each
->arange
.high
== 0
5120 || comp_unit_contains_address (each
, addr
))
5121 && (range
= (comp_unit_find_nearest_line
5122 (each
, addr
, &local_filename
,
5123 &local_function
, &local_linenumber
,
5124 &local_discriminator
))) != 0);
5127 /* PRs 15935 15994: Bogus debug information may have provided us
5128 with an erroneous match. We attempt to counter this by
5129 selecting the match that has the smallest address range
5130 associated with it. (We are assuming that corrupt debug info
5131 will tend to result in extra large address ranges rather than
5132 extra small ranges).
5134 This does mean that we scan through all of the CUs associated
5135 with the bfd each time this function is called. But this does
5136 have the benefit of producing consistent results every time the
5137 function is called. */
5138 if (range
<= min_range
)
5140 if (filename_ptr
&& local_filename
)
5141 * filename_ptr
= local_filename
;
5143 function
= local_function
;
5144 if (discriminator_ptr
&& local_discriminator
)
5145 * discriminator_ptr
= local_discriminator
;
5146 if (local_linenumber
)
5147 * linenumber_ptr
= local_linenumber
;
5153 if (* linenumber_ptr
)
5160 /* Read each remaining comp. units checking each as they are read. */
5161 while ((each
= stash_comp_unit (stash
, &stash
->f
)) != NULL
)
5163 /* DW_AT_low_pc and DW_AT_high_pc are optional for
5164 compilation units. If we don't have them (i.e.,
5165 unit->high == 0), we need to consult the line info table
5166 to see if a compilation unit contains the given
5169 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
5170 || each
->arange
.high
== 0
5171 || comp_unit_contains_address (each
, addr
))
5172 && comp_unit_find_line (each
, symbol
, addr
,
5173 filename_ptr
, linenumber_ptr
));
5175 found
= ((each
->arange
.high
== 0
5176 || comp_unit_contains_address (each
, addr
))
5177 && comp_unit_find_nearest_line (each
, addr
,
5181 discriminator_ptr
) != 0);
5188 if (functionname_ptr
&& function
&& function
->is_linkage
)
5189 *functionname_ptr
= function
->name
;
5190 else if (functionname_ptr
5191 && (!*functionname_ptr
5192 || (function
&& !function
->is_linkage
)))
5195 asymbol
**syms
= symbols
;
5196 asection
*sec
= section
;
5198 _bfd_dwarf2_stash_syms (stash
, abfd
, &sec
, &syms
);
5199 fun
= _bfd_elf_find_function (abfd
, syms
, sec
, offset
,
5200 *filename_ptr
? NULL
: filename_ptr
,
5203 if (!found
&& fun
!= NULL
)
5206 if (function
&& !function
->is_linkage
)
5210 sec_vma
= section
->vma
;
5211 if (section
->output_section
!= NULL
)
5212 sec_vma
= section
->output_section
->vma
+ section
->output_offset
;
5214 && fun
->value
+ sec_vma
== function
->arange
.low
)
5215 function
->name
= *functionname_ptr
;
5216 /* Even if we didn't find a linkage name, say that we have
5217 to stop a repeated search of symbols. */
5218 function
->is_linkage
= TRUE
;
5222 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
5223 unset_sections (stash
);
5229 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
5230 const char **filename_ptr
,
5231 const char **functionname_ptr
,
5232 unsigned int *linenumber_ptr
,
5235 struct dwarf2_debug
*stash
;
5237 stash
= (struct dwarf2_debug
*) *pinfo
;
5240 struct funcinfo
*func
= stash
->inliner_chain
;
5242 if (func
&& func
->caller_func
)
5244 *filename_ptr
= func
->caller_file
;
5245 *functionname_ptr
= func
->caller_func
->name
;
5246 *linenumber_ptr
= func
->caller_line
;
5247 stash
->inliner_chain
= func
->caller_func
;
5256 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
, void **pinfo
)
5258 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
5259 struct comp_unit
*each
;
5260 struct dwarf2_debug_file
*file
;
5262 if (abfd
== NULL
|| stash
== NULL
)
5265 if (stash
->varinfo_hash_table
)
5266 bfd_hash_table_free (&stash
->varinfo_hash_table
->base
);
5267 if (stash
->funcinfo_hash_table
)
5268 bfd_hash_table_free (&stash
->funcinfo_hash_table
->base
);
5273 for (each
= file
->all_comp_units
; each
; each
= each
->next_unit
)
5275 struct funcinfo
*function_table
= each
->function_table
;
5276 struct varinfo
*variable_table
= each
->variable_table
;
5278 if (each
->line_table
&& each
->line_table
!= file
->line_table
)
5280 free (each
->line_table
->files
);
5281 free (each
->line_table
->dirs
);
5284 free (each
->lookup_funcinfo_table
);
5285 each
->lookup_funcinfo_table
= NULL
;
5287 while (function_table
)
5289 free (function_table
->file
);
5290 function_table
->file
= NULL
;
5291 free (function_table
->caller_file
);
5292 function_table
->caller_file
= NULL
;
5293 function_table
= function_table
->prev_func
;
5296 while (variable_table
)
5298 free (variable_table
->file
);
5299 variable_table
->file
= NULL
;
5300 variable_table
= variable_table
->prev_var
;
5304 if (file
->line_table
)
5306 free (file
->line_table
->files
);
5307 free (file
->line_table
->dirs
);
5309 htab_delete (file
->abbrev_offsets
);
5311 free (file
->dwarf_line_str_buffer
);
5312 free (file
->dwarf_str_buffer
);
5313 free (file
->dwarf_ranges_buffer
);
5314 free (file
->dwarf_line_buffer
);
5315 free (file
->dwarf_abbrev_buffer
);
5316 free (file
->dwarf_info_buffer
);
5317 if (file
== &stash
->alt
)
5321 free (stash
->sec_vma
);
5322 free (stash
->adjusted_sections
);
5323 if (stash
->close_on_cleanup
)
5324 bfd_close (stash
->f
.bfd_ptr
);
5325 if (stash
->alt
.bfd_ptr
)
5326 bfd_close (stash
->alt
.bfd_ptr
);
5329 /* Find the function to a particular section and offset,
5330 for error reporting. */
5333 _bfd_elf_find_function (bfd
*abfd
,
5337 const char **filename_ptr
,
5338 const char **functionname_ptr
)
5340 struct elf_find_function_cache
5342 asection
*last_section
;
5344 const char *filename
;
5345 bfd_size_type func_size
;
5348 if (symbols
== NULL
)
5351 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
5354 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
5357 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
5358 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
5362 if (cache
->last_section
!= section
5363 || cache
->func
== NULL
5364 || offset
< cache
->func
->value
5365 || offset
>= cache
->func
->value
+ cache
->func_size
)
5370 /* ??? Given multiple file symbols, it is impossible to reliably
5371 choose the right file name for global symbols. File symbols are
5372 local symbols, and thus all file symbols must sort before any
5373 global symbols. The ELF spec may be interpreted to say that a
5374 file symbol must sort before other local symbols, but currently
5375 ld -r doesn't do this. So, for ld -r output, it is possible to
5376 make a better choice of file name for local symbols by ignoring
5377 file symbols appearing after a given local symbol. */
5378 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
5379 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5383 state
= nothing_seen
;
5384 cache
->filename
= NULL
;
5386 cache
->func_size
= 0;
5387 cache
->last_section
= section
;
5389 for (p
= symbols
; *p
!= NULL
; p
++)
5395 if ((sym
->flags
& BSF_FILE
) != 0)
5398 if (state
== symbol_seen
)
5399 state
= file_after_symbol_seen
;
5403 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
5405 && code_off
<= offset
5406 && (code_off
> low_func
5407 || (code_off
== low_func
5408 && size
> cache
->func_size
)))
5411 cache
->func_size
= size
;
5412 cache
->filename
= NULL
;
5413 low_func
= code_off
;
5415 && ((sym
->flags
& BSF_LOCAL
) != 0
5416 || state
!= file_after_symbol_seen
))
5417 cache
->filename
= bfd_asymbol_name (file
);
5419 if (state
== nothing_seen
)
5420 state
= symbol_seen
;
5424 if (cache
->func
== NULL
)
5428 *filename_ptr
= cache
->filename
;
5429 if (functionname_ptr
)
5430 *functionname_ptr
= bfd_asymbol_name (cache
->func
);