2 * Copyright (c) 2007 John Birrell (jb@freebsd.org)
3 * Copyright (c) 2009,2010 Kai Wang
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include "_libdwarf.h"
30 ELFTC_VCSID("$Id: dwarf_form.c 2073 2011-10-27 03:30:47Z jkoshy $");
33 dwarf_hasform(Dwarf_Attribute at, Dwarf_Half form, Dwarf_Bool *return_hasform,
38 dbg = at != NULL ? at->at_die->die_dbg : NULL;
40 if (at == NULL || return_hasform == NULL) {
41 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
42 return (DW_DLV_ERROR);
45 *return_hasform = (at->at_form == form);
51 dwarf_whatform(Dwarf_Attribute at, Dwarf_Half *return_form, Dwarf_Error *error)
55 dbg = at != NULL ? at->at_die->die_dbg : NULL;
57 if (at == NULL || return_form == NULL) {
58 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
59 return (DW_DLV_ERROR);
62 *return_form = at->at_form;
68 dwarf_whatform_direct(Dwarf_Attribute at, Dwarf_Half *return_form,
73 dbg = at != NULL ? at->at_die->die_dbg : NULL;
75 if (at == NULL || return_form == NULL) {
76 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
77 return (DW_DLV_ERROR);
81 *return_form = DW_FORM_indirect;
83 *return_form = (Dwarf_Half) at->at_form;
89 dwarf_whatattr(Dwarf_Attribute at, Dwarf_Half *return_attr, Dwarf_Error *error)
93 dbg = at != NULL ? at->at_die->die_dbg : NULL;
95 if (at == NULL || return_attr == NULL) {
96 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
97 return (DW_DLV_ERROR);
100 *return_attr = (Dwarf_Half) at->at_attrib;
106 dwarf_formref(Dwarf_Attribute at, Dwarf_Off *return_offset, Dwarf_Error *error)
111 dbg = at != NULL ? at->at_die->die_dbg : NULL;
113 if (at == NULL || return_offset == NULL) {
114 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
115 return (DW_DLV_ERROR);
118 switch (at->at_form) {
123 case DW_FORM_ref_udata:
124 *return_offset = (Dwarf_Off) at->u[0].u64;
128 DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
136 dwarf_global_formref(Dwarf_Attribute at, Dwarf_Off *return_offset,
142 dbg = at != NULL ? at->at_die->die_dbg : NULL;
144 if (at == NULL || return_offset == NULL) {
145 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
146 return (DW_DLV_ERROR);
149 switch (at->at_form) {
150 case DW_FORM_ref_addr:
151 case DW_FORM_sec_offset:
152 *return_offset = (Dwarf_Off) at->u[0].u64;
159 case DW_FORM_ref_udata:
160 *return_offset = (Dwarf_Off) at->u[0].u64 +
161 at->at_die->die_cu->cu_offset;
165 DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
173 dwarf_formaddr(Dwarf_Attribute at, Dwarf_Addr *return_addr, Dwarf_Error *error)
178 dbg = at != NULL ? at->at_die->die_dbg : NULL;
180 if (at == NULL || return_addr == NULL) {
181 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
182 return (DW_DLV_ERROR);
185 if (at->at_form == DW_FORM_addr) {
186 *return_addr = at->u[0].u64;
189 DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
197 dwarf_formflag(Dwarf_Attribute at, Dwarf_Bool *return_bool, Dwarf_Error *error)
202 dbg = at != NULL ? at->at_die->die_dbg : NULL;
204 if (at == NULL || return_bool == NULL) {
205 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
206 return (DW_DLV_ERROR);
209 if (at->at_form == DW_FORM_flag ||
210 at->at_form == DW_FORM_flag_present) {
211 *return_bool = (Dwarf_Bool) (!!at->u[0].u64);
214 DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
222 dwarf_formudata(Dwarf_Attribute at, Dwarf_Unsigned *return_uvalue,
228 dbg = at != NULL ? at->at_die->die_dbg : NULL;
230 if (at == NULL || return_uvalue == NULL) {
231 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
232 return (DW_DLV_ERROR);
235 switch (at->at_form) {
241 *return_uvalue = at->u[0].u64;
245 DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
253 dwarf_formsdata(Dwarf_Attribute at, Dwarf_Signed *return_svalue,
259 dbg = at != NULL ? at->at_die->die_dbg : NULL;
261 if (at == NULL || return_svalue == NULL) {
262 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
263 return (DW_DLV_ERROR);
266 switch (at->at_form) {
268 *return_svalue = (int8_t) at->u[0].s64;
272 *return_svalue = (int16_t) at->u[0].s64;
276 *return_svalue = (int32_t) at->u[0].s64;
281 *return_svalue = at->u[0].s64;
285 DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
293 dwarf_formblock(Dwarf_Attribute at, Dwarf_Block **return_block,
299 dbg = at != NULL ? at->at_die->die_dbg : NULL;
301 if (at == NULL || return_block == NULL) {
302 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
303 return (DW_DLV_ERROR);
306 switch (at->at_form) {
311 *return_block = &at->at_block;
315 DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
323 dwarf_formsig8(Dwarf_Attribute at, Dwarf_Sig8 *return_sig8, Dwarf_Error *error)
327 dbg = at != NULL ? at->at_die->die_dbg : NULL;
329 if (at == NULL || return_sig8 == NULL) {
330 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
331 return (DW_DLV_ERROR);
334 if (at->at_form != DW_FORM_ref_sig8) {
335 DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
336 return (DW_DLV_ERROR);
339 assert(at->u[0].u64 == 8);
340 memcpy(return_sig8->signature, at->u[1].u8p, at->u[0].u64);
346 dwarf_formexprloc(Dwarf_Attribute at, Dwarf_Unsigned *return_exprlen,
347 Dwarf_Ptr *return_expr, Dwarf_Error *error)
352 dbg = at != NULL ? at->at_die->die_dbg : NULL;
354 if (at == NULL || return_exprlen == NULL || return_expr == NULL) {
355 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
356 return (DW_DLV_ERROR);
359 if (at->at_form != DW_FORM_exprloc) {
360 DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
361 return (DW_DLV_ERROR);
364 *return_exprlen = at->u[0].u64;
365 *return_expr = (void *) at->u[1].u8p;
371 dwarf_formstring(Dwarf_Attribute at, char **return_string,
377 dbg = at != NULL ? at->at_die->die_dbg : NULL;
379 if (at == NULL || return_string == NULL) {
380 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
381 return (DW_DLV_ERROR);
384 switch (at->at_form) {
386 *return_string = (char *) at->u[0].s;
390 *return_string = (char *) at->u[1].s;
394 DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD);
401 enum Dwarf_Form_Class
402 dwarf_get_form_class(Dwarf_Half dwversion, Dwarf_Half attr,
403 Dwarf_Half offset_size, Dwarf_Half form)
408 return (DW_FORM_CLASS_ADDRESS);
413 return (DW_FORM_CLASS_BLOCK);
416 return (DW_FORM_CLASS_STRING);
418 case DW_FORM_flag_present:
419 return (DW_FORM_CLASS_FLAG);
420 case DW_FORM_ref_addr:
421 case DW_FORM_ref_sig8:
422 case DW_FORM_ref_udata:
427 return (DW_FORM_CLASS_REFERENCE);
428 case DW_FORM_exprloc:
429 return (DW_FORM_CLASS_EXPRLOC);
434 return (DW_FORM_CLASS_CONSTANT);
438 return (DW_FORM_CLASS_CONSTANT);
439 if (form == DW_FORM_data4 && offset_size != 4)
440 return (DW_FORM_CLASS_CONSTANT);
441 if (form == DW_FORM_data8 && offset_size != 8)
442 return (DW_FORM_CLASS_CONSTANT);
444 case DW_FORM_sec_offset:
446 * DW_FORM_data4 and DW_FORM_data8 can be used as
447 * offset/pointer before DWARF4. Newly added
448 * DWARF4 form DW_FORM_sec_offset intents to replace
449 * DW_FORM_data{4,8} for this purpose. Anyway, to
450 * determine the actual class for these forms, we need
451 * to also look at the attribute number.
455 case DW_AT_string_length:
456 case DW_AT_return_addr:
457 case DW_AT_data_member_location:
458 case DW_AT_frame_base:
460 case DW_AT_static_link:
461 case DW_AT_use_location:
462 case DW_AT_vtable_elem_location:
463 return (DW_FORM_CLASS_LOCLISTPTR);
464 case DW_AT_stmt_list:
465 return (DW_FORM_CLASS_LINEPTR);
466 case DW_AT_start_scope:
468 return (DW_FORM_CLASS_RANGELISTPTR);
469 case DW_AT_macro_info:
470 return (DW_FORM_CLASS_MACPTR);
472 if (form == DW_FORM_data4 || form == DW_FORM_data8)
473 return (DW_FORM_CLASS_CONSTANT);
475 return (DW_FORM_CLASS_UNKNOWN);
478 return (DW_FORM_CLASS_UNKNOWN);