* hosts/alphalinux.h (TRAD_CORE_EXTRA_SIZE_ALLOWED): Expand to 4096.
[binutils.git] / bfd / dwarf2.c
blob1ea7cffb3d9d0d39edde8b068446460c28b6bb70
1 /* DWARF 2 support.
2 Copyright 1994, 1995, 1996, 1997, 1998 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;
106 /* A minimal decoding of DWARF2 compilation units. We only decode
107 what's needed to get to the line number information. */
109 struct comp_unit {
111 /* Chain the previously read compilation units. */
112 struct comp_unit* next_unit;
114 /* Keep the bdf convenient (for memory allocation). */
115 bfd* abfd;
117 /* The lowest and higest addresses contained in this compilation
118 unit as specified in the compilation unit header. */
119 bfd_vma low;
120 bfd_vma high;
122 /* The DW_AT_name attribute (for error messages). */
123 char* name;
125 /* The abbrev hash table. */
126 struct abbrev_info** abbrevs;
128 /* Note that an error was found by comp_unit_find_nearest_line. */
129 int error;
131 /* The DW_AT_comp_dir attribute */
132 char* comp_dir;
134 /* True if there is a line number table associated with this comp. unit. */
135 int stmtlist;
137 /* The offset into .debug_line of the line number table. */
138 unsigned long line_offset;
140 /* Pointer to the first child die for the comp unit. */
141 char *first_child_die_ptr;
143 /* The end of the comp unit. */
144 char *end_ptr;
146 /* The decoded line number, NULL if not yet decoded. */
147 struct line_info_table* line_table;
149 /* A list of the functions found in this comp. unit. */
150 struct funcinfo* function_table;
152 /* Address size for this unit - from unit header */
153 unsigned char addr_size;
158 /* VERBATIM
159 The following function up to the END VERBATIM mark are
160 copied directly from dwarf2read.c. */
162 /* read dwarf information from a buffer */
164 static unsigned int
165 read_1_byte (abfd, buf)
166 bfd *abfd;
167 char *buf;
169 return bfd_get_8 (abfd, (bfd_byte *) buf);
172 static int
173 read_1_signed_byte (abfd, buf)
174 bfd *abfd;
175 char *buf;
177 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
180 static unsigned int
181 read_2_bytes (abfd, buf)
182 bfd *abfd;
183 char *buf;
185 return bfd_get_16 (abfd, (bfd_byte *) buf);
188 #if 0
190 /* This is not used. */
192 static int
193 read_2_signed_bytes (abfd, buf)
194 bfd *abfd;
195 char *buf;
197 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
200 #endif
202 static unsigned int
203 read_4_bytes (abfd, buf)
204 bfd *abfd;
205 char *buf;
207 return bfd_get_32 (abfd, (bfd_byte *) buf);
210 #if 0
212 /* This is not used. */
214 static int
215 read_4_signed_bytes (abfd, buf)
216 bfd *abfd;
217 char *buf;
219 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
222 #endif
224 static unsigned int
225 read_8_bytes (abfd, buf)
226 bfd *abfd;
227 char *buf;
229 return bfd_get_64 (abfd, (bfd_byte *) buf);
232 static char *
233 read_n_bytes (abfd, buf, size)
234 bfd * abfd;
235 char *buf;
236 unsigned int size;
238 /* If the size of a host char is 8 bits, we can return a pointer
239 to the buffer, otherwise we have to copy the data to a buffer
240 allocated on the temporary obstack. */
241 return buf;
244 static char *
245 read_string (abfd, buf, bytes_read_ptr)
246 bfd *abfd;
247 char *buf;
248 unsigned int *bytes_read_ptr;
250 /* If the size of a host char is 8 bits, we can return a pointer
251 to the string, otherwise we have to copy the string to a buffer
252 allocated on the temporary obstack. */
253 if (*buf == '\0')
255 *bytes_read_ptr = 1;
256 return NULL;
258 *bytes_read_ptr = strlen (buf) + 1;
259 return buf;
262 static unsigned int
263 read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
264 bfd *abfd;
265 char *buf;
266 unsigned int *bytes_read_ptr;
268 unsigned int result;
269 unsigned int num_read;
270 int shift;
271 unsigned char byte;
273 result = 0;
274 shift = 0;
275 num_read = 0;
279 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
280 buf ++;
281 num_read ++;
282 result |= ((byte & 0x7f) << shift);
283 shift += 7;
285 while (byte & 0x80);
287 * bytes_read_ptr = num_read;
289 return result;
292 static int
293 read_signed_leb128 (abfd, buf, bytes_read_ptr)
294 bfd * abfd;
295 char * buf;
296 unsigned int * bytes_read_ptr;
298 int result;
299 int shift;
300 int num_read;
301 unsigned char byte;
303 result = 0;
304 shift = 0;
305 num_read = 0;
309 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
310 buf ++;
311 num_read ++;
312 result |= ((byte & 0x7f) << shift);
313 shift += 7;
315 while (byte & 0x80);
317 if ((shift < 32) && (byte & 0x40))
318 result |= -(1 << shift);
320 * bytes_read_ptr = num_read;
322 return result;
325 /* END VERBATIM */
327 static bfd_vma
328 read_address (unit, buf)
329 struct comp_unit* unit;
330 char *buf;
332 bfd_vma retval = 0;
334 if (unit->addr_size == 4)
336 retval = bfd_get_32 (unit->abfd, (bfd_byte *) buf);
337 } else {
338 retval = bfd_get_64 (unit->abfd, (bfd_byte *) buf);
340 return retval;
347 /* This data structure holds the information of an abbrev. */
348 struct abbrev_info
350 unsigned int number; /* number identifying abbrev */
351 enum dwarf_tag tag; /* dwarf tag */
352 int has_children; /* boolean */
353 unsigned int num_attrs; /* number of attributes */
354 struct attr_abbrev *attrs; /* an array of attribute descriptions */
355 struct abbrev_info *next; /* next in chain */
358 struct attr_abbrev
360 enum dwarf_attribute name;
361 enum dwarf_form form;
364 #ifndef ABBREV_HASH_SIZE
365 #define ABBREV_HASH_SIZE 121
366 #endif
367 #ifndef ATTR_ALLOC_CHUNK
368 #define ATTR_ALLOC_CHUNK 4
369 #endif
371 /* Lookup an abbrev_info structure in the abbrev hash table. */
373 static struct abbrev_info *
374 lookup_abbrev (number,abbrevs)
375 unsigned int number;
376 struct abbrev_info **abbrevs;
378 unsigned int hash_number;
379 struct abbrev_info *abbrev;
381 hash_number = number % ABBREV_HASH_SIZE;
382 abbrev = abbrevs[hash_number];
384 while (abbrev)
386 if (abbrev->number == number)
387 return abbrev;
388 else
389 abbrev = abbrev->next;
391 return NULL;
394 /* In DWARF version 2, the description of the debugging information is
395 stored in a separate .debug_abbrev section. Before we read any
396 dies from a section we read in all abbreviations and install them
397 in a hash table. */
399 static struct abbrev_info**
400 read_abbrevs (abfd, offset)
401 bfd * abfd;
402 unsigned int offset;
404 struct abbrev_info **abbrevs;
405 char *abbrev_ptr;
406 struct abbrev_info *cur_abbrev;
407 unsigned int abbrev_number, bytes_read, abbrev_name;
408 unsigned int abbrev_form, hash_number;
409 struct dwarf2_debug *stash;
411 stash = elf_tdata(abfd)->dwarf2_find_line_info;
413 if (! stash->dwarf_abbrev_buffer)
415 asection *msec;
417 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
418 if (! msec)
420 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
421 bfd_set_error (bfd_error_bad_value);
422 return 0;
425 stash->dwarf_abbrev_size = bfd_get_section_size_before_reloc (msec);
426 stash->dwarf_abbrev_buffer = (char*) bfd_alloc (abfd, stash->dwarf_abbrev_size);
427 if (! stash->dwarf_abbrev_buffer)
428 return 0;
430 if (! bfd_get_section_contents (abfd, msec,
431 stash->dwarf_abbrev_buffer, 0,
432 stash->dwarf_abbrev_size))
433 return 0;
436 if (offset > stash->dwarf_abbrev_size)
438 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%u) bigger than abbrev size (%u)."),
439 offset, stash->dwarf_abbrev_size );
440 bfd_set_error (bfd_error_bad_value);
441 return 0;
444 abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, sizeof(struct abbrev_info*) * ABBREV_HASH_SIZE);
446 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
447 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
448 abbrev_ptr += bytes_read;
450 /* loop until we reach an abbrev number of 0 */
451 while (abbrev_number)
453 cur_abbrev = (struct abbrev_info*)bfd_zalloc (abfd, sizeof (struct abbrev_info));
455 /* read in abbrev header */
456 cur_abbrev->number = abbrev_number;
457 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
458 abbrev_ptr += bytes_read;
459 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
460 abbrev_ptr += 1;
462 /* now read in declarations */
463 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
464 abbrev_ptr += bytes_read;
465 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
466 abbrev_ptr += bytes_read;
467 while (abbrev_name)
469 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
471 cur_abbrev->attrs = (struct attr_abbrev *)
472 bfd_realloc (cur_abbrev->attrs,
473 (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)
474 * sizeof (struct attr_abbrev));
475 if (! cur_abbrev->attrs)
476 return 0;
478 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
479 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
480 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
481 abbrev_ptr += bytes_read;
482 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
483 abbrev_ptr += bytes_read;
486 hash_number = abbrev_number % ABBREV_HASH_SIZE;
487 cur_abbrev->next = abbrevs[hash_number];
488 abbrevs[hash_number] = cur_abbrev;
490 /* Get next abbreviation.
491 Under Irix6 the abbreviations for a compilation unit are not
492 always properly terminated with an abbrev number of 0.
493 Exit loop if we encounter an abbreviation which we have
494 already read (which means we are about to read the abbreviations
495 for the next compile unit) or if the end of the abbreviation
496 table is reached. */
497 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
498 >= stash->dwarf_abbrev_size)
499 break;
500 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
501 abbrev_ptr += bytes_read;
502 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
503 break;
506 return abbrevs;
509 /* Read an attribute described by an abbreviated attribute. */
511 static char *
512 read_attribute (attr, abbrev, unit, info_ptr)
513 struct attribute *attr;
514 struct attr_abbrev *abbrev;
515 struct comp_unit *unit;
516 char *info_ptr;
518 bfd *abfd = unit->abfd;
519 unsigned int bytes_read;
520 struct dwarf_block *blk;
522 attr->name = abbrev->name;
523 attr->form = abbrev->form;
524 switch (abbrev->form)
526 case DW_FORM_addr:
527 case DW_FORM_ref_addr:
528 DW_ADDR (attr) = read_address (unit, info_ptr);
529 info_ptr += unit->addr_size;
530 break;
531 case DW_FORM_block2:
532 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
533 blk->size = read_2_bytes (abfd, info_ptr);
534 info_ptr += 2;
535 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
536 info_ptr += blk->size;
537 DW_BLOCK (attr) = blk;
538 break;
539 case DW_FORM_block4:
540 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
541 blk->size = read_4_bytes (abfd, info_ptr);
542 info_ptr += 4;
543 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
544 info_ptr += blk->size;
545 DW_BLOCK (attr) = blk;
546 break;
547 case DW_FORM_data2:
548 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
549 info_ptr += 2;
550 break;
551 case DW_FORM_data4:
552 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
553 info_ptr += 4;
554 break;
555 case DW_FORM_data8:
556 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
557 info_ptr += 8;
558 break;
559 case DW_FORM_string:
560 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
561 info_ptr += bytes_read;
562 break;
563 case DW_FORM_block:
564 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
565 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
566 info_ptr += bytes_read;
567 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
568 info_ptr += blk->size;
569 DW_BLOCK (attr) = blk;
570 break;
571 case DW_FORM_block1:
572 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
573 blk->size = read_1_byte (abfd, info_ptr);
574 info_ptr += 1;
575 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
576 info_ptr += blk->size;
577 DW_BLOCK (attr) = blk;
578 break;
579 case DW_FORM_data1:
580 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
581 info_ptr += 1;
582 break;
583 case DW_FORM_flag:
584 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
585 info_ptr += 1;
586 break;
587 case DW_FORM_sdata:
588 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
589 info_ptr += bytes_read;
590 break;
591 case DW_FORM_udata:
592 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
593 info_ptr += bytes_read;
594 break;
595 case DW_FORM_ref1:
596 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
597 info_ptr += 1;
598 break;
599 case DW_FORM_ref2:
600 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
601 info_ptr += 2;
602 break;
603 case DW_FORM_ref4:
604 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
605 info_ptr += 4;
606 break;
607 case DW_FORM_ref_udata:
608 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
609 info_ptr += bytes_read;
610 break;
611 case DW_FORM_strp:
612 case DW_FORM_indirect:
613 default:
614 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %d."),
615 abbrev->form);
616 bfd_set_error (bfd_error_bad_value);
618 return info_ptr;
622 /* Source line information table routines. */
624 #define FILE_ALLOC_CHUNK 5
625 #define DIR_ALLOC_CHUNK 5
627 struct line_info {
628 struct line_info* prev_line;
630 bfd_vma address;
631 char* filename;
632 unsigned int line;
633 unsigned int column;
636 struct fileinfo {
637 char *name;
638 unsigned int dir;
639 unsigned int time;
640 unsigned int size;
643 struct line_info_table {
644 bfd* abfd;
646 unsigned int num_files;
647 unsigned int num_dirs;
649 char* comp_dir;
650 char** dirs;
651 struct fileinfo* files;
652 struct line_info* last_line;
655 static void
656 add_line_info (table, address, filename, line, column)
657 struct line_info_table* table;
658 bfd_vma address;
659 char* filename;
660 unsigned int line;
661 unsigned int column;
663 struct line_info* info = (struct line_info*)
664 bfd_alloc (table->abfd, sizeof (struct line_info));
666 info->prev_line = table->last_line;
667 table->last_line = info;
669 info->address = address;
670 info->filename = filename;
671 info->line = line;
672 info->column = column;
675 static char*
676 concat_filename (table, file)
677 struct line_info_table* table;
678 unsigned int file;
680 char* filename = table->files[file - 1].name;
681 if (*filename == '/')
682 return filename;
684 else
686 char* dirname = (table->files[file - 1].dir
687 ? table->dirs[table->files[file - 1].dir - 1]
688 : table->comp_dir);
689 return (char*) concat (dirname, "/", filename, NULL);
693 /* Decode the line number information for UNIT. */
695 static struct line_info_table*
696 decode_line_info (unit)
697 struct comp_unit *unit;
699 bfd *abfd = unit->abfd;
701 static char* dwarf_line_buffer = 0;
703 struct line_info_table* table;
705 char *line_ptr;
706 char *line_end;
707 struct line_head lh;
708 unsigned int i, bytes_read;
709 char *cur_file, *cur_dir;
710 unsigned char op_code, extended_op, adj_opcode;
712 if (! dwarf_line_buffer)
714 asection *msec;
715 unsigned long size;
717 msec = bfd_get_section_by_name (abfd, ".debug_line");
718 if (! msec)
720 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
721 bfd_set_error (bfd_error_bad_value);
722 return 0;
725 size = bfd_get_section_size_before_reloc (msec);
726 dwarf_line_buffer = (char*) bfd_alloc (abfd, size);
727 if (! dwarf_line_buffer)
728 return 0;
730 if (! bfd_get_section_contents (abfd, msec,
731 dwarf_line_buffer, 0,
732 size))
733 return 0;
735 /* FIXME: We ought to apply the relocs against this section before
736 we process it.... */
739 table = (struct line_info_table*) bfd_alloc (abfd,
740 sizeof (struct line_info_table));
741 table->abfd = abfd;
742 table->comp_dir = unit->comp_dir;
744 table->num_files = 0;
745 table->files = NULL;
747 table->num_dirs = 0;
748 table->dirs = NULL;
750 line_ptr = dwarf_line_buffer + unit->line_offset;
752 /* read in the prologue */
753 lh.total_length = read_4_bytes (abfd, line_ptr);
754 line_ptr += 4;
755 line_end = line_ptr + lh.total_length;
756 lh.version = read_2_bytes (abfd, line_ptr);
757 line_ptr += 2;
758 lh.prologue_length = read_4_bytes (abfd, line_ptr);
759 line_ptr += 4;
760 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
761 line_ptr += 1;
762 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
763 line_ptr += 1;
764 lh.line_base = read_1_signed_byte (abfd, line_ptr);
765 line_ptr += 1;
766 lh.line_range = read_1_byte (abfd, line_ptr);
767 line_ptr += 1;
768 lh.opcode_base = read_1_byte (abfd, line_ptr);
769 line_ptr += 1;
770 lh.standard_opcode_lengths = (unsigned char *)
771 bfd_alloc (abfd, lh.opcode_base * sizeof (unsigned char));
773 lh.standard_opcode_lengths[0] = 1;
774 for (i = 1; i < lh.opcode_base; ++i)
776 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
777 line_ptr += 1;
780 /* Read directory table */
781 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
783 line_ptr += bytes_read;
784 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
786 table->dirs = (char **)
787 bfd_realloc (table->dirs,
788 (table->num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *));
789 if (! table->dirs)
790 return 0;
792 table->dirs[table->num_dirs++] = cur_dir;
794 line_ptr += bytes_read;
796 /* Read file name table */
797 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
799 line_ptr += bytes_read;
800 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
802 table->files = (struct fileinfo *)
803 bfd_realloc (table->files,
804 (table->num_files + FILE_ALLOC_CHUNK)
805 * sizeof (struct fileinfo));
806 if (! table->files)
807 return 0;
809 table->files[table->num_files].name = cur_file;
810 table->files[table->num_files].dir =
811 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
812 line_ptr += bytes_read;
813 table->files[table->num_files].time =
814 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
815 line_ptr += bytes_read;
816 table->files[table->num_files].size =
817 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
818 line_ptr += bytes_read;
819 table->num_files++;
821 line_ptr += bytes_read;
823 /* Read the statement sequences until there's nothing left. */
824 while (line_ptr < line_end)
826 /* state machine registers */
827 bfd_vma address = 0;
828 char* filename = concat_filename (table, 1);
829 unsigned int line = 1;
830 unsigned int column = 0;
831 int is_stmt = lh.default_is_stmt;
832 int basic_block = 0;
833 int end_sequence = 0;
835 /* Decode the table. */
836 while (! end_sequence)
838 op_code = read_1_byte (abfd, line_ptr);
839 line_ptr += 1;
840 switch (op_code)
842 case DW_LNS_extended_op:
843 line_ptr += 1; /* ignore length */
844 extended_op = read_1_byte (abfd, line_ptr);
845 line_ptr += 1;
846 switch (extended_op)
848 case DW_LNE_end_sequence:
849 end_sequence = 1;
850 add_line_info (table, address, filename, line, column);
851 break;
852 case DW_LNE_set_address:
853 address = read_address (unit, line_ptr);
854 line_ptr += unit->addr_size;
855 break;
856 case DW_LNE_define_file:
857 cur_file = read_string (abfd, line_ptr, &bytes_read);
858 line_ptr += bytes_read;
859 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
861 table->files = (struct fileinfo *)
862 bfd_realloc (table->files,
863 (table->num_files + FILE_ALLOC_CHUNK)
864 * sizeof (struct fileinfo));
865 if (! table->files)
866 return 0;
868 table->files[table->num_files].name = cur_file;
869 table->files[table->num_files].dir =
870 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
871 line_ptr += bytes_read;
872 table->files[table->num_files].time =
873 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
874 line_ptr += bytes_read;
875 table->files[table->num_files].size =
876 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
877 line_ptr += bytes_read;
878 table->num_files++;
879 break;
880 default:
881 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
882 bfd_set_error (bfd_error_bad_value);
883 return 0;
885 break;
886 case DW_LNS_copy:
887 add_line_info (table, address, filename, line, column);
888 basic_block = 0;
889 break;
890 case DW_LNS_advance_pc:
891 address += lh.minimum_instruction_length
892 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
893 line_ptr += bytes_read;
894 break;
895 case DW_LNS_advance_line:
896 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
897 line_ptr += bytes_read;
898 break;
899 case DW_LNS_set_file:
901 unsigned int file;
903 /* The file and directory tables are 0 based, the references
904 are 1 based. */
905 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
906 line_ptr += bytes_read;
907 filename = concat_filename (table, file);
908 break;
910 case DW_LNS_set_column:
911 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
912 line_ptr += bytes_read;
913 break;
914 case DW_LNS_negate_stmt:
915 is_stmt = (!is_stmt);
916 break;
917 case DW_LNS_set_basic_block:
918 basic_block = 1;
919 break;
920 case DW_LNS_const_add_pc:
921 address += (255 - lh.opcode_base) / lh.line_range;
922 break;
923 case DW_LNS_fixed_advance_pc:
924 address += read_2_bytes (abfd, line_ptr);
925 line_ptr += 2;
926 break;
927 default: /* special operand */
928 adj_opcode = op_code - lh.opcode_base;
929 address += (adj_opcode / lh.line_range)
930 * lh.minimum_instruction_length;
931 line += lh.line_base + (adj_opcode % lh.line_range);
932 /* append row to matrix using current values */
933 add_line_info (table, address, filename, line, column);
934 basic_block = 1;
939 return table;
943 /* If ADDR is within TABLE set the output parameters and return true,
944 otherwise return false. The output parameters, FILENAME_PTR and
945 LINENUMBER_PTR, are pointers to the objects to be filled in. */
947 static boolean
948 lookup_address_in_line_info_table (table,
949 addr,
950 filename_ptr,
951 linenumber_ptr)
952 struct line_info_table* table;
953 bfd_vma addr;
954 const char **filename_ptr;
955 unsigned int *linenumber_ptr;
957 struct line_info* each_line;
958 struct line_info* next_line;
960 for (next_line = 0, each_line = table->last_line;
961 each_line;
962 next_line = each_line, each_line = each_line->prev_line)
964 if (addr >= each_line->address
965 && (next_line == 0
966 || addr < next_line->address))
968 *filename_ptr = each_line->filename;
969 *linenumber_ptr = each_line->line;
970 return true;
974 return false;
980 /* Function table functions. */
982 struct funcinfo {
983 struct funcinfo *prev_func;
985 char* name;
986 bfd_vma low;
987 bfd_vma high;
991 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
993 static boolean
994 lookup_address_in_function_table (table,
995 addr,
996 functionname_ptr)
997 struct funcinfo* table;
998 bfd_vma addr;
999 const char **functionname_ptr;
1001 struct funcinfo* each_func;
1003 for (each_func = table;
1004 each_func;
1005 each_func = each_func->prev_func)
1007 if (addr >= each_func->low && addr < each_func->high)
1009 *functionname_ptr = each_func->name;
1010 return true;
1014 return false;
1020 /* DWARF2 Compilation unit functions. */
1023 /* Scan over each die in a comp. unit looking for functions to add
1024 to the function table. */
1026 static boolean
1027 scan_unit_for_functions (unit)
1028 struct comp_unit *unit;
1030 bfd *abfd = unit->abfd;
1031 char *info_ptr = unit->first_child_die_ptr;
1032 int nesting_level = 1;
1034 while (nesting_level)
1036 unsigned int abbrev_number, bytes_read, i;
1037 struct abbrev_info *abbrev;
1038 struct attribute attr;
1039 struct funcinfo *func;
1040 char* name = 0;
1042 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1043 info_ptr += bytes_read;
1045 if (! abbrev_number)
1047 nesting_level--;
1048 continue;
1051 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1052 if (! abbrev)
1054 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1055 abbrev_number);
1056 bfd_set_error (bfd_error_bad_value);
1057 return false;
1060 if (abbrev->tag == DW_TAG_subprogram)
1062 func = (struct funcinfo*) bfd_zalloc (abfd, sizeof (struct funcinfo));
1063 func->prev_func = unit->function_table;
1064 unit->function_table = func;
1066 else
1067 func = NULL;
1069 for (i = 0; i < abbrev->num_attrs; ++i)
1071 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1073 if (func)
1075 switch (attr.name)
1077 case DW_AT_name:
1079 name = DW_STRING (&attr);
1081 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1082 if (func->name == NULL)
1083 func->name = DW_STRING (&attr);
1084 break;
1086 case DW_AT_MIPS_linkage_name:
1087 func->name = DW_STRING (&attr);
1088 break;
1090 case DW_AT_low_pc:
1091 func->low = DW_ADDR (&attr);
1092 break;
1094 case DW_AT_high_pc:
1095 func->high = DW_ADDR (&attr);
1096 break;
1098 default:
1099 break;
1102 else
1104 switch (attr.name)
1106 case DW_AT_name:
1107 name = DW_STRING (&attr);
1108 break;
1110 default:
1111 break;
1116 if (abbrev->has_children)
1117 nesting_level++;
1120 return true;
1128 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This includes
1129 the compilation unit header that proceeds the DIE's, but does not
1130 include the length field that preceeds each compilation unit header.
1131 END_PTR points one past the end of this comp unit.
1133 This routine does not read the whole compilation unit; only enough
1134 to get to the line number information for the compilation unit. */
1136 static struct comp_unit *
1137 parse_comp_unit (abfd, info_ptr, end_ptr)
1138 bfd* abfd;
1139 char* info_ptr;
1140 char* end_ptr;
1142 struct comp_unit* unit;
1144 unsigned short version;
1145 unsigned int abbrev_offset;
1146 unsigned char addr_size;
1147 struct abbrev_info** abbrevs;
1149 unsigned int abbrev_number, bytes_read, i;
1150 struct abbrev_info *abbrev;
1151 struct attribute attr;
1153 version = read_2_bytes (abfd, info_ptr);
1154 info_ptr += 2;
1155 abbrev_offset = read_4_bytes (abfd, info_ptr);
1156 info_ptr += 4;
1157 addr_size = read_1_byte (abfd, info_ptr);
1158 info_ptr += 1;
1160 if (version != 2)
1162 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information."), version );
1163 bfd_set_error (bfd_error_bad_value);
1164 return 0;
1167 if (addr_size > sizeof (bfd_vma))
1169 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1170 addr_size,
1171 sizeof (bfd_vma));
1172 bfd_set_error (bfd_error_bad_value);
1173 return 0;
1176 if (addr_size != 4 && addr_size != 8)
1178 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '4' and '8'.", addr_size );
1179 bfd_set_error (bfd_error_bad_value);
1180 return 0;
1183 /* Read the abbrevs for this compilation unit into a table */
1184 abbrevs = read_abbrevs (abfd, abbrev_offset);
1185 if (! abbrevs)
1186 return 0;
1188 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1189 info_ptr += bytes_read;
1190 if (! abbrev_number)
1192 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %d."),
1193 abbrev_number);
1194 bfd_set_error (bfd_error_bad_value);
1195 return 0;
1198 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1199 if (! abbrev)
1201 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1202 abbrev_number);
1203 bfd_set_error (bfd_error_bad_value);
1204 return 0;
1207 unit = (struct comp_unit*) bfd_zalloc (abfd, sizeof (struct comp_unit));
1208 unit->abfd = abfd;
1209 unit->addr_size = addr_size;
1210 unit->abbrevs = abbrevs;
1211 unit->end_ptr = end_ptr;
1213 for (i = 0; i < abbrev->num_attrs; ++i)
1215 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1217 /* Store the data if it is of an attribute we want to keep in a
1218 partial symbol table. */
1219 switch (attr.name)
1221 case DW_AT_stmt_list:
1222 unit->stmtlist = 1;
1223 unit->line_offset = DW_UNSND (&attr);
1224 break;
1226 case DW_AT_name:
1227 unit->name = DW_STRING (&attr);
1228 break;
1230 case DW_AT_low_pc:
1231 unit->low = DW_ADDR (&attr);
1232 break;
1234 case DW_AT_high_pc:
1235 unit->high = DW_ADDR (&attr);
1236 break;
1238 case DW_AT_comp_dir:
1240 char* comp_dir = DW_STRING (&attr);
1241 if (comp_dir)
1243 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1244 directory, get rid of it. */
1245 char *cp = (char*) strchr (comp_dir, ':');
1247 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1248 comp_dir = cp + 1;
1250 unit->comp_dir = comp_dir;
1251 break;
1254 default:
1255 break;
1259 unit->first_child_die_ptr = info_ptr;
1260 return unit;
1267 /* Return true if UNIT contains the address given by ADDR. */
1269 static boolean
1270 comp_unit_contains_address (unit, addr)
1271 struct comp_unit* unit;
1272 bfd_vma addr;
1274 return ! unit->error
1275 && (addr >= unit->low && addr <= unit->high);
1279 /* If UNIT contains ADDR, set the output parameters to the values for
1280 the line containing ADDR. The output parameters, FILENAME_PTR,
1281 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1282 to be filled in.
1284 Return true of UNIT contains ADDR, and no errors were encountered;
1285 false otherwise. */
1287 static boolean
1288 comp_unit_find_nearest_line (unit, addr,
1289 filename_ptr, functionname_ptr, linenumber_ptr)
1290 struct comp_unit* unit;
1291 bfd_vma addr;
1292 const char **filename_ptr;
1293 const char **functionname_ptr;
1294 unsigned int *linenumber_ptr;
1296 boolean line_p;
1297 boolean func_p;
1299 if (unit->error)
1300 return false;
1302 if (! unit->line_table)
1304 if (! unit->stmtlist)
1306 unit->error = 1;
1307 return false;
1310 unit->line_table = decode_line_info (unit);
1312 if (! unit->line_table)
1314 unit->error = 1;
1315 return false;
1318 if (! scan_unit_for_functions (unit))
1320 unit->error = 1;
1321 return false;
1325 line_p = lookup_address_in_line_info_table (unit->line_table,
1326 addr,
1327 filename_ptr,
1328 linenumber_ptr);
1329 func_p = lookup_address_in_function_table (unit->function_table,
1330 addr,
1331 functionname_ptr);
1332 return line_p || func_p;
1335 /* The DWARF2 version of find_nearest line.
1336 Return true if the line is found without error. */
1338 boolean
1339 _bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
1340 filename_ptr, functionname_ptr, linenumber_ptr)
1341 bfd *abfd;
1342 asection *section;
1343 asymbol **symbols;
1344 bfd_vma offset;
1345 const char **filename_ptr;
1346 const char **functionname_ptr;
1347 unsigned int *linenumber_ptr;
1349 /* Read each compilation unit from the section .debug_info, and check
1350 to see if it contains the address we are searching for. If yes,
1351 lookup the address, and return the line number info. If no, go
1352 on to the next compilation unit.
1354 We keep a list of all the previously read compilation units, and
1355 a pointer to the next un-read compilation unit. Check the
1356 previously read units before reading more.
1359 struct dwarf2_debug *stash = elf_tdata (abfd)->dwarf2_find_line_info;
1361 /* What address are we looking for? */
1362 bfd_vma addr = offset + section->vma;
1364 struct comp_unit* each;
1366 *filename_ptr = NULL;
1367 *functionname_ptr = NULL;
1368 *linenumber_ptr = 0;
1370 if (! stash)
1372 asection *msec;
1373 unsigned long size;
1375 stash = elf_tdata (abfd)->dwarf2_find_line_info =
1376 (struct dwarf2_debug*) bfd_zalloc (abfd, sizeof (struct dwarf2_debug));
1378 if (! stash)
1379 return false;
1381 msec = bfd_get_section_by_name (abfd, ".debug_info");
1382 if (! msec)
1384 /* No dwarf2 info. Note that at this point the stash
1385 has been allocated, but contains zeros, this lets
1386 future calls to this function fail quicker. */
1387 return false;
1390 size = bfd_get_section_size_before_reloc (msec);
1391 if (size == 0)
1392 return false;
1394 stash->info_ptr = (char *) bfd_alloc (abfd, size);
1396 if (! stash->info_ptr)
1397 return false;
1399 if (! bfd_get_section_contents (abfd, msec, stash->info_ptr, 0, size))
1401 stash->info_ptr = 0;
1402 return false;
1405 stash->info_ptr_end = stash->info_ptr + size;
1407 /* FIXME: There is a problem with the contents of the .debug_info section.
1408 The 'low' and 'high' addresses of the comp_units are computed by relocs
1409 against symbols in the .text segment. We need these addresses in
1410 order to determine the nearest line number, and so we have to resolve
1411 the relocs. There is a similar problem when the .debug_line section is
1412 processed as well.
1414 Unfortunately getting hold of the reloc information is hard... */
1417 /* A null info_ptr indicates that there is no dwarf2 info
1418 (or that an error occured while setting up the stash). */
1420 if (! stash->info_ptr)
1421 return false;
1423 /* Check the previously read comp. units first. */
1425 for (each = stash->all_comp_units; each; each = each->next_unit)
1427 if (comp_unit_contains_address (each, addr))
1428 return comp_unit_find_nearest_line (each, addr,
1429 filename_ptr,
1430 functionname_ptr,
1431 linenumber_ptr);
1434 /* Read each remaining comp. units checking each as they are read. */
1435 while (stash->info_ptr < stash->info_ptr_end)
1437 struct comp_unit* each;
1438 unsigned int length;
1440 length = read_4_bytes (abfd, stash->info_ptr);
1441 stash->info_ptr += 4;
1443 if (length > 0)
1445 each = parse_comp_unit (abfd, stash->info_ptr,
1446 stash->info_ptr + length);
1447 stash->info_ptr += length;
1449 if (each)
1451 each->next_unit = stash->all_comp_units;
1452 stash->all_comp_units = each;
1454 if (comp_unit_contains_address (each, addr))
1455 return comp_unit_find_nearest_line (each, addr,
1456 filename_ptr,
1457 functionname_ptr,
1458 linenumber_ptr);
1463 return false;
1466 /* end of file */