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