2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
34 #include "libiberty.h"
37 #include "elf/dwarf2.h"
39 /* The data in the .debug_line statement prologue looks like this. */
44 unsigned short version;
45 bfd_vma prologue_length;
46 unsigned char minimum_instruction_length;
47 unsigned char default_is_stmt;
49 unsigned char line_range;
50 unsigned char opcode_base;
51 unsigned char *standard_opcode_lengths;
54 /* Attributes have a name and a value. */
58 enum dwarf_attribute name;
63 struct dwarf_block *blk;
70 /* Blocks are a bunch of untyped bytes. */
77 struct loadable_section
85 /* A list of all previously read comp_units. */
86 struct comp_unit *all_comp_units;
88 /* The next unread compilation unit within the .debug_info section.
89 Zero indicates that the .debug_info section has not been loaded
93 /* Pointer to the end of the .debug_info section memory buffer. */
94 bfd_byte *info_ptr_end;
96 /* Pointer to the bfd, section and address of the beginning of the
97 section. The bfd might be different than expected because of
98 gnu_debuglink sections. */
101 bfd_byte *sec_info_ptr;
103 /* Pointer to the symbol table. */
106 /* Pointer to the .debug_abbrev section loaded into memory. */
107 bfd_byte *dwarf_abbrev_buffer;
109 /* Length of the loaded .debug_abbrev section. */
110 unsigned long dwarf_abbrev_size;
112 /* Buffer for decode_line_info. */
113 bfd_byte *dwarf_line_buffer;
115 /* Length of the loaded .debug_line section. */
116 unsigned long dwarf_line_size;
118 /* Pointer to the .debug_str section loaded into memory. */
119 bfd_byte *dwarf_str_buffer;
121 /* Length of the loaded .debug_str section. */
122 unsigned long dwarf_str_size;
124 /* Pointer to the .debug_ranges section loaded into memory. */
125 bfd_byte *dwarf_ranges_buffer;
127 /* Length of the loaded .debug_ranges section. */
128 unsigned long dwarf_ranges_size;
130 /* If the most recent call to bfd_find_nearest_line was given an
131 address in an inlined function, preserve a pointer into the
132 calling chain for subsequent calls to bfd_find_inliner_info to
134 struct funcinfo *inliner_chain;
136 /* Number of loadable sections. */
137 unsigned int loadable_section_count;
139 /* Array of loadable sections. */
140 struct loadable_section *loadable_sections;
150 /* A minimal decoding of DWARF2 compilation units. We only decode
151 what's needed to get to the line number information. */
155 /* Chain the previously read compilation units. */
156 struct comp_unit *next_unit;
158 /* Keep the bfd convenient (for memory allocation). */
161 /* The lowest and highest addresses contained in this compilation
162 unit as specified in the compilation unit header. */
163 struct arange arange;
165 /* The DW_AT_name attribute (for error messages). */
168 /* The abbrev hash table. */
169 struct abbrev_info **abbrevs;
171 /* Note that an error was found by comp_unit_find_nearest_line. */
174 /* The DW_AT_comp_dir attribute. */
177 /* TRUE if there is a line number table associated with this comp. unit. */
180 /* Pointer to the current comp_unit so that we can find a given entry
182 bfd_byte *info_ptr_unit;
184 /* The offset into .debug_line of the line number table. */
185 unsigned long line_offset;
187 /* Pointer to the first child die for the comp unit. */
188 bfd_byte *first_child_die_ptr;
190 /* The end of the comp unit. */
193 /* The decoded line number, NULL if not yet decoded. */
194 struct line_info_table *line_table;
196 /* A list of the functions found in this comp. unit. */
197 struct funcinfo *function_table;
199 /* A list of the variables found in this comp. unit. */
200 struct varinfo *variable_table;
202 /* Pointer to dwarf2_debug structure. */
203 struct dwarf2_debug *stash;
205 /* Address size for this unit - from unit header. */
206 unsigned char addr_size;
208 /* Offset size for this unit - from unit header. */
209 unsigned char offset_size;
211 /* Base address for this unit - from DW_AT_low_pc attribute of
212 DW_TAG_compile_unit DIE */
213 bfd_vma base_address;
216 /* This data structure holds the information of an abbrev. */
219 unsigned int number; /* Number identifying abbrev. */
220 enum dwarf_tag tag; /* DWARF tag. */
221 int has_children; /* Boolean. */
222 unsigned int num_attrs; /* Number of attributes. */
223 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
224 struct abbrev_info *next; /* Next in chain. */
229 enum dwarf_attribute name;
230 enum dwarf_form form;
233 #ifndef ABBREV_HASH_SIZE
234 #define ABBREV_HASH_SIZE 121
236 #ifndef ATTR_ALLOC_CHUNK
237 #define ATTR_ALLOC_CHUNK 4
241 The following function up to the END VERBATIM mark are
242 copied directly from dwarf2read.c. */
244 /* Read dwarf information from a buffer. */
247 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
249 return bfd_get_8 (abfd, buf);
253 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
255 return bfd_get_signed_8 (abfd, buf);
259 read_2_bytes (bfd *abfd, bfd_byte *buf)
261 return bfd_get_16 (abfd, buf);
265 read_4_bytes (bfd *abfd, bfd_byte *buf)
267 return bfd_get_32 (abfd, buf);
271 read_8_bytes (bfd *abfd, bfd_byte *buf)
273 return bfd_get_64 (abfd, buf);
277 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
279 unsigned int size ATTRIBUTE_UNUSED)
281 /* If the size of a host char is 8 bits, we can return a pointer
282 to the buffer, otherwise we have to copy the data to a buffer
283 allocated on the temporary obstack. */
288 read_string (bfd *abfd ATTRIBUTE_UNUSED,
290 unsigned int *bytes_read_ptr)
292 /* Return a pointer to the embedded string. */
293 char *str = (char *) buf;
300 *bytes_read_ptr = strlen (str) + 1;
305 read_indirect_string (struct comp_unit* unit,
307 unsigned int *bytes_read_ptr)
310 struct dwarf2_debug *stash = unit->stash;
313 if (unit->offset_size == 4)
314 offset = read_4_bytes (unit->abfd, buf);
316 offset = read_8_bytes (unit->abfd, buf);
317 *bytes_read_ptr = unit->offset_size;
319 if (! stash->dwarf_str_buffer)
322 bfd *abfd = unit->abfd;
325 msec = bfd_get_section_by_name (abfd, ".debug_str");
328 (*_bfd_error_handler)
329 (_("Dwarf Error: Can't find .debug_str section."));
330 bfd_set_error (bfd_error_bad_value);
334 sz = msec->rawsize ? msec->rawsize : msec->size;
335 stash->dwarf_str_size = sz;
336 stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
337 if (! stash->dwarf_str_buffer)
340 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
345 if (offset >= stash->dwarf_str_size)
347 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
348 (unsigned long) offset, stash->dwarf_str_size);
349 bfd_set_error (bfd_error_bad_value);
353 str = (char *) stash->dwarf_str_buffer + offset;
362 read_address (struct comp_unit *unit, bfd_byte *buf)
364 int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
368 switch (unit->addr_size)
371 return bfd_get_signed_64 (unit->abfd, buf);
373 return bfd_get_signed_32 (unit->abfd, buf);
375 return bfd_get_signed_16 (unit->abfd, buf);
382 switch (unit->addr_size)
385 return bfd_get_64 (unit->abfd, buf);
387 return bfd_get_32 (unit->abfd, buf);
389 return bfd_get_16 (unit->abfd, buf);
396 /* Lookup an abbrev_info structure in the abbrev hash table. */
398 static struct abbrev_info *
399 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
401 unsigned int hash_number;
402 struct abbrev_info *abbrev;
404 hash_number = number % ABBREV_HASH_SIZE;
405 abbrev = abbrevs[hash_number];
409 if (abbrev->number == number)
412 abbrev = abbrev->next;
418 /* In DWARF version 2, the description of the debugging information is
419 stored in a separate .debug_abbrev section. Before we read any
420 dies from a section we read in all abbreviations and install them
423 static struct abbrev_info**
424 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
426 struct abbrev_info **abbrevs;
427 bfd_byte *abbrev_ptr;
428 struct abbrev_info *cur_abbrev;
429 unsigned int abbrev_number, bytes_read, abbrev_name;
430 unsigned int abbrev_form, hash_number;
433 if (! stash->dwarf_abbrev_buffer)
437 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
440 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
441 bfd_set_error (bfd_error_bad_value);
445 stash->dwarf_abbrev_size = msec->size;
446 stash->dwarf_abbrev_buffer
447 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
449 if (! stash->dwarf_abbrev_buffer)
453 if (offset >= stash->dwarf_abbrev_size)
455 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
456 (unsigned long) offset, stash->dwarf_abbrev_size);
457 bfd_set_error (bfd_error_bad_value);
461 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
462 abbrevs = bfd_zalloc (abfd, amt);
464 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
465 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
466 abbrev_ptr += bytes_read;
468 /* Loop until we reach an abbrev number of 0. */
469 while (abbrev_number)
471 amt = sizeof (struct abbrev_info);
472 cur_abbrev = bfd_zalloc (abfd, amt);
474 /* Read in abbrev header. */
475 cur_abbrev->number = abbrev_number;
476 cur_abbrev->tag = (enum dwarf_tag)
477 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
478 abbrev_ptr += bytes_read;
479 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
482 /* Now read in declarations. */
483 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
484 abbrev_ptr += bytes_read;
485 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
486 abbrev_ptr += bytes_read;
490 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
492 struct attr_abbrev *tmp;
494 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
495 amt *= sizeof (struct attr_abbrev);
496 tmp = bfd_realloc (cur_abbrev->attrs, amt);
501 for (i = 0; i < ABBREV_HASH_SIZE; i++)
503 struct abbrev_info *abbrev = abbrevs[i];
507 free (abbrev->attrs);
508 abbrev = abbrev->next;
513 cur_abbrev->attrs = tmp;
516 cur_abbrev->attrs[cur_abbrev->num_attrs].name
517 = (enum dwarf_attribute) abbrev_name;
518 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
519 = (enum dwarf_form) abbrev_form;
520 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
521 abbrev_ptr += bytes_read;
522 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
523 abbrev_ptr += bytes_read;
526 hash_number = abbrev_number % ABBREV_HASH_SIZE;
527 cur_abbrev->next = abbrevs[hash_number];
528 abbrevs[hash_number] = cur_abbrev;
530 /* Get next abbreviation.
531 Under Irix6 the abbreviations for a compilation unit are not
532 always properly terminated with an abbrev number of 0.
533 Exit loop if we encounter an abbreviation which we have
534 already read (which means we are about to read the abbreviations
535 for the next compile unit) or if the end of the abbreviation
537 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
538 >= stash->dwarf_abbrev_size)
540 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
541 abbrev_ptr += bytes_read;
542 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
549 /* Read an attribute value described by an attribute form. */
552 read_attribute_value (struct attribute *attr,
554 struct comp_unit *unit,
557 bfd *abfd = unit->abfd;
558 unsigned int bytes_read;
559 struct dwarf_block *blk;
562 attr->form = (enum dwarf_form) form;
567 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
568 case DW_FORM_ref_addr:
569 attr->u.val = read_address (unit, info_ptr);
570 info_ptr += unit->addr_size;
573 amt = sizeof (struct dwarf_block);
574 blk = bfd_alloc (abfd, amt);
575 blk->size = read_2_bytes (abfd, info_ptr);
577 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
578 info_ptr += blk->size;
582 amt = sizeof (struct dwarf_block);
583 blk = bfd_alloc (abfd, amt);
584 blk->size = read_4_bytes (abfd, info_ptr);
586 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
587 info_ptr += blk->size;
591 attr->u.val = read_2_bytes (abfd, info_ptr);
595 attr->u.val = read_4_bytes (abfd, info_ptr);
599 attr->u.val = read_8_bytes (abfd, info_ptr);
603 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
604 info_ptr += bytes_read;
607 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
608 info_ptr += bytes_read;
611 amt = sizeof (struct dwarf_block);
612 blk = bfd_alloc (abfd, amt);
613 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
614 info_ptr += bytes_read;
615 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
616 info_ptr += blk->size;
620 amt = sizeof (struct dwarf_block);
621 blk = bfd_alloc (abfd, amt);
622 blk->size = read_1_byte (abfd, info_ptr);
624 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
625 info_ptr += blk->size;
629 attr->u.val = read_1_byte (abfd, info_ptr);
633 attr->u.val = read_1_byte (abfd, info_ptr);
637 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
638 info_ptr += bytes_read;
641 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
642 info_ptr += bytes_read;
645 attr->u.val = read_1_byte (abfd, info_ptr);
649 attr->u.val = read_2_bytes (abfd, info_ptr);
653 attr->u.val = read_4_bytes (abfd, info_ptr);
657 attr->u.val = read_8_bytes (abfd, info_ptr);
660 case DW_FORM_ref_udata:
661 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
662 info_ptr += bytes_read;
664 case DW_FORM_indirect:
665 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
666 info_ptr += bytes_read;
667 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
670 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
672 bfd_set_error (bfd_error_bad_value);
677 /* Read an attribute described by an abbreviated attribute. */
680 read_attribute (struct attribute *attr,
681 struct attr_abbrev *abbrev,
682 struct comp_unit *unit,
685 attr->name = abbrev->name;
686 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
690 /* Source line information table routines. */
692 #define FILE_ALLOC_CHUNK 5
693 #define DIR_ALLOC_CHUNK 5
697 struct line_info* prev_line;
702 int end_sequence; /* End of (sequential) code sequence. */
713 struct line_info_table
716 unsigned int num_files;
717 unsigned int num_dirs;
720 struct fileinfo* files;
721 struct line_info* last_line; /* largest VMA */
722 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
725 /* Remember some information about each function. If the function is
726 inlined (DW_TAG_inlined_subroutine) it may have two additional
727 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
728 source code location where this function was inlined. */
732 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */
733 struct funcinfo *caller_func; /* Pointer to function one scope higher */
734 char *caller_file; /* Source location file name where caller_func inlines this func */
735 int caller_line; /* Source location line number where caller_func inlines this func */
736 char *file; /* Source location file name */
737 int line; /* Source location line number */
740 struct arange arange;
741 asection *sec; /* Where the symbol is defined */
746 /* Pointer to previous variable in list of all variables */
747 struct varinfo *prev_var;
748 /* Source location file name */
750 /* Source location line number */
755 /* Where the symbol is defined */
757 /* Is this a stack variable? */
758 unsigned int stack: 1;
761 /* Return TRUE if NEW_LINE should sort after LINE. */
763 static inline bfd_boolean
764 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
766 return (new_line->address > line->address
767 || (new_line->address == line->address
768 && new_line->end_sequence < line->end_sequence));
772 /* Adds a new entry to the line_info list in the line_info_table, ensuring
773 that the list is sorted. Note that the line_info list is sorted from
774 highest to lowest VMA (with possible duplicates); that is,
775 line_info->prev_line always accesses an equal or smaller VMA. */
778 add_line_info (struct line_info_table *table,
785 bfd_size_type amt = sizeof (struct line_info);
786 struct line_info* info = bfd_alloc (table->abfd, amt);
788 /* Set member data of 'info'. */
789 info->address = address;
791 info->column = column;
792 info->end_sequence = end_sequence;
794 if (filename && filename[0])
796 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
798 strcpy (info->filename, filename);
801 info->filename = NULL;
803 /* Find the correct location for 'info'. Normally we will receive
804 new line_info data 1) in order and 2) with increasing VMAs.
805 However some compilers break the rules (cf. decode_line_info) and
806 so we include some heuristics for quickly finding the correct
807 location for 'info'. In particular, these heuristics optimize for
808 the common case in which the VMA sequence that we receive is a
809 list of locally sorted VMAs such as
810 p...z a...j (where a < j < p < z)
812 Note: table->lcl_head is used to head an *actual* or *possible*
813 sequence within the list (such as a...j) that is not directly
814 headed by table->last_line
816 Note: we may receive duplicate entries from 'decode_line_info'. */
818 if (!table->last_line
819 || new_line_sorts_after (info, table->last_line))
821 /* Normal case: add 'info' to the beginning of the list */
822 info->prev_line = table->last_line;
823 table->last_line = info;
825 /* lcl_head: initialize to head a *possible* sequence at the end. */
826 if (!table->lcl_head)
827 table->lcl_head = info;
829 else if (!new_line_sorts_after (info, table->lcl_head)
830 && (!table->lcl_head->prev_line
831 || new_line_sorts_after (info, table->lcl_head->prev_line)))
833 /* Abnormal but easy: lcl_head is the head of 'info'. */
834 info->prev_line = table->lcl_head->prev_line;
835 table->lcl_head->prev_line = info;
839 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
840 heads for 'info'. Reset 'lcl_head'. */
841 struct line_info* li2 = table->last_line; /* always non-NULL */
842 struct line_info* li1 = li2->prev_line;
846 if (!new_line_sorts_after (info, li2)
847 && new_line_sorts_after (info, li1))
850 li2 = li1; /* always non-NULL */
851 li1 = li1->prev_line;
853 table->lcl_head = li2;
854 info->prev_line = table->lcl_head->prev_line;
855 table->lcl_head->prev_line = info;
859 /* Extract a fully qualified filename from a line info table.
860 The returned string has been malloc'ed and it is the caller's
861 responsibility to free it. */
864 concat_filename (struct line_info_table *table, unsigned int file)
868 if (file - 1 >= table->num_files)
870 /* FILE == 0 means unknown. */
872 (*_bfd_error_handler)
873 (_("Dwarf Error: mangled line number section (bad file number)."));
874 return strdup ("<unknown>");
877 filename = table->files[file - 1].name;
879 if (!IS_ABSOLUTE_PATH (filename))
881 char *dirname = NULL;
882 char *subdirname = NULL;
886 if (table->files[file - 1].dir)
887 subdirname = table->dirs[table->files[file - 1].dir - 1];
889 if (!subdirname || !IS_ABSOLUTE_PATH (subdirname))
890 dirname = table->comp_dir;
894 dirname = subdirname;
899 return strdup (filename);
901 len = strlen (dirname) + strlen (filename) + 2;
905 len += strlen (subdirname) + 1;
906 name = bfd_malloc (len);
908 sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
912 name = bfd_malloc (len);
914 sprintf (name, "%s/%s", dirname, filename);
920 return strdup (filename);
924 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
926 struct arange *arange;
928 /* If the first arange is empty, use it. */
929 if (first_arange->high == 0)
931 first_arange->low = low_pc;
932 first_arange->high = high_pc;
936 /* Next see if we can cheaply extend an existing range. */
937 arange = first_arange;
940 if (low_pc == arange->high)
942 arange->high = high_pc;
945 if (high_pc == arange->low)
947 arange->low = low_pc;
950 arange = arange->next;
954 /* Need to allocate a new arange and insert it into the arange list.
955 Order isn't significant, so just insert after the first arange. */
956 arange = bfd_zalloc (abfd, sizeof (*arange));
957 arange->low = low_pc;
958 arange->high = high_pc;
959 arange->next = first_arange->next;
960 first_arange->next = arange;
963 /* Decode the line number information for UNIT. */
965 static struct line_info_table*
966 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
968 bfd *abfd = unit->abfd;
969 struct line_info_table* table;
973 unsigned int i, bytes_read, offset_size;
974 char *cur_file, *cur_dir;
975 unsigned char op_code, extended_op, adj_opcode;
978 if (! stash->dwarf_line_buffer)
982 msec = bfd_get_section_by_name (abfd, ".debug_line");
985 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
986 bfd_set_error (bfd_error_bad_value);
990 stash->dwarf_line_size = msec->size;
991 stash->dwarf_line_buffer
992 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
994 if (! stash->dwarf_line_buffer)
998 /* It is possible to get a bad value for the line_offset. Validate
999 it here so that we won't get a segfault below. */
1000 if (unit->line_offset >= stash->dwarf_line_size)
1002 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
1003 unit->line_offset, stash->dwarf_line_size);
1004 bfd_set_error (bfd_error_bad_value);
1008 amt = sizeof (struct line_info_table);
1009 table = bfd_alloc (abfd, amt);
1011 table->comp_dir = unit->comp_dir;
1013 table->num_files = 0;
1014 table->files = NULL;
1016 table->num_dirs = 0;
1019 table->files = NULL;
1020 table->last_line = NULL;
1021 table->lcl_head = NULL;
1023 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1025 /* Read in the prologue. */
1026 lh.total_length = read_4_bytes (abfd, line_ptr);
1029 if (lh.total_length == 0xffffffff)
1031 lh.total_length = read_8_bytes (abfd, line_ptr);
1035 else if (lh.total_length == 0 && unit->addr_size == 8)
1037 /* Handle (non-standard) 64-bit DWARF2 formats. */
1038 lh.total_length = read_4_bytes (abfd, line_ptr);
1042 line_end = line_ptr + lh.total_length;
1043 lh.version = read_2_bytes (abfd, line_ptr);
1045 if (offset_size == 4)
1046 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1048 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1049 line_ptr += offset_size;
1050 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1052 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1054 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1056 lh.line_range = read_1_byte (abfd, line_ptr);
1058 lh.opcode_base = read_1_byte (abfd, line_ptr);
1060 amt = lh.opcode_base * sizeof (unsigned char);
1061 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
1063 lh.standard_opcode_lengths[0] = 1;
1065 for (i = 1; i < lh.opcode_base; ++i)
1067 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1071 /* Read directory table. */
1072 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1074 line_ptr += bytes_read;
1076 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1080 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1081 amt *= sizeof (char *);
1083 tmp = bfd_realloc (table->dirs, amt);
1092 table->dirs[table->num_dirs++] = cur_dir;
1095 line_ptr += bytes_read;
1097 /* Read file name table. */
1098 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1100 line_ptr += bytes_read;
1102 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1104 struct fileinfo *tmp;
1106 amt = table->num_files + FILE_ALLOC_CHUNK;
1107 amt *= sizeof (struct fileinfo);
1109 tmp = bfd_realloc (table->files, amt);
1112 free (table->files);
1119 table->files[table->num_files].name = cur_file;
1120 table->files[table->num_files].dir =
1121 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1122 line_ptr += bytes_read;
1123 table->files[table->num_files].time =
1124 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1125 line_ptr += bytes_read;
1126 table->files[table->num_files].size =
1127 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1128 line_ptr += bytes_read;
1132 line_ptr += bytes_read;
1134 /* Read the statement sequences until there's nothing left. */
1135 while (line_ptr < line_end)
1137 /* State machine registers. */
1138 bfd_vma address = 0;
1139 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1140 unsigned int line = 1;
1141 unsigned int column = 0;
1142 int is_stmt = lh.default_is_stmt;
1143 int end_sequence = 0;
1144 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1145 compilers generate address sequences that are wildly out of
1146 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1147 for ia64-Linux). Thus, to determine the low and high
1148 address, we must compare on every DW_LNS_copy, etc. */
1149 bfd_vma low_pc = (bfd_vma) -1;
1150 bfd_vma high_pc = 0;
1152 /* Decode the table. */
1153 while (! end_sequence)
1155 op_code = read_1_byte (abfd, line_ptr);
1158 if (op_code >= lh.opcode_base)
1160 /* Special operand. */
1161 adj_opcode = op_code - lh.opcode_base;
1162 address += (adj_opcode / lh.line_range)
1163 * lh.minimum_instruction_length;
1164 line += lh.line_base + (adj_opcode % lh.line_range);
1165 /* Append row to matrix using current values. */
1166 add_line_info (table, address, filename, line, column, 0);
1167 if (address < low_pc)
1169 if (address > high_pc)
1172 else switch (op_code)
1174 case DW_LNS_extended_op:
1175 /* Ignore length. */
1177 extended_op = read_1_byte (abfd, line_ptr);
1180 switch (extended_op)
1182 case DW_LNE_end_sequence:
1184 add_line_info (table, address, filename, line, column,
1186 if (address < low_pc)
1188 if (address > high_pc)
1190 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1192 case DW_LNE_set_address:
1193 address = read_address (unit, line_ptr);
1194 line_ptr += unit->addr_size;
1196 case DW_LNE_define_file:
1197 cur_file = read_string (abfd, line_ptr, &bytes_read);
1198 line_ptr += bytes_read;
1199 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1201 struct fileinfo *tmp;
1203 amt = table->num_files + FILE_ALLOC_CHUNK;
1204 amt *= sizeof (struct fileinfo);
1205 tmp = bfd_realloc (table->files, amt);
1208 free (table->files);
1215 table->files[table->num_files].name = cur_file;
1216 table->files[table->num_files].dir =
1217 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1218 line_ptr += bytes_read;
1219 table->files[table->num_files].time =
1220 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1221 line_ptr += bytes_read;
1222 table->files[table->num_files].size =
1223 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1224 line_ptr += bytes_read;
1228 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1229 bfd_set_error (bfd_error_bad_value);
1231 free (table->files);
1237 add_line_info (table, address, filename, line, column, 0);
1238 if (address < low_pc)
1240 if (address > high_pc)
1243 case DW_LNS_advance_pc:
1244 address += lh.minimum_instruction_length
1245 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1246 line_ptr += bytes_read;
1248 case DW_LNS_advance_line:
1249 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1250 line_ptr += bytes_read;
1252 case DW_LNS_set_file:
1256 /* The file and directory tables are 0
1257 based, the references are 1 based. */
1258 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1259 line_ptr += bytes_read;
1262 filename = concat_filename (table, file);
1265 case DW_LNS_set_column:
1266 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1267 line_ptr += bytes_read;
1269 case DW_LNS_negate_stmt:
1270 is_stmt = (!is_stmt);
1272 case DW_LNS_set_basic_block:
1274 case DW_LNS_const_add_pc:
1275 address += lh.minimum_instruction_length
1276 * ((255 - lh.opcode_base) / lh.line_range);
1278 case DW_LNS_fixed_advance_pc:
1279 address += read_2_bytes (abfd, line_ptr);
1286 /* Unknown standard opcode, ignore it. */
1287 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1289 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1290 line_ptr += bytes_read;
1303 /* If ADDR is within TABLE set the output parameters and return TRUE,
1304 otherwise return FALSE. The output parameters, FILENAME_PTR and
1305 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1308 lookup_address_in_line_info_table (struct line_info_table *table,
1310 struct funcinfo *function,
1311 const char **filename_ptr,
1312 unsigned int *linenumber_ptr)
1314 /* Note: table->last_line should be a descendingly sorted list. */
1315 struct line_info* next_line = table->last_line;
1316 struct line_info* each_line = NULL;
1317 *filename_ptr = NULL;
1322 each_line = next_line->prev_line;
1324 /* Check for large addresses */
1325 if (addr > next_line->address)
1326 each_line = NULL; /* ensure we skip over the normal case */
1328 /* Normal case: search the list; save */
1329 while (each_line && next_line)
1331 /* If we have an address match, save this info. This allows us
1332 to return as good as results as possible for strange debugging
1334 bfd_boolean addr_match = FALSE;
1335 if (each_line->address <= addr && addr < next_line->address)
1339 /* If this line appears to span functions, and addr is in the
1340 later function, return the first line of that function instead
1341 of the last line of the earlier one. This check is for GCC
1342 2.95, which emits the first line number for a function late. */
1344 if (function != NULL)
1347 struct arange *arange;
1349 /* Find the lowest address in the function's range list */
1350 lowest_pc = function->arange.low;
1351 for (arange = &function->arange;
1353 arange = arange->next)
1355 if (function->arange.low < lowest_pc)
1356 lowest_pc = function->arange.low;
1358 /* Check for spanning function and set outgoing line info */
1359 if (addr >= lowest_pc
1360 && each_line->address < lowest_pc
1361 && next_line->address > lowest_pc)
1363 *filename_ptr = next_line->filename;
1364 *linenumber_ptr = next_line->line;
1368 *filename_ptr = each_line->filename;
1369 *linenumber_ptr = each_line->line;
1374 *filename_ptr = each_line->filename;
1375 *linenumber_ptr = each_line->line;
1379 if (addr_match && !each_line->end_sequence)
1380 return TRUE; /* we have definitely found what we want */
1382 next_line = each_line;
1383 each_line = each_line->prev_line;
1386 /* At this point each_line is NULL but next_line is not. If we found
1387 a candidate end-of-sequence point in the loop above, we can return
1388 that (compatibility with a bug in the Intel compiler); otherwise,
1389 assuming that we found the containing function for this address in
1390 this compilation unit, return the first line we have a number for
1391 (compatibility with GCC 2.95). */
1392 if (*filename_ptr == NULL && function != NULL)
1394 *filename_ptr = next_line->filename;
1395 *linenumber_ptr = next_line->line;
1402 /* Read in the .debug_ranges section for future reference */
1405 read_debug_ranges (struct comp_unit *unit)
1407 struct dwarf2_debug *stash = unit->stash;
1408 if (! stash->dwarf_ranges_buffer)
1410 bfd *abfd = unit->abfd;
1413 msec = bfd_get_section_by_name (abfd, ".debug_ranges");
1416 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
1417 bfd_set_error (bfd_error_bad_value);
1421 stash->dwarf_ranges_size = msec->size;
1422 stash->dwarf_ranges_buffer
1423 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1425 if (! stash->dwarf_ranges_buffer)
1431 /* Function table functions. */
1433 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1434 Note that we need to find the function that has the smallest
1435 range that contains ADDR, to handle inlined functions without
1436 depending upon them being ordered in TABLE by increasing range. */
1439 lookup_address_in_function_table (struct comp_unit *unit,
1441 struct funcinfo **function_ptr,
1442 const char **functionname_ptr)
1444 struct funcinfo* each_func;
1445 struct funcinfo* best_fit = NULL;
1446 struct arange *arange;
1448 for (each_func = unit->function_table;
1450 each_func = each_func->prev_func)
1452 for (arange = &each_func->arange;
1454 arange = arange->next)
1456 if (addr >= arange->low && addr < arange->high)
1459 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1460 best_fit = each_func;
1467 *functionname_ptr = best_fit->name;
1468 *function_ptr = best_fit;
1477 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1478 and LINENUMBER_PTR, and return TRUE. */
1481 lookup_symbol_in_function_table (struct comp_unit *unit,
1484 const char **filename_ptr,
1485 unsigned int *linenumber_ptr)
1487 struct funcinfo* each_func;
1488 struct funcinfo* best_fit = NULL;
1489 struct arange *arange;
1490 const char *name = bfd_asymbol_name (sym);
1491 asection *sec = bfd_get_section (sym);
1493 for (each_func = unit->function_table;
1495 each_func = each_func->prev_func)
1497 for (arange = &each_func->arange;
1499 arange = arange->next)
1501 if ((!each_func->sec || each_func->sec == sec)
1502 && addr >= arange->low
1503 && addr < arange->high
1505 && strcmp (name, each_func->name) == 0
1507 || ((arange->high - arange->low)
1508 < (best_fit->arange.high - best_fit->arange.low))))
1509 best_fit = each_func;
1515 best_fit->sec = sec;
1516 *filename_ptr = best_fit->file;
1517 *linenumber_ptr = best_fit->line;
1524 /* Variable table functions. */
1526 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1527 LINENUMBER_PTR, and return TRUE. */
1530 lookup_symbol_in_variable_table (struct comp_unit *unit,
1533 const char **filename_ptr,
1534 unsigned int *linenumber_ptr)
1536 const char *name = bfd_asymbol_name (sym);
1537 asection *sec = bfd_get_section (sym);
1538 struct varinfo* each;
1540 for (each = unit->variable_table; each; each = each->prev_var)
1541 if (each->stack == 0
1542 && each->file != NULL
1543 && each->name != NULL
1544 && each->addr == addr
1545 && (!each->sec || each->sec == sec)
1546 && strcmp (name, each->name) == 0)
1552 *filename_ptr = each->file;
1553 *linenumber_ptr = each->line;
1561 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1563 bfd *abfd = unit->abfd;
1565 unsigned int abbrev_number, bytes_read, i;
1566 struct abbrev_info *abbrev;
1567 struct attribute attr;
1570 info_ptr = unit->info_ptr_unit + die_ref;
1571 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1572 info_ptr += bytes_read;
1576 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1579 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1581 bfd_set_error (bfd_error_bad_value);
1585 for (i = 0; i < abbrev->num_attrs; ++i)
1587 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1591 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1595 case DW_AT_specification:
1596 name = find_abstract_instance_name (unit, attr.u.val);
1598 case DW_AT_MIPS_linkage_name:
1611 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1613 bfd_byte *ranges_ptr;
1614 bfd_vma base_address = unit->base_address;
1616 if (! unit->stash->dwarf_ranges_buffer)
1618 if (! read_debug_ranges (unit))
1621 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1628 if (unit->addr_size == 4)
1630 low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1632 high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1637 low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1639 high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1642 if (low_pc == 0 && high_pc == 0)
1644 if (low_pc == -1UL && high_pc != -1UL)
1645 base_address = high_pc;
1647 arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1651 /* DWARF2 Compilation unit functions. */
1653 /* Scan over each die in a comp. unit looking for functions to add
1654 to the function table and variables to the variable table. */
1657 scan_unit_for_symbols (struct comp_unit *unit)
1659 bfd *abfd = unit->abfd;
1660 bfd_byte *info_ptr = unit->first_child_die_ptr;
1661 int nesting_level = 1;
1662 struct funcinfo **nested_funcs;
1663 int nested_funcs_size;
1665 /* Maintain a stack of in-scope functions and inlined functions, which we
1666 can use to set the caller_func field. */
1667 nested_funcs_size = 32;
1668 nested_funcs = bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
1669 if (nested_funcs == NULL)
1671 nested_funcs[nesting_level] = 0;
1673 while (nesting_level)
1675 unsigned int abbrev_number, bytes_read, i;
1676 struct abbrev_info *abbrev;
1677 struct attribute attr;
1678 struct funcinfo *func;
1679 struct varinfo *var;
1681 bfd_vma high_pc = 0;
1683 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1684 info_ptr += bytes_read;
1686 if (! abbrev_number)
1692 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1695 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1697 bfd_set_error (bfd_error_bad_value);
1698 free (nested_funcs);
1703 if (abbrev->tag == DW_TAG_subprogram
1704 || abbrev->tag == DW_TAG_entry_point
1705 || abbrev->tag == DW_TAG_inlined_subroutine)
1707 bfd_size_type amt = sizeof (struct funcinfo);
1708 func = bfd_zalloc (abfd, amt);
1709 func->tag = abbrev->tag;
1710 func->prev_func = unit->function_table;
1711 unit->function_table = func;
1713 if (func->tag == DW_TAG_inlined_subroutine)
1714 for (i = nesting_level - 1; i >= 1; i--)
1715 if (nested_funcs[i])
1717 func->caller_func = nested_funcs[i];
1720 nested_funcs[nesting_level] = func;
1725 if (abbrev->tag == DW_TAG_variable)
1727 bfd_size_type amt = sizeof (struct varinfo);
1728 var = bfd_zalloc (abfd, amt);
1729 var->tag = abbrev->tag;
1731 var->prev_var = unit->variable_table;
1732 unit->variable_table = var;
1735 /* No inline function in scope at this nesting level. */
1736 nested_funcs[nesting_level] = 0;
1739 for (i = 0; i < abbrev->num_attrs; ++i)
1741 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1747 case DW_AT_call_file:
1748 func->caller_file = concat_filename (unit->line_table, attr.u.val);
1751 case DW_AT_call_line:
1752 func->caller_line = attr.u.val;
1755 case DW_AT_abstract_origin:
1756 func->name = find_abstract_instance_name (unit, attr.u.val);
1760 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1761 if (func->name == NULL)
1762 func->name = attr.u.str;
1765 case DW_AT_MIPS_linkage_name:
1766 func->name = attr.u.str;
1770 low_pc = attr.u.val;
1774 high_pc = attr.u.val;
1778 read_rangelist (unit, &func->arange, attr.u.val);
1781 case DW_AT_decl_file:
1782 func->file = concat_filename (unit->line_table,
1786 case DW_AT_decl_line:
1787 func->line = attr.u.val;
1799 var->name = attr.u.str;
1802 case DW_AT_decl_file:
1803 var->file = concat_filename (unit->line_table,
1807 case DW_AT_decl_line:
1808 var->line = attr.u.val;
1811 case DW_AT_external:
1812 if (attr.u.val != 0)
1816 case DW_AT_location:
1820 case DW_FORM_block1:
1821 case DW_FORM_block2:
1822 case DW_FORM_block4:
1823 if (*attr.u.blk->data == DW_OP_addr)
1827 /* Verify that DW_OP_addr is the only opcode in the
1828 location, in which case the block size will be 1
1829 plus the address size. */
1830 /* ??? For TLS variables, gcc can emit
1831 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
1832 which we don't handle here yet. */
1833 if (attr.u.blk->size == unit->addr_size + 1U)
1834 var->addr = bfd_get (unit->addr_size * 8,
1836 attr.u.blk->data + 1);
1851 if (func && high_pc != 0)
1853 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
1856 if (abbrev->has_children)
1860 if (nesting_level >= nested_funcs_size)
1862 struct funcinfo **tmp;
1864 nested_funcs_size *= 2;
1865 tmp = bfd_realloc (nested_funcs,
1867 * sizeof (struct funcinfo *)));
1870 free (nested_funcs);
1875 nested_funcs[nesting_level] = 0;
1879 free (nested_funcs);
1883 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1884 includes the compilation unit header that proceeds the DIE's, but
1885 does not include the length field that precedes each compilation
1886 unit header. END_PTR points one past the end of this comp unit.
1887 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1889 This routine does not read the whole compilation unit; only enough
1890 to get to the line number information for the compilation unit. */
1892 static struct comp_unit *
1893 parse_comp_unit (struct dwarf2_debug *stash,
1894 bfd_vma unit_length,
1895 bfd_byte *info_ptr_unit,
1896 unsigned int offset_size)
1898 struct comp_unit* unit;
1899 unsigned int version;
1900 bfd_uint64_t abbrev_offset = 0;
1901 unsigned int addr_size;
1902 struct abbrev_info** abbrevs;
1903 unsigned int abbrev_number, bytes_read, i;
1904 struct abbrev_info *abbrev;
1905 struct attribute attr;
1906 bfd_byte *info_ptr = stash->info_ptr;
1907 bfd_byte *end_ptr = info_ptr + unit_length;
1910 bfd_vma high_pc = 0;
1911 bfd *abfd = stash->bfd;
1913 version = read_2_bytes (abfd, info_ptr);
1915 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1916 if (offset_size == 4)
1917 abbrev_offset = read_4_bytes (abfd, info_ptr);
1919 abbrev_offset = read_8_bytes (abfd, info_ptr);
1920 info_ptr += offset_size;
1921 addr_size = read_1_byte (abfd, info_ptr);
1926 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1927 bfd_set_error (bfd_error_bad_value);
1931 if (addr_size > sizeof (bfd_vma))
1933 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1935 (unsigned int) sizeof (bfd_vma));
1936 bfd_set_error (bfd_error_bad_value);
1940 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1942 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1943 bfd_set_error (bfd_error_bad_value);
1947 /* Read the abbrevs for this compilation unit into a table. */
1948 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1952 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1953 info_ptr += bytes_read;
1954 if (! abbrev_number)
1956 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1958 bfd_set_error (bfd_error_bad_value);
1962 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1965 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1967 bfd_set_error (bfd_error_bad_value);
1971 amt = sizeof (struct comp_unit);
1972 unit = bfd_zalloc (abfd, amt);
1974 unit->addr_size = addr_size;
1975 unit->offset_size = offset_size;
1976 unit->abbrevs = abbrevs;
1977 unit->end_ptr = end_ptr;
1978 unit->stash = stash;
1979 unit->info_ptr_unit = info_ptr_unit;
1981 for (i = 0; i < abbrev->num_attrs; ++i)
1983 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1985 /* Store the data if it is of an attribute we want to keep in a
1986 partial symbol table. */
1989 case DW_AT_stmt_list:
1991 unit->line_offset = attr.u.val;
1995 unit->name = attr.u.str;
1999 low_pc = attr.u.val;
2000 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2001 this is the base address to use when reading location
2002 lists or range lists. */
2003 unit->base_address = low_pc;
2007 high_pc = attr.u.val;
2011 read_rangelist (unit, &unit->arange, attr.u.val);
2014 case DW_AT_comp_dir:
2016 char *comp_dir = attr.u.str;
2019 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2020 directory, get rid of it. */
2021 char *cp = strchr (comp_dir, ':');
2023 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2026 unit->comp_dir = comp_dir;
2036 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
2039 unit->first_child_die_ptr = info_ptr;
2043 /* Return TRUE if UNIT may contain the address given by ADDR. When
2044 there are functions written entirely with inline asm statements, the
2045 range info in the compilation unit header may not be correct. We
2046 need to consult the line info table to see if a compilation unit
2047 really contains the given address. */
2050 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2052 struct arange *arange;
2057 arange = &unit->arange;
2060 if (addr >= arange->low && addr < arange->high)
2062 arange = arange->next;
2069 /* If UNIT contains ADDR, set the output parameters to the values for
2070 the line containing ADDR. The output parameters, FILENAME_PTR,
2071 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2074 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2078 comp_unit_find_nearest_line (struct comp_unit *unit,
2080 const char **filename_ptr,
2081 const char **functionname_ptr,
2082 unsigned int *linenumber_ptr,
2083 struct dwarf2_debug *stash)
2087 struct funcinfo *function;
2092 if (! unit->line_table)
2094 if (! unit->stmtlist)
2100 unit->line_table = decode_line_info (unit, stash);
2102 if (! unit->line_table)
2108 if (unit->first_child_die_ptr < unit->end_ptr
2109 && ! scan_unit_for_symbols (unit))
2117 func_p = lookup_address_in_function_table (unit, addr,
2118 &function, functionname_ptr);
2119 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2120 stash->inliner_chain = function;
2121 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2122 function, filename_ptr,
2124 return line_p || func_p;
2127 /* If UNIT contains SYM at ADDR, set the output parameters to the
2128 values for the line containing SYM. The output parameters,
2129 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2132 Return TRUE if UNIT contains SYM, and no errors were encountered;
2136 comp_unit_find_line (struct comp_unit *unit,
2139 const char **filename_ptr,
2140 unsigned int *linenumber_ptr,
2141 struct dwarf2_debug *stash)
2146 if (! unit->line_table)
2148 if (! unit->stmtlist)
2154 unit->line_table = decode_line_info (unit, stash);
2156 if (! unit->line_table)
2162 if (unit->first_child_die_ptr < unit->end_ptr
2163 && ! scan_unit_for_symbols (unit))
2170 if (sym->flags & BSF_FUNCTION)
2171 return lookup_symbol_in_function_table (unit, sym, addr,
2175 return lookup_symbol_in_variable_table (unit, sym, addr,
2180 /* Locate a section in a BFD containing debugging info. The search starts
2181 from the section after AFTER_SEC, or from the first section in the BFD if
2182 AFTER_SEC is NULL. The search works by examining the names of the
2183 sections. There are two permissiable names. The first is .debug_info.
2184 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2185 This is a variation on the .debug_info section which has a checksum
2186 describing the contents appended onto the name. This allows the linker to
2187 identify and discard duplicate debugging sections for different
2188 compilation units. */
2189 #define DWARF2_DEBUG_INFO ".debug_info"
2190 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2193 find_debug_info (bfd *abfd, asection *after_sec)
2197 msec = after_sec != NULL ? after_sec->next : abfd->sections;
2201 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2204 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
2213 /* Unset vmas for loadable sections in STASH. */
2216 unset_sections (struct dwarf2_debug *stash)
2219 struct loadable_section *p;
2221 i = stash->loadable_section_count;
2222 p = stash->loadable_sections;
2223 for (; i > 0; i--, p++)
2224 p->section->vma = 0;
2227 /* Set unique vmas for loadable sections in ABFD and save vmas in
2228 STASH for unset_sections. */
2231 place_sections (bfd *abfd, struct dwarf2_debug *stash)
2233 struct loadable_section *p;
2236 if (stash->loadable_section_count != 0)
2238 i = stash->loadable_section_count;
2239 p = stash->loadable_sections;
2240 for (; i > 0; i--, p++)
2241 p->section->vma = p->adj_vma;
2246 bfd_vma last_vma = 0;
2248 struct loadable_section *p;
2251 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2255 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2258 sz = sect->rawsize ? sect->rawsize : sect->size;
2265 amt = i * sizeof (struct loadable_section);
2266 p = (struct loadable_section *) bfd_zalloc (abfd, amt);
2270 stash->loadable_sections = p;
2271 stash->loadable_section_count = i;
2273 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2277 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2280 sz = sect->rawsize ? sect->rawsize : sect->size;
2287 /* Align the new address to the current section
2289 last_vma = ((last_vma
2290 + ~((bfd_vma) -1 << sect->alignment_power))
2291 & ((bfd_vma) -1 << sect->alignment_power));
2292 sect->vma = last_vma;
2294 p->adj_vma = sect->vma;
2295 last_vma += sect->vma + sz;
2304 /* Find the source code location of SYMBOL. If SYMBOL is NULL
2305 then find the nearest source code location corresponding to
2306 the address SECTION + OFFSET.
2307 Returns TRUE if the line is found without error and fills in
2308 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
2309 NULL the FUNCTIONNAME_PTR is also filled in.
2310 SYMBOLS contains the symbol table for ABFD.
2311 ADDR_SIZE is the number of bytes in the initial .debug_info length
2312 field and in the abbreviation offset, or zero to indicate that the
2313 default value should be used. */
2316 find_line (bfd *abfd,
2321 const char **filename_ptr,
2322 const char **functionname_ptr,
2323 unsigned int *linenumber_ptr,
2324 unsigned int addr_size,
2327 /* Read each compilation unit from the section .debug_info, and check
2328 to see if it contains the address we are searching for. If yes,
2329 lookup the address, and return the line number info. If no, go
2330 on to the next compilation unit.
2332 We keep a list of all the previously read compilation units, and
2333 a pointer to the next un-read compilation unit. Check the
2334 previously read units before reading more. */
2335 struct dwarf2_debug *stash;
2336 /* What address are we looking for? */
2338 struct comp_unit* each;
2339 bfd_vma found = FALSE;
2340 bfd_boolean do_line;
2346 bfd_size_type amt = sizeof (struct dwarf2_debug);
2348 stash = bfd_zalloc (abfd, amt);
2353 /* In a relocatable file, 2 functions may have the same address.
2354 We change the section vma so that they won't overlap. */
2355 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2357 if (! place_sections (abfd, stash))
2361 do_line = (section == NULL
2363 && functionname_ptr == NULL
2367 addr = symbol->value;
2368 section = bfd_get_section (symbol);
2370 else if (section != NULL
2371 && functionname_ptr != NULL
2377 if (section->output_section)
2378 addr += section->output_section->vma + section->output_offset;
2380 addr += section->vma;
2381 *filename_ptr = NULL;
2382 *functionname_ptr = NULL;
2383 *linenumber_ptr = 0;
2388 bfd_size_type total_size;
2393 msec = find_debug_info (abfd, NULL);
2396 char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
2398 if (debug_filename == NULL)
2399 /* No dwarf2 info, and no gnu_debuglink to follow.
2400 Note that at this point the stash has been allocated, but
2401 contains zeros. This lets future calls to this function
2402 fail more quickly. */
2405 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
2406 || ! bfd_check_format (debug_bfd, bfd_object)
2407 || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
2410 bfd_close (debug_bfd);
2411 /* FIXME: Should we report our failure to follow the debuglink ? */
2412 free (debug_filename);
2419 /* There can be more than one DWARF2 info section in a BFD these days.
2420 Read them all in and produce one large stash. We do this in two
2421 passes - in the first pass we just accumulate the section sizes.
2422 In the second pass we read in the section's contents. The allows
2423 us to avoid reallocing the data as we add sections to the stash. */
2424 for (total_size = 0; msec; msec = find_debug_info (debug_bfd, msec))
2425 total_size += msec->size;
2427 stash->info_ptr = bfd_alloc (debug_bfd, total_size);
2428 if (stash->info_ptr == NULL)
2431 stash->info_ptr_end = stash->info_ptr;
2433 for (msec = find_debug_info (debug_bfd, NULL);
2435 msec = find_debug_info (debug_bfd, msec))
2438 bfd_size_type start;
2444 start = stash->info_ptr_end - stash->info_ptr;
2446 if ((bfd_simple_get_relocated_section_contents
2447 (debug_bfd, msec, stash->info_ptr + start, symbols)) == NULL)
2450 stash->info_ptr_end = stash->info_ptr + start + size;
2453 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2455 stash->sec = find_debug_info (debug_bfd, NULL);
2456 stash->sec_info_ptr = stash->info_ptr;
2457 stash->syms = symbols;
2458 stash->bfd = debug_bfd;
2461 /* A null info_ptr indicates that there is no dwarf2 info
2462 (or that an error occured while setting up the stash). */
2463 if (! stash->info_ptr)
2466 stash->inliner_chain = NULL;
2468 /* Check the previously read comp. units first. */
2469 for (each = stash->all_comp_units; each; each = each->next_unit)
2472 found = (((symbol->flags & BSF_FUNCTION) == 0
2473 || comp_unit_contains_address (each, addr))
2474 && comp_unit_find_line (each, symbol, addr,
2475 filename_ptr, linenumber_ptr,
2478 found = (comp_unit_contains_address (each, addr)
2479 && comp_unit_find_nearest_line (each, addr,
2488 /* The DWARF2 spec says that the initial length field, and the
2489 offset of the abbreviation table, should both be 4-byte values.
2490 However, some compilers do things differently. */
2493 BFD_ASSERT (addr_size == 4 || addr_size == 8);
2495 /* Read each remaining comp. units checking each as they are read. */
2496 while (stash->info_ptr < stash->info_ptr_end)
2499 unsigned int offset_size = addr_size;
2500 bfd_byte *info_ptr_unit = stash->info_ptr;
2502 length = read_4_bytes (stash->bfd, stash->info_ptr);
2503 /* A 0xffffff length is the DWARF3 way of indicating
2504 we use 64-bit offsets, instead of 32-bit offsets. */
2505 if (length == 0xffffffff)
2508 length = read_8_bytes (stash->bfd, stash->info_ptr + 4);
2509 stash->info_ptr += 12;
2511 /* A zero length is the IRIX way of indicating 64-bit offsets,
2512 mostly because the 64-bit length will generally fit in 32
2513 bits, and the endianness helps. */
2514 else if (length == 0)
2517 length = read_4_bytes (stash->bfd, stash->info_ptr + 4);
2518 stash->info_ptr += 8;
2520 /* In the absence of the hints above, we assume 32-bit DWARF2
2521 offsets even for targets with 64-bit addresses, because:
2522 a) most of the time these targets will not have generated
2523 more than 2Gb of debug info and so will not need 64-bit
2526 b) if they do use 64-bit offsets but they are not using
2527 the size hints that are tested for above then they are
2528 not conforming to the DWARF3 standard anyway. */
2529 else if (addr_size == 8)
2532 stash->info_ptr += 4;
2535 stash->info_ptr += 4;
2539 each = parse_comp_unit (stash, length, info_ptr_unit,
2541 stash->info_ptr += length;
2543 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
2544 == stash->sec->size)
2546 stash->sec = find_debug_info (stash->bfd, stash->sec);
2547 stash->sec_info_ptr = stash->info_ptr;
2552 each->next_unit = stash->all_comp_units;
2553 stash->all_comp_units = each;
2555 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2556 compilation units. If we don't have them (i.e.,
2557 unit->high == 0), we need to consult the line info
2558 table to see if a compilation unit contains the given
2561 found = (((symbol->flags & BSF_FUNCTION) == 0
2562 || each->arange.high == 0
2563 || comp_unit_contains_address (each, addr))
2564 && comp_unit_find_line (each, symbol, addr,
2569 found = ((each->arange.high == 0
2570 || comp_unit_contains_address (each, addr))
2571 && comp_unit_find_nearest_line (each, addr,
2583 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2584 unset_sections (stash);
2589 /* The DWARF2 version of find_nearest_line.
2590 Return TRUE if the line is found without error. */
2593 _bfd_dwarf2_find_nearest_line (bfd *abfd,
2597 const char **filename_ptr,
2598 const char **functionname_ptr,
2599 unsigned int *linenumber_ptr,
2600 unsigned int addr_size,
2603 return find_line (abfd, section, offset, NULL, symbols, filename_ptr,
2604 functionname_ptr, linenumber_ptr, addr_size,
2608 /* The DWARF2 version of find_line.
2609 Return TRUE if the line is found without error. */
2612 _bfd_dwarf2_find_line (bfd *abfd,
2615 const char **filename_ptr,
2616 unsigned int *linenumber_ptr,
2617 unsigned int addr_size,
2620 return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr,
2621 NULL, linenumber_ptr, addr_size,
2626 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
2627 const char **filename_ptr,
2628 const char **functionname_ptr,
2629 unsigned int *linenumber_ptr,
2632 struct dwarf2_debug *stash;
2637 struct funcinfo *func = stash->inliner_chain;
2639 if (func && func->caller_func)
2641 *filename_ptr = func->caller_file;
2642 *functionname_ptr = func->caller_func->name;
2643 *linenumber_ptr = func->caller_line;
2644 stash->inliner_chain = func->caller_func;
2653 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
2655 struct comp_unit *each;
2656 struct dwarf2_debug *stash;
2658 if (abfd == NULL || elf_tdata (abfd) == NULL)
2661 stash = elf_tdata (abfd)->dwarf2_find_line_info;
2666 for (each = stash->all_comp_units; each; each = each->next_unit)
2668 struct abbrev_info **abbrevs = each->abbrevs;
2671 for (i = 0; i < ABBREV_HASH_SIZE; i++)
2673 struct abbrev_info *abbrev = abbrevs[i];
2677 free (abbrev->attrs);
2678 abbrev = abbrev->next;
2682 if (each->line_table)
2684 free (each->line_table->dirs);
2685 free (each->line_table->files);
2689 free (stash->dwarf_abbrev_buffer);
2690 free (stash->dwarf_line_buffer);
2691 free (stash->dwarf_ranges_buffer);