* bfd.m4 (BFD_NEED_DECLARATION): Remove.
[binutils.git] / bfd / dwarf2.c
blob40883dd09ed138ecfa5b0443b1a63361b479053f
1 /* DWARF 2 support.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005 Free Software Foundation, Inc.
5 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
6 (gavin@cygnus.com).
8 From the dwarf2read.c header:
9 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10 Inc. with support from Florida State University (under contract
11 with the Ada Joint Program Office), and Silicon Graphics, Inc.
12 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14 support in dwarfread.c
16 This file is part of BFD.
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or (at
21 your option) any later version.
23 This program is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
32 #include "bfd.h"
33 #include "sysdep.h"
34 #include "libiberty.h"
35 #include "libbfd.h"
36 #include "elf-bfd.h"
37 #include "elf/dwarf2.h"
39 /* The data in the .debug_line statement prologue looks like this. */
41 struct line_head
43 bfd_vma total_length;
44 unsigned short version;
45 bfd_vma prologue_length;
46 unsigned char minimum_instruction_length;
47 unsigned char default_is_stmt;
48 int line_base;
49 unsigned char line_range;
50 unsigned char opcode_base;
51 unsigned char *standard_opcode_lengths;
54 /* Attributes have a name and a value. */
56 struct attribute
58 enum dwarf_attribute name;
59 enum dwarf_form form;
60 union
62 char *str;
63 struct dwarf_block *blk;
64 bfd_uint64_t val;
65 bfd_int64_t sval;
70 /* Blocks are a bunch of untyped bytes. */
71 struct dwarf_block
73 unsigned int size;
74 bfd_byte *data;
77 struct dwarf2_debug
79 /* A list of all previously read comp_units. */
80 struct comp_unit *all_comp_units;
82 /* The next unread compilation unit within the .debug_info section.
83 Zero indicates that the .debug_info section has not been loaded
84 into a buffer yet. */
85 bfd_byte *info_ptr;
87 /* Pointer to the end of the .debug_info section memory buffer. */
88 bfd_byte *info_ptr_end;
90 /* Pointer to the section and address of the beginning of the
91 section. */
92 asection *sec;
93 bfd_byte *sec_info_ptr;
95 /* Pointer to the symbol table. */
96 asymbol **syms;
98 /* Pointer to the .debug_abbrev section loaded into memory. */
99 bfd_byte *dwarf_abbrev_buffer;
101 /* Length of the loaded .debug_abbrev section. */
102 unsigned long dwarf_abbrev_size;
104 /* Buffer for decode_line_info. */
105 bfd_byte *dwarf_line_buffer;
107 /* Length of the loaded .debug_line section. */
108 unsigned long dwarf_line_size;
110 /* Pointer to the .debug_str section loaded into memory. */
111 bfd_byte *dwarf_str_buffer;
113 /* Length of the loaded .debug_str section. */
114 unsigned long dwarf_str_size;
116 /* Pointer to the .debug_ranges section loaded into memory. */
117 bfd_byte *dwarf_ranges_buffer;
119 /* Length of the loaded .debug_ranges section. */
120 unsigned long dwarf_ranges_size;
122 /* If the most recent call to bfd_find_nearest_line was given an
123 address in an inlined function, preserve a pointer into the
124 calling chain for subsequent calls to bfd_find_inliner_info to
125 use. */
126 struct funcinfo *inliner_chain;
129 struct arange
131 struct arange *next;
132 bfd_vma low;
133 bfd_vma high;
136 /* A minimal decoding of DWARF2 compilation units. We only decode
137 what's needed to get to the line number information. */
139 struct comp_unit
141 /* Chain the previously read compilation units. */
142 struct comp_unit *next_unit;
144 /* Keep the bfd convenient (for memory allocation). */
145 bfd *abfd;
147 /* The lowest and highest addresses contained in this compilation
148 unit as specified in the compilation unit header. */
149 struct arange arange;
151 /* The DW_AT_name attribute (for error messages). */
152 char *name;
154 /* The abbrev hash table. */
155 struct abbrev_info **abbrevs;
157 /* Note that an error was found by comp_unit_find_nearest_line. */
158 int error;
160 /* The DW_AT_comp_dir attribute. */
161 char *comp_dir;
163 /* TRUE if there is a line number table associated with this comp. unit. */
164 int stmtlist;
166 /* Pointer to the current comp_unit so that we can find a given entry
167 by its reference. */
168 bfd_byte *info_ptr_unit;
170 /* The offset into .debug_line of the line number table. */
171 unsigned long line_offset;
173 /* Pointer to the first child die for the comp unit. */
174 bfd_byte *first_child_die_ptr;
176 /* The end of the comp unit. */
177 bfd_byte *end_ptr;
179 /* The decoded line number, NULL if not yet decoded. */
180 struct line_info_table *line_table;
182 /* A list of the functions found in this comp. unit. */
183 struct funcinfo *function_table;
185 /* A list of the variables found in this comp. unit. */
186 struct varinfo *variable_table;
188 /* Pointer to dwarf2_debug structure. */
189 struct dwarf2_debug *stash;
191 /* Address size for this unit - from unit header. */
192 unsigned char addr_size;
194 /* Offset size for this unit - from unit header. */
195 unsigned char offset_size;
197 /* Base address for this unit - from DW_AT_low_pc attribute of
198 DW_TAG_compile_unit DIE */
199 bfd_vma base_address;
202 /* This data structure holds the information of an abbrev. */
203 struct abbrev_info
205 unsigned int number; /* Number identifying abbrev. */
206 enum dwarf_tag tag; /* DWARF tag. */
207 int has_children; /* Boolean. */
208 unsigned int num_attrs; /* Number of attributes. */
209 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
210 struct abbrev_info *next; /* Next in chain. */
213 struct attr_abbrev
215 enum dwarf_attribute name;
216 enum dwarf_form form;
219 #ifndef ABBREV_HASH_SIZE
220 #define ABBREV_HASH_SIZE 121
221 #endif
222 #ifndef ATTR_ALLOC_CHUNK
223 #define ATTR_ALLOC_CHUNK 4
224 #endif
226 /* VERBATIM
227 The following function up to the END VERBATIM mark are
228 copied directly from dwarf2read.c. */
230 /* Read dwarf information from a buffer. */
232 static unsigned int
233 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
235 return bfd_get_8 (abfd, buf);
238 static int
239 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
241 return bfd_get_signed_8 (abfd, buf);
244 static unsigned int
245 read_2_bytes (bfd *abfd, bfd_byte *buf)
247 return bfd_get_16 (abfd, buf);
250 static unsigned int
251 read_4_bytes (bfd *abfd, bfd_byte *buf)
253 return bfd_get_32 (abfd, buf);
256 static bfd_uint64_t
257 read_8_bytes (bfd *abfd, bfd_byte *buf)
259 return bfd_get_64 (abfd, buf);
262 static bfd_byte *
263 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
264 bfd_byte *buf,
265 unsigned int size ATTRIBUTE_UNUSED)
267 /* If the size of a host char is 8 bits, we can return a pointer
268 to the buffer, otherwise we have to copy the data to a buffer
269 allocated on the temporary obstack. */
270 return buf;
273 static char *
274 read_string (bfd *abfd ATTRIBUTE_UNUSED,
275 bfd_byte *buf,
276 unsigned int *bytes_read_ptr)
278 /* Return a pointer to the embedded string. */
279 char *str = (char *) buf;
280 if (*str == '\0')
282 *bytes_read_ptr = 1;
283 return NULL;
286 *bytes_read_ptr = strlen (str) + 1;
287 return str;
290 static char *
291 read_indirect_string (struct comp_unit* unit,
292 bfd_byte *buf,
293 unsigned int *bytes_read_ptr)
295 bfd_uint64_t offset;
296 struct dwarf2_debug *stash = unit->stash;
297 char *str;
299 if (unit->offset_size == 4)
300 offset = read_4_bytes (unit->abfd, buf);
301 else
302 offset = read_8_bytes (unit->abfd, buf);
303 *bytes_read_ptr = unit->offset_size;
305 if (! stash->dwarf_str_buffer)
307 asection *msec;
308 bfd *abfd = unit->abfd;
309 bfd_size_type sz;
311 msec = bfd_get_section_by_name (abfd, ".debug_str");
312 if (! msec)
314 (*_bfd_error_handler)
315 (_("Dwarf Error: Can't find .debug_str section."));
316 bfd_set_error (bfd_error_bad_value);
317 return NULL;
320 sz = msec->rawsize ? msec->rawsize : msec->size;
321 stash->dwarf_str_size = sz;
322 stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
323 if (! stash->dwarf_str_buffer)
324 return NULL;
326 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
327 0, sz))
328 return NULL;
331 if (offset >= stash->dwarf_str_size)
333 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
334 (unsigned long) offset, stash->dwarf_str_size);
335 bfd_set_error (bfd_error_bad_value);
336 return NULL;
339 str = (char *) stash->dwarf_str_buffer + offset;
340 if (*str == '\0')
341 return NULL;
342 return str;
345 /* END VERBATIM */
347 static bfd_uint64_t
348 read_address (struct comp_unit *unit, bfd_byte *buf)
350 int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
352 if (signed_vma)
354 switch (unit->addr_size)
356 case 8:
357 return bfd_get_signed_64 (unit->abfd, buf);
358 case 4:
359 return bfd_get_signed_32 (unit->abfd, buf);
360 case 2:
361 return bfd_get_signed_16 (unit->abfd, buf);
362 default:
363 abort ();
366 else
368 switch (unit->addr_size)
370 case 8:
371 return bfd_get_64 (unit->abfd, buf);
372 case 4:
373 return bfd_get_32 (unit->abfd, buf);
374 case 2:
375 return bfd_get_16 (unit->abfd, buf);
376 default:
377 abort ();
382 /* Lookup an abbrev_info structure in the abbrev hash table. */
384 static struct abbrev_info *
385 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
387 unsigned int hash_number;
388 struct abbrev_info *abbrev;
390 hash_number = number % ABBREV_HASH_SIZE;
391 abbrev = abbrevs[hash_number];
393 while (abbrev)
395 if (abbrev->number == number)
396 return abbrev;
397 else
398 abbrev = abbrev->next;
401 return NULL;
404 /* In DWARF version 2, the description of the debugging information is
405 stored in a separate .debug_abbrev section. Before we read any
406 dies from a section we read in all abbreviations and install them
407 in a hash table. */
409 static struct abbrev_info**
410 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
412 struct abbrev_info **abbrevs;
413 bfd_byte *abbrev_ptr;
414 struct abbrev_info *cur_abbrev;
415 unsigned int abbrev_number, bytes_read, abbrev_name;
416 unsigned int abbrev_form, hash_number;
417 bfd_size_type amt;
419 if (! stash->dwarf_abbrev_buffer)
421 asection *msec;
423 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
424 if (! msec)
426 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
427 bfd_set_error (bfd_error_bad_value);
428 return 0;
431 stash->dwarf_abbrev_size = msec->size;
432 stash->dwarf_abbrev_buffer
433 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
434 stash->syms);
435 if (! stash->dwarf_abbrev_buffer)
436 return 0;
439 if (offset >= stash->dwarf_abbrev_size)
441 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
442 (unsigned long) offset, stash->dwarf_abbrev_size);
443 bfd_set_error (bfd_error_bad_value);
444 return 0;
447 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
448 abbrevs = bfd_zalloc (abfd, amt);
450 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
451 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
452 abbrev_ptr += bytes_read;
454 /* Loop until we reach an abbrev number of 0. */
455 while (abbrev_number)
457 amt = sizeof (struct abbrev_info);
458 cur_abbrev = bfd_zalloc (abfd, amt);
460 /* Read in abbrev header. */
461 cur_abbrev->number = abbrev_number;
462 cur_abbrev->tag = (enum dwarf_tag)
463 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
464 abbrev_ptr += bytes_read;
465 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
466 abbrev_ptr += 1;
468 /* Now read in declarations. */
469 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
470 abbrev_ptr += bytes_read;
471 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
472 abbrev_ptr += bytes_read;
474 while (abbrev_name)
476 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
478 struct attr_abbrev *tmp;
480 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
481 amt *= sizeof (struct attr_abbrev);
482 tmp = bfd_realloc (cur_abbrev->attrs, amt);
483 if (tmp == NULL)
485 size_t i;
487 for (i = 0; i < ABBREV_HASH_SIZE; i++)
489 struct abbrev_info *abbrev = abbrevs[i];
491 while (abbrev)
493 free (abbrev->attrs);
494 abbrev = abbrev->next;
497 return NULL;
499 cur_abbrev->attrs = tmp;
502 cur_abbrev->attrs[cur_abbrev->num_attrs].name
503 = (enum dwarf_attribute) abbrev_name;
504 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
505 = (enum dwarf_form) abbrev_form;
506 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
507 abbrev_ptr += bytes_read;
508 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
509 abbrev_ptr += bytes_read;
512 hash_number = abbrev_number % ABBREV_HASH_SIZE;
513 cur_abbrev->next = abbrevs[hash_number];
514 abbrevs[hash_number] = cur_abbrev;
516 /* Get next abbreviation.
517 Under Irix6 the abbreviations for a compilation unit are not
518 always properly terminated with an abbrev number of 0.
519 Exit loop if we encounter an abbreviation which we have
520 already read (which means we are about to read the abbreviations
521 for the next compile unit) or if the end of the abbreviation
522 table is reached. */
523 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
524 >= stash->dwarf_abbrev_size)
525 break;
526 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
527 abbrev_ptr += bytes_read;
528 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
529 break;
532 return abbrevs;
535 /* Read an attribute value described by an attribute form. */
537 static bfd_byte *
538 read_attribute_value (struct attribute *attr,
539 unsigned form,
540 struct comp_unit *unit,
541 bfd_byte *info_ptr)
543 bfd *abfd = unit->abfd;
544 unsigned int bytes_read;
545 struct dwarf_block *blk;
546 bfd_size_type amt;
548 attr->form = (enum dwarf_form) form;
550 switch (form)
552 case DW_FORM_addr:
553 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
554 case DW_FORM_ref_addr:
555 attr->u.val = read_address (unit, info_ptr);
556 info_ptr += unit->addr_size;
557 break;
558 case DW_FORM_block2:
559 amt = sizeof (struct dwarf_block);
560 blk = bfd_alloc (abfd, amt);
561 blk->size = read_2_bytes (abfd, info_ptr);
562 info_ptr += 2;
563 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
564 info_ptr += blk->size;
565 attr->u.blk = blk;
566 break;
567 case DW_FORM_block4:
568 amt = sizeof (struct dwarf_block);
569 blk = bfd_alloc (abfd, amt);
570 blk->size = read_4_bytes (abfd, info_ptr);
571 info_ptr += 4;
572 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
573 info_ptr += blk->size;
574 attr->u.blk = blk;
575 break;
576 case DW_FORM_data2:
577 attr->u.val = read_2_bytes (abfd, info_ptr);
578 info_ptr += 2;
579 break;
580 case DW_FORM_data4:
581 attr->u.val = read_4_bytes (abfd, info_ptr);
582 info_ptr += 4;
583 break;
584 case DW_FORM_data8:
585 attr->u.val = read_8_bytes (abfd, info_ptr);
586 info_ptr += 8;
587 break;
588 case DW_FORM_string:
589 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
590 info_ptr += bytes_read;
591 break;
592 case DW_FORM_strp:
593 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
594 info_ptr += bytes_read;
595 break;
596 case DW_FORM_block:
597 amt = sizeof (struct dwarf_block);
598 blk = bfd_alloc (abfd, amt);
599 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
600 info_ptr += bytes_read;
601 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
602 info_ptr += blk->size;
603 attr->u.blk = blk;
604 break;
605 case DW_FORM_block1:
606 amt = sizeof (struct dwarf_block);
607 blk = bfd_alloc (abfd, amt);
608 blk->size = read_1_byte (abfd, info_ptr);
609 info_ptr += 1;
610 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
611 info_ptr += blk->size;
612 attr->u.blk = blk;
613 break;
614 case DW_FORM_data1:
615 attr->u.val = read_1_byte (abfd, info_ptr);
616 info_ptr += 1;
617 break;
618 case DW_FORM_flag:
619 attr->u.val = read_1_byte (abfd, info_ptr);
620 info_ptr += 1;
621 break;
622 case DW_FORM_sdata:
623 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
624 info_ptr += bytes_read;
625 break;
626 case DW_FORM_udata:
627 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
628 info_ptr += bytes_read;
629 break;
630 case DW_FORM_ref1:
631 attr->u.val = read_1_byte (abfd, info_ptr);
632 info_ptr += 1;
633 break;
634 case DW_FORM_ref2:
635 attr->u.val = read_2_bytes (abfd, info_ptr);
636 info_ptr += 2;
637 break;
638 case DW_FORM_ref4:
639 attr->u.val = read_4_bytes (abfd, info_ptr);
640 info_ptr += 4;
641 break;
642 case DW_FORM_ref8:
643 attr->u.val = read_8_bytes (abfd, info_ptr);
644 info_ptr += 8;
645 break;
646 case DW_FORM_ref_udata:
647 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
648 info_ptr += bytes_read;
649 break;
650 case DW_FORM_indirect:
651 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
652 info_ptr += bytes_read;
653 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
654 break;
655 default:
656 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
657 form);
658 bfd_set_error (bfd_error_bad_value);
660 return info_ptr;
663 /* Read an attribute described by an abbreviated attribute. */
665 static bfd_byte *
666 read_attribute (struct attribute *attr,
667 struct attr_abbrev *abbrev,
668 struct comp_unit *unit,
669 bfd_byte *info_ptr)
671 attr->name = abbrev->name;
672 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
673 return info_ptr;
676 /* Source line information table routines. */
678 #define FILE_ALLOC_CHUNK 5
679 #define DIR_ALLOC_CHUNK 5
681 struct line_info
683 struct line_info* prev_line;
684 bfd_vma address;
685 char *filename;
686 unsigned int line;
687 unsigned int column;
688 int end_sequence; /* End of (sequential) code sequence. */
691 struct fileinfo
693 char *name;
694 unsigned int dir;
695 unsigned int time;
696 unsigned int size;
699 struct line_info_table
701 bfd* abfd;
702 unsigned int num_files;
703 unsigned int num_dirs;
704 char *comp_dir;
705 char **dirs;
706 struct fileinfo* files;
707 struct line_info* last_line; /* largest VMA */
708 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
711 /* Remember some information about each function. If the function is
712 inlined (DW_TAG_inlined_subroutine) it may have two additional
713 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
714 source code location where this function was inlined. */
716 struct funcinfo
718 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */
719 struct funcinfo *caller_func; /* Pointer to function one scope higher */
720 char *caller_file; /* Source location file name where caller_func inlines this func */
721 int caller_line; /* Source location line number where caller_func inlines this func */
722 char *file; /* Source location file name */
723 int line; /* Source location line number */
724 int tag;
725 int nesting_level;
726 char *name;
727 struct arange arange;
728 asection *sec; /* Where the symbol is defined */
731 struct varinfo
733 /* Pointer to previous variable in list of all variables */
734 struct varinfo *prev_var;
735 /* Source location file name */
736 char *file;
737 /* Source location line number */
738 int line;
739 int tag;
740 char *name;
741 bfd_vma addr;
742 /* Where the symbol is defined */
743 asection *sec;
744 /* Is this a stack variable? */
745 unsigned int stack: 1;
748 /* Adds a new entry to the line_info list in the line_info_table, ensuring
749 that the list is sorted. Note that the line_info list is sorted from
750 highest to lowest VMA (with possible duplicates); that is,
751 line_info->prev_line always accesses an equal or smaller VMA. */
753 static void
754 add_line_info (struct line_info_table *table,
755 bfd_vma address,
756 char *filename,
757 unsigned int line,
758 unsigned int column,
759 int end_sequence)
761 bfd_size_type amt = sizeof (struct line_info);
762 struct line_info* info = bfd_alloc (table->abfd, amt);
764 /* Find the correct location for 'info'. Normally we will receive
765 new line_info data 1) in order and 2) with increasing VMAs.
766 However some compilers break the rules (cf. decode_line_info) and
767 so we include some heuristics for quickly finding the correct
768 location for 'info'. In particular, these heuristics optimize for
769 the common case in which the VMA sequence that we receive is a
770 list of locally sorted VMAs such as
771 p...z a...j (where a < j < p < z)
773 Note: table->lcl_head is used to head an *actual* or *possible*
774 sequence within the list (such as a...j) that is not directly
775 headed by table->last_line
777 Note: we may receive duplicate entries from 'decode_line_info'. */
779 while (1)
780 if (!table->last_line
781 || address >= table->last_line->address)
783 /* Normal case: add 'info' to the beginning of the list */
784 info->prev_line = table->last_line;
785 table->last_line = info;
787 /* lcl_head: initialize to head a *possible* sequence at the end. */
788 if (!table->lcl_head)
789 table->lcl_head = info;
790 break;
792 else if (!table->lcl_head->prev_line
793 && table->lcl_head->address > address)
795 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
796 list and 2) the head of 'info'. */
797 info->prev_line = NULL;
798 table->lcl_head->prev_line = info;
799 break;
801 else if (table->lcl_head->prev_line
802 && table->lcl_head->address > address
803 && address >= table->lcl_head->prev_line->address)
805 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
806 list and 2) the head of 'info'. */
807 info->prev_line = table->lcl_head->prev_line;
808 table->lcl_head->prev_line = info;
809 break;
811 else
813 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
814 heads for 'info'. Reset 'lcl_head' and repeat. */
815 struct line_info* li2 = table->last_line; /* always non-NULL */
816 struct line_info* li1 = li2->prev_line;
818 while (li1)
820 if (li2->address > address && address >= li1->address)
821 break;
823 li2 = li1; /* always non-NULL */
824 li1 = li1->prev_line;
826 table->lcl_head = li2;
829 /* Set member data of 'info'. */
830 info->address = address;
831 info->line = line;
832 info->column = column;
833 info->end_sequence = end_sequence;
835 if (filename && filename[0])
837 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
838 if (info->filename)
839 strcpy (info->filename, filename);
841 else
842 info->filename = NULL;
845 /* Extract a fully qualified filename from a line info table.
846 The returned string has been malloc'ed and it is the caller's
847 responsibility to free it. */
849 static char *
850 concat_filename (struct line_info_table *table, unsigned int file)
852 char *filename;
854 if (file - 1 >= table->num_files)
856 (*_bfd_error_handler)
857 (_("Dwarf Error: mangled line number section (bad file number)."));
858 return strdup ("<unknown>");
861 filename = table->files[file - 1].name;
863 if (! IS_ABSOLUTE_PATH (filename))
865 char *dirname = (table->files[file - 1].dir
866 ? table->dirs[table->files[file - 1].dir - 1]
867 : table->comp_dir);
869 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
870 The best we can do is return the filename part. */
871 if (dirname != NULL)
873 unsigned int len = strlen (dirname) + strlen (filename) + 2;
874 char * name;
876 name = bfd_malloc (len);
877 if (name)
878 sprintf (name, "%s/%s", dirname, filename);
879 return name;
883 return strdup (filename);
886 static void
887 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
889 struct arange *arange;
891 /* If the first arange is empty, use it. */
892 if (first_arange->high == 0)
894 first_arange->low = low_pc;
895 first_arange->high = high_pc;
896 return;
899 /* Next see if we can cheaply extend an existing range. */
900 arange = first_arange;
903 if (low_pc == arange->high)
905 arange->high = high_pc;
906 return;
908 if (high_pc == arange->low)
910 arange->low = low_pc;
911 return;
913 arange = arange->next;
915 while (arange);
917 /* Need to allocate a new arange and insert it into the arange list.
918 Order isn't significant, so just insert after the first arange. */
919 arange = bfd_zalloc (abfd, sizeof (*arange));
920 arange->low = low_pc;
921 arange->high = high_pc;
922 arange->next = first_arange->next;
923 first_arange->next = arange;
926 /* Decode the line number information for UNIT. */
928 static struct line_info_table*
929 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
931 bfd *abfd = unit->abfd;
932 struct line_info_table* table;
933 bfd_byte *line_ptr;
934 bfd_byte *line_end;
935 struct line_head lh;
936 unsigned int i, bytes_read, offset_size;
937 char *cur_file, *cur_dir;
938 unsigned char op_code, extended_op, adj_opcode;
939 bfd_size_type amt;
941 if (! stash->dwarf_line_buffer)
943 asection *msec;
945 msec = bfd_get_section_by_name (abfd, ".debug_line");
946 if (! msec)
948 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
949 bfd_set_error (bfd_error_bad_value);
950 return 0;
953 stash->dwarf_line_size = msec->size;
954 stash->dwarf_line_buffer
955 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
956 stash->syms);
957 if (! stash->dwarf_line_buffer)
958 return 0;
961 /* It is possible to get a bad value for the line_offset. Validate
962 it here so that we won't get a segfault below. */
963 if (unit->line_offset >= stash->dwarf_line_size)
965 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
966 unit->line_offset, stash->dwarf_line_size);
967 bfd_set_error (bfd_error_bad_value);
968 return 0;
971 amt = sizeof (struct line_info_table);
972 table = bfd_alloc (abfd, amt);
973 table->abfd = abfd;
974 table->comp_dir = unit->comp_dir;
976 table->num_files = 0;
977 table->files = NULL;
979 table->num_dirs = 0;
980 table->dirs = NULL;
982 table->files = NULL;
983 table->last_line = NULL;
984 table->lcl_head = NULL;
986 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
988 /* Read in the prologue. */
989 lh.total_length = read_4_bytes (abfd, line_ptr);
990 line_ptr += 4;
991 offset_size = 4;
992 if (lh.total_length == 0xffffffff)
994 lh.total_length = read_8_bytes (abfd, line_ptr);
995 line_ptr += 8;
996 offset_size = 8;
998 else if (lh.total_length == 0 && unit->addr_size == 8)
1000 /* Handle (non-standard) 64-bit DWARF2 formats. */
1001 lh.total_length = read_4_bytes (abfd, line_ptr);
1002 line_ptr += 4;
1003 offset_size = 8;
1005 line_end = line_ptr + lh.total_length;
1006 lh.version = read_2_bytes (abfd, line_ptr);
1007 line_ptr += 2;
1008 if (offset_size == 4)
1009 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1010 else
1011 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1012 line_ptr += offset_size;
1013 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1014 line_ptr += 1;
1015 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1016 line_ptr += 1;
1017 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1018 line_ptr += 1;
1019 lh.line_range = read_1_byte (abfd, line_ptr);
1020 line_ptr += 1;
1021 lh.opcode_base = read_1_byte (abfd, line_ptr);
1022 line_ptr += 1;
1023 amt = lh.opcode_base * sizeof (unsigned char);
1024 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
1026 lh.standard_opcode_lengths[0] = 1;
1028 for (i = 1; i < lh.opcode_base; ++i)
1030 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1031 line_ptr += 1;
1034 /* Read directory table. */
1035 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1037 line_ptr += bytes_read;
1039 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1041 char **tmp;
1043 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1044 amt *= sizeof (char *);
1046 tmp = bfd_realloc (table->dirs, amt);
1047 if (tmp == NULL)
1049 free (table->dirs);
1050 return NULL;
1052 table->dirs = tmp;
1055 table->dirs[table->num_dirs++] = cur_dir;
1058 line_ptr += bytes_read;
1060 /* Read file name table. */
1061 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1063 line_ptr += bytes_read;
1065 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1067 struct fileinfo *tmp;
1069 amt = table->num_files + FILE_ALLOC_CHUNK;
1070 amt *= sizeof (struct fileinfo);
1072 tmp = bfd_realloc (table->files, amt);
1073 if (tmp == NULL)
1075 free (table->files);
1076 free (table->dirs);
1077 return NULL;
1079 table->files = tmp;
1082 table->files[table->num_files].name = cur_file;
1083 table->files[table->num_files].dir =
1084 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1085 line_ptr += bytes_read;
1086 table->files[table->num_files].time =
1087 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1088 line_ptr += bytes_read;
1089 table->files[table->num_files].size =
1090 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1091 line_ptr += bytes_read;
1092 table->num_files++;
1095 line_ptr += bytes_read;
1097 /* Read the statement sequences until there's nothing left. */
1098 while (line_ptr < line_end)
1100 /* State machine registers. */
1101 bfd_vma address = 0;
1102 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1103 unsigned int line = 1;
1104 unsigned int column = 0;
1105 int is_stmt = lh.default_is_stmt;
1106 int basic_block = 0;
1107 int end_sequence = 0;
1108 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1109 compilers generate address sequences that are wildly out of
1110 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1111 for ia64-Linux). Thus, to determine the low and high
1112 address, we must compare on every DW_LNS_copy, etc. */
1113 bfd_vma low_pc = (bfd_vma) -1;
1114 bfd_vma high_pc = 0;
1116 /* Decode the table. */
1117 while (! end_sequence)
1119 op_code = read_1_byte (abfd, line_ptr);
1120 line_ptr += 1;
1122 if (op_code >= lh.opcode_base)
1124 /* Special operand. */
1125 adj_opcode = op_code - lh.opcode_base;
1126 address += (adj_opcode / lh.line_range)
1127 * lh.minimum_instruction_length;
1128 line += lh.line_base + (adj_opcode % lh.line_range);
1129 /* Append row to matrix using current values. */
1130 add_line_info (table, address, filename, line, column, 0);
1131 basic_block = 1;
1132 if (address < low_pc)
1133 low_pc = address;
1134 if (address > high_pc)
1135 high_pc = address;
1137 else switch (op_code)
1139 case DW_LNS_extended_op:
1140 /* Ignore length. */
1141 line_ptr += 1;
1142 extended_op = read_1_byte (abfd, line_ptr);
1143 line_ptr += 1;
1145 switch (extended_op)
1147 case DW_LNE_end_sequence:
1148 end_sequence = 1;
1149 add_line_info (table, address, filename, line, column,
1150 end_sequence);
1151 if (address < low_pc)
1152 low_pc = address;
1153 if (address > high_pc)
1154 high_pc = address;
1155 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1156 break;
1157 case DW_LNE_set_address:
1158 address = read_address (unit, line_ptr);
1159 line_ptr += unit->addr_size;
1160 break;
1161 case DW_LNE_define_file:
1162 cur_file = read_string (abfd, line_ptr, &bytes_read);
1163 line_ptr += bytes_read;
1164 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1166 struct fileinfo *tmp;
1168 amt = table->num_files + FILE_ALLOC_CHUNK;
1169 amt *= sizeof (struct fileinfo);
1170 tmp = bfd_realloc (table->files, amt);
1171 if (tmp == NULL)
1173 free (table->files);
1174 free (table->dirs);
1175 free (filename);
1176 return NULL;
1178 table->files = tmp;
1180 table->files[table->num_files].name = cur_file;
1181 table->files[table->num_files].dir =
1182 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1183 line_ptr += bytes_read;
1184 table->files[table->num_files].time =
1185 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1186 line_ptr += bytes_read;
1187 table->files[table->num_files].size =
1188 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1189 line_ptr += bytes_read;
1190 table->num_files++;
1191 break;
1192 default:
1193 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1194 bfd_set_error (bfd_error_bad_value);
1195 free (filename);
1196 free (table->files);
1197 free (table->dirs);
1198 return NULL;
1200 break;
1201 case DW_LNS_copy:
1202 add_line_info (table, address, filename, line, column, 0);
1203 basic_block = 0;
1204 if (address < low_pc)
1205 low_pc = address;
1206 if (address > high_pc)
1207 high_pc = address;
1208 break;
1209 case DW_LNS_advance_pc:
1210 address += lh.minimum_instruction_length
1211 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1212 line_ptr += bytes_read;
1213 break;
1214 case DW_LNS_advance_line:
1215 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1216 line_ptr += bytes_read;
1217 break;
1218 case DW_LNS_set_file:
1220 unsigned int file;
1222 /* The file and directory tables are 0
1223 based, the references are 1 based. */
1224 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1225 line_ptr += bytes_read;
1226 if (filename)
1227 free (filename);
1228 filename = concat_filename (table, file);
1229 break;
1231 case DW_LNS_set_column:
1232 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1233 line_ptr += bytes_read;
1234 break;
1235 case DW_LNS_negate_stmt:
1236 is_stmt = (!is_stmt);
1237 break;
1238 case DW_LNS_set_basic_block:
1239 basic_block = 1;
1240 break;
1241 case DW_LNS_const_add_pc:
1242 address += lh.minimum_instruction_length
1243 * ((255 - lh.opcode_base) / lh.line_range);
1244 break;
1245 case DW_LNS_fixed_advance_pc:
1246 address += read_2_bytes (abfd, line_ptr);
1247 line_ptr += 2;
1248 break;
1249 default:
1251 int i;
1253 /* Unknown standard opcode, ignore it. */
1254 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1256 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1257 line_ptr += bytes_read;
1263 if (filename)
1264 free (filename);
1267 return table;
1270 /* If ADDR is within TABLE set the output parameters and return TRUE,
1271 otherwise return FALSE. The output parameters, FILENAME_PTR and
1272 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1274 static bfd_boolean
1275 lookup_address_in_line_info_table (struct line_info_table *table,
1276 bfd_vma addr,
1277 struct funcinfo *function,
1278 const char **filename_ptr,
1279 unsigned int *linenumber_ptr)
1281 /* Note: table->last_line should be a descendingly sorted list. */
1282 struct line_info* next_line = table->last_line;
1283 struct line_info* each_line = NULL;
1284 *filename_ptr = NULL;
1286 if (!next_line)
1287 return FALSE;
1289 each_line = next_line->prev_line;
1291 /* Check for large addresses */
1292 if (addr > next_line->address)
1293 each_line = NULL; /* ensure we skip over the normal case */
1295 /* Normal case: search the list; save */
1296 while (each_line && next_line)
1298 /* If we have an address match, save this info. This allows us
1299 to return as good as results as possible for strange debugging
1300 info. */
1301 bfd_boolean addr_match = FALSE;
1302 if (each_line->address <= addr && addr < next_line->address)
1304 addr_match = TRUE;
1306 /* If this line appears to span functions, and addr is in the
1307 later function, return the first line of that function instead
1308 of the last line of the earlier one. This check is for GCC
1309 2.95, which emits the first line number for a function late. */
1311 if (function != NULL)
1313 bfd_vma lowest_pc;
1314 struct arange *arange;
1316 /* Find the lowest address in the function's range list */
1317 lowest_pc = function->arange.low;
1318 for (arange = &function->arange;
1319 arange;
1320 arange = arange->next)
1322 if (function->arange.low < lowest_pc)
1323 lowest_pc = function->arange.low;
1325 /* Check for spanning function and set outgoing line info */
1326 if (addr >= lowest_pc
1327 && each_line->address < lowest_pc
1328 && next_line->address > lowest_pc)
1330 *filename_ptr = next_line->filename;
1331 *linenumber_ptr = next_line->line;
1333 else
1335 *filename_ptr = each_line->filename;
1336 *linenumber_ptr = each_line->line;
1339 else
1341 *filename_ptr = each_line->filename;
1342 *linenumber_ptr = each_line->line;
1346 if (addr_match && !each_line->end_sequence)
1347 return TRUE; /* we have definitely found what we want */
1349 next_line = each_line;
1350 each_line = each_line->prev_line;
1353 /* At this point each_line is NULL but next_line is not. If we found
1354 a candidate end-of-sequence point in the loop above, we can return
1355 that (compatibility with a bug in the Intel compiler); otherwise,
1356 assuming that we found the containing function for this address in
1357 this compilation unit, return the first line we have a number for
1358 (compatibility with GCC 2.95). */
1359 if (*filename_ptr == NULL && function != NULL)
1361 *filename_ptr = next_line->filename;
1362 *linenumber_ptr = next_line->line;
1363 return TRUE;
1366 return FALSE;
1369 /* Read in the .debug_ranges section for future reference */
1371 static bfd_boolean
1372 read_debug_ranges (struct comp_unit *unit)
1374 struct dwarf2_debug *stash = unit->stash;
1375 if (! stash->dwarf_ranges_buffer)
1377 bfd *abfd = unit->abfd;
1378 asection *msec;
1380 msec = bfd_get_section_by_name (abfd, ".debug_ranges");
1381 if (! msec)
1383 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
1384 bfd_set_error (bfd_error_bad_value);
1385 return FALSE;
1388 stash->dwarf_ranges_size = msec->size;
1389 stash->dwarf_ranges_buffer
1390 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1391 stash->syms);
1392 if (! stash->dwarf_ranges_buffer)
1393 return FALSE;
1395 return TRUE;
1398 /* Function table functions. */
1400 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1401 Note that we need to find the function that has the smallest
1402 range that contains ADDR, to handle inlined functions without
1403 depending upon them being ordered in TABLE by increasing range. */
1405 static bfd_boolean
1406 lookup_address_in_function_table (struct comp_unit *unit,
1407 bfd_vma addr,
1408 struct funcinfo **function_ptr,
1409 const char **functionname_ptr)
1411 struct funcinfo* each_func;
1412 struct funcinfo* best_fit = NULL;
1413 struct arange *arange;
1415 for (each_func = unit->function_table;
1416 each_func;
1417 each_func = each_func->prev_func)
1419 for (arange = &each_func->arange;
1420 arange;
1421 arange = arange->next)
1423 if (addr >= arange->low && addr < arange->high)
1425 if (!best_fit ||
1426 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1427 best_fit = each_func;
1432 if (best_fit)
1434 struct funcinfo* curr_func = best_fit;
1436 *functionname_ptr = best_fit->name;
1437 *function_ptr = best_fit;
1439 /* If we found a match and it is a function that was inlined,
1440 traverse the function list looking for the function at the
1441 next higher scope and save a pointer to it for future use.
1442 Note that because of the way the DWARF info is generated, and
1443 the way we build the function list, the first function at the
1444 next higher level is the one we want. */
1446 for (each_func = best_fit -> prev_func;
1447 each_func && (curr_func->tag == DW_TAG_inlined_subroutine);
1448 each_func = each_func->prev_func)
1450 if (each_func->nesting_level < curr_func->nesting_level)
1452 curr_func->caller_func = each_func;
1453 curr_func = each_func;
1456 return TRUE;
1458 else
1460 return FALSE;
1464 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1465 and LINENUMBER_PTR, and return TRUE. */
1467 static bfd_boolean
1468 lookup_symbol_in_function_table (struct comp_unit *unit,
1469 asymbol *sym,
1470 bfd_vma addr,
1471 const char **filename_ptr,
1472 unsigned int *linenumber_ptr)
1474 struct funcinfo* each_func;
1475 struct funcinfo* best_fit = NULL;
1476 struct arange *arange;
1477 const char *name = bfd_asymbol_name (sym);
1478 asection *sec = bfd_get_section (sym);
1480 for (each_func = unit->function_table;
1481 each_func;
1482 each_func = each_func->prev_func)
1484 for (arange = &each_func->arange;
1485 arange;
1486 arange = arange->next)
1488 if ((!each_func->sec || each_func->sec == sec)
1489 && addr >= arange->low
1490 && addr < arange->high
1491 && strcmp (name, each_func->name) == 0
1492 && (!best_fit
1493 || ((arange->high - arange->low)
1494 < (best_fit->arange.high - best_fit->arange.low))))
1495 best_fit = each_func;
1499 if (best_fit)
1501 best_fit->sec = sec;
1502 *filename_ptr = best_fit->file;
1503 *linenumber_ptr = best_fit->line;
1504 return TRUE;
1506 else
1507 return FALSE;
1510 /* Variable table functions. */
1512 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1513 LINENUMBER_PTR, and return TRUE. */
1515 static bfd_boolean
1516 lookup_symbol_in_variable_table (struct comp_unit *unit,
1517 asymbol *sym,
1518 bfd_vma addr,
1519 const char **filename_ptr,
1520 unsigned int *linenumber_ptr)
1522 const char *name = bfd_asymbol_name (sym);
1523 asection *sec = bfd_get_section (sym);
1524 struct varinfo* each;
1526 for (each = unit->variable_table; each; each = each->prev_var)
1527 if (each->stack == 0
1528 && each->file != NULL
1529 && each->name != NULL
1530 && each->addr == addr
1531 && (!each->sec || each->sec == sec)
1532 && strcmp (name, each->name) == 0)
1533 break;
1535 if (each)
1537 each->sec = sec;
1538 *filename_ptr = each->file;
1539 *linenumber_ptr = each->line;
1540 return TRUE;
1542 else
1543 return FALSE;
1546 static char *
1547 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1549 bfd *abfd = unit->abfd;
1550 bfd_byte *info_ptr;
1551 unsigned int abbrev_number, bytes_read, i;
1552 struct abbrev_info *abbrev;
1553 struct attribute attr;
1554 char *name = 0;
1556 info_ptr = unit->info_ptr_unit + die_ref;
1557 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1558 info_ptr += bytes_read;
1560 if (abbrev_number)
1562 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1563 if (! abbrev)
1565 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1566 abbrev_number);
1567 bfd_set_error (bfd_error_bad_value);
1569 else
1571 for (i = 0; i < abbrev->num_attrs && !name; ++i)
1573 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1574 switch (attr.name)
1576 case DW_AT_name:
1577 name = attr.u.str;
1578 break;
1579 case DW_AT_specification:
1580 name = find_abstract_instance_name (unit, attr.u.val);
1581 break;
1582 default:
1583 break;
1588 return (name);
1591 static void
1592 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1594 bfd_byte *ranges_ptr;
1595 bfd_vma base_address = unit->base_address;
1597 if (! unit->stash->dwarf_ranges_buffer)
1599 if (! read_debug_ranges (unit))
1600 return;
1602 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1604 for (;;)
1606 bfd_vma low_pc;
1607 bfd_vma high_pc;
1609 if (unit->offset_size == 4)
1611 low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1612 ranges_ptr += 4;
1613 high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1614 ranges_ptr += 4;
1616 else
1618 low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1619 ranges_ptr += 8;
1620 high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1621 ranges_ptr += 8;
1623 if (low_pc == 0 && high_pc == 0)
1624 break;
1625 if (low_pc == -1UL && high_pc != -1UL)
1626 base_address = high_pc;
1627 else
1628 arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1632 /* DWARF2 Compilation unit functions. */
1634 /* Scan over each die in a comp. unit looking for functions to add
1635 to the function table and variables to the variable table. */
1637 static bfd_boolean
1638 scan_unit_for_symbols (struct comp_unit *unit)
1640 bfd *abfd = unit->abfd;
1641 bfd_byte *info_ptr = unit->first_child_die_ptr;
1642 int nesting_level = 1;
1644 while (nesting_level)
1646 unsigned int abbrev_number, bytes_read, i;
1647 struct abbrev_info *abbrev;
1648 struct attribute attr;
1649 struct funcinfo *func;
1650 struct varinfo *var;
1651 bfd_vma low_pc = 0;
1652 bfd_vma high_pc = 0;
1654 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1655 info_ptr += bytes_read;
1657 if (! abbrev_number)
1659 nesting_level--;
1660 continue;
1663 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1664 if (! abbrev)
1666 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1667 abbrev_number);
1668 bfd_set_error (bfd_error_bad_value);
1669 return FALSE;
1672 var = NULL;
1673 if (abbrev->tag == DW_TAG_subprogram
1674 || abbrev->tag == DW_TAG_entry_point
1675 || abbrev->tag == DW_TAG_inlined_subroutine)
1677 bfd_size_type amt = sizeof (struct funcinfo);
1678 func = bfd_zalloc (abfd, amt);
1679 func->tag = abbrev->tag;
1680 func->nesting_level = nesting_level;
1681 func->prev_func = unit->function_table;
1682 unit->function_table = func;
1684 else
1686 func = NULL;
1687 if (abbrev->tag == DW_TAG_variable)
1689 bfd_size_type amt = sizeof (struct varinfo);
1690 var = bfd_zalloc (abfd, amt);
1691 var->tag = abbrev->tag;
1692 var->stack = 1;
1693 var->prev_var = unit->variable_table;
1694 unit->variable_table = var;
1698 for (i = 0; i < abbrev->num_attrs; ++i)
1700 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1702 if (func)
1704 switch (attr.name)
1706 case DW_AT_call_file:
1707 func->caller_file = concat_filename (unit->line_table, attr.u.val);
1708 break;
1710 case DW_AT_call_line:
1711 func->caller_line = attr.u.val;
1712 break;
1714 case DW_AT_abstract_origin:
1715 func->name = find_abstract_instance_name (unit, attr.u.val);
1716 break;
1718 case DW_AT_name:
1719 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1720 if (func->name == NULL)
1721 func->name = attr.u.str;
1722 break;
1724 case DW_AT_MIPS_linkage_name:
1725 func->name = attr.u.str;
1726 break;
1728 case DW_AT_low_pc:
1729 low_pc = attr.u.val;
1730 break;
1732 case DW_AT_high_pc:
1733 high_pc = attr.u.val;
1734 break;
1736 case DW_AT_ranges:
1737 read_rangelist (unit, &func->arange, attr.u.val);
1738 break;
1740 case DW_AT_decl_file:
1741 func->file = concat_filename (unit->line_table,
1742 attr.u.val);
1743 break;
1745 case DW_AT_decl_line:
1746 func->line = attr.u.val;
1747 break;
1749 default:
1750 break;
1753 else if (var)
1755 switch (attr.name)
1757 case DW_AT_name:
1758 var->name = attr.u.str;
1759 break;
1761 case DW_AT_decl_file:
1762 var->file = concat_filename (unit->line_table,
1763 attr.u.val);
1764 break;
1766 case DW_AT_decl_line:
1767 var->line = attr.u.val;
1768 break;
1770 case DW_AT_external:
1771 if (attr.u.val != 0)
1772 var->stack = 0;
1773 break;
1775 case DW_AT_location:
1776 switch (attr.form)
1778 case DW_FORM_block:
1779 case DW_FORM_block1:
1780 case DW_FORM_block2:
1781 case DW_FORM_block4:
1782 if (*attr.u.blk->data == DW_OP_addr)
1784 var->stack = 0;
1785 var->addr = bfd_get ((attr.u.blk->size - 1) * 8,
1786 unit->abfd,
1787 attr.u.blk->data + 1);
1789 break;
1791 default:
1792 break;
1794 break;
1796 default:
1797 break;
1802 if (func && high_pc != 0)
1804 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
1807 if (abbrev->has_children)
1808 nesting_level++;
1811 return TRUE;
1814 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1815 includes the compilation unit header that proceeds the DIE's, but
1816 does not include the length field that precedes each compilation
1817 unit header. END_PTR points one past the end of this comp unit.
1818 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1820 This routine does not read the whole compilation unit; only enough
1821 to get to the line number information for the compilation unit. */
1823 static struct comp_unit *
1824 parse_comp_unit (bfd *abfd,
1825 struct dwarf2_debug *stash,
1826 bfd_vma unit_length,
1827 bfd_byte *info_ptr_unit,
1828 unsigned int offset_size)
1830 struct comp_unit* unit;
1831 unsigned int version;
1832 bfd_uint64_t abbrev_offset = 0;
1833 unsigned int addr_size;
1834 struct abbrev_info** abbrevs;
1835 unsigned int abbrev_number, bytes_read, i;
1836 struct abbrev_info *abbrev;
1837 struct attribute attr;
1838 bfd_byte *info_ptr = stash->info_ptr;
1839 bfd_byte *end_ptr = info_ptr + unit_length;
1840 bfd_size_type amt;
1841 bfd_vma low_pc = 0;
1842 bfd_vma high_pc = 0;
1844 version = read_2_bytes (abfd, info_ptr);
1845 info_ptr += 2;
1846 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1847 if (offset_size == 4)
1848 abbrev_offset = read_4_bytes (abfd, info_ptr);
1849 else
1850 abbrev_offset = read_8_bytes (abfd, info_ptr);
1851 info_ptr += offset_size;
1852 addr_size = read_1_byte (abfd, info_ptr);
1853 info_ptr += 1;
1855 if (version != 2)
1857 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1858 bfd_set_error (bfd_error_bad_value);
1859 return 0;
1862 if (addr_size > sizeof (bfd_vma))
1864 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1865 addr_size,
1866 (unsigned int) sizeof (bfd_vma));
1867 bfd_set_error (bfd_error_bad_value);
1868 return 0;
1871 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1873 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1874 bfd_set_error (bfd_error_bad_value);
1875 return 0;
1878 /* Read the abbrevs for this compilation unit into a table. */
1879 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1880 if (! abbrevs)
1881 return 0;
1883 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1884 info_ptr += bytes_read;
1885 if (! abbrev_number)
1887 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1888 abbrev_number);
1889 bfd_set_error (bfd_error_bad_value);
1890 return 0;
1893 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1894 if (! abbrev)
1896 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1897 abbrev_number);
1898 bfd_set_error (bfd_error_bad_value);
1899 return 0;
1902 amt = sizeof (struct comp_unit);
1903 unit = bfd_zalloc (abfd, amt);
1904 unit->abfd = abfd;
1905 unit->addr_size = addr_size;
1906 unit->offset_size = offset_size;
1907 unit->abbrevs = abbrevs;
1908 unit->end_ptr = end_ptr;
1909 unit->stash = stash;
1910 unit->info_ptr_unit = info_ptr_unit;
1912 for (i = 0; i < abbrev->num_attrs; ++i)
1914 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1916 /* Store the data if it is of an attribute we want to keep in a
1917 partial symbol table. */
1918 switch (attr.name)
1920 case DW_AT_stmt_list:
1921 unit->stmtlist = 1;
1922 unit->line_offset = attr.u.val;
1923 break;
1925 case DW_AT_name:
1926 unit->name = attr.u.str;
1927 break;
1929 case DW_AT_low_pc:
1930 low_pc = attr.u.val;
1931 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
1932 this is the base address to use when reading location
1933 lists or range lists. */
1934 unit->base_address = low_pc;
1935 break;
1937 case DW_AT_high_pc:
1938 high_pc = attr.u.val;
1939 break;
1941 case DW_AT_ranges:
1942 read_rangelist (unit, &unit->arange, attr.u.val);
1943 break;
1945 case DW_AT_comp_dir:
1947 char *comp_dir = attr.u.str;
1948 if (comp_dir)
1950 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1951 directory, get rid of it. */
1952 char *cp = strchr (comp_dir, ':');
1954 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1955 comp_dir = cp + 1;
1957 unit->comp_dir = comp_dir;
1958 break;
1961 default:
1962 break;
1965 if (high_pc != 0)
1967 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1970 unit->first_child_die_ptr = info_ptr;
1971 return unit;
1974 /* Return TRUE if UNIT contains the address given by ADDR. */
1976 static bfd_boolean
1977 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
1979 struct arange *arange;
1981 if (unit->error)
1982 return FALSE;
1984 arange = &unit->arange;
1987 if (addr >= arange->low && addr < arange->high)
1988 return TRUE;
1989 arange = arange->next;
1991 while (arange);
1993 return FALSE;
1996 /* If UNIT contains ADDR, set the output parameters to the values for
1997 the line containing ADDR. The output parameters, FILENAME_PTR,
1998 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1999 to be filled in.
2001 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2002 FALSE otherwise. */
2004 static bfd_boolean
2005 comp_unit_find_nearest_line (struct comp_unit *unit,
2006 bfd_vma addr,
2007 const char **filename_ptr,
2008 const char **functionname_ptr,
2009 unsigned int *linenumber_ptr,
2010 struct dwarf2_debug *stash)
2012 bfd_boolean line_p;
2013 bfd_boolean func_p;
2014 struct funcinfo *function;
2016 if (unit->error)
2017 return FALSE;
2019 if (! unit->line_table)
2021 if (! unit->stmtlist)
2023 unit->error = 1;
2024 return FALSE;
2027 unit->line_table = decode_line_info (unit, stash);
2029 if (! unit->line_table)
2031 unit->error = 1;
2032 return FALSE;
2035 if (unit->first_child_die_ptr < unit->end_ptr
2036 && ! scan_unit_for_symbols (unit))
2038 unit->error = 1;
2039 return FALSE;
2043 function = NULL;
2044 func_p = lookup_address_in_function_table (unit, addr,
2045 &function, functionname_ptr);
2046 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2047 stash->inliner_chain = function;
2048 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2049 function, filename_ptr,
2050 linenumber_ptr);
2051 return line_p || func_p;
2054 /* If UNIT contains SYM at ADDR, set the output parameters to the
2055 values for the line containing SYM. The output parameters,
2056 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2057 filled in.
2059 Return TRUE if UNIT contains SYM, and no errors were encountered;
2060 FALSE otherwise. */
2062 static bfd_boolean
2063 comp_unit_find_line (struct comp_unit *unit,
2064 asymbol *sym,
2065 bfd_vma addr,
2066 const char **filename_ptr,
2067 unsigned int *linenumber_ptr,
2068 struct dwarf2_debug *stash)
2070 if (unit->error)
2071 return FALSE;
2073 if (! unit->line_table)
2075 if (! unit->stmtlist)
2077 unit->error = 1;
2078 return FALSE;
2081 unit->line_table = decode_line_info (unit, stash);
2083 if (! unit->line_table)
2085 unit->error = 1;
2086 return FALSE;
2089 if (unit->first_child_die_ptr < unit->end_ptr
2090 && ! scan_unit_for_symbols (unit))
2092 unit->error = 1;
2093 return FALSE;
2097 if (sym->flags & BSF_FUNCTION)
2098 return lookup_symbol_in_function_table (unit, sym, addr,
2099 filename_ptr,
2100 linenumber_ptr);
2101 else
2102 return lookup_symbol_in_variable_table (unit, sym, addr,
2103 filename_ptr,
2104 linenumber_ptr);
2107 /* Locate a section in a BFD containing debugging info. The search starts
2108 from the section after AFTER_SEC, or from the first section in the BFD if
2109 AFTER_SEC is NULL. The search works by examining the names of the
2110 sections. There are two permissiable names. The first is .debug_info.
2111 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2112 This is a variation on the .debug_info section which has a checksum
2113 describing the contents appended onto the name. This allows the linker to
2114 identify and discard duplicate debugging sections for different
2115 compilation units. */
2116 #define DWARF2_DEBUG_INFO ".debug_info"
2117 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2119 static asection *
2120 find_debug_info (bfd *abfd, asection *after_sec)
2122 asection * msec;
2124 if (after_sec)
2125 msec = after_sec->next;
2126 else
2127 msec = abfd->sections;
2129 while (msec)
2131 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2132 return msec;
2134 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
2135 return msec;
2137 msec = msec->next;
2140 return NULL;
2143 /* The DWARF2 version of find_nearest_line. Return TRUE if the line
2144 is found without error. ADDR_SIZE is the number of bytes in the
2145 initial .debug_info length field and in the abbreviation offset.
2146 You may use zero to indicate that the default value should be
2147 used. */
2149 bfd_boolean
2150 _bfd_dwarf2_find_nearest_line (bfd *abfd,
2151 asection *section,
2152 asymbol **symbols,
2153 bfd_vma offset,
2154 const char **filename_ptr,
2155 const char **functionname_ptr,
2156 unsigned int *linenumber_ptr,
2157 unsigned int addr_size,
2158 void **pinfo)
2160 /* Read each compilation unit from the section .debug_info, and check
2161 to see if it contains the address we are searching for. If yes,
2162 lookup the address, and return the line number info. If no, go
2163 on to the next compilation unit.
2165 We keep a list of all the previously read compilation units, and
2166 a pointer to the next un-read compilation unit. Check the
2167 previously read units before reading more. */
2168 struct dwarf2_debug *stash;
2170 /* What address are we looking for? */
2171 bfd_vma addr;
2173 struct comp_unit* each;
2175 stash = *pinfo;
2176 addr = offset;
2177 if (section->output_section)
2178 addr += section->output_section->vma + section->output_offset;
2179 else
2180 addr += section->vma;
2181 *filename_ptr = NULL;
2182 *functionname_ptr = NULL;
2183 *linenumber_ptr = 0;
2185 /* The DWARF2 spec says that the initial length field, and the
2186 offset of the abbreviation table, should both be 4-byte values.
2187 However, some compilers do things differently. */
2188 if (addr_size == 0)
2189 addr_size = 4;
2190 BFD_ASSERT (addr_size == 4 || addr_size == 8);
2192 if (! stash)
2194 bfd_size_type total_size;
2195 asection *msec;
2196 bfd_size_type amt = sizeof (struct dwarf2_debug);
2198 stash = bfd_zalloc (abfd, amt);
2199 if (! stash)
2200 return FALSE;
2202 *pinfo = stash;
2204 msec = find_debug_info (abfd, NULL);
2205 if (! msec)
2206 /* No dwarf2 info. Note that at this point the stash
2207 has been allocated, but contains zeros, this lets
2208 future calls to this function fail quicker. */
2209 return FALSE;
2211 /* There can be more than one DWARF2 info section in a BFD these days.
2212 Read them all in and produce one large stash. We do this in two
2213 passes - in the first pass we just accumulate the section sizes.
2214 In the second pass we read in the section's contents. The allows
2215 us to avoid reallocing the data as we add sections to the stash. */
2216 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
2217 total_size += msec->size;
2219 stash->info_ptr = bfd_alloc (abfd, total_size);
2220 if (stash->info_ptr == NULL)
2221 return FALSE;
2223 stash->info_ptr_end = stash->info_ptr;
2225 for (msec = find_debug_info (abfd, NULL);
2226 msec;
2227 msec = find_debug_info (abfd, msec))
2229 bfd_size_type size;
2230 bfd_size_type start;
2232 size = msec->size;
2233 if (size == 0)
2234 continue;
2236 start = stash->info_ptr_end - stash->info_ptr;
2238 if ((bfd_simple_get_relocated_section_contents
2239 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
2240 continue;
2242 stash->info_ptr_end = stash->info_ptr + start + size;
2245 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2247 stash->sec = find_debug_info (abfd, NULL);
2248 stash->sec_info_ptr = stash->info_ptr;
2249 stash->syms = symbols;
2252 /* A null info_ptr indicates that there is no dwarf2 info
2253 (or that an error occured while setting up the stash). */
2254 if (! stash->info_ptr)
2255 return FALSE;
2257 stash->inliner_chain = NULL;
2259 /* Check the previously read comp. units first. */
2260 for (each = stash->all_comp_units; each; each = each->next_unit)
2261 if (comp_unit_contains_address (each, addr))
2262 return comp_unit_find_nearest_line (each, addr, filename_ptr,
2263 functionname_ptr, linenumber_ptr,
2264 stash);
2266 /* Read each remaining comp. units checking each as they are read. */
2267 while (stash->info_ptr < stash->info_ptr_end)
2269 bfd_vma length;
2270 bfd_boolean found;
2271 unsigned int offset_size = addr_size;
2272 bfd_byte *info_ptr_unit = stash->info_ptr;
2274 length = read_4_bytes (abfd, stash->info_ptr);
2275 /* A 0xffffff length is the DWARF3 way of indicating we use
2276 64-bit offsets, instead of 32-bit offsets. */
2277 if (length == 0xffffffff)
2279 offset_size = 8;
2280 length = read_8_bytes (abfd, stash->info_ptr + 4);
2281 stash->info_ptr += 12;
2283 /* A zero length is the IRIX way of indicating 64-bit offsets,
2284 mostly because the 64-bit length will generally fit in 32
2285 bits, and the endianness helps. */
2286 else if (length == 0)
2288 offset_size = 8;
2289 length = read_4_bytes (abfd, stash->info_ptr + 4);
2290 stash->info_ptr += 8;
2292 /* In the absence of the hints above, we assume addr_size-sized
2293 offsets, for backward-compatibility with pre-DWARF3 64-bit
2294 platforms. */
2295 else if (addr_size == 8)
2297 length = read_8_bytes (abfd, stash->info_ptr);
2298 stash->info_ptr += 8;
2300 else
2301 stash->info_ptr += 4;
2303 if (length > 0)
2305 each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
2306 offset_size);
2307 stash->info_ptr += length;
2309 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
2310 == stash->sec->size)
2312 stash->sec = find_debug_info (abfd, stash->sec);
2313 stash->sec_info_ptr = stash->info_ptr;
2316 if (each)
2318 each->next_unit = stash->all_comp_units;
2319 stash->all_comp_units = each;
2321 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2322 compilation units. If we don't have them (i.e.,
2323 unit->high == 0), we need to consult the line info
2324 table to see if a compilation unit contains the given
2325 address. */
2326 if (each->arange.high > 0)
2328 if (comp_unit_contains_address (each, addr))
2329 return comp_unit_find_nearest_line (each, addr,
2330 filename_ptr,
2331 functionname_ptr,
2332 linenumber_ptr,
2333 stash);
2335 else
2337 found = comp_unit_find_nearest_line (each, addr,
2338 filename_ptr,
2339 functionname_ptr,
2340 linenumber_ptr,
2341 stash);
2342 if (found)
2343 return TRUE;
2349 return FALSE;
2352 /* The DWARF2 version of find_line. Return TRUE if the line is found
2353 without error. */
2355 bfd_boolean
2356 _bfd_dwarf2_find_line (bfd *abfd,
2357 asymbol **symbols,
2358 asymbol *symbol,
2359 const char **filename_ptr,
2360 unsigned int *linenumber_ptr,
2361 unsigned int addr_size,
2362 void **pinfo)
2364 /* Read each compilation unit from the section .debug_info, and check
2365 to see if it contains the address we are searching for. If yes,
2366 lookup the address, and return the line number info. If no, go
2367 on to the next compilation unit.
2369 We keep a list of all the previously read compilation units, and
2370 a pointer to the next un-read compilation unit. Check the
2371 previously read units before reading more. */
2372 struct dwarf2_debug *stash;
2374 /* What address are we looking for? */
2375 bfd_vma addr;
2377 struct comp_unit* each;
2379 asection *section;
2381 bfd_boolean found;
2383 section = bfd_get_section (symbol);
2385 addr = symbol->value;
2386 if (section->output_section)
2387 addr += section->output_section->vma + section->output_offset;
2388 else
2389 addr += section->vma;
2391 *filename_ptr = NULL;
2392 stash = *pinfo;
2393 *filename_ptr = NULL;
2394 *linenumber_ptr = 0;
2396 if (! stash)
2398 bfd_size_type total_size;
2399 asection *msec;
2400 bfd_size_type amt = sizeof (struct dwarf2_debug);
2402 stash = bfd_zalloc (abfd, amt);
2403 if (! stash)
2404 return FALSE;
2406 *pinfo = stash;
2408 msec = find_debug_info (abfd, NULL);
2409 if (! msec)
2410 /* No dwarf2 info. Note that at this point the stash
2411 has been allocated, but contains zeros, this lets
2412 future calls to this function fail quicker. */
2413 return FALSE;
2415 /* There can be more than one DWARF2 info section in a BFD these days.
2416 Read them all in and produce one large stash. We do this in two
2417 passes - in the first pass we just accumulate the section sizes.
2418 In the second pass we read in the section's contents. The allows
2419 us to avoid reallocing the data as we add sections to the stash. */
2420 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
2421 total_size += msec->size;
2423 stash->info_ptr = bfd_alloc (abfd, total_size);
2424 if (stash->info_ptr == NULL)
2425 return FALSE;
2427 stash->info_ptr_end = stash->info_ptr;
2429 for (msec = find_debug_info (abfd, NULL);
2430 msec;
2431 msec = find_debug_info (abfd, msec))
2433 bfd_size_type size;
2434 bfd_size_type start;
2436 size = msec->size;
2437 if (size == 0)
2438 continue;
2440 start = stash->info_ptr_end - stash->info_ptr;
2442 if ((bfd_simple_get_relocated_section_contents
2443 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
2444 continue;
2446 stash->info_ptr_end = stash->info_ptr + start + size;
2449 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2451 stash->sec = find_debug_info (abfd, NULL);
2452 stash->sec_info_ptr = stash->info_ptr;
2453 stash->syms = symbols;
2456 /* A null info_ptr indicates that there is no dwarf2 info
2457 (or that an error occured while setting up the stash). */
2458 if (! stash->info_ptr)
2459 return FALSE;
2461 stash->inliner_chain = NULL;
2463 /* Check the previously read comp. units first. */
2464 for (each = stash->all_comp_units; each; each = each->next_unit)
2465 if ((symbol->flags & BSF_FUNCTION) == 0
2466 || comp_unit_contains_address (each, addr))
2468 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
2469 linenumber_ptr, stash);
2470 if (found)
2471 return found;
2474 /* The DWARF2 spec says that the initial length field, and the
2475 offset of the abbreviation table, should both be 4-byte values.
2476 However, some compilers do things differently. */
2477 if (addr_size == 0)
2478 addr_size = 4;
2479 BFD_ASSERT (addr_size == 4 || addr_size == 8);
2481 /* Read each remaining comp. units checking each as they are read. */
2482 while (stash->info_ptr < stash->info_ptr_end)
2484 bfd_vma length;
2485 unsigned int offset_size = addr_size;
2486 bfd_byte *info_ptr_unit = stash->info_ptr;
2488 length = read_4_bytes (abfd, stash->info_ptr);
2489 /* A 0xffffff length is the DWARF3 way of indicating we use
2490 64-bit offsets, instead of 32-bit offsets. */
2491 if (length == 0xffffffff)
2493 offset_size = 8;
2494 length = read_8_bytes (abfd, stash->info_ptr + 4);
2495 stash->info_ptr += 12;
2497 /* A zero length is the IRIX way of indicating 64-bit offsets,
2498 mostly because the 64-bit length will generally fit in 32
2499 bits, and the endianness helps. */
2500 else if (length == 0)
2502 offset_size = 8;
2503 length = read_4_bytes (abfd, stash->info_ptr + 4);
2504 stash->info_ptr += 8;
2506 /* In the absence of the hints above, we assume addr_size-sized
2507 offsets, for backward-compatibility with pre-DWARF3 64-bit
2508 platforms. */
2509 else if (addr_size == 8)
2511 length = read_8_bytes (abfd, stash->info_ptr);
2512 stash->info_ptr += 8;
2514 else
2515 stash->info_ptr += 4;
2517 if (length > 0)
2519 each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
2520 offset_size);
2521 stash->info_ptr += length;
2523 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
2524 == stash->sec->size)
2526 stash->sec = find_debug_info (abfd, stash->sec);
2527 stash->sec_info_ptr = stash->info_ptr;
2530 if (each)
2532 each->next_unit = stash->all_comp_units;
2533 stash->all_comp_units = each;
2535 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2536 compilation units. If we don't have them (i.e.,
2537 unit->high == 0), we need to consult the line info
2538 table to see if a compilation unit contains the given
2539 address. */
2540 found = (((symbol->flags & BSF_FUNCTION) == 0
2541 || each->arange.high <= 0
2542 || comp_unit_contains_address (each, addr))
2543 && comp_unit_find_line (each, symbol, addr,
2544 filename_ptr,
2545 linenumber_ptr,
2546 stash));
2547 if (found)
2548 return TRUE;
2553 return FALSE;
2556 bfd_boolean
2557 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
2558 const char **filename_ptr,
2559 const char **functionname_ptr,
2560 unsigned int *linenumber_ptr,
2561 void **pinfo)
2563 struct dwarf2_debug *stash;
2565 stash = *pinfo;
2566 if (stash)
2568 struct funcinfo *func = stash->inliner_chain;
2569 if (func && func->caller_func)
2571 *filename_ptr = func->caller_file;
2572 *functionname_ptr = func->caller_func->name;
2573 *linenumber_ptr = func->caller_line;
2574 stash->inliner_chain = func->caller_func;
2575 return (TRUE);
2579 return (FALSE);
2582 void
2583 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
2585 struct comp_unit *each;
2586 struct dwarf2_debug *stash;
2588 if (abfd == NULL || elf_tdata (abfd) == NULL)
2589 return;
2591 stash = elf_tdata (abfd)->dwarf2_find_line_info;
2593 if (stash == NULL)
2594 return;
2596 for (each = stash->all_comp_units; each; each = each->next_unit)
2598 struct abbrev_info **abbrevs = each->abbrevs;
2599 size_t i;
2601 for (i = 0; i < ABBREV_HASH_SIZE; i++)
2603 struct abbrev_info *abbrev = abbrevs[i];
2605 while (abbrev)
2607 free (abbrev->attrs);
2608 abbrev = abbrev->next;
2612 if (each->line_table)
2614 free (each->line_table->dirs);
2615 free (each->line_table->files);
2619 free (stash->dwarf_abbrev_buffer);
2620 free (stash->dwarf_line_buffer);
2621 free (stash->dwarf_ranges_buffer);