| 1 |
/* |
| 2 |
Copyright (C) 2016-2020 David Anderson. All Rights Reserved. |
| 3 |
|
| 4 |
This program is free software; you can redistribute it |
| 5 |
and/or modify it under the terms of version 2.1 of the |
| 6 |
GNU Lesser General Public License as published by the Free |
| 7 |
Software Foundation. |
| 8 |
|
| 9 |
This program is distributed in the hope that it would be |
| 10 |
useful, but WITHOUT ANY WARRANTY; without even the implied |
| 11 |
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 12 |
PURPOSE. |
| 13 |
|
| 14 |
Further, this software is distributed without any warranty |
| 15 |
that it is free of the rightful claim of any third person |
| 16 |
regarding infringement or the like. Any license provided |
| 17 |
herein, whether implied or otherwise, applies only to this |
| 18 |
software file. Patent licenses, if any, provided herein |
| 19 |
do not apply to combinations of this program with other |
| 20 |
software, or any other product whatsoever. |
| 21 |
|
| 22 |
You should have received a copy of the GNU Lesser General |
| 23 |
Public License along with this program; if not, write the |
| 24 |
Free Software Foundation, Inc., 51 Franklin Street - Fifth |
| 25 |
Floor, Boston MA 02110-1301, USA. |
| 26 |
|
| 27 |
*/ |
| 28 |
|
| 29 |
#include <config.h> |
| 30 |
|
| 31 |
#include <stdlib.h> /* calloc() free() */ |
| 32 |
#include <string.h> /* memcpy() */ |
| 33 |
|
| 34 |
#if defined(_WIN32) && defined(HAVE_STDAFX_H) |
| 35 |
#include "stdafx.h" |
| 36 |
#endif /* HAVE_STDAFX_H */ |
| 37 |
|
| 38 |
#include "dwarf.h" |
| 39 |
#include "libdwarf.h" |
| 40 |
#include "libdwarf_private.h" |
| 41 |
#include "dwarf_base_types.h" |
| 42 |
#include "dwarf_opaque.h" |
| 43 |
#include "dwarf_alloc.h" |
| 44 |
#include "dwarf_error.h" |
| 45 |
#include "dwarf_util.h" |
| 46 |
#include "dwarf_dsc.h" |
| 47 |
|
| 48 |
/* When called with ary and *arraycount 0 |
| 49 |
this just counts the elements found. |
| 50 |
Otherwise it records the values in ary and |
| 51 |
recounts. The arraycount pointer must be |
| 52 |
passed-in non-null always. */ |
| 53 |
static int |
| 54 |
get_dsc_leb_entries(Dwarf_Debug dbg, |
| 55 |
Dwarf_Small * blockpointer, |
| 56 |
Dwarf_Unsigned blocklen, |
| 57 |
int dounsigned, |
| 58 |
struct Dwarf_Dsc_Entry_s *ary, |
| 59 |
size_t * arraycount, |
| 60 |
Dwarf_Error * error) |
| 61 |
{ |
| 62 |
Dwarf_Small *p = blockpointer; |
| 63 |
Dwarf_Small *endp = blockpointer + blocklen; |
| 64 |
size_t larraycount = 0; |
| 65 |
size_t iarraycount = *arraycount; |
| 66 |
|
| 67 |
if (!ary) { |
| 68 |
if (iarraycount) { |
| 69 |
/* Internal botch calling this static function. */ |
| 70 |
_dwarf_error(dbg, error, DW_DLE_DISCR_ARRAY_ERROR); |
| 71 |
return DW_DLV_ERROR; |
| 72 |
} else {} |
| 73 |
} else { |
| 74 |
if (!iarraycount) { |
| 75 |
/* Internal botch calling this static function. */ |
| 76 |
_dwarf_error(dbg, error, DW_DLE_DISCR_ARRAY_ERROR); |
| 77 |
return DW_DLV_ERROR; |
| 78 |
} else {} |
| 79 |
} |
| 80 |
if (dounsigned) { |
| 81 |
while (p < endp) { |
| 82 |
Dwarf_Unsigned dsc = 0; |
| 83 |
Dwarf_Unsigned low = 0; |
| 84 |
Dwarf_Unsigned high = 0; |
| 85 |
|
| 86 |
if (ary && (larraycount >= iarraycount)) { |
| 87 |
_dwarf_error(dbg, error, DW_DLE_DISCR_ARRAY_ERROR); |
| 88 |
return DW_DLV_ERROR; |
| 89 |
} |
| 90 |
DECODE_LEB128_UWORD_CK(p,dsc, |
| 91 |
dbg,error,endp); |
| 92 |
if (!dsc) { |
| 93 |
DECODE_LEB128_UWORD_CK(p,low, |
| 94 |
dbg,error,endp); |
| 95 |
} else { |
| 96 |
DECODE_LEB128_UWORD_CK(p,low, |
| 97 |
dbg,error,endp); |
| 98 |
DECODE_LEB128_UWORD_CK(p,high, |
| 99 |
dbg,error,endp); |
| 100 |
} |
| 101 |
if (ary) { |
| 102 |
struct Dwarf_Dsc_Entry_s *arye = |
| 103 |
ary+larraycount; |
| 104 |
|
| 105 |
/* type reads the same as uleb and leb because |
| 106 |
it is only zero or one. */ |
| 107 |
arye->dsc_type = (Dwarf_Half)dsc; |
| 108 |
arye->dsc_low_u = low; |
| 109 |
arye->dsc_high_u = high; |
| 110 |
} |
| 111 |
larraycount++; |
| 112 |
} |
| 113 |
} else { |
| 114 |
while (p < endp) { |
| 115 |
Dwarf_Signed dsc = 0; |
| 116 |
Dwarf_Signed low = 0; |
| 117 |
Dwarf_Signed high = 0; |
| 118 |
Dwarf_Unsigned leblen = 0; |
| 119 |
|
| 120 |
(void)leblen; |
| 121 |
if (ary && (larraycount >= iarraycount)) { |
| 122 |
_dwarf_error(dbg, error, DW_DLE_DISCR_ARRAY_ERROR); |
| 123 |
return DW_DLV_ERROR; |
| 124 |
} |
| 125 |
DECODE_LEB128_SWORD_LEN_CK(p,dsc, |
| 126 |
leblen,dbg,error,endp); |
| 127 |
if (!dsc) { |
| 128 |
DECODE_LEB128_SWORD_LEN_CK(p,low, |
| 129 |
leblen,dbg,error,endp); |
| 130 |
} else { |
| 131 |
DECODE_LEB128_SWORD_LEN_CK(p,low, |
| 132 |
leblen,dbg,error,endp); |
| 133 |
DECODE_LEB128_SWORD_LEN_CK(p,high, |
| 134 |
leblen,dbg,error,endp); |
| 135 |
} |
| 136 |
if (ary) { |
| 137 |
struct Dwarf_Dsc_Entry_s *arye = |
| 138 |
ary+larraycount; |
| 139 |
|
| 140 |
/* type reads the same as uleb and leb because |
| 141 |
it is only zero or one. */ |
| 142 |
arye->dsc_type = (Dwarf_Half)dsc; |
| 143 |
arye->dsc_low_s = low; |
| 144 |
arye->dsc_high_s = high; |
| 145 |
} |
| 146 |
larraycount++; |
| 147 |
} |
| 148 |
} |
| 149 |
if (ary) { |
| 150 |
/* Just verify this recount matches original */ |
| 151 |
if (iarraycount != larraycount) { |
| 152 |
_dwarf_error(dbg, error, DW_DLE_DISCR_ARRAY_ERROR); |
| 153 |
return DW_DLV_ERROR; |
| 154 |
} |
| 155 |
} else { |
| 156 |
/* This matters for first call with |
| 157 |
ary 0 and iarraycount 0 as we are generating the |
| 158 |
count. */ |
| 159 |
*arraycount = larraycount; |
| 160 |
} |
| 161 |
return DW_DLV_OK; |
| 162 |
} |
| 163 |
|
| 164 |
int dwarf_discr_list(Dwarf_Debug dbg, |
| 165 |
Dwarf_Small * blockpointer, |
| 166 |
Dwarf_Unsigned blocklen, |
| 167 |
Dwarf_Dsc_Head * dsc_head_out, |
| 168 |
Dwarf_Unsigned * dsc_array_length_out, |
| 169 |
Dwarf_Error * error) |
| 170 |
{ |
| 171 |
Dwarf_Dsc_Head h = 0; |
| 172 |
int res = 0; |
| 173 |
size_t arraycount = 0; |
| 174 |
struct Dwarf_Dsc_Entry_s *ary = 0; |
| 175 |
Dwarf_Small * dscblockp = 0; |
| 176 |
Dwarf_Unsigned dscblocklen = 0; |
| 177 |
|
| 178 |
CHECK_DBG(dbg,error,"dwarf_discr_list()"); |
| 179 |
if (blocklen == 0) { |
| 180 |
return DW_DLV_NO_ENTRY; |
| 181 |
} |
| 182 |
dscblockp = (Dwarf_Small *)calloc(blocklen,sizeof(Dwarf_Small)); |
| 183 |
if (!dscblockp) { |
| 184 |
_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL); |
| 185 |
return DW_DLV_ERROR; |
| 186 |
} |
| 187 |
dscblocklen = blocklen; |
| 188 |
memcpy(dscblockp,blockpointer,blocklen); |
| 189 |
|
| 190 |
res = get_dsc_leb_entries(dbg,dscblockp,dscblocklen, |
| 191 |
/* TRUE or FALSE here is not important, the arraycount |
| 192 |
returned to us will be identical either way. */ |
| 193 |
FALSE, 0, &arraycount,error); |
| 194 |
if (res != DW_DLV_OK) { |
| 195 |
free(dscblockp); |
| 196 |
return res; |
| 197 |
} |
| 198 |
|
| 199 |
h = (Dwarf_Dsc_Head)_dwarf_get_alloc(dbg,DW_DLA_DSC_HEAD,1); |
| 200 |
if (!h) { |
| 201 |
free(dscblockp); |
| 202 |
_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL); |
| 203 |
return DW_DLV_ERROR; |
| 204 |
} |
| 205 |
|
| 206 |
h->dsh_block = dscblockp; |
| 207 |
h->dsh_block_len = dscblocklen; |
| 208 |
h->dsh_debug = dbg; |
| 209 |
/* Now the destructor for h will deal with block malloc space. */ |
| 210 |
|
| 211 |
ary = (struct Dwarf_Dsc_Entry_s *)calloc(arraycount, |
| 212 |
sizeof(struct Dwarf_Dsc_Entry_s)); |
| 213 |
if (!ary) { |
| 214 |
dwarf_dealloc(dbg,h,DW_DLA_DSC_HEAD); |
| 215 |
_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL); |
| 216 |
return DW_DLV_ERROR; |
| 217 |
} |
| 218 |
h->dsh_count = arraycount; |
| 219 |
h->dsh_array = ary; |
| 220 |
h->dsh_set_unsigned = 0; |
| 221 |
h->dsh_set_signed = 0; |
| 222 |
|
| 223 |
*dsc_head_out = h; |
| 224 |
*dsc_array_length_out = arraycount; |
| 225 |
return DW_DLV_OK; |
| 226 |
} |
| 227 |
|
| 228 |
/* NEW September 2016. Allows easy access to DW_AT_discr_list |
| 229 |
entry. Callers must know which is the appropriate |
| 230 |
one of the following two interfaces, though both |
| 231 |
will work. */ |
| 232 |
int |
| 233 |
dwarf_discr_entry_u(Dwarf_Dsc_Head dsh , |
| 234 |
Dwarf_Unsigned entrynum, |
| 235 |
Dwarf_Half * out_type, |
| 236 |
Dwarf_Unsigned * out_discr_low, |
| 237 |
Dwarf_Unsigned * out_discr_high, |
| 238 |
Dwarf_Error * error) |
| 239 |
{ |
| 240 |
struct Dwarf_Dsc_Entry_s *dse = 0; |
| 241 |
|
| 242 |
(void)error; |
| 243 |
if (entrynum >= dsh->dsh_count) { |
| 244 |
return DW_DLV_NO_ENTRY; |
| 245 |
} |
| 246 |
if (!dsh->dsh_set_unsigned) { |
| 247 |
int res =0; |
| 248 |
int dounsigned = 1; |
| 249 |
size_t count = dsh->dsh_count; |
| 250 |
|
| 251 |
res = get_dsc_leb_entries(dsh->dsh_debug, |
| 252 |
dsh->dsh_block, |
| 253 |
dsh->dsh_block_len, |
| 254 |
dounsigned, |
| 255 |
dsh->dsh_array, |
| 256 |
&count, |
| 257 |
error); |
| 258 |
if (res != DW_DLV_OK) { |
| 259 |
return res; |
| 260 |
} |
| 261 |
dsh->dsh_set_unsigned = TRUE; |
| 262 |
} |
| 263 |
if (!dsh->dsh_array) { |
| 264 |
_dwarf_error(dsh->dsh_debug, error, DW_DLE_DISCR_ARRAY_ERROR); |
| 265 |
return DW_DLV_ERROR; |
| 266 |
} |
| 267 |
dse = dsh->dsh_array + entrynum; |
| 268 |
*out_type = dse->dsc_type; |
| 269 |
*out_discr_low = dse->dsc_low_u; |
| 270 |
*out_discr_high = dse->dsc_high_u; |
| 271 |
return DW_DLV_OK; |
| 272 |
} |
| 273 |
|
| 274 |
/* NEW September 2016. Allows easy access to DW_AT_discr_list |
| 275 |
entry. */ |
| 276 |
int |
| 277 |
dwarf_discr_entry_s(Dwarf_Dsc_Head dsh, |
| 278 |
Dwarf_Unsigned entrynum, |
| 279 |
Dwarf_Half * out_type, |
| 280 |
Dwarf_Signed * out_discr_low, |
| 281 |
Dwarf_Signed * out_discr_high, |
| 282 |
Dwarf_Error * error) |
| 283 |
{ |
| 284 |
struct Dwarf_Dsc_Entry_s *dse = 0; |
| 285 |
|
| 286 |
(void)error; |
| 287 |
if (entrynum >= dsh->dsh_count) { |
| 288 |
return DW_DLV_NO_ENTRY; |
| 289 |
} |
| 290 |
if (!dsh->dsh_set_signed) { |
| 291 |
int res =0; |
| 292 |
int dounsigned = 0; |
| 293 |
size_t count = dsh->dsh_count; |
| 294 |
|
| 295 |
res = get_dsc_leb_entries(dsh->dsh_debug, |
| 296 |
dsh->dsh_block, |
| 297 |
dsh->dsh_block_len, |
| 298 |
dounsigned, |
| 299 |
dsh->dsh_array, |
| 300 |
&count, |
| 301 |
error); |
| 302 |
if (res != DW_DLV_OK) { |
| 303 |
return res; |
| 304 |
} |
| 305 |
dsh->dsh_set_signed = TRUE; |
| 306 |
} |
| 307 |
if (!dsh->dsh_array) { |
| 308 |
_dwarf_error(dsh->dsh_debug, error, DW_DLE_DISCR_ARRAY_ERROR); |
| 309 |
return DW_DLV_ERROR; |
| 310 |
} |
| 311 |
dse = dsh->dsh_array + entrynum; |
| 312 |
*out_type = dse->dsc_type; |
| 313 |
*out_discr_low = dse->dsc_low_s; |
| 314 |
*out_discr_high = dse->dsc_high_s; |
| 315 |
return DW_DLV_OK; |
| 316 |
} |
| 317 |
|
| 318 |
void |
| 319 |
_dwarf_dsc_destructor(void *m) |
| 320 |
{ |
| 321 |
Dwarf_Dsc_Head h = (Dwarf_Dsc_Head) m; |
| 322 |
|
| 323 |
free(h->dsh_array); |
| 324 |
h->dsh_array = 0; |
| 325 |
free(h->dsh_block); |
| 326 |
h->dsh_block = 0; |
| 327 |
h->dsh_count = 0; |
| 328 |
} |