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