1 /* Get attributes of the DIE. 2 Copyright (C) 2004, 2005, 2008, 2009, 2014 Red Hat, Inc. 3 This file is part of elfutils. 4 Written by Ulrich Drepper <drepper (at) redhat.com>, 2004. 5 6 This file is free software; you can redistribute it and/or modify 7 it under the terms of either 8 9 * the GNU Lesser General Public License as published by the Free 10 Software Foundation; either version 3 of the License, or (at 11 your option) any later version 12 13 or 14 15 * the GNU General Public License as published by the Free 16 Software Foundation; either version 2 of the License, or (at 17 your option) any later version 18 19 or both in parallel, as here. 20 21 elfutils is distributed in the hope that it will be useful, but 22 WITHOUT ANY WARRANTY; without even the implied warranty of 23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 General Public License for more details. 25 26 You should have received copies of the GNU General Public License and 27 the GNU Lesser General Public License along with this program. If 28 not, see <http://www.gnu.org/licenses/>. */ 29 30 #ifdef HAVE_CONFIG_H 31 # include <config.h> 32 #endif 33 34 #include "libdwP.h" 35 36 37 ptrdiff_t 38 dwarf_getattrs (Dwarf_Die *die, int (*callback) (Dwarf_Attribute *, void *), 39 void *arg, ptrdiff_t offset) 40 { 41 if (die == NULL) 42 return -1l; 43 44 if (unlikely (offset == 1)) 45 return 1; 46 47 const unsigned char *die_addr; 48 49 /* Find the abbreviation entry. */ 50 Dwarf_Abbrev *abbrevp = __libdw_dieabbrev (die, &die_addr); 51 52 if (unlikely (abbrevp == DWARF_END_ABBREV)) 53 { 54 invalid_dwarf: 55 __libdw_seterrno (DWARF_E_INVALID_DWARF); 56 return -1l; 57 } 58 59 /* This is where the attributes start. */ 60 const unsigned char *attrp = abbrevp->attrp; 61 const unsigned char *const offset_attrp = abbrevp->attrp + offset; 62 63 /* Go over the list of attributes. */ 64 Dwarf *dbg = die->cu->dbg; 65 const unsigned char *endp; 66 endp = ((const unsigned char *) dbg->sectiondata[IDX_debug_abbrev]->d_buf 67 + dbg->sectiondata[IDX_debug_abbrev]->d_size); 68 while (1) 69 { 70 /* Are we still in bounds? */ 71 if (unlikely (attrp >= endp)) 72 goto invalid_dwarf; 73 74 /* Get attribute name and form. */ 75 Dwarf_Attribute attr; 76 const unsigned char *remembered_attrp = attrp; 77 78 get_uleb128 (attr.code, attrp, endp); 79 if (unlikely (attrp >= endp)) 80 goto invalid_dwarf; 81 get_uleb128 (attr.form, attrp, endp); 82 83 /* We can stop if we found the attribute with value zero. */ 84 if (attr.code == 0 && attr.form == 0) 85 /* Do not return 0 here - there would be no way to 86 distinguish this value from the attribute at offset 0. 87 Instead we return +1 which would never be a valid 88 offset of an attribute. */ 89 return 1l; 90 91 /* If we are not to OFFSET_ATTRP yet, we just have to skip 92 the values of the intervening attributes. */ 93 if (remembered_attrp >= offset_attrp) 94 { 95 /* Fill in the rest. */ 96 attr.valp = (unsigned char *) die_addr; 97 attr.cu = die->cu; 98 99 /* Now call the callback function. */ 100 if (callback (&attr, arg) != DWARF_CB_OK) 101 /* Return the offset of the start of the attribute, so that 102 dwarf_getattrs() can be restarted from this point if the 103 caller so desires. */ 104 return remembered_attrp - abbrevp->attrp; 105 } 106 107 /* Skip over the rest of this attribute (if there is any). */ 108 if (attr.form != 0) 109 { 110 size_t len = __libdw_form_val_len (die->cu, attr.form, die_addr); 111 if (unlikely (len == (size_t) -1l)) 112 /* Something wrong with the file. */ 113 return -1l; 114 115 // __libdw_form_val_len will have done a bounds check. 116 die_addr += len; 117 } 118 } 119 /* NOTREACHED */ 120 } 121