1/* $NetBSD: libdwarf_abbrev.c,v 1.5 2024/03/03 17:37:32 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.5 2024/03/03 17:37:32 christos Exp $"); 33ELFTC_VCSID("Id: libdwarf_abbrev.c 4008 2023-10-12 18:17:06Z kaiwang27"); 34 35int 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 *abp = ab; 66 return (DW_DLE_NONE); 67} 68 69int 70_dwarf_attrdef_add(Dwarf_Debug dbg, Dwarf_Abbrev ab, uint64_t attr, 71 uint64_t form, int64_t ic, uint64_t adoff, Dwarf_AttrDef *adp, 72 Dwarf_Error *error) 73{ 74 Dwarf_AttrDef ad; 75 76 if (ab == NULL) { 77 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); 78 return (DW_DLE_ARGUMENT); 79 } 80 81 if ((ad = malloc(sizeof(struct _Dwarf_AttrDef))) == NULL) { 82 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 83 return (DW_DLE_MEMORY); 84 } 85 86 /* Initialise the attribute definition structure. */ 87 ad->ad_attrib = attr; 88 ad->ad_form = form; 89 ad->ad_const = ic; 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 104int 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 int64_t ic; 116 uint8_t children; 117 int ret; 118 119 assert(abp != NULL); 120 121 ds = _dwarf_find_section(dbg, ".debug_abbrev"); 122 if (ds == NULL || *offset >= ds->ds_size) 123 return (DW_DLE_NO_ENTRY); 124 125 aboff = *offset; 126 127 entry = _dwarf_read_uleb128(ds->ds_data, offset); 128 if (entry == 0) { 129 /* Last entry. */ 130 ret = _dwarf_abbrev_add(cu, entry, 0, 0, aboff, abp, 131 error); 132 if (ret == DW_DLE_NONE) { 133 (*abp)->ab_length = 1; 134 return (ret); 135 } else 136 return (ret); 137 } 138 tag = _dwarf_read_uleb128(ds->ds_data, offset); 139 children = dbg->read(ds->ds_data, offset, 1); 140 if ((ret = _dwarf_abbrev_add(cu, entry, tag, children, aboff, 141 abp, error)) != DW_DLE_NONE) 142 return (ret); 143 144 /* Parse attribute definitions. */ 145 do { 146 adoff = *offset; 147 attr = _dwarf_read_uleb128(ds->ds_data, offset); 148 form = _dwarf_read_uleb128(ds->ds_data, offset); 149 if (form == DW_FORM_implicit_const) { 150 /* 151 * DWARF5 7.5.3: atrribute definition with the form 152 * DW_FORM_implicit_const contains a third part, a 153 * signed LEB128 value indicating a constant value. 154 * No value is needed to store in the .debug_info 155 * as a result. 156 */ 157 ic = _dwarf_read_sleb128(ds->ds_data, offset); 158 } 159 if (attr != 0) 160 if ((ret = _dwarf_attrdef_add(dbg, *abp, attr, 161 form, ic, adoff, NULL, error)) != DW_DLE_NONE) 162 return (ret); 163 } while (attr != 0); 164 165 (*abp)->ab_length = *offset - aboff; 166 167 return (ret); 168} 169 170int 171_dwarf_abbrev_find(Dwarf_CU cu, uint64_t entry, Dwarf_Abbrev *abp, 172 Dwarf_Error *error) 173{ 174 Dwarf_Abbrev ab; 175 Dwarf_Section *ds; 176 Dwarf_Unsigned offset; 177 int ret; 178 179 if (entry == 0) 180 return (DW_DLE_NO_ENTRY); 181 182 /* Check if the desired abbrev entry is already in the hash table. */ 183 HASH_FIND(ab_hh, cu->cu_abbrev_hash, &entry, sizeof(entry), ab); 184 if (ab != NULL) { 185 *abp = ab; 186 return (DW_DLE_NONE); 187 } 188 189 if (cu->cu_abbrev_loaded) { 190 return (DW_DLE_NO_ENTRY); 191 } 192 193 /* Load and search the abbrev table. */ 194 ds = _dwarf_find_section(cu->cu_dbg, ".debug_abbrev"); 195 if (ds == NULL) 196 return (DW_DLE_NO_ENTRY); 197 198 offset = cu->cu_abbrev_offset_cur; 199 while (offset < ds->ds_size) { 200 ret = _dwarf_abbrev_parse(cu->cu_dbg, cu, &offset, &ab, error); 201 if (ret != DW_DLE_NONE) 202 return (ret); 203 if (ab->ab_entry == entry) { 204 cu->cu_abbrev_offset_cur = offset; 205 *abp = ab; 206 return (DW_DLE_NONE); 207 } 208 if (ab->ab_entry == 0) { 209 cu->cu_abbrev_offset_cur = offset; 210 cu->cu_abbrev_loaded = 1; 211 break; 212 } 213 } 214 215 return (DW_DLE_NO_ENTRY); 216} 217 218void 219_dwarf_abbrev_cleanup(Dwarf_CU cu) 220{ 221 Dwarf_Abbrev ab, tab; 222 Dwarf_AttrDef ad, tad; 223 224 assert(cu != NULL); 225 226 HASH_ITER(ab_hh, cu->cu_abbrev_hash, ab, tab) { 227 HASH_DELETE(ab_hh, cu->cu_abbrev_hash, ab); 228 STAILQ_FOREACH_SAFE(ad, &ab->ab_attrdef, ad_next, tad) { 229 STAILQ_REMOVE(&ab->ab_attrdef, ad, _Dwarf_AttrDef, 230 ad_next); 231 free(ad); 232 } 233 free(ab); 234 } 235} 236 237int 238_dwarf_abbrev_gen(Dwarf_P_Debug dbg, Dwarf_Error *error) 239{ 240 Dwarf_CU cu; 241 Dwarf_Abbrev ab; 242 Dwarf_AttrDef ad; 243 Dwarf_P_Section ds; 244 int ret; 245 246 cu = STAILQ_FIRST(&dbg->dbg_cu); 247 if (cu == NULL) 248 return (DW_DLE_NONE); 249 250 /* Create .debug_abbrev section. */ 251 if ((ret = _dwarf_section_init(dbg, &ds, ".debug_abbrev", 0, error)) != 252 DW_DLE_NONE) 253 return (ret); 254 255 for (ab = cu->cu_abbrev_hash; ab != NULL; ab = ab->ab_hh.next) { 256 RCHECK(WRITE_ULEB128(ab->ab_entry)); 257 RCHECK(WRITE_ULEB128(ab->ab_tag)); 258 RCHECK(WRITE_VALUE(ab->ab_children, 1)); 259 STAILQ_FOREACH(ad, &ab->ab_attrdef, ad_next) { 260 RCHECK(WRITE_ULEB128(ad->ad_attrib)); 261 RCHECK(WRITE_ULEB128(ad->ad_form)); 262 } 263 /* Signal end of attribute spec list. */ 264 RCHECK(WRITE_ULEB128(0)); 265 RCHECK(WRITE_ULEB128(0)); 266 } 267 /* End of abbreviation for this CU. */ 268 RCHECK(WRITE_ULEB128(0)); 269 270 /* Notify the creation of .debug_abbrev ELF section. */ 271 RCHECK(_dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error)); 272 273 return (DW_DLE_NONE); 274 275gen_fail: 276 277 _dwarf_section_free(dbg, &ds); 278 279 return (ret); 280} 281