1 /* $NetBSD: libdwarf_abbrev.c,v 1.3 2016/02/20 02:43:41 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 John Birrell (jb@freebsd.org) 5 * Copyright (c) 2009-2011 Kai Wang 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30 #include "_libdwarf.h" 31 32 __RCSID("$NetBSD: libdwarf_abbrev.c,v 1.3 2016/02/20 02:43:41 christos Exp $"); 33 ELFTC_VCSID("Id: libdwarf_abbrev.c 3136 2014-12-24 16:04:38Z kaiwang27 "); 34 35 int 36 _dwarf_abbrev_add(Dwarf_CU cu, uint64_t entry, uint64_t tag, uint8_t children, 37 uint64_t aboff, Dwarf_Abbrev *abp, Dwarf_Error *error) 38 { 39 Dwarf_Abbrev ab; 40 Dwarf_Debug dbg; 41 42 dbg = cu != NULL ? cu->cu_dbg : NULL; 43 44 if ((ab = malloc(sizeof(struct _Dwarf_Abbrev))) == NULL) { 45 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 46 return (DW_DLE_MEMORY); 47 } 48 49 /* Initialise the abbrev structure. */ 50 ab->ab_entry = entry; 51 ab->ab_tag = tag; 52 ab->ab_children = children; 53 ab->ab_offset = aboff; 54 ab->ab_length = 0; /* fill in later. */ 55 ab->ab_atnum = 0; /* fill in later. */ 56 57 /* Initialise the list of attribute definitions. */ 58 STAILQ_INIT(&ab->ab_attrdef); 59 60 /* Add the abbrev to the hash table of the compilation unit. */ 61 if (cu != NULL) 62 HASH_ADD(ab_hh, cu->cu_abbrev_hash, ab_entry, 63 sizeof(ab->ab_entry), ab); 64 65 if (abp != NULL) 66 *abp = ab; 67 68 return (DW_DLE_NONE); 69 } 70 71 int 72 _dwarf_attrdef_add(Dwarf_Debug dbg, Dwarf_Abbrev ab, uint64_t attr, 73 uint64_t form, uint64_t adoff, Dwarf_AttrDef *adp, Dwarf_Error *error) 74 { 75 Dwarf_AttrDef ad; 76 77 if (ab == NULL) { 78 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); 79 return (DW_DLE_ARGUMENT); 80 } 81 82 if ((ad = malloc(sizeof(struct _Dwarf_AttrDef))) == NULL) { 83 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 84 return (DW_DLE_MEMORY); 85 } 86 87 /* Initialise the attribute definition structure. */ 88 ad->ad_attrib = attr; 89 ad->ad_form = form; 90 ad->ad_offset = adoff; 91 92 /* Add the attribute definition to the list in the abbrev. */ 93 STAILQ_INSERT_TAIL(&ab->ab_attrdef, ad, ad_next); 94 95 /* Increase number of attribute counter. */ 96 ab->ab_atnum++; 97 98 if (adp != NULL) 99 *adp = ad; 100 101 return (DW_DLE_NONE); 102 } 103 104 int 105 _dwarf_abbrev_parse(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Unsigned *offset, 106 Dwarf_Abbrev *abp, Dwarf_Error *error) 107 { 108 Dwarf_Section *ds; 109 uint64_t attr; 110 uint64_t entry; 111 uint64_t form; 112 uint64_t aboff; 113 uint64_t adoff; 114 uint64_t tag; 115 uint8_t children; 116 int ret; 117 118 assert(abp != NULL); 119 120 ds = _dwarf_find_section(dbg, ".debug_abbrev"); 121 assert(ds != NULL); 122 123 if (*offset >= ds->ds_size) 124 return (DW_DLE_NO_ENTRY); 125 126 aboff = *offset; 127 128 entry = _dwarf_read_uleb128(ds->ds_data, offset); 129 if (entry == 0) { 130 /* Last entry. */ 131 ret = _dwarf_abbrev_add(cu, entry, 0, 0, aboff, abp, 132 error); 133 if (ret == DW_DLE_NONE) { 134 (*abp)->ab_length = 1; 135 return (ret); 136 } else 137 return (ret); 138 } 139 tag = _dwarf_read_uleb128(ds->ds_data, offset); 140 children = dbg->read(ds->ds_data, offset, 1); 141 if ((ret = _dwarf_abbrev_add(cu, entry, tag, children, aboff, 142 abp, error)) != DW_DLE_NONE) 143 return (ret); 144 145 /* Parse attribute definitions. */ 146 do { 147 adoff = *offset; 148 attr = _dwarf_read_uleb128(ds->ds_data, offset); 149 form = _dwarf_read_uleb128(ds->ds_data, offset); 150 if (attr != 0) 151 if ((ret = _dwarf_attrdef_add(dbg, *abp, attr, 152 form, adoff, NULL, error)) != DW_DLE_NONE) 153 return (ret); 154 } while (attr != 0); 155 156 (*abp)->ab_length = *offset - aboff; 157 158 return (ret); 159 } 160 161 int 162 _dwarf_abbrev_find(Dwarf_CU cu, uint64_t entry, Dwarf_Abbrev *abp, 163 Dwarf_Error *error) 164 { 165 Dwarf_Abbrev ab; 166 Dwarf_Section *ds; 167 Dwarf_Unsigned offset; 168 int ret; 169 170 if (entry == 0) 171 return (DW_DLE_NO_ENTRY); 172 173 /* Check if the desired abbrev entry is already in the hash table. */ 174 HASH_FIND(ab_hh, cu->cu_abbrev_hash, &entry, sizeof(entry), ab); 175 if (ab != NULL) { 176 *abp = ab; 177 return (DW_DLE_NONE); 178 } 179 180 if (cu->cu_abbrev_loaded) { 181 return (DW_DLE_NO_ENTRY); 182 } 183 184 /* Load and search the abbrev table. */ 185 ds = _dwarf_find_section(cu->cu_dbg, ".debug_abbrev"); 186 if (ds == NULL) 187 return (DW_DLE_NO_ENTRY); 188 189 offset = cu->cu_abbrev_offset_cur; 190 while (offset < ds->ds_size) { 191 ret = _dwarf_abbrev_parse(cu->cu_dbg, cu, &offset, &ab, error); 192 if (ret != DW_DLE_NONE) 193 return (ret); 194 if (ab->ab_entry == entry) { 195 cu->cu_abbrev_offset_cur = offset; 196 *abp = ab; 197 return (DW_DLE_NONE); 198 } 199 if (ab->ab_entry == 0) { 200 cu->cu_abbrev_offset_cur = offset; 201 cu->cu_abbrev_loaded = 1; 202 break; 203 } 204 } 205 206 return (DW_DLE_NO_ENTRY); 207 } 208 209 void 210 _dwarf_abbrev_cleanup(Dwarf_CU cu) 211 { 212 Dwarf_Abbrev ab, tab; 213 Dwarf_AttrDef ad, tad; 214 215 assert(cu != NULL); 216 217 HASH_ITER(ab_hh, cu->cu_abbrev_hash, ab, tab) { 218 HASH_DELETE(ab_hh, cu->cu_abbrev_hash, ab); 219 STAILQ_FOREACH_SAFE(ad, &ab->ab_attrdef, ad_next, tad) { 220 STAILQ_REMOVE(&ab->ab_attrdef, ad, _Dwarf_AttrDef, 221 ad_next); 222 free(ad); 223 } 224 free(ab); 225 } 226 } 227 228 int 229 _dwarf_abbrev_gen(Dwarf_P_Debug dbg, Dwarf_Error *error) 230 { 231 Dwarf_CU cu; 232 Dwarf_Abbrev ab; 233 Dwarf_AttrDef ad; 234 Dwarf_P_Section ds; 235 int ret; 236 237 cu = STAILQ_FIRST(&dbg->dbg_cu); 238 if (cu == NULL) 239 return (DW_DLE_NONE); 240 241 /* Create .debug_abbrev section. */ 242 if ((ret = _dwarf_section_init(dbg, &ds, ".debug_abbrev", 0, error)) != 243 DW_DLE_NONE) 244 return (ret); 245 246 for (ab = cu->cu_abbrev_hash; ab != NULL; ab = ab->ab_hh.next) { 247 RCHECK(WRITE_ULEB128(ab->ab_entry)); 248 RCHECK(WRITE_ULEB128(ab->ab_tag)); 249 RCHECK(WRITE_VALUE(ab->ab_children, 1)); 250 STAILQ_FOREACH(ad, &ab->ab_attrdef, ad_next) { 251 RCHECK(WRITE_ULEB128(ad->ad_attrib)); 252 RCHECK(WRITE_ULEB128(ad->ad_form)); 253 } 254 /* Signal end of attribute spec list. */ 255 RCHECK(WRITE_ULEB128(0)); 256 RCHECK(WRITE_ULEB128(0)); 257 } 258 /* End of abbreviation for this CU. */ 259 RCHECK(WRITE_ULEB128(0)); 260 261 /* Notify the creation of .debug_abbrev ELF section. */ 262 RCHECK(_dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error)); 263 264 return (DW_DLE_NONE); 265 266 gen_fail: 267 268 _dwarf_section_free(dbg, &ds); 269 270 return (ret); 271 } 272