2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005 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. */
79 /* A list of all previously read comp_units. */
80 struct comp_unit *all_comp_units;
82 /* The next unread compilation unit within the .debug_info section.
83 Zero indicates that the .debug_info section has not been loaded
87 /* Pointer to the end of the .debug_info section memory buffer. */
88 bfd_byte *info_ptr_end;
90 /* Pointer to the section and address of the beginning of the
93 bfd_byte *sec_info_ptr;
95 /* Pointer to the symbol table. */
98 /* Pointer to the .debug_abbrev section loaded into memory. */
99 bfd_byte *dwarf_abbrev_buffer;
101 /* Length of the loaded .debug_abbrev section. */
102 unsigned long dwarf_abbrev_size;
104 /* Buffer for decode_line_info. */
105 bfd_byte *dwarf_line_buffer;
107 /* Length of the loaded .debug_line section. */
108 unsigned long dwarf_line_size;
110 /* Pointer to the .debug_str section loaded into memory. */
111 bfd_byte *dwarf_str_buffer;
113 /* Length of the loaded .debug_str section. */
114 unsigned long dwarf_str_size;
116 /* Pointer to the .debug_ranges section loaded into memory. */
117 bfd_byte *dwarf_ranges_buffer;
119 /* Length of the loaded .debug_ranges section. */
120 unsigned long dwarf_ranges_size;
130 /* A minimal decoding of DWARF2 compilation units. We only decode
131 what's needed to get to the line number information. */
135 /* Chain the previously read compilation units. */
136 struct comp_unit *next_unit;
138 /* Keep the bdf convenient (for memory allocation). */
141 /* The lowest and highest addresses contained in this compilation
142 unit as specified in the compilation unit header. */
143 struct arange arange;
145 /* The DW_AT_name attribute (for error messages). */
148 /* The abbrev hash table. */
149 struct abbrev_info **abbrevs;
151 /* Note that an error was found by comp_unit_find_nearest_line. */
154 /* The DW_AT_comp_dir attribute. */
157 /* TRUE if there is a line number table associated with this comp. unit. */
160 /* Pointer to the current comp_unit so that we can find a given entry
162 bfd_byte *info_ptr_unit;
164 /* The offset into .debug_line of the line number table. */
165 unsigned long line_offset;
167 /* Pointer to the first child die for the comp unit. */
168 bfd_byte *first_child_die_ptr;
170 /* The end of the comp unit. */
173 /* The decoded line number, NULL if not yet decoded. */
174 struct line_info_table *line_table;
176 /* A list of the functions found in this comp. unit. */
177 struct funcinfo *function_table;
179 /* Pointer to dwarf2_debug structure. */
180 struct dwarf2_debug *stash;
182 /* Address size for this unit - from unit header. */
183 unsigned char addr_size;
185 /* Offset size for this unit - from unit header. */
186 unsigned char offset_size;
188 /* Base address for this unit - from DW_AT_low_pc attribute of
189 DW_TAG_compile_unit DIE */
190 bfd_vma base_address;
193 /* This data structure holds the information of an abbrev. */
196 unsigned int number; /* Number identifying abbrev. */
197 enum dwarf_tag tag; /* DWARF tag. */
198 int has_children; /* Boolean. */
199 unsigned int num_attrs; /* Number of attributes. */
200 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
201 struct abbrev_info *next; /* Next in chain. */
206 enum dwarf_attribute name;
207 enum dwarf_form form;
210 #ifndef ABBREV_HASH_SIZE
211 #define ABBREV_HASH_SIZE 121
213 #ifndef ATTR_ALLOC_CHUNK
214 #define ATTR_ALLOC_CHUNK 4
218 The following function up to the END VERBATIM mark are
219 copied directly from dwarf2read.c. */
221 /* Read dwarf information from a buffer. */
224 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
226 return bfd_get_8 (abfd, buf);
230 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
232 return bfd_get_signed_8 (abfd, buf);
236 read_2_bytes (bfd *abfd, bfd_byte *buf)
238 return bfd_get_16 (abfd, buf);
242 read_4_bytes (bfd *abfd, bfd_byte *buf)
244 return bfd_get_32 (abfd, buf);
248 read_8_bytes (bfd *abfd, bfd_byte *buf)
250 return bfd_get_64 (abfd, buf);
254 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
256 unsigned int size ATTRIBUTE_UNUSED)
258 /* If the size of a host char is 8 bits, we can return a pointer
259 to the buffer, otherwise we have to copy the data to a buffer
260 allocated on the temporary obstack. */
265 read_string (bfd *abfd ATTRIBUTE_UNUSED,
267 unsigned int *bytes_read_ptr)
269 /* Return a pointer to the embedded string. */
270 char *str = (char *) buf;
277 *bytes_read_ptr = strlen (str) + 1;
282 read_indirect_string (struct comp_unit* unit,
284 unsigned int *bytes_read_ptr)
287 struct dwarf2_debug *stash = unit->stash;
290 if (unit->offset_size == 4)
291 offset = read_4_bytes (unit->abfd, buf);
293 offset = read_8_bytes (unit->abfd, buf);
294 *bytes_read_ptr = unit->offset_size;
296 if (! stash->dwarf_str_buffer)
299 bfd *abfd = unit->abfd;
302 msec = bfd_get_section_by_name (abfd, ".debug_str");
305 (*_bfd_error_handler)
306 (_("Dwarf Error: Can't find .debug_str section."));
307 bfd_set_error (bfd_error_bad_value);
311 sz = msec->rawsize ? msec->rawsize : msec->size;
312 stash->dwarf_str_size = sz;
313 stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
314 if (! stash->dwarf_str_buffer)
317 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
322 if (offset >= stash->dwarf_str_size)
324 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
325 (unsigned long) offset, stash->dwarf_str_size);
326 bfd_set_error (bfd_error_bad_value);
330 str = (char *) stash->dwarf_str_buffer + offset;
339 read_address (struct comp_unit *unit, bfd_byte *buf)
341 switch (unit->addr_size)
344 return bfd_get_64 (unit->abfd, buf);
346 return bfd_get_32 (unit->abfd, buf);
348 return bfd_get_16 (unit->abfd, buf);
354 /* Lookup an abbrev_info structure in the abbrev hash table. */
356 static struct abbrev_info *
357 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
359 unsigned int hash_number;
360 struct abbrev_info *abbrev;
362 hash_number = number % ABBREV_HASH_SIZE;
363 abbrev = abbrevs[hash_number];
367 if (abbrev->number == number)
370 abbrev = abbrev->next;
376 /* In DWARF version 2, the description of the debugging information is
377 stored in a separate .debug_abbrev section. Before we read any
378 dies from a section we read in all abbreviations and install them
381 static struct abbrev_info**
382 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
384 struct abbrev_info **abbrevs;
385 bfd_byte *abbrev_ptr;
386 struct abbrev_info *cur_abbrev;
387 unsigned int abbrev_number, bytes_read, abbrev_name;
388 unsigned int abbrev_form, hash_number;
391 if (! stash->dwarf_abbrev_buffer)
395 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
398 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
399 bfd_set_error (bfd_error_bad_value);
403 stash->dwarf_abbrev_size = msec->size;
404 stash->dwarf_abbrev_buffer
405 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
407 if (! stash->dwarf_abbrev_buffer)
411 if (offset >= stash->dwarf_abbrev_size)
413 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
414 (unsigned long) offset, stash->dwarf_abbrev_size);
415 bfd_set_error (bfd_error_bad_value);
419 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
420 abbrevs = bfd_zalloc (abfd, amt);
422 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
423 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
424 abbrev_ptr += bytes_read;
426 /* Loop until we reach an abbrev number of 0. */
427 while (abbrev_number)
429 amt = sizeof (struct abbrev_info);
430 cur_abbrev = bfd_zalloc (abfd, amt);
432 /* Read in abbrev header. */
433 cur_abbrev->number = abbrev_number;
434 cur_abbrev->tag = (enum dwarf_tag)
435 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
436 abbrev_ptr += bytes_read;
437 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
440 /* Now read in declarations. */
441 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
442 abbrev_ptr += bytes_read;
443 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
444 abbrev_ptr += bytes_read;
448 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
450 struct attr_abbrev *tmp;
452 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
453 amt *= sizeof (struct attr_abbrev);
454 tmp = bfd_realloc (cur_abbrev->attrs, amt);
459 for (i = 0; i < ABBREV_HASH_SIZE; i++)
461 struct abbrev_info *abbrev = abbrevs[i];
465 free (abbrev->attrs);
466 abbrev = abbrev->next;
471 cur_abbrev->attrs = tmp;
474 cur_abbrev->attrs[cur_abbrev->num_attrs].name
475 = (enum dwarf_attribute) abbrev_name;
476 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
477 = (enum dwarf_form) abbrev_form;
478 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
479 abbrev_ptr += bytes_read;
480 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
481 abbrev_ptr += bytes_read;
484 hash_number = abbrev_number % ABBREV_HASH_SIZE;
485 cur_abbrev->next = abbrevs[hash_number];
486 abbrevs[hash_number] = cur_abbrev;
488 /* Get next abbreviation.
489 Under Irix6 the abbreviations for a compilation unit are not
490 always properly terminated with an abbrev number of 0.
491 Exit loop if we encounter an abbreviation which we have
492 already read (which means we are about to read the abbreviations
493 for the next compile unit) or if the end of the abbreviation
495 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
496 >= stash->dwarf_abbrev_size)
498 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
499 abbrev_ptr += bytes_read;
500 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
507 /* Read an attribute value described by an attribute form. */
510 read_attribute_value (struct attribute *attr,
512 struct comp_unit *unit,
515 bfd *abfd = unit->abfd;
516 unsigned int bytes_read;
517 struct dwarf_block *blk;
520 attr->form = (enum dwarf_form) form;
525 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
526 case DW_FORM_ref_addr:
527 attr->u.val = read_address (unit, info_ptr);
528 info_ptr += unit->addr_size;
531 amt = sizeof (struct dwarf_block);
532 blk = bfd_alloc (abfd, amt);
533 blk->size = read_2_bytes (abfd, info_ptr);
535 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
536 info_ptr += blk->size;
540 amt = sizeof (struct dwarf_block);
541 blk = bfd_alloc (abfd, amt);
542 blk->size = read_4_bytes (abfd, info_ptr);
544 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
545 info_ptr += blk->size;
549 attr->u.val = read_2_bytes (abfd, info_ptr);
553 attr->u.val = read_4_bytes (abfd, info_ptr);
557 attr->u.val = read_8_bytes (abfd, info_ptr);
561 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
562 info_ptr += bytes_read;
565 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
566 info_ptr += bytes_read;
569 amt = sizeof (struct dwarf_block);
570 blk = bfd_alloc (abfd, amt);
571 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
572 info_ptr += bytes_read;
573 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
574 info_ptr += blk->size;
578 amt = sizeof (struct dwarf_block);
579 blk = bfd_alloc (abfd, amt);
580 blk->size = read_1_byte (abfd, info_ptr);
582 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
583 info_ptr += blk->size;
587 attr->u.val = read_1_byte (abfd, info_ptr);
591 attr->u.val = read_1_byte (abfd, info_ptr);
595 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
596 info_ptr += bytes_read;
599 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
600 info_ptr += bytes_read;
603 attr->u.val = read_1_byte (abfd, info_ptr);
607 attr->u.val = read_2_bytes (abfd, info_ptr);
611 attr->u.val = read_4_bytes (abfd, info_ptr);
615 attr->u.val = read_8_bytes (abfd, info_ptr);
618 case DW_FORM_ref_udata:
619 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
620 info_ptr += bytes_read;
622 case DW_FORM_indirect:
623 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
624 info_ptr += bytes_read;
625 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
628 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
630 bfd_set_error (bfd_error_bad_value);
635 /* Read an attribute described by an abbreviated attribute. */
638 read_attribute (struct attribute *attr,
639 struct attr_abbrev *abbrev,
640 struct comp_unit *unit,
643 attr->name = abbrev->name;
644 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
648 /* Source line information table routines. */
650 #define FILE_ALLOC_CHUNK 5
651 #define DIR_ALLOC_CHUNK 5
655 struct line_info* prev_line;
660 int end_sequence; /* End of (sequential) code sequence. */
671 struct line_info_table
674 unsigned int num_files;
675 unsigned int num_dirs;
678 struct fileinfo* files;
679 struct line_info* last_line; /* largest VMA */
680 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
685 struct funcinfo *prev_func;
687 struct arange arange;
690 /* Adds a new entry to the line_info list in the line_info_table, ensuring
691 that the list is sorted. Note that the line_info list is sorted from
692 highest to lowest VMA (with possible duplicates); that is,
693 line_info->prev_line always accesses an equal or smaller VMA. */
696 add_line_info (struct line_info_table *table,
703 bfd_size_type amt = sizeof (struct line_info);
704 struct line_info* info = bfd_alloc (table->abfd, amt);
706 /* Find the correct location for 'info'. Normally we will receive
707 new line_info data 1) in order and 2) with increasing VMAs.
708 However some compilers break the rules (cf. decode_line_info) and
709 so we include some heuristics for quickly finding the correct
710 location for 'info'. In particular, these heuristics optimize for
711 the common case in which the VMA sequence that we receive is a
712 list of locally sorted VMAs such as
713 p...z a...j (where a < j < p < z)
715 Note: table->lcl_head is used to head an *actual* or *possible*
716 sequence within the list (such as a...j) that is not directly
717 headed by table->last_line
719 Note: we may receive duplicate entries from 'decode_line_info'. */
722 if (!table->last_line
723 || address >= table->last_line->address)
725 /* Normal case: add 'info' to the beginning of the list */
726 info->prev_line = table->last_line;
727 table->last_line = info;
729 /* lcl_head: initialize to head a *possible* sequence at the end. */
730 if (!table->lcl_head)
731 table->lcl_head = info;
734 else if (!table->lcl_head->prev_line
735 && table->lcl_head->address > address)
737 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
738 list and 2) the head of 'info'. */
739 info->prev_line = NULL;
740 table->lcl_head->prev_line = info;
743 else if (table->lcl_head->prev_line
744 && table->lcl_head->address > address
745 && address >= table->lcl_head->prev_line->address)
747 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
748 list and 2) the head of 'info'. */
749 info->prev_line = table->lcl_head->prev_line;
750 table->lcl_head->prev_line = info;
755 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
756 heads for 'info'. Reset 'lcl_head' and repeat. */
757 struct line_info* li2 = table->last_line; /* always non-NULL */
758 struct line_info* li1 = li2->prev_line;
762 if (li2->address > address && address >= li1->address)
765 li2 = li1; /* always non-NULL */
766 li1 = li1->prev_line;
768 table->lcl_head = li2;
771 /* Set member data of 'info'. */
772 info->address = address;
774 info->column = column;
775 info->end_sequence = end_sequence;
777 if (filename && filename[0])
779 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
781 strcpy (info->filename, filename);
784 info->filename = NULL;
787 /* Extract a fully qualified filename from a line info table.
788 The returned string has been malloc'ed and it is the caller's
789 responsibility to free it. */
792 concat_filename (struct line_info_table *table, unsigned int file)
796 if (file - 1 >= table->num_files)
798 (*_bfd_error_handler)
799 (_("Dwarf Error: mangled line number section (bad file number)."));
800 return strdup ("<unknown>");
803 filename = table->files[file - 1].name;
805 if (! IS_ABSOLUTE_PATH (filename))
807 char *dirname = (table->files[file - 1].dir
808 ? table->dirs[table->files[file - 1].dir - 1]
811 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
812 The best we can do is return the filename part. */
815 unsigned int len = strlen (dirname) + strlen (filename) + 2;
818 name = bfd_malloc (len);
820 sprintf (name, "%s/%s", dirname, filename);
825 return strdup (filename);
829 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
831 struct arange *arange;
833 /* If the first arange is empty, use it. */
834 if (first_arange->high == 0)
836 first_arange->low = low_pc;
837 first_arange->high = high_pc;
841 /* Next see if we can cheaply extend an existing range. */
842 arange = first_arange;
845 if (low_pc == arange->high)
847 arange->high = high_pc;
850 if (high_pc == arange->low)
852 arange->low = low_pc;
855 arange = arange->next;
859 /* Need to allocate a new arange and insert it into the arange list.
860 Order isn't significant, so just insert after the first arange. */
861 arange = bfd_zalloc (abfd, sizeof (*arange));
862 arange->low = low_pc;
863 arange->high = high_pc;
864 arange->next = first_arange->next;
865 first_arange->next = arange;
868 /* Decode the line number information for UNIT. */
870 static struct line_info_table*
871 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
873 bfd *abfd = unit->abfd;
874 struct line_info_table* table;
878 unsigned int i, bytes_read, offset_size;
879 char *cur_file, *cur_dir;
880 unsigned char op_code, extended_op, adj_opcode;
883 if (! stash->dwarf_line_buffer)
887 msec = bfd_get_section_by_name (abfd, ".debug_line");
890 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
891 bfd_set_error (bfd_error_bad_value);
895 stash->dwarf_line_size = msec->size;
896 stash->dwarf_line_buffer
897 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
899 if (! stash->dwarf_line_buffer)
903 /* It is possible to get a bad value for the line_offset. Validate
904 it here so that we won't get a segfault below. */
905 if (unit->line_offset >= stash->dwarf_line_size)
907 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
908 unit->line_offset, stash->dwarf_line_size);
909 bfd_set_error (bfd_error_bad_value);
913 amt = sizeof (struct line_info_table);
914 table = bfd_alloc (abfd, amt);
916 table->comp_dir = unit->comp_dir;
918 table->num_files = 0;
925 table->last_line = NULL;
926 table->lcl_head = NULL;
928 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
930 /* Read in the prologue. */
931 lh.total_length = read_4_bytes (abfd, line_ptr);
934 if (lh.total_length == 0xffffffff)
936 lh.total_length = read_8_bytes (abfd, line_ptr);
940 else if (lh.total_length == 0 && unit->addr_size == 8)
942 /* Handle (non-standard) 64-bit DWARF2 formats. */
943 lh.total_length = read_4_bytes (abfd, line_ptr);
947 line_end = line_ptr + lh.total_length;
948 lh.version = read_2_bytes (abfd, line_ptr);
950 if (offset_size == 4)
951 lh.prologue_length = read_4_bytes (abfd, line_ptr);
953 lh.prologue_length = read_8_bytes (abfd, line_ptr);
954 line_ptr += offset_size;
955 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
957 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
959 lh.line_base = read_1_signed_byte (abfd, line_ptr);
961 lh.line_range = read_1_byte (abfd, line_ptr);
963 lh.opcode_base = read_1_byte (abfd, line_ptr);
965 amt = lh.opcode_base * sizeof (unsigned char);
966 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
968 lh.standard_opcode_lengths[0] = 1;
970 for (i = 1; i < lh.opcode_base; ++i)
972 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
976 /* Read directory table. */
977 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
979 line_ptr += bytes_read;
981 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
985 amt = table->num_dirs + DIR_ALLOC_CHUNK;
986 amt *= sizeof (char *);
988 tmp = bfd_realloc (table->dirs, amt);
997 table->dirs[table->num_dirs++] = cur_dir;
1000 line_ptr += bytes_read;
1002 /* Read file name table. */
1003 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1005 line_ptr += bytes_read;
1007 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1009 struct fileinfo *tmp;
1011 amt = table->num_files + FILE_ALLOC_CHUNK;
1012 amt *= sizeof (struct fileinfo);
1014 tmp = bfd_realloc (table->files, amt);
1017 free (table->files);
1024 table->files[table->num_files].name = cur_file;
1025 table->files[table->num_files].dir =
1026 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1027 line_ptr += bytes_read;
1028 table->files[table->num_files].time =
1029 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1030 line_ptr += bytes_read;
1031 table->files[table->num_files].size =
1032 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1033 line_ptr += bytes_read;
1037 line_ptr += bytes_read;
1039 /* Read the statement sequences until there's nothing left. */
1040 while (line_ptr < line_end)
1042 /* State machine registers. */
1043 bfd_vma address = 0;
1044 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1045 unsigned int line = 1;
1046 unsigned int column = 0;
1047 int is_stmt = lh.default_is_stmt;
1048 int basic_block = 0;
1049 int end_sequence = 0;
1050 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1051 compilers generate address sequences that are wildly out of
1052 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1053 for ia64-Linux). Thus, to determine the low and high
1054 address, we must compare on every DW_LNS_copy, etc. */
1056 bfd_vma high_pc = 0;
1058 /* Decode the table. */
1059 while (! end_sequence)
1061 op_code = read_1_byte (abfd, line_ptr);
1064 if (op_code >= lh.opcode_base)
1066 /* Special operand. */
1067 adj_opcode = op_code - lh.opcode_base;
1068 address += (adj_opcode / lh.line_range)
1069 * lh.minimum_instruction_length;
1070 line += lh.line_base + (adj_opcode % lh.line_range);
1071 /* Append row to matrix using current values. */
1072 add_line_info (table, address, filename, line, column, 0);
1074 if (low_pc == 0 || address < low_pc)
1076 if (address > high_pc)
1079 else switch (op_code)
1081 case DW_LNS_extended_op:
1082 /* Ignore length. */
1084 extended_op = read_1_byte (abfd, line_ptr);
1087 switch (extended_op)
1089 case DW_LNE_end_sequence:
1091 add_line_info (table, address, filename, line, column,
1093 if (low_pc == 0 || address < low_pc)
1095 if (address > high_pc)
1097 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1099 case DW_LNE_set_address:
1100 address = read_address (unit, line_ptr);
1101 line_ptr += unit->addr_size;
1103 case DW_LNE_define_file:
1104 cur_file = read_string (abfd, line_ptr, &bytes_read);
1105 line_ptr += bytes_read;
1106 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1108 struct fileinfo *tmp;
1110 amt = table->num_files + FILE_ALLOC_CHUNK;
1111 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 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1136 bfd_set_error (bfd_error_bad_value);
1138 free (table->files);
1144 add_line_info (table, address, filename, line, column, 0);
1146 if (low_pc == 0 || address < low_pc)
1148 if (address > high_pc)
1151 case DW_LNS_advance_pc:
1152 address += lh.minimum_instruction_length
1153 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1154 line_ptr += bytes_read;
1156 case DW_LNS_advance_line:
1157 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1158 line_ptr += bytes_read;
1160 case DW_LNS_set_file:
1164 /* The file and directory tables are 0
1165 based, the references are 1 based. */
1166 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1167 line_ptr += bytes_read;
1170 filename = concat_filename (table, file);
1173 case DW_LNS_set_column:
1174 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1175 line_ptr += bytes_read;
1177 case DW_LNS_negate_stmt:
1178 is_stmt = (!is_stmt);
1180 case DW_LNS_set_basic_block:
1183 case DW_LNS_const_add_pc:
1184 address += lh.minimum_instruction_length
1185 * ((255 - lh.opcode_base) / lh.line_range);
1187 case DW_LNS_fixed_advance_pc:
1188 address += read_2_bytes (abfd, line_ptr);
1195 /* Unknown standard opcode, ignore it. */
1196 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1198 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1199 line_ptr += bytes_read;
1212 /* If ADDR is within TABLE set the output parameters and return TRUE,
1213 otherwise return FALSE. The output parameters, FILENAME_PTR and
1214 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1217 lookup_address_in_line_info_table (struct line_info_table *table,
1219 struct funcinfo *function,
1220 const char **filename_ptr,
1221 unsigned int *linenumber_ptr)
1223 /* Note: table->last_line should be a descendingly sorted list. */
1224 struct line_info* next_line = table->last_line;
1225 struct line_info* each_line = NULL;
1226 *filename_ptr = NULL;
1231 each_line = next_line->prev_line;
1233 /* Check for large addresses */
1234 if (addr > next_line->address)
1235 each_line = NULL; /* ensure we skip over the normal case */
1237 /* Normal case: search the list; save */
1238 while (each_line && next_line)
1240 /* If we have an address match, save this info. This allows us
1241 to return as good as results as possible for strange debugging
1243 bfd_boolean addr_match = FALSE;
1244 if (each_line->address <= addr && addr < next_line->address)
1248 /* If this line appears to span functions, and addr is in the
1249 later function, return the first line of that function instead
1250 of the last line of the earlier one. This check is for GCC
1251 2.95, which emits the first line number for a function late. */
1253 if (function != NULL)
1256 struct arange *arange;
1258 /* Find the lowest address in the function's range list */
1259 lowest_pc = function->arange.low;
1260 for (arange = &function->arange;
1262 arange = arange->next)
1264 if (function->arange.low < lowest_pc)
1265 lowest_pc = function->arange.low;
1267 /* Check for spanning function and set outgoing line info */
1268 if (addr >= lowest_pc
1269 && each_line->address < lowest_pc
1270 && next_line->address > lowest_pc)
1272 *filename_ptr = next_line->filename;
1273 *linenumber_ptr = next_line->line;
1277 *filename_ptr = each_line->filename;
1278 *linenumber_ptr = each_line->line;
1283 if (addr_match && !each_line->end_sequence)
1284 return TRUE; /* we have definitely found what we want */
1286 next_line = each_line;
1287 each_line = each_line->prev_line;
1290 /* At this point each_line is NULL but next_line is not. If we found
1291 a candidate end-of-sequence point in the loop above, we can return
1292 that (compatibility with a bug in the Intel compiler); otherwise,
1293 assuming that we found the containing function for this address in
1294 this compilation unit, return the first line we have a number for
1295 (compatibility with GCC 2.95). */
1296 if (*filename_ptr == NULL && function != NULL)
1298 *filename_ptr = next_line->filename;
1299 *linenumber_ptr = next_line->line;
1306 /* Read in the .debug_ranges section for future reference */
1309 read_debug_ranges (struct comp_unit *unit)
1311 struct dwarf2_debug *stash = unit->stash;
1312 if (! stash->dwarf_ranges_buffer)
1314 bfd *abfd = unit->abfd;
1317 msec = bfd_get_section_by_name (abfd, ".debug_ranges");
1320 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
1321 bfd_set_error (bfd_error_bad_value);
1325 stash->dwarf_ranges_size = msec->size;
1326 stash->dwarf_ranges_buffer
1327 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1329 if (! stash->dwarf_ranges_buffer)
1335 /* Function table functions. */
1337 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1338 Note that we need to find the function that has the smallest
1339 range that contains ADDR, to handle inlined functions without
1340 depending upon them being ordered in TABLE by increasing range. */
1343 lookup_address_in_function_table (struct funcinfo *table,
1345 struct funcinfo **function_ptr,
1346 const char **functionname_ptr)
1348 struct funcinfo* each_func;
1349 struct funcinfo* best_fit = NULL;
1350 struct arange *arange;
1352 for (each_func = table;
1354 each_func = each_func->prev_func)
1356 for (arange = &each_func->arange;
1358 arange = arange->next)
1360 if (addr >= arange->low && addr < arange->high)
1363 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1364 best_fit = each_func;
1371 *functionname_ptr = best_fit->name;
1372 *function_ptr = best_fit;
1382 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1384 bfd *abfd = unit->abfd;
1386 unsigned int abbrev_number, bytes_read, i;
1387 struct abbrev_info *abbrev;
1388 struct attribute attr;
1391 info_ptr = unit->info_ptr_unit + die_ref;
1392 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1393 info_ptr += bytes_read;
1397 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1400 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1402 bfd_set_error (bfd_error_bad_value);
1406 for (i = 0; i < abbrev->num_attrs && !name; ++i)
1408 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1414 case DW_AT_specification:
1415 name = find_abstract_instance_name (unit, attr.u.val);
1427 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1429 bfd_byte *ranges_ptr;
1430 bfd_vma base_address = unit->base_address;
1432 if (! unit->stash->dwarf_ranges_buffer)
1434 if (! read_debug_ranges (unit))
1437 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1444 if (unit->offset_size == 4)
1446 low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1448 high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1453 low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1455 high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1458 if (low_pc == 0 && high_pc == 0)
1460 if (low_pc == -1UL && high_pc != -1UL)
1461 base_address = high_pc;
1463 arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1467 /* DWARF2 Compilation unit functions. */
1469 /* Scan over each die in a comp. unit looking for functions to add
1470 to the function table. */
1473 scan_unit_for_functions (struct comp_unit *unit)
1475 bfd *abfd = unit->abfd;
1476 bfd_byte *info_ptr = unit->first_child_die_ptr;
1477 int nesting_level = 1;
1479 while (nesting_level)
1481 unsigned int abbrev_number, bytes_read, i;
1482 struct abbrev_info *abbrev;
1483 struct attribute attr;
1484 struct funcinfo *func;
1486 bfd_vma high_pc = 0;
1488 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1489 info_ptr += bytes_read;
1491 if (! abbrev_number)
1497 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1500 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1502 bfd_set_error (bfd_error_bad_value);
1506 if (abbrev->tag == DW_TAG_subprogram
1507 || abbrev->tag == DW_TAG_inlined_subroutine)
1509 bfd_size_type amt = sizeof (struct funcinfo);
1510 func = bfd_zalloc (abfd, amt);
1511 func->prev_func = unit->function_table;
1512 unit->function_table = func;
1517 for (i = 0; i < abbrev->num_attrs; ++i)
1519 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1525 case DW_AT_abstract_origin:
1526 func->name = find_abstract_instance_name (unit, attr.u.val);
1530 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1531 if (func->name == NULL)
1532 func->name = attr.u.str;
1535 case DW_AT_MIPS_linkage_name:
1536 func->name = attr.u.str;
1540 low_pc = attr.u.val;
1544 high_pc = attr.u.val;
1548 read_rangelist (unit, &func->arange, attr.u.val);
1557 if (func && high_pc != 0)
1559 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
1562 if (abbrev->has_children)
1569 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1570 includes the compilation unit header that proceeds the DIE's, but
1571 does not include the length field that precedes each compilation
1572 unit header. END_PTR points one past the end of this comp unit.
1573 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1575 This routine does not read the whole compilation unit; only enough
1576 to get to the line number information for the compilation unit. */
1578 static struct comp_unit *
1579 parse_comp_unit (bfd *abfd,
1580 struct dwarf2_debug *stash,
1581 bfd_vma unit_length,
1582 bfd_byte *info_ptr_unit,
1583 unsigned int offset_size)
1585 struct comp_unit* unit;
1586 unsigned int version;
1587 bfd_uint64_t abbrev_offset = 0;
1588 unsigned int addr_size;
1589 struct abbrev_info** abbrevs;
1590 unsigned int abbrev_number, bytes_read, i;
1591 struct abbrev_info *abbrev;
1592 struct attribute attr;
1593 bfd_byte *info_ptr = stash->info_ptr;
1594 bfd_byte *end_ptr = info_ptr + unit_length;
1597 bfd_vma high_pc = 0;
1599 version = read_2_bytes (abfd, info_ptr);
1601 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1602 if (offset_size == 4)
1603 abbrev_offset = read_4_bytes (abfd, info_ptr);
1605 abbrev_offset = read_8_bytes (abfd, info_ptr);
1606 info_ptr += offset_size;
1607 addr_size = read_1_byte (abfd, info_ptr);
1612 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1613 bfd_set_error (bfd_error_bad_value);
1617 if (addr_size > sizeof (bfd_vma))
1619 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1621 (unsigned int) sizeof (bfd_vma));
1622 bfd_set_error (bfd_error_bad_value);
1626 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1628 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1629 bfd_set_error (bfd_error_bad_value);
1633 /* Read the abbrevs for this compilation unit into a table. */
1634 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1638 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1639 info_ptr += bytes_read;
1640 if (! abbrev_number)
1642 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1644 bfd_set_error (bfd_error_bad_value);
1648 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1651 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1653 bfd_set_error (bfd_error_bad_value);
1657 amt = sizeof (struct comp_unit);
1658 unit = bfd_zalloc (abfd, amt);
1660 unit->addr_size = addr_size;
1661 unit->offset_size = offset_size;
1662 unit->abbrevs = abbrevs;
1663 unit->end_ptr = end_ptr;
1664 unit->stash = stash;
1665 unit->info_ptr_unit = info_ptr_unit;
1667 for (i = 0; i < abbrev->num_attrs; ++i)
1669 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1671 /* Store the data if it is of an attribute we want to keep in a
1672 partial symbol table. */
1675 case DW_AT_stmt_list:
1677 unit->line_offset = attr.u.val;
1681 unit->name = attr.u.str;
1685 low_pc = attr.u.val;
1686 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
1687 this is the base address to use when reading location
1688 lists or range lists. */
1689 unit->base_address = low_pc;
1693 high_pc = attr.u.val;
1697 read_rangelist (unit, &unit->arange, attr.u.val);
1700 case DW_AT_comp_dir:
1702 char *comp_dir = attr.u.str;
1705 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1706 directory, get rid of it. */
1707 char *cp = strchr (comp_dir, ':');
1709 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1712 unit->comp_dir = comp_dir;
1722 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1725 unit->first_child_die_ptr = info_ptr;
1729 /* Return TRUE if UNIT contains the address given by ADDR. */
1732 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
1734 struct arange *arange;
1739 arange = &unit->arange;
1742 if (addr >= arange->low && addr < arange->high)
1744 arange = arange->next;
1751 /* If UNIT contains ADDR, set the output parameters to the values for
1752 the line containing ADDR. The output parameters, FILENAME_PTR,
1753 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1756 Return TRUE if UNIT contains ADDR, and no errors were encountered;
1760 comp_unit_find_nearest_line (struct comp_unit *unit,
1762 const char **filename_ptr,
1763 const char **functionname_ptr,
1764 unsigned int *linenumber_ptr,
1765 struct dwarf2_debug *stash)
1769 struct funcinfo *function;
1774 if (! unit->line_table)
1776 if (! unit->stmtlist)
1782 unit->line_table = decode_line_info (unit, stash);
1784 if (! unit->line_table)
1790 if (unit->first_child_die_ptr < unit->end_ptr
1791 && ! scan_unit_for_functions (unit))
1799 func_p = lookup_address_in_function_table (unit->function_table, addr,
1800 &function, functionname_ptr);
1801 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
1802 function, filename_ptr,
1804 return line_p || func_p;
1807 /* Locate a section in a BFD containing debugging info. The search starts
1808 from the section after AFTER_SEC, or from the first section in the BFD if
1809 AFTER_SEC is NULL. The search works by examining the names of the
1810 sections. There are two permissiable names. The first is .debug_info.
1811 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
1812 This is a variation on the .debug_info section which has a checksum
1813 describing the contents appended onto the name. This allows the linker to
1814 identify and discard duplicate debugging sections for different
1815 compilation units. */
1816 #define DWARF2_DEBUG_INFO ".debug_info"
1817 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1820 find_debug_info (bfd *abfd, asection *after_sec)
1825 msec = after_sec->next;
1827 msec = abfd->sections;
1831 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1834 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1843 /* The DWARF2 version of find_nearest_line. Return TRUE if the line
1844 is found without error. ADDR_SIZE is the number of bytes in the
1845 initial .debug_info length field and in the abbreviation offset.
1846 You may use zero to indicate that the default value should be
1850 _bfd_dwarf2_find_nearest_line (bfd *abfd,
1854 const char **filename_ptr,
1855 const char **functionname_ptr,
1856 unsigned int *linenumber_ptr,
1857 unsigned int addr_size,
1860 /* Read each compilation unit from the section .debug_info, and check
1861 to see if it contains the address we are searching for. If yes,
1862 lookup the address, and return the line number info. If no, go
1863 on to the next compilation unit.
1865 We keep a list of all the previously read compilation units, and
1866 a pointer to the next un-read compilation unit. Check the
1867 previously read units before reading more. */
1868 struct dwarf2_debug *stash;
1870 /* What address are we looking for? */
1873 struct comp_unit* each;
1877 if (section->output_section)
1878 addr += section->output_section->vma + section->output_offset;
1880 addr += section->vma;
1881 *filename_ptr = NULL;
1882 *functionname_ptr = NULL;
1883 *linenumber_ptr = 0;
1885 /* The DWARF2 spec says that the initial length field, and the
1886 offset of the abbreviation table, should both be 4-byte values.
1887 However, some compilers do things differently. */
1890 BFD_ASSERT (addr_size == 4 || addr_size == 8);
1894 bfd_size_type total_size;
1896 bfd_size_type amt = sizeof (struct dwarf2_debug);
1898 stash = bfd_zalloc (abfd, amt);
1904 msec = find_debug_info (abfd, NULL);
1906 /* No dwarf2 info. Note that at this point the stash
1907 has been allocated, but contains zeros, this lets
1908 future calls to this function fail quicker. */
1911 /* There can be more than one DWARF2 info section in a BFD these days.
1912 Read them all in and produce one large stash. We do this in two
1913 passes - in the first pass we just accumulate the section sizes.
1914 In the second pass we read in the section's contents. The allows
1915 us to avoid reallocing the data as we add sections to the stash. */
1916 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1917 total_size += msec->size;
1919 stash->info_ptr = bfd_alloc (abfd, total_size);
1920 if (stash->info_ptr == NULL)
1923 stash->info_ptr_end = stash->info_ptr;
1925 for (msec = find_debug_info (abfd, NULL);
1927 msec = find_debug_info (abfd, msec))
1930 bfd_size_type start;
1936 start = stash->info_ptr_end - stash->info_ptr;
1938 if ((bfd_simple_get_relocated_section_contents
1939 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
1942 stash->info_ptr_end = stash->info_ptr + start + size;
1945 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1947 stash->sec = find_debug_info (abfd, NULL);
1948 stash->sec_info_ptr = stash->info_ptr;
1949 stash->syms = symbols;
1952 /* A null info_ptr indicates that there is no dwarf2 info
1953 (or that an error occured while setting up the stash). */
1954 if (! stash->info_ptr)
1957 /* Check the previously read comp. units first. */
1958 for (each = stash->all_comp_units; each; each = each->next_unit)
1959 if (comp_unit_contains_address (each, addr))
1960 return comp_unit_find_nearest_line (each, addr, filename_ptr,
1961 functionname_ptr, linenumber_ptr,
1964 /* Read each remaining comp. units checking each as they are read. */
1965 while (stash->info_ptr < stash->info_ptr_end)
1969 unsigned int offset_size = addr_size;
1970 bfd_byte *info_ptr_unit = stash->info_ptr;
1972 length = read_4_bytes (abfd, stash->info_ptr);
1973 /* A 0xffffff length is the DWARF3 way of indicating we use
1974 64-bit offsets, instead of 32-bit offsets. */
1975 if (length == 0xffffffff)
1978 length = read_8_bytes (abfd, stash->info_ptr + 4);
1979 stash->info_ptr += 12;
1981 /* A zero length is the IRIX way of indicating 64-bit offsets,
1982 mostly because the 64-bit length will generally fit in 32
1983 bits, and the endianness helps. */
1984 else if (length == 0)
1987 length = read_4_bytes (abfd, stash->info_ptr + 4);
1988 stash->info_ptr += 8;
1990 /* In the absence of the hints above, we assume addr_size-sized
1991 offsets, for backward-compatibility with pre-DWARF3 64-bit
1993 else if (addr_size == 8)
1995 length = read_8_bytes (abfd, stash->info_ptr);
1996 stash->info_ptr += 8;
1999 stash->info_ptr += 4;
2003 each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
2005 stash->info_ptr += length;
2007 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
2008 == stash->sec->size)
2010 stash->sec = find_debug_info (abfd, stash->sec);
2011 stash->sec_info_ptr = stash->info_ptr;
2016 each->next_unit = stash->all_comp_units;
2017 stash->all_comp_units = each;
2019 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2020 compilation units. If we don't have them (i.e.,
2021 unit->high == 0), we need to consult the line info
2022 table to see if a compilation unit contains the given
2024 if (each->arange.high > 0)
2026 if (comp_unit_contains_address (each, addr))
2027 return comp_unit_find_nearest_line (each, addr,
2035 found = comp_unit_find_nearest_line (each, addr,
2051 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
2053 struct comp_unit *each;
2054 struct dwarf2_debug *stash;
2056 if (abfd == NULL || elf_tdata (abfd) == NULL)
2059 stash = elf_tdata (abfd)->dwarf2_find_line_info;
2064 for (each = stash->all_comp_units; each; each = each->next_unit)
2066 struct abbrev_info **abbrevs = each->abbrevs;
2069 for (i = 0; i < ABBREV_HASH_SIZE; i++)
2071 struct abbrev_info *abbrev = abbrevs[i];
2075 free (abbrev->attrs);
2076 abbrev = abbrev->next;
2080 if (each->line_table)
2082 free (each->line_table->dirs);
2083 free (each->line_table->files);
2087 free (stash->dwarf_abbrev_buffer);
2088 free (stash->dwarf_line_buffer);
2089 free (stash->dwarf_ranges_buffer);