]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/dwarf2.c
PR29230, segv in lookup_symbol_in_variable_table
[thirdparty/binutils-gdb.git] / bfd / dwarf2.c
1 /* DWARF 2 support.
2 Copyright (C) 1994-2022 Free Software Foundation, Inc.
3
4 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
5 (gavin@cygnus.com).
6
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
14
15 This file is part of BFD.
16
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.
21
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.
26
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. */
31
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"
39
40 /* The data in the .debug_line statement prologue looks like this. */
41
42 struct line_head
43 {
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;
54 };
55
56 /* Attributes have a name and a value. */
57
58 struct attribute
59 {
60 enum dwarf_attribute name;
61 enum dwarf_form form;
62 union
63 {
64 char *str;
65 struct dwarf_block *blk;
66 uint64_t val;
67 int64_t sval;
68 }
69 u;
70 };
71
72 /* Blocks are a bunch of untyped bytes. */
73 struct dwarf_block
74 {
75 unsigned int size;
76 bfd_byte *data;
77 };
78
79 struct adjusted_section
80 {
81 asection *section;
82 bfd_vma adj_vma;
83 };
84
85 /* A trie to map quickly from address range to compilation unit.
86
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.
93
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.
108
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.
112 */
113
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.
118 */
119 #define TRIE_LEAF_SIZE 16
120
121 /* All trie_node pointers will really be trie_leaf or trie_interior,
122 but they have this common head. */
123 struct trie_node
124 {
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;
128 };
129
130 struct trie_leaf
131 {
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];
138 };
139
140 struct trie_interior
141 {
142 struct trie_node head;
143 struct trie_node *children[256];
144 };
145
146 static struct trie_node *alloc_trie_leaf (bfd *abfd)
147 {
148 struct trie_leaf *leaf = bfd_zalloc (abfd, sizeof (struct trie_leaf));
149 if (leaf == NULL)
150 return NULL;
151 leaf->head.num_room_in_leaf = TRIE_LEAF_SIZE;
152 return &leaf->head;
153 }
154
155 struct dwarf2_debug_file
156 {
157 /* The actual bfd from which debug info was loaded. Might be
158 different to orig_bfd because of gnu_debuglink sections. */
159 bfd *bfd_ptr;
160
161 /* Pointer to the symbol table. */
162 asymbol **syms;
163
164 /* The current info pointer for the .debug_info section being parsed. */
165 bfd_byte *info_ptr;
166
167 /* A pointer to the memory block allocated for .debug_info sections. */
168 bfd_byte *dwarf_info_buffer;
169
170 /* Length of the loaded .debug_info sections. */
171 bfd_size_type dwarf_info_size;
172
173 /* Pointer to the .debug_abbrev section loaded into memory. */
174 bfd_byte *dwarf_abbrev_buffer;
175
176 /* Length of the loaded .debug_abbrev section. */
177 bfd_size_type dwarf_abbrev_size;
178
179 /* Buffer for decode_line_info. */
180 bfd_byte *dwarf_line_buffer;
181
182 /* Length of the loaded .debug_line section. */
183 bfd_size_type dwarf_line_size;
184
185 /* Pointer to the .debug_str section loaded into memory. */
186 bfd_byte *dwarf_str_buffer;
187
188 /* Length of the loaded .debug_str section. */
189 bfd_size_type dwarf_str_size;
190
191 /* Pointer to the .debug_str_offsets section loaded into memory. */
192 bfd_byte *dwarf_str_offsets_buffer;
193
194 /* Length of the loaded .debug_str_offsets section. */
195 bfd_size_type dwarf_str_offsets_size;
196
197 /* Pointer to the .debug_addr section loaded into memory. */
198 bfd_byte *dwarf_addr_buffer;
199
200 /* Length of the loaded .debug_addr section. */
201 bfd_size_type dwarf_addr_size;
202
203 /* Pointer to the .debug_line_str section loaded into memory. */
204 bfd_byte *dwarf_line_str_buffer;
205
206 /* Length of the loaded .debug_line_str section. */
207 bfd_size_type dwarf_line_str_size;
208
209 /* Pointer to the .debug_ranges section loaded into memory. */
210 bfd_byte *dwarf_ranges_buffer;
211
212 /* Length of the loaded .debug_ranges section. */
213 bfd_size_type dwarf_ranges_size;
214
215 /* Pointer to the .debug_rnglists section loaded into memory. */
216 bfd_byte *dwarf_rnglists_buffer;
217
218 /* Length of the loaded .debug_rnglists section. */
219 bfd_size_type dwarf_rnglists_size;
220
221 /* A list of all previously read comp_units. */
222 struct comp_unit *all_comp_units;
223
224 /* A list of all previously read comp_units with no ranges (yet). */
225 struct comp_unit *all_comp_units_without_ranges;
226
227 /* Last comp unit in list above. */
228 struct comp_unit *last_comp_unit;
229
230 /* Line table at line_offset zero. */
231 struct line_info_table *line_table;
232
233 /* Hash table to map offsets to decoded abbrevs. */
234 htab_t abbrev_offsets;
235
236 /* Root of a trie to map addresses to compilation units. */
237 struct trie_node *trie_root;
238 };
239
240 struct dwarf2_debug
241 {
242 /* Names of the debug sections. */
243 const struct dwarf_debug_section *debug_sections;
244
245 /* Per-file stuff. */
246 struct dwarf2_debug_file f, alt;
247
248 /* Pointer to the original bfd for which debug was loaded. This is what
249 we use to compare and so check that the cached debug data is still
250 valid - it saves having to possibly dereference the gnu_debuglink each
251 time. */
252 bfd *orig_bfd;
253
254 /* If the most recent call to bfd_find_nearest_line was given an
255 address in an inlined function, preserve a pointer into the
256 calling chain for subsequent calls to bfd_find_inliner_info to
257 use. */
258 struct funcinfo *inliner_chain;
259
260 /* Section VMAs at the time the stash was built. */
261 bfd_vma *sec_vma;
262 /* Number of sections in the SEC_VMA table. */
263 unsigned int sec_vma_count;
264
265 /* Number of sections whose VMA we must adjust. */
266 int adjusted_section_count;
267
268 /* Array of sections with adjusted VMA. */
269 struct adjusted_section *adjusted_sections;
270
271 /* Number of times find_line is called. This is used in
272 the heuristic for enabling the info hash tables. */
273 int info_hash_count;
274
275 #define STASH_INFO_HASH_TRIGGER 100
276
277 /* Hash table mapping symbol names to function infos. */
278 struct info_hash_table *funcinfo_hash_table;
279
280 /* Hash table mapping symbol names to variable infos. */
281 struct info_hash_table *varinfo_hash_table;
282
283 /* Head of comp_unit list in the last hash table update. */
284 struct comp_unit *hash_units_head;
285
286 /* Status of info hash. */
287 int info_hash_status;
288 #define STASH_INFO_HASH_OFF 0
289 #define STASH_INFO_HASH_ON 1
290 #define STASH_INFO_HASH_DISABLED 2
291
292 /* True if we opened bfd_ptr. */
293 bool close_on_cleanup;
294 };
295
296 struct arange
297 {
298 struct arange *next;
299 bfd_vma low;
300 bfd_vma high;
301 };
302
303 /* A minimal decoding of DWARF2 compilation units. We only decode
304 what's needed to get to the line number information. */
305
306 struct comp_unit
307 {
308 /* Chain the previously read compilation units. */
309 struct comp_unit *next_unit;
310
311 /* Chain the previously read compilation units that have no ranges yet.
312 We scan these separately when we have a trie over the ranges.
313 Unused if arange.high != 0. */
314 struct comp_unit *next_unit_without_ranges;
315
316 /* Likewise, chain the compilation unit read after this one.
317 The comp units are stored in reversed reading order. */
318 struct comp_unit *prev_unit;
319
320 /* Keep the bfd convenient (for memory allocation). */
321 bfd *abfd;
322
323 /* The lowest and highest addresses contained in this compilation
324 unit as specified in the compilation unit header. */
325 struct arange arange;
326
327 /* The DW_AT_name attribute (for error messages). */
328 char *name;
329
330 /* The abbrev hash table. */
331 struct abbrev_info **abbrevs;
332
333 /* DW_AT_language. */
334 int lang;
335
336 /* Note that an error was found by comp_unit_find_nearest_line. */
337 int error;
338
339 /* The DW_AT_comp_dir attribute. */
340 char *comp_dir;
341
342 /* TRUE if there is a line number table associated with this comp. unit. */
343 int stmtlist;
344
345 /* Pointer to the current comp_unit so that we can find a given entry
346 by its reference. */
347 bfd_byte *info_ptr_unit;
348
349 /* The offset into .debug_line of the line number table. */
350 unsigned long line_offset;
351
352 /* Pointer to the first child die for the comp unit. */
353 bfd_byte *first_child_die_ptr;
354
355 /* The end of the comp unit. */
356 bfd_byte *end_ptr;
357
358 /* The decoded line number, NULL if not yet decoded. */
359 struct line_info_table *line_table;
360
361 /* A list of the functions found in this comp. unit. */
362 struct funcinfo *function_table;
363
364 /* A table of function information references searchable by address. */
365 struct lookup_funcinfo *lookup_funcinfo_table;
366
367 /* Number of functions in the function_table and sorted_function_table. */
368 bfd_size_type number_of_functions;
369
370 /* A list of the variables found in this comp. unit. */
371 struct varinfo *variable_table;
372
373 /* Pointers to dwarf2_debug structures. */
374 struct dwarf2_debug *stash;
375 struct dwarf2_debug_file *file;
376
377 /* DWARF format version for this unit - from unit header. */
378 int version;
379
380 /* Address size for this unit - from unit header. */
381 unsigned char addr_size;
382
383 /* Offset size for this unit - from unit header. */
384 unsigned char offset_size;
385
386 /* Base address for this unit - from DW_AT_low_pc attribute of
387 DW_TAG_compile_unit DIE */
388 bfd_vma base_address;
389
390 /* TRUE if symbols are cached in hash table for faster lookup by name. */
391 bool cached;
392
393 /* Used when iterating over trie leaves to know which units we have
394 already seen in this iteration. */
395 bool mark;
396
397 /* Base address of debug_addr section. */
398 size_t dwarf_addr_offset;
399
400 /* Base address of string offset table. */
401 size_t dwarf_str_offset;
402 };
403
404 /* This data structure holds the information of an abbrev. */
405 struct abbrev_info
406 {
407 unsigned int number; /* Number identifying abbrev. */
408 enum dwarf_tag tag; /* DWARF tag. */
409 bool has_children; /* TRUE if the abbrev has children. */
410 unsigned int num_attrs; /* Number of attributes. */
411 struct attr_abbrev * attrs; /* An array of attribute descriptions. */
412 struct abbrev_info * next; /* Next in chain. */
413 };
414
415 struct attr_abbrev
416 {
417 enum dwarf_attribute name;
418 enum dwarf_form form;
419 bfd_vma implicit_const;
420 };
421
422 /* Map of uncompressed DWARF debug section name to compressed one. It
423 is terminated by NULL uncompressed_name. */
424
425 const struct dwarf_debug_section dwarf_debug_sections[] =
426 {
427 { ".debug_abbrev", ".zdebug_abbrev" },
428 { ".debug_aranges", ".zdebug_aranges" },
429 { ".debug_frame", ".zdebug_frame" },
430 { ".debug_info", ".zdebug_info" },
431 { ".debug_info", ".zdebug_info" },
432 { ".debug_line", ".zdebug_line" },
433 { ".debug_loc", ".zdebug_loc" },
434 { ".debug_macinfo", ".zdebug_macinfo" },
435 { ".debug_macro", ".zdebug_macro" },
436 { ".debug_pubnames", ".zdebug_pubnames" },
437 { ".debug_pubtypes", ".zdebug_pubtypes" },
438 { ".debug_ranges", ".zdebug_ranges" },
439 { ".debug_rnglists", ".zdebug_rnglist" },
440 { ".debug_static_func", ".zdebug_static_func" },
441 { ".debug_static_vars", ".zdebug_static_vars" },
442 { ".debug_str", ".zdebug_str", },
443 { ".debug_str", ".zdebug_str", },
444 { ".debug_str_offsets", ".zdebug_str_offsets", },
445 { ".debug_addr", ".zdebug_addr", },
446 { ".debug_line_str", ".zdebug_line_str", },
447 { ".debug_types", ".zdebug_types" },
448 /* GNU DWARF 1 extensions */
449 { ".debug_sfnames", ".zdebug_sfnames" },
450 { ".debug_srcinfo", ".zebug_srcinfo" },
451 /* SGI/MIPS DWARF 2 extensions */
452 { ".debug_funcnames", ".zdebug_funcnames" },
453 { ".debug_typenames", ".zdebug_typenames" },
454 { ".debug_varnames", ".zdebug_varnames" },
455 { ".debug_weaknames", ".zdebug_weaknames" },
456 { NULL, NULL },
457 };
458
459 /* NB/ Numbers in this enum must match up with indices
460 into the dwarf_debug_sections[] array above. */
461 enum dwarf_debug_section_enum
462 {
463 debug_abbrev = 0,
464 debug_aranges,
465 debug_frame,
466 debug_info,
467 debug_info_alt,
468 debug_line,
469 debug_loc,
470 debug_macinfo,
471 debug_macro,
472 debug_pubnames,
473 debug_pubtypes,
474 debug_ranges,
475 debug_rnglists,
476 debug_static_func,
477 debug_static_vars,
478 debug_str,
479 debug_str_alt,
480 debug_str_offsets,
481 debug_addr,
482 debug_line_str,
483 debug_types,
484 debug_sfnames,
485 debug_srcinfo,
486 debug_funcnames,
487 debug_typenames,
488 debug_varnames,
489 debug_weaknames,
490 debug_max
491 };
492
493 /* A static assertion. */
494 extern int dwarf_debug_section_assert[ARRAY_SIZE (dwarf_debug_sections)
495 == debug_max + 1 ? 1 : -1];
496
497 #ifndef ABBREV_HASH_SIZE
498 #define ABBREV_HASH_SIZE 121
499 #endif
500 #ifndef ATTR_ALLOC_CHUNK
501 #define ATTR_ALLOC_CHUNK 4
502 #endif
503
504 /* Variable and function hash tables. This is used to speed up look-up
505 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
506 In order to share code between variable and function infos, we use
507 a list of untyped pointer for all variable/function info associated with
508 a symbol. We waste a bit of memory for list with one node but that
509 simplifies the code. */
510
511 struct info_list_node
512 {
513 struct info_list_node *next;
514 void *info;
515 };
516
517 /* Info hash entry. */
518 struct info_hash_entry
519 {
520 struct bfd_hash_entry root;
521 struct info_list_node *head;
522 };
523
524 struct info_hash_table
525 {
526 struct bfd_hash_table base;
527 };
528
529 /* Function to create a new entry in info hash table. */
530
531 static struct bfd_hash_entry *
532 info_hash_table_newfunc (struct bfd_hash_entry *entry,
533 struct bfd_hash_table *table,
534 const char *string)
535 {
536 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
537
538 /* Allocate the structure if it has not already been allocated by a
539 derived class. */
540 if (ret == NULL)
541 {
542 ret = (struct info_hash_entry *) bfd_hash_allocate (table,
543 sizeof (* ret));
544 if (ret == NULL)
545 return NULL;
546 }
547
548 /* Call the allocation method of the base class. */
549 ret = ((struct info_hash_entry *)
550 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
551
552 /* Initialize the local fields here. */
553 if (ret)
554 ret->head = NULL;
555
556 return (struct bfd_hash_entry *) ret;
557 }
558
559 /* Function to create a new info hash table. It returns a pointer to the
560 newly created table or NULL if there is any error. We need abfd
561 solely for memory allocation. */
562
563 static struct info_hash_table *
564 create_info_hash_table (bfd *abfd)
565 {
566 struct info_hash_table *hash_table;
567
568 hash_table = ((struct info_hash_table *)
569 bfd_alloc (abfd, sizeof (struct info_hash_table)));
570 if (!hash_table)
571 return hash_table;
572
573 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
574 sizeof (struct info_hash_entry)))
575 {
576 bfd_release (abfd, hash_table);
577 return NULL;
578 }
579
580 return hash_table;
581 }
582
583 /* Insert an info entry into an info hash table. We do not check of
584 duplicate entries. Also, the caller need to guarantee that the
585 right type of info in inserted as info is passed as a void* pointer.
586 This function returns true if there is no error. */
587
588 static bool
589 insert_info_hash_table (struct info_hash_table *hash_table,
590 const char *key,
591 void *info,
592 bool copy_p)
593 {
594 struct info_hash_entry *entry;
595 struct info_list_node *node;
596
597 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
598 key, true, copy_p);
599 if (!entry)
600 return false;
601
602 node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
603 sizeof (*node));
604 if (!node)
605 return false;
606
607 node->info = info;
608 node->next = entry->head;
609 entry->head = node;
610
611 return true;
612 }
613
614 /* Look up an info entry list from an info hash table. Return NULL
615 if there is none. */
616
617 static struct info_list_node *
618 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
619 {
620 struct info_hash_entry *entry;
621
622 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
623 false, false);
624 return entry ? entry->head : NULL;
625 }
626
627 /* Read a section into its appropriate place in the dwarf2_debug
628 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
629 not NULL, use bfd_simple_get_relocated_section_contents to read the
630 section contents, otherwise use bfd_get_section_contents. Fail if
631 the located section does not contain at least OFFSET bytes. */
632
633 static bool
634 read_section (bfd *abfd,
635 const struct dwarf_debug_section *sec,
636 asymbol **syms,
637 uint64_t offset,
638 bfd_byte **section_buffer,
639 bfd_size_type *section_size)
640 {
641 const char *section_name = sec->uncompressed_name;
642 bfd_byte *contents = *section_buffer;
643
644 /* The section may have already been read. */
645 if (contents == NULL)
646 {
647 bfd_size_type amt;
648 asection *msec;
649 ufile_ptr filesize;
650
651 msec = bfd_get_section_by_name (abfd, section_name);
652 if (msec == NULL)
653 {
654 section_name = sec->compressed_name;
655 msec = bfd_get_section_by_name (abfd, section_name);
656 }
657 if (msec == NULL)
658 {
659 _bfd_error_handler (_("DWARF error: can't find %s section."),
660 sec->uncompressed_name);
661 bfd_set_error (bfd_error_bad_value);
662 return false;
663 }
664
665 amt = bfd_get_section_limit_octets (abfd, msec);
666 filesize = bfd_get_file_size (abfd);
667 /* PR 28834: A compressed debug section could well decompress to a size
668 larger than the file, so we choose an arbitrary modifier of 10x in
669 the test below. If this ever turns out to be insufficient, it can
670 be changed by a future update. */
671 if (amt >= filesize * 10)
672 {
673 /* PR 26946 */
674 _bfd_error_handler (_("DWARF error: section %s is larger than 10x its filesize! (0x%lx vs 0x%lx)"),
675 section_name, (long) amt, (long) filesize);
676 bfd_set_error (bfd_error_bad_value);
677 return false;
678 }
679 *section_size = amt;
680 /* Paranoia - alloc one extra so that we can make sure a string
681 section is NUL terminated. */
682 amt += 1;
683 if (amt == 0)
684 {
685 /* Paranoia - this should never happen. */
686 bfd_set_error (bfd_error_no_memory);
687 return false;
688 }
689 contents = (bfd_byte *) bfd_malloc (amt);
690 if (contents == NULL)
691 return false;
692 if (syms
693 ? !bfd_simple_get_relocated_section_contents (abfd, msec, contents,
694 syms)
695 : !bfd_get_section_contents (abfd, msec, contents, 0, *section_size))
696 {
697 free (contents);
698 return false;
699 }
700 contents[*section_size] = 0;
701 *section_buffer = contents;
702 }
703
704 /* It is possible to get a bad value for the offset into the section
705 that the client wants. Validate it here to avoid trouble later. */
706 if (offset != 0 && offset >= *section_size)
707 {
708 /* xgettext: c-format */
709 _bfd_error_handler (_("DWARF error: offset (%" PRIu64 ")"
710 " greater than or equal to %s size (%" PRIu64 ")"),
711 (uint64_t) offset, section_name,
712 (uint64_t) *section_size);
713 bfd_set_error (bfd_error_bad_value);
714 return false;
715 }
716
717 return true;
718 }
719
720 /* Read dwarf information from a buffer. */
721
722 static inline uint64_t
723 read_n_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end, int n)
724 {
725 bfd_byte *buf = *ptr;
726 if (end - buf < n)
727 {
728 *ptr = end;
729 return 0;
730 }
731 *ptr = buf + n;
732 return bfd_get (n * 8, abfd, buf);
733 }
734
735 static unsigned int
736 read_1_byte (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
737 {
738 return read_n_bytes (abfd, ptr, end, 1);
739 }
740
741 static int
742 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte **ptr, bfd_byte *end)
743 {
744 bfd_byte *buf = *ptr;
745 if (end - buf < 1)
746 {
747 *ptr = end;
748 return 0;
749 }
750 *ptr = buf + 1;
751 return bfd_get_signed_8 (abfd, buf);
752 }
753
754 static unsigned int
755 read_2_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
756 {
757 return read_n_bytes (abfd, ptr, end, 2);
758 }
759
760 static unsigned int
761 read_3_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
762 {
763 unsigned int val = read_1_byte (abfd, ptr, end);
764 val <<= 8;
765 val |= read_1_byte (abfd, ptr, end);
766 val <<= 8;
767 val |= read_1_byte (abfd, ptr, end);
768 if (bfd_little_endian (abfd))
769 val = (((val >> 16) & 0xff)
770 | (val & 0xff00)
771 | ((val & 0xff) << 16));
772 return val;
773 }
774
775 static unsigned int
776 read_4_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
777 {
778 return read_n_bytes (abfd, ptr, end, 4);
779 }
780
781 static uint64_t
782 read_8_bytes (bfd *abfd, bfd_byte **ptr, bfd_byte *end)
783 {
784 return read_n_bytes (abfd, ptr, end, 8);
785 }
786
787 static struct dwarf_block *
788 read_blk (bfd *abfd, bfd_byte **ptr, bfd_byte *end, size_t size)
789 {
790 bfd_byte *buf = *ptr;
791 struct dwarf_block *block;
792
793 block = (struct dwarf_block *) bfd_alloc (abfd, sizeof (*block));
794 if (block == NULL)
795 return NULL;
796
797 if (size > (size_t) (end - buf))
798 {
799 *ptr = end;
800 block->data = NULL;
801 block->size = 0;
802 }
803 else
804 {
805 *ptr = buf + size;
806 block->data = buf;
807 block->size = size;
808 }
809 return block;
810 }
811
812 /* Scans a NUL terminated string starting at *PTR, returning a pointer to it.
813 Bytes at or beyond BUF_END will not be read. Returns NULL if the
814 terminator is not found or if the string is empty. *PTR is
815 incremented over the bytes scanned, including the terminator. */
816
817 static char *
818 read_string (bfd_byte **ptr,
819 bfd_byte *buf_end)
820 {
821 bfd_byte *buf = *ptr;
822 bfd_byte *str = buf;
823
824 while (buf < buf_end)
825 if (*buf++ == 0)
826 {
827 if (str == buf - 1)
828 break;
829 *ptr = buf;
830 return (char *) str;
831 }
832
833 *ptr = buf;
834 return NULL;
835 }
836
837 /* Reads an offset from *PTR and then locates the string at this offset
838 inside the debug string section. Returns a pointer to the string.
839 Increments *PTR by the number of bytes read for the offset. This
840 value is set even if the function fails. Bytes at or beyond
841 BUF_END will not be read. Returns NULL if there was a problem, or
842 if the string is empty. Does not check for NUL termination of the
843 string. */
844
845 static char *
846 read_indirect_string (struct comp_unit *unit,
847 bfd_byte **ptr,
848 bfd_byte *buf_end)
849 {
850 uint64_t offset;
851 struct dwarf2_debug *stash = unit->stash;
852 struct dwarf2_debug_file *file = unit->file;
853 char *str;
854
855 if (unit->offset_size > (size_t) (buf_end - *ptr))
856 {
857 *ptr = buf_end;
858 return NULL;
859 }
860
861 if (unit->offset_size == 4)
862 offset = read_4_bytes (unit->abfd, ptr, buf_end);
863 else
864 offset = read_8_bytes (unit->abfd, ptr, buf_end);
865
866 if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
867 file->syms, offset,
868 &file->dwarf_str_buffer, &file->dwarf_str_size))
869 return NULL;
870
871 str = (char *) file->dwarf_str_buffer + offset;
872 if (*str == '\0')
873 return NULL;
874 return str;
875 }
876
877 /* Like read_indirect_string but from .debug_line_str section. */
878
879 static char *
880 read_indirect_line_string (struct comp_unit *unit,
881 bfd_byte **ptr,
882 bfd_byte *buf_end)
883 {
884 uint64_t offset;
885 struct dwarf2_debug *stash = unit->stash;
886 struct dwarf2_debug_file *file = unit->file;
887 char *str;
888
889 if (unit->offset_size > (size_t) (buf_end - *ptr))
890 {
891 *ptr = buf_end;
892 return NULL;
893 }
894
895 if (unit->offset_size == 4)
896 offset = read_4_bytes (unit->abfd, ptr, buf_end);
897 else
898 offset = read_8_bytes (unit->abfd, ptr, buf_end);
899
900 if (! read_section (unit->abfd, &stash->debug_sections[debug_line_str],
901 file->syms, offset,
902 &file->dwarf_line_str_buffer,
903 &file->dwarf_line_str_size))
904 return NULL;
905
906 str = (char *) file->dwarf_line_str_buffer + offset;
907 if (*str == '\0')
908 return NULL;
909 return str;
910 }
911
912 /* Like read_indirect_string but uses a .debug_str located in
913 an alternate file pointed to by the .gnu_debugaltlink section.
914 Used to impement DW_FORM_GNU_strp_alt. */
915
916 static char *
917 read_alt_indirect_string (struct comp_unit *unit,
918 bfd_byte **ptr,
919 bfd_byte *buf_end)
920 {
921 uint64_t offset;
922 struct dwarf2_debug *stash = unit->stash;
923 char *str;
924
925 if (unit->offset_size > (size_t) (buf_end - *ptr))
926 {
927 *ptr = buf_end;
928 return NULL;
929 }
930
931 if (unit->offset_size == 4)
932 offset = read_4_bytes (unit->abfd, ptr, buf_end);
933 else
934 offset = read_8_bytes (unit->abfd, ptr, buf_end);
935
936 if (stash->alt.bfd_ptr == NULL)
937 {
938 bfd *debug_bfd;
939 char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
940
941 if (debug_filename == NULL)
942 return NULL;
943
944 debug_bfd = bfd_openr (debug_filename, NULL);
945 free (debug_filename);
946 if (debug_bfd == NULL)
947 /* FIXME: Should we report our failure to follow the debuglink ? */
948 return NULL;
949
950 if (!bfd_check_format (debug_bfd, bfd_object))
951 {
952 bfd_close (debug_bfd);
953 return NULL;
954 }
955 stash->alt.bfd_ptr = debug_bfd;
956 }
957
958 if (! read_section (unit->stash->alt.bfd_ptr,
959 stash->debug_sections + debug_str_alt,
960 stash->alt.syms, offset,
961 &stash->alt.dwarf_str_buffer,
962 &stash->alt.dwarf_str_size))
963 return NULL;
964
965 str = (char *) stash->alt.dwarf_str_buffer + offset;
966 if (*str == '\0')
967 return NULL;
968
969 return str;
970 }
971
972 /* Resolve an alternate reference from UNIT at OFFSET.
973 Returns a pointer into the loaded alternate CU upon success
974 or NULL upon failure. */
975
976 static bfd_byte *
977 read_alt_indirect_ref (struct comp_unit *unit, uint64_t offset)
978 {
979 struct dwarf2_debug *stash = unit->stash;
980
981 if (stash->alt.bfd_ptr == NULL)
982 {
983 bfd *debug_bfd;
984 char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
985
986 if (debug_filename == NULL)
987 return NULL;
988
989 debug_bfd = bfd_openr (debug_filename, NULL);
990 free (debug_filename);
991 if (debug_bfd == NULL)
992 /* FIXME: Should we report our failure to follow the debuglink ? */
993 return NULL;
994
995 if (!bfd_check_format (debug_bfd, bfd_object))
996 {
997 bfd_close (debug_bfd);
998 return NULL;
999 }
1000 stash->alt.bfd_ptr = debug_bfd;
1001 }
1002
1003 if (! read_section (unit->stash->alt.bfd_ptr,
1004 stash->debug_sections + debug_info_alt,
1005 stash->alt.syms, offset,
1006 &stash->alt.dwarf_info_buffer,
1007 &stash->alt.dwarf_info_size))
1008 return NULL;
1009
1010 return stash->alt.dwarf_info_buffer + offset;
1011 }
1012
1013 static uint64_t
1014 read_address (struct comp_unit *unit, bfd_byte **ptr, bfd_byte *buf_end)
1015 {
1016 bfd_byte *buf = *ptr;
1017 int signed_vma = 0;
1018
1019 if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour)
1020 signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
1021
1022 if (unit->addr_size > (size_t) (buf_end - buf))
1023 {
1024 *ptr = buf_end;
1025 return 0;
1026 }
1027
1028 *ptr = buf + unit->addr_size;
1029 if (signed_vma)
1030 {
1031 switch (unit->addr_size)
1032 {
1033 case 8:
1034 return bfd_get_signed_64 (unit->abfd, buf);
1035 case 4:
1036 return bfd_get_signed_32 (unit->abfd, buf);
1037 case 2:
1038 return bfd_get_signed_16 (unit->abfd, buf);
1039 default:
1040 abort ();
1041 }
1042 }
1043 else
1044 {
1045 switch (unit->addr_size)
1046 {
1047 case 8:
1048 return bfd_get_64 (unit->abfd, buf);
1049 case 4:
1050 return bfd_get_32 (unit->abfd, buf);
1051 case 2:
1052 return bfd_get_16 (unit->abfd, buf);
1053 default:
1054 abort ();
1055 }
1056 }
1057 }
1058
1059 /* Lookup an abbrev_info structure in the abbrev hash table. */
1060
1061 static struct abbrev_info *
1062 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
1063 {
1064 unsigned int hash_number;
1065 struct abbrev_info *abbrev;
1066
1067 hash_number = number % ABBREV_HASH_SIZE;
1068 abbrev = abbrevs[hash_number];
1069
1070 while (abbrev)
1071 {
1072 if (abbrev->number == number)
1073 return abbrev;
1074 else
1075 abbrev = abbrev->next;
1076 }
1077
1078 return NULL;
1079 }
1080
1081 /* We keep a hash table to map .debug_abbrev section offsets to the
1082 array of abbrevs, so that compilation units using the same set of
1083 abbrevs do not waste memory. */
1084
1085 struct abbrev_offset_entry
1086 {
1087 size_t offset;
1088 struct abbrev_info **abbrevs;
1089 };
1090
1091 static hashval_t
1092 hash_abbrev (const void *p)
1093 {
1094 const struct abbrev_offset_entry *ent = p;
1095 return htab_hash_pointer ((void *) ent->offset);
1096 }
1097
1098 static int
1099 eq_abbrev (const void *pa, const void *pb)
1100 {
1101 const struct abbrev_offset_entry *a = pa;
1102 const struct abbrev_offset_entry *b = pb;
1103 return a->offset == b->offset;
1104 }
1105
1106 static void
1107 del_abbrev (void *p)
1108 {
1109 struct abbrev_offset_entry *ent = p;
1110 struct abbrev_info **abbrevs = ent->abbrevs;
1111 size_t i;
1112
1113 for (i = 0; i < ABBREV_HASH_SIZE; i++)
1114 {
1115 struct abbrev_info *abbrev = abbrevs[i];
1116
1117 while (abbrev)
1118 {
1119 free (abbrev->attrs);
1120 abbrev = abbrev->next;
1121 }
1122 }
1123 free (ent);
1124 }
1125
1126 /* In DWARF version 2, the description of the debugging information is
1127 stored in a separate .debug_abbrev section. Before we read any
1128 dies from a section we read in all abbreviations and install them
1129 in a hash table. */
1130
1131 static struct abbrev_info**
1132 read_abbrevs (bfd *abfd, uint64_t offset, struct dwarf2_debug *stash,
1133 struct dwarf2_debug_file *file)
1134 {
1135 struct abbrev_info **abbrevs;
1136 bfd_byte *abbrev_ptr;
1137 bfd_byte *abbrev_end;
1138 struct abbrev_info *cur_abbrev;
1139 unsigned int abbrev_number, abbrev_name;
1140 unsigned int abbrev_form, hash_number;
1141 size_t amt;
1142 void **slot;
1143 struct abbrev_offset_entry ent = { offset, NULL };
1144
1145 if (ent.offset != offset)
1146 return NULL;
1147
1148 slot = htab_find_slot (file->abbrev_offsets, &ent, INSERT);
1149 if (slot == NULL)
1150 return NULL;
1151 if (*slot != NULL)
1152 return ((struct abbrev_offset_entry *) (*slot))->abbrevs;
1153
1154 if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
1155 file->syms, offset,
1156 &file->dwarf_abbrev_buffer,
1157 &file->dwarf_abbrev_size))
1158 return NULL;
1159
1160 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
1161 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
1162 if (abbrevs == NULL)
1163 return NULL;
1164
1165 abbrev_ptr = file->dwarf_abbrev_buffer + offset;
1166 abbrev_end = file->dwarf_abbrev_buffer + file->dwarf_abbrev_size;
1167 abbrev_number = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1168 false, abbrev_end);
1169
1170 /* Loop until we reach an abbrev number of 0. */
1171 while (abbrev_number)
1172 {
1173 amt = sizeof (struct abbrev_info);
1174 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
1175 if (cur_abbrev == NULL)
1176 goto fail;
1177
1178 /* Read in abbrev header. */
1179 cur_abbrev->number = abbrev_number;
1180 cur_abbrev->tag = (enum dwarf_tag)
1181 _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1182 false, abbrev_end);
1183 cur_abbrev->has_children = read_1_byte (abfd, &abbrev_ptr, abbrev_end);
1184
1185 /* Now read in declarations. */
1186 for (;;)
1187 {
1188 /* Initialize it just to avoid a GCC false warning. */
1189 bfd_vma implicit_const = -1;
1190
1191 abbrev_name = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1192 false, abbrev_end);
1193 abbrev_form = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1194 false, abbrev_end);
1195 if (abbrev_form == DW_FORM_implicit_const)
1196 implicit_const = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1197 true, abbrev_end);
1198 if (abbrev_name == 0)
1199 break;
1200
1201 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
1202 {
1203 struct attr_abbrev *tmp;
1204
1205 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
1206 amt *= sizeof (struct attr_abbrev);
1207 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
1208 if (tmp == NULL)
1209 goto fail;
1210 cur_abbrev->attrs = tmp;
1211 }
1212
1213 cur_abbrev->attrs[cur_abbrev->num_attrs].name
1214 = (enum dwarf_attribute) abbrev_name;
1215 cur_abbrev->attrs[cur_abbrev->num_attrs].form
1216 = (enum dwarf_form) abbrev_form;
1217 cur_abbrev->attrs[cur_abbrev->num_attrs].implicit_const
1218 = implicit_const;
1219 ++cur_abbrev->num_attrs;
1220 }
1221
1222 hash_number = abbrev_number % ABBREV_HASH_SIZE;
1223 cur_abbrev->next = abbrevs[hash_number];
1224 abbrevs[hash_number] = cur_abbrev;
1225
1226 /* Get next abbreviation.
1227 Under Irix6 the abbreviations for a compilation unit are not
1228 always properly terminated with an abbrev number of 0.
1229 Exit loop if we encounter an abbreviation which we have
1230 already read (which means we are about to read the abbreviations
1231 for the next compile unit) or if the end of the abbreviation
1232 table is reached. */
1233 if ((size_t) (abbrev_ptr - file->dwarf_abbrev_buffer)
1234 >= file->dwarf_abbrev_size)
1235 break;
1236 abbrev_number = _bfd_safe_read_leb128 (abfd, &abbrev_ptr,
1237 false, abbrev_end);
1238 if (lookup_abbrev (abbrev_number, abbrevs) != NULL)
1239 break;
1240 }
1241
1242 *slot = bfd_malloc (sizeof ent);
1243 if (!*slot)
1244 goto fail;
1245 ent.abbrevs = abbrevs;
1246 memcpy (*slot, &ent, sizeof ent);
1247 return abbrevs;
1248
1249 fail:
1250 if (abbrevs != NULL)
1251 {
1252 size_t i;
1253
1254 for (i = 0; i < ABBREV_HASH_SIZE; i++)
1255 {
1256 struct abbrev_info *abbrev = abbrevs[i];
1257
1258 while (abbrev)
1259 {
1260 free (abbrev->attrs);
1261 abbrev = abbrev->next;
1262 }
1263 }
1264 free (abbrevs);
1265 }
1266 return NULL;
1267 }
1268
1269 /* Returns true if the form is one which has a string value. */
1270
1271 static bool
1272 is_str_form (const struct attribute *attr)
1273 {
1274 switch (attr->form)
1275 {
1276 case DW_FORM_string:
1277 case DW_FORM_strp:
1278 case DW_FORM_strx:
1279 case DW_FORM_strx1:
1280 case DW_FORM_strx2:
1281 case DW_FORM_strx3:
1282 case DW_FORM_strx4:
1283 case DW_FORM_line_strp:
1284 case DW_FORM_GNU_strp_alt:
1285 return true;
1286
1287 default:
1288 return false;
1289 }
1290 }
1291
1292 /* Returns true if the form is one which has an integer value. */
1293
1294 static bool
1295 is_int_form (const struct attribute *attr)
1296 {
1297 switch (attr->form)
1298 {
1299 case DW_FORM_addr:
1300 case DW_FORM_data2:
1301 case DW_FORM_data4:
1302 case DW_FORM_data8:
1303 case DW_FORM_data1:
1304 case DW_FORM_flag:
1305 case DW_FORM_sdata:
1306 case DW_FORM_udata:
1307 case DW_FORM_ref_addr:
1308 case DW_FORM_ref1:
1309 case DW_FORM_ref2:
1310 case DW_FORM_ref4:
1311 case DW_FORM_ref8:
1312 case DW_FORM_ref_udata:
1313 case DW_FORM_sec_offset:
1314 case DW_FORM_flag_present:
1315 case DW_FORM_ref_sig8:
1316 case DW_FORM_addrx:
1317 case DW_FORM_implicit_const:
1318 case DW_FORM_addrx1:
1319 case DW_FORM_addrx2:
1320 case DW_FORM_addrx3:
1321 case DW_FORM_addrx4:
1322 case DW_FORM_GNU_ref_alt:
1323 return true;
1324
1325 default:
1326 return false;
1327 }
1328 }
1329
1330 /* Returns true if the form is strx[1-4]. */
1331
1332 static inline bool
1333 is_strx_form (enum dwarf_form form)
1334 {
1335 return (form == DW_FORM_strx
1336 || form == DW_FORM_strx1
1337 || form == DW_FORM_strx2
1338 || form == DW_FORM_strx3
1339 || form == DW_FORM_strx4);
1340 }
1341
1342 /* Return true if the form is addrx[1-4]. */
1343
1344 static inline bool
1345 is_addrx_form (enum dwarf_form form)
1346 {
1347 return (form == DW_FORM_addrx
1348 || form == DW_FORM_addrx1
1349 || form == DW_FORM_addrx2
1350 || form == DW_FORM_addrx3
1351 || form == DW_FORM_addrx4);
1352 }
1353
1354 /* Returns the address in .debug_addr section using DW_AT_addr_base.
1355 Used to implement DW_FORM_addrx*. */
1356 static uint64_t
1357 read_indexed_address (uint64_t idx, struct comp_unit *unit)
1358 {
1359 struct dwarf2_debug *stash = unit->stash;
1360 struct dwarf2_debug_file *file = unit->file;
1361 bfd_byte *info_ptr;
1362 size_t offset;
1363
1364 if (stash == NULL)
1365 return 0;
1366
1367 if (!read_section (unit->abfd, &stash->debug_sections[debug_addr],
1368 file->syms, 0,
1369 &file->dwarf_addr_buffer, &file->dwarf_addr_size))
1370 return 0;
1371
1372 if (_bfd_mul_overflow (idx, unit->offset_size, &offset))
1373 return 0;
1374
1375 offset += unit->dwarf_addr_offset;
1376 if (offset < unit->dwarf_addr_offset
1377 || offset > file->dwarf_addr_size
1378 || file->dwarf_addr_size - offset < unit->offset_size)
1379 return 0;
1380
1381 info_ptr = file->dwarf_addr_buffer + offset;
1382
1383 if (unit->offset_size == 4)
1384 return bfd_get_32 (unit->abfd, info_ptr);
1385 else if (unit->offset_size == 8)
1386 return bfd_get_64 (unit->abfd, info_ptr);
1387 else
1388 return 0;
1389 }
1390
1391 /* Returns the string using DW_AT_str_offsets_base.
1392 Used to implement DW_FORM_strx*. */
1393 static const char *
1394 read_indexed_string (uint64_t idx, struct comp_unit *unit)
1395 {
1396 struct dwarf2_debug *stash = unit->stash;
1397 struct dwarf2_debug_file *file = unit->file;
1398 bfd_byte *info_ptr;
1399 uint64_t str_offset;
1400 size_t offset;
1401
1402 if (stash == NULL)
1403 return NULL;
1404
1405 if (!read_section (unit->abfd, &stash->debug_sections[debug_str],
1406 file->syms, 0,
1407 &file->dwarf_str_buffer, &file->dwarf_str_size))
1408 return NULL;
1409
1410 if (!read_section (unit->abfd, &stash->debug_sections[debug_str_offsets],
1411 file->syms, 0,
1412 &file->dwarf_str_offsets_buffer,
1413 &file->dwarf_str_offsets_size))
1414 return NULL;
1415
1416 if (_bfd_mul_overflow (idx, unit->offset_size, &offset))
1417 return NULL;
1418
1419 offset += unit->dwarf_str_offset;
1420 if (offset < unit->dwarf_str_offset
1421 || offset > file->dwarf_str_offsets_size
1422 || file->dwarf_str_offsets_size - offset < unit->offset_size)
1423 return NULL;
1424
1425 info_ptr = file->dwarf_str_offsets_buffer + offset;
1426
1427 if (unit->offset_size == 4)
1428 str_offset = bfd_get_32 (unit->abfd, info_ptr);
1429 else if (unit->offset_size == 8)
1430 str_offset = bfd_get_64 (unit->abfd, info_ptr);
1431 else
1432 return NULL;
1433
1434 if (str_offset >= file->dwarf_str_size)
1435 return NULL;
1436 return (const char *) file->dwarf_str_buffer + str_offset;
1437 }
1438
1439 /* Read and fill in the value of attribute ATTR as described by FORM.
1440 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1441 Returns an updated INFO_PTR taking into account the amount of data read. */
1442
1443 static bfd_byte *
1444 read_attribute_value (struct attribute * attr,
1445 unsigned form,
1446 bfd_vma implicit_const,
1447 struct comp_unit * unit,
1448 bfd_byte * info_ptr,
1449 bfd_byte * info_ptr_end)
1450 {
1451 bfd *abfd = unit->abfd;
1452 size_t amt;
1453
1454 if (info_ptr >= info_ptr_end && form != DW_FORM_flag_present)
1455 {
1456 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1457 bfd_set_error (bfd_error_bad_value);
1458 return NULL;
1459 }
1460
1461 attr->form = (enum dwarf_form) form;
1462
1463 switch (form)
1464 {
1465 case DW_FORM_flag_present:
1466 attr->u.val = 1;
1467 break;
1468 case DW_FORM_ref_addr:
1469 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1470 DWARF3. */
1471 if (unit->version >= 3)
1472 {
1473 if (unit->offset_size == 4)
1474 attr->u.val = read_4_bytes (unit->abfd, &info_ptr, info_ptr_end);
1475 else
1476 attr->u.val = read_8_bytes (unit->abfd, &info_ptr, info_ptr_end);
1477 break;
1478 }
1479 /* FALLTHROUGH */
1480 case DW_FORM_addr:
1481 attr->u.val = read_address (unit, &info_ptr, info_ptr_end);
1482 break;
1483 case DW_FORM_GNU_ref_alt:
1484 case DW_FORM_sec_offset:
1485 if (unit->offset_size == 4)
1486 attr->u.val = read_4_bytes (unit->abfd, &info_ptr, info_ptr_end);
1487 else
1488 attr->u.val = read_8_bytes (unit->abfd, &info_ptr, info_ptr_end);
1489 break;
1490 case DW_FORM_block2:
1491 amt = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1492 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1493 if (attr->u.blk == NULL)
1494 return NULL;
1495 break;
1496 case DW_FORM_block4:
1497 amt = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1498 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1499 if (attr->u.blk == NULL)
1500 return NULL;
1501 break;
1502 case DW_FORM_ref1:
1503 case DW_FORM_flag:
1504 case DW_FORM_data1:
1505 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1506 break;
1507 case DW_FORM_addrx1:
1508 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1509 /* dwarf_addr_offset value 0 indicates the attribute DW_AT_addr_base
1510 is not yet read. */
1511 if (unit->dwarf_addr_offset != 0)
1512 attr->u.val = read_indexed_address (attr->u.val, unit);
1513 break;
1514 case DW_FORM_data2:
1515 case DW_FORM_ref2:
1516 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1517 break;
1518 case DW_FORM_addrx2:
1519 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1520 if (unit->dwarf_addr_offset != 0)
1521 attr->u.val = read_indexed_address (attr->u.val, unit);
1522 break;
1523 case DW_FORM_addrx3:
1524 attr->u.val = read_3_bytes (abfd, &info_ptr, info_ptr_end);
1525 if (unit->dwarf_addr_offset != 0)
1526 attr->u.val = read_indexed_address(attr->u.val, unit);
1527 break;
1528 case DW_FORM_ref4:
1529 case DW_FORM_data4:
1530 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1531 break;
1532 case DW_FORM_addrx4:
1533 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1534 if (unit->dwarf_addr_offset != 0)
1535 attr->u.val = read_indexed_address (attr->u.val, unit);
1536 break;
1537 case DW_FORM_data8:
1538 case DW_FORM_ref8:
1539 case DW_FORM_ref_sig8:
1540 attr->u.val = read_8_bytes (abfd, &info_ptr, info_ptr_end);
1541 break;
1542 case DW_FORM_string:
1543 attr->u.str = read_string (&info_ptr, info_ptr_end);
1544 break;
1545 case DW_FORM_strp:
1546 attr->u.str = read_indirect_string (unit, &info_ptr, info_ptr_end);
1547 break;
1548 case DW_FORM_line_strp:
1549 attr->u.str = read_indirect_line_string (unit, &info_ptr, info_ptr_end);
1550 break;
1551 case DW_FORM_GNU_strp_alt:
1552 attr->u.str = read_alt_indirect_string (unit, &info_ptr, info_ptr_end);
1553 break;
1554 case DW_FORM_strx1:
1555 attr->u.val = read_1_byte (abfd, &info_ptr, info_ptr_end);
1556 /* dwarf_str_offset value 0 indicates the attribute DW_AT_str_offsets_base
1557 is not yet read. */
1558 if (unit->dwarf_str_offset != 0)
1559 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1560 else
1561 attr->u.str = NULL;
1562 break;
1563 case DW_FORM_strx2:
1564 attr->u.val = read_2_bytes (abfd, &info_ptr, info_ptr_end);
1565 if (unit->dwarf_str_offset != 0)
1566 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1567 else
1568 attr->u.str = NULL;
1569 break;
1570 case DW_FORM_strx3:
1571 attr->u.val = read_3_bytes (abfd, &info_ptr, info_ptr_end);
1572 if (unit->dwarf_str_offset != 0)
1573 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1574 else
1575 attr->u.str = NULL;
1576 break;
1577 case DW_FORM_strx4:
1578 attr->u.val = read_4_bytes (abfd, &info_ptr, info_ptr_end);
1579 if (unit->dwarf_str_offset != 0)
1580 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1581 else
1582 attr->u.str = NULL;
1583 break;
1584 case DW_FORM_strx:
1585 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1586 false, info_ptr_end);
1587 if (unit->dwarf_str_offset != 0)
1588 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
1589 else
1590 attr->u.str = NULL;
1591 break;
1592 case DW_FORM_exprloc:
1593 case DW_FORM_block:
1594 amt = _bfd_safe_read_leb128 (abfd, &info_ptr,
1595 false, info_ptr_end);
1596 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1597 if (attr->u.blk == NULL)
1598 return NULL;
1599 break;
1600 case DW_FORM_block1:
1601 amt = read_1_byte (abfd, &info_ptr, info_ptr_end);
1602 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, amt);
1603 if (attr->u.blk == NULL)
1604 return NULL;
1605 break;
1606 case DW_FORM_sdata:
1607 attr->u.sval = _bfd_safe_read_leb128 (abfd, &info_ptr,
1608 true, info_ptr_end);
1609 break;
1610 case DW_FORM_ref_udata:
1611 case DW_FORM_udata:
1612 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1613 false, info_ptr_end);
1614 break;
1615 case DW_FORM_addrx:
1616 attr->u.val = _bfd_safe_read_leb128 (abfd, &info_ptr,
1617 false, info_ptr_end);
1618 if (unit->dwarf_addr_offset != 0)
1619 attr->u.val = read_indexed_address (attr->u.val, unit);
1620 break;
1621 case DW_FORM_indirect:
1622 form = _bfd_safe_read_leb128 (abfd, &info_ptr,
1623 false, info_ptr_end);
1624 if (form == DW_FORM_implicit_const)
1625 implicit_const = _bfd_safe_read_leb128 (abfd, &info_ptr,
1626 true, info_ptr_end);
1627 info_ptr = read_attribute_value (attr, form, implicit_const, unit,
1628 info_ptr, info_ptr_end);
1629 break;
1630 case DW_FORM_implicit_const:
1631 attr->form = DW_FORM_sdata;
1632 attr->u.sval = implicit_const;
1633 break;
1634 case DW_FORM_data16:
1635 /* This is really a "constant", but there is no way to store that
1636 so pretend it is a 16 byte block instead. */
1637 attr->u.blk = read_blk (abfd, &info_ptr, info_ptr_end, 16);
1638 if (attr->u.blk == NULL)
1639 return NULL;
1640 break;
1641
1642 default:
1643 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1644 form);
1645 bfd_set_error (bfd_error_bad_value);
1646 return NULL;
1647 }
1648 return info_ptr;
1649 }
1650
1651 /* Read an attribute described by an abbreviated attribute. */
1652
1653 static bfd_byte *
1654 read_attribute (struct attribute * attr,
1655 struct attr_abbrev * abbrev,
1656 struct comp_unit * unit,
1657 bfd_byte * info_ptr,
1658 bfd_byte * info_ptr_end)
1659 {
1660 attr->name = abbrev->name;
1661 info_ptr = read_attribute_value (attr, abbrev->form, abbrev->implicit_const,
1662 unit, info_ptr, info_ptr_end);
1663 return info_ptr;
1664 }
1665
1666 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1667 for a function. */
1668
1669 static bool
1670 non_mangled (int lang)
1671 {
1672 switch (lang)
1673 {
1674 default:
1675 return false;
1676
1677 case DW_LANG_C89:
1678 case DW_LANG_C:
1679 case DW_LANG_Ada83:
1680 case DW_LANG_Cobol74:
1681 case DW_LANG_Cobol85:
1682 case DW_LANG_Fortran77:
1683 case DW_LANG_Pascal83:
1684 case DW_LANG_C99:
1685 case DW_LANG_Ada95:
1686 case DW_LANG_PLI:
1687 case DW_LANG_UPC:
1688 case DW_LANG_C11:
1689 case DW_LANG_Mips_Assembler:
1690 return true;
1691 }
1692 }
1693
1694 /* Source line information table routines. */
1695
1696 #define FILE_ALLOC_CHUNK 5
1697 #define DIR_ALLOC_CHUNK 5
1698
1699 struct line_info
1700 {
1701 struct line_info * prev_line;
1702 bfd_vma address;
1703 char * filename;
1704 unsigned int line;
1705 unsigned int column;
1706 unsigned int discriminator;
1707 unsigned char op_index;
1708 unsigned char end_sequence; /* End of (sequential) code sequence. */
1709 };
1710
1711 struct fileinfo
1712 {
1713 char * name;
1714 unsigned int dir;
1715 unsigned int time;
1716 unsigned int size;
1717 };
1718
1719 struct line_sequence
1720 {
1721 bfd_vma low_pc;
1722 struct line_sequence* prev_sequence;
1723 struct line_info* last_line; /* Largest VMA. */
1724 struct line_info** line_info_lookup;
1725 bfd_size_type num_lines;
1726 };
1727
1728 struct line_info_table
1729 {
1730 bfd * abfd;
1731 unsigned int num_files;
1732 unsigned int num_dirs;
1733 unsigned int num_sequences;
1734 char * comp_dir;
1735 char ** dirs;
1736 struct fileinfo* files;
1737 struct line_sequence* sequences;
1738 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
1739 };
1740
1741 /* Remember some information about each function. If the function is
1742 inlined (DW_TAG_inlined_subroutine) it may have two additional
1743 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1744 source code location where this function was inlined. */
1745
1746 struct funcinfo
1747 {
1748 /* Pointer to previous function in list of all functions. */
1749 struct funcinfo *prev_func;
1750 /* Pointer to function one scope higher. */
1751 struct funcinfo *caller_func;
1752 /* Source location file name where caller_func inlines this func. */
1753 char *caller_file;
1754 /* Source location file name. */
1755 char *file;
1756 /* Source location line number where caller_func inlines this func. */
1757 int caller_line;
1758 /* Source location line number. */
1759 int line;
1760 int tag;
1761 bool is_linkage;
1762 const char *name;
1763 struct arange arange;
1764 /* Where the symbol is defined. */
1765 asection *sec;
1766 /* The offset of the funcinfo from the start of the unit. */
1767 uint64_t unit_offset;
1768 };
1769
1770 struct lookup_funcinfo
1771 {
1772 /* Function information corresponding to this lookup table entry. */
1773 struct funcinfo *funcinfo;
1774
1775 /* The lowest address for this specific function. */
1776 bfd_vma low_addr;
1777
1778 /* The highest address of this function before the lookup table is sorted.
1779 The highest address of all prior functions after the lookup table is
1780 sorted, which is used for binary search. */
1781 bfd_vma high_addr;
1782 /* Index of this function, used to ensure qsort is stable. */
1783 unsigned int idx;
1784 };
1785
1786 struct varinfo
1787 {
1788 /* Pointer to previous variable in list of all variables. */
1789 struct varinfo *prev_var;
1790 /* The offset of the varinfo from the start of the unit. */
1791 uint64_t unit_offset;
1792 /* Source location file name. */
1793 char *file;
1794 /* Source location line number. */
1795 int line;
1796 /* The type of this variable. */
1797 int tag;
1798 /* The name of the variable, if it has one. */
1799 char *name;
1800 /* The address of the variable. */
1801 bfd_vma addr;
1802 /* Where the symbol is defined. */
1803 asection *sec;
1804 /* Is this a stack variable? */
1805 bool stack;
1806 };
1807
1808 /* Return TRUE if NEW_LINE should sort after LINE. */
1809
1810 static inline bool
1811 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1812 {
1813 return (new_line->address > line->address
1814 || (new_line->address == line->address
1815 && new_line->op_index > line->op_index));
1816 }
1817
1818
1819 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1820 that the list is sorted. Note that the line_info list is sorted from
1821 highest to lowest VMA (with possible duplicates); that is,
1822 line_info->prev_line always accesses an equal or smaller VMA. */
1823
1824 static bool
1825 add_line_info (struct line_info_table *table,
1826 bfd_vma address,
1827 unsigned char op_index,
1828 char *filename,
1829 unsigned int line,
1830 unsigned int column,
1831 unsigned int discriminator,
1832 int end_sequence)
1833 {
1834 size_t amt = sizeof (struct line_info);
1835 struct line_sequence* seq = table->sequences;
1836 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
1837
1838 if (info == NULL)
1839 return false;
1840
1841 /* Set member data of 'info'. */
1842 info->prev_line = NULL;
1843 info->address = address;
1844 info->op_index = op_index;
1845 info->line = line;
1846 info->column = column;
1847 info->discriminator = discriminator;
1848 info->end_sequence = end_sequence;
1849
1850 if (filename && filename[0])
1851 {
1852 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1853 if (info->filename == NULL)
1854 return false;
1855 strcpy (info->filename, filename);
1856 }
1857 else
1858 info->filename = NULL;
1859
1860 /* Find the correct location for 'info'. Normally we will receive
1861 new line_info data 1) in order and 2) with increasing VMAs.
1862 However some compilers break the rules (cf. decode_line_info) and
1863 so we include some heuristics for quickly finding the correct
1864 location for 'info'. In particular, these heuristics optimize for
1865 the common case in which the VMA sequence that we receive is a
1866 list of locally sorted VMAs such as
1867 p...z a...j (where a < j < p < z)
1868
1869 Note: table->lcl_head is used to head an *actual* or *possible*
1870 sub-sequence within the list (such as a...j) that is not directly
1871 headed by table->last_line
1872
1873 Note: we may receive duplicate entries from 'decode_line_info'. */
1874
1875 if (seq
1876 && seq->last_line->address == address
1877 && seq->last_line->op_index == op_index
1878 && seq->last_line->end_sequence == end_sequence)
1879 {
1880 /* We only keep the last entry with the same address and end
1881 sequence. See PR ld/4986. */
1882 if (table->lcl_head == seq->last_line)
1883 table->lcl_head = info;
1884 info->prev_line = seq->last_line->prev_line;
1885 seq->last_line = info;
1886 }
1887 else if (!seq || seq->last_line->end_sequence)
1888 {
1889 /* Start a new line sequence. */
1890 amt = sizeof (struct line_sequence);
1891 seq = (struct line_sequence *) bfd_malloc (amt);
1892 if (seq == NULL)
1893 return false;
1894 seq->low_pc = address;
1895 seq->prev_sequence = table->sequences;
1896 seq->last_line = info;
1897 table->lcl_head = info;
1898 table->sequences = seq;
1899 table->num_sequences++;
1900 }
1901 else if (info->end_sequence
1902 || new_line_sorts_after (info, seq->last_line))
1903 {
1904 /* Normal case: add 'info' to the beginning of the current sequence. */
1905 info->prev_line = seq->last_line;
1906 seq->last_line = info;
1907
1908 /* lcl_head: initialize to head a *possible* sequence at the end. */
1909 if (!table->lcl_head)
1910 table->lcl_head = info;
1911 }
1912 else if (!new_line_sorts_after (info, table->lcl_head)
1913 && (!table->lcl_head->prev_line
1914 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1915 {
1916 /* Abnormal but easy: lcl_head is the head of 'info'. */
1917 info->prev_line = table->lcl_head->prev_line;
1918 table->lcl_head->prev_line = info;
1919 }
1920 else
1921 {
1922 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1923 are valid heads for 'info'. Reset 'lcl_head'. */
1924 struct line_info* li2 = seq->last_line; /* Always non-NULL. */
1925 struct line_info* li1 = li2->prev_line;
1926
1927 while (li1)
1928 {
1929 if (!new_line_sorts_after (info, li2)
1930 && new_line_sorts_after (info, li1))
1931 break;
1932
1933 li2 = li1; /* always non-NULL */
1934 li1 = li1->prev_line;
1935 }
1936 table->lcl_head = li2;
1937 info->prev_line = table->lcl_head->prev_line;
1938 table->lcl_head->prev_line = info;
1939 if (address < seq->low_pc)
1940 seq->low_pc = address;
1941 }
1942 return true;
1943 }
1944
1945 /* Extract a fully qualified filename from a line info table.
1946 The returned string has been malloc'ed and it is the caller's
1947 responsibility to free it. */
1948
1949 static char *
1950 concat_filename (struct line_info_table *table, unsigned int file)
1951 {
1952 char *filename;
1953
1954 if (table == NULL || file - 1 >= table->num_files)
1955 {
1956 /* FILE == 0 means unknown. */
1957 if (file)
1958 _bfd_error_handler
1959 (_("DWARF error: mangled line number section (bad file number)"));
1960 return strdup ("<unknown>");
1961 }
1962
1963 filename = table->files[file - 1].name;
1964 if (filename == NULL)
1965 return strdup ("<unknown>");
1966
1967 if (!IS_ABSOLUTE_PATH (filename))
1968 {
1969 char *dir_name = NULL;
1970 char *subdir_name = NULL;
1971 char *name;
1972 size_t len;
1973
1974 if (table->files[file - 1].dir
1975 /* PR 17512: file: 0317e960. */
1976 && table->files[file - 1].dir <= table->num_dirs
1977 /* PR 17512: file: 7f3d2e4b. */
1978 && table->dirs != NULL)
1979 subdir_name = table->dirs[table->files[file - 1].dir - 1];
1980
1981 if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
1982 dir_name = table->comp_dir;
1983
1984 if (!dir_name)
1985 {
1986 dir_name = subdir_name;
1987 subdir_name = NULL;
1988 }
1989
1990 if (!dir_name)
1991 return strdup (filename);
1992
1993 len = strlen (dir_name) + strlen (filename) + 2;
1994
1995 if (subdir_name)
1996 {
1997 len += strlen (subdir_name) + 1;
1998 name = (char *) bfd_malloc (len);
1999 if (name)
2000 sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
2001 }
2002 else
2003 {
2004 name = (char *) bfd_malloc (len);
2005 if (name)
2006 sprintf (name, "%s/%s", dir_name, filename);
2007 }
2008
2009 return name;
2010 }
2011
2012 return strdup (filename);
2013 }
2014
2015 /* Number of bits in a bfd_vma. */
2016 #define VMA_BITS (8 * sizeof (bfd_vma))
2017
2018 /* Check whether [low1, high1) can be combined with [low2, high2),
2019 i.e., they touch or overlap. */
2020 static bool ranges_overlap (bfd_vma low1,
2021 bfd_vma high1,
2022 bfd_vma low2,
2023 bfd_vma high2)
2024 {
2025 if (low1 == low2 || high1 == high2)
2026 return true;
2027
2028 /* Sort so that low1 is below low2. */
2029 if (low1 > low2)
2030 {
2031 bfd_vma tmp;
2032
2033 tmp = low1;
2034 low1 = low2;
2035 low2 = tmp;
2036
2037 tmp = high1;
2038 high1 = high2;
2039 high2 = tmp;
2040 }
2041
2042 /* We touch iff low2 == high1.
2043 We overlap iff low2 is within [low1, high1). */
2044 return low2 <= high1;
2045 }
2046
2047 /* Insert an address range in the trie mapping addresses to compilation units.
2048 Will return the new trie node (usually the same as is being sent in, but
2049 in case of a leaf-to-interior conversion, or expansion of a leaf, it may be
2050 different), or NULL on failure.
2051 */
2052 static struct trie_node *insert_arange_in_trie(bfd *abfd,
2053 struct trie_node *trie,
2054 bfd_vma trie_pc,
2055 unsigned int trie_pc_bits,
2056 struct comp_unit *unit,
2057 bfd_vma low_pc,
2058 bfd_vma high_pc)
2059 {
2060 bfd_vma clamped_low_pc, clamped_high_pc;
2061 int ch, from_ch, to_ch;
2062 bool is_full_leaf = false;
2063
2064 /* See if we can extend any of the existing ranges. This merging
2065 isn't perfect (if merging opens up the possibility of merging two existing
2066 ranges, we won't find them), but it takes the majority of the cases. */
2067 if (trie->num_room_in_leaf > 0)
2068 {
2069 struct trie_leaf *leaf = (struct trie_leaf *) trie;
2070 unsigned int i;
2071
2072 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2073 {
2074 if (leaf->ranges[i].unit == unit
2075 && ranges_overlap (low_pc, high_pc,
2076 leaf->ranges[i].low_pc,
2077 leaf->ranges[i].high_pc))
2078 {
2079 if (low_pc < leaf->ranges[i].low_pc)
2080 leaf->ranges[i].low_pc = low_pc;
2081 if (high_pc > leaf->ranges[i].high_pc)
2082 leaf->ranges[i].high_pc = high_pc;
2083 return trie;
2084 }
2085 }
2086
2087 is_full_leaf = leaf->num_stored_in_leaf == trie->num_room_in_leaf;
2088 }
2089
2090 /* If we're a leaf with no more room and we're _not_ at the bottom,
2091 convert to an interior node. */
2092 if (is_full_leaf && trie_pc_bits < VMA_BITS)
2093 {
2094 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
2095 unsigned int i;
2096
2097 trie = bfd_zalloc (abfd, sizeof (struct trie_interior));
2098 if (!trie)
2099 return NULL;
2100 is_full_leaf = false;
2101
2102 /* TODO: If we wanted to save a little more memory at the cost of
2103 complexity, we could have reused the old leaf node as one of the
2104 children of the new interior node, instead of throwing it away. */
2105 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
2106 {
2107 if (!insert_arange_in_trie (abfd, trie, trie_pc, trie_pc_bits,
2108 leaf->ranges[i].unit, leaf->ranges[i].low_pc,
2109 leaf->ranges[i].high_pc))
2110 return NULL;
2111 }
2112 }
2113
2114 /* If we're a leaf with no more room and we _are_ at the bottom,
2115 we have no choice but to just make it larger. */
2116 if (is_full_leaf)
2117 {
2118 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
2119 unsigned int new_room_in_leaf = trie->num_room_in_leaf * 2;
2120 struct trie_leaf *new_leaf;
2121 size_t amt = (sizeof (struct trie_leaf)
2122 + ((new_room_in_leaf - TRIE_LEAF_SIZE)
2123 * sizeof (leaf->ranges[0])));
2124 new_leaf = bfd_zalloc (abfd, amt);
2125 new_leaf->head.num_room_in_leaf = new_room_in_leaf;
2126 new_leaf->num_stored_in_leaf = leaf->num_stored_in_leaf;
2127
2128 memcpy (new_leaf->ranges,
2129 leaf->ranges,
2130 leaf->num_stored_in_leaf * sizeof (leaf->ranges[0]));
2131 trie = &new_leaf->head;
2132 is_full_leaf = false;
2133
2134 /* Now the insert below will go through. */
2135 }
2136
2137 /* If we're a leaf (now with room), we can just insert at the end. */
2138 if (trie->num_room_in_leaf > 0)
2139 {
2140 struct trie_leaf *leaf = (struct trie_leaf *) trie;
2141
2142 unsigned int i = leaf->num_stored_in_leaf++;
2143 leaf->ranges[i].unit = unit;
2144 leaf->ranges[i].low_pc = low_pc;
2145 leaf->ranges[i].high_pc = high_pc;
2146 return trie;
2147 }
2148
2149 /* Now we are definitely an interior node, so recurse into all
2150 the relevant buckets. */
2151
2152 /* Clamp the range to the current trie bucket. */
2153 clamped_low_pc = low_pc;
2154 clamped_high_pc = high_pc;
2155 if (trie_pc_bits > 0)
2156 {
2157 bfd_vma bucket_high_pc =
2158 trie_pc + ((bfd_vma) -1 >> trie_pc_bits); /* Inclusive. */
2159 if (clamped_low_pc < trie_pc)
2160 clamped_low_pc = trie_pc;
2161 if (clamped_high_pc > bucket_high_pc)
2162 clamped_high_pc = bucket_high_pc;
2163 }
2164
2165 /* Insert the ranges in all buckets that it spans. */
2166 from_ch = (clamped_low_pc >> (VMA_BITS - trie_pc_bits - 8)) & 0xff;
2167 to_ch = ((clamped_high_pc - 1) >> (VMA_BITS - trie_pc_bits - 8)) & 0xff;
2168 for (ch = from_ch; ch <= to_ch; ++ch)
2169 {
2170 struct trie_interior *interior = (struct trie_interior *) trie;
2171 struct trie_node *child = interior->children[ch];
2172
2173 if (child == NULL)
2174 {
2175 child = alloc_trie_leaf (abfd);
2176 if (!child)
2177 return NULL;
2178 }
2179 bfd_vma bucket = (bfd_vma) ch << (VMA_BITS - trie_pc_bits - 8);
2180 child = insert_arange_in_trie (abfd,
2181 child,
2182 trie_pc + bucket,
2183 trie_pc_bits + 8,
2184 unit,
2185 low_pc,
2186 high_pc);
2187 if (!child)
2188 return NULL;
2189
2190 interior->children[ch] = child;
2191 }
2192
2193 return trie;
2194 }
2195
2196
2197 static bool
2198 arange_add (struct comp_unit *unit, struct arange *first_arange,
2199 struct trie_node **trie_root, bfd_vma low_pc, bfd_vma high_pc)
2200 {
2201 struct arange *arange;
2202
2203 /* Ignore empty ranges. */
2204 if (low_pc == high_pc)
2205 return true;
2206
2207 if (trie_root != NULL)
2208 {
2209 *trie_root = insert_arange_in_trie (unit->file->bfd_ptr,
2210 *trie_root,
2211 0,
2212 0,
2213 unit,
2214 low_pc,
2215 high_pc);
2216 if (*trie_root == NULL)
2217 return false;
2218 }
2219
2220 /* If the first arange is empty, use it. */
2221 if (first_arange->high == 0)
2222 {
2223 first_arange->low = low_pc;
2224 first_arange->high = high_pc;
2225 return true;
2226 }
2227
2228 /* Next see if we can cheaply extend an existing range. */
2229 arange = first_arange;
2230 do
2231 {
2232 if (low_pc == arange->high)
2233 {
2234 arange->high = high_pc;
2235 return true;
2236 }
2237 if (high_pc == arange->low)
2238 {
2239 arange->low = low_pc;
2240 return true;
2241 }
2242 arange = arange->next;
2243 }
2244 while (arange);
2245
2246 /* Need to allocate a new arange and insert it into the arange list.
2247 Order isn't significant, so just insert after the first arange. */
2248 arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
2249 if (arange == NULL)
2250 return false;
2251 arange->low = low_pc;
2252 arange->high = high_pc;
2253 arange->next = first_arange->next;
2254 first_arange->next = arange;
2255 return true;
2256 }
2257
2258 /* Compare function for line sequences. */
2259
2260 static int
2261 compare_sequences (const void* a, const void* b)
2262 {
2263 const struct line_sequence* seq1 = a;
2264 const struct line_sequence* seq2 = b;
2265
2266 /* Sort by low_pc as the primary key. */
2267 if (seq1->low_pc < seq2->low_pc)
2268 return -1;
2269 if (seq1->low_pc > seq2->low_pc)
2270 return 1;
2271
2272 /* If low_pc values are equal, sort in reverse order of
2273 high_pc, so that the largest region comes first. */
2274 if (seq1->last_line->address < seq2->last_line->address)
2275 return 1;
2276 if (seq1->last_line->address > seq2->last_line->address)
2277 return -1;
2278
2279 if (seq1->last_line->op_index < seq2->last_line->op_index)
2280 return 1;
2281 if (seq1->last_line->op_index > seq2->last_line->op_index)
2282 return -1;
2283
2284 /* num_lines is initially an index, to make the sort stable. */
2285 if (seq1->num_lines < seq2->num_lines)
2286 return -1;
2287 if (seq1->num_lines > seq2->num_lines)
2288 return 1;
2289 return 0;
2290 }
2291
2292 /* Construct the line information table for quick lookup. */
2293
2294 static bool
2295 build_line_info_table (struct line_info_table * table,
2296 struct line_sequence * seq)
2297 {
2298 size_t amt;
2299 struct line_info **line_info_lookup;
2300 struct line_info *each_line;
2301 unsigned int num_lines;
2302 unsigned int line_index;
2303
2304 if (seq->line_info_lookup != NULL)
2305 return true;
2306
2307 /* Count the number of line information entries. We could do this while
2308 scanning the debug information, but some entries may be added via
2309 lcl_head without having a sequence handy to increment the number of
2310 lines. */
2311 num_lines = 0;
2312 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
2313 num_lines++;
2314
2315 seq->num_lines = num_lines;
2316 if (num_lines == 0)
2317 return true;
2318
2319 /* Allocate space for the line information lookup table. */
2320 amt = sizeof (struct line_info*) * num_lines;
2321 line_info_lookup = (struct line_info**) bfd_alloc (table->abfd, amt);
2322 seq->line_info_lookup = line_info_lookup;
2323 if (line_info_lookup == NULL)
2324 return false;
2325
2326 /* Create the line information lookup table. */
2327 line_index = num_lines;
2328 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
2329 line_info_lookup[--line_index] = each_line;
2330
2331 BFD_ASSERT (line_index == 0);
2332 return true;
2333 }
2334
2335 /* Sort the line sequences for quick lookup. */
2336
2337 static bool
2338 sort_line_sequences (struct line_info_table* table)
2339 {
2340 size_t amt;
2341 struct line_sequence *sequences;
2342 struct line_sequence *seq;
2343 unsigned int n = 0;
2344 unsigned int num_sequences = table->num_sequences;
2345 bfd_vma last_high_pc;
2346
2347 if (num_sequences == 0)
2348 return true;
2349
2350 /* Allocate space for an array of sequences. */
2351 amt = sizeof (struct line_sequence) * num_sequences;
2352 sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
2353 if (sequences == NULL)
2354 return false;
2355
2356 /* Copy the linked list into the array, freeing the original nodes. */
2357 seq = table->sequences;
2358 for (n = 0; n < num_sequences; n++)
2359 {
2360 struct line_sequence* last_seq = seq;
2361
2362 BFD_ASSERT (seq);
2363 sequences[n].low_pc = seq->low_pc;
2364 sequences[n].prev_sequence = NULL;
2365 sequences[n].last_line = seq->last_line;
2366 sequences[n].line_info_lookup = NULL;
2367 sequences[n].num_lines = n;
2368 seq = seq->prev_sequence;
2369 free (last_seq);
2370 }
2371 BFD_ASSERT (seq == NULL);
2372
2373 qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
2374
2375 /* Make the list binary-searchable by trimming overlapping entries
2376 and removing nested entries. */
2377 num_sequences = 1;
2378 last_high_pc = sequences[0].last_line->address;
2379 for (n = 1; n < table->num_sequences; n++)
2380 {
2381 if (sequences[n].low_pc < last_high_pc)
2382 {
2383 if (sequences[n].last_line->address <= last_high_pc)
2384 /* Skip nested entries. */
2385 continue;
2386
2387 /* Trim overlapping entries. */
2388 sequences[n].low_pc = last_high_pc;
2389 }
2390 last_high_pc = sequences[n].last_line->address;
2391 if (n > num_sequences)
2392 {
2393 /* Close up the gap. */
2394 sequences[num_sequences].low_pc = sequences[n].low_pc;
2395 sequences[num_sequences].last_line = sequences[n].last_line;
2396 }
2397 num_sequences++;
2398 }
2399
2400 table->sequences = sequences;
2401 table->num_sequences = num_sequences;
2402 return true;
2403 }
2404
2405 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
2406
2407 static bool
2408 line_info_add_include_dir (struct line_info_table *table, char *cur_dir)
2409 {
2410 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
2411 {
2412 char **tmp;
2413 size_t amt;
2414
2415 amt = table->num_dirs + DIR_ALLOC_CHUNK;
2416 amt *= sizeof (char *);
2417
2418 tmp = (char **) bfd_realloc (table->dirs, amt);
2419 if (tmp == NULL)
2420 return false;
2421 table->dirs = tmp;
2422 }
2423
2424 table->dirs[table->num_dirs++] = cur_dir;
2425 return true;
2426 }
2427
2428 static bool
2429 line_info_add_include_dir_stub (struct line_info_table *table, char *cur_dir,
2430 unsigned int dir ATTRIBUTE_UNUSED,
2431 unsigned int xtime ATTRIBUTE_UNUSED,
2432 unsigned int size ATTRIBUTE_UNUSED)
2433 {
2434 return line_info_add_include_dir (table, cur_dir);
2435 }
2436
2437 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
2438
2439 static bool
2440 line_info_add_file_name (struct line_info_table *table, char *cur_file,
2441 unsigned int dir, unsigned int xtime,
2442 unsigned int size)
2443 {
2444 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
2445 {
2446 struct fileinfo *tmp;
2447 size_t amt;
2448
2449 amt = table->num_files + FILE_ALLOC_CHUNK;
2450 amt *= sizeof (struct fileinfo);
2451
2452 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
2453 if (tmp == NULL)
2454 return false;
2455 table->files = tmp;
2456 }
2457
2458 table->files[table->num_files].name = cur_file;
2459 table->files[table->num_files].dir = dir;
2460 table->files[table->num_files].time = xtime;
2461 table->files[table->num_files].size = size;
2462 table->num_files++;
2463 return true;
2464 }
2465
2466 /* Read directory or file name entry format, starting with byte of
2467 format count entries, ULEB128 pairs of entry formats, ULEB128 of
2468 entries count and the entries themselves in the described entry
2469 format. */
2470
2471 static bool
2472 read_formatted_entries (struct comp_unit *unit, bfd_byte **bufp,
2473 bfd_byte *buf_end, struct line_info_table *table,
2474 bool (*callback) (struct line_info_table *table,
2475 char *cur_file,
2476 unsigned int dir,
2477 unsigned int time,
2478 unsigned int size))
2479 {
2480 bfd *abfd = unit->abfd;
2481 bfd_byte format_count, formati;
2482 bfd_vma data_count, datai;
2483 bfd_byte *buf = *bufp;
2484 bfd_byte *format_header_data;
2485
2486 format_count = read_1_byte (abfd, &buf, buf_end);
2487 format_header_data = buf;
2488 for (formati = 0; formati < format_count; formati++)
2489 {
2490 _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2491 _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2492 }
2493
2494 data_count = _bfd_safe_read_leb128 (abfd, &buf, false, buf_end);
2495 if (format_count == 0 && data_count != 0)
2496 {
2497 _bfd_error_handler (_("DWARF error: zero format count"));
2498 bfd_set_error (bfd_error_bad_value);
2499 return false;
2500 }
2501
2502 /* PR 22210. Paranoia check. Don't bother running the loop
2503 if we know that we are going to run out of buffer. */
2504 if (data_count > (bfd_vma) (buf_end - buf))
2505 {
2506 _bfd_error_handler
2507 (_("DWARF error: data count (%" PRIx64 ") larger than buffer size"),
2508 (uint64_t) data_count);
2509 bfd_set_error (bfd_error_bad_value);
2510 return false;
2511 }
2512
2513 for (datai = 0; datai < data_count; datai++)
2514 {
2515 bfd_byte *format = format_header_data;
2516 struct fileinfo fe;
2517
2518 memset (&fe, 0, sizeof fe);
2519 for (formati = 0; formati < format_count; formati++)
2520 {
2521 bfd_vma content_type, form;
2522 char *string_trash;
2523 char **stringp = &string_trash;
2524 unsigned int uint_trash, *uintp = &uint_trash;
2525 struct attribute attr;
2526
2527 content_type = _bfd_safe_read_leb128 (abfd, &format, false, buf_end);
2528 switch (content_type)
2529 {
2530 case DW_LNCT_path:
2531 stringp = &fe.name;
2532 break;
2533 case DW_LNCT_directory_index:
2534 uintp = &fe.dir;
2535 break;
2536 case DW_LNCT_timestamp:
2537 uintp = &fe.time;
2538 break;
2539 case DW_LNCT_size:
2540 uintp = &fe.size;
2541 break;
2542 case DW_LNCT_MD5:
2543 break;
2544 default:
2545 _bfd_error_handler
2546 (_("DWARF error: unknown format content type %" PRIu64),
2547 (uint64_t) content_type);
2548 bfd_set_error (bfd_error_bad_value);
2549 return false;
2550 }
2551
2552 form = _bfd_safe_read_leb128 (abfd, &format, false, buf_end);
2553 buf = read_attribute_value (&attr, form, 0, unit, buf, buf_end);
2554 if (buf == NULL)
2555 return false;
2556 switch (form)
2557 {
2558 case DW_FORM_string:
2559 case DW_FORM_line_strp:
2560 case DW_FORM_strx:
2561 case DW_FORM_strx1:
2562 case DW_FORM_strx2:
2563 case DW_FORM_strx3:
2564 case DW_FORM_strx4:
2565 *stringp = attr.u.str;
2566 break;
2567
2568 case DW_FORM_data1:
2569 case DW_FORM_data2:
2570 case DW_FORM_data4:
2571 case DW_FORM_data8:
2572 case DW_FORM_udata:
2573 *uintp = attr.u.val;
2574 break;
2575
2576 case DW_FORM_data16:
2577 /* MD5 data is in the attr.blk, but we are ignoring those. */
2578 break;
2579 }
2580 }
2581
2582 /* Skip the first "zero entry", which is the compilation dir/file. */
2583 if (datai != 0)
2584 if (!callback (table, fe.name, fe.dir, fe.time, fe.size))
2585 return false;
2586 }
2587
2588 *bufp = buf;
2589 return true;
2590 }
2591
2592 /* Decode the line number information for UNIT. */
2593
2594 static struct line_info_table*
2595 decode_line_info (struct comp_unit *unit)
2596 {
2597 bfd *abfd = unit->abfd;
2598 struct dwarf2_debug *stash = unit->stash;
2599 struct dwarf2_debug_file *file = unit->file;
2600 struct line_info_table* table;
2601 bfd_byte *line_ptr;
2602 bfd_byte *line_end;
2603 struct line_head lh;
2604 unsigned int i, offset_size;
2605 char *cur_file, *cur_dir;
2606 unsigned char op_code, extended_op, adj_opcode;
2607 unsigned int exop_len;
2608 size_t amt;
2609
2610 if (unit->line_offset == 0 && file->line_table)
2611 return file->line_table;
2612
2613 if (! read_section (abfd, &stash->debug_sections[debug_line],
2614 file->syms, unit->line_offset,
2615 &file->dwarf_line_buffer, &file->dwarf_line_size))
2616 return NULL;
2617
2618 if (file->dwarf_line_size < 16)
2619 {
2620 _bfd_error_handler
2621 (_("DWARF error: line info section is too small (%" PRId64 ")"),
2622 (int64_t) file->dwarf_line_size);
2623 bfd_set_error (bfd_error_bad_value);
2624 return NULL;
2625 }
2626 line_ptr = file->dwarf_line_buffer + unit->line_offset;
2627 line_end = file->dwarf_line_buffer + file->dwarf_line_size;
2628
2629 /* Read in the prologue. */
2630 lh.total_length = read_4_bytes (abfd, &line_ptr, line_end);
2631 offset_size = 4;
2632 if (lh.total_length == 0xffffffff)
2633 {
2634 lh.total_length = read_8_bytes (abfd, &line_ptr, line_end);
2635 offset_size = 8;
2636 }
2637 else if (lh.total_length == 0 && unit->addr_size == 8)
2638 {
2639 /* Handle (non-standard) 64-bit DWARF2 formats. */
2640 lh.total_length = read_4_bytes (abfd, &line_ptr, line_end);
2641 offset_size = 8;
2642 }
2643
2644 if (lh.total_length > (size_t) (line_end - line_ptr))
2645 {
2646 _bfd_error_handler
2647 /* xgettext: c-format */
2648 (_("DWARF error: line info data is bigger (%#" PRIx64 ")"
2649 " than the space remaining in the section (%#lx)"),
2650 (uint64_t) lh.total_length, (unsigned long) (line_end - line_ptr));
2651 bfd_set_error (bfd_error_bad_value);
2652 return NULL;
2653 }
2654
2655 line_end = line_ptr + lh.total_length;
2656
2657 lh.version = read_2_bytes (abfd, &line_ptr, line_end);
2658 if (lh.version < 2 || lh.version > 5)
2659 {
2660 _bfd_error_handler
2661 (_("DWARF error: unhandled .debug_line version %d"), lh.version);
2662 bfd_set_error (bfd_error_bad_value);
2663 return NULL;
2664 }
2665
2666 if (line_ptr + offset_size + (lh.version >= 5 ? 8 : (lh.version >= 4 ? 6 : 5))
2667 >= line_end)
2668 {
2669 _bfd_error_handler
2670 (_("DWARF error: ran out of room reading prologue"));
2671 bfd_set_error (bfd_error_bad_value);
2672 return NULL;
2673 }
2674
2675 if (lh.version >= 5)
2676 {
2677 unsigned int segment_selector_size;
2678
2679 /* Skip address size. */
2680 read_1_byte (abfd, &line_ptr, line_end);
2681
2682 segment_selector_size = read_1_byte (abfd, &line_ptr, line_end);
2683 if (segment_selector_size != 0)
2684 {
2685 _bfd_error_handler
2686 (_("DWARF error: line info unsupported segment selector size %u"),
2687 segment_selector_size);
2688 bfd_set_error (bfd_error_bad_value);
2689 return NULL;
2690 }
2691 }
2692
2693 if (offset_size == 4)
2694 lh.prologue_length = read_4_bytes (abfd, &line_ptr, line_end);
2695 else
2696 lh.prologue_length = read_8_bytes (abfd, &line_ptr, line_end);
2697
2698 lh.minimum_instruction_length = read_1_byte (abfd, &line_ptr, line_end);
2699
2700 if (lh.version >= 4)
2701 lh.maximum_ops_per_insn = read_1_byte (abfd, &line_ptr, line_end);
2702 else
2703 lh.maximum_ops_per_insn = 1;
2704
2705 if (lh.maximum_ops_per_insn == 0)
2706 {
2707 _bfd_error_handler
2708 (_("DWARF error: invalid maximum operations per instruction"));
2709 bfd_set_error (bfd_error_bad_value);
2710 return NULL;
2711 }
2712
2713 lh.default_is_stmt = read_1_byte (abfd, &line_ptr, line_end);
2714 lh.line_base = read_1_signed_byte (abfd, &line_ptr, line_end);
2715 lh.line_range = read_1_byte (abfd, &line_ptr, line_end);
2716 lh.opcode_base = read_1_byte (abfd, &line_ptr, line_end);
2717
2718 if (line_ptr + (lh.opcode_base - 1) >= line_end)
2719 {
2720 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2721 bfd_set_error (bfd_error_bad_value);
2722 return NULL;
2723 }
2724
2725 amt = lh.opcode_base * sizeof (unsigned char);
2726 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
2727
2728 lh.standard_opcode_lengths[0] = 1;
2729
2730 for (i = 1; i < lh.opcode_base; ++i)
2731 lh.standard_opcode_lengths[i] = read_1_byte (abfd, &line_ptr, line_end);
2732
2733 amt = sizeof (struct line_info_table);
2734 table = (struct line_info_table *) bfd_alloc (abfd, amt);
2735 if (table == NULL)
2736 return NULL;
2737 table->abfd = abfd;
2738 table->comp_dir = unit->comp_dir;
2739
2740 table->num_files = 0;
2741 table->files = NULL;
2742
2743 table->num_dirs = 0;
2744 table->dirs = NULL;
2745
2746 table->num_sequences = 0;
2747 table->sequences = NULL;
2748
2749 table->lcl_head = NULL;
2750
2751 if (lh.version >= 5)
2752 {
2753 /* Read directory table. */
2754 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2755 line_info_add_include_dir_stub))
2756 goto fail;
2757
2758 /* Read file name table. */
2759 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2760 line_info_add_file_name))
2761 goto fail;
2762 }
2763 else
2764 {
2765 /* Read directory table. */
2766 while ((cur_dir = read_string (&line_ptr, line_end)) != NULL)
2767 {
2768 if (!line_info_add_include_dir (table, cur_dir))
2769 goto fail;
2770 }
2771
2772 /* Read file name table. */
2773 while ((cur_file = read_string (&line_ptr, line_end)) != NULL)
2774 {
2775 unsigned int dir, xtime, size;
2776
2777 dir = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2778 xtime = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2779 size = _bfd_safe_read_leb128 (abfd, &line_ptr, false, line_end);
2780
2781 if (!line_info_add_file_name (table, cur_file, dir, xtime, size))
2782 goto fail;
2783 }
2784 }
2785
2786 /* Read the statement sequences until there's nothing left. */
2787 while (line_ptr < line_end)
2788 {
2789 /* State machine registers. */
2790 bfd_vma address = 0;
2791 unsigned char op_index = 0;
2792 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
2793 unsigned int line = 1;
2794 unsigned int column = 0;
2795 unsigned int discriminator = 0;
2796 int is_stmt = lh.default_is_stmt;
2797 int end_sequence = 0;
2798 unsigned int dir, xtime, size;
2799 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2800 compilers generate address sequences that are wildly out of
2801 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2802 for ia64-Linux). Thus, to determine the low and high
2803 address, we must compare on every DW_LNS_copy, etc. */
2804 bfd_vma low_pc = (bfd_vma) -1;
2805 bfd_vma high_pc = 0;
2806
2807 /* Decode the table. */
2808 while (!end_sequence && line_ptr < line_end)
2809 {
2810 op_code = read_1_byte (abfd, &line_ptr, line_end);
2811
2812 if (op_code >= lh.opcode_base)
2813 {
2814 /* Special operand. */
2815 adj_opcode = op_code - lh.opcode_base;
2816 if (lh.line_range == 0)
2817 goto line_fail;
2818 if (lh.maximum_ops_per_insn == 1)
2819 address += (adj_opcode / lh.line_range
2820 * lh.minimum_instruction_length);
2821 else
2822 {
2823 address += ((op_index + adj_opcode / lh.line_range)
2824 / lh.maximum_ops_per_insn
2825 * lh.minimum_instruction_length);
2826 op_index = ((op_index + adj_opcode / lh.line_range)
2827 % lh.maximum_ops_per_insn);
2828 }
2829 line += lh.line_base + (adj_opcode % lh.line_range);
2830 /* Append row to matrix using current values. */
2831 if (!add_line_info (table, address, op_index, filename,
2832 line, column, discriminator, 0))
2833 goto line_fail;
2834 discriminator = 0;
2835 if (address < low_pc)
2836 low_pc = address;
2837 if (address > high_pc)
2838 high_pc = address;
2839 }
2840 else switch (op_code)
2841 {
2842 case DW_LNS_extended_op:
2843 exop_len = _bfd_safe_read_leb128 (abfd, &line_ptr,
2844 false, line_end);
2845 extended_op = read_1_byte (abfd, &line_ptr, line_end);
2846
2847 switch (extended_op)
2848 {
2849 case DW_LNE_end_sequence:
2850 end_sequence = 1;
2851 if (!add_line_info (table, address, op_index, filename, line,
2852 column, discriminator, end_sequence))
2853 goto line_fail;
2854 discriminator = 0;
2855 if (address < low_pc)
2856 low_pc = address;
2857 if (address > high_pc)
2858 high_pc = address;
2859 if (!arange_add (unit, &unit->arange, &unit->file->trie_root,
2860 low_pc, high_pc))
2861 goto line_fail;
2862 break;
2863 case DW_LNE_set_address:
2864 address = read_address (unit, &line_ptr, line_end);
2865 op_index = 0;
2866 break;
2867 case DW_LNE_define_file:
2868 cur_file = read_string (&line_ptr, line_end);
2869 dir = _bfd_safe_read_leb128 (abfd, &line_ptr,
2870 false, line_end);
2871 xtime = _bfd_safe_read_leb128 (abfd, &line_ptr,
2872 false, line_end);
2873 size = _bfd_safe_read_leb128 (abfd, &line_ptr,
2874 false, line_end);
2875 if (!line_info_add_file_name (table, cur_file, dir,
2876 xtime, size))
2877 goto line_fail;
2878 break;
2879 case DW_LNE_set_discriminator:
2880 discriminator = _bfd_safe_read_leb128 (abfd, &line_ptr,
2881 false, line_end);
2882 break;
2883 case DW_LNE_HP_source_file_correlation:
2884 line_ptr += exop_len - 1;
2885 break;
2886 default:
2887 _bfd_error_handler
2888 (_("DWARF error: mangled line number section"));
2889 bfd_set_error (bfd_error_bad_value);
2890 line_fail:
2891 free (filename);
2892 goto fail;
2893 }
2894 break;
2895 case DW_LNS_copy:
2896 if (!add_line_info (table, address, op_index,
2897 filename, line, column, discriminator, 0))
2898 goto line_fail;
2899 discriminator = 0;
2900 if (address < low_pc)
2901 low_pc = address;
2902 if (address > high_pc)
2903 high_pc = address;
2904 break;
2905 case DW_LNS_advance_pc:
2906 if (lh.maximum_ops_per_insn == 1)
2907 address += (lh.minimum_instruction_length
2908 * _bfd_safe_read_leb128 (abfd, &line_ptr,
2909 false, line_end));
2910 else
2911 {
2912 bfd_vma adjust = _bfd_safe_read_leb128 (abfd, &line_ptr,
2913 false, line_end);
2914 address = ((op_index + adjust) / lh.maximum_ops_per_insn
2915 * lh.minimum_instruction_length);
2916 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
2917 }
2918 break;
2919 case DW_LNS_advance_line:
2920 line += _bfd_safe_read_leb128 (abfd, &line_ptr,
2921 true, line_end);
2922 break;
2923 case DW_LNS_set_file:
2924 {
2925 unsigned int filenum;
2926
2927 /* The file and directory tables are 0
2928 based, the references are 1 based. */
2929 filenum = _bfd_safe_read_leb128 (abfd, &line_ptr,
2930 false, line_end);
2931 free (filename);
2932 filename = concat_filename (table, filenum);
2933 break;
2934 }
2935 case DW_LNS_set_column:
2936 column = _bfd_safe_read_leb128 (abfd, &line_ptr,
2937 false, line_end);
2938 break;
2939 case DW_LNS_negate_stmt:
2940 is_stmt = (!is_stmt);
2941 break;
2942 case DW_LNS_set_basic_block:
2943 break;
2944 case DW_LNS_const_add_pc:
2945 if (lh.line_range == 0)
2946 goto line_fail;
2947 if (lh.maximum_ops_per_insn == 1)
2948 address += (lh.minimum_instruction_length
2949 * ((255 - lh.opcode_base) / lh.line_range));
2950 else
2951 {
2952 bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
2953 address += (lh.minimum_instruction_length
2954 * ((op_index + adjust)
2955 / lh.maximum_ops_per_insn));
2956 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
2957 }
2958 break;
2959 case DW_LNS_fixed_advance_pc:
2960 address += read_2_bytes (abfd, &line_ptr, line_end);
2961 op_index = 0;
2962 break;
2963 default:
2964 /* Unknown standard opcode, ignore it. */
2965 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
2966 (void) _bfd_safe_read_leb128 (abfd, &line_ptr,
2967 false, line_end);
2968 break;
2969 }
2970 }
2971
2972 free (filename);
2973 }
2974
2975 if (unit->line_offset == 0)
2976 file->line_table = table;
2977 if (sort_line_sequences (table))
2978 return table;
2979
2980 fail:
2981 while (table->sequences != NULL)
2982 {
2983 struct line_sequence* seq = table->sequences;
2984 table->sequences = table->sequences->prev_sequence;
2985 free (seq);
2986 }
2987 free (table->files);
2988 free (table->dirs);
2989 return NULL;
2990 }
2991
2992 /* If ADDR is within TABLE set the output parameters and return TRUE,
2993 otherwise set *FILENAME_PTR to NULL and return FALSE.
2994 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2995 are pointers to the objects to be filled in. */
2996
2997 static bool
2998 lookup_address_in_line_info_table (struct line_info_table *table,
2999 bfd_vma addr,
3000 const char **filename_ptr,
3001 unsigned int *linenumber_ptr,
3002 unsigned int *discriminator_ptr)
3003 {
3004 struct line_sequence *seq = NULL;
3005 struct line_info *info;
3006 int low, high, mid;
3007
3008 /* Binary search the array of sequences. */
3009 low = 0;
3010 high = table->num_sequences;
3011 while (low < high)
3012 {
3013 mid = (low + high) / 2;
3014 seq = &table->sequences[mid];
3015 if (addr < seq->low_pc)
3016 high = mid;
3017 else if (addr >= seq->last_line->address)
3018 low = mid + 1;
3019 else
3020 break;
3021 }
3022
3023 /* Check for a valid sequence. */
3024 if (!seq || addr < seq->low_pc || addr >= seq->last_line->address)
3025 goto fail;
3026
3027 if (!build_line_info_table (table, seq))
3028 goto fail;
3029
3030 /* Binary search the array of line information. */
3031 low = 0;
3032 high = seq->num_lines;
3033 info = NULL;
3034 while (low < high)
3035 {
3036 mid = (low + high) / 2;
3037 info = seq->line_info_lookup[mid];
3038 if (addr < info->address)
3039 high = mid;
3040 else if (addr >= seq->line_info_lookup[mid + 1]->address)
3041 low = mid + 1;
3042 else
3043 break;
3044 }
3045
3046 /* Check for a valid line information entry. */
3047 if (info
3048 && addr >= info->address
3049 && addr < seq->line_info_lookup[mid + 1]->address
3050 && !(info->end_sequence || info == seq->last_line))
3051 {
3052 *filename_ptr = info->filename;
3053 *linenumber_ptr = info->line;
3054 if (discriminator_ptr)
3055 *discriminator_ptr = info->discriminator;
3056 return true;
3057 }
3058
3059 fail:
3060 *filename_ptr = NULL;
3061 return false;
3062 }
3063
3064 /* Read in the .debug_ranges section for future reference. */
3065
3066 static bool
3067 read_debug_ranges (struct comp_unit * unit)
3068 {
3069 struct dwarf2_debug *stash = unit->stash;
3070 struct dwarf2_debug_file *file = unit->file;
3071
3072 return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
3073 file->syms, 0,
3074 &file->dwarf_ranges_buffer, &file->dwarf_ranges_size);
3075 }
3076
3077 /* Read in the .debug_rnglists section for future reference. */
3078
3079 static bool
3080 read_debug_rnglists (struct comp_unit * unit)
3081 {
3082 struct dwarf2_debug *stash = unit->stash;
3083 struct dwarf2_debug_file *file = unit->file;
3084
3085 return read_section (unit->abfd, &stash->debug_sections[debug_rnglists],
3086 file->syms, 0,
3087 &file->dwarf_rnglists_buffer, &file->dwarf_rnglists_size);
3088 }
3089
3090 /* Function table functions. */
3091
3092 static int
3093 compare_lookup_funcinfos (const void * a, const void * b)
3094 {
3095 const struct lookup_funcinfo * lookup1 = a;
3096 const struct lookup_funcinfo * lookup2 = b;
3097
3098 if (lookup1->low_addr < lookup2->low_addr)
3099 return -1;
3100 if (lookup1->low_addr > lookup2->low_addr)
3101 return 1;
3102 if (lookup1->high_addr < lookup2->high_addr)
3103 return -1;
3104 if (lookup1->high_addr > lookup2->high_addr)
3105 return 1;
3106
3107 if (lookup1->idx < lookup2->idx)
3108 return -1;
3109 if (lookup1->idx > lookup2->idx)
3110 return 1;
3111 return 0;
3112 }
3113
3114 static bool
3115 build_lookup_funcinfo_table (struct comp_unit * unit)
3116 {
3117 struct lookup_funcinfo *lookup_funcinfo_table = unit->lookup_funcinfo_table;
3118 unsigned int number_of_functions = unit->number_of_functions;
3119 struct funcinfo *each;
3120 struct lookup_funcinfo *entry;
3121 size_t func_index;
3122 struct arange *range;
3123 bfd_vma low_addr, high_addr;
3124
3125 if (lookup_funcinfo_table || number_of_functions == 0)
3126 return true;
3127
3128 /* Create the function info lookup table. */
3129 lookup_funcinfo_table = (struct lookup_funcinfo *)
3130 bfd_malloc (number_of_functions * sizeof (struct lookup_funcinfo));
3131 if (lookup_funcinfo_table == NULL)
3132 return false;
3133
3134 /* Populate the function info lookup table. */
3135 func_index = number_of_functions;
3136 for (each = unit->function_table; each; each = each->prev_func)
3137 {
3138 entry = &lookup_funcinfo_table[--func_index];
3139 entry->funcinfo = each;
3140 entry->idx = func_index;
3141
3142 /* Calculate the lowest and highest address for this function entry. */
3143 low_addr = entry->funcinfo->arange.low;
3144 high_addr = entry->funcinfo->arange.high;
3145
3146 for (range = entry->funcinfo->arange.next; range; range = range->next)
3147 {
3148 if (range->low < low_addr)
3149 low_addr = range->low;
3150 if (range->high > high_addr)
3151 high_addr = range->high;
3152 }
3153
3154 entry->low_addr = low_addr;
3155 entry->high_addr = high_addr;
3156 }
3157
3158 BFD_ASSERT (func_index == 0);
3159
3160 /* Sort the function by address. */
3161 qsort (lookup_funcinfo_table,
3162 number_of_functions,
3163 sizeof (struct lookup_funcinfo),
3164 compare_lookup_funcinfos);
3165
3166 /* Calculate the high watermark for each function in the lookup table. */
3167 high_addr = lookup_funcinfo_table[0].high_addr;
3168 for (func_index = 1; func_index < number_of_functions; func_index++)
3169 {
3170 entry = &lookup_funcinfo_table[func_index];
3171 if (entry->high_addr > high_addr)
3172 high_addr = entry->high_addr;
3173 else
3174 entry->high_addr = high_addr;
3175 }
3176
3177 unit->lookup_funcinfo_table = lookup_funcinfo_table;
3178 return true;
3179 }
3180
3181 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
3182 TRUE. Note that we need to find the function that has the smallest range
3183 that contains ADDR, to handle inlined functions without depending upon
3184 them being ordered in TABLE by increasing range. */
3185
3186 static bool
3187 lookup_address_in_function_table (struct comp_unit *unit,
3188 bfd_vma addr,
3189 struct funcinfo **function_ptr)
3190 {
3191 unsigned int number_of_functions = unit->number_of_functions;
3192 struct lookup_funcinfo* lookup_funcinfo = NULL;
3193 struct funcinfo* funcinfo = NULL;
3194 struct funcinfo* best_fit = NULL;
3195 bfd_vma best_fit_len = 0;
3196 bfd_size_type low, high, mid, first;
3197 struct arange *arange;
3198
3199 if (number_of_functions == 0)
3200 return false;
3201
3202 if (!build_lookup_funcinfo_table (unit))
3203 return false;
3204
3205 if (unit->lookup_funcinfo_table[number_of_functions - 1].high_addr < addr)
3206 return false;
3207
3208 /* Find the first function in the lookup table which may contain the
3209 specified address. */
3210 low = 0;
3211 high = number_of_functions;
3212 first = high;
3213 while (low < high)
3214 {
3215 mid = (low + high) / 2;
3216 lookup_funcinfo = &unit->lookup_funcinfo_table[mid];
3217 if (addr < lookup_funcinfo->low_addr)
3218 high = mid;
3219 else if (addr >= lookup_funcinfo->high_addr)
3220 low = mid + 1;
3221 else
3222 high = first = mid;
3223 }
3224
3225 /* Find the 'best' match for the address. The prior algorithm defined the
3226 best match as the function with the smallest address range containing
3227 the specified address. This definition should probably be changed to the
3228 innermost inline routine containing the address, but right now we want
3229 to get the same results we did before. */
3230 while (first < number_of_functions)
3231 {
3232 if (addr < unit->lookup_funcinfo_table[first].low_addr)
3233 break;
3234 funcinfo = unit->lookup_funcinfo_table[first].funcinfo;
3235
3236 for (arange = &funcinfo->arange; arange; arange = arange->next)
3237 {
3238 if (addr < arange->low || addr >= arange->high)
3239 continue;
3240
3241 if (!best_fit
3242 || arange->high - arange->low < best_fit_len
3243 /* The following comparison is designed to return the same
3244 match as the previous algorithm for routines which have the
3245 same best fit length. */
3246 || (arange->high - arange->low == best_fit_len
3247 && funcinfo > best_fit))
3248 {
3249 best_fit = funcinfo;
3250 best_fit_len = arange->high - arange->low;
3251 }
3252 }
3253
3254 first++;
3255 }
3256
3257 if (!best_fit)
3258 return false;
3259
3260 *function_ptr = best_fit;
3261 return true;
3262 }
3263
3264 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
3265 and LINENUMBER_PTR, and return TRUE. */
3266
3267 static bool
3268 lookup_symbol_in_function_table (struct comp_unit *unit,
3269 asymbol *sym,
3270 bfd_vma addr,
3271 const char **filename_ptr,
3272 unsigned int *linenumber_ptr)
3273 {
3274 struct funcinfo* each_func;
3275 struct funcinfo* best_fit = NULL;
3276 bfd_vma best_fit_len = 0;
3277 struct arange *arange;
3278 const char *name = bfd_asymbol_name (sym);
3279 asection *sec = bfd_asymbol_section (sym);
3280
3281 for (each_func = unit->function_table;
3282 each_func;
3283 each_func = each_func->prev_func)
3284 {
3285 for (arange = &each_func->arange;
3286 arange;
3287 arange = arange->next)
3288 {
3289 if ((!each_func->sec || each_func->sec == sec)
3290 && addr >= arange->low
3291 && addr < arange->high
3292 && each_func->name
3293 && strcmp (name, each_func->name) == 0
3294 && (!best_fit
3295 || arange->high - arange->low < best_fit_len))
3296 {
3297 best_fit = each_func;
3298 best_fit_len = arange->high - arange->low;
3299 }
3300 }
3301 }
3302
3303 if (best_fit)
3304 {
3305 best_fit->sec = sec;
3306 *filename_ptr = best_fit->file;
3307 *linenumber_ptr = best_fit->line;
3308 return true;
3309 }
3310 else
3311 return false;
3312 }
3313
3314 /* Variable table functions. */
3315
3316 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
3317 LINENUMBER_PTR, and return TRUE. */
3318
3319 static bool
3320 lookup_symbol_in_variable_table (struct comp_unit *unit,
3321 asymbol *sym,
3322 bfd_vma addr,
3323 const char **filename_ptr,
3324 unsigned int *linenumber_ptr)
3325 {
3326 const char *name = bfd_asymbol_name (sym);
3327 asection *sec = bfd_asymbol_section (sym);
3328 struct varinfo* each;
3329
3330 for (each = unit->variable_table; each; each = each->prev_var)
3331 if (! each->stack
3332 && each->file != NULL
3333 && each->name != NULL
3334 && each->addr == addr
3335 && (!each->sec || each->sec == sec)
3336 && strcmp (name, each->name) == 0)
3337 break;
3338
3339 if (each)
3340 {
3341 each->sec = sec;
3342 *filename_ptr = each->file;
3343 *linenumber_ptr = each->line;
3344 return true;
3345 }
3346
3347 return false;
3348 }
3349
3350 static struct comp_unit *stash_comp_unit (struct dwarf2_debug *,
3351 struct dwarf2_debug_file *);
3352 static bool comp_unit_maybe_decode_line_info (struct comp_unit *);
3353
3354 static bool
3355 find_abstract_instance (struct comp_unit *unit,
3356 struct attribute *attr_ptr,
3357 unsigned int recur_count,
3358 const char **pname,
3359 bool *is_linkage,
3360 char **filename_ptr,
3361 int *linenumber_ptr)
3362 {
3363 bfd *abfd = unit->abfd;
3364 bfd_byte *info_ptr = NULL;
3365 bfd_byte *info_ptr_end;
3366 unsigned int abbrev_number, i;
3367 struct abbrev_info *abbrev;
3368 uint64_t die_ref = attr_ptr->u.val;
3369 struct attribute attr;
3370 const char *name = NULL;
3371
3372 if (recur_count == 100)
3373 {
3374 _bfd_error_handler
3375 (_("DWARF error: abstract instance recursion detected"));
3376 bfd_set_error (bfd_error_bad_value);
3377 return false;
3378 }
3379
3380 /* DW_FORM_ref_addr can reference an entry in a different CU. It
3381 is an offset from the .debug_info section, not the current CU. */
3382 if (attr_ptr->form == DW_FORM_ref_addr)
3383 {
3384 /* We only support DW_FORM_ref_addr within the same file, so
3385 any relocations should be resolved already. Check this by
3386 testing for a zero die_ref; There can't be a valid reference
3387 to the header of a .debug_info section.
3388 DW_FORM_ref_addr is an offset relative to .debug_info.
3389 Normally when using the GNU linker this is accomplished by
3390 emitting a symbolic reference to a label, because .debug_info
3391 sections are linked at zero. When there are multiple section
3392 groups containing .debug_info, as there might be in a
3393 relocatable object file, it would be reasonable to assume that
3394 a symbolic reference to a label in any .debug_info section
3395 might be used. Since we lay out multiple .debug_info
3396 sections at non-zero VMAs (see place_sections), and read
3397 them contiguously into dwarf_info_buffer, that means the
3398 reference is relative to dwarf_info_buffer. */
3399 size_t total;
3400
3401 info_ptr = unit->file->dwarf_info_buffer;
3402 info_ptr_end = info_ptr + unit->file->dwarf_info_size;
3403 total = info_ptr_end - info_ptr;
3404 if (!die_ref)
3405 return true;
3406 else if (die_ref >= total)
3407 {
3408 _bfd_error_handler
3409 (_("DWARF error: invalid abstract instance DIE ref"));
3410 bfd_set_error (bfd_error_bad_value);
3411 return false;
3412 }
3413 info_ptr += die_ref;
3414 }
3415 else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
3416 {
3417 bool first_time = unit->stash->alt.dwarf_info_buffer == NULL;
3418
3419 info_ptr = read_alt_indirect_ref (unit, die_ref);
3420 if (first_time)
3421 unit->stash->alt.info_ptr = unit->stash->alt.dwarf_info_buffer;
3422 if (info_ptr == NULL)
3423 {
3424 _bfd_error_handler
3425 (_("DWARF error: unable to read alt ref %" PRIu64),
3426 (uint64_t) die_ref);
3427 bfd_set_error (bfd_error_bad_value);
3428 return false;
3429 }
3430 info_ptr_end = (unit->stash->alt.dwarf_info_buffer
3431 + unit->stash->alt.dwarf_info_size);
3432 if (unit->stash->alt.all_comp_units)
3433 unit = unit->stash->alt.all_comp_units;
3434 }
3435
3436 if (attr_ptr->form == DW_FORM_ref_addr
3437 || attr_ptr->form == DW_FORM_GNU_ref_alt)
3438 {
3439 /* Now find the CU containing this pointer. */
3440 if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr)
3441 info_ptr_end = unit->end_ptr;
3442 else
3443 {
3444 /* Check other CUs to see if they contain the abbrev. */
3445 struct comp_unit *u;
3446
3447 for (u = unit->prev_unit; u != NULL; u = u->prev_unit)
3448 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3449 break;
3450
3451 if (u == NULL)
3452 for (u = unit->next_unit; u != NULL; u = u->next_unit)
3453 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3454 break;
3455
3456 if (attr_ptr->form == DW_FORM_ref_addr)
3457 while (u == NULL)
3458 {
3459 u = stash_comp_unit (unit->stash, &unit->stash->f);
3460 if (u == NULL)
3461 break;
3462 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3463 break;
3464 u = NULL;
3465 }
3466
3467 if (attr_ptr->form == DW_FORM_GNU_ref_alt)
3468 while (u == NULL)
3469 {
3470 u = stash_comp_unit (unit->stash, &unit->stash->alt);
3471 if (u == NULL)
3472 break;
3473 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3474 break;
3475 u = NULL;
3476 }
3477
3478 if (u == NULL)
3479 {
3480 _bfd_error_handler
3481 (_("DWARF error: unable to locate abstract instance DIE ref %"
3482 PRIu64), (uint64_t) die_ref);
3483 bfd_set_error (bfd_error_bad_value);
3484 return false;
3485 }
3486 unit = u;
3487 info_ptr_end = unit->end_ptr;
3488 }
3489 }
3490 else
3491 {
3492 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
3493 DW_FORM_ref_udata. These are all references relative to the
3494 start of the current CU. */
3495 size_t total;
3496
3497 info_ptr = unit->info_ptr_unit;
3498 info_ptr_end = unit->end_ptr;
3499 total = info_ptr_end - info_ptr;
3500 if (!die_ref || die_ref >= total)
3501 {
3502 _bfd_error_handler
3503 (_("DWARF error: invalid abstract instance DIE ref"));
3504 bfd_set_error (bfd_error_bad_value);
3505 return false;
3506 }
3507 info_ptr += die_ref;
3508 }
3509
3510 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
3511 false, info_ptr_end);
3512 if (abbrev_number)
3513 {
3514 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3515 if (! abbrev)
3516 {
3517 _bfd_error_handler
3518 (_("DWARF error: could not find abbrev number %u"), abbrev_number);
3519 bfd_set_error (bfd_error_bad_value);
3520 return false;
3521 }
3522 else
3523 {
3524 for (i = 0; i < abbrev->num_attrs; ++i)
3525 {
3526 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
3527 info_ptr, info_ptr_end);
3528 if (info_ptr == NULL)
3529 break;
3530 switch (attr.name)
3531 {
3532 case DW_AT_name:
3533 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3534 over DW_AT_name. */
3535 if (name == NULL && is_str_form (&attr))
3536 {
3537 name = attr.u.str;
3538 if (non_mangled (unit->lang))
3539 *is_linkage = true;
3540 }
3541 break;
3542 case DW_AT_specification:
3543 if (is_int_form (&attr)
3544 && !find_abstract_instance (unit, &attr, recur_count + 1,
3545 &name, is_linkage,
3546 filename_ptr, linenumber_ptr))
3547 return false;
3548 break;
3549 case DW_AT_linkage_name:
3550 case DW_AT_MIPS_linkage_name:
3551 /* PR 16949: Corrupt debug info can place
3552 non-string forms into these attributes. */
3553 if (is_str_form (&attr))
3554 {
3555 name = attr.u.str;
3556 *is_linkage = true;
3557 }
3558 break;
3559 case DW_AT_decl_file:
3560 if (!comp_unit_maybe_decode_line_info (unit))
3561 return false;
3562 if (is_int_form (&attr))
3563 *filename_ptr = concat_filename (unit->line_table,
3564 attr.u.val);
3565 break;
3566 case DW_AT_decl_line:
3567 if (is_int_form (&attr))
3568 *linenumber_ptr = attr.u.val;
3569 break;
3570 default:
3571 break;
3572 }
3573 }
3574 }
3575 }
3576 *pname = name;
3577 return true;
3578 }
3579
3580 static bool
3581 read_ranges (struct comp_unit *unit, struct arange *arange,
3582 struct trie_node **trie_root, uint64_t offset)
3583 {
3584 bfd_byte *ranges_ptr;
3585 bfd_byte *ranges_end;
3586 bfd_vma base_address = unit->base_address;
3587
3588 if (! unit->file->dwarf_ranges_buffer)
3589 {
3590 if (! read_debug_ranges (unit))
3591 return false;
3592 }
3593
3594 if (offset > unit->file->dwarf_ranges_size)
3595 return false;
3596 ranges_ptr = unit->file->dwarf_ranges_buffer + offset;
3597 ranges_end = unit->file->dwarf_ranges_buffer + unit->file->dwarf_ranges_size;
3598
3599 for (;;)
3600 {
3601 bfd_vma low_pc;
3602 bfd_vma high_pc;
3603
3604 /* PR 17512: file: 62cada7d. */
3605 if (2u * unit->addr_size > (size_t) (ranges_end - ranges_ptr))
3606 return false;
3607
3608 low_pc = read_address (unit, &ranges_ptr, ranges_end);
3609 high_pc = read_address (unit, &ranges_ptr, ranges_end);
3610
3611 if (low_pc == 0 && high_pc == 0)
3612 break;
3613 if (low_pc == -1UL && high_pc != -1UL)
3614 base_address = high_pc;
3615 else
3616 {
3617 if (!arange_add (unit, arange, trie_root,
3618 base_address + low_pc, base_address + high_pc))
3619 return false;
3620 }
3621 }
3622 return true;
3623 }
3624
3625 static bool
3626 read_rnglists (struct comp_unit *unit, struct arange *arange,
3627 struct trie_node **trie_root, uint64_t offset)
3628 {
3629 bfd_byte *rngs_ptr;
3630 bfd_byte *rngs_end;
3631 bfd_vma base_address = unit->base_address;
3632 bfd_vma low_pc;
3633 bfd_vma high_pc;
3634 bfd *abfd = unit->abfd;
3635
3636 if (! unit->file->dwarf_rnglists_buffer)
3637 {
3638 if (! read_debug_rnglists (unit))
3639 return false;
3640 }
3641
3642 rngs_ptr = unit->file->dwarf_rnglists_buffer + offset;
3643 if (rngs_ptr < unit->file->dwarf_rnglists_buffer)
3644 return false;
3645 rngs_end = unit->file->dwarf_rnglists_buffer;
3646 rngs_end += unit->file->dwarf_rnglists_size;
3647
3648 for (;;)
3649 {
3650 enum dwarf_range_list_entry rlet;
3651
3652 if (rngs_ptr >= rngs_end)
3653 return false;
3654
3655 rlet = read_1_byte (abfd, &rngs_ptr, rngs_end);
3656
3657 switch (rlet)
3658 {
3659 case DW_RLE_end_of_list:
3660 return true;
3661
3662 case DW_RLE_base_address:
3663 if (unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3664 return false;
3665 base_address = read_address (unit, &rngs_ptr, rngs_end);
3666 continue;
3667
3668 case DW_RLE_start_length:
3669 if (unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3670 return false;
3671 low_pc = read_address (unit, &rngs_ptr, rngs_end);
3672 high_pc = low_pc;
3673 high_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3674 false, rngs_end);
3675 break;
3676
3677 case DW_RLE_offset_pair:
3678 low_pc = base_address;
3679 low_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3680 false, rngs_end);
3681 high_pc = base_address;
3682 high_pc += _bfd_safe_read_leb128 (abfd, &rngs_ptr,
3683 false, rngs_end);
3684 break;
3685
3686 case DW_RLE_start_end:
3687 if (2u * unit->addr_size > (size_t) (rngs_end - rngs_ptr))
3688 return false;
3689 low_pc = read_address (unit, &rngs_ptr, rngs_end);
3690 high_pc = read_address (unit, &rngs_ptr, rngs_end);
3691 break;
3692
3693 /* TODO x-variants need .debug_addr support used for split-dwarf. */
3694 case DW_RLE_base_addressx:
3695 case DW_RLE_startx_endx:
3696 case DW_RLE_startx_length:
3697 default:
3698 return false;
3699 }
3700
3701 if (!arange_add (unit, arange, trie_root, low_pc, high_pc))
3702 return false;
3703 }
3704 }
3705
3706 static bool
3707 read_rangelist (struct comp_unit *unit, struct arange *arange,
3708 struct trie_node **trie_root, uint64_t offset)
3709 {
3710 if (unit->version <= 4)
3711 return read_ranges (unit, arange, trie_root, offset);
3712 else
3713 return read_rnglists (unit, arange, trie_root, offset);
3714 }
3715
3716 static struct funcinfo *
3717 lookup_func_by_offset (uint64_t offset, struct funcinfo * table)
3718 {
3719 for (; table != NULL; table = table->prev_func)
3720 if (table->unit_offset == offset)
3721 return table;
3722 return NULL;
3723 }
3724
3725 static struct varinfo *
3726 lookup_var_by_offset (uint64_t offset, struct varinfo * table)
3727 {
3728 while (table)
3729 {
3730 if (table->unit_offset == offset)
3731 return table;
3732 table = table->prev_var;
3733 }
3734
3735 return NULL;
3736 }
3737
3738
3739 /* DWARF2 Compilation unit functions. */
3740
3741 static struct funcinfo *
3742 reverse_funcinfo_list (struct funcinfo *head)
3743 {
3744 struct funcinfo *rhead;
3745 struct funcinfo *temp;
3746
3747 for (rhead = NULL; head; head = temp)
3748 {
3749 temp = head->prev_func;
3750 head->prev_func = rhead;
3751 rhead = head;
3752 }
3753 return rhead;
3754 }
3755
3756 static struct varinfo *
3757 reverse_varinfo_list (struct varinfo *head)
3758 {
3759 struct varinfo *rhead;
3760 struct varinfo *temp;
3761
3762 for (rhead = NULL; head; head = temp)
3763 {
3764 temp = head->prev_var;
3765 head->prev_var = rhead;
3766 rhead = head;
3767 }
3768 return rhead;
3769 }
3770
3771 /* Scan over each die in a comp. unit looking for functions to add
3772 to the function table and variables to the variable table. */
3773
3774 static bool
3775 scan_unit_for_symbols (struct comp_unit *unit)
3776 {
3777 bfd *abfd = unit->abfd;
3778 bfd_byte *info_ptr = unit->first_child_die_ptr;
3779 bfd_byte *info_ptr_end = unit->end_ptr;
3780 int nesting_level = 0;
3781 struct nest_funcinfo
3782 {
3783 struct funcinfo *func;
3784 } *nested_funcs;
3785 int nested_funcs_size;
3786 struct funcinfo *last_func;
3787 struct varinfo *last_var;
3788
3789 /* Maintain a stack of in-scope functions and inlined functions, which we
3790 can use to set the caller_func field. */
3791 nested_funcs_size = 32;
3792 nested_funcs = (struct nest_funcinfo *)
3793 bfd_malloc (nested_funcs_size * sizeof (*nested_funcs));
3794 if (nested_funcs == NULL)
3795 return false;
3796 nested_funcs[nesting_level].func = 0;
3797
3798 /* PR 27484: We must scan the DIEs twice. The first time we look for
3799 function and variable tags and accumulate them into their respective
3800 tables. The second time through we process the attributes of the
3801 functions/variables and augment the table entries. */
3802 while (nesting_level >= 0)
3803 {
3804 unsigned int abbrev_number, i;
3805 struct abbrev_info *abbrev;
3806 struct funcinfo *func;
3807 struct varinfo *var;
3808 uint64_t current_offset;
3809
3810 /* PR 17512: file: 9f405d9d. */
3811 if (info_ptr >= info_ptr_end)
3812 goto fail;
3813
3814 current_offset = info_ptr - unit->info_ptr_unit;
3815 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
3816 false, info_ptr_end);
3817 if (abbrev_number == 0)
3818 {
3819 nesting_level--;
3820 continue;
3821 }
3822
3823 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3824 if (! abbrev)
3825 {
3826 static unsigned int previous_failed_abbrev = -1U;
3827
3828 /* Avoid multiple reports of the same missing abbrev. */
3829 if (abbrev_number != previous_failed_abbrev)
3830 {
3831 _bfd_error_handler
3832 (_("DWARF error: could not find abbrev number %u"),
3833 abbrev_number);
3834 previous_failed_abbrev = abbrev_number;
3835 }
3836 bfd_set_error (bfd_error_bad_value);
3837 goto fail;
3838 }
3839
3840 if (abbrev->tag == DW_TAG_subprogram
3841 || abbrev->tag == DW_TAG_entry_point
3842 || abbrev->tag == DW_TAG_inlined_subroutine)
3843 {
3844 size_t amt = sizeof (struct funcinfo);
3845
3846 var = NULL;
3847 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
3848 if (func == NULL)
3849 goto fail;
3850 func->tag = abbrev->tag;
3851 func->prev_func = unit->function_table;
3852 func->unit_offset = current_offset;
3853 unit->function_table = func;
3854 unit->number_of_functions++;
3855 BFD_ASSERT (!unit->cached);
3856
3857 if (func->tag == DW_TAG_inlined_subroutine)
3858 for (i = nesting_level; i-- != 0; )
3859 if (nested_funcs[i].func)
3860 {
3861 func->caller_func = nested_funcs[i].func;
3862 break;
3863 }
3864 nested_funcs[nesting_level].func = func;
3865 }
3866 else
3867 {
3868 func = NULL;
3869 if (abbrev->tag == DW_TAG_variable
3870 || abbrev->tag == DW_TAG_member)
3871 {
3872 size_t amt = sizeof (struct varinfo);
3873
3874 var = (struct varinfo *) bfd_zalloc (abfd, amt);
3875 if (var == NULL)
3876 goto fail;
3877 var->tag = abbrev->tag;
3878 var->stack = true;
3879 var->prev_var = unit->variable_table;
3880 unit->variable_table = var;
3881 var->unit_offset = current_offset;
3882 /* PR 18205: Missing debug information can cause this
3883 var to be attached to an already cached unit. */
3884 }
3885 else
3886 var = NULL;
3887
3888 /* No inline function in scope at this nesting level. */
3889 nested_funcs[nesting_level].func = 0;
3890 }
3891
3892 for (i = 0; i < abbrev->num_attrs; ++i)
3893 {
3894 struct attribute attr;
3895
3896 info_ptr = read_attribute (&attr, &abbrev->attrs[i],
3897 unit, info_ptr, info_ptr_end);
3898 if (info_ptr == NULL)
3899 goto fail;
3900 }
3901
3902 if (abbrev->has_children)
3903 {
3904 nesting_level++;
3905
3906 if (nesting_level >= nested_funcs_size)
3907 {
3908 struct nest_funcinfo *tmp;
3909
3910 nested_funcs_size *= 2;
3911 tmp = (struct nest_funcinfo *)
3912 bfd_realloc (nested_funcs,
3913 nested_funcs_size * sizeof (*nested_funcs));
3914 if (tmp == NULL)
3915 goto fail;
3916 nested_funcs = tmp;
3917 }
3918 nested_funcs[nesting_level].func = 0;
3919 }
3920 }
3921
3922 unit->function_table = reverse_funcinfo_list (unit->function_table);
3923 unit->variable_table = reverse_varinfo_list (unit->variable_table);
3924
3925 /* This is the second pass over the abbrevs. */
3926 info_ptr = unit->first_child_die_ptr;
3927 nesting_level = 0;
3928
3929 last_func = NULL;
3930 last_var = NULL;
3931
3932 while (nesting_level >= 0)
3933 {
3934 unsigned int abbrev_number, i;
3935 struct abbrev_info *abbrev;
3936 struct attribute attr;
3937 struct funcinfo *func;
3938 struct varinfo *var;
3939 bfd_vma low_pc = 0;
3940 bfd_vma high_pc = 0;
3941 bool high_pc_relative = false;
3942 uint64_t current_offset;
3943
3944 /* PR 17512: file: 9f405d9d. */
3945 if (info_ptr >= info_ptr_end)
3946 goto fail;
3947
3948 current_offset = info_ptr - unit->info_ptr_unit;
3949 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
3950 false, info_ptr_end);
3951 if (! abbrev_number)
3952 {
3953 nesting_level--;
3954 continue;
3955 }
3956
3957 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3958 /* This should have been handled above. */
3959 BFD_ASSERT (abbrev != NULL);
3960
3961 func = NULL;
3962 var = NULL;
3963 if (abbrev->tag == DW_TAG_subprogram
3964 || abbrev->tag == DW_TAG_entry_point
3965 || abbrev->tag == DW_TAG_inlined_subroutine)
3966 {
3967 if (last_func
3968 && last_func->prev_func
3969 && last_func->prev_func->unit_offset == current_offset)
3970 func = last_func->prev_func;
3971 else
3972 func = lookup_func_by_offset (current_offset, unit->function_table);
3973
3974 if (func == NULL)
3975 goto fail;
3976
3977 last_func = func;
3978 }
3979 else if (abbrev->tag == DW_TAG_variable
3980 || abbrev->tag == DW_TAG_member)
3981 {
3982 if (last_var
3983 && last_var->prev_var
3984 && last_var->prev_var->unit_offset == current_offset)
3985 var = last_var->prev_var;
3986 else
3987 var = lookup_var_by_offset (current_offset, unit->variable_table);
3988
3989 if (var == NULL)
3990 goto fail;
3991
3992 last_var = var;
3993 }
3994
3995 for (i = 0; i < abbrev->num_attrs; ++i)
3996 {
3997 info_ptr = read_attribute (&attr, &abbrev->attrs[i],
3998 unit, info_ptr, info_ptr_end);
3999 if (info_ptr == NULL)
4000 goto fail;
4001
4002 if (func)
4003 {
4004 switch (attr.name)
4005 {
4006 case DW_AT_call_file:
4007 if (is_int_form (&attr))
4008 func->caller_file = concat_filename (unit->line_table,
4009 attr.u.val);
4010 break;
4011
4012 case DW_AT_call_line:
4013 if (is_int_form (&attr))
4014 func->caller_line = attr.u.val;
4015 break;
4016
4017 case DW_AT_abstract_origin:
4018 case DW_AT_specification:
4019 if (is_int_form (&attr)
4020 && !find_abstract_instance (unit, &attr, 0,
4021 &func->name,
4022 &func->is_linkage,
4023 &func->file,
4024 &func->line))
4025 goto fail;
4026 break;
4027
4028 case DW_AT_name:
4029 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
4030 over DW_AT_name. */
4031 if (func->name == NULL && is_str_form (&attr))
4032 {
4033 func->name = attr.u.str;
4034 if (non_mangled (unit->lang))
4035 func->is_linkage = true;
4036 }
4037 break;
4038
4039 case DW_AT_linkage_name:
4040 case DW_AT_MIPS_linkage_name:
4041 /* PR 16949: Corrupt debug info can place
4042 non-string forms into these attributes. */
4043 if (is_str_form (&attr))
4044 {
4045 func->name = attr.u.str;
4046 func->is_linkage = true;
4047 }
4048 break;
4049
4050 case DW_AT_low_pc:
4051 if (is_int_form (&attr))
4052 low_pc = attr.u.val;
4053 break;
4054
4055 case DW_AT_high_pc:
4056 if (is_int_form (&attr))
4057 {
4058 high_pc = attr.u.val;
4059 high_pc_relative = attr.form != DW_FORM_addr;
4060 }
4061 break;
4062
4063 case DW_AT_ranges:
4064 if (is_int_form (&attr)
4065 && !read_rangelist (unit, &func->arange,
4066 &unit->file->trie_root, attr.u.val))
4067 goto fail;
4068 break;
4069
4070 case DW_AT_decl_file:
4071 if (is_int_form (&attr))
4072 func->file = concat_filename (unit->line_table,
4073 attr.u.val);
4074 break;
4075
4076 case DW_AT_decl_line:
4077 if (is_int_form (&attr))
4078 func->line = attr.u.val;
4079 break;
4080
4081 default:
4082 break;
4083 }
4084 }
4085 else if (var)
4086 {
4087 switch (attr.name)
4088 {
4089 case DW_AT_specification:
4090 if (is_int_form (&attr) && attr.u.val)
4091 {
4092 struct varinfo * spec_var;
4093
4094 spec_var = lookup_var_by_offset (attr.u.val,
4095 unit->variable_table);
4096 if (spec_var == NULL)
4097 {
4098 _bfd_error_handler (_("DWARF error: could not find "
4099 "variable specification "
4100 "at offset 0x%lx"),
4101 (unsigned long) attr.u.val);
4102 break;
4103 }
4104
4105 if (var->name == NULL)
4106 var->name = spec_var->name;
4107 if (var->file == NULL && spec_var->file != NULL)
4108 var->file = strdup (spec_var->file);
4109 if (var->line == 0)
4110 var->line = spec_var->line;
4111 if (var->sec == NULL)
4112 var->sec = spec_var->sec;
4113 }
4114 break;
4115
4116 case DW_AT_name:
4117 if (is_str_form (&attr))
4118 var->name = attr.u.str;
4119 break;
4120
4121 case DW_AT_decl_file:
4122 if (is_int_form (&attr))
4123 var->file = concat_filename (unit->line_table,
4124 attr.u.val);
4125 break;
4126
4127 case DW_AT_decl_line:
4128 if (is_int_form (&attr))
4129 var->line = attr.u.val;
4130 break;
4131
4132 case DW_AT_external:
4133 if (is_int_form (&attr) && attr.u.val != 0)
4134 var->stack = false;
4135 break;
4136
4137 case DW_AT_location:
4138 switch (attr.form)
4139 {
4140 case DW_FORM_block:
4141 case DW_FORM_block1:
4142 case DW_FORM_block2:
4143 case DW_FORM_block4:
4144 case DW_FORM_exprloc:
4145 if (attr.u.blk->data != NULL
4146 && *attr.u.blk->data == DW_OP_addr)
4147 {
4148 var->stack = false;
4149
4150 /* Verify that DW_OP_addr is the only opcode in the
4151 location, in which case the block size will be 1
4152 plus the address size. */
4153 /* ??? For TLS variables, gcc can emit
4154 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
4155 which we don't handle here yet. */
4156 if (attr.u.blk->size == unit->addr_size + 1U)
4157 var->addr = bfd_get (unit->addr_size * 8,
4158 unit->abfd,
4159 attr.u.blk->data + 1);
4160 }
4161 break;
4162
4163 default:
4164 break;
4165 }
4166 break;
4167
4168 default:
4169 break;
4170 }
4171 }
4172 }
4173
4174 if (abbrev->has_children)
4175 nesting_level++;
4176
4177 if (high_pc_relative)
4178 high_pc += low_pc;
4179
4180 if (func && high_pc != 0)
4181 {
4182 if (!arange_add (unit, &func->arange, &unit->file->trie_root,
4183 low_pc, high_pc))
4184 goto fail;
4185 }
4186 }
4187
4188 unit->function_table = reverse_funcinfo_list (unit->function_table);
4189 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4190
4191 free (nested_funcs);
4192 return true;
4193
4194 fail:
4195 free (nested_funcs);
4196 return false;
4197 }
4198
4199 /* Read the attributes of the form strx and addrx. */
4200
4201 static void
4202 reread_attribute (struct comp_unit *unit,
4203 struct attribute *attr,
4204 bfd_vma *low_pc,
4205 bfd_vma *high_pc,
4206 bool *high_pc_relative,
4207 bool compunit)
4208 {
4209 if (is_strx_form (attr->form))
4210 attr->u.str = (char *) read_indexed_string (attr->u.val, unit);
4211 if (is_addrx_form (attr->form))
4212 attr->u.val = read_indexed_address (attr->u.val, unit);
4213
4214 switch (attr->name)
4215 {
4216 case DW_AT_stmt_list:
4217 unit->stmtlist = 1;
4218 unit->line_offset = attr->u.val;
4219 break;
4220
4221 case DW_AT_name:
4222 if (is_str_form (attr))
4223 unit->name = attr->u.str;
4224 break;
4225
4226 case DW_AT_low_pc:
4227 *low_pc = attr->u.val;
4228 if (compunit)
4229 unit->base_address = *low_pc;
4230 break;
4231
4232 case DW_AT_high_pc:
4233 *high_pc = attr->u.val;
4234 *high_pc_relative = attr->form != DW_FORM_addr;
4235 break;
4236
4237 case DW_AT_ranges:
4238 if (!read_rangelist (unit, &unit->arange,
4239 &unit->file->trie_root, attr->u.val))
4240 return;
4241 break;
4242
4243 case DW_AT_comp_dir:
4244 {
4245 char *comp_dir = attr->u.str;
4246
4247 if (!is_str_form (attr))
4248 {
4249 _bfd_error_handler
4250 (_("DWARF error: DW_AT_comp_dir attribute encountered "
4251 "with a non-string form"));
4252 comp_dir = NULL;
4253 }
4254
4255 if (comp_dir)
4256 {
4257 char *cp = strchr (comp_dir, ':');
4258
4259 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
4260 comp_dir = cp + 1;
4261 }
4262 unit->comp_dir = comp_dir;
4263 break;
4264 }
4265
4266 case DW_AT_language:
4267 unit->lang = attr->u.val;
4268 default:
4269 break;
4270 }
4271 }
4272
4273 /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
4274 includes the compilation unit header that proceeds the DIE's, but
4275 does not include the length field that precedes each compilation
4276 unit header. END_PTR points one past the end of this comp unit.
4277 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
4278
4279 This routine does not read the whole compilation unit; only enough
4280 to get to the line number information for the compilation unit. */
4281
4282 static struct comp_unit *
4283 parse_comp_unit (struct dwarf2_debug *stash,
4284 struct dwarf2_debug_file *file,
4285 bfd_byte *info_ptr,
4286 bfd_vma unit_length,
4287 bfd_byte *info_ptr_unit,
4288 unsigned int offset_size)
4289 {
4290 struct comp_unit* unit;
4291 unsigned int version;
4292 uint64_t abbrev_offset = 0;
4293 /* Initialize it just to avoid a GCC false warning. */
4294 unsigned int addr_size = -1;
4295 struct abbrev_info** abbrevs;
4296 unsigned int abbrev_number, i;
4297 struct abbrev_info *abbrev;
4298 struct attribute attr;
4299 bfd_byte *end_ptr = info_ptr + unit_length;
4300 size_t amt;
4301 bfd_vma low_pc = 0;
4302 bfd_vma high_pc = 0;
4303 bfd *abfd = file->bfd_ptr;
4304 bool high_pc_relative = false;
4305 enum dwarf_unit_type unit_type;
4306 struct attribute *str_addrp = NULL;
4307 size_t str_count = 0;
4308 size_t str_alloc = 0;
4309 bool compunit_flag = false;
4310
4311 version = read_2_bytes (abfd, &info_ptr, end_ptr);
4312 if (version < 2 || version > 5)
4313 {
4314 /* PR 19872: A version number of 0 probably means that there is padding
4315 at the end of the .debug_info section. Gold puts it there when
4316 performing an incremental link, for example. So do not generate
4317 an error, just return a NULL. */
4318 if (version)
4319 {
4320 _bfd_error_handler
4321 (_("DWARF error: found dwarf version '%u', this reader"
4322 " only handles version 2, 3, 4 and 5 information"), version);
4323 bfd_set_error (bfd_error_bad_value);
4324 }
4325 return NULL;
4326 }
4327
4328 if (version < 5)
4329 unit_type = DW_UT_compile;
4330 else
4331 {
4332 unit_type = read_1_byte (abfd, &info_ptr, end_ptr);
4333 addr_size = read_1_byte (abfd, &info_ptr, end_ptr);
4334 }
4335
4336 BFD_ASSERT (offset_size == 4 || offset_size == 8);
4337 if (offset_size == 4)
4338 abbrev_offset = read_4_bytes (abfd, &info_ptr, end_ptr);
4339 else
4340 abbrev_offset = read_8_bytes (abfd, &info_ptr, end_ptr);
4341
4342 if (version < 5)
4343 addr_size = read_1_byte (abfd, &info_ptr, end_ptr);
4344
4345 if (unit_type == DW_UT_type)
4346 {
4347 /* Skip type signature. */
4348 info_ptr += 8;
4349
4350 /* Skip type offset. */
4351 info_ptr += offset_size;
4352 }
4353
4354 if (addr_size > sizeof (bfd_vma))
4355 {
4356 _bfd_error_handler
4357 /* xgettext: c-format */
4358 (_("DWARF error: found address size '%u', this reader"
4359 " can not handle sizes greater than '%u'"),
4360 addr_size,
4361 (unsigned int) sizeof (bfd_vma));
4362 bfd_set_error (bfd_error_bad_value);
4363 return NULL;
4364 }
4365
4366 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
4367 {
4368 _bfd_error_handler
4369 ("DWARF error: found address size '%u', this reader"
4370 " can only handle address sizes '2', '4' and '8'", addr_size);
4371 bfd_set_error (bfd_error_bad_value);
4372 return NULL;
4373 }
4374
4375 /* Read the abbrevs for this compilation unit into a table. */
4376 abbrevs = read_abbrevs (abfd, abbrev_offset, stash, file);
4377 if (! abbrevs)
4378 return NULL;
4379
4380 abbrev_number = _bfd_safe_read_leb128 (abfd, &info_ptr,
4381 false, end_ptr);
4382 if (! abbrev_number)
4383 {
4384 /* PR 19872: An abbrev number of 0 probably means that there is padding
4385 at the end of the .debug_abbrev section. Gold puts it there when
4386 performing an incremental link, for example. So do not generate
4387 an error, just return a NULL. */
4388 return NULL;
4389 }
4390
4391 abbrev = lookup_abbrev (abbrev_number, abbrevs);
4392 if (! abbrev)
4393 {
4394 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
4395 abbrev_number);
4396 bfd_set_error (bfd_error_bad_value);
4397 return NULL;
4398 }
4399
4400 amt = sizeof (struct comp_unit);
4401 unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
4402 if (unit == NULL)
4403 return NULL;
4404 unit->abfd = abfd;
4405 unit->version = version;
4406 unit->addr_size = addr_size;
4407 unit->offset_size = offset_size;
4408 unit->abbrevs = abbrevs;
4409 unit->end_ptr = end_ptr;
4410 unit->stash = stash;
4411 unit->file = file;
4412 unit->info_ptr_unit = info_ptr_unit;
4413
4414 if (abbrev->tag == DW_TAG_compile_unit)
4415 compunit_flag = true;
4416
4417 for (i = 0; i < abbrev->num_attrs; ++i)
4418 {
4419 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, end_ptr);
4420 if (info_ptr == NULL)
4421 goto err_exit;
4422
4423 /* Identify attributes of the form strx* and addrx* which come before
4424 DW_AT_str_offsets_base and DW_AT_addr_base respectively in the CU.
4425 Store the attributes in an array and process them later. */
4426 if ((unit->dwarf_str_offset == 0 && is_strx_form (attr.form))
4427 || (unit->dwarf_addr_offset == 0 && is_addrx_form (attr.form)))
4428 {
4429 if (str_count <= str_alloc)
4430 {
4431 str_alloc = 2 * str_alloc + 200;
4432 str_addrp = bfd_realloc (str_addrp,
4433 str_alloc * sizeof (*str_addrp));
4434 if (str_addrp == NULL)
4435 goto err_exit;
4436 }
4437 str_addrp[str_count] = attr;
4438 str_count++;
4439 continue;
4440 }
4441
4442 /* Store the data if it is of an attribute we want to keep in a
4443 partial symbol table. */
4444 switch (attr.name)
4445 {
4446 case DW_AT_stmt_list:
4447 if (is_int_form (&attr))
4448 {
4449 unit->stmtlist = 1;
4450 unit->line_offset = attr.u.val;
4451 }
4452 break;
4453
4454 case DW_AT_name:
4455 if (is_str_form (&attr))
4456 unit->name = attr.u.str;
4457 break;
4458
4459 case DW_AT_low_pc:
4460 if (is_int_form (&attr))
4461 {
4462 low_pc = attr.u.val;
4463 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
4464 this is the base address to use when reading location
4465 lists or range lists. */
4466 if (compunit_flag)
4467 unit->base_address = low_pc;
4468 }
4469 break;
4470
4471 case DW_AT_high_pc:
4472 if (is_int_form (&attr))
4473 {
4474 high_pc = attr.u.val;
4475 high_pc_relative = attr.form != DW_FORM_addr;
4476 }
4477 break;
4478
4479 case DW_AT_ranges:
4480 if (is_int_form (&attr)
4481 && !read_rangelist (unit, &unit->arange,
4482 &unit->file->trie_root, attr.u.val))
4483 goto err_exit;
4484 break;
4485
4486 case DW_AT_comp_dir:
4487 {
4488 char *comp_dir = attr.u.str;
4489
4490 /* PR 17512: file: 1fe726be. */
4491 if (!is_str_form (&attr))
4492 {
4493 _bfd_error_handler
4494 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
4495 comp_dir = NULL;
4496 }
4497
4498 if (comp_dir)
4499 {
4500 /* Irix 6.2 native cc prepends <machine>.: to the compilation
4501 directory, get rid of it. */
4502 char *cp = strchr (comp_dir, ':');
4503
4504 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
4505 comp_dir = cp + 1;
4506 }
4507 unit->comp_dir = comp_dir;
4508 break;
4509 }
4510
4511 case DW_AT_language:
4512 if (is_int_form (&attr))
4513 unit->lang = attr.u.val;
4514 break;
4515
4516 case DW_AT_addr_base:
4517 unit->dwarf_addr_offset = attr.u.val;
4518 break;
4519
4520 case DW_AT_str_offsets_base:
4521 unit->dwarf_str_offset = attr.u.val;
4522 break;
4523
4524 default:
4525 break;
4526 }
4527 }
4528
4529 for (i = 0; i < str_count; ++i)
4530 reread_attribute (unit, &str_addrp[i], &low_pc, &high_pc,
4531 &high_pc_relative, compunit_flag);
4532
4533 if (high_pc_relative)
4534 high_pc += low_pc;
4535 if (high_pc != 0)
4536 {
4537 if (!arange_add (unit, &unit->arange, &unit->file->trie_root,
4538 low_pc, high_pc))
4539 goto err_exit;
4540 }
4541
4542 unit->first_child_die_ptr = info_ptr;
4543
4544 free (str_addrp);
4545 return unit;
4546
4547 err_exit:
4548 unit->error = 1;
4549 free (str_addrp);
4550 return NULL;
4551 }
4552
4553 /* Return TRUE if UNIT may contain the address given by ADDR. When
4554 there are functions written entirely with inline asm statements, the
4555 range info in the compilation unit header may not be correct. We
4556 need to consult the line info table to see if a compilation unit
4557 really contains the given address. */
4558
4559 static bool
4560 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
4561 {
4562 struct arange *arange;
4563
4564 if (unit->error)
4565 return false;
4566
4567 arange = &unit->arange;
4568 do
4569 {
4570 if (addr >= arange->low && addr < arange->high)
4571 return true;
4572 arange = arange->next;
4573 }
4574 while (arange);
4575
4576 return false;
4577 }
4578
4579 /* If UNIT contains ADDR, set the output parameters to the values for
4580 the line containing ADDR and return TRUE. Otherwise return FALSE.
4581 The output parameters, FILENAME_PTR, FUNCTION_PTR, and
4582 LINENUMBER_PTR, are pointers to the objects to be filled in. */
4583
4584 static bool
4585 comp_unit_find_nearest_line (struct comp_unit *unit,
4586 bfd_vma addr,
4587 const char **filename_ptr,
4588 struct funcinfo **function_ptr,
4589 unsigned int *linenumber_ptr,
4590 unsigned int *discriminator_ptr)
4591 {
4592 bool line_p, func_p;
4593
4594 if (!comp_unit_maybe_decode_line_info (unit))
4595 return false;
4596
4597 *function_ptr = NULL;
4598 func_p = lookup_address_in_function_table (unit, addr, function_ptr);
4599 if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine)
4600 unit->stash->inliner_chain = *function_ptr;
4601
4602 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
4603 filename_ptr,
4604 linenumber_ptr,
4605 discriminator_ptr);
4606 return line_p || func_p;
4607 }
4608
4609 /* Check to see if line info is already decoded in a comp_unit.
4610 If not, decode it. Returns TRUE if no errors were encountered;
4611 FALSE otherwise. */
4612
4613 static bool
4614 comp_unit_maybe_decode_line_info (struct comp_unit *unit)
4615 {
4616 if (unit->error)
4617 return false;
4618
4619 if (! unit->line_table)
4620 {
4621 if (! unit->stmtlist)
4622 {
4623 unit->error = 1;
4624 return false;
4625 }
4626
4627 unit->line_table = decode_line_info (unit);
4628
4629 if (! unit->line_table)
4630 {
4631 unit->error = 1;
4632 return false;
4633 }
4634
4635 if (unit->first_child_die_ptr < unit->end_ptr
4636 && ! scan_unit_for_symbols (unit))
4637 {
4638 unit->error = 1;
4639 return false;
4640 }
4641 }
4642
4643 return true;
4644 }
4645
4646 /* If UNIT contains SYM at ADDR, set the output parameters to the
4647 values for the line containing SYM. The output parameters,
4648 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
4649 filled in.
4650
4651 Return TRUE if UNIT contains SYM, and no errors were encountered;
4652 FALSE otherwise. */
4653
4654 static bool
4655 comp_unit_find_line (struct comp_unit *unit,
4656 asymbol *sym,
4657 bfd_vma addr,
4658 const char **filename_ptr,
4659 unsigned int *linenumber_ptr)
4660 {
4661 if (!comp_unit_maybe_decode_line_info (unit))
4662 return false;
4663
4664 if (sym->flags & BSF_FUNCTION)
4665 return lookup_symbol_in_function_table (unit, sym, addr,
4666 filename_ptr,
4667 linenumber_ptr);
4668
4669 return lookup_symbol_in_variable_table (unit, sym, addr,
4670 filename_ptr,
4671 linenumber_ptr);
4672 }
4673
4674 /* Extract all interesting funcinfos and varinfos of a compilation
4675 unit into hash tables for faster lookup. Returns TRUE if no
4676 errors were enountered; FALSE otherwise. */
4677
4678 static bool
4679 comp_unit_hash_info (struct dwarf2_debug *stash,
4680 struct comp_unit *unit,
4681 struct info_hash_table *funcinfo_hash_table,
4682 struct info_hash_table *varinfo_hash_table)
4683 {
4684 struct funcinfo* each_func;
4685 struct varinfo* each_var;
4686 bool okay = true;
4687
4688 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
4689
4690 if (!comp_unit_maybe_decode_line_info (unit))
4691 return false;
4692
4693 BFD_ASSERT (!unit->cached);
4694
4695 /* To preserve the original search order, we went to visit the function
4696 infos in the reversed order of the list. However, making the list
4697 bi-directional use quite a bit of extra memory. So we reverse
4698 the list first, traverse the list in the now reversed order and
4699 finally reverse the list again to get back the original order. */
4700 unit->function_table = reverse_funcinfo_list (unit->function_table);
4701 for (each_func = unit->function_table;
4702 each_func && okay;
4703 each_func = each_func->prev_func)
4704 {
4705 /* Skip nameless functions. */
4706 if (each_func->name)
4707 /* There is no need to copy name string into hash table as
4708 name string is either in the dwarf string buffer or
4709 info in the stash. */
4710 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
4711 (void*) each_func, false);
4712 }
4713 unit->function_table = reverse_funcinfo_list (unit->function_table);
4714 if (!okay)
4715 return false;
4716
4717 /* We do the same for variable infos. */
4718 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4719 for (each_var = unit->variable_table;
4720 each_var && okay;
4721 each_var = each_var->prev_var)
4722 {
4723 /* Skip stack vars and vars with no files or names. */
4724 if (! each_var->stack
4725 && each_var->file != NULL
4726 && each_var->name != NULL)
4727 /* There is no need to copy name string into hash table as
4728 name string is either in the dwarf string buffer or
4729 info in the stash. */
4730 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
4731 (void*) each_var, false);
4732 }
4733
4734 unit->variable_table = reverse_varinfo_list (unit->variable_table);
4735 unit->cached = true;
4736 return okay;
4737 }
4738
4739 /* Locate a section in a BFD containing debugging info. The search starts
4740 from the section after AFTER_SEC, or from the first section in the BFD if
4741 AFTER_SEC is NULL. The search works by examining the names of the
4742 sections. There are three permissiable names. The first two are given
4743 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
4744 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
4745 This is a variation on the .debug_info section which has a checksum
4746 describing the contents appended onto the name. This allows the linker to
4747 identify and discard duplicate debugging sections for different
4748 compilation units. */
4749 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
4750
4751 static asection *
4752 find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
4753 asection *after_sec)
4754 {
4755 asection *msec;
4756 const char *look;
4757
4758 if (after_sec == NULL)
4759 {
4760 look = debug_sections[debug_info].uncompressed_name;
4761 msec = bfd_get_section_by_name (abfd, look);
4762 if (msec != NULL)
4763 return msec;
4764
4765 look = debug_sections[debug_info].compressed_name;
4766 msec = bfd_get_section_by_name (abfd, look);
4767 if (msec != NULL)
4768 return msec;
4769
4770 for (msec = abfd->sections; msec != NULL; msec = msec->next)
4771 if (startswith (msec->name, GNU_LINKONCE_INFO))
4772 return msec;
4773
4774 return NULL;
4775 }
4776
4777 for (msec = after_sec->next; msec != NULL; msec = msec->next)
4778 {
4779 look = debug_sections[debug_info].uncompressed_name;
4780 if (strcmp (msec->name, look) == 0)
4781 return msec;
4782
4783 look = debug_sections[debug_info].compressed_name;
4784 if (look != NULL && strcmp (msec->name, look) == 0)
4785 return msec;
4786
4787 if (startswith (msec->name, GNU_LINKONCE_INFO))
4788 return msec;
4789 }
4790
4791 return NULL;
4792 }
4793
4794 /* Transfer VMAs from object file to separate debug file. */
4795
4796 static void
4797 set_debug_vma (bfd *orig_bfd, bfd *debug_bfd)
4798 {
4799 asection *s, *d;
4800
4801 for (s = orig_bfd->sections, d = debug_bfd->sections;
4802 s != NULL && d != NULL;
4803 s = s->next, d = d->next)
4804 {
4805 if ((d->flags & SEC_DEBUGGING) != 0)
4806 break;
4807 /* ??? Assumes 1-1 correspondence between sections in the
4808 two files. */
4809 if (strcmp (s->name, d->name) == 0)
4810 {
4811 d->output_section = s->output_section;
4812 d->output_offset = s->output_offset;
4813 d->vma = s->vma;
4814 }
4815 }
4816 }
4817
4818 /* If the dwarf2 info was found in a separate debug file, return the
4819 debug file section corresponding to the section in the original file
4820 and the debug file symbols. */
4821
4822 static void
4823 _bfd_dwarf2_stash_syms (struct dwarf2_debug *stash, bfd *abfd,
4824 asection **sec, asymbol ***syms)
4825 {
4826 if (stash->f.bfd_ptr != abfd)
4827 {
4828 asection *s, *d;
4829
4830 if (*sec == NULL)
4831 {
4832 *syms = stash->f.syms;
4833 return;
4834 }
4835
4836 for (s = abfd->sections, d = stash->f.bfd_ptr->sections;
4837 s != NULL && d != NULL;
4838 s = s->next, d = d->next)
4839 {
4840 if ((d->flags & SEC_DEBUGGING) != 0)
4841 break;
4842 if (s == *sec
4843 && strcmp (s->name, d->name) == 0)
4844 {
4845 *sec = d;
4846 *syms = stash->f.syms;
4847 break;
4848 }
4849 }
4850 }
4851 }
4852
4853 /* Unset vmas for adjusted sections in STASH. */
4854
4855 static void
4856 unset_sections (struct dwarf2_debug *stash)
4857 {
4858 int i;
4859 struct adjusted_section *p;
4860
4861 i = stash->adjusted_section_count;
4862 p = stash->adjusted_sections;
4863 for (; i > 0; i--, p++)
4864 p->section->vma = 0;
4865 }
4866
4867 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4868 relocatable object file. VMAs are normally all zero in relocatable
4869 object files, so if we want to distinguish locations in sections by
4870 address we need to set VMAs so the sections do not overlap. We
4871 also set VMA on .debug_info so that when we have multiple
4872 .debug_info sections (or the linkonce variant) they also do not
4873 overlap. The multiple .debug_info sections make up a single
4874 logical section. ??? We should probably do the same for other
4875 debug sections. */
4876
4877 static bool
4878 place_sections (bfd *orig_bfd, struct dwarf2_debug *stash)
4879 {
4880 bfd *abfd;
4881 struct adjusted_section *p;
4882 int i;
4883 const char *debug_info_name;
4884
4885 if (stash->adjusted_section_count != 0)
4886 {
4887 i = stash->adjusted_section_count;
4888 p = stash->adjusted_sections;
4889 for (; i > 0; i--, p++)
4890 p->section->vma = p->adj_vma;
4891 return true;
4892 }
4893
4894 debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
4895 i = 0;
4896 abfd = orig_bfd;
4897 while (1)
4898 {
4899 asection *sect;
4900
4901 for (sect = abfd->sections; sect != NULL; sect = sect->next)
4902 {
4903 int is_debug_info;
4904
4905 if ((sect->output_section != NULL
4906 && sect->output_section != sect
4907 && (sect->flags & SEC_DEBUGGING) == 0)
4908 || sect->vma != 0)
4909 continue;
4910
4911 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
4912 || startswith (sect->name, GNU_LINKONCE_INFO));
4913
4914 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
4915 && !is_debug_info)
4916 continue;
4917
4918 i++;
4919 }
4920 if (abfd == stash->f.bfd_ptr)
4921 break;
4922 abfd = stash->f.bfd_ptr;
4923 }
4924
4925 if (i <= 1)
4926 stash->adjusted_section_count = -1;
4927 else
4928 {
4929 bfd_vma last_vma = 0, last_dwarf = 0;
4930 size_t amt = i * sizeof (struct adjusted_section);
4931
4932 p = (struct adjusted_section *) bfd_malloc (amt);
4933 if (p == NULL)
4934 return false;
4935
4936 stash->adjusted_sections = p;
4937 stash->adjusted_section_count = i;
4938
4939 abfd = orig_bfd;
4940 while (1)
4941 {
4942 asection *sect;
4943
4944 for (sect = abfd->sections; sect != NULL; sect = sect->next)
4945 {
4946 bfd_size_type sz;
4947 int is_debug_info;
4948
4949 if ((sect->output_section != NULL
4950 && sect->output_section != sect
4951 && (sect->flags & SEC_DEBUGGING) == 0)
4952 || sect->vma != 0)
4953 continue;
4954
4955 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
4956 || startswith (sect->name, GNU_LINKONCE_INFO));
4957
4958 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
4959 && !is_debug_info)
4960 continue;
4961
4962 sz = sect->rawsize ? sect->rawsize : sect->size;
4963
4964 if (is_debug_info)
4965 {
4966 BFD_ASSERT (sect->alignment_power == 0);
4967 sect->vma = last_dwarf;
4968 last_dwarf += sz;
4969 }
4970 else
4971 {
4972 /* Align the new address to the current section
4973 alignment. */
4974 last_vma = ((last_vma
4975 + ~(-((bfd_vma) 1 << sect->alignment_power)))
4976 & (-((bfd_vma) 1 << sect->alignment_power)));
4977 sect->vma = last_vma;
4978 last_vma += sz;
4979 }
4980
4981 p->section = sect;
4982 p->adj_vma = sect->vma;
4983 p++;
4984 }
4985 if (abfd == stash->f.bfd_ptr)
4986 break;
4987 abfd = stash->f.bfd_ptr;
4988 }
4989 }
4990
4991 if (orig_bfd != stash->f.bfd_ptr)
4992 set_debug_vma (orig_bfd, stash->f.bfd_ptr);
4993
4994 return true;
4995 }
4996
4997 /* Look up a funcinfo by name using the given info hash table. If found,
4998 also update the locations pointed to by filename_ptr and linenumber_ptr.
4999
5000 This function returns TRUE if a funcinfo that matches the given symbol
5001 and address is found with any error; otherwise it returns FALSE. */
5002
5003 static bool
5004 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
5005 asymbol *sym,
5006 bfd_vma addr,
5007 const char **filename_ptr,
5008 unsigned int *linenumber_ptr)
5009 {
5010 struct funcinfo* each_func;
5011 struct funcinfo* best_fit = NULL;
5012 bfd_vma best_fit_len = 0;
5013 struct info_list_node *node;
5014 struct arange *arange;
5015 const char *name = bfd_asymbol_name (sym);
5016 asection *sec = bfd_asymbol_section (sym);
5017
5018 for (node = lookup_info_hash_table (hash_table, name);
5019 node;
5020 node = node->next)
5021 {
5022 each_func = (struct funcinfo *) node->info;
5023 for (arange = &each_func->arange;
5024 arange;
5025 arange = arange->next)
5026 {
5027 if ((!each_func->sec || each_func->sec == sec)
5028 && addr >= arange->low
5029 && addr < arange->high
5030 && (!best_fit
5031 || arange->high - arange->low < best_fit_len))
5032 {
5033 best_fit = each_func;
5034 best_fit_len = arange->high - arange->low;
5035 }
5036 }
5037 }
5038
5039 if (best_fit)
5040 {
5041 best_fit->sec = sec;
5042 *filename_ptr = best_fit->file;
5043 *linenumber_ptr = best_fit->line;
5044 return true;
5045 }
5046
5047 return false;
5048 }
5049
5050 /* Look up a varinfo by name using the given info hash table. If found,
5051 also update the locations pointed to by filename_ptr and linenumber_ptr.
5052
5053 This function returns TRUE if a varinfo that matches the given symbol
5054 and address is found with any error; otherwise it returns FALSE. */
5055
5056 static bool
5057 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
5058 asymbol *sym,
5059 bfd_vma addr,
5060 const char **filename_ptr,
5061 unsigned int *linenumber_ptr)
5062 {
5063 const char *name = bfd_asymbol_name (sym);
5064 asection *sec = bfd_asymbol_section (sym);
5065 struct varinfo* each;
5066 struct info_list_node *node;
5067
5068 for (node = lookup_info_hash_table (hash_table, name);
5069 node;
5070 node = node->next)
5071 {
5072 each = (struct varinfo *) node->info;
5073 if (each->addr == addr
5074 && (!each->sec || each->sec == sec))
5075 {
5076 each->sec = sec;
5077 *filename_ptr = each->file;
5078 *linenumber_ptr = each->line;
5079 return true;
5080 }
5081 }
5082
5083 return false;
5084 }
5085
5086 /* Update the funcinfo and varinfo info hash tables if they are
5087 not up to date. Returns TRUE if there is no error; otherwise
5088 returns FALSE and disable the info hash tables. */
5089
5090 static bool
5091 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
5092 {
5093 struct comp_unit *each;
5094
5095 /* Exit if hash tables are up-to-date. */
5096 if (stash->f.all_comp_units == stash->hash_units_head)
5097 return true;
5098
5099 if (stash->hash_units_head)
5100 each = stash->hash_units_head->prev_unit;
5101 else
5102 each = stash->f.last_comp_unit;
5103
5104 while (each)
5105 {
5106 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
5107 stash->varinfo_hash_table))
5108 {
5109 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
5110 return false;
5111 }
5112 each = each->prev_unit;
5113 }
5114
5115 stash->hash_units_head = stash->f.all_comp_units;
5116 return true;
5117 }
5118
5119 /* Check consistency of info hash tables. This is for debugging only. */
5120
5121 static void ATTRIBUTE_UNUSED
5122 stash_verify_info_hash_table (struct dwarf2_debug *stash)
5123 {
5124 struct comp_unit *each_unit;
5125 struct funcinfo *each_func;
5126 struct varinfo *each_var;
5127 struct info_list_node *node;
5128 bool found;
5129
5130 for (each_unit = stash->f.all_comp_units;
5131 each_unit;
5132 each_unit = each_unit->next_unit)
5133 {
5134 for (each_func = each_unit->function_table;
5135 each_func;
5136 each_func = each_func->prev_func)
5137 {
5138 if (!each_func->name)
5139 continue;
5140 node = lookup_info_hash_table (stash->funcinfo_hash_table,
5141 each_func->name);
5142 BFD_ASSERT (node);
5143 found = false;
5144 while (node && !found)
5145 {
5146 found = node->info == each_func;
5147 node = node->next;
5148 }
5149 BFD_ASSERT (found);
5150 }
5151
5152 for (each_var = each_unit->variable_table;
5153 each_var;
5154 each_var = each_var->prev_var)
5155 {
5156 if (!each_var->name || !each_var->file || each_var->stack)
5157 continue;
5158 node = lookup_info_hash_table (stash->varinfo_hash_table,
5159 each_var->name);
5160 BFD_ASSERT (node);
5161 found = false;
5162 while (node && !found)
5163 {
5164 found = node->info == each_var;
5165 node = node->next;
5166 }
5167 BFD_ASSERT (found);
5168 }
5169 }
5170 }
5171
5172 /* Check to see if we want to enable the info hash tables, which consume
5173 quite a bit of memory. Currently we only check the number times
5174 bfd_dwarf2_find_line is called. In the future, we may also want to
5175 take the number of symbols into account. */
5176
5177 static void
5178 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
5179 {
5180 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
5181
5182 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
5183 return;
5184
5185 /* FIXME: Maybe we should check the reduce_memory_overheads
5186 and optimize fields in the bfd_link_info structure ? */
5187
5188 /* Create hash tables. */
5189 stash->funcinfo_hash_table = create_info_hash_table (abfd);
5190 stash->varinfo_hash_table = create_info_hash_table (abfd);
5191 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
5192 {
5193 /* Turn off info hashes if any allocation above fails. */
5194 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
5195 return;
5196 }
5197 /* We need a forced update so that the info hash tables will
5198 be created even though there is no compilation unit. That
5199 happens if STASH_INFO_HASH_TRIGGER is 0. */
5200 if (stash_maybe_update_info_hash_tables (stash))
5201 stash->info_hash_status = STASH_INFO_HASH_ON;
5202 }
5203
5204 /* Find the file and line associated with a symbol and address using the
5205 info hash tables of a stash. If there is a match, the function returns
5206 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
5207 otherwise it returns FALSE. */
5208
5209 static bool
5210 stash_find_line_fast (struct dwarf2_debug *stash,
5211 asymbol *sym,
5212 bfd_vma addr,
5213 const char **filename_ptr,
5214 unsigned int *linenumber_ptr)
5215 {
5216 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
5217
5218 if (sym->flags & BSF_FUNCTION)
5219 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
5220 filename_ptr, linenumber_ptr);
5221 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
5222 filename_ptr, linenumber_ptr);
5223 }
5224
5225 /* Save current section VMAs. */
5226
5227 static bool
5228 save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
5229 {
5230 asection *s;
5231 unsigned int i;
5232
5233 if (abfd->section_count == 0)
5234 return true;
5235 stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count);
5236 if (stash->sec_vma == NULL)
5237 return false;
5238 stash->sec_vma_count = abfd->section_count;
5239 for (i = 0, s = abfd->sections;
5240 s != NULL && i < abfd->section_count;
5241 i++, s = s->next)
5242 {
5243 if (s->output_section != NULL)
5244 stash->sec_vma[i] = s->output_section->vma + s->output_offset;
5245 else
5246 stash->sec_vma[i] = s->vma;
5247 }
5248 return true;
5249 }
5250
5251 /* Compare current section VMAs against those at the time the stash
5252 was created. If find_nearest_line is used in linker warnings or
5253 errors early in the link process, the debug info stash will be
5254 invalid for later calls. This is because we relocate debug info
5255 sections, so the stashed section contents depend on symbol values,
5256 which in turn depend on section VMAs. */
5257
5258 static bool
5259 section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash)
5260 {
5261 asection *s;
5262 unsigned int i;
5263
5264 /* PR 24334: If the number of sections in ABFD has changed between
5265 when the stash was created and now, then we cannot trust the
5266 stashed vma information. */
5267 if (abfd->section_count != stash->sec_vma_count)
5268 return false;
5269
5270 for (i = 0, s = abfd->sections;
5271 s != NULL && i < abfd->section_count;
5272 i++, s = s->next)
5273 {
5274 bfd_vma vma;
5275
5276 if (s->output_section != NULL)
5277 vma = s->output_section->vma + s->output_offset;
5278 else
5279 vma = s->vma;
5280 if (vma != stash->sec_vma[i])
5281 return false;
5282 }
5283 return true;
5284 }
5285
5286 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
5287 If DEBUG_BFD is not specified, we read debug information from ABFD
5288 or its gnu_debuglink. The results will be stored in PINFO.
5289 The function returns TRUE iff debug information is ready. */
5290
5291 bool
5292 _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
5293 const struct dwarf_debug_section *debug_sections,
5294 asymbol **symbols,
5295 void **pinfo,
5296 bool do_place)
5297 {
5298 size_t amt = sizeof (struct dwarf2_debug);
5299 bfd_size_type total_size;
5300 asection *msec;
5301 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
5302
5303 if (stash != NULL)
5304 {
5305 if (stash->orig_bfd == abfd
5306 && section_vma_same (abfd, stash))
5307 {
5308 /* Check that we did previously find some debug information
5309 before attempting to make use of it. */
5310 if (stash->f.bfd_ptr != NULL)
5311 {
5312 if (do_place && !place_sections (abfd, stash))
5313 return false;
5314 return true;
5315 }
5316
5317 return false;
5318 }
5319 _bfd_dwarf2_cleanup_debug_info (abfd, pinfo);
5320 memset (stash, 0, amt);
5321 }
5322 else
5323 {
5324 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
5325 if (! stash)
5326 return false;
5327 }
5328 stash->orig_bfd = abfd;
5329 stash->debug_sections = debug_sections;
5330 stash->f.syms = symbols;
5331 if (!save_section_vma (abfd, stash))
5332 return false;
5333
5334 stash->f.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
5335 del_abbrev, calloc, free);
5336 if (!stash->f.abbrev_offsets)
5337 return false;
5338
5339 stash->alt.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
5340 del_abbrev, calloc, free);
5341 if (!stash->alt.abbrev_offsets)
5342 return false;
5343
5344 stash->f.trie_root = alloc_trie_leaf (abfd);
5345 if (!stash->f.trie_root)
5346 return false;
5347
5348 stash->alt.trie_root = alloc_trie_leaf (abfd);
5349 if (!stash->alt.trie_root)
5350 return false;
5351
5352 *pinfo = stash;
5353
5354 if (debug_bfd == NULL)
5355 debug_bfd = abfd;
5356
5357 msec = find_debug_info (debug_bfd, debug_sections, NULL);
5358 if (msec == NULL && abfd == debug_bfd)
5359 {
5360 char * debug_filename;
5361
5362 debug_filename = bfd_follow_build_id_debuglink (abfd, DEBUGDIR);
5363 if (debug_filename == NULL)
5364 debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
5365
5366 if (debug_filename == NULL)
5367 /* No dwarf2 info, and no gnu_debuglink to follow.
5368 Note that at this point the stash has been allocated, but
5369 contains zeros. This lets future calls to this function
5370 fail more quickly. */
5371 return false;
5372
5373 debug_bfd = bfd_openr (debug_filename, NULL);
5374 free (debug_filename);
5375 if (debug_bfd == NULL)
5376 /* FIXME: Should we report our failure to follow the debuglink ? */
5377 return false;
5378
5379 /* Set BFD_DECOMPRESS to decompress debug sections. */
5380 debug_bfd->flags |= BFD_DECOMPRESS;
5381 if (!bfd_check_format (debug_bfd, bfd_object)
5382 || (msec = find_debug_info (debug_bfd,
5383 debug_sections, NULL)) == NULL
5384 || !bfd_generic_link_read_symbols (debug_bfd))
5385 {
5386 bfd_close (debug_bfd);
5387 return false;
5388 }
5389
5390 symbols = bfd_get_outsymbols (debug_bfd);
5391 stash->f.syms = symbols;
5392 stash->close_on_cleanup = true;
5393 }
5394 stash->f.bfd_ptr = debug_bfd;
5395
5396 if (do_place
5397 && !place_sections (abfd, stash))
5398 return false;
5399
5400 /* There can be more than one DWARF2 info section in a BFD these
5401 days. First handle the easy case when there's only one. If
5402 there's more than one, try case two: none of the sections is
5403 compressed. In that case, read them all in and produce one
5404 large stash. We do this in two passes - in the first pass we
5405 just accumulate the section sizes, and in the second pass we
5406 read in the section's contents. (The allows us to avoid
5407 reallocing the data as we add sections to the stash.) If
5408 some or all sections are compressed, then do things the slow
5409 way, with a bunch of reallocs. */
5410
5411 if (! find_debug_info (debug_bfd, debug_sections, msec))
5412 {
5413 /* Case 1: only one info section. */
5414 total_size = msec->size;
5415 if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
5416 symbols, 0,
5417 &stash->f.dwarf_info_buffer, &total_size))
5418 return false;
5419 }
5420 else
5421 {
5422 /* Case 2: multiple sections. */
5423 for (total_size = 0;
5424 msec;
5425 msec = find_debug_info (debug_bfd, debug_sections, msec))
5426 {
5427 /* Catch PR25070 testcase overflowing size calculation here. */
5428 if (total_size + msec->size < total_size
5429 || total_size + msec->size < msec->size)
5430 {
5431 bfd_set_error (bfd_error_no_memory);
5432 return false;
5433 }
5434 total_size += msec->size;
5435 }
5436
5437 stash->f.dwarf_info_buffer = (bfd_byte *) bfd_malloc (total_size);
5438 if (stash->f.dwarf_info_buffer == NULL)
5439 return false;
5440
5441 total_size = 0;
5442 for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
5443 msec;
5444 msec = find_debug_info (debug_bfd, debug_sections, msec))
5445 {
5446 bfd_size_type size;
5447
5448 size = msec->size;
5449 if (size == 0)
5450 continue;
5451
5452 if (!(bfd_simple_get_relocated_section_contents
5453 (debug_bfd, msec, stash->f.dwarf_info_buffer + total_size,
5454 symbols)))
5455 return false;
5456
5457 total_size += size;
5458 }
5459 }
5460
5461 stash->f.info_ptr = stash->f.dwarf_info_buffer;
5462 stash->f.dwarf_info_size = total_size;
5463 return true;
5464 }
5465
5466 /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
5467
5468 static struct comp_unit *
5469 stash_comp_unit (struct dwarf2_debug *stash, struct dwarf2_debug_file *file)
5470 {
5471 bfd_size_type length;
5472 unsigned int offset_size;
5473 bfd_byte *info_ptr_unit = file->info_ptr;
5474 bfd_byte *info_ptr_end = file->dwarf_info_buffer + file->dwarf_info_size;
5475
5476 if (file->info_ptr >= info_ptr_end)
5477 return NULL;
5478
5479 length = read_4_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5480 /* A 0xffffff length is the DWARF3 way of indicating
5481 we use 64-bit offsets, instead of 32-bit offsets. */
5482 if (length == 0xffffffff)
5483 {
5484 offset_size = 8;
5485 length = read_8_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5486 }
5487 /* A zero length is the IRIX way of indicating 64-bit offsets,
5488 mostly because the 64-bit length will generally fit in 32
5489 bits, and the endianness helps. */
5490 else if (length == 0)
5491 {
5492 offset_size = 8;
5493 length = read_4_bytes (file->bfd_ptr, &file->info_ptr, info_ptr_end);
5494 }
5495 /* In the absence of the hints above, we assume 32-bit DWARF2
5496 offsets even for targets with 64-bit addresses, because:
5497 a) most of the time these targets will not have generated
5498 more than 2Gb of debug info and so will not need 64-bit
5499 offsets,
5500 and
5501 b) if they do use 64-bit offsets but they are not using
5502 the size hints that are tested for above then they are
5503 not conforming to the DWARF3 standard anyway. */
5504 else
5505 offset_size = 4;
5506
5507 if (length != 0
5508 && length <= (size_t) (info_ptr_end - file->info_ptr))
5509 {
5510 struct comp_unit *each = parse_comp_unit (stash, file,
5511 file->info_ptr, length,
5512 info_ptr_unit, offset_size);
5513 if (each)
5514 {
5515 if (file->all_comp_units)
5516 file->all_comp_units->prev_unit = each;
5517 else
5518 file->last_comp_unit = each;
5519
5520 each->next_unit = file->all_comp_units;
5521 file->all_comp_units = each;
5522
5523 if (each->arange.high == 0)
5524 {
5525 each->next_unit_without_ranges = file->all_comp_units_without_ranges;
5526 file->all_comp_units_without_ranges = each->next_unit_without_ranges;
5527 }
5528
5529 file->info_ptr += length;
5530 return each;
5531 }
5532 }
5533
5534 /* Don't trust any of the DWARF info after a corrupted length or
5535 parse error. */
5536 file->info_ptr = info_ptr_end;
5537 return NULL;
5538 }
5539
5540 /* Hash function for an asymbol. */
5541
5542 static hashval_t
5543 hash_asymbol (const void *sym)
5544 {
5545 const asymbol *asym = sym;
5546 return htab_hash_string (asym->name);
5547 }
5548
5549 /* Equality function for asymbols. */
5550
5551 static int
5552 eq_asymbol (const void *a, const void *b)
5553 {
5554 const asymbol *sa = a;
5555 const asymbol *sb = b;
5556 return strcmp (sa->name, sb->name) == 0;
5557 }
5558
5559 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
5560 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
5561 symbol in SYMBOLS and return the difference between the low_pc and
5562 the symbol's address. Returns 0 if no suitable symbol could be found. */
5563
5564 bfd_signed_vma
5565 _bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo)
5566 {
5567 struct dwarf2_debug *stash;
5568 struct comp_unit * unit;
5569 htab_t sym_hash;
5570 bfd_signed_vma result = 0;
5571 asymbol ** psym;
5572
5573 stash = (struct dwarf2_debug *) *pinfo;
5574
5575 if (stash == NULL || symbols == NULL)
5576 return 0;
5577
5578 sym_hash = htab_create_alloc (10, hash_asymbol, eq_asymbol,
5579 NULL, xcalloc, free);
5580 for (psym = symbols; * psym != NULL; psym++)
5581 {
5582 asymbol * sym = * psym;
5583
5584 if (sym->flags & BSF_FUNCTION && sym->section != NULL)
5585 {
5586 void **slot = htab_find_slot (sym_hash, sym, INSERT);
5587 *slot = sym;
5588 }
5589 }
5590
5591 for (unit = stash->f.all_comp_units; unit; unit = unit->next_unit)
5592 {
5593 struct funcinfo * func;
5594
5595 comp_unit_maybe_decode_line_info (unit);
5596
5597 for (func = unit->function_table; func != NULL; func = func->prev_func)
5598 if (func->name && func->arange.low)
5599 {
5600 asymbol search, *sym;
5601
5602 /* FIXME: Do we need to scan the aranges looking for the
5603 lowest pc value? */
5604
5605 search.name = func->name;
5606 sym = htab_find (sym_hash, &search);
5607 if (sym != NULL)
5608 {
5609 result = func->arange.low - (sym->value + sym->section->vma);
5610 goto done;
5611 }
5612 }
5613 }
5614
5615 done:
5616 htab_delete (sym_hash);
5617 return result;
5618 }
5619
5620 /* Find the source code location of SYMBOL. If SYMBOL is NULL
5621 then find the nearest source code location corresponding to
5622 the address SECTION + OFFSET.
5623 Returns 1 if the line is found without error and fills in
5624 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
5625 NULL the FUNCTIONNAME_PTR is also filled in.
5626 Returns 2 if partial information from _bfd_elf_find_function is
5627 returned (function and maybe file) by looking at symbols. DWARF2
5628 info is present but not regarding the requested code location.
5629 Returns 0 otherwise.
5630 SYMBOLS contains the symbol table for ABFD.
5631 DEBUG_SECTIONS contains the name of the dwarf debug sections. */
5632
5633 int
5634 _bfd_dwarf2_find_nearest_line (bfd *abfd,
5635 asymbol **symbols,
5636 asymbol *symbol,
5637 asection *section,
5638 bfd_vma offset,
5639 const char **filename_ptr,
5640 const char **functionname_ptr,
5641 unsigned int *linenumber_ptr,
5642 unsigned int *discriminator_ptr,
5643 const struct dwarf_debug_section *debug_sections,
5644 void **pinfo)
5645 {
5646 /* Read each compilation unit from the section .debug_info, and check
5647 to see if it contains the address we are searching for. If yes,
5648 lookup the address, and return the line number info. If no, go
5649 on to the next compilation unit.
5650
5651 We keep a list of all the previously read compilation units, and
5652 a pointer to the next un-read compilation unit. Check the
5653 previously read units before reading more. */
5654 struct dwarf2_debug *stash;
5655 /* What address are we looking for? */
5656 bfd_vma addr;
5657 struct comp_unit* each;
5658 struct funcinfo *function = NULL;
5659 int found = false;
5660 bool do_line;
5661
5662 *filename_ptr = NULL;
5663 if (functionname_ptr != NULL)
5664 *functionname_ptr = NULL;
5665 *linenumber_ptr = 0;
5666 if (discriminator_ptr)
5667 *discriminator_ptr = 0;
5668
5669 if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections,
5670 symbols, pinfo,
5671 (abfd->flags & (EXEC_P | DYNAMIC)) == 0))
5672 return false;
5673
5674 stash = (struct dwarf2_debug *) *pinfo;
5675
5676 do_line = symbol != NULL;
5677 if (do_line)
5678 {
5679 BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL);
5680 section = bfd_asymbol_section (symbol);
5681 addr = symbol->value;
5682 }
5683 else
5684 {
5685 BFD_ASSERT (section != NULL && functionname_ptr != NULL);
5686 addr = offset;
5687
5688 /* If we have no SYMBOL but the section we're looking at is not a
5689 code section, then take a look through the list of symbols to see
5690 if we have a symbol at the address we're looking for. If we do
5691 then use this to look up line information. This will allow us to
5692 give file and line results for data symbols. We exclude code
5693 symbols here, if we look up a function symbol and then look up the
5694 line information we'll actually return the line number for the
5695 opening '{' rather than the function definition line. This is
5696 because looking up by symbol uses the line table, in which the
5697 first line for a function is usually the opening '{', while
5698 looking up the function by section + offset uses the
5699 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
5700 which will be the line of the function name. */
5701 if (symbols != NULL && (section->flags & SEC_CODE) == 0)
5702 {
5703 asymbol **tmp;
5704
5705 for (tmp = symbols; (*tmp) != NULL; ++tmp)
5706 if ((*tmp)->the_bfd == abfd
5707 && (*tmp)->section == section
5708 && (*tmp)->value == offset
5709 && ((*tmp)->flags & BSF_SECTION_SYM) == 0)
5710 {
5711 symbol = *tmp;
5712 do_line = true;
5713 /* For local symbols, keep going in the hope we find a
5714 global. */
5715 if ((symbol->flags & BSF_GLOBAL) != 0)
5716 break;
5717 }
5718 }
5719 }
5720
5721 if (section->output_section)
5722 addr += section->output_section->vma + section->output_offset;
5723 else
5724 addr += section->vma;
5725
5726 /* A null info_ptr indicates that there is no dwarf2 info
5727 (or that an error occured while setting up the stash). */
5728 if (! stash->f.info_ptr)
5729 return false;
5730
5731 stash->inliner_chain = NULL;
5732
5733 /* Check the previously read comp. units first. */
5734 if (do_line)
5735 {
5736 /* The info hash tables use quite a bit of memory. We may not want to
5737 always use them. We use some heuristics to decide if and when to
5738 turn it on. */
5739 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
5740 stash_maybe_enable_info_hash_tables (abfd, stash);
5741
5742 /* Keep info hash table up to date if they are available. Note that we
5743 may disable the hash tables if there is any error duing update. */
5744 if (stash->info_hash_status == STASH_INFO_HASH_ON)
5745 stash_maybe_update_info_hash_tables (stash);
5746
5747 if (stash->info_hash_status == STASH_INFO_HASH_ON)
5748 {
5749 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
5750 linenumber_ptr);
5751 if (found)
5752 goto done;
5753 }
5754 else
5755 {
5756 /* Check the previously read comp. units first. */
5757 for (each = stash->f.all_comp_units; each; each = each->next_unit)
5758 if ((symbol->flags & BSF_FUNCTION) == 0
5759 || each->arange.high == 0
5760 || comp_unit_contains_address (each, addr))
5761 {
5762 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
5763 linenumber_ptr);
5764 if (found)
5765 goto done;
5766 }
5767 }
5768 }
5769 else
5770 {
5771 struct trie_node *trie = stash->f.trie_root;
5772 unsigned int bits = VMA_BITS - 8;
5773 struct comp_unit **prev_each;
5774
5775 /* Traverse interior nodes until we get to a leaf. */
5776 while (trie && trie->num_room_in_leaf == 0)
5777 {
5778 int ch = (addr >> bits) & 0xff;
5779 trie = ((struct trie_interior *) trie)->children[ch];
5780 bits -= 8;
5781 }
5782
5783 if (trie)
5784 {
5785 const struct trie_leaf *leaf = (struct trie_leaf *) trie;
5786 unsigned int i;
5787
5788 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
5789 leaf->ranges[i].unit->mark = false;
5790
5791 for (i = 0; i < leaf->num_stored_in_leaf; ++i)
5792 {
5793 struct comp_unit *unit = leaf->ranges[i].unit;
5794 if (unit->mark
5795 || addr < leaf->ranges[i].low_pc
5796 || addr >= leaf->ranges[i].high_pc)
5797 continue;
5798 unit->mark = true;
5799
5800 found = comp_unit_find_nearest_line (unit, addr,
5801 filename_ptr,
5802 &function,
5803 linenumber_ptr,
5804 discriminator_ptr);
5805 if (found)
5806 goto done;
5807 }
5808 }
5809
5810 /* Also scan through all compilation units without any ranges,
5811 taking them out of the list if they have acquired any since
5812 last time. */
5813 prev_each = &stash->f.all_comp_units_without_ranges;
5814 for (each = *prev_each; each; each = each->next_unit_without_ranges)
5815 {
5816 if (each->arange.high != 0)
5817 {
5818 *prev_each = each->next_unit_without_ranges;
5819 continue;
5820 }
5821
5822 found = comp_unit_find_nearest_line (each, addr,
5823 filename_ptr,
5824 &function,
5825 linenumber_ptr,
5826 discriminator_ptr);
5827 if (found)
5828 goto done;
5829 prev_each = &each->next_unit_without_ranges;
5830 }
5831 }
5832
5833 /* Read each remaining comp. units checking each as they are read. */
5834 while ((each = stash_comp_unit (stash, &stash->f)) != NULL)
5835 {
5836 /* DW_AT_low_pc and DW_AT_high_pc are optional for
5837 compilation units. If we don't have them (i.e.,
5838 unit->high == 0), we need to consult the line info table
5839 to see if a compilation unit contains the given
5840 address. */
5841 if (do_line)
5842 found = (((symbol->flags & BSF_FUNCTION) == 0
5843 || each->arange.high == 0
5844 || comp_unit_contains_address (each, addr))
5845 && comp_unit_find_line (each, symbol, addr,
5846 filename_ptr, linenumber_ptr));
5847 else
5848 found = ((each->arange.high == 0
5849 || comp_unit_contains_address (each, addr))
5850 && comp_unit_find_nearest_line (each, addr,
5851 filename_ptr,
5852 &function,
5853 linenumber_ptr,
5854 discriminator_ptr));
5855
5856 if (found)
5857 break;
5858 }
5859
5860 done:
5861 if (functionname_ptr && function && function->is_linkage)
5862 {
5863 *functionname_ptr = function->name;
5864 if (!found)
5865 found = 2;
5866 }
5867 else if (functionname_ptr
5868 && (!*functionname_ptr
5869 || (function && !function->is_linkage)))
5870 {
5871 asymbol *fun;
5872 asymbol **syms = symbols;
5873 asection *sec = section;
5874
5875 _bfd_dwarf2_stash_syms (stash, abfd, &sec, &syms);
5876 fun = _bfd_elf_find_function (abfd, syms, sec, offset,
5877 *filename_ptr ? NULL : filename_ptr,
5878 functionname_ptr);
5879
5880 if (!found && fun != NULL)
5881 found = 2;
5882
5883 if (function && !function->is_linkage)
5884 {
5885 bfd_vma sec_vma;
5886
5887 sec_vma = section->vma;
5888 if (section->output_section != NULL)
5889 sec_vma = section->output_section->vma + section->output_offset;
5890 if (fun == NULL)
5891 *functionname_ptr = function->name;
5892 else if (fun->value + sec_vma == function->arange.low)
5893 function->name = *functionname_ptr;
5894 /* Even if we didn't find a linkage name, say that we have
5895 to stop a repeated search of symbols. */
5896 function->is_linkage = true;
5897 }
5898 }
5899
5900 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
5901 unset_sections (stash);
5902
5903 return found;
5904 }
5905
5906 bool
5907 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
5908 const char **filename_ptr,
5909 const char **functionname_ptr,
5910 unsigned int *linenumber_ptr,
5911 void **pinfo)
5912 {
5913 struct dwarf2_debug *stash;
5914
5915 stash = (struct dwarf2_debug *) *pinfo;
5916 if (stash)
5917 {
5918 struct funcinfo *func = stash->inliner_chain;
5919
5920 if (func && func->caller_func)
5921 {
5922 *filename_ptr = func->caller_file;
5923 *functionname_ptr = func->caller_func->name;
5924 *linenumber_ptr = func->caller_line;
5925 stash->inliner_chain = func->caller_func;
5926 return true;
5927 }
5928 }
5929
5930 return false;
5931 }
5932
5933 void
5934 _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
5935 {
5936 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
5937 struct comp_unit *each;
5938 struct dwarf2_debug_file *file;
5939
5940 if (abfd == NULL || stash == NULL)
5941 return;
5942
5943 if (stash->varinfo_hash_table)
5944 bfd_hash_table_free (&stash->varinfo_hash_table->base);
5945 if (stash->funcinfo_hash_table)
5946 bfd_hash_table_free (&stash->funcinfo_hash_table->base);
5947
5948 file = &stash->f;
5949 while (1)
5950 {
5951 for (each = file->all_comp_units; each; each = each->next_unit)
5952 {
5953 struct funcinfo *function_table = each->function_table;
5954 struct varinfo *variable_table = each->variable_table;
5955
5956 if (each->line_table && each->line_table != file->line_table)
5957 {
5958 free (each->line_table->files);
5959 free (each->line_table->dirs);
5960 }
5961
5962 free (each->lookup_funcinfo_table);
5963 each->lookup_funcinfo_table = NULL;
5964
5965 while (function_table)
5966 {
5967 free (function_table->file);
5968 function_table->file = NULL;
5969 free (function_table->caller_file);
5970 function_table->caller_file = NULL;
5971 function_table = function_table->prev_func;
5972 }
5973
5974 while (variable_table)
5975 {
5976 free (variable_table->file);
5977 variable_table->file = NULL;
5978 variable_table = variable_table->prev_var;
5979 }
5980 }
5981
5982 if (file->line_table)
5983 {
5984 free (file->line_table->files);
5985 free (file->line_table->dirs);
5986 }
5987 htab_delete (file->abbrev_offsets);
5988
5989 free (file->dwarf_line_str_buffer);
5990 free (file->dwarf_str_buffer);
5991 free (file->dwarf_ranges_buffer);
5992 free (file->dwarf_line_buffer);
5993 free (file->dwarf_abbrev_buffer);
5994 free (file->dwarf_info_buffer);
5995 if (file == &stash->alt)
5996 break;
5997 file = &stash->alt;
5998 }
5999 free (stash->sec_vma);
6000 free (stash->adjusted_sections);
6001 if (stash->close_on_cleanup)
6002 bfd_close (stash->f.bfd_ptr);
6003 if (stash->alt.bfd_ptr)
6004 bfd_close (stash->alt.bfd_ptr);
6005 }
6006
6007 /* Find the function to a particular section and offset,
6008 for error reporting. */
6009
6010 asymbol *
6011 _bfd_elf_find_function (bfd *abfd,
6012 asymbol **symbols,
6013 asection *section,
6014 bfd_vma offset,
6015 const char **filename_ptr,
6016 const char **functionname_ptr)
6017 {
6018 struct elf_find_function_cache
6019 {
6020 asection *last_section;
6021 asymbol *func;
6022 const char *filename;
6023 bfd_size_type func_size;
6024 } *cache;
6025
6026 if (symbols == NULL)
6027 return NULL;
6028
6029 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6030 return NULL;
6031
6032 cache = elf_tdata (abfd)->elf_find_function_cache;
6033 if (cache == NULL)
6034 {
6035 cache = bfd_zalloc (abfd, sizeof (*cache));
6036 elf_tdata (abfd)->elf_find_function_cache = cache;
6037 if (cache == NULL)
6038 return NULL;
6039 }
6040 if (cache->last_section != section
6041 || cache->func == NULL
6042 || offset < cache->func->value
6043 || offset >= cache->func->value + cache->func_size)
6044 {
6045 asymbol *file;
6046 bfd_vma low_func;
6047 asymbol **p;
6048 /* ??? Given multiple file symbols, it is impossible to reliably
6049 choose the right file name for global symbols. File symbols are
6050 local symbols, and thus all file symbols must sort before any
6051 global symbols. The ELF spec may be interpreted to say that a
6052 file symbol must sort before other local symbols, but currently
6053 ld -r doesn't do this. So, for ld -r output, it is possible to
6054 make a better choice of file name for local symbols by ignoring
6055 file symbols appearing after a given local symbol. */
6056 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
6057 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6058
6059 file = NULL;
6060 low_func = 0;
6061 state = nothing_seen;
6062 cache->filename = NULL;
6063 cache->func = NULL;
6064 cache->func_size = 0;
6065 cache->last_section = section;
6066
6067 for (p = symbols; *p != NULL; p++)
6068 {
6069 asymbol *sym = *p;
6070 bfd_vma code_off;
6071 bfd_size_type size;
6072
6073 if ((sym->flags & BSF_FILE) != 0)
6074 {
6075 file = sym;
6076 if (state == symbol_seen)
6077 state = file_after_symbol_seen;
6078 continue;
6079 }
6080
6081 size = bed->maybe_function_sym (sym, section, &code_off);
6082 if (size != 0
6083 && code_off <= offset
6084 && (code_off > low_func
6085 || (code_off == low_func
6086 && size > cache->func_size)))
6087 {
6088 cache->func = sym;
6089 cache->func_size = size;
6090 cache->filename = NULL;
6091 low_func = code_off;
6092 if (file != NULL
6093 && ((sym->flags & BSF_LOCAL) != 0
6094 || state != file_after_symbol_seen))
6095 cache->filename = bfd_asymbol_name (file);
6096 }
6097 if (state == nothing_seen)
6098 state = symbol_seen;
6099 }
6100 }
6101
6102 if (cache->func == NULL)
6103 return NULL;
6104
6105 if (filename_ptr)
6106 *filename_ptr = cache->filename;
6107 if (functionname_ptr)
6108 *functionname_ptr = bfd_asymbol_name (cache->func);
6109
6110 return cache->func;
6111 }