| 1 |
/* |
| 2 |
Copyright (C) 2000-2005 Silicon Graphics, Inc. All Rights Reserved. |
| 3 |
Portions Copyright (C) 2007-2022 David Anderson. All Rights Reserved. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it |
| 6 |
and/or modify it under the terms of version 2.1 of the |
| 7 |
GNU Lesser General Public License as published by the Free |
| 8 |
Software Foundation. |
| 9 |
|
| 10 |
This program is distributed in the hope that it would be |
| 11 |
useful, but WITHOUT ANY WARRANTY; without even the implied |
| 12 |
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 13 |
PURPOSE. |
| 14 |
|
| 15 |
Further, this software is distributed without any warranty |
| 16 |
that it is free of the rightful claim of any third person |
| 17 |
regarding infringement or the like. Any license provided |
| 18 |
herein, whether implied or otherwise, applies only to this |
| 19 |
software file. Patent licenses, if any, provided herein |
| 20 |
do not apply to combinations of this program with other |
| 21 |
software, or any other product whatsoever. |
| 22 |
|
| 23 |
You should have received a copy of the GNU Lesser General |
| 24 |
Public License along with this program; if not, write the |
| 25 |
Free Software Foundation, Inc., 51 Franklin Street - Fifth |
| 26 |
Floor, Boston MA 02110-1301, USA. |
| 27 |
|
| 28 |
*/ |
| 29 |
/* To see the full set of DW_DLA types and nothing |
| 30 |
else try: |
| 31 |
grep DW_DLA dwarf_alloc.c | grep 0x |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <config.h> |
| 35 |
|
| 36 |
#include <stdio.h> /* fclose() */ |
| 37 |
#include <stdlib.h> /* malloc() free() */ |
| 38 |
#include <string.h> /* memset() */ |
| 39 |
|
| 40 |
#if defined(_WIN32) && defined(HAVE_STDAFX_H) |
| 41 |
#include "stdafx.h" |
| 42 |
#endif /* HAVE_STDAFX_H */ |
| 43 |
|
| 44 |
#ifdef HAVE_STDINT_H |
| 45 |
#include <stdint.h> /* uintptr_t */ |
| 46 |
#endif /* HAVE_STDINT_H */ |
| 47 |
|
| 48 |
#include "dwarf.h" |
| 49 |
#include "libdwarf.h" |
| 50 |
#include "libdwarf_private.h" |
| 51 |
#include "dwarf_base_types.h" |
| 52 |
#include "dwarf_opaque.h" |
| 53 |
#include "dwarf_error.h" |
| 54 |
#include "dwarf_alloc.h" |
| 55 |
/* These files are included to get the sizes |
| 56 |
of structs for malloc. |
| 57 |
*/ |
| 58 |
#include "dwarf_util.h" |
| 59 |
#include "dwarf_line.h" |
| 60 |
#include "dwarf_global.h" |
| 61 |
#include "dwarf_arange.h" |
| 62 |
#include "dwarf_abbrev.h" |
| 63 |
#include "dwarf_debugaddr.h" |
| 64 |
#include "dwarf_die_deliv.h" |
| 65 |
#include "dwarf_frame.h" |
| 66 |
#include "dwarf_loc.h" |
| 67 |
#include "dwarf_harmless.h" |
| 68 |
#include "dwarf_tsearch.h" |
| 69 |
#include "dwarf_gdbindex.h" |
| 70 |
#include "dwarf_gnu_index.h" |
| 71 |
#include "dwarf_xu_index.h" |
| 72 |
#include "dwarf_macro5.h" |
| 73 |
#include "dwarf_debugnames.h" |
| 74 |
#include "dwarf_rnglists.h" |
| 75 |
#include "dwarf_dsc.h" |
| 76 |
#include "dwarf_string.h" |
| 77 |
#include "dwarf_str_offsets.h" |
| 78 |
|
| 79 |
/* if DEBUG_ALLOC is defined a lot of stdout is generated here. */ |
| 80 |
#undef DEBUG_ALLOC |
| 81 |
/* Some allocations are simple some not. These reduce |
| 82 |
the issue of determining which sort of thing to a simple |
| 83 |
test. See ia_multiply_count |
| 84 |
Usually when MULTIPLY_NO is set the count |
| 85 |
is 1, so MULTIPY_CT would work as well. */ |
| 86 |
#define MULTIPLY_NO 0 |
| 87 |
#define MULTIPLY_CT 1 |
| 88 |
#define MULTIPLY_SP 2 |
| 89 |
/* This translates into de_alloc_hdr into a per-instance size |
| 90 |
and allows room for a constructor/destructor pointer. |
| 91 |
Rearranging the DW_DLA values would break binary compatibility |
| 92 |
so that is not an option. |
| 93 |
*/ |
| 94 |
struct ial_s { |
| 95 |
/* In bytes, one struct instance. */ |
| 96 |
short ia_struct_size; |
| 97 |
|
| 98 |
/* Not a count, but a MULTIPLY{_NO,_CT,_SP} value. */ |
| 99 |
short ia_multiply_count; |
| 100 |
|
| 101 |
/* When we really need a constructor/destructor |
| 102 |
these make applying such quite simple. */ |
| 103 |
int (*specialconstructor) (Dwarf_Debug, void *); |
| 104 |
void (*specialdestructor) (void *); |
| 105 |
}; |
| 106 |
|
| 107 |
/* Used as a way to return meaningful errors when |
| 108 |
the malloc arena is exhausted (when malloc returns NULL). |
| 109 |
Not normally used. |
| 110 |
New in December 2014.*/ |
| 111 |
struct Dwarf_Error_s _dwarf_failsafe_error = { |
| 112 |
DW_DLE_FAILSAFE_ERRVAL, |
| 113 |
0, |
| 114 |
1 |
| 115 |
}; |
| 116 |
|
| 117 |
/* If non-zero (the default) de_alloc_tree (see dwarf_alloc.c) |
| 118 |
is used normally. If zero then dwarf allocations |
| 119 |
are not tracked by libdwarf and dwarf_finish() cannot |
| 120 |
clean up any per-Dwarf_Debug allocations the |
| 121 |
caller forgot to dealloc. */ |
| 122 |
static signed char global_de_alloc_tree_on = 1; |
| 123 |
|
| 124 |
/* Defined March 7 2020. Allows a caller to |
| 125 |
avoid most tracking by the de_alloc_tree hash |
| 126 |
table if called with v of zero. |
| 127 |
Returns the value the flag was before this call. */ |
| 128 |
int dwarf_set_de_alloc_flag(int v) |
| 129 |
{ |
| 130 |
int ov = global_de_alloc_tree_on; |
| 131 |
global_de_alloc_tree_on = (char)v; |
| 132 |
return ov; |
| 133 |
} |
| 134 |
|
| 135 |
void |
| 136 |
_dwarf_error_destructor(void *m) |
| 137 |
{ |
| 138 |
Dwarf_Error er = (Dwarf_Error)m; |
| 139 |
dwarfstring *erm = (dwarfstring *)er->er_msg; |
| 140 |
if (! erm) { |
| 141 |
return; |
| 142 |
} |
| 143 |
#if DEBUG_ALLOC |
| 144 |
printf("libdwarfdetector DEALLOC Now destruct error " |
| 145 |
"string %s\n",dwarfstring_string(erm)); |
| 146 |
fflush(stdout); |
| 147 |
#endif /* DEBUG_ALLOC */ |
| 148 |
dwarfstring_destructor(erm); |
| 149 |
free(erm); |
| 150 |
er->er_msg = 0; |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
/* To do destructors we need some extra data in every |
| 155 |
_dwarf_get_alloc situation. */ |
| 156 |
/* Here is the extra we malloc for a prefix. */ |
| 157 |
struct reserve_size_s { |
| 158 |
void *dummy_rsv1; |
| 159 |
void *dummy_rsv2; |
| 160 |
}; |
| 161 |
/* Here is how we use the extra prefix area. */ |
| 162 |
struct reserve_data_s { |
| 163 |
void *rd_dbg; |
| 164 |
/* rd_length can only record correctly for short |
| 165 |
allocations, but that's not a problem im practice |
| 166 |
as the value is only for debugging and to |
| 167 |
ensure this struct length is correct. */ |
| 168 |
unsigned short rd_length; |
| 169 |
unsigned short rd_type; |
| 170 |
}; |
| 171 |
#define DW_RESERVE sizeof(struct reserve_size_s) |
| 172 |
|
| 173 |
/* In rare cases (bad object files) an error is created |
| 174 |
via malloc with no dbg to attach it to. |
| 175 |
We do not expect this except on corrupt objects. |
| 176 |
|
| 177 |
In all cases the user is *supposed* to dealloc |
| 178 |
the returned Dwarf_Error, and if it is in case |
| 179 |
of a NULL Dwarf_Debug the code were will find it |
| 180 |
in this special array and free and zero it. |
| 181 |
Hence no leak. |
| 182 |
*/ |
| 183 |
|
| 184 |
#define STATIC_ALLOWED 10 /* arbitrary, must be > 2, see below*/ |
| 185 |
static unsigned static_used = 0; |
| 186 |
/* entries in this list point to allocations of |
| 187 |
type DW_DLA_ERROR. */ |
| 188 |
static Dwarf_Error staticerrlist[STATIC_ALLOWED]; |
| 189 |
|
| 190 |
/* Clean this out if found */ |
| 191 |
static void |
| 192 |
dw_empty_errlist_item(Dwarf_Error e_in) |
| 193 |
{ |
| 194 |
unsigned i = 0; |
| 195 |
if (!e_in) { |
| 196 |
return; |
| 197 |
} |
| 198 |
for ( ; i <static_used; ++i) { |
| 199 |
Dwarf_Error e = staticerrlist[i]; |
| 200 |
if (e != e_in) { |
| 201 |
continue; |
| 202 |
} |
| 203 |
if (e->er_static_alloc == DE_MALLOC) { |
| 204 |
/* e is the returned address, not |
| 205 |
the base. Free by the base. */ |
| 206 |
void *mallocaddr = 0; |
| 207 |
|
| 208 |
if ( (uintptr_t)e > DW_RESERVE) { |
| 209 |
mallocaddr = (char*)e - DW_RESERVE; |
| 210 |
} else { |
| 211 |
/* Impossible */ |
| 212 |
continue; |
| 213 |
} |
| 214 |
_dwarf_error_destructor(e); |
| 215 |
free(mallocaddr); |
| 216 |
} |
| 217 |
staticerrlist[i] = 0; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
/* If the user calls dwarf_dealloc on an error |
| 222 |
out of a dwarf_init*() call, this will find |
| 223 |
it in the static err list. Here dbg is NULL |
| 224 |
so not mentioned. */ |
| 225 |
void |
| 226 |
_dwarf_add_to_static_err_list(Dwarf_Error error) |
| 227 |
{ |
| 228 |
unsigned i = 0; |
| 229 |
if (!error) { |
| 230 |
return; |
| 231 |
} |
| 232 |
#ifdef DEBUG_ALLOC |
| 233 |
printf("\nlibdwarfdetector add to static err list " |
| 234 |
" 0x%lx\n",(unsigned long)(uintptr_t)error); |
| 235 |
fflush(stdout); |
| 236 |
#endif /* DEBUG_ALLOC */ |
| 237 |
for ( ; i <static_used; ++i) { |
| 238 |
Dwarf_Error e = staticerrlist[i]; |
| 239 |
if (e) { |
| 240 |
continue; |
| 241 |
} |
| 242 |
#ifdef DEBUG_ALLOC |
| 243 |
printf("libdwarfdetector add to static err list at %u\n", |
| 244 |
i); |
| 245 |
fflush(stdout); |
| 246 |
#endif /* DEBUG_ALLOC */ |
| 247 |
staticerrlist[i] = error; |
| 248 |
return; |
| 249 |
} |
| 250 |
if (static_used < STATIC_ALLOWED) { |
| 251 |
staticerrlist[static_used] = error; |
| 252 |
++static_used; |
| 253 |
} |
| 254 |
} |
| 255 |
/* See libdwarf vulnerability DW202402-002 |
| 256 |
for the motivation. |
| 257 |
*/ |
| 258 |
static void |
| 259 |
_dwarf_remove_from_staticerrlist(Dwarf_Ptr *space) |
| 260 |
{ |
| 261 |
unsigned i = 0; |
| 262 |
if (!space) { |
| 263 |
return; |
| 264 |
} |
| 265 |
#ifdef DEBUG_ALLOC |
| 266 |
printf("\nlibdwarfdetector remove from static err list " |
| 267 |
" 0x%lx\n",(unsigned long)(uintptr_t)space); |
| 268 |
fflush(stdout); |
| 269 |
#endif /* DEBUG_ALLOC */ |
| 270 |
for ( ; i <static_used; ++i) { |
| 271 |
Dwarf_Error e = staticerrlist[i]; |
| 272 |
if (!e) { |
| 273 |
continue; |
| 274 |
} |
| 275 |
if ((void *)e == space) { |
| 276 |
#ifdef DEBUG_ALLOC |
| 277 |
printf("libdwarfdetector rm from static err list at %u\n", |
| 278 |
i); |
| 279 |
fflush(stdout); |
| 280 |
#endif /* DEBUG_ALLOC */ |
| 281 |
staticerrlist[i] = 0; |
| 282 |
return; |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
/* This will free everything in the staticerrlist, |
| 288 |
but that is ok */ |
| 289 |
void |
| 290 |
_dwarf_free_static_errlist(void) |
| 291 |
{ |
| 292 |
unsigned i = 0; |
| 293 |
|
| 294 |
for ( ; i <static_used; ++i) { |
| 295 |
Dwarf_Error e = staticerrlist[i]; |
| 296 |
if (e) { |
| 297 |
dw_empty_errlist_item(e); |
| 298 |
staticerrlist[i] = 0; |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
static const |
| 304 |
struct ial_s alloc_instance_basics[ALLOC_AREA_INDEX_TABLE_MAX] = { |
| 305 |
/* 0 none */ |
| 306 |
{ 1,MULTIPLY_NO, 0, 0}, |
| 307 |
|
| 308 |
/* 0x1 x1 DW_DLA_STRING */ |
| 309 |
{ 1,MULTIPLY_CT, 0, 0}, |
| 310 |
|
| 311 |
/* 0x2 DW_DLA_LOC */ |
| 312 |
{1/* sizeof(Dwarf_Loc)*/,MULTIPLY_NO, 0, 0} , |
| 313 |
|
| 314 |
/* x3 DW_DLA_LOCDESC */ |
| 315 |
{1/* sizeof(Dwarf_Locdesc)*/,MULTIPLY_NO, 0, 0}, |
| 316 |
|
| 317 |
/* 0x4 DW_DLA_ELLIST */ /* not used */ |
| 318 |
{ 1,MULTIPLY_NO, 0, 0}, |
| 319 |
|
| 320 |
/* 0x5 DW_DLA_BOUNDS */ /* not used */ |
| 321 |
{ 1,MULTIPLY_NO, 0, 0}, |
| 322 |
|
| 323 |
/* 0x6 DW_DLA_BLOCK */ |
| 324 |
{ sizeof(Dwarf_Block),MULTIPLY_NO, 0, 0}, |
| 325 |
|
| 326 |
/* x7 DW_DLA_DEBUG */ |
| 327 |
/* the actual dwarf_debug structure */ |
| 328 |
{ 1,MULTIPLY_NO, 0, 0} , |
| 329 |
|
| 330 |
/* x8 DW_DLA_DIE */ |
| 331 |
{sizeof(struct Dwarf_Die_s),MULTIPLY_NO, 0, 0}, |
| 332 |
|
| 333 |
/* x9 DW_DLA_LINE */ |
| 334 |
{sizeof(struct Dwarf_Line_s),MULTIPLY_NO, 0, 0}, |
| 335 |
|
| 336 |
/* 0xa 10 DW_DLA_ATTR */ |
| 337 |
{sizeof(struct Dwarf_Attribute_s),MULTIPLY_NO, 0, 0}, |
| 338 |
|
| 339 |
/* 0xb DW_DLA_TYPE *//* not used */ |
| 340 |
{1,MULTIPLY_NO, 0, 0}, |
| 341 |
|
| 342 |
/* 0xc DW_DLA_SUBSCR *//* not used */ |
| 343 |
{1,MULTIPLY_NO, 0, 0}, |
| 344 |
|
| 345 |
/* 0xd 13 DW_DLA_GLOBAL */ |
| 346 |
{sizeof(struct Dwarf_Global_s),MULTIPLY_NO, 0, 0}, |
| 347 |
|
| 348 |
/* 0xe 14 DW_DLA_ERROR */ |
| 349 |
{sizeof(struct Dwarf_Error_s),MULTIPLY_NO, 0, |
| 350 |
_dwarf_error_destructor}, |
| 351 |
|
| 352 |
/* 0xf DW_DLA_LIST */ |
| 353 |
{sizeof(Dwarf_Ptr),MULTIPLY_CT, 0, 0}, |
| 354 |
|
| 355 |
/* 0x10 DW_DLA_LINEBUF */ /* not used */ |
| 356 |
{1,MULTIPLY_NO, 0, 0}, |
| 357 |
|
| 358 |
/* 0x11 17 DW_DLA_ARANGE */ |
| 359 |
{sizeof(struct Dwarf_Arange_s),MULTIPLY_NO, 0, 0}, |
| 360 |
|
| 361 |
/* 0x12 18 DW_DLA_ABBREV Used by dwarf_get_abbrev() */ |
| 362 |
{sizeof(struct Dwarf_Abbrev_s),MULTIPLY_NO, 0, 0}, |
| 363 |
|
| 364 |
/* 0x13 19 DW_DLA_FRAME_INSTR_HEAD */ |
| 365 |
{sizeof(struct Dwarf_Frame_Instr_Head_s),MULTIPLY_NO, 0, |
| 366 |
_dwarf_frame_instr_destructor} , |
| 367 |
|
| 368 |
/* 0x14 20 DW_DLA_CIE */ |
| 369 |
{sizeof(struct Dwarf_Cie_s),MULTIPLY_NO, 0, 0}, |
| 370 |
|
| 371 |
/* 0x15 DW_DLA_FDE */ |
| 372 |
{sizeof(struct Dwarf_Fde_s),MULTIPLY_NO, 0, |
| 373 |
_dwarf_fde_destructor}, |
| 374 |
|
| 375 |
/* 0x16 DW_DLA_LOC_BLOCK */ |
| 376 |
{1 /*sizeof(Dwarf_Loc)*/,MULTIPLY_CT, 0, 0}, |
| 377 |
|
| 378 |
/* 0x17 DW_DLA_FRAME_OP UNUSED */ |
| 379 |
{sizeof(int),MULTIPLY_NO, 0, 0}, |
| 380 |
|
| 381 |
/* 0x18 DW_DLA_FUNC UNUSED */ |
| 382 |
{sizeof(struct Dwarf_Global_s),MULTIPLY_NO, 0, 0}, |
| 383 |
|
| 384 |
/* 0x19 DW_DLA_UARRAY */ |
| 385 |
{sizeof(Dwarf_Off),MULTIPLY_CT, 0, 0}, |
| 386 |
|
| 387 |
/* 0x1a DW_DLA_VAR UNUSED */ |
| 388 |
{sizeof(struct Dwarf_Global_s),MULTIPLY_NO, 0, 0}, |
| 389 |
|
| 390 |
/* 0x1b DW_DLA_WEAK UNUSED */ |
| 391 |
{sizeof(struct Dwarf_Global_s),MULTIPLY_NO, 0, 0}, |
| 392 |
|
| 393 |
/* 0x1c DW_DLA_ADDR */ |
| 394 |
{1,MULTIPLY_SP, 0, 0}, |
| 395 |
|
| 396 |
/* 0x1d DW_DLA_RANGES */ |
| 397 |
{sizeof(Dwarf_Ranges),MULTIPLY_CT, 0,0 }, |
| 398 |
|
| 399 |
/* The following DW_DLA data types |
| 400 |
are known only inside libdwarf. */ |
| 401 |
|
| 402 |
/* 0x1e DW_DLA_ABBREV_LIST No longer alloc'd, |
| 403 |
just plain calloc instead. |
| 404 |
Found in each CU_Context These |
| 405 |
form a singly-linked list. */ |
| 406 |
{ sizeof(struct Dwarf_Abbrev_List_s),MULTIPLY_NO, 0,0}, |
| 407 |
|
| 408 |
/* 0x1f DW_DLA_CHAIN */ |
| 409 |
{sizeof(struct Dwarf_Chain_s),MULTIPLY_NO, 0, 0}, |
| 410 |
|
| 411 |
/* 0x20 DW_DLA_CU_CONTEXT */ |
| 412 |
{sizeof(struct Dwarf_CU_Context_s),MULTIPLY_NO, 0, 0}, |
| 413 |
|
| 414 |
/* 0x21 DW_DLA_FRAME */ |
| 415 |
{sizeof(struct Dwarf_Frame_s),MULTIPLY_NO, |
| 416 |
_dwarf_frame_constructor, |
| 417 |
_dwarf_frame_destructor}, |
| 418 |
|
| 419 |
/* 0x22 DW_DLA_GLOBAL_CONTEXT */ |
| 420 |
{sizeof(struct Dwarf_Global_Context_s),MULTIPLY_NO, 0, 0}, |
| 421 |
|
| 422 |
/* 0x23 DW_DLA_FILE_ENTRY */ |
| 423 |
{sizeof(struct Dwarf_File_Entry_s),MULTIPLY_NO, 0, 0}, |
| 424 |
|
| 425 |
/* 0x24 DW_DLA_LINE_CONTEXT */ |
| 426 |
{sizeof(struct Dwarf_Line_Context_s),MULTIPLY_NO, |
| 427 |
_dwarf_line_context_constructor, |
| 428 |
_dwarf_line_context_destructor}, |
| 429 |
|
| 430 |
/* 0x25 DW_DLA_LOC_CHAIN */ |
| 431 |
{sizeof(struct Dwarf_Loc_Chain_s),MULTIPLY_NO, 0, 0}, |
| 432 |
|
| 433 |
/* 0x26 0x26 DW_DLA_HASH_TABLE No longer used |
| 434 |
as dealloc or dwarf_get_alloc */ |
| 435 |
{sizeof(int),MULTIPLY_NO, 0, 0}, |
| 436 |
|
| 437 |
/* The following really use Global struct: used to be |
| 438 |
unique struct per type, but now merged (11/99). The |
| 439 |
opaque types are visible in the interface. |
| 440 |
The types for |
| 441 |
DW_DLA_FUNC, DW_DLA_TYPENAME, DW_DLA_VAR, DW_DLA_WEAK |
| 442 |
also use the global types. */ |
| 443 |
|
| 444 |
/* 0x27 DW_DLA_FUNC_CONTEXT */ |
| 445 |
{sizeof(struct Dwarf_Global_Context_s),MULTIPLY_NO, 0, 0}, |
| 446 |
|
| 447 |
/* 0x28 40 DW_DLA_TYPENAME_CONTEXT */ |
| 448 |
{sizeof(struct Dwarf_Global_Context_s),MULTIPLY_NO, 0, 0}, |
| 449 |
|
| 450 |
/* 0x29 41 DW_DLA_VAR_CONTEXT */ |
| 451 |
{sizeof(struct Dwarf_Global_Context_s),MULTIPLY_NO, 0, 0}, |
| 452 |
|
| 453 |
/* 0x2a 42 DW_DLA_WEAK_CONTEXT */ |
| 454 |
{sizeof(struct Dwarf_Global_Context_s),MULTIPLY_NO, 0, 0}, |
| 455 |
|
| 456 |
/* 0x2b 43 DW_DLA_PUBTYPES_CONTEXT DWARF3 */ |
| 457 |
{sizeof(struct Dwarf_Global_Context_s),MULTIPLY_NO, 0, 0}, |
| 458 |
|
| 459 |
/* 0x2c 44 DW_DLA_HASH_TABLE_ENTRY. No longer used. */ |
| 460 |
{sizeof(int),MULTIPLY_NO, 0, 0}, |
| 461 |
|
| 462 |
/* 0x2d - 0x34 reserved */ |
| 463 |
{sizeof(int),MULTIPLY_NO, 0, 0}, |
| 464 |
|
| 465 |
/* 0x2e 46 reserved for future use */ |
| 466 |
{sizeof(int),MULTIPLY_NO, 0, 0}, |
| 467 |
|
| 468 |
/* 0x2f 47 reserved for future use */ |
| 469 |
{sizeof(int),MULTIPLY_NO, 0, 0}, |
| 470 |
|
| 471 |
/* 0x30 reserved for future internal use */ |
| 472 |
{sizeof(int),MULTIPLY_NO, 0, 0}, |
| 473 |
|
| 474 |
/* 0x31 reserved for future internal use */ |
| 475 |
{sizeof(int),MULTIPLY_NO, 0, 0}, |
| 476 |
|
| 477 |
/* 0x32 50 reserved for future internal use */ |
| 478 |
{sizeof(int),MULTIPLY_NO, 0, 0}, |
| 479 |
|
| 480 |
/* 0x33 51 reserved for future internal use */ |
| 481 |
{sizeof(int),MULTIPLY_NO, 0, 0}, |
| 482 |
|
| 483 |
/* 0x34 52 reserved for future internal use */ |
| 484 |
{sizeof(int),MULTIPLY_NO, 0, 0}, |
| 485 |
|
| 486 |
/* 0x35 53 Used starting July 2020 DW_DLA_GNU_INDEX_HEAD */ |
| 487 |
{sizeof(struct Dwarf_Gnu_Index_Head_s),MULTIPLY_NO, 0, |
| 488 |
_dwarf_gnu_index_head_destructor}, |
| 489 |
|
| 490 |
/* 0x36 54 Used starting May 2020 DW_DLA_RNGLISTS_HEAD */ |
| 491 |
{sizeof(struct Dwarf_Rnglists_Head_s),MULTIPLY_NO, 0, |
| 492 |
_dwarf_rnglists_head_destructor}, |
| 493 |
|
| 494 |
/* now, we have types that are public. */ |
| 495 |
/* 0x37 55. New in June 2014. Gdb. */ |
| 496 |
{sizeof(struct Dwarf_Gdbindex_s),MULTIPLY_NO, 0, 0}, |
| 497 |
|
| 498 |
/* 0x38 56. New in July 2014. */ |
| 499 |
/* DWARF5 DebugFission dwp file sections |
| 500 |
.debug_cu_index and .debug_tu_index . */ |
| 501 |
{sizeof(struct Dwarf_Xu_Index_Header_s),MULTIPLY_NO, 0, 0}, |
| 502 |
|
| 503 |
/* These required by new features in DWARF5. Also usable |
| 504 |
for DWARF2,3,4. */ |
| 505 |
/* 0x39 57 DW_DLA_LOC_BLOCK_C DWARF5 */ |
| 506 |
{sizeof(struct Dwarf_Loc_Expr_Op_s),MULTIPLY_CT, 0, 0}, |
| 507 |
|
| 508 |
/* 0x3a 58 DW_DLA_LOCDESC_C */ |
| 509 |
{sizeof(struct Dwarf_Locdesc_c_s),MULTIPLY_CT, |
| 510 |
_dwarf_locdesc_c_constructor, 0}, |
| 511 |
|
| 512 |
/* 0x3b 59 DW_DLA_LOC_HEAD_C */ |
| 513 |
{sizeof(struct Dwarf_Loc_Head_c_s),MULTIPLY_NO, 0, |
| 514 |
_dwarf_loclists_head_destructor}, |
| 515 |
|
| 516 |
/* 0x3c 60 DW_DLA_MACRO_CONTEXT */ |
| 517 |
{sizeof(struct Dwarf_Macro_Context_s),MULTIPLY_NO, |
| 518 |
_dwarf_macro_constructor, |
| 519 |
_dwarf_macro_destructor}, |
| 520 |
|
| 521 |
/* 0x3d 61 DW_DLA_CHAIN_2 */ |
| 522 |
{sizeof(struct Dwarf_Chain_o),MULTIPLY_NO, 0, 0}, |
| 523 |
|
| 524 |
/* 0x3e 62 DW_DLA_DSC_HEAD */ |
| 525 |
{sizeof(struct Dwarf_Dsc_Head_s),MULTIPLY_NO, 0, |
| 526 |
_dwarf_dsc_destructor}, |
| 527 |
|
| 528 |
/* 0x3f 63 DW_DLA_DNAMES_HEAD */ |
| 529 |
{sizeof(struct Dwarf_Dnames_Head_s),MULTIPLY_NO, 0, |
| 530 |
_dwarf_dnames_destructor}, |
| 531 |
|
| 532 |
/* 0x40 64 DW_DLA_STR_OFFSETS */ |
| 533 |
{sizeof(struct Dwarf_Str_Offsets_Table_s),MULTIPLY_NO, 0,0}, |
| 534 |
|
| 535 |
/* 0x41 65 DW_DLA_DEBUG_ADDR */ |
| 536 |
{sizeof(struct Dwarf_Debug_Addr_Table_s),MULTIPLY_NO, 0,0}, |
| 537 |
}; |
| 538 |
|
| 539 |
/* We are simply using the incoming pointer as the key-pointer. |
| 540 |
*/ |
| 541 |
|
| 542 |
static DW_TSHASHTYPE |
| 543 |
simple_value_hashfunc(const void *keyp) |
| 544 |
{ |
| 545 |
DW_TSHASHTYPE up = (DW_TSHASHTYPE)(uintptr_t)keyp; |
| 546 |
return up; |
| 547 |
} |
| 548 |
/* We did alloc something but not a fixed-length thing. |
| 549 |
Instead, it starts with some special data we noted. |
| 550 |
The incoming pointer is to the caller data, we |
| 551 |
destruct based on caller, but find the special |
| 552 |
extra data in a prefix area. */ |
| 553 |
static void |
| 554 |
tdestroy_free_node(void *nodep) |
| 555 |
{ |
| 556 |
char *m = 0; |
| 557 |
char *malloc_addr = 0; |
| 558 |
struct reserve_data_s *reserve = 0; |
| 559 |
unsigned int type = 0; |
| 560 |
|
| 561 |
m = (char *)nodep; |
| 562 |
if ((uintptr_t)m > DW_RESERVE) { |
| 563 |
malloc_addr = m - DW_RESERVE; |
| 564 |
} else { |
| 565 |
/* impossible */ |
| 566 |
return; |
| 567 |
} |
| 568 |
reserve = (struct reserve_data_s *)malloc_addr; |
| 569 |
type = reserve->rd_type; |
| 570 |
if (type >= ALLOC_AREA_INDEX_TABLE_MAX) { |
| 571 |
/* Internal error, corrupted data. */ |
| 572 |
return; |
| 573 |
} |
| 574 |
if (!reserve->rd_dbg) { |
| 575 |
/* Unused (corrupted?) node in the tree. |
| 576 |
Should never happen. */ |
| 577 |
return; |
| 578 |
} |
| 579 |
if (!reserve->rd_type) { |
| 580 |
/* Unused (corrupted?) node in the tree. |
| 581 |
Should never happen. */ |
| 582 |
return; |
| 583 |
} |
| 584 |
if (alloc_instance_basics[type].specialdestructor) { |
| 585 |
alloc_instance_basics[type].specialdestructor(m); |
| 586 |
} |
| 587 |
free(malloc_addr); |
| 588 |
} |
| 589 |
|
| 590 |
/* The sort of hash table entries result in very simple |
| 591 |
helper functions. */ |
| 592 |
static int |
| 593 |
simple_compare_function(const void *l, const void *r) |
| 594 |
{ |
| 595 |
DW_TSHASHTYPE lp = (DW_TSHASHTYPE)(uintptr_t)l; |
| 596 |
DW_TSHASHTYPE rp = (DW_TSHASHTYPE)(uintptr_t)r; |
| 597 |
if (lp < rp) { |
| 598 |
return -1; |
| 599 |
} |
| 600 |
if (lp > rp) { |
| 601 |
return 1; |
| 602 |
} |
| 603 |
return 0; |
| 604 |
} |
| 605 |
|
| 606 |
/* This function returns a pointer to a region |
| 607 |
of memory. For alloc_types that are not |
| 608 |
strings or lists of pointers, only 1 struct |
| 609 |
can be requested at a time. This is indicated |
| 610 |
by an input count of 1. For strings, count |
| 611 |
equals the length of the string it will |
| 612 |
contain, i.e it the length of the string |
| 613 |
plus 1 for the terminating null. For lists |
| 614 |
of pointers, count is equal to the number of |
| 615 |
pointers. For DW_DLA_FRAME_BLOCK, DW_DLA_RANGES, and |
| 616 |
DW_DLA_LOC_BLOCK allocation types also, count |
| 617 |
is the count of the number of structs needed. |
| 618 |
|
| 619 |
This function cannot be used to allocate a |
| 620 |
Dwarf_Debug_s struct. */ |
| 621 |
/* coverity[+alloc] */ |
| 622 |
char * |
| 623 |
_dwarf_get_alloc(Dwarf_Debug dbg, |
| 624 |
Dwarf_Small alloc_type, Dwarf_Unsigned count) |
| 625 |
{ |
| 626 |
char * alloc_mem = 0; |
| 627 |
Dwarf_Unsigned basesize = 0; |
| 628 |
Dwarf_Unsigned size = 0; |
| 629 |
unsigned int type = alloc_type; |
| 630 |
short action = 0; |
| 631 |
|
| 632 |
if (IS_INVALID_DBG(dbg)) { |
| 633 |
#if DEBUG_ALLOC |
| 634 |
printf("libdwarfdetector ALLOC dbg null " |
| 635 |
"ret NULL type 0x%x size %lu line %d %s\n", |
| 636 |
(unsigned)alloc_type,(unsigned long)size, |
| 637 |
__LINE__,__FILE__); |
| 638 |
fflush(stdout); |
| 639 |
#endif /* DEBUG_ALLOC */ |
| 640 |
return NULL; |
| 641 |
} |
| 642 |
if (type >= ALLOC_AREA_INDEX_TABLE_MAX) { |
| 643 |
/* internal error */ |
| 644 |
#if DEBUG_ALLOC |
| 645 |
printf("libdwarfdetector ALLOC type bad ret null " |
| 646 |
"ret NULL type 0x%x size %lu line %d %s\n", |
| 647 |
(unsigned)alloc_type,(unsigned long)size, |
| 648 |
__LINE__,__FILE__); |
| 649 |
fflush(stdout); |
| 650 |
#endif /* DEBUG_ALLOC */ |
| 651 |
return NULL; |
| 652 |
} |
| 653 |
basesize = alloc_instance_basics[alloc_type].ia_struct_size; |
| 654 |
action = alloc_instance_basics[alloc_type].ia_multiply_count; |
| 655 |
if (action == MULTIPLY_NO) { |
| 656 |
/* Usually count is 1, but do not assume it. */ |
| 657 |
size = basesize; |
| 658 |
} else if (action == MULTIPLY_CT) { |
| 659 |
size = basesize * count; |
| 660 |
} else { |
| 661 |
/* MULTIPLY_SP */ |
| 662 |
/* DW_DLA_ADDR.. count * largest size */ |
| 663 |
size = count * |
| 664 |
(sizeof(Dwarf_Addr) > sizeof(Dwarf_Off) ? |
| 665 |
sizeof(Dwarf_Addr) : sizeof(Dwarf_Off)); |
| 666 |
} |
| 667 |
size += DW_RESERVE; |
| 668 |
alloc_mem = malloc(size); |
| 669 |
if (!alloc_mem) { |
| 670 |
return NULL; |
| 671 |
} |
| 672 |
{ |
| 673 |
char * ret_mem = alloc_mem + DW_RESERVE; |
| 674 |
void *key = ret_mem; |
| 675 |
struct reserve_data_s *r = (struct reserve_data_s*)alloc_mem; |
| 676 |
void *result = 0; |
| 677 |
|
| 678 |
memset(alloc_mem, 0, size); |
| 679 |
/* We are not actually using rd_dbg, we are using rd_type. */ |
| 680 |
r->rd_dbg = dbg; |
| 681 |
r->rd_type = (unsigned short)alloc_type; |
| 682 |
/* The following is wrong for large records, but |
| 683 |
it's not important, so let it be truncated.*/ |
| 684 |
r->rd_length = (unsigned short)size; |
| 685 |
if (alloc_instance_basics[type].specialconstructor) { |
| 686 |
int res = alloc_instance_basics[type]. |
| 687 |
specialconstructor(dbg, ret_mem); |
| 688 |
if (res != DW_DLV_OK) { |
| 689 |
/* We leak what we allocated in |
| 690 |
_dwarf_find_memory when |
| 691 |
constructor fails. */ |
| 692 |
#if DEBUG_ALLOC |
| 693 |
printf("libdwarfdetector ALLOC constructor fails ret NULL " |
| 694 |
"type 0x%x size %lu line %d %s\n", |
| 695 |
(unsigned)alloc_type,(unsigned long)size,__LINE__,__FILE__); |
| 696 |
fflush(stdout); |
| 697 |
#endif /* DEBUG_ALLOC */ |
| 698 |
return NULL; |
| 699 |
} |
| 700 |
} |
| 701 |
/* See global flag. |
| 702 |
If zero then caller chooses not |
| 703 |
to track allocations, so dwarf_finish() |
| 704 |
is unable to free anything the caller |
| 705 |
omitted to dealloc. Normally |
| 706 |
the global flag is non-zero */ |
| 707 |
/* As of March 14, 2020 it's |
| 708 |
not necessary to test for alloc type, but instead |
| 709 |
only call tsearch if de_alloc_tree_on. */ |
| 710 |
if (global_de_alloc_tree_on) { |
| 711 |
result = dwarf_tsearch((void *)key, |
| 712 |
&dbg->de_alloc_tree,simple_compare_function); |
| 713 |
if (!result) { |
| 714 |
/* Something badly wrong. Out of memory. |
| 715 |
pretend all is well. */ |
| 716 |
} |
| 717 |
} |
| 718 |
#if DEBUG_ALLOC |
| 719 |
printf("\nlibdwarfdetector ALLOC ret 0x%lx type 0x%x " |
| 720 |
"size %lu line %d %s\n", |
| 721 |
(unsigned long)ret_mem,(unsigned)alloc_type, |
| 722 |
(unsigned long)size,__LINE__,__FILE__); |
| 723 |
fflush(stdout); |
| 724 |
#endif /* DEBUG_ALLOC */ |
| 725 |
return (ret_mem); |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
/* This was once a long list of tests using dss_data |
| 730 |
and dss_size to see if 'space' was inside a debug section. |
| 731 |
This tfind approach removes that maintenance headache. */ |
| 732 |
static int |
| 733 |
string_is_in_debug_section(Dwarf_Debug dbg,void * space) |
| 734 |
{ |
| 735 |
/* See dwarf_line.c dwarf_srcfiles() |
| 736 |
for one way we can wind up with |
| 737 |
a DW_DLA_STRING string that may or may not be malloc-ed |
| 738 |
by _dwarf_get_alloc(). |
| 739 |
|
| 740 |
dwarf_formstring(), for example, returns strings |
| 741 |
which point into .debug_info or .debug_types but |
| 742 |
dwarf_dealloc is never supposed to be applied |
| 743 |
to strings dwarf_formstring() returns! |
| 744 |
|
| 745 |
Lots of calls returning strings |
| 746 |
have always been documented as requiring |
| 747 |
dwarf_dealloc(...DW_DLA_STRING) when the code |
| 748 |
just returns a pointer to a portion of a loaded section! |
| 749 |
It is too late to change the documentation. */ |
| 750 |
|
| 751 |
void *result = 0; |
| 752 |
|
| 753 |
/* The alloc tree can be in main or tied or both. */ |
| 754 |
result = dwarf_tfind((void *)space, |
| 755 |
&dbg->de_alloc_tree,simple_compare_function); |
| 756 |
if (!result) { |
| 757 |
/* Not in the tree, so not malloc-ed |
| 758 |
Nothing to delete. */ |
| 759 |
return TRUE; |
| 760 |
} |
| 761 |
/* We found the address in the tree, so it is NOT |
| 762 |
part of .debug_info or any other dwarf section, |
| 763 |
but is space malloc-d in _dwarf_get_alloc(). */ |
| 764 |
return FALSE; |
| 765 |
} |
| 766 |
|
| 767 |
/* These wrappers for dwarf_dealloc enable type-checking |
| 768 |
at call points. */ |
| 769 |
void |
| 770 |
dwarf_dealloc_error(Dwarf_Debug dbg, Dwarf_Error err) |
| 771 |
{ |
| 772 |
dwarf_dealloc(dbg,err,DW_DLA_ERROR); |
| 773 |
} |
| 774 |
void |
| 775 |
dwarf_dealloc_die( Dwarf_Die die) |
| 776 |
{ |
| 777 |
Dwarf_Debug dbg = 0; |
| 778 |
Dwarf_CU_Context context = 0; |
| 779 |
|
| 780 |
if (!die) { |
| 781 |
#ifdef DEBUG_ALLOC |
| 782 |
printf("DEALLOC die does nothing, die NULL line %d %s\n", |
| 783 |
__LINE__,__FILE__); |
| 784 |
fflush(stdout); |
| 785 |
#endif /* DEBUG_ALLOC */ |
| 786 |
return; |
| 787 |
} |
| 788 |
context = die->di_cu_context; |
| 789 |
if (!context) { |
| 790 |
#ifdef DEBUG_ALLOC |
| 791 |
printf("DEALLOC die does nothing, context NULL line %d %s\n", |
| 792 |
__LINE__,__FILE__); |
| 793 |
fflush(stdout); |
| 794 |
#endif /* DEBUG_ALLOC */ |
| 795 |
return; |
| 796 |
} |
| 797 |
dbg = context->cc_dbg; |
| 798 |
if (IS_INVALID_DBG(dbg)) { |
| 799 |
return; |
| 800 |
} |
| 801 |
dwarf_dealloc(dbg,die,DW_DLA_DIE); |
| 802 |
} |
| 803 |
|
| 804 |
void |
| 805 |
dwarf_dealloc_attribute(Dwarf_Attribute attr) |
| 806 |
{ |
| 807 |
Dwarf_Debug dbg = 0; |
| 808 |
|
| 809 |
if (!attr) { |
| 810 |
#ifdef DEBUG_ALLOC |
| 811 |
printf("DEALLOC does nothing, attr is NULL line %d %s\n", |
| 812 |
__LINE__,__FILE__); |
| 813 |
fflush(stdout); |
| 814 |
#endif /* DEBUG_ALLOC */ |
| 815 |
return; |
| 816 |
} |
| 817 |
dbg = attr->ar_dbg; |
| 818 |
dwarf_dealloc(dbg,attr,DW_DLA_ATTR); |
| 819 |
} |
| 820 |
/* |
| 821 |
This function is used to deallocate a region of memory |
| 822 |
that was obtained by a call to _dwarf_get_alloc. Note |
| 823 |
that though dwarf_dealloc() is a public function, |
| 824 |
_dwarf_get_alloc() isn't. |
| 825 |
|
| 826 |
For lists, typically arrays of pointers, it is assumed |
| 827 |
that the space was allocated by a direct call to malloc, |
| 828 |
and so a straight free() is done. This is also the case |
| 829 |
for variable length blocks such as DW_DLA_FRAME_BLOCK |
| 830 |
and DW_DLA_LOC_BLOCK and DW_DLA_RANGES. |
| 831 |
|
| 832 |
For strings, the pointer might point to a string in |
| 833 |
.debug_info or .debug_string. After this is checked, |
| 834 |
and if found not to be the case, a free() is done, |
| 835 |
again on the assumption that a malloc was used to |
| 836 |
obtain the space. |
| 837 |
|
| 838 |
This function does not return anything. |
| 839 |
The _dwarf_error_destructor() will be called |
| 840 |
to free the er_msg string |
| 841 |
(if this is a Dwarf_Error) just before the |
| 842 |
Dwarf_Error is freed here. See...specialdestructor() |
| 843 |
below. |
| 844 |
|
| 845 |
*/ |
| 846 |
/* coverity[+free : arg-1] */ |
| 847 |
void |
| 848 |
dwarf_dealloc(Dwarf_Debug dbg, |
| 849 |
Dwarf_Ptr space, Dwarf_Unsigned alloc_type) |
| 850 |
{ |
| 851 |
unsigned int type = 0; |
| 852 |
char * malloc_addr = 0; |
| 853 |
struct reserve_data_s * r = 0; |
| 854 |
|
| 855 |
if (!space) { |
| 856 |
#ifdef DEBUG_ALLOC |
| 857 |
printf("DEALLOC does nothing, space NULL line %d %s\n", |
| 858 |
__LINE__,__FILE__); |
| 859 |
fflush(stdout); |
| 860 |
#endif /* DEBUG_ALLOC*/ |
| 861 |
return; |
| 862 |
} |
| 863 |
if (IS_INVALID_DBG(dbg)) { |
| 864 |
/* App error, or an app that failed in a |
| 865 |
dwarf_init*() or dwarf_elf_init*() call. |
| 866 |
|
| 867 |
*/ |
| 868 |
dw_empty_errlist_item(space); |
| 869 |
#ifdef DEBUG_ALLOC |
| 870 |
printf( "DEALLOC dbg NULL line %d %s\n", |
| 871 |
__LINE__,__FILE__); |
| 872 |
fflush(stdout); |
| 873 |
#endif /* DEBUG_ALLOC*/ |
| 874 |
return; |
| 875 |
} |
| 876 |
if (space == (Dwarf_Ptr)&_dwarf_failsafe_error) { |
| 877 |
#ifdef DEBUG_ALLOC |
| 878 |
printf("DEALLOC failsafe requested at 0x%lx. " |
| 879 |
"ignore. line %d %s\n", |
| 880 |
(unsigned long)space, |
| 881 |
__LINE__,__FILE__); |
| 882 |
fflush(stdout); |
| 883 |
return; |
| 884 |
#endif /* DEBUG_ALLOC*/ |
| 885 |
} |
| 886 |
if (dbg && alloc_type == DW_DLA_ERROR) { |
| 887 |
dbg = dbg->de_errors_dbg; |
| 888 |
} |
| 889 |
if (dbg && dbg->de_alloc_tree) { |
| 890 |
/* If it's a string in debug_info etc doing |
| 891 |
(char *)space - DW_RESERVE is totally bogus. */ |
| 892 |
if (alloc_type == DW_DLA_STRING && |
| 893 |
string_is_in_debug_section(dbg,space)) { |
| 894 |
/* A string pointer may point into .debug_info or |
| 895 |
.debug_string etc. |
| 896 |
So must not be freed. And strings have |
| 897 |
no need of a specialdestructor(). |
| 898 |
Mostly a historical mistake here. |
| 899 |
Corrected in libdwarf March 14,2020. */ |
| 900 |
#ifdef DEBUG_ALLOC |
| 901 |
printf( "DEALLOC string in section, no dealloc " |
| 902 |
"line %d %s\n", __LINE__,__FILE__); |
| 903 |
fflush(stdout); |
| 904 |
#endif /* DEBUG_ALLOC*/ |
| 905 |
return; |
| 906 |
} |
| 907 |
} |
| 908 |
/* Otherwise it might be allocated string so it is ok |
| 909 |
do the (char *)space - DW_RESERVE */ |
| 910 |
|
| 911 |
/* If it's a DW_DLA_STRING case and erroneous |
| 912 |
the following pointer operations might |
| 913 |
result in a coredump if the pointer |
| 914 |
is to the beginning of a string section. |
| 915 |
If not DW_DLA_STRING |
| 916 |
no correctly written caller could coredump |
| 917 |
here. */ |
| 918 |
if ((uintptr_t)space > DW_RESERVE) { |
| 919 |
malloc_addr = (char *)space - DW_RESERVE; |
| 920 |
} else { |
| 921 |
/* Impossible */ |
| 922 |
return; |
| 923 |
} |
| 924 |
r =(struct reserve_data_s *)malloc_addr; |
| 925 |
if (dbg && dbg != r->rd_dbg) { |
| 926 |
/* Mixed up or originally a no_dbg alloc */ |
| 927 |
#ifdef DEBUG_ALLOC |
| 928 |
printf("DEALLOC find was NULL dbg 0x%lx " |
| 929 |
"rd_dbg 0x%lx space 0x%lx line %d %s\n", |
| 930 |
(unsigned long)dbg, |
| 931 |
(unsigned long)r->rd_dbg, |
| 932 |
(unsigned long)space, |
| 933 |
__LINE__,__FILE__); |
| 934 |
fflush(stdout); |
| 935 |
#endif /* DEBUG_ALLOC*/ |
| 936 |
} |
| 937 |
if (dbg && alloc_type != r->rd_type) { |
| 938 |
/* Something is mixed up. */ |
| 939 |
#ifdef DEBUG_ALLOC |
| 940 |
printf("DEALLOC does nothing, type 0x%lx rd_type 0x%lx" |
| 941 |
" space 0x%lx line %d %s\n", |
| 942 |
(unsigned long)alloc_type, |
| 943 |
(unsigned long)r->rd_type, |
| 944 |
(unsigned long)space, |
| 945 |
__LINE__,__FILE__); |
| 946 |
fflush(stdout); |
| 947 |
#endif /* DEBUG_ALLOC*/ |
| 948 |
return; |
| 949 |
} |
| 950 |
if (alloc_type == DW_DLA_ERROR) { |
| 951 |
Dwarf_Error ep = (Dwarf_Error)space; |
| 952 |
|
| 953 |
if (ep->er_static_alloc == DE_STATIC) { |
| 954 |
/* This is special, malloc arena |
| 955 |
was exhausted or a NULL dbg |
| 956 |
was used for the error because the real |
| 957 |
dbg was unavailable. |
| 958 |
There is nothing to delete, really. |
| 959 |
Set er_errval to signal that the |
| 960 |
space was dealloc'd. */ |
| 961 |
_dwarf_failsafe_error.er_errval = |
| 962 |
DW_DLE_FAILSAFE_ERRVAL; |
| 963 |
_dwarf_error_destructor(ep); |
| 964 |
#ifdef DEBUG_ALLOC |
| 965 |
printf("DEALLOC does nothing, DE_STATIC line %d %s\n", |
| 966 |
__LINE__,__FILE__); |
| 967 |
fflush(stdout); |
| 968 |
#endif /* DEBUG_ALLOC*/ |
| 969 |
return; |
| 970 |
} |
| 971 |
if (ep->er_static_alloc == DE_MALLOC) { |
| 972 |
/* This is special, we had no arena |
| 973 |
but have a full special area as normal. */ |
| 974 |
#ifdef DEBUG_ALLOC |
| 975 |
printf("DEALLOC does free, DE_MALLOC line %d %s\n", |
| 976 |
__LINE__,__FILE__); |
| 977 |
fflush(stdout); |
| 978 |
#endif /* DEBUG_ALLOC*/ |
| 979 |
_dwarf_remove_from_staticerrlist(space); |
| 980 |
} |
| 981 |
/* Was normal alloc, use normal dealloc. */ |
| 982 |
/* DW_DLA_ERROR has a specialdestructor */ |
| 983 |
} |
| 984 |
/* alloc types are a defined library-private |
| 985 |
set of integers. Less than 256 of them. */ |
| 986 |
type = (unsigned int)alloc_type; |
| 987 |
#if DEBUG_ALLOC |
| 988 |
if (dbg != r->rd_dbg) { |
| 989 |
printf("DEALLOC dbg != rd_dbg" |
| 990 |
" going ahead line %d %s\n", |
| 991 |
__LINE__,__FILE__); |
| 992 |
fflush(stdout); |
| 993 |
} |
| 994 |
printf("libdwarfdetector DEALLOC ret 0x%lx type 0x%x " |
| 995 |
"size %lu line %d %s\n", |
| 996 |
(unsigned long)space,(unsigned)type, |
| 997 |
(unsigned long)r->rd_length,__LINE__,__FILE__); |
| 998 |
#endif /* DEBUG_ALLOC*/ |
| 999 |
if (type >= ALLOC_AREA_INDEX_TABLE_MAX) { |
| 1000 |
/* internal or user app error */ |
| 1001 |
#ifdef DEBUG_ALLOC |
| 1002 |
printf("DEALLOC does nothing, type too big %lu line %d %s\n", |
| 1003 |
(unsigned long)type, |
| 1004 |
__LINE__,__FILE__); |
| 1005 |
fflush(stdout); |
| 1006 |
#endif /* DEBUG_ALLOC*/ |
| 1007 |
return; |
| 1008 |
} |
| 1009 |
if (alloc_instance_basics[type].specialdestructor) { |
| 1010 |
alloc_instance_basics[type].specialdestructor(space); |
| 1011 |
} |
| 1012 |
if (dbg && dbg->de_alloc_tree) { |
| 1013 |
/* The 'space' pointer we get points after the |
| 1014 |
reserve space. The key is 'space' |
| 1015 |
and address to free |
| 1016 |
is just a few bytes before 'space'. */ |
| 1017 |
void *key = space; |
| 1018 |
|
| 1019 |
dwarf_tdelete(key,&dbg->de_alloc_tree, |
| 1020 |
simple_compare_function); |
| 1021 |
/* If dwarf_tdelete returns NULL it might mean |
| 1022 |
a) tree is empty. |
| 1023 |
b) If hashsearch, then a single chain might |
| 1024 |
now be empty, |
| 1025 |
so we do not know of a 'parent node'. |
| 1026 |
c) We did not find that key, we did nothing. |
| 1027 |
|
| 1028 |
In any case, we simply don't worry about it. |
| 1029 |
Not Supposed To Happen. */ |
| 1030 |
} |
| 1031 |
r->rd_dbg = (void *)(uintptr_t)0xfeadbeef; |
| 1032 |
r->rd_length = 0; |
| 1033 |
r->rd_type = 0; |
| 1034 |
free(malloc_addr); |
| 1035 |
return; |
| 1036 |
} |
| 1037 |
|
| 1038 |
/* |
| 1039 |
Allocates space for a Dwarf_Debug_s struct, |
| 1040 |
since one does not exist. |
| 1041 |
*/ |
| 1042 |
Dwarf_Debug |
| 1043 |
_dwarf_get_debug(Dwarf_Unsigned filesize) |
| 1044 |
{ |
| 1045 |
Dwarf_Debug dbg; |
| 1046 |
|
| 1047 |
dbg = (Dwarf_Debug) malloc(sizeof(struct Dwarf_Debug_s)); |
| 1048 |
if (!dbg) { |
| 1049 |
return NULL; |
| 1050 |
} |
| 1051 |
memset(dbg, 0, sizeof(struct Dwarf_Debug_s)); |
| 1052 |
/* Set up for a dwarf_tsearch hash table */ |
| 1053 |
dbg->de_magic = DBG_IS_VALID; |
| 1054 |
|
| 1055 |
if (global_de_alloc_tree_on) { |
| 1056 |
/* The type of the dwarf_initialize_search_hash |
| 1057 |
initial-size argument */ |
| 1058 |
unsigned long size_est = (unsigned long)(filesize/30); |
| 1059 |
|
| 1060 |
#ifdef TESTINGHASHTAB |
| 1061 |
printf("debugging: src filesize %lu hashtab init %lu\n", |
| 1062 |
(unsigned long)filesize,size_est); |
| 1063 |
#endif |
| 1064 |
dwarf_initialize_search_hash(&dbg->de_alloc_tree, |
| 1065 |
simple_value_hashfunc,size_est); |
| 1066 |
} |
| 1067 |
return dbg; |
| 1068 |
} |
| 1069 |
|
| 1070 |
/* In the 'rela' relocation case or in case |
| 1071 |
of compressed sections we might have malloc'd |
| 1072 |
space (to ensure it is read-write or to decompress it |
| 1073 |
respectively, or both). In that case, free the space. */ |
| 1074 |
static void |
| 1075 |
malloc_section_free(struct Dwarf_Section_s * sec) |
| 1076 |
{ |
| 1077 |
if (sec->dss_data_was_malloc) { |
| 1078 |
free(sec->dss_data); |
| 1079 |
} |
| 1080 |
sec->dss_data = 0; |
| 1081 |
sec->dss_data_was_malloc = 0; |
| 1082 |
} |
| 1083 |
|
| 1084 |
static void |
| 1085 |
freecontextlist(Dwarf_Debug dbg, Dwarf_Debug_InfoTypes dis) |
| 1086 |
{ |
| 1087 |
Dwarf_CU_Context context = 0; |
| 1088 |
Dwarf_CU_Context nextcontext = 0; |
| 1089 |
for (context = dis->de_cu_context_list; |
| 1090 |
context; context = nextcontext) { |
| 1091 |
Dwarf_Hash_Table hash_table = 0; |
| 1092 |
|
| 1093 |
hash_table = context->cc_abbrev_hash_table; |
| 1094 |
|
| 1095 |
_dwarf_free_abbrev_hash_table_contents(hash_table, |
| 1096 |
FALSE); |
| 1097 |
hash_table->tb_entries = 0; |
| 1098 |
nextcontext = context->cc_next; |
| 1099 |
context->cc_next = 0; |
| 1100 |
/* See also local_dealloc_cu_context() in |
| 1101 |
dwarf_die_deliv.c */ |
| 1102 |
free(hash_table); |
| 1103 |
context->cc_abbrev_hash_table = 0; |
| 1104 |
dwarf_dealloc(dbg, context, DW_DLA_CU_CONTEXT); |
| 1105 |
} |
| 1106 |
dis->de_cu_context_list = 0; |
| 1107 |
} |
| 1108 |
|
| 1109 |
/* |
| 1110 |
Used to free all space allocated for this Dwarf_Debug. |
| 1111 |
The caller should assume that the Dwarf_Debug pointer |
| 1112 |
itself is no longer valid upon return from this function. |
| 1113 |
|
| 1114 |
NEVER returns DW_DLV_ERROR. |
| 1115 |
|
| 1116 |
In case of difficulty, this function simply returns quietly. |
| 1117 |
*/ |
| 1118 |
int |
| 1119 |
_dwarf_free_all_of_one_debug(Dwarf_Debug dbg) |
| 1120 |
{ |
| 1121 |
unsigned g = 0; |
| 1122 |
|
| 1123 |
if (IS_INVALID_DBG(dbg)) { |
| 1124 |
_dwarf_free_static_errlist(); |
| 1125 |
return DW_DLV_NO_ENTRY; |
| 1126 |
} |
| 1127 |
/* To do complete validation that we have no surprising |
| 1128 |
missing or erroneous deallocs it is advisable to do |
| 1129 |
the dwarf_deallocs here |
| 1130 |
that are not things the user can otherwise request. |
| 1131 |
Housecleaning. */ |
| 1132 |
if (dbg->de_cu_hashindex_data) { |
| 1133 |
dwarf_dealloc_xu_header(dbg->de_cu_hashindex_data); |
| 1134 |
dbg->de_cu_hashindex_data = 0; |
| 1135 |
} |
| 1136 |
if (dbg->de_tu_hashindex_data) { |
| 1137 |
dwarf_dealloc_xu_header(dbg->de_tu_hashindex_data); |
| 1138 |
dbg->de_tu_hashindex_data = 0; |
| 1139 |
} |
| 1140 |
if (dbg->de_printf_callback_null_device_handle) { |
| 1141 |
fclose(dbg->de_printf_callback_null_device_handle); |
| 1142 |
dbg->de_printf_callback_null_device_handle = 0; |
| 1143 |
} |
| 1144 |
freecontextlist(dbg,&dbg->de_info_reading); |
| 1145 |
freecontextlist(dbg,&dbg->de_types_reading); |
| 1146 |
/* Housecleaning done. Now really free all the space. */ |
| 1147 |
malloc_section_free(&dbg->de_debug_info); |
| 1148 |
malloc_section_free(&dbg->de_debug_types); |
| 1149 |
malloc_section_free(&dbg->de_debug_abbrev); |
| 1150 |
malloc_section_free(&dbg->de_debug_line); |
| 1151 |
malloc_section_free(&dbg->de_debug_line_str); |
| 1152 |
malloc_section_free(&dbg->de_debug_loc); |
| 1153 |
malloc_section_free(&dbg->de_debug_aranges); |
| 1154 |
malloc_section_free(&dbg->de_debug_macinfo); |
| 1155 |
malloc_section_free(&dbg->de_debug_macro); |
| 1156 |
malloc_section_free(&dbg->de_debug_names); |
| 1157 |
malloc_section_free(&dbg->de_debug_pubnames); |
| 1158 |
malloc_section_free(&dbg->de_debug_str); |
| 1159 |
malloc_section_free(&dbg->de_debug_sup); |
| 1160 |
malloc_section_free(&dbg->de_debug_frame); |
| 1161 |
malloc_section_free(&dbg->de_debug_frame_eh_gnu); |
| 1162 |
malloc_section_free(&dbg->de_debug_pubtypes); |
| 1163 |
malloc_section_free(&dbg->de_debug_funcnames); |
| 1164 |
malloc_section_free(&dbg->de_debug_typenames); |
| 1165 |
malloc_section_free(&dbg->de_debug_varnames); |
| 1166 |
malloc_section_free(&dbg->de_debug_weaknames); |
| 1167 |
malloc_section_free(&dbg->de_debug_ranges); |
| 1168 |
malloc_section_free(&dbg->de_debug_str_offsets); |
| 1169 |
malloc_section_free(&dbg->de_debug_addr); |
| 1170 |
malloc_section_free(&dbg->de_debug_gdbindex); |
| 1171 |
malloc_section_free(&dbg->de_debug_cu_index); |
| 1172 |
malloc_section_free(&dbg->de_debug_tu_index); |
| 1173 |
malloc_section_free(&dbg->de_debug_loclists); |
| 1174 |
malloc_section_free(&dbg->de_debug_rnglists); |
| 1175 |
malloc_section_free(&dbg->de_gnu_debuglink); |
| 1176 |
malloc_section_free(&dbg->de_note_gnu_buildid); |
| 1177 |
_dwarf_harmless_cleanout(&dbg->de_harmless_errors); |
| 1178 |
|
| 1179 |
_dwarf_dealloc_rnglists_context(dbg); |
| 1180 |
_dwarf_dealloc_loclists_context(dbg); |
| 1181 |
if (dbg->de_printf_callback.dp_buffer && |
| 1182 |
!dbg->de_printf_callback.dp_buffer_user_provided ) { |
| 1183 |
free(dbg->de_printf_callback.dp_buffer); |
| 1184 |
} |
| 1185 |
|
| 1186 |
_dwarf_destroy_group_map(dbg); |
| 1187 |
/* de_alloc_tree might be NULL if |
| 1188 |
global_de_alloc_tree_on is zero. */ |
| 1189 |
if (dbg->de_alloc_tree) { |
| 1190 |
dbg->de_in_tdestroy = TRUE; |
| 1191 |
dwarf_tdestroy(dbg->de_alloc_tree,tdestroy_free_node); |
| 1192 |
dbg->de_in_tdestroy = FALSE; |
| 1193 |
dbg->de_alloc_tree = 0; |
| 1194 |
} |
| 1195 |
_dwarf_free_static_errlist(); |
| 1196 |
/* first, walk the search and free() |
| 1197 |
contents. */ |
| 1198 |
/* Now do the search tree itself */ |
| 1199 |
if (dbg->de_tied_data.td_tied_search) { |
| 1200 |
dwarf_tdestroy(dbg->de_tied_data.td_tied_search, |
| 1201 |
_dwarf_tied_destroy_free_node); |
| 1202 |
dbg->de_tied_data.td_tied_search = 0; |
| 1203 |
} |
| 1204 |
free((void *)dbg->de_path); |
| 1205 |
dbg->de_path = 0; |
| 1206 |
for (g = 0; g < dbg->de_gnu_global_path_count; ++g) { |
| 1207 |
free((char *)dbg->de_gnu_global_paths[g]); |
| 1208 |
dbg->de_gnu_global_paths[g] = 0; |
| 1209 |
} |
| 1210 |
free((void*)dbg->de_gnu_global_paths); |
| 1211 |
dbg->de_gnu_global_paths = 0; |
| 1212 |
dbg->de_gnu_global_path_count = 0; |
| 1213 |
memset(dbg, 0, sizeof(*dbg)); /* Prevent accidental use later. */ |
| 1214 |
free(dbg); |
| 1215 |
return DW_DLV_OK; |
| 1216 |
} |
| 1217 |
/* A special case: we have no dbg, no alloc header etc. |
| 1218 |
So create something out of thin air that we can recognize |
| 1219 |
in dwarf_dealloc. |
| 1220 |
Something with the prefix (prefix space hidden from caller). |
| 1221 |
|
| 1222 |
Only applies to DW_DLA_ERROR, and making up an error record. |
| 1223 |
|
| 1224 |
dwarf_error.c calls this and it adds to the staticerrlist |
| 1225 |
all of which is freed by free_static_errlist(); |
| 1226 |
*/ |
| 1227 |
struct Dwarf_Error_s * |
| 1228 |
_dwarf_special_no_dbg_error_malloc(void) |
| 1229 |
{ |
| 1230 |
Dwarf_Error e = 0; |
| 1231 |
Dwarf_Unsigned len = 0; |
| 1232 |
struct reserve_data_s *base = 0; |
| 1233 |
char *mem = 0; |
| 1234 |
|
| 1235 |
len = sizeof(struct Dwarf_Error_s) + DW_RESERVE; |
| 1236 |
mem = (char *)malloc((size_t)len); |
| 1237 |
if (!mem) { |
| 1238 |
return 0; |
| 1239 |
} |
| 1240 |
memset(mem, 0, len); |
| 1241 |
base = (struct reserve_data_s *)mem; |
| 1242 |
base->rd_dbg = 0; |
| 1243 |
base->rd_length = (unsigned short)sizeof(struct Dwarf_Error_s); |
| 1244 |
base->rd_type = DW_DLA_ERROR; |
| 1245 |
e = (Dwarf_Error)(mem+DW_RESERVE); |
| 1246 |
e->er_static_alloc = DE_MALLOC; |
| 1247 |
return e; |
| 1248 |
} |