bfd/
[binutils.git] / bfd / dwarf2.c
bloba09265755a00e78b988be3484d102a18fae0465a
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;
284 bfd_size_type sz;
286 msec = bfd_get_section_by_name (abfd, ".debug_str");
287 if (! msec)
289 (*_bfd_error_handler)
290 (_("Dwarf Error: Can't find .debug_str section."));
291 bfd_set_error (bfd_error_bad_value);
292 return NULL;
295 sz = msec->rawsize ? msec->rawsize : msec->size;
296 stash->dwarf_str_size = sz;
297 stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
298 if (! stash->dwarf_abbrev_buffer)
299 return NULL;
301 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
302 0, sz))
303 return NULL;
306 if (offset >= stash->dwarf_str_size)
308 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
309 (unsigned long) offset, stash->dwarf_str_size);
310 bfd_set_error (bfd_error_bad_value);
311 return NULL;
314 buf = stash->dwarf_str_buffer + offset;
315 if (*buf == '\0')
316 return NULL;
317 return buf;
320 static unsigned int
321 read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
322 char *buf,
323 unsigned int *bytes_read_ptr)
325 unsigned int result;
326 unsigned int num_read;
327 int shift;
328 unsigned char byte;
330 result = 0;
331 shift = 0;
332 num_read = 0;
336 byte = bfd_get_8 (abfd, buf);
337 buf ++;
338 num_read ++;
339 result |= ((byte & 0x7f) << shift);
340 shift += 7;
342 while (byte & 0x80);
344 * bytes_read_ptr = num_read;
346 return result;
349 static int
350 read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
351 char *buf,
352 unsigned int * bytes_read_ptr)
354 int result;
355 int shift;
356 int num_read;
357 unsigned char byte;
359 result = 0;
360 shift = 0;
361 num_read = 0;
365 byte = bfd_get_8 (abfd, buf);
366 buf ++;
367 num_read ++;
368 result |= ((byte & 0x7f) << shift);
369 shift += 7;
371 while (byte & 0x80);
373 if ((shift < 32) && (byte & 0x40))
374 result |= -(1 << shift);
376 * bytes_read_ptr = num_read;
378 return result;
381 /* END VERBATIM */
383 static bfd_uint64_t
384 read_address (struct comp_unit *unit, char *buf)
386 switch (unit->addr_size)
388 case 8:
389 return bfd_get_64 (unit->abfd, buf);
390 case 4:
391 return bfd_get_32 (unit->abfd, buf);
392 case 2:
393 return bfd_get_16 (unit->abfd, buf);
394 default:
395 abort ();
399 /* Lookup an abbrev_info structure in the abbrev hash table. */
401 static struct abbrev_info *
402 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
404 unsigned int hash_number;
405 struct abbrev_info *abbrev;
407 hash_number = number % ABBREV_HASH_SIZE;
408 abbrev = abbrevs[hash_number];
410 while (abbrev)
412 if (abbrev->number == number)
413 return abbrev;
414 else
415 abbrev = abbrev->next;
418 return NULL;
421 /* In DWARF version 2, the description of the debugging information is
422 stored in a separate .debug_abbrev section. Before we read any
423 dies from a section we read in all abbreviations and install them
424 in a hash table. */
426 static struct abbrev_info**
427 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
429 struct abbrev_info **abbrevs;
430 char *abbrev_ptr;
431 struct abbrev_info *cur_abbrev;
432 unsigned int abbrev_number, bytes_read, abbrev_name;
433 unsigned int abbrev_form, hash_number;
434 bfd_size_type amt;
436 if (! stash->dwarf_abbrev_buffer)
438 asection *msec;
440 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
441 if (! msec)
443 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
444 bfd_set_error (bfd_error_bad_value);
445 return 0;
448 stash->dwarf_abbrev_size = msec->size;
449 stash->dwarf_abbrev_buffer
450 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
451 stash->syms);
452 if (! stash->dwarf_abbrev_buffer)
453 return 0;
456 if (offset >= stash->dwarf_abbrev_size)
458 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
459 (unsigned long) offset, stash->dwarf_abbrev_size);
460 bfd_set_error (bfd_error_bad_value);
461 return 0;
464 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
465 abbrevs = bfd_zalloc (abfd, amt);
467 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
468 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
469 abbrev_ptr += bytes_read;
471 /* Loop until we reach an abbrev number of 0. */
472 while (abbrev_number)
474 amt = sizeof (struct abbrev_info);
475 cur_abbrev = bfd_zalloc (abfd, amt);
477 /* Read in abbrev header. */
478 cur_abbrev->number = abbrev_number;
479 cur_abbrev->tag = (enum dwarf_tag)
480 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
481 abbrev_ptr += bytes_read;
482 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
483 abbrev_ptr += 1;
485 /* Now read in declarations. */
486 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
487 abbrev_ptr += bytes_read;
488 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
489 abbrev_ptr += bytes_read;
491 while (abbrev_name)
493 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
495 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
496 amt *= sizeof (struct attr_abbrev);
497 cur_abbrev->attrs = bfd_realloc (cur_abbrev->attrs, amt);
498 if (! cur_abbrev->attrs)
499 return 0;
502 cur_abbrev->attrs[cur_abbrev->num_attrs].name
503 = (enum dwarf_attribute) abbrev_name;
504 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
505 = (enum dwarf_form) abbrev_form;
506 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
507 abbrev_ptr += bytes_read;
508 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
509 abbrev_ptr += bytes_read;
512 hash_number = abbrev_number % ABBREV_HASH_SIZE;
513 cur_abbrev->next = abbrevs[hash_number];
514 abbrevs[hash_number] = cur_abbrev;
516 /* Get next abbreviation.
517 Under Irix6 the abbreviations for a compilation unit are not
518 always properly terminated with an abbrev number of 0.
519 Exit loop if we encounter an abbreviation which we have
520 already read (which means we are about to read the abbreviations
521 for the next compile unit) or if the end of the abbreviation
522 table is reached. */
523 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
524 >= stash->dwarf_abbrev_size)
525 break;
526 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
527 abbrev_ptr += bytes_read;
528 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
529 break;
532 return abbrevs;
535 /* Read an attribute value described by an attribute form. */
537 static char *
538 read_attribute_value (struct attribute *attr,
539 unsigned form,
540 struct comp_unit *unit,
541 char *info_ptr)
543 bfd *abfd = unit->abfd;
544 unsigned int bytes_read;
545 struct dwarf_block *blk;
546 bfd_size_type amt;
548 attr->form = (enum dwarf_form) form;
550 switch (form)
552 case DW_FORM_addr:
553 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
554 case DW_FORM_ref_addr:
555 attr->u.val = read_address (unit, info_ptr);
556 info_ptr += unit->addr_size;
557 break;
558 case DW_FORM_block2:
559 amt = sizeof (struct dwarf_block);
560 blk = bfd_alloc (abfd, amt);
561 blk->size = read_2_bytes (abfd, info_ptr);
562 info_ptr += 2;
563 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
564 info_ptr += blk->size;
565 attr->u.blk = blk;
566 break;
567 case DW_FORM_block4:
568 amt = sizeof (struct dwarf_block);
569 blk = bfd_alloc (abfd, amt);
570 blk->size = read_4_bytes (abfd, info_ptr);
571 info_ptr += 4;
572 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
573 info_ptr += blk->size;
574 attr->u.blk = blk;
575 break;
576 case DW_FORM_data2:
577 attr->u.val = read_2_bytes (abfd, info_ptr);
578 info_ptr += 2;
579 break;
580 case DW_FORM_data4:
581 attr->u.val = read_4_bytes (abfd, info_ptr);
582 info_ptr += 4;
583 break;
584 case DW_FORM_data8:
585 attr->u.val = read_8_bytes (abfd, info_ptr);
586 info_ptr += 8;
587 break;
588 case DW_FORM_string:
589 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
590 info_ptr += bytes_read;
591 break;
592 case DW_FORM_strp:
593 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
594 info_ptr += bytes_read;
595 break;
596 case DW_FORM_block:
597 amt = sizeof (struct dwarf_block);
598 blk = bfd_alloc (abfd, amt);
599 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
600 info_ptr += bytes_read;
601 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
602 info_ptr += blk->size;
603 attr->u.blk = blk;
604 break;
605 case DW_FORM_block1:
606 amt = sizeof (struct dwarf_block);
607 blk = bfd_alloc (abfd, amt);
608 blk->size = read_1_byte (abfd, info_ptr);
609 info_ptr += 1;
610 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
611 info_ptr += blk->size;
612 attr->u.blk = blk;
613 break;
614 case DW_FORM_data1:
615 attr->u.val = read_1_byte (abfd, info_ptr);
616 info_ptr += 1;
617 break;
618 case DW_FORM_flag:
619 attr->u.val = read_1_byte (abfd, info_ptr);
620 info_ptr += 1;
621 break;
622 case DW_FORM_sdata:
623 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
624 info_ptr += bytes_read;
625 break;
626 case DW_FORM_udata:
627 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
628 info_ptr += bytes_read;
629 break;
630 case DW_FORM_ref1:
631 attr->u.val = read_1_byte (abfd, info_ptr);
632 info_ptr += 1;
633 break;
634 case DW_FORM_ref2:
635 attr->u.val = read_2_bytes (abfd, info_ptr);
636 info_ptr += 2;
637 break;
638 case DW_FORM_ref4:
639 attr->u.val = read_4_bytes (abfd, info_ptr);
640 info_ptr += 4;
641 break;
642 case DW_FORM_ref8:
643 attr->u.val = read_8_bytes (abfd, info_ptr);
644 info_ptr += 8;
645 break;
646 case DW_FORM_ref_udata:
647 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
648 info_ptr += bytes_read;
649 break;
650 case DW_FORM_indirect:
651 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
652 info_ptr += bytes_read;
653 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
654 break;
655 default:
656 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
657 form);
658 bfd_set_error (bfd_error_bad_value);
660 return info_ptr;
663 /* Read an attribute described by an abbreviated attribute. */
665 static char *
666 read_attribute (struct attribute *attr,
667 struct attr_abbrev *abbrev,
668 struct comp_unit *unit,
669 char *info_ptr)
671 attr->name = abbrev->name;
672 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
673 return info_ptr;
676 /* Source line information table routines. */
678 #define FILE_ALLOC_CHUNK 5
679 #define DIR_ALLOC_CHUNK 5
681 struct line_info
683 struct line_info* prev_line;
684 bfd_vma address;
685 char* filename;
686 unsigned int line;
687 unsigned int column;
688 int end_sequence; /* End of (sequential) code sequence. */
691 struct fileinfo
693 char *name;
694 unsigned int dir;
695 unsigned int time;
696 unsigned int size;
699 struct line_info_table
701 bfd* abfd;
702 unsigned int num_files;
703 unsigned int num_dirs;
704 char* comp_dir;
705 char** dirs;
706 struct fileinfo* files;
707 struct line_info* last_line; /* largest VMA */
708 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
711 struct funcinfo
713 struct funcinfo *prev_func;
714 char* name;
715 bfd_vma low;
716 bfd_vma high;
719 /* Adds a new entry to the line_info list in the line_info_table, ensuring
720 that the list is sorted. Note that the line_info list is sorted from
721 highest to lowest VMA (with possible duplicates); that is,
722 line_info->prev_line always accesses an equal or smaller VMA. */
724 static void
725 add_line_info (struct line_info_table *table,
726 bfd_vma address,
727 char *filename,
728 unsigned int line,
729 unsigned int column,
730 int end_sequence)
732 bfd_size_type amt = sizeof (struct line_info);
733 struct line_info* info = bfd_alloc (table->abfd, amt);
735 /* Find the correct location for 'info'. Normally we will receive
736 new line_info data 1) in order and 2) with increasing VMAs.
737 However some compilers break the rules (cf. decode_line_info) and
738 so we include some heuristics for quickly finding the correct
739 location for 'info'. In particular, these heuristics optimize for
740 the common case in which the VMA sequence that we receive is a
741 list of locally sorted VMAs such as
742 p...z a...j (where a < j < p < z)
744 Note: table->lcl_head is used to head an *actual* or *possible*
745 sequence within the list (such as a...j) that is not directly
746 headed by table->last_line
748 Note: we may receive duplicate entries from 'decode_line_info'. */
750 while (1)
751 if (!table->last_line
752 || address >= table->last_line->address)
754 /* Normal case: add 'info' to the beginning of the list */
755 info->prev_line = table->last_line;
756 table->last_line = info;
758 /* lcl_head: initialize to head a *possible* sequence at the end. */
759 if (!table->lcl_head)
760 table->lcl_head = info;
761 break;
763 else if (!table->lcl_head->prev_line
764 && table->lcl_head->address > address)
766 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
767 list and 2) the head of 'info'. */
768 info->prev_line = NULL;
769 table->lcl_head->prev_line = info;
770 break;
772 else if (table->lcl_head->prev_line
773 && table->lcl_head->address > address
774 && address >= table->lcl_head->prev_line->address)
776 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
777 list and 2) the head of 'info'. */
778 info->prev_line = table->lcl_head->prev_line;
779 table->lcl_head->prev_line = info;
780 break;
782 else
784 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
785 heads for 'info'. Reset 'lcl_head' and repeat. */
786 struct line_info* li2 = table->last_line; /* always non-NULL */
787 struct line_info* li1 = li2->prev_line;
789 while (li1)
791 if (li2->address > address && address >= li1->address)
792 break;
794 li2 = li1; /* always non-NULL */
795 li1 = li1->prev_line;
797 table->lcl_head = li2;
800 /* Set member data of 'info'. */
801 info->address = address;
802 info->line = line;
803 info->column = column;
804 info->end_sequence = end_sequence;
806 if (filename && filename[0])
808 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
809 if (info->filename)
810 strcpy (info->filename, filename);
812 else
813 info->filename = NULL;
816 /* Extract a fully qualified filename from a line info table.
817 The returned string has been malloc'ed and it is the caller's
818 responsibility to free it. */
820 static char *
821 concat_filename (struct line_info_table *table, unsigned int file)
823 char* filename;
825 if (file - 1 >= table->num_files)
827 (*_bfd_error_handler)
828 (_("Dwarf Error: mangled line number section (bad file number)."));
829 return strdup ("<unknown>");
832 filename = table->files[file - 1].name;
834 if (! IS_ABSOLUTE_PATH (filename))
836 char* dirname = (table->files[file - 1].dir
837 ? table->dirs[table->files[file - 1].dir - 1]
838 : table->comp_dir);
840 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
841 The best we can do is return the filename part. */
842 if (dirname != NULL)
844 unsigned int len = strlen (dirname) + strlen (filename) + 2;
845 char * name;
847 name = bfd_malloc (len);
848 if (name)
849 sprintf (name, "%s/%s", dirname, filename);
850 return name;
854 return strdup (filename);
857 static void
858 arange_add (struct comp_unit *unit, bfd_vma low_pc, bfd_vma high_pc)
860 struct arange *arange;
862 /* First see if we can cheaply extend an existing range. */
863 arange = &unit->arange;
867 if (low_pc == arange->high)
869 arange->high = high_pc;
870 return;
872 if (high_pc == arange->low)
874 arange->low = low_pc;
875 return;
877 arange = arange->next;
879 while (arange);
881 if (unit->arange.high == 0)
883 /* This is the first address range: store it in unit->arange. */
884 unit->arange.next = 0;
885 unit->arange.low = low_pc;
886 unit->arange.high = high_pc;
887 return;
890 /* Need to allocate a new arange and insert it into the arange list. */
891 arange = bfd_zalloc (unit->abfd, sizeof (*arange));
892 arange->low = low_pc;
893 arange->high = high_pc;
895 arange->next = unit->arange.next;
896 unit->arange.next = arange;
899 /* Decode the line number information for UNIT. */
901 static struct line_info_table*
902 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
904 bfd *abfd = unit->abfd;
905 struct line_info_table* table;
906 char *line_ptr;
907 char *line_end;
908 struct line_head lh;
909 unsigned int i, bytes_read, offset_size;
910 char *cur_file, *cur_dir;
911 unsigned char op_code, extended_op, adj_opcode;
912 bfd_size_type amt;
914 if (! stash->dwarf_line_buffer)
916 asection *msec;
918 msec = bfd_get_section_by_name (abfd, ".debug_line");
919 if (! msec)
921 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
922 bfd_set_error (bfd_error_bad_value);
923 return 0;
926 stash->dwarf_line_size = msec->size;
927 stash->dwarf_line_buffer
928 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
929 stash->syms);
930 if (! stash->dwarf_line_buffer)
931 return 0;
934 /* It is possible to get a bad value for the line_offset. Validate
935 it here so that we won't get a segfault below. */
936 if (unit->line_offset >= stash->dwarf_line_size)
938 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
939 unit->line_offset, stash->dwarf_line_size);
940 bfd_set_error (bfd_error_bad_value);
941 return 0;
944 amt = sizeof (struct line_info_table);
945 table = bfd_alloc (abfd, amt);
946 table->abfd = abfd;
947 table->comp_dir = unit->comp_dir;
949 table->num_files = 0;
950 table->files = NULL;
952 table->num_dirs = 0;
953 table->dirs = NULL;
955 table->files = NULL;
956 table->last_line = NULL;
957 table->lcl_head = NULL;
959 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
961 /* Read in the prologue. */
962 lh.total_length = read_4_bytes (abfd, line_ptr);
963 line_ptr += 4;
964 offset_size = 4;
965 if (lh.total_length == 0xffffffff)
967 lh.total_length = read_8_bytes (abfd, line_ptr);
968 line_ptr += 8;
969 offset_size = 8;
971 else if (lh.total_length == 0 && unit->addr_size == 8)
973 /* Handle (non-standard) 64-bit DWARF2 formats. */
974 lh.total_length = read_4_bytes (abfd, line_ptr);
975 line_ptr += 4;
976 offset_size = 8;
978 line_end = line_ptr + lh.total_length;
979 lh.version = read_2_bytes (abfd, line_ptr);
980 line_ptr += 2;
981 if (offset_size == 4)
982 lh.prologue_length = read_4_bytes (abfd, line_ptr);
983 else
984 lh.prologue_length = read_8_bytes (abfd, line_ptr);
985 line_ptr += offset_size;
986 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
987 line_ptr += 1;
988 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
989 line_ptr += 1;
990 lh.line_base = read_1_signed_byte (abfd, line_ptr);
991 line_ptr += 1;
992 lh.line_range = read_1_byte (abfd, line_ptr);
993 line_ptr += 1;
994 lh.opcode_base = read_1_byte (abfd, line_ptr);
995 line_ptr += 1;
996 amt = lh.opcode_base * sizeof (unsigned char);
997 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
999 lh.standard_opcode_lengths[0] = 1;
1001 for (i = 1; i < lh.opcode_base; ++i)
1003 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1004 line_ptr += 1;
1007 /* Read directory table. */
1008 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1010 line_ptr += bytes_read;
1012 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1014 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1015 amt *= sizeof (char *);
1016 table->dirs = bfd_realloc (table->dirs, amt);
1017 if (! table->dirs)
1018 return 0;
1021 table->dirs[table->num_dirs++] = cur_dir;
1024 line_ptr += bytes_read;
1026 /* Read file name table. */
1027 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1029 line_ptr += bytes_read;
1031 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1033 amt = table->num_files + FILE_ALLOC_CHUNK;
1034 amt *= sizeof (struct fileinfo);
1035 table->files = bfd_realloc (table->files, amt);
1036 if (! table->files)
1037 return 0;
1040 table->files[table->num_files].name = cur_file;
1041 table->files[table->num_files].dir =
1042 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1043 line_ptr += bytes_read;
1044 table->files[table->num_files].time =
1045 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1046 line_ptr += bytes_read;
1047 table->files[table->num_files].size =
1048 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1049 line_ptr += bytes_read;
1050 table->num_files++;
1053 line_ptr += bytes_read;
1055 /* Read the statement sequences until there's nothing left. */
1056 while (line_ptr < line_end)
1058 /* State machine registers. */
1059 bfd_vma address = 0;
1060 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1061 unsigned int line = 1;
1062 unsigned int column = 0;
1063 int is_stmt = lh.default_is_stmt;
1064 int basic_block = 0;
1065 int end_sequence = 0;
1066 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1067 compilers generate address sequences that are wildly out of
1068 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1069 for ia64-Linux). Thus, to determine the low and high
1070 address, we must compare on every DW_LNS_copy, etc. */
1071 bfd_vma low_pc = 0;
1072 bfd_vma high_pc = 0;
1074 /* Decode the table. */
1075 while (! end_sequence)
1077 op_code = read_1_byte (abfd, line_ptr);
1078 line_ptr += 1;
1080 if (op_code >= lh.opcode_base)
1082 /* Special operand. */
1083 adj_opcode = op_code - lh.opcode_base;
1084 address += (adj_opcode / lh.line_range)
1085 * lh.minimum_instruction_length;
1086 line += lh.line_base + (adj_opcode % lh.line_range);
1087 /* Append row to matrix using current values. */
1088 add_line_info (table, address, filename, line, column, 0);
1089 basic_block = 1;
1090 if (low_pc == 0 || address < low_pc)
1091 low_pc = address;
1092 if (address > high_pc)
1093 high_pc = address;
1095 else switch (op_code)
1097 case DW_LNS_extended_op:
1098 /* Ignore length. */
1099 line_ptr += 1;
1100 extended_op = read_1_byte (abfd, line_ptr);
1101 line_ptr += 1;
1103 switch (extended_op)
1105 case DW_LNE_end_sequence:
1106 end_sequence = 1;
1107 add_line_info (table, address, filename, line, column,
1108 end_sequence);
1109 if (low_pc == 0 || address < low_pc)
1110 low_pc = address;
1111 if (address > high_pc)
1112 high_pc = address;
1113 arange_add (unit, low_pc, high_pc);
1114 break;
1115 case DW_LNE_set_address:
1116 address = read_address (unit, line_ptr);
1117 line_ptr += unit->addr_size;
1118 break;
1119 case DW_LNE_define_file:
1120 cur_file = read_string (abfd, line_ptr, &bytes_read);
1121 line_ptr += bytes_read;
1122 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1124 amt = table->num_files + FILE_ALLOC_CHUNK;
1125 amt *= sizeof (struct fileinfo);
1126 table->files = bfd_realloc (table->files, amt);
1127 if (! table->files)
1128 return 0;
1130 table->files[table->num_files].name = cur_file;
1131 table->files[table->num_files].dir =
1132 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1133 line_ptr += bytes_read;
1134 table->files[table->num_files].time =
1135 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1136 line_ptr += bytes_read;
1137 table->files[table->num_files].size =
1138 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1139 line_ptr += bytes_read;
1140 table->num_files++;
1141 break;
1142 default:
1143 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1144 bfd_set_error (bfd_error_bad_value);
1145 return 0;
1147 break;
1148 case DW_LNS_copy:
1149 add_line_info (table, address, filename, line, column, 0);
1150 basic_block = 0;
1151 if (low_pc == 0 || address < low_pc)
1152 low_pc = address;
1153 if (address > high_pc)
1154 high_pc = address;
1155 break;
1156 case DW_LNS_advance_pc:
1157 address += lh.minimum_instruction_length
1158 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1159 line_ptr += bytes_read;
1160 break;
1161 case DW_LNS_advance_line:
1162 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1163 line_ptr += bytes_read;
1164 break;
1165 case DW_LNS_set_file:
1167 unsigned int file;
1169 /* The file and directory tables are 0
1170 based, the references are 1 based. */
1171 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1172 line_ptr += bytes_read;
1173 if (filename)
1174 free (filename);
1175 filename = concat_filename (table, file);
1176 break;
1178 case DW_LNS_set_column:
1179 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1180 line_ptr += bytes_read;
1181 break;
1182 case DW_LNS_negate_stmt:
1183 is_stmt = (!is_stmt);
1184 break;
1185 case DW_LNS_set_basic_block:
1186 basic_block = 1;
1187 break;
1188 case DW_LNS_const_add_pc:
1189 address += lh.minimum_instruction_length
1190 * ((255 - lh.opcode_base) / lh.line_range);
1191 break;
1192 case DW_LNS_fixed_advance_pc:
1193 address += read_2_bytes (abfd, line_ptr);
1194 line_ptr += 2;
1195 break;
1196 default:
1198 int i;
1200 /* Unknown standard opcode, ignore it. */
1201 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1203 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1204 line_ptr += bytes_read;
1210 if (filename)
1211 free (filename);
1214 return table;
1217 /* If ADDR is within TABLE set the output parameters and return TRUE,
1218 otherwise return FALSE. The output parameters, FILENAME_PTR and
1219 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1221 static bfd_boolean
1222 lookup_address_in_line_info_table (struct line_info_table *table,
1223 bfd_vma addr,
1224 struct funcinfo *function,
1225 const char **filename_ptr,
1226 unsigned int *linenumber_ptr)
1228 /* Note: table->last_line should be a descendingly sorted list. */
1229 struct line_info* next_line = table->last_line;
1230 struct line_info* each_line = NULL;
1231 *filename_ptr = NULL;
1233 if (!next_line)
1234 return FALSE;
1236 each_line = next_line->prev_line;
1238 /* Check for large addresses */
1239 if (addr > next_line->address)
1240 each_line = NULL; /* ensure we skip over the normal case */
1242 /* Normal case: search the list; save */
1243 while (each_line && next_line)
1245 /* If we have an address match, save this info. This allows us
1246 to return as good as results as possible for strange debugging
1247 info. */
1248 bfd_boolean addr_match = FALSE;
1249 if (each_line->address <= addr && addr <= next_line->address)
1251 addr_match = TRUE;
1253 /* If this line appears to span functions, and addr is in the
1254 later function, return the first line of that function instead
1255 of the last line of the earlier one. This check is for GCC
1256 2.95, which emits the first line number for a function late. */
1257 if (function != NULL
1258 && each_line->address < function->low
1259 && next_line->address > function->low)
1261 *filename_ptr = next_line->filename;
1262 *linenumber_ptr = next_line->line;
1264 else
1266 *filename_ptr = each_line->filename;
1267 *linenumber_ptr = each_line->line;
1271 if (addr_match && !each_line->end_sequence)
1272 return TRUE; /* we have definitely found what we want */
1274 next_line = each_line;
1275 each_line = each_line->prev_line;
1278 /* At this point each_line is NULL but next_line is not. If we found
1279 a candidate end-of-sequence point in the loop above, we can return
1280 that (compatibility with a bug in the Intel compiler); otherwise,
1281 assuming that we found the containing function for this address in
1282 this compilation unit, return the first line we have a number for
1283 (compatibility with GCC 2.95). */
1284 if (*filename_ptr == NULL && function != NULL)
1286 *filename_ptr = next_line->filename;
1287 *linenumber_ptr = next_line->line;
1288 return TRUE;
1291 return FALSE;
1294 /* Function table functions. */
1296 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE. */
1298 static bfd_boolean
1299 lookup_address_in_function_table (struct funcinfo *table,
1300 bfd_vma addr,
1301 struct funcinfo **function_ptr,
1302 const char **functionname_ptr)
1304 struct funcinfo* each_func;
1306 for (each_func = table;
1307 each_func;
1308 each_func = each_func->prev_func)
1310 if (addr >= each_func->low && addr < each_func->high)
1312 *functionname_ptr = each_func->name;
1313 *function_ptr = each_func;
1314 return TRUE;
1318 return FALSE;
1321 /* DWARF2 Compilation unit functions. */
1323 /* Scan over each die in a comp. unit looking for functions to add
1324 to the function table. */
1326 static bfd_boolean
1327 scan_unit_for_functions (struct comp_unit *unit)
1329 bfd *abfd = unit->abfd;
1330 char *info_ptr = unit->first_child_die_ptr;
1331 int nesting_level = 1;
1333 while (nesting_level)
1335 unsigned int abbrev_number, bytes_read, i;
1336 struct abbrev_info *abbrev;
1337 struct attribute attr;
1338 struct funcinfo *func;
1339 char* name = 0;
1341 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1342 info_ptr += bytes_read;
1344 if (! abbrev_number)
1346 nesting_level--;
1347 continue;
1350 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1351 if (! abbrev)
1353 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1354 abbrev_number);
1355 bfd_set_error (bfd_error_bad_value);
1356 return FALSE;
1359 if (abbrev->tag == DW_TAG_subprogram)
1361 bfd_size_type amt = sizeof (struct funcinfo);
1362 func = bfd_zalloc (abfd, amt);
1363 func->prev_func = unit->function_table;
1364 unit->function_table = func;
1366 else
1367 func = NULL;
1369 for (i = 0; i < abbrev->num_attrs; ++i)
1371 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1373 if (func)
1375 switch (attr.name)
1377 case DW_AT_name:
1379 name = attr.u.str;
1381 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1382 if (func->name == NULL)
1383 func->name = attr.u.str;
1384 break;
1386 case DW_AT_MIPS_linkage_name:
1387 func->name = attr.u.str;
1388 break;
1390 case DW_AT_low_pc:
1391 func->low = attr.u.val;
1392 break;
1394 case DW_AT_high_pc:
1395 func->high = attr.u.val;
1396 break;
1398 default:
1399 break;
1402 else
1404 switch (attr.name)
1406 case DW_AT_name:
1407 name = attr.u.str;
1408 break;
1410 default:
1411 break;
1416 if (abbrev->has_children)
1417 nesting_level++;
1420 return TRUE;
1423 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1424 includes the compilation unit header that proceeds the DIE's, but
1425 does not include the length field that precedes each compilation
1426 unit header. END_PTR points one past the end of this comp unit.
1427 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1429 This routine does not read the whole compilation unit; only enough
1430 to get to the line number information for the compilation unit. */
1432 static struct comp_unit *
1433 parse_comp_unit (bfd *abfd,
1434 struct dwarf2_debug *stash,
1435 bfd_vma unit_length,
1436 unsigned int offset_size)
1438 struct comp_unit* unit;
1439 unsigned int version;
1440 bfd_uint64_t abbrev_offset = 0;
1441 unsigned int addr_size;
1442 struct abbrev_info** abbrevs;
1443 unsigned int abbrev_number, bytes_read, i;
1444 struct abbrev_info *abbrev;
1445 struct attribute attr;
1446 char *info_ptr = stash->info_ptr;
1447 char *end_ptr = info_ptr + unit_length;
1448 bfd_size_type amt;
1450 version = read_2_bytes (abfd, info_ptr);
1451 info_ptr += 2;
1452 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1453 if (offset_size == 4)
1454 abbrev_offset = read_4_bytes (abfd, info_ptr);
1455 else
1456 abbrev_offset = read_8_bytes (abfd, info_ptr);
1457 info_ptr += offset_size;
1458 addr_size = read_1_byte (abfd, info_ptr);
1459 info_ptr += 1;
1461 if (version != 2)
1463 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1464 bfd_set_error (bfd_error_bad_value);
1465 return 0;
1468 if (addr_size > sizeof (bfd_vma))
1470 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1471 addr_size,
1472 (unsigned int) sizeof (bfd_vma));
1473 bfd_set_error (bfd_error_bad_value);
1474 return 0;
1477 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1479 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1480 bfd_set_error (bfd_error_bad_value);
1481 return 0;
1484 /* Read the abbrevs for this compilation unit into a table. */
1485 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1486 if (! abbrevs)
1487 return 0;
1489 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1490 info_ptr += bytes_read;
1491 if (! abbrev_number)
1493 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1494 abbrev_number);
1495 bfd_set_error (bfd_error_bad_value);
1496 return 0;
1499 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1500 if (! abbrev)
1502 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1503 abbrev_number);
1504 bfd_set_error (bfd_error_bad_value);
1505 return 0;
1508 amt = sizeof (struct comp_unit);
1509 unit = bfd_zalloc (abfd, amt);
1510 unit->abfd = abfd;
1511 unit->addr_size = addr_size;
1512 unit->offset_size = offset_size;
1513 unit->abbrevs = abbrevs;
1514 unit->end_ptr = end_ptr;
1515 unit->stash = stash;
1517 for (i = 0; i < abbrev->num_attrs; ++i)
1519 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1521 /* Store the data if it is of an attribute we want to keep in a
1522 partial symbol table. */
1523 switch (attr.name)
1525 case DW_AT_stmt_list:
1526 unit->stmtlist = 1;
1527 unit->line_offset = attr.u.val;
1528 break;
1530 case DW_AT_name:
1531 unit->name = attr.u.str;
1532 break;
1534 case DW_AT_low_pc:
1535 unit->arange.low = attr.u.val;
1536 break;
1538 case DW_AT_high_pc:
1539 unit->arange.high = attr.u.val;
1540 break;
1542 case DW_AT_comp_dir:
1544 char* comp_dir = attr.u.str;
1545 if (comp_dir)
1547 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1548 directory, get rid of it. */
1549 char *cp = strchr (comp_dir, ':');
1551 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1552 comp_dir = cp + 1;
1554 unit->comp_dir = comp_dir;
1555 break;
1558 default:
1559 break;
1563 unit->first_child_die_ptr = info_ptr;
1564 return unit;
1567 /* Return TRUE if UNIT contains the address given by ADDR. */
1569 static bfd_boolean
1570 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
1572 struct arange *arange;
1574 if (unit->error)
1575 return FALSE;
1577 arange = &unit->arange;
1580 if (addr >= arange->low && addr < arange->high)
1581 return TRUE;
1582 arange = arange->next;
1584 while (arange);
1586 return FALSE;
1589 /* If UNIT contains ADDR, set the output parameters to the values for
1590 the line containing ADDR. The output parameters, FILENAME_PTR,
1591 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1592 to be filled in.
1594 Return TRUE if UNIT contains ADDR, and no errors were encountered;
1595 FALSE otherwise. */
1597 static bfd_boolean
1598 comp_unit_find_nearest_line (struct comp_unit *unit,
1599 bfd_vma addr,
1600 const char **filename_ptr,
1601 const char **functionname_ptr,
1602 unsigned int *linenumber_ptr,
1603 struct dwarf2_debug *stash)
1605 bfd_boolean line_p;
1606 bfd_boolean func_p;
1607 struct funcinfo *function;
1609 if (unit->error)
1610 return FALSE;
1612 if (! unit->line_table)
1614 if (! unit->stmtlist)
1616 unit->error = 1;
1617 return FALSE;
1620 unit->line_table = decode_line_info (unit, stash);
1622 if (! unit->line_table)
1624 unit->error = 1;
1625 return FALSE;
1628 if (unit->first_child_die_ptr < unit->end_ptr
1629 && ! scan_unit_for_functions (unit))
1631 unit->error = 1;
1632 return FALSE;
1636 function = NULL;
1637 func_p = lookup_address_in_function_table (unit->function_table, addr,
1638 &function, functionname_ptr);
1639 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
1640 function, filename_ptr,
1641 linenumber_ptr);
1642 return line_p || func_p;
1645 /* Locate a section in a BFD containing debugging info. The search starts
1646 from the section after AFTER_SEC, or from the first section in the BFD if
1647 AFTER_SEC is NULL. The search works by examining the names of the
1648 sections. There are two permissiable names. The first is .debug_info.
1649 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
1650 This is a variation on the .debug_info section which has a checksum
1651 describing the contents appended onto the name. This allows the linker to
1652 identify and discard duplicate debugging sections for different
1653 compilation units. */
1654 #define DWARF2_DEBUG_INFO ".debug_info"
1655 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1657 static asection *
1658 find_debug_info (bfd *abfd, asection *after_sec)
1660 asection * msec;
1662 if (after_sec)
1663 msec = after_sec->next;
1664 else
1665 msec = abfd->sections;
1667 while (msec)
1669 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1670 return msec;
1672 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1673 return msec;
1675 msec = msec->next;
1678 return NULL;
1681 /* The DWARF2 version of find_nearest_line. Return TRUE if the line
1682 is found without error. ADDR_SIZE is the number of bytes in the
1683 initial .debug_info length field and in the abbreviation offset.
1684 You may use zero to indicate that the default value should be
1685 used. */
1687 bfd_boolean
1688 _bfd_dwarf2_find_nearest_line (bfd *abfd,
1689 asection *section,
1690 asymbol **symbols,
1691 bfd_vma offset,
1692 const char **filename_ptr,
1693 const char **functionname_ptr,
1694 unsigned int *linenumber_ptr,
1695 unsigned int addr_size,
1696 void **pinfo)
1698 /* Read each compilation unit from the section .debug_info, and check
1699 to see if it contains the address we are searching for. If yes,
1700 lookup the address, and return the line number info. If no, go
1701 on to the next compilation unit.
1703 We keep a list of all the previously read compilation units, and
1704 a pointer to the next un-read compilation unit. Check the
1705 previously read units before reading more. */
1706 struct dwarf2_debug *stash = *pinfo;
1708 /* What address are we looking for? */
1709 bfd_vma addr = offset + section->vma;
1711 struct comp_unit* each;
1713 *filename_ptr = NULL;
1714 *functionname_ptr = NULL;
1715 *linenumber_ptr = 0;
1717 /* The DWARF2 spec says that the initial length field, and the
1718 offset of the abbreviation table, should both be 4-byte values.
1719 However, some compilers do things differently. */
1720 if (addr_size == 0)
1721 addr_size = 4;
1722 BFD_ASSERT (addr_size == 4 || addr_size == 8);
1724 if (! stash)
1726 bfd_size_type total_size;
1727 asection *msec;
1728 bfd_size_type amt = sizeof (struct dwarf2_debug);
1730 stash = bfd_zalloc (abfd, amt);
1731 if (! stash)
1732 return FALSE;
1734 *pinfo = stash;
1736 msec = find_debug_info (abfd, NULL);
1737 if (! msec)
1738 /* No dwarf2 info. Note that at this point the stash
1739 has been allocated, but contains zeros, this lets
1740 future calls to this function fail quicker. */
1741 return FALSE;
1743 /* There can be more than one DWARF2 info section in a BFD these days.
1744 Read them all in and produce one large stash. We do this in two
1745 passes - in the first pass we just accumulate the section sizes.
1746 In the second pass we read in the section's contents. The allows
1747 us to avoid reallocing the data as we add sections to the stash. */
1748 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1749 total_size += msec->size;
1751 stash->info_ptr = bfd_alloc (abfd, total_size);
1752 if (stash->info_ptr == NULL)
1753 return FALSE;
1755 stash->info_ptr_end = stash->info_ptr;
1757 for (msec = find_debug_info (abfd, NULL);
1758 msec;
1759 msec = find_debug_info (abfd, msec))
1761 bfd_size_type size;
1762 bfd_size_type start;
1764 size = msec->size;
1765 if (size == 0)
1766 continue;
1768 start = stash->info_ptr_end - stash->info_ptr;
1770 if ((bfd_simple_get_relocated_section_contents
1771 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
1772 continue;
1774 stash->info_ptr_end = stash->info_ptr + start + size;
1777 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1779 stash->sec = find_debug_info (abfd, NULL);
1780 stash->sec_info_ptr = stash->info_ptr;
1781 stash->syms = symbols;
1784 /* A null info_ptr indicates that there is no dwarf2 info
1785 (or that an error occured while setting up the stash). */
1786 if (! stash->info_ptr)
1787 return FALSE;
1789 /* Check the previously read comp. units first. */
1790 for (each = stash->all_comp_units; each; each = each->next_unit)
1791 if (comp_unit_contains_address (each, addr))
1792 return comp_unit_find_nearest_line (each, addr, filename_ptr,
1793 functionname_ptr, linenumber_ptr,
1794 stash);
1796 /* Read each remaining comp. units checking each as they are read. */
1797 while (stash->info_ptr < stash->info_ptr_end)
1799 bfd_vma length;
1800 bfd_boolean found;
1801 unsigned int offset_size = addr_size;
1803 length = read_4_bytes (abfd, stash->info_ptr);
1804 /* A 0xffffff length is the DWARF3 way of indicating we use
1805 64-bit offsets, instead of 32-bit offsets. */
1806 if (length == 0xffffffff)
1808 offset_size = 8;
1809 length = read_8_bytes (abfd, stash->info_ptr + 4);
1810 stash->info_ptr += 12;
1812 /* A zero length is the IRIX way of indicating 64-bit offsets,
1813 mostly because the 64-bit length will generally fit in 32
1814 bits, and the endianness helps. */
1815 else if (length == 0)
1817 offset_size = 8;
1818 length = read_4_bytes (abfd, stash->info_ptr + 4);
1819 stash->info_ptr += 8;
1821 /* In the absence of the hints above, we assume addr_size-sized
1822 offsets, for backward-compatibility with pre-DWARF3 64-bit
1823 platforms. */
1824 else if (addr_size == 8)
1826 length = read_8_bytes (abfd, stash->info_ptr);
1827 stash->info_ptr += 8;
1829 else
1830 stash->info_ptr += 4;
1832 if (length > 0)
1834 each = parse_comp_unit (abfd, stash, length, offset_size);
1835 stash->info_ptr += length;
1837 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
1838 == stash->sec->size)
1840 stash->sec = find_debug_info (abfd, stash->sec);
1841 stash->sec_info_ptr = stash->info_ptr;
1844 if (each)
1846 each->next_unit = stash->all_comp_units;
1847 stash->all_comp_units = each;
1849 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1850 compilation units. If we don't have them (i.e.,
1851 unit->high == 0), we need to consult the line info
1852 table to see if a compilation unit contains the given
1853 address. */
1854 if (each->arange.high > 0)
1856 if (comp_unit_contains_address (each, addr))
1857 return comp_unit_find_nearest_line (each, addr,
1858 filename_ptr,
1859 functionname_ptr,
1860 linenumber_ptr,
1861 stash);
1863 else
1865 found = comp_unit_find_nearest_line (each, addr,
1866 filename_ptr,
1867 functionname_ptr,
1868 linenumber_ptr,
1869 stash);
1870 if (found)
1871 return TRUE;
1877 return FALSE;