Correct previous misapplied patch.
[binutils.git] / bfd / dwarf2.c
blob92397a2e119e2dc75b2c84c108cb16fa52455aaf
1 /* DWARF 2 support.
2 Copyright 1994, 95, 96, 97, 98, 99, 2000 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;
106 /* Length of the loaded .debug_line section. */
107 unsigned long dwarf_line_size;
110 struct arange {
111 struct arange *next;
112 bfd_vma low;
113 bfd_vma high;
117 /* A minimal decoding of DWARF2 compilation units. We only decode
118 what's needed to get to the line number information. */
120 struct comp_unit {
122 /* Chain the previously read compilation units. */
123 struct comp_unit* next_unit;
125 /* Keep the bdf convenient (for memory allocation). */
126 bfd* abfd;
128 /* The lowest and higest addresses contained in this compilation
129 unit as specified in the compilation unit header. */
130 struct arange arange;
132 /* The DW_AT_name attribute (for error messages). */
133 char* name;
135 /* The abbrev hash table. */
136 struct abbrev_info** abbrevs;
138 /* Note that an error was found by comp_unit_find_nearest_line. */
139 int error;
141 /* The DW_AT_comp_dir attribute */
142 char* comp_dir;
144 /* True if there is a line number table associated with this comp. unit. */
145 int stmtlist;
147 /* The offset into .debug_line of the line number table. */
148 unsigned long line_offset;
150 /* Pointer to the first child die for the comp unit. */
151 char *first_child_die_ptr;
153 /* The end of the comp unit. */
154 char *end_ptr;
156 /* The decoded line number, NULL if not yet decoded. */
157 struct line_info_table* line_table;
159 /* A list of the functions found in this comp. unit. */
160 struct funcinfo* function_table;
162 /* Address size for this unit - from unit header */
163 unsigned char addr_size;
168 /* VERBATIM
169 The following function up to the END VERBATIM mark are
170 copied directly from dwarf2read.c. */
172 /* read dwarf information from a buffer */
174 static unsigned int
175 read_1_byte (abfd, buf)
176 bfd *abfd ATTRIBUTE_UNUSED;
177 char *buf;
179 return bfd_get_8 (abfd, (bfd_byte *) buf);
182 static int
183 read_1_signed_byte (abfd, buf)
184 bfd *abfd ATTRIBUTE_UNUSED;
185 char *buf;
187 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
190 static unsigned int
191 read_2_bytes (abfd, buf)
192 bfd *abfd;
193 char *buf;
195 return bfd_get_16 (abfd, (bfd_byte *) buf);
198 #if 0
200 /* This is not used. */
202 static int
203 read_2_signed_bytes (abfd, buf)
204 bfd *abfd;
205 char *buf;
207 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
210 #endif
212 static unsigned int
213 read_4_bytes (abfd, buf)
214 bfd *abfd;
215 char *buf;
217 return bfd_get_32 (abfd, (bfd_byte *) buf);
220 #if 0
222 /* This is not used. */
224 static int
225 read_4_signed_bytes (abfd, buf)
226 bfd *abfd;
227 char *buf;
229 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
232 #endif
234 static unsigned int
235 read_8_bytes (abfd, buf)
236 bfd *abfd;
237 char *buf;
239 return bfd_get_64 (abfd, (bfd_byte *) buf);
242 static char *
243 read_n_bytes (abfd, buf, size)
244 bfd *abfd ATTRIBUTE_UNUSED;
245 char *buf;
246 unsigned int size ATTRIBUTE_UNUSED;
248 /* If the size of a host char is 8 bits, we can return a pointer
249 to the buffer, otherwise we have to copy the data to a buffer
250 allocated on the temporary obstack. */
251 return buf;
254 static char *
255 read_string (abfd, buf, bytes_read_ptr)
256 bfd *abfd ATTRIBUTE_UNUSED;
257 char *buf;
258 unsigned int *bytes_read_ptr;
260 /* If the size of a host char is 8 bits, we can return a pointer
261 to the string, otherwise we have to copy the string to a buffer
262 allocated on the temporary obstack. */
263 if (*buf == '\0')
265 *bytes_read_ptr = 1;
266 return NULL;
268 *bytes_read_ptr = strlen (buf) + 1;
269 return buf;
272 static unsigned int
273 read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
274 bfd *abfd ATTRIBUTE_UNUSED;
275 char *buf;
276 unsigned int *bytes_read_ptr;
278 unsigned int result;
279 unsigned int num_read;
280 int shift;
281 unsigned char byte;
283 result = 0;
284 shift = 0;
285 num_read = 0;
289 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
290 buf ++;
291 num_read ++;
292 result |= ((byte & 0x7f) << shift);
293 shift += 7;
295 while (byte & 0x80);
297 * bytes_read_ptr = num_read;
299 return result;
302 static int
303 read_signed_leb128 (abfd, buf, bytes_read_ptr)
304 bfd *abfd ATTRIBUTE_UNUSED;
305 char *buf;
306 unsigned int * bytes_read_ptr;
308 int result;
309 int shift;
310 int num_read;
311 unsigned char byte;
313 result = 0;
314 shift = 0;
315 num_read = 0;
319 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
320 buf ++;
321 num_read ++;
322 result |= ((byte & 0x7f) << shift);
323 shift += 7;
325 while (byte & 0x80);
327 if ((shift < 32) && (byte & 0x40))
328 result |= -(1 << shift);
330 * bytes_read_ptr = num_read;
332 return result;
335 /* END VERBATIM */
337 static bfd_vma
338 read_address (unit, buf)
339 struct comp_unit* unit;
340 char *buf;
342 switch (unit->addr_size)
344 case 8:
345 return bfd_get_64 (unit->abfd, (bfd_byte *) buf);
346 case 4:
347 return bfd_get_32 (unit->abfd, (bfd_byte *) buf);
348 case 2:
349 return bfd_get_16 (unit->abfd, (bfd_byte *) buf);
350 default:
351 abort ();
359 /* This data structure holds the information of an abbrev. */
360 struct abbrev_info
362 unsigned int number; /* number identifying abbrev */
363 enum dwarf_tag tag; /* dwarf tag */
364 int has_children; /* boolean */
365 unsigned int num_attrs; /* number of attributes */
366 struct attr_abbrev *attrs; /* an array of attribute descriptions */
367 struct abbrev_info *next; /* next in chain */
370 struct attr_abbrev
372 enum dwarf_attribute name;
373 enum dwarf_form form;
376 #ifndef ABBREV_HASH_SIZE
377 #define ABBREV_HASH_SIZE 121
378 #endif
379 #ifndef ATTR_ALLOC_CHUNK
380 #define ATTR_ALLOC_CHUNK 4
381 #endif
383 /* Lookup an abbrev_info structure in the abbrev hash table. */
385 static struct abbrev_info *
386 lookup_abbrev (number,abbrevs)
387 unsigned int number;
388 struct abbrev_info **abbrevs;
390 unsigned int hash_number;
391 struct abbrev_info *abbrev;
393 hash_number = number % ABBREV_HASH_SIZE;
394 abbrev = abbrevs[hash_number];
396 while (abbrev)
398 if (abbrev->number == number)
399 return abbrev;
400 else
401 abbrev = abbrev->next;
403 return NULL;
406 /* In DWARF version 2, the description of the debugging information is
407 stored in a separate .debug_abbrev section. Before we read any
408 dies from a section we read in all abbreviations and install them
409 in a hash table. */
411 static struct abbrev_info**
412 read_abbrevs (abfd, offset)
413 bfd * abfd;
414 unsigned int offset;
416 struct abbrev_info **abbrevs;
417 char *abbrev_ptr;
418 struct abbrev_info *cur_abbrev;
419 unsigned int abbrev_number, bytes_read, abbrev_name;
420 unsigned int abbrev_form, hash_number;
421 struct dwarf2_debug *stash;
423 stash = elf_tdata(abfd)->dwarf2_find_line_info;
425 if (! stash->dwarf_abbrev_buffer)
427 asection *msec;
429 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
430 if (! msec)
432 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
433 bfd_set_error (bfd_error_bad_value);
434 return 0;
437 stash->dwarf_abbrev_size = msec->_raw_size;
438 stash->dwarf_abbrev_buffer = (char*) bfd_alloc (abfd, stash->dwarf_abbrev_size);
439 if (! stash->dwarf_abbrev_buffer)
440 return 0;
442 if (! bfd_get_section_contents (abfd, msec,
443 stash->dwarf_abbrev_buffer, 0,
444 stash->dwarf_abbrev_size))
445 return 0;
448 if (offset > stash->dwarf_abbrev_size)
450 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%u) bigger than abbrev size (%u)."),
451 offset, stash->dwarf_abbrev_size );
452 bfd_set_error (bfd_error_bad_value);
453 return 0;
456 abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, sizeof(struct abbrev_info*) * ABBREV_HASH_SIZE);
458 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
459 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
460 abbrev_ptr += bytes_read;
462 /* loop until we reach an abbrev number of 0 */
463 while (abbrev_number)
465 cur_abbrev = (struct abbrev_info*)bfd_zalloc (abfd, sizeof (struct abbrev_info));
467 /* read in abbrev header */
468 cur_abbrev->number = abbrev_number;
469 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
470 abbrev_ptr += bytes_read;
471 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
472 abbrev_ptr += 1;
474 /* now read in declarations */
475 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
476 abbrev_ptr += bytes_read;
477 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
478 abbrev_ptr += bytes_read;
479 while (abbrev_name)
481 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
483 cur_abbrev->attrs = (struct attr_abbrev *)
484 bfd_realloc (cur_abbrev->attrs,
485 (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)
486 * sizeof (struct attr_abbrev));
487 if (! cur_abbrev->attrs)
488 return 0;
490 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
491 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
492 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
493 abbrev_ptr += bytes_read;
494 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
495 abbrev_ptr += bytes_read;
498 hash_number = abbrev_number % ABBREV_HASH_SIZE;
499 cur_abbrev->next = abbrevs[hash_number];
500 abbrevs[hash_number] = cur_abbrev;
502 /* Get next abbreviation.
503 Under Irix6 the abbreviations for a compilation unit are not
504 always properly terminated with an abbrev number of 0.
505 Exit loop if we encounter an abbreviation which we have
506 already read (which means we are about to read the abbreviations
507 for the next compile unit) or if the end of the abbreviation
508 table is reached. */
509 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
510 >= stash->dwarf_abbrev_size)
511 break;
512 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
513 abbrev_ptr += bytes_read;
514 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
515 break;
518 return abbrevs;
521 /* Read an attribute described by an abbreviated attribute. */
523 static char *
524 read_attribute (attr, abbrev, unit, info_ptr)
525 struct attribute *attr;
526 struct attr_abbrev *abbrev;
527 struct comp_unit *unit;
528 char *info_ptr;
530 bfd *abfd = unit->abfd;
531 unsigned int bytes_read;
532 struct dwarf_block *blk;
534 attr->name = abbrev->name;
535 attr->form = abbrev->form;
536 switch (abbrev->form)
538 case DW_FORM_addr:
539 case DW_FORM_ref_addr:
540 DW_ADDR (attr) = read_address (unit, info_ptr);
541 info_ptr += unit->addr_size;
542 break;
543 case DW_FORM_block2:
544 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
545 blk->size = read_2_bytes (abfd, info_ptr);
546 info_ptr += 2;
547 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
548 info_ptr += blk->size;
549 DW_BLOCK (attr) = blk;
550 break;
551 case DW_FORM_block4:
552 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
553 blk->size = read_4_bytes (abfd, info_ptr);
554 info_ptr += 4;
555 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
556 info_ptr += blk->size;
557 DW_BLOCK (attr) = blk;
558 break;
559 case DW_FORM_data2:
560 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
561 info_ptr += 2;
562 break;
563 case DW_FORM_data4:
564 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
565 info_ptr += 4;
566 break;
567 case DW_FORM_data8:
568 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
569 info_ptr += 8;
570 break;
571 case DW_FORM_string:
572 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
573 info_ptr += bytes_read;
574 break;
575 case DW_FORM_block:
576 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
577 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
578 info_ptr += bytes_read;
579 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
580 info_ptr += blk->size;
581 DW_BLOCK (attr) = blk;
582 break;
583 case DW_FORM_block1:
584 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
585 blk->size = read_1_byte (abfd, info_ptr);
586 info_ptr += 1;
587 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
588 info_ptr += blk->size;
589 DW_BLOCK (attr) = blk;
590 break;
591 case DW_FORM_data1:
592 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
593 info_ptr += 1;
594 break;
595 case DW_FORM_flag:
596 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
597 info_ptr += 1;
598 break;
599 case DW_FORM_sdata:
600 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
601 info_ptr += bytes_read;
602 break;
603 case DW_FORM_udata:
604 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
605 info_ptr += bytes_read;
606 break;
607 case DW_FORM_ref1:
608 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
609 info_ptr += 1;
610 break;
611 case DW_FORM_ref2:
612 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
613 info_ptr += 2;
614 break;
615 case DW_FORM_ref4:
616 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
617 info_ptr += 4;
618 break;
619 case DW_FORM_ref8:
620 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
621 info_ptr += 8;
622 break;
623 case DW_FORM_ref_udata:
624 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
625 info_ptr += bytes_read;
626 break;
627 case DW_FORM_strp:
628 case DW_FORM_indirect:
629 default:
630 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %d."),
631 abbrev->form);
632 bfd_set_error (bfd_error_bad_value);
634 return info_ptr;
638 /* Source line information table routines. */
640 #define FILE_ALLOC_CHUNK 5
641 #define DIR_ALLOC_CHUNK 5
643 struct line_info {
644 struct line_info* prev_line;
646 bfd_vma address;
647 char* filename;
648 unsigned int line;
649 unsigned int column;
650 int end_sequence; /* end of (sequential) code sequence */
653 struct fileinfo {
654 char *name;
655 unsigned int dir;
656 unsigned int time;
657 unsigned int size;
660 struct line_info_table {
661 bfd* abfd;
663 unsigned int num_files;
664 unsigned int num_dirs;
666 char* comp_dir;
667 char** dirs;
668 struct fileinfo* files;
669 struct line_info* last_line;
672 static void
673 add_line_info (table, address, filename, line, column, end_sequence)
674 struct line_info_table* table;
675 bfd_vma address;
676 char* filename;
677 unsigned int line;
678 unsigned int column;
679 int end_sequence;
681 struct line_info* info = (struct line_info*)
682 bfd_alloc (table->abfd, sizeof (struct line_info));
684 info->prev_line = table->last_line;
685 table->last_line = info;
687 info->address = address;
688 info->filename = filename;
689 info->line = line;
690 info->column = column;
691 info->end_sequence = end_sequence;
694 static char*
695 concat_filename (table, file)
696 struct line_info_table* table;
697 unsigned int file;
699 char* filename;
701 if (file - 1 >= table->num_files)
703 (*_bfd_error_handler) (_("Dwarf Error: mangled line number "
704 "section (bad file number)."));
705 return "<unknown>";
708 filename = table->files[file - 1].name;
709 if (*filename == '/')
710 return filename;
712 else
714 char* dirname = (table->files[file - 1].dir
715 ? table->dirs[table->files[file - 1].dir - 1]
716 : table->comp_dir);
717 return (char*) concat (dirname, "/", filename, NULL);
721 static void
722 arange_add (unit, low_pc, high_pc)
723 struct comp_unit *unit;
724 bfd_vma low_pc;
725 bfd_vma high_pc;
727 struct arange *arange;
729 /* first see if we can cheaply extend an existing range: */
730 arange = &unit->arange;
733 if (low_pc == arange->high)
735 arange->high = high_pc;
736 return;
738 if (high_pc == arange->low)
740 arange->low = low_pc;
741 return;
743 arange = arange->next;
745 while (arange);
747 if (unit->arange.high == 0)
749 /* this is the first address range: store it in unit->arange: */
750 unit->arange.next = 0;
751 unit->arange.low = low_pc;
752 unit->arange.high = high_pc;
753 return;
756 /* need to allocate a new arange and insert it into the arange list: */
757 arange = bfd_zalloc (unit->abfd, sizeof (*arange));
758 arange->low = low_pc;
759 arange->high = high_pc;
761 arange->next = unit->arange.next;
762 unit->arange.next = arange;
765 /* Decode the line number information for UNIT. */
767 static struct line_info_table*
768 decode_line_info (unit)
769 struct comp_unit *unit;
771 bfd *abfd = unit->abfd;
773 struct dwarf2_debug *stash;
775 struct line_info_table* table;
777 char *line_ptr;
778 char *line_end;
779 struct line_head lh;
780 unsigned int i, bytes_read;
781 char *cur_file, *cur_dir;
782 unsigned char op_code, extended_op, adj_opcode;
784 stash = elf_tdata (abfd)->dwarf2_find_line_info;
786 if (! stash->dwarf_line_buffer)
788 asection *msec;
790 msec = bfd_get_section_by_name (abfd, ".debug_line");
791 if (! msec)
793 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
794 bfd_set_error (bfd_error_bad_value);
795 return 0;
798 stash->dwarf_line_size = msec->_raw_size;
799 stash->dwarf_line_buffer = (char *) bfd_alloc (abfd, stash->dwarf_line_size);
800 if (! stash->dwarf_line_buffer)
801 return 0;
803 if (! bfd_get_section_contents (abfd, msec,
804 stash->dwarf_line_buffer, 0,
805 stash->dwarf_line_size))
806 return 0;
808 /* FIXME: We ought to apply the relocs against this section before
809 we process it.... */
812 /* Since we are using un-relocated data, it is possible to get a bad value
813 for the line_offset. Validate it here so that we won't get a segfault
814 below. */
815 if (unit->line_offset >= stash->dwarf_line_size)
817 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%u) bigger than line size (%u)."),
818 unit->line_offset, stash->dwarf_line_size);
819 bfd_set_error (bfd_error_bad_value);
820 return 0;
823 table = (struct line_info_table*) bfd_alloc (abfd,
824 sizeof (struct line_info_table));
825 table->abfd = abfd;
826 table->comp_dir = unit->comp_dir;
828 table->num_files = 0;
829 table->files = NULL;
831 table->num_dirs = 0;
832 table->dirs = NULL;
834 table->files = NULL;
835 table->last_line = NULL;
837 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
839 /* read in the prologue */
840 lh.total_length = read_4_bytes (abfd, line_ptr);
841 line_ptr += 4;
842 line_end = line_ptr + lh.total_length;
843 lh.version = read_2_bytes (abfd, line_ptr);
844 line_ptr += 2;
845 lh.prologue_length = read_4_bytes (abfd, line_ptr);
846 line_ptr += 4;
847 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
848 line_ptr += 1;
849 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
850 line_ptr += 1;
851 lh.line_base = read_1_signed_byte (abfd, line_ptr);
852 line_ptr += 1;
853 lh.line_range = read_1_byte (abfd, line_ptr);
854 line_ptr += 1;
855 lh.opcode_base = read_1_byte (abfd, line_ptr);
856 line_ptr += 1;
857 lh.standard_opcode_lengths = (unsigned char *)
858 bfd_alloc (abfd, lh.opcode_base * sizeof (unsigned char));
860 lh.standard_opcode_lengths[0] = 1;
861 for (i = 1; i < lh.opcode_base; ++i)
863 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
864 line_ptr += 1;
867 /* Read directory table */
868 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
870 line_ptr += bytes_read;
871 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
873 table->dirs = (char **)
874 bfd_realloc (table->dirs,
875 (table->num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *));
876 if (! table->dirs)
877 return 0;
879 table->dirs[table->num_dirs++] = cur_dir;
881 line_ptr += bytes_read;
883 /* Read file name table */
884 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
886 line_ptr += bytes_read;
887 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
889 table->files = (struct fileinfo *)
890 bfd_realloc (table->files,
891 (table->num_files + FILE_ALLOC_CHUNK)
892 * sizeof (struct fileinfo));
893 if (! table->files)
894 return 0;
896 table->files[table->num_files].name = cur_file;
897 table->files[table->num_files].dir =
898 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
899 line_ptr += bytes_read;
900 table->files[table->num_files].time =
901 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
902 line_ptr += bytes_read;
903 table->files[table->num_files].size =
904 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
905 line_ptr += bytes_read;
906 table->num_files++;
908 line_ptr += bytes_read;
910 /* Read the statement sequences until there's nothing left. */
911 while (line_ptr < line_end)
913 /* state machine registers */
914 bfd_vma address = 0;
915 char* filename = concat_filename (table, 1);
916 unsigned int line = 1;
917 unsigned int column = 0;
918 int is_stmt = lh.default_is_stmt;
919 int basic_block = 0;
920 int end_sequence = 0, need_low_pc = 1;
921 bfd_vma low_pc = 0;
923 /* Decode the table. */
924 while (! end_sequence)
926 op_code = read_1_byte (abfd, line_ptr);
927 line_ptr += 1;
928 switch (op_code)
930 case DW_LNS_extended_op:
931 line_ptr += 1; /* ignore length */
932 extended_op = read_1_byte (abfd, line_ptr);
933 line_ptr += 1;
934 switch (extended_op)
936 case DW_LNE_end_sequence:
937 end_sequence = 1;
938 add_line_info (table, address, filename, line, column,
939 end_sequence);
940 if (need_low_pc)
942 need_low_pc = 0;
943 low_pc = address;
945 arange_add (unit, low_pc, address);
946 break;
947 case DW_LNE_set_address:
948 address = read_address (unit, line_ptr);
949 line_ptr += unit->addr_size;
950 break;
951 case DW_LNE_define_file:
952 cur_file = read_string (abfd, line_ptr, &bytes_read);
953 line_ptr += bytes_read;
954 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
956 table->files = (struct fileinfo *)
957 bfd_realloc (table->files,
958 (table->num_files + FILE_ALLOC_CHUNK)
959 * sizeof (struct fileinfo));
960 if (! table->files)
961 return 0;
963 table->files[table->num_files].name = cur_file;
964 table->files[table->num_files].dir =
965 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
966 line_ptr += bytes_read;
967 table->files[table->num_files].time =
968 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
969 line_ptr += bytes_read;
970 table->files[table->num_files].size =
971 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
972 line_ptr += bytes_read;
973 table->num_files++;
974 break;
975 default:
976 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
977 bfd_set_error (bfd_error_bad_value);
978 return 0;
980 break;
981 case DW_LNS_copy:
982 add_line_info (table, address, filename, line, column, 0);
983 basic_block = 0;
984 if (need_low_pc)
986 need_low_pc = 0;
987 low_pc = address;
989 break;
990 case DW_LNS_advance_pc:
991 address += lh.minimum_instruction_length
992 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
993 line_ptr += bytes_read;
994 break;
995 case DW_LNS_advance_line:
996 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
997 line_ptr += bytes_read;
998 break;
999 case DW_LNS_set_file:
1001 unsigned int file;
1003 /* The file and directory tables are 0 based, the references
1004 are 1 based. */
1005 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1006 line_ptr += bytes_read;
1007 filename = concat_filename (table, file);
1008 break;
1010 case DW_LNS_set_column:
1011 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1012 line_ptr += bytes_read;
1013 break;
1014 case DW_LNS_negate_stmt:
1015 is_stmt = (!is_stmt);
1016 break;
1017 case DW_LNS_set_basic_block:
1018 basic_block = 1;
1019 break;
1020 case DW_LNS_const_add_pc:
1021 address += lh.minimum_instruction_length
1022 * ((255 - lh.opcode_base) / lh.line_range);
1023 break;
1024 case DW_LNS_fixed_advance_pc:
1025 address += read_2_bytes (abfd, line_ptr);
1026 line_ptr += 2;
1027 break;
1028 default: /* special operand */
1029 adj_opcode = op_code - lh.opcode_base;
1030 address += (adj_opcode / lh.line_range)
1031 * lh.minimum_instruction_length;
1032 line += lh.line_base + (adj_opcode % lh.line_range);
1033 /* append row to matrix using current values */
1034 add_line_info (table, address, filename, line, column, 0);
1035 basic_block = 1;
1036 if (need_low_pc)
1038 need_low_pc = 0;
1039 low_pc = address;
1045 return table;
1049 /* If ADDR is within TABLE set the output parameters and return true,
1050 otherwise return false. The output parameters, FILENAME_PTR and
1051 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1053 static boolean
1054 lookup_address_in_line_info_table (table,
1055 addr,
1056 filename_ptr,
1057 linenumber_ptr)
1058 struct line_info_table* table;
1059 bfd_vma addr;
1060 const char **filename_ptr;
1061 unsigned int *linenumber_ptr;
1063 struct line_info* next_line = table->last_line;
1064 struct line_info* each_line;
1066 if (!next_line)
1067 return false;
1069 each_line = next_line->prev_line;
1071 while (each_line && next_line)
1073 if (!each_line->end_sequence
1074 && addr >= each_line->address && addr < next_line->address)
1076 *filename_ptr = each_line->filename;
1077 *linenumber_ptr = each_line->line;
1078 return true;
1080 next_line = each_line;
1081 each_line = each_line->prev_line;
1084 return false;
1090 /* Function table functions. */
1092 struct funcinfo {
1093 struct funcinfo *prev_func;
1095 char* name;
1096 bfd_vma low;
1097 bfd_vma high;
1101 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
1103 static boolean
1104 lookup_address_in_function_table (table,
1105 addr,
1106 functionname_ptr)
1107 struct funcinfo* table;
1108 bfd_vma addr;
1109 const char **functionname_ptr;
1111 struct funcinfo* each_func;
1113 for (each_func = table;
1114 each_func;
1115 each_func = each_func->prev_func)
1117 if (addr >= each_func->low && addr < each_func->high)
1119 *functionname_ptr = each_func->name;
1120 return true;
1124 return false;
1130 /* DWARF2 Compilation unit functions. */
1133 /* Scan over each die in a comp. unit looking for functions to add
1134 to the function table. */
1136 static boolean
1137 scan_unit_for_functions (unit)
1138 struct comp_unit *unit;
1140 bfd *abfd = unit->abfd;
1141 char *info_ptr = unit->first_child_die_ptr;
1142 int nesting_level = 1;
1144 while (nesting_level)
1146 unsigned int abbrev_number, bytes_read, i;
1147 struct abbrev_info *abbrev;
1148 struct attribute attr;
1149 struct funcinfo *func;
1150 char* name = 0;
1152 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1153 info_ptr += bytes_read;
1155 if (! abbrev_number)
1157 nesting_level--;
1158 continue;
1161 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1162 if (! abbrev)
1164 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1165 abbrev_number);
1166 bfd_set_error (bfd_error_bad_value);
1167 return false;
1170 if (abbrev->tag == DW_TAG_subprogram)
1172 func = (struct funcinfo*) bfd_zalloc (abfd, sizeof (struct funcinfo));
1173 func->prev_func = unit->function_table;
1174 unit->function_table = func;
1176 else
1177 func = NULL;
1179 for (i = 0; i < abbrev->num_attrs; ++i)
1181 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1183 if (func)
1185 switch (attr.name)
1187 case DW_AT_name:
1189 name = DW_STRING (&attr);
1191 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1192 if (func->name == NULL)
1193 func->name = DW_STRING (&attr);
1194 break;
1196 case DW_AT_MIPS_linkage_name:
1197 func->name = DW_STRING (&attr);
1198 break;
1200 case DW_AT_low_pc:
1201 func->low = DW_ADDR (&attr);
1202 break;
1204 case DW_AT_high_pc:
1205 func->high = DW_ADDR (&attr);
1206 break;
1208 default:
1209 break;
1212 else
1214 switch (attr.name)
1216 case DW_AT_name:
1217 name = DW_STRING (&attr);
1218 break;
1220 default:
1221 break;
1226 if (abbrev->has_children)
1227 nesting_level++;
1230 return true;
1238 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1239 includes the compilation unit header that proceeds the DIE's, but
1240 does not include the length field that preceeds each compilation
1241 unit header. END_PTR points one past the end of this comp unit.
1242 If ABBREV_LENGTH is 0, then the length of the abbreviation offset
1243 is assumed to be four bytes. Otherwise, it it is the size given.
1245 This routine does not read the whole compilation unit; only enough
1246 to get to the line number information for the compilation unit. */
1248 static struct comp_unit *
1249 parse_comp_unit (abfd, info_ptr, end_ptr, abbrev_length)
1250 bfd* abfd;
1251 char* info_ptr;
1252 char* end_ptr;
1253 unsigned int abbrev_length;
1255 struct comp_unit* unit;
1257 unsigned short version;
1258 unsigned int abbrev_offset = 0;
1259 unsigned char addr_size;
1260 struct abbrev_info** abbrevs;
1262 unsigned int abbrev_number, bytes_read, i;
1263 struct abbrev_info *abbrev;
1264 struct attribute attr;
1266 version = read_2_bytes (abfd, info_ptr);
1267 info_ptr += 2;
1268 BFD_ASSERT (abbrev_length == 0
1269 || abbrev_length == 4
1270 || abbrev_length == 8);
1271 if (abbrev_length == 0 || abbrev_length == 4)
1272 abbrev_offset = read_4_bytes (abfd, info_ptr);
1273 else if (abbrev_length == 8)
1274 abbrev_offset = read_8_bytes (abfd, info_ptr);
1275 info_ptr += abbrev_length;
1276 addr_size = read_1_byte (abfd, info_ptr);
1277 info_ptr += 1;
1279 if (version != 2)
1281 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information."), version );
1282 bfd_set_error (bfd_error_bad_value);
1283 return 0;
1286 if (addr_size > sizeof (bfd_vma))
1288 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1289 addr_size,
1290 sizeof (bfd_vma));
1291 bfd_set_error (bfd_error_bad_value);
1292 return 0;
1295 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1297 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size );
1298 bfd_set_error (bfd_error_bad_value);
1299 return 0;
1302 /* Read the abbrevs for this compilation unit into a table */
1303 abbrevs = read_abbrevs (abfd, abbrev_offset);
1304 if (! abbrevs)
1305 return 0;
1307 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1308 info_ptr += bytes_read;
1309 if (! abbrev_number)
1311 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %d."),
1312 abbrev_number);
1313 bfd_set_error (bfd_error_bad_value);
1314 return 0;
1317 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1318 if (! abbrev)
1320 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1321 abbrev_number);
1322 bfd_set_error (bfd_error_bad_value);
1323 return 0;
1326 unit = (struct comp_unit*) bfd_zalloc (abfd, sizeof (struct comp_unit));
1327 unit->abfd = abfd;
1328 unit->addr_size = addr_size;
1329 unit->abbrevs = abbrevs;
1330 unit->end_ptr = end_ptr;
1332 for (i = 0; i < abbrev->num_attrs; ++i)
1334 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1336 /* Store the data if it is of an attribute we want to keep in a
1337 partial symbol table. */
1338 switch (attr.name)
1340 case DW_AT_stmt_list:
1341 unit->stmtlist = 1;
1342 unit->line_offset = DW_UNSND (&attr);
1343 break;
1345 case DW_AT_name:
1346 unit->name = DW_STRING (&attr);
1347 break;
1349 case DW_AT_low_pc:
1350 unit->arange.low = DW_ADDR (&attr);
1351 break;
1353 case DW_AT_high_pc:
1354 unit->arange.high = DW_ADDR (&attr);
1355 break;
1357 case DW_AT_comp_dir:
1359 char* comp_dir = DW_STRING (&attr);
1360 if (comp_dir)
1362 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1363 directory, get rid of it. */
1364 char *cp = (char*) strchr (comp_dir, ':');
1366 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1367 comp_dir = cp + 1;
1369 unit->comp_dir = comp_dir;
1370 break;
1373 default:
1374 break;
1378 unit->first_child_die_ptr = info_ptr;
1379 return unit;
1386 /* Return true if UNIT contains the address given by ADDR. */
1388 static boolean
1389 comp_unit_contains_address (unit, addr)
1390 struct comp_unit* unit;
1391 bfd_vma addr;
1393 struct arange *arange;
1395 if (unit->error)
1396 return 0;
1398 arange = &unit->arange;
1401 if (addr >= arange->low && addr < arange->high)
1402 return 1;
1403 arange = arange->next;
1405 while (arange);
1406 return 0;
1410 /* If UNIT contains ADDR, set the output parameters to the values for
1411 the line containing ADDR. The output parameters, FILENAME_PTR,
1412 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1413 to be filled in.
1415 Return true of UNIT contains ADDR, and no errors were encountered;
1416 false otherwise. */
1418 static boolean
1419 comp_unit_find_nearest_line (unit, addr,
1420 filename_ptr, functionname_ptr, linenumber_ptr)
1421 struct comp_unit* unit;
1422 bfd_vma addr;
1423 const char **filename_ptr;
1424 const char **functionname_ptr;
1425 unsigned int *linenumber_ptr;
1427 boolean line_p;
1428 boolean func_p;
1430 if (unit->error)
1431 return false;
1433 if (! unit->line_table)
1435 if (! unit->stmtlist)
1437 unit->error = 1;
1438 return false;
1441 unit->line_table = decode_line_info (unit);
1443 if (! unit->line_table)
1445 unit->error = 1;
1446 return false;
1449 if (! scan_unit_for_functions (unit))
1451 unit->error = 1;
1452 return false;
1456 line_p = lookup_address_in_line_info_table (unit->line_table,
1457 addr,
1458 filename_ptr,
1459 linenumber_ptr);
1460 func_p = lookup_address_in_function_table (unit->function_table,
1461 addr,
1462 functionname_ptr);
1463 return line_p || func_p;
1466 /* The DWARF2 version of find_nearest line. Return true if the line
1467 is found without error. ADDR_SIZE is the number of bytes in the
1468 initial .debug_info length field and in the abbreviation offset.
1469 You may use zero to indicate that the default value should be
1470 used. */
1472 boolean
1473 _bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
1474 filename_ptr, functionname_ptr,
1475 linenumber_ptr,
1476 addr_size)
1477 bfd *abfd;
1478 asection *section;
1479 asymbol **symbols ATTRIBUTE_UNUSED;
1480 bfd_vma offset;
1481 const char **filename_ptr;
1482 const char **functionname_ptr;
1483 unsigned int *linenumber_ptr;
1484 unsigned int addr_size;
1486 /* Read each compilation unit from the section .debug_info, and check
1487 to see if it contains the address we are searching for. If yes,
1488 lookup the address, and return the line number info. If no, go
1489 on to the next compilation unit.
1491 We keep a list of all the previously read compilation units, and
1492 a pointer to the next un-read compilation unit. Check the
1493 previously read units before reading more.
1496 struct dwarf2_debug *stash = elf_tdata (abfd)->dwarf2_find_line_info;
1498 /* What address are we looking for? */
1499 bfd_vma addr = offset + section->vma;
1501 struct comp_unit* each;
1503 *filename_ptr = NULL;
1504 *functionname_ptr = NULL;
1505 *linenumber_ptr = 0;
1507 /* The DWARF2 spec says that the initial length field, and the
1508 offset of the abbreviation table, should both be 4-byte values.
1509 However, some compilers do things differently. */
1510 if (addr_size == 0)
1511 addr_size = 4;
1512 BFD_ASSERT (addr_size == 4 || addr_size == 8);
1514 if (! stash)
1516 asection *msec;
1517 unsigned long size;
1519 stash = elf_tdata (abfd)->dwarf2_find_line_info =
1520 (struct dwarf2_debug*) bfd_zalloc (abfd, sizeof (struct dwarf2_debug));
1522 if (! stash)
1523 return false;
1525 msec = bfd_get_section_by_name (abfd, ".debug_info");
1526 if (! msec)
1528 /* No dwarf2 info. Note that at this point the stash
1529 has been allocated, but contains zeros, this lets
1530 future calls to this function fail quicker. */
1531 return false;
1534 size = msec->_raw_size;
1535 if (size == 0)
1536 return false;
1538 stash->info_ptr = (char *) bfd_alloc (abfd, size);
1540 if (! stash->info_ptr)
1541 return false;
1543 if (! bfd_get_section_contents (abfd, msec, stash->info_ptr, 0, size))
1545 stash->info_ptr = 0;
1546 return false;
1549 stash->info_ptr_end = stash->info_ptr + size;
1551 /* FIXME: There is a problem with the contents of the
1552 .debug_info section. The 'low' and 'high' addresses of the
1553 comp_units are computed by relocs against symbols in the
1554 .text segment. We need these addresses in order to determine
1555 the nearest line number, and so we have to resolve the
1556 relocs. There is a similar problem when the .debug_line
1557 section is processed as well (e.g., there may be relocs
1558 against the operand of the DW_LNE_set_address operator).
1560 Unfortunately getting hold of the reloc information is hard...
1562 For now, this means that disassembling object files (as
1563 opposed to fully executables) does not always work as well as
1564 we would like. */
1567 /* A null info_ptr indicates that there is no dwarf2 info
1568 (or that an error occured while setting up the stash). */
1570 if (! stash->info_ptr)
1571 return false;
1573 /* Check the previously read comp. units first. */
1575 for (each = stash->all_comp_units; each; each = each->next_unit)
1576 if (comp_unit_contains_address (each, addr))
1577 return comp_unit_find_nearest_line (each, addr, filename_ptr,
1578 functionname_ptr, linenumber_ptr);
1580 /* Read each remaining comp. units checking each as they are read. */
1581 while (stash->info_ptr < stash->info_ptr_end)
1583 struct comp_unit* each;
1584 bfd_vma length;
1585 boolean found;
1587 if (addr_size == 4)
1588 length = read_4_bytes (abfd, stash->info_ptr);
1589 else
1590 length = read_8_bytes (abfd, stash->info_ptr);
1591 stash->info_ptr += addr_size;
1593 if (length > 0)
1595 each = parse_comp_unit (abfd, stash->info_ptr,
1596 stash->info_ptr + length,
1597 addr_size);
1598 stash->info_ptr += length;
1600 if (each)
1602 each->next_unit = stash->all_comp_units;
1603 stash->all_comp_units = each;
1605 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1606 compilation units. If we don't have them (i.e.,
1607 unit->high == 0), we need to consult the line info
1608 table to see if a compilation unit contains the given
1609 address. */
1610 if (each->arange.high > 0)
1612 if (comp_unit_contains_address (each, addr))
1613 return comp_unit_find_nearest_line (each, addr,
1614 filename_ptr,
1615 functionname_ptr,
1616 linenumber_ptr);
1618 else
1620 found = comp_unit_find_nearest_line (each, addr,
1621 filename_ptr,
1622 functionname_ptr,
1623 linenumber_ptr);
1624 if (found)
1625 return true;
1631 return false;
1634 /* end of file */