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