1 /* Return child of current DIE. 2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Red Hat, Inc. 3 This file is part of Red Hat elfutils. 4 Written by Ulrich Drepper <drepper (at) redhat.com>, 2003. 5 6 Red Hat elfutils is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by the 8 Free Software Foundation; version 2 of the License. 9 10 Red Hat elfutils is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 General Public License for more details. 14 15 You should have received a copy of the GNU General Public License along 16 with Red Hat elfutils; if not, write to the Free Software Foundation, 17 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. 18 19 In addition, as a special exception, Red Hat, Inc. gives You the 20 additional right to link the code of Red Hat elfutils with code licensed 21 under any Open Source Initiative certified open source license 22 (http://www.opensource.org/licenses/index.php) which requires the 23 distribution of source code with any binary distribution and to 24 distribute linked combinations of the two. Non-GPL Code permitted under 25 this exception must only link to the code of Red Hat elfutils through 26 those well defined interfaces identified in the file named EXCEPTION 27 found in the source code files (the "Approved Interfaces"). The files 28 of Non-GPL Code may instantiate templates or use macros or inline 29 functions from the Approved Interfaces without causing the resulting 30 work to be covered by the GNU General Public License. Only Red Hat, 31 Inc. may make changes or additions to the list of Approved Interfaces. 32 Red Hat's grant of this exception is conditioned upon your not adding 33 any new exceptions. If you wish to add a new Approved Interface or 34 exception, please contact Red Hat. You must obey the GNU General Public 35 License in all respects for all of the Red Hat elfutils code and other 36 code used in conjunction with Red Hat elfutils except the Non-GPL Code 37 covered by this exception. If you modify this file, you may extend this 38 exception to your version of the file, but you are not obligated to do 39 so. If you do not wish to provide this exception without modification, 40 you must delete this exception statement from your version and license 41 this file solely under the GPL without exception. 42 43 Red Hat elfutils is an included package of the Open Invention Network. 44 An included package of the Open Invention Network is a package for which 45 Open Invention Network licensees cross-license their patents. No patent 46 license is granted, either expressly or impliedly, by designation as an 47 included package. Should you wish to participate in the Open Invention 48 Network licensing program, please visit www.openinventionnetwork.com 49 <http://www.openinventionnetwork.com>. */ 50 51 #ifdef HAVE_CONFIG_H 52 # include <config.h> 53 #endif 54 55 #include "libdwP.h" 56 #include <string.h> 57 58 /* Some arbitrary value not conflicting with any existing code. */ 59 #define INVALID 0xffffe444 60 61 62 unsigned char * 63 internal_function 64 __libdw_find_attr (Dwarf_Die *die, unsigned int search_name, 65 unsigned int *codep, unsigned int *formp) 66 { 67 Dwarf *dbg = die->cu->dbg; 68 const unsigned char *readp = (unsigned char *) die->addr; 69 70 /* First we have to get the abbreviation code so that we can decode 71 the data in the DIE. */ 72 unsigned int abbrev_code; 73 get_uleb128 (abbrev_code, readp); 74 75 /* Find the abbreviation entry. */ 76 Dwarf_Abbrev *abbrevp = die->abbrev; 77 if (abbrevp == NULL) 78 { 79 abbrevp = __libdw_findabbrev (die->cu, abbrev_code); 80 die->abbrev = abbrevp ?: DWARF_END_ABBREV; 81 } 82 if (unlikely (die->abbrev == DWARF_END_ABBREV)) 83 { 84 invalid_dwarf: 85 __libdw_seterrno (DWARF_E_INVALID_DWARF); 86 return NULL; 87 } 88 89 /* Search the name attribute. */ 90 unsigned char *const endp 91 = ((unsigned char *) dbg->sectiondata[IDX_debug_abbrev]->d_buf 92 + dbg->sectiondata[IDX_debug_abbrev]->d_size); 93 94 const unsigned char *attrp = die->abbrev->attrp; 95 while (1) 96 { 97 /* Are we still in bounds? This test needs to be refined. */ 98 if (unlikely (attrp + 1 >= endp)) 99 goto invalid_dwarf; 100 101 /* Get attribute name and form. 102 103 XXX We don't check whether this reads beyond the end of the 104 section. */ 105 unsigned int attr_name; 106 get_uleb128 (attr_name, attrp); 107 unsigned int attr_form; 108 get_uleb128 (attr_form, attrp); 109 110 /* We can stop if we found the attribute with value zero. */ 111 if (attr_name == 0 && attr_form == 0) 112 break; 113 114 /* Is this the name attribute? */ 115 if (attr_name == search_name && search_name != INVALID) 116 { 117 if (codep != NULL) 118 *codep = attr_name; 119 if (formp != NULL) 120 *formp = attr_form; 121 122 return (unsigned char *) readp; 123 } 124 125 /* Skip over the rest of this attribute (if there is any). */ 126 if (attr_form != 0) 127 { 128 size_t len = __libdw_form_val_len (dbg, die->cu, attr_form, readp); 129 130 if (unlikely (len == (size_t) -1l)) 131 { 132 readp = NULL; 133 break; 134 } 135 136 // XXX We need better boundary checks. 137 readp += len; 138 } 139 } 140 141 // XXX Do we need other values? 142 if (codep != NULL) 143 *codep = INVALID; 144 if (formp != NULL) 145 *formp = INVALID; 146 147 return (unsigned char *) readp; 148 } 149 150 151 int 152 dwarf_child (die, result) 153 Dwarf_Die *die; 154 Dwarf_Die *result; 155 { 156 /* Ignore previous errors. */ 157 if (die == NULL) 158 return -1; 159 160 /* Skip past the last attribute. */ 161 void *addr = NULL; 162 163 /* If we already know there are no children do not search. */ 164 if (die->abbrev != DWARF_END_ABBREV 165 && (die->abbrev == NULL || die->abbrev->has_children)) 166 addr = __libdw_find_attr (die, INVALID, NULL, NULL); 167 if (unlikely (die->abbrev == (Dwarf_Abbrev *) -1l)) 168 return -1; 169 170 /* Make sure the DIE really has children. */ 171 if (! die->abbrev->has_children) 172 /* There cannot be any children. */ 173 return 1; 174 175 if (addr == NULL) 176 return -1; 177 178 /* It's kosher (just suboptimal) to have a null entry first thing (7.5.3). 179 So if this starts with ULEB128 of 0 (even with silly encoding of 0), 180 it is a kosher null entry and we do not really have any children. */ 181 const unsigned char *code = addr; 182 while (unlikely (*code == 0x80)) 183 ++code; 184 if (unlikely (*code == '\0')) 185 return 1; 186 187 /* RESULT can be the same as DIE. So preserve what we need. */ 188 struct Dwarf_CU *cu = die->cu; 189 190 /* Clear the entire DIE structure. This signals we have not yet 191 determined any of the information. */ 192 memset (result, '\0', sizeof (Dwarf_Die)); 193 194 /* We have the address. */ 195 result->addr = addr; 196 197 /* Same CU as the parent. */ 198 result->cu = cu; 199 200 return 0; 201 } 202 INTDEF(dwarf_child) 203