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