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