Fix another illegal memory access triggered by corrupt ELF input files.
[binutils-gdb.git] / bfd / dwarf2.c
blobbc17347fbf854a2c658b59b132b2d5f440f05842
1 /* DWARF 2 support.
2 Copyright (C) 1994-2025 Free Software Foundation, Inc.
4 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
5 (gavin@cygnus.com).
7 From the dwarf2read.c header:
8 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
9 Inc. with support from Florida State University (under contract
10 with the Ada Joint Program Office), and Silicon Graphics, Inc.
11 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
12 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
13 support in dwarfread.c
15 This file is part of BFD.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 3 of the License, or (at
20 your option) any later version.
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
30 MA 02110-1301, USA. */
32 #include "sysdep.h"
33 #include "bfd.h"
34 #include "libiberty.h"
35 #include "demangle.h"
36 #include "libbfd.h"
37 #include "elf-bfd.h"
38 #include "dwarf2.h"
39 #include "hashtab.h"
40 #include "splay-tree.h"
42 /* The data in the .debug_line statement prologue looks like this. */
44 struct line_head
46 bfd_vma total_length;
47 unsigned short version;
48 bfd_vma prologue_length;
49 unsigned char minimum_instruction_length;
50 unsigned char maximum_ops_per_insn;
51 unsigned char default_is_stmt;
52 int line_base;
53 unsigned char line_range;
54 unsigned char opcode_base;
55 unsigned char *standard_opcode_lengths;
58 /* Attributes have a name and a value. */
60 struct attribute
62 enum dwarf_attribute name;
63 enum dwarf_form form;
64 union
66 char *str;
67 struct dwarf_block *blk;
68 uint64_t val;
69 int64_t sval;
74 /* Blocks are a bunch of untyped bytes. */
75 struct dwarf_block
77 unsigned int size;
78 bfd_byte *data;
81 struct adjusted_section
83 asection *section;
84 bfd_vma adj_vma;
85 bfd_vma orig_vma;
88 /* A trie to map quickly from address range to compilation unit.
90 This is a fairly standard radix-256 trie, used to quickly locate which
91 compilation unit any given address belongs to. Given that each compilation
92 unit may register hundreds of very small and unaligned ranges (which may
93 potentially overlap, due to inlining and other concerns), and a large
94 program may end up containing hundreds of thousands of such ranges, we cannot
95 scan through them linearly without undue slowdown.
97 We use a hybrid trie to avoid memory explosion: There are two types of trie
98 nodes, leaves and interior nodes. (Almost all nodes are leaves, so they
99 take up the bulk of the memory usage.) Leaves contain a simple array of
100 ranges (high/low address) and which compilation unit contains those ranges,
101 and when we get to a leaf, we scan through it linearly. Interior nodes
102 contain pointers to 256 other nodes, keyed by the next byte of the address.
103 So for a 64-bit address like 0x1234567abcd, we would start at the root and go
104 down child[0x00]->child[0x00]->child[0x01]->child[0x23]->child[0x45] etc.,
105 until we hit a leaf. (Nodes are, in general, leaves until they exceed the
106 default allocation of 16 elements, at which point they are converted to
107 interior node if possible.) This gives us near-constant lookup times;
108 the only thing that can be costly is if there are lots of overlapping ranges
109 within a single 256-byte segment of the binary, in which case we have to
110 scan through them all to find the best match.
112 For a binary with few ranges, we will in practice only have a single leaf
113 node at the root, containing a simple array. Thus, the scheme is efficient
114 for both small and large binaries.
117 /* Experiments have shown 16 to be a memory-efficient default leaf size.
118 The only case where a leaf will hold more memory than this, is at the
119 bottomost level (covering 256 bytes in the binary), where we'll expand
120 the leaf to be able to hold more ranges if needed.
122 #define TRIE_LEAF_SIZE 16
124 /* All trie_node pointers will really be trie_leaf or trie_interior,
125 but they have this common head. */
126 struct trie_node
128 /* If zero, we are an interior node.
129 Otherwise, how many ranges we have room for in this leaf. */
130 unsigned int num_room_in_leaf;
133 struct trie_leaf
135 struct trie_node head;
136 unsigned int num_stored_in_leaf;
137 struct {
138 struct comp_unit *unit;
139 bfd_vma low_pc, high_pc;
140 } ranges[];
143 struct trie_interior
145 struct trie_node head;
146 struct trie_node *children[256];
149 static struct trie_node *alloc_trie_leaf (bfd *abfd)
151 struct trie_leaf *leaf;
152 size_t amt = sizeof (*leaf) + TRIE_LEAF_SIZE * sizeof (leaf->ranges[0]);
153 leaf = bfd_zalloc (abfd, amt);
154 if (leaf == NULL)
155 return NULL;
156 leaf->head.num_room_in_leaf = TRIE_LEAF_SIZE;
157 return &leaf->head;
160 struct addr_range
162 bfd_byte *start;
163 bfd_byte *end;
166 /* Return true if address range do intersect. */
168 static bool
169 addr_range_intersects (struct addr_range *r1, struct addr_range *r2)
171 return (r1->start <= r2->start && r2->start < r1->end)
172 || (r1->start <= (r2->end - 1) && (r2->end - 1) < r1->end);
175 /* Compare function for splay tree of addr_ranges. */
177 static int
178 splay_tree_compare_addr_range (splay_tree_key xa, splay_tree_key xb)
180 struct addr_range *r1 = (struct addr_range *) xa;
181 struct addr_range *r2 = (struct addr_range *) xb;
183 if (addr_range_intersects (r1, r2) || addr_range_intersects (r2, r1))
184 return 0;
185 else if (r1->end <= r2->start)
186 return -1;
187 else
188 return 1;
191 /* Splay tree release function for keys (addr_range). */
193 static void
194 splay_tree_free_addr_range (splay_tree_key key)
196 free ((struct addr_range *)key);
199 struct dwarf2_debug_file
201 /* The actual bfd from which debug info was loaded. Might be
202 different to orig_bfd because of gnu_debuglink sections. */
203 bfd *bfd_ptr;
205 /* Pointer to the symbol table. */
206 asymbol **syms;
208 /* The current info pointer for the .debug_info section being parsed. */
209 bfd_byte *info_ptr;
211 /* A pointer to the memory block allocated for .debug_info sections. */
212 bfd_byte *dwarf_info_buffer;
214 /* Length of the loaded .debug_info sections. */
215 bfd_size_type dwarf_info_size;
217 /* Pointer to the .debug_abbrev section loaded into memory. */
218 bfd_byte *dwarf_abbrev_buffer;
220 /* Length of the loaded .debug_abbrev section. */
221 bfd_size_type dwarf_abbrev_size;
223 /* Buffer for decode_line_info. */
224 bfd_byte *dwarf_line_buffer;
226 /* Length of the loaded .debug_line section. */
227 bfd_size_type dwarf_line_size;
229 /* Pointer to the .debug_str section loaded into memory. */
230 bfd_byte *dwarf_str_buffer;
232 /* Length of the loaded .debug_str section. */
233 bfd_size_type dwarf_str_size;
235 /* Pointer to the .debug_str_offsets section loaded into memory. */
236 bfd_byte *dwarf_str_offsets_buffer;
238 /* Length of the loaded .debug_str_offsets section. */
239 bfd_size_type dwarf_str_offsets_size;
241 /* Pointer to the .debug_addr section loaded into memory. */
242 bfd_byte *dwarf_addr_buffer;
244 /* Length of the loaded .debug_addr section. */
245 bfd_size_type dwarf_addr_size;
247 /* Pointer to the .debug_line_str section loaded into memory. */
248 bfd_byte *dwarf_line_str_buffer;
250 /* Length of the loaded .debug_line_str section. */
251 bfd_size_type dwarf_line_str_size;
253 /* Pointer to the .debug_ranges section loaded into memory. */
254 bfd_byte *dwarf_ranges_buffer;
256 /* Length of the loaded .debug_ranges section. */
257 bfd_size_type dwarf_ranges_size;
259 /* Pointer to the .debug_rnglists section loaded into memory. */
260 bfd_byte *dwarf_rnglists_buffer;
262 /* Length of the loaded .debug_rnglists section. */
263 bfd_size_type dwarf_rnglists_size;
265 /* A list of all previously read comp_units. */
266 struct comp_unit *all_comp_units;
268 /* A list of all previously read comp_units with no ranges (yet). */
269 struct comp_unit *all_comp_units_without_ranges;
271 /* Last comp unit in list above. */
272 struct comp_unit *last_comp_unit;
274 /* Line table at line_offset zero. */
275 struct line_info_table *line_table;
277 /* Hash table to map offsets to decoded abbrevs. */
278 htab_t abbrev_offsets;
280 /* Root of a trie to map addresses to compilation units. */
281 struct trie_node *trie_root;
283 /* Splay tree to map info_ptr address to compilation units. */
284 splay_tree comp_unit_tree;
287 struct dwarf2_debug
289 /* Names of the debug sections. */
290 const struct dwarf_debug_section *debug_sections;
292 /* Per-file stuff. */
293 struct dwarf2_debug_file f, alt;
295 /* If the most recent call to bfd_find_nearest_line was given an
296 address in an inlined function, preserve a pointer into the
297 calling chain for subsequent calls to bfd_find_inliner_info to
298 use. */
299 struct funcinfo *inliner_chain;
301 /* Section VMAs at the time the stash was built. */
302 bfd_vma *sec_vma;
303 /* Number of sections in the SEC_VMA table. */
304 unsigned int sec_vma_count;
306 /* Number of sections whose VMA we must adjust. */
307 int adjusted_section_count;
309 /* Array of sections with adjusted VMA. */
310 struct adjusted_section *adjusted_sections;
312 /* Used to validate the cached debug data. */
313 unsigned int orig_bfd_id;
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 ((msec->flags & SEC_HAS_CONTENTS) == 0)
710 _bfd_error_handler (_("DWARF error: section %s has no contents"),
711 section_name);
712 bfd_set_error (bfd_error_no_contents);
713 return false;
716 if (bfd_section_size_insane (abfd, msec))
718 /* PR 26946 */
719 _bfd_error_handler (_("DWARF error: section %s is too big"),
720 section_name);
721 return false;
723 amt = bfd_get_section_limit_octets (abfd, msec);
724 *section_size = amt;
725 /* Paranoia - alloc one extra so that we can make sure a string
726 section is NUL terminated. */
727 amt += 1;
728 if (amt == 0)
730 /* Paranoia - this should never happen. */
731 bfd_set_error (bfd_error_no_memory);
732 return false;
734 contents = (bfd_byte *) bfd_malloc (amt);
735 if (contents == NULL)
736 return false;
737 if (syms
738 ? !bfd_simple_get_relocated_section_contents (abfd, msec, contents,
739 syms)
740 : !bfd_get_section_contents (abfd, msec, contents, 0, *section_size))
742 free (contents);
743 return false;
745 contents[*section_size] = 0;
746 *section_buffer = contents;
749 /* It is possible to get a bad value for the offset into the section
750 that the client wants. Validate it here to avoid trouble later. */
751 if (offset != 0 && offset >= *section_size)
753 /* xgettext: c-format */
754 _bfd_error_handler (_("DWARF error: offset (%" PRIu64 ")"
755 " greater than or equal to %s size (%" PRIu64 ")"),
756 (uint64_t) offset, section_name,
757 (uint64_t) *section_size);
758 bfd_set_error (bfd_error_bad_value);
759 return false;
762 return true;
765 /* Read dwarf information from a buffer. */
767 static inline uint64_t
768 read_n_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end, int n)
770 bfd_byte *buf = *ptr;
771 if (end - buf < n)
773 *ptr = end;
774 return 0;
776 *ptr = buf + n;
777 return bfd_get (n * 8, abfd, buf);
780 static unsigned int
781 read_1_byte (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
783 return read_n_bytes (abfd, ptr, end, 1);
786 static int
787 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte **ptr, bfd_byte *end)
789 bfd_byte *buf = *ptr;
790 if (end - buf < 1)
792 *ptr = end;
793 return 0;
795 *ptr = buf + 1;
796 return bfd_get_signed_8 (abfd, buf);
799 static unsigned int
800 read_2_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
802 return read_n_bytes (abfd, ptr, end, 2);
805 static unsigned int
806 read_3_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
808 unsigned int val = read_1_byte (abfd, ptr, end);
809 val <<= 8;
810 val |= read_1_byte (abfd, ptr, end);
811 val <<= 8;
812 val |= read_1_byte (abfd, ptr, end);
813 if (bfd_little_endian (abfd))
814 val = (((val >> 16) & 0xff)
815 | (val & 0xff00)
816 | ((val & 0xff) << 16));
817 return val;
820 static unsigned int
821 read_4_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
823 return read_n_bytes (abfd, ptr, end, 4);
826 static uint64_t
827 read_8_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
829 return read_n_bytes (abfd, ptr, end, 8);
832 static struct dwarf_block *
833 read_blk (bfd *abfd, bfd_byte **ptr, bfd_byte *end, size_t size)
835 bfd_byte *buf = *ptr;
836 struct dwarf_block *block;
838 block = (struct dwarf_block *) bfd_alloc (abfd, sizeof (*block));
839 if (block == NULL)
840 return NULL;
842 if (size > (size_t) (end - buf))
844 *ptr = end;
845 block->data = NULL;
846 block->size = 0;
848 else
850 *ptr = buf + size;
851 block->data = buf;
852 block->size = size;
854 return block;
857 /* Scans a NUL terminated string starting at *PTR, returning a pointer to it.
858 Bytes at or beyond BUF_END will not be read. Returns NULL if the
859 terminator is not found or if the string is empty. *PTR is
860 incremented over the bytes scanned, including the terminator. */
862 static char *
863 read_string (bfd_byte **ptr,
864 bfd_byte *buf_end)
866 bfd_byte *buf = *ptr;
867 bfd_byte *str = buf;
869 while (buf < buf_end)
870 if (*buf++ == 0)
872 if (str == buf - 1)
873 break;
874 *ptr = buf;
875 return (char *) str;
878 *ptr = buf;
879 return NULL;
882 /* Reads an offset from *PTR and then locates the string at this offset
883 inside the debug string section. Returns a pointer to the string.
884 Increments *PTR by the number of bytes read for the offset. This
885 value is set even if the function fails. Bytes at or beyond
886 BUF_END will not be read. Returns NULL if there was a problem, or
887 if the string is empty. Does not check for NUL termination of the
888 string. */
890 static char *
891 read_indirect_string (struct comp_unit *unit,
892 bfd_byte **ptr,
893 bfd_byte *buf_end)
895 uint64_t offset;
896 struct dwarf2_debug *stash = unit->stash;
897 struct dwarf2_debug_file *file = unit->file;
898 char *str;
900 if (unit->offset_size > (size_t) (buf_end - *ptr))
902 *ptr = buf_end;
903 return NULL;
906 if (unit->offset_size == 4)
907 offset = read_4_bytes (unit->abfd, ptr, buf_end);
908 else
909 offset = read_8_bytes (unit->abfd, ptr, buf_end);
911 if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
912 file->syms, offset,
913 &file->dwarf_str_buffer, &file->dwarf_str_size))
914 return NULL;
916 str = (char *) file->dwarf_str_buffer + offset;
917 if (*str == '\0')
918 return NULL;
919 return str;
922 /* Like read_indirect_string but from .debug_line_str section. */
924 static char *
925 read_indirect_line_string (struct comp_unit *unit,
926 bfd_byte **ptr,
927 bfd_byte *buf_end)
929 uint64_t offset;
930 struct dwarf2_debug *stash = unit->stash;
931 struct dwarf2_debug_file *file = unit->file;
932 char *str;
934 if (unit->offset_size > (size_t) (buf_end - *ptr))
936 *ptr = buf_end;
937 return NULL;
940 if (unit->offset_size == 4)
941 offset = read_4_bytes (unit->abfd, ptr, buf_end);
942 else
943 offset = read_8_bytes (unit->abfd, ptr, buf_end);
945 if (! read_section (unit->abfd, &stash->debug_sections[debug_line_str],
946 file->syms, offset,
947 &file->dwarf_line_str_buffer,
948 &file->dwarf_line_str_size))
949 return NULL;
951 str = (char *) file->dwarf_line_str_buffer + offset;
952 if (*str == '\0')
953 return NULL;
954 return str;
957 /* Like read_indirect_string but uses a .debug_str located in
958 an alternate file pointed to by the .gnu_debugaltlink section.
959 Used to impement DW_FORM_GNU_strp_alt. */
961 static char *
962 read_alt_indirect_string (struct comp_unit *unit,
963 bfd_byte **ptr,
964 bfd_byte *buf_end)
966 uint64_t offset;
967 struct dwarf2_debug *stash = unit->stash;
968 char *str;
970 if (unit->offset_size > (size_t) (buf_end - *ptr))
972 *ptr = buf_end;
973 return NULL;
976 if (unit->offset_size == 4)
977 offset = read_4_bytes (unit->abfd, ptr, buf_end);
978 else
979 offset = read_8_bytes (unit->abfd, ptr, buf_end);
981 if (stash->alt.bfd_ptr == NULL)
983 bfd *debug_bfd;
984 char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
986 if (debug_filename == NULL)
987 return NULL;
989 debug_bfd = bfd_openr (debug_filename, NULL);
990 free (debug_filename);
991 if (debug_bfd == NULL)
992 /* FIXME: Should we report our failure to follow the debuglink ? */
993 return NULL;
995 if (!bfd_check_format (debug_bfd, bfd_object))
997 bfd_close (debug_bfd);
998 return NULL;
1000 stash->alt.bfd_ptr = debug_bfd;
1003 if (! read_section (unit->stash->alt.bfd_ptr,
1004 stash->debug_sections + debug_str_alt,
1005 stash->alt.syms, offset,
1006 &stash->alt.dwarf_str_buffer,
1007 &stash->alt.dwarf_str_size))
1008 return NULL;
1010 str = (char *) stash->alt.dwarf_str_buffer + offset;
1011 if (*str == '\0')
1012 return NULL;
1014 return str;
1017 /* Resolve an alternate reference from UNIT at OFFSET.
1018 Returns a pointer into the loaded alternate CU upon success
1019 or NULL upon failure. */
1021 static bfd_byte *
1022 read_alt_indirect_ref (struct comp_unit *unit, uint64_t offset)
1024 struct dwarf2_debug *stash = unit->stash;
1026 if (stash->alt.bfd_ptr == NULL)
1028 bfd *debug_bfd;
1029 char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
1031 if (debug_filename == NULL)
1032 return NULL;
1034 debug_bfd = bfd_openr (debug_filename, NULL);
1035 free (debug_filename);
1036 if (debug_bfd == NULL)
1037 /* FIXME: Should we report our failure to follow the debuglink ? */
1038 return NULL;
1040 if (!bfd_check_format (debug_bfd, bfd_object))
1042 bfd_close (debug_bfd);
1043 return NULL;
1045 stash->alt.bfd_ptr = debug_bfd;
1048 if (! read_section (unit->stash->alt.bfd_ptr,
1049 stash->debug_sections + debug_info_alt,
1050 stash->alt.syms, offset,
1051 &stash->alt.dwarf_info_buffer,
1052 &stash->alt.dwarf_info_size))
1053 return NULL;
1055 return stash->alt.dwarf_info_buffer + offset;
1058 static uint64_t
1059 read_address (struct comp_unit *unit, bfd_byte **ptr, bfd_byte *buf_end)
1061 bfd_byte *buf = *ptr;
1062 int signed_vma = 0;
1064 if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour)
1065 signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
1067 if (unit->addr_size > (size_t) (buf_end - buf))
1069 *ptr = buf_end;
1070 return 0;
1073 *ptr = buf + unit->addr_size;
1074 if (signed_vma)
1076 switch (unit->addr_size)
1078 case 8:
1079 return bfd_get_signed_64 (unit->abfd, buf);
1080 case 4:
1081 return bfd_get_signed_32 (unit->abfd, buf);
1082 case 2:
1083 return bfd_get_signed_16 (unit->abfd, buf);
1084 default:
1085 abort ();
1088 else
1090 switch (unit->addr_size)
1092 case 8:
1093 return bfd_get_64 (unit->abfd, buf);
1094 case 4:
1095 return bfd_get_32 (unit->abfd, buf);
1096 case 2:
1097 return bfd_get_16 (unit->abfd, buf);
1098 default:
1099 abort ();
1104 /* Lookup an abbrev_info structure in the abbrev hash table. */
1106 static struct abbrev_info *
1107 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
1109 unsigned int hash_number;
1110 struct abbrev_info *abbrev;
1112 hash_number = number % ABBREV_HASH_SIZE;
1113 abbrev = abbrevs[hash_number];
1115 while (abbrev)
1117 if (abbrev->number == number)
1118 return abbrev;
1119 else
1120 abbrev = abbrev->next;
1123 return NULL;
1126 /* We keep a hash table to map .debug_abbrev section offsets to the
1127 array of abbrevs, so that compilation units using the same set of
1128 abbrevs do not waste memory. */
1130 struct abbrev_offset_entry
1132 size_t offset;
1133 struct abbrev_info **abbrevs;
1136 static hashval_t
1137 hash_abbrev (const void *p)
1139 const struct abbrev_offset_entry *ent = p;
1140 return htab_hash_pointer ((void *) ent->offset);
1143 static int
1144 eq_abbrev (const void *pa, const void *pb)
1146 const struct abbrev_offset_entry *a = pa;
1147 const struct abbrev_offset_entry *b = pb;
1148 return a->offset == b->offset;
1151 static void
1152 del_abbrev (void *p)
1154 struct abbrev_offset_entry *ent = p;
1155 struct abbrev_info **abbrevs = ent->abbrevs;
1156 size_t i;
1158 for (i = 0; i < ABBREV_HASH_SIZE; i++)
1160 struct abbrev_info *abbrev = abbrevs[i];
1162 while (abbrev)
1164 free (abbrev->attrs);
1165 abbrev = abbrev->next;
1168 free (ent);
1171 /* In DWARF version 2, the description of the debugging information is
1172 stored in a separate .debug_abbrev section. Before we read any
1173 dies from a section we read in all abbreviations and install them
1174 in a hash table. */
1176 static struct abbrev_info**
1177 read_abbrevs (bfd *abfd, uint64_t offset, struct dwarf2_debug *stash,
1178 struct dwarf2_debug_file *file)
1180 struct abbrev_info **abbrevs;
1181 bfd_byte *abbrev_ptr;
1182 bfd_byte *abbrev_end;
1183 struct abbrev_info *cur_abbrev;
1184 unsigned int abbrev_number, abbrev_name;
1185 unsigned int abbrev_form, hash_number;
1186 size_t amt;
1187 void **slot;
1188 struct abbrev_offset_entry ent = { offset, NULL };
1190 if (ent.offset != offset)
1191 return NULL;
1193 slot = htab_find_slot (file->abbrev_offsets, &ent, INSERT);
1194 if (slot == NULL)
1195 return NULL;
1196 if (*slot != NULL)
1197 return ((struct abbrev_offset_entry *) (*slot))->abbrevs;
1199 if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
1200 file->syms, offset,
1201 &file->dwarf_abbrev_buffer,
1202 &file->dwarf_abbrev_size))
1203 return NULL;
1205 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
1206 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
1207 if (abbrevs == NULL)
1208 return NULL;
1210 abbrev_ptr = file->dwarf_abbrev_buffer + offset;
1211 abbrev_end = file->dwarf_abbrev_buffer + file->dwarf_abbrev_size;
1212 abbrev_number = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1213 false, abbrev_end);
1215 /* Loop until we reach an abbrev number of 0. */
1216 while (abbrev_number)
1218 amt = sizeof (struct abbrev_info);
1219 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
1220 if (cur_abbrev == NULL)
1221 goto fail;
1223 /* Read in abbrev header. */
1224 cur_abbrev->number = abbrev_number;
1225 cur_abbrev->tag = (enum dwarf_tag)
1226 _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1227 false, abbrev_end);
1228 cur_abbrev->has_children = read_1_byte (abfd, &abbrev_ptr, abbrev_end);
1230 /* Now read in declarations. */
1231 for (;;)
1233 /* Initialize it just to avoid a GCC false warning. */
1234 bfd_vma implicit_const = -1;
1236 abbrev_name = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1237 false, abbrev_end);
1238 abbrev_form = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1239 false, abbrev_end);
1240 if (abbrev_form == DW_FORM_implicit_const)
1241 implicit_const = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1242 true, abbrev_end);
1243 if (abbrev_name == 0)
1244 break;
1246 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
1248 struct attr_abbrev *tmp;
1250 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
1251 amt *= sizeof (struct attr_abbrev);
1252 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
1253 if (tmp == NULL)
1254 goto fail;
1255 cur_abbrev->attrs = tmp;
1258 cur_abbrev->attrs[cur_abbrev->num_attrs].name
1259 = (enum dwarf_attribute) abbrev_name;
1260 cur_abbrev->attrs[cur_abbrev->num_attrs].form
1261 = (enum dwarf_form) abbrev_form;
1262 cur_abbrev->attrs[cur_abbrev->num_attrs].implicit_const
1263 = implicit_const;
1264 ++cur_abbrev->num_attrs;
1267 hash_number = abbrev_number % ABBREV_HASH_SIZE;
1268 cur_abbrev->next = abbrevs[hash_number];
1269 abbrevs[hash_number] = cur_abbrev;
1271 /* Get next abbreviation.
1272 Under Irix6 the abbreviations for a compilation unit are not
1273 always properly terminated with an abbrev number of 0.
1274 Exit loop if we encounter an abbreviation which we have
1275 already read (which means we are about to read the abbreviations
1276 for the next compile unit) or if the end of the abbreviation
1277 table is reached. */
1278 if ((size_t) (abbrev_ptr - file->dwarf_abbrev_buffer)
1279 >= file->dwarf_abbrev_size)
1280 break;
1281 abbrev_number = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1282 false, abbrev_end);
1283 if (lookup_abbrev (abbrev_number, abbrevs) != NULL)
1284 break;
1287 *slot = bfd_malloc (sizeof ent);
1288 if (!*slot)
1289 goto fail;
1290 ent.abbrevs = abbrevs;
1291 memcpy (*slot, &ent, sizeof ent);
1292 return abbrevs;
1294 fail:
1295 if (abbrevs != NULL)
1297 size_t i;
1299 for (i = 0; i < ABBREV_HASH_SIZE; i++)
1301 struct abbrev_info *abbrev = abbrevs[i];
1303 while (abbrev)
1305 free (abbrev->attrs);
1306 abbrev = abbrev->next;
1309 free (abbrevs);
1311 return NULL;
1314 /* Returns true if the form is one which has a string value. */
1316 static bool
1317 is_str_form (const struct attribute *attr)
1319 switch (attr->form)
1321 case DW_FORM_string:
1322 case DW_FORM_strp:
1323 case DW_FORM_strx:
1324 case DW_FORM_strx1:
1325 case DW_FORM_strx2:
1326 case DW_FORM_strx3:
1327 case DW_FORM_strx4:
1328 case DW_FORM_line_strp:
1329 case DW_FORM_GNU_strp_alt:
1330 return true;
1332 default:
1333 return false;
1337 /* Returns true if the form is one which has an integer value. */
1339 static bool
1340 is_int_form (const struct attribute *attr)
1342 switch (attr->form)
1344 case DW_FORM_addr:
1345 case DW_FORM_data2:
1346 case DW_FORM_data4:
1347 case DW_FORM_data8:
1348 case DW_FORM_data1:
1349 case DW_FORM_flag:
1350 case DW_FORM_sdata:
1351 case DW_FORM_udata:
1352 case DW_FORM_ref_addr:
1353 case DW_FORM_ref1:
1354 case DW_FORM_ref2:
1355 case DW_FORM_ref4:
1356 case DW_FORM_ref8:
1357 case DW_FORM_ref_udata:
1358 case DW_FORM_sec_offset:
1359 case DW_FORM_flag_present:
1360 case DW_FORM_ref_sig8:
1361 case DW_FORM_addrx:
1362 case DW_FORM_implicit_const:
1363 case DW_FORM_addrx1:
1364 case DW_FORM_addrx2:
1365 case DW_FORM_addrx3:
1366 case DW_FORM_addrx4:
1367 case DW_FORM_GNU_ref_alt:
1368 return true;
1370 default:
1371 return false;
1375 /* Returns true if the form is strx[1-4]. */
1377 static inline bool
1378 is_strx_form (enum dwarf_form form)
1380 return (form == DW_FORM_strx
1381 || form == DW_FORM_strx1
1382 || form == DW_FORM_strx2
1383 || form == DW_FORM_strx3
1384 || form == DW_FORM_strx4);
1387 /* Return true if the form is addrx[1-4]. */
1389 static inline bool
1390 is_addrx_form (enum dwarf_form form)
1392 return (form == DW_FORM_addrx
1393 || form == DW_FORM_addrx1
1394 || form == DW_FORM_addrx2
1395 || form == DW_FORM_addrx3
1396 || form == DW_FORM_addrx4);
1399 /* Returns the address in .debug_addr section using DW_AT_addr_base.
1400 Used to implement DW_FORM_addrx*. */
1401 static uint64_t
1402 read_indexed_address (uint64_t idx, struct comp_unit *unit)
1404 struct dwarf2_debug *stash = unit->stash;
1405 struct dwarf2_debug_file *file = unit->file;
1406 bfd_byte *info_ptr;
1407 size_t offset;
1409 if (stash == NULL)
1410 return 0;
1412 if (!read_section (unit->abfd, &stash->debug_sections[debug_addr],
1413 file->syms, 0,
1414 &file->dwarf_addr_buffer, &file->dwarf_addr_size))
1415 return 0;
1417 if (_bfd_mul_overflow (idx, unit->addr_size, &offset))
1418 return 0;
1420 offset += unit->dwarf_addr_offset;
1421 if (offset < unit->dwarf_addr_offset
1422 || offset > file->dwarf_addr_size
1423 || file->dwarf_addr_size - offset < unit->addr_size)
1424 return 0;
1426 info_ptr = file->dwarf_addr_buffer + offset;
1428 if (unit->addr_size == 4)
1429 return bfd_get_32 (unit->abfd, info_ptr);
1430 else if (unit->addr_size == 8)
1431 return bfd_get_64 (unit->abfd, info_ptr);
1432 else
1433 return 0;
1436 /* Returns the string using DW_AT_str_offsets_base.
1437 Used to implement DW_FORM_strx*. */
1438 static const char *
1439 read_indexed_string (uint64_t idx, struct comp_unit *unit)
1441 struct dwarf2_debug *stash = unit->stash;
1442 struct dwarf2_debug_file *file = unit->file;
1443 bfd_byte *info_ptr;
1444 uint64_t str_offset;
1445 size_t offset;
1447 if (stash == NULL)
1448 return NULL;
1450 if (!read_section (unit->abfd, &stash->debug_sections[debug_str],
1451 file->syms, 0,
1452 &file->dwarf_str_buffer, &file->dwarf_str_size))
1453 return NULL;
1455 if (!read_section (unit->abfd, &stash->debug_sections[debug_str_offsets],
1456 file->syms, 0,
1457 &file->dwarf_str_offsets_buffer,
1458 &file->dwarf_str_offsets_size))
1459 return NULL;
1461 if (_bfd_mul_overflow (idx, unit->offset_size, &offset))
1462 return NULL;
1464 offset += unit->dwarf_str_offset;
1465 if (offset < unit->dwarf_str_offset
1466 || offset > file->dwarf_str_offsets_size
1467 || file->dwarf_str_offsets_size - offset < unit->offset_size)
1468 return NULL;
1470 info_ptr = file->dwarf_str_offsets_buffer + offset;
1472 if (unit->offset_size == 4)
1473 str_offset = bfd_get_32 (unit->abfd, info_ptr);
1474 else if (unit->offset_size == 8)
1475 str_offset = bfd_get_64 (unit->abfd, info_ptr);
1476 else
1477 return NULL;
1479 if (str_offset >= file->dwarf_str_size)
1480 return NULL;
1481 return (const char *) file->dwarf_str_buffer + str_offset;
1484 /* Read and fill in the value of attribute ATTR as described by FORM.
1485 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1486 Returns an updated INFO_PTR taking into account the amount of data read. */
1488 static bfd_byte *
1489 read_attribute_value (struct attribute * attr,
1490 unsigned form,
1491 bfd_vma implicit_const,
1492 struct comp_unit * unit,
1493 bfd_byte * info_ptr,
1494 bfd_byte * info_ptr_end)
1496 bfd *abfd = unit->abfd;
1497 size_t amt;
1499 if (info_ptr >= info_ptr_end && form != DW_FORM_flag_present)
1501 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1502 bfd_set_error (bfd_error_bad_value);
1503 return NULL;
1506 attr->form = (enum dwarf_form) form;
1508 switch (form)
1510 case DW_FORM_flag_present:
1511 attr->u.val = 1;
1512 break;
1513 case DW_FORM_ref_addr:
1514 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1515 DWARF3. */
1516 if (unit->version >= 3)
1518 if (unit->offset_size == 4)
1519 attr->u.val = read_4_bytes (unit->abfd, &info_ptr, info_ptr_end);
1520 else
1521 attr->u.val = read_8_bytes (unit->abfd, &info_ptr, info_ptr_end);
1522 break;
1524 /* FALLTHROUGH */
1525 case DW_FORM_addr:
1526 attr->u.val = read_address (unit, &info_ptr, info_ptr_end);
1527 break;
1528 case DW_FORM_GNU_ref_alt:
1529 case DW_FORM_sec_offset:
1530 if (unit->offset_size == 4)
1531 attr->u.val = read_4_bytes (unit->abfd, &info_ptr, info_ptr_end);
1532 else
1533 attr->u.val = read_8_bytes (unit->abfd, &info_ptr, info_ptr_end);
1534 break;
1535 case DW_FORM_block2:
1536 amt = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1537 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1538 if (attr->u.blk == NULL)
1539 return NULL;
1540 break;
1541 case DW_FORM_block4:
1542 amt = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1543 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1544 if (attr->u.blk == NULL)
1545 return NULL;
1546 break;
1547 case DW_FORM_ref1:
1548 case DW_FORM_flag:
1549 case DW_FORM_data1:
1550 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1551 break;
1552 case DW_FORM_addrx1:
1553 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1554 /* dwarf_addr_offset value 0 indicates the attribute DW_AT_addr_base
1555 is not yet read. */
1556 if (unit->dwarf_addr_offset != 0)
1557 attr->u.val = read_indexed_address (attr->u.val, unit);
1558 break;
1559 case DW_FORM_data2:
1560 case DW_FORM_ref2:
1561 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1562 break;
1563 case DW_FORM_addrx2:
1564 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1565 if (unit->dwarf_addr_offset != 0)
1566 attr->u.val = read_indexed_address (attr->u.val, unit);
1567 break;
1568 case DW_FORM_addrx3:
1569 attr->u.val = read_3_bytes (abfd, &info_ptr, info_ptr_end);
1570 if (unit->dwarf_addr_offset != 0)
1571 attr->u.val = read_indexed_address(attr->u.val, unit);
1572 break;
1573 case DW_FORM_ref4:
1574 case DW_FORM_data4:
1575 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1576 break;
1577 case DW_FORM_addrx4:
1578 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1579 if (unit->dwarf_addr_offset != 0)
1580 attr->u.val = read_indexed_address (attr->u.val, unit);
1581 break;
1582 case DW_FORM_data8:
1583 case DW_FORM_ref8:
1584 case DW_FORM_ref_sig8:
1585 attr->u.val = read_8_bytes (abfd, &info_ptr, info_ptr_end);
1586 break;
1587 case DW_FORM_string:
1588 attr->u.str = read_string (&info_ptr, info_ptr_end);
1589 break;
1590 case DW_FORM_strp:
1591 attr->u.str = read_indirect_string (unit, &info_ptr, info_ptr_end);
1592 break;
1593 case DW_FORM_line_strp:
1594 attr->u.str = read_indirect_line_string (unit, &info_ptr, info_ptr_end);
1595 break;
1596 case DW_FORM_GNU_strp_alt:
1597 attr->u.str = read_alt_indirect_string (unit, &info_ptr, info_ptr_end);
1598 break;
1599 case DW_FORM_strx1:
1600 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1601 /* dwarf_str_offset value 0 indicates the attribute DW_AT_str_offsets_base
1602 is not yet read. */
1603 if (unit->dwarf_str_offset != 0)
1604 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1605 else
1606 attr->u.str = NULL;
1607 break;
1608 case DW_FORM_strx2:
1609 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1610 if (unit->dwarf_str_offset != 0)
1611 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1612 else
1613 attr->u.str = NULL;
1614 break;
1615 case DW_FORM_strx3:
1616 attr->u.val = read_3_bytes (abfd, &info_ptr, info_ptr_end);
1617 if (unit->dwarf_str_offset != 0)
1618 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1619 else
1620 attr->u.str = NULL;
1621 break;
1622 case DW_FORM_strx4:
1623 attr->u.val = read_4_bytes (abfd, &info_ptr, 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_strx:
1630 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1631 false, info_ptr_end);
1632 if (unit->dwarf_str_offset != 0)
1633 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1634 else
1635 attr->u.str = NULL;
1636 break;
1637 case DW_FORM_exprloc:
1638 case DW_FORM_block:
1639 amt = _bfd_safe_read_leb128 (abfd, &info_ptr,
1640 false, info_ptr_end);
1641 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1642 if (attr->u.blk == NULL)
1643 return NULL;
1644 break;
1645 case DW_FORM_block1:
1646 amt = read_1_byte (abfd, &info_ptr, info_ptr_end);
1647 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1648 if (attr->u.blk == NULL)
1649 return NULL;
1650 break;
1651 case DW_FORM_sdata:
1652 attr->u.sval = _bfd_safe_read_leb128 (abfd, &info_ptr,
1653 true, info_ptr_end);
1654 break;
1656 case DW_FORM_rnglistx:
1657 case DW_FORM_loclistx:
1658 /* FIXME: Add support for these forms! */
1659 /* Fall through. */
1660 case DW_FORM_ref_udata:
1661 case DW_FORM_udata:
1662 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1663 false, info_ptr_end);
1664 break;
1665 case DW_FORM_addrx:
1666 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1667 false, info_ptr_end);
1668 if (unit->dwarf_addr_offset != 0)
1669 attr->u.val = read_indexed_address (attr->u.val, unit);
1670 break;
1671 case DW_FORM_indirect:
1672 form = _bfd_safe_read_leb128 (abfd, &info_ptr,
1673 false, info_ptr_end);
1674 if (form == DW_FORM_implicit_const)
1675 implicit_const = _bfd_safe_read_leb128 (abfd, &info_ptr,
1676 true, info_ptr_end);
1677 info_ptr = read_attribute_value (attr, form, implicit_const, unit,
1678 info_ptr, info_ptr_end);
1679 break;
1680 case DW_FORM_implicit_const:
1681 attr->form = DW_FORM_sdata;
1682 attr->u.sval = implicit_const;
1683 break;
1684 case DW_FORM_data16:
1685 /* This is really a "constant", but there is no way to store that
1686 so pretend it is a 16 byte block instead. */
1687 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, 16);
1688 if (attr->u.blk == NULL)
1689 return NULL;
1690 break;
1692 default:
1693 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1694 form);
1695 bfd_set_error (bfd_error_bad_value);
1696 return NULL;
1698 return info_ptr;
1701 /* Read an attribute described by an abbreviated attribute. */
1703 static bfd_byte *
1704 read_attribute (struct attribute * attr,
1705 struct attr_abbrev * abbrev,
1706 struct comp_unit * unit,
1707 bfd_byte * info_ptr,
1708 bfd_byte * info_ptr_end)
1710 attr->name = abbrev->name;
1711 info_ptr = read_attribute_value (attr, abbrev->form, abbrev->implicit_const,
1712 unit, info_ptr, info_ptr_end);
1713 return info_ptr;
1716 /* Return mangling style given LANG. */
1718 static int
1719 mangle_style (int lang)
1721 switch (lang)
1723 case DW_LANG_Ada83:
1724 case DW_LANG_Ada95:
1725 case DW_LANG_Ada2005:
1726 case DW_LANG_Ada2012:
1727 return DMGL_GNAT;
1729 case DW_LANG_C_plus_plus:
1730 case DW_LANG_C_plus_plus_03:
1731 case DW_LANG_C_plus_plus_11:
1732 case DW_LANG_C_plus_plus_14:
1733 case DW_LANG_C_plus_plus_17:
1734 case DW_LANG_C_plus_plus_20:
1735 case DW_LANG_C_plus_plus_23:
1736 return DMGL_GNU_V3;
1738 case DW_LANG_Java:
1739 return DMGL_JAVA;
1741 case DW_LANG_D:
1742 return DMGL_DLANG;
1744 case DW_LANG_Rust:
1745 case DW_LANG_Rust_old:
1746 return DMGL_RUST;
1748 default:
1749 return DMGL_AUTO;
1751 case DW_LANG_C89:
1752 case DW_LANG_C:
1753 case DW_LANG_Cobol74:
1754 case DW_LANG_Cobol85:
1755 case DW_LANG_Fortran77:
1756 case DW_LANG_Fortran18:
1757 case DW_LANG_Fortran23:
1758 case DW_LANG_Pascal83:
1759 case DW_LANG_PLI:
1760 case DW_LANG_C99:
1761 case DW_LANG_UPC:
1762 case DW_LANG_C11:
1763 case DW_LANG_C17:
1764 case DW_LANG_C23:
1765 case DW_LANG_Mips_Assembler:
1766 case DW_LANG_Assembly:
1767 case DW_LANG_Upc:
1768 case DW_LANG_HP_Basic91:
1769 case DW_LANG_HP_IMacro:
1770 case DW_LANG_HP_Assembler:
1771 return 0;
1775 /* Source line information table routines. */
1777 #define FILE_ALLOC_CHUNK 5
1778 #define DIR_ALLOC_CHUNK 5
1780 struct line_info
1782 struct line_info * prev_line;
1783 bfd_vma address;
1784 char * filename;
1785 unsigned int line;
1786 unsigned int column;
1787 unsigned int discriminator;
1788 unsigned char op_index;
1789 unsigned char end_sequence; /* End of (sequential) code sequence. */
1792 struct fileinfo
1794 char * name;
1795 unsigned int dir;
1796 unsigned int time;
1797 unsigned int size;
1800 struct line_sequence
1802 bfd_vma low_pc;
1803 struct line_sequence* prev_sequence;
1804 struct line_info* last_line; /* Largest VMA. */
1805 struct line_info** line_info_lookup;
1806 bfd_size_type num_lines;
1809 struct line_info_table
1811 bfd * abfd;
1812 unsigned int num_files;
1813 unsigned int num_dirs;
1814 unsigned int num_sequences;
1815 bool use_dir_and_file_0;
1816 char * comp_dir;
1817 char ** dirs;
1818 struct fileinfo* files;
1819 struct line_sequence* sequences;
1820 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
1823 /* Remember some information about each function. If the function is
1824 inlined (DW_TAG_inlined_subroutine) it may have two additional
1825 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1826 source code location where this function was inlined. */
1828 struct funcinfo
1830 /* Pointer to previous function in list of all functions. */
1831 struct funcinfo *prev_func;
1832 /* Pointer to function one scope higher. */
1833 struct funcinfo *caller_func;
1834 /* Source location file name where caller_func inlines this func. */
1835 char *caller_file;
1836 /* Source location file name. */
1837 char *file;
1838 /* Source location line number where caller_func inlines this func. */
1839 int caller_line;
1840 /* Source location line number. */
1841 int line;
1842 int tag;
1843 bool is_linkage;
1844 const char *name;
1845 struct arange arange;
1846 /* The offset of the funcinfo from the start of the unit. */
1847 uint64_t unit_offset;
1850 struct lookup_funcinfo
1852 /* Function information corresponding to this lookup table entry. */
1853 struct funcinfo *funcinfo;
1855 /* The lowest address for this specific function. */
1856 bfd_vma low_addr;
1858 /* The highest address of this function before the lookup table is sorted.
1859 The highest address of all prior functions after the lookup table is
1860 sorted, which is used for binary search. */
1861 bfd_vma high_addr;
1862 /* Index of this function, used to ensure qsort is stable. */
1863 unsigned int idx;
1866 struct varinfo
1868 /* Pointer to previous variable in list of all variables. */
1869 struct varinfo *prev_var;
1870 /* The offset of the varinfo from the start of the unit. */
1871 uint64_t unit_offset;
1872 /* Source location file name. */
1873 char *file;
1874 /* Source location line number. */
1875 int line;
1876 /* The type of this variable. */
1877 int tag;
1878 /* The name of the variable, if it has one. */
1879 const char *name;
1880 /* The address of the variable. */
1881 bfd_vma addr;
1882 /* Is this a stack variable? */
1883 bool stack;
1886 /* Return TRUE if NEW_LINE should sort after LINE. */
1888 static inline bool
1889 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1891 return (new_line->address > line->address
1892 || (new_line->address == line->address
1893 && new_line->op_index > line->op_index));
1897 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1898 that the list is sorted. Note that the line_info list is sorted from
1899 highest to lowest VMA (with possible duplicates); that is,
1900 line_info->prev_line always accesses an equal or smaller VMA. */
1902 static bool
1903 add_line_info (struct line_info_table *table,
1904 bfd_vma address,
1905 unsigned char op_index,
1906 char *filename,
1907 unsigned int line,
1908 unsigned int column,
1909 unsigned int discriminator,
1910 int end_sequence)
1912 size_t amt = sizeof (struct line_info);
1913 struct line_sequence* seq = table->sequences;
1914 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
1916 if (info == NULL)
1917 return false;
1919 /* Set member data of 'info'. */
1920 info->prev_line = NULL;
1921 info->address = address;
1922 info->op_index = op_index;
1923 info->line = line;
1924 info->column = column;
1925 info->discriminator = discriminator;
1926 info->end_sequence = end_sequence;
1928 if (filename && filename[0])
1930 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1931 if (info->filename == NULL)
1932 return false;
1933 strcpy (info->filename, filename);
1935 else
1936 info->filename = NULL;
1938 /* Find the correct location for 'info'. Normally we will receive
1939 new line_info data 1) in order and 2) with increasing VMAs.
1940 However some compilers break the rules (cf. decode_line_info) and
1941 so we include some heuristics for quickly finding the correct
1942 location for 'info'. In particular, these heuristics optimize for
1943 the common case in which the VMA sequence that we receive is a
1944 list of locally sorted VMAs such as
1945 p...z a...j (where a < j < p < z)
1947 Note: table->lcl_head is used to head an *actual* or *possible*
1948 sub-sequence within the list (such as a...j) that is not directly
1949 headed by table->last_line
1951 Note: we may receive duplicate entries from 'decode_line_info'. */
1953 if (seq
1954 && seq->last_line->address == address
1955 && seq->last_line->op_index == op_index
1956 && seq->last_line->end_sequence == end_sequence)
1958 /* We only keep the last entry with the same address and end
1959 sequence. See PR ld/4986. */
1960 if (table->lcl_head == seq->last_line)
1961 table->lcl_head = info;
1962 info->prev_line = seq->last_line->prev_line;
1963 seq->last_line = info;
1965 else if (!seq || seq->last_line->end_sequence)
1967 /* Start a new line sequence. */
1968 amt = sizeof (struct line_sequence);
1969 seq = (struct line_sequence *) bfd_malloc (amt);
1970 if (seq == NULL)
1971 return false;
1972 seq->low_pc = address;
1973 seq->prev_sequence = table->sequences;
1974 seq->last_line = info;
1975 table->lcl_head = info;
1976 table->sequences = seq;
1977 table->num_sequences++;
1979 else if (info->end_sequence
1980 || new_line_sorts_after (info, seq->last_line))
1982 /* Normal case: add 'info' to the beginning of the current sequence. */
1983 info->prev_line = seq->last_line;
1984 seq->last_line = info;
1986 /* lcl_head: initialize to head a *possible* sequence at the end. */
1987 if (!table->lcl_head)
1988 table->lcl_head = info;
1990 else if (!new_line_sorts_after (info, table->lcl_head)
1991 && (!table->lcl_head->prev_line
1992 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1994 /* Abnormal but easy: lcl_head is the head of 'info'. */
1995 info->prev_line = table->lcl_head->prev_line;
1996 table->lcl_head->prev_line = info;
1998 else
2000 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
2001 are valid heads for 'info'. Reset 'lcl_head'. */
2002 struct line_info* li2 = seq->last_line; /* Always non-NULL. */
2003 struct line_info* li1 = li2->prev_line;
2005 while (li1)
2007 if (!new_line_sorts_after (info, li2)
2008 && new_line_sorts_after (info, li1))
2009 break;
2011 li2 = li1; /* always non-NULL */
2012 li1 = li1->prev_line;
2014 table->lcl_head = li2;
2015 info->prev_line = table->lcl_head->prev_line;
2016 table->lcl_head->prev_line = info;
2017 if (address < seq->low_pc)
2018 seq->low_pc = address;
2020 return true;
2023 /* Extract a fully qualified filename from a line info table.
2024 The returned string has been malloc'ed and it is the caller's
2025 responsibility to free it. */
2027 static char *
2028 concat_filename (struct line_info_table *table, unsigned int file)
2030 char *filename;
2032 /* Pre DWARF-5 entry 0 in the directory and filename tables was not used.
2033 So in order to save space in the tables used here the info for, eg
2034 directory 1 is stored in slot 0 of the directory table, directory 2
2035 in slot 1 and so on.
2037 Starting with DWARF-5 the 0'th entry is used so there is a one to one
2038 mapping between DWARF slots and internal table entries. */
2039 if (! table->use_dir_and_file_0)
2041 /* Pre DWARF-5, FILE == 0 means unknown. */
2042 if (file == 0)
2043 return strdup ("<unknown>");
2044 -- file;
2047 if (table == NULL || file >= table->num_files)
2049 _bfd_error_handler
2050 (_("DWARF error: mangled line number section (bad file number)"));
2051 return strdup ("<unknown>");
2054 filename = table->files[file].name;
2056 if (filename == NULL)
2057 return strdup ("<unknown>");
2059 if (!IS_ABSOLUTE_PATH (filename))
2061 char *dir_name = NULL;
2062 char *subdir_name = NULL;
2063 char *name;
2064 size_t len;
2065 unsigned int dir = table->files[file].dir;
2067 if (!table->use_dir_and_file_0)
2068 --dir;
2069 /* Wrapping from 0 to -1u above gives the intended result with
2070 the test below of leaving subdir_name NULL for pre-DWARF5 dir
2071 of 0. */
2072 /* PR 17512: file: 0317e960, file: 7f3d2e4b. */
2073 if (dir < table->num_dirs)
2074 subdir_name = table->dirs[dir];
2076 if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
2077 dir_name = table->comp_dir;
2079 if (!dir_name)
2081 dir_name = subdir_name;
2082 subdir_name = NULL;
2085 if (!dir_name)
2086 return strdup (filename);
2088 len = strlen (dir_name) + strlen (filename) + 2;
2090 if (subdir_name)
2092 len += strlen (subdir_name) + 1;
2093 name = (char *) bfd_malloc (len);
2094 if (name)
2095 sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
2097 else
2099 name = (char *) bfd_malloc (len);
2100 if (name)
2101 sprintf (name, "%s/%s", dir_name, filename);
2104 return name;
2107 return strdup (filename);
2110 /* Number of bits in a bfd_vma. */
2111 #define VMA_BITS (8 * sizeof (bfd_vma))
2113 /* Check whether [low1, high1) can be combined with [low2, high2),
2114 i.e., they touch or overlap. */
2116 static bool
2117 ranges_overlap (bfd_vma low1,
2118 bfd_vma high1,
2119 bfd_vma low2,
2120 bfd_vma high2)
2122 if (low1 == low2 || high1 == high2)
2123 return true;
2125 /* Sort so that low1 is below low2. */
2126 if (low1 > low2)
2128 bfd_vma tmp;
2130 tmp = low1;
2131 low1 = low2;
2132 low2 = tmp;
2134 tmp = high1;
2135 high1 = high2;
2136 high2 = tmp;
2139 /* We touch iff low2 == high1.
2140 We overlap iff low2 is within [low1, high1). */
2141 return low2 <= high1;
2144 /* Insert an address range in the trie mapping addresses to compilation units.
2145 Will return the new trie node (usually the same as is being sent in, but
2146 in case of a leaf-to-interior conversion, or expansion of a leaf, it may be
2147 different), or NULL on failure. */
2149 static struct trie_node *
2150 insert_arange_in_trie (bfd *abfd,
2151 struct trie_node *trie,
2152 bfd_vma trie_pc,
2153 unsigned int trie_pc_bits,
2154 struct comp_unit *unit,
2155 bfd_vma low_pc,
2156 bfd_vma high_pc)
2158 bfd_vma clamped_low_pc, clamped_high_pc;
2159 int ch, from_ch, to_ch;
2160 bool is_full_leaf = false;
2161 bool splitting_leaf_will_help = false;
2163 /* See if we can extend any of the existing ranges. This merging
2164 isn't perfect (if merging opens up the possibility of merging two existing
2165 ranges, we won't find them), but it takes the majority of the cases. */
2166 if (trie->num_room_in_leaf > 0)
2168 struct trie_leaf *leaf = (struct trie_leaf *) trie;
2169 unsigned int i;
2171 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2173 if (leaf->ranges[i].unit == unit
2174 && ranges_overlap (low_pc, high_pc,
2175 leaf->ranges[i].low_pc,
2176 leaf->ranges[i].high_pc))
2178 if (low_pc < leaf->ranges[i].low_pc)
2179 leaf->ranges[i].low_pc = low_pc;
2180 if (high_pc > leaf->ranges[i].high_pc)
2181 leaf->ranges[i].high_pc = high_pc;
2182 return trie;
2186 is_full_leaf = leaf->num_stored_in_leaf == trie->num_room_in_leaf;
2188 if (is_full_leaf && trie_pc_bits < VMA_BITS)
2190 /* See if we have at least one leaf that does _not_ cover the
2191 entire bucket, so that splitting will actually reduce the number
2192 of elements in at least one of the child nodes. (For simplicity,
2193 we don't test the range we're inserting, but it will be counted
2194 on the next insertion where we're full, if any.) */
2195 bfd_vma bucket_high_pc =
2196 trie_pc + ((bfd_vma) -1 >> trie_pc_bits); /* Inclusive. */
2197 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2199 if (leaf->ranges[i].low_pc > trie_pc
2200 || leaf->ranges[i].high_pc <= bucket_high_pc)
2202 splitting_leaf_will_help = true;
2203 break;
2209 /* If we're a leaf with no more room and we're _not_ at the bottom,
2210 convert to an interior node. */
2211 if (is_full_leaf && splitting_leaf_will_help)
2213 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
2214 unsigned int i;
2216 trie = bfd_zalloc (abfd, sizeof (struct trie_interior));
2217 if (!trie)
2218 return NULL;
2219 is_full_leaf = false;
2221 /* TODO: If we wanted to save a little more memory at the cost of
2222 complexity, we could have reused the old leaf node as one of the
2223 children of the new interior node, instead of throwing it away. */
2224 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2226 if (!insert_arange_in_trie (abfd, trie, trie_pc, trie_pc_bits,
2227 leaf->ranges[i].unit, leaf->ranges[i].low_pc,
2228 leaf->ranges[i].high_pc))
2229 return NULL;
2233 /* If we're a leaf with no more room and we _are_ at the bottom
2234 (or splitting it won't help), we have no choice but to just
2235 make it larger. */
2236 if (is_full_leaf)
2238 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
2239 unsigned int new_room_in_leaf = trie->num_room_in_leaf * 2;
2240 struct trie_leaf *new_leaf;
2241 size_t amt = sizeof (*leaf) + new_room_in_leaf * sizeof (leaf->ranges[0]);
2242 new_leaf = bfd_zalloc (abfd, amt);
2243 new_leaf->head.num_room_in_leaf = new_room_in_leaf;
2244 new_leaf->num_stored_in_leaf = leaf->num_stored_in_leaf;
2246 memcpy (new_leaf->ranges,
2247 leaf->ranges,
2248 leaf->num_stored_in_leaf * sizeof (leaf->ranges[0]));
2249 trie = &new_leaf->head;
2250 is_full_leaf = false;
2252 /* Now the insert below will go through. */
2255 /* If we're a leaf (now with room), we can just insert at the end. */
2256 if (trie->num_room_in_leaf > 0)
2258 struct trie_leaf *leaf = (struct trie_leaf *) trie;
2260 unsigned int i = leaf->num_stored_in_leaf++;
2261 leaf->ranges[i].unit = unit;
2262 leaf->ranges[i].low_pc = low_pc;
2263 leaf->ranges[i].high_pc = high_pc;
2264 return trie;
2267 /* Now we are definitely an interior node, so recurse into all
2268 the relevant buckets. */
2270 /* Clamp the range to the current trie bucket. */
2271 clamped_low_pc = low_pc;
2272 clamped_high_pc = high_pc;
2273 if (trie_pc_bits > 0)
2275 bfd_vma bucket_high_pc =
2276 trie_pc + ((bfd_vma) -1 >> trie_pc_bits); /* Inclusive. */
2277 if (clamped_low_pc < trie_pc)
2278 clamped_low_pc = trie_pc;
2279 if (clamped_high_pc > bucket_high_pc)
2280 clamped_high_pc = bucket_high_pc;
2283 /* Insert the ranges in all buckets that it spans. */
2284 from_ch = (clamped_low_pc >> (VMA_BITS - trie_pc_bits - 8)) & 0xff;
2285 to_ch = ((clamped_high_pc - 1) >> (VMA_BITS - trie_pc_bits - 8)) & 0xff;
2286 for (ch = from_ch; ch <= to_ch; ++ch)
2288 struct trie_interior *interior = (struct trie_interior *) trie;
2289 struct trie_node *child = interior->children[ch];
2291 if (child == NULL)
2293 child = alloc_trie_leaf (abfd);
2294 if (!child)
2295 return NULL;
2297 bfd_vma bucket = (bfd_vma) ch << (VMA_BITS - trie_pc_bits - 8);
2298 child = insert_arange_in_trie (abfd,
2299 child,
2300 trie_pc + bucket,
2301 trie_pc_bits + 8,
2302 unit,
2303 low_pc,
2304 high_pc);
2305 if (!child)
2306 return NULL;
2308 interior->children[ch] = child;
2311 return trie;
2314 static bool
2315 arange_add (struct comp_unit *unit, struct arange *first_arange,
2316 struct trie_node **trie_root, bfd_vma low_pc, bfd_vma high_pc)
2318 struct arange *arange;
2320 /* Ignore empty ranges. */
2321 if (low_pc == high_pc)
2322 return true;
2324 if (trie_root != NULL)
2326 *trie_root = insert_arange_in_trie (unit->file->bfd_ptr,
2327 *trie_root,
2330 unit,
2331 low_pc,
2332 high_pc);
2333 if (*trie_root == NULL)
2334 return false;
2337 /* If the first arange is empty, use it. */
2338 if (first_arange->high == 0)
2340 first_arange->low = low_pc;
2341 first_arange->high = high_pc;
2342 return true;
2345 /* Next see if we can cheaply extend an existing range. */
2346 arange = first_arange;
2349 if (low_pc == arange->high)
2351 arange->high = high_pc;
2352 return true;
2354 if (high_pc == arange->low)
2356 arange->low = low_pc;
2357 return true;
2359 arange = arange->next;
2361 while (arange);
2363 /* Need to allocate a new arange and insert it into the arange list.
2364 Order isn't significant, so just insert after the first arange. */
2365 arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
2366 if (arange == NULL)
2367 return false;
2368 arange->low = low_pc;
2369 arange->high = high_pc;
2370 arange->next = first_arange->next;
2371 first_arange->next = arange;
2372 return true;
2375 /* Compare function for line sequences. */
2377 static int
2378 compare_sequences (const void* a, const void* b)
2380 const struct line_sequence* seq1 = a;
2381 const struct line_sequence* seq2 = b;
2383 /* Sort by low_pc as the primary key. */
2384 if (seq1->low_pc < seq2->low_pc)
2385 return -1;
2386 if (seq1->low_pc > seq2->low_pc)
2387 return 1;
2389 /* If low_pc values are equal, sort in reverse order of
2390 high_pc, so that the largest region comes first. */
2391 if (seq1->last_line->address < seq2->last_line->address)
2392 return 1;
2393 if (seq1->last_line->address > seq2->last_line->address)
2394 return -1;
2396 if (seq1->last_line->op_index < seq2->last_line->op_index)
2397 return 1;
2398 if (seq1->last_line->op_index > seq2->last_line->op_index)
2399 return -1;
2401 /* num_lines is initially an index, to make the sort stable. */
2402 if (seq1->num_lines < seq2->num_lines)
2403 return -1;
2404 if (seq1->num_lines > seq2->num_lines)
2405 return 1;
2406 return 0;
2409 /* Construct the line information table for quick lookup. */
2411 static bool
2412 build_line_info_table (struct line_info_table * table,
2413 struct line_sequence * seq)
2415 size_t amt;
2416 struct line_info **line_info_lookup;
2417 struct line_info *each_line;
2418 unsigned int num_lines;
2419 unsigned int line_index;
2421 if (seq->line_info_lookup != NULL)
2422 return true;
2424 /* Count the number of line information entries. We could do this while
2425 scanning the debug information, but some entries may be added via
2426 lcl_head without having a sequence handy to increment the number of
2427 lines. */
2428 num_lines = 0;
2429 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
2430 num_lines++;
2432 seq->num_lines = num_lines;
2433 if (num_lines == 0)
2434 return true;
2436 /* Allocate space for the line information lookup table. */
2437 amt = sizeof (struct line_info*) * num_lines;
2438 line_info_lookup = (struct line_info**) bfd_alloc (table->abfd, amt);
2439 seq->line_info_lookup = line_info_lookup;
2440 if (line_info_lookup == NULL)
2441 return false;
2443 /* Create the line information lookup table. */
2444 line_index = num_lines;
2445 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
2446 line_info_lookup[--line_index] = each_line;
2448 BFD_ASSERT (line_index == 0);
2449 return true;
2452 /* Sort the line sequences for quick lookup. */
2454 static bool
2455 sort_line_sequences (struct line_info_table* table)
2457 size_t amt;
2458 struct line_sequence *sequences;
2459 struct line_sequence *seq;
2460 unsigned int n = 0;
2461 unsigned int num_sequences = table->num_sequences;
2462 bfd_vma last_high_pc;
2464 if (num_sequences == 0)
2465 return true;
2467 /* Allocate space for an array of sequences. */
2468 amt = sizeof (struct line_sequence) * num_sequences;
2469 sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
2470 if (sequences == NULL)
2471 return false;
2473 /* Copy the linked list into the array, freeing the original nodes. */
2474 seq = table->sequences;
2475 for (n = 0; n < num_sequences; n++)
2477 struct line_sequence* last_seq = seq;
2479 BFD_ASSERT (seq);
2480 sequences[n].low_pc = seq->low_pc;
2481 sequences[n].prev_sequence = NULL;
2482 sequences[n].last_line = seq->last_line;
2483 sequences[n].line_info_lookup = NULL;
2484 sequences[n].num_lines = n;
2485 seq = seq->prev_sequence;
2486 free (last_seq);
2488 BFD_ASSERT (seq == NULL);
2490 qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
2492 /* Make the list binary-searchable by trimming overlapping entries
2493 and removing nested entries. */
2494 num_sequences = 1;
2495 last_high_pc = sequences[0].last_line->address;
2496 for (n = 1; n < table->num_sequences; n++)
2498 if (sequences[n].low_pc < last_high_pc)
2500 if (sequences[n].last_line->address <= last_high_pc)
2501 /* Skip nested entries. */
2502 continue;
2504 /* Trim overlapping entries. */
2505 sequences[n].low_pc = last_high_pc;
2507 last_high_pc = sequences[n].last_line->address;
2508 if (n > num_sequences)
2510 /* Close up the gap. */
2511 sequences[num_sequences].low_pc = sequences[n].low_pc;
2512 sequences[num_sequences].last_line = sequences[n].last_line;
2514 num_sequences++;
2517 table->sequences = sequences;
2518 table->num_sequences = num_sequences;
2519 return true;
2522 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
2524 static bool
2525 line_info_add_include_dir (struct line_info_table *table, char *cur_dir)
2527 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
2529 char **tmp;
2530 size_t amt;
2532 amt = table->num_dirs + DIR_ALLOC_CHUNK;
2533 amt *= sizeof (char *);
2535 tmp = (char **) bfd_realloc (table->dirs, amt);
2536 if (tmp == NULL)
2537 return false;
2538 table->dirs = tmp;
2541 table->dirs[table->num_dirs++] = cur_dir;
2542 return true;
2545 static bool
2546 line_info_add_include_dir_stub (struct line_info_table *table, char *cur_dir,
2547 unsigned int dir ATTRIBUTE_UNUSED,
2548 unsigned int xtime ATTRIBUTE_UNUSED,
2549 unsigned int size ATTRIBUTE_UNUSED)
2551 return line_info_add_include_dir (table, cur_dir);
2554 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
2556 static bool
2557 line_info_add_file_name (struct line_info_table *table, char *cur_file,
2558 unsigned int dir, unsigned int xtime,
2559 unsigned int size)
2561 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
2563 struct fileinfo *tmp;
2564 size_t amt;
2566 amt = table->num_files + FILE_ALLOC_CHUNK;
2567 amt *= sizeof (struct fileinfo);
2569 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
2570 if (tmp == NULL)
2571 return false;
2572 table->files = tmp;
2575 table->files[table->num_files].name = cur_file;
2576 table->files[table->num_files].dir = dir;
2577 table->files[table->num_files].time = xtime;
2578 table->files[table->num_files].size = size;
2579 table->num_files++;
2580 return true;
2583 /* Read directory or file name entry format, starting with byte of
2584 format count entries, ULEB128 pairs of entry formats, ULEB128 of
2585 entries count and the entries themselves in the described entry
2586 format. */
2588 static bool
2589 read_formatted_entries (struct comp_unit *unit, bfd_byte **bufp,
2590 bfd_byte *buf_end, struct line_info_table *table,
2591 bool (*callback) (struct line_info_table *table,
2592 char *cur_file,
2593 unsigned int dir,
2594 unsigned int time,
2595 unsigned int size))
2597 bfd *abfd = unit->abfd;
2598 bfd_byte format_count, formati;
2599 bfd_vma data_count, datai;
2600 bfd_byte *buf = *bufp;
2601 bfd_byte *format_header_data;
2603 format_count = read_1_byte (abfd, &buf, buf_end);
2604 format_header_data = buf;
2605 for (formati = 0; formati < format_count; formati++)
2607 _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2608 _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2611 data_count = _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2612 if (format_count == 0 && data_count != 0)
2614 _bfd_error_handler (_("DWARF error: zero format count"));
2615 bfd_set_error (bfd_error_bad_value);
2616 return false;
2619 /* PR 22210. Paranoia check. Don't bother running the loop
2620 if we know that we are going to run out of buffer. */
2621 if (data_count > (bfd_vma) (buf_end - buf))
2623 _bfd_error_handler
2624 (_("DWARF error: data count (%" PRIx64 ") larger than buffer size"),
2625 (uint64_t) data_count);
2626 bfd_set_error (bfd_error_bad_value);
2627 return false;
2630 for (datai = 0; datai < data_count; datai++)
2632 bfd_byte *format = format_header_data;
2633 struct fileinfo fe;
2635 memset (&fe, 0, sizeof fe);
2636 for (formati = 0; formati < format_count; formati++)
2638 bfd_vma content_type, form;
2639 char *string_trash;
2640 char **stringp = &string_trash;
2641 unsigned int uint_trash, *uintp = &uint_trash;
2642 struct attribute attr;
2644 content_type = _bfd_safe_read_leb128 (abfd, &format, false, buf_end);
2645 switch (content_type)
2647 case DW_LNCT_path:
2648 stringp = &fe.name;
2649 break;
2650 case DW_LNCT_directory_index:
2651 uintp = &fe.dir;
2652 break;
2653 case DW_LNCT_timestamp:
2654 uintp = &fe.time;
2655 break;
2656 case DW_LNCT_size:
2657 uintp = &fe.size;
2658 break;
2659 case DW_LNCT_MD5:
2660 break;
2661 default:
2662 _bfd_error_handler
2663 (_("DWARF error: unknown format content type %" PRIu64),
2664 (uint64_t) content_type);
2665 bfd_set_error (bfd_error_bad_value);
2666 return false;
2669 form = _bfd_safe_read_leb128 (abfd, &format, false, buf_end);
2670 buf = read_attribute_value (&attr, form, 0, unit, buf, buf_end);
2671 if (buf == NULL)
2672 return false;
2673 switch (form)
2675 case DW_FORM_string:
2676 case DW_FORM_line_strp:
2677 case DW_FORM_strx:
2678 case DW_FORM_strx1:
2679 case DW_FORM_strx2:
2680 case DW_FORM_strx3:
2681 case DW_FORM_strx4:
2682 *stringp = attr.u.str;
2683 break;
2685 case DW_FORM_data1:
2686 case DW_FORM_data2:
2687 case DW_FORM_data4:
2688 case DW_FORM_data8:
2689 case DW_FORM_udata:
2690 *uintp = attr.u.val;
2691 break;
2693 case DW_FORM_data16:
2694 /* MD5 data is in the attr.blk, but we are ignoring those. */
2695 break;
2699 if (!callback (table, fe.name, fe.dir, fe.time, fe.size))
2700 return false;
2703 *bufp = buf;
2704 return true;
2707 /* Decode the line number information for UNIT. */
2709 static struct line_info_table*
2710 decode_line_info (struct comp_unit *unit)
2712 bfd *abfd = unit->abfd;
2713 struct dwarf2_debug *stash = unit->stash;
2714 struct dwarf2_debug_file *file = unit->file;
2715 struct line_info_table* table;
2716 bfd_byte *line_ptr;
2717 bfd_byte *line_end;
2718 struct line_head lh;
2719 unsigned int i, offset_size;
2720 char *cur_file, *cur_dir;
2721 unsigned char op_code, extended_op, adj_opcode;
2722 unsigned int exop_len;
2723 size_t amt;
2725 if (unit->line_offset == 0 && file->line_table)
2726 return file->line_table;
2728 if (! read_section (abfd, &stash->debug_sections[debug_line],
2729 file->syms, unit->line_offset,
2730 &file->dwarf_line_buffer, &file->dwarf_line_size))
2731 return NULL;
2733 if (file->dwarf_line_size < 16)
2735 _bfd_error_handler
2736 (_("DWARF error: line info section is too small (%" PRId64 ")"),
2737 (int64_t) file->dwarf_line_size);
2738 bfd_set_error (bfd_error_bad_value);
2739 return NULL;
2741 line_ptr = file->dwarf_line_buffer + unit->line_offset;
2742 line_end = file->dwarf_line_buffer + file->dwarf_line_size;
2744 /* Read in the prologue. */
2745 lh.total_length = read_4_bytes (abfd, &line_ptr, line_end);
2746 offset_size = 4;
2747 if (lh.total_length == 0xffffffff)
2749 lh.total_length = read_8_bytes (abfd, &line_ptr, line_end);
2750 offset_size = 8;
2752 else if (lh.total_length == 0 && unit->addr_size == 8)
2754 /* Handle (non-standard) 64-bit DWARF2 formats. */
2755 lh.total_length = read_4_bytes (abfd, &line_ptr, line_end);
2756 offset_size = 8;
2759 if (lh.total_length > (size_t) (line_end - line_ptr))
2761 _bfd_error_handler
2762 /* xgettext: c-format */
2763 (_("DWARF error: line info data is bigger (%#" PRIx64 ")"
2764 " than the space remaining in the section (%#lx)"),
2765 (uint64_t) lh.total_length, (unsigned long) (line_end - line_ptr));
2766 bfd_set_error (bfd_error_bad_value);
2767 return NULL;
2770 line_end = line_ptr + lh.total_length;
2772 lh.version = read_2_bytes (abfd, &line_ptr, line_end);
2773 if (lh.version < 2 || lh.version > 5)
2775 _bfd_error_handler
2776 (_("DWARF error: unhandled .debug_line version %d"), lh.version);
2777 bfd_set_error (bfd_error_bad_value);
2778 return NULL;
2781 if (line_ptr + offset_size + (lh.version >= 5 ? 8 : (lh.version >= 4 ? 6 : 5))
2782 >= line_end)
2784 _bfd_error_handler
2785 (_("DWARF error: ran out of room reading prologue"));
2786 bfd_set_error (bfd_error_bad_value);
2787 return NULL;
2790 if (lh.version >= 5)
2792 unsigned int segment_selector_size;
2794 /* Skip address size. */
2795 read_1_byte (abfd, &line_ptr, line_end);
2797 segment_selector_size = read_1_byte (abfd, &line_ptr, line_end);
2798 if (segment_selector_size != 0)
2800 _bfd_error_handler
2801 (_("DWARF error: line info unsupported segment selector size %u"),
2802 segment_selector_size);
2803 bfd_set_error (bfd_error_bad_value);
2804 return NULL;
2808 if (offset_size == 4)
2809 lh.prologue_length = read_4_bytes (abfd, &line_ptr, line_end);
2810 else
2811 lh.prologue_length = read_8_bytes (abfd, &line_ptr, line_end);
2813 lh.minimum_instruction_length = read_1_byte (abfd, &line_ptr, line_end);
2815 if (lh.version >= 4)
2816 lh.maximum_ops_per_insn = read_1_byte (abfd, &line_ptr, line_end);
2817 else
2818 lh.maximum_ops_per_insn = 1;
2820 if (lh.maximum_ops_per_insn == 0)
2822 _bfd_error_handler
2823 (_("DWARF error: invalid maximum operations per instruction"));
2824 bfd_set_error (bfd_error_bad_value);
2825 return NULL;
2828 lh.default_is_stmt = read_1_byte (abfd, &line_ptr, line_end);
2829 lh.line_base = read_1_signed_byte (abfd, &line_ptr, line_end);
2830 lh.line_range = read_1_byte (abfd, &line_ptr, line_end);
2831 lh.opcode_base = read_1_byte (abfd, &line_ptr, line_end);
2833 if (line_ptr + (lh.opcode_base - 1) >= line_end)
2835 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2836 bfd_set_error (bfd_error_bad_value);
2837 return NULL;
2840 amt = lh.opcode_base * sizeof (unsigned char);
2841 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
2843 lh.standard_opcode_lengths[0] = 1;
2845 for (i = 1; i < lh.opcode_base; ++i)
2846 lh.standard_opcode_lengths[i] = read_1_byte (abfd, &line_ptr, line_end);
2848 amt = sizeof (struct line_info_table);
2849 table = (struct line_info_table *) bfd_alloc (abfd, amt);
2850 if (table == NULL)
2851 return NULL;
2852 table->abfd = abfd;
2853 table->comp_dir = unit->comp_dir;
2855 table->num_files = 0;
2856 table->files = NULL;
2858 table->num_dirs = 0;
2859 table->dirs = NULL;
2861 table->num_sequences = 0;
2862 table->sequences = NULL;
2864 table->lcl_head = NULL;
2866 if (lh.version >= 5)
2868 /* Read directory table. */
2869 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2870 line_info_add_include_dir_stub))
2871 goto fail;
2873 /* Read file name table. */
2874 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2875 line_info_add_file_name))
2876 goto fail;
2877 table->use_dir_and_file_0 = true;
2879 else
2881 /* Read directory table. */
2882 while ((cur_dir = read_string (&line_ptr, line_end)) != NULL)
2884 if (!line_info_add_include_dir (table, cur_dir))
2885 goto fail;
2888 /* Read file name table. */
2889 while ((cur_file = read_string (&line_ptr, line_end)) != NULL)
2891 unsigned int dir, xtime, size;
2893 dir = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2894 xtime = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2895 size = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2897 if (!line_info_add_file_name (table, cur_file, dir, xtime, size))
2898 goto fail;
2900 table->use_dir_and_file_0 = false;
2903 /* Read the statement sequences until there's nothing left. */
2904 while (line_ptr < line_end)
2906 /* State machine registers. */
2907 bfd_vma address = 0;
2908 unsigned char op_index = 0;
2909 char * filename = NULL;
2910 unsigned int line = 1;
2911 unsigned int column = 0;
2912 unsigned int discriminator = 0;
2913 int is_stmt = lh.default_is_stmt;
2914 int end_sequence = 0;
2915 unsigned int dir, xtime, size;
2916 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2917 compilers generate address sequences that are wildly out of
2918 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2919 for ia64-Linux). Thus, to determine the low and high
2920 address, we must compare on every DW_LNS_copy, etc. */
2921 bfd_vma low_pc = (bfd_vma) -1;
2922 bfd_vma high_pc = 0;
2924 if (table->num_files)
2926 /* PR 30783: Always start with a file index of 1, even
2927 for DWARF-5. */
2928 filename = concat_filename (table, 1);
2931 /* Decode the table. */
2932 while (!end_sequence && line_ptr < line_end)
2934 op_code = read_1_byte (abfd, &line_ptr, line_end);
2936 if (op_code >= lh.opcode_base)
2938 /* Special operand. */
2939 adj_opcode = op_code - lh.opcode_base;
2940 if (lh.line_range == 0)
2941 goto line_fail;
2942 if (lh.maximum_ops_per_insn == 1)
2943 address += (adj_opcode / lh.line_range
2944 * lh.minimum_instruction_length);
2945 else
2947 address += ((op_index + adj_opcode / lh.line_range)
2948 / lh.maximum_ops_per_insn
2949 * lh.minimum_instruction_length);
2950 op_index = ((op_index + adj_opcode / lh.line_range)
2951 % lh.maximum_ops_per_insn);
2953 line += lh.line_base + (adj_opcode % lh.line_range);
2954 /* Append row to matrix using current values. */
2955 if (!add_line_info (table, address, op_index, filename,
2956 line, column, discriminator, 0))
2957 goto line_fail;
2958 discriminator = 0;
2959 if (address < low_pc)
2960 low_pc = address;
2961 if (address > high_pc)
2962 high_pc = address;
2964 else switch (op_code)
2966 case DW_LNS_extended_op:
2967 exop_len = _bfd_safe_read_leb128 (abfd, &line_ptr,
2968 false, line_end);
2969 extended_op = read_1_byte (abfd, &line_ptr, line_end);
2971 switch (extended_op)
2973 case DW_LNE_end_sequence:
2974 end_sequence = 1;
2975 if (!add_line_info (table, address, op_index, filename, line,
2976 column, discriminator, end_sequence))
2977 goto line_fail;
2978 discriminator = 0;
2979 if (address < low_pc)
2980 low_pc = address;
2981 if (address > high_pc)
2982 high_pc = address;
2983 if (!arange_add (unit, &unit->arange, &unit->file->trie_root,
2984 low_pc, high_pc))
2985 goto line_fail;
2986 break;
2987 case DW_LNE_set_address:
2988 address = read_address (unit, &line_ptr, line_end);
2989 op_index = 0;
2990 break;
2991 case DW_LNE_define_file:
2992 cur_file = read_string (&line_ptr, line_end);
2993 dir = _bfd_safe_read_leb128 (abfd, &line_ptr,
2994 false, line_end);
2995 xtime = _bfd_safe_read_leb128 (abfd, &line_ptr,
2996 false, line_end);
2997 size = _bfd_safe_read_leb128 (abfd, &line_ptr,
2998 false, line_end);
2999 if (!line_info_add_file_name (table, cur_file, dir,
3000 xtime, size))
3001 goto line_fail;
3002 break;
3003 case DW_LNE_set_discriminator:
3004 discriminator = _bfd_safe_read_leb128 (abfd, &line_ptr,
3005 false, line_end);
3006 break;
3007 case DW_LNE_HP_source_file_correlation:
3008 line_ptr += exop_len - 1;
3009 break;
3010 default:
3011 _bfd_error_handler
3012 (_("DWARF error: mangled line number section"));
3013 bfd_set_error (bfd_error_bad_value);
3014 line_fail:
3015 free (filename);
3016 goto fail;
3018 break;
3019 case DW_LNS_copy:
3020 if (!add_line_info (table, address, op_index,
3021 filename, line, column, discriminator, 0))
3022 goto line_fail;
3023 discriminator = 0;
3024 if (address < low_pc)
3025 low_pc = address;
3026 if (address > high_pc)
3027 high_pc = address;
3028 break;
3029 case DW_LNS_advance_pc:
3030 if (lh.maximum_ops_per_insn == 1)
3031 address += (lh.minimum_instruction_length
3032 * _bfd_safe_read_leb128 (abfd, &line_ptr,
3033 false, line_end));
3034 else
3036 bfd_vma adjust = _bfd_safe_read_leb128 (abfd, &line_ptr,
3037 false, line_end);
3038 address = ((op_index + adjust) / lh.maximum_ops_per_insn
3039 * lh.minimum_instruction_length);
3040 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
3042 break;
3043 case DW_LNS_advance_line:
3044 line += _bfd_safe_read_leb128 (abfd, &line_ptr,
3045 true, line_end);
3046 break;
3047 case DW_LNS_set_file:
3049 unsigned int filenum;
3051 /* The file and directory tables are 0
3052 based, the references are 1 based. */
3053 filenum = _bfd_safe_read_leb128 (abfd, &line_ptr,
3054 false, line_end);
3055 free (filename);
3056 filename = concat_filename (table, filenum);
3057 break;
3059 case DW_LNS_set_column:
3060 column = _bfd_safe_read_leb128 (abfd, &line_ptr,
3061 false, line_end);
3062 break;
3063 case DW_LNS_negate_stmt:
3064 is_stmt = (!is_stmt);
3065 break;
3066 case DW_LNS_set_basic_block:
3067 break;
3068 case DW_LNS_const_add_pc:
3069 if (lh.line_range == 0)
3070 goto line_fail;
3071 if (lh.maximum_ops_per_insn == 1)
3072 address += (lh.minimum_instruction_length
3073 * ((255 - lh.opcode_base) / lh.line_range));
3074 else
3076 bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
3077 address += (lh.minimum_instruction_length
3078 * ((op_index + adjust)
3079 / lh.maximum_ops_per_insn));
3080 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
3082 break;
3083 case DW_LNS_fixed_advance_pc:
3084 address += read_2_bytes (abfd, &line_ptr, line_end);
3085 op_index = 0;
3086 break;
3087 default:
3088 /* Unknown standard opcode, ignore it. */
3089 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
3090 (void) _bfd_safe_read_leb128 (abfd, &line_ptr,
3091 false, line_end);
3092 break;
3096 free (filename);
3099 if (unit->line_offset == 0)
3100 file->line_table = table;
3101 if (sort_line_sequences (table))
3102 return table;
3104 fail:
3105 while (table->sequences != NULL)
3107 struct line_sequence* seq = table->sequences;
3108 table->sequences = table->sequences->prev_sequence;
3109 free (seq);
3111 free (table->files);
3112 free (table->dirs);
3113 return NULL;
3116 /* If ADDR is within TABLE set the output parameters and return TRUE,
3117 otherwise set *FILENAME_PTR to NULL and return FALSE.
3118 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
3119 are pointers to the objects to be filled in. */
3121 static bool
3122 lookup_address_in_line_info_table (struct line_info_table *table,
3123 bfd_vma addr,
3124 const char **filename_ptr,
3125 unsigned int *linenumber_ptr,
3126 unsigned int *discriminator_ptr)
3128 struct line_sequence *seq = NULL;
3129 struct line_info *info;
3130 int low, high, mid;
3132 /* Binary search the array of sequences. */
3133 low = 0;
3134 high = table->num_sequences;
3135 while (low < high)
3137 mid = (low + high) / 2;
3138 seq = &table->sequences[mid];
3139 if (addr < seq->low_pc)
3140 high = mid;
3141 else if (addr >= seq->last_line->address)
3142 low = mid + 1;
3143 else
3144 break;
3147 /* Check for a valid sequence. */
3148 if (!seq || addr < seq->low_pc || addr >= seq->last_line->address)
3149 goto fail;
3151 if (!build_line_info_table (table, seq))
3152 goto fail;
3154 /* Binary search the array of line information. */
3155 low = 0;
3156 high = seq->num_lines;
3157 info = NULL;
3158 while (low < high)
3160 mid = (low + high) / 2;
3161 info = seq->line_info_lookup[mid];
3162 if (addr < info->address)
3163 high = mid;
3164 else if (addr >= seq->line_info_lookup[mid + 1]->address)
3165 low = mid + 1;
3166 else
3167 break;
3170 /* Check for a valid line information entry. */
3171 if (info
3172 && addr >= info->address
3173 && addr < seq->line_info_lookup[mid + 1]->address
3174 && !(info->end_sequence || info == seq->last_line))
3176 *filename_ptr = info->filename;
3177 *linenumber_ptr = info->line;
3178 if (discriminator_ptr)
3179 *discriminator_ptr = info->discriminator;
3180 return true;
3183 fail:
3184 *filename_ptr = NULL;
3185 return false;
3188 /* Read in the .debug_ranges section for future reference. */
3190 static bool
3191 read_debug_ranges (struct comp_unit * unit)
3193 struct dwarf2_debug *stash = unit->stash;
3194 struct dwarf2_debug_file *file = unit->file;
3196 return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
3197 file->syms, 0,
3198 &file->dwarf_ranges_buffer, &file->dwarf_ranges_size);
3201 /* Read in the .debug_rnglists section for future reference. */
3203 static bool
3204 read_debug_rnglists (struct comp_unit * unit)
3206 struct dwarf2_debug *stash = unit->stash;
3207 struct dwarf2_debug_file *file = unit->file;
3209 return read_section (unit->abfd, &stash->debug_sections[debug_rnglists],
3210 file->syms, 0,
3211 &file->dwarf_rnglists_buffer, &file->dwarf_rnglists_size);
3214 /* Function table functions. */
3216 static int
3217 compare_lookup_funcinfos (const void * a, const void * b)
3219 const struct lookup_funcinfo * lookup1 = a;
3220 const struct lookup_funcinfo * lookup2 = b;
3222 if (lookup1->low_addr < lookup2->low_addr)
3223 return -1;
3224 if (lookup1->low_addr > lookup2->low_addr)
3225 return 1;
3226 if (lookup1->high_addr < lookup2->high_addr)
3227 return -1;
3228 if (lookup1->high_addr > lookup2->high_addr)
3229 return 1;
3231 if (lookup1->idx < lookup2->idx)
3232 return -1;
3233 if (lookup1->idx > lookup2->idx)
3234 return 1;
3235 return 0;
3238 static bool
3239 build_lookup_funcinfo_table (struct comp_unit * unit)
3241 struct lookup_funcinfo *lookup_funcinfo_table = unit->lookup_funcinfo_table;
3242 unsigned int number_of_functions = unit->number_of_functions;
3243 struct funcinfo *each;
3244 struct lookup_funcinfo *entry;
3245 size_t func_index;
3246 struct arange *range;
3247 bfd_vma low_addr, high_addr;
3249 if (lookup_funcinfo_table || number_of_functions == 0)
3250 return true;
3252 /* Create the function info lookup table. */
3253 lookup_funcinfo_table = (struct lookup_funcinfo *)
3254 bfd_malloc (number_of_functions * sizeof (struct lookup_funcinfo));
3255 if (lookup_funcinfo_table == NULL)
3256 return false;
3258 /* Populate the function info lookup table. */
3259 func_index = number_of_functions;
3260 for (each = unit->function_table; each; each = each->prev_func)
3262 entry = &lookup_funcinfo_table[--func_index];
3263 entry->funcinfo = each;
3264 entry->idx = func_index;
3266 /* Calculate the lowest and highest address for this function entry. */
3267 low_addr = entry->funcinfo->arange.low;
3268 high_addr = entry->funcinfo->arange.high;
3270 for (range = entry->funcinfo->arange.next; range; range = range->next)
3272 if (range->low < low_addr)
3273 low_addr = range->low;
3274 if (range->high > high_addr)
3275 high_addr = range->high;
3278 entry->low_addr = low_addr;
3279 entry->high_addr = high_addr;
3282 BFD_ASSERT (func_index == 0);
3284 /* Sort the function by address. */
3285 qsort (lookup_funcinfo_table,
3286 number_of_functions,
3287 sizeof (struct lookup_funcinfo),
3288 compare_lookup_funcinfos);
3290 /* Calculate the high watermark for each function in the lookup table. */
3291 high_addr = lookup_funcinfo_table[0].high_addr;
3292 for (func_index = 1; func_index < number_of_functions; func_index++)
3294 entry = &lookup_funcinfo_table[func_index];
3295 if (entry->high_addr > high_addr)
3296 high_addr = entry->high_addr;
3297 else
3298 entry->high_addr = high_addr;
3301 unit->lookup_funcinfo_table = lookup_funcinfo_table;
3302 return true;
3305 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
3306 TRUE. Note that we need to find the function that has the smallest range
3307 that contains ADDR, to handle inlined functions without depending upon
3308 them being ordered in TABLE by increasing range. */
3310 static bool
3311 lookup_address_in_function_table (struct comp_unit *unit,
3312 bfd_vma addr,
3313 struct funcinfo **function_ptr)
3315 unsigned int number_of_functions = unit->number_of_functions;
3316 struct lookup_funcinfo* lookup_funcinfo = NULL;
3317 struct funcinfo* funcinfo = NULL;
3318 struct funcinfo* best_fit = NULL;
3319 bfd_vma best_fit_len = (bfd_vma) -1;
3320 bfd_size_type low, high, mid, first;
3321 struct arange *arange;
3323 if (number_of_functions == 0)
3324 return false;
3326 if (!build_lookup_funcinfo_table (unit))
3327 return false;
3329 if (unit->lookup_funcinfo_table[number_of_functions - 1].high_addr < addr)
3330 return false;
3332 /* Find the first function in the lookup table which may contain the
3333 specified address. */
3334 low = 0;
3335 high = number_of_functions;
3336 first = high;
3337 while (low < high)
3339 mid = (low + high) / 2;
3340 lookup_funcinfo = &unit->lookup_funcinfo_table[mid];
3341 if (addr < lookup_funcinfo->low_addr)
3342 high = mid;
3343 else if (addr >= lookup_funcinfo->high_addr)
3344 low = mid + 1;
3345 else
3346 high = first = mid;
3349 /* Find the 'best' match for the address. The prior algorithm defined the
3350 best match as the function with the smallest address range containing
3351 the specified address. This definition should probably be changed to the
3352 innermost inline routine containing the address, but right now we want
3353 to get the same results we did before. */
3354 while (first < number_of_functions)
3356 if (addr < unit->lookup_funcinfo_table[first].low_addr)
3357 break;
3358 funcinfo = unit->lookup_funcinfo_table[first].funcinfo;
3360 for (arange = &funcinfo->arange; arange; arange = arange->next)
3362 if (addr < arange->low || addr >= arange->high)
3363 continue;
3365 if (arange->high - arange->low < best_fit_len
3366 /* The following comparison is designed to return the same
3367 match as the previous algorithm for routines which have the
3368 same best fit length. */
3369 || (arange->high - arange->low == best_fit_len
3370 && funcinfo > best_fit))
3372 best_fit = funcinfo;
3373 best_fit_len = arange->high - arange->low;
3377 first++;
3380 if (!best_fit)
3381 return false;
3383 *function_ptr = best_fit;
3384 return true;
3387 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
3388 and LINENUMBER_PTR, and return TRUE. */
3390 static bool
3391 lookup_symbol_in_function_table (struct comp_unit *unit,
3392 asymbol *sym,
3393 bfd_vma addr,
3394 const char **filename_ptr,
3395 unsigned int *linenumber_ptr)
3397 struct funcinfo* each;
3398 struct funcinfo* best_fit = NULL;
3399 bfd_vma best_fit_len = (bfd_vma) -1;
3400 struct arange *arange;
3401 const char *name = bfd_asymbol_name (sym);
3403 for (each = unit->function_table; each; each = each->prev_func)
3404 for (arange = &each->arange; arange; arange = arange->next)
3405 if (addr >= arange->low
3406 && addr < arange->high
3407 && arange->high - arange->low < best_fit_len
3408 && each->file
3409 && each->name
3410 && strstr (name, each->name) != NULL)
3412 best_fit = each;
3413 best_fit_len = arange->high - arange->low;
3416 if (best_fit)
3418 *filename_ptr = best_fit->file;
3419 *linenumber_ptr = best_fit->line;
3420 return true;
3423 return false;
3426 /* Variable table functions. */
3428 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
3429 LINENUMBER_PTR, and return TRUE. */
3431 static bool
3432 lookup_symbol_in_variable_table (struct comp_unit *unit,
3433 asymbol *sym,
3434 bfd_vma addr,
3435 const char **filename_ptr,
3436 unsigned int *linenumber_ptr)
3438 struct varinfo* each;
3439 const char *name = bfd_asymbol_name (sym);
3441 for (each = unit->variable_table; each; each = each->prev_var)
3442 if (each->addr == addr
3443 && !each->stack
3444 && each->file != NULL
3445 && each->name != NULL
3446 && strstr (name, each->name) != NULL)
3447 break;
3449 if (each)
3451 *filename_ptr = each->file;
3452 *linenumber_ptr = each->line;
3453 return true;
3456 return false;
3459 static struct comp_unit *stash_comp_unit (struct dwarf2_debug *,
3460 struct dwarf2_debug_file *);
3461 static bool comp_unit_maybe_decode_line_info (struct comp_unit *);
3463 static bool
3464 find_abstract_instance (struct comp_unit *unit,
3465 struct attribute *attr_ptr,
3466 unsigned int recur_count,
3467 const char **pname,
3468 bool *is_linkage,
3469 char **filename_ptr,
3470 int *linenumber_ptr)
3472 bfd *abfd = unit->abfd;
3473 bfd_byte *info_ptr = NULL;
3474 bfd_byte *info_ptr_end;
3475 unsigned int abbrev_number, i;
3476 struct abbrev_info *abbrev;
3477 uint64_t die_ref = attr_ptr->u.val;
3478 struct attribute attr;
3480 if (recur_count == 100)
3482 _bfd_error_handler
3483 (_("DWARF error: abstract instance recursion detected"));
3484 bfd_set_error (bfd_error_bad_value);
3485 return false;
3488 /* DW_FORM_ref_addr can reference an entry in a different CU. It
3489 is an offset from the .debug_info section, not the current CU. */
3490 if (attr_ptr->form == DW_FORM_ref_addr)
3492 /* We only support DW_FORM_ref_addr within the same file, so
3493 any relocations should be resolved already. Check this by
3494 testing for a zero die_ref; There can't be a valid reference
3495 to the header of a .debug_info section.
3496 DW_FORM_ref_addr is an offset relative to .debug_info.
3497 Normally when using the GNU linker this is accomplished by
3498 emitting a symbolic reference to a label, because .debug_info
3499 sections are linked at zero. When there are multiple section
3500 groups containing .debug_info, as there might be in a
3501 relocatable object file, it would be reasonable to assume that
3502 a symbolic reference to a label in any .debug_info section
3503 might be used. Since we lay out multiple .debug_info
3504 sections at non-zero VMAs (see place_sections), and read
3505 them contiguously into dwarf_info_buffer, that means the
3506 reference is relative to dwarf_info_buffer. */
3507 size_t total;
3509 info_ptr = unit->file->dwarf_info_buffer;
3510 info_ptr_end = info_ptr + unit->file->dwarf_info_size;
3511 total = info_ptr_end - info_ptr;
3512 if (!die_ref)
3513 return true;
3514 else if (die_ref >= total)
3516 _bfd_error_handler
3517 (_("DWARF error: invalid abstract instance DIE ref"));
3518 bfd_set_error (bfd_error_bad_value);
3519 return false;
3521 info_ptr += die_ref;
3523 else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
3525 bool first_time = unit->stash->alt.dwarf_info_buffer == NULL;
3527 info_ptr = read_alt_indirect_ref (unit, die_ref);
3528 if (first_time)
3529 unit->stash->alt.info_ptr = unit->stash->alt.dwarf_info_buffer;
3530 if (info_ptr == NULL)
3532 _bfd_error_handler
3533 (_("DWARF error: unable to read alt ref %" PRIu64),
3534 (uint64_t) die_ref);
3535 bfd_set_error (bfd_error_bad_value);
3536 return false;
3538 info_ptr_end = (unit->stash->alt.dwarf_info_buffer
3539 + unit->stash->alt.dwarf_info_size);
3540 if (unit->stash->alt.all_comp_units)
3541 unit = unit->stash->alt.all_comp_units;
3544 if (attr_ptr->form == DW_FORM_ref_addr
3545 || attr_ptr->form == DW_FORM_GNU_ref_alt)
3547 /* Now find the CU containing this pointer. */
3548 if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr)
3549 info_ptr_end = unit->end_ptr;
3550 else
3552 /* Check other CUs to see if they contain the abbrev. */
3553 struct comp_unit *u = NULL;
3554 struct addr_range range = { info_ptr, info_ptr };
3555 splay_tree_node v = splay_tree_lookup (unit->file->comp_unit_tree,
3556 (splay_tree_key)&range);
3557 if (v != NULL)
3558 u = (struct comp_unit *)v->value;
3560 if (attr_ptr->form == DW_FORM_ref_addr)
3561 while (u == NULL)
3563 u = stash_comp_unit (unit->stash, &unit->stash->f);
3564 if (u == NULL)
3565 break;
3566 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3567 break;
3568 u = NULL;
3571 if (attr_ptr->form == DW_FORM_GNU_ref_alt)
3572 while (u == NULL)
3574 u = stash_comp_unit (unit->stash, &unit->stash->alt);
3575 if (u == NULL)
3576 break;
3577 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3578 break;
3579 u = NULL;
3582 if (u == NULL)
3584 _bfd_error_handler
3585 (_("DWARF error: unable to locate abstract instance DIE ref %"
3586 PRIu64), (uint64_t) die_ref);
3587 bfd_set_error (bfd_error_bad_value);
3588 return false;
3590 unit = u;
3591 info_ptr_end = unit->end_ptr;
3594 else
3596 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
3597 DW_FORM_ref_udata. These are all references relative to the
3598 start of the current CU. */
3599 size_t total;
3601 info_ptr = unit->info_ptr_unit;
3602 info_ptr_end = unit->end_ptr;
3603 total = info_ptr_end - info_ptr;
3604 if (!die_ref || die_ref >= total)
3606 _bfd_error_handler
3607 (_("DWARF error: invalid abstract instance DIE ref"));
3608 bfd_set_error (bfd_error_bad_value);
3609 return false;
3611 info_ptr += die_ref;
3614 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
3615 false, info_ptr_end);
3616 if (abbrev_number)
3618 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3619 if (! abbrev)
3621 _bfd_error_handler
3622 (_("DWARF error: could not find abbrev number %u"), abbrev_number);
3623 bfd_set_error (bfd_error_bad_value);
3624 return false;
3626 else
3628 for (i = 0; i < abbrev->num_attrs; ++i)
3630 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
3631 info_ptr, info_ptr_end);
3632 if (info_ptr == NULL)
3633 break;
3634 switch (attr.name)
3636 case DW_AT_name:
3637 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3638 over DW_AT_name. */
3639 if (*pname == NULL && is_str_form (&attr))
3641 *pname = attr.u.str;
3642 if (mangle_style (unit->lang) == 0)
3643 *is_linkage = true;
3645 break;
3646 case DW_AT_specification:
3647 if (is_int_form (&attr)
3648 && !find_abstract_instance (unit, &attr, recur_count + 1,
3649 pname, is_linkage,
3650 filename_ptr, linenumber_ptr))
3651 return false;
3652 break;
3653 case DW_AT_linkage_name:
3654 case DW_AT_MIPS_linkage_name:
3655 /* PR 16949: Corrupt debug info can place
3656 non-string forms into these attributes. */
3657 if (is_str_form (&attr))
3659 *pname = attr.u.str;
3660 *is_linkage = true;
3662 break;
3663 case DW_AT_decl_file:
3664 if (!comp_unit_maybe_decode_line_info (unit))
3665 return false;
3666 if (is_int_form (&attr))
3668 free (*filename_ptr);
3669 *filename_ptr = concat_filename (unit->line_table,
3670 attr.u.val);
3672 break;
3673 case DW_AT_decl_line:
3674 if (is_int_form (&attr))
3675 *linenumber_ptr = attr.u.val;
3676 break;
3677 default:
3678 break;
3683 return true;
3686 static bool
3687 read_ranges (struct comp_unit *unit, struct arange *arange,
3688 struct trie_node **trie_root, uint64_t offset)
3690 bfd_byte *ranges_ptr;
3691 bfd_byte *ranges_end;
3692 bfd_vma base_address = unit->base_address;
3694 if (! unit->file->dwarf_ranges_buffer)
3696 if (! read_debug_ranges (unit))
3697 return false;
3700 if (offset > unit->file->dwarf_ranges_size)
3701 return false;
3702 ranges_ptr = unit->file->dwarf_ranges_buffer + offset;
3703 ranges_end = unit->file->dwarf_ranges_buffer + unit->file->dwarf_ranges_size;
3705 for (;;)
3707 bfd_vma low_pc;
3708 bfd_vma high_pc;
3710 /* PR 17512: file: 62cada7d. */
3711 if (2u * unit->addr_size > (size_t) (ranges_end - ranges_ptr))
3712 return false;
3714 low_pc = read_address (unit, &ranges_ptr, ranges_end);
3715 high_pc = read_address (unit, &ranges_ptr, ranges_end);
3717 if (low_pc == 0 && high_pc == 0)
3718 break;
3719 if (low_pc == (bfd_vma) -1 && high_pc != (bfd_vma) -1)
3720 base_address = high_pc;
3721 else
3723 if (!arange_add (unit, arange, trie_root,
3724 base_address + low_pc, base_address + high_pc))
3725 return false;
3728 return true;
3731 static bool
3732 read_rnglists (struct comp_unit *unit, struct arange *arange,
3733 struct trie_node **trie_root, uint64_t offset)
3735 bfd_byte *rngs_ptr;
3736 bfd_byte *rngs_end;
3737 bfd_vma base_address = unit->base_address;
3738 bfd_vma low_pc;
3739 bfd_vma high_pc;
3740 bfd *abfd = unit->abfd;
3742 if (! unit->file->dwarf_rnglists_buffer)
3744 if (! read_debug_rnglists (unit))
3745 return false;
3748 rngs_ptr = unit->file->dwarf_rnglists_buffer + offset;
3749 if (rngs_ptr < unit->file->dwarf_rnglists_buffer)
3750 return false;
3751 rngs_end = unit->file->dwarf_rnglists_buffer;
3752 rngs_end += unit->file->dwarf_rnglists_size;
3754 for (;;)
3756 enum dwarf_range_list_entry rlet;
3758 if (rngs_ptr >= rngs_end)
3759 return false;
3761 rlet = read_1_byte (abfd, &rngs_ptr, rngs_end);
3763 switch (rlet)
3765 case DW_RLE_end_of_list:
3766 return true;
3768 case DW_RLE_base_address:
3769 if (unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3770 return false;
3771 base_address = read_address (unit, &rngs_ptr, rngs_end);
3772 continue;
3774 case DW_RLE_start_length:
3775 if (unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3776 return false;
3777 low_pc = read_address (unit, &rngs_ptr, rngs_end);
3778 high_pc = low_pc;
3779 high_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3780 false, rngs_end);
3781 break;
3783 case DW_RLE_offset_pair:
3784 low_pc = base_address;
3785 low_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3786 false, rngs_end);
3787 high_pc = base_address;
3788 high_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3789 false, rngs_end);
3790 break;
3792 case DW_RLE_start_end:
3793 if (2u * unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3794 return false;
3795 low_pc = read_address (unit, &rngs_ptr, rngs_end);
3796 high_pc = read_address (unit, &rngs_ptr, rngs_end);
3797 break;
3799 /* TODO x-variants need .debug_addr support used for split-dwarf. */
3800 case DW_RLE_base_addressx:
3801 case DW_RLE_startx_endx:
3802 case DW_RLE_startx_length:
3803 default:
3804 return false;
3807 if (!arange_add (unit, arange, trie_root, low_pc, high_pc))
3808 return false;
3812 static bool
3813 read_rangelist (struct comp_unit *unit, struct arange *arange,
3814 struct trie_node **trie_root, uint64_t offset)
3816 if (unit->version <= 4)
3817 return read_ranges (unit, arange, trie_root, offset);
3818 else
3819 return read_rnglists (unit, arange, trie_root, offset);
3822 static struct funcinfo *
3823 lookup_func_by_offset (uint64_t offset, struct funcinfo * table)
3825 for (; table != NULL; table = table->prev_func)
3826 if (table->unit_offset == offset)
3827 return table;
3828 return NULL;
3831 static struct varinfo *
3832 lookup_var_by_offset (uint64_t offset, struct varinfo * table)
3834 while (table)
3836 if (table->unit_offset == offset)
3837 return table;
3838 table = table->prev_var;
3841 return NULL;
3845 /* DWARF2 Compilation unit functions. */
3847 static struct funcinfo *
3848 reverse_funcinfo_list (struct funcinfo *head)
3850 struct funcinfo *rhead;
3851 struct funcinfo *temp;
3853 for (rhead = NULL; head; head = temp)
3855 temp = head->prev_func;
3856 head->prev_func = rhead;
3857 rhead = head;
3859 return rhead;
3862 static struct varinfo *
3863 reverse_varinfo_list (struct varinfo *head)
3865 struct varinfo *rhead;
3866 struct varinfo *temp;
3868 for (rhead = NULL; head; head = temp)
3870 temp = head->prev_var;
3871 head->prev_var = rhead;
3872 rhead = head;
3874 return rhead;
3877 /* Scan over each die in a comp. unit looking for functions to add
3878 to the function table and variables to the variable table. */
3880 static bool
3881 scan_unit_for_symbols (struct comp_unit *unit)
3883 bfd *abfd = unit->abfd;
3884 bfd_byte *info_ptr = unit->first_child_die_ptr;
3885 bfd_byte *info_ptr_end = unit->end_ptr;
3886 int nesting_level = 0;
3887 struct nest_funcinfo
3889 struct funcinfo *func;
3890 } *nested_funcs;
3891 int nested_funcs_size;
3892 struct funcinfo *last_func;
3893 struct varinfo *last_var;
3895 /* Maintain a stack of in-scope functions and inlined functions, which we
3896 can use to set the caller_func field. */
3897 nested_funcs_size = 32;
3898 nested_funcs = (struct nest_funcinfo *)
3899 bfd_malloc (nested_funcs_size * sizeof (*nested_funcs));
3900 if (nested_funcs == NULL)
3901 return false;
3902 nested_funcs[nesting_level].func = 0;
3904 /* PR 27484: We must scan the DIEs twice. The first time we look for
3905 function and variable tags and accumulate them into their respective
3906 tables. The second time through we process the attributes of the
3907 functions/variables and augment the table entries. */
3908 while (nesting_level >= 0)
3910 unsigned int abbrev_number, i;
3911 struct abbrev_info *abbrev;
3912 struct funcinfo *func;
3913 struct varinfo *var;
3914 uint64_t current_offset;
3916 /* PR 17512: file: 9f405d9d. */
3917 if (info_ptr >= info_ptr_end)
3918 goto fail;
3920 current_offset = info_ptr - unit->info_ptr_unit;
3921 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
3922 false, info_ptr_end);
3923 if (abbrev_number == 0)
3925 nesting_level--;
3926 continue;
3929 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3930 if (! abbrev)
3932 static unsigned int previous_failed_abbrev = -1U;
3934 /* Avoid multiple reports of the same missing abbrev. */
3935 if (abbrev_number != previous_failed_abbrev)
3937 _bfd_error_handler
3938 (_("DWARF error: could not find abbrev number %u"),
3939 abbrev_number);
3940 previous_failed_abbrev = abbrev_number;
3942 bfd_set_error (bfd_error_bad_value);
3943 goto fail;
3946 if (abbrev->tag == DW_TAG_subprogram
3947 || abbrev->tag == DW_TAG_entry_point
3948 || abbrev->tag == DW_TAG_inlined_subroutine)
3950 size_t amt = sizeof (struct funcinfo);
3952 var = NULL;
3953 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
3954 if (func == NULL)
3955 goto fail;
3956 func->tag = abbrev->tag;
3957 func->prev_func = unit->function_table;
3958 func->unit_offset = current_offset;
3959 unit->function_table = func;
3960 unit->number_of_functions++;
3961 BFD_ASSERT (!unit->cached);
3963 if (func->tag == DW_TAG_inlined_subroutine)
3964 for (i = nesting_level; i-- != 0; )
3965 if (nested_funcs[i].func)
3967 func->caller_func = nested_funcs[i].func;
3968 break;
3970 nested_funcs[nesting_level].func = func;
3972 else
3974 func = NULL;
3975 if (abbrev->tag == DW_TAG_variable
3976 || abbrev->tag == DW_TAG_member)
3978 size_t amt = sizeof (struct varinfo);
3980 var = (struct varinfo *) bfd_zalloc (abfd, amt);
3981 if (var == NULL)
3982 goto fail;
3983 var->tag = abbrev->tag;
3984 var->stack = true;
3985 var->prev_var = unit->variable_table;
3986 unit->variable_table = var;
3987 var->unit_offset = current_offset;
3988 /* PR 18205: Missing debug information can cause this
3989 var to be attached to an already cached unit. */
3991 else
3992 var = NULL;
3994 /* No inline function in scope at this nesting level. */
3995 nested_funcs[nesting_level].func = 0;
3998 for (i = 0; i < abbrev->num_attrs; ++i)
4000 struct attribute attr;
4002 info_ptr = read_attribute (&attr, &abbrev->attrs[i],
4003 unit, info_ptr, info_ptr_end);
4004 if (info_ptr == NULL)
4005 goto fail;
4008 if (abbrev->has_children)
4010 nesting_level++;
4012 if (nesting_level >= nested_funcs_size)
4014 struct nest_funcinfo *tmp;
4016 nested_funcs_size *= 2;
4017 tmp = (struct nest_funcinfo *)
4018 bfd_realloc (nested_funcs,
4019 nested_funcs_size * sizeof (*nested_funcs));
4020 if (tmp == NULL)
4021 goto fail;
4022 nested_funcs = tmp;
4024 nested_funcs[nesting_level].func = 0;
4028 unit->function_table = reverse_funcinfo_list (unit->function_table);
4029 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4031 /* This is the second pass over the abbrevs. */
4032 info_ptr = unit->first_child_die_ptr;
4033 nesting_level = 0;
4035 last_func = NULL;
4036 last_var = NULL;
4038 while (nesting_level >= 0)
4040 unsigned int abbrev_number, i;
4041 struct abbrev_info *abbrev;
4042 struct attribute attr;
4043 struct funcinfo *func;
4044 struct varinfo *var;
4045 bfd_vma low_pc = 0;
4046 bfd_vma high_pc = 0;
4047 bool high_pc_relative = false;
4048 uint64_t current_offset;
4050 /* PR 17512: file: 9f405d9d. */
4051 if (info_ptr >= info_ptr_end)
4052 goto fail;
4054 current_offset = info_ptr - unit->info_ptr_unit;
4055 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
4056 false, info_ptr_end);
4057 if (! abbrev_number)
4059 nesting_level--;
4060 continue;
4063 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
4064 /* This should have been handled above. */
4065 BFD_ASSERT (abbrev != NULL);
4067 func = NULL;
4068 var = NULL;
4069 if (abbrev->tag == DW_TAG_subprogram
4070 || abbrev->tag == DW_TAG_entry_point
4071 || abbrev->tag == DW_TAG_inlined_subroutine)
4073 if (last_func
4074 && last_func->prev_func
4075 && last_func->prev_func->unit_offset == current_offset)
4076 func = last_func->prev_func;
4077 else
4078 func = lookup_func_by_offset (current_offset, unit->function_table);
4080 if (func == NULL)
4081 goto fail;
4083 last_func = func;
4085 else if (abbrev->tag == DW_TAG_variable
4086 || abbrev->tag == DW_TAG_member)
4088 if (last_var
4089 && last_var->prev_var
4090 && last_var->prev_var->unit_offset == current_offset)
4091 var = last_var->prev_var;
4092 else
4093 var = lookup_var_by_offset (current_offset, unit->variable_table);
4095 if (var == NULL)
4096 goto fail;
4098 last_var = var;
4101 for (i = 0; i < abbrev->num_attrs; ++i)
4103 info_ptr = read_attribute (&attr, &abbrev->attrs[i],
4104 unit, info_ptr, info_ptr_end);
4105 if (info_ptr == NULL)
4106 goto fail;
4108 if (func)
4110 switch (attr.name)
4112 case DW_AT_call_file:
4113 if (is_int_form (&attr))
4115 free (func->caller_file);
4116 func->caller_file = concat_filename (unit->line_table,
4117 attr.u.val);
4119 break;
4121 case DW_AT_call_line:
4122 if (is_int_form (&attr))
4123 func->caller_line = attr.u.val;
4124 break;
4126 case DW_AT_abstract_origin:
4127 case DW_AT_specification:
4128 if (is_int_form (&attr)
4129 && !find_abstract_instance (unit, &attr, 0,
4130 &func->name,
4131 &func->is_linkage,
4132 &func->file,
4133 &func->line))
4134 goto fail;
4135 break;
4137 case DW_AT_name:
4138 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
4139 over DW_AT_name. */
4140 if (func->name == NULL && is_str_form (&attr))
4142 func->name = attr.u.str;
4143 if (mangle_style (unit->lang) == 0)
4144 func->is_linkage = true;
4146 break;
4148 case DW_AT_linkage_name:
4149 case DW_AT_MIPS_linkage_name:
4150 /* PR 16949: Corrupt debug info can place
4151 non-string forms into these attributes. */
4152 if (is_str_form (&attr))
4154 func->name = attr.u.str;
4155 func->is_linkage = true;
4157 break;
4159 case DW_AT_low_pc:
4160 if (is_int_form (&attr))
4161 low_pc = attr.u.val;
4162 break;
4164 case DW_AT_high_pc:
4165 if (is_int_form (&attr))
4167 high_pc = attr.u.val;
4168 high_pc_relative = attr.form != DW_FORM_addr;
4170 break;
4172 case DW_AT_ranges:
4173 if (is_int_form (&attr)
4174 && !read_rangelist (unit, &func->arange,
4175 &unit->file->trie_root, attr.u.val))
4176 goto fail;
4177 break;
4179 case DW_AT_decl_file:
4180 if (is_int_form (&attr))
4182 free (func->file);
4183 func->file = concat_filename (unit->line_table,
4184 attr.u.val);
4186 break;
4188 case DW_AT_decl_line:
4189 if (is_int_form (&attr))
4190 func->line = attr.u.val;
4191 break;
4193 default:
4194 break;
4197 else if (var)
4199 switch (attr.name)
4201 case DW_AT_specification:
4202 if (is_int_form (&attr) && attr.u.val)
4204 bool is_linkage;
4205 if (!find_abstract_instance (unit, &attr, 0,
4206 &var->name,
4207 &is_linkage,
4208 &var->file,
4209 &var->line))
4211 _bfd_error_handler (_("DWARF error: could not find "
4212 "variable specification "
4213 "at offset 0x%lx"),
4214 (unsigned long) attr.u.val);
4215 break;
4218 break;
4220 case DW_AT_name:
4221 if (is_str_form (&attr))
4222 var->name = attr.u.str;
4223 break;
4225 case DW_AT_decl_file:
4226 if (is_int_form (&attr))
4228 free (var->file);
4229 var->file = concat_filename (unit->line_table,
4230 attr.u.val);
4232 break;
4234 case DW_AT_decl_line:
4235 if (is_int_form (&attr))
4236 var->line = attr.u.val;
4237 break;
4239 case DW_AT_external:
4240 if (is_int_form (&attr) && attr.u.val != 0)
4241 var->stack = false;
4242 break;
4244 case DW_AT_location:
4245 switch (attr.form)
4247 case DW_FORM_block:
4248 case DW_FORM_block1:
4249 case DW_FORM_block2:
4250 case DW_FORM_block4:
4251 case DW_FORM_exprloc:
4252 if (attr.u.blk->data != NULL
4253 && *attr.u.blk->data == DW_OP_addr)
4255 var->stack = false;
4257 /* Verify that DW_OP_addr is the only opcode in the
4258 location, in which case the block size will be 1
4259 plus the address size. */
4260 /* ??? For TLS variables, gcc can emit
4261 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
4262 which we don't handle here yet. */
4263 if (attr.u.blk->size == unit->addr_size + 1U)
4264 var->addr = bfd_get (unit->addr_size * 8,
4265 unit->abfd,
4266 attr.u.blk->data + 1);
4268 break;
4270 default:
4271 break;
4273 break;
4275 default:
4276 break;
4281 if (abbrev->has_children)
4282 nesting_level++;
4284 if (high_pc_relative)
4285 high_pc += low_pc;
4287 if (func && high_pc != 0)
4289 if (!arange_add (unit, &func->arange, &unit->file->trie_root,
4290 low_pc, high_pc))
4291 goto fail;
4295 unit->function_table = reverse_funcinfo_list (unit->function_table);
4296 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4298 free (nested_funcs);
4299 return true;
4301 fail:
4302 free (nested_funcs);
4303 return false;
4306 /* Read the attributes of the form strx and addrx. */
4308 static void
4309 reread_attribute (struct comp_unit *unit,
4310 struct attribute *attr,
4311 bfd_vma *low_pc,
4312 bfd_vma *high_pc,
4313 bool *high_pc_relative,
4314 bool compunit)
4316 if (is_strx_form (attr->form))
4317 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
4318 if (is_addrx_form (attr->form))
4319 attr->u.val = read_indexed_address (attr->u.val, unit);
4321 switch (attr->name)
4323 case DW_AT_stmt_list:
4324 unit->stmtlist = 1;
4325 unit->line_offset = attr->u.val;
4326 break;
4328 case DW_AT_name:
4329 if (is_str_form (attr))
4330 unit->name = attr->u.str;
4331 break;
4333 case DW_AT_low_pc:
4334 *low_pc = attr->u.val;
4335 if (compunit)
4336 unit->base_address = *low_pc;
4337 break;
4339 case DW_AT_high_pc:
4340 *high_pc = attr->u.val;
4341 *high_pc_relative = attr->form != DW_FORM_addr;
4342 break;
4344 case DW_AT_ranges:
4345 if (!read_rangelist (unit, &unit->arange,
4346 &unit->file->trie_root, attr->u.val))
4347 return;
4348 break;
4350 case DW_AT_comp_dir:
4352 char *comp_dir = attr->u.str;
4354 if (!is_str_form (attr))
4356 _bfd_error_handler
4357 (_("DWARF error: DW_AT_comp_dir attribute encountered "
4358 "with a non-string form"));
4359 comp_dir = NULL;
4362 if (comp_dir)
4364 char *cp = strchr (comp_dir, ':');
4366 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
4367 comp_dir = cp + 1;
4369 unit->comp_dir = comp_dir;
4370 break;
4373 case DW_AT_language:
4374 unit->lang = attr->u.val;
4375 default:
4376 break;
4380 /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
4381 includes the compilation unit header that proceeds the DIE's, but
4382 does not include the length field that precedes each compilation
4383 unit header. END_PTR points one past the end of this comp unit.
4384 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
4386 This routine does not read the whole compilation unit; only enough
4387 to get to the line number information for the compilation unit. */
4389 static struct comp_unit *
4390 parse_comp_unit (struct dwarf2_debug *stash,
4391 struct dwarf2_debug_file *file,
4392 bfd_byte *info_ptr,
4393 bfd_vma unit_length,
4394 bfd_byte *info_ptr_unit,
4395 unsigned int offset_size)
4397 struct comp_unit* unit;
4398 unsigned int version;
4399 uint64_t abbrev_offset = 0;
4400 /* Initialize it just to avoid a GCC false warning. */
4401 unsigned int addr_size = -1;
4402 struct abbrev_info** abbrevs;
4403 unsigned int abbrev_number, i;
4404 struct abbrev_info *abbrev;
4405 struct attribute attr;
4406 bfd_byte *end_ptr = info_ptr + unit_length;
4407 size_t amt;
4408 bfd_vma low_pc = 0;
4409 bfd_vma high_pc = 0;
4410 bfd *abfd = file->bfd_ptr;
4411 bool high_pc_relative = false;
4412 enum dwarf_unit_type unit_type;
4413 struct attribute *str_addrp = NULL;
4414 size_t str_count = 0;
4415 size_t str_alloc = 0;
4416 bool compunit_flag = false;
4418 version = read_2_bytes (abfd, &info_ptr, end_ptr);
4419 if (version < 2 || version > 5)
4421 /* PR 19872: A version number of 0 probably means that there is padding
4422 at the end of the .debug_info section. Gold puts it there when
4423 performing an incremental link, for example. So do not generate
4424 an error, just return a NULL. */
4425 if (version)
4427 _bfd_error_handler
4428 (_("DWARF error: found dwarf version '%u', this reader"
4429 " only handles version 2, 3, 4 and 5 information"), version);
4430 bfd_set_error (bfd_error_bad_value);
4432 return NULL;
4435 if (version < 5)
4436 unit_type = DW_UT_compile;
4437 else
4439 unit_type = read_1_byte (abfd, &info_ptr, end_ptr);
4440 addr_size = read_1_byte (abfd, &info_ptr, end_ptr);
4443 BFD_ASSERT (offset_size == 4 || offset_size == 8);
4444 if (offset_size == 4)
4445 abbrev_offset = read_4_bytes (abfd, &info_ptr, end_ptr);
4446 else
4447 abbrev_offset = read_8_bytes (abfd, &info_ptr, end_ptr);
4449 if (version < 5)
4450 addr_size = read_1_byte (abfd, &info_ptr, end_ptr);
4452 switch (unit_type)
4454 case DW_UT_type:
4455 /* Skip type signature. */
4456 info_ptr += 8;
4458 /* Skip type offset. */
4459 info_ptr += offset_size;
4460 break;
4462 case DW_UT_skeleton:
4463 /* Skip DWO_id field. */
4464 info_ptr += 8;
4465 break;
4467 default:
4468 break;
4471 if (addr_size > sizeof (bfd_vma))
4473 _bfd_error_handler
4474 /* xgettext: c-format */
4475 (_("DWARF error: found address size '%u', this reader"
4476 " can not handle sizes greater than '%u'"),
4477 addr_size,
4478 (unsigned int) sizeof (bfd_vma));
4479 bfd_set_error (bfd_error_bad_value);
4480 return NULL;
4483 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
4485 _bfd_error_handler
4486 ("DWARF error: found address size '%u', this reader"
4487 " can only handle address sizes '2', '4' and '8'", addr_size);
4488 bfd_set_error (bfd_error_bad_value);
4489 return NULL;
4492 /* Read the abbrevs for this compilation unit into a table. */
4493 abbrevs = read_abbrevs (abfd, abbrev_offset, stash, file);
4494 if (! abbrevs)
4495 return NULL;
4497 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
4498 false, end_ptr);
4499 if (! abbrev_number)
4501 /* PR 19872: An abbrev number of 0 probably means that there is padding
4502 at the end of the .debug_abbrev section. Gold puts it there when
4503 performing an incremental link, for example. So do not generate
4504 an error, just return a NULL. */
4505 return NULL;
4508 abbrev = lookup_abbrev (abbrev_number, abbrevs);
4509 if (! abbrev)
4511 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
4512 abbrev_number);
4513 bfd_set_error (bfd_error_bad_value);
4514 return NULL;
4517 amt = sizeof (struct comp_unit);
4518 unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
4519 if (unit == NULL)
4520 return NULL;
4521 unit->abfd = abfd;
4522 unit->version = version;
4523 unit->addr_size = addr_size;
4524 unit->offset_size = offset_size;
4525 unit->abbrevs = abbrevs;
4526 unit->end_ptr = end_ptr;
4527 unit->stash = stash;
4528 unit->file = file;
4529 unit->info_ptr_unit = info_ptr_unit;
4531 if (abbrev->tag == DW_TAG_compile_unit)
4532 compunit_flag = true;
4534 for (i = 0; i < abbrev->num_attrs; ++i)
4536 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, end_ptr);
4537 if (info_ptr == NULL)
4538 goto err_exit;
4540 /* Identify attributes of the form strx* and addrx* which come before
4541 DW_AT_str_offsets_base and DW_AT_addr_base respectively in the CU.
4542 Store the attributes in an array and process them later. */
4543 if ((unit->dwarf_str_offset == 0 && is_strx_form (attr.form))
4544 || (unit->dwarf_addr_offset == 0 && is_addrx_form (attr.form)))
4546 if (str_count <= str_alloc)
4548 str_alloc = 2 * str_alloc + 200;
4549 str_addrp = bfd_realloc (str_addrp,
4550 str_alloc * sizeof (*str_addrp));
4551 if (str_addrp == NULL)
4552 goto err_exit;
4554 str_addrp[str_count] = attr;
4555 str_count++;
4556 continue;
4559 /* Store the data if it is of an attribute we want to keep in a
4560 partial symbol table. */
4561 switch (attr.name)
4563 case DW_AT_stmt_list:
4564 if (is_int_form (&attr))
4566 unit->stmtlist = 1;
4567 unit->line_offset = attr.u.val;
4569 break;
4571 case DW_AT_name:
4572 if (is_str_form (&attr))
4573 unit->name = attr.u.str;
4574 break;
4576 case DW_AT_low_pc:
4577 if (is_int_form (&attr))
4579 low_pc = attr.u.val;
4580 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
4581 this is the base address to use when reading location
4582 lists or range lists. */
4583 if (compunit_flag)
4584 unit->base_address = low_pc;
4586 break;
4588 case DW_AT_high_pc:
4589 if (is_int_form (&attr))
4591 high_pc = attr.u.val;
4592 high_pc_relative = attr.form != DW_FORM_addr;
4594 break;
4596 case DW_AT_ranges:
4597 if (is_int_form (&attr)
4598 && !read_rangelist (unit, &unit->arange,
4599 &unit->file->trie_root, attr.u.val))
4600 goto err_exit;
4601 break;
4603 case DW_AT_comp_dir:
4605 char *comp_dir = attr.u.str;
4607 /* PR 17512: file: 1fe726be. */
4608 if (!is_str_form (&attr))
4610 _bfd_error_handler
4611 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
4612 comp_dir = NULL;
4615 if (comp_dir)
4617 /* Irix 6.2 native cc prepends <machine>.: to the compilation
4618 directory, get rid of it. */
4619 char *cp = strchr (comp_dir, ':');
4621 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
4622 comp_dir = cp + 1;
4624 unit->comp_dir = comp_dir;
4625 break;
4628 case DW_AT_language:
4629 if (is_int_form (&attr))
4630 unit->lang = attr.u.val;
4631 break;
4633 case DW_AT_addr_base:
4634 unit->dwarf_addr_offset = attr.u.val;
4635 break;
4637 case DW_AT_str_offsets_base:
4638 unit->dwarf_str_offset = attr.u.val;
4639 break;
4641 default:
4642 break;
4646 for (i = 0; i < str_count; ++i)
4647 reread_attribute (unit, &str_addrp[i], &low_pc, &high_pc,
4648 &high_pc_relative, compunit_flag);
4650 if (high_pc_relative)
4651 high_pc += low_pc;
4652 if (high_pc != 0)
4654 if (!arange_add (unit, &unit->arange, &unit->file->trie_root,
4655 low_pc, high_pc))
4656 goto err_exit;
4659 unit->first_child_die_ptr = info_ptr;
4661 free (str_addrp);
4662 return unit;
4664 err_exit:
4665 unit->error = 1;
4666 free (str_addrp);
4667 return NULL;
4670 /* Return TRUE if UNIT may contain the address given by ADDR. When
4671 there are functions written entirely with inline asm statements, the
4672 range info in the compilation unit header may not be correct. We
4673 need to consult the line info table to see if a compilation unit
4674 really contains the given address. */
4676 static bool
4677 comp_unit_may_contain_address (struct comp_unit *unit, bfd_vma addr)
4679 struct arange *arange;
4681 if (unit->error)
4682 return false;
4684 if (unit->arange.high == 0 /* No ranges have been computed yet. */
4685 || unit->line_table == NULL) /* The line info table has not been loaded. */
4686 return true;
4688 for (arange = &unit->arange; arange != NULL; arange = arange->next)
4689 if (addr >= arange->low && addr < arange->high)
4690 return true;
4692 return false;
4695 /* If UNIT contains ADDR, set the output parameters to the values for
4696 the line containing ADDR and return TRUE. Otherwise return FALSE.
4697 The output parameters, FILENAME_PTR, FUNCTION_PTR, and
4698 LINENUMBER_PTR, are pointers to the objects to be filled in. */
4700 static bool
4701 comp_unit_find_nearest_line (struct comp_unit *unit,
4702 bfd_vma addr,
4703 const char **filename_ptr,
4704 struct funcinfo **function_ptr,
4705 unsigned int *linenumber_ptr,
4706 unsigned int *discriminator_ptr)
4708 bool line_p, func_p;
4710 if (!comp_unit_maybe_decode_line_info (unit))
4711 return false;
4713 *function_ptr = NULL;
4714 func_p = lookup_address_in_function_table (unit, addr, function_ptr);
4716 if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine)
4717 unit->stash->inliner_chain = *function_ptr;
4719 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
4720 filename_ptr,
4721 linenumber_ptr,
4722 discriminator_ptr);
4723 return line_p || func_p;
4726 /* Check to see if line info is already decoded in a comp_unit.
4727 If not, decode it. Returns TRUE if no errors were encountered;
4728 FALSE otherwise. */
4730 static bool
4731 comp_unit_maybe_decode_line_info (struct comp_unit *unit)
4733 if (unit->error)
4734 return false;
4736 if (! unit->line_table)
4738 if (! unit->stmtlist)
4740 unit->error = 1;
4741 return false;
4744 unit->line_table = decode_line_info (unit);
4746 if (! unit->line_table)
4748 unit->error = 1;
4749 return false;
4752 if (unit->first_child_die_ptr < unit->end_ptr
4753 && ! scan_unit_for_symbols (unit))
4755 unit->error = 1;
4756 return false;
4760 return true;
4763 /* If UNIT contains SYM at ADDR, set the output parameters to the
4764 values for the line containing SYM. The output parameters,
4765 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
4766 filled in.
4768 Return TRUE if UNIT contains SYM, and no errors were encountered;
4769 FALSE otherwise. */
4771 static bool
4772 comp_unit_find_line (struct comp_unit *unit,
4773 asymbol *sym,
4774 bfd_vma addr,
4775 const char **filename_ptr,
4776 unsigned int *linenumber_ptr)
4778 if (!comp_unit_maybe_decode_line_info (unit))
4779 return false;
4781 if (sym->flags & BSF_FUNCTION)
4782 return lookup_symbol_in_function_table (unit, sym, addr,
4783 filename_ptr,
4784 linenumber_ptr);
4786 return lookup_symbol_in_variable_table (unit, sym, addr,
4787 filename_ptr,
4788 linenumber_ptr);
4791 /* Extract all interesting funcinfos and varinfos of a compilation
4792 unit into hash tables for faster lookup. Returns TRUE if no
4793 errors were enountered; FALSE otherwise. */
4795 static bool
4796 comp_unit_hash_info (struct dwarf2_debug *stash,
4797 struct comp_unit *unit,
4798 struct info_hash_table *funcinfo_hash_table,
4799 struct info_hash_table *varinfo_hash_table)
4801 struct funcinfo* each_func;
4802 struct varinfo* each_var;
4803 bool okay = true;
4805 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
4807 if (!comp_unit_maybe_decode_line_info (unit))
4808 return false;
4810 BFD_ASSERT (!unit->cached);
4812 /* To preserve the original search order, we went to visit the function
4813 infos in the reversed order of the list. However, making the list
4814 bi-directional use quite a bit of extra memory. So we reverse
4815 the list first, traverse the list in the now reversed order and
4816 finally reverse the list again to get back the original order. */
4817 unit->function_table = reverse_funcinfo_list (unit->function_table);
4818 for (each_func = unit->function_table;
4819 each_func && okay;
4820 each_func = each_func->prev_func)
4822 /* Skip nameless functions. */
4823 if (each_func->name)
4824 /* There is no need to copy name string into hash table as
4825 name string is either in the dwarf string buffer or
4826 info in the stash. */
4827 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
4828 (void*) each_func, false);
4830 unit->function_table = reverse_funcinfo_list (unit->function_table);
4831 if (!okay)
4832 return false;
4834 /* We do the same for variable infos. */
4835 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4836 for (each_var = unit->variable_table;
4837 each_var && okay;
4838 each_var = each_var->prev_var)
4840 /* Skip stack vars and vars with no files or names. */
4841 if (! each_var->stack
4842 && each_var->file != NULL
4843 && each_var->name != NULL)
4844 /* There is no need to copy name string into hash table as
4845 name string is either in the dwarf string buffer or
4846 info in the stash. */
4847 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
4848 (void*) each_var, false);
4851 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4852 unit->cached = true;
4853 return okay;
4856 /* Locate a section in a BFD containing debugging info. The search starts
4857 from the section after AFTER_SEC, or from the first section in the BFD if
4858 AFTER_SEC is NULL. The search works by examining the names of the
4859 sections. There are three permissiable names. The first two are given
4860 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
4861 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
4862 This is a variation on the .debug_info section which has a checksum
4863 describing the contents appended onto the name. This allows the linker to
4864 identify and discard duplicate debugging sections for different
4865 compilation units. */
4866 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
4868 static asection *
4869 find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
4870 asection *after_sec)
4872 asection *msec;
4873 const char *look;
4875 if (after_sec == NULL)
4877 look = debug_sections[debug_info].uncompressed_name;
4878 msec = bfd_get_section_by_name (abfd, look);
4879 /* Testing SEC_HAS_CONTENTS is an anti-fuzzer measure. Of
4880 course debug sections always have contents. */
4881 if (msec != NULL && (msec->flags & SEC_HAS_CONTENTS) != 0)
4882 return msec;
4884 look = debug_sections[debug_info].compressed_name;
4885 msec = bfd_get_section_by_name (abfd, look);
4886 if (msec != NULL && (msec->flags & SEC_HAS_CONTENTS) != 0)
4887 return msec;
4889 for (msec = abfd->sections; msec != NULL; msec = msec->next)
4890 if ((msec->flags & SEC_HAS_CONTENTS) != 0
4891 && startswith (msec->name, GNU_LINKONCE_INFO))
4892 return msec;
4894 return NULL;
4897 for (msec = after_sec->next; msec != NULL; msec = msec->next)
4899 if ((msec->flags & SEC_HAS_CONTENTS) == 0)
4900 continue;
4902 look = debug_sections[debug_info].uncompressed_name;
4903 if (strcmp (msec->name, look) == 0)
4904 return msec;
4906 look = debug_sections[debug_info].compressed_name;
4907 if (look != NULL && strcmp (msec->name, look) == 0)
4908 return msec;
4910 if (startswith (msec->name, GNU_LINKONCE_INFO))
4911 return msec;
4914 return NULL;
4917 /* Transfer VMAs from object file to separate debug file. */
4919 static void
4920 set_debug_vma (bfd *orig_bfd, bfd *debug_bfd)
4922 asection *s, *d;
4924 for (s = orig_bfd->sections, d = debug_bfd->sections;
4925 s != NULL && d != NULL;
4926 s = s->next, d = d->next)
4928 if ((d->flags & SEC_DEBUGGING) != 0)
4929 break;
4930 /* ??? Assumes 1-1 correspondence between sections in the
4931 two files. */
4932 if (strcmp (s->name, d->name) == 0)
4934 d->output_section = s->output_section;
4935 d->output_offset = s->output_offset;
4936 d->vma = s->vma;
4941 /* If the dwarf2 info was found in a separate debug file, return the
4942 debug file section corresponding to the section in the original file
4943 and the debug file symbols. */
4945 static void
4946 _bfd_dwarf2_stash_syms (struct dwarf2_debug *stash, bfd *abfd,
4947 asection **sec, asymbol ***syms)
4949 if (stash->f.bfd_ptr != abfd)
4951 asection *s, *d;
4953 if (*sec == NULL)
4955 *syms = stash->f.syms;
4956 return;
4959 for (s = abfd->sections, d = stash->f.bfd_ptr->sections;
4960 s != NULL && d != NULL;
4961 s = s->next, d = d->next)
4963 if ((d->flags & SEC_DEBUGGING) != 0)
4964 break;
4965 if (s == *sec
4966 && strcmp (s->name, d->name) == 0)
4968 *sec = d;
4969 *syms = stash->f.syms;
4970 break;
4976 /* Unset vmas for adjusted sections in STASH. */
4978 static void
4979 unset_sections (struct dwarf2_debug *stash)
4981 int i;
4982 struct adjusted_section *p;
4984 i = stash->adjusted_section_count;
4985 p = stash->adjusted_sections;
4986 for (; i > 0; i--, p++)
4987 p->section->vma = p->orig_vma;
4990 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4991 relocatable object file. VMAs are normally all zero in relocatable
4992 object files, so if we want to distinguish locations in sections by
4993 address we need to set VMAs so the sections do not overlap. We
4994 also set VMA on .debug_info so that when we have multiple
4995 .debug_info sections (or the linkonce variant) they also do not
4996 overlap. The multiple .debug_info sections make up a single
4997 logical section. ??? We should probably do the same for other
4998 debug sections. */
5000 static bool
5001 place_sections (bfd *orig_bfd, struct dwarf2_debug *stash)
5003 bfd *abfd;
5004 struct adjusted_section *p;
5005 int i;
5006 const char *debug_info_name;
5008 if (stash->adjusted_section_count != 0)
5010 i = stash->adjusted_section_count;
5011 p = stash->adjusted_sections;
5012 for (; i > 0; i--, p++)
5013 p->section->vma = p->adj_vma;
5014 return true;
5017 debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
5018 i = 0;
5019 abfd = orig_bfd;
5020 while (1)
5022 asection *sect;
5024 for (sect = abfd->sections; sect != NULL; sect = sect->next)
5026 int is_debug_info;
5028 if (sect->output_section != NULL
5029 && sect->output_section != sect
5030 && (sect->flags & SEC_DEBUGGING) == 0)
5031 continue;
5033 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
5034 || startswith (sect->name, GNU_LINKONCE_INFO));
5036 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
5037 && !is_debug_info)
5038 continue;
5040 i++;
5042 if (abfd == stash->f.bfd_ptr)
5043 break;
5044 abfd = stash->f.bfd_ptr;
5047 if (i <= 1)
5048 stash->adjusted_section_count = -1;
5049 else
5051 bfd_vma last_vma = 0, last_dwarf = 0;
5052 size_t amt = i * sizeof (struct adjusted_section);
5054 p = (struct adjusted_section *) bfd_malloc (amt);
5055 if (p == NULL)
5056 return false;
5058 stash->adjusted_sections = p;
5059 stash->adjusted_section_count = i;
5061 abfd = orig_bfd;
5062 while (1)
5064 asection *sect;
5066 for (sect = abfd->sections; sect != NULL; sect = sect->next)
5068 bfd_size_type sz;
5069 int is_debug_info;
5071 if (sect->output_section != NULL
5072 && sect->output_section != sect
5073 && (sect->flags & SEC_DEBUGGING) == 0)
5074 continue;
5076 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
5077 || startswith (sect->name, GNU_LINKONCE_INFO));
5079 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
5080 && !is_debug_info)
5081 continue;
5083 sz = sect->rawsize ? sect->rawsize : sect->size;
5085 p->section = sect;
5086 p->orig_vma = sect->vma;
5088 bfd_vma *v = is_debug_info ? &last_dwarf : &last_vma;
5089 /* Align the new address to the current section
5090 alignment. */
5091 bfd_vma mask = -(bfd_vma) 1 << sect->alignment_power;
5092 *v = (*v + ~mask) & mask;
5093 sect->vma = *v;
5094 *v += sz;
5096 p->adj_vma = sect->vma;
5097 p++;
5099 if (abfd == stash->f.bfd_ptr)
5100 break;
5101 abfd = stash->f.bfd_ptr;
5105 if (orig_bfd != stash->f.bfd_ptr)
5106 set_debug_vma (orig_bfd, stash->f.bfd_ptr);
5108 return true;
5111 /* Look up a funcinfo by name using the given info hash table. If found,
5112 also update the locations pointed to by filename_ptr and linenumber_ptr.
5114 This function returns TRUE if a funcinfo that matches the given symbol
5115 and address is found with any error; otherwise it returns FALSE. */
5117 static bool
5118 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
5119 asymbol *sym,
5120 bfd_vma addr,
5121 const char **filename_ptr,
5122 unsigned int *linenumber_ptr)
5124 struct funcinfo* each_func;
5125 struct funcinfo* best_fit = NULL;
5126 bfd_vma best_fit_len = (bfd_vma) -1;
5127 struct info_list_node *node;
5128 struct arange *arange;
5129 const char *name = bfd_asymbol_name (sym);
5131 for (node = lookup_info_hash_table (hash_table, name);
5132 node;
5133 node = node->next)
5135 each_func = (struct funcinfo *) node->info;
5136 for (arange = &each_func->arange;
5137 arange;
5138 arange = arange->next)
5140 if (addr >= arange->low
5141 && addr < arange->high
5142 && arange->high - arange->low < best_fit_len)
5144 best_fit = each_func;
5145 best_fit_len = arange->high - arange->low;
5150 if (best_fit)
5152 *filename_ptr = best_fit->file;
5153 *linenumber_ptr = best_fit->line;
5154 return true;
5157 return false;
5160 /* Look up a varinfo by name using the given info hash table. If found,
5161 also update the locations pointed to by filename_ptr and linenumber_ptr.
5163 This function returns TRUE if a varinfo that matches the given symbol
5164 and address is found with any error; otherwise it returns FALSE. */
5166 static bool
5167 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
5168 asymbol *sym,
5169 bfd_vma addr,
5170 const char **filename_ptr,
5171 unsigned int *linenumber_ptr)
5173 struct varinfo* each;
5174 struct info_list_node *node;
5175 const char *name = bfd_asymbol_name (sym);
5177 for (node = lookup_info_hash_table (hash_table, name);
5178 node;
5179 node = node->next)
5181 each = (struct varinfo *) node->info;
5182 if (each->addr == addr)
5184 *filename_ptr = each->file;
5185 *linenumber_ptr = each->line;
5186 return true;
5190 return false;
5193 /* Update the funcinfo and varinfo info hash tables if they are
5194 not up to date. Returns TRUE if there is no error; otherwise
5195 returns FALSE and disable the info hash tables. */
5197 static bool
5198 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
5200 struct comp_unit *each;
5202 /* Exit if hash tables are up-to-date. */
5203 if (stash->f.all_comp_units == stash->hash_units_head)
5204 return true;
5206 if (stash->hash_units_head)
5207 each = stash->hash_units_head->prev_unit;
5208 else
5209 each = stash->f.last_comp_unit;
5211 while (each)
5213 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
5214 stash->varinfo_hash_table))
5216 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
5217 return false;
5219 each = each->prev_unit;
5222 stash->hash_units_head = stash->f.all_comp_units;
5223 return true;
5226 /* Check consistency of info hash tables. This is for debugging only. */
5228 static void ATTRIBUTE_UNUSED
5229 stash_verify_info_hash_table (struct dwarf2_debug *stash)
5231 struct comp_unit *each_unit;
5232 struct funcinfo *each_func;
5233 struct varinfo *each_var;
5234 struct info_list_node *node;
5235 bool found;
5237 for (each_unit = stash->f.all_comp_units;
5238 each_unit;
5239 each_unit = each_unit->next_unit)
5241 for (each_func = each_unit->function_table;
5242 each_func;
5243 each_func = each_func->prev_func)
5245 if (!each_func->name)
5246 continue;
5247 node = lookup_info_hash_table (stash->funcinfo_hash_table,
5248 each_func->name);
5249 BFD_ASSERT (node);
5250 found = false;
5251 while (node && !found)
5253 found = node->info == each_func;
5254 node = node->next;
5256 BFD_ASSERT (found);
5259 for (each_var = each_unit->variable_table;
5260 each_var;
5261 each_var = each_var->prev_var)
5263 if (!each_var->name || !each_var->file || each_var->stack)
5264 continue;
5265 node = lookup_info_hash_table (stash->varinfo_hash_table,
5266 each_var->name);
5267 BFD_ASSERT (node);
5268 found = false;
5269 while (node && !found)
5271 found = node->info == each_var;
5272 node = node->next;
5274 BFD_ASSERT (found);
5279 /* Check to see if we want to enable the info hash tables, which consume
5280 quite a bit of memory. Currently we only check the number times
5281 bfd_dwarf2_find_line is called. In the future, we may also want to
5282 take the number of symbols into account. */
5284 static void
5285 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
5287 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
5289 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
5290 return;
5292 /* FIXME: Maybe we should check the reduce_memory_overheads
5293 and optimize fields in the bfd_link_info structure ? */
5295 /* Create hash tables. */
5296 stash->funcinfo_hash_table = create_info_hash_table (abfd);
5297 stash->varinfo_hash_table = create_info_hash_table (abfd);
5298 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
5300 /* Turn off info hashes if any allocation above fails. */
5301 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
5302 return;
5304 /* We need a forced update so that the info hash tables will
5305 be created even though there is no compilation unit. That
5306 happens if STASH_INFO_HASH_TRIGGER is 0. */
5307 if (stash_maybe_update_info_hash_tables (stash))
5308 stash->info_hash_status = STASH_INFO_HASH_ON;
5311 /* Find the file and line associated with a symbol and address using the
5312 info hash tables of a stash. If there is a match, the function returns
5313 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
5314 otherwise it returns FALSE. */
5316 static bool
5317 stash_find_line_fast (struct dwarf2_debug *stash,
5318 asymbol *sym,
5319 bfd_vma addr,
5320 const char **filename_ptr,
5321 unsigned int *linenumber_ptr)
5323 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
5325 if (sym->flags & BSF_FUNCTION)
5326 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
5327 filename_ptr, linenumber_ptr);
5328 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
5329 filename_ptr, linenumber_ptr);
5332 /* Save current section VMAs. */
5334 static bool
5335 save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
5337 asection *s;
5338 unsigned int i;
5340 if (abfd->section_count == 0)
5341 return true;
5342 stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count);
5343 if (stash->sec_vma == NULL)
5344 return false;
5345 stash->sec_vma_count = abfd->section_count;
5346 for (i = 0, s = abfd->sections;
5347 s != NULL && i < abfd->section_count;
5348 i++, s = s->next)
5350 if (s->output_section != NULL)
5351 stash->sec_vma[i] = s->output_section->vma + s->output_offset;
5352 else
5353 stash->sec_vma[i] = s->vma;
5355 return true;
5358 /* Compare current section VMAs against those at the time the stash
5359 was created. If find_nearest_line is used in linker warnings or
5360 errors early in the link process, the debug info stash will be
5361 invalid for later calls. This is because we relocate debug info
5362 sections, so the stashed section contents depend on symbol values,
5363 which in turn depend on section VMAs. */
5365 static bool
5366 section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash)
5368 asection *s;
5369 unsigned int i;
5371 /* PR 24334: If the number of sections in ABFD has changed between
5372 when the stash was created and now, then we cannot trust the
5373 stashed vma information. */
5374 if (abfd->section_count != stash->sec_vma_count)
5375 return false;
5377 for (i = 0, s = abfd->sections;
5378 s != NULL && i < abfd->section_count;
5379 i++, s = s->next)
5381 bfd_vma vma;
5383 if (s->output_section != NULL)
5384 vma = s->output_section->vma + s->output_offset;
5385 else
5386 vma = s->vma;
5387 if (vma != stash->sec_vma[i])
5388 return false;
5390 return true;
5393 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
5394 If DEBUG_BFD is not specified, we read debug information from ABFD
5395 or its gnu_debuglink. The results will be stored in PINFO.
5396 The function returns TRUE iff debug information is ready. */
5398 bool
5399 _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
5400 const struct dwarf_debug_section *debug_sections,
5401 asymbol **symbols,
5402 void **pinfo,
5403 bool do_place)
5405 bfd_size_type total_size;
5406 asection *msec;
5407 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
5409 if (stash != NULL)
5411 if (stash->orig_bfd_id == abfd->id
5412 && section_vma_same (abfd, stash))
5414 /* Check that we did previously find some debug information
5415 before attempting to make use of it. */
5416 if (stash->f.dwarf_info_size != 0)
5418 if (do_place && !place_sections (abfd, stash))
5419 return false;
5420 return true;
5423 return false;
5425 _bfd_dwarf2_cleanup_debug_info (abfd, pinfo);
5426 memset (stash, 0, sizeof (*stash));
5428 else
5430 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, sizeof (*stash));
5431 if (! stash)
5432 return false;
5433 *pinfo = stash;
5435 stash->orig_bfd_id = abfd->id;
5436 stash->debug_sections = debug_sections;
5437 stash->f.syms = symbols;
5438 if (!save_section_vma (abfd, stash))
5439 return false;
5441 stash->f.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
5442 del_abbrev, calloc, free);
5443 if (!stash->f.abbrev_offsets)
5444 return false;
5446 stash->alt.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
5447 del_abbrev, calloc, free);
5448 if (!stash->alt.abbrev_offsets)
5449 return false;
5451 stash->f.trie_root = alloc_trie_leaf (abfd);
5452 if (!stash->f.trie_root)
5453 return false;
5455 stash->alt.trie_root = alloc_trie_leaf (abfd);
5456 if (!stash->alt.trie_root)
5457 return false;
5459 if (debug_bfd == NULL)
5460 debug_bfd = abfd;
5462 msec = find_debug_info (debug_bfd, debug_sections, NULL);
5463 if (msec == NULL && abfd == debug_bfd)
5465 char * debug_filename;
5467 debug_filename = bfd_follow_build_id_debuglink (abfd, DEBUGDIR);
5468 if (debug_filename == NULL)
5469 debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
5471 if (debug_filename == NULL)
5472 /* No dwarf2 info, and no gnu_debuglink to follow.
5473 Note that at this point the stash has been allocated, but
5474 contains zeros. This lets future calls to this function
5475 fail more quickly. */
5476 return false;
5478 debug_bfd = bfd_openr (debug_filename, NULL);
5479 free (debug_filename);
5480 if (debug_bfd == NULL)
5481 /* FIXME: Should we report our failure to follow the debuglink ? */
5482 return false;
5484 /* Set BFD_DECOMPRESS to decompress debug sections. */
5485 debug_bfd->flags |= BFD_DECOMPRESS;
5486 if (!bfd_check_format (debug_bfd, bfd_object)
5487 || (msec = find_debug_info (debug_bfd,
5488 debug_sections, NULL)) == NULL
5489 || !bfd_generic_link_read_symbols (debug_bfd))
5491 bfd_close (debug_bfd);
5492 return false;
5495 symbols = bfd_get_outsymbols (debug_bfd);
5496 stash->f.syms = symbols;
5497 stash->close_on_cleanup = true;
5499 stash->f.bfd_ptr = debug_bfd;
5501 if (do_place
5502 && !place_sections (abfd, stash))
5503 return false;
5505 /* There can be more than one DWARF2 info section in a BFD these
5506 days. First handle the easy case when there's only one. If
5507 there's more than one, try case two: read them all in and produce
5508 one large stash. We do this in two passes - in the first pass we
5509 just accumulate the section sizes, and in the second pass we
5510 read in the section's contents. (The allows us to avoid
5511 reallocing the data as we add sections to the stash.) */
5513 if (! find_debug_info (debug_bfd, debug_sections, msec))
5515 /* Case 1: only one info section. */
5516 total_size = msec->size;
5517 if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
5518 symbols, 0,
5519 &stash->f.dwarf_info_buffer, &total_size))
5520 goto restore_vma;
5522 else
5524 /* Case 2: multiple sections. */
5525 for (total_size = 0;
5526 msec;
5527 msec = find_debug_info (debug_bfd, debug_sections, msec))
5529 if (bfd_section_size_insane (debug_bfd, msec))
5530 goto restore_vma;
5531 /* Catch PR25070 testcase overflowing size calculation here. */
5532 if (total_size + msec->size < total_size)
5534 bfd_set_error (bfd_error_no_memory);
5535 goto restore_vma;
5537 total_size += msec->size;
5540 stash->f.dwarf_info_buffer = (bfd_byte *) bfd_malloc (total_size);
5541 if (stash->f.dwarf_info_buffer == NULL)
5542 goto restore_vma;
5544 total_size = 0;
5545 for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
5546 msec;
5547 msec = find_debug_info (debug_bfd, debug_sections, msec))
5549 bfd_size_type size;
5551 size = msec->size;
5552 if (size == 0)
5553 continue;
5555 if (!(bfd_simple_get_relocated_section_contents
5556 (debug_bfd, msec, stash->f.dwarf_info_buffer + total_size,
5557 symbols)))
5558 goto restore_vma;
5560 total_size += size;
5564 stash->f.info_ptr = stash->f.dwarf_info_buffer;
5565 stash->f.dwarf_info_size = total_size;
5566 return true;
5568 restore_vma:
5569 unset_sections (stash);
5570 return false;
5573 /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
5575 static struct comp_unit *
5576 stash_comp_unit (struct dwarf2_debug *stash, struct dwarf2_debug_file *file)
5578 bfd_size_type length;
5579 unsigned int offset_size;
5580 bfd_byte *info_ptr_unit = file->info_ptr;
5581 bfd_byte *info_ptr_end = file->dwarf_info_buffer + file->dwarf_info_size;
5583 if (file->info_ptr >= info_ptr_end)
5584 return NULL;
5586 length = read_4_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5587 /* A 0xffffff length is the DWARF3 way of indicating
5588 we use 64-bit offsets, instead of 32-bit offsets. */
5589 if (length == 0xffffffff)
5591 offset_size = 8;
5592 length = read_8_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5594 /* A zero length is the IRIX way of indicating 64-bit offsets,
5595 mostly because the 64-bit length will generally fit in 32
5596 bits, and the endianness helps. */
5597 else if (length == 0)
5599 offset_size = 8;
5600 length = read_4_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5602 /* In the absence of the hints above, we assume 32-bit DWARF2
5603 offsets even for targets with 64-bit addresses, because:
5604 a) most of the time these targets will not have generated
5605 more than 2Gb of debug info and so will not need 64-bit
5606 offsets,
5608 b) if they do use 64-bit offsets but they are not using
5609 the size hints that are tested for above then they are
5610 not conforming to the DWARF3 standard anyway. */
5611 else
5612 offset_size = 4;
5614 if (length != 0
5615 && length <= (size_t) (info_ptr_end - file->info_ptr))
5617 struct comp_unit *each = parse_comp_unit (stash, file,
5618 file->info_ptr, length,
5619 info_ptr_unit, offset_size);
5620 if (each)
5622 if (file->comp_unit_tree == NULL)
5623 file->comp_unit_tree
5624 = splay_tree_new (splay_tree_compare_addr_range,
5625 splay_tree_free_addr_range, NULL);
5627 struct addr_range *r
5628 = (struct addr_range *)bfd_malloc (sizeof (struct addr_range));
5629 r->start = each->info_ptr_unit;
5630 r->end = each->end_ptr;
5631 splay_tree_node v = splay_tree_lookup (file->comp_unit_tree,
5632 (splay_tree_key)r);
5633 if (v != NULL || r->end <= r->start)
5634 abort ();
5635 splay_tree_insert (file->comp_unit_tree, (splay_tree_key)r,
5636 (splay_tree_value)each);
5638 if (file->all_comp_units)
5639 file->all_comp_units->prev_unit = each;
5640 else
5641 file->last_comp_unit = each;
5643 each->next_unit = file->all_comp_units;
5644 file->all_comp_units = each;
5646 if (each->arange.high == 0)
5648 each->next_unit_without_ranges = file->all_comp_units_without_ranges;
5649 file->all_comp_units_without_ranges = each->next_unit_without_ranges;
5652 file->info_ptr += length;
5653 return each;
5657 /* Don't trust any of the DWARF info after a corrupted length or
5658 parse error. */
5659 file->info_ptr = info_ptr_end;
5660 return NULL;
5663 /* Hash function for an asymbol. */
5665 static hashval_t
5666 hash_asymbol (const void *sym)
5668 const asymbol *asym = sym;
5669 return htab_hash_string (asym->name);
5672 /* Equality function for asymbols. */
5674 static int
5675 eq_asymbol (const void *a, const void *b)
5677 const asymbol *sa = a;
5678 const asymbol *sb = b;
5679 return strcmp (sa->name, sb->name) == 0;
5682 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
5683 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
5684 symbol in SYMBOLS and return the difference between the low_pc and
5685 the symbol's address. Returns 0 if no suitable symbol could be found. */
5687 bfd_signed_vma
5688 _bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo)
5690 struct dwarf2_debug *stash;
5691 struct comp_unit * unit;
5692 htab_t sym_hash;
5693 bfd_signed_vma result = 0;
5694 asymbol ** psym;
5696 stash = (struct dwarf2_debug *) *pinfo;
5698 if (stash == NULL || symbols == NULL)
5699 return 0;
5701 sym_hash = htab_create_alloc (10, hash_asymbol, eq_asymbol,
5702 NULL, xcalloc, free);
5703 for (psym = symbols; * psym != NULL; psym++)
5705 asymbol * sym = * psym;
5707 if (sym->flags & BSF_FUNCTION && sym->section != NULL)
5709 void **slot = htab_find_slot (sym_hash, sym, INSERT);
5710 *slot = sym;
5714 for (unit = stash->f.all_comp_units; unit; unit = unit->next_unit)
5716 struct funcinfo * func;
5718 comp_unit_maybe_decode_line_info (unit);
5720 for (func = unit->function_table; func != NULL; func = func->prev_func)
5721 if (func->name && func->arange.low)
5723 asymbol search, *sym;
5725 /* FIXME: Do we need to scan the aranges looking for the
5726 lowest pc value? */
5728 search.name = func->name;
5729 sym = htab_find (sym_hash, &search);
5730 if (sym != NULL)
5732 result = func->arange.low - (sym->value + sym->section->vma);
5733 goto done;
5738 done:
5739 htab_delete (sym_hash);
5740 return result;
5743 /* See _bfd_dwarf2_find_nearest_line_with_alt. */
5746 _bfd_dwarf2_find_nearest_line (bfd *abfd,
5747 asymbol **symbols,
5748 asymbol *symbol,
5749 asection *section,
5750 bfd_vma offset,
5751 const char **filename_ptr,
5752 const char **functionname_ptr,
5753 unsigned int *linenumber_ptr,
5754 unsigned int *discriminator_ptr,
5755 const struct dwarf_debug_section *debug_sections,
5756 void **pinfo)
5758 return _bfd_dwarf2_find_nearest_line_with_alt
5759 (abfd, NULL, symbols, symbol, section, offset, filename_ptr,
5760 functionname_ptr, linenumber_ptr, discriminator_ptr, debug_sections,
5761 pinfo);
5764 /* Find the source code location of SYMBOL. If SYMBOL is NULL
5765 then find the nearest source code location corresponding to
5766 the address SECTION + OFFSET.
5767 Returns 1 if the line is found without error and fills in
5768 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
5769 NULL the FUNCTIONNAME_PTR is also filled in.
5770 Returns 2 if partial information from _bfd_elf_find_function is
5771 returned (function and maybe file) by looking at symbols. DWARF2
5772 info is present but not regarding the requested code location.
5773 Returns 0 otherwise.
5774 SYMBOLS contains the symbol table for ABFD.
5775 DEBUG_SECTIONS contains the name of the dwarf debug sections.
5776 If ALT_FILENAME is given, attempt to open the file and use it
5777 as the .gnu_debugaltlink file. Otherwise this file will be
5778 searched for when needed. */
5781 _bfd_dwarf2_find_nearest_line_with_alt
5782 (bfd *abfd,
5783 const char *alt_filename,
5784 asymbol **symbols,
5785 asymbol *symbol,
5786 asection *section,
5787 bfd_vma offset,
5788 const char **filename_ptr,
5789 const char **functionname_ptr,
5790 unsigned int *linenumber_ptr,
5791 unsigned int *discriminator_ptr,
5792 const struct dwarf_debug_section *debug_sections,
5793 void **pinfo)
5795 /* Read each compilation unit from the section .debug_info, and check
5796 to see if it contains the address we are searching for. If yes,
5797 lookup the address, and return the line number info. If no, go
5798 on to the next compilation unit.
5800 We keep a list of all the previously read compilation units, and
5801 a pointer to the next un-read compilation unit. Check the
5802 previously read units before reading more. */
5803 struct dwarf2_debug *stash;
5804 /* What address are we looking for? */
5805 bfd_vma addr;
5806 struct comp_unit* each;
5807 struct funcinfo *function = NULL;
5808 int found = false;
5809 bool do_line;
5811 *filename_ptr = NULL;
5812 if (functionname_ptr != NULL)
5813 *functionname_ptr = NULL;
5814 *linenumber_ptr = 0;
5815 if (discriminator_ptr)
5816 *discriminator_ptr = 0;
5818 if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections,
5819 symbols, pinfo,
5820 (abfd->flags & (EXEC_P | DYNAMIC)) == 0))
5821 return false;
5823 stash = (struct dwarf2_debug *) *pinfo;
5825 if (stash->alt.bfd_ptr == NULL && alt_filename != NULL)
5827 bfd *alt_bfd = bfd_openr (alt_filename, NULL);
5829 if (alt_bfd == NULL)
5830 /* bfd_openr will have set the bfd_error. */
5831 return false;
5832 if (!bfd_check_format (alt_bfd, bfd_object))
5834 bfd_set_error (bfd_error_wrong_format);
5835 bfd_close (alt_bfd);
5836 return false;
5839 stash->alt.bfd_ptr = alt_bfd;
5842 do_line = symbol != NULL;
5843 if (do_line)
5845 BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL);
5846 section = bfd_asymbol_section (symbol);
5847 addr = symbol->value;
5849 else
5851 BFD_ASSERT (section != NULL && functionname_ptr != NULL);
5852 addr = offset;
5854 /* If we have no SYMBOL but the section we're looking at is not a
5855 code section, then take a look through the list of symbols to see
5856 if we have a symbol at the address we're looking for. If we do
5857 then use this to look up line information. This will allow us to
5858 give file and line results for data symbols. We exclude code
5859 symbols here, if we look up a function symbol and then look up the
5860 line information we'll actually return the line number for the
5861 opening '{' rather than the function definition line. This is
5862 because looking up by symbol uses the line table, in which the
5863 first line for a function is usually the opening '{', while
5864 looking up the function by section + offset uses the
5865 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
5866 which will be the line of the function name. */
5867 if (symbols != NULL && (section->flags & SEC_CODE) == 0)
5869 asymbol **tmp;
5871 for (tmp = symbols; (*tmp) != NULL; ++tmp)
5872 if ((*tmp)->the_bfd == abfd
5873 && (*tmp)->section == section
5874 && (*tmp)->value == offset
5875 && ((*tmp)->flags & BSF_SECTION_SYM) == 0)
5877 symbol = *tmp;
5878 do_line = true;
5879 /* For local symbols, keep going in the hope we find a
5880 global. */
5881 if ((symbol->flags & BSF_GLOBAL) != 0)
5882 break;
5887 if (section->output_section)
5888 addr += section->output_section->vma + section->output_offset;
5889 else
5890 addr += section->vma;
5892 /* A null info_ptr indicates that there is no dwarf2 info
5893 (or that an error occured while setting up the stash). */
5894 if (! stash->f.info_ptr)
5895 return false;
5897 stash->inliner_chain = NULL;
5899 /* Check the previously read comp. units first. */
5900 if (do_line)
5902 /* The info hash tables use quite a bit of memory. We may not want to
5903 always use them. We use some heuristics to decide if and when to
5904 turn it on. */
5905 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
5906 stash_maybe_enable_info_hash_tables (abfd, stash);
5908 /* Keep info hash table up to date if they are available. Note that we
5909 may disable the hash tables if there is any error duing update. */
5910 if (stash->info_hash_status == STASH_INFO_HASH_ON)
5911 stash_maybe_update_info_hash_tables (stash);
5913 if (stash->info_hash_status == STASH_INFO_HASH_ON)
5915 found = stash_find_line_fast (stash, symbol, addr,
5916 filename_ptr, linenumber_ptr);
5917 if (found)
5918 goto done;
5921 /* Check the previously read comp. units first. */
5922 for (each = stash->f.all_comp_units; each; each = each->next_unit)
5923 if ((symbol->flags & BSF_FUNCTION) == 0
5924 || comp_unit_may_contain_address (each, addr))
5926 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
5927 linenumber_ptr);
5928 if (found)
5929 goto done;
5932 else
5934 struct trie_node *trie = stash->f.trie_root;
5935 unsigned int bits = VMA_BITS - 8;
5936 struct comp_unit **prev_each;
5938 /* Traverse interior nodes until we get to a leaf. */
5939 while (trie && trie->num_room_in_leaf == 0)
5941 int ch = (addr >> bits) & 0xff;
5942 trie = ((struct trie_interior *) trie)->children[ch];
5943 bits -= 8;
5946 if (trie)
5948 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
5949 unsigned int i;
5951 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
5952 leaf->ranges[i].unit->mark = false;
5954 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
5956 struct comp_unit *unit = leaf->ranges[i].unit;
5957 if (unit->mark
5958 || addr < leaf->ranges[i].low_pc
5959 || addr >= leaf->ranges[i].high_pc)
5960 continue;
5961 unit->mark = true;
5963 found = comp_unit_find_nearest_line (unit, addr,
5964 filename_ptr,
5965 &function,
5966 linenumber_ptr,
5967 discriminator_ptr);
5968 if (found)
5969 goto done;
5973 /* Also scan through all compilation units without any ranges,
5974 taking them out of the list if they have acquired any since
5975 last time. */
5976 prev_each = &stash->f.all_comp_units_without_ranges;
5977 for (each = *prev_each; each; each = each->next_unit_without_ranges)
5979 if (each->arange.high != 0)
5981 *prev_each = each->next_unit_without_ranges;
5982 continue;
5985 found = comp_unit_find_nearest_line (each, addr,
5986 filename_ptr,
5987 &function,
5988 linenumber_ptr,
5989 discriminator_ptr);
5990 if (found)
5991 goto done;
5992 prev_each = &each->next_unit_without_ranges;
5996 /* Read each remaining comp. units checking each as they are read. */
5997 while ((each = stash_comp_unit (stash, &stash->f)) != NULL)
5999 /* DW_AT_low_pc and DW_AT_high_pc are optional for
6000 compilation units. If we don't have them (i.e.,
6001 unit->high == 0), we need to consult the line info table
6002 to see if a compilation unit contains the given
6003 address. */
6004 if (do_line)
6005 found = (((symbol->flags & BSF_FUNCTION) == 0
6006 || comp_unit_may_contain_address (each, addr))
6007 && comp_unit_find_line (each, symbol, addr,
6008 filename_ptr, linenumber_ptr));
6009 else
6010 found = (comp_unit_may_contain_address (each, addr)
6011 && comp_unit_find_nearest_line (each, addr,
6012 filename_ptr,
6013 &function,
6014 linenumber_ptr,
6015 discriminator_ptr));
6017 if (found)
6018 break;
6021 done:
6022 if (functionname_ptr && function && function->is_linkage)
6024 *functionname_ptr = function->name;
6025 if (!found)
6026 found = 2;
6028 else if (functionname_ptr
6029 && (!*functionname_ptr
6030 || (function && !function->is_linkage)))
6032 asymbol *fun;
6033 asymbol **syms = symbols;
6034 asection *sec = section;
6036 _bfd_dwarf2_stash_syms (stash, abfd, &sec, &syms);
6037 fun = _bfd_elf_find_function (abfd, syms, sec, offset,
6038 *filename_ptr ? NULL : filename_ptr,
6039 functionname_ptr);
6041 if (!found && fun != NULL)
6042 found = 2;
6044 if (function && !function->is_linkage)
6046 bfd_vma sec_vma;
6048 sec_vma = section->vma;
6049 if (section->output_section != NULL)
6050 sec_vma = section->output_section->vma + section->output_offset;
6051 if (fun == NULL)
6052 *functionname_ptr = function->name;
6053 else if (fun->value + sec_vma == function->arange.low)
6054 function->name = *functionname_ptr;
6055 /* Even if we didn't find a linkage name, say that we have
6056 to stop a repeated search of symbols. */
6057 function->is_linkage = true;
6061 unset_sections (stash);
6063 return found;
6066 bool
6067 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
6068 const char **filename_ptr,
6069 const char **functionname_ptr,
6070 unsigned int *linenumber_ptr,
6071 void **pinfo)
6073 struct dwarf2_debug *stash;
6075 stash = (struct dwarf2_debug *) *pinfo;
6076 if (stash)
6078 struct funcinfo *func = stash->inliner_chain;
6080 if (func && func->caller_func)
6082 *filename_ptr = func->caller_file;
6083 *functionname_ptr = func->caller_func->name;
6084 *linenumber_ptr = func->caller_line;
6085 stash->inliner_chain = func->caller_func;
6086 return true;
6090 return false;
6093 void
6094 _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
6096 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
6097 struct comp_unit *each;
6098 struct dwarf2_debug_file *file;
6100 if (abfd == NULL || stash == NULL)
6101 return;
6103 if (stash->varinfo_hash_table)
6104 bfd_hash_table_free (&stash->varinfo_hash_table->base);
6105 if (stash->funcinfo_hash_table)
6106 bfd_hash_table_free (&stash->funcinfo_hash_table->base);
6108 file = &stash->f;
6109 while (1)
6111 for (each = file->all_comp_units; each; each = each->next_unit)
6113 struct funcinfo *function_table = each->function_table;
6114 struct varinfo *variable_table = each->variable_table;
6116 if (each->line_table && each->line_table != file->line_table)
6118 free (each->line_table->files);
6119 free (each->line_table->dirs);
6122 free (each->lookup_funcinfo_table);
6123 each->lookup_funcinfo_table = NULL;
6125 while (function_table)
6127 free (function_table->file);
6128 function_table->file = NULL;
6129 free (function_table->caller_file);
6130 function_table->caller_file = NULL;
6131 function_table = function_table->prev_func;
6134 while (variable_table)
6136 free (variable_table->file);
6137 variable_table->file = NULL;
6138 variable_table = variable_table->prev_var;
6142 if (file->line_table)
6144 free (file->line_table->files);
6145 free (file->line_table->dirs);
6147 htab_delete (file->abbrev_offsets);
6148 if (file->comp_unit_tree != NULL)
6149 splay_tree_delete (file->comp_unit_tree);
6151 free (file->dwarf_line_str_buffer);
6152 free (file->dwarf_str_buffer);
6153 free (file->dwarf_ranges_buffer);
6154 free (file->dwarf_rnglists_buffer);
6155 free (file->dwarf_line_buffer);
6156 free (file->dwarf_abbrev_buffer);
6157 free (file->dwarf_info_buffer);
6158 free (file->dwarf_addr_buffer);
6159 free (file->dwarf_str_offsets_buffer);
6160 if (file == &stash->alt)
6161 break;
6162 file = &stash->alt;
6164 free (stash->sec_vma);
6165 free (stash->adjusted_sections);
6166 if (stash->close_on_cleanup)
6167 bfd_close (stash->f.bfd_ptr);
6168 if (stash->alt.bfd_ptr)
6169 bfd_close (stash->alt.bfd_ptr);
6172 typedef struct elf_find_function_cache
6174 asection * last_section;
6175 asymbol * func;
6176 const char * filename;
6177 bfd_size_type code_size;
6178 bfd_vma code_off;
6180 } elf_find_function_cache;
6183 /* Returns TRUE if symbol SYM with address CODE_OFF and size CODE_SIZE
6184 is a better fit to match OFFSET than whatever is currenly stored in
6185 CACHE. */
6187 static inline bool
6188 better_fit (elf_find_function_cache * cache,
6189 asymbol * sym,
6190 bfd_vma code_off,
6191 bfd_size_type code_size,
6192 bfd_vma offset)
6194 /* If the symbol is beyond the desired offset, ignore it. */
6195 if (code_off > offset)
6196 return false;
6198 /* If the symbol is further away from the desired
6199 offset than our current best, then ignore it. */
6200 if (code_off < cache->code_off)
6201 return false;
6203 /* On the other hand, if it is closer, then use it. */
6204 if (code_off > cache->code_off)
6205 return true;
6207 /* assert (code_off == cache->code_off); */
6209 /* If our current best fit does not actually reach the desired
6210 offset... */
6211 if (cache->code_off + cache->code_size <= offset)
6212 /* ... then return whichever candidate covers
6213 more area and hence gets closer to OFFSET. */
6214 return code_size > cache->code_size;
6216 /* The current cache'd symbol covers OFFSET. */
6218 /* If the new symbol does not cover the desired offset then skip it. */
6219 if (code_off + code_size <= offset)
6220 return false;
6222 /* Both symbols cover OFFSET. */
6224 /* Prefer functions over non-functions. */
6225 flagword cache_flags = cache->func->flags;
6226 flagword sym_flags = sym->flags;
6228 if ((cache_flags & BSF_FUNCTION) && ((sym_flags & BSF_FUNCTION) == 0))
6229 return false;
6230 if ((sym_flags & BSF_FUNCTION) && ((cache_flags & BSF_FUNCTION) == 0))
6231 return true;
6233 /* FIXME: Should we choose LOCAL over GLOBAL ? */
6235 /* Prefer typed symbols over notyped. */
6236 int cache_type = ELF_ST_TYPE (((elf_symbol_type *) cache->func)->internal_elf_sym.st_info);
6237 int sym_type = ELF_ST_TYPE (((elf_symbol_type *) sym)->internal_elf_sym.st_info);
6239 if (cache_type == STT_NOTYPE && sym_type != STT_NOTYPE)
6240 return true;
6241 if (cache_type != STT_NOTYPE && sym_type == STT_NOTYPE)
6242 return false;
6244 /* Otherwise choose whichever symbol covers a smaller area. */
6245 return code_size < cache->code_size;
6248 /* Find the function to a particular section and offset,
6249 for error reporting. */
6251 asymbol *
6252 _bfd_elf_find_function (bfd *abfd,
6253 asymbol **symbols,
6254 asection *section,
6255 bfd_vma offset,
6256 const char **filename_ptr,
6257 const char **functionname_ptr)
6259 if (symbols == NULL)
6260 return NULL;
6262 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6263 return NULL;
6265 elf_find_function_cache * cache = elf_tdata (abfd)->elf_find_function_cache;
6267 if (cache == NULL)
6269 cache = bfd_zalloc (abfd, sizeof (*cache));
6270 elf_tdata (abfd)->elf_find_function_cache = cache;
6271 if (cache == NULL)
6272 return NULL;
6275 if (cache->last_section != section
6276 || cache->func == NULL
6277 || offset < cache->func->value
6278 || offset >= cache->func->value + cache->code_size)
6280 asymbol *file;
6281 asymbol **p;
6282 /* ??? Given multiple file symbols, it is impossible to reliably
6283 choose the right file name for global symbols. File symbols are
6284 local symbols, and thus all file symbols must sort before any
6285 global symbols. The ELF spec may be interpreted to say that a
6286 file symbol must sort before other local symbols, but currently
6287 ld -r doesn't do this. So, for ld -r output, it is possible to
6288 make a better choice of file name for local symbols by ignoring
6289 file symbols appearing after a given local symbol. */
6290 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
6291 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6293 file = NULL;
6294 state = nothing_seen;
6295 cache->filename = NULL;
6296 cache->func = NULL;
6297 cache->code_size = 0;
6298 cache->code_off = 0;
6299 cache->last_section = section;
6301 for (p = symbols; *p != NULL; p++)
6303 asymbol *sym = *p;
6304 bfd_vma code_off;
6305 bfd_size_type size;
6307 if ((sym->flags & BSF_FILE) != 0)
6309 file = sym;
6310 if (state == symbol_seen)
6311 state = file_after_symbol_seen;
6312 continue;
6315 if (state == nothing_seen)
6316 state = symbol_seen;
6318 size = bed->maybe_function_sym (sym, section, &code_off);
6320 if (size == 0)
6321 continue;
6323 if (better_fit (cache, sym, code_off, size, offset))
6325 cache->func = sym;
6326 cache->code_size = size;
6327 cache->code_off = code_off;
6328 cache->filename = NULL;
6330 if (file != NULL
6331 && ((sym->flags & BSF_LOCAL) != 0
6332 || state != file_after_symbol_seen))
6333 cache->filename = bfd_asymbol_name (file);
6335 /* Otherwise, if the symbol is beyond the desired offset but it
6336 lies within the bounds of the current best match then reduce
6337 the size of the current best match so that future searches
6338 will not not used the cached symbol by mistake. */
6339 else if (code_off > offset
6340 && code_off > cache->code_off
6341 && code_off < cache->code_off + cache->code_size)
6343 cache->code_size = code_off - cache->code_off;
6348 if (cache->func == NULL)
6349 return NULL;
6351 if (filename_ptr)
6352 *filename_ptr = cache->filename;
6353 if (functionname_ptr)
6354 *functionname_ptr = bfd_asymbol_name (cache->func);
6356 return cache->func;