(bfd_sym_parse_contained_variables_table_entry_v32): Avoid call to memcpy with
[binutils.git] / bfd / dwarf2.c
blobd5544d9caa40cad6d12fbbf3a1c91e19ee641571
1 /* DWARF 2 support.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004 Free Software Foundation, Inc.
5 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
6 (gavin@cygnus.com).
8 From the dwarf2read.c header:
9 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10 Inc. with support from Florida State University (under contract
11 with the Ada Joint Program Office), and Silicon Graphics, Inc.
12 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14 support in dwarfread.c
16 This file is part of BFD.
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or (at
21 your option) any later version.
23 This program is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32 #include "bfd.h"
33 #include "sysdep.h"
34 #include "libiberty.h"
35 #include "libbfd.h"
36 #include "elf-bfd.h"
37 #include "elf/dwarf2.h"
39 /* The data in the .debug_line statement prologue looks like this. */
41 struct line_head
43 bfd_vma total_length;
44 unsigned short version;
45 bfd_vma prologue_length;
46 unsigned char minimum_instruction_length;
47 unsigned char default_is_stmt;
48 int line_base;
49 unsigned char line_range;
50 unsigned char opcode_base;
51 unsigned char *standard_opcode_lengths;
54 /* Attributes have a name and a value. */
56 struct attribute
58 enum dwarf_attribute name;
59 enum dwarf_form form;
60 union
62 char *str;
63 struct dwarf_block *blk;
64 bfd_uint64_t val;
65 bfd_int64_t sval;
70 /* Blocks are a bunch of untyped bytes. */
71 struct dwarf_block
73 unsigned int size;
74 char *data;
77 struct dwarf2_debug
79 /* A list of all previously read comp_units. */
80 struct comp_unit* all_comp_units;
82 /* The next unread compilation unit within the .debug_info section.
83 Zero indicates that the .debug_info section has not been loaded
84 into a buffer yet. */
85 char* info_ptr;
87 /* Pointer to the end of the .debug_info section memory buffer. */
88 char* info_ptr_end;
90 /* Pointer to the section and address of the beginning of the
91 section. */
92 asection* sec;
93 char* sec_info_ptr;
95 /* Pointer to the symbol table. */
96 asymbol** syms;
98 /* Pointer to the .debug_abbrev section loaded into memory. */
99 char* dwarf_abbrev_buffer;
101 /* Length of the loaded .debug_abbrev section. */
102 unsigned long dwarf_abbrev_size;
104 /* Buffer for decode_line_info. */
105 char *dwarf_line_buffer;
107 /* Length of the loaded .debug_line section. */
108 unsigned long dwarf_line_size;
110 /* Pointer to the .debug_str section loaded into memory. */
111 char* dwarf_str_buffer;
113 /* Length of the loaded .debug_str section. */
114 unsigned long dwarf_str_size;
117 struct arange
119 struct arange *next;
120 bfd_vma low;
121 bfd_vma high;
124 /* A minimal decoding of DWARF2 compilation units. We only decode
125 what's needed to get to the line number information. */
127 struct comp_unit
129 /* Chain the previously read compilation units. */
130 struct comp_unit* next_unit;
132 /* Keep the bdf convenient (for memory allocation). */
133 bfd* abfd;
135 /* The lowest and higest addresses contained in this compilation
136 unit as specified in the compilation unit header. */
137 struct arange arange;
139 /* The DW_AT_name attribute (for error messages). */
140 char* name;
142 /* The abbrev hash table. */
143 struct abbrev_info** abbrevs;
145 /* Note that an error was found by comp_unit_find_nearest_line. */
146 int error;
148 /* The DW_AT_comp_dir attribute. */
149 char* comp_dir;
151 /* TRUE if there is a line number table associated with this comp. unit. */
152 int stmtlist;
154 /* Pointer to the current comp_unit so that we can find a given entry
155 by its reference. */
156 char *info_ptr_unit;
158 /* The offset into .debug_line of the line number table. */
159 unsigned long line_offset;
161 /* Pointer to the first child die for the comp unit. */
162 char *first_child_die_ptr;
164 /* The end of the comp unit. */
165 char *end_ptr;
167 /* The decoded line number, NULL if not yet decoded. */
168 struct line_info_table* line_table;
170 /* A list of the functions found in this comp. unit. */
171 struct funcinfo* function_table;
173 /* Pointer to dwarf2_debug structure. */
174 struct dwarf2_debug *stash;
176 /* Address size for this unit - from unit header. */
177 unsigned char addr_size;
179 /* Offset size for this unit - from unit header. */
180 unsigned char offset_size;
183 /* This data structure holds the information of an abbrev. */
184 struct abbrev_info
186 unsigned int number; /* Number identifying abbrev. */
187 enum dwarf_tag tag; /* DWARF tag. */
188 int has_children; /* Boolean. */
189 unsigned int num_attrs; /* Number of attributes. */
190 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
191 struct abbrev_info *next; /* Next in chain. */
194 struct attr_abbrev
196 enum dwarf_attribute name;
197 enum dwarf_form form;
200 #ifndef ABBREV_HASH_SIZE
201 #define ABBREV_HASH_SIZE 121
202 #endif
203 #ifndef ATTR_ALLOC_CHUNK
204 #define ATTR_ALLOC_CHUNK 4
205 #endif
207 /* VERBATIM
208 The following function up to the END VERBATIM mark are
209 copied directly from dwarf2read.c. */
211 /* Read dwarf information from a buffer. */
213 static unsigned int
214 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, char *buf)
216 return bfd_get_8 (abfd, buf);
219 static int
220 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, char *buf)
222 return bfd_get_signed_8 (abfd, buf);
225 static unsigned int
226 read_2_bytes (bfd *abfd, char *buf)
228 return bfd_get_16 (abfd, buf);
231 static unsigned int
232 read_4_bytes (bfd *abfd, char *buf)
234 return bfd_get_32 (abfd, buf);
237 static bfd_uint64_t
238 read_8_bytes (bfd *abfd, char *buf)
240 return bfd_get_64 (abfd, buf);
243 static char *
244 read_n_bytes (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 (bfd *abfd ATTRIBUTE_UNUSED,
256 char *buf,
257 unsigned int *bytes_read_ptr)
259 /* Return a pointer to the embedded string. */
260 if (*buf == '\0')
262 *bytes_read_ptr = 1;
263 return NULL;
266 *bytes_read_ptr = strlen (buf) + 1;
267 return buf;
270 static char *
271 read_indirect_string (struct comp_unit* unit,
272 char *buf,
273 unsigned int *bytes_read_ptr)
275 bfd_uint64_t offset;
276 struct dwarf2_debug *stash = unit->stash;
278 if (unit->offset_size == 4)
279 offset = read_4_bytes (unit->abfd, buf);
280 else
281 offset = read_8_bytes (unit->abfd, buf);
282 *bytes_read_ptr = unit->offset_size;
284 if (! stash->dwarf_str_buffer)
286 asection *msec;
287 bfd *abfd = unit->abfd;
288 bfd_size_type sz;
290 msec = bfd_get_section_by_name (abfd, ".debug_str");
291 if (! msec)
293 (*_bfd_error_handler)
294 (_("Dwarf Error: Can't find .debug_str section."));
295 bfd_set_error (bfd_error_bad_value);
296 return NULL;
299 sz = msec->rawsize ? msec->rawsize : msec->size;
300 stash->dwarf_str_size = sz;
301 stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
302 if (! stash->dwarf_abbrev_buffer)
303 return NULL;
305 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
306 0, sz))
307 return NULL;
310 if (offset >= stash->dwarf_str_size)
312 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
313 (unsigned long) offset, stash->dwarf_str_size);
314 bfd_set_error (bfd_error_bad_value);
315 return NULL;
318 buf = stash->dwarf_str_buffer + offset;
319 if (*buf == '\0')
320 return NULL;
321 return buf;
324 /* END VERBATIM */
326 static bfd_uint64_t
327 read_address (struct comp_unit *unit, char *buf)
329 switch (unit->addr_size)
331 case 8:
332 return bfd_get_64 (unit->abfd, buf);
333 case 4:
334 return bfd_get_32 (unit->abfd, buf);
335 case 2:
336 return bfd_get_16 (unit->abfd, buf);
337 default:
338 abort ();
342 /* Lookup an abbrev_info structure in the abbrev hash table. */
344 static struct abbrev_info *
345 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
347 unsigned int hash_number;
348 struct abbrev_info *abbrev;
350 hash_number = number % ABBREV_HASH_SIZE;
351 abbrev = abbrevs[hash_number];
353 while (abbrev)
355 if (abbrev->number == number)
356 return abbrev;
357 else
358 abbrev = abbrev->next;
361 return NULL;
364 /* In DWARF version 2, the description of the debugging information is
365 stored in a separate .debug_abbrev section. Before we read any
366 dies from a section we read in all abbreviations and install them
367 in a hash table. */
369 static struct abbrev_info**
370 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
372 struct abbrev_info **abbrevs;
373 char *abbrev_ptr;
374 struct abbrev_info *cur_abbrev;
375 unsigned int abbrev_number, bytes_read, abbrev_name;
376 unsigned int abbrev_form, hash_number;
377 bfd_size_type amt;
379 if (! stash->dwarf_abbrev_buffer)
381 asection *msec;
383 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
384 if (! msec)
386 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
387 bfd_set_error (bfd_error_bad_value);
388 return 0;
391 stash->dwarf_abbrev_size = msec->size;
392 stash->dwarf_abbrev_buffer
393 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
394 stash->syms);
395 if (! stash->dwarf_abbrev_buffer)
396 return 0;
399 if (offset >= stash->dwarf_abbrev_size)
401 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
402 (unsigned long) offset, stash->dwarf_abbrev_size);
403 bfd_set_error (bfd_error_bad_value);
404 return 0;
407 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
408 abbrevs = bfd_zalloc (abfd, amt);
410 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
411 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
412 abbrev_ptr += bytes_read;
414 /* Loop until we reach an abbrev number of 0. */
415 while (abbrev_number)
417 amt = sizeof (struct abbrev_info);
418 cur_abbrev = bfd_zalloc (abfd, amt);
420 /* Read in abbrev header. */
421 cur_abbrev->number = abbrev_number;
422 cur_abbrev->tag = (enum dwarf_tag)
423 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
424 abbrev_ptr += bytes_read;
425 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
426 abbrev_ptr += 1;
428 /* Now read in declarations. */
429 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
430 abbrev_ptr += bytes_read;
431 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
432 abbrev_ptr += bytes_read;
434 while (abbrev_name)
436 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
438 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
439 amt *= sizeof (struct attr_abbrev);
440 cur_abbrev->attrs = bfd_realloc (cur_abbrev->attrs, amt);
441 if (! cur_abbrev->attrs)
442 return 0;
445 cur_abbrev->attrs[cur_abbrev->num_attrs].name
446 = (enum dwarf_attribute) abbrev_name;
447 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
448 = (enum dwarf_form) abbrev_form;
449 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
450 abbrev_ptr += bytes_read;
451 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
452 abbrev_ptr += bytes_read;
455 hash_number = abbrev_number % ABBREV_HASH_SIZE;
456 cur_abbrev->next = abbrevs[hash_number];
457 abbrevs[hash_number] = cur_abbrev;
459 /* Get next abbreviation.
460 Under Irix6 the abbreviations for a compilation unit are not
461 always properly terminated with an abbrev number of 0.
462 Exit loop if we encounter an abbreviation which we have
463 already read (which means we are about to read the abbreviations
464 for the next compile unit) or if the end of the abbreviation
465 table is reached. */
466 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
467 >= stash->dwarf_abbrev_size)
468 break;
469 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
470 abbrev_ptr += bytes_read;
471 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
472 break;
475 return abbrevs;
478 /* Read an attribute value described by an attribute form. */
480 static char *
481 read_attribute_value (struct attribute *attr,
482 unsigned form,
483 struct comp_unit *unit,
484 char *info_ptr)
486 bfd *abfd = unit->abfd;
487 unsigned int bytes_read;
488 struct dwarf_block *blk;
489 bfd_size_type amt;
491 attr->form = (enum dwarf_form) form;
493 switch (form)
495 case DW_FORM_addr:
496 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
497 case DW_FORM_ref_addr:
498 attr->u.val = read_address (unit, info_ptr);
499 info_ptr += unit->addr_size;
500 break;
501 case DW_FORM_block2:
502 amt = sizeof (struct dwarf_block);
503 blk = bfd_alloc (abfd, amt);
504 blk->size = read_2_bytes (abfd, info_ptr);
505 info_ptr += 2;
506 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
507 info_ptr += blk->size;
508 attr->u.blk = blk;
509 break;
510 case DW_FORM_block4:
511 amt = sizeof (struct dwarf_block);
512 blk = bfd_alloc (abfd, amt);
513 blk->size = read_4_bytes (abfd, info_ptr);
514 info_ptr += 4;
515 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
516 info_ptr += blk->size;
517 attr->u.blk = blk;
518 break;
519 case DW_FORM_data2:
520 attr->u.val = read_2_bytes (abfd, info_ptr);
521 info_ptr += 2;
522 break;
523 case DW_FORM_data4:
524 attr->u.val = read_4_bytes (abfd, info_ptr);
525 info_ptr += 4;
526 break;
527 case DW_FORM_data8:
528 attr->u.val = read_8_bytes (abfd, info_ptr);
529 info_ptr += 8;
530 break;
531 case DW_FORM_string:
532 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
533 info_ptr += bytes_read;
534 break;
535 case DW_FORM_strp:
536 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
537 info_ptr += bytes_read;
538 break;
539 case DW_FORM_block:
540 amt = sizeof (struct dwarf_block);
541 blk = bfd_alloc (abfd, amt);
542 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
543 info_ptr += bytes_read;
544 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
545 info_ptr += blk->size;
546 attr->u.blk = blk;
547 break;
548 case DW_FORM_block1:
549 amt = sizeof (struct dwarf_block);
550 blk = bfd_alloc (abfd, amt);
551 blk->size = read_1_byte (abfd, info_ptr);
552 info_ptr += 1;
553 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
554 info_ptr += blk->size;
555 attr->u.blk = blk;
556 break;
557 case DW_FORM_data1:
558 attr->u.val = read_1_byte (abfd, info_ptr);
559 info_ptr += 1;
560 break;
561 case DW_FORM_flag:
562 attr->u.val = read_1_byte (abfd, info_ptr);
563 info_ptr += 1;
564 break;
565 case DW_FORM_sdata:
566 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
567 info_ptr += bytes_read;
568 break;
569 case DW_FORM_udata:
570 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
571 info_ptr += bytes_read;
572 break;
573 case DW_FORM_ref1:
574 attr->u.val = read_1_byte (abfd, info_ptr);
575 info_ptr += 1;
576 break;
577 case DW_FORM_ref2:
578 attr->u.val = read_2_bytes (abfd, info_ptr);
579 info_ptr += 2;
580 break;
581 case DW_FORM_ref4:
582 attr->u.val = read_4_bytes (abfd, info_ptr);
583 info_ptr += 4;
584 break;
585 case DW_FORM_ref8:
586 attr->u.val = read_8_bytes (abfd, info_ptr);
587 info_ptr += 8;
588 break;
589 case DW_FORM_ref_udata:
590 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
591 info_ptr += bytes_read;
592 break;
593 case DW_FORM_indirect:
594 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
595 info_ptr += bytes_read;
596 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
597 break;
598 default:
599 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
600 form);
601 bfd_set_error (bfd_error_bad_value);
603 return info_ptr;
606 /* Read an attribute described by an abbreviated attribute. */
608 static char *
609 read_attribute (struct attribute *attr,
610 struct attr_abbrev *abbrev,
611 struct comp_unit *unit,
612 char *info_ptr)
614 attr->name = abbrev->name;
615 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
616 return info_ptr;
619 /* Source line information table routines. */
621 #define FILE_ALLOC_CHUNK 5
622 #define DIR_ALLOC_CHUNK 5
624 struct line_info
626 struct line_info* prev_line;
627 bfd_vma address;
628 char* filename;
629 unsigned int line;
630 unsigned int column;
631 int end_sequence; /* End of (sequential) code sequence. */
634 struct fileinfo
636 char *name;
637 unsigned int dir;
638 unsigned int time;
639 unsigned int size;
642 struct line_info_table
644 bfd* abfd;
645 unsigned int num_files;
646 unsigned int num_dirs;
647 char* comp_dir;
648 char** dirs;
649 struct fileinfo* files;
650 struct line_info* last_line; /* largest VMA */
651 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
654 struct funcinfo
656 struct funcinfo *prev_func;
657 char* name;
658 bfd_vma low;
659 bfd_vma high;
662 /* Adds a new entry to the line_info list in the line_info_table, ensuring
663 that the list is sorted. Note that the line_info list is sorted from
664 highest to lowest VMA (with possible duplicates); that is,
665 line_info->prev_line always accesses an equal or smaller VMA. */
667 static void
668 add_line_info (struct line_info_table *table,
669 bfd_vma address,
670 char *filename,
671 unsigned int line,
672 unsigned int column,
673 int end_sequence)
675 bfd_size_type amt = sizeof (struct line_info);
676 struct line_info* info = bfd_alloc (table->abfd, amt);
678 /* Find the correct location for 'info'. Normally we will receive
679 new line_info data 1) in order and 2) with increasing VMAs.
680 However some compilers break the rules (cf. decode_line_info) and
681 so we include some heuristics for quickly finding the correct
682 location for 'info'. In particular, these heuristics optimize for
683 the common case in which the VMA sequence that we receive is a
684 list of locally sorted VMAs such as
685 p...z a...j (where a < j < p < z)
687 Note: table->lcl_head is used to head an *actual* or *possible*
688 sequence within the list (such as a...j) that is not directly
689 headed by table->last_line
691 Note: we may receive duplicate entries from 'decode_line_info'. */
693 while (1)
694 if (!table->last_line
695 || address >= table->last_line->address)
697 /* Normal case: add 'info' to the beginning of the list */
698 info->prev_line = table->last_line;
699 table->last_line = info;
701 /* lcl_head: initialize to head a *possible* sequence at the end. */
702 if (!table->lcl_head)
703 table->lcl_head = info;
704 break;
706 else if (!table->lcl_head->prev_line
707 && table->lcl_head->address > address)
709 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
710 list and 2) the head of 'info'. */
711 info->prev_line = NULL;
712 table->lcl_head->prev_line = info;
713 break;
715 else if (table->lcl_head->prev_line
716 && table->lcl_head->address > address
717 && address >= table->lcl_head->prev_line->address)
719 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
720 list and 2) the head of 'info'. */
721 info->prev_line = table->lcl_head->prev_line;
722 table->lcl_head->prev_line = info;
723 break;
725 else
727 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
728 heads for 'info'. Reset 'lcl_head' and repeat. */
729 struct line_info* li2 = table->last_line; /* always non-NULL */
730 struct line_info* li1 = li2->prev_line;
732 while (li1)
734 if (li2->address > address && address >= li1->address)
735 break;
737 li2 = li1; /* always non-NULL */
738 li1 = li1->prev_line;
740 table->lcl_head = li2;
743 /* Set member data of 'info'. */
744 info->address = address;
745 info->line = line;
746 info->column = column;
747 info->end_sequence = end_sequence;
749 if (filename && filename[0])
751 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
752 if (info->filename)
753 strcpy (info->filename, filename);
755 else
756 info->filename = NULL;
759 /* Extract a fully qualified filename from a line info table.
760 The returned string has been malloc'ed and it is the caller's
761 responsibility to free it. */
763 static char *
764 concat_filename (struct line_info_table *table, unsigned int file)
766 char* filename;
768 if (file - 1 >= table->num_files)
770 (*_bfd_error_handler)
771 (_("Dwarf Error: mangled line number section (bad file number)."));
772 return strdup ("<unknown>");
775 filename = table->files[file - 1].name;
777 if (! IS_ABSOLUTE_PATH (filename))
779 char* dirname = (table->files[file - 1].dir
780 ? table->dirs[table->files[file - 1].dir - 1]
781 : table->comp_dir);
783 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
784 The best we can do is return the filename part. */
785 if (dirname != NULL)
787 unsigned int len = strlen (dirname) + strlen (filename) + 2;
788 char * name;
790 name = bfd_malloc (len);
791 if (name)
792 sprintf (name, "%s/%s", dirname, filename);
793 return name;
797 return strdup (filename);
800 static void
801 arange_add (struct comp_unit *unit, bfd_vma low_pc, bfd_vma high_pc)
803 struct arange *arange;
805 /* First see if we can cheaply extend an existing range. */
806 arange = &unit->arange;
810 if (low_pc == arange->high)
812 arange->high = high_pc;
813 return;
815 if (high_pc == arange->low)
817 arange->low = low_pc;
818 return;
820 arange = arange->next;
822 while (arange);
824 if (unit->arange.high == 0)
826 /* This is the first address range: store it in unit->arange. */
827 unit->arange.next = 0;
828 unit->arange.low = low_pc;
829 unit->arange.high = high_pc;
830 return;
833 /* Need to allocate a new arange and insert it into the arange list. */
834 arange = bfd_zalloc (unit->abfd, sizeof (*arange));
835 arange->low = low_pc;
836 arange->high = high_pc;
838 arange->next = unit->arange.next;
839 unit->arange.next = arange;
842 /* Decode the line number information for UNIT. */
844 static struct line_info_table*
845 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
847 bfd *abfd = unit->abfd;
848 struct line_info_table* table;
849 char *line_ptr;
850 char *line_end;
851 struct line_head lh;
852 unsigned int i, bytes_read, offset_size;
853 char *cur_file, *cur_dir;
854 unsigned char op_code, extended_op, adj_opcode;
855 bfd_size_type amt;
857 if (! stash->dwarf_line_buffer)
859 asection *msec;
861 msec = bfd_get_section_by_name (abfd, ".debug_line");
862 if (! msec)
864 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
865 bfd_set_error (bfd_error_bad_value);
866 return 0;
869 stash->dwarf_line_size = msec->size;
870 stash->dwarf_line_buffer
871 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
872 stash->syms);
873 if (! stash->dwarf_line_buffer)
874 return 0;
877 /* It is possible to get a bad value for the line_offset. Validate
878 it here so that we won't get a segfault below. */
879 if (unit->line_offset >= stash->dwarf_line_size)
881 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
882 unit->line_offset, stash->dwarf_line_size);
883 bfd_set_error (bfd_error_bad_value);
884 return 0;
887 amt = sizeof (struct line_info_table);
888 table = bfd_alloc (abfd, amt);
889 table->abfd = abfd;
890 table->comp_dir = unit->comp_dir;
892 table->num_files = 0;
893 table->files = NULL;
895 table->num_dirs = 0;
896 table->dirs = NULL;
898 table->files = NULL;
899 table->last_line = NULL;
900 table->lcl_head = NULL;
902 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
904 /* Read in the prologue. */
905 lh.total_length = read_4_bytes (abfd, line_ptr);
906 line_ptr += 4;
907 offset_size = 4;
908 if (lh.total_length == 0xffffffff)
910 lh.total_length = read_8_bytes (abfd, line_ptr);
911 line_ptr += 8;
912 offset_size = 8;
914 else if (lh.total_length == 0 && unit->addr_size == 8)
916 /* Handle (non-standard) 64-bit DWARF2 formats. */
917 lh.total_length = read_4_bytes (abfd, line_ptr);
918 line_ptr += 4;
919 offset_size = 8;
921 line_end = line_ptr + lh.total_length;
922 lh.version = read_2_bytes (abfd, line_ptr);
923 line_ptr += 2;
924 if (offset_size == 4)
925 lh.prologue_length = read_4_bytes (abfd, line_ptr);
926 else
927 lh.prologue_length = read_8_bytes (abfd, line_ptr);
928 line_ptr += offset_size;
929 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
930 line_ptr += 1;
931 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
932 line_ptr += 1;
933 lh.line_base = read_1_signed_byte (abfd, line_ptr);
934 line_ptr += 1;
935 lh.line_range = read_1_byte (abfd, line_ptr);
936 line_ptr += 1;
937 lh.opcode_base = read_1_byte (abfd, line_ptr);
938 line_ptr += 1;
939 amt = lh.opcode_base * sizeof (unsigned char);
940 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
942 lh.standard_opcode_lengths[0] = 1;
944 for (i = 1; i < lh.opcode_base; ++i)
946 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
947 line_ptr += 1;
950 /* Read directory table. */
951 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
953 line_ptr += bytes_read;
955 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
957 amt = table->num_dirs + DIR_ALLOC_CHUNK;
958 amt *= sizeof (char *);
959 table->dirs = bfd_realloc (table->dirs, amt);
960 if (! table->dirs)
961 return 0;
964 table->dirs[table->num_dirs++] = cur_dir;
967 line_ptr += bytes_read;
969 /* Read file name table. */
970 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
972 line_ptr += bytes_read;
974 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
976 amt = table->num_files + FILE_ALLOC_CHUNK;
977 amt *= sizeof (struct fileinfo);
978 table->files = bfd_realloc (table->files, amt);
979 if (! table->files)
980 return 0;
983 table->files[table->num_files].name = cur_file;
984 table->files[table->num_files].dir =
985 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
986 line_ptr += bytes_read;
987 table->files[table->num_files].time =
988 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
989 line_ptr += bytes_read;
990 table->files[table->num_files].size =
991 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
992 line_ptr += bytes_read;
993 table->num_files++;
996 line_ptr += bytes_read;
998 /* Read the statement sequences until there's nothing left. */
999 while (line_ptr < line_end)
1001 /* State machine registers. */
1002 bfd_vma address = 0;
1003 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1004 unsigned int line = 1;
1005 unsigned int column = 0;
1006 int is_stmt = lh.default_is_stmt;
1007 int basic_block = 0;
1008 int end_sequence = 0;
1009 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1010 compilers generate address sequences that are wildly out of
1011 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1012 for ia64-Linux). Thus, to determine the low and high
1013 address, we must compare on every DW_LNS_copy, etc. */
1014 bfd_vma low_pc = 0;
1015 bfd_vma high_pc = 0;
1017 /* Decode the table. */
1018 while (! end_sequence)
1020 op_code = read_1_byte (abfd, line_ptr);
1021 line_ptr += 1;
1023 if (op_code >= lh.opcode_base)
1025 /* Special operand. */
1026 adj_opcode = op_code - lh.opcode_base;
1027 address += (adj_opcode / lh.line_range)
1028 * lh.minimum_instruction_length;
1029 line += lh.line_base + (adj_opcode % lh.line_range);
1030 /* Append row to matrix using current values. */
1031 add_line_info (table, address, filename, line, column, 0);
1032 basic_block = 1;
1033 if (low_pc == 0 || address < low_pc)
1034 low_pc = address;
1035 if (address > high_pc)
1036 high_pc = address;
1038 else switch (op_code)
1040 case DW_LNS_extended_op:
1041 /* Ignore length. */
1042 line_ptr += 1;
1043 extended_op = read_1_byte (abfd, line_ptr);
1044 line_ptr += 1;
1046 switch (extended_op)
1048 case DW_LNE_end_sequence:
1049 end_sequence = 1;
1050 add_line_info (table, address, filename, line, column,
1051 end_sequence);
1052 if (low_pc == 0 || address < low_pc)
1053 low_pc = address;
1054 if (address > high_pc)
1055 high_pc = address;
1056 arange_add (unit, low_pc, high_pc);
1057 break;
1058 case DW_LNE_set_address:
1059 address = read_address (unit, line_ptr);
1060 line_ptr += unit->addr_size;
1061 break;
1062 case DW_LNE_define_file:
1063 cur_file = read_string (abfd, line_ptr, &bytes_read);
1064 line_ptr += bytes_read;
1065 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1067 amt = table->num_files + FILE_ALLOC_CHUNK;
1068 amt *= sizeof (struct fileinfo);
1069 table->files = bfd_realloc (table->files, amt);
1070 if (! table->files)
1071 return 0;
1073 table->files[table->num_files].name = cur_file;
1074 table->files[table->num_files].dir =
1075 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1076 line_ptr += bytes_read;
1077 table->files[table->num_files].time =
1078 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1079 line_ptr += bytes_read;
1080 table->files[table->num_files].size =
1081 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1082 line_ptr += bytes_read;
1083 table->num_files++;
1084 break;
1085 default:
1086 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1087 bfd_set_error (bfd_error_bad_value);
1088 return 0;
1090 break;
1091 case DW_LNS_copy:
1092 add_line_info (table, address, filename, line, column, 0);
1093 basic_block = 0;
1094 if (low_pc == 0 || address < low_pc)
1095 low_pc = address;
1096 if (address > high_pc)
1097 high_pc = address;
1098 break;
1099 case DW_LNS_advance_pc:
1100 address += lh.minimum_instruction_length
1101 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1102 line_ptr += bytes_read;
1103 break;
1104 case DW_LNS_advance_line:
1105 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1106 line_ptr += bytes_read;
1107 break;
1108 case DW_LNS_set_file:
1110 unsigned int file;
1112 /* The file and directory tables are 0
1113 based, the references are 1 based. */
1114 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1115 line_ptr += bytes_read;
1116 if (filename)
1117 free (filename);
1118 filename = concat_filename (table, file);
1119 break;
1121 case DW_LNS_set_column:
1122 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1123 line_ptr += bytes_read;
1124 break;
1125 case DW_LNS_negate_stmt:
1126 is_stmt = (!is_stmt);
1127 break;
1128 case DW_LNS_set_basic_block:
1129 basic_block = 1;
1130 break;
1131 case DW_LNS_const_add_pc:
1132 address += lh.minimum_instruction_length
1133 * ((255 - lh.opcode_base) / lh.line_range);
1134 break;
1135 case DW_LNS_fixed_advance_pc:
1136 address += read_2_bytes (abfd, line_ptr);
1137 line_ptr += 2;
1138 break;
1139 default:
1141 int i;
1143 /* Unknown standard opcode, ignore it. */
1144 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1146 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1147 line_ptr += bytes_read;
1153 if (filename)
1154 free (filename);
1157 return table;
1160 /* If ADDR is within TABLE set the output parameters and return TRUE,
1161 otherwise return FALSE. The output parameters, FILENAME_PTR and
1162 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1164 static bfd_boolean
1165 lookup_address_in_line_info_table (struct line_info_table *table,
1166 bfd_vma addr,
1167 struct funcinfo *function,
1168 const char **filename_ptr,
1169 unsigned int *linenumber_ptr)
1171 /* Note: table->last_line should be a descendingly sorted list. */
1172 struct line_info* next_line = table->last_line;
1173 struct line_info* each_line = NULL;
1174 *filename_ptr = NULL;
1176 if (!next_line)
1177 return FALSE;
1179 each_line = next_line->prev_line;
1181 /* Check for large addresses */
1182 if (addr > next_line->address)
1183 each_line = NULL; /* ensure we skip over the normal case */
1185 /* Normal case: search the list; save */
1186 while (each_line && next_line)
1188 /* If we have an address match, save this info. This allows us
1189 to return as good as results as possible for strange debugging
1190 info. */
1191 bfd_boolean addr_match = FALSE;
1192 if (each_line->address <= addr && addr <= next_line->address)
1194 addr_match = TRUE;
1196 /* If this line appears to span functions, and addr is in the
1197 later function, return the first line of that function instead
1198 of the last line of the earlier one. This check is for GCC
1199 2.95, which emits the first line number for a function late. */
1200 if (function != NULL
1201 && each_line->address < function->low
1202 && next_line->address > function->low)
1204 *filename_ptr = next_line->filename;
1205 *linenumber_ptr = next_line->line;
1207 else
1209 *filename_ptr = each_line->filename;
1210 *linenumber_ptr = each_line->line;
1214 if (addr_match && !each_line->end_sequence)
1215 return TRUE; /* we have definitely found what we want */
1217 next_line = each_line;
1218 each_line = each_line->prev_line;
1221 /* At this point each_line is NULL but next_line is not. If we found
1222 a candidate end-of-sequence point in the loop above, we can return
1223 that (compatibility with a bug in the Intel compiler); otherwise,
1224 assuming that we found the containing function for this address in
1225 this compilation unit, return the first line we have a number for
1226 (compatibility with GCC 2.95). */
1227 if (*filename_ptr == NULL && function != NULL)
1229 *filename_ptr = next_line->filename;
1230 *linenumber_ptr = next_line->line;
1231 return TRUE;
1234 return FALSE;
1237 /* Function table functions. */
1239 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE. */
1241 static bfd_boolean
1242 lookup_address_in_function_table (struct funcinfo *table,
1243 bfd_vma addr,
1244 struct funcinfo **function_ptr,
1245 const char **functionname_ptr)
1247 struct funcinfo* each_func;
1249 for (each_func = table;
1250 each_func;
1251 each_func = each_func->prev_func)
1253 if (addr >= each_func->low && addr < each_func->high)
1255 *functionname_ptr = each_func->name;
1256 *function_ptr = each_func;
1257 return TRUE;
1261 return FALSE;
1264 static char *
1265 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1267 bfd *abfd = unit->abfd;
1268 char *info_ptr;
1269 unsigned int abbrev_number, bytes_read, i;
1270 struct abbrev_info *abbrev;
1271 struct attribute attr;
1272 char *name = 0;
1274 info_ptr = unit->info_ptr_unit + die_ref;
1275 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1276 info_ptr += bytes_read;
1278 if (abbrev_number)
1280 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1281 if (! abbrev)
1283 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1284 abbrev_number);
1285 bfd_set_error (bfd_error_bad_value);
1287 else
1289 for (i = 0; i < abbrev->num_attrs && !name; ++i)
1291 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1292 if (attr.name == DW_AT_name)
1293 name = attr.u.str;
1297 return (name);
1300 /* DWARF2 Compilation unit functions. */
1302 /* Scan over each die in a comp. unit looking for functions to add
1303 to the function table. */
1305 static bfd_boolean
1306 scan_unit_for_functions (struct comp_unit *unit)
1308 bfd *abfd = unit->abfd;
1309 char *info_ptr = unit->first_child_die_ptr;
1310 int nesting_level = 1;
1312 while (nesting_level)
1314 unsigned int abbrev_number, bytes_read, i;
1315 struct abbrev_info *abbrev;
1316 struct attribute attr;
1317 struct funcinfo *func;
1318 char* name = 0;
1320 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1321 info_ptr += bytes_read;
1323 if (! abbrev_number)
1325 nesting_level--;
1326 continue;
1329 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1330 if (! abbrev)
1332 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1333 abbrev_number);
1334 bfd_set_error (bfd_error_bad_value);
1335 return FALSE;
1338 if (abbrev->tag == DW_TAG_subprogram
1339 || abbrev->tag == DW_TAG_inlined_subroutine)
1341 bfd_size_type amt = sizeof (struct funcinfo);
1342 func = bfd_zalloc (abfd, amt);
1343 func->prev_func = unit->function_table;
1344 unit->function_table = func;
1346 else
1347 func = NULL;
1349 for (i = 0; i < abbrev->num_attrs; ++i)
1351 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1353 if (func)
1355 switch (attr.name)
1357 case DW_AT_abstract_origin:
1358 func->name = find_abstract_instance_name (unit, attr.u.val);
1359 break;
1361 case DW_AT_name:
1363 name = attr.u.str;
1365 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1366 if (func->name == NULL)
1367 func->name = attr.u.str;
1368 break;
1370 case DW_AT_MIPS_linkage_name:
1371 func->name = attr.u.str;
1372 break;
1374 case DW_AT_low_pc:
1375 func->low = attr.u.val;
1376 break;
1378 case DW_AT_high_pc:
1379 func->high = attr.u.val;
1380 break;
1382 default:
1383 break;
1386 else
1388 switch (attr.name)
1390 case DW_AT_name:
1391 name = attr.u.str;
1392 break;
1394 default:
1395 break;
1400 if (abbrev->has_children)
1401 nesting_level++;
1404 return TRUE;
1407 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1408 includes the compilation unit header that proceeds the DIE's, but
1409 does not include the length field that precedes each compilation
1410 unit header. END_PTR points one past the end of this comp unit.
1411 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1413 This routine does not read the whole compilation unit; only enough
1414 to get to the line number information for the compilation unit. */
1416 static struct comp_unit *
1417 parse_comp_unit (bfd *abfd,
1418 struct dwarf2_debug *stash,
1419 bfd_vma unit_length,
1420 char *info_ptr_unit,
1421 unsigned int offset_size)
1423 struct comp_unit* unit;
1424 unsigned int version;
1425 bfd_uint64_t abbrev_offset = 0;
1426 unsigned int addr_size;
1427 struct abbrev_info** abbrevs;
1428 unsigned int abbrev_number, bytes_read, i;
1429 struct abbrev_info *abbrev;
1430 struct attribute attr;
1431 char *info_ptr = stash->info_ptr;
1432 char *end_ptr = info_ptr + unit_length;
1433 bfd_size_type amt;
1435 version = read_2_bytes (abfd, info_ptr);
1436 info_ptr += 2;
1437 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1438 if (offset_size == 4)
1439 abbrev_offset = read_4_bytes (abfd, info_ptr);
1440 else
1441 abbrev_offset = read_8_bytes (abfd, info_ptr);
1442 info_ptr += offset_size;
1443 addr_size = read_1_byte (abfd, info_ptr);
1444 info_ptr += 1;
1446 if (version != 2)
1448 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1449 bfd_set_error (bfd_error_bad_value);
1450 return 0;
1453 if (addr_size > sizeof (bfd_vma))
1455 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1456 addr_size,
1457 (unsigned int) sizeof (bfd_vma));
1458 bfd_set_error (bfd_error_bad_value);
1459 return 0;
1462 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1464 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1465 bfd_set_error (bfd_error_bad_value);
1466 return 0;
1469 /* Read the abbrevs for this compilation unit into a table. */
1470 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1471 if (! abbrevs)
1472 return 0;
1474 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1475 info_ptr += bytes_read;
1476 if (! abbrev_number)
1478 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1479 abbrev_number);
1480 bfd_set_error (bfd_error_bad_value);
1481 return 0;
1484 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1485 if (! abbrev)
1487 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1488 abbrev_number);
1489 bfd_set_error (bfd_error_bad_value);
1490 return 0;
1493 amt = sizeof (struct comp_unit);
1494 unit = bfd_zalloc (abfd, amt);
1495 unit->abfd = abfd;
1496 unit->addr_size = addr_size;
1497 unit->offset_size = offset_size;
1498 unit->abbrevs = abbrevs;
1499 unit->end_ptr = end_ptr;
1500 unit->stash = stash;
1501 unit->info_ptr_unit = info_ptr_unit;
1503 for (i = 0; i < abbrev->num_attrs; ++i)
1505 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1507 /* Store the data if it is of an attribute we want to keep in a
1508 partial symbol table. */
1509 switch (attr.name)
1511 case DW_AT_stmt_list:
1512 unit->stmtlist = 1;
1513 unit->line_offset = attr.u.val;
1514 break;
1516 case DW_AT_name:
1517 unit->name = attr.u.str;
1518 break;
1520 case DW_AT_low_pc:
1521 unit->arange.low = attr.u.val;
1522 break;
1524 case DW_AT_high_pc:
1525 unit->arange.high = attr.u.val;
1526 break;
1528 case DW_AT_comp_dir:
1530 char* comp_dir = attr.u.str;
1531 if (comp_dir)
1533 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1534 directory, get rid of it. */
1535 char *cp = strchr (comp_dir, ':');
1537 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1538 comp_dir = cp + 1;
1540 unit->comp_dir = comp_dir;
1541 break;
1544 default:
1545 break;
1549 unit->first_child_die_ptr = info_ptr;
1550 return unit;
1553 /* Return TRUE if UNIT contains the address given by ADDR. */
1555 static bfd_boolean
1556 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
1558 struct arange *arange;
1560 if (unit->error)
1561 return FALSE;
1563 arange = &unit->arange;
1566 if (addr >= arange->low && addr < arange->high)
1567 return TRUE;
1568 arange = arange->next;
1570 while (arange);
1572 return FALSE;
1575 /* If UNIT contains ADDR, set the output parameters to the values for
1576 the line containing ADDR. The output parameters, FILENAME_PTR,
1577 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1578 to be filled in.
1580 Return TRUE if UNIT contains ADDR, and no errors were encountered;
1581 FALSE otherwise. */
1583 static bfd_boolean
1584 comp_unit_find_nearest_line (struct comp_unit *unit,
1585 bfd_vma addr,
1586 const char **filename_ptr,
1587 const char **functionname_ptr,
1588 unsigned int *linenumber_ptr,
1589 struct dwarf2_debug *stash)
1591 bfd_boolean line_p;
1592 bfd_boolean func_p;
1593 struct funcinfo *function;
1595 if (unit->error)
1596 return FALSE;
1598 if (! unit->line_table)
1600 if (! unit->stmtlist)
1602 unit->error = 1;
1603 return FALSE;
1606 unit->line_table = decode_line_info (unit, stash);
1608 if (! unit->line_table)
1610 unit->error = 1;
1611 return FALSE;
1614 if (unit->first_child_die_ptr < unit->end_ptr
1615 && ! scan_unit_for_functions (unit))
1617 unit->error = 1;
1618 return FALSE;
1622 function = NULL;
1623 func_p = lookup_address_in_function_table (unit->function_table, addr,
1624 &function, functionname_ptr);
1625 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
1626 function, filename_ptr,
1627 linenumber_ptr);
1628 return line_p || func_p;
1631 /* Locate a section in a BFD containing debugging info. The search starts
1632 from the section after AFTER_SEC, or from the first section in the BFD if
1633 AFTER_SEC is NULL. The search works by examining the names of the
1634 sections. There are two permissiable names. The first is .debug_info.
1635 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
1636 This is a variation on the .debug_info section which has a checksum
1637 describing the contents appended onto the name. This allows the linker to
1638 identify and discard duplicate debugging sections for different
1639 compilation units. */
1640 #define DWARF2_DEBUG_INFO ".debug_info"
1641 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1643 static asection *
1644 find_debug_info (bfd *abfd, asection *after_sec)
1646 asection * msec;
1648 if (after_sec)
1649 msec = after_sec->next;
1650 else
1651 msec = abfd->sections;
1653 while (msec)
1655 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1656 return msec;
1658 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1659 return msec;
1661 msec = msec->next;
1664 return NULL;
1667 /* The DWARF2 version of find_nearest_line. Return TRUE if the line
1668 is found without error. ADDR_SIZE is the number of bytes in the
1669 initial .debug_info length field and in the abbreviation offset.
1670 You may use zero to indicate that the default value should be
1671 used. */
1673 bfd_boolean
1674 _bfd_dwarf2_find_nearest_line (bfd *abfd,
1675 asection *section,
1676 asymbol **symbols,
1677 bfd_vma offset,
1678 const char **filename_ptr,
1679 const char **functionname_ptr,
1680 unsigned int *linenumber_ptr,
1681 unsigned int addr_size,
1682 void **pinfo)
1684 /* Read each compilation unit from the section .debug_info, and check
1685 to see if it contains the address we are searching for. If yes,
1686 lookup the address, and return the line number info. If no, go
1687 on to the next compilation unit.
1689 We keep a list of all the previously read compilation units, and
1690 a pointer to the next un-read compilation unit. Check the
1691 previously read units before reading more. */
1692 struct dwarf2_debug *stash;
1694 /* What address are we looking for? */
1695 bfd_vma addr;
1697 struct comp_unit* each;
1699 stash = *pinfo;
1700 addr = offset;
1701 if (section->output_section)
1702 addr += section->output_section->vma + section->output_offset;
1703 else
1704 addr += section->vma;
1705 *filename_ptr = NULL;
1706 *functionname_ptr = NULL;
1707 *linenumber_ptr = 0;
1709 /* The DWARF2 spec says that the initial length field, and the
1710 offset of the abbreviation table, should both be 4-byte values.
1711 However, some compilers do things differently. */
1712 if (addr_size == 0)
1713 addr_size = 4;
1714 BFD_ASSERT (addr_size == 4 || addr_size == 8);
1716 if (! stash)
1718 bfd_size_type total_size;
1719 asection *msec;
1720 bfd_size_type amt = sizeof (struct dwarf2_debug);
1722 stash = bfd_zalloc (abfd, amt);
1723 if (! stash)
1724 return FALSE;
1726 *pinfo = stash;
1728 msec = find_debug_info (abfd, NULL);
1729 if (! msec)
1730 /* No dwarf2 info. Note that at this point the stash
1731 has been allocated, but contains zeros, this lets
1732 future calls to this function fail quicker. */
1733 return FALSE;
1735 /* There can be more than one DWARF2 info section in a BFD these days.
1736 Read them all in and produce one large stash. We do this in two
1737 passes - in the first pass we just accumulate the section sizes.
1738 In the second pass we read in the section's contents. The allows
1739 us to avoid reallocing the data as we add sections to the stash. */
1740 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1741 total_size += msec->size;
1743 stash->info_ptr = bfd_alloc (abfd, total_size);
1744 if (stash->info_ptr == NULL)
1745 return FALSE;
1747 stash->info_ptr_end = stash->info_ptr;
1749 for (msec = find_debug_info (abfd, NULL);
1750 msec;
1751 msec = find_debug_info (abfd, msec))
1753 bfd_size_type size;
1754 bfd_size_type start;
1756 size = msec->size;
1757 if (size == 0)
1758 continue;
1760 start = stash->info_ptr_end - stash->info_ptr;
1762 if ((bfd_simple_get_relocated_section_contents
1763 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
1764 continue;
1766 stash->info_ptr_end = stash->info_ptr + start + size;
1769 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1771 stash->sec = find_debug_info (abfd, NULL);
1772 stash->sec_info_ptr = stash->info_ptr;
1773 stash->syms = symbols;
1776 /* A null info_ptr indicates that there is no dwarf2 info
1777 (or that an error occured while setting up the stash). */
1778 if (! stash->info_ptr)
1779 return FALSE;
1781 /* Check the previously read comp. units first. */
1782 for (each = stash->all_comp_units; each; each = each->next_unit)
1783 if (comp_unit_contains_address (each, addr))
1784 return comp_unit_find_nearest_line (each, addr, filename_ptr,
1785 functionname_ptr, linenumber_ptr,
1786 stash);
1788 /* Read each remaining comp. units checking each as they are read. */
1789 while (stash->info_ptr < stash->info_ptr_end)
1791 bfd_vma length;
1792 bfd_boolean found;
1793 unsigned int offset_size = addr_size;
1794 char *info_ptr_unit = stash->info_ptr;
1796 length = read_4_bytes (abfd, stash->info_ptr);
1797 /* A 0xffffff length is the DWARF3 way of indicating we use
1798 64-bit offsets, instead of 32-bit offsets. */
1799 if (length == 0xffffffff)
1801 offset_size = 8;
1802 length = read_8_bytes (abfd, stash->info_ptr + 4);
1803 stash->info_ptr += 12;
1805 /* A zero length is the IRIX way of indicating 64-bit offsets,
1806 mostly because the 64-bit length will generally fit in 32
1807 bits, and the endianness helps. */
1808 else if (length == 0)
1810 offset_size = 8;
1811 length = read_4_bytes (abfd, stash->info_ptr + 4);
1812 stash->info_ptr += 8;
1814 /* In the absence of the hints above, we assume addr_size-sized
1815 offsets, for backward-compatibility with pre-DWARF3 64-bit
1816 platforms. */
1817 else if (addr_size == 8)
1819 length = read_8_bytes (abfd, stash->info_ptr);
1820 stash->info_ptr += 8;
1822 else
1823 stash->info_ptr += 4;
1825 if (length > 0)
1827 each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
1828 offset_size);
1829 stash->info_ptr += length;
1831 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
1832 == stash->sec->size)
1834 stash->sec = find_debug_info (abfd, stash->sec);
1835 stash->sec_info_ptr = stash->info_ptr;
1838 if (each)
1840 each->next_unit = stash->all_comp_units;
1841 stash->all_comp_units = each;
1843 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1844 compilation units. If we don't have them (i.e.,
1845 unit->high == 0), we need to consult the line info
1846 table to see if a compilation unit contains the given
1847 address. */
1848 if (each->arange.high > 0)
1850 if (comp_unit_contains_address (each, addr))
1851 return comp_unit_find_nearest_line (each, addr,
1852 filename_ptr,
1853 functionname_ptr,
1854 linenumber_ptr,
1855 stash);
1857 else
1859 found = comp_unit_find_nearest_line (each, addr,
1860 filename_ptr,
1861 functionname_ptr,
1862 linenumber_ptr,
1863 stash);
1864 if (found)
1865 return TRUE;
1871 return FALSE;