Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / libdwarf / dist / dwarf_attrval.c
blob1a1d0b3f2afb1ba3cdca49eb3ce70301c6f2b8fb
1 /* $NetBSD$ */
3 /*-
4 * Copyright (c) 2007 John Birrell (jb@freebsd.org)
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
28 * $FreeBSD: src/lib/libdwarf/dwarf_attrval.c,v 1.1.4.1 2009/08/03 08:13:06 kensmith Exp $
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include "_libdwarf.h"
36 Dwarf_AttrValue
37 dwarf_attrval_find(Dwarf_Die die, Dwarf_Half attr)
39 Dwarf_AttrValue av;
41 STAILQ_FOREACH(av, &die->die_attrval, av_next) {
42 if (av->av_attrib == attr)
43 break;
46 return av;
49 int
50 dwarf_attrval_add(Dwarf_Die die, Dwarf_AttrValue avref, Dwarf_AttrValue *avp, Dwarf_Error *error)
52 Dwarf_AttrValue av;
53 int ret = DWARF_E_NONE;
55 if ((av = malloc(sizeof(struct _Dwarf_AttrValue))) == NULL) {
56 DWARF_SET_ERROR(error, DWARF_E_MEMORY);
57 return DWARF_E_MEMORY;
60 memcpy(av, avref, sizeof(struct _Dwarf_AttrValue));
62 /* Add the attribute value to the list in the die. */
63 STAILQ_INSERT_TAIL(&die->die_attrval, av, av_next);
65 /* Save a pointer to the attribute name if this is one. */
66 if (av->av_attrib == DW_AT_name)
67 switch (av->av_form) {
68 case DW_FORM_strp:
69 die->die_name = av->u[1].s;
70 break;
71 case DW_FORM_string:
72 die->die_name = av->u[0].s;
73 break;
74 default:
75 break;
78 if (avp != NULL)
79 *avp = av;
81 return ret;
84 int
85 dwarf_attrval_flag(Dwarf_Die die, uint64_t attr, Dwarf_Bool *valp, Dwarf_Error *err)
87 Dwarf_AttrValue av;
88 int ret = DWARF_E_NONE;
90 if (err == NULL)
91 return DWARF_E_ERROR;
93 if (die == NULL || valp == NULL) {
94 DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
95 return DWARF_E_ARGUMENT;
98 *valp = 0;
100 if ((av = dwarf_attrval_find(die, attr)) == NULL) {
101 DWARF_SET_ERROR(err, DWARF_E_NO_ENTRY);
102 ret = DWARF_E_NO_ENTRY;
103 } else {
104 switch (av->av_form) {
105 case DW_FORM_flag:
106 *valp = (Dwarf_Bool) av->u[0].u64;
107 break;
108 default:
109 printf("%s(%d): av->av_form '%s' (0x%lx) not handled\n",
110 __func__,__LINE__,get_form_desc(av->av_form),
111 (u_long) av->av_form);
112 DWARF_SET_ERROR(err, DWARF_E_BAD_FORM);
113 ret = DWARF_E_BAD_FORM;
117 return ret;
121 dwarf_attrval_string(Dwarf_Die die, uint64_t attr, const char **strp, Dwarf_Error *err)
123 Dwarf_AttrValue av;
124 int ret = DWARF_E_NONE;
126 if (err == NULL)
127 return DWARF_E_ERROR;
129 if (die == NULL || strp == NULL) {
130 DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
131 return DWARF_E_ARGUMENT;
134 *strp = NULL;
136 if (attr == DW_AT_name)
137 *strp = die->die_name;
138 else if ((av = dwarf_attrval_find(die, attr)) == NULL) {
139 DWARF_SET_ERROR(err, DWARF_E_NO_ENTRY);
140 ret = DWARF_E_NO_ENTRY;
141 } else {
142 switch (av->av_form) {
143 case DW_FORM_strp:
144 *strp = av->u[1].s;
145 break;
146 case DW_FORM_string:
147 *strp = av->u[0].s;
148 break;
149 default:
150 printf("%s(%d): av->av_form '%s' (0x%lx) not handled\n",
151 __func__,__LINE__,get_form_desc(av->av_form),
152 (u_long) av->av_form);
153 DWARF_SET_ERROR(err, DWARF_E_BAD_FORM);
154 ret = DWARF_E_BAD_FORM;
158 return ret;
162 dwarf_attrval_signed(Dwarf_Die die, uint64_t attr, Dwarf_Signed *valp, Dwarf_Error *err)
164 Dwarf_AttrValue av;
165 int ret = DWARF_E_NONE;
167 if (err == NULL)
168 return DWARF_E_ERROR;
170 if (die == NULL || valp == NULL) {
171 DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
172 return DWARF_E_ARGUMENT;
175 *valp = 0;
177 if ((av = dwarf_attrval_find(die, attr)) == NULL) {
178 DWARF_SET_ERROR(err, DWARF_E_NO_ENTRY);
179 ret = DWARF_E_NO_ENTRY;
180 } else {
181 switch (av->av_form) {
182 case DW_FORM_data1:
183 case DW_FORM_sdata:
184 *valp = av->u[0].s64;
185 break;
186 default:
187 printf("%s(%d): av->av_form '%s' (0x%lx) not handled\n",
188 __func__,__LINE__,get_form_desc(av->av_form),
189 (u_long) av->av_form);
190 DWARF_SET_ERROR(err, DWARF_E_BAD_FORM);
191 ret = DWARF_E_BAD_FORM;
195 return ret;
199 dwarf_attrval_unsigned(Dwarf_Die die, uint64_t attr, Dwarf_Unsigned *valp, Dwarf_Error *err)
201 Dwarf_AttrValue av;
202 int ret = DWARF_E_NONE;
204 if (err == NULL)
205 return DWARF_E_ERROR;
207 if (die == NULL || valp == NULL) {
208 DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
209 return DWARF_E_ARGUMENT;
212 *valp = 0;
214 if ((av = dwarf_attrval_find(die, attr)) == NULL && attr != DW_AT_type) {
215 DWARF_SET_ERROR(err, DWARF_E_NO_ENTRY);
216 ret = DWARF_E_NO_ENTRY;
217 } else if (av == NULL && (av = dwarf_attrval_find(die,
218 DW_AT_abstract_origin)) != NULL) {
219 Dwarf_Die die1;
220 Dwarf_Unsigned val;
222 switch (av->av_form) {
223 case DW_FORM_data1:
224 case DW_FORM_data2:
225 case DW_FORM_data4:
226 case DW_FORM_data8:
227 case DW_FORM_ref1:
228 case DW_FORM_ref2:
229 case DW_FORM_ref4:
230 case DW_FORM_ref8:
231 case DW_FORM_ref_udata:
232 val = av->u[0].u64;
234 if ((die1 = dwarf_die_find(die, val)) == NULL ||
235 (av = dwarf_attrval_find(die1, attr)) == NULL) {
236 DWARF_SET_ERROR(err, DWARF_E_NO_ENTRY);
237 ret = DWARF_E_NO_ENTRY;
239 break;
240 default:
241 printf("%s(%d): av->av_form '%s' (0x%lx) not handled\n",
242 __func__,__LINE__,get_form_desc(av->av_form),
243 (u_long) av->av_form);
244 DWARF_SET_ERROR(err, DWARF_E_BAD_FORM);
245 ret = DWARF_E_BAD_FORM;
249 if (ret == DWARF_E_NONE) {
250 switch (av->av_form) {
251 case DW_FORM_data1:
252 case DW_FORM_data2:
253 case DW_FORM_data4:
254 case DW_FORM_data8:
255 case DW_FORM_ref1:
256 case DW_FORM_ref2:
257 case DW_FORM_ref4:
258 case DW_FORM_ref8:
259 case DW_FORM_ref_udata:
260 *valp = av->u[0].u64;
261 break;
262 default:
263 printf("%s(%d): av->av_form '%s' (0x%lx) not handled\n",
264 __func__,__LINE__,get_form_desc(av->av_form),
265 (u_long) av->av_form);
266 DWARF_SET_ERROR(err, DWARF_E_BAD_FORM);
267 ret = DWARF_E_BAD_FORM;
271 return ret;