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
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. */
35 #include "libiberty.h"
38 #include "elf/dwarf2.h"
39 #include "arange-set.h"
41 /* The data in the .debug_line statement prologue looks like this. */
46 unsigned short version;
47 bfd_vma prologue_length;
48 unsigned char minimum_instruction_length;
49 unsigned char default_is_stmt;
51 unsigned char line_range;
52 unsigned char opcode_base;
53 unsigned char *standard_opcode_lengths;
56 /* Attributes have a name and a value. */
60 enum dwarf_attribute name;
65 struct dwarf_block *blk;
72 /* Blocks are a bunch of untyped bytes. */
79 struct loadable_section
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. */
96 /* The next unread compilation unit within the .debug_info section.
97 Zero indicates that the .debug_info section has not been loaded
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. */
109 bfd_byte *sec_info_ptr;
111 /* Pointer to the symbol table. */
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
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. */
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. */
195 /* The lowest and highest addresses contained a compilation
196 unit as specified in the compilation unit's header. */
201 /* A minimal decoding of DWARF2 compilation units. We only decode
202 what's needed to get to the line number information. */
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). */
216 /* The set of aranges in a compilation unit. */
217 arange_set arange_set;
219 /* The DW_AT_name attribute (for error messages). */
222 /* The abbrev hash table. */
223 struct abbrev_info **abbrevs;
225 /* Note that an error was found by comp_unit_find_nearest_line. */
228 /* The DW_AT_comp_dir attribute. */
231 /* TRUE if there is a line number table associated with this comp. unit. */
234 /* Pointer to the current comp_unit so that we can find a given entry
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. */
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. */
273 /* This data structure holds the information of an abbrev. */
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. */
286 enum dwarf_attribute name;
287 enum dwarf_form form;
290 #ifndef ABBREV_HASH_SIZE
291 #define ABBREV_HASH_SIZE 121
293 #ifndef ATTR_ALLOC_CHUNK
294 #define ATTR_ALLOC_CHUNK 4
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;
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,
329 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
331 /* Allocate the structure if it has not already been allocated by a
335 ret = bfd_hash_allocate (table, sizeof (* ret));
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. */
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));
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);
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. */
380 insert_info_hash_table (struct info_hash_table *hash_table,
385 struct info_hash_entry *entry;
386 struct info_list_node *node;
388 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
393 node = bfd_hash_allocate (&hash_table->base, sizeof (*node));
398 node->next = entry->head;
404 /* Look up an info entry list from an info hash table. Return NULL
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,
414 return entry ? entry->head : NULL;
418 The following function up to the END VERBATIM mark are
419 copied directly from dwarf2read.c. */
421 /* Read dwarf information from a buffer. */
424 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
426 return bfd_get_8 (abfd, buf);
430 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
432 return bfd_get_signed_8 (abfd, buf);
436 read_2_bytes (bfd *abfd, bfd_byte *buf)
438 return bfd_get_16 (abfd, buf);
442 read_4_bytes (bfd *abfd, bfd_byte *buf)
444 return bfd_get_32 (abfd, buf);
448 read_8_bytes (bfd *abfd, bfd_byte *buf)
450 return bfd_get_64 (abfd, buf);
454 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
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. */
465 read_string (bfd *abfd ATTRIBUTE_UNUSED,
467 unsigned int *bytes_read_ptr)
469 /* Return a pointer to the embedded string. */
470 char *str = (char *) buf;
477 *bytes_read_ptr = strlen (str) + 1;
482 read_indirect_string (struct comp_unit* unit,
484 unsigned int *bytes_read_ptr)
487 struct dwarf2_debug *stash = unit->stash;
490 if (unit->offset_size == 4)
491 offset = read_4_bytes (unit->abfd, buf);
493 offset = read_8_bytes (unit->abfd, buf);
494 *bytes_read_ptr = unit->offset_size;
496 if (! stash->dwarf_str_buffer)
499 bfd *abfd = unit->abfd;
502 msec = bfd_get_section_by_name (abfd, ".debug_str");
505 (*_bfd_error_handler)
506 (_("Dwarf Error: Can't find .debug_str section."));
507 bfd_set_error (bfd_error_bad_value);
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)
517 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
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);
530 str = (char *) stash->dwarf_str_buffer + offset;
539 read_address (struct comp_unit *unit, bfd_byte *buf)
541 int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
545 switch (unit->addr_size)
548 return bfd_get_signed_64 (unit->abfd, buf);
550 return bfd_get_signed_32 (unit->abfd, buf);
552 return bfd_get_signed_16 (unit->abfd, buf);
559 switch (unit->addr_size)
562 return bfd_get_64 (unit->abfd, buf);
564 return bfd_get_32 (unit->abfd, buf);
566 return bfd_get_16 (unit->abfd, buf);
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];
586 if (abbrev->number == number)
589 abbrev = abbrev->next;
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
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;
610 if (! stash->dwarf_abbrev_buffer)
614 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
617 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
618 bfd_set_error (bfd_error_bad_value);
622 stash->dwarf_abbrev_size = msec->size;
623 stash->dwarf_abbrev_buffer
624 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
626 if (! stash->dwarf_abbrev_buffer)
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);
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);
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;
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);
678 for (i = 0; i < ABBREV_HASH_SIZE; i++)
680 struct abbrev_info *abbrev = abbrevs[i];
684 free (abbrev->attrs);
685 abbrev = abbrev->next;
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
714 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
715 >= stash->dwarf_abbrev_size)
717 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
718 abbrev_ptr += bytes_read;
719 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
726 /* Read an attribute value described by an attribute form. */
729 read_attribute_value (struct attribute *attr,
731 struct comp_unit *unit,
734 bfd *abfd = unit->abfd;
735 unsigned int bytes_read;
736 struct dwarf_block *blk;
739 attr->form = (enum dwarf_form) form;
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;
750 amt = sizeof (struct dwarf_block);
751 blk = bfd_alloc (abfd, amt);
752 blk->size = read_2_bytes (abfd, info_ptr);
754 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
755 info_ptr += blk->size;
759 amt = sizeof (struct dwarf_block);
760 blk = bfd_alloc (abfd, amt);
761 blk->size = read_4_bytes (abfd, info_ptr);
763 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
764 info_ptr += blk->size;
768 attr->u.val = read_2_bytes (abfd, info_ptr);
772 attr->u.val = read_4_bytes (abfd, info_ptr);
776 attr->u.val = read_8_bytes (abfd, info_ptr);
780 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
781 info_ptr += bytes_read;
784 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
785 info_ptr += bytes_read;
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;
797 amt = sizeof (struct dwarf_block);
798 blk = bfd_alloc (abfd, amt);
799 blk->size = read_1_byte (abfd, info_ptr);
801 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
802 info_ptr += blk->size;
806 attr->u.val = read_1_byte (abfd, info_ptr);
810 attr->u.val = read_1_byte (abfd, info_ptr);
814 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
815 info_ptr += bytes_read;
818 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
819 info_ptr += bytes_read;
822 attr->u.val = read_1_byte (abfd, info_ptr);
826 attr->u.val = read_2_bytes (abfd, info_ptr);
830 attr->u.val = read_4_bytes (abfd, info_ptr);
834 attr->u.val = read_8_bytes (abfd, info_ptr);
837 case DW_FORM_ref_udata:
838 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
839 info_ptr += bytes_read;
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);
847 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
849 bfd_set_error (bfd_error_bad_value);
854 /* Read an attribute described by an abbreviated attribute. */
857 read_attribute (struct attribute *attr,
858 struct attr_abbrev *abbrev,
859 struct comp_unit *unit,
862 attr->name = abbrev->name;
863 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
867 /* Source line information table routines. */
869 #define FILE_ALLOC_CHUNK 5
870 #define DIR_ALLOC_CHUNK 5
874 struct line_info* prev_line;
879 int end_sequence; /* End of (sequential) code sequence. */
890 struct line_info_table
893 unsigned int num_files;
894 unsigned int num_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. */
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. */
917 struct arange arange;
918 asection *sec; /* Where the symbol is defined. */
923 /* Pointer to previous variable in list of all variables. */
924 struct varinfo *prev_var;
925 /* Source location file name. */
927 /* Source location line number. */
932 /* Where the symbol is defined. */
934 /* Is this a stack variable? */
935 unsigned int stack: 1;
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
956 /* Allocate memory for an arange set. */
959 dwarf2_arange_set_allocate (int size, void *data)
961 return bfd_alloc ((bfd *) data, size);
964 /* Deallocate memory of an arange set. */
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. */
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. */
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. */
1009 dwarf2_comp_unit_arange_add (struct comp_unit *unit,
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. */
1041 add_line_info (struct line_info_table *table,
1045 unsigned int column,
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;
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);
1061 strcpy (info->filename, filename);
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;
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;
1120 if (!new_line_sorts_after (info, li2)
1121 && new_line_sorts_after (info, li1))
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. */
1138 concat_filename (struct line_info_table *table, unsigned int file)
1142 if (file - 1 >= table->num_files)
1144 /* FILE == 0 means unknown. */
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;
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;
1168 dirname = subdirname;
1173 return strdup (filename);
1175 len = strlen (dirname) + strlen (filename) + 2;
1179 len += strlen (subdirname) + 1;
1180 name = bfd_malloc (len);
1182 sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
1186 name = bfd_malloc (len);
1188 sprintf (name, "%s/%s", dirname, filename);
1194 return strdup (filename);
1198 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_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;
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;
1220 if (high_pc == arange->low)
1222 arange->low = low_pc;
1225 arange = arange->next;
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;
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;
1253 if (! stash->dwarf_line_buffer)
1257 msec = bfd_get_section_by_name (abfd, ".debug_line");
1260 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
1261 bfd_set_error (bfd_error_bad_value);
1265 stash->dwarf_line_size = msec->size;
1266 stash->dwarf_line_buffer
1267 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1269 if (! stash->dwarf_line_buffer)
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);
1283 amt = sizeof (struct line_info_table);
1284 table = bfd_alloc (abfd, amt);
1286 table->comp_dir = unit->comp_dir;
1288 table->num_files = 0;
1289 table->files = NULL;
1291 table->num_dirs = 0;
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);
1304 if (lh.total_length == 0xffffffff)
1306 lh.total_length = read_8_bytes (abfd, line_ptr);
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);
1317 line_end = line_ptr + lh.total_length;
1318 lh.version = read_2_bytes (abfd, line_ptr);
1320 if (offset_size == 4)
1321 lh.prologue_length = read_4_bytes (abfd, line_ptr);
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);
1327 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1329 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1331 lh.line_range = read_1_byte (abfd, line_ptr);
1333 lh.opcode_base = read_1_byte (abfd, line_ptr);
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);
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)
1355 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1356 amt *= sizeof (char *);
1358 tmp = bfd_realloc (table->dirs, amt);
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);
1387 free (table->files);
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;
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);
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)
1444 if (address > high_pc)
1447 else switch (op_code)
1449 case DW_LNS_extended_op:
1450 /* Ignore length. */
1452 extended_op = read_1_byte (abfd, line_ptr);
1455 switch (extended_op)
1457 case DW_LNE_end_sequence:
1459 add_line_info (table, address, filename, line, column,
1461 if (address < low_pc)
1463 if (address > high_pc)
1465 dwarf2_comp_unit_arange_add (unit, low_pc, high_pc);
1467 case DW_LNE_set_address:
1468 address = read_address (unit, line_ptr);
1469 line_ptr += unit->addr_size;
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);
1483 free (table->files);
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;
1503 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1504 bfd_set_error (bfd_error_bad_value);
1506 free (table->files);
1512 add_line_info (table, address, filename, line, column, 0);
1513 if (address < low_pc)
1515 if (address > high_pc)
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;
1523 case DW_LNS_advance_line:
1524 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1525 line_ptr += bytes_read;
1527 case DW_LNS_set_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;
1537 filename = concat_filename (table, file);
1540 case DW_LNS_set_column:
1541 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1542 line_ptr += bytes_read;
1544 case DW_LNS_negate_stmt:
1545 is_stmt = (!is_stmt);
1547 case DW_LNS_set_basic_block:
1549 case DW_LNS_const_add_pc:
1550 address += lh.minimum_instruction_length
1551 * ((255 - lh.opcode_base) / lh.line_range);
1553 case DW_LNS_fixed_advance_pc:
1554 address += read_2_bytes (abfd, line_ptr);
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;
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. */
1583 lookup_address_in_line_info_table (struct line_info_table *table,
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;
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
1609 bfd_boolean addr_match = FALSE;
1610 if (each_line->address <= addr && addr < next_line->address)
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)
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;
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;
1643 *filename_ptr = each_line->filename;
1644 *linenumber_ptr = each_line->line;
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;
1677 /* Read in the .debug_ranges section for future reference */
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;
1688 msec = bfd_get_section_by_name (abfd, ".debug_ranges");
1691 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
1692 bfd_set_error (bfd_error_bad_value);
1696 stash->dwarf_ranges_size = msec->size;
1697 stash->dwarf_ranges_buffer
1698 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1700 if (! stash->dwarf_ranges_buffer)
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. */
1714 lookup_address_in_function_table (struct comp_unit *unit,
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;
1725 each_func = each_func->prev_func)
1727 for (arange = &each_func->arange;
1729 arange = arange->next)
1731 if (addr >= arange->low && addr < arange->high)
1734 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1735 best_fit = each_func;
1742 *functionname_ptr = best_fit->name;
1743 *function_ptr = best_fit;
1752 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1753 and LINENUMBER_PTR, and return TRUE. */
1756 lookup_symbol_in_function_table (struct comp_unit *unit,
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;
1770 each_func = each_func->prev_func)
1772 for (arange = &each_func->arange;
1774 arange = arange->next)
1776 if ((!each_func->sec || each_func->sec == sec)
1777 && addr >= arange->low
1778 && addr < arange->high
1780 && strcmp (name, each_func->name) == 0
1782 || ((arange->high - arange->low)
1783 < (best_fit->arange.high - best_fit->arange.low))))
1784 best_fit = each_func;
1790 best_fit->sec = sec;
1791 *filename_ptr = best_fit->file;
1792 *linenumber_ptr = best_fit->line;
1799 /* Variable table functions. */
1801 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1802 LINENUMBER_PTR, and return TRUE. */
1805 lookup_symbol_in_variable_table (struct comp_unit *unit,
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)
1827 *filename_ptr = each->file;
1828 *linenumber_ptr = each->line;
1836 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1838 bfd *abfd = unit->abfd;
1840 unsigned int abbrev_number, bytes_read, i;
1841 struct abbrev_info *abbrev;
1842 struct attribute attr;
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;
1851 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1854 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1856 bfd_set_error (bfd_error_bad_value);
1860 for (i = 0; i < abbrev->num_attrs; ++i)
1862 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1866 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1870 case DW_AT_specification:
1871 name = find_abstract_instance_name (unit, attr.u.val);
1873 case DW_AT_MIPS_linkage_name:
1885 /* Type of callback function used in read_rangelist below. */
1887 typedef void (*read_rangelist_callback_t)(struct comp_unit*, bfd_vma,
1890 /* Call back to add an arange to the old-style arange list. */
1893 read_rangelist_insert_arange_list (struct comp_unit *unit,
1898 arange_add (unit->abfd, (struct arange*) data, low, high);
1901 /* Callback to add an arange in the arange set of a compilation unit. */
1904 read_rangelist_comp_unit_arange_add (struct comp_unit *unit,
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. */
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))
1929 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1936 if (unit->addr_size == 4)
1938 low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1940 high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1945 low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1947 high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1950 if (low_pc == 0 && high_pc == 0)
1952 if (low_pc == -1UL && high_pc != -1UL)
1953 base_address = high_pc;
1955 /* Call callback to process new arange. */
1956 (callback) (unit, base_address + low_pc, base_address + high_pc,
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. */
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)
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;
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)
2002 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
2005 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
2007 bfd_set_error (bfd_error_bad_value);
2008 free (nested_funcs);
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];
2031 nested_funcs[nesting_level] = func;
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;
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);
2059 case DW_AT_call_file:
2060 func->caller_file = concat_filename (unit->line_table, attr.u.val);
2063 case DW_AT_call_line:
2064 func->caller_line = attr.u.val;
2067 case DW_AT_abstract_origin:
2068 func->name = find_abstract_instance_name (unit, attr.u.val);
2072 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
2073 if (func->name == NULL)
2074 func->name = attr.u.str;
2077 case DW_AT_MIPS_linkage_name:
2078 func->name = attr.u.str;
2082 low_pc = attr.u.val;
2086 high_pc = attr.u.val;
2090 read_rangelist (unit, attr.u.val,
2091 read_rangelist_insert_arange_list,
2095 case DW_AT_decl_file:
2096 func->file = concat_filename (unit->line_table,
2100 case DW_AT_decl_line:
2101 func->line = attr.u.val;
2113 var->name = attr.u.str;
2116 case DW_AT_decl_file:
2117 var->file = concat_filename (unit->line_table,
2121 case DW_AT_decl_line:
2122 var->line = attr.u.val;
2125 case DW_AT_external:
2126 if (attr.u.val != 0)
2130 case DW_AT_location:
2134 case DW_FORM_block1:
2135 case DW_FORM_block2:
2136 case DW_FORM_block4:
2137 if (*attr.u.blk->data == DW_OP_addr)
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,
2150 attr.u.blk->data + 1);
2165 if (func && high_pc != 0)
2167 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
2170 if (abbrev->has_children)
2174 if (nesting_level >= nested_funcs_size)
2176 struct funcinfo **tmp;
2178 nested_funcs_size *= 2;
2179 tmp = bfd_realloc (nested_funcs,
2181 * sizeof (struct funcinfo *)));
2184 free (nested_funcs);
2189 nested_funcs[nesting_level] = 0;
2193 free (nested_funcs);
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;
2224 bfd_vma high_pc = 0;
2225 bfd *abfd = stash->bfd;
2227 version = read_2_bytes (abfd, info_ptr);
2229 BFD_ASSERT (offset_size == 4 || offset_size == 8);
2230 if (offset_size == 4)
2231 abbrev_offset = read_4_bytes (abfd, info_ptr);
2233 abbrev_offset = read_8_bytes (abfd, info_ptr);
2234 info_ptr += offset_size;
2235 addr_size = read_1_byte (abfd, info_ptr);
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);
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'."),
2249 (unsigned int) sizeof (bfd_vma));
2250 bfd_set_error (bfd_error_bad_value);
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);
2261 /* Read the abbrevs for this compilation unit into a table. */
2262 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
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."),
2272 bfd_set_error (bfd_error_bad_value);
2276 abbrev = lookup_abbrev (abbrev_number, abbrevs);
2279 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
2281 bfd_set_error (bfd_error_bad_value);
2285 amt = sizeof (struct comp_unit);
2286 unit = bfd_zalloc (abfd, amt);
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. */
2304 case DW_AT_stmt_list:
2306 unit->line_offset = attr.u.val;
2310 unit->name = attr.u.str;
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;
2322 high_pc = attr.u.val;
2326 read_rangelist (unit, attr.u.val,
2327 read_rangelist_comp_unit_arange_add, NULL);
2330 case DW_AT_comp_dir:
2332 char *comp_dir = attr.u.str;
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] == '/')
2343 unit->comp_dir = comp_dir;
2353 dwarf2_comp_unit_arange_add (unit, low_pc, high_pc);
2355 unit->first_child_die_ptr = info_ptr;
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. */
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
2376 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2380 comp_unit_find_nearest_line (struct comp_unit *unit,
2382 const char **filename_ptr,
2383 const char **functionname_ptr,
2384 unsigned int *linenumber_ptr,
2385 struct dwarf2_debug *stash)
2389 struct funcinfo *function;
2394 if (! unit->line_table)
2396 if (! unit->stmtlist)
2402 unit->line_table = decode_line_info (unit, stash);
2404 if (! unit->line_table)
2410 if (unit->first_child_die_ptr < unit->end_ptr
2411 && ! scan_unit_for_symbols (unit))
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,
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;
2434 comp_unit_maybe_decode_line_info (struct comp_unit *unit,
2435 struct dwarf2_debug *stash)
2440 if (! unit->line_table)
2442 if (! unit->stmtlist)
2448 unit->line_table = decode_line_info (unit, stash);
2450 if (! unit->line_table)
2456 if (unit->first_child_die_ptr < unit->end_ptr
2457 && ! scan_unit_for_symbols (unit))
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
2472 Return TRUE if UNIT contains SYM, and no errors were encountered;
2476 comp_unit_find_line (struct comp_unit *unit,
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))
2486 if (sym->flags & BSF_FUNCTION)
2487 return lookup_symbol_in_function_table (unit, sym, addr,
2491 return lookup_symbol_in_variable_table (unit, sym, addr,
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;
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;
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. */
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))
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;
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);
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;
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;
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."
2604 find_debug_info (bfd *abfd, asection *after_sec)
2608 msec = after_sec != NULL ? after_sec->next : abfd->sections;
2612 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2615 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
2624 /* Unset vmas for loadable sections in STASH. */
2627 unset_sections (struct dwarf2_debug *stash)
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. */
2642 place_sections (bfd *abfd, struct dwarf2_debug *stash)
2644 struct loadable_section *p;
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;
2657 bfd_vma last_vma = 0;
2659 struct loadable_section *p;
2662 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2666 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2669 sz = sect->rawsize ? sect->rawsize : sect->size;
2676 amt = i * sizeof (struct loadable_section);
2677 p = (struct loadable_section *) bfd_zalloc (abfd, amt);
2681 stash->loadable_sections = p;
2682 stash->loadable_section_count = i;
2684 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2688 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2691 sz = sect->rawsize ? sect->rawsize : sect->size;
2698 /* Align the new address to the current section
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;
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. */
2722 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
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);
2739 each_func = node->info;
2740 for (arange = &each_func->arange;
2742 arange = arange->next)
2744 if ((!each_func->sec || each_func->sec == sec)
2745 && addr >= arange->low
2746 && addr < arange->high
2748 || ((arange->high - arange->low)
2749 < (best_fit->arange.high - best_fit->arange.low))))
2750 best_fit = each_func;
2756 best_fit->sec = sec;
2757 *filename_ptr = best_fit->file;
2758 *linenumber_ptr = best_fit->line;
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. */
2772 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
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);
2788 if (each->addr == addr
2789 && (!each->sec || each->sec == sec))
2792 *filename_ptr = each->file;
2793 *linenumber_ptr = each->line;
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. */
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)
2814 if (stash->hash_units_head)
2815 each = stash->hash_units_head->prev_unit;
2817 each = stash->last_comp_unit;
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;
2827 each = each->prev_unit;
2830 stash->hash_units_head = stash->all_comp_units;
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;
2845 for (each_unit = stash->all_comp_units;
2847 each_unit = each_unit->next_unit)
2849 for (each_func = each_unit->function_table;
2851 each_func = each_func->prev_func)
2853 if (!each_func->name)
2855 node = lookup_info_hash_table (stash->funcinfo_hash_table,
2859 while (node && !found)
2861 found = node->info == each_func;
2867 for (each_var = each_unit->variable_table;
2869 each_var = each_var->prev_var)
2871 if (!each_var->name || !each_var->file || each_var->stack)
2873 node = lookup_info_hash_table (stash->varinfo_hash_table,
2877 while (node && !found)
2879 found = node->info == each_var;
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. */
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)
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;
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. */
2925 stash_find_line_fast (struct dwarf2_debug *stash,
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);
2942 struct dwarf2_debug * stash;
2944 struct comp_unit * unit;
2945 } stash_copy_local_aranges_data_t;
2948 stash_copy_local_aranges (bfd_vma low,
2950 arange_value_type data ATTRIBUTE_UNUSED,
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;
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)
2971 if (stash->comp_unit_count < STASH_ARANGE_SET_TRIGGER)
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;
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,
2993 stash->arange_set_status = STASH_ARANGE_SET_DISABLED;
2997 stash->arange_set_status = STASH_ARANGE_SET_ON;
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,
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,
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,
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,
3034 linenumber_ptr, stash))
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. */
3053 find_line (bfd *abfd,
3058 const char **filename_ptr,
3059 const char **functionname_ptr,
3060 unsigned int *linenumber_ptr,
3061 unsigned int addr_size,
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? */
3075 struct comp_unit* each;
3076 bfd_vma found = FALSE;
3077 bfd_boolean do_line;
3083 bfd_size_type amt = sizeof (struct dwarf2_debug);
3085 stash = bfd_zalloc (abfd, amt);
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))
3098 do_line = (section == NULL
3100 && functionname_ptr == NULL
3104 addr = symbol->value;
3105 section = bfd_get_section (symbol);
3107 else if (section != NULL
3108 && functionname_ptr != NULL
3114 if (section->output_section)
3115 addr += section->output_section->vma + section->output_offset;
3117 addr += section->vma;
3118 *filename_ptr = NULL;
3120 *functionname_ptr = NULL;
3121 *linenumber_ptr = 0;
3126 bfd_size_type total_size;
3131 msec = find_debug_info (abfd, 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. */
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)
3148 bfd_close (debug_bfd);
3149 /* FIXME: Should we report our failure to follow the debuglink ? */
3150 free (debug_filename);
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)
3169 stash->info_ptr_end = stash->info_ptr;
3171 for (msec = find_debug_info (debug_bfd, NULL);
3173 msec = find_debug_info (debug_bfd, msec))
3176 bfd_size_type start;
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)
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)
3204 stash->inliner_chain = NULL;
3206 /* Check the previously read comp. units first. */
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
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,
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);
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);
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. */
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)
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)
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)
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
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)
3296 stash->info_ptr += 4;
3299 stash->info_ptr += 4;
3303 each = parse_comp_unit (stash, length, info_ptr_unit,
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;
3316 if (stash->all_comp_units)
3317 stash->all_comp_units->prev_unit = each;
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. */
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,
3338 found = ((arange_set_empty_p (each->arange_set)
3339 || comp_unit_contains_address (each, addr))
3340 && comp_unit_find_nearest_line (each, addr,
3352 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3353 unset_sections (stash);
3358 /* The DWARF2 version of find_nearest_line.
3359 Return TRUE if the line is found without error. */
3362 _bfd_dwarf2_find_nearest_line (bfd *abfd,
3366 const char **filename_ptr,
3367 const char **functionname_ptr,
3368 unsigned int *linenumber_ptr,
3369 unsigned int addr_size,
3372 return find_line (abfd, section, offset, NULL, symbols, filename_ptr,
3373 functionname_ptr, linenumber_ptr, addr_size,
3377 /* The DWARF2 version of find_line.
3378 Return TRUE if the line is found without error. */
3381 _bfd_dwarf2_find_line (bfd *abfd,
3384 const char **filename_ptr,
3385 unsigned int *linenumber_ptr,
3386 unsigned int addr_size,
3389 return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr,
3390 NULL, linenumber_ptr, addr_size,
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,
3401 struct dwarf2_debug *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;
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)
3430 stash = elf_tdata (abfd)->dwarf2_find_line_info;
3435 for (each = stash->all_comp_units; each; each = each->next_unit)
3437 struct abbrev_info **abbrevs = each->abbrevs;
3440 for (i = 0; i < ABBREV_HASH_SIZE; i++)
3442 struct abbrev_info *abbrev = abbrevs[i];
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);