2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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;
71 /* Get at parts of an attribute structure. */
73 #define DW_STRING(attr) ((attr)->u.str)
74 #define DW_UNSND(attr) ((attr)->u.unsnd)
75 #define DW_BLOCK(attr) ((attr)->u.blk)
76 #define DW_SND(attr) ((attr)->u.snd)
77 #define DW_ADDR(attr) ((attr)->u.addr)
79 /* Blocks are a bunch of untyped bytes. */
88 /* A list of all previously read comp_units. */
89 struct comp_unit* all_comp_units;
91 /* The next unread compilation unit within the .debug_info section.
92 Zero indicates that the .debug_info section has not been loaded
96 /* Pointer to the end of the .debug_info section memory buffer. */
99 /* Pointer to the section and address of the beginning of the
104 /* Pointer to the symbol table. */
107 /* Pointer to the .debug_abbrev section loaded into memory. */
108 char* dwarf_abbrev_buffer;
110 /* Length of the loaded .debug_abbrev section. */
111 unsigned long dwarf_abbrev_size;
113 /* Buffer for decode_line_info. */
114 char *dwarf_line_buffer;
116 /* Length of the loaded .debug_line section. */
117 unsigned long dwarf_line_size;
119 /* Pointer to the .debug_str section loaded into memory. */
120 char* dwarf_str_buffer;
122 /* Length of the loaded .debug_str section. */
123 unsigned long dwarf_str_size;
133 /* A minimal decoding of DWARF2 compilation units. We only decode
134 what's needed to get to the line number information. */
138 /* Chain the previously read compilation units. */
139 struct comp_unit* next_unit;
141 /* Keep the bdf convenient (for memory allocation). */
144 /* The lowest and higest addresses contained in this compilation
145 unit as specified in the compilation unit header. */
146 struct arange arange;
148 /* The DW_AT_name attribute (for error messages). */
151 /* The abbrev hash table. */
152 struct abbrev_info** abbrevs;
154 /* Note that an error was found by comp_unit_find_nearest_line. */
157 /* The DW_AT_comp_dir attribute. */
160 /* TRUE if there is a line number table associated with this comp. unit. */
163 /* The offset into .debug_line of the line number table. */
164 unsigned long line_offset;
166 /* Pointer to the first child die for the comp unit. */
167 char *first_child_die_ptr;
169 /* The end of the comp unit. */
172 /* The decoded line number, NULL if not yet decoded. */
173 struct line_info_table* line_table;
175 /* A list of the functions found in this comp. unit. */
176 struct funcinfo* function_table;
178 /* Pointer to dwarf2_debug structure. */
179 struct dwarf2_debug *stash;
181 /* Address size for this unit - from unit header. */
182 unsigned char addr_size;
184 /* Offset size for this unit - from unit header. */
185 unsigned char offset_size;
188 /* This data structure holds the information of an abbrev. */
191 unsigned int number; /* Number identifying abbrev. */
192 enum dwarf_tag tag; /* DWARF tag. */
193 int has_children; /* Boolean. */
194 unsigned int num_attrs; /* Number of attributes. */
195 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
196 struct abbrev_info *next; /* Next in chain. */
201 enum dwarf_attribute name;
202 enum dwarf_form form;
205 #ifndef ABBREV_HASH_SIZE
206 #define ABBREV_HASH_SIZE 121
208 #ifndef ATTR_ALLOC_CHUNK
209 #define ATTR_ALLOC_CHUNK 4
212 static unsigned int read_1_byte PARAMS ((bfd *, char *));
213 static int read_1_signed_byte PARAMS ((bfd *, char *));
214 static unsigned int read_2_bytes PARAMS ((bfd *, char *));
215 static unsigned int read_4_bytes PARAMS ((bfd *, char *));
216 static bfd_vma read_8_bytes PARAMS ((bfd *, char *));
217 static char *read_n_bytes PARAMS ((bfd *, char *, unsigned int));
218 static char *read_string PARAMS ((bfd *, char *, unsigned int *));
219 static char *read_indirect_string PARAMS ((struct comp_unit *, char *, unsigned int *));
220 static unsigned int read_unsigned_leb128
221 PARAMS ((bfd *, char *, unsigned int *));
222 static int read_signed_leb128
223 PARAMS ((bfd *, char *, unsigned int *));
224 static bfd_vma read_address PARAMS ((struct comp_unit *, char *));
225 static struct abbrev_info *lookup_abbrev
226 PARAMS ((unsigned int, struct abbrev_info **));
227 static struct abbrev_info **read_abbrevs
228 PARAMS ((bfd *, bfd_vma, struct dwarf2_debug *));
229 static char *read_attribute
230 PARAMS ((struct attribute *, struct attr_abbrev *,
231 struct comp_unit *, char *));
232 static char *read_attribute_value
233 PARAMS ((struct attribute *, unsigned,
234 struct comp_unit *, char *));
235 static void add_line_info
236 PARAMS ((struct line_info_table *, bfd_vma, char *,
237 unsigned int, unsigned int, int));
238 static char *concat_filename PARAMS ((struct line_info_table *, unsigned int));
239 static void arange_add PARAMS ((struct comp_unit *, bfd_vma, bfd_vma));
240 static struct line_info_table *decode_line_info
241 PARAMS ((struct comp_unit *, struct dwarf2_debug *));
242 static bfd_boolean lookup_address_in_line_info_table
243 PARAMS ((struct line_info_table *, bfd_vma, struct funcinfo *,
244 const char **, unsigned int *));
245 static bfd_boolean lookup_address_in_function_table
246 PARAMS ((struct funcinfo *, bfd_vma, struct funcinfo **, const char **));
247 static bfd_boolean scan_unit_for_functions PARAMS ((struct comp_unit *));
248 static struct comp_unit *parse_comp_unit
249 PARAMS ((bfd *, struct dwarf2_debug *, bfd_vma, unsigned int));
250 static bfd_boolean comp_unit_contains_address
251 PARAMS ((struct comp_unit *, bfd_vma));
252 static bfd_boolean comp_unit_find_nearest_line
253 PARAMS ((struct comp_unit *, bfd_vma, const char **, const char **,
254 unsigned int *, struct dwarf2_debug *));
255 static asection *find_debug_info PARAMS ((bfd *, asection *));
258 The following function up to the END VERBATIM mark are
259 copied directly from dwarf2read.c. */
261 /* Read dwarf information from a buffer. */
264 read_1_byte (abfd, buf)
265 bfd *abfd ATTRIBUTE_UNUSED;
268 return bfd_get_8 (abfd, (bfd_byte *) buf);
272 read_1_signed_byte (abfd, buf)
273 bfd *abfd ATTRIBUTE_UNUSED;
276 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
280 read_2_bytes (abfd, buf)
284 return bfd_get_16 (abfd, (bfd_byte *) buf);
287 #if 0 /* This is not used. */
290 read_2_signed_bytes (abfd, buf)
294 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
300 read_4_bytes (abfd, buf)
304 return bfd_get_32 (abfd, (bfd_byte *) buf);
307 #if 0 /* This is not used. */
310 read_4_signed_bytes (abfd, buf)
314 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
320 read_8_bytes (abfd, buf)
324 return bfd_get_64 (abfd, (bfd_byte *) buf);
328 read_n_bytes (abfd, buf, size)
329 bfd *abfd ATTRIBUTE_UNUSED;
331 unsigned int size ATTRIBUTE_UNUSED;
333 /* If the size of a host char is 8 bits, we can return a pointer
334 to the buffer, otherwise we have to copy the data to a buffer
335 allocated on the temporary obstack. */
340 read_string (abfd, buf, bytes_read_ptr)
341 bfd *abfd ATTRIBUTE_UNUSED;
343 unsigned int *bytes_read_ptr;
345 /* Return a pointer to the embedded string. */
352 *bytes_read_ptr = strlen (buf) + 1;
357 read_indirect_string (unit, buf, bytes_read_ptr)
358 struct comp_unit* unit;
360 unsigned int *bytes_read_ptr;
363 struct dwarf2_debug *stash = unit->stash;
365 if (unit->offset_size == 4)
366 offset = read_4_bytes (unit->abfd, buf);
368 offset = read_8_bytes (unit->abfd, buf);
369 *bytes_read_ptr = unit->offset_size;
371 if (! stash->dwarf_str_buffer)
374 bfd *abfd = unit->abfd;
376 msec = bfd_get_section_by_name (abfd, ".debug_str");
379 (*_bfd_error_handler)
380 (_("Dwarf Error: Can't find .debug_str section."));
381 bfd_set_error (bfd_error_bad_value);
385 stash->dwarf_str_size = msec->_raw_size;
386 stash->dwarf_str_buffer = (char*) bfd_alloc (abfd, msec->_raw_size);
387 if (! stash->dwarf_abbrev_buffer)
390 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
391 (bfd_vma) 0, msec->_raw_size))
395 if (offset >= stash->dwarf_str_size)
397 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
398 (unsigned long) offset, stash->dwarf_str_size);
399 bfd_set_error (bfd_error_bad_value);
403 buf = stash->dwarf_str_buffer + offset;
410 read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
411 bfd *abfd ATTRIBUTE_UNUSED;
413 unsigned int *bytes_read_ptr;
416 unsigned int num_read;
426 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
429 result |= ((byte & 0x7f) << shift);
434 * bytes_read_ptr = num_read;
440 read_signed_leb128 (abfd, buf, bytes_read_ptr)
441 bfd *abfd ATTRIBUTE_UNUSED;
443 unsigned int * bytes_read_ptr;
456 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
459 result |= ((byte & 0x7f) << shift);
464 if ((shift < 32) && (byte & 0x40))
465 result |= -(1 << shift);
467 * bytes_read_ptr = num_read;
475 read_address (unit, buf)
476 struct comp_unit* unit;
479 switch (unit->addr_size)
482 return bfd_get_64 (unit->abfd, (bfd_byte *) buf);
484 return bfd_get_32 (unit->abfd, (bfd_byte *) buf);
486 return bfd_get_16 (unit->abfd, (bfd_byte *) buf);
492 /* Lookup an abbrev_info structure in the abbrev hash table. */
494 static struct abbrev_info *
495 lookup_abbrev (number,abbrevs)
497 struct abbrev_info **abbrevs;
499 unsigned int hash_number;
500 struct abbrev_info *abbrev;
502 hash_number = number % ABBREV_HASH_SIZE;
503 abbrev = abbrevs[hash_number];
507 if (abbrev->number == number)
510 abbrev = abbrev->next;
516 /* In DWARF version 2, the description of the debugging information is
517 stored in a separate .debug_abbrev section. Before we read any
518 dies from a section we read in all abbreviations and install them
521 static struct abbrev_info**
522 read_abbrevs (abfd, offset, stash)
525 struct dwarf2_debug *stash;
527 struct abbrev_info **abbrevs;
529 struct abbrev_info *cur_abbrev;
530 unsigned int abbrev_number, bytes_read, abbrev_name;
531 unsigned int abbrev_form, hash_number;
534 if (! stash->dwarf_abbrev_buffer)
538 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
541 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
542 bfd_set_error (bfd_error_bad_value);
546 stash->dwarf_abbrev_size = msec->_raw_size;
547 stash->dwarf_abbrev_buffer
548 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
550 if (! stash->dwarf_abbrev_buffer)
554 if (offset >= stash->dwarf_abbrev_size)
556 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
557 (unsigned long) offset, stash->dwarf_abbrev_size);
558 bfd_set_error (bfd_error_bad_value);
562 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
563 abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, amt);
565 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
566 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
567 abbrev_ptr += bytes_read;
569 /* Loop until we reach an abbrev number of 0. */
570 while (abbrev_number)
572 amt = sizeof (struct abbrev_info);
573 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
575 /* Read in abbrev header. */
576 cur_abbrev->number = abbrev_number;
577 cur_abbrev->tag = (enum dwarf_tag)
578 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
579 abbrev_ptr += bytes_read;
580 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
583 /* Now read in declarations. */
584 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
585 abbrev_ptr += bytes_read;
586 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
587 abbrev_ptr += bytes_read;
591 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
593 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
594 amt *= sizeof (struct attr_abbrev);
595 cur_abbrev->attrs = ((struct attr_abbrev *)
596 bfd_realloc (cur_abbrev->attrs, amt));
597 if (! cur_abbrev->attrs)
601 cur_abbrev->attrs[cur_abbrev->num_attrs].name
602 = (enum dwarf_attribute) abbrev_name;
603 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
604 = (enum dwarf_form) abbrev_form;
605 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
606 abbrev_ptr += bytes_read;
607 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
608 abbrev_ptr += bytes_read;
611 hash_number = abbrev_number % ABBREV_HASH_SIZE;
612 cur_abbrev->next = abbrevs[hash_number];
613 abbrevs[hash_number] = cur_abbrev;
615 /* Get next abbreviation.
616 Under Irix6 the abbreviations for a compilation unit are not
617 always properly terminated with an abbrev number of 0.
618 Exit loop if we encounter an abbreviation which we have
619 already read (which means we are about to read the abbreviations
620 for the next compile unit) or if the end of the abbreviation
622 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
623 >= stash->dwarf_abbrev_size)
625 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
626 abbrev_ptr += bytes_read;
627 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
634 /* Read an attribute value described by an attribute form. */
637 read_attribute_value (attr, form, unit, info_ptr)
638 struct attribute *attr;
640 struct comp_unit *unit;
643 bfd *abfd = unit->abfd;
644 unsigned int bytes_read;
645 struct dwarf_block *blk;
648 attr->form = (enum dwarf_form) form;
653 /* FIXME: DWARF3 draft sais DW_FORM_ref_addr is offset_size. */
654 case DW_FORM_ref_addr:
655 DW_ADDR (attr) = read_address (unit, info_ptr);
656 info_ptr += unit->addr_size;
659 amt = sizeof (struct dwarf_block);
660 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
661 blk->size = read_2_bytes (abfd, info_ptr);
663 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
664 info_ptr += blk->size;
665 DW_BLOCK (attr) = blk;
668 amt = sizeof (struct dwarf_block);
669 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
670 blk->size = read_4_bytes (abfd, info_ptr);
672 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
673 info_ptr += blk->size;
674 DW_BLOCK (attr) = blk;
677 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
681 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
685 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
689 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
690 info_ptr += bytes_read;
693 DW_STRING (attr) = read_indirect_string (unit, info_ptr, &bytes_read);
694 info_ptr += bytes_read;
697 amt = sizeof (struct dwarf_block);
698 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
699 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
700 info_ptr += bytes_read;
701 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
702 info_ptr += blk->size;
703 DW_BLOCK (attr) = blk;
706 amt = sizeof (struct dwarf_block);
707 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
708 blk->size = read_1_byte (abfd, info_ptr);
710 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
711 info_ptr += blk->size;
712 DW_BLOCK (attr) = blk;
715 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
719 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
723 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
724 info_ptr += bytes_read;
727 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
728 info_ptr += bytes_read;
731 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
735 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
739 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
743 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
746 case DW_FORM_ref_udata:
747 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
748 info_ptr += bytes_read;
750 case DW_FORM_indirect:
751 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
752 info_ptr += bytes_read;
753 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
756 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
758 bfd_set_error (bfd_error_bad_value);
763 /* Read an attribute described by an abbreviated attribute. */
766 read_attribute (attr, abbrev, unit, info_ptr)
767 struct attribute *attr;
768 struct attr_abbrev *abbrev;
769 struct comp_unit *unit;
772 attr->name = abbrev->name;
773 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
777 /* Source line information table routines. */
779 #define FILE_ALLOC_CHUNK 5
780 #define DIR_ALLOC_CHUNK 5
784 struct line_info* prev_line;
789 int end_sequence; /* End of (sequential) code sequence. */
800 struct line_info_table
803 unsigned int num_files;
804 unsigned int num_dirs;
807 struct fileinfo* files;
808 struct line_info* last_line; /* largest VMA */
809 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
814 struct funcinfo *prev_func;
820 /* add_line_info: adds a new entry to the line_info list in the
821 line_info_table, ensuring that the list is sorted. Note that the
822 line_info list is sorted from highest to lowest VMA (with possible
823 duplicates); that is, line_info->prev_line always accesses an equal
826 add_line_info (table, address, filename, line, column, end_sequence)
827 struct line_info_table* table;
834 bfd_size_type amt = sizeof (struct line_info);
835 struct line_info* info = (struct line_info*) bfd_alloc (table->abfd, amt);
837 /* Find the correct location for 'info'. Normally we will receive
838 new line_info data 1) in order and 2) with increasing VMAs.
839 However some compilers break the rules (cf. decode_line_info) and
840 so we include some heuristics for quickly finding the correct
841 location for 'info'. In particular, these heuristics optimize for
842 the common case in which the VMA sequence that we receive is a
843 list of locally sorted VMAs such as
844 p...z a...j (where a < j < p < z)
846 Note: table->lcl_head is used to head an *actual* or *possible*
847 sequence within the list (such as a...j) that is not directly
848 headed by table->last_line
850 Note: we may receive duplicate entries from 'decode_line_info'. */
853 if (!table->last_line
854 || address >= table->last_line->address)
856 /* Normal case: add 'info' to the beginning of the list */
857 info->prev_line = table->last_line;
858 table->last_line = info;
860 /* lcl_head: initialize to head a *possible* sequence at the end. */
861 if (!table->lcl_head)
862 table->lcl_head = info;
865 else if (!table->lcl_head->prev_line
866 && table->lcl_head->address > address)
868 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
869 list and 2) the head of 'info'. */
870 info->prev_line = NULL;
871 table->lcl_head->prev_line = info;
874 else if (table->lcl_head->prev_line
875 && table->lcl_head->address > address
876 && address >= table->lcl_head->prev_line->address)
878 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
879 list and 2) the head of 'info'. */
880 info->prev_line = table->lcl_head->prev_line;
881 table->lcl_head->prev_line = info;
886 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
887 heads for 'info'. Reset 'lcl_head' and repeat. */
888 struct line_info* li2 = table->last_line; /* always non-NULL */
889 struct line_info* li1 = li2->prev_line;
893 if (li2->address > address && address >= li1->address)
896 li2 = li1; /* always non-NULL */
897 li1 = li1->prev_line;
899 table->lcl_head = li2;
902 /* Set member data of 'info'. */
903 info->address = address;
904 info->filename = filename;
906 info->column = column;
907 info->end_sequence = end_sequence;
910 /* Extract a fully qualified filename from a line info table.
911 The returned string has been xmalloc'ed. */
914 concat_filename (table, file)
915 struct line_info_table* table;
920 if (file - 1 >= table->num_files)
922 (*_bfd_error_handler)
923 (_("Dwarf Error: mangled line number section (bad file number)."));
924 return concat ("<unknown>");
927 filename = table->files[file - 1].name;
929 if (IS_ABSOLUTE_PATH (filename))
930 return concat (filename);
933 char* dirname = (table->files[file - 1].dir
934 ? table->dirs[table->files[file - 1].dir - 1]
937 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown. The
938 best we can do is return the filename part. */
940 return concat (filename);
942 return concat (dirname, "/", filename, NULL);
947 arange_add (unit, low_pc, high_pc)
948 struct comp_unit *unit;
952 struct arange *arange;
954 /* First see if we can cheaply extend an existing range. */
955 arange = &unit->arange;
959 if (low_pc == arange->high)
961 arange->high = high_pc;
964 if (high_pc == arange->low)
966 arange->low = low_pc;
969 arange = arange->next;
973 if (unit->arange.high == 0)
975 /* This is the first address range: store it in unit->arange. */
976 unit->arange.next = 0;
977 unit->arange.low = low_pc;
978 unit->arange.high = high_pc;
982 /* Need to allocate a new arange and insert it into the arange list. */
983 arange = (struct arange *)
984 bfd_zalloc (unit->abfd, (bfd_size_type) sizeof (*arange));
985 arange->low = low_pc;
986 arange->high = high_pc;
988 arange->next = unit->arange.next;
989 unit->arange.next = arange;
992 /* Decode the line number information for UNIT. */
994 static struct line_info_table*
995 decode_line_info (unit, stash)
996 struct comp_unit *unit;
997 struct dwarf2_debug *stash;
999 bfd *abfd = unit->abfd;
1000 struct line_info_table* table;
1003 struct line_head lh;
1004 unsigned int i, bytes_read, offset_size;
1005 char *cur_file, *cur_dir;
1006 unsigned char op_code, extended_op, adj_opcode;
1009 if (! stash->dwarf_line_buffer)
1013 msec = bfd_get_section_by_name (abfd, ".debug_line");
1016 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
1017 bfd_set_error (bfd_error_bad_value);
1021 stash->dwarf_line_size = msec->_raw_size;
1022 stash->dwarf_line_buffer
1023 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1025 if (! stash->dwarf_line_buffer)
1029 /* It is possible to get a bad value for the line_offset. Validate
1030 it here so that we won't get a segfault below. */
1031 if (unit->line_offset >= stash->dwarf_line_size)
1033 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
1034 unit->line_offset, stash->dwarf_line_size);
1035 bfd_set_error (bfd_error_bad_value);
1039 amt = sizeof (struct line_info_table);
1040 table = (struct line_info_table*) bfd_alloc (abfd, amt);
1042 table->comp_dir = unit->comp_dir;
1044 table->num_files = 0;
1045 table->files = NULL;
1047 table->num_dirs = 0;
1050 table->files = NULL;
1051 table->last_line = NULL;
1052 table->lcl_head = NULL;
1054 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1056 /* Read in the prologue. */
1057 lh.total_length = read_4_bytes (abfd, line_ptr);
1060 if (lh.total_length == 0xffffffff)
1062 lh.total_length = read_8_bytes (abfd, line_ptr);
1066 else if (lh.total_length == 0 && unit->addr_size == 8)
1068 /* Handle (non-standard) 64-bit DWARF2 formats. */
1069 lh.total_length = read_4_bytes (abfd, line_ptr);
1073 line_end = line_ptr + lh.total_length;
1074 lh.version = read_2_bytes (abfd, line_ptr);
1076 if (offset_size == 4)
1077 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1079 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1080 line_ptr += offset_size;
1081 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1083 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1085 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1087 lh.line_range = read_1_byte (abfd, line_ptr);
1089 lh.opcode_base = read_1_byte (abfd, line_ptr);
1091 amt = lh.opcode_base * sizeof (unsigned char);
1092 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
1094 lh.standard_opcode_lengths[0] = 1;
1096 for (i = 1; i < lh.opcode_base; ++i)
1098 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1102 /* Read directory table. */
1103 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1105 line_ptr += bytes_read;
1107 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1109 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1110 amt *= sizeof (char *);
1111 table->dirs = (char **) bfd_realloc (table->dirs, amt);
1116 table->dirs[table->num_dirs++] = cur_dir;
1119 line_ptr += bytes_read;
1121 /* Read file name table. */
1122 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1124 line_ptr += bytes_read;
1126 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1128 amt = table->num_files + FILE_ALLOC_CHUNK;
1129 amt *= sizeof (struct fileinfo);
1130 table->files = (struct fileinfo *) bfd_realloc (table->files, amt);
1135 table->files[table->num_files].name = cur_file;
1136 table->files[table->num_files].dir =
1137 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1138 line_ptr += bytes_read;
1139 table->files[table->num_files].time =
1140 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1141 line_ptr += bytes_read;
1142 table->files[table->num_files].size =
1143 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1144 line_ptr += bytes_read;
1148 line_ptr += bytes_read;
1150 /* Read the statement sequences until there's nothing left. */
1151 while (line_ptr < line_end)
1153 /* State machine registers. */
1154 bfd_vma address = 0;
1155 char * filename = concat_filename (table, 1);
1156 unsigned int line = 1;
1157 unsigned int column = 0;
1158 int is_stmt = lh.default_is_stmt;
1159 int basic_block = 0;
1160 int end_sequence = 0;
1161 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1162 compilers generate address sequences that are wildly out of
1163 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1164 for ia64-Linux). Thus, to determine the low and high
1165 address, we must compare on every DW_LNS_copy, etc. */
1167 bfd_vma high_pc = 0;
1169 /* Decode the table. */
1170 while (! end_sequence)
1172 op_code = read_1_byte (abfd, line_ptr);
1175 if (op_code >= lh.opcode_base)
1177 /* Special operand. */
1178 adj_opcode = op_code - lh.opcode_base;
1179 address += (adj_opcode / lh.line_range)
1180 * lh.minimum_instruction_length;
1181 line += lh.line_base + (adj_opcode % lh.line_range);
1182 /* Append row to matrix using current values. */
1183 add_line_info (table, address, filename, line, column, 0);
1185 if (low_pc == 0 || address < low_pc)
1187 if (address > high_pc)
1190 else switch (op_code)
1192 case DW_LNS_extended_op:
1193 /* Ignore length. */
1195 extended_op = read_1_byte (abfd, line_ptr);
1198 switch (extended_op)
1200 case DW_LNE_end_sequence:
1202 add_line_info (table, address, filename, line, column,
1204 if (low_pc == 0 || address < low_pc)
1206 if (address > high_pc)
1208 arange_add (unit, low_pc, high_pc);
1210 case DW_LNE_set_address:
1211 address = read_address (unit, line_ptr);
1212 line_ptr += unit->addr_size;
1214 case DW_LNE_define_file:
1215 cur_file = read_string (abfd, line_ptr, &bytes_read);
1216 line_ptr += bytes_read;
1217 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1219 amt = table->num_files + FILE_ALLOC_CHUNK;
1220 amt *= sizeof (struct fileinfo);
1222 (struct fileinfo *) bfd_realloc (table->files, amt);
1226 table->files[table->num_files].name = cur_file;
1227 table->files[table->num_files].dir =
1228 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1229 line_ptr += bytes_read;
1230 table->files[table->num_files].time =
1231 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1232 line_ptr += bytes_read;
1233 table->files[table->num_files].size =
1234 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1235 line_ptr += bytes_read;
1239 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1240 bfd_set_error (bfd_error_bad_value);
1245 add_line_info (table, address, filename, line, column, 0);
1247 if (low_pc == 0 || address < low_pc)
1249 if (address > high_pc)
1252 case DW_LNS_advance_pc:
1253 address += lh.minimum_instruction_length
1254 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1255 line_ptr += bytes_read;
1257 case DW_LNS_advance_line:
1258 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1259 line_ptr += bytes_read;
1261 case DW_LNS_set_file:
1265 /* The file and directory tables are 0
1266 based, the references are 1 based. */
1267 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1268 line_ptr += bytes_read;
1270 filename = concat_filename (table, file);
1273 case DW_LNS_set_column:
1274 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1275 line_ptr += bytes_read;
1277 case DW_LNS_negate_stmt:
1278 is_stmt = (!is_stmt);
1280 case DW_LNS_set_basic_block:
1283 case DW_LNS_const_add_pc:
1284 address += lh.minimum_instruction_length
1285 * ((255 - lh.opcode_base) / lh.line_range);
1287 case DW_LNS_fixed_advance_pc:
1288 address += read_2_bytes (abfd, line_ptr);
1295 /* Unknown standard opcode, ignore it. */
1296 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1298 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1299 line_ptr += bytes_read;
1311 /* If ADDR is within TABLE set the output parameters and return TRUE,
1312 otherwise return FALSE. The output parameters, FILENAME_PTR and
1313 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1316 lookup_address_in_line_info_table (table, addr, function, filename_ptr,
1318 struct line_info_table* table;
1320 struct funcinfo *function;
1321 const char **filename_ptr;
1322 unsigned int *linenumber_ptr;
1324 /* Note: table->last_line should be a descendingly sorted list. */
1325 struct line_info* next_line = table->last_line;
1326 struct line_info* each_line = NULL;
1327 *filename_ptr = NULL;
1332 each_line = next_line->prev_line;
1334 /* Check for large addresses */
1335 if (addr > next_line->address)
1336 each_line = NULL; /* ensure we skip over the normal case */
1338 /* Normal case: search the list; save */
1339 while (each_line && next_line)
1341 /* If we have an address match, save this info. This allows us
1342 to return as good as results as possible for strange debugging
1344 bfd_boolean addr_match = FALSE;
1345 if (each_line->address <= addr && addr <= next_line->address)
1349 /* If this line appears to span functions, and addr is in the
1350 later function, return the first line of that function instead
1351 of the last line of the earlier one. This check is for GCC
1352 2.95, which emits the first line number for a function late. */
1353 if (function != NULL
1354 && each_line->address < function->low
1355 && next_line->address > function->low)
1357 *filename_ptr = next_line->filename;
1358 *linenumber_ptr = next_line->line;
1362 *filename_ptr = each_line->filename;
1363 *linenumber_ptr = each_line->line;
1367 if (addr_match && !each_line->end_sequence)
1368 return TRUE; /* we have definitely found what we want */
1370 next_line = each_line;
1371 each_line = each_line->prev_line;
1374 /* At this point each_line is NULL but next_line is not. If we found
1375 a candidate end-of-sequence point in the loop above, we can return
1376 that (compatibility with a bug in the Intel compiler); otherwise,
1377 assuming that we found the containing function for this address in
1378 this compilation unit, return the first line we have a number for
1379 (compatibility with GCC 2.95). */
1380 if (*filename_ptr == NULL && function != NULL)
1382 *filename_ptr = next_line->filename;
1383 *linenumber_ptr = next_line->line;
1390 /* Function table functions. */
1392 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE. */
1395 lookup_address_in_function_table (table, addr, function_ptr,
1397 struct funcinfo* table;
1399 struct funcinfo** function_ptr;
1400 const char **functionname_ptr;
1402 struct funcinfo* each_func;
1404 for (each_func = table;
1406 each_func = each_func->prev_func)
1408 if (addr >= each_func->low && addr < each_func->high)
1410 *functionname_ptr = each_func->name;
1411 *function_ptr = each_func;
1419 /* DWARF2 Compilation unit functions. */
1421 /* Scan over each die in a comp. unit looking for functions to add
1422 to the function table. */
1425 scan_unit_for_functions (unit)
1426 struct comp_unit *unit;
1428 bfd *abfd = unit->abfd;
1429 char *info_ptr = unit->first_child_die_ptr;
1430 int nesting_level = 1;
1432 while (nesting_level)
1434 unsigned int abbrev_number, bytes_read, i;
1435 struct abbrev_info *abbrev;
1436 struct attribute attr;
1437 struct funcinfo *func;
1440 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1441 info_ptr += bytes_read;
1443 if (! abbrev_number)
1449 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1452 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1454 bfd_set_error (bfd_error_bad_value);
1458 if (abbrev->tag == DW_TAG_subprogram)
1460 bfd_size_type amt = sizeof (struct funcinfo);
1461 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
1462 func->prev_func = unit->function_table;
1463 unit->function_table = func;
1468 for (i = 0; i < abbrev->num_attrs; ++i)
1470 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1478 name = DW_STRING (&attr);
1480 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1481 if (func->name == NULL)
1482 func->name = DW_STRING (&attr);
1485 case DW_AT_MIPS_linkage_name:
1486 func->name = DW_STRING (&attr);
1490 func->low = DW_ADDR (&attr);
1494 func->high = DW_ADDR (&attr);
1506 name = DW_STRING (&attr);
1515 if (abbrev->has_children)
1522 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1523 includes the compilation unit header that proceeds the DIE's, but
1524 does not include the length field that preceeds each compilation
1525 unit header. END_PTR points one past the end of this comp unit.
1526 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1528 This routine does not read the whole compilation unit; only enough
1529 to get to the line number information for the compilation unit. */
1531 static struct comp_unit *
1532 parse_comp_unit (abfd, stash, unit_length, offset_size)
1534 struct dwarf2_debug *stash;
1535 bfd_vma unit_length;
1536 unsigned int offset_size;
1538 struct comp_unit* unit;
1539 unsigned int version;
1540 bfd_vma abbrev_offset = 0;
1541 unsigned int addr_size;
1542 struct abbrev_info** abbrevs;
1543 unsigned int abbrev_number, bytes_read, i;
1544 struct abbrev_info *abbrev;
1545 struct attribute attr;
1546 char *info_ptr = stash->info_ptr;
1547 char *end_ptr = info_ptr + unit_length;
1550 version = read_2_bytes (abfd, info_ptr);
1552 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1553 if (offset_size == 4)
1554 abbrev_offset = read_4_bytes (abfd, info_ptr);
1556 abbrev_offset = read_8_bytes (abfd, info_ptr);
1557 info_ptr += offset_size;
1558 addr_size = read_1_byte (abfd, info_ptr);
1563 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1564 bfd_set_error (bfd_error_bad_value);
1568 if (addr_size > sizeof (bfd_vma))
1570 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1572 (unsigned int) sizeof (bfd_vma));
1573 bfd_set_error (bfd_error_bad_value);
1577 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1579 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1580 bfd_set_error (bfd_error_bad_value);
1584 /* Read the abbrevs for this compilation unit into a table. */
1585 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1589 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1590 info_ptr += bytes_read;
1591 if (! abbrev_number)
1593 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1595 bfd_set_error (bfd_error_bad_value);
1599 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1602 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1604 bfd_set_error (bfd_error_bad_value);
1608 amt = sizeof (struct comp_unit);
1609 unit = (struct comp_unit*) bfd_zalloc (abfd, amt);
1611 unit->addr_size = addr_size;
1612 unit->offset_size = offset_size;
1613 unit->abbrevs = abbrevs;
1614 unit->end_ptr = end_ptr;
1615 unit->stash = stash;
1617 for (i = 0; i < abbrev->num_attrs; ++i)
1619 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1621 /* Store the data if it is of an attribute we want to keep in a
1622 partial symbol table. */
1625 case DW_AT_stmt_list:
1627 unit->line_offset = DW_UNSND (&attr);
1631 unit->name = DW_STRING (&attr);
1635 unit->arange.low = DW_ADDR (&attr);
1639 unit->arange.high = DW_ADDR (&attr);
1642 case DW_AT_comp_dir:
1644 char* comp_dir = DW_STRING (&attr);
1647 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1648 directory, get rid of it. */
1649 char *cp = (char*) strchr (comp_dir, ':');
1651 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1654 unit->comp_dir = comp_dir;
1663 unit->first_child_die_ptr = info_ptr;
1667 /* Return TRUE if UNIT contains the address given by ADDR. */
1670 comp_unit_contains_address (unit, addr)
1671 struct comp_unit* unit;
1674 struct arange *arange;
1679 arange = &unit->arange;
1682 if (addr >= arange->low && addr < arange->high)
1684 arange = arange->next;
1691 /* If UNIT contains ADDR, set the output parameters to the values for
1692 the line containing ADDR. The output parameters, FILENAME_PTR,
1693 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1696 Return TRUE if UNIT contains ADDR, and no errors were encountered;
1700 comp_unit_find_nearest_line (unit, addr, filename_ptr, functionname_ptr,
1701 linenumber_ptr, stash)
1702 struct comp_unit* unit;
1704 const char **filename_ptr;
1705 const char **functionname_ptr;
1706 unsigned int *linenumber_ptr;
1707 struct dwarf2_debug *stash;
1711 struct funcinfo *function;
1716 if (! unit->line_table)
1718 if (! unit->stmtlist)
1724 unit->line_table = decode_line_info (unit, stash);
1726 if (! unit->line_table)
1732 if (unit->first_child_die_ptr < unit->end_ptr
1733 && ! scan_unit_for_functions (unit))
1741 func_p = lookup_address_in_function_table (unit->function_table, addr,
1742 &function, functionname_ptr);
1743 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
1744 function, filename_ptr,
1746 return line_p || func_p;
1749 /* Locate a section in a BFD containing debugging info. The search starts
1750 from the section after AFTER_SEC, or from the first section in the BFD if
1751 AFTER_SEC is NULL. The search works by examining the names of the
1752 sections. There are two permissiable names. The first is .debug_info.
1753 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
1754 This is a variation on the .debug_info section which has a checksum
1755 describing the contents appended onto the name. This allows the linker to
1756 identify and discard duplicate debugging sections for different
1757 compilation units. */
1758 #define DWARF2_DEBUG_INFO ".debug_info"
1759 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1762 find_debug_info (abfd, after_sec)
1764 asection * after_sec;
1769 msec = after_sec->next;
1771 msec = abfd->sections;
1775 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1778 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1787 /* The DWARF2 version of find_nearest line. Return TRUE if the line
1788 is found without error. ADDR_SIZE is the number of bytes in the
1789 initial .debug_info length field and in the abbreviation offset.
1790 You may use zero to indicate that the default value should be
1794 _bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
1795 filename_ptr, functionname_ptr,
1796 linenumber_ptr, addr_size, pinfo)
1801 const char **filename_ptr;
1802 const char **functionname_ptr;
1803 unsigned int *linenumber_ptr;
1804 unsigned int addr_size;
1807 /* Read each compilation unit from the section .debug_info, and check
1808 to see if it contains the address we are searching for. If yes,
1809 lookup the address, and return the line number info. If no, go
1810 on to the next compilation unit.
1812 We keep a list of all the previously read compilation units, and
1813 a pointer to the next un-read compilation unit. Check the
1814 previously read units before reading more. */
1815 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
1817 /* What address are we looking for? */
1818 bfd_vma addr = offset + section->vma;
1820 struct comp_unit* each;
1822 *filename_ptr = NULL;
1823 *functionname_ptr = NULL;
1824 *linenumber_ptr = 0;
1826 /* The DWARF2 spec says that the initial length field, and the
1827 offset of the abbreviation table, should both be 4-byte values.
1828 However, some compilers do things differently. */
1831 BFD_ASSERT (addr_size == 4 || addr_size == 8);
1835 bfd_size_type total_size;
1837 bfd_size_type amt = sizeof (struct dwarf2_debug);
1839 stash = (struct dwarf2_debug*) bfd_zalloc (abfd, amt);
1843 *pinfo = (PTR) stash;
1845 msec = find_debug_info (abfd, NULL);
1847 /* No dwarf2 info. Note that at this point the stash
1848 has been allocated, but contains zeros, this lets
1849 future calls to this function fail quicker. */
1852 /* There can be more than one DWARF2 info section in a BFD these days.
1853 Read them all in and produce one large stash. We do this in two
1854 passes - in the first pass we just accumulate the section sizes.
1855 In the second pass we read in the section's contents. The allows
1856 us to avoid reallocing the data as we add sections to the stash. */
1857 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1858 total_size += msec->_raw_size;
1860 stash->info_ptr = (char *) bfd_alloc (abfd, total_size);
1861 if (stash->info_ptr == NULL)
1864 stash->info_ptr_end = stash->info_ptr;
1866 for (msec = find_debug_info (abfd, NULL);
1868 msec = find_debug_info (abfd, msec))
1871 bfd_size_type start;
1873 size = msec->_raw_size;
1877 start = stash->info_ptr_end - stash->info_ptr;
1879 if ((bfd_simple_get_relocated_section_contents
1880 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
1883 stash->info_ptr_end = stash->info_ptr + start + size;
1886 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1888 stash->sec = find_debug_info (abfd, NULL);
1889 stash->sec_info_ptr = stash->info_ptr;
1890 stash->syms = symbols;
1893 /* A null info_ptr indicates that there is no dwarf2 info
1894 (or that an error occured while setting up the stash). */
1895 if (! stash->info_ptr)
1898 /* Check the previously read comp. units first. */
1899 for (each = stash->all_comp_units; each; each = each->next_unit)
1900 if (comp_unit_contains_address (each, addr))
1901 return comp_unit_find_nearest_line (each, addr, filename_ptr,
1902 functionname_ptr, linenumber_ptr,
1905 /* Read each remaining comp. units checking each as they are read. */
1906 while (stash->info_ptr < stash->info_ptr_end)
1910 unsigned int offset_size = addr_size;
1914 length = read_4_bytes (abfd, stash->info_ptr);
1915 if (length == 0xffffffff)
1918 length = read_8_bytes (abfd, stash->info_ptr + 4);
1919 stash->info_ptr += 8;
1921 else if (length == 0)
1923 /* Handle (non-standard) 64-bit DWARF2 formats. */
1925 length = read_4_bytes (abfd, stash->info_ptr + 4);
1926 stash->info_ptr += 4;
1930 length = read_8_bytes (abfd, stash->info_ptr);
1931 stash->info_ptr += addr_size;
1935 each = parse_comp_unit (abfd, stash, length, offset_size);
1936 stash->info_ptr += length;
1938 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
1939 == stash->sec->_raw_size)
1941 stash->sec = find_debug_info (abfd, stash->sec);
1942 stash->sec_info_ptr = stash->info_ptr;
1947 each->next_unit = stash->all_comp_units;
1948 stash->all_comp_units = each;
1950 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1951 compilation units. If we don't have them (i.e.,
1952 unit->high == 0), we need to consult the line info
1953 table to see if a compilation unit contains the given
1955 if (each->arange.high > 0)
1957 if (comp_unit_contains_address (each, addr))
1958 return comp_unit_find_nearest_line (each, addr,
1966 found = comp_unit_find_nearest_line (each, addr,