1 /* DWARF 2 abbreviations
3 Copyright (C) 1994-2022 Free Software Foundation, Inc.
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 This file is part of GDB.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "dwarf2/read.h"
29 #include "dwarf2/abbrev.h"
30 #include "dwarf2/leb.h"
33 /* Hash function for an abbrev. */
36 hash_abbrev (const void *item)
38 const struct abbrev_info *info = (const struct abbrev_info *) item;
39 /* Warning: if you change this next line, you must also update the
40 other code in this class using the _with_hash functions. */
44 /* Comparison function for abbrevs. */
47 eq_abbrev (const void *lhs, const void *rhs)
49 const struct abbrev_info *l_info = (const struct abbrev_info *) lhs;
50 const struct abbrev_info *r_info = (const struct abbrev_info *) rhs;
51 return l_info->number == r_info->number;
54 /* Abbreviation tables.
56 In DWARF version 2, the description of the debugging information is
57 stored in a separate .debug_abbrev section. Before we read any
58 dies from a section we read in all abbreviations and install them
61 abbrev_table::abbrev_table (sect_offset off, struct dwarf2_section_info *sect)
64 m_abbrevs (htab_create_alloc (20, hash_abbrev, eq_abbrev,
65 nullptr, xcalloc, xfree))
69 /* Add an abbreviation to the table. */
72 abbrev_table::add_abbrev (struct abbrev_info *abbrev)
74 void **slot = htab_find_slot_with_hash (m_abbrevs.get (), abbrev,
75 abbrev->number, INSERT);
79 /* Helper function that returns true if a DIE with the given tag might
80 plausibly be indexed. */
83 tag_interesting_for_index (dwarf_tag tag)
87 case DW_TAG_array_type:
88 case DW_TAG_base_type:
89 case DW_TAG_class_type:
91 case DW_TAG_enumeration_type:
92 case DW_TAG_enumerator:
93 case DW_TAG_imported_declaration:
94 case DW_TAG_imported_unit:
95 case DW_TAG_inlined_subroutine:
96 case DW_TAG_interface_type:
98 case DW_TAG_namespace:
99 case DW_TAG_ptr_to_member_type:
100 case DW_TAG_set_type:
101 case DW_TAG_string_type:
102 case DW_TAG_structure_type:
103 case DW_TAG_subprogram:
104 case DW_TAG_subrange_type:
105 case DW_TAG_generic_subrange:
106 case DW_TAG_subroutine_type:
108 case DW_TAG_union_type:
109 case DW_TAG_unspecified_type:
110 case DW_TAG_variable:
117 /* Read in an abbrev table. */
120 abbrev_table::read (struct dwarf2_section_info *section,
121 sect_offset sect_off)
123 bfd *abfd = section->get_bfd_owner ();
124 const gdb_byte *abbrev_ptr;
125 struct abbrev_info *cur_abbrev;
127 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off, section));
128 struct obstack *obstack = &abbrev_table->m_abbrev_obstack;
130 /* Caller must ensure this. */
131 gdb_assert (section->readin);
132 abbrev_ptr = section->buffer + to_underlying (sect_off);
136 unsigned int bytes_read;
137 /* Loop until we reach an abbrev number of 0. */
138 unsigned int abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr,
140 if (abbrev_number == 0)
142 abbrev_ptr += bytes_read;
144 /* Start without any attrs. */
145 obstack_blank (obstack, offsetof (abbrev_info, attrs));
146 cur_abbrev = (struct abbrev_info *) obstack_base (obstack);
148 /* Read in abbrev header. */
149 cur_abbrev->number = abbrev_number;
151 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr,
153 abbrev_ptr += bytes_read;
154 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
157 unsigned int size = 0;
158 unsigned int sibling_offset = -1;
159 bool is_csize = true;
161 bool has_hardcoded_declaration = false;
162 bool has_specification_or_origin = false;
163 bool has_name = false;
164 bool has_linkage_name = false;
165 bool has_location = false;
166 bool has_external = false;
168 /* Now read in declarations. */
172 struct attr_abbrev cur_attr;
175 = (enum dwarf_attribute) read_unsigned_leb128 (abfd, abbrev_ptr,
177 abbrev_ptr += bytes_read;
179 = (enum dwarf_form) read_unsigned_leb128 (abfd, abbrev_ptr,
181 abbrev_ptr += bytes_read;
182 if (cur_attr.form == DW_FORM_implicit_const)
184 cur_attr.implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
186 abbrev_ptr += bytes_read;
189 cur_attr.implicit_const = -1;
191 if (cur_attr.name == 0)
194 switch (cur_attr.name)
196 case DW_AT_declaration:
197 if (cur_attr.form == DW_FORM_flag_present)
198 has_hardcoded_declaration = true;
205 case DW_AT_specification:
206 case DW_AT_abstract_origin:
207 case DW_AT_extension:
208 has_specification_or_origin = true;
215 case DW_AT_MIPS_linkage_name:
216 case DW_AT_linkage_name:
217 has_linkage_name = true;
220 case DW_AT_const_value:
226 if (is_csize && cur_attr.form == DW_FORM_ref4)
227 sibling_offset = size;
231 switch (cur_attr.form)
239 case DW_FORM_flag_present:
240 case DW_FORM_implicit_const:
257 case DW_FORM_ref_sig8:
270 obstack_grow (obstack, &cur_attr, sizeof (cur_attr));
273 cur_abbrev = (struct abbrev_info *) obstack_finish (obstack);
274 cur_abbrev->num_attrs = num_attrs;
276 if (!has_name && !has_linkage_name && !has_specification_or_origin)
278 /* Some anonymous DIEs are worth examining. */
279 cur_abbrev->interesting
280 = (cur_abbrev->tag == DW_TAG_namespace
281 || cur_abbrev->tag == DW_TAG_enumeration_type);
283 else if ((cur_abbrev->tag == DW_TAG_structure_type
284 || cur_abbrev->tag == DW_TAG_class_type
285 || cur_abbrev->tag == DW_TAG_union_type)
286 && cur_abbrev->has_children)
288 /* We have to record this as interesting, regardless of how
289 DW_AT_declaration is set, so that any subsequent
290 DW_AT_specification pointing at a child of this will get
291 the correct scope. */
292 cur_abbrev->interesting = true;
294 else if (has_hardcoded_declaration
295 && (cur_abbrev->tag != DW_TAG_variable || !has_external))
296 cur_abbrev->interesting = false;
297 else if (!tag_interesting_for_index (cur_abbrev->tag))
298 cur_abbrev->interesting = false;
299 else if (!has_location && !has_specification_or_origin && !has_external
300 && cur_abbrev->tag == DW_TAG_variable)
301 cur_abbrev->interesting = false;
303 cur_abbrev->interesting = true;
305 /* If there are no children, and the abbrev has a constant size,
306 then we don't care about the sibling offset, because it's
307 simple to just skip the entire DIE without reading a sibling
309 if ((!cur_abbrev->has_children && is_csize)
311 || sibling_offset != (unsigned short) sibling_offset)
313 cur_abbrev->size_if_constant = is_csize ? size : 0;
314 cur_abbrev->sibling_offset = sibling_offset;
316 abbrev_table->add_abbrev (cur_abbrev);