* config/tc-avr.c (mcu_types): Add atmega16u4.
[binutils.git] / bfd / dwarf2.c
blob73b7b56cc5044c6d6e786620445a8c0903039387
1 /* DWARF 2 support.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
6 (gavin@cygnus.com).
8 From the dwarf2read.c header:
9 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10 Inc. with support from Florida State University (under contract
11 with the Ada Joint Program Office), and Silicon Graphics, Inc.
12 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14 support in dwarfread.c
16 This file is part of BFD.
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 3 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,
31 MA 02110-1301, USA. */
33 #include "sysdep.h"
34 #include "bfd.h"
35 #include "libiberty.h"
36 #include "libbfd.h"
37 #include "elf-bfd.h"
38 #include "elf/dwarf2.h"
40 /* The data in the .debug_line statement prologue looks like this. */
42 struct line_head
44 bfd_vma total_length;
45 unsigned short version;
46 bfd_vma prologue_length;
47 unsigned char minimum_instruction_length;
48 unsigned char default_is_stmt;
49 int line_base;
50 unsigned char line_range;
51 unsigned char opcode_base;
52 unsigned char *standard_opcode_lengths;
55 /* Attributes have a name and a value. */
57 struct attribute
59 enum dwarf_attribute name;
60 enum dwarf_form form;
61 union
63 char *str;
64 struct dwarf_block *blk;
65 bfd_uint64_t val;
66 bfd_int64_t sval;
71 /* Blocks are a bunch of untyped bytes. */
72 struct dwarf_block
74 unsigned int size;
75 bfd_byte *data;
78 struct loadable_section
80 asection *section;
81 bfd_vma adj_vma;
84 struct dwarf2_debug
86 /* A list of all previously read comp_units. */
87 struct comp_unit *all_comp_units;
89 /* Last comp unit in list above. */
90 struct comp_unit *last_comp_unit;
92 /* The next unread compilation unit within the .debug_info section.
93 Zero indicates that the .debug_info section has not been loaded
94 into a buffer yet. */
95 bfd_byte *info_ptr;
97 /* Pointer to the end of the .debug_info section memory buffer. */
98 bfd_byte *info_ptr_end;
100 /* Pointer to the bfd, section and address of the beginning of the
101 section. The bfd might be different than expected because of
102 gnu_debuglink sections. */
103 bfd * bfd;
104 asection *sec;
105 bfd_byte *sec_info_ptr;
107 /* A pointer to the memory block allocated for info_ptr. Neither
108 info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
109 beginning of the malloc block. This is used only to free the
110 memory later. */
111 bfd_byte *info_ptr_memory;
113 /* Pointer to the symbol table. */
114 asymbol **syms;
116 /* Pointer to the .debug_abbrev section loaded into memory. */
117 bfd_byte *dwarf_abbrev_buffer;
119 /* Length of the loaded .debug_abbrev section. */
120 bfd_size_type dwarf_abbrev_size;
122 /* Buffer for decode_line_info. */
123 bfd_byte *dwarf_line_buffer;
125 /* Length of the loaded .debug_line section. */
126 bfd_size_type dwarf_line_size;
128 /* Pointer to the .debug_str section loaded into memory. */
129 bfd_byte *dwarf_str_buffer;
131 /* Length of the loaded .debug_str section. */
132 bfd_size_type dwarf_str_size;
134 /* Pointer to the .debug_ranges section loaded into memory. */
135 bfd_byte *dwarf_ranges_buffer;
137 /* Length of the loaded .debug_ranges section. */
138 bfd_size_type dwarf_ranges_size;
140 /* If the most recent call to bfd_find_nearest_line was given an
141 address in an inlined function, preserve a pointer into the
142 calling chain for subsequent calls to bfd_find_inliner_info to
143 use. */
144 struct funcinfo *inliner_chain;
146 /* Number of loadable sections. */
147 unsigned int loadable_section_count;
149 /* Array of loadable sections. */
150 struct loadable_section *loadable_sections;
152 /* Number of times find_line is called. This is used in
153 the heuristic for enabling the info hash tables. */
154 int info_hash_count;
156 #define STASH_INFO_HASH_TRIGGER 100
158 /* Hash table mapping symbol names to function infos. */
159 struct info_hash_table *funcinfo_hash_table;
161 /* Hash table mapping symbol names to variable infos. */
162 struct info_hash_table *varinfo_hash_table;
164 /* Head of comp_unit list in the last hash table update. */
165 struct comp_unit *hash_units_head;
167 /* Status of info hash. */
168 int info_hash_status;
169 #define STASH_INFO_HASH_OFF 0
170 #define STASH_INFO_HASH_ON 1
171 #define STASH_INFO_HASH_DISABLED 2
174 struct arange
176 struct arange *next;
177 bfd_vma low;
178 bfd_vma high;
181 /* A minimal decoding of DWARF2 compilation units. We only decode
182 what's needed to get to the line number information. */
184 struct comp_unit
186 /* Chain the previously read compilation units. */
187 struct comp_unit *next_unit;
189 /* Likewise, chain the compilation unit read after this one.
190 The comp units are stored in reversed reading order. */
191 struct comp_unit *prev_unit;
193 /* Keep the bfd convenient (for memory allocation). */
194 bfd *abfd;
196 /* The lowest and highest addresses contained in this compilation
197 unit as specified in the compilation unit header. */
198 struct arange arange;
200 /* The DW_AT_name attribute (for error messages). */
201 char *name;
203 /* The abbrev hash table. */
204 struct abbrev_info **abbrevs;
206 /* Note that an error was found by comp_unit_find_nearest_line. */
207 int error;
209 /* The DW_AT_comp_dir attribute. */
210 char *comp_dir;
212 /* TRUE if there is a line number table associated with this comp. unit. */
213 int stmtlist;
215 /* Pointer to the current comp_unit so that we can find a given entry
216 by its reference. */
217 bfd_byte *info_ptr_unit;
219 /* The offset into .debug_line of the line number table. */
220 unsigned long line_offset;
222 /* Pointer to the first child die for the comp unit. */
223 bfd_byte *first_child_die_ptr;
225 /* The end of the comp unit. */
226 bfd_byte *end_ptr;
228 /* The decoded line number, NULL if not yet decoded. */
229 struct line_info_table *line_table;
231 /* A list of the functions found in this comp. unit. */
232 struct funcinfo *function_table;
234 /* A list of the variables found in this comp. unit. */
235 struct varinfo *variable_table;
237 /* Pointer to dwarf2_debug structure. */
238 struct dwarf2_debug *stash;
240 /* Address size for this unit - from unit header. */
241 unsigned char addr_size;
243 /* Offset size for this unit - from unit header. */
244 unsigned char offset_size;
246 /* Base address for this unit - from DW_AT_low_pc attribute of
247 DW_TAG_compile_unit DIE */
248 bfd_vma base_address;
250 /* TRUE if symbols are cached in hash table for faster lookup by name. */
251 bfd_boolean cached;
254 /* This data structure holds the information of an abbrev. */
255 struct abbrev_info
257 unsigned int number; /* Number identifying abbrev. */
258 enum dwarf_tag tag; /* DWARF tag. */
259 int has_children; /* Boolean. */
260 unsigned int num_attrs; /* Number of attributes. */
261 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
262 struct abbrev_info *next; /* Next in chain. */
265 struct attr_abbrev
267 enum dwarf_attribute name;
268 enum dwarf_form form;
271 #ifndef ABBREV_HASH_SIZE
272 #define ABBREV_HASH_SIZE 121
273 #endif
274 #ifndef ATTR_ALLOC_CHUNK
275 #define ATTR_ALLOC_CHUNK 4
276 #endif
278 /* Variable and function hash tables. This is used to speed up look-up
279 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
280 In order to share code between variable and function infos, we use
281 a list of untyped pointer for all variable/function info associated with
282 a symbol. We waste a bit of memory for list with one node but that
283 simplifies the code. */
285 struct info_list_node
287 struct info_list_node *next;
288 void *info;
291 /* Info hash entry. */
292 struct info_hash_entry
294 struct bfd_hash_entry root;
295 struct info_list_node *head;
298 struct info_hash_table
300 struct bfd_hash_table base;
303 /* Function to create a new entry in info hash table. */
305 static struct bfd_hash_entry *
306 info_hash_table_newfunc (struct bfd_hash_entry *entry,
307 struct bfd_hash_table *table,
308 const char *string)
310 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
312 /* Allocate the structure if it has not already been allocated by a
313 derived class. */
314 if (ret == NULL)
316 ret = bfd_hash_allocate (table, sizeof (* ret));
317 if (ret == NULL)
318 return NULL;
321 /* Call the allocation method of the base class. */
322 ret = ((struct info_hash_entry *)
323 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
325 /* Initialize the local fields here. */
326 if (ret)
327 ret->head = NULL;
329 return (struct bfd_hash_entry *) ret;
332 /* Function to create a new info hash table. It returns a pointer to the
333 newly created table or NULL if there is any error. We need abfd
334 solely for memory allocation. */
336 static struct info_hash_table *
337 create_info_hash_table (bfd *abfd)
339 struct info_hash_table *hash_table;
341 hash_table = bfd_alloc (abfd, sizeof (struct info_hash_table));
342 if (!hash_table)
343 return hash_table;
345 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
346 sizeof (struct info_hash_entry)))
348 bfd_release (abfd, hash_table);
349 return NULL;
352 return hash_table;
355 /* Insert an info entry into an info hash table. We do not check of
356 duplicate entries. Also, the caller need to guarantee that the
357 right type of info in inserted as info is passed as a void* pointer.
358 This function returns true if there is no error. */
360 static bfd_boolean
361 insert_info_hash_table (struct info_hash_table *hash_table,
362 const char *key,
363 void *info,
364 bfd_boolean copy_p)
366 struct info_hash_entry *entry;
367 struct info_list_node *node;
369 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
370 key, TRUE, copy_p);
371 if (!entry)
372 return FALSE;
374 node = bfd_hash_allocate (&hash_table->base, sizeof (*node));
375 if (!node)
376 return FALSE;
378 node->info = info;
379 node->next = entry->head;
380 entry->head = node;
382 return TRUE;
385 /* Look up an info entry list from an info hash table. Return NULL
386 if there is none. */
388 static struct info_list_node *
389 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
391 struct info_hash_entry *entry;
393 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
394 FALSE, FALSE);
395 return entry ? entry->head : NULL;
398 /* Read a section into its appropriate place in the dwarf2_debug
399 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If syms is
400 not NULL, use bfd_simple_get_relocated_section_contents to read the
401 section contents, otherwise use bfd_get_section_contents. */
403 static bfd_boolean
404 read_section (bfd *abfd,
405 const char* section_name, const char* compressed_section_name,
406 asymbol** syms, bfd_uint64_t offset,
407 bfd_byte **section_buffer, bfd_size_type *section_size)
409 asection *msec;
410 bfd_boolean section_is_compressed = FALSE;
412 /* read_section is a noop if the section has already been read. */
413 if (*section_buffer)
414 return TRUE;
416 msec = bfd_get_section_by_name (abfd, section_name);
417 if (! msec && compressed_section_name)
419 msec = bfd_get_section_by_name (abfd, compressed_section_name);
420 section_is_compressed = TRUE;
422 if (! msec)
424 (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."), section_name);
425 bfd_set_error (bfd_error_bad_value);
426 return FALSE;
429 if (syms)
431 *section_size = msec->size;
432 *section_buffer
433 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
434 if (! *section_buffer)
435 return FALSE;
437 else
439 *section_size = msec->rawsize ? msec->rawsize : msec->size;
440 *section_buffer = bfd_malloc (*section_size);
441 if (! *section_buffer)
442 return FALSE;
443 if (! bfd_get_section_contents (abfd, msec, *section_buffer,
444 0, *section_size))
445 return FALSE;
448 if (section_is_compressed)
450 if (! bfd_uncompress_section_contents (section_buffer, section_size))
452 (*_bfd_error_handler) (_("Dwarf Error: unable to decompress %s section."), compressed_section_name);
453 bfd_set_error (bfd_error_bad_value);
454 return FALSE;
458 /* It is possible to get a bad value for the offset into the section
459 * that the client wants. Validate it here to avoid trouble later. */
460 if (offset != 0 && offset >= *section_size)
462 (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu) greater than or equal to %s size (%lu)."),
463 offset, section_name, *section_size);
464 bfd_set_error (bfd_error_bad_value);
465 return FALSE;
468 return TRUE;
471 /* VERBATIM
472 The following function up to the END VERBATIM mark are
473 copied directly from dwarf2read.c. */
475 /* Read dwarf information from a buffer. */
477 static unsigned int
478 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
480 return bfd_get_8 (abfd, buf);
483 static int
484 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
486 return bfd_get_signed_8 (abfd, buf);
489 static unsigned int
490 read_2_bytes (bfd *abfd, bfd_byte *buf)
492 return bfd_get_16 (abfd, buf);
495 static unsigned int
496 read_4_bytes (bfd *abfd, bfd_byte *buf)
498 return bfd_get_32 (abfd, buf);
501 static bfd_uint64_t
502 read_8_bytes (bfd *abfd, bfd_byte *buf)
504 return bfd_get_64 (abfd, buf);
507 static bfd_byte *
508 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
509 bfd_byte *buf,
510 unsigned int size ATTRIBUTE_UNUSED)
512 /* If the size of a host char is 8 bits, we can return a pointer
513 to the buffer, otherwise we have to copy the data to a buffer
514 allocated on the temporary obstack. */
515 return buf;
518 static char *
519 read_string (bfd *abfd ATTRIBUTE_UNUSED,
520 bfd_byte *buf,
521 unsigned int *bytes_read_ptr)
523 /* Return a pointer to the embedded string. */
524 char *str = (char *) buf;
525 if (*str == '\0')
527 *bytes_read_ptr = 1;
528 return NULL;
531 *bytes_read_ptr = strlen (str) + 1;
532 return str;
535 static char *
536 read_indirect_string (struct comp_unit* unit,
537 bfd_byte *buf,
538 unsigned int *bytes_read_ptr)
540 bfd_uint64_t offset;
541 struct dwarf2_debug *stash = unit->stash;
542 char *str;
544 if (unit->offset_size == 4)
545 offset = read_4_bytes (unit->abfd, buf);
546 else
547 offset = read_8_bytes (unit->abfd, buf);
548 *bytes_read_ptr = unit->offset_size;
550 if (! read_section (unit->abfd, ".debug_str", ".zdebug_str",
551 0, offset,
552 &stash->dwarf_str_buffer, &stash->dwarf_str_size))
553 return 0;
555 str = (char *) stash->dwarf_str_buffer + offset;
556 if (*str == '\0')
557 return NULL;
558 return str;
561 /* END VERBATIM */
563 static bfd_uint64_t
564 read_address (struct comp_unit *unit, bfd_byte *buf)
566 int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
568 if (signed_vma)
570 switch (unit->addr_size)
572 case 8:
573 return bfd_get_signed_64 (unit->abfd, buf);
574 case 4:
575 return bfd_get_signed_32 (unit->abfd, buf);
576 case 2:
577 return bfd_get_signed_16 (unit->abfd, buf);
578 default:
579 abort ();
582 else
584 switch (unit->addr_size)
586 case 8:
587 return bfd_get_64 (unit->abfd, buf);
588 case 4:
589 return bfd_get_32 (unit->abfd, buf);
590 case 2:
591 return bfd_get_16 (unit->abfd, buf);
592 default:
593 abort ();
598 /* Lookup an abbrev_info structure in the abbrev hash table. */
600 static struct abbrev_info *
601 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
603 unsigned int hash_number;
604 struct abbrev_info *abbrev;
606 hash_number = number % ABBREV_HASH_SIZE;
607 abbrev = abbrevs[hash_number];
609 while (abbrev)
611 if (abbrev->number == number)
612 return abbrev;
613 else
614 abbrev = abbrev->next;
617 return NULL;
620 /* In DWARF version 2, the description of the debugging information is
621 stored in a separate .debug_abbrev section. Before we read any
622 dies from a section we read in all abbreviations and install them
623 in a hash table. */
625 static struct abbrev_info**
626 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
628 struct abbrev_info **abbrevs;
629 bfd_byte *abbrev_ptr;
630 struct abbrev_info *cur_abbrev;
631 unsigned int abbrev_number, bytes_read, abbrev_name;
632 unsigned int abbrev_form, hash_number;
633 bfd_size_type amt;
635 if (! read_section (abfd, ".debug_abbrev", ".zdebug_abbrev",
636 stash->syms, offset,
637 &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
638 return 0;
640 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
641 abbrevs = bfd_zalloc (abfd, amt);
643 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
644 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
645 abbrev_ptr += bytes_read;
647 /* Loop until we reach an abbrev number of 0. */
648 while (abbrev_number)
650 amt = sizeof (struct abbrev_info);
651 cur_abbrev = bfd_zalloc (abfd, amt);
653 /* Read in abbrev header. */
654 cur_abbrev->number = abbrev_number;
655 cur_abbrev->tag = (enum dwarf_tag)
656 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
657 abbrev_ptr += bytes_read;
658 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
659 abbrev_ptr += 1;
661 /* Now read in declarations. */
662 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
663 abbrev_ptr += bytes_read;
664 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
665 abbrev_ptr += bytes_read;
667 while (abbrev_name)
669 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
671 struct attr_abbrev *tmp;
673 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
674 amt *= sizeof (struct attr_abbrev);
675 tmp = bfd_realloc (cur_abbrev->attrs, amt);
676 if (tmp == NULL)
678 size_t i;
680 for (i = 0; i < ABBREV_HASH_SIZE; i++)
682 struct abbrev_info *abbrev = abbrevs[i];
684 while (abbrev)
686 free (abbrev->attrs);
687 abbrev = abbrev->next;
690 return NULL;
692 cur_abbrev->attrs = tmp;
695 cur_abbrev->attrs[cur_abbrev->num_attrs].name
696 = (enum dwarf_attribute) abbrev_name;
697 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
698 = (enum dwarf_form) abbrev_form;
699 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
700 abbrev_ptr += bytes_read;
701 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
702 abbrev_ptr += bytes_read;
705 hash_number = abbrev_number % ABBREV_HASH_SIZE;
706 cur_abbrev->next = abbrevs[hash_number];
707 abbrevs[hash_number] = cur_abbrev;
709 /* Get next abbreviation.
710 Under Irix6 the abbreviations for a compilation unit are not
711 always properly terminated with an abbrev number of 0.
712 Exit loop if we encounter an abbreviation which we have
713 already read (which means we are about to read the abbreviations
714 for the next compile unit) or if the end of the abbreviation
715 table is reached. */
716 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
717 >= stash->dwarf_abbrev_size)
718 break;
719 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
720 abbrev_ptr += bytes_read;
721 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
722 break;
725 return abbrevs;
728 /* Read an attribute value described by an attribute form. */
730 static bfd_byte *
731 read_attribute_value (struct attribute *attr,
732 unsigned form,
733 struct comp_unit *unit,
734 bfd_byte *info_ptr)
736 bfd *abfd = unit->abfd;
737 unsigned int bytes_read;
738 struct dwarf_block *blk;
739 bfd_size_type amt;
741 attr->form = (enum dwarf_form) form;
743 switch (form)
745 case DW_FORM_addr:
746 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
747 case DW_FORM_ref_addr:
748 attr->u.val = read_address (unit, info_ptr);
749 info_ptr += unit->addr_size;
750 break;
751 case DW_FORM_block2:
752 amt = sizeof (struct dwarf_block);
753 blk = bfd_alloc (abfd, amt);
754 blk->size = read_2_bytes (abfd, info_ptr);
755 info_ptr += 2;
756 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
757 info_ptr += blk->size;
758 attr->u.blk = blk;
759 break;
760 case DW_FORM_block4:
761 amt = sizeof (struct dwarf_block);
762 blk = bfd_alloc (abfd, amt);
763 blk->size = read_4_bytes (abfd, info_ptr);
764 info_ptr += 4;
765 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
766 info_ptr += blk->size;
767 attr->u.blk = blk;
768 break;
769 case DW_FORM_data2:
770 attr->u.val = read_2_bytes (abfd, info_ptr);
771 info_ptr += 2;
772 break;
773 case DW_FORM_data4:
774 attr->u.val = read_4_bytes (abfd, info_ptr);
775 info_ptr += 4;
776 break;
777 case DW_FORM_data8:
778 attr->u.val = read_8_bytes (abfd, info_ptr);
779 info_ptr += 8;
780 break;
781 case DW_FORM_string:
782 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
783 info_ptr += bytes_read;
784 break;
785 case DW_FORM_strp:
786 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
787 info_ptr += bytes_read;
788 break;
789 case DW_FORM_block:
790 amt = sizeof (struct dwarf_block);
791 blk = bfd_alloc (abfd, amt);
792 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
793 info_ptr += bytes_read;
794 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
795 info_ptr += blk->size;
796 attr->u.blk = blk;
797 break;
798 case DW_FORM_block1:
799 amt = sizeof (struct dwarf_block);
800 blk = bfd_alloc (abfd, amt);
801 blk->size = read_1_byte (abfd, info_ptr);
802 info_ptr += 1;
803 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
804 info_ptr += blk->size;
805 attr->u.blk = blk;
806 break;
807 case DW_FORM_data1:
808 attr->u.val = read_1_byte (abfd, info_ptr);
809 info_ptr += 1;
810 break;
811 case DW_FORM_flag:
812 attr->u.val = read_1_byte (abfd, info_ptr);
813 info_ptr += 1;
814 break;
815 case DW_FORM_sdata:
816 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
817 info_ptr += bytes_read;
818 break;
819 case DW_FORM_udata:
820 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
821 info_ptr += bytes_read;
822 break;
823 case DW_FORM_ref1:
824 attr->u.val = read_1_byte (abfd, info_ptr);
825 info_ptr += 1;
826 break;
827 case DW_FORM_ref2:
828 attr->u.val = read_2_bytes (abfd, info_ptr);
829 info_ptr += 2;
830 break;
831 case DW_FORM_ref4:
832 attr->u.val = read_4_bytes (abfd, info_ptr);
833 info_ptr += 4;
834 break;
835 case DW_FORM_ref8:
836 attr->u.val = read_8_bytes (abfd, info_ptr);
837 info_ptr += 8;
838 break;
839 case DW_FORM_ref_udata:
840 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
841 info_ptr += bytes_read;
842 break;
843 case DW_FORM_indirect:
844 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
845 info_ptr += bytes_read;
846 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
847 break;
848 default:
849 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
850 form);
851 bfd_set_error (bfd_error_bad_value);
853 return info_ptr;
856 /* Read an attribute described by an abbreviated attribute. */
858 static bfd_byte *
859 read_attribute (struct attribute *attr,
860 struct attr_abbrev *abbrev,
861 struct comp_unit *unit,
862 bfd_byte *info_ptr)
864 attr->name = abbrev->name;
865 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
866 return info_ptr;
869 /* Source line information table routines. */
871 #define FILE_ALLOC_CHUNK 5
872 #define DIR_ALLOC_CHUNK 5
874 struct line_info
876 struct line_info* prev_line;
877 bfd_vma address;
878 char *filename;
879 unsigned int line;
880 unsigned int column;
881 int end_sequence; /* End of (sequential) code sequence. */
884 struct fileinfo
886 char *name;
887 unsigned int dir;
888 unsigned int time;
889 unsigned int size;
892 struct line_info_table
894 bfd* abfd;
895 unsigned int num_files;
896 unsigned int num_dirs;
897 char *comp_dir;
898 char **dirs;
899 struct fileinfo* files;
900 struct line_info* last_line; /* largest VMA */
901 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
904 /* Remember some information about each function. If the function is
905 inlined (DW_TAG_inlined_subroutine) it may have two additional
906 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
907 source code location where this function was inlined. */
909 struct funcinfo
911 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */
912 struct funcinfo *caller_func; /* Pointer to function one scope higher */
913 char *caller_file; /* Source location file name where caller_func inlines this func */
914 int caller_line; /* Source location line number where caller_func inlines this func */
915 char *file; /* Source location file name */
916 int line; /* Source location line number */
917 int tag;
918 char *name;
919 struct arange arange;
920 asection *sec; /* Where the symbol is defined */
923 struct varinfo
925 /* Pointer to previous variable in list of all variables */
926 struct varinfo *prev_var;
927 /* Source location file name */
928 char *file;
929 /* Source location line number */
930 int line;
931 int tag;
932 char *name;
933 bfd_vma addr;
934 /* Where the symbol is defined */
935 asection *sec;
936 /* Is this a stack variable? */
937 unsigned int stack: 1;
940 /* Return TRUE if NEW_LINE should sort after LINE. */
942 static inline bfd_boolean
943 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
945 return (new_line->address > line->address
946 || (new_line->address == line->address
947 && new_line->end_sequence < line->end_sequence));
951 /* Adds a new entry to the line_info list in the line_info_table, ensuring
952 that the list is sorted. Note that the line_info list is sorted from
953 highest to lowest VMA (with possible duplicates); that is,
954 line_info->prev_line always accesses an equal or smaller VMA. */
956 static void
957 add_line_info (struct line_info_table *table,
958 bfd_vma address,
959 char *filename,
960 unsigned int line,
961 unsigned int column,
962 int end_sequence)
964 bfd_size_type amt = sizeof (struct line_info);
965 struct line_info* info = bfd_alloc (table->abfd, amt);
967 /* Set member data of 'info'. */
968 info->address = address;
969 info->line = line;
970 info->column = column;
971 info->end_sequence = end_sequence;
973 if (filename && filename[0])
975 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
976 if (info->filename)
977 strcpy (info->filename, filename);
979 else
980 info->filename = NULL;
982 /* Find the correct location for 'info'. Normally we will receive
983 new line_info data 1) in order and 2) with increasing VMAs.
984 However some compilers break the rules (cf. decode_line_info) and
985 so we include some heuristics for quickly finding the correct
986 location for 'info'. In particular, these heuristics optimize for
987 the common case in which the VMA sequence that we receive is a
988 list of locally sorted VMAs such as
989 p...z a...j (where a < j < p < z)
991 Note: table->lcl_head is used to head an *actual* or *possible*
992 sequence within the list (such as a...j) that is not directly
993 headed by table->last_line
995 Note: we may receive duplicate entries from 'decode_line_info'. */
997 if (table->last_line
998 && table->last_line->address == address
999 && table->last_line->end_sequence == end_sequence)
1001 /* We only keep the last entry with the same address and end
1002 sequence. See PR ld/4986. */
1003 if (table->lcl_head == table->last_line)
1004 table->lcl_head = info;
1005 info->prev_line = table->last_line->prev_line;
1006 table->last_line = info;
1008 else if (!table->last_line
1009 || new_line_sorts_after (info, table->last_line))
1011 /* Normal case: add 'info' to the beginning of the list */
1012 info->prev_line = table->last_line;
1013 table->last_line = info;
1015 /* lcl_head: initialize to head a *possible* sequence at the end. */
1016 if (!table->lcl_head)
1017 table->lcl_head = info;
1019 else if (!new_line_sorts_after (info, table->lcl_head)
1020 && (!table->lcl_head->prev_line
1021 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1023 /* Abnormal but easy: lcl_head is the head of 'info'. */
1024 info->prev_line = table->lcl_head->prev_line;
1025 table->lcl_head->prev_line = info;
1027 else
1029 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
1030 heads for 'info'. Reset 'lcl_head'. */
1031 struct line_info* li2 = table->last_line; /* always non-NULL */
1032 struct line_info* li1 = li2->prev_line;
1034 while (li1)
1036 if (!new_line_sorts_after (info, li2)
1037 && new_line_sorts_after (info, li1))
1038 break;
1040 li2 = li1; /* always non-NULL */
1041 li1 = li1->prev_line;
1043 table->lcl_head = li2;
1044 info->prev_line = table->lcl_head->prev_line;
1045 table->lcl_head->prev_line = info;
1049 /* Extract a fully qualified filename from a line info table.
1050 The returned string has been malloc'ed and it is the caller's
1051 responsibility to free it. */
1053 static char *
1054 concat_filename (struct line_info_table *table, unsigned int file)
1056 char *filename;
1058 if (file - 1 >= table->num_files)
1060 /* FILE == 0 means unknown. */
1061 if (file)
1062 (*_bfd_error_handler)
1063 (_("Dwarf Error: mangled line number section (bad file number)."));
1064 return strdup ("<unknown>");
1067 filename = table->files[file - 1].name;
1069 if (!IS_ABSOLUTE_PATH (filename))
1071 char *dirname = NULL;
1072 char *subdirname = NULL;
1073 char *name;
1074 size_t len;
1076 if (table->files[file - 1].dir)
1077 subdirname = table->dirs[table->files[file - 1].dir - 1];
1079 if (!subdirname || !IS_ABSOLUTE_PATH (subdirname))
1080 dirname = table->comp_dir;
1082 if (!dirname)
1084 dirname = subdirname;
1085 subdirname = NULL;
1088 if (!dirname)
1089 return strdup (filename);
1091 len = strlen (dirname) + strlen (filename) + 2;
1093 if (subdirname)
1095 len += strlen (subdirname) + 1;
1096 name = bfd_malloc (len);
1097 if (name)
1098 sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
1100 else
1102 name = bfd_malloc (len);
1103 if (name)
1104 sprintf (name, "%s/%s", dirname, filename);
1107 return name;
1110 return strdup (filename);
1113 static void
1114 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
1116 struct arange *arange;
1118 /* If the first arange is empty, use it. */
1119 if (first_arange->high == 0)
1121 first_arange->low = low_pc;
1122 first_arange->high = high_pc;
1123 return;
1126 /* Next see if we can cheaply extend an existing range. */
1127 arange = first_arange;
1130 if (low_pc == arange->high)
1132 arange->high = high_pc;
1133 return;
1135 if (high_pc == arange->low)
1137 arange->low = low_pc;
1138 return;
1140 arange = arange->next;
1142 while (arange);
1144 /* Need to allocate a new arange and insert it into the arange list.
1145 Order isn't significant, so just insert after the first arange. */
1146 arange = bfd_zalloc (abfd, sizeof (*arange));
1147 arange->low = low_pc;
1148 arange->high = high_pc;
1149 arange->next = first_arange->next;
1150 first_arange->next = arange;
1153 /* Decode the line number information for UNIT. */
1155 static struct line_info_table*
1156 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
1158 bfd *abfd = unit->abfd;
1159 struct line_info_table* table;
1160 bfd_byte *line_ptr;
1161 bfd_byte *line_end;
1162 struct line_head lh;
1163 unsigned int i, bytes_read, offset_size;
1164 char *cur_file, *cur_dir;
1165 unsigned char op_code, extended_op, adj_opcode;
1166 bfd_size_type amt;
1168 if (! read_section (abfd, ".debug_line", ".zdebug_line",
1169 stash->syms, unit->line_offset,
1170 &stash->dwarf_line_buffer, &stash->dwarf_line_size))
1171 return 0;
1173 amt = sizeof (struct line_info_table);
1174 table = bfd_alloc (abfd, amt);
1175 table->abfd = abfd;
1176 table->comp_dir = unit->comp_dir;
1178 table->num_files = 0;
1179 table->files = NULL;
1181 table->num_dirs = 0;
1182 table->dirs = NULL;
1184 table->files = NULL;
1185 table->last_line = NULL;
1186 table->lcl_head = NULL;
1188 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1190 /* Read in the prologue. */
1191 lh.total_length = read_4_bytes (abfd, line_ptr);
1192 line_ptr += 4;
1193 offset_size = 4;
1194 if (lh.total_length == 0xffffffff)
1196 lh.total_length = read_8_bytes (abfd, line_ptr);
1197 line_ptr += 8;
1198 offset_size = 8;
1200 else if (lh.total_length == 0 && unit->addr_size == 8)
1202 /* Handle (non-standard) 64-bit DWARF2 formats. */
1203 lh.total_length = read_4_bytes (abfd, line_ptr);
1204 line_ptr += 4;
1205 offset_size = 8;
1207 line_end = line_ptr + lh.total_length;
1208 lh.version = read_2_bytes (abfd, line_ptr);
1209 line_ptr += 2;
1210 if (offset_size == 4)
1211 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1212 else
1213 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1214 line_ptr += offset_size;
1215 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1216 line_ptr += 1;
1217 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1218 line_ptr += 1;
1219 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1220 line_ptr += 1;
1221 lh.line_range = read_1_byte (abfd, line_ptr);
1222 line_ptr += 1;
1223 lh.opcode_base = read_1_byte (abfd, line_ptr);
1224 line_ptr += 1;
1225 amt = lh.opcode_base * sizeof (unsigned char);
1226 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
1228 lh.standard_opcode_lengths[0] = 1;
1230 for (i = 1; i < lh.opcode_base; ++i)
1232 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1233 line_ptr += 1;
1236 /* Read directory table. */
1237 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1239 line_ptr += bytes_read;
1241 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1243 char **tmp;
1245 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1246 amt *= sizeof (char *);
1248 tmp = bfd_realloc (table->dirs, amt);
1249 if (tmp == NULL)
1251 free (table->dirs);
1252 return NULL;
1254 table->dirs = tmp;
1257 table->dirs[table->num_dirs++] = cur_dir;
1260 line_ptr += bytes_read;
1262 /* Read file name table. */
1263 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1265 line_ptr += bytes_read;
1267 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1269 struct fileinfo *tmp;
1271 amt = table->num_files + FILE_ALLOC_CHUNK;
1272 amt *= sizeof (struct fileinfo);
1274 tmp = bfd_realloc (table->files, amt);
1275 if (tmp == NULL)
1277 free (table->files);
1278 free (table->dirs);
1279 return NULL;
1281 table->files = tmp;
1284 table->files[table->num_files].name = cur_file;
1285 table->files[table->num_files].dir =
1286 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1287 line_ptr += bytes_read;
1288 table->files[table->num_files].time =
1289 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1290 line_ptr += bytes_read;
1291 table->files[table->num_files].size =
1292 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1293 line_ptr += bytes_read;
1294 table->num_files++;
1297 line_ptr += bytes_read;
1299 /* Read the statement sequences until there's nothing left. */
1300 while (line_ptr < line_end)
1302 /* State machine registers. */
1303 bfd_vma address = 0;
1304 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1305 unsigned int line = 1;
1306 unsigned int column = 0;
1307 int is_stmt = lh.default_is_stmt;
1308 int end_sequence = 0;
1309 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1310 compilers generate address sequences that are wildly out of
1311 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1312 for ia64-Linux). Thus, to determine the low and high
1313 address, we must compare on every DW_LNS_copy, etc. */
1314 bfd_vma low_pc = (bfd_vma) -1;
1315 bfd_vma high_pc = 0;
1317 /* Decode the table. */
1318 while (! end_sequence)
1320 op_code = read_1_byte (abfd, line_ptr);
1321 line_ptr += 1;
1323 if (op_code >= lh.opcode_base)
1325 /* Special operand. */
1326 adj_opcode = op_code - lh.opcode_base;
1327 address += (adj_opcode / lh.line_range)
1328 * lh.minimum_instruction_length;
1329 line += lh.line_base + (adj_opcode % lh.line_range);
1330 /* Append row to matrix using current values. */
1331 add_line_info (table, address, filename, line, column, 0);
1332 if (address < low_pc)
1333 low_pc = address;
1334 if (address > high_pc)
1335 high_pc = address;
1337 else switch (op_code)
1339 case DW_LNS_extended_op:
1340 /* Ignore length. */
1341 line_ptr += 1;
1342 extended_op = read_1_byte (abfd, line_ptr);
1343 line_ptr += 1;
1345 switch (extended_op)
1347 case DW_LNE_end_sequence:
1348 end_sequence = 1;
1349 add_line_info (table, address, filename, line, column,
1350 end_sequence);
1351 if (address < low_pc)
1352 low_pc = address;
1353 if (address > high_pc)
1354 high_pc = address;
1355 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1356 break;
1357 case DW_LNE_set_address:
1358 address = read_address (unit, line_ptr);
1359 line_ptr += unit->addr_size;
1360 break;
1361 case DW_LNE_define_file:
1362 cur_file = read_string (abfd, line_ptr, &bytes_read);
1363 line_ptr += bytes_read;
1364 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1366 struct fileinfo *tmp;
1368 amt = table->num_files + FILE_ALLOC_CHUNK;
1369 amt *= sizeof (struct fileinfo);
1370 tmp = bfd_realloc (table->files, amt);
1371 if (tmp == NULL)
1373 free (table->files);
1374 free (table->dirs);
1375 free (filename);
1376 return NULL;
1378 table->files = tmp;
1380 table->files[table->num_files].name = cur_file;
1381 table->files[table->num_files].dir =
1382 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1383 line_ptr += bytes_read;
1384 table->files[table->num_files].time =
1385 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1386 line_ptr += bytes_read;
1387 table->files[table->num_files].size =
1388 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1389 line_ptr += bytes_read;
1390 table->num_files++;
1391 break;
1392 default:
1393 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1394 bfd_set_error (bfd_error_bad_value);
1395 free (filename);
1396 free (table->files);
1397 free (table->dirs);
1398 return NULL;
1400 break;
1401 case DW_LNS_copy:
1402 add_line_info (table, address, filename, line, column, 0);
1403 if (address < low_pc)
1404 low_pc = address;
1405 if (address > high_pc)
1406 high_pc = address;
1407 break;
1408 case DW_LNS_advance_pc:
1409 address += lh.minimum_instruction_length
1410 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1411 line_ptr += bytes_read;
1412 break;
1413 case DW_LNS_advance_line:
1414 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1415 line_ptr += bytes_read;
1416 break;
1417 case DW_LNS_set_file:
1419 unsigned int file;
1421 /* The file and directory tables are 0
1422 based, the references are 1 based. */
1423 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1424 line_ptr += bytes_read;
1425 if (filename)
1426 free (filename);
1427 filename = concat_filename (table, file);
1428 break;
1430 case DW_LNS_set_column:
1431 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1432 line_ptr += bytes_read;
1433 break;
1434 case DW_LNS_negate_stmt:
1435 is_stmt = (!is_stmt);
1436 break;
1437 case DW_LNS_set_basic_block:
1438 break;
1439 case DW_LNS_const_add_pc:
1440 address += lh.minimum_instruction_length
1441 * ((255 - lh.opcode_base) / lh.line_range);
1442 break;
1443 case DW_LNS_fixed_advance_pc:
1444 address += read_2_bytes (abfd, line_ptr);
1445 line_ptr += 2;
1446 break;
1447 default:
1449 int i;
1451 /* Unknown standard opcode, ignore it. */
1452 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1454 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1455 line_ptr += bytes_read;
1461 if (filename)
1462 free (filename);
1465 return table;
1468 /* If ADDR is within TABLE set the output parameters and return TRUE,
1469 otherwise return FALSE. The output parameters, FILENAME_PTR and
1470 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1472 static bfd_boolean
1473 lookup_address_in_line_info_table (struct line_info_table *table,
1474 bfd_vma addr,
1475 struct funcinfo *function,
1476 const char **filename_ptr,
1477 unsigned int *linenumber_ptr)
1479 /* Note: table->last_line should be a descendingly sorted list. */
1480 struct line_info* next_line = table->last_line;
1481 struct line_info* each_line = NULL;
1482 *filename_ptr = NULL;
1484 if (!next_line)
1485 return FALSE;
1487 each_line = next_line->prev_line;
1489 /* Check for large addresses */
1490 if (addr > next_line->address)
1491 each_line = NULL; /* ensure we skip over the normal case */
1493 /* Normal case: search the list; save */
1494 while (each_line && next_line)
1496 /* If we have an address match, save this info. This allows us
1497 to return as good as results as possible for strange debugging
1498 info. */
1499 bfd_boolean addr_match = FALSE;
1500 if (each_line->address <= addr && addr < next_line->address)
1502 addr_match = TRUE;
1504 /* If this line appears to span functions, and addr is in the
1505 later function, return the first line of that function instead
1506 of the last line of the earlier one. This check is for GCC
1507 2.95, which emits the first line number for a function late. */
1509 if (function != NULL)
1511 bfd_vma lowest_pc;
1512 struct arange *arange;
1514 /* Find the lowest address in the function's range list */
1515 lowest_pc = function->arange.low;
1516 for (arange = &function->arange;
1517 arange;
1518 arange = arange->next)
1520 if (function->arange.low < lowest_pc)
1521 lowest_pc = function->arange.low;
1523 /* Check for spanning function and set outgoing line info */
1524 if (addr >= lowest_pc
1525 && each_line->address < lowest_pc
1526 && next_line->address > lowest_pc)
1528 *filename_ptr = next_line->filename;
1529 *linenumber_ptr = next_line->line;
1531 else
1533 *filename_ptr = each_line->filename;
1534 *linenumber_ptr = each_line->line;
1537 else
1539 *filename_ptr = each_line->filename;
1540 *linenumber_ptr = each_line->line;
1544 if (addr_match && !each_line->end_sequence)
1545 return TRUE; /* we have definitely found what we want */
1547 next_line = each_line;
1548 each_line = each_line->prev_line;
1551 /* At this point each_line is NULL but next_line is not. If we found
1552 a candidate end-of-sequence point in the loop above, we can return
1553 that (compatibility with a bug in the Intel compiler); otherwise,
1554 assuming that we found the containing function for this address in
1555 this compilation unit, return the first line we have a number for
1556 (compatibility with GCC 2.95). */
1557 if (*filename_ptr == NULL && function != NULL)
1559 *filename_ptr = next_line->filename;
1560 *linenumber_ptr = next_line->line;
1561 return TRUE;
1564 return FALSE;
1567 /* Read in the .debug_ranges section for future reference */
1569 static bfd_boolean
1570 read_debug_ranges (struct comp_unit *unit)
1572 struct dwarf2_debug *stash = unit->stash;
1573 return read_section (unit->abfd, ".debug_ranges", ".zdebug_ranges",
1574 stash->syms, 0,
1575 &stash->dwarf_ranges_buffer, &stash->dwarf_ranges_size);
1578 /* Function table functions. */
1580 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1581 Note that we need to find the function that has the smallest
1582 range that contains ADDR, to handle inlined functions without
1583 depending upon them being ordered in TABLE by increasing range. */
1585 static bfd_boolean
1586 lookup_address_in_function_table (struct comp_unit *unit,
1587 bfd_vma addr,
1588 struct funcinfo **function_ptr,
1589 const char **functionname_ptr)
1591 struct funcinfo* each_func;
1592 struct funcinfo* best_fit = NULL;
1593 struct arange *arange;
1595 for (each_func = unit->function_table;
1596 each_func;
1597 each_func = each_func->prev_func)
1599 for (arange = &each_func->arange;
1600 arange;
1601 arange = arange->next)
1603 if (addr >= arange->low && addr < arange->high)
1605 if (!best_fit ||
1606 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1607 best_fit = each_func;
1612 if (best_fit)
1614 *functionname_ptr = best_fit->name;
1615 *function_ptr = best_fit;
1616 return TRUE;
1618 else
1620 return FALSE;
1624 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1625 and LINENUMBER_PTR, and return TRUE. */
1627 static bfd_boolean
1628 lookup_symbol_in_function_table (struct comp_unit *unit,
1629 asymbol *sym,
1630 bfd_vma addr,
1631 const char **filename_ptr,
1632 unsigned int *linenumber_ptr)
1634 struct funcinfo* each_func;
1635 struct funcinfo* best_fit = NULL;
1636 struct arange *arange;
1637 const char *name = bfd_asymbol_name (sym);
1638 asection *sec = bfd_get_section (sym);
1640 for (each_func = unit->function_table;
1641 each_func;
1642 each_func = each_func->prev_func)
1644 for (arange = &each_func->arange;
1645 arange;
1646 arange = arange->next)
1648 if ((!each_func->sec || each_func->sec == sec)
1649 && addr >= arange->low
1650 && addr < arange->high
1651 && each_func->name
1652 && strcmp (name, each_func->name) == 0
1653 && (!best_fit
1654 || ((arange->high - arange->low)
1655 < (best_fit->arange.high - best_fit->arange.low))))
1656 best_fit = each_func;
1660 if (best_fit)
1662 best_fit->sec = sec;
1663 *filename_ptr = best_fit->file;
1664 *linenumber_ptr = best_fit->line;
1665 return TRUE;
1667 else
1668 return FALSE;
1671 /* Variable table functions. */
1673 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1674 LINENUMBER_PTR, and return TRUE. */
1676 static bfd_boolean
1677 lookup_symbol_in_variable_table (struct comp_unit *unit,
1678 asymbol *sym,
1679 bfd_vma addr,
1680 const char **filename_ptr,
1681 unsigned int *linenumber_ptr)
1683 const char *name = bfd_asymbol_name (sym);
1684 asection *sec = bfd_get_section (sym);
1685 struct varinfo* each;
1687 for (each = unit->variable_table; each; each = each->prev_var)
1688 if (each->stack == 0
1689 && each->file != NULL
1690 && each->name != NULL
1691 && each->addr == addr
1692 && (!each->sec || each->sec == sec)
1693 && strcmp (name, each->name) == 0)
1694 break;
1696 if (each)
1698 each->sec = sec;
1699 *filename_ptr = each->file;
1700 *linenumber_ptr = each->line;
1701 return TRUE;
1703 else
1704 return FALSE;
1707 static char *
1708 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1710 bfd *abfd = unit->abfd;
1711 bfd_byte *info_ptr;
1712 unsigned int abbrev_number, bytes_read, i;
1713 struct abbrev_info *abbrev;
1714 struct attribute attr;
1715 char *name = 0;
1717 info_ptr = unit->info_ptr_unit + die_ref;
1718 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1719 info_ptr += bytes_read;
1721 if (abbrev_number)
1723 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1724 if (! abbrev)
1726 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1727 abbrev_number);
1728 bfd_set_error (bfd_error_bad_value);
1730 else
1732 for (i = 0; i < abbrev->num_attrs; ++i)
1734 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1735 switch (attr.name)
1737 case DW_AT_name:
1738 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1739 if (name == NULL)
1740 name = attr.u.str;
1741 break;
1742 case DW_AT_specification:
1743 name = find_abstract_instance_name (unit, attr.u.val);
1744 break;
1745 case DW_AT_MIPS_linkage_name:
1746 name = attr.u.str;
1747 break;
1748 default:
1749 break;
1754 return (name);
1757 static void
1758 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1760 bfd_byte *ranges_ptr;
1761 bfd_vma base_address = unit->base_address;
1763 if (! unit->stash->dwarf_ranges_buffer)
1765 if (! read_debug_ranges (unit))
1766 return;
1768 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1770 for (;;)
1772 bfd_vma low_pc;
1773 bfd_vma high_pc;
1775 if (unit->addr_size == 4)
1777 low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1778 ranges_ptr += 4;
1779 high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1780 ranges_ptr += 4;
1782 else
1784 low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1785 ranges_ptr += 8;
1786 high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1787 ranges_ptr += 8;
1789 if (low_pc == 0 && high_pc == 0)
1790 break;
1791 if (low_pc == -1UL && high_pc != -1UL)
1792 base_address = high_pc;
1793 else
1794 arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1798 /* DWARF2 Compilation unit functions. */
1800 /* Scan over each die in a comp. unit looking for functions to add
1801 to the function table and variables to the variable table. */
1803 static bfd_boolean
1804 scan_unit_for_symbols (struct comp_unit *unit)
1806 bfd *abfd = unit->abfd;
1807 bfd_byte *info_ptr = unit->first_child_die_ptr;
1808 int nesting_level = 1;
1809 struct funcinfo **nested_funcs;
1810 int nested_funcs_size;
1812 /* Maintain a stack of in-scope functions and inlined functions, which we
1813 can use to set the caller_func field. */
1814 nested_funcs_size = 32;
1815 nested_funcs = bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
1816 if (nested_funcs == NULL)
1817 return FALSE;
1818 nested_funcs[nesting_level] = 0;
1820 while (nesting_level)
1822 unsigned int abbrev_number, bytes_read, i;
1823 struct abbrev_info *abbrev;
1824 struct attribute attr;
1825 struct funcinfo *func;
1826 struct varinfo *var;
1827 bfd_vma low_pc = 0;
1828 bfd_vma high_pc = 0;
1830 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1831 info_ptr += bytes_read;
1833 if (! abbrev_number)
1835 nesting_level--;
1836 continue;
1839 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1840 if (! abbrev)
1842 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1843 abbrev_number);
1844 bfd_set_error (bfd_error_bad_value);
1845 free (nested_funcs);
1846 return FALSE;
1849 var = NULL;
1850 if (abbrev->tag == DW_TAG_subprogram
1851 || abbrev->tag == DW_TAG_entry_point
1852 || abbrev->tag == DW_TAG_inlined_subroutine)
1854 bfd_size_type amt = sizeof (struct funcinfo);
1855 func = bfd_zalloc (abfd, amt);
1856 func->tag = abbrev->tag;
1857 func->prev_func = unit->function_table;
1858 unit->function_table = func;
1859 BFD_ASSERT (!unit->cached);
1861 if (func->tag == DW_TAG_inlined_subroutine)
1862 for (i = nesting_level - 1; i >= 1; i--)
1863 if (nested_funcs[i])
1865 func->caller_func = nested_funcs[i];
1866 break;
1868 nested_funcs[nesting_level] = func;
1870 else
1872 func = NULL;
1873 if (abbrev->tag == DW_TAG_variable)
1875 bfd_size_type amt = sizeof (struct varinfo);
1876 var = bfd_zalloc (abfd, amt);
1877 var->tag = abbrev->tag;
1878 var->stack = 1;
1879 var->prev_var = unit->variable_table;
1880 unit->variable_table = var;
1881 BFD_ASSERT (!unit->cached);
1884 /* No inline function in scope at this nesting level. */
1885 nested_funcs[nesting_level] = 0;
1888 for (i = 0; i < abbrev->num_attrs; ++i)
1890 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1892 if (func)
1894 switch (attr.name)
1896 case DW_AT_call_file:
1897 func->caller_file = concat_filename (unit->line_table, attr.u.val);
1898 break;
1900 case DW_AT_call_line:
1901 func->caller_line = attr.u.val;
1902 break;
1904 case DW_AT_abstract_origin:
1905 func->name = find_abstract_instance_name (unit, attr.u.val);
1906 break;
1908 case DW_AT_name:
1909 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1910 if (func->name == NULL)
1911 func->name = attr.u.str;
1912 break;
1914 case DW_AT_MIPS_linkage_name:
1915 func->name = attr.u.str;
1916 break;
1918 case DW_AT_low_pc:
1919 low_pc = attr.u.val;
1920 break;
1922 case DW_AT_high_pc:
1923 high_pc = attr.u.val;
1924 break;
1926 case DW_AT_ranges:
1927 read_rangelist (unit, &func->arange, attr.u.val);
1928 break;
1930 case DW_AT_decl_file:
1931 func->file = concat_filename (unit->line_table,
1932 attr.u.val);
1933 break;
1935 case DW_AT_decl_line:
1936 func->line = attr.u.val;
1937 break;
1939 default:
1940 break;
1943 else if (var)
1945 switch (attr.name)
1947 case DW_AT_name:
1948 var->name = attr.u.str;
1949 break;
1951 case DW_AT_decl_file:
1952 var->file = concat_filename (unit->line_table,
1953 attr.u.val);
1954 break;
1956 case DW_AT_decl_line:
1957 var->line = attr.u.val;
1958 break;
1960 case DW_AT_external:
1961 if (attr.u.val != 0)
1962 var->stack = 0;
1963 break;
1965 case DW_AT_location:
1966 switch (attr.form)
1968 case DW_FORM_block:
1969 case DW_FORM_block1:
1970 case DW_FORM_block2:
1971 case DW_FORM_block4:
1972 if (*attr.u.blk->data == DW_OP_addr)
1974 var->stack = 0;
1976 /* Verify that DW_OP_addr is the only opcode in the
1977 location, in which case the block size will be 1
1978 plus the address size. */
1979 /* ??? For TLS variables, gcc can emit
1980 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
1981 which we don't handle here yet. */
1982 if (attr.u.blk->size == unit->addr_size + 1U)
1983 var->addr = bfd_get (unit->addr_size * 8,
1984 unit->abfd,
1985 attr.u.blk->data + 1);
1987 break;
1989 default:
1990 break;
1992 break;
1994 default:
1995 break;
2000 if (func && high_pc != 0)
2002 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
2005 if (abbrev->has_children)
2007 nesting_level++;
2009 if (nesting_level >= nested_funcs_size)
2011 struct funcinfo **tmp;
2013 nested_funcs_size *= 2;
2014 tmp = bfd_realloc (nested_funcs,
2015 (nested_funcs_size
2016 * sizeof (struct funcinfo *)));
2017 if (tmp == NULL)
2019 free (nested_funcs);
2020 return FALSE;
2022 nested_funcs = tmp;
2024 nested_funcs[nesting_level] = 0;
2028 free (nested_funcs);
2029 return TRUE;
2032 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
2033 includes the compilation unit header that proceeds the DIE's, but
2034 does not include the length field that precedes each compilation
2035 unit header. END_PTR points one past the end of this comp unit.
2036 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
2038 This routine does not read the whole compilation unit; only enough
2039 to get to the line number information for the compilation unit. */
2041 static struct comp_unit *
2042 parse_comp_unit (struct dwarf2_debug *stash,
2043 bfd_vma unit_length,
2044 bfd_byte *info_ptr_unit,
2045 unsigned int offset_size)
2047 struct comp_unit* unit;
2048 unsigned int version;
2049 bfd_uint64_t abbrev_offset = 0;
2050 unsigned int addr_size;
2051 struct abbrev_info** abbrevs;
2052 unsigned int abbrev_number, bytes_read, i;
2053 struct abbrev_info *abbrev;
2054 struct attribute attr;
2055 bfd_byte *info_ptr = stash->info_ptr;
2056 bfd_byte *end_ptr = info_ptr + unit_length;
2057 bfd_size_type amt;
2058 bfd_vma low_pc = 0;
2059 bfd_vma high_pc = 0;
2060 bfd *abfd = stash->bfd;
2062 version = read_2_bytes (abfd, info_ptr);
2063 info_ptr += 2;
2064 BFD_ASSERT (offset_size == 4 || offset_size == 8);
2065 if (offset_size == 4)
2066 abbrev_offset = read_4_bytes (abfd, info_ptr);
2067 else
2068 abbrev_offset = read_8_bytes (abfd, info_ptr);
2069 info_ptr += offset_size;
2070 addr_size = read_1_byte (abfd, info_ptr);
2071 info_ptr += 1;
2073 if (version != 2)
2075 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
2076 bfd_set_error (bfd_error_bad_value);
2077 return 0;
2080 if (addr_size > sizeof (bfd_vma))
2082 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
2083 addr_size,
2084 (unsigned int) sizeof (bfd_vma));
2085 bfd_set_error (bfd_error_bad_value);
2086 return 0;
2089 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
2091 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
2092 bfd_set_error (bfd_error_bad_value);
2093 return 0;
2096 /* Read the abbrevs for this compilation unit into a table. */
2097 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
2098 if (! abbrevs)
2099 return 0;
2101 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2102 info_ptr += bytes_read;
2103 if (! abbrev_number)
2105 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
2106 abbrev_number);
2107 bfd_set_error (bfd_error_bad_value);
2108 return 0;
2111 abbrev = lookup_abbrev (abbrev_number, abbrevs);
2112 if (! abbrev)
2114 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
2115 abbrev_number);
2116 bfd_set_error (bfd_error_bad_value);
2117 return 0;
2120 amt = sizeof (struct comp_unit);
2121 unit = bfd_zalloc (abfd, amt);
2122 unit->abfd = abfd;
2123 unit->addr_size = addr_size;
2124 unit->offset_size = offset_size;
2125 unit->abbrevs = abbrevs;
2126 unit->end_ptr = end_ptr;
2127 unit->stash = stash;
2128 unit->info_ptr_unit = info_ptr_unit;
2130 for (i = 0; i < abbrev->num_attrs; ++i)
2132 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2134 /* Store the data if it is of an attribute we want to keep in a
2135 partial symbol table. */
2136 switch (attr.name)
2138 case DW_AT_stmt_list:
2139 unit->stmtlist = 1;
2140 unit->line_offset = attr.u.val;
2141 break;
2143 case DW_AT_name:
2144 unit->name = attr.u.str;
2145 break;
2147 case DW_AT_low_pc:
2148 low_pc = attr.u.val;
2149 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2150 this is the base address to use when reading location
2151 lists or range lists. */
2152 unit->base_address = low_pc;
2153 break;
2155 case DW_AT_high_pc:
2156 high_pc = attr.u.val;
2157 break;
2159 case DW_AT_ranges:
2160 read_rangelist (unit, &unit->arange, attr.u.val);
2161 break;
2163 case DW_AT_comp_dir:
2165 char *comp_dir = attr.u.str;
2166 if (comp_dir)
2168 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2169 directory, get rid of it. */
2170 char *cp = strchr (comp_dir, ':');
2172 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2173 comp_dir = cp + 1;
2175 unit->comp_dir = comp_dir;
2176 break;
2179 default:
2180 break;
2183 if (high_pc != 0)
2185 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
2188 unit->first_child_die_ptr = info_ptr;
2189 return unit;
2192 /* Return TRUE if UNIT may contain the address given by ADDR. When
2193 there are functions written entirely with inline asm statements, the
2194 range info in the compilation unit header may not be correct. We
2195 need to consult the line info table to see if a compilation unit
2196 really contains the given address. */
2198 static bfd_boolean
2199 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2201 struct arange *arange;
2203 if (unit->error)
2204 return FALSE;
2206 arange = &unit->arange;
2209 if (addr >= arange->low && addr < arange->high)
2210 return TRUE;
2211 arange = arange->next;
2213 while (arange);
2215 return FALSE;
2218 /* If UNIT contains ADDR, set the output parameters to the values for
2219 the line containing ADDR. The output parameters, FILENAME_PTR,
2220 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2221 to be filled in.
2223 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2224 FALSE otherwise. */
2226 static bfd_boolean
2227 comp_unit_find_nearest_line (struct comp_unit *unit,
2228 bfd_vma addr,
2229 const char **filename_ptr,
2230 const char **functionname_ptr,
2231 unsigned int *linenumber_ptr,
2232 struct dwarf2_debug *stash)
2234 bfd_boolean line_p;
2235 bfd_boolean func_p;
2236 struct funcinfo *function;
2238 if (unit->error)
2239 return FALSE;
2241 if (! unit->line_table)
2243 if (! unit->stmtlist)
2245 unit->error = 1;
2246 return FALSE;
2249 unit->line_table = decode_line_info (unit, stash);
2251 if (! unit->line_table)
2253 unit->error = 1;
2254 return FALSE;
2257 if (unit->first_child_die_ptr < unit->end_ptr
2258 && ! scan_unit_for_symbols (unit))
2260 unit->error = 1;
2261 return FALSE;
2265 function = NULL;
2266 func_p = lookup_address_in_function_table (unit, addr,
2267 &function, functionname_ptr);
2268 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2269 stash->inliner_chain = function;
2270 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2271 function, filename_ptr,
2272 linenumber_ptr);
2273 return line_p || func_p;
2276 /* Check to see if line info is already decoded in a comp_unit.
2277 If not, decode it. Returns TRUE if no errors were encountered;
2278 FALSE otherwise. */
2280 static bfd_boolean
2281 comp_unit_maybe_decode_line_info (struct comp_unit *unit,
2282 struct dwarf2_debug *stash)
2284 if (unit->error)
2285 return FALSE;
2287 if (! unit->line_table)
2289 if (! unit->stmtlist)
2291 unit->error = 1;
2292 return FALSE;
2295 unit->line_table = decode_line_info (unit, stash);
2297 if (! unit->line_table)
2299 unit->error = 1;
2300 return FALSE;
2303 if (unit->first_child_die_ptr < unit->end_ptr
2304 && ! scan_unit_for_symbols (unit))
2306 unit->error = 1;
2307 return FALSE;
2311 return TRUE;
2314 /* If UNIT contains SYM at ADDR, set the output parameters to the
2315 values for the line containing SYM. The output parameters,
2316 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2317 filled in.
2319 Return TRUE if UNIT contains SYM, and no errors were encountered;
2320 FALSE otherwise. */
2322 static bfd_boolean
2323 comp_unit_find_line (struct comp_unit *unit,
2324 asymbol *sym,
2325 bfd_vma addr,
2326 const char **filename_ptr,
2327 unsigned int *linenumber_ptr,
2328 struct dwarf2_debug *stash)
2330 if (!comp_unit_maybe_decode_line_info (unit, stash))
2331 return FALSE;
2333 if (sym->flags & BSF_FUNCTION)
2334 return lookup_symbol_in_function_table (unit, sym, addr,
2335 filename_ptr,
2336 linenumber_ptr);
2338 return lookup_symbol_in_variable_table (unit, sym, addr,
2339 filename_ptr,
2340 linenumber_ptr);
2343 static struct funcinfo *
2344 reverse_funcinfo_list (struct funcinfo *head)
2346 struct funcinfo *rhead;
2347 struct funcinfo *temp;
2349 for (rhead = NULL; head; head = temp)
2351 temp = head->prev_func;
2352 head->prev_func = rhead;
2353 rhead = head;
2355 return rhead;
2358 static struct varinfo *
2359 reverse_varinfo_list (struct varinfo *head)
2361 struct varinfo *rhead;
2362 struct varinfo *temp;
2364 for (rhead = NULL; head; head = temp)
2366 temp = head->prev_var;
2367 head->prev_var = rhead;
2368 rhead = head;
2370 return rhead;
2373 /* Extract all interesting funcinfos and varinfos of a compilation
2374 unit into hash tables for faster lookup. Returns TRUE if no
2375 errors were enountered; FALSE otherwise. */
2377 static bfd_boolean
2378 comp_unit_hash_info (struct dwarf2_debug *stash,
2379 struct comp_unit *unit,
2380 struct info_hash_table *funcinfo_hash_table,
2381 struct info_hash_table *varinfo_hash_table)
2383 struct funcinfo* each_func;
2384 struct varinfo* each_var;
2385 bfd_boolean okay = TRUE;
2387 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
2389 if (!comp_unit_maybe_decode_line_info (unit, stash))
2390 return FALSE;
2392 BFD_ASSERT (!unit->cached);
2394 /* To preserve the original search order, we went to visit the function
2395 infos in the reversed order of the list. However, making the list
2396 bi-directional use quite a bit of extra memory. So we reverse
2397 the list first, traverse the list in the now reversed order and
2398 finally reverse the list again to get back the original order. */
2399 unit->function_table = reverse_funcinfo_list (unit->function_table);
2400 for (each_func = unit->function_table;
2401 each_func && okay;
2402 each_func = each_func->prev_func)
2404 /* Skip nameless functions. */
2405 if (each_func->name)
2406 /* There is no need to copy name string into hash table as
2407 name string is either in the dwarf string buffer or
2408 info in the stash. */
2409 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
2410 (void*) each_func, FALSE);
2412 unit->function_table = reverse_funcinfo_list (unit->function_table);
2413 if (!okay)
2414 return FALSE;
2416 /* We do the same for variable infos. */
2417 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2418 for (each_var = unit->variable_table;
2419 each_var && okay;
2420 each_var = each_var->prev_var)
2422 /* Skip stack vars and vars with no files or names. */
2423 if (each_var->stack == 0
2424 && each_var->file != NULL
2425 && each_var->name != NULL)
2426 /* There is no need to copy name string into hash table as
2427 name string is either in the dwarf string buffer or
2428 info in the stash. */
2429 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
2430 (void*) each_var, FALSE);
2433 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2434 unit->cached = TRUE;
2435 return okay;
2438 /* Locate a section in a BFD containing debugging info. The search starts
2439 from the section after AFTER_SEC, or from the first section in the BFD if
2440 AFTER_SEC is NULL. The search works by examining the names of the
2441 sections. There are two permissiable names. The first is .debug_info.
2442 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2443 This is a variation on the .debug_info section which has a checksum
2444 describing the contents appended onto the name. This allows the linker to
2445 identify and discard duplicate debugging sections for different
2446 compilation units. */
2447 #define DWARF2_DEBUG_INFO ".debug_info"
2448 #define DWARF2_COMPRESSED_DEBUG_INFO ".zdebug_info"
2449 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2451 static asection *
2452 find_debug_info (bfd *abfd, asection *after_sec)
2454 asection * msec;
2456 msec = after_sec != NULL ? after_sec->next : abfd->sections;
2458 while (msec)
2460 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2461 return msec;
2463 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2464 return msec;
2466 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
2467 return msec;
2469 msec = msec->next;
2472 return NULL;
2475 /* Unset vmas for loadable sections in STASH. */
2477 static void
2478 unset_sections (struct dwarf2_debug *stash)
2480 unsigned int i;
2481 struct loadable_section *p;
2483 i = stash->loadable_section_count;
2484 p = stash->loadable_sections;
2485 for (; i > 0; i--, p++)
2486 p->section->vma = 0;
2489 /* Set unique vmas for loadable sections in ABFD and save vmas in
2490 STASH for unset_sections. */
2492 static bfd_boolean
2493 place_sections (bfd *abfd, struct dwarf2_debug *stash)
2495 struct loadable_section *p;
2496 unsigned int i;
2498 if (stash->loadable_section_count != 0)
2500 i = stash->loadable_section_count;
2501 p = stash->loadable_sections;
2502 for (; i > 0; i--, p++)
2503 p->section->vma = p->adj_vma;
2505 else
2507 asection *sect;
2508 bfd_vma last_vma = 0;
2509 bfd_size_type amt;
2510 struct loadable_section *p;
2512 i = 0;
2513 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2515 bfd_size_type sz;
2517 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2518 continue;
2520 sz = sect->rawsize ? sect->rawsize : sect->size;
2521 if (sz == 0)
2522 continue;
2524 i++;
2527 amt = i * sizeof (struct loadable_section);
2528 p = (struct loadable_section *) bfd_zalloc (abfd, amt);
2529 if (! p)
2530 return FALSE;
2532 stash->loadable_sections = p;
2533 stash->loadable_section_count = i;
2535 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2537 bfd_size_type sz;
2539 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2540 continue;
2542 sz = sect->rawsize ? sect->rawsize : sect->size;
2543 if (sz == 0)
2544 continue;
2546 p->section = sect;
2547 if (last_vma != 0)
2549 /* Align the new address to the current section
2550 alignment. */
2551 last_vma = ((last_vma
2552 + ~((bfd_vma) -1 << sect->alignment_power))
2553 & ((bfd_vma) -1 << sect->alignment_power));
2554 sect->vma = last_vma;
2556 p->adj_vma = sect->vma;
2557 last_vma += sect->vma + sz;
2559 p++;
2563 return TRUE;
2566 /* Look up a funcinfo by name using the given info hash table. If found,
2567 also update the locations pointed to by filename_ptr and linenumber_ptr.
2569 This function returns TRUE if a funcinfo that matches the given symbol
2570 and address is found with any error; otherwise it returns FALSE. */
2572 static bfd_boolean
2573 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
2574 asymbol *sym,
2575 bfd_vma addr,
2576 const char **filename_ptr,
2577 unsigned int *linenumber_ptr)
2579 struct funcinfo* each_func;
2580 struct funcinfo* best_fit = NULL;
2581 struct info_list_node *node;
2582 struct arange *arange;
2583 const char *name = bfd_asymbol_name (sym);
2584 asection *sec = bfd_get_section (sym);
2586 for (node = lookup_info_hash_table (hash_table, name);
2587 node;
2588 node = node->next)
2590 each_func = node->info;
2591 for (arange = &each_func->arange;
2592 arange;
2593 arange = arange->next)
2595 if ((!each_func->sec || each_func->sec == sec)
2596 && addr >= arange->low
2597 && addr < arange->high
2598 && (!best_fit
2599 || ((arange->high - arange->low)
2600 < (best_fit->arange.high - best_fit->arange.low))))
2601 best_fit = each_func;
2605 if (best_fit)
2607 best_fit->sec = sec;
2608 *filename_ptr = best_fit->file;
2609 *linenumber_ptr = best_fit->line;
2610 return TRUE;
2613 return FALSE;
2616 /* Look up a varinfo by name using the given info hash table. If found,
2617 also update the locations pointed to by filename_ptr and linenumber_ptr.
2619 This function returns TRUE if a varinfo that matches the given symbol
2620 and address is found with any error; otherwise it returns FALSE. */
2622 static bfd_boolean
2623 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
2624 asymbol *sym,
2625 bfd_vma addr,
2626 const char **filename_ptr,
2627 unsigned int *linenumber_ptr)
2629 const char *name = bfd_asymbol_name (sym);
2630 asection *sec = bfd_get_section (sym);
2631 struct varinfo* each;
2632 struct info_list_node *node;
2634 for (node = lookup_info_hash_table (hash_table, name);
2635 node;
2636 node = node->next)
2638 each = node->info;
2639 if (each->addr == addr
2640 && (!each->sec || each->sec == sec))
2642 each->sec = sec;
2643 *filename_ptr = each->file;
2644 *linenumber_ptr = each->line;
2645 return TRUE;
2649 return FALSE;
2652 /* Update the funcinfo and varinfo info hash tables if they are
2653 not up to date. Returns TRUE if there is no error; otherwise
2654 returns FALSE and disable the info hash tables. */
2656 static bfd_boolean
2657 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
2659 struct comp_unit *each;
2661 /* Exit if hash tables are up-to-date. */
2662 if (stash->all_comp_units == stash->hash_units_head)
2663 return TRUE;
2665 if (stash->hash_units_head)
2666 each = stash->hash_units_head->prev_unit;
2667 else
2668 each = stash->last_comp_unit;
2670 while (each)
2672 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
2673 stash->varinfo_hash_table))
2675 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2676 return FALSE;
2678 each = each->prev_unit;
2681 stash->hash_units_head = stash->all_comp_units;
2682 return TRUE;
2685 /* Check consistency of info hash tables. This is for debugging only. */
2687 static void ATTRIBUTE_UNUSED
2688 stash_verify_info_hash_table (struct dwarf2_debug *stash)
2690 struct comp_unit *each_unit;
2691 struct funcinfo *each_func;
2692 struct varinfo *each_var;
2693 struct info_list_node *node;
2694 bfd_boolean found;
2696 for (each_unit = stash->all_comp_units;
2697 each_unit;
2698 each_unit = each_unit->next_unit)
2700 for (each_func = each_unit->function_table;
2701 each_func;
2702 each_func = each_func->prev_func)
2704 if (!each_func->name)
2705 continue;
2706 node = lookup_info_hash_table (stash->funcinfo_hash_table,
2707 each_func->name);
2708 BFD_ASSERT (node);
2709 found = FALSE;
2710 while (node && !found)
2712 found = node->info == each_func;
2713 node = node->next;
2715 BFD_ASSERT (found);
2718 for (each_var = each_unit->variable_table;
2719 each_var;
2720 each_var = each_var->prev_var)
2722 if (!each_var->name || !each_var->file || each_var->stack)
2723 continue;
2724 node = lookup_info_hash_table (stash->varinfo_hash_table,
2725 each_var->name);
2726 BFD_ASSERT (node);
2727 found = FALSE;
2728 while (node && !found)
2730 found = node->info == each_var;
2731 node = node->next;
2733 BFD_ASSERT (found);
2738 /* Check to see if we want to enable the info hash tables, which consume
2739 quite a bit of memory. Currently we only check the number times
2740 bfd_dwarf2_find_line is called. In the future, we may also want to
2741 take the number of symbols into account. */
2743 static void
2744 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
2746 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
2748 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
2749 return;
2751 /* FIXME: Maybe we should check the reduce_memory_overheads
2752 and optimize fields in the bfd_link_info structure ? */
2754 /* Create hash tables. */
2755 stash->funcinfo_hash_table = create_info_hash_table (abfd);
2756 stash->varinfo_hash_table = create_info_hash_table (abfd);
2757 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
2759 /* Turn off info hashes if any allocation above fails. */
2760 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2761 return;
2763 /* We need a forced update so that the info hash tables will
2764 be created even though there is no compilation unit. That
2765 happens if STASH_INFO_HASH_TRIGGER is 0. */
2766 stash_maybe_update_info_hash_tables (stash);
2767 stash->info_hash_status = STASH_INFO_HASH_ON;
2770 /* Find the file and line associated with a symbol and address using the
2771 info hash tables of a stash. If there is a match, the function returns
2772 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
2773 otherwise it returns FALSE. */
2775 static bfd_boolean
2776 stash_find_line_fast (struct dwarf2_debug *stash,
2777 asymbol *sym,
2778 bfd_vma addr,
2779 const char **filename_ptr,
2780 unsigned int *linenumber_ptr)
2782 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
2784 if (sym->flags & BSF_FUNCTION)
2785 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
2786 filename_ptr, linenumber_ptr);
2787 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
2788 filename_ptr, linenumber_ptr);
2791 /* Find the source code location of SYMBOL. If SYMBOL is NULL
2792 then find the nearest source code location corresponding to
2793 the address SECTION + OFFSET.
2794 Returns TRUE if the line is found without error and fills in
2795 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
2796 NULL the FUNCTIONNAME_PTR is also filled in.
2797 SYMBOLS contains the symbol table for ABFD.
2798 ADDR_SIZE is the number of bytes in the initial .debug_info length
2799 field and in the abbreviation offset, or zero to indicate that the
2800 default value should be used. */
2802 static bfd_boolean
2803 find_line (bfd *abfd,
2804 asection *section,
2805 bfd_vma offset,
2806 asymbol *symbol,
2807 asymbol **symbols,
2808 const char **filename_ptr,
2809 const char **functionname_ptr,
2810 unsigned int *linenumber_ptr,
2811 unsigned int addr_size,
2812 void **pinfo)
2814 /* Read each compilation unit from the section .debug_info, and check
2815 to see if it contains the address we are searching for. If yes,
2816 lookup the address, and return the line number info. If no, go
2817 on to the next compilation unit.
2819 We keep a list of all the previously read compilation units, and
2820 a pointer to the next un-read compilation unit. Check the
2821 previously read units before reading more. */
2822 struct dwarf2_debug *stash;
2823 /* What address are we looking for? */
2824 bfd_vma addr;
2825 struct comp_unit* each;
2826 bfd_vma found = FALSE;
2827 bfd_boolean do_line;
2829 stash = *pinfo;
2831 if (! stash)
2833 bfd_size_type amt = sizeof (struct dwarf2_debug);
2835 stash = bfd_zalloc (abfd, amt);
2836 if (! stash)
2837 return FALSE;
2840 /* In a relocatable file, 2 functions may have the same address.
2841 We change the section vma so that they won't overlap. */
2842 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2844 if (! place_sections (abfd, stash))
2845 return FALSE;
2848 do_line = (section == NULL
2849 && offset == 0
2850 && functionname_ptr == NULL
2851 && symbol != NULL);
2852 if (do_line)
2854 addr = symbol->value;
2855 section = bfd_get_section (symbol);
2857 else if (section != NULL
2858 && functionname_ptr != NULL
2859 && symbol == NULL)
2860 addr = offset;
2861 else
2862 abort ();
2864 if (section->output_section)
2865 addr += section->output_section->vma + section->output_offset;
2866 else
2867 addr += section->vma;
2868 *filename_ptr = NULL;
2869 if (! do_line)
2870 *functionname_ptr = NULL;
2871 *linenumber_ptr = 0;
2873 if (! *pinfo)
2875 bfd *debug_bfd;
2876 bfd_size_type total_size;
2877 asection *msec;
2879 *pinfo = stash;
2881 msec = find_debug_info (abfd, NULL);
2882 if (msec == NULL)
2884 char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
2886 if (debug_filename == NULL)
2887 /* No dwarf2 info, and no gnu_debuglink to follow.
2888 Note that at this point the stash has been allocated, but
2889 contains zeros. This lets future calls to this function
2890 fail more quickly. */
2891 goto done;
2893 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
2894 || ! bfd_check_format (debug_bfd, bfd_object)
2895 || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
2897 if (debug_bfd)
2898 bfd_close (debug_bfd);
2899 /* FIXME: Should we report our failure to follow the debuglink ? */
2900 free (debug_filename);
2901 goto done;
2904 else
2905 debug_bfd = abfd;
2907 /* There can be more than one DWARF2 info section in a BFD these
2908 days. First handle the easy case when there's only one. If
2909 there's more than one, try case two: none of the sections is
2910 compressed. In that case, read them all in and produce one
2911 large stash. We do this in two passes - in the first pass we
2912 just accumulate the section sizes, and in the second pass we
2913 read in the section's contents. (The allows us to avoid
2914 reallocing the data as we add sections to the stash.) If
2915 some or all sections are compressed, then do things the slow
2916 way, with a bunch of reallocs. */
2918 if (! find_debug_info (debug_bfd, msec))
2920 /* Case 1: only one info section. */
2921 total_size = msec->size;
2922 if (! read_section (debug_bfd, ".debug_info", ".zdebug_info",
2923 symbols, 0,
2924 &stash->info_ptr_memory, &total_size))
2925 goto done;
2926 stash->info_ptr = stash->info_ptr_memory;
2927 stash->info_ptr_end = stash->info_ptr + total_size;
2929 else
2931 int all_uncompressed = 1;
2932 for (total_size = 0; msec; msec = find_debug_info (debug_bfd, msec))
2934 total_size += msec->size;
2935 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2936 all_uncompressed = 0;
2938 if (all_uncompressed)
2940 /* Case 2: multiple sections, but none is compressed. */
2941 stash->info_ptr_memory = bfd_malloc (total_size);
2942 if (stash->info_ptr_memory == NULL)
2943 goto done;
2945 stash->info_ptr = stash->info_ptr_memory;
2946 stash->info_ptr_end = stash->info_ptr;
2948 for (msec = find_debug_info (debug_bfd, NULL);
2949 msec;
2950 msec = find_debug_info (debug_bfd, msec))
2952 bfd_size_type size;
2953 bfd_size_type start;
2955 size = msec->size;
2956 if (size == 0)
2957 continue;
2959 start = stash->info_ptr_end - stash->info_ptr;
2961 if ((bfd_simple_get_relocated_section_contents
2962 (debug_bfd, msec, stash->info_ptr + start, symbols))
2963 == NULL)
2964 continue;
2966 stash->info_ptr_end = stash->info_ptr + start + size;
2969 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2971 else
2973 /* Case 3: multiple sections, some or all compressed. */
2974 stash->info_ptr_memory = bfd_malloc (1);
2975 stash->info_ptr = stash->info_ptr_memory;
2976 stash->info_ptr_end = stash->info_ptr;
2977 for (msec = find_debug_info (debug_bfd, NULL);
2978 msec;
2979 msec = find_debug_info (debug_bfd, msec))
2981 bfd_size_type size = msec->size;
2982 bfd_byte* buffer
2983 = (bfd_simple_get_relocated_section_contents
2984 (debug_bfd, msec, NULL, symbols));
2985 if (! buffer)
2986 continue;
2987 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2989 if (! bfd_uncompress_section_contents (&buffer, &size))
2990 continue;
2992 stash->info_ptr = bfd_realloc (stash->info_ptr,
2993 stash->info_ptr_end
2994 - stash->info_ptr + size);
2995 memcpy (stash->info_ptr_end, buffer, size);
2996 free (buffer);
2997 stash->info_ptr_end += size;
3002 stash->sec = find_debug_info (debug_bfd, NULL);
3003 stash->sec_info_ptr = stash->info_ptr;
3004 stash->syms = symbols;
3005 stash->bfd = debug_bfd;
3008 /* A null info_ptr indicates that there is no dwarf2 info
3009 (or that an error occured while setting up the stash). */
3010 if (! stash->info_ptr)
3011 goto done;
3013 stash->inliner_chain = NULL;
3015 /* Check the previously read comp. units first. */
3016 if (do_line)
3018 /* The info hash tables use quite a bit of memory. We may not want to
3019 always use them. We use some heuristics to decide if and when to
3020 turn it on. */
3021 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
3022 stash_maybe_enable_info_hash_tables (abfd, stash);
3024 /* Keep info hash table up to date if they are available. Note that we
3025 may disable the hash tables if there is any error duing update. */
3026 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3027 stash_maybe_update_info_hash_tables (stash);
3029 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3031 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
3032 linenumber_ptr);
3033 if (found)
3034 goto done;
3036 else
3038 /* Check the previously read comp. units first. */
3039 for (each = stash->all_comp_units; each; each = each->next_unit)
3040 if ((symbol->flags & BSF_FUNCTION) == 0
3041 || comp_unit_contains_address (each, addr))
3043 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
3044 linenumber_ptr, stash);
3045 if (found)
3046 goto done;
3050 else
3052 for (each = stash->all_comp_units; each; each = each->next_unit)
3054 found = (comp_unit_contains_address (each, addr)
3055 && comp_unit_find_nearest_line (each, addr,
3056 filename_ptr,
3057 functionname_ptr,
3058 linenumber_ptr,
3059 stash));
3060 if (found)
3061 goto done;
3065 /* The DWARF2 spec says that the initial length field, and the
3066 offset of the abbreviation table, should both be 4-byte values.
3067 However, some compilers do things differently. */
3068 if (addr_size == 0)
3069 addr_size = 4;
3070 BFD_ASSERT (addr_size == 4 || addr_size == 8);
3072 /* Read each remaining comp. units checking each as they are read. */
3073 while (stash->info_ptr < stash->info_ptr_end)
3075 bfd_vma length;
3076 unsigned int offset_size = addr_size;
3077 bfd_byte *info_ptr_unit = stash->info_ptr;
3079 length = read_4_bytes (stash->bfd, stash->info_ptr);
3080 /* A 0xffffff length is the DWARF3 way of indicating
3081 we use 64-bit offsets, instead of 32-bit offsets. */
3082 if (length == 0xffffffff)
3084 offset_size = 8;
3085 length = read_8_bytes (stash->bfd, stash->info_ptr + 4);
3086 stash->info_ptr += 12;
3088 /* A zero length is the IRIX way of indicating 64-bit offsets,
3089 mostly because the 64-bit length will generally fit in 32
3090 bits, and the endianness helps. */
3091 else if (length == 0)
3093 offset_size = 8;
3094 length = read_4_bytes (stash->bfd, stash->info_ptr + 4);
3095 stash->info_ptr += 8;
3097 /* In the absence of the hints above, we assume 32-bit DWARF2
3098 offsets even for targets with 64-bit addresses, because:
3099 a) most of the time these targets will not have generated
3100 more than 2Gb of debug info and so will not need 64-bit
3101 offsets,
3103 b) if they do use 64-bit offsets but they are not using
3104 the size hints that are tested for above then they are
3105 not conforming to the DWARF3 standard anyway. */
3106 else if (addr_size == 8)
3108 offset_size = 4;
3109 stash->info_ptr += 4;
3111 else
3112 stash->info_ptr += 4;
3114 if (length > 0)
3116 each = parse_comp_unit (stash, length, info_ptr_unit,
3117 offset_size);
3118 if (!each)
3119 /* The dwarf information is damaged, don't trust it any
3120 more. */
3121 break;
3122 stash->info_ptr += length;
3124 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
3125 == stash->sec->size)
3127 stash->sec = find_debug_info (stash->bfd, stash->sec);
3128 stash->sec_info_ptr = stash->info_ptr;
3131 if (stash->all_comp_units)
3132 stash->all_comp_units->prev_unit = each;
3133 else
3134 stash->last_comp_unit = each;
3136 each->next_unit = stash->all_comp_units;
3137 stash->all_comp_units = each;
3139 /* DW_AT_low_pc and DW_AT_high_pc are optional for
3140 compilation units. If we don't have them (i.e.,
3141 unit->high == 0), we need to consult the line info table
3142 to see if a compilation unit contains the given
3143 address. */
3144 if (do_line)
3145 found = (((symbol->flags & BSF_FUNCTION) == 0
3146 || each->arange.high == 0
3147 || comp_unit_contains_address (each, addr))
3148 && comp_unit_find_line (each, symbol, addr,
3149 filename_ptr,
3150 linenumber_ptr,
3151 stash));
3152 else
3153 found = ((each->arange.high == 0
3154 || comp_unit_contains_address (each, addr))
3155 && comp_unit_find_nearest_line (each, addr,
3156 filename_ptr,
3157 functionname_ptr,
3158 linenumber_ptr,
3159 stash));
3160 if (found)
3161 goto done;
3165 done:
3166 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3167 unset_sections (stash);
3169 return found;
3172 /* The DWARF2 version of find_nearest_line.
3173 Return TRUE if the line is found without error. */
3175 bfd_boolean
3176 _bfd_dwarf2_find_nearest_line (bfd *abfd,
3177 asection *section,
3178 asymbol **symbols,
3179 bfd_vma offset,
3180 const char **filename_ptr,
3181 const char **functionname_ptr,
3182 unsigned int *linenumber_ptr,
3183 unsigned int addr_size,
3184 void **pinfo)
3186 return find_line (abfd, section, offset, NULL, symbols, filename_ptr,
3187 functionname_ptr, linenumber_ptr, addr_size,
3188 pinfo);
3191 /* The DWARF2 version of find_line.
3192 Return TRUE if the line is found without error. */
3194 bfd_boolean
3195 _bfd_dwarf2_find_line (bfd *abfd,
3196 asymbol **symbols,
3197 asymbol *symbol,
3198 const char **filename_ptr,
3199 unsigned int *linenumber_ptr,
3200 unsigned int addr_size,
3201 void **pinfo)
3203 return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr,
3204 NULL, linenumber_ptr, addr_size,
3205 pinfo);
3208 bfd_boolean
3209 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
3210 const char **filename_ptr,
3211 const char **functionname_ptr,
3212 unsigned int *linenumber_ptr,
3213 void **pinfo)
3215 struct dwarf2_debug *stash;
3217 stash = *pinfo;
3218 if (stash)
3220 struct funcinfo *func = stash->inliner_chain;
3222 if (func && func->caller_func)
3224 *filename_ptr = func->caller_file;
3225 *functionname_ptr = func->caller_func->name;
3226 *linenumber_ptr = func->caller_line;
3227 stash->inliner_chain = func->caller_func;
3228 return TRUE;
3232 return FALSE;
3235 void
3236 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
3238 struct comp_unit *each;
3239 struct dwarf2_debug *stash;
3241 if (abfd == NULL || elf_tdata (abfd) == NULL)
3242 return;
3244 stash = elf_tdata (abfd)->dwarf2_find_line_info;
3246 if (stash == NULL)
3247 return;
3249 for (each = stash->all_comp_units; each; each = each->next_unit)
3251 struct abbrev_info **abbrevs = each->abbrevs;
3252 struct funcinfo *function_table = each->function_table;
3253 struct varinfo *variable_table = each->variable_table;
3254 size_t i;
3256 for (i = 0; i < ABBREV_HASH_SIZE; i++)
3258 struct abbrev_info *abbrev = abbrevs[i];
3260 while (abbrev)
3262 free (abbrev->attrs);
3263 abbrev = abbrev->next;
3267 if (each->line_table)
3269 free (each->line_table->dirs);
3270 free (each->line_table->files);
3273 while (function_table)
3275 if (function_table->file)
3277 free (function_table->file);
3278 function_table->file = NULL;
3281 if (function_table->caller_file)
3283 free (function_table->caller_file);
3284 function_table->caller_file = NULL;
3286 function_table = function_table->prev_func;
3289 while (variable_table)
3291 if (variable_table->file)
3293 free (variable_table->file);
3294 variable_table->file = NULL;
3297 variable_table = variable_table->prev_var;
3301 free (stash->dwarf_abbrev_buffer);
3302 free (stash->dwarf_line_buffer);
3303 free (stash->dwarf_ranges_buffer);
3304 free (stash->info_ptr_memory);