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