* Makefile.am: Run "make dep-am".
[binutils.git] / bfd / dwarf2.c
blob7c000b9890e54562186aaa1f11fda3d42f2cf9c1
1 /* DWARF 2 support.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 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 unsigned int unsnd;
65 int snd;
66 bfd_vma addr;
71 /* Get at parts of an attribute structure. */
73 #define DW_STRING(attr) ((attr)->u.str)
74 #define DW_UNSND(attr) ((attr)->u.unsnd)
75 #define DW_BLOCK(attr) ((attr)->u.blk)
76 #define DW_SND(attr) ((attr)->u.snd)
77 #define DW_ADDR(attr) ((attr)->u.addr)
79 /* Blocks are a bunch of untyped bytes. */
80 struct dwarf_block
82 unsigned int size;
83 char *data;
86 struct dwarf2_debug
88 /* A list of all previously read comp_units. */
89 struct comp_unit* all_comp_units;
91 /* The next unread compilation unit within the .debug_info section.
92 Zero indicates that the .debug_info section has not been loaded
93 into a buffer yet. */
94 char* info_ptr;
96 /* Pointer to the end of the .debug_info section memory buffer. */
97 char* info_ptr_end;
99 /* Pointer to the section and address of the beginning of the
100 section. */
101 asection* sec;
102 char* sec_info_ptr;
104 /* Pointer to the symbol table. */
105 asymbol** syms;
107 /* Pointer to the .debug_abbrev section loaded into memory. */
108 char* dwarf_abbrev_buffer;
110 /* Length of the loaded .debug_abbrev section. */
111 unsigned long dwarf_abbrev_size;
113 /* Buffer for decode_line_info. */
114 char *dwarf_line_buffer;
116 /* Length of the loaded .debug_line section. */
117 unsigned long dwarf_line_size;
119 /* Pointer to the .debug_str section loaded into memory. */
120 char* dwarf_str_buffer;
122 /* Length of the loaded .debug_str section. */
123 unsigned long dwarf_str_size;
126 struct arange
128 struct arange *next;
129 bfd_vma low;
130 bfd_vma high;
133 /* A minimal decoding of DWARF2 compilation units. We only decode
134 what's needed to get to the line number information. */
136 struct comp_unit
138 /* Chain the previously read compilation units. */
139 struct comp_unit* next_unit;
141 /* Keep the bdf convenient (for memory allocation). */
142 bfd* abfd;
144 /* The lowest and higest addresses contained in this compilation
145 unit as specified in the compilation unit header. */
146 struct arange arange;
148 /* The DW_AT_name attribute (for error messages). */
149 char* name;
151 /* The abbrev hash table. */
152 struct abbrev_info** abbrevs;
154 /* Note that an error was found by comp_unit_find_nearest_line. */
155 int error;
157 /* The DW_AT_comp_dir attribute. */
158 char* comp_dir;
160 /* True if there is a line number table associated with this comp. unit. */
161 int stmtlist;
163 /* The offset into .debug_line of the line number table. */
164 unsigned long line_offset;
166 /* Pointer to the first child die for the comp unit. */
167 char *first_child_die_ptr;
169 /* The end of the comp unit. */
170 char *end_ptr;
172 /* The decoded line number, NULL if not yet decoded. */
173 struct line_info_table* line_table;
175 /* A list of the functions found in this comp. unit. */
176 struct funcinfo* function_table;
178 /* Pointer to dwarf2_debug structure. */
179 struct dwarf2_debug *stash;
181 /* Address size for this unit - from unit header. */
182 unsigned char addr_size;
184 /* Offset size for this unit - from unit header. */
185 unsigned char offset_size;
188 /* This data structure holds the information of an abbrev. */
189 struct abbrev_info
191 unsigned int number; /* Number identifying abbrev. */
192 enum dwarf_tag tag; /* DWARF tag. */
193 int has_children; /* Boolean. */
194 unsigned int num_attrs; /* Number of attributes. */
195 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
196 struct abbrev_info *next; /* Next in chain. */
199 struct attr_abbrev
201 enum dwarf_attribute name;
202 enum dwarf_form form;
205 #ifndef ABBREV_HASH_SIZE
206 #define ABBREV_HASH_SIZE 121
207 #endif
208 #ifndef ATTR_ALLOC_CHUNK
209 #define ATTR_ALLOC_CHUNK 4
210 #endif
212 static unsigned int read_1_byte PARAMS ((bfd *, char *));
213 static int read_1_signed_byte PARAMS ((bfd *, char *));
214 static unsigned int read_2_bytes PARAMS ((bfd *, char *));
215 static unsigned int read_4_bytes PARAMS ((bfd *, char *));
216 static bfd_vma read_8_bytes PARAMS ((bfd *, char *));
217 static char *read_n_bytes PARAMS ((bfd *, char *, unsigned int));
218 static char *read_string PARAMS ((bfd *, char *, unsigned int *));
219 static char *read_indirect_string PARAMS ((struct comp_unit *, char *, unsigned int *));
220 static unsigned int read_unsigned_leb128
221 PARAMS ((bfd *, char *, unsigned int *));
222 static int read_signed_leb128
223 PARAMS ((bfd *, char *, unsigned int *));
224 static bfd_vma read_address PARAMS ((struct comp_unit *, char *));
225 static struct abbrev_info *lookup_abbrev
226 PARAMS ((unsigned int, struct abbrev_info **));
227 static struct abbrev_info **read_abbrevs
228 PARAMS ((bfd *, bfd_vma, struct dwarf2_debug *));
229 static char *read_attribute
230 PARAMS ((struct attribute *, struct attr_abbrev *,
231 struct comp_unit *, char *));
232 static char *read_attribute_value
233 PARAMS ((struct attribute *, unsigned,
234 struct comp_unit *, char *));
235 static void add_line_info
236 PARAMS ((struct line_info_table *, bfd_vma, char *,
237 unsigned int, unsigned int, int));
238 static char *concat_filename PARAMS ((struct line_info_table *, unsigned int));
239 static void arange_add PARAMS ((struct comp_unit *, bfd_vma, bfd_vma));
240 static struct line_info_table *decode_line_info
241 PARAMS ((struct comp_unit *, struct dwarf2_debug *));
242 static boolean lookup_address_in_line_info_table
243 PARAMS ((struct line_info_table *, bfd_vma, struct funcinfo *,
244 const char **, unsigned int *));
245 static boolean lookup_address_in_function_table
246 PARAMS ((struct funcinfo *, bfd_vma, struct funcinfo **, const char **));
247 static boolean scan_unit_for_functions PARAMS ((struct comp_unit *));
248 static bfd_vma find_rela_addend
249 PARAMS ((bfd *, asection *, bfd_size_type, asymbol**));
250 static struct comp_unit *parse_comp_unit
251 PARAMS ((bfd *, struct dwarf2_debug *, bfd_vma, unsigned int));
252 static boolean comp_unit_contains_address
253 PARAMS ((struct comp_unit *, bfd_vma));
254 static boolean comp_unit_find_nearest_line
255 PARAMS ((struct comp_unit *, bfd_vma, const char **, const char **,
256 unsigned int *, struct dwarf2_debug *));
257 static asection *find_debug_info PARAMS ((bfd *, asection *));
259 /* VERBATIM
260 The following function up to the END VERBATIM mark are
261 copied directly from dwarf2read.c. */
263 /* Read dwarf information from a buffer. */
265 static unsigned int
266 read_1_byte (abfd, buf)
267 bfd *abfd ATTRIBUTE_UNUSED;
268 char *buf;
270 return bfd_get_8 (abfd, (bfd_byte *) buf);
273 static int
274 read_1_signed_byte (abfd, buf)
275 bfd *abfd ATTRIBUTE_UNUSED;
276 char *buf;
278 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
281 static unsigned int
282 read_2_bytes (abfd, buf)
283 bfd *abfd;
284 char *buf;
286 return bfd_get_16 (abfd, (bfd_byte *) buf);
289 #if 0 /* This is not used. */
291 static int
292 read_2_signed_bytes (abfd, buf)
293 bfd *abfd;
294 char *buf;
296 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
299 #endif
301 static unsigned int
302 read_4_bytes (abfd, buf)
303 bfd *abfd;
304 char *buf;
306 return bfd_get_32 (abfd, (bfd_byte *) buf);
309 #if 0 /* This is not used. */
311 static int
312 read_4_signed_bytes (abfd, buf)
313 bfd *abfd;
314 char *buf;
316 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
319 #endif
321 static bfd_vma
322 read_8_bytes (abfd, buf)
323 bfd *abfd;
324 char *buf;
326 return bfd_get_64 (abfd, (bfd_byte *) buf);
329 static char *
330 read_n_bytes (abfd, buf, size)
331 bfd *abfd ATTRIBUTE_UNUSED;
332 char *buf;
333 unsigned int size ATTRIBUTE_UNUSED;
335 /* If the size of a host char is 8 bits, we can return a pointer
336 to the buffer, otherwise we have to copy the data to a buffer
337 allocated on the temporary obstack. */
338 return buf;
341 static char *
342 read_string (abfd, buf, bytes_read_ptr)
343 bfd *abfd ATTRIBUTE_UNUSED;
344 char *buf;
345 unsigned int *bytes_read_ptr;
347 /* Return a pointer to the embedded string. */
348 if (*buf == '\0')
350 *bytes_read_ptr = 1;
351 return NULL;
354 *bytes_read_ptr = strlen (buf) + 1;
355 return buf;
358 static char *
359 read_indirect_string (unit, buf, bytes_read_ptr)
360 struct comp_unit* unit;
361 char *buf;
362 unsigned int *bytes_read_ptr;
364 bfd_vma offset;
365 struct dwarf2_debug *stash = unit->stash;
367 if (unit->offset_size == 4)
368 offset = read_4_bytes (unit->abfd, buf);
369 else
370 offset = read_8_bytes (unit->abfd, buf);
371 *bytes_read_ptr = unit->offset_size;
373 if (! stash->dwarf_str_buffer)
375 asection *msec;
376 bfd *abfd = unit->abfd;
378 msec = bfd_get_section_by_name (abfd, ".debug_str");
379 if (! msec)
381 (*_bfd_error_handler)
382 (_("Dwarf Error: Can't find .debug_str section."));
383 bfd_set_error (bfd_error_bad_value);
384 return NULL;
387 stash->dwarf_str_size = msec->_raw_size;
388 stash->dwarf_str_buffer = (char*) bfd_alloc (abfd, msec->_raw_size);
389 if (! stash->dwarf_abbrev_buffer)
390 return NULL;
392 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
393 (bfd_vma) 0, msec->_raw_size))
394 return NULL;
397 if (offset >= stash->dwarf_str_size)
399 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
400 (unsigned long) offset, stash->dwarf_str_size);
401 bfd_set_error (bfd_error_bad_value);
402 return NULL;
405 buf = stash->dwarf_str_buffer + offset;
406 if (*buf == '\0')
407 return NULL;
408 return buf;
411 static unsigned int
412 read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
413 bfd *abfd ATTRIBUTE_UNUSED;
414 char *buf;
415 unsigned int *bytes_read_ptr;
417 unsigned int result;
418 unsigned int num_read;
419 int shift;
420 unsigned char byte;
422 result = 0;
423 shift = 0;
424 num_read = 0;
428 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
429 buf ++;
430 num_read ++;
431 result |= ((byte & 0x7f) << shift);
432 shift += 7;
434 while (byte & 0x80);
436 * bytes_read_ptr = num_read;
438 return result;
441 static int
442 read_signed_leb128 (abfd, buf, bytes_read_ptr)
443 bfd *abfd ATTRIBUTE_UNUSED;
444 char *buf;
445 unsigned int * bytes_read_ptr;
447 int result;
448 int shift;
449 int num_read;
450 unsigned char byte;
452 result = 0;
453 shift = 0;
454 num_read = 0;
458 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
459 buf ++;
460 num_read ++;
461 result |= ((byte & 0x7f) << shift);
462 shift += 7;
464 while (byte & 0x80);
466 if ((shift < 32) && (byte & 0x40))
467 result |= -(1 << shift);
469 * bytes_read_ptr = num_read;
471 return result;
474 /* END VERBATIM */
476 static bfd_vma
477 read_address (unit, buf)
478 struct comp_unit* unit;
479 char *buf;
481 switch (unit->addr_size)
483 case 8:
484 return bfd_get_64 (unit->abfd, (bfd_byte *) buf);
485 case 4:
486 return bfd_get_32 (unit->abfd, (bfd_byte *) buf);
487 case 2:
488 return bfd_get_16 (unit->abfd, (bfd_byte *) buf);
489 default:
490 abort ();
494 /* Lookup an abbrev_info structure in the abbrev hash table. */
496 static struct abbrev_info *
497 lookup_abbrev (number,abbrevs)
498 unsigned int number;
499 struct abbrev_info **abbrevs;
501 unsigned int hash_number;
502 struct abbrev_info *abbrev;
504 hash_number = number % ABBREV_HASH_SIZE;
505 abbrev = abbrevs[hash_number];
507 while (abbrev)
509 if (abbrev->number == number)
510 return abbrev;
511 else
512 abbrev = abbrev->next;
515 return NULL;
518 /* In DWARF version 2, the description of the debugging information is
519 stored in a separate .debug_abbrev section. Before we read any
520 dies from a section we read in all abbreviations and install them
521 in a hash table. */
523 static struct abbrev_info**
524 read_abbrevs (abfd, offset, stash)
525 bfd * abfd;
526 bfd_vma offset;
527 struct dwarf2_debug *stash;
529 struct abbrev_info **abbrevs;
530 char *abbrev_ptr;
531 struct abbrev_info *cur_abbrev;
532 unsigned int abbrev_number, bytes_read, abbrev_name;
533 unsigned int abbrev_form, hash_number;
534 bfd_size_type amt;
536 if (! stash->dwarf_abbrev_buffer)
538 asection *msec;
540 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
541 if (! msec)
543 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
544 bfd_set_error (bfd_error_bad_value);
545 return 0;
548 stash->dwarf_abbrev_size = msec->_raw_size;
549 stash->dwarf_abbrev_buffer = (char*) bfd_alloc (abfd, msec->_raw_size);
550 if (! stash->dwarf_abbrev_buffer)
551 return 0;
553 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_abbrev_buffer,
554 (bfd_vma) 0, msec->_raw_size))
555 return 0;
558 if (offset >= stash->dwarf_abbrev_size)
560 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
561 (unsigned long) offset, stash->dwarf_abbrev_size);
562 bfd_set_error (bfd_error_bad_value);
563 return 0;
566 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
567 abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, amt);
569 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
570 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
571 abbrev_ptr += bytes_read;
573 /* Loop until we reach an abbrev number of 0. */
574 while (abbrev_number)
576 amt = sizeof (struct abbrev_info);
577 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
579 /* Read in abbrev header. */
580 cur_abbrev->number = abbrev_number;
581 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
582 abbrev_ptr += bytes_read;
583 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
584 abbrev_ptr += 1;
586 /* Now read in declarations. */
587 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
588 abbrev_ptr += bytes_read;
589 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
590 abbrev_ptr += bytes_read;
592 while (abbrev_name)
594 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
596 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
597 amt *= sizeof (struct attr_abbrev);
598 cur_abbrev->attrs = ((struct attr_abbrev *)
599 bfd_realloc (cur_abbrev->attrs, amt));
600 if (! cur_abbrev->attrs)
601 return 0;
604 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
605 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
606 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
607 abbrev_ptr += bytes_read;
608 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
609 abbrev_ptr += bytes_read;
612 hash_number = abbrev_number % ABBREV_HASH_SIZE;
613 cur_abbrev->next = abbrevs[hash_number];
614 abbrevs[hash_number] = cur_abbrev;
616 /* Get next abbreviation.
617 Under Irix6 the abbreviations for a compilation unit are not
618 always properly terminated with an abbrev number of 0.
619 Exit loop if we encounter an abbreviation which we have
620 already read (which means we are about to read the abbreviations
621 for the next compile unit) or if the end of the abbreviation
622 table is reached. */
623 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
624 >= stash->dwarf_abbrev_size)
625 break;
626 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
627 abbrev_ptr += bytes_read;
628 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
629 break;
632 return abbrevs;
635 /* Read an attribute value described by an attribute form. */
637 static char *
638 read_attribute_value (attr, form, unit, info_ptr)
639 struct attribute *attr;
640 unsigned form;
641 struct comp_unit *unit;
642 char *info_ptr;
644 bfd *abfd = unit->abfd;
645 unsigned int bytes_read;
646 struct dwarf_block *blk;
647 bfd_size_type amt;
649 attr->form = form;
651 switch (form)
653 case DW_FORM_addr:
654 /* FIXME: DWARF3 draft sais DW_FORM_ref_addr is offset_size. */
655 case DW_FORM_ref_addr:
656 DW_ADDR (attr) = read_address (unit, info_ptr);
657 info_ptr += unit->addr_size;
658 break;
659 case DW_FORM_block2:
660 amt = sizeof (struct dwarf_block);
661 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
662 blk->size = read_2_bytes (abfd, info_ptr);
663 info_ptr += 2;
664 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
665 info_ptr += blk->size;
666 DW_BLOCK (attr) = blk;
667 break;
668 case DW_FORM_block4:
669 amt = sizeof (struct dwarf_block);
670 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
671 blk->size = read_4_bytes (abfd, info_ptr);
672 info_ptr += 4;
673 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
674 info_ptr += blk->size;
675 DW_BLOCK (attr) = blk;
676 break;
677 case DW_FORM_data2:
678 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
679 info_ptr += 2;
680 break;
681 case DW_FORM_data4:
682 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
683 info_ptr += 4;
684 break;
685 case DW_FORM_data8:
686 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
687 info_ptr += 8;
688 break;
689 case DW_FORM_string:
690 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
691 info_ptr += bytes_read;
692 break;
693 case DW_FORM_strp:
694 DW_STRING (attr) = read_indirect_string (unit, info_ptr, &bytes_read);
695 info_ptr += bytes_read;
696 break;
697 case DW_FORM_block:
698 amt = sizeof (struct dwarf_block);
699 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
700 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
701 info_ptr += bytes_read;
702 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
703 info_ptr += blk->size;
704 DW_BLOCK (attr) = blk;
705 break;
706 case DW_FORM_block1:
707 amt = sizeof (struct dwarf_block);
708 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
709 blk->size = read_1_byte (abfd, info_ptr);
710 info_ptr += 1;
711 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
712 info_ptr += blk->size;
713 DW_BLOCK (attr) = blk;
714 break;
715 case DW_FORM_data1:
716 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
717 info_ptr += 1;
718 break;
719 case DW_FORM_flag:
720 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
721 info_ptr += 1;
722 break;
723 case DW_FORM_sdata:
724 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
725 info_ptr += bytes_read;
726 break;
727 case DW_FORM_udata:
728 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
729 info_ptr += bytes_read;
730 break;
731 case DW_FORM_ref1:
732 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
733 info_ptr += 1;
734 break;
735 case DW_FORM_ref2:
736 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
737 info_ptr += 2;
738 break;
739 case DW_FORM_ref4:
740 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
741 info_ptr += 4;
742 break;
743 case DW_FORM_ref8:
744 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
745 info_ptr += 8;
746 break;
747 case DW_FORM_ref_udata:
748 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
749 info_ptr += bytes_read;
750 break;
751 case DW_FORM_indirect:
752 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
753 info_ptr += bytes_read;
754 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
755 break;
756 default:
757 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
758 form);
759 bfd_set_error (bfd_error_bad_value);
761 return info_ptr;
764 /* Read an attribute described by an abbreviated attribute. */
766 static char *
767 read_attribute (attr, abbrev, unit, info_ptr)
768 struct attribute *attr;
769 struct attr_abbrev *abbrev;
770 struct comp_unit *unit;
771 char *info_ptr;
773 attr->name = abbrev->name;
774 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
775 return info_ptr;
778 /* Source line information table routines. */
780 #define FILE_ALLOC_CHUNK 5
781 #define DIR_ALLOC_CHUNK 5
783 struct line_info
785 struct line_info* prev_line;
786 bfd_vma address;
787 char* filename;
788 unsigned int line;
789 unsigned int column;
790 int end_sequence; /* End of (sequential) code sequence. */
793 struct fileinfo
795 char *name;
796 unsigned int dir;
797 unsigned int time;
798 unsigned int size;
801 struct line_info_table
803 bfd* abfd;
804 unsigned int num_files;
805 unsigned int num_dirs;
806 char* comp_dir;
807 char** dirs;
808 struct fileinfo* files;
809 struct line_info* last_line;
812 struct funcinfo
814 struct funcinfo *prev_func;
815 char* name;
816 bfd_vma low;
817 bfd_vma high;
820 static void
821 add_line_info (table, address, filename, line, column, end_sequence)
822 struct line_info_table* table;
823 bfd_vma address;
824 char* filename;
825 unsigned int line;
826 unsigned int column;
827 int end_sequence;
829 bfd_size_type amt = sizeof (struct line_info);
830 struct line_info* info = (struct line_info*) bfd_alloc (table->abfd, amt);
832 info->prev_line = table->last_line;
833 table->last_line = info;
835 info->address = address;
836 info->filename = filename;
837 info->line = line;
838 info->column = column;
839 info->end_sequence = end_sequence;
842 static char *
843 concat_filename (table, file)
844 struct line_info_table* table;
845 unsigned int file;
847 char* filename;
849 if (file - 1 >= table->num_files)
851 (*_bfd_error_handler)
852 (_("Dwarf Error: mangled line number section (bad file number)."));
853 return "<unknown>";
856 filename = table->files[file - 1].name;
857 if (IS_ABSOLUTE_PATH(filename))
858 return filename;
859 else
861 char* dirname = (table->files[file - 1].dir
862 ? table->dirs[table->files[file - 1].dir - 1]
863 : table->comp_dir);
865 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown. The
866 best we can do is return the filename part. */
867 if (dirname == NULL)
868 return filename;
869 else
870 return (char*) concat (dirname, "/", filename, NULL);
874 static void
875 arange_add (unit, low_pc, high_pc)
876 struct comp_unit *unit;
877 bfd_vma low_pc;
878 bfd_vma high_pc;
880 struct arange *arange;
882 /* First see if we can cheaply extend an existing range. */
883 arange = &unit->arange;
887 if (low_pc == arange->high)
889 arange->high = high_pc;
890 return;
892 if (high_pc == arange->low)
894 arange->low = low_pc;
895 return;
897 arange = arange->next;
899 while (arange);
901 if (unit->arange.high == 0)
903 /* This is the first address range: store it in unit->arange. */
904 unit->arange.next = 0;
905 unit->arange.low = low_pc;
906 unit->arange.high = high_pc;
907 return;
910 /* Need to allocate a new arange and insert it into the arange list. */
911 arange = bfd_zalloc (unit->abfd, (bfd_size_type) sizeof (*arange));
912 arange->low = low_pc;
913 arange->high = high_pc;
915 arange->next = unit->arange.next;
916 unit->arange.next = arange;
919 /* Decode the line number information for UNIT. */
921 static struct line_info_table*
922 decode_line_info (unit, stash)
923 struct comp_unit *unit;
924 struct dwarf2_debug *stash;
926 bfd *abfd = unit->abfd;
927 struct line_info_table* table;
928 char *line_ptr;
929 char *line_end;
930 struct line_head lh;
931 unsigned int i, bytes_read, offset_size;
932 char *cur_file, *cur_dir;
933 unsigned char op_code, extended_op, adj_opcode;
934 bfd_size_type amt;
936 if (! stash->dwarf_line_buffer)
938 asection *msec;
940 msec = bfd_get_section_by_name (abfd, ".debug_line");
941 if (! msec)
943 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
944 bfd_set_error (bfd_error_bad_value);
945 return 0;
948 stash->dwarf_line_size = msec->_raw_size;
949 stash->dwarf_line_buffer = (char *) bfd_alloc (abfd, msec->_raw_size);
950 if (! stash->dwarf_line_buffer)
951 return 0;
953 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_line_buffer,
954 (bfd_vma) 0, msec->_raw_size))
955 return 0;
957 /* FIXME: We ought to apply the relocs against this section before
958 we process it... */
961 /* Since we are using un-relocated data, it is possible to get a bad value
962 for the line_offset. Validate it here so that we won't get a segfault
963 below. */
964 if (unit->line_offset >= stash->dwarf_line_size)
966 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
967 unit->line_offset, stash->dwarf_line_size);
968 bfd_set_error (bfd_error_bad_value);
969 return 0;
972 amt = sizeof (struct line_info_table);
973 table = (struct line_info_table*) bfd_alloc (abfd, amt);
974 table->abfd = abfd;
975 table->comp_dir = unit->comp_dir;
977 table->num_files = 0;
978 table->files = NULL;
980 table->num_dirs = 0;
981 table->dirs = NULL;
983 table->files = NULL;
984 table->last_line = NULL;
986 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
988 /* Read in the prologue. */
989 lh.total_length = read_4_bytes (abfd, line_ptr);
990 line_ptr += 4;
991 offset_size = 4;
992 if (lh.total_length == 0xffffffff)
994 lh.total_length = read_8_bytes (abfd, line_ptr);
995 line_ptr += 8;
996 offset_size = 8;
998 else if (lh.total_length == 0 && unit->addr_size == 8)
1000 /* Handle (non-standard) 64-bit DWARF2 formats. */
1001 lh.total_length = read_4_bytes (abfd, line_ptr);
1002 line_ptr += 4;
1003 offset_size = 8;
1005 line_end = line_ptr + lh.total_length;
1006 lh.version = read_2_bytes (abfd, line_ptr);
1007 line_ptr += 2;
1008 if (offset_size == 4)
1009 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1010 else
1011 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1012 line_ptr += offset_size;
1013 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1014 line_ptr += 1;
1015 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1016 line_ptr += 1;
1017 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1018 line_ptr += 1;
1019 lh.line_range = read_1_byte (abfd, line_ptr);
1020 line_ptr += 1;
1021 lh.opcode_base = read_1_byte (abfd, line_ptr);
1022 line_ptr += 1;
1023 amt = lh.opcode_base * sizeof (unsigned char);
1024 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
1026 lh.standard_opcode_lengths[0] = 1;
1028 for (i = 1; i < lh.opcode_base; ++i)
1030 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1031 line_ptr += 1;
1034 /* Read directory table. */
1035 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1037 line_ptr += bytes_read;
1039 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1041 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1042 amt *= sizeof (char *);
1043 table->dirs = (char **) bfd_realloc (table->dirs, amt);
1044 if (! table->dirs)
1045 return 0;
1048 table->dirs[table->num_dirs++] = cur_dir;
1051 line_ptr += bytes_read;
1053 /* Read file name table. */
1054 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1056 line_ptr += bytes_read;
1058 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1060 amt = table->num_files + FILE_ALLOC_CHUNK;
1061 amt *= sizeof (struct fileinfo);
1062 table->files = (struct fileinfo *) bfd_realloc (table->files, amt);
1063 if (! table->files)
1064 return 0;
1067 table->files[table->num_files].name = cur_file;
1068 table->files[table->num_files].dir =
1069 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1070 line_ptr += bytes_read;
1071 table->files[table->num_files].time =
1072 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1073 line_ptr += bytes_read;
1074 table->files[table->num_files].size =
1075 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1076 line_ptr += bytes_read;
1077 table->num_files++;
1080 line_ptr += bytes_read;
1082 /* Read the statement sequences until there's nothing left. */
1083 while (line_ptr < line_end)
1085 /* State machine registers. */
1086 bfd_vma address = 0;
1087 char * filename = concat_filename (table, 1);
1088 unsigned int line = 1;
1089 unsigned int column = 0;
1090 int is_stmt = lh.default_is_stmt;
1091 int basic_block = 0;
1092 int end_sequence = 0;
1093 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1094 compilers generate address sequences that are wildly out of
1095 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1096 for ia64-Linux). Thus, to determine the low and high
1097 address, we must compare on every DW_LNS_copy, etc. */
1098 bfd_vma low_pc = 0;
1099 bfd_vma high_pc = 0;
1101 /* Decode the table. */
1102 while (! end_sequence)
1104 op_code = read_1_byte (abfd, line_ptr);
1105 line_ptr += 1;
1107 if (op_code >= lh.opcode_base)
1109 /* Special operand. */
1110 adj_opcode = op_code - lh.opcode_base;
1111 address += (adj_opcode / lh.line_range)
1112 * lh.minimum_instruction_length;
1113 line += lh.line_base + (adj_opcode % lh.line_range);
1114 /* Append row to matrix using current values. */
1115 add_line_info (table, address, filename, line, column, 0);
1116 basic_block = 1;
1117 if (low_pc == 0 || address < low_pc)
1118 low_pc = address;
1119 if (address > high_pc)
1120 high_pc = address;
1122 else switch (op_code)
1124 case DW_LNS_extended_op:
1125 /* Ignore length. */
1126 line_ptr += 1;
1127 extended_op = read_1_byte (abfd, line_ptr);
1128 line_ptr += 1;
1130 switch (extended_op)
1132 case DW_LNE_end_sequence:
1133 end_sequence = 1;
1134 add_line_info (table, address, filename, line, column,
1135 end_sequence);
1136 if (low_pc == 0 || address < low_pc)
1137 low_pc = address;
1138 if (address > high_pc)
1139 high_pc = address;
1140 arange_add (unit, low_pc, high_pc);
1141 break;
1142 case DW_LNE_set_address:
1143 address = read_address (unit, line_ptr);
1144 line_ptr += unit->addr_size;
1145 break;
1146 case DW_LNE_define_file:
1147 cur_file = read_string (abfd, line_ptr, &bytes_read);
1148 line_ptr += bytes_read;
1149 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1151 amt = table->num_files + FILE_ALLOC_CHUNK;
1152 amt *= sizeof (struct fileinfo);
1153 table->files =
1154 (struct fileinfo *) bfd_realloc (table->files, amt);
1155 if (! table->files)
1156 return 0;
1158 table->files[table->num_files].name = cur_file;
1159 table->files[table->num_files].dir =
1160 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1161 line_ptr += bytes_read;
1162 table->files[table->num_files].time =
1163 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1164 line_ptr += bytes_read;
1165 table->files[table->num_files].size =
1166 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1167 line_ptr += bytes_read;
1168 table->num_files++;
1169 break;
1170 default:
1171 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1172 bfd_set_error (bfd_error_bad_value);
1173 return 0;
1175 break;
1176 case DW_LNS_copy:
1177 add_line_info (table, address, filename, line, column, 0);
1178 basic_block = 0;
1179 if (low_pc == 0 || address < low_pc)
1180 low_pc = address;
1181 if (address > high_pc)
1182 high_pc = address;
1183 break;
1184 case DW_LNS_advance_pc:
1185 address += lh.minimum_instruction_length
1186 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1187 line_ptr += bytes_read;
1188 break;
1189 case DW_LNS_advance_line:
1190 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1191 line_ptr += bytes_read;
1192 break;
1193 case DW_LNS_set_file:
1195 unsigned int file;
1197 /* The file and directory tables are 0
1198 based, the references are 1 based. */
1199 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1200 line_ptr += bytes_read;
1201 filename = concat_filename (table, file);
1202 break;
1204 case DW_LNS_set_column:
1205 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1206 line_ptr += bytes_read;
1207 break;
1208 case DW_LNS_negate_stmt:
1209 is_stmt = (!is_stmt);
1210 break;
1211 case DW_LNS_set_basic_block:
1212 basic_block = 1;
1213 break;
1214 case DW_LNS_const_add_pc:
1215 address += lh.minimum_instruction_length
1216 * ((255 - lh.opcode_base) / lh.line_range);
1217 break;
1218 case DW_LNS_fixed_advance_pc:
1219 address += read_2_bytes (abfd, line_ptr);
1220 line_ptr += 2;
1221 break;
1222 default:
1224 int i;
1225 /* Unknown standard opcode, ignore it. */
1226 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1228 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1229 line_ptr += bytes_read;
1236 return table;
1239 /* If ADDR is within TABLE set the output parameters and return true,
1240 otherwise return false. The output parameters, FILENAME_PTR and
1241 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1243 static boolean
1244 lookup_address_in_line_info_table (table, addr, function, filename_ptr,
1245 linenumber_ptr)
1246 struct line_info_table* table;
1247 bfd_vma addr;
1248 struct funcinfo *function;
1249 const char **filename_ptr;
1250 unsigned int *linenumber_ptr;
1252 struct line_info* next_line = table->last_line;
1253 struct line_info* each_line;
1255 if (!next_line)
1256 return false;
1258 each_line = next_line->prev_line;
1260 while (each_line && next_line)
1262 if (!each_line->end_sequence
1263 && addr >= each_line->address && addr < next_line->address)
1265 /* If this line appears to span functions, and addr is in the
1266 later function, return the first line of that function instead
1267 of the last line of the earlier one. This check is for GCC
1268 2.95, which emits the first line number for a function late. */
1269 if (function != NULL
1270 && each_line->address < function->low
1271 && next_line->address > function->low)
1273 *filename_ptr = next_line->filename;
1274 *linenumber_ptr = next_line->line;
1276 else
1278 *filename_ptr = each_line->filename;
1279 *linenumber_ptr = each_line->line;
1281 return true;
1283 next_line = each_line;
1284 each_line = each_line->prev_line;
1287 /* At this point each_line is NULL but next_line is not. If we found the
1288 containing function in this compilation unit, return the first line we
1289 have a number for. This is also for compatibility with GCC 2.95. */
1290 if (function != NULL)
1292 *filename_ptr = next_line->filename;
1293 *linenumber_ptr = next_line->line;
1294 return true;
1297 return false;
1300 /* Function table functions. */
1302 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
1304 static boolean
1305 lookup_address_in_function_table (table, addr, function_ptr,
1306 functionname_ptr)
1307 struct funcinfo* table;
1308 bfd_vma addr;
1309 struct funcinfo** function_ptr;
1310 const char **functionname_ptr;
1312 struct funcinfo* each_func;
1314 for (each_func = table;
1315 each_func;
1316 each_func = each_func->prev_func)
1318 if (addr >= each_func->low && addr < each_func->high)
1320 *functionname_ptr = each_func->name;
1321 *function_ptr = each_func;
1322 return true;
1326 return false;
1329 /* DWARF2 Compilation unit functions. */
1331 /* Scan over each die in a comp. unit looking for functions to add
1332 to the function table. */
1334 static boolean
1335 scan_unit_for_functions (unit)
1336 struct comp_unit *unit;
1338 bfd *abfd = unit->abfd;
1339 char *info_ptr = unit->first_child_die_ptr;
1340 int nesting_level = 1;
1342 while (nesting_level)
1344 unsigned int abbrev_number, bytes_read, i;
1345 struct abbrev_info *abbrev;
1346 struct attribute attr;
1347 struct funcinfo *func;
1348 char* name = 0;
1350 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1351 info_ptr += bytes_read;
1353 if (! abbrev_number)
1355 nesting_level--;
1356 continue;
1359 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1360 if (! abbrev)
1362 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1363 abbrev_number);
1364 bfd_set_error (bfd_error_bad_value);
1365 return false;
1368 if (abbrev->tag == DW_TAG_subprogram)
1370 bfd_size_type amt = sizeof (struct funcinfo);
1371 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
1372 func->prev_func = unit->function_table;
1373 unit->function_table = func;
1375 else
1376 func = NULL;
1378 for (i = 0; i < abbrev->num_attrs; ++i)
1380 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1382 if (func)
1384 switch (attr.name)
1386 case DW_AT_name:
1388 name = DW_STRING (&attr);
1390 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1391 if (func->name == NULL)
1392 func->name = DW_STRING (&attr);
1393 break;
1395 case DW_AT_MIPS_linkage_name:
1396 func->name = DW_STRING (&attr);
1397 break;
1399 case DW_AT_low_pc:
1400 func->low = DW_ADDR (&attr);
1401 break;
1403 case DW_AT_high_pc:
1404 func->high = DW_ADDR (&attr);
1405 break;
1407 default:
1408 break;
1411 else
1413 switch (attr.name)
1415 case DW_AT_name:
1416 name = DW_STRING (&attr);
1417 break;
1419 default:
1420 break;
1425 if (abbrev->has_children)
1426 nesting_level++;
1429 return true;
1432 /* Look for a RELA relocation to be applied on OFFSET of section SEC,
1433 and return the addend if such a relocation is found. Since this is
1434 only used to find relocations referring to the .debug_abbrev
1435 section, we make sure the relocation refers to this section, but
1436 this is not strictly necessary, and it can probably be safely
1437 removed if needed. However, it is important to note that this
1438 function only returns the addend, it doesn't serve the purpose of
1439 applying a generic relocation.
1441 If no suitable relocation is found, or if it is not a real RELA
1442 relocation, this function returns 0. */
1444 static bfd_vma
1445 find_rela_addend (abfd, sec, offset, syms)
1446 bfd* abfd;
1447 asection* sec;
1448 bfd_size_type offset;
1449 asymbol** syms;
1451 long reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
1452 arelent **relocs = NULL;
1453 long reloc_count, relc;
1455 if (reloc_size <= 0)
1456 return 0;
1458 relocs = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
1459 if (relocs == NULL)
1460 return 0;
1462 reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, syms);
1464 if (reloc_count <= 0)
1466 free (relocs);
1467 return 0;
1470 for (relc = 0; relc < reloc_count; relc++)
1471 if (relocs[relc]->address == offset
1472 && (*relocs[relc]->sym_ptr_ptr)->flags & BSF_SECTION_SYM
1473 && strcmp ((*relocs[relc]->sym_ptr_ptr)->name,
1474 ".debug_abbrev") == 0)
1476 bfd_vma addend = (relocs[relc]->howto->partial_inplace
1477 ? 0 : relocs[relc]->addend);
1478 free (relocs);
1479 return addend;
1482 free (relocs);
1483 return 0;
1486 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1487 includes the compilation unit header that proceeds the DIE's, but
1488 does not include the length field that preceeds each compilation
1489 unit header. END_PTR points one past the end of this comp unit.
1490 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1492 This routine does not read the whole compilation unit; only enough
1493 to get to the line number information for the compilation unit. */
1495 static struct comp_unit *
1496 parse_comp_unit (abfd, stash, unit_length, offset_size)
1497 bfd* abfd;
1498 struct dwarf2_debug *stash;
1499 bfd_vma unit_length;
1500 unsigned int offset_size;
1502 struct comp_unit* unit;
1503 unsigned int version;
1504 bfd_vma abbrev_offset = 0;
1505 unsigned int addr_size;
1506 struct abbrev_info** abbrevs;
1507 unsigned int abbrev_number, bytes_read, i;
1508 struct abbrev_info *abbrev;
1509 struct attribute attr;
1510 char *info_ptr = stash->info_ptr;
1511 char *end_ptr = info_ptr + unit_length;
1512 bfd_size_type amt;
1513 bfd_size_type off;
1515 version = read_2_bytes (abfd, info_ptr);
1516 info_ptr += 2;
1517 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1518 if (offset_size == 4)
1519 abbrev_offset = read_4_bytes (abfd, info_ptr);
1520 else
1521 abbrev_offset = read_8_bytes (abfd, info_ptr);
1522 /* The abbrev offset is generally a relocation pointing to
1523 .debug_abbrev+offset. On RELA targets, we have to find the
1524 relocation and extract the addend to obtain the actual
1525 abbrev_offset, so do it here. */
1526 off = info_ptr - stash->sec_info_ptr;
1527 abbrev_offset += find_rela_addend (abfd, stash->sec, off, stash->syms);
1528 info_ptr += offset_size;
1529 addr_size = read_1_byte (abfd, info_ptr);
1530 info_ptr += 1;
1532 if (version != 2)
1534 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1535 bfd_set_error (bfd_error_bad_value);
1536 return 0;
1539 if (addr_size > sizeof (bfd_vma))
1541 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1542 addr_size,
1543 (unsigned int) sizeof (bfd_vma));
1544 bfd_set_error (bfd_error_bad_value);
1545 return 0;
1548 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1550 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1551 bfd_set_error (bfd_error_bad_value);
1552 return 0;
1555 /* Read the abbrevs for this compilation unit into a table. */
1556 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1557 if (! abbrevs)
1558 return 0;
1560 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1561 info_ptr += bytes_read;
1562 if (! abbrev_number)
1564 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1565 abbrev_number);
1566 bfd_set_error (bfd_error_bad_value);
1567 return 0;
1570 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1571 if (! abbrev)
1573 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1574 abbrev_number);
1575 bfd_set_error (bfd_error_bad_value);
1576 return 0;
1579 amt = sizeof (struct comp_unit);
1580 unit = (struct comp_unit*) bfd_zalloc (abfd, amt);
1581 unit->abfd = abfd;
1582 unit->addr_size = addr_size;
1583 unit->offset_size = offset_size;
1584 unit->abbrevs = abbrevs;
1585 unit->end_ptr = end_ptr;
1586 unit->stash = stash;
1588 for (i = 0; i < abbrev->num_attrs; ++i)
1590 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1592 /* Store the data if it is of an attribute we want to keep in a
1593 partial symbol table. */
1594 switch (attr.name)
1596 case DW_AT_stmt_list:
1597 unit->stmtlist = 1;
1598 unit->line_offset = DW_UNSND (&attr);
1599 break;
1601 case DW_AT_name:
1602 unit->name = DW_STRING (&attr);
1603 break;
1605 case DW_AT_low_pc:
1606 unit->arange.low = DW_ADDR (&attr);
1607 break;
1609 case DW_AT_high_pc:
1610 unit->arange.high = DW_ADDR (&attr);
1611 break;
1613 case DW_AT_comp_dir:
1615 char* comp_dir = DW_STRING (&attr);
1616 if (comp_dir)
1618 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1619 directory, get rid of it. */
1620 char *cp = (char*) strchr (comp_dir, ':');
1622 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1623 comp_dir = cp + 1;
1625 unit->comp_dir = comp_dir;
1626 break;
1629 default:
1630 break;
1634 unit->first_child_die_ptr = info_ptr;
1635 return unit;
1638 /* Return true if UNIT contains the address given by ADDR. */
1640 static boolean
1641 comp_unit_contains_address (unit, addr)
1642 struct comp_unit* unit;
1643 bfd_vma addr;
1645 struct arange *arange;
1647 if (unit->error)
1648 return 0;
1650 arange = &unit->arange;
1653 if (addr >= arange->low && addr < arange->high)
1654 return 1;
1655 arange = arange->next;
1657 while (arange);
1659 return 0;
1662 /* If UNIT contains ADDR, set the output parameters to the values for
1663 the line containing ADDR. The output parameters, FILENAME_PTR,
1664 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1665 to be filled in.
1667 Return true of UNIT contains ADDR, and no errors were encountered;
1668 false otherwise. */
1670 static boolean
1671 comp_unit_find_nearest_line (unit, addr, filename_ptr, functionname_ptr,
1672 linenumber_ptr, stash)
1673 struct comp_unit* unit;
1674 bfd_vma addr;
1675 const char **filename_ptr;
1676 const char **functionname_ptr;
1677 unsigned int *linenumber_ptr;
1678 struct dwarf2_debug *stash;
1680 boolean line_p;
1681 boolean func_p;
1682 struct funcinfo *function;
1684 if (unit->error)
1685 return false;
1687 if (! unit->line_table)
1689 if (! unit->stmtlist)
1691 unit->error = 1;
1692 return false;
1695 unit->line_table = decode_line_info (unit, stash);
1697 if (! unit->line_table)
1699 unit->error = 1;
1700 return false;
1703 if (unit->first_child_die_ptr < unit->end_ptr
1704 && ! scan_unit_for_functions (unit))
1706 unit->error = 1;
1707 return false;
1711 function = NULL;
1712 func_p = lookup_address_in_function_table (unit->function_table, addr,
1713 &function, functionname_ptr);
1714 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
1715 function, filename_ptr,
1716 linenumber_ptr);
1717 return line_p || func_p;
1720 /* Locate a section in a BFD containing debugging info. The search starts
1721 from the section after AFTER_SEC, or from the first section in the BFD if
1722 AFTER_SEC is NULL. The search works by examining the names of the
1723 sections. There are two permissiable names. The first is .debug_info.
1724 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
1725 This is a variation on the .debug_info section which has a checksum
1726 describing the contents appended onto the name. This allows the linker to
1727 identify and discard duplicate debugging sections for different
1728 compilation units. */
1729 #define DWARF2_DEBUG_INFO ".debug_info"
1730 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1732 static asection *
1733 find_debug_info (abfd, after_sec)
1734 bfd * abfd;
1735 asection * after_sec;
1737 asection * msec;
1739 if (after_sec)
1740 msec = after_sec->next;
1741 else
1742 msec = abfd->sections;
1744 while (msec)
1746 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1747 return msec;
1749 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1750 return msec;
1752 msec = msec->next;
1755 return NULL;
1758 /* The DWARF2 version of find_nearest line. Return true if the line
1759 is found without error. ADDR_SIZE is the number of bytes in the
1760 initial .debug_info length field and in the abbreviation offset.
1761 You may use zero to indicate that the default value should be
1762 used. */
1764 boolean
1765 _bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
1766 filename_ptr, functionname_ptr,
1767 linenumber_ptr, addr_size, pinfo)
1768 bfd *abfd;
1769 asection *section;
1770 asymbol **symbols;
1771 bfd_vma offset;
1772 const char **filename_ptr;
1773 const char **functionname_ptr;
1774 unsigned int *linenumber_ptr;
1775 unsigned int addr_size;
1776 PTR *pinfo;
1778 /* Read each compilation unit from the section .debug_info, and check
1779 to see if it contains the address we are searching for. If yes,
1780 lookup the address, and return the line number info. If no, go
1781 on to the next compilation unit.
1783 We keep a list of all the previously read compilation units, and
1784 a pointer to the next un-read compilation unit. Check the
1785 previously read units before reading more. */
1786 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
1788 /* What address are we looking for? */
1789 bfd_vma addr = offset + section->vma;
1791 struct comp_unit* each;
1793 *filename_ptr = NULL;
1794 *functionname_ptr = NULL;
1795 *linenumber_ptr = 0;
1797 /* The DWARF2 spec says that the initial length field, and the
1798 offset of the abbreviation table, should both be 4-byte values.
1799 However, some compilers do things differently. */
1800 if (addr_size == 0)
1801 addr_size = 4;
1802 BFD_ASSERT (addr_size == 4 || addr_size == 8);
1804 if (! stash)
1806 bfd_size_type total_size;
1807 asection *msec;
1808 bfd_size_type amt = sizeof (struct dwarf2_debug);
1810 stash = (struct dwarf2_debug*) bfd_zalloc (abfd, amt);
1811 if (! stash)
1812 return false;
1814 *pinfo = (PTR) stash;
1816 msec = find_debug_info (abfd, NULL);
1817 if (! msec)
1818 /* No dwarf2 info. Note that at this point the stash
1819 has been allocated, but contains zeros, this lets
1820 future calls to this function fail quicker. */
1821 return false;
1823 /* There can be more than one DWARF2 info section in a BFD these days.
1824 Read them all in and produce one large stash. We do this in two
1825 passes - in the first pass we just accumulate the section sizes.
1826 In the second pass we read in the section's contents. The allows
1827 us to avoid reallocing the data as we add sections to the stash. */
1828 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1829 total_size += msec->_raw_size;
1831 stash->info_ptr = (char *) bfd_alloc (abfd, total_size);
1832 if (stash->info_ptr == NULL)
1833 return false;
1835 stash->info_ptr_end = stash->info_ptr;
1837 for (msec = find_debug_info (abfd, NULL);
1838 msec;
1839 msec = find_debug_info (abfd, msec))
1841 bfd_size_type size;
1842 bfd_size_type start;
1844 size = msec->_raw_size;
1845 if (size == 0)
1846 continue;
1848 start = stash->info_ptr_end - stash->info_ptr;
1850 if (! bfd_get_section_contents (abfd, msec, stash->info_ptr + start,
1851 (bfd_vma) 0, size))
1852 continue;
1854 stash->info_ptr_end = stash->info_ptr + start + size;
1857 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1859 stash->sec = find_debug_info (abfd, NULL);
1860 stash->sec_info_ptr = stash->info_ptr;
1861 stash->syms = symbols;
1864 /* FIXME: There is a problem with the contents of the
1865 .debug_info section. The 'low' and 'high' addresses of the
1866 comp_units are computed by relocs against symbols in the
1867 .text segment. We need these addresses in order to determine
1868 the nearest line number, and so we have to resolve the
1869 relocs. There is a similar problem when the .debug_line
1870 section is processed as well (e.g., there may be relocs
1871 against the operand of the DW_LNE_set_address operator).
1873 Unfortunately getting hold of the reloc information is hard...
1875 For now, this means that disassembling object files (as
1876 opposed to fully executables) does not always work as well as
1877 we would like. */
1879 /* A null info_ptr indicates that there is no dwarf2 info
1880 (or that an error occured while setting up the stash). */
1881 if (! stash->info_ptr)
1882 return false;
1884 /* Check the previously read comp. units first. */
1885 for (each = stash->all_comp_units; each; each = each->next_unit)
1886 if (comp_unit_contains_address (each, addr))
1887 return comp_unit_find_nearest_line (each, addr, filename_ptr,
1888 functionname_ptr, linenumber_ptr,
1889 stash);
1891 /* Read each remaining comp. units checking each as they are read. */
1892 while (stash->info_ptr < stash->info_ptr_end)
1894 bfd_vma length;
1895 boolean found;
1896 unsigned int offset_size = addr_size;
1898 if (addr_size == 4)
1900 length = read_4_bytes (abfd, stash->info_ptr);
1901 if (length == 0xffffffff)
1903 offset_size = 8;
1904 length = read_8_bytes (abfd, stash->info_ptr + 4);
1905 stash->info_ptr += 8;
1907 else if (length == 0)
1909 /* Handle (non-standard) 64-bit DWARF2 formats. */
1910 offset_size = 8;
1911 length = read_4_bytes (abfd, stash->info_ptr + 4);
1912 stash->info_ptr += 4;
1915 else
1916 length = read_8_bytes (abfd, stash->info_ptr);
1917 stash->info_ptr += addr_size;
1919 if (length > 0)
1921 each = parse_comp_unit (abfd, stash, length, offset_size);
1922 stash->info_ptr += length;
1924 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
1925 == stash->sec->_raw_size)
1927 stash->sec = find_debug_info (abfd, stash->sec);
1928 stash->sec_info_ptr = stash->info_ptr;
1931 if (each)
1933 each->next_unit = stash->all_comp_units;
1934 stash->all_comp_units = each;
1936 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1937 compilation units. If we don't have them (i.e.,
1938 unit->high == 0), we need to consult the line info
1939 table to see if a compilation unit contains the given
1940 address. */
1941 if (each->arange.high > 0)
1943 if (comp_unit_contains_address (each, addr))
1944 return comp_unit_find_nearest_line (each, addr,
1945 filename_ptr,
1946 functionname_ptr,
1947 linenumber_ptr,
1948 stash);
1950 else
1952 found = comp_unit_find_nearest_line (each, addr,
1953 filename_ptr,
1954 functionname_ptr,
1955 linenumber_ptr,
1956 stash);
1957 if (found)
1958 return true;
1964 return false;