2 Copyright (C) 1994-2017 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"
39 /* The data in the .debug_line statement prologue looks like this. */
44 unsigned short version
;
45 bfd_vma prologue_length
;
46 unsigned char minimum_instruction_length
;
47 unsigned char maximum_ops_per_insn
;
48 unsigned char default_is_stmt
;
50 unsigned char line_range
;
51 unsigned char opcode_base
;
52 unsigned char *standard_opcode_lengths
;
55 /* Attributes have a name and a value. */
59 enum dwarf_attribute name
;
64 struct dwarf_block
*blk
;
71 /* Blocks are a bunch of untyped bytes. */
78 struct adjusted_section
86 /* A list of all previously read comp_units. */
87 struct comp_unit
*all_comp_units
;
89 /* Last comp unit in list above. */
90 struct comp_unit
*last_comp_unit
;
92 /* Names of the debug sections. */
93 const struct dwarf_debug_section
*debug_sections
;
95 /* The next unread compilation unit within the .debug_info section.
96 Zero indicates that the .debug_info section has not been loaded
100 /* Pointer to the end of the .debug_info section memory buffer. */
101 bfd_byte
*info_ptr_end
;
103 /* Pointer to the original bfd for which debug was loaded. This is what
104 we use to compare and so check that the cached debug data is still
105 valid - it saves having to possibly dereference the gnu_debuglink each
109 /* Pointer to the bfd, section and address of the beginning of the
110 section. The bfd might be different than expected because of
111 gnu_debuglink sections. */
114 bfd_byte
*sec_info_ptr
;
116 /* Support for alternate debug info sections created by the DWZ utility:
117 This includes a pointer to an alternate bfd which contains *extra*,
118 possibly duplicate debug sections, and pointers to the loaded
119 .debug_str and .debug_info sections from this bfd. */
121 bfd_byte
* alt_dwarf_str_buffer
;
122 bfd_size_type alt_dwarf_str_size
;
123 bfd_byte
* alt_dwarf_info_buffer
;
124 bfd_size_type alt_dwarf_info_size
;
126 /* A pointer to the memory block allocated for info_ptr. Neither
127 info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
128 beginning of the malloc block. This is used only to free the
130 bfd_byte
*info_ptr_memory
;
132 /* Pointer to the symbol table. */
135 /* Pointer to the .debug_abbrev section loaded into memory. */
136 bfd_byte
*dwarf_abbrev_buffer
;
138 /* Length of the loaded .debug_abbrev section. */
139 bfd_size_type dwarf_abbrev_size
;
141 /* Buffer for decode_line_info. */
142 bfd_byte
*dwarf_line_buffer
;
144 /* Length of the loaded .debug_line section. */
145 bfd_size_type dwarf_line_size
;
147 /* Pointer to the .debug_str section loaded into memory. */
148 bfd_byte
*dwarf_str_buffer
;
150 /* Length of the loaded .debug_str section. */
151 bfd_size_type dwarf_str_size
;
153 /* Pointer to the .debug_line_str section loaded into memory. */
154 bfd_byte
*dwarf_line_str_buffer
;
156 /* Length of the loaded .debug_line_str section. */
157 bfd_size_type dwarf_line_str_size
;
159 /* Pointer to the .debug_ranges section loaded into memory. */
160 bfd_byte
*dwarf_ranges_buffer
;
162 /* Length of the loaded .debug_ranges section. */
163 bfd_size_type dwarf_ranges_size
;
165 /* If the most recent call to bfd_find_nearest_line was given an
166 address in an inlined function, preserve a pointer into the
167 calling chain for subsequent calls to bfd_find_inliner_info to
169 struct funcinfo
*inliner_chain
;
171 /* Section VMAs at the time the stash was built. */
174 /* Number of sections whose VMA we must adjust. */
175 int adjusted_section_count
;
177 /* Array of sections with adjusted VMA. */
178 struct adjusted_section
*adjusted_sections
;
180 /* Number of times find_line is called. This is used in
181 the heuristic for enabling the info hash tables. */
184 #define STASH_INFO_HASH_TRIGGER 100
186 /* Hash table mapping symbol names to function infos. */
187 struct info_hash_table
*funcinfo_hash_table
;
189 /* Hash table mapping symbol names to variable infos. */
190 struct info_hash_table
*varinfo_hash_table
;
192 /* Head of comp_unit list in the last hash table update. */
193 struct comp_unit
*hash_units_head
;
195 /* Status of info hash. */
196 int info_hash_status
;
197 #define STASH_INFO_HASH_OFF 0
198 #define STASH_INFO_HASH_ON 1
199 #define STASH_INFO_HASH_DISABLED 2
201 /* True if we opened bfd_ptr. */
202 bfd_boolean close_on_cleanup
;
212 /* A minimal decoding of DWARF2 compilation units. We only decode
213 what's needed to get to the line number information. */
217 /* Chain the previously read compilation units. */
218 struct comp_unit
*next_unit
;
220 /* Likewise, chain the compilation unit read after this one.
221 The comp units are stored in reversed reading order. */
222 struct comp_unit
*prev_unit
;
224 /* Keep the bfd convenient (for memory allocation). */
227 /* The lowest and highest addresses contained in this compilation
228 unit as specified in the compilation unit header. */
229 struct arange arange
;
231 /* The DW_AT_name attribute (for error messages). */
234 /* The abbrev hash table. */
235 struct abbrev_info
**abbrevs
;
237 /* DW_AT_language. */
240 /* Note that an error was found by comp_unit_find_nearest_line. */
243 /* The DW_AT_comp_dir attribute. */
246 /* TRUE if there is a line number table associated with this comp. unit. */
249 /* Pointer to the current comp_unit so that we can find a given entry
251 bfd_byte
*info_ptr_unit
;
253 /* Pointer to the start of the debug section, for DW_FORM_ref_addr. */
254 bfd_byte
*sec_info_ptr
;
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 /* Pointer to dwarf2_debug structure. */
281 struct dwarf2_debug
*stash
;
283 /* DWARF format version for this unit - from unit header. */
286 /* Address size for this unit - from unit header. */
287 unsigned char addr_size
;
289 /* Offset size for this unit - from unit header. */
290 unsigned char offset_size
;
292 /* Base address for this unit - from DW_AT_low_pc attribute of
293 DW_TAG_compile_unit DIE */
294 bfd_vma base_address
;
296 /* TRUE if symbols are cached in hash table for faster lookup by name. */
300 /* This data structure holds the information of an abbrev. */
303 unsigned int number
; /* Number identifying abbrev. */
304 enum dwarf_tag tag
; /* DWARF tag. */
305 int has_children
; /* Boolean. */
306 unsigned int num_attrs
; /* Number of attributes. */
307 struct attr_abbrev
*attrs
; /* An array of attribute descriptions. */
308 struct abbrev_info
*next
; /* Next in chain. */
313 enum dwarf_attribute name
;
314 enum dwarf_form form
;
315 bfd_vma implicit_const
;
318 /* Map of uncompressed DWARF debug section name to compressed one. It
319 is terminated by NULL uncompressed_name. */
321 const struct dwarf_debug_section dwarf_debug_sections
[] =
323 { ".debug_abbrev", ".zdebug_abbrev" },
324 { ".debug_aranges", ".zdebug_aranges" },
325 { ".debug_frame", ".zdebug_frame" },
326 { ".debug_info", ".zdebug_info" },
327 { ".debug_info", ".zdebug_info" },
328 { ".debug_line", ".zdebug_line" },
329 { ".debug_loc", ".zdebug_loc" },
330 { ".debug_macinfo", ".zdebug_macinfo" },
331 { ".debug_macro", ".zdebug_macro" },
332 { ".debug_pubnames", ".zdebug_pubnames" },
333 { ".debug_pubtypes", ".zdebug_pubtypes" },
334 { ".debug_ranges", ".zdebug_ranges" },
335 { ".debug_static_func", ".zdebug_static_func" },
336 { ".debug_static_vars", ".zdebug_static_vars" },
337 { ".debug_str", ".zdebug_str", },
338 { ".debug_str", ".zdebug_str", },
339 { ".debug_line_str", ".zdebug_line_str", },
340 { ".debug_types", ".zdebug_types" },
341 /* GNU DWARF 1 extensions */
342 { ".debug_sfnames", ".zdebug_sfnames" },
343 { ".debug_srcinfo", ".zebug_srcinfo" },
344 /* SGI/MIPS DWARF 2 extensions */
345 { ".debug_funcnames", ".zdebug_funcnames" },
346 { ".debug_typenames", ".zdebug_typenames" },
347 { ".debug_varnames", ".zdebug_varnames" },
348 { ".debug_weaknames", ".zdebug_weaknames" },
352 /* NB/ Numbers in this enum must match up with indicies
353 into the dwarf_debug_sections[] array above. */
354 enum dwarf_debug_section_enum
383 /* A static assertion. */
384 extern int dwarf_debug_section_assert
[ARRAY_SIZE (dwarf_debug_sections
)
385 == debug_max
+ 1 ? 1 : -1];
387 #ifndef ABBREV_HASH_SIZE
388 #define ABBREV_HASH_SIZE 121
390 #ifndef ATTR_ALLOC_CHUNK
391 #define ATTR_ALLOC_CHUNK 4
394 /* Variable and function hash tables. This is used to speed up look-up
395 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
396 In order to share code between variable and function infos, we use
397 a list of untyped pointer for all variable/function info associated with
398 a symbol. We waste a bit of memory for list with one node but that
399 simplifies the code. */
401 struct info_list_node
403 struct info_list_node
*next
;
407 /* Info hash entry. */
408 struct info_hash_entry
410 struct bfd_hash_entry root
;
411 struct info_list_node
*head
;
414 struct info_hash_table
416 struct bfd_hash_table base
;
419 /* Function to create a new entry in info hash table. */
421 static struct bfd_hash_entry
*
422 info_hash_table_newfunc (struct bfd_hash_entry
*entry
,
423 struct bfd_hash_table
*table
,
426 struct info_hash_entry
*ret
= (struct info_hash_entry
*) entry
;
428 /* Allocate the structure if it has not already been allocated by a
432 ret
= (struct info_hash_entry
*) bfd_hash_allocate (table
,
438 /* Call the allocation method of the base class. */
439 ret
= ((struct info_hash_entry
*)
440 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
442 /* Initialize the local fields here. */
446 return (struct bfd_hash_entry
*) ret
;
449 /* Function to create a new info hash table. It returns a pointer to the
450 newly created table or NULL if there is any error. We need abfd
451 solely for memory allocation. */
453 static struct info_hash_table
*
454 create_info_hash_table (bfd
*abfd
)
456 struct info_hash_table
*hash_table
;
458 hash_table
= ((struct info_hash_table
*)
459 bfd_alloc (abfd
, sizeof (struct info_hash_table
)));
463 if (!bfd_hash_table_init (&hash_table
->base
, info_hash_table_newfunc
,
464 sizeof (struct info_hash_entry
)))
466 bfd_release (abfd
, hash_table
);
473 /* Insert an info entry into an info hash table. We do not check of
474 duplicate entries. Also, the caller need to guarantee that the
475 right type of info in inserted as info is passed as a void* pointer.
476 This function returns true if there is no error. */
479 insert_info_hash_table (struct info_hash_table
*hash_table
,
484 struct info_hash_entry
*entry
;
485 struct info_list_node
*node
;
487 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
,
492 node
= (struct info_list_node
*) bfd_hash_allocate (&hash_table
->base
,
498 node
->next
= entry
->head
;
504 /* Look up an info entry list from an info hash table. Return NULL
507 static struct info_list_node
*
508 lookup_info_hash_table (struct info_hash_table
*hash_table
, const char *key
)
510 struct info_hash_entry
*entry
;
512 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
, key
,
514 return entry
? entry
->head
: NULL
;
517 /* Read a section into its appropriate place in the dwarf2_debug
518 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
519 not NULL, use bfd_simple_get_relocated_section_contents to read the
520 section contents, otherwise use bfd_get_section_contents. Fail if
521 the located section does not contain at least OFFSET bytes. */
524 read_section (bfd
* abfd
,
525 const struct dwarf_debug_section
*sec
,
528 bfd_byte
** section_buffer
,
529 bfd_size_type
* section_size
)
532 const char *section_name
= sec
->uncompressed_name
;
534 /* The section may have already been read. */
535 if (*section_buffer
== NULL
)
537 msec
= bfd_get_section_by_name (abfd
, section_name
);
540 section_name
= sec
->compressed_name
;
541 if (section_name
!= NULL
)
542 msec
= bfd_get_section_by_name (abfd
, section_name
);
546 _bfd_error_handler (_("Dwarf Error: Can't find %s section."),
547 sec
->uncompressed_name
);
548 bfd_set_error (bfd_error_bad_value
);
552 *section_size
= msec
->rawsize
? msec
->rawsize
: msec
->size
;
556 = bfd_simple_get_relocated_section_contents (abfd
, msec
, NULL
, syms
);
557 if (! *section_buffer
)
562 *section_buffer
= (bfd_byte
*) bfd_malloc (*section_size
);
563 if (! *section_buffer
)
565 if (! bfd_get_section_contents (abfd
, msec
, *section_buffer
,
570 /* Paranoia - if we are reading in a string section, make sure that it
571 is NUL terminated. This is to prevent string functions from running
572 off the end of the buffer. Note - knowing the size of the buffer is
573 not enough as some functions, eg strchr, do not have a range limited
576 FIXME: We ought to use a flag in the dwarf_debug_sections[] table to
577 determine the nature of a debug section, rather than checking the
578 section name as we do here. */
579 if (*section_size
> 0
580 && (*section_buffer
)[*section_size
- 1] != 0
581 && (strstr (section_name
, "_str") || strstr (section_name
, "names")))
583 bfd_byte
* new_buffer
= malloc (*section_size
+ 1);
585 _bfd_error_handler (_("warning: dwarf string section '%s' is not NUL terminated"),
587 memcpy (new_buffer
, *section_buffer
, *section_size
);
588 new_buffer
[*section_size
] = 0;
589 free (*section_buffer
);
590 *section_buffer
= new_buffer
;
594 /* It is possible to get a bad value for the offset into the section
595 that the client wants. Validate it here to avoid trouble later. */
596 if (offset
!= 0 && offset
>= *section_size
)
598 /* xgettext: c-format */
599 _bfd_error_handler (_("Dwarf Error: Offset (%llu)"
600 " greater than or equal to %s size (%Lu)."),
601 (long long) offset
, section_name
, *section_size
);
602 bfd_set_error (bfd_error_bad_value
);
609 /* Read dwarf information from a buffer. */
612 read_1_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
, bfd_byte
*end
)
616 return bfd_get_8 (abfd
, buf
);
620 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
, bfd_byte
*end
)
624 return bfd_get_signed_8 (abfd
, buf
);
628 read_2_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
632 return bfd_get_16 (abfd
, buf
);
636 read_4_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
640 return bfd_get_32 (abfd
, buf
);
644 read_8_bytes (bfd
*abfd
, bfd_byte
*buf
, bfd_byte
*end
)
648 return bfd_get_64 (abfd
, buf
);
652 read_n_bytes (bfd
*abfd ATTRIBUTE_UNUSED
,
655 unsigned int size ATTRIBUTE_UNUSED
)
657 if (buf
+ size
> end
)
662 /* Scans a NUL terminated string starting at BUF, returning a pointer to it.
663 Returns the number of characters in the string, *including* the NUL byte,
664 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
665 at or beyond BUF_END will not be read. Returns NULL if there was a
666 problem, or if the string is empty. */
669 read_string (bfd
* abfd ATTRIBUTE_UNUSED
,
672 unsigned int * bytes_read_ptr
)
678 * bytes_read_ptr
= 0;
684 * bytes_read_ptr
= 1;
688 while (buf
< buf_end
)
691 * bytes_read_ptr
= buf
- str
;
695 * bytes_read_ptr
= buf
- str
;
699 /* Reads an offset from BUF and then locates the string at this offset
700 inside the debug string section. Returns a pointer to the string.
701 Returns the number of bytes read from BUF, *not* the length of the string,
702 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
703 at or beyond BUF_END will not be read from BUF. Returns NULL if there was
704 a problem, or if the string is empty. Does not check for NUL termination
708 read_indirect_string (struct comp_unit
* unit
,
711 unsigned int * bytes_read_ptr
)
714 struct dwarf2_debug
*stash
= unit
->stash
;
717 if (buf
+ unit
->offset_size
> buf_end
)
719 * bytes_read_ptr
= 0;
723 if (unit
->offset_size
== 4)
724 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
726 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
728 *bytes_read_ptr
= unit
->offset_size
;
730 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
732 &stash
->dwarf_str_buffer
, &stash
->dwarf_str_size
))
735 if (offset
>= stash
->dwarf_str_size
)
737 str
= (char *) stash
->dwarf_str_buffer
+ offset
;
743 /* Like read_indirect_string but from .debug_line_str section. */
746 read_indirect_line_string (struct comp_unit
* unit
,
749 unsigned int * bytes_read_ptr
)
752 struct dwarf2_debug
*stash
= unit
->stash
;
755 if (buf
+ unit
->offset_size
> buf_end
)
757 * bytes_read_ptr
= 0;
761 if (unit
->offset_size
== 4)
762 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
764 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
766 *bytes_read_ptr
= unit
->offset_size
;
768 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_line_str
],
770 &stash
->dwarf_line_str_buffer
,
771 &stash
->dwarf_line_str_size
))
774 if (offset
>= stash
->dwarf_line_str_size
)
776 str
= (char *) stash
->dwarf_line_str_buffer
+ offset
;
782 /* Like read_indirect_string but uses a .debug_str located in
783 an alternate file pointed to by the .gnu_debugaltlink section.
784 Used to impement DW_FORM_GNU_strp_alt. */
787 read_alt_indirect_string (struct comp_unit
* unit
,
790 unsigned int * bytes_read_ptr
)
793 struct dwarf2_debug
*stash
= unit
->stash
;
796 if (buf
+ unit
->offset_size
> buf_end
)
798 * bytes_read_ptr
= 0;
802 if (unit
->offset_size
== 4)
803 offset
= read_4_bytes (unit
->abfd
, buf
, buf_end
);
805 offset
= read_8_bytes (unit
->abfd
, buf
, buf_end
);
807 *bytes_read_ptr
= unit
->offset_size
;
809 if (stash
->alt_bfd_ptr
== NULL
)
812 char * debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
814 if (debug_filename
== NULL
)
817 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
818 || ! bfd_check_format (debug_bfd
, bfd_object
))
821 bfd_close (debug_bfd
);
823 /* FIXME: Should we report our failure to follow the debuglink ? */
824 free (debug_filename
);
827 stash
->alt_bfd_ptr
= debug_bfd
;
830 if (! read_section (unit
->stash
->alt_bfd_ptr
,
831 stash
->debug_sections
+ debug_str_alt
,
832 NULL
, /* FIXME: Do we need to load alternate symbols ? */
834 &stash
->alt_dwarf_str_buffer
,
835 &stash
->alt_dwarf_str_size
))
838 if (offset
>= stash
->alt_dwarf_str_size
)
840 str
= (char *) stash
->alt_dwarf_str_buffer
+ offset
;
847 /* Resolve an alternate reference from UNIT at OFFSET.
848 Returns a pointer into the loaded alternate CU upon success
849 or NULL upon failure. */
852 read_alt_indirect_ref (struct comp_unit
* unit
,
855 struct dwarf2_debug
*stash
= unit
->stash
;
857 if (stash
->alt_bfd_ptr
== NULL
)
860 char * debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
862 if (debug_filename
== NULL
)
865 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
866 || ! bfd_check_format (debug_bfd
, bfd_object
))
869 bfd_close (debug_bfd
);
871 /* FIXME: Should we report our failure to follow the debuglink ? */
872 free (debug_filename
);
875 stash
->alt_bfd_ptr
= debug_bfd
;
878 if (! read_section (unit
->stash
->alt_bfd_ptr
,
879 stash
->debug_sections
+ debug_info_alt
,
880 NULL
, /* FIXME: Do we need to load alternate symbols ? */
882 &stash
->alt_dwarf_info_buffer
,
883 &stash
->alt_dwarf_info_size
))
886 if (offset
>= stash
->alt_dwarf_info_size
)
888 return stash
->alt_dwarf_info_buffer
+ offset
;
892 read_address (struct comp_unit
*unit
, bfd_byte
*buf
, bfd_byte
* buf_end
)
896 if (bfd_get_flavour (unit
->abfd
) == bfd_target_elf_flavour
)
897 signed_vma
= get_elf_backend_data (unit
->abfd
)->sign_extend_vma
;
899 if (buf
+ unit
->addr_size
> buf_end
)
904 switch (unit
->addr_size
)
907 return bfd_get_signed_64 (unit
->abfd
, buf
);
909 return bfd_get_signed_32 (unit
->abfd
, buf
);
911 return bfd_get_signed_16 (unit
->abfd
, buf
);
918 switch (unit
->addr_size
)
921 return bfd_get_64 (unit
->abfd
, buf
);
923 return bfd_get_32 (unit
->abfd
, buf
);
925 return bfd_get_16 (unit
->abfd
, buf
);
932 /* Lookup an abbrev_info structure in the abbrev hash table. */
934 static struct abbrev_info
*
935 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
937 unsigned int hash_number
;
938 struct abbrev_info
*abbrev
;
940 hash_number
= number
% ABBREV_HASH_SIZE
;
941 abbrev
= abbrevs
[hash_number
];
945 if (abbrev
->number
== number
)
948 abbrev
= abbrev
->next
;
954 /* In DWARF version 2, the description of the debugging information is
955 stored in a separate .debug_abbrev section. Before we read any
956 dies from a section we read in all abbreviations and install them
959 static struct abbrev_info
**
960 read_abbrevs (bfd
*abfd
, bfd_uint64_t offset
, struct dwarf2_debug
*stash
)
962 struct abbrev_info
**abbrevs
;
963 bfd_byte
*abbrev_ptr
;
964 bfd_byte
*abbrev_end
;
965 struct abbrev_info
*cur_abbrev
;
966 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
967 unsigned int abbrev_form
, hash_number
;
970 if (! read_section (abfd
, &stash
->debug_sections
[debug_abbrev
],
972 &stash
->dwarf_abbrev_buffer
, &stash
->dwarf_abbrev_size
))
975 if (offset
>= stash
->dwarf_abbrev_size
)
978 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
979 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
983 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
984 abbrev_end
= stash
->dwarf_abbrev_buffer
+ stash
->dwarf_abbrev_size
;
985 abbrev_number
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
987 abbrev_ptr
+= bytes_read
;
989 /* Loop until we reach an abbrev number of 0. */
990 while (abbrev_number
)
992 amt
= sizeof (struct abbrev_info
);
993 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
994 if (cur_abbrev
== NULL
)
997 /* Read in abbrev header. */
998 cur_abbrev
->number
= abbrev_number
;
999 cur_abbrev
->tag
= (enum dwarf_tag
)
1000 _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1002 abbrev_ptr
+= bytes_read
;
1003 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
, abbrev_end
);
1006 /* Now read in declarations. */
1009 /* Initialize it just to avoid a GCC false warning. */
1010 bfd_vma implicit_const
= -1;
1012 abbrev_name
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1014 abbrev_ptr
+= bytes_read
;
1015 abbrev_form
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
, &bytes_read
,
1017 abbrev_ptr
+= bytes_read
;
1018 if (abbrev_form
== DW_FORM_implicit_const
)
1020 implicit_const
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
,
1023 abbrev_ptr
+= bytes_read
;
1026 if (abbrev_name
== 0)
1029 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
1031 struct attr_abbrev
*tmp
;
1033 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
1034 amt
*= sizeof (struct attr_abbrev
);
1035 tmp
= (struct attr_abbrev
*) bfd_realloc (cur_abbrev
->attrs
, amt
);
1040 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
1042 struct abbrev_info
*abbrev
= abbrevs
[i
];
1046 free (abbrev
->attrs
);
1047 abbrev
= abbrev
->next
;
1052 cur_abbrev
->attrs
= tmp
;
1055 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
1056 = (enum dwarf_attribute
) abbrev_name
;
1057 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].form
1058 = (enum dwarf_form
) abbrev_form
;
1059 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].implicit_const
1061 ++cur_abbrev
->num_attrs
;
1064 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
1065 cur_abbrev
->next
= abbrevs
[hash_number
];
1066 abbrevs
[hash_number
] = cur_abbrev
;
1068 /* Get next abbreviation.
1069 Under Irix6 the abbreviations for a compilation unit are not
1070 always properly terminated with an abbrev number of 0.
1071 Exit loop if we encounter an abbreviation which we have
1072 already read (which means we are about to read the abbreviations
1073 for the next compile unit) or if the end of the abbreviation
1074 table is reached. */
1075 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
1076 >= stash
->dwarf_abbrev_size
)
1078 abbrev_number
= _bfd_safe_read_leb128 (abfd
, abbrev_ptr
,
1079 &bytes_read
, FALSE
, abbrev_end
);
1080 abbrev_ptr
+= bytes_read
;
1081 if (lookup_abbrev (abbrev_number
, abbrevs
) != NULL
)
1088 /* Returns true if the form is one which has a string value. */
1090 static inline bfd_boolean
1091 is_str_attr (enum dwarf_form form
)
1093 return (form
== DW_FORM_string
|| form
== DW_FORM_strp
1094 || form
== DW_FORM_line_strp
|| form
== DW_FORM_GNU_strp_alt
);
1097 /* Read and fill in the value of attribute ATTR as described by FORM.
1098 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1099 Returns an updated INFO_PTR taking into account the amount of data read. */
1102 read_attribute_value (struct attribute
* attr
,
1104 bfd_vma implicit_const
,
1105 struct comp_unit
* unit
,
1106 bfd_byte
* info_ptr
,
1107 bfd_byte
* info_ptr_end
)
1109 bfd
*abfd
= unit
->abfd
;
1110 unsigned int bytes_read
;
1111 struct dwarf_block
*blk
;
1114 if (info_ptr
>= info_ptr_end
&& form
!= DW_FORM_flag_present
)
1116 _bfd_error_handler (_("Dwarf Error: Info pointer extends beyond end of attributes"));
1117 bfd_set_error (bfd_error_bad_value
);
1121 attr
->form
= (enum dwarf_form
) form
;
1125 case DW_FORM_ref_addr
:
1126 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1128 if (unit
->version
== 3 || unit
->version
== 4)
1130 if (unit
->offset_size
== 4)
1131 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1133 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1134 info_ptr
+= unit
->offset_size
;
1139 attr
->u
.val
= read_address (unit
, info_ptr
, info_ptr_end
);
1140 info_ptr
+= unit
->addr_size
;
1142 case DW_FORM_GNU_ref_alt
:
1143 case DW_FORM_sec_offset
:
1144 if (unit
->offset_size
== 4)
1145 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1147 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
, info_ptr_end
);
1148 info_ptr
+= unit
->offset_size
;
1150 case DW_FORM_block2
:
1151 amt
= sizeof (struct dwarf_block
);
1152 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1155 blk
->size
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1157 blk
->data
= read_n_bytes (abfd
, info_ptr
, info_ptr_end
, blk
->size
);
1158 info_ptr
+= blk
->size
;
1161 case DW_FORM_block4
:
1162 amt
= sizeof (struct dwarf_block
);
1163 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1166 blk
->size
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1168 blk
->data
= read_n_bytes (abfd
, info_ptr
, info_ptr_end
, blk
->size
);
1169 info_ptr
+= blk
->size
;
1173 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1177 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1181 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1184 case DW_FORM_string
:
1185 attr
->u
.str
= read_string (abfd
, info_ptr
, info_ptr_end
, &bytes_read
);
1186 info_ptr
+= bytes_read
;
1189 attr
->u
.str
= read_indirect_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1190 info_ptr
+= bytes_read
;
1192 case DW_FORM_line_strp
:
1193 attr
->u
.str
= read_indirect_line_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1194 info_ptr
+= bytes_read
;
1196 case DW_FORM_GNU_strp_alt
:
1197 attr
->u
.str
= read_alt_indirect_string (unit
, info_ptr
, info_ptr_end
, &bytes_read
);
1198 info_ptr
+= bytes_read
;
1200 case DW_FORM_exprloc
:
1202 amt
= sizeof (struct dwarf_block
);
1203 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1206 blk
->size
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1207 FALSE
, info_ptr_end
);
1208 info_ptr
+= bytes_read
;
1209 blk
->data
= read_n_bytes (abfd
, info_ptr
, info_ptr_end
, blk
->size
);
1210 info_ptr
+= blk
->size
;
1213 case DW_FORM_block1
:
1214 amt
= sizeof (struct dwarf_block
);
1215 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1218 blk
->size
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1220 blk
->data
= read_n_bytes (abfd
, info_ptr
, info_ptr_end
, blk
->size
);
1221 info_ptr
+= blk
->size
;
1225 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1229 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1232 case DW_FORM_flag_present
:
1236 attr
->u
.sval
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1237 TRUE
, info_ptr_end
);
1238 info_ptr
+= bytes_read
;
1241 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1242 FALSE
, info_ptr_end
);
1243 info_ptr
+= bytes_read
;
1246 attr
->u
.val
= read_1_byte (abfd
, info_ptr
, info_ptr_end
);
1250 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
, info_ptr_end
);
1254 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
, info_ptr_end
);
1258 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1261 case DW_FORM_ref_sig8
:
1262 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
, info_ptr_end
);
1265 case DW_FORM_ref_udata
:
1266 attr
->u
.val
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1267 FALSE
, info_ptr_end
);
1268 info_ptr
+= bytes_read
;
1270 case DW_FORM_indirect
:
1271 form
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1272 FALSE
, info_ptr_end
);
1273 info_ptr
+= bytes_read
;
1274 if (form
== DW_FORM_implicit_const
)
1276 implicit_const
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
1277 TRUE
, info_ptr_end
);
1278 info_ptr
+= bytes_read
;
1280 info_ptr
= read_attribute_value (attr
, form
, implicit_const
, unit
,
1281 info_ptr
, info_ptr_end
);
1283 case DW_FORM_implicit_const
:
1284 attr
->form
= DW_FORM_sdata
;
1285 attr
->u
.sval
= implicit_const
;
1288 _bfd_error_handler (_("Dwarf Error: Invalid or unhandled FORM value: %#x."),
1290 bfd_set_error (bfd_error_bad_value
);
1296 /* Read an attribute described by an abbreviated attribute. */
1299 read_attribute (struct attribute
* attr
,
1300 struct attr_abbrev
* abbrev
,
1301 struct comp_unit
* unit
,
1302 bfd_byte
* info_ptr
,
1303 bfd_byte
* info_ptr_end
)
1305 attr
->name
= abbrev
->name
;
1306 info_ptr
= read_attribute_value (attr
, abbrev
->form
, abbrev
->implicit_const
,
1307 unit
, info_ptr
, info_ptr_end
);
1311 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1315 non_mangled (int lang
)
1325 case DW_LANG_Cobol74
:
1326 case DW_LANG_Cobol85
:
1327 case DW_LANG_Fortran77
:
1328 case DW_LANG_Pascal83
:
1338 /* Source line information table routines. */
1340 #define FILE_ALLOC_CHUNK 5
1341 #define DIR_ALLOC_CHUNK 5
1345 struct line_info
* prev_line
;
1349 unsigned int column
;
1350 unsigned int discriminator
;
1351 unsigned char op_index
;
1352 unsigned char end_sequence
; /* End of (sequential) code sequence. */
1363 struct line_sequence
1366 struct line_sequence
* prev_sequence
;
1367 struct line_info
* last_line
; /* Largest VMA. */
1368 struct line_info
** line_info_lookup
;
1369 bfd_size_type num_lines
;
1372 struct line_info_table
1375 unsigned int num_files
;
1376 unsigned int num_dirs
;
1377 unsigned int num_sequences
;
1380 struct fileinfo
* files
;
1381 struct line_sequence
* sequences
;
1382 struct line_info
* lcl_head
; /* Local head; used in 'add_line_info'. */
1385 /* Remember some information about each function. If the function is
1386 inlined (DW_TAG_inlined_subroutine) it may have two additional
1387 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1388 source code location where this function was inlined. */
1392 /* Pointer to previous function in list of all functions. */
1393 struct funcinfo
* prev_func
;
1394 /* Pointer to function one scope higher. */
1395 struct funcinfo
* caller_func
;
1396 /* Source location file name where caller_func inlines this func. */
1398 /* Source location file name. */
1400 /* Source location line number where caller_func inlines this func. */
1402 /* Source location line number. */
1405 bfd_boolean is_linkage
;
1407 struct arange arange
;
1408 /* Where the symbol is defined. */
1412 struct lookup_funcinfo
1414 /* Function information corresponding to this lookup table entry. */
1415 struct funcinfo
* funcinfo
;
1417 /* The lowest address for this specific function. */
1420 /* The highest address of this function before the lookup table is sorted.
1421 The highest address of all prior functions after the lookup table is
1422 sorted, which is used for binary search. */
1428 /* Pointer to previous variable in list of all variables */
1429 struct varinfo
*prev_var
;
1430 /* Source location file name */
1432 /* Source location line number */
1437 /* Where the symbol is defined */
1439 /* Is this a stack variable? */
1440 unsigned int stack
: 1;
1443 /* Return TRUE if NEW_LINE should sort after LINE. */
1445 static inline bfd_boolean
1446 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
1448 return (new_line
->address
> line
->address
1449 || (new_line
->address
== line
->address
1450 && (new_line
->op_index
> line
->op_index
1451 || (new_line
->op_index
== line
->op_index
1452 && new_line
->end_sequence
< line
->end_sequence
))));
1456 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1457 that the list is sorted. Note that the line_info list is sorted from
1458 highest to lowest VMA (with possible duplicates); that is,
1459 line_info->prev_line always accesses an equal or smaller VMA. */
1462 add_line_info (struct line_info_table
*table
,
1464 unsigned char op_index
,
1467 unsigned int column
,
1468 unsigned int discriminator
,
1471 bfd_size_type amt
= sizeof (struct line_info
);
1472 struct line_sequence
* seq
= table
->sequences
;
1473 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
1478 /* Set member data of 'info'. */
1479 info
->prev_line
= NULL
;
1480 info
->address
= address
;
1481 info
->op_index
= op_index
;
1483 info
->column
= column
;
1484 info
->discriminator
= discriminator
;
1485 info
->end_sequence
= end_sequence
;
1487 if (filename
&& filename
[0])
1489 info
->filename
= (char *) bfd_alloc (table
->abfd
, strlen (filename
) + 1);
1490 if (info
->filename
== NULL
)
1492 strcpy (info
->filename
, filename
);
1495 info
->filename
= NULL
;
1497 /* Find the correct location for 'info'. Normally we will receive
1498 new line_info data 1) in order and 2) with increasing VMAs.
1499 However some compilers break the rules (cf. decode_line_info) and
1500 so we include some heuristics for quickly finding the correct
1501 location for 'info'. In particular, these heuristics optimize for
1502 the common case in which the VMA sequence that we receive is a
1503 list of locally sorted VMAs such as
1504 p...z a...j (where a < j < p < z)
1506 Note: table->lcl_head is used to head an *actual* or *possible*
1507 sub-sequence within the list (such as a...j) that is not directly
1508 headed by table->last_line
1510 Note: we may receive duplicate entries from 'decode_line_info'. */
1513 && seq
->last_line
->address
== address
1514 && seq
->last_line
->op_index
== op_index
1515 && seq
->last_line
->end_sequence
== end_sequence
)
1517 /* We only keep the last entry with the same address and end
1518 sequence. See PR ld/4986. */
1519 if (table
->lcl_head
== seq
->last_line
)
1520 table
->lcl_head
= info
;
1521 info
->prev_line
= seq
->last_line
->prev_line
;
1522 seq
->last_line
= info
;
1524 else if (!seq
|| seq
->last_line
->end_sequence
)
1526 /* Start a new line sequence. */
1527 amt
= sizeof (struct line_sequence
);
1528 seq
= (struct line_sequence
*) bfd_malloc (amt
);
1531 seq
->low_pc
= address
;
1532 seq
->prev_sequence
= table
->sequences
;
1533 seq
->last_line
= info
;
1534 table
->lcl_head
= info
;
1535 table
->sequences
= seq
;
1536 table
->num_sequences
++;
1538 else if (new_line_sorts_after (info
, seq
->last_line
))
1540 /* Normal case: add 'info' to the beginning of the current sequence. */
1541 info
->prev_line
= seq
->last_line
;
1542 seq
->last_line
= info
;
1544 /* lcl_head: initialize to head a *possible* sequence at the end. */
1545 if (!table
->lcl_head
)
1546 table
->lcl_head
= info
;
1548 else if (!new_line_sorts_after (info
, table
->lcl_head
)
1549 && (!table
->lcl_head
->prev_line
1550 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
1552 /* Abnormal but easy: lcl_head is the head of 'info'. */
1553 info
->prev_line
= table
->lcl_head
->prev_line
;
1554 table
->lcl_head
->prev_line
= info
;
1558 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1559 are valid heads for 'info'. Reset 'lcl_head'. */
1560 struct line_info
* li2
= seq
->last_line
; /* Always non-NULL. */
1561 struct line_info
* li1
= li2
->prev_line
;
1565 if (!new_line_sorts_after (info
, li2
)
1566 && new_line_sorts_after (info
, li1
))
1569 li2
= li1
; /* always non-NULL */
1570 li1
= li1
->prev_line
;
1572 table
->lcl_head
= li2
;
1573 info
->prev_line
= table
->lcl_head
->prev_line
;
1574 table
->lcl_head
->prev_line
= info
;
1575 if (address
< seq
->low_pc
)
1576 seq
->low_pc
= address
;
1581 /* Extract a fully qualified filename from a line info table.
1582 The returned string has been malloc'ed and it is the caller's
1583 responsibility to free it. */
1586 concat_filename (struct line_info_table
*table
, unsigned int file
)
1590 if (file
- 1 >= table
->num_files
)
1592 /* FILE == 0 means unknown. */
1595 (_("Dwarf Error: mangled line number section (bad file number)."));
1596 return strdup ("<unknown>");
1599 filename
= table
->files
[file
- 1].name
;
1601 if (!IS_ABSOLUTE_PATH (filename
))
1603 char *dir_name
= NULL
;
1604 char *subdir_name
= NULL
;
1608 if (table
->files
[file
- 1].dir
1609 /* PR 17512: file: 0317e960. */
1610 && table
->files
[file
- 1].dir
<= table
->num_dirs
1611 /* PR 17512: file: 7f3d2e4b. */
1612 && table
->dirs
!= NULL
)
1613 subdir_name
= table
->dirs
[table
->files
[file
- 1].dir
- 1];
1615 if (!subdir_name
|| !IS_ABSOLUTE_PATH (subdir_name
))
1616 dir_name
= table
->comp_dir
;
1620 dir_name
= subdir_name
;
1625 return strdup (filename
);
1627 len
= strlen (dir_name
) + strlen (filename
) + 2;
1631 len
+= strlen (subdir_name
) + 1;
1632 name
= (char *) bfd_malloc (len
);
1634 sprintf (name
, "%s/%s/%s", dir_name
, subdir_name
, filename
);
1638 name
= (char *) bfd_malloc (len
);
1640 sprintf (name
, "%s/%s", dir_name
, filename
);
1646 return strdup (filename
);
1650 arange_add (const struct comp_unit
*unit
, struct arange
*first_arange
,
1651 bfd_vma low_pc
, bfd_vma high_pc
)
1653 struct arange
*arange
;
1655 /* Ignore empty ranges. */
1656 if (low_pc
== high_pc
)
1659 /* If the first arange is empty, use it. */
1660 if (first_arange
->high
== 0)
1662 first_arange
->low
= low_pc
;
1663 first_arange
->high
= high_pc
;
1667 /* Next see if we can cheaply extend an existing range. */
1668 arange
= first_arange
;
1671 if (low_pc
== arange
->high
)
1673 arange
->high
= high_pc
;
1676 if (high_pc
== arange
->low
)
1678 arange
->low
= low_pc
;
1681 arange
= arange
->next
;
1685 /* Need to allocate a new arange and insert it into the arange list.
1686 Order isn't significant, so just insert after the first arange. */
1687 arange
= (struct arange
*) bfd_alloc (unit
->abfd
, sizeof (*arange
));
1690 arange
->low
= low_pc
;
1691 arange
->high
= high_pc
;
1692 arange
->next
= first_arange
->next
;
1693 first_arange
->next
= arange
;
1697 /* Compare function for line sequences. */
1700 compare_sequences (const void* a
, const void* b
)
1702 const struct line_sequence
* seq1
= a
;
1703 const struct line_sequence
* seq2
= b
;
1705 /* Sort by low_pc as the primary key. */
1706 if (seq1
->low_pc
< seq2
->low_pc
)
1708 if (seq1
->low_pc
> seq2
->low_pc
)
1711 /* If low_pc values are equal, sort in reverse order of
1712 high_pc, so that the largest region comes first. */
1713 if (seq1
->last_line
->address
< seq2
->last_line
->address
)
1715 if (seq1
->last_line
->address
> seq2
->last_line
->address
)
1718 if (seq1
->last_line
->op_index
< seq2
->last_line
->op_index
)
1720 if (seq1
->last_line
->op_index
> seq2
->last_line
->op_index
)
1726 /* Construct the line information table for quick lookup. */
1729 build_line_info_table (struct line_info_table
* table
,
1730 struct line_sequence
* seq
)
1733 struct line_info
** line_info_lookup
;
1734 struct line_info
* each_line
;
1735 unsigned int num_lines
;
1736 unsigned int line_index
;
1738 if (seq
->line_info_lookup
!= NULL
)
1741 /* Count the number of line information entries. We could do this while
1742 scanning the debug information, but some entries may be added via
1743 lcl_head without having a sequence handy to increment the number of
1746 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
1752 /* Allocate space for the line information lookup table. */
1753 amt
= sizeof (struct line_info
*) * num_lines
;
1754 line_info_lookup
= (struct line_info
**) bfd_alloc (table
->abfd
, amt
);
1755 if (line_info_lookup
== NULL
)
1758 /* Create the line information lookup table. */
1759 line_index
= num_lines
;
1760 for (each_line
= seq
->last_line
; each_line
; each_line
= each_line
->prev_line
)
1761 line_info_lookup
[--line_index
] = each_line
;
1763 BFD_ASSERT (line_index
== 0);
1765 seq
->num_lines
= num_lines
;
1766 seq
->line_info_lookup
= line_info_lookup
;
1771 /* Sort the line sequences for quick lookup. */
1774 sort_line_sequences (struct line_info_table
* table
)
1777 struct line_sequence
* sequences
;
1778 struct line_sequence
* seq
;
1780 unsigned int num_sequences
= table
->num_sequences
;
1781 bfd_vma last_high_pc
;
1783 if (num_sequences
== 0)
1786 /* Allocate space for an array of sequences. */
1787 amt
= sizeof (struct line_sequence
) * num_sequences
;
1788 sequences
= (struct line_sequence
*) bfd_alloc (table
->abfd
, amt
);
1789 if (sequences
== NULL
)
1792 /* Copy the linked list into the array, freeing the original nodes. */
1793 seq
= table
->sequences
;
1794 for (n
= 0; n
< num_sequences
; n
++)
1796 struct line_sequence
* last_seq
= seq
;
1799 sequences
[n
].low_pc
= seq
->low_pc
;
1800 sequences
[n
].prev_sequence
= NULL
;
1801 sequences
[n
].last_line
= seq
->last_line
;
1802 sequences
[n
].line_info_lookup
= NULL
;
1803 sequences
[n
].num_lines
= 0;
1804 seq
= seq
->prev_sequence
;
1807 BFD_ASSERT (seq
== NULL
);
1809 qsort (sequences
, n
, sizeof (struct line_sequence
), compare_sequences
);
1811 /* Make the list binary-searchable by trimming overlapping entries
1812 and removing nested entries. */
1814 last_high_pc
= sequences
[0].last_line
->address
;
1815 for (n
= 1; n
< table
->num_sequences
; n
++)
1817 if (sequences
[n
].low_pc
< last_high_pc
)
1819 if (sequences
[n
].last_line
->address
<= last_high_pc
)
1820 /* Skip nested entries. */
1823 /* Trim overlapping entries. */
1824 sequences
[n
].low_pc
= last_high_pc
;
1826 last_high_pc
= sequences
[n
].last_line
->address
;
1827 if (n
> num_sequences
)
1829 /* Close up the gap. */
1830 sequences
[num_sequences
].low_pc
= sequences
[n
].low_pc
;
1831 sequences
[num_sequences
].last_line
= sequences
[n
].last_line
;
1836 table
->sequences
= sequences
;
1837 table
->num_sequences
= num_sequences
;
1841 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
1844 line_info_add_include_dir (struct line_info_table
*table
, char *cur_dir
)
1846 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1851 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1852 amt
*= sizeof (char *);
1854 tmp
= (char **) bfd_realloc (table
->dirs
, amt
);
1860 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1865 line_info_add_include_dir_stub (struct line_info_table
*table
, char *cur_dir
,
1866 unsigned int dir ATTRIBUTE_UNUSED
,
1867 unsigned int xtime ATTRIBUTE_UNUSED
,
1868 unsigned int size ATTRIBUTE_UNUSED
)
1870 return line_info_add_include_dir (table
, cur_dir
);
1873 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
1876 line_info_add_file_name (struct line_info_table
*table
, char *cur_file
,
1877 unsigned int dir
, unsigned int xtime
,
1880 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1882 struct fileinfo
*tmp
;
1885 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1886 amt
*= sizeof (struct fileinfo
);
1888 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1894 table
->files
[table
->num_files
].name
= cur_file
;
1895 table
->files
[table
->num_files
].dir
= dir
;
1896 table
->files
[table
->num_files
].time
= xtime
;
1897 table
->files
[table
->num_files
].size
= size
;
1902 /* Read directory or file name entry format, starting with byte of
1903 format count entries, ULEB128 pairs of entry formats, ULEB128 of
1904 entries count and the entries themselves in the described entry
1908 read_formatted_entries (struct comp_unit
*unit
, bfd_byte
**bufp
,
1909 bfd_byte
*buf_end
, struct line_info_table
*table
,
1910 bfd_boolean (*callback
) (struct line_info_table
*table
,
1916 bfd
*abfd
= unit
->abfd
;
1917 bfd_byte format_count
, formati
;
1918 bfd_vma data_count
, datai
;
1919 bfd_byte
*buf
= *bufp
;
1920 bfd_byte
*format_header_data
;
1921 unsigned int bytes_read
;
1923 format_count
= read_1_byte (abfd
, buf
, buf_end
);
1925 format_header_data
= buf
;
1926 for (formati
= 0; formati
< format_count
; formati
++)
1928 _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
1930 _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
1934 data_count
= _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
, buf_end
);
1936 if (format_count
== 0 && data_count
!= 0)
1938 _bfd_error_handler (_("Dwarf Error: Zero format count."));
1939 bfd_set_error (bfd_error_bad_value
);
1943 for (datai
= 0; datai
< data_count
; datai
++)
1945 bfd_byte
*format
= format_header_data
;
1948 for (formati
= 0; formati
< format_count
; formati
++)
1950 bfd_vma content_type
, form
;
1952 char **stringp
= &string_trash
;
1953 unsigned int uint_trash
, *uintp
= &uint_trash
;
1955 content_type
= _bfd_safe_read_leb128 (abfd
, format
, &bytes_read
,
1957 format
+= bytes_read
;
1958 switch (content_type
)
1963 case DW_LNCT_directory_index
:
1966 case DW_LNCT_timestamp
:
1976 (_("Dwarf Error: Unknown format content type %Lu."),
1978 bfd_set_error (bfd_error_bad_value
);
1982 form
= _bfd_safe_read_leb128 (abfd
, format
, &bytes_read
, FALSE
,
1984 format
+= bytes_read
;
1987 case DW_FORM_string
:
1988 *stringp
= read_string (abfd
, buf
, buf_end
, &bytes_read
);
1992 case DW_FORM_line_strp
:
1993 *stringp
= read_indirect_line_string (unit
, buf
, buf_end
, &bytes_read
);
1998 *uintp
= read_1_byte (abfd
, buf
, buf_end
);
2003 *uintp
= read_2_bytes (abfd
, buf
, buf_end
);
2008 *uintp
= read_4_bytes (abfd
, buf
, buf_end
);
2013 *uintp
= read_8_bytes (abfd
, buf
, buf_end
);
2018 *uintp
= _bfd_safe_read_leb128 (abfd
, buf
, &bytes_read
, FALSE
,
2024 /* It is valid only for DW_LNCT_timestamp which is ignored by
2030 if (!callback (table
, fe
.name
, fe
.dir
, fe
.time
, fe
.size
))
2038 /* Decode the line number information for UNIT. */
2040 static struct line_info_table
*
2041 decode_line_info (struct comp_unit
*unit
, struct dwarf2_debug
*stash
)
2043 bfd
*abfd
= unit
->abfd
;
2044 struct line_info_table
* table
;
2047 struct line_head lh
;
2048 unsigned int i
, bytes_read
, offset_size
;
2049 char *cur_file
, *cur_dir
;
2050 unsigned char op_code
, extended_op
, adj_opcode
;
2051 unsigned int exop_len
;
2054 if (! read_section (abfd
, &stash
->debug_sections
[debug_line
],
2055 stash
->syms
, unit
->line_offset
,
2056 &stash
->dwarf_line_buffer
, &stash
->dwarf_line_size
))
2059 amt
= sizeof (struct line_info_table
);
2060 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
2064 table
->comp_dir
= unit
->comp_dir
;
2066 table
->num_files
= 0;
2067 table
->files
= NULL
;
2069 table
->num_dirs
= 0;
2072 table
->num_sequences
= 0;
2073 table
->sequences
= NULL
;
2075 table
->lcl_head
= NULL
;
2077 if (stash
->dwarf_line_size
< 16)
2080 (_("Dwarf Error: Line info section is too small (%Ld)"),
2081 stash
->dwarf_line_size
);
2082 bfd_set_error (bfd_error_bad_value
);
2085 line_ptr
= stash
->dwarf_line_buffer
+ unit
->line_offset
;
2086 line_end
= stash
->dwarf_line_buffer
+ stash
->dwarf_line_size
;
2088 /* Read in the prologue. */
2089 lh
.total_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2092 if (lh
.total_length
== 0xffffffff)
2094 lh
.total_length
= read_8_bytes (abfd
, line_ptr
, line_end
);
2098 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
2100 /* Handle (non-standard) 64-bit DWARF2 formats. */
2101 lh
.total_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2106 if (lh
.total_length
> (size_t) (line_end
- line_ptr
))
2109 /* xgettext: c-format */
2110 (_("Dwarf Error: Line info data is bigger (%#Lx)"
2111 " than the space remaining in the section (%#lx)"),
2112 lh
.total_length
, (unsigned long) (line_end
- line_ptr
));
2113 bfd_set_error (bfd_error_bad_value
);
2117 line_end
= line_ptr
+ lh
.total_length
;
2119 lh
.version
= read_2_bytes (abfd
, line_ptr
, line_end
);
2120 if (lh
.version
< 2 || lh
.version
> 5)
2123 (_("Dwarf Error: Unhandled .debug_line version %d."), lh
.version
);
2124 bfd_set_error (bfd_error_bad_value
);
2129 if (line_ptr
+ offset_size
+ (lh
.version
>= 5 ? 8 : (lh
.version
>= 4 ? 6 : 5))
2133 (_("Dwarf Error: Ran out of room reading prologue"));
2134 bfd_set_error (bfd_error_bad_value
);
2138 if (lh
.version
>= 5)
2140 unsigned int segment_selector_size
;
2142 /* Skip address size. */
2143 read_1_byte (abfd
, line_ptr
, line_end
);
2146 segment_selector_size
= read_1_byte (abfd
, line_ptr
, line_end
);
2148 if (segment_selector_size
!= 0)
2151 (_("Dwarf Error: Line info unsupported segment selector size %u."),
2152 segment_selector_size
);
2153 bfd_set_error (bfd_error_bad_value
);
2158 if (offset_size
== 4)
2159 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
, line_end
);
2161 lh
.prologue_length
= read_8_bytes (abfd
, line_ptr
, line_end
);
2162 line_ptr
+= offset_size
;
2164 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
, line_end
);
2167 if (lh
.version
>= 4)
2169 lh
.maximum_ops_per_insn
= read_1_byte (abfd
, line_ptr
, line_end
);
2173 lh
.maximum_ops_per_insn
= 1;
2175 if (lh
.maximum_ops_per_insn
== 0)
2178 (_("Dwarf Error: Invalid maximum operations per instruction."));
2179 bfd_set_error (bfd_error_bad_value
);
2183 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
, line_end
);
2186 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
, line_end
);
2189 lh
.line_range
= read_1_byte (abfd
, line_ptr
, line_end
);
2192 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
, line_end
);
2195 if (line_ptr
+ (lh
.opcode_base
- 1) >= line_end
)
2197 _bfd_error_handler (_("Dwarf Error: Ran out of room reading opcodes"));
2198 bfd_set_error (bfd_error_bad_value
);
2202 amt
= lh
.opcode_base
* sizeof (unsigned char);
2203 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
2205 lh
.standard_opcode_lengths
[0] = 1;
2207 for (i
= 1; i
< lh
.opcode_base
; ++i
)
2209 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
, line_end
);
2213 if (lh
.version
>= 5)
2215 /* Read directory table. */
2216 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2217 line_info_add_include_dir_stub
))
2220 /* Read file name table. */
2221 if (!read_formatted_entries (unit
, &line_ptr
, line_end
, table
,
2222 line_info_add_file_name
))
2227 /* Read directory table. */
2228 while ((cur_dir
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
)) != NULL
)
2230 line_ptr
+= bytes_read
;
2232 if (!line_info_add_include_dir (table
, cur_dir
))
2236 line_ptr
+= bytes_read
;
2238 /* Read file name table. */
2239 while ((cur_file
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
)) != NULL
)
2241 unsigned int dir
, xtime
, size
;
2243 line_ptr
+= bytes_read
;
2245 dir
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2246 line_ptr
+= bytes_read
;
2247 xtime
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2248 line_ptr
+= bytes_read
;
2249 size
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
, FALSE
, line_end
);
2250 line_ptr
+= bytes_read
;
2252 if (!line_info_add_file_name (table
, cur_file
, dir
, xtime
, size
))
2256 line_ptr
+= bytes_read
;
2259 /* Read the statement sequences until there's nothing left. */
2260 while (line_ptr
< line_end
)
2262 /* State machine registers. */
2263 bfd_vma address
= 0;
2264 unsigned char op_index
= 0;
2265 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
2266 unsigned int line
= 1;
2267 unsigned int column
= 0;
2268 unsigned int discriminator
= 0;
2269 int is_stmt
= lh
.default_is_stmt
;
2270 int end_sequence
= 0;
2271 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2272 compilers generate address sequences that are wildly out of
2273 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2274 for ia64-Linux). Thus, to determine the low and high
2275 address, we must compare on every DW_LNS_copy, etc. */
2276 bfd_vma low_pc
= (bfd_vma
) -1;
2277 bfd_vma high_pc
= 0;
2279 /* Decode the table. */
2280 while (! end_sequence
)
2282 op_code
= read_1_byte (abfd
, line_ptr
, line_end
);
2285 if (op_code
>= lh
.opcode_base
)
2287 /* Special operand. */
2288 adj_opcode
= op_code
- lh
.opcode_base
;
2289 if (lh
.line_range
== 0)
2291 if (lh
.maximum_ops_per_insn
== 1)
2292 address
+= (adj_opcode
/ lh
.line_range
2293 * lh
.minimum_instruction_length
);
2296 address
+= ((op_index
+ adj_opcode
/ lh
.line_range
)
2297 / lh
.maximum_ops_per_insn
2298 * lh
.minimum_instruction_length
);
2299 op_index
= ((op_index
+ adj_opcode
/ lh
.line_range
)
2300 % lh
.maximum_ops_per_insn
);
2302 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
2303 /* Append row to matrix using current values. */
2304 if (!add_line_info (table
, address
, op_index
, filename
,
2305 line
, column
, discriminator
, 0))
2308 if (address
< low_pc
)
2310 if (address
> high_pc
)
2313 else switch (op_code
)
2315 case DW_LNS_extended_op
:
2316 exop_len
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2318 line_ptr
+= bytes_read
;
2319 extended_op
= read_1_byte (abfd
, line_ptr
, line_end
);
2322 switch (extended_op
)
2324 case DW_LNE_end_sequence
:
2326 if (!add_line_info (table
, address
, op_index
, filename
, line
,
2327 column
, discriminator
, end_sequence
))
2330 if (address
< low_pc
)
2332 if (address
> high_pc
)
2334 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
2337 case DW_LNE_set_address
:
2338 address
= read_address (unit
, line_ptr
, line_end
);
2340 line_ptr
+= unit
->addr_size
;
2342 case DW_LNE_define_file
:
2343 cur_file
= read_string (abfd
, line_ptr
, line_end
, &bytes_read
);
2344 line_ptr
+= bytes_read
;
2345 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
2347 struct fileinfo
*tmp
;
2349 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
2350 amt
*= sizeof (struct fileinfo
);
2351 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
2356 table
->files
[table
->num_files
].name
= cur_file
;
2357 table
->files
[table
->num_files
].dir
=
2358 _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2360 line_ptr
+= bytes_read
;
2361 table
->files
[table
->num_files
].time
=
2362 _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2364 line_ptr
+= bytes_read
;
2365 table
->files
[table
->num_files
].size
=
2366 _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2368 line_ptr
+= bytes_read
;
2371 case DW_LNE_set_discriminator
:
2373 _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2375 line_ptr
+= bytes_read
;
2377 case DW_LNE_HP_source_file_correlation
:
2378 line_ptr
+= exop_len
- 1;
2382 (_("Dwarf Error: mangled line number section."));
2383 bfd_set_error (bfd_error_bad_value
);
2385 if (filename
!= NULL
)
2391 if (!add_line_info (table
, address
, op_index
,
2392 filename
, line
, column
, discriminator
, 0))
2395 if (address
< low_pc
)
2397 if (address
> high_pc
)
2400 case DW_LNS_advance_pc
:
2401 if (lh
.maximum_ops_per_insn
== 1)
2402 address
+= (lh
.minimum_instruction_length
2403 * _bfd_safe_read_leb128 (abfd
, line_ptr
,
2408 bfd_vma adjust
= _bfd_safe_read_leb128 (abfd
, line_ptr
,
2411 address
= ((op_index
+ adjust
) / lh
.maximum_ops_per_insn
2412 * lh
.minimum_instruction_length
);
2413 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2415 line_ptr
+= bytes_read
;
2417 case DW_LNS_advance_line
:
2418 line
+= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2420 line_ptr
+= bytes_read
;
2422 case DW_LNS_set_file
:
2426 /* The file and directory tables are 0
2427 based, the references are 1 based. */
2428 file
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2430 line_ptr
+= bytes_read
;
2433 filename
= concat_filename (table
, file
);
2436 case DW_LNS_set_column
:
2437 column
= _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2439 line_ptr
+= bytes_read
;
2441 case DW_LNS_negate_stmt
:
2442 is_stmt
= (!is_stmt
);
2444 case DW_LNS_set_basic_block
:
2446 case DW_LNS_const_add_pc
:
2447 if (lh
.line_range
== 0)
2449 if (lh
.maximum_ops_per_insn
== 1)
2450 address
+= (lh
.minimum_instruction_length
2451 * ((255 - lh
.opcode_base
) / lh
.line_range
));
2454 bfd_vma adjust
= ((255 - lh
.opcode_base
) / lh
.line_range
);
2455 address
+= (lh
.minimum_instruction_length
2456 * ((op_index
+ adjust
)
2457 / lh
.maximum_ops_per_insn
));
2458 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
2461 case DW_LNS_fixed_advance_pc
:
2462 address
+= read_2_bytes (abfd
, line_ptr
, line_end
);
2467 /* Unknown standard opcode, ignore it. */
2468 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
2470 (void) _bfd_safe_read_leb128 (abfd
, line_ptr
, &bytes_read
,
2472 line_ptr
+= bytes_read
;
2482 if (sort_line_sequences (table
))
2486 while (table
->sequences
!= NULL
)
2488 struct line_sequence
* seq
= table
->sequences
;
2489 table
->sequences
= table
->sequences
->prev_sequence
;
2492 if (table
->files
!= NULL
)
2493 free (table
->files
);
2494 if (table
->dirs
!= NULL
)
2499 /* If ADDR is within TABLE set the output parameters and return the
2500 range of addresses covered by the entry used to fill them out.
2501 Otherwise set * FILENAME_PTR to NULL and return 0.
2502 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2503 are pointers to the objects to be filled in. */
2506 lookup_address_in_line_info_table (struct line_info_table
*table
,
2508 const char **filename_ptr
,
2509 unsigned int *linenumber_ptr
,
2510 unsigned int *discriminator_ptr
)
2512 struct line_sequence
*seq
= NULL
;
2513 struct line_info
*info
;
2516 /* Binary search the array of sequences. */
2518 high
= table
->num_sequences
;
2521 mid
= (low
+ high
) / 2;
2522 seq
= &table
->sequences
[mid
];
2523 if (addr
< seq
->low_pc
)
2525 else if (addr
>= seq
->last_line
->address
)
2531 /* Check for a valid sequence. */
2532 if (!seq
|| addr
< seq
->low_pc
|| addr
>= seq
->last_line
->address
)
2535 if (!build_line_info_table (table
, seq
))
2538 /* Binary search the array of line information. */
2540 high
= seq
->num_lines
;
2544 mid
= (low
+ high
) / 2;
2545 info
= seq
->line_info_lookup
[mid
];
2546 if (addr
< info
->address
)
2548 else if (addr
>= seq
->line_info_lookup
[mid
+ 1]->address
)
2554 /* Check for a valid line information entry. */
2556 && addr
>= info
->address
2557 && addr
< seq
->line_info_lookup
[mid
+ 1]->address
2558 && !(info
->end_sequence
|| info
== seq
->last_line
))
2560 *filename_ptr
= info
->filename
;
2561 *linenumber_ptr
= info
->line
;
2562 if (discriminator_ptr
)
2563 *discriminator_ptr
= info
->discriminator
;
2564 return seq
->last_line
->address
- seq
->low_pc
;
2568 *filename_ptr
= NULL
;
2572 /* Read in the .debug_ranges section for future reference. */
2575 read_debug_ranges (struct comp_unit
* unit
)
2577 struct dwarf2_debug
* stash
= unit
->stash
;
2579 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_ranges
],
2581 &stash
->dwarf_ranges_buffer
,
2582 &stash
->dwarf_ranges_size
);
2585 /* Function table functions. */
2588 compare_lookup_funcinfos (const void * a
, const void * b
)
2590 const struct lookup_funcinfo
* lookup1
= a
;
2591 const struct lookup_funcinfo
* lookup2
= b
;
2593 if (lookup1
->low_addr
< lookup2
->low_addr
)
2595 if (lookup1
->low_addr
> lookup2
->low_addr
)
2597 if (lookup1
->high_addr
< lookup2
->high_addr
)
2599 if (lookup1
->high_addr
> lookup2
->high_addr
)
2606 build_lookup_funcinfo_table (struct comp_unit
* unit
)
2608 struct lookup_funcinfo
*lookup_funcinfo_table
= unit
->lookup_funcinfo_table
;
2609 unsigned int number_of_functions
= unit
->number_of_functions
;
2610 struct funcinfo
*each
;
2611 struct lookup_funcinfo
*entry
;
2613 struct arange
*range
;
2614 bfd_vma low_addr
, high_addr
;
2616 if (lookup_funcinfo_table
|| number_of_functions
== 0)
2619 /* Create the function info lookup table. */
2620 lookup_funcinfo_table
= (struct lookup_funcinfo
*)
2621 bfd_malloc (number_of_functions
* sizeof (struct lookup_funcinfo
));
2622 if (lookup_funcinfo_table
== NULL
)
2625 /* Populate the function info lookup table. */
2626 func_index
= number_of_functions
;
2627 for (each
= unit
->function_table
; each
; each
= each
->prev_func
)
2629 entry
= &lookup_funcinfo_table
[--func_index
];
2630 entry
->funcinfo
= each
;
2632 /* Calculate the lowest and highest address for this function entry. */
2633 low_addr
= entry
->funcinfo
->arange
.low
;
2634 high_addr
= entry
->funcinfo
->arange
.high
;
2636 for (range
= entry
->funcinfo
->arange
.next
; range
; range
= range
->next
)
2638 if (range
->low
< low_addr
)
2639 low_addr
= range
->low
;
2640 if (range
->high
> high_addr
)
2641 high_addr
= range
->high
;
2644 entry
->low_addr
= low_addr
;
2645 entry
->high_addr
= high_addr
;
2648 BFD_ASSERT (func_index
== 0);
2650 /* Sort the function by address. */
2651 qsort (lookup_funcinfo_table
,
2652 number_of_functions
,
2653 sizeof (struct lookup_funcinfo
),
2654 compare_lookup_funcinfos
);
2656 /* Calculate the high watermark for each function in the lookup table. */
2657 high_addr
= lookup_funcinfo_table
[0].high_addr
;
2658 for (func_index
= 1; func_index
< number_of_functions
; func_index
++)
2660 entry
= &lookup_funcinfo_table
[func_index
];
2661 if (entry
->high_addr
> high_addr
)
2662 high_addr
= entry
->high_addr
;
2664 entry
->high_addr
= high_addr
;
2667 unit
->lookup_funcinfo_table
= lookup_funcinfo_table
;
2671 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
2672 TRUE. Note that we need to find the function that has the smallest range
2673 that contains ADDR, to handle inlined functions without depending upon
2674 them being ordered in TABLE by increasing range. */
2677 lookup_address_in_function_table (struct comp_unit
*unit
,
2679 struct funcinfo
**function_ptr
)
2681 unsigned int number_of_functions
= unit
->number_of_functions
;
2682 struct lookup_funcinfo
* lookup_funcinfo
= NULL
;
2683 struct funcinfo
* funcinfo
= NULL
;
2684 struct funcinfo
* best_fit
= NULL
;
2685 bfd_vma best_fit_len
= 0;
2686 bfd_size_type low
, high
, mid
, first
;
2687 struct arange
*arange
;
2689 if (number_of_functions
== 0)
2692 if (!build_lookup_funcinfo_table (unit
))
2695 if (unit
->lookup_funcinfo_table
[number_of_functions
- 1].high_addr
< addr
)
2698 /* Find the first function in the lookup table which may contain the
2699 specified address. */
2701 high
= number_of_functions
;
2705 mid
= (low
+ high
) / 2;
2706 lookup_funcinfo
= &unit
->lookup_funcinfo_table
[mid
];
2707 if (addr
< lookup_funcinfo
->low_addr
)
2709 else if (addr
>= lookup_funcinfo
->high_addr
)
2715 /* Find the 'best' match for the address. The prior algorithm defined the
2716 best match as the function with the smallest address range containing
2717 the specified address. This definition should probably be changed to the
2718 innermost inline routine containing the address, but right now we want
2719 to get the same results we did before. */
2720 while (first
< number_of_functions
)
2722 if (addr
< unit
->lookup_funcinfo_table
[first
].low_addr
)
2724 funcinfo
= unit
->lookup_funcinfo_table
[first
].funcinfo
;
2726 for (arange
= &funcinfo
->arange
; arange
; arange
= arange
->next
)
2728 if (addr
< arange
->low
|| addr
>= arange
->high
)
2732 || arange
->high
- arange
->low
< best_fit_len
2733 /* The following comparison is designed to return the same
2734 match as the previous algorithm for routines which have the
2735 same best fit length. */
2736 || (arange
->high
- arange
->low
== best_fit_len
2737 && funcinfo
> best_fit
))
2739 best_fit
= funcinfo
;
2740 best_fit_len
= arange
->high
- arange
->low
;
2750 *function_ptr
= best_fit
;
2754 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2755 and LINENUMBER_PTR, and return TRUE. */
2758 lookup_symbol_in_function_table (struct comp_unit
*unit
,
2761 const char **filename_ptr
,
2762 unsigned int *linenumber_ptr
)
2764 struct funcinfo
* each_func
;
2765 struct funcinfo
* best_fit
= NULL
;
2766 bfd_vma best_fit_len
= 0;
2767 struct arange
*arange
;
2768 const char *name
= bfd_asymbol_name (sym
);
2769 asection
*sec
= bfd_get_section (sym
);
2771 for (each_func
= unit
->function_table
;
2773 each_func
= each_func
->prev_func
)
2775 for (arange
= &each_func
->arange
;
2777 arange
= arange
->next
)
2779 if ((!each_func
->sec
|| each_func
->sec
== sec
)
2780 && addr
>= arange
->low
2781 && addr
< arange
->high
2783 && strcmp (name
, each_func
->name
) == 0
2785 || arange
->high
- arange
->low
< best_fit_len
))
2787 best_fit
= each_func
;
2788 best_fit_len
= arange
->high
- arange
->low
;
2795 best_fit
->sec
= sec
;
2796 *filename_ptr
= best_fit
->file
;
2797 *linenumber_ptr
= best_fit
->line
;
2804 /* Variable table functions. */
2806 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
2807 LINENUMBER_PTR, and return TRUE. */
2810 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
2813 const char **filename_ptr
,
2814 unsigned int *linenumber_ptr
)
2816 const char *name
= bfd_asymbol_name (sym
);
2817 asection
*sec
= bfd_get_section (sym
);
2818 struct varinfo
* each
;
2820 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
2821 if (each
->stack
== 0
2822 && each
->file
!= NULL
2823 && each
->name
!= NULL
2824 && each
->addr
== addr
2825 && (!each
->sec
|| each
->sec
== sec
)
2826 && strcmp (name
, each
->name
) == 0)
2832 *filename_ptr
= each
->file
;
2833 *linenumber_ptr
= each
->line
;
2841 find_abstract_instance_name (struct comp_unit
*unit
,
2842 bfd_byte
*orig_info_ptr
,
2843 struct attribute
*attr_ptr
,
2845 bfd_boolean
*is_linkage
)
2847 bfd
*abfd
= unit
->abfd
;
2849 bfd_byte
*info_ptr_end
;
2850 unsigned int abbrev_number
, bytes_read
, i
;
2851 struct abbrev_info
*abbrev
;
2852 bfd_uint64_t die_ref
= attr_ptr
->u
.val
;
2853 struct attribute attr
;
2854 const char *name
= NULL
;
2856 /* DW_FORM_ref_addr can reference an entry in a different CU. It
2857 is an offset from the .debug_info section, not the current CU. */
2858 if (attr_ptr
->form
== DW_FORM_ref_addr
)
2860 /* We only support DW_FORM_ref_addr within the same file, so
2861 any relocations should be resolved already. */
2865 (_("Dwarf Error: Abstract instance DIE ref zero."));
2866 bfd_set_error (bfd_error_bad_value
);
2870 info_ptr
= unit
->sec_info_ptr
+ die_ref
;
2871 info_ptr_end
= unit
->end_ptr
;
2873 /* Now find the CU containing this pointer. */
2874 if (info_ptr
>= unit
->info_ptr_unit
&& info_ptr
< unit
->end_ptr
)
2878 /* Check other CUs to see if they contain the abbrev. */
2879 struct comp_unit
* u
;
2881 for (u
= unit
->prev_unit
; u
!= NULL
; u
= u
->prev_unit
)
2882 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
2886 for (u
= unit
->next_unit
; u
!= NULL
; u
= u
->next_unit
)
2887 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
2892 /* else FIXME: What do we do now ? */
2895 else if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
2897 info_ptr
= read_alt_indirect_ref (unit
, die_ref
);
2898 if (info_ptr
== NULL
)
2901 (_("Dwarf Error: Unable to read alt ref %llu."),
2902 (long long) die_ref
);
2903 bfd_set_error (bfd_error_bad_value
);
2906 info_ptr_end
= (unit
->stash
->alt_dwarf_info_buffer
2907 + unit
->stash
->alt_dwarf_info_size
);
2909 /* FIXME: Do we need to locate the correct CU, in a similar
2910 fashion to the code in the DW_FORM_ref_addr case above ? */
2914 info_ptr
= unit
->info_ptr_unit
+ die_ref
;
2915 info_ptr_end
= unit
->end_ptr
;
2918 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
2919 FALSE
, info_ptr_end
);
2920 info_ptr
+= bytes_read
;
2924 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
2928 (_("Dwarf Error: Could not find abbrev number %u."), abbrev_number
);
2929 bfd_set_error (bfd_error_bad_value
);
2934 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2936 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
,
2937 info_ptr
, info_ptr_end
);
2938 if (info_ptr
== NULL
)
2940 /* It doesn't ever make sense for DW_AT_specification to
2941 refer to the same DIE. Stop simple recursion. */
2942 if (info_ptr
== orig_info_ptr
)
2945 (_("Dwarf Error: Abstract instance recursion detected."));
2946 bfd_set_error (bfd_error_bad_value
);
2952 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2954 if (name
== NULL
&& is_str_attr (attr
.form
))
2957 if (non_mangled (unit
->lang
))
2961 case DW_AT_specification
:
2962 if (!find_abstract_instance_name (unit
, info_ptr
, &attr
,
2966 case DW_AT_linkage_name
:
2967 case DW_AT_MIPS_linkage_name
:
2968 /* PR 16949: Corrupt debug info can place
2969 non-string forms into these attributes. */
2970 if (is_str_attr (attr
.form
))
2987 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
,
2988 bfd_uint64_t offset
)
2990 bfd_byte
*ranges_ptr
;
2991 bfd_byte
*ranges_end
;
2992 bfd_vma base_address
= unit
->base_address
;
2994 if (! unit
->stash
->dwarf_ranges_buffer
)
2996 if (! read_debug_ranges (unit
))
3000 ranges_ptr
= unit
->stash
->dwarf_ranges_buffer
+ offset
;
3001 if (ranges_ptr
< unit
->stash
->dwarf_ranges_buffer
)
3003 ranges_end
= unit
->stash
->dwarf_ranges_buffer
+ unit
->stash
->dwarf_ranges_size
;
3010 /* PR 17512: file: 62cada7d. */
3011 if (ranges_ptr
+ 2 * unit
->addr_size
> ranges_end
)
3014 low_pc
= read_address (unit
, ranges_ptr
, ranges_end
);
3015 ranges_ptr
+= unit
->addr_size
;
3016 high_pc
= read_address (unit
, ranges_ptr
, ranges_end
);
3017 ranges_ptr
+= unit
->addr_size
;
3019 if (low_pc
== 0 && high_pc
== 0)
3021 if (low_pc
== -1UL && high_pc
!= -1UL)
3022 base_address
= high_pc
;
3025 if (!arange_add (unit
, arange
,
3026 base_address
+ low_pc
, base_address
+ high_pc
))
3033 /* DWARF2 Compilation unit functions. */
3035 /* Scan over each die in a comp. unit looking for functions to add
3036 to the function table and variables to the variable table. */
3039 scan_unit_for_symbols (struct comp_unit
*unit
)
3041 bfd
*abfd
= unit
->abfd
;
3042 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
3043 bfd_byte
*info_ptr_end
= unit
->stash
->info_ptr_end
;
3044 int nesting_level
= 0;
3045 struct nest_funcinfo
{
3046 struct funcinfo
*func
;
3048 int nested_funcs_size
;
3050 /* Maintain a stack of in-scope functions and inlined functions, which we
3051 can use to set the caller_func field. */
3052 nested_funcs_size
= 32;
3053 nested_funcs
= (struct nest_funcinfo
*)
3054 bfd_malloc (nested_funcs_size
* sizeof (*nested_funcs
));
3055 if (nested_funcs
== NULL
)
3057 nested_funcs
[nesting_level
].func
= 0;
3059 while (nesting_level
>= 0)
3061 unsigned int abbrev_number
, bytes_read
, i
;
3062 struct abbrev_info
*abbrev
;
3063 struct attribute attr
;
3064 struct funcinfo
*func
;
3065 struct varinfo
*var
;
3067 bfd_vma high_pc
= 0;
3068 bfd_boolean high_pc_relative
= FALSE
;
3070 /* PR 17512: file: 9f405d9d. */
3071 if (info_ptr
>= info_ptr_end
)
3074 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
3075 FALSE
, info_ptr_end
);
3076 info_ptr
+= bytes_read
;
3078 if (! abbrev_number
)
3084 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
3087 static unsigned int previous_failed_abbrev
= -1U;
3089 /* Avoid multiple reports of the same missing abbrev. */
3090 if (abbrev_number
!= previous_failed_abbrev
)
3093 (_("Dwarf Error: Could not find abbrev number %u."),
3095 previous_failed_abbrev
= abbrev_number
;
3097 bfd_set_error (bfd_error_bad_value
);
3102 if (abbrev
->tag
== DW_TAG_subprogram
3103 || abbrev
->tag
== DW_TAG_entry_point
3104 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
3106 bfd_size_type amt
= sizeof (struct funcinfo
);
3107 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
3110 func
->tag
= abbrev
->tag
;
3111 func
->prev_func
= unit
->function_table
;
3112 unit
->function_table
= func
;
3113 unit
->number_of_functions
++;
3114 BFD_ASSERT (!unit
->cached
);
3116 if (func
->tag
== DW_TAG_inlined_subroutine
)
3117 for (i
= nesting_level
; i
-- != 0; )
3118 if (nested_funcs
[i
].func
)
3120 func
->caller_func
= nested_funcs
[i
].func
;
3123 nested_funcs
[nesting_level
].func
= func
;
3128 if (abbrev
->tag
== DW_TAG_variable
)
3130 bfd_size_type amt
= sizeof (struct varinfo
);
3131 var
= (struct varinfo
*) bfd_zalloc (abfd
, amt
);
3134 var
->tag
= abbrev
->tag
;
3136 var
->prev_var
= unit
->variable_table
;
3137 unit
->variable_table
= var
;
3138 /* PR 18205: Missing debug information can cause this
3139 var to be attached to an already cached unit. */
3142 /* No inline function in scope at this nesting level. */
3143 nested_funcs
[nesting_level
].func
= 0;
3146 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3148 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
],
3149 unit
, info_ptr
, info_ptr_end
);
3150 if (info_ptr
== NULL
)
3157 case DW_AT_call_file
:
3158 func
->caller_file
= concat_filename (unit
->line_table
,
3162 case DW_AT_call_line
:
3163 func
->caller_line
= attr
.u
.val
;
3166 case DW_AT_abstract_origin
:
3167 case DW_AT_specification
:
3168 if (!find_abstract_instance_name (unit
, info_ptr
, &attr
,
3175 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3177 if (func
->name
== NULL
&& is_str_attr (attr
.form
))
3179 func
->name
= attr
.u
.str
;
3180 if (non_mangled (unit
->lang
))
3181 func
->is_linkage
= TRUE
;
3185 case DW_AT_linkage_name
:
3186 case DW_AT_MIPS_linkage_name
:
3187 /* PR 16949: Corrupt debug info can place
3188 non-string forms into these attributes. */
3189 if (is_str_attr (attr
.form
))
3191 func
->name
= attr
.u
.str
;
3192 func
->is_linkage
= TRUE
;
3197 low_pc
= attr
.u
.val
;
3201 high_pc
= attr
.u
.val
;
3202 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
3206 if (!read_rangelist (unit
, &func
->arange
, attr
.u
.val
))
3210 case DW_AT_decl_file
:
3211 func
->file
= concat_filename (unit
->line_table
,
3215 case DW_AT_decl_line
:
3216 func
->line
= attr
.u
.val
;
3228 var
->name
= attr
.u
.str
;
3231 case DW_AT_decl_file
:
3232 var
->file
= concat_filename (unit
->line_table
,
3236 case DW_AT_decl_line
:
3237 var
->line
= attr
.u
.val
;
3240 case DW_AT_external
:
3241 if (attr
.u
.val
!= 0)
3245 case DW_AT_location
:
3249 case DW_FORM_block1
:
3250 case DW_FORM_block2
:
3251 case DW_FORM_block4
:
3252 case DW_FORM_exprloc
:
3253 if (attr
.u
.blk
->data
!= NULL
3254 && *attr
.u
.blk
->data
== DW_OP_addr
)
3258 /* Verify that DW_OP_addr is the only opcode in the
3259 location, in which case the block size will be 1
3260 plus the address size. */
3261 /* ??? For TLS variables, gcc can emit
3262 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
3263 which we don't handle here yet. */
3264 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
3265 var
->addr
= bfd_get (unit
->addr_size
* 8,
3267 attr
.u
.blk
->data
+ 1);
3282 if (high_pc_relative
)
3285 if (func
&& high_pc
!= 0)
3287 if (!arange_add (unit
, &func
->arange
, low_pc
, high_pc
))
3291 if (abbrev
->has_children
)
3295 if (nesting_level
>= nested_funcs_size
)
3297 struct nest_funcinfo
*tmp
;
3299 nested_funcs_size
*= 2;
3300 tmp
= (struct nest_funcinfo
*)
3301 bfd_realloc (nested_funcs
,
3302 nested_funcs_size
* sizeof (*nested_funcs
));
3307 nested_funcs
[nesting_level
].func
= 0;
3311 free (nested_funcs
);
3315 free (nested_funcs
);
3319 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
3320 includes the compilation unit header that proceeds the DIE's, but
3321 does not include the length field that precedes each compilation
3322 unit header. END_PTR points one past the end of this comp unit.
3323 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
3325 This routine does not read the whole compilation unit; only enough
3326 to get to the line number information for the compilation unit. */
3328 static struct comp_unit
*
3329 parse_comp_unit (struct dwarf2_debug
*stash
,
3330 bfd_vma unit_length
,
3331 bfd_byte
*info_ptr_unit
,
3332 unsigned int offset_size
)
3334 struct comp_unit
* unit
;
3335 unsigned int version
;
3336 bfd_uint64_t abbrev_offset
= 0;
3337 /* Initialize it just to avoid a GCC false warning. */
3338 unsigned int addr_size
= -1;
3339 struct abbrev_info
** abbrevs
;
3340 unsigned int abbrev_number
, bytes_read
, i
;
3341 struct abbrev_info
*abbrev
;
3342 struct attribute attr
;
3343 bfd_byte
*info_ptr
= stash
->info_ptr
;
3344 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
3347 bfd_vma high_pc
= 0;
3348 bfd
*abfd
= stash
->bfd_ptr
;
3349 bfd_boolean high_pc_relative
= FALSE
;
3350 enum dwarf_unit_type unit_type
;
3352 version
= read_2_bytes (abfd
, info_ptr
, end_ptr
);
3354 if (version
< 2 || version
> 5)
3356 /* PR 19872: A version number of 0 probably means that there is padding
3357 at the end of the .debug_info section. Gold puts it there when
3358 performing an incremental link, for example. So do not generate
3359 an error, just return a NULL. */
3363 (_("Dwarf Error: found dwarf version '%u', this reader"
3364 " only handles version 2, 3, 4 and 5 information."), version
);
3365 bfd_set_error (bfd_error_bad_value
);
3371 unit_type
= DW_UT_compile
;
3374 unit_type
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3377 addr_size
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3381 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
3382 if (offset_size
== 4)
3383 abbrev_offset
= read_4_bytes (abfd
, info_ptr
, end_ptr
);
3385 abbrev_offset
= read_8_bytes (abfd
, info_ptr
, end_ptr
);
3386 info_ptr
+= offset_size
;
3390 addr_size
= read_1_byte (abfd
, info_ptr
, end_ptr
);
3394 if (unit_type
== DW_UT_type
)
3396 /* Skip type signature. */
3399 /* Skip type offset. */
3400 info_ptr
+= offset_size
;
3403 if (addr_size
> sizeof (bfd_vma
))
3406 /* xgettext: c-format */
3407 (_("Dwarf Error: found address size '%u', this reader"
3408 " can not handle sizes greater than '%u'."),
3410 (unsigned int) sizeof (bfd_vma
));
3411 bfd_set_error (bfd_error_bad_value
);
3415 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
3418 ("Dwarf Error: found address size '%u', this reader"
3419 " can only handle address sizes '2', '4' and '8'.", addr_size
);
3420 bfd_set_error (bfd_error_bad_value
);
3424 /* Read the abbrevs for this compilation unit into a table. */
3425 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
);
3429 abbrev_number
= _bfd_safe_read_leb128 (abfd
, info_ptr
, &bytes_read
,
3431 info_ptr
+= bytes_read
;
3432 if (! abbrev_number
)
3434 /* PR 19872: An abbrev number of 0 probably means that there is padding
3435 at the end of the .debug_abbrev section. Gold puts it there when
3436 performing an incremental link, for example. So do not generate
3437 an error, just return a NULL. */
3441 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
3444 _bfd_error_handler (_("Dwarf Error: Could not find abbrev number %u."),
3446 bfd_set_error (bfd_error_bad_value
);
3450 amt
= sizeof (struct comp_unit
);
3451 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
3455 unit
->version
= version
;
3456 unit
->addr_size
= addr_size
;
3457 unit
->offset_size
= offset_size
;
3458 unit
->abbrevs
= abbrevs
;
3459 unit
->end_ptr
= end_ptr
;
3460 unit
->stash
= stash
;
3461 unit
->info_ptr_unit
= info_ptr_unit
;
3462 unit
->sec_info_ptr
= stash
->sec_info_ptr
;
3464 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3466 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
, end_ptr
);
3467 if (info_ptr
== NULL
)
3470 /* Store the data if it is of an attribute we want to keep in a
3471 partial symbol table. */
3474 case DW_AT_stmt_list
:
3476 unit
->line_offset
= attr
.u
.val
;
3480 unit
->name
= attr
.u
.str
;
3484 low_pc
= attr
.u
.val
;
3485 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
3486 this is the base address to use when reading location
3487 lists or range lists. */
3488 if (abbrev
->tag
== DW_TAG_compile_unit
)
3489 unit
->base_address
= low_pc
;
3493 high_pc
= attr
.u
.val
;
3494 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
3498 if (!read_rangelist (unit
, &unit
->arange
, attr
.u
.val
))
3502 case DW_AT_comp_dir
:
3504 char *comp_dir
= attr
.u
.str
;
3506 /* PR 17512: file: 1fe726be. */
3507 if (! is_str_attr (attr
.form
))
3510 (_("Dwarf Error: DW_AT_comp_dir attribute encountered with a non-string form."));
3516 /* Irix 6.2 native cc prepends <machine>.: to the compilation
3517 directory, get rid of it. */
3518 char *cp
= strchr (comp_dir
, ':');
3520 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
3523 unit
->comp_dir
= comp_dir
;
3527 case DW_AT_language
:
3528 unit
->lang
= attr
.u
.val
;
3535 if (high_pc_relative
)
3539 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
3543 unit
->first_child_die_ptr
= info_ptr
;
3547 /* Return TRUE if UNIT may contain the address given by ADDR. When
3548 there are functions written entirely with inline asm statements, the
3549 range info in the compilation unit header may not be correct. We
3550 need to consult the line info table to see if a compilation unit
3551 really contains the given address. */
3554 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
3556 struct arange
*arange
;
3561 arange
= &unit
->arange
;
3564 if (addr
>= arange
->low
&& addr
< arange
->high
)
3566 arange
= arange
->next
;
3573 /* If UNIT contains ADDR, set the output parameters to the values for
3574 the line containing ADDR. The output parameters, FILENAME_PTR,
3575 FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects
3578 Returns the range of addresses covered by the entry that was used
3579 to fill in *LINENUMBER_PTR or 0 if it was not filled in. */
3582 comp_unit_find_nearest_line (struct comp_unit
*unit
,
3584 const char **filename_ptr
,
3585 struct funcinfo
**function_ptr
,
3586 unsigned int *linenumber_ptr
,
3587 unsigned int *discriminator_ptr
,
3588 struct dwarf2_debug
*stash
)
3595 if (! unit
->line_table
)
3597 if (! unit
->stmtlist
)
3603 unit
->line_table
= decode_line_info (unit
, stash
);
3605 if (! unit
->line_table
)
3611 if (unit
->first_child_die_ptr
< unit
->end_ptr
3612 && ! scan_unit_for_symbols (unit
))
3619 *function_ptr
= NULL
;
3620 func_p
= lookup_address_in_function_table (unit
, addr
, function_ptr
);
3621 if (func_p
&& (*function_ptr
)->tag
== DW_TAG_inlined_subroutine
)
3622 stash
->inliner_chain
= *function_ptr
;
3624 return lookup_address_in_line_info_table (unit
->line_table
, addr
,
3630 /* Check to see if line info is already decoded in a comp_unit.
3631 If not, decode it. Returns TRUE if no errors were encountered;
3635 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
,
3636 struct dwarf2_debug
*stash
)
3641 if (! unit
->line_table
)
3643 if (! unit
->stmtlist
)
3649 unit
->line_table
= decode_line_info (unit
, stash
);
3651 if (! unit
->line_table
)
3657 if (unit
->first_child_die_ptr
< unit
->end_ptr
3658 && ! scan_unit_for_symbols (unit
))
3668 /* If UNIT contains SYM at ADDR, set the output parameters to the
3669 values for the line containing SYM. The output parameters,
3670 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
3673 Return TRUE if UNIT contains SYM, and no errors were encountered;
3677 comp_unit_find_line (struct comp_unit
*unit
,
3680 const char **filename_ptr
,
3681 unsigned int *linenumber_ptr
,
3682 struct dwarf2_debug
*stash
)
3684 if (!comp_unit_maybe_decode_line_info (unit
, stash
))
3687 if (sym
->flags
& BSF_FUNCTION
)
3688 return lookup_symbol_in_function_table (unit
, sym
, addr
,
3692 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
3697 static struct funcinfo
*
3698 reverse_funcinfo_list (struct funcinfo
*head
)
3700 struct funcinfo
*rhead
;
3701 struct funcinfo
*temp
;
3703 for (rhead
= NULL
; head
; head
= temp
)
3705 temp
= head
->prev_func
;
3706 head
->prev_func
= rhead
;
3712 static struct varinfo
*
3713 reverse_varinfo_list (struct varinfo
*head
)
3715 struct varinfo
*rhead
;
3716 struct varinfo
*temp
;
3718 for (rhead
= NULL
; head
; head
= temp
)
3720 temp
= head
->prev_var
;
3721 head
->prev_var
= rhead
;
3727 /* Extract all interesting funcinfos and varinfos of a compilation
3728 unit into hash tables for faster lookup. Returns TRUE if no
3729 errors were enountered; FALSE otherwise. */
3732 comp_unit_hash_info (struct dwarf2_debug
*stash
,
3733 struct comp_unit
*unit
,
3734 struct info_hash_table
*funcinfo_hash_table
,
3735 struct info_hash_table
*varinfo_hash_table
)
3737 struct funcinfo
* each_func
;
3738 struct varinfo
* each_var
;
3739 bfd_boolean okay
= TRUE
;
3741 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
3743 if (!comp_unit_maybe_decode_line_info (unit
, stash
))
3746 BFD_ASSERT (!unit
->cached
);
3748 /* To preserve the original search order, we went to visit the function
3749 infos in the reversed order of the list. However, making the list
3750 bi-directional use quite a bit of extra memory. So we reverse
3751 the list first, traverse the list in the now reversed order and
3752 finally reverse the list again to get back the original order. */
3753 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
3754 for (each_func
= unit
->function_table
;
3756 each_func
= each_func
->prev_func
)
3758 /* Skip nameless functions. */
3759 if (each_func
->name
)
3760 /* There is no need to copy name string into hash table as
3761 name string is either in the dwarf string buffer or
3762 info in the stash. */
3763 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
3764 (void*) each_func
, FALSE
);
3766 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
3770 /* We do the same for variable infos. */
3771 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
3772 for (each_var
= unit
->variable_table
;
3774 each_var
= each_var
->prev_var
)
3776 /* Skip stack vars and vars with no files or names. */
3777 if (each_var
->stack
== 0
3778 && each_var
->file
!= NULL
3779 && each_var
->name
!= NULL
)
3780 /* There is no need to copy name string into hash table as
3781 name string is either in the dwarf string buffer or
3782 info in the stash. */
3783 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
3784 (void*) each_var
, FALSE
);
3787 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
3788 unit
->cached
= TRUE
;
3792 /* Locate a section in a BFD containing debugging info. The search starts
3793 from the section after AFTER_SEC, or from the first section in the BFD if
3794 AFTER_SEC is NULL. The search works by examining the names of the
3795 sections. There are three permissiable names. The first two are given
3796 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
3797 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
3798 This is a variation on the .debug_info section which has a checksum
3799 describing the contents appended onto the name. This allows the linker to
3800 identify and discard duplicate debugging sections for different
3801 compilation units. */
3802 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
3805 find_debug_info (bfd
*abfd
, const struct dwarf_debug_section
*debug_sections
,
3806 asection
*after_sec
)
3811 if (after_sec
== NULL
)
3813 look
= debug_sections
[debug_info
].uncompressed_name
;
3814 msec
= bfd_get_section_by_name (abfd
, look
);
3818 look
= debug_sections
[debug_info
].compressed_name
;
3821 msec
= bfd_get_section_by_name (abfd
, look
);
3826 for (msec
= abfd
->sections
; msec
!= NULL
; msec
= msec
->next
)
3827 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
3833 for (msec
= after_sec
->next
; msec
!= NULL
; msec
= msec
->next
)
3835 look
= debug_sections
[debug_info
].uncompressed_name
;
3836 if (strcmp (msec
->name
, look
) == 0)
3839 look
= debug_sections
[debug_info
].compressed_name
;
3840 if (look
!= NULL
&& strcmp (msec
->name
, look
) == 0)
3843 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
3850 /* Transfer VMAs from object file to separate debug file. */
3853 set_debug_vma (bfd
*orig_bfd
, bfd
*debug_bfd
)
3857 for (s
= orig_bfd
->sections
, d
= debug_bfd
->sections
;
3858 s
!= NULL
&& d
!= NULL
;
3859 s
= s
->next
, d
= d
->next
)
3861 if ((d
->flags
& SEC_DEBUGGING
) != 0)
3863 /* ??? Assumes 1-1 correspondence between sections in the
3865 if (strcmp (s
->name
, d
->name
) == 0)
3867 d
->output_section
= s
->output_section
;
3868 d
->output_offset
= s
->output_offset
;
3874 /* Unset vmas for adjusted sections in STASH. */
3877 unset_sections (struct dwarf2_debug
*stash
)
3880 struct adjusted_section
*p
;
3882 i
= stash
->adjusted_section_count
;
3883 p
= stash
->adjusted_sections
;
3884 for (; i
> 0; i
--, p
++)
3885 p
->section
->vma
= 0;
3888 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
3889 relocatable object file. VMAs are normally all zero in relocatable
3890 object files, so if we want to distinguish locations in sections by
3891 address we need to set VMAs so the sections do not overlap. We
3892 also set VMA on .debug_info so that when we have multiple
3893 .debug_info sections (or the linkonce variant) they also do not
3894 overlap. The multiple .debug_info sections make up a single
3895 logical section. ??? We should probably do the same for other
3899 place_sections (bfd
*orig_bfd
, struct dwarf2_debug
*stash
)
3902 struct adjusted_section
*p
;
3904 const char *debug_info_name
;
3906 if (stash
->adjusted_section_count
!= 0)
3908 i
= stash
->adjusted_section_count
;
3909 p
= stash
->adjusted_sections
;
3910 for (; i
> 0; i
--, p
++)
3911 p
->section
->vma
= p
->adj_vma
;
3915 debug_info_name
= stash
->debug_sections
[debug_info
].uncompressed_name
;
3922 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
3926 if ((sect
->output_section
!= NULL
3927 && sect
->output_section
!= sect
3928 && (sect
->flags
& SEC_DEBUGGING
) == 0)
3932 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
3933 || CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
));
3935 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
3941 if (abfd
== stash
->bfd_ptr
)
3943 abfd
= stash
->bfd_ptr
;
3947 stash
->adjusted_section_count
= -1;
3950 bfd_vma last_vma
= 0, last_dwarf
= 0;
3951 bfd_size_type amt
= i
* sizeof (struct adjusted_section
);
3953 p
= (struct adjusted_section
*) bfd_malloc (amt
);
3957 stash
->adjusted_sections
= p
;
3958 stash
->adjusted_section_count
= i
;
3965 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
3970 if ((sect
->output_section
!= NULL
3971 && sect
->output_section
!= sect
3972 && (sect
->flags
& SEC_DEBUGGING
) == 0)
3976 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
3977 || CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
));
3979 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
3983 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
3987 BFD_ASSERT (sect
->alignment_power
== 0);
3988 sect
->vma
= last_dwarf
;
3993 /* Align the new address to the current section
3995 last_vma
= ((last_vma
3996 + ~(-((bfd_vma
) 1 << sect
->alignment_power
)))
3997 & (-((bfd_vma
) 1 << sect
->alignment_power
)));
3998 sect
->vma
= last_vma
;
4003 p
->adj_vma
= sect
->vma
;
4006 if (abfd
== stash
->bfd_ptr
)
4008 abfd
= stash
->bfd_ptr
;
4012 if (orig_bfd
!= stash
->bfd_ptr
)
4013 set_debug_vma (orig_bfd
, stash
->bfd_ptr
);
4018 /* Look up a funcinfo by name using the given info hash table. If found,
4019 also update the locations pointed to by filename_ptr and linenumber_ptr.
4021 This function returns TRUE if a funcinfo that matches the given symbol
4022 and address is found with any error; otherwise it returns FALSE. */
4025 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
4028 const char **filename_ptr
,
4029 unsigned int *linenumber_ptr
)
4031 struct funcinfo
* each_func
;
4032 struct funcinfo
* best_fit
= NULL
;
4033 bfd_vma best_fit_len
= 0;
4034 struct info_list_node
*node
;
4035 struct arange
*arange
;
4036 const char *name
= bfd_asymbol_name (sym
);
4037 asection
*sec
= bfd_get_section (sym
);
4039 for (node
= lookup_info_hash_table (hash_table
, name
);
4043 each_func
= (struct funcinfo
*) node
->info
;
4044 for (arange
= &each_func
->arange
;
4046 arange
= arange
->next
)
4048 if ((!each_func
->sec
|| each_func
->sec
== sec
)
4049 && addr
>= arange
->low
4050 && addr
< arange
->high
4052 || arange
->high
- arange
->low
< best_fit_len
))
4054 best_fit
= each_func
;
4055 best_fit_len
= arange
->high
- arange
->low
;
4062 best_fit
->sec
= sec
;
4063 *filename_ptr
= best_fit
->file
;
4064 *linenumber_ptr
= best_fit
->line
;
4071 /* Look up a varinfo by name using the given info hash table. If found,
4072 also update the locations pointed to by filename_ptr and linenumber_ptr.
4074 This function returns TRUE if a varinfo that matches the given symbol
4075 and address is found with any error; otherwise it returns FALSE. */
4078 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
4081 const char **filename_ptr
,
4082 unsigned int *linenumber_ptr
)
4084 const char *name
= bfd_asymbol_name (sym
);
4085 asection
*sec
= bfd_get_section (sym
);
4086 struct varinfo
* each
;
4087 struct info_list_node
*node
;
4089 for (node
= lookup_info_hash_table (hash_table
, name
);
4093 each
= (struct varinfo
*) node
->info
;
4094 if (each
->addr
== addr
4095 && (!each
->sec
|| each
->sec
== sec
))
4098 *filename_ptr
= each
->file
;
4099 *linenumber_ptr
= each
->line
;
4107 /* Update the funcinfo and varinfo info hash tables if they are
4108 not up to date. Returns TRUE if there is no error; otherwise
4109 returns FALSE and disable the info hash tables. */
4112 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
4114 struct comp_unit
*each
;
4116 /* Exit if hash tables are up-to-date. */
4117 if (stash
->all_comp_units
== stash
->hash_units_head
)
4120 if (stash
->hash_units_head
)
4121 each
= stash
->hash_units_head
->prev_unit
;
4123 each
= stash
->last_comp_unit
;
4127 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
4128 stash
->varinfo_hash_table
))
4130 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
4133 each
= each
->prev_unit
;
4136 stash
->hash_units_head
= stash
->all_comp_units
;
4140 /* Check consistency of info hash tables. This is for debugging only. */
4142 static void ATTRIBUTE_UNUSED
4143 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
4145 struct comp_unit
*each_unit
;
4146 struct funcinfo
*each_func
;
4147 struct varinfo
*each_var
;
4148 struct info_list_node
*node
;
4151 for (each_unit
= stash
->all_comp_units
;
4153 each_unit
= each_unit
->next_unit
)
4155 for (each_func
= each_unit
->function_table
;
4157 each_func
= each_func
->prev_func
)
4159 if (!each_func
->name
)
4161 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
4165 while (node
&& !found
)
4167 found
= node
->info
== each_func
;
4173 for (each_var
= each_unit
->variable_table
;
4175 each_var
= each_var
->prev_var
)
4177 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
4179 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
4183 while (node
&& !found
)
4185 found
= node
->info
== each_var
;
4193 /* Check to see if we want to enable the info hash tables, which consume
4194 quite a bit of memory. Currently we only check the number times
4195 bfd_dwarf2_find_line is called. In the future, we may also want to
4196 take the number of symbols into account. */
4199 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
4201 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
4203 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
4206 /* FIXME: Maybe we should check the reduce_memory_overheads
4207 and optimize fields in the bfd_link_info structure ? */
4209 /* Create hash tables. */
4210 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
4211 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
4212 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
4214 /* Turn off info hashes if any allocation above fails. */
4215 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
4218 /* We need a forced update so that the info hash tables will
4219 be created even though there is no compilation unit. That
4220 happens if STASH_INFO_HASH_TRIGGER is 0. */
4221 stash_maybe_update_info_hash_tables (stash
);
4222 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
4225 /* Find the file and line associated with a symbol and address using the
4226 info hash tables of a stash. If there is a match, the function returns
4227 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
4228 otherwise it returns FALSE. */
4231 stash_find_line_fast (struct dwarf2_debug
*stash
,
4234 const char **filename_ptr
,
4235 unsigned int *linenumber_ptr
)
4237 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
4239 if (sym
->flags
& BSF_FUNCTION
)
4240 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
4241 filename_ptr
, linenumber_ptr
);
4242 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
4243 filename_ptr
, linenumber_ptr
);
4246 /* Save current section VMAs. */
4249 save_section_vma (const bfd
*abfd
, struct dwarf2_debug
*stash
)
4254 if (abfd
->section_count
== 0)
4256 stash
->sec_vma
= bfd_malloc (sizeof (*stash
->sec_vma
) * abfd
->section_count
);
4257 if (stash
->sec_vma
== NULL
)
4259 for (i
= 0, s
= abfd
->sections
; i
< abfd
->section_count
; i
++, s
= s
->next
)
4261 if (s
->output_section
!= NULL
)
4262 stash
->sec_vma
[i
] = s
->output_section
->vma
+ s
->output_offset
;
4264 stash
->sec_vma
[i
] = s
->vma
;
4269 /* Compare current section VMAs against those at the time the stash
4270 was created. If find_nearest_line is used in linker warnings or
4271 errors early in the link process, the debug info stash will be
4272 invalid for later calls. This is because we relocate debug info
4273 sections, so the stashed section contents depend on symbol values,
4274 which in turn depend on section VMAs. */
4277 section_vma_same (const bfd
*abfd
, const struct dwarf2_debug
*stash
)
4282 for (i
= 0, s
= abfd
->sections
; i
< abfd
->section_count
; i
++, s
= s
->next
)
4286 if (s
->output_section
!= NULL
)
4287 vma
= s
->output_section
->vma
+ s
->output_offset
;
4290 if (vma
!= stash
->sec_vma
[i
])
4296 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
4297 If DEBUG_BFD is not specified, we read debug information from ABFD
4298 or its gnu_debuglink. The results will be stored in PINFO.
4299 The function returns TRUE iff debug information is ready. */
4302 _bfd_dwarf2_slurp_debug_info (bfd
*abfd
, bfd
*debug_bfd
,
4303 const struct dwarf_debug_section
*debug_sections
,
4306 bfd_boolean do_place
)
4308 bfd_size_type amt
= sizeof (struct dwarf2_debug
);
4309 bfd_size_type total_size
;
4311 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
4315 if (stash
->orig_bfd
== abfd
4316 && section_vma_same (abfd
, stash
))
4318 /* Check that we did previously find some debug information
4319 before attempting to make use of it. */
4320 if (stash
->bfd_ptr
!= NULL
)
4322 if (do_place
&& !place_sections (abfd
, stash
))
4329 _bfd_dwarf2_cleanup_debug_info (abfd
, pinfo
);
4330 memset (stash
, 0, amt
);
4334 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
4338 stash
->orig_bfd
= abfd
;
4339 stash
->debug_sections
= debug_sections
;
4340 stash
->syms
= symbols
;
4341 if (!save_section_vma (abfd
, stash
))
4346 if (debug_bfd
== NULL
)
4349 msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4350 if (msec
== NULL
&& abfd
== debug_bfd
)
4352 char * debug_filename
;
4354 debug_filename
= bfd_follow_build_id_debuglink (abfd
, DEBUGDIR
);
4355 if (debug_filename
== NULL
)
4356 debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
4358 if (debug_filename
== NULL
)
4359 /* No dwarf2 info, and no gnu_debuglink to follow.
4360 Note that at this point the stash has been allocated, but
4361 contains zeros. This lets future calls to this function
4362 fail more quickly. */
4365 /* Set BFD_DECOMPRESS to decompress debug sections. */
4366 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
4367 || !(debug_bfd
->flags
|= BFD_DECOMPRESS
,
4368 bfd_check_format (debug_bfd
, bfd_object
))
4369 || (msec
= find_debug_info (debug_bfd
,
4370 debug_sections
, NULL
)) == NULL
4371 || !bfd_generic_link_read_symbols (debug_bfd
))
4374 bfd_close (debug_bfd
);
4375 /* FIXME: Should we report our failure to follow the debuglink ? */
4376 free (debug_filename
);
4380 symbols
= bfd_get_outsymbols (debug_bfd
);
4381 stash
->syms
= symbols
;
4382 stash
->close_on_cleanup
= TRUE
;
4384 stash
->bfd_ptr
= debug_bfd
;
4387 && !place_sections (abfd
, stash
))
4390 /* There can be more than one DWARF2 info section in a BFD these
4391 days. First handle the easy case when there's only one. If
4392 there's more than one, try case two: none of the sections is
4393 compressed. In that case, read them all in and produce one
4394 large stash. We do this in two passes - in the first pass we
4395 just accumulate the section sizes, and in the second pass we
4396 read in the section's contents. (The allows us to avoid
4397 reallocing the data as we add sections to the stash.) If
4398 some or all sections are compressed, then do things the slow
4399 way, with a bunch of reallocs. */
4401 if (! find_debug_info (debug_bfd
, debug_sections
, msec
))
4403 /* Case 1: only one info section. */
4404 total_size
= msec
->size
;
4405 if (! read_section (debug_bfd
, &stash
->debug_sections
[debug_info
],
4407 &stash
->info_ptr_memory
, &total_size
))
4412 /* Case 2: multiple sections. */
4413 for (total_size
= 0;
4415 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
4416 total_size
+= msec
->size
;
4418 stash
->info_ptr_memory
= (bfd_byte
*) bfd_malloc (total_size
);
4419 if (stash
->info_ptr_memory
== NULL
)
4423 for (msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4425 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
4433 if (!(bfd_simple_get_relocated_section_contents
4434 (debug_bfd
, msec
, stash
->info_ptr_memory
+ total_size
,
4442 stash
->info_ptr
= stash
->info_ptr_memory
;
4443 stash
->info_ptr_end
= stash
->info_ptr
+ total_size
;
4444 stash
->sec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
4445 stash
->sec_info_ptr
= stash
->info_ptr
;
4449 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
4450 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
4451 symbol in SYMBOLS and return the difference between the low_pc and
4452 the symbol's address. Returns 0 if no suitable symbol could be found. */
4455 _bfd_dwarf2_find_symbol_bias (asymbol
** symbols
, void ** pinfo
)
4457 struct dwarf2_debug
*stash
;
4458 struct comp_unit
* unit
;
4460 stash
= (struct dwarf2_debug
*) *pinfo
;
4465 for (unit
= stash
->all_comp_units
; unit
; unit
= unit
->next_unit
)
4467 struct funcinfo
* func
;
4469 if (unit
->function_table
== NULL
)
4471 if (unit
->line_table
== NULL
)
4472 unit
->line_table
= decode_line_info (unit
, stash
);
4473 if (unit
->line_table
!= NULL
)
4474 scan_unit_for_symbols (unit
);
4477 for (func
= unit
->function_table
; func
!= NULL
; func
= func
->prev_func
)
4478 if (func
->name
&& func
->arange
.low
)
4482 /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
4484 for (psym
= symbols
; * psym
!= NULL
; psym
++)
4486 asymbol
* sym
= * psym
;
4488 if (sym
->flags
& BSF_FUNCTION
4489 && sym
->section
!= NULL
4490 && strcmp (sym
->name
, func
->name
) == 0)
4491 return ((bfd_signed_vma
) func
->arange
.low
) -
4492 ((bfd_signed_vma
) (sym
->value
+ sym
->section
->vma
));
4500 /* Find the source code location of SYMBOL. If SYMBOL is NULL
4501 then find the nearest source code location corresponding to
4502 the address SECTION + OFFSET.
4503 Returns TRUE if the line is found without error and fills in
4504 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
4505 NULL the FUNCTIONNAME_PTR is also filled in.
4506 SYMBOLS contains the symbol table for ABFD.
4507 DEBUG_SECTIONS contains the name of the dwarf debug sections.
4508 ADDR_SIZE is the number of bytes in the initial .debug_info length
4509 field and in the abbreviation offset, or zero to indicate that the
4510 default value should be used. */
4513 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
4518 const char **filename_ptr
,
4519 const char **functionname_ptr
,
4520 unsigned int *linenumber_ptr
,
4521 unsigned int *discriminator_ptr
,
4522 const struct dwarf_debug_section
*debug_sections
,
4523 unsigned int addr_size
,
4526 /* Read each compilation unit from the section .debug_info, and check
4527 to see if it contains the address we are searching for. If yes,
4528 lookup the address, and return the line number info. If no, go
4529 on to the next compilation unit.
4531 We keep a list of all the previously read compilation units, and
4532 a pointer to the next un-read compilation unit. Check the
4533 previously read units before reading more. */
4534 struct dwarf2_debug
*stash
;
4535 /* What address are we looking for? */
4537 struct comp_unit
* each
;
4538 struct funcinfo
*function
= NULL
;
4539 bfd_boolean found
= FALSE
;
4540 bfd_boolean do_line
;
4542 *filename_ptr
= NULL
;
4543 if (functionname_ptr
!= NULL
)
4544 *functionname_ptr
= NULL
;
4545 *linenumber_ptr
= 0;
4546 if (discriminator_ptr
)
4547 *discriminator_ptr
= 0;
4549 if (! _bfd_dwarf2_slurp_debug_info (abfd
, NULL
, debug_sections
,
4551 (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0))
4554 stash
= (struct dwarf2_debug
*) *pinfo
;
4556 do_line
= symbol
!= NULL
;
4559 BFD_ASSERT (section
== NULL
&& offset
== 0 && functionname_ptr
== NULL
);
4560 section
= bfd_get_section (symbol
);
4561 addr
= symbol
->value
;
4565 BFD_ASSERT (section
!= NULL
&& functionname_ptr
!= NULL
);
4568 /* If we have no SYMBOL but the section we're looking at is not a
4569 code section, then take a look through the list of symbols to see
4570 if we have a symbol at the address we're looking for. If we do
4571 then use this to look up line information. This will allow us to
4572 give file and line results for data symbols. We exclude code
4573 symbols here, if we look up a function symbol and then look up the
4574 line information we'll actually return the line number for the
4575 opening '{' rather than the function definition line. This is
4576 because looking up by symbol uses the line table, in which the
4577 first line for a function is usually the opening '{', while
4578 looking up the function by section + offset uses the
4579 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
4580 which will be the line of the function name. */
4581 if (symbols
!= NULL
&& (section
->flags
& SEC_CODE
) == 0)
4585 for (tmp
= symbols
; (*tmp
) != NULL
; ++tmp
)
4586 if ((*tmp
)->the_bfd
== abfd
4587 && (*tmp
)->section
== section
4588 && (*tmp
)->value
== offset
4589 && ((*tmp
)->flags
& BSF_SECTION_SYM
) == 0)
4593 /* For local symbols, keep going in the hope we find a
4595 if ((symbol
->flags
& BSF_GLOBAL
) != 0)
4601 if (section
->output_section
)
4602 addr
+= section
->output_section
->vma
+ section
->output_offset
;
4604 addr
+= section
->vma
;
4606 /* A null info_ptr indicates that there is no dwarf2 info
4607 (or that an error occured while setting up the stash). */
4608 if (! stash
->info_ptr
)
4611 stash
->inliner_chain
= NULL
;
4613 /* Check the previously read comp. units first. */
4616 /* The info hash tables use quite a bit of memory. We may not want to
4617 always use them. We use some heuristics to decide if and when to
4619 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
4620 stash_maybe_enable_info_hash_tables (abfd
, stash
);
4622 /* Keep info hash table up to date if they are available. Note that we
4623 may disable the hash tables if there is any error duing update. */
4624 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
4625 stash_maybe_update_info_hash_tables (stash
);
4627 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
4629 found
= stash_find_line_fast (stash
, symbol
, addr
, filename_ptr
,
4636 /* Check the previously read comp. units first. */
4637 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
4638 if ((symbol
->flags
& BSF_FUNCTION
) == 0
4639 || each
->arange
.high
== 0
4640 || comp_unit_contains_address (each
, addr
))
4642 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
4643 linenumber_ptr
, stash
);
4651 bfd_vma min_range
= (bfd_vma
) -1;
4652 const char * local_filename
= NULL
;
4653 struct funcinfo
*local_function
= NULL
;
4654 unsigned int local_linenumber
= 0;
4655 unsigned int local_discriminator
= 0;
4657 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
4659 bfd_vma range
= (bfd_vma
) -1;
4661 found
= ((each
->arange
.high
== 0
4662 || comp_unit_contains_address (each
, addr
))
4663 && (range
= comp_unit_find_nearest_line (each
, addr
,
4667 & local_discriminator
,
4671 /* PRs 15935 15994: Bogus debug information may have provided us
4672 with an erroneous match. We attempt to counter this by
4673 selecting the match that has the smallest address range
4674 associated with it. (We are assuming that corrupt debug info
4675 will tend to result in extra large address ranges rather than
4676 extra small ranges).
4678 This does mean that we scan through all of the CUs associated
4679 with the bfd each time this function is called. But this does
4680 have the benefit of producing consistent results every time the
4681 function is called. */
4682 if (range
<= min_range
)
4684 if (filename_ptr
&& local_filename
)
4685 * filename_ptr
= local_filename
;
4687 function
= local_function
;
4688 if (discriminator_ptr
&& local_discriminator
)
4689 * discriminator_ptr
= local_discriminator
;
4690 if (local_linenumber
)
4691 * linenumber_ptr
= local_linenumber
;
4697 if (* linenumber_ptr
)
4704 /* The DWARF2 spec says that the initial length field, and the
4705 offset of the abbreviation table, should both be 4-byte values.
4706 However, some compilers do things differently. */
4709 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
4711 /* Read each remaining comp. units checking each as they are read. */
4712 while (stash
->info_ptr
< stash
->info_ptr_end
)
4715 unsigned int offset_size
= addr_size
;
4716 bfd_byte
*info_ptr_unit
= stash
->info_ptr
;
4718 length
= read_4_bytes (stash
->bfd_ptr
, stash
->info_ptr
, stash
->info_ptr_end
);
4719 /* A 0xffffff length is the DWARF3 way of indicating
4720 we use 64-bit offsets, instead of 32-bit offsets. */
4721 if (length
== 0xffffffff)
4724 length
= read_8_bytes (stash
->bfd_ptr
, stash
->info_ptr
+ 4, stash
->info_ptr_end
);
4725 stash
->info_ptr
+= 12;
4727 /* A zero length is the IRIX way of indicating 64-bit offsets,
4728 mostly because the 64-bit length will generally fit in 32
4729 bits, and the endianness helps. */
4730 else if (length
== 0)
4733 length
= read_4_bytes (stash
->bfd_ptr
, stash
->info_ptr
+ 4, stash
->info_ptr_end
);
4734 stash
->info_ptr
+= 8;
4736 /* In the absence of the hints above, we assume 32-bit DWARF2
4737 offsets even for targets with 64-bit addresses, because:
4738 a) most of the time these targets will not have generated
4739 more than 2Gb of debug info and so will not need 64-bit
4742 b) if they do use 64-bit offsets but they are not using
4743 the size hints that are tested for above then they are
4744 not conforming to the DWARF3 standard anyway. */
4745 else if (addr_size
== 8)
4748 stash
->info_ptr
+= 4;
4751 stash
->info_ptr
+= 4;
4758 if (stash
->info_ptr
+ length
> stash
->info_ptr_end
)
4761 each
= parse_comp_unit (stash
, length
, info_ptr_unit
,
4764 /* The dwarf information is damaged, don't trust it any
4768 new_ptr
= stash
->info_ptr
+ length
;
4769 /* PR 17512: file: 1500698c. */
4770 if (new_ptr
< stash
->info_ptr
)
4772 /* A corrupt length value - do not trust the info any more. */
4777 stash
->info_ptr
= new_ptr
;
4779 if (stash
->all_comp_units
)
4780 stash
->all_comp_units
->prev_unit
= each
;
4782 stash
->last_comp_unit
= each
;
4784 each
->next_unit
= stash
->all_comp_units
;
4785 stash
->all_comp_units
= each
;
4787 /* DW_AT_low_pc and DW_AT_high_pc are optional for
4788 compilation units. If we don't have them (i.e.,
4789 unit->high == 0), we need to consult the line info table
4790 to see if a compilation unit contains the given
4793 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
4794 || each
->arange
.high
== 0
4795 || comp_unit_contains_address (each
, addr
))
4796 && comp_unit_find_line (each
, symbol
, addr
,
4801 found
= ((each
->arange
.high
== 0
4802 || comp_unit_contains_address (each
, addr
))
4803 && comp_unit_find_nearest_line (each
, addr
,
4810 if ((bfd_vma
) (stash
->info_ptr
- stash
->sec_info_ptr
)
4811 == stash
->sec
->size
)
4813 stash
->sec
= find_debug_info (stash
->bfd_ptr
, debug_sections
,
4815 stash
->sec_info_ptr
= stash
->info_ptr
;
4826 if (!function
->is_linkage
)
4831 fun
= _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
4832 *filename_ptr
? NULL
: filename_ptr
,
4834 sec_vma
= section
->vma
;
4835 if (section
->output_section
!= NULL
)
4836 sec_vma
= section
->output_section
->vma
+ section
->output_offset
;
4838 && fun
->value
+ sec_vma
== function
->arange
.low
)
4839 function
->name
= *functionname_ptr
;
4840 /* Even if we didn't find a linkage name, say that we have
4841 to stop a repeated search of symbols. */
4842 function
->is_linkage
= TRUE
;
4844 *functionname_ptr
= function
->name
;
4846 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
4847 unset_sections (stash
);
4853 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
4854 const char **filename_ptr
,
4855 const char **functionname_ptr
,
4856 unsigned int *linenumber_ptr
,
4859 struct dwarf2_debug
*stash
;
4861 stash
= (struct dwarf2_debug
*) *pinfo
;
4864 struct funcinfo
*func
= stash
->inliner_chain
;
4866 if (func
&& func
->caller_func
)
4868 *filename_ptr
= func
->caller_file
;
4869 *functionname_ptr
= func
->caller_func
->name
;
4870 *linenumber_ptr
= func
->caller_line
;
4871 stash
->inliner_chain
= func
->caller_func
;
4880 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
, void **pinfo
)
4882 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
4883 struct comp_unit
*each
;
4885 if (abfd
== NULL
|| stash
== NULL
)
4888 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
4890 struct abbrev_info
**abbrevs
= each
->abbrevs
;
4891 struct funcinfo
*function_table
= each
->function_table
;
4892 struct varinfo
*variable_table
= each
->variable_table
;
4895 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
4897 struct abbrev_info
*abbrev
= abbrevs
[i
];
4901 free (abbrev
->attrs
);
4902 abbrev
= abbrev
->next
;
4906 if (each
->line_table
)
4908 free (each
->line_table
->dirs
);
4909 free (each
->line_table
->files
);
4912 while (function_table
)
4914 if (function_table
->file
)
4916 free (function_table
->file
);
4917 function_table
->file
= NULL
;
4920 if (function_table
->caller_file
)
4922 free (function_table
->caller_file
);
4923 function_table
->caller_file
= NULL
;
4925 function_table
= function_table
->prev_func
;
4928 if (each
->lookup_funcinfo_table
)
4930 free (each
->lookup_funcinfo_table
);
4931 each
->lookup_funcinfo_table
= NULL
;
4934 while (variable_table
)
4936 if (variable_table
->file
)
4938 free (variable_table
->file
);
4939 variable_table
->file
= NULL
;
4942 variable_table
= variable_table
->prev_var
;
4946 if (stash
->dwarf_abbrev_buffer
)
4947 free (stash
->dwarf_abbrev_buffer
);
4948 if (stash
->dwarf_line_buffer
)
4949 free (stash
->dwarf_line_buffer
);
4950 if (stash
->dwarf_str_buffer
)
4951 free (stash
->dwarf_str_buffer
);
4952 if (stash
->dwarf_line_str_buffer
)
4953 free (stash
->dwarf_line_str_buffer
);
4954 if (stash
->dwarf_ranges_buffer
)
4955 free (stash
->dwarf_ranges_buffer
);
4956 if (stash
->info_ptr_memory
)
4957 free (stash
->info_ptr_memory
);
4958 if (stash
->close_on_cleanup
)
4959 bfd_close (stash
->bfd_ptr
);
4960 if (stash
->alt_dwarf_str_buffer
)
4961 free (stash
->alt_dwarf_str_buffer
);
4962 if (stash
->alt_dwarf_info_buffer
)
4963 free (stash
->alt_dwarf_info_buffer
);
4965 free (stash
->sec_vma
);
4966 if (stash
->adjusted_sections
)
4967 free (stash
->adjusted_sections
);
4968 if (stash
->alt_bfd_ptr
)
4969 bfd_close (stash
->alt_bfd_ptr
);
4972 /* Find the function to a particular section and offset,
4973 for error reporting. */
4976 _bfd_elf_find_function (bfd
*abfd
,
4980 const char **filename_ptr
,
4981 const char **functionname_ptr
)
4983 struct elf_find_function_cache
4985 asection
*last_section
;
4987 const char *filename
;
4988 bfd_size_type func_size
;
4991 if (symbols
== NULL
)
4994 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
4997 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
5000 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
5001 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
5005 if (cache
->last_section
!= section
5006 || cache
->func
== NULL
5007 || offset
< cache
->func
->value
5008 || offset
>= cache
->func
->value
+ cache
->func_size
)
5013 /* ??? Given multiple file symbols, it is impossible to reliably
5014 choose the right file name for global symbols. File symbols are
5015 local symbols, and thus all file symbols must sort before any
5016 global symbols. The ELF spec may be interpreted to say that a
5017 file symbol must sort before other local symbols, but currently
5018 ld -r doesn't do this. So, for ld -r output, it is possible to
5019 make a better choice of file name for local symbols by ignoring
5020 file symbols appearing after a given local symbol. */
5021 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
5022 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5026 state
= nothing_seen
;
5027 cache
->filename
= NULL
;
5029 cache
->func_size
= 0;
5030 cache
->last_section
= section
;
5032 for (p
= symbols
; *p
!= NULL
; p
++)
5038 if ((sym
->flags
& BSF_FILE
) != 0)
5041 if (state
== symbol_seen
)
5042 state
= file_after_symbol_seen
;
5046 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
5048 && code_off
<= offset
5049 && (code_off
> low_func
5050 || (code_off
== low_func
5051 && size
> cache
->func_size
)))
5054 cache
->func_size
= size
;
5055 cache
->filename
= NULL
;
5056 low_func
= code_off
;
5058 && ((sym
->flags
& BSF_LOCAL
) != 0
5059 || state
!= file_after_symbol_seen
))
5060 cache
->filename
= bfd_asymbol_name (file
);
5062 if (state
== nothing_seen
)
5063 state
= symbol_seen
;
5067 if (cache
->func
== NULL
)
5071 *filename_ptr
= cache
->filename
;
5072 if (functionname_ptr
)
5073 *functionname_ptr
= bfd_asymbol_name (cache
->func
);