daily update
[binutils.git] / bfd / dwarf2.c
blob5cfcc908e8dccb2ff709e52e1c8935fc1f54dca0
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 char *name;
726 struct arange arange;
727 asection *sec; /* Where the symbol is defined */
730 struct varinfo
732 /* Pointer to previous variable in list of all variables */
733 struct varinfo *prev_var;
734 /* Source location file name */
735 char *file;
736 /* Source location line number */
737 int line;
738 int tag;
739 char *name;
740 bfd_vma addr;
741 /* Where the symbol is defined */
742 asection *sec;
743 /* Is this a stack variable? */
744 unsigned int stack: 1;
747 /* Adds a new entry to the line_info list in the line_info_table, ensuring
748 that the list is sorted. Note that the line_info list is sorted from
749 highest to lowest VMA (with possible duplicates); that is,
750 line_info->prev_line always accesses an equal or smaller VMA. */
752 static void
753 add_line_info (struct line_info_table *table,
754 bfd_vma address,
755 char *filename,
756 unsigned int line,
757 unsigned int column,
758 int end_sequence)
760 bfd_size_type amt = sizeof (struct line_info);
761 struct line_info* info = bfd_alloc (table->abfd, amt);
763 /* Find the correct location for 'info'. Normally we will receive
764 new line_info data 1) in order and 2) with increasing VMAs.
765 However some compilers break the rules (cf. decode_line_info) and
766 so we include some heuristics for quickly finding the correct
767 location for 'info'. In particular, these heuristics optimize for
768 the common case in which the VMA sequence that we receive is a
769 list of locally sorted VMAs such as
770 p...z a...j (where a < j < p < z)
772 Note: table->lcl_head is used to head an *actual* or *possible*
773 sequence within the list (such as a...j) that is not directly
774 headed by table->last_line
776 Note: we may receive duplicate entries from 'decode_line_info'. */
778 while (1)
779 if (!table->last_line
780 || address >= table->last_line->address)
782 /* Normal case: add 'info' to the beginning of the list */
783 info->prev_line = table->last_line;
784 table->last_line = info;
786 /* lcl_head: initialize to head a *possible* sequence at the end. */
787 if (!table->lcl_head)
788 table->lcl_head = info;
789 break;
791 else if (!table->lcl_head->prev_line
792 && table->lcl_head->address > address)
794 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
795 list and 2) the head of 'info'. */
796 info->prev_line = NULL;
797 table->lcl_head->prev_line = info;
798 break;
800 else if (table->lcl_head->prev_line
801 && table->lcl_head->address > address
802 && address >= table->lcl_head->prev_line->address)
804 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
805 list and 2) the head of 'info'. */
806 info->prev_line = table->lcl_head->prev_line;
807 table->lcl_head->prev_line = info;
808 break;
810 else
812 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
813 heads for 'info'. Reset 'lcl_head' and repeat. */
814 struct line_info* li2 = table->last_line; /* always non-NULL */
815 struct line_info* li1 = li2->prev_line;
817 while (li1)
819 if (li2->address > address && address >= li1->address)
820 break;
822 li2 = li1; /* always non-NULL */
823 li1 = li1->prev_line;
825 table->lcl_head = li2;
828 /* Set member data of 'info'. */
829 info->address = address;
830 info->line = line;
831 info->column = column;
832 info->end_sequence = end_sequence;
834 if (filename && filename[0])
836 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
837 if (info->filename)
838 strcpy (info->filename, filename);
840 else
841 info->filename = NULL;
844 /* Extract a fully qualified filename from a line info table.
845 The returned string has been malloc'ed and it is the caller's
846 responsibility to free it. */
848 static char *
849 concat_filename (struct line_info_table *table, unsigned int file)
851 char *filename;
853 if (file - 1 >= table->num_files)
855 (*_bfd_error_handler)
856 (_("Dwarf Error: mangled line number section (bad file number)."));
857 return strdup ("<unknown>");
860 filename = table->files[file - 1].name;
862 if (! IS_ABSOLUTE_PATH (filename))
864 char *dirname = (table->files[file - 1].dir
865 ? table->dirs[table->files[file - 1].dir - 1]
866 : table->comp_dir);
868 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
869 The best we can do is return the filename part. */
870 if (dirname != NULL)
872 unsigned int len = strlen (dirname) + strlen (filename) + 2;
873 char * name;
875 name = bfd_malloc (len);
876 if (name)
877 sprintf (name, "%s/%s", dirname, filename);
878 return name;
882 return strdup (filename);
885 static void
886 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
888 struct arange *arange;
890 /* If the first arange is empty, use it. */
891 if (first_arange->high == 0)
893 first_arange->low = low_pc;
894 first_arange->high = high_pc;
895 return;
898 /* Next see if we can cheaply extend an existing range. */
899 arange = first_arange;
902 if (low_pc == arange->high)
904 arange->high = high_pc;
905 return;
907 if (high_pc == arange->low)
909 arange->low = low_pc;
910 return;
912 arange = arange->next;
914 while (arange);
916 /* Need to allocate a new arange and insert it into the arange list.
917 Order isn't significant, so just insert after the first arange. */
918 arange = bfd_zalloc (abfd, sizeof (*arange));
919 arange->low = low_pc;
920 arange->high = high_pc;
921 arange->next = first_arange->next;
922 first_arange->next = arange;
925 /* Decode the line number information for UNIT. */
927 static struct line_info_table*
928 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
930 bfd *abfd = unit->abfd;
931 struct line_info_table* table;
932 bfd_byte *line_ptr;
933 bfd_byte *line_end;
934 struct line_head lh;
935 unsigned int i, bytes_read, offset_size;
936 char *cur_file, *cur_dir;
937 unsigned char op_code, extended_op, adj_opcode;
938 bfd_size_type amt;
940 if (! stash->dwarf_line_buffer)
942 asection *msec;
944 msec = bfd_get_section_by_name (abfd, ".debug_line");
945 if (! msec)
947 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
948 bfd_set_error (bfd_error_bad_value);
949 return 0;
952 stash->dwarf_line_size = msec->size;
953 stash->dwarf_line_buffer
954 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
955 stash->syms);
956 if (! stash->dwarf_line_buffer)
957 return 0;
960 /* It is possible to get a bad value for the line_offset. Validate
961 it here so that we won't get a segfault below. */
962 if (unit->line_offset >= stash->dwarf_line_size)
964 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
965 unit->line_offset, stash->dwarf_line_size);
966 bfd_set_error (bfd_error_bad_value);
967 return 0;
970 amt = sizeof (struct line_info_table);
971 table = bfd_alloc (abfd, amt);
972 table->abfd = abfd;
973 table->comp_dir = unit->comp_dir;
975 table->num_files = 0;
976 table->files = NULL;
978 table->num_dirs = 0;
979 table->dirs = NULL;
981 table->files = NULL;
982 table->last_line = NULL;
983 table->lcl_head = NULL;
985 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
987 /* Read in the prologue. */
988 lh.total_length = read_4_bytes (abfd, line_ptr);
989 line_ptr += 4;
990 offset_size = 4;
991 if (lh.total_length == 0xffffffff)
993 lh.total_length = read_8_bytes (abfd, line_ptr);
994 line_ptr += 8;
995 offset_size = 8;
997 else if (lh.total_length == 0 && unit->addr_size == 8)
999 /* Handle (non-standard) 64-bit DWARF2 formats. */
1000 lh.total_length = read_4_bytes (abfd, line_ptr);
1001 line_ptr += 4;
1002 offset_size = 8;
1004 line_end = line_ptr + lh.total_length;
1005 lh.version = read_2_bytes (abfd, line_ptr);
1006 line_ptr += 2;
1007 if (offset_size == 4)
1008 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1009 else
1010 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1011 line_ptr += offset_size;
1012 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1013 line_ptr += 1;
1014 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1015 line_ptr += 1;
1016 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1017 line_ptr += 1;
1018 lh.line_range = read_1_byte (abfd, line_ptr);
1019 line_ptr += 1;
1020 lh.opcode_base = read_1_byte (abfd, line_ptr);
1021 line_ptr += 1;
1022 amt = lh.opcode_base * sizeof (unsigned char);
1023 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
1025 lh.standard_opcode_lengths[0] = 1;
1027 for (i = 1; i < lh.opcode_base; ++i)
1029 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1030 line_ptr += 1;
1033 /* Read directory table. */
1034 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1036 line_ptr += bytes_read;
1038 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1040 char **tmp;
1042 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1043 amt *= sizeof (char *);
1045 tmp = bfd_realloc (table->dirs, amt);
1046 if (tmp == NULL)
1048 free (table->dirs);
1049 return NULL;
1051 table->dirs = tmp;
1054 table->dirs[table->num_dirs++] = cur_dir;
1057 line_ptr += bytes_read;
1059 /* Read file name table. */
1060 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1062 line_ptr += bytes_read;
1064 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1066 struct fileinfo *tmp;
1068 amt = table->num_files + FILE_ALLOC_CHUNK;
1069 amt *= sizeof (struct fileinfo);
1071 tmp = bfd_realloc (table->files, amt);
1072 if (tmp == NULL)
1074 free (table->files);
1075 free (table->dirs);
1076 return NULL;
1078 table->files = tmp;
1081 table->files[table->num_files].name = cur_file;
1082 table->files[table->num_files].dir =
1083 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1084 line_ptr += bytes_read;
1085 table->files[table->num_files].time =
1086 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1087 line_ptr += bytes_read;
1088 table->files[table->num_files].size =
1089 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1090 line_ptr += bytes_read;
1091 table->num_files++;
1094 line_ptr += bytes_read;
1096 /* Read the statement sequences until there's nothing left. */
1097 while (line_ptr < line_end)
1099 /* State machine registers. */
1100 bfd_vma address = 0;
1101 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1102 unsigned int line = 1;
1103 unsigned int column = 0;
1104 int is_stmt = lh.default_is_stmt;
1105 int basic_block = 0;
1106 int end_sequence = 0;
1107 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1108 compilers generate address sequences that are wildly out of
1109 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1110 for ia64-Linux). Thus, to determine the low and high
1111 address, we must compare on every DW_LNS_copy, etc. */
1112 bfd_vma low_pc = (bfd_vma) -1;
1113 bfd_vma high_pc = 0;
1115 /* Decode the table. */
1116 while (! end_sequence)
1118 op_code = read_1_byte (abfd, line_ptr);
1119 line_ptr += 1;
1121 if (op_code >= lh.opcode_base)
1123 /* Special operand. */
1124 adj_opcode = op_code - lh.opcode_base;
1125 address += (adj_opcode / lh.line_range)
1126 * lh.minimum_instruction_length;
1127 line += lh.line_base + (adj_opcode % lh.line_range);
1128 /* Append row to matrix using current values. */
1129 add_line_info (table, address, filename, line, column, 0);
1130 basic_block = 1;
1131 if (address < low_pc)
1132 low_pc = address;
1133 if (address > high_pc)
1134 high_pc = address;
1136 else switch (op_code)
1138 case DW_LNS_extended_op:
1139 /* Ignore length. */
1140 line_ptr += 1;
1141 extended_op = read_1_byte (abfd, line_ptr);
1142 line_ptr += 1;
1144 switch (extended_op)
1146 case DW_LNE_end_sequence:
1147 end_sequence = 1;
1148 add_line_info (table, address, filename, line, column,
1149 end_sequence);
1150 if (address < low_pc)
1151 low_pc = address;
1152 if (address > high_pc)
1153 high_pc = address;
1154 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1155 break;
1156 case DW_LNE_set_address:
1157 address = read_address (unit, line_ptr);
1158 line_ptr += unit->addr_size;
1159 break;
1160 case DW_LNE_define_file:
1161 cur_file = read_string (abfd, line_ptr, &bytes_read);
1162 line_ptr += bytes_read;
1163 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1165 struct fileinfo *tmp;
1167 amt = table->num_files + FILE_ALLOC_CHUNK;
1168 amt *= sizeof (struct fileinfo);
1169 tmp = bfd_realloc (table->files, amt);
1170 if (tmp == NULL)
1172 free (table->files);
1173 free (table->dirs);
1174 free (filename);
1175 return NULL;
1177 table->files = tmp;
1179 table->files[table->num_files].name = cur_file;
1180 table->files[table->num_files].dir =
1181 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1182 line_ptr += bytes_read;
1183 table->files[table->num_files].time =
1184 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1185 line_ptr += bytes_read;
1186 table->files[table->num_files].size =
1187 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1188 line_ptr += bytes_read;
1189 table->num_files++;
1190 break;
1191 default:
1192 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1193 bfd_set_error (bfd_error_bad_value);
1194 free (filename);
1195 free (table->files);
1196 free (table->dirs);
1197 return NULL;
1199 break;
1200 case DW_LNS_copy:
1201 add_line_info (table, address, filename, line, column, 0);
1202 basic_block = 0;
1203 if (address < low_pc)
1204 low_pc = address;
1205 if (address > high_pc)
1206 high_pc = address;
1207 break;
1208 case DW_LNS_advance_pc:
1209 address += lh.minimum_instruction_length
1210 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1211 line_ptr += bytes_read;
1212 break;
1213 case DW_LNS_advance_line:
1214 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1215 line_ptr += bytes_read;
1216 break;
1217 case DW_LNS_set_file:
1219 unsigned int file;
1221 /* The file and directory tables are 0
1222 based, the references are 1 based. */
1223 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1224 line_ptr += bytes_read;
1225 if (filename)
1226 free (filename);
1227 filename = concat_filename (table, file);
1228 break;
1230 case DW_LNS_set_column:
1231 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1232 line_ptr += bytes_read;
1233 break;
1234 case DW_LNS_negate_stmt:
1235 is_stmt = (!is_stmt);
1236 break;
1237 case DW_LNS_set_basic_block:
1238 basic_block = 1;
1239 break;
1240 case DW_LNS_const_add_pc:
1241 address += lh.minimum_instruction_length
1242 * ((255 - lh.opcode_base) / lh.line_range);
1243 break;
1244 case DW_LNS_fixed_advance_pc:
1245 address += read_2_bytes (abfd, line_ptr);
1246 line_ptr += 2;
1247 break;
1248 default:
1250 int i;
1252 /* Unknown standard opcode, ignore it. */
1253 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1255 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1256 line_ptr += bytes_read;
1262 if (filename)
1263 free (filename);
1266 return table;
1269 /* If ADDR is within TABLE set the output parameters and return TRUE,
1270 otherwise return FALSE. The output parameters, FILENAME_PTR and
1271 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1273 static bfd_boolean
1274 lookup_address_in_line_info_table (struct line_info_table *table,
1275 bfd_vma addr,
1276 struct funcinfo *function,
1277 const char **filename_ptr,
1278 unsigned int *linenumber_ptr)
1280 /* Note: table->last_line should be a descendingly sorted list. */
1281 struct line_info* next_line = table->last_line;
1282 struct line_info* each_line = NULL;
1283 *filename_ptr = NULL;
1285 if (!next_line)
1286 return FALSE;
1288 each_line = next_line->prev_line;
1290 /* Check for large addresses */
1291 if (addr > next_line->address)
1292 each_line = NULL; /* ensure we skip over the normal case */
1294 /* Normal case: search the list; save */
1295 while (each_line && next_line)
1297 /* If we have an address match, save this info. This allows us
1298 to return as good as results as possible for strange debugging
1299 info. */
1300 bfd_boolean addr_match = FALSE;
1301 if (each_line->address <= addr && addr < next_line->address)
1303 addr_match = TRUE;
1305 /* If this line appears to span functions, and addr is in the
1306 later function, return the first line of that function instead
1307 of the last line of the earlier one. This check is for GCC
1308 2.95, which emits the first line number for a function late. */
1310 if (function != NULL)
1312 bfd_vma lowest_pc;
1313 struct arange *arange;
1315 /* Find the lowest address in the function's range list */
1316 lowest_pc = function->arange.low;
1317 for (arange = &function->arange;
1318 arange;
1319 arange = arange->next)
1321 if (function->arange.low < lowest_pc)
1322 lowest_pc = function->arange.low;
1324 /* Check for spanning function and set outgoing line info */
1325 if (addr >= lowest_pc
1326 && each_line->address < lowest_pc
1327 && next_line->address > lowest_pc)
1329 *filename_ptr = next_line->filename;
1330 *linenumber_ptr = next_line->line;
1332 else
1334 *filename_ptr = each_line->filename;
1335 *linenumber_ptr = each_line->line;
1338 else
1340 *filename_ptr = each_line->filename;
1341 *linenumber_ptr = each_line->line;
1345 if (addr_match && !each_line->end_sequence)
1346 return TRUE; /* we have definitely found what we want */
1348 next_line = each_line;
1349 each_line = each_line->prev_line;
1352 /* At this point each_line is NULL but next_line is not. If we found
1353 a candidate end-of-sequence point in the loop above, we can return
1354 that (compatibility with a bug in the Intel compiler); otherwise,
1355 assuming that we found the containing function for this address in
1356 this compilation unit, return the first line we have a number for
1357 (compatibility with GCC 2.95). */
1358 if (*filename_ptr == NULL && function != NULL)
1360 *filename_ptr = next_line->filename;
1361 *linenumber_ptr = next_line->line;
1362 return TRUE;
1365 return FALSE;
1368 /* Read in the .debug_ranges section for future reference */
1370 static bfd_boolean
1371 read_debug_ranges (struct comp_unit *unit)
1373 struct dwarf2_debug *stash = unit->stash;
1374 if (! stash->dwarf_ranges_buffer)
1376 bfd *abfd = unit->abfd;
1377 asection *msec;
1379 msec = bfd_get_section_by_name (abfd, ".debug_ranges");
1380 if (! msec)
1382 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
1383 bfd_set_error (bfd_error_bad_value);
1384 return FALSE;
1387 stash->dwarf_ranges_size = msec->size;
1388 stash->dwarf_ranges_buffer
1389 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1390 stash->syms);
1391 if (! stash->dwarf_ranges_buffer)
1392 return FALSE;
1394 return TRUE;
1397 /* Function table functions. */
1399 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1400 Note that we need to find the function that has the smallest
1401 range that contains ADDR, to handle inlined functions without
1402 depending upon them being ordered in TABLE by increasing range. */
1404 static bfd_boolean
1405 lookup_address_in_function_table (struct comp_unit *unit,
1406 bfd_vma addr,
1407 struct funcinfo **function_ptr,
1408 const char **functionname_ptr)
1410 struct funcinfo* each_func;
1411 struct funcinfo* best_fit = NULL;
1412 struct arange *arange;
1414 for (each_func = unit->function_table;
1415 each_func;
1416 each_func = each_func->prev_func)
1418 for (arange = &each_func->arange;
1419 arange;
1420 arange = arange->next)
1422 if (addr >= arange->low && addr < arange->high)
1424 if (!best_fit ||
1425 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1426 best_fit = each_func;
1431 if (best_fit)
1433 *functionname_ptr = best_fit->name;
1434 *function_ptr = best_fit;
1435 return TRUE;
1437 else
1439 return FALSE;
1443 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1444 and LINENUMBER_PTR, and return TRUE. */
1446 static bfd_boolean
1447 lookup_symbol_in_function_table (struct comp_unit *unit,
1448 asymbol *sym,
1449 bfd_vma addr,
1450 const char **filename_ptr,
1451 unsigned int *linenumber_ptr)
1453 struct funcinfo* each_func;
1454 struct funcinfo* best_fit = NULL;
1455 struct arange *arange;
1456 const char *name = bfd_asymbol_name (sym);
1457 asection *sec = bfd_get_section (sym);
1459 for (each_func = unit->function_table;
1460 each_func;
1461 each_func = each_func->prev_func)
1463 for (arange = &each_func->arange;
1464 arange;
1465 arange = arange->next)
1467 if ((!each_func->sec || each_func->sec == sec)
1468 && addr >= arange->low
1469 && addr < arange->high
1470 && strcmp (name, each_func->name) == 0
1471 && (!best_fit
1472 || ((arange->high - arange->low)
1473 < (best_fit->arange.high - best_fit->arange.low))))
1474 best_fit = each_func;
1478 if (best_fit)
1480 best_fit->sec = sec;
1481 *filename_ptr = best_fit->file;
1482 *linenumber_ptr = best_fit->line;
1483 return TRUE;
1485 else
1486 return FALSE;
1489 /* Variable table functions. */
1491 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1492 LINENUMBER_PTR, and return TRUE. */
1494 static bfd_boolean
1495 lookup_symbol_in_variable_table (struct comp_unit *unit,
1496 asymbol *sym,
1497 bfd_vma addr,
1498 const char **filename_ptr,
1499 unsigned int *linenumber_ptr)
1501 const char *name = bfd_asymbol_name (sym);
1502 asection *sec = bfd_get_section (sym);
1503 struct varinfo* each;
1505 for (each = unit->variable_table; each; each = each->prev_var)
1506 if (each->stack == 0
1507 && each->file != NULL
1508 && each->name != NULL
1509 && each->addr == addr
1510 && (!each->sec || each->sec == sec)
1511 && strcmp (name, each->name) == 0)
1512 break;
1514 if (each)
1516 each->sec = sec;
1517 *filename_ptr = each->file;
1518 *linenumber_ptr = each->line;
1519 return TRUE;
1521 else
1522 return FALSE;
1525 static char *
1526 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1528 bfd *abfd = unit->abfd;
1529 bfd_byte *info_ptr;
1530 unsigned int abbrev_number, bytes_read, i;
1531 struct abbrev_info *abbrev;
1532 struct attribute attr;
1533 char *name = 0;
1535 info_ptr = unit->info_ptr_unit + die_ref;
1536 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1537 info_ptr += bytes_read;
1539 if (abbrev_number)
1541 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1542 if (! abbrev)
1544 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1545 abbrev_number);
1546 bfd_set_error (bfd_error_bad_value);
1548 else
1550 for (i = 0; i < abbrev->num_attrs; ++i)
1552 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1553 switch (attr.name)
1555 case DW_AT_name:
1556 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1557 if (name == NULL)
1558 name = attr.u.str;
1559 break;
1560 case DW_AT_specification:
1561 name = find_abstract_instance_name (unit, attr.u.val);
1562 break;
1563 case DW_AT_MIPS_linkage_name:
1564 name = attr.u.str;
1565 break;
1566 default:
1567 break;
1572 return (name);
1575 static void
1576 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1578 bfd_byte *ranges_ptr;
1579 bfd_vma base_address = unit->base_address;
1581 if (! unit->stash->dwarf_ranges_buffer)
1583 if (! read_debug_ranges (unit))
1584 return;
1586 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1588 for (;;)
1590 bfd_vma low_pc;
1591 bfd_vma high_pc;
1593 if (unit->addr_size == 4)
1595 low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1596 ranges_ptr += 4;
1597 high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1598 ranges_ptr += 4;
1600 else
1602 low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1603 ranges_ptr += 8;
1604 high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1605 ranges_ptr += 8;
1607 if (low_pc == 0 && high_pc == 0)
1608 break;
1609 if (low_pc == -1UL && high_pc != -1UL)
1610 base_address = high_pc;
1611 else
1612 arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1616 /* DWARF2 Compilation unit functions. */
1618 /* Scan over each die in a comp. unit looking for functions to add
1619 to the function table and variables to the variable table. */
1621 static bfd_boolean
1622 scan_unit_for_symbols (struct comp_unit *unit)
1624 bfd *abfd = unit->abfd;
1625 bfd_byte *info_ptr = unit->first_child_die_ptr;
1626 int nesting_level = 1;
1627 struct funcinfo **nested_funcs;
1628 int nested_funcs_size;
1630 /* Maintain a stack of in-scope functions and inlined functions, which we
1631 can use to set the caller_func field. */
1632 nested_funcs_size = 32;
1633 nested_funcs = bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
1634 if (nested_funcs == NULL)
1635 return FALSE;
1636 nested_funcs[nesting_level] = 0;
1638 while (nesting_level)
1640 unsigned int abbrev_number, bytes_read, i;
1641 struct abbrev_info *abbrev;
1642 struct attribute attr;
1643 struct funcinfo *func;
1644 struct varinfo *var;
1645 bfd_vma low_pc = 0;
1646 bfd_vma high_pc = 0;
1648 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1649 info_ptr += bytes_read;
1651 if (! abbrev_number)
1653 nesting_level--;
1654 continue;
1657 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1658 if (! abbrev)
1660 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1661 abbrev_number);
1662 bfd_set_error (bfd_error_bad_value);
1663 free (nested_funcs);
1664 return FALSE;
1667 var = NULL;
1668 if (abbrev->tag == DW_TAG_subprogram
1669 || abbrev->tag == DW_TAG_entry_point
1670 || abbrev->tag == DW_TAG_inlined_subroutine)
1672 bfd_size_type amt = sizeof (struct funcinfo);
1673 func = bfd_zalloc (abfd, amt);
1674 func->tag = abbrev->tag;
1675 func->prev_func = unit->function_table;
1676 unit->function_table = func;
1678 if (func->tag == DW_TAG_inlined_subroutine)
1679 for (i = nesting_level - 1; i >= 1; i--)
1680 if (nested_funcs[i])
1682 func->caller_func = nested_funcs[i];
1683 break;
1685 nested_funcs[nesting_level] = func;
1687 else
1689 func = NULL;
1690 if (abbrev->tag == DW_TAG_variable)
1692 bfd_size_type amt = sizeof (struct varinfo);
1693 var = bfd_zalloc (abfd, amt);
1694 var->tag = abbrev->tag;
1695 var->stack = 1;
1696 var->prev_var = unit->variable_table;
1697 unit->variable_table = var;
1700 /* No inline function in scope at this nesting level. */
1701 nested_funcs[nesting_level] = 0;
1704 for (i = 0; i < abbrev->num_attrs; ++i)
1706 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1708 if (func)
1710 switch (attr.name)
1712 case DW_AT_call_file:
1713 func->caller_file = concat_filename (unit->line_table, attr.u.val);
1714 break;
1716 case DW_AT_call_line:
1717 func->caller_line = attr.u.val;
1718 break;
1720 case DW_AT_abstract_origin:
1721 func->name = find_abstract_instance_name (unit, attr.u.val);
1722 break;
1724 case DW_AT_name:
1725 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1726 if (func->name == NULL)
1727 func->name = attr.u.str;
1728 break;
1730 case DW_AT_MIPS_linkage_name:
1731 func->name = attr.u.str;
1732 break;
1734 case DW_AT_low_pc:
1735 low_pc = attr.u.val;
1736 break;
1738 case DW_AT_high_pc:
1739 high_pc = attr.u.val;
1740 break;
1742 case DW_AT_ranges:
1743 read_rangelist (unit, &func->arange, attr.u.val);
1744 break;
1746 case DW_AT_decl_file:
1747 func->file = concat_filename (unit->line_table,
1748 attr.u.val);
1749 break;
1751 case DW_AT_decl_line:
1752 func->line = attr.u.val;
1753 break;
1755 default:
1756 break;
1759 else if (var)
1761 switch (attr.name)
1763 case DW_AT_name:
1764 var->name = attr.u.str;
1765 break;
1767 case DW_AT_decl_file:
1768 var->file = concat_filename (unit->line_table,
1769 attr.u.val);
1770 break;
1772 case DW_AT_decl_line:
1773 var->line = attr.u.val;
1774 break;
1776 case DW_AT_external:
1777 if (attr.u.val != 0)
1778 var->stack = 0;
1779 break;
1781 case DW_AT_location:
1782 switch (attr.form)
1784 case DW_FORM_block:
1785 case DW_FORM_block1:
1786 case DW_FORM_block2:
1787 case DW_FORM_block4:
1788 if (*attr.u.blk->data == DW_OP_addr)
1790 var->stack = 0;
1792 /* Verify that DW_OP_addr is the only opcode in the
1793 location, in which case the block size will be 1
1794 plus the address size. */
1795 /* ??? For TLS variables, gcc can emit
1796 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
1797 which we don't handle here yet. */
1798 if (attr.u.blk->size == unit->addr_size + 1U)
1799 var->addr = bfd_get (unit->addr_size * 8,
1800 unit->abfd,
1801 attr.u.blk->data + 1);
1803 break;
1805 default:
1806 break;
1808 break;
1810 default:
1811 break;
1816 if (func && high_pc != 0)
1818 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
1821 if (abbrev->has_children)
1823 nesting_level++;
1825 if (nesting_level >= nested_funcs_size)
1827 struct funcinfo **tmp;
1829 nested_funcs_size *= 2;
1830 tmp = bfd_realloc (nested_funcs,
1831 (nested_funcs_size
1832 * sizeof (struct funcinfo *)));
1833 if (tmp == NULL)
1835 free (nested_funcs);
1836 return FALSE;
1838 nested_funcs = tmp;
1840 nested_funcs[nesting_level] = 0;
1844 free (nested_funcs);
1845 return TRUE;
1848 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1849 includes the compilation unit header that proceeds the DIE's, but
1850 does not include the length field that precedes each compilation
1851 unit header. END_PTR points one past the end of this comp unit.
1852 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1854 This routine does not read the whole compilation unit; only enough
1855 to get to the line number information for the compilation unit. */
1857 static struct comp_unit *
1858 parse_comp_unit (bfd *abfd,
1859 struct dwarf2_debug *stash,
1860 bfd_vma unit_length,
1861 bfd_byte *info_ptr_unit,
1862 unsigned int offset_size)
1864 struct comp_unit* unit;
1865 unsigned int version;
1866 bfd_uint64_t abbrev_offset = 0;
1867 unsigned int addr_size;
1868 struct abbrev_info** abbrevs;
1869 unsigned int abbrev_number, bytes_read, i;
1870 struct abbrev_info *abbrev;
1871 struct attribute attr;
1872 bfd_byte *info_ptr = stash->info_ptr;
1873 bfd_byte *end_ptr = info_ptr + unit_length;
1874 bfd_size_type amt;
1875 bfd_vma low_pc = 0;
1876 bfd_vma high_pc = 0;
1878 version = read_2_bytes (abfd, info_ptr);
1879 info_ptr += 2;
1880 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1881 if (offset_size == 4)
1882 abbrev_offset = read_4_bytes (abfd, info_ptr);
1883 else
1884 abbrev_offset = read_8_bytes (abfd, info_ptr);
1885 info_ptr += offset_size;
1886 addr_size = read_1_byte (abfd, info_ptr);
1887 info_ptr += 1;
1889 if (version != 2)
1891 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1892 bfd_set_error (bfd_error_bad_value);
1893 return 0;
1896 if (addr_size > sizeof (bfd_vma))
1898 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1899 addr_size,
1900 (unsigned int) sizeof (bfd_vma));
1901 bfd_set_error (bfd_error_bad_value);
1902 return 0;
1905 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1907 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1908 bfd_set_error (bfd_error_bad_value);
1909 return 0;
1912 /* Read the abbrevs for this compilation unit into a table. */
1913 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1914 if (! abbrevs)
1915 return 0;
1917 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1918 info_ptr += bytes_read;
1919 if (! abbrev_number)
1921 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1922 abbrev_number);
1923 bfd_set_error (bfd_error_bad_value);
1924 return 0;
1927 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1928 if (! abbrev)
1930 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1931 abbrev_number);
1932 bfd_set_error (bfd_error_bad_value);
1933 return 0;
1936 amt = sizeof (struct comp_unit);
1937 unit = bfd_zalloc (abfd, amt);
1938 unit->abfd = abfd;
1939 unit->addr_size = addr_size;
1940 unit->offset_size = offset_size;
1941 unit->abbrevs = abbrevs;
1942 unit->end_ptr = end_ptr;
1943 unit->stash = stash;
1944 unit->info_ptr_unit = info_ptr_unit;
1946 for (i = 0; i < abbrev->num_attrs; ++i)
1948 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1950 /* Store the data if it is of an attribute we want to keep in a
1951 partial symbol table. */
1952 switch (attr.name)
1954 case DW_AT_stmt_list:
1955 unit->stmtlist = 1;
1956 unit->line_offset = attr.u.val;
1957 break;
1959 case DW_AT_name:
1960 unit->name = attr.u.str;
1961 break;
1963 case DW_AT_low_pc:
1964 low_pc = attr.u.val;
1965 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
1966 this is the base address to use when reading location
1967 lists or range lists. */
1968 unit->base_address = low_pc;
1969 break;
1971 case DW_AT_high_pc:
1972 high_pc = attr.u.val;
1973 break;
1975 case DW_AT_ranges:
1976 read_rangelist (unit, &unit->arange, attr.u.val);
1977 break;
1979 case DW_AT_comp_dir:
1981 char *comp_dir = attr.u.str;
1982 if (comp_dir)
1984 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1985 directory, get rid of it. */
1986 char *cp = strchr (comp_dir, ':');
1988 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1989 comp_dir = cp + 1;
1991 unit->comp_dir = comp_dir;
1992 break;
1995 default:
1996 break;
1999 if (high_pc != 0)
2001 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
2004 unit->first_child_die_ptr = info_ptr;
2005 return unit;
2008 /* Return TRUE if UNIT contains the address given by ADDR. */
2010 static bfd_boolean
2011 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2013 struct arange *arange;
2015 if (unit->error)
2016 return FALSE;
2018 arange = &unit->arange;
2021 if (addr >= arange->low && addr < arange->high)
2022 return TRUE;
2023 arange = arange->next;
2025 while (arange);
2027 return FALSE;
2030 /* If UNIT contains ADDR, set the output parameters to the values for
2031 the line containing ADDR. The output parameters, FILENAME_PTR,
2032 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2033 to be filled in.
2035 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2036 FALSE otherwise. */
2038 static bfd_boolean
2039 comp_unit_find_nearest_line (struct comp_unit *unit,
2040 bfd_vma addr,
2041 const char **filename_ptr,
2042 const char **functionname_ptr,
2043 unsigned int *linenumber_ptr,
2044 struct dwarf2_debug *stash)
2046 bfd_boolean line_p;
2047 bfd_boolean func_p;
2048 struct funcinfo *function;
2050 if (unit->error)
2051 return FALSE;
2053 if (! unit->line_table)
2055 if (! unit->stmtlist)
2057 unit->error = 1;
2058 return FALSE;
2061 unit->line_table = decode_line_info (unit, stash);
2063 if (! unit->line_table)
2065 unit->error = 1;
2066 return FALSE;
2069 if (unit->first_child_die_ptr < unit->end_ptr
2070 && ! scan_unit_for_symbols (unit))
2072 unit->error = 1;
2073 return FALSE;
2077 function = NULL;
2078 func_p = lookup_address_in_function_table (unit, addr,
2079 &function, functionname_ptr);
2080 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2081 stash->inliner_chain = function;
2082 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2083 function, filename_ptr,
2084 linenumber_ptr);
2085 return line_p || func_p;
2088 /* If UNIT contains SYM at ADDR, set the output parameters to the
2089 values for the line containing SYM. The output parameters,
2090 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2091 filled in.
2093 Return TRUE if UNIT contains SYM, and no errors were encountered;
2094 FALSE otherwise. */
2096 static bfd_boolean
2097 comp_unit_find_line (struct comp_unit *unit,
2098 asymbol *sym,
2099 bfd_vma addr,
2100 const char **filename_ptr,
2101 unsigned int *linenumber_ptr,
2102 struct dwarf2_debug *stash)
2104 if (unit->error)
2105 return FALSE;
2107 if (! unit->line_table)
2109 if (! unit->stmtlist)
2111 unit->error = 1;
2112 return FALSE;
2115 unit->line_table = decode_line_info (unit, stash);
2117 if (! unit->line_table)
2119 unit->error = 1;
2120 return FALSE;
2123 if (unit->first_child_die_ptr < unit->end_ptr
2124 && ! scan_unit_for_symbols (unit))
2126 unit->error = 1;
2127 return FALSE;
2131 if (sym->flags & BSF_FUNCTION)
2132 return lookup_symbol_in_function_table (unit, sym, addr,
2133 filename_ptr,
2134 linenumber_ptr);
2135 else
2136 return lookup_symbol_in_variable_table (unit, sym, addr,
2137 filename_ptr,
2138 linenumber_ptr);
2141 /* Locate a section in a BFD containing debugging info. The search starts
2142 from the section after AFTER_SEC, or from the first section in the BFD if
2143 AFTER_SEC is NULL. The search works by examining the names of the
2144 sections. There are two permissiable names. The first is .debug_info.
2145 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2146 This is a variation on the .debug_info section which has a checksum
2147 describing the contents appended onto the name. This allows the linker to
2148 identify and discard duplicate debugging sections for different
2149 compilation units. */
2150 #define DWARF2_DEBUG_INFO ".debug_info"
2151 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2153 static asection *
2154 find_debug_info (bfd *abfd, asection *after_sec)
2156 asection * msec;
2158 if (after_sec)
2159 msec = after_sec->next;
2160 else
2161 msec = abfd->sections;
2163 while (msec)
2165 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2166 return msec;
2168 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
2169 return msec;
2171 msec = msec->next;
2174 return NULL;
2177 /* The DWARF2 version of find_nearest_line. Return TRUE if the line
2178 is found without error. ADDR_SIZE is the number of bytes in the
2179 initial .debug_info length field and in the abbreviation offset.
2180 You may use zero to indicate that the default value should be
2181 used. */
2183 bfd_boolean
2184 _bfd_dwarf2_find_nearest_line (bfd *abfd,
2185 asection *section,
2186 asymbol **symbols,
2187 bfd_vma offset,
2188 const char **filename_ptr,
2189 const char **functionname_ptr,
2190 unsigned int *linenumber_ptr,
2191 unsigned int addr_size,
2192 void **pinfo)
2194 /* Read each compilation unit from the section .debug_info, and check
2195 to see if it contains the address we are searching for. If yes,
2196 lookup the address, and return the line number info. If no, go
2197 on to the next compilation unit.
2199 We keep a list of all the previously read compilation units, and
2200 a pointer to the next un-read compilation unit. Check the
2201 previously read units before reading more. */
2202 struct dwarf2_debug *stash;
2204 /* What address are we looking for? */
2205 bfd_vma addr;
2207 struct comp_unit* each;
2209 stash = *pinfo;
2210 addr = offset;
2211 if (section->output_section)
2212 addr += section->output_section->lma + section->output_offset;
2213 else
2214 addr += section->lma;
2215 *filename_ptr = NULL;
2216 *functionname_ptr = NULL;
2217 *linenumber_ptr = 0;
2219 /* The DWARF2 spec says that the initial length field, and the
2220 offset of the abbreviation table, should both be 4-byte values.
2221 However, some compilers do things differently. */
2222 if (addr_size == 0)
2223 addr_size = 4;
2224 BFD_ASSERT (addr_size == 4 || addr_size == 8);
2226 if (! stash)
2228 bfd_size_type total_size;
2229 asection *msec;
2230 bfd_size_type amt = sizeof (struct dwarf2_debug);
2232 stash = bfd_zalloc (abfd, amt);
2233 if (! stash)
2234 return FALSE;
2236 *pinfo = stash;
2238 msec = find_debug_info (abfd, NULL);
2239 if (! msec)
2240 /* No dwarf2 info. Note that at this point the stash
2241 has been allocated, but contains zeros, this lets
2242 future calls to this function fail quicker. */
2243 return FALSE;
2245 /* There can be more than one DWARF2 info section in a BFD these days.
2246 Read them all in and produce one large stash. We do this in two
2247 passes - in the first pass we just accumulate the section sizes.
2248 In the second pass we read in the section's contents. The allows
2249 us to avoid reallocing the data as we add sections to the stash. */
2250 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
2251 total_size += msec->size;
2253 stash->info_ptr = bfd_alloc (abfd, total_size);
2254 if (stash->info_ptr == NULL)
2255 return FALSE;
2257 stash->info_ptr_end = stash->info_ptr;
2259 for (msec = find_debug_info (abfd, NULL);
2260 msec;
2261 msec = find_debug_info (abfd, msec))
2263 bfd_size_type size;
2264 bfd_size_type start;
2266 size = msec->size;
2267 if (size == 0)
2268 continue;
2270 start = stash->info_ptr_end - stash->info_ptr;
2272 if ((bfd_simple_get_relocated_section_contents
2273 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
2274 continue;
2276 stash->info_ptr_end = stash->info_ptr + start + size;
2279 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2281 stash->sec = find_debug_info (abfd, NULL);
2282 stash->sec_info_ptr = stash->info_ptr;
2283 stash->syms = symbols;
2286 /* A null info_ptr indicates that there is no dwarf2 info
2287 (or that an error occured while setting up the stash). */
2288 if (! stash->info_ptr)
2289 return FALSE;
2291 stash->inliner_chain = NULL;
2293 /* Check the previously read comp. units first. */
2294 for (each = stash->all_comp_units; each; each = each->next_unit)
2295 if (comp_unit_contains_address (each, addr))
2296 return comp_unit_find_nearest_line (each, addr, filename_ptr,
2297 functionname_ptr, linenumber_ptr,
2298 stash);
2300 /* Read each remaining comp. units checking each as they are read. */
2301 while (stash->info_ptr < stash->info_ptr_end)
2303 bfd_vma length;
2304 bfd_boolean found;
2305 unsigned int offset_size = addr_size;
2306 bfd_byte *info_ptr_unit = stash->info_ptr;
2308 length = read_4_bytes (abfd, stash->info_ptr);
2309 /* A 0xffffff length is the DWARF3 way of indicating we use
2310 64-bit offsets, instead of 32-bit offsets. */
2311 if (length == 0xffffffff)
2313 offset_size = 8;
2314 length = read_8_bytes (abfd, stash->info_ptr + 4);
2315 stash->info_ptr += 12;
2317 /* A zero length is the IRIX way of indicating 64-bit offsets,
2318 mostly because the 64-bit length will generally fit in 32
2319 bits, and the endianness helps. */
2320 else if (length == 0)
2322 offset_size = 8;
2323 length = read_4_bytes (abfd, stash->info_ptr + 4);
2324 stash->info_ptr += 8;
2326 /* In the absence of the hints above, we assume addr_size-sized
2327 offsets, for backward-compatibility with pre-DWARF3 64-bit
2328 platforms. */
2329 else if (addr_size == 8)
2331 length = read_8_bytes (abfd, stash->info_ptr);
2332 stash->info_ptr += 8;
2334 else
2335 stash->info_ptr += 4;
2337 if (length > 0)
2339 each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
2340 offset_size);
2341 stash->info_ptr += length;
2343 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
2344 == stash->sec->size)
2346 stash->sec = find_debug_info (abfd, stash->sec);
2347 stash->sec_info_ptr = stash->info_ptr;
2350 if (each)
2352 each->next_unit = stash->all_comp_units;
2353 stash->all_comp_units = each;
2355 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2356 compilation units. If we don't have them (i.e.,
2357 unit->high == 0), we need to consult the line info
2358 table to see if a compilation unit contains the given
2359 address. */
2360 if (each->arange.high > 0)
2362 if (comp_unit_contains_address (each, addr))
2363 return comp_unit_find_nearest_line (each, addr,
2364 filename_ptr,
2365 functionname_ptr,
2366 linenumber_ptr,
2367 stash);
2369 else
2371 found = comp_unit_find_nearest_line (each, addr,
2372 filename_ptr,
2373 functionname_ptr,
2374 linenumber_ptr,
2375 stash);
2376 if (found)
2377 return TRUE;
2383 return FALSE;
2386 /* The DWARF2 version of find_line. Return TRUE if the line is found
2387 without error. */
2389 bfd_boolean
2390 _bfd_dwarf2_find_line (bfd *abfd,
2391 asymbol **symbols,
2392 asymbol *symbol,
2393 const char **filename_ptr,
2394 unsigned int *linenumber_ptr,
2395 unsigned int addr_size,
2396 void **pinfo)
2398 /* Read each compilation unit from the section .debug_info, and check
2399 to see if it contains the address we are searching for. If yes,
2400 lookup the address, and return the line number info. If no, go
2401 on to the next compilation unit.
2403 We keep a list of all the previously read compilation units, and
2404 a pointer to the next un-read compilation unit. Check the
2405 previously read units before reading more. */
2406 struct dwarf2_debug *stash;
2408 /* What address are we looking for? */
2409 bfd_vma addr;
2411 struct comp_unit* each;
2413 asection *section;
2415 bfd_boolean found;
2417 section = bfd_get_section (symbol);
2419 addr = symbol->value;
2420 if (section->output_section)
2421 addr += section->output_section->lma + section->output_offset;
2422 else
2423 addr += section->lma;
2425 *filename_ptr = NULL;
2426 stash = *pinfo;
2427 *filename_ptr = NULL;
2428 *linenumber_ptr = 0;
2430 if (! stash)
2432 bfd_size_type total_size;
2433 asection *msec;
2434 bfd_size_type amt = sizeof (struct dwarf2_debug);
2436 stash = bfd_zalloc (abfd, amt);
2437 if (! stash)
2438 return FALSE;
2440 *pinfo = stash;
2442 msec = find_debug_info (abfd, NULL);
2443 if (! msec)
2444 /* No dwarf2 info. Note that at this point the stash
2445 has been allocated, but contains zeros, this lets
2446 future calls to this function fail quicker. */
2447 return FALSE;
2449 /* There can be more than one DWARF2 info section in a BFD these days.
2450 Read them all in and produce one large stash. We do this in two
2451 passes - in the first pass we just accumulate the section sizes.
2452 In the second pass we read in the section's contents. The allows
2453 us to avoid reallocing the data as we add sections to the stash. */
2454 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
2455 total_size += msec->size;
2457 stash->info_ptr = bfd_alloc (abfd, total_size);
2458 if (stash->info_ptr == NULL)
2459 return FALSE;
2461 stash->info_ptr_end = stash->info_ptr;
2463 for (msec = find_debug_info (abfd, NULL);
2464 msec;
2465 msec = find_debug_info (abfd, msec))
2467 bfd_size_type size;
2468 bfd_size_type start;
2470 size = msec->size;
2471 if (size == 0)
2472 continue;
2474 start = stash->info_ptr_end - stash->info_ptr;
2476 if ((bfd_simple_get_relocated_section_contents
2477 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
2478 continue;
2480 stash->info_ptr_end = stash->info_ptr + start + size;
2483 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2485 stash->sec = find_debug_info (abfd, NULL);
2486 stash->sec_info_ptr = stash->info_ptr;
2487 stash->syms = symbols;
2490 /* A null info_ptr indicates that there is no dwarf2 info
2491 (or that an error occured while setting up the stash). */
2492 if (! stash->info_ptr)
2493 return FALSE;
2495 stash->inliner_chain = NULL;
2497 /* Check the previously read comp. units first. */
2498 for (each = stash->all_comp_units; each; each = each->next_unit)
2499 if ((symbol->flags & BSF_FUNCTION) == 0
2500 || comp_unit_contains_address (each, addr))
2502 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
2503 linenumber_ptr, stash);
2504 if (found)
2505 return found;
2508 /* The DWARF2 spec says that the initial length field, and the
2509 offset of the abbreviation table, should both be 4-byte values.
2510 However, some compilers do things differently. */
2511 if (addr_size == 0)
2512 addr_size = 4;
2513 BFD_ASSERT (addr_size == 4 || addr_size == 8);
2515 /* Read each remaining comp. units checking each as they are read. */
2516 while (stash->info_ptr < stash->info_ptr_end)
2518 bfd_vma length;
2519 unsigned int offset_size = addr_size;
2520 bfd_byte *info_ptr_unit = stash->info_ptr;
2522 length = read_4_bytes (abfd, stash->info_ptr);
2523 /* A 0xffffff length is the DWARF3 way of indicating we use
2524 64-bit offsets, instead of 32-bit offsets. */
2525 if (length == 0xffffffff)
2527 offset_size = 8;
2528 length = read_8_bytes (abfd, stash->info_ptr + 4);
2529 stash->info_ptr += 12;
2531 /* A zero length is the IRIX way of indicating 64-bit offsets,
2532 mostly because the 64-bit length will generally fit in 32
2533 bits, and the endianness helps. */
2534 else if (length == 0)
2536 offset_size = 8;
2537 length = read_4_bytes (abfd, stash->info_ptr + 4);
2538 stash->info_ptr += 8;
2540 /* In the absence of the hints above, we assume addr_size-sized
2541 offsets, for backward-compatibility with pre-DWARF3 64-bit
2542 platforms. */
2543 else if (addr_size == 8)
2545 length = read_8_bytes (abfd, stash->info_ptr);
2546 stash->info_ptr += 8;
2548 else
2549 stash->info_ptr += 4;
2551 if (length > 0)
2553 each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
2554 offset_size);
2555 stash->info_ptr += length;
2557 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
2558 == stash->sec->size)
2560 stash->sec = find_debug_info (abfd, stash->sec);
2561 stash->sec_info_ptr = stash->info_ptr;
2564 if (each)
2566 each->next_unit = stash->all_comp_units;
2567 stash->all_comp_units = each;
2569 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2570 compilation units. If we don't have them (i.e.,
2571 unit->high == 0), we need to consult the line info
2572 table to see if a compilation unit contains the given
2573 address. */
2574 found = (((symbol->flags & BSF_FUNCTION) == 0
2575 || each->arange.high <= 0
2576 || comp_unit_contains_address (each, addr))
2577 && comp_unit_find_line (each, symbol, addr,
2578 filename_ptr,
2579 linenumber_ptr,
2580 stash));
2581 if (found)
2582 return TRUE;
2587 return FALSE;
2590 bfd_boolean
2591 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
2592 const char **filename_ptr,
2593 const char **functionname_ptr,
2594 unsigned int *linenumber_ptr,
2595 void **pinfo)
2597 struct dwarf2_debug *stash;
2599 stash = *pinfo;
2600 if (stash)
2602 struct funcinfo *func = stash->inliner_chain;
2603 if (func && func->caller_func)
2605 *filename_ptr = func->caller_file;
2606 *functionname_ptr = func->caller_func->name;
2607 *linenumber_ptr = func->caller_line;
2608 stash->inliner_chain = func->caller_func;
2609 return (TRUE);
2613 return (FALSE);
2616 void
2617 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
2619 struct comp_unit *each;
2620 struct dwarf2_debug *stash;
2622 if (abfd == NULL || elf_tdata (abfd) == NULL)
2623 return;
2625 stash = elf_tdata (abfd)->dwarf2_find_line_info;
2627 if (stash == NULL)
2628 return;
2630 for (each = stash->all_comp_units; each; each = each->next_unit)
2632 struct abbrev_info **abbrevs = each->abbrevs;
2633 size_t i;
2635 for (i = 0; i < ABBREV_HASH_SIZE; i++)
2637 struct abbrev_info *abbrev = abbrevs[i];
2639 while (abbrev)
2641 free (abbrev->attrs);
2642 abbrev = abbrev->next;
2646 if (each->line_table)
2648 free (each->line_table->dirs);
2649 free (each->line_table->files);
2653 free (stash->dwarf_abbrev_buffer);
2654 free (stash->dwarf_line_buffer);
2655 free (stash->dwarf_ranges_buffer);