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