Set GDB version number to 13.1.
[binutils-gdb.git] / bfd / dwarf2.c
blob95f45708e9d155ce349653bdf24eeadc1ee92de5
1 /* DWARF 2 support.
2 Copyright (C) 1994-2022 Free Software Foundation, Inc.
4 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
5 (gavin@cygnus.com).
7 From the dwarf2read.c header:
8 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
9 Inc. with support from Florida State University (under contract
10 with the Ada Joint Program Office), and Silicon Graphics, Inc.
11 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
12 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
13 support in dwarfread.c
15 This file is part of BFD.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 3 of the License, or (at
20 your option) any later version.
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
30 MA 02110-1301, USA. */
32 #include "sysdep.h"
33 #include "bfd.h"
34 #include "libiberty.h"
35 #include "demangle.h"
36 #include "libbfd.h"
37 #include "elf-bfd.h"
38 #include "dwarf2.h"
39 #include "hashtab.h"
40 #include "splay-tree.h"
42 /* The data in the .debug_line statement prologue looks like this. */
44 struct line_head
46 bfd_vma total_length;
47 unsigned short version;
48 bfd_vma prologue_length;
49 unsigned char minimum_instruction_length;
50 unsigned char maximum_ops_per_insn;
51 unsigned char default_is_stmt;
52 int line_base;
53 unsigned char line_range;
54 unsigned char opcode_base;
55 unsigned char *standard_opcode_lengths;
58 /* Attributes have a name and a value. */
60 struct attribute
62 enum dwarf_attribute name;
63 enum dwarf_form form;
64 union
66 char *str;
67 struct dwarf_block *blk;
68 uint64_t val;
69 int64_t sval;
74 /* Blocks are a bunch of untyped bytes. */
75 struct dwarf_block
77 unsigned int size;
78 bfd_byte *data;
81 struct adjusted_section
83 asection *section;
84 bfd_vma adj_vma;
87 /* A trie to map quickly from address range to compilation unit.
89 This is a fairly standard radix-256 trie, used to quickly locate which
90 compilation unit any given address belongs to. Given that each compilation
91 unit may register hundreds of very small and unaligned ranges (which may
92 potentially overlap, due to inlining and other concerns), and a large
93 program may end up containing hundreds of thousands of such ranges, we cannot
94 scan through them linearly without undue slowdown.
96 We use a hybrid trie to avoid memory explosion: There are two types of trie
97 nodes, leaves and interior nodes. (Almost all nodes are leaves, so they
98 take up the bulk of the memory usage.) Leaves contain a simple array of
99 ranges (high/low address) and which compilation unit contains those ranges,
100 and when we get to a leaf, we scan through it linearly. Interior nodes
101 contain pointers to 256 other nodes, keyed by the next byte of the address.
102 So for a 64-bit address like 0x1234567abcd, we would start at the root and go
103 down child[0x00]->child[0x00]->child[0x01]->child[0x23]->child[0x45] etc.,
104 until we hit a leaf. (Nodes are, in general, leaves until they exceed the
105 default allocation of 16 elements, at which point they are converted to
106 interior node if possible.) This gives us near-constant lookup times;
107 the only thing that can be costly is if there are lots of overlapping ranges
108 within a single 256-byte segment of the binary, in which case we have to
109 scan through them all to find the best match.
111 For a binary with few ranges, we will in practice only have a single leaf
112 node at the root, containing a simple array. Thus, the scheme is efficient
113 for both small and large binaries.
116 /* Experiments have shown 16 to be a memory-efficient default leaf size.
117 The only case where a leaf will hold more memory than this, is at the
118 bottomost level (covering 256 bytes in the binary), where we'll expand
119 the leaf to be able to hold more ranges if needed.
121 #define TRIE_LEAF_SIZE 16
123 /* All trie_node pointers will really be trie_leaf or trie_interior,
124 but they have this common head. */
125 struct trie_node
127 /* If zero, we are an interior node.
128 Otherwise, how many ranges we have room for in this leaf. */
129 unsigned int num_room_in_leaf;
132 struct trie_leaf
134 struct trie_node head;
135 unsigned int num_stored_in_leaf;
136 struct {
137 struct comp_unit *unit;
138 bfd_vma low_pc, high_pc;
139 } ranges[TRIE_LEAF_SIZE];
142 struct trie_interior
144 struct trie_node head;
145 struct trie_node *children[256];
148 static struct trie_node *alloc_trie_leaf (bfd *abfd)
150 struct trie_leaf *leaf = bfd_zalloc (abfd, sizeof (struct trie_leaf));
151 if (leaf == NULL)
152 return NULL;
153 leaf->head.num_room_in_leaf = TRIE_LEAF_SIZE;
154 return &leaf->head;
157 struct addr_range
159 bfd_byte *start;
160 bfd_byte *end;
163 /* Return true if address range do intersect. */
165 static bool
166 addr_range_intersects (struct addr_range *r1, struct addr_range *r2)
168 return (r1->start <= r2->start && r2->start < r1->end)
169 || (r1->start <= (r2->end - 1) && (r2->end - 1) < r1->end);
172 /* Compare function for splay tree of addr_ranges. */
174 static int
175 splay_tree_compare_addr_range (splay_tree_key xa, splay_tree_key xb)
177 struct addr_range *r1 = (struct addr_range *) xa;
178 struct addr_range *r2 = (struct addr_range *) xb;
180 if (addr_range_intersects (r1, r2) || addr_range_intersects (r2, r1))
181 return 0;
182 else if (r1->end <= r2->start)
183 return -1;
184 else
185 return 1;
188 /* Splay tree release function for keys (addr_range). */
190 static void
191 splay_tree_free_addr_range (splay_tree_key key)
193 free ((struct addr_range *)key);
196 struct dwarf2_debug_file
198 /* The actual bfd from which debug info was loaded. Might be
199 different to orig_bfd because of gnu_debuglink sections. */
200 bfd *bfd_ptr;
202 /* Pointer to the symbol table. */
203 asymbol **syms;
205 /* The current info pointer for the .debug_info section being parsed. */
206 bfd_byte *info_ptr;
208 /* A pointer to the memory block allocated for .debug_info sections. */
209 bfd_byte *dwarf_info_buffer;
211 /* Length of the loaded .debug_info sections. */
212 bfd_size_type dwarf_info_size;
214 /* Pointer to the .debug_abbrev section loaded into memory. */
215 bfd_byte *dwarf_abbrev_buffer;
217 /* Length of the loaded .debug_abbrev section. */
218 bfd_size_type dwarf_abbrev_size;
220 /* Buffer for decode_line_info. */
221 bfd_byte *dwarf_line_buffer;
223 /* Length of the loaded .debug_line section. */
224 bfd_size_type dwarf_line_size;
226 /* Pointer to the .debug_str section loaded into memory. */
227 bfd_byte *dwarf_str_buffer;
229 /* Length of the loaded .debug_str section. */
230 bfd_size_type dwarf_str_size;
232 /* Pointer to the .debug_str_offsets section loaded into memory. */
233 bfd_byte *dwarf_str_offsets_buffer;
235 /* Length of the loaded .debug_str_offsets section. */
236 bfd_size_type dwarf_str_offsets_size;
238 /* Pointer to the .debug_addr section loaded into memory. */
239 bfd_byte *dwarf_addr_buffer;
241 /* Length of the loaded .debug_addr section. */
242 bfd_size_type dwarf_addr_size;
244 /* Pointer to the .debug_line_str section loaded into memory. */
245 bfd_byte *dwarf_line_str_buffer;
247 /* Length of the loaded .debug_line_str section. */
248 bfd_size_type dwarf_line_str_size;
250 /* Pointer to the .debug_ranges section loaded into memory. */
251 bfd_byte *dwarf_ranges_buffer;
253 /* Length of the loaded .debug_ranges section. */
254 bfd_size_type dwarf_ranges_size;
256 /* Pointer to the .debug_rnglists section loaded into memory. */
257 bfd_byte *dwarf_rnglists_buffer;
259 /* Length of the loaded .debug_rnglists section. */
260 bfd_size_type dwarf_rnglists_size;
262 /* A list of all previously read comp_units. */
263 struct comp_unit *all_comp_units;
265 /* A list of all previously read comp_units with no ranges (yet). */
266 struct comp_unit *all_comp_units_without_ranges;
268 /* Last comp unit in list above. */
269 struct comp_unit *last_comp_unit;
271 /* Line table at line_offset zero. */
272 struct line_info_table *line_table;
274 /* Hash table to map offsets to decoded abbrevs. */
275 htab_t abbrev_offsets;
277 /* Root of a trie to map addresses to compilation units. */
278 struct trie_node *trie_root;
280 /* Splay tree to map info_ptr address to compilation units. */
281 splay_tree comp_unit_tree;
284 struct dwarf2_debug
286 /* Names of the debug sections. */
287 const struct dwarf_debug_section *debug_sections;
289 /* Per-file stuff. */
290 struct dwarf2_debug_file f, alt;
292 /* Pointer to the original bfd for which debug was loaded. This is what
293 we use to compare and so check that the cached debug data is still
294 valid - it saves having to possibly dereference the gnu_debuglink each
295 time. */
296 bfd *orig_bfd;
298 /* If the most recent call to bfd_find_nearest_line was given an
299 address in an inlined function, preserve a pointer into the
300 calling chain for subsequent calls to bfd_find_inliner_info to
301 use. */
302 struct funcinfo *inliner_chain;
304 /* Section VMAs at the time the stash was built. */
305 bfd_vma *sec_vma;
306 /* Number of sections in the SEC_VMA table. */
307 unsigned int sec_vma_count;
309 /* Number of sections whose VMA we must adjust. */
310 int adjusted_section_count;
312 /* Array of sections with adjusted VMA. */
313 struct adjusted_section *adjusted_sections;
315 /* Number of times find_line is called. This is used in
316 the heuristic for enabling the info hash tables. */
317 int info_hash_count;
319 #define STASH_INFO_HASH_TRIGGER 100
321 /* Hash table mapping symbol names to function infos. */
322 struct info_hash_table *funcinfo_hash_table;
324 /* Hash table mapping symbol names to variable infos. */
325 struct info_hash_table *varinfo_hash_table;
327 /* Head of comp_unit list in the last hash table update. */
328 struct comp_unit *hash_units_head;
330 /* Status of info hash. */
331 int info_hash_status;
332 #define STASH_INFO_HASH_OFF 0
333 #define STASH_INFO_HASH_ON 1
334 #define STASH_INFO_HASH_DISABLED 2
336 /* True if we opened bfd_ptr. */
337 bool close_on_cleanup;
340 struct arange
342 struct arange *next;
343 bfd_vma low;
344 bfd_vma high;
347 /* A minimal decoding of DWARF2 compilation units. We only decode
348 what's needed to get to the line number information. */
350 struct comp_unit
352 /* Chain the previously read compilation units. */
353 struct comp_unit *next_unit;
355 /* Chain the previously read compilation units that have no ranges yet.
356 We scan these separately when we have a trie over the ranges.
357 Unused if arange.high != 0. */
358 struct comp_unit *next_unit_without_ranges;
360 /* Likewise, chain the compilation unit read after this one.
361 The comp units are stored in reversed reading order. */
362 struct comp_unit *prev_unit;
364 /* Keep the bfd convenient (for memory allocation). */
365 bfd *abfd;
367 /* The lowest and highest addresses contained in this compilation
368 unit as specified in the compilation unit header. */
369 struct arange arange;
371 /* The DW_AT_name attribute (for error messages). */
372 char *name;
374 /* The abbrev hash table. */
375 struct abbrev_info **abbrevs;
377 /* DW_AT_language. */
378 int lang;
380 /* Note that an error was found by comp_unit_find_nearest_line. */
381 int error;
383 /* The DW_AT_comp_dir attribute. */
384 char *comp_dir;
386 /* TRUE if there is a line number table associated with this comp. unit. */
387 int stmtlist;
389 /* Pointer to the current comp_unit so that we can find a given entry
390 by its reference. */
391 bfd_byte *info_ptr_unit;
393 /* The offset into .debug_line of the line number table. */
394 unsigned long line_offset;
396 /* Pointer to the first child die for the comp unit. */
397 bfd_byte *first_child_die_ptr;
399 /* The end of the comp unit. */
400 bfd_byte *end_ptr;
402 /* The decoded line number, NULL if not yet decoded. */
403 struct line_info_table *line_table;
405 /* A list of the functions found in this comp. unit. */
406 struct funcinfo *function_table;
408 /* A table of function information references searchable by address. */
409 struct lookup_funcinfo *lookup_funcinfo_table;
411 /* Number of functions in the function_table and sorted_function_table. */
412 bfd_size_type number_of_functions;
414 /* A list of the variables found in this comp. unit. */
415 struct varinfo *variable_table;
417 /* Pointers to dwarf2_debug structures. */
418 struct dwarf2_debug *stash;
419 struct dwarf2_debug_file *file;
421 /* DWARF format version for this unit - from unit header. */
422 int version;
424 /* Address size for this unit - from unit header. */
425 unsigned char addr_size;
427 /* Offset size for this unit - from unit header. */
428 unsigned char offset_size;
430 /* Base address for this unit - from DW_AT_low_pc attribute of
431 DW_TAG_compile_unit DIE */
432 bfd_vma base_address;
434 /* TRUE if symbols are cached in hash table for faster lookup by name. */
435 bool cached;
437 /* Used when iterating over trie leaves to know which units we have
438 already seen in this iteration. */
439 bool mark;
441 /* Base address of debug_addr section. */
442 size_t dwarf_addr_offset;
444 /* Base address of string offset table. */
445 size_t dwarf_str_offset;
448 /* This data structure holds the information of an abbrev. */
449 struct abbrev_info
451 unsigned int number; /* Number identifying abbrev. */
452 enum dwarf_tag tag; /* DWARF tag. */
453 bool has_children; /* TRUE if the abbrev has children. */
454 unsigned int num_attrs; /* Number of attributes. */
455 struct attr_abbrev * attrs; /* An array of attribute descriptions. */
456 struct abbrev_info * next; /* Next in chain. */
459 struct attr_abbrev
461 enum dwarf_attribute name;
462 enum dwarf_form form;
463 bfd_vma implicit_const;
466 /* Map of uncompressed DWARF debug section name to compressed one. It
467 is terminated by NULL uncompressed_name. */
469 const struct dwarf_debug_section dwarf_debug_sections[] =
471 { ".debug_abbrev", ".zdebug_abbrev" },
472 { ".debug_aranges", ".zdebug_aranges" },
473 { ".debug_frame", ".zdebug_frame" },
474 { ".debug_info", ".zdebug_info" },
475 { ".debug_info", ".zdebug_info" },
476 { ".debug_line", ".zdebug_line" },
477 { ".debug_loc", ".zdebug_loc" },
478 { ".debug_macinfo", ".zdebug_macinfo" },
479 { ".debug_macro", ".zdebug_macro" },
480 { ".debug_pubnames", ".zdebug_pubnames" },
481 { ".debug_pubtypes", ".zdebug_pubtypes" },
482 { ".debug_ranges", ".zdebug_ranges" },
483 { ".debug_rnglists", ".zdebug_rnglist" },
484 { ".debug_static_func", ".zdebug_static_func" },
485 { ".debug_static_vars", ".zdebug_static_vars" },
486 { ".debug_str", ".zdebug_str", },
487 { ".debug_str", ".zdebug_str", },
488 { ".debug_str_offsets", ".zdebug_str_offsets", },
489 { ".debug_addr", ".zdebug_addr", },
490 { ".debug_line_str", ".zdebug_line_str", },
491 { ".debug_types", ".zdebug_types" },
492 /* GNU DWARF 1 extensions */
493 { ".debug_sfnames", ".zdebug_sfnames" },
494 { ".debug_srcinfo", ".zebug_srcinfo" },
495 /* SGI/MIPS DWARF 2 extensions */
496 { ".debug_funcnames", ".zdebug_funcnames" },
497 { ".debug_typenames", ".zdebug_typenames" },
498 { ".debug_varnames", ".zdebug_varnames" },
499 { ".debug_weaknames", ".zdebug_weaknames" },
500 { NULL, NULL },
503 /* NB/ Numbers in this enum must match up with indices
504 into the dwarf_debug_sections[] array above. */
505 enum dwarf_debug_section_enum
507 debug_abbrev = 0,
508 debug_aranges,
509 debug_frame,
510 debug_info,
511 debug_info_alt,
512 debug_line,
513 debug_loc,
514 debug_macinfo,
515 debug_macro,
516 debug_pubnames,
517 debug_pubtypes,
518 debug_ranges,
519 debug_rnglists,
520 debug_static_func,
521 debug_static_vars,
522 debug_str,
523 debug_str_alt,
524 debug_str_offsets,
525 debug_addr,
526 debug_line_str,
527 debug_types,
528 debug_sfnames,
529 debug_srcinfo,
530 debug_funcnames,
531 debug_typenames,
532 debug_varnames,
533 debug_weaknames,
534 debug_max
537 /* A static assertion. */
538 extern int dwarf_debug_section_assert[ARRAY_SIZE (dwarf_debug_sections)
539 == debug_max + 1 ? 1 : -1];
541 #ifndef ABBREV_HASH_SIZE
542 #define ABBREV_HASH_SIZE 121
543 #endif
544 #ifndef ATTR_ALLOC_CHUNK
545 #define ATTR_ALLOC_CHUNK 4
546 #endif
548 /* Variable and function hash tables. This is used to speed up look-up
549 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
550 In order to share code between variable and function infos, we use
551 a list of untyped pointer for all variable/function info associated with
552 a symbol. We waste a bit of memory for list with one node but that
553 simplifies the code. */
555 struct info_list_node
557 struct info_list_node *next;
558 void *info;
561 /* Info hash entry. */
562 struct info_hash_entry
564 struct bfd_hash_entry root;
565 struct info_list_node *head;
568 struct info_hash_table
570 struct bfd_hash_table base;
573 /* Function to create a new entry in info hash table. */
575 static struct bfd_hash_entry *
576 info_hash_table_newfunc (struct bfd_hash_entry *entry,
577 struct bfd_hash_table *table,
578 const char *string)
580 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
582 /* Allocate the structure if it has not already been allocated by a
583 derived class. */
584 if (ret == NULL)
586 ret = (struct info_hash_entry *) bfd_hash_allocate (table,
587 sizeof (* ret));
588 if (ret == NULL)
589 return NULL;
592 /* Call the allocation method of the base class. */
593 ret = ((struct info_hash_entry *)
594 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
596 /* Initialize the local fields here. */
597 if (ret)
598 ret->head = NULL;
600 return (struct bfd_hash_entry *) ret;
603 /* Function to create a new info hash table. It returns a pointer to the
604 newly created table or NULL if there is any error. We need abfd
605 solely for memory allocation. */
607 static struct info_hash_table *
608 create_info_hash_table (bfd *abfd)
610 struct info_hash_table *hash_table;
612 hash_table = ((struct info_hash_table *)
613 bfd_alloc (abfd, sizeof (struct info_hash_table)));
614 if (!hash_table)
615 return hash_table;
617 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
618 sizeof (struct info_hash_entry)))
620 bfd_release (abfd, hash_table);
621 return NULL;
624 return hash_table;
627 /* Insert an info entry into an info hash table. We do not check of
628 duplicate entries. Also, the caller need to guarantee that the
629 right type of info in inserted as info is passed as a void* pointer.
630 This function returns true if there is no error. */
632 static bool
633 insert_info_hash_table (struct info_hash_table *hash_table,
634 const char *key,
635 void *info,
636 bool copy_p)
638 struct info_hash_entry *entry;
639 struct info_list_node *node;
641 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
642 key, true, copy_p);
643 if (!entry)
644 return false;
646 node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
647 sizeof (*node));
648 if (!node)
649 return false;
651 node->info = info;
652 node->next = entry->head;
653 entry->head = node;
655 return true;
658 /* Look up an info entry list from an info hash table. Return NULL
659 if there is none. */
661 static struct info_list_node *
662 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
664 struct info_hash_entry *entry;
666 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
667 false, false);
668 return entry ? entry->head : NULL;
671 /* Read a section into its appropriate place in the dwarf2_debug
672 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
673 not NULL, use bfd_simple_get_relocated_section_contents to read the
674 section contents, otherwise use bfd_get_section_contents. Fail if
675 the located section does not contain at least OFFSET bytes. */
677 static bool
678 read_section (bfd *abfd,
679 const struct dwarf_debug_section *sec,
680 asymbol **syms,
681 uint64_t offset,
682 bfd_byte **section_buffer,
683 bfd_size_type *section_size)
685 const char *section_name = sec->uncompressed_name;
686 bfd_byte *contents = *section_buffer;
688 /* The section may have already been read. */
689 if (contents == NULL)
691 bfd_size_type amt;
692 asection *msec;
694 msec = bfd_get_section_by_name (abfd, section_name);
695 if (msec == NULL)
697 section_name = sec->compressed_name;
698 msec = bfd_get_section_by_name (abfd, section_name);
700 if (msec == NULL)
702 _bfd_error_handler (_("DWARF error: can't find %s section."),
703 sec->uncompressed_name);
704 bfd_set_error (bfd_error_bad_value);
705 return false;
708 if (_bfd_section_size_insane (abfd, msec))
710 /* PR 26946 */
711 _bfd_error_handler (_("DWARF error: section %s is too big"),
712 section_name);
713 return false;
715 amt = bfd_get_section_limit_octets (abfd, msec);
716 *section_size = amt;
717 /* Paranoia - alloc one extra so that we can make sure a string
718 section is NUL terminated. */
719 amt += 1;
720 if (amt == 0)
722 /* Paranoia - this should never happen. */
723 bfd_set_error (bfd_error_no_memory);
724 return false;
726 contents = (bfd_byte *) bfd_malloc (amt);
727 if (contents == NULL)
728 return false;
729 if (syms
730 ? !bfd_simple_get_relocated_section_contents (abfd, msec, contents,
731 syms)
732 : !bfd_get_section_contents (abfd, msec, contents, 0, *section_size))
734 free (contents);
735 return false;
737 contents[*section_size] = 0;
738 *section_buffer = contents;
741 /* It is possible to get a bad value for the offset into the section
742 that the client wants. Validate it here to avoid trouble later. */
743 if (offset != 0 && offset >= *section_size)
745 /* xgettext: c-format */
746 _bfd_error_handler (_("DWARF error: offset (%" PRIu64 ")"
747 " greater than or equal to %s size (%" PRIu64 ")"),
748 (uint64_t) offset, section_name,
749 (uint64_t) *section_size);
750 bfd_set_error (bfd_error_bad_value);
751 return false;
754 return true;
757 /* Read dwarf information from a buffer. */
759 static inline uint64_t
760 read_n_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end, int n)
762 bfd_byte *buf = *ptr;
763 if (end - buf < n)
765 *ptr = end;
766 return 0;
768 *ptr = buf + n;
769 return bfd_get (n * 8, abfd, buf);
772 static unsigned int
773 read_1_byte (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
775 return read_n_bytes (abfd, ptr, end, 1);
778 static int
779 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte **ptr, bfd_byte *end)
781 bfd_byte *buf = *ptr;
782 if (end - buf < 1)
784 *ptr = end;
785 return 0;
787 *ptr = buf + 1;
788 return bfd_get_signed_8 (abfd, buf);
791 static unsigned int
792 read_2_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
794 return read_n_bytes (abfd, ptr, end, 2);
797 static unsigned int
798 read_3_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
800 unsigned int val = read_1_byte (abfd, ptr, end);
801 val <<= 8;
802 val |= read_1_byte (abfd, ptr, end);
803 val <<= 8;
804 val |= read_1_byte (abfd, ptr, end);
805 if (bfd_little_endian (abfd))
806 val = (((val >> 16) & 0xff)
807 | (val & 0xff00)
808 | ((val & 0xff) << 16));
809 return val;
812 static unsigned int
813 read_4_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
815 return read_n_bytes (abfd, ptr, end, 4);
818 static uint64_t
819 read_8_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
821 return read_n_bytes (abfd, ptr, end, 8);
824 static struct dwarf_block *
825 read_blk (bfd *abfd, bfd_byte **ptr, bfd_byte *end, size_t size)
827 bfd_byte *buf = *ptr;
828 struct dwarf_block *block;
830 block = (struct dwarf_block *) bfd_alloc (abfd, sizeof (*block));
831 if (block == NULL)
832 return NULL;
834 if (size > (size_t) (end - buf))
836 *ptr = end;
837 block->data = NULL;
838 block->size = 0;
840 else
842 *ptr = buf + size;
843 block->data = buf;
844 block->size = size;
846 return block;
849 /* Scans a NUL terminated string starting at *PTR, returning a pointer to it.
850 Bytes at or beyond BUF_END will not be read. Returns NULL if the
851 terminator is not found or if the string is empty. *PTR is
852 incremented over the bytes scanned, including the terminator. */
854 static char *
855 read_string (bfd_byte **ptr,
856 bfd_byte *buf_end)
858 bfd_byte *buf = *ptr;
859 bfd_byte *str = buf;
861 while (buf < buf_end)
862 if (*buf++ == 0)
864 if (str == buf - 1)
865 break;
866 *ptr = buf;
867 return (char *) str;
870 *ptr = buf;
871 return NULL;
874 /* Reads an offset from *PTR and then locates the string at this offset
875 inside the debug string section. Returns a pointer to the string.
876 Increments *PTR by the number of bytes read for the offset. This
877 value is set even if the function fails. Bytes at or beyond
878 BUF_END will not be read. Returns NULL if there was a problem, or
879 if the string is empty. Does not check for NUL termination of the
880 string. */
882 static char *
883 read_indirect_string (struct comp_unit *unit,
884 bfd_byte **ptr,
885 bfd_byte *buf_end)
887 uint64_t offset;
888 struct dwarf2_debug *stash = unit->stash;
889 struct dwarf2_debug_file *file = unit->file;
890 char *str;
892 if (unit->offset_size > (size_t) (buf_end - *ptr))
894 *ptr = buf_end;
895 return NULL;
898 if (unit->offset_size == 4)
899 offset = read_4_bytes (unit->abfd, ptr, buf_end);
900 else
901 offset = read_8_bytes (unit->abfd, ptr, buf_end);
903 if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
904 file->syms, offset,
905 &file->dwarf_str_buffer, &file->dwarf_str_size))
906 return NULL;
908 str = (char *) file->dwarf_str_buffer + offset;
909 if (*str == '\0')
910 return NULL;
911 return str;
914 /* Like read_indirect_string but from .debug_line_str section. */
916 static char *
917 read_indirect_line_string (struct comp_unit *unit,
918 bfd_byte **ptr,
919 bfd_byte *buf_end)
921 uint64_t offset;
922 struct dwarf2_debug *stash = unit->stash;
923 struct dwarf2_debug_file *file = unit->file;
924 char *str;
926 if (unit->offset_size > (size_t) (buf_end - *ptr))
928 *ptr = buf_end;
929 return NULL;
932 if (unit->offset_size == 4)
933 offset = read_4_bytes (unit->abfd, ptr, buf_end);
934 else
935 offset = read_8_bytes (unit->abfd, ptr, buf_end);
937 if (! read_section (unit->abfd, &stash->debug_sections[debug_line_str],
938 file->syms, offset,
939 &file->dwarf_line_str_buffer,
940 &file->dwarf_line_str_size))
941 return NULL;
943 str = (char *) file->dwarf_line_str_buffer + offset;
944 if (*str == '\0')
945 return NULL;
946 return str;
949 /* Like read_indirect_string but uses a .debug_str located in
950 an alternate file pointed to by the .gnu_debugaltlink section.
951 Used to impement DW_FORM_GNU_strp_alt. */
953 static char *
954 read_alt_indirect_string (struct comp_unit *unit,
955 bfd_byte **ptr,
956 bfd_byte *buf_end)
958 uint64_t offset;
959 struct dwarf2_debug *stash = unit->stash;
960 char *str;
962 if (unit->offset_size > (size_t) (buf_end - *ptr))
964 *ptr = buf_end;
965 return NULL;
968 if (unit->offset_size == 4)
969 offset = read_4_bytes (unit->abfd, ptr, buf_end);
970 else
971 offset = read_8_bytes (unit->abfd, ptr, buf_end);
973 if (stash->alt.bfd_ptr == NULL)
975 bfd *debug_bfd;
976 char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
978 if (debug_filename == NULL)
979 return NULL;
981 debug_bfd = bfd_openr (debug_filename, NULL);
982 free (debug_filename);
983 if (debug_bfd == NULL)
984 /* FIXME: Should we report our failure to follow the debuglink ? */
985 return NULL;
987 if (!bfd_check_format (debug_bfd, bfd_object))
989 bfd_close (debug_bfd);
990 return NULL;
992 stash->alt.bfd_ptr = debug_bfd;
995 if (! read_section (unit->stash->alt.bfd_ptr,
996 stash->debug_sections + debug_str_alt,
997 stash->alt.syms, offset,
998 &stash->alt.dwarf_str_buffer,
999 &stash->alt.dwarf_str_size))
1000 return NULL;
1002 str = (char *) stash->alt.dwarf_str_buffer + offset;
1003 if (*str == '\0')
1004 return NULL;
1006 return str;
1009 /* Resolve an alternate reference from UNIT at OFFSET.
1010 Returns a pointer into the loaded alternate CU upon success
1011 or NULL upon failure. */
1013 static bfd_byte *
1014 read_alt_indirect_ref (struct comp_unit *unit, uint64_t offset)
1016 struct dwarf2_debug *stash = unit->stash;
1018 if (stash->alt.bfd_ptr == NULL)
1020 bfd *debug_bfd;
1021 char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
1023 if (debug_filename == NULL)
1024 return NULL;
1026 debug_bfd = bfd_openr (debug_filename, NULL);
1027 free (debug_filename);
1028 if (debug_bfd == NULL)
1029 /* FIXME: Should we report our failure to follow the debuglink ? */
1030 return NULL;
1032 if (!bfd_check_format (debug_bfd, bfd_object))
1034 bfd_close (debug_bfd);
1035 return NULL;
1037 stash->alt.bfd_ptr = debug_bfd;
1040 if (! read_section (unit->stash->alt.bfd_ptr,
1041 stash->debug_sections + debug_info_alt,
1042 stash->alt.syms, offset,
1043 &stash->alt.dwarf_info_buffer,
1044 &stash->alt.dwarf_info_size))
1045 return NULL;
1047 return stash->alt.dwarf_info_buffer + offset;
1050 static uint64_t
1051 read_address (struct comp_unit *unit, bfd_byte **ptr, bfd_byte *buf_end)
1053 bfd_byte *buf = *ptr;
1054 int signed_vma = 0;
1056 if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour)
1057 signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
1059 if (unit->addr_size > (size_t) (buf_end - buf))
1061 *ptr = buf_end;
1062 return 0;
1065 *ptr = buf + unit->addr_size;
1066 if (signed_vma)
1068 switch (unit->addr_size)
1070 case 8:
1071 return bfd_get_signed_64 (unit->abfd, buf);
1072 case 4:
1073 return bfd_get_signed_32 (unit->abfd, buf);
1074 case 2:
1075 return bfd_get_signed_16 (unit->abfd, buf);
1076 default:
1077 abort ();
1080 else
1082 switch (unit->addr_size)
1084 case 8:
1085 return bfd_get_64 (unit->abfd, buf);
1086 case 4:
1087 return bfd_get_32 (unit->abfd, buf);
1088 case 2:
1089 return bfd_get_16 (unit->abfd, buf);
1090 default:
1091 abort ();
1096 /* Lookup an abbrev_info structure in the abbrev hash table. */
1098 static struct abbrev_info *
1099 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
1101 unsigned int hash_number;
1102 struct abbrev_info *abbrev;
1104 hash_number = number % ABBREV_HASH_SIZE;
1105 abbrev = abbrevs[hash_number];
1107 while (abbrev)
1109 if (abbrev->number == number)
1110 return abbrev;
1111 else
1112 abbrev = abbrev->next;
1115 return NULL;
1118 /* We keep a hash table to map .debug_abbrev section offsets to the
1119 array of abbrevs, so that compilation units using the same set of
1120 abbrevs do not waste memory. */
1122 struct abbrev_offset_entry
1124 size_t offset;
1125 struct abbrev_info **abbrevs;
1128 static hashval_t
1129 hash_abbrev (const void *p)
1131 const struct abbrev_offset_entry *ent = p;
1132 return htab_hash_pointer ((void *) ent->offset);
1135 static int
1136 eq_abbrev (const void *pa, const void *pb)
1138 const struct abbrev_offset_entry *a = pa;
1139 const struct abbrev_offset_entry *b = pb;
1140 return a->offset == b->offset;
1143 static void
1144 del_abbrev (void *p)
1146 struct abbrev_offset_entry *ent = p;
1147 struct abbrev_info **abbrevs = ent->abbrevs;
1148 size_t i;
1150 for (i = 0; i < ABBREV_HASH_SIZE; i++)
1152 struct abbrev_info *abbrev = abbrevs[i];
1154 while (abbrev)
1156 free (abbrev->attrs);
1157 abbrev = abbrev->next;
1160 free (ent);
1163 /* In DWARF version 2, the description of the debugging information is
1164 stored in a separate .debug_abbrev section. Before we read any
1165 dies from a section we read in all abbreviations and install them
1166 in a hash table. */
1168 static struct abbrev_info**
1169 read_abbrevs (bfd *abfd, uint64_t offset, struct dwarf2_debug *stash,
1170 struct dwarf2_debug_file *file)
1172 struct abbrev_info **abbrevs;
1173 bfd_byte *abbrev_ptr;
1174 bfd_byte *abbrev_end;
1175 struct abbrev_info *cur_abbrev;
1176 unsigned int abbrev_number, abbrev_name;
1177 unsigned int abbrev_form, hash_number;
1178 size_t amt;
1179 void **slot;
1180 struct abbrev_offset_entry ent = { offset, NULL };
1182 if (ent.offset != offset)
1183 return NULL;
1185 slot = htab_find_slot (file->abbrev_offsets, &ent, INSERT);
1186 if (slot == NULL)
1187 return NULL;
1188 if (*slot != NULL)
1189 return ((struct abbrev_offset_entry *) (*slot))->abbrevs;
1191 if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
1192 file->syms, offset,
1193 &file->dwarf_abbrev_buffer,
1194 &file->dwarf_abbrev_size))
1195 return NULL;
1197 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
1198 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
1199 if (abbrevs == NULL)
1200 return NULL;
1202 abbrev_ptr = file->dwarf_abbrev_buffer + offset;
1203 abbrev_end = file->dwarf_abbrev_buffer + file->dwarf_abbrev_size;
1204 abbrev_number = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1205 false, abbrev_end);
1207 /* Loop until we reach an abbrev number of 0. */
1208 while (abbrev_number)
1210 amt = sizeof (struct abbrev_info);
1211 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
1212 if (cur_abbrev == NULL)
1213 goto fail;
1215 /* Read in abbrev header. */
1216 cur_abbrev->number = abbrev_number;
1217 cur_abbrev->tag = (enum dwarf_tag)
1218 _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1219 false, abbrev_end);
1220 cur_abbrev->has_children = read_1_byte (abfd, &abbrev_ptr, abbrev_end);
1222 /* Now read in declarations. */
1223 for (;;)
1225 /* Initialize it just to avoid a GCC false warning. */
1226 bfd_vma implicit_const = -1;
1228 abbrev_name = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1229 false, abbrev_end);
1230 abbrev_form = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1231 false, abbrev_end);
1232 if (abbrev_form == DW_FORM_implicit_const)
1233 implicit_const = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1234 true, abbrev_end);
1235 if (abbrev_name == 0)
1236 break;
1238 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
1240 struct attr_abbrev *tmp;
1242 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
1243 amt *= sizeof (struct attr_abbrev);
1244 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
1245 if (tmp == NULL)
1246 goto fail;
1247 cur_abbrev->attrs = tmp;
1250 cur_abbrev->attrs[cur_abbrev->num_attrs].name
1251 = (enum dwarf_attribute) abbrev_name;
1252 cur_abbrev->attrs[cur_abbrev->num_attrs].form
1253 = (enum dwarf_form) abbrev_form;
1254 cur_abbrev->attrs[cur_abbrev->num_attrs].implicit_const
1255 = implicit_const;
1256 ++cur_abbrev->num_attrs;
1259 hash_number = abbrev_number % ABBREV_HASH_SIZE;
1260 cur_abbrev->next = abbrevs[hash_number];
1261 abbrevs[hash_number] = cur_abbrev;
1263 /* Get next abbreviation.
1264 Under Irix6 the abbreviations for a compilation unit are not
1265 always properly terminated with an abbrev number of 0.
1266 Exit loop if we encounter an abbreviation which we have
1267 already read (which means we are about to read the abbreviations
1268 for the next compile unit) or if the end of the abbreviation
1269 table is reached. */
1270 if ((size_t) (abbrev_ptr - file->dwarf_abbrev_buffer)
1271 >= file->dwarf_abbrev_size)
1272 break;
1273 abbrev_number = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1274 false, abbrev_end);
1275 if (lookup_abbrev (abbrev_number, abbrevs) != NULL)
1276 break;
1279 *slot = bfd_malloc (sizeof ent);
1280 if (!*slot)
1281 goto fail;
1282 ent.abbrevs = abbrevs;
1283 memcpy (*slot, &ent, sizeof ent);
1284 return abbrevs;
1286 fail:
1287 if (abbrevs != NULL)
1289 size_t i;
1291 for (i = 0; i < ABBREV_HASH_SIZE; i++)
1293 struct abbrev_info *abbrev = abbrevs[i];
1295 while (abbrev)
1297 free (abbrev->attrs);
1298 abbrev = abbrev->next;
1301 free (abbrevs);
1303 return NULL;
1306 /* Returns true if the form is one which has a string value. */
1308 static bool
1309 is_str_form (const struct attribute *attr)
1311 switch (attr->form)
1313 case DW_FORM_string:
1314 case DW_FORM_strp:
1315 case DW_FORM_strx:
1316 case DW_FORM_strx1:
1317 case DW_FORM_strx2:
1318 case DW_FORM_strx3:
1319 case DW_FORM_strx4:
1320 case DW_FORM_line_strp:
1321 case DW_FORM_GNU_strp_alt:
1322 return true;
1324 default:
1325 return false;
1329 /* Returns true if the form is one which has an integer value. */
1331 static bool
1332 is_int_form (const struct attribute *attr)
1334 switch (attr->form)
1336 case DW_FORM_addr:
1337 case DW_FORM_data2:
1338 case DW_FORM_data4:
1339 case DW_FORM_data8:
1340 case DW_FORM_data1:
1341 case DW_FORM_flag:
1342 case DW_FORM_sdata:
1343 case DW_FORM_udata:
1344 case DW_FORM_ref_addr:
1345 case DW_FORM_ref1:
1346 case DW_FORM_ref2:
1347 case DW_FORM_ref4:
1348 case DW_FORM_ref8:
1349 case DW_FORM_ref_udata:
1350 case DW_FORM_sec_offset:
1351 case DW_FORM_flag_present:
1352 case DW_FORM_ref_sig8:
1353 case DW_FORM_addrx:
1354 case DW_FORM_implicit_const:
1355 case DW_FORM_addrx1:
1356 case DW_FORM_addrx2:
1357 case DW_FORM_addrx3:
1358 case DW_FORM_addrx4:
1359 case DW_FORM_GNU_ref_alt:
1360 return true;
1362 default:
1363 return false;
1367 /* Returns true if the form is strx[1-4]. */
1369 static inline bool
1370 is_strx_form (enum dwarf_form form)
1372 return (form == DW_FORM_strx
1373 || form == DW_FORM_strx1
1374 || form == DW_FORM_strx2
1375 || form == DW_FORM_strx3
1376 || form == DW_FORM_strx4);
1379 /* Return true if the form is addrx[1-4]. */
1381 static inline bool
1382 is_addrx_form (enum dwarf_form form)
1384 return (form == DW_FORM_addrx
1385 || form == DW_FORM_addrx1
1386 || form == DW_FORM_addrx2
1387 || form == DW_FORM_addrx3
1388 || form == DW_FORM_addrx4);
1391 /* Returns the address in .debug_addr section using DW_AT_addr_base.
1392 Used to implement DW_FORM_addrx*. */
1393 static uint64_t
1394 read_indexed_address (uint64_t idx, struct comp_unit *unit)
1396 struct dwarf2_debug *stash = unit->stash;
1397 struct dwarf2_debug_file *file = unit->file;
1398 bfd_byte *info_ptr;
1399 size_t offset;
1401 if (stash == NULL)
1402 return 0;
1404 if (!read_section (unit->abfd, &stash->debug_sections[debug_addr],
1405 file->syms, 0,
1406 &file->dwarf_addr_buffer, &file->dwarf_addr_size))
1407 return 0;
1409 if (_bfd_mul_overflow (idx, unit->addr_size, &offset))
1410 return 0;
1412 offset += unit->dwarf_addr_offset;
1413 if (offset < unit->dwarf_addr_offset
1414 || offset > file->dwarf_addr_size
1415 || file->dwarf_addr_size - offset < unit->offset_size)
1416 return 0;
1418 info_ptr = file->dwarf_addr_buffer + offset;
1420 if (unit->addr_size == 4)
1421 return bfd_get_32 (unit->abfd, info_ptr);
1422 else if (unit->addr_size == 8)
1423 return bfd_get_64 (unit->abfd, info_ptr);
1424 else
1425 return 0;
1428 /* Returns the string using DW_AT_str_offsets_base.
1429 Used to implement DW_FORM_strx*. */
1430 static const char *
1431 read_indexed_string (uint64_t idx, struct comp_unit *unit)
1433 struct dwarf2_debug *stash = unit->stash;
1434 struct dwarf2_debug_file *file = unit->file;
1435 bfd_byte *info_ptr;
1436 uint64_t str_offset;
1437 size_t offset;
1439 if (stash == NULL)
1440 return NULL;
1442 if (!read_section (unit->abfd, &stash->debug_sections[debug_str],
1443 file->syms, 0,
1444 &file->dwarf_str_buffer, &file->dwarf_str_size))
1445 return NULL;
1447 if (!read_section (unit->abfd, &stash->debug_sections[debug_str_offsets],
1448 file->syms, 0,
1449 &file->dwarf_str_offsets_buffer,
1450 &file->dwarf_str_offsets_size))
1451 return NULL;
1453 if (_bfd_mul_overflow (idx, unit->offset_size, &offset))
1454 return NULL;
1456 offset += unit->dwarf_str_offset;
1457 if (offset < unit->dwarf_str_offset
1458 || offset > file->dwarf_str_offsets_size
1459 || file->dwarf_str_offsets_size - offset < unit->offset_size)
1460 return NULL;
1462 info_ptr = file->dwarf_str_offsets_buffer + offset;
1464 if (unit->offset_size == 4)
1465 str_offset = bfd_get_32 (unit->abfd, info_ptr);
1466 else if (unit->offset_size == 8)
1467 str_offset = bfd_get_64 (unit->abfd, info_ptr);
1468 else
1469 return NULL;
1471 if (str_offset >= file->dwarf_str_size)
1472 return NULL;
1473 return (const char *) file->dwarf_str_buffer + str_offset;
1476 /* Read and fill in the value of attribute ATTR as described by FORM.
1477 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1478 Returns an updated INFO_PTR taking into account the amount of data read. */
1480 static bfd_byte *
1481 read_attribute_value (struct attribute * attr,
1482 unsigned form,
1483 bfd_vma implicit_const,
1484 struct comp_unit * unit,
1485 bfd_byte * info_ptr,
1486 bfd_byte * info_ptr_end)
1488 bfd *abfd = unit->abfd;
1489 size_t amt;
1491 if (info_ptr >= info_ptr_end && form != DW_FORM_flag_present)
1493 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1494 bfd_set_error (bfd_error_bad_value);
1495 return NULL;
1498 attr->form = (enum dwarf_form) form;
1500 switch (form)
1502 case DW_FORM_flag_present:
1503 attr->u.val = 1;
1504 break;
1505 case DW_FORM_ref_addr:
1506 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1507 DWARF3. */
1508 if (unit->version >= 3)
1510 if (unit->offset_size == 4)
1511 attr->u.val = read_4_bytes (unit->abfd, &info_ptr, info_ptr_end);
1512 else
1513 attr->u.val = read_8_bytes (unit->abfd, &info_ptr, info_ptr_end);
1514 break;
1516 /* FALLTHROUGH */
1517 case DW_FORM_addr:
1518 attr->u.val = read_address (unit, &info_ptr, info_ptr_end);
1519 break;
1520 case DW_FORM_GNU_ref_alt:
1521 case DW_FORM_sec_offset:
1522 if (unit->offset_size == 4)
1523 attr->u.val = read_4_bytes (unit->abfd, &info_ptr, info_ptr_end);
1524 else
1525 attr->u.val = read_8_bytes (unit->abfd, &info_ptr, info_ptr_end);
1526 break;
1527 case DW_FORM_block2:
1528 amt = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1529 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1530 if (attr->u.blk == NULL)
1531 return NULL;
1532 break;
1533 case DW_FORM_block4:
1534 amt = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1535 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1536 if (attr->u.blk == NULL)
1537 return NULL;
1538 break;
1539 case DW_FORM_ref1:
1540 case DW_FORM_flag:
1541 case DW_FORM_data1:
1542 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1543 break;
1544 case DW_FORM_addrx1:
1545 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1546 /* dwarf_addr_offset value 0 indicates the attribute DW_AT_addr_base
1547 is not yet read. */
1548 if (unit->dwarf_addr_offset != 0)
1549 attr->u.val = read_indexed_address (attr->u.val, unit);
1550 break;
1551 case DW_FORM_data2:
1552 case DW_FORM_ref2:
1553 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1554 break;
1555 case DW_FORM_addrx2:
1556 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1557 if (unit->dwarf_addr_offset != 0)
1558 attr->u.val = read_indexed_address (attr->u.val, unit);
1559 break;
1560 case DW_FORM_addrx3:
1561 attr->u.val = read_3_bytes (abfd, &info_ptr, info_ptr_end);
1562 if (unit->dwarf_addr_offset != 0)
1563 attr->u.val = read_indexed_address(attr->u.val, unit);
1564 break;
1565 case DW_FORM_ref4:
1566 case DW_FORM_data4:
1567 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1568 break;
1569 case DW_FORM_addrx4:
1570 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1571 if (unit->dwarf_addr_offset != 0)
1572 attr->u.val = read_indexed_address (attr->u.val, unit);
1573 break;
1574 case DW_FORM_data8:
1575 case DW_FORM_ref8:
1576 case DW_FORM_ref_sig8:
1577 attr->u.val = read_8_bytes (abfd, &info_ptr, info_ptr_end);
1578 break;
1579 case DW_FORM_string:
1580 attr->u.str = read_string (&info_ptr, info_ptr_end);
1581 break;
1582 case DW_FORM_strp:
1583 attr->u.str = read_indirect_string (unit, &info_ptr, info_ptr_end);
1584 break;
1585 case DW_FORM_line_strp:
1586 attr->u.str = read_indirect_line_string (unit, &info_ptr, info_ptr_end);
1587 break;
1588 case DW_FORM_GNU_strp_alt:
1589 attr->u.str = read_alt_indirect_string (unit, &info_ptr, info_ptr_end);
1590 break;
1591 case DW_FORM_strx1:
1592 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1593 /* dwarf_str_offset value 0 indicates the attribute DW_AT_str_offsets_base
1594 is not yet read. */
1595 if (unit->dwarf_str_offset != 0)
1596 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1597 else
1598 attr->u.str = NULL;
1599 break;
1600 case DW_FORM_strx2:
1601 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1602 if (unit->dwarf_str_offset != 0)
1603 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1604 else
1605 attr->u.str = NULL;
1606 break;
1607 case DW_FORM_strx3:
1608 attr->u.val = read_3_bytes (abfd, &info_ptr, info_ptr_end);
1609 if (unit->dwarf_str_offset != 0)
1610 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1611 else
1612 attr->u.str = NULL;
1613 break;
1614 case DW_FORM_strx4:
1615 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1616 if (unit->dwarf_str_offset != 0)
1617 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1618 else
1619 attr->u.str = NULL;
1620 break;
1621 case DW_FORM_strx:
1622 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1623 false, info_ptr_end);
1624 if (unit->dwarf_str_offset != 0)
1625 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1626 else
1627 attr->u.str = NULL;
1628 break;
1629 case DW_FORM_exprloc:
1630 case DW_FORM_block:
1631 amt = _bfd_safe_read_leb128 (abfd, &info_ptr,
1632 false, info_ptr_end);
1633 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1634 if (attr->u.blk == NULL)
1635 return NULL;
1636 break;
1637 case DW_FORM_block1:
1638 amt = read_1_byte (abfd, &info_ptr, info_ptr_end);
1639 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1640 if (attr->u.blk == NULL)
1641 return NULL;
1642 break;
1643 case DW_FORM_sdata:
1644 attr->u.sval = _bfd_safe_read_leb128 (abfd, &info_ptr,
1645 true, info_ptr_end);
1646 break;
1648 case DW_FORM_rnglistx:
1649 case DW_FORM_loclistx:
1650 /* FIXME: Add support for these forms! */
1651 /* Fall through. */
1652 case DW_FORM_ref_udata:
1653 case DW_FORM_udata:
1654 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1655 false, info_ptr_end);
1656 break;
1657 case DW_FORM_addrx:
1658 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1659 false, info_ptr_end);
1660 if (unit->dwarf_addr_offset != 0)
1661 attr->u.val = read_indexed_address (attr->u.val, unit);
1662 break;
1663 case DW_FORM_indirect:
1664 form = _bfd_safe_read_leb128 (abfd, &info_ptr,
1665 false, info_ptr_end);
1666 if (form == DW_FORM_implicit_const)
1667 implicit_const = _bfd_safe_read_leb128 (abfd, &info_ptr,
1668 true, info_ptr_end);
1669 info_ptr = read_attribute_value (attr, form, implicit_const, unit,
1670 info_ptr, info_ptr_end);
1671 break;
1672 case DW_FORM_implicit_const:
1673 attr->form = DW_FORM_sdata;
1674 attr->u.sval = implicit_const;
1675 break;
1676 case DW_FORM_data16:
1677 /* This is really a "constant", but there is no way to store that
1678 so pretend it is a 16 byte block instead. */
1679 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, 16);
1680 if (attr->u.blk == NULL)
1681 return NULL;
1682 break;
1684 default:
1685 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1686 form);
1687 bfd_set_error (bfd_error_bad_value);
1688 return NULL;
1690 return info_ptr;
1693 /* Read an attribute described by an abbreviated attribute. */
1695 static bfd_byte *
1696 read_attribute (struct attribute * attr,
1697 struct attr_abbrev * abbrev,
1698 struct comp_unit * unit,
1699 bfd_byte * info_ptr,
1700 bfd_byte * info_ptr_end)
1702 attr->name = abbrev->name;
1703 info_ptr = read_attribute_value (attr, abbrev->form, abbrev->implicit_const,
1704 unit, info_ptr, info_ptr_end);
1705 return info_ptr;
1708 /* Return mangling style given LANG. */
1710 static int
1711 mangle_style (int lang)
1713 switch (lang)
1715 case DW_LANG_Ada83:
1716 case DW_LANG_Ada95:
1717 return DMGL_GNAT;
1719 case DW_LANG_C_plus_plus:
1720 case DW_LANG_C_plus_plus_03:
1721 case DW_LANG_C_plus_plus_11:
1722 case DW_LANG_C_plus_plus_14:
1723 return DMGL_GNU_V3;
1725 case DW_LANG_Java:
1726 return DMGL_JAVA;
1728 case DW_LANG_D:
1729 return DMGL_DLANG;
1731 case DW_LANG_Rust:
1732 case DW_LANG_Rust_old:
1733 return DMGL_RUST;
1735 default:
1736 return DMGL_AUTO;
1738 case DW_LANG_C89:
1739 case DW_LANG_C:
1740 case DW_LANG_Cobol74:
1741 case DW_LANG_Cobol85:
1742 case DW_LANG_Fortran77:
1743 case DW_LANG_Pascal83:
1744 case DW_LANG_PLI:
1745 case DW_LANG_C99:
1746 case DW_LANG_UPC:
1747 case DW_LANG_C11:
1748 case DW_LANG_Mips_Assembler:
1749 case DW_LANG_Upc:
1750 case DW_LANG_HP_Basic91:
1751 case DW_LANG_HP_IMacro:
1752 case DW_LANG_HP_Assembler:
1753 return 0;
1757 /* Source line information table routines. */
1759 #define FILE_ALLOC_CHUNK 5
1760 #define DIR_ALLOC_CHUNK 5
1762 struct line_info
1764 struct line_info * prev_line;
1765 bfd_vma address;
1766 char * filename;
1767 unsigned int line;
1768 unsigned int column;
1769 unsigned int discriminator;
1770 unsigned char op_index;
1771 unsigned char end_sequence; /* End of (sequential) code sequence. */
1774 struct fileinfo
1776 char * name;
1777 unsigned int dir;
1778 unsigned int time;
1779 unsigned int size;
1782 struct line_sequence
1784 bfd_vma low_pc;
1785 struct line_sequence* prev_sequence;
1786 struct line_info* last_line; /* Largest VMA. */
1787 struct line_info** line_info_lookup;
1788 bfd_size_type num_lines;
1791 struct line_info_table
1793 bfd * abfd;
1794 unsigned int num_files;
1795 unsigned int num_dirs;
1796 unsigned int num_sequences;
1797 bool use_dir_and_file_0;
1798 char * comp_dir;
1799 char ** dirs;
1800 struct fileinfo* files;
1801 struct line_sequence* sequences;
1802 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
1805 /* Remember some information about each function. If the function is
1806 inlined (DW_TAG_inlined_subroutine) it may have two additional
1807 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1808 source code location where this function was inlined. */
1810 struct funcinfo
1812 /* Pointer to previous function in list of all functions. */
1813 struct funcinfo *prev_func;
1814 /* Pointer to function one scope higher. */
1815 struct funcinfo *caller_func;
1816 /* Source location file name where caller_func inlines this func. */
1817 char *caller_file;
1818 /* Source location file name. */
1819 char *file;
1820 /* Source location line number where caller_func inlines this func. */
1821 int caller_line;
1822 /* Source location line number. */
1823 int line;
1824 int tag;
1825 bool is_linkage;
1826 const char *name;
1827 struct arange arange;
1828 /* The offset of the funcinfo from the start of the unit. */
1829 uint64_t unit_offset;
1832 struct lookup_funcinfo
1834 /* Function information corresponding to this lookup table entry. */
1835 struct funcinfo *funcinfo;
1837 /* The lowest address for this specific function. */
1838 bfd_vma low_addr;
1840 /* The highest address of this function before the lookup table is sorted.
1841 The highest address of all prior functions after the lookup table is
1842 sorted, which is used for binary search. */
1843 bfd_vma high_addr;
1844 /* Index of this function, used to ensure qsort is stable. */
1845 unsigned int idx;
1848 struct varinfo
1850 /* Pointer to previous variable in list of all variables. */
1851 struct varinfo *prev_var;
1852 /* The offset of the varinfo from the start of the unit. */
1853 uint64_t unit_offset;
1854 /* Source location file name. */
1855 char *file;
1856 /* Source location line number. */
1857 int line;
1858 /* The type of this variable. */
1859 int tag;
1860 /* The name of the variable, if it has one. */
1861 const char *name;
1862 /* The address of the variable. */
1863 bfd_vma addr;
1864 /* Is this a stack variable? */
1865 bool stack;
1868 /* Return TRUE if NEW_LINE should sort after LINE. */
1870 static inline bool
1871 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1873 return (new_line->address > line->address
1874 || (new_line->address == line->address
1875 && new_line->op_index > line->op_index));
1879 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1880 that the list is sorted. Note that the line_info list is sorted from
1881 highest to lowest VMA (with possible duplicates); that is,
1882 line_info->prev_line always accesses an equal or smaller VMA. */
1884 static bool
1885 add_line_info (struct line_info_table *table,
1886 bfd_vma address,
1887 unsigned char op_index,
1888 char *filename,
1889 unsigned int line,
1890 unsigned int column,
1891 unsigned int discriminator,
1892 int end_sequence)
1894 size_t amt = sizeof (struct line_info);
1895 struct line_sequence* seq = table->sequences;
1896 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
1898 if (info == NULL)
1899 return false;
1901 /* Set member data of 'info'. */
1902 info->prev_line = NULL;
1903 info->address = address;
1904 info->op_index = op_index;
1905 info->line = line;
1906 info->column = column;
1907 info->discriminator = discriminator;
1908 info->end_sequence = end_sequence;
1910 if (filename && filename[0])
1912 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1913 if (info->filename == NULL)
1914 return false;
1915 strcpy (info->filename, filename);
1917 else
1918 info->filename = NULL;
1920 /* Find the correct location for 'info'. Normally we will receive
1921 new line_info data 1) in order and 2) with increasing VMAs.
1922 However some compilers break the rules (cf. decode_line_info) and
1923 so we include some heuristics for quickly finding the correct
1924 location for 'info'. In particular, these heuristics optimize for
1925 the common case in which the VMA sequence that we receive is a
1926 list of locally sorted VMAs such as
1927 p...z a...j (where a < j < p < z)
1929 Note: table->lcl_head is used to head an *actual* or *possible*
1930 sub-sequence within the list (such as a...j) that is not directly
1931 headed by table->last_line
1933 Note: we may receive duplicate entries from 'decode_line_info'. */
1935 if (seq
1936 && seq->last_line->address == address
1937 && seq->last_line->op_index == op_index
1938 && seq->last_line->end_sequence == end_sequence)
1940 /* We only keep the last entry with the same address and end
1941 sequence. See PR ld/4986. */
1942 if (table->lcl_head == seq->last_line)
1943 table->lcl_head = info;
1944 info->prev_line = seq->last_line->prev_line;
1945 seq->last_line = info;
1947 else if (!seq || seq->last_line->end_sequence)
1949 /* Start a new line sequence. */
1950 amt = sizeof (struct line_sequence);
1951 seq = (struct line_sequence *) bfd_malloc (amt);
1952 if (seq == NULL)
1953 return false;
1954 seq->low_pc = address;
1955 seq->prev_sequence = table->sequences;
1956 seq->last_line = info;
1957 table->lcl_head = info;
1958 table->sequences = seq;
1959 table->num_sequences++;
1961 else if (info->end_sequence
1962 || new_line_sorts_after (info, seq->last_line))
1964 /* Normal case: add 'info' to the beginning of the current sequence. */
1965 info->prev_line = seq->last_line;
1966 seq->last_line = info;
1968 /* lcl_head: initialize to head a *possible* sequence at the end. */
1969 if (!table->lcl_head)
1970 table->lcl_head = info;
1972 else if (!new_line_sorts_after (info, table->lcl_head)
1973 && (!table->lcl_head->prev_line
1974 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1976 /* Abnormal but easy: lcl_head is the head of 'info'. */
1977 info->prev_line = table->lcl_head->prev_line;
1978 table->lcl_head->prev_line = info;
1980 else
1982 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1983 are valid heads for 'info'. Reset 'lcl_head'. */
1984 struct line_info* li2 = seq->last_line; /* Always non-NULL. */
1985 struct line_info* li1 = li2->prev_line;
1987 while (li1)
1989 if (!new_line_sorts_after (info, li2)
1990 && new_line_sorts_after (info, li1))
1991 break;
1993 li2 = li1; /* always non-NULL */
1994 li1 = li1->prev_line;
1996 table->lcl_head = li2;
1997 info->prev_line = table->lcl_head->prev_line;
1998 table->lcl_head->prev_line = info;
1999 if (address < seq->low_pc)
2000 seq->low_pc = address;
2002 return true;
2005 /* Extract a fully qualified filename from a line info table.
2006 The returned string has been malloc'ed and it is the caller's
2007 responsibility to free it. */
2009 static char *
2010 concat_filename (struct line_info_table *table, unsigned int file)
2012 char *filename;
2014 /* Pre DWARF-5 entry 0 in the directory and filename tables was not used.
2015 So in order to save space in the tables used here the info for, eg
2016 directory 1 is stored in slot 0 of the directory table, directory 2
2017 in slot 1 and so on.
2019 Starting with DWARF-5 the 0'th entry is used so there is a one to one
2020 mapping between DWARF slots and internal table entries. */
2021 if (! table->use_dir_and_file_0)
2023 /* Pre DWARF-5, FILE == 0 means unknown. */
2024 if (file == 0)
2025 return strdup ("<unknown>");
2026 -- file;
2029 if (table == NULL || file >= table->num_files)
2031 _bfd_error_handler
2032 (_("DWARF error: mangled line number section (bad file number)"));
2033 return strdup ("<unknown>");
2036 filename = table->files[file].name;
2038 if (filename == NULL)
2039 return strdup ("<unknown>");
2041 if (!IS_ABSOLUTE_PATH (filename))
2043 char *dir_name = NULL;
2044 char *subdir_name = NULL;
2045 char *name;
2046 size_t len;
2048 if (table->files[file].dir
2049 /* PR 17512: file: 0317e960. */
2050 && table->files[file].dir <= table->num_dirs
2051 /* PR 17512: file: 7f3d2e4b. */
2052 && table->dirs != NULL)
2054 if (table->use_dir_and_file_0)
2055 subdir_name = table->dirs[table->files[file].dir];
2056 else
2057 subdir_name = table->dirs[table->files[file].dir - 1];
2060 if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
2061 dir_name = table->comp_dir;
2063 if (!dir_name)
2065 dir_name = subdir_name;
2066 subdir_name = NULL;
2069 if (!dir_name)
2070 return strdup (filename);
2072 len = strlen (dir_name) + strlen (filename) + 2;
2074 if (subdir_name)
2076 len += strlen (subdir_name) + 1;
2077 name = (char *) bfd_malloc (len);
2078 if (name)
2079 sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
2081 else
2083 name = (char *) bfd_malloc (len);
2084 if (name)
2085 sprintf (name, "%s/%s", dir_name, filename);
2088 return name;
2091 return strdup (filename);
2094 /* Number of bits in a bfd_vma. */
2095 #define VMA_BITS (8 * sizeof (bfd_vma))
2097 /* Check whether [low1, high1) can be combined with [low2, high2),
2098 i.e., they touch or overlap. */
2100 static bool
2101 ranges_overlap (bfd_vma low1,
2102 bfd_vma high1,
2103 bfd_vma low2,
2104 bfd_vma high2)
2106 if (low1 == low2 || high1 == high2)
2107 return true;
2109 /* Sort so that low1 is below low2. */
2110 if (low1 > low2)
2112 bfd_vma tmp;
2114 tmp = low1;
2115 low1 = low2;
2116 low2 = tmp;
2118 tmp = high1;
2119 high1 = high2;
2120 high2 = tmp;
2123 /* We touch iff low2 == high1.
2124 We overlap iff low2 is within [low1, high1). */
2125 return low2 <= high1;
2128 /* Insert an address range in the trie mapping addresses to compilation units.
2129 Will return the new trie node (usually the same as is being sent in, but
2130 in case of a leaf-to-interior conversion, or expansion of a leaf, it may be
2131 different), or NULL on failure. */
2133 static struct trie_node *
2134 insert_arange_in_trie (bfd *abfd,
2135 struct trie_node *trie,
2136 bfd_vma trie_pc,
2137 unsigned int trie_pc_bits,
2138 struct comp_unit *unit,
2139 bfd_vma low_pc,
2140 bfd_vma high_pc)
2142 bfd_vma clamped_low_pc, clamped_high_pc;
2143 int ch, from_ch, to_ch;
2144 bool is_full_leaf = false;
2146 /* See if we can extend any of the existing ranges. This merging
2147 isn't perfect (if merging opens up the possibility of merging two existing
2148 ranges, we won't find them), but it takes the majority of the cases. */
2149 if (trie->num_room_in_leaf > 0)
2151 struct trie_leaf *leaf = (struct trie_leaf *) trie;
2152 unsigned int i;
2154 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2156 if (leaf->ranges[i].unit == unit
2157 && ranges_overlap (low_pc, high_pc,
2158 leaf->ranges[i].low_pc,
2159 leaf->ranges[i].high_pc))
2161 if (low_pc < leaf->ranges[i].low_pc)
2162 leaf->ranges[i].low_pc = low_pc;
2163 if (high_pc > leaf->ranges[i].high_pc)
2164 leaf->ranges[i].high_pc = high_pc;
2165 return trie;
2169 is_full_leaf = leaf->num_stored_in_leaf == trie->num_room_in_leaf;
2172 /* If we're a leaf with no more room and we're _not_ at the bottom,
2173 convert to an interior node. */
2174 if (is_full_leaf && trie_pc_bits < VMA_BITS)
2176 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
2177 unsigned int i;
2179 trie = bfd_zalloc (abfd, sizeof (struct trie_interior));
2180 if (!trie)
2181 return NULL;
2182 is_full_leaf = false;
2184 /* TODO: If we wanted to save a little more memory at the cost of
2185 complexity, we could have reused the old leaf node as one of the
2186 children of the new interior node, instead of throwing it away. */
2187 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2189 if (!insert_arange_in_trie (abfd, trie, trie_pc, trie_pc_bits,
2190 leaf->ranges[i].unit, leaf->ranges[i].low_pc,
2191 leaf->ranges[i].high_pc))
2192 return NULL;
2196 /* If we're a leaf with no more room and we _are_ at the bottom,
2197 we have no choice but to just make it larger. */
2198 if (is_full_leaf)
2200 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
2201 unsigned int new_room_in_leaf = trie->num_room_in_leaf * 2;
2202 struct trie_leaf *new_leaf;
2203 size_t amt = (sizeof (struct trie_leaf)
2204 + ((new_room_in_leaf - TRIE_LEAF_SIZE)
2205 * sizeof (leaf->ranges[0])));
2206 new_leaf = bfd_zalloc (abfd, amt);
2207 new_leaf->head.num_room_in_leaf = new_room_in_leaf;
2208 new_leaf->num_stored_in_leaf = leaf->num_stored_in_leaf;
2210 memcpy (new_leaf->ranges,
2211 leaf->ranges,
2212 leaf->num_stored_in_leaf * sizeof (leaf->ranges[0]));
2213 trie = &new_leaf->head;
2214 is_full_leaf = false;
2216 /* Now the insert below will go through. */
2219 /* If we're a leaf (now with room), we can just insert at the end. */
2220 if (trie->num_room_in_leaf > 0)
2222 struct trie_leaf *leaf = (struct trie_leaf *) trie;
2224 unsigned int i = leaf->num_stored_in_leaf++;
2225 leaf->ranges[i].unit = unit;
2226 leaf->ranges[i].low_pc = low_pc;
2227 leaf->ranges[i].high_pc = high_pc;
2228 return trie;
2231 /* Now we are definitely an interior node, so recurse into all
2232 the relevant buckets. */
2234 /* Clamp the range to the current trie bucket. */
2235 clamped_low_pc = low_pc;
2236 clamped_high_pc = high_pc;
2237 if (trie_pc_bits > 0)
2239 bfd_vma bucket_high_pc =
2240 trie_pc + ((bfd_vma) -1 >> trie_pc_bits); /* Inclusive. */
2241 if (clamped_low_pc < trie_pc)
2242 clamped_low_pc = trie_pc;
2243 if (clamped_high_pc > bucket_high_pc)
2244 clamped_high_pc = bucket_high_pc;
2247 /* Insert the ranges in all buckets that it spans. */
2248 from_ch = (clamped_low_pc >> (VMA_BITS - trie_pc_bits - 8)) & 0xff;
2249 to_ch = ((clamped_high_pc - 1) >> (VMA_BITS - trie_pc_bits - 8)) & 0xff;
2250 for (ch = from_ch; ch <= to_ch; ++ch)
2252 struct trie_interior *interior = (struct trie_interior *) trie;
2253 struct trie_node *child = interior->children[ch];
2255 if (child == NULL)
2257 child = alloc_trie_leaf (abfd);
2258 if (!child)
2259 return NULL;
2261 bfd_vma bucket = (bfd_vma) ch << (VMA_BITS - trie_pc_bits - 8);
2262 child = insert_arange_in_trie (abfd,
2263 child,
2264 trie_pc + bucket,
2265 trie_pc_bits + 8,
2266 unit,
2267 low_pc,
2268 high_pc);
2269 if (!child)
2270 return NULL;
2272 interior->children[ch] = child;
2275 return trie;
2278 static bool
2279 arange_add (struct comp_unit *unit, struct arange *first_arange,
2280 struct trie_node **trie_root, bfd_vma low_pc, bfd_vma high_pc)
2282 struct arange *arange;
2284 /* Ignore empty ranges. */
2285 if (low_pc == high_pc)
2286 return true;
2288 if (trie_root != NULL)
2290 *trie_root = insert_arange_in_trie (unit->file->bfd_ptr,
2291 *trie_root,
2294 unit,
2295 low_pc,
2296 high_pc);
2297 if (*trie_root == NULL)
2298 return false;
2301 /* If the first arange is empty, use it. */
2302 if (first_arange->high == 0)
2304 first_arange->low = low_pc;
2305 first_arange->high = high_pc;
2306 return true;
2309 /* Next see if we can cheaply extend an existing range. */
2310 arange = first_arange;
2313 if (low_pc == arange->high)
2315 arange->high = high_pc;
2316 return true;
2318 if (high_pc == arange->low)
2320 arange->low = low_pc;
2321 return true;
2323 arange = arange->next;
2325 while (arange);
2327 /* Need to allocate a new arange and insert it into the arange list.
2328 Order isn't significant, so just insert after the first arange. */
2329 arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
2330 if (arange == NULL)
2331 return false;
2332 arange->low = low_pc;
2333 arange->high = high_pc;
2334 arange->next = first_arange->next;
2335 first_arange->next = arange;
2336 return true;
2339 /* Compare function for line sequences. */
2341 static int
2342 compare_sequences (const void* a, const void* b)
2344 const struct line_sequence* seq1 = a;
2345 const struct line_sequence* seq2 = b;
2347 /* Sort by low_pc as the primary key. */
2348 if (seq1->low_pc < seq2->low_pc)
2349 return -1;
2350 if (seq1->low_pc > seq2->low_pc)
2351 return 1;
2353 /* If low_pc values are equal, sort in reverse order of
2354 high_pc, so that the largest region comes first. */
2355 if (seq1->last_line->address < seq2->last_line->address)
2356 return 1;
2357 if (seq1->last_line->address > seq2->last_line->address)
2358 return -1;
2360 if (seq1->last_line->op_index < seq2->last_line->op_index)
2361 return 1;
2362 if (seq1->last_line->op_index > seq2->last_line->op_index)
2363 return -1;
2365 /* num_lines is initially an index, to make the sort stable. */
2366 if (seq1->num_lines < seq2->num_lines)
2367 return -1;
2368 if (seq1->num_lines > seq2->num_lines)
2369 return 1;
2370 return 0;
2373 /* Construct the line information table for quick lookup. */
2375 static bool
2376 build_line_info_table (struct line_info_table * table,
2377 struct line_sequence * seq)
2379 size_t amt;
2380 struct line_info **line_info_lookup;
2381 struct line_info *each_line;
2382 unsigned int num_lines;
2383 unsigned int line_index;
2385 if (seq->line_info_lookup != NULL)
2386 return true;
2388 /* Count the number of line information entries. We could do this while
2389 scanning the debug information, but some entries may be added via
2390 lcl_head without having a sequence handy to increment the number of
2391 lines. */
2392 num_lines = 0;
2393 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
2394 num_lines++;
2396 seq->num_lines = num_lines;
2397 if (num_lines == 0)
2398 return true;
2400 /* Allocate space for the line information lookup table. */
2401 amt = sizeof (struct line_info*) * num_lines;
2402 line_info_lookup = (struct line_info**) bfd_alloc (table->abfd, amt);
2403 seq->line_info_lookup = line_info_lookup;
2404 if (line_info_lookup == NULL)
2405 return false;
2407 /* Create the line information lookup table. */
2408 line_index = num_lines;
2409 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
2410 line_info_lookup[--line_index] = each_line;
2412 BFD_ASSERT (line_index == 0);
2413 return true;
2416 /* Sort the line sequences for quick lookup. */
2418 static bool
2419 sort_line_sequences (struct line_info_table* table)
2421 size_t amt;
2422 struct line_sequence *sequences;
2423 struct line_sequence *seq;
2424 unsigned int n = 0;
2425 unsigned int num_sequences = table->num_sequences;
2426 bfd_vma last_high_pc;
2428 if (num_sequences == 0)
2429 return true;
2431 /* Allocate space for an array of sequences. */
2432 amt = sizeof (struct line_sequence) * num_sequences;
2433 sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
2434 if (sequences == NULL)
2435 return false;
2437 /* Copy the linked list into the array, freeing the original nodes. */
2438 seq = table->sequences;
2439 for (n = 0; n < num_sequences; n++)
2441 struct line_sequence* last_seq = seq;
2443 BFD_ASSERT (seq);
2444 sequences[n].low_pc = seq->low_pc;
2445 sequences[n].prev_sequence = NULL;
2446 sequences[n].last_line = seq->last_line;
2447 sequences[n].line_info_lookup = NULL;
2448 sequences[n].num_lines = n;
2449 seq = seq->prev_sequence;
2450 free (last_seq);
2452 BFD_ASSERT (seq == NULL);
2454 qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
2456 /* Make the list binary-searchable by trimming overlapping entries
2457 and removing nested entries. */
2458 num_sequences = 1;
2459 last_high_pc = sequences[0].last_line->address;
2460 for (n = 1; n < table->num_sequences; n++)
2462 if (sequences[n].low_pc < last_high_pc)
2464 if (sequences[n].last_line->address <= last_high_pc)
2465 /* Skip nested entries. */
2466 continue;
2468 /* Trim overlapping entries. */
2469 sequences[n].low_pc = last_high_pc;
2471 last_high_pc = sequences[n].last_line->address;
2472 if (n > num_sequences)
2474 /* Close up the gap. */
2475 sequences[num_sequences].low_pc = sequences[n].low_pc;
2476 sequences[num_sequences].last_line = sequences[n].last_line;
2478 num_sequences++;
2481 table->sequences = sequences;
2482 table->num_sequences = num_sequences;
2483 return true;
2486 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
2488 static bool
2489 line_info_add_include_dir (struct line_info_table *table, char *cur_dir)
2491 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
2493 char **tmp;
2494 size_t amt;
2496 amt = table->num_dirs + DIR_ALLOC_CHUNK;
2497 amt *= sizeof (char *);
2499 tmp = (char **) bfd_realloc (table->dirs, amt);
2500 if (tmp == NULL)
2501 return false;
2502 table->dirs = tmp;
2505 table->dirs[table->num_dirs++] = cur_dir;
2506 return true;
2509 static bool
2510 line_info_add_include_dir_stub (struct line_info_table *table, char *cur_dir,
2511 unsigned int dir ATTRIBUTE_UNUSED,
2512 unsigned int xtime ATTRIBUTE_UNUSED,
2513 unsigned int size ATTRIBUTE_UNUSED)
2515 return line_info_add_include_dir (table, cur_dir);
2518 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
2520 static bool
2521 line_info_add_file_name (struct line_info_table *table, char *cur_file,
2522 unsigned int dir, unsigned int xtime,
2523 unsigned int size)
2525 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
2527 struct fileinfo *tmp;
2528 size_t amt;
2530 amt = table->num_files + FILE_ALLOC_CHUNK;
2531 amt *= sizeof (struct fileinfo);
2533 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
2534 if (tmp == NULL)
2535 return false;
2536 table->files = tmp;
2539 table->files[table->num_files].name = cur_file;
2540 table->files[table->num_files].dir = dir;
2541 table->files[table->num_files].time = xtime;
2542 table->files[table->num_files].size = size;
2543 table->num_files++;
2544 return true;
2547 /* Read directory or file name entry format, starting with byte of
2548 format count entries, ULEB128 pairs of entry formats, ULEB128 of
2549 entries count and the entries themselves in the described entry
2550 format. */
2552 static bool
2553 read_formatted_entries (struct comp_unit *unit, bfd_byte **bufp,
2554 bfd_byte *buf_end, struct line_info_table *table,
2555 bool (*callback) (struct line_info_table *table,
2556 char *cur_file,
2557 unsigned int dir,
2558 unsigned int time,
2559 unsigned int size))
2561 bfd *abfd = unit->abfd;
2562 bfd_byte format_count, formati;
2563 bfd_vma data_count, datai;
2564 bfd_byte *buf = *bufp;
2565 bfd_byte *format_header_data;
2567 format_count = read_1_byte (abfd, &buf, buf_end);
2568 format_header_data = buf;
2569 for (formati = 0; formati < format_count; formati++)
2571 _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2572 _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2575 data_count = _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2576 if (format_count == 0 && data_count != 0)
2578 _bfd_error_handler (_("DWARF error: zero format count"));
2579 bfd_set_error (bfd_error_bad_value);
2580 return false;
2583 /* PR 22210. Paranoia check. Don't bother running the loop
2584 if we know that we are going to run out of buffer. */
2585 if (data_count > (bfd_vma) (buf_end - buf))
2587 _bfd_error_handler
2588 (_("DWARF error: data count (%" PRIx64 ") larger than buffer size"),
2589 (uint64_t) data_count);
2590 bfd_set_error (bfd_error_bad_value);
2591 return false;
2594 for (datai = 0; datai < data_count; datai++)
2596 bfd_byte *format = format_header_data;
2597 struct fileinfo fe;
2599 memset (&fe, 0, sizeof fe);
2600 for (formati = 0; formati < format_count; formati++)
2602 bfd_vma content_type, form;
2603 char *string_trash;
2604 char **stringp = &string_trash;
2605 unsigned int uint_trash, *uintp = &uint_trash;
2606 struct attribute attr;
2608 content_type = _bfd_safe_read_leb128 (abfd, &format, false, buf_end);
2609 switch (content_type)
2611 case DW_LNCT_path:
2612 stringp = &fe.name;
2613 break;
2614 case DW_LNCT_directory_index:
2615 uintp = &fe.dir;
2616 break;
2617 case DW_LNCT_timestamp:
2618 uintp = &fe.time;
2619 break;
2620 case DW_LNCT_size:
2621 uintp = &fe.size;
2622 break;
2623 case DW_LNCT_MD5:
2624 break;
2625 default:
2626 _bfd_error_handler
2627 (_("DWARF error: unknown format content type %" PRIu64),
2628 (uint64_t) content_type);
2629 bfd_set_error (bfd_error_bad_value);
2630 return false;
2633 form = _bfd_safe_read_leb128 (abfd, &format, false, buf_end);
2634 buf = read_attribute_value (&attr, form, 0, unit, buf, buf_end);
2635 if (buf == NULL)
2636 return false;
2637 switch (form)
2639 case DW_FORM_string:
2640 case DW_FORM_line_strp:
2641 case DW_FORM_strx:
2642 case DW_FORM_strx1:
2643 case DW_FORM_strx2:
2644 case DW_FORM_strx3:
2645 case DW_FORM_strx4:
2646 *stringp = attr.u.str;
2647 break;
2649 case DW_FORM_data1:
2650 case DW_FORM_data2:
2651 case DW_FORM_data4:
2652 case DW_FORM_data8:
2653 case DW_FORM_udata:
2654 *uintp = attr.u.val;
2655 break;
2657 case DW_FORM_data16:
2658 /* MD5 data is in the attr.blk, but we are ignoring those. */
2659 break;
2663 if (!callback (table, fe.name, fe.dir, fe.time, fe.size))
2664 return false;
2667 *bufp = buf;
2668 return true;
2671 /* Decode the line number information for UNIT. */
2673 static struct line_info_table*
2674 decode_line_info (struct comp_unit *unit)
2676 bfd *abfd = unit->abfd;
2677 struct dwarf2_debug *stash = unit->stash;
2678 struct dwarf2_debug_file *file = unit->file;
2679 struct line_info_table* table;
2680 bfd_byte *line_ptr;
2681 bfd_byte *line_end;
2682 struct line_head lh;
2683 unsigned int i, offset_size;
2684 char *cur_file, *cur_dir;
2685 unsigned char op_code, extended_op, adj_opcode;
2686 unsigned int exop_len;
2687 size_t amt;
2689 if (unit->line_offset == 0 && file->line_table)
2690 return file->line_table;
2692 if (! read_section (abfd, &stash->debug_sections[debug_line],
2693 file->syms, unit->line_offset,
2694 &file->dwarf_line_buffer, &file->dwarf_line_size))
2695 return NULL;
2697 if (file->dwarf_line_size < 16)
2699 _bfd_error_handler
2700 (_("DWARF error: line info section is too small (%" PRId64 ")"),
2701 (int64_t) file->dwarf_line_size);
2702 bfd_set_error (bfd_error_bad_value);
2703 return NULL;
2705 line_ptr = file->dwarf_line_buffer + unit->line_offset;
2706 line_end = file->dwarf_line_buffer + file->dwarf_line_size;
2708 /* Read in the prologue. */
2709 lh.total_length = read_4_bytes (abfd, &line_ptr, line_end);
2710 offset_size = 4;
2711 if (lh.total_length == 0xffffffff)
2713 lh.total_length = read_8_bytes (abfd, &line_ptr, line_end);
2714 offset_size = 8;
2716 else if (lh.total_length == 0 && unit->addr_size == 8)
2718 /* Handle (non-standard) 64-bit DWARF2 formats. */
2719 lh.total_length = read_4_bytes (abfd, &line_ptr, line_end);
2720 offset_size = 8;
2723 if (lh.total_length > (size_t) (line_end - line_ptr))
2725 _bfd_error_handler
2726 /* xgettext: c-format */
2727 (_("DWARF error: line info data is bigger (%#" PRIx64 ")"
2728 " than the space remaining in the section (%#lx)"),
2729 (uint64_t) lh.total_length, (unsigned long) (line_end - line_ptr));
2730 bfd_set_error (bfd_error_bad_value);
2731 return NULL;
2734 line_end = line_ptr + lh.total_length;
2736 lh.version = read_2_bytes (abfd, &line_ptr, line_end);
2737 if (lh.version < 2 || lh.version > 5)
2739 _bfd_error_handler
2740 (_("DWARF error: unhandled .debug_line version %d"), lh.version);
2741 bfd_set_error (bfd_error_bad_value);
2742 return NULL;
2745 if (line_ptr + offset_size + (lh.version >= 5 ? 8 : (lh.version >= 4 ? 6 : 5))
2746 >= line_end)
2748 _bfd_error_handler
2749 (_("DWARF error: ran out of room reading prologue"));
2750 bfd_set_error (bfd_error_bad_value);
2751 return NULL;
2754 if (lh.version >= 5)
2756 unsigned int segment_selector_size;
2758 /* Skip address size. */
2759 read_1_byte (abfd, &line_ptr, line_end);
2761 segment_selector_size = read_1_byte (abfd, &line_ptr, line_end);
2762 if (segment_selector_size != 0)
2764 _bfd_error_handler
2765 (_("DWARF error: line info unsupported segment selector size %u"),
2766 segment_selector_size);
2767 bfd_set_error (bfd_error_bad_value);
2768 return NULL;
2772 if (offset_size == 4)
2773 lh.prologue_length = read_4_bytes (abfd, &line_ptr, line_end);
2774 else
2775 lh.prologue_length = read_8_bytes (abfd, &line_ptr, line_end);
2777 lh.minimum_instruction_length = read_1_byte (abfd, &line_ptr, line_end);
2779 if (lh.version >= 4)
2780 lh.maximum_ops_per_insn = read_1_byte (abfd, &line_ptr, line_end);
2781 else
2782 lh.maximum_ops_per_insn = 1;
2784 if (lh.maximum_ops_per_insn == 0)
2786 _bfd_error_handler
2787 (_("DWARF error: invalid maximum operations per instruction"));
2788 bfd_set_error (bfd_error_bad_value);
2789 return NULL;
2792 lh.default_is_stmt = read_1_byte (abfd, &line_ptr, line_end);
2793 lh.line_base = read_1_signed_byte (abfd, &line_ptr, line_end);
2794 lh.line_range = read_1_byte (abfd, &line_ptr, line_end);
2795 lh.opcode_base = read_1_byte (abfd, &line_ptr, line_end);
2797 if (line_ptr + (lh.opcode_base - 1) >= line_end)
2799 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2800 bfd_set_error (bfd_error_bad_value);
2801 return NULL;
2804 amt = lh.opcode_base * sizeof (unsigned char);
2805 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
2807 lh.standard_opcode_lengths[0] = 1;
2809 for (i = 1; i < lh.opcode_base; ++i)
2810 lh.standard_opcode_lengths[i] = read_1_byte (abfd, &line_ptr, line_end);
2812 amt = sizeof (struct line_info_table);
2813 table = (struct line_info_table *) bfd_alloc (abfd, amt);
2814 if (table == NULL)
2815 return NULL;
2816 table->abfd = abfd;
2817 table->comp_dir = unit->comp_dir;
2819 table->num_files = 0;
2820 table->files = NULL;
2822 table->num_dirs = 0;
2823 table->dirs = NULL;
2825 table->num_sequences = 0;
2826 table->sequences = NULL;
2828 table->lcl_head = NULL;
2830 if (lh.version >= 5)
2832 /* Read directory table. */
2833 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2834 line_info_add_include_dir_stub))
2835 goto fail;
2837 /* Read file name table. */
2838 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2839 line_info_add_file_name))
2840 goto fail;
2841 table->use_dir_and_file_0 = true;
2843 else
2845 /* Read directory table. */
2846 while ((cur_dir = read_string (&line_ptr, line_end)) != NULL)
2848 if (!line_info_add_include_dir (table, cur_dir))
2849 goto fail;
2852 /* Read file name table. */
2853 while ((cur_file = read_string (&line_ptr, line_end)) != NULL)
2855 unsigned int dir, xtime, size;
2857 dir = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2858 xtime = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2859 size = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2861 if (!line_info_add_file_name (table, cur_file, dir, xtime, size))
2862 goto fail;
2864 table->use_dir_and_file_0 = false;
2867 /* Read the statement sequences until there's nothing left. */
2868 while (line_ptr < line_end)
2870 /* State machine registers. */
2871 bfd_vma address = 0;
2872 unsigned char op_index = 0;
2873 char * filename = NULL;
2874 unsigned int line = 1;
2875 unsigned int column = 0;
2876 unsigned int discriminator = 0;
2877 int is_stmt = lh.default_is_stmt;
2878 int end_sequence = 0;
2879 unsigned int dir, xtime, size;
2880 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2881 compilers generate address sequences that are wildly out of
2882 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2883 for ia64-Linux). Thus, to determine the low and high
2884 address, we must compare on every DW_LNS_copy, etc. */
2885 bfd_vma low_pc = (bfd_vma) -1;
2886 bfd_vma high_pc = 0;
2888 if (table->num_files)
2890 if (table->use_dir_and_file_0)
2891 filename = concat_filename (table, 0);
2892 else
2893 filename = concat_filename (table, 1);
2896 /* Decode the table. */
2897 while (!end_sequence && line_ptr < line_end)
2899 op_code = read_1_byte (abfd, &line_ptr, line_end);
2901 if (op_code >= lh.opcode_base)
2903 /* Special operand. */
2904 adj_opcode = op_code - lh.opcode_base;
2905 if (lh.line_range == 0)
2906 goto line_fail;
2907 if (lh.maximum_ops_per_insn == 1)
2908 address += (adj_opcode / lh.line_range
2909 * lh.minimum_instruction_length);
2910 else
2912 address += ((op_index + adj_opcode / lh.line_range)
2913 / lh.maximum_ops_per_insn
2914 * lh.minimum_instruction_length);
2915 op_index = ((op_index + adj_opcode / lh.line_range)
2916 % lh.maximum_ops_per_insn);
2918 line += lh.line_base + (adj_opcode % lh.line_range);
2919 /* Append row to matrix using current values. */
2920 if (!add_line_info (table, address, op_index, filename,
2921 line, column, discriminator, 0))
2922 goto line_fail;
2923 discriminator = 0;
2924 if (address < low_pc)
2925 low_pc = address;
2926 if (address > high_pc)
2927 high_pc = address;
2929 else switch (op_code)
2931 case DW_LNS_extended_op:
2932 exop_len = _bfd_safe_read_leb128 (abfd, &line_ptr,
2933 false, line_end);
2934 extended_op = read_1_byte (abfd, &line_ptr, line_end);
2936 switch (extended_op)
2938 case DW_LNE_end_sequence:
2939 end_sequence = 1;
2940 if (!add_line_info (table, address, op_index, filename, line,
2941 column, discriminator, end_sequence))
2942 goto line_fail;
2943 discriminator = 0;
2944 if (address < low_pc)
2945 low_pc = address;
2946 if (address > high_pc)
2947 high_pc = address;
2948 if (!arange_add (unit, &unit->arange, &unit->file->trie_root,
2949 low_pc, high_pc))
2950 goto line_fail;
2951 break;
2952 case DW_LNE_set_address:
2953 address = read_address (unit, &line_ptr, line_end);
2954 op_index = 0;
2955 break;
2956 case DW_LNE_define_file:
2957 cur_file = read_string (&line_ptr, line_end);
2958 dir = _bfd_safe_read_leb128 (abfd, &line_ptr,
2959 false, line_end);
2960 xtime = _bfd_safe_read_leb128 (abfd, &line_ptr,
2961 false, line_end);
2962 size = _bfd_safe_read_leb128 (abfd, &line_ptr,
2963 false, line_end);
2964 if (!line_info_add_file_name (table, cur_file, dir,
2965 xtime, size))
2966 goto line_fail;
2967 break;
2968 case DW_LNE_set_discriminator:
2969 discriminator = _bfd_safe_read_leb128 (abfd, &line_ptr,
2970 false, line_end);
2971 break;
2972 case DW_LNE_HP_source_file_correlation:
2973 line_ptr += exop_len - 1;
2974 break;
2975 default:
2976 _bfd_error_handler
2977 (_("DWARF error: mangled line number section"));
2978 bfd_set_error (bfd_error_bad_value);
2979 line_fail:
2980 free (filename);
2981 goto fail;
2983 break;
2984 case DW_LNS_copy:
2985 if (!add_line_info (table, address, op_index,
2986 filename, line, column, discriminator, 0))
2987 goto line_fail;
2988 discriminator = 0;
2989 if (address < low_pc)
2990 low_pc = address;
2991 if (address > high_pc)
2992 high_pc = address;
2993 break;
2994 case DW_LNS_advance_pc:
2995 if (lh.maximum_ops_per_insn == 1)
2996 address += (lh.minimum_instruction_length
2997 * _bfd_safe_read_leb128 (abfd, &line_ptr,
2998 false, line_end));
2999 else
3001 bfd_vma adjust = _bfd_safe_read_leb128 (abfd, &line_ptr,
3002 false, line_end);
3003 address = ((op_index + adjust) / lh.maximum_ops_per_insn
3004 * lh.minimum_instruction_length);
3005 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
3007 break;
3008 case DW_LNS_advance_line:
3009 line += _bfd_safe_read_leb128 (abfd, &line_ptr,
3010 true, line_end);
3011 break;
3012 case DW_LNS_set_file:
3014 unsigned int filenum;
3016 /* The file and directory tables are 0
3017 based, the references are 1 based. */
3018 filenum = _bfd_safe_read_leb128 (abfd, &line_ptr,
3019 false, line_end);
3020 free (filename);
3021 filename = concat_filename (table, filenum);
3022 break;
3024 case DW_LNS_set_column:
3025 column = _bfd_safe_read_leb128 (abfd, &line_ptr,
3026 false, line_end);
3027 break;
3028 case DW_LNS_negate_stmt:
3029 is_stmt = (!is_stmt);
3030 break;
3031 case DW_LNS_set_basic_block:
3032 break;
3033 case DW_LNS_const_add_pc:
3034 if (lh.line_range == 0)
3035 goto line_fail;
3036 if (lh.maximum_ops_per_insn == 1)
3037 address += (lh.minimum_instruction_length
3038 * ((255 - lh.opcode_base) / lh.line_range));
3039 else
3041 bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
3042 address += (lh.minimum_instruction_length
3043 * ((op_index + adjust)
3044 / lh.maximum_ops_per_insn));
3045 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
3047 break;
3048 case DW_LNS_fixed_advance_pc:
3049 address += read_2_bytes (abfd, &line_ptr, line_end);
3050 op_index = 0;
3051 break;
3052 default:
3053 /* Unknown standard opcode, ignore it. */
3054 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
3055 (void) _bfd_safe_read_leb128 (abfd, &line_ptr,
3056 false, line_end);
3057 break;
3061 free (filename);
3064 if (unit->line_offset == 0)
3065 file->line_table = table;
3066 if (sort_line_sequences (table))
3067 return table;
3069 fail:
3070 while (table->sequences != NULL)
3072 struct line_sequence* seq = table->sequences;
3073 table->sequences = table->sequences->prev_sequence;
3074 free (seq);
3076 free (table->files);
3077 free (table->dirs);
3078 return NULL;
3081 /* If ADDR is within TABLE set the output parameters and return TRUE,
3082 otherwise set *FILENAME_PTR to NULL and return FALSE.
3083 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
3084 are pointers to the objects to be filled in. */
3086 static bool
3087 lookup_address_in_line_info_table (struct line_info_table *table,
3088 bfd_vma addr,
3089 const char **filename_ptr,
3090 unsigned int *linenumber_ptr,
3091 unsigned int *discriminator_ptr)
3093 struct line_sequence *seq = NULL;
3094 struct line_info *info;
3095 int low, high, mid;
3097 /* Binary search the array of sequences. */
3098 low = 0;
3099 high = table->num_sequences;
3100 while (low < high)
3102 mid = (low + high) / 2;
3103 seq = &table->sequences[mid];
3104 if (addr < seq->low_pc)
3105 high = mid;
3106 else if (addr >= seq->last_line->address)
3107 low = mid + 1;
3108 else
3109 break;
3112 /* Check for a valid sequence. */
3113 if (!seq || addr < seq->low_pc || addr >= seq->last_line->address)
3114 goto fail;
3116 if (!build_line_info_table (table, seq))
3117 goto fail;
3119 /* Binary search the array of line information. */
3120 low = 0;
3121 high = seq->num_lines;
3122 info = NULL;
3123 while (low < high)
3125 mid = (low + high) / 2;
3126 info = seq->line_info_lookup[mid];
3127 if (addr < info->address)
3128 high = mid;
3129 else if (addr >= seq->line_info_lookup[mid + 1]->address)
3130 low = mid + 1;
3131 else
3132 break;
3135 /* Check for a valid line information entry. */
3136 if (info
3137 && addr >= info->address
3138 && addr < seq->line_info_lookup[mid + 1]->address
3139 && !(info->end_sequence || info == seq->last_line))
3141 *filename_ptr = info->filename;
3142 *linenumber_ptr = info->line;
3143 if (discriminator_ptr)
3144 *discriminator_ptr = info->discriminator;
3145 return true;
3148 fail:
3149 *filename_ptr = NULL;
3150 return false;
3153 /* Read in the .debug_ranges section for future reference. */
3155 static bool
3156 read_debug_ranges (struct comp_unit * unit)
3158 struct dwarf2_debug *stash = unit->stash;
3159 struct dwarf2_debug_file *file = unit->file;
3161 return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
3162 file->syms, 0,
3163 &file->dwarf_ranges_buffer, &file->dwarf_ranges_size);
3166 /* Read in the .debug_rnglists section for future reference. */
3168 static bool
3169 read_debug_rnglists (struct comp_unit * unit)
3171 struct dwarf2_debug *stash = unit->stash;
3172 struct dwarf2_debug_file *file = unit->file;
3174 return read_section (unit->abfd, &stash->debug_sections[debug_rnglists],
3175 file->syms, 0,
3176 &file->dwarf_rnglists_buffer, &file->dwarf_rnglists_size);
3179 /* Function table functions. */
3181 static int
3182 compare_lookup_funcinfos (const void * a, const void * b)
3184 const struct lookup_funcinfo * lookup1 = a;
3185 const struct lookup_funcinfo * lookup2 = b;
3187 if (lookup1->low_addr < lookup2->low_addr)
3188 return -1;
3189 if (lookup1->low_addr > lookup2->low_addr)
3190 return 1;
3191 if (lookup1->high_addr < lookup2->high_addr)
3192 return -1;
3193 if (lookup1->high_addr > lookup2->high_addr)
3194 return 1;
3196 if (lookup1->idx < lookup2->idx)
3197 return -1;
3198 if (lookup1->idx > lookup2->idx)
3199 return 1;
3200 return 0;
3203 static bool
3204 build_lookup_funcinfo_table (struct comp_unit * unit)
3206 struct lookup_funcinfo *lookup_funcinfo_table = unit->lookup_funcinfo_table;
3207 unsigned int number_of_functions = unit->number_of_functions;
3208 struct funcinfo *each;
3209 struct lookup_funcinfo *entry;
3210 size_t func_index;
3211 struct arange *range;
3212 bfd_vma low_addr, high_addr;
3214 if (lookup_funcinfo_table || number_of_functions == 0)
3215 return true;
3217 /* Create the function info lookup table. */
3218 lookup_funcinfo_table = (struct lookup_funcinfo *)
3219 bfd_malloc (number_of_functions * sizeof (struct lookup_funcinfo));
3220 if (lookup_funcinfo_table == NULL)
3221 return false;
3223 /* Populate the function info lookup table. */
3224 func_index = number_of_functions;
3225 for (each = unit->function_table; each; each = each->prev_func)
3227 entry = &lookup_funcinfo_table[--func_index];
3228 entry->funcinfo = each;
3229 entry->idx = func_index;
3231 /* Calculate the lowest and highest address for this function entry. */
3232 low_addr = entry->funcinfo->arange.low;
3233 high_addr = entry->funcinfo->arange.high;
3235 for (range = entry->funcinfo->arange.next; range; range = range->next)
3237 if (range->low < low_addr)
3238 low_addr = range->low;
3239 if (range->high > high_addr)
3240 high_addr = range->high;
3243 entry->low_addr = low_addr;
3244 entry->high_addr = high_addr;
3247 BFD_ASSERT (func_index == 0);
3249 /* Sort the function by address. */
3250 qsort (lookup_funcinfo_table,
3251 number_of_functions,
3252 sizeof (struct lookup_funcinfo),
3253 compare_lookup_funcinfos);
3255 /* Calculate the high watermark for each function in the lookup table. */
3256 high_addr = lookup_funcinfo_table[0].high_addr;
3257 for (func_index = 1; func_index < number_of_functions; func_index++)
3259 entry = &lookup_funcinfo_table[func_index];
3260 if (entry->high_addr > high_addr)
3261 high_addr = entry->high_addr;
3262 else
3263 entry->high_addr = high_addr;
3266 unit->lookup_funcinfo_table = lookup_funcinfo_table;
3267 return true;
3270 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
3271 TRUE. Note that we need to find the function that has the smallest range
3272 that contains ADDR, to handle inlined functions without depending upon
3273 them being ordered in TABLE by increasing range. */
3275 static bool
3276 lookup_address_in_function_table (struct comp_unit *unit,
3277 bfd_vma addr,
3278 struct funcinfo **function_ptr)
3280 unsigned int number_of_functions = unit->number_of_functions;
3281 struct lookup_funcinfo* lookup_funcinfo = NULL;
3282 struct funcinfo* funcinfo = NULL;
3283 struct funcinfo* best_fit = NULL;
3284 bfd_vma best_fit_len = (bfd_vma) -1;
3285 bfd_size_type low, high, mid, first;
3286 struct arange *arange;
3288 if (number_of_functions == 0)
3289 return false;
3291 if (!build_lookup_funcinfo_table (unit))
3292 return false;
3294 if (unit->lookup_funcinfo_table[number_of_functions - 1].high_addr < addr)
3295 return false;
3297 /* Find the first function in the lookup table which may contain the
3298 specified address. */
3299 low = 0;
3300 high = number_of_functions;
3301 first = high;
3302 while (low < high)
3304 mid = (low + high) / 2;
3305 lookup_funcinfo = &unit->lookup_funcinfo_table[mid];
3306 if (addr < lookup_funcinfo->low_addr)
3307 high = mid;
3308 else if (addr >= lookup_funcinfo->high_addr)
3309 low = mid + 1;
3310 else
3311 high = first = mid;
3314 /* Find the 'best' match for the address. The prior algorithm defined the
3315 best match as the function with the smallest address range containing
3316 the specified address. This definition should probably be changed to the
3317 innermost inline routine containing the address, but right now we want
3318 to get the same results we did before. */
3319 while (first < number_of_functions)
3321 if (addr < unit->lookup_funcinfo_table[first].low_addr)
3322 break;
3323 funcinfo = unit->lookup_funcinfo_table[first].funcinfo;
3325 for (arange = &funcinfo->arange; arange; arange = arange->next)
3327 if (addr < arange->low || addr >= arange->high)
3328 continue;
3330 if (arange->high - arange->low < best_fit_len
3331 /* The following comparison is designed to return the same
3332 match as the previous algorithm for routines which have the
3333 same best fit length. */
3334 || (arange->high - arange->low == best_fit_len
3335 && funcinfo > best_fit))
3337 best_fit = funcinfo;
3338 best_fit_len = arange->high - arange->low;
3342 first++;
3345 if (!best_fit)
3346 return false;
3348 *function_ptr = best_fit;
3349 return true;
3352 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
3353 and LINENUMBER_PTR, and return TRUE. */
3355 static bool
3356 lookup_symbol_in_function_table (struct comp_unit *unit,
3357 asymbol *sym,
3358 bfd_vma addr,
3359 const char **filename_ptr,
3360 unsigned int *linenumber_ptr)
3362 struct funcinfo* each;
3363 struct funcinfo* best_fit = NULL;
3364 bfd_vma best_fit_len = (bfd_vma) -1;
3365 struct arange *arange;
3366 const char *name = bfd_asymbol_name (sym);
3368 for (each = unit->function_table; each; each = each->prev_func)
3369 for (arange = &each->arange; arange; arange = arange->next)
3370 if (addr >= arange->low
3371 && addr < arange->high
3372 && arange->high - arange->low < best_fit_len
3373 && each->file
3374 && each->name
3375 && strstr (name, each->name) != NULL)
3377 best_fit = each;
3378 best_fit_len = arange->high - arange->low;
3381 if (best_fit)
3383 *filename_ptr = best_fit->file;
3384 *linenumber_ptr = best_fit->line;
3385 return true;
3388 return false;
3391 /* Variable table functions. */
3393 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
3394 LINENUMBER_PTR, and return TRUE. */
3396 static bool
3397 lookup_symbol_in_variable_table (struct comp_unit *unit,
3398 asymbol *sym,
3399 bfd_vma addr,
3400 const char **filename_ptr,
3401 unsigned int *linenumber_ptr)
3403 struct varinfo* each;
3404 const char *name = bfd_asymbol_name (sym);
3406 for (each = unit->variable_table; each; each = each->prev_var)
3407 if (each->addr == addr
3408 && !each->stack
3409 && each->file != NULL
3410 && each->name != NULL
3411 && strstr (name, each->name) != NULL)
3412 break;
3414 if (each)
3416 *filename_ptr = each->file;
3417 *linenumber_ptr = each->line;
3418 return true;
3421 return false;
3424 static struct comp_unit *stash_comp_unit (struct dwarf2_debug *,
3425 struct dwarf2_debug_file *);
3426 static bool comp_unit_maybe_decode_line_info (struct comp_unit *);
3428 static bool
3429 find_abstract_instance (struct comp_unit *unit,
3430 struct attribute *attr_ptr,
3431 unsigned int recur_count,
3432 const char **pname,
3433 bool *is_linkage,
3434 char **filename_ptr,
3435 int *linenumber_ptr)
3437 bfd *abfd = unit->abfd;
3438 bfd_byte *info_ptr = NULL;
3439 bfd_byte *info_ptr_end;
3440 unsigned int abbrev_number, i;
3441 struct abbrev_info *abbrev;
3442 uint64_t die_ref = attr_ptr->u.val;
3443 struct attribute attr;
3444 const char *name = NULL;
3446 if (recur_count == 100)
3448 _bfd_error_handler
3449 (_("DWARF error: abstract instance recursion detected"));
3450 bfd_set_error (bfd_error_bad_value);
3451 return false;
3454 /* DW_FORM_ref_addr can reference an entry in a different CU. It
3455 is an offset from the .debug_info section, not the current CU. */
3456 if (attr_ptr->form == DW_FORM_ref_addr)
3458 /* We only support DW_FORM_ref_addr within the same file, so
3459 any relocations should be resolved already. Check this by
3460 testing for a zero die_ref; There can't be a valid reference
3461 to the header of a .debug_info section.
3462 DW_FORM_ref_addr is an offset relative to .debug_info.
3463 Normally when using the GNU linker this is accomplished by
3464 emitting a symbolic reference to a label, because .debug_info
3465 sections are linked at zero. When there are multiple section
3466 groups containing .debug_info, as there might be in a
3467 relocatable object file, it would be reasonable to assume that
3468 a symbolic reference to a label in any .debug_info section
3469 might be used. Since we lay out multiple .debug_info
3470 sections at non-zero VMAs (see place_sections), and read
3471 them contiguously into dwarf_info_buffer, that means the
3472 reference is relative to dwarf_info_buffer. */
3473 size_t total;
3475 info_ptr = unit->file->dwarf_info_buffer;
3476 info_ptr_end = info_ptr + unit->file->dwarf_info_size;
3477 total = info_ptr_end - info_ptr;
3478 if (!die_ref)
3479 return true;
3480 else if (die_ref >= total)
3482 _bfd_error_handler
3483 (_("DWARF error: invalid abstract instance DIE ref"));
3484 bfd_set_error (bfd_error_bad_value);
3485 return false;
3487 info_ptr += die_ref;
3489 else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
3491 bool first_time = unit->stash->alt.dwarf_info_buffer == NULL;
3493 info_ptr = read_alt_indirect_ref (unit, die_ref);
3494 if (first_time)
3495 unit->stash->alt.info_ptr = unit->stash->alt.dwarf_info_buffer;
3496 if (info_ptr == NULL)
3498 _bfd_error_handler
3499 (_("DWARF error: unable to read alt ref %" PRIu64),
3500 (uint64_t) die_ref);
3501 bfd_set_error (bfd_error_bad_value);
3502 return false;
3504 info_ptr_end = (unit->stash->alt.dwarf_info_buffer
3505 + unit->stash->alt.dwarf_info_size);
3506 if (unit->stash->alt.all_comp_units)
3507 unit = unit->stash->alt.all_comp_units;
3510 if (attr_ptr->form == DW_FORM_ref_addr
3511 || attr_ptr->form == DW_FORM_GNU_ref_alt)
3513 /* Now find the CU containing this pointer. */
3514 if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr)
3515 info_ptr_end = unit->end_ptr;
3516 else
3518 /* Check other CUs to see if they contain the abbrev. */
3519 struct comp_unit *u = NULL;
3520 struct addr_range range = { info_ptr, info_ptr };
3521 splay_tree_node v = splay_tree_lookup (unit->file->comp_unit_tree,
3522 (splay_tree_key)&range);
3523 if (v != NULL)
3524 u = (struct comp_unit *)v->value;
3526 if (attr_ptr->form == DW_FORM_ref_addr)
3527 while (u == NULL)
3529 u = stash_comp_unit (unit->stash, &unit->stash->f);
3530 if (u == NULL)
3531 break;
3532 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3533 break;
3534 u = NULL;
3537 if (attr_ptr->form == DW_FORM_GNU_ref_alt)
3538 while (u == NULL)
3540 u = stash_comp_unit (unit->stash, &unit->stash->alt);
3541 if (u == NULL)
3542 break;
3543 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3544 break;
3545 u = NULL;
3548 if (u == NULL)
3550 _bfd_error_handler
3551 (_("DWARF error: unable to locate abstract instance DIE ref %"
3552 PRIu64), (uint64_t) die_ref);
3553 bfd_set_error (bfd_error_bad_value);
3554 return false;
3556 unit = u;
3557 info_ptr_end = unit->end_ptr;
3560 else
3562 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
3563 DW_FORM_ref_udata. These are all references relative to the
3564 start of the current CU. */
3565 size_t total;
3567 info_ptr = unit->info_ptr_unit;
3568 info_ptr_end = unit->end_ptr;
3569 total = info_ptr_end - info_ptr;
3570 if (!die_ref || die_ref >= total)
3572 _bfd_error_handler
3573 (_("DWARF error: invalid abstract instance DIE ref"));
3574 bfd_set_error (bfd_error_bad_value);
3575 return false;
3577 info_ptr += die_ref;
3580 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
3581 false, info_ptr_end);
3582 if (abbrev_number)
3584 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3585 if (! abbrev)
3587 _bfd_error_handler
3588 (_("DWARF error: could not find abbrev number %u"), abbrev_number);
3589 bfd_set_error (bfd_error_bad_value);
3590 return false;
3592 else
3594 for (i = 0; i < abbrev->num_attrs; ++i)
3596 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
3597 info_ptr, info_ptr_end);
3598 if (info_ptr == NULL)
3599 break;
3600 switch (attr.name)
3602 case DW_AT_name:
3603 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3604 over DW_AT_name. */
3605 if (name == NULL && is_str_form (&attr))
3607 name = attr.u.str;
3608 if (mangle_style (unit->lang) == 0)
3609 *is_linkage = true;
3611 break;
3612 case DW_AT_specification:
3613 if (is_int_form (&attr)
3614 && !find_abstract_instance (unit, &attr, recur_count + 1,
3615 &name, is_linkage,
3616 filename_ptr, linenumber_ptr))
3617 return false;
3618 break;
3619 case DW_AT_linkage_name:
3620 case DW_AT_MIPS_linkage_name:
3621 /* PR 16949: Corrupt debug info can place
3622 non-string forms into these attributes. */
3623 if (is_str_form (&attr))
3625 name = attr.u.str;
3626 *is_linkage = true;
3628 break;
3629 case DW_AT_decl_file:
3630 if (!comp_unit_maybe_decode_line_info (unit))
3631 return false;
3632 if (is_int_form (&attr))
3633 *filename_ptr = concat_filename (unit->line_table,
3634 attr.u.val);
3635 break;
3636 case DW_AT_decl_line:
3637 if (is_int_form (&attr))
3638 *linenumber_ptr = attr.u.val;
3639 break;
3640 default:
3641 break;
3646 *pname = name;
3647 return true;
3650 static bool
3651 read_ranges (struct comp_unit *unit, struct arange *arange,
3652 struct trie_node **trie_root, uint64_t offset)
3654 bfd_byte *ranges_ptr;
3655 bfd_byte *ranges_end;
3656 bfd_vma base_address = unit->base_address;
3658 if (! unit->file->dwarf_ranges_buffer)
3660 if (! read_debug_ranges (unit))
3661 return false;
3664 if (offset > unit->file->dwarf_ranges_size)
3665 return false;
3666 ranges_ptr = unit->file->dwarf_ranges_buffer + offset;
3667 ranges_end = unit->file->dwarf_ranges_buffer + unit->file->dwarf_ranges_size;
3669 for (;;)
3671 bfd_vma low_pc;
3672 bfd_vma high_pc;
3674 /* PR 17512: file: 62cada7d. */
3675 if (2u * unit->addr_size > (size_t) (ranges_end - ranges_ptr))
3676 return false;
3678 low_pc = read_address (unit, &ranges_ptr, ranges_end);
3679 high_pc = read_address (unit, &ranges_ptr, ranges_end);
3681 if (low_pc == 0 && high_pc == 0)
3682 break;
3683 if (low_pc == -1UL && high_pc != -1UL)
3684 base_address = high_pc;
3685 else
3687 if (!arange_add (unit, arange, trie_root,
3688 base_address + low_pc, base_address + high_pc))
3689 return false;
3692 return true;
3695 static bool
3696 read_rnglists (struct comp_unit *unit, struct arange *arange,
3697 struct trie_node **trie_root, uint64_t offset)
3699 bfd_byte *rngs_ptr;
3700 bfd_byte *rngs_end;
3701 bfd_vma base_address = unit->base_address;
3702 bfd_vma low_pc;
3703 bfd_vma high_pc;
3704 bfd *abfd = unit->abfd;
3706 if (! unit->file->dwarf_rnglists_buffer)
3708 if (! read_debug_rnglists (unit))
3709 return false;
3712 rngs_ptr = unit->file->dwarf_rnglists_buffer + offset;
3713 if (rngs_ptr < unit->file->dwarf_rnglists_buffer)
3714 return false;
3715 rngs_end = unit->file->dwarf_rnglists_buffer;
3716 rngs_end += unit->file->dwarf_rnglists_size;
3718 for (;;)
3720 enum dwarf_range_list_entry rlet;
3722 if (rngs_ptr >= rngs_end)
3723 return false;
3725 rlet = read_1_byte (abfd, &rngs_ptr, rngs_end);
3727 switch (rlet)
3729 case DW_RLE_end_of_list:
3730 return true;
3732 case DW_RLE_base_address:
3733 if (unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3734 return false;
3735 base_address = read_address (unit, &rngs_ptr, rngs_end);
3736 continue;
3738 case DW_RLE_start_length:
3739 if (unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3740 return false;
3741 low_pc = read_address (unit, &rngs_ptr, rngs_end);
3742 high_pc = low_pc;
3743 high_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3744 false, rngs_end);
3745 break;
3747 case DW_RLE_offset_pair:
3748 low_pc = base_address;
3749 low_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3750 false, rngs_end);
3751 high_pc = base_address;
3752 high_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3753 false, rngs_end);
3754 break;
3756 case DW_RLE_start_end:
3757 if (2u * unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3758 return false;
3759 low_pc = read_address (unit, &rngs_ptr, rngs_end);
3760 high_pc = read_address (unit, &rngs_ptr, rngs_end);
3761 break;
3763 /* TODO x-variants need .debug_addr support used for split-dwarf. */
3764 case DW_RLE_base_addressx:
3765 case DW_RLE_startx_endx:
3766 case DW_RLE_startx_length:
3767 default:
3768 return false;
3771 if (!arange_add (unit, arange, trie_root, low_pc, high_pc))
3772 return false;
3776 static bool
3777 read_rangelist (struct comp_unit *unit, struct arange *arange,
3778 struct trie_node **trie_root, uint64_t offset)
3780 if (unit->version <= 4)
3781 return read_ranges (unit, arange, trie_root, offset);
3782 else
3783 return read_rnglists (unit, arange, trie_root, offset);
3786 static struct funcinfo *
3787 lookup_func_by_offset (uint64_t offset, struct funcinfo * table)
3789 for (; table != NULL; table = table->prev_func)
3790 if (table->unit_offset == offset)
3791 return table;
3792 return NULL;
3795 static struct varinfo *
3796 lookup_var_by_offset (uint64_t offset, struct varinfo * table)
3798 while (table)
3800 if (table->unit_offset == offset)
3801 return table;
3802 table = table->prev_var;
3805 return NULL;
3809 /* DWARF2 Compilation unit functions. */
3811 static struct funcinfo *
3812 reverse_funcinfo_list (struct funcinfo *head)
3814 struct funcinfo *rhead;
3815 struct funcinfo *temp;
3817 for (rhead = NULL; head; head = temp)
3819 temp = head->prev_func;
3820 head->prev_func = rhead;
3821 rhead = head;
3823 return rhead;
3826 static struct varinfo *
3827 reverse_varinfo_list (struct varinfo *head)
3829 struct varinfo *rhead;
3830 struct varinfo *temp;
3832 for (rhead = NULL; head; head = temp)
3834 temp = head->prev_var;
3835 head->prev_var = rhead;
3836 rhead = head;
3838 return rhead;
3841 /* Scan over each die in a comp. unit looking for functions to add
3842 to the function table and variables to the variable table. */
3844 static bool
3845 scan_unit_for_symbols (struct comp_unit *unit)
3847 bfd *abfd = unit->abfd;
3848 bfd_byte *info_ptr = unit->first_child_die_ptr;
3849 bfd_byte *info_ptr_end = unit->end_ptr;
3850 int nesting_level = 0;
3851 struct nest_funcinfo
3853 struct funcinfo *func;
3854 } *nested_funcs;
3855 int nested_funcs_size;
3856 struct funcinfo *last_func;
3857 struct varinfo *last_var;
3859 /* Maintain a stack of in-scope functions and inlined functions, which we
3860 can use to set the caller_func field. */
3861 nested_funcs_size = 32;
3862 nested_funcs = (struct nest_funcinfo *)
3863 bfd_malloc (nested_funcs_size * sizeof (*nested_funcs));
3864 if (nested_funcs == NULL)
3865 return false;
3866 nested_funcs[nesting_level].func = 0;
3868 /* PR 27484: We must scan the DIEs twice. The first time we look for
3869 function and variable tags and accumulate them into their respective
3870 tables. The second time through we process the attributes of the
3871 functions/variables and augment the table entries. */
3872 while (nesting_level >= 0)
3874 unsigned int abbrev_number, i;
3875 struct abbrev_info *abbrev;
3876 struct funcinfo *func;
3877 struct varinfo *var;
3878 uint64_t current_offset;
3880 /* PR 17512: file: 9f405d9d. */
3881 if (info_ptr >= info_ptr_end)
3882 goto fail;
3884 current_offset = info_ptr - unit->info_ptr_unit;
3885 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
3886 false, info_ptr_end);
3887 if (abbrev_number == 0)
3889 nesting_level--;
3890 continue;
3893 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3894 if (! abbrev)
3896 static unsigned int previous_failed_abbrev = -1U;
3898 /* Avoid multiple reports of the same missing abbrev. */
3899 if (abbrev_number != previous_failed_abbrev)
3901 _bfd_error_handler
3902 (_("DWARF error: could not find abbrev number %u"),
3903 abbrev_number);
3904 previous_failed_abbrev = abbrev_number;
3906 bfd_set_error (bfd_error_bad_value);
3907 goto fail;
3910 if (abbrev->tag == DW_TAG_subprogram
3911 || abbrev->tag == DW_TAG_entry_point
3912 || abbrev->tag == DW_TAG_inlined_subroutine)
3914 size_t amt = sizeof (struct funcinfo);
3916 var = NULL;
3917 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
3918 if (func == NULL)
3919 goto fail;
3920 func->tag = abbrev->tag;
3921 func->prev_func = unit->function_table;
3922 func->unit_offset = current_offset;
3923 unit->function_table = func;
3924 unit->number_of_functions++;
3925 BFD_ASSERT (!unit->cached);
3927 if (func->tag == DW_TAG_inlined_subroutine)
3928 for (i = nesting_level; i-- != 0; )
3929 if (nested_funcs[i].func)
3931 func->caller_func = nested_funcs[i].func;
3932 break;
3934 nested_funcs[nesting_level].func = func;
3936 else
3938 func = NULL;
3939 if (abbrev->tag == DW_TAG_variable
3940 || abbrev->tag == DW_TAG_member)
3942 size_t amt = sizeof (struct varinfo);
3944 var = (struct varinfo *) bfd_zalloc (abfd, amt);
3945 if (var == NULL)
3946 goto fail;
3947 var->tag = abbrev->tag;
3948 var->stack = true;
3949 var->prev_var = unit->variable_table;
3950 unit->variable_table = var;
3951 var->unit_offset = current_offset;
3952 /* PR 18205: Missing debug information can cause this
3953 var to be attached to an already cached unit. */
3955 else
3956 var = NULL;
3958 /* No inline function in scope at this nesting level. */
3959 nested_funcs[nesting_level].func = 0;
3962 for (i = 0; i < abbrev->num_attrs; ++i)
3964 struct attribute attr;
3966 info_ptr = read_attribute (&attr, &abbrev->attrs[i],
3967 unit, info_ptr, info_ptr_end);
3968 if (info_ptr == NULL)
3969 goto fail;
3972 if (abbrev->has_children)
3974 nesting_level++;
3976 if (nesting_level >= nested_funcs_size)
3978 struct nest_funcinfo *tmp;
3980 nested_funcs_size *= 2;
3981 tmp = (struct nest_funcinfo *)
3982 bfd_realloc (nested_funcs,
3983 nested_funcs_size * sizeof (*nested_funcs));
3984 if (tmp == NULL)
3985 goto fail;
3986 nested_funcs = tmp;
3988 nested_funcs[nesting_level].func = 0;
3992 unit->function_table = reverse_funcinfo_list (unit->function_table);
3993 unit->variable_table = reverse_varinfo_list (unit->variable_table);
3995 /* This is the second pass over the abbrevs. */
3996 info_ptr = unit->first_child_die_ptr;
3997 nesting_level = 0;
3999 last_func = NULL;
4000 last_var = NULL;
4002 while (nesting_level >= 0)
4004 unsigned int abbrev_number, i;
4005 struct abbrev_info *abbrev;
4006 struct attribute attr;
4007 struct funcinfo *func;
4008 struct varinfo *var;
4009 bfd_vma low_pc = 0;
4010 bfd_vma high_pc = 0;
4011 bool high_pc_relative = false;
4012 uint64_t current_offset;
4014 /* PR 17512: file: 9f405d9d. */
4015 if (info_ptr >= info_ptr_end)
4016 goto fail;
4018 current_offset = info_ptr - unit->info_ptr_unit;
4019 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
4020 false, info_ptr_end);
4021 if (! abbrev_number)
4023 nesting_level--;
4024 continue;
4027 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
4028 /* This should have been handled above. */
4029 BFD_ASSERT (abbrev != NULL);
4031 func = NULL;
4032 var = NULL;
4033 if (abbrev->tag == DW_TAG_subprogram
4034 || abbrev->tag == DW_TAG_entry_point
4035 || abbrev->tag == DW_TAG_inlined_subroutine)
4037 if (last_func
4038 && last_func->prev_func
4039 && last_func->prev_func->unit_offset == current_offset)
4040 func = last_func->prev_func;
4041 else
4042 func = lookup_func_by_offset (current_offset, unit->function_table);
4044 if (func == NULL)
4045 goto fail;
4047 last_func = func;
4049 else if (abbrev->tag == DW_TAG_variable
4050 || abbrev->tag == DW_TAG_member)
4052 if (last_var
4053 && last_var->prev_var
4054 && last_var->prev_var->unit_offset == current_offset)
4055 var = last_var->prev_var;
4056 else
4057 var = lookup_var_by_offset (current_offset, unit->variable_table);
4059 if (var == NULL)
4060 goto fail;
4062 last_var = var;
4065 for (i = 0; i < abbrev->num_attrs; ++i)
4067 info_ptr = read_attribute (&attr, &abbrev->attrs[i],
4068 unit, info_ptr, info_ptr_end);
4069 if (info_ptr == NULL)
4070 goto fail;
4072 if (func)
4074 switch (attr.name)
4076 case DW_AT_call_file:
4077 if (is_int_form (&attr))
4078 func->caller_file = concat_filename (unit->line_table,
4079 attr.u.val);
4080 break;
4082 case DW_AT_call_line:
4083 if (is_int_form (&attr))
4084 func->caller_line = attr.u.val;
4085 break;
4087 case DW_AT_abstract_origin:
4088 case DW_AT_specification:
4089 if (is_int_form (&attr)
4090 && !find_abstract_instance (unit, &attr, 0,
4091 &func->name,
4092 &func->is_linkage,
4093 &func->file,
4094 &func->line))
4095 goto fail;
4096 break;
4098 case DW_AT_name:
4099 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
4100 over DW_AT_name. */
4101 if (func->name == NULL && is_str_form (&attr))
4103 func->name = attr.u.str;
4104 if (mangle_style (unit->lang) == 0)
4105 func->is_linkage = true;
4107 break;
4109 case DW_AT_linkage_name:
4110 case DW_AT_MIPS_linkage_name:
4111 /* PR 16949: Corrupt debug info can place
4112 non-string forms into these attributes. */
4113 if (is_str_form (&attr))
4115 func->name = attr.u.str;
4116 func->is_linkage = true;
4118 break;
4120 case DW_AT_low_pc:
4121 if (is_int_form (&attr))
4122 low_pc = attr.u.val;
4123 break;
4125 case DW_AT_high_pc:
4126 if (is_int_form (&attr))
4128 high_pc = attr.u.val;
4129 high_pc_relative = attr.form != DW_FORM_addr;
4131 break;
4133 case DW_AT_ranges:
4134 if (is_int_form (&attr)
4135 && !read_rangelist (unit, &func->arange,
4136 &unit->file->trie_root, attr.u.val))
4137 goto fail;
4138 break;
4140 case DW_AT_decl_file:
4141 if (is_int_form (&attr))
4142 func->file = concat_filename (unit->line_table,
4143 attr.u.val);
4144 break;
4146 case DW_AT_decl_line:
4147 if (is_int_form (&attr))
4148 func->line = attr.u.val;
4149 break;
4151 default:
4152 break;
4155 else if (var)
4157 switch (attr.name)
4159 case DW_AT_specification:
4160 if (is_int_form (&attr) && attr.u.val)
4162 bool is_linkage;
4163 if (!find_abstract_instance (unit, &attr, 0,
4164 &var->name,
4165 &is_linkage,
4166 &var->file,
4167 &var->line))
4169 _bfd_error_handler (_("DWARF error: could not find "
4170 "variable specification "
4171 "at offset 0x%lx"),
4172 (unsigned long) attr.u.val);
4173 break;
4176 break;
4178 case DW_AT_name:
4179 if (is_str_form (&attr))
4180 var->name = attr.u.str;
4181 break;
4183 case DW_AT_decl_file:
4184 if (is_int_form (&attr))
4185 var->file = concat_filename (unit->line_table,
4186 attr.u.val);
4187 break;
4189 case DW_AT_decl_line:
4190 if (is_int_form (&attr))
4191 var->line = attr.u.val;
4192 break;
4194 case DW_AT_external:
4195 if (is_int_form (&attr) && attr.u.val != 0)
4196 var->stack = false;
4197 break;
4199 case DW_AT_location:
4200 switch (attr.form)
4202 case DW_FORM_block:
4203 case DW_FORM_block1:
4204 case DW_FORM_block2:
4205 case DW_FORM_block4:
4206 case DW_FORM_exprloc:
4207 if (attr.u.blk->data != NULL
4208 && *attr.u.blk->data == DW_OP_addr)
4210 var->stack = false;
4212 /* Verify that DW_OP_addr is the only opcode in the
4213 location, in which case the block size will be 1
4214 plus the address size. */
4215 /* ??? For TLS variables, gcc can emit
4216 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
4217 which we don't handle here yet. */
4218 if (attr.u.blk->size == unit->addr_size + 1U)
4219 var->addr = bfd_get (unit->addr_size * 8,
4220 unit->abfd,
4221 attr.u.blk->data + 1);
4223 break;
4225 default:
4226 break;
4228 break;
4230 default:
4231 break;
4236 if (abbrev->has_children)
4237 nesting_level++;
4239 if (high_pc_relative)
4240 high_pc += low_pc;
4242 if (func && high_pc != 0)
4244 if (!arange_add (unit, &func->arange, &unit->file->trie_root,
4245 low_pc, high_pc))
4246 goto fail;
4250 unit->function_table = reverse_funcinfo_list (unit->function_table);
4251 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4253 free (nested_funcs);
4254 return true;
4256 fail:
4257 free (nested_funcs);
4258 return false;
4261 /* Read the attributes of the form strx and addrx. */
4263 static void
4264 reread_attribute (struct comp_unit *unit,
4265 struct attribute *attr,
4266 bfd_vma *low_pc,
4267 bfd_vma *high_pc,
4268 bool *high_pc_relative,
4269 bool compunit)
4271 if (is_strx_form (attr->form))
4272 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
4273 if (is_addrx_form (attr->form))
4274 attr->u.val = read_indexed_address (attr->u.val, unit);
4276 switch (attr->name)
4278 case DW_AT_stmt_list:
4279 unit->stmtlist = 1;
4280 unit->line_offset = attr->u.val;
4281 break;
4283 case DW_AT_name:
4284 if (is_str_form (attr))
4285 unit->name = attr->u.str;
4286 break;
4288 case DW_AT_low_pc:
4289 *low_pc = attr->u.val;
4290 if (compunit)
4291 unit->base_address = *low_pc;
4292 break;
4294 case DW_AT_high_pc:
4295 *high_pc = attr->u.val;
4296 *high_pc_relative = attr->form != DW_FORM_addr;
4297 break;
4299 case DW_AT_ranges:
4300 if (!read_rangelist (unit, &unit->arange,
4301 &unit->file->trie_root, attr->u.val))
4302 return;
4303 break;
4305 case DW_AT_comp_dir:
4307 char *comp_dir = attr->u.str;
4309 if (!is_str_form (attr))
4311 _bfd_error_handler
4312 (_("DWARF error: DW_AT_comp_dir attribute encountered "
4313 "with a non-string form"));
4314 comp_dir = NULL;
4317 if (comp_dir)
4319 char *cp = strchr (comp_dir, ':');
4321 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
4322 comp_dir = cp + 1;
4324 unit->comp_dir = comp_dir;
4325 break;
4328 case DW_AT_language:
4329 unit->lang = attr->u.val;
4330 default:
4331 break;
4335 /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
4336 includes the compilation unit header that proceeds the DIE's, but
4337 does not include the length field that precedes each compilation
4338 unit header. END_PTR points one past the end of this comp unit.
4339 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
4341 This routine does not read the whole compilation unit; only enough
4342 to get to the line number information for the compilation unit. */
4344 static struct comp_unit *
4345 parse_comp_unit (struct dwarf2_debug *stash,
4346 struct dwarf2_debug_file *file,
4347 bfd_byte *info_ptr,
4348 bfd_vma unit_length,
4349 bfd_byte *info_ptr_unit,
4350 unsigned int offset_size)
4352 struct comp_unit* unit;
4353 unsigned int version;
4354 uint64_t abbrev_offset = 0;
4355 /* Initialize it just to avoid a GCC false warning. */
4356 unsigned int addr_size = -1;
4357 struct abbrev_info** abbrevs;
4358 unsigned int abbrev_number, i;
4359 struct abbrev_info *abbrev;
4360 struct attribute attr;
4361 bfd_byte *end_ptr = info_ptr + unit_length;
4362 size_t amt;
4363 bfd_vma low_pc = 0;
4364 bfd_vma high_pc = 0;
4365 bfd *abfd = file->bfd_ptr;
4366 bool high_pc_relative = false;
4367 enum dwarf_unit_type unit_type;
4368 struct attribute *str_addrp = NULL;
4369 size_t str_count = 0;
4370 size_t str_alloc = 0;
4371 bool compunit_flag = false;
4373 version = read_2_bytes (abfd, &info_ptr, end_ptr);
4374 if (version < 2 || version > 5)
4376 /* PR 19872: A version number of 0 probably means that there is padding
4377 at the end of the .debug_info section. Gold puts it there when
4378 performing an incremental link, for example. So do not generate
4379 an error, just return a NULL. */
4380 if (version)
4382 _bfd_error_handler
4383 (_("DWARF error: found dwarf version '%u', this reader"
4384 " only handles version 2, 3, 4 and 5 information"), version);
4385 bfd_set_error (bfd_error_bad_value);
4387 return NULL;
4390 if (version < 5)
4391 unit_type = DW_UT_compile;
4392 else
4394 unit_type = read_1_byte (abfd, &info_ptr, end_ptr);
4395 addr_size = read_1_byte (abfd, &info_ptr, end_ptr);
4398 BFD_ASSERT (offset_size == 4 || offset_size == 8);
4399 if (offset_size == 4)
4400 abbrev_offset = read_4_bytes (abfd, &info_ptr, end_ptr);
4401 else
4402 abbrev_offset = read_8_bytes (abfd, &info_ptr, end_ptr);
4404 if (version < 5)
4405 addr_size = read_1_byte (abfd, &info_ptr, end_ptr);
4407 switch (unit_type)
4409 case DW_UT_type:
4410 /* Skip type signature. */
4411 info_ptr += 8;
4413 /* Skip type offset. */
4414 info_ptr += offset_size;
4415 break;
4417 case DW_UT_skeleton:
4418 /* Skip DWO_id field. */
4419 info_ptr += 8;
4420 break;
4422 default:
4423 break;
4426 if (addr_size > sizeof (bfd_vma))
4428 _bfd_error_handler
4429 /* xgettext: c-format */
4430 (_("DWARF error: found address size '%u', this reader"
4431 " can not handle sizes greater than '%u'"),
4432 addr_size,
4433 (unsigned int) sizeof (bfd_vma));
4434 bfd_set_error (bfd_error_bad_value);
4435 return NULL;
4438 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
4440 _bfd_error_handler
4441 ("DWARF error: found address size '%u', this reader"
4442 " can only handle address sizes '2', '4' and '8'", addr_size);
4443 bfd_set_error (bfd_error_bad_value);
4444 return NULL;
4447 /* Read the abbrevs for this compilation unit into a table. */
4448 abbrevs = read_abbrevs (abfd, abbrev_offset, stash, file);
4449 if (! abbrevs)
4450 return NULL;
4452 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
4453 false, end_ptr);
4454 if (! abbrev_number)
4456 /* PR 19872: An abbrev number of 0 probably means that there is padding
4457 at the end of the .debug_abbrev section. Gold puts it there when
4458 performing an incremental link, for example. So do not generate
4459 an error, just return a NULL. */
4460 return NULL;
4463 abbrev = lookup_abbrev (abbrev_number, abbrevs);
4464 if (! abbrev)
4466 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
4467 abbrev_number);
4468 bfd_set_error (bfd_error_bad_value);
4469 return NULL;
4472 amt = sizeof (struct comp_unit);
4473 unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
4474 if (unit == NULL)
4475 return NULL;
4476 unit->abfd = abfd;
4477 unit->version = version;
4478 unit->addr_size = addr_size;
4479 unit->offset_size = offset_size;
4480 unit->abbrevs = abbrevs;
4481 unit->end_ptr = end_ptr;
4482 unit->stash = stash;
4483 unit->file = file;
4484 unit->info_ptr_unit = info_ptr_unit;
4486 if (abbrev->tag == DW_TAG_compile_unit)
4487 compunit_flag = true;
4489 for (i = 0; i < abbrev->num_attrs; ++i)
4491 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, end_ptr);
4492 if (info_ptr == NULL)
4493 goto err_exit;
4495 /* Identify attributes of the form strx* and addrx* which come before
4496 DW_AT_str_offsets_base and DW_AT_addr_base respectively in the CU.
4497 Store the attributes in an array and process them later. */
4498 if ((unit->dwarf_str_offset == 0 && is_strx_form (attr.form))
4499 || (unit->dwarf_addr_offset == 0 && is_addrx_form (attr.form)))
4501 if (str_count <= str_alloc)
4503 str_alloc = 2 * str_alloc + 200;
4504 str_addrp = bfd_realloc (str_addrp,
4505 str_alloc * sizeof (*str_addrp));
4506 if (str_addrp == NULL)
4507 goto err_exit;
4509 str_addrp[str_count] = attr;
4510 str_count++;
4511 continue;
4514 /* Store the data if it is of an attribute we want to keep in a
4515 partial symbol table. */
4516 switch (attr.name)
4518 case DW_AT_stmt_list:
4519 if (is_int_form (&attr))
4521 unit->stmtlist = 1;
4522 unit->line_offset = attr.u.val;
4524 break;
4526 case DW_AT_name:
4527 if (is_str_form (&attr))
4528 unit->name = attr.u.str;
4529 break;
4531 case DW_AT_low_pc:
4532 if (is_int_form (&attr))
4534 low_pc = attr.u.val;
4535 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
4536 this is the base address to use when reading location
4537 lists or range lists. */
4538 if (compunit_flag)
4539 unit->base_address = low_pc;
4541 break;
4543 case DW_AT_high_pc:
4544 if (is_int_form (&attr))
4546 high_pc = attr.u.val;
4547 high_pc_relative = attr.form != DW_FORM_addr;
4549 break;
4551 case DW_AT_ranges:
4552 if (is_int_form (&attr)
4553 && !read_rangelist (unit, &unit->arange,
4554 &unit->file->trie_root, attr.u.val))
4555 goto err_exit;
4556 break;
4558 case DW_AT_comp_dir:
4560 char *comp_dir = attr.u.str;
4562 /* PR 17512: file: 1fe726be. */
4563 if (!is_str_form (&attr))
4565 _bfd_error_handler
4566 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
4567 comp_dir = NULL;
4570 if (comp_dir)
4572 /* Irix 6.2 native cc prepends <machine>.: to the compilation
4573 directory, get rid of it. */
4574 char *cp = strchr (comp_dir, ':');
4576 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
4577 comp_dir = cp + 1;
4579 unit->comp_dir = comp_dir;
4580 break;
4583 case DW_AT_language:
4584 if (is_int_form (&attr))
4585 unit->lang = attr.u.val;
4586 break;
4588 case DW_AT_addr_base:
4589 unit->dwarf_addr_offset = attr.u.val;
4590 break;
4592 case DW_AT_str_offsets_base:
4593 unit->dwarf_str_offset = attr.u.val;
4594 break;
4596 default:
4597 break;
4601 for (i = 0; i < str_count; ++i)
4602 reread_attribute (unit, &str_addrp[i], &low_pc, &high_pc,
4603 &high_pc_relative, compunit_flag);
4605 if (high_pc_relative)
4606 high_pc += low_pc;
4607 if (high_pc != 0)
4609 if (!arange_add (unit, &unit->arange, &unit->file->trie_root,
4610 low_pc, high_pc))
4611 goto err_exit;
4614 unit->first_child_die_ptr = info_ptr;
4616 free (str_addrp);
4617 return unit;
4619 err_exit:
4620 unit->error = 1;
4621 free (str_addrp);
4622 return NULL;
4625 /* Return TRUE if UNIT may contain the address given by ADDR. When
4626 there are functions written entirely with inline asm statements, the
4627 range info in the compilation unit header may not be correct. We
4628 need to consult the line info table to see if a compilation unit
4629 really contains the given address. */
4631 static bool
4632 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
4634 struct arange *arange;
4636 if (unit->error)
4637 return false;
4639 arange = &unit->arange;
4642 if (addr >= arange->low && addr < arange->high)
4643 return true;
4644 arange = arange->next;
4646 while (arange);
4648 return false;
4651 /* If UNIT contains ADDR, set the output parameters to the values for
4652 the line containing ADDR and return TRUE. Otherwise return FALSE.
4653 The output parameters, FILENAME_PTR, FUNCTION_PTR, and
4654 LINENUMBER_PTR, are pointers to the objects to be filled in. */
4656 static bool
4657 comp_unit_find_nearest_line (struct comp_unit *unit,
4658 bfd_vma addr,
4659 const char **filename_ptr,
4660 struct funcinfo **function_ptr,
4661 unsigned int *linenumber_ptr,
4662 unsigned int *discriminator_ptr)
4664 bool line_p, func_p;
4666 if (!comp_unit_maybe_decode_line_info (unit))
4667 return false;
4669 *function_ptr = NULL;
4670 func_p = lookup_address_in_function_table (unit, addr, function_ptr);
4671 if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine)
4672 unit->stash->inliner_chain = *function_ptr;
4674 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
4675 filename_ptr,
4676 linenumber_ptr,
4677 discriminator_ptr);
4678 return line_p || func_p;
4681 /* Check to see if line info is already decoded in a comp_unit.
4682 If not, decode it. Returns TRUE if no errors were encountered;
4683 FALSE otherwise. */
4685 static bool
4686 comp_unit_maybe_decode_line_info (struct comp_unit *unit)
4688 if (unit->error)
4689 return false;
4691 if (! unit->line_table)
4693 if (! unit->stmtlist)
4695 unit->error = 1;
4696 return false;
4699 unit->line_table = decode_line_info (unit);
4701 if (! unit->line_table)
4703 unit->error = 1;
4704 return false;
4707 if (unit->first_child_die_ptr < unit->end_ptr
4708 && ! scan_unit_for_symbols (unit))
4710 unit->error = 1;
4711 return false;
4715 return true;
4718 /* If UNIT contains SYM at ADDR, set the output parameters to the
4719 values for the line containing SYM. The output parameters,
4720 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
4721 filled in.
4723 Return TRUE if UNIT contains SYM, and no errors were encountered;
4724 FALSE otherwise. */
4726 static bool
4727 comp_unit_find_line (struct comp_unit *unit,
4728 asymbol *sym,
4729 bfd_vma addr,
4730 const char **filename_ptr,
4731 unsigned int *linenumber_ptr)
4733 if (!comp_unit_maybe_decode_line_info (unit))
4734 return false;
4736 if (sym->flags & BSF_FUNCTION)
4737 return lookup_symbol_in_function_table (unit, sym, addr,
4738 filename_ptr,
4739 linenumber_ptr);
4741 return lookup_symbol_in_variable_table (unit, sym, addr,
4742 filename_ptr,
4743 linenumber_ptr);
4746 /* Extract all interesting funcinfos and varinfos of a compilation
4747 unit into hash tables for faster lookup. Returns TRUE if no
4748 errors were enountered; FALSE otherwise. */
4750 static bool
4751 comp_unit_hash_info (struct dwarf2_debug *stash,
4752 struct comp_unit *unit,
4753 struct info_hash_table *funcinfo_hash_table,
4754 struct info_hash_table *varinfo_hash_table)
4756 struct funcinfo* each_func;
4757 struct varinfo* each_var;
4758 bool okay = true;
4760 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
4762 if (!comp_unit_maybe_decode_line_info (unit))
4763 return false;
4765 BFD_ASSERT (!unit->cached);
4767 /* To preserve the original search order, we went to visit the function
4768 infos in the reversed order of the list. However, making the list
4769 bi-directional use quite a bit of extra memory. So we reverse
4770 the list first, traverse the list in the now reversed order and
4771 finally reverse the list again to get back the original order. */
4772 unit->function_table = reverse_funcinfo_list (unit->function_table);
4773 for (each_func = unit->function_table;
4774 each_func && okay;
4775 each_func = each_func->prev_func)
4777 /* Skip nameless functions. */
4778 if (each_func->name)
4779 /* There is no need to copy name string into hash table as
4780 name string is either in the dwarf string buffer or
4781 info in the stash. */
4782 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
4783 (void*) each_func, false);
4785 unit->function_table = reverse_funcinfo_list (unit->function_table);
4786 if (!okay)
4787 return false;
4789 /* We do the same for variable infos. */
4790 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4791 for (each_var = unit->variable_table;
4792 each_var && okay;
4793 each_var = each_var->prev_var)
4795 /* Skip stack vars and vars with no files or names. */
4796 if (! each_var->stack
4797 && each_var->file != NULL
4798 && each_var->name != NULL)
4799 /* There is no need to copy name string into hash table as
4800 name string is either in the dwarf string buffer or
4801 info in the stash. */
4802 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
4803 (void*) each_var, false);
4806 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4807 unit->cached = true;
4808 return okay;
4811 /* Locate a section in a BFD containing debugging info. The search starts
4812 from the section after AFTER_SEC, or from the first section in the BFD if
4813 AFTER_SEC is NULL. The search works by examining the names of the
4814 sections. There are three permissiable names. The first two are given
4815 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
4816 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
4817 This is a variation on the .debug_info section which has a checksum
4818 describing the contents appended onto the name. This allows the linker to
4819 identify and discard duplicate debugging sections for different
4820 compilation units. */
4821 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
4823 static asection *
4824 find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
4825 asection *after_sec)
4827 asection *msec;
4828 const char *look;
4830 if (after_sec == NULL)
4832 look = debug_sections[debug_info].uncompressed_name;
4833 msec = bfd_get_section_by_name (abfd, look);
4834 if (msec != NULL)
4835 return msec;
4837 look = debug_sections[debug_info].compressed_name;
4838 msec = bfd_get_section_by_name (abfd, look);
4839 if (msec != NULL)
4840 return msec;
4842 for (msec = abfd->sections; msec != NULL; msec = msec->next)
4843 if (startswith (msec->name, GNU_LINKONCE_INFO))
4844 return msec;
4846 return NULL;
4849 for (msec = after_sec->next; msec != NULL; msec = msec->next)
4851 look = debug_sections[debug_info].uncompressed_name;
4852 if (strcmp (msec->name, look) == 0)
4853 return msec;
4855 look = debug_sections[debug_info].compressed_name;
4856 if (look != NULL && strcmp (msec->name, look) == 0)
4857 return msec;
4859 if (startswith (msec->name, GNU_LINKONCE_INFO))
4860 return msec;
4863 return NULL;
4866 /* Transfer VMAs from object file to separate debug file. */
4868 static void
4869 set_debug_vma (bfd *orig_bfd, bfd *debug_bfd)
4871 asection *s, *d;
4873 for (s = orig_bfd->sections, d = debug_bfd->sections;
4874 s != NULL && d != NULL;
4875 s = s->next, d = d->next)
4877 if ((d->flags & SEC_DEBUGGING) != 0)
4878 break;
4879 /* ??? Assumes 1-1 correspondence between sections in the
4880 two files. */
4881 if (strcmp (s->name, d->name) == 0)
4883 d->output_section = s->output_section;
4884 d->output_offset = s->output_offset;
4885 d->vma = s->vma;
4890 /* If the dwarf2 info was found in a separate debug file, return the
4891 debug file section corresponding to the section in the original file
4892 and the debug file symbols. */
4894 static void
4895 _bfd_dwarf2_stash_syms (struct dwarf2_debug *stash, bfd *abfd,
4896 asection **sec, asymbol ***syms)
4898 if (stash->f.bfd_ptr != abfd)
4900 asection *s, *d;
4902 if (*sec == NULL)
4904 *syms = stash->f.syms;
4905 return;
4908 for (s = abfd->sections, d = stash->f.bfd_ptr->sections;
4909 s != NULL && d != NULL;
4910 s = s->next, d = d->next)
4912 if ((d->flags & SEC_DEBUGGING) != 0)
4913 break;
4914 if (s == *sec
4915 && strcmp (s->name, d->name) == 0)
4917 *sec = d;
4918 *syms = stash->f.syms;
4919 break;
4925 /* Unset vmas for adjusted sections in STASH. */
4927 static void
4928 unset_sections (struct dwarf2_debug *stash)
4930 int i;
4931 struct adjusted_section *p;
4933 i = stash->adjusted_section_count;
4934 p = stash->adjusted_sections;
4935 for (; i > 0; i--, p++)
4936 p->section->vma = 0;
4939 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4940 relocatable object file. VMAs are normally all zero in relocatable
4941 object files, so if we want to distinguish locations in sections by
4942 address we need to set VMAs so the sections do not overlap. We
4943 also set VMA on .debug_info so that when we have multiple
4944 .debug_info sections (or the linkonce variant) they also do not
4945 overlap. The multiple .debug_info sections make up a single
4946 logical section. ??? We should probably do the same for other
4947 debug sections. */
4949 static bool
4950 place_sections (bfd *orig_bfd, struct dwarf2_debug *stash)
4952 bfd *abfd;
4953 struct adjusted_section *p;
4954 int i;
4955 const char *debug_info_name;
4957 if (stash->adjusted_section_count != 0)
4959 i = stash->adjusted_section_count;
4960 p = stash->adjusted_sections;
4961 for (; i > 0; i--, p++)
4962 p->section->vma = p->adj_vma;
4963 return true;
4966 debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
4967 i = 0;
4968 abfd = orig_bfd;
4969 while (1)
4971 asection *sect;
4973 for (sect = abfd->sections; sect != NULL; sect = sect->next)
4975 int is_debug_info;
4977 if ((sect->output_section != NULL
4978 && sect->output_section != sect
4979 && (sect->flags & SEC_DEBUGGING) == 0)
4980 || sect->vma != 0)
4981 continue;
4983 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
4984 || startswith (sect->name, GNU_LINKONCE_INFO));
4986 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
4987 && !is_debug_info)
4988 continue;
4990 i++;
4992 if (abfd == stash->f.bfd_ptr)
4993 break;
4994 abfd = stash->f.bfd_ptr;
4997 if (i <= 1)
4998 stash->adjusted_section_count = -1;
4999 else
5001 bfd_vma last_vma = 0, last_dwarf = 0;
5002 size_t amt = i * sizeof (struct adjusted_section);
5004 p = (struct adjusted_section *) bfd_malloc (amt);
5005 if (p == NULL)
5006 return false;
5008 stash->adjusted_sections = p;
5009 stash->adjusted_section_count = i;
5011 abfd = orig_bfd;
5012 while (1)
5014 asection *sect;
5016 for (sect = abfd->sections; sect != NULL; sect = sect->next)
5018 bfd_size_type sz;
5019 int is_debug_info;
5021 if ((sect->output_section != NULL
5022 && sect->output_section != sect
5023 && (sect->flags & SEC_DEBUGGING) == 0)
5024 || sect->vma != 0)
5025 continue;
5027 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
5028 || startswith (sect->name, GNU_LINKONCE_INFO));
5030 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
5031 && !is_debug_info)
5032 continue;
5034 sz = sect->rawsize ? sect->rawsize : sect->size;
5036 if (is_debug_info)
5038 BFD_ASSERT (sect->alignment_power == 0);
5039 sect->vma = last_dwarf;
5040 last_dwarf += sz;
5042 else
5044 /* Align the new address to the current section
5045 alignment. */
5046 last_vma = ((last_vma
5047 + ~(-((bfd_vma) 1 << sect->alignment_power)))
5048 & (-((bfd_vma) 1 << sect->alignment_power)));
5049 sect->vma = last_vma;
5050 last_vma += sz;
5053 p->section = sect;
5054 p->adj_vma = sect->vma;
5055 p++;
5057 if (abfd == stash->f.bfd_ptr)
5058 break;
5059 abfd = stash->f.bfd_ptr;
5063 if (orig_bfd != stash->f.bfd_ptr)
5064 set_debug_vma (orig_bfd, stash->f.bfd_ptr);
5066 return true;
5069 /* Look up a funcinfo by name using the given info hash table. If found,
5070 also update the locations pointed to by filename_ptr and linenumber_ptr.
5072 This function returns TRUE if a funcinfo that matches the given symbol
5073 and address is found with any error; otherwise it returns FALSE. */
5075 static bool
5076 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
5077 asymbol *sym,
5078 bfd_vma addr,
5079 const char **filename_ptr,
5080 unsigned int *linenumber_ptr)
5082 struct funcinfo* each_func;
5083 struct funcinfo* best_fit = NULL;
5084 bfd_vma best_fit_len = (bfd_vma) -1;
5085 struct info_list_node *node;
5086 struct arange *arange;
5087 const char *name = bfd_asymbol_name (sym);
5089 for (node = lookup_info_hash_table (hash_table, name);
5090 node;
5091 node = node->next)
5093 each_func = (struct funcinfo *) node->info;
5094 for (arange = &each_func->arange;
5095 arange;
5096 arange = arange->next)
5098 if (addr >= arange->low
5099 && addr < arange->high
5100 && arange->high - arange->low < best_fit_len)
5102 best_fit = each_func;
5103 best_fit_len = arange->high - arange->low;
5108 if (best_fit)
5110 *filename_ptr = best_fit->file;
5111 *linenumber_ptr = best_fit->line;
5112 return true;
5115 return false;
5118 /* Look up a varinfo by name using the given info hash table. If found,
5119 also update the locations pointed to by filename_ptr and linenumber_ptr.
5121 This function returns TRUE if a varinfo that matches the given symbol
5122 and address is found with any error; otherwise it returns FALSE. */
5124 static bool
5125 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
5126 asymbol *sym,
5127 bfd_vma addr,
5128 const char **filename_ptr,
5129 unsigned int *linenumber_ptr)
5131 struct varinfo* each;
5132 struct info_list_node *node;
5133 const char *name = bfd_asymbol_name (sym);
5135 for (node = lookup_info_hash_table (hash_table, name);
5136 node;
5137 node = node->next)
5139 each = (struct varinfo *) node->info;
5140 if (each->addr == addr)
5142 *filename_ptr = each->file;
5143 *linenumber_ptr = each->line;
5144 return true;
5148 return false;
5151 /* Update the funcinfo and varinfo info hash tables if they are
5152 not up to date. Returns TRUE if there is no error; otherwise
5153 returns FALSE and disable the info hash tables. */
5155 static bool
5156 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
5158 struct comp_unit *each;
5160 /* Exit if hash tables are up-to-date. */
5161 if (stash->f.all_comp_units == stash->hash_units_head)
5162 return true;
5164 if (stash->hash_units_head)
5165 each = stash->hash_units_head->prev_unit;
5166 else
5167 each = stash->f.last_comp_unit;
5169 while (each)
5171 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
5172 stash->varinfo_hash_table))
5174 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
5175 return false;
5177 each = each->prev_unit;
5180 stash->hash_units_head = stash->f.all_comp_units;
5181 return true;
5184 /* Check consistency of info hash tables. This is for debugging only. */
5186 static void ATTRIBUTE_UNUSED
5187 stash_verify_info_hash_table (struct dwarf2_debug *stash)
5189 struct comp_unit *each_unit;
5190 struct funcinfo *each_func;
5191 struct varinfo *each_var;
5192 struct info_list_node *node;
5193 bool found;
5195 for (each_unit = stash->f.all_comp_units;
5196 each_unit;
5197 each_unit = each_unit->next_unit)
5199 for (each_func = each_unit->function_table;
5200 each_func;
5201 each_func = each_func->prev_func)
5203 if (!each_func->name)
5204 continue;
5205 node = lookup_info_hash_table (stash->funcinfo_hash_table,
5206 each_func->name);
5207 BFD_ASSERT (node);
5208 found = false;
5209 while (node && !found)
5211 found = node->info == each_func;
5212 node = node->next;
5214 BFD_ASSERT (found);
5217 for (each_var = each_unit->variable_table;
5218 each_var;
5219 each_var = each_var->prev_var)
5221 if (!each_var->name || !each_var->file || each_var->stack)
5222 continue;
5223 node = lookup_info_hash_table (stash->varinfo_hash_table,
5224 each_var->name);
5225 BFD_ASSERT (node);
5226 found = false;
5227 while (node && !found)
5229 found = node->info == each_var;
5230 node = node->next;
5232 BFD_ASSERT (found);
5237 /* Check to see if we want to enable the info hash tables, which consume
5238 quite a bit of memory. Currently we only check the number times
5239 bfd_dwarf2_find_line is called. In the future, we may also want to
5240 take the number of symbols into account. */
5242 static void
5243 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
5245 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
5247 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
5248 return;
5250 /* FIXME: Maybe we should check the reduce_memory_overheads
5251 and optimize fields in the bfd_link_info structure ? */
5253 /* Create hash tables. */
5254 stash->funcinfo_hash_table = create_info_hash_table (abfd);
5255 stash->varinfo_hash_table = create_info_hash_table (abfd);
5256 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
5258 /* Turn off info hashes if any allocation above fails. */
5259 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
5260 return;
5262 /* We need a forced update so that the info hash tables will
5263 be created even though there is no compilation unit. That
5264 happens if STASH_INFO_HASH_TRIGGER is 0. */
5265 if (stash_maybe_update_info_hash_tables (stash))
5266 stash->info_hash_status = STASH_INFO_HASH_ON;
5269 /* Find the file and line associated with a symbol and address using the
5270 info hash tables of a stash. If there is a match, the function returns
5271 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
5272 otherwise it returns FALSE. */
5274 static bool
5275 stash_find_line_fast (struct dwarf2_debug *stash,
5276 asymbol *sym,
5277 bfd_vma addr,
5278 const char **filename_ptr,
5279 unsigned int *linenumber_ptr)
5281 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
5283 if (sym->flags & BSF_FUNCTION)
5284 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
5285 filename_ptr, linenumber_ptr);
5286 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
5287 filename_ptr, linenumber_ptr);
5290 /* Save current section VMAs. */
5292 static bool
5293 save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
5295 asection *s;
5296 unsigned int i;
5298 if (abfd->section_count == 0)
5299 return true;
5300 stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count);
5301 if (stash->sec_vma == NULL)
5302 return false;
5303 stash->sec_vma_count = abfd->section_count;
5304 for (i = 0, s = abfd->sections;
5305 s != NULL && i < abfd->section_count;
5306 i++, s = s->next)
5308 if (s->output_section != NULL)
5309 stash->sec_vma[i] = s->output_section->vma + s->output_offset;
5310 else
5311 stash->sec_vma[i] = s->vma;
5313 return true;
5316 /* Compare current section VMAs against those at the time the stash
5317 was created. If find_nearest_line is used in linker warnings or
5318 errors early in the link process, the debug info stash will be
5319 invalid for later calls. This is because we relocate debug info
5320 sections, so the stashed section contents depend on symbol values,
5321 which in turn depend on section VMAs. */
5323 static bool
5324 section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash)
5326 asection *s;
5327 unsigned int i;
5329 /* PR 24334: If the number of sections in ABFD has changed between
5330 when the stash was created and now, then we cannot trust the
5331 stashed vma information. */
5332 if (abfd->section_count != stash->sec_vma_count)
5333 return false;
5335 for (i = 0, s = abfd->sections;
5336 s != NULL && i < abfd->section_count;
5337 i++, s = s->next)
5339 bfd_vma vma;
5341 if (s->output_section != NULL)
5342 vma = s->output_section->vma + s->output_offset;
5343 else
5344 vma = s->vma;
5345 if (vma != stash->sec_vma[i])
5346 return false;
5348 return true;
5351 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
5352 If DEBUG_BFD is not specified, we read debug information from ABFD
5353 or its gnu_debuglink. The results will be stored in PINFO.
5354 The function returns TRUE iff debug information is ready. */
5356 bool
5357 _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
5358 const struct dwarf_debug_section *debug_sections,
5359 asymbol **symbols,
5360 void **pinfo,
5361 bool do_place)
5363 size_t amt = sizeof (struct dwarf2_debug);
5364 bfd_size_type total_size;
5365 asection *msec;
5366 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
5368 if (stash != NULL)
5370 if (stash->orig_bfd == abfd
5371 && section_vma_same (abfd, stash))
5373 /* Check that we did previously find some debug information
5374 before attempting to make use of it. */
5375 if (stash->f.bfd_ptr != NULL)
5377 if (do_place && !place_sections (abfd, stash))
5378 return false;
5379 return true;
5382 return false;
5384 _bfd_dwarf2_cleanup_debug_info (abfd, pinfo);
5385 memset (stash, 0, amt);
5387 else
5389 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
5390 if (! stash)
5391 return false;
5393 stash->orig_bfd = abfd;
5394 stash->debug_sections = debug_sections;
5395 stash->f.syms = symbols;
5396 if (!save_section_vma (abfd, stash))
5397 return false;
5399 stash->f.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
5400 del_abbrev, calloc, free);
5401 if (!stash->f.abbrev_offsets)
5402 return false;
5404 stash->alt.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
5405 del_abbrev, calloc, free);
5406 if (!stash->alt.abbrev_offsets)
5407 return false;
5409 stash->f.trie_root = alloc_trie_leaf (abfd);
5410 if (!stash->f.trie_root)
5411 return false;
5413 stash->alt.trie_root = alloc_trie_leaf (abfd);
5414 if (!stash->alt.trie_root)
5415 return false;
5417 *pinfo = stash;
5419 if (debug_bfd == NULL)
5420 debug_bfd = abfd;
5422 msec = find_debug_info (debug_bfd, debug_sections, NULL);
5423 if (msec == NULL && abfd == debug_bfd)
5425 char * debug_filename;
5427 debug_filename = bfd_follow_build_id_debuglink (abfd, DEBUGDIR);
5428 if (debug_filename == NULL)
5429 debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
5431 if (debug_filename == NULL)
5432 /* No dwarf2 info, and no gnu_debuglink to follow.
5433 Note that at this point the stash has been allocated, but
5434 contains zeros. This lets future calls to this function
5435 fail more quickly. */
5436 return false;
5438 debug_bfd = bfd_openr (debug_filename, NULL);
5439 free (debug_filename);
5440 if (debug_bfd == NULL)
5441 /* FIXME: Should we report our failure to follow the debuglink ? */
5442 return false;
5444 /* Set BFD_DECOMPRESS to decompress debug sections. */
5445 debug_bfd->flags |= BFD_DECOMPRESS;
5446 if (!bfd_check_format (debug_bfd, bfd_object)
5447 || (msec = find_debug_info (debug_bfd,
5448 debug_sections, NULL)) == NULL
5449 || !bfd_generic_link_read_symbols (debug_bfd))
5451 bfd_close (debug_bfd);
5452 return false;
5455 symbols = bfd_get_outsymbols (debug_bfd);
5456 stash->f.syms = symbols;
5457 stash->close_on_cleanup = true;
5459 stash->f.bfd_ptr = debug_bfd;
5461 if (do_place
5462 && !place_sections (abfd, stash))
5463 return false;
5465 /* There can be more than one DWARF2 info section in a BFD these
5466 days. First handle the easy case when there's only one. If
5467 there's more than one, try case two: none of the sections is
5468 compressed. In that case, read them all in and produce one
5469 large stash. We do this in two passes - in the first pass we
5470 just accumulate the section sizes, and in the second pass we
5471 read in the section's contents. (The allows us to avoid
5472 reallocing the data as we add sections to the stash.) If
5473 some or all sections are compressed, then do things the slow
5474 way, with a bunch of reallocs. */
5476 if (! find_debug_info (debug_bfd, debug_sections, msec))
5478 /* Case 1: only one info section. */
5479 total_size = msec->size;
5480 if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
5481 symbols, 0,
5482 &stash->f.dwarf_info_buffer, &total_size))
5483 return false;
5485 else
5487 /* Case 2: multiple sections. */
5488 for (total_size = 0;
5489 msec;
5490 msec = find_debug_info (debug_bfd, debug_sections, msec))
5492 if (_bfd_section_size_insane (debug_bfd, msec))
5493 return false;
5494 /* Catch PR25070 testcase overflowing size calculation here. */
5495 if (total_size + msec->size < total_size)
5497 bfd_set_error (bfd_error_no_memory);
5498 return false;
5500 total_size += msec->size;
5503 stash->f.dwarf_info_buffer = (bfd_byte *) bfd_malloc (total_size);
5504 if (stash->f.dwarf_info_buffer == NULL)
5505 return false;
5507 total_size = 0;
5508 for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
5509 msec;
5510 msec = find_debug_info (debug_bfd, debug_sections, msec))
5512 bfd_size_type size;
5514 size = msec->size;
5515 if (size == 0)
5516 continue;
5518 if (!(bfd_simple_get_relocated_section_contents
5519 (debug_bfd, msec, stash->f.dwarf_info_buffer + total_size,
5520 symbols)))
5521 return false;
5523 total_size += size;
5527 stash->f.info_ptr = stash->f.dwarf_info_buffer;
5528 stash->f.dwarf_info_size = total_size;
5529 return true;
5532 /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
5534 static struct comp_unit *
5535 stash_comp_unit (struct dwarf2_debug *stash, struct dwarf2_debug_file *file)
5537 bfd_size_type length;
5538 unsigned int offset_size;
5539 bfd_byte *info_ptr_unit = file->info_ptr;
5540 bfd_byte *info_ptr_end = file->dwarf_info_buffer + file->dwarf_info_size;
5542 if (file->info_ptr >= info_ptr_end)
5543 return NULL;
5545 length = read_4_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5546 /* A 0xffffff length is the DWARF3 way of indicating
5547 we use 64-bit offsets, instead of 32-bit offsets. */
5548 if (length == 0xffffffff)
5550 offset_size = 8;
5551 length = read_8_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5553 /* A zero length is the IRIX way of indicating 64-bit offsets,
5554 mostly because the 64-bit length will generally fit in 32
5555 bits, and the endianness helps. */
5556 else if (length == 0)
5558 offset_size = 8;
5559 length = read_4_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5561 /* In the absence of the hints above, we assume 32-bit DWARF2
5562 offsets even for targets with 64-bit addresses, because:
5563 a) most of the time these targets will not have generated
5564 more than 2Gb of debug info and so will not need 64-bit
5565 offsets,
5567 b) if they do use 64-bit offsets but they are not using
5568 the size hints that are tested for above then they are
5569 not conforming to the DWARF3 standard anyway. */
5570 else
5571 offset_size = 4;
5573 if (length != 0
5574 && length <= (size_t) (info_ptr_end - file->info_ptr))
5576 struct comp_unit *each = parse_comp_unit (stash, file,
5577 file->info_ptr, length,
5578 info_ptr_unit, offset_size);
5579 if (each)
5581 if (file->comp_unit_tree == NULL)
5582 file->comp_unit_tree
5583 = splay_tree_new (splay_tree_compare_addr_range,
5584 splay_tree_free_addr_range, NULL);
5586 struct addr_range *r
5587 = (struct addr_range *)bfd_malloc (sizeof (struct addr_range));
5588 r->start = each->info_ptr_unit;
5589 r->end = each->end_ptr;
5590 splay_tree_node v = splay_tree_lookup (file->comp_unit_tree,
5591 (splay_tree_key)r);
5592 if (v != NULL || r->end <= r->start)
5593 abort ();
5594 splay_tree_insert (file->comp_unit_tree, (splay_tree_key)r,
5595 (splay_tree_value)each);
5597 if (file->all_comp_units)
5598 file->all_comp_units->prev_unit = each;
5599 else
5600 file->last_comp_unit = each;
5602 each->next_unit = file->all_comp_units;
5603 file->all_comp_units = each;
5605 if (each->arange.high == 0)
5607 each->next_unit_without_ranges = file->all_comp_units_without_ranges;
5608 file->all_comp_units_without_ranges = each->next_unit_without_ranges;
5611 file->info_ptr += length;
5612 return each;
5616 /* Don't trust any of the DWARF info after a corrupted length or
5617 parse error. */
5618 file->info_ptr = info_ptr_end;
5619 return NULL;
5622 /* Hash function for an asymbol. */
5624 static hashval_t
5625 hash_asymbol (const void *sym)
5627 const asymbol *asym = sym;
5628 return htab_hash_string (asym->name);
5631 /* Equality function for asymbols. */
5633 static int
5634 eq_asymbol (const void *a, const void *b)
5636 const asymbol *sa = a;
5637 const asymbol *sb = b;
5638 return strcmp (sa->name, sb->name) == 0;
5641 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
5642 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
5643 symbol in SYMBOLS and return the difference between the low_pc and
5644 the symbol's address. Returns 0 if no suitable symbol could be found. */
5646 bfd_signed_vma
5647 _bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo)
5649 struct dwarf2_debug *stash;
5650 struct comp_unit * unit;
5651 htab_t sym_hash;
5652 bfd_signed_vma result = 0;
5653 asymbol ** psym;
5655 stash = (struct dwarf2_debug *) *pinfo;
5657 if (stash == NULL || symbols == NULL)
5658 return 0;
5660 sym_hash = htab_create_alloc (10, hash_asymbol, eq_asymbol,
5661 NULL, xcalloc, free);
5662 for (psym = symbols; * psym != NULL; psym++)
5664 asymbol * sym = * psym;
5666 if (sym->flags & BSF_FUNCTION && sym->section != NULL)
5668 void **slot = htab_find_slot (sym_hash, sym, INSERT);
5669 *slot = sym;
5673 for (unit = stash->f.all_comp_units; unit; unit = unit->next_unit)
5675 struct funcinfo * func;
5677 comp_unit_maybe_decode_line_info (unit);
5679 for (func = unit->function_table; func != NULL; func = func->prev_func)
5680 if (func->name && func->arange.low)
5682 asymbol search, *sym;
5684 /* FIXME: Do we need to scan the aranges looking for the
5685 lowest pc value? */
5687 search.name = func->name;
5688 sym = htab_find (sym_hash, &search);
5689 if (sym != NULL)
5691 result = func->arange.low - (sym->value + sym->section->vma);
5692 goto done;
5697 done:
5698 htab_delete (sym_hash);
5699 return result;
5702 /* See _bfd_dwarf2_find_nearest_line_with_alt. */
5705 _bfd_dwarf2_find_nearest_line (bfd *abfd,
5706 asymbol **symbols,
5707 asymbol *symbol,
5708 asection *section,
5709 bfd_vma offset,
5710 const char **filename_ptr,
5711 const char **functionname_ptr,
5712 unsigned int *linenumber_ptr,
5713 unsigned int *discriminator_ptr,
5714 const struct dwarf_debug_section *debug_sections,
5715 void **pinfo)
5717 return _bfd_dwarf2_find_nearest_line_with_alt
5718 (abfd, NULL, symbols, symbol, section, offset, filename_ptr,
5719 functionname_ptr, linenumber_ptr, discriminator_ptr, debug_sections,
5720 pinfo);
5723 /* Find the source code location of SYMBOL. If SYMBOL is NULL
5724 then find the nearest source code location corresponding to
5725 the address SECTION + OFFSET.
5726 Returns 1 if the line is found without error and fills in
5727 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
5728 NULL the FUNCTIONNAME_PTR is also filled in.
5729 Returns 2 if partial information from _bfd_elf_find_function is
5730 returned (function and maybe file) by looking at symbols. DWARF2
5731 info is present but not regarding the requested code location.
5732 Returns 0 otherwise.
5733 SYMBOLS contains the symbol table for ABFD.
5734 DEBUG_SECTIONS contains the name of the dwarf debug sections.
5735 If ALT_FILENAME is given, attempt to open the file and use it
5736 as the .gnu_debugaltlink file. Otherwise this file will be
5737 searched for when needed. */
5740 _bfd_dwarf2_find_nearest_line_with_alt
5741 (bfd *abfd,
5742 const char *alt_filename,
5743 asymbol **symbols,
5744 asymbol *symbol,
5745 asection *section,
5746 bfd_vma offset,
5747 const char **filename_ptr,
5748 const char **functionname_ptr,
5749 unsigned int *linenumber_ptr,
5750 unsigned int *discriminator_ptr,
5751 const struct dwarf_debug_section *debug_sections,
5752 void **pinfo)
5754 /* Read each compilation unit from the section .debug_info, and check
5755 to see if it contains the address we are searching for. If yes,
5756 lookup the address, and return the line number info. If no, go
5757 on to the next compilation unit.
5759 We keep a list of all the previously read compilation units, and
5760 a pointer to the next un-read compilation unit. Check the
5761 previously read units before reading more. */
5762 struct dwarf2_debug *stash;
5763 /* What address are we looking for? */
5764 bfd_vma addr;
5765 struct comp_unit* each;
5766 struct funcinfo *function = NULL;
5767 int found = false;
5768 bool do_line;
5770 *filename_ptr = NULL;
5771 if (functionname_ptr != NULL)
5772 *functionname_ptr = NULL;
5773 *linenumber_ptr = 0;
5774 if (discriminator_ptr)
5775 *discriminator_ptr = 0;
5777 if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections,
5778 symbols, pinfo,
5779 (abfd->flags & (EXEC_P | DYNAMIC)) == 0))
5780 return false;
5782 stash = (struct dwarf2_debug *) *pinfo;
5784 if (stash->alt.bfd_ptr == NULL && alt_filename != NULL)
5786 bfd *alt_bfd = bfd_openr (alt_filename, NULL);
5788 if (alt_bfd == NULL)
5789 /* bfd_openr will have set the bfd_error. */
5790 return false;
5791 if (!bfd_check_format (alt_bfd, bfd_object))
5793 bfd_set_error (bfd_error_wrong_format);
5794 bfd_close (alt_bfd);
5795 return false;
5798 stash->alt.bfd_ptr = alt_bfd;
5801 do_line = symbol != NULL;
5802 if (do_line)
5804 BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL);
5805 section = bfd_asymbol_section (symbol);
5806 addr = symbol->value;
5808 else
5810 BFD_ASSERT (section != NULL && functionname_ptr != NULL);
5811 addr = offset;
5813 /* If we have no SYMBOL but the section we're looking at is not a
5814 code section, then take a look through the list of symbols to see
5815 if we have a symbol at the address we're looking for. If we do
5816 then use this to look up line information. This will allow us to
5817 give file and line results for data symbols. We exclude code
5818 symbols here, if we look up a function symbol and then look up the
5819 line information we'll actually return the line number for the
5820 opening '{' rather than the function definition line. This is
5821 because looking up by symbol uses the line table, in which the
5822 first line for a function is usually the opening '{', while
5823 looking up the function by section + offset uses the
5824 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
5825 which will be the line of the function name. */
5826 if (symbols != NULL && (section->flags & SEC_CODE) == 0)
5828 asymbol **tmp;
5830 for (tmp = symbols; (*tmp) != NULL; ++tmp)
5831 if ((*tmp)->the_bfd == abfd
5832 && (*tmp)->section == section
5833 && (*tmp)->value == offset
5834 && ((*tmp)->flags & BSF_SECTION_SYM) == 0)
5836 symbol = *tmp;
5837 do_line = true;
5838 /* For local symbols, keep going in the hope we find a
5839 global. */
5840 if ((symbol->flags & BSF_GLOBAL) != 0)
5841 break;
5846 if (section->output_section)
5847 addr += section->output_section->vma + section->output_offset;
5848 else
5849 addr += section->vma;
5851 /* A null info_ptr indicates that there is no dwarf2 info
5852 (or that an error occured while setting up the stash). */
5853 if (! stash->f.info_ptr)
5854 return false;
5856 stash->inliner_chain = NULL;
5858 /* Check the previously read comp. units first. */
5859 if (do_line)
5861 /* The info hash tables use quite a bit of memory. We may not want to
5862 always use them. We use some heuristics to decide if and when to
5863 turn it on. */
5864 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
5865 stash_maybe_enable_info_hash_tables (abfd, stash);
5867 /* Keep info hash table up to date if they are available. Note that we
5868 may disable the hash tables if there is any error duing update. */
5869 if (stash->info_hash_status == STASH_INFO_HASH_ON)
5870 stash_maybe_update_info_hash_tables (stash);
5872 if (stash->info_hash_status == STASH_INFO_HASH_ON)
5874 found = stash_find_line_fast (stash, symbol, addr,
5875 filename_ptr, linenumber_ptr);
5876 if (found)
5877 goto done;
5880 /* Check the previously read comp. units first. */
5881 for (each = stash->f.all_comp_units; each; each = each->next_unit)
5882 if ((symbol->flags & BSF_FUNCTION) == 0
5883 || each->arange.high == 0
5884 || comp_unit_contains_address (each, addr))
5886 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
5887 linenumber_ptr);
5888 if (found)
5889 goto done;
5892 else
5894 struct trie_node *trie = stash->f.trie_root;
5895 unsigned int bits = VMA_BITS - 8;
5896 struct comp_unit **prev_each;
5898 /* Traverse interior nodes until we get to a leaf. */
5899 while (trie && trie->num_room_in_leaf == 0)
5901 int ch = (addr >> bits) & 0xff;
5902 trie = ((struct trie_interior *) trie)->children[ch];
5903 bits -= 8;
5906 if (trie)
5908 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
5909 unsigned int i;
5911 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
5912 leaf->ranges[i].unit->mark = false;
5914 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
5916 struct comp_unit *unit = leaf->ranges[i].unit;
5917 if (unit->mark
5918 || addr < leaf->ranges[i].low_pc
5919 || addr >= leaf->ranges[i].high_pc)
5920 continue;
5921 unit->mark = true;
5923 found = comp_unit_find_nearest_line (unit, addr,
5924 filename_ptr,
5925 &function,
5926 linenumber_ptr,
5927 discriminator_ptr);
5928 if (found)
5929 goto done;
5933 /* Also scan through all compilation units without any ranges,
5934 taking them out of the list if they have acquired any since
5935 last time. */
5936 prev_each = &stash->f.all_comp_units_without_ranges;
5937 for (each = *prev_each; each; each = each->next_unit_without_ranges)
5939 if (each->arange.high != 0)
5941 *prev_each = each->next_unit_without_ranges;
5942 continue;
5945 found = comp_unit_find_nearest_line (each, addr,
5946 filename_ptr,
5947 &function,
5948 linenumber_ptr,
5949 discriminator_ptr);
5950 if (found)
5951 goto done;
5952 prev_each = &each->next_unit_without_ranges;
5956 /* Read each remaining comp. units checking each as they are read. */
5957 while ((each = stash_comp_unit (stash, &stash->f)) != NULL)
5959 /* DW_AT_low_pc and DW_AT_high_pc are optional for
5960 compilation units. If we don't have them (i.e.,
5961 unit->high == 0), we need to consult the line info table
5962 to see if a compilation unit contains the given
5963 address. */
5964 if (do_line)
5965 found = (((symbol->flags & BSF_FUNCTION) == 0
5966 || each->arange.high == 0
5967 || comp_unit_contains_address (each, addr))
5968 && comp_unit_find_line (each, symbol, addr,
5969 filename_ptr, linenumber_ptr));
5970 else
5971 found = ((each->arange.high == 0
5972 || comp_unit_contains_address (each, addr))
5973 && comp_unit_find_nearest_line (each, addr,
5974 filename_ptr,
5975 &function,
5976 linenumber_ptr,
5977 discriminator_ptr));
5979 if (found)
5980 break;
5983 done:
5984 if (functionname_ptr && function && function->is_linkage)
5986 *functionname_ptr = function->name;
5987 if (!found)
5988 found = 2;
5990 else if (functionname_ptr
5991 && (!*functionname_ptr
5992 || (function && !function->is_linkage)))
5994 asymbol *fun;
5995 asymbol **syms = symbols;
5996 asection *sec = section;
5998 _bfd_dwarf2_stash_syms (stash, abfd, &sec, &syms);
5999 fun = _bfd_elf_find_function (abfd, syms, sec, offset,
6000 *filename_ptr ? NULL : filename_ptr,
6001 functionname_ptr);
6003 if (!found && fun != NULL)
6004 found = 2;
6006 if (function && !function->is_linkage)
6008 bfd_vma sec_vma;
6010 sec_vma = section->vma;
6011 if (section->output_section != NULL)
6012 sec_vma = section->output_section->vma + section->output_offset;
6013 if (fun == NULL)
6014 *functionname_ptr = function->name;
6015 else if (fun->value + sec_vma == function->arange.low)
6016 function->name = *functionname_ptr;
6017 /* Even if we didn't find a linkage name, say that we have
6018 to stop a repeated search of symbols. */
6019 function->is_linkage = true;
6023 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
6024 unset_sections (stash);
6026 return found;
6029 bool
6030 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
6031 const char **filename_ptr,
6032 const char **functionname_ptr,
6033 unsigned int *linenumber_ptr,
6034 void **pinfo)
6036 struct dwarf2_debug *stash;
6038 stash = (struct dwarf2_debug *) *pinfo;
6039 if (stash)
6041 struct funcinfo *func = stash->inliner_chain;
6043 if (func && func->caller_func)
6045 *filename_ptr = func->caller_file;
6046 *functionname_ptr = func->caller_func->name;
6047 *linenumber_ptr = func->caller_line;
6048 stash->inliner_chain = func->caller_func;
6049 return true;
6053 return false;
6056 void
6057 _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
6059 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
6060 struct comp_unit *each;
6061 struct dwarf2_debug_file *file;
6063 if (abfd == NULL || stash == NULL)
6064 return;
6066 if (stash->varinfo_hash_table)
6067 bfd_hash_table_free (&stash->varinfo_hash_table->base);
6068 if (stash->funcinfo_hash_table)
6069 bfd_hash_table_free (&stash->funcinfo_hash_table->base);
6071 file = &stash->f;
6072 while (1)
6074 for (each = file->all_comp_units; each; each = each->next_unit)
6076 struct funcinfo *function_table = each->function_table;
6077 struct varinfo *variable_table = each->variable_table;
6079 if (each->line_table && each->line_table != file->line_table)
6081 free (each->line_table->files);
6082 free (each->line_table->dirs);
6085 free (each->lookup_funcinfo_table);
6086 each->lookup_funcinfo_table = NULL;
6088 while (function_table)
6090 free (function_table->file);
6091 function_table->file = NULL;
6092 free (function_table->caller_file);
6093 function_table->caller_file = NULL;
6094 function_table = function_table->prev_func;
6097 while (variable_table)
6099 free (variable_table->file);
6100 variable_table->file = NULL;
6101 variable_table = variable_table->prev_var;
6105 if (file->line_table)
6107 free (file->line_table->files);
6108 free (file->line_table->dirs);
6110 htab_delete (file->abbrev_offsets);
6111 if (file->comp_unit_tree != NULL)
6112 splay_tree_delete (file->comp_unit_tree);
6114 free (file->dwarf_line_str_buffer);
6115 free (file->dwarf_str_buffer);
6116 free (file->dwarf_ranges_buffer);
6117 free (file->dwarf_line_buffer);
6118 free (file->dwarf_abbrev_buffer);
6119 free (file->dwarf_info_buffer);
6120 if (file == &stash->alt)
6121 break;
6122 file = &stash->alt;
6124 free (stash->sec_vma);
6125 free (stash->adjusted_sections);
6126 if (stash->close_on_cleanup)
6127 bfd_close (stash->f.bfd_ptr);
6128 if (stash->alt.bfd_ptr)
6129 bfd_close (stash->alt.bfd_ptr);
6132 /* Find the function to a particular section and offset,
6133 for error reporting. */
6135 asymbol *
6136 _bfd_elf_find_function (bfd *abfd,
6137 asymbol **symbols,
6138 asection *section,
6139 bfd_vma offset,
6140 const char **filename_ptr,
6141 const char **functionname_ptr)
6143 struct elf_find_function_cache
6145 asection *last_section;
6146 asymbol *func;
6147 const char *filename;
6148 bfd_size_type func_size;
6149 } *cache;
6151 if (symbols == NULL)
6152 return NULL;
6154 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6155 return NULL;
6157 cache = elf_tdata (abfd)->elf_find_function_cache;
6158 if (cache == NULL)
6160 cache = bfd_zalloc (abfd, sizeof (*cache));
6161 elf_tdata (abfd)->elf_find_function_cache = cache;
6162 if (cache == NULL)
6163 return NULL;
6165 if (cache->last_section != section
6166 || cache->func == NULL
6167 || offset < cache->func->value
6168 || offset >= cache->func->value + cache->func_size)
6170 asymbol *file;
6171 bfd_vma low_func;
6172 asymbol **p;
6173 /* ??? Given multiple file symbols, it is impossible to reliably
6174 choose the right file name for global symbols. File symbols are
6175 local symbols, and thus all file symbols must sort before any
6176 global symbols. The ELF spec may be interpreted to say that a
6177 file symbol must sort before other local symbols, but currently
6178 ld -r doesn't do this. So, for ld -r output, it is possible to
6179 make a better choice of file name for local symbols by ignoring
6180 file symbols appearing after a given local symbol. */
6181 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
6182 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6184 file = NULL;
6185 low_func = 0;
6186 state = nothing_seen;
6187 cache->filename = NULL;
6188 cache->func = NULL;
6189 cache->func_size = 0;
6190 cache->last_section = section;
6192 for (p = symbols; *p != NULL; p++)
6194 asymbol *sym = *p;
6195 bfd_vma code_off;
6196 bfd_size_type size;
6198 if ((sym->flags & BSF_FILE) != 0)
6200 file = sym;
6201 if (state == symbol_seen)
6202 state = file_after_symbol_seen;
6203 continue;
6206 size = bed->maybe_function_sym (sym, section, &code_off);
6207 if (size != 0
6208 && code_off <= offset
6209 && (code_off > low_func
6210 || (code_off == low_func
6211 && size > cache->func_size)))
6213 cache->func = sym;
6214 cache->func_size = size;
6215 cache->filename = NULL;
6216 low_func = code_off;
6217 if (file != NULL
6218 && ((sym->flags & BSF_LOCAL) != 0
6219 || state != file_after_symbol_seen))
6220 cache->filename = bfd_asymbol_name (file);
6222 if (state == nothing_seen)
6223 state = symbol_seen;
6227 if (cache->func == NULL)
6228 return NULL;
6230 if (filename_ptr)
6231 *filename_ptr = cache->filename;
6232 if (functionname_ptr)
6233 *functionname_ptr = bfd_asymbol_name (cache->func);
6235 return cache->func;