Implement tp_richcompare for gdb.Block
[binutils-gdb.git] / bfd / dwarf2.c
blob151ed0237ca025d114a37b7e2e2decd6ac73c193
1 /* DWARF 2 support.
2 Copyright (C) 1994-2024 Free Software Foundation, Inc.
4 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
5 (gavin@cygnus.com).
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. */
32 #include "sysdep.h"
33 #include "bfd.h"
34 #include "libiberty.h"
35 #include "demangle.h"
36 #include "libbfd.h"
37 #include "elf-bfd.h"
38 #include "dwarf2.h"
39 #include "hashtab.h"
40 #include "splay-tree.h"
42 /* The data in the .debug_line statement prologue looks like this. */
44 struct line_head
46 bfd_vma total_length;
47 unsigned short version;
48 bfd_vma prologue_length;
49 unsigned char minimum_instruction_length;
50 unsigned char maximum_ops_per_insn;
51 unsigned char default_is_stmt;
52 int line_base;
53 unsigned char line_range;
54 unsigned char opcode_base;
55 unsigned char *standard_opcode_lengths;
58 /* Attributes have a name and a value. */
60 struct attribute
62 enum dwarf_attribute name;
63 enum dwarf_form form;
64 union
66 char *str;
67 struct dwarf_block *blk;
68 uint64_t val;
69 int64_t sval;
74 /* Blocks are a bunch of untyped bytes. */
75 struct dwarf_block
77 unsigned int size;
78 bfd_byte *data;
81 struct adjusted_section
83 asection *section;
84 bfd_vma adj_vma;
85 bfd_vma orig_vma;
88 /* A trie to map quickly from address range to compilation unit.
90 This is a fairly standard radix-256 trie, used to quickly locate which
91 compilation unit any given address belongs to. Given that each compilation
92 unit may register hundreds of very small and unaligned ranges (which may
93 potentially overlap, due to inlining and other concerns), and a large
94 program may end up containing hundreds of thousands of such ranges, we cannot
95 scan through them linearly without undue slowdown.
97 We use a hybrid trie to avoid memory explosion: There are two types of trie
98 nodes, leaves and interior nodes. (Almost all nodes are leaves, so they
99 take up the bulk of the memory usage.) Leaves contain a simple array of
100 ranges (high/low address) and which compilation unit contains those ranges,
101 and when we get to a leaf, we scan through it linearly. Interior nodes
102 contain pointers to 256 other nodes, keyed by the next byte of the address.
103 So for a 64-bit address like 0x1234567abcd, we would start at the root and go
104 down child[0x00]->child[0x00]->child[0x01]->child[0x23]->child[0x45] etc.,
105 until we hit a leaf. (Nodes are, in general, leaves until they exceed the
106 default allocation of 16 elements, at which point they are converted to
107 interior node if possible.) This gives us near-constant lookup times;
108 the only thing that can be costly is if there are lots of overlapping ranges
109 within a single 256-byte segment of the binary, in which case we have to
110 scan through them all to find the best match.
112 For a binary with few ranges, we will in practice only have a single leaf
113 node at the root, containing a simple array. Thus, the scheme is efficient
114 for both small and large binaries.
117 /* Experiments have shown 16 to be a memory-efficient default leaf size.
118 The only case where a leaf will hold more memory than this, is at the
119 bottomost level (covering 256 bytes in the binary), where we'll expand
120 the leaf to be able to hold more ranges if needed.
122 #define TRIE_LEAF_SIZE 16
124 /* All trie_node pointers will really be trie_leaf or trie_interior,
125 but they have this common head. */
126 struct trie_node
128 /* If zero, we are an interior node.
129 Otherwise, how many ranges we have room for in this leaf. */
130 unsigned int num_room_in_leaf;
133 struct trie_leaf
135 struct trie_node head;
136 unsigned int num_stored_in_leaf;
137 struct {
138 struct comp_unit *unit;
139 bfd_vma low_pc, high_pc;
140 } ranges[];
143 struct trie_interior
145 struct trie_node head;
146 struct trie_node *children[256];
149 static struct trie_node *alloc_trie_leaf (bfd *abfd)
151 struct trie_leaf *leaf;
152 size_t amt = sizeof (*leaf) + TRIE_LEAF_SIZE * sizeof (leaf->ranges[0]);
153 leaf = bfd_zalloc (abfd, amt);
154 if (leaf == NULL)
155 return NULL;
156 leaf->head.num_room_in_leaf = TRIE_LEAF_SIZE;
157 return &leaf->head;
160 struct addr_range
162 bfd_byte *start;
163 bfd_byte *end;
166 /* Return true if address range do intersect. */
168 static bool
169 addr_range_intersects (struct addr_range *r1, struct addr_range *r2)
171 return (r1->start <= r2->start && r2->start < r1->end)
172 || (r1->start <= (r2->end - 1) && (r2->end - 1) < r1->end);
175 /* Compare function for splay tree of addr_ranges. */
177 static int
178 splay_tree_compare_addr_range (splay_tree_key xa, splay_tree_key xb)
180 struct addr_range *r1 = (struct addr_range *) xa;
181 struct addr_range *r2 = (struct addr_range *) xb;
183 if (addr_range_intersects (r1, r2) || addr_range_intersects (r2, r1))
184 return 0;
185 else if (r1->end <= r2->start)
186 return -1;
187 else
188 return 1;
191 /* Splay tree release function for keys (addr_range). */
193 static void
194 splay_tree_free_addr_range (splay_tree_key key)
196 free ((struct addr_range *)key);
199 struct dwarf2_debug_file
201 /* The actual bfd from which debug info was loaded. Might be
202 different to orig_bfd because of gnu_debuglink sections. */
203 bfd *bfd_ptr;
205 /* Pointer to the symbol table. */
206 asymbol **syms;
208 /* The current info pointer for the .debug_info section being parsed. */
209 bfd_byte *info_ptr;
211 /* A pointer to the memory block allocated for .debug_info sections. */
212 bfd_byte *dwarf_info_buffer;
214 /* Length of the loaded .debug_info sections. */
215 bfd_size_type dwarf_info_size;
217 /* Pointer to the .debug_abbrev section loaded into memory. */
218 bfd_byte *dwarf_abbrev_buffer;
220 /* Length of the loaded .debug_abbrev section. */
221 bfd_size_type dwarf_abbrev_size;
223 /* Buffer for decode_line_info. */
224 bfd_byte *dwarf_line_buffer;
226 /* Length of the loaded .debug_line section. */
227 bfd_size_type dwarf_line_size;
229 /* Pointer to the .debug_str section loaded into memory. */
230 bfd_byte *dwarf_str_buffer;
232 /* Length of the loaded .debug_str section. */
233 bfd_size_type dwarf_str_size;
235 /* Pointer to the .debug_str_offsets section loaded into memory. */
236 bfd_byte *dwarf_str_offsets_buffer;
238 /* Length of the loaded .debug_str_offsets section. */
239 bfd_size_type dwarf_str_offsets_size;
241 /* Pointer to the .debug_addr section loaded into memory. */
242 bfd_byte *dwarf_addr_buffer;
244 /* Length of the loaded .debug_addr section. */
245 bfd_size_type dwarf_addr_size;
247 /* Pointer to the .debug_line_str section loaded into memory. */
248 bfd_byte *dwarf_line_str_buffer;
250 /* Length of the loaded .debug_line_str section. */
251 bfd_size_type dwarf_line_str_size;
253 /* Pointer to the .debug_ranges section loaded into memory. */
254 bfd_byte *dwarf_ranges_buffer;
256 /* Length of the loaded .debug_ranges section. */
257 bfd_size_type dwarf_ranges_size;
259 /* Pointer to the .debug_rnglists section loaded into memory. */
260 bfd_byte *dwarf_rnglists_buffer;
262 /* Length of the loaded .debug_rnglists section. */
263 bfd_size_type dwarf_rnglists_size;
265 /* A list of all previously read comp_units. */
266 struct comp_unit *all_comp_units;
268 /* A list of all previously read comp_units with no ranges (yet). */
269 struct comp_unit *all_comp_units_without_ranges;
271 /* Last comp unit in list above. */
272 struct comp_unit *last_comp_unit;
274 /* Line table at line_offset zero. */
275 struct line_info_table *line_table;
277 /* Hash table to map offsets to decoded abbrevs. */
278 htab_t abbrev_offsets;
280 /* Root of a trie to map addresses to compilation units. */
281 struct trie_node *trie_root;
283 /* Splay tree to map info_ptr address to compilation units. */
284 splay_tree comp_unit_tree;
287 struct dwarf2_debug
289 /* Names of the debug sections. */
290 const struct dwarf_debug_section *debug_sections;
292 /* Per-file stuff. */
293 struct dwarf2_debug_file f, alt;
295 /* Pointer to the original bfd for which debug was loaded. This is what
296 we use to compare and so check that the cached debug data is still
297 valid - it saves having to possibly dereference the gnu_debuglink each
298 time. */
299 bfd *orig_bfd;
301 /* If the most recent call to bfd_find_nearest_line was given an
302 address in an inlined function, preserve a pointer into the
303 calling chain for subsequent calls to bfd_find_inliner_info to
304 use. */
305 struct funcinfo *inliner_chain;
307 /* Section VMAs at the time the stash was built. */
308 bfd_vma *sec_vma;
309 /* Number of sections in the SEC_VMA table. */
310 unsigned int sec_vma_count;
312 /* Number of sections whose VMA we must adjust. */
313 int adjusted_section_count;
315 /* Array of sections with adjusted VMA. */
316 struct adjusted_section *adjusted_sections;
318 /* Number of times find_line is called. This is used in
319 the heuristic for enabling the info hash tables. */
320 int info_hash_count;
322 #define STASH_INFO_HASH_TRIGGER 100
324 /* Hash table mapping symbol names to function infos. */
325 struct info_hash_table *funcinfo_hash_table;
327 /* Hash table mapping symbol names to variable infos. */
328 struct info_hash_table *varinfo_hash_table;
330 /* Head of comp_unit list in the last hash table update. */
331 struct comp_unit *hash_units_head;
333 /* Status of info hash. */
334 int info_hash_status;
335 #define STASH_INFO_HASH_OFF 0
336 #define STASH_INFO_HASH_ON 1
337 #define STASH_INFO_HASH_DISABLED 2
339 /* True if we opened bfd_ptr. */
340 bool close_on_cleanup;
343 struct arange
345 struct arange *next;
346 bfd_vma low;
347 bfd_vma high;
350 /* A minimal decoding of DWARF2 compilation units. We only decode
351 what's needed to get to the line number information. */
353 struct comp_unit
355 /* Chain the previously read compilation units. */
356 struct comp_unit *next_unit;
358 /* Chain the previously read compilation units that have no ranges yet.
359 We scan these separately when we have a trie over the ranges.
360 Unused if arange.high != 0. */
361 struct comp_unit *next_unit_without_ranges;
363 /* Likewise, chain the compilation unit read after this one.
364 The comp units are stored in reversed reading order. */
365 struct comp_unit *prev_unit;
367 /* Keep the bfd convenient (for memory allocation). */
368 bfd *abfd;
370 /* The lowest and highest addresses contained in this compilation
371 unit as specified in the compilation unit header. */
372 struct arange arange;
374 /* The DW_AT_name attribute (for error messages). */
375 char *name;
377 /* The abbrev hash table. */
378 struct abbrev_info **abbrevs;
380 /* DW_AT_language. */
381 int lang;
383 /* Note that an error was found by comp_unit_find_nearest_line. */
384 int error;
386 /* The DW_AT_comp_dir attribute. */
387 char *comp_dir;
389 /* TRUE if there is a line number table associated with this comp. unit. */
390 int stmtlist;
392 /* Pointer to the current comp_unit so that we can find a given entry
393 by its reference. */
394 bfd_byte *info_ptr_unit;
396 /* The offset into .debug_line of the line number table. */
397 unsigned long line_offset;
399 /* Pointer to the first child die for the comp unit. */
400 bfd_byte *first_child_die_ptr;
402 /* The end of the comp unit. */
403 bfd_byte *end_ptr;
405 /* The decoded line number, NULL if not yet decoded. */
406 struct line_info_table *line_table;
408 /* A list of the functions found in this comp. unit. */
409 struct funcinfo *function_table;
411 /* A table of function information references searchable by address. */
412 struct lookup_funcinfo *lookup_funcinfo_table;
414 /* Number of functions in the function_table and sorted_function_table. */
415 bfd_size_type number_of_functions;
417 /* A list of the variables found in this comp. unit. */
418 struct varinfo *variable_table;
420 /* Pointers to dwarf2_debug structures. */
421 struct dwarf2_debug *stash;
422 struct dwarf2_debug_file *file;
424 /* DWARF format version for this unit - from unit header. */
425 int version;
427 /* Address size for this unit - from unit header. */
428 unsigned char addr_size;
430 /* Offset size for this unit - from unit header. */
431 unsigned char offset_size;
433 /* Base address for this unit - from DW_AT_low_pc attribute of
434 DW_TAG_compile_unit DIE */
435 bfd_vma base_address;
437 /* TRUE if symbols are cached in hash table for faster lookup by name. */
438 bool cached;
440 /* Used when iterating over trie leaves to know which units we have
441 already seen in this iteration. */
442 bool mark;
444 /* Base address of debug_addr section. */
445 size_t dwarf_addr_offset;
447 /* Base address of string offset table. */
448 size_t dwarf_str_offset;
451 /* This data structure holds the information of an abbrev. */
452 struct abbrev_info
454 unsigned int number; /* Number identifying abbrev. */
455 enum dwarf_tag tag; /* DWARF tag. */
456 bool has_children; /* TRUE if the abbrev has children. */
457 unsigned int num_attrs; /* Number of attributes. */
458 struct attr_abbrev * attrs; /* An array of attribute descriptions. */
459 struct abbrev_info * next; /* Next in chain. */
462 struct attr_abbrev
464 enum dwarf_attribute name;
465 enum dwarf_form form;
466 bfd_vma implicit_const;
469 /* Map of uncompressed DWARF debug section name to compressed one. It
470 is terminated by NULL uncompressed_name. */
472 const struct dwarf_debug_section dwarf_debug_sections[] =
474 { ".debug_abbrev", ".zdebug_abbrev" },
475 { ".debug_aranges", ".zdebug_aranges" },
476 { ".debug_frame", ".zdebug_frame" },
477 { ".debug_info", ".zdebug_info" },
478 { ".debug_info", ".zdebug_info" },
479 { ".debug_line", ".zdebug_line" },
480 { ".debug_loc", ".zdebug_loc" },
481 { ".debug_macinfo", ".zdebug_macinfo" },
482 { ".debug_macro", ".zdebug_macro" },
483 { ".debug_pubnames", ".zdebug_pubnames" },
484 { ".debug_pubtypes", ".zdebug_pubtypes" },
485 { ".debug_ranges", ".zdebug_ranges" },
486 { ".debug_rnglists", ".zdebug_rnglist" },
487 { ".debug_static_func", ".zdebug_static_func" },
488 { ".debug_static_vars", ".zdebug_static_vars" },
489 { ".debug_str", ".zdebug_str", },
490 { ".debug_str", ".zdebug_str", },
491 { ".debug_str_offsets", ".zdebug_str_offsets", },
492 { ".debug_addr", ".zdebug_addr", },
493 { ".debug_line_str", ".zdebug_line_str", },
494 { ".debug_types", ".zdebug_types" },
495 /* GNU DWARF 1 extensions */
496 { ".debug_sfnames", ".zdebug_sfnames" },
497 { ".debug_srcinfo", ".zebug_srcinfo" },
498 /* SGI/MIPS DWARF 2 extensions */
499 { ".debug_funcnames", ".zdebug_funcnames" },
500 { ".debug_typenames", ".zdebug_typenames" },
501 { ".debug_varnames", ".zdebug_varnames" },
502 { ".debug_weaknames", ".zdebug_weaknames" },
503 { NULL, NULL },
506 /* NB/ Numbers in this enum must match up with indices
507 into the dwarf_debug_sections[] array above. */
508 enum dwarf_debug_section_enum
510 debug_abbrev = 0,
511 debug_aranges,
512 debug_frame,
513 debug_info,
514 debug_info_alt,
515 debug_line,
516 debug_loc,
517 debug_macinfo,
518 debug_macro,
519 debug_pubnames,
520 debug_pubtypes,
521 debug_ranges,
522 debug_rnglists,
523 debug_static_func,
524 debug_static_vars,
525 debug_str,
526 debug_str_alt,
527 debug_str_offsets,
528 debug_addr,
529 debug_line_str,
530 debug_types,
531 debug_sfnames,
532 debug_srcinfo,
533 debug_funcnames,
534 debug_typenames,
535 debug_varnames,
536 debug_weaknames,
537 debug_max
540 /* A static assertion. */
541 extern int dwarf_debug_section_assert[ARRAY_SIZE (dwarf_debug_sections)
542 == debug_max + 1 ? 1 : -1];
544 #ifndef ABBREV_HASH_SIZE
545 #define ABBREV_HASH_SIZE 121
546 #endif
547 #ifndef ATTR_ALLOC_CHUNK
548 #define ATTR_ALLOC_CHUNK 4
549 #endif
551 /* Variable and function hash tables. This is used to speed up look-up
552 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
553 In order to share code between variable and function infos, we use
554 a list of untyped pointer for all variable/function info associated with
555 a symbol. We waste a bit of memory for list with one node but that
556 simplifies the code. */
558 struct info_list_node
560 struct info_list_node *next;
561 void *info;
564 /* Info hash entry. */
565 struct info_hash_entry
567 struct bfd_hash_entry root;
568 struct info_list_node *head;
571 struct info_hash_table
573 struct bfd_hash_table base;
576 /* Function to create a new entry in info hash table. */
578 static struct bfd_hash_entry *
579 info_hash_table_newfunc (struct bfd_hash_entry *entry,
580 struct bfd_hash_table *table,
581 const char *string)
583 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
585 /* Allocate the structure if it has not already been allocated by a
586 derived class. */
587 if (ret == NULL)
589 ret = (struct info_hash_entry *) bfd_hash_allocate (table,
590 sizeof (* ret));
591 if (ret == NULL)
592 return NULL;
595 /* Call the allocation method of the base class. */
596 ret = ((struct info_hash_entry *)
597 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
599 /* Initialize the local fields here. */
600 if (ret)
601 ret->head = NULL;
603 return (struct bfd_hash_entry *) ret;
606 /* Function to create a new info hash table. It returns a pointer to the
607 newly created table or NULL if there is any error. We need abfd
608 solely for memory allocation. */
610 static struct info_hash_table *
611 create_info_hash_table (bfd *abfd)
613 struct info_hash_table *hash_table;
615 hash_table = ((struct info_hash_table *)
616 bfd_alloc (abfd, sizeof (struct info_hash_table)));
617 if (!hash_table)
618 return hash_table;
620 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
621 sizeof (struct info_hash_entry)))
623 bfd_release (abfd, hash_table);
624 return NULL;
627 return hash_table;
630 /* Insert an info entry into an info hash table. We do not check of
631 duplicate entries. Also, the caller need to guarantee that the
632 right type of info in inserted as info is passed as a void* pointer.
633 This function returns true if there is no error. */
635 static bool
636 insert_info_hash_table (struct info_hash_table *hash_table,
637 const char *key,
638 void *info,
639 bool copy_p)
641 struct info_hash_entry *entry;
642 struct info_list_node *node;
644 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
645 key, true, copy_p);
646 if (!entry)
647 return false;
649 node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
650 sizeof (*node));
651 if (!node)
652 return false;
654 node->info = info;
655 node->next = entry->head;
656 entry->head = node;
658 return true;
661 /* Look up an info entry list from an info hash table. Return NULL
662 if there is none. */
664 static struct info_list_node *
665 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
667 struct info_hash_entry *entry;
669 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
670 false, false);
671 return entry ? entry->head : NULL;
674 /* Read a section into its appropriate place in the dwarf2_debug
675 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
676 not NULL, use bfd_simple_get_relocated_section_contents to read the
677 section contents, otherwise use bfd_get_section_contents. Fail if
678 the located section does not contain at least OFFSET bytes. */
680 static bool
681 read_section (bfd *abfd,
682 const struct dwarf_debug_section *sec,
683 asymbol **syms,
684 uint64_t offset,
685 bfd_byte **section_buffer,
686 bfd_size_type *section_size)
688 const char *section_name = sec->uncompressed_name;
689 bfd_byte *contents = *section_buffer;
691 /* The section may have already been read. */
692 if (contents == NULL)
694 bfd_size_type amt;
695 asection *msec;
697 msec = bfd_get_section_by_name (abfd, section_name);
698 if (msec == NULL)
700 section_name = sec->compressed_name;
701 msec = bfd_get_section_by_name (abfd, section_name);
703 if (msec == NULL)
705 _bfd_error_handler (_("DWARF error: can't find %s section."),
706 sec->uncompressed_name);
707 bfd_set_error (bfd_error_bad_value);
708 return false;
711 if ((msec->flags & SEC_HAS_CONTENTS) == 0)
713 _bfd_error_handler (_("DWARF error: section %s has no contents"),
714 section_name);
715 bfd_set_error (bfd_error_no_contents);
716 return false;
719 if (bfd_section_size_insane (abfd, msec))
721 /* PR 26946 */
722 _bfd_error_handler (_("DWARF error: section %s is too big"),
723 section_name);
724 return false;
726 amt = bfd_get_section_limit_octets (abfd, msec);
727 *section_size = amt;
728 /* Paranoia - alloc one extra so that we can make sure a string
729 section is NUL terminated. */
730 amt += 1;
731 if (amt == 0)
733 /* Paranoia - this should never happen. */
734 bfd_set_error (bfd_error_no_memory);
735 return false;
737 contents = (bfd_byte *) bfd_malloc (amt);
738 if (contents == NULL)
739 return false;
740 if (syms
741 ? !bfd_simple_get_relocated_section_contents (abfd, msec, contents,
742 syms)
743 : !bfd_get_section_contents (abfd, msec, contents, 0, *section_size))
745 free (contents);
746 return false;
748 contents[*section_size] = 0;
749 *section_buffer = contents;
752 /* It is possible to get a bad value for the offset into the section
753 that the client wants. Validate it here to avoid trouble later. */
754 if (offset != 0 && offset >= *section_size)
756 /* xgettext: c-format */
757 _bfd_error_handler (_("DWARF error: offset (%" PRIu64 ")"
758 " greater than or equal to %s size (%" PRIu64 ")"),
759 (uint64_t) offset, section_name,
760 (uint64_t) *section_size);
761 bfd_set_error (bfd_error_bad_value);
762 return false;
765 return true;
768 /* Read dwarf information from a buffer. */
770 static inline uint64_t
771 read_n_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end, int n)
773 bfd_byte *buf = *ptr;
774 if (end - buf < n)
776 *ptr = end;
777 return 0;
779 *ptr = buf + n;
780 return bfd_get (n * 8, abfd, buf);
783 static unsigned int
784 read_1_byte (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
786 return read_n_bytes (abfd, ptr, end, 1);
789 static int
790 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte **ptr, bfd_byte *end)
792 bfd_byte *buf = *ptr;
793 if (end - buf < 1)
795 *ptr = end;
796 return 0;
798 *ptr = buf + 1;
799 return bfd_get_signed_8 (abfd, buf);
802 static unsigned int
803 read_2_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
805 return read_n_bytes (abfd, ptr, end, 2);
808 static unsigned int
809 read_3_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
811 unsigned int val = read_1_byte (abfd, ptr, end);
812 val <<= 8;
813 val |= read_1_byte (abfd, ptr, end);
814 val <<= 8;
815 val |= read_1_byte (abfd, ptr, end);
816 if (bfd_little_endian (abfd))
817 val = (((val >> 16) & 0xff)
818 | (val & 0xff00)
819 | ((val & 0xff) << 16));
820 return val;
823 static unsigned int
824 read_4_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
826 return read_n_bytes (abfd, ptr, end, 4);
829 static uint64_t
830 read_8_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
832 return read_n_bytes (abfd, ptr, end, 8);
835 static struct dwarf_block *
836 read_blk (bfd *abfd, bfd_byte **ptr, bfd_byte *end, size_t size)
838 bfd_byte *buf = *ptr;
839 struct dwarf_block *block;
841 block = (struct dwarf_block *) bfd_alloc (abfd, sizeof (*block));
842 if (block == NULL)
843 return NULL;
845 if (size > (size_t) (end - buf))
847 *ptr = end;
848 block->data = NULL;
849 block->size = 0;
851 else
853 *ptr = buf + size;
854 block->data = buf;
855 block->size = size;
857 return block;
860 /* Scans a NUL terminated string starting at *PTR, returning a pointer to it.
861 Bytes at or beyond BUF_END will not be read. Returns NULL if the
862 terminator is not found or if the string is empty. *PTR is
863 incremented over the bytes scanned, including the terminator. */
865 static char *
866 read_string (bfd_byte **ptr,
867 bfd_byte *buf_end)
869 bfd_byte *buf = *ptr;
870 bfd_byte *str = buf;
872 while (buf < buf_end)
873 if (*buf++ == 0)
875 if (str == buf - 1)
876 break;
877 *ptr = buf;
878 return (char *) str;
881 *ptr = buf;
882 return NULL;
885 /* Reads an offset from *PTR and then locates the string at this offset
886 inside the debug string section. Returns a pointer to the string.
887 Increments *PTR by the number of bytes read for the offset. This
888 value is set even if the function fails. Bytes at or beyond
889 BUF_END will not be read. Returns NULL if there was a problem, or
890 if the string is empty. Does not check for NUL termination of the
891 string. */
893 static char *
894 read_indirect_string (struct comp_unit *unit,
895 bfd_byte **ptr,
896 bfd_byte *buf_end)
898 uint64_t offset;
899 struct dwarf2_debug *stash = unit->stash;
900 struct dwarf2_debug_file *file = unit->file;
901 char *str;
903 if (unit->offset_size > (size_t) (buf_end - *ptr))
905 *ptr = buf_end;
906 return NULL;
909 if (unit->offset_size == 4)
910 offset = read_4_bytes (unit->abfd, ptr, buf_end);
911 else
912 offset = read_8_bytes (unit->abfd, ptr, buf_end);
914 if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
915 file->syms, offset,
916 &file->dwarf_str_buffer, &file->dwarf_str_size))
917 return NULL;
919 str = (char *) file->dwarf_str_buffer + offset;
920 if (*str == '\0')
921 return NULL;
922 return str;
925 /* Like read_indirect_string but from .debug_line_str section. */
927 static char *
928 read_indirect_line_string (struct comp_unit *unit,
929 bfd_byte **ptr,
930 bfd_byte *buf_end)
932 uint64_t offset;
933 struct dwarf2_debug *stash = unit->stash;
934 struct dwarf2_debug_file *file = unit->file;
935 char *str;
937 if (unit->offset_size > (size_t) (buf_end - *ptr))
939 *ptr = buf_end;
940 return NULL;
943 if (unit->offset_size == 4)
944 offset = read_4_bytes (unit->abfd, ptr, buf_end);
945 else
946 offset = read_8_bytes (unit->abfd, ptr, buf_end);
948 if (! read_section (unit->abfd, &stash->debug_sections[debug_line_str],
949 file->syms, offset,
950 &file->dwarf_line_str_buffer,
951 &file->dwarf_line_str_size))
952 return NULL;
954 str = (char *) file->dwarf_line_str_buffer + offset;
955 if (*str == '\0')
956 return NULL;
957 return str;
960 /* Like read_indirect_string but uses a .debug_str located in
961 an alternate file pointed to by the .gnu_debugaltlink section.
962 Used to impement DW_FORM_GNU_strp_alt. */
964 static char *
965 read_alt_indirect_string (struct comp_unit *unit,
966 bfd_byte **ptr,
967 bfd_byte *buf_end)
969 uint64_t offset;
970 struct dwarf2_debug *stash = unit->stash;
971 char *str;
973 if (unit->offset_size > (size_t) (buf_end - *ptr))
975 *ptr = buf_end;
976 return NULL;
979 if (unit->offset_size == 4)
980 offset = read_4_bytes (unit->abfd, ptr, buf_end);
981 else
982 offset = read_8_bytes (unit->abfd, ptr, buf_end);
984 if (stash->alt.bfd_ptr == NULL)
986 bfd *debug_bfd;
987 char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
989 if (debug_filename == NULL)
990 return NULL;
992 debug_bfd = bfd_openr (debug_filename, NULL);
993 free (debug_filename);
994 if (debug_bfd == NULL)
995 /* FIXME: Should we report our failure to follow the debuglink ? */
996 return NULL;
998 if (!bfd_check_format (debug_bfd, bfd_object))
1000 bfd_close (debug_bfd);
1001 return NULL;
1003 stash->alt.bfd_ptr = debug_bfd;
1006 if (! read_section (unit->stash->alt.bfd_ptr,
1007 stash->debug_sections + debug_str_alt,
1008 stash->alt.syms, offset,
1009 &stash->alt.dwarf_str_buffer,
1010 &stash->alt.dwarf_str_size))
1011 return NULL;
1013 str = (char *) stash->alt.dwarf_str_buffer + offset;
1014 if (*str == '\0')
1015 return NULL;
1017 return str;
1020 /* Resolve an alternate reference from UNIT at OFFSET.
1021 Returns a pointer into the loaded alternate CU upon success
1022 or NULL upon failure. */
1024 static bfd_byte *
1025 read_alt_indirect_ref (struct comp_unit *unit, uint64_t offset)
1027 struct dwarf2_debug *stash = unit->stash;
1029 if (stash->alt.bfd_ptr == NULL)
1031 bfd *debug_bfd;
1032 char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
1034 if (debug_filename == NULL)
1035 return NULL;
1037 debug_bfd = bfd_openr (debug_filename, NULL);
1038 free (debug_filename);
1039 if (debug_bfd == NULL)
1040 /* FIXME: Should we report our failure to follow the debuglink ? */
1041 return NULL;
1043 if (!bfd_check_format (debug_bfd, bfd_object))
1045 bfd_close (debug_bfd);
1046 return NULL;
1048 stash->alt.bfd_ptr = debug_bfd;
1051 if (! read_section (unit->stash->alt.bfd_ptr,
1052 stash->debug_sections + debug_info_alt,
1053 stash->alt.syms, offset,
1054 &stash->alt.dwarf_info_buffer,
1055 &stash->alt.dwarf_info_size))
1056 return NULL;
1058 return stash->alt.dwarf_info_buffer + offset;
1061 static uint64_t
1062 read_address (struct comp_unit *unit, bfd_byte **ptr, bfd_byte *buf_end)
1064 bfd_byte *buf = *ptr;
1065 int signed_vma = 0;
1067 if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour)
1068 signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
1070 if (unit->addr_size > (size_t) (buf_end - buf))
1072 *ptr = buf_end;
1073 return 0;
1076 *ptr = buf + unit->addr_size;
1077 if (signed_vma)
1079 switch (unit->addr_size)
1081 case 8:
1082 return bfd_get_signed_64 (unit->abfd, buf);
1083 case 4:
1084 return bfd_get_signed_32 (unit->abfd, buf);
1085 case 2:
1086 return bfd_get_signed_16 (unit->abfd, buf);
1087 default:
1088 abort ();
1091 else
1093 switch (unit->addr_size)
1095 case 8:
1096 return bfd_get_64 (unit->abfd, buf);
1097 case 4:
1098 return bfd_get_32 (unit->abfd, buf);
1099 case 2:
1100 return bfd_get_16 (unit->abfd, buf);
1101 default:
1102 abort ();
1107 /* Lookup an abbrev_info structure in the abbrev hash table. */
1109 static struct abbrev_info *
1110 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
1112 unsigned int hash_number;
1113 struct abbrev_info *abbrev;
1115 hash_number = number % ABBREV_HASH_SIZE;
1116 abbrev = abbrevs[hash_number];
1118 while (abbrev)
1120 if (abbrev->number == number)
1121 return abbrev;
1122 else
1123 abbrev = abbrev->next;
1126 return NULL;
1129 /* We keep a hash table to map .debug_abbrev section offsets to the
1130 array of abbrevs, so that compilation units using the same set of
1131 abbrevs do not waste memory. */
1133 struct abbrev_offset_entry
1135 size_t offset;
1136 struct abbrev_info **abbrevs;
1139 static hashval_t
1140 hash_abbrev (const void *p)
1142 const struct abbrev_offset_entry *ent = p;
1143 return htab_hash_pointer ((void *) ent->offset);
1146 static int
1147 eq_abbrev (const void *pa, const void *pb)
1149 const struct abbrev_offset_entry *a = pa;
1150 const struct abbrev_offset_entry *b = pb;
1151 return a->offset == b->offset;
1154 static void
1155 del_abbrev (void *p)
1157 struct abbrev_offset_entry *ent = p;
1158 struct abbrev_info **abbrevs = ent->abbrevs;
1159 size_t i;
1161 for (i = 0; i < ABBREV_HASH_SIZE; i++)
1163 struct abbrev_info *abbrev = abbrevs[i];
1165 while (abbrev)
1167 free (abbrev->attrs);
1168 abbrev = abbrev->next;
1171 free (ent);
1174 /* In DWARF version 2, the description of the debugging information is
1175 stored in a separate .debug_abbrev section. Before we read any
1176 dies from a section we read in all abbreviations and install them
1177 in a hash table. */
1179 static struct abbrev_info**
1180 read_abbrevs (bfd *abfd, uint64_t offset, struct dwarf2_debug *stash,
1181 struct dwarf2_debug_file *file)
1183 struct abbrev_info **abbrevs;
1184 bfd_byte *abbrev_ptr;
1185 bfd_byte *abbrev_end;
1186 struct abbrev_info *cur_abbrev;
1187 unsigned int abbrev_number, abbrev_name;
1188 unsigned int abbrev_form, hash_number;
1189 size_t amt;
1190 void **slot;
1191 struct abbrev_offset_entry ent = { offset, NULL };
1193 if (ent.offset != offset)
1194 return NULL;
1196 slot = htab_find_slot (file->abbrev_offsets, &ent, INSERT);
1197 if (slot == NULL)
1198 return NULL;
1199 if (*slot != NULL)
1200 return ((struct abbrev_offset_entry *) (*slot))->abbrevs;
1202 if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
1203 file->syms, offset,
1204 &file->dwarf_abbrev_buffer,
1205 &file->dwarf_abbrev_size))
1206 return NULL;
1208 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
1209 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
1210 if (abbrevs == NULL)
1211 return NULL;
1213 abbrev_ptr = file->dwarf_abbrev_buffer + offset;
1214 abbrev_end = file->dwarf_abbrev_buffer + file->dwarf_abbrev_size;
1215 abbrev_number = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1216 false, abbrev_end);
1218 /* Loop until we reach an abbrev number of 0. */
1219 while (abbrev_number)
1221 amt = sizeof (struct abbrev_info);
1222 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
1223 if (cur_abbrev == NULL)
1224 goto fail;
1226 /* Read in abbrev header. */
1227 cur_abbrev->number = abbrev_number;
1228 cur_abbrev->tag = (enum dwarf_tag)
1229 _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1230 false, abbrev_end);
1231 cur_abbrev->has_children = read_1_byte (abfd, &abbrev_ptr, abbrev_end);
1233 /* Now read in declarations. */
1234 for (;;)
1236 /* Initialize it just to avoid a GCC false warning. */
1237 bfd_vma implicit_const = -1;
1239 abbrev_name = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1240 false, abbrev_end);
1241 abbrev_form = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1242 false, abbrev_end);
1243 if (abbrev_form == DW_FORM_implicit_const)
1244 implicit_const = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1245 true, abbrev_end);
1246 if (abbrev_name == 0)
1247 break;
1249 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
1251 struct attr_abbrev *tmp;
1253 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
1254 amt *= sizeof (struct attr_abbrev);
1255 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
1256 if (tmp == NULL)
1257 goto fail;
1258 cur_abbrev->attrs = tmp;
1261 cur_abbrev->attrs[cur_abbrev->num_attrs].name
1262 = (enum dwarf_attribute) abbrev_name;
1263 cur_abbrev->attrs[cur_abbrev->num_attrs].form
1264 = (enum dwarf_form) abbrev_form;
1265 cur_abbrev->attrs[cur_abbrev->num_attrs].implicit_const
1266 = implicit_const;
1267 ++cur_abbrev->num_attrs;
1270 hash_number = abbrev_number % ABBREV_HASH_SIZE;
1271 cur_abbrev->next = abbrevs[hash_number];
1272 abbrevs[hash_number] = cur_abbrev;
1274 /* Get next abbreviation.
1275 Under Irix6 the abbreviations for a compilation unit are not
1276 always properly terminated with an abbrev number of 0.
1277 Exit loop if we encounter an abbreviation which we have
1278 already read (which means we are about to read the abbreviations
1279 for the next compile unit) or if the end of the abbreviation
1280 table is reached. */
1281 if ((size_t) (abbrev_ptr - file->dwarf_abbrev_buffer)
1282 >= file->dwarf_abbrev_size)
1283 break;
1284 abbrev_number = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1285 false, abbrev_end);
1286 if (lookup_abbrev (abbrev_number, abbrevs) != NULL)
1287 break;
1290 *slot = bfd_malloc (sizeof ent);
1291 if (!*slot)
1292 goto fail;
1293 ent.abbrevs = abbrevs;
1294 memcpy (*slot, &ent, sizeof ent);
1295 return abbrevs;
1297 fail:
1298 if (abbrevs != NULL)
1300 size_t i;
1302 for (i = 0; i < ABBREV_HASH_SIZE; i++)
1304 struct abbrev_info *abbrev = abbrevs[i];
1306 while (abbrev)
1308 free (abbrev->attrs);
1309 abbrev = abbrev->next;
1312 free (abbrevs);
1314 return NULL;
1317 /* Returns true if the form is one which has a string value. */
1319 static bool
1320 is_str_form (const struct attribute *attr)
1322 switch (attr->form)
1324 case DW_FORM_string:
1325 case DW_FORM_strp:
1326 case DW_FORM_strx:
1327 case DW_FORM_strx1:
1328 case DW_FORM_strx2:
1329 case DW_FORM_strx3:
1330 case DW_FORM_strx4:
1331 case DW_FORM_line_strp:
1332 case DW_FORM_GNU_strp_alt:
1333 return true;
1335 default:
1336 return false;
1340 /* Returns true if the form is one which has an integer value. */
1342 static bool
1343 is_int_form (const struct attribute *attr)
1345 switch (attr->form)
1347 case DW_FORM_addr:
1348 case DW_FORM_data2:
1349 case DW_FORM_data4:
1350 case DW_FORM_data8:
1351 case DW_FORM_data1:
1352 case DW_FORM_flag:
1353 case DW_FORM_sdata:
1354 case DW_FORM_udata:
1355 case DW_FORM_ref_addr:
1356 case DW_FORM_ref1:
1357 case DW_FORM_ref2:
1358 case DW_FORM_ref4:
1359 case DW_FORM_ref8:
1360 case DW_FORM_ref_udata:
1361 case DW_FORM_sec_offset:
1362 case DW_FORM_flag_present:
1363 case DW_FORM_ref_sig8:
1364 case DW_FORM_addrx:
1365 case DW_FORM_implicit_const:
1366 case DW_FORM_addrx1:
1367 case DW_FORM_addrx2:
1368 case DW_FORM_addrx3:
1369 case DW_FORM_addrx4:
1370 case DW_FORM_GNU_ref_alt:
1371 return true;
1373 default:
1374 return false;
1378 /* Returns true if the form is strx[1-4]. */
1380 static inline bool
1381 is_strx_form (enum dwarf_form form)
1383 return (form == DW_FORM_strx
1384 || form == DW_FORM_strx1
1385 || form == DW_FORM_strx2
1386 || form == DW_FORM_strx3
1387 || form == DW_FORM_strx4);
1390 /* Return true if the form is addrx[1-4]. */
1392 static inline bool
1393 is_addrx_form (enum dwarf_form form)
1395 return (form == DW_FORM_addrx
1396 || form == DW_FORM_addrx1
1397 || form == DW_FORM_addrx2
1398 || form == DW_FORM_addrx3
1399 || form == DW_FORM_addrx4);
1402 /* Returns the address in .debug_addr section using DW_AT_addr_base.
1403 Used to implement DW_FORM_addrx*. */
1404 static uint64_t
1405 read_indexed_address (uint64_t idx, struct comp_unit *unit)
1407 struct dwarf2_debug *stash = unit->stash;
1408 struct dwarf2_debug_file *file = unit->file;
1409 bfd_byte *info_ptr;
1410 size_t offset;
1412 if (stash == NULL)
1413 return 0;
1415 if (!read_section (unit->abfd, &stash->debug_sections[debug_addr],
1416 file->syms, 0,
1417 &file->dwarf_addr_buffer, &file->dwarf_addr_size))
1418 return 0;
1420 if (_bfd_mul_overflow (idx, unit->addr_size, &offset))
1421 return 0;
1423 offset += unit->dwarf_addr_offset;
1424 if (offset < unit->dwarf_addr_offset
1425 || offset > file->dwarf_addr_size
1426 || file->dwarf_addr_size - offset < unit->addr_size)
1427 return 0;
1429 info_ptr = file->dwarf_addr_buffer + offset;
1431 if (unit->addr_size == 4)
1432 return bfd_get_32 (unit->abfd, info_ptr);
1433 else if (unit->addr_size == 8)
1434 return bfd_get_64 (unit->abfd, info_ptr);
1435 else
1436 return 0;
1439 /* Returns the string using DW_AT_str_offsets_base.
1440 Used to implement DW_FORM_strx*. */
1441 static const char *
1442 read_indexed_string (uint64_t idx, struct comp_unit *unit)
1444 struct dwarf2_debug *stash = unit->stash;
1445 struct dwarf2_debug_file *file = unit->file;
1446 bfd_byte *info_ptr;
1447 uint64_t str_offset;
1448 size_t offset;
1450 if (stash == NULL)
1451 return NULL;
1453 if (!read_section (unit->abfd, &stash->debug_sections[debug_str],
1454 file->syms, 0,
1455 &file->dwarf_str_buffer, &file->dwarf_str_size))
1456 return NULL;
1458 if (!read_section (unit->abfd, &stash->debug_sections[debug_str_offsets],
1459 file->syms, 0,
1460 &file->dwarf_str_offsets_buffer,
1461 &file->dwarf_str_offsets_size))
1462 return NULL;
1464 if (_bfd_mul_overflow (idx, unit->offset_size, &offset))
1465 return NULL;
1467 offset += unit->dwarf_str_offset;
1468 if (offset < unit->dwarf_str_offset
1469 || offset > file->dwarf_str_offsets_size
1470 || file->dwarf_str_offsets_size - offset < unit->offset_size)
1471 return NULL;
1473 info_ptr = file->dwarf_str_offsets_buffer + offset;
1475 if (unit->offset_size == 4)
1476 str_offset = bfd_get_32 (unit->abfd, info_ptr);
1477 else if (unit->offset_size == 8)
1478 str_offset = bfd_get_64 (unit->abfd, info_ptr);
1479 else
1480 return NULL;
1482 if (str_offset >= file->dwarf_str_size)
1483 return NULL;
1484 return (const char *) file->dwarf_str_buffer + str_offset;
1487 /* Read and fill in the value of attribute ATTR as described by FORM.
1488 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1489 Returns an updated INFO_PTR taking into account the amount of data read. */
1491 static bfd_byte *
1492 read_attribute_value (struct attribute * attr,
1493 unsigned form,
1494 bfd_vma implicit_const,
1495 struct comp_unit * unit,
1496 bfd_byte * info_ptr,
1497 bfd_byte * info_ptr_end)
1499 bfd *abfd = unit->abfd;
1500 size_t amt;
1502 if (info_ptr >= info_ptr_end && form != DW_FORM_flag_present)
1504 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1505 bfd_set_error (bfd_error_bad_value);
1506 return NULL;
1509 attr->form = (enum dwarf_form) form;
1511 switch (form)
1513 case DW_FORM_flag_present:
1514 attr->u.val = 1;
1515 break;
1516 case DW_FORM_ref_addr:
1517 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1518 DWARF3. */
1519 if (unit->version >= 3)
1521 if (unit->offset_size == 4)
1522 attr->u.val = read_4_bytes (unit->abfd, &info_ptr, info_ptr_end);
1523 else
1524 attr->u.val = read_8_bytes (unit->abfd, &info_ptr, info_ptr_end);
1525 break;
1527 /* FALLTHROUGH */
1528 case DW_FORM_addr:
1529 attr->u.val = read_address (unit, &info_ptr, info_ptr_end);
1530 break;
1531 case DW_FORM_GNU_ref_alt:
1532 case DW_FORM_sec_offset:
1533 if (unit->offset_size == 4)
1534 attr->u.val = read_4_bytes (unit->abfd, &info_ptr, info_ptr_end);
1535 else
1536 attr->u.val = read_8_bytes (unit->abfd, &info_ptr, info_ptr_end);
1537 break;
1538 case DW_FORM_block2:
1539 amt = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1540 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1541 if (attr->u.blk == NULL)
1542 return NULL;
1543 break;
1544 case DW_FORM_block4:
1545 amt = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1546 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1547 if (attr->u.blk == NULL)
1548 return NULL;
1549 break;
1550 case DW_FORM_ref1:
1551 case DW_FORM_flag:
1552 case DW_FORM_data1:
1553 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1554 break;
1555 case DW_FORM_addrx1:
1556 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1557 /* dwarf_addr_offset value 0 indicates the attribute DW_AT_addr_base
1558 is not yet read. */
1559 if (unit->dwarf_addr_offset != 0)
1560 attr->u.val = read_indexed_address (attr->u.val, unit);
1561 break;
1562 case DW_FORM_data2:
1563 case DW_FORM_ref2:
1564 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1565 break;
1566 case DW_FORM_addrx2:
1567 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1568 if (unit->dwarf_addr_offset != 0)
1569 attr->u.val = read_indexed_address (attr->u.val, unit);
1570 break;
1571 case DW_FORM_addrx3:
1572 attr->u.val = read_3_bytes (abfd, &info_ptr, info_ptr_end);
1573 if (unit->dwarf_addr_offset != 0)
1574 attr->u.val = read_indexed_address(attr->u.val, unit);
1575 break;
1576 case DW_FORM_ref4:
1577 case DW_FORM_data4:
1578 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1579 break;
1580 case DW_FORM_addrx4:
1581 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1582 if (unit->dwarf_addr_offset != 0)
1583 attr->u.val = read_indexed_address (attr->u.val, unit);
1584 break;
1585 case DW_FORM_data8:
1586 case DW_FORM_ref8:
1587 case DW_FORM_ref_sig8:
1588 attr->u.val = read_8_bytes (abfd, &info_ptr, info_ptr_end);
1589 break;
1590 case DW_FORM_string:
1591 attr->u.str = read_string (&info_ptr, info_ptr_end);
1592 break;
1593 case DW_FORM_strp:
1594 attr->u.str = read_indirect_string (unit, &info_ptr, info_ptr_end);
1595 break;
1596 case DW_FORM_line_strp:
1597 attr->u.str = read_indirect_line_string (unit, &info_ptr, info_ptr_end);
1598 break;
1599 case DW_FORM_GNU_strp_alt:
1600 attr->u.str = read_alt_indirect_string (unit, &info_ptr, info_ptr_end);
1601 break;
1602 case DW_FORM_strx1:
1603 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1604 /* dwarf_str_offset value 0 indicates the attribute DW_AT_str_offsets_base
1605 is not yet read. */
1606 if (unit->dwarf_str_offset != 0)
1607 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1608 else
1609 attr->u.str = NULL;
1610 break;
1611 case DW_FORM_strx2:
1612 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1613 if (unit->dwarf_str_offset != 0)
1614 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1615 else
1616 attr->u.str = NULL;
1617 break;
1618 case DW_FORM_strx3:
1619 attr->u.val = read_3_bytes (abfd, &info_ptr, info_ptr_end);
1620 if (unit->dwarf_str_offset != 0)
1621 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1622 else
1623 attr->u.str = NULL;
1624 break;
1625 case DW_FORM_strx4:
1626 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1627 if (unit->dwarf_str_offset != 0)
1628 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1629 else
1630 attr->u.str = NULL;
1631 break;
1632 case DW_FORM_strx:
1633 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1634 false, info_ptr_end);
1635 if (unit->dwarf_str_offset != 0)
1636 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1637 else
1638 attr->u.str = NULL;
1639 break;
1640 case DW_FORM_exprloc:
1641 case DW_FORM_block:
1642 amt = _bfd_safe_read_leb128 (abfd, &info_ptr,
1643 false, info_ptr_end);
1644 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1645 if (attr->u.blk == NULL)
1646 return NULL;
1647 break;
1648 case DW_FORM_block1:
1649 amt = read_1_byte (abfd, &info_ptr, info_ptr_end);
1650 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1651 if (attr->u.blk == NULL)
1652 return NULL;
1653 break;
1654 case DW_FORM_sdata:
1655 attr->u.sval = _bfd_safe_read_leb128 (abfd, &info_ptr,
1656 true, info_ptr_end);
1657 break;
1659 case DW_FORM_rnglistx:
1660 case DW_FORM_loclistx:
1661 /* FIXME: Add support for these forms! */
1662 /* Fall through. */
1663 case DW_FORM_ref_udata:
1664 case DW_FORM_udata:
1665 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1666 false, info_ptr_end);
1667 break;
1668 case DW_FORM_addrx:
1669 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1670 false, info_ptr_end);
1671 if (unit->dwarf_addr_offset != 0)
1672 attr->u.val = read_indexed_address (attr->u.val, unit);
1673 break;
1674 case DW_FORM_indirect:
1675 form = _bfd_safe_read_leb128 (abfd, &info_ptr,
1676 false, info_ptr_end);
1677 if (form == DW_FORM_implicit_const)
1678 implicit_const = _bfd_safe_read_leb128 (abfd, &info_ptr,
1679 true, info_ptr_end);
1680 info_ptr = read_attribute_value (attr, form, implicit_const, unit,
1681 info_ptr, info_ptr_end);
1682 break;
1683 case DW_FORM_implicit_const:
1684 attr->form = DW_FORM_sdata;
1685 attr->u.sval = implicit_const;
1686 break;
1687 case DW_FORM_data16:
1688 /* This is really a "constant", but there is no way to store that
1689 so pretend it is a 16 byte block instead. */
1690 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, 16);
1691 if (attr->u.blk == NULL)
1692 return NULL;
1693 break;
1695 default:
1696 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1697 form);
1698 bfd_set_error (bfd_error_bad_value);
1699 return NULL;
1701 return info_ptr;
1704 /* Read an attribute described by an abbreviated attribute. */
1706 static bfd_byte *
1707 read_attribute (struct attribute * attr,
1708 struct attr_abbrev * abbrev,
1709 struct comp_unit * unit,
1710 bfd_byte * info_ptr,
1711 bfd_byte * info_ptr_end)
1713 attr->name = abbrev->name;
1714 info_ptr = read_attribute_value (attr, abbrev->form, abbrev->implicit_const,
1715 unit, info_ptr, info_ptr_end);
1716 return info_ptr;
1719 /* Return mangling style given LANG. */
1721 static int
1722 mangle_style (int lang)
1724 switch (lang)
1726 case DW_LANG_Ada83:
1727 case DW_LANG_Ada95:
1728 return DMGL_GNAT;
1730 case DW_LANG_C_plus_plus:
1731 case DW_LANG_C_plus_plus_03:
1732 case DW_LANG_C_plus_plus_11:
1733 case DW_LANG_C_plus_plus_14:
1734 return DMGL_GNU_V3;
1736 case DW_LANG_Java:
1737 return DMGL_JAVA;
1739 case DW_LANG_D:
1740 return DMGL_DLANG;
1742 case DW_LANG_Rust:
1743 case DW_LANG_Rust_old:
1744 return DMGL_RUST;
1746 default:
1747 return DMGL_AUTO;
1749 case DW_LANG_C89:
1750 case DW_LANG_C:
1751 case DW_LANG_Cobol74:
1752 case DW_LANG_Cobol85:
1753 case DW_LANG_Fortran77:
1754 case DW_LANG_Pascal83:
1755 case DW_LANG_PLI:
1756 case DW_LANG_C99:
1757 case DW_LANG_UPC:
1758 case DW_LANG_C11:
1759 case DW_LANG_Mips_Assembler:
1760 case DW_LANG_Upc:
1761 case DW_LANG_HP_Basic91:
1762 case DW_LANG_HP_IMacro:
1763 case DW_LANG_HP_Assembler:
1764 return 0;
1768 /* Source line information table routines. */
1770 #define FILE_ALLOC_CHUNK 5
1771 #define DIR_ALLOC_CHUNK 5
1773 struct line_info
1775 struct line_info * prev_line;
1776 bfd_vma address;
1777 char * filename;
1778 unsigned int line;
1779 unsigned int column;
1780 unsigned int discriminator;
1781 unsigned char op_index;
1782 unsigned char end_sequence; /* End of (sequential) code sequence. */
1785 struct fileinfo
1787 char * name;
1788 unsigned int dir;
1789 unsigned int time;
1790 unsigned int size;
1793 struct line_sequence
1795 bfd_vma low_pc;
1796 struct line_sequence* prev_sequence;
1797 struct line_info* last_line; /* Largest VMA. */
1798 struct line_info** line_info_lookup;
1799 bfd_size_type num_lines;
1802 struct line_info_table
1804 bfd * abfd;
1805 unsigned int num_files;
1806 unsigned int num_dirs;
1807 unsigned int num_sequences;
1808 bool use_dir_and_file_0;
1809 char * comp_dir;
1810 char ** dirs;
1811 struct fileinfo* files;
1812 struct line_sequence* sequences;
1813 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
1816 /* Remember some information about each function. If the function is
1817 inlined (DW_TAG_inlined_subroutine) it may have two additional
1818 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1819 source code location where this function was inlined. */
1821 struct funcinfo
1823 /* Pointer to previous function in list of all functions. */
1824 struct funcinfo *prev_func;
1825 /* Pointer to function one scope higher. */
1826 struct funcinfo *caller_func;
1827 /* Source location file name where caller_func inlines this func. */
1828 char *caller_file;
1829 /* Source location file name. */
1830 char *file;
1831 /* Source location line number where caller_func inlines this func. */
1832 int caller_line;
1833 /* Source location line number. */
1834 int line;
1835 int tag;
1836 bool is_linkage;
1837 const char *name;
1838 struct arange arange;
1839 /* The offset of the funcinfo from the start of the unit. */
1840 uint64_t unit_offset;
1843 struct lookup_funcinfo
1845 /* Function information corresponding to this lookup table entry. */
1846 struct funcinfo *funcinfo;
1848 /* The lowest address for this specific function. */
1849 bfd_vma low_addr;
1851 /* The highest address of this function before the lookup table is sorted.
1852 The highest address of all prior functions after the lookup table is
1853 sorted, which is used for binary search. */
1854 bfd_vma high_addr;
1855 /* Index of this function, used to ensure qsort is stable. */
1856 unsigned int idx;
1859 struct varinfo
1861 /* Pointer to previous variable in list of all variables. */
1862 struct varinfo *prev_var;
1863 /* The offset of the varinfo from the start of the unit. */
1864 uint64_t unit_offset;
1865 /* Source location file name. */
1866 char *file;
1867 /* Source location line number. */
1868 int line;
1869 /* The type of this variable. */
1870 int tag;
1871 /* The name of the variable, if it has one. */
1872 const char *name;
1873 /* The address of the variable. */
1874 bfd_vma addr;
1875 /* Is this a stack variable? */
1876 bool stack;
1879 /* Return TRUE if NEW_LINE should sort after LINE. */
1881 static inline bool
1882 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1884 return (new_line->address > line->address
1885 || (new_line->address == line->address
1886 && new_line->op_index > line->op_index));
1890 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1891 that the list is sorted. Note that the line_info list is sorted from
1892 highest to lowest VMA (with possible duplicates); that is,
1893 line_info->prev_line always accesses an equal or smaller VMA. */
1895 static bool
1896 add_line_info (struct line_info_table *table,
1897 bfd_vma address,
1898 unsigned char op_index,
1899 char *filename,
1900 unsigned int line,
1901 unsigned int column,
1902 unsigned int discriminator,
1903 int end_sequence)
1905 size_t amt = sizeof (struct line_info);
1906 struct line_sequence* seq = table->sequences;
1907 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
1909 if (info == NULL)
1910 return false;
1912 /* Set member data of 'info'. */
1913 info->prev_line = NULL;
1914 info->address = address;
1915 info->op_index = op_index;
1916 info->line = line;
1917 info->column = column;
1918 info->discriminator = discriminator;
1919 info->end_sequence = end_sequence;
1921 if (filename && filename[0])
1923 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1924 if (info->filename == NULL)
1925 return false;
1926 strcpy (info->filename, filename);
1928 else
1929 info->filename = NULL;
1931 /* Find the correct location for 'info'. Normally we will receive
1932 new line_info data 1) in order and 2) with increasing VMAs.
1933 However some compilers break the rules (cf. decode_line_info) and
1934 so we include some heuristics for quickly finding the correct
1935 location for 'info'. In particular, these heuristics optimize for
1936 the common case in which the VMA sequence that we receive is a
1937 list of locally sorted VMAs such as
1938 p...z a...j (where a < j < p < z)
1940 Note: table->lcl_head is used to head an *actual* or *possible*
1941 sub-sequence within the list (such as a...j) that is not directly
1942 headed by table->last_line
1944 Note: we may receive duplicate entries from 'decode_line_info'. */
1946 if (seq
1947 && seq->last_line->address == address
1948 && seq->last_line->op_index == op_index
1949 && seq->last_line->end_sequence == end_sequence)
1951 /* We only keep the last entry with the same address and end
1952 sequence. See PR ld/4986. */
1953 if (table->lcl_head == seq->last_line)
1954 table->lcl_head = info;
1955 info->prev_line = seq->last_line->prev_line;
1956 seq->last_line = info;
1958 else if (!seq || seq->last_line->end_sequence)
1960 /* Start a new line sequence. */
1961 amt = sizeof (struct line_sequence);
1962 seq = (struct line_sequence *) bfd_malloc (amt);
1963 if (seq == NULL)
1964 return false;
1965 seq->low_pc = address;
1966 seq->prev_sequence = table->sequences;
1967 seq->last_line = info;
1968 table->lcl_head = info;
1969 table->sequences = seq;
1970 table->num_sequences++;
1972 else if (info->end_sequence
1973 || new_line_sorts_after (info, seq->last_line))
1975 /* Normal case: add 'info' to the beginning of the current sequence. */
1976 info->prev_line = seq->last_line;
1977 seq->last_line = info;
1979 /* lcl_head: initialize to head a *possible* sequence at the end. */
1980 if (!table->lcl_head)
1981 table->lcl_head = info;
1983 else if (!new_line_sorts_after (info, table->lcl_head)
1984 && (!table->lcl_head->prev_line
1985 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1987 /* Abnormal but easy: lcl_head is the head of 'info'. */
1988 info->prev_line = table->lcl_head->prev_line;
1989 table->lcl_head->prev_line = info;
1991 else
1993 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1994 are valid heads for 'info'. Reset 'lcl_head'. */
1995 struct line_info* li2 = seq->last_line; /* Always non-NULL. */
1996 struct line_info* li1 = li2->prev_line;
1998 while (li1)
2000 if (!new_line_sorts_after (info, li2)
2001 && new_line_sorts_after (info, li1))
2002 break;
2004 li2 = li1; /* always non-NULL */
2005 li1 = li1->prev_line;
2007 table->lcl_head = li2;
2008 info->prev_line = table->lcl_head->prev_line;
2009 table->lcl_head->prev_line = info;
2010 if (address < seq->low_pc)
2011 seq->low_pc = address;
2013 return true;
2016 /* Extract a fully qualified filename from a line info table.
2017 The returned string has been malloc'ed and it is the caller's
2018 responsibility to free it. */
2020 static char *
2021 concat_filename (struct line_info_table *table, unsigned int file)
2023 char *filename;
2025 /* Pre DWARF-5 entry 0 in the directory and filename tables was not used.
2026 So in order to save space in the tables used here the info for, eg
2027 directory 1 is stored in slot 0 of the directory table, directory 2
2028 in slot 1 and so on.
2030 Starting with DWARF-5 the 0'th entry is used so there is a one to one
2031 mapping between DWARF slots and internal table entries. */
2032 if (! table->use_dir_and_file_0)
2034 /* Pre DWARF-5, FILE == 0 means unknown. */
2035 if (file == 0)
2036 return strdup ("<unknown>");
2037 -- file;
2040 if (table == NULL || file >= table->num_files)
2042 _bfd_error_handler
2043 (_("DWARF error: mangled line number section (bad file number)"));
2044 return strdup ("<unknown>");
2047 filename = table->files[file].name;
2049 if (filename == NULL)
2050 return strdup ("<unknown>");
2052 if (!IS_ABSOLUTE_PATH (filename))
2054 char *dir_name = NULL;
2055 char *subdir_name = NULL;
2056 char *name;
2057 size_t len;
2058 unsigned int dir = table->files[file].dir;
2060 if (!table->use_dir_and_file_0)
2061 --dir;
2062 /* Wrapping from 0 to -1u above gives the intended result with
2063 the test below of leaving subdir_name NULL for pre-DWARF5 dir
2064 of 0. */
2065 /* PR 17512: file: 0317e960, file: 7f3d2e4b. */
2066 if (dir < table->num_dirs)
2067 subdir_name = table->dirs[dir];
2069 if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
2070 dir_name = table->comp_dir;
2072 if (!dir_name)
2074 dir_name = subdir_name;
2075 subdir_name = NULL;
2078 if (!dir_name)
2079 return strdup (filename);
2081 len = strlen (dir_name) + strlen (filename) + 2;
2083 if (subdir_name)
2085 len += strlen (subdir_name) + 1;
2086 name = (char *) bfd_malloc (len);
2087 if (name)
2088 sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
2090 else
2092 name = (char *) bfd_malloc (len);
2093 if (name)
2094 sprintf (name, "%s/%s", dir_name, filename);
2097 return name;
2100 return strdup (filename);
2103 /* Number of bits in a bfd_vma. */
2104 #define VMA_BITS (8 * sizeof (bfd_vma))
2106 /* Check whether [low1, high1) can be combined with [low2, high2),
2107 i.e., they touch or overlap. */
2109 static bool
2110 ranges_overlap (bfd_vma low1,
2111 bfd_vma high1,
2112 bfd_vma low2,
2113 bfd_vma high2)
2115 if (low1 == low2 || high1 == high2)
2116 return true;
2118 /* Sort so that low1 is below low2. */
2119 if (low1 > low2)
2121 bfd_vma tmp;
2123 tmp = low1;
2124 low1 = low2;
2125 low2 = tmp;
2127 tmp = high1;
2128 high1 = high2;
2129 high2 = tmp;
2132 /* We touch iff low2 == high1.
2133 We overlap iff low2 is within [low1, high1). */
2134 return low2 <= high1;
2137 /* Insert an address range in the trie mapping addresses to compilation units.
2138 Will return the new trie node (usually the same as is being sent in, but
2139 in case of a leaf-to-interior conversion, or expansion of a leaf, it may be
2140 different), or NULL on failure. */
2142 static struct trie_node *
2143 insert_arange_in_trie (bfd *abfd,
2144 struct trie_node *trie,
2145 bfd_vma trie_pc,
2146 unsigned int trie_pc_bits,
2147 struct comp_unit *unit,
2148 bfd_vma low_pc,
2149 bfd_vma high_pc)
2151 bfd_vma clamped_low_pc, clamped_high_pc;
2152 int ch, from_ch, to_ch;
2153 bool is_full_leaf = false;
2154 bool splitting_leaf_will_help = false;
2156 /* See if we can extend any of the existing ranges. This merging
2157 isn't perfect (if merging opens up the possibility of merging two existing
2158 ranges, we won't find them), but it takes the majority of the cases. */
2159 if (trie->num_room_in_leaf > 0)
2161 struct trie_leaf *leaf = (struct trie_leaf *) trie;
2162 unsigned int i;
2164 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2166 if (leaf->ranges[i].unit == unit
2167 && ranges_overlap (low_pc, high_pc,
2168 leaf->ranges[i].low_pc,
2169 leaf->ranges[i].high_pc))
2171 if (low_pc < leaf->ranges[i].low_pc)
2172 leaf->ranges[i].low_pc = low_pc;
2173 if (high_pc > leaf->ranges[i].high_pc)
2174 leaf->ranges[i].high_pc = high_pc;
2175 return trie;
2179 is_full_leaf = leaf->num_stored_in_leaf == trie->num_room_in_leaf;
2181 if (is_full_leaf && trie_pc_bits < VMA_BITS)
2183 /* See if we have at least one leaf that does _not_ cover the
2184 entire bucket, so that splitting will actually reduce the number
2185 of elements in at least one of the child nodes. (For simplicity,
2186 we don't test the range we're inserting, but it will be counted
2187 on the next insertion where we're full, if any.) */
2188 bfd_vma bucket_high_pc =
2189 trie_pc + ((bfd_vma) -1 >> trie_pc_bits); /* Inclusive. */
2190 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2192 if (leaf->ranges[i].low_pc > trie_pc
2193 || leaf->ranges[i].high_pc <= bucket_high_pc)
2195 splitting_leaf_will_help = true;
2196 break;
2202 /* If we're a leaf with no more room and we're _not_ at the bottom,
2203 convert to an interior node. */
2204 if (is_full_leaf && splitting_leaf_will_help)
2206 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
2207 unsigned int i;
2209 trie = bfd_zalloc (abfd, sizeof (struct trie_interior));
2210 if (!trie)
2211 return NULL;
2212 is_full_leaf = false;
2214 /* TODO: If we wanted to save a little more memory at the cost of
2215 complexity, we could have reused the old leaf node as one of the
2216 children of the new interior node, instead of throwing it away. */
2217 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2219 if (!insert_arange_in_trie (abfd, trie, trie_pc, trie_pc_bits,
2220 leaf->ranges[i].unit, leaf->ranges[i].low_pc,
2221 leaf->ranges[i].high_pc))
2222 return NULL;
2226 /* If we're a leaf with no more room and we _are_ at the bottom
2227 (or splitting it won't help), we have no choice but to just
2228 make it larger. */
2229 if (is_full_leaf)
2231 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
2232 unsigned int new_room_in_leaf = trie->num_room_in_leaf * 2;
2233 struct trie_leaf *new_leaf;
2234 size_t amt = sizeof (*leaf) + new_room_in_leaf * sizeof (leaf->ranges[0]);
2235 new_leaf = bfd_zalloc (abfd, amt);
2236 new_leaf->head.num_room_in_leaf = new_room_in_leaf;
2237 new_leaf->num_stored_in_leaf = leaf->num_stored_in_leaf;
2239 memcpy (new_leaf->ranges,
2240 leaf->ranges,
2241 leaf->num_stored_in_leaf * sizeof (leaf->ranges[0]));
2242 trie = &new_leaf->head;
2243 is_full_leaf = false;
2245 /* Now the insert below will go through. */
2248 /* If we're a leaf (now with room), we can just insert at the end. */
2249 if (trie->num_room_in_leaf > 0)
2251 struct trie_leaf *leaf = (struct trie_leaf *) trie;
2253 unsigned int i = leaf->num_stored_in_leaf++;
2254 leaf->ranges[i].unit = unit;
2255 leaf->ranges[i].low_pc = low_pc;
2256 leaf->ranges[i].high_pc = high_pc;
2257 return trie;
2260 /* Now we are definitely an interior node, so recurse into all
2261 the relevant buckets. */
2263 /* Clamp the range to the current trie bucket. */
2264 clamped_low_pc = low_pc;
2265 clamped_high_pc = high_pc;
2266 if (trie_pc_bits > 0)
2268 bfd_vma bucket_high_pc =
2269 trie_pc + ((bfd_vma) -1 >> trie_pc_bits); /* Inclusive. */
2270 if (clamped_low_pc < trie_pc)
2271 clamped_low_pc = trie_pc;
2272 if (clamped_high_pc > bucket_high_pc)
2273 clamped_high_pc = bucket_high_pc;
2276 /* Insert the ranges in all buckets that it spans. */
2277 from_ch = (clamped_low_pc >> (VMA_BITS - trie_pc_bits - 8)) & 0xff;
2278 to_ch = ((clamped_high_pc - 1) >> (VMA_BITS - trie_pc_bits - 8)) & 0xff;
2279 for (ch = from_ch; ch <= to_ch; ++ch)
2281 struct trie_interior *interior = (struct trie_interior *) trie;
2282 struct trie_node *child = interior->children[ch];
2284 if (child == NULL)
2286 child = alloc_trie_leaf (abfd);
2287 if (!child)
2288 return NULL;
2290 bfd_vma bucket = (bfd_vma) ch << (VMA_BITS - trie_pc_bits - 8);
2291 child = insert_arange_in_trie (abfd,
2292 child,
2293 trie_pc + bucket,
2294 trie_pc_bits + 8,
2295 unit,
2296 low_pc,
2297 high_pc);
2298 if (!child)
2299 return NULL;
2301 interior->children[ch] = child;
2304 return trie;
2307 static bool
2308 arange_add (struct comp_unit *unit, struct arange *first_arange,
2309 struct trie_node **trie_root, bfd_vma low_pc, bfd_vma high_pc)
2311 struct arange *arange;
2313 /* Ignore empty ranges. */
2314 if (low_pc == high_pc)
2315 return true;
2317 if (trie_root != NULL)
2319 *trie_root = insert_arange_in_trie (unit->file->bfd_ptr,
2320 *trie_root,
2323 unit,
2324 low_pc,
2325 high_pc);
2326 if (*trie_root == NULL)
2327 return false;
2330 /* If the first arange is empty, use it. */
2331 if (first_arange->high == 0)
2333 first_arange->low = low_pc;
2334 first_arange->high = high_pc;
2335 return true;
2338 /* Next see if we can cheaply extend an existing range. */
2339 arange = first_arange;
2342 if (low_pc == arange->high)
2344 arange->high = high_pc;
2345 return true;
2347 if (high_pc == arange->low)
2349 arange->low = low_pc;
2350 return true;
2352 arange = arange->next;
2354 while (arange);
2356 /* Need to allocate a new arange and insert it into the arange list.
2357 Order isn't significant, so just insert after the first arange. */
2358 arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
2359 if (arange == NULL)
2360 return false;
2361 arange->low = low_pc;
2362 arange->high = high_pc;
2363 arange->next = first_arange->next;
2364 first_arange->next = arange;
2365 return true;
2368 /* Compare function for line sequences. */
2370 static int
2371 compare_sequences (const void* a, const void* b)
2373 const struct line_sequence* seq1 = a;
2374 const struct line_sequence* seq2 = b;
2376 /* Sort by low_pc as the primary key. */
2377 if (seq1->low_pc < seq2->low_pc)
2378 return -1;
2379 if (seq1->low_pc > seq2->low_pc)
2380 return 1;
2382 /* If low_pc values are equal, sort in reverse order of
2383 high_pc, so that the largest region comes first. */
2384 if (seq1->last_line->address < seq2->last_line->address)
2385 return 1;
2386 if (seq1->last_line->address > seq2->last_line->address)
2387 return -1;
2389 if (seq1->last_line->op_index < seq2->last_line->op_index)
2390 return 1;
2391 if (seq1->last_line->op_index > seq2->last_line->op_index)
2392 return -1;
2394 /* num_lines is initially an index, to make the sort stable. */
2395 if (seq1->num_lines < seq2->num_lines)
2396 return -1;
2397 if (seq1->num_lines > seq2->num_lines)
2398 return 1;
2399 return 0;
2402 /* Construct the line information table for quick lookup. */
2404 static bool
2405 build_line_info_table (struct line_info_table * table,
2406 struct line_sequence * seq)
2408 size_t amt;
2409 struct line_info **line_info_lookup;
2410 struct line_info *each_line;
2411 unsigned int num_lines;
2412 unsigned int line_index;
2414 if (seq->line_info_lookup != NULL)
2415 return true;
2417 /* Count the number of line information entries. We could do this while
2418 scanning the debug information, but some entries may be added via
2419 lcl_head without having a sequence handy to increment the number of
2420 lines. */
2421 num_lines = 0;
2422 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
2423 num_lines++;
2425 seq->num_lines = num_lines;
2426 if (num_lines == 0)
2427 return true;
2429 /* Allocate space for the line information lookup table. */
2430 amt = sizeof (struct line_info*) * num_lines;
2431 line_info_lookup = (struct line_info**) bfd_alloc (table->abfd, amt);
2432 seq->line_info_lookup = line_info_lookup;
2433 if (line_info_lookup == NULL)
2434 return false;
2436 /* Create the line information lookup table. */
2437 line_index = num_lines;
2438 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
2439 line_info_lookup[--line_index] = each_line;
2441 BFD_ASSERT (line_index == 0);
2442 return true;
2445 /* Sort the line sequences for quick lookup. */
2447 static bool
2448 sort_line_sequences (struct line_info_table* table)
2450 size_t amt;
2451 struct line_sequence *sequences;
2452 struct line_sequence *seq;
2453 unsigned int n = 0;
2454 unsigned int num_sequences = table->num_sequences;
2455 bfd_vma last_high_pc;
2457 if (num_sequences == 0)
2458 return true;
2460 /* Allocate space for an array of sequences. */
2461 amt = sizeof (struct line_sequence) * num_sequences;
2462 sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
2463 if (sequences == NULL)
2464 return false;
2466 /* Copy the linked list into the array, freeing the original nodes. */
2467 seq = table->sequences;
2468 for (n = 0; n < num_sequences; n++)
2470 struct line_sequence* last_seq = seq;
2472 BFD_ASSERT (seq);
2473 sequences[n].low_pc = seq->low_pc;
2474 sequences[n].prev_sequence = NULL;
2475 sequences[n].last_line = seq->last_line;
2476 sequences[n].line_info_lookup = NULL;
2477 sequences[n].num_lines = n;
2478 seq = seq->prev_sequence;
2479 free (last_seq);
2481 BFD_ASSERT (seq == NULL);
2483 qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
2485 /* Make the list binary-searchable by trimming overlapping entries
2486 and removing nested entries. */
2487 num_sequences = 1;
2488 last_high_pc = sequences[0].last_line->address;
2489 for (n = 1; n < table->num_sequences; n++)
2491 if (sequences[n].low_pc < last_high_pc)
2493 if (sequences[n].last_line->address <= last_high_pc)
2494 /* Skip nested entries. */
2495 continue;
2497 /* Trim overlapping entries. */
2498 sequences[n].low_pc = last_high_pc;
2500 last_high_pc = sequences[n].last_line->address;
2501 if (n > num_sequences)
2503 /* Close up the gap. */
2504 sequences[num_sequences].low_pc = sequences[n].low_pc;
2505 sequences[num_sequences].last_line = sequences[n].last_line;
2507 num_sequences++;
2510 table->sequences = sequences;
2511 table->num_sequences = num_sequences;
2512 return true;
2515 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
2517 static bool
2518 line_info_add_include_dir (struct line_info_table *table, char *cur_dir)
2520 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
2522 char **tmp;
2523 size_t amt;
2525 amt = table->num_dirs + DIR_ALLOC_CHUNK;
2526 amt *= sizeof (char *);
2528 tmp = (char **) bfd_realloc (table->dirs, amt);
2529 if (tmp == NULL)
2530 return false;
2531 table->dirs = tmp;
2534 table->dirs[table->num_dirs++] = cur_dir;
2535 return true;
2538 static bool
2539 line_info_add_include_dir_stub (struct line_info_table *table, char *cur_dir,
2540 unsigned int dir ATTRIBUTE_UNUSED,
2541 unsigned int xtime ATTRIBUTE_UNUSED,
2542 unsigned int size ATTRIBUTE_UNUSED)
2544 return line_info_add_include_dir (table, cur_dir);
2547 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
2549 static bool
2550 line_info_add_file_name (struct line_info_table *table, char *cur_file,
2551 unsigned int dir, unsigned int xtime,
2552 unsigned int size)
2554 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
2556 struct fileinfo *tmp;
2557 size_t amt;
2559 amt = table->num_files + FILE_ALLOC_CHUNK;
2560 amt *= sizeof (struct fileinfo);
2562 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
2563 if (tmp == NULL)
2564 return false;
2565 table->files = tmp;
2568 table->files[table->num_files].name = cur_file;
2569 table->files[table->num_files].dir = dir;
2570 table->files[table->num_files].time = xtime;
2571 table->files[table->num_files].size = size;
2572 table->num_files++;
2573 return true;
2576 /* Read directory or file name entry format, starting with byte of
2577 format count entries, ULEB128 pairs of entry formats, ULEB128 of
2578 entries count and the entries themselves in the described entry
2579 format. */
2581 static bool
2582 read_formatted_entries (struct comp_unit *unit, bfd_byte **bufp,
2583 bfd_byte *buf_end, struct line_info_table *table,
2584 bool (*callback) (struct line_info_table *table,
2585 char *cur_file,
2586 unsigned int dir,
2587 unsigned int time,
2588 unsigned int size))
2590 bfd *abfd = unit->abfd;
2591 bfd_byte format_count, formati;
2592 bfd_vma data_count, datai;
2593 bfd_byte *buf = *bufp;
2594 bfd_byte *format_header_data;
2596 format_count = read_1_byte (abfd, &buf, buf_end);
2597 format_header_data = buf;
2598 for (formati = 0; formati < format_count; formati++)
2600 _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2601 _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2604 data_count = _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2605 if (format_count == 0 && data_count != 0)
2607 _bfd_error_handler (_("DWARF error: zero format count"));
2608 bfd_set_error (bfd_error_bad_value);
2609 return false;
2612 /* PR 22210. Paranoia check. Don't bother running the loop
2613 if we know that we are going to run out of buffer. */
2614 if (data_count > (bfd_vma) (buf_end - buf))
2616 _bfd_error_handler
2617 (_("DWARF error: data count (%" PRIx64 ") larger than buffer size"),
2618 (uint64_t) data_count);
2619 bfd_set_error (bfd_error_bad_value);
2620 return false;
2623 for (datai = 0; datai < data_count; datai++)
2625 bfd_byte *format = format_header_data;
2626 struct fileinfo fe;
2628 memset (&fe, 0, sizeof fe);
2629 for (formati = 0; formati < format_count; formati++)
2631 bfd_vma content_type, form;
2632 char *string_trash;
2633 char **stringp = &string_trash;
2634 unsigned int uint_trash, *uintp = &uint_trash;
2635 struct attribute attr;
2637 content_type = _bfd_safe_read_leb128 (abfd, &format, false, buf_end);
2638 switch (content_type)
2640 case DW_LNCT_path:
2641 stringp = &fe.name;
2642 break;
2643 case DW_LNCT_directory_index:
2644 uintp = &fe.dir;
2645 break;
2646 case DW_LNCT_timestamp:
2647 uintp = &fe.time;
2648 break;
2649 case DW_LNCT_size:
2650 uintp = &fe.size;
2651 break;
2652 case DW_LNCT_MD5:
2653 break;
2654 default:
2655 _bfd_error_handler
2656 (_("DWARF error: unknown format content type %" PRIu64),
2657 (uint64_t) content_type);
2658 bfd_set_error (bfd_error_bad_value);
2659 return false;
2662 form = _bfd_safe_read_leb128 (abfd, &format, false, buf_end);
2663 buf = read_attribute_value (&attr, form, 0, unit, buf, buf_end);
2664 if (buf == NULL)
2665 return false;
2666 switch (form)
2668 case DW_FORM_string:
2669 case DW_FORM_line_strp:
2670 case DW_FORM_strx:
2671 case DW_FORM_strx1:
2672 case DW_FORM_strx2:
2673 case DW_FORM_strx3:
2674 case DW_FORM_strx4:
2675 *stringp = attr.u.str;
2676 break;
2678 case DW_FORM_data1:
2679 case DW_FORM_data2:
2680 case DW_FORM_data4:
2681 case DW_FORM_data8:
2682 case DW_FORM_udata:
2683 *uintp = attr.u.val;
2684 break;
2686 case DW_FORM_data16:
2687 /* MD5 data is in the attr.blk, but we are ignoring those. */
2688 break;
2692 if (!callback (table, fe.name, fe.dir, fe.time, fe.size))
2693 return false;
2696 *bufp = buf;
2697 return true;
2700 /* Decode the line number information for UNIT. */
2702 static struct line_info_table*
2703 decode_line_info (struct comp_unit *unit)
2705 bfd *abfd = unit->abfd;
2706 struct dwarf2_debug *stash = unit->stash;
2707 struct dwarf2_debug_file *file = unit->file;
2708 struct line_info_table* table;
2709 bfd_byte *line_ptr;
2710 bfd_byte *line_end;
2711 struct line_head lh;
2712 unsigned int i, offset_size;
2713 char *cur_file, *cur_dir;
2714 unsigned char op_code, extended_op, adj_opcode;
2715 unsigned int exop_len;
2716 size_t amt;
2718 if (unit->line_offset == 0 && file->line_table)
2719 return file->line_table;
2721 if (! read_section (abfd, &stash->debug_sections[debug_line],
2722 file->syms, unit->line_offset,
2723 &file->dwarf_line_buffer, &file->dwarf_line_size))
2724 return NULL;
2726 if (file->dwarf_line_size < 16)
2728 _bfd_error_handler
2729 (_("DWARF error: line info section is too small (%" PRId64 ")"),
2730 (int64_t) file->dwarf_line_size);
2731 bfd_set_error (bfd_error_bad_value);
2732 return NULL;
2734 line_ptr = file->dwarf_line_buffer + unit->line_offset;
2735 line_end = file->dwarf_line_buffer + file->dwarf_line_size;
2737 /* Read in the prologue. */
2738 lh.total_length = read_4_bytes (abfd, &line_ptr, line_end);
2739 offset_size = 4;
2740 if (lh.total_length == 0xffffffff)
2742 lh.total_length = read_8_bytes (abfd, &line_ptr, line_end);
2743 offset_size = 8;
2745 else if (lh.total_length == 0 && unit->addr_size == 8)
2747 /* Handle (non-standard) 64-bit DWARF2 formats. */
2748 lh.total_length = read_4_bytes (abfd, &line_ptr, line_end);
2749 offset_size = 8;
2752 if (lh.total_length > (size_t) (line_end - line_ptr))
2754 _bfd_error_handler
2755 /* xgettext: c-format */
2756 (_("DWARF error: line info data is bigger (%#" PRIx64 ")"
2757 " than the space remaining in the section (%#lx)"),
2758 (uint64_t) lh.total_length, (unsigned long) (line_end - line_ptr));
2759 bfd_set_error (bfd_error_bad_value);
2760 return NULL;
2763 line_end = line_ptr + lh.total_length;
2765 lh.version = read_2_bytes (abfd, &line_ptr, line_end);
2766 if (lh.version < 2 || lh.version > 5)
2768 _bfd_error_handler
2769 (_("DWARF error: unhandled .debug_line version %d"), lh.version);
2770 bfd_set_error (bfd_error_bad_value);
2771 return NULL;
2774 if (line_ptr + offset_size + (lh.version >= 5 ? 8 : (lh.version >= 4 ? 6 : 5))
2775 >= line_end)
2777 _bfd_error_handler
2778 (_("DWARF error: ran out of room reading prologue"));
2779 bfd_set_error (bfd_error_bad_value);
2780 return NULL;
2783 if (lh.version >= 5)
2785 unsigned int segment_selector_size;
2787 /* Skip address size. */
2788 read_1_byte (abfd, &line_ptr, line_end);
2790 segment_selector_size = read_1_byte (abfd, &line_ptr, line_end);
2791 if (segment_selector_size != 0)
2793 _bfd_error_handler
2794 (_("DWARF error: line info unsupported segment selector size %u"),
2795 segment_selector_size);
2796 bfd_set_error (bfd_error_bad_value);
2797 return NULL;
2801 if (offset_size == 4)
2802 lh.prologue_length = read_4_bytes (abfd, &line_ptr, line_end);
2803 else
2804 lh.prologue_length = read_8_bytes (abfd, &line_ptr, line_end);
2806 lh.minimum_instruction_length = read_1_byte (abfd, &line_ptr, line_end);
2808 if (lh.version >= 4)
2809 lh.maximum_ops_per_insn = read_1_byte (abfd, &line_ptr, line_end);
2810 else
2811 lh.maximum_ops_per_insn = 1;
2813 if (lh.maximum_ops_per_insn == 0)
2815 _bfd_error_handler
2816 (_("DWARF error: invalid maximum operations per instruction"));
2817 bfd_set_error (bfd_error_bad_value);
2818 return NULL;
2821 lh.default_is_stmt = read_1_byte (abfd, &line_ptr, line_end);
2822 lh.line_base = read_1_signed_byte (abfd, &line_ptr, line_end);
2823 lh.line_range = read_1_byte (abfd, &line_ptr, line_end);
2824 lh.opcode_base = read_1_byte (abfd, &line_ptr, line_end);
2826 if (line_ptr + (lh.opcode_base - 1) >= line_end)
2828 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2829 bfd_set_error (bfd_error_bad_value);
2830 return NULL;
2833 amt = lh.opcode_base * sizeof (unsigned char);
2834 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
2836 lh.standard_opcode_lengths[0] = 1;
2838 for (i = 1; i < lh.opcode_base; ++i)
2839 lh.standard_opcode_lengths[i] = read_1_byte (abfd, &line_ptr, line_end);
2841 amt = sizeof (struct line_info_table);
2842 table = (struct line_info_table *) bfd_alloc (abfd, amt);
2843 if (table == NULL)
2844 return NULL;
2845 table->abfd = abfd;
2846 table->comp_dir = unit->comp_dir;
2848 table->num_files = 0;
2849 table->files = NULL;
2851 table->num_dirs = 0;
2852 table->dirs = NULL;
2854 table->num_sequences = 0;
2855 table->sequences = NULL;
2857 table->lcl_head = NULL;
2859 if (lh.version >= 5)
2861 /* Read directory table. */
2862 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2863 line_info_add_include_dir_stub))
2864 goto fail;
2866 /* Read file name table. */
2867 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2868 line_info_add_file_name))
2869 goto fail;
2870 table->use_dir_and_file_0 = true;
2872 else
2874 /* Read directory table. */
2875 while ((cur_dir = read_string (&line_ptr, line_end)) != NULL)
2877 if (!line_info_add_include_dir (table, cur_dir))
2878 goto fail;
2881 /* Read file name table. */
2882 while ((cur_file = read_string (&line_ptr, line_end)) != NULL)
2884 unsigned int dir, xtime, size;
2886 dir = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2887 xtime = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2888 size = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2890 if (!line_info_add_file_name (table, cur_file, dir, xtime, size))
2891 goto fail;
2893 table->use_dir_and_file_0 = false;
2896 /* Read the statement sequences until there's nothing left. */
2897 while (line_ptr < line_end)
2899 /* State machine registers. */
2900 bfd_vma address = 0;
2901 unsigned char op_index = 0;
2902 char * filename = NULL;
2903 unsigned int line = 1;
2904 unsigned int column = 0;
2905 unsigned int discriminator = 0;
2906 int is_stmt = lh.default_is_stmt;
2907 int end_sequence = 0;
2908 unsigned int dir, xtime, size;
2909 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2910 compilers generate address sequences that are wildly out of
2911 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2912 for ia64-Linux). Thus, to determine the low and high
2913 address, we must compare on every DW_LNS_copy, etc. */
2914 bfd_vma low_pc = (bfd_vma) -1;
2915 bfd_vma high_pc = 0;
2917 if (table->num_files)
2919 /* PR 30783: Always start with a file index of 1, even
2920 for DWARF-5. */
2921 filename = concat_filename (table, 1);
2924 /* Decode the table. */
2925 while (!end_sequence && line_ptr < line_end)
2927 op_code = read_1_byte (abfd, &line_ptr, line_end);
2929 if (op_code >= lh.opcode_base)
2931 /* Special operand. */
2932 adj_opcode = op_code - lh.opcode_base;
2933 if (lh.line_range == 0)
2934 goto line_fail;
2935 if (lh.maximum_ops_per_insn == 1)
2936 address += (adj_opcode / lh.line_range
2937 * lh.minimum_instruction_length);
2938 else
2940 address += ((op_index + adj_opcode / lh.line_range)
2941 / lh.maximum_ops_per_insn
2942 * lh.minimum_instruction_length);
2943 op_index = ((op_index + adj_opcode / lh.line_range)
2944 % lh.maximum_ops_per_insn);
2946 line += lh.line_base + (adj_opcode % lh.line_range);
2947 /* Append row to matrix using current values. */
2948 if (!add_line_info (table, address, op_index, filename,
2949 line, column, discriminator, 0))
2950 goto line_fail;
2951 discriminator = 0;
2952 if (address < low_pc)
2953 low_pc = address;
2954 if (address > high_pc)
2955 high_pc = address;
2957 else switch (op_code)
2959 case DW_LNS_extended_op:
2960 exop_len = _bfd_safe_read_leb128 (abfd, &line_ptr,
2961 false, line_end);
2962 extended_op = read_1_byte (abfd, &line_ptr, line_end);
2964 switch (extended_op)
2966 case DW_LNE_end_sequence:
2967 end_sequence = 1;
2968 if (!add_line_info (table, address, op_index, filename, line,
2969 column, discriminator, end_sequence))
2970 goto line_fail;
2971 discriminator = 0;
2972 if (address < low_pc)
2973 low_pc = address;
2974 if (address > high_pc)
2975 high_pc = address;
2976 if (!arange_add (unit, &unit->arange, &unit->file->trie_root,
2977 low_pc, high_pc))
2978 goto line_fail;
2979 break;
2980 case DW_LNE_set_address:
2981 address = read_address (unit, &line_ptr, line_end);
2982 op_index = 0;
2983 break;
2984 case DW_LNE_define_file:
2985 cur_file = read_string (&line_ptr, line_end);
2986 dir = _bfd_safe_read_leb128 (abfd, &line_ptr,
2987 false, line_end);
2988 xtime = _bfd_safe_read_leb128 (abfd, &line_ptr,
2989 false, line_end);
2990 size = _bfd_safe_read_leb128 (abfd, &line_ptr,
2991 false, line_end);
2992 if (!line_info_add_file_name (table, cur_file, dir,
2993 xtime, size))
2994 goto line_fail;
2995 break;
2996 case DW_LNE_set_discriminator:
2997 discriminator = _bfd_safe_read_leb128 (abfd, &line_ptr,
2998 false, line_end);
2999 break;
3000 case DW_LNE_HP_source_file_correlation:
3001 line_ptr += exop_len - 1;
3002 break;
3003 default:
3004 _bfd_error_handler
3005 (_("DWARF error: mangled line number section"));
3006 bfd_set_error (bfd_error_bad_value);
3007 line_fail:
3008 free (filename);
3009 goto fail;
3011 break;
3012 case DW_LNS_copy:
3013 if (!add_line_info (table, address, op_index,
3014 filename, line, column, discriminator, 0))
3015 goto line_fail;
3016 discriminator = 0;
3017 if (address < low_pc)
3018 low_pc = address;
3019 if (address > high_pc)
3020 high_pc = address;
3021 break;
3022 case DW_LNS_advance_pc:
3023 if (lh.maximum_ops_per_insn == 1)
3024 address += (lh.minimum_instruction_length
3025 * _bfd_safe_read_leb128 (abfd, &line_ptr,
3026 false, line_end));
3027 else
3029 bfd_vma adjust = _bfd_safe_read_leb128 (abfd, &line_ptr,
3030 false, line_end);
3031 address = ((op_index + adjust) / lh.maximum_ops_per_insn
3032 * lh.minimum_instruction_length);
3033 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
3035 break;
3036 case DW_LNS_advance_line:
3037 line += _bfd_safe_read_leb128 (abfd, &line_ptr,
3038 true, line_end);
3039 break;
3040 case DW_LNS_set_file:
3042 unsigned int filenum;
3044 /* The file and directory tables are 0
3045 based, the references are 1 based. */
3046 filenum = _bfd_safe_read_leb128 (abfd, &line_ptr,
3047 false, line_end);
3048 free (filename);
3049 filename = concat_filename (table, filenum);
3050 break;
3052 case DW_LNS_set_column:
3053 column = _bfd_safe_read_leb128 (abfd, &line_ptr,
3054 false, line_end);
3055 break;
3056 case DW_LNS_negate_stmt:
3057 is_stmt = (!is_stmt);
3058 break;
3059 case DW_LNS_set_basic_block:
3060 break;
3061 case DW_LNS_const_add_pc:
3062 if (lh.line_range == 0)
3063 goto line_fail;
3064 if (lh.maximum_ops_per_insn == 1)
3065 address += (lh.minimum_instruction_length
3066 * ((255 - lh.opcode_base) / lh.line_range));
3067 else
3069 bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
3070 address += (lh.minimum_instruction_length
3071 * ((op_index + adjust)
3072 / lh.maximum_ops_per_insn));
3073 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
3075 break;
3076 case DW_LNS_fixed_advance_pc:
3077 address += read_2_bytes (abfd, &line_ptr, line_end);
3078 op_index = 0;
3079 break;
3080 default:
3081 /* Unknown standard opcode, ignore it. */
3082 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
3083 (void) _bfd_safe_read_leb128 (abfd, &line_ptr,
3084 false, line_end);
3085 break;
3089 free (filename);
3092 if (unit->line_offset == 0)
3093 file->line_table = table;
3094 if (sort_line_sequences (table))
3095 return table;
3097 fail:
3098 while (table->sequences != NULL)
3100 struct line_sequence* seq = table->sequences;
3101 table->sequences = table->sequences->prev_sequence;
3102 free (seq);
3104 free (table->files);
3105 free (table->dirs);
3106 return NULL;
3109 /* If ADDR is within TABLE set the output parameters and return TRUE,
3110 otherwise set *FILENAME_PTR to NULL and return FALSE.
3111 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
3112 are pointers to the objects to be filled in. */
3114 static bool
3115 lookup_address_in_line_info_table (struct line_info_table *table,
3116 bfd_vma addr,
3117 const char **filename_ptr,
3118 unsigned int *linenumber_ptr,
3119 unsigned int *discriminator_ptr)
3121 struct line_sequence *seq = NULL;
3122 struct line_info *info;
3123 int low, high, mid;
3125 /* Binary search the array of sequences. */
3126 low = 0;
3127 high = table->num_sequences;
3128 while (low < high)
3130 mid = (low + high) / 2;
3131 seq = &table->sequences[mid];
3132 if (addr < seq->low_pc)
3133 high = mid;
3134 else if (addr >= seq->last_line->address)
3135 low = mid + 1;
3136 else
3137 break;
3140 /* Check for a valid sequence. */
3141 if (!seq || addr < seq->low_pc || addr >= seq->last_line->address)
3142 goto fail;
3144 if (!build_line_info_table (table, seq))
3145 goto fail;
3147 /* Binary search the array of line information. */
3148 low = 0;
3149 high = seq->num_lines;
3150 info = NULL;
3151 while (low < high)
3153 mid = (low + high) / 2;
3154 info = seq->line_info_lookup[mid];
3155 if (addr < info->address)
3156 high = mid;
3157 else if (addr >= seq->line_info_lookup[mid + 1]->address)
3158 low = mid + 1;
3159 else
3160 break;
3163 /* Check for a valid line information entry. */
3164 if (info
3165 && addr >= info->address
3166 && addr < seq->line_info_lookup[mid + 1]->address
3167 && !(info->end_sequence || info == seq->last_line))
3169 *filename_ptr = info->filename;
3170 *linenumber_ptr = info->line;
3171 if (discriminator_ptr)
3172 *discriminator_ptr = info->discriminator;
3173 return true;
3176 fail:
3177 *filename_ptr = NULL;
3178 return false;
3181 /* Read in the .debug_ranges section for future reference. */
3183 static bool
3184 read_debug_ranges (struct comp_unit * unit)
3186 struct dwarf2_debug *stash = unit->stash;
3187 struct dwarf2_debug_file *file = unit->file;
3189 return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
3190 file->syms, 0,
3191 &file->dwarf_ranges_buffer, &file->dwarf_ranges_size);
3194 /* Read in the .debug_rnglists section for future reference. */
3196 static bool
3197 read_debug_rnglists (struct comp_unit * unit)
3199 struct dwarf2_debug *stash = unit->stash;
3200 struct dwarf2_debug_file *file = unit->file;
3202 return read_section (unit->abfd, &stash->debug_sections[debug_rnglists],
3203 file->syms, 0,
3204 &file->dwarf_rnglists_buffer, &file->dwarf_rnglists_size);
3207 /* Function table functions. */
3209 static int
3210 compare_lookup_funcinfos (const void * a, const void * b)
3212 const struct lookup_funcinfo * lookup1 = a;
3213 const struct lookup_funcinfo * lookup2 = b;
3215 if (lookup1->low_addr < lookup2->low_addr)
3216 return -1;
3217 if (lookup1->low_addr > lookup2->low_addr)
3218 return 1;
3219 if (lookup1->high_addr < lookup2->high_addr)
3220 return -1;
3221 if (lookup1->high_addr > lookup2->high_addr)
3222 return 1;
3224 if (lookup1->idx < lookup2->idx)
3225 return -1;
3226 if (lookup1->idx > lookup2->idx)
3227 return 1;
3228 return 0;
3231 static bool
3232 build_lookup_funcinfo_table (struct comp_unit * unit)
3234 struct lookup_funcinfo *lookup_funcinfo_table = unit->lookup_funcinfo_table;
3235 unsigned int number_of_functions = unit->number_of_functions;
3236 struct funcinfo *each;
3237 struct lookup_funcinfo *entry;
3238 size_t func_index;
3239 struct arange *range;
3240 bfd_vma low_addr, high_addr;
3242 if (lookup_funcinfo_table || number_of_functions == 0)
3243 return true;
3245 /* Create the function info lookup table. */
3246 lookup_funcinfo_table = (struct lookup_funcinfo *)
3247 bfd_malloc (number_of_functions * sizeof (struct lookup_funcinfo));
3248 if (lookup_funcinfo_table == NULL)
3249 return false;
3251 /* Populate the function info lookup table. */
3252 func_index = number_of_functions;
3253 for (each = unit->function_table; each; each = each->prev_func)
3255 entry = &lookup_funcinfo_table[--func_index];
3256 entry->funcinfo = each;
3257 entry->idx = func_index;
3259 /* Calculate the lowest and highest address for this function entry. */
3260 low_addr = entry->funcinfo->arange.low;
3261 high_addr = entry->funcinfo->arange.high;
3263 for (range = entry->funcinfo->arange.next; range; range = range->next)
3265 if (range->low < low_addr)
3266 low_addr = range->low;
3267 if (range->high > high_addr)
3268 high_addr = range->high;
3271 entry->low_addr = low_addr;
3272 entry->high_addr = high_addr;
3275 BFD_ASSERT (func_index == 0);
3277 /* Sort the function by address. */
3278 qsort (lookup_funcinfo_table,
3279 number_of_functions,
3280 sizeof (struct lookup_funcinfo),
3281 compare_lookup_funcinfos);
3283 /* Calculate the high watermark for each function in the lookup table. */
3284 high_addr = lookup_funcinfo_table[0].high_addr;
3285 for (func_index = 1; func_index < number_of_functions; func_index++)
3287 entry = &lookup_funcinfo_table[func_index];
3288 if (entry->high_addr > high_addr)
3289 high_addr = entry->high_addr;
3290 else
3291 entry->high_addr = high_addr;
3294 unit->lookup_funcinfo_table = lookup_funcinfo_table;
3295 return true;
3298 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
3299 TRUE. Note that we need to find the function that has the smallest range
3300 that contains ADDR, to handle inlined functions without depending upon
3301 them being ordered in TABLE by increasing range. */
3303 static bool
3304 lookup_address_in_function_table (struct comp_unit *unit,
3305 bfd_vma addr,
3306 struct funcinfo **function_ptr)
3308 unsigned int number_of_functions = unit->number_of_functions;
3309 struct lookup_funcinfo* lookup_funcinfo = NULL;
3310 struct funcinfo* funcinfo = NULL;
3311 struct funcinfo* best_fit = NULL;
3312 bfd_vma best_fit_len = (bfd_vma) -1;
3313 bfd_size_type low, high, mid, first;
3314 struct arange *arange;
3316 if (number_of_functions == 0)
3317 return false;
3319 if (!build_lookup_funcinfo_table (unit))
3320 return false;
3322 if (unit->lookup_funcinfo_table[number_of_functions - 1].high_addr < addr)
3323 return false;
3325 /* Find the first function in the lookup table which may contain the
3326 specified address. */
3327 low = 0;
3328 high = number_of_functions;
3329 first = high;
3330 while (low < high)
3332 mid = (low + high) / 2;
3333 lookup_funcinfo = &unit->lookup_funcinfo_table[mid];
3334 if (addr < lookup_funcinfo->low_addr)
3335 high = mid;
3336 else if (addr >= lookup_funcinfo->high_addr)
3337 low = mid + 1;
3338 else
3339 high = first = mid;
3342 /* Find the 'best' match for the address. The prior algorithm defined the
3343 best match as the function with the smallest address range containing
3344 the specified address. This definition should probably be changed to the
3345 innermost inline routine containing the address, but right now we want
3346 to get the same results we did before. */
3347 while (first < number_of_functions)
3349 if (addr < unit->lookup_funcinfo_table[first].low_addr)
3350 break;
3351 funcinfo = unit->lookup_funcinfo_table[first].funcinfo;
3353 for (arange = &funcinfo->arange; arange; arange = arange->next)
3355 if (addr < arange->low || addr >= arange->high)
3356 continue;
3358 if (arange->high - arange->low < best_fit_len
3359 /* The following comparison is designed to return the same
3360 match as the previous algorithm for routines which have the
3361 same best fit length. */
3362 || (arange->high - arange->low == best_fit_len
3363 && funcinfo > best_fit))
3365 best_fit = funcinfo;
3366 best_fit_len = arange->high - arange->low;
3370 first++;
3373 if (!best_fit)
3374 return false;
3376 *function_ptr = best_fit;
3377 return true;
3380 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
3381 and LINENUMBER_PTR, and return TRUE. */
3383 static bool
3384 lookup_symbol_in_function_table (struct comp_unit *unit,
3385 asymbol *sym,
3386 bfd_vma addr,
3387 const char **filename_ptr,
3388 unsigned int *linenumber_ptr)
3390 struct funcinfo* each;
3391 struct funcinfo* best_fit = NULL;
3392 bfd_vma best_fit_len = (bfd_vma) -1;
3393 struct arange *arange;
3394 const char *name = bfd_asymbol_name (sym);
3396 for (each = unit->function_table; each; each = each->prev_func)
3397 for (arange = &each->arange; arange; arange = arange->next)
3398 if (addr >= arange->low
3399 && addr < arange->high
3400 && arange->high - arange->low < best_fit_len
3401 && each->file
3402 && each->name
3403 && strstr (name, each->name) != NULL)
3405 best_fit = each;
3406 best_fit_len = arange->high - arange->low;
3409 if (best_fit)
3411 *filename_ptr = best_fit->file;
3412 *linenumber_ptr = best_fit->line;
3413 return true;
3416 return false;
3419 /* Variable table functions. */
3421 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
3422 LINENUMBER_PTR, and return TRUE. */
3424 static bool
3425 lookup_symbol_in_variable_table (struct comp_unit *unit,
3426 asymbol *sym,
3427 bfd_vma addr,
3428 const char **filename_ptr,
3429 unsigned int *linenumber_ptr)
3431 struct varinfo* each;
3432 const char *name = bfd_asymbol_name (sym);
3434 for (each = unit->variable_table; each; each = each->prev_var)
3435 if (each->addr == addr
3436 && !each->stack
3437 && each->file != NULL
3438 && each->name != NULL
3439 && strstr (name, each->name) != NULL)
3440 break;
3442 if (each)
3444 *filename_ptr = each->file;
3445 *linenumber_ptr = each->line;
3446 return true;
3449 return false;
3452 static struct comp_unit *stash_comp_unit (struct dwarf2_debug *,
3453 struct dwarf2_debug_file *);
3454 static bool comp_unit_maybe_decode_line_info (struct comp_unit *);
3456 static bool
3457 find_abstract_instance (struct comp_unit *unit,
3458 struct attribute *attr_ptr,
3459 unsigned int recur_count,
3460 const char **pname,
3461 bool *is_linkage,
3462 char **filename_ptr,
3463 int *linenumber_ptr)
3465 bfd *abfd = unit->abfd;
3466 bfd_byte *info_ptr = NULL;
3467 bfd_byte *info_ptr_end;
3468 unsigned int abbrev_number, i;
3469 struct abbrev_info *abbrev;
3470 uint64_t die_ref = attr_ptr->u.val;
3471 struct attribute attr;
3473 if (recur_count == 100)
3475 _bfd_error_handler
3476 (_("DWARF error: abstract instance recursion detected"));
3477 bfd_set_error (bfd_error_bad_value);
3478 return false;
3481 /* DW_FORM_ref_addr can reference an entry in a different CU. It
3482 is an offset from the .debug_info section, not the current CU. */
3483 if (attr_ptr->form == DW_FORM_ref_addr)
3485 /* We only support DW_FORM_ref_addr within the same file, so
3486 any relocations should be resolved already. Check this by
3487 testing for a zero die_ref; There can't be a valid reference
3488 to the header of a .debug_info section.
3489 DW_FORM_ref_addr is an offset relative to .debug_info.
3490 Normally when using the GNU linker this is accomplished by
3491 emitting a symbolic reference to a label, because .debug_info
3492 sections are linked at zero. When there are multiple section
3493 groups containing .debug_info, as there might be in a
3494 relocatable object file, it would be reasonable to assume that
3495 a symbolic reference to a label in any .debug_info section
3496 might be used. Since we lay out multiple .debug_info
3497 sections at non-zero VMAs (see place_sections), and read
3498 them contiguously into dwarf_info_buffer, that means the
3499 reference is relative to dwarf_info_buffer. */
3500 size_t total;
3502 info_ptr = unit->file->dwarf_info_buffer;
3503 info_ptr_end = info_ptr + unit->file->dwarf_info_size;
3504 total = info_ptr_end - info_ptr;
3505 if (!die_ref)
3506 return true;
3507 else if (die_ref >= total)
3509 _bfd_error_handler
3510 (_("DWARF error: invalid abstract instance DIE ref"));
3511 bfd_set_error (bfd_error_bad_value);
3512 return false;
3514 info_ptr += die_ref;
3516 else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
3518 bool first_time = unit->stash->alt.dwarf_info_buffer == NULL;
3520 info_ptr = read_alt_indirect_ref (unit, die_ref);
3521 if (first_time)
3522 unit->stash->alt.info_ptr = unit->stash->alt.dwarf_info_buffer;
3523 if (info_ptr == NULL)
3525 _bfd_error_handler
3526 (_("DWARF error: unable to read alt ref %" PRIu64),
3527 (uint64_t) die_ref);
3528 bfd_set_error (bfd_error_bad_value);
3529 return false;
3531 info_ptr_end = (unit->stash->alt.dwarf_info_buffer
3532 + unit->stash->alt.dwarf_info_size);
3533 if (unit->stash->alt.all_comp_units)
3534 unit = unit->stash->alt.all_comp_units;
3537 if (attr_ptr->form == DW_FORM_ref_addr
3538 || attr_ptr->form == DW_FORM_GNU_ref_alt)
3540 /* Now find the CU containing this pointer. */
3541 if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr)
3542 info_ptr_end = unit->end_ptr;
3543 else
3545 /* Check other CUs to see if they contain the abbrev. */
3546 struct comp_unit *u = NULL;
3547 struct addr_range range = { info_ptr, info_ptr };
3548 splay_tree_node v = splay_tree_lookup (unit->file->comp_unit_tree,
3549 (splay_tree_key)&range);
3550 if (v != NULL)
3551 u = (struct comp_unit *)v->value;
3553 if (attr_ptr->form == DW_FORM_ref_addr)
3554 while (u == NULL)
3556 u = stash_comp_unit (unit->stash, &unit->stash->f);
3557 if (u == NULL)
3558 break;
3559 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3560 break;
3561 u = NULL;
3564 if (attr_ptr->form == DW_FORM_GNU_ref_alt)
3565 while (u == NULL)
3567 u = stash_comp_unit (unit->stash, &unit->stash->alt);
3568 if (u == NULL)
3569 break;
3570 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3571 break;
3572 u = NULL;
3575 if (u == NULL)
3577 _bfd_error_handler
3578 (_("DWARF error: unable to locate abstract instance DIE ref %"
3579 PRIu64), (uint64_t) die_ref);
3580 bfd_set_error (bfd_error_bad_value);
3581 return false;
3583 unit = u;
3584 info_ptr_end = unit->end_ptr;
3587 else
3589 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
3590 DW_FORM_ref_udata. These are all references relative to the
3591 start of the current CU. */
3592 size_t total;
3594 info_ptr = unit->info_ptr_unit;
3595 info_ptr_end = unit->end_ptr;
3596 total = info_ptr_end - info_ptr;
3597 if (!die_ref || die_ref >= total)
3599 _bfd_error_handler
3600 (_("DWARF error: invalid abstract instance DIE ref"));
3601 bfd_set_error (bfd_error_bad_value);
3602 return false;
3604 info_ptr += die_ref;
3607 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
3608 false, info_ptr_end);
3609 if (abbrev_number)
3611 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3612 if (! abbrev)
3614 _bfd_error_handler
3615 (_("DWARF error: could not find abbrev number %u"), abbrev_number);
3616 bfd_set_error (bfd_error_bad_value);
3617 return false;
3619 else
3621 for (i = 0; i < abbrev->num_attrs; ++i)
3623 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
3624 info_ptr, info_ptr_end);
3625 if (info_ptr == NULL)
3626 break;
3627 switch (attr.name)
3629 case DW_AT_name:
3630 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3631 over DW_AT_name. */
3632 if (*pname == NULL && is_str_form (&attr))
3634 *pname = attr.u.str;
3635 if (mangle_style (unit->lang) == 0)
3636 *is_linkage = true;
3638 break;
3639 case DW_AT_specification:
3640 if (is_int_form (&attr)
3641 && !find_abstract_instance (unit, &attr, recur_count + 1,
3642 pname, is_linkage,
3643 filename_ptr, linenumber_ptr))
3644 return false;
3645 break;
3646 case DW_AT_linkage_name:
3647 case DW_AT_MIPS_linkage_name:
3648 /* PR 16949: Corrupt debug info can place
3649 non-string forms into these attributes. */
3650 if (is_str_form (&attr))
3652 *pname = attr.u.str;
3653 *is_linkage = true;
3655 break;
3656 case DW_AT_decl_file:
3657 if (!comp_unit_maybe_decode_line_info (unit))
3658 return false;
3659 if (is_int_form (&attr))
3661 free (*filename_ptr);
3662 *filename_ptr = concat_filename (unit->line_table,
3663 attr.u.val);
3665 break;
3666 case DW_AT_decl_line:
3667 if (is_int_form (&attr))
3668 *linenumber_ptr = attr.u.val;
3669 break;
3670 default:
3671 break;
3676 return true;
3679 static bool
3680 read_ranges (struct comp_unit *unit, struct arange *arange,
3681 struct trie_node **trie_root, uint64_t offset)
3683 bfd_byte *ranges_ptr;
3684 bfd_byte *ranges_end;
3685 bfd_vma base_address = unit->base_address;
3687 if (! unit->file->dwarf_ranges_buffer)
3689 if (! read_debug_ranges (unit))
3690 return false;
3693 if (offset > unit->file->dwarf_ranges_size)
3694 return false;
3695 ranges_ptr = unit->file->dwarf_ranges_buffer + offset;
3696 ranges_end = unit->file->dwarf_ranges_buffer + unit->file->dwarf_ranges_size;
3698 for (;;)
3700 bfd_vma low_pc;
3701 bfd_vma high_pc;
3703 /* PR 17512: file: 62cada7d. */
3704 if (2u * unit->addr_size > (size_t) (ranges_end - ranges_ptr))
3705 return false;
3707 low_pc = read_address (unit, &ranges_ptr, ranges_end);
3708 high_pc = read_address (unit, &ranges_ptr, ranges_end);
3710 if (low_pc == 0 && high_pc == 0)
3711 break;
3712 if (low_pc == (bfd_vma) -1 && high_pc != (bfd_vma) -1)
3713 base_address = high_pc;
3714 else
3716 if (!arange_add (unit, arange, trie_root,
3717 base_address + low_pc, base_address + high_pc))
3718 return false;
3721 return true;
3724 static bool
3725 read_rnglists (struct comp_unit *unit, struct arange *arange,
3726 struct trie_node **trie_root, uint64_t offset)
3728 bfd_byte *rngs_ptr;
3729 bfd_byte *rngs_end;
3730 bfd_vma base_address = unit->base_address;
3731 bfd_vma low_pc;
3732 bfd_vma high_pc;
3733 bfd *abfd = unit->abfd;
3735 if (! unit->file->dwarf_rnglists_buffer)
3737 if (! read_debug_rnglists (unit))
3738 return false;
3741 rngs_ptr = unit->file->dwarf_rnglists_buffer + offset;
3742 if (rngs_ptr < unit->file->dwarf_rnglists_buffer)
3743 return false;
3744 rngs_end = unit->file->dwarf_rnglists_buffer;
3745 rngs_end += unit->file->dwarf_rnglists_size;
3747 for (;;)
3749 enum dwarf_range_list_entry rlet;
3751 if (rngs_ptr >= rngs_end)
3752 return false;
3754 rlet = read_1_byte (abfd, &rngs_ptr, rngs_end);
3756 switch (rlet)
3758 case DW_RLE_end_of_list:
3759 return true;
3761 case DW_RLE_base_address:
3762 if (unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3763 return false;
3764 base_address = read_address (unit, &rngs_ptr, rngs_end);
3765 continue;
3767 case DW_RLE_start_length:
3768 if (unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3769 return false;
3770 low_pc = read_address (unit, &rngs_ptr, rngs_end);
3771 high_pc = low_pc;
3772 high_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3773 false, rngs_end);
3774 break;
3776 case DW_RLE_offset_pair:
3777 low_pc = base_address;
3778 low_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3779 false, rngs_end);
3780 high_pc = base_address;
3781 high_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3782 false, rngs_end);
3783 break;
3785 case DW_RLE_start_end:
3786 if (2u * unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3787 return false;
3788 low_pc = read_address (unit, &rngs_ptr, rngs_end);
3789 high_pc = read_address (unit, &rngs_ptr, rngs_end);
3790 break;
3792 /* TODO x-variants need .debug_addr support used for split-dwarf. */
3793 case DW_RLE_base_addressx:
3794 case DW_RLE_startx_endx:
3795 case DW_RLE_startx_length:
3796 default:
3797 return false;
3800 if (!arange_add (unit, arange, trie_root, low_pc, high_pc))
3801 return false;
3805 static bool
3806 read_rangelist (struct comp_unit *unit, struct arange *arange,
3807 struct trie_node **trie_root, uint64_t offset)
3809 if (unit->version <= 4)
3810 return read_ranges (unit, arange, trie_root, offset);
3811 else
3812 return read_rnglists (unit, arange, trie_root, offset);
3815 static struct funcinfo *
3816 lookup_func_by_offset (uint64_t offset, struct funcinfo * table)
3818 for (; table != NULL; table = table->prev_func)
3819 if (table->unit_offset == offset)
3820 return table;
3821 return NULL;
3824 static struct varinfo *
3825 lookup_var_by_offset (uint64_t offset, struct varinfo * table)
3827 while (table)
3829 if (table->unit_offset == offset)
3830 return table;
3831 table = table->prev_var;
3834 return NULL;
3838 /* DWARF2 Compilation unit functions. */
3840 static struct funcinfo *
3841 reverse_funcinfo_list (struct funcinfo *head)
3843 struct funcinfo *rhead;
3844 struct funcinfo *temp;
3846 for (rhead = NULL; head; head = temp)
3848 temp = head->prev_func;
3849 head->prev_func = rhead;
3850 rhead = head;
3852 return rhead;
3855 static struct varinfo *
3856 reverse_varinfo_list (struct varinfo *head)
3858 struct varinfo *rhead;
3859 struct varinfo *temp;
3861 for (rhead = NULL; head; head = temp)
3863 temp = head->prev_var;
3864 head->prev_var = rhead;
3865 rhead = head;
3867 return rhead;
3870 /* Scan over each die in a comp. unit looking for functions to add
3871 to the function table and variables to the variable table. */
3873 static bool
3874 scan_unit_for_symbols (struct comp_unit *unit)
3876 bfd *abfd = unit->abfd;
3877 bfd_byte *info_ptr = unit->first_child_die_ptr;
3878 bfd_byte *info_ptr_end = unit->end_ptr;
3879 int nesting_level = 0;
3880 struct nest_funcinfo
3882 struct funcinfo *func;
3883 } *nested_funcs;
3884 int nested_funcs_size;
3885 struct funcinfo *last_func;
3886 struct varinfo *last_var;
3888 /* Maintain a stack of in-scope functions and inlined functions, which we
3889 can use to set the caller_func field. */
3890 nested_funcs_size = 32;
3891 nested_funcs = (struct nest_funcinfo *)
3892 bfd_malloc (nested_funcs_size * sizeof (*nested_funcs));
3893 if (nested_funcs == NULL)
3894 return false;
3895 nested_funcs[nesting_level].func = 0;
3897 /* PR 27484: We must scan the DIEs twice. The first time we look for
3898 function and variable tags and accumulate them into their respective
3899 tables. The second time through we process the attributes of the
3900 functions/variables and augment the table entries. */
3901 while (nesting_level >= 0)
3903 unsigned int abbrev_number, i;
3904 struct abbrev_info *abbrev;
3905 struct funcinfo *func;
3906 struct varinfo *var;
3907 uint64_t current_offset;
3909 /* PR 17512: file: 9f405d9d. */
3910 if (info_ptr >= info_ptr_end)
3911 goto fail;
3913 current_offset = info_ptr - unit->info_ptr_unit;
3914 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
3915 false, info_ptr_end);
3916 if (abbrev_number == 0)
3918 nesting_level--;
3919 continue;
3922 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3923 if (! abbrev)
3925 static unsigned int previous_failed_abbrev = -1U;
3927 /* Avoid multiple reports of the same missing abbrev. */
3928 if (abbrev_number != previous_failed_abbrev)
3930 _bfd_error_handler
3931 (_("DWARF error: could not find abbrev number %u"),
3932 abbrev_number);
3933 previous_failed_abbrev = abbrev_number;
3935 bfd_set_error (bfd_error_bad_value);
3936 goto fail;
3939 if (abbrev->tag == DW_TAG_subprogram
3940 || abbrev->tag == DW_TAG_entry_point
3941 || abbrev->tag == DW_TAG_inlined_subroutine)
3943 size_t amt = sizeof (struct funcinfo);
3945 var = NULL;
3946 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
3947 if (func == NULL)
3948 goto fail;
3949 func->tag = abbrev->tag;
3950 func->prev_func = unit->function_table;
3951 func->unit_offset = current_offset;
3952 unit->function_table = func;
3953 unit->number_of_functions++;
3954 BFD_ASSERT (!unit->cached);
3956 if (func->tag == DW_TAG_inlined_subroutine)
3957 for (i = nesting_level; i-- != 0; )
3958 if (nested_funcs[i].func)
3960 func->caller_func = nested_funcs[i].func;
3961 break;
3963 nested_funcs[nesting_level].func = func;
3965 else
3967 func = NULL;
3968 if (abbrev->tag == DW_TAG_variable
3969 || abbrev->tag == DW_TAG_member)
3971 size_t amt = sizeof (struct varinfo);
3973 var = (struct varinfo *) bfd_zalloc (abfd, amt);
3974 if (var == NULL)
3975 goto fail;
3976 var->tag = abbrev->tag;
3977 var->stack = true;
3978 var->prev_var = unit->variable_table;
3979 unit->variable_table = var;
3980 var->unit_offset = current_offset;
3981 /* PR 18205: Missing debug information can cause this
3982 var to be attached to an already cached unit. */
3984 else
3985 var = NULL;
3987 /* No inline function in scope at this nesting level. */
3988 nested_funcs[nesting_level].func = 0;
3991 for (i = 0; i < abbrev->num_attrs; ++i)
3993 struct attribute attr;
3995 info_ptr = read_attribute (&attr, &abbrev->attrs[i],
3996 unit, info_ptr, info_ptr_end);
3997 if (info_ptr == NULL)
3998 goto fail;
4001 if (abbrev->has_children)
4003 nesting_level++;
4005 if (nesting_level >= nested_funcs_size)
4007 struct nest_funcinfo *tmp;
4009 nested_funcs_size *= 2;
4010 tmp = (struct nest_funcinfo *)
4011 bfd_realloc (nested_funcs,
4012 nested_funcs_size * sizeof (*nested_funcs));
4013 if (tmp == NULL)
4014 goto fail;
4015 nested_funcs = tmp;
4017 nested_funcs[nesting_level].func = 0;
4021 unit->function_table = reverse_funcinfo_list (unit->function_table);
4022 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4024 /* This is the second pass over the abbrevs. */
4025 info_ptr = unit->first_child_die_ptr;
4026 nesting_level = 0;
4028 last_func = NULL;
4029 last_var = NULL;
4031 while (nesting_level >= 0)
4033 unsigned int abbrev_number, i;
4034 struct abbrev_info *abbrev;
4035 struct attribute attr;
4036 struct funcinfo *func;
4037 struct varinfo *var;
4038 bfd_vma low_pc = 0;
4039 bfd_vma high_pc = 0;
4040 bool high_pc_relative = false;
4041 uint64_t current_offset;
4043 /* PR 17512: file: 9f405d9d. */
4044 if (info_ptr >= info_ptr_end)
4045 goto fail;
4047 current_offset = info_ptr - unit->info_ptr_unit;
4048 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
4049 false, info_ptr_end);
4050 if (! abbrev_number)
4052 nesting_level--;
4053 continue;
4056 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
4057 /* This should have been handled above. */
4058 BFD_ASSERT (abbrev != NULL);
4060 func = NULL;
4061 var = NULL;
4062 if (abbrev->tag == DW_TAG_subprogram
4063 || abbrev->tag == DW_TAG_entry_point
4064 || abbrev->tag == DW_TAG_inlined_subroutine)
4066 if (last_func
4067 && last_func->prev_func
4068 && last_func->prev_func->unit_offset == current_offset)
4069 func = last_func->prev_func;
4070 else
4071 func = lookup_func_by_offset (current_offset, unit->function_table);
4073 if (func == NULL)
4074 goto fail;
4076 last_func = func;
4078 else if (abbrev->tag == DW_TAG_variable
4079 || abbrev->tag == DW_TAG_member)
4081 if (last_var
4082 && last_var->prev_var
4083 && last_var->prev_var->unit_offset == current_offset)
4084 var = last_var->prev_var;
4085 else
4086 var = lookup_var_by_offset (current_offset, unit->variable_table);
4088 if (var == NULL)
4089 goto fail;
4091 last_var = var;
4094 for (i = 0; i < abbrev->num_attrs; ++i)
4096 info_ptr = read_attribute (&attr, &abbrev->attrs[i],
4097 unit, info_ptr, info_ptr_end);
4098 if (info_ptr == NULL)
4099 goto fail;
4101 if (func)
4103 switch (attr.name)
4105 case DW_AT_call_file:
4106 if (is_int_form (&attr))
4108 free (func->caller_file);
4109 func->caller_file = concat_filename (unit->line_table,
4110 attr.u.val);
4112 break;
4114 case DW_AT_call_line:
4115 if (is_int_form (&attr))
4116 func->caller_line = attr.u.val;
4117 break;
4119 case DW_AT_abstract_origin:
4120 case DW_AT_specification:
4121 if (is_int_form (&attr)
4122 && !find_abstract_instance (unit, &attr, 0,
4123 &func->name,
4124 &func->is_linkage,
4125 &func->file,
4126 &func->line))
4127 goto fail;
4128 break;
4130 case DW_AT_name:
4131 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
4132 over DW_AT_name. */
4133 if (func->name == NULL && is_str_form (&attr))
4135 func->name = attr.u.str;
4136 if (mangle_style (unit->lang) == 0)
4137 func->is_linkage = true;
4139 break;
4141 case DW_AT_linkage_name:
4142 case DW_AT_MIPS_linkage_name:
4143 /* PR 16949: Corrupt debug info can place
4144 non-string forms into these attributes. */
4145 if (is_str_form (&attr))
4147 func->name = attr.u.str;
4148 func->is_linkage = true;
4150 break;
4152 case DW_AT_low_pc:
4153 if (is_int_form (&attr))
4154 low_pc = attr.u.val;
4155 break;
4157 case DW_AT_high_pc:
4158 if (is_int_form (&attr))
4160 high_pc = attr.u.val;
4161 high_pc_relative = attr.form != DW_FORM_addr;
4163 break;
4165 case DW_AT_ranges:
4166 if (is_int_form (&attr)
4167 && !read_rangelist (unit, &func->arange,
4168 &unit->file->trie_root, attr.u.val))
4169 goto fail;
4170 break;
4172 case DW_AT_decl_file:
4173 if (is_int_form (&attr))
4175 free (func->file);
4176 func->file = concat_filename (unit->line_table,
4177 attr.u.val);
4179 break;
4181 case DW_AT_decl_line:
4182 if (is_int_form (&attr))
4183 func->line = attr.u.val;
4184 break;
4186 default:
4187 break;
4190 else if (var)
4192 switch (attr.name)
4194 case DW_AT_specification:
4195 if (is_int_form (&attr) && attr.u.val)
4197 bool is_linkage;
4198 if (!find_abstract_instance (unit, &attr, 0,
4199 &var->name,
4200 &is_linkage,
4201 &var->file,
4202 &var->line))
4204 _bfd_error_handler (_("DWARF error: could not find "
4205 "variable specification "
4206 "at offset 0x%lx"),
4207 (unsigned long) attr.u.val);
4208 break;
4211 break;
4213 case DW_AT_name:
4214 if (is_str_form (&attr))
4215 var->name = attr.u.str;
4216 break;
4218 case DW_AT_decl_file:
4219 if (is_int_form (&attr))
4221 free (var->file);
4222 var->file = concat_filename (unit->line_table,
4223 attr.u.val);
4225 break;
4227 case DW_AT_decl_line:
4228 if (is_int_form (&attr))
4229 var->line = attr.u.val;
4230 break;
4232 case DW_AT_external:
4233 if (is_int_form (&attr) && attr.u.val != 0)
4234 var->stack = false;
4235 break;
4237 case DW_AT_location:
4238 switch (attr.form)
4240 case DW_FORM_block:
4241 case DW_FORM_block1:
4242 case DW_FORM_block2:
4243 case DW_FORM_block4:
4244 case DW_FORM_exprloc:
4245 if (attr.u.blk->data != NULL
4246 && *attr.u.blk->data == DW_OP_addr)
4248 var->stack = false;
4250 /* Verify that DW_OP_addr is the only opcode in the
4251 location, in which case the block size will be 1
4252 plus the address size. */
4253 /* ??? For TLS variables, gcc can emit
4254 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
4255 which we don't handle here yet. */
4256 if (attr.u.blk->size == unit->addr_size + 1U)
4257 var->addr = bfd_get (unit->addr_size * 8,
4258 unit->abfd,
4259 attr.u.blk->data + 1);
4261 break;
4263 default:
4264 break;
4266 break;
4268 default:
4269 break;
4274 if (abbrev->has_children)
4275 nesting_level++;
4277 if (high_pc_relative)
4278 high_pc += low_pc;
4280 if (func && high_pc != 0)
4282 if (!arange_add (unit, &func->arange, &unit->file->trie_root,
4283 low_pc, high_pc))
4284 goto fail;
4288 unit->function_table = reverse_funcinfo_list (unit->function_table);
4289 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4291 free (nested_funcs);
4292 return true;
4294 fail:
4295 free (nested_funcs);
4296 return false;
4299 /* Read the attributes of the form strx and addrx. */
4301 static void
4302 reread_attribute (struct comp_unit *unit,
4303 struct attribute *attr,
4304 bfd_vma *low_pc,
4305 bfd_vma *high_pc,
4306 bool *high_pc_relative,
4307 bool compunit)
4309 if (is_strx_form (attr->form))
4310 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
4311 if (is_addrx_form (attr->form))
4312 attr->u.val = read_indexed_address (attr->u.val, unit);
4314 switch (attr->name)
4316 case DW_AT_stmt_list:
4317 unit->stmtlist = 1;
4318 unit->line_offset = attr->u.val;
4319 break;
4321 case DW_AT_name:
4322 if (is_str_form (attr))
4323 unit->name = attr->u.str;
4324 break;
4326 case DW_AT_low_pc:
4327 *low_pc = attr->u.val;
4328 if (compunit)
4329 unit->base_address = *low_pc;
4330 break;
4332 case DW_AT_high_pc:
4333 *high_pc = attr->u.val;
4334 *high_pc_relative = attr->form != DW_FORM_addr;
4335 break;
4337 case DW_AT_ranges:
4338 if (!read_rangelist (unit, &unit->arange,
4339 &unit->file->trie_root, attr->u.val))
4340 return;
4341 break;
4343 case DW_AT_comp_dir:
4345 char *comp_dir = attr->u.str;
4347 if (!is_str_form (attr))
4349 _bfd_error_handler
4350 (_("DWARF error: DW_AT_comp_dir attribute encountered "
4351 "with a non-string form"));
4352 comp_dir = NULL;
4355 if (comp_dir)
4357 char *cp = strchr (comp_dir, ':');
4359 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
4360 comp_dir = cp + 1;
4362 unit->comp_dir = comp_dir;
4363 break;
4366 case DW_AT_language:
4367 unit->lang = attr->u.val;
4368 default:
4369 break;
4373 /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
4374 includes the compilation unit header that proceeds the DIE's, but
4375 does not include the length field that precedes each compilation
4376 unit header. END_PTR points one past the end of this comp unit.
4377 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
4379 This routine does not read the whole compilation unit; only enough
4380 to get to the line number information for the compilation unit. */
4382 static struct comp_unit *
4383 parse_comp_unit (struct dwarf2_debug *stash,
4384 struct dwarf2_debug_file *file,
4385 bfd_byte *info_ptr,
4386 bfd_vma unit_length,
4387 bfd_byte *info_ptr_unit,
4388 unsigned int offset_size)
4390 struct comp_unit* unit;
4391 unsigned int version;
4392 uint64_t abbrev_offset = 0;
4393 /* Initialize it just to avoid a GCC false warning. */
4394 unsigned int addr_size = -1;
4395 struct abbrev_info** abbrevs;
4396 unsigned int abbrev_number, i;
4397 struct abbrev_info *abbrev;
4398 struct attribute attr;
4399 bfd_byte *end_ptr = info_ptr + unit_length;
4400 size_t amt;
4401 bfd_vma low_pc = 0;
4402 bfd_vma high_pc = 0;
4403 bfd *abfd = file->bfd_ptr;
4404 bool high_pc_relative = false;
4405 enum dwarf_unit_type unit_type;
4406 struct attribute *str_addrp = NULL;
4407 size_t str_count = 0;
4408 size_t str_alloc = 0;
4409 bool compunit_flag = false;
4411 version = read_2_bytes (abfd, &info_ptr, end_ptr);
4412 if (version < 2 || version > 5)
4414 /* PR 19872: A version number of 0 probably means that there is padding
4415 at the end of the .debug_info section. Gold puts it there when
4416 performing an incremental link, for example. So do not generate
4417 an error, just return a NULL. */
4418 if (version)
4420 _bfd_error_handler
4421 (_("DWARF error: found dwarf version '%u', this reader"
4422 " only handles version 2, 3, 4 and 5 information"), version);
4423 bfd_set_error (bfd_error_bad_value);
4425 return NULL;
4428 if (version < 5)
4429 unit_type = DW_UT_compile;
4430 else
4432 unit_type = read_1_byte (abfd, &info_ptr, end_ptr);
4433 addr_size = read_1_byte (abfd, &info_ptr, end_ptr);
4436 BFD_ASSERT (offset_size == 4 || offset_size == 8);
4437 if (offset_size == 4)
4438 abbrev_offset = read_4_bytes (abfd, &info_ptr, end_ptr);
4439 else
4440 abbrev_offset = read_8_bytes (abfd, &info_ptr, end_ptr);
4442 if (version < 5)
4443 addr_size = read_1_byte (abfd, &info_ptr, end_ptr);
4445 switch (unit_type)
4447 case DW_UT_type:
4448 /* Skip type signature. */
4449 info_ptr += 8;
4451 /* Skip type offset. */
4452 info_ptr += offset_size;
4453 break;
4455 case DW_UT_skeleton:
4456 /* Skip DWO_id field. */
4457 info_ptr += 8;
4458 break;
4460 default:
4461 break;
4464 if (addr_size > sizeof (bfd_vma))
4466 _bfd_error_handler
4467 /* xgettext: c-format */
4468 (_("DWARF error: found address size '%u', this reader"
4469 " can not handle sizes greater than '%u'"),
4470 addr_size,
4471 (unsigned int) sizeof (bfd_vma));
4472 bfd_set_error (bfd_error_bad_value);
4473 return NULL;
4476 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
4478 _bfd_error_handler
4479 ("DWARF error: found address size '%u', this reader"
4480 " can only handle address sizes '2', '4' and '8'", addr_size);
4481 bfd_set_error (bfd_error_bad_value);
4482 return NULL;
4485 /* Read the abbrevs for this compilation unit into a table. */
4486 abbrevs = read_abbrevs (abfd, abbrev_offset, stash, file);
4487 if (! abbrevs)
4488 return NULL;
4490 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
4491 false, end_ptr);
4492 if (! abbrev_number)
4494 /* PR 19872: An abbrev number of 0 probably means that there is padding
4495 at the end of the .debug_abbrev section. Gold puts it there when
4496 performing an incremental link, for example. So do not generate
4497 an error, just return a NULL. */
4498 return NULL;
4501 abbrev = lookup_abbrev (abbrev_number, abbrevs);
4502 if (! abbrev)
4504 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
4505 abbrev_number);
4506 bfd_set_error (bfd_error_bad_value);
4507 return NULL;
4510 amt = sizeof (struct comp_unit);
4511 unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
4512 if (unit == NULL)
4513 return NULL;
4514 unit->abfd = abfd;
4515 unit->version = version;
4516 unit->addr_size = addr_size;
4517 unit->offset_size = offset_size;
4518 unit->abbrevs = abbrevs;
4519 unit->end_ptr = end_ptr;
4520 unit->stash = stash;
4521 unit->file = file;
4522 unit->info_ptr_unit = info_ptr_unit;
4524 if (abbrev->tag == DW_TAG_compile_unit)
4525 compunit_flag = true;
4527 for (i = 0; i < abbrev->num_attrs; ++i)
4529 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, end_ptr);
4530 if (info_ptr == NULL)
4531 goto err_exit;
4533 /* Identify attributes of the form strx* and addrx* which come before
4534 DW_AT_str_offsets_base and DW_AT_addr_base respectively in the CU.
4535 Store the attributes in an array and process them later. */
4536 if ((unit->dwarf_str_offset == 0 && is_strx_form (attr.form))
4537 || (unit->dwarf_addr_offset == 0 && is_addrx_form (attr.form)))
4539 if (str_count <= str_alloc)
4541 str_alloc = 2 * str_alloc + 200;
4542 str_addrp = bfd_realloc (str_addrp,
4543 str_alloc * sizeof (*str_addrp));
4544 if (str_addrp == NULL)
4545 goto err_exit;
4547 str_addrp[str_count] = attr;
4548 str_count++;
4549 continue;
4552 /* Store the data if it is of an attribute we want to keep in a
4553 partial symbol table. */
4554 switch (attr.name)
4556 case DW_AT_stmt_list:
4557 if (is_int_form (&attr))
4559 unit->stmtlist = 1;
4560 unit->line_offset = attr.u.val;
4562 break;
4564 case DW_AT_name:
4565 if (is_str_form (&attr))
4566 unit->name = attr.u.str;
4567 break;
4569 case DW_AT_low_pc:
4570 if (is_int_form (&attr))
4572 low_pc = attr.u.val;
4573 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
4574 this is the base address to use when reading location
4575 lists or range lists. */
4576 if (compunit_flag)
4577 unit->base_address = low_pc;
4579 break;
4581 case DW_AT_high_pc:
4582 if (is_int_form (&attr))
4584 high_pc = attr.u.val;
4585 high_pc_relative = attr.form != DW_FORM_addr;
4587 break;
4589 case DW_AT_ranges:
4590 if (is_int_form (&attr)
4591 && !read_rangelist (unit, &unit->arange,
4592 &unit->file->trie_root, attr.u.val))
4593 goto err_exit;
4594 break;
4596 case DW_AT_comp_dir:
4598 char *comp_dir = attr.u.str;
4600 /* PR 17512: file: 1fe726be. */
4601 if (!is_str_form (&attr))
4603 _bfd_error_handler
4604 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
4605 comp_dir = NULL;
4608 if (comp_dir)
4610 /* Irix 6.2 native cc prepends <machine>.: to the compilation
4611 directory, get rid of it. */
4612 char *cp = strchr (comp_dir, ':');
4614 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
4615 comp_dir = cp + 1;
4617 unit->comp_dir = comp_dir;
4618 break;
4621 case DW_AT_language:
4622 if (is_int_form (&attr))
4623 unit->lang = attr.u.val;
4624 break;
4626 case DW_AT_addr_base:
4627 unit->dwarf_addr_offset = attr.u.val;
4628 break;
4630 case DW_AT_str_offsets_base:
4631 unit->dwarf_str_offset = attr.u.val;
4632 break;
4634 default:
4635 break;
4639 for (i = 0; i < str_count; ++i)
4640 reread_attribute (unit, &str_addrp[i], &low_pc, &high_pc,
4641 &high_pc_relative, compunit_flag);
4643 if (high_pc_relative)
4644 high_pc += low_pc;
4645 if (high_pc != 0)
4647 if (!arange_add (unit, &unit->arange, &unit->file->trie_root,
4648 low_pc, high_pc))
4649 goto err_exit;
4652 unit->first_child_die_ptr = info_ptr;
4654 free (str_addrp);
4655 return unit;
4657 err_exit:
4658 unit->error = 1;
4659 free (str_addrp);
4660 return NULL;
4663 /* Return TRUE if UNIT may contain the address given by ADDR. When
4664 there are functions written entirely with inline asm statements, the
4665 range info in the compilation unit header may not be correct. We
4666 need to consult the line info table to see if a compilation unit
4667 really contains the given address. */
4669 static bool
4670 comp_unit_may_contain_address (struct comp_unit *unit, bfd_vma addr)
4672 struct arange *arange;
4674 if (unit->error)
4675 return false;
4677 if (unit->arange.high == 0 /* No ranges have been computed yet. */
4678 || unit->line_table == NULL) /* The line info table has not been loaded. */
4679 return true;
4681 for (arange = &unit->arange; arange != NULL; arange = arange->next)
4682 if (addr >= arange->low && addr < arange->high)
4683 return true;
4685 return false;
4688 /* If UNIT contains ADDR, set the output parameters to the values for
4689 the line containing ADDR and return TRUE. Otherwise return FALSE.
4690 The output parameters, FILENAME_PTR, FUNCTION_PTR, and
4691 LINENUMBER_PTR, are pointers to the objects to be filled in. */
4693 static bool
4694 comp_unit_find_nearest_line (struct comp_unit *unit,
4695 bfd_vma addr,
4696 const char **filename_ptr,
4697 struct funcinfo **function_ptr,
4698 unsigned int *linenumber_ptr,
4699 unsigned int *discriminator_ptr)
4701 bool line_p, func_p;
4703 if (!comp_unit_maybe_decode_line_info (unit))
4704 return false;
4706 *function_ptr = NULL;
4707 func_p = lookup_address_in_function_table (unit, addr, function_ptr);
4709 if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine)
4710 unit->stash->inliner_chain = *function_ptr;
4712 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
4713 filename_ptr,
4714 linenumber_ptr,
4715 discriminator_ptr);
4716 return line_p || func_p;
4719 /* Check to see if line info is already decoded in a comp_unit.
4720 If not, decode it. Returns TRUE if no errors were encountered;
4721 FALSE otherwise. */
4723 static bool
4724 comp_unit_maybe_decode_line_info (struct comp_unit *unit)
4726 if (unit->error)
4727 return false;
4729 if (! unit->line_table)
4731 if (! unit->stmtlist)
4733 unit->error = 1;
4734 return false;
4737 unit->line_table = decode_line_info (unit);
4739 if (! unit->line_table)
4741 unit->error = 1;
4742 return false;
4745 if (unit->first_child_die_ptr < unit->end_ptr
4746 && ! scan_unit_for_symbols (unit))
4748 unit->error = 1;
4749 return false;
4753 return true;
4756 /* If UNIT contains SYM at ADDR, set the output parameters to the
4757 values for the line containing SYM. The output parameters,
4758 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
4759 filled in.
4761 Return TRUE if UNIT contains SYM, and no errors were encountered;
4762 FALSE otherwise. */
4764 static bool
4765 comp_unit_find_line (struct comp_unit *unit,
4766 asymbol *sym,
4767 bfd_vma addr,
4768 const char **filename_ptr,
4769 unsigned int *linenumber_ptr)
4771 if (!comp_unit_maybe_decode_line_info (unit))
4772 return false;
4774 if (sym->flags & BSF_FUNCTION)
4775 return lookup_symbol_in_function_table (unit, sym, addr,
4776 filename_ptr,
4777 linenumber_ptr);
4779 return lookup_symbol_in_variable_table (unit, sym, addr,
4780 filename_ptr,
4781 linenumber_ptr);
4784 /* Extract all interesting funcinfos and varinfos of a compilation
4785 unit into hash tables for faster lookup. Returns TRUE if no
4786 errors were enountered; FALSE otherwise. */
4788 static bool
4789 comp_unit_hash_info (struct dwarf2_debug *stash,
4790 struct comp_unit *unit,
4791 struct info_hash_table *funcinfo_hash_table,
4792 struct info_hash_table *varinfo_hash_table)
4794 struct funcinfo* each_func;
4795 struct varinfo* each_var;
4796 bool okay = true;
4798 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
4800 if (!comp_unit_maybe_decode_line_info (unit))
4801 return false;
4803 BFD_ASSERT (!unit->cached);
4805 /* To preserve the original search order, we went to visit the function
4806 infos in the reversed order of the list. However, making the list
4807 bi-directional use quite a bit of extra memory. So we reverse
4808 the list first, traverse the list in the now reversed order and
4809 finally reverse the list again to get back the original order. */
4810 unit->function_table = reverse_funcinfo_list (unit->function_table);
4811 for (each_func = unit->function_table;
4812 each_func && okay;
4813 each_func = each_func->prev_func)
4815 /* Skip nameless functions. */
4816 if (each_func->name)
4817 /* There is no need to copy name string into hash table as
4818 name string is either in the dwarf string buffer or
4819 info in the stash. */
4820 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
4821 (void*) each_func, false);
4823 unit->function_table = reverse_funcinfo_list (unit->function_table);
4824 if (!okay)
4825 return false;
4827 /* We do the same for variable infos. */
4828 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4829 for (each_var = unit->variable_table;
4830 each_var && okay;
4831 each_var = each_var->prev_var)
4833 /* Skip stack vars and vars with no files or names. */
4834 if (! each_var->stack
4835 && each_var->file != NULL
4836 && each_var->name != NULL)
4837 /* There is no need to copy name string into hash table as
4838 name string is either in the dwarf string buffer or
4839 info in the stash. */
4840 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
4841 (void*) each_var, false);
4844 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4845 unit->cached = true;
4846 return okay;
4849 /* Locate a section in a BFD containing debugging info. The search starts
4850 from the section after AFTER_SEC, or from the first section in the BFD if
4851 AFTER_SEC is NULL. The search works by examining the names of the
4852 sections. There are three permissiable names. The first two are given
4853 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
4854 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
4855 This is a variation on the .debug_info section which has a checksum
4856 describing the contents appended onto the name. This allows the linker to
4857 identify and discard duplicate debugging sections for different
4858 compilation units. */
4859 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
4861 static asection *
4862 find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
4863 asection *after_sec)
4865 asection *msec;
4866 const char *look;
4868 if (after_sec == NULL)
4870 look = debug_sections[debug_info].uncompressed_name;
4871 msec = bfd_get_section_by_name (abfd, look);
4872 /* Testing SEC_HAS_CONTENTS is an anti-fuzzer measure. Of
4873 course debug sections always have contents. */
4874 if (msec != NULL && (msec->flags & SEC_HAS_CONTENTS) != 0)
4875 return msec;
4877 look = debug_sections[debug_info].compressed_name;
4878 msec = bfd_get_section_by_name (abfd, look);
4879 if (msec != NULL && (msec->flags & SEC_HAS_CONTENTS) != 0)
4880 return msec;
4882 for (msec = abfd->sections; msec != NULL; msec = msec->next)
4883 if ((msec->flags & SEC_HAS_CONTENTS) != 0
4884 && startswith (msec->name, GNU_LINKONCE_INFO))
4885 return msec;
4887 return NULL;
4890 for (msec = after_sec->next; msec != NULL; msec = msec->next)
4892 if ((msec->flags & SEC_HAS_CONTENTS) == 0)
4893 continue;
4895 look = debug_sections[debug_info].uncompressed_name;
4896 if (strcmp (msec->name, look) == 0)
4897 return msec;
4899 look = debug_sections[debug_info].compressed_name;
4900 if (look != NULL && strcmp (msec->name, look) == 0)
4901 return msec;
4903 if (startswith (msec->name, GNU_LINKONCE_INFO))
4904 return msec;
4907 return NULL;
4910 /* Transfer VMAs from object file to separate debug file. */
4912 static void
4913 set_debug_vma (bfd *orig_bfd, bfd *debug_bfd)
4915 asection *s, *d;
4917 for (s = orig_bfd->sections, d = debug_bfd->sections;
4918 s != NULL && d != NULL;
4919 s = s->next, d = d->next)
4921 if ((d->flags & SEC_DEBUGGING) != 0)
4922 break;
4923 /* ??? Assumes 1-1 correspondence between sections in the
4924 two files. */
4925 if (strcmp (s->name, d->name) == 0)
4927 d->output_section = s->output_section;
4928 d->output_offset = s->output_offset;
4929 d->vma = s->vma;
4934 /* If the dwarf2 info was found in a separate debug file, return the
4935 debug file section corresponding to the section in the original file
4936 and the debug file symbols. */
4938 static void
4939 _bfd_dwarf2_stash_syms (struct dwarf2_debug *stash, bfd *abfd,
4940 asection **sec, asymbol ***syms)
4942 if (stash->f.bfd_ptr != abfd)
4944 asection *s, *d;
4946 if (*sec == NULL)
4948 *syms = stash->f.syms;
4949 return;
4952 for (s = abfd->sections, d = stash->f.bfd_ptr->sections;
4953 s != NULL && d != NULL;
4954 s = s->next, d = d->next)
4956 if ((d->flags & SEC_DEBUGGING) != 0)
4957 break;
4958 if (s == *sec
4959 && strcmp (s->name, d->name) == 0)
4961 *sec = d;
4962 *syms = stash->f.syms;
4963 break;
4969 /* Unset vmas for adjusted sections in STASH. */
4971 static void
4972 unset_sections (struct dwarf2_debug *stash)
4974 int i;
4975 struct adjusted_section *p;
4977 i = stash->adjusted_section_count;
4978 p = stash->adjusted_sections;
4979 for (; i > 0; i--, p++)
4980 p->section->vma = p->orig_vma;
4983 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4984 relocatable object file. VMAs are normally all zero in relocatable
4985 object files, so if we want to distinguish locations in sections by
4986 address we need to set VMAs so the sections do not overlap. We
4987 also set VMA on .debug_info so that when we have multiple
4988 .debug_info sections (or the linkonce variant) they also do not
4989 overlap. The multiple .debug_info sections make up a single
4990 logical section. ??? We should probably do the same for other
4991 debug sections. */
4993 static bool
4994 place_sections (bfd *orig_bfd, struct dwarf2_debug *stash)
4996 bfd *abfd;
4997 struct adjusted_section *p;
4998 int i;
4999 const char *debug_info_name;
5001 if (stash->adjusted_section_count != 0)
5003 i = stash->adjusted_section_count;
5004 p = stash->adjusted_sections;
5005 for (; i > 0; i--, p++)
5006 p->section->vma = p->adj_vma;
5007 return true;
5010 debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
5011 i = 0;
5012 abfd = orig_bfd;
5013 while (1)
5015 asection *sect;
5017 for (sect = abfd->sections; sect != NULL; sect = sect->next)
5019 int is_debug_info;
5021 if (sect->output_section != NULL
5022 && sect->output_section != sect
5023 && (sect->flags & SEC_DEBUGGING) == 0)
5024 continue;
5026 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
5027 || startswith (sect->name, GNU_LINKONCE_INFO));
5029 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
5030 && !is_debug_info)
5031 continue;
5033 i++;
5035 if (abfd == stash->f.bfd_ptr)
5036 break;
5037 abfd = stash->f.bfd_ptr;
5040 if (i <= 1)
5041 stash->adjusted_section_count = -1;
5042 else
5044 bfd_vma last_vma = 0, last_dwarf = 0;
5045 size_t amt = i * sizeof (struct adjusted_section);
5047 p = (struct adjusted_section *) bfd_malloc (amt);
5048 if (p == NULL)
5049 return false;
5051 stash->adjusted_sections = p;
5052 stash->adjusted_section_count = i;
5054 abfd = orig_bfd;
5055 while (1)
5057 asection *sect;
5059 for (sect = abfd->sections; sect != NULL; sect = sect->next)
5061 bfd_size_type sz;
5062 int is_debug_info;
5064 if (sect->output_section != NULL
5065 && sect->output_section != sect
5066 && (sect->flags & SEC_DEBUGGING) == 0)
5067 continue;
5069 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
5070 || startswith (sect->name, GNU_LINKONCE_INFO));
5072 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
5073 && !is_debug_info)
5074 continue;
5076 sz = sect->rawsize ? sect->rawsize : sect->size;
5078 p->section = sect;
5079 p->orig_vma = sect->vma;
5081 bfd_vma *v = is_debug_info ? &last_dwarf : &last_vma;
5082 /* Align the new address to the current section
5083 alignment. */
5084 bfd_vma mask = -(bfd_vma) 1 << sect->alignment_power;
5085 *v = (*v + ~mask) & mask;
5086 sect->vma = *v;
5087 *v += sz;
5089 p->adj_vma = sect->vma;
5090 p++;
5092 if (abfd == stash->f.bfd_ptr)
5093 break;
5094 abfd = stash->f.bfd_ptr;
5098 if (orig_bfd != stash->f.bfd_ptr)
5099 set_debug_vma (orig_bfd, stash->f.bfd_ptr);
5101 return true;
5104 /* Look up a funcinfo by name using the given info hash table. If found,
5105 also update the locations pointed to by filename_ptr and linenumber_ptr.
5107 This function returns TRUE if a funcinfo that matches the given symbol
5108 and address is found with any error; otherwise it returns FALSE. */
5110 static bool
5111 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
5112 asymbol *sym,
5113 bfd_vma addr,
5114 const char **filename_ptr,
5115 unsigned int *linenumber_ptr)
5117 struct funcinfo* each_func;
5118 struct funcinfo* best_fit = NULL;
5119 bfd_vma best_fit_len = (bfd_vma) -1;
5120 struct info_list_node *node;
5121 struct arange *arange;
5122 const char *name = bfd_asymbol_name (sym);
5124 for (node = lookup_info_hash_table (hash_table, name);
5125 node;
5126 node = node->next)
5128 each_func = (struct funcinfo *) node->info;
5129 for (arange = &each_func->arange;
5130 arange;
5131 arange = arange->next)
5133 if (addr >= arange->low
5134 && addr < arange->high
5135 && arange->high - arange->low < best_fit_len)
5137 best_fit = each_func;
5138 best_fit_len = arange->high - arange->low;
5143 if (best_fit)
5145 *filename_ptr = best_fit->file;
5146 *linenumber_ptr = best_fit->line;
5147 return true;
5150 return false;
5153 /* Look up a varinfo by name using the given info hash table. If found,
5154 also update the locations pointed to by filename_ptr and linenumber_ptr.
5156 This function returns TRUE if a varinfo that matches the given symbol
5157 and address is found with any error; otherwise it returns FALSE. */
5159 static bool
5160 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
5161 asymbol *sym,
5162 bfd_vma addr,
5163 const char **filename_ptr,
5164 unsigned int *linenumber_ptr)
5166 struct varinfo* each;
5167 struct info_list_node *node;
5168 const char *name = bfd_asymbol_name (sym);
5170 for (node = lookup_info_hash_table (hash_table, name);
5171 node;
5172 node = node->next)
5174 each = (struct varinfo *) node->info;
5175 if (each->addr == addr)
5177 *filename_ptr = each->file;
5178 *linenumber_ptr = each->line;
5179 return true;
5183 return false;
5186 /* Update the funcinfo and varinfo info hash tables if they are
5187 not up to date. Returns TRUE if there is no error; otherwise
5188 returns FALSE and disable the info hash tables. */
5190 static bool
5191 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
5193 struct comp_unit *each;
5195 /* Exit if hash tables are up-to-date. */
5196 if (stash->f.all_comp_units == stash->hash_units_head)
5197 return true;
5199 if (stash->hash_units_head)
5200 each = stash->hash_units_head->prev_unit;
5201 else
5202 each = stash->f.last_comp_unit;
5204 while (each)
5206 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
5207 stash->varinfo_hash_table))
5209 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
5210 return false;
5212 each = each->prev_unit;
5215 stash->hash_units_head = stash->f.all_comp_units;
5216 return true;
5219 /* Check consistency of info hash tables. This is for debugging only. */
5221 static void ATTRIBUTE_UNUSED
5222 stash_verify_info_hash_table (struct dwarf2_debug *stash)
5224 struct comp_unit *each_unit;
5225 struct funcinfo *each_func;
5226 struct varinfo *each_var;
5227 struct info_list_node *node;
5228 bool found;
5230 for (each_unit = stash->f.all_comp_units;
5231 each_unit;
5232 each_unit = each_unit->next_unit)
5234 for (each_func = each_unit->function_table;
5235 each_func;
5236 each_func = each_func->prev_func)
5238 if (!each_func->name)
5239 continue;
5240 node = lookup_info_hash_table (stash->funcinfo_hash_table,
5241 each_func->name);
5242 BFD_ASSERT (node);
5243 found = false;
5244 while (node && !found)
5246 found = node->info == each_func;
5247 node = node->next;
5249 BFD_ASSERT (found);
5252 for (each_var = each_unit->variable_table;
5253 each_var;
5254 each_var = each_var->prev_var)
5256 if (!each_var->name || !each_var->file || each_var->stack)
5257 continue;
5258 node = lookup_info_hash_table (stash->varinfo_hash_table,
5259 each_var->name);
5260 BFD_ASSERT (node);
5261 found = false;
5262 while (node && !found)
5264 found = node->info == each_var;
5265 node = node->next;
5267 BFD_ASSERT (found);
5272 /* Check to see if we want to enable the info hash tables, which consume
5273 quite a bit of memory. Currently we only check the number times
5274 bfd_dwarf2_find_line is called. In the future, we may also want to
5275 take the number of symbols into account. */
5277 static void
5278 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
5280 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
5282 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
5283 return;
5285 /* FIXME: Maybe we should check the reduce_memory_overheads
5286 and optimize fields in the bfd_link_info structure ? */
5288 /* Create hash tables. */
5289 stash->funcinfo_hash_table = create_info_hash_table (abfd);
5290 stash->varinfo_hash_table = create_info_hash_table (abfd);
5291 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
5293 /* Turn off info hashes if any allocation above fails. */
5294 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
5295 return;
5297 /* We need a forced update so that the info hash tables will
5298 be created even though there is no compilation unit. That
5299 happens if STASH_INFO_HASH_TRIGGER is 0. */
5300 if (stash_maybe_update_info_hash_tables (stash))
5301 stash->info_hash_status = STASH_INFO_HASH_ON;
5304 /* Find the file and line associated with a symbol and address using the
5305 info hash tables of a stash. If there is a match, the function returns
5306 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
5307 otherwise it returns FALSE. */
5309 static bool
5310 stash_find_line_fast (struct dwarf2_debug *stash,
5311 asymbol *sym,
5312 bfd_vma addr,
5313 const char **filename_ptr,
5314 unsigned int *linenumber_ptr)
5316 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
5318 if (sym->flags & BSF_FUNCTION)
5319 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
5320 filename_ptr, linenumber_ptr);
5321 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
5322 filename_ptr, linenumber_ptr);
5325 /* Save current section VMAs. */
5327 static bool
5328 save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
5330 asection *s;
5331 unsigned int i;
5333 if (abfd->section_count == 0)
5334 return true;
5335 stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count);
5336 if (stash->sec_vma == NULL)
5337 return false;
5338 stash->sec_vma_count = abfd->section_count;
5339 for (i = 0, s = abfd->sections;
5340 s != NULL && i < abfd->section_count;
5341 i++, s = s->next)
5343 if (s->output_section != NULL)
5344 stash->sec_vma[i] = s->output_section->vma + s->output_offset;
5345 else
5346 stash->sec_vma[i] = s->vma;
5348 return true;
5351 /* Compare current section VMAs against those at the time the stash
5352 was created. If find_nearest_line is used in linker warnings or
5353 errors early in the link process, the debug info stash will be
5354 invalid for later calls. This is because we relocate debug info
5355 sections, so the stashed section contents depend on symbol values,
5356 which in turn depend on section VMAs. */
5358 static bool
5359 section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash)
5361 asection *s;
5362 unsigned int i;
5364 /* PR 24334: If the number of sections in ABFD has changed between
5365 when the stash was created and now, then we cannot trust the
5366 stashed vma information. */
5367 if (abfd->section_count != stash->sec_vma_count)
5368 return false;
5370 for (i = 0, s = abfd->sections;
5371 s != NULL && i < abfd->section_count;
5372 i++, s = s->next)
5374 bfd_vma vma;
5376 if (s->output_section != NULL)
5377 vma = s->output_section->vma + s->output_offset;
5378 else
5379 vma = s->vma;
5380 if (vma != stash->sec_vma[i])
5381 return false;
5383 return true;
5386 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
5387 If DEBUG_BFD is not specified, we read debug information from ABFD
5388 or its gnu_debuglink. The results will be stored in PINFO.
5389 The function returns TRUE iff debug information is ready. */
5391 bool
5392 _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
5393 const struct dwarf_debug_section *debug_sections,
5394 asymbol **symbols,
5395 void **pinfo,
5396 bool do_place)
5398 bfd_size_type total_size;
5399 asection *msec;
5400 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
5402 if (stash != NULL)
5404 if (stash->orig_bfd == abfd
5405 && section_vma_same (abfd, stash))
5407 /* Check that we did previously find some debug information
5408 before attempting to make use of it. */
5409 if (stash->f.dwarf_info_size != 0)
5411 if (do_place && !place_sections (abfd, stash))
5412 return false;
5413 return true;
5416 return false;
5418 _bfd_dwarf2_cleanup_debug_info (abfd, pinfo);
5419 memset (stash, 0, sizeof (*stash));
5421 else
5423 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, sizeof (*stash));
5424 if (! stash)
5425 return false;
5426 *pinfo = stash;
5428 stash->orig_bfd = abfd;
5429 stash->debug_sections = debug_sections;
5430 stash->f.syms = symbols;
5431 if (!save_section_vma (abfd, stash))
5432 return false;
5434 stash->f.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
5435 del_abbrev, calloc, free);
5436 if (!stash->f.abbrev_offsets)
5437 return false;
5439 stash->alt.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
5440 del_abbrev, calloc, free);
5441 if (!stash->alt.abbrev_offsets)
5442 return false;
5444 stash->f.trie_root = alloc_trie_leaf (abfd);
5445 if (!stash->f.trie_root)
5446 return false;
5448 stash->alt.trie_root = alloc_trie_leaf (abfd);
5449 if (!stash->alt.trie_root)
5450 return false;
5452 if (debug_bfd == NULL)
5453 debug_bfd = abfd;
5455 msec = find_debug_info (debug_bfd, debug_sections, NULL);
5456 if (msec == NULL && abfd == debug_bfd)
5458 char * debug_filename;
5460 debug_filename = bfd_follow_build_id_debuglink (abfd, DEBUGDIR);
5461 if (debug_filename == NULL)
5462 debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
5464 if (debug_filename == NULL)
5465 /* No dwarf2 info, and no gnu_debuglink to follow.
5466 Note that at this point the stash has been allocated, but
5467 contains zeros. This lets future calls to this function
5468 fail more quickly. */
5469 return false;
5471 debug_bfd = bfd_openr (debug_filename, NULL);
5472 free (debug_filename);
5473 if (debug_bfd == NULL)
5474 /* FIXME: Should we report our failure to follow the debuglink ? */
5475 return false;
5477 /* Set BFD_DECOMPRESS to decompress debug sections. */
5478 debug_bfd->flags |= BFD_DECOMPRESS;
5479 if (!bfd_check_format (debug_bfd, bfd_object)
5480 || (msec = find_debug_info (debug_bfd,
5481 debug_sections, NULL)) == NULL
5482 || !bfd_generic_link_read_symbols (debug_bfd))
5484 bfd_close (debug_bfd);
5485 return false;
5488 symbols = bfd_get_outsymbols (debug_bfd);
5489 stash->f.syms = symbols;
5490 stash->close_on_cleanup = true;
5492 stash->f.bfd_ptr = debug_bfd;
5494 if (do_place
5495 && !place_sections (abfd, stash))
5496 return false;
5498 /* There can be more than one DWARF2 info section in a BFD these
5499 days. First handle the easy case when there's only one. If
5500 there's more than one, try case two: read them all in and produce
5501 one large stash. We do this in two passes - in the first pass we
5502 just accumulate the section sizes, and in the second pass we
5503 read in the section's contents. (The allows us to avoid
5504 reallocing the data as we add sections to the stash.) */
5506 if (! find_debug_info (debug_bfd, debug_sections, msec))
5508 /* Case 1: only one info section. */
5509 total_size = msec->size;
5510 if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
5511 symbols, 0,
5512 &stash->f.dwarf_info_buffer, &total_size))
5513 goto restore_vma;
5515 else
5517 /* Case 2: multiple sections. */
5518 for (total_size = 0;
5519 msec;
5520 msec = find_debug_info (debug_bfd, debug_sections, msec))
5522 if (bfd_section_size_insane (debug_bfd, msec))
5523 goto restore_vma;
5524 /* Catch PR25070 testcase overflowing size calculation here. */
5525 if (total_size + msec->size < total_size)
5527 bfd_set_error (bfd_error_no_memory);
5528 goto restore_vma;
5530 total_size += msec->size;
5533 stash->f.dwarf_info_buffer = (bfd_byte *) bfd_malloc (total_size);
5534 if (stash->f.dwarf_info_buffer == NULL)
5535 goto restore_vma;
5537 total_size = 0;
5538 for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
5539 msec;
5540 msec = find_debug_info (debug_bfd, debug_sections, msec))
5542 bfd_size_type size;
5544 size = msec->size;
5545 if (size == 0)
5546 continue;
5548 if (!(bfd_simple_get_relocated_section_contents
5549 (debug_bfd, msec, stash->f.dwarf_info_buffer + total_size,
5550 symbols)))
5551 goto restore_vma;
5553 total_size += size;
5557 stash->f.info_ptr = stash->f.dwarf_info_buffer;
5558 stash->f.dwarf_info_size = total_size;
5559 return true;
5561 restore_vma:
5562 unset_sections (stash);
5563 return false;
5566 /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
5568 static struct comp_unit *
5569 stash_comp_unit (struct dwarf2_debug *stash, struct dwarf2_debug_file *file)
5571 bfd_size_type length;
5572 unsigned int offset_size;
5573 bfd_byte *info_ptr_unit = file->info_ptr;
5574 bfd_byte *info_ptr_end = file->dwarf_info_buffer + file->dwarf_info_size;
5576 if (file->info_ptr >= info_ptr_end)
5577 return NULL;
5579 length = read_4_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5580 /* A 0xffffff length is the DWARF3 way of indicating
5581 we use 64-bit offsets, instead of 32-bit offsets. */
5582 if (length == 0xffffffff)
5584 offset_size = 8;
5585 length = read_8_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5587 /* A zero length is the IRIX way of indicating 64-bit offsets,
5588 mostly because the 64-bit length will generally fit in 32
5589 bits, and the endianness helps. */
5590 else if (length == 0)
5592 offset_size = 8;
5593 length = read_4_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5595 /* In the absence of the hints above, we assume 32-bit DWARF2
5596 offsets even for targets with 64-bit addresses, because:
5597 a) most of the time these targets will not have generated
5598 more than 2Gb of debug info and so will not need 64-bit
5599 offsets,
5601 b) if they do use 64-bit offsets but they are not using
5602 the size hints that are tested for above then they are
5603 not conforming to the DWARF3 standard anyway. */
5604 else
5605 offset_size = 4;
5607 if (length != 0
5608 && length <= (size_t) (info_ptr_end - file->info_ptr))
5610 struct comp_unit *each = parse_comp_unit (stash, file,
5611 file->info_ptr, length,
5612 info_ptr_unit, offset_size);
5613 if (each)
5615 if (file->comp_unit_tree == NULL)
5616 file->comp_unit_tree
5617 = splay_tree_new (splay_tree_compare_addr_range,
5618 splay_tree_free_addr_range, NULL);
5620 struct addr_range *r
5621 = (struct addr_range *)bfd_malloc (sizeof (struct addr_range));
5622 r->start = each->info_ptr_unit;
5623 r->end = each->end_ptr;
5624 splay_tree_node v = splay_tree_lookup (file->comp_unit_tree,
5625 (splay_tree_key)r);
5626 if (v != NULL || r->end <= r->start)
5627 abort ();
5628 splay_tree_insert (file->comp_unit_tree, (splay_tree_key)r,
5629 (splay_tree_value)each);
5631 if (file->all_comp_units)
5632 file->all_comp_units->prev_unit = each;
5633 else
5634 file->last_comp_unit = each;
5636 each->next_unit = file->all_comp_units;
5637 file->all_comp_units = each;
5639 if (each->arange.high == 0)
5641 each->next_unit_without_ranges = file->all_comp_units_without_ranges;
5642 file->all_comp_units_without_ranges = each->next_unit_without_ranges;
5645 file->info_ptr += length;
5646 return each;
5650 /* Don't trust any of the DWARF info after a corrupted length or
5651 parse error. */
5652 file->info_ptr = info_ptr_end;
5653 return NULL;
5656 /* Hash function for an asymbol. */
5658 static hashval_t
5659 hash_asymbol (const void *sym)
5661 const asymbol *asym = sym;
5662 return htab_hash_string (asym->name);
5665 /* Equality function for asymbols. */
5667 static int
5668 eq_asymbol (const void *a, const void *b)
5670 const asymbol *sa = a;
5671 const asymbol *sb = b;
5672 return strcmp (sa->name, sb->name) == 0;
5675 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
5676 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
5677 symbol in SYMBOLS and return the difference between the low_pc and
5678 the symbol's address. Returns 0 if no suitable symbol could be found. */
5680 bfd_signed_vma
5681 _bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo)
5683 struct dwarf2_debug *stash;
5684 struct comp_unit * unit;
5685 htab_t sym_hash;
5686 bfd_signed_vma result = 0;
5687 asymbol ** psym;
5689 stash = (struct dwarf2_debug *) *pinfo;
5691 if (stash == NULL || symbols == NULL)
5692 return 0;
5694 sym_hash = htab_create_alloc (10, hash_asymbol, eq_asymbol,
5695 NULL, xcalloc, free);
5696 for (psym = symbols; * psym != NULL; psym++)
5698 asymbol * sym = * psym;
5700 if (sym->flags & BSF_FUNCTION && sym->section != NULL)
5702 void **slot = htab_find_slot (sym_hash, sym, INSERT);
5703 *slot = sym;
5707 for (unit = stash->f.all_comp_units; unit; unit = unit->next_unit)
5709 struct funcinfo * func;
5711 comp_unit_maybe_decode_line_info (unit);
5713 for (func = unit->function_table; func != NULL; func = func->prev_func)
5714 if (func->name && func->arange.low)
5716 asymbol search, *sym;
5718 /* FIXME: Do we need to scan the aranges looking for the
5719 lowest pc value? */
5721 search.name = func->name;
5722 sym = htab_find (sym_hash, &search);
5723 if (sym != NULL)
5725 result = func->arange.low - (sym->value + sym->section->vma);
5726 goto done;
5731 done:
5732 htab_delete (sym_hash);
5733 return result;
5736 /* See _bfd_dwarf2_find_nearest_line_with_alt. */
5739 _bfd_dwarf2_find_nearest_line (bfd *abfd,
5740 asymbol **symbols,
5741 asymbol *symbol,
5742 asection *section,
5743 bfd_vma offset,
5744 const char **filename_ptr,
5745 const char **functionname_ptr,
5746 unsigned int *linenumber_ptr,
5747 unsigned int *discriminator_ptr,
5748 const struct dwarf_debug_section *debug_sections,
5749 void **pinfo)
5751 return _bfd_dwarf2_find_nearest_line_with_alt
5752 (abfd, NULL, symbols, symbol, section, offset, filename_ptr,
5753 functionname_ptr, linenumber_ptr, discriminator_ptr, debug_sections,
5754 pinfo);
5757 /* Find the source code location of SYMBOL. If SYMBOL is NULL
5758 then find the nearest source code location corresponding to
5759 the address SECTION + OFFSET.
5760 Returns 1 if the line is found without error and fills in
5761 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
5762 NULL the FUNCTIONNAME_PTR is also filled in.
5763 Returns 2 if partial information from _bfd_elf_find_function is
5764 returned (function and maybe file) by looking at symbols. DWARF2
5765 info is present but not regarding the requested code location.
5766 Returns 0 otherwise.
5767 SYMBOLS contains the symbol table for ABFD.
5768 DEBUG_SECTIONS contains the name of the dwarf debug sections.
5769 If ALT_FILENAME is given, attempt to open the file and use it
5770 as the .gnu_debugaltlink file. Otherwise this file will be
5771 searched for when needed. */
5774 _bfd_dwarf2_find_nearest_line_with_alt
5775 (bfd *abfd,
5776 const char *alt_filename,
5777 asymbol **symbols,
5778 asymbol *symbol,
5779 asection *section,
5780 bfd_vma offset,
5781 const char **filename_ptr,
5782 const char **functionname_ptr,
5783 unsigned int *linenumber_ptr,
5784 unsigned int *discriminator_ptr,
5785 const struct dwarf_debug_section *debug_sections,
5786 void **pinfo)
5788 /* Read each compilation unit from the section .debug_info, and check
5789 to see if it contains the address we are searching for. If yes,
5790 lookup the address, and return the line number info. If no, go
5791 on to the next compilation unit.
5793 We keep a list of all the previously read compilation units, and
5794 a pointer to the next un-read compilation unit. Check the
5795 previously read units before reading more. */
5796 struct dwarf2_debug *stash;
5797 /* What address are we looking for? */
5798 bfd_vma addr;
5799 struct comp_unit* each;
5800 struct funcinfo *function = NULL;
5801 int found = false;
5802 bool do_line;
5804 *filename_ptr = NULL;
5805 if (functionname_ptr != NULL)
5806 *functionname_ptr = NULL;
5807 *linenumber_ptr = 0;
5808 if (discriminator_ptr)
5809 *discriminator_ptr = 0;
5811 if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections,
5812 symbols, pinfo,
5813 (abfd->flags & (EXEC_P | DYNAMIC)) == 0))
5814 return false;
5816 stash = (struct dwarf2_debug *) *pinfo;
5818 if (stash->alt.bfd_ptr == NULL && alt_filename != NULL)
5820 bfd *alt_bfd = bfd_openr (alt_filename, NULL);
5822 if (alt_bfd == NULL)
5823 /* bfd_openr will have set the bfd_error. */
5824 return false;
5825 if (!bfd_check_format (alt_bfd, bfd_object))
5827 bfd_set_error (bfd_error_wrong_format);
5828 bfd_close (alt_bfd);
5829 return false;
5832 stash->alt.bfd_ptr = alt_bfd;
5835 do_line = symbol != NULL;
5836 if (do_line)
5838 BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL);
5839 section = bfd_asymbol_section (symbol);
5840 addr = symbol->value;
5842 else
5844 BFD_ASSERT (section != NULL && functionname_ptr != NULL);
5845 addr = offset;
5847 /* If we have no SYMBOL but the section we're looking at is not a
5848 code section, then take a look through the list of symbols to see
5849 if we have a symbol at the address we're looking for. If we do
5850 then use this to look up line information. This will allow us to
5851 give file and line results for data symbols. We exclude code
5852 symbols here, if we look up a function symbol and then look up the
5853 line information we'll actually return the line number for the
5854 opening '{' rather than the function definition line. This is
5855 because looking up by symbol uses the line table, in which the
5856 first line for a function is usually the opening '{', while
5857 looking up the function by section + offset uses the
5858 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
5859 which will be the line of the function name. */
5860 if (symbols != NULL && (section->flags & SEC_CODE) == 0)
5862 asymbol **tmp;
5864 for (tmp = symbols; (*tmp) != NULL; ++tmp)
5865 if ((*tmp)->the_bfd == abfd
5866 && (*tmp)->section == section
5867 && (*tmp)->value == offset
5868 && ((*tmp)->flags & BSF_SECTION_SYM) == 0)
5870 symbol = *tmp;
5871 do_line = true;
5872 /* For local symbols, keep going in the hope we find a
5873 global. */
5874 if ((symbol->flags & BSF_GLOBAL) != 0)
5875 break;
5880 if (section->output_section)
5881 addr += section->output_section->vma + section->output_offset;
5882 else
5883 addr += section->vma;
5885 /* A null info_ptr indicates that there is no dwarf2 info
5886 (or that an error occured while setting up the stash). */
5887 if (! stash->f.info_ptr)
5888 return false;
5890 stash->inliner_chain = NULL;
5892 /* Check the previously read comp. units first. */
5893 if (do_line)
5895 /* The info hash tables use quite a bit of memory. We may not want to
5896 always use them. We use some heuristics to decide if and when to
5897 turn it on. */
5898 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
5899 stash_maybe_enable_info_hash_tables (abfd, stash);
5901 /* Keep info hash table up to date if they are available. Note that we
5902 may disable the hash tables if there is any error duing update. */
5903 if (stash->info_hash_status == STASH_INFO_HASH_ON)
5904 stash_maybe_update_info_hash_tables (stash);
5906 if (stash->info_hash_status == STASH_INFO_HASH_ON)
5908 found = stash_find_line_fast (stash, symbol, addr,
5909 filename_ptr, linenumber_ptr);
5910 if (found)
5911 goto done;
5914 /* Check the previously read comp. units first. */
5915 for (each = stash->f.all_comp_units; each; each = each->next_unit)
5916 if ((symbol->flags & BSF_FUNCTION) == 0
5917 || comp_unit_may_contain_address (each, addr))
5919 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
5920 linenumber_ptr);
5921 if (found)
5922 goto done;
5925 else
5927 struct trie_node *trie = stash->f.trie_root;
5928 unsigned int bits = VMA_BITS - 8;
5929 struct comp_unit **prev_each;
5931 /* Traverse interior nodes until we get to a leaf. */
5932 while (trie && trie->num_room_in_leaf == 0)
5934 int ch = (addr >> bits) & 0xff;
5935 trie = ((struct trie_interior *) trie)->children[ch];
5936 bits -= 8;
5939 if (trie)
5941 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
5942 unsigned int i;
5944 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
5945 leaf->ranges[i].unit->mark = false;
5947 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
5949 struct comp_unit *unit = leaf->ranges[i].unit;
5950 if (unit->mark
5951 || addr < leaf->ranges[i].low_pc
5952 || addr >= leaf->ranges[i].high_pc)
5953 continue;
5954 unit->mark = true;
5956 found = comp_unit_find_nearest_line (unit, addr,
5957 filename_ptr,
5958 &function,
5959 linenumber_ptr,
5960 discriminator_ptr);
5961 if (found)
5962 goto done;
5966 /* Also scan through all compilation units without any ranges,
5967 taking them out of the list if they have acquired any since
5968 last time. */
5969 prev_each = &stash->f.all_comp_units_without_ranges;
5970 for (each = *prev_each; each; each = each->next_unit_without_ranges)
5972 if (each->arange.high != 0)
5974 *prev_each = each->next_unit_without_ranges;
5975 continue;
5978 found = comp_unit_find_nearest_line (each, addr,
5979 filename_ptr,
5980 &function,
5981 linenumber_ptr,
5982 discriminator_ptr);
5983 if (found)
5984 goto done;
5985 prev_each = &each->next_unit_without_ranges;
5989 /* Read each remaining comp. units checking each as they are read. */
5990 while ((each = stash_comp_unit (stash, &stash->f)) != NULL)
5992 /* DW_AT_low_pc and DW_AT_high_pc are optional for
5993 compilation units. If we don't have them (i.e.,
5994 unit->high == 0), we need to consult the line info table
5995 to see if a compilation unit contains the given
5996 address. */
5997 if (do_line)
5998 found = (((symbol->flags & BSF_FUNCTION) == 0
5999 || comp_unit_may_contain_address (each, addr))
6000 && comp_unit_find_line (each, symbol, addr,
6001 filename_ptr, linenumber_ptr));
6002 else
6003 found = (comp_unit_may_contain_address (each, addr)
6004 && comp_unit_find_nearest_line (each, addr,
6005 filename_ptr,
6006 &function,
6007 linenumber_ptr,
6008 discriminator_ptr));
6010 if (found)
6011 break;
6014 done:
6015 if (functionname_ptr && function && function->is_linkage)
6017 *functionname_ptr = function->name;
6018 if (!found)
6019 found = 2;
6021 else if (functionname_ptr
6022 && (!*functionname_ptr
6023 || (function && !function->is_linkage)))
6025 asymbol *fun;
6026 asymbol **syms = symbols;
6027 asection *sec = section;
6029 _bfd_dwarf2_stash_syms (stash, abfd, &sec, &syms);
6030 fun = _bfd_elf_find_function (abfd, syms, sec, offset,
6031 *filename_ptr ? NULL : filename_ptr,
6032 functionname_ptr);
6034 if (!found && fun != NULL)
6035 found = 2;
6037 if (function && !function->is_linkage)
6039 bfd_vma sec_vma;
6041 sec_vma = section->vma;
6042 if (section->output_section != NULL)
6043 sec_vma = section->output_section->vma + section->output_offset;
6044 if (fun == NULL)
6045 *functionname_ptr = function->name;
6046 else if (fun->value + sec_vma == function->arange.low)
6047 function->name = *functionname_ptr;
6048 /* Even if we didn't find a linkage name, say that we have
6049 to stop a repeated search of symbols. */
6050 function->is_linkage = true;
6054 unset_sections (stash);
6056 return found;
6059 bool
6060 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
6061 const char **filename_ptr,
6062 const char **functionname_ptr,
6063 unsigned int *linenumber_ptr,
6064 void **pinfo)
6066 struct dwarf2_debug *stash;
6068 stash = (struct dwarf2_debug *) *pinfo;
6069 if (stash)
6071 struct funcinfo *func = stash->inliner_chain;
6073 if (func && func->caller_func)
6075 *filename_ptr = func->caller_file;
6076 *functionname_ptr = func->caller_func->name;
6077 *linenumber_ptr = func->caller_line;
6078 stash->inliner_chain = func->caller_func;
6079 return true;
6083 return false;
6086 void
6087 _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
6089 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
6090 struct comp_unit *each;
6091 struct dwarf2_debug_file *file;
6093 if (abfd == NULL || stash == NULL)
6094 return;
6096 if (stash->varinfo_hash_table)
6097 bfd_hash_table_free (&stash->varinfo_hash_table->base);
6098 if (stash->funcinfo_hash_table)
6099 bfd_hash_table_free (&stash->funcinfo_hash_table->base);
6101 file = &stash->f;
6102 while (1)
6104 for (each = file->all_comp_units; each; each = each->next_unit)
6106 struct funcinfo *function_table = each->function_table;
6107 struct varinfo *variable_table = each->variable_table;
6109 if (each->line_table && each->line_table != file->line_table)
6111 free (each->line_table->files);
6112 free (each->line_table->dirs);
6115 free (each->lookup_funcinfo_table);
6116 each->lookup_funcinfo_table = NULL;
6118 while (function_table)
6120 free (function_table->file);
6121 function_table->file = NULL;
6122 free (function_table->caller_file);
6123 function_table->caller_file = NULL;
6124 function_table = function_table->prev_func;
6127 while (variable_table)
6129 free (variable_table->file);
6130 variable_table->file = NULL;
6131 variable_table = variable_table->prev_var;
6135 if (file->line_table)
6137 free (file->line_table->files);
6138 free (file->line_table->dirs);
6140 htab_delete (file->abbrev_offsets);
6141 if (file->comp_unit_tree != NULL)
6142 splay_tree_delete (file->comp_unit_tree);
6144 free (file->dwarf_line_str_buffer);
6145 free (file->dwarf_str_buffer);
6146 free (file->dwarf_ranges_buffer);
6147 free (file->dwarf_rnglists_buffer);
6148 free (file->dwarf_line_buffer);
6149 free (file->dwarf_abbrev_buffer);
6150 free (file->dwarf_info_buffer);
6151 free (file->dwarf_addr_buffer);
6152 free (file->dwarf_str_offsets_buffer);
6153 if (file == &stash->alt)
6154 break;
6155 file = &stash->alt;
6157 free (stash->sec_vma);
6158 free (stash->adjusted_sections);
6159 if (stash->close_on_cleanup)
6160 bfd_close (stash->f.bfd_ptr);
6161 if (stash->alt.bfd_ptr)
6162 bfd_close (stash->alt.bfd_ptr);
6165 typedef struct elf_find_function_cache
6167 asection * last_section;
6168 asymbol * func;
6169 const char * filename;
6170 bfd_size_type code_size;
6171 bfd_vma code_off;
6173 } elf_find_function_cache;
6176 /* Returns TRUE if symbol SYM with address CODE_OFF and size CODE_SIZE
6177 is a better fit to match OFFSET than whatever is currenly stored in
6178 CACHE. */
6180 static inline bool
6181 better_fit (elf_find_function_cache * cache,
6182 asymbol * sym,
6183 bfd_vma code_off,
6184 bfd_size_type code_size,
6185 bfd_vma offset)
6187 /* If the symbol is beyond the desired offset, ignore it. */
6188 if (code_off > offset)
6189 return false;
6191 /* If the symbol is further away from the desired
6192 offset than our current best, then ignore it. */
6193 if (code_off < cache->code_off)
6194 return false;
6196 /* On the other hand, if it is closer, then use it. */
6197 if (code_off > cache->code_off)
6198 return true;
6200 /* assert (code_off == cache->code_off); */
6202 /* If our current best fit does not actually reach the desired
6203 offset... */
6204 if (cache->code_off + cache->code_size <= offset)
6205 /* ... then return whichever candidate covers
6206 more area and hence gets closer to OFFSET. */
6207 return code_size > cache->code_size;
6209 /* The current cache'd symbol covers OFFSET. */
6211 /* If the new symbol does not cover the desired offset then skip it. */
6212 if (code_off + code_size <= offset)
6213 return false;
6215 /* Both symbols cover OFFSET. */
6217 /* Prefer functions over non-functions. */
6218 flagword cache_flags = cache->func->flags;
6219 flagword sym_flags = sym->flags;
6221 if ((cache_flags & BSF_FUNCTION) && ((sym_flags & BSF_FUNCTION) == 0))
6222 return false;
6223 if ((sym_flags & BSF_FUNCTION) && ((cache_flags & BSF_FUNCTION) == 0))
6224 return true;
6226 /* FIXME: Should we choose LOCAL over GLOBAL ? */
6228 /* Prefer typed symbols over notyped. */
6229 int cache_type = ELF_ST_TYPE (((elf_symbol_type *) cache->func)->internal_elf_sym.st_info);
6230 int sym_type = ELF_ST_TYPE (((elf_symbol_type *) sym)->internal_elf_sym.st_info);
6232 if (cache_type == STT_NOTYPE && sym_type != STT_NOTYPE)
6233 return true;
6234 if (cache_type != STT_NOTYPE && sym_type == STT_NOTYPE)
6235 return false;
6237 /* Otherwise choose whichever symbol covers a smaller area. */
6238 return code_size < cache->code_size;
6241 /* Find the function to a particular section and offset,
6242 for error reporting. */
6244 asymbol *
6245 _bfd_elf_find_function (bfd *abfd,
6246 asymbol **symbols,
6247 asection *section,
6248 bfd_vma offset,
6249 const char **filename_ptr,
6250 const char **functionname_ptr)
6252 if (symbols == NULL)
6253 return NULL;
6255 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6256 return NULL;
6258 elf_find_function_cache * cache = elf_tdata (abfd)->elf_find_function_cache;
6260 if (cache == NULL)
6262 cache = bfd_zalloc (abfd, sizeof (*cache));
6263 elf_tdata (abfd)->elf_find_function_cache = cache;
6264 if (cache == NULL)
6265 return NULL;
6268 if (cache->last_section != section
6269 || cache->func == NULL
6270 || offset < cache->func->value
6271 || offset >= cache->func->value + cache->code_size)
6273 asymbol *file;
6274 asymbol **p;
6275 /* ??? Given multiple file symbols, it is impossible to reliably
6276 choose the right file name for global symbols. File symbols are
6277 local symbols, and thus all file symbols must sort before any
6278 global symbols. The ELF spec may be interpreted to say that a
6279 file symbol must sort before other local symbols, but currently
6280 ld -r doesn't do this. So, for ld -r output, it is possible to
6281 make a better choice of file name for local symbols by ignoring
6282 file symbols appearing after a given local symbol. */
6283 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
6284 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6286 file = NULL;
6287 state = nothing_seen;
6288 cache->filename = NULL;
6289 cache->func = NULL;
6290 cache->code_size = 0;
6291 cache->code_off = 0;
6292 cache->last_section = section;
6294 for (p = symbols; *p != NULL; p++)
6296 asymbol *sym = *p;
6297 bfd_vma code_off;
6298 bfd_size_type size;
6300 if ((sym->flags & BSF_FILE) != 0)
6302 file = sym;
6303 if (state == symbol_seen)
6304 state = file_after_symbol_seen;
6305 continue;
6308 if (state == nothing_seen)
6309 state = symbol_seen;
6311 size = bed->maybe_function_sym (sym, section, &code_off);
6313 if (size == 0)
6314 continue;
6316 if (better_fit (cache, sym, code_off, size, offset))
6318 cache->func = sym;
6319 cache->code_size = size;
6320 cache->code_off = code_off;
6321 cache->filename = NULL;
6323 if (file != NULL
6324 && ((sym->flags & BSF_LOCAL) != 0
6325 || state != file_after_symbol_seen))
6326 cache->filename = bfd_asymbol_name (file);
6328 /* Otherwise, if the symbol is beyond the desired offset but it
6329 lies within the bounds of the current best match then reduce
6330 the size of the current best match so that future searches
6331 will not not used the cached symbol by mistake. */
6332 else if (code_off > offset
6333 && code_off > cache->code_off
6334 && code_off < cache->code_off + cache->code_size)
6336 cache->code_size = code_off - cache->code_off;
6341 if (cache->func == NULL)
6342 return NULL;
6344 if (filename_ptr)
6345 *filename_ptr = cache->filename;
6346 if (functionname_ptr)
6347 *functionname_ptr = bfd_asymbol_name (cache->func);
6349 return cache->func;