Add support for dynamic type lengths
[deliverable/binutils-gdb.git] / gdb / dwarf2 / abbrev.c
1 /* DWARF 2 abbreviations
2
3 Copyright (C) 1994-2020 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
35 static hashval_t
36 hash_abbrev (const void *item)
37 {
38 const struct abbrev_info *info = (const struct abbrev_info *) item;
39 return info->number;
40 }
41
42 /* Comparison function for abbrevs. */
43
44 static int
45 eq_abbrev (const void *lhs, const void *rhs)
46 {
47 const struct abbrev_info *l_info = (const struct abbrev_info *) lhs;
48 const struct abbrev_info *r_info = (const struct abbrev_info *) rhs;
49 return l_info->number == r_info->number;
50 }
51
52 /* Abbreviation tables.
53
54 In DWARF version 2, the description of the debugging information is
55 stored in a separate .debug_abbrev section. Before we read any
56 dies from a section we read in all abbreviations and install them
57 in a hash table. */
58
59 abbrev_table::abbrev_table (sect_offset off)
60 : sect_off (off),
61 m_abbrevs (htab_create_alloc (20, hash_abbrev, eq_abbrev,
62 nullptr, xcalloc, xfree))
63 {
64 }
65
66 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
67
68 struct abbrev_info *
69 abbrev_table::alloc_abbrev ()
70 {
71 struct abbrev_info *abbrev;
72
73 abbrev = XOBNEW (&m_abbrev_obstack, struct abbrev_info);
74 memset (abbrev, 0, sizeof (struct abbrev_info));
75
76 return abbrev;
77 }
78
79 /* Add an abbreviation to the table. */
80
81 void
82 abbrev_table::add_abbrev (unsigned int abbrev_number,
83 struct abbrev_info *abbrev)
84 {
85 void **slot = htab_find_slot (m_abbrevs.get (), abbrev, INSERT);
86 *slot = abbrev;
87 }
88
89 /* Look up an abbrev in the table.
90 Returns NULL if the abbrev is not found. */
91
92 struct abbrev_info *
93 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
94 {
95 struct abbrev_info search;
96 search.number = abbrev_number;
97
98 return (struct abbrev_info *) htab_find (m_abbrevs.get (), &search);
99 }
100
101 /* Read in an abbrev table. */
102
103 abbrev_table_up
104 abbrev_table::read (struct objfile *objfile,
105 struct dwarf2_section_info *section,
106 sect_offset sect_off)
107 {
108 bfd *abfd = section->get_bfd_owner ();
109 const gdb_byte *abbrev_ptr;
110 struct abbrev_info *cur_abbrev;
111 unsigned int abbrev_number, bytes_read, abbrev_name;
112 unsigned int abbrev_form;
113 std::vector<struct attr_abbrev> cur_attrs;
114
115 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
116
117 section->read (objfile);
118 abbrev_ptr = section->buffer + to_underlying (sect_off);
119 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
120 abbrev_ptr += bytes_read;
121
122 /* Loop until we reach an abbrev number of 0. */
123 while (abbrev_number)
124 {
125 cur_attrs.clear ();
126 cur_abbrev = abbrev_table->alloc_abbrev ();
127
128 /* read in abbrev header */
129 cur_abbrev->number = abbrev_number;
130 cur_abbrev->tag
131 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
132 abbrev_ptr += bytes_read;
133 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
134 abbrev_ptr += 1;
135
136 /* now read in declarations */
137 for (;;)
138 {
139 LONGEST implicit_const;
140
141 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
142 abbrev_ptr += bytes_read;
143 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
144 abbrev_ptr += bytes_read;
145 if (abbrev_form == DW_FORM_implicit_const)
146 {
147 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
148 &bytes_read);
149 abbrev_ptr += bytes_read;
150 }
151 else
152 {
153 /* Initialize it due to a false compiler warning. */
154 implicit_const = -1;
155 }
156
157 if (abbrev_name == 0)
158 break;
159
160 cur_attrs.emplace_back ();
161 struct attr_abbrev &cur_attr = cur_attrs.back ();
162 cur_attr.name = (enum dwarf_attribute) abbrev_name;
163 cur_attr.form = (enum dwarf_form) abbrev_form;
164 cur_attr.implicit_const = implicit_const;
165 }
166
167 cur_abbrev->num_attrs = cur_attrs.size ();
168 cur_abbrev->attrs =
169 XOBNEWVEC (&abbrev_table->m_abbrev_obstack, struct attr_abbrev,
170 cur_abbrev->num_attrs);
171 if (!cur_attrs.empty ())
172 memcpy (cur_abbrev->attrs, cur_attrs.data (),
173 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
174
175 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
176
177 /* Get next abbreviation.
178 Under Irix6 the abbreviations for a compilation unit are not
179 always properly terminated with an abbrev number of 0.
180 Exit loop if we encounter an abbreviation which we have
181 already read (which means we are about to read the abbreviations
182 for the next compile unit) or if the end of the abbreviation
183 table is reached. */
184 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
185 break;
186 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
187 abbrev_ptr += bytes_read;
188 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
189 break;
190 }
191
192 return abbrev_table;
193 }
This page took 0.032581 seconds and 4 git commands to generate.