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