Approved by nickc@redhat.com:
[binutils.git] / bfd / dwarf2.c
blobd86c66ee45a0f5195917dc832e0fed3c7db6267b
1 /* DWARF 2 support.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 char *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 char* info_ptr;
87 /* Preserve the original value of info_ptr for the current
88 comp_unit so we can find a given entry by its reference. */
89 char* info_ptr_unit;
91 /* Pointer to the end of the .debug_info section memory buffer. */
92 char* info_ptr_end;
94 /* Pointer to the section and address of the beginning of the
95 section. */
96 asection* sec;
97 char* sec_info_ptr;
99 /* Pointer to the symbol table. */
100 asymbol** syms;
102 /* Pointer to the .debug_abbrev section loaded into memory. */
103 char* dwarf_abbrev_buffer;
105 /* Length of the loaded .debug_abbrev section. */
106 unsigned long dwarf_abbrev_size;
108 /* Buffer for decode_line_info. */
109 char *dwarf_line_buffer;
111 /* Length of the loaded .debug_line section. */
112 unsigned long dwarf_line_size;
114 /* Pointer to the .debug_str section loaded into memory. */
115 char* dwarf_str_buffer;
117 /* Length of the loaded .debug_str section. */
118 unsigned long dwarf_str_size;
121 struct arange
123 struct arange *next;
124 bfd_vma low;
125 bfd_vma high;
128 /* A minimal decoding of DWARF2 compilation units. We only decode
129 what's needed to get to the line number information. */
131 struct comp_unit
133 /* Chain the previously read compilation units. */
134 struct comp_unit* next_unit;
136 /* Keep the bdf convenient (for memory allocation). */
137 bfd* abfd;
139 /* The lowest and higest addresses contained in this compilation
140 unit as specified in the compilation unit header. */
141 struct arange arange;
143 /* The DW_AT_name attribute (for error messages). */
144 char* name;
146 /* The abbrev hash table. */
147 struct abbrev_info** abbrevs;
149 /* Note that an error was found by comp_unit_find_nearest_line. */
150 int error;
152 /* The DW_AT_comp_dir attribute. */
153 char* comp_dir;
155 /* TRUE if there is a line number table associated with this comp. unit. */
156 int stmtlist;
158 /* The offset into .debug_line of the line number table. */
159 unsigned long line_offset;
161 /* Pointer to the first child die for the comp unit. */
162 char *first_child_die_ptr;
164 /* The end of the comp unit. */
165 char *end_ptr;
167 /* The decoded line number, NULL if not yet decoded. */
168 struct line_info_table* line_table;
170 /* A list of the functions found in this comp. unit. */
171 struct funcinfo* function_table;
173 /* Pointer to dwarf2_debug structure. */
174 struct dwarf2_debug *stash;
176 /* Address size for this unit - from unit header. */
177 unsigned char addr_size;
179 /* Offset size for this unit - from unit header. */
180 unsigned char offset_size;
183 /* This data structure holds the information of an abbrev. */
184 struct abbrev_info
186 unsigned int number; /* Number identifying abbrev. */
187 enum dwarf_tag tag; /* DWARF tag. */
188 int has_children; /* Boolean. */
189 unsigned int num_attrs; /* Number of attributes. */
190 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
191 struct abbrev_info *next; /* Next in chain. */
194 struct attr_abbrev
196 enum dwarf_attribute name;
197 enum dwarf_form form;
200 #ifndef ABBREV_HASH_SIZE
201 #define ABBREV_HASH_SIZE 121
202 #endif
203 #ifndef ATTR_ALLOC_CHUNK
204 #define ATTR_ALLOC_CHUNK 4
205 #endif
207 /* VERBATIM
208 The following function up to the END VERBATIM mark are
209 copied directly from dwarf2read.c. */
211 /* Read dwarf information from a buffer. */
213 static unsigned int
214 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, char *buf)
216 return bfd_get_8 (abfd, buf);
219 static int
220 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, char *buf)
222 return bfd_get_signed_8 (abfd, buf);
225 static unsigned int
226 read_2_bytes (bfd *abfd, char *buf)
228 return bfd_get_16 (abfd, buf);
231 static unsigned int
232 read_4_bytes (bfd *abfd, char *buf)
234 return bfd_get_32 (abfd, buf);
237 static bfd_uint64_t
238 read_8_bytes (bfd *abfd, char *buf)
240 return bfd_get_64 (abfd, buf);
243 static char *
244 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
245 char *buf,
246 unsigned int size ATTRIBUTE_UNUSED)
248 /* If the size of a host char is 8 bits, we can return a pointer
249 to the buffer, otherwise we have to copy the data to a buffer
250 allocated on the temporary obstack. */
251 return buf;
254 static char *
255 read_string (bfd *abfd ATTRIBUTE_UNUSED,
256 char *buf,
257 unsigned int *bytes_read_ptr)
259 /* Return a pointer to the embedded string. */
260 if (*buf == '\0')
262 *bytes_read_ptr = 1;
263 return NULL;
266 *bytes_read_ptr = strlen (buf) + 1;
267 return buf;
270 static char *
271 read_indirect_string (struct comp_unit* unit,
272 char *buf,
273 unsigned int *bytes_read_ptr)
275 bfd_uint64_t offset;
276 struct dwarf2_debug *stash = unit->stash;
278 if (unit->offset_size == 4)
279 offset = read_4_bytes (unit->abfd, buf);
280 else
281 offset = read_8_bytes (unit->abfd, buf);
282 *bytes_read_ptr = unit->offset_size;
284 if (! stash->dwarf_str_buffer)
286 asection *msec;
287 bfd *abfd = unit->abfd;
288 bfd_size_type sz;
290 msec = bfd_get_section_by_name (abfd, ".debug_str");
291 if (! msec)
293 (*_bfd_error_handler)
294 (_("Dwarf Error: Can't find .debug_str section."));
295 bfd_set_error (bfd_error_bad_value);
296 return NULL;
299 sz = msec->rawsize ? msec->rawsize : msec->size;
300 stash->dwarf_str_size = sz;
301 stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
302 if (! stash->dwarf_abbrev_buffer)
303 return NULL;
305 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
306 0, sz))
307 return NULL;
310 if (offset >= stash->dwarf_str_size)
312 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
313 (unsigned long) offset, stash->dwarf_str_size);
314 bfd_set_error (bfd_error_bad_value);
315 return NULL;
318 buf = stash->dwarf_str_buffer + offset;
319 if (*buf == '\0')
320 return NULL;
321 return buf;
324 static unsigned int
325 read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
326 char *buf,
327 unsigned int *bytes_read_ptr)
329 unsigned int result;
330 unsigned int num_read;
331 int shift;
332 unsigned char byte;
334 result = 0;
335 shift = 0;
336 num_read = 0;
340 byte = bfd_get_8 (abfd, buf);
341 buf ++;
342 num_read ++;
343 result |= ((byte & 0x7f) << shift);
344 shift += 7;
346 while (byte & 0x80);
348 * bytes_read_ptr = num_read;
350 return result;
353 static int
354 read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
355 char *buf,
356 unsigned int * bytes_read_ptr)
358 int result;
359 int shift;
360 int num_read;
361 unsigned char byte;
363 result = 0;
364 shift = 0;
365 num_read = 0;
369 byte = bfd_get_8 (abfd, buf);
370 buf ++;
371 num_read ++;
372 result |= ((byte & 0x7f) << shift);
373 shift += 7;
375 while (byte & 0x80);
377 if ((shift < 32) && (byte & 0x40))
378 result |= -(1 << shift);
380 * bytes_read_ptr = num_read;
382 return result;
385 /* END VERBATIM */
387 static bfd_uint64_t
388 read_address (struct comp_unit *unit, char *buf)
390 switch (unit->addr_size)
392 case 8:
393 return bfd_get_64 (unit->abfd, buf);
394 case 4:
395 return bfd_get_32 (unit->abfd, buf);
396 case 2:
397 return bfd_get_16 (unit->abfd, buf);
398 default:
399 abort ();
403 /* Lookup an abbrev_info structure in the abbrev hash table. */
405 static struct abbrev_info *
406 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
408 unsigned int hash_number;
409 struct abbrev_info *abbrev;
411 hash_number = number % ABBREV_HASH_SIZE;
412 abbrev = abbrevs[hash_number];
414 while (abbrev)
416 if (abbrev->number == number)
417 return abbrev;
418 else
419 abbrev = abbrev->next;
422 return NULL;
425 /* In DWARF version 2, the description of the debugging information is
426 stored in a separate .debug_abbrev section. Before we read any
427 dies from a section we read in all abbreviations and install them
428 in a hash table. */
430 static struct abbrev_info**
431 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
433 struct abbrev_info **abbrevs;
434 char *abbrev_ptr;
435 struct abbrev_info *cur_abbrev;
436 unsigned int abbrev_number, bytes_read, abbrev_name;
437 unsigned int abbrev_form, hash_number;
438 bfd_size_type amt;
440 if (! stash->dwarf_abbrev_buffer)
442 asection *msec;
444 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
445 if (! msec)
447 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
448 bfd_set_error (bfd_error_bad_value);
449 return 0;
452 stash->dwarf_abbrev_size = msec->size;
453 stash->dwarf_abbrev_buffer
454 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
455 stash->syms);
456 if (! stash->dwarf_abbrev_buffer)
457 return 0;
460 if (offset >= stash->dwarf_abbrev_size)
462 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
463 (unsigned long) offset, stash->dwarf_abbrev_size);
464 bfd_set_error (bfd_error_bad_value);
465 return 0;
468 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
469 abbrevs = bfd_zalloc (abfd, amt);
471 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
472 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
473 abbrev_ptr += bytes_read;
475 /* Loop until we reach an abbrev number of 0. */
476 while (abbrev_number)
478 amt = sizeof (struct abbrev_info);
479 cur_abbrev = bfd_zalloc (abfd, amt);
481 /* Read in abbrev header. */
482 cur_abbrev->number = abbrev_number;
483 cur_abbrev->tag = (enum dwarf_tag)
484 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
485 abbrev_ptr += bytes_read;
486 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
487 abbrev_ptr += 1;
489 /* Now read in declarations. */
490 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
491 abbrev_ptr += bytes_read;
492 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
493 abbrev_ptr += bytes_read;
495 while (abbrev_name)
497 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
499 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
500 amt *= sizeof (struct attr_abbrev);
501 cur_abbrev->attrs = bfd_realloc (cur_abbrev->attrs, amt);
502 if (! cur_abbrev->attrs)
503 return 0;
506 cur_abbrev->attrs[cur_abbrev->num_attrs].name
507 = (enum dwarf_attribute) abbrev_name;
508 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
509 = (enum dwarf_form) abbrev_form;
510 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
511 abbrev_ptr += bytes_read;
512 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
513 abbrev_ptr += bytes_read;
516 hash_number = abbrev_number % ABBREV_HASH_SIZE;
517 cur_abbrev->next = abbrevs[hash_number];
518 abbrevs[hash_number] = cur_abbrev;
520 /* Get next abbreviation.
521 Under Irix6 the abbreviations for a compilation unit are not
522 always properly terminated with an abbrev number of 0.
523 Exit loop if we encounter an abbreviation which we have
524 already read (which means we are about to read the abbreviations
525 for the next compile unit) or if the end of the abbreviation
526 table is reached. */
527 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
528 >= stash->dwarf_abbrev_size)
529 break;
530 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
531 abbrev_ptr += bytes_read;
532 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
533 break;
536 return abbrevs;
539 /* Read an attribute value described by an attribute form. */
541 static char *
542 read_attribute_value (struct attribute *attr,
543 unsigned form,
544 struct comp_unit *unit,
545 char *info_ptr)
547 bfd *abfd = unit->abfd;
548 unsigned int bytes_read;
549 struct dwarf_block *blk;
550 bfd_size_type amt;
552 attr->form = (enum dwarf_form) form;
554 switch (form)
556 case DW_FORM_addr:
557 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
558 case DW_FORM_ref_addr:
559 attr->u.val = read_address (unit, info_ptr);
560 info_ptr += unit->addr_size;
561 break;
562 case DW_FORM_block2:
563 amt = sizeof (struct dwarf_block);
564 blk = bfd_alloc (abfd, amt);
565 blk->size = read_2_bytes (abfd, info_ptr);
566 info_ptr += 2;
567 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
568 info_ptr += blk->size;
569 attr->u.blk = blk;
570 break;
571 case DW_FORM_block4:
572 amt = sizeof (struct dwarf_block);
573 blk = bfd_alloc (abfd, amt);
574 blk->size = read_4_bytes (abfd, info_ptr);
575 info_ptr += 4;
576 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
577 info_ptr += blk->size;
578 attr->u.blk = blk;
579 break;
580 case DW_FORM_data2:
581 attr->u.val = read_2_bytes (abfd, info_ptr);
582 info_ptr += 2;
583 break;
584 case DW_FORM_data4:
585 attr->u.val = read_4_bytes (abfd, info_ptr);
586 info_ptr += 4;
587 break;
588 case DW_FORM_data8:
589 attr->u.val = read_8_bytes (abfd, info_ptr);
590 info_ptr += 8;
591 break;
592 case DW_FORM_string:
593 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
594 info_ptr += bytes_read;
595 break;
596 case DW_FORM_strp:
597 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
598 info_ptr += bytes_read;
599 break;
600 case DW_FORM_block:
601 amt = sizeof (struct dwarf_block);
602 blk = bfd_alloc (abfd, amt);
603 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
604 info_ptr += bytes_read;
605 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
606 info_ptr += blk->size;
607 attr->u.blk = blk;
608 break;
609 case DW_FORM_block1:
610 amt = sizeof (struct dwarf_block);
611 blk = bfd_alloc (abfd, amt);
612 blk->size = read_1_byte (abfd, info_ptr);
613 info_ptr += 1;
614 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
615 info_ptr += blk->size;
616 attr->u.blk = blk;
617 break;
618 case DW_FORM_data1:
619 attr->u.val = read_1_byte (abfd, info_ptr);
620 info_ptr += 1;
621 break;
622 case DW_FORM_flag:
623 attr->u.val = read_1_byte (abfd, info_ptr);
624 info_ptr += 1;
625 break;
626 case DW_FORM_sdata:
627 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
628 info_ptr += bytes_read;
629 break;
630 case DW_FORM_udata:
631 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
632 info_ptr += bytes_read;
633 break;
634 case DW_FORM_ref1:
635 attr->u.val = read_1_byte (abfd, info_ptr);
636 info_ptr += 1;
637 break;
638 case DW_FORM_ref2:
639 attr->u.val = read_2_bytes (abfd, info_ptr);
640 info_ptr += 2;
641 break;
642 case DW_FORM_ref4:
643 attr->u.val = read_4_bytes (abfd, info_ptr);
644 info_ptr += 4;
645 break;
646 case DW_FORM_ref8:
647 attr->u.val = read_8_bytes (abfd, info_ptr);
648 info_ptr += 8;
649 break;
650 case DW_FORM_ref_udata:
651 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
652 info_ptr += bytes_read;
653 break;
654 case DW_FORM_indirect:
655 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
656 info_ptr += bytes_read;
657 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
658 break;
659 default:
660 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
661 form);
662 bfd_set_error (bfd_error_bad_value);
664 return info_ptr;
667 /* Read an attribute described by an abbreviated attribute. */
669 static char *
670 read_attribute (struct attribute *attr,
671 struct attr_abbrev *abbrev,
672 struct comp_unit *unit,
673 char *info_ptr)
675 attr->name = abbrev->name;
676 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
677 return info_ptr;
680 /* Source line information table routines. */
682 #define FILE_ALLOC_CHUNK 5
683 #define DIR_ALLOC_CHUNK 5
685 struct line_info
687 struct line_info* prev_line;
688 bfd_vma address;
689 char* filename;
690 unsigned int line;
691 unsigned int column;
692 int end_sequence; /* End of (sequential) code sequence. */
695 struct fileinfo
697 char *name;
698 unsigned int dir;
699 unsigned int time;
700 unsigned int size;
703 struct line_info_table
705 bfd* abfd;
706 unsigned int num_files;
707 unsigned int num_dirs;
708 char* comp_dir;
709 char** dirs;
710 struct fileinfo* files;
711 struct line_info* last_line; /* largest VMA */
712 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
715 struct funcinfo
717 struct funcinfo *prev_func;
718 char* name;
719 bfd_vma low;
720 bfd_vma high;
723 /* Adds a new entry to the line_info list in the line_info_table, ensuring
724 that the list is sorted. Note that the line_info list is sorted from
725 highest to lowest VMA (with possible duplicates); that is,
726 line_info->prev_line always accesses an equal or smaller VMA. */
728 static void
729 add_line_info (struct line_info_table *table,
730 bfd_vma address,
731 char *filename,
732 unsigned int line,
733 unsigned int column,
734 int end_sequence)
736 bfd_size_type amt = sizeof (struct line_info);
737 struct line_info* info = bfd_alloc (table->abfd, amt);
739 /* Find the correct location for 'info'. Normally we will receive
740 new line_info data 1) in order and 2) with increasing VMAs.
741 However some compilers break the rules (cf. decode_line_info) and
742 so we include some heuristics for quickly finding the correct
743 location for 'info'. In particular, these heuristics optimize for
744 the common case in which the VMA sequence that we receive is a
745 list of locally sorted VMAs such as
746 p...z a...j (where a < j < p < z)
748 Note: table->lcl_head is used to head an *actual* or *possible*
749 sequence within the list (such as a...j) that is not directly
750 headed by table->last_line
752 Note: we may receive duplicate entries from 'decode_line_info'. */
754 while (1)
755 if (!table->last_line
756 || address >= table->last_line->address)
758 /* Normal case: add 'info' to the beginning of the list */
759 info->prev_line = table->last_line;
760 table->last_line = info;
762 /* lcl_head: initialize to head a *possible* sequence at the end. */
763 if (!table->lcl_head)
764 table->lcl_head = info;
765 break;
767 else if (!table->lcl_head->prev_line
768 && table->lcl_head->address > address)
770 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
771 list and 2) the head of 'info'. */
772 info->prev_line = NULL;
773 table->lcl_head->prev_line = info;
774 break;
776 else if (table->lcl_head->prev_line
777 && table->lcl_head->address > address
778 && address >= table->lcl_head->prev_line->address)
780 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
781 list and 2) the head of 'info'. */
782 info->prev_line = table->lcl_head->prev_line;
783 table->lcl_head->prev_line = info;
784 break;
786 else
788 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
789 heads for 'info'. Reset 'lcl_head' and repeat. */
790 struct line_info* li2 = table->last_line; /* always non-NULL */
791 struct line_info* li1 = li2->prev_line;
793 while (li1)
795 if (li2->address > address && address >= li1->address)
796 break;
798 li2 = li1; /* always non-NULL */
799 li1 = li1->prev_line;
801 table->lcl_head = li2;
804 /* Set member data of 'info'. */
805 info->address = address;
806 info->line = line;
807 info->column = column;
808 info->end_sequence = end_sequence;
810 if (filename && filename[0])
812 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
813 if (info->filename)
814 strcpy (info->filename, filename);
816 else
817 info->filename = NULL;
820 /* Extract a fully qualified filename from a line info table.
821 The returned string has been malloc'ed and it is the caller's
822 responsibility to free it. */
824 static char *
825 concat_filename (struct line_info_table *table, unsigned int file)
827 char* filename;
829 if (file - 1 >= table->num_files)
831 (*_bfd_error_handler)
832 (_("Dwarf Error: mangled line number section (bad file number)."));
833 return strdup ("<unknown>");
836 filename = table->files[file - 1].name;
838 if (! IS_ABSOLUTE_PATH (filename))
840 char* dirname = (table->files[file - 1].dir
841 ? table->dirs[table->files[file - 1].dir - 1]
842 : table->comp_dir);
844 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
845 The best we can do is return the filename part. */
846 if (dirname != NULL)
848 unsigned int len = strlen (dirname) + strlen (filename) + 2;
849 char * name;
851 name = bfd_malloc (len);
852 if (name)
853 sprintf (name, "%s/%s", dirname, filename);
854 return name;
858 return strdup (filename);
861 static void
862 arange_add (struct comp_unit *unit, bfd_vma low_pc, bfd_vma high_pc)
864 struct arange *arange;
866 /* First see if we can cheaply extend an existing range. */
867 arange = &unit->arange;
871 if (low_pc == arange->high)
873 arange->high = high_pc;
874 return;
876 if (high_pc == arange->low)
878 arange->low = low_pc;
879 return;
881 arange = arange->next;
883 while (arange);
885 if (unit->arange.high == 0)
887 /* This is the first address range: store it in unit->arange. */
888 unit->arange.next = 0;
889 unit->arange.low = low_pc;
890 unit->arange.high = high_pc;
891 return;
894 /* Need to allocate a new arange and insert it into the arange list. */
895 arange = bfd_zalloc (unit->abfd, sizeof (*arange));
896 arange->low = low_pc;
897 arange->high = high_pc;
899 arange->next = unit->arange.next;
900 unit->arange.next = arange;
903 /* Decode the line number information for UNIT. */
905 static struct line_info_table*
906 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
908 bfd *abfd = unit->abfd;
909 struct line_info_table* table;
910 char *line_ptr;
911 char *line_end;
912 struct line_head lh;
913 unsigned int i, bytes_read, offset_size;
914 char *cur_file, *cur_dir;
915 unsigned char op_code, extended_op, adj_opcode;
916 bfd_size_type amt;
918 if (! stash->dwarf_line_buffer)
920 asection *msec;
922 msec = bfd_get_section_by_name (abfd, ".debug_line");
923 if (! msec)
925 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
926 bfd_set_error (bfd_error_bad_value);
927 return 0;
930 stash->dwarf_line_size = msec->size;
931 stash->dwarf_line_buffer
932 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
933 stash->syms);
934 if (! stash->dwarf_line_buffer)
935 return 0;
938 /* It is possible to get a bad value for the line_offset. Validate
939 it here so that we won't get a segfault below. */
940 if (unit->line_offset >= stash->dwarf_line_size)
942 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
943 unit->line_offset, stash->dwarf_line_size);
944 bfd_set_error (bfd_error_bad_value);
945 return 0;
948 amt = sizeof (struct line_info_table);
949 table = bfd_alloc (abfd, amt);
950 table->abfd = abfd;
951 table->comp_dir = unit->comp_dir;
953 table->num_files = 0;
954 table->files = NULL;
956 table->num_dirs = 0;
957 table->dirs = NULL;
959 table->files = NULL;
960 table->last_line = NULL;
961 table->lcl_head = NULL;
963 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
965 /* Read in the prologue. */
966 lh.total_length = read_4_bytes (abfd, line_ptr);
967 line_ptr += 4;
968 offset_size = 4;
969 if (lh.total_length == 0xffffffff)
971 lh.total_length = read_8_bytes (abfd, line_ptr);
972 line_ptr += 8;
973 offset_size = 8;
975 else if (lh.total_length == 0 && unit->addr_size == 8)
977 /* Handle (non-standard) 64-bit DWARF2 formats. */
978 lh.total_length = read_4_bytes (abfd, line_ptr);
979 line_ptr += 4;
980 offset_size = 8;
982 line_end = line_ptr + lh.total_length;
983 lh.version = read_2_bytes (abfd, line_ptr);
984 line_ptr += 2;
985 if (offset_size == 4)
986 lh.prologue_length = read_4_bytes (abfd, line_ptr);
987 else
988 lh.prologue_length = read_8_bytes (abfd, line_ptr);
989 line_ptr += offset_size;
990 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
991 line_ptr += 1;
992 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
993 line_ptr += 1;
994 lh.line_base = read_1_signed_byte (abfd, line_ptr);
995 line_ptr += 1;
996 lh.line_range = read_1_byte (abfd, line_ptr);
997 line_ptr += 1;
998 lh.opcode_base = read_1_byte (abfd, line_ptr);
999 line_ptr += 1;
1000 amt = lh.opcode_base * sizeof (unsigned char);
1001 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
1003 lh.standard_opcode_lengths[0] = 1;
1005 for (i = 1; i < lh.opcode_base; ++i)
1007 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1008 line_ptr += 1;
1011 /* Read directory table. */
1012 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1014 line_ptr += bytes_read;
1016 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1018 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1019 amt *= sizeof (char *);
1020 table->dirs = bfd_realloc (table->dirs, amt);
1021 if (! table->dirs)
1022 return 0;
1025 table->dirs[table->num_dirs++] = cur_dir;
1028 line_ptr += bytes_read;
1030 /* Read file name table. */
1031 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1033 line_ptr += bytes_read;
1035 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1037 amt = table->num_files + FILE_ALLOC_CHUNK;
1038 amt *= sizeof (struct fileinfo);
1039 table->files = bfd_realloc (table->files, amt);
1040 if (! table->files)
1041 return 0;
1044 table->files[table->num_files].name = cur_file;
1045 table->files[table->num_files].dir =
1046 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1047 line_ptr += bytes_read;
1048 table->files[table->num_files].time =
1049 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1050 line_ptr += bytes_read;
1051 table->files[table->num_files].size =
1052 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1053 line_ptr += bytes_read;
1054 table->num_files++;
1057 line_ptr += bytes_read;
1059 /* Read the statement sequences until there's nothing left. */
1060 while (line_ptr < line_end)
1062 /* State machine registers. */
1063 bfd_vma address = 0;
1064 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1065 unsigned int line = 1;
1066 unsigned int column = 0;
1067 int is_stmt = lh.default_is_stmt;
1068 int basic_block = 0;
1069 int end_sequence = 0;
1070 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1071 compilers generate address sequences that are wildly out of
1072 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1073 for ia64-Linux). Thus, to determine the low and high
1074 address, we must compare on every DW_LNS_copy, etc. */
1075 bfd_vma low_pc = 0;
1076 bfd_vma high_pc = 0;
1078 /* Decode the table. */
1079 while (! end_sequence)
1081 op_code = read_1_byte (abfd, line_ptr);
1082 line_ptr += 1;
1084 if (op_code >= lh.opcode_base)
1086 /* Special operand. */
1087 adj_opcode = op_code - lh.opcode_base;
1088 address += (adj_opcode / lh.line_range)
1089 * lh.minimum_instruction_length;
1090 line += lh.line_base + (adj_opcode % lh.line_range);
1091 /* Append row to matrix using current values. */
1092 add_line_info (table, address, filename, line, column, 0);
1093 basic_block = 1;
1094 if (low_pc == 0 || address < low_pc)
1095 low_pc = address;
1096 if (address > high_pc)
1097 high_pc = address;
1099 else switch (op_code)
1101 case DW_LNS_extended_op:
1102 /* Ignore length. */
1103 line_ptr += 1;
1104 extended_op = read_1_byte (abfd, line_ptr);
1105 line_ptr += 1;
1107 switch (extended_op)
1109 case DW_LNE_end_sequence:
1110 end_sequence = 1;
1111 add_line_info (table, address, filename, line, column,
1112 end_sequence);
1113 if (low_pc == 0 || address < low_pc)
1114 low_pc = address;
1115 if (address > high_pc)
1116 high_pc = address;
1117 arange_add (unit, low_pc, high_pc);
1118 break;
1119 case DW_LNE_set_address:
1120 address = read_address (unit, line_ptr);
1121 line_ptr += unit->addr_size;
1122 break;
1123 case DW_LNE_define_file:
1124 cur_file = read_string (abfd, line_ptr, &bytes_read);
1125 line_ptr += bytes_read;
1126 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1128 amt = table->num_files + FILE_ALLOC_CHUNK;
1129 amt *= sizeof (struct fileinfo);
1130 table->files = bfd_realloc (table->files, amt);
1131 if (! table->files)
1132 return 0;
1134 table->files[table->num_files].name = cur_file;
1135 table->files[table->num_files].dir =
1136 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1137 line_ptr += bytes_read;
1138 table->files[table->num_files].time =
1139 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1140 line_ptr += bytes_read;
1141 table->files[table->num_files].size =
1142 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1143 line_ptr += bytes_read;
1144 table->num_files++;
1145 break;
1146 default:
1147 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1148 bfd_set_error (bfd_error_bad_value);
1149 return 0;
1151 break;
1152 case DW_LNS_copy:
1153 add_line_info (table, address, filename, line, column, 0);
1154 basic_block = 0;
1155 if (low_pc == 0 || address < low_pc)
1156 low_pc = address;
1157 if (address > high_pc)
1158 high_pc = address;
1159 break;
1160 case DW_LNS_advance_pc:
1161 address += lh.minimum_instruction_length
1162 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1163 line_ptr += bytes_read;
1164 break;
1165 case DW_LNS_advance_line:
1166 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1167 line_ptr += bytes_read;
1168 break;
1169 case DW_LNS_set_file:
1171 unsigned int file;
1173 /* The file and directory tables are 0
1174 based, the references are 1 based. */
1175 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1176 line_ptr += bytes_read;
1177 if (filename)
1178 free (filename);
1179 filename = concat_filename (table, file);
1180 break;
1182 case DW_LNS_set_column:
1183 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1184 line_ptr += bytes_read;
1185 break;
1186 case DW_LNS_negate_stmt:
1187 is_stmt = (!is_stmt);
1188 break;
1189 case DW_LNS_set_basic_block:
1190 basic_block = 1;
1191 break;
1192 case DW_LNS_const_add_pc:
1193 address += lh.minimum_instruction_length
1194 * ((255 - lh.opcode_base) / lh.line_range);
1195 break;
1196 case DW_LNS_fixed_advance_pc:
1197 address += read_2_bytes (abfd, line_ptr);
1198 line_ptr += 2;
1199 break;
1200 default:
1202 int i;
1204 /* Unknown standard opcode, ignore it. */
1205 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1207 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1208 line_ptr += bytes_read;
1214 if (filename)
1215 free (filename);
1218 return table;
1221 /* If ADDR is within TABLE set the output parameters and return TRUE,
1222 otherwise return FALSE. The output parameters, FILENAME_PTR and
1223 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1225 static bfd_boolean
1226 lookup_address_in_line_info_table (struct line_info_table *table,
1227 bfd_vma addr,
1228 struct funcinfo *function,
1229 const char **filename_ptr,
1230 unsigned int *linenumber_ptr)
1232 /* Note: table->last_line should be a descendingly sorted list. */
1233 struct line_info* next_line = table->last_line;
1234 struct line_info* each_line = NULL;
1235 *filename_ptr = NULL;
1237 if (!next_line)
1238 return FALSE;
1240 each_line = next_line->prev_line;
1242 /* Check for large addresses */
1243 if (addr > next_line->address)
1244 each_line = NULL; /* ensure we skip over the normal case */
1246 /* Normal case: search the list; save */
1247 while (each_line && next_line)
1249 /* If we have an address match, save this info. This allows us
1250 to return as good as results as possible for strange debugging
1251 info. */
1252 bfd_boolean addr_match = FALSE;
1253 if (each_line->address <= addr && addr <= next_line->address)
1255 addr_match = TRUE;
1257 /* If this line appears to span functions, and addr is in the
1258 later function, return the first line of that function instead
1259 of the last line of the earlier one. This check is for GCC
1260 2.95, which emits the first line number for a function late. */
1261 if (function != NULL
1262 && each_line->address < function->low
1263 && next_line->address > function->low)
1265 *filename_ptr = next_line->filename;
1266 *linenumber_ptr = next_line->line;
1268 else
1270 *filename_ptr = each_line->filename;
1271 *linenumber_ptr = each_line->line;
1275 if (addr_match && !each_line->end_sequence)
1276 return TRUE; /* we have definitely found what we want */
1278 next_line = each_line;
1279 each_line = each_line->prev_line;
1282 /* At this point each_line is NULL but next_line is not. If we found
1283 a candidate end-of-sequence point in the loop above, we can return
1284 that (compatibility with a bug in the Intel compiler); otherwise,
1285 assuming that we found the containing function for this address in
1286 this compilation unit, return the first line we have a number for
1287 (compatibility with GCC 2.95). */
1288 if (*filename_ptr == NULL && function != NULL)
1290 *filename_ptr = next_line->filename;
1291 *linenumber_ptr = next_line->line;
1292 return TRUE;
1295 return FALSE;
1298 /* Function table functions. */
1300 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE. */
1302 static bfd_boolean
1303 lookup_address_in_function_table (struct funcinfo *table,
1304 bfd_vma addr,
1305 struct funcinfo **function_ptr,
1306 const char **functionname_ptr)
1308 struct funcinfo* each_func;
1310 for (each_func = table;
1311 each_func;
1312 each_func = each_func->prev_func)
1314 if (addr >= each_func->low && addr < each_func->high)
1316 *functionname_ptr = each_func->name;
1317 *function_ptr = each_func;
1318 return TRUE;
1322 return FALSE;
1325 static char *
1326 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1328 bfd *abfd = unit->abfd;
1329 char *info_ptr;
1330 unsigned int abbrev_number, bytes_read, i;
1331 struct abbrev_info *abbrev;
1332 struct attribute attr;
1333 char *name = 0;
1335 info_ptr = unit->stash->info_ptr_unit + die_ref;
1336 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1337 info_ptr += bytes_read;
1339 if (abbrev_number)
1341 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1342 if (! abbrev)
1344 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1345 abbrev_number);
1346 bfd_set_error (bfd_error_bad_value);
1348 else
1350 for (i = 0; i < abbrev->num_attrs && !name; ++i)
1352 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1353 if (attr.name == DW_AT_name)
1354 name = attr.u.str;
1358 return (name);
1361 /* DWARF2 Compilation unit functions. */
1363 /* Scan over each die in a comp. unit looking for functions to add
1364 to the function table. */
1366 static bfd_boolean
1367 scan_unit_for_functions (struct comp_unit *unit)
1369 bfd *abfd = unit->abfd;
1370 char *info_ptr = unit->first_child_die_ptr;
1371 int nesting_level = 1;
1373 while (nesting_level)
1375 unsigned int abbrev_number, bytes_read, i;
1376 struct abbrev_info *abbrev;
1377 struct attribute attr;
1378 struct funcinfo *func;
1379 char* name = 0;
1381 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1382 info_ptr += bytes_read;
1384 if (! abbrev_number)
1386 nesting_level--;
1387 continue;
1390 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1391 if (! abbrev)
1393 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1394 abbrev_number);
1395 bfd_set_error (bfd_error_bad_value);
1396 return FALSE;
1399 if (abbrev->tag == DW_TAG_subprogram
1400 || abbrev->tag == DW_TAG_inlined_subroutine)
1402 bfd_size_type amt = sizeof (struct funcinfo);
1403 func = bfd_zalloc (abfd, amt);
1404 func->prev_func = unit->function_table;
1405 unit->function_table = func;
1407 else
1408 func = NULL;
1410 for (i = 0; i < abbrev->num_attrs; ++i)
1412 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1414 if (func)
1416 switch (attr.name)
1418 case DW_AT_abstract_origin:
1419 func->name = find_abstract_instance_name (unit, attr.u.val);
1420 break;
1422 case DW_AT_name:
1424 name = attr.u.str;
1426 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1427 if (func->name == NULL)
1428 func->name = attr.u.str;
1429 break;
1431 case DW_AT_MIPS_linkage_name:
1432 func->name = attr.u.str;
1433 break;
1435 case DW_AT_low_pc:
1436 func->low = attr.u.val;
1437 break;
1439 case DW_AT_high_pc:
1440 func->high = attr.u.val;
1441 break;
1443 default:
1444 break;
1447 else
1449 switch (attr.name)
1451 case DW_AT_name:
1452 name = attr.u.str;
1453 break;
1455 default:
1456 break;
1461 if (abbrev->has_children)
1462 nesting_level++;
1465 return TRUE;
1468 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1469 includes the compilation unit header that proceeds the DIE's, but
1470 does not include the length field that precedes each compilation
1471 unit header. END_PTR points one past the end of this comp unit.
1472 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1474 This routine does not read the whole compilation unit; only enough
1475 to get to the line number information for the compilation unit. */
1477 static struct comp_unit *
1478 parse_comp_unit (bfd *abfd,
1479 struct dwarf2_debug *stash,
1480 bfd_vma unit_length,
1481 unsigned int offset_size)
1483 struct comp_unit* unit;
1484 unsigned int version;
1485 bfd_uint64_t abbrev_offset = 0;
1486 unsigned int addr_size;
1487 struct abbrev_info** abbrevs;
1488 unsigned int abbrev_number, bytes_read, i;
1489 struct abbrev_info *abbrev;
1490 struct attribute attr;
1491 char *info_ptr = stash->info_ptr;
1492 char *end_ptr = info_ptr + unit_length;
1493 bfd_size_type amt;
1495 version = read_2_bytes (abfd, info_ptr);
1496 info_ptr += 2;
1497 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1498 if (offset_size == 4)
1499 abbrev_offset = read_4_bytes (abfd, info_ptr);
1500 else
1501 abbrev_offset = read_8_bytes (abfd, info_ptr);
1502 info_ptr += offset_size;
1503 addr_size = read_1_byte (abfd, info_ptr);
1504 info_ptr += 1;
1506 if (version != 2)
1508 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1509 bfd_set_error (bfd_error_bad_value);
1510 return 0;
1513 if (addr_size > sizeof (bfd_vma))
1515 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1516 addr_size,
1517 (unsigned int) sizeof (bfd_vma));
1518 bfd_set_error (bfd_error_bad_value);
1519 return 0;
1522 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1524 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1525 bfd_set_error (bfd_error_bad_value);
1526 return 0;
1529 /* Read the abbrevs for this compilation unit into a table. */
1530 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1531 if (! abbrevs)
1532 return 0;
1534 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1535 info_ptr += bytes_read;
1536 if (! abbrev_number)
1538 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1539 abbrev_number);
1540 bfd_set_error (bfd_error_bad_value);
1541 return 0;
1544 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1545 if (! abbrev)
1547 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1548 abbrev_number);
1549 bfd_set_error (bfd_error_bad_value);
1550 return 0;
1553 amt = sizeof (struct comp_unit);
1554 unit = bfd_zalloc (abfd, amt);
1555 unit->abfd = abfd;
1556 unit->addr_size = addr_size;
1557 unit->offset_size = offset_size;
1558 unit->abbrevs = abbrevs;
1559 unit->end_ptr = end_ptr;
1560 unit->stash = stash;
1562 for (i = 0; i < abbrev->num_attrs; ++i)
1564 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1566 /* Store the data if it is of an attribute we want to keep in a
1567 partial symbol table. */
1568 switch (attr.name)
1570 case DW_AT_stmt_list:
1571 unit->stmtlist = 1;
1572 unit->line_offset = attr.u.val;
1573 break;
1575 case DW_AT_name:
1576 unit->name = attr.u.str;
1577 break;
1579 case DW_AT_low_pc:
1580 unit->arange.low = attr.u.val;
1581 break;
1583 case DW_AT_high_pc:
1584 unit->arange.high = attr.u.val;
1585 break;
1587 case DW_AT_comp_dir:
1589 char* comp_dir = attr.u.str;
1590 if (comp_dir)
1592 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1593 directory, get rid of it. */
1594 char *cp = strchr (comp_dir, ':');
1596 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1597 comp_dir = cp + 1;
1599 unit->comp_dir = comp_dir;
1600 break;
1603 default:
1604 break;
1608 unit->first_child_die_ptr = info_ptr;
1609 return unit;
1612 /* Return TRUE if UNIT contains the address given by ADDR. */
1614 static bfd_boolean
1615 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
1617 struct arange *arange;
1619 if (unit->error)
1620 return FALSE;
1622 arange = &unit->arange;
1625 if (addr >= arange->low && addr < arange->high)
1626 return TRUE;
1627 arange = arange->next;
1629 while (arange);
1631 return FALSE;
1634 /* If UNIT contains ADDR, set the output parameters to the values for
1635 the line containing ADDR. The output parameters, FILENAME_PTR,
1636 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1637 to be filled in.
1639 Return TRUE if UNIT contains ADDR, and no errors were encountered;
1640 FALSE otherwise. */
1642 static bfd_boolean
1643 comp_unit_find_nearest_line (struct comp_unit *unit,
1644 bfd_vma addr,
1645 const char **filename_ptr,
1646 const char **functionname_ptr,
1647 unsigned int *linenumber_ptr,
1648 struct dwarf2_debug *stash)
1650 bfd_boolean line_p;
1651 bfd_boolean func_p;
1652 struct funcinfo *function;
1654 if (unit->error)
1655 return FALSE;
1657 if (! unit->line_table)
1659 if (! unit->stmtlist)
1661 unit->error = 1;
1662 return FALSE;
1665 unit->line_table = decode_line_info (unit, stash);
1667 if (! unit->line_table)
1669 unit->error = 1;
1670 return FALSE;
1673 if (unit->first_child_die_ptr < unit->end_ptr
1674 && ! scan_unit_for_functions (unit))
1676 unit->error = 1;
1677 return FALSE;
1681 function = NULL;
1682 func_p = lookup_address_in_function_table (unit->function_table, addr,
1683 &function, functionname_ptr);
1684 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
1685 function, filename_ptr,
1686 linenumber_ptr);
1687 return line_p || func_p;
1690 /* Locate a section in a BFD containing debugging info. The search starts
1691 from the section after AFTER_SEC, or from the first section in the BFD if
1692 AFTER_SEC is NULL. The search works by examining the names of the
1693 sections. There are two permissiable names. The first is .debug_info.
1694 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
1695 This is a variation on the .debug_info section which has a checksum
1696 describing the contents appended onto the name. This allows the linker to
1697 identify and discard duplicate debugging sections for different
1698 compilation units. */
1699 #define DWARF2_DEBUG_INFO ".debug_info"
1700 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1702 static asection *
1703 find_debug_info (bfd *abfd, asection *after_sec)
1705 asection * msec;
1707 if (after_sec)
1708 msec = after_sec->next;
1709 else
1710 msec = abfd->sections;
1712 while (msec)
1714 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1715 return msec;
1717 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1718 return msec;
1720 msec = msec->next;
1723 return NULL;
1726 /* The DWARF2 version of find_nearest_line. Return TRUE if the line
1727 is found without error. ADDR_SIZE is the number of bytes in the
1728 initial .debug_info length field and in the abbreviation offset.
1729 You may use zero to indicate that the default value should be
1730 used. */
1732 bfd_boolean
1733 _bfd_dwarf2_find_nearest_line (bfd *abfd,
1734 asection *section,
1735 asymbol **symbols,
1736 bfd_vma offset,
1737 const char **filename_ptr,
1738 const char **functionname_ptr,
1739 unsigned int *linenumber_ptr,
1740 unsigned int addr_size,
1741 void **pinfo)
1743 /* Read each compilation unit from the section .debug_info, and check
1744 to see if it contains the address we are searching for. If yes,
1745 lookup the address, and return the line number info. If no, go
1746 on to the next compilation unit.
1748 We keep a list of all the previously read compilation units, and
1749 a pointer to the next un-read compilation unit. Check the
1750 previously read units before reading more. */
1751 struct dwarf2_debug *stash;
1753 /* What address are we looking for? */
1754 bfd_vma addr;
1756 struct comp_unit* each;
1758 stash = *pinfo;
1759 addr = offset;
1760 if (section->output_section)
1761 addr += section->output_section->vma + section->output_offset;
1762 else
1763 addr += section->vma;
1764 *filename_ptr = NULL;
1765 *functionname_ptr = NULL;
1766 *linenumber_ptr = 0;
1768 /* The DWARF2 spec says that the initial length field, and the
1769 offset of the abbreviation table, should both be 4-byte values.
1770 However, some compilers do things differently. */
1771 if (addr_size == 0)
1772 addr_size = 4;
1773 BFD_ASSERT (addr_size == 4 || addr_size == 8);
1775 if (! stash)
1777 bfd_size_type total_size;
1778 asection *msec;
1779 bfd_size_type amt = sizeof (struct dwarf2_debug);
1781 stash = bfd_zalloc (abfd, amt);
1782 if (! stash)
1783 return FALSE;
1785 *pinfo = stash;
1787 msec = find_debug_info (abfd, NULL);
1788 if (! msec)
1789 /* No dwarf2 info. Note that at this point the stash
1790 has been allocated, but contains zeros, this lets
1791 future calls to this function fail quicker. */
1792 return FALSE;
1794 /* There can be more than one DWARF2 info section in a BFD these days.
1795 Read them all in and produce one large stash. We do this in two
1796 passes - in the first pass we just accumulate the section sizes.
1797 In the second pass we read in the section's contents. The allows
1798 us to avoid reallocing the data as we add sections to the stash. */
1799 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1800 total_size += msec->size;
1802 stash->info_ptr = bfd_alloc (abfd, total_size);
1803 if (stash->info_ptr == NULL)
1804 return FALSE;
1806 stash->info_ptr_unit = stash->info_ptr;
1807 stash->info_ptr_end = stash->info_ptr;
1809 for (msec = find_debug_info (abfd, NULL);
1810 msec;
1811 msec = find_debug_info (abfd, msec))
1813 bfd_size_type size;
1814 bfd_size_type start;
1816 size = msec->size;
1817 if (size == 0)
1818 continue;
1820 start = stash->info_ptr_end - stash->info_ptr;
1822 if ((bfd_simple_get_relocated_section_contents
1823 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
1824 continue;
1826 stash->info_ptr_end = stash->info_ptr + start + size;
1829 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1831 stash->sec = find_debug_info (abfd, NULL);
1832 stash->sec_info_ptr = stash->info_ptr;
1833 stash->syms = symbols;
1836 /* A null info_ptr indicates that there is no dwarf2 info
1837 (or that an error occured while setting up the stash). */
1838 if (! stash->info_ptr)
1839 return FALSE;
1841 /* Check the previously read comp. units first. */
1842 for (each = stash->all_comp_units; each; each = each->next_unit)
1843 if (comp_unit_contains_address (each, addr))
1844 return comp_unit_find_nearest_line (each, addr, filename_ptr,
1845 functionname_ptr, linenumber_ptr,
1846 stash);
1848 /* Read each remaining comp. units checking each as they are read. */
1849 while (stash->info_ptr < stash->info_ptr_end)
1851 bfd_vma length;
1852 bfd_boolean found;
1853 unsigned int offset_size = addr_size;
1855 length = read_4_bytes (abfd, stash->info_ptr);
1856 /* A 0xffffff length is the DWARF3 way of indicating we use
1857 64-bit offsets, instead of 32-bit offsets. */
1858 if (length == 0xffffffff)
1860 offset_size = 8;
1861 length = read_8_bytes (abfd, stash->info_ptr + 4);
1862 stash->info_ptr += 12;
1864 /* A zero length is the IRIX way of indicating 64-bit offsets,
1865 mostly because the 64-bit length will generally fit in 32
1866 bits, and the endianness helps. */
1867 else if (length == 0)
1869 offset_size = 8;
1870 length = read_4_bytes (abfd, stash->info_ptr + 4);
1871 stash->info_ptr += 8;
1873 /* In the absence of the hints above, we assume addr_size-sized
1874 offsets, for backward-compatibility with pre-DWARF3 64-bit
1875 platforms. */
1876 else if (addr_size == 8)
1878 length = read_8_bytes (abfd, stash->info_ptr);
1879 stash->info_ptr += 8;
1881 else
1882 stash->info_ptr += 4;
1884 if (length > 0)
1886 each = parse_comp_unit (abfd, stash, length, offset_size);
1887 stash->info_ptr += length;
1889 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
1890 == stash->sec->size)
1892 stash->sec = find_debug_info (abfd, stash->sec);
1893 stash->sec_info_ptr = stash->info_ptr;
1896 if (each)
1898 each->next_unit = stash->all_comp_units;
1899 stash->all_comp_units = each;
1901 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1902 compilation units. If we don't have them (i.e.,
1903 unit->high == 0), we need to consult the line info
1904 table to see if a compilation unit contains the given
1905 address. */
1906 if (each->arange.high > 0)
1908 if (comp_unit_contains_address (each, addr))
1909 return comp_unit_find_nearest_line (each, addr,
1910 filename_ptr,
1911 functionname_ptr,
1912 linenumber_ptr,
1913 stash);
1915 else
1917 found = comp_unit_find_nearest_line (each, addr,
1918 filename_ptr,
1919 functionname_ptr,
1920 linenumber_ptr,
1921 stash);
1922 if (found)
1923 return TRUE;
1929 return FALSE;