xref: /minix3/external/bsd/elftoolchain/dist/libdwarf/dwarf_attrval.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: dwarf_attrval.c,v 1.3 2015/01/14 09:29:27 martin Exp $	*/
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2007 John Birrell (jb@freebsd.org)
5*0a6a1f1dSLionel Sambuc  * All rights reserved.
6*0a6a1f1dSLionel Sambuc  *
7*0a6a1f1dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc  * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc  * are met:
10*0a6a1f1dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc  *
16*0a6a1f1dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*0a6a1f1dSLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*0a6a1f1dSLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*0a6a1f1dSLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*0a6a1f1dSLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*0a6a1f1dSLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*0a6a1f1dSLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*0a6a1f1dSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*0a6a1f1dSLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*0a6a1f1dSLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*0a6a1f1dSLionel Sambuc  * SUCH DAMAGE.
27*0a6a1f1dSLionel Sambuc  */
28*0a6a1f1dSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc #include "_libdwarf.h"
30*0a6a1f1dSLionel Sambuc 
31*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: dwarf_attrval.c,v 1.3 2015/01/14 09:29:27 martin Exp $");
32*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: dwarf_attrval.c 2072 2011-10-27 03:26:49Z jkoshy ");
33*0a6a1f1dSLionel Sambuc 
34*0a6a1f1dSLionel Sambuc int
dwarf_attrval_flag(Dwarf_Die die,Dwarf_Half attr,Dwarf_Bool * valp,Dwarf_Error * err)35*0a6a1f1dSLionel Sambuc dwarf_attrval_flag(Dwarf_Die die, Dwarf_Half attr, Dwarf_Bool *valp, Dwarf_Error *err)
36*0a6a1f1dSLionel Sambuc {
37*0a6a1f1dSLionel Sambuc 	Dwarf_Attribute at;
38*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
39*0a6a1f1dSLionel Sambuc 
40*0a6a1f1dSLionel Sambuc 	dbg = die != NULL ? die->die_dbg : NULL;
41*0a6a1f1dSLionel Sambuc 
42*0a6a1f1dSLionel Sambuc 	if (die == NULL || valp == NULL) {
43*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, err, DW_DLE_ARGUMENT);
44*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
45*0a6a1f1dSLionel Sambuc 	}
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc 	*valp = 0;
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc 	if ((at = _dwarf_attr_find(die, attr)) == NULL) {
50*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, err, DW_DLE_NO_ENTRY);
51*0a6a1f1dSLionel Sambuc 		return (DW_DLV_NO_ENTRY);
52*0a6a1f1dSLionel Sambuc 	}
53*0a6a1f1dSLionel Sambuc 
54*0a6a1f1dSLionel Sambuc 	switch (at->at_form) {
55*0a6a1f1dSLionel Sambuc 	case DW_FORM_flag:
56*0a6a1f1dSLionel Sambuc 	case DW_FORM_flag_present:
57*0a6a1f1dSLionel Sambuc 		*valp = (Dwarf_Bool) (!!at->u[0].u64);
58*0a6a1f1dSLionel Sambuc 		break;
59*0a6a1f1dSLionel Sambuc 	default:
60*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, err, DW_DLE_ATTR_FORM_BAD);
61*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
62*0a6a1f1dSLionel Sambuc 	}
63*0a6a1f1dSLionel Sambuc 
64*0a6a1f1dSLionel Sambuc 	return (DW_DLV_OK);
65*0a6a1f1dSLionel Sambuc }
66*0a6a1f1dSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc int
dwarf_attrval_string(Dwarf_Die die,Dwarf_Half attr,const char ** strp,Dwarf_Error * err)68*0a6a1f1dSLionel Sambuc dwarf_attrval_string(Dwarf_Die die, Dwarf_Half attr, const char **strp, Dwarf_Error *err)
69*0a6a1f1dSLionel Sambuc {
70*0a6a1f1dSLionel Sambuc 	Dwarf_Attribute at;
71*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
72*0a6a1f1dSLionel Sambuc 
73*0a6a1f1dSLionel Sambuc 	dbg = die != NULL ? die->die_dbg : NULL;
74*0a6a1f1dSLionel Sambuc 
75*0a6a1f1dSLionel Sambuc 	if (die == NULL || strp == NULL) {
76*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, err, DW_DLE_ARGUMENT);
77*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
78*0a6a1f1dSLionel Sambuc 	}
79*0a6a1f1dSLionel Sambuc 
80*0a6a1f1dSLionel Sambuc 	*strp = NULL;
81*0a6a1f1dSLionel Sambuc 
82*0a6a1f1dSLionel Sambuc 	if ((at = _dwarf_attr_find(die, attr)) == NULL) {
83*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, err, DW_DLE_NO_ENTRY);
84*0a6a1f1dSLionel Sambuc 		return (DW_DLV_NO_ENTRY);
85*0a6a1f1dSLionel Sambuc 	}
86*0a6a1f1dSLionel Sambuc 
87*0a6a1f1dSLionel Sambuc 	switch (at->at_form) {
88*0a6a1f1dSLionel Sambuc 	case DW_FORM_strp:
89*0a6a1f1dSLionel Sambuc 		*strp = at->u[1].s;
90*0a6a1f1dSLionel Sambuc 		break;
91*0a6a1f1dSLionel Sambuc 	case DW_FORM_string:
92*0a6a1f1dSLionel Sambuc 		*strp = at->u[0].s;
93*0a6a1f1dSLionel Sambuc 		break;
94*0a6a1f1dSLionel Sambuc 	default:
95*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, err, DW_DLE_ATTR_FORM_BAD);
96*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
97*0a6a1f1dSLionel Sambuc 	}
98*0a6a1f1dSLionel Sambuc 
99*0a6a1f1dSLionel Sambuc 	return (DW_DLV_OK);
100*0a6a1f1dSLionel Sambuc }
101*0a6a1f1dSLionel Sambuc 
102*0a6a1f1dSLionel Sambuc int
dwarf_attrval_signed(Dwarf_Die die,Dwarf_Half attr,Dwarf_Signed * valp,Dwarf_Error * err)103*0a6a1f1dSLionel Sambuc dwarf_attrval_signed(Dwarf_Die die, Dwarf_Half attr, Dwarf_Signed *valp, Dwarf_Error *err)
104*0a6a1f1dSLionel Sambuc {
105*0a6a1f1dSLionel Sambuc 	Dwarf_Attribute at;
106*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
107*0a6a1f1dSLionel Sambuc 
108*0a6a1f1dSLionel Sambuc 	dbg = die != NULL ? die->die_dbg : NULL;
109*0a6a1f1dSLionel Sambuc 
110*0a6a1f1dSLionel Sambuc 	if (die == NULL || valp == NULL) {
111*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, err, DW_DLE_ARGUMENT);
112*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
113*0a6a1f1dSLionel Sambuc 	}
114*0a6a1f1dSLionel Sambuc 
115*0a6a1f1dSLionel Sambuc 	*valp = 0;
116*0a6a1f1dSLionel Sambuc 
117*0a6a1f1dSLionel Sambuc 	if ((at = _dwarf_attr_find(die, attr)) == NULL) {
118*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, err, DW_DLE_NO_ENTRY);
119*0a6a1f1dSLionel Sambuc 		return (DW_DLV_NO_ENTRY);
120*0a6a1f1dSLionel Sambuc 	}
121*0a6a1f1dSLionel Sambuc 
122*0a6a1f1dSLionel Sambuc 	switch (at->at_form) {
123*0a6a1f1dSLionel Sambuc 	case DW_FORM_data1:
124*0a6a1f1dSLionel Sambuc 		*valp = (int8_t) at->u[0].s64;
125*0a6a1f1dSLionel Sambuc 		break;
126*0a6a1f1dSLionel Sambuc 	case DW_FORM_data2:
127*0a6a1f1dSLionel Sambuc 		*valp = (int16_t) at->u[0].s64;
128*0a6a1f1dSLionel Sambuc 		break;
129*0a6a1f1dSLionel Sambuc 	case DW_FORM_data4:
130*0a6a1f1dSLionel Sambuc 		*valp = (int32_t) at->u[0].s64;
131*0a6a1f1dSLionel Sambuc 		break;
132*0a6a1f1dSLionel Sambuc 	case DW_FORM_data8:
133*0a6a1f1dSLionel Sambuc 	case DW_FORM_sdata:
134*0a6a1f1dSLionel Sambuc 		*valp = at->u[0].s64;
135*0a6a1f1dSLionel Sambuc 		break;
136*0a6a1f1dSLionel Sambuc 	default:
137*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, err, DW_DLE_ATTR_FORM_BAD);
138*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
139*0a6a1f1dSLionel Sambuc 	}
140*0a6a1f1dSLionel Sambuc 
141*0a6a1f1dSLionel Sambuc 	return (DW_DLV_OK);
142*0a6a1f1dSLionel Sambuc }
143*0a6a1f1dSLionel Sambuc 
144*0a6a1f1dSLionel Sambuc int
dwarf_attrval_unsigned(Dwarf_Die die,Dwarf_Half attr,Dwarf_Unsigned * valp,Dwarf_Error * err)145*0a6a1f1dSLionel Sambuc dwarf_attrval_unsigned(Dwarf_Die die, Dwarf_Half attr, Dwarf_Unsigned *valp, Dwarf_Error *err)
146*0a6a1f1dSLionel Sambuc {
147*0a6a1f1dSLionel Sambuc 	Dwarf_Attribute at;
148*0a6a1f1dSLionel Sambuc 	Dwarf_Die die1;
149*0a6a1f1dSLionel Sambuc 	Dwarf_Unsigned val;
150*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
151*0a6a1f1dSLionel Sambuc 
152*0a6a1f1dSLionel Sambuc 	dbg = die != NULL ? die->die_dbg : NULL;
153*0a6a1f1dSLionel Sambuc 
154*0a6a1f1dSLionel Sambuc 	if (die == NULL || valp == NULL) {
155*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, err, DW_DLE_ARGUMENT);
156*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
157*0a6a1f1dSLionel Sambuc 	}
158*0a6a1f1dSLionel Sambuc 
159*0a6a1f1dSLionel Sambuc 	*valp = 0;
160*0a6a1f1dSLionel Sambuc 
161*0a6a1f1dSLionel Sambuc 	if ((at = _dwarf_attr_find(die, attr)) == NULL && attr != DW_AT_type) {
162*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, err, DW_DLE_NO_ENTRY);
163*0a6a1f1dSLionel Sambuc 		return (DW_DLV_NO_ENTRY);
164*0a6a1f1dSLionel Sambuc 	}
165*0a6a1f1dSLionel Sambuc 
166*0a6a1f1dSLionel Sambuc 	die1 = NULL;
167*0a6a1f1dSLionel Sambuc 	if (at == NULL &&
168*0a6a1f1dSLionel Sambuc 	    (at = _dwarf_attr_find(die, DW_AT_abstract_origin)) != NULL) {
169*0a6a1f1dSLionel Sambuc 		switch (at->at_form) {
170*0a6a1f1dSLionel Sambuc 		case DW_FORM_ref1:
171*0a6a1f1dSLionel Sambuc 		case DW_FORM_ref2:
172*0a6a1f1dSLionel Sambuc 		case DW_FORM_ref4:
173*0a6a1f1dSLionel Sambuc 		case DW_FORM_ref8:
174*0a6a1f1dSLionel Sambuc 		case DW_FORM_ref_udata:
175*0a6a1f1dSLionel Sambuc 			val = at->u[0].u64;
176*0a6a1f1dSLionel Sambuc 			if ((die1 = _dwarf_die_find(die, val)) == NULL ||
177*0a6a1f1dSLionel Sambuc 			    (at = _dwarf_attr_find(die1, attr)) == NULL) {
178*0a6a1f1dSLionel Sambuc 				if (die1 != NULL)
179*0a6a1f1dSLionel Sambuc 					dwarf_dealloc(dbg, die1, DW_DLA_DIE);
180*0a6a1f1dSLionel Sambuc 				DWARF_SET_ERROR(dbg, err, DW_DLE_NO_ENTRY);
181*0a6a1f1dSLionel Sambuc 				return (DW_DLV_NO_ENTRY);
182*0a6a1f1dSLionel Sambuc 			}
183*0a6a1f1dSLionel Sambuc 			break;
184*0a6a1f1dSLionel Sambuc 		default:
185*0a6a1f1dSLionel Sambuc 			DWARF_SET_ERROR(dbg, err, DW_DLE_ATTR_FORM_BAD);
186*0a6a1f1dSLionel Sambuc 			return (DW_DLV_ERROR);
187*0a6a1f1dSLionel Sambuc 		}
188*0a6a1f1dSLionel Sambuc 	}
189*0a6a1f1dSLionel Sambuc 
190*0a6a1f1dSLionel Sambuc 	switch (at->at_form) {
191*0a6a1f1dSLionel Sambuc 	case DW_FORM_addr:
192*0a6a1f1dSLionel Sambuc 	case DW_FORM_data1:
193*0a6a1f1dSLionel Sambuc 	case DW_FORM_data2:
194*0a6a1f1dSLionel Sambuc 	case DW_FORM_data4:
195*0a6a1f1dSLionel Sambuc 	case DW_FORM_data8:
196*0a6a1f1dSLionel Sambuc 	case DW_FORM_udata:
197*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref1:
198*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref2:
199*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref4:
200*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref8:
201*0a6a1f1dSLionel Sambuc 	case DW_FORM_ref_udata:
202*0a6a1f1dSLionel Sambuc 		*valp = at->u[0].u64;
203*0a6a1f1dSLionel Sambuc 		break;
204*0a6a1f1dSLionel Sambuc 	default:
205*0a6a1f1dSLionel Sambuc 		if (die1 != NULL)
206*0a6a1f1dSLionel Sambuc 			dwarf_dealloc(dbg, die1, DW_DLA_DIE);
207*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, err, DW_DLE_ATTR_FORM_BAD);
208*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
209*0a6a1f1dSLionel Sambuc 	}
210*0a6a1f1dSLionel Sambuc 
211*0a6a1f1dSLionel Sambuc 	if (die1 != NULL)
212*0a6a1f1dSLionel Sambuc 		dwarf_dealloc(dbg, die1, DW_DLA_DIE);
213*0a6a1f1dSLionel Sambuc 
214*0a6a1f1dSLionel Sambuc 	return (DW_DLV_OK);
215*0a6a1f1dSLionel Sambuc }
216