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