More -fPIC options.
[binutils.git] / bfd / dwarf2.c
blob52cfe9e62e43b8bcf54560502c7357fabc625750
1 /* DWARF 2 support.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
6 (gavin@cygnus.com).
8 From the dwarf2read.c header:
9 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10 Inc. with support from Florida State University (under contract
11 with the Ada Joint Program Office), and Silicon Graphics, Inc.
12 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14 support in dwarfread.c
16 This file is part of BFD.
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 3 of the License, or (at
21 your option) any later version.
23 This program is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
31 MA 02110-1301, USA. */
33 #include "sysdep.h"
34 #include "bfd.h"
35 #include "libiberty.h"
36 #include "libbfd.h"
37 #include "elf-bfd.h"
38 #include "elf/dwarf2.h"
39 #include "arange-set.h"
41 /* The data in the .debug_line statement prologue looks like this. */
43 struct line_head
45 bfd_vma total_length;
46 unsigned short version;
47 bfd_vma prologue_length;
48 unsigned char minimum_instruction_length;
49 unsigned char default_is_stmt;
50 int line_base;
51 unsigned char line_range;
52 unsigned char opcode_base;
53 unsigned char *standard_opcode_lengths;
56 /* Attributes have a name and a value. */
58 struct attribute
60 enum dwarf_attribute name;
61 enum dwarf_form form;
62 union
64 char *str;
65 struct dwarf_block *blk;
66 bfd_uint64_t val;
67 bfd_int64_t sval;
72 /* Blocks are a bunch of untyped bytes. */
73 struct dwarf_block
75 unsigned int size;
76 bfd_byte *data;
79 struct loadable_section
81 asection *section;
82 bfd_vma adj_vma;
85 struct dwarf2_debug
87 /* A list of all previously read comp_units. */
88 struct comp_unit *all_comp_units;
90 /* Last comp unit in list above. */
91 struct comp_unit *last_comp_unit;
93 /* Number of comp units. */
94 int comp_unit_count;
96 /* The next unread compilation unit within the .debug_info section.
97 Zero indicates that the .debug_info section has not been loaded
98 into a buffer yet. */
99 bfd_byte *info_ptr;
101 /* Pointer to the end of the .debug_info section memory buffer. */
102 bfd_byte *info_ptr_end;
104 /* Pointer to the bfd, section and address of the beginning of the
105 section. The bfd might be different than expected because of
106 gnu_debuglink sections. */
107 bfd * bfd;
108 asection *sec;
109 bfd_byte *sec_info_ptr;
111 /* Pointer to the symbol table. */
112 asymbol **syms;
114 /* Pointer to the .debug_abbrev section loaded into memory. */
115 bfd_byte *dwarf_abbrev_buffer;
117 /* Length of the loaded .debug_abbrev section. */
118 unsigned long dwarf_abbrev_size;
120 /* Buffer for decode_line_info. */
121 bfd_byte *dwarf_line_buffer;
123 /* Length of the loaded .debug_line section. */
124 unsigned long dwarf_line_size;
126 /* Pointer to the .debug_str section loaded into memory. */
127 bfd_byte *dwarf_str_buffer;
129 /* Length of the loaded .debug_str section. */
130 unsigned long dwarf_str_size;
132 /* Pointer to the .debug_ranges section loaded into memory. */
133 bfd_byte *dwarf_ranges_buffer;
135 /* Length of the loaded .debug_ranges section. */
136 unsigned long dwarf_ranges_size;
138 /* If the most recent call to bfd_find_nearest_line was given an
139 address in an inlined function, preserve a pointer into the
140 calling chain for subsequent calls to bfd_find_inliner_info to
141 use. */
142 struct funcinfo *inliner_chain;
144 /* Number of loadable sections. */
145 unsigned int loadable_section_count;
147 /* Array of loadable sections. */
148 struct loadable_section *loadable_sections;
150 /* Number of times find_line is called. This is used in
151 the heuristic for enabling the info hash tables. */
152 int info_hash_count;
154 #define STASH_INFO_HASH_TRIGGER 100
156 /* Hash table mapping symbol names to function infos. */
157 struct info_hash_table *funcinfo_hash_table;
159 /* Hash table mapping symbol names to variable infos. */
160 struct info_hash_table *varinfo_hash_table;
162 /* Head of comp_unit list in the last hash table update. */
163 struct comp_unit *hash_units_head;
165 /* Status of info hash. */
166 int info_hash_status;
167 #define STASH_INFO_HASH_OFF 0
168 #define STASH_INFO_HASH_ON 1
169 #define STASH_INFO_HASH_DISABLED 2
171 /* Arange-set for fast lookup. The aranges in this set have pointers
172 to compilation units containing them. In the unlikely case that there
173 are multiple compilation units associated with an arange, the arange-set
174 is a NULL pointer and we need to fall back to sequential search. */
175 arange_set comp_unit_arange_set;
177 /* Status of global arange set. */
178 int arange_set_status;
179 #define STASH_ARANGE_SET_OFF 0
180 #define STASH_ARANGE_SET_ON 1
181 #define STASH_ARANGE_SET_DISABLED 2
183 /* Build a whole binary arange-set for compilation unit look-up
184 if there are at least this many compilation units. */
185 #define STASH_ARANGE_SET_TRIGGER 500
188 /* Simple singly linked list for aranges. We now use a more scalable
189 arange-set for aranges in compilation units. For functions, we still
190 use this since it is more efficient for simple cases. */
192 struct arange
194 struct arange *next;
195 /* The lowest and highest addresses contained a compilation
196 unit as specified in the compilation unit's header. */
197 bfd_vma low;
198 bfd_vma high;
201 /* A minimal decoding of DWARF2 compilation units. We only decode
202 what's needed to get to the line number information. */
204 struct comp_unit
206 /* Chain the previously read compilation units. */
207 struct comp_unit *next_unit;
209 /* Likewise, chain the compilation unit read after this one.
210 The comp units are stored in reversed reading order. */
211 struct comp_unit *prev_unit;
213 /* Keep the bfd convenient (for memory allocation). */
214 bfd *abfd;
216 /* The set of aranges in a compilation unit. */
217 arange_set arange_set;
219 /* The DW_AT_name attribute (for error messages). */
220 char *name;
222 /* The abbrev hash table. */
223 struct abbrev_info **abbrevs;
225 /* Note that an error was found by comp_unit_find_nearest_line. */
226 int error;
228 /* The DW_AT_comp_dir attribute. */
229 char *comp_dir;
231 /* TRUE if there is a line number table associated with this comp. unit. */
232 int stmtlist;
234 /* Pointer to the current comp_unit so that we can find a given entry
235 by its reference. */
236 bfd_byte *info_ptr_unit;
238 /* The offset into .debug_line of the line number table. */
239 unsigned long line_offset;
241 /* Pointer to the first child die for the comp unit. */
242 bfd_byte *first_child_die_ptr;
244 /* The end of the comp unit. */
245 bfd_byte *end_ptr;
247 /* The decoded line number, NULL if not yet decoded. */
248 struct line_info_table *line_table;
250 /* A list of the functions found in this comp. unit. */
251 struct funcinfo *function_table;
253 /* A list of the variables found in this comp. unit. */
254 struct varinfo *variable_table;
256 /* Pointer to dwarf2_debug structure. */
257 struct dwarf2_debug *stash;
259 /* Address size for this unit - from unit header. */
260 unsigned char addr_size;
262 /* Offset size for this unit - from unit header. */
263 unsigned char offset_size;
265 /* Base address for this unit - from DW_AT_low_pc attribute of
266 DW_TAG_compile_unit DIE */
267 bfd_vma base_address;
269 /* TRUE if symbols are cached in hash table for faster lookup by name. */
270 bfd_boolean cached;
273 /* This data structure holds the information of an abbrev. */
274 struct abbrev_info
276 unsigned int number; /* Number identifying abbrev. */
277 enum dwarf_tag tag; /* DWARF tag. */
278 int has_children; /* Boolean. */
279 unsigned int num_attrs; /* Number of attributes. */
280 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
281 struct abbrev_info *next; /* Next in chain. */
284 struct attr_abbrev
286 enum dwarf_attribute name;
287 enum dwarf_form form;
290 #ifndef ABBREV_HASH_SIZE
291 #define ABBREV_HASH_SIZE 121
292 #endif
293 #ifndef ATTR_ALLOC_CHUNK
294 #define ATTR_ALLOC_CHUNK 4
295 #endif
297 /* Variable and function hash tables. This is used to speed up look-up
298 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
299 In order to share code between variable and function infos, we use
300 a list of untyped pointer for all variable/function info associated with
301 a symbol. We waste a bit of memory for list with one node but that
302 simplifies the code. */
304 struct info_list_node
306 struct info_list_node *next;
307 void *info;
310 /* Info hash entry. */
311 struct info_hash_entry
313 struct bfd_hash_entry root;
314 struct info_list_node *head;
317 struct info_hash_table
319 struct bfd_hash_table base;
322 /* Function to create a new entry in info hash table. */
324 static struct bfd_hash_entry *
325 info_hash_table_newfunc (struct bfd_hash_entry *entry,
326 struct bfd_hash_table *table,
327 const char *string)
329 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
331 /* Allocate the structure if it has not already been allocated by a
332 derived class. */
333 if (ret == NULL)
335 ret = bfd_hash_allocate (table, sizeof (* ret));
336 if (ret == NULL)
337 return NULL;
340 /* Call the allocation method of the base class. */
341 ret = ((struct info_hash_entry *)
342 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
344 /* Initialize the local fields here. */
345 if (ret)
346 ret->head = NULL;
348 return (struct bfd_hash_entry *) ret;
351 /* Function to create a new info hash table. It returns a pointer to the
352 newly created table or NULL if there is any error. We need abfd
353 solely for memory allocation. */
355 static struct info_hash_table *
356 create_info_hash_table (bfd *abfd)
358 struct info_hash_table *hash_table;
360 hash_table = bfd_alloc (abfd, sizeof (struct info_hash_table));
361 if (!hash_table)
362 return hash_table;
364 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
365 sizeof (struct info_hash_entry)))
367 bfd_release (abfd, hash_table);
368 return NULL;
371 return hash_table;
374 /* Insert an info entry into an info hash table. We do not check of
375 duplicate entries. Also, the caller need to guarantee that the
376 right type of info in inserted as info is passed as a void* pointer.
377 This function returns true if there is no error. */
379 static bfd_boolean
380 insert_info_hash_table (struct info_hash_table *hash_table,
381 const char *key,
382 void *info,
383 bfd_boolean copy_p)
385 struct info_hash_entry *entry;
386 struct info_list_node *node;
388 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
389 key, TRUE, copy_p);
390 if (!entry)
391 return FALSE;
393 node = bfd_hash_allocate (&hash_table->base, sizeof (*node));
394 if (!node)
395 return FALSE;
397 node->info = info;
398 node->next = entry->head;
399 entry->head = node;
401 return TRUE;
404 /* Look up an info entry list from an info hash table. Return NULL
405 if there is none. */
407 static struct info_list_node *
408 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
410 struct info_hash_entry *entry;
412 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
413 FALSE, FALSE);
414 return entry ? entry->head : NULL;
417 /* VERBATIM
418 The following function up to the END VERBATIM mark are
419 copied directly from dwarf2read.c. */
421 /* Read dwarf information from a buffer. */
423 static unsigned int
424 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
426 return bfd_get_8 (abfd, buf);
429 static int
430 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
432 return bfd_get_signed_8 (abfd, buf);
435 static unsigned int
436 read_2_bytes (bfd *abfd, bfd_byte *buf)
438 return bfd_get_16 (abfd, buf);
441 static unsigned int
442 read_4_bytes (bfd *abfd, bfd_byte *buf)
444 return bfd_get_32 (abfd, buf);
447 static bfd_uint64_t
448 read_8_bytes (bfd *abfd, bfd_byte *buf)
450 return bfd_get_64 (abfd, buf);
453 static bfd_byte *
454 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
455 bfd_byte *buf,
456 unsigned int size ATTRIBUTE_UNUSED)
458 /* If the size of a host char is 8 bits, we can return a pointer
459 to the buffer, otherwise we have to copy the data to a buffer
460 allocated on the temporary obstack. */
461 return buf;
464 static char *
465 read_string (bfd *abfd ATTRIBUTE_UNUSED,
466 bfd_byte *buf,
467 unsigned int *bytes_read_ptr)
469 /* Return a pointer to the embedded string. */
470 char *str = (char *) buf;
471 if (*str == '\0')
473 *bytes_read_ptr = 1;
474 return NULL;
477 *bytes_read_ptr = strlen (str) + 1;
478 return str;
481 static char *
482 read_indirect_string (struct comp_unit* unit,
483 bfd_byte *buf,
484 unsigned int *bytes_read_ptr)
486 bfd_uint64_t offset;
487 struct dwarf2_debug *stash = unit->stash;
488 char *str;
490 if (unit->offset_size == 4)
491 offset = read_4_bytes (unit->abfd, buf);
492 else
493 offset = read_8_bytes (unit->abfd, buf);
494 *bytes_read_ptr = unit->offset_size;
496 if (! stash->dwarf_str_buffer)
498 asection *msec;
499 bfd *abfd = unit->abfd;
500 bfd_size_type sz;
502 msec = bfd_get_section_by_name (abfd, ".debug_str");
503 if (! msec)
505 (*_bfd_error_handler)
506 (_("Dwarf Error: Can't find .debug_str section."));
507 bfd_set_error (bfd_error_bad_value);
508 return NULL;
511 sz = msec->rawsize ? msec->rawsize : msec->size;
512 stash->dwarf_str_size = sz;
513 stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
514 if (! stash->dwarf_str_buffer)
515 return NULL;
517 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
518 0, sz))
519 return NULL;
522 if (offset >= stash->dwarf_str_size)
524 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
525 (unsigned long) offset, stash->dwarf_str_size);
526 bfd_set_error (bfd_error_bad_value);
527 return NULL;
530 str = (char *) stash->dwarf_str_buffer + offset;
531 if (*str == '\0')
532 return NULL;
533 return str;
536 /* END VERBATIM */
538 static bfd_uint64_t
539 read_address (struct comp_unit *unit, bfd_byte *buf)
541 int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
543 if (signed_vma)
545 switch (unit->addr_size)
547 case 8:
548 return bfd_get_signed_64 (unit->abfd, buf);
549 case 4:
550 return bfd_get_signed_32 (unit->abfd, buf);
551 case 2:
552 return bfd_get_signed_16 (unit->abfd, buf);
553 default:
554 abort ();
557 else
559 switch (unit->addr_size)
561 case 8:
562 return bfd_get_64 (unit->abfd, buf);
563 case 4:
564 return bfd_get_32 (unit->abfd, buf);
565 case 2:
566 return bfd_get_16 (unit->abfd, buf);
567 default:
568 abort ();
573 /* Lookup an abbrev_info structure in the abbrev hash table. */
575 static struct abbrev_info *
576 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
578 unsigned int hash_number;
579 struct abbrev_info *abbrev;
581 hash_number = number % ABBREV_HASH_SIZE;
582 abbrev = abbrevs[hash_number];
584 while (abbrev)
586 if (abbrev->number == number)
587 return abbrev;
588 else
589 abbrev = abbrev->next;
592 return NULL;
595 /* In DWARF version 2, the description of the debugging information is
596 stored in a separate .debug_abbrev section. Before we read any
597 dies from a section we read in all abbreviations and install them
598 in a hash table. */
600 static struct abbrev_info**
601 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
603 struct abbrev_info **abbrevs;
604 bfd_byte *abbrev_ptr;
605 struct abbrev_info *cur_abbrev;
606 unsigned int abbrev_number, bytes_read, abbrev_name;
607 unsigned int abbrev_form, hash_number;
608 bfd_size_type amt;
610 if (! stash->dwarf_abbrev_buffer)
612 asection *msec;
614 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
615 if (! msec)
617 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
618 bfd_set_error (bfd_error_bad_value);
619 return 0;
622 stash->dwarf_abbrev_size = msec->size;
623 stash->dwarf_abbrev_buffer
624 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
625 stash->syms);
626 if (! stash->dwarf_abbrev_buffer)
627 return 0;
630 if (offset >= stash->dwarf_abbrev_size)
632 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
633 (unsigned long) offset, stash->dwarf_abbrev_size);
634 bfd_set_error (bfd_error_bad_value);
635 return 0;
638 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
639 abbrevs = bfd_zalloc (abfd, amt);
641 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
642 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
643 abbrev_ptr += bytes_read;
645 /* Loop until we reach an abbrev number of 0. */
646 while (abbrev_number)
648 amt = sizeof (struct abbrev_info);
649 cur_abbrev = bfd_zalloc (abfd, amt);
651 /* Read in abbrev header. */
652 cur_abbrev->number = abbrev_number;
653 cur_abbrev->tag = (enum dwarf_tag)
654 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
655 abbrev_ptr += bytes_read;
656 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
657 abbrev_ptr += 1;
659 /* Now read in declarations. */
660 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
661 abbrev_ptr += bytes_read;
662 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
663 abbrev_ptr += bytes_read;
665 while (abbrev_name)
667 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
669 struct attr_abbrev *tmp;
671 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
672 amt *= sizeof (struct attr_abbrev);
673 tmp = bfd_realloc (cur_abbrev->attrs, amt);
674 if (tmp == NULL)
676 size_t i;
678 for (i = 0; i < ABBREV_HASH_SIZE; i++)
680 struct abbrev_info *abbrev = abbrevs[i];
682 while (abbrev)
684 free (abbrev->attrs);
685 abbrev = abbrev->next;
688 return NULL;
690 cur_abbrev->attrs = tmp;
693 cur_abbrev->attrs[cur_abbrev->num_attrs].name
694 = (enum dwarf_attribute) abbrev_name;
695 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
696 = (enum dwarf_form) abbrev_form;
697 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
698 abbrev_ptr += bytes_read;
699 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
700 abbrev_ptr += bytes_read;
703 hash_number = abbrev_number % ABBREV_HASH_SIZE;
704 cur_abbrev->next = abbrevs[hash_number];
705 abbrevs[hash_number] = cur_abbrev;
707 /* Get next abbreviation.
708 Under Irix6 the abbreviations for a compilation unit are not
709 always properly terminated with an abbrev number of 0.
710 Exit loop if we encounter an abbreviation which we have
711 already read (which means we are about to read the abbreviations
712 for the next compile unit) or if the end of the abbreviation
713 table is reached. */
714 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
715 >= stash->dwarf_abbrev_size)
716 break;
717 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
718 abbrev_ptr += bytes_read;
719 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
720 break;
723 return abbrevs;
726 /* Read an attribute value described by an attribute form. */
728 static bfd_byte *
729 read_attribute_value (struct attribute *attr,
730 unsigned form,
731 struct comp_unit *unit,
732 bfd_byte *info_ptr)
734 bfd *abfd = unit->abfd;
735 unsigned int bytes_read;
736 struct dwarf_block *blk;
737 bfd_size_type amt;
739 attr->form = (enum dwarf_form) form;
741 switch (form)
743 case DW_FORM_addr:
744 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
745 case DW_FORM_ref_addr:
746 attr->u.val = read_address (unit, info_ptr);
747 info_ptr += unit->addr_size;
748 break;
749 case DW_FORM_block2:
750 amt = sizeof (struct dwarf_block);
751 blk = bfd_alloc (abfd, amt);
752 blk->size = read_2_bytes (abfd, info_ptr);
753 info_ptr += 2;
754 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
755 info_ptr += blk->size;
756 attr->u.blk = blk;
757 break;
758 case DW_FORM_block4:
759 amt = sizeof (struct dwarf_block);
760 blk = bfd_alloc (abfd, amt);
761 blk->size = read_4_bytes (abfd, info_ptr);
762 info_ptr += 4;
763 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
764 info_ptr += blk->size;
765 attr->u.blk = blk;
766 break;
767 case DW_FORM_data2:
768 attr->u.val = read_2_bytes (abfd, info_ptr);
769 info_ptr += 2;
770 break;
771 case DW_FORM_data4:
772 attr->u.val = read_4_bytes (abfd, info_ptr);
773 info_ptr += 4;
774 break;
775 case DW_FORM_data8:
776 attr->u.val = read_8_bytes (abfd, info_ptr);
777 info_ptr += 8;
778 break;
779 case DW_FORM_string:
780 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
781 info_ptr += bytes_read;
782 break;
783 case DW_FORM_strp:
784 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
785 info_ptr += bytes_read;
786 break;
787 case DW_FORM_block:
788 amt = sizeof (struct dwarf_block);
789 blk = bfd_alloc (abfd, amt);
790 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
791 info_ptr += bytes_read;
792 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
793 info_ptr += blk->size;
794 attr->u.blk = blk;
795 break;
796 case DW_FORM_block1:
797 amt = sizeof (struct dwarf_block);
798 blk = bfd_alloc (abfd, amt);
799 blk->size = read_1_byte (abfd, info_ptr);
800 info_ptr += 1;
801 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
802 info_ptr += blk->size;
803 attr->u.blk = blk;
804 break;
805 case DW_FORM_data1:
806 attr->u.val = read_1_byte (abfd, info_ptr);
807 info_ptr += 1;
808 break;
809 case DW_FORM_flag:
810 attr->u.val = read_1_byte (abfd, info_ptr);
811 info_ptr += 1;
812 break;
813 case DW_FORM_sdata:
814 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
815 info_ptr += bytes_read;
816 break;
817 case DW_FORM_udata:
818 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
819 info_ptr += bytes_read;
820 break;
821 case DW_FORM_ref1:
822 attr->u.val = read_1_byte (abfd, info_ptr);
823 info_ptr += 1;
824 break;
825 case DW_FORM_ref2:
826 attr->u.val = read_2_bytes (abfd, info_ptr);
827 info_ptr += 2;
828 break;
829 case DW_FORM_ref4:
830 attr->u.val = read_4_bytes (abfd, info_ptr);
831 info_ptr += 4;
832 break;
833 case DW_FORM_ref8:
834 attr->u.val = read_8_bytes (abfd, info_ptr);
835 info_ptr += 8;
836 break;
837 case DW_FORM_ref_udata:
838 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
839 info_ptr += bytes_read;
840 break;
841 case DW_FORM_indirect:
842 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
843 info_ptr += bytes_read;
844 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
845 break;
846 default:
847 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
848 form);
849 bfd_set_error (bfd_error_bad_value);
851 return info_ptr;
854 /* Read an attribute described by an abbreviated attribute. */
856 static bfd_byte *
857 read_attribute (struct attribute *attr,
858 struct attr_abbrev *abbrev,
859 struct comp_unit *unit,
860 bfd_byte *info_ptr)
862 attr->name = abbrev->name;
863 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
864 return info_ptr;
867 /* Source line information table routines. */
869 #define FILE_ALLOC_CHUNK 5
870 #define DIR_ALLOC_CHUNK 5
872 struct line_info
874 struct line_info* prev_line;
875 bfd_vma address;
876 char *filename;
877 unsigned int line;
878 unsigned int column;
879 int end_sequence; /* End of (sequential) code sequence. */
882 struct fileinfo
884 char *name;
885 unsigned int dir;
886 unsigned int time;
887 unsigned int size;
890 struct line_info_table
892 bfd* abfd;
893 unsigned int num_files;
894 unsigned int num_dirs;
895 char *comp_dir;
896 char **dirs;
897 struct fileinfo* files;
898 struct line_info* last_line; /* Largest VMA. */
899 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
902 /* Remember some information about each function. If the function is
903 inlined (DW_TAG_inlined_subroutine) it may have two additional
904 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
905 source code location where this function was inlined. */
907 struct funcinfo
909 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions. */
910 struct funcinfo *caller_func; /* Pointer to function one scope higher. */
911 char *caller_file; /* Source location file name where caller_func inlines this func. */
912 int caller_line; /* Source location line number where caller_func inlines this func. */
913 char *file; /* Source location file name. */
914 int line; /* Source location line number. */
915 int tag;
916 char *name;
917 struct arange arange;
918 asection *sec; /* Where the symbol is defined. */
921 struct varinfo
923 /* Pointer to previous variable in list of all variables. */
924 struct varinfo *prev_var;
925 /* Source location file name. */
926 char *file;
927 /* Source location line number. */
928 int line;
929 int tag;
930 char *name;
931 bfd_vma addr;
932 /* Where the symbol is defined. */
933 asection *sec;
934 /* Is this a stack variable? */
935 unsigned int stack: 1;
938 /* Arange-sets:
940 To handle extremely large binaries, we want to use a more efficient data
941 structure than a singly-linked list to represent aranges. So instead we
942 use an arange-set, which supports efficient insertions and queries. We
943 use a simple arange-set with no values attached to represent the aranges
944 in a compilation unit and we also use a global arange-set to store all
945 the aranges in all the compilation units. The global arange-set stores
946 values which are pointers to the compilation units.
948 Normally aranges in the global set do not overlap, but this can happen.
949 To simplify things and to prevent excessive memory usage, an arange in
950 the global set can only point to at most one compilation unit. In case
951 of an overlap, the pointer is set to NULL, meaning that there are more
952 than one compilation units containing that arange. Code that looks up
953 the global set should fall back to searching all compilation units if
954 that happens. */
956 /* Allocate memory for an arange set. */
958 static void *
959 dwarf2_arange_set_allocate (int size, void *data)
961 return bfd_alloc ((bfd *) data, size);
964 /* Deallocate memory of an arange set. */
966 static void
967 dwarf2_arange_set_deallocate (void *object ATTRIBUTE_UNUSED,
968 void *data ATTRIBUTE_UNUSED)
970 /* Do nothing. Let BFD clean up when it's done. */
973 /* Combine two comp unit pointers. If they are the same,
974 return either one, otherwise return NULL. */
976 static arange_value_type
977 dwarf2_combine_arange_value (arange_value_type value1,
978 arange_value_type value2,
979 void *data ATTRIBUTE_UNUSED)
981 return ((value1 == value2) ? value1 : 0);
984 /* Create a simple arange set that does not store values. */
986 static arange_set
987 dwarf2_arange_set_new (bfd *abfd)
989 return arange_set_new (dwarf2_arange_set_allocate,
990 dwarf2_arange_set_deallocate,
991 FALSE, NULL, NULL, NULL, NULL, (void *) abfd);
994 /* Create an arange set that stores pointers to compilation units. */
996 static arange_set
997 dwarf2_arange_set_with_value_new (bfd *abfd)
999 return arange_set_new (dwarf2_arange_set_allocate,
1000 dwarf2_arange_set_deallocate,
1001 TRUE, NULL, NULL, dwarf2_combine_arange_value,
1002 NULL, (void *) abfd);
1005 /* Add an arange to a compilation unit. Add the arange to both the
1006 unit's valueless arange set and the global arange set. */
1008 static void
1009 dwarf2_comp_unit_arange_add (struct comp_unit *unit,
1010 bfd_vma low,
1011 bfd_vma high)
1013 /* Add arange to unit's local arange set. */
1014 arange_set_insert (unit->arange_set, low, high - 1, 0);
1016 if (unit->stash->arange_set_status == STASH_ARANGE_SET_ON)
1018 BFD_ASSERT (unit->stash->comp_unit_arange_set);
1019 arange_set_insert (unit->stash->comp_unit_arange_set, low, high - 1,
1020 (arange_value_type) unit);
1024 /* Return TRUE if NEW_LINE should sort after LINE. */
1026 static inline bfd_boolean
1027 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1029 return (new_line->address > line->address
1030 || (new_line->address == line->address
1031 && new_line->end_sequence < line->end_sequence));
1035 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1036 that the list is sorted. Note that the line_info list is sorted from
1037 highest to lowest VMA (with possible duplicates); that is,
1038 line_info->prev_line always accesses an equal or smaller VMA. */
1040 static void
1041 add_line_info (struct line_info_table *table,
1042 bfd_vma address,
1043 char *filename,
1044 unsigned int line,
1045 unsigned int column,
1046 int end_sequence)
1048 bfd_size_type amt = sizeof (struct line_info);
1049 struct line_info * info = bfd_alloc (table->abfd, amt);
1051 /* Set member data of 'info'. */
1052 info->address = address;
1053 info->line = line;
1054 info->column = column;
1055 info->end_sequence = end_sequence;
1057 if (filename && filename[0])
1059 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
1060 if (info->filename)
1061 strcpy (info->filename, filename);
1063 else
1064 info->filename = NULL;
1066 /* Find the correct location for 'info'. Normally we will receive
1067 new line_info data 1) in order and 2) with increasing VMAs.
1068 However some compilers break the rules (cf. decode_line_info) and
1069 so we include some heuristics for quickly finding the correct
1070 location for 'info'. In particular, these heuristics optimize for
1071 the common case in which the VMA sequence that we receive is a
1072 list of locally sorted VMAs such as
1073 p...z a...j (where a < j < p < z)
1075 Note: table->lcl_head is used to head an *actual* or *possible*
1076 sequence within the list (such as a...j) that is not directly
1077 headed by table->last_line
1079 Note: we may receive duplicate entries from 'decode_line_info'. */
1081 if (table->last_line
1082 && table->last_line->address == address
1083 && table->last_line->end_sequence == end_sequence)
1085 /* We only keep the last entry with the same address and end
1086 sequence. See PR ld/4986. */
1087 if (table->lcl_head == table->last_line)
1088 table->lcl_head = info;
1089 info->prev_line = table->last_line->prev_line;
1090 table->last_line = info;
1092 else if (!table->last_line
1093 || new_line_sorts_after (info, table->last_line))
1095 /* Normal case: add 'info' to the beginning of the list. */
1096 info->prev_line = table->last_line;
1097 table->last_line = info;
1099 /* lcl_head: initialize to head a *possible* sequence at the end. */
1100 if (!table->lcl_head)
1101 table->lcl_head = info;
1103 else if (!new_line_sorts_after (info, table->lcl_head)
1104 && (!table->lcl_head->prev_line
1105 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1107 /* Abnormal but easy: lcl_head is the head of 'info'. */
1108 info->prev_line = table->lcl_head->prev_line;
1109 table->lcl_head->prev_line = info;
1111 else
1113 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
1114 heads for 'info'. Reset 'lcl_head'. */
1115 struct line_info* li2 = table->last_line; /* Always non-NULL. */
1116 struct line_info* li1 = li2->prev_line;
1118 while (li1)
1120 if (!new_line_sorts_after (info, li2)
1121 && new_line_sorts_after (info, li1))
1122 break;
1124 li2 = li1; /* Always non-NULL. */
1125 li1 = li1->prev_line;
1127 table->lcl_head = li2;
1128 info->prev_line = table->lcl_head->prev_line;
1129 table->lcl_head->prev_line = info;
1133 /* Extract a fully qualified filename from a line info table.
1134 The returned string has been malloc'ed and it is the caller's
1135 responsibility to free it. */
1137 static char *
1138 concat_filename (struct line_info_table *table, unsigned int file)
1140 char *filename;
1142 if (file - 1 >= table->num_files)
1144 /* FILE == 0 means unknown. */
1145 if (file)
1146 (*_bfd_error_handler)
1147 (_("Dwarf Error: mangled line number section (bad file number)."));
1148 return strdup ("<unknown>");
1151 filename = table->files[file - 1].name;
1153 if (!IS_ABSOLUTE_PATH (filename))
1155 char *dirname = NULL;
1156 char *subdirname = NULL;
1157 char *name;
1158 size_t len;
1160 if (table->files[file - 1].dir)
1161 subdirname = table->dirs[table->files[file - 1].dir - 1];
1163 if (!subdirname || !IS_ABSOLUTE_PATH (subdirname))
1164 dirname = table->comp_dir;
1166 if (!dirname)
1168 dirname = subdirname;
1169 subdirname = NULL;
1172 if (!dirname)
1173 return strdup (filename);
1175 len = strlen (dirname) + strlen (filename) + 2;
1177 if (subdirname)
1179 len += strlen (subdirname) + 1;
1180 name = bfd_malloc (len);
1181 if (name)
1182 sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
1184 else
1186 name = bfd_malloc (len);
1187 if (name)
1188 sprintf (name, "%s/%s", dirname, filename);
1191 return name;
1194 return strdup (filename);
1197 static void
1198 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc,
1199 bfd_vma high_pc)
1201 struct arange *arange;
1203 /* If the first arange is empty, use it. */
1204 if (first_arange->high == 0)
1206 first_arange->low = low_pc;
1207 first_arange->high = high_pc;
1208 return;
1211 /* Next see if we can cheaply extend an existing range. */
1212 arange = first_arange;
1215 if (low_pc == arange->high)
1217 arange->high = high_pc;
1218 return;
1220 if (high_pc == arange->low)
1222 arange->low = low_pc;
1223 return;
1225 arange = arange->next;
1227 while (arange);
1229 /* Need to allocate a new arange and insert it into the arange list.
1230 Order isn't significant, so just insert after the first arange. */
1231 arange = bfd_zalloc (abfd, sizeof (*arange));
1232 arange->low = low_pc;
1233 arange->high = high_pc;
1234 arange->next = first_arange->next;
1235 first_arange->next = arange;
1238 /* Decode the line number information for UNIT. */
1240 static struct line_info_table*
1241 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
1243 bfd *abfd = unit->abfd;
1244 struct line_info_table* table;
1245 bfd_byte *line_ptr;
1246 bfd_byte *line_end;
1247 struct line_head lh;
1248 unsigned int i, bytes_read, offset_size;
1249 char *cur_file, *cur_dir;
1250 unsigned char op_code, extended_op, adj_opcode;
1251 bfd_size_type amt;
1253 if (! stash->dwarf_line_buffer)
1255 asection *msec;
1257 msec = bfd_get_section_by_name (abfd, ".debug_line");
1258 if (! msec)
1260 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
1261 bfd_set_error (bfd_error_bad_value);
1262 return 0;
1265 stash->dwarf_line_size = msec->size;
1266 stash->dwarf_line_buffer
1267 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1268 stash->syms);
1269 if (! stash->dwarf_line_buffer)
1270 return 0;
1273 /* It is possible to get a bad value for the line_offset. Validate
1274 it here so that we won't get a segfault below. */
1275 if (unit->line_offset >= stash->dwarf_line_size)
1277 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
1278 unit->line_offset, stash->dwarf_line_size);
1279 bfd_set_error (bfd_error_bad_value);
1280 return 0;
1283 amt = sizeof (struct line_info_table);
1284 table = bfd_alloc (abfd, amt);
1285 table->abfd = abfd;
1286 table->comp_dir = unit->comp_dir;
1288 table->num_files = 0;
1289 table->files = NULL;
1291 table->num_dirs = 0;
1292 table->dirs = NULL;
1294 table->files = NULL;
1295 table->last_line = NULL;
1296 table->lcl_head = NULL;
1298 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1300 /* Read in the prologue. */
1301 lh.total_length = read_4_bytes (abfd, line_ptr);
1302 line_ptr += 4;
1303 offset_size = 4;
1304 if (lh.total_length == 0xffffffff)
1306 lh.total_length = read_8_bytes (abfd, line_ptr);
1307 line_ptr += 8;
1308 offset_size = 8;
1310 else if (lh.total_length == 0 && unit->addr_size == 8)
1312 /* Handle (non-standard) 64-bit DWARF2 formats. */
1313 lh.total_length = read_4_bytes (abfd, line_ptr);
1314 line_ptr += 4;
1315 offset_size = 8;
1317 line_end = line_ptr + lh.total_length;
1318 lh.version = read_2_bytes (abfd, line_ptr);
1319 line_ptr += 2;
1320 if (offset_size == 4)
1321 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1322 else
1323 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1324 line_ptr += offset_size;
1325 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1326 line_ptr += 1;
1327 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1328 line_ptr += 1;
1329 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1330 line_ptr += 1;
1331 lh.line_range = read_1_byte (abfd, line_ptr);
1332 line_ptr += 1;
1333 lh.opcode_base = read_1_byte (abfd, line_ptr);
1334 line_ptr += 1;
1335 amt = lh.opcode_base * sizeof (unsigned char);
1336 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
1338 lh.standard_opcode_lengths[0] = 1;
1340 for (i = 1; i < lh.opcode_base; ++i)
1342 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1343 line_ptr += 1;
1346 /* Read directory table. */
1347 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1349 line_ptr += bytes_read;
1351 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1353 char **tmp;
1355 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1356 amt *= sizeof (char *);
1358 tmp = bfd_realloc (table->dirs, amt);
1359 if (tmp == NULL)
1361 free (table->dirs);
1362 return NULL;
1364 table->dirs = tmp;
1367 table->dirs[table->num_dirs++] = cur_dir;
1370 line_ptr += bytes_read;
1372 /* Read file name table. */
1373 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1375 line_ptr += bytes_read;
1377 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1379 struct fileinfo *tmp;
1381 amt = table->num_files + FILE_ALLOC_CHUNK;
1382 amt *= sizeof (struct fileinfo);
1384 tmp = bfd_realloc (table->files, amt);
1385 if (tmp == NULL)
1387 free (table->files);
1388 free (table->dirs);
1389 return NULL;
1391 table->files = tmp;
1394 table->files[table->num_files].name = cur_file;
1395 table->files[table->num_files].dir =
1396 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1397 line_ptr += bytes_read;
1398 table->files[table->num_files].time =
1399 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1400 line_ptr += bytes_read;
1401 table->files[table->num_files].size =
1402 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1403 line_ptr += bytes_read;
1404 table->num_files++;
1407 line_ptr += bytes_read;
1409 /* Read the statement sequences until there's nothing left. */
1410 while (line_ptr < line_end)
1412 /* State machine registers. */
1413 bfd_vma address = 0;
1414 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1415 unsigned int line = 1;
1416 unsigned int column = 0;
1417 int is_stmt = lh.default_is_stmt;
1418 int end_sequence = 0;
1419 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1420 compilers generate address sequences that are wildly out of
1421 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1422 for ia64-Linux). Thus, to determine the low and high
1423 address, we must compare on every DW_LNS_copy, etc. */
1424 bfd_vma low_pc = (bfd_vma) -1;
1425 bfd_vma high_pc = 0;
1427 /* Decode the table. */
1428 while (! end_sequence)
1430 op_code = read_1_byte (abfd, line_ptr);
1431 line_ptr += 1;
1433 if (op_code >= lh.opcode_base)
1435 /* Special operand. */
1436 adj_opcode = op_code - lh.opcode_base;
1437 address += (adj_opcode / lh.line_range)
1438 * lh.minimum_instruction_length;
1439 line += lh.line_base + (adj_opcode % lh.line_range);
1440 /* Append row to matrix using current values. */
1441 add_line_info (table, address, filename, line, column, 0);
1442 if (address < low_pc)
1443 low_pc = address;
1444 if (address > high_pc)
1445 high_pc = address;
1447 else switch (op_code)
1449 case DW_LNS_extended_op:
1450 /* Ignore length. */
1451 line_ptr += 1;
1452 extended_op = read_1_byte (abfd, line_ptr);
1453 line_ptr += 1;
1455 switch (extended_op)
1457 case DW_LNE_end_sequence:
1458 end_sequence = 1;
1459 add_line_info (table, address, filename, line, column,
1460 end_sequence);
1461 if (address < low_pc)
1462 low_pc = address;
1463 if (address > high_pc)
1464 high_pc = address;
1465 dwarf2_comp_unit_arange_add (unit, low_pc, high_pc);
1466 break;
1467 case DW_LNE_set_address:
1468 address = read_address (unit, line_ptr);
1469 line_ptr += unit->addr_size;
1470 break;
1471 case DW_LNE_define_file:
1472 cur_file = read_string (abfd, line_ptr, &bytes_read);
1473 line_ptr += bytes_read;
1474 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1476 struct fileinfo *tmp;
1478 amt = table->num_files + FILE_ALLOC_CHUNK;
1479 amt *= sizeof (struct fileinfo);
1480 tmp = bfd_realloc (table->files, amt);
1481 if (tmp == NULL)
1483 free (table->files);
1484 free (table->dirs);
1485 free (filename);
1486 return NULL;
1488 table->files = tmp;
1490 table->files[table->num_files].name = cur_file;
1491 table->files[table->num_files].dir =
1492 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1493 line_ptr += bytes_read;
1494 table->files[table->num_files].time =
1495 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1496 line_ptr += bytes_read;
1497 table->files[table->num_files].size =
1498 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1499 line_ptr += bytes_read;
1500 table->num_files++;
1501 break;
1502 default:
1503 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1504 bfd_set_error (bfd_error_bad_value);
1505 free (filename);
1506 free (table->files);
1507 free (table->dirs);
1508 return NULL;
1510 break;
1511 case DW_LNS_copy:
1512 add_line_info (table, address, filename, line, column, 0);
1513 if (address < low_pc)
1514 low_pc = address;
1515 if (address > high_pc)
1516 high_pc = address;
1517 break;
1518 case DW_LNS_advance_pc:
1519 address += lh.minimum_instruction_length
1520 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1521 line_ptr += bytes_read;
1522 break;
1523 case DW_LNS_advance_line:
1524 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1525 line_ptr += bytes_read;
1526 break;
1527 case DW_LNS_set_file:
1529 unsigned int file;
1531 /* The file and directory tables are 0
1532 based, the references are 1 based. */
1533 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1534 line_ptr += bytes_read;
1535 if (filename)
1536 free (filename);
1537 filename = concat_filename (table, file);
1538 break;
1540 case DW_LNS_set_column:
1541 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1542 line_ptr += bytes_read;
1543 break;
1544 case DW_LNS_negate_stmt:
1545 is_stmt = (!is_stmt);
1546 break;
1547 case DW_LNS_set_basic_block:
1548 break;
1549 case DW_LNS_const_add_pc:
1550 address += lh.minimum_instruction_length
1551 * ((255 - lh.opcode_base) / lh.line_range);
1552 break;
1553 case DW_LNS_fixed_advance_pc:
1554 address += read_2_bytes (abfd, line_ptr);
1555 line_ptr += 2;
1556 break;
1557 default:
1559 int i;
1561 /* Unknown standard opcode, ignore it. */
1562 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1564 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1565 line_ptr += bytes_read;
1571 if (filename)
1572 free (filename);
1575 return table;
1578 /* If ADDR is within TABLE set the output parameters and return TRUE,
1579 otherwise return FALSE. The output parameters, FILENAME_PTR and
1580 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1582 static bfd_boolean
1583 lookup_address_in_line_info_table (struct line_info_table *table,
1584 bfd_vma addr,
1585 struct funcinfo *function,
1586 const char **filename_ptr,
1587 unsigned int *linenumber_ptr)
1589 /* Note: table->last_line should be a descendingly sorted list. */
1590 struct line_info* next_line = table->last_line;
1591 struct line_info* each_line = NULL;
1592 *filename_ptr = NULL;
1594 if (!next_line)
1595 return FALSE;
1597 each_line = next_line->prev_line;
1599 /* Check for large addresses */
1600 if (addr > next_line->address)
1601 each_line = NULL; /* ensure we skip over the normal case */
1603 /* Normal case: search the list; save */
1604 while (each_line && next_line)
1606 /* If we have an address match, save this info. This allows us
1607 to return as good as results as possible for strange debugging
1608 info. */
1609 bfd_boolean addr_match = FALSE;
1610 if (each_line->address <= addr && addr < next_line->address)
1612 addr_match = TRUE;
1614 /* If this line appears to span functions, and addr is in the
1615 later function, return the first line of that function instead
1616 of the last line of the earlier one. This check is for GCC
1617 2.95, which emits the first line number for a function late. */
1619 if (function != NULL)
1621 bfd_vma lowest_pc;
1622 struct arange *arange;
1624 /* Find the lowest address in the function's range list */
1625 lowest_pc = function->arange.low;
1626 for (arange = &function->arange;
1627 arange;
1628 arange = arange->next)
1630 if (function->arange.low < lowest_pc)
1631 lowest_pc = function->arange.low;
1633 /* Check for spanning function and set outgoing line info */
1634 if (addr >= lowest_pc
1635 && each_line->address < lowest_pc
1636 && next_line->address > lowest_pc)
1638 *filename_ptr = next_line->filename;
1639 *linenumber_ptr = next_line->line;
1641 else
1643 *filename_ptr = each_line->filename;
1644 *linenumber_ptr = each_line->line;
1647 else
1649 *filename_ptr = each_line->filename;
1650 *linenumber_ptr = each_line->line;
1654 if (addr_match && !each_line->end_sequence)
1655 return TRUE; /* we have definitely found what we want */
1657 next_line = each_line;
1658 each_line = each_line->prev_line;
1661 /* At this point each_line is NULL but next_line is not. If we found
1662 a candidate end-of-sequence point in the loop above, we can return
1663 that (compatibility with a bug in the Intel compiler); otherwise,
1664 assuming that we found the containing function for this address in
1665 this compilation unit, return the first line we have a number for
1666 (compatibility with GCC 2.95). */
1667 if (*filename_ptr == NULL && function != NULL)
1669 *filename_ptr = next_line->filename;
1670 *linenumber_ptr = next_line->line;
1671 return TRUE;
1674 return FALSE;
1677 /* Read in the .debug_ranges section for future reference */
1679 static bfd_boolean
1680 read_debug_ranges (struct comp_unit *unit)
1682 struct dwarf2_debug *stash = unit->stash;
1683 if (! stash->dwarf_ranges_buffer)
1685 bfd *abfd = unit->abfd;
1686 asection *msec;
1688 msec = bfd_get_section_by_name (abfd, ".debug_ranges");
1689 if (! msec)
1691 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
1692 bfd_set_error (bfd_error_bad_value);
1693 return FALSE;
1696 stash->dwarf_ranges_size = msec->size;
1697 stash->dwarf_ranges_buffer
1698 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1699 stash->syms);
1700 if (! stash->dwarf_ranges_buffer)
1701 return FALSE;
1703 return TRUE;
1706 /* Function table functions. */
1708 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1709 Note that we need to find the function that has the smallest
1710 range that contains ADDR, to handle inlined functions without
1711 depending upon them being ordered in TABLE by increasing range. */
1713 static bfd_boolean
1714 lookup_address_in_function_table (struct comp_unit *unit,
1715 bfd_vma addr,
1716 struct funcinfo **function_ptr,
1717 const char **functionname_ptr)
1719 struct funcinfo* each_func;
1720 struct funcinfo* best_fit = NULL;
1721 struct arange *arange;
1723 for (each_func = unit->function_table;
1724 each_func;
1725 each_func = each_func->prev_func)
1727 for (arange = &each_func->arange;
1728 arange;
1729 arange = arange->next)
1731 if (addr >= arange->low && addr < arange->high)
1733 if (!best_fit ||
1734 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1735 best_fit = each_func;
1740 if (best_fit)
1742 *functionname_ptr = best_fit->name;
1743 *function_ptr = best_fit;
1744 return TRUE;
1746 else
1748 return FALSE;
1752 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1753 and LINENUMBER_PTR, and return TRUE. */
1755 static bfd_boolean
1756 lookup_symbol_in_function_table (struct comp_unit *unit,
1757 asymbol *sym,
1758 bfd_vma addr,
1759 const char **filename_ptr,
1760 unsigned int *linenumber_ptr)
1762 struct funcinfo* each_func;
1763 struct funcinfo* best_fit = NULL;
1764 struct arange *arange;
1765 const char *name = bfd_asymbol_name (sym);
1766 asection *sec = bfd_get_section (sym);
1768 for (each_func = unit->function_table;
1769 each_func;
1770 each_func = each_func->prev_func)
1772 for (arange = &each_func->arange;
1773 arange;
1774 arange = arange->next)
1776 if ((!each_func->sec || each_func->sec == sec)
1777 && addr >= arange->low
1778 && addr < arange->high
1779 && each_func->name
1780 && strcmp (name, each_func->name) == 0
1781 && (!best_fit
1782 || ((arange->high - arange->low)
1783 < (best_fit->arange.high - best_fit->arange.low))))
1784 best_fit = each_func;
1788 if (best_fit)
1790 best_fit->sec = sec;
1791 *filename_ptr = best_fit->file;
1792 *linenumber_ptr = best_fit->line;
1793 return TRUE;
1795 else
1796 return FALSE;
1799 /* Variable table functions. */
1801 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1802 LINENUMBER_PTR, and return TRUE. */
1804 static bfd_boolean
1805 lookup_symbol_in_variable_table (struct comp_unit *unit,
1806 asymbol *sym,
1807 bfd_vma addr,
1808 const char **filename_ptr,
1809 unsigned int *linenumber_ptr)
1811 const char *name = bfd_asymbol_name (sym);
1812 asection *sec = bfd_get_section (sym);
1813 struct varinfo* each;
1815 for (each = unit->variable_table; each; each = each->prev_var)
1816 if (each->stack == 0
1817 && each->file != NULL
1818 && each->name != NULL
1819 && each->addr == addr
1820 && (!each->sec || each->sec == sec)
1821 && strcmp (name, each->name) == 0)
1822 break;
1824 if (each)
1826 each->sec = sec;
1827 *filename_ptr = each->file;
1828 *linenumber_ptr = each->line;
1829 return TRUE;
1831 else
1832 return FALSE;
1835 static char *
1836 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1838 bfd *abfd = unit->abfd;
1839 bfd_byte *info_ptr;
1840 unsigned int abbrev_number, bytes_read, i;
1841 struct abbrev_info *abbrev;
1842 struct attribute attr;
1843 char *name = 0;
1845 info_ptr = unit->info_ptr_unit + die_ref;
1846 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1847 info_ptr += bytes_read;
1849 if (abbrev_number)
1851 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1852 if (! abbrev)
1854 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1855 abbrev_number);
1856 bfd_set_error (bfd_error_bad_value);
1858 else
1860 for (i = 0; i < abbrev->num_attrs; ++i)
1862 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1863 switch (attr.name)
1865 case DW_AT_name:
1866 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1867 if (name == NULL)
1868 name = attr.u.str;
1869 break;
1870 case DW_AT_specification:
1871 name = find_abstract_instance_name (unit, attr.u.val);
1872 break;
1873 case DW_AT_MIPS_linkage_name:
1874 name = attr.u.str;
1875 break;
1876 default:
1877 break;
1882 return name;
1885 /* Type of callback function used in read_rangelist below. */
1887 typedef void (*read_rangelist_callback_t)(struct comp_unit*, bfd_vma,
1888 bfd_vma, void*);
1890 /* Call back to add an arange to the old-style arange list. */
1892 static void
1893 read_rangelist_insert_arange_list (struct comp_unit *unit,
1894 bfd_vma low,
1895 bfd_vma high,
1896 void *data)
1898 arange_add (unit->abfd, (struct arange*) data, low, high);
1901 /* Callback to add an arange in the arange set of a compilation unit. */
1903 static void
1904 read_rangelist_comp_unit_arange_add (struct comp_unit *unit,
1905 bfd_vma low,
1906 bfd_vma high,
1907 void *data ATTRIBUTE_UNUSED)
1909 dwarf2_comp_unit_arange_add (unit, low, high);
1912 /* Read ARANGE list of a compilation unit. For each read arange,
1913 call the supplied callback function for further processing. */
1915 static void
1916 read_rangelist (struct comp_unit *unit,
1917 bfd_uint64_t offset,
1918 read_rangelist_callback_t callback,
1919 void *callback_data)
1921 bfd_byte *ranges_ptr;
1922 bfd_vma base_address = unit->base_address;
1924 if (! unit->stash->dwarf_ranges_buffer)
1926 if (! read_debug_ranges (unit))
1927 return;
1929 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1931 for (;;)
1933 bfd_vma low_pc;
1934 bfd_vma high_pc;
1936 if (unit->addr_size == 4)
1938 low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1939 ranges_ptr += 4;
1940 high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1941 ranges_ptr += 4;
1943 else
1945 low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1946 ranges_ptr += 8;
1947 high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1948 ranges_ptr += 8;
1950 if (low_pc == 0 && high_pc == 0)
1951 break;
1952 if (low_pc == -1UL && high_pc != -1UL)
1953 base_address = high_pc;
1954 else
1955 /* Call callback to process new arange. */
1956 (callback) (unit, base_address + low_pc, base_address + high_pc,
1957 callback_data);
1961 /* DWARF2 Compilation unit functions. */
1963 /* Scan over each die in a comp. unit looking for functions to add
1964 to the function table and variables to the variable table. */
1966 static bfd_boolean
1967 scan_unit_for_symbols (struct comp_unit *unit)
1969 bfd *abfd = unit->abfd;
1970 bfd_byte *info_ptr = unit->first_child_die_ptr;
1971 int nesting_level = 1;
1972 struct funcinfo **nested_funcs;
1973 int nested_funcs_size;
1975 /* Maintain a stack of in-scope functions and inlined functions, which we
1976 can use to set the caller_func field. */
1977 nested_funcs_size = 32;
1978 nested_funcs = bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
1979 if (nested_funcs == NULL)
1980 return FALSE;
1981 nested_funcs[nesting_level] = 0;
1983 while (nesting_level)
1985 unsigned int abbrev_number, bytes_read, i;
1986 struct abbrev_info *abbrev;
1987 struct attribute attr;
1988 struct funcinfo *func;
1989 struct varinfo *var;
1990 bfd_vma low_pc = 0;
1991 bfd_vma high_pc = 0;
1993 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1994 info_ptr += bytes_read;
1996 if (! abbrev_number)
1998 nesting_level--;
1999 continue;
2002 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
2003 if (! abbrev)
2005 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
2006 abbrev_number);
2007 bfd_set_error (bfd_error_bad_value);
2008 free (nested_funcs);
2009 return FALSE;
2012 var = NULL;
2013 if (abbrev->tag == DW_TAG_subprogram
2014 || abbrev->tag == DW_TAG_entry_point
2015 || abbrev->tag == DW_TAG_inlined_subroutine)
2017 bfd_size_type amt = sizeof (struct funcinfo);
2018 func = bfd_zalloc (abfd, amt);
2019 func->tag = abbrev->tag;
2020 func->prev_func = unit->function_table;
2021 unit->function_table = func;
2022 BFD_ASSERT (!unit->cached);
2024 if (func->tag == DW_TAG_inlined_subroutine)
2025 for (i = nesting_level - 1; i >= 1; i--)
2026 if (nested_funcs[i])
2028 func->caller_func = nested_funcs[i];
2029 break;
2031 nested_funcs[nesting_level] = func;
2033 else
2035 func = NULL;
2036 if (abbrev->tag == DW_TAG_variable)
2038 bfd_size_type amt = sizeof (struct varinfo);
2039 var = bfd_zalloc (abfd, amt);
2040 var->tag = abbrev->tag;
2041 var->stack = 1;
2042 var->prev_var = unit->variable_table;
2043 unit->variable_table = var;
2044 BFD_ASSERT (!unit->cached);
2047 /* No inline function in scope at this nesting level. */
2048 nested_funcs[nesting_level] = 0;
2051 for (i = 0; i < abbrev->num_attrs; ++i)
2053 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2055 if (func)
2057 switch (attr.name)
2059 case DW_AT_call_file:
2060 func->caller_file = concat_filename (unit->line_table, attr.u.val);
2061 break;
2063 case DW_AT_call_line:
2064 func->caller_line = attr.u.val;
2065 break;
2067 case DW_AT_abstract_origin:
2068 func->name = find_abstract_instance_name (unit, attr.u.val);
2069 break;
2071 case DW_AT_name:
2072 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
2073 if (func->name == NULL)
2074 func->name = attr.u.str;
2075 break;
2077 case DW_AT_MIPS_linkage_name:
2078 func->name = attr.u.str;
2079 break;
2081 case DW_AT_low_pc:
2082 low_pc = attr.u.val;
2083 break;
2085 case DW_AT_high_pc:
2086 high_pc = attr.u.val;
2087 break;
2089 case DW_AT_ranges:
2090 read_rangelist (unit, attr.u.val,
2091 read_rangelist_insert_arange_list,
2092 & func->arange);
2093 break;
2095 case DW_AT_decl_file:
2096 func->file = concat_filename (unit->line_table,
2097 attr.u.val);
2098 break;
2100 case DW_AT_decl_line:
2101 func->line = attr.u.val;
2102 break;
2104 default:
2105 break;
2108 else if (var)
2110 switch (attr.name)
2112 case DW_AT_name:
2113 var->name = attr.u.str;
2114 break;
2116 case DW_AT_decl_file:
2117 var->file = concat_filename (unit->line_table,
2118 attr.u.val);
2119 break;
2121 case DW_AT_decl_line:
2122 var->line = attr.u.val;
2123 break;
2125 case DW_AT_external:
2126 if (attr.u.val != 0)
2127 var->stack = 0;
2128 break;
2130 case DW_AT_location:
2131 switch (attr.form)
2133 case DW_FORM_block:
2134 case DW_FORM_block1:
2135 case DW_FORM_block2:
2136 case DW_FORM_block4:
2137 if (*attr.u.blk->data == DW_OP_addr)
2139 var->stack = 0;
2141 /* Verify that DW_OP_addr is the only opcode in the
2142 location, in which case the block size will be 1
2143 plus the address size. */
2144 /* ??? For TLS variables, gcc can emit
2145 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
2146 which we don't handle here yet. */
2147 if (attr.u.blk->size == unit->addr_size + 1U)
2148 var->addr = bfd_get (unit->addr_size * 8,
2149 unit->abfd,
2150 attr.u.blk->data + 1);
2152 break;
2154 default:
2155 break;
2157 break;
2159 default:
2160 break;
2165 if (func && high_pc != 0)
2167 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
2170 if (abbrev->has_children)
2172 nesting_level++;
2174 if (nesting_level >= nested_funcs_size)
2176 struct funcinfo **tmp;
2178 nested_funcs_size *= 2;
2179 tmp = bfd_realloc (nested_funcs,
2180 (nested_funcs_size
2181 * sizeof (struct funcinfo *)));
2182 if (tmp == NULL)
2184 free (nested_funcs);
2185 return FALSE;
2187 nested_funcs = tmp;
2189 nested_funcs[nesting_level] = 0;
2193 free (nested_funcs);
2194 return TRUE;
2197 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
2198 includes the compilation unit header that proceeds the DIE's, but
2199 does not include the length field that precedes each compilation
2200 unit header. END_PTR points one past the end of this comp unit.
2201 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
2203 This routine does not read the whole compilation unit; only enough
2204 to get to the line number information for the compilation unit. */
2206 static struct comp_unit *
2207 parse_comp_unit (struct dwarf2_debug *stash,
2208 bfd_vma unit_length,
2209 bfd_byte *info_ptr_unit,
2210 unsigned int offset_size)
2212 struct comp_unit* unit;
2213 unsigned int version;
2214 bfd_uint64_t abbrev_offset = 0;
2215 unsigned int addr_size;
2216 struct abbrev_info** abbrevs;
2217 unsigned int abbrev_number, bytes_read, i;
2218 struct abbrev_info *abbrev;
2219 struct attribute attr;
2220 bfd_byte *info_ptr = stash->info_ptr;
2221 bfd_byte *end_ptr = info_ptr + unit_length;
2222 bfd_size_type amt;
2223 bfd_vma low_pc = 0;
2224 bfd_vma high_pc = 0;
2225 bfd *abfd = stash->bfd;
2227 version = read_2_bytes (abfd, info_ptr);
2228 info_ptr += 2;
2229 BFD_ASSERT (offset_size == 4 || offset_size == 8);
2230 if (offset_size == 4)
2231 abbrev_offset = read_4_bytes (abfd, info_ptr);
2232 else
2233 abbrev_offset = read_8_bytes (abfd, info_ptr);
2234 info_ptr += offset_size;
2235 addr_size = read_1_byte (abfd, info_ptr);
2236 info_ptr += 1;
2238 if (version != 2)
2240 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
2241 bfd_set_error (bfd_error_bad_value);
2242 return 0;
2245 if (addr_size > sizeof (bfd_vma))
2247 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
2248 addr_size,
2249 (unsigned int) sizeof (bfd_vma));
2250 bfd_set_error (bfd_error_bad_value);
2251 return 0;
2254 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
2256 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
2257 bfd_set_error (bfd_error_bad_value);
2258 return 0;
2261 /* Read the abbrevs for this compilation unit into a table. */
2262 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
2263 if (! abbrevs)
2264 return 0;
2266 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2267 info_ptr += bytes_read;
2268 if (! abbrev_number)
2270 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
2271 abbrev_number);
2272 bfd_set_error (bfd_error_bad_value);
2273 return 0;
2276 abbrev = lookup_abbrev (abbrev_number, abbrevs);
2277 if (! abbrev)
2279 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
2280 abbrev_number);
2281 bfd_set_error (bfd_error_bad_value);
2282 return 0;
2285 amt = sizeof (struct comp_unit);
2286 unit = bfd_zalloc (abfd, amt);
2287 unit->abfd = abfd;
2288 unit->addr_size = addr_size;
2289 unit->offset_size = offset_size;
2290 unit->abbrevs = abbrevs;
2291 unit->end_ptr = end_ptr;
2292 unit->stash = stash;
2293 unit->info_ptr_unit = info_ptr_unit;
2294 unit->arange_set = dwarf2_arange_set_new (abfd);
2296 for (i = 0; i < abbrev->num_attrs; ++i)
2298 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2300 /* Store the data if it is of an attribute we want to keep in a
2301 partial symbol table. */
2302 switch (attr.name)
2304 case DW_AT_stmt_list:
2305 unit->stmtlist = 1;
2306 unit->line_offset = attr.u.val;
2307 break;
2309 case DW_AT_name:
2310 unit->name = attr.u.str;
2311 break;
2313 case DW_AT_low_pc:
2314 low_pc = attr.u.val;
2315 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2316 this is the base address to use when reading location
2317 lists or range lists. */
2318 unit->base_address = low_pc;
2319 break;
2321 case DW_AT_high_pc:
2322 high_pc = attr.u.val;
2323 break;
2325 case DW_AT_ranges:
2326 read_rangelist (unit, attr.u.val,
2327 read_rangelist_comp_unit_arange_add, NULL);
2328 break;
2330 case DW_AT_comp_dir:
2332 char *comp_dir = attr.u.str;
2334 if (comp_dir)
2336 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2337 directory, get rid of it. */
2338 char *cp = strchr (comp_dir, ':');
2340 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2341 comp_dir = cp + 1;
2343 unit->comp_dir = comp_dir;
2344 break;
2347 default:
2348 break;
2352 if (high_pc != 0)
2353 dwarf2_comp_unit_arange_add (unit, low_pc, high_pc);
2355 unit->first_child_die_ptr = info_ptr;
2356 return unit;
2359 /* Return TRUE if UNIT may contain the address given by ADDR. When
2360 there are functions written entirely with inline asm statements, the
2361 range info in the compilation unit header may not be correct. We
2362 need to consult the line info table to see if a compilation unit
2363 really contains the given address. */
2365 static bfd_boolean
2366 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2368 return arange_set_lookup_address (unit->arange_set, addr, NULL, NULL, NULL);
2371 /* If UNIT contains ADDR, set the output parameters to the values for
2372 the line containing ADDR. The output parameters, FILENAME_PTR,
2373 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2374 to be filled in.
2376 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2377 FALSE otherwise. */
2379 static bfd_boolean
2380 comp_unit_find_nearest_line (struct comp_unit *unit,
2381 bfd_vma addr,
2382 const char **filename_ptr,
2383 const char **functionname_ptr,
2384 unsigned int *linenumber_ptr,
2385 struct dwarf2_debug *stash)
2387 bfd_boolean line_p;
2388 bfd_boolean func_p;
2389 struct funcinfo *function;
2391 if (unit->error)
2392 return FALSE;
2394 if (! unit->line_table)
2396 if (! unit->stmtlist)
2398 unit->error = 1;
2399 return FALSE;
2402 unit->line_table = decode_line_info (unit, stash);
2404 if (! unit->line_table)
2406 unit->error = 1;
2407 return FALSE;
2410 if (unit->first_child_die_ptr < unit->end_ptr
2411 && ! scan_unit_for_symbols (unit))
2413 unit->error = 1;
2414 return FALSE;
2418 function = NULL;
2419 func_p = lookup_address_in_function_table (unit, addr,
2420 &function, functionname_ptr);
2421 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2422 stash->inliner_chain = function;
2423 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2424 function, filename_ptr,
2425 linenumber_ptr);
2426 return line_p || func_p;
2429 /* Check to see if line info is already decoded in a comp_unit.
2430 If not, decode it. Returns TRUE if no errors were encountered;
2431 FALSE otherwise. */
2433 static bfd_boolean
2434 comp_unit_maybe_decode_line_info (struct comp_unit *unit,
2435 struct dwarf2_debug *stash)
2437 if (unit->error)
2438 return FALSE;
2440 if (! unit->line_table)
2442 if (! unit->stmtlist)
2444 unit->error = 1;
2445 return FALSE;
2448 unit->line_table = decode_line_info (unit, stash);
2450 if (! unit->line_table)
2452 unit->error = 1;
2453 return FALSE;
2456 if (unit->first_child_die_ptr < unit->end_ptr
2457 && ! scan_unit_for_symbols (unit))
2459 unit->error = 1;
2460 return FALSE;
2464 return TRUE;
2467 /* If UNIT contains SYM at ADDR, set the output parameters to the
2468 values for the line containing SYM. The output parameters,
2469 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2470 filled in.
2472 Return TRUE if UNIT contains SYM, and no errors were encountered;
2473 FALSE otherwise. */
2475 static bfd_boolean
2476 comp_unit_find_line (struct comp_unit *unit,
2477 asymbol *sym,
2478 bfd_vma addr,
2479 const char **filename_ptr,
2480 unsigned int *linenumber_ptr,
2481 struct dwarf2_debug *stash)
2483 if (!comp_unit_maybe_decode_line_info (unit, stash))
2484 return FALSE;
2486 if (sym->flags & BSF_FUNCTION)
2487 return lookup_symbol_in_function_table (unit, sym, addr,
2488 filename_ptr,
2489 linenumber_ptr);
2491 return lookup_symbol_in_variable_table (unit, sym, addr,
2492 filename_ptr,
2493 linenumber_ptr);
2496 static struct funcinfo *
2497 reverse_funcinfo_list (struct funcinfo *head)
2499 struct funcinfo *rhead;
2500 struct funcinfo *temp;
2502 for (rhead = NULL; head; head = temp)
2504 temp = head->prev_func;
2505 head->prev_func = rhead;
2506 rhead = head;
2508 return rhead;
2511 static struct varinfo *
2512 reverse_varinfo_list (struct varinfo *head)
2514 struct varinfo *rhead;
2515 struct varinfo *temp;
2517 for (rhead = NULL; head; head = temp)
2519 temp = head->prev_var;
2520 head->prev_var = rhead;
2521 rhead = head;
2523 return rhead;
2526 /* Extract all interesting funcinfos and varinfos of a compilation
2527 unit into hash tables for faster lookup. Returns TRUE if no
2528 errors were enountered; FALSE otherwise. */
2530 static bfd_boolean
2531 comp_unit_hash_info (struct dwarf2_debug *stash,
2532 struct comp_unit *unit,
2533 struct info_hash_table *funcinfo_hash_table,
2534 struct info_hash_table *varinfo_hash_table)
2536 struct funcinfo* each_func;
2537 struct varinfo* each_var;
2538 bfd_boolean okay = TRUE;
2540 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
2542 if (!comp_unit_maybe_decode_line_info (unit, stash))
2543 return FALSE;
2545 BFD_ASSERT (!unit->cached);
2547 /* To preserve the original search order, we went to visit the function
2548 infos in the reversed order of the list. However, making the list
2549 bi-directional use quite a bit of extra memory. So we reverse
2550 the list first, traverse the list in the now reversed order and
2551 finally reverse the list again to get back the original order. */
2552 unit->function_table = reverse_funcinfo_list (unit->function_table);
2553 for (each_func = unit->function_table;
2554 each_func && okay;
2555 each_func = each_func->prev_func)
2557 /* Skip nameless functions. */
2558 if (each_func->name)
2559 /* There is no need to copy name string into hash table as
2560 name string is either in the dwarf string buffer or
2561 info in the stash. */
2562 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
2563 (void*) each_func, FALSE);
2565 unit->function_table = reverse_funcinfo_list (unit->function_table);
2566 if (!okay)
2567 return FALSE;
2569 /* We do the same for variable infos. */
2570 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2571 for (each_var = unit->variable_table;
2572 each_var && okay;
2573 each_var = each_var->prev_var)
2575 /* Skip stack vars and vars with no files or names. */
2576 if (each_var->stack == 0
2577 && each_var->file != NULL
2578 && each_var->name != NULL)
2579 /* There is no need to copy name string into hash table as
2580 name string is either in the dwarf string buffer or
2581 info in the stash. */
2582 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
2583 (void*) each_var, FALSE);
2586 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2587 unit->cached = TRUE;
2588 return okay;
2591 /* Locate a section in a BFD containing debugging info. The search starts
2592 from the section after AFTER_SEC, or from the first section in the BFD if
2593 AFTER_SEC is NULL. The search works by examining the names of the
2594 sections. There are two permissiable names. The first is .debug_info.
2595 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2596 This is a variation on the .debug_info section which has a checksum
2597 describing the contents appended onto the name. This allows the linker to
2598 identify and discard duplicate debugging sections for different
2599 compilation units. */
2600 #define DWARF2_DEBUG_INFO ".debug_info"
2601 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2603 static asection *
2604 find_debug_info (bfd *abfd, asection *after_sec)
2606 asection * msec;
2608 msec = after_sec != NULL ? after_sec->next : abfd->sections;
2610 while (msec)
2612 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2613 return msec;
2615 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
2616 return msec;
2618 msec = msec->next;
2621 return NULL;
2624 /* Unset vmas for loadable sections in STASH. */
2626 static void
2627 unset_sections (struct dwarf2_debug *stash)
2629 unsigned int i;
2630 struct loadable_section *p;
2632 i = stash->loadable_section_count;
2633 p = stash->loadable_sections;
2634 for (; i > 0; i--, p++)
2635 p->section->vma = 0;
2638 /* Set unique vmas for loadable sections in ABFD and save vmas in
2639 STASH for unset_sections. */
2641 static bfd_boolean
2642 place_sections (bfd *abfd, struct dwarf2_debug *stash)
2644 struct loadable_section *p;
2645 unsigned int i;
2647 if (stash->loadable_section_count != 0)
2649 i = stash->loadable_section_count;
2650 p = stash->loadable_sections;
2651 for (; i > 0; i--, p++)
2652 p->section->vma = p->adj_vma;
2654 else
2656 asection *sect;
2657 bfd_vma last_vma = 0;
2658 bfd_size_type amt;
2659 struct loadable_section *p;
2661 i = 0;
2662 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2664 bfd_size_type sz;
2666 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2667 continue;
2669 sz = sect->rawsize ? sect->rawsize : sect->size;
2670 if (sz == 0)
2671 continue;
2673 i++;
2676 amt = i * sizeof (struct loadable_section);
2677 p = (struct loadable_section *) bfd_zalloc (abfd, amt);
2678 if (! p)
2679 return FALSE;
2681 stash->loadable_sections = p;
2682 stash->loadable_section_count = i;
2684 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2686 bfd_size_type sz;
2688 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2689 continue;
2691 sz = sect->rawsize ? sect->rawsize : sect->size;
2692 if (sz == 0)
2693 continue;
2695 p->section = sect;
2696 if (last_vma != 0)
2698 /* Align the new address to the current section
2699 alignment. */
2700 last_vma = ((last_vma
2701 + ~((bfd_vma) -1 << sect->alignment_power))
2702 & ((bfd_vma) -1 << sect->alignment_power));
2703 sect->vma = last_vma;
2705 p->adj_vma = sect->vma;
2706 last_vma += sect->vma + sz;
2708 p++;
2712 return TRUE;
2715 /* Look up a funcinfo by name using the given info hash table. If found,
2716 also update the locations pointed to by filename_ptr and linenumber_ptr.
2718 This function returns TRUE if a funcinfo that matches the given symbol
2719 and address is found with any error; otherwise it returns FALSE. */
2721 static bfd_boolean
2722 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
2723 asymbol *sym,
2724 bfd_vma addr,
2725 const char **filename_ptr,
2726 unsigned int *linenumber_ptr)
2728 struct funcinfo* each_func;
2729 struct funcinfo* best_fit = NULL;
2730 struct info_list_node *node;
2731 struct arange *arange;
2732 const char *name = bfd_asymbol_name (sym);
2733 asection *sec = bfd_get_section (sym);
2735 for (node = lookup_info_hash_table (hash_table, name);
2736 node;
2737 node = node->next)
2739 each_func = node->info;
2740 for (arange = &each_func->arange;
2741 arange;
2742 arange = arange->next)
2744 if ((!each_func->sec || each_func->sec == sec)
2745 && addr >= arange->low
2746 && addr < arange->high
2747 && (!best_fit
2748 || ((arange->high - arange->low)
2749 < (best_fit->arange.high - best_fit->arange.low))))
2750 best_fit = each_func;
2754 if (best_fit)
2756 best_fit->sec = sec;
2757 *filename_ptr = best_fit->file;
2758 *linenumber_ptr = best_fit->line;
2759 return TRUE;
2762 return FALSE;
2765 /* Look up a varinfo by name using the given info hash table. If found,
2766 also update the locations pointed to by filename_ptr and linenumber_ptr.
2768 This function returns TRUE if a varinfo that matches the given symbol
2769 and address is found with any error; otherwise it returns FALSE. */
2771 static bfd_boolean
2772 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
2773 asymbol *sym,
2774 bfd_vma addr,
2775 const char **filename_ptr,
2776 unsigned int *linenumber_ptr)
2778 const char *name = bfd_asymbol_name (sym);
2779 asection *sec = bfd_get_section (sym);
2780 struct varinfo* each;
2781 struct info_list_node *node;
2783 for (node = lookup_info_hash_table (hash_table, name);
2784 node;
2785 node = node->next)
2787 each = node->info;
2788 if (each->addr == addr
2789 && (!each->sec || each->sec == sec))
2791 each->sec = sec;
2792 *filename_ptr = each->file;
2793 *linenumber_ptr = each->line;
2794 return TRUE;
2798 return FALSE;
2801 /* Update the funcinfo and varinfo info hash tables if they are
2802 not up to date. Returns TRUE if there is no error; otherwise
2803 returns FALSE and disable the info hash tables. */
2805 static bfd_boolean
2806 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
2808 struct comp_unit *each;
2810 /* Exit if hash tables are up-to-date. */
2811 if (stash->all_comp_units == stash->hash_units_head)
2812 return TRUE;
2814 if (stash->hash_units_head)
2815 each = stash->hash_units_head->prev_unit;
2816 else
2817 each = stash->last_comp_unit;
2819 while (each)
2821 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
2822 stash->varinfo_hash_table))
2824 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2825 return FALSE;
2827 each = each->prev_unit;
2830 stash->hash_units_head = stash->all_comp_units;
2831 return TRUE;
2834 /* Check consistency of info hash tables. This is for debugging only. */
2836 static void ATTRIBUTE_UNUSED
2837 stash_verify_info_hash_table (struct dwarf2_debug *stash)
2839 struct comp_unit *each_unit;
2840 struct funcinfo *each_func;
2841 struct varinfo *each_var;
2842 struct info_list_node *node;
2843 bfd_boolean found;
2845 for (each_unit = stash->all_comp_units;
2846 each_unit;
2847 each_unit = each_unit->next_unit)
2849 for (each_func = each_unit->function_table;
2850 each_func;
2851 each_func = each_func->prev_func)
2853 if (!each_func->name)
2854 continue;
2855 node = lookup_info_hash_table (stash->funcinfo_hash_table,
2856 each_func->name);
2857 BFD_ASSERT (node);
2858 found = FALSE;
2859 while (node && !found)
2861 found = node->info == each_func;
2862 node = node->next;
2864 BFD_ASSERT (found);
2867 for (each_var = each_unit->variable_table;
2868 each_var;
2869 each_var = each_var->prev_var)
2871 if (!each_var->name || !each_var->file || each_var->stack)
2872 continue;
2873 node = lookup_info_hash_table (stash->varinfo_hash_table,
2874 each_var->name);
2875 BFD_ASSERT (node);
2876 found = FALSE;
2877 while (node && !found)
2879 found = node->info == each_var;
2880 node = node->next;
2882 BFD_ASSERT (found);
2887 /* Check to see if we want to enable the info hash tables, which consume
2888 quite a bit of memory. Currently we only check the number times
2889 bfd_dwarf2_find_line is called. In the future, we may also want to
2890 take the number of symbols into account. */
2892 static void
2893 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
2895 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
2897 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
2898 return;
2900 /* FIXME: Maybe we should check the reduce_memory_overheads
2901 and optimize fields in the bfd_link_info structure ? */
2903 /* Create hash tables. */
2904 stash->funcinfo_hash_table = create_info_hash_table (abfd);
2905 stash->varinfo_hash_table = create_info_hash_table (abfd);
2906 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
2908 /* Turn off info hashes if any allocation above fails. */
2909 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2910 return;
2912 /* We need a forced update so that the info hash tables will
2913 be created even though there is no compilation unit. That
2914 happens if STASH_INFO_HASH_TRIGGER is 0. */
2915 stash_maybe_update_info_hash_tables (stash);
2916 stash->info_hash_status = STASH_INFO_HASH_ON;
2919 /* Find the file and line associated with a symbol and address using the
2920 info hash tables of a stash. If there is a match, the function returns
2921 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
2922 otherwise it returns FALSE. */
2924 static bfd_boolean
2925 stash_find_line_fast (struct dwarf2_debug *stash,
2926 asymbol *sym,
2927 bfd_vma addr,
2928 const char **filename_ptr,
2929 unsigned int *linenumber_ptr)
2931 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
2933 if (sym->flags & BSF_FUNCTION)
2934 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
2935 filename_ptr, linenumber_ptr);
2936 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
2937 filename_ptr, linenumber_ptr);
2940 typedef struct
2942 struct dwarf2_debug * stash;
2943 arange_set set;
2944 struct comp_unit * unit;
2945 } stash_copy_local_aranges_data_t;
2947 static int
2948 stash_copy_local_aranges (bfd_vma low,
2949 bfd_vma high,
2950 arange_value_type data ATTRIBUTE_UNUSED,
2951 void *info)
2953 bfd_boolean status;
2955 stash_copy_local_aranges_data_t *copy_data = info;
2956 status = arange_set_insert (copy_data->set, low, high,
2957 (arange_value_type) copy_data->unit);
2959 return status ? 0 : 1;
2962 static bfd_boolean
2963 stash_maybe_enable_arange_set (bfd *abfd, struct dwarf2_debug *stash)
2965 struct comp_unit *unit;
2966 stash_copy_local_aranges_data_t copy_data;
2968 if (stash->arange_set_status != STASH_ARANGE_SET_OFF)
2969 return TRUE;
2971 if (stash->comp_unit_count < STASH_ARANGE_SET_TRIGGER)
2972 return TRUE;
2974 if (stash->comp_unit_arange_set == NULL)
2976 stash->comp_unit_arange_set =
2977 dwarf2_arange_set_with_value_new (abfd);
2978 if (!stash->comp_unit_arange_set)
2980 stash->arange_set_status = STASH_ARANGE_SET_DISABLED;
2981 return FALSE;
2985 copy_data.stash = stash;
2986 copy_data.set = stash->comp_unit_arange_set;
2987 for (unit = stash->all_comp_units; unit; unit = unit->next_unit)
2989 copy_data.unit = unit;
2990 if (arange_set_foreach (unit->arange_set, stash_copy_local_aranges,
2991 & copy_data))
2993 stash->arange_set_status = STASH_ARANGE_SET_DISABLED;
2994 return FALSE;
2997 stash->arange_set_status = STASH_ARANGE_SET_ON;
2998 return TRUE;
3001 /* Find the nearest line to a given address and record filename,
3002 function name and line number if found. Return TRUE if a line is
3003 found or FALSE otherwise. */
3005 static bfd_boolean ATTRIBUTE_UNUSED
3006 stash_find_nearest_line_fast (struct dwarf2_debug *stash,
3007 bfd_vma addr,
3008 const char **filename_ptr,
3009 const char **functionname_ptr,
3010 unsigned int *linenumber_ptr)
3012 arange_value_type value;
3013 struct comp_unit *unit;
3015 /* Try looking up global arange set first. */
3016 if (stash->arange_set_status == STASH_ARANGE_SET_ON
3017 && arange_set_lookup_address (stash->comp_unit_arange_set, addr, NULL,
3018 NULL, &value))
3020 if ((unit = (struct comp_unit *) value) != NULL)
3021 /* There is only one compilation unit containing this address. */
3022 return comp_unit_find_nearest_line (unit, addr, filename_ptr,
3023 functionname_ptr, linenumber_ptr,
3024 stash);
3027 /* The arange set is not available or there are multiple compilation
3028 units containing this address. Search all compilation units. */
3029 for (unit = stash->all_comp_units; unit; unit = unit->next_unit)
3031 if (comp_unit_contains_address (unit, addr)
3032 && comp_unit_find_nearest_line (unit, addr, filename_ptr,
3033 functionname_ptr,
3034 linenumber_ptr, stash))
3035 return TRUE;
3038 return FALSE;
3041 /* Find the source code location of SYMBOL. If SYMBOL is NULL
3042 then find the nearest source code location corresponding to
3043 the address SECTION + OFFSET.
3044 Returns TRUE if the line is found without error and fills in
3045 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
3046 NULL the FUNCTIONNAME_PTR is also filled in.
3047 SYMBOLS contains the symbol table for ABFD.
3048 ADDR_SIZE is the number of bytes in the initial .debug_info length
3049 field and in the abbreviation offset, or zero to indicate that the
3050 default value should be used. */
3052 static bfd_boolean
3053 find_line (bfd *abfd,
3054 asection *section,
3055 bfd_vma offset,
3056 asymbol *symbol,
3057 asymbol **symbols,
3058 const char **filename_ptr,
3059 const char **functionname_ptr,
3060 unsigned int *linenumber_ptr,
3061 unsigned int addr_size,
3062 void **pinfo)
3064 /* Read each compilation unit from the section .debug_info, and check
3065 to see if it contains the address we are searching for. If yes,
3066 lookup the address, and return the line number info. If no, go
3067 on to the next compilation unit.
3069 We keep a list of all the previously read compilation units, and
3070 a pointer to the next un-read compilation unit. Check the
3071 previously read units before reading more. */
3072 struct dwarf2_debug *stash;
3073 /* What address are we looking for? */
3074 bfd_vma addr;
3075 struct comp_unit* each;
3076 bfd_vma found = FALSE;
3077 bfd_boolean do_line;
3079 stash = *pinfo;
3081 if (! stash)
3083 bfd_size_type amt = sizeof (struct dwarf2_debug);
3085 stash = bfd_zalloc (abfd, amt);
3086 if (! stash)
3087 return FALSE;
3090 /* In a relocatable file, 2 functions may have the same address.
3091 We change the section vma so that they won't overlap. */
3092 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3094 if (! place_sections (abfd, stash))
3095 return FALSE;
3098 do_line = (section == NULL
3099 && offset == 0
3100 && functionname_ptr == NULL
3101 && symbol != NULL);
3102 if (do_line)
3104 addr = symbol->value;
3105 section = bfd_get_section (symbol);
3107 else if (section != NULL
3108 && functionname_ptr != NULL
3109 && symbol == NULL)
3110 addr = offset;
3111 else
3112 abort ();
3114 if (section->output_section)
3115 addr += section->output_section->vma + section->output_offset;
3116 else
3117 addr += section->vma;
3118 *filename_ptr = NULL;
3119 if (! do_line)
3120 *functionname_ptr = NULL;
3121 *linenumber_ptr = 0;
3123 if (! *pinfo)
3125 bfd *debug_bfd;
3126 bfd_size_type total_size;
3127 asection *msec;
3129 *pinfo = stash;
3131 msec = find_debug_info (abfd, NULL);
3132 if (msec == NULL)
3134 char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
3136 if (debug_filename == NULL)
3137 /* No dwarf2 info, and no gnu_debuglink to follow.
3138 Note that at this point the stash has been allocated, but
3139 contains zeros. This lets future calls to this function
3140 fail more quickly. */
3141 goto done;
3143 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
3144 || ! bfd_check_format (debug_bfd, bfd_object)
3145 || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
3147 if (debug_bfd)
3148 bfd_close (debug_bfd);
3149 /* FIXME: Should we report our failure to follow the debuglink ? */
3150 free (debug_filename);
3151 goto done;
3154 else
3155 debug_bfd = abfd;
3157 /* There can be more than one DWARF2 info section in a BFD these days.
3158 Read them all in and produce one large stash. We do this in two
3159 passes - in the first pass we just accumulate the section sizes.
3160 In the second pass we read in the section's contents. The allows
3161 us to avoid reallocing the data as we add sections to the stash. */
3162 for (total_size = 0; msec; msec = find_debug_info (debug_bfd, msec))
3163 total_size += msec->size;
3165 stash->info_ptr = bfd_alloc (debug_bfd, total_size);
3166 if (stash->info_ptr == NULL)
3167 goto done;
3169 stash->info_ptr_end = stash->info_ptr;
3171 for (msec = find_debug_info (debug_bfd, NULL);
3172 msec;
3173 msec = find_debug_info (debug_bfd, msec))
3175 bfd_size_type size;
3176 bfd_size_type start;
3178 size = msec->size;
3179 if (size == 0)
3180 continue;
3182 start = stash->info_ptr_end - stash->info_ptr;
3184 if ((bfd_simple_get_relocated_section_contents
3185 (debug_bfd, msec, stash->info_ptr + start, symbols)) == NULL)
3186 continue;
3188 stash->info_ptr_end = stash->info_ptr + start + size;
3191 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
3193 stash->sec = find_debug_info (debug_bfd, NULL);
3194 stash->sec_info_ptr = stash->info_ptr;
3195 stash->syms = symbols;
3196 stash->bfd = debug_bfd;
3199 /* A null info_ptr indicates that there is no dwarf2 info
3200 (or that an error occured while setting up the stash). */
3201 if (! stash->info_ptr)
3202 goto done;
3204 stash->inliner_chain = NULL;
3206 /* Check the previously read comp. units first. */
3207 if (do_line)
3209 /* The info hash tables use quite a bit of memory. We may not want to
3210 always use them. We use some heuristics to decide if and when to
3211 turn it on. */
3212 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
3213 stash_maybe_enable_info_hash_tables (abfd, stash);
3215 /* Keep info hash table up to date if they are available. Note that we
3216 may disable the hash tables if there is any error duing update. */
3217 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3218 stash_maybe_update_info_hash_tables (stash);
3220 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3222 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
3223 linenumber_ptr);
3224 if (found)
3225 goto done;
3227 else
3229 /* Check the previously read comp. units first. */
3230 for (each = stash->all_comp_units; each; each = each->next_unit)
3231 if ((symbol->flags & BSF_FUNCTION) == 0
3232 || comp_unit_contains_address (each, addr))
3234 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
3235 linenumber_ptr, stash);
3236 if (found)
3237 goto done;
3241 else
3243 if (stash->arange_set_status == STASH_ARANGE_SET_OFF)
3244 stash_maybe_enable_arange_set (abfd, stash);
3246 found = stash_find_nearest_line_fast (stash, addr, filename_ptr,
3247 functionname_ptr, linenumber_ptr);
3248 if (found)
3249 goto done;
3252 /* The DWARF2 spec says that the initial length field, and the
3253 offset of the abbreviation table, should both be 4-byte values.
3254 However, some compilers do things differently. */
3255 if (addr_size == 0)
3256 addr_size = 4;
3257 BFD_ASSERT (addr_size == 4 || addr_size == 8);
3259 /* Read each remaining comp. units checking each as they are read. */
3260 while (stash->info_ptr < stash->info_ptr_end)
3262 bfd_vma length;
3263 unsigned int offset_size = addr_size;
3264 bfd_byte *info_ptr_unit = stash->info_ptr;
3266 length = read_4_bytes (stash->bfd, stash->info_ptr);
3267 /* A 0xffffff length is the DWARF3 way of indicating
3268 we use 64-bit offsets, instead of 32-bit offsets. */
3269 if (length == 0xffffffff)
3271 offset_size = 8;
3272 length = read_8_bytes (stash->bfd, stash->info_ptr + 4);
3273 stash->info_ptr += 12;
3275 /* A zero length is the IRIX way of indicating 64-bit offsets,
3276 mostly because the 64-bit length will generally fit in 32
3277 bits, and the endianness helps. */
3278 else if (length == 0)
3280 offset_size = 8;
3281 length = read_4_bytes (stash->bfd, stash->info_ptr + 4);
3282 stash->info_ptr += 8;
3284 /* In the absence of the hints above, we assume 32-bit DWARF2
3285 offsets even for targets with 64-bit addresses, because:
3286 a) most of the time these targets will not have generated
3287 more than 2Gb of debug info and so will not need 64-bit
3288 offsets,
3290 b) if they do use 64-bit offsets but they are not using
3291 the size hints that are tested for above then they are
3292 not conforming to the DWARF3 standard anyway. */
3293 else if (addr_size == 8)
3295 offset_size = 4;
3296 stash->info_ptr += 4;
3298 else
3299 stash->info_ptr += 4;
3301 if (length > 0)
3303 each = parse_comp_unit (stash, length, info_ptr_unit,
3304 offset_size);
3305 stash->info_ptr += length;
3307 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
3308 == stash->sec->size)
3310 stash->sec = find_debug_info (stash->bfd, stash->sec);
3311 stash->sec_info_ptr = stash->info_ptr;
3314 if (each)
3316 if (stash->all_comp_units)
3317 stash->all_comp_units->prev_unit = each;
3318 else
3319 stash->last_comp_unit = each;
3321 each->next_unit = stash->all_comp_units;
3322 stash->all_comp_units = each;
3323 stash->comp_unit_count++;
3325 /* DW_AT_low_pc and DW_AT_high_pc are optional for
3326 compilation units. If we don't have them, we need to
3327 consult the line info table to see if a compilation unit
3328 contains the given address. */
3329 if (do_line)
3330 found = (((symbol->flags & BSF_FUNCTION) == 0
3331 || arange_set_empty_p (each->arange_set)
3332 || comp_unit_contains_address (each, addr))
3333 && comp_unit_find_line (each, symbol, addr,
3334 filename_ptr,
3335 linenumber_ptr,
3336 stash));
3337 else
3338 found = ((arange_set_empty_p (each->arange_set)
3339 || comp_unit_contains_address (each, addr))
3340 && comp_unit_find_nearest_line (each, addr,
3341 filename_ptr,
3342 functionname_ptr,
3343 linenumber_ptr,
3344 stash));
3345 if (found)
3346 goto done;
3351 done:
3352 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3353 unset_sections (stash);
3355 return found;
3358 /* The DWARF2 version of find_nearest_line.
3359 Return TRUE if the line is found without error. */
3361 bfd_boolean
3362 _bfd_dwarf2_find_nearest_line (bfd *abfd,
3363 asection *section,
3364 asymbol **symbols,
3365 bfd_vma offset,
3366 const char **filename_ptr,
3367 const char **functionname_ptr,
3368 unsigned int *linenumber_ptr,
3369 unsigned int addr_size,
3370 void **pinfo)
3372 return find_line (abfd, section, offset, NULL, symbols, filename_ptr,
3373 functionname_ptr, linenumber_ptr, addr_size,
3374 pinfo);
3377 /* The DWARF2 version of find_line.
3378 Return TRUE if the line is found without error. */
3380 bfd_boolean
3381 _bfd_dwarf2_find_line (bfd *abfd,
3382 asymbol **symbols,
3383 asymbol *symbol,
3384 const char **filename_ptr,
3385 unsigned int *linenumber_ptr,
3386 unsigned int addr_size,
3387 void **pinfo)
3389 return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr,
3390 NULL, linenumber_ptr, addr_size,
3391 pinfo);
3394 bfd_boolean
3395 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
3396 const char **filename_ptr,
3397 const char **functionname_ptr,
3398 unsigned int *linenumber_ptr,
3399 void **pinfo)
3401 struct dwarf2_debug *stash;
3403 stash = *pinfo;
3404 if (stash)
3406 struct funcinfo *func = stash->inliner_chain;
3408 if (func && func->caller_func)
3410 *filename_ptr = func->caller_file;
3411 *functionname_ptr = func->caller_func->name;
3412 *linenumber_ptr = func->caller_line;
3413 stash->inliner_chain = func->caller_func;
3414 return TRUE;
3418 return FALSE;
3421 void
3422 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
3424 struct comp_unit *each;
3425 struct dwarf2_debug *stash;
3427 if (abfd == NULL || elf_tdata (abfd) == NULL)
3428 return;
3430 stash = elf_tdata (abfd)->dwarf2_find_line_info;
3432 if (stash == NULL)
3433 return;
3435 for (each = stash->all_comp_units; each; each = each->next_unit)
3437 struct abbrev_info **abbrevs = each->abbrevs;
3438 size_t i;
3440 for (i = 0; i < ABBREV_HASH_SIZE; i++)
3442 struct abbrev_info *abbrev = abbrevs[i];
3444 while (abbrev)
3446 free (abbrev->attrs);
3447 abbrev = abbrev->next;
3451 if (each->line_table)
3453 free (each->line_table->dirs);
3454 free (each->line_table->files);
3458 free (stash->dwarf_abbrev_buffer);
3459 free (stash->dwarf_line_buffer);
3460 free (stash->dwarf_ranges_buffer);