Updated Bulgarian translation.
[binutils.git] / bfd / dwarf2.c
blob89d9f4b01d6a9a3ad3e93d23bc67255bc1e2a743
1 /* DWARF 2 support.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009, 2010 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 "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 adjusted_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_ptr;
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 sections whose VMA we must adjust. */
147 unsigned int adjusted_section_count;
149 /* Array of sections with adjusted VMA. */
150 struct adjusted_section *adjusted_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 /* Pointer to the start of the debug section, for DW_FORM_ref_addr. */
220 bfd_byte *sec_info_ptr;
222 /* The offset into .debug_line of the line number table. */
223 unsigned long line_offset;
225 /* Pointer to the first child die for the comp unit. */
226 bfd_byte *first_child_die_ptr;
228 /* The end of the comp unit. */
229 bfd_byte *end_ptr;
231 /* The decoded line number, NULL if not yet decoded. */
232 struct line_info_table *line_table;
234 /* A list of the functions found in this comp. unit. */
235 struct funcinfo *function_table;
237 /* A list of the variables found in this comp. unit. */
238 struct varinfo *variable_table;
240 /* Pointer to dwarf2_debug structure. */
241 struct dwarf2_debug *stash;
243 /* DWARF format version for this unit - from unit header. */
244 int version;
246 /* Address size for this unit - from unit header. */
247 unsigned char addr_size;
249 /* Offset size for this unit - from unit header. */
250 unsigned char offset_size;
252 /* Base address for this unit - from DW_AT_low_pc attribute of
253 DW_TAG_compile_unit DIE */
254 bfd_vma base_address;
256 /* TRUE if symbols are cached in hash table for faster lookup by name. */
257 bfd_boolean cached;
260 /* This data structure holds the information of an abbrev. */
261 struct abbrev_info
263 unsigned int number; /* Number identifying abbrev. */
264 enum dwarf_tag tag; /* DWARF tag. */
265 int has_children; /* Boolean. */
266 unsigned int num_attrs; /* Number of attributes. */
267 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
268 struct abbrev_info *next; /* Next in chain. */
271 struct attr_abbrev
273 enum dwarf_attribute name;
274 enum dwarf_form form;
277 #ifndef ABBREV_HASH_SIZE
278 #define ABBREV_HASH_SIZE 121
279 #endif
280 #ifndef ATTR_ALLOC_CHUNK
281 #define ATTR_ALLOC_CHUNK 4
282 #endif
284 /* Variable and function hash tables. This is used to speed up look-up
285 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
286 In order to share code between variable and function infos, we use
287 a list of untyped pointer for all variable/function info associated with
288 a symbol. We waste a bit of memory for list with one node but that
289 simplifies the code. */
291 struct info_list_node
293 struct info_list_node *next;
294 void *info;
297 /* Info hash entry. */
298 struct info_hash_entry
300 struct bfd_hash_entry root;
301 struct info_list_node *head;
304 struct info_hash_table
306 struct bfd_hash_table base;
309 /* Function to create a new entry in info hash table. */
311 static struct bfd_hash_entry *
312 info_hash_table_newfunc (struct bfd_hash_entry *entry,
313 struct bfd_hash_table *table,
314 const char *string)
316 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
318 /* Allocate the structure if it has not already been allocated by a
319 derived class. */
320 if (ret == NULL)
322 ret = (struct info_hash_entry *) bfd_hash_allocate (table,
323 sizeof (* ret));
324 if (ret == NULL)
325 return NULL;
328 /* Call the allocation method of the base class. */
329 ret = ((struct info_hash_entry *)
330 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
332 /* Initialize the local fields here. */
333 if (ret)
334 ret->head = NULL;
336 return (struct bfd_hash_entry *) ret;
339 /* Function to create a new info hash table. It returns a pointer to the
340 newly created table or NULL if there is any error. We need abfd
341 solely for memory allocation. */
343 static struct info_hash_table *
344 create_info_hash_table (bfd *abfd)
346 struct info_hash_table *hash_table;
348 hash_table = (struct info_hash_table *)
349 bfd_alloc (abfd, sizeof (struct info_hash_table));
350 if (!hash_table)
351 return hash_table;
353 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
354 sizeof (struct info_hash_entry)))
356 bfd_release (abfd, hash_table);
357 return NULL;
360 return hash_table;
363 /* Insert an info entry into an info hash table. We do not check of
364 duplicate entries. Also, the caller need to guarantee that the
365 right type of info in inserted as info is passed as a void* pointer.
366 This function returns true if there is no error. */
368 static bfd_boolean
369 insert_info_hash_table (struct info_hash_table *hash_table,
370 const char *key,
371 void *info,
372 bfd_boolean copy_p)
374 struct info_hash_entry *entry;
375 struct info_list_node *node;
377 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
378 key, TRUE, copy_p);
379 if (!entry)
380 return FALSE;
382 node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
383 sizeof (*node));
384 if (!node)
385 return FALSE;
387 node->info = info;
388 node->next = entry->head;
389 entry->head = node;
391 return TRUE;
394 /* Look up an info entry list from an info hash table. Return NULL
395 if there is none. */
397 static struct info_list_node *
398 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
400 struct info_hash_entry *entry;
402 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
403 FALSE, FALSE);
404 return entry ? entry->head : NULL;
407 /* Read a section into its appropriate place in the dwarf2_debug
408 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
409 not NULL, use bfd_simple_get_relocated_section_contents to read the
410 section contents, otherwise use bfd_get_section_contents. Fail if
411 the located section does not contain at least OFFSET bytes. */
413 static bfd_boolean
414 read_section (bfd * abfd,
415 const char * section_name,
416 const char * compressed_section_name,
417 asymbol ** syms,
418 bfd_uint64_t offset,
419 bfd_byte ** section_buffer,
420 bfd_size_type * section_size)
422 asection *msec;
423 bfd_boolean section_is_compressed = FALSE;
425 /* read_section is a noop if the section has already been read. */
426 if (!*section_buffer)
428 msec = bfd_get_section_by_name (abfd, section_name);
429 if (! msec && compressed_section_name)
431 msec = bfd_get_section_by_name (abfd, compressed_section_name);
432 section_is_compressed = TRUE;
434 if (! msec)
436 (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."), section_name);
437 bfd_set_error (bfd_error_bad_value);
438 return FALSE;
441 *section_size = msec->rawsize ? msec->rawsize : msec->size;
442 if (syms)
444 *section_buffer
445 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
446 if (! *section_buffer)
447 return FALSE;
449 else
451 *section_buffer = (bfd_byte *) bfd_malloc (*section_size);
452 if (! *section_buffer)
453 return FALSE;
454 if (! bfd_get_section_contents (abfd, msec, *section_buffer,
455 0, *section_size))
456 return FALSE;
459 if (section_is_compressed)
461 if (! bfd_uncompress_section_contents (section_buffer, section_size))
463 (*_bfd_error_handler) (_("Dwarf Error: unable to decompress %s section."), compressed_section_name);
464 bfd_set_error (bfd_error_bad_value);
465 return FALSE;
470 /* It is possible to get a bad value for the offset into the section
471 that the client wants. Validate it here to avoid trouble later. */
472 if (offset != 0 && offset >= *section_size)
474 (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu) greater than or equal to %s size (%lu)."),
475 (long) offset, section_name, *section_size);
476 bfd_set_error (bfd_error_bad_value);
477 return FALSE;
480 return TRUE;
483 /* VERBATIM
484 The following function up to the END VERBATIM mark are
485 copied directly from dwarf2read.c. */
487 /* Read dwarf information from a buffer. */
489 static unsigned int
490 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
492 return bfd_get_8 (abfd, buf);
495 static int
496 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
498 return bfd_get_signed_8 (abfd, buf);
501 static unsigned int
502 read_2_bytes (bfd *abfd, bfd_byte *buf)
504 return bfd_get_16 (abfd, buf);
507 static unsigned int
508 read_4_bytes (bfd *abfd, bfd_byte *buf)
510 return bfd_get_32 (abfd, buf);
513 static bfd_uint64_t
514 read_8_bytes (bfd *abfd, bfd_byte *buf)
516 return bfd_get_64 (abfd, buf);
519 static bfd_byte *
520 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
521 bfd_byte *buf,
522 unsigned int size ATTRIBUTE_UNUSED)
524 return buf;
527 static char *
528 read_string (bfd *abfd ATTRIBUTE_UNUSED,
529 bfd_byte *buf,
530 unsigned int *bytes_read_ptr)
532 /* Return a pointer to the embedded string. */
533 char *str = (char *) buf;
535 if (*str == '\0')
537 *bytes_read_ptr = 1;
538 return NULL;
541 *bytes_read_ptr = strlen (str) + 1;
542 return str;
545 /* END VERBATIM */
547 static char *
548 read_indirect_string (struct comp_unit * unit,
549 bfd_byte * buf,
550 unsigned int * bytes_read_ptr)
552 bfd_uint64_t offset;
553 struct dwarf2_debug *stash = unit->stash;
554 char *str;
556 if (unit->offset_size == 4)
557 offset = read_4_bytes (unit->abfd, buf);
558 else
559 offset = read_8_bytes (unit->abfd, buf);
561 *bytes_read_ptr = unit->offset_size;
563 if (! read_section (unit->abfd, ".debug_str", ".zdebug_str",
564 stash->syms, offset,
565 &stash->dwarf_str_buffer, &stash->dwarf_str_size))
566 return NULL;
568 str = (char *) stash->dwarf_str_buffer + offset;
569 if (*str == '\0')
570 return NULL;
571 return str;
574 static bfd_uint64_t
575 read_address (struct comp_unit *unit, bfd_byte *buf)
577 int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
579 if (signed_vma)
581 switch (unit->addr_size)
583 case 8:
584 return bfd_get_signed_64 (unit->abfd, buf);
585 case 4:
586 return bfd_get_signed_32 (unit->abfd, buf);
587 case 2:
588 return bfd_get_signed_16 (unit->abfd, buf);
589 default:
590 abort ();
593 else
595 switch (unit->addr_size)
597 case 8:
598 return bfd_get_64 (unit->abfd, buf);
599 case 4:
600 return bfd_get_32 (unit->abfd, buf);
601 case 2:
602 return bfd_get_16 (unit->abfd, buf);
603 default:
604 abort ();
609 /* Lookup an abbrev_info structure in the abbrev hash table. */
611 static struct abbrev_info *
612 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
614 unsigned int hash_number;
615 struct abbrev_info *abbrev;
617 hash_number = number % ABBREV_HASH_SIZE;
618 abbrev = abbrevs[hash_number];
620 while (abbrev)
622 if (abbrev->number == number)
623 return abbrev;
624 else
625 abbrev = abbrev->next;
628 return NULL;
631 /* In DWARF version 2, the description of the debugging information is
632 stored in a separate .debug_abbrev section. Before we read any
633 dies from a section we read in all abbreviations and install them
634 in a hash table. */
636 static struct abbrev_info**
637 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
639 struct abbrev_info **abbrevs;
640 bfd_byte *abbrev_ptr;
641 struct abbrev_info *cur_abbrev;
642 unsigned int abbrev_number, bytes_read, abbrev_name;
643 unsigned int abbrev_form, hash_number;
644 bfd_size_type amt;
646 if (! read_section (abfd, ".debug_abbrev", ".zdebug_abbrev",
647 stash->syms, offset,
648 &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
649 return 0;
651 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
652 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
654 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
655 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
656 abbrev_ptr += bytes_read;
658 /* Loop until we reach an abbrev number of 0. */
659 while (abbrev_number)
661 amt = sizeof (struct abbrev_info);
662 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
664 /* Read in abbrev header. */
665 cur_abbrev->number = abbrev_number;
666 cur_abbrev->tag = (enum dwarf_tag)
667 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
668 abbrev_ptr += bytes_read;
669 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
670 abbrev_ptr += 1;
672 /* Now read in declarations. */
673 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
674 abbrev_ptr += bytes_read;
675 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
676 abbrev_ptr += bytes_read;
678 while (abbrev_name)
680 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
682 struct attr_abbrev *tmp;
684 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
685 amt *= sizeof (struct attr_abbrev);
686 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
687 if (tmp == NULL)
689 size_t i;
691 for (i = 0; i < ABBREV_HASH_SIZE; i++)
693 struct abbrev_info *abbrev = abbrevs[i];
695 while (abbrev)
697 free (abbrev->attrs);
698 abbrev = abbrev->next;
701 return NULL;
703 cur_abbrev->attrs = tmp;
706 cur_abbrev->attrs[cur_abbrev->num_attrs].name
707 = (enum dwarf_attribute) abbrev_name;
708 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
709 = (enum dwarf_form) abbrev_form;
710 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
711 abbrev_ptr += bytes_read;
712 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
713 abbrev_ptr += bytes_read;
716 hash_number = abbrev_number % ABBREV_HASH_SIZE;
717 cur_abbrev->next = abbrevs[hash_number];
718 abbrevs[hash_number] = cur_abbrev;
720 /* Get next abbreviation.
721 Under Irix6 the abbreviations for a compilation unit are not
722 always properly terminated with an abbrev number of 0.
723 Exit loop if we encounter an abbreviation which we have
724 already read (which means we are about to read the abbreviations
725 for the next compile unit) or if the end of the abbreviation
726 table is reached. */
727 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
728 >= stash->dwarf_abbrev_size)
729 break;
730 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
731 abbrev_ptr += bytes_read;
732 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
733 break;
736 return abbrevs;
739 /* Read an attribute value described by an attribute form. */
741 static bfd_byte *
742 read_attribute_value (struct attribute *attr,
743 unsigned form,
744 struct comp_unit *unit,
745 bfd_byte *info_ptr)
747 bfd *abfd = unit->abfd;
748 unsigned int bytes_read;
749 struct dwarf_block *blk;
750 bfd_size_type amt;
752 attr->form = (enum dwarf_form) form;
754 switch (form)
756 case DW_FORM_ref_addr:
757 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
758 DWARF3. */
759 if (unit->version == 3)
761 if (unit->offset_size == 4)
762 attr->u.val = read_4_bytes (unit->abfd, info_ptr);
763 else
764 attr->u.val = read_8_bytes (unit->abfd, info_ptr);
765 info_ptr += unit->offset_size;
766 break;
768 /* FALLTHROUGH */
769 case DW_FORM_addr:
770 attr->u.val = read_address (unit, info_ptr);
771 info_ptr += unit->addr_size;
772 break;
773 case DW_FORM_block2:
774 amt = sizeof (struct dwarf_block);
775 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
776 blk->size = read_2_bytes (abfd, info_ptr);
777 info_ptr += 2;
778 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
779 info_ptr += blk->size;
780 attr->u.blk = blk;
781 break;
782 case DW_FORM_block4:
783 amt = sizeof (struct dwarf_block);
784 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
785 blk->size = read_4_bytes (abfd, info_ptr);
786 info_ptr += 4;
787 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
788 info_ptr += blk->size;
789 attr->u.blk = blk;
790 break;
791 case DW_FORM_data2:
792 attr->u.val = read_2_bytes (abfd, info_ptr);
793 info_ptr += 2;
794 break;
795 case DW_FORM_data4:
796 attr->u.val = read_4_bytes (abfd, info_ptr);
797 info_ptr += 4;
798 break;
799 case DW_FORM_data8:
800 attr->u.val = read_8_bytes (abfd, info_ptr);
801 info_ptr += 8;
802 break;
803 case DW_FORM_string:
804 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
805 info_ptr += bytes_read;
806 break;
807 case DW_FORM_strp:
808 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
809 info_ptr += bytes_read;
810 break;
811 case DW_FORM_block:
812 amt = sizeof (struct dwarf_block);
813 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
814 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
815 info_ptr += bytes_read;
816 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
817 info_ptr += blk->size;
818 attr->u.blk = blk;
819 break;
820 case DW_FORM_block1:
821 amt = sizeof (struct dwarf_block);
822 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
823 blk->size = read_1_byte (abfd, info_ptr);
824 info_ptr += 1;
825 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
826 info_ptr += blk->size;
827 attr->u.blk = blk;
828 break;
829 case DW_FORM_data1:
830 attr->u.val = read_1_byte (abfd, info_ptr);
831 info_ptr += 1;
832 break;
833 case DW_FORM_flag:
834 attr->u.val = read_1_byte (abfd, info_ptr);
835 info_ptr += 1;
836 break;
837 case DW_FORM_sdata:
838 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
839 info_ptr += bytes_read;
840 break;
841 case DW_FORM_udata:
842 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
843 info_ptr += bytes_read;
844 break;
845 case DW_FORM_ref1:
846 attr->u.val = read_1_byte (abfd, info_ptr);
847 info_ptr += 1;
848 break;
849 case DW_FORM_ref2:
850 attr->u.val = read_2_bytes (abfd, info_ptr);
851 info_ptr += 2;
852 break;
853 case DW_FORM_ref4:
854 attr->u.val = read_4_bytes (abfd, info_ptr);
855 info_ptr += 4;
856 break;
857 case DW_FORM_ref8:
858 attr->u.val = read_8_bytes (abfd, info_ptr);
859 info_ptr += 8;
860 break;
861 case DW_FORM_ref_sig8:
862 attr->u.val = read_8_bytes (abfd, info_ptr);
863 info_ptr += 8;
864 break;
865 case DW_FORM_ref_udata:
866 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
867 info_ptr += bytes_read;
868 break;
869 case DW_FORM_indirect:
870 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
871 info_ptr += bytes_read;
872 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
873 break;
874 default:
875 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
876 form);
877 bfd_set_error (bfd_error_bad_value);
879 return info_ptr;
882 /* Read an attribute described by an abbreviated attribute. */
884 static bfd_byte *
885 read_attribute (struct attribute *attr,
886 struct attr_abbrev *abbrev,
887 struct comp_unit *unit,
888 bfd_byte *info_ptr)
890 attr->name = abbrev->name;
891 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
892 return info_ptr;
895 /* Source line information table routines. */
897 #define FILE_ALLOC_CHUNK 5
898 #define DIR_ALLOC_CHUNK 5
900 struct line_info
902 struct line_info* prev_line;
903 bfd_vma address;
904 char *filename;
905 unsigned int line;
906 unsigned int column;
907 int end_sequence; /* End of (sequential) code sequence. */
910 struct fileinfo
912 char *name;
913 unsigned int dir;
914 unsigned int time;
915 unsigned int size;
918 struct line_sequence
920 bfd_vma low_pc;
921 struct line_sequence* prev_sequence;
922 struct line_info* last_line; /* Largest VMA. */
925 struct line_info_table
927 bfd* abfd;
928 unsigned int num_files;
929 unsigned int num_dirs;
930 unsigned int num_sequences;
931 char * comp_dir;
932 char ** dirs;
933 struct fileinfo* files;
934 struct line_sequence* sequences;
935 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
938 /* Remember some information about each function. If the function is
939 inlined (DW_TAG_inlined_subroutine) it may have two additional
940 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
941 source code location where this function was inlined. */
943 struct funcinfo
945 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */
946 struct funcinfo *caller_func; /* Pointer to function one scope higher */
947 char *caller_file; /* Source location file name where caller_func inlines this func */
948 int caller_line; /* Source location line number where caller_func inlines this func */
949 char *file; /* Source location file name */
950 int line; /* Source location line number */
951 int tag;
952 char *name;
953 struct arange arange;
954 asection *sec; /* Where the symbol is defined */
957 struct varinfo
959 /* Pointer to previous variable in list of all variables */
960 struct varinfo *prev_var;
961 /* Source location file name */
962 char *file;
963 /* Source location line number */
964 int line;
965 int tag;
966 char *name;
967 bfd_vma addr;
968 /* Where the symbol is defined */
969 asection *sec;
970 /* Is this a stack variable? */
971 unsigned int stack: 1;
974 /* Return TRUE if NEW_LINE should sort after LINE. */
976 static inline bfd_boolean
977 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
979 return (new_line->address > line->address
980 || (new_line->address == line->address
981 && new_line->end_sequence < line->end_sequence));
985 /* Adds a new entry to the line_info list in the line_info_table, ensuring
986 that the list is sorted. Note that the line_info list is sorted from
987 highest to lowest VMA (with possible duplicates); that is,
988 line_info->prev_line always accesses an equal or smaller VMA. */
990 static void
991 add_line_info (struct line_info_table *table,
992 bfd_vma address,
993 char *filename,
994 unsigned int line,
995 unsigned int column,
996 int end_sequence)
998 bfd_size_type amt = sizeof (struct line_info);
999 struct line_sequence* seq = table->sequences;
1000 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
1002 /* Set member data of 'info'. */
1003 info->address = address;
1004 info->line = line;
1005 info->column = column;
1006 info->end_sequence = end_sequence;
1008 if (filename && filename[0])
1010 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1011 if (info->filename)
1012 strcpy (info->filename, filename);
1014 else
1015 info->filename = NULL;
1017 /* Find the correct location for 'info'. Normally we will receive
1018 new line_info data 1) in order and 2) with increasing VMAs.
1019 However some compilers break the rules (cf. decode_line_info) and
1020 so we include some heuristics for quickly finding the correct
1021 location for 'info'. In particular, these heuristics optimize for
1022 the common case in which the VMA sequence that we receive is a
1023 list of locally sorted VMAs such as
1024 p...z a...j (where a < j < p < z)
1026 Note: table->lcl_head is used to head an *actual* or *possible*
1027 sub-sequence within the list (such as a...j) that is not directly
1028 headed by table->last_line
1030 Note: we may receive duplicate entries from 'decode_line_info'. */
1032 if (seq
1033 && seq->last_line->address == address
1034 && seq->last_line->end_sequence == end_sequence)
1036 /* We only keep the last entry with the same address and end
1037 sequence. See PR ld/4986. */
1038 if (table->lcl_head == seq->last_line)
1039 table->lcl_head = info;
1040 info->prev_line = seq->last_line->prev_line;
1041 seq->last_line = info;
1043 else if (!seq || seq->last_line->end_sequence)
1045 /* Start a new line sequence. */
1046 amt = sizeof (struct line_sequence);
1047 seq = (struct line_sequence *) bfd_malloc (amt);
1048 seq->low_pc = address;
1049 seq->prev_sequence = table->sequences;
1050 seq->last_line = info;
1051 table->lcl_head = info;
1052 table->sequences = seq;
1053 table->num_sequences++;
1055 else if (new_line_sorts_after (info, seq->last_line))
1057 /* Normal case: add 'info' to the beginning of the current sequence. */
1058 info->prev_line = seq->last_line;
1059 seq->last_line = info;
1061 /* lcl_head: initialize to head a *possible* sequence at the end. */
1062 if (!table->lcl_head)
1063 table->lcl_head = info;
1065 else if (!new_line_sorts_after (info, table->lcl_head)
1066 && (!table->lcl_head->prev_line
1067 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1069 /* Abnormal but easy: lcl_head is the head of 'info'. */
1070 info->prev_line = table->lcl_head->prev_line;
1071 table->lcl_head->prev_line = info;
1073 else
1075 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1076 are valid heads for 'info'. Reset 'lcl_head'. */
1077 struct line_info* li2 = seq->last_line; /* Always non-NULL. */
1078 struct line_info* li1 = li2->prev_line;
1080 while (li1)
1082 if (!new_line_sorts_after (info, li2)
1083 && new_line_sorts_after (info, li1))
1084 break;
1086 li2 = li1; /* always non-NULL */
1087 li1 = li1->prev_line;
1089 table->lcl_head = li2;
1090 info->prev_line = table->lcl_head->prev_line;
1091 table->lcl_head->prev_line = info;
1092 if (address < seq->low_pc)
1093 seq->low_pc = address;
1097 /* Extract a fully qualified filename from a line info table.
1098 The returned string has been malloc'ed and it is the caller's
1099 responsibility to free it. */
1101 static char *
1102 concat_filename (struct line_info_table *table, unsigned int file)
1104 char *filename;
1106 if (file - 1 >= table->num_files)
1108 /* FILE == 0 means unknown. */
1109 if (file)
1110 (*_bfd_error_handler)
1111 (_("Dwarf Error: mangled line number section (bad file number)."));
1112 return strdup ("<unknown>");
1115 filename = table->files[file - 1].name;
1117 if (!IS_ABSOLUTE_PATH (filename))
1119 char *dir_name = NULL;
1120 char *subdir_name = NULL;
1121 char *name;
1122 size_t len;
1124 if (table->files[file - 1].dir)
1125 subdir_name = table->dirs[table->files[file - 1].dir - 1];
1127 if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
1128 dir_name = table->comp_dir;
1130 if (!dir_name)
1132 dir_name = subdir_name;
1133 subdir_name = NULL;
1136 if (!dir_name)
1137 return strdup (filename);
1139 len = strlen (dir_name) + strlen (filename) + 2;
1141 if (subdir_name)
1143 len += strlen (subdir_name) + 1;
1144 name = (char *) bfd_malloc (len);
1145 if (name)
1146 sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
1148 else
1150 name = (char *) bfd_malloc (len);
1151 if (name)
1152 sprintf (name, "%s/%s", dir_name, filename);
1155 return name;
1158 return strdup (filename);
1161 static void
1162 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
1164 struct arange *arange;
1166 /* If the first arange is empty, use it. */
1167 if (first_arange->high == 0)
1169 first_arange->low = low_pc;
1170 first_arange->high = high_pc;
1171 return;
1174 /* Next see if we can cheaply extend an existing range. */
1175 arange = first_arange;
1178 if (low_pc == arange->high)
1180 arange->high = high_pc;
1181 return;
1183 if (high_pc == arange->low)
1185 arange->low = low_pc;
1186 return;
1188 arange = arange->next;
1190 while (arange);
1192 /* Need to allocate a new arange and insert it into the arange list.
1193 Order isn't significant, so just insert after the first arange. */
1194 arange = (struct arange *) bfd_zalloc (abfd, sizeof (*arange));
1195 arange->low = low_pc;
1196 arange->high = high_pc;
1197 arange->next = first_arange->next;
1198 first_arange->next = arange;
1201 /* Compare function for line sequences. */
1203 static int
1204 compare_sequences (const void* a, const void* b)
1206 const struct line_sequence* seq1 = a;
1207 const struct line_sequence* seq2 = b;
1209 /* Sort by low_pc as the primary key. */
1210 if (seq1->low_pc < seq2->low_pc)
1211 return -1;
1212 if (seq1->low_pc > seq2->low_pc)
1213 return 1;
1215 /* If low_pc values are equal, sort in reverse order of
1216 high_pc, so that the largest region comes first. */
1217 if (seq1->last_line->address < seq2->last_line->address)
1218 return 1;
1219 if (seq1->last_line->address > seq2->last_line->address)
1220 return -1;
1222 return 0;
1225 /* Sort the line sequences for quick lookup. */
1227 static void
1228 sort_line_sequences (struct line_info_table* table)
1230 bfd_size_type amt;
1231 struct line_sequence* sequences;
1232 struct line_sequence* seq;
1233 unsigned int n = 0;
1234 unsigned int num_sequences = table->num_sequences;
1235 bfd_vma last_high_pc;
1237 if (num_sequences == 0)
1238 return;
1240 /* Allocate space for an array of sequences. */
1241 amt = sizeof (struct line_sequence) * num_sequences;
1242 sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
1244 /* Copy the linked list into the array, freeing the original nodes. */
1245 seq = table->sequences;
1246 for (n = 0; n < num_sequences; n++)
1248 struct line_sequence* last_seq = seq;
1250 BFD_ASSERT (seq);
1251 sequences[n].low_pc = seq->low_pc;
1252 sequences[n].prev_sequence = NULL;
1253 sequences[n].last_line = seq->last_line;
1254 seq = seq->prev_sequence;
1255 free (last_seq);
1257 BFD_ASSERT (seq == NULL);
1259 qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
1261 /* Make the list binary-searchable by trimming overlapping entries
1262 and removing nested entries. */
1263 num_sequences = 1;
1264 last_high_pc = sequences[0].last_line->address;
1265 for (n = 1; n < table->num_sequences; n++)
1267 if (sequences[n].low_pc < last_high_pc)
1269 if (sequences[n].last_line->address <= last_high_pc)
1270 /* Skip nested entries. */
1271 continue;
1273 /* Trim overlapping entries. */
1274 sequences[n].low_pc = last_high_pc;
1276 last_high_pc = sequences[n].last_line->address;
1277 if (n > num_sequences)
1279 /* Close up the gap. */
1280 sequences[num_sequences].low_pc = sequences[n].low_pc;
1281 sequences[num_sequences].last_line = sequences[n].last_line;
1283 num_sequences++;
1286 table->sequences = sequences;
1287 table->num_sequences = num_sequences;
1290 /* Decode the line number information for UNIT. */
1292 static struct line_info_table*
1293 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
1295 bfd *abfd = unit->abfd;
1296 struct line_info_table* table;
1297 bfd_byte *line_ptr;
1298 bfd_byte *line_end;
1299 struct line_head lh;
1300 unsigned int i, bytes_read, offset_size;
1301 char *cur_file, *cur_dir;
1302 unsigned char op_code, extended_op, adj_opcode;
1303 bfd_size_type amt;
1305 if (! read_section (abfd, ".debug_line", ".zdebug_line",
1306 stash->syms, unit->line_offset,
1307 &stash->dwarf_line_buffer, &stash->dwarf_line_size))
1308 return 0;
1310 amt = sizeof (struct line_info_table);
1311 table = (struct line_info_table *) bfd_alloc (abfd, amt);
1312 table->abfd = abfd;
1313 table->comp_dir = unit->comp_dir;
1315 table->num_files = 0;
1316 table->files = NULL;
1318 table->num_dirs = 0;
1319 table->dirs = NULL;
1321 table->num_sequences = 0;
1322 table->sequences = NULL;
1324 table->lcl_head = NULL;
1326 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1328 /* Read in the prologue. */
1329 lh.total_length = read_4_bytes (abfd, line_ptr);
1330 line_ptr += 4;
1331 offset_size = 4;
1332 if (lh.total_length == 0xffffffff)
1334 lh.total_length = read_8_bytes (abfd, line_ptr);
1335 line_ptr += 8;
1336 offset_size = 8;
1338 else if (lh.total_length == 0 && unit->addr_size == 8)
1340 /* Handle (non-standard) 64-bit DWARF2 formats. */
1341 lh.total_length = read_4_bytes (abfd, line_ptr);
1342 line_ptr += 4;
1343 offset_size = 8;
1345 line_end = line_ptr + lh.total_length;
1346 lh.version = read_2_bytes (abfd, line_ptr);
1347 line_ptr += 2;
1348 if (offset_size == 4)
1349 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1350 else
1351 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1352 line_ptr += offset_size;
1353 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1354 line_ptr += 1;
1355 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1356 line_ptr += 1;
1357 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1358 line_ptr += 1;
1359 lh.line_range = read_1_byte (abfd, line_ptr);
1360 line_ptr += 1;
1361 lh.opcode_base = read_1_byte (abfd, line_ptr);
1362 line_ptr += 1;
1363 amt = lh.opcode_base * sizeof (unsigned char);
1364 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
1366 lh.standard_opcode_lengths[0] = 1;
1368 for (i = 1; i < lh.opcode_base; ++i)
1370 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1371 line_ptr += 1;
1374 /* Read directory table. */
1375 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1377 line_ptr += bytes_read;
1379 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1381 char **tmp;
1383 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1384 amt *= sizeof (char *);
1386 tmp = (char **) bfd_realloc (table->dirs, amt);
1387 if (tmp == NULL)
1389 free (table->dirs);
1390 return NULL;
1392 table->dirs = tmp;
1395 table->dirs[table->num_dirs++] = cur_dir;
1398 line_ptr += bytes_read;
1400 /* Read file name table. */
1401 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1403 line_ptr += bytes_read;
1405 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1407 struct fileinfo *tmp;
1409 amt = table->num_files + FILE_ALLOC_CHUNK;
1410 amt *= sizeof (struct fileinfo);
1412 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1413 if (tmp == NULL)
1415 free (table->files);
1416 free (table->dirs);
1417 return NULL;
1419 table->files = tmp;
1422 table->files[table->num_files].name = cur_file;
1423 table->files[table->num_files].dir =
1424 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1425 line_ptr += bytes_read;
1426 table->files[table->num_files].time =
1427 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1428 line_ptr += bytes_read;
1429 table->files[table->num_files].size =
1430 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1431 line_ptr += bytes_read;
1432 table->num_files++;
1435 line_ptr += bytes_read;
1437 /* Read the statement sequences until there's nothing left. */
1438 while (line_ptr < line_end)
1440 /* State machine registers. */
1441 bfd_vma address = 0;
1442 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1443 unsigned int line = 1;
1444 unsigned int column = 0;
1445 int is_stmt = lh.default_is_stmt;
1446 int end_sequence = 0;
1447 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1448 compilers generate address sequences that are wildly out of
1449 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1450 for ia64-Linux). Thus, to determine the low and high
1451 address, we must compare on every DW_LNS_copy, etc. */
1452 bfd_vma low_pc = (bfd_vma) -1;
1453 bfd_vma high_pc = 0;
1455 /* Decode the table. */
1456 while (! end_sequence)
1458 op_code = read_1_byte (abfd, line_ptr);
1459 line_ptr += 1;
1461 if (op_code >= lh.opcode_base)
1463 /* Special operand. */
1464 adj_opcode = op_code - lh.opcode_base;
1465 address += (adj_opcode / lh.line_range)
1466 * lh.minimum_instruction_length;
1467 line += lh.line_base + (adj_opcode % lh.line_range);
1468 /* Append row to matrix using current values. */
1469 add_line_info (table, address, filename, line, column, 0);
1470 if (address < low_pc)
1471 low_pc = address;
1472 if (address > high_pc)
1473 high_pc = address;
1475 else switch (op_code)
1477 case DW_LNS_extended_op:
1478 /* Ignore length. */
1479 line_ptr += 1;
1480 extended_op = read_1_byte (abfd, line_ptr);
1481 line_ptr += 1;
1483 switch (extended_op)
1485 case DW_LNE_end_sequence:
1486 end_sequence = 1;
1487 add_line_info (table, address, filename, line, column,
1488 end_sequence);
1489 if (address < low_pc)
1490 low_pc = address;
1491 if (address > high_pc)
1492 high_pc = address;
1493 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1494 break;
1495 case DW_LNE_set_address:
1496 address = read_address (unit, line_ptr);
1497 line_ptr += unit->addr_size;
1498 break;
1499 case DW_LNE_define_file:
1500 cur_file = read_string (abfd, line_ptr, &bytes_read);
1501 line_ptr += bytes_read;
1502 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1504 struct fileinfo *tmp;
1506 amt = table->num_files + FILE_ALLOC_CHUNK;
1507 amt *= sizeof (struct fileinfo);
1508 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1509 if (tmp == NULL)
1511 free (table->files);
1512 free (table->dirs);
1513 free (filename);
1514 return NULL;
1516 table->files = tmp;
1518 table->files[table->num_files].name = cur_file;
1519 table->files[table->num_files].dir =
1520 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1521 line_ptr += bytes_read;
1522 table->files[table->num_files].time =
1523 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1524 line_ptr += bytes_read;
1525 table->files[table->num_files].size =
1526 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1527 line_ptr += bytes_read;
1528 table->num_files++;
1529 break;
1530 case DW_LNE_set_discriminator:
1531 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1532 line_ptr += bytes_read;
1533 break;
1534 default:
1535 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1536 bfd_set_error (bfd_error_bad_value);
1537 free (filename);
1538 free (table->files);
1539 free (table->dirs);
1540 return NULL;
1542 break;
1543 case DW_LNS_copy:
1544 add_line_info (table, address, filename, line, column, 0);
1545 if (address < low_pc)
1546 low_pc = address;
1547 if (address > high_pc)
1548 high_pc = address;
1549 break;
1550 case DW_LNS_advance_pc:
1551 address += lh.minimum_instruction_length
1552 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1553 line_ptr += bytes_read;
1554 break;
1555 case DW_LNS_advance_line:
1556 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1557 line_ptr += bytes_read;
1558 break;
1559 case DW_LNS_set_file:
1561 unsigned int file;
1563 /* The file and directory tables are 0
1564 based, the references are 1 based. */
1565 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1566 line_ptr += bytes_read;
1567 if (filename)
1568 free (filename);
1569 filename = concat_filename (table, file);
1570 break;
1572 case DW_LNS_set_column:
1573 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1574 line_ptr += bytes_read;
1575 break;
1576 case DW_LNS_negate_stmt:
1577 is_stmt = (!is_stmt);
1578 break;
1579 case DW_LNS_set_basic_block:
1580 break;
1581 case DW_LNS_const_add_pc:
1582 address += lh.minimum_instruction_length
1583 * ((255 - lh.opcode_base) / lh.line_range);
1584 break;
1585 case DW_LNS_fixed_advance_pc:
1586 address += read_2_bytes (abfd, line_ptr);
1587 line_ptr += 2;
1588 break;
1589 default:
1590 /* Unknown standard opcode, ignore it. */
1591 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1593 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1594 line_ptr += bytes_read;
1596 break;
1600 if (filename)
1601 free (filename);
1604 sort_line_sequences (table);
1606 return table;
1609 /* If ADDR is within TABLE set the output parameters and return TRUE,
1610 otherwise return FALSE. The output parameters, FILENAME_PTR and
1611 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1613 static bfd_boolean
1614 lookup_address_in_line_info_table (struct line_info_table *table,
1615 bfd_vma addr,
1616 const char **filename_ptr,
1617 unsigned int *linenumber_ptr)
1619 struct line_sequence *seq = NULL;
1620 struct line_info *each_line;
1621 int low, high, mid;
1623 /* Binary search the array of sequences. */
1624 low = 0;
1625 high = table->num_sequences;
1626 while (low < high)
1628 mid = (low + high) / 2;
1629 seq = &table->sequences[mid];
1630 if (addr < seq->low_pc)
1631 high = mid;
1632 else if (addr >= seq->last_line->address)
1633 low = mid + 1;
1634 else
1635 break;
1638 if (seq && addr >= seq->low_pc && addr < seq->last_line->address)
1640 /* Note: seq->last_line should be a descendingly sorted list. */
1641 for (each_line = seq->last_line;
1642 each_line;
1643 each_line = each_line->prev_line)
1644 if (addr >= each_line->address)
1645 break;
1647 if (each_line
1648 && !(each_line->end_sequence || each_line == seq->last_line))
1650 *filename_ptr = each_line->filename;
1651 *linenumber_ptr = each_line->line;
1652 return TRUE;
1656 *filename_ptr = NULL;
1657 return FALSE;
1660 /* Read in the .debug_ranges section for future reference. */
1662 static bfd_boolean
1663 read_debug_ranges (struct comp_unit *unit)
1665 struct dwarf2_debug *stash = unit->stash;
1666 return read_section (unit->abfd, ".debug_ranges", ".zdebug_ranges",
1667 stash->syms, 0,
1668 &stash->dwarf_ranges_buffer, &stash->dwarf_ranges_size);
1671 /* Function table functions. */
1673 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1674 Note that we need to find the function that has the smallest
1675 range that contains ADDR, to handle inlined functions without
1676 depending upon them being ordered in TABLE by increasing range. */
1678 static bfd_boolean
1679 lookup_address_in_function_table (struct comp_unit *unit,
1680 bfd_vma addr,
1681 struct funcinfo **function_ptr,
1682 const char **functionname_ptr)
1684 struct funcinfo* each_func;
1685 struct funcinfo* best_fit = NULL;
1686 struct arange *arange;
1688 for (each_func = unit->function_table;
1689 each_func;
1690 each_func = each_func->prev_func)
1692 for (arange = &each_func->arange;
1693 arange;
1694 arange = arange->next)
1696 if (addr >= arange->low && addr < arange->high)
1698 if (!best_fit ||
1699 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1700 best_fit = each_func;
1705 if (best_fit)
1707 *functionname_ptr = best_fit->name;
1708 *function_ptr = best_fit;
1709 return TRUE;
1711 else
1713 return FALSE;
1717 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1718 and LINENUMBER_PTR, and return TRUE. */
1720 static bfd_boolean
1721 lookup_symbol_in_function_table (struct comp_unit *unit,
1722 asymbol *sym,
1723 bfd_vma addr,
1724 const char **filename_ptr,
1725 unsigned int *linenumber_ptr)
1727 struct funcinfo* each_func;
1728 struct funcinfo* best_fit = NULL;
1729 struct arange *arange;
1730 const char *name = bfd_asymbol_name (sym);
1731 asection *sec = bfd_get_section (sym);
1733 for (each_func = unit->function_table;
1734 each_func;
1735 each_func = each_func->prev_func)
1737 for (arange = &each_func->arange;
1738 arange;
1739 arange = arange->next)
1741 if ((!each_func->sec || each_func->sec == sec)
1742 && addr >= arange->low
1743 && addr < arange->high
1744 && each_func->name
1745 && strcmp (name, each_func->name) == 0
1746 && (!best_fit
1747 || ((arange->high - arange->low)
1748 < (best_fit->arange.high - best_fit->arange.low))))
1749 best_fit = each_func;
1753 if (best_fit)
1755 best_fit->sec = sec;
1756 *filename_ptr = best_fit->file;
1757 *linenumber_ptr = best_fit->line;
1758 return TRUE;
1760 else
1761 return FALSE;
1764 /* Variable table functions. */
1766 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1767 LINENUMBER_PTR, and return TRUE. */
1769 static bfd_boolean
1770 lookup_symbol_in_variable_table (struct comp_unit *unit,
1771 asymbol *sym,
1772 bfd_vma addr,
1773 const char **filename_ptr,
1774 unsigned int *linenumber_ptr)
1776 const char *name = bfd_asymbol_name (sym);
1777 asection *sec = bfd_get_section (sym);
1778 struct varinfo* each;
1780 for (each = unit->variable_table; each; each = each->prev_var)
1781 if (each->stack == 0
1782 && each->file != NULL
1783 && each->name != NULL
1784 && each->addr == addr
1785 && (!each->sec || each->sec == sec)
1786 && strcmp (name, each->name) == 0)
1787 break;
1789 if (each)
1791 each->sec = sec;
1792 *filename_ptr = each->file;
1793 *linenumber_ptr = each->line;
1794 return TRUE;
1796 else
1797 return FALSE;
1800 static char *
1801 find_abstract_instance_name (struct comp_unit *unit,
1802 struct attribute *attr_ptr)
1804 bfd *abfd = unit->abfd;
1805 bfd_byte *info_ptr;
1806 unsigned int abbrev_number, bytes_read, i;
1807 struct abbrev_info *abbrev;
1808 bfd_uint64_t die_ref = attr_ptr->u.val;
1809 struct attribute attr;
1810 char *name = 0;
1812 /* DW_FORM_ref_addr can reference an entry in a different CU. It
1813 is an offset from the .debug_info section, not the current CU. */
1814 if (attr_ptr->form == DW_FORM_ref_addr)
1816 /* We only support DW_FORM_ref_addr within the same file, so
1817 any relocations should be resolved already. */
1818 if (!die_ref)
1819 abort ();
1821 info_ptr = unit->sec_info_ptr + die_ref;
1823 else
1824 info_ptr = unit->info_ptr_unit + die_ref;
1825 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1826 info_ptr += bytes_read;
1828 if (abbrev_number)
1830 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1831 if (! abbrev)
1833 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1834 abbrev_number);
1835 bfd_set_error (bfd_error_bad_value);
1837 else
1839 for (i = 0; i < abbrev->num_attrs; ++i)
1841 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1842 switch (attr.name)
1844 case DW_AT_name:
1845 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1846 if (name == NULL)
1847 name = attr.u.str;
1848 break;
1849 case DW_AT_specification:
1850 name = find_abstract_instance_name (unit, &attr);
1851 break;
1852 case DW_AT_MIPS_linkage_name:
1853 name = attr.u.str;
1854 break;
1855 default:
1856 break;
1861 return (name);
1864 static void
1865 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1867 bfd_byte *ranges_ptr;
1868 bfd_vma base_address = unit->base_address;
1870 if (! unit->stash->dwarf_ranges_buffer)
1872 if (! read_debug_ranges (unit))
1873 return;
1875 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1877 for (;;)
1879 bfd_vma low_pc;
1880 bfd_vma high_pc;
1882 low_pc = read_address (unit, ranges_ptr);
1883 ranges_ptr += unit->addr_size;
1884 high_pc = read_address (unit, ranges_ptr);
1885 ranges_ptr += unit->addr_size;
1887 if (low_pc == 0 && high_pc == 0)
1888 break;
1889 if (low_pc == -1UL && high_pc != -1UL)
1890 base_address = high_pc;
1891 else
1892 arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1896 /* DWARF2 Compilation unit functions. */
1898 /* Scan over each die in a comp. unit looking for functions to add
1899 to the function table and variables to the variable table. */
1901 static bfd_boolean
1902 scan_unit_for_symbols (struct comp_unit *unit)
1904 bfd *abfd = unit->abfd;
1905 bfd_byte *info_ptr = unit->first_child_die_ptr;
1906 int nesting_level = 1;
1907 struct funcinfo **nested_funcs;
1908 int nested_funcs_size;
1910 /* Maintain a stack of in-scope functions and inlined functions, which we
1911 can use to set the caller_func field. */
1912 nested_funcs_size = 32;
1913 nested_funcs = (struct funcinfo **)
1914 bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
1915 if (nested_funcs == NULL)
1916 return FALSE;
1917 nested_funcs[nesting_level] = 0;
1919 while (nesting_level)
1921 unsigned int abbrev_number, bytes_read, i;
1922 struct abbrev_info *abbrev;
1923 struct attribute attr;
1924 struct funcinfo *func;
1925 struct varinfo *var;
1926 bfd_vma low_pc = 0;
1927 bfd_vma high_pc = 0;
1929 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1930 info_ptr += bytes_read;
1932 if (! abbrev_number)
1934 nesting_level--;
1935 continue;
1938 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1939 if (! abbrev)
1941 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1942 abbrev_number);
1943 bfd_set_error (bfd_error_bad_value);
1944 free (nested_funcs);
1945 return FALSE;
1948 var = NULL;
1949 if (abbrev->tag == DW_TAG_subprogram
1950 || abbrev->tag == DW_TAG_entry_point
1951 || abbrev->tag == DW_TAG_inlined_subroutine)
1953 bfd_size_type amt = sizeof (struct funcinfo);
1954 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
1955 func->tag = abbrev->tag;
1956 func->prev_func = unit->function_table;
1957 unit->function_table = func;
1958 BFD_ASSERT (!unit->cached);
1960 if (func->tag == DW_TAG_inlined_subroutine)
1961 for (i = nesting_level - 1; i >= 1; i--)
1962 if (nested_funcs[i])
1964 func->caller_func = nested_funcs[i];
1965 break;
1967 nested_funcs[nesting_level] = func;
1969 else
1971 func = NULL;
1972 if (abbrev->tag == DW_TAG_variable)
1974 bfd_size_type amt = sizeof (struct varinfo);
1975 var = (struct varinfo *) bfd_zalloc (abfd, amt);
1976 var->tag = abbrev->tag;
1977 var->stack = 1;
1978 var->prev_var = unit->variable_table;
1979 unit->variable_table = var;
1980 BFD_ASSERT (!unit->cached);
1983 /* No inline function in scope at this nesting level. */
1984 nested_funcs[nesting_level] = 0;
1987 for (i = 0; i < abbrev->num_attrs; ++i)
1989 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1991 if (func)
1993 switch (attr.name)
1995 case DW_AT_call_file:
1996 func->caller_file = concat_filename (unit->line_table, attr.u.val);
1997 break;
1999 case DW_AT_call_line:
2000 func->caller_line = attr.u.val;
2001 break;
2003 case DW_AT_abstract_origin:
2004 func->name = find_abstract_instance_name (unit, &attr);
2005 break;
2007 case DW_AT_name:
2008 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
2009 if (func->name == NULL)
2010 func->name = attr.u.str;
2011 break;
2013 case DW_AT_MIPS_linkage_name:
2014 func->name = attr.u.str;
2015 break;
2017 case DW_AT_low_pc:
2018 low_pc = attr.u.val;
2019 break;
2021 case DW_AT_high_pc:
2022 high_pc = attr.u.val;
2023 break;
2025 case DW_AT_ranges:
2026 read_rangelist (unit, &func->arange, attr.u.val);
2027 break;
2029 case DW_AT_decl_file:
2030 func->file = concat_filename (unit->line_table,
2031 attr.u.val);
2032 break;
2034 case DW_AT_decl_line:
2035 func->line = attr.u.val;
2036 break;
2038 default:
2039 break;
2042 else if (var)
2044 switch (attr.name)
2046 case DW_AT_name:
2047 var->name = attr.u.str;
2048 break;
2050 case DW_AT_decl_file:
2051 var->file = concat_filename (unit->line_table,
2052 attr.u.val);
2053 break;
2055 case DW_AT_decl_line:
2056 var->line = attr.u.val;
2057 break;
2059 case DW_AT_external:
2060 if (attr.u.val != 0)
2061 var->stack = 0;
2062 break;
2064 case DW_AT_location:
2065 switch (attr.form)
2067 case DW_FORM_block:
2068 case DW_FORM_block1:
2069 case DW_FORM_block2:
2070 case DW_FORM_block4:
2071 if (*attr.u.blk->data == DW_OP_addr)
2073 var->stack = 0;
2075 /* Verify that DW_OP_addr is the only opcode in the
2076 location, in which case the block size will be 1
2077 plus the address size. */
2078 /* ??? For TLS variables, gcc can emit
2079 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
2080 which we don't handle here yet. */
2081 if (attr.u.blk->size == unit->addr_size + 1U)
2082 var->addr = bfd_get (unit->addr_size * 8,
2083 unit->abfd,
2084 attr.u.blk->data + 1);
2086 break;
2088 default:
2089 break;
2091 break;
2093 default:
2094 break;
2099 if (func && high_pc != 0)
2101 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
2104 if (abbrev->has_children)
2106 nesting_level++;
2108 if (nesting_level >= nested_funcs_size)
2110 struct funcinfo **tmp;
2112 nested_funcs_size *= 2;
2113 tmp = (struct funcinfo **)
2114 bfd_realloc (nested_funcs,
2115 (nested_funcs_size * sizeof (struct funcinfo *)));
2116 if (tmp == NULL)
2118 free (nested_funcs);
2119 return FALSE;
2121 nested_funcs = tmp;
2123 nested_funcs[nesting_level] = 0;
2127 free (nested_funcs);
2128 return TRUE;
2131 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
2132 includes the compilation unit header that proceeds the DIE's, but
2133 does not include the length field that precedes each compilation
2134 unit header. END_PTR points one past the end of this comp unit.
2135 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
2137 This routine does not read the whole compilation unit; only enough
2138 to get to the line number information for the compilation unit. */
2140 static struct comp_unit *
2141 parse_comp_unit (struct dwarf2_debug *stash,
2142 bfd_vma unit_length,
2143 bfd_byte *info_ptr_unit,
2144 unsigned int offset_size)
2146 struct comp_unit* unit;
2147 unsigned int version;
2148 bfd_uint64_t abbrev_offset = 0;
2149 unsigned int addr_size;
2150 struct abbrev_info** abbrevs;
2151 unsigned int abbrev_number, bytes_read, i;
2152 struct abbrev_info *abbrev;
2153 struct attribute attr;
2154 bfd_byte *info_ptr = stash->info_ptr;
2155 bfd_byte *end_ptr = info_ptr + unit_length;
2156 bfd_size_type amt;
2157 bfd_vma low_pc = 0;
2158 bfd_vma high_pc = 0;
2159 bfd *abfd = stash->bfd_ptr;
2161 version = read_2_bytes (abfd, info_ptr);
2162 info_ptr += 2;
2163 BFD_ASSERT (offset_size == 4 || offset_size == 8);
2164 if (offset_size == 4)
2165 abbrev_offset = read_4_bytes (abfd, info_ptr);
2166 else
2167 abbrev_offset = read_8_bytes (abfd, info_ptr);
2168 info_ptr += offset_size;
2169 addr_size = read_1_byte (abfd, info_ptr);
2170 info_ptr += 1;
2172 if (version != 2 && version != 3)
2174 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 and 3 information."), version);
2175 bfd_set_error (bfd_error_bad_value);
2176 return 0;
2179 if (addr_size > sizeof (bfd_vma))
2181 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
2182 addr_size,
2183 (unsigned int) sizeof (bfd_vma));
2184 bfd_set_error (bfd_error_bad_value);
2185 return 0;
2188 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
2190 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
2191 bfd_set_error (bfd_error_bad_value);
2192 return 0;
2195 /* Read the abbrevs for this compilation unit into a table. */
2196 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
2197 if (! abbrevs)
2198 return 0;
2200 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2201 info_ptr += bytes_read;
2202 if (! abbrev_number)
2204 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
2205 abbrev_number);
2206 bfd_set_error (bfd_error_bad_value);
2207 return 0;
2210 abbrev = lookup_abbrev (abbrev_number, abbrevs);
2211 if (! abbrev)
2213 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
2214 abbrev_number);
2215 bfd_set_error (bfd_error_bad_value);
2216 return 0;
2219 amt = sizeof (struct comp_unit);
2220 unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
2221 unit->abfd = abfd;
2222 unit->version = version;
2223 unit->addr_size = addr_size;
2224 unit->offset_size = offset_size;
2225 unit->abbrevs = abbrevs;
2226 unit->end_ptr = end_ptr;
2227 unit->stash = stash;
2228 unit->info_ptr_unit = info_ptr_unit;
2229 unit->sec_info_ptr = stash->sec_info_ptr;
2231 for (i = 0; i < abbrev->num_attrs; ++i)
2233 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2235 /* Store the data if it is of an attribute we want to keep in a
2236 partial symbol table. */
2237 switch (attr.name)
2239 case DW_AT_stmt_list:
2240 unit->stmtlist = 1;
2241 unit->line_offset = attr.u.val;
2242 break;
2244 case DW_AT_name:
2245 unit->name = attr.u.str;
2246 break;
2248 case DW_AT_low_pc:
2249 low_pc = attr.u.val;
2250 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2251 this is the base address to use when reading location
2252 lists or range lists. */
2253 unit->base_address = low_pc;
2254 break;
2256 case DW_AT_high_pc:
2257 high_pc = attr.u.val;
2258 break;
2260 case DW_AT_ranges:
2261 read_rangelist (unit, &unit->arange, attr.u.val);
2262 break;
2264 case DW_AT_comp_dir:
2266 char *comp_dir = attr.u.str;
2267 if (comp_dir)
2269 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2270 directory, get rid of it. */
2271 char *cp = strchr (comp_dir, ':');
2273 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2274 comp_dir = cp + 1;
2276 unit->comp_dir = comp_dir;
2277 break;
2280 default:
2281 break;
2284 if (high_pc != 0)
2286 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
2289 unit->first_child_die_ptr = info_ptr;
2290 return unit;
2293 /* Return TRUE if UNIT may contain the address given by ADDR. When
2294 there are functions written entirely with inline asm statements, the
2295 range info in the compilation unit header may not be correct. We
2296 need to consult the line info table to see if a compilation unit
2297 really contains the given address. */
2299 static bfd_boolean
2300 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2302 struct arange *arange;
2304 if (unit->error)
2305 return FALSE;
2307 arange = &unit->arange;
2310 if (addr >= arange->low && addr < arange->high)
2311 return TRUE;
2312 arange = arange->next;
2314 while (arange);
2316 return FALSE;
2319 /* If UNIT contains ADDR, set the output parameters to the values for
2320 the line containing ADDR. The output parameters, FILENAME_PTR,
2321 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2322 to be filled in.
2324 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2325 FALSE otherwise. */
2327 static bfd_boolean
2328 comp_unit_find_nearest_line (struct comp_unit *unit,
2329 bfd_vma addr,
2330 const char **filename_ptr,
2331 const char **functionname_ptr,
2332 unsigned int *linenumber_ptr,
2333 struct dwarf2_debug *stash)
2335 bfd_boolean line_p;
2336 bfd_boolean func_p;
2337 struct funcinfo *function;
2339 if (unit->error)
2340 return FALSE;
2342 if (! unit->line_table)
2344 if (! unit->stmtlist)
2346 unit->error = 1;
2347 return FALSE;
2350 unit->line_table = decode_line_info (unit, stash);
2352 if (! unit->line_table)
2354 unit->error = 1;
2355 return FALSE;
2358 if (unit->first_child_die_ptr < unit->end_ptr
2359 && ! scan_unit_for_symbols (unit))
2361 unit->error = 1;
2362 return FALSE;
2366 function = NULL;
2367 func_p = lookup_address_in_function_table (unit, addr,
2368 &function, functionname_ptr);
2369 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2370 stash->inliner_chain = function;
2371 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2372 filename_ptr,
2373 linenumber_ptr);
2374 return line_p || func_p;
2377 /* Check to see if line info is already decoded in a comp_unit.
2378 If not, decode it. Returns TRUE if no errors were encountered;
2379 FALSE otherwise. */
2381 static bfd_boolean
2382 comp_unit_maybe_decode_line_info (struct comp_unit *unit,
2383 struct dwarf2_debug *stash)
2385 if (unit->error)
2386 return FALSE;
2388 if (! unit->line_table)
2390 if (! unit->stmtlist)
2392 unit->error = 1;
2393 return FALSE;
2396 unit->line_table = decode_line_info (unit, stash);
2398 if (! unit->line_table)
2400 unit->error = 1;
2401 return FALSE;
2404 if (unit->first_child_die_ptr < unit->end_ptr
2405 && ! scan_unit_for_symbols (unit))
2407 unit->error = 1;
2408 return FALSE;
2412 return TRUE;
2415 /* If UNIT contains SYM at ADDR, set the output parameters to the
2416 values for the line containing SYM. The output parameters,
2417 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2418 filled in.
2420 Return TRUE if UNIT contains SYM, and no errors were encountered;
2421 FALSE otherwise. */
2423 static bfd_boolean
2424 comp_unit_find_line (struct comp_unit *unit,
2425 asymbol *sym,
2426 bfd_vma addr,
2427 const char **filename_ptr,
2428 unsigned int *linenumber_ptr,
2429 struct dwarf2_debug *stash)
2431 if (!comp_unit_maybe_decode_line_info (unit, stash))
2432 return FALSE;
2434 if (sym->flags & BSF_FUNCTION)
2435 return lookup_symbol_in_function_table (unit, sym, addr,
2436 filename_ptr,
2437 linenumber_ptr);
2439 return lookup_symbol_in_variable_table (unit, sym, addr,
2440 filename_ptr,
2441 linenumber_ptr);
2444 static struct funcinfo *
2445 reverse_funcinfo_list (struct funcinfo *head)
2447 struct funcinfo *rhead;
2448 struct funcinfo *temp;
2450 for (rhead = NULL; head; head = temp)
2452 temp = head->prev_func;
2453 head->prev_func = rhead;
2454 rhead = head;
2456 return rhead;
2459 static struct varinfo *
2460 reverse_varinfo_list (struct varinfo *head)
2462 struct varinfo *rhead;
2463 struct varinfo *temp;
2465 for (rhead = NULL; head; head = temp)
2467 temp = head->prev_var;
2468 head->prev_var = rhead;
2469 rhead = head;
2471 return rhead;
2474 /* Extract all interesting funcinfos and varinfos of a compilation
2475 unit into hash tables for faster lookup. Returns TRUE if no
2476 errors were enountered; FALSE otherwise. */
2478 static bfd_boolean
2479 comp_unit_hash_info (struct dwarf2_debug *stash,
2480 struct comp_unit *unit,
2481 struct info_hash_table *funcinfo_hash_table,
2482 struct info_hash_table *varinfo_hash_table)
2484 struct funcinfo* each_func;
2485 struct varinfo* each_var;
2486 bfd_boolean okay = TRUE;
2488 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
2490 if (!comp_unit_maybe_decode_line_info (unit, stash))
2491 return FALSE;
2493 BFD_ASSERT (!unit->cached);
2495 /* To preserve the original search order, we went to visit the function
2496 infos in the reversed order of the list. However, making the list
2497 bi-directional use quite a bit of extra memory. So we reverse
2498 the list first, traverse the list in the now reversed order and
2499 finally reverse the list again to get back the original order. */
2500 unit->function_table = reverse_funcinfo_list (unit->function_table);
2501 for (each_func = unit->function_table;
2502 each_func && okay;
2503 each_func = each_func->prev_func)
2505 /* Skip nameless functions. */
2506 if (each_func->name)
2507 /* There is no need to copy name string into hash table as
2508 name string is either in the dwarf string buffer or
2509 info in the stash. */
2510 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
2511 (void*) each_func, FALSE);
2513 unit->function_table = reverse_funcinfo_list (unit->function_table);
2514 if (!okay)
2515 return FALSE;
2517 /* We do the same for variable infos. */
2518 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2519 for (each_var = unit->variable_table;
2520 each_var && okay;
2521 each_var = each_var->prev_var)
2523 /* Skip stack vars and vars with no files or names. */
2524 if (each_var->stack == 0
2525 && each_var->file != NULL
2526 && each_var->name != NULL)
2527 /* There is no need to copy name string into hash table as
2528 name string is either in the dwarf string buffer or
2529 info in the stash. */
2530 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
2531 (void*) each_var, FALSE);
2534 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2535 unit->cached = TRUE;
2536 return okay;
2539 /* Locate a section in a BFD containing debugging info. The search starts
2540 from the section after AFTER_SEC, or from the first section in the BFD if
2541 AFTER_SEC is NULL. The search works by examining the names of the
2542 sections. There are two permissiable names. The first is .debug_info.
2543 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2544 This is a variation on the .debug_info section which has a checksum
2545 describing the contents appended onto the name. This allows the linker to
2546 identify and discard duplicate debugging sections for different
2547 compilation units. */
2548 #define DWARF2_DEBUG_INFO ".debug_info"
2549 #define DWARF2_COMPRESSED_DEBUG_INFO ".zdebug_info"
2550 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2552 static asection *
2553 find_debug_info (bfd *abfd, asection *after_sec)
2555 asection * msec;
2557 msec = after_sec != NULL ? after_sec->next : abfd->sections;
2559 while (msec)
2561 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2562 return msec;
2564 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2565 return msec;
2567 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
2568 return msec;
2570 msec = msec->next;
2573 return NULL;
2576 /* Unset vmas for adjusted sections in STASH. */
2578 static void
2579 unset_sections (struct dwarf2_debug *stash)
2581 unsigned int i;
2582 struct adjusted_section *p;
2584 i = stash->adjusted_section_count;
2585 p = stash->adjusted_sections;
2586 for (; i > 0; i--, p++)
2587 p->section->vma = 0;
2590 /* Set unique VMAs for loadable and DWARF sections in ABFD and save
2591 VMAs in STASH for unset_sections. */
2593 static bfd_boolean
2594 place_sections (bfd *abfd, struct dwarf2_debug *stash)
2596 struct adjusted_section *p;
2597 unsigned int i;
2599 if (stash->adjusted_section_count != 0)
2601 i = stash->adjusted_section_count;
2602 p = stash->adjusted_sections;
2603 for (; i > 0; i--, p++)
2604 p->section->vma = p->adj_vma;
2606 else
2608 asection *sect;
2609 bfd_vma last_vma = 0, last_dwarf = 0;
2610 bfd_size_type amt;
2612 i = 0;
2613 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2615 bfd_size_type sz;
2616 int is_debug_info;
2618 if (sect->vma != 0)
2619 continue;
2621 /* We need to adjust the VMAs of any .debug_info sections.
2622 Skip compressed ones, since no relocations could target
2623 them - they should not appear in object files anyway. */
2624 if (strcmp (sect->name, DWARF2_DEBUG_INFO) == 0)
2625 is_debug_info = 1;
2626 else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
2627 is_debug_info = 1;
2628 else
2629 is_debug_info = 0;
2631 if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
2632 continue;
2634 sz = sect->rawsize ? sect->rawsize : sect->size;
2635 if (sz == 0)
2636 continue;
2638 i++;
2641 amt = i * sizeof (struct adjusted_section);
2642 p = (struct adjusted_section *) bfd_zalloc (abfd, amt);
2643 if (! p)
2644 return FALSE;
2646 stash->adjusted_sections = p;
2647 stash->adjusted_section_count = i;
2649 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2651 bfd_size_type sz;
2652 int is_debug_info;
2654 if (sect->vma != 0)
2655 continue;
2657 /* We need to adjust the VMAs of any .debug_info sections.
2658 Skip compressed ones, since no relocations could target
2659 them - they should not appear in object files anyway. */
2660 if (strcmp (sect->name, DWARF2_DEBUG_INFO) == 0)
2661 is_debug_info = 1;
2662 else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
2663 is_debug_info = 1;
2664 else
2665 is_debug_info = 0;
2667 if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
2668 continue;
2670 sz = sect->rawsize ? sect->rawsize : sect->size;
2671 if (sz == 0)
2672 continue;
2674 p->section = sect;
2675 if (is_debug_info)
2677 BFD_ASSERT (sect->alignment_power == 0);
2678 sect->vma = last_dwarf;
2679 last_dwarf += sz;
2681 else if (last_vma != 0)
2683 /* Align the new address to the current section
2684 alignment. */
2685 last_vma = ((last_vma
2686 + ~((bfd_vma) -1 << sect->alignment_power))
2687 & ((bfd_vma) -1 << sect->alignment_power));
2688 sect->vma = last_vma;
2689 last_vma += sect->vma + sz;
2691 else
2692 last_vma += sect->vma + sz;
2694 p->adj_vma = sect->vma;
2696 p++;
2700 return TRUE;
2703 /* Look up a funcinfo by name using the given info hash table. If found,
2704 also update the locations pointed to by filename_ptr and linenumber_ptr.
2706 This function returns TRUE if a funcinfo that matches the given symbol
2707 and address is found with any error; otherwise it returns FALSE. */
2709 static bfd_boolean
2710 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
2711 asymbol *sym,
2712 bfd_vma addr,
2713 const char **filename_ptr,
2714 unsigned int *linenumber_ptr)
2716 struct funcinfo* each_func;
2717 struct funcinfo* best_fit = NULL;
2718 struct info_list_node *node;
2719 struct arange *arange;
2720 const char *name = bfd_asymbol_name (sym);
2721 asection *sec = bfd_get_section (sym);
2723 for (node = lookup_info_hash_table (hash_table, name);
2724 node;
2725 node = node->next)
2727 each_func = (struct funcinfo *) node->info;
2728 for (arange = &each_func->arange;
2729 arange;
2730 arange = arange->next)
2732 if ((!each_func->sec || each_func->sec == sec)
2733 && addr >= arange->low
2734 && addr < arange->high
2735 && (!best_fit
2736 || ((arange->high - arange->low)
2737 < (best_fit->arange.high - best_fit->arange.low))))
2738 best_fit = each_func;
2742 if (best_fit)
2744 best_fit->sec = sec;
2745 *filename_ptr = best_fit->file;
2746 *linenumber_ptr = best_fit->line;
2747 return TRUE;
2750 return FALSE;
2753 /* Look up a varinfo by name using the given info hash table. If found,
2754 also update the locations pointed to by filename_ptr and linenumber_ptr.
2756 This function returns TRUE if a varinfo that matches the given symbol
2757 and address is found with any error; otherwise it returns FALSE. */
2759 static bfd_boolean
2760 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
2761 asymbol *sym,
2762 bfd_vma addr,
2763 const char **filename_ptr,
2764 unsigned int *linenumber_ptr)
2766 const char *name = bfd_asymbol_name (sym);
2767 asection *sec = bfd_get_section (sym);
2768 struct varinfo* each;
2769 struct info_list_node *node;
2771 for (node = lookup_info_hash_table (hash_table, name);
2772 node;
2773 node = node->next)
2775 each = (struct varinfo *) node->info;
2776 if (each->addr == addr
2777 && (!each->sec || each->sec == sec))
2779 each->sec = sec;
2780 *filename_ptr = each->file;
2781 *linenumber_ptr = each->line;
2782 return TRUE;
2786 return FALSE;
2789 /* Update the funcinfo and varinfo info hash tables if they are
2790 not up to date. Returns TRUE if there is no error; otherwise
2791 returns FALSE and disable the info hash tables. */
2793 static bfd_boolean
2794 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
2796 struct comp_unit *each;
2798 /* Exit if hash tables are up-to-date. */
2799 if (stash->all_comp_units == stash->hash_units_head)
2800 return TRUE;
2802 if (stash->hash_units_head)
2803 each = stash->hash_units_head->prev_unit;
2804 else
2805 each = stash->last_comp_unit;
2807 while (each)
2809 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
2810 stash->varinfo_hash_table))
2812 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2813 return FALSE;
2815 each = each->prev_unit;
2818 stash->hash_units_head = stash->all_comp_units;
2819 return TRUE;
2822 /* Check consistency of info hash tables. This is for debugging only. */
2824 static void ATTRIBUTE_UNUSED
2825 stash_verify_info_hash_table (struct dwarf2_debug *stash)
2827 struct comp_unit *each_unit;
2828 struct funcinfo *each_func;
2829 struct varinfo *each_var;
2830 struct info_list_node *node;
2831 bfd_boolean found;
2833 for (each_unit = stash->all_comp_units;
2834 each_unit;
2835 each_unit = each_unit->next_unit)
2837 for (each_func = each_unit->function_table;
2838 each_func;
2839 each_func = each_func->prev_func)
2841 if (!each_func->name)
2842 continue;
2843 node = lookup_info_hash_table (stash->funcinfo_hash_table,
2844 each_func->name);
2845 BFD_ASSERT (node);
2846 found = FALSE;
2847 while (node && !found)
2849 found = node->info == each_func;
2850 node = node->next;
2852 BFD_ASSERT (found);
2855 for (each_var = each_unit->variable_table;
2856 each_var;
2857 each_var = each_var->prev_var)
2859 if (!each_var->name || !each_var->file || each_var->stack)
2860 continue;
2861 node = lookup_info_hash_table (stash->varinfo_hash_table,
2862 each_var->name);
2863 BFD_ASSERT (node);
2864 found = FALSE;
2865 while (node && !found)
2867 found = node->info == each_var;
2868 node = node->next;
2870 BFD_ASSERT (found);
2875 /* Check to see if we want to enable the info hash tables, which consume
2876 quite a bit of memory. Currently we only check the number times
2877 bfd_dwarf2_find_line is called. In the future, we may also want to
2878 take the number of symbols into account. */
2880 static void
2881 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
2883 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
2885 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
2886 return;
2888 /* FIXME: Maybe we should check the reduce_memory_overheads
2889 and optimize fields in the bfd_link_info structure ? */
2891 /* Create hash tables. */
2892 stash->funcinfo_hash_table = create_info_hash_table (abfd);
2893 stash->varinfo_hash_table = create_info_hash_table (abfd);
2894 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
2896 /* Turn off info hashes if any allocation above fails. */
2897 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2898 return;
2900 /* We need a forced update so that the info hash tables will
2901 be created even though there is no compilation unit. That
2902 happens if STASH_INFO_HASH_TRIGGER is 0. */
2903 stash_maybe_update_info_hash_tables (stash);
2904 stash->info_hash_status = STASH_INFO_HASH_ON;
2907 /* Find the file and line associated with a symbol and address using the
2908 info hash tables of a stash. If there is a match, the function returns
2909 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
2910 otherwise it returns FALSE. */
2912 static bfd_boolean
2913 stash_find_line_fast (struct dwarf2_debug *stash,
2914 asymbol *sym,
2915 bfd_vma addr,
2916 const char **filename_ptr,
2917 unsigned int *linenumber_ptr)
2919 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
2921 if (sym->flags & BSF_FUNCTION)
2922 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
2923 filename_ptr, linenumber_ptr);
2924 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
2925 filename_ptr, linenumber_ptr);
2928 /* Find the source code location of SYMBOL. If SYMBOL is NULL
2929 then find the nearest source code location corresponding to
2930 the address SECTION + OFFSET.
2931 Returns TRUE if the line is found without error and fills in
2932 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
2933 NULL the FUNCTIONNAME_PTR is also filled in.
2934 SYMBOLS contains the symbol table for ABFD.
2935 ADDR_SIZE is the number of bytes in the initial .debug_info length
2936 field and in the abbreviation offset, or zero to indicate that the
2937 default value should be used. */
2939 static bfd_boolean
2940 find_line (bfd *abfd,
2941 asection *section,
2942 bfd_vma offset,
2943 asymbol *symbol,
2944 asymbol **symbols,
2945 const char **filename_ptr,
2946 const char **functionname_ptr,
2947 unsigned int *linenumber_ptr,
2948 unsigned int addr_size,
2949 void **pinfo)
2951 /* Read each compilation unit from the section .debug_info, and check
2952 to see if it contains the address we are searching for. If yes,
2953 lookup the address, and return the line number info. If no, go
2954 on to the next compilation unit.
2956 We keep a list of all the previously read compilation units, and
2957 a pointer to the next un-read compilation unit. Check the
2958 previously read units before reading more. */
2959 struct dwarf2_debug *stash;
2960 /* What address are we looking for? */
2961 bfd_vma addr;
2962 struct comp_unit* each;
2963 bfd_vma found = FALSE;
2964 bfd_boolean do_line;
2966 stash = (struct dwarf2_debug *) *pinfo;
2968 if (! stash)
2970 bfd_size_type amt = sizeof (struct dwarf2_debug);
2972 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
2973 if (! stash)
2974 return FALSE;
2977 /* In a relocatable file, 2 functions may have the same address.
2978 We change the section vma so that they won't overlap. */
2979 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2981 if (! place_sections (abfd, stash))
2982 return FALSE;
2985 do_line = (section == NULL
2986 && offset == 0
2987 && functionname_ptr == NULL
2988 && symbol != NULL);
2989 if (do_line)
2991 addr = symbol->value;
2992 section = bfd_get_section (symbol);
2994 else if (section != NULL
2995 && functionname_ptr != NULL
2996 && symbol == NULL)
2997 addr = offset;
2998 else
2999 abort ();
3001 if (section->output_section)
3002 addr += section->output_section->vma + section->output_offset;
3003 else
3004 addr += section->vma;
3005 *filename_ptr = NULL;
3006 if (! do_line)
3007 *functionname_ptr = NULL;
3008 *linenumber_ptr = 0;
3010 if (! *pinfo)
3012 bfd *debug_bfd;
3013 bfd_size_type total_size;
3014 asection *msec;
3016 *pinfo = stash;
3018 msec = find_debug_info (abfd, NULL);
3019 if (msec == NULL)
3021 char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
3023 if (debug_filename == NULL)
3024 /* No dwarf2 info, and no gnu_debuglink to follow.
3025 Note that at this point the stash has been allocated, but
3026 contains zeros. This lets future calls to this function
3027 fail more quickly. */
3028 goto done;
3030 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
3031 || ! bfd_check_format (debug_bfd, bfd_object)
3032 || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
3034 if (debug_bfd)
3035 bfd_close (debug_bfd);
3036 /* FIXME: Should we report our failure to follow the debuglink ? */
3037 free (debug_filename);
3038 goto done;
3041 else
3042 debug_bfd = abfd;
3044 /* There can be more than one DWARF2 info section in a BFD these
3045 days. First handle the easy case when there's only one. If
3046 there's more than one, try case two: none of the sections is
3047 compressed. In that case, read them all in and produce one
3048 large stash. We do this in two passes - in the first pass we
3049 just accumulate the section sizes, and in the second pass we
3050 read in the section's contents. (The allows us to avoid
3051 reallocing the data as we add sections to the stash.) If
3052 some or all sections are compressed, then do things the slow
3053 way, with a bunch of reallocs. */
3055 if (! find_debug_info (debug_bfd, msec))
3057 /* Case 1: only one info section. */
3058 total_size = msec->size;
3059 if (! read_section (debug_bfd, ".debug_info", ".zdebug_info",
3060 symbols, 0,
3061 &stash->info_ptr_memory, &total_size))
3062 goto done;
3064 else
3066 int all_uncompressed = 1;
3067 for (total_size = 0; msec; msec = find_debug_info (debug_bfd, msec))
3069 total_size += msec->size;
3070 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
3071 all_uncompressed = 0;
3073 if (all_uncompressed)
3075 /* Case 2: multiple sections, but none is compressed. */
3076 stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size);
3077 if (stash->info_ptr_memory == NULL)
3078 goto done;
3080 total_size = 0;
3081 for (msec = find_debug_info (debug_bfd, NULL);
3082 msec;
3083 msec = find_debug_info (debug_bfd, msec))
3085 bfd_size_type size;
3087 size = msec->size;
3088 if (size == 0)
3089 continue;
3091 if (!(bfd_simple_get_relocated_section_contents
3092 (debug_bfd, msec, stash->info_ptr_memory + total_size,
3093 symbols)))
3094 goto done;
3096 total_size += size;
3099 else
3101 /* Case 3: multiple sections, some or all compressed. */
3102 stash->info_ptr_memory = NULL;
3103 total_size = 0;
3104 for (msec = find_debug_info (debug_bfd, NULL);
3105 msec;
3106 msec = find_debug_info (debug_bfd, msec))
3108 bfd_size_type size = msec->size;
3109 bfd_byte* buffer;
3111 if (size == 0)
3112 continue;
3114 buffer = (bfd_simple_get_relocated_section_contents
3115 (debug_bfd, msec, NULL, symbols));
3116 if (! buffer)
3117 goto done;
3119 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
3121 if (! bfd_uncompress_section_contents (&buffer, &size))
3123 free (buffer);
3124 goto done;
3127 stash->info_ptr_memory = (bfd_byte *)
3128 bfd_realloc (stash->info_ptr_memory, total_size + size);
3129 memcpy (stash->info_ptr_memory + total_size, buffer, size);
3130 free (buffer);
3131 total_size += size;
3136 stash->info_ptr = stash->info_ptr_memory;
3137 stash->info_ptr_end = stash->info_ptr + total_size;
3138 stash->sec = find_debug_info (debug_bfd, NULL);
3139 stash->sec_info_ptr = stash->info_ptr;
3140 stash->syms = symbols;
3141 stash->bfd_ptr = debug_bfd;
3144 /* A null info_ptr indicates that there is no dwarf2 info
3145 (or that an error occured while setting up the stash). */
3146 if (! stash->info_ptr)
3147 goto done;
3149 stash->inliner_chain = NULL;
3151 /* Check the previously read comp. units first. */
3152 if (do_line)
3154 /* The info hash tables use quite a bit of memory. We may not want to
3155 always use them. We use some heuristics to decide if and when to
3156 turn it on. */
3157 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
3158 stash_maybe_enable_info_hash_tables (abfd, stash);
3160 /* Keep info hash table up to date if they are available. Note that we
3161 may disable the hash tables if there is any error duing update. */
3162 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3163 stash_maybe_update_info_hash_tables (stash);
3165 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3167 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
3168 linenumber_ptr);
3169 if (found)
3170 goto done;
3172 else
3174 /* Check the previously read comp. units first. */
3175 for (each = stash->all_comp_units; each; each = each->next_unit)
3176 if ((symbol->flags & BSF_FUNCTION) == 0
3177 || comp_unit_contains_address (each, addr))
3179 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
3180 linenumber_ptr, stash);
3181 if (found)
3182 goto done;
3186 else
3188 for (each = stash->all_comp_units; each; each = each->next_unit)
3190 found = (comp_unit_contains_address (each, addr)
3191 && comp_unit_find_nearest_line (each, addr,
3192 filename_ptr,
3193 functionname_ptr,
3194 linenumber_ptr,
3195 stash));
3196 if (found)
3197 goto done;
3201 /* The DWARF2 spec says that the initial length field, and the
3202 offset of the abbreviation table, should both be 4-byte values.
3203 However, some compilers do things differently. */
3204 if (addr_size == 0)
3205 addr_size = 4;
3206 BFD_ASSERT (addr_size == 4 || addr_size == 8);
3208 /* Read each remaining comp. units checking each as they are read. */
3209 while (stash->info_ptr < stash->info_ptr_end)
3211 bfd_vma length;
3212 unsigned int offset_size = addr_size;
3213 bfd_byte *info_ptr_unit = stash->info_ptr;
3215 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr);
3216 /* A 0xffffff length is the DWARF3 way of indicating
3217 we use 64-bit offsets, instead of 32-bit offsets. */
3218 if (length == 0xffffffff)
3220 offset_size = 8;
3221 length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4);
3222 stash->info_ptr += 12;
3224 /* A zero length is the IRIX way of indicating 64-bit offsets,
3225 mostly because the 64-bit length will generally fit in 32
3226 bits, and the endianness helps. */
3227 else if (length == 0)
3229 offset_size = 8;
3230 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4);
3231 stash->info_ptr += 8;
3233 /* In the absence of the hints above, we assume 32-bit DWARF2
3234 offsets even for targets with 64-bit addresses, because:
3235 a) most of the time these targets will not have generated
3236 more than 2Gb of debug info and so will not need 64-bit
3237 offsets,
3239 b) if they do use 64-bit offsets but they are not using
3240 the size hints that are tested for above then they are
3241 not conforming to the DWARF3 standard anyway. */
3242 else if (addr_size == 8)
3244 offset_size = 4;
3245 stash->info_ptr += 4;
3247 else
3248 stash->info_ptr += 4;
3250 if (length > 0)
3252 each = parse_comp_unit (stash, length, info_ptr_unit,
3253 offset_size);
3254 if (!each)
3255 /* The dwarf information is damaged, don't trust it any
3256 more. */
3257 break;
3258 stash->info_ptr += length;
3260 if (stash->all_comp_units)
3261 stash->all_comp_units->prev_unit = each;
3262 else
3263 stash->last_comp_unit = each;
3265 each->next_unit = stash->all_comp_units;
3266 stash->all_comp_units = each;
3268 /* DW_AT_low_pc and DW_AT_high_pc are optional for
3269 compilation units. If we don't have them (i.e.,
3270 unit->high == 0), we need to consult the line info table
3271 to see if a compilation unit contains the given
3272 address. */
3273 if (do_line)
3274 found = (((symbol->flags & BSF_FUNCTION) == 0
3275 || each->arange.high == 0
3276 || comp_unit_contains_address (each, addr))
3277 && comp_unit_find_line (each, symbol, addr,
3278 filename_ptr,
3279 linenumber_ptr,
3280 stash));
3281 else
3282 found = ((each->arange.high == 0
3283 || comp_unit_contains_address (each, addr))
3284 && comp_unit_find_nearest_line (each, addr,
3285 filename_ptr,
3286 functionname_ptr,
3287 linenumber_ptr,
3288 stash));
3290 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
3291 == stash->sec->size)
3293 stash->sec = find_debug_info (stash->bfd_ptr, stash->sec);
3294 stash->sec_info_ptr = stash->info_ptr;
3297 if (found)
3298 goto done;
3302 done:
3303 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3304 unset_sections (stash);
3306 return found;
3309 /* The DWARF2 version of find_nearest_line.
3310 Return TRUE if the line is found without error. */
3312 bfd_boolean
3313 _bfd_dwarf2_find_nearest_line (bfd *abfd,
3314 asection *section,
3315 asymbol **symbols,
3316 bfd_vma offset,
3317 const char **filename_ptr,
3318 const char **functionname_ptr,
3319 unsigned int *linenumber_ptr,
3320 unsigned int addr_size,
3321 void **pinfo)
3323 return find_line (abfd, section, offset, NULL, symbols, filename_ptr,
3324 functionname_ptr, linenumber_ptr, addr_size,
3325 pinfo);
3328 /* The DWARF2 version of find_line.
3329 Return TRUE if the line is found without error. */
3331 bfd_boolean
3332 _bfd_dwarf2_find_line (bfd *abfd,
3333 asymbol **symbols,
3334 asymbol *symbol,
3335 const char **filename_ptr,
3336 unsigned int *linenumber_ptr,
3337 unsigned int addr_size,
3338 void **pinfo)
3340 return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr,
3341 NULL, linenumber_ptr, addr_size,
3342 pinfo);
3345 bfd_boolean
3346 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
3347 const char **filename_ptr,
3348 const char **functionname_ptr,
3349 unsigned int *linenumber_ptr,
3350 void **pinfo)
3352 struct dwarf2_debug *stash;
3354 stash = (struct dwarf2_debug *) *pinfo;
3355 if (stash)
3357 struct funcinfo *func = stash->inliner_chain;
3359 if (func && func->caller_func)
3361 *filename_ptr = func->caller_file;
3362 *functionname_ptr = func->caller_func->name;
3363 *linenumber_ptr = func->caller_line;
3364 stash->inliner_chain = func->caller_func;
3365 return TRUE;
3369 return FALSE;
3372 void
3373 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
3375 struct comp_unit *each;
3376 struct dwarf2_debug *stash;
3378 if (abfd == NULL || elf_tdata (abfd) == NULL)
3379 return;
3381 stash = (struct dwarf2_debug *) elf_tdata (abfd)->dwarf2_find_line_info;
3383 if (stash == NULL)
3384 return;
3386 for (each = stash->all_comp_units; each; each = each->next_unit)
3388 struct abbrev_info **abbrevs = each->abbrevs;
3389 struct funcinfo *function_table = each->function_table;
3390 struct varinfo *variable_table = each->variable_table;
3391 size_t i;
3393 for (i = 0; i < ABBREV_HASH_SIZE; i++)
3395 struct abbrev_info *abbrev = abbrevs[i];
3397 while (abbrev)
3399 free (abbrev->attrs);
3400 abbrev = abbrev->next;
3404 if (each->line_table)
3406 free (each->line_table->dirs);
3407 free (each->line_table->files);
3410 while (function_table)
3412 if (function_table->file)
3414 free (function_table->file);
3415 function_table->file = NULL;
3418 if (function_table->caller_file)
3420 free (function_table->caller_file);
3421 function_table->caller_file = NULL;
3423 function_table = function_table->prev_func;
3426 while (variable_table)
3428 if (variable_table->file)
3430 free (variable_table->file);
3431 variable_table->file = NULL;
3434 variable_table = variable_table->prev_var;
3438 if (stash->dwarf_abbrev_buffer)
3439 free (stash->dwarf_abbrev_buffer);
3440 if (stash->dwarf_line_buffer)
3441 free (stash->dwarf_line_buffer);
3442 if (stash->dwarf_str_buffer)
3443 free (stash->dwarf_str_buffer);
3444 if (stash->dwarf_ranges_buffer)
3445 free (stash->dwarf_ranges_buffer);
3446 if (stash->info_ptr_memory)
3447 free (stash->info_ptr_memory);