xref: /minix3/external/bsd/elftoolchain/dist/libdwarf/dwarf_form.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: dwarf_form.c,v 1.2 2014/03/09 16:58:03 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,2010 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: dwarf_form.c,v 1.2 2014/03/09 16:58:03 christos Exp $");
33*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: dwarf_form.c 2073 2011-10-27 03:30:47Z jkoshy ");
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc int
dwarf_hasform(Dwarf_Attribute at,Dwarf_Half form,Dwarf_Bool * return_hasform,Dwarf_Error * error)36*0a6a1f1dSLionel Sambuc dwarf_hasform(Dwarf_Attribute at, Dwarf_Half form, Dwarf_Bool *return_hasform,
37*0a6a1f1dSLionel Sambuc     Dwarf_Error *error)
38*0a6a1f1dSLionel Sambuc {
39*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
40*0a6a1f1dSLionel Sambuc 
41*0a6a1f1dSLionel Sambuc 	dbg = at != NULL ? at->at_die->die_dbg : NULL;
42*0a6a1f1dSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc 	if (at == NULL || return_hasform == NULL) {
44*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
45*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
46*0a6a1f1dSLionel Sambuc 	}
47*0a6a1f1dSLionel Sambuc 
48*0a6a1f1dSLionel Sambuc 	*return_hasform = (at->at_form == form);
49*0a6a1f1dSLionel Sambuc 
50*0a6a1f1dSLionel Sambuc 	return (DW_DLV_OK);
51*0a6a1f1dSLionel Sambuc }
52*0a6a1f1dSLionel Sambuc 
53*0a6a1f1dSLionel Sambuc int
dwarf_whatform(Dwarf_Attribute at,Dwarf_Half * return_form,Dwarf_Error * error)54*0a6a1f1dSLionel Sambuc dwarf_whatform(Dwarf_Attribute at, Dwarf_Half *return_form, Dwarf_Error *error)
55*0a6a1f1dSLionel Sambuc {
56*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
57*0a6a1f1dSLionel Sambuc 
58*0a6a1f1dSLionel Sambuc 	dbg = at != NULL ? at->at_die->die_dbg : NULL;
59*0a6a1f1dSLionel Sambuc 
60*0a6a1f1dSLionel Sambuc 	if (at == NULL || return_form == NULL) {
61*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
62*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
63*0a6a1f1dSLionel Sambuc 	}
64*0a6a1f1dSLionel Sambuc 
65*0a6a1f1dSLionel Sambuc 	*return_form = at->at_form;
66*0a6a1f1dSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc 	return (DW_DLV_OK);
68*0a6a1f1dSLionel Sambuc }
69*0a6a1f1dSLionel Sambuc 
70*0a6a1f1dSLionel Sambuc int
dwarf_whatform_direct(Dwarf_Attribute at,Dwarf_Half * return_form,Dwarf_Error * error)71*0a6a1f1dSLionel Sambuc dwarf_whatform_direct(Dwarf_Attribute at, Dwarf_Half *return_form,
72*0a6a1f1dSLionel Sambuc     Dwarf_Error *error)
73*0a6a1f1dSLionel Sambuc {
74*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
75*0a6a1f1dSLionel Sambuc 
76*0a6a1f1dSLionel Sambuc 	dbg = at != NULL ? at->at_die->die_dbg : NULL;
77*0a6a1f1dSLionel Sambuc 
78*0a6a1f1dSLionel Sambuc 	if (at == NULL || return_form == NULL) {
79*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
80*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
81*0a6a1f1dSLionel Sambuc 	}
82*0a6a1f1dSLionel Sambuc 
83*0a6a1f1dSLionel Sambuc 	if (at->at_indirect)
84*0a6a1f1dSLionel Sambuc 		*return_form = DW_FORM_indirect;
85*0a6a1f1dSLionel Sambuc 	else
86*0a6a1f1dSLionel Sambuc 		*return_form = (Dwarf_Half) at->at_form;
87*0a6a1f1dSLionel Sambuc 
88*0a6a1f1dSLionel Sambuc 	return (DW_DLV_OK);
89*0a6a1f1dSLionel Sambuc }
90*0a6a1f1dSLionel Sambuc 
91*0a6a1f1dSLionel Sambuc int
dwarf_whatattr(Dwarf_Attribute at,Dwarf_Half * return_attr,Dwarf_Error * error)92*0a6a1f1dSLionel Sambuc dwarf_whatattr(Dwarf_Attribute at, Dwarf_Half *return_attr, Dwarf_Error *error)
93*0a6a1f1dSLionel Sambuc {
94*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
95*0a6a1f1dSLionel Sambuc 
96*0a6a1f1dSLionel Sambuc 	dbg = at != NULL ? at->at_die->die_dbg : NULL;
97*0a6a1f1dSLionel Sambuc 
98*0a6a1f1dSLionel Sambuc 	if (at == NULL || return_attr == NULL) {
99*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
100*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
101*0a6a1f1dSLionel Sambuc 	}
102*0a6a1f1dSLionel Sambuc 
103*0a6a1f1dSLionel Sambuc 	*return_attr = (Dwarf_Half) at->at_attrib;
104*0a6a1f1dSLionel Sambuc 
105*0a6a1f1dSLionel Sambuc 	return (DW_DLV_OK);
106*0a6a1f1dSLionel Sambuc }
107*0a6a1f1dSLionel Sambuc 
108*0a6a1f1dSLionel Sambuc int
dwarf_formref(Dwarf_Attribute at,Dwarf_Off * return_offset,Dwarf_Error * error)109*0a6a1f1dSLionel Sambuc dwarf_formref(Dwarf_Attribute at, Dwarf_Off *return_offset, Dwarf_Error *error)
110*0a6a1f1dSLionel Sambuc {
111*0a6a1f1dSLionel Sambuc 	int ret;
112*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc 	dbg = at != NULL ? at->at_die->die_dbg : NULL;
115*0a6a1f1dSLionel Sambuc 
116*0a6a1f1dSLionel Sambuc 	if (at == NULL || return_offset == NULL) {
117*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
118*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
119*0a6a1f1dSLionel Sambuc 	}
120*0a6a1f1dSLionel Sambuc 
121*0a6a1f1dSLionel Sambuc 	switch (at->at_form) {
122*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref1:
123*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref2:
124*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref4:
125*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref8:
126*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref_udata:
127*0a6a1f1dSLionel Sambuc 		*return_offset = (Dwarf_Off) at->u[0].u64;
128*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_OK;
129*0a6a1f1dSLionel Sambuc 		break;
130*0a6a1f1dSLionel Sambuc 	default:
131*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
132*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_ERROR;
133*0a6a1f1dSLionel Sambuc 	}
134*0a6a1f1dSLionel Sambuc 
135*0a6a1f1dSLionel Sambuc 	return (ret);
136*0a6a1f1dSLionel Sambuc }
137*0a6a1f1dSLionel Sambuc 
138*0a6a1f1dSLionel Sambuc int
dwarf_global_formref(Dwarf_Attribute at,Dwarf_Off * return_offset,Dwarf_Error * error)139*0a6a1f1dSLionel Sambuc dwarf_global_formref(Dwarf_Attribute at, Dwarf_Off *return_offset,
140*0a6a1f1dSLionel Sambuc     Dwarf_Error *error)
141*0a6a1f1dSLionel Sambuc {
142*0a6a1f1dSLionel Sambuc 	int ret;
143*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
144*0a6a1f1dSLionel Sambuc 
145*0a6a1f1dSLionel Sambuc 	dbg = at != NULL ? at->at_die->die_dbg : NULL;
146*0a6a1f1dSLionel Sambuc 
147*0a6a1f1dSLionel Sambuc 	if (at == NULL || return_offset == NULL) {
148*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
149*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
150*0a6a1f1dSLionel Sambuc 	}
151*0a6a1f1dSLionel Sambuc 
152*0a6a1f1dSLionel Sambuc 	switch (at->at_form) {
153*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref_addr:
154*0a6a1f1dSLionel Sambuc 	case DW_FORM_sec_offset:
155*0a6a1f1dSLionel Sambuc 		*return_offset = (Dwarf_Off) at->u[0].u64;
156*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_OK;
157*0a6a1f1dSLionel Sambuc 		break;
158*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref1:
159*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref2:
160*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref4:
161*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref8:
162*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref_udata:
163*0a6a1f1dSLionel Sambuc 		*return_offset = (Dwarf_Off) at->u[0].u64 +
164*0a6a1f1dSLionel Sambuc 			at->at_die->die_cu->cu_offset;
165*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_OK;
166*0a6a1f1dSLionel Sambuc 		break;
167*0a6a1f1dSLionel Sambuc 	default:
168*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
169*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_ERROR;
170*0a6a1f1dSLionel Sambuc 	}
171*0a6a1f1dSLionel Sambuc 
172*0a6a1f1dSLionel Sambuc 	return (ret);
173*0a6a1f1dSLionel Sambuc }
174*0a6a1f1dSLionel Sambuc 
175*0a6a1f1dSLionel Sambuc int
dwarf_formaddr(Dwarf_Attribute at,Dwarf_Addr * return_addr,Dwarf_Error * error)176*0a6a1f1dSLionel Sambuc dwarf_formaddr(Dwarf_Attribute at, Dwarf_Addr *return_addr, Dwarf_Error *error)
177*0a6a1f1dSLionel Sambuc {
178*0a6a1f1dSLionel Sambuc 	int ret;
179*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
180*0a6a1f1dSLionel Sambuc 
181*0a6a1f1dSLionel Sambuc 	dbg = at != NULL ? at->at_die->die_dbg : NULL;
182*0a6a1f1dSLionel Sambuc 
183*0a6a1f1dSLionel Sambuc 	if (at == NULL || return_addr == NULL) {
184*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
185*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
186*0a6a1f1dSLionel Sambuc 	}
187*0a6a1f1dSLionel Sambuc 
188*0a6a1f1dSLionel Sambuc 	if (at->at_form == DW_FORM_addr) {
189*0a6a1f1dSLionel Sambuc 		*return_addr = at->u[0].u64;
190*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_OK;
191*0a6a1f1dSLionel Sambuc 	} else {
192*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
193*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_ERROR;
194*0a6a1f1dSLionel Sambuc 	}
195*0a6a1f1dSLionel Sambuc 
196*0a6a1f1dSLionel Sambuc 	return (ret);
197*0a6a1f1dSLionel Sambuc }
198*0a6a1f1dSLionel Sambuc 
199*0a6a1f1dSLionel Sambuc int
dwarf_formflag(Dwarf_Attribute at,Dwarf_Bool * return_bool,Dwarf_Error * error)200*0a6a1f1dSLionel Sambuc dwarf_formflag(Dwarf_Attribute at, Dwarf_Bool *return_bool, Dwarf_Error *error)
201*0a6a1f1dSLionel Sambuc {
202*0a6a1f1dSLionel Sambuc 	int ret;
203*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
204*0a6a1f1dSLionel Sambuc 
205*0a6a1f1dSLionel Sambuc 	dbg = at != NULL ? at->at_die->die_dbg : NULL;
206*0a6a1f1dSLionel Sambuc 
207*0a6a1f1dSLionel Sambuc 	if (at == NULL || return_bool == NULL) {
208*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
209*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
210*0a6a1f1dSLionel Sambuc 	}
211*0a6a1f1dSLionel Sambuc 
212*0a6a1f1dSLionel Sambuc 	if (at->at_form == DW_FORM_flag ||
213*0a6a1f1dSLionel Sambuc 	    at->at_form == DW_FORM_flag_present) {
214*0a6a1f1dSLionel Sambuc 		*return_bool = (Dwarf_Bool) (!!at->u[0].u64);
215*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_OK;
216*0a6a1f1dSLionel Sambuc 	} else {
217*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
218*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_ERROR;
219*0a6a1f1dSLionel Sambuc 	}
220*0a6a1f1dSLionel Sambuc 
221*0a6a1f1dSLionel Sambuc 	return (ret);
222*0a6a1f1dSLionel Sambuc }
223*0a6a1f1dSLionel Sambuc 
224*0a6a1f1dSLionel Sambuc int
dwarf_formudata(Dwarf_Attribute at,Dwarf_Unsigned * return_uvalue,Dwarf_Error * error)225*0a6a1f1dSLionel Sambuc dwarf_formudata(Dwarf_Attribute at, Dwarf_Unsigned *return_uvalue,
226*0a6a1f1dSLionel Sambuc     Dwarf_Error *error)
227*0a6a1f1dSLionel Sambuc {
228*0a6a1f1dSLionel Sambuc 	int ret;
229*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
230*0a6a1f1dSLionel Sambuc 
231*0a6a1f1dSLionel Sambuc 	dbg = at != NULL ? at->at_die->die_dbg : NULL;
232*0a6a1f1dSLionel Sambuc 
233*0a6a1f1dSLionel Sambuc 	if (at == NULL || return_uvalue == NULL) {
234*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
235*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
236*0a6a1f1dSLionel Sambuc 	}
237*0a6a1f1dSLionel Sambuc 
238*0a6a1f1dSLionel Sambuc 	switch (at->at_form) {
239*0a6a1f1dSLionel Sambuc 	case DW_FORM_data1:
240*0a6a1f1dSLionel Sambuc 	case DW_FORM_data2:
241*0a6a1f1dSLionel Sambuc 	case DW_FORM_data4:
242*0a6a1f1dSLionel Sambuc 	case DW_FORM_data8:
243*0a6a1f1dSLionel Sambuc 	case DW_FORM_udata:
244*0a6a1f1dSLionel Sambuc 		*return_uvalue = at->u[0].u64;
245*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_OK;
246*0a6a1f1dSLionel Sambuc 		break;
247*0a6a1f1dSLionel Sambuc 	default:
248*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
249*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_ERROR;
250*0a6a1f1dSLionel Sambuc 	}
251*0a6a1f1dSLionel Sambuc 
252*0a6a1f1dSLionel Sambuc 	return (ret);
253*0a6a1f1dSLionel Sambuc }
254*0a6a1f1dSLionel Sambuc 
255*0a6a1f1dSLionel Sambuc int
dwarf_formsdata(Dwarf_Attribute at,Dwarf_Signed * return_svalue,Dwarf_Error * error)256*0a6a1f1dSLionel Sambuc dwarf_formsdata(Dwarf_Attribute at, Dwarf_Signed *return_svalue,
257*0a6a1f1dSLionel Sambuc     Dwarf_Error *error)
258*0a6a1f1dSLionel Sambuc {
259*0a6a1f1dSLionel Sambuc 	int ret;
260*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
261*0a6a1f1dSLionel Sambuc 
262*0a6a1f1dSLionel Sambuc 	dbg = at != NULL ? at->at_die->die_dbg : NULL;
263*0a6a1f1dSLionel Sambuc 
264*0a6a1f1dSLionel Sambuc 	if (at == NULL || return_svalue == NULL) {
265*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
266*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
267*0a6a1f1dSLionel Sambuc 	}
268*0a6a1f1dSLionel Sambuc 
269*0a6a1f1dSLionel Sambuc 	switch (at->at_form) {
270*0a6a1f1dSLionel Sambuc 	case DW_FORM_data1:
271*0a6a1f1dSLionel Sambuc 		*return_svalue = (int8_t) at->u[0].s64;
272*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_OK;
273*0a6a1f1dSLionel Sambuc 		break;
274*0a6a1f1dSLionel Sambuc 	case DW_FORM_data2:
275*0a6a1f1dSLionel Sambuc 		*return_svalue = (int16_t) at->u[0].s64;
276*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_OK;
277*0a6a1f1dSLionel Sambuc 		break;
278*0a6a1f1dSLionel Sambuc 	case DW_FORM_data4:
279*0a6a1f1dSLionel Sambuc 		*return_svalue = (int32_t) at->u[0].s64;
280*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_OK;
281*0a6a1f1dSLionel Sambuc 		break;
282*0a6a1f1dSLionel Sambuc 	case DW_FORM_data8:
283*0a6a1f1dSLionel Sambuc 	case DW_FORM_sdata:
284*0a6a1f1dSLionel Sambuc 		*return_svalue = at->u[0].s64;
285*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_OK;
286*0a6a1f1dSLionel Sambuc 		break;
287*0a6a1f1dSLionel Sambuc 	default:
288*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
289*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_ERROR;
290*0a6a1f1dSLionel Sambuc 	}
291*0a6a1f1dSLionel Sambuc 
292*0a6a1f1dSLionel Sambuc 	return (ret);
293*0a6a1f1dSLionel Sambuc }
294*0a6a1f1dSLionel Sambuc 
295*0a6a1f1dSLionel Sambuc int
dwarf_formblock(Dwarf_Attribute at,Dwarf_Block ** return_block,Dwarf_Error * error)296*0a6a1f1dSLionel Sambuc dwarf_formblock(Dwarf_Attribute at, Dwarf_Block **return_block,
297*0a6a1f1dSLionel Sambuc     Dwarf_Error *error)
298*0a6a1f1dSLionel Sambuc {
299*0a6a1f1dSLionel Sambuc 	int ret;
300*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
301*0a6a1f1dSLionel Sambuc 
302*0a6a1f1dSLionel Sambuc 	dbg = at != NULL ? at->at_die->die_dbg : NULL;
303*0a6a1f1dSLionel Sambuc 
304*0a6a1f1dSLionel Sambuc 	if (at == NULL || return_block == NULL) {
305*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
306*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
307*0a6a1f1dSLionel Sambuc 	}
308*0a6a1f1dSLionel Sambuc 
309*0a6a1f1dSLionel Sambuc 	switch (at->at_form) {
310*0a6a1f1dSLionel Sambuc 	case DW_FORM_block:
311*0a6a1f1dSLionel Sambuc 	case DW_FORM_block1:
312*0a6a1f1dSLionel Sambuc 	case DW_FORM_block2:
313*0a6a1f1dSLionel Sambuc 	case DW_FORM_block4:
314*0a6a1f1dSLionel Sambuc 		*return_block = &at->at_block;
315*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_OK;
316*0a6a1f1dSLionel Sambuc 		break;
317*0a6a1f1dSLionel Sambuc 	default:
318*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
319*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_ERROR;
320*0a6a1f1dSLionel Sambuc 	}
321*0a6a1f1dSLionel Sambuc 
322*0a6a1f1dSLionel Sambuc 	return (ret);
323*0a6a1f1dSLionel Sambuc }
324*0a6a1f1dSLionel Sambuc 
325*0a6a1f1dSLionel Sambuc int
dwarf_formsig8(Dwarf_Attribute at,Dwarf_Sig8 * return_sig8,Dwarf_Error * error)326*0a6a1f1dSLionel Sambuc dwarf_formsig8(Dwarf_Attribute at, Dwarf_Sig8 *return_sig8, Dwarf_Error *error)
327*0a6a1f1dSLionel Sambuc {
328*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
329*0a6a1f1dSLionel Sambuc 
330*0a6a1f1dSLionel Sambuc 	dbg = at != NULL ? at->at_die->die_dbg : NULL;
331*0a6a1f1dSLionel Sambuc 
332*0a6a1f1dSLionel Sambuc 	if (at == NULL || return_sig8 == NULL) {
333*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
334*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
335*0a6a1f1dSLionel Sambuc 	}
336*0a6a1f1dSLionel Sambuc 
337*0a6a1f1dSLionel Sambuc 	if (at->at_form != DW_FORM_ref_sig8) {
338*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
339*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
340*0a6a1f1dSLionel Sambuc 	}
341*0a6a1f1dSLionel Sambuc 
342*0a6a1f1dSLionel Sambuc 	assert(at->u[0].u64 == 8);
343*0a6a1f1dSLionel Sambuc 	memcpy(return_sig8->signature, at->u[1].u8p, at->u[0].u64);
344*0a6a1f1dSLionel Sambuc 
345*0a6a1f1dSLionel Sambuc 	return (DW_DLV_OK);
346*0a6a1f1dSLionel Sambuc }
347*0a6a1f1dSLionel Sambuc 
348*0a6a1f1dSLionel Sambuc int
dwarf_formexprloc(Dwarf_Attribute at,Dwarf_Unsigned * return_exprlen,Dwarf_Ptr * return_expr,Dwarf_Error * error)349*0a6a1f1dSLionel Sambuc dwarf_formexprloc(Dwarf_Attribute at, Dwarf_Unsigned *return_exprlen,
350*0a6a1f1dSLionel Sambuc     Dwarf_Ptr *return_expr, Dwarf_Error *error)
351*0a6a1f1dSLionel Sambuc {
352*0a6a1f1dSLionel Sambuc 
353*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
354*0a6a1f1dSLionel Sambuc 
355*0a6a1f1dSLionel Sambuc 	dbg = at != NULL ? at->at_die->die_dbg : NULL;
356*0a6a1f1dSLionel Sambuc 
357*0a6a1f1dSLionel Sambuc 	if (at == NULL || return_exprlen == NULL || return_expr == NULL) {
358*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
359*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
360*0a6a1f1dSLionel Sambuc 	}
361*0a6a1f1dSLionel Sambuc 
362*0a6a1f1dSLionel Sambuc 	if (at->at_form != DW_FORM_exprloc) {
363*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
364*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
365*0a6a1f1dSLionel Sambuc 	}
366*0a6a1f1dSLionel Sambuc 
367*0a6a1f1dSLionel Sambuc 	*return_exprlen = at->u[0].u64;
368*0a6a1f1dSLionel Sambuc 	*return_expr = (void *) at->u[1].u8p;
369*0a6a1f1dSLionel Sambuc 
370*0a6a1f1dSLionel Sambuc 	return (DW_DLV_OK);
371*0a6a1f1dSLionel Sambuc }
372*0a6a1f1dSLionel Sambuc 
373*0a6a1f1dSLionel Sambuc int
dwarf_formstring(Dwarf_Attribute at,char ** return_string,Dwarf_Error * error)374*0a6a1f1dSLionel Sambuc dwarf_formstring(Dwarf_Attribute at, char **return_string,
375*0a6a1f1dSLionel Sambuc     Dwarf_Error *error)
376*0a6a1f1dSLionel Sambuc {
377*0a6a1f1dSLionel Sambuc 	int ret;
378*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
379*0a6a1f1dSLionel Sambuc 
380*0a6a1f1dSLionel Sambuc 	dbg = at != NULL ? at->at_die->die_dbg : NULL;
381*0a6a1f1dSLionel Sambuc 
382*0a6a1f1dSLionel Sambuc 	if (at == NULL || return_string == NULL) {
383*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
384*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
385*0a6a1f1dSLionel Sambuc 	}
386*0a6a1f1dSLionel Sambuc 
387*0a6a1f1dSLionel Sambuc 	switch (at->at_form) {
388*0a6a1f1dSLionel Sambuc 	case DW_FORM_string:
389*0a6a1f1dSLionel Sambuc 		*return_string = (char *) at->u[0].s;
390*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_OK;
391*0a6a1f1dSLionel Sambuc 		break;
392*0a6a1f1dSLionel Sambuc 	case DW_FORM_strp:
393*0a6a1f1dSLionel Sambuc 		*return_string = (char *) at->u[1].s;
394*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_OK;
395*0a6a1f1dSLionel Sambuc 		break;
396*0a6a1f1dSLionel Sambuc 	default:
397*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
398*0a6a1f1dSLionel Sambuc 		ret = DW_DLV_ERROR;
399*0a6a1f1dSLionel Sambuc 	}
400*0a6a1f1dSLionel Sambuc 
401*0a6a1f1dSLionel Sambuc 	return (ret);
402*0a6a1f1dSLionel Sambuc }
403*0a6a1f1dSLionel Sambuc 
404*0a6a1f1dSLionel Sambuc enum Dwarf_Form_Class
dwarf_get_form_class(Dwarf_Half dwversion,Dwarf_Half attr,Dwarf_Half offset_size,Dwarf_Half form)405*0a6a1f1dSLionel Sambuc dwarf_get_form_class(Dwarf_Half dwversion, Dwarf_Half attr,
406*0a6a1f1dSLionel Sambuc     Dwarf_Half offset_size, Dwarf_Half form)
407*0a6a1f1dSLionel Sambuc {
408*0a6a1f1dSLionel Sambuc 
409*0a6a1f1dSLionel Sambuc 	switch (form) {
410*0a6a1f1dSLionel Sambuc 	case DW_FORM_addr:
411*0a6a1f1dSLionel Sambuc 		return (DW_FORM_CLASS_ADDRESS);
412*0a6a1f1dSLionel Sambuc 	case DW_FORM_block:
413*0a6a1f1dSLionel Sambuc 	case DW_FORM_block1:
414*0a6a1f1dSLionel Sambuc 	case DW_FORM_block2:
415*0a6a1f1dSLionel Sambuc 	case DW_FORM_block4:
416*0a6a1f1dSLionel Sambuc 		return (DW_FORM_CLASS_BLOCK);
417*0a6a1f1dSLionel Sambuc 	case DW_FORM_string:
418*0a6a1f1dSLionel Sambuc 	case DW_FORM_strp:
419*0a6a1f1dSLionel Sambuc 		return (DW_FORM_CLASS_STRING);
420*0a6a1f1dSLionel Sambuc 	case DW_FORM_flag:
421*0a6a1f1dSLionel Sambuc 	case DW_FORM_flag_present:
422*0a6a1f1dSLionel Sambuc 		return (DW_FORM_CLASS_FLAG);
423*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref_addr:
424*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref_sig8:
425*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref_udata:
426*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref1:
427*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref2:
428*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref4:
429*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref8:
430*0a6a1f1dSLionel Sambuc 		return (DW_FORM_CLASS_REFERENCE);
431*0a6a1f1dSLionel Sambuc 	case DW_FORM_exprloc:
432*0a6a1f1dSLionel Sambuc 		return (DW_FORM_CLASS_EXPRLOC);
433*0a6a1f1dSLionel Sambuc 	case DW_FORM_data1:
434*0a6a1f1dSLionel Sambuc 	case DW_FORM_data2:
435*0a6a1f1dSLionel Sambuc 	case DW_FORM_sdata:
436*0a6a1f1dSLionel Sambuc 	case DW_FORM_udata:
437*0a6a1f1dSLionel Sambuc 		return (DW_FORM_CLASS_CONSTANT);
438*0a6a1f1dSLionel Sambuc 	case DW_FORM_data4:
439*0a6a1f1dSLionel Sambuc 	case DW_FORM_data8:
440*0a6a1f1dSLionel Sambuc 		if (dwversion > 3)
441*0a6a1f1dSLionel Sambuc 			return (DW_FORM_CLASS_CONSTANT);
442*0a6a1f1dSLionel Sambuc 		if (form == DW_FORM_data4 && offset_size != 4)
443*0a6a1f1dSLionel Sambuc 			return (DW_FORM_CLASS_CONSTANT);
444*0a6a1f1dSLionel Sambuc 		if (form == DW_FORM_data8 && offset_size != 8)
445*0a6a1f1dSLionel Sambuc 			return (DW_FORM_CLASS_CONSTANT);
446*0a6a1f1dSLionel Sambuc 		/* FALLTHROUGH */
447*0a6a1f1dSLionel Sambuc 	case DW_FORM_sec_offset:
448*0a6a1f1dSLionel Sambuc 		/*
449*0a6a1f1dSLionel Sambuc 		 * DW_FORM_data4 and DW_FORM_data8 can be used as
450*0a6a1f1dSLionel Sambuc 		 * offset/pointer before DWARF4. Newly added
451*0a6a1f1dSLionel Sambuc 		 * DWARF4 form DW_FORM_sec_offset intents to replace
452*0a6a1f1dSLionel Sambuc 		 * DW_FORM_data{4,8} for this purpose. Anyway, to
453*0a6a1f1dSLionel Sambuc 		 * determine the actual class for these forms, we need
454*0a6a1f1dSLionel Sambuc 		 * to also look at the attribute number.
455*0a6a1f1dSLionel Sambuc 		 */
456*0a6a1f1dSLionel Sambuc 		switch (attr) {
457*0a6a1f1dSLionel Sambuc 		case DW_AT_location:
458*0a6a1f1dSLionel Sambuc 		case DW_AT_string_length:
459*0a6a1f1dSLionel Sambuc 		case DW_AT_return_addr:
460*0a6a1f1dSLionel Sambuc 		case DW_AT_data_member_location:
461*0a6a1f1dSLionel Sambuc 		case DW_AT_frame_base:
462*0a6a1f1dSLionel Sambuc 		case DW_AT_segment:
463*0a6a1f1dSLionel Sambuc 		case DW_AT_static_link:
464*0a6a1f1dSLionel Sambuc 		case DW_AT_use_location:
465*0a6a1f1dSLionel Sambuc 		case DW_AT_vtable_elem_location:
466*0a6a1f1dSLionel Sambuc 			return (DW_FORM_CLASS_LOCLISTPTR);
467*0a6a1f1dSLionel Sambuc 		case DW_AT_stmt_list:
468*0a6a1f1dSLionel Sambuc 			return (DW_FORM_CLASS_LINEPTR);
469*0a6a1f1dSLionel Sambuc 		case DW_AT_start_scope:
470*0a6a1f1dSLionel Sambuc 		case DW_AT_ranges:
471*0a6a1f1dSLionel Sambuc 			return (DW_FORM_CLASS_RANGELISTPTR);
472*0a6a1f1dSLionel Sambuc 		case DW_AT_macro_info:
473*0a6a1f1dSLionel Sambuc 			return (DW_FORM_CLASS_MACPTR);
474*0a6a1f1dSLionel Sambuc 		default:
475*0a6a1f1dSLionel Sambuc 			if (form == DW_FORM_data4 || form == DW_FORM_data8)
476*0a6a1f1dSLionel Sambuc 				return (DW_FORM_CLASS_CONSTANT);
477*0a6a1f1dSLionel Sambuc 			else
478*0a6a1f1dSLionel Sambuc 				return (DW_FORM_CLASS_UNKNOWN);
479*0a6a1f1dSLionel Sambuc 		}
480*0a6a1f1dSLionel Sambuc 	default:
481*0a6a1f1dSLionel Sambuc 		return (DW_FORM_CLASS_UNKNOWN);
482*0a6a1f1dSLionel Sambuc 	}
483*0a6a1f1dSLionel Sambuc }
484