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);
636 case DW_FORM_flag_present:
640 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
641 info_ptr += bytes_read;
644 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
645 info_ptr += bytes_read;
648 attr->u.val = read_1_byte (abfd, info_ptr);
652 attr->u.val = read_2_bytes (abfd, info_ptr);
656 attr->u.val = read_4_bytes (abfd, info_ptr);
660 attr->u.val = read_8_bytes (abfd, info_ptr);
663 case DW_FORM_ref_udata:
664 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
665 info_ptr += bytes_read;
667 case DW_FORM_indirect:
668 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
669 info_ptr += bytes_read;
670 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
673 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
675 bfd_set_error (bfd_error_bad_value);
680 /* Read an attribute described by an abbreviated attribute. */
683 read_attribute (struct attribute *attr,
684 struct attr_abbrev *abbrev,
685 struct comp_unit *unit,
688 attr->name = abbrev->name;
689 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
693 /* Source line information table routines. */
695 #define FILE_ALLOC_CHUNK 5
696 #define DIR_ALLOC_CHUNK 5
700 struct line_info* prev_line;
705 int end_sequence; /* End of (sequential) code sequence. */
716 struct line_info_table
719 unsigned int num_files;
720 unsigned int num_dirs;
723 struct fileinfo* files;
724 struct line_info* last_line; /* largest VMA */
725 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
728 /* Remember some information about each function. If the function is
729 inlined (DW_TAG_inlined_subroutine) it may have two additional
730 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
731 source code location where this function was inlined. */
735 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */
736 struct funcinfo *caller_func; /* Pointer to function one scope higher */
737 char *caller_file; /* Source location file name where caller_func inlines this func */
738 int caller_line; /* Source location line number where caller_func inlines this func */
739 char *file; /* Source location file name */
740 int line; /* Source location line number */
743 struct arange arange;
744 asection *sec; /* Where the symbol is defined */
749 /* Pointer to previous variable in list of all variables */
750 struct varinfo *prev_var;
751 /* Source location file name */
753 /* Source location line number */
758 /* Where the symbol is defined */
760 /* Is this a stack variable? */
761 unsigned int stack: 1;
764 /* Return TRUE if NEW_LINE should sort after LINE. */
766 static inline bfd_boolean
767 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
769 return (new_line->address > line->address
770 || (new_line->address == line->address
771 && new_line->end_sequence < line->end_sequence));
775 /* Adds a new entry to the line_info list in the line_info_table, ensuring
776 that the list is sorted. Note that the line_info list is sorted from
777 highest to lowest VMA (with possible duplicates); that is,
778 line_info->prev_line always accesses an equal or smaller VMA. */
781 add_line_info (struct line_info_table *table,
788 bfd_size_type amt = sizeof (struct line_info);
789 struct line_info* info = bfd_alloc (table->abfd, amt);
791 /* Set member data of 'info'. */
792 info->address = address;
794 info->column = column;
795 info->end_sequence = end_sequence;
797 if (filename && filename[0])
799 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
801 strcpy (info->filename, filename);
804 info->filename = NULL;
806 /* Find the correct location for 'info'. Normally we will receive
807 new line_info data 1) in order and 2) with increasing VMAs.
808 However some compilers break the rules (cf. decode_line_info) and
809 so we include some heuristics for quickly finding the correct
810 location for 'info'. In particular, these heuristics optimize for
811 the common case in which the VMA sequence that we receive is a
812 list of locally sorted VMAs such as
813 p...z a...j (where a < j < p < z)
815 Note: table->lcl_head is used to head an *actual* or *possible*
816 sequence within the list (such as a...j) that is not directly
817 headed by table->last_line
819 Note: we may receive duplicate entries from 'decode_line_info'. */
821 if (!table->last_line
822 || new_line_sorts_after (info, table->last_line))
824 /* Normal case: add 'info' to the beginning of the list */
825 info->prev_line = table->last_line;
826 table->last_line = info;
828 /* lcl_head: initialize to head a *possible* sequence at the end. */
829 if (!table->lcl_head)
830 table->lcl_head = info;
832 else if (!new_line_sorts_after (info, table->lcl_head)
833 && (!table->lcl_head->prev_line
834 || new_line_sorts_after (info, table->lcl_head->prev_line)))
836 /* Abnormal but easy: lcl_head is the head of 'info'. */
837 info->prev_line = table->lcl_head->prev_line;
838 table->lcl_head->prev_line = info;
842 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
843 heads for 'info'. Reset 'lcl_head'. */
844 struct line_info* li2 = table->last_line; /* always non-NULL */
845 struct line_info* li1 = li2->prev_line;
849 if (!new_line_sorts_after (info, li2)
850 && new_line_sorts_after (info, li1))
853 li2 = li1; /* always non-NULL */
854 li1 = li1->prev_line;
856 table->lcl_head = li2;
857 info->prev_line = table->lcl_head->prev_line;
858 table->lcl_head->prev_line = info;
862 /* Extract a fully qualified filename from a line info table.
863 The returned string has been malloc'ed and it is the caller's
864 responsibility to free it. */
867 concat_filename (struct line_info_table *table, unsigned int file)
871 if (file - 1 >= table->num_files)
873 /* FILE == 0 means unknown. */
875 (*_bfd_error_handler)
876 (_("Dwarf Error: mangled line number section (bad file number)."));
877 return strdup ("<unknown>");
880 filename = table->files[file - 1].name;
882 if (!IS_ABSOLUTE_PATH (filename))
884 char *dirname = NULL;
885 char *subdirname = NULL;
889 if (table->files[file - 1].dir)
890 subdirname = table->dirs[table->files[file - 1].dir - 1];
892 if (!subdirname || !IS_ABSOLUTE_PATH (subdirname))
893 dirname = table->comp_dir;
897 dirname = subdirname;
902 return strdup (filename);
904 len = strlen (dirname) + strlen (filename) + 2;
908 len += strlen (subdirname) + 1;
909 name = bfd_malloc (len);
911 sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
915 name = bfd_malloc (len);
917 sprintf (name, "%s/%s", dirname, filename);
923 return strdup (filename);
927 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
929 struct arange *arange;
931 /* If the first arange is empty, use it. */
932 if (first_arange->high == 0)
934 first_arange->low = low_pc;
935 first_arange->high = high_pc;
939 /* Next see if we can cheaply extend an existing range. */
940 arange = first_arange;
943 if (low_pc == arange->high)
945 arange->high = high_pc;
948 if (high_pc == arange->low)
950 arange->low = low_pc;
953 arange = arange->next;
957 /* Need to allocate a new arange and insert it into the arange list.
958 Order isn't significant, so just insert after the first arange. */
959 arange = bfd_zalloc (abfd, sizeof (*arange));
960 arange->low = low_pc;
961 arange->high = high_pc;
962 arange->next = first_arange->next;
963 first_arange->next = arange;
966 /* Decode the line number information for UNIT. */
968 static struct line_info_table*
969 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
971 bfd *abfd = unit->abfd;
972 struct line_info_table* table;
976 unsigned int i, bytes_read, offset_size;
977 char *cur_file, *cur_dir;
978 unsigned char op_code, extended_op, adj_opcode;
981 if (! stash->dwarf_line_buffer)
985 msec = bfd_get_section_by_name (abfd, ".debug_line");
988 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
989 bfd_set_error (bfd_error_bad_value);
993 stash->dwarf_line_size = msec->size;
994 stash->dwarf_line_buffer
995 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
997 if (! stash->dwarf_line_buffer)
1001 /* It is possible to get a bad value for the line_offset. Validate
1002 it here so that we won't get a segfault below. */
1003 if (unit->line_offset >= stash->dwarf_line_size)
1005 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
1006 unit->line_offset, stash->dwarf_line_size);
1007 bfd_set_error (bfd_error_bad_value);
1011 amt = sizeof (struct line_info_table);
1012 table = bfd_alloc (abfd, amt);
1014 table->comp_dir = unit->comp_dir;
1016 table->num_files = 0;
1017 table->files = NULL;
1019 table->num_dirs = 0;
1022 table->files = NULL;
1023 table->last_line = NULL;
1024 table->lcl_head = NULL;
1026 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1028 /* Read in the prologue. */
1029 lh.total_length = read_4_bytes (abfd, line_ptr);
1032 if (lh.total_length == 0xffffffff)
1034 lh.total_length = read_8_bytes (abfd, line_ptr);
1038 else if (lh.total_length == 0 && unit->addr_size == 8)
1040 /* Handle (non-standard) 64-bit DWARF2 formats. */
1041 lh.total_length = read_4_bytes (abfd, line_ptr);
1045 line_end = line_ptr + lh.total_length;
1046 lh.version = read_2_bytes (abfd, line_ptr);
1048 if (offset_size == 4)
1049 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1051 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1052 line_ptr += offset_size;
1053 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1055 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1057 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1059 lh.line_range = read_1_byte (abfd, line_ptr);
1061 lh.opcode_base = read_1_byte (abfd, line_ptr);
1063 amt = lh.opcode_base * sizeof (unsigned char);
1064 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
1066 lh.standard_opcode_lengths[0] = 1;
1068 for (i = 1; i < lh.opcode_base; ++i)
1070 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1074 /* Read directory table. */
1075 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1077 line_ptr += bytes_read;
1079 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1083 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1084 amt *= sizeof (char *);
1086 tmp = bfd_realloc (table->dirs, amt);
1095 table->dirs[table->num_dirs++] = cur_dir;
1098 line_ptr += bytes_read;
1100 /* Read file name table. */
1101 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1103 line_ptr += bytes_read;
1105 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1107 struct fileinfo *tmp;
1109 amt = table->num_files + FILE_ALLOC_CHUNK;
1110 amt *= sizeof (struct fileinfo);
1112 tmp = bfd_realloc (table->files, amt);
1115 free (table->files);
1122 table->files[table->num_files].name = cur_file;
1123 table->files[table->num_files].dir =
1124 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1125 line_ptr += bytes_read;
1126 table->files[table->num_files].time =
1127 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1128 line_ptr += bytes_read;
1129 table->files[table->num_files].size =
1130 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1131 line_ptr += bytes_read;
1135 line_ptr += bytes_read;
1137 /* Read the statement sequences until there's nothing left. */
1138 while (line_ptr < line_end)
1140 /* State machine registers. */
1141 bfd_vma address = 0;
1142 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1143 unsigned int line = 1;
1144 unsigned int column = 0;
1145 int is_stmt = lh.default_is_stmt;
1146 int end_sequence = 0;
1147 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1148 compilers generate address sequences that are wildly out of
1149 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1150 for ia64-Linux). Thus, to determine the low and high
1151 address, we must compare on every DW_LNS_copy, etc. */
1152 bfd_vma low_pc = (bfd_vma) -1;
1153 bfd_vma high_pc = 0;
1155 /* Decode the table. */
1156 while (! end_sequence)
1158 op_code = read_1_byte (abfd, line_ptr);
1161 if (op_code >= lh.opcode_base)
1163 /* Special operand. */
1164 adj_opcode = op_code - lh.opcode_base;
1165 address += (adj_opcode / lh.line_range)
1166 * lh.minimum_instruction_length;
1167 line += lh.line_base + (adj_opcode % lh.line_range);
1168 /* Append row to matrix using current values. */
1169 add_line_info (table, address, filename, line, column, 0);
1170 if (address < low_pc)
1172 if (address > high_pc)
1175 else switch (op_code)
1177 case DW_LNS_extended_op:
1178 /* Ignore length. */
1180 extended_op = read_1_byte (abfd, line_ptr);
1183 switch (extended_op)
1185 case DW_LNE_end_sequence:
1187 add_line_info (table, address, filename, line, column,
1189 if (address < low_pc)
1191 if (address > high_pc)
1193 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1195 case DW_LNE_set_address:
1196 address = read_address (unit, line_ptr);
1197 line_ptr += unit->addr_size;
1199 case DW_LNE_define_file:
1200 cur_file = read_string (abfd, line_ptr, &bytes_read);
1201 line_ptr += bytes_read;
1202 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1204 struct fileinfo *tmp;
1206 amt = table->num_files + FILE_ALLOC_CHUNK;
1207 amt *= sizeof (struct fileinfo);
1208 tmp = bfd_realloc (table->files, amt);
1211 free (table->files);
1218 table->files[table->num_files].name = cur_file;
1219 table->files[table->num_files].dir =
1220 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1221 line_ptr += bytes_read;
1222 table->files[table->num_files].time =
1223 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1224 line_ptr += bytes_read;
1225 table->files[table->num_files].size =
1226 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1227 line_ptr += bytes_read;
1231 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1232 bfd_set_error (bfd_error_bad_value);
1234 free (table->files);
1240 add_line_info (table, address, filename, line, column, 0);
1241 if (address < low_pc)
1243 if (address > high_pc)
1246 case DW_LNS_advance_pc:
1247 address += lh.minimum_instruction_length
1248 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1249 line_ptr += bytes_read;
1251 case DW_LNS_advance_line:
1252 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1253 line_ptr += bytes_read;
1255 case DW_LNS_set_file:
1259 /* The file and directory tables are 0
1260 based, the references are 1 based. */
1261 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1262 line_ptr += bytes_read;
1265 filename = concat_filename (table, file);
1268 case DW_LNS_set_column:
1269 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1270 line_ptr += bytes_read;
1272 case DW_LNS_negate_stmt:
1273 is_stmt = (!is_stmt);
1275 case DW_LNS_set_basic_block:
1277 case DW_LNS_const_add_pc:
1278 address += lh.minimum_instruction_length
1279 * ((255 - lh.opcode_base) / lh.line_range);
1281 case DW_LNS_fixed_advance_pc:
1282 address += read_2_bytes (abfd, line_ptr);
1289 /* Unknown standard opcode, ignore it. */
1290 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1292 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1293 line_ptr += bytes_read;
1306 /* If ADDR is within TABLE set the output parameters and return TRUE,
1307 otherwise return FALSE. The output parameters, FILENAME_PTR and
1308 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1311 lookup_address_in_line_info_table (struct line_info_table *table,
1313 struct funcinfo *function,
1314 const char **filename_ptr,
1315 unsigned int *linenumber_ptr)
1317 /* Note: table->last_line should be a descendingly sorted list. */
1318 struct line_info* next_line = table->last_line;
1319 struct line_info* each_line = NULL;
1320 *filename_ptr = NULL;
1325 each_line = next_line->prev_line;
1327 /* Check for large addresses */
1328 if (addr > next_line->address)
1329 each_line = NULL; /* ensure we skip over the normal case */
1331 /* Normal case: search the list; save */
1332 while (each_line && next_line)
1334 /* If we have an address match, save this info. This allows us
1335 to return as good as results as possible for strange debugging
1337 bfd_boolean addr_match = FALSE;
1338 if (each_line->address <= addr && addr < next_line->address)
1342 /* If this line appears to span functions, and addr is in the
1343 later function, return the first line of that function instead
1344 of the last line of the earlier one. This check is for GCC
1345 2.95, which emits the first line number for a function late. */
1347 if (function != NULL)
1350 struct arange *arange;
1352 /* Find the lowest address in the function's range list */
1353 lowest_pc = function->arange.low;
1354 for (arange = &function->arange;
1356 arange = arange->next)
1358 if (function->arange.low < lowest_pc)
1359 lowest_pc = function->arange.low;
1361 /* Check for spanning function and set outgoing line info */
1362 if (addr >= lowest_pc
1363 && each_line->address < lowest_pc
1364 && next_line->address > lowest_pc)
1366 *filename_ptr = next_line->filename;
1367 *linenumber_ptr = next_line->line;
1371 *filename_ptr = each_line->filename;
1372 *linenumber_ptr = each_line->line;
1377 *filename_ptr = each_line->filename;
1378 *linenumber_ptr = each_line->line;
1382 if (addr_match && !each_line->end_sequence)
1383 return TRUE; /* we have definitely found what we want */
1385 next_line = each_line;
1386 each_line = each_line->prev_line;
1389 /* At this point each_line is NULL but next_line is not. If we found
1390 a candidate end-of-sequence point in the loop above, we can return
1391 that (compatibility with a bug in the Intel compiler); otherwise,
1392 assuming that we found the containing function for this address in
1393 this compilation unit, return the first line we have a number for
1394 (compatibility with GCC 2.95). */
1395 if (*filename_ptr == NULL && function != NULL)
1397 *filename_ptr = next_line->filename;
1398 *linenumber_ptr = next_line->line;
1405 /* Read in the .debug_ranges section for future reference */
1408 read_debug_ranges (struct comp_unit *unit)
1410 struct dwarf2_debug *stash = unit->stash;
1411 if (! stash->dwarf_ranges_buffer)
1413 bfd *abfd = unit->abfd;
1416 msec = bfd_get_section_by_name (abfd, ".debug_ranges");
1419 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
1420 bfd_set_error (bfd_error_bad_value);
1424 stash->dwarf_ranges_size = msec->size;
1425 stash->dwarf_ranges_buffer
1426 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1428 if (! stash->dwarf_ranges_buffer)
1434 /* Function table functions. */
1436 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1437 Note that we need to find the function that has the smallest
1438 range that contains ADDR, to handle inlined functions without
1439 depending upon them being ordered in TABLE by increasing range. */
1442 lookup_address_in_function_table (struct comp_unit *unit,
1444 struct funcinfo **function_ptr,
1445 const char **functionname_ptr)
1447 struct funcinfo* each_func;
1448 struct funcinfo* best_fit = NULL;
1449 struct arange *arange;
1451 for (each_func = unit->function_table;
1453 each_func = each_func->prev_func)
1455 for (arange = &each_func->arange;
1457 arange = arange->next)
1459 if (addr >= arange->low && addr < arange->high)
1462 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1463 best_fit = each_func;
1470 *functionname_ptr = best_fit->name;
1471 *function_ptr = best_fit;
1480 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1481 and LINENUMBER_PTR, and return TRUE. */
1484 lookup_symbol_in_function_table (struct comp_unit *unit,
1487 const char **filename_ptr,
1488 unsigned int *linenumber_ptr)
1490 struct funcinfo* each_func;
1491 struct funcinfo* best_fit = NULL;
1492 struct arange *arange;
1493 const char *name = bfd_asymbol_name (sym);
1494 asection *sec = bfd_get_section (sym);
1496 for (each_func = unit->function_table;
1498 each_func = each_func->prev_func)
1500 for (arange = &each_func->arange;
1502 arange = arange->next)
1504 if ((!each_func->sec || each_func->sec == sec)
1505 && addr >= arange->low
1506 && addr < arange->high
1508 && strcmp (name, each_func->name) == 0
1510 || ((arange->high - arange->low)
1511 < (best_fit->arange.high - best_fit->arange.low))))
1512 best_fit = each_func;
1518 best_fit->sec = sec;
1519 *filename_ptr = best_fit->file;
1520 *linenumber_ptr = best_fit->line;
1527 /* Variable table functions. */
1529 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1530 LINENUMBER_PTR, and return TRUE. */
1533 lookup_symbol_in_variable_table (struct comp_unit *unit,
1536 const char **filename_ptr,
1537 unsigned int *linenumber_ptr)
1539 const char *name = bfd_asymbol_name (sym);
1540 asection *sec = bfd_get_section (sym);
1541 struct varinfo* each;
1543 for (each = unit->variable_table; each; each = each->prev_var)
1544 if (each->stack == 0
1545 && each->file != NULL
1546 && each->name != NULL
1547 && each->addr == addr
1548 && (!each->sec || each->sec == sec)
1549 && strcmp (name, each->name) == 0)
1555 *filename_ptr = each->file;
1556 *linenumber_ptr = each->line;
1564 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1566 bfd *abfd = unit->abfd;
1568 unsigned int abbrev_number, bytes_read, i;
1569 struct abbrev_info *abbrev;
1570 struct attribute attr;
1573 info_ptr = unit->info_ptr_unit + die_ref;
1574 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1575 info_ptr += bytes_read;
1579 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1582 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1584 bfd_set_error (bfd_error_bad_value);
1588 for (i = 0; i < abbrev->num_attrs; ++i)
1590 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1594 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1598 case DW_AT_specification:
1599 name = find_abstract_instance_name (unit, attr.u.val);
1601 case DW_AT_MIPS_linkage_name:
1614 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1616 bfd_byte *ranges_ptr;
1617 bfd_vma base_address = unit->base_address;
1619 if (! unit->stash->dwarf_ranges_buffer)
1621 if (! read_debug_ranges (unit))
1624 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1631 if (unit->addr_size == 4)
1633 low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1635 high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1640 low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1642 high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1645 if (low_pc == 0 && high_pc == 0)
1647 if (low_pc == -1UL && high_pc != -1UL)
1648 base_address = high_pc;
1650 arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1654 /* DWARF2 Compilation unit functions. */
1656 /* Scan over each die in a comp. unit looking for functions to add
1657 to the function table and variables to the variable table. */
1660 scan_unit_for_symbols (struct comp_unit *unit)
1662 bfd *abfd = unit->abfd;
1663 bfd_byte *info_ptr = unit->first_child_die_ptr;
1664 int nesting_level = 1;
1665 struct funcinfo **nested_funcs;
1666 int nested_funcs_size;
1668 /* Maintain a stack of in-scope functions and inlined functions, which we
1669 can use to set the caller_func field. */
1670 nested_funcs_size = 32;
1671 nested_funcs = bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
1672 if (nested_funcs == NULL)
1674 nested_funcs[nesting_level] = 0;
1676 while (nesting_level)
1678 unsigned int abbrev_number, bytes_read, i;
1679 struct abbrev_info *abbrev;
1680 struct attribute attr;
1681 struct funcinfo *func;
1682 struct varinfo *var;
1684 bfd_vma high_pc = 0;
1686 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1687 info_ptr += bytes_read;
1689 if (! abbrev_number)
1695 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1698 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1700 bfd_set_error (bfd_error_bad_value);
1701 free (nested_funcs);
1706 if (abbrev->tag == DW_TAG_subprogram
1707 || abbrev->tag == DW_TAG_entry_point
1708 || abbrev->tag == DW_TAG_inlined_subroutine)
1710 bfd_size_type amt = sizeof (struct funcinfo);
1711 func = bfd_zalloc (abfd, amt);
1712 func->tag = abbrev->tag;
1713 func->prev_func = unit->function_table;
1714 unit->function_table = func;
1716 if (func->tag == DW_TAG_inlined_subroutine)
1717 for (i = nesting_level - 1; i >= 1; i--)
1718 if (nested_funcs[i])
1720 func->caller_func = nested_funcs[i];
1723 nested_funcs[nesting_level] = func;
1728 if (abbrev->tag == DW_TAG_variable)
1730 bfd_size_type amt = sizeof (struct varinfo);
1731 var = bfd_zalloc (abfd, amt);
1732 var->tag = abbrev->tag;
1734 var->prev_var = unit->variable_table;
1735 unit->variable_table = var;
1738 /* No inline function in scope at this nesting level. */
1739 nested_funcs[nesting_level] = 0;
1742 for (i = 0; i < abbrev->num_attrs; ++i)
1744 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1750 case DW_AT_call_file:
1751 func->caller_file = concat_filename (unit->line_table, attr.u.val);
1754 case DW_AT_call_line:
1755 func->caller_line = attr.u.val;
1758 case DW_AT_abstract_origin:
1759 func->name = find_abstract_instance_name (unit, attr.u.val);
1763 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1764 if (func->name == NULL)
1765 func->name = attr.u.str;
1768 case DW_AT_MIPS_linkage_name:
1769 func->name = attr.u.str;
1773 low_pc = attr.u.val;
1777 high_pc = attr.u.val;
1781 read_rangelist (unit, &func->arange, attr.u.val);
1784 case DW_AT_decl_file:
1785 func->file = concat_filename (unit->line_table,
1789 case DW_AT_decl_line:
1790 func->line = attr.u.val;
1802 var->name = attr.u.str;
1805 case DW_AT_decl_file:
1806 var->file = concat_filename (unit->line_table,
1810 case DW_AT_decl_line:
1811 var->line = attr.u.val;
1814 case DW_AT_external:
1815 if (attr.u.val != 0)
1819 case DW_AT_location:
1823 case DW_FORM_block1:
1824 case DW_FORM_block2:
1825 case DW_FORM_block4:
1826 if (*attr.u.blk->data == DW_OP_addr)
1830 /* Verify that DW_OP_addr is the only opcode in the
1831 location, in which case the block size will be 1
1832 plus the address size. */
1833 /* ??? For TLS variables, gcc can emit
1834 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
1835 which we don't handle here yet. */
1836 if (attr.u.blk->size == unit->addr_size + 1U)
1837 var->addr = bfd_get (unit->addr_size * 8,
1839 attr.u.blk->data + 1);
1854 if (func && high_pc != 0)
1856 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
1859 if (abbrev->has_children)
1863 if (nesting_level >= nested_funcs_size)
1865 struct funcinfo **tmp;
1867 nested_funcs_size *= 2;
1868 tmp = bfd_realloc (nested_funcs,
1870 * sizeof (struct funcinfo *)));
1873 free (nested_funcs);
1878 nested_funcs[nesting_level] = 0;
1882 free (nested_funcs);
1886 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1887 includes the compilation unit header that proceeds the DIE's, but
1888 does not include the length field that precedes each compilation
1889 unit header. END_PTR points one past the end of this comp unit.
1890 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1892 This routine does not read the whole compilation unit; only enough
1893 to get to the line number information for the compilation unit. */
1895 static struct comp_unit *
1896 parse_comp_unit (struct dwarf2_debug *stash,
1897 bfd_vma unit_length,
1898 bfd_byte *info_ptr_unit,
1899 unsigned int offset_size)
1901 struct comp_unit* unit;
1902 unsigned int version;
1903 bfd_uint64_t abbrev_offset = 0;
1904 unsigned int addr_size;
1905 struct abbrev_info** abbrevs;
1906 unsigned int abbrev_number, bytes_read, i;
1907 struct abbrev_info *abbrev;
1908 struct attribute attr;
1909 bfd_byte *info_ptr = stash->info_ptr;
1910 bfd_byte *end_ptr = info_ptr + unit_length;
1913 bfd_vma high_pc = 0;
1914 bfd *abfd = stash->bfd;
1916 version = read_2_bytes (abfd, info_ptr);
1918 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1919 if (offset_size == 4)
1920 abbrev_offset = read_4_bytes (abfd, info_ptr);
1922 abbrev_offset = read_8_bytes (abfd, info_ptr);
1923 info_ptr += offset_size;
1924 addr_size = read_1_byte (abfd, info_ptr);
1929 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1930 bfd_set_error (bfd_error_bad_value);
1934 if (addr_size > sizeof (bfd_vma))
1936 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1938 (unsigned int) sizeof (bfd_vma));
1939 bfd_set_error (bfd_error_bad_value);
1943 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1945 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1946 bfd_set_error (bfd_error_bad_value);
1950 /* Read the abbrevs for this compilation unit into a table. */
1951 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1955 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1956 info_ptr += bytes_read;
1957 if (! abbrev_number)
1959 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1961 bfd_set_error (bfd_error_bad_value);
1965 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1968 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1970 bfd_set_error (bfd_error_bad_value);
1974 amt = sizeof (struct comp_unit);
1975 unit = bfd_zalloc (abfd, amt);
1977 unit->addr_size = addr_size;
1978 unit->offset_size = offset_size;
1979 unit->abbrevs = abbrevs;
1980 unit->end_ptr = end_ptr;
1981 unit->stash = stash;
1982 unit->info_ptr_unit = info_ptr_unit;
1984 for (i = 0; i < abbrev->num_attrs; ++i)
1986 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1988 /* Store the data if it is of an attribute we want to keep in a
1989 partial symbol table. */
1992 case DW_AT_stmt_list:
1994 unit->line_offset = attr.u.val;
1998 unit->name = attr.u.str;
2002 low_pc = attr.u.val;
2003 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2004 this is the base address to use when reading location
2005 lists or range lists. */
2006 unit->base_address = low_pc;
2010 high_pc = attr.u.val;
2014 read_rangelist (unit, &unit->arange, attr.u.val);
2017 case DW_AT_comp_dir:
2019 char *comp_dir = attr.u.str;
2022 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2023 directory, get rid of it. */
2024 char *cp = strchr (comp_dir, ':');
2026 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2029 unit->comp_dir = comp_dir;
2039 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
2042 unit->first_child_die_ptr = info_ptr;
2046 /* Return TRUE if UNIT may contain the address given by ADDR. When
2047 there are functions written entirely with inline asm statements, the
2048 range info in the compilation unit header may not be correct. We
2049 need to consult the line info table to see if a compilation unit
2050 really contains the given address. */
2053 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2055 struct arange *arange;
2060 arange = &unit->arange;
2063 if (addr >= arange->low && addr < arange->high)
2065 arange = arange->next;
2072 /* If UNIT contains ADDR, set the output parameters to the values for
2073 the line containing ADDR. The output parameters, FILENAME_PTR,
2074 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2077 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2081 comp_unit_find_nearest_line (struct comp_unit *unit,
2083 const char **filename_ptr,
2084 const char **functionname_ptr,
2085 unsigned int *linenumber_ptr,
2086 struct dwarf2_debug *stash)
2090 struct funcinfo *function;
2095 if (! unit->line_table)
2097 if (! unit->stmtlist)
2103 unit->line_table = decode_line_info (unit, stash);
2105 if (! unit->line_table)
2111 if (unit->first_child_die_ptr < unit->end_ptr
2112 && ! scan_unit_for_symbols (unit))
2120 func_p = lookup_address_in_function_table (unit, addr,
2121 &function, functionname_ptr);
2122 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2123 stash->inliner_chain = function;
2124 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2125 function, filename_ptr,
2127 return line_p || func_p;
2130 /* If UNIT contains SYM at ADDR, set the output parameters to the
2131 values for the line containing SYM. The output parameters,
2132 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2135 Return TRUE if UNIT contains SYM, and no errors were encountered;
2139 comp_unit_find_line (struct comp_unit *unit,
2142 const char **filename_ptr,
2143 unsigned int *linenumber_ptr,
2144 struct dwarf2_debug *stash)
2149 if (! unit->line_table)
2151 if (! unit->stmtlist)
2157 unit->line_table = decode_line_info (unit, stash);
2159 if (! unit->line_table)
2165 if (unit->first_child_die_ptr < unit->end_ptr
2166 && ! scan_unit_for_symbols (unit))
2173 if (sym->flags & BSF_FUNCTION)
2174 return lookup_symbol_in_function_table (unit, sym, addr,
2178 return lookup_symbol_in_variable_table (unit, sym, addr,
2183 /* Locate a section in a BFD containing debugging info. The search starts
2184 from the section after AFTER_SEC, or from the first section in the BFD if
2185 AFTER_SEC is NULL. The search works by examining the names of the
2186 sections. There are two permissiable names. The first is .debug_info.
2187 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2188 This is a variation on the .debug_info section which has a checksum
2189 describing the contents appended onto the name. This allows the linker to
2190 identify and discard duplicate debugging sections for different
2191 compilation units. */
2192 #define DWARF2_DEBUG_INFO ".debug_info"
2193 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2196 find_debug_info (bfd *abfd, asection *after_sec)
2200 msec = after_sec != NULL ? after_sec->next : abfd->sections;
2204 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2207 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
2216 /* Unset vmas for loadable sections in STASH. */
2219 unset_sections (struct dwarf2_debug *stash)
2222 struct loadable_section *p;
2224 i = stash->loadable_section_count;
2225 p = stash->loadable_sections;
2226 for (; i > 0; i--, p++)
2227 p->section->vma = 0;
2230 /* Set unique vmas for loadable sections in ABFD and save vmas in
2231 STASH for unset_sections. */
2234 place_sections (bfd *abfd, struct dwarf2_debug *stash)
2236 struct loadable_section *p;
2239 if (stash->loadable_section_count != 0)
2241 i = stash->loadable_section_count;
2242 p = stash->loadable_sections;
2243 for (; i > 0; i--, p++)
2244 p->section->vma = p->adj_vma;
2249 bfd_vma last_vma = 0;
2251 struct loadable_section *p;
2254 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2258 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2261 sz = sect->rawsize ? sect->rawsize : sect->size;
2268 amt = i * sizeof (struct loadable_section);
2269 p = (struct loadable_section *) bfd_zalloc (abfd, amt);
2273 stash->loadable_sections = p;
2274 stash->loadable_section_count = i;
2276 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2280 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2283 sz = sect->rawsize ? sect->rawsize : sect->size;
2290 /* Align the new address to the current section
2292 last_vma = ((last_vma
2293 + ~((bfd_vma) -1 << sect->alignment_power))
2294 & ((bfd_vma) -1 << sect->alignment_power));
2295 sect->vma = last_vma;
2297 p->adj_vma = sect->vma;
2298 last_vma += sect->vma + sz;
2307 /* Find the source code location of SYMBOL. If SYMBOL is NULL
2308 then find the nearest source code location corresponding to
2309 the address SECTION + OFFSET.
2310 Returns TRUE if the line is found without error and fills in
2311 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
2312 NULL the FUNCTIONNAME_PTR is also filled in.
2313 SYMBOLS contains the symbol table for ABFD.
2314 ADDR_SIZE is the number of bytes in the initial .debug_info length
2315 field and in the abbreviation offset, or zero to indicate that the
2316 default value should be used. */
2319 find_line (bfd *abfd,
2324 const char **filename_ptr,
2325 const char **functionname_ptr,
2326 unsigned int *linenumber_ptr,
2327 unsigned int addr_size,
2330 /* Read each compilation unit from the section .debug_info, and check
2331 to see if it contains the address we are searching for. If yes,
2332 lookup the address, and return the line number info. If no, go
2333 on to the next compilation unit.
2335 We keep a list of all the previously read compilation units, and
2336 a pointer to the next un-read compilation unit. Check the
2337 previously read units before reading more. */
2338 struct dwarf2_debug *stash;
2339 /* What address are we looking for? */
2341 struct comp_unit* each;
2342 bfd_vma found = FALSE;
2343 bfd_boolean do_line;
2349 bfd_size_type amt = sizeof (struct dwarf2_debug);
2351 stash = bfd_zalloc (abfd, amt);
2356 /* In a relocatable file, 2 functions may have the same address.
2357 We change the section vma so that they won't overlap. */
2358 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2360 if (! place_sections (abfd, stash))
2364 do_line = (section == NULL
2366 && functionname_ptr == NULL
2370 addr = symbol->value;
2371 section = bfd_get_section (symbol);
2373 else if (section != NULL
2374 && functionname_ptr != NULL
2380 if (section->output_section)
2381 addr += section->output_section->vma + section->output_offset;
2383 addr += section->vma;
2384 *filename_ptr = NULL;
2385 *functionname_ptr = NULL;
2386 *linenumber_ptr = 0;
2391 bfd_size_type total_size;
2396 msec = find_debug_info (abfd, NULL);
2399 char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
2401 if (debug_filename == NULL)
2402 /* No dwarf2 info, and no gnu_debuglink to follow.
2403 Note that at this point the stash has been allocated, but
2404 contains zeros. This lets future calls to this function
2405 fail more quickly. */
2408 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
2409 || ! bfd_check_format (debug_bfd, bfd_object)
2410 || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
2413 bfd_close (debug_bfd);
2414 /* FIXME: Should we report our failure to follow the debuglink ? */
2415 free (debug_filename);
2422 /* There can be more than one DWARF2 info section in a BFD these days.
2423 Read them all in and produce one large stash. We do this in two
2424 passes - in the first pass we just accumulate the section sizes.
2425 In the second pass we read in the section's contents. The allows
2426 us to avoid reallocing the data as we add sections to the stash. */
2427 for (total_size = 0; msec; msec = find_debug_info (debug_bfd, msec))
2428 total_size += msec->size;
2430 stash->info_ptr = bfd_alloc (debug_bfd, total_size);
2431 if (stash->info_ptr == NULL)
2434 stash->info_ptr_end = stash->info_ptr;
2436 for (msec = find_debug_info (debug_bfd, NULL);
2438 msec = find_debug_info (debug_bfd, msec))
2441 bfd_size_type start;
2447 start = stash->info_ptr_end - stash->info_ptr;
2449 if ((bfd_simple_get_relocated_section_contents
2450 (debug_bfd, msec, stash->info_ptr + start, symbols)) == NULL)
2453 stash->info_ptr_end = stash->info_ptr + start + size;
2456 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2458 stash->sec = find_debug_info (debug_bfd, NULL);
2459 stash->sec_info_ptr = stash->info_ptr;
2460 stash->syms = symbols;
2461 stash->bfd = debug_bfd;
2464 /* A null info_ptr indicates that there is no dwarf2 info
2465 (or that an error occured while setting up the stash). */
2466 if (! stash->info_ptr)
2469 stash->inliner_chain = NULL;
2471 /* Check the previously read comp. units first. */
2472 for (each = stash->all_comp_units; each; each = each->next_unit)
2475 found = (((symbol->flags & BSF_FUNCTION) == 0
2476 || comp_unit_contains_address (each, addr))
2477 && comp_unit_find_line (each, symbol, addr,
2478 filename_ptr, linenumber_ptr,
2481 found = (comp_unit_contains_address (each, addr)
2482 && comp_unit_find_nearest_line (each, addr,
2491 /* The DWARF2 spec says that the initial length field, and the
2492 offset of the abbreviation table, should both be 4-byte values.
2493 However, some compilers do things differently. */
2496 BFD_ASSERT (addr_size == 4 || addr_size == 8);
2498 /* Read each remaining comp. units checking each as they are read. */
2499 while (stash->info_ptr < stash->info_ptr_end)
2502 unsigned int offset_size = addr_size;
2503 bfd_byte *info_ptr_unit = stash->info_ptr;
2505 length = read_4_bytes (stash->bfd, stash->info_ptr);
2506 /* A 0xffffff length is the DWARF3 way of indicating
2507 we use 64-bit offsets, instead of 32-bit offsets. */
2508 if (length == 0xffffffff)
2511 length = read_8_bytes (stash->bfd, stash->info_ptr + 4);
2512 stash->info_ptr += 12;
2514 /* A zero length is the IRIX way of indicating 64-bit offsets,
2515 mostly because the 64-bit length will generally fit in 32
2516 bits, and the endianness helps. */
2517 else if (length == 0)
2520 length = read_4_bytes (stash->bfd, stash->info_ptr + 4);
2521 stash->info_ptr += 8;
2523 /* In the absence of the hints above, we assume 32-bit DWARF2
2524 offsets even for targets with 64-bit addresses, because:
2525 a) most of the time these targets will not have generated
2526 more than 2Gb of debug info and so will not need 64-bit
2529 b) if they do use 64-bit offsets but they are not using
2530 the size hints that are tested for above then they are
2531 not conforming to the DWARF3 standard anyway. */
2532 else if (addr_size == 8)
2535 stash->info_ptr += 4;
2538 stash->info_ptr += 4;
2542 each = parse_comp_unit (stash, length, info_ptr_unit,
2544 stash->info_ptr += length;
2546 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
2547 == stash->sec->size)
2549 stash->sec = find_debug_info (stash->bfd, stash->sec);
2550 stash->sec_info_ptr = stash->info_ptr;
2555 each->next_unit = stash->all_comp_units;
2556 stash->all_comp_units = each;
2558 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2559 compilation units. If we don't have them (i.e.,
2560 unit->high == 0), we need to consult the line info
2561 table to see if a compilation unit contains the given
2564 found = (((symbol->flags & BSF_FUNCTION) == 0
2565 || each->arange.high == 0
2566 || comp_unit_contains_address (each, addr))
2567 && comp_unit_find_line (each, symbol, addr,
2572 found = ((each->arange.high == 0
2573 || comp_unit_contains_address (each, addr))
2574 && comp_unit_find_nearest_line (each, addr,
2586 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2587 unset_sections (stash);
2592 /* The DWARF2 version of find_nearest_line.
2593 Return TRUE if the line is found without error. */
2596 _bfd_dwarf2_find_nearest_line (bfd *abfd,
2600 const char **filename_ptr,
2601 const char **functionname_ptr,
2602 unsigned int *linenumber_ptr,
2603 unsigned int addr_size,
2606 return find_line (abfd, section, offset, NULL, symbols, filename_ptr,
2607 functionname_ptr, linenumber_ptr, addr_size,
2611 /* The DWARF2 version of find_line.
2612 Return TRUE if the line is found without error. */
2615 _bfd_dwarf2_find_line (bfd *abfd,
2618 const char **filename_ptr,
2619 unsigned int *linenumber_ptr,
2620 unsigned int addr_size,
2623 return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr,
2624 NULL, linenumber_ptr, addr_size,
2629 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
2630 const char **filename_ptr,
2631 const char **functionname_ptr,
2632 unsigned int *linenumber_ptr,
2635 struct dwarf2_debug *stash;
2640 struct funcinfo *func = stash->inliner_chain;
2642 if (func && func->caller_func)
2644 *filename_ptr = func->caller_file;
2645 *functionname_ptr = func->caller_func->name;
2646 *linenumber_ptr = func->caller_line;
2647 stash->inliner_chain = func->caller_func;
2656 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
2658 struct comp_unit *each;
2659 struct dwarf2_debug *stash;
2661 if (abfd == NULL || elf_tdata (abfd) == NULL)
2664 stash = elf_tdata (abfd)->dwarf2_find_line_info;
2669 for (each = stash->all_comp_units; each; each = each->next_unit)
2671 struct abbrev_info **abbrevs = each->abbrevs;
2674 for (i = 0; i < ABBREV_HASH_SIZE; i++)
2676 struct abbrev_info *abbrev = abbrevs[i];
2680 free (abbrev->attrs);
2681 abbrev = abbrev->next;
2685 if (each->line_table)
2687 free (each->line_table->dirs);
2688 free (each->line_table->files);
2692 free (stash->dwarf_abbrev_buffer);
2693 free (stash->dwarf_line_buffer);
2694 free (stash->dwarf_ranges_buffer);