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