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