Menu

[r4191]: / trunk / libdwarf / libdwarf_abbrev.c  Maximize  Restore  History

Download this file

307 lines (252 with data), 7.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*-
* Copyright (c) 2007 John Birrell (jb@freebsd.org)
* Copyright (c) 2009-2011 Kai Wang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "_libdwarf.h"
ELFTC_VCSID("$Id$");
static int
_dwarf_abbrev_allocate(Dwarf_Debug dbg, Dwarf_Abbrev *abp,
Dwarf_Error *error)
{
Dwarf_Abbrev ab;
if ((ab = calloc(1, sizeof(struct _Dwarf_Abbrev))) == NULL) {
DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
return (DW_DLE_MEMORY);
}
/* Initialise the list of attribute definitions. */
STAILQ_INIT(&ab->ab_attrdef);
*abp = ab;
return (DW_DLE_NONE);
}
static void
_dwarf_abbrev_release(Dwarf_Abbrev ab) {
Dwarf_AttrDef ad, tad;
STAILQ_FOREACH_SAFE(ad, &ab->ab_attrdef, ad_next, tad) {
STAILQ_REMOVE(&ab->ab_attrdef, ad, _Dwarf_AttrDef,
ad_next);
free(ad);
}
free(ab);
}
int
_dwarf_abbrev_add(Dwarf_CU cu, uint64_t entry, uint64_t tag, uint8_t children,
uint64_t aboff, Dwarf_Abbrev *abp, Dwarf_Error *error)
{
Dwarf_Abbrev ab;
Dwarf_Debug dbg;
int ret;
dbg = cu != NULL ? cu->cu_dbg : NULL;
if ((ret = _dwarf_abbrev_allocate(dbg, &ab, error)) != DW_DLE_NONE)
return (ret);
/* Initialise the abbrev structure. */
ab->ab_entry = entry;
ab->ab_tag = tag;
ab->ab_children = children;
ab->ab_offset = aboff;
ab->ab_length = 0; /* fill in later. */
ab->ab_atnum = 0; /* fill in later. */
/* Add the abbrev to the hash table of the compilation unit. */
if (cu != NULL)
HASH_ADD(ab_hh, cu->cu_abbrev_hash, ab_entry,
sizeof(ab->ab_entry), ab);
*abp = ab;
return (DW_DLE_NONE);
}
int
_dwarf_attrdef_add(Dwarf_Debug dbg, Dwarf_Abbrev ab, uint64_t attr,
uint64_t form, int64_t ic, uint64_t adoff, Dwarf_AttrDef *adp,
Dwarf_Error *error)
{
Dwarf_AttrDef ad;
if (ab == NULL) {
DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
return (DW_DLE_ARGUMENT);
}
if ((ad = malloc(sizeof(struct _Dwarf_AttrDef))) == NULL) {
DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
return (DW_DLE_MEMORY);
}
/* Initialise the attribute definition structure. */
ad->ad_attrib = attr;
ad->ad_form = form;
ad->ad_const = ic;
ad->ad_offset = adoff;
/* Add the attribute definition to the list in the abbrev. */
STAILQ_INSERT_TAIL(&ab->ab_attrdef, ad, ad_next);
/* Increase number of attribute counter. */
ab->ab_atnum++;
if (adp != NULL)
*adp = ad;
return (DW_DLE_NONE);
}
int
_dwarf_abbrev_parse(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Unsigned *offset,
Dwarf_Abbrev *abp, Dwarf_Error *error)
{
Dwarf_Section *ds;
uint64_t attr;
uint64_t entry;
uint64_t form;
uint64_t aboff;
uint64_t adoff;
uint64_t tag;
int64_t ic;
uint8_t children;
int ret;
assert(abp != NULL);
ds = _dwarf_find_section(dbg, ".debug_abbrev");
if (ds == NULL || *offset >= ds->ds_size)
return (DW_DLE_NO_ENTRY);
aboff = *offset;
entry = _dwarf_read_uleb128(ds->ds_data, offset);
if (entry == 0) {
/* Last entry. */
ret = _dwarf_abbrev_add(cu, entry, 0, 0, aboff, abp,
error);
if (ret != DW_DLE_NONE)
goto error;
(*abp)->ab_length = 1;
return (ret);
}
tag = _dwarf_read_uleb128(ds->ds_data, offset);
children = dbg->read(ds->ds_data, offset, 1);
if ((ret = _dwarf_abbrev_add(cu, entry, tag, children, aboff,
abp, error)) != DW_DLE_NONE)
goto error;
/* Parse attribute definitions. */
do {
adoff = *offset;
attr = _dwarf_read_uleb128(ds->ds_data, offset);
form = _dwarf_read_uleb128(ds->ds_data, offset);
ic = 0;
if (form == DW_FORM_implicit_const) {
/*
* DWARF5 7.5.3: atrribute definition with the form
* DW_FORM_implicit_const contains a third part, a
* signed LEB128 value indicating a constant value.
* No value is needed to store in the .debug_info
* as a result.
*/
ic = _dwarf_read_sleb128(ds->ds_data, offset);
}
if (attr != 0) {
if ((ret = _dwarf_attrdef_add(dbg, *abp, attr,
form, ic, adoff, NULL, error)) != DW_DLE_NONE)
goto error;
}
} while (attr != 0);
(*abp)->ab_length = *offset - aboff;
return (ret);
error:
_dwarf_abbrev_release(*abp);
return (ret);
}
int
_dwarf_abbrev_find(Dwarf_CU cu, uint64_t entry, Dwarf_Abbrev *abp,
Dwarf_Error *error)
{
Dwarf_Abbrev ab;
Dwarf_Section *ds;
Dwarf_Unsigned offset;
int ret;
if (entry == 0)
return (DW_DLE_NO_ENTRY);
/* Check if the desired abbrev entry is already in the hash table. */
HASH_FIND(ab_hh, cu->cu_abbrev_hash, &entry, sizeof(entry), ab);
if (ab != NULL) {
*abp = ab;
return (DW_DLE_NONE);
}
if (cu->cu_abbrev_loaded) {
return (DW_DLE_NO_ENTRY);
}
/* Load and search the abbrev table. */
ds = _dwarf_find_section(cu->cu_dbg, ".debug_abbrev");
if (ds == NULL)
return (DW_DLE_NO_ENTRY);
offset = cu->cu_abbrev_offset_cur;
while (offset < ds->ds_size) {
ret = _dwarf_abbrev_parse(cu->cu_dbg, cu, &offset, &ab, error);
if (ret != DW_DLE_NONE)
return (ret);
if (ab->ab_entry == entry) {
cu->cu_abbrev_offset_cur = offset;
*abp = ab;
return (DW_DLE_NONE);
}
if (ab->ab_entry == 0) {
cu->cu_abbrev_offset_cur = offset;
cu->cu_abbrev_loaded = 1;
break;
}
}
return (DW_DLE_NO_ENTRY);
}
void
_dwarf_abbrev_cleanup(Dwarf_CU cu)
{
Dwarf_Abbrev ab, tab;
assert(cu != NULL);
HASH_ITER(ab_hh, cu->cu_abbrev_hash, ab, tab) {
HASH_DELETE(ab_hh, cu->cu_abbrev_hash, ab);
_dwarf_abbrev_release(ab);
}
}
int
_dwarf_abbrev_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
{
Dwarf_CU cu;
Dwarf_Abbrev ab;
Dwarf_AttrDef ad;
Dwarf_P_Section ds;
int ret;
cu = STAILQ_FIRST(&dbg->dbg_cu);
if (cu == NULL)
return (DW_DLE_NONE);
/* Create .debug_abbrev section. */
if ((ret = _dwarf_section_init(dbg, &ds, ".debug_abbrev", 0, error)) !=
DW_DLE_NONE)
return (ret);
for (ab = cu->cu_abbrev_hash; ab != NULL; ab = ab->ab_hh.next) {
RCHECK(WRITE_ULEB128(ab->ab_entry));
RCHECK(WRITE_ULEB128(ab->ab_tag));
RCHECK(WRITE_VALUE(ab->ab_children, 1));
STAILQ_FOREACH(ad, &ab->ab_attrdef, ad_next) {
RCHECK(WRITE_ULEB128(ad->ad_attrib));
RCHECK(WRITE_ULEB128(ad->ad_form));
}
/* Signal end of attribute spec list. */
RCHECK(WRITE_ULEB128(0));
RCHECK(WRITE_ULEB128(0));
}
/* End of abbreviation for this CU. */
RCHECK(WRITE_ULEB128(0));
/* Notify the creation of .debug_abbrev ELF section. */
RCHECK(_dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error));
return (DW_DLE_NONE);
gen_fail:
_dwarf_section_free(dbg, &ds);
return (ret);
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.