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