1 /* $NetBSD: libdwarf_str.c,v 1.6 2026/05/17 21:40:49 jkoshy Exp $ */ 2 3 /*- 4 * Copyright (c) 2009,2010,2023 Kai Wang 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include "_libdwarf.h" 30 31 __RCSID("$NetBSD: libdwarf_str.c,v 1.6 2026/05/17 21:40:49 jkoshy Exp $"); 32 ELFTC_VCSID("Id: libdwarf_str.c 4039 2024-03-15 04:07:32Z kaiwang27"); 33 34 #define _INIT_DWARF_STRTAB_SIZE 1024 35 36 int 37 _dwarf_strtab_add(Dwarf_Debug dbg, char *string, uint64_t *off, 38 Dwarf_Error *error) 39 { 40 size_t len; 41 42 assert(dbg != NULL && string != NULL); 43 44 len = strlen(string) + 1; 45 while (dbg->dbg_strtab_size + len > dbg->dbg_strtab_cap) { 46 dbg->dbg_strtab_cap *= 2; 47 dbg->dbg_strtab = realloc(dbg->dbg_strtab, 48 (size_t) dbg->dbg_strtab_cap); 49 if (dbg->dbg_strtab == NULL) { 50 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 51 return (DW_DLE_MEMORY); 52 } 53 } 54 55 if (off != NULL) 56 *off = dbg->dbg_strtab_size; 57 58 memcpy(&dbg->dbg_strtab[dbg->dbg_strtab_size], string, len - 1); 59 dbg->dbg_strtab_size += len; 60 dbg->dbg_strtab[dbg->dbg_strtab_size - 1] = '\0'; 61 62 return (DW_DLE_NONE); 63 } 64 65 char * 66 _dwarf_strtab_get_table(Dwarf_Debug dbg) 67 { 68 69 assert(dbg != NULL); 70 71 return (dbg->dbg_strtab); 72 } 73 74 char * 75 _dwarf_strtab_get_line_table(Dwarf_Debug dbg) 76 { 77 78 assert(dbg != NULL); 79 80 return (dbg->dbg_line_strtab); 81 } 82 83 static int 84 _dwarf_str_offsets_init(Dwarf_Debug dbg, Dwarf_Error *error) 85 { 86 Dwarf_Section *ds; 87 Dwarf_StrOffsets *str_off; 88 Dwarf_Half version; 89 uint64_t offset, length; 90 int dwarf_size; 91 92 assert(dbg != NULL); 93 94 dbg->dbg_str_offsets = NULL; 95 96 if ((ds = _dwarf_find_section(dbg, ".debug_str_offsets")) == NULL) 97 return (DW_DLE_NONE); 98 99 offset = 0; 100 101 /* Read in the table header. */ 102 length = dbg->read(ds->ds_data, &offset, 4); 103 if (length == 0xffffffff) { 104 dwarf_size = 8; 105 length = dbg->read(ds->ds_data, &offset, 8); 106 } else 107 dwarf_size = 4; 108 109 version = dbg->read(ds->ds_data, &offset, 2); 110 if (version != 5) { 111 DWARF_SET_ERROR(dbg, error, DW_DLE_VERSION_STAMP_ERROR); 112 return (DW_DLE_VERSION_STAMP_ERROR); 113 } 114 115 if ((str_off = calloc(1, sizeof(*str_off))) == NULL) { 116 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 117 return (DW_DLE_MEMORY); 118 } 119 120 /* 2 byte padding. */ 121 offset += 2; 122 123 str_off->so_length = length; 124 str_off->so_version = version; 125 str_off->so_header_size = offset; 126 str_off->so_dwarf_size = dwarf_size; 127 str_off->so_data = ds->ds_data; 128 129 dbg->dbg_str_offsets = str_off; 130 131 return (DW_DLE_NONE); 132 } 133 134 static int 135 _dwarf_find_cu_str_offsets_base(Dwarf_Debug dbg, Dwarf_CU cu, 136 uint64_t *offset_basep, Dwarf_Error *error) 137 { 138 Dwarf_Attribute at; 139 Dwarf_Die cu_die; 140 int ret; 141 142 assert(dbg->dbg_str_offsets != NULL); 143 144 if (!cu->cu_stroff_base_valid) { 145 if ((ret = dwarf_siblingof(dbg, 0, &cu_die, error)) != 146 DW_DLV_OK) { 147 DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); 148 return (DW_DLE_NO_ENTRY); 149 } 150 151 if ((at = _dwarf_attr_find(cu_die, DW_AT_str_offsets_base)) != 152 NULL) { 153 cu->cu_stroff_base = at->u[0].u64; 154 } else { 155 /* 156 * No DW_AT_str_offsets_base found. Use header size 157 * instead. 158 */ 159 cu->cu_stroff_base = 160 dbg->dbg_str_offsets->so_header_size; 161 } 162 cu->cu_stroff_base_valid = 1; 163 dwarf_dealloc(dbg, cu_die, DW_DLA_DIE); 164 } 165 *offset_basep = cu->cu_stroff_base; 166 167 return (DW_DLE_NONE); 168 } 169 170 int 171 _dwarf_read_indexed_str(Dwarf_Debug dbg, Dwarf_CU cu, uint64_t index, 172 char **str_p, Dwarf_Error *error) 173 { 174 Dwarf_StrOffsets *so; 175 uint64_t offset, offsets_base, strtab_offset; 176 int ret; 177 178 if (dbg->dbg_str_offsets == NULL) { 179 if ((ret = _dwarf_str_offsets_init(dbg, error)) != DW_DLE_NONE) 180 return (ret); 181 if (dbg->dbg_str_offsets == NULL) { 182 DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); 183 return (DW_DLE_NO_ENTRY); 184 } 185 } 186 187 if ((ret = _dwarf_find_cu_str_offsets_base(dbg, cu, &offsets_base, 188 error)) != DW_DLE_NONE) 189 return (ret); 190 191 so = dbg->dbg_str_offsets; 192 /* Caculate where to read from string offsets array. */ 193 offset = offsets_base + so->so_dwarf_size * index; 194 /* Read from the str offsets array to get offset into string table. */ 195 strtab_offset = dbg->read(so->so_data, &offset, so->so_dwarf_size); 196 *str_p = _dwarf_strtab_get_table(dbg) + strtab_offset; 197 198 return (DW_DLE_NONE); 199 } 200 201 int 202 _dwarf_strtab_init(Dwarf_Debug dbg, Dwarf_Error *error) 203 { 204 Dwarf_Section *ds; 205 206 assert(dbg != NULL); 207 208 if (dbg->dbg_mode == DW_DLC_READ || dbg->dbg_mode == DW_DLC_RDWR) { 209 ds = _dwarf_find_section(dbg, ".debug_str"); 210 if (ds == NULL) { 211 dbg->dbg_strtab = NULL; 212 dbg->dbg_strtab_cap = dbg->dbg_strtab_size = 0; 213 return (DW_DLE_NONE); 214 } 215 216 dbg->dbg_strtab_cap = dbg->dbg_strtab_size = ds->ds_size; 217 218 if (dbg->dbg_mode == DW_DLC_RDWR) { 219 if ((dbg->dbg_strtab = malloc((size_t) ds->ds_size)) == 220 NULL) { 221 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 222 return (DW_DLE_MEMORY); 223 } 224 memcpy(dbg->dbg_strtab, ds->ds_data, ds->ds_size); 225 } else 226 dbg->dbg_strtab = (char *) ds->ds_data; 227 228 ds = _dwarf_find_section(dbg, ".debug_line_str"); 229 if (ds != NULL) { 230 dbg->dbg_line_strtab = (char *) ds->ds_data; 231 } 232 } else { 233 /* DW_DLC_WRITE */ 234 235 dbg->dbg_strtab_cap = _INIT_DWARF_STRTAB_SIZE; 236 dbg->dbg_strtab_size = 0; 237 238 if ((dbg->dbg_strtab = malloc((size_t) dbg->dbg_strtab_cap)) == 239 NULL) { 240 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 241 return (DW_DLE_MEMORY); 242 } 243 244 dbg->dbg_strtab[0] = '\0'; 245 } 246 247 return (DW_DLE_NONE); 248 } 249 250 void 251 _dwarf_str_offsets_cleanup(Dwarf_Debug dbg) 252 { 253 254 assert(dbg != NULL); 255 256 if (dbg->dbg_str_offsets) 257 free(dbg->dbg_str_offsets); 258 } 259 260 void 261 _dwarf_strtab_cleanup(Dwarf_Debug dbg) 262 { 263 264 assert(dbg != NULL); 265 266 if (dbg->dbg_mode == DW_DLC_RDWR || dbg->dbg_mode == DW_DLC_WRITE) 267 free(dbg->dbg_strtab); 268 } 269 270 int 271 _dwarf_strtab_gen(Dwarf_P_Debug dbg, Dwarf_Error *error) 272 { 273 Dwarf_P_Section ds; 274 int ret; 275 276 assert(dbg != NULL); 277 278 if ((ret = _dwarf_section_init(dbg, &ds, ".debug_str", 0, error)) != 279 DW_DLE_NONE) 280 return (ret); 281 282 if (dbg->dbg_strtab_size > ds->ds_cap) { 283 ds->ds_data = realloc(ds->ds_data, 284 (size_t) dbg->dbg_strtab_size); 285 if (ds->ds_data == NULL) { 286 _dwarf_section_free(dbg, &ds); 287 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 288 return (DW_DLE_MEMORY); 289 } 290 ds->ds_cap = dbg->dbg_strtab_size; 291 } 292 293 memcpy(ds->ds_data, dbg->dbg_strtab, dbg->dbg_strtab_size); 294 ds->ds_size = dbg->dbg_strtab_size; 295 296 /* 297 * Inform application the creation of .debug_str ELF section. 298 * Note that .debug_str use a different format than usual ELF 299 * string table, so it should not have SHT_STRTAB as its type. 300 */ 301 ret = _dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error); 302 303 return (ret); 304 } 305