Fix snafu in version number. Regenerate files
[binutils-gdb.git] / bfd / dwarf2.c
blob4ec0053a11154276271a22e03db4f3da32331726
1 /* DWARF 2 support.
2 Copyright (C) 1994-2023 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;
87 /* A trie to map quickly from address range to compilation unit.
89 This is a fairly standard radix-256 trie, used to quickly locate which
90 compilation unit any given address belongs to. Given that each compilation
91 unit may register hundreds of very small and unaligned ranges (which may
92 potentially overlap, due to inlining and other concerns), and a large
93 program may end up containing hundreds of thousands of such ranges, we cannot
94 scan through them linearly without undue slowdown.
96 We use a hybrid trie to avoid memory explosion: There are two types of trie
97 nodes, leaves and interior nodes. (Almost all nodes are leaves, so they
98 take up the bulk of the memory usage.) Leaves contain a simple array of
99 ranges (high/low address) and which compilation unit contains those ranges,
100 and when we get to a leaf, we scan through it linearly. Interior nodes
101 contain pointers to 256 other nodes, keyed by the next byte of the address.
102 So for a 64-bit address like 0x1234567abcd, we would start at the root and go
103 down child[0x00]->child[0x00]->child[0x01]->child[0x23]->child[0x45] etc.,
104 until we hit a leaf. (Nodes are, in general, leaves until they exceed the
105 default allocation of 16 elements, at which point they are converted to
106 interior node if possible.) This gives us near-constant lookup times;
107 the only thing that can be costly is if there are lots of overlapping ranges
108 within a single 256-byte segment of the binary, in which case we have to
109 scan through them all to find the best match.
111 For a binary with few ranges, we will in practice only have a single leaf
112 node at the root, containing a simple array. Thus, the scheme is efficient
113 for both small and large binaries.
116 /* Experiments have shown 16 to be a memory-efficient default leaf size.
117 The only case where a leaf will hold more memory than this, is at the
118 bottomost level (covering 256 bytes in the binary), where we'll expand
119 the leaf to be able to hold more ranges if needed.
121 #define TRIE_LEAF_SIZE 16
123 /* All trie_node pointers will really be trie_leaf or trie_interior,
124 but they have this common head. */
125 struct trie_node
127 /* If zero, we are an interior node.
128 Otherwise, how many ranges we have room for in this leaf. */
129 unsigned int num_room_in_leaf;
132 struct trie_leaf
134 struct trie_node head;
135 unsigned int num_stored_in_leaf;
136 struct {
137 struct comp_unit *unit;
138 bfd_vma low_pc, high_pc;
139 } ranges[TRIE_LEAF_SIZE];
142 struct trie_interior
144 struct trie_node head;
145 struct trie_node *children[256];
148 static struct trie_node *alloc_trie_leaf (bfd *abfd)
150 struct trie_leaf *leaf = bfd_zalloc (abfd, sizeof (struct trie_leaf));
151 if (leaf == NULL)
152 return NULL;
153 leaf->head.num_room_in_leaf = TRIE_LEAF_SIZE;
154 return &leaf->head;
157 struct addr_range
159 bfd_byte *start;
160 bfd_byte *end;
163 /* Return true if address range do intersect. */
165 static bool
166 addr_range_intersects (struct addr_range *r1, struct addr_range *r2)
168 return (r1->start <= r2->start && r2->start < r1->end)
169 || (r1->start <= (r2->end - 1) && (r2->end - 1) < r1->end);
172 /* Compare function for splay tree of addr_ranges. */
174 static int
175 splay_tree_compare_addr_range (splay_tree_key xa, splay_tree_key xb)
177 struct addr_range *r1 = (struct addr_range *) xa;
178 struct addr_range *r2 = (struct addr_range *) xb;
180 if (addr_range_intersects (r1, r2) || addr_range_intersects (r2, r1))
181 return 0;
182 else if (r1->end <= r2->start)
183 return -1;
184 else
185 return 1;
188 /* Splay tree release function for keys (addr_range). */
190 static void
191 splay_tree_free_addr_range (splay_tree_key key)
193 free ((struct addr_range *)key);
196 struct dwarf2_debug_file
198 /* The actual bfd from which debug info was loaded. Might be
199 different to orig_bfd because of gnu_debuglink sections. */
200 bfd *bfd_ptr;
202 /* Pointer to the symbol table. */
203 asymbol **syms;
205 /* The current info pointer for the .debug_info section being parsed. */
206 bfd_byte *info_ptr;
208 /* A pointer to the memory block allocated for .debug_info sections. */
209 bfd_byte *dwarf_info_buffer;
211 /* Length of the loaded .debug_info sections. */
212 bfd_size_type dwarf_info_size;
214 /* Pointer to the .debug_abbrev section loaded into memory. */
215 bfd_byte *dwarf_abbrev_buffer;
217 /* Length of the loaded .debug_abbrev section. */
218 bfd_size_type dwarf_abbrev_size;
220 /* Buffer for decode_line_info. */
221 bfd_byte *dwarf_line_buffer;
223 /* Length of the loaded .debug_line section. */
224 bfd_size_type dwarf_line_size;
226 /* Pointer to the .debug_str section loaded into memory. */
227 bfd_byte *dwarf_str_buffer;
229 /* Length of the loaded .debug_str section. */
230 bfd_size_type dwarf_str_size;
232 /* Pointer to the .debug_str_offsets section loaded into memory. */
233 bfd_byte *dwarf_str_offsets_buffer;
235 /* Length of the loaded .debug_str_offsets section. */
236 bfd_size_type dwarf_str_offsets_size;
238 /* Pointer to the .debug_addr section loaded into memory. */
239 bfd_byte *dwarf_addr_buffer;
241 /* Length of the loaded .debug_addr section. */
242 bfd_size_type dwarf_addr_size;
244 /* Pointer to the .debug_line_str section loaded into memory. */
245 bfd_byte *dwarf_line_str_buffer;
247 /* Length of the loaded .debug_line_str section. */
248 bfd_size_type dwarf_line_str_size;
250 /* Pointer to the .debug_ranges section loaded into memory. */
251 bfd_byte *dwarf_ranges_buffer;
253 /* Length of the loaded .debug_ranges section. */
254 bfd_size_type dwarf_ranges_size;
256 /* Pointer to the .debug_rnglists section loaded into memory. */
257 bfd_byte *dwarf_rnglists_buffer;
259 /* Length of the loaded .debug_rnglists section. */
260 bfd_size_type dwarf_rnglists_size;
262 /* A list of all previously read comp_units. */
263 struct comp_unit *all_comp_units;
265 /* A list of all previously read comp_units with no ranges (yet). */
266 struct comp_unit *all_comp_units_without_ranges;
268 /* Last comp unit in list above. */
269 struct comp_unit *last_comp_unit;
271 /* Line table at line_offset zero. */
272 struct line_info_table *line_table;
274 /* Hash table to map offsets to decoded abbrevs. */
275 htab_t abbrev_offsets;
277 /* Root of a trie to map addresses to compilation units. */
278 struct trie_node *trie_root;
280 /* Splay tree to map info_ptr address to compilation units. */
281 splay_tree comp_unit_tree;
284 struct dwarf2_debug
286 /* Names of the debug sections. */
287 const struct dwarf_debug_section *debug_sections;
289 /* Per-file stuff. */
290 struct dwarf2_debug_file f, alt;
292 /* Pointer to the original bfd for which debug was loaded. This is what
293 we use to compare and so check that the cached debug data is still
294 valid - it saves having to possibly dereference the gnu_debuglink each
295 time. */
296 bfd *orig_bfd;
298 /* If the most recent call to bfd_find_nearest_line was given an
299 address in an inlined function, preserve a pointer into the
300 calling chain for subsequent calls to bfd_find_inliner_info to
301 use. */
302 struct funcinfo *inliner_chain;
304 /* Section VMAs at the time the stash was built. */
305 bfd_vma *sec_vma;
306 /* Number of sections in the SEC_VMA table. */
307 unsigned int sec_vma_count;
309 /* Number of sections whose VMA we must adjust. */
310 int adjusted_section_count;
312 /* Array of sections with adjusted VMA. */
313 struct adjusted_section *adjusted_sections;
315 /* Number of times find_line is called. This is used in
316 the heuristic for enabling the info hash tables. */
317 int info_hash_count;
319 #define STASH_INFO_HASH_TRIGGER 100
321 /* Hash table mapping symbol names to function infos. */
322 struct info_hash_table *funcinfo_hash_table;
324 /* Hash table mapping symbol names to variable infos. */
325 struct info_hash_table *varinfo_hash_table;
327 /* Head of comp_unit list in the last hash table update. */
328 struct comp_unit *hash_units_head;
330 /* Status of info hash. */
331 int info_hash_status;
332 #define STASH_INFO_HASH_OFF 0
333 #define STASH_INFO_HASH_ON 1
334 #define STASH_INFO_HASH_DISABLED 2
336 /* True if we opened bfd_ptr. */
337 bool close_on_cleanup;
340 struct arange
342 struct arange *next;
343 bfd_vma low;
344 bfd_vma high;
347 /* A minimal decoding of DWARF2 compilation units. We only decode
348 what's needed to get to the line number information. */
350 struct comp_unit
352 /* Chain the previously read compilation units. */
353 struct comp_unit *next_unit;
355 /* Chain the previously read compilation units that have no ranges yet.
356 We scan these separately when we have a trie over the ranges.
357 Unused if arange.high != 0. */
358 struct comp_unit *next_unit_without_ranges;
360 /* Likewise, chain the compilation unit read after this one.
361 The comp units are stored in reversed reading order. */
362 struct comp_unit *prev_unit;
364 /* Keep the bfd convenient (for memory allocation). */
365 bfd *abfd;
367 /* The lowest and highest addresses contained in this compilation
368 unit as specified in the compilation unit header. */
369 struct arange arange;
371 /* The DW_AT_name attribute (for error messages). */
372 char *name;
374 /* The abbrev hash table. */
375 struct abbrev_info **abbrevs;
377 /* DW_AT_language. */
378 int lang;
380 /* Note that an error was found by comp_unit_find_nearest_line. */
381 int error;
383 /* The DW_AT_comp_dir attribute. */
384 char *comp_dir;
386 /* TRUE if there is a line number table associated with this comp. unit. */
387 int stmtlist;
389 /* Pointer to the current comp_unit so that we can find a given entry
390 by its reference. */
391 bfd_byte *info_ptr_unit;
393 /* The offset into .debug_line of the line number table. */
394 unsigned long line_offset;
396 /* Pointer to the first child die for the comp unit. */
397 bfd_byte *first_child_die_ptr;
399 /* The end of the comp unit. */
400 bfd_byte *end_ptr;
402 /* The decoded line number, NULL if not yet decoded. */
403 struct line_info_table *line_table;
405 /* A list of the functions found in this comp. unit. */
406 struct funcinfo *function_table;
408 /* A table of function information references searchable by address. */
409 struct lookup_funcinfo *lookup_funcinfo_table;
411 /* Number of functions in the function_table and sorted_function_table. */
412 bfd_size_type number_of_functions;
414 /* A list of the variables found in this comp. unit. */
415 struct varinfo *variable_table;
417 /* Pointers to dwarf2_debug structures. */
418 struct dwarf2_debug *stash;
419 struct dwarf2_debug_file *file;
421 /* DWARF format version for this unit - from unit header. */
422 int version;
424 /* Address size for this unit - from unit header. */
425 unsigned char addr_size;
427 /* Offset size for this unit - from unit header. */
428 unsigned char offset_size;
430 /* Base address for this unit - from DW_AT_low_pc attribute of
431 DW_TAG_compile_unit DIE */
432 bfd_vma base_address;
434 /* TRUE if symbols are cached in hash table for faster lookup by name. */
435 bool cached;
437 /* Used when iterating over trie leaves to know which units we have
438 already seen in this iteration. */
439 bool mark;
441 /* Base address of debug_addr section. */
442 size_t dwarf_addr_offset;
444 /* Base address of string offset table. */
445 size_t dwarf_str_offset;
448 /* This data structure holds the information of an abbrev. */
449 struct abbrev_info
451 unsigned int number; /* Number identifying abbrev. */
452 enum dwarf_tag tag; /* DWARF tag. */
453 bool has_children; /* TRUE if the abbrev has children. */
454 unsigned int num_attrs; /* Number of attributes. */
455 struct attr_abbrev * attrs; /* An array of attribute descriptions. */
456 struct abbrev_info * next; /* Next in chain. */
459 struct attr_abbrev
461 enum dwarf_attribute name;
462 enum dwarf_form form;
463 bfd_vma implicit_const;
466 /* Map of uncompressed DWARF debug section name to compressed one. It
467 is terminated by NULL uncompressed_name. */
469 const struct dwarf_debug_section dwarf_debug_sections[] =
471 { ".debug_abbrev", ".zdebug_abbrev" },
472 { ".debug_aranges", ".zdebug_aranges" },
473 { ".debug_frame", ".zdebug_frame" },
474 { ".debug_info", ".zdebug_info" },
475 { ".debug_info", ".zdebug_info" },
476 { ".debug_line", ".zdebug_line" },
477 { ".debug_loc", ".zdebug_loc" },
478 { ".debug_macinfo", ".zdebug_macinfo" },
479 { ".debug_macro", ".zdebug_macro" },
480 { ".debug_pubnames", ".zdebug_pubnames" },
481 { ".debug_pubtypes", ".zdebug_pubtypes" },
482 { ".debug_ranges", ".zdebug_ranges" },
483 { ".debug_rnglists", ".zdebug_rnglist" },
484 { ".debug_static_func", ".zdebug_static_func" },
485 { ".debug_static_vars", ".zdebug_static_vars" },
486 { ".debug_str", ".zdebug_str", },
487 { ".debug_str", ".zdebug_str", },
488 { ".debug_str_offsets", ".zdebug_str_offsets", },
489 { ".debug_addr", ".zdebug_addr", },
490 { ".debug_line_str", ".zdebug_line_str", },
491 { ".debug_types", ".zdebug_types" },
492 /* GNU DWARF 1 extensions */
493 { ".debug_sfnames", ".zdebug_sfnames" },
494 { ".debug_srcinfo", ".zebug_srcinfo" },
495 /* SGI/MIPS DWARF 2 extensions */
496 { ".debug_funcnames", ".zdebug_funcnames" },
497 { ".debug_typenames", ".zdebug_typenames" },
498 { ".debug_varnames", ".zdebug_varnames" },
499 { ".debug_weaknames", ".zdebug_weaknames" },
500 { NULL, NULL },
503 /* NB/ Numbers in this enum must match up with indices
504 into the dwarf_debug_sections[] array above. */
505 enum dwarf_debug_section_enum
507 debug_abbrev = 0,
508 debug_aranges,
509 debug_frame,
510 debug_info,
511 debug_info_alt,
512 debug_line,
513 debug_loc,
514 debug_macinfo,
515 debug_macro,
516 debug_pubnames,
517 debug_pubtypes,
518 debug_ranges,
519 debug_rnglists,
520 debug_static_func,
521 debug_static_vars,
522 debug_str,
523 debug_str_alt,
524 debug_str_offsets,
525 debug_addr,
526 debug_line_str,
527 debug_types,
528 debug_sfnames,
529 debug_srcinfo,
530 debug_funcnames,
531 debug_typenames,
532 debug_varnames,
533 debug_weaknames,
534 debug_max
537 /* A static assertion. */
538 extern int dwarf_debug_section_assert[ARRAY_SIZE (dwarf_debug_sections)
539 == debug_max + 1 ? 1 : -1];
541 #ifndef ABBREV_HASH_SIZE
542 #define ABBREV_HASH_SIZE 121
543 #endif
544 #ifndef ATTR_ALLOC_CHUNK
545 #define ATTR_ALLOC_CHUNK 4
546 #endif
548 /* Variable and function hash tables. This is used to speed up look-up
549 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
550 In order to share code between variable and function infos, we use
551 a list of untyped pointer for all variable/function info associated with
552 a symbol. We waste a bit of memory for list with one node but that
553 simplifies the code. */
555 struct info_list_node
557 struct info_list_node *next;
558 void *info;
561 /* Info hash entry. */
562 struct info_hash_entry
564 struct bfd_hash_entry root;
565 struct info_list_node *head;
568 struct info_hash_table
570 struct bfd_hash_table base;
573 /* Function to create a new entry in info hash table. */
575 static struct bfd_hash_entry *
576 info_hash_table_newfunc (struct bfd_hash_entry *entry,
577 struct bfd_hash_table *table,
578 const char *string)
580 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
582 /* Allocate the structure if it has not already been allocated by a
583 derived class. */
584 if (ret == NULL)
586 ret = (struct info_hash_entry *) bfd_hash_allocate (table,
587 sizeof (* ret));
588 if (ret == NULL)
589 return NULL;
592 /* Call the allocation method of the base class. */
593 ret = ((struct info_hash_entry *)
594 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
596 /* Initialize the local fields here. */
597 if (ret)
598 ret->head = NULL;
600 return (struct bfd_hash_entry *) ret;
603 /* Function to create a new info hash table. It returns a pointer to the
604 newly created table or NULL if there is any error. We need abfd
605 solely for memory allocation. */
607 static struct info_hash_table *
608 create_info_hash_table (bfd *abfd)
610 struct info_hash_table *hash_table;
612 hash_table = ((struct info_hash_table *)
613 bfd_alloc (abfd, sizeof (struct info_hash_table)));
614 if (!hash_table)
615 return hash_table;
617 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
618 sizeof (struct info_hash_entry)))
620 bfd_release (abfd, hash_table);
621 return NULL;
624 return hash_table;
627 /* Insert an info entry into an info hash table. We do not check of
628 duplicate entries. Also, the caller need to guarantee that the
629 right type of info in inserted as info is passed as a void* pointer.
630 This function returns true if there is no error. */
632 static bool
633 insert_info_hash_table (struct info_hash_table *hash_table,
634 const char *key,
635 void *info,
636 bool copy_p)
638 struct info_hash_entry *entry;
639 struct info_list_node *node;
641 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
642 key, true, copy_p);
643 if (!entry)
644 return false;
646 node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
647 sizeof (*node));
648 if (!node)
649 return false;
651 node->info = info;
652 node->next = entry->head;
653 entry->head = node;
655 return true;
658 /* Look up an info entry list from an info hash table. Return NULL
659 if there is none. */
661 static struct info_list_node *
662 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
664 struct info_hash_entry *entry;
666 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
667 false, false);
668 return entry ? entry->head : NULL;
671 /* Read a section into its appropriate place in the dwarf2_debug
672 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
673 not NULL, use bfd_simple_get_relocated_section_contents to read the
674 section contents, otherwise use bfd_get_section_contents. Fail if
675 the located section does not contain at least OFFSET bytes. */
677 static bool
678 read_section (bfd *abfd,
679 const struct dwarf_debug_section *sec,
680 asymbol **syms,
681 uint64_t offset,
682 bfd_byte **section_buffer,
683 bfd_size_type *section_size)
685 const char *section_name = sec->uncompressed_name;
686 bfd_byte *contents = *section_buffer;
688 /* The section may have already been read. */
689 if (contents == NULL)
691 bfd_size_type amt;
692 asection *msec;
694 msec = bfd_get_section_by_name (abfd, section_name);
695 if (msec == NULL)
697 section_name = sec->compressed_name;
698 msec = bfd_get_section_by_name (abfd, section_name);
700 if (msec == NULL)
702 _bfd_error_handler (_("DWARF error: can't find %s section."),
703 sec->uncompressed_name);
704 bfd_set_error (bfd_error_bad_value);
705 return false;
708 if (_bfd_section_size_insane (abfd, msec))
710 /* PR 26946 */
711 _bfd_error_handler (_("DWARF error: section %s is too big"),
712 section_name);
713 return false;
715 amt = bfd_get_section_limit_octets (abfd, msec);
716 *section_size = amt;
717 /* Paranoia - alloc one extra so that we can make sure a string
718 section is NUL terminated. */
719 amt += 1;
720 if (amt == 0)
722 /* Paranoia - this should never happen. */
723 bfd_set_error (bfd_error_no_memory);
724 return false;
726 contents = (bfd_byte *) bfd_malloc (amt);
727 if (contents == NULL)
728 return false;
729 if (syms
730 ? !bfd_simple_get_relocated_section_contents (abfd, msec, contents,
731 syms)
732 : !bfd_get_section_contents (abfd, msec, contents, 0, *section_size))
734 free (contents);
735 return false;
737 contents[*section_size] = 0;
738 *section_buffer = contents;
741 /* It is possible to get a bad value for the offset into the section
742 that the client wants. Validate it here to avoid trouble later. */
743 if (offset != 0 && offset >= *section_size)
745 /* xgettext: c-format */
746 _bfd_error_handler (_("DWARF error: offset (%" PRIu64 ")"
747 " greater than or equal to %s size (%" PRIu64 ")"),
748 (uint64_t) offset, section_name,
749 (uint64_t) *section_size);
750 bfd_set_error (bfd_error_bad_value);
751 return false;
754 return true;
757 /* Read dwarf information from a buffer. */
759 static inline uint64_t
760 read_n_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end, int n)
762 bfd_byte *buf = *ptr;
763 if (end - buf < n)
765 *ptr = end;
766 return 0;
768 *ptr = buf + n;
769 return bfd_get (n * 8, abfd, buf);
772 static unsigned int
773 read_1_byte (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
775 return read_n_bytes (abfd, ptr, end, 1);
778 static int
779 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte **ptr, bfd_byte *end)
781 bfd_byte *buf = *ptr;
782 if (end - buf < 1)
784 *ptr = end;
785 return 0;
787 *ptr = buf + 1;
788 return bfd_get_signed_8 (abfd, buf);
791 static unsigned int
792 read_2_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
794 return read_n_bytes (abfd, ptr, end, 2);
797 static unsigned int
798 read_3_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
800 unsigned int val = read_1_byte (abfd, ptr, end);
801 val <<= 8;
802 val |= read_1_byte (abfd, ptr, end);
803 val <<= 8;
804 val |= read_1_byte (abfd, ptr, end);
805 if (bfd_little_endian (abfd))
806 val = (((val >> 16) & 0xff)
807 | (val & 0xff00)
808 | ((val & 0xff) << 16));
809 return val;
812 static unsigned int
813 read_4_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
815 return read_n_bytes (abfd, ptr, end, 4);
818 static uint64_t
819 read_8_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
821 return read_n_bytes (abfd, ptr, end, 8);
824 static struct dwarf_block *
825 read_blk (bfd *abfd, bfd_byte **ptr, bfd_byte *end, size_t size)
827 bfd_byte *buf = *ptr;
828 struct dwarf_block *block;
830 block = (struct dwarf_block *) bfd_alloc (abfd, sizeof (*block));
831 if (block == NULL)
832 return NULL;
834 if (size > (size_t) (end - buf))
836 *ptr = end;
837 block->data = NULL;
838 block->size = 0;
840 else
842 *ptr = buf + size;
843 block->data = buf;
844 block->size = size;
846 return block;
849 /* Scans a NUL terminated string starting at *PTR, returning a pointer to it.
850 Bytes at or beyond BUF_END will not be read. Returns NULL if the
851 terminator is not found or if the string is empty. *PTR is
852 incremented over the bytes scanned, including the terminator. */
854 static char *
855 read_string (bfd_byte **ptr,
856 bfd_byte *buf_end)
858 bfd_byte *buf = *ptr;
859 bfd_byte *str = buf;
861 while (buf < buf_end)
862 if (*buf++ == 0)
864 if (str == buf - 1)
865 break;
866 *ptr = buf;
867 return (char *) str;
870 *ptr = buf;
871 return NULL;
874 /* Reads an offset from *PTR and then locates the string at this offset
875 inside the debug string section. Returns a pointer to the string.
876 Increments *PTR by the number of bytes read for the offset. This
877 value is set even if the function fails. Bytes at or beyond
878 BUF_END will not be read. Returns NULL if there was a problem, or
879 if the string is empty. Does not check for NUL termination of the
880 string. */
882 static char *
883 read_indirect_string (struct comp_unit *unit,
884 bfd_byte **ptr,
885 bfd_byte *buf_end)
887 uint64_t offset;
888 struct dwarf2_debug *stash = unit->stash;
889 struct dwarf2_debug_file *file = unit->file;
890 char *str;
892 if (unit->offset_size > (size_t) (buf_end - *ptr))
894 *ptr = buf_end;
895 return NULL;
898 if (unit->offset_size == 4)
899 offset = read_4_bytes (unit->abfd, ptr, buf_end);
900 else
901 offset = read_8_bytes (unit->abfd, ptr, buf_end);
903 if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
904 file->syms, offset,
905 &file->dwarf_str_buffer, &file->dwarf_str_size))
906 return NULL;
908 str = (char *) file->dwarf_str_buffer + offset;
909 if (*str == '\0')
910 return NULL;
911 return str;
914 /* Like read_indirect_string but from .debug_line_str section. */
916 static char *
917 read_indirect_line_string (struct comp_unit *unit,
918 bfd_byte **ptr,
919 bfd_byte *buf_end)
921 uint64_t offset;
922 struct dwarf2_debug *stash = unit->stash;
923 struct dwarf2_debug_file *file = unit->file;
924 char *str;
926 if (unit->offset_size > (size_t) (buf_end - *ptr))
928 *ptr = buf_end;
929 return NULL;
932 if (unit->offset_size == 4)
933 offset = read_4_bytes (unit->abfd, ptr, buf_end);
934 else
935 offset = read_8_bytes (unit->abfd, ptr, buf_end);
937 if (! read_section (unit->abfd, &stash->debug_sections[debug_line_str],
938 file->syms, offset,
939 &file->dwarf_line_str_buffer,
940 &file->dwarf_line_str_size))
941 return NULL;
943 str = (char *) file->dwarf_line_str_buffer + offset;
944 if (*str == '\0')
945 return NULL;
946 return str;
949 /* Like read_indirect_string but uses a .debug_str located in
950 an alternate file pointed to by the .gnu_debugaltlink section.
951 Used to impement DW_FORM_GNU_strp_alt. */
953 static char *
954 read_alt_indirect_string (struct comp_unit *unit,
955 bfd_byte **ptr,
956 bfd_byte *buf_end)
958 uint64_t offset;
959 struct dwarf2_debug *stash = unit->stash;
960 char *str;
962 if (unit->offset_size > (size_t) (buf_end - *ptr))
964 *ptr = buf_end;
965 return NULL;
968 if (unit->offset_size == 4)
969 offset = read_4_bytes (unit->abfd, ptr, buf_end);
970 else
971 offset = read_8_bytes (unit->abfd, ptr, buf_end);
973 if (stash->alt.bfd_ptr == NULL)
975 bfd *debug_bfd;
976 char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
978 if (debug_filename == NULL)
979 return NULL;
981 debug_bfd = bfd_openr (debug_filename, NULL);
982 free (debug_filename);
983 if (debug_bfd == NULL)
984 /* FIXME: Should we report our failure to follow the debuglink ? */
985 return NULL;
987 if (!bfd_check_format (debug_bfd, bfd_object))
989 bfd_close (debug_bfd);
990 return NULL;
992 stash->alt.bfd_ptr = debug_bfd;
995 if (! read_section (unit->stash->alt.bfd_ptr,
996 stash->debug_sections + debug_str_alt,
997 stash->alt.syms, offset,
998 &stash->alt.dwarf_str_buffer,
999 &stash->alt.dwarf_str_size))
1000 return NULL;
1002 str = (char *) stash->alt.dwarf_str_buffer + offset;
1003 if (*str == '\0')
1004 return NULL;
1006 return str;
1009 /* Resolve an alternate reference from UNIT at OFFSET.
1010 Returns a pointer into the loaded alternate CU upon success
1011 or NULL upon failure. */
1013 static bfd_byte *
1014 read_alt_indirect_ref (struct comp_unit *unit, uint64_t offset)
1016 struct dwarf2_debug *stash = unit->stash;
1018 if (stash->alt.bfd_ptr == NULL)
1020 bfd *debug_bfd;
1021 char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
1023 if (debug_filename == NULL)
1024 return NULL;
1026 debug_bfd = bfd_openr (debug_filename, NULL);
1027 free (debug_filename);
1028 if (debug_bfd == NULL)
1029 /* FIXME: Should we report our failure to follow the debuglink ? */
1030 return NULL;
1032 if (!bfd_check_format (debug_bfd, bfd_object))
1034 bfd_close (debug_bfd);
1035 return NULL;
1037 stash->alt.bfd_ptr = debug_bfd;
1040 if (! read_section (unit->stash->alt.bfd_ptr,
1041 stash->debug_sections + debug_info_alt,
1042 stash->alt.syms, offset,
1043 &stash->alt.dwarf_info_buffer,
1044 &stash->alt.dwarf_info_size))
1045 return NULL;
1047 return stash->alt.dwarf_info_buffer + offset;
1050 static uint64_t
1051 read_address (struct comp_unit *unit, bfd_byte **ptr, bfd_byte *buf_end)
1053 bfd_byte *buf = *ptr;
1054 int signed_vma = 0;
1056 if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour)
1057 signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
1059 if (unit->addr_size > (size_t) (buf_end - buf))
1061 *ptr = buf_end;
1062 return 0;
1065 *ptr = buf + unit->addr_size;
1066 if (signed_vma)
1068 switch (unit->addr_size)
1070 case 8:
1071 return bfd_get_signed_64 (unit->abfd, buf);
1072 case 4:
1073 return bfd_get_signed_32 (unit->abfd, buf);
1074 case 2:
1075 return bfd_get_signed_16 (unit->abfd, buf);
1076 default:
1077 abort ();
1080 else
1082 switch (unit->addr_size)
1084 case 8:
1085 return bfd_get_64 (unit->abfd, buf);
1086 case 4:
1087 return bfd_get_32 (unit->abfd, buf);
1088 case 2:
1089 return bfd_get_16 (unit->abfd, buf);
1090 default:
1091 abort ();
1096 /* Lookup an abbrev_info structure in the abbrev hash table. */
1098 static struct abbrev_info *
1099 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
1101 unsigned int hash_number;
1102 struct abbrev_info *abbrev;
1104 hash_number = number % ABBREV_HASH_SIZE;
1105 abbrev = abbrevs[hash_number];
1107 while (abbrev)
1109 if (abbrev->number == number)
1110 return abbrev;
1111 else
1112 abbrev = abbrev->next;
1115 return NULL;
1118 /* We keep a hash table to map .debug_abbrev section offsets to the
1119 array of abbrevs, so that compilation units using the same set of
1120 abbrevs do not waste memory. */
1122 struct abbrev_offset_entry
1124 size_t offset;
1125 struct abbrev_info **abbrevs;
1128 static hashval_t
1129 hash_abbrev (const void *p)
1131 const struct abbrev_offset_entry *ent = p;
1132 return htab_hash_pointer ((void *) ent->offset);
1135 static int
1136 eq_abbrev (const void *pa, const void *pb)
1138 const struct abbrev_offset_entry *a = pa;
1139 const struct abbrev_offset_entry *b = pb;
1140 return a->offset == b->offset;
1143 static void
1144 del_abbrev (void *p)
1146 struct abbrev_offset_entry *ent = p;
1147 struct abbrev_info **abbrevs = ent->abbrevs;
1148 size_t i;
1150 for (i = 0; i < ABBREV_HASH_SIZE; i++)
1152 struct abbrev_info *abbrev = abbrevs[i];
1154 while (abbrev)
1156 free (abbrev->attrs);
1157 abbrev = abbrev->next;
1160 free (ent);
1163 /* In DWARF version 2, the description of the debugging information is
1164 stored in a separate .debug_abbrev section. Before we read any
1165 dies from a section we read in all abbreviations and install them
1166 in a hash table. */
1168 static struct abbrev_info**
1169 read_abbrevs (bfd *abfd, uint64_t offset, struct dwarf2_debug *stash,
1170 struct dwarf2_debug_file *file)
1172 struct abbrev_info **abbrevs;
1173 bfd_byte *abbrev_ptr;
1174 bfd_byte *abbrev_end;
1175 struct abbrev_info *cur_abbrev;
1176 unsigned int abbrev_number, abbrev_name;
1177 unsigned int abbrev_form, hash_number;
1178 size_t amt;
1179 void **slot;
1180 struct abbrev_offset_entry ent = { offset, NULL };
1182 if (ent.offset != offset)
1183 return NULL;
1185 slot = htab_find_slot (file->abbrev_offsets, &ent, INSERT);
1186 if (slot == NULL)
1187 return NULL;
1188 if (*slot != NULL)
1189 return ((struct abbrev_offset_entry *) (*slot))->abbrevs;
1191 if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
1192 file->syms, offset,
1193 &file->dwarf_abbrev_buffer,
1194 &file->dwarf_abbrev_size))
1195 return NULL;
1197 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
1198 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
1199 if (abbrevs == NULL)
1200 return NULL;
1202 abbrev_ptr = file->dwarf_abbrev_buffer + offset;
1203 abbrev_end = file->dwarf_abbrev_buffer + file->dwarf_abbrev_size;
1204 abbrev_number = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1205 false, abbrev_end);
1207 /* Loop until we reach an abbrev number of 0. */
1208 while (abbrev_number)
1210 amt = sizeof (struct abbrev_info);
1211 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
1212 if (cur_abbrev == NULL)
1213 goto fail;
1215 /* Read in abbrev header. */
1216 cur_abbrev->number = abbrev_number;
1217 cur_abbrev->tag = (enum dwarf_tag)
1218 _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1219 false, abbrev_end);
1220 cur_abbrev->has_children = read_1_byte (abfd, &abbrev_ptr, abbrev_end);
1222 /* Now read in declarations. */
1223 for (;;)
1225 /* Initialize it just to avoid a GCC false warning. */
1226 bfd_vma implicit_const = -1;
1228 abbrev_name = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1229 false, abbrev_end);
1230 abbrev_form = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1231 false, abbrev_end);
1232 if (abbrev_form == DW_FORM_implicit_const)
1233 implicit_const = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1234 true, abbrev_end);
1235 if (abbrev_name == 0)
1236 break;
1238 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
1240 struct attr_abbrev *tmp;
1242 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
1243 amt *= sizeof (struct attr_abbrev);
1244 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
1245 if (tmp == NULL)
1246 goto fail;
1247 cur_abbrev->attrs = tmp;
1250 cur_abbrev->attrs[cur_abbrev->num_attrs].name
1251 = (enum dwarf_attribute) abbrev_name;
1252 cur_abbrev->attrs[cur_abbrev->num_attrs].form
1253 = (enum dwarf_form) abbrev_form;
1254 cur_abbrev->attrs[cur_abbrev->num_attrs].implicit_const
1255 = implicit_const;
1256 ++cur_abbrev->num_attrs;
1259 hash_number = abbrev_number % ABBREV_HASH_SIZE;
1260 cur_abbrev->next = abbrevs[hash_number];
1261 abbrevs[hash_number] = cur_abbrev;
1263 /* Get next abbreviation.
1264 Under Irix6 the abbreviations for a compilation unit are not
1265 always properly terminated with an abbrev number of 0.
1266 Exit loop if we encounter an abbreviation which we have
1267 already read (which means we are about to read the abbreviations
1268 for the next compile unit) or if the end of the abbreviation
1269 table is reached. */
1270 if ((size_t) (abbrev_ptr - file->dwarf_abbrev_buffer)
1271 >= file->dwarf_abbrev_size)
1272 break;
1273 abbrev_number = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1274 false, abbrev_end);
1275 if (lookup_abbrev (abbrev_number, abbrevs) != NULL)
1276 break;
1279 *slot = bfd_malloc (sizeof ent);
1280 if (!*slot)
1281 goto fail;
1282 ent.abbrevs = abbrevs;
1283 memcpy (*slot, &ent, sizeof ent);
1284 return abbrevs;
1286 fail:
1287 if (abbrevs != NULL)
1289 size_t i;
1291 for (i = 0; i < ABBREV_HASH_SIZE; i++)
1293 struct abbrev_info *abbrev = abbrevs[i];
1295 while (abbrev)
1297 free (abbrev->attrs);
1298 abbrev = abbrev->next;
1301 free (abbrevs);
1303 return NULL;
1306 /* Returns true if the form is one which has a string value. */
1308 static bool
1309 is_str_form (const struct attribute *attr)
1311 switch (attr->form)
1313 case DW_FORM_string:
1314 case DW_FORM_strp:
1315 case DW_FORM_strx:
1316 case DW_FORM_strx1:
1317 case DW_FORM_strx2:
1318 case DW_FORM_strx3:
1319 case DW_FORM_strx4:
1320 case DW_FORM_line_strp:
1321 case DW_FORM_GNU_strp_alt:
1322 return true;
1324 default:
1325 return false;
1329 /* Returns true if the form is one which has an integer value. */
1331 static bool
1332 is_int_form (const struct attribute *attr)
1334 switch (attr->form)
1336 case DW_FORM_addr:
1337 case DW_FORM_data2:
1338 case DW_FORM_data4:
1339 case DW_FORM_data8:
1340 case DW_FORM_data1:
1341 case DW_FORM_flag:
1342 case DW_FORM_sdata:
1343 case DW_FORM_udata:
1344 case DW_FORM_ref_addr:
1345 case DW_FORM_ref1:
1346 case DW_FORM_ref2:
1347 case DW_FORM_ref4:
1348 case DW_FORM_ref8:
1349 case DW_FORM_ref_udata:
1350 case DW_FORM_sec_offset:
1351 case DW_FORM_flag_present:
1352 case DW_FORM_ref_sig8:
1353 case DW_FORM_addrx:
1354 case DW_FORM_implicit_const:
1355 case DW_FORM_addrx1:
1356 case DW_FORM_addrx2:
1357 case DW_FORM_addrx3:
1358 case DW_FORM_addrx4:
1359 case DW_FORM_GNU_ref_alt:
1360 return true;
1362 default:
1363 return false;
1367 /* Returns true if the form is strx[1-4]. */
1369 static inline bool
1370 is_strx_form (enum dwarf_form form)
1372 return (form == DW_FORM_strx
1373 || form == DW_FORM_strx1
1374 || form == DW_FORM_strx2
1375 || form == DW_FORM_strx3
1376 || form == DW_FORM_strx4);
1379 /* Return true if the form is addrx[1-4]. */
1381 static inline bool
1382 is_addrx_form (enum dwarf_form form)
1384 return (form == DW_FORM_addrx
1385 || form == DW_FORM_addrx1
1386 || form == DW_FORM_addrx2
1387 || form == DW_FORM_addrx3
1388 || form == DW_FORM_addrx4);
1391 /* Returns the address in .debug_addr section using DW_AT_addr_base.
1392 Used to implement DW_FORM_addrx*. */
1393 static uint64_t
1394 read_indexed_address (uint64_t idx, struct comp_unit *unit)
1396 struct dwarf2_debug *stash = unit->stash;
1397 struct dwarf2_debug_file *file = unit->file;
1398 bfd_byte *info_ptr;
1399 size_t offset;
1401 if (stash == NULL)
1402 return 0;
1404 if (!read_section (unit->abfd, &stash->debug_sections[debug_addr],
1405 file->syms, 0,
1406 &file->dwarf_addr_buffer, &file->dwarf_addr_size))
1407 return 0;
1409 if (_bfd_mul_overflow (idx, unit->addr_size, &offset))
1410 return 0;
1412 offset += unit->dwarf_addr_offset;
1413 if (offset < unit->dwarf_addr_offset
1414 || offset > file->dwarf_addr_size
1415 || file->dwarf_addr_size - offset < unit->addr_size)
1416 return 0;
1418 info_ptr = file->dwarf_addr_buffer + offset;
1420 if (unit->addr_size == 4)
1421 return bfd_get_32 (unit->abfd, info_ptr);
1422 else if (unit->addr_size == 8)
1423 return bfd_get_64 (unit->abfd, info_ptr);
1424 else
1425 return 0;
1428 /* Returns the string using DW_AT_str_offsets_base.
1429 Used to implement DW_FORM_strx*. */
1430 static const char *
1431 read_indexed_string (uint64_t idx, struct comp_unit *unit)
1433 struct dwarf2_debug *stash = unit->stash;
1434 struct dwarf2_debug_file *file = unit->file;
1435 bfd_byte *info_ptr;
1436 uint64_t str_offset;
1437 size_t offset;
1439 if (stash == NULL)
1440 return NULL;
1442 if (!read_section (unit->abfd, &stash->debug_sections[debug_str],
1443 file->syms, 0,
1444 &file->dwarf_str_buffer, &file->dwarf_str_size))
1445 return NULL;
1447 if (!read_section (unit->abfd, &stash->debug_sections[debug_str_offsets],
1448 file->syms, 0,
1449 &file->dwarf_str_offsets_buffer,
1450 &file->dwarf_str_offsets_size))
1451 return NULL;
1453 if (_bfd_mul_overflow (idx, unit->offset_size, &offset))
1454 return NULL;
1456 offset += unit->dwarf_str_offset;
1457 if (offset < unit->dwarf_str_offset
1458 || offset > file->dwarf_str_offsets_size
1459 || file->dwarf_str_offsets_size - offset < unit->offset_size)
1460 return NULL;
1462 info_ptr = file->dwarf_str_offsets_buffer + offset;
1464 if (unit->offset_size == 4)
1465 str_offset = bfd_get_32 (unit->abfd, info_ptr);
1466 else if (unit->offset_size == 8)
1467 str_offset = bfd_get_64 (unit->abfd, info_ptr);
1468 else
1469 return NULL;
1471 if (str_offset >= file->dwarf_str_size)
1472 return NULL;
1473 return (const char *) file->dwarf_str_buffer + str_offset;
1476 /* Read and fill in the value of attribute ATTR as described by FORM.
1477 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1478 Returns an updated INFO_PTR taking into account the amount of data read. */
1480 static bfd_byte *
1481 read_attribute_value (struct attribute * attr,
1482 unsigned form,
1483 bfd_vma implicit_const,
1484 struct comp_unit * unit,
1485 bfd_byte * info_ptr,
1486 bfd_byte * info_ptr_end)
1488 bfd *abfd = unit->abfd;
1489 size_t amt;
1491 if (info_ptr >= info_ptr_end && form != DW_FORM_flag_present)
1493 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1494 bfd_set_error (bfd_error_bad_value);
1495 return NULL;
1498 attr->form = (enum dwarf_form) form;
1500 switch (form)
1502 case DW_FORM_flag_present:
1503 attr->u.val = 1;
1504 break;
1505 case DW_FORM_ref_addr:
1506 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1507 DWARF3. */
1508 if (unit->version >= 3)
1510 if (unit->offset_size == 4)
1511 attr->u.val = read_4_bytes (unit->abfd, &info_ptr, info_ptr_end);
1512 else
1513 attr->u.val = read_8_bytes (unit->abfd, &info_ptr, info_ptr_end);
1514 break;
1516 /* FALLTHROUGH */
1517 case DW_FORM_addr:
1518 attr->u.val = read_address (unit, &info_ptr, info_ptr_end);
1519 break;
1520 case DW_FORM_GNU_ref_alt:
1521 case DW_FORM_sec_offset:
1522 if (unit->offset_size == 4)
1523 attr->u.val = read_4_bytes (unit->abfd, &info_ptr, info_ptr_end);
1524 else
1525 attr->u.val = read_8_bytes (unit->abfd, &info_ptr, info_ptr_end);
1526 break;
1527 case DW_FORM_block2:
1528 amt = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1529 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1530 if (attr->u.blk == NULL)
1531 return NULL;
1532 break;
1533 case DW_FORM_block4:
1534 amt = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1535 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1536 if (attr->u.blk == NULL)
1537 return NULL;
1538 break;
1539 case DW_FORM_ref1:
1540 case DW_FORM_flag:
1541 case DW_FORM_data1:
1542 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1543 break;
1544 case DW_FORM_addrx1:
1545 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1546 /* dwarf_addr_offset value 0 indicates the attribute DW_AT_addr_base
1547 is not yet read. */
1548 if (unit->dwarf_addr_offset != 0)
1549 attr->u.val = read_indexed_address (attr->u.val, unit);
1550 break;
1551 case DW_FORM_data2:
1552 case DW_FORM_ref2:
1553 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1554 break;
1555 case DW_FORM_addrx2:
1556 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1557 if (unit->dwarf_addr_offset != 0)
1558 attr->u.val = read_indexed_address (attr->u.val, unit);
1559 break;
1560 case DW_FORM_addrx3:
1561 attr->u.val = read_3_bytes (abfd, &info_ptr, info_ptr_end);
1562 if (unit->dwarf_addr_offset != 0)
1563 attr->u.val = read_indexed_address(attr->u.val, unit);
1564 break;
1565 case DW_FORM_ref4:
1566 case DW_FORM_data4:
1567 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1568 break;
1569 case DW_FORM_addrx4:
1570 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1571 if (unit->dwarf_addr_offset != 0)
1572 attr->u.val = read_indexed_address (attr->u.val, unit);
1573 break;
1574 case DW_FORM_data8:
1575 case DW_FORM_ref8:
1576 case DW_FORM_ref_sig8:
1577 attr->u.val = read_8_bytes (abfd, &info_ptr, info_ptr_end);
1578 break;
1579 case DW_FORM_string:
1580 attr->u.str = read_string (&info_ptr, info_ptr_end);
1581 break;
1582 case DW_FORM_strp:
1583 attr->u.str = read_indirect_string (unit, &info_ptr, info_ptr_end);
1584 break;
1585 case DW_FORM_line_strp:
1586 attr->u.str = read_indirect_line_string (unit, &info_ptr, info_ptr_end);
1587 break;
1588 case DW_FORM_GNU_strp_alt:
1589 attr->u.str = read_alt_indirect_string (unit, &info_ptr, info_ptr_end);
1590 break;
1591 case DW_FORM_strx1:
1592 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1593 /* dwarf_str_offset value 0 indicates the attribute DW_AT_str_offsets_base
1594 is not yet read. */
1595 if (unit->dwarf_str_offset != 0)
1596 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1597 else
1598 attr->u.str = NULL;
1599 break;
1600 case DW_FORM_strx2:
1601 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1602 if (unit->dwarf_str_offset != 0)
1603 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1604 else
1605 attr->u.str = NULL;
1606 break;
1607 case DW_FORM_strx3:
1608 attr->u.val = read_3_bytes (abfd, &info_ptr, info_ptr_end);
1609 if (unit->dwarf_str_offset != 0)
1610 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1611 else
1612 attr->u.str = NULL;
1613 break;
1614 case DW_FORM_strx4:
1615 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1616 if (unit->dwarf_str_offset != 0)
1617 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1618 else
1619 attr->u.str = NULL;
1620 break;
1621 case DW_FORM_strx:
1622 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1623 false, info_ptr_end);
1624 if (unit->dwarf_str_offset != 0)
1625 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1626 else
1627 attr->u.str = NULL;
1628 break;
1629 case DW_FORM_exprloc:
1630 case DW_FORM_block:
1631 amt = _bfd_safe_read_leb128 (abfd, &info_ptr,
1632 false, info_ptr_end);
1633 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1634 if (attr->u.blk == NULL)
1635 return NULL;
1636 break;
1637 case DW_FORM_block1:
1638 amt = read_1_byte (abfd, &info_ptr, info_ptr_end);
1639 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1640 if (attr->u.blk == NULL)
1641 return NULL;
1642 break;
1643 case DW_FORM_sdata:
1644 attr->u.sval = _bfd_safe_read_leb128 (abfd, &info_ptr,
1645 true, info_ptr_end);
1646 break;
1648 case DW_FORM_rnglistx:
1649 case DW_FORM_loclistx:
1650 /* FIXME: Add support for these forms! */
1651 /* Fall through. */
1652 case DW_FORM_ref_udata:
1653 case DW_FORM_udata:
1654 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1655 false, info_ptr_end);
1656 break;
1657 case DW_FORM_addrx:
1658 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1659 false, info_ptr_end);
1660 if (unit->dwarf_addr_offset != 0)
1661 attr->u.val = read_indexed_address (attr->u.val, unit);
1662 break;
1663 case DW_FORM_indirect:
1664 form = _bfd_safe_read_leb128 (abfd, &info_ptr,
1665 false, info_ptr_end);
1666 if (form == DW_FORM_implicit_const)
1667 implicit_const = _bfd_safe_read_leb128 (abfd, &info_ptr,
1668 true, info_ptr_end);
1669 info_ptr = read_attribute_value (attr, form, implicit_const, unit,
1670 info_ptr, info_ptr_end);
1671 break;
1672 case DW_FORM_implicit_const:
1673 attr->form = DW_FORM_sdata;
1674 attr->u.sval = implicit_const;
1675 break;
1676 case DW_FORM_data16:
1677 /* This is really a "constant", but there is no way to store that
1678 so pretend it is a 16 byte block instead. */
1679 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, 16);
1680 if (attr->u.blk == NULL)
1681 return NULL;
1682 break;
1684 default:
1685 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1686 form);
1687 bfd_set_error (bfd_error_bad_value);
1688 return NULL;
1690 return info_ptr;
1693 /* Read an attribute described by an abbreviated attribute. */
1695 static bfd_byte *
1696 read_attribute (struct attribute * attr,
1697 struct attr_abbrev * abbrev,
1698 struct comp_unit * unit,
1699 bfd_byte * info_ptr,
1700 bfd_byte * info_ptr_end)
1702 attr->name = abbrev->name;
1703 info_ptr = read_attribute_value (attr, abbrev->form, abbrev->implicit_const,
1704 unit, info_ptr, info_ptr_end);
1705 return info_ptr;
1708 /* Return mangling style given LANG. */
1710 static int
1711 mangle_style (int lang)
1713 switch (lang)
1715 case DW_LANG_Ada83:
1716 case DW_LANG_Ada95:
1717 return DMGL_GNAT;
1719 case DW_LANG_C_plus_plus:
1720 case DW_LANG_C_plus_plus_03:
1721 case DW_LANG_C_plus_plus_11:
1722 case DW_LANG_C_plus_plus_14:
1723 return DMGL_GNU_V3;
1725 case DW_LANG_Java:
1726 return DMGL_JAVA;
1728 case DW_LANG_D:
1729 return DMGL_DLANG;
1731 case DW_LANG_Rust:
1732 case DW_LANG_Rust_old:
1733 return DMGL_RUST;
1735 default:
1736 return DMGL_AUTO;
1738 case DW_LANG_C89:
1739 case DW_LANG_C:
1740 case DW_LANG_Cobol74:
1741 case DW_LANG_Cobol85:
1742 case DW_LANG_Fortran77:
1743 case DW_LANG_Pascal83:
1744 case DW_LANG_PLI:
1745 case DW_LANG_C99:
1746 case DW_LANG_UPC:
1747 case DW_LANG_C11:
1748 case DW_LANG_Mips_Assembler:
1749 case DW_LANG_Upc:
1750 case DW_LANG_HP_Basic91:
1751 case DW_LANG_HP_IMacro:
1752 case DW_LANG_HP_Assembler:
1753 return 0;
1757 /* Source line information table routines. */
1759 #define FILE_ALLOC_CHUNK 5
1760 #define DIR_ALLOC_CHUNK 5
1762 struct line_info
1764 struct line_info * prev_line;
1765 bfd_vma address;
1766 char * filename;
1767 unsigned int line;
1768 unsigned int column;
1769 unsigned int discriminator;
1770 unsigned char op_index;
1771 unsigned char end_sequence; /* End of (sequential) code sequence. */
1774 struct fileinfo
1776 char * name;
1777 unsigned int dir;
1778 unsigned int time;
1779 unsigned int size;
1782 struct line_sequence
1784 bfd_vma low_pc;
1785 struct line_sequence* prev_sequence;
1786 struct line_info* last_line; /* Largest VMA. */
1787 struct line_info** line_info_lookup;
1788 bfd_size_type num_lines;
1791 struct line_info_table
1793 bfd * abfd;
1794 unsigned int num_files;
1795 unsigned int num_dirs;
1796 unsigned int num_sequences;
1797 bool use_dir_and_file_0;
1798 char * comp_dir;
1799 char ** dirs;
1800 struct fileinfo* files;
1801 struct line_sequence* sequences;
1802 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
1805 /* Remember some information about each function. If the function is
1806 inlined (DW_TAG_inlined_subroutine) it may have two additional
1807 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1808 source code location where this function was inlined. */
1810 struct funcinfo
1812 /* Pointer to previous function in list of all functions. */
1813 struct funcinfo *prev_func;
1814 /* Pointer to function one scope higher. */
1815 struct funcinfo *caller_func;
1816 /* Source location file name where caller_func inlines this func. */
1817 char *caller_file;
1818 /* Source location file name. */
1819 char *file;
1820 /* Source location line number where caller_func inlines this func. */
1821 int caller_line;
1822 /* Source location line number. */
1823 int line;
1824 int tag;
1825 bool is_linkage;
1826 const char *name;
1827 struct arange arange;
1828 /* The offset of the funcinfo from the start of the unit. */
1829 uint64_t unit_offset;
1832 struct lookup_funcinfo
1834 /* Function information corresponding to this lookup table entry. */
1835 struct funcinfo *funcinfo;
1837 /* The lowest address for this specific function. */
1838 bfd_vma low_addr;
1840 /* The highest address of this function before the lookup table is sorted.
1841 The highest address of all prior functions after the lookup table is
1842 sorted, which is used for binary search. */
1843 bfd_vma high_addr;
1844 /* Index of this function, used to ensure qsort is stable. */
1845 unsigned int idx;
1848 struct varinfo
1850 /* Pointer to previous variable in list of all variables. */
1851 struct varinfo *prev_var;
1852 /* The offset of the varinfo from the start of the unit. */
1853 uint64_t unit_offset;
1854 /* Source location file name. */
1855 char *file;
1856 /* Source location line number. */
1857 int line;
1858 /* The type of this variable. */
1859 int tag;
1860 /* The name of the variable, if it has one. */
1861 const char *name;
1862 /* The address of the variable. */
1863 bfd_vma addr;
1864 /* Is this a stack variable? */
1865 bool stack;
1868 /* Return TRUE if NEW_LINE should sort after LINE. */
1870 static inline bool
1871 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1873 return (new_line->address > line->address
1874 || (new_line->address == line->address
1875 && new_line->op_index > line->op_index));
1879 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1880 that the list is sorted. Note that the line_info list is sorted from
1881 highest to lowest VMA (with possible duplicates); that is,
1882 line_info->prev_line always accesses an equal or smaller VMA. */
1884 static bool
1885 add_line_info (struct line_info_table *table,
1886 bfd_vma address,
1887 unsigned char op_index,
1888 char *filename,
1889 unsigned int line,
1890 unsigned int column,
1891 unsigned int discriminator,
1892 int end_sequence)
1894 size_t amt = sizeof (struct line_info);
1895 struct line_sequence* seq = table->sequences;
1896 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
1898 if (info == NULL)
1899 return false;
1901 /* Set member data of 'info'. */
1902 info->prev_line = NULL;
1903 info->address = address;
1904 info->op_index = op_index;
1905 info->line = line;
1906 info->column = column;
1907 info->discriminator = discriminator;
1908 info->end_sequence = end_sequence;
1910 if (filename && filename[0])
1912 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1913 if (info->filename == NULL)
1914 return false;
1915 strcpy (info->filename, filename);
1917 else
1918 info->filename = NULL;
1920 /* Find the correct location for 'info'. Normally we will receive
1921 new line_info data 1) in order and 2) with increasing VMAs.
1922 However some compilers break the rules (cf. decode_line_info) and
1923 so we include some heuristics for quickly finding the correct
1924 location for 'info'. In particular, these heuristics optimize for
1925 the common case in which the VMA sequence that we receive is a
1926 list of locally sorted VMAs such as
1927 p...z a...j (where a < j < p < z)
1929 Note: table->lcl_head is used to head an *actual* or *possible*
1930 sub-sequence within the list (such as a...j) that is not directly
1931 headed by table->last_line
1933 Note: we may receive duplicate entries from 'decode_line_info'. */
1935 if (seq
1936 && seq->last_line->address == address
1937 && seq->last_line->op_index == op_index
1938 && seq->last_line->end_sequence == end_sequence)
1940 /* We only keep the last entry with the same address and end
1941 sequence. See PR ld/4986. */
1942 if (table->lcl_head == seq->last_line)
1943 table->lcl_head = info;
1944 info->prev_line = seq->last_line->prev_line;
1945 seq->last_line = info;
1947 else if (!seq || seq->last_line->end_sequence)
1949 /* Start a new line sequence. */
1950 amt = sizeof (struct line_sequence);
1951 seq = (struct line_sequence *) bfd_malloc (amt);
1952 if (seq == NULL)
1953 return false;
1954 seq->low_pc = address;
1955 seq->prev_sequence = table->sequences;
1956 seq->last_line = info;
1957 table->lcl_head = info;
1958 table->sequences = seq;
1959 table->num_sequences++;
1961 else if (info->end_sequence
1962 || new_line_sorts_after (info, seq->last_line))
1964 /* Normal case: add 'info' to the beginning of the current sequence. */
1965 info->prev_line = seq->last_line;
1966 seq->last_line = info;
1968 /* lcl_head: initialize to head a *possible* sequence at the end. */
1969 if (!table->lcl_head)
1970 table->lcl_head = info;
1972 else if (!new_line_sorts_after (info, table->lcl_head)
1973 && (!table->lcl_head->prev_line
1974 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1976 /* Abnormal but easy: lcl_head is the head of 'info'. */
1977 info->prev_line = table->lcl_head->prev_line;
1978 table->lcl_head->prev_line = info;
1980 else
1982 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1983 are valid heads for 'info'. Reset 'lcl_head'. */
1984 struct line_info* li2 = seq->last_line; /* Always non-NULL. */
1985 struct line_info* li1 = li2->prev_line;
1987 while (li1)
1989 if (!new_line_sorts_after (info, li2)
1990 && new_line_sorts_after (info, li1))
1991 break;
1993 li2 = li1; /* always non-NULL */
1994 li1 = li1->prev_line;
1996 table->lcl_head = li2;
1997 info->prev_line = table->lcl_head->prev_line;
1998 table->lcl_head->prev_line = info;
1999 if (address < seq->low_pc)
2000 seq->low_pc = address;
2002 return true;
2005 /* Extract a fully qualified filename from a line info table.
2006 The returned string has been malloc'ed and it is the caller's
2007 responsibility to free it. */
2009 static char *
2010 concat_filename (struct line_info_table *table, unsigned int file)
2012 char *filename;
2014 /* Pre DWARF-5 entry 0 in the directory and filename tables was not used.
2015 So in order to save space in the tables used here the info for, eg
2016 directory 1 is stored in slot 0 of the directory table, directory 2
2017 in slot 1 and so on.
2019 Starting with DWARF-5 the 0'th entry is used so there is a one to one
2020 mapping between DWARF slots and internal table entries. */
2021 if (! table->use_dir_and_file_0)
2023 /* Pre DWARF-5, FILE == 0 means unknown. */
2024 if (file == 0)
2025 return strdup ("<unknown>");
2026 -- file;
2029 if (table == NULL || file >= table->num_files)
2031 _bfd_error_handler
2032 (_("DWARF error: mangled line number section (bad file number)"));
2033 return strdup ("<unknown>");
2036 filename = table->files[file].name;
2038 if (filename == NULL)
2039 return strdup ("<unknown>");
2041 if (!IS_ABSOLUTE_PATH (filename))
2043 char *dir_name = NULL;
2044 char *subdir_name = NULL;
2045 char *name;
2046 size_t len;
2047 unsigned int dir = table->files[file].dir;
2049 if (!table->use_dir_and_file_0)
2050 --dir;
2051 /* Wrapping from 0 to -1u above gives the intended result with
2052 the test below of leaving subdir_name NULL for pre-DWARF5 dir
2053 of 0. */
2054 /* PR 17512: file: 0317e960, file: 7f3d2e4b. */
2055 if (dir < table->num_dirs)
2056 subdir_name = table->dirs[dir];
2058 if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
2059 dir_name = table->comp_dir;
2061 if (!dir_name)
2063 dir_name = subdir_name;
2064 subdir_name = NULL;
2067 if (!dir_name)
2068 return strdup (filename);
2070 len = strlen (dir_name) + strlen (filename) + 2;
2072 if (subdir_name)
2074 len += strlen (subdir_name) + 1;
2075 name = (char *) bfd_malloc (len);
2076 if (name)
2077 sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
2079 else
2081 name = (char *) bfd_malloc (len);
2082 if (name)
2083 sprintf (name, "%s/%s", dir_name, filename);
2086 return name;
2089 return strdup (filename);
2092 /* Number of bits in a bfd_vma. */
2093 #define VMA_BITS (8 * sizeof (bfd_vma))
2095 /* Check whether [low1, high1) can be combined with [low2, high2),
2096 i.e., they touch or overlap. */
2098 static bool
2099 ranges_overlap (bfd_vma low1,
2100 bfd_vma high1,
2101 bfd_vma low2,
2102 bfd_vma high2)
2104 if (low1 == low2 || high1 == high2)
2105 return true;
2107 /* Sort so that low1 is below low2. */
2108 if (low1 > low2)
2110 bfd_vma tmp;
2112 tmp = low1;
2113 low1 = low2;
2114 low2 = tmp;
2116 tmp = high1;
2117 high1 = high2;
2118 high2 = tmp;
2121 /* We touch iff low2 == high1.
2122 We overlap iff low2 is within [low1, high1). */
2123 return low2 <= high1;
2126 /* Insert an address range in the trie mapping addresses to compilation units.
2127 Will return the new trie node (usually the same as is being sent in, but
2128 in case of a leaf-to-interior conversion, or expansion of a leaf, it may be
2129 different), or NULL on failure. */
2131 static struct trie_node *
2132 insert_arange_in_trie (bfd *abfd,
2133 struct trie_node *trie,
2134 bfd_vma trie_pc,
2135 unsigned int trie_pc_bits,
2136 struct comp_unit *unit,
2137 bfd_vma low_pc,
2138 bfd_vma high_pc)
2140 bfd_vma clamped_low_pc, clamped_high_pc;
2141 int ch, from_ch, to_ch;
2142 bool is_full_leaf = false;
2144 /* See if we can extend any of the existing ranges. This merging
2145 isn't perfect (if merging opens up the possibility of merging two existing
2146 ranges, we won't find them), but it takes the majority of the cases. */
2147 if (trie->num_room_in_leaf > 0)
2149 struct trie_leaf *leaf = (struct trie_leaf *) trie;
2150 unsigned int i;
2152 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2154 if (leaf->ranges[i].unit == unit
2155 && ranges_overlap (low_pc, high_pc,
2156 leaf->ranges[i].low_pc,
2157 leaf->ranges[i].high_pc))
2159 if (low_pc < leaf->ranges[i].low_pc)
2160 leaf->ranges[i].low_pc = low_pc;
2161 if (high_pc > leaf->ranges[i].high_pc)
2162 leaf->ranges[i].high_pc = high_pc;
2163 return trie;
2167 is_full_leaf = leaf->num_stored_in_leaf == trie->num_room_in_leaf;
2170 /* If we're a leaf with no more room and we're _not_ at the bottom,
2171 convert to an interior node. */
2172 if (is_full_leaf && trie_pc_bits < VMA_BITS)
2174 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
2175 unsigned int i;
2177 trie = bfd_zalloc (abfd, sizeof (struct trie_interior));
2178 if (!trie)
2179 return NULL;
2180 is_full_leaf = false;
2182 /* TODO: If we wanted to save a little more memory at the cost of
2183 complexity, we could have reused the old leaf node as one of the
2184 children of the new interior node, instead of throwing it away. */
2185 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2187 if (!insert_arange_in_trie (abfd, trie, trie_pc, trie_pc_bits,
2188 leaf->ranges[i].unit, leaf->ranges[i].low_pc,
2189 leaf->ranges[i].high_pc))
2190 return NULL;
2194 /* If we're a leaf with no more room and we _are_ at the bottom,
2195 we have no choice but to just make it larger. */
2196 if (is_full_leaf)
2198 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
2199 unsigned int new_room_in_leaf = trie->num_room_in_leaf * 2;
2200 struct trie_leaf *new_leaf;
2201 size_t amt = (sizeof (struct trie_leaf)
2202 + ((new_room_in_leaf - TRIE_LEAF_SIZE)
2203 * sizeof (leaf->ranges[0])));
2204 new_leaf = bfd_zalloc (abfd, amt);
2205 new_leaf->head.num_room_in_leaf = new_room_in_leaf;
2206 new_leaf->num_stored_in_leaf = leaf->num_stored_in_leaf;
2208 memcpy (new_leaf->ranges,
2209 leaf->ranges,
2210 leaf->num_stored_in_leaf * sizeof (leaf->ranges[0]));
2211 trie = &new_leaf->head;
2212 is_full_leaf = false;
2214 /* Now the insert below will go through. */
2217 /* If we're a leaf (now with room), we can just insert at the end. */
2218 if (trie->num_room_in_leaf > 0)
2220 struct trie_leaf *leaf = (struct trie_leaf *) trie;
2222 unsigned int i = leaf->num_stored_in_leaf++;
2223 leaf->ranges[i].unit = unit;
2224 leaf->ranges[i].low_pc = low_pc;
2225 leaf->ranges[i].high_pc = high_pc;
2226 return trie;
2229 /* Now we are definitely an interior node, so recurse into all
2230 the relevant buckets. */
2232 /* Clamp the range to the current trie bucket. */
2233 clamped_low_pc = low_pc;
2234 clamped_high_pc = high_pc;
2235 if (trie_pc_bits > 0)
2237 bfd_vma bucket_high_pc =
2238 trie_pc + ((bfd_vma) -1 >> trie_pc_bits); /* Inclusive. */
2239 if (clamped_low_pc < trie_pc)
2240 clamped_low_pc = trie_pc;
2241 if (clamped_high_pc > bucket_high_pc)
2242 clamped_high_pc = bucket_high_pc;
2245 /* Insert the ranges in all buckets that it spans. */
2246 from_ch = (clamped_low_pc >> (VMA_BITS - trie_pc_bits - 8)) & 0xff;
2247 to_ch = ((clamped_high_pc - 1) >> (VMA_BITS - trie_pc_bits - 8)) & 0xff;
2248 for (ch = from_ch; ch <= to_ch; ++ch)
2250 struct trie_interior *interior = (struct trie_interior *) trie;
2251 struct trie_node *child = interior->children[ch];
2253 if (child == NULL)
2255 child = alloc_trie_leaf (abfd);
2256 if (!child)
2257 return NULL;
2259 bfd_vma bucket = (bfd_vma) ch << (VMA_BITS - trie_pc_bits - 8);
2260 child = insert_arange_in_trie (abfd,
2261 child,
2262 trie_pc + bucket,
2263 trie_pc_bits + 8,
2264 unit,
2265 low_pc,
2266 high_pc);
2267 if (!child)
2268 return NULL;
2270 interior->children[ch] = child;
2273 return trie;
2276 static bool
2277 arange_add (struct comp_unit *unit, struct arange *first_arange,
2278 struct trie_node **trie_root, bfd_vma low_pc, bfd_vma high_pc)
2280 struct arange *arange;
2282 /* Ignore empty ranges. */
2283 if (low_pc == high_pc)
2284 return true;
2286 if (trie_root != NULL)
2288 *trie_root = insert_arange_in_trie (unit->file->bfd_ptr,
2289 *trie_root,
2292 unit,
2293 low_pc,
2294 high_pc);
2295 if (*trie_root == NULL)
2296 return false;
2299 /* If the first arange is empty, use it. */
2300 if (first_arange->high == 0)
2302 first_arange->low = low_pc;
2303 first_arange->high = high_pc;
2304 return true;
2307 /* Next see if we can cheaply extend an existing range. */
2308 arange = first_arange;
2311 if (low_pc == arange->high)
2313 arange->high = high_pc;
2314 return true;
2316 if (high_pc == arange->low)
2318 arange->low = low_pc;
2319 return true;
2321 arange = arange->next;
2323 while (arange);
2325 /* Need to allocate a new arange and insert it into the arange list.
2326 Order isn't significant, so just insert after the first arange. */
2327 arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
2328 if (arange == NULL)
2329 return false;
2330 arange->low = low_pc;
2331 arange->high = high_pc;
2332 arange->next = first_arange->next;
2333 first_arange->next = arange;
2334 return true;
2337 /* Compare function for line sequences. */
2339 static int
2340 compare_sequences (const void* a, const void* b)
2342 const struct line_sequence* seq1 = a;
2343 const struct line_sequence* seq2 = b;
2345 /* Sort by low_pc as the primary key. */
2346 if (seq1->low_pc < seq2->low_pc)
2347 return -1;
2348 if (seq1->low_pc > seq2->low_pc)
2349 return 1;
2351 /* If low_pc values are equal, sort in reverse order of
2352 high_pc, so that the largest region comes first. */
2353 if (seq1->last_line->address < seq2->last_line->address)
2354 return 1;
2355 if (seq1->last_line->address > seq2->last_line->address)
2356 return -1;
2358 if (seq1->last_line->op_index < seq2->last_line->op_index)
2359 return 1;
2360 if (seq1->last_line->op_index > seq2->last_line->op_index)
2361 return -1;
2363 /* num_lines is initially an index, to make the sort stable. */
2364 if (seq1->num_lines < seq2->num_lines)
2365 return -1;
2366 if (seq1->num_lines > seq2->num_lines)
2367 return 1;
2368 return 0;
2371 /* Construct the line information table for quick lookup. */
2373 static bool
2374 build_line_info_table (struct line_info_table * table,
2375 struct line_sequence * seq)
2377 size_t amt;
2378 struct line_info **line_info_lookup;
2379 struct line_info *each_line;
2380 unsigned int num_lines;
2381 unsigned int line_index;
2383 if (seq->line_info_lookup != NULL)
2384 return true;
2386 /* Count the number of line information entries. We could do this while
2387 scanning the debug information, but some entries may be added via
2388 lcl_head without having a sequence handy to increment the number of
2389 lines. */
2390 num_lines = 0;
2391 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
2392 num_lines++;
2394 seq->num_lines = num_lines;
2395 if (num_lines == 0)
2396 return true;
2398 /* Allocate space for the line information lookup table. */
2399 amt = sizeof (struct line_info*) * num_lines;
2400 line_info_lookup = (struct line_info**) bfd_alloc (table->abfd, amt);
2401 seq->line_info_lookup = line_info_lookup;
2402 if (line_info_lookup == NULL)
2403 return false;
2405 /* Create the line information lookup table. */
2406 line_index = num_lines;
2407 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
2408 line_info_lookup[--line_index] = each_line;
2410 BFD_ASSERT (line_index == 0);
2411 return true;
2414 /* Sort the line sequences for quick lookup. */
2416 static bool
2417 sort_line_sequences (struct line_info_table* table)
2419 size_t amt;
2420 struct line_sequence *sequences;
2421 struct line_sequence *seq;
2422 unsigned int n = 0;
2423 unsigned int num_sequences = table->num_sequences;
2424 bfd_vma last_high_pc;
2426 if (num_sequences == 0)
2427 return true;
2429 /* Allocate space for an array of sequences. */
2430 amt = sizeof (struct line_sequence) * num_sequences;
2431 sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
2432 if (sequences == NULL)
2433 return false;
2435 /* Copy the linked list into the array, freeing the original nodes. */
2436 seq = table->sequences;
2437 for (n = 0; n < num_sequences; n++)
2439 struct line_sequence* last_seq = seq;
2441 BFD_ASSERT (seq);
2442 sequences[n].low_pc = seq->low_pc;
2443 sequences[n].prev_sequence = NULL;
2444 sequences[n].last_line = seq->last_line;
2445 sequences[n].line_info_lookup = NULL;
2446 sequences[n].num_lines = n;
2447 seq = seq->prev_sequence;
2448 free (last_seq);
2450 BFD_ASSERT (seq == NULL);
2452 qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
2454 /* Make the list binary-searchable by trimming overlapping entries
2455 and removing nested entries. */
2456 num_sequences = 1;
2457 last_high_pc = sequences[0].last_line->address;
2458 for (n = 1; n < table->num_sequences; n++)
2460 if (sequences[n].low_pc < last_high_pc)
2462 if (sequences[n].last_line->address <= last_high_pc)
2463 /* Skip nested entries. */
2464 continue;
2466 /* Trim overlapping entries. */
2467 sequences[n].low_pc = last_high_pc;
2469 last_high_pc = sequences[n].last_line->address;
2470 if (n > num_sequences)
2472 /* Close up the gap. */
2473 sequences[num_sequences].low_pc = sequences[n].low_pc;
2474 sequences[num_sequences].last_line = sequences[n].last_line;
2476 num_sequences++;
2479 table->sequences = sequences;
2480 table->num_sequences = num_sequences;
2481 return true;
2484 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
2486 static bool
2487 line_info_add_include_dir (struct line_info_table *table, char *cur_dir)
2489 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
2491 char **tmp;
2492 size_t amt;
2494 amt = table->num_dirs + DIR_ALLOC_CHUNK;
2495 amt *= sizeof (char *);
2497 tmp = (char **) bfd_realloc (table->dirs, amt);
2498 if (tmp == NULL)
2499 return false;
2500 table->dirs = tmp;
2503 table->dirs[table->num_dirs++] = cur_dir;
2504 return true;
2507 static bool
2508 line_info_add_include_dir_stub (struct line_info_table *table, char *cur_dir,
2509 unsigned int dir ATTRIBUTE_UNUSED,
2510 unsigned int xtime ATTRIBUTE_UNUSED,
2511 unsigned int size ATTRIBUTE_UNUSED)
2513 return line_info_add_include_dir (table, cur_dir);
2516 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
2518 static bool
2519 line_info_add_file_name (struct line_info_table *table, char *cur_file,
2520 unsigned int dir, unsigned int xtime,
2521 unsigned int size)
2523 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
2525 struct fileinfo *tmp;
2526 size_t amt;
2528 amt = table->num_files + FILE_ALLOC_CHUNK;
2529 amt *= sizeof (struct fileinfo);
2531 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
2532 if (tmp == NULL)
2533 return false;
2534 table->files = tmp;
2537 table->files[table->num_files].name = cur_file;
2538 table->files[table->num_files].dir = dir;
2539 table->files[table->num_files].time = xtime;
2540 table->files[table->num_files].size = size;
2541 table->num_files++;
2542 return true;
2545 /* Read directory or file name entry format, starting with byte of
2546 format count entries, ULEB128 pairs of entry formats, ULEB128 of
2547 entries count and the entries themselves in the described entry
2548 format. */
2550 static bool
2551 read_formatted_entries (struct comp_unit *unit, bfd_byte **bufp,
2552 bfd_byte *buf_end, struct line_info_table *table,
2553 bool (*callback) (struct line_info_table *table,
2554 char *cur_file,
2555 unsigned int dir,
2556 unsigned int time,
2557 unsigned int size))
2559 bfd *abfd = unit->abfd;
2560 bfd_byte format_count, formati;
2561 bfd_vma data_count, datai;
2562 bfd_byte *buf = *bufp;
2563 bfd_byte *format_header_data;
2565 format_count = read_1_byte (abfd, &buf, buf_end);
2566 format_header_data = buf;
2567 for (formati = 0; formati < format_count; formati++)
2569 _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2570 _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2573 data_count = _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2574 if (format_count == 0 && data_count != 0)
2576 _bfd_error_handler (_("DWARF error: zero format count"));
2577 bfd_set_error (bfd_error_bad_value);
2578 return false;
2581 /* PR 22210. Paranoia check. Don't bother running the loop
2582 if we know that we are going to run out of buffer. */
2583 if (data_count > (bfd_vma) (buf_end - buf))
2585 _bfd_error_handler
2586 (_("DWARF error: data count (%" PRIx64 ") larger than buffer size"),
2587 (uint64_t) data_count);
2588 bfd_set_error (bfd_error_bad_value);
2589 return false;
2592 for (datai = 0; datai < data_count; datai++)
2594 bfd_byte *format = format_header_data;
2595 struct fileinfo fe;
2597 memset (&fe, 0, sizeof fe);
2598 for (formati = 0; formati < format_count; formati++)
2600 bfd_vma content_type, form;
2601 char *string_trash;
2602 char **stringp = &string_trash;
2603 unsigned int uint_trash, *uintp = &uint_trash;
2604 struct attribute attr;
2606 content_type = _bfd_safe_read_leb128 (abfd, &format, false, buf_end);
2607 switch (content_type)
2609 case DW_LNCT_path:
2610 stringp = &fe.name;
2611 break;
2612 case DW_LNCT_directory_index:
2613 uintp = &fe.dir;
2614 break;
2615 case DW_LNCT_timestamp:
2616 uintp = &fe.time;
2617 break;
2618 case DW_LNCT_size:
2619 uintp = &fe.size;
2620 break;
2621 case DW_LNCT_MD5:
2622 break;
2623 default:
2624 _bfd_error_handler
2625 (_("DWARF error: unknown format content type %" PRIu64),
2626 (uint64_t) content_type);
2627 bfd_set_error (bfd_error_bad_value);
2628 return false;
2631 form = _bfd_safe_read_leb128 (abfd, &format, false, buf_end);
2632 buf = read_attribute_value (&attr, form, 0, unit, buf, buf_end);
2633 if (buf == NULL)
2634 return false;
2635 switch (form)
2637 case DW_FORM_string:
2638 case DW_FORM_line_strp:
2639 case DW_FORM_strx:
2640 case DW_FORM_strx1:
2641 case DW_FORM_strx2:
2642 case DW_FORM_strx3:
2643 case DW_FORM_strx4:
2644 *stringp = attr.u.str;
2645 break;
2647 case DW_FORM_data1:
2648 case DW_FORM_data2:
2649 case DW_FORM_data4:
2650 case DW_FORM_data8:
2651 case DW_FORM_udata:
2652 *uintp = attr.u.val;
2653 break;
2655 case DW_FORM_data16:
2656 /* MD5 data is in the attr.blk, but we are ignoring those. */
2657 break;
2661 if (!callback (table, fe.name, fe.dir, fe.time, fe.size))
2662 return false;
2665 *bufp = buf;
2666 return true;
2669 /* Decode the line number information for UNIT. */
2671 static struct line_info_table*
2672 decode_line_info (struct comp_unit *unit)
2674 bfd *abfd = unit->abfd;
2675 struct dwarf2_debug *stash = unit->stash;
2676 struct dwarf2_debug_file *file = unit->file;
2677 struct line_info_table* table;
2678 bfd_byte *line_ptr;
2679 bfd_byte *line_end;
2680 struct line_head lh;
2681 unsigned int i, offset_size;
2682 char *cur_file, *cur_dir;
2683 unsigned char op_code, extended_op, adj_opcode;
2684 unsigned int exop_len;
2685 size_t amt;
2687 if (unit->line_offset == 0 && file->line_table)
2688 return file->line_table;
2690 if (! read_section (abfd, &stash->debug_sections[debug_line],
2691 file->syms, unit->line_offset,
2692 &file->dwarf_line_buffer, &file->dwarf_line_size))
2693 return NULL;
2695 if (file->dwarf_line_size < 16)
2697 _bfd_error_handler
2698 (_("DWARF error: line info section is too small (%" PRId64 ")"),
2699 (int64_t) file->dwarf_line_size);
2700 bfd_set_error (bfd_error_bad_value);
2701 return NULL;
2703 line_ptr = file->dwarf_line_buffer + unit->line_offset;
2704 line_end = file->dwarf_line_buffer + file->dwarf_line_size;
2706 /* Read in the prologue. */
2707 lh.total_length = read_4_bytes (abfd, &line_ptr, line_end);
2708 offset_size = 4;
2709 if (lh.total_length == 0xffffffff)
2711 lh.total_length = read_8_bytes (abfd, &line_ptr, line_end);
2712 offset_size = 8;
2714 else if (lh.total_length == 0 && unit->addr_size == 8)
2716 /* Handle (non-standard) 64-bit DWARF2 formats. */
2717 lh.total_length = read_4_bytes (abfd, &line_ptr, line_end);
2718 offset_size = 8;
2721 if (lh.total_length > (size_t) (line_end - line_ptr))
2723 _bfd_error_handler
2724 /* xgettext: c-format */
2725 (_("DWARF error: line info data is bigger (%#" PRIx64 ")"
2726 " than the space remaining in the section (%#lx)"),
2727 (uint64_t) lh.total_length, (unsigned long) (line_end - line_ptr));
2728 bfd_set_error (bfd_error_bad_value);
2729 return NULL;
2732 line_end = line_ptr + lh.total_length;
2734 lh.version = read_2_bytes (abfd, &line_ptr, line_end);
2735 if (lh.version < 2 || lh.version > 5)
2737 _bfd_error_handler
2738 (_("DWARF error: unhandled .debug_line version %d"), lh.version);
2739 bfd_set_error (bfd_error_bad_value);
2740 return NULL;
2743 if (line_ptr + offset_size + (lh.version >= 5 ? 8 : (lh.version >= 4 ? 6 : 5))
2744 >= line_end)
2746 _bfd_error_handler
2747 (_("DWARF error: ran out of room reading prologue"));
2748 bfd_set_error (bfd_error_bad_value);
2749 return NULL;
2752 if (lh.version >= 5)
2754 unsigned int segment_selector_size;
2756 /* Skip address size. */
2757 read_1_byte (abfd, &line_ptr, line_end);
2759 segment_selector_size = read_1_byte (abfd, &line_ptr, line_end);
2760 if (segment_selector_size != 0)
2762 _bfd_error_handler
2763 (_("DWARF error: line info unsupported segment selector size %u"),
2764 segment_selector_size);
2765 bfd_set_error (bfd_error_bad_value);
2766 return NULL;
2770 if (offset_size == 4)
2771 lh.prologue_length = read_4_bytes (abfd, &line_ptr, line_end);
2772 else
2773 lh.prologue_length = read_8_bytes (abfd, &line_ptr, line_end);
2775 lh.minimum_instruction_length = read_1_byte (abfd, &line_ptr, line_end);
2777 if (lh.version >= 4)
2778 lh.maximum_ops_per_insn = read_1_byte (abfd, &line_ptr, line_end);
2779 else
2780 lh.maximum_ops_per_insn = 1;
2782 if (lh.maximum_ops_per_insn == 0)
2784 _bfd_error_handler
2785 (_("DWARF error: invalid maximum operations per instruction"));
2786 bfd_set_error (bfd_error_bad_value);
2787 return NULL;
2790 lh.default_is_stmt = read_1_byte (abfd, &line_ptr, line_end);
2791 lh.line_base = read_1_signed_byte (abfd, &line_ptr, line_end);
2792 lh.line_range = read_1_byte (abfd, &line_ptr, line_end);
2793 lh.opcode_base = read_1_byte (abfd, &line_ptr, line_end);
2795 if (line_ptr + (lh.opcode_base - 1) >= line_end)
2797 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2798 bfd_set_error (bfd_error_bad_value);
2799 return NULL;
2802 amt = lh.opcode_base * sizeof (unsigned char);
2803 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
2805 lh.standard_opcode_lengths[0] = 1;
2807 for (i = 1; i < lh.opcode_base; ++i)
2808 lh.standard_opcode_lengths[i] = read_1_byte (abfd, &line_ptr, line_end);
2810 amt = sizeof (struct line_info_table);
2811 table = (struct line_info_table *) bfd_alloc (abfd, amt);
2812 if (table == NULL)
2813 return NULL;
2814 table->abfd = abfd;
2815 table->comp_dir = unit->comp_dir;
2817 table->num_files = 0;
2818 table->files = NULL;
2820 table->num_dirs = 0;
2821 table->dirs = NULL;
2823 table->num_sequences = 0;
2824 table->sequences = NULL;
2826 table->lcl_head = NULL;
2828 if (lh.version >= 5)
2830 /* Read directory table. */
2831 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2832 line_info_add_include_dir_stub))
2833 goto fail;
2835 /* Read file name table. */
2836 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2837 line_info_add_file_name))
2838 goto fail;
2839 table->use_dir_and_file_0 = true;
2841 else
2843 /* Read directory table. */
2844 while ((cur_dir = read_string (&line_ptr, line_end)) != NULL)
2846 if (!line_info_add_include_dir (table, cur_dir))
2847 goto fail;
2850 /* Read file name table. */
2851 while ((cur_file = read_string (&line_ptr, line_end)) != NULL)
2853 unsigned int dir, xtime, size;
2855 dir = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2856 xtime = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2857 size = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2859 if (!line_info_add_file_name (table, cur_file, dir, xtime, size))
2860 goto fail;
2862 table->use_dir_and_file_0 = false;
2865 /* Read the statement sequences until there's nothing left. */
2866 while (line_ptr < line_end)
2868 /* State machine registers. */
2869 bfd_vma address = 0;
2870 unsigned char op_index = 0;
2871 char * filename = NULL;
2872 unsigned int line = 1;
2873 unsigned int column = 0;
2874 unsigned int discriminator = 0;
2875 int is_stmt = lh.default_is_stmt;
2876 int end_sequence = 0;
2877 unsigned int dir, xtime, size;
2878 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2879 compilers generate address sequences that are wildly out of
2880 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2881 for ia64-Linux). Thus, to determine the low and high
2882 address, we must compare on every DW_LNS_copy, etc. */
2883 bfd_vma low_pc = (bfd_vma) -1;
2884 bfd_vma high_pc = 0;
2886 if (table->num_files)
2888 if (table->use_dir_and_file_0)
2889 filename = concat_filename (table, 0);
2890 else
2891 filename = concat_filename (table, 1);
2894 /* Decode the table. */
2895 while (!end_sequence && line_ptr < line_end)
2897 op_code = read_1_byte (abfd, &line_ptr, line_end);
2899 if (op_code >= lh.opcode_base)
2901 /* Special operand. */
2902 adj_opcode = op_code - lh.opcode_base;
2903 if (lh.line_range == 0)
2904 goto line_fail;
2905 if (lh.maximum_ops_per_insn == 1)
2906 address += (adj_opcode / lh.line_range
2907 * lh.minimum_instruction_length);
2908 else
2910 address += ((op_index + adj_opcode / lh.line_range)
2911 / lh.maximum_ops_per_insn
2912 * lh.minimum_instruction_length);
2913 op_index = ((op_index + adj_opcode / lh.line_range)
2914 % lh.maximum_ops_per_insn);
2916 line += lh.line_base + (adj_opcode % lh.line_range);
2917 /* Append row to matrix using current values. */
2918 if (!add_line_info (table, address, op_index, filename,
2919 line, column, discriminator, 0))
2920 goto line_fail;
2921 discriminator = 0;
2922 if (address < low_pc)
2923 low_pc = address;
2924 if (address > high_pc)
2925 high_pc = address;
2927 else switch (op_code)
2929 case DW_LNS_extended_op:
2930 exop_len = _bfd_safe_read_leb128 (abfd, &line_ptr,
2931 false, line_end);
2932 extended_op = read_1_byte (abfd, &line_ptr, line_end);
2934 switch (extended_op)
2936 case DW_LNE_end_sequence:
2937 end_sequence = 1;
2938 if (!add_line_info (table, address, op_index, filename, line,
2939 column, discriminator, end_sequence))
2940 goto line_fail;
2941 discriminator = 0;
2942 if (address < low_pc)
2943 low_pc = address;
2944 if (address > high_pc)
2945 high_pc = address;
2946 if (!arange_add (unit, &unit->arange, &unit->file->trie_root,
2947 low_pc, high_pc))
2948 goto line_fail;
2949 break;
2950 case DW_LNE_set_address:
2951 address = read_address (unit, &line_ptr, line_end);
2952 op_index = 0;
2953 break;
2954 case DW_LNE_define_file:
2955 cur_file = read_string (&line_ptr, line_end);
2956 dir = _bfd_safe_read_leb128 (abfd, &line_ptr,
2957 false, line_end);
2958 xtime = _bfd_safe_read_leb128 (abfd, &line_ptr,
2959 false, line_end);
2960 size = _bfd_safe_read_leb128 (abfd, &line_ptr,
2961 false, line_end);
2962 if (!line_info_add_file_name (table, cur_file, dir,
2963 xtime, size))
2964 goto line_fail;
2965 break;
2966 case DW_LNE_set_discriminator:
2967 discriminator = _bfd_safe_read_leb128 (abfd, &line_ptr,
2968 false, line_end);
2969 break;
2970 case DW_LNE_HP_source_file_correlation:
2971 line_ptr += exop_len - 1;
2972 break;
2973 default:
2974 _bfd_error_handler
2975 (_("DWARF error: mangled line number section"));
2976 bfd_set_error (bfd_error_bad_value);
2977 line_fail:
2978 free (filename);
2979 goto fail;
2981 break;
2982 case DW_LNS_copy:
2983 if (!add_line_info (table, address, op_index,
2984 filename, line, column, discriminator, 0))
2985 goto line_fail;
2986 discriminator = 0;
2987 if (address < low_pc)
2988 low_pc = address;
2989 if (address > high_pc)
2990 high_pc = address;
2991 break;
2992 case DW_LNS_advance_pc:
2993 if (lh.maximum_ops_per_insn == 1)
2994 address += (lh.minimum_instruction_length
2995 * _bfd_safe_read_leb128 (abfd, &line_ptr,
2996 false, line_end));
2997 else
2999 bfd_vma adjust = _bfd_safe_read_leb128 (abfd, &line_ptr,
3000 false, line_end);
3001 address = ((op_index + adjust) / lh.maximum_ops_per_insn
3002 * lh.minimum_instruction_length);
3003 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
3005 break;
3006 case DW_LNS_advance_line:
3007 line += _bfd_safe_read_leb128 (abfd, &line_ptr,
3008 true, line_end);
3009 break;
3010 case DW_LNS_set_file:
3012 unsigned int filenum;
3014 /* The file and directory tables are 0
3015 based, the references are 1 based. */
3016 filenum = _bfd_safe_read_leb128 (abfd, &line_ptr,
3017 false, line_end);
3018 free (filename);
3019 filename = concat_filename (table, filenum);
3020 break;
3022 case DW_LNS_set_column:
3023 column = _bfd_safe_read_leb128 (abfd, &line_ptr,
3024 false, line_end);
3025 break;
3026 case DW_LNS_negate_stmt:
3027 is_stmt = (!is_stmt);
3028 break;
3029 case DW_LNS_set_basic_block:
3030 break;
3031 case DW_LNS_const_add_pc:
3032 if (lh.line_range == 0)
3033 goto line_fail;
3034 if (lh.maximum_ops_per_insn == 1)
3035 address += (lh.minimum_instruction_length
3036 * ((255 - lh.opcode_base) / lh.line_range));
3037 else
3039 bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
3040 address += (lh.minimum_instruction_length
3041 * ((op_index + adjust)
3042 / lh.maximum_ops_per_insn));
3043 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
3045 break;
3046 case DW_LNS_fixed_advance_pc:
3047 address += read_2_bytes (abfd, &line_ptr, line_end);
3048 op_index = 0;
3049 break;
3050 default:
3051 /* Unknown standard opcode, ignore it. */
3052 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
3053 (void) _bfd_safe_read_leb128 (abfd, &line_ptr,
3054 false, line_end);
3055 break;
3059 free (filename);
3062 if (unit->line_offset == 0)
3063 file->line_table = table;
3064 if (sort_line_sequences (table))
3065 return table;
3067 fail:
3068 while (table->sequences != NULL)
3070 struct line_sequence* seq = table->sequences;
3071 table->sequences = table->sequences->prev_sequence;
3072 free (seq);
3074 free (table->files);
3075 free (table->dirs);
3076 return NULL;
3079 /* If ADDR is within TABLE set the output parameters and return TRUE,
3080 otherwise set *FILENAME_PTR to NULL and return FALSE.
3081 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
3082 are pointers to the objects to be filled in. */
3084 static bool
3085 lookup_address_in_line_info_table (struct line_info_table *table,
3086 bfd_vma addr,
3087 const char **filename_ptr,
3088 unsigned int *linenumber_ptr,
3089 unsigned int *discriminator_ptr)
3091 struct line_sequence *seq = NULL;
3092 struct line_info *info;
3093 int low, high, mid;
3095 /* Binary search the array of sequences. */
3096 low = 0;
3097 high = table->num_sequences;
3098 while (low < high)
3100 mid = (low + high) / 2;
3101 seq = &table->sequences[mid];
3102 if (addr < seq->low_pc)
3103 high = mid;
3104 else if (addr >= seq->last_line->address)
3105 low = mid + 1;
3106 else
3107 break;
3110 /* Check for a valid sequence. */
3111 if (!seq || addr < seq->low_pc || addr >= seq->last_line->address)
3112 goto fail;
3114 if (!build_line_info_table (table, seq))
3115 goto fail;
3117 /* Binary search the array of line information. */
3118 low = 0;
3119 high = seq->num_lines;
3120 info = NULL;
3121 while (low < high)
3123 mid = (low + high) / 2;
3124 info = seq->line_info_lookup[mid];
3125 if (addr < info->address)
3126 high = mid;
3127 else if (addr >= seq->line_info_lookup[mid + 1]->address)
3128 low = mid + 1;
3129 else
3130 break;
3133 /* Check for a valid line information entry. */
3134 if (info
3135 && addr >= info->address
3136 && addr < seq->line_info_lookup[mid + 1]->address
3137 && !(info->end_sequence || info == seq->last_line))
3139 *filename_ptr = info->filename;
3140 *linenumber_ptr = info->line;
3141 if (discriminator_ptr)
3142 *discriminator_ptr = info->discriminator;
3143 return true;
3146 fail:
3147 *filename_ptr = NULL;
3148 return false;
3151 /* Read in the .debug_ranges section for future reference. */
3153 static bool
3154 read_debug_ranges (struct comp_unit * unit)
3156 struct dwarf2_debug *stash = unit->stash;
3157 struct dwarf2_debug_file *file = unit->file;
3159 return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
3160 file->syms, 0,
3161 &file->dwarf_ranges_buffer, &file->dwarf_ranges_size);
3164 /* Read in the .debug_rnglists section for future reference. */
3166 static bool
3167 read_debug_rnglists (struct comp_unit * unit)
3169 struct dwarf2_debug *stash = unit->stash;
3170 struct dwarf2_debug_file *file = unit->file;
3172 return read_section (unit->abfd, &stash->debug_sections[debug_rnglists],
3173 file->syms, 0,
3174 &file->dwarf_rnglists_buffer, &file->dwarf_rnglists_size);
3177 /* Function table functions. */
3179 static int
3180 compare_lookup_funcinfos (const void * a, const void * b)
3182 const struct lookup_funcinfo * lookup1 = a;
3183 const struct lookup_funcinfo * lookup2 = b;
3185 if (lookup1->low_addr < lookup2->low_addr)
3186 return -1;
3187 if (lookup1->low_addr > lookup2->low_addr)
3188 return 1;
3189 if (lookup1->high_addr < lookup2->high_addr)
3190 return -1;
3191 if (lookup1->high_addr > lookup2->high_addr)
3192 return 1;
3194 if (lookup1->idx < lookup2->idx)
3195 return -1;
3196 if (lookup1->idx > lookup2->idx)
3197 return 1;
3198 return 0;
3201 static bool
3202 build_lookup_funcinfo_table (struct comp_unit * unit)
3204 struct lookup_funcinfo *lookup_funcinfo_table = unit->lookup_funcinfo_table;
3205 unsigned int number_of_functions = unit->number_of_functions;
3206 struct funcinfo *each;
3207 struct lookup_funcinfo *entry;
3208 size_t func_index;
3209 struct arange *range;
3210 bfd_vma low_addr, high_addr;
3212 if (lookup_funcinfo_table || number_of_functions == 0)
3213 return true;
3215 /* Create the function info lookup table. */
3216 lookup_funcinfo_table = (struct lookup_funcinfo *)
3217 bfd_malloc (number_of_functions * sizeof (struct lookup_funcinfo));
3218 if (lookup_funcinfo_table == NULL)
3219 return false;
3221 /* Populate the function info lookup table. */
3222 func_index = number_of_functions;
3223 for (each = unit->function_table; each; each = each->prev_func)
3225 entry = &lookup_funcinfo_table[--func_index];
3226 entry->funcinfo = each;
3227 entry->idx = func_index;
3229 /* Calculate the lowest and highest address for this function entry. */
3230 low_addr = entry->funcinfo->arange.low;
3231 high_addr = entry->funcinfo->arange.high;
3233 for (range = entry->funcinfo->arange.next; range; range = range->next)
3235 if (range->low < low_addr)
3236 low_addr = range->low;
3237 if (range->high > high_addr)
3238 high_addr = range->high;
3241 entry->low_addr = low_addr;
3242 entry->high_addr = high_addr;
3245 BFD_ASSERT (func_index == 0);
3247 /* Sort the function by address. */
3248 qsort (lookup_funcinfo_table,
3249 number_of_functions,
3250 sizeof (struct lookup_funcinfo),
3251 compare_lookup_funcinfos);
3253 /* Calculate the high watermark for each function in the lookup table. */
3254 high_addr = lookup_funcinfo_table[0].high_addr;
3255 for (func_index = 1; func_index < number_of_functions; func_index++)
3257 entry = &lookup_funcinfo_table[func_index];
3258 if (entry->high_addr > high_addr)
3259 high_addr = entry->high_addr;
3260 else
3261 entry->high_addr = high_addr;
3264 unit->lookup_funcinfo_table = lookup_funcinfo_table;
3265 return true;
3268 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
3269 TRUE. Note that we need to find the function that has the smallest range
3270 that contains ADDR, to handle inlined functions without depending upon
3271 them being ordered in TABLE by increasing range. */
3273 static bool
3274 lookup_address_in_function_table (struct comp_unit *unit,
3275 bfd_vma addr,
3276 struct funcinfo **function_ptr)
3278 unsigned int number_of_functions = unit->number_of_functions;
3279 struct lookup_funcinfo* lookup_funcinfo = NULL;
3280 struct funcinfo* funcinfo = NULL;
3281 struct funcinfo* best_fit = NULL;
3282 bfd_vma best_fit_len = (bfd_vma) -1;
3283 bfd_size_type low, high, mid, first;
3284 struct arange *arange;
3286 if (number_of_functions == 0)
3287 return false;
3289 if (!build_lookup_funcinfo_table (unit))
3290 return false;
3292 if (unit->lookup_funcinfo_table[number_of_functions - 1].high_addr < addr)
3293 return false;
3295 /* Find the first function in the lookup table which may contain the
3296 specified address. */
3297 low = 0;
3298 high = number_of_functions;
3299 first = high;
3300 while (low < high)
3302 mid = (low + high) / 2;
3303 lookup_funcinfo = &unit->lookup_funcinfo_table[mid];
3304 if (addr < lookup_funcinfo->low_addr)
3305 high = mid;
3306 else if (addr >= lookup_funcinfo->high_addr)
3307 low = mid + 1;
3308 else
3309 high = first = mid;
3312 /* Find the 'best' match for the address. The prior algorithm defined the
3313 best match as the function with the smallest address range containing
3314 the specified address. This definition should probably be changed to the
3315 innermost inline routine containing the address, but right now we want
3316 to get the same results we did before. */
3317 while (first < number_of_functions)
3319 if (addr < unit->lookup_funcinfo_table[first].low_addr)
3320 break;
3321 funcinfo = unit->lookup_funcinfo_table[first].funcinfo;
3323 for (arange = &funcinfo->arange; arange; arange = arange->next)
3325 if (addr < arange->low || addr >= arange->high)
3326 continue;
3328 if (arange->high - arange->low < best_fit_len
3329 /* The following comparison is designed to return the same
3330 match as the previous algorithm for routines which have the
3331 same best fit length. */
3332 || (arange->high - arange->low == best_fit_len
3333 && funcinfo > best_fit))
3335 best_fit = funcinfo;
3336 best_fit_len = arange->high - arange->low;
3340 first++;
3343 if (!best_fit)
3344 return false;
3346 *function_ptr = best_fit;
3347 return true;
3350 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
3351 and LINENUMBER_PTR, and return TRUE. */
3353 static bool
3354 lookup_symbol_in_function_table (struct comp_unit *unit,
3355 asymbol *sym,
3356 bfd_vma addr,
3357 const char **filename_ptr,
3358 unsigned int *linenumber_ptr)
3360 struct funcinfo* each;
3361 struct funcinfo* best_fit = NULL;
3362 bfd_vma best_fit_len = (bfd_vma) -1;
3363 struct arange *arange;
3364 const char *name = bfd_asymbol_name (sym);
3366 for (each = unit->function_table; each; each = each->prev_func)
3367 for (arange = &each->arange; arange; arange = arange->next)
3368 if (addr >= arange->low
3369 && addr < arange->high
3370 && arange->high - arange->low < best_fit_len
3371 && each->file
3372 && each->name
3373 && strstr (name, each->name) != NULL)
3375 best_fit = each;
3376 best_fit_len = arange->high - arange->low;
3379 if (best_fit)
3381 *filename_ptr = best_fit->file;
3382 *linenumber_ptr = best_fit->line;
3383 return true;
3386 return false;
3389 /* Variable table functions. */
3391 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
3392 LINENUMBER_PTR, and return TRUE. */
3394 static bool
3395 lookup_symbol_in_variable_table (struct comp_unit *unit,
3396 asymbol *sym,
3397 bfd_vma addr,
3398 const char **filename_ptr,
3399 unsigned int *linenumber_ptr)
3401 struct varinfo* each;
3402 const char *name = bfd_asymbol_name (sym);
3404 for (each = unit->variable_table; each; each = each->prev_var)
3405 if (each->addr == addr
3406 && !each->stack
3407 && each->file != NULL
3408 && each->name != NULL
3409 && strstr (name, each->name) != NULL)
3410 break;
3412 if (each)
3414 *filename_ptr = each->file;
3415 *linenumber_ptr = each->line;
3416 return true;
3419 return false;
3422 static struct comp_unit *stash_comp_unit (struct dwarf2_debug *,
3423 struct dwarf2_debug_file *);
3424 static bool comp_unit_maybe_decode_line_info (struct comp_unit *);
3426 static bool
3427 find_abstract_instance (struct comp_unit *unit,
3428 struct attribute *attr_ptr,
3429 unsigned int recur_count,
3430 const char **pname,
3431 bool *is_linkage,
3432 char **filename_ptr,
3433 int *linenumber_ptr)
3435 bfd *abfd = unit->abfd;
3436 bfd_byte *info_ptr = NULL;
3437 bfd_byte *info_ptr_end;
3438 unsigned int abbrev_number, i;
3439 struct abbrev_info *abbrev;
3440 uint64_t die_ref = attr_ptr->u.val;
3441 struct attribute attr;
3443 if (recur_count == 100)
3445 _bfd_error_handler
3446 (_("DWARF error: abstract instance recursion detected"));
3447 bfd_set_error (bfd_error_bad_value);
3448 return false;
3451 /* DW_FORM_ref_addr can reference an entry in a different CU. It
3452 is an offset from the .debug_info section, not the current CU. */
3453 if (attr_ptr->form == DW_FORM_ref_addr)
3455 /* We only support DW_FORM_ref_addr within the same file, so
3456 any relocations should be resolved already. Check this by
3457 testing for a zero die_ref; There can't be a valid reference
3458 to the header of a .debug_info section.
3459 DW_FORM_ref_addr is an offset relative to .debug_info.
3460 Normally when using the GNU linker this is accomplished by
3461 emitting a symbolic reference to a label, because .debug_info
3462 sections are linked at zero. When there are multiple section
3463 groups containing .debug_info, as there might be in a
3464 relocatable object file, it would be reasonable to assume that
3465 a symbolic reference to a label in any .debug_info section
3466 might be used. Since we lay out multiple .debug_info
3467 sections at non-zero VMAs (see place_sections), and read
3468 them contiguously into dwarf_info_buffer, that means the
3469 reference is relative to dwarf_info_buffer. */
3470 size_t total;
3472 info_ptr = unit->file->dwarf_info_buffer;
3473 info_ptr_end = info_ptr + unit->file->dwarf_info_size;
3474 total = info_ptr_end - info_ptr;
3475 if (!die_ref)
3476 return true;
3477 else if (die_ref >= total)
3479 _bfd_error_handler
3480 (_("DWARF error: invalid abstract instance DIE ref"));
3481 bfd_set_error (bfd_error_bad_value);
3482 return false;
3484 info_ptr += die_ref;
3486 else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
3488 bool first_time = unit->stash->alt.dwarf_info_buffer == NULL;
3490 info_ptr = read_alt_indirect_ref (unit, die_ref);
3491 if (first_time)
3492 unit->stash->alt.info_ptr = unit->stash->alt.dwarf_info_buffer;
3493 if (info_ptr == NULL)
3495 _bfd_error_handler
3496 (_("DWARF error: unable to read alt ref %" PRIu64),
3497 (uint64_t) die_ref);
3498 bfd_set_error (bfd_error_bad_value);
3499 return false;
3501 info_ptr_end = (unit->stash->alt.dwarf_info_buffer
3502 + unit->stash->alt.dwarf_info_size);
3503 if (unit->stash->alt.all_comp_units)
3504 unit = unit->stash->alt.all_comp_units;
3507 if (attr_ptr->form == DW_FORM_ref_addr
3508 || attr_ptr->form == DW_FORM_GNU_ref_alt)
3510 /* Now find the CU containing this pointer. */
3511 if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr)
3512 info_ptr_end = unit->end_ptr;
3513 else
3515 /* Check other CUs to see if they contain the abbrev. */
3516 struct comp_unit *u = NULL;
3517 struct addr_range range = { info_ptr, info_ptr };
3518 splay_tree_node v = splay_tree_lookup (unit->file->comp_unit_tree,
3519 (splay_tree_key)&range);
3520 if (v != NULL)
3521 u = (struct comp_unit *)v->value;
3523 if (attr_ptr->form == DW_FORM_ref_addr)
3524 while (u == NULL)
3526 u = stash_comp_unit (unit->stash, &unit->stash->f);
3527 if (u == NULL)
3528 break;
3529 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3530 break;
3531 u = NULL;
3534 if (attr_ptr->form == DW_FORM_GNU_ref_alt)
3535 while (u == NULL)
3537 u = stash_comp_unit (unit->stash, &unit->stash->alt);
3538 if (u == NULL)
3539 break;
3540 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3541 break;
3542 u = NULL;
3545 if (u == NULL)
3547 _bfd_error_handler
3548 (_("DWARF error: unable to locate abstract instance DIE ref %"
3549 PRIu64), (uint64_t) die_ref);
3550 bfd_set_error (bfd_error_bad_value);
3551 return false;
3553 unit = u;
3554 info_ptr_end = unit->end_ptr;
3557 else
3559 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
3560 DW_FORM_ref_udata. These are all references relative to the
3561 start of the current CU. */
3562 size_t total;
3564 info_ptr = unit->info_ptr_unit;
3565 info_ptr_end = unit->end_ptr;
3566 total = info_ptr_end - info_ptr;
3567 if (!die_ref || die_ref >= total)
3569 _bfd_error_handler
3570 (_("DWARF error: invalid abstract instance DIE ref"));
3571 bfd_set_error (bfd_error_bad_value);
3572 return false;
3574 info_ptr += die_ref;
3577 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
3578 false, info_ptr_end);
3579 if (abbrev_number)
3581 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3582 if (! abbrev)
3584 _bfd_error_handler
3585 (_("DWARF error: could not find abbrev number %u"), abbrev_number);
3586 bfd_set_error (bfd_error_bad_value);
3587 return false;
3589 else
3591 for (i = 0; i < abbrev->num_attrs; ++i)
3593 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
3594 info_ptr, info_ptr_end);
3595 if (info_ptr == NULL)
3596 break;
3597 switch (attr.name)
3599 case DW_AT_name:
3600 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3601 over DW_AT_name. */
3602 if (*pname == NULL && is_str_form (&attr))
3604 *pname = attr.u.str;
3605 if (mangle_style (unit->lang) == 0)
3606 *is_linkage = true;
3608 break;
3609 case DW_AT_specification:
3610 if (is_int_form (&attr)
3611 && !find_abstract_instance (unit, &attr, recur_count + 1,
3612 pname, is_linkage,
3613 filename_ptr, linenumber_ptr))
3614 return false;
3615 break;
3616 case DW_AT_linkage_name:
3617 case DW_AT_MIPS_linkage_name:
3618 /* PR 16949: Corrupt debug info can place
3619 non-string forms into these attributes. */
3620 if (is_str_form (&attr))
3622 *pname = attr.u.str;
3623 *is_linkage = true;
3625 break;
3626 case DW_AT_decl_file:
3627 if (!comp_unit_maybe_decode_line_info (unit))
3628 return false;
3629 if (is_int_form (&attr))
3631 free (*filename_ptr);
3632 *filename_ptr = concat_filename (unit->line_table,
3633 attr.u.val);
3635 break;
3636 case DW_AT_decl_line:
3637 if (is_int_form (&attr))
3638 *linenumber_ptr = attr.u.val;
3639 break;
3640 default:
3641 break;
3646 return true;
3649 static bool
3650 read_ranges (struct comp_unit *unit, struct arange *arange,
3651 struct trie_node **trie_root, uint64_t offset)
3653 bfd_byte *ranges_ptr;
3654 bfd_byte *ranges_end;
3655 bfd_vma base_address = unit->base_address;
3657 if (! unit->file->dwarf_ranges_buffer)
3659 if (! read_debug_ranges (unit))
3660 return false;
3663 if (offset > unit->file->dwarf_ranges_size)
3664 return false;
3665 ranges_ptr = unit->file->dwarf_ranges_buffer + offset;
3666 ranges_end = unit->file->dwarf_ranges_buffer + unit->file->dwarf_ranges_size;
3668 for (;;)
3670 bfd_vma low_pc;
3671 bfd_vma high_pc;
3673 /* PR 17512: file: 62cada7d. */
3674 if (2u * unit->addr_size > (size_t) (ranges_end - ranges_ptr))
3675 return false;
3677 low_pc = read_address (unit, &ranges_ptr, ranges_end);
3678 high_pc = read_address (unit, &ranges_ptr, ranges_end);
3680 if (low_pc == 0 && high_pc == 0)
3681 break;
3682 if (low_pc == -1UL && high_pc != -1UL)
3683 base_address = high_pc;
3684 else
3686 if (!arange_add (unit, arange, trie_root,
3687 base_address + low_pc, base_address + high_pc))
3688 return false;
3691 return true;
3694 static bool
3695 read_rnglists (struct comp_unit *unit, struct arange *arange,
3696 struct trie_node **trie_root, uint64_t offset)
3698 bfd_byte *rngs_ptr;
3699 bfd_byte *rngs_end;
3700 bfd_vma base_address = unit->base_address;
3701 bfd_vma low_pc;
3702 bfd_vma high_pc;
3703 bfd *abfd = unit->abfd;
3705 if (! unit->file->dwarf_rnglists_buffer)
3707 if (! read_debug_rnglists (unit))
3708 return false;
3711 rngs_ptr = unit->file->dwarf_rnglists_buffer + offset;
3712 if (rngs_ptr < unit->file->dwarf_rnglists_buffer)
3713 return false;
3714 rngs_end = unit->file->dwarf_rnglists_buffer;
3715 rngs_end += unit->file->dwarf_rnglists_size;
3717 for (;;)
3719 enum dwarf_range_list_entry rlet;
3721 if (rngs_ptr >= rngs_end)
3722 return false;
3724 rlet = read_1_byte (abfd, &rngs_ptr, rngs_end);
3726 switch (rlet)
3728 case DW_RLE_end_of_list:
3729 return true;
3731 case DW_RLE_base_address:
3732 if (unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3733 return false;
3734 base_address = read_address (unit, &rngs_ptr, rngs_end);
3735 continue;
3737 case DW_RLE_start_length:
3738 if (unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3739 return false;
3740 low_pc = read_address (unit, &rngs_ptr, rngs_end);
3741 high_pc = low_pc;
3742 high_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3743 false, rngs_end);
3744 break;
3746 case DW_RLE_offset_pair:
3747 low_pc = base_address;
3748 low_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3749 false, rngs_end);
3750 high_pc = base_address;
3751 high_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3752 false, rngs_end);
3753 break;
3755 case DW_RLE_start_end:
3756 if (2u * unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3757 return false;
3758 low_pc = read_address (unit, &rngs_ptr, rngs_end);
3759 high_pc = read_address (unit, &rngs_ptr, rngs_end);
3760 break;
3762 /* TODO x-variants need .debug_addr support used for split-dwarf. */
3763 case DW_RLE_base_addressx:
3764 case DW_RLE_startx_endx:
3765 case DW_RLE_startx_length:
3766 default:
3767 return false;
3770 if (!arange_add (unit, arange, trie_root, low_pc, high_pc))
3771 return false;
3775 static bool
3776 read_rangelist (struct comp_unit *unit, struct arange *arange,
3777 struct trie_node **trie_root, uint64_t offset)
3779 if (unit->version <= 4)
3780 return read_ranges (unit, arange, trie_root, offset);
3781 else
3782 return read_rnglists (unit, arange, trie_root, offset);
3785 static struct funcinfo *
3786 lookup_func_by_offset (uint64_t offset, struct funcinfo * table)
3788 for (; table != NULL; table = table->prev_func)
3789 if (table->unit_offset == offset)
3790 return table;
3791 return NULL;
3794 static struct varinfo *
3795 lookup_var_by_offset (uint64_t offset, struct varinfo * table)
3797 while (table)
3799 if (table->unit_offset == offset)
3800 return table;
3801 table = table->prev_var;
3804 return NULL;
3808 /* DWARF2 Compilation unit functions. */
3810 static struct funcinfo *
3811 reverse_funcinfo_list (struct funcinfo *head)
3813 struct funcinfo *rhead;
3814 struct funcinfo *temp;
3816 for (rhead = NULL; head; head = temp)
3818 temp = head->prev_func;
3819 head->prev_func = rhead;
3820 rhead = head;
3822 return rhead;
3825 static struct varinfo *
3826 reverse_varinfo_list (struct varinfo *head)
3828 struct varinfo *rhead;
3829 struct varinfo *temp;
3831 for (rhead = NULL; head; head = temp)
3833 temp = head->prev_var;
3834 head->prev_var = rhead;
3835 rhead = head;
3837 return rhead;
3840 /* Scan over each die in a comp. unit looking for functions to add
3841 to the function table and variables to the variable table. */
3843 static bool
3844 scan_unit_for_symbols (struct comp_unit *unit)
3846 bfd *abfd = unit->abfd;
3847 bfd_byte *info_ptr = unit->first_child_die_ptr;
3848 bfd_byte *info_ptr_end = unit->end_ptr;
3849 int nesting_level = 0;
3850 struct nest_funcinfo
3852 struct funcinfo *func;
3853 } *nested_funcs;
3854 int nested_funcs_size;
3855 struct funcinfo *last_func;
3856 struct varinfo *last_var;
3858 /* Maintain a stack of in-scope functions and inlined functions, which we
3859 can use to set the caller_func field. */
3860 nested_funcs_size = 32;
3861 nested_funcs = (struct nest_funcinfo *)
3862 bfd_malloc (nested_funcs_size * sizeof (*nested_funcs));
3863 if (nested_funcs == NULL)
3864 return false;
3865 nested_funcs[nesting_level].func = 0;
3867 /* PR 27484: We must scan the DIEs twice. The first time we look for
3868 function and variable tags and accumulate them into their respective
3869 tables. The second time through we process the attributes of the
3870 functions/variables and augment the table entries. */
3871 while (nesting_level >= 0)
3873 unsigned int abbrev_number, i;
3874 struct abbrev_info *abbrev;
3875 struct funcinfo *func;
3876 struct varinfo *var;
3877 uint64_t current_offset;
3879 /* PR 17512: file: 9f405d9d. */
3880 if (info_ptr >= info_ptr_end)
3881 goto fail;
3883 current_offset = info_ptr - unit->info_ptr_unit;
3884 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
3885 false, info_ptr_end);
3886 if (abbrev_number == 0)
3888 nesting_level--;
3889 continue;
3892 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3893 if (! abbrev)
3895 static unsigned int previous_failed_abbrev = -1U;
3897 /* Avoid multiple reports of the same missing abbrev. */
3898 if (abbrev_number != previous_failed_abbrev)
3900 _bfd_error_handler
3901 (_("DWARF error: could not find abbrev number %u"),
3902 abbrev_number);
3903 previous_failed_abbrev = abbrev_number;
3905 bfd_set_error (bfd_error_bad_value);
3906 goto fail;
3909 if (abbrev->tag == DW_TAG_subprogram
3910 || abbrev->tag == DW_TAG_entry_point
3911 || abbrev->tag == DW_TAG_inlined_subroutine)
3913 size_t amt = sizeof (struct funcinfo);
3915 var = NULL;
3916 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
3917 if (func == NULL)
3918 goto fail;
3919 func->tag = abbrev->tag;
3920 func->prev_func = unit->function_table;
3921 func->unit_offset = current_offset;
3922 unit->function_table = func;
3923 unit->number_of_functions++;
3924 BFD_ASSERT (!unit->cached);
3926 if (func->tag == DW_TAG_inlined_subroutine)
3927 for (i = nesting_level; i-- != 0; )
3928 if (nested_funcs[i].func)
3930 func->caller_func = nested_funcs[i].func;
3931 break;
3933 nested_funcs[nesting_level].func = func;
3935 else
3937 func = NULL;
3938 if (abbrev->tag == DW_TAG_variable
3939 || abbrev->tag == DW_TAG_member)
3941 size_t amt = sizeof (struct varinfo);
3943 var = (struct varinfo *) bfd_zalloc (abfd, amt);
3944 if (var == NULL)
3945 goto fail;
3946 var->tag = abbrev->tag;
3947 var->stack = true;
3948 var->prev_var = unit->variable_table;
3949 unit->variable_table = var;
3950 var->unit_offset = current_offset;
3951 /* PR 18205: Missing debug information can cause this
3952 var to be attached to an already cached unit. */
3954 else
3955 var = NULL;
3957 /* No inline function in scope at this nesting level. */
3958 nested_funcs[nesting_level].func = 0;
3961 for (i = 0; i < abbrev->num_attrs; ++i)
3963 struct attribute attr;
3965 info_ptr = read_attribute (&attr, &abbrev->attrs[i],
3966 unit, info_ptr, info_ptr_end);
3967 if (info_ptr == NULL)
3968 goto fail;
3971 if (abbrev->has_children)
3973 nesting_level++;
3975 if (nesting_level >= nested_funcs_size)
3977 struct nest_funcinfo *tmp;
3979 nested_funcs_size *= 2;
3980 tmp = (struct nest_funcinfo *)
3981 bfd_realloc (nested_funcs,
3982 nested_funcs_size * sizeof (*nested_funcs));
3983 if (tmp == NULL)
3984 goto fail;
3985 nested_funcs = tmp;
3987 nested_funcs[nesting_level].func = 0;
3991 unit->function_table = reverse_funcinfo_list (unit->function_table);
3992 unit->variable_table = reverse_varinfo_list (unit->variable_table);
3994 /* This is the second pass over the abbrevs. */
3995 info_ptr = unit->first_child_die_ptr;
3996 nesting_level = 0;
3998 last_func = NULL;
3999 last_var = NULL;
4001 while (nesting_level >= 0)
4003 unsigned int abbrev_number, i;
4004 struct abbrev_info *abbrev;
4005 struct attribute attr;
4006 struct funcinfo *func;
4007 struct varinfo *var;
4008 bfd_vma low_pc = 0;
4009 bfd_vma high_pc = 0;
4010 bool high_pc_relative = false;
4011 uint64_t current_offset;
4013 /* PR 17512: file: 9f405d9d. */
4014 if (info_ptr >= info_ptr_end)
4015 goto fail;
4017 current_offset = info_ptr - unit->info_ptr_unit;
4018 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
4019 false, info_ptr_end);
4020 if (! abbrev_number)
4022 nesting_level--;
4023 continue;
4026 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
4027 /* This should have been handled above. */
4028 BFD_ASSERT (abbrev != NULL);
4030 func = NULL;
4031 var = NULL;
4032 if (abbrev->tag == DW_TAG_subprogram
4033 || abbrev->tag == DW_TAG_entry_point
4034 || abbrev->tag == DW_TAG_inlined_subroutine)
4036 if (last_func
4037 && last_func->prev_func
4038 && last_func->prev_func->unit_offset == current_offset)
4039 func = last_func->prev_func;
4040 else
4041 func = lookup_func_by_offset (current_offset, unit->function_table);
4043 if (func == NULL)
4044 goto fail;
4046 last_func = func;
4048 else if (abbrev->tag == DW_TAG_variable
4049 || abbrev->tag == DW_TAG_member)
4051 if (last_var
4052 && last_var->prev_var
4053 && last_var->prev_var->unit_offset == current_offset)
4054 var = last_var->prev_var;
4055 else
4056 var = lookup_var_by_offset (current_offset, unit->variable_table);
4058 if (var == NULL)
4059 goto fail;
4061 last_var = var;
4064 for (i = 0; i < abbrev->num_attrs; ++i)
4066 info_ptr = read_attribute (&attr, &abbrev->attrs[i],
4067 unit, info_ptr, info_ptr_end);
4068 if (info_ptr == NULL)
4069 goto fail;
4071 if (func)
4073 switch (attr.name)
4075 case DW_AT_call_file:
4076 if (is_int_form (&attr))
4077 func->caller_file = concat_filename (unit->line_table,
4078 attr.u.val);
4079 break;
4081 case DW_AT_call_line:
4082 if (is_int_form (&attr))
4083 func->caller_line = attr.u.val;
4084 break;
4086 case DW_AT_abstract_origin:
4087 case DW_AT_specification:
4088 if (is_int_form (&attr)
4089 && !find_abstract_instance (unit, &attr, 0,
4090 &func->name,
4091 &func->is_linkage,
4092 &func->file,
4093 &func->line))
4094 goto fail;
4095 break;
4097 case DW_AT_name:
4098 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
4099 over DW_AT_name. */
4100 if (func->name == NULL && is_str_form (&attr))
4102 func->name = attr.u.str;
4103 if (mangle_style (unit->lang) == 0)
4104 func->is_linkage = true;
4106 break;
4108 case DW_AT_linkage_name:
4109 case DW_AT_MIPS_linkage_name:
4110 /* PR 16949: Corrupt debug info can place
4111 non-string forms into these attributes. */
4112 if (is_str_form (&attr))
4114 func->name = attr.u.str;
4115 func->is_linkage = true;
4117 break;
4119 case DW_AT_low_pc:
4120 if (is_int_form (&attr))
4121 low_pc = attr.u.val;
4122 break;
4124 case DW_AT_high_pc:
4125 if (is_int_form (&attr))
4127 high_pc = attr.u.val;
4128 high_pc_relative = attr.form != DW_FORM_addr;
4130 break;
4132 case DW_AT_ranges:
4133 if (is_int_form (&attr)
4134 && !read_rangelist (unit, &func->arange,
4135 &unit->file->trie_root, attr.u.val))
4136 goto fail;
4137 break;
4139 case DW_AT_decl_file:
4140 if (is_int_form (&attr))
4142 free (func->file);
4143 func->file = concat_filename (unit->line_table,
4144 attr.u.val);
4146 break;
4148 case DW_AT_decl_line:
4149 if (is_int_form (&attr))
4150 func->line = attr.u.val;
4151 break;
4153 default:
4154 break;
4157 else if (var)
4159 switch (attr.name)
4161 case DW_AT_specification:
4162 if (is_int_form (&attr) && attr.u.val)
4164 bool is_linkage;
4165 if (!find_abstract_instance (unit, &attr, 0,
4166 &var->name,
4167 &is_linkage,
4168 &var->file,
4169 &var->line))
4171 _bfd_error_handler (_("DWARF error: could not find "
4172 "variable specification "
4173 "at offset 0x%lx"),
4174 (unsigned long) attr.u.val);
4175 break;
4178 break;
4180 case DW_AT_name:
4181 if (is_str_form (&attr))
4182 var->name = attr.u.str;
4183 break;
4185 case DW_AT_decl_file:
4186 if (is_int_form (&attr))
4188 free (var->file);
4189 var->file = concat_filename (unit->line_table,
4190 attr.u.val);
4192 break;
4194 case DW_AT_decl_line:
4195 if (is_int_form (&attr))
4196 var->line = attr.u.val;
4197 break;
4199 case DW_AT_external:
4200 if (is_int_form (&attr) && attr.u.val != 0)
4201 var->stack = false;
4202 break;
4204 case DW_AT_location:
4205 switch (attr.form)
4207 case DW_FORM_block:
4208 case DW_FORM_block1:
4209 case DW_FORM_block2:
4210 case DW_FORM_block4:
4211 case DW_FORM_exprloc:
4212 if (attr.u.blk->data != NULL
4213 && *attr.u.blk->data == DW_OP_addr)
4215 var->stack = false;
4217 /* Verify that DW_OP_addr is the only opcode in the
4218 location, in which case the block size will be 1
4219 plus the address size. */
4220 /* ??? For TLS variables, gcc can emit
4221 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
4222 which we don't handle here yet. */
4223 if (attr.u.blk->size == unit->addr_size + 1U)
4224 var->addr = bfd_get (unit->addr_size * 8,
4225 unit->abfd,
4226 attr.u.blk->data + 1);
4228 break;
4230 default:
4231 break;
4233 break;
4235 default:
4236 break;
4241 if (abbrev->has_children)
4242 nesting_level++;
4244 if (high_pc_relative)
4245 high_pc += low_pc;
4247 if (func && high_pc != 0)
4249 if (!arange_add (unit, &func->arange, &unit->file->trie_root,
4250 low_pc, high_pc))
4251 goto fail;
4255 unit->function_table = reverse_funcinfo_list (unit->function_table);
4256 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4258 free (nested_funcs);
4259 return true;
4261 fail:
4262 free (nested_funcs);
4263 return false;
4266 /* Read the attributes of the form strx and addrx. */
4268 static void
4269 reread_attribute (struct comp_unit *unit,
4270 struct attribute *attr,
4271 bfd_vma *low_pc,
4272 bfd_vma *high_pc,
4273 bool *high_pc_relative,
4274 bool compunit)
4276 if (is_strx_form (attr->form))
4277 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
4278 if (is_addrx_form (attr->form))
4279 attr->u.val = read_indexed_address (attr->u.val, unit);
4281 switch (attr->name)
4283 case DW_AT_stmt_list:
4284 unit->stmtlist = 1;
4285 unit->line_offset = attr->u.val;
4286 break;
4288 case DW_AT_name:
4289 if (is_str_form (attr))
4290 unit->name = attr->u.str;
4291 break;
4293 case DW_AT_low_pc:
4294 *low_pc = attr->u.val;
4295 if (compunit)
4296 unit->base_address = *low_pc;
4297 break;
4299 case DW_AT_high_pc:
4300 *high_pc = attr->u.val;
4301 *high_pc_relative = attr->form != DW_FORM_addr;
4302 break;
4304 case DW_AT_ranges:
4305 if (!read_rangelist (unit, &unit->arange,
4306 &unit->file->trie_root, attr->u.val))
4307 return;
4308 break;
4310 case DW_AT_comp_dir:
4312 char *comp_dir = attr->u.str;
4314 if (!is_str_form (attr))
4316 _bfd_error_handler
4317 (_("DWARF error: DW_AT_comp_dir attribute encountered "
4318 "with a non-string form"));
4319 comp_dir = NULL;
4322 if (comp_dir)
4324 char *cp = strchr (comp_dir, ':');
4326 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
4327 comp_dir = cp + 1;
4329 unit->comp_dir = comp_dir;
4330 break;
4333 case DW_AT_language:
4334 unit->lang = attr->u.val;
4335 default:
4336 break;
4340 /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
4341 includes the compilation unit header that proceeds the DIE's, but
4342 does not include the length field that precedes each compilation
4343 unit header. END_PTR points one past the end of this comp unit.
4344 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
4346 This routine does not read the whole compilation unit; only enough
4347 to get to the line number information for the compilation unit. */
4349 static struct comp_unit *
4350 parse_comp_unit (struct dwarf2_debug *stash,
4351 struct dwarf2_debug_file *file,
4352 bfd_byte *info_ptr,
4353 bfd_vma unit_length,
4354 bfd_byte *info_ptr_unit,
4355 unsigned int offset_size)
4357 struct comp_unit* unit;
4358 unsigned int version;
4359 uint64_t abbrev_offset = 0;
4360 /* Initialize it just to avoid a GCC false warning. */
4361 unsigned int addr_size = -1;
4362 struct abbrev_info** abbrevs;
4363 unsigned int abbrev_number, i;
4364 struct abbrev_info *abbrev;
4365 struct attribute attr;
4366 bfd_byte *end_ptr = info_ptr + unit_length;
4367 size_t amt;
4368 bfd_vma low_pc = 0;
4369 bfd_vma high_pc = 0;
4370 bfd *abfd = file->bfd_ptr;
4371 bool high_pc_relative = false;
4372 enum dwarf_unit_type unit_type;
4373 struct attribute *str_addrp = NULL;
4374 size_t str_count = 0;
4375 size_t str_alloc = 0;
4376 bool compunit_flag = false;
4378 version = read_2_bytes (abfd, &info_ptr, end_ptr);
4379 if (version < 2 || version > 5)
4381 /* PR 19872: A version number of 0 probably means that there is padding
4382 at the end of the .debug_info section. Gold puts it there when
4383 performing an incremental link, for example. So do not generate
4384 an error, just return a NULL. */
4385 if (version)
4387 _bfd_error_handler
4388 (_("DWARF error: found dwarf version '%u', this reader"
4389 " only handles version 2, 3, 4 and 5 information"), version);
4390 bfd_set_error (bfd_error_bad_value);
4392 return NULL;
4395 if (version < 5)
4396 unit_type = DW_UT_compile;
4397 else
4399 unit_type = read_1_byte (abfd, &info_ptr, end_ptr);
4400 addr_size = read_1_byte (abfd, &info_ptr, end_ptr);
4403 BFD_ASSERT (offset_size == 4 || offset_size == 8);
4404 if (offset_size == 4)
4405 abbrev_offset = read_4_bytes (abfd, &info_ptr, end_ptr);
4406 else
4407 abbrev_offset = read_8_bytes (abfd, &info_ptr, end_ptr);
4409 if (version < 5)
4410 addr_size = read_1_byte (abfd, &info_ptr, end_ptr);
4412 switch (unit_type)
4414 case DW_UT_type:
4415 /* Skip type signature. */
4416 info_ptr += 8;
4418 /* Skip type offset. */
4419 info_ptr += offset_size;
4420 break;
4422 case DW_UT_skeleton:
4423 /* Skip DWO_id field. */
4424 info_ptr += 8;
4425 break;
4427 default:
4428 break;
4431 if (addr_size > sizeof (bfd_vma))
4433 _bfd_error_handler
4434 /* xgettext: c-format */
4435 (_("DWARF error: found address size '%u', this reader"
4436 " can not handle sizes greater than '%u'"),
4437 addr_size,
4438 (unsigned int) sizeof (bfd_vma));
4439 bfd_set_error (bfd_error_bad_value);
4440 return NULL;
4443 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
4445 _bfd_error_handler
4446 ("DWARF error: found address size '%u', this reader"
4447 " can only handle address sizes '2', '4' and '8'", addr_size);
4448 bfd_set_error (bfd_error_bad_value);
4449 return NULL;
4452 /* Read the abbrevs for this compilation unit into a table. */
4453 abbrevs = read_abbrevs (abfd, abbrev_offset, stash, file);
4454 if (! abbrevs)
4455 return NULL;
4457 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
4458 false, end_ptr);
4459 if (! abbrev_number)
4461 /* PR 19872: An abbrev number of 0 probably means that there is padding
4462 at the end of the .debug_abbrev section. Gold puts it there when
4463 performing an incremental link, for example. So do not generate
4464 an error, just return a NULL. */
4465 return NULL;
4468 abbrev = lookup_abbrev (abbrev_number, abbrevs);
4469 if (! abbrev)
4471 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
4472 abbrev_number);
4473 bfd_set_error (bfd_error_bad_value);
4474 return NULL;
4477 amt = sizeof (struct comp_unit);
4478 unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
4479 if (unit == NULL)
4480 return NULL;
4481 unit->abfd = abfd;
4482 unit->version = version;
4483 unit->addr_size = addr_size;
4484 unit->offset_size = offset_size;
4485 unit->abbrevs = abbrevs;
4486 unit->end_ptr = end_ptr;
4487 unit->stash = stash;
4488 unit->file = file;
4489 unit->info_ptr_unit = info_ptr_unit;
4491 if (abbrev->tag == DW_TAG_compile_unit)
4492 compunit_flag = true;
4494 for (i = 0; i < abbrev->num_attrs; ++i)
4496 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, end_ptr);
4497 if (info_ptr == NULL)
4498 goto err_exit;
4500 /* Identify attributes of the form strx* and addrx* which come before
4501 DW_AT_str_offsets_base and DW_AT_addr_base respectively in the CU.
4502 Store the attributes in an array and process them later. */
4503 if ((unit->dwarf_str_offset == 0 && is_strx_form (attr.form))
4504 || (unit->dwarf_addr_offset == 0 && is_addrx_form (attr.form)))
4506 if (str_count <= str_alloc)
4508 str_alloc = 2 * str_alloc + 200;
4509 str_addrp = bfd_realloc (str_addrp,
4510 str_alloc * sizeof (*str_addrp));
4511 if (str_addrp == NULL)
4512 goto err_exit;
4514 str_addrp[str_count] = attr;
4515 str_count++;
4516 continue;
4519 /* Store the data if it is of an attribute we want to keep in a
4520 partial symbol table. */
4521 switch (attr.name)
4523 case DW_AT_stmt_list:
4524 if (is_int_form (&attr))
4526 unit->stmtlist = 1;
4527 unit->line_offset = attr.u.val;
4529 break;
4531 case DW_AT_name:
4532 if (is_str_form (&attr))
4533 unit->name = attr.u.str;
4534 break;
4536 case DW_AT_low_pc:
4537 if (is_int_form (&attr))
4539 low_pc = attr.u.val;
4540 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
4541 this is the base address to use when reading location
4542 lists or range lists. */
4543 if (compunit_flag)
4544 unit->base_address = low_pc;
4546 break;
4548 case DW_AT_high_pc:
4549 if (is_int_form (&attr))
4551 high_pc = attr.u.val;
4552 high_pc_relative = attr.form != DW_FORM_addr;
4554 break;
4556 case DW_AT_ranges:
4557 if (is_int_form (&attr)
4558 && !read_rangelist (unit, &unit->arange,
4559 &unit->file->trie_root, attr.u.val))
4560 goto err_exit;
4561 break;
4563 case DW_AT_comp_dir:
4565 char *comp_dir = attr.u.str;
4567 /* PR 17512: file: 1fe726be. */
4568 if (!is_str_form (&attr))
4570 _bfd_error_handler
4571 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
4572 comp_dir = NULL;
4575 if (comp_dir)
4577 /* Irix 6.2 native cc prepends <machine>.: to the compilation
4578 directory, get rid of it. */
4579 char *cp = strchr (comp_dir, ':');
4581 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
4582 comp_dir = cp + 1;
4584 unit->comp_dir = comp_dir;
4585 break;
4588 case DW_AT_language:
4589 if (is_int_form (&attr))
4590 unit->lang = attr.u.val;
4591 break;
4593 case DW_AT_addr_base:
4594 unit->dwarf_addr_offset = attr.u.val;
4595 break;
4597 case DW_AT_str_offsets_base:
4598 unit->dwarf_str_offset = attr.u.val;
4599 break;
4601 default:
4602 break;
4606 for (i = 0; i < str_count; ++i)
4607 reread_attribute (unit, &str_addrp[i], &low_pc, &high_pc,
4608 &high_pc_relative, compunit_flag);
4610 if (high_pc_relative)
4611 high_pc += low_pc;
4612 if (high_pc != 0)
4614 if (!arange_add (unit, &unit->arange, &unit->file->trie_root,
4615 low_pc, high_pc))
4616 goto err_exit;
4619 unit->first_child_die_ptr = info_ptr;
4621 free (str_addrp);
4622 return unit;
4624 err_exit:
4625 unit->error = 1;
4626 free (str_addrp);
4627 return NULL;
4630 /* Return TRUE if UNIT may contain the address given by ADDR. When
4631 there are functions written entirely with inline asm statements, the
4632 range info in the compilation unit header may not be correct. We
4633 need to consult the line info table to see if a compilation unit
4634 really contains the given address. */
4636 static bool
4637 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
4639 struct arange *arange;
4641 if (unit->error)
4642 return false;
4644 arange = &unit->arange;
4647 if (addr >= arange->low && addr < arange->high)
4648 return true;
4649 arange = arange->next;
4651 while (arange);
4653 return false;
4656 /* If UNIT contains ADDR, set the output parameters to the values for
4657 the line containing ADDR and return TRUE. Otherwise return FALSE.
4658 The output parameters, FILENAME_PTR, FUNCTION_PTR, and
4659 LINENUMBER_PTR, are pointers to the objects to be filled in. */
4661 static bool
4662 comp_unit_find_nearest_line (struct comp_unit *unit,
4663 bfd_vma addr,
4664 const char **filename_ptr,
4665 struct funcinfo **function_ptr,
4666 unsigned int *linenumber_ptr,
4667 unsigned int *discriminator_ptr)
4669 bool line_p, func_p;
4671 if (!comp_unit_maybe_decode_line_info (unit))
4672 return false;
4674 *function_ptr = NULL;
4675 func_p = lookup_address_in_function_table (unit, addr, function_ptr);
4676 if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine)
4677 unit->stash->inliner_chain = *function_ptr;
4679 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
4680 filename_ptr,
4681 linenumber_ptr,
4682 discriminator_ptr);
4683 return line_p || func_p;
4686 /* Check to see if line info is already decoded in a comp_unit.
4687 If not, decode it. Returns TRUE if no errors were encountered;
4688 FALSE otherwise. */
4690 static bool
4691 comp_unit_maybe_decode_line_info (struct comp_unit *unit)
4693 if (unit->error)
4694 return false;
4696 if (! unit->line_table)
4698 if (! unit->stmtlist)
4700 unit->error = 1;
4701 return false;
4704 unit->line_table = decode_line_info (unit);
4706 if (! unit->line_table)
4708 unit->error = 1;
4709 return false;
4712 if (unit->first_child_die_ptr < unit->end_ptr
4713 && ! scan_unit_for_symbols (unit))
4715 unit->error = 1;
4716 return false;
4720 return true;
4723 /* If UNIT contains SYM at ADDR, set the output parameters to the
4724 values for the line containing SYM. The output parameters,
4725 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
4726 filled in.
4728 Return TRUE if UNIT contains SYM, and no errors were encountered;
4729 FALSE otherwise. */
4731 static bool
4732 comp_unit_find_line (struct comp_unit *unit,
4733 asymbol *sym,
4734 bfd_vma addr,
4735 const char **filename_ptr,
4736 unsigned int *linenumber_ptr)
4738 if (!comp_unit_maybe_decode_line_info (unit))
4739 return false;
4741 if (sym->flags & BSF_FUNCTION)
4742 return lookup_symbol_in_function_table (unit, sym, addr,
4743 filename_ptr,
4744 linenumber_ptr);
4746 return lookup_symbol_in_variable_table (unit, sym, addr,
4747 filename_ptr,
4748 linenumber_ptr);
4751 /* Extract all interesting funcinfos and varinfos of a compilation
4752 unit into hash tables for faster lookup. Returns TRUE if no
4753 errors were enountered; FALSE otherwise. */
4755 static bool
4756 comp_unit_hash_info (struct dwarf2_debug *stash,
4757 struct comp_unit *unit,
4758 struct info_hash_table *funcinfo_hash_table,
4759 struct info_hash_table *varinfo_hash_table)
4761 struct funcinfo* each_func;
4762 struct varinfo* each_var;
4763 bool okay = true;
4765 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
4767 if (!comp_unit_maybe_decode_line_info (unit))
4768 return false;
4770 BFD_ASSERT (!unit->cached);
4772 /* To preserve the original search order, we went to visit the function
4773 infos in the reversed order of the list. However, making the list
4774 bi-directional use quite a bit of extra memory. So we reverse
4775 the list first, traverse the list in the now reversed order and
4776 finally reverse the list again to get back the original order. */
4777 unit->function_table = reverse_funcinfo_list (unit->function_table);
4778 for (each_func = unit->function_table;
4779 each_func && okay;
4780 each_func = each_func->prev_func)
4782 /* Skip nameless functions. */
4783 if (each_func->name)
4784 /* There is no need to copy name string into hash table as
4785 name string is either in the dwarf string buffer or
4786 info in the stash. */
4787 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
4788 (void*) each_func, false);
4790 unit->function_table = reverse_funcinfo_list (unit->function_table);
4791 if (!okay)
4792 return false;
4794 /* We do the same for variable infos. */
4795 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4796 for (each_var = unit->variable_table;
4797 each_var && okay;
4798 each_var = each_var->prev_var)
4800 /* Skip stack vars and vars with no files or names. */
4801 if (! each_var->stack
4802 && each_var->file != NULL
4803 && each_var->name != NULL)
4804 /* There is no need to copy name string into hash table as
4805 name string is either in the dwarf string buffer or
4806 info in the stash. */
4807 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
4808 (void*) each_var, false);
4811 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4812 unit->cached = true;
4813 return okay;
4816 /* Locate a section in a BFD containing debugging info. The search starts
4817 from the section after AFTER_SEC, or from the first section in the BFD if
4818 AFTER_SEC is NULL. The search works by examining the names of the
4819 sections. There are three permissiable names. The first two are given
4820 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
4821 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
4822 This is a variation on the .debug_info section which has a checksum
4823 describing the contents appended onto the name. This allows the linker to
4824 identify and discard duplicate debugging sections for different
4825 compilation units. */
4826 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
4828 static asection *
4829 find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
4830 asection *after_sec)
4832 asection *msec;
4833 const char *look;
4835 if (after_sec == NULL)
4837 look = debug_sections[debug_info].uncompressed_name;
4838 msec = bfd_get_section_by_name (abfd, look);
4839 /* Testing SEC_HAS_CONTENTS is an anti-fuzzer measure. Of
4840 course debug sections always have contents. */
4841 if (msec != NULL && (msec->flags & SEC_HAS_CONTENTS) != 0)
4842 return msec;
4844 look = debug_sections[debug_info].compressed_name;
4845 msec = bfd_get_section_by_name (abfd, look);
4846 if (msec != NULL && (msec->flags & SEC_HAS_CONTENTS) != 0)
4847 return msec;
4849 for (msec = abfd->sections; msec != NULL; msec = msec->next)
4850 if ((msec->flags & SEC_HAS_CONTENTS) != 0
4851 && startswith (msec->name, GNU_LINKONCE_INFO))
4852 return msec;
4854 return NULL;
4857 for (msec = after_sec->next; msec != NULL; msec = msec->next)
4859 if ((msec->flags & SEC_HAS_CONTENTS) == 0)
4860 continue;
4862 look = debug_sections[debug_info].uncompressed_name;
4863 if (strcmp (msec->name, look) == 0)
4864 return msec;
4866 look = debug_sections[debug_info].compressed_name;
4867 if (look != NULL && strcmp (msec->name, look) == 0)
4868 return msec;
4870 if (startswith (msec->name, GNU_LINKONCE_INFO))
4871 return msec;
4874 return NULL;
4877 /* Transfer VMAs from object file to separate debug file. */
4879 static void
4880 set_debug_vma (bfd *orig_bfd, bfd *debug_bfd)
4882 asection *s, *d;
4884 for (s = orig_bfd->sections, d = debug_bfd->sections;
4885 s != NULL && d != NULL;
4886 s = s->next, d = d->next)
4888 if ((d->flags & SEC_DEBUGGING) != 0)
4889 break;
4890 /* ??? Assumes 1-1 correspondence between sections in the
4891 two files. */
4892 if (strcmp (s->name, d->name) == 0)
4894 d->output_section = s->output_section;
4895 d->output_offset = s->output_offset;
4896 d->vma = s->vma;
4901 /* If the dwarf2 info was found in a separate debug file, return the
4902 debug file section corresponding to the section in the original file
4903 and the debug file symbols. */
4905 static void
4906 _bfd_dwarf2_stash_syms (struct dwarf2_debug *stash, bfd *abfd,
4907 asection **sec, asymbol ***syms)
4909 if (stash->f.bfd_ptr != abfd)
4911 asection *s, *d;
4913 if (*sec == NULL)
4915 *syms = stash->f.syms;
4916 return;
4919 for (s = abfd->sections, d = stash->f.bfd_ptr->sections;
4920 s != NULL && d != NULL;
4921 s = s->next, d = d->next)
4923 if ((d->flags & SEC_DEBUGGING) != 0)
4924 break;
4925 if (s == *sec
4926 && strcmp (s->name, d->name) == 0)
4928 *sec = d;
4929 *syms = stash->f.syms;
4930 break;
4936 /* Unset vmas for adjusted sections in STASH. */
4938 static void
4939 unset_sections (struct dwarf2_debug *stash)
4941 int i;
4942 struct adjusted_section *p;
4944 i = stash->adjusted_section_count;
4945 p = stash->adjusted_sections;
4946 for (; i > 0; i--, p++)
4947 p->section->vma = 0;
4950 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4951 relocatable object file. VMAs are normally all zero in relocatable
4952 object files, so if we want to distinguish locations in sections by
4953 address we need to set VMAs so the sections do not overlap. We
4954 also set VMA on .debug_info so that when we have multiple
4955 .debug_info sections (or the linkonce variant) they also do not
4956 overlap. The multiple .debug_info sections make up a single
4957 logical section. ??? We should probably do the same for other
4958 debug sections. */
4960 static bool
4961 place_sections (bfd *orig_bfd, struct dwarf2_debug *stash)
4963 bfd *abfd;
4964 struct adjusted_section *p;
4965 int i;
4966 const char *debug_info_name;
4968 if (stash->adjusted_section_count != 0)
4970 i = stash->adjusted_section_count;
4971 p = stash->adjusted_sections;
4972 for (; i > 0; i--, p++)
4973 p->section->vma = p->adj_vma;
4974 return true;
4977 debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
4978 i = 0;
4979 abfd = orig_bfd;
4980 while (1)
4982 asection *sect;
4984 for (sect = abfd->sections; sect != NULL; sect = sect->next)
4986 int is_debug_info;
4988 if ((sect->output_section != NULL
4989 && sect->output_section != sect
4990 && (sect->flags & SEC_DEBUGGING) == 0)
4991 || sect->vma != 0)
4992 continue;
4994 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
4995 || startswith (sect->name, GNU_LINKONCE_INFO));
4997 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
4998 && !is_debug_info)
4999 continue;
5001 i++;
5003 if (abfd == stash->f.bfd_ptr)
5004 break;
5005 abfd = stash->f.bfd_ptr;
5008 if (i <= 1)
5009 stash->adjusted_section_count = -1;
5010 else
5012 bfd_vma last_vma = 0, last_dwarf = 0;
5013 size_t amt = i * sizeof (struct adjusted_section);
5015 p = (struct adjusted_section *) bfd_malloc (amt);
5016 if (p == NULL)
5017 return false;
5019 stash->adjusted_sections = p;
5020 stash->adjusted_section_count = i;
5022 abfd = orig_bfd;
5023 while (1)
5025 asection *sect;
5027 for (sect = abfd->sections; sect != NULL; sect = sect->next)
5029 bfd_size_type sz;
5030 int is_debug_info;
5032 if ((sect->output_section != NULL
5033 && sect->output_section != sect
5034 && (sect->flags & SEC_DEBUGGING) == 0)
5035 || sect->vma != 0)
5036 continue;
5038 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
5039 || startswith (sect->name, GNU_LINKONCE_INFO));
5041 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
5042 && !is_debug_info)
5043 continue;
5045 sz = sect->rawsize ? sect->rawsize : sect->size;
5047 if (is_debug_info)
5049 BFD_ASSERT (sect->alignment_power == 0);
5050 sect->vma = last_dwarf;
5051 last_dwarf += sz;
5053 else
5055 /* Align the new address to the current section
5056 alignment. */
5057 last_vma = ((last_vma
5058 + ~(-((bfd_vma) 1 << sect->alignment_power)))
5059 & (-((bfd_vma) 1 << sect->alignment_power)));
5060 sect->vma = last_vma;
5061 last_vma += sz;
5064 p->section = sect;
5065 p->adj_vma = sect->vma;
5066 p++;
5068 if (abfd == stash->f.bfd_ptr)
5069 break;
5070 abfd = stash->f.bfd_ptr;
5074 if (orig_bfd != stash->f.bfd_ptr)
5075 set_debug_vma (orig_bfd, stash->f.bfd_ptr);
5077 return true;
5080 /* Look up a funcinfo by name using the given info hash table. If found,
5081 also update the locations pointed to by filename_ptr and linenumber_ptr.
5083 This function returns TRUE if a funcinfo that matches the given symbol
5084 and address is found with any error; otherwise it returns FALSE. */
5086 static bool
5087 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
5088 asymbol *sym,
5089 bfd_vma addr,
5090 const char **filename_ptr,
5091 unsigned int *linenumber_ptr)
5093 struct funcinfo* each_func;
5094 struct funcinfo* best_fit = NULL;
5095 bfd_vma best_fit_len = (bfd_vma) -1;
5096 struct info_list_node *node;
5097 struct arange *arange;
5098 const char *name = bfd_asymbol_name (sym);
5100 for (node = lookup_info_hash_table (hash_table, name);
5101 node;
5102 node = node->next)
5104 each_func = (struct funcinfo *) node->info;
5105 for (arange = &each_func->arange;
5106 arange;
5107 arange = arange->next)
5109 if (addr >= arange->low
5110 && addr < arange->high
5111 && arange->high - arange->low < best_fit_len)
5113 best_fit = each_func;
5114 best_fit_len = arange->high - arange->low;
5119 if (best_fit)
5121 *filename_ptr = best_fit->file;
5122 *linenumber_ptr = best_fit->line;
5123 return true;
5126 return false;
5129 /* Look up a varinfo by name using the given info hash table. If found,
5130 also update the locations pointed to by filename_ptr and linenumber_ptr.
5132 This function returns TRUE if a varinfo that matches the given symbol
5133 and address is found with any error; otherwise it returns FALSE. */
5135 static bool
5136 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
5137 asymbol *sym,
5138 bfd_vma addr,
5139 const char **filename_ptr,
5140 unsigned int *linenumber_ptr)
5142 struct varinfo* each;
5143 struct info_list_node *node;
5144 const char *name = bfd_asymbol_name (sym);
5146 for (node = lookup_info_hash_table (hash_table, name);
5147 node;
5148 node = node->next)
5150 each = (struct varinfo *) node->info;
5151 if (each->addr == addr)
5153 *filename_ptr = each->file;
5154 *linenumber_ptr = each->line;
5155 return true;
5159 return false;
5162 /* Update the funcinfo and varinfo info hash tables if they are
5163 not up to date. Returns TRUE if there is no error; otherwise
5164 returns FALSE and disable the info hash tables. */
5166 static bool
5167 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
5169 struct comp_unit *each;
5171 /* Exit if hash tables are up-to-date. */
5172 if (stash->f.all_comp_units == stash->hash_units_head)
5173 return true;
5175 if (stash->hash_units_head)
5176 each = stash->hash_units_head->prev_unit;
5177 else
5178 each = stash->f.last_comp_unit;
5180 while (each)
5182 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
5183 stash->varinfo_hash_table))
5185 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
5186 return false;
5188 each = each->prev_unit;
5191 stash->hash_units_head = stash->f.all_comp_units;
5192 return true;
5195 /* Check consistency of info hash tables. This is for debugging only. */
5197 static void ATTRIBUTE_UNUSED
5198 stash_verify_info_hash_table (struct dwarf2_debug *stash)
5200 struct comp_unit *each_unit;
5201 struct funcinfo *each_func;
5202 struct varinfo *each_var;
5203 struct info_list_node *node;
5204 bool found;
5206 for (each_unit = stash->f.all_comp_units;
5207 each_unit;
5208 each_unit = each_unit->next_unit)
5210 for (each_func = each_unit->function_table;
5211 each_func;
5212 each_func = each_func->prev_func)
5214 if (!each_func->name)
5215 continue;
5216 node = lookup_info_hash_table (stash->funcinfo_hash_table,
5217 each_func->name);
5218 BFD_ASSERT (node);
5219 found = false;
5220 while (node && !found)
5222 found = node->info == each_func;
5223 node = node->next;
5225 BFD_ASSERT (found);
5228 for (each_var = each_unit->variable_table;
5229 each_var;
5230 each_var = each_var->prev_var)
5232 if (!each_var->name || !each_var->file || each_var->stack)
5233 continue;
5234 node = lookup_info_hash_table (stash->varinfo_hash_table,
5235 each_var->name);
5236 BFD_ASSERT (node);
5237 found = false;
5238 while (node && !found)
5240 found = node->info == each_var;
5241 node = node->next;
5243 BFD_ASSERT (found);
5248 /* Check to see if we want to enable the info hash tables, which consume
5249 quite a bit of memory. Currently we only check the number times
5250 bfd_dwarf2_find_line is called. In the future, we may also want to
5251 take the number of symbols into account. */
5253 static void
5254 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
5256 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
5258 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
5259 return;
5261 /* FIXME: Maybe we should check the reduce_memory_overheads
5262 and optimize fields in the bfd_link_info structure ? */
5264 /* Create hash tables. */
5265 stash->funcinfo_hash_table = create_info_hash_table (abfd);
5266 stash->varinfo_hash_table = create_info_hash_table (abfd);
5267 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
5269 /* Turn off info hashes if any allocation above fails. */
5270 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
5271 return;
5273 /* We need a forced update so that the info hash tables will
5274 be created even though there is no compilation unit. That
5275 happens if STASH_INFO_HASH_TRIGGER is 0. */
5276 if (stash_maybe_update_info_hash_tables (stash))
5277 stash->info_hash_status = STASH_INFO_HASH_ON;
5280 /* Find the file and line associated with a symbol and address using the
5281 info hash tables of a stash. If there is a match, the function returns
5282 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
5283 otherwise it returns FALSE. */
5285 static bool
5286 stash_find_line_fast (struct dwarf2_debug *stash,
5287 asymbol *sym,
5288 bfd_vma addr,
5289 const char **filename_ptr,
5290 unsigned int *linenumber_ptr)
5292 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
5294 if (sym->flags & BSF_FUNCTION)
5295 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
5296 filename_ptr, linenumber_ptr);
5297 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
5298 filename_ptr, linenumber_ptr);
5301 /* Save current section VMAs. */
5303 static bool
5304 save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
5306 asection *s;
5307 unsigned int i;
5309 if (abfd->section_count == 0)
5310 return true;
5311 stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count);
5312 if (stash->sec_vma == NULL)
5313 return false;
5314 stash->sec_vma_count = abfd->section_count;
5315 for (i = 0, s = abfd->sections;
5316 s != NULL && i < abfd->section_count;
5317 i++, s = s->next)
5319 if (s->output_section != NULL)
5320 stash->sec_vma[i] = s->output_section->vma + s->output_offset;
5321 else
5322 stash->sec_vma[i] = s->vma;
5324 return true;
5327 /* Compare current section VMAs against those at the time the stash
5328 was created. If find_nearest_line is used in linker warnings or
5329 errors early in the link process, the debug info stash will be
5330 invalid for later calls. This is because we relocate debug info
5331 sections, so the stashed section contents depend on symbol values,
5332 which in turn depend on section VMAs. */
5334 static bool
5335 section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash)
5337 asection *s;
5338 unsigned int i;
5340 /* PR 24334: If the number of sections in ABFD has changed between
5341 when the stash was created and now, then we cannot trust the
5342 stashed vma information. */
5343 if (abfd->section_count != stash->sec_vma_count)
5344 return false;
5346 for (i = 0, s = abfd->sections;
5347 s != NULL && i < abfd->section_count;
5348 i++, s = s->next)
5350 bfd_vma vma;
5352 if (s->output_section != NULL)
5353 vma = s->output_section->vma + s->output_offset;
5354 else
5355 vma = s->vma;
5356 if (vma != stash->sec_vma[i])
5357 return false;
5359 return true;
5362 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
5363 If DEBUG_BFD is not specified, we read debug information from ABFD
5364 or its gnu_debuglink. The results will be stored in PINFO.
5365 The function returns TRUE iff debug information is ready. */
5367 bool
5368 _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
5369 const struct dwarf_debug_section *debug_sections,
5370 asymbol **symbols,
5371 void **pinfo,
5372 bool do_place)
5374 size_t amt = sizeof (struct dwarf2_debug);
5375 bfd_size_type total_size;
5376 asection *msec;
5377 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
5379 if (stash != NULL)
5381 if (stash->orig_bfd == abfd
5382 && section_vma_same (abfd, stash))
5384 /* Check that we did previously find some debug information
5385 before attempting to make use of it. */
5386 if (stash->f.bfd_ptr != NULL)
5388 if (do_place && !place_sections (abfd, stash))
5389 return false;
5390 return true;
5393 return false;
5395 _bfd_dwarf2_cleanup_debug_info (abfd, pinfo);
5396 memset (stash, 0, amt);
5398 else
5400 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
5401 if (! stash)
5402 return false;
5404 stash->orig_bfd = abfd;
5405 stash->debug_sections = debug_sections;
5406 stash->f.syms = symbols;
5407 if (!save_section_vma (abfd, stash))
5408 return false;
5410 stash->f.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
5411 del_abbrev, calloc, free);
5412 if (!stash->f.abbrev_offsets)
5413 return false;
5415 stash->alt.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
5416 del_abbrev, calloc, free);
5417 if (!stash->alt.abbrev_offsets)
5418 return false;
5420 stash->f.trie_root = alloc_trie_leaf (abfd);
5421 if (!stash->f.trie_root)
5422 return false;
5424 stash->alt.trie_root = alloc_trie_leaf (abfd);
5425 if (!stash->alt.trie_root)
5426 return false;
5428 *pinfo = stash;
5430 if (debug_bfd == NULL)
5431 debug_bfd = abfd;
5433 msec = find_debug_info (debug_bfd, debug_sections, NULL);
5434 if (msec == NULL && abfd == debug_bfd)
5436 char * debug_filename;
5438 debug_filename = bfd_follow_build_id_debuglink (abfd, DEBUGDIR);
5439 if (debug_filename == NULL)
5440 debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
5442 if (debug_filename == NULL)
5443 /* No dwarf2 info, and no gnu_debuglink to follow.
5444 Note that at this point the stash has been allocated, but
5445 contains zeros. This lets future calls to this function
5446 fail more quickly. */
5447 return false;
5449 debug_bfd = bfd_openr (debug_filename, NULL);
5450 free (debug_filename);
5451 if (debug_bfd == NULL)
5452 /* FIXME: Should we report our failure to follow the debuglink ? */
5453 return false;
5455 /* Set BFD_DECOMPRESS to decompress debug sections. */
5456 debug_bfd->flags |= BFD_DECOMPRESS;
5457 if (!bfd_check_format (debug_bfd, bfd_object)
5458 || (msec = find_debug_info (debug_bfd,
5459 debug_sections, NULL)) == NULL
5460 || !bfd_generic_link_read_symbols (debug_bfd))
5462 bfd_close (debug_bfd);
5463 return false;
5466 symbols = bfd_get_outsymbols (debug_bfd);
5467 stash->f.syms = symbols;
5468 stash->close_on_cleanup = true;
5470 stash->f.bfd_ptr = debug_bfd;
5472 if (do_place
5473 && !place_sections (abfd, stash))
5474 return false;
5476 /* There can be more than one DWARF2 info section in a BFD these
5477 days. First handle the easy case when there's only one. If
5478 there's more than one, try case two: none of the sections is
5479 compressed. In that case, read them all in and produce one
5480 large stash. We do this in two passes - in the first pass we
5481 just accumulate the section sizes, and in the second pass we
5482 read in the section's contents. (The allows us to avoid
5483 reallocing the data as we add sections to the stash.) If
5484 some or all sections are compressed, then do things the slow
5485 way, with a bunch of reallocs. */
5487 if (! find_debug_info (debug_bfd, debug_sections, msec))
5489 /* Case 1: only one info section. */
5490 total_size = msec->size;
5491 if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
5492 symbols, 0,
5493 &stash->f.dwarf_info_buffer, &total_size))
5494 return false;
5496 else
5498 /* Case 2: multiple sections. */
5499 for (total_size = 0;
5500 msec;
5501 msec = find_debug_info (debug_bfd, debug_sections, msec))
5503 if (_bfd_section_size_insane (debug_bfd, msec))
5504 return false;
5505 /* Catch PR25070 testcase overflowing size calculation here. */
5506 if (total_size + msec->size < total_size)
5508 bfd_set_error (bfd_error_no_memory);
5509 return false;
5511 total_size += msec->size;
5514 stash->f.dwarf_info_buffer = (bfd_byte *) bfd_malloc (total_size);
5515 if (stash->f.dwarf_info_buffer == NULL)
5516 return false;
5518 total_size = 0;
5519 for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
5520 msec;
5521 msec = find_debug_info (debug_bfd, debug_sections, msec))
5523 bfd_size_type size;
5525 size = msec->size;
5526 if (size == 0)
5527 continue;
5529 if (!(bfd_simple_get_relocated_section_contents
5530 (debug_bfd, msec, stash->f.dwarf_info_buffer + total_size,
5531 symbols)))
5532 return false;
5534 total_size += size;
5538 stash->f.info_ptr = stash->f.dwarf_info_buffer;
5539 stash->f.dwarf_info_size = total_size;
5540 return true;
5543 /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
5545 static struct comp_unit *
5546 stash_comp_unit (struct dwarf2_debug *stash, struct dwarf2_debug_file *file)
5548 bfd_size_type length;
5549 unsigned int offset_size;
5550 bfd_byte *info_ptr_unit = file->info_ptr;
5551 bfd_byte *info_ptr_end = file->dwarf_info_buffer + file->dwarf_info_size;
5553 if (file->info_ptr >= info_ptr_end)
5554 return NULL;
5556 length = read_4_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5557 /* A 0xffffff length is the DWARF3 way of indicating
5558 we use 64-bit offsets, instead of 32-bit offsets. */
5559 if (length == 0xffffffff)
5561 offset_size = 8;
5562 length = read_8_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5564 /* A zero length is the IRIX way of indicating 64-bit offsets,
5565 mostly because the 64-bit length will generally fit in 32
5566 bits, and the endianness helps. */
5567 else if (length == 0)
5569 offset_size = 8;
5570 length = read_4_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5572 /* In the absence of the hints above, we assume 32-bit DWARF2
5573 offsets even for targets with 64-bit addresses, because:
5574 a) most of the time these targets will not have generated
5575 more than 2Gb of debug info and so will not need 64-bit
5576 offsets,
5578 b) if they do use 64-bit offsets but they are not using
5579 the size hints that are tested for above then they are
5580 not conforming to the DWARF3 standard anyway. */
5581 else
5582 offset_size = 4;
5584 if (length != 0
5585 && length <= (size_t) (info_ptr_end - file->info_ptr))
5587 struct comp_unit *each = parse_comp_unit (stash, file,
5588 file->info_ptr, length,
5589 info_ptr_unit, offset_size);
5590 if (each)
5592 if (file->comp_unit_tree == NULL)
5593 file->comp_unit_tree
5594 = splay_tree_new (splay_tree_compare_addr_range,
5595 splay_tree_free_addr_range, NULL);
5597 struct addr_range *r
5598 = (struct addr_range *)bfd_malloc (sizeof (struct addr_range));
5599 r->start = each->info_ptr_unit;
5600 r->end = each->end_ptr;
5601 splay_tree_node v = splay_tree_lookup (file->comp_unit_tree,
5602 (splay_tree_key)r);
5603 if (v != NULL || r->end <= r->start)
5604 abort ();
5605 splay_tree_insert (file->comp_unit_tree, (splay_tree_key)r,
5606 (splay_tree_value)each);
5608 if (file->all_comp_units)
5609 file->all_comp_units->prev_unit = each;
5610 else
5611 file->last_comp_unit = each;
5613 each->next_unit = file->all_comp_units;
5614 file->all_comp_units = each;
5616 if (each->arange.high == 0)
5618 each->next_unit_without_ranges = file->all_comp_units_without_ranges;
5619 file->all_comp_units_without_ranges = each->next_unit_without_ranges;
5622 file->info_ptr += length;
5623 return each;
5627 /* Don't trust any of the DWARF info after a corrupted length or
5628 parse error. */
5629 file->info_ptr = info_ptr_end;
5630 return NULL;
5633 /* Hash function for an asymbol. */
5635 static hashval_t
5636 hash_asymbol (const void *sym)
5638 const asymbol *asym = sym;
5639 return htab_hash_string (asym->name);
5642 /* Equality function for asymbols. */
5644 static int
5645 eq_asymbol (const void *a, const void *b)
5647 const asymbol *sa = a;
5648 const asymbol *sb = b;
5649 return strcmp (sa->name, sb->name) == 0;
5652 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
5653 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
5654 symbol in SYMBOLS and return the difference between the low_pc and
5655 the symbol's address. Returns 0 if no suitable symbol could be found. */
5657 bfd_signed_vma
5658 _bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo)
5660 struct dwarf2_debug *stash;
5661 struct comp_unit * unit;
5662 htab_t sym_hash;
5663 bfd_signed_vma result = 0;
5664 asymbol ** psym;
5666 stash = (struct dwarf2_debug *) *pinfo;
5668 if (stash == NULL || symbols == NULL)
5669 return 0;
5671 sym_hash = htab_create_alloc (10, hash_asymbol, eq_asymbol,
5672 NULL, xcalloc, free);
5673 for (psym = symbols; * psym != NULL; psym++)
5675 asymbol * sym = * psym;
5677 if (sym->flags & BSF_FUNCTION && sym->section != NULL)
5679 void **slot = htab_find_slot (sym_hash, sym, INSERT);
5680 *slot = sym;
5684 for (unit = stash->f.all_comp_units; unit; unit = unit->next_unit)
5686 struct funcinfo * func;
5688 comp_unit_maybe_decode_line_info (unit);
5690 for (func = unit->function_table; func != NULL; func = func->prev_func)
5691 if (func->name && func->arange.low)
5693 asymbol search, *sym;
5695 /* FIXME: Do we need to scan the aranges looking for the
5696 lowest pc value? */
5698 search.name = func->name;
5699 sym = htab_find (sym_hash, &search);
5700 if (sym != NULL)
5702 result = func->arange.low - (sym->value + sym->section->vma);
5703 goto done;
5708 done:
5709 htab_delete (sym_hash);
5710 return result;
5713 /* See _bfd_dwarf2_find_nearest_line_with_alt. */
5716 _bfd_dwarf2_find_nearest_line (bfd *abfd,
5717 asymbol **symbols,
5718 asymbol *symbol,
5719 asection *section,
5720 bfd_vma offset,
5721 const char **filename_ptr,
5722 const char **functionname_ptr,
5723 unsigned int *linenumber_ptr,
5724 unsigned int *discriminator_ptr,
5725 const struct dwarf_debug_section *debug_sections,
5726 void **pinfo)
5728 return _bfd_dwarf2_find_nearest_line_with_alt
5729 (abfd, NULL, symbols, symbol, section, offset, filename_ptr,
5730 functionname_ptr, linenumber_ptr, discriminator_ptr, debug_sections,
5731 pinfo);
5734 /* Find the source code location of SYMBOL. If SYMBOL is NULL
5735 then find the nearest source code location corresponding to
5736 the address SECTION + OFFSET.
5737 Returns 1 if the line is found without error and fills in
5738 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
5739 NULL the FUNCTIONNAME_PTR is also filled in.
5740 Returns 2 if partial information from _bfd_elf_find_function is
5741 returned (function and maybe file) by looking at symbols. DWARF2
5742 info is present but not regarding the requested code location.
5743 Returns 0 otherwise.
5744 SYMBOLS contains the symbol table for ABFD.
5745 DEBUG_SECTIONS contains the name of the dwarf debug sections.
5746 If ALT_FILENAME is given, attempt to open the file and use it
5747 as the .gnu_debugaltlink file. Otherwise this file will be
5748 searched for when needed. */
5751 _bfd_dwarf2_find_nearest_line_with_alt
5752 (bfd *abfd,
5753 const char *alt_filename,
5754 asymbol **symbols,
5755 asymbol *symbol,
5756 asection *section,
5757 bfd_vma offset,
5758 const char **filename_ptr,
5759 const char **functionname_ptr,
5760 unsigned int *linenumber_ptr,
5761 unsigned int *discriminator_ptr,
5762 const struct dwarf_debug_section *debug_sections,
5763 void **pinfo)
5765 /* Read each compilation unit from the section .debug_info, and check
5766 to see if it contains the address we are searching for. If yes,
5767 lookup the address, and return the line number info. If no, go
5768 on to the next compilation unit.
5770 We keep a list of all the previously read compilation units, and
5771 a pointer to the next un-read compilation unit. Check the
5772 previously read units before reading more. */
5773 struct dwarf2_debug *stash;
5774 /* What address are we looking for? */
5775 bfd_vma addr;
5776 struct comp_unit* each;
5777 struct funcinfo *function = NULL;
5778 int found = false;
5779 bool do_line;
5781 *filename_ptr = NULL;
5782 if (functionname_ptr != NULL)
5783 *functionname_ptr = NULL;
5784 *linenumber_ptr = 0;
5785 if (discriminator_ptr)
5786 *discriminator_ptr = 0;
5788 if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections,
5789 symbols, pinfo,
5790 (abfd->flags & (EXEC_P | DYNAMIC)) == 0))
5791 return false;
5793 stash = (struct dwarf2_debug *) *pinfo;
5795 if (stash->alt.bfd_ptr == NULL && alt_filename != NULL)
5797 bfd *alt_bfd = bfd_openr (alt_filename, NULL);
5799 if (alt_bfd == NULL)
5800 /* bfd_openr will have set the bfd_error. */
5801 return false;
5802 if (!bfd_check_format (alt_bfd, bfd_object))
5804 bfd_set_error (bfd_error_wrong_format);
5805 bfd_close (alt_bfd);
5806 return false;
5809 stash->alt.bfd_ptr = alt_bfd;
5812 do_line = symbol != NULL;
5813 if (do_line)
5815 BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL);
5816 section = bfd_asymbol_section (symbol);
5817 addr = symbol->value;
5819 else
5821 BFD_ASSERT (section != NULL && functionname_ptr != NULL);
5822 addr = offset;
5824 /* If we have no SYMBOL but the section we're looking at is not a
5825 code section, then take a look through the list of symbols to see
5826 if we have a symbol at the address we're looking for. If we do
5827 then use this to look up line information. This will allow us to
5828 give file and line results for data symbols. We exclude code
5829 symbols here, if we look up a function symbol and then look up the
5830 line information we'll actually return the line number for the
5831 opening '{' rather than the function definition line. This is
5832 because looking up by symbol uses the line table, in which the
5833 first line for a function is usually the opening '{', while
5834 looking up the function by section + offset uses the
5835 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
5836 which will be the line of the function name. */
5837 if (symbols != NULL && (section->flags & SEC_CODE) == 0)
5839 asymbol **tmp;
5841 for (tmp = symbols; (*tmp) != NULL; ++tmp)
5842 if ((*tmp)->the_bfd == abfd
5843 && (*tmp)->section == section
5844 && (*tmp)->value == offset
5845 && ((*tmp)->flags & BSF_SECTION_SYM) == 0)
5847 symbol = *tmp;
5848 do_line = true;
5849 /* For local symbols, keep going in the hope we find a
5850 global. */
5851 if ((symbol->flags & BSF_GLOBAL) != 0)
5852 break;
5857 if (section->output_section)
5858 addr += section->output_section->vma + section->output_offset;
5859 else
5860 addr += section->vma;
5862 /* A null info_ptr indicates that there is no dwarf2 info
5863 (or that an error occured while setting up the stash). */
5864 if (! stash->f.info_ptr)
5865 return false;
5867 stash->inliner_chain = NULL;
5869 /* Check the previously read comp. units first. */
5870 if (do_line)
5872 /* The info hash tables use quite a bit of memory. We may not want to
5873 always use them. We use some heuristics to decide if and when to
5874 turn it on. */
5875 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
5876 stash_maybe_enable_info_hash_tables (abfd, stash);
5878 /* Keep info hash table up to date if they are available. Note that we
5879 may disable the hash tables if there is any error duing update. */
5880 if (stash->info_hash_status == STASH_INFO_HASH_ON)
5881 stash_maybe_update_info_hash_tables (stash);
5883 if (stash->info_hash_status == STASH_INFO_HASH_ON)
5885 found = stash_find_line_fast (stash, symbol, addr,
5886 filename_ptr, linenumber_ptr);
5887 if (found)
5888 goto done;
5891 /* Check the previously read comp. units first. */
5892 for (each = stash->f.all_comp_units; each; each = each->next_unit)
5893 if ((symbol->flags & BSF_FUNCTION) == 0
5894 || each->arange.high == 0
5895 || comp_unit_contains_address (each, addr))
5897 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
5898 linenumber_ptr);
5899 if (found)
5900 goto done;
5903 else
5905 struct trie_node *trie = stash->f.trie_root;
5906 unsigned int bits = VMA_BITS - 8;
5907 struct comp_unit **prev_each;
5909 /* Traverse interior nodes until we get to a leaf. */
5910 while (trie && trie->num_room_in_leaf == 0)
5912 int ch = (addr >> bits) & 0xff;
5913 trie = ((struct trie_interior *) trie)->children[ch];
5914 bits -= 8;
5917 if (trie)
5919 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
5920 unsigned int i;
5922 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
5923 leaf->ranges[i].unit->mark = false;
5925 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
5927 struct comp_unit *unit = leaf->ranges[i].unit;
5928 if (unit->mark
5929 || addr < leaf->ranges[i].low_pc
5930 || addr >= leaf->ranges[i].high_pc)
5931 continue;
5932 unit->mark = true;
5934 found = comp_unit_find_nearest_line (unit, addr,
5935 filename_ptr,
5936 &function,
5937 linenumber_ptr,
5938 discriminator_ptr);
5939 if (found)
5940 goto done;
5944 /* Also scan through all compilation units without any ranges,
5945 taking them out of the list if they have acquired any since
5946 last time. */
5947 prev_each = &stash->f.all_comp_units_without_ranges;
5948 for (each = *prev_each; each; each = each->next_unit_without_ranges)
5950 if (each->arange.high != 0)
5952 *prev_each = each->next_unit_without_ranges;
5953 continue;
5956 found = comp_unit_find_nearest_line (each, addr,
5957 filename_ptr,
5958 &function,
5959 linenumber_ptr,
5960 discriminator_ptr);
5961 if (found)
5962 goto done;
5963 prev_each = &each->next_unit_without_ranges;
5967 /* Read each remaining comp. units checking each as they are read. */
5968 while ((each = stash_comp_unit (stash, &stash->f)) != NULL)
5970 /* DW_AT_low_pc and DW_AT_high_pc are optional for
5971 compilation units. If we don't have them (i.e.,
5972 unit->high == 0), we need to consult the line info table
5973 to see if a compilation unit contains the given
5974 address. */
5975 if (do_line)
5976 found = (((symbol->flags & BSF_FUNCTION) == 0
5977 || each->arange.high == 0
5978 || comp_unit_contains_address (each, addr))
5979 && comp_unit_find_line (each, symbol, addr,
5980 filename_ptr, linenumber_ptr));
5981 else
5982 found = ((each->arange.high == 0
5983 || comp_unit_contains_address (each, addr))
5984 && comp_unit_find_nearest_line (each, addr,
5985 filename_ptr,
5986 &function,
5987 linenumber_ptr,
5988 discriminator_ptr));
5990 if (found)
5991 break;
5994 done:
5995 if (functionname_ptr && function && function->is_linkage)
5997 *functionname_ptr = function->name;
5998 if (!found)
5999 found = 2;
6001 else if (functionname_ptr
6002 && (!*functionname_ptr
6003 || (function && !function->is_linkage)))
6005 asymbol *fun;
6006 asymbol **syms = symbols;
6007 asection *sec = section;
6009 _bfd_dwarf2_stash_syms (stash, abfd, &sec, &syms);
6010 fun = _bfd_elf_find_function (abfd, syms, sec, offset,
6011 *filename_ptr ? NULL : filename_ptr,
6012 functionname_ptr);
6014 if (!found && fun != NULL)
6015 found = 2;
6017 if (function && !function->is_linkage)
6019 bfd_vma sec_vma;
6021 sec_vma = section->vma;
6022 if (section->output_section != NULL)
6023 sec_vma = section->output_section->vma + section->output_offset;
6024 if (fun == NULL)
6025 *functionname_ptr = function->name;
6026 else if (fun->value + sec_vma == function->arange.low)
6027 function->name = *functionname_ptr;
6028 /* Even if we didn't find a linkage name, say that we have
6029 to stop a repeated search of symbols. */
6030 function->is_linkage = true;
6034 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
6035 unset_sections (stash);
6037 return found;
6040 bool
6041 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
6042 const char **filename_ptr,
6043 const char **functionname_ptr,
6044 unsigned int *linenumber_ptr,
6045 void **pinfo)
6047 struct dwarf2_debug *stash;
6049 stash = (struct dwarf2_debug *) *pinfo;
6050 if (stash)
6052 struct funcinfo *func = stash->inliner_chain;
6054 if (func && func->caller_func)
6056 *filename_ptr = func->caller_file;
6057 *functionname_ptr = func->caller_func->name;
6058 *linenumber_ptr = func->caller_line;
6059 stash->inliner_chain = func->caller_func;
6060 return true;
6064 return false;
6067 void
6068 _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
6070 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
6071 struct comp_unit *each;
6072 struct dwarf2_debug_file *file;
6074 if (abfd == NULL || stash == NULL)
6075 return;
6077 if (stash->varinfo_hash_table)
6078 bfd_hash_table_free (&stash->varinfo_hash_table->base);
6079 if (stash->funcinfo_hash_table)
6080 bfd_hash_table_free (&stash->funcinfo_hash_table->base);
6082 file = &stash->f;
6083 while (1)
6085 for (each = file->all_comp_units; each; each = each->next_unit)
6087 struct funcinfo *function_table = each->function_table;
6088 struct varinfo *variable_table = each->variable_table;
6090 if (each->line_table && each->line_table != file->line_table)
6092 free (each->line_table->files);
6093 free (each->line_table->dirs);
6096 free (each->lookup_funcinfo_table);
6097 each->lookup_funcinfo_table = NULL;
6099 while (function_table)
6101 free (function_table->file);
6102 function_table->file = NULL;
6103 free (function_table->caller_file);
6104 function_table->caller_file = NULL;
6105 function_table = function_table->prev_func;
6108 while (variable_table)
6110 free (variable_table->file);
6111 variable_table->file = NULL;
6112 variable_table = variable_table->prev_var;
6116 if (file->line_table)
6118 free (file->line_table->files);
6119 free (file->line_table->dirs);
6121 htab_delete (file->abbrev_offsets);
6122 if (file->comp_unit_tree != NULL)
6123 splay_tree_delete (file->comp_unit_tree);
6125 free (file->dwarf_line_str_buffer);
6126 free (file->dwarf_str_buffer);
6127 free (file->dwarf_ranges_buffer);
6128 free (file->dwarf_line_buffer);
6129 free (file->dwarf_abbrev_buffer);
6130 free (file->dwarf_info_buffer);
6131 if (file == &stash->alt)
6132 break;
6133 file = &stash->alt;
6135 free (stash->sec_vma);
6136 free (stash->adjusted_sections);
6137 if (stash->close_on_cleanup)
6138 bfd_close (stash->f.bfd_ptr);
6139 if (stash->alt.bfd_ptr)
6140 bfd_close (stash->alt.bfd_ptr);
6143 /* Find the function to a particular section and offset,
6144 for error reporting. */
6146 asymbol *
6147 _bfd_elf_find_function (bfd *abfd,
6148 asymbol **symbols,
6149 asection *section,
6150 bfd_vma offset,
6151 const char **filename_ptr,
6152 const char **functionname_ptr)
6154 struct elf_find_function_cache
6156 asection *last_section;
6157 asymbol *func;
6158 const char *filename;
6159 bfd_size_type func_size;
6160 } *cache;
6162 if (symbols == NULL)
6163 return NULL;
6165 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6166 return NULL;
6168 cache = elf_tdata (abfd)->elf_find_function_cache;
6169 if (cache == NULL)
6171 cache = bfd_zalloc (abfd, sizeof (*cache));
6172 elf_tdata (abfd)->elf_find_function_cache = cache;
6173 if (cache == NULL)
6174 return NULL;
6176 if (cache->last_section != section
6177 || cache->func == NULL
6178 || offset < cache->func->value
6179 || offset >= cache->func->value + cache->func_size)
6181 asymbol *file;
6182 bfd_vma low_func;
6183 asymbol **p;
6184 /* ??? Given multiple file symbols, it is impossible to reliably
6185 choose the right file name for global symbols. File symbols are
6186 local symbols, and thus all file symbols must sort before any
6187 global symbols. The ELF spec may be interpreted to say that a
6188 file symbol must sort before other local symbols, but currently
6189 ld -r doesn't do this. So, for ld -r output, it is possible to
6190 make a better choice of file name for local symbols by ignoring
6191 file symbols appearing after a given local symbol. */
6192 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
6193 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6195 file = NULL;
6196 low_func = 0;
6197 state = nothing_seen;
6198 cache->filename = NULL;
6199 cache->func = NULL;
6200 cache->func_size = 0;
6201 cache->last_section = section;
6203 for (p = symbols; *p != NULL; p++)
6205 asymbol *sym = *p;
6206 bfd_vma code_off;
6207 bfd_size_type size;
6209 if ((sym->flags & BSF_FILE) != 0)
6211 file = sym;
6212 if (state == symbol_seen)
6213 state = file_after_symbol_seen;
6214 continue;
6217 size = bed->maybe_function_sym (sym, section, &code_off);
6218 if (size != 0
6219 && code_off <= offset
6220 && (code_off > low_func
6221 || (code_off == low_func
6222 && size > cache->func_size)))
6224 cache->func = sym;
6225 cache->func_size = size;
6226 cache->filename = NULL;
6227 low_func = code_off;
6228 if (file != NULL
6229 && ((sym->flags & BSF_LOCAL) != 0
6230 || state != file_after_symbol_seen))
6231 cache->filename = bfd_asymbol_name (file);
6233 if (state == nothing_seen)
6234 state = symbol_seen;
6238 if (cache->func == NULL)
6239 return NULL;
6241 if (filename_ptr)
6242 *filename_ptr = cache->filename;
6243 if (functionname_ptr)
6244 *functionname_ptr = bfd_asymbol_name (cache->func);
6246 return cache->func;