* dwarf2.c (read_attribute): Support DW_FORM_ref8.
[binutils.git] / bfd / dwarf2.c
blob143c3fa1d8cd91e22a28c4d38713cebd4b94aeb2
1 /* DWARF 2 support.
2 Copyright 1994, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
4 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
5 (gavin@cygnus.com).
7 From the dwarf2read.c header:
8 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
9 Inc. with support from Florida State University (under contract
10 with the Ada Joint Program Office), and Silicon Graphics, Inc.
11 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
12 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
13 support in dwarfread.c
15 This file is part of BFD.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or (at
20 your option) any later version.
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
31 #include "bfd.h"
32 #include "sysdep.h"
33 #include "libiberty.h"
34 #include "libbfd.h"
35 #include "elf-bfd.h"
36 #include "elf/dwarf2.h"
38 /* The data in the .debug_line statement prologue looks like this. */
39 struct line_head
41 unsigned int total_length;
42 unsigned short version;
43 unsigned int prologue_length;
44 unsigned char minimum_instruction_length;
45 unsigned char default_is_stmt;
46 int line_base;
47 unsigned char line_range;
48 unsigned char opcode_base;
49 unsigned char *standard_opcode_lengths;
52 /* Attributes have a name and a value */
53 struct attribute
55 enum dwarf_attribute name;
56 enum dwarf_form form;
57 union
59 char *str;
60 struct dwarf_block *blk;
61 unsigned int unsnd;
62 int snd;
63 bfd_vma addr;
68 /* Get at parts of an attribute structure */
70 #define DW_STRING(attr) ((attr)->u.str)
71 #define DW_UNSND(attr) ((attr)->u.unsnd)
72 #define DW_BLOCK(attr) ((attr)->u.blk)
73 #define DW_SND(attr) ((attr)->u.snd)
74 #define DW_ADDR(attr) ((attr)->u.addr)
76 /* Blocks are a bunch of untyped bytes. */
77 struct dwarf_block
79 unsigned int size;
80 char *data;
84 struct dwarf2_debug {
86 /* A list of all previously read comp_units. */
87 struct comp_unit* all_comp_units;
89 /* The next unread compilation unit within the .debug_info section.
90 Zero indicates that the .debug_info section has not been loaded
91 into a buffer yet.*/
92 char* info_ptr;
94 /* Pointer to the end of the .debug_info section memory buffer. */
95 char* info_ptr_end;
97 /* Pointer to the .debug_abbrev section loaded into memory. */
98 char* dwarf_abbrev_buffer;
100 /* Length of the loaded .debug_abbrev section. */
101 unsigned long dwarf_abbrev_size;
103 /* Buffer for decode_line_info. */
104 char *dwarf_line_buffer;
107 struct arange {
108 struct arange *next;
109 bfd_vma low;
110 bfd_vma high;
114 /* A minimal decoding of DWARF2 compilation units. We only decode
115 what's needed to get to the line number information. */
117 struct comp_unit {
119 /* Chain the previously read compilation units. */
120 struct comp_unit* next_unit;
122 /* Keep the bdf convenient (for memory allocation). */
123 bfd* abfd;
125 /* The lowest and higest addresses contained in this compilation
126 unit as specified in the compilation unit header. */
127 struct arange arange;
129 /* The DW_AT_name attribute (for error messages). */
130 char* name;
132 /* The abbrev hash table. */
133 struct abbrev_info** abbrevs;
135 /* Note that an error was found by comp_unit_find_nearest_line. */
136 int error;
138 /* The DW_AT_comp_dir attribute */
139 char* comp_dir;
141 /* True if there is a line number table associated with this comp. unit. */
142 int stmtlist;
144 /* The offset into .debug_line of the line number table. */
145 unsigned long line_offset;
147 /* Pointer to the first child die for the comp unit. */
148 char *first_child_die_ptr;
150 /* The end of the comp unit. */
151 char *end_ptr;
153 /* The decoded line number, NULL if not yet decoded. */
154 struct line_info_table* line_table;
156 /* A list of the functions found in this comp. unit. */
157 struct funcinfo* function_table;
159 /* Address size for this unit - from unit header */
160 unsigned char addr_size;
165 /* VERBATIM
166 The following function up to the END VERBATIM mark are
167 copied directly from dwarf2read.c. */
169 /* read dwarf information from a buffer */
171 static unsigned int
172 read_1_byte (abfd, buf)
173 bfd *abfd ATTRIBUTE_UNUSED;
174 char *buf;
176 return bfd_get_8 (abfd, (bfd_byte *) buf);
179 static int
180 read_1_signed_byte (abfd, buf)
181 bfd *abfd ATTRIBUTE_UNUSED;
182 char *buf;
184 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
187 static unsigned int
188 read_2_bytes (abfd, buf)
189 bfd *abfd;
190 char *buf;
192 return bfd_get_16 (abfd, (bfd_byte *) buf);
195 #if 0
197 /* This is not used. */
199 static int
200 read_2_signed_bytes (abfd, buf)
201 bfd *abfd;
202 char *buf;
204 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
207 #endif
209 static unsigned int
210 read_4_bytes (abfd, buf)
211 bfd *abfd;
212 char *buf;
214 return bfd_get_32 (abfd, (bfd_byte *) buf);
217 #if 0
219 /* This is not used. */
221 static int
222 read_4_signed_bytes (abfd, buf)
223 bfd *abfd;
224 char *buf;
226 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
229 #endif
231 static unsigned int
232 read_8_bytes (abfd, buf)
233 bfd *abfd;
234 char *buf;
236 return bfd_get_64 (abfd, (bfd_byte *) buf);
239 static char *
240 read_n_bytes (abfd, buf, size)
241 bfd *abfd ATTRIBUTE_UNUSED;
242 char *buf;
243 unsigned int size ATTRIBUTE_UNUSED;
245 /* If the size of a host char is 8 bits, we can return a pointer
246 to the buffer, otherwise we have to copy the data to a buffer
247 allocated on the temporary obstack. */
248 return buf;
251 static char *
252 read_string (abfd, buf, bytes_read_ptr)
253 bfd *abfd ATTRIBUTE_UNUSED;
254 char *buf;
255 unsigned int *bytes_read_ptr;
257 /* If the size of a host char is 8 bits, we can return a pointer
258 to the string, otherwise we have to copy the string to a buffer
259 allocated on the temporary obstack. */
260 if (*buf == '\0')
262 *bytes_read_ptr = 1;
263 return NULL;
265 *bytes_read_ptr = strlen (buf) + 1;
266 return buf;
269 static unsigned int
270 read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
271 bfd *abfd ATTRIBUTE_UNUSED;
272 char *buf;
273 unsigned int *bytes_read_ptr;
275 unsigned int result;
276 unsigned int num_read;
277 int shift;
278 unsigned char byte;
280 result = 0;
281 shift = 0;
282 num_read = 0;
286 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
287 buf ++;
288 num_read ++;
289 result |= ((byte & 0x7f) << shift);
290 shift += 7;
292 while (byte & 0x80);
294 * bytes_read_ptr = num_read;
296 return result;
299 static int
300 read_signed_leb128 (abfd, buf, bytes_read_ptr)
301 bfd *abfd ATTRIBUTE_UNUSED;
302 char *buf;
303 unsigned int * bytes_read_ptr;
305 int result;
306 int shift;
307 int num_read;
308 unsigned char byte;
310 result = 0;
311 shift = 0;
312 num_read = 0;
316 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
317 buf ++;
318 num_read ++;
319 result |= ((byte & 0x7f) << shift);
320 shift += 7;
322 while (byte & 0x80);
324 if ((shift < 32) && (byte & 0x40))
325 result |= -(1 << shift);
327 * bytes_read_ptr = num_read;
329 return result;
332 /* END VERBATIM */
334 static bfd_vma
335 read_address (unit, buf)
336 struct comp_unit* unit;
337 char *buf;
339 bfd_vma retval = 0;
341 if (unit->addr_size == 4)
343 retval = bfd_get_32 (unit->abfd, (bfd_byte *) buf);
344 } else {
345 retval = bfd_get_64 (unit->abfd, (bfd_byte *) buf);
347 return retval;
354 /* This data structure holds the information of an abbrev. */
355 struct abbrev_info
357 unsigned int number; /* number identifying abbrev */
358 enum dwarf_tag tag; /* dwarf tag */
359 int has_children; /* boolean */
360 unsigned int num_attrs; /* number of attributes */
361 struct attr_abbrev *attrs; /* an array of attribute descriptions */
362 struct abbrev_info *next; /* next in chain */
365 struct attr_abbrev
367 enum dwarf_attribute name;
368 enum dwarf_form form;
371 #ifndef ABBREV_HASH_SIZE
372 #define ABBREV_HASH_SIZE 121
373 #endif
374 #ifndef ATTR_ALLOC_CHUNK
375 #define ATTR_ALLOC_CHUNK 4
376 #endif
378 /* Lookup an abbrev_info structure in the abbrev hash table. */
380 static struct abbrev_info *
381 lookup_abbrev (number,abbrevs)
382 unsigned int number;
383 struct abbrev_info **abbrevs;
385 unsigned int hash_number;
386 struct abbrev_info *abbrev;
388 hash_number = number % ABBREV_HASH_SIZE;
389 abbrev = abbrevs[hash_number];
391 while (abbrev)
393 if (abbrev->number == number)
394 return abbrev;
395 else
396 abbrev = abbrev->next;
398 return NULL;
401 /* In DWARF version 2, the description of the debugging information is
402 stored in a separate .debug_abbrev section. Before we read any
403 dies from a section we read in all abbreviations and install them
404 in a hash table. */
406 static struct abbrev_info**
407 read_abbrevs (abfd, offset)
408 bfd * abfd;
409 unsigned int offset;
411 struct abbrev_info **abbrevs;
412 char *abbrev_ptr;
413 struct abbrev_info *cur_abbrev;
414 unsigned int abbrev_number, bytes_read, abbrev_name;
415 unsigned int abbrev_form, hash_number;
416 struct dwarf2_debug *stash;
418 stash = elf_tdata(abfd)->dwarf2_find_line_info;
420 if (! stash->dwarf_abbrev_buffer)
422 asection *msec;
424 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
425 if (! msec)
427 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
428 bfd_set_error (bfd_error_bad_value);
429 return 0;
432 stash->dwarf_abbrev_size = bfd_get_section_size_before_reloc (msec);
433 stash->dwarf_abbrev_buffer = (char*) bfd_alloc (abfd, stash->dwarf_abbrev_size);
434 if (! stash->dwarf_abbrev_buffer)
435 return 0;
437 if (! bfd_get_section_contents (abfd, msec,
438 stash->dwarf_abbrev_buffer, 0,
439 stash->dwarf_abbrev_size))
440 return 0;
443 if (offset > stash->dwarf_abbrev_size)
445 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%u) bigger than abbrev size (%u)."),
446 offset, stash->dwarf_abbrev_size );
447 bfd_set_error (bfd_error_bad_value);
448 return 0;
451 abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, sizeof(struct abbrev_info*) * ABBREV_HASH_SIZE);
453 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
454 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
455 abbrev_ptr += bytes_read;
457 /* loop until we reach an abbrev number of 0 */
458 while (abbrev_number)
460 cur_abbrev = (struct abbrev_info*)bfd_zalloc (abfd, sizeof (struct abbrev_info));
462 /* read in abbrev header */
463 cur_abbrev->number = abbrev_number;
464 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
465 abbrev_ptr += bytes_read;
466 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
467 abbrev_ptr += 1;
469 /* now read in declarations */
470 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
471 abbrev_ptr += bytes_read;
472 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
473 abbrev_ptr += bytes_read;
474 while (abbrev_name)
476 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
478 cur_abbrev->attrs = (struct attr_abbrev *)
479 bfd_realloc (cur_abbrev->attrs,
480 (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)
481 * sizeof (struct attr_abbrev));
482 if (! cur_abbrev->attrs)
483 return 0;
485 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
486 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
487 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
488 abbrev_ptr += bytes_read;
489 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
490 abbrev_ptr += bytes_read;
493 hash_number = abbrev_number % ABBREV_HASH_SIZE;
494 cur_abbrev->next = abbrevs[hash_number];
495 abbrevs[hash_number] = cur_abbrev;
497 /* Get next abbreviation.
498 Under Irix6 the abbreviations for a compilation unit are not
499 always properly terminated with an abbrev number of 0.
500 Exit loop if we encounter an abbreviation which we have
501 already read (which means we are about to read the abbreviations
502 for the next compile unit) or if the end of the abbreviation
503 table is reached. */
504 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
505 >= stash->dwarf_abbrev_size)
506 break;
507 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
508 abbrev_ptr += bytes_read;
509 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
510 break;
513 return abbrevs;
516 /* Read an attribute described by an abbreviated attribute. */
518 static char *
519 read_attribute (attr, abbrev, unit, info_ptr)
520 struct attribute *attr;
521 struct attr_abbrev *abbrev;
522 struct comp_unit *unit;
523 char *info_ptr;
525 bfd *abfd = unit->abfd;
526 unsigned int bytes_read;
527 struct dwarf_block *blk;
529 attr->name = abbrev->name;
530 attr->form = abbrev->form;
531 switch (abbrev->form)
533 case DW_FORM_addr:
534 case DW_FORM_ref_addr:
535 DW_ADDR (attr) = read_address (unit, info_ptr);
536 info_ptr += unit->addr_size;
537 break;
538 case DW_FORM_block2:
539 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
540 blk->size = read_2_bytes (abfd, info_ptr);
541 info_ptr += 2;
542 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
543 info_ptr += blk->size;
544 DW_BLOCK (attr) = blk;
545 break;
546 case DW_FORM_block4:
547 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
548 blk->size = read_4_bytes (abfd, info_ptr);
549 info_ptr += 4;
550 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
551 info_ptr += blk->size;
552 DW_BLOCK (attr) = blk;
553 break;
554 case DW_FORM_data2:
555 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
556 info_ptr += 2;
557 break;
558 case DW_FORM_data4:
559 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
560 info_ptr += 4;
561 break;
562 case DW_FORM_data8:
563 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
564 info_ptr += 8;
565 break;
566 case DW_FORM_string:
567 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
568 info_ptr += bytes_read;
569 break;
570 case DW_FORM_block:
571 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
572 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
573 info_ptr += bytes_read;
574 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
575 info_ptr += blk->size;
576 DW_BLOCK (attr) = blk;
577 break;
578 case DW_FORM_block1:
579 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
580 blk->size = read_1_byte (abfd, info_ptr);
581 info_ptr += 1;
582 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
583 info_ptr += blk->size;
584 DW_BLOCK (attr) = blk;
585 break;
586 case DW_FORM_data1:
587 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
588 info_ptr += 1;
589 break;
590 case DW_FORM_flag:
591 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
592 info_ptr += 1;
593 break;
594 case DW_FORM_sdata:
595 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
596 info_ptr += bytes_read;
597 break;
598 case DW_FORM_udata:
599 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
600 info_ptr += bytes_read;
601 break;
602 case DW_FORM_ref1:
603 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
604 info_ptr += 1;
605 break;
606 case DW_FORM_ref2:
607 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
608 info_ptr += 2;
609 break;
610 case DW_FORM_ref4:
611 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
612 info_ptr += 4;
613 break;
614 case DW_FORM_ref_udata:
615 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
616 info_ptr += bytes_read;
617 break;
618 case DW_FORM_strp:
619 case DW_FORM_indirect:
620 default:
621 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %d."),
622 abbrev->form);
623 bfd_set_error (bfd_error_bad_value);
625 return info_ptr;
629 /* Source line information table routines. */
631 #define FILE_ALLOC_CHUNK 5
632 #define DIR_ALLOC_CHUNK 5
634 struct line_info {
635 struct line_info* prev_line;
637 bfd_vma address;
638 char* filename;
639 unsigned int line;
640 unsigned int column;
641 int end_sequence; /* end of (sequential) code sequence */
644 struct fileinfo {
645 char *name;
646 unsigned int dir;
647 unsigned int time;
648 unsigned int size;
651 struct line_info_table {
652 bfd* abfd;
654 unsigned int num_files;
655 unsigned int num_dirs;
657 char* comp_dir;
658 char** dirs;
659 struct fileinfo* files;
660 struct line_info* last_line;
663 static void
664 add_line_info (table, address, filename, line, column, end_sequence)
665 struct line_info_table* table;
666 bfd_vma address;
667 char* filename;
668 unsigned int line;
669 unsigned int column;
670 int end_sequence;
672 struct line_info* info = (struct line_info*)
673 bfd_alloc (table->abfd, sizeof (struct line_info));
675 info->prev_line = table->last_line;
676 table->last_line = info;
678 info->address = address;
679 info->filename = filename;
680 info->line = line;
681 info->column = column;
682 info->end_sequence = end_sequence;
685 static char*
686 concat_filename (table, file)
687 struct line_info_table* table;
688 unsigned int file;
690 char* filename;
692 if (file - 1 >= table->num_files)
694 (*_bfd_error_handler) (_("Dwarf Error: mangled line number "
695 "section (bad file number)."));
696 return "<unknown>";
699 filename = table->files[file - 1].name;
700 if (*filename == '/')
701 return filename;
703 else
705 char* dirname = (table->files[file - 1].dir
706 ? table->dirs[table->files[file - 1].dir - 1]
707 : table->comp_dir);
708 return (char*) concat (dirname, "/", filename, NULL);
712 static void
713 arange_add (unit, low_pc, high_pc)
714 struct comp_unit *unit;
715 bfd_vma low_pc;
716 bfd_vma high_pc;
718 struct arange *arange;
720 /* first see if we can cheaply extend an existing range: */
721 arange = &unit->arange;
724 if (low_pc == arange->high)
726 arange->high = high_pc;
727 return;
729 if (high_pc == arange->low)
731 arange->low = low_pc;
732 return;
734 arange = arange->next;
736 while (arange);
738 if (unit->arange.high == 0)
740 /* this is the first address range: store it in unit->arange: */
741 unit->arange.next = 0;
742 unit->arange.low = low_pc;
743 unit->arange.high = high_pc;
744 return;
747 /* need to allocate a new arange and insert it into the arange list: */
748 arange = bfd_zalloc (unit->abfd, sizeof (*arange));
749 arange->low = low_pc;
750 arange->high = high_pc;
752 arange->next = unit->arange.next;
753 unit->arange.next = arange;
756 /* Decode the line number information for UNIT. */
758 static struct line_info_table*
759 decode_line_info (unit)
760 struct comp_unit *unit;
762 bfd *abfd = unit->abfd;
764 struct dwarf2_debug *stash;
766 struct line_info_table* table;
768 char *line_ptr;
769 char *line_end;
770 struct line_head lh;
771 unsigned int i, bytes_read;
772 char *cur_file, *cur_dir;
773 unsigned char op_code, extended_op, adj_opcode;
775 stash = elf_tdata (abfd)->dwarf2_find_line_info;
777 if (! stash->dwarf_line_buffer)
779 asection *msec;
780 unsigned long size;
782 msec = bfd_get_section_by_name (abfd, ".debug_line");
783 if (! msec)
785 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
786 bfd_set_error (bfd_error_bad_value);
787 return 0;
790 size = bfd_get_section_size_before_reloc (msec);
791 stash->dwarf_line_buffer = (char *) bfd_alloc (abfd, size);
792 if (! stash->dwarf_line_buffer)
793 return 0;
795 if (! bfd_get_section_contents (abfd, msec,
796 stash->dwarf_line_buffer, 0,
797 size))
798 return 0;
800 /* FIXME: We ought to apply the relocs against this section before
801 we process it.... */
804 table = (struct line_info_table*) bfd_alloc (abfd,
805 sizeof (struct line_info_table));
806 table->abfd = abfd;
807 table->comp_dir = unit->comp_dir;
809 table->num_files = 0;
810 table->files = NULL;
812 table->num_dirs = 0;
813 table->dirs = NULL;
815 table->files = NULL;
816 table->last_line = NULL;
818 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
820 /* read in the prologue */
821 lh.total_length = read_4_bytes (abfd, line_ptr);
822 line_ptr += 4;
823 line_end = line_ptr + lh.total_length;
824 lh.version = read_2_bytes (abfd, line_ptr);
825 line_ptr += 2;
826 lh.prologue_length = read_4_bytes (abfd, line_ptr);
827 line_ptr += 4;
828 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
829 line_ptr += 1;
830 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
831 line_ptr += 1;
832 lh.line_base = read_1_signed_byte (abfd, line_ptr);
833 line_ptr += 1;
834 lh.line_range = read_1_byte (abfd, line_ptr);
835 line_ptr += 1;
836 lh.opcode_base = read_1_byte (abfd, line_ptr);
837 line_ptr += 1;
838 lh.standard_opcode_lengths = (unsigned char *)
839 bfd_alloc (abfd, lh.opcode_base * sizeof (unsigned char));
841 lh.standard_opcode_lengths[0] = 1;
842 for (i = 1; i < lh.opcode_base; ++i)
844 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
845 line_ptr += 1;
848 /* Read directory table */
849 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
851 line_ptr += bytes_read;
852 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
854 table->dirs = (char **)
855 bfd_realloc (table->dirs,
856 (table->num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *));
857 if (! table->dirs)
858 return 0;
860 table->dirs[table->num_dirs++] = cur_dir;
862 line_ptr += bytes_read;
864 /* Read file name table */
865 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
867 line_ptr += bytes_read;
868 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
870 table->files = (struct fileinfo *)
871 bfd_realloc (table->files,
872 (table->num_files + FILE_ALLOC_CHUNK)
873 * sizeof (struct fileinfo));
874 if (! table->files)
875 return 0;
877 table->files[table->num_files].name = cur_file;
878 table->files[table->num_files].dir =
879 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
880 line_ptr += bytes_read;
881 table->files[table->num_files].time =
882 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
883 line_ptr += bytes_read;
884 table->files[table->num_files].size =
885 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
886 line_ptr += bytes_read;
887 table->num_files++;
889 line_ptr += bytes_read;
891 /* Read the statement sequences until there's nothing left. */
892 while (line_ptr < line_end)
894 /* state machine registers */
895 bfd_vma address = 0;
896 char* filename = concat_filename (table, 1);
897 unsigned int line = 1;
898 unsigned int column = 0;
899 int is_stmt = lh.default_is_stmt;
900 int basic_block = 0;
901 int end_sequence = 0, need_low_pc = 1;
902 bfd_vma low_pc = 0;
904 /* Decode the table. */
905 while (! end_sequence)
907 op_code = read_1_byte (abfd, line_ptr);
908 line_ptr += 1;
909 switch (op_code)
911 case DW_LNS_extended_op:
912 line_ptr += 1; /* ignore length */
913 extended_op = read_1_byte (abfd, line_ptr);
914 line_ptr += 1;
915 switch (extended_op)
917 case DW_LNE_end_sequence:
918 end_sequence = 1;
919 add_line_info (table, address, filename, line, column,
920 end_sequence);
921 if (need_low_pc)
923 need_low_pc = 0;
924 low_pc = address;
926 arange_add (unit, low_pc, address);
927 break;
928 case DW_LNE_set_address:
929 address = read_address (unit, line_ptr);
930 line_ptr += unit->addr_size;
931 break;
932 case DW_LNE_define_file:
933 cur_file = read_string (abfd, line_ptr, &bytes_read);
934 line_ptr += bytes_read;
935 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
937 table->files = (struct fileinfo *)
938 bfd_realloc (table->files,
939 (table->num_files + FILE_ALLOC_CHUNK)
940 * sizeof (struct fileinfo));
941 if (! table->files)
942 return 0;
944 table->files[table->num_files].name = cur_file;
945 table->files[table->num_files].dir =
946 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
947 line_ptr += bytes_read;
948 table->files[table->num_files].time =
949 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
950 line_ptr += bytes_read;
951 table->files[table->num_files].size =
952 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
953 line_ptr += bytes_read;
954 table->num_files++;
955 break;
956 default:
957 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
958 bfd_set_error (bfd_error_bad_value);
959 return 0;
961 break;
962 case DW_LNS_copy:
963 add_line_info (table, address, filename, line, column, 0);
964 basic_block = 0;
965 if (need_low_pc)
967 need_low_pc = 0;
968 low_pc = address;
970 break;
971 case DW_LNS_advance_pc:
972 address += lh.minimum_instruction_length
973 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
974 line_ptr += bytes_read;
975 break;
976 case DW_LNS_advance_line:
977 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
978 line_ptr += bytes_read;
979 break;
980 case DW_LNS_set_file:
982 unsigned int file;
984 /* The file and directory tables are 0 based, the references
985 are 1 based. */
986 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
987 line_ptr += bytes_read;
988 filename = concat_filename (table, file);
989 break;
991 case DW_LNS_set_column:
992 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
993 line_ptr += bytes_read;
994 break;
995 case DW_LNS_negate_stmt:
996 is_stmt = (!is_stmt);
997 break;
998 case DW_LNS_set_basic_block:
999 basic_block = 1;
1000 break;
1001 case DW_LNS_const_add_pc:
1002 address += lh.minimum_instruction_length
1003 * ((255 - lh.opcode_base) / lh.line_range);
1004 break;
1005 case DW_LNS_fixed_advance_pc:
1006 address += read_2_bytes (abfd, line_ptr);
1007 line_ptr += 2;
1008 break;
1009 default: /* special operand */
1010 adj_opcode = op_code - lh.opcode_base;
1011 address += (adj_opcode / lh.line_range)
1012 * lh.minimum_instruction_length;
1013 line += lh.line_base + (adj_opcode % lh.line_range);
1014 /* append row to matrix using current values */
1015 add_line_info (table, address, filename, line, column, 0);
1016 basic_block = 1;
1017 if (need_low_pc)
1019 need_low_pc = 0;
1020 low_pc = address;
1026 return table;
1030 /* If ADDR is within TABLE set the output parameters and return true,
1031 otherwise return false. The output parameters, FILENAME_PTR and
1032 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1034 static boolean
1035 lookup_address_in_line_info_table (table,
1036 addr,
1037 filename_ptr,
1038 linenumber_ptr)
1039 struct line_info_table* table;
1040 bfd_vma addr;
1041 const char **filename_ptr;
1042 unsigned int *linenumber_ptr;
1044 struct line_info* next_line = table->last_line;
1045 struct line_info* each_line;
1047 if (!next_line)
1048 return false;
1050 each_line = next_line->prev_line;
1052 while (each_line && next_line)
1054 if (!each_line->end_sequence
1055 && addr >= each_line->address && addr < next_line->address)
1057 *filename_ptr = each_line->filename;
1058 *linenumber_ptr = each_line->line;
1059 return true;
1061 next_line = each_line;
1062 each_line = each_line->prev_line;
1065 return false;
1071 /* Function table functions. */
1073 struct funcinfo {
1074 struct funcinfo *prev_func;
1076 char* name;
1077 bfd_vma low;
1078 bfd_vma high;
1082 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
1084 static boolean
1085 lookup_address_in_function_table (table,
1086 addr,
1087 functionname_ptr)
1088 struct funcinfo* table;
1089 bfd_vma addr;
1090 const char **functionname_ptr;
1092 struct funcinfo* each_func;
1094 for (each_func = table;
1095 each_func;
1096 each_func = each_func->prev_func)
1098 if (addr >= each_func->low && addr < each_func->high)
1100 *functionname_ptr = each_func->name;
1101 return true;
1105 return false;
1111 /* DWARF2 Compilation unit functions. */
1114 /* Scan over each die in a comp. unit looking for functions to add
1115 to the function table. */
1117 static boolean
1118 scan_unit_for_functions (unit)
1119 struct comp_unit *unit;
1121 bfd *abfd = unit->abfd;
1122 char *info_ptr = unit->first_child_die_ptr;
1123 int nesting_level = 1;
1125 while (nesting_level)
1127 unsigned int abbrev_number, bytes_read, i;
1128 struct abbrev_info *abbrev;
1129 struct attribute attr;
1130 struct funcinfo *func;
1131 char* name = 0;
1133 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1134 info_ptr += bytes_read;
1136 if (! abbrev_number)
1138 nesting_level--;
1139 continue;
1142 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1143 if (! abbrev)
1145 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1146 abbrev_number);
1147 bfd_set_error (bfd_error_bad_value);
1148 return false;
1151 if (abbrev->tag == DW_TAG_subprogram)
1153 func = (struct funcinfo*) bfd_zalloc (abfd, sizeof (struct funcinfo));
1154 func->prev_func = unit->function_table;
1155 unit->function_table = func;
1157 else
1158 func = NULL;
1160 for (i = 0; i < abbrev->num_attrs; ++i)
1162 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1164 if (func)
1166 switch (attr.name)
1168 case DW_AT_name:
1170 name = DW_STRING (&attr);
1172 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1173 if (func->name == NULL)
1174 func->name = DW_STRING (&attr);
1175 break;
1177 case DW_AT_MIPS_linkage_name:
1178 func->name = DW_STRING (&attr);
1179 break;
1181 case DW_AT_low_pc:
1182 func->low = DW_ADDR (&attr);
1183 break;
1185 case DW_AT_high_pc:
1186 func->high = DW_ADDR (&attr);
1187 break;
1189 default:
1190 break;
1193 else
1195 switch (attr.name)
1197 case DW_AT_name:
1198 name = DW_STRING (&attr);
1199 break;
1201 default:
1202 break;
1207 if (abbrev->has_children)
1208 nesting_level++;
1211 return true;
1219 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1220 includes the compilation unit header that proceeds the DIE's, but
1221 does not include the length field that preceeds each compilation
1222 unit header. END_PTR points one past the end of this comp unit.
1223 If ABBREV_LENGTH is 0, then the length of the abbreviation offset
1224 is assumed to be four bytes. Otherwise, it it is the size given.
1226 This routine does not read the whole compilation unit; only enough
1227 to get to the line number information for the compilation unit. */
1229 static struct comp_unit *
1230 parse_comp_unit (abfd, info_ptr, end_ptr, abbrev_length)
1231 bfd* abfd;
1232 char* info_ptr;
1233 char* end_ptr;
1234 unsigned int abbrev_length;
1236 struct comp_unit* unit;
1238 unsigned short version;
1239 unsigned int abbrev_offset = 0;
1240 unsigned char addr_size;
1241 struct abbrev_info** abbrevs;
1243 unsigned int abbrev_number, bytes_read, i;
1244 struct abbrev_info *abbrev;
1245 struct attribute attr;
1247 version = read_2_bytes (abfd, info_ptr);
1248 info_ptr += 2;
1249 BFD_ASSERT (abbrev_length == 0
1250 || abbrev_length == 4
1251 || abbrev_length == 8);
1252 if (abbrev_length == 0 || abbrev_length == 4)
1253 abbrev_offset = read_4_bytes (abfd, info_ptr);
1254 else if (abbrev_length == 8)
1255 abbrev_offset = read_8_bytes (abfd, info_ptr);
1256 info_ptr += abbrev_length;
1257 addr_size = read_1_byte (abfd, info_ptr);
1258 info_ptr += 1;
1260 if (version != 2)
1262 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information."), version );
1263 bfd_set_error (bfd_error_bad_value);
1264 return 0;
1267 if (addr_size > sizeof (bfd_vma))
1269 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1270 addr_size,
1271 sizeof (bfd_vma));
1272 bfd_set_error (bfd_error_bad_value);
1273 return 0;
1276 if (addr_size != 4 && addr_size != 8)
1278 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '4' and '8'.", addr_size );
1279 bfd_set_error (bfd_error_bad_value);
1280 return 0;
1283 /* Read the abbrevs for this compilation unit into a table */
1284 abbrevs = read_abbrevs (abfd, abbrev_offset);
1285 if (! abbrevs)
1286 return 0;
1288 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1289 info_ptr += bytes_read;
1290 if (! abbrev_number)
1292 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %d."),
1293 abbrev_number);
1294 bfd_set_error (bfd_error_bad_value);
1295 return 0;
1298 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1299 if (! abbrev)
1301 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1302 abbrev_number);
1303 bfd_set_error (bfd_error_bad_value);
1304 return 0;
1307 unit = (struct comp_unit*) bfd_zalloc (abfd, sizeof (struct comp_unit));
1308 unit->abfd = abfd;
1309 unit->addr_size = addr_size;
1310 unit->abbrevs = abbrevs;
1311 unit->end_ptr = end_ptr;
1313 for (i = 0; i < abbrev->num_attrs; ++i)
1315 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1317 /* Store the data if it is of an attribute we want to keep in a
1318 partial symbol table. */
1319 switch (attr.name)
1321 case DW_AT_stmt_list:
1322 unit->stmtlist = 1;
1323 unit->line_offset = DW_UNSND (&attr);
1324 break;
1326 case DW_AT_name:
1327 unit->name = DW_STRING (&attr);
1328 break;
1330 case DW_AT_low_pc:
1331 unit->arange.low = DW_ADDR (&attr);
1332 break;
1334 case DW_AT_high_pc:
1335 unit->arange.high = DW_ADDR (&attr);
1336 break;
1338 case DW_AT_comp_dir:
1340 char* comp_dir = DW_STRING (&attr);
1341 if (comp_dir)
1343 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1344 directory, get rid of it. */
1345 char *cp = (char*) strchr (comp_dir, ':');
1347 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1348 comp_dir = cp + 1;
1350 unit->comp_dir = comp_dir;
1351 break;
1354 default:
1355 break;
1359 unit->first_child_die_ptr = info_ptr;
1360 return unit;
1367 /* Return true if UNIT contains the address given by ADDR. */
1369 static boolean
1370 comp_unit_contains_address (unit, addr)
1371 struct comp_unit* unit;
1372 bfd_vma addr;
1374 struct arange *arange;
1376 if (unit->error)
1377 return 0;
1379 arange = &unit->arange;
1382 if (addr >= arange->low && addr < arange->high)
1383 return 1;
1384 arange = arange->next;
1386 while (arange);
1387 return 0;
1391 /* If UNIT contains ADDR, set the output parameters to the values for
1392 the line containing ADDR. The output parameters, FILENAME_PTR,
1393 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1394 to be filled in.
1396 Return true of UNIT contains ADDR, and no errors were encountered;
1397 false otherwise. */
1399 static boolean
1400 comp_unit_find_nearest_line (unit, addr,
1401 filename_ptr, functionname_ptr, linenumber_ptr)
1402 struct comp_unit* unit;
1403 bfd_vma addr;
1404 const char **filename_ptr;
1405 const char **functionname_ptr;
1406 unsigned int *linenumber_ptr;
1408 boolean line_p;
1409 boolean func_p;
1411 if (unit->error)
1412 return false;
1414 if (! unit->line_table)
1416 if (! unit->stmtlist)
1418 unit->error = 1;
1419 return false;
1422 unit->line_table = decode_line_info (unit);
1424 if (! unit->line_table)
1426 unit->error = 1;
1427 return false;
1430 if (! scan_unit_for_functions (unit))
1432 unit->error = 1;
1433 return false;
1437 line_p = lookup_address_in_line_info_table (unit->line_table,
1438 addr,
1439 filename_ptr,
1440 linenumber_ptr);
1441 func_p = lookup_address_in_function_table (unit->function_table,
1442 addr,
1443 functionname_ptr);
1444 return line_p || func_p;
1447 /* The DWARF2 version of find_nearest line. Return true if the line
1448 is found without error. ADDR_SIZE is the number of bytes in the
1449 initial .debug_info length field and in the abbreviation offset.
1450 You may use zero to indicate that the default value should be
1451 used. */
1453 boolean
1454 _bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
1455 filename_ptr, functionname_ptr,
1456 linenumber_ptr,
1457 addr_size)
1458 bfd *abfd;
1459 asection *section;
1460 asymbol **symbols ATTRIBUTE_UNUSED;
1461 bfd_vma offset;
1462 const char **filename_ptr;
1463 const char **functionname_ptr;
1464 unsigned int *linenumber_ptr;
1465 unsigned int addr_size;
1467 /* Read each compilation unit from the section .debug_info, and check
1468 to see if it contains the address we are searching for. If yes,
1469 lookup the address, and return the line number info. If no, go
1470 on to the next compilation unit.
1472 We keep a list of all the previously read compilation units, and
1473 a pointer to the next un-read compilation unit. Check the
1474 previously read units before reading more.
1477 struct dwarf2_debug *stash = elf_tdata (abfd)->dwarf2_find_line_info;
1479 /* What address are we looking for? */
1480 bfd_vma addr = offset + section->vma;
1482 struct comp_unit* each;
1484 *filename_ptr = NULL;
1485 *functionname_ptr = NULL;
1486 *linenumber_ptr = 0;
1488 /* The DWARF2 spec says that the initial length field, and the
1489 offset of the abbreviation table, should both be 4-byte values.
1490 However, some compilers do things differently. */
1491 if (addr_size == 0)
1492 addr_size = 4;
1493 BFD_ASSERT (addr_size == 4 || addr_size == 8);
1495 if (! stash)
1497 asection *msec;
1498 unsigned long size;
1500 stash = elf_tdata (abfd)->dwarf2_find_line_info =
1501 (struct dwarf2_debug*) bfd_zalloc (abfd, sizeof (struct dwarf2_debug));
1503 if (! stash)
1504 return false;
1506 msec = bfd_get_section_by_name (abfd, ".debug_info");
1507 if (! msec)
1509 /* No dwarf2 info. Note that at this point the stash
1510 has been allocated, but contains zeros, this lets
1511 future calls to this function fail quicker. */
1512 return false;
1515 size = bfd_get_section_size_before_reloc (msec);
1516 if (size == 0)
1517 return false;
1519 stash->info_ptr = (char *) bfd_alloc (abfd, size);
1521 if (! stash->info_ptr)
1522 return false;
1524 if (! bfd_get_section_contents (abfd, msec, stash->info_ptr, 0, size))
1526 stash->info_ptr = 0;
1527 return false;
1530 stash->info_ptr_end = stash->info_ptr + size;
1532 /* FIXME: There is a problem with the contents of the
1533 .debug_info section. The 'low' and 'high' addresses of the
1534 comp_units are computed by relocs against symbols in the
1535 .text segment. We need these addresses in order to determine
1536 the nearest line number, and so we have to resolve the
1537 relocs. There is a similar problem when the .debug_line
1538 section is processed as well (e.g., there may be relocs
1539 against the operand of the DW_LNE_set_address operator).
1541 Unfortunately getting hold of the reloc information is hard...
1543 For now, this means that disassembling object files (as
1544 opposed to fully executables) does not always work as well as
1545 we would like. */
1548 /* A null info_ptr indicates that there is no dwarf2 info
1549 (or that an error occured while setting up the stash). */
1551 if (! stash->info_ptr)
1552 return false;
1554 /* Check the previously read comp. units first. */
1556 for (each = stash->all_comp_units; each; each = each->next_unit)
1557 if (comp_unit_contains_address (each, addr))
1558 return comp_unit_find_nearest_line (each, addr, filename_ptr,
1559 functionname_ptr, linenumber_ptr);
1561 /* Read each remaining comp. units checking each as they are read. */
1562 while (stash->info_ptr < stash->info_ptr_end)
1564 struct comp_unit* each;
1565 bfd_vma length;
1566 boolean found;
1568 if (addr_size == 4)
1569 length = read_4_bytes (abfd, stash->info_ptr);
1570 else
1571 length = read_8_bytes (abfd, stash->info_ptr);
1572 stash->info_ptr += addr_size;
1574 if (length > 0)
1576 each = parse_comp_unit (abfd, stash->info_ptr,
1577 stash->info_ptr + length,
1578 addr_size);
1579 stash->info_ptr += length;
1581 if (each)
1583 each->next_unit = stash->all_comp_units;
1584 stash->all_comp_units = each;
1586 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1587 compilation units. If we don't have them (i.e.,
1588 unit->high == 0), we need to consult the line info
1589 table to see if a compilation unit contains the given
1590 address. */
1591 if (each->arange.high > 0)
1593 if (comp_unit_contains_address (each, addr))
1594 return comp_unit_find_nearest_line (each, addr,
1595 filename_ptr,
1596 functionname_ptr,
1597 linenumber_ptr);
1599 else
1601 found = comp_unit_find_nearest_line (each, addr,
1602 filename_ptr,
1603 functionname_ptr,
1604 linenumber_ptr);
1605 if (found)
1606 return true;
1612 return false;
1615 /* end of file */