1 /*- 2 * Copyright (c) 2007 John Birrell (jb@freebsd.org) 3 * Copyright (c) 2010,2011 Kai Wang 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include "_libdwarf.h" 29 30 ELFTC_VCSID("$Id: libdwarf_info.c 2942 2013-05-04 23:03:54Z kaiwang27 $"); 31 32 int 33 _dwarf_info_first_cu(Dwarf_Debug dbg, Dwarf_Error *error) 34 { 35 Dwarf_CU cu; 36 int ret; 37 38 assert(dbg->dbg_cu_current == NULL); 39 cu = STAILQ_FIRST(&dbg->dbg_cu); 40 if (cu != NULL) { 41 dbg->dbg_cu_current = cu; 42 return (DW_DLE_NONE); 43 } 44 45 if (dbg->dbg_info_loaded) 46 return (DW_DLE_NO_ENTRY); 47 48 dbg->dbg_info_off = 0; 49 ret = _dwarf_info_load(dbg, 0, error); 50 if (ret != DW_DLE_NONE) 51 return (ret); 52 53 dbg->dbg_cu_current = STAILQ_FIRST(&dbg->dbg_cu); 54 55 return (DW_DLE_NONE); 56 } 57 58 int 59 _dwarf_info_next_cu(Dwarf_Debug dbg, Dwarf_Error *error) 60 { 61 Dwarf_CU cu; 62 int ret; 63 64 assert(dbg->dbg_cu_current != NULL); 65 cu = STAILQ_NEXT(dbg->dbg_cu_current, cu_next); 66 if (cu != NULL) { 67 dbg->dbg_cu_current = cu; 68 return (DW_DLE_NONE); 69 } 70 71 if (dbg->dbg_info_loaded) { 72 dbg->dbg_cu_current = NULL; 73 return (DW_DLE_NO_ENTRY); 74 } 75 76 ret = _dwarf_info_load(dbg, 0, error); 77 if (ret != DW_DLE_NONE) 78 return (ret); 79 80 dbg->dbg_cu_current = STAILQ_NEXT(dbg->dbg_cu_current, cu_next); 81 82 return (DW_DLE_NONE); 83 } 84 85 int 86 _dwarf_info_load(Dwarf_Debug dbg, int load_all, Dwarf_Error *error) 87 { 88 Dwarf_CU cu; 89 Dwarf_Section *ds; 90 int dwarf_size, ret; 91 uint64_t length; 92 uint64_t next_offset; 93 uint64_t offset; 94 95 ret = DW_DLE_NONE; 96 if (dbg->dbg_info_loaded) 97 return (DW_DLE_NONE); 98 99 offset = dbg->dbg_info_off; 100 ds = dbg->dbg_info_sec; 101 assert(ds != NULL); 102 while (offset < ds->ds_size) { 103 if ((cu = calloc(1, sizeof(struct _Dwarf_CU))) == NULL) { 104 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 105 return (DW_DLE_MEMORY); 106 } 107 108 cu->cu_dbg = dbg; 109 cu->cu_offset = offset; 110 111 length = dbg->read(ds->ds_data, &offset, 4); 112 if (length == 0xffffffff) { 113 length = dbg->read(ds->ds_data, &offset, 8); 114 dwarf_size = 8; 115 } else 116 dwarf_size = 4; 117 cu->cu_dwarf_size = dwarf_size; 118 119 /* 120 * Check if there is enough ELF data for this CU. This assumes 121 * that libelf gives us the entire section in one Elf_Data 122 * object. 123 */ 124 if (length > ds->ds_size - offset) { 125 free(cu); 126 DWARF_SET_ERROR(dbg, error, DW_DLE_CU_LENGTH_ERROR); 127 return (DW_DLE_CU_LENGTH_ERROR); 128 } 129 130 /* Compute the offset to the next compilation unit: */ 131 next_offset = offset + length; 132 dbg->dbg_info_off = next_offset; 133 134 /* Initialise the compilation unit. */ 135 cu->cu_length = length; 136 cu->cu_length_size = (dwarf_size == 4 ? 4 : 12); 137 cu->cu_version = dbg->read(ds->ds_data, &offset, 2); 138 cu->cu_abbrev_offset = dbg->read(ds->ds_data, &offset, 139 dwarf_size); 140 cu->cu_abbrev_offset_cur = cu->cu_abbrev_offset; 141 cu->cu_pointer_size = dbg->read(ds->ds_data, &offset, 1); 142 cu->cu_next_offset = next_offset; 143 144 /* Add the compilation unit to the list. */ 145 STAILQ_INSERT_TAIL(&dbg->dbg_cu, cu, cu_next); 146 147 if (cu->cu_version < 2 || cu->cu_version > 4) { 148 DWARF_SET_ERROR(dbg, error, DW_DLE_VERSION_STAMP_ERROR); 149 ret = DW_DLE_VERSION_STAMP_ERROR; 150 break; 151 } 152 153 cu->cu_1st_offset = offset; 154 155 offset = next_offset; 156 157 if (!load_all) 158 break; 159 } 160 161 if ((Dwarf_Unsigned) dbg->dbg_info_off >= ds->ds_size) 162 dbg->dbg_info_loaded = 1; 163 164 return (ret); 165 } 166 167 void 168 _dwarf_info_cleanup(Dwarf_Debug dbg) 169 { 170 Dwarf_CU cu, tcu; 171 172 assert(dbg != NULL && dbg->dbg_mode == DW_DLC_READ); 173 174 STAILQ_FOREACH_SAFE(cu, &dbg->dbg_cu, cu_next, tcu) { 175 STAILQ_REMOVE(&dbg->dbg_cu, cu, _Dwarf_CU, cu_next); 176 _dwarf_abbrev_cleanup(cu); 177 if (cu->cu_lineinfo != NULL) { 178 _dwarf_lineno_cleanup(cu->cu_lineinfo); 179 cu->cu_lineinfo = NULL; 180 } 181 free(cu); 182 } 183 } 184 185 int 186 _dwarf_info_gen(Dwarf_P_Debug dbg, Dwarf_Error *error) 187 { 188 Dwarf_P_Section ds; 189 Dwarf_Rel_Section drs; 190 Dwarf_Unsigned offset; 191 Dwarf_CU cu; 192 int ret; 193 194 assert(dbg != NULL && dbg->write_alloc != NULL); 195 196 if (dbg->dbgp_root_die == NULL) 197 return (DW_DLE_NONE); 198 199 /* Create the single CU for this debugging object. */ 200 if ((cu = calloc(1, sizeof(struct _Dwarf_CU))) == NULL) { 201 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 202 return (DW_DLE_MEMORY); 203 } 204 cu->cu_dbg = dbg; 205 cu->cu_version = 2; /* DWARF2 */ 206 cu->cu_pointer_size = dbg->dbg_pointer_size; 207 STAILQ_INSERT_TAIL(&dbg->dbg_cu, cu, cu_next); 208 209 /* Create .debug_info section. */ 210 if ((ret = _dwarf_section_init(dbg, &dbg->dbgp_info, ".debug_info", 0, 211 error)) != DW_DLE_NONE) 212 goto gen_fail1; 213 ds = dbg->dbgp_info; 214 215 /* Create relocation section for .debug_init */ 216 if ((ret = _dwarf_reloc_section_init(dbg, &drs, ds, error)) != 217 DW_DLE_NONE) 218 goto gen_fail0; 219 220 /* Length placeholder. (We only use 32-bit DWARF format) */ 221 RCHECK(WRITE_VALUE(cu->cu_length, 4)); 222 223 /* Write CU version */ 224 RCHECK(WRITE_VALUE(cu->cu_version, 2)); 225 226 /* 227 * Write abbrev offset. (always 0, we only support single CU) 228 * Also generate a relocation entry for this offset. 229 */ 230 RCHECK(_dwarf_reloc_entry_add(dbg, drs, ds, dwarf_drt_data_reloc, 4, 231 ds->ds_size, 0, cu->cu_abbrev_offset, ".debug_abbrev", error)); 232 233 /* Pointer size. */ 234 RCHECK(WRITE_VALUE(cu->cu_pointer_size, 1)); 235 236 /* Transform the DIE(s) of this CU. */ 237 RCHECK(_dwarf_die_gen(dbg, cu, drs, error)); 238 239 /* Now we can fill in the length of this CU. */ 240 cu->cu_length = ds->ds_size - 4; 241 offset = 0; 242 dbg->write(ds->ds_data, &offset, cu->cu_length, 4); 243 244 /* Inform application the creation of .debug_info ELF section. */ 245 RCHECK(_dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error)); 246 247 /* 248 * Inform application the creation of relocation section for 249 * .debug_info. 250 */ 251 RCHECK(_dwarf_reloc_section_finalize(dbg, drs, error)); 252 253 return (DW_DLE_NONE); 254 255 gen_fail: 256 _dwarf_reloc_section_free(dbg, &drs); 257 258 gen_fail0: 259 _dwarf_section_free(dbg, &dbg->dbgp_info); 260 261 gen_fail1: 262 STAILQ_REMOVE(&dbg->dbg_cu, cu, _Dwarf_CU, cu_next); 263 free(cu); 264 265 return (ret); 266 } 267 268 void 269 _dwarf_info_pro_cleanup(Dwarf_P_Debug dbg) 270 { 271 Dwarf_CU cu; 272 273 assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE); 274 275 cu = STAILQ_FIRST(&dbg->dbg_cu); 276 if (cu != NULL) { 277 STAILQ_REMOVE(&dbg->dbg_cu, cu, _Dwarf_CU, cu_next); 278 _dwarf_abbrev_cleanup(cu); 279 free(cu); 280 } 281 } 282