2 Copyright (C) 1994-2015 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 bfd, section and address of the beginning of the
104 section. The bfd might be different than expected because of
105 gnu_debuglink sections. */
108 bfd_byte
*sec_info_ptr
;
110 /* Support for alternate debug info sections created by the DWZ utility:
111 This includes a pointer to an alternate bfd which contains *extra*,
112 possibly duplicate debug sections, and pointers to the loaded
113 .debug_str and .debug_info sections from this bfd. */
115 bfd_byte
* alt_dwarf_str_buffer
;
116 bfd_size_type alt_dwarf_str_size
;
117 bfd_byte
* alt_dwarf_info_buffer
;
118 bfd_size_type alt_dwarf_info_size
;
120 /* A pointer to the memory block allocated for info_ptr. Neither
121 info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
122 beginning of the malloc block. This is used only to free the
124 bfd_byte
*info_ptr_memory
;
126 /* Pointer to the symbol table. */
129 /* Pointer to the .debug_abbrev section loaded into memory. */
130 bfd_byte
*dwarf_abbrev_buffer
;
132 /* Length of the loaded .debug_abbrev section. */
133 bfd_size_type dwarf_abbrev_size
;
135 /* Buffer for decode_line_info. */
136 bfd_byte
*dwarf_line_buffer
;
138 /* Length of the loaded .debug_line section. */
139 bfd_size_type dwarf_line_size
;
141 /* Pointer to the .debug_str section loaded into memory. */
142 bfd_byte
*dwarf_str_buffer
;
144 /* Length of the loaded .debug_str section. */
145 bfd_size_type dwarf_str_size
;
147 /* Pointer to the .debug_ranges section loaded into memory. */
148 bfd_byte
*dwarf_ranges_buffer
;
150 /* Length of the loaded .debug_ranges section. */
151 bfd_size_type dwarf_ranges_size
;
153 /* If the most recent call to bfd_find_nearest_line was given an
154 address in an inlined function, preserve a pointer into the
155 calling chain for subsequent calls to bfd_find_inliner_info to
157 struct funcinfo
*inliner_chain
;
159 /* Section VMAs at the time the stash was built. */
162 /* Number of sections whose VMA we must adjust. */
163 int adjusted_section_count
;
165 /* Array of sections with adjusted VMA. */
166 struct adjusted_section
*adjusted_sections
;
168 /* Number of times find_line is called. This is used in
169 the heuristic for enabling the info hash tables. */
172 #define STASH_INFO_HASH_TRIGGER 100
174 /* Hash table mapping symbol names to function infos. */
175 struct info_hash_table
*funcinfo_hash_table
;
177 /* Hash table mapping symbol names to variable infos. */
178 struct info_hash_table
*varinfo_hash_table
;
180 /* Head of comp_unit list in the last hash table update. */
181 struct comp_unit
*hash_units_head
;
183 /* Status of info hash. */
184 int info_hash_status
;
185 #define STASH_INFO_HASH_OFF 0
186 #define STASH_INFO_HASH_ON 1
187 #define STASH_INFO_HASH_DISABLED 2
189 /* True if we opened bfd_ptr. */
190 bfd_boolean close_on_cleanup
;
200 /* A minimal decoding of DWARF2 compilation units. We only decode
201 what's needed to get to the line number information. */
205 /* Chain the previously read compilation units. */
206 struct comp_unit
*next_unit
;
208 /* Likewise, chain the compilation unit read after this one.
209 The comp units are stored in reversed reading order. */
210 struct comp_unit
*prev_unit
;
212 /* Keep the bfd convenient (for memory allocation). */
215 /* The lowest and highest addresses contained in this compilation
216 unit as specified in the compilation unit header. */
217 struct arange arange
;
219 /* The DW_AT_name attribute (for error messages). */
222 /* The abbrev hash table. */
223 struct abbrev_info
**abbrevs
;
225 /* DW_AT_language. */
228 /* Note that an error was found by comp_unit_find_nearest_line. */
231 /* The DW_AT_comp_dir attribute. */
234 /* TRUE if there is a line number table associated with this comp. unit. */
237 /* Pointer to the current comp_unit so that we can find a given entry
239 bfd_byte
*info_ptr_unit
;
241 /* Pointer to the start of the debug section, for DW_FORM_ref_addr. */
242 bfd_byte
*sec_info_ptr
;
244 /* The offset into .debug_line of the line number table. */
245 unsigned long line_offset
;
247 /* Pointer to the first child die for the comp unit. */
248 bfd_byte
*first_child_die_ptr
;
250 /* The end of the comp unit. */
253 /* The decoded line number, NULL if not yet decoded. */
254 struct line_info_table
*line_table
;
256 /* A list of the functions found in this comp. unit. */
257 struct funcinfo
*function_table
;
259 /* A list of the variables found in this comp. unit. */
260 struct varinfo
*variable_table
;
262 /* Pointer to dwarf2_debug structure. */
263 struct dwarf2_debug
*stash
;
265 /* DWARF format version for this unit - from unit header. */
268 /* Address size for this unit - from unit header. */
269 unsigned char addr_size
;
271 /* Offset size for this unit - from unit header. */
272 unsigned char offset_size
;
274 /* Base address for this unit - from DW_AT_low_pc attribute of
275 DW_TAG_compile_unit DIE */
276 bfd_vma base_address
;
278 /* TRUE if symbols are cached in hash table for faster lookup by name. */
282 /* This data structure holds the information of an abbrev. */
285 unsigned int number
; /* Number identifying abbrev. */
286 enum dwarf_tag tag
; /* DWARF tag. */
287 int has_children
; /* Boolean. */
288 unsigned int num_attrs
; /* Number of attributes. */
289 struct attr_abbrev
*attrs
; /* An array of attribute descriptions. */
290 struct abbrev_info
*next
; /* Next in chain. */
295 enum dwarf_attribute name
;
296 enum dwarf_form form
;
299 /* Map of uncompressed DWARF debug section name to compressed one. It
300 is terminated by NULL uncompressed_name. */
302 const struct dwarf_debug_section dwarf_debug_sections
[] =
304 { ".debug_abbrev", ".zdebug_abbrev" },
305 { ".debug_aranges", ".zdebug_aranges" },
306 { ".debug_frame", ".zdebug_frame" },
307 { ".debug_info", ".zdebug_info" },
308 { ".debug_info", ".zdebug_info" },
309 { ".debug_line", ".zdebug_line" },
310 { ".debug_loc", ".zdebug_loc" },
311 { ".debug_macinfo", ".zdebug_macinfo" },
312 { ".debug_macro", ".zdebug_macro" },
313 { ".debug_pubnames", ".zdebug_pubnames" },
314 { ".debug_pubtypes", ".zdebug_pubtypes" },
315 { ".debug_ranges", ".zdebug_ranges" },
316 { ".debug_static_func", ".zdebug_static_func" },
317 { ".debug_static_vars", ".zdebug_static_vars" },
318 { ".debug_str", ".zdebug_str", },
319 { ".debug_str", ".zdebug_str", },
320 { ".debug_types", ".zdebug_types" },
321 /* GNU DWARF 1 extensions */
322 { ".debug_sfnames", ".zdebug_sfnames" },
323 { ".debug_srcinfo", ".zebug_srcinfo" },
324 /* SGI/MIPS DWARF 2 extensions */
325 { ".debug_funcnames", ".zdebug_funcnames" },
326 { ".debug_typenames", ".zdebug_typenames" },
327 { ".debug_varnames", ".zdebug_varnames" },
328 { ".debug_weaknames", ".zdebug_weaknames" },
332 /* NB/ Numbers in this enum must match up with indicies
333 into the dwarf_debug_sections[] array above. */
334 enum dwarf_debug_section_enum
361 #ifndef ABBREV_HASH_SIZE
362 #define ABBREV_HASH_SIZE 121
364 #ifndef ATTR_ALLOC_CHUNK
365 #define ATTR_ALLOC_CHUNK 4
368 /* Variable and function hash tables. This is used to speed up look-up
369 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
370 In order to share code between variable and function infos, we use
371 a list of untyped pointer for all variable/function info associated with
372 a symbol. We waste a bit of memory for list with one node but that
373 simplifies the code. */
375 struct info_list_node
377 struct info_list_node
*next
;
381 /* Info hash entry. */
382 struct info_hash_entry
384 struct bfd_hash_entry root
;
385 struct info_list_node
*head
;
388 struct info_hash_table
390 struct bfd_hash_table base
;
393 /* Function to create a new entry in info hash table. */
395 static struct bfd_hash_entry
*
396 info_hash_table_newfunc (struct bfd_hash_entry
*entry
,
397 struct bfd_hash_table
*table
,
400 struct info_hash_entry
*ret
= (struct info_hash_entry
*) entry
;
402 /* Allocate the structure if it has not already been allocated by a
406 ret
= (struct info_hash_entry
*) bfd_hash_allocate (table
,
412 /* Call the allocation method of the base class. */
413 ret
= ((struct info_hash_entry
*)
414 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
416 /* Initialize the local fields here. */
420 return (struct bfd_hash_entry
*) ret
;
423 /* Function to create a new info hash table. It returns a pointer to the
424 newly created table or NULL if there is any error. We need abfd
425 solely for memory allocation. */
427 static struct info_hash_table
*
428 create_info_hash_table (bfd
*abfd
)
430 struct info_hash_table
*hash_table
;
432 hash_table
= ((struct info_hash_table
*)
433 bfd_alloc (abfd
, sizeof (struct info_hash_table
)));
437 if (!bfd_hash_table_init (&hash_table
->base
, info_hash_table_newfunc
,
438 sizeof (struct info_hash_entry
)))
440 bfd_release (abfd
, hash_table
);
447 /* Insert an info entry into an info hash table. We do not check of
448 duplicate entries. Also, the caller need to guarantee that the
449 right type of info in inserted as info is passed as a void* pointer.
450 This function returns true if there is no error. */
453 insert_info_hash_table (struct info_hash_table
*hash_table
,
458 struct info_hash_entry
*entry
;
459 struct info_list_node
*node
;
461 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
,
466 node
= (struct info_list_node
*) bfd_hash_allocate (&hash_table
->base
,
472 node
->next
= entry
->head
;
478 /* Look up an info entry list from an info hash table. Return NULL
481 static struct info_list_node
*
482 lookup_info_hash_table (struct info_hash_table
*hash_table
, const char *key
)
484 struct info_hash_entry
*entry
;
486 entry
= (struct info_hash_entry
*) bfd_hash_lookup (&hash_table
->base
, key
,
488 return entry
? entry
->head
: NULL
;
491 /* Read a section into its appropriate place in the dwarf2_debug
492 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
493 not NULL, use bfd_simple_get_relocated_section_contents to read the
494 section contents, otherwise use bfd_get_section_contents. Fail if
495 the located section does not contain at least OFFSET bytes. */
498 read_section (bfd
* abfd
,
499 const struct dwarf_debug_section
*sec
,
502 bfd_byte
** section_buffer
,
503 bfd_size_type
* section_size
)
506 const char *section_name
= sec
->uncompressed_name
;
508 /* The section may have already been read. */
509 if (*section_buffer
== NULL
)
511 msec
= bfd_get_section_by_name (abfd
, section_name
);
514 section_name
= sec
->compressed_name
;
515 if (section_name
!= NULL
)
516 msec
= bfd_get_section_by_name (abfd
, section_name
);
520 (*_bfd_error_handler
) (_("Dwarf Error: Can't find %s section."),
521 sec
->uncompressed_name
);
522 bfd_set_error (bfd_error_bad_value
);
526 *section_size
= msec
->rawsize
? msec
->rawsize
: msec
->size
;
530 = bfd_simple_get_relocated_section_contents (abfd
, msec
, NULL
, syms
);
531 if (! *section_buffer
)
536 *section_buffer
= (bfd_byte
*) bfd_malloc (*section_size
);
537 if (! *section_buffer
)
539 if (! bfd_get_section_contents (abfd
, msec
, *section_buffer
,
545 /* It is possible to get a bad value for the offset into the section
546 that the client wants. Validate it here to avoid trouble later. */
547 if (offset
!= 0 && offset
>= *section_size
)
549 (*_bfd_error_handler
) (_("Dwarf Error: Offset (%lu)"
550 " greater than or equal to %s size (%lu)."),
551 (long) offset
, section_name
, *section_size
);
552 bfd_set_error (bfd_error_bad_value
);
560 The following function up to the END VERBATIM mark are
561 copied directly from dwarf2read.c. */
563 /* Read dwarf information from a buffer. */
566 read_1_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
)
568 return bfd_get_8 (abfd
, buf
);
572 read_1_signed_byte (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_byte
*buf
)
574 return bfd_get_signed_8 (abfd
, buf
);
578 read_2_bytes (bfd
*abfd
, bfd_byte
*buf
)
580 return bfd_get_16 (abfd
, buf
);
584 read_4_bytes (bfd
*abfd
, bfd_byte
*buf
)
586 return bfd_get_32 (abfd
, buf
);
590 read_8_bytes (bfd
*abfd
, bfd_byte
*buf
)
592 return bfd_get_64 (abfd
, buf
);
596 read_n_bytes (bfd
*abfd ATTRIBUTE_UNUSED
,
598 unsigned int size ATTRIBUTE_UNUSED
)
604 read_string (bfd
*abfd ATTRIBUTE_UNUSED
,
606 unsigned int *bytes_read_ptr
)
608 /* Return a pointer to the embedded string. */
609 char *str
= (char *) buf
;
617 *bytes_read_ptr
= strlen (str
) + 1;
624 read_indirect_string (struct comp_unit
* unit
,
626 unsigned int * bytes_read_ptr
)
629 struct dwarf2_debug
*stash
= unit
->stash
;
632 if (unit
->offset_size
== 4)
633 offset
= read_4_bytes (unit
->abfd
, buf
);
635 offset
= read_8_bytes (unit
->abfd
, buf
);
637 *bytes_read_ptr
= unit
->offset_size
;
639 if (! read_section (unit
->abfd
, &stash
->debug_sections
[debug_str
],
641 &stash
->dwarf_str_buffer
, &stash
->dwarf_str_size
))
644 str
= (char *) stash
->dwarf_str_buffer
+ offset
;
650 /* Like read_indirect_string but uses a .debug_str located in
651 an alternate file pointed to by the .gnu_debugaltlink section.
652 Used to impement DW_FORM_GNU_strp_alt. */
655 read_alt_indirect_string (struct comp_unit
* unit
,
657 unsigned int * bytes_read_ptr
)
660 struct dwarf2_debug
*stash
= unit
->stash
;
663 if (unit
->offset_size
== 4)
664 offset
= read_4_bytes (unit
->abfd
, buf
);
666 offset
= read_8_bytes (unit
->abfd
, buf
);
668 *bytes_read_ptr
= unit
->offset_size
;
670 if (stash
->alt_bfd_ptr
== NULL
)
673 char * debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
675 if (debug_filename
== NULL
)
678 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
679 || ! bfd_check_format (debug_bfd
, bfd_object
))
682 bfd_close (debug_bfd
);
684 /* FIXME: Should we report our failure to follow the debuglink ? */
685 free (debug_filename
);
688 stash
->alt_bfd_ptr
= debug_bfd
;
691 if (! read_section (unit
->stash
->alt_bfd_ptr
,
692 stash
->debug_sections
+ debug_str_alt
,
693 NULL
, /* FIXME: Do we need to load alternate symbols ? */
695 &stash
->alt_dwarf_str_buffer
,
696 &stash
->alt_dwarf_str_size
))
699 str
= (char *) stash
->alt_dwarf_str_buffer
+ offset
;
706 /* Resolve an alternate reference from UNIT at OFFSET.
707 Returns a pointer into the loaded alternate CU upon success
708 or NULL upon failure. */
711 read_alt_indirect_ref (struct comp_unit
* unit
,
714 struct dwarf2_debug
*stash
= unit
->stash
;
716 if (stash
->alt_bfd_ptr
== NULL
)
719 char * debug_filename
= bfd_follow_gnu_debugaltlink (unit
->abfd
, DEBUGDIR
);
721 if (debug_filename
== NULL
)
724 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
725 || ! bfd_check_format (debug_bfd
, bfd_object
))
728 bfd_close (debug_bfd
);
730 /* FIXME: Should we report our failure to follow the debuglink ? */
731 free (debug_filename
);
734 stash
->alt_bfd_ptr
= debug_bfd
;
737 if (! read_section (unit
->stash
->alt_bfd_ptr
,
738 stash
->debug_sections
+ debug_info_alt
,
739 NULL
, /* FIXME: Do we need to load alternate symbols ? */
741 &stash
->alt_dwarf_info_buffer
,
742 &stash
->alt_dwarf_info_size
))
745 return stash
->alt_dwarf_info_buffer
+ offset
;
749 read_address (struct comp_unit
*unit
, bfd_byte
*buf
)
753 if (bfd_get_flavour (unit
->abfd
) == bfd_target_elf_flavour
)
754 signed_vma
= get_elf_backend_data (unit
->abfd
)->sign_extend_vma
;
758 switch (unit
->addr_size
)
761 return bfd_get_signed_64 (unit
->abfd
, buf
);
763 return bfd_get_signed_32 (unit
->abfd
, buf
);
765 return bfd_get_signed_16 (unit
->abfd
, buf
);
772 switch (unit
->addr_size
)
775 return bfd_get_64 (unit
->abfd
, buf
);
777 return bfd_get_32 (unit
->abfd
, buf
);
779 return bfd_get_16 (unit
->abfd
, buf
);
786 /* Lookup an abbrev_info structure in the abbrev hash table. */
788 static struct abbrev_info
*
789 lookup_abbrev (unsigned int number
, struct abbrev_info
**abbrevs
)
791 unsigned int hash_number
;
792 struct abbrev_info
*abbrev
;
794 hash_number
= number
% ABBREV_HASH_SIZE
;
795 abbrev
= abbrevs
[hash_number
];
799 if (abbrev
->number
== number
)
802 abbrev
= abbrev
->next
;
808 /* In DWARF version 2, the description of the debugging information is
809 stored in a separate .debug_abbrev section. Before we read any
810 dies from a section we read in all abbreviations and install them
813 static struct abbrev_info
**
814 read_abbrevs (bfd
*abfd
, bfd_uint64_t offset
, struct dwarf2_debug
*stash
)
816 struct abbrev_info
**abbrevs
;
817 bfd_byte
*abbrev_ptr
;
818 struct abbrev_info
*cur_abbrev
;
819 unsigned int abbrev_number
, bytes_read
, abbrev_name
;
820 unsigned int abbrev_form
, hash_number
;
823 if (! read_section (abfd
, &stash
->debug_sections
[debug_abbrev
],
825 &stash
->dwarf_abbrev_buffer
, &stash
->dwarf_abbrev_size
))
828 amt
= sizeof (struct abbrev_info
*) * ABBREV_HASH_SIZE
;
829 abbrevs
= (struct abbrev_info
**) bfd_zalloc (abfd
, amt
);
833 abbrev_ptr
= stash
->dwarf_abbrev_buffer
+ offset
;
834 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
835 abbrev_ptr
+= bytes_read
;
837 /* Loop until we reach an abbrev number of 0. */
838 while (abbrev_number
)
840 amt
= sizeof (struct abbrev_info
);
841 cur_abbrev
= (struct abbrev_info
*) bfd_zalloc (abfd
, amt
);
842 if (cur_abbrev
== NULL
)
845 /* Read in abbrev header. */
846 cur_abbrev
->number
= abbrev_number
;
847 cur_abbrev
->tag
= (enum dwarf_tag
)
848 read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
849 abbrev_ptr
+= bytes_read
;
850 cur_abbrev
->has_children
= read_1_byte (abfd
, abbrev_ptr
);
853 /* Now read in declarations. */
854 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
855 abbrev_ptr
+= bytes_read
;
856 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
857 abbrev_ptr
+= bytes_read
;
861 if ((cur_abbrev
->num_attrs
% ATTR_ALLOC_CHUNK
) == 0)
863 struct attr_abbrev
*tmp
;
865 amt
= cur_abbrev
->num_attrs
+ ATTR_ALLOC_CHUNK
;
866 amt
*= sizeof (struct attr_abbrev
);
867 tmp
= (struct attr_abbrev
*) bfd_realloc (cur_abbrev
->attrs
, amt
);
872 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
874 struct abbrev_info
*abbrev
= abbrevs
[i
];
878 free (abbrev
->attrs
);
879 abbrev
= abbrev
->next
;
884 cur_abbrev
->attrs
= tmp
;
887 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
].name
888 = (enum dwarf_attribute
) abbrev_name
;
889 cur_abbrev
->attrs
[cur_abbrev
->num_attrs
++].form
890 = (enum dwarf_form
) abbrev_form
;
891 abbrev_name
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
892 abbrev_ptr
+= bytes_read
;
893 abbrev_form
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
894 abbrev_ptr
+= bytes_read
;
897 hash_number
= abbrev_number
% ABBREV_HASH_SIZE
;
898 cur_abbrev
->next
= abbrevs
[hash_number
];
899 abbrevs
[hash_number
] = cur_abbrev
;
901 /* Get next abbreviation.
902 Under Irix6 the abbreviations for a compilation unit are not
903 always properly terminated with an abbrev number of 0.
904 Exit loop if we encounter an abbreviation which we have
905 already read (which means we are about to read the abbreviations
906 for the next compile unit) or if the end of the abbreviation
908 if ((unsigned int) (abbrev_ptr
- stash
->dwarf_abbrev_buffer
)
909 >= stash
->dwarf_abbrev_size
)
911 abbrev_number
= read_unsigned_leb128 (abfd
, abbrev_ptr
, &bytes_read
);
912 abbrev_ptr
+= bytes_read
;
913 if (lookup_abbrev (abbrev_number
,abbrevs
) != NULL
)
920 /* Returns true if the form is one which has a string value. */
922 static inline bfd_boolean
923 is_str_attr (enum dwarf_form form
)
925 return form
== DW_FORM_string
|| form
== DW_FORM_strp
|| form
== DW_FORM_GNU_strp_alt
;
928 /* Read an attribute value described by an attribute form. */
931 read_attribute_value (struct attribute
*attr
,
933 struct comp_unit
*unit
,
936 bfd
*abfd
= unit
->abfd
;
937 unsigned int bytes_read
;
938 struct dwarf_block
*blk
;
941 attr
->form
= (enum dwarf_form
) form
;
945 case DW_FORM_ref_addr
:
946 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
948 if (unit
->version
== 3 || unit
->version
== 4)
950 if (unit
->offset_size
== 4)
951 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
);
953 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
);
954 info_ptr
+= unit
->offset_size
;
959 attr
->u
.val
= read_address (unit
, info_ptr
);
960 info_ptr
+= unit
->addr_size
;
962 case DW_FORM_GNU_ref_alt
:
963 case DW_FORM_sec_offset
:
964 if (unit
->offset_size
== 4)
965 attr
->u
.val
= read_4_bytes (unit
->abfd
, info_ptr
);
967 attr
->u
.val
= read_8_bytes (unit
->abfd
, info_ptr
);
968 info_ptr
+= unit
->offset_size
;
971 amt
= sizeof (struct dwarf_block
);
972 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
975 blk
->size
= read_2_bytes (abfd
, info_ptr
);
977 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
978 info_ptr
+= blk
->size
;
982 amt
= sizeof (struct dwarf_block
);
983 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
986 blk
->size
= read_4_bytes (abfd
, info_ptr
);
988 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
989 info_ptr
+= blk
->size
;
993 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
);
997 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
);
1001 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
1004 case DW_FORM_string
:
1005 attr
->u
.str
= read_string (abfd
, info_ptr
, &bytes_read
);
1006 info_ptr
+= bytes_read
;
1009 attr
->u
.str
= read_indirect_string (unit
, info_ptr
, &bytes_read
);
1010 info_ptr
+= bytes_read
;
1012 case DW_FORM_GNU_strp_alt
:
1013 attr
->u
.str
= read_alt_indirect_string (unit
, info_ptr
, &bytes_read
);
1014 info_ptr
+= bytes_read
;
1016 case DW_FORM_exprloc
:
1018 amt
= sizeof (struct dwarf_block
);
1019 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1022 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1023 info_ptr
+= bytes_read
;
1024 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
1025 info_ptr
+= blk
->size
;
1028 case DW_FORM_block1
:
1029 amt
= sizeof (struct dwarf_block
);
1030 blk
= (struct dwarf_block
*) bfd_alloc (abfd
, amt
);
1033 blk
->size
= read_1_byte (abfd
, info_ptr
);
1035 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
1036 info_ptr
+= blk
->size
;
1040 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
1044 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
1047 case DW_FORM_flag_present
:
1051 attr
->u
.sval
= read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
1052 info_ptr
+= bytes_read
;
1055 attr
->u
.val
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1056 info_ptr
+= bytes_read
;
1059 attr
->u
.val
= read_1_byte (abfd
, info_ptr
);
1063 attr
->u
.val
= read_2_bytes (abfd
, info_ptr
);
1067 attr
->u
.val
= read_4_bytes (abfd
, info_ptr
);
1071 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
1074 case DW_FORM_ref_sig8
:
1075 attr
->u
.val
= read_8_bytes (abfd
, info_ptr
);
1078 case DW_FORM_ref_udata
:
1079 attr
->u
.val
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1080 info_ptr
+= bytes_read
;
1082 case DW_FORM_indirect
:
1083 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
1084 info_ptr
+= bytes_read
;
1085 info_ptr
= read_attribute_value (attr
, form
, unit
, info_ptr
);
1088 (*_bfd_error_handler
) (_("Dwarf Error: Invalid or unhandled FORM value: %#x."),
1090 bfd_set_error (bfd_error_bad_value
);
1096 /* Read an attribute described by an abbreviated attribute. */
1099 read_attribute (struct attribute
*attr
,
1100 struct attr_abbrev
*abbrev
,
1101 struct comp_unit
*unit
,
1104 attr
->name
= abbrev
->name
;
1105 info_ptr
= read_attribute_value (attr
, abbrev
->form
, unit
, info_ptr
);
1109 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1113 non_mangled (int lang
)
1123 case DW_LANG_Cobol74
:
1124 case DW_LANG_Cobol85
:
1125 case DW_LANG_Fortran77
:
1126 case DW_LANG_Pascal83
:
1136 /* Source line information table routines. */
1138 #define FILE_ALLOC_CHUNK 5
1139 #define DIR_ALLOC_CHUNK 5
1143 struct line_info
* prev_line
;
1147 unsigned int column
;
1148 unsigned int discriminator
;
1149 unsigned char op_index
;
1150 unsigned char end_sequence
; /* End of (sequential) code sequence. */
1161 struct line_sequence
1164 struct line_sequence
* prev_sequence
;
1165 struct line_info
* last_line
; /* Largest VMA. */
1168 struct line_info_table
1171 unsigned int num_files
;
1172 unsigned int num_dirs
;
1173 unsigned int num_sequences
;
1176 struct fileinfo
* files
;
1177 struct line_sequence
* sequences
;
1178 struct line_info
* lcl_head
; /* Local head; used in 'add_line_info'. */
1181 /* Remember some information about each function. If the function is
1182 inlined (DW_TAG_inlined_subroutine) it may have two additional
1183 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1184 source code location where this function was inlined. */
1188 /* Pointer to previous function in list of all functions. */
1189 struct funcinfo
*prev_func
;
1190 /* Pointer to function one scope higher. */
1191 struct funcinfo
*caller_func
;
1192 /* Source location file name where caller_func inlines this func. */
1194 /* Source location file name. */
1196 /* Source location line number where caller_func inlines this func. */
1198 /* Source location line number. */
1201 bfd_boolean is_linkage
;
1203 struct arange arange
;
1204 /* Where the symbol is defined. */
1210 /* Pointer to previous variable in list of all variables */
1211 struct varinfo
*prev_var
;
1212 /* Source location file name */
1214 /* Source location line number */
1219 /* Where the symbol is defined */
1221 /* Is this a stack variable? */
1222 unsigned int stack
: 1;
1225 /* Return TRUE if NEW_LINE should sort after LINE. */
1227 static inline bfd_boolean
1228 new_line_sorts_after (struct line_info
*new_line
, struct line_info
*line
)
1230 return (new_line
->address
> line
->address
1231 || (new_line
->address
== line
->address
1232 && (new_line
->op_index
> line
->op_index
1233 || (new_line
->op_index
== line
->op_index
1234 && new_line
->end_sequence
< line
->end_sequence
))));
1238 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1239 that the list is sorted. Note that the line_info list is sorted from
1240 highest to lowest VMA (with possible duplicates); that is,
1241 line_info->prev_line always accesses an equal or smaller VMA. */
1244 add_line_info (struct line_info_table
*table
,
1246 unsigned char op_index
,
1249 unsigned int column
,
1250 unsigned int discriminator
,
1253 bfd_size_type amt
= sizeof (struct line_info
);
1254 struct line_sequence
* seq
= table
->sequences
;
1255 struct line_info
* info
= (struct line_info
*) bfd_alloc (table
->abfd
, amt
);
1260 /* Set member data of 'info'. */
1261 info
->prev_line
= NULL
;
1262 info
->address
= address
;
1263 info
->op_index
= op_index
;
1265 info
->column
= column
;
1266 info
->discriminator
= discriminator
;
1267 info
->end_sequence
= end_sequence
;
1269 if (filename
&& filename
[0])
1271 info
->filename
= (char *) bfd_alloc (table
->abfd
, strlen (filename
) + 1);
1272 if (info
->filename
== NULL
)
1274 strcpy (info
->filename
, filename
);
1277 info
->filename
= NULL
;
1279 /* Find the correct location for 'info'. Normally we will receive
1280 new line_info data 1) in order and 2) with increasing VMAs.
1281 However some compilers break the rules (cf. decode_line_info) and
1282 so we include some heuristics for quickly finding the correct
1283 location for 'info'. In particular, these heuristics optimize for
1284 the common case in which the VMA sequence that we receive is a
1285 list of locally sorted VMAs such as
1286 p...z a...j (where a < j < p < z)
1288 Note: table->lcl_head is used to head an *actual* or *possible*
1289 sub-sequence within the list (such as a...j) that is not directly
1290 headed by table->last_line
1292 Note: we may receive duplicate entries from 'decode_line_info'. */
1295 && seq
->last_line
->address
== address
1296 && seq
->last_line
->op_index
== op_index
1297 && seq
->last_line
->end_sequence
== end_sequence
)
1299 /* We only keep the last entry with the same address and end
1300 sequence. See PR ld/4986. */
1301 if (table
->lcl_head
== seq
->last_line
)
1302 table
->lcl_head
= info
;
1303 info
->prev_line
= seq
->last_line
->prev_line
;
1304 seq
->last_line
= info
;
1306 else if (!seq
|| seq
->last_line
->end_sequence
)
1308 /* Start a new line sequence. */
1309 amt
= sizeof (struct line_sequence
);
1310 seq
= (struct line_sequence
*) bfd_malloc (amt
);
1313 seq
->low_pc
= address
;
1314 seq
->prev_sequence
= table
->sequences
;
1315 seq
->last_line
= info
;
1316 table
->lcl_head
= info
;
1317 table
->sequences
= seq
;
1318 table
->num_sequences
++;
1320 else if (new_line_sorts_after (info
, seq
->last_line
))
1322 /* Normal case: add 'info' to the beginning of the current sequence. */
1323 info
->prev_line
= seq
->last_line
;
1324 seq
->last_line
= info
;
1326 /* lcl_head: initialize to head a *possible* sequence at the end. */
1327 if (!table
->lcl_head
)
1328 table
->lcl_head
= info
;
1330 else if (!new_line_sorts_after (info
, table
->lcl_head
)
1331 && (!table
->lcl_head
->prev_line
1332 || new_line_sorts_after (info
, table
->lcl_head
->prev_line
)))
1334 /* Abnormal but easy: lcl_head is the head of 'info'. */
1335 info
->prev_line
= table
->lcl_head
->prev_line
;
1336 table
->lcl_head
->prev_line
= info
;
1340 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1341 are valid heads for 'info'. Reset 'lcl_head'. */
1342 struct line_info
* li2
= seq
->last_line
; /* Always non-NULL. */
1343 struct line_info
* li1
= li2
->prev_line
;
1347 if (!new_line_sorts_after (info
, li2
)
1348 && new_line_sorts_after (info
, li1
))
1351 li2
= li1
; /* always non-NULL */
1352 li1
= li1
->prev_line
;
1354 table
->lcl_head
= li2
;
1355 info
->prev_line
= table
->lcl_head
->prev_line
;
1356 table
->lcl_head
->prev_line
= info
;
1357 if (address
< seq
->low_pc
)
1358 seq
->low_pc
= address
;
1363 /* Extract a fully qualified filename from a line info table.
1364 The returned string has been malloc'ed and it is the caller's
1365 responsibility to free it. */
1368 concat_filename (struct line_info_table
*table
, unsigned int file
)
1372 if (file
- 1 >= table
->num_files
)
1374 /* FILE == 0 means unknown. */
1376 (*_bfd_error_handler
)
1377 (_("Dwarf Error: mangled line number section (bad file number)."));
1378 return strdup ("<unknown>");
1381 filename
= table
->files
[file
- 1].name
;
1383 if (!IS_ABSOLUTE_PATH (filename
))
1385 char *dir_name
= NULL
;
1386 char *subdir_name
= NULL
;
1390 if (table
->files
[file
- 1].dir
1391 /* PR 17512: file: 7f3d2e4b. */
1392 && table
->dirs
!= NULL
)
1393 subdir_name
= table
->dirs
[table
->files
[file
- 1].dir
- 1];
1395 if (!subdir_name
|| !IS_ABSOLUTE_PATH (subdir_name
))
1396 dir_name
= table
->comp_dir
;
1400 dir_name
= subdir_name
;
1405 return strdup (filename
);
1407 len
= strlen (dir_name
) + strlen (filename
) + 2;
1411 len
+= strlen (subdir_name
) + 1;
1412 name
= (char *) bfd_malloc (len
);
1414 sprintf (name
, "%s/%s/%s", dir_name
, subdir_name
, filename
);
1418 name
= (char *) bfd_malloc (len
);
1420 sprintf (name
, "%s/%s", dir_name
, filename
);
1426 return strdup (filename
);
1430 arange_add (const struct comp_unit
*unit
, struct arange
*first_arange
,
1431 bfd_vma low_pc
, bfd_vma high_pc
)
1433 struct arange
*arange
;
1435 /* Ignore empty ranges. */
1436 if (low_pc
== high_pc
)
1439 /* If the first arange is empty, use it. */
1440 if (first_arange
->high
== 0)
1442 first_arange
->low
= low_pc
;
1443 first_arange
->high
= high_pc
;
1447 /* Next see if we can cheaply extend an existing range. */
1448 arange
= first_arange
;
1451 if (low_pc
== arange
->high
)
1453 arange
->high
= high_pc
;
1456 if (high_pc
== arange
->low
)
1458 arange
->low
= low_pc
;
1461 arange
= arange
->next
;
1465 /* Need to allocate a new arange and insert it into the arange list.
1466 Order isn't significant, so just insert after the first arange. */
1467 arange
= (struct arange
*) bfd_alloc (unit
->abfd
, sizeof (*arange
));
1470 arange
->low
= low_pc
;
1471 arange
->high
= high_pc
;
1472 arange
->next
= first_arange
->next
;
1473 first_arange
->next
= arange
;
1477 /* Compare function for line sequences. */
1480 compare_sequences (const void* a
, const void* b
)
1482 const struct line_sequence
* seq1
= a
;
1483 const struct line_sequence
* seq2
= b
;
1485 /* Sort by low_pc as the primary key. */
1486 if (seq1
->low_pc
< seq2
->low_pc
)
1488 if (seq1
->low_pc
> seq2
->low_pc
)
1491 /* If low_pc values are equal, sort in reverse order of
1492 high_pc, so that the largest region comes first. */
1493 if (seq1
->last_line
->address
< seq2
->last_line
->address
)
1495 if (seq1
->last_line
->address
> seq2
->last_line
->address
)
1498 if (seq1
->last_line
->op_index
< seq2
->last_line
->op_index
)
1500 if (seq1
->last_line
->op_index
> seq2
->last_line
->op_index
)
1506 /* Sort the line sequences for quick lookup. */
1509 sort_line_sequences (struct line_info_table
* table
)
1512 struct line_sequence
* sequences
;
1513 struct line_sequence
* seq
;
1515 unsigned int num_sequences
= table
->num_sequences
;
1516 bfd_vma last_high_pc
;
1518 if (num_sequences
== 0)
1521 /* Allocate space for an array of sequences. */
1522 amt
= sizeof (struct line_sequence
) * num_sequences
;
1523 sequences
= (struct line_sequence
*) bfd_alloc (table
->abfd
, amt
);
1524 if (sequences
== NULL
)
1527 /* Copy the linked list into the array, freeing the original nodes. */
1528 seq
= table
->sequences
;
1529 for (n
= 0; n
< num_sequences
; n
++)
1531 struct line_sequence
* last_seq
= seq
;
1534 sequences
[n
].low_pc
= seq
->low_pc
;
1535 sequences
[n
].prev_sequence
= NULL
;
1536 sequences
[n
].last_line
= seq
->last_line
;
1537 seq
= seq
->prev_sequence
;
1540 BFD_ASSERT (seq
== NULL
);
1542 qsort (sequences
, n
, sizeof (struct line_sequence
), compare_sequences
);
1544 /* Make the list binary-searchable by trimming overlapping entries
1545 and removing nested entries. */
1547 last_high_pc
= sequences
[0].last_line
->address
;
1548 for (n
= 1; n
< table
->num_sequences
; n
++)
1550 if (sequences
[n
].low_pc
< last_high_pc
)
1552 if (sequences
[n
].last_line
->address
<= last_high_pc
)
1553 /* Skip nested entries. */
1556 /* Trim overlapping entries. */
1557 sequences
[n
].low_pc
= last_high_pc
;
1559 last_high_pc
= sequences
[n
].last_line
->address
;
1560 if (n
> num_sequences
)
1562 /* Close up the gap. */
1563 sequences
[num_sequences
].low_pc
= sequences
[n
].low_pc
;
1564 sequences
[num_sequences
].last_line
= sequences
[n
].last_line
;
1569 table
->sequences
= sequences
;
1570 table
->num_sequences
= num_sequences
;
1574 /* Decode the line number information for UNIT. */
1576 static struct line_info_table
*
1577 decode_line_info (struct comp_unit
*unit
, struct dwarf2_debug
*stash
)
1579 bfd
*abfd
= unit
->abfd
;
1580 struct line_info_table
* table
;
1583 struct line_head lh
;
1584 unsigned int i
, bytes_read
, offset_size
;
1585 char *cur_file
, *cur_dir
;
1586 unsigned char op_code
, extended_op
, adj_opcode
;
1587 unsigned int exop_len
;
1590 if (! read_section (abfd
, &stash
->debug_sections
[debug_line
],
1591 stash
->syms
, unit
->line_offset
,
1592 &stash
->dwarf_line_buffer
, &stash
->dwarf_line_size
))
1595 amt
= sizeof (struct line_info_table
);
1596 table
= (struct line_info_table
*) bfd_alloc (abfd
, amt
);
1600 table
->comp_dir
= unit
->comp_dir
;
1602 table
->num_files
= 0;
1603 table
->files
= NULL
;
1605 table
->num_dirs
= 0;
1608 table
->num_sequences
= 0;
1609 table
->sequences
= NULL
;
1611 table
->lcl_head
= NULL
;
1613 line_ptr
= stash
->dwarf_line_buffer
+ unit
->line_offset
;
1615 /* Read in the prologue. */
1616 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
1619 if (lh
.total_length
== 0xffffffff)
1621 lh
.total_length
= read_8_bytes (abfd
, line_ptr
);
1625 else if (lh
.total_length
== 0 && unit
->addr_size
== 8)
1627 /* Handle (non-standard) 64-bit DWARF2 formats. */
1628 lh
.total_length
= read_4_bytes (abfd
, line_ptr
);
1632 line_end
= line_ptr
+ lh
.total_length
;
1633 lh
.version
= read_2_bytes (abfd
, line_ptr
);
1634 if (lh
.version
< 2 || lh
.version
> 4)
1636 (*_bfd_error_handler
)
1637 (_("Dwarf Error: Unhandled .debug_line version %d."), lh
.version
);
1638 bfd_set_error (bfd_error_bad_value
);
1642 if (offset_size
== 4)
1643 lh
.prologue_length
= read_4_bytes (abfd
, line_ptr
);
1645 lh
.prologue_length
= read_8_bytes (abfd
, line_ptr
);
1646 line_ptr
+= offset_size
;
1647 lh
.minimum_instruction_length
= read_1_byte (abfd
, line_ptr
);
1649 if (lh
.version
>= 4)
1651 lh
.maximum_ops_per_insn
= read_1_byte (abfd
, line_ptr
);
1655 lh
.maximum_ops_per_insn
= 1;
1656 if (lh
.maximum_ops_per_insn
== 0)
1658 (*_bfd_error_handler
)
1659 (_("Dwarf Error: Invalid maximum operations per instruction."));
1660 bfd_set_error (bfd_error_bad_value
);
1663 lh
.default_is_stmt
= read_1_byte (abfd
, line_ptr
);
1665 lh
.line_base
= read_1_signed_byte (abfd
, line_ptr
);
1667 lh
.line_range
= read_1_byte (abfd
, line_ptr
);
1669 lh
.opcode_base
= read_1_byte (abfd
, line_ptr
);
1671 amt
= lh
.opcode_base
* sizeof (unsigned char);
1672 lh
.standard_opcode_lengths
= (unsigned char *) bfd_alloc (abfd
, amt
);
1674 lh
.standard_opcode_lengths
[0] = 1;
1676 for (i
= 1; i
< lh
.opcode_base
; ++i
)
1678 lh
.standard_opcode_lengths
[i
] = read_1_byte (abfd
, line_ptr
);
1682 /* Read directory table. */
1683 while ((cur_dir
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1685 line_ptr
+= bytes_read
;
1687 if ((table
->num_dirs
% DIR_ALLOC_CHUNK
) == 0)
1691 amt
= table
->num_dirs
+ DIR_ALLOC_CHUNK
;
1692 amt
*= sizeof (char *);
1694 tmp
= (char **) bfd_realloc (table
->dirs
, amt
);
1700 table
->dirs
[table
->num_dirs
++] = cur_dir
;
1703 line_ptr
+= bytes_read
;
1705 /* Read file name table. */
1706 while ((cur_file
= read_string (abfd
, line_ptr
, &bytes_read
)) != NULL
)
1708 line_ptr
+= bytes_read
;
1710 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1712 struct fileinfo
*tmp
;
1714 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1715 amt
*= sizeof (struct fileinfo
);
1717 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1723 table
->files
[table
->num_files
].name
= cur_file
;
1724 table
->files
[table
->num_files
].dir
=
1725 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1726 line_ptr
+= bytes_read
;
1727 table
->files
[table
->num_files
].time
=
1728 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1729 line_ptr
+= bytes_read
;
1730 table
->files
[table
->num_files
].size
=
1731 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1732 line_ptr
+= bytes_read
;
1736 line_ptr
+= bytes_read
;
1738 /* Read the statement sequences until there's nothing left. */
1739 while (line_ptr
< line_end
)
1741 /* State machine registers. */
1742 bfd_vma address
= 0;
1743 unsigned char op_index
= 0;
1744 char * filename
= table
->num_files
? concat_filename (table
, 1) : NULL
;
1745 unsigned int line
= 1;
1746 unsigned int column
= 0;
1747 unsigned int discriminator
= 0;
1748 int is_stmt
= lh
.default_is_stmt
;
1749 int end_sequence
= 0;
1750 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1751 compilers generate address sequences that are wildly out of
1752 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1753 for ia64-Linux). Thus, to determine the low and high
1754 address, we must compare on every DW_LNS_copy, etc. */
1755 bfd_vma low_pc
= (bfd_vma
) -1;
1756 bfd_vma high_pc
= 0;
1758 /* Decode the table. */
1759 while (! end_sequence
)
1761 op_code
= read_1_byte (abfd
, line_ptr
);
1764 if (op_code
>= lh
.opcode_base
)
1766 /* Special operand. */
1767 adj_opcode
= op_code
- lh
.opcode_base
;
1768 if (lh
.maximum_ops_per_insn
== 1)
1769 address
+= (adj_opcode
/ lh
.line_range
1770 * lh
.minimum_instruction_length
);
1773 address
+= ((op_index
+ adj_opcode
/ lh
.line_range
)
1774 / lh
.maximum_ops_per_insn
1775 * lh
.minimum_instruction_length
);
1776 op_index
= ((op_index
+ adj_opcode
/ lh
.line_range
)
1777 % lh
.maximum_ops_per_insn
);
1779 line
+= lh
.line_base
+ (adj_opcode
% lh
.line_range
);
1780 /* Append row to matrix using current values. */
1781 if (!add_line_info (table
, address
, op_index
, filename
,
1782 line
, column
, discriminator
, 0))
1785 if (address
< low_pc
)
1787 if (address
> high_pc
)
1790 else switch (op_code
)
1792 case DW_LNS_extended_op
:
1793 exop_len
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1794 line_ptr
+= bytes_read
;
1795 extended_op
= read_1_byte (abfd
, line_ptr
);
1798 switch (extended_op
)
1800 case DW_LNE_end_sequence
:
1802 if (!add_line_info (table
, address
, op_index
, filename
, line
,
1803 column
, discriminator
, end_sequence
))
1806 if (address
< low_pc
)
1808 if (address
> high_pc
)
1810 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
1813 case DW_LNE_set_address
:
1814 address
= read_address (unit
, line_ptr
);
1816 line_ptr
+= unit
->addr_size
;
1818 case DW_LNE_define_file
:
1819 cur_file
= read_string (abfd
, line_ptr
, &bytes_read
);
1820 line_ptr
+= bytes_read
;
1821 if ((table
->num_files
% FILE_ALLOC_CHUNK
) == 0)
1823 struct fileinfo
*tmp
;
1825 amt
= table
->num_files
+ FILE_ALLOC_CHUNK
;
1826 amt
*= sizeof (struct fileinfo
);
1827 tmp
= (struct fileinfo
*) bfd_realloc (table
->files
, amt
);
1832 table
->files
[table
->num_files
].name
= cur_file
;
1833 table
->files
[table
->num_files
].dir
=
1834 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1835 line_ptr
+= bytes_read
;
1836 table
->files
[table
->num_files
].time
=
1837 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1838 line_ptr
+= bytes_read
;
1839 table
->files
[table
->num_files
].size
=
1840 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1841 line_ptr
+= bytes_read
;
1844 case DW_LNE_set_discriminator
:
1846 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1847 line_ptr
+= bytes_read
;
1849 case DW_LNE_HP_source_file_correlation
:
1850 line_ptr
+= exop_len
- 1;
1853 (*_bfd_error_handler
)
1854 (_("Dwarf Error: mangled line number section."));
1855 bfd_set_error (bfd_error_bad_value
);
1857 if (filename
!= NULL
)
1863 if (!add_line_info (table
, address
, op_index
,
1864 filename
, line
, column
, discriminator
, 0))
1867 if (address
< low_pc
)
1869 if (address
> high_pc
)
1872 case DW_LNS_advance_pc
:
1873 if (lh
.maximum_ops_per_insn
== 1)
1874 address
+= (lh
.minimum_instruction_length
1875 * read_unsigned_leb128 (abfd
, line_ptr
,
1879 bfd_vma adjust
= read_unsigned_leb128 (abfd
, line_ptr
,
1881 address
= ((op_index
+ adjust
) / lh
.maximum_ops_per_insn
1882 * lh
.minimum_instruction_length
);
1883 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
1885 line_ptr
+= bytes_read
;
1887 case DW_LNS_advance_line
:
1888 line
+= read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
1889 line_ptr
+= bytes_read
;
1891 case DW_LNS_set_file
:
1895 /* The file and directory tables are 0
1896 based, the references are 1 based. */
1897 file
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1898 line_ptr
+= bytes_read
;
1901 filename
= concat_filename (table
, file
);
1904 case DW_LNS_set_column
:
1905 column
= read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1906 line_ptr
+= bytes_read
;
1908 case DW_LNS_negate_stmt
:
1909 is_stmt
= (!is_stmt
);
1911 case DW_LNS_set_basic_block
:
1913 case DW_LNS_const_add_pc
:
1914 if (lh
.maximum_ops_per_insn
== 1)
1915 address
+= (lh
.minimum_instruction_length
1916 * ((255 - lh
.opcode_base
) / lh
.line_range
));
1919 bfd_vma adjust
= ((255 - lh
.opcode_base
) / lh
.line_range
);
1920 address
+= (lh
.minimum_instruction_length
1921 * ((op_index
+ adjust
)
1922 / lh
.maximum_ops_per_insn
));
1923 op_index
= (op_index
+ adjust
) % lh
.maximum_ops_per_insn
;
1926 case DW_LNS_fixed_advance_pc
:
1927 address
+= read_2_bytes (abfd
, line_ptr
);
1932 /* Unknown standard opcode, ignore it. */
1933 for (i
= 0; i
< lh
.standard_opcode_lengths
[op_code
]; i
++)
1935 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
1936 line_ptr
+= bytes_read
;
1946 if (sort_line_sequences (table
))
1950 if (table
->sequences
!= NULL
)
1951 free (table
->sequences
);
1952 if (table
->files
!= NULL
)
1953 free (table
->files
);
1954 if (table
->dirs
!= NULL
)
1959 /* If ADDR is within TABLE set the output parameters and return the
1960 range of addresses covered by the entry used to fill them out.
1961 Otherwise set * FILENAME_PTR to NULL and return 0.
1962 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
1963 are pointers to the objects to be filled in. */
1966 lookup_address_in_line_info_table (struct line_info_table
*table
,
1968 const char **filename_ptr
,
1969 unsigned int *linenumber_ptr
,
1970 unsigned int *discriminator_ptr
)
1972 struct line_sequence
*seq
= NULL
;
1973 struct line_info
*each_line
;
1976 /* Binary search the array of sequences. */
1978 high
= table
->num_sequences
;
1981 mid
= (low
+ high
) / 2;
1982 seq
= &table
->sequences
[mid
];
1983 if (addr
< seq
->low_pc
)
1985 else if (addr
>= seq
->last_line
->address
)
1991 if (seq
&& addr
>= seq
->low_pc
&& addr
< seq
->last_line
->address
)
1993 /* Note: seq->last_line should be a descendingly sorted list. */
1994 for (each_line
= seq
->last_line
;
1996 each_line
= each_line
->prev_line
)
1997 if (addr
>= each_line
->address
)
2001 && !(each_line
->end_sequence
|| each_line
== seq
->last_line
))
2003 *filename_ptr
= each_line
->filename
;
2004 *linenumber_ptr
= each_line
->line
;
2005 if (discriminator_ptr
)
2006 *discriminator_ptr
= each_line
->discriminator
;
2007 return seq
->last_line
->address
- seq
->low_pc
;
2011 *filename_ptr
= NULL
;
2015 /* Read in the .debug_ranges section for future reference. */
2018 read_debug_ranges (struct comp_unit
*unit
)
2020 struct dwarf2_debug
*stash
= unit
->stash
;
2021 return read_section (unit
->abfd
, &stash
->debug_sections
[debug_ranges
],
2023 &stash
->dwarf_ranges_buffer
, &stash
->dwarf_ranges_size
);
2026 /* Function table functions. */
2028 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
2029 TRUE. Note that we need to find the function that has the smallest range
2030 that contains ADDR, to handle inlined functions without depending upon
2031 them being ordered in TABLE by increasing range. */
2034 lookup_address_in_function_table (struct comp_unit
*unit
,
2036 struct funcinfo
**function_ptr
)
2038 struct funcinfo
* each_func
;
2039 struct funcinfo
* best_fit
= NULL
;
2040 bfd_vma best_fit_len
= 0;
2041 struct arange
*arange
;
2043 for (each_func
= unit
->function_table
;
2045 each_func
= each_func
->prev_func
)
2047 for (arange
= &each_func
->arange
;
2049 arange
= arange
->next
)
2051 if (addr
>= arange
->low
&& addr
< arange
->high
)
2054 || arange
->high
- arange
->low
< best_fit_len
)
2056 best_fit
= each_func
;
2057 best_fit_len
= arange
->high
- arange
->low
;
2065 *function_ptr
= best_fit
;
2071 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2072 and LINENUMBER_PTR, and return TRUE. */
2075 lookup_symbol_in_function_table (struct comp_unit
*unit
,
2078 const char **filename_ptr
,
2079 unsigned int *linenumber_ptr
)
2081 struct funcinfo
* each_func
;
2082 struct funcinfo
* best_fit
= NULL
;
2083 bfd_vma best_fit_len
= 0;
2084 struct arange
*arange
;
2085 const char *name
= bfd_asymbol_name (sym
);
2086 asection
*sec
= bfd_get_section (sym
);
2088 for (each_func
= unit
->function_table
;
2090 each_func
= each_func
->prev_func
)
2092 for (arange
= &each_func
->arange
;
2094 arange
= arange
->next
)
2096 if ((!each_func
->sec
|| each_func
->sec
== sec
)
2097 && addr
>= arange
->low
2098 && addr
< arange
->high
2100 && strcmp (name
, each_func
->name
) == 0
2102 || arange
->high
- arange
->low
< best_fit_len
))
2104 best_fit
= each_func
;
2105 best_fit_len
= arange
->high
- arange
->low
;
2112 best_fit
->sec
= sec
;
2113 *filename_ptr
= best_fit
->file
;
2114 *linenumber_ptr
= best_fit
->line
;
2121 /* Variable table functions. */
2123 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
2124 LINENUMBER_PTR, and return TRUE. */
2127 lookup_symbol_in_variable_table (struct comp_unit
*unit
,
2130 const char **filename_ptr
,
2131 unsigned int *linenumber_ptr
)
2133 const char *name
= bfd_asymbol_name (sym
);
2134 asection
*sec
= bfd_get_section (sym
);
2135 struct varinfo
* each
;
2137 for (each
= unit
->variable_table
; each
; each
= each
->prev_var
)
2138 if (each
->stack
== 0
2139 && each
->file
!= NULL
2140 && each
->name
!= NULL
2141 && each
->addr
== addr
2142 && (!each
->sec
|| each
->sec
== sec
)
2143 && strcmp (name
, each
->name
) == 0)
2149 *filename_ptr
= each
->file
;
2150 *linenumber_ptr
= each
->line
;
2158 find_abstract_instance_name (struct comp_unit
*unit
,
2159 struct attribute
*attr_ptr
,
2160 bfd_boolean
*is_linkage
)
2162 bfd
*abfd
= unit
->abfd
;
2164 unsigned int abbrev_number
, bytes_read
, i
;
2165 struct abbrev_info
*abbrev
;
2166 bfd_uint64_t die_ref
= attr_ptr
->u
.val
;
2167 struct attribute attr
;
2170 /* DW_FORM_ref_addr can reference an entry in a different CU. It
2171 is an offset from the .debug_info section, not the current CU. */
2172 if (attr_ptr
->form
== DW_FORM_ref_addr
)
2174 /* We only support DW_FORM_ref_addr within the same file, so
2175 any relocations should be resolved already. */
2179 info_ptr
= unit
->sec_info_ptr
+ die_ref
;
2181 /* Now find the CU containing this pointer. */
2182 if (info_ptr
>= unit
->info_ptr_unit
&& info_ptr
< unit
->end_ptr
)
2186 /* Check other CUs to see if they contain the abbrev. */
2187 struct comp_unit
* u
;
2189 for (u
= unit
->prev_unit
; u
!= NULL
; u
= u
->prev_unit
)
2190 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
2194 for (u
= unit
->next_unit
; u
!= NULL
; u
= u
->next_unit
)
2195 if (info_ptr
>= u
->info_ptr_unit
&& info_ptr
< u
->end_ptr
)
2200 /* else FIXME: What do we do now ? */
2203 else if (attr_ptr
->form
== DW_FORM_GNU_ref_alt
)
2205 info_ptr
= read_alt_indirect_ref (unit
, die_ref
);
2206 if (info_ptr
== NULL
)
2208 (*_bfd_error_handler
)
2209 (_("Dwarf Error: Unable to read alt ref %u."), die_ref
);
2210 bfd_set_error (bfd_error_bad_value
);
2213 /* FIXME: Do we need to locate the correct CU, in a similar
2214 fashion to the code in the DW_FORM_ref_addr case above ? */
2217 info_ptr
= unit
->info_ptr_unit
+ die_ref
;
2219 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
2220 info_ptr
+= bytes_read
;
2224 abbrev
= lookup_abbrev (abbrev_number
, unit
->abbrevs
);
2227 (*_bfd_error_handler
)
2228 (_("Dwarf Error: Could not find abbrev number %u."), abbrev_number
);
2229 bfd_set_error (bfd_error_bad_value
);
2233 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2235 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
,
2237 if (info_ptr
== NULL
)
2242 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2244 if (name
== NULL
&& is_str_attr (attr
.form
))
2247 if (non_mangled (unit
->lang
))
2251 case DW_AT_specification
:
2252 name
= find_abstract_instance_name (unit
, &attr
, is_linkage
);
2254 case DW_AT_linkage_name
:
2255 case DW_AT_MIPS_linkage_name
:
2256 /* PR 16949: Corrupt debug info can place
2257 non-string forms into these attributes. */
2258 if (is_str_attr (attr
.form
))
2274 read_rangelist (struct comp_unit
*unit
, struct arange
*arange
,
2275 bfd_uint64_t offset
)
2277 bfd_byte
*ranges_ptr
;
2278 bfd_vma base_address
= unit
->base_address
;
2280 if (! unit
->stash
->dwarf_ranges_buffer
)
2282 if (! read_debug_ranges (unit
))
2285 ranges_ptr
= unit
->stash
->dwarf_ranges_buffer
+ offset
;
2292 low_pc
= read_address (unit
, ranges_ptr
);
2293 ranges_ptr
+= unit
->addr_size
;
2294 high_pc
= read_address (unit
, ranges_ptr
);
2295 ranges_ptr
+= unit
->addr_size
;
2297 if (low_pc
== 0 && high_pc
== 0)
2299 if (low_pc
== -1UL && high_pc
!= -1UL)
2300 base_address
= high_pc
;
2303 if (!arange_add (unit
, arange
,
2304 base_address
+ low_pc
, base_address
+ high_pc
))
2311 /* DWARF2 Compilation unit functions. */
2313 /* Scan over each die in a comp. unit looking for functions to add
2314 to the function table and variables to the variable table. */
2317 scan_unit_for_symbols (struct comp_unit
*unit
)
2319 bfd
*abfd
= unit
->abfd
;
2320 bfd_byte
*info_ptr
= unit
->first_child_die_ptr
;
2321 int nesting_level
= 1;
2322 struct funcinfo
**nested_funcs
;
2323 int nested_funcs_size
;
2325 /* Maintain a stack of in-scope functions and inlined functions, which we
2326 can use to set the caller_func field. */
2327 nested_funcs_size
= 32;
2328 nested_funcs
= (struct funcinfo
**)
2329 bfd_malloc (nested_funcs_size
* sizeof (struct funcinfo
*));
2330 if (nested_funcs
== NULL
)
2332 nested_funcs
[nesting_level
] = 0;
2334 while (nesting_level
)
2336 unsigned int abbrev_number
, bytes_read
, i
;
2337 struct abbrev_info
*abbrev
;
2338 struct attribute attr
;
2339 struct funcinfo
*func
;
2340 struct varinfo
*var
;
2342 bfd_vma high_pc
= 0;
2343 bfd_boolean high_pc_relative
= FALSE
;
2345 /* PR 17512: file: 9f405d9d. */
2346 if (info_ptr
>= unit
->stash
->info_ptr_end
)
2349 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
2350 info_ptr
+= bytes_read
;
2352 if (! abbrev_number
)
2358 abbrev
= lookup_abbrev (abbrev_number
,unit
->abbrevs
);
2361 (*_bfd_error_handler
)
2362 (_("Dwarf Error: Could not find abbrev number %u."),
2364 bfd_set_error (bfd_error_bad_value
);
2369 if (abbrev
->tag
== DW_TAG_subprogram
2370 || abbrev
->tag
== DW_TAG_entry_point
2371 || abbrev
->tag
== DW_TAG_inlined_subroutine
)
2373 bfd_size_type amt
= sizeof (struct funcinfo
);
2374 func
= (struct funcinfo
*) bfd_zalloc (abfd
, amt
);
2377 func
->tag
= abbrev
->tag
;
2378 func
->prev_func
= unit
->function_table
;
2379 unit
->function_table
= func
;
2380 BFD_ASSERT (!unit
->cached
);
2382 if (func
->tag
== DW_TAG_inlined_subroutine
)
2383 for (i
= nesting_level
- 1; i
>= 1; i
--)
2384 if (nested_funcs
[i
])
2386 func
->caller_func
= nested_funcs
[i
];
2389 nested_funcs
[nesting_level
] = func
;
2394 if (abbrev
->tag
== DW_TAG_variable
)
2396 bfd_size_type amt
= sizeof (struct varinfo
);
2397 var
= (struct varinfo
*) bfd_zalloc (abfd
, amt
);
2400 var
->tag
= abbrev
->tag
;
2402 var
->prev_var
= unit
->variable_table
;
2403 unit
->variable_table
= var
;
2404 BFD_ASSERT (!unit
->cached
);
2407 /* No inline function in scope at this nesting level. */
2408 nested_funcs
[nesting_level
] = 0;
2411 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2413 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
2414 if (info_ptr
== NULL
)
2421 case DW_AT_call_file
:
2422 func
->caller_file
= concat_filename (unit
->line_table
,
2426 case DW_AT_call_line
:
2427 func
->caller_line
= attr
.u
.val
;
2430 case DW_AT_abstract_origin
:
2431 case DW_AT_specification
:
2432 func
->name
= find_abstract_instance_name (unit
, &attr
,
2437 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2439 if (func
->name
== NULL
&& is_str_attr (attr
.form
))
2441 func
->name
= attr
.u
.str
;
2442 if (non_mangled (unit
->lang
))
2443 func
->is_linkage
= TRUE
;
2447 case DW_AT_linkage_name
:
2448 case DW_AT_MIPS_linkage_name
:
2449 /* PR 16949: Corrupt debug info can place
2450 non-string forms into these attributes. */
2451 if (is_str_attr (attr
.form
))
2453 func
->name
= attr
.u
.str
;
2454 func
->is_linkage
= TRUE
;
2459 low_pc
= attr
.u
.val
;
2463 high_pc
= attr
.u
.val
;
2464 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
2468 if (!read_rangelist (unit
, &func
->arange
, attr
.u
.val
))
2472 case DW_AT_decl_file
:
2473 func
->file
= concat_filename (unit
->line_table
,
2477 case DW_AT_decl_line
:
2478 func
->line
= attr
.u
.val
;
2490 var
->name
= attr
.u
.str
;
2493 case DW_AT_decl_file
:
2494 var
->file
= concat_filename (unit
->line_table
,
2498 case DW_AT_decl_line
:
2499 var
->line
= attr
.u
.val
;
2502 case DW_AT_external
:
2503 if (attr
.u
.val
!= 0)
2507 case DW_AT_location
:
2511 case DW_FORM_block1
:
2512 case DW_FORM_block2
:
2513 case DW_FORM_block4
:
2514 case DW_FORM_exprloc
:
2515 if (*attr
.u
.blk
->data
== DW_OP_addr
)
2519 /* Verify that DW_OP_addr is the only opcode in the
2520 location, in which case the block size will be 1
2521 plus the address size. */
2522 /* ??? For TLS variables, gcc can emit
2523 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
2524 which we don't handle here yet. */
2525 if (attr
.u
.blk
->size
== unit
->addr_size
+ 1U)
2526 var
->addr
= bfd_get (unit
->addr_size
* 8,
2528 attr
.u
.blk
->data
+ 1);
2543 if (high_pc_relative
)
2546 if (func
&& high_pc
!= 0)
2548 if (!arange_add (unit
, &func
->arange
, low_pc
, high_pc
))
2552 if (abbrev
->has_children
)
2556 if (nesting_level
>= nested_funcs_size
)
2558 struct funcinfo
**tmp
;
2560 nested_funcs_size
*= 2;
2561 tmp
= (struct funcinfo
**)
2562 bfd_realloc (nested_funcs
,
2563 nested_funcs_size
* sizeof (struct funcinfo
*));
2568 nested_funcs
[nesting_level
] = 0;
2572 free (nested_funcs
);
2576 free (nested_funcs
);
2580 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
2581 includes the compilation unit header that proceeds the DIE's, but
2582 does not include the length field that precedes each compilation
2583 unit header. END_PTR points one past the end of this comp unit.
2584 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
2586 This routine does not read the whole compilation unit; only enough
2587 to get to the line number information for the compilation unit. */
2589 static struct comp_unit
*
2590 parse_comp_unit (struct dwarf2_debug
*stash
,
2591 bfd_vma unit_length
,
2592 bfd_byte
*info_ptr_unit
,
2593 unsigned int offset_size
)
2595 struct comp_unit
* unit
;
2596 unsigned int version
;
2597 bfd_uint64_t abbrev_offset
= 0;
2598 unsigned int addr_size
;
2599 struct abbrev_info
** abbrevs
;
2600 unsigned int abbrev_number
, bytes_read
, i
;
2601 struct abbrev_info
*abbrev
;
2602 struct attribute attr
;
2603 bfd_byte
*info_ptr
= stash
->info_ptr
;
2604 bfd_byte
*end_ptr
= info_ptr
+ unit_length
;
2607 bfd_vma high_pc
= 0;
2608 bfd
*abfd
= stash
->bfd_ptr
;
2609 bfd_boolean high_pc_relative
= FALSE
;
2611 version
= read_2_bytes (abfd
, info_ptr
);
2613 BFD_ASSERT (offset_size
== 4 || offset_size
== 8);
2614 if (offset_size
== 4)
2615 abbrev_offset
= read_4_bytes (abfd
, info_ptr
);
2617 abbrev_offset
= read_8_bytes (abfd
, info_ptr
);
2618 info_ptr
+= offset_size
;
2619 addr_size
= read_1_byte (abfd
, info_ptr
);
2622 if (version
!= 2 && version
!= 3 && version
!= 4)
2624 (*_bfd_error_handler
)
2625 (_("Dwarf Error: found dwarf version '%u', this reader"
2626 " only handles version 2, 3 and 4 information."), version
);
2627 bfd_set_error (bfd_error_bad_value
);
2631 if (addr_size
> sizeof (bfd_vma
))
2633 (*_bfd_error_handler
)
2634 (_("Dwarf Error: found address size '%u', this reader"
2635 " can not handle sizes greater than '%u'."),
2637 (unsigned int) sizeof (bfd_vma
));
2638 bfd_set_error (bfd_error_bad_value
);
2642 if (addr_size
!= 2 && addr_size
!= 4 && addr_size
!= 8)
2644 (*_bfd_error_handler
)
2645 ("Dwarf Error: found address size '%u', this reader"
2646 " can only handle address sizes '2', '4' and '8'.", addr_size
);
2647 bfd_set_error (bfd_error_bad_value
);
2651 /* Read the abbrevs for this compilation unit into a table. */
2652 abbrevs
= read_abbrevs (abfd
, abbrev_offset
, stash
);
2656 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
2657 info_ptr
+= bytes_read
;
2658 if (! abbrev_number
)
2660 (*_bfd_error_handler
) (_("Dwarf Error: Bad abbrev number: %u."),
2662 bfd_set_error (bfd_error_bad_value
);
2666 abbrev
= lookup_abbrev (abbrev_number
, abbrevs
);
2669 (*_bfd_error_handler
) (_("Dwarf Error: Could not find abbrev number %u."),
2671 bfd_set_error (bfd_error_bad_value
);
2675 amt
= sizeof (struct comp_unit
);
2676 unit
= (struct comp_unit
*) bfd_zalloc (abfd
, amt
);
2680 unit
->version
= version
;
2681 unit
->addr_size
= addr_size
;
2682 unit
->offset_size
= offset_size
;
2683 unit
->abbrevs
= abbrevs
;
2684 unit
->end_ptr
= end_ptr
;
2685 unit
->stash
= stash
;
2686 unit
->info_ptr_unit
= info_ptr_unit
;
2687 unit
->sec_info_ptr
= stash
->sec_info_ptr
;
2689 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2691 info_ptr
= read_attribute (&attr
, &abbrev
->attrs
[i
], unit
, info_ptr
);
2692 if (info_ptr
== NULL
)
2695 /* Store the data if it is of an attribute we want to keep in a
2696 partial symbol table. */
2699 case DW_AT_stmt_list
:
2701 unit
->line_offset
= attr
.u
.val
;
2705 unit
->name
= attr
.u
.str
;
2709 low_pc
= attr
.u
.val
;
2710 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2711 this is the base address to use when reading location
2712 lists or range lists. */
2713 if (abbrev
->tag
== DW_TAG_compile_unit
)
2714 unit
->base_address
= low_pc
;
2718 high_pc
= attr
.u
.val
;
2719 high_pc_relative
= attr
.form
!= DW_FORM_addr
;
2723 if (!read_rangelist (unit
, &unit
->arange
, attr
.u
.val
))
2727 case DW_AT_comp_dir
:
2729 char *comp_dir
= attr
.u
.str
;
2731 /* PR 17512: file: 1fe726be. */
2732 if (! is_str_attr (attr
.form
))
2734 (*_bfd_error_handler
)
2735 (_("Dwarf Error: DW_AT_comp_dir attribute encountered with a non-string form."));
2741 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2742 directory, get rid of it. */
2743 char *cp
= strchr (comp_dir
, ':');
2745 if (cp
&& cp
!= comp_dir
&& cp
[-1] == '.' && cp
[1] == '/')
2748 unit
->comp_dir
= comp_dir
;
2752 case DW_AT_language
:
2753 unit
->lang
= attr
.u
.val
;
2760 if (high_pc_relative
)
2764 if (!arange_add (unit
, &unit
->arange
, low_pc
, high_pc
))
2768 unit
->first_child_die_ptr
= info_ptr
;
2772 /* Return TRUE if UNIT may contain the address given by ADDR. When
2773 there are functions written entirely with inline asm statements, the
2774 range info in the compilation unit header may not be correct. We
2775 need to consult the line info table to see if a compilation unit
2776 really contains the given address. */
2779 comp_unit_contains_address (struct comp_unit
*unit
, bfd_vma addr
)
2781 struct arange
*arange
;
2786 arange
= &unit
->arange
;
2789 if (addr
>= arange
->low
&& addr
< arange
->high
)
2791 arange
= arange
->next
;
2798 /* If UNIT contains ADDR, set the output parameters to the values for
2799 the line containing ADDR. The output parameters, FILENAME_PTR,
2800 FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects
2803 Returns the range of addresses covered by the entry that was used
2804 to fill in *LINENUMBER_PTR or 0 if it was not filled in. */
2807 comp_unit_find_nearest_line (struct comp_unit
*unit
,
2809 const char **filename_ptr
,
2810 struct funcinfo
**function_ptr
,
2811 unsigned int *linenumber_ptr
,
2812 unsigned int *discriminator_ptr
,
2813 struct dwarf2_debug
*stash
)
2820 if (! unit
->line_table
)
2822 if (! unit
->stmtlist
)
2828 unit
->line_table
= decode_line_info (unit
, stash
);
2830 if (! unit
->line_table
)
2836 if (unit
->first_child_die_ptr
< unit
->end_ptr
2837 && ! scan_unit_for_symbols (unit
))
2844 *function_ptr
= NULL
;
2845 func_p
= lookup_address_in_function_table (unit
, addr
, function_ptr
);
2846 if (func_p
&& (*function_ptr
)->tag
== DW_TAG_inlined_subroutine
)
2847 stash
->inliner_chain
= *function_ptr
;
2849 return lookup_address_in_line_info_table (unit
->line_table
, addr
,
2855 /* Check to see if line info is already decoded in a comp_unit.
2856 If not, decode it. Returns TRUE if no errors were encountered;
2860 comp_unit_maybe_decode_line_info (struct comp_unit
*unit
,
2861 struct dwarf2_debug
*stash
)
2866 if (! unit
->line_table
)
2868 if (! unit
->stmtlist
)
2874 unit
->line_table
= decode_line_info (unit
, stash
);
2876 if (! unit
->line_table
)
2882 if (unit
->first_child_die_ptr
< unit
->end_ptr
2883 && ! scan_unit_for_symbols (unit
))
2893 /* If UNIT contains SYM at ADDR, set the output parameters to the
2894 values for the line containing SYM. The output parameters,
2895 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2898 Return TRUE if UNIT contains SYM, and no errors were encountered;
2902 comp_unit_find_line (struct comp_unit
*unit
,
2905 const char **filename_ptr
,
2906 unsigned int *linenumber_ptr
,
2907 struct dwarf2_debug
*stash
)
2909 if (!comp_unit_maybe_decode_line_info (unit
, stash
))
2912 if (sym
->flags
& BSF_FUNCTION
)
2913 return lookup_symbol_in_function_table (unit
, sym
, addr
,
2917 return lookup_symbol_in_variable_table (unit
, sym
, addr
,
2922 static struct funcinfo
*
2923 reverse_funcinfo_list (struct funcinfo
*head
)
2925 struct funcinfo
*rhead
;
2926 struct funcinfo
*temp
;
2928 for (rhead
= NULL
; head
; head
= temp
)
2930 temp
= head
->prev_func
;
2931 head
->prev_func
= rhead
;
2937 static struct varinfo
*
2938 reverse_varinfo_list (struct varinfo
*head
)
2940 struct varinfo
*rhead
;
2941 struct varinfo
*temp
;
2943 for (rhead
= NULL
; head
; head
= temp
)
2945 temp
= head
->prev_var
;
2946 head
->prev_var
= rhead
;
2952 /* Extract all interesting funcinfos and varinfos of a compilation
2953 unit into hash tables for faster lookup. Returns TRUE if no
2954 errors were enountered; FALSE otherwise. */
2957 comp_unit_hash_info (struct dwarf2_debug
*stash
,
2958 struct comp_unit
*unit
,
2959 struct info_hash_table
*funcinfo_hash_table
,
2960 struct info_hash_table
*varinfo_hash_table
)
2962 struct funcinfo
* each_func
;
2963 struct varinfo
* each_var
;
2964 bfd_boolean okay
= TRUE
;
2966 BFD_ASSERT (stash
->info_hash_status
!= STASH_INFO_HASH_DISABLED
);
2968 if (!comp_unit_maybe_decode_line_info (unit
, stash
))
2971 BFD_ASSERT (!unit
->cached
);
2973 /* To preserve the original search order, we went to visit the function
2974 infos in the reversed order of the list. However, making the list
2975 bi-directional use quite a bit of extra memory. So we reverse
2976 the list first, traverse the list in the now reversed order and
2977 finally reverse the list again to get back the original order. */
2978 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
2979 for (each_func
= unit
->function_table
;
2981 each_func
= each_func
->prev_func
)
2983 /* Skip nameless functions. */
2984 if (each_func
->name
)
2985 /* There is no need to copy name string into hash table as
2986 name string is either in the dwarf string buffer or
2987 info in the stash. */
2988 okay
= insert_info_hash_table (funcinfo_hash_table
, each_func
->name
,
2989 (void*) each_func
, FALSE
);
2991 unit
->function_table
= reverse_funcinfo_list (unit
->function_table
);
2995 /* We do the same for variable infos. */
2996 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
2997 for (each_var
= unit
->variable_table
;
2999 each_var
= each_var
->prev_var
)
3001 /* Skip stack vars and vars with no files or names. */
3002 if (each_var
->stack
== 0
3003 && each_var
->file
!= NULL
3004 && each_var
->name
!= NULL
)
3005 /* There is no need to copy name string into hash table as
3006 name string is either in the dwarf string buffer or
3007 info in the stash. */
3008 okay
= insert_info_hash_table (varinfo_hash_table
, each_var
->name
,
3009 (void*) each_var
, FALSE
);
3012 unit
->variable_table
= reverse_varinfo_list (unit
->variable_table
);
3013 unit
->cached
= TRUE
;
3017 /* Locate a section in a BFD containing debugging info. The search starts
3018 from the section after AFTER_SEC, or from the first section in the BFD if
3019 AFTER_SEC is NULL. The search works by examining the names of the
3020 sections. There are three permissiable names. The first two are given
3021 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
3022 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
3023 This is a variation on the .debug_info section which has a checksum
3024 describing the contents appended onto the name. This allows the linker to
3025 identify and discard duplicate debugging sections for different
3026 compilation units. */
3027 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
3030 find_debug_info (bfd
*abfd
, const struct dwarf_debug_section
*debug_sections
,
3031 asection
*after_sec
)
3036 if (after_sec
== NULL
)
3038 look
= debug_sections
[debug_info
].uncompressed_name
;
3039 msec
= bfd_get_section_by_name (abfd
, look
);
3043 look
= debug_sections
[debug_info
].compressed_name
;
3046 msec
= bfd_get_section_by_name (abfd
, look
);
3051 for (msec
= abfd
->sections
; msec
!= NULL
; msec
= msec
->next
)
3052 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
3058 for (msec
= after_sec
->next
; msec
!= NULL
; msec
= msec
->next
)
3060 look
= debug_sections
[debug_info
].uncompressed_name
;
3061 if (strcmp (msec
->name
, look
) == 0)
3064 look
= debug_sections
[debug_info
].compressed_name
;
3065 if (look
!= NULL
&& strcmp (msec
->name
, look
) == 0)
3068 if (CONST_STRNEQ (msec
->name
, GNU_LINKONCE_INFO
))
3075 /* Transfer VMAs from object file to separate debug file. */
3078 set_debug_vma (bfd
*orig_bfd
, bfd
*debug_bfd
)
3082 for (s
= orig_bfd
->sections
, d
= debug_bfd
->sections
;
3083 s
!= NULL
&& d
!= NULL
;
3084 s
= s
->next
, d
= d
->next
)
3086 if ((d
->flags
& SEC_DEBUGGING
) != 0)
3088 /* ??? Assumes 1-1 correspondence between sections in the
3090 if (strcmp (s
->name
, d
->name
) == 0)
3092 d
->output_section
= s
->output_section
;
3093 d
->output_offset
= s
->output_offset
;
3099 /* Unset vmas for adjusted sections in STASH. */
3102 unset_sections (struct dwarf2_debug
*stash
)
3105 struct adjusted_section
*p
;
3107 i
= stash
->adjusted_section_count
;
3108 p
= stash
->adjusted_sections
;
3109 for (; i
> 0; i
--, p
++)
3110 p
->section
->vma
= 0;
3113 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
3114 relocatable object file. VMAs are normally all zero in relocatable
3115 object files, so if we want to distinguish locations in sections by
3116 address we need to set VMAs so the sections do not overlap. We
3117 also set VMA on .debug_info so that when we have multiple
3118 .debug_info sections (or the linkonce variant) they also do not
3119 overlap. The multiple .debug_info sections make up a single
3120 logical section. ??? We should probably do the same for other
3124 place_sections (bfd
*orig_bfd
, struct dwarf2_debug
*stash
)
3127 struct adjusted_section
*p
;
3129 const char *debug_info_name
;
3131 if (stash
->adjusted_section_count
!= 0)
3133 i
= stash
->adjusted_section_count
;
3134 p
= stash
->adjusted_sections
;
3135 for (; i
> 0; i
--, p
++)
3136 p
->section
->vma
= p
->adj_vma
;
3140 debug_info_name
= stash
->debug_sections
[debug_info
].uncompressed_name
;
3147 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
3151 if ((sect
->output_section
!= NULL
3152 && sect
->output_section
!= sect
3153 && (sect
->flags
& SEC_DEBUGGING
) == 0)
3157 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
3158 || CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
));
3160 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
3166 if (abfd
== stash
->bfd_ptr
)
3168 abfd
= stash
->bfd_ptr
;
3172 stash
->adjusted_section_count
= -1;
3175 bfd_vma last_vma
= 0, last_dwarf
= 0;
3176 bfd_size_type amt
= i
* sizeof (struct adjusted_section
);
3178 p
= (struct adjusted_section
*) bfd_malloc (amt
);
3182 stash
->adjusted_sections
= p
;
3183 stash
->adjusted_section_count
= i
;
3190 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
3195 if ((sect
->output_section
!= NULL
3196 && sect
->output_section
!= sect
3197 && (sect
->flags
& SEC_DEBUGGING
) == 0)
3201 is_debug_info
= (strcmp (sect
->name
, debug_info_name
) == 0
3202 || CONST_STRNEQ (sect
->name
, GNU_LINKONCE_INFO
));
3204 if (!((sect
->flags
& SEC_ALLOC
) != 0 && abfd
== orig_bfd
)
3208 sz
= sect
->rawsize
? sect
->rawsize
: sect
->size
;
3212 BFD_ASSERT (sect
->alignment_power
== 0);
3213 sect
->vma
= last_dwarf
;
3218 /* Align the new address to the current section
3220 last_vma
= ((last_vma
3221 + ~((bfd_vma
) -1 << sect
->alignment_power
))
3222 & ((bfd_vma
) -1 << sect
->alignment_power
));
3223 sect
->vma
= last_vma
;
3228 p
->adj_vma
= sect
->vma
;
3231 if (abfd
== stash
->bfd_ptr
)
3233 abfd
= stash
->bfd_ptr
;
3237 if (orig_bfd
!= stash
->bfd_ptr
)
3238 set_debug_vma (orig_bfd
, stash
->bfd_ptr
);
3243 /* Look up a funcinfo by name using the given info hash table. If found,
3244 also update the locations pointed to by filename_ptr and linenumber_ptr.
3246 This function returns TRUE if a funcinfo that matches the given symbol
3247 and address is found with any error; otherwise it returns FALSE. */
3250 info_hash_lookup_funcinfo (struct info_hash_table
*hash_table
,
3253 const char **filename_ptr
,
3254 unsigned int *linenumber_ptr
)
3256 struct funcinfo
* each_func
;
3257 struct funcinfo
* best_fit
= NULL
;
3258 bfd_vma best_fit_len
= 0;
3259 struct info_list_node
*node
;
3260 struct arange
*arange
;
3261 const char *name
= bfd_asymbol_name (sym
);
3262 asection
*sec
= bfd_get_section (sym
);
3264 for (node
= lookup_info_hash_table (hash_table
, name
);
3268 each_func
= (struct funcinfo
*) node
->info
;
3269 for (arange
= &each_func
->arange
;
3271 arange
= arange
->next
)
3273 if ((!each_func
->sec
|| each_func
->sec
== sec
)
3274 && addr
>= arange
->low
3275 && addr
< arange
->high
3277 || arange
->high
- arange
->low
< best_fit_len
))
3279 best_fit
= each_func
;
3280 best_fit_len
= arange
->high
- arange
->low
;
3287 best_fit
->sec
= sec
;
3288 *filename_ptr
= best_fit
->file
;
3289 *linenumber_ptr
= best_fit
->line
;
3296 /* Look up a varinfo by name using the given info hash table. If found,
3297 also update the locations pointed to by filename_ptr and linenumber_ptr.
3299 This function returns TRUE if a varinfo that matches the given symbol
3300 and address is found with any error; otherwise it returns FALSE. */
3303 info_hash_lookup_varinfo (struct info_hash_table
*hash_table
,
3306 const char **filename_ptr
,
3307 unsigned int *linenumber_ptr
)
3309 const char *name
= bfd_asymbol_name (sym
);
3310 asection
*sec
= bfd_get_section (sym
);
3311 struct varinfo
* each
;
3312 struct info_list_node
*node
;
3314 for (node
= lookup_info_hash_table (hash_table
, name
);
3318 each
= (struct varinfo
*) node
->info
;
3319 if (each
->addr
== addr
3320 && (!each
->sec
|| each
->sec
== sec
))
3323 *filename_ptr
= each
->file
;
3324 *linenumber_ptr
= each
->line
;
3332 /* Update the funcinfo and varinfo info hash tables if they are
3333 not up to date. Returns TRUE if there is no error; otherwise
3334 returns FALSE and disable the info hash tables. */
3337 stash_maybe_update_info_hash_tables (struct dwarf2_debug
*stash
)
3339 struct comp_unit
*each
;
3341 /* Exit if hash tables are up-to-date. */
3342 if (stash
->all_comp_units
== stash
->hash_units_head
)
3345 if (stash
->hash_units_head
)
3346 each
= stash
->hash_units_head
->prev_unit
;
3348 each
= stash
->last_comp_unit
;
3352 if (!comp_unit_hash_info (stash
, each
, stash
->funcinfo_hash_table
,
3353 stash
->varinfo_hash_table
))
3355 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
3358 each
= each
->prev_unit
;
3361 stash
->hash_units_head
= stash
->all_comp_units
;
3365 /* Check consistency of info hash tables. This is for debugging only. */
3367 static void ATTRIBUTE_UNUSED
3368 stash_verify_info_hash_table (struct dwarf2_debug
*stash
)
3370 struct comp_unit
*each_unit
;
3371 struct funcinfo
*each_func
;
3372 struct varinfo
*each_var
;
3373 struct info_list_node
*node
;
3376 for (each_unit
= stash
->all_comp_units
;
3378 each_unit
= each_unit
->next_unit
)
3380 for (each_func
= each_unit
->function_table
;
3382 each_func
= each_func
->prev_func
)
3384 if (!each_func
->name
)
3386 node
= lookup_info_hash_table (stash
->funcinfo_hash_table
,
3390 while (node
&& !found
)
3392 found
= node
->info
== each_func
;
3398 for (each_var
= each_unit
->variable_table
;
3400 each_var
= each_var
->prev_var
)
3402 if (!each_var
->name
|| !each_var
->file
|| each_var
->stack
)
3404 node
= lookup_info_hash_table (stash
->varinfo_hash_table
,
3408 while (node
&& !found
)
3410 found
= node
->info
== each_var
;
3418 /* Check to see if we want to enable the info hash tables, which consume
3419 quite a bit of memory. Currently we only check the number times
3420 bfd_dwarf2_find_line is called. In the future, we may also want to
3421 take the number of symbols into account. */
3424 stash_maybe_enable_info_hash_tables (bfd
*abfd
, struct dwarf2_debug
*stash
)
3426 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_OFF
);
3428 if (stash
->info_hash_count
++ < STASH_INFO_HASH_TRIGGER
)
3431 /* FIXME: Maybe we should check the reduce_memory_overheads
3432 and optimize fields in the bfd_link_info structure ? */
3434 /* Create hash tables. */
3435 stash
->funcinfo_hash_table
= create_info_hash_table (abfd
);
3436 stash
->varinfo_hash_table
= create_info_hash_table (abfd
);
3437 if (!stash
->funcinfo_hash_table
|| !stash
->varinfo_hash_table
)
3439 /* Turn off info hashes if any allocation above fails. */
3440 stash
->info_hash_status
= STASH_INFO_HASH_DISABLED
;
3443 /* We need a forced update so that the info hash tables will
3444 be created even though there is no compilation unit. That
3445 happens if STASH_INFO_HASH_TRIGGER is 0. */
3446 stash_maybe_update_info_hash_tables (stash
);
3447 stash
->info_hash_status
= STASH_INFO_HASH_ON
;
3450 /* Find the file and line associated with a symbol and address using the
3451 info hash tables of a stash. If there is a match, the function returns
3452 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
3453 otherwise it returns FALSE. */
3456 stash_find_line_fast (struct dwarf2_debug
*stash
,
3459 const char **filename_ptr
,
3460 unsigned int *linenumber_ptr
)
3462 BFD_ASSERT (stash
->info_hash_status
== STASH_INFO_HASH_ON
);
3464 if (sym
->flags
& BSF_FUNCTION
)
3465 return info_hash_lookup_funcinfo (stash
->funcinfo_hash_table
, sym
, addr
,
3466 filename_ptr
, linenumber_ptr
);
3467 return info_hash_lookup_varinfo (stash
->varinfo_hash_table
, sym
, addr
,
3468 filename_ptr
, linenumber_ptr
);
3471 /* Save current section VMAs. */
3474 save_section_vma (const bfd
*abfd
, struct dwarf2_debug
*stash
)
3479 if (abfd
->section_count
== 0)
3481 stash
->sec_vma
= bfd_malloc (sizeof (*stash
->sec_vma
) * abfd
->section_count
);
3482 if (stash
->sec_vma
== NULL
)
3484 for (i
= 0, s
= abfd
->sections
; i
< abfd
->section_count
; i
++, s
= s
->next
)
3486 if (s
->output_section
!= NULL
)
3487 stash
->sec_vma
[i
] = s
->output_section
->vma
+ s
->output_offset
;
3489 stash
->sec_vma
[i
] = s
->vma
;
3494 /* Compare current section VMAs against those at the time the stash
3495 was created. If find_nearest_line is used in linker warnings or
3496 errors early in the link process, the debug info stash will be
3497 invalid for later calls. This is because we relocate debug info
3498 sections, so the stashed section contents depend on symbol values,
3499 which in turn depend on section VMAs. */
3502 section_vma_same (const bfd
*abfd
, const struct dwarf2_debug
*stash
)
3507 for (i
= 0, s
= abfd
->sections
; i
< abfd
->section_count
; i
++, s
= s
->next
)
3511 if (s
->output_section
!= NULL
)
3512 vma
= s
->output_section
->vma
+ s
->output_offset
;
3515 if (vma
!= stash
->sec_vma
[i
])
3521 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
3522 If DEBUG_BFD is not specified, we read debug information from ABFD
3523 or its gnu_debuglink. The results will be stored in PINFO.
3524 The function returns TRUE iff debug information is ready. */
3527 _bfd_dwarf2_slurp_debug_info (bfd
*abfd
, bfd
*debug_bfd
,
3528 const struct dwarf_debug_section
*debug_sections
,
3531 bfd_boolean do_place
)
3533 bfd_size_type amt
= sizeof (struct dwarf2_debug
);
3534 bfd_size_type total_size
;
3536 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
3540 if (section_vma_same (abfd
, stash
))
3542 _bfd_dwarf2_cleanup_debug_info (abfd
, pinfo
);
3543 memset (stash
, 0, amt
);
3547 stash
= (struct dwarf2_debug
*) bfd_zalloc (abfd
, amt
);
3551 stash
->debug_sections
= debug_sections
;
3552 stash
->syms
= symbols
;
3553 if (!save_section_vma (abfd
, stash
))
3558 if (debug_bfd
== NULL
)
3561 msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
3562 if (msec
== NULL
&& abfd
== debug_bfd
)
3564 char * debug_filename
= bfd_follow_gnu_debuglink (abfd
, DEBUGDIR
);
3566 if (debug_filename
== NULL
)
3567 /* No dwarf2 info, and no gnu_debuglink to follow.
3568 Note that at this point the stash has been allocated, but
3569 contains zeros. This lets future calls to this function
3570 fail more quickly. */
3573 if ((debug_bfd
= bfd_openr (debug_filename
, NULL
)) == NULL
3574 || ! bfd_check_format (debug_bfd
, bfd_object
)
3575 || (msec
= find_debug_info (debug_bfd
,
3576 debug_sections
, NULL
)) == NULL
3577 || !bfd_generic_link_read_symbols (debug_bfd
))
3580 bfd_close (debug_bfd
);
3581 /* FIXME: Should we report our failure to follow the debuglink ? */
3582 free (debug_filename
);
3586 symbols
= bfd_get_outsymbols (debug_bfd
);
3587 stash
->syms
= symbols
;
3588 stash
->close_on_cleanup
= TRUE
;
3590 stash
->bfd_ptr
= debug_bfd
;
3593 && !place_sections (abfd
, stash
))
3596 /* There can be more than one DWARF2 info section in a BFD these
3597 days. First handle the easy case when there's only one. If
3598 there's more than one, try case two: none of the sections is
3599 compressed. In that case, read them all in and produce one
3600 large stash. We do this in two passes - in the first pass we
3601 just accumulate the section sizes, and in the second pass we
3602 read in the section's contents. (The allows us to avoid
3603 reallocing the data as we add sections to the stash.) If
3604 some or all sections are compressed, then do things the slow
3605 way, with a bunch of reallocs. */
3607 if (! find_debug_info (debug_bfd
, debug_sections
, msec
))
3609 /* Case 1: only one info section. */
3610 total_size
= msec
->size
;
3611 if (! read_section (debug_bfd
, &stash
->debug_sections
[debug_info
],
3613 &stash
->info_ptr_memory
, &total_size
))
3618 /* Case 2: multiple sections. */
3619 for (total_size
= 0;
3621 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
3622 total_size
+= msec
->size
;
3624 stash
->info_ptr_memory
= (bfd_byte
*) bfd_malloc (total_size
);
3625 if (stash
->info_ptr_memory
== NULL
)
3629 for (msec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
3631 msec
= find_debug_info (debug_bfd
, debug_sections
, msec
))
3639 if (!(bfd_simple_get_relocated_section_contents
3640 (debug_bfd
, msec
, stash
->info_ptr_memory
+ total_size
,
3648 stash
->info_ptr
= stash
->info_ptr_memory
;
3649 stash
->info_ptr_end
= stash
->info_ptr
+ total_size
;
3650 stash
->sec
= find_debug_info (debug_bfd
, debug_sections
, NULL
);
3651 stash
->sec_info_ptr
= stash
->info_ptr
;
3655 /* Find the source code location of SYMBOL. If SYMBOL is NULL
3656 then find the nearest source code location corresponding to
3657 the address SECTION + OFFSET.
3658 Returns TRUE if the line is found without error and fills in
3659 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
3660 NULL the FUNCTIONNAME_PTR is also filled in.
3661 SYMBOLS contains the symbol table for ABFD.
3662 DEBUG_SECTIONS contains the name of the dwarf debug sections.
3663 ADDR_SIZE is the number of bytes in the initial .debug_info length
3664 field and in the abbreviation offset, or zero to indicate that the
3665 default value should be used. */
3668 _bfd_dwarf2_find_nearest_line (bfd
*abfd
,
3673 const char **filename_ptr
,
3674 const char **functionname_ptr
,
3675 unsigned int *linenumber_ptr
,
3676 unsigned int *discriminator_ptr
,
3677 const struct dwarf_debug_section
*debug_sections
,
3678 unsigned int addr_size
,
3681 /* Read each compilation unit from the section .debug_info, and check
3682 to see if it contains the address we are searching for. If yes,
3683 lookup the address, and return the line number info. If no, go
3684 on to the next compilation unit.
3686 We keep a list of all the previously read compilation units, and
3687 a pointer to the next un-read compilation unit. Check the
3688 previously read units before reading more. */
3689 struct dwarf2_debug
*stash
;
3690 /* What address are we looking for? */
3692 struct comp_unit
* each
;
3693 struct funcinfo
*function
= NULL
;
3694 bfd_boolean found
= FALSE
;
3695 bfd_boolean do_line
;
3697 *filename_ptr
= NULL
;
3698 if (functionname_ptr
!= NULL
)
3699 *functionname_ptr
= NULL
;
3700 *linenumber_ptr
= 0;
3701 if (discriminator_ptr
)
3702 *discriminator_ptr
= 0;
3704 if (! _bfd_dwarf2_slurp_debug_info (abfd
, NULL
, debug_sections
,
3706 (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0))
3709 stash
= (struct dwarf2_debug
*) *pinfo
;
3711 do_line
= symbol
!= NULL
;
3714 BFD_ASSERT (section
== NULL
&& offset
== 0 && functionname_ptr
== NULL
);
3715 section
= bfd_get_section (symbol
);
3716 addr
= symbol
->value
;
3720 BFD_ASSERT (section
!= NULL
&& functionname_ptr
!= NULL
);
3724 if (section
->output_section
)
3725 addr
+= section
->output_section
->vma
+ section
->output_offset
;
3727 addr
+= section
->vma
;
3729 /* A null info_ptr indicates that there is no dwarf2 info
3730 (or that an error occured while setting up the stash). */
3731 if (! stash
->info_ptr
)
3734 stash
->inliner_chain
= NULL
;
3736 /* Check the previously read comp. units first. */
3739 /* The info hash tables use quite a bit of memory. We may not want to
3740 always use them. We use some heuristics to decide if and when to
3742 if (stash
->info_hash_status
== STASH_INFO_HASH_OFF
)
3743 stash_maybe_enable_info_hash_tables (abfd
, stash
);
3745 /* Keep info hash table up to date if they are available. Note that we
3746 may disable the hash tables if there is any error duing update. */
3747 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
3748 stash_maybe_update_info_hash_tables (stash
);
3750 if (stash
->info_hash_status
== STASH_INFO_HASH_ON
)
3752 found
= stash_find_line_fast (stash
, symbol
, addr
, filename_ptr
,
3759 /* Check the previously read comp. units first. */
3760 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
3761 if ((symbol
->flags
& BSF_FUNCTION
) == 0
3762 || each
->arange
.high
== 0
3763 || comp_unit_contains_address (each
, addr
))
3765 found
= comp_unit_find_line (each
, symbol
, addr
, filename_ptr
,
3766 linenumber_ptr
, stash
);
3774 bfd_vma min_range
= (bfd_vma
) -1;
3775 const char * local_filename
= NULL
;
3776 struct funcinfo
*local_function
= NULL
;
3777 unsigned int local_linenumber
= 0;
3778 unsigned int local_discriminator
= 0;
3780 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
3782 bfd_vma range
= (bfd_vma
) -1;
3784 found
= ((each
->arange
.high
== 0
3785 || comp_unit_contains_address (each
, addr
))
3786 && (range
= comp_unit_find_nearest_line (each
, addr
,
3790 & local_discriminator
,
3794 /* PRs 15935 15994: Bogus debug information may have provided us
3795 with an erroneous match. We attempt to counter this by
3796 selecting the match that has the smallest address range
3797 associated with it. (We are assuming that corrupt debug info
3798 will tend to result in extra large address ranges rather than
3799 extra small ranges).
3801 This does mean that we scan through all of the CUs associated
3802 with the bfd each time this function is called. But this does
3803 have the benefit of producing consistent results every time the
3804 function is called. */
3805 if (range
<= min_range
)
3807 if (filename_ptr
&& local_filename
)
3808 * filename_ptr
= local_filename
;
3810 function
= local_function
;
3811 if (discriminator_ptr
&& local_discriminator
)
3812 * discriminator_ptr
= local_discriminator
;
3813 if (local_linenumber
)
3814 * linenumber_ptr
= local_linenumber
;
3820 if (* linenumber_ptr
)
3827 /* The DWARF2 spec says that the initial length field, and the
3828 offset of the abbreviation table, should both be 4-byte values.
3829 However, some compilers do things differently. */
3832 BFD_ASSERT (addr_size
== 4 || addr_size
== 8);
3834 /* Read each remaining comp. units checking each as they are read. */
3835 while (stash
->info_ptr
< stash
->info_ptr_end
)
3838 unsigned int offset_size
= addr_size
;
3839 bfd_byte
*info_ptr_unit
= stash
->info_ptr
;
3841 length
= read_4_bytes (stash
->bfd_ptr
, stash
->info_ptr
);
3842 /* A 0xffffff length is the DWARF3 way of indicating
3843 we use 64-bit offsets, instead of 32-bit offsets. */
3844 if (length
== 0xffffffff)
3847 length
= read_8_bytes (stash
->bfd_ptr
, stash
->info_ptr
+ 4);
3848 stash
->info_ptr
+= 12;
3850 /* A zero length is the IRIX way of indicating 64-bit offsets,
3851 mostly because the 64-bit length will generally fit in 32
3852 bits, and the endianness helps. */
3853 else if (length
== 0)
3856 length
= read_4_bytes (stash
->bfd_ptr
, stash
->info_ptr
+ 4);
3857 stash
->info_ptr
+= 8;
3859 /* In the absence of the hints above, we assume 32-bit DWARF2
3860 offsets even for targets with 64-bit addresses, because:
3861 a) most of the time these targets will not have generated
3862 more than 2Gb of debug info and so will not need 64-bit
3865 b) if they do use 64-bit offsets but they are not using
3866 the size hints that are tested for above then they are
3867 not conforming to the DWARF3 standard anyway. */
3868 else if (addr_size
== 8)
3871 stash
->info_ptr
+= 4;
3874 stash
->info_ptr
+= 4;
3878 each
= parse_comp_unit (stash
, length
, info_ptr_unit
,
3881 /* The dwarf information is damaged, don't trust it any
3884 stash
->info_ptr
+= length
;
3886 if (stash
->all_comp_units
)
3887 stash
->all_comp_units
->prev_unit
= each
;
3889 stash
->last_comp_unit
= each
;
3891 each
->next_unit
= stash
->all_comp_units
;
3892 stash
->all_comp_units
= each
;
3894 /* DW_AT_low_pc and DW_AT_high_pc are optional for
3895 compilation units. If we don't have them (i.e.,
3896 unit->high == 0), we need to consult the line info table
3897 to see if a compilation unit contains the given
3900 found
= (((symbol
->flags
& BSF_FUNCTION
) == 0
3901 || each
->arange
.high
== 0
3902 || comp_unit_contains_address (each
, addr
))
3903 && comp_unit_find_line (each
, symbol
, addr
,
3908 found
= ((each
->arange
.high
== 0
3909 || comp_unit_contains_address (each
, addr
))
3910 && comp_unit_find_nearest_line (each
, addr
,
3917 if ((bfd_vma
) (stash
->info_ptr
- stash
->sec_info_ptr
)
3918 == stash
->sec
->size
)
3920 stash
->sec
= find_debug_info (stash
->bfd_ptr
, debug_sections
,
3922 stash
->sec_info_ptr
= stash
->info_ptr
;
3933 if (!function
->is_linkage
3934 && _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
3935 *filename_ptr
? NULL
: filename_ptr
,
3938 function
->name
= *functionname_ptr
;
3939 function
->is_linkage
= TRUE
;
3942 *functionname_ptr
= function
->name
;
3944 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
3945 unset_sections (stash
);
3951 _bfd_dwarf2_find_inliner_info (bfd
*abfd ATTRIBUTE_UNUSED
,
3952 const char **filename_ptr
,
3953 const char **functionname_ptr
,
3954 unsigned int *linenumber_ptr
,
3957 struct dwarf2_debug
*stash
;
3959 stash
= (struct dwarf2_debug
*) *pinfo
;
3962 struct funcinfo
*func
= stash
->inliner_chain
;
3964 if (func
&& func
->caller_func
)
3966 *filename_ptr
= func
->caller_file
;
3967 *functionname_ptr
= func
->caller_func
->name
;
3968 *linenumber_ptr
= func
->caller_line
;
3969 stash
->inliner_chain
= func
->caller_func
;
3978 _bfd_dwarf2_cleanup_debug_info (bfd
*abfd
, void **pinfo
)
3980 struct dwarf2_debug
*stash
= (struct dwarf2_debug
*) *pinfo
;
3981 struct comp_unit
*each
;
3983 if (abfd
== NULL
|| stash
== NULL
)
3986 for (each
= stash
->all_comp_units
; each
; each
= each
->next_unit
)
3988 struct abbrev_info
**abbrevs
= each
->abbrevs
;
3989 struct funcinfo
*function_table
= each
->function_table
;
3990 struct varinfo
*variable_table
= each
->variable_table
;
3993 for (i
= 0; i
< ABBREV_HASH_SIZE
; i
++)
3995 struct abbrev_info
*abbrev
= abbrevs
[i
];
3999 free (abbrev
->attrs
);
4000 abbrev
= abbrev
->next
;
4004 if (each
->line_table
)
4006 free (each
->line_table
->dirs
);
4007 free (each
->line_table
->files
);
4010 while (function_table
)
4012 if (function_table
->file
)
4014 free (function_table
->file
);
4015 function_table
->file
= NULL
;
4018 if (function_table
->caller_file
)
4020 free (function_table
->caller_file
);
4021 function_table
->caller_file
= NULL
;
4023 function_table
= function_table
->prev_func
;
4026 while (variable_table
)
4028 if (variable_table
->file
)
4030 free (variable_table
->file
);
4031 variable_table
->file
= NULL
;
4034 variable_table
= variable_table
->prev_var
;
4038 if (stash
->dwarf_abbrev_buffer
)
4039 free (stash
->dwarf_abbrev_buffer
);
4040 if (stash
->dwarf_line_buffer
)
4041 free (stash
->dwarf_line_buffer
);
4042 if (stash
->dwarf_str_buffer
)
4043 free (stash
->dwarf_str_buffer
);
4044 if (stash
->dwarf_ranges_buffer
)
4045 free (stash
->dwarf_ranges_buffer
);
4046 if (stash
->info_ptr_memory
)
4047 free (stash
->info_ptr_memory
);
4048 if (stash
->close_on_cleanup
)
4049 bfd_close (stash
->bfd_ptr
);
4050 if (stash
->alt_dwarf_str_buffer
)
4051 free (stash
->alt_dwarf_str_buffer
);
4052 if (stash
->alt_dwarf_info_buffer
)
4053 free (stash
->alt_dwarf_info_buffer
);
4055 free (stash
->sec_vma
);
4056 if (stash
->adjusted_sections
)
4057 free (stash
->adjusted_sections
);
4058 if (stash
->alt_bfd_ptr
)
4059 bfd_close (stash
->alt_bfd_ptr
);
4062 /* Find the function to a particular section and offset,
4063 for error reporting. */
4066 _bfd_elf_find_function (bfd
*abfd
,
4070 const char **filename_ptr
,
4071 const char **functionname_ptr
)
4073 struct elf_find_function_cache
4075 asection
*last_section
;
4077 const char *filename
;
4078 bfd_size_type func_size
;
4081 if (symbols
== NULL
)
4084 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
4087 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
4090 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
4091 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
4095 if (cache
->last_section
!= section
4096 || cache
->func
== NULL
4097 || offset
< cache
->func
->value
4098 || offset
>= cache
->func
->value
+ cache
->func_size
)
4103 /* ??? Given multiple file symbols, it is impossible to reliably
4104 choose the right file name for global symbols. File symbols are
4105 local symbols, and thus all file symbols must sort before any
4106 global symbols. The ELF spec may be interpreted to say that a
4107 file symbol must sort before other local symbols, but currently
4108 ld -r doesn't do this. So, for ld -r output, it is possible to
4109 make a better choice of file name for local symbols by ignoring
4110 file symbols appearing after a given local symbol. */
4111 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
4112 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4116 state
= nothing_seen
;
4117 cache
->filename
= NULL
;
4119 cache
->func_size
= 0;
4120 cache
->last_section
= section
;
4122 for (p
= symbols
; *p
!= NULL
; p
++)
4128 if ((sym
->flags
& BSF_FILE
) != 0)
4131 if (state
== symbol_seen
)
4132 state
= file_after_symbol_seen
;
4136 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
4138 && code_off
<= offset
4139 && (code_off
> low_func
4140 || (code_off
== low_func
4141 && size
> cache
->func_size
)))
4144 cache
->func_size
= size
;
4145 cache
->filename
= NULL
;
4146 low_func
= code_off
;
4148 && ((sym
->flags
& BSF_LOCAL
) != 0
4149 || state
!= file_after_symbol_seen
))
4150 cache
->filename
= bfd_asymbol_name (file
);
4152 if (state
== nothing_seen
)
4153 state
= symbol_seen
;
4157 if (cache
->func
== NULL
)
4161 *filename_ptr
= cache
->filename
;
4162 if (functionname_ptr
)
4163 *functionname_ptr
= bfd_asymbol_name (cache
->func
);