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