* bfd/elf32-arm.h (arm_print_private_bfd_data): Add EABI v3.
[binutils.git] / bfd / dwarf2.c
blobceb28af47914bfc93577f76522407a488f28d6e5
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 /* Pointer to the end of the .debug_info section memory buffer. */
88 char* info_ptr_end;
90 /* Pointer to the section and address of the beginning of the
91 section. */
92 asection* sec;
93 char* sec_info_ptr;
95 /* Pointer to the symbol table. */
96 asymbol** syms;
98 /* Pointer to the .debug_abbrev section loaded into memory. */
99 char* dwarf_abbrev_buffer;
101 /* Length of the loaded .debug_abbrev section. */
102 unsigned long dwarf_abbrev_size;
104 /* Buffer for decode_line_info. */
105 char *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 char* dwarf_str_buffer;
113 /* Length of the loaded .debug_str section. */
114 unsigned long dwarf_str_size;
117 struct arange
119 struct arange *next;
120 bfd_vma low;
121 bfd_vma high;
124 /* A minimal decoding of DWARF2 compilation units. We only decode
125 what's needed to get to the line number information. */
127 struct comp_unit
129 /* Chain the previously read compilation units. */
130 struct comp_unit* next_unit;
132 /* Keep the bdf convenient (for memory allocation). */
133 bfd* abfd;
135 /* The lowest and higest addresses contained in this compilation
136 unit as specified in the compilation unit header. */
137 struct arange arange;
139 /* The DW_AT_name attribute (for error messages). */
140 char* name;
142 /* The abbrev hash table. */
143 struct abbrev_info** abbrevs;
145 /* Note that an error was found by comp_unit_find_nearest_line. */
146 int error;
148 /* The DW_AT_comp_dir attribute. */
149 char* comp_dir;
151 /* TRUE if there is a line number table associated with this comp. unit. */
152 int stmtlist;
154 /* The offset into .debug_line of the line number table. */
155 unsigned long line_offset;
157 /* Pointer to the first child die for the comp unit. */
158 char *first_child_die_ptr;
160 /* The end of the comp unit. */
161 char *end_ptr;
163 /* The decoded line number, NULL if not yet decoded. */
164 struct line_info_table* line_table;
166 /* A list of the functions found in this comp. unit. */
167 struct funcinfo* function_table;
169 /* Pointer to dwarf2_debug structure. */
170 struct dwarf2_debug *stash;
172 /* Address size for this unit - from unit header. */
173 unsigned char addr_size;
175 /* Offset size for this unit - from unit header. */
176 unsigned char offset_size;
179 /* This data structure holds the information of an abbrev. */
180 struct abbrev_info
182 unsigned int number; /* Number identifying abbrev. */
183 enum dwarf_tag tag; /* DWARF tag. */
184 int has_children; /* Boolean. */
185 unsigned int num_attrs; /* Number of attributes. */
186 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
187 struct abbrev_info *next; /* Next in chain. */
190 struct attr_abbrev
192 enum dwarf_attribute name;
193 enum dwarf_form form;
196 #ifndef ABBREV_HASH_SIZE
197 #define ABBREV_HASH_SIZE 121
198 #endif
199 #ifndef ATTR_ALLOC_CHUNK
200 #define ATTR_ALLOC_CHUNK 4
201 #endif
203 /* VERBATIM
204 The following function up to the END VERBATIM mark are
205 copied directly from dwarf2read.c. */
207 /* Read dwarf information from a buffer. */
209 static unsigned int
210 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, char *buf)
212 return bfd_get_8 (abfd, buf);
215 static int
216 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, char *buf)
218 return bfd_get_signed_8 (abfd, buf);
221 static unsigned int
222 read_2_bytes (bfd *abfd, char *buf)
224 return bfd_get_16 (abfd, buf);
227 static unsigned int
228 read_4_bytes (bfd *abfd, char *buf)
230 return bfd_get_32 (abfd, buf);
233 static bfd_uint64_t
234 read_8_bytes (bfd *abfd, char *buf)
236 return bfd_get_64 (abfd, buf);
239 static char *
240 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
241 char *buf,
242 unsigned int size ATTRIBUTE_UNUSED)
244 /* If the size of a host char is 8 bits, we can return a pointer
245 to the buffer, otherwise we have to copy the data to a buffer
246 allocated on the temporary obstack. */
247 return buf;
250 static char *
251 read_string (bfd *abfd ATTRIBUTE_UNUSED,
252 char *buf,
253 unsigned int *bytes_read_ptr)
255 /* Return a pointer to the embedded string. */
256 if (*buf == '\0')
258 *bytes_read_ptr = 1;
259 return NULL;
262 *bytes_read_ptr = strlen (buf) + 1;
263 return buf;
266 static char *
267 read_indirect_string (struct comp_unit* unit,
268 char *buf,
269 unsigned int *bytes_read_ptr)
271 bfd_uint64_t offset;
272 struct dwarf2_debug *stash = unit->stash;
274 if (unit->offset_size == 4)
275 offset = read_4_bytes (unit->abfd, buf);
276 else
277 offset = read_8_bytes (unit->abfd, buf);
278 *bytes_read_ptr = unit->offset_size;
280 if (! stash->dwarf_str_buffer)
282 asection *msec;
283 bfd *abfd = unit->abfd;
285 msec = bfd_get_section_by_name (abfd, ".debug_str");
286 if (! msec)
288 (*_bfd_error_handler)
289 (_("Dwarf Error: Can't find .debug_str section."));
290 bfd_set_error (bfd_error_bad_value);
291 return NULL;
294 stash->dwarf_str_size = msec->_raw_size;
295 stash->dwarf_str_buffer = bfd_alloc (abfd, msec->_raw_size);
296 if (! stash->dwarf_abbrev_buffer)
297 return NULL;
299 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
300 0, msec->_raw_size))
301 return NULL;
304 if (offset >= stash->dwarf_str_size)
306 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
307 (unsigned long) offset, stash->dwarf_str_size);
308 bfd_set_error (bfd_error_bad_value);
309 return NULL;
312 buf = stash->dwarf_str_buffer + offset;
313 if (*buf == '\0')
314 return NULL;
315 return buf;
318 static unsigned int
319 read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
320 char *buf,
321 unsigned int *bytes_read_ptr)
323 unsigned int result;
324 unsigned int num_read;
325 int shift;
326 unsigned char byte;
328 result = 0;
329 shift = 0;
330 num_read = 0;
334 byte = bfd_get_8 (abfd, buf);
335 buf ++;
336 num_read ++;
337 result |= ((byte & 0x7f) << shift);
338 shift += 7;
340 while (byte & 0x80);
342 * bytes_read_ptr = num_read;
344 return result;
347 static int
348 read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
349 char *buf,
350 unsigned int * bytes_read_ptr)
352 int result;
353 int shift;
354 int num_read;
355 unsigned char byte;
357 result = 0;
358 shift = 0;
359 num_read = 0;
363 byte = bfd_get_8 (abfd, buf);
364 buf ++;
365 num_read ++;
366 result |= ((byte & 0x7f) << shift);
367 shift += 7;
369 while (byte & 0x80);
371 if ((shift < 32) && (byte & 0x40))
372 result |= -(1 << shift);
374 * bytes_read_ptr = num_read;
376 return result;
379 /* END VERBATIM */
381 static bfd_uint64_t
382 read_address (struct comp_unit *unit, char *buf)
384 switch (unit->addr_size)
386 case 8:
387 return bfd_get_64 (unit->abfd, buf);
388 case 4:
389 return bfd_get_32 (unit->abfd, buf);
390 case 2:
391 return bfd_get_16 (unit->abfd, buf);
392 default:
393 abort ();
397 /* Lookup an abbrev_info structure in the abbrev hash table. */
399 static struct abbrev_info *
400 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
402 unsigned int hash_number;
403 struct abbrev_info *abbrev;
405 hash_number = number % ABBREV_HASH_SIZE;
406 abbrev = abbrevs[hash_number];
408 while (abbrev)
410 if (abbrev->number == number)
411 return abbrev;
412 else
413 abbrev = abbrev->next;
416 return NULL;
419 /* In DWARF version 2, the description of the debugging information is
420 stored in a separate .debug_abbrev section. Before we read any
421 dies from a section we read in all abbreviations and install them
422 in a hash table. */
424 static struct abbrev_info**
425 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
427 struct abbrev_info **abbrevs;
428 char *abbrev_ptr;
429 struct abbrev_info *cur_abbrev;
430 unsigned int abbrev_number, bytes_read, abbrev_name;
431 unsigned int abbrev_form, hash_number;
432 bfd_size_type amt;
434 if (! stash->dwarf_abbrev_buffer)
436 asection *msec;
438 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
439 if (! msec)
441 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
442 bfd_set_error (bfd_error_bad_value);
443 return 0;
446 stash->dwarf_abbrev_size = msec->_raw_size;
447 stash->dwarf_abbrev_buffer
448 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
449 stash->syms);
450 if (! stash->dwarf_abbrev_buffer)
451 return 0;
454 if (offset >= stash->dwarf_abbrev_size)
456 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
457 (unsigned long) offset, stash->dwarf_abbrev_size);
458 bfd_set_error (bfd_error_bad_value);
459 return 0;
462 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
463 abbrevs = bfd_zalloc (abfd, amt);
465 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
466 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
467 abbrev_ptr += bytes_read;
469 /* Loop until we reach an abbrev number of 0. */
470 while (abbrev_number)
472 amt = sizeof (struct abbrev_info);
473 cur_abbrev = bfd_zalloc (abfd, amt);
475 /* Read in abbrev header. */
476 cur_abbrev->number = abbrev_number;
477 cur_abbrev->tag = (enum dwarf_tag)
478 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
479 abbrev_ptr += bytes_read;
480 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
481 abbrev_ptr += 1;
483 /* Now read in declarations. */
484 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
485 abbrev_ptr += bytes_read;
486 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
487 abbrev_ptr += bytes_read;
489 while (abbrev_name)
491 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
493 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
494 amt *= sizeof (struct attr_abbrev);
495 cur_abbrev->attrs = bfd_realloc (cur_abbrev->attrs, amt);
496 if (! cur_abbrev->attrs)
497 return 0;
500 cur_abbrev->attrs[cur_abbrev->num_attrs].name
501 = (enum dwarf_attribute) abbrev_name;
502 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
503 = (enum dwarf_form) abbrev_form;
504 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
505 abbrev_ptr += bytes_read;
506 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
507 abbrev_ptr += bytes_read;
510 hash_number = abbrev_number % ABBREV_HASH_SIZE;
511 cur_abbrev->next = abbrevs[hash_number];
512 abbrevs[hash_number] = cur_abbrev;
514 /* Get next abbreviation.
515 Under Irix6 the abbreviations for a compilation unit are not
516 always properly terminated with an abbrev number of 0.
517 Exit loop if we encounter an abbreviation which we have
518 already read (which means we are about to read the abbreviations
519 for the next compile unit) or if the end of the abbreviation
520 table is reached. */
521 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
522 >= stash->dwarf_abbrev_size)
523 break;
524 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
525 abbrev_ptr += bytes_read;
526 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
527 break;
530 return abbrevs;
533 /* Read an attribute value described by an attribute form. */
535 static char *
536 read_attribute_value (struct attribute *attr,
537 unsigned form,
538 struct comp_unit *unit,
539 char *info_ptr)
541 bfd *abfd = unit->abfd;
542 unsigned int bytes_read;
543 struct dwarf_block *blk;
544 bfd_size_type amt;
546 attr->form = (enum dwarf_form) form;
548 switch (form)
550 case DW_FORM_addr:
551 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
552 case DW_FORM_ref_addr:
553 attr->u.val = read_address (unit, info_ptr);
554 info_ptr += unit->addr_size;
555 break;
556 case DW_FORM_block2:
557 amt = sizeof (struct dwarf_block);
558 blk = bfd_alloc (abfd, amt);
559 blk->size = read_2_bytes (abfd, info_ptr);
560 info_ptr += 2;
561 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
562 info_ptr += blk->size;
563 attr->u.blk = blk;
564 break;
565 case DW_FORM_block4:
566 amt = sizeof (struct dwarf_block);
567 blk = bfd_alloc (abfd, amt);
568 blk->size = read_4_bytes (abfd, info_ptr);
569 info_ptr += 4;
570 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
571 info_ptr += blk->size;
572 attr->u.blk = blk;
573 break;
574 case DW_FORM_data2:
575 attr->u.val = read_2_bytes (abfd, info_ptr);
576 info_ptr += 2;
577 break;
578 case DW_FORM_data4:
579 attr->u.val = read_4_bytes (abfd, info_ptr);
580 info_ptr += 4;
581 break;
582 case DW_FORM_data8:
583 attr->u.val = read_8_bytes (abfd, info_ptr);
584 info_ptr += 8;
585 break;
586 case DW_FORM_string:
587 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
588 info_ptr += bytes_read;
589 break;
590 case DW_FORM_strp:
591 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
592 info_ptr += bytes_read;
593 break;
594 case DW_FORM_block:
595 amt = sizeof (struct dwarf_block);
596 blk = bfd_alloc (abfd, amt);
597 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
598 info_ptr += bytes_read;
599 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
600 info_ptr += blk->size;
601 attr->u.blk = blk;
602 break;
603 case DW_FORM_block1:
604 amt = sizeof (struct dwarf_block);
605 blk = bfd_alloc (abfd, amt);
606 blk->size = read_1_byte (abfd, info_ptr);
607 info_ptr += 1;
608 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
609 info_ptr += blk->size;
610 attr->u.blk = blk;
611 break;
612 case DW_FORM_data1:
613 attr->u.val = read_1_byte (abfd, info_ptr);
614 info_ptr += 1;
615 break;
616 case DW_FORM_flag:
617 attr->u.val = read_1_byte (abfd, info_ptr);
618 info_ptr += 1;
619 break;
620 case DW_FORM_sdata:
621 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
622 info_ptr += bytes_read;
623 break;
624 case DW_FORM_udata:
625 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
626 info_ptr += bytes_read;
627 break;
628 case DW_FORM_ref1:
629 attr->u.val = read_1_byte (abfd, info_ptr);
630 info_ptr += 1;
631 break;
632 case DW_FORM_ref2:
633 attr->u.val = read_2_bytes (abfd, info_ptr);
634 info_ptr += 2;
635 break;
636 case DW_FORM_ref4:
637 attr->u.val = read_4_bytes (abfd, info_ptr);
638 info_ptr += 4;
639 break;
640 case DW_FORM_ref8:
641 attr->u.val = read_8_bytes (abfd, info_ptr);
642 info_ptr += 8;
643 break;
644 case DW_FORM_ref_udata:
645 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
646 info_ptr += bytes_read;
647 break;
648 case DW_FORM_indirect:
649 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
650 info_ptr += bytes_read;
651 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
652 break;
653 default:
654 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
655 form);
656 bfd_set_error (bfd_error_bad_value);
658 return info_ptr;
661 /* Read an attribute described by an abbreviated attribute. */
663 static char *
664 read_attribute (struct attribute *attr,
665 struct attr_abbrev *abbrev,
666 struct comp_unit *unit,
667 char *info_ptr)
669 attr->name = abbrev->name;
670 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
671 return info_ptr;
674 /* Source line information table routines. */
676 #define FILE_ALLOC_CHUNK 5
677 #define DIR_ALLOC_CHUNK 5
679 struct line_info
681 struct line_info* prev_line;
682 bfd_vma address;
683 char* filename;
684 unsigned int line;
685 unsigned int column;
686 int end_sequence; /* End of (sequential) code sequence. */
689 struct fileinfo
691 char *name;
692 unsigned int dir;
693 unsigned int time;
694 unsigned int size;
697 struct line_info_table
699 bfd* abfd;
700 unsigned int num_files;
701 unsigned int num_dirs;
702 char* comp_dir;
703 char** dirs;
704 struct fileinfo* files;
705 struct line_info* last_line; /* largest VMA */
706 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
709 struct funcinfo
711 struct funcinfo *prev_func;
712 char* name;
713 bfd_vma low;
714 bfd_vma high;
717 /* Adds a new entry to the line_info list in the line_info_table, ensuring
718 that the list is sorted. Note that the line_info list is sorted from
719 highest to lowest VMA (with possible duplicates); that is,
720 line_info->prev_line always accesses an equal or smaller VMA. */
722 static void
723 add_line_info (struct line_info_table *table,
724 bfd_vma address,
725 char *filename,
726 unsigned int line,
727 unsigned int column,
728 int end_sequence)
730 bfd_size_type amt = sizeof (struct line_info);
731 struct line_info* info = bfd_alloc (table->abfd, amt);
733 /* Find the correct location for 'info'. Normally we will receive
734 new line_info data 1) in order and 2) with increasing VMAs.
735 However some compilers break the rules (cf. decode_line_info) and
736 so we include some heuristics for quickly finding the correct
737 location for 'info'. In particular, these heuristics optimize for
738 the common case in which the VMA sequence that we receive is a
739 list of locally sorted VMAs such as
740 p...z a...j (where a < j < p < z)
742 Note: table->lcl_head is used to head an *actual* or *possible*
743 sequence within the list (such as a...j) that is not directly
744 headed by table->last_line
746 Note: we may receive duplicate entries from 'decode_line_info'. */
748 while (1)
749 if (!table->last_line
750 || address >= table->last_line->address)
752 /* Normal case: add 'info' to the beginning of the list */
753 info->prev_line = table->last_line;
754 table->last_line = info;
756 /* lcl_head: initialize to head a *possible* sequence at the end. */
757 if (!table->lcl_head)
758 table->lcl_head = info;
759 break;
761 else if (!table->lcl_head->prev_line
762 && table->lcl_head->address > address)
764 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
765 list and 2) the head of 'info'. */
766 info->prev_line = NULL;
767 table->lcl_head->prev_line = info;
768 break;
770 else if (table->lcl_head->prev_line
771 && table->lcl_head->address > address
772 && address >= table->lcl_head->prev_line->address)
774 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
775 list and 2) the head of 'info'. */
776 info->prev_line = table->lcl_head->prev_line;
777 table->lcl_head->prev_line = info;
778 break;
780 else
782 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
783 heads for 'info'. Reset 'lcl_head' and repeat. */
784 struct line_info* li2 = table->last_line; /* always non-NULL */
785 struct line_info* li1 = li2->prev_line;
787 while (li1)
789 if (li2->address > address && address >= li1->address)
790 break;
792 li2 = li1; /* always non-NULL */
793 li1 = li1->prev_line;
795 table->lcl_head = li2;
798 /* Set member data of 'info'. */
799 info->address = address;
800 info->line = line;
801 info->column = column;
802 info->end_sequence = end_sequence;
804 amt = strlen (filename);
805 if (amt)
807 info->filename = bfd_alloc (table->abfd, amt + 1);
808 if (info->filename)
809 strcpy (info->filename, filename);
811 else
812 info->filename = NULL;
815 /* Extract a fully qualified filename from a line info table.
816 The returned string has been malloc'ed and it is the caller's
817 responsibility to free it. */
819 static char *
820 concat_filename (struct line_info_table *table, unsigned int file)
822 char* filename;
824 if (file - 1 >= table->num_files)
826 (*_bfd_error_handler)
827 (_("Dwarf Error: mangled line number section (bad file number)."));
828 return strdup ("<unknown>");
831 filename = table->files[file - 1].name;
833 if (! IS_ABSOLUTE_PATH (filename))
835 char* dirname = (table->files[file - 1].dir
836 ? table->dirs[table->files[file - 1].dir - 1]
837 : table->comp_dir);
839 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
840 The best we can do is return the filename part. */
841 if (dirname != NULL)
843 unsigned int len = strlen (dirname) + strlen (filename) + 2;
844 char * name;
846 name = bfd_malloc (len);
847 if (name)
848 sprintf (name, "%s/%s", dirname, filename);
849 return name;
853 return strdup (filename);
856 static void
857 arange_add (struct comp_unit *unit, bfd_vma low_pc, bfd_vma high_pc)
859 struct arange *arange;
861 /* First see if we can cheaply extend an existing range. */
862 arange = &unit->arange;
866 if (low_pc == arange->high)
868 arange->high = high_pc;
869 return;
871 if (high_pc == arange->low)
873 arange->low = low_pc;
874 return;
876 arange = arange->next;
878 while (arange);
880 if (unit->arange.high == 0)
882 /* This is the first address range: store it in unit->arange. */
883 unit->arange.next = 0;
884 unit->arange.low = low_pc;
885 unit->arange.high = high_pc;
886 return;
889 /* Need to allocate a new arange and insert it into the arange list. */
890 arange = bfd_zalloc (unit->abfd, sizeof (*arange));
891 arange->low = low_pc;
892 arange->high = high_pc;
894 arange->next = unit->arange.next;
895 unit->arange.next = arange;
898 /* Decode the line number information for UNIT. */
900 static struct line_info_table*
901 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
903 bfd *abfd = unit->abfd;
904 struct line_info_table* table;
905 char *line_ptr;
906 char *line_end;
907 struct line_head lh;
908 unsigned int i, bytes_read, offset_size;
909 char *cur_file, *cur_dir;
910 unsigned char op_code, extended_op, adj_opcode;
911 bfd_size_type amt;
913 if (! stash->dwarf_line_buffer)
915 asection *msec;
917 msec = bfd_get_section_by_name (abfd, ".debug_line");
918 if (! msec)
920 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
921 bfd_set_error (bfd_error_bad_value);
922 return 0;
925 stash->dwarf_line_size = msec->_raw_size;
926 stash->dwarf_line_buffer
927 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
928 stash->syms);
929 if (! stash->dwarf_line_buffer)
930 return 0;
933 /* It is possible to get a bad value for the line_offset. Validate
934 it here so that we won't get a segfault below. */
935 if (unit->line_offset >= stash->dwarf_line_size)
937 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
938 unit->line_offset, stash->dwarf_line_size);
939 bfd_set_error (bfd_error_bad_value);
940 return 0;
943 amt = sizeof (struct line_info_table);
944 table = bfd_alloc (abfd, amt);
945 table->abfd = abfd;
946 table->comp_dir = unit->comp_dir;
948 table->num_files = 0;
949 table->files = NULL;
951 table->num_dirs = 0;
952 table->dirs = NULL;
954 table->files = NULL;
955 table->last_line = NULL;
956 table->lcl_head = NULL;
958 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
960 /* Read in the prologue. */
961 lh.total_length = read_4_bytes (abfd, line_ptr);
962 line_ptr += 4;
963 offset_size = 4;
964 if (lh.total_length == 0xffffffff)
966 lh.total_length = read_8_bytes (abfd, line_ptr);
967 line_ptr += 8;
968 offset_size = 8;
970 else if (lh.total_length == 0 && unit->addr_size == 8)
972 /* Handle (non-standard) 64-bit DWARF2 formats. */
973 lh.total_length = read_4_bytes (abfd, line_ptr);
974 line_ptr += 4;
975 offset_size = 8;
977 line_end = line_ptr + lh.total_length;
978 lh.version = read_2_bytes (abfd, line_ptr);
979 line_ptr += 2;
980 if (offset_size == 4)
981 lh.prologue_length = read_4_bytes (abfd, line_ptr);
982 else
983 lh.prologue_length = read_8_bytes (abfd, line_ptr);
984 line_ptr += offset_size;
985 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
986 line_ptr += 1;
987 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
988 line_ptr += 1;
989 lh.line_base = read_1_signed_byte (abfd, line_ptr);
990 line_ptr += 1;
991 lh.line_range = read_1_byte (abfd, line_ptr);
992 line_ptr += 1;
993 lh.opcode_base = read_1_byte (abfd, line_ptr);
994 line_ptr += 1;
995 amt = lh.opcode_base * sizeof (unsigned char);
996 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
998 lh.standard_opcode_lengths[0] = 1;
1000 for (i = 1; i < lh.opcode_base; ++i)
1002 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1003 line_ptr += 1;
1006 /* Read directory table. */
1007 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1009 line_ptr += bytes_read;
1011 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1013 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1014 amt *= sizeof (char *);
1015 table->dirs = bfd_realloc (table->dirs, amt);
1016 if (! table->dirs)
1017 return 0;
1020 table->dirs[table->num_dirs++] = cur_dir;
1023 line_ptr += bytes_read;
1025 /* Read file name table. */
1026 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1028 line_ptr += bytes_read;
1030 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1032 amt = table->num_files + FILE_ALLOC_CHUNK;
1033 amt *= sizeof (struct fileinfo);
1034 table->files = bfd_realloc (table->files, amt);
1035 if (! table->files)
1036 return 0;
1039 table->files[table->num_files].name = cur_file;
1040 table->files[table->num_files].dir =
1041 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1042 line_ptr += bytes_read;
1043 table->files[table->num_files].time =
1044 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1045 line_ptr += bytes_read;
1046 table->files[table->num_files].size =
1047 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1048 line_ptr += bytes_read;
1049 table->num_files++;
1052 line_ptr += bytes_read;
1054 /* Read the statement sequences until there's nothing left. */
1055 while (line_ptr < line_end)
1057 /* State machine registers. */
1058 bfd_vma address = 0;
1059 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1060 unsigned int line = 1;
1061 unsigned int column = 0;
1062 int is_stmt = lh.default_is_stmt;
1063 int basic_block = 0;
1064 int end_sequence = 0;
1065 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1066 compilers generate address sequences that are wildly out of
1067 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1068 for ia64-Linux). Thus, to determine the low and high
1069 address, we must compare on every DW_LNS_copy, etc. */
1070 bfd_vma low_pc = 0;
1071 bfd_vma high_pc = 0;
1073 /* Decode the table. */
1074 while (! end_sequence)
1076 op_code = read_1_byte (abfd, line_ptr);
1077 line_ptr += 1;
1079 if (op_code >= lh.opcode_base)
1081 /* Special operand. */
1082 adj_opcode = op_code - lh.opcode_base;
1083 address += (adj_opcode / lh.line_range)
1084 * lh.minimum_instruction_length;
1085 line += lh.line_base + (adj_opcode % lh.line_range);
1086 /* Append row to matrix using current values. */
1087 add_line_info (table, address, filename, line, column, 0);
1088 basic_block = 1;
1089 if (low_pc == 0 || address < low_pc)
1090 low_pc = address;
1091 if (address > high_pc)
1092 high_pc = address;
1094 else switch (op_code)
1096 case DW_LNS_extended_op:
1097 /* Ignore length. */
1098 line_ptr += 1;
1099 extended_op = read_1_byte (abfd, line_ptr);
1100 line_ptr += 1;
1102 switch (extended_op)
1104 case DW_LNE_end_sequence:
1105 end_sequence = 1;
1106 add_line_info (table, address, filename, line, column,
1107 end_sequence);
1108 if (low_pc == 0 || address < low_pc)
1109 low_pc = address;
1110 if (address > high_pc)
1111 high_pc = address;
1112 arange_add (unit, low_pc, high_pc);
1113 break;
1114 case DW_LNE_set_address:
1115 address = read_address (unit, line_ptr);
1116 line_ptr += unit->addr_size;
1117 break;
1118 case DW_LNE_define_file:
1119 cur_file = read_string (abfd, line_ptr, &bytes_read);
1120 line_ptr += bytes_read;
1121 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1123 amt = table->num_files + FILE_ALLOC_CHUNK;
1124 amt *= sizeof (struct fileinfo);
1125 table->files = bfd_realloc (table->files, amt);
1126 if (! table->files)
1127 return 0;
1129 table->files[table->num_files].name = cur_file;
1130 table->files[table->num_files].dir =
1131 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1132 line_ptr += bytes_read;
1133 table->files[table->num_files].time =
1134 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1135 line_ptr += bytes_read;
1136 table->files[table->num_files].size =
1137 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1138 line_ptr += bytes_read;
1139 table->num_files++;
1140 break;
1141 default:
1142 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1143 bfd_set_error (bfd_error_bad_value);
1144 return 0;
1146 break;
1147 case DW_LNS_copy:
1148 add_line_info (table, address, filename, line, column, 0);
1149 basic_block = 0;
1150 if (low_pc == 0 || address < low_pc)
1151 low_pc = address;
1152 if (address > high_pc)
1153 high_pc = address;
1154 break;
1155 case DW_LNS_advance_pc:
1156 address += lh.minimum_instruction_length
1157 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1158 line_ptr += bytes_read;
1159 break;
1160 case DW_LNS_advance_line:
1161 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1162 line_ptr += bytes_read;
1163 break;
1164 case DW_LNS_set_file:
1166 unsigned int file;
1168 /* The file and directory tables are 0
1169 based, the references are 1 based. */
1170 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1171 line_ptr += bytes_read;
1172 if (filename)
1173 free (filename);
1174 filename = concat_filename (table, file);
1175 break;
1177 case DW_LNS_set_column:
1178 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1179 line_ptr += bytes_read;
1180 break;
1181 case DW_LNS_negate_stmt:
1182 is_stmt = (!is_stmt);
1183 break;
1184 case DW_LNS_set_basic_block:
1185 basic_block = 1;
1186 break;
1187 case DW_LNS_const_add_pc:
1188 address += lh.minimum_instruction_length
1189 * ((255 - lh.opcode_base) / lh.line_range);
1190 break;
1191 case DW_LNS_fixed_advance_pc:
1192 address += read_2_bytes (abfd, line_ptr);
1193 line_ptr += 2;
1194 break;
1195 default:
1197 int i;
1199 /* Unknown standard opcode, ignore it. */
1200 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1202 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1203 line_ptr += bytes_read;
1209 if (filename)
1210 free (filename);
1213 return table;
1216 /* If ADDR is within TABLE set the output parameters and return TRUE,
1217 otherwise return FALSE. The output parameters, FILENAME_PTR and
1218 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1220 static bfd_boolean
1221 lookup_address_in_line_info_table (struct line_info_table *table,
1222 bfd_vma addr,
1223 struct funcinfo *function,
1224 const char **filename_ptr,
1225 unsigned int *linenumber_ptr)
1227 /* Note: table->last_line should be a descendingly sorted list. */
1228 struct line_info* next_line = table->last_line;
1229 struct line_info* each_line = NULL;
1230 *filename_ptr = NULL;
1232 if (!next_line)
1233 return FALSE;
1235 each_line = next_line->prev_line;
1237 /* Check for large addresses */
1238 if (addr > next_line->address)
1239 each_line = NULL; /* ensure we skip over the normal case */
1241 /* Normal case: search the list; save */
1242 while (each_line && next_line)
1244 /* If we have an address match, save this info. This allows us
1245 to return as good as results as possible for strange debugging
1246 info. */
1247 bfd_boolean addr_match = FALSE;
1248 if (each_line->address <= addr && addr <= next_line->address)
1250 addr_match = TRUE;
1252 /* If this line appears to span functions, and addr is in the
1253 later function, return the first line of that function instead
1254 of the last line of the earlier one. This check is for GCC
1255 2.95, which emits the first line number for a function late. */
1256 if (function != NULL
1257 && each_line->address < function->low
1258 && next_line->address > function->low)
1260 *filename_ptr = next_line->filename;
1261 *linenumber_ptr = next_line->line;
1263 else
1265 *filename_ptr = each_line->filename;
1266 *linenumber_ptr = each_line->line;
1270 if (addr_match && !each_line->end_sequence)
1271 return TRUE; /* we have definitely found what we want */
1273 next_line = each_line;
1274 each_line = each_line->prev_line;
1277 /* At this point each_line is NULL but next_line is not. If we found
1278 a candidate end-of-sequence point in the loop above, we can return
1279 that (compatibility with a bug in the Intel compiler); otherwise,
1280 assuming that we found the containing function for this address in
1281 this compilation unit, return the first line we have a number for
1282 (compatibility with GCC 2.95). */
1283 if (*filename_ptr == NULL && function != NULL)
1285 *filename_ptr = next_line->filename;
1286 *linenumber_ptr = next_line->line;
1287 return TRUE;
1290 return FALSE;
1293 /* Function table functions. */
1295 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE. */
1297 static bfd_boolean
1298 lookup_address_in_function_table (struct funcinfo *table,
1299 bfd_vma addr,
1300 struct funcinfo **function_ptr,
1301 const char **functionname_ptr)
1303 struct funcinfo* each_func;
1305 for (each_func = table;
1306 each_func;
1307 each_func = each_func->prev_func)
1309 if (addr >= each_func->low && addr < each_func->high)
1311 *functionname_ptr = each_func->name;
1312 *function_ptr = each_func;
1313 return TRUE;
1317 return FALSE;
1320 /* DWARF2 Compilation unit functions. */
1322 /* Scan over each die in a comp. unit looking for functions to add
1323 to the function table. */
1325 static bfd_boolean
1326 scan_unit_for_functions (struct comp_unit *unit)
1328 bfd *abfd = unit->abfd;
1329 char *info_ptr = unit->first_child_die_ptr;
1330 int nesting_level = 1;
1332 while (nesting_level)
1334 unsigned int abbrev_number, bytes_read, i;
1335 struct abbrev_info *abbrev;
1336 struct attribute attr;
1337 struct funcinfo *func;
1338 char* name = 0;
1340 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1341 info_ptr += bytes_read;
1343 if (! abbrev_number)
1345 nesting_level--;
1346 continue;
1349 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1350 if (! abbrev)
1352 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1353 abbrev_number);
1354 bfd_set_error (bfd_error_bad_value);
1355 return FALSE;
1358 if (abbrev->tag == DW_TAG_subprogram)
1360 bfd_size_type amt = sizeof (struct funcinfo);
1361 func = bfd_zalloc (abfd, amt);
1362 func->prev_func = unit->function_table;
1363 unit->function_table = func;
1365 else
1366 func = NULL;
1368 for (i = 0; i < abbrev->num_attrs; ++i)
1370 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1372 if (func)
1374 switch (attr.name)
1376 case DW_AT_name:
1378 name = attr.u.str;
1380 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1381 if (func->name == NULL)
1382 func->name = attr.u.str;
1383 break;
1385 case DW_AT_MIPS_linkage_name:
1386 func->name = attr.u.str;
1387 break;
1389 case DW_AT_low_pc:
1390 func->low = attr.u.val;
1391 break;
1393 case DW_AT_high_pc:
1394 func->high = attr.u.val;
1395 break;
1397 default:
1398 break;
1401 else
1403 switch (attr.name)
1405 case DW_AT_name:
1406 name = attr.u.str;
1407 break;
1409 default:
1410 break;
1415 if (abbrev->has_children)
1416 nesting_level++;
1419 return TRUE;
1422 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1423 includes the compilation unit header that proceeds the DIE's, but
1424 does not include the length field that precedes each compilation
1425 unit header. END_PTR points one past the end of this comp unit.
1426 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1428 This routine does not read the whole compilation unit; only enough
1429 to get to the line number information for the compilation unit. */
1431 static struct comp_unit *
1432 parse_comp_unit (bfd *abfd,
1433 struct dwarf2_debug *stash,
1434 bfd_vma unit_length,
1435 unsigned int offset_size)
1437 struct comp_unit* unit;
1438 unsigned int version;
1439 bfd_uint64_t abbrev_offset = 0;
1440 unsigned int addr_size;
1441 struct abbrev_info** abbrevs;
1442 unsigned int abbrev_number, bytes_read, i;
1443 struct abbrev_info *abbrev;
1444 struct attribute attr;
1445 char *info_ptr = stash->info_ptr;
1446 char *end_ptr = info_ptr + unit_length;
1447 bfd_size_type amt;
1449 version = read_2_bytes (abfd, info_ptr);
1450 info_ptr += 2;
1451 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1452 if (offset_size == 4)
1453 abbrev_offset = read_4_bytes (abfd, info_ptr);
1454 else
1455 abbrev_offset = read_8_bytes (abfd, info_ptr);
1456 info_ptr += offset_size;
1457 addr_size = read_1_byte (abfd, info_ptr);
1458 info_ptr += 1;
1460 if (version != 2)
1462 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1463 bfd_set_error (bfd_error_bad_value);
1464 return 0;
1467 if (addr_size > sizeof (bfd_vma))
1469 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1470 addr_size,
1471 (unsigned int) sizeof (bfd_vma));
1472 bfd_set_error (bfd_error_bad_value);
1473 return 0;
1476 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1478 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1479 bfd_set_error (bfd_error_bad_value);
1480 return 0;
1483 /* Read the abbrevs for this compilation unit into a table. */
1484 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1485 if (! abbrevs)
1486 return 0;
1488 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1489 info_ptr += bytes_read;
1490 if (! abbrev_number)
1492 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1493 abbrev_number);
1494 bfd_set_error (bfd_error_bad_value);
1495 return 0;
1498 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1499 if (! abbrev)
1501 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1502 abbrev_number);
1503 bfd_set_error (bfd_error_bad_value);
1504 return 0;
1507 amt = sizeof (struct comp_unit);
1508 unit = bfd_zalloc (abfd, amt);
1509 unit->abfd = abfd;
1510 unit->addr_size = addr_size;
1511 unit->offset_size = offset_size;
1512 unit->abbrevs = abbrevs;
1513 unit->end_ptr = end_ptr;
1514 unit->stash = stash;
1516 for (i = 0; i < abbrev->num_attrs; ++i)
1518 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1520 /* Store the data if it is of an attribute we want to keep in a
1521 partial symbol table. */
1522 switch (attr.name)
1524 case DW_AT_stmt_list:
1525 unit->stmtlist = 1;
1526 unit->line_offset = attr.u.val;
1527 break;
1529 case DW_AT_name:
1530 unit->name = attr.u.str;
1531 break;
1533 case DW_AT_low_pc:
1534 unit->arange.low = attr.u.val;
1535 break;
1537 case DW_AT_high_pc:
1538 unit->arange.high = attr.u.val;
1539 break;
1541 case DW_AT_comp_dir:
1543 char* comp_dir = attr.u.str;
1544 if (comp_dir)
1546 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1547 directory, get rid of it. */
1548 char *cp = strchr (comp_dir, ':');
1550 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1551 comp_dir = cp + 1;
1553 unit->comp_dir = comp_dir;
1554 break;
1557 default:
1558 break;
1562 unit->first_child_die_ptr = info_ptr;
1563 return unit;
1566 /* Return TRUE if UNIT contains the address given by ADDR. */
1568 static bfd_boolean
1569 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
1571 struct arange *arange;
1573 if (unit->error)
1574 return FALSE;
1576 arange = &unit->arange;
1579 if (addr >= arange->low && addr < arange->high)
1580 return TRUE;
1581 arange = arange->next;
1583 while (arange);
1585 return FALSE;
1588 /* If UNIT contains ADDR, set the output parameters to the values for
1589 the line containing ADDR. The output parameters, FILENAME_PTR,
1590 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1591 to be filled in.
1593 Return TRUE if UNIT contains ADDR, and no errors were encountered;
1594 FALSE otherwise. */
1596 static bfd_boolean
1597 comp_unit_find_nearest_line (struct comp_unit *unit,
1598 bfd_vma addr,
1599 const char **filename_ptr,
1600 const char **functionname_ptr,
1601 unsigned int *linenumber_ptr,
1602 struct dwarf2_debug *stash)
1604 bfd_boolean line_p;
1605 bfd_boolean func_p;
1606 struct funcinfo *function;
1608 if (unit->error)
1609 return FALSE;
1611 if (! unit->line_table)
1613 if (! unit->stmtlist)
1615 unit->error = 1;
1616 return FALSE;
1619 unit->line_table = decode_line_info (unit, stash);
1621 if (! unit->line_table)
1623 unit->error = 1;
1624 return FALSE;
1627 if (unit->first_child_die_ptr < unit->end_ptr
1628 && ! scan_unit_for_functions (unit))
1630 unit->error = 1;
1631 return FALSE;
1635 function = NULL;
1636 func_p = lookup_address_in_function_table (unit->function_table, addr,
1637 &function, functionname_ptr);
1638 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
1639 function, filename_ptr,
1640 linenumber_ptr);
1641 return line_p || func_p;
1644 /* Locate a section in a BFD containing debugging info. The search starts
1645 from the section after AFTER_SEC, or from the first section in the BFD if
1646 AFTER_SEC is NULL. The search works by examining the names of the
1647 sections. There are two permissiable names. The first is .debug_info.
1648 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
1649 This is a variation on the .debug_info section which has a checksum
1650 describing the contents appended onto the name. This allows the linker to
1651 identify and discard duplicate debugging sections for different
1652 compilation units. */
1653 #define DWARF2_DEBUG_INFO ".debug_info"
1654 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1656 static asection *
1657 find_debug_info (bfd *abfd, asection *after_sec)
1659 asection * msec;
1661 if (after_sec)
1662 msec = after_sec->next;
1663 else
1664 msec = abfd->sections;
1666 while (msec)
1668 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1669 return msec;
1671 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1672 return msec;
1674 msec = msec->next;
1677 return NULL;
1680 /* The DWARF2 version of find_nearest line. Return TRUE if the line
1681 is found without error. ADDR_SIZE is the number of bytes in the
1682 initial .debug_info length field and in the abbreviation offset.
1683 You may use zero to indicate that the default value should be
1684 used. */
1686 bfd_boolean
1687 _bfd_dwarf2_find_nearest_line (bfd *abfd,
1688 asection *section,
1689 asymbol **symbols,
1690 bfd_vma offset,
1691 const char **filename_ptr,
1692 const char **functionname_ptr,
1693 unsigned int *linenumber_ptr,
1694 unsigned int addr_size,
1695 void **pinfo)
1697 /* Read each compilation unit from the section .debug_info, and check
1698 to see if it contains the address we are searching for. If yes,
1699 lookup the address, and return the line number info. If no, go
1700 on to the next compilation unit.
1702 We keep a list of all the previously read compilation units, and
1703 a pointer to the next un-read compilation unit. Check the
1704 previously read units before reading more. */
1705 struct dwarf2_debug *stash = *pinfo;
1707 /* What address are we looking for? */
1708 bfd_vma addr = offset + section->vma;
1710 struct comp_unit* each;
1712 *filename_ptr = NULL;
1713 *functionname_ptr = NULL;
1714 *linenumber_ptr = 0;
1716 /* The DWARF2 spec says that the initial length field, and the
1717 offset of the abbreviation table, should both be 4-byte values.
1718 However, some compilers do things differently. */
1719 if (addr_size == 0)
1720 addr_size = 4;
1721 BFD_ASSERT (addr_size == 4 || addr_size == 8);
1723 if (! stash)
1725 bfd_size_type total_size;
1726 asection *msec;
1727 bfd_size_type amt = sizeof (struct dwarf2_debug);
1729 stash = bfd_zalloc (abfd, amt);
1730 if (! stash)
1731 return FALSE;
1733 *pinfo = stash;
1735 msec = find_debug_info (abfd, NULL);
1736 if (! msec)
1737 /* No dwarf2 info. Note that at this point the stash
1738 has been allocated, but contains zeros, this lets
1739 future calls to this function fail quicker. */
1740 return FALSE;
1742 /* There can be more than one DWARF2 info section in a BFD these days.
1743 Read them all in and produce one large stash. We do this in two
1744 passes - in the first pass we just accumulate the section sizes.
1745 In the second pass we read in the section's contents. The allows
1746 us to avoid reallocing the data as we add sections to the stash. */
1747 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1748 total_size += msec->_raw_size;
1750 stash->info_ptr = bfd_alloc (abfd, total_size);
1751 if (stash->info_ptr == NULL)
1752 return FALSE;
1754 stash->info_ptr_end = stash->info_ptr;
1756 for (msec = find_debug_info (abfd, NULL);
1757 msec;
1758 msec = find_debug_info (abfd, msec))
1760 bfd_size_type size;
1761 bfd_size_type start;
1763 size = msec->_raw_size;
1764 if (size == 0)
1765 continue;
1767 start = stash->info_ptr_end - stash->info_ptr;
1769 if ((bfd_simple_get_relocated_section_contents
1770 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
1771 continue;
1773 stash->info_ptr_end = stash->info_ptr + start + size;
1776 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1778 stash->sec = find_debug_info (abfd, NULL);
1779 stash->sec_info_ptr = stash->info_ptr;
1780 stash->syms = symbols;
1783 /* A null info_ptr indicates that there is no dwarf2 info
1784 (or that an error occured while setting up the stash). */
1785 if (! stash->info_ptr)
1786 return FALSE;
1788 /* Check the previously read comp. units first. */
1789 for (each = stash->all_comp_units; each; each = each->next_unit)
1790 if (comp_unit_contains_address (each, addr))
1791 return comp_unit_find_nearest_line (each, addr, filename_ptr,
1792 functionname_ptr, linenumber_ptr,
1793 stash);
1795 /* Read each remaining comp. units checking each as they are read. */
1796 while (stash->info_ptr < stash->info_ptr_end)
1798 bfd_vma length;
1799 bfd_boolean found;
1800 unsigned int offset_size = addr_size;
1802 length = read_4_bytes (abfd, stash->info_ptr);
1803 /* A 0xffffff length is the DWARF3 way of indicating we use
1804 64-bit offsets, instead of 32-bit offsets. */
1805 if (length == 0xffffffff)
1807 offset_size = 8;
1808 length = read_8_bytes (abfd, stash->info_ptr + 4);
1809 stash->info_ptr += 12;
1811 /* A zero length is the IRIX way of indicating 64-bit offsets,
1812 mostly because the 64-bit length will generally fit in 32
1813 bits, and the endianness helps. */
1814 else if (length == 0)
1816 offset_size = 8;
1817 length = read_4_bytes (abfd, stash->info_ptr + 4);
1818 stash->info_ptr += 8;
1820 /* In the absence of the hints above, we assume addr_size-sized
1821 offsets, for backward-compatibility with pre-DWARF3 64-bit
1822 platforms. */
1823 else if (addr_size == 8)
1825 length = read_8_bytes (abfd, stash->info_ptr);
1826 stash->info_ptr += 8;
1828 else
1829 stash->info_ptr += 4;
1831 if (length > 0)
1833 each = parse_comp_unit (abfd, stash, length, offset_size);
1834 stash->info_ptr += length;
1836 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
1837 == stash->sec->_raw_size)
1839 stash->sec = find_debug_info (abfd, stash->sec);
1840 stash->sec_info_ptr = stash->info_ptr;
1843 if (each)
1845 each->next_unit = stash->all_comp_units;
1846 stash->all_comp_units = each;
1848 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1849 compilation units. If we don't have them (i.e.,
1850 unit->high == 0), we need to consult the line info
1851 table to see if a compilation unit contains the given
1852 address. */
1853 if (each->arange.high > 0)
1855 if (comp_unit_contains_address (each, addr))
1856 return comp_unit_find_nearest_line (each, addr,
1857 filename_ptr,
1858 functionname_ptr,
1859 linenumber_ptr,
1860 stash);
1862 else
1864 found = comp_unit_find_nearest_line (each, addr,
1865 filename_ptr,
1866 functionname_ptr,
1867 linenumber_ptr,
1868 stash);
1869 if (found)
1870 return TRUE;
1876 return FALSE;