GDB (xrefs)
Loading...
Searching...
No Matches
abbrev.c
Go to the documentation of this file.
1/* DWARF 2 abbreviations
2
3 Copyright (C) 1994-2023 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
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
10 support.
11
12 This file is part of GDB.
13
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.
18
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.
23
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/>. */
26
27#include "defs.h"
28#include "dwarf2/read.h"
29#include "dwarf2/abbrev.h"
30#include "dwarf2/leb.h"
31#include "bfd.h"
32
33/* Hash function for an abbrev. */
34
35static hashval_t
36hash_abbrev (const void *item)
37{
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. */
41 return info->number;
42}
43
44/* Comparison function for abbrevs. */
45
46static int
47eq_abbrev (const void *lhs, const void *rhs)
48{
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;
52}
53
54/* Abbreviation tables.
55
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
59 in a hash table. */
60
61abbrev_table::abbrev_table (sect_offset off, struct dwarf2_section_info *sect)
62 : sect_off (off),
63 section (sect),
64 m_abbrevs (htab_create_alloc (20, hash_abbrev, eq_abbrev,
66{
67}
68
69/* Add an abbreviation to the table. */
70
71void
73{
74 void **slot = htab_find_slot_with_hash (m_abbrevs.get (), abbrev,
75 abbrev->number, INSERT);
76 *slot = abbrev;
77}
78
79/* Helper function that returns true if a DIE with the given tag might
80 plausibly be indexed. */
81
82static bool
84{
85 switch (tag)
86 {
87 case DW_TAG_array_type:
88 case DW_TAG_base_type:
89 case DW_TAG_class_type:
90 case DW_TAG_constant:
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:
97 case DW_TAG_module:
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:
107 case DW_TAG_typedef:
108 case DW_TAG_union_type:
109 case DW_TAG_unspecified_type:
110 case DW_TAG_variable:
111 return true;
112 }
113
114 return false;
115}
116
117/* Read in an abbrev table. */
118
121 sect_offset sect_off)
122{
123 bfd *abfd = section->get_bfd_owner ();
124 const gdb_byte *abbrev_ptr;
125 struct abbrev_info *cur_abbrev;
126
128 struct obstack *obstack = &abbrev_table->m_abbrev_obstack;
129
130 /* Caller must ensure this. */
131 gdb_assert (section->readin);
132 abbrev_ptr = section->buffer + to_underlying (sect_off);
133
134 while (true)
135 {
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,
139 &bytes_read);
140 if (abbrev_number == 0)
141 break;
142 abbrev_ptr += bytes_read;
143
144 /* Start without any attrs. */
145 obstack_blank (obstack, offsetof (abbrev_info, attrs));
146 cur_abbrev = (struct abbrev_info *) obstack_base (obstack);
147
148 /* Read in abbrev header. */
149 cur_abbrev->number = abbrev_number;
150 cur_abbrev->tag
151 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr,
152 &bytes_read);
153 abbrev_ptr += bytes_read;
154 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
155 abbrev_ptr += 1;
156
157 unsigned int size = 0;
158 unsigned int sibling_offset = -1;
159 bool is_csize = true;
160
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_external = false;
166
167 /* Now read in declarations. */
168 int num_attrs = 0;
169 for (;;)
170 {
171 struct attr_abbrev cur_attr;
172
173 cur_attr.name
174 = (enum dwarf_attribute) read_unsigned_leb128 (abfd, abbrev_ptr,
175 &bytes_read);
176 abbrev_ptr += bytes_read;
177 cur_attr.form
178 = (enum dwarf_form) read_unsigned_leb128 (abfd, abbrev_ptr,
179 &bytes_read);
180 abbrev_ptr += bytes_read;
181 if (cur_attr.form == DW_FORM_implicit_const)
182 {
183 cur_attr.implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
184 &bytes_read);
185 abbrev_ptr += bytes_read;
186 }
187 else
188 cur_attr.implicit_const = -1;
189
190 if (cur_attr.name == 0)
191 break;
192
193 switch (cur_attr.name)
194 {
195 case DW_AT_declaration:
196 if (cur_attr.form == DW_FORM_flag_present)
197 has_hardcoded_declaration = true;
198 break;
199
200 case DW_AT_external:
201 has_external = true;
202 break;
203
204 case DW_AT_specification:
205 case DW_AT_abstract_origin:
206 case DW_AT_extension:
207 has_specification_or_origin = true;
208 break;
209
210 case DW_AT_name:
211 has_name = true;
212 break;
213
214 case DW_AT_MIPS_linkage_name:
215 case DW_AT_linkage_name:
216 has_linkage_name = true;
217 break;
218
219 case DW_AT_sibling:
220 if (is_csize && cur_attr.form == DW_FORM_ref4)
221 sibling_offset = size;
222 break;
223 }
224
225 switch (cur_attr.form)
226 {
227 case DW_FORM_data1:
228 case DW_FORM_ref1:
229 case DW_FORM_flag:
230 case DW_FORM_strx1:
231 size += 1;
232 break;
233 case DW_FORM_flag_present:
234 case DW_FORM_implicit_const:
235 break;
236 case DW_FORM_data2:
237 case DW_FORM_ref2:
238 case DW_FORM_strx2:
239 size += 2;
240 break;
241 case DW_FORM_strx3:
242 size += 3;
243 break;
244 case DW_FORM_data4:
245 case DW_FORM_ref4:
246 case DW_FORM_strx4:
247 size += 4;
248 break;
249 case DW_FORM_data8:
250 case DW_FORM_ref8:
251 case DW_FORM_ref_sig8:
252 size += 8;
253 break;
254 case DW_FORM_data16:
255 size += 16;
256 break;
257
258 default:
259 is_csize = false;
260 break;
261 }
262
263 ++num_attrs;
264 obstack_grow (obstack, &cur_attr, sizeof (cur_attr));
265 }
266
267 cur_abbrev = (struct abbrev_info *) obstack_finish (obstack);
268 cur_abbrev->num_attrs = num_attrs;
269
270 if (!has_name && !has_linkage_name && !has_specification_or_origin)
271 {
272 /* Some anonymous DIEs are worth examining. */
273 cur_abbrev->interesting
274 = (cur_abbrev->tag == DW_TAG_namespace
275 || cur_abbrev->tag == DW_TAG_enumeration_type);
276 }
277 else if ((cur_abbrev->tag == DW_TAG_structure_type
278 || cur_abbrev->tag == DW_TAG_class_type
279 || cur_abbrev->tag == DW_TAG_union_type)
280 && cur_abbrev->has_children)
281 {
282 /* We have to record this as interesting, regardless of how
283 DW_AT_declaration is set, so that any subsequent
284 DW_AT_specification pointing at a child of this will get
285 the correct scope. */
286 cur_abbrev->interesting = true;
287 }
288 else if (has_hardcoded_declaration
289 && (cur_abbrev->tag != DW_TAG_variable || !has_external))
290 cur_abbrev->interesting = false;
291 else if (!tag_interesting_for_index (cur_abbrev->tag))
292 cur_abbrev->interesting = false;
293 else
294 cur_abbrev->interesting = true;
295
296 /* If there are no children, and the abbrev has a constant size,
297 then we don't care about the sibling offset, because it's
298 simple to just skip the entire DIE without reading a sibling
299 offset. */
300 if ((!cur_abbrev->has_children && is_csize)
301 /* Overflow. */
302 || sibling_offset != (unsigned short) sibling_offset)
303 sibling_offset = -1;
304 cur_abbrev->size_if_constant = is_csize ? size : 0;
305 cur_abbrev->sibling_offset = sibling_offset;
306
307 abbrev_table->add_abbrev (cur_abbrev);
308 }
309
310 return abbrev_table;
311}
static int eq_abbrev(const void *lhs, const void *rhs)
Definition abbrev.c:47
static bool tag_interesting_for_index(dwarf_tag tag)
Definition abbrev.c:83
static hashval_t hash_abbrev(const void *item)
Definition abbrev.c:36
std::unique_ptr< struct abbrev_table > abbrev_table_up
Definition abbrev.h:61
void xfree(void *)
void * xcalloc(size_t number, size_t size)
Definition alloc.c:85
size_t size
Definition go32-nat.c:239
LONGEST read_signed_leb128(bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read_ptr)
Definition leb.c:59
ULONGEST read_unsigned_leb128(bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read_ptr)
Definition leb.c:31
static unsigned int read_1_byte(bfd *abfd, const gdb_byte *buf)
Definition leb.h:33
__extension__ enum dwarf_tag tag
Definition abbrev.h:47
unsigned short num_attrs
Definition abbrev.h:54
unsigned int number
Definition abbrev.h:45
unsigned short sibling_offset
Definition abbrev.h:52
unsigned short size_if_constant
Definition abbrev.h:51
bool interesting
Definition abbrev.h:50
bool has_children
Definition abbrev.h:49
const sect_offset sect_off
Definition abbrev.h:89
void add_abbrev(struct abbrev_info *abbrev)
Definition abbrev.c:72
htab_up m_abbrevs
Definition abbrev.h:103
auto_obstack m_abbrev_obstack
Definition abbrev.h:106
static abbrev_table_up read(struct dwarf2_section_info *section, sect_offset sect_off)
Definition abbrev.c:120
abbrev_table(sect_offset off, struct dwarf2_section_info *sect)
Definition abbrev.c:61
struct dwarf2_section_info * section
Definition abbrev.h:91
LONGEST implicit_const
Definition abbrev.h:38
__extension__ enum dwarf_attribute name
Definition abbrev.h:34
__extension__ enum dwarf_form form
Definition abbrev.h:35
const gdb_byte * buffer
Definition section.h:115
struct bfd * get_bfd_owner() const
Definition section.c:49
#define nullptr
Definition x86-cpuid.h:28