2 * Copyright (c) 2007 John Birrell (jb@freebsd.org)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include "_libdwarf.h"
35 dwarf_attrval_find(Dwarf_Die die, Dwarf_Half attr)
39 STAILQ_FOREACH(av, &die->die_attrval, av_next) {
40 if (av->av_attrib == attr)
48 dwarf_attrval_add(Dwarf_Die die, Dwarf_AttrValue avref, Dwarf_AttrValue *avp, Dwarf_Error *error)
51 int ret = DWARF_E_NONE;
53 if ((av = malloc(sizeof(struct _Dwarf_AttrValue))) == NULL) {
54 DWARF_SET_ERROR(error, DWARF_E_MEMORY);
55 return DWARF_E_MEMORY;
58 memcpy(av, avref, sizeof(struct _Dwarf_AttrValue));
60 /* Add the attribute value to the list in the die. */
61 STAILQ_INSERT_TAIL(&die->die_attrval, av, av_next);
63 /* Save a pointer to the attribute name if this is one. */
64 if (av->av_attrib == DW_AT_name)
65 switch (av->av_form) {
67 die->die_name = av->u[1].s;
70 die->die_name = av->u[0].s;
83 dwarf_attrval_flag(Dwarf_Die die, uint64_t attr, Dwarf_Bool *valp, Dwarf_Error *err)
86 int ret = DWARF_E_NONE;
91 if (die == NULL || valp == NULL) {
92 DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
93 return DWARF_E_ARGUMENT;
98 if ((av = dwarf_attrval_find(die, attr)) == NULL) {
99 DWARF_SET_ERROR(err, DWARF_E_NO_ENTRY);
100 ret = DWARF_E_NO_ENTRY;
102 switch (av->av_form) {
104 *valp = (Dwarf_Bool) av->u[0].u64;
107 printf("%s(%d): av->av_form '%s' (0x%lx) not handled\n",
108 __func__,__LINE__,get_form_desc(av->av_form),
109 (u_long) av->av_form);
110 DWARF_SET_ERROR(err, DWARF_E_BAD_FORM);
111 ret = DWARF_E_BAD_FORM;
119 dwarf_attrval_string(Dwarf_Die die, uint64_t attr, const char **strp, Dwarf_Error *err)
122 int ret = DWARF_E_NONE;
125 return DWARF_E_ERROR;
127 if (die == NULL || strp == NULL) {
128 DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
129 return DWARF_E_ARGUMENT;
134 if (attr == DW_AT_name)
135 *strp = die->die_name;
136 else if ((av = dwarf_attrval_find(die, attr)) == NULL) {
137 DWARF_SET_ERROR(err, DWARF_E_NO_ENTRY);
138 ret = DWARF_E_NO_ENTRY;
140 switch (av->av_form) {
148 printf("%s(%d): av->av_form '%s' (0x%lx) not handled\n",
149 __func__,__LINE__,get_form_desc(av->av_form),
150 (u_long) av->av_form);
151 DWARF_SET_ERROR(err, DWARF_E_BAD_FORM);
152 ret = DWARF_E_BAD_FORM;
160 dwarf_attrval_signed(Dwarf_Die die, uint64_t attr, Dwarf_Signed *valp, Dwarf_Error *err)
163 int ret = DWARF_E_NONE;
166 return DWARF_E_ERROR;
168 if (die == NULL || valp == NULL) {
169 DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
170 return DWARF_E_ARGUMENT;
175 if ((av = dwarf_attrval_find(die, attr)) == NULL) {
176 DWARF_SET_ERROR(err, DWARF_E_NO_ENTRY);
177 ret = DWARF_E_NO_ENTRY;
179 switch (av->av_form) {
182 *valp = av->u[0].s64;
185 printf("%s(%d): av->av_form '%s' (0x%lx) not handled\n",
186 __func__,__LINE__,get_form_desc(av->av_form),
187 (u_long) av->av_form);
188 DWARF_SET_ERROR(err, DWARF_E_BAD_FORM);
189 ret = DWARF_E_BAD_FORM;
197 dwarf_attrval_unsigned(Dwarf_Die die, uint64_t attr, Dwarf_Unsigned *valp, Dwarf_Error *err)
200 int ret = DWARF_E_NONE;
203 return DWARF_E_ERROR;
205 if (die == NULL || valp == NULL) {
206 DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
207 return DWARF_E_ARGUMENT;
212 if ((av = dwarf_attrval_find(die, attr)) == NULL && attr != DW_AT_type) {
213 DWARF_SET_ERROR(err, DWARF_E_NO_ENTRY);
214 ret = DWARF_E_NO_ENTRY;
215 } else if (av == NULL && (av = dwarf_attrval_find(die,
216 DW_AT_abstract_origin)) != NULL) {
220 switch (av->av_form) {
229 case DW_FORM_ref_udata:
232 if ((die1 = dwarf_die_find(die, val)) == NULL ||
233 (av = dwarf_attrval_find(die1, attr)) == NULL) {
234 DWARF_SET_ERROR(err, DWARF_E_NO_ENTRY);
235 ret = DWARF_E_NO_ENTRY;
239 printf("%s(%d): av->av_form '%s' (0x%lx) not handled\n",
240 __func__,__LINE__,get_form_desc(av->av_form),
241 (u_long) av->av_form);
242 DWARF_SET_ERROR(err, DWARF_E_BAD_FORM);
243 ret = DWARF_E_BAD_FORM;
247 if (ret == DWARF_E_NONE) {
248 switch (av->av_form) {
257 case DW_FORM_ref_udata:
258 *valp = av->u[0].u64;
261 printf("%s(%d): av->av_form '%s' (0x%lx) not handled\n",
262 __func__,__LINE__,get_form_desc(av->av_form),
263 (u_long) av->av_form);
264 DWARF_SET_ERROR(err, DWARF_E_BAD_FORM);
265 ret = DWARF_E_BAD_FORM;