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