1 /* $NetBSD: libdwarf_str.c,v 1.3 2016/02/20 02:43:42 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2009,2010 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.3 2016/02/20 02:43:42 christos Exp $"); 32 ELFTC_VCSID("Id: libdwarf_str.c 2070 2011-10-27 03:05:32Z jkoshy "); 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 strncpy(&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 int 75 _dwarf_strtab_init(Dwarf_Debug dbg, Dwarf_Error *error) 76 { 77 Dwarf_Section *ds; 78 79 assert(dbg != NULL); 80 81 if (dbg->dbg_mode == DW_DLC_READ || dbg->dbg_mode == DW_DLC_RDWR) { 82 ds = _dwarf_find_section(dbg, ".debug_str"); 83 if (ds == NULL) { 84 dbg->dbg_strtab = NULL; 85 dbg->dbg_strtab_cap = dbg->dbg_strtab_size = 0; 86 return (DW_DLE_NONE); 87 } 88 89 dbg->dbg_strtab_cap = dbg->dbg_strtab_size = ds->ds_size; 90 91 if (dbg->dbg_mode == DW_DLC_RDWR) { 92 if ((dbg->dbg_strtab = malloc((size_t) ds->ds_size)) == 93 NULL) { 94 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 95 return (DW_DLE_MEMORY); 96 } 97 memcpy(dbg->dbg_strtab, ds->ds_data, ds->ds_size); 98 } else 99 dbg->dbg_strtab = (char *) ds->ds_data; 100 } else { 101 /* DW_DLC_WRITE */ 102 103 dbg->dbg_strtab_cap = _INIT_DWARF_STRTAB_SIZE; 104 dbg->dbg_strtab_size = 0; 105 106 if ((dbg->dbg_strtab = malloc((size_t) dbg->dbg_strtab_cap)) == 107 NULL) { 108 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 109 return (DW_DLE_MEMORY); 110 } 111 112 dbg->dbg_strtab[0] = '\0'; 113 } 114 115 return (DW_DLE_NONE); 116 } 117 118 void 119 _dwarf_strtab_cleanup(Dwarf_Debug dbg) 120 { 121 122 assert(dbg != NULL); 123 124 if (dbg->dbg_mode == DW_DLC_RDWR || dbg->dbg_mode == DW_DLC_WRITE) 125 free(dbg->dbg_strtab); 126 } 127 128 int 129 _dwarf_strtab_gen(Dwarf_P_Debug dbg, Dwarf_Error *error) 130 { 131 Dwarf_P_Section ds; 132 int ret; 133 134 assert(dbg != NULL); 135 136 if ((ret = _dwarf_section_init(dbg, &ds, ".debug_str", 0, error)) != 137 DW_DLE_NONE) 138 return (ret); 139 140 if (dbg->dbg_strtab_size > ds->ds_cap) { 141 ds->ds_data = realloc(ds->ds_data, 142 (size_t) dbg->dbg_strtab_size); 143 if (ds->ds_data == NULL) { 144 _dwarf_section_free(dbg, &ds); 145 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 146 return (DW_DLE_MEMORY); 147 } 148 ds->ds_cap = dbg->dbg_strtab_size; 149 } 150 151 memcpy(ds->ds_data, dbg->dbg_strtab, dbg->dbg_strtab_size); 152 ds->ds_size = dbg->dbg_strtab_size; 153 154 /* 155 * Inform application the creation of .debug_str ELF section. 156 * Note that .debug_str use a different format than usual ELF 157 * string table, so it should not have SHT_STRTAB as its type. 158 */ 159 ret = _dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error); 160 161 return (ret); 162 } 163