1 /* $NetBSD: dwarf_attr.c,v 1.2 2014/03/09 16:58:03 christos Exp $ */
4 * Copyright (c) 2007 John Birrell (jb@freebsd.org)
5 * Copyright (c) 2009 Kai Wang
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include "_libdwarf.h"
32 __RCSID("$NetBSD: dwarf_attr.c,v 1.2 2014/03/09 16:58:03 christos Exp $");
33 ELFTC_VCSID("Id: dwarf_attr.c 2072 2011-10-27 03:26:49Z jkoshy ");
36 dwarf_attr(Dwarf_Die die
, Dwarf_Half attr
, Dwarf_Attribute
*atp
,
42 dbg
= die
!= NULL
? die
->die_dbg
: NULL
;
44 if (die
== NULL
|| atp
== NULL
) {
45 DWARF_SET_ERROR(dbg
, error
, DW_DLE_ARGUMENT
);
46 return (DW_DLV_ERROR
);
49 if ((at
= _dwarf_attr_find(die
, attr
)) == NULL
) {
50 DWARF_SET_ERROR(dbg
, error
, DW_DLE_NO_ENTRY
);
51 return (DW_DLV_NO_ENTRY
);
60 dwarf_attrlist(Dwarf_Die die
, Dwarf_Attribute
**attrbuf
,
61 Dwarf_Signed
*attrcount
, Dwarf_Error
*error
)
67 dbg
= die
!= NULL
? die
->die_dbg
: NULL
;
69 if (die
== NULL
|| attrbuf
== NULL
|| attrcount
== NULL
) {
70 DWARF_SET_ERROR(dbg
, error
, DW_DLE_ARGUMENT
);
71 return (DW_DLV_ERROR
);
74 if (die
->die_ab
->ab_atnum
== 0) {
75 DWARF_SET_ERROR(dbg
, error
, DW_DLE_NO_ENTRY
);
76 return (DW_DLV_NO_ENTRY
);
79 *attrcount
= die
->die_ab
->ab_atnum
;
81 if (die
->die_attrarray
!= NULL
) {
82 *attrbuf
= die
->die_attrarray
;
86 if ((die
->die_attrarray
= malloc(*attrcount
* sizeof(Dwarf_Attribute
)))
88 DWARF_SET_ERROR(dbg
, error
, DW_DLE_MEMORY
);
89 return (DW_DLV_ERROR
);
92 for (i
= 0, at
= STAILQ_FIRST(&die
->die_attr
);
93 i
< *attrcount
&& at
!= NULL
; i
++, at
= STAILQ_NEXT(at
, at_next
))
94 die
->die_attrarray
[i
] = at
;
96 *attrbuf
= die
->die_attrarray
;
102 dwarf_hasattr(Dwarf_Die die
, Dwarf_Half attr
, Dwarf_Bool
*ret_bool
,
107 dbg
= die
!= NULL
? die
->die_dbg
: NULL
;
109 if (die
== NULL
|| ret_bool
== NULL
) {
110 DWARF_SET_ERROR(dbg
, error
, DW_DLE_ARGUMENT
);
111 return (DW_DLV_ERROR
);
114 *ret_bool
= (_dwarf_attr_find(die
, attr
) != NULL
);
120 dwarf_lowpc(Dwarf_Die die
, Dwarf_Addr
*ret_lowpc
, Dwarf_Error
*error
)
125 dbg
= die
!= NULL
? die
->die_dbg
: NULL
;
127 if (die
== NULL
|| ret_lowpc
== NULL
) {
128 DWARF_SET_ERROR(dbg
, error
, DW_DLE_ARGUMENT
);
129 return (DW_DLV_ERROR
);
132 if ((at
= _dwarf_attr_find(die
, DW_AT_low_pc
)) == NULL
) {
133 DWARF_SET_ERROR(dbg
, error
, DW_DLE_NO_ENTRY
);
134 return (DW_DLV_NO_ENTRY
);
137 *ret_lowpc
= at
->u
[0].u64
;
143 dwarf_highpc(Dwarf_Die die
, Dwarf_Addr
*ret_highpc
, Dwarf_Error
*error
)
148 dbg
= die
!= NULL
? die
->die_dbg
: NULL
;
150 if (die
== NULL
|| ret_highpc
== NULL
) {
151 DWARF_SET_ERROR(dbg
, error
, DW_DLE_ARGUMENT
);
152 return (DW_DLV_ERROR
);
155 if ((at
= _dwarf_attr_find(die
, DW_AT_high_pc
)) == NULL
) {
156 DWARF_SET_ERROR(dbg
, error
, DW_DLE_NO_ENTRY
);
157 return (DW_DLV_NO_ENTRY
);
160 *ret_highpc
= at
->u
[0].u64
;
166 dwarf_bytesize(Dwarf_Die die
, Dwarf_Unsigned
*ret_size
, Dwarf_Error
*error
)
171 dbg
= die
!= NULL
? die
->die_dbg
: NULL
;
173 if (die
== NULL
|| ret_size
== NULL
) {
174 DWARF_SET_ERROR(dbg
, error
, DW_DLE_ARGUMENT
);
175 return (DW_DLV_ERROR
);
178 if ((at
= _dwarf_attr_find(die
, DW_AT_byte_size
)) == NULL
) {
179 DWARF_SET_ERROR(dbg
, error
, DW_DLE_NO_ENTRY
);
180 return (DW_DLV_NO_ENTRY
);
183 *ret_size
= at
->u
[0].u64
;
189 dwarf_bitsize(Dwarf_Die die
, Dwarf_Unsigned
*ret_size
, Dwarf_Error
*error
)
194 dbg
= die
!= NULL
? die
->die_dbg
: NULL
;
196 if (die
== NULL
|| ret_size
== NULL
) {
197 DWARF_SET_ERROR(dbg
, error
, DW_DLE_ARGUMENT
);
198 return (DW_DLV_ERROR
);
201 if ((at
= _dwarf_attr_find(die
, DW_AT_bit_size
)) == NULL
) {
202 DWARF_SET_ERROR(dbg
, error
, DW_DLE_NO_ENTRY
);
203 return (DW_DLV_NO_ENTRY
);
206 *ret_size
= at
->u
[0].u64
;
212 dwarf_bitoffset(Dwarf_Die die
, Dwarf_Unsigned
*ret_size
, Dwarf_Error
*error
)
217 dbg
= die
!= NULL
? die
->die_dbg
: NULL
;
219 if (die
== NULL
|| ret_size
== NULL
) {
220 DWARF_SET_ERROR(dbg
, error
, DW_DLE_ARGUMENT
);
221 return (DW_DLV_ERROR
);
224 if ((at
= _dwarf_attr_find(die
, DW_AT_bit_offset
)) == NULL
) {
225 DWARF_SET_ERROR(dbg
, error
, DW_DLE_NO_ENTRY
);
226 return (DW_DLV_NO_ENTRY
);
229 *ret_size
= at
->u
[0].u64
;
235 dwarf_srclang(Dwarf_Die die
, Dwarf_Unsigned
*ret_lang
, Dwarf_Error
*error
)
240 dbg
= die
!= NULL
? die
->die_dbg
: NULL
;
242 if (die
== NULL
|| ret_lang
== NULL
) {
243 DWARF_SET_ERROR(dbg
, error
, DW_DLE_ARGUMENT
);
244 return (DW_DLV_ERROR
);
247 if ((at
= _dwarf_attr_find(die
, DW_AT_language
)) == NULL
) {
248 DWARF_SET_ERROR(dbg
, error
, DW_DLE_NO_ENTRY
);
249 return (DW_DLV_NO_ENTRY
);
252 *ret_lang
= at
->u
[0].u64
;
258 dwarf_arrayorder(Dwarf_Die die
, Dwarf_Unsigned
*ret_order
, Dwarf_Error
*error
)
263 dbg
= die
!= NULL
? die
->die_dbg
: NULL
;
265 if (die
== NULL
|| ret_order
== NULL
) {
266 DWARF_SET_ERROR(dbg
, error
, DW_DLE_ARGUMENT
);
267 return (DW_DLV_ERROR
);
270 if ((at
= _dwarf_attr_find(die
, DW_AT_ordering
)) == NULL
) {
271 DWARF_SET_ERROR(dbg
, error
, DW_DLE_NO_ENTRY
);
272 return (DW_DLV_NO_ENTRY
);
275 *ret_order
= at
->u
[0].u64
;