gprofng: fix build with -mx32
[binutils-gdb/blckswan.git] / bfd / dwarf2.c
blob6a728fc38b0366507493f943e5478fedd5af80bc
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 "libbfd.h"
36 #include "elf-bfd.h"
37 #include "dwarf2.h"
38 #include "hashtab.h"
40 /* The data in the .debug_line statement prologue looks like this. */
42 struct line_head
44 bfd_vma total_length;
45 unsigned short version;
46 bfd_vma prologue_length;
47 unsigned char minimum_instruction_length;
48 unsigned char maximum_ops_per_insn;
49 unsigned char default_is_stmt;
50 int line_base;
51 unsigned char line_range;
52 unsigned char opcode_base;
53 unsigned char *standard_opcode_lengths;
56 /* Attributes have a name and a value. */
58 struct attribute
60 enum dwarf_attribute name;
61 enum dwarf_form form;
62 union
64 char *str;
65 struct dwarf_block *blk;
66 uint64_t val;
67 int64_t sval;
72 /* Blocks are a bunch of untyped bytes. */
73 struct dwarf_block
75 unsigned int size;
76 bfd_byte *data;
79 struct adjusted_section
81 asection *section;
82 bfd_vma adj_vma;
85 /* A trie to map quickly from address range to compilation unit.
87 This is a fairly standard radix-256 trie, used to quickly locate which
88 compilation unit any given address belongs to. Given that each compilation
89 unit may register hundreds of very small and unaligned ranges (which may
90 potentially overlap, due to inlining and other concerns), and a large
91 program may end up containing hundreds of thousands of such ranges, we cannot
92 scan through them linearly without undue slowdown.
94 We use a hybrid trie to avoid memory explosion: There are two types of trie
95 nodes, leaves and interior nodes. (Almost all nodes are leaves, so they
96 take up the bulk of the memory usage.) Leaves contain a simple array of
97 ranges (high/low address) and which compilation unit contains those ranges,
98 and when we get to a leaf, we scan through it linearly. Interior nodes
99 contain pointers to 256 other nodes, keyed by the next byte of the address.
100 So for a 64-bit address like 0x1234567abcd, we would start at the root and go
101 down child[0x00]->child[0x00]->child[0x01]->child[0x23]->child[0x45] etc.,
102 until we hit a leaf. (Nodes are, in general, leaves until they exceed the
103 default allocation of 16 elements, at which point they are converted to
104 interior node if possible.) This gives us near-constant lookup times;
105 the only thing that can be costly is if there are lots of overlapping ranges
106 within a single 256-byte segment of the binary, in which case we have to
107 scan through them all to find the best match.
109 For a binary with few ranges, we will in practice only have a single leaf
110 node at the root, containing a simple array. Thus, the scheme is efficient
111 for both small and large binaries.
114 /* Experiments have shown 16 to be a memory-efficient default leaf size.
115 The only case where a leaf will hold more memory than this, is at the
116 bottomost level (covering 256 bytes in the binary), where we'll expand
117 the leaf to be able to hold more ranges if needed.
119 #define TRIE_LEAF_SIZE 16
121 /* All trie_node pointers will really be trie_leaf or trie_interior,
122 but they have this common head. */
123 struct trie_node
125 /* If zero, we are an interior node.
126 Otherwise, how many ranges we have room for in this leaf. */
127 unsigned int num_room_in_leaf;
130 struct trie_leaf
132 struct trie_node head;
133 unsigned int num_stored_in_leaf;
134 struct {
135 struct comp_unit *unit;
136 bfd_vma low_pc, high_pc;
137 } ranges[TRIE_LEAF_SIZE];
140 struct trie_interior
142 struct trie_node head;
143 struct trie_node *children[256];
146 static struct trie_node *alloc_trie_leaf (bfd *abfd)
148 struct trie_leaf *leaf =
149 bfd_zalloc (abfd, sizeof (struct trie_leaf));
150 if (leaf == NULL)
151 return NULL;
152 leaf->head.num_room_in_leaf = TRIE_LEAF_SIZE;
153 return &leaf->head;
156 struct dwarf2_debug_file
158 /* The actual bfd from which debug info was loaded. Might be
159 different to orig_bfd because of gnu_debuglink sections. */
160 bfd *bfd_ptr;
162 /* Pointer to the symbol table. */
163 asymbol **syms;
165 /* The current info pointer for the .debug_info section being parsed. */
166 bfd_byte *info_ptr;
168 /* A pointer to the memory block allocated for .debug_info sections. */
169 bfd_byte *dwarf_info_buffer;
171 /* Length of the loaded .debug_info sections. */
172 bfd_size_type dwarf_info_size;
174 /* Pointer to the .debug_abbrev section loaded into memory. */
175 bfd_byte *dwarf_abbrev_buffer;
177 /* Length of the loaded .debug_abbrev section. */
178 bfd_size_type dwarf_abbrev_size;
180 /* Buffer for decode_line_info. */
181 bfd_byte *dwarf_line_buffer;
183 /* Length of the loaded .debug_line section. */
184 bfd_size_type dwarf_line_size;
186 /* Pointer to the .debug_str section loaded into memory. */
187 bfd_byte *dwarf_str_buffer;
189 /* Length of the loaded .debug_str section. */
190 bfd_size_type dwarf_str_size;
192 /* Pointer to the .debug_str_offsets section loaded into memory. */
193 bfd_byte *dwarf_str_offsets_buffer;
195 /* Length of the loaded .debug_str_offsets section. */
196 bfd_size_type dwarf_str_offsets_size;
198 /* Pointer to the .debug_addr section loaded into memory. */
199 bfd_byte *dwarf_addr_buffer;
201 /* Length of the loaded .debug_addr section. */
202 bfd_size_type dwarf_addr_size;
204 /* Pointer to the .debug_line_str section loaded into memory. */
205 bfd_byte *dwarf_line_str_buffer;
207 /* Length of the loaded .debug_line_str section. */
208 bfd_size_type dwarf_line_str_size;
210 /* Pointer to the .debug_ranges section loaded into memory. */
211 bfd_byte *dwarf_ranges_buffer;
213 /* Length of the loaded .debug_ranges section. */
214 bfd_size_type dwarf_ranges_size;
216 /* Pointer to the .debug_rnglists section loaded into memory. */
217 bfd_byte *dwarf_rnglists_buffer;
219 /* Length of the loaded .debug_rnglists section. */
220 bfd_size_type dwarf_rnglists_size;
222 /* A list of all previously read comp_units. */
223 struct comp_unit *all_comp_units;
225 /* A list of all previously read comp_units with no ranges (yet). */
226 struct comp_unit *all_comp_units_without_ranges;
228 /* Last comp unit in list above. */
229 struct comp_unit *last_comp_unit;
231 /* Line table at line_offset zero. */
232 struct line_info_table *line_table;
234 /* Hash table to map offsets to decoded abbrevs. */
235 htab_t abbrev_offsets;
237 /* Root of a trie to map addresses to compilation units. */
238 struct trie_node *trie_root;
241 struct dwarf2_debug
243 /* Names of the debug sections. */
244 const struct dwarf_debug_section *debug_sections;
246 /* Per-file stuff. */
247 struct dwarf2_debug_file f, alt;
249 /* Pointer to the original bfd for which debug was loaded. This is what
250 we use to compare and so check that the cached debug data is still
251 valid - it saves having to possibly dereference the gnu_debuglink each
252 time. */
253 bfd *orig_bfd;
255 /* If the most recent call to bfd_find_nearest_line was given an
256 address in an inlined function, preserve a pointer into the
257 calling chain for subsequent calls to bfd_find_inliner_info to
258 use. */
259 struct funcinfo *inliner_chain;
261 /* Section VMAs at the time the stash was built. */
262 bfd_vma *sec_vma;
263 /* Number of sections in the SEC_VMA table. */
264 unsigned int sec_vma_count;
266 /* Number of sections whose VMA we must adjust. */
267 int adjusted_section_count;
269 /* Array of sections with adjusted VMA. */
270 struct adjusted_section *adjusted_sections;
272 /* Number of times find_line is called. This is used in
273 the heuristic for enabling the info hash tables. */
274 int info_hash_count;
276 #define STASH_INFO_HASH_TRIGGER 100
278 /* Hash table mapping symbol names to function infos. */
279 struct info_hash_table *funcinfo_hash_table;
281 /* Hash table mapping symbol names to variable infos. */
282 struct info_hash_table *varinfo_hash_table;
284 /* Head of comp_unit list in the last hash table update. */
285 struct comp_unit *hash_units_head;
287 /* Status of info hash. */
288 int info_hash_status;
289 #define STASH_INFO_HASH_OFF 0
290 #define STASH_INFO_HASH_ON 1
291 #define STASH_INFO_HASH_DISABLED 2
293 /* True if we opened bfd_ptr. */
294 bool close_on_cleanup;
297 struct arange
299 struct arange *next;
300 bfd_vma low;
301 bfd_vma high;
304 /* A minimal decoding of DWARF2 compilation units. We only decode
305 what's needed to get to the line number information. */
307 struct comp_unit
309 /* Chain the previously read compilation units. */
310 struct comp_unit *next_unit;
312 /* Chain the previously read compilation units that have no ranges yet.
313 We scan these separately when we have a trie over the ranges.
314 Unused if arange.high != 0. */
315 struct comp_unit *next_unit_without_ranges;
317 /* Likewise, chain the compilation unit read after this one.
318 The comp units are stored in reversed reading order. */
319 struct comp_unit *prev_unit;
321 /* Keep the bfd convenient (for memory allocation). */
322 bfd *abfd;
324 /* The lowest and highest addresses contained in this compilation
325 unit as specified in the compilation unit header. */
326 struct arange arange;
328 /* The DW_AT_name attribute (for error messages). */
329 char *name;
331 /* The abbrev hash table. */
332 struct abbrev_info **abbrevs;
334 /* DW_AT_language. */
335 int lang;
337 /* Note that an error was found by comp_unit_find_nearest_line. */
338 int error;
340 /* The DW_AT_comp_dir attribute. */
341 char *comp_dir;
343 /* TRUE if there is a line number table associated with this comp. unit. */
344 int stmtlist;
346 /* Pointer to the current comp_unit so that we can find a given entry
347 by its reference. */
348 bfd_byte *info_ptr_unit;
350 /* The offset into .debug_line of the line number table. */
351 unsigned long line_offset;
353 /* Pointer to the first child die for the comp unit. */
354 bfd_byte *first_child_die_ptr;
356 /* The end of the comp unit. */
357 bfd_byte *end_ptr;
359 /* The decoded line number, NULL if not yet decoded. */
360 struct line_info_table *line_table;
362 /* A list of the functions found in this comp. unit. */
363 struct funcinfo *function_table;
365 /* A table of function information references searchable by address. */
366 struct lookup_funcinfo *lookup_funcinfo_table;
368 /* Number of functions in the function_table and sorted_function_table. */
369 bfd_size_type number_of_functions;
371 /* A list of the variables found in this comp. unit. */
372 struct varinfo *variable_table;
374 /* Pointers to dwarf2_debug structures. */
375 struct dwarf2_debug *stash;
376 struct dwarf2_debug_file *file;
378 /* DWARF format version for this unit - from unit header. */
379 int version;
381 /* Address size for this unit - from unit header. */
382 unsigned char addr_size;
384 /* Offset size for this unit - from unit header. */
385 unsigned char offset_size;
387 /* Base address for this unit - from DW_AT_low_pc attribute of
388 DW_TAG_compile_unit DIE */
389 bfd_vma base_address;
391 /* TRUE if symbols are cached in hash table for faster lookup by name. */
392 bool cached;
394 /* Used when iterating over trie leaves to know which units we have
395 already seen in this iteration. */
396 bool mark;
398 /* Base address of debug_addr section. */
399 size_t dwarf_addr_offset;
401 /* Base address of string offset table. */
402 size_t dwarf_str_offset;
405 /* This data structure holds the information of an abbrev. */
406 struct abbrev_info
408 unsigned int number; /* Number identifying abbrev. */
409 enum dwarf_tag tag; /* DWARF tag. */
410 bool has_children; /* TRUE if the abbrev has children. */
411 unsigned int num_attrs; /* Number of attributes. */
412 struct attr_abbrev * attrs; /* An array of attribute descriptions. */
413 struct abbrev_info * next; /* Next in chain. */
416 struct attr_abbrev
418 enum dwarf_attribute name;
419 enum dwarf_form form;
420 bfd_vma implicit_const;
423 /* Map of uncompressed DWARF debug section name to compressed one. It
424 is terminated by NULL uncompressed_name. */
426 const struct dwarf_debug_section dwarf_debug_sections[] =
428 { ".debug_abbrev", ".zdebug_abbrev" },
429 { ".debug_aranges", ".zdebug_aranges" },
430 { ".debug_frame", ".zdebug_frame" },
431 { ".debug_info", ".zdebug_info" },
432 { ".debug_info", ".zdebug_info" },
433 { ".debug_line", ".zdebug_line" },
434 { ".debug_loc", ".zdebug_loc" },
435 { ".debug_macinfo", ".zdebug_macinfo" },
436 { ".debug_macro", ".zdebug_macro" },
437 { ".debug_pubnames", ".zdebug_pubnames" },
438 { ".debug_pubtypes", ".zdebug_pubtypes" },
439 { ".debug_ranges", ".zdebug_ranges" },
440 { ".debug_rnglists", ".zdebug_rnglist" },
441 { ".debug_static_func", ".zdebug_static_func" },
442 { ".debug_static_vars", ".zdebug_static_vars" },
443 { ".debug_str", ".zdebug_str", },
444 { ".debug_str", ".zdebug_str", },
445 { ".debug_str_offsets", ".zdebug_str_offsets", },
446 { ".debug_addr", ".zdebug_addr", },
447 { ".debug_line_str", ".zdebug_line_str", },
448 { ".debug_types", ".zdebug_types" },
449 /* GNU DWARF 1 extensions */
450 { ".debug_sfnames", ".zdebug_sfnames" },
451 { ".debug_srcinfo", ".zebug_srcinfo" },
452 /* SGI/MIPS DWARF 2 extensions */
453 { ".debug_funcnames", ".zdebug_funcnames" },
454 { ".debug_typenames", ".zdebug_typenames" },
455 { ".debug_varnames", ".zdebug_varnames" },
456 { ".debug_weaknames", ".zdebug_weaknames" },
457 { NULL, NULL },
460 /* NB/ Numbers in this enum must match up with indices
461 into the dwarf_debug_sections[] array above. */
462 enum dwarf_debug_section_enum
464 debug_abbrev = 0,
465 debug_aranges,
466 debug_frame,
467 debug_info,
468 debug_info_alt,
469 debug_line,
470 debug_loc,
471 debug_macinfo,
472 debug_macro,
473 debug_pubnames,
474 debug_pubtypes,
475 debug_ranges,
476 debug_rnglists,
477 debug_static_func,
478 debug_static_vars,
479 debug_str,
480 debug_str_alt,
481 debug_str_offsets,
482 debug_addr,
483 debug_line_str,
484 debug_types,
485 debug_sfnames,
486 debug_srcinfo,
487 debug_funcnames,
488 debug_typenames,
489 debug_varnames,
490 debug_weaknames,
491 debug_max
494 /* A static assertion. */
495 extern int dwarf_debug_section_assert[ARRAY_SIZE (dwarf_debug_sections)
496 == debug_max + 1 ? 1 : -1];
498 #ifndef ABBREV_HASH_SIZE
499 #define ABBREV_HASH_SIZE 121
500 #endif
501 #ifndef ATTR_ALLOC_CHUNK
502 #define ATTR_ALLOC_CHUNK 4
503 #endif
505 /* Variable and function hash tables. This is used to speed up look-up
506 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
507 In order to share code between variable and function infos, we use
508 a list of untyped pointer for all variable/function info associated with
509 a symbol. We waste a bit of memory for list with one node but that
510 simplifies the code. */
512 struct info_list_node
514 struct info_list_node *next;
515 void *info;
518 /* Info hash entry. */
519 struct info_hash_entry
521 struct bfd_hash_entry root;
522 struct info_list_node *head;
525 struct info_hash_table
527 struct bfd_hash_table base;
530 /* Function to create a new entry in info hash table. */
532 static struct bfd_hash_entry *
533 info_hash_table_newfunc (struct bfd_hash_entry *entry,
534 struct bfd_hash_table *table,
535 const char *string)
537 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
539 /* Allocate the structure if it has not already been allocated by a
540 derived class. */
541 if (ret == NULL)
543 ret = (struct info_hash_entry *) bfd_hash_allocate (table,
544 sizeof (* ret));
545 if (ret == NULL)
546 return NULL;
549 /* Call the allocation method of the base class. */
550 ret = ((struct info_hash_entry *)
551 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
553 /* Initialize the local fields here. */
554 if (ret)
555 ret->head = NULL;
557 return (struct bfd_hash_entry *) ret;
560 /* Function to create a new info hash table. It returns a pointer to the
561 newly created table or NULL if there is any error. We need abfd
562 solely for memory allocation. */
564 static struct info_hash_table *
565 create_info_hash_table (bfd *abfd)
567 struct info_hash_table *hash_table;
569 hash_table = ((struct info_hash_table *)
570 bfd_alloc (abfd, sizeof (struct info_hash_table)));
571 if (!hash_table)
572 return hash_table;
574 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
575 sizeof (struct info_hash_entry)))
577 bfd_release (abfd, hash_table);
578 return NULL;
581 return hash_table;
584 /* Insert an info entry into an info hash table. We do not check of
585 duplicate entries. Also, the caller need to guarantee that the
586 right type of info in inserted as info is passed as a void* pointer.
587 This function returns true if there is no error. */
589 static bool
590 insert_info_hash_table (struct info_hash_table *hash_table,
591 const char *key,
592 void *info,
593 bool copy_p)
595 struct info_hash_entry *entry;
596 struct info_list_node *node;
598 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
599 key, true, copy_p);
600 if (!entry)
601 return false;
603 node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
604 sizeof (*node));
605 if (!node)
606 return false;
608 node->info = info;
609 node->next = entry->head;
610 entry->head = node;
612 return true;
615 /* Look up an info entry list from an info hash table. Return NULL
616 if there is none. */
618 static struct info_list_node *
619 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
621 struct info_hash_entry *entry;
623 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
624 false, false);
625 return entry ? entry->head : NULL;
628 /* Read a section into its appropriate place in the dwarf2_debug
629 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
630 not NULL, use bfd_simple_get_relocated_section_contents to read the
631 section contents, otherwise use bfd_get_section_contents. Fail if
632 the located section does not contain at least OFFSET bytes. */
634 static bool
635 read_section (bfd *abfd,
636 const struct dwarf_debug_section *sec,
637 asymbol **syms,
638 uint64_t offset,
639 bfd_byte **section_buffer,
640 bfd_size_type *section_size)
642 const char *section_name = sec->uncompressed_name;
643 bfd_byte *contents = *section_buffer;
645 /* The section may have already been read. */
646 if (contents == NULL)
648 bfd_size_type amt;
649 asection *msec;
650 ufile_ptr filesize;
652 msec = bfd_get_section_by_name (abfd, section_name);
653 if (msec == NULL)
655 section_name = sec->compressed_name;
656 msec = bfd_get_section_by_name (abfd, section_name);
658 if (msec == NULL)
660 _bfd_error_handler (_("DWARF error: can't find %s section."),
661 sec->uncompressed_name);
662 bfd_set_error (bfd_error_bad_value);
663 return false;
666 amt = bfd_get_section_limit_octets (abfd, msec);
667 filesize = bfd_get_file_size (abfd);
668 /* PR 28834: A compressed debug section could well decompress to a size
669 larger than the file, so we choose an arbitrary modifier of 10x in
670 the test below. If this ever turns out to be insufficient, it can
671 be changed by a future update. */
672 if (amt >= filesize * 10)
674 /* PR 26946 */
675 _bfd_error_handler (_("DWARF error: section %s is larger than 10x its filesize! (0x%lx vs 0x%lx)"),
676 section_name, (long) amt, (long) filesize);
677 bfd_set_error (bfd_error_bad_value);
678 return false;
680 *section_size = amt;
681 /* Paranoia - alloc one extra so that we can make sure a string
682 section is NUL terminated. */
683 amt += 1;
684 if (amt == 0)
686 /* Paranoia - this should never happen. */
687 bfd_set_error (bfd_error_no_memory);
688 return false;
690 contents = (bfd_byte *) bfd_malloc (amt);
691 if (contents == NULL)
692 return false;
693 if (syms
694 ? !bfd_simple_get_relocated_section_contents (abfd, msec, contents,
695 syms)
696 : !bfd_get_section_contents (abfd, msec, contents, 0, *section_size))
698 free (contents);
699 return false;
701 contents[*section_size] = 0;
702 *section_buffer = contents;
705 /* It is possible to get a bad value for the offset into the section
706 that the client wants. Validate it here to avoid trouble later. */
707 if (offset != 0 && offset >= *section_size)
709 /* xgettext: c-format */
710 _bfd_error_handler (_("DWARF error: offset (%" PRIu64 ")"
711 " greater than or equal to %s size (%" PRIu64 ")"),
712 (uint64_t) offset, section_name,
713 (uint64_t) *section_size);
714 bfd_set_error (bfd_error_bad_value);
715 return false;
718 return true;
721 /* Read dwarf information from a buffer. */
723 static inline uint64_t
724 read_n_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end, int n)
726 bfd_byte *buf = *ptr;
727 if (end - buf < n)
729 *ptr = end;
730 return 0;
732 *ptr = buf + n;
733 return bfd_get (n * 8, abfd, buf);
736 static unsigned int
737 read_1_byte (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
739 return read_n_bytes (abfd, ptr, end, 1);
742 static int
743 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte **ptr, bfd_byte *end)
745 bfd_byte *buf = *ptr;
746 if (end - buf < 1)
748 *ptr = end;
749 return 0;
751 *ptr = buf + 1;
752 return bfd_get_signed_8 (abfd, buf);
755 static unsigned int
756 read_2_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
758 return read_n_bytes (abfd, ptr, end, 2);
761 static unsigned int
762 read_3_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
764 unsigned int val = read_1_byte (abfd, ptr, end);
765 val <<= 8;
766 val |= read_1_byte (abfd, ptr, end);
767 val <<= 8;
768 val |= read_1_byte (abfd, ptr, end);
769 if (bfd_little_endian (abfd))
770 val = (((val >> 16) & 0xff)
771 | (val & 0xff00)
772 | ((val & 0xff) << 16));
773 return val;
776 static unsigned int
777 read_4_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
779 return read_n_bytes (abfd, ptr, end, 4);
782 static uint64_t
783 read_8_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
785 return read_n_bytes (abfd, ptr, end, 8);
788 static struct dwarf_block *
789 read_blk (bfd *abfd, bfd_byte **ptr, bfd_byte *end, size_t size)
791 bfd_byte *buf = *ptr;
792 struct dwarf_block *block;
794 block = (struct dwarf_block *) bfd_alloc (abfd, sizeof (*block));
795 if (block == NULL)
796 return NULL;
798 if (size > (size_t) (end - buf))
800 *ptr = end;
801 block->data = NULL;
802 block->size = 0;
804 else
806 *ptr = buf + size;
807 block->data = buf;
808 block->size = size;
810 return block;
813 /* Scans a NUL terminated string starting at *PTR, returning a pointer to it.
814 Bytes at or beyond BUF_END will not be read. Returns NULL if the
815 terminator is not found or if the string is empty. *PTR is
816 incremented over the bytes scanned, including the terminator. */
818 static char *
819 read_string (bfd_byte **ptr,
820 bfd_byte *buf_end)
822 bfd_byte *buf = *ptr;
823 bfd_byte *str = buf;
825 while (buf < buf_end)
826 if (*buf++ == 0)
828 if (str == buf - 1)
829 break;
830 *ptr = buf;
831 return (char *) str;
834 *ptr = buf;
835 return NULL;
838 /* Reads an offset from *PTR and then locates the string at this offset
839 inside the debug string section. Returns a pointer to the string.
840 Increments *PTR by the number of bytes read for the offset. This
841 value is set even if the function fails. Bytes at or beyond
842 BUF_END will not be read. Returns NULL if there was a problem, or
843 if the string is empty. Does not check for NUL termination of the
844 string. */
846 static char *
847 read_indirect_string (struct comp_unit *unit,
848 bfd_byte **ptr,
849 bfd_byte *buf_end)
851 uint64_t offset;
852 struct dwarf2_debug *stash = unit->stash;
853 struct dwarf2_debug_file *file = unit->file;
854 char *str;
856 if (unit->offset_size > (size_t) (buf_end - *ptr))
858 *ptr = buf_end;
859 return NULL;
862 if (unit->offset_size == 4)
863 offset = read_4_bytes (unit->abfd, ptr, buf_end);
864 else
865 offset = read_8_bytes (unit->abfd, ptr, buf_end);
867 if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
868 file->syms, offset,
869 &file->dwarf_str_buffer, &file->dwarf_str_size))
870 return NULL;
872 str = (char *) file->dwarf_str_buffer + offset;
873 if (*str == '\0')
874 return NULL;
875 return str;
878 /* Like read_indirect_string but from .debug_line_str section. */
880 static char *
881 read_indirect_line_string (struct comp_unit *unit,
882 bfd_byte **ptr,
883 bfd_byte *buf_end)
885 uint64_t offset;
886 struct dwarf2_debug *stash = unit->stash;
887 struct dwarf2_debug_file *file = unit->file;
888 char *str;
890 if (unit->offset_size > (size_t) (buf_end - *ptr))
892 *ptr = buf_end;
893 return NULL;
896 if (unit->offset_size == 4)
897 offset = read_4_bytes (unit->abfd, ptr, buf_end);
898 else
899 offset = read_8_bytes (unit->abfd, ptr, buf_end);
901 if (! read_section (unit->abfd, &stash->debug_sections[debug_line_str],
902 file->syms, offset,
903 &file->dwarf_line_str_buffer,
904 &file->dwarf_line_str_size))
905 return NULL;
907 str = (char *) file->dwarf_line_str_buffer + offset;
908 if (*str == '\0')
909 return NULL;
910 return str;
913 /* Like read_indirect_string but uses a .debug_str located in
914 an alternate file pointed to by the .gnu_debugaltlink section.
915 Used to impement DW_FORM_GNU_strp_alt. */
917 static char *
918 read_alt_indirect_string (struct comp_unit *unit,
919 bfd_byte **ptr,
920 bfd_byte *buf_end)
922 uint64_t offset;
923 struct dwarf2_debug *stash = unit->stash;
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 (stash->alt.bfd_ptr == NULL)
939 bfd *debug_bfd;
940 char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
942 if (debug_filename == NULL)
943 return NULL;
945 debug_bfd = bfd_openr (debug_filename, NULL);
946 free (debug_filename);
947 if (debug_bfd == NULL)
948 /* FIXME: Should we report our failure to follow the debuglink ? */
949 return NULL;
951 if (!bfd_check_format (debug_bfd, bfd_object))
953 bfd_close (debug_bfd);
954 return NULL;
956 stash->alt.bfd_ptr = debug_bfd;
959 if (! read_section (unit->stash->alt.bfd_ptr,
960 stash->debug_sections + debug_str_alt,
961 stash->alt.syms, offset,
962 &stash->alt.dwarf_str_buffer,
963 &stash->alt.dwarf_str_size))
964 return NULL;
966 str = (char *) stash->alt.dwarf_str_buffer + offset;
967 if (*str == '\0')
968 return NULL;
970 return str;
973 /* Resolve an alternate reference from UNIT at OFFSET.
974 Returns a pointer into the loaded alternate CU upon success
975 or NULL upon failure. */
977 static bfd_byte *
978 read_alt_indirect_ref (struct comp_unit *unit, uint64_t offset)
980 struct dwarf2_debug *stash = unit->stash;
982 if (stash->alt.bfd_ptr == NULL)
984 bfd *debug_bfd;
985 char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
987 if (debug_filename == NULL)
988 return NULL;
990 debug_bfd = bfd_openr (debug_filename, NULL);
991 free (debug_filename);
992 if (debug_bfd == NULL)
993 /* FIXME: Should we report our failure to follow the debuglink ? */
994 return NULL;
996 if (!bfd_check_format (debug_bfd, bfd_object))
998 bfd_close (debug_bfd);
999 return NULL;
1001 stash->alt.bfd_ptr = debug_bfd;
1004 if (! read_section (unit->stash->alt.bfd_ptr,
1005 stash->debug_sections + debug_info_alt,
1006 stash->alt.syms, offset,
1007 &stash->alt.dwarf_info_buffer,
1008 &stash->alt.dwarf_info_size))
1009 return NULL;
1011 return stash->alt.dwarf_info_buffer + offset;
1014 static uint64_t
1015 read_address (struct comp_unit *unit, bfd_byte **ptr, bfd_byte *buf_end)
1017 bfd_byte *buf = *ptr;
1018 int signed_vma = 0;
1020 if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour)
1021 signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
1023 if (unit->addr_size > (size_t) (buf_end - buf))
1025 *ptr = buf_end;
1026 return 0;
1029 *ptr = buf + unit->addr_size;
1030 if (signed_vma)
1032 switch (unit->addr_size)
1034 case 8:
1035 return bfd_get_signed_64 (unit->abfd, buf);
1036 case 4:
1037 return bfd_get_signed_32 (unit->abfd, buf);
1038 case 2:
1039 return bfd_get_signed_16 (unit->abfd, buf);
1040 default:
1041 abort ();
1044 else
1046 switch (unit->addr_size)
1048 case 8:
1049 return bfd_get_64 (unit->abfd, buf);
1050 case 4:
1051 return bfd_get_32 (unit->abfd, buf);
1052 case 2:
1053 return bfd_get_16 (unit->abfd, buf);
1054 default:
1055 abort ();
1060 /* Lookup an abbrev_info structure in the abbrev hash table. */
1062 static struct abbrev_info *
1063 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
1065 unsigned int hash_number;
1066 struct abbrev_info *abbrev;
1068 hash_number = number % ABBREV_HASH_SIZE;
1069 abbrev = abbrevs[hash_number];
1071 while (abbrev)
1073 if (abbrev->number == number)
1074 return abbrev;
1075 else
1076 abbrev = abbrev->next;
1079 return NULL;
1082 /* We keep a hash table to map .debug_abbrev section offsets to the
1083 array of abbrevs, so that compilation units using the same set of
1084 abbrevs do not waste memory. */
1086 struct abbrev_offset_entry
1088 size_t offset;
1089 struct abbrev_info **abbrevs;
1092 static hashval_t
1093 hash_abbrev (const void *p)
1095 const struct abbrev_offset_entry *ent = p;
1096 return htab_hash_pointer ((void *) ent->offset);
1099 static int
1100 eq_abbrev (const void *pa, const void *pb)
1102 const struct abbrev_offset_entry *a = pa;
1103 const struct abbrev_offset_entry *b = pb;
1104 return a->offset == b->offset;
1107 static void
1108 del_abbrev (void *p)
1110 struct abbrev_offset_entry *ent = p;
1111 struct abbrev_info **abbrevs = ent->abbrevs;
1112 size_t i;
1114 for (i = 0; i < ABBREV_HASH_SIZE; i++)
1116 struct abbrev_info *abbrev = abbrevs[i];
1118 while (abbrev)
1120 free (abbrev->attrs);
1121 abbrev = abbrev->next;
1124 free (ent);
1127 /* In DWARF version 2, the description of the debugging information is
1128 stored in a separate .debug_abbrev section. Before we read any
1129 dies from a section we read in all abbreviations and install them
1130 in a hash table. */
1132 static struct abbrev_info**
1133 read_abbrevs (bfd *abfd, uint64_t offset, struct dwarf2_debug *stash,
1134 struct dwarf2_debug_file *file)
1136 struct abbrev_info **abbrevs;
1137 bfd_byte *abbrev_ptr;
1138 bfd_byte *abbrev_end;
1139 struct abbrev_info *cur_abbrev;
1140 unsigned int abbrev_number, abbrev_name;
1141 unsigned int abbrev_form, hash_number;
1142 size_t amt;
1143 void **slot;
1144 struct abbrev_offset_entry ent = { offset, NULL };
1146 if (ent.offset != offset)
1147 return NULL;
1149 slot = htab_find_slot (file->abbrev_offsets, &ent, INSERT);
1150 if (slot == NULL)
1151 return NULL;
1152 if (*slot != NULL)
1153 return ((struct abbrev_offset_entry *) (*slot))->abbrevs;
1155 if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
1156 file->syms, offset,
1157 &file->dwarf_abbrev_buffer,
1158 &file->dwarf_abbrev_size))
1159 return NULL;
1161 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
1162 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
1163 if (abbrevs == NULL)
1164 return NULL;
1166 abbrev_ptr = file->dwarf_abbrev_buffer + offset;
1167 abbrev_end = file->dwarf_abbrev_buffer + file->dwarf_abbrev_size;
1168 abbrev_number = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1169 false, abbrev_end);
1171 /* Loop until we reach an abbrev number of 0. */
1172 while (abbrev_number)
1174 amt = sizeof (struct abbrev_info);
1175 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
1176 if (cur_abbrev == NULL)
1177 goto fail;
1179 /* Read in abbrev header. */
1180 cur_abbrev->number = abbrev_number;
1181 cur_abbrev->tag = (enum dwarf_tag)
1182 _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1183 false, abbrev_end);
1184 cur_abbrev->has_children = read_1_byte (abfd, &abbrev_ptr, abbrev_end);
1186 /* Now read in declarations. */
1187 for (;;)
1189 /* Initialize it just to avoid a GCC false warning. */
1190 bfd_vma implicit_const = -1;
1192 abbrev_name = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1193 false, abbrev_end);
1194 abbrev_form = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1195 false, abbrev_end);
1196 if (abbrev_form == DW_FORM_implicit_const)
1197 implicit_const = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1198 true, abbrev_end);
1199 if (abbrev_name == 0)
1200 break;
1202 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
1204 struct attr_abbrev *tmp;
1206 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
1207 amt *= sizeof (struct attr_abbrev);
1208 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
1209 if (tmp == NULL)
1210 goto fail;
1211 cur_abbrev->attrs = tmp;
1214 cur_abbrev->attrs[cur_abbrev->num_attrs].name
1215 = (enum dwarf_attribute) abbrev_name;
1216 cur_abbrev->attrs[cur_abbrev->num_attrs].form
1217 = (enum dwarf_form) abbrev_form;
1218 cur_abbrev->attrs[cur_abbrev->num_attrs].implicit_const
1219 = implicit_const;
1220 ++cur_abbrev->num_attrs;
1223 hash_number = abbrev_number % ABBREV_HASH_SIZE;
1224 cur_abbrev->next = abbrevs[hash_number];
1225 abbrevs[hash_number] = cur_abbrev;
1227 /* Get next abbreviation.
1228 Under Irix6 the abbreviations for a compilation unit are not
1229 always properly terminated with an abbrev number of 0.
1230 Exit loop if we encounter an abbreviation which we have
1231 already read (which means we are about to read the abbreviations
1232 for the next compile unit) or if the end of the abbreviation
1233 table is reached. */
1234 if ((size_t) (abbrev_ptr - file->dwarf_abbrev_buffer)
1235 >= file->dwarf_abbrev_size)
1236 break;
1237 abbrev_number = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1238 false, abbrev_end);
1239 if (lookup_abbrev (abbrev_number, abbrevs) != NULL)
1240 break;
1243 *slot = bfd_malloc (sizeof ent);
1244 if (!*slot)
1245 goto fail;
1246 ent.abbrevs = abbrevs;
1247 memcpy (*slot, &ent, sizeof ent);
1248 return abbrevs;
1250 fail:
1251 if (abbrevs != NULL)
1253 size_t i;
1255 for (i = 0; i < ABBREV_HASH_SIZE; i++)
1257 struct abbrev_info *abbrev = abbrevs[i];
1259 while (abbrev)
1261 free (abbrev->attrs);
1262 abbrev = abbrev->next;
1265 free (abbrevs);
1267 return NULL;
1270 /* Returns true if the form is one which has a string value. */
1272 static bool
1273 is_str_form (const struct attribute *attr)
1275 switch (attr->form)
1277 case DW_FORM_string:
1278 case DW_FORM_strp:
1279 case DW_FORM_strx:
1280 case DW_FORM_strx1:
1281 case DW_FORM_strx2:
1282 case DW_FORM_strx3:
1283 case DW_FORM_strx4:
1284 case DW_FORM_line_strp:
1285 case DW_FORM_GNU_strp_alt:
1286 return true;
1288 default:
1289 return false;
1293 /* Returns true if the form is one which has an integer value. */
1295 static bool
1296 is_int_form (const struct attribute *attr)
1298 switch (attr->form)
1300 case DW_FORM_addr:
1301 case DW_FORM_data2:
1302 case DW_FORM_data4:
1303 case DW_FORM_data8:
1304 case DW_FORM_data1:
1305 case DW_FORM_flag:
1306 case DW_FORM_sdata:
1307 case DW_FORM_udata:
1308 case DW_FORM_ref_addr:
1309 case DW_FORM_ref1:
1310 case DW_FORM_ref2:
1311 case DW_FORM_ref4:
1312 case DW_FORM_ref8:
1313 case DW_FORM_ref_udata:
1314 case DW_FORM_sec_offset:
1315 case DW_FORM_flag_present:
1316 case DW_FORM_ref_sig8:
1317 case DW_FORM_addrx:
1318 case DW_FORM_implicit_const:
1319 case DW_FORM_addrx1:
1320 case DW_FORM_addrx2:
1321 case DW_FORM_addrx3:
1322 case DW_FORM_addrx4:
1323 case DW_FORM_GNU_ref_alt:
1324 return true;
1326 default:
1327 return false;
1331 /* Returns true if the form is strx[1-4]. */
1333 static inline bool
1334 is_strx_form (enum dwarf_form form)
1336 return (form == DW_FORM_strx
1337 || form == DW_FORM_strx1
1338 || form == DW_FORM_strx2
1339 || form == DW_FORM_strx3
1340 || form == DW_FORM_strx4);
1343 /* Return true if the form is addrx[1-4]. */
1345 static inline bool
1346 is_addrx_form (enum dwarf_form form)
1348 return (form == DW_FORM_addrx
1349 || form == DW_FORM_addrx1
1350 || form == DW_FORM_addrx2
1351 || form == DW_FORM_addrx3
1352 || form == DW_FORM_addrx4);
1355 /* Returns the address in .debug_addr section using DW_AT_addr_base.
1356 Used to implement DW_FORM_addrx*. */
1357 static bfd_vma
1358 read_indexed_address (uint64_t idx, struct comp_unit *unit)
1360 struct dwarf2_debug *stash = unit->stash;
1361 struct dwarf2_debug_file *file = unit->file;
1362 size_t addr_base = unit->dwarf_addr_offset;
1363 bfd_byte *info_ptr;
1365 if (stash == NULL)
1366 return 0;
1368 if (!read_section (unit->abfd, &stash->debug_sections[debug_addr],
1369 file->syms, 0,
1370 &file->dwarf_addr_buffer, &file->dwarf_addr_size))
1371 return 0;
1373 info_ptr = file->dwarf_addr_buffer + addr_base + idx * unit->offset_size;
1375 if (unit->offset_size == 4)
1376 return bfd_get_32 (unit->abfd, info_ptr);
1377 else
1378 return bfd_get_64 (unit->abfd, info_ptr);
1381 /* Returns the string using DW_AT_str_offsets_base.
1382 Used to implement DW_FORM_strx*. */
1383 static const char *
1384 read_indexed_string (uint64_t idx, struct comp_unit *unit)
1386 struct dwarf2_debug *stash = unit->stash;
1387 struct dwarf2_debug_file *file = unit->file;
1388 bfd_byte *info_ptr;
1389 unsigned long str_offset;
1391 if (stash == NULL)
1392 return NULL;
1394 if (!read_section (unit->abfd, &stash->debug_sections[debug_str],
1395 file->syms, 0,
1396 &file->dwarf_str_buffer, &file->dwarf_str_size))
1397 return NULL;
1399 if (!read_section (unit->abfd, &stash->debug_sections[debug_str_offsets],
1400 file->syms, 0,
1401 &file->dwarf_str_offsets_buffer,
1402 &file->dwarf_str_offsets_size))
1403 return NULL;
1405 info_ptr = (file->dwarf_str_offsets_buffer
1406 + unit->dwarf_str_offset
1407 + idx * unit->offset_size);
1409 if (unit->offset_size == 4)
1410 str_offset = bfd_get_32 (unit->abfd, info_ptr);
1411 else
1412 str_offset = bfd_get_64 (unit->abfd, info_ptr);
1414 return (const char *) file->dwarf_str_buffer + str_offset;
1417 /* Read and fill in the value of attribute ATTR as described by FORM.
1418 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1419 Returns an updated INFO_PTR taking into account the amount of data read. */
1421 static bfd_byte *
1422 read_attribute_value (struct attribute * attr,
1423 unsigned form,
1424 bfd_vma implicit_const,
1425 struct comp_unit * unit,
1426 bfd_byte * info_ptr,
1427 bfd_byte * info_ptr_end)
1429 bfd *abfd = unit->abfd;
1430 size_t amt;
1432 if (info_ptr >= info_ptr_end && form != DW_FORM_flag_present)
1434 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1435 bfd_set_error (bfd_error_bad_value);
1436 return NULL;
1439 attr->form = (enum dwarf_form) form;
1441 switch (form)
1443 case DW_FORM_flag_present:
1444 attr->u.val = 1;
1445 break;
1446 case DW_FORM_ref_addr:
1447 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1448 DWARF3. */
1449 if (unit->version >= 3)
1451 if (unit->offset_size == 4)
1452 attr->u.val = read_4_bytes (unit->abfd, &info_ptr, info_ptr_end);
1453 else
1454 attr->u.val = read_8_bytes (unit->abfd, &info_ptr, info_ptr_end);
1455 break;
1457 /* FALLTHROUGH */
1458 case DW_FORM_addr:
1459 attr->u.val = read_address (unit, &info_ptr, info_ptr_end);
1460 break;
1461 case DW_FORM_GNU_ref_alt:
1462 case DW_FORM_sec_offset:
1463 if (unit->offset_size == 4)
1464 attr->u.val = read_4_bytes (unit->abfd, &info_ptr, info_ptr_end);
1465 else
1466 attr->u.val = read_8_bytes (unit->abfd, &info_ptr, info_ptr_end);
1467 break;
1468 case DW_FORM_block2:
1469 amt = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1470 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1471 if (attr->u.blk == NULL)
1472 return NULL;
1473 break;
1474 case DW_FORM_block4:
1475 amt = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1476 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1477 if (attr->u.blk == NULL)
1478 return NULL;
1479 break;
1480 case DW_FORM_ref1:
1481 case DW_FORM_flag:
1482 case DW_FORM_data1:
1483 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1484 break;
1485 case DW_FORM_addrx1:
1486 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1487 /* dwarf_addr_offset value 0 indicates the attribute DW_AT_addr_base
1488 is not yet read. */
1489 if (unit->dwarf_addr_offset != 0)
1490 attr->u.val = read_indexed_address (attr->u.val, unit);
1491 break;
1492 case DW_FORM_data2:
1493 case DW_FORM_ref2:
1494 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1495 break;
1496 case DW_FORM_addrx2:
1497 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1498 if (unit->dwarf_addr_offset != 0)
1499 attr->u.val = read_indexed_address (attr->u.val, unit);
1500 break;
1501 case DW_FORM_addrx3:
1502 attr->u.val = read_3_bytes (abfd, &info_ptr, info_ptr_end);
1503 if (unit->dwarf_addr_offset != 0)
1504 attr->u.val = read_indexed_address(attr->u.val, unit);
1505 break;
1506 case DW_FORM_ref4:
1507 case DW_FORM_data4:
1508 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1509 break;
1510 case DW_FORM_addrx4:
1511 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1512 if (unit->dwarf_addr_offset != 0)
1513 attr->u.val = read_indexed_address (attr->u.val, unit);
1514 break;
1515 case DW_FORM_data8:
1516 case DW_FORM_ref8:
1517 case DW_FORM_ref_sig8:
1518 attr->u.val = read_8_bytes (abfd, &info_ptr, info_ptr_end);
1519 break;
1520 case DW_FORM_string:
1521 attr->u.str = read_string (&info_ptr, info_ptr_end);
1522 break;
1523 case DW_FORM_strp:
1524 attr->u.str = read_indirect_string (unit, &info_ptr, info_ptr_end);
1525 break;
1526 case DW_FORM_line_strp:
1527 attr->u.str = read_indirect_line_string (unit, &info_ptr, info_ptr_end);
1528 break;
1529 case DW_FORM_GNU_strp_alt:
1530 attr->u.str = read_alt_indirect_string (unit, &info_ptr, info_ptr_end);
1531 break;
1532 case DW_FORM_strx1:
1533 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1534 /* dwarf_str_offset value 0 indicates the attribute DW_AT_str_offsets_base
1535 is not yet read. */
1536 if (unit->dwarf_str_offset != 0)
1537 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1538 break;
1539 case DW_FORM_strx2:
1540 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1541 if (unit->dwarf_str_offset != 0)
1542 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1543 break;
1544 case DW_FORM_strx3:
1545 attr->u.val = read_3_bytes (abfd, &info_ptr, info_ptr_end);
1546 if (unit->dwarf_str_offset != 0)
1547 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1548 break;
1549 case DW_FORM_strx4:
1550 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1551 if (unit->dwarf_str_offset != 0)
1552 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1553 break;
1554 case DW_FORM_strx:
1555 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1556 false, info_ptr_end);
1557 if (unit->dwarf_str_offset != 0)
1558 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1559 break;
1560 case DW_FORM_exprloc:
1561 case DW_FORM_block:
1562 amt = _bfd_safe_read_leb128 (abfd, &info_ptr,
1563 false, info_ptr_end);
1564 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1565 if (attr->u.blk == NULL)
1566 return NULL;
1567 break;
1568 case DW_FORM_block1:
1569 amt = read_1_byte (abfd, &info_ptr, info_ptr_end);
1570 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1571 if (attr->u.blk == NULL)
1572 return NULL;
1573 break;
1574 case DW_FORM_sdata:
1575 attr->u.sval = _bfd_safe_read_leb128 (abfd, &info_ptr,
1576 true, info_ptr_end);
1577 break;
1578 case DW_FORM_ref_udata:
1579 case DW_FORM_udata:
1580 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1581 false, info_ptr_end);
1582 break;
1583 case DW_FORM_addrx:
1584 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1585 false, info_ptr_end);
1586 if (unit->dwarf_addr_offset != 0)
1587 attr->u.val = read_indexed_address (attr->u.val, unit);
1588 break;
1589 case DW_FORM_indirect:
1590 form = _bfd_safe_read_leb128 (abfd, &info_ptr,
1591 false, info_ptr_end);
1592 if (form == DW_FORM_implicit_const)
1593 implicit_const = _bfd_safe_read_leb128 (abfd, &info_ptr,
1594 true, info_ptr_end);
1595 info_ptr = read_attribute_value (attr, form, implicit_const, unit,
1596 info_ptr, info_ptr_end);
1597 break;
1598 case DW_FORM_implicit_const:
1599 attr->form = DW_FORM_sdata;
1600 attr->u.sval = implicit_const;
1601 break;
1602 case DW_FORM_data16:
1603 /* This is really a "constant", but there is no way to store that
1604 so pretend it is a 16 byte block instead. */
1605 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, 16);
1606 if (attr->u.blk == NULL)
1607 return NULL;
1608 break;
1610 default:
1611 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1612 form);
1613 bfd_set_error (bfd_error_bad_value);
1614 return NULL;
1616 return info_ptr;
1619 /* Read an attribute described by an abbreviated attribute. */
1621 static bfd_byte *
1622 read_attribute (struct attribute * attr,
1623 struct attr_abbrev * abbrev,
1624 struct comp_unit * unit,
1625 bfd_byte * info_ptr,
1626 bfd_byte * info_ptr_end)
1628 attr->name = abbrev->name;
1629 info_ptr = read_attribute_value (attr, abbrev->form, abbrev->implicit_const,
1630 unit, info_ptr, info_ptr_end);
1631 return info_ptr;
1634 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1635 for a function. */
1637 static bool
1638 non_mangled (int lang)
1640 switch (lang)
1642 default:
1643 return false;
1645 case DW_LANG_C89:
1646 case DW_LANG_C:
1647 case DW_LANG_Ada83:
1648 case DW_LANG_Cobol74:
1649 case DW_LANG_Cobol85:
1650 case DW_LANG_Fortran77:
1651 case DW_LANG_Pascal83:
1652 case DW_LANG_C99:
1653 case DW_LANG_Ada95:
1654 case DW_LANG_PLI:
1655 case DW_LANG_UPC:
1656 case DW_LANG_C11:
1657 case DW_LANG_Mips_Assembler:
1658 return true;
1662 /* Source line information table routines. */
1664 #define FILE_ALLOC_CHUNK 5
1665 #define DIR_ALLOC_CHUNK 5
1667 struct line_info
1669 struct line_info * prev_line;
1670 bfd_vma address;
1671 char * filename;
1672 unsigned int line;
1673 unsigned int column;
1674 unsigned int discriminator;
1675 unsigned char op_index;
1676 unsigned char end_sequence; /* End of (sequential) code sequence. */
1679 struct fileinfo
1681 char * name;
1682 unsigned int dir;
1683 unsigned int time;
1684 unsigned int size;
1687 struct line_sequence
1689 bfd_vma low_pc;
1690 struct line_sequence* prev_sequence;
1691 struct line_info* last_line; /* Largest VMA. */
1692 struct line_info** line_info_lookup;
1693 bfd_size_type num_lines;
1696 struct line_info_table
1698 bfd * abfd;
1699 unsigned int num_files;
1700 unsigned int num_dirs;
1701 unsigned int num_sequences;
1702 char * comp_dir;
1703 char ** dirs;
1704 struct fileinfo* files;
1705 struct line_sequence* sequences;
1706 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
1709 /* Remember some information about each function. If the function is
1710 inlined (DW_TAG_inlined_subroutine) it may have two additional
1711 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1712 source code location where this function was inlined. */
1714 struct funcinfo
1716 /* Pointer to previous function in list of all functions. */
1717 struct funcinfo *prev_func;
1718 /* Pointer to function one scope higher. */
1719 struct funcinfo *caller_func;
1720 /* Source location file name where caller_func inlines this func. */
1721 char *caller_file;
1722 /* Source location file name. */
1723 char *file;
1724 /* Source location line number where caller_func inlines this func. */
1725 int caller_line;
1726 /* Source location line number. */
1727 int line;
1728 int tag;
1729 bool is_linkage;
1730 const char *name;
1731 struct arange arange;
1732 /* Where the symbol is defined. */
1733 asection *sec;
1734 /* The offset of the funcinfo from the start of the unit. */
1735 uint64_t unit_offset;
1738 struct lookup_funcinfo
1740 /* Function information corresponding to this lookup table entry. */
1741 struct funcinfo *funcinfo;
1743 /* The lowest address for this specific function. */
1744 bfd_vma low_addr;
1746 /* The highest address of this function before the lookup table is sorted.
1747 The highest address of all prior functions after the lookup table is
1748 sorted, which is used for binary search. */
1749 bfd_vma high_addr;
1750 /* Index of this function, used to ensure qsort is stable. */
1751 unsigned int idx;
1754 struct varinfo
1756 /* Pointer to previous variable in list of all variables. */
1757 struct varinfo *prev_var;
1758 /* The offset of the varinfo from the start of the unit. */
1759 uint64_t unit_offset;
1760 /* Source location file name. */
1761 char *file;
1762 /* Source location line number. */
1763 int line;
1764 /* The type of this variable. */
1765 int tag;
1766 /* The name of the variable, if it has one. */
1767 char *name;
1768 /* The address of the variable. */
1769 bfd_vma addr;
1770 /* Where the symbol is defined. */
1771 asection *sec;
1772 /* Is this a stack variable? */
1773 bool stack;
1776 /* Return TRUE if NEW_LINE should sort after LINE. */
1778 static inline bool
1779 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1781 return (new_line->address > line->address
1782 || (new_line->address == line->address
1783 && new_line->op_index > line->op_index));
1787 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1788 that the list is sorted. Note that the line_info list is sorted from
1789 highest to lowest VMA (with possible duplicates); that is,
1790 line_info->prev_line always accesses an equal or smaller VMA. */
1792 static bool
1793 add_line_info (struct line_info_table *table,
1794 bfd_vma address,
1795 unsigned char op_index,
1796 char *filename,
1797 unsigned int line,
1798 unsigned int column,
1799 unsigned int discriminator,
1800 int end_sequence)
1802 size_t amt = sizeof (struct line_info);
1803 struct line_sequence* seq = table->sequences;
1804 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
1806 if (info == NULL)
1807 return false;
1809 /* Set member data of 'info'. */
1810 info->prev_line = NULL;
1811 info->address = address;
1812 info->op_index = op_index;
1813 info->line = line;
1814 info->column = column;
1815 info->discriminator = discriminator;
1816 info->end_sequence = end_sequence;
1818 if (filename && filename[0])
1820 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1821 if (info->filename == NULL)
1822 return false;
1823 strcpy (info->filename, filename);
1825 else
1826 info->filename = NULL;
1828 /* Find the correct location for 'info'. Normally we will receive
1829 new line_info data 1) in order and 2) with increasing VMAs.
1830 However some compilers break the rules (cf. decode_line_info) and
1831 so we include some heuristics for quickly finding the correct
1832 location for 'info'. In particular, these heuristics optimize for
1833 the common case in which the VMA sequence that we receive is a
1834 list of locally sorted VMAs such as
1835 p...z a...j (where a < j < p < z)
1837 Note: table->lcl_head is used to head an *actual* or *possible*
1838 sub-sequence within the list (such as a...j) that is not directly
1839 headed by table->last_line
1841 Note: we may receive duplicate entries from 'decode_line_info'. */
1843 if (seq
1844 && seq->last_line->address == address
1845 && seq->last_line->op_index == op_index
1846 && seq->last_line->end_sequence == end_sequence)
1848 /* We only keep the last entry with the same address and end
1849 sequence. See PR ld/4986. */
1850 if (table->lcl_head == seq->last_line)
1851 table->lcl_head = info;
1852 info->prev_line = seq->last_line->prev_line;
1853 seq->last_line = info;
1855 else if (!seq || seq->last_line->end_sequence)
1857 /* Start a new line sequence. */
1858 amt = sizeof (struct line_sequence);
1859 seq = (struct line_sequence *) bfd_malloc (amt);
1860 if (seq == NULL)
1861 return false;
1862 seq->low_pc = address;
1863 seq->prev_sequence = table->sequences;
1864 seq->last_line = info;
1865 table->lcl_head = info;
1866 table->sequences = seq;
1867 table->num_sequences++;
1869 else if (info->end_sequence
1870 || new_line_sorts_after (info, seq->last_line))
1872 /* Normal case: add 'info' to the beginning of the current sequence. */
1873 info->prev_line = seq->last_line;
1874 seq->last_line = info;
1876 /* lcl_head: initialize to head a *possible* sequence at the end. */
1877 if (!table->lcl_head)
1878 table->lcl_head = info;
1880 else if (!new_line_sorts_after (info, table->lcl_head)
1881 && (!table->lcl_head->prev_line
1882 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1884 /* Abnormal but easy: lcl_head is the head of 'info'. */
1885 info->prev_line = table->lcl_head->prev_line;
1886 table->lcl_head->prev_line = info;
1888 else
1890 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1891 are valid heads for 'info'. Reset 'lcl_head'. */
1892 struct line_info* li2 = seq->last_line; /* Always non-NULL. */
1893 struct line_info* li1 = li2->prev_line;
1895 while (li1)
1897 if (!new_line_sorts_after (info, li2)
1898 && new_line_sorts_after (info, li1))
1899 break;
1901 li2 = li1; /* always non-NULL */
1902 li1 = li1->prev_line;
1904 table->lcl_head = li2;
1905 info->prev_line = table->lcl_head->prev_line;
1906 table->lcl_head->prev_line = info;
1907 if (address < seq->low_pc)
1908 seq->low_pc = address;
1910 return true;
1913 /* Extract a fully qualified filename from a line info table.
1914 The returned string has been malloc'ed and it is the caller's
1915 responsibility to free it. */
1917 static char *
1918 concat_filename (struct line_info_table *table, unsigned int file)
1920 char *filename;
1922 if (table == NULL || file - 1 >= table->num_files)
1924 /* FILE == 0 means unknown. */
1925 if (file)
1926 _bfd_error_handler
1927 (_("DWARF error: mangled line number section (bad file number)"));
1928 return strdup ("<unknown>");
1931 filename = table->files[file - 1].name;
1932 if (filename == NULL)
1933 return strdup ("<unknown>");
1935 if (!IS_ABSOLUTE_PATH (filename))
1937 char *dir_name = NULL;
1938 char *subdir_name = NULL;
1939 char *name;
1940 size_t len;
1942 if (table->files[file - 1].dir
1943 /* PR 17512: file: 0317e960. */
1944 && table->files[file - 1].dir <= table->num_dirs
1945 /* PR 17512: file: 7f3d2e4b. */
1946 && table->dirs != NULL)
1947 subdir_name = table->dirs[table->files[file - 1].dir - 1];
1949 if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
1950 dir_name = table->comp_dir;
1952 if (!dir_name)
1954 dir_name = subdir_name;
1955 subdir_name = NULL;
1958 if (!dir_name)
1959 return strdup (filename);
1961 len = strlen (dir_name) + strlen (filename) + 2;
1963 if (subdir_name)
1965 len += strlen (subdir_name) + 1;
1966 name = (char *) bfd_malloc (len);
1967 if (name)
1968 sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
1970 else
1972 name = (char *) bfd_malloc (len);
1973 if (name)
1974 sprintf (name, "%s/%s", dir_name, filename);
1977 return name;
1980 return strdup (filename);
1983 /* Number of bits in a bfd_vma. */
1984 #define VMA_BITS (8 * sizeof (bfd_vma))
1986 /* Check whether [low1, high1) can be combined with [low2, high2),
1987 i.e., they touch or overlap. */
1988 static bool ranges_overlap (bfd_vma low1,
1989 bfd_vma high1,
1990 bfd_vma low2,
1991 bfd_vma high2)
1993 if (low1 == low2 || high1 == high2)
1994 return true;
1996 /* Sort so that low1 is below low2. */
1997 if (low1 > low2)
1999 bfd_vma tmp;
2001 tmp = low1;
2002 low1 = low2;
2003 low2 = tmp;
2005 tmp = high1;
2006 high1 = high2;
2007 high2 = tmp;
2010 /* We touch iff low2 == high1.
2011 We overlap iff low2 is within [low1, high1). */
2012 return (low2 <= high1);
2015 /* Insert an address range in the trie mapping addresses to compilation units.
2016 Will return the new trie node (usually the same as is being sent in, but
2017 in case of a leaf-to-interior conversion, or expansion of a leaf, it may be
2018 different), or NULL on failure.
2020 static struct trie_node *insert_arange_in_trie(bfd *abfd,
2021 struct trie_node *trie,
2022 bfd_vma trie_pc,
2023 unsigned int trie_pc_bits,
2024 struct comp_unit *unit,
2025 bfd_vma low_pc,
2026 bfd_vma high_pc)
2028 bfd_vma clamped_low_pc, clamped_high_pc;
2029 int ch, from_ch, to_ch;
2030 bool is_full_leaf = false;
2032 /* See if we can extend any of the existing ranges. This merging
2033 isn't perfect (if merging opens up the possibility of merging two existing
2034 ranges, we won't find them), but it takes the majority of the cases. */
2035 if (trie->num_room_in_leaf > 0)
2037 struct trie_leaf *leaf = (struct trie_leaf *) trie;
2038 unsigned int i;
2040 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2042 if (leaf->ranges[i].unit == unit &&
2043 ranges_overlap(low_pc, high_pc,
2044 leaf->ranges[i].low_pc, leaf->ranges[i].high_pc))
2046 if (low_pc < leaf->ranges[i].low_pc)
2047 leaf->ranges[i].low_pc = low_pc;
2048 if (high_pc > leaf->ranges[i].high_pc)
2049 leaf->ranges[i].high_pc = high_pc;
2050 return trie;
2054 is_full_leaf = leaf->num_stored_in_leaf == trie->num_room_in_leaf;
2057 /* If we're a leaf with no more room and we're _not_ at the bottom,
2058 convert to an interior node. */
2059 if (is_full_leaf && trie_pc_bits < VMA_BITS)
2061 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
2062 unsigned int i;
2064 trie = bfd_zalloc (abfd, sizeof (struct trie_interior));
2065 if (!trie)
2066 return NULL;
2067 is_full_leaf = false;
2069 /* TODO: If we wanted to save a little more memory at the cost of
2070 complexity, we could have reused the old leaf node as one of the
2071 children of the new interior node, instead of throwing it away. */
2072 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2074 if (!insert_arange_in_trie (abfd, trie, trie_pc, trie_pc_bits,
2075 leaf->ranges[i].unit, leaf->ranges[i].low_pc,
2076 leaf->ranges[i].high_pc))
2077 return NULL;
2081 /* If we're a leaf with no more room and we _are_ at the bottom,
2082 we have no choice but to just make it larger. */
2083 if (is_full_leaf)
2085 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
2086 unsigned int new_room_in_leaf = trie->num_room_in_leaf * 2;
2087 struct trie_leaf *new_leaf;
2089 new_leaf = bfd_zalloc (abfd,
2090 sizeof (struct trie_leaf) +
2091 (new_room_in_leaf - TRIE_LEAF_SIZE) * sizeof (leaf->ranges[0]));
2092 new_leaf->head.num_room_in_leaf = new_room_in_leaf;
2093 new_leaf->num_stored_in_leaf = leaf->num_stored_in_leaf;
2095 memcpy (new_leaf->ranges,
2096 leaf->ranges,
2097 leaf->num_stored_in_leaf * sizeof (leaf->ranges[0]));
2098 trie = &new_leaf->head;
2099 is_full_leaf = false;
2101 /* Now the insert below will go through. */
2104 /* If we're a leaf (now with room), we can just insert at the end. */
2105 if (trie->num_room_in_leaf > 0)
2107 struct trie_leaf *leaf = (struct trie_leaf *) trie;
2109 unsigned int i = leaf->num_stored_in_leaf++;
2110 leaf->ranges[i].unit = unit;
2111 leaf->ranges[i].low_pc = low_pc;
2112 leaf->ranges[i].high_pc = high_pc;
2113 return trie;
2116 /* Now we are definitely an interior node, so recurse into all
2117 the relevant buckets. */
2119 /* Clamp the range to the current trie bucket. */
2120 clamped_low_pc = low_pc;
2121 clamped_high_pc = high_pc;
2122 if (trie_pc_bits > 0)
2124 bfd_vma bucket_high_pc =
2125 trie_pc + ((bfd_vma)-1 >> trie_pc_bits); /* Inclusive. */
2126 if (clamped_low_pc < trie_pc)
2127 clamped_low_pc = trie_pc;
2128 if (clamped_high_pc > bucket_high_pc)
2129 clamped_high_pc = bucket_high_pc;
2132 /* Insert the ranges in all buckets that it spans. */
2133 from_ch = (clamped_low_pc >> (VMA_BITS - trie_pc_bits - 8)) & 0xff;
2134 to_ch = ((clamped_high_pc - 1) >> (VMA_BITS - trie_pc_bits - 8)) & 0xff;
2135 for (ch = from_ch; ch <= to_ch; ++ch)
2137 struct trie_interior *interior = (struct trie_interior *) trie;
2138 struct trie_node *child = interior->children[ch];
2140 if (child == NULL)
2142 child = alloc_trie_leaf (abfd);
2143 if (!child)
2144 return NULL;
2146 child = insert_arange_in_trie (abfd,
2147 child,
2148 trie_pc + ((bfd_vma)ch << (VMA_BITS - trie_pc_bits - 8)),
2149 trie_pc_bits + 8,
2150 unit,
2151 low_pc,
2152 high_pc);
2153 if (!child)
2154 return NULL;
2156 interior->children[ch] = child;
2159 return trie;
2163 static bool
2164 arange_add (struct comp_unit *unit, struct arange *first_arange,
2165 struct trie_node **trie_root, bfd_vma low_pc, bfd_vma high_pc)
2167 struct arange *arange;
2169 /* Ignore empty ranges. */
2170 if (low_pc == high_pc)
2171 return true;
2173 if (trie_root != NULL)
2175 *trie_root = insert_arange_in_trie (unit->file->bfd_ptr,
2176 *trie_root,
2179 unit,
2180 low_pc,
2181 high_pc);
2182 if (*trie_root == NULL)
2183 return false;
2186 /* If the first arange is empty, use it. */
2187 if (first_arange->high == 0)
2189 first_arange->low = low_pc;
2190 first_arange->high = high_pc;
2191 return true;
2194 /* Next see if we can cheaply extend an existing range. */
2195 arange = first_arange;
2198 if (low_pc == arange->high)
2200 arange->high = high_pc;
2201 return true;
2203 if (high_pc == arange->low)
2205 arange->low = low_pc;
2206 return true;
2208 arange = arange->next;
2210 while (arange);
2212 /* Need to allocate a new arange and insert it into the arange list.
2213 Order isn't significant, so just insert after the first arange. */
2214 arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
2215 if (arange == NULL)
2216 return false;
2217 arange->low = low_pc;
2218 arange->high = high_pc;
2219 arange->next = first_arange->next;
2220 first_arange->next = arange;
2221 return true;
2224 /* Compare function for line sequences. */
2226 static int
2227 compare_sequences (const void* a, const void* b)
2229 const struct line_sequence* seq1 = a;
2230 const struct line_sequence* seq2 = b;
2232 /* Sort by low_pc as the primary key. */
2233 if (seq1->low_pc < seq2->low_pc)
2234 return -1;
2235 if (seq1->low_pc > seq2->low_pc)
2236 return 1;
2238 /* If low_pc values are equal, sort in reverse order of
2239 high_pc, so that the largest region comes first. */
2240 if (seq1->last_line->address < seq2->last_line->address)
2241 return 1;
2242 if (seq1->last_line->address > seq2->last_line->address)
2243 return -1;
2245 if (seq1->last_line->op_index < seq2->last_line->op_index)
2246 return 1;
2247 if (seq1->last_line->op_index > seq2->last_line->op_index)
2248 return -1;
2250 /* num_lines is initially an index, to make the sort stable. */
2251 if (seq1->num_lines < seq2->num_lines)
2252 return -1;
2253 if (seq1->num_lines > seq2->num_lines)
2254 return 1;
2255 return 0;
2258 /* Construct the line information table for quick lookup. */
2260 static bool
2261 build_line_info_table (struct line_info_table * table,
2262 struct line_sequence * seq)
2264 size_t amt;
2265 struct line_info **line_info_lookup;
2266 struct line_info *each_line;
2267 unsigned int num_lines;
2268 unsigned int line_index;
2270 if (seq->line_info_lookup != NULL)
2271 return true;
2273 /* Count the number of line information entries. We could do this while
2274 scanning the debug information, but some entries may be added via
2275 lcl_head without having a sequence handy to increment the number of
2276 lines. */
2277 num_lines = 0;
2278 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
2279 num_lines++;
2281 seq->num_lines = num_lines;
2282 if (num_lines == 0)
2283 return true;
2285 /* Allocate space for the line information lookup table. */
2286 amt = sizeof (struct line_info*) * num_lines;
2287 line_info_lookup = (struct line_info**) bfd_alloc (table->abfd, amt);
2288 seq->line_info_lookup = line_info_lookup;
2289 if (line_info_lookup == NULL)
2290 return false;
2292 /* Create the line information lookup table. */
2293 line_index = num_lines;
2294 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
2295 line_info_lookup[--line_index] = each_line;
2297 BFD_ASSERT (line_index == 0);
2298 return true;
2301 /* Sort the line sequences for quick lookup. */
2303 static bool
2304 sort_line_sequences (struct line_info_table* table)
2306 size_t amt;
2307 struct line_sequence *sequences;
2308 struct line_sequence *seq;
2309 unsigned int n = 0;
2310 unsigned int num_sequences = table->num_sequences;
2311 bfd_vma last_high_pc;
2313 if (num_sequences == 0)
2314 return true;
2316 /* Allocate space for an array of sequences. */
2317 amt = sizeof (struct line_sequence) * num_sequences;
2318 sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
2319 if (sequences == NULL)
2320 return false;
2322 /* Copy the linked list into the array, freeing the original nodes. */
2323 seq = table->sequences;
2324 for (n = 0; n < num_sequences; n++)
2326 struct line_sequence* last_seq = seq;
2328 BFD_ASSERT (seq);
2329 sequences[n].low_pc = seq->low_pc;
2330 sequences[n].prev_sequence = NULL;
2331 sequences[n].last_line = seq->last_line;
2332 sequences[n].line_info_lookup = NULL;
2333 sequences[n].num_lines = n;
2334 seq = seq->prev_sequence;
2335 free (last_seq);
2337 BFD_ASSERT (seq == NULL);
2339 qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
2341 /* Make the list binary-searchable by trimming overlapping entries
2342 and removing nested entries. */
2343 num_sequences = 1;
2344 last_high_pc = sequences[0].last_line->address;
2345 for (n = 1; n < table->num_sequences; n++)
2347 if (sequences[n].low_pc < last_high_pc)
2349 if (sequences[n].last_line->address <= last_high_pc)
2350 /* Skip nested entries. */
2351 continue;
2353 /* Trim overlapping entries. */
2354 sequences[n].low_pc = last_high_pc;
2356 last_high_pc = sequences[n].last_line->address;
2357 if (n > num_sequences)
2359 /* Close up the gap. */
2360 sequences[num_sequences].low_pc = sequences[n].low_pc;
2361 sequences[num_sequences].last_line = sequences[n].last_line;
2363 num_sequences++;
2366 table->sequences = sequences;
2367 table->num_sequences = num_sequences;
2368 return true;
2371 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
2373 static bool
2374 line_info_add_include_dir (struct line_info_table *table, char *cur_dir)
2376 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
2378 char **tmp;
2379 size_t amt;
2381 amt = table->num_dirs + DIR_ALLOC_CHUNK;
2382 amt *= sizeof (char *);
2384 tmp = (char **) bfd_realloc (table->dirs, amt);
2385 if (tmp == NULL)
2386 return false;
2387 table->dirs = tmp;
2390 table->dirs[table->num_dirs++] = cur_dir;
2391 return true;
2394 static bool
2395 line_info_add_include_dir_stub (struct line_info_table *table, char *cur_dir,
2396 unsigned int dir ATTRIBUTE_UNUSED,
2397 unsigned int xtime ATTRIBUTE_UNUSED,
2398 unsigned int size ATTRIBUTE_UNUSED)
2400 return line_info_add_include_dir (table, cur_dir);
2403 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
2405 static bool
2406 line_info_add_file_name (struct line_info_table *table, char *cur_file,
2407 unsigned int dir, unsigned int xtime,
2408 unsigned int size)
2410 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
2412 struct fileinfo *tmp;
2413 size_t amt;
2415 amt = table->num_files + FILE_ALLOC_CHUNK;
2416 amt *= sizeof (struct fileinfo);
2418 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
2419 if (tmp == NULL)
2420 return false;
2421 table->files = tmp;
2424 table->files[table->num_files].name = cur_file;
2425 table->files[table->num_files].dir = dir;
2426 table->files[table->num_files].time = xtime;
2427 table->files[table->num_files].size = size;
2428 table->num_files++;
2429 return true;
2432 /* Read directory or file name entry format, starting with byte of
2433 format count entries, ULEB128 pairs of entry formats, ULEB128 of
2434 entries count and the entries themselves in the described entry
2435 format. */
2437 static bool
2438 read_formatted_entries (struct comp_unit *unit, bfd_byte **bufp,
2439 bfd_byte *buf_end, struct line_info_table *table,
2440 bool (*callback) (struct line_info_table *table,
2441 char *cur_file,
2442 unsigned int dir,
2443 unsigned int time,
2444 unsigned int size))
2446 bfd *abfd = unit->abfd;
2447 bfd_byte format_count, formati;
2448 bfd_vma data_count, datai;
2449 bfd_byte *buf = *bufp;
2450 bfd_byte *format_header_data;
2452 format_count = read_1_byte (abfd, &buf, buf_end);
2453 format_header_data = buf;
2454 for (formati = 0; formati < format_count; formati++)
2456 _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2457 _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2460 data_count = _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2461 if (format_count == 0 && data_count != 0)
2463 _bfd_error_handler (_("DWARF error: zero format count"));
2464 bfd_set_error (bfd_error_bad_value);
2465 return false;
2468 /* PR 22210. Paranoia check. Don't bother running the loop
2469 if we know that we are going to run out of buffer. */
2470 if (data_count > (bfd_vma) (buf_end - buf))
2472 _bfd_error_handler
2473 (_("DWARF error: data count (%" PRIx64 ") larger than buffer size"),
2474 (uint64_t) data_count);
2475 bfd_set_error (bfd_error_bad_value);
2476 return false;
2479 for (datai = 0; datai < data_count; datai++)
2481 bfd_byte *format = format_header_data;
2482 struct fileinfo fe;
2484 memset (&fe, 0, sizeof fe);
2485 for (formati = 0; formati < format_count; formati++)
2487 bfd_vma content_type, form;
2488 char *string_trash;
2489 char **stringp = &string_trash;
2490 unsigned int uint_trash, *uintp = &uint_trash;
2491 struct attribute attr;
2493 content_type = _bfd_safe_read_leb128 (abfd, &format, false, buf_end);
2494 switch (content_type)
2496 case DW_LNCT_path:
2497 stringp = &fe.name;
2498 break;
2499 case DW_LNCT_directory_index:
2500 uintp = &fe.dir;
2501 break;
2502 case DW_LNCT_timestamp:
2503 uintp = &fe.time;
2504 break;
2505 case DW_LNCT_size:
2506 uintp = &fe.size;
2507 break;
2508 case DW_LNCT_MD5:
2509 break;
2510 default:
2511 _bfd_error_handler
2512 (_("DWARF error: unknown format content type %" PRIu64),
2513 (uint64_t) content_type);
2514 bfd_set_error (bfd_error_bad_value);
2515 return false;
2518 form = _bfd_safe_read_leb128 (abfd, &format, false, buf_end);
2519 buf = read_attribute_value (&attr, form, 0, unit, buf, buf_end);
2520 if (buf == NULL)
2521 return false;
2522 switch (form)
2524 case DW_FORM_string:
2525 case DW_FORM_line_strp:
2526 case DW_FORM_strx:
2527 case DW_FORM_strx1:
2528 case DW_FORM_strx2:
2529 case DW_FORM_strx3:
2530 case DW_FORM_strx4:
2531 *stringp = attr.u.str;
2532 break;
2534 case DW_FORM_data1:
2535 case DW_FORM_data2:
2536 case DW_FORM_data4:
2537 case DW_FORM_data8:
2538 case DW_FORM_udata:
2539 *uintp = attr.u.val;
2540 break;
2542 case DW_FORM_data16:
2543 /* MD5 data is in the attr.blk, but we are ignoring those. */
2544 break;
2548 /* Skip the first "zero entry", which is the compilation dir/file. */
2549 if (datai != 0)
2550 if (!callback (table, fe.name, fe.dir, fe.time, fe.size))
2551 return false;
2554 *bufp = buf;
2555 return true;
2558 /* Decode the line number information for UNIT. */
2560 static struct line_info_table*
2561 decode_line_info (struct comp_unit *unit)
2563 bfd *abfd = unit->abfd;
2564 struct dwarf2_debug *stash = unit->stash;
2565 struct dwarf2_debug_file *file = unit->file;
2566 struct line_info_table* table;
2567 bfd_byte *line_ptr;
2568 bfd_byte *line_end;
2569 struct line_head lh;
2570 unsigned int i, offset_size;
2571 char *cur_file, *cur_dir;
2572 unsigned char op_code, extended_op, adj_opcode;
2573 unsigned int exop_len;
2574 size_t amt;
2576 if (unit->line_offset == 0 && file->line_table)
2577 return file->line_table;
2579 if (! read_section (abfd, &stash->debug_sections[debug_line],
2580 file->syms, unit->line_offset,
2581 &file->dwarf_line_buffer, &file->dwarf_line_size))
2582 return NULL;
2584 if (file->dwarf_line_size < 16)
2586 _bfd_error_handler
2587 (_("DWARF error: line info section is too small (%" PRId64 ")"),
2588 (int64_t) file->dwarf_line_size);
2589 bfd_set_error (bfd_error_bad_value);
2590 return NULL;
2592 line_ptr = file->dwarf_line_buffer + unit->line_offset;
2593 line_end = file->dwarf_line_buffer + file->dwarf_line_size;
2595 /* Read in the prologue. */
2596 lh.total_length = read_4_bytes (abfd, &line_ptr, line_end);
2597 offset_size = 4;
2598 if (lh.total_length == 0xffffffff)
2600 lh.total_length = read_8_bytes (abfd, &line_ptr, line_end);
2601 offset_size = 8;
2603 else if (lh.total_length == 0 && unit->addr_size == 8)
2605 /* Handle (non-standard) 64-bit DWARF2 formats. */
2606 lh.total_length = read_4_bytes (abfd, &line_ptr, line_end);
2607 offset_size = 8;
2610 if (lh.total_length > (size_t) (line_end - line_ptr))
2612 _bfd_error_handler
2613 /* xgettext: c-format */
2614 (_("DWARF error: line info data is bigger (%#" PRIx64 ")"
2615 " than the space remaining in the section (%#lx)"),
2616 (uint64_t) lh.total_length, (unsigned long) (line_end - line_ptr));
2617 bfd_set_error (bfd_error_bad_value);
2618 return NULL;
2621 line_end = line_ptr + lh.total_length;
2623 lh.version = read_2_bytes (abfd, &line_ptr, line_end);
2624 if (lh.version < 2 || lh.version > 5)
2626 _bfd_error_handler
2627 (_("DWARF error: unhandled .debug_line version %d"), lh.version);
2628 bfd_set_error (bfd_error_bad_value);
2629 return NULL;
2632 if (line_ptr + offset_size + (lh.version >= 5 ? 8 : (lh.version >= 4 ? 6 : 5))
2633 >= line_end)
2635 _bfd_error_handler
2636 (_("DWARF error: ran out of room reading prologue"));
2637 bfd_set_error (bfd_error_bad_value);
2638 return NULL;
2641 if (lh.version >= 5)
2643 unsigned int segment_selector_size;
2645 /* Skip address size. */
2646 read_1_byte (abfd, &line_ptr, line_end);
2648 segment_selector_size = read_1_byte (abfd, &line_ptr, line_end);
2649 if (segment_selector_size != 0)
2651 _bfd_error_handler
2652 (_("DWARF error: line info unsupported segment selector size %u"),
2653 segment_selector_size);
2654 bfd_set_error (bfd_error_bad_value);
2655 return NULL;
2659 if (offset_size == 4)
2660 lh.prologue_length = read_4_bytes (abfd, &line_ptr, line_end);
2661 else
2662 lh.prologue_length = read_8_bytes (abfd, &line_ptr, line_end);
2664 lh.minimum_instruction_length = read_1_byte (abfd, &line_ptr, line_end);
2666 if (lh.version >= 4)
2667 lh.maximum_ops_per_insn = read_1_byte (abfd, &line_ptr, line_end);
2668 else
2669 lh.maximum_ops_per_insn = 1;
2671 if (lh.maximum_ops_per_insn == 0)
2673 _bfd_error_handler
2674 (_("DWARF error: invalid maximum operations per instruction"));
2675 bfd_set_error (bfd_error_bad_value);
2676 return NULL;
2679 lh.default_is_stmt = read_1_byte (abfd, &line_ptr, line_end);
2680 lh.line_base = read_1_signed_byte (abfd, &line_ptr, line_end);
2681 lh.line_range = read_1_byte (abfd, &line_ptr, line_end);
2682 lh.opcode_base = read_1_byte (abfd, &line_ptr, line_end);
2684 if (line_ptr + (lh.opcode_base - 1) >= line_end)
2686 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2687 bfd_set_error (bfd_error_bad_value);
2688 return NULL;
2691 amt = lh.opcode_base * sizeof (unsigned char);
2692 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
2694 lh.standard_opcode_lengths[0] = 1;
2696 for (i = 1; i < lh.opcode_base; ++i)
2697 lh.standard_opcode_lengths[i] = read_1_byte (abfd, &line_ptr, line_end);
2699 amt = sizeof (struct line_info_table);
2700 table = (struct line_info_table *) bfd_alloc (abfd, amt);
2701 if (table == NULL)
2702 return NULL;
2703 table->abfd = abfd;
2704 table->comp_dir = unit->comp_dir;
2706 table->num_files = 0;
2707 table->files = NULL;
2709 table->num_dirs = 0;
2710 table->dirs = NULL;
2712 table->num_sequences = 0;
2713 table->sequences = NULL;
2715 table->lcl_head = NULL;
2717 if (lh.version >= 5)
2719 /* Read directory table. */
2720 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2721 line_info_add_include_dir_stub))
2722 goto fail;
2724 /* Read file name table. */
2725 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2726 line_info_add_file_name))
2727 goto fail;
2729 else
2731 /* Read directory table. */
2732 while ((cur_dir = read_string (&line_ptr, line_end)) != NULL)
2734 if (!line_info_add_include_dir (table, cur_dir))
2735 goto fail;
2738 /* Read file name table. */
2739 while ((cur_file = read_string (&line_ptr, line_end)) != NULL)
2741 unsigned int dir, xtime, size;
2743 dir = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2744 xtime = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2745 size = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2747 if (!line_info_add_file_name (table, cur_file, dir, xtime, size))
2748 goto fail;
2752 /* Read the statement sequences until there's nothing left. */
2753 while (line_ptr < line_end)
2755 /* State machine registers. */
2756 bfd_vma address = 0;
2757 unsigned char op_index = 0;
2758 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
2759 unsigned int line = 1;
2760 unsigned int column = 0;
2761 unsigned int discriminator = 0;
2762 int is_stmt = lh.default_is_stmt;
2763 int end_sequence = 0;
2764 unsigned int dir, xtime, size;
2765 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2766 compilers generate address sequences that are wildly out of
2767 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2768 for ia64-Linux). Thus, to determine the low and high
2769 address, we must compare on every DW_LNS_copy, etc. */
2770 bfd_vma low_pc = (bfd_vma) -1;
2771 bfd_vma high_pc = 0;
2773 /* Decode the table. */
2774 while (!end_sequence && line_ptr < line_end)
2776 op_code = read_1_byte (abfd, &line_ptr, line_end);
2778 if (op_code >= lh.opcode_base)
2780 /* Special operand. */
2781 adj_opcode = op_code - lh.opcode_base;
2782 if (lh.line_range == 0)
2783 goto line_fail;
2784 if (lh.maximum_ops_per_insn == 1)
2785 address += (adj_opcode / lh.line_range
2786 * lh.minimum_instruction_length);
2787 else
2789 address += ((op_index + adj_opcode / lh.line_range)
2790 / lh.maximum_ops_per_insn
2791 * lh.minimum_instruction_length);
2792 op_index = ((op_index + adj_opcode / lh.line_range)
2793 % lh.maximum_ops_per_insn);
2795 line += lh.line_base + (adj_opcode % lh.line_range);
2796 /* Append row to matrix using current values. */
2797 if (!add_line_info (table, address, op_index, filename,
2798 line, column, discriminator, 0))
2799 goto line_fail;
2800 discriminator = 0;
2801 if (address < low_pc)
2802 low_pc = address;
2803 if (address > high_pc)
2804 high_pc = address;
2806 else switch (op_code)
2808 case DW_LNS_extended_op:
2809 exop_len = _bfd_safe_read_leb128 (abfd, &line_ptr,
2810 false, line_end);
2811 extended_op = read_1_byte (abfd, &line_ptr, line_end);
2813 switch (extended_op)
2815 case DW_LNE_end_sequence:
2816 end_sequence = 1;
2817 if (!add_line_info (table, address, op_index, filename, line,
2818 column, discriminator, end_sequence))
2819 goto line_fail;
2820 discriminator = 0;
2821 if (address < low_pc)
2822 low_pc = address;
2823 if (address > high_pc)
2824 high_pc = address;
2825 if (!arange_add (unit, &unit->arange, &unit->file->trie_root,
2826 low_pc, high_pc))
2827 goto line_fail;
2828 break;
2829 case DW_LNE_set_address:
2830 address = read_address (unit, &line_ptr, line_end);
2831 op_index = 0;
2832 break;
2833 case DW_LNE_define_file:
2834 cur_file = read_string (&line_ptr, line_end);
2835 dir = _bfd_safe_read_leb128 (abfd, &line_ptr,
2836 false, line_end);
2837 xtime = _bfd_safe_read_leb128 (abfd, &line_ptr,
2838 false, line_end);
2839 size = _bfd_safe_read_leb128 (abfd, &line_ptr,
2840 false, line_end);
2841 if (!line_info_add_file_name (table, cur_file, dir,
2842 xtime, size))
2843 goto line_fail;
2844 break;
2845 case DW_LNE_set_discriminator:
2846 discriminator =
2847 _bfd_safe_read_leb128 (abfd, &line_ptr,
2848 false, line_end);
2849 break;
2850 case DW_LNE_HP_source_file_correlation:
2851 line_ptr += exop_len - 1;
2852 break;
2853 default:
2854 _bfd_error_handler
2855 (_("DWARF error: mangled line number section"));
2856 bfd_set_error (bfd_error_bad_value);
2857 line_fail:
2858 free (filename);
2859 goto fail;
2861 break;
2862 case DW_LNS_copy:
2863 if (!add_line_info (table, address, op_index,
2864 filename, line, column, discriminator, 0))
2865 goto line_fail;
2866 discriminator = 0;
2867 if (address < low_pc)
2868 low_pc = address;
2869 if (address > high_pc)
2870 high_pc = address;
2871 break;
2872 case DW_LNS_advance_pc:
2873 if (lh.maximum_ops_per_insn == 1)
2874 address += (lh.minimum_instruction_length
2875 * _bfd_safe_read_leb128 (abfd, &line_ptr,
2876 false, line_end));
2877 else
2879 bfd_vma adjust = _bfd_safe_read_leb128 (abfd, &line_ptr,
2880 false, line_end);
2881 address = ((op_index + adjust) / lh.maximum_ops_per_insn
2882 * lh.minimum_instruction_length);
2883 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
2885 break;
2886 case DW_LNS_advance_line:
2887 line += _bfd_safe_read_leb128 (abfd, &line_ptr,
2888 true, line_end);
2889 break;
2890 case DW_LNS_set_file:
2892 unsigned int filenum;
2894 /* The file and directory tables are 0
2895 based, the references are 1 based. */
2896 filenum = _bfd_safe_read_leb128 (abfd, &line_ptr,
2897 false, line_end);
2898 free (filename);
2899 filename = concat_filename (table, filenum);
2900 break;
2902 case DW_LNS_set_column:
2903 column = _bfd_safe_read_leb128 (abfd, &line_ptr,
2904 false, line_end);
2905 break;
2906 case DW_LNS_negate_stmt:
2907 is_stmt = (!is_stmt);
2908 break;
2909 case DW_LNS_set_basic_block:
2910 break;
2911 case DW_LNS_const_add_pc:
2912 if (lh.line_range == 0)
2913 goto line_fail;
2914 if (lh.maximum_ops_per_insn == 1)
2915 address += (lh.minimum_instruction_length
2916 * ((255 - lh.opcode_base) / lh.line_range));
2917 else
2919 bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
2920 address += (lh.minimum_instruction_length
2921 * ((op_index + adjust)
2922 / lh.maximum_ops_per_insn));
2923 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
2925 break;
2926 case DW_LNS_fixed_advance_pc:
2927 address += read_2_bytes (abfd, &line_ptr, line_end);
2928 op_index = 0;
2929 break;
2930 default:
2931 /* Unknown standard opcode, ignore it. */
2932 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
2933 (void) _bfd_safe_read_leb128 (abfd, &line_ptr,
2934 false, line_end);
2935 break;
2939 free (filename);
2942 if (unit->line_offset == 0)
2943 file->line_table = table;
2944 if (sort_line_sequences (table))
2945 return table;
2947 fail:
2948 while (table->sequences != NULL)
2950 struct line_sequence* seq = table->sequences;
2951 table->sequences = table->sequences->prev_sequence;
2952 free (seq);
2954 free (table->files);
2955 free (table->dirs);
2956 return NULL;
2959 /* If ADDR is within TABLE set the output parameters and return TRUE,
2960 otherwise set *FILENAME_PTR to NULL and return FALSE.
2961 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2962 are pointers to the objects to be filled in. */
2964 static bool
2965 lookup_address_in_line_info_table (struct line_info_table *table,
2966 bfd_vma addr,
2967 const char **filename_ptr,
2968 unsigned int *linenumber_ptr,
2969 unsigned int *discriminator_ptr)
2971 struct line_sequence *seq = NULL;
2972 struct line_info *info;
2973 int low, high, mid;
2975 /* Binary search the array of sequences. */
2976 low = 0;
2977 high = table->num_sequences;
2978 while (low < high)
2980 mid = (low + high) / 2;
2981 seq = &table->sequences[mid];
2982 if (addr < seq->low_pc)
2983 high = mid;
2984 else if (addr >= seq->last_line->address)
2985 low = mid + 1;
2986 else
2987 break;
2990 /* Check for a valid sequence. */
2991 if (!seq || addr < seq->low_pc || addr >= seq->last_line->address)
2992 goto fail;
2994 if (!build_line_info_table (table, seq))
2995 goto fail;
2997 /* Binary search the array of line information. */
2998 low = 0;
2999 high = seq->num_lines;
3000 info = NULL;
3001 while (low < high)
3003 mid = (low + high) / 2;
3004 info = seq->line_info_lookup[mid];
3005 if (addr < info->address)
3006 high = mid;
3007 else if (addr >= seq->line_info_lookup[mid + 1]->address)
3008 low = mid + 1;
3009 else
3010 break;
3013 /* Check for a valid line information entry. */
3014 if (info
3015 && addr >= info->address
3016 && addr < seq->line_info_lookup[mid + 1]->address
3017 && !(info->end_sequence || info == seq->last_line))
3019 *filename_ptr = info->filename;
3020 *linenumber_ptr = info->line;
3021 if (discriminator_ptr)
3022 *discriminator_ptr = info->discriminator;
3023 return true;
3026 fail:
3027 *filename_ptr = NULL;
3028 return false;
3031 /* Read in the .debug_ranges section for future reference. */
3033 static bool
3034 read_debug_ranges (struct comp_unit * unit)
3036 struct dwarf2_debug *stash = unit->stash;
3037 struct dwarf2_debug_file *file = unit->file;
3039 return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
3040 file->syms, 0,
3041 &file->dwarf_ranges_buffer, &file->dwarf_ranges_size);
3044 /* Read in the .debug_rnglists section for future reference. */
3046 static bool
3047 read_debug_rnglists (struct comp_unit * unit)
3049 struct dwarf2_debug *stash = unit->stash;
3050 struct dwarf2_debug_file *file = unit->file;
3052 return read_section (unit->abfd, &stash->debug_sections[debug_rnglists],
3053 file->syms, 0,
3054 &file->dwarf_rnglists_buffer, &file->dwarf_rnglists_size);
3057 /* Function table functions. */
3059 static int
3060 compare_lookup_funcinfos (const void * a, const void * b)
3062 const struct lookup_funcinfo * lookup1 = a;
3063 const struct lookup_funcinfo * lookup2 = b;
3065 if (lookup1->low_addr < lookup2->low_addr)
3066 return -1;
3067 if (lookup1->low_addr > lookup2->low_addr)
3068 return 1;
3069 if (lookup1->high_addr < lookup2->high_addr)
3070 return -1;
3071 if (lookup1->high_addr > lookup2->high_addr)
3072 return 1;
3074 if (lookup1->idx < lookup2->idx)
3075 return -1;
3076 if (lookup1->idx > lookup2->idx)
3077 return 1;
3078 return 0;
3081 static bool
3082 build_lookup_funcinfo_table (struct comp_unit * unit)
3084 struct lookup_funcinfo *lookup_funcinfo_table = unit->lookup_funcinfo_table;
3085 unsigned int number_of_functions = unit->number_of_functions;
3086 struct funcinfo *each;
3087 struct lookup_funcinfo *entry;
3088 size_t func_index;
3089 struct arange *range;
3090 bfd_vma low_addr, high_addr;
3092 if (lookup_funcinfo_table || number_of_functions == 0)
3093 return true;
3095 /* Create the function info lookup table. */
3096 lookup_funcinfo_table = (struct lookup_funcinfo *)
3097 bfd_malloc (number_of_functions * sizeof (struct lookup_funcinfo));
3098 if (lookup_funcinfo_table == NULL)
3099 return false;
3101 /* Populate the function info lookup table. */
3102 func_index = number_of_functions;
3103 for (each = unit->function_table; each; each = each->prev_func)
3105 entry = &lookup_funcinfo_table[--func_index];
3106 entry->funcinfo = each;
3107 entry->idx = func_index;
3109 /* Calculate the lowest and highest address for this function entry. */
3110 low_addr = entry->funcinfo->arange.low;
3111 high_addr = entry->funcinfo->arange.high;
3113 for (range = entry->funcinfo->arange.next; range; range = range->next)
3115 if (range->low < low_addr)
3116 low_addr = range->low;
3117 if (range->high > high_addr)
3118 high_addr = range->high;
3121 entry->low_addr = low_addr;
3122 entry->high_addr = high_addr;
3125 BFD_ASSERT (func_index == 0);
3127 /* Sort the function by address. */
3128 qsort (lookup_funcinfo_table,
3129 number_of_functions,
3130 sizeof (struct lookup_funcinfo),
3131 compare_lookup_funcinfos);
3133 /* Calculate the high watermark for each function in the lookup table. */
3134 high_addr = lookup_funcinfo_table[0].high_addr;
3135 for (func_index = 1; func_index < number_of_functions; func_index++)
3137 entry = &lookup_funcinfo_table[func_index];
3138 if (entry->high_addr > high_addr)
3139 high_addr = entry->high_addr;
3140 else
3141 entry->high_addr = high_addr;
3144 unit->lookup_funcinfo_table = lookup_funcinfo_table;
3145 return true;
3148 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
3149 TRUE. Note that we need to find the function that has the smallest range
3150 that contains ADDR, to handle inlined functions without depending upon
3151 them being ordered in TABLE by increasing range. */
3153 static bool
3154 lookup_address_in_function_table (struct comp_unit *unit,
3155 bfd_vma addr,
3156 struct funcinfo **function_ptr)
3158 unsigned int number_of_functions = unit->number_of_functions;
3159 struct lookup_funcinfo* lookup_funcinfo = NULL;
3160 struct funcinfo* funcinfo = NULL;
3161 struct funcinfo* best_fit = NULL;
3162 bfd_vma best_fit_len = 0;
3163 bfd_size_type low, high, mid, first;
3164 struct arange *arange;
3166 if (number_of_functions == 0)
3167 return false;
3169 if (!build_lookup_funcinfo_table (unit))
3170 return false;
3172 if (unit->lookup_funcinfo_table[number_of_functions - 1].high_addr < addr)
3173 return false;
3175 /* Find the first function in the lookup table which may contain the
3176 specified address. */
3177 low = 0;
3178 high = number_of_functions;
3179 first = high;
3180 while (low < high)
3182 mid = (low + high) / 2;
3183 lookup_funcinfo = &unit->lookup_funcinfo_table[mid];
3184 if (addr < lookup_funcinfo->low_addr)
3185 high = mid;
3186 else if (addr >= lookup_funcinfo->high_addr)
3187 low = mid + 1;
3188 else
3189 high = first = mid;
3192 /* Find the 'best' match for the address. The prior algorithm defined the
3193 best match as the function with the smallest address range containing
3194 the specified address. This definition should probably be changed to the
3195 innermost inline routine containing the address, but right now we want
3196 to get the same results we did before. */
3197 while (first < number_of_functions)
3199 if (addr < unit->lookup_funcinfo_table[first].low_addr)
3200 break;
3201 funcinfo = unit->lookup_funcinfo_table[first].funcinfo;
3203 for (arange = &funcinfo->arange; arange; arange = arange->next)
3205 if (addr < arange->low || addr >= arange->high)
3206 continue;
3208 if (!best_fit
3209 || arange->high - arange->low < best_fit_len
3210 /* The following comparison is designed to return the same
3211 match as the previous algorithm for routines which have the
3212 same best fit length. */
3213 || (arange->high - arange->low == best_fit_len
3214 && funcinfo > best_fit))
3216 best_fit = funcinfo;
3217 best_fit_len = arange->high - arange->low;
3221 first++;
3224 if (!best_fit)
3225 return false;
3227 *function_ptr = best_fit;
3228 return true;
3231 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
3232 and LINENUMBER_PTR, and return TRUE. */
3234 static bool
3235 lookup_symbol_in_function_table (struct comp_unit *unit,
3236 asymbol *sym,
3237 bfd_vma addr,
3238 const char **filename_ptr,
3239 unsigned int *linenumber_ptr)
3241 struct funcinfo* each_func;
3242 struct funcinfo* best_fit = NULL;
3243 bfd_vma best_fit_len = 0;
3244 struct arange *arange;
3245 const char *name = bfd_asymbol_name (sym);
3246 asection *sec = bfd_asymbol_section (sym);
3248 for (each_func = unit->function_table;
3249 each_func;
3250 each_func = each_func->prev_func)
3252 for (arange = &each_func->arange;
3253 arange;
3254 arange = arange->next)
3256 if ((!each_func->sec || each_func->sec == sec)
3257 && addr >= arange->low
3258 && addr < arange->high
3259 && each_func->name
3260 && strcmp (name, each_func->name) == 0
3261 && (!best_fit
3262 || arange->high - arange->low < best_fit_len))
3264 best_fit = each_func;
3265 best_fit_len = arange->high - arange->low;
3270 if (best_fit)
3272 best_fit->sec = sec;
3273 *filename_ptr = best_fit->file;
3274 *linenumber_ptr = best_fit->line;
3275 return true;
3277 else
3278 return false;
3281 /* Variable table functions. */
3283 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
3284 LINENUMBER_PTR, and return TRUE. */
3286 static bool
3287 lookup_symbol_in_variable_table (struct comp_unit *unit,
3288 asymbol *sym,
3289 bfd_vma addr,
3290 const char **filename_ptr,
3291 unsigned int *linenumber_ptr)
3293 const char *name = bfd_asymbol_name (sym);
3294 asection *sec = bfd_asymbol_section (sym);
3295 struct varinfo* each;
3297 for (each = unit->variable_table; each; each = each->prev_var)
3298 if (! each->stack
3299 && each->file != NULL
3300 && each->name != NULL
3301 && each->addr == addr
3302 && (!each->sec || each->sec == sec)
3303 && strcmp (name, each->name) == 0)
3304 break;
3306 if (each)
3308 each->sec = sec;
3309 *filename_ptr = each->file;
3310 *linenumber_ptr = each->line;
3311 return true;
3314 return false;
3317 static struct comp_unit *stash_comp_unit (struct dwarf2_debug *,
3318 struct dwarf2_debug_file *);
3319 static bool comp_unit_maybe_decode_line_info (struct comp_unit *);
3321 static bool
3322 find_abstract_instance (struct comp_unit *unit,
3323 struct attribute *attr_ptr,
3324 unsigned int recur_count,
3325 const char **pname,
3326 bool *is_linkage,
3327 char **filename_ptr,
3328 int *linenumber_ptr)
3330 bfd *abfd = unit->abfd;
3331 bfd_byte *info_ptr = NULL;
3332 bfd_byte *info_ptr_end;
3333 unsigned int abbrev_number, i;
3334 struct abbrev_info *abbrev;
3335 uint64_t die_ref = attr_ptr->u.val;
3336 struct attribute attr;
3337 const char *name = NULL;
3339 if (recur_count == 100)
3341 _bfd_error_handler
3342 (_("DWARF error: abstract instance recursion detected"));
3343 bfd_set_error (bfd_error_bad_value);
3344 return false;
3347 /* DW_FORM_ref_addr can reference an entry in a different CU. It
3348 is an offset from the .debug_info section, not the current CU. */
3349 if (attr_ptr->form == DW_FORM_ref_addr)
3351 /* We only support DW_FORM_ref_addr within the same file, so
3352 any relocations should be resolved already. Check this by
3353 testing for a zero die_ref; There can't be a valid reference
3354 to the header of a .debug_info section.
3355 DW_FORM_ref_addr is an offset relative to .debug_info.
3356 Normally when using the GNU linker this is accomplished by
3357 emitting a symbolic reference to a label, because .debug_info
3358 sections are linked at zero. When there are multiple section
3359 groups containing .debug_info, as there might be in a
3360 relocatable object file, it would be reasonable to assume that
3361 a symbolic reference to a label in any .debug_info section
3362 might be used. Since we lay out multiple .debug_info
3363 sections at non-zero VMAs (see place_sections), and read
3364 them contiguously into dwarf_info_buffer, that means the
3365 reference is relative to dwarf_info_buffer. */
3366 size_t total;
3368 info_ptr = unit->file->dwarf_info_buffer;
3369 info_ptr_end = info_ptr + unit->file->dwarf_info_size;
3370 total = info_ptr_end - info_ptr;
3371 if (!die_ref)
3372 return true;
3373 else if (die_ref >= total)
3375 _bfd_error_handler
3376 (_("DWARF error: invalid abstract instance DIE ref"));
3377 bfd_set_error (bfd_error_bad_value);
3378 return false;
3380 info_ptr += die_ref;
3382 else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
3384 bool first_time = unit->stash->alt.dwarf_info_buffer == NULL;
3386 info_ptr = read_alt_indirect_ref (unit, die_ref);
3387 if (first_time)
3388 unit->stash->alt.info_ptr = unit->stash->alt.dwarf_info_buffer;
3389 if (info_ptr == NULL)
3391 _bfd_error_handler
3392 (_("DWARF error: unable to read alt ref %" PRIu64),
3393 (uint64_t) die_ref);
3394 bfd_set_error (bfd_error_bad_value);
3395 return false;
3397 info_ptr_end = (unit->stash->alt.dwarf_info_buffer
3398 + unit->stash->alt.dwarf_info_size);
3399 if (unit->stash->alt.all_comp_units)
3400 unit = unit->stash->alt.all_comp_units;
3403 if (attr_ptr->form == DW_FORM_ref_addr
3404 || attr_ptr->form == DW_FORM_GNU_ref_alt)
3406 /* Now find the CU containing this pointer. */
3407 if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr)
3408 info_ptr_end = unit->end_ptr;
3409 else
3411 /* Check other CUs to see if they contain the abbrev. */
3412 struct comp_unit *u;
3414 for (u = unit->prev_unit; u != NULL; u = u->prev_unit)
3415 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3416 break;
3418 if (u == NULL)
3419 for (u = unit->next_unit; u != NULL; u = u->next_unit)
3420 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3421 break;
3423 if (attr_ptr->form == DW_FORM_ref_addr)
3424 while (u == NULL)
3426 u = stash_comp_unit (unit->stash, &unit->stash->f);
3427 if (u == NULL)
3428 break;
3429 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3430 break;
3431 u = NULL;
3434 if (attr_ptr->form == DW_FORM_GNU_ref_alt)
3435 while (u == NULL)
3437 u = stash_comp_unit (unit->stash, &unit->stash->alt);
3438 if (u == NULL)
3439 break;
3440 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3441 break;
3442 u = NULL;
3445 if (u == NULL)
3447 _bfd_error_handler
3448 (_("DWARF error: unable to locate abstract instance DIE ref %"
3449 PRIu64), (uint64_t) die_ref);
3450 bfd_set_error (bfd_error_bad_value);
3451 return false;
3453 unit = u;
3454 info_ptr_end = unit->end_ptr;
3457 else
3459 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
3460 DW_FORM_ref_udata. These are all references relative to the
3461 start of the current CU. */
3462 size_t total;
3464 info_ptr = unit->info_ptr_unit;
3465 info_ptr_end = unit->end_ptr;
3466 total = info_ptr_end - info_ptr;
3467 if (!die_ref || die_ref >= total)
3469 _bfd_error_handler
3470 (_("DWARF error: invalid abstract instance DIE ref"));
3471 bfd_set_error (bfd_error_bad_value);
3472 return false;
3474 info_ptr += die_ref;
3477 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
3478 false, info_ptr_end);
3479 if (abbrev_number)
3481 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3482 if (! abbrev)
3484 _bfd_error_handler
3485 (_("DWARF error: could not find abbrev number %u"), abbrev_number);
3486 bfd_set_error (bfd_error_bad_value);
3487 return false;
3489 else
3491 for (i = 0; i < abbrev->num_attrs; ++i)
3493 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
3494 info_ptr, info_ptr_end);
3495 if (info_ptr == NULL)
3496 break;
3497 switch (attr.name)
3499 case DW_AT_name:
3500 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3501 over DW_AT_name. */
3502 if (name == NULL && is_str_form (&attr))
3504 name = attr.u.str;
3505 if (non_mangled (unit->lang))
3506 *is_linkage = true;
3508 break;
3509 case DW_AT_specification:
3510 if (is_int_form (&attr)
3511 && !find_abstract_instance (unit, &attr, recur_count + 1,
3512 &name, is_linkage,
3513 filename_ptr, linenumber_ptr))
3514 return false;
3515 break;
3516 case DW_AT_linkage_name:
3517 case DW_AT_MIPS_linkage_name:
3518 /* PR 16949: Corrupt debug info can place
3519 non-string forms into these attributes. */
3520 if (is_str_form (&attr))
3522 name = attr.u.str;
3523 *is_linkage = true;
3525 break;
3526 case DW_AT_decl_file:
3527 if (!comp_unit_maybe_decode_line_info (unit))
3528 return false;
3529 if (is_int_form (&attr))
3530 *filename_ptr = concat_filename (unit->line_table,
3531 attr.u.val);
3532 break;
3533 case DW_AT_decl_line:
3534 if (is_int_form (&attr))
3535 *linenumber_ptr = attr.u.val;
3536 break;
3537 default:
3538 break;
3543 *pname = name;
3544 return true;
3547 static bool
3548 read_ranges (struct comp_unit *unit, struct arange *arange,
3549 struct trie_node **trie_root, uint64_t offset)
3551 bfd_byte *ranges_ptr;
3552 bfd_byte *ranges_end;
3553 bfd_vma base_address = unit->base_address;
3555 if (! unit->file->dwarf_ranges_buffer)
3557 if (! read_debug_ranges (unit))
3558 return false;
3561 if (offset > unit->file->dwarf_ranges_size)
3562 return false;
3563 ranges_ptr = unit->file->dwarf_ranges_buffer + offset;
3564 ranges_end = unit->file->dwarf_ranges_buffer + unit->file->dwarf_ranges_size;
3566 for (;;)
3568 bfd_vma low_pc;
3569 bfd_vma high_pc;
3571 /* PR 17512: file: 62cada7d. */
3572 if (2u * unit->addr_size > (size_t) (ranges_end - ranges_ptr))
3573 return false;
3575 low_pc = read_address (unit, &ranges_ptr, ranges_end);
3576 high_pc = read_address (unit, &ranges_ptr, ranges_end);
3578 if (low_pc == 0 && high_pc == 0)
3579 break;
3580 if (low_pc == -1UL && high_pc != -1UL)
3581 base_address = high_pc;
3582 else
3584 if (!arange_add (unit, arange, trie_root,
3585 base_address + low_pc, base_address + high_pc))
3586 return false;
3589 return true;
3592 static bool
3593 read_rnglists (struct comp_unit *unit, struct arange *arange,
3594 struct trie_node **trie_root, uint64_t offset)
3596 bfd_byte *rngs_ptr;
3597 bfd_byte *rngs_end;
3598 bfd_vma base_address = unit->base_address;
3599 bfd_vma low_pc;
3600 bfd_vma high_pc;
3601 bfd *abfd = unit->abfd;
3603 if (! unit->file->dwarf_rnglists_buffer)
3605 if (! read_debug_rnglists (unit))
3606 return false;
3609 rngs_ptr = unit->file->dwarf_rnglists_buffer + offset;
3610 if (rngs_ptr < unit->file->dwarf_rnglists_buffer)
3611 return false;
3612 rngs_end = unit->file->dwarf_rnglists_buffer;
3613 rngs_end += unit->file->dwarf_rnglists_size;
3615 for (;;)
3617 enum dwarf_range_list_entry rlet;
3619 if (rngs_ptr >= rngs_end)
3620 return false;
3622 rlet = read_1_byte (abfd, &rngs_ptr, rngs_end);
3624 switch (rlet)
3626 case DW_RLE_end_of_list:
3627 return true;
3629 case DW_RLE_base_address:
3630 if (unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3631 return false;
3632 base_address = read_address (unit, &rngs_ptr, rngs_end);
3633 continue;
3635 case DW_RLE_start_length:
3636 if (unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3637 return false;
3638 low_pc = read_address (unit, &rngs_ptr, rngs_end);
3639 high_pc = low_pc;
3640 high_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3641 false, rngs_end);
3642 break;
3644 case DW_RLE_offset_pair:
3645 low_pc = base_address;
3646 low_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3647 false, rngs_end);
3648 high_pc = base_address;
3649 high_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3650 false, rngs_end);
3651 break;
3653 case DW_RLE_start_end:
3654 if (2u * unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3655 return false;
3656 low_pc = read_address (unit, &rngs_ptr, rngs_end);
3657 high_pc = read_address (unit, &rngs_ptr, rngs_end);
3658 break;
3660 /* TODO x-variants need .debug_addr support used for split-dwarf. */
3661 case DW_RLE_base_addressx:
3662 case DW_RLE_startx_endx:
3663 case DW_RLE_startx_length:
3664 default:
3665 return false;
3668 if (!arange_add (unit, arange, trie_root, low_pc, high_pc))
3669 return false;
3673 static bool
3674 read_rangelist (struct comp_unit *unit, struct arange *arange,
3675 struct trie_node **trie_root, uint64_t offset)
3677 if (unit->version <= 4)
3678 return read_ranges (unit, arange, trie_root, offset);
3679 else
3680 return read_rnglists (unit, arange, trie_root, offset);
3683 static struct funcinfo *
3684 lookup_func_by_offset (uint64_t offset, struct funcinfo * table)
3686 for (; table != NULL; table = table->prev_func)
3687 if (table->unit_offset == offset)
3688 return table;
3689 return NULL;
3692 static struct varinfo *
3693 lookup_var_by_offset (uint64_t offset, struct varinfo * table)
3695 while (table)
3697 if (table->unit_offset == offset)
3698 return table;
3699 table = table->prev_var;
3702 return NULL;
3706 /* DWARF2 Compilation unit functions. */
3708 static struct funcinfo *
3709 reverse_funcinfo_list (struct funcinfo *head)
3711 struct funcinfo *rhead;
3712 struct funcinfo *temp;
3714 for (rhead = NULL; head; head = temp)
3716 temp = head->prev_func;
3717 head->prev_func = rhead;
3718 rhead = head;
3720 return rhead;
3723 static struct varinfo *
3724 reverse_varinfo_list (struct varinfo *head)
3726 struct varinfo *rhead;
3727 struct varinfo *temp;
3729 for (rhead = NULL; head; head = temp)
3731 temp = head->prev_var;
3732 head->prev_var = rhead;
3733 rhead = head;
3735 return rhead;
3738 /* Scan over each die in a comp. unit looking for functions to add
3739 to the function table and variables to the variable table. */
3741 static bool
3742 scan_unit_for_symbols (struct comp_unit *unit)
3744 bfd *abfd = unit->abfd;
3745 bfd_byte *info_ptr = unit->first_child_die_ptr;
3746 bfd_byte *info_ptr_end = unit->end_ptr;
3747 int nesting_level = 0;
3748 struct nest_funcinfo
3750 struct funcinfo *func;
3751 } *nested_funcs;
3752 int nested_funcs_size;
3753 struct funcinfo *last_func;
3754 struct varinfo *last_var;
3756 /* Maintain a stack of in-scope functions and inlined functions, which we
3757 can use to set the caller_func field. */
3758 nested_funcs_size = 32;
3759 nested_funcs = (struct nest_funcinfo *)
3760 bfd_malloc (nested_funcs_size * sizeof (*nested_funcs));
3761 if (nested_funcs == NULL)
3762 return false;
3763 nested_funcs[nesting_level].func = 0;
3765 /* PR 27484: We must scan the DIEs twice. The first time we look for
3766 function and variable tags and accumulate them into their respective
3767 tables. The second time through we process the attributes of the
3768 functions/variables and augment the table entries. */
3769 while (nesting_level >= 0)
3771 unsigned int abbrev_number, i;
3772 struct abbrev_info *abbrev;
3773 struct funcinfo *func;
3774 struct varinfo *var;
3775 uint64_t current_offset;
3777 /* PR 17512: file: 9f405d9d. */
3778 if (info_ptr >= info_ptr_end)
3779 goto fail;
3781 current_offset = info_ptr - unit->info_ptr_unit;
3782 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
3783 false, info_ptr_end);
3784 if (abbrev_number == 0)
3786 nesting_level--;
3787 continue;
3790 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3791 if (! abbrev)
3793 static unsigned int previous_failed_abbrev = -1U;
3795 /* Avoid multiple reports of the same missing abbrev. */
3796 if (abbrev_number != previous_failed_abbrev)
3798 _bfd_error_handler
3799 (_("DWARF error: could not find abbrev number %u"),
3800 abbrev_number);
3801 previous_failed_abbrev = abbrev_number;
3803 bfd_set_error (bfd_error_bad_value);
3804 goto fail;
3807 if (abbrev->tag == DW_TAG_subprogram
3808 || abbrev->tag == DW_TAG_entry_point
3809 || abbrev->tag == DW_TAG_inlined_subroutine)
3811 size_t amt = sizeof (struct funcinfo);
3813 var = NULL;
3814 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
3815 if (func == NULL)
3816 goto fail;
3817 func->tag = abbrev->tag;
3818 func->prev_func = unit->function_table;
3819 func->unit_offset = current_offset;
3820 unit->function_table = func;
3821 unit->number_of_functions++;
3822 BFD_ASSERT (!unit->cached);
3824 if (func->tag == DW_TAG_inlined_subroutine)
3825 for (i = nesting_level; i-- != 0; )
3826 if (nested_funcs[i].func)
3828 func->caller_func = nested_funcs[i].func;
3829 break;
3831 nested_funcs[nesting_level].func = func;
3833 else
3835 func = NULL;
3836 if (abbrev->tag == DW_TAG_variable
3837 || abbrev->tag == DW_TAG_member)
3839 size_t amt = sizeof (struct varinfo);
3841 var = (struct varinfo *) bfd_zalloc (abfd, amt);
3842 if (var == NULL)
3843 goto fail;
3844 var->tag = abbrev->tag;
3845 var->stack = true;
3846 var->prev_var = unit->variable_table;
3847 unit->variable_table = var;
3848 var->unit_offset = current_offset;
3849 /* PR 18205: Missing debug information can cause this
3850 var to be attached to an already cached unit. */
3852 else
3853 var = NULL;
3855 /* No inline function in scope at this nesting level. */
3856 nested_funcs[nesting_level].func = 0;
3859 for (i = 0; i < abbrev->num_attrs; ++i)
3861 struct attribute attr;
3863 info_ptr = read_attribute (&attr, &abbrev->attrs[i],
3864 unit, info_ptr, info_ptr_end);
3865 if (info_ptr == NULL)
3866 goto fail;
3869 if (abbrev->has_children)
3871 nesting_level++;
3873 if (nesting_level >= nested_funcs_size)
3875 struct nest_funcinfo *tmp;
3877 nested_funcs_size *= 2;
3878 tmp = (struct nest_funcinfo *)
3879 bfd_realloc (nested_funcs,
3880 nested_funcs_size * sizeof (*nested_funcs));
3881 if (tmp == NULL)
3882 goto fail;
3883 nested_funcs = tmp;
3885 nested_funcs[nesting_level].func = 0;
3889 unit->function_table = reverse_funcinfo_list (unit->function_table);
3890 unit->variable_table = reverse_varinfo_list (unit->variable_table);
3892 /* This is the second pass over the abbrevs. */
3893 info_ptr = unit->first_child_die_ptr;
3894 nesting_level = 0;
3896 last_func = NULL;
3897 last_var = NULL;
3899 while (nesting_level >= 0)
3901 unsigned int abbrev_number, i;
3902 struct abbrev_info *abbrev;
3903 struct attribute attr;
3904 struct funcinfo *func;
3905 struct varinfo *var;
3906 bfd_vma low_pc = 0;
3907 bfd_vma high_pc = 0;
3908 bool high_pc_relative = false;
3909 uint64_t current_offset;
3911 /* PR 17512: file: 9f405d9d. */
3912 if (info_ptr >= info_ptr_end)
3913 goto fail;
3915 current_offset = info_ptr - unit->info_ptr_unit;
3916 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
3917 false, info_ptr_end);
3918 if (! abbrev_number)
3920 nesting_level--;
3921 continue;
3924 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3925 /* This should have been handled above. */
3926 BFD_ASSERT (abbrev != NULL);
3928 func = NULL;
3929 var = NULL;
3930 if (abbrev->tag == DW_TAG_subprogram
3931 || abbrev->tag == DW_TAG_entry_point
3932 || abbrev->tag == DW_TAG_inlined_subroutine)
3934 if (last_func
3935 && last_func->prev_func
3936 && last_func->prev_func->unit_offset == current_offset)
3937 func = last_func->prev_func;
3938 else
3939 func = lookup_func_by_offset (current_offset, unit->function_table);
3941 if (func == NULL)
3942 goto fail;
3944 last_func = func;
3946 else if (abbrev->tag == DW_TAG_variable
3947 || abbrev->tag == DW_TAG_member)
3949 if (last_var
3950 && last_var->prev_var
3951 && last_var->prev_var->unit_offset == current_offset)
3952 var = last_var->prev_var;
3953 else
3954 var = lookup_var_by_offset (current_offset, unit->variable_table);
3956 if (var == NULL)
3957 goto fail;
3959 last_var = var;
3962 for (i = 0; i < abbrev->num_attrs; ++i)
3964 info_ptr = read_attribute (&attr, &abbrev->attrs[i],
3965 unit, info_ptr, info_ptr_end);
3966 if (info_ptr == NULL)
3967 goto fail;
3969 if (func)
3971 switch (attr.name)
3973 case DW_AT_call_file:
3974 if (is_int_form (&attr))
3975 func->caller_file = concat_filename (unit->line_table,
3976 attr.u.val);
3977 break;
3979 case DW_AT_call_line:
3980 if (is_int_form (&attr))
3981 func->caller_line = attr.u.val;
3982 break;
3984 case DW_AT_abstract_origin:
3985 case DW_AT_specification:
3986 if (is_int_form (&attr)
3987 && !find_abstract_instance (unit, &attr, 0,
3988 &func->name,
3989 &func->is_linkage,
3990 &func->file,
3991 &func->line))
3992 goto fail;
3993 break;
3995 case DW_AT_name:
3996 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3997 over DW_AT_name. */
3998 if (func->name == NULL && is_str_form (&attr))
4000 func->name = attr.u.str;
4001 if (non_mangled (unit->lang))
4002 func->is_linkage = true;
4004 break;
4006 case DW_AT_linkage_name:
4007 case DW_AT_MIPS_linkage_name:
4008 /* PR 16949: Corrupt debug info can place
4009 non-string forms into these attributes. */
4010 if (is_str_form (&attr))
4012 func->name = attr.u.str;
4013 func->is_linkage = true;
4015 break;
4017 case DW_AT_low_pc:
4018 if (is_int_form (&attr))
4019 low_pc = attr.u.val;
4020 break;
4022 case DW_AT_high_pc:
4023 if (is_int_form (&attr))
4025 high_pc = attr.u.val;
4026 high_pc_relative = attr.form != DW_FORM_addr;
4028 break;
4030 case DW_AT_ranges:
4031 if (is_int_form (&attr)
4032 && !read_rangelist (unit, &func->arange,
4033 &unit->file->trie_root, attr.u.val))
4034 goto fail;
4035 break;
4037 case DW_AT_decl_file:
4038 if (is_int_form (&attr))
4039 func->file = concat_filename (unit->line_table,
4040 attr.u.val);
4041 break;
4043 case DW_AT_decl_line:
4044 if (is_int_form (&attr))
4045 func->line = attr.u.val;
4046 break;
4048 default:
4049 break;
4052 else if (var)
4054 switch (attr.name)
4056 case DW_AT_specification:
4057 if (is_int_form (&attr) && attr.u.val)
4059 struct varinfo * spec_var;
4061 spec_var = lookup_var_by_offset (attr.u.val,
4062 unit->variable_table);
4063 if (spec_var == NULL)
4065 _bfd_error_handler (_("DWARF error: could not find "
4066 "variable specification "
4067 "at offset 0x%lx"),
4068 (unsigned long) attr.u.val);
4069 break;
4072 if (var->name == NULL)
4073 var->name = spec_var->name;
4074 if (var->file == NULL && spec_var->file != NULL)
4075 var->file = strdup (spec_var->file);
4076 if (var->line == 0)
4077 var->line = spec_var->line;
4078 if (var->sec == NULL)
4079 var->sec = spec_var->sec;
4081 break;
4083 case DW_AT_name:
4084 if (is_str_form (&attr))
4085 var->name = attr.u.str;
4086 break;
4088 case DW_AT_decl_file:
4089 if (is_int_form (&attr))
4090 var->file = concat_filename (unit->line_table,
4091 attr.u.val);
4092 break;
4094 case DW_AT_decl_line:
4095 if (is_int_form (&attr))
4096 var->line = attr.u.val;
4097 break;
4099 case DW_AT_external:
4100 if (is_int_form (&attr) && attr.u.val != 0)
4101 var->stack = false;
4102 break;
4104 case DW_AT_location:
4105 switch (attr.form)
4107 case DW_FORM_block:
4108 case DW_FORM_block1:
4109 case DW_FORM_block2:
4110 case DW_FORM_block4:
4111 case DW_FORM_exprloc:
4112 if (attr.u.blk->data != NULL
4113 && *attr.u.blk->data == DW_OP_addr)
4115 var->stack = false;
4117 /* Verify that DW_OP_addr is the only opcode in the
4118 location, in which case the block size will be 1
4119 plus the address size. */
4120 /* ??? For TLS variables, gcc can emit
4121 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
4122 which we don't handle here yet. */
4123 if (attr.u.blk->size == unit->addr_size + 1U)
4124 var->addr = bfd_get (unit->addr_size * 8,
4125 unit->abfd,
4126 attr.u.blk->data + 1);
4128 break;
4130 default:
4131 break;
4133 break;
4135 default:
4136 break;
4141 if (abbrev->has_children)
4142 nesting_level++;
4144 if (high_pc_relative)
4145 high_pc += low_pc;
4147 if (func && high_pc != 0)
4149 if (!arange_add (unit, &func->arange, &unit->file->trie_root,
4150 low_pc, high_pc))
4151 goto fail;
4155 unit->function_table = reverse_funcinfo_list (unit->function_table);
4156 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4158 free (nested_funcs);
4159 return true;
4161 fail:
4162 free (nested_funcs);
4163 return false;
4166 /* Read the attributes of the form strx and addrx. */
4168 static void
4169 reread_attribute (struct comp_unit *unit,
4170 struct attribute *attr,
4171 bfd_vma *low_pc,
4172 bfd_vma *high_pc,
4173 bool *high_pc_relative,
4174 bool compunit)
4176 if (is_strx_form (attr->form))
4177 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
4178 if (is_addrx_form (attr->form))
4179 attr->u.val = read_indexed_address (attr->u.val, unit);
4181 switch (attr->name)
4183 case DW_AT_stmt_list:
4184 unit->stmtlist = 1;
4185 unit->line_offset = attr->u.val;
4186 break;
4188 case DW_AT_name:
4189 if (is_str_form (attr))
4190 unit->name = attr->u.str;
4191 break;
4193 case DW_AT_low_pc:
4194 *low_pc = attr->u.val;
4195 if (compunit)
4196 unit->base_address = *low_pc;
4197 break;
4199 case DW_AT_high_pc:
4200 *high_pc = attr->u.val;
4201 *high_pc_relative = attr->form != DW_FORM_addr;
4202 break;
4204 case DW_AT_ranges:
4205 if (!read_rangelist (unit, &unit->arange,
4206 &unit->file->trie_root, attr->u.val))
4207 return;
4208 break;
4210 case DW_AT_comp_dir:
4212 char *comp_dir = attr->u.str;
4214 if (!is_str_form (attr))
4216 _bfd_error_handler
4217 (_("DWARF error: DW_AT_comp_dir attribute encountered "
4218 "with a non-string form"));
4219 comp_dir = NULL;
4222 if (comp_dir)
4224 char *cp = strchr (comp_dir, ':');
4226 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
4227 comp_dir = cp + 1;
4229 unit->comp_dir = comp_dir;
4230 break;
4233 case DW_AT_language:
4234 unit->lang = attr->u.val;
4235 default:
4236 break;
4240 /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
4241 includes the compilation unit header that proceeds the DIE's, but
4242 does not include the length field that precedes each compilation
4243 unit header. END_PTR points one past the end of this comp unit.
4244 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
4246 This routine does not read the whole compilation unit; only enough
4247 to get to the line number information for the compilation unit. */
4249 static struct comp_unit *
4250 parse_comp_unit (struct dwarf2_debug *stash,
4251 struct dwarf2_debug_file *file,
4252 bfd_byte *info_ptr,
4253 bfd_vma unit_length,
4254 bfd_byte *info_ptr_unit,
4255 unsigned int offset_size)
4257 struct comp_unit* unit;
4258 unsigned int version;
4259 uint64_t abbrev_offset = 0;
4260 /* Initialize it just to avoid a GCC false warning. */
4261 unsigned int addr_size = -1;
4262 struct abbrev_info** abbrevs;
4263 unsigned int abbrev_number, i;
4264 struct abbrev_info *abbrev;
4265 struct attribute attr;
4266 bfd_byte *end_ptr = info_ptr + unit_length;
4267 size_t amt;
4268 bfd_vma low_pc = 0;
4269 bfd_vma high_pc = 0;
4270 bfd *abfd = file->bfd_ptr;
4271 bool high_pc_relative = false;
4272 enum dwarf_unit_type unit_type;
4273 struct attribute *str_addrp = NULL;
4274 size_t str_count = 0;
4275 size_t str_alloc = 0;
4276 bool compunit_flag = false;
4278 version = read_2_bytes (abfd, &info_ptr, end_ptr);
4279 if (version < 2 || version > 5)
4281 /* PR 19872: A version number of 0 probably means that there is padding
4282 at the end of the .debug_info section. Gold puts it there when
4283 performing an incremental link, for example. So do not generate
4284 an error, just return a NULL. */
4285 if (version)
4287 _bfd_error_handler
4288 (_("DWARF error: found dwarf version '%u', this reader"
4289 " only handles version 2, 3, 4 and 5 information"), version);
4290 bfd_set_error (bfd_error_bad_value);
4292 return NULL;
4295 if (version < 5)
4296 unit_type = DW_UT_compile;
4297 else
4299 unit_type = read_1_byte (abfd, &info_ptr, end_ptr);
4300 addr_size = read_1_byte (abfd, &info_ptr, end_ptr);
4303 BFD_ASSERT (offset_size == 4 || offset_size == 8);
4304 if (offset_size == 4)
4305 abbrev_offset = read_4_bytes (abfd, &info_ptr, end_ptr);
4306 else
4307 abbrev_offset = read_8_bytes (abfd, &info_ptr, end_ptr);
4309 if (version < 5)
4310 addr_size = read_1_byte (abfd, &info_ptr, end_ptr);
4312 if (unit_type == DW_UT_type)
4314 /* Skip type signature. */
4315 info_ptr += 8;
4317 /* Skip type offset. */
4318 info_ptr += offset_size;
4321 if (addr_size > sizeof (bfd_vma))
4323 _bfd_error_handler
4324 /* xgettext: c-format */
4325 (_("DWARF error: found address size '%u', this reader"
4326 " can not handle sizes greater than '%u'"),
4327 addr_size,
4328 (unsigned int) sizeof (bfd_vma));
4329 bfd_set_error (bfd_error_bad_value);
4330 return NULL;
4333 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
4335 _bfd_error_handler
4336 ("DWARF error: found address size '%u', this reader"
4337 " can only handle address sizes '2', '4' and '8'", addr_size);
4338 bfd_set_error (bfd_error_bad_value);
4339 return NULL;
4342 /* Read the abbrevs for this compilation unit into a table. */
4343 abbrevs = read_abbrevs (abfd, abbrev_offset, stash, file);
4344 if (! abbrevs)
4345 return NULL;
4347 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
4348 false, end_ptr);
4349 if (! abbrev_number)
4351 /* PR 19872: An abbrev number of 0 probably means that there is padding
4352 at the end of the .debug_abbrev section. Gold puts it there when
4353 performing an incremental link, for example. So do not generate
4354 an error, just return a NULL. */
4355 return NULL;
4358 abbrev = lookup_abbrev (abbrev_number, abbrevs);
4359 if (! abbrev)
4361 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
4362 abbrev_number);
4363 bfd_set_error (bfd_error_bad_value);
4364 return NULL;
4367 amt = sizeof (struct comp_unit);
4368 unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
4369 if (unit == NULL)
4370 return NULL;
4371 unit->abfd = abfd;
4372 unit->version = version;
4373 unit->addr_size = addr_size;
4374 unit->offset_size = offset_size;
4375 unit->abbrevs = abbrevs;
4376 unit->end_ptr = end_ptr;
4377 unit->stash = stash;
4378 unit->file = file;
4379 unit->info_ptr_unit = info_ptr_unit;
4381 if (abbrev->tag == DW_TAG_compile_unit)
4382 compunit_flag = true;
4384 for (i = 0; i < abbrev->num_attrs; ++i)
4386 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, end_ptr);
4387 if (info_ptr == NULL)
4388 goto err_exit;
4390 /* Identify attributes of the form strx* and addrx* which come before
4391 DW_AT_str_offsets_base and DW_AT_addr_base respectively in the CU.
4392 Store the attributes in an array and process them later. */
4393 if ((unit->dwarf_str_offset == 0 && is_strx_form (attr.form))
4394 || (unit->dwarf_addr_offset == 0 && is_addrx_form (attr.form)))
4396 if (str_count <= str_alloc)
4398 str_alloc = 2 * str_alloc + 200;
4399 str_addrp = bfd_realloc (str_addrp,
4400 str_alloc * sizeof (*str_addrp));
4401 if (str_addrp == NULL)
4402 goto err_exit;
4404 str_addrp[str_count] = attr;
4405 str_count++;
4406 continue;
4409 /* Store the data if it is of an attribute we want to keep in a
4410 partial symbol table. */
4411 switch (attr.name)
4413 case DW_AT_stmt_list:
4414 if (is_int_form (&attr))
4416 unit->stmtlist = 1;
4417 unit->line_offset = attr.u.val;
4419 break;
4421 case DW_AT_name:
4422 if (is_str_form (&attr))
4423 unit->name = attr.u.str;
4424 break;
4426 case DW_AT_low_pc:
4427 if (is_int_form (&attr))
4429 low_pc = attr.u.val;
4430 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
4431 this is the base address to use when reading location
4432 lists or range lists. */
4433 if (compunit_flag)
4434 unit->base_address = low_pc;
4436 break;
4438 case DW_AT_high_pc:
4439 if (is_int_form (&attr))
4441 high_pc = attr.u.val;
4442 high_pc_relative = attr.form != DW_FORM_addr;
4444 break;
4446 case DW_AT_ranges:
4447 if (is_int_form (&attr)
4448 && !read_rangelist (unit, &unit->arange,
4449 &unit->file->trie_root, attr.u.val))
4450 goto err_exit;
4451 break;
4453 case DW_AT_comp_dir:
4455 char *comp_dir = attr.u.str;
4457 /* PR 17512: file: 1fe726be. */
4458 if (!is_str_form (&attr))
4460 _bfd_error_handler
4461 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
4462 comp_dir = NULL;
4465 if (comp_dir)
4467 /* Irix 6.2 native cc prepends <machine>.: to the compilation
4468 directory, get rid of it. */
4469 char *cp = strchr (comp_dir, ':');
4471 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
4472 comp_dir = cp + 1;
4474 unit->comp_dir = comp_dir;
4475 break;
4478 case DW_AT_language:
4479 if (is_int_form (&attr))
4480 unit->lang = attr.u.val;
4481 break;
4483 case DW_AT_addr_base:
4484 unit->dwarf_addr_offset = attr.u.val;
4485 break;
4487 case DW_AT_str_offsets_base:
4488 unit->dwarf_str_offset = attr.u.val;
4489 break;
4491 default:
4492 break;
4496 for (i = 0; i < str_count; ++i)
4497 reread_attribute (unit, &str_addrp[i], &low_pc, &high_pc,
4498 &high_pc_relative, compunit_flag);
4500 if (high_pc_relative)
4501 high_pc += low_pc;
4502 if (high_pc != 0)
4504 if (!arange_add (unit, &unit->arange, &unit->file->trie_root,
4505 low_pc, high_pc))
4506 goto err_exit;
4509 unit->first_child_die_ptr = info_ptr;
4511 free (str_addrp);
4512 return unit;
4514 err_exit:
4515 free (str_addrp);
4516 return NULL;
4519 /* Return TRUE if UNIT may contain the address given by ADDR. When
4520 there are functions written entirely with inline asm statements, the
4521 range info in the compilation unit header may not be correct. We
4522 need to consult the line info table to see if a compilation unit
4523 really contains the given address. */
4525 static bool
4526 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
4528 struct arange *arange;
4530 if (unit->error)
4531 return false;
4533 arange = &unit->arange;
4536 if (addr >= arange->low && addr < arange->high)
4537 return true;
4538 arange = arange->next;
4540 while (arange);
4542 return false;
4545 /* If UNIT contains ADDR, set the output parameters to the values for
4546 the line containing ADDR and return TRUE. Otherwise return FALSE.
4547 The output parameters, FILENAME_PTR, FUNCTION_PTR, and
4548 LINENUMBER_PTR, are pointers to the objects to be filled in. */
4550 static bool
4551 comp_unit_find_nearest_line (struct comp_unit *unit,
4552 bfd_vma addr,
4553 const char **filename_ptr,
4554 struct funcinfo **function_ptr,
4555 unsigned int *linenumber_ptr,
4556 unsigned int *discriminator_ptr)
4558 bool line_p, func_p;
4560 if (!comp_unit_maybe_decode_line_info (unit))
4561 return false;
4563 *function_ptr = NULL;
4564 func_p = lookup_address_in_function_table (unit, addr, function_ptr);
4565 if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine)
4566 unit->stash->inliner_chain = *function_ptr;
4568 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
4569 filename_ptr,
4570 linenumber_ptr,
4571 discriminator_ptr);
4572 return line_p || func_p;
4575 /* Check to see if line info is already decoded in a comp_unit.
4576 If not, decode it. Returns TRUE if no errors were encountered;
4577 FALSE otherwise. */
4579 static bool
4580 comp_unit_maybe_decode_line_info (struct comp_unit *unit)
4582 if (unit->error)
4583 return false;
4585 if (! unit->line_table)
4587 if (! unit->stmtlist)
4589 unit->error = 1;
4590 return false;
4593 unit->line_table = decode_line_info (unit);
4595 if (! unit->line_table)
4597 unit->error = 1;
4598 return false;
4601 if (unit->first_child_die_ptr < unit->end_ptr
4602 && ! scan_unit_for_symbols (unit))
4604 unit->error = 1;
4605 return false;
4609 return true;
4612 /* If UNIT contains SYM at ADDR, set the output parameters to the
4613 values for the line containing SYM. The output parameters,
4614 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
4615 filled in.
4617 Return TRUE if UNIT contains SYM, and no errors were encountered;
4618 FALSE otherwise. */
4620 static bool
4621 comp_unit_find_line (struct comp_unit *unit,
4622 asymbol *sym,
4623 bfd_vma addr,
4624 const char **filename_ptr,
4625 unsigned int *linenumber_ptr)
4627 if (!comp_unit_maybe_decode_line_info (unit))
4628 return false;
4630 if (sym->flags & BSF_FUNCTION)
4631 return lookup_symbol_in_function_table (unit, sym, addr,
4632 filename_ptr,
4633 linenumber_ptr);
4635 return lookup_symbol_in_variable_table (unit, sym, addr,
4636 filename_ptr,
4637 linenumber_ptr);
4640 /* Extract all interesting funcinfos and varinfos of a compilation
4641 unit into hash tables for faster lookup. Returns TRUE if no
4642 errors were enountered; FALSE otherwise. */
4644 static bool
4645 comp_unit_hash_info (struct dwarf2_debug *stash,
4646 struct comp_unit *unit,
4647 struct info_hash_table *funcinfo_hash_table,
4648 struct info_hash_table *varinfo_hash_table)
4650 struct funcinfo* each_func;
4651 struct varinfo* each_var;
4652 bool okay = true;
4654 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
4656 if (!comp_unit_maybe_decode_line_info (unit))
4657 return false;
4659 BFD_ASSERT (!unit->cached);
4661 /* To preserve the original search order, we went to visit the function
4662 infos in the reversed order of the list. However, making the list
4663 bi-directional use quite a bit of extra memory. So we reverse
4664 the list first, traverse the list in the now reversed order and
4665 finally reverse the list again to get back the original order. */
4666 unit->function_table = reverse_funcinfo_list (unit->function_table);
4667 for (each_func = unit->function_table;
4668 each_func && okay;
4669 each_func = each_func->prev_func)
4671 /* Skip nameless functions. */
4672 if (each_func->name)
4673 /* There is no need to copy name string into hash table as
4674 name string is either in the dwarf string buffer or
4675 info in the stash. */
4676 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
4677 (void*) each_func, false);
4679 unit->function_table = reverse_funcinfo_list (unit->function_table);
4680 if (!okay)
4681 return false;
4683 /* We do the same for variable infos. */
4684 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4685 for (each_var = unit->variable_table;
4686 each_var && okay;
4687 each_var = each_var->prev_var)
4689 /* Skip stack vars and vars with no files or names. */
4690 if (! each_var->stack
4691 && each_var->file != NULL
4692 && each_var->name != NULL)
4693 /* There is no need to copy name string into hash table as
4694 name string is either in the dwarf string buffer or
4695 info in the stash. */
4696 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
4697 (void*) each_var, false);
4700 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4701 unit->cached = true;
4702 return okay;
4705 /* Locate a section in a BFD containing debugging info. The search starts
4706 from the section after AFTER_SEC, or from the first section in the BFD if
4707 AFTER_SEC is NULL. The search works by examining the names of the
4708 sections. There are three permissiable names. The first two are given
4709 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
4710 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
4711 This is a variation on the .debug_info section which has a checksum
4712 describing the contents appended onto the name. This allows the linker to
4713 identify and discard duplicate debugging sections for different
4714 compilation units. */
4715 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
4717 static asection *
4718 find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
4719 asection *after_sec)
4721 asection *msec;
4722 const char *look;
4724 if (after_sec == NULL)
4726 look = debug_sections[debug_info].uncompressed_name;
4727 msec = bfd_get_section_by_name (abfd, look);
4728 if (msec != NULL)
4729 return msec;
4731 look = debug_sections[debug_info].compressed_name;
4732 msec = bfd_get_section_by_name (abfd, look);
4733 if (msec != NULL)
4734 return msec;
4736 for (msec = abfd->sections; msec != NULL; msec = msec->next)
4737 if (startswith (msec->name, GNU_LINKONCE_INFO))
4738 return msec;
4740 return NULL;
4743 for (msec = after_sec->next; msec != NULL; msec = msec->next)
4745 look = debug_sections[debug_info].uncompressed_name;
4746 if (strcmp (msec->name, look) == 0)
4747 return msec;
4749 look = debug_sections[debug_info].compressed_name;
4750 if (look != NULL && strcmp (msec->name, look) == 0)
4751 return msec;
4753 if (startswith (msec->name, GNU_LINKONCE_INFO))
4754 return msec;
4757 return NULL;
4760 /* Transfer VMAs from object file to separate debug file. */
4762 static void
4763 set_debug_vma (bfd *orig_bfd, bfd *debug_bfd)
4765 asection *s, *d;
4767 for (s = orig_bfd->sections, d = debug_bfd->sections;
4768 s != NULL && d != NULL;
4769 s = s->next, d = d->next)
4771 if ((d->flags & SEC_DEBUGGING) != 0)
4772 break;
4773 /* ??? Assumes 1-1 correspondence between sections in the
4774 two files. */
4775 if (strcmp (s->name, d->name) == 0)
4777 d->output_section = s->output_section;
4778 d->output_offset = s->output_offset;
4779 d->vma = s->vma;
4784 /* If the dwarf2 info was found in a separate debug file, return the
4785 debug file section corresponding to the section in the original file
4786 and the debug file symbols. */
4788 static void
4789 _bfd_dwarf2_stash_syms (struct dwarf2_debug *stash, bfd *abfd,
4790 asection **sec, asymbol ***syms)
4792 if (stash->f.bfd_ptr != abfd)
4794 asection *s, *d;
4796 if (*sec == NULL)
4798 *syms = stash->f.syms;
4799 return;
4802 for (s = abfd->sections, d = stash->f.bfd_ptr->sections;
4803 s != NULL && d != NULL;
4804 s = s->next, d = d->next)
4806 if ((d->flags & SEC_DEBUGGING) != 0)
4807 break;
4808 if (s == *sec
4809 && strcmp (s->name, d->name) == 0)
4811 *sec = d;
4812 *syms = stash->f.syms;
4813 break;
4819 /* Unset vmas for adjusted sections in STASH. */
4821 static void
4822 unset_sections (struct dwarf2_debug *stash)
4824 int i;
4825 struct adjusted_section *p;
4827 i = stash->adjusted_section_count;
4828 p = stash->adjusted_sections;
4829 for (; i > 0; i--, p++)
4830 p->section->vma = 0;
4833 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4834 relocatable object file. VMAs are normally all zero in relocatable
4835 object files, so if we want to distinguish locations in sections by
4836 address we need to set VMAs so the sections do not overlap. We
4837 also set VMA on .debug_info so that when we have multiple
4838 .debug_info sections (or the linkonce variant) they also do not
4839 overlap. The multiple .debug_info sections make up a single
4840 logical section. ??? We should probably do the same for other
4841 debug sections. */
4843 static bool
4844 place_sections (bfd *orig_bfd, struct dwarf2_debug *stash)
4846 bfd *abfd;
4847 struct adjusted_section *p;
4848 int i;
4849 const char *debug_info_name;
4851 if (stash->adjusted_section_count != 0)
4853 i = stash->adjusted_section_count;
4854 p = stash->adjusted_sections;
4855 for (; i > 0; i--, p++)
4856 p->section->vma = p->adj_vma;
4857 return true;
4860 debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
4861 i = 0;
4862 abfd = orig_bfd;
4863 while (1)
4865 asection *sect;
4867 for (sect = abfd->sections; sect != NULL; sect = sect->next)
4869 int is_debug_info;
4871 if ((sect->output_section != NULL
4872 && sect->output_section != sect
4873 && (sect->flags & SEC_DEBUGGING) == 0)
4874 || sect->vma != 0)
4875 continue;
4877 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
4878 || startswith (sect->name, GNU_LINKONCE_INFO));
4880 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
4881 && !is_debug_info)
4882 continue;
4884 i++;
4886 if (abfd == stash->f.bfd_ptr)
4887 break;
4888 abfd = stash->f.bfd_ptr;
4891 if (i <= 1)
4892 stash->adjusted_section_count = -1;
4893 else
4895 bfd_vma last_vma = 0, last_dwarf = 0;
4896 size_t amt = i * sizeof (struct adjusted_section);
4898 p = (struct adjusted_section *) bfd_malloc (amt);
4899 if (p == NULL)
4900 return false;
4902 stash->adjusted_sections = p;
4903 stash->adjusted_section_count = i;
4905 abfd = orig_bfd;
4906 while (1)
4908 asection *sect;
4910 for (sect = abfd->sections; sect != NULL; sect = sect->next)
4912 bfd_size_type sz;
4913 int is_debug_info;
4915 if ((sect->output_section != NULL
4916 && sect->output_section != sect
4917 && (sect->flags & SEC_DEBUGGING) == 0)
4918 || sect->vma != 0)
4919 continue;
4921 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
4922 || startswith (sect->name, GNU_LINKONCE_INFO));
4924 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
4925 && !is_debug_info)
4926 continue;
4928 sz = sect->rawsize ? sect->rawsize : sect->size;
4930 if (is_debug_info)
4932 BFD_ASSERT (sect->alignment_power == 0);
4933 sect->vma = last_dwarf;
4934 last_dwarf += sz;
4936 else
4938 /* Align the new address to the current section
4939 alignment. */
4940 last_vma = ((last_vma
4941 + ~(-((bfd_vma) 1 << sect->alignment_power)))
4942 & (-((bfd_vma) 1 << sect->alignment_power)));
4943 sect->vma = last_vma;
4944 last_vma += sz;
4947 p->section = sect;
4948 p->adj_vma = sect->vma;
4949 p++;
4951 if (abfd == stash->f.bfd_ptr)
4952 break;
4953 abfd = stash->f.bfd_ptr;
4957 if (orig_bfd != stash->f.bfd_ptr)
4958 set_debug_vma (orig_bfd, stash->f.bfd_ptr);
4960 return true;
4963 /* Look up a funcinfo by name using the given info hash table. If found,
4964 also update the locations pointed to by filename_ptr and linenumber_ptr.
4966 This function returns TRUE if a funcinfo that matches the given symbol
4967 and address is found with any error; otherwise it returns FALSE. */
4969 static bool
4970 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
4971 asymbol *sym,
4972 bfd_vma addr,
4973 const char **filename_ptr,
4974 unsigned int *linenumber_ptr)
4976 struct funcinfo* each_func;
4977 struct funcinfo* best_fit = NULL;
4978 bfd_vma best_fit_len = 0;
4979 struct info_list_node *node;
4980 struct arange *arange;
4981 const char *name = bfd_asymbol_name (sym);
4982 asection *sec = bfd_asymbol_section (sym);
4984 for (node = lookup_info_hash_table (hash_table, name);
4985 node;
4986 node = node->next)
4988 each_func = (struct funcinfo *) node->info;
4989 for (arange = &each_func->arange;
4990 arange;
4991 arange = arange->next)
4993 if ((!each_func->sec || each_func->sec == sec)
4994 && addr >= arange->low
4995 && addr < arange->high
4996 && (!best_fit
4997 || arange->high - arange->low < best_fit_len))
4999 best_fit = each_func;
5000 best_fit_len = arange->high - arange->low;
5005 if (best_fit)
5007 best_fit->sec = sec;
5008 *filename_ptr = best_fit->file;
5009 *linenumber_ptr = best_fit->line;
5010 return true;
5013 return false;
5016 /* Look up a varinfo by name using the given info hash table. If found,
5017 also update the locations pointed to by filename_ptr and linenumber_ptr.
5019 This function returns TRUE if a varinfo that matches the given symbol
5020 and address is found with any error; otherwise it returns FALSE. */
5022 static bool
5023 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
5024 asymbol *sym,
5025 bfd_vma addr,
5026 const char **filename_ptr,
5027 unsigned int *linenumber_ptr)
5029 const char *name = bfd_asymbol_name (sym);
5030 asection *sec = bfd_asymbol_section (sym);
5031 struct varinfo* each;
5032 struct info_list_node *node;
5034 for (node = lookup_info_hash_table (hash_table, name);
5035 node;
5036 node = node->next)
5038 each = (struct varinfo *) node->info;
5039 if (each->addr == addr
5040 && (!each->sec || each->sec == sec))
5042 each->sec = sec;
5043 *filename_ptr = each->file;
5044 *linenumber_ptr = each->line;
5045 return true;
5049 return false;
5052 /* Update the funcinfo and varinfo info hash tables if they are
5053 not up to date. Returns TRUE if there is no error; otherwise
5054 returns FALSE and disable the info hash tables. */
5056 static bool
5057 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
5059 struct comp_unit *each;
5061 /* Exit if hash tables are up-to-date. */
5062 if (stash->f.all_comp_units == stash->hash_units_head)
5063 return true;
5065 if (stash->hash_units_head)
5066 each = stash->hash_units_head->prev_unit;
5067 else
5068 each = stash->f.last_comp_unit;
5070 while (each)
5072 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
5073 stash->varinfo_hash_table))
5075 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
5076 return false;
5078 each = each->prev_unit;
5081 stash->hash_units_head = stash->f.all_comp_units;
5082 return true;
5085 /* Check consistency of info hash tables. This is for debugging only. */
5087 static void ATTRIBUTE_UNUSED
5088 stash_verify_info_hash_table (struct dwarf2_debug *stash)
5090 struct comp_unit *each_unit;
5091 struct funcinfo *each_func;
5092 struct varinfo *each_var;
5093 struct info_list_node *node;
5094 bool found;
5096 for (each_unit = stash->f.all_comp_units;
5097 each_unit;
5098 each_unit = each_unit->next_unit)
5100 for (each_func = each_unit->function_table;
5101 each_func;
5102 each_func = each_func->prev_func)
5104 if (!each_func->name)
5105 continue;
5106 node = lookup_info_hash_table (stash->funcinfo_hash_table,
5107 each_func->name);
5108 BFD_ASSERT (node);
5109 found = false;
5110 while (node && !found)
5112 found = node->info == each_func;
5113 node = node->next;
5115 BFD_ASSERT (found);
5118 for (each_var = each_unit->variable_table;
5119 each_var;
5120 each_var = each_var->prev_var)
5122 if (!each_var->name || !each_var->file || each_var->stack)
5123 continue;
5124 node = lookup_info_hash_table (stash->varinfo_hash_table,
5125 each_var->name);
5126 BFD_ASSERT (node);
5127 found = false;
5128 while (node && !found)
5130 found = node->info == each_var;
5131 node = node->next;
5133 BFD_ASSERT (found);
5138 /* Check to see if we want to enable the info hash tables, which consume
5139 quite a bit of memory. Currently we only check the number times
5140 bfd_dwarf2_find_line is called. In the future, we may also want to
5141 take the number of symbols into account. */
5143 static void
5144 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
5146 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
5148 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
5149 return;
5151 /* FIXME: Maybe we should check the reduce_memory_overheads
5152 and optimize fields in the bfd_link_info structure ? */
5154 /* Create hash tables. */
5155 stash->funcinfo_hash_table = create_info_hash_table (abfd);
5156 stash->varinfo_hash_table = create_info_hash_table (abfd);
5157 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
5159 /* Turn off info hashes if any allocation above fails. */
5160 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
5161 return;
5163 /* We need a forced update so that the info hash tables will
5164 be created even though there is no compilation unit. That
5165 happens if STASH_INFO_HASH_TRIGGER is 0. */
5166 if (stash_maybe_update_info_hash_tables (stash))
5167 stash->info_hash_status = STASH_INFO_HASH_ON;
5170 /* Find the file and line associated with a symbol and address using the
5171 info hash tables of a stash. If there is a match, the function returns
5172 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
5173 otherwise it returns FALSE. */
5175 static bool
5176 stash_find_line_fast (struct dwarf2_debug *stash,
5177 asymbol *sym,
5178 bfd_vma addr,
5179 const char **filename_ptr,
5180 unsigned int *linenumber_ptr)
5182 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
5184 if (sym->flags & BSF_FUNCTION)
5185 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
5186 filename_ptr, linenumber_ptr);
5187 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
5188 filename_ptr, linenumber_ptr);
5191 /* Save current section VMAs. */
5193 static bool
5194 save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
5196 asection *s;
5197 unsigned int i;
5199 if (abfd->section_count == 0)
5200 return true;
5201 stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count);
5202 if (stash->sec_vma == NULL)
5203 return false;
5204 stash->sec_vma_count = abfd->section_count;
5205 for (i = 0, s = abfd->sections;
5206 s != NULL && i < abfd->section_count;
5207 i++, s = s->next)
5209 if (s->output_section != NULL)
5210 stash->sec_vma[i] = s->output_section->vma + s->output_offset;
5211 else
5212 stash->sec_vma[i] = s->vma;
5214 return true;
5217 /* Compare current section VMAs against those at the time the stash
5218 was created. If find_nearest_line is used in linker warnings or
5219 errors early in the link process, the debug info stash will be
5220 invalid for later calls. This is because we relocate debug info
5221 sections, so the stashed section contents depend on symbol values,
5222 which in turn depend on section VMAs. */
5224 static bool
5225 section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash)
5227 asection *s;
5228 unsigned int i;
5230 /* PR 24334: If the number of sections in ABFD has changed between
5231 when the stash was created and now, then we cannot trust the
5232 stashed vma information. */
5233 if (abfd->section_count != stash->sec_vma_count)
5234 return false;
5236 for (i = 0, s = abfd->sections;
5237 s != NULL && i < abfd->section_count;
5238 i++, s = s->next)
5240 bfd_vma vma;
5242 if (s->output_section != NULL)
5243 vma = s->output_section->vma + s->output_offset;
5244 else
5245 vma = s->vma;
5246 if (vma != stash->sec_vma[i])
5247 return false;
5249 return true;
5252 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
5253 If DEBUG_BFD is not specified, we read debug information from ABFD
5254 or its gnu_debuglink. The results will be stored in PINFO.
5255 The function returns TRUE iff debug information is ready. */
5257 bool
5258 _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
5259 const struct dwarf_debug_section *debug_sections,
5260 asymbol **symbols,
5261 void **pinfo,
5262 bool do_place)
5264 size_t amt = sizeof (struct dwarf2_debug);
5265 bfd_size_type total_size;
5266 asection *msec;
5267 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
5269 if (stash != NULL)
5271 if (stash->orig_bfd == abfd
5272 && section_vma_same (abfd, stash))
5274 /* Check that we did previously find some debug information
5275 before attempting to make use of it. */
5276 if (stash->f.bfd_ptr != NULL)
5278 if (do_place && !place_sections (abfd, stash))
5279 return false;
5280 return true;
5283 return false;
5285 _bfd_dwarf2_cleanup_debug_info (abfd, pinfo);
5286 memset (stash, 0, amt);
5288 else
5290 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
5291 if (! stash)
5292 return false;
5294 stash->orig_bfd = abfd;
5295 stash->debug_sections = debug_sections;
5296 stash->f.syms = symbols;
5297 if (!save_section_vma (abfd, stash))
5298 return false;
5300 stash->f.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
5301 del_abbrev, calloc, free);
5302 if (!stash->f.abbrev_offsets)
5303 return false;
5305 stash->alt.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
5306 del_abbrev, calloc, free);
5307 if (!stash->alt.abbrev_offsets)
5308 return false;
5310 stash->f.trie_root = alloc_trie_leaf (abfd);
5311 if (!stash->f.trie_root)
5312 return false;
5314 stash->alt.trie_root = alloc_trie_leaf (abfd);
5315 if (!stash->alt.trie_root)
5316 return false;
5318 *pinfo = stash;
5320 if (debug_bfd == NULL)
5321 debug_bfd = abfd;
5323 msec = find_debug_info (debug_bfd, debug_sections, NULL);
5324 if (msec == NULL && abfd == debug_bfd)
5326 char * debug_filename;
5328 debug_filename = bfd_follow_build_id_debuglink (abfd, DEBUGDIR);
5329 if (debug_filename == NULL)
5330 debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
5332 if (debug_filename == NULL)
5333 /* No dwarf2 info, and no gnu_debuglink to follow.
5334 Note that at this point the stash has been allocated, but
5335 contains zeros. This lets future calls to this function
5336 fail more quickly. */
5337 return false;
5339 debug_bfd = bfd_openr (debug_filename, NULL);
5340 free (debug_filename);
5341 if (debug_bfd == NULL)
5342 /* FIXME: Should we report our failure to follow the debuglink ? */
5343 return false;
5345 /* Set BFD_DECOMPRESS to decompress debug sections. */
5346 debug_bfd->flags |= BFD_DECOMPRESS;
5347 if (!bfd_check_format (debug_bfd, bfd_object)
5348 || (msec = find_debug_info (debug_bfd,
5349 debug_sections, NULL)) == NULL
5350 || !bfd_generic_link_read_symbols (debug_bfd))
5352 bfd_close (debug_bfd);
5353 return false;
5356 symbols = bfd_get_outsymbols (debug_bfd);
5357 stash->f.syms = symbols;
5358 stash->close_on_cleanup = true;
5360 stash->f.bfd_ptr = debug_bfd;
5362 if (do_place
5363 && !place_sections (abfd, stash))
5364 return false;
5366 /* There can be more than one DWARF2 info section in a BFD these
5367 days. First handle the easy case when there's only one. If
5368 there's more than one, try case two: none of the sections is
5369 compressed. In that case, read them all in and produce one
5370 large stash. We do this in two passes - in the first pass we
5371 just accumulate the section sizes, and in the second pass we
5372 read in the section's contents. (The allows us to avoid
5373 reallocing the data as we add sections to the stash.) If
5374 some or all sections are compressed, then do things the slow
5375 way, with a bunch of reallocs. */
5377 if (! find_debug_info (debug_bfd, debug_sections, msec))
5379 /* Case 1: only one info section. */
5380 total_size = msec->size;
5381 if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
5382 symbols, 0,
5383 &stash->f.dwarf_info_buffer, &total_size))
5384 return false;
5386 else
5388 /* Case 2: multiple sections. */
5389 for (total_size = 0;
5390 msec;
5391 msec = find_debug_info (debug_bfd, debug_sections, msec))
5393 /* Catch PR25070 testcase overflowing size calculation here. */
5394 if (total_size + msec->size < total_size
5395 || total_size + msec->size < msec->size)
5397 bfd_set_error (bfd_error_no_memory);
5398 return false;
5400 total_size += msec->size;
5403 stash->f.dwarf_info_buffer = (bfd_byte *) bfd_malloc (total_size);
5404 if (stash->f.dwarf_info_buffer == NULL)
5405 return false;
5407 total_size = 0;
5408 for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
5409 msec;
5410 msec = find_debug_info (debug_bfd, debug_sections, msec))
5412 bfd_size_type size;
5414 size = msec->size;
5415 if (size == 0)
5416 continue;
5418 if (!(bfd_simple_get_relocated_section_contents
5419 (debug_bfd, msec, stash->f.dwarf_info_buffer + total_size,
5420 symbols)))
5421 return false;
5423 total_size += size;
5427 stash->f.info_ptr = stash->f.dwarf_info_buffer;
5428 stash->f.dwarf_info_size = total_size;
5429 return true;
5432 /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
5434 static struct comp_unit *
5435 stash_comp_unit (struct dwarf2_debug *stash, struct dwarf2_debug_file *file)
5437 bfd_size_type length;
5438 unsigned int offset_size;
5439 bfd_byte *info_ptr_unit = file->info_ptr;
5440 bfd_byte *info_ptr_end = file->dwarf_info_buffer + file->dwarf_info_size;
5442 if (file->info_ptr >= info_ptr_end)
5443 return NULL;
5445 length = read_4_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5446 /* A 0xffffff length is the DWARF3 way of indicating
5447 we use 64-bit offsets, instead of 32-bit offsets. */
5448 if (length == 0xffffffff)
5450 offset_size = 8;
5451 length = read_8_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5453 /* A zero length is the IRIX way of indicating 64-bit offsets,
5454 mostly because the 64-bit length will generally fit in 32
5455 bits, and the endianness helps. */
5456 else if (length == 0)
5458 offset_size = 8;
5459 length = read_4_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5461 /* In the absence of the hints above, we assume 32-bit DWARF2
5462 offsets even for targets with 64-bit addresses, because:
5463 a) most of the time these targets will not have generated
5464 more than 2Gb of debug info and so will not need 64-bit
5465 offsets,
5467 b) if they do use 64-bit offsets but they are not using
5468 the size hints that are tested for above then they are
5469 not conforming to the DWARF3 standard anyway. */
5470 else
5471 offset_size = 4;
5473 if (length != 0
5474 && length <= (size_t) (info_ptr_end - file->info_ptr))
5476 struct comp_unit *each = parse_comp_unit (stash, file,
5477 file->info_ptr, length,
5478 info_ptr_unit, offset_size);
5479 if (each)
5481 if (file->all_comp_units)
5482 file->all_comp_units->prev_unit = each;
5483 else
5484 file->last_comp_unit = each;
5486 each->next_unit = file->all_comp_units;
5487 file->all_comp_units = each;
5489 if (each->arange.high == 0)
5491 each->next_unit_without_ranges = file->all_comp_units_without_ranges;
5492 file->all_comp_units_without_ranges = each->next_unit_without_ranges;
5495 file->info_ptr += length;
5496 return each;
5500 /* Don't trust any of the DWARF info after a corrupted length or
5501 parse error. */
5502 file->info_ptr = info_ptr_end;
5503 return NULL;
5506 /* Hash function for an asymbol. */
5508 static hashval_t
5509 hash_asymbol (const void *sym)
5511 const asymbol *asym = sym;
5512 return htab_hash_string (asym->name);
5515 /* Equality function for asymbols. */
5517 static int
5518 eq_asymbol (const void *a, const void *b)
5520 const asymbol *sa = a;
5521 const asymbol *sb = b;
5522 return strcmp (sa->name, sb->name) == 0;
5525 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
5526 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
5527 symbol in SYMBOLS and return the difference between the low_pc and
5528 the symbol's address. Returns 0 if no suitable symbol could be found. */
5530 bfd_signed_vma
5531 _bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo)
5533 struct dwarf2_debug *stash;
5534 struct comp_unit * unit;
5535 htab_t sym_hash;
5536 bfd_signed_vma result = 0;
5537 asymbol ** psym;
5539 stash = (struct dwarf2_debug *) *pinfo;
5541 if (stash == NULL || symbols == NULL)
5542 return 0;
5544 sym_hash = htab_create_alloc (10, hash_asymbol, eq_asymbol,
5545 NULL, xcalloc, free);
5546 for (psym = symbols; * psym != NULL; psym++)
5548 asymbol * sym = * psym;
5550 if (sym->flags & BSF_FUNCTION && sym->section != NULL)
5552 void **slot = htab_find_slot (sym_hash, sym, INSERT);
5553 *slot = sym;
5557 for (unit = stash->f.all_comp_units; unit; unit = unit->next_unit)
5559 struct funcinfo * func;
5561 comp_unit_maybe_decode_line_info (unit);
5563 for (func = unit->function_table; func != NULL; func = func->prev_func)
5564 if (func->name && func->arange.low)
5566 asymbol search, *sym;
5568 /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
5570 search.name = func->name;
5571 sym = htab_find (sym_hash, &search);
5572 if (sym != NULL)
5574 result = ((bfd_signed_vma) func->arange.low) -
5575 ((bfd_signed_vma) (sym->value + sym->section->vma));
5576 goto done;
5581 done:
5582 htab_delete (sym_hash);
5583 return result;
5586 /* Find the source code location of SYMBOL. If SYMBOL is NULL
5587 then find the nearest source code location corresponding to
5588 the address SECTION + OFFSET.
5589 Returns 1 if the line is found without error and fills in
5590 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
5591 NULL the FUNCTIONNAME_PTR is also filled in.
5592 Returns 2 if partial information from _bfd_elf_find_function is
5593 returned (function and maybe file) by looking at symbols. DWARF2
5594 info is present but not regarding the requested code location.
5595 Returns 0 otherwise.
5596 SYMBOLS contains the symbol table for ABFD.
5597 DEBUG_SECTIONS contains the name of the dwarf debug sections. */
5600 _bfd_dwarf2_find_nearest_line (bfd *abfd,
5601 asymbol **symbols,
5602 asymbol *symbol,
5603 asection *section,
5604 bfd_vma offset,
5605 const char **filename_ptr,
5606 const char **functionname_ptr,
5607 unsigned int *linenumber_ptr,
5608 unsigned int *discriminator_ptr,
5609 const struct dwarf_debug_section *debug_sections,
5610 void **pinfo)
5612 /* Read each compilation unit from the section .debug_info, and check
5613 to see if it contains the address we are searching for. If yes,
5614 lookup the address, and return the line number info. If no, go
5615 on to the next compilation unit.
5617 We keep a list of all the previously read compilation units, and
5618 a pointer to the next un-read compilation unit. Check the
5619 previously read units before reading more. */
5620 struct dwarf2_debug *stash;
5621 /* What address are we looking for? */
5622 bfd_vma addr;
5623 struct comp_unit* each;
5624 struct funcinfo *function = NULL;
5625 int found = false;
5626 bool do_line;
5628 *filename_ptr = NULL;
5629 if (functionname_ptr != NULL)
5630 *functionname_ptr = NULL;
5631 *linenumber_ptr = 0;
5632 if (discriminator_ptr)
5633 *discriminator_ptr = 0;
5635 if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections,
5636 symbols, pinfo,
5637 (abfd->flags & (EXEC_P | DYNAMIC)) == 0))
5638 return false;
5640 stash = (struct dwarf2_debug *) *pinfo;
5642 do_line = symbol != NULL;
5643 if (do_line)
5645 BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL);
5646 section = bfd_asymbol_section (symbol);
5647 addr = symbol->value;
5649 else
5651 BFD_ASSERT (section != NULL && functionname_ptr != NULL);
5652 addr = offset;
5654 /* If we have no SYMBOL but the section we're looking at is not a
5655 code section, then take a look through the list of symbols to see
5656 if we have a symbol at the address we're looking for. If we do
5657 then use this to look up line information. This will allow us to
5658 give file and line results for data symbols. We exclude code
5659 symbols here, if we look up a function symbol and then look up the
5660 line information we'll actually return the line number for the
5661 opening '{' rather than the function definition line. This is
5662 because looking up by symbol uses the line table, in which the
5663 first line for a function is usually the opening '{', while
5664 looking up the function by section + offset uses the
5665 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
5666 which will be the line of the function name. */
5667 if (symbols != NULL && (section->flags & SEC_CODE) == 0)
5669 asymbol **tmp;
5671 for (tmp = symbols; (*tmp) != NULL; ++tmp)
5672 if ((*tmp)->the_bfd == abfd
5673 && (*tmp)->section == section
5674 && (*tmp)->value == offset
5675 && ((*tmp)->flags & BSF_SECTION_SYM) == 0)
5677 symbol = *tmp;
5678 do_line = true;
5679 /* For local symbols, keep going in the hope we find a
5680 global. */
5681 if ((symbol->flags & BSF_GLOBAL) != 0)
5682 break;
5687 if (section->output_section)
5688 addr += section->output_section->vma + section->output_offset;
5689 else
5690 addr += section->vma;
5692 /* A null info_ptr indicates that there is no dwarf2 info
5693 (or that an error occured while setting up the stash). */
5694 if (! stash->f.info_ptr)
5695 return false;
5697 stash->inliner_chain = NULL;
5699 /* Check the previously read comp. units first. */
5700 if (do_line)
5702 /* The info hash tables use quite a bit of memory. We may not want to
5703 always use them. We use some heuristics to decide if and when to
5704 turn it on. */
5705 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
5706 stash_maybe_enable_info_hash_tables (abfd, stash);
5708 /* Keep info hash table up to date if they are available. Note that we
5709 may disable the hash tables if there is any error duing update. */
5710 if (stash->info_hash_status == STASH_INFO_HASH_ON)
5711 stash_maybe_update_info_hash_tables (stash);
5713 if (stash->info_hash_status == STASH_INFO_HASH_ON)
5715 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
5716 linenumber_ptr);
5717 if (found)
5718 goto done;
5720 else
5722 /* Check the previously read comp. units first. */
5723 for (each = stash->f.all_comp_units; each; each = each->next_unit)
5724 if ((symbol->flags & BSF_FUNCTION) == 0
5725 || each->arange.high == 0
5726 || comp_unit_contains_address (each, addr))
5728 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
5729 linenumber_ptr);
5730 if (found)
5731 goto done;
5735 else
5737 struct trie_node *trie = stash->f.trie_root;
5738 unsigned int bits = VMA_BITS - 8;
5739 struct comp_unit **prev_each;
5741 /* Traverse interior nodes until we get to a leaf. */
5742 while (trie && trie->num_room_in_leaf == 0)
5744 int ch = (addr >> bits) & 0xff;
5745 trie = ((struct trie_interior *) trie)->children[ch];
5746 bits -= 8;
5749 if (trie)
5751 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
5752 unsigned int i;
5754 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
5756 leaf->ranges[i].unit->mark = false;
5759 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
5761 struct comp_unit *unit = leaf->ranges[i].unit;
5762 if (unit->mark ||
5763 addr < leaf->ranges[i].low_pc ||
5764 addr >= leaf->ranges[i].high_pc)
5765 continue;
5766 unit->mark = true;
5768 found = comp_unit_find_nearest_line (unit, addr,
5769 filename_ptr,
5770 &function,
5771 linenumber_ptr,
5772 discriminator_ptr);
5773 if (found)
5774 goto done;
5778 /* Also scan through all compilation units without any ranges,
5779 taking them out of the list if they have acquired any since
5780 last time. */
5781 prev_each = &stash->f.all_comp_units_without_ranges;
5782 for (each = *prev_each; each; each = each->next_unit_without_ranges)
5784 if (each->arange.high != 0)
5786 *prev_each = each->next_unit_without_ranges;
5787 continue;
5790 found = comp_unit_find_nearest_line (each, addr,
5791 filename_ptr,
5792 &function,
5793 linenumber_ptr,
5794 discriminator_ptr);
5795 if (found)
5796 goto done;
5797 prev_each = &each->next_unit_without_ranges;
5801 /* Read each remaining comp. units checking each as they are read. */
5802 while ((each = stash_comp_unit (stash, &stash->f)) != NULL)
5804 /* DW_AT_low_pc and DW_AT_high_pc are optional for
5805 compilation units. If we don't have them (i.e.,
5806 unit->high == 0), we need to consult the line info table
5807 to see if a compilation unit contains the given
5808 address. */
5809 if (do_line)
5810 found = (((symbol->flags & BSF_FUNCTION) == 0
5811 || each->arange.high == 0
5812 || comp_unit_contains_address (each, addr))
5813 && comp_unit_find_line (each, symbol, addr,
5814 filename_ptr, linenumber_ptr));
5815 else
5816 found = ((each->arange.high == 0
5817 || comp_unit_contains_address (each, addr))
5818 && comp_unit_find_nearest_line (each, addr,
5819 filename_ptr,
5820 &function,
5821 linenumber_ptr,
5822 discriminator_ptr));
5824 if (found)
5825 break;
5828 done:
5829 if (functionname_ptr && function && function->is_linkage)
5831 *functionname_ptr = function->name;
5832 if (!found)
5833 found = 2;
5835 else if (functionname_ptr
5836 && (!*functionname_ptr
5837 || (function && !function->is_linkage)))
5839 asymbol *fun;
5840 asymbol **syms = symbols;
5841 asection *sec = section;
5843 _bfd_dwarf2_stash_syms (stash, abfd, &sec, &syms);
5844 fun = _bfd_elf_find_function (abfd, syms, sec, offset,
5845 *filename_ptr ? NULL : filename_ptr,
5846 functionname_ptr);
5848 if (!found && fun != NULL)
5849 found = 2;
5851 if (function && !function->is_linkage)
5853 bfd_vma sec_vma;
5855 sec_vma = section->vma;
5856 if (section->output_section != NULL)
5857 sec_vma = section->output_section->vma + section->output_offset;
5858 if (fun == NULL)
5859 *functionname_ptr = function->name;
5860 else if (fun->value + sec_vma == function->arange.low)
5861 function->name = *functionname_ptr;
5862 /* Even if we didn't find a linkage name, say that we have
5863 to stop a repeated search of symbols. */
5864 function->is_linkage = true;
5868 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
5869 unset_sections (stash);
5871 return found;
5874 bool
5875 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
5876 const char **filename_ptr,
5877 const char **functionname_ptr,
5878 unsigned int *linenumber_ptr,
5879 void **pinfo)
5881 struct dwarf2_debug *stash;
5883 stash = (struct dwarf2_debug *) *pinfo;
5884 if (stash)
5886 struct funcinfo *func = stash->inliner_chain;
5888 if (func && func->caller_func)
5890 *filename_ptr = func->caller_file;
5891 *functionname_ptr = func->caller_func->name;
5892 *linenumber_ptr = func->caller_line;
5893 stash->inliner_chain = func->caller_func;
5894 return true;
5898 return false;
5901 void
5902 _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
5904 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
5905 struct comp_unit *each;
5906 struct dwarf2_debug_file *file;
5908 if (abfd == NULL || stash == NULL)
5909 return;
5911 if (stash->varinfo_hash_table)
5912 bfd_hash_table_free (&stash->varinfo_hash_table->base);
5913 if (stash->funcinfo_hash_table)
5914 bfd_hash_table_free (&stash->funcinfo_hash_table->base);
5916 file = &stash->f;
5917 while (1)
5919 for (each = file->all_comp_units; each; each = each->next_unit)
5921 struct funcinfo *function_table = each->function_table;
5922 struct varinfo *variable_table = each->variable_table;
5924 if (each->line_table && each->line_table != file->line_table)
5926 free (each->line_table->files);
5927 free (each->line_table->dirs);
5930 free (each->lookup_funcinfo_table);
5931 each->lookup_funcinfo_table = NULL;
5933 while (function_table)
5935 free (function_table->file);
5936 function_table->file = NULL;
5937 free (function_table->caller_file);
5938 function_table->caller_file = NULL;
5939 function_table = function_table->prev_func;
5942 while (variable_table)
5944 free (variable_table->file);
5945 variable_table->file = NULL;
5946 variable_table = variable_table->prev_var;
5950 if (file->line_table)
5952 free (file->line_table->files);
5953 free (file->line_table->dirs);
5955 htab_delete (file->abbrev_offsets);
5957 free (file->dwarf_line_str_buffer);
5958 free (file->dwarf_str_buffer);
5959 free (file->dwarf_ranges_buffer);
5960 free (file->dwarf_line_buffer);
5961 free (file->dwarf_abbrev_buffer);
5962 free (file->dwarf_info_buffer);
5963 if (file == &stash->alt)
5964 break;
5965 file = &stash->alt;
5967 free (stash->sec_vma);
5968 free (stash->adjusted_sections);
5969 if (stash->close_on_cleanup)
5970 bfd_close (stash->f.bfd_ptr);
5971 if (stash->alt.bfd_ptr)
5972 bfd_close (stash->alt.bfd_ptr);
5975 /* Find the function to a particular section and offset,
5976 for error reporting. */
5978 asymbol *
5979 _bfd_elf_find_function (bfd *abfd,
5980 asymbol **symbols,
5981 asection *section,
5982 bfd_vma offset,
5983 const char **filename_ptr,
5984 const char **functionname_ptr)
5986 struct elf_find_function_cache
5988 asection *last_section;
5989 asymbol *func;
5990 const char *filename;
5991 bfd_size_type func_size;
5992 } *cache;
5994 if (symbols == NULL)
5995 return NULL;
5997 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
5998 return NULL;
6000 cache = elf_tdata (abfd)->elf_find_function_cache;
6001 if (cache == NULL)
6003 cache = bfd_zalloc (abfd, sizeof (*cache));
6004 elf_tdata (abfd)->elf_find_function_cache = cache;
6005 if (cache == NULL)
6006 return NULL;
6008 if (cache->last_section != section
6009 || cache->func == NULL
6010 || offset < cache->func->value
6011 || offset >= cache->func->value + cache->func_size)
6013 asymbol *file;
6014 bfd_vma low_func;
6015 asymbol **p;
6016 /* ??? Given multiple file symbols, it is impossible to reliably
6017 choose the right file name for global symbols. File symbols are
6018 local symbols, and thus all file symbols must sort before any
6019 global symbols. The ELF spec may be interpreted to say that a
6020 file symbol must sort before other local symbols, but currently
6021 ld -r doesn't do this. So, for ld -r output, it is possible to
6022 make a better choice of file name for local symbols by ignoring
6023 file symbols appearing after a given local symbol. */
6024 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
6025 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6027 file = NULL;
6028 low_func = 0;
6029 state = nothing_seen;
6030 cache->filename = NULL;
6031 cache->func = NULL;
6032 cache->func_size = 0;
6033 cache->last_section = section;
6035 for (p = symbols; *p != NULL; p++)
6037 asymbol *sym = *p;
6038 bfd_vma code_off;
6039 bfd_size_type size;
6041 if ((sym->flags & BSF_FILE) != 0)
6043 file = sym;
6044 if (state == symbol_seen)
6045 state = file_after_symbol_seen;
6046 continue;
6049 size = bed->maybe_function_sym (sym, section, &code_off);
6050 if (size != 0
6051 && code_off <= offset
6052 && (code_off > low_func
6053 || (code_off == low_func
6054 && size > cache->func_size)))
6056 cache->func = sym;
6057 cache->func_size = size;
6058 cache->filename = NULL;
6059 low_func = code_off;
6060 if (file != NULL
6061 && ((sym->flags & BSF_LOCAL) != 0
6062 || state != file_after_symbol_seen))
6063 cache->filename = bfd_asymbol_name (file);
6065 if (state == nothing_seen)
6066 state = symbol_seen;
6070 if (cache->func == NULL)
6071 return NULL;
6073 if (filename_ptr)
6074 *filename_ptr = cache->filename;
6075 if (functionname_ptr)
6076 *functionname_ptr = bfd_asymbol_name (cache->func);
6078 return cache->func;