Path: blob/main/contrib/elftoolchain/libdwarf/dwarf_attrval.c
39483 views
/*-1* Copyright (c) 2007 John Birrell ([email protected])2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include "_libdwarf.h"2728ELFTC_VCSID("$Id: dwarf_attrval.c 3509 2016-12-29 03:58:41Z emaste $");2930int31dwarf_attrval_flag(Dwarf_Die die, Dwarf_Half attr, Dwarf_Bool *valp, Dwarf_Error *err)32{33Dwarf_Attribute at;34Dwarf_Debug dbg;3536dbg = die != NULL ? die->die_dbg : NULL;3738if (die == NULL || valp == NULL) {39DWARF_SET_ERROR(dbg, err, DW_DLE_ARGUMENT);40return (DW_DLV_ERROR);41}4243*valp = 0;4445if ((at = _dwarf_attr_find(die, attr)) == NULL) {46DWARF_SET_ERROR(dbg, err, DW_DLE_NO_ENTRY);47return (DW_DLV_NO_ENTRY);48}4950switch (at->at_form) {51case DW_FORM_flag:52case DW_FORM_flag_present:53*valp = (Dwarf_Bool) (!!at->u[0].u64);54break;55default:56DWARF_SET_ERROR(dbg, err, DW_DLE_ATTR_FORM_BAD);57return (DW_DLV_ERROR);58}5960return (DW_DLV_OK);61}6263int64dwarf_attrval_string(Dwarf_Die die, Dwarf_Half attr, const char **strp, Dwarf_Error *err)65{66Dwarf_Attribute at;67Dwarf_Debug dbg;6869dbg = die != NULL ? die->die_dbg : NULL;7071if (die == NULL || strp == NULL) {72DWARF_SET_ERROR(dbg, err, DW_DLE_ARGUMENT);73return (DW_DLV_ERROR);74}7576*strp = NULL;7778if ((at = _dwarf_attr_find(die, attr)) == NULL) {79DWARF_SET_ERROR(dbg, err, DW_DLE_NO_ENTRY);80return (DW_DLV_NO_ENTRY);81}8283switch (at->at_form) {84case DW_FORM_strp:85*strp = at->u[1].s;86break;87case DW_FORM_string:88*strp = at->u[0].s;89break;90default:91DWARF_SET_ERROR(dbg, err, DW_DLE_ATTR_FORM_BAD);92return (DW_DLV_ERROR);93}9495return (DW_DLV_OK);96}9798int99dwarf_attrval_signed(Dwarf_Die die, Dwarf_Half attr, Dwarf_Signed *valp, Dwarf_Error *err)100{101Dwarf_Attribute at;102Dwarf_Debug dbg;103104dbg = die != NULL ? die->die_dbg : NULL;105106if (die == NULL || valp == NULL) {107DWARF_SET_ERROR(dbg, err, DW_DLE_ARGUMENT);108return (DW_DLV_ERROR);109}110111*valp = 0;112113if ((at = _dwarf_attr_find(die, attr)) == NULL) {114DWARF_SET_ERROR(dbg, err, DW_DLE_NO_ENTRY);115return (DW_DLV_NO_ENTRY);116}117118switch (at->at_form) {119case DW_FORM_data1:120*valp = (int8_t) at->u[0].s64;121break;122case DW_FORM_data2:123*valp = (int16_t) at->u[0].s64;124break;125case DW_FORM_data4:126*valp = (int32_t) at->u[0].s64;127break;128case DW_FORM_data8:129case DW_FORM_sdata:130*valp = at->u[0].s64;131break;132default:133DWARF_SET_ERROR(dbg, err, DW_DLE_ATTR_FORM_BAD);134return (DW_DLV_ERROR);135}136137return (DW_DLV_OK);138}139140int141dwarf_attrval_unsigned(Dwarf_Die die, Dwarf_Half attr, Dwarf_Unsigned *valp, Dwarf_Error *err)142{143Dwarf_Attribute at;144Dwarf_Die die1;145Dwarf_Unsigned val;146Dwarf_Debug dbg;147int first;148149dbg = die != NULL ? die->die_dbg : NULL;150151if (die == NULL || valp == NULL) {152DWARF_SET_ERROR(dbg, err, DW_DLE_ARGUMENT);153return (DW_DLV_ERROR);154}155156*valp = 0;157158die1 = NULL;159for (;;) {160if ((at = _dwarf_attr_find(die, attr)) != NULL ||161attr != DW_AT_type)162break;163if ((at = _dwarf_attr_find(die, DW_AT_abstract_origin)) ==164NULL &&165(at = _dwarf_attr_find(die, DW_AT_specification)) == NULL)166break;167168switch (at->at_form) {169case DW_FORM_ref1:170case DW_FORM_ref2:171case DW_FORM_ref4:172case DW_FORM_ref8:173case DW_FORM_ref_udata:174val = at->u[0].u64;175first = (die1 == NULL);176die1 = _dwarf_die_find(die, val);177if (!first)178dwarf_dealloc(dbg, die, DW_DLA_DIE);179if (die1 == NULL) {180DWARF_SET_ERROR(dbg, err, DW_DLE_NO_ENTRY);181return (DW_DLV_NO_ENTRY);182}183die = die1;184break;185default:186DWARF_SET_ERROR(dbg, err, DW_DLE_ATTR_FORM_BAD);187return (DW_DLV_ERROR);188}189}190191if (at == NULL) {192DWARF_SET_ERROR(dbg, err, DW_DLE_NO_ENTRY);193return (DW_DLV_NO_ENTRY);194}195196switch (at->at_form) {197case DW_FORM_addr:198case DW_FORM_data1:199case DW_FORM_data2:200case DW_FORM_data4:201case DW_FORM_data8:202case DW_FORM_udata:203case DW_FORM_ref1:204case DW_FORM_ref2:205case DW_FORM_ref4:206case DW_FORM_ref8:207case DW_FORM_ref_udata:208*valp = at->u[0].u64;209break;210default:211if (die1 != NULL)212dwarf_dealloc(dbg, die1, DW_DLA_DIE);213DWARF_SET_ERROR(dbg, err, DW_DLE_ATTR_FORM_BAD);214return (DW_DLV_ERROR);215}216217if (die1 != NULL)218dwarf_dealloc(dbg, die1, DW_DLA_DIE);219220return (DW_DLV_OK);221}222223224