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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
/*
Copyright (C) 2008-2020 David Anderson. All Rights Reserved.
Portions Copyright 2012 SN Systems Ltd. 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 /* HAVE_STDLIB_H */
#include "dwarf_incl.h"
#include "dwarf_alloc.h"
#include "dwarf_error.h"
#include "dwarf_util.h"
#include "dwarfstring.h"
#define FALSE 0
#define TRUE 1
struct ranges_entry {
struct ranges_entry *next;
Dwarf_Ranges cur;
};
static void
free_allocated_ranges( struct ranges_entry *base)
{
struct ranges_entry *cur = 0;
struct ranges_entry *next = 0;
for ( cur = base ; cur ; cur = next ) {
next = cur->next;
free(cur);
}
}
/* We encapsulate the macro use so we can
free local malloc resources that would otherwise
leak. See the call points below. */
static int
read_unaligned_addr_check(Dwarf_Debug dbg,
Dwarf_Addr *addr_out,
Dwarf_Small *rangeptr,
unsigned address_size,
Dwarf_Error *error,
Dwarf_Small *section_end)
{
Dwarf_Addr a = 0;
READ_UNALIGNED_CK(dbg,a,
Dwarf_Addr, rangeptr,
address_size,
error,section_end);
*addr_out = a;
return DW_DLV_OK;
}
/* As of DWARF5 the ranges section each range list set has
a range-list-table header. See "7.28 Range List Table"
in the DWARF5 standard.
For DWARF5 the offset should be the offset of
the range-list-table-header for that range list.
For DWARF3 and DWARF4 the offset has to be that
of a range list.
*/
/* Ranges and pc values can be in a split dwarf object.
In that case the appropriate values need to be
incremented by data from the executable in
the compilation unit with the same dwo_id.
We return an error which is on the incoming dbg, not
the possibly-tied-dbg localdbg.
If incoming die is NULL there is no context, so do not look
for a tied file, and address_size is the size
of the overall object, not the address_size of the context. */
#define MAX_ADDR ((address_size == 8)? \
0xffffffffffffffffULL:0xffffffff)
int dwarf_get_ranges_a(Dwarf_Debug dbg,
Dwarf_Off rangesoffset,
Dwarf_Die die,
Dwarf_Ranges ** rangesbuf,
Dwarf_Signed * listlen,
Dwarf_Unsigned * bytecount,
Dwarf_Error * error)
{
Dwarf_Off finaloffset = 0;
int res = 0;
res = dwarf_get_ranges_b(
dbg,rangesoffset,die,
&finaloffset,rangesbuf,listlen,
bytecount,error);
return res;
}
/* New 10 September 2020 to accomodate the
GNU extension of DWARF4 split-dwarf.
The actual_offset field is set by the function
to the actual final offset of the ranges
in the separate tied (a.out) file. */
int dwarf_get_ranges_b(Dwarf_Debug dbg,
Dwarf_Off rangesoffset,
Dwarf_Die die,
Dwarf_Off *actual_offset,
Dwarf_Ranges ** rangesbuf,
Dwarf_Signed * listlen,
Dwarf_Unsigned * bytecount,
Dwarf_Error * error)
{
Dwarf_Small *rangeptr = 0;
Dwarf_Small *beginrangeptr = 0;
Dwarf_Small *section_end = 0;
unsigned entry_count = 0;
struct ranges_entry *base = 0;
struct ranges_entry *last = 0;
struct ranges_entry *curre = 0;
Dwarf_Ranges * ranges_data_out = 0;
unsigned copyindex = 0;
Dwarf_Half address_size = 0;
int res = DW_DLV_ERROR;
Dwarf_Unsigned ranges_base = 0;
Dwarf_Debug localdbg = dbg;
Dwarf_Error localerror = 0;
Dwarf_Half die_version = 3; /* default for dwarf_get_ranges() */
UNUSEDARG Dwarf_Half offset_size = 4;
Dwarf_CU_Context cucontext = 0;
Dwarf_Bool rangeslocal = TRUE;
if (!dbg) {
_dwarf_error(NULL, error, DW_DLE_DBG_NULL);
return DW_DLV_ERROR;
}
address_size = localdbg->de_pointer_size; /* default */
if (die) {
/* If we wind up using the tied file the die_version
had better match! It cannot be other than a match. */
/* Can return DW_DLV_ERROR, not DW_DLV_NO_ENTRY.
Add err code if error. Version comes from the
cu context, not the DIE itself. */
res = dwarf_get_version_of_die(die,&die_version,
&offset_size);
if (res == DW_DLV_ERROR) {
_dwarf_error(dbg, error, DW_DLE_DIE_NO_CU_CONTEXT);
return DW_DLV_ERROR;
}
if (!die->di_cu_context) {
_dwarf_error(dbg, error, DW_DLE_DIE_NO_CU_CONTEXT);
return DW_DLV_ERROR;
}
cucontext = die->di_cu_context;
/* The DW4 ranges base was never used in GNU
but did get emitted, the note says, but
the note is probably obsolete (so, now wrong).
http://llvm.1065342.n5.nabble.com/DebugInfo\
-DW-AT-GNU-ranges-base-in-non-fission-\
td64194.html
*/
/* ranges_base was merged from tied context. */
ranges_base = cucontext->cc_ranges_base;
address_size = cucontext->cc_address_size;
}
localdbg = dbg;
res = _dwarf_load_section(localdbg, &localdbg->de_debug_ranges,
error);
if (res == DW_DLV_ERROR) {
return res;
} else if (res == DW_DLV_NO_ENTRY) {
/* data is in a.out, not dwp */
localdbg = dbg->de_tied_data.td_tied_object;
if (!localdbg) {
return DW_DLV_NO_ENTRY;
}
res = _dwarf_load_section(localdbg, &localdbg->de_debug_ranges,
&localerror);
if (res == DW_DLV_ERROR) {
_dwarf_error_mv_s_to_t(localdbg,&localerror,dbg,error);
return res;
} else if (res == DW_DLV_NO_ENTRY) {
return res;
}
rangeslocal = FALSE;
}
/* Be safe in case adding rangesoffset and rangebase
overflows. */
if (rangesoffset >= localdbg->de_debug_ranges.dss_size) {
/* Documented behavior in libdwarf2.1.mm */
return DW_DLV_NO_ENTRY;
}
if (ranges_base >= localdbg->de_debug_ranges.dss_size) {
dwarfstring m;
dwarfstring_constructor(&m);
dwarfstring_append_printf_u(&m,
"DW_DLE_DEBUG_RANGES_OFFSET_BAD: "
" ranges base is 0x%lx ",ranges_base);
dwarfstring_append_printf_u(&m,
" and section size is 0x%lx.",
localdbg->de_debug_ranges.dss_size);
_dwarf_error_string(dbg, error,
DW_DLE_DEBUG_RANGES_OFFSET_BAD,
dwarfstring_string(&m));
dwarfstring_destructor(&m);
return DW_DLV_ERROR;
}
if (!rangeslocal && ((rangesoffset +ranges_base) >=
localdbg->de_debug_ranges.dss_size)) {
dwarfstring m;
dwarfstring_constructor(&m);
dwarfstring_append_printf_u(&m,
"DW_DLE_DEBUG_RANGES_OFFSET_BAD: "
" ranges base+offset is 0x%lx ",
ranges_base+rangesoffset);
dwarfstring_append_printf_u(&m,
" and section size is 0x%lx.",
localdbg->de_debug_ranges.dss_size);
_dwarf_error_string(dbg, error,
DW_DLE_DEBUG_RANGES_OFFSET_BAD,
dwarfstring_string(&m));
dwarfstring_destructor(&m);
return DW_DLV_ERROR;
}
/* tied address_size must match the dwo address_size */
section_end = localdbg->de_debug_ranges.dss_data +
localdbg->de_debug_ranges.dss_size;
rangeptr = localdbg->de_debug_ranges.dss_data;
if (!rangeslocal) {
/* printing ranges where range source is dwp,
here we just assume present. */
rangesoffset += ranges_base;
}
rangeptr += rangesoffset;
beginrangeptr = rangeptr;
for (;;) {
struct ranges_entry * re = 0;
if (rangeptr == section_end) {
break;
}
if (rangeptr > section_end) {
dwarfstring m;
free_allocated_ranges(base);
dwarfstring_constructor(&m);
dwarfstring_append(&m,
"DW_DLE_DEBUG_RANGES_OFFSET_BAD: "
" ranges pointer ran off the end "
"of the section");
_dwarf_error_string(dbg, error,
DW_DLE_DEBUG_RANGES_OFFSET_BAD,
dwarfstring_string(&m));
dwarfstring_destructor(&m);
return DW_DLV_ERROR;
}
re = calloc(sizeof(struct ranges_entry),1);
if (!re) {
free_allocated_ranges(base);
_dwarf_error(dbg, error, DW_DLE_DEBUG_RANGES_OUT_OF_MEM);
return DW_DLV_ERROR;
}
if ((rangeptr + (2*address_size)) > section_end) {
free(re);
free_allocated_ranges(base);
_dwarf_error_string(dbg, error,
DW_DLE_DEBUG_RANGES_OFFSET_BAD,
"DW_DLE_DEBUG_RANGES_OFFSET_BAD: "
" Not at the end of the ranges section "
" but there is not enough room in the section "
" for the next ranges entry");
return DW_DLV_ERROR;
}
entry_count++;
res = read_unaligned_addr_check(localdbg,&re->cur.dwr_addr1,
rangeptr, address_size,error,section_end);
if (res != DW_DLV_OK) {
free(re);
free_allocated_ranges(base);
return res;
}
rangeptr += address_size;
res = read_unaligned_addr_check(localdbg,&re->cur.dwr_addr2,
rangeptr, address_size,error,section_end);
if (res != DW_DLV_OK) {
free(re);
free_allocated_ranges(base);
return res;
}
rangeptr += address_size;
if (!base) {
base = re;
last = re;
} else {
last->next = re;
last = re;
}
if (re->cur.dwr_addr1 == 0 && re->cur.dwr_addr2 == 0) {
re->cur.dwr_type = DW_RANGES_END;
break;
} else if (re->cur.dwr_addr1 == MAX_ADDR) {
re->cur.dwr_type = DW_RANGES_ADDRESS_SELECTION;
} else {
re->cur.dwr_type = DW_RANGES_ENTRY;
}
}
/* We return ranges on dbg, so use that to allocate. */
ranges_data_out = (Dwarf_Ranges *)
_dwarf_get_alloc(dbg,DW_DLA_RANGES,entry_count);
if (!ranges_data_out) {
/* Error, apply to original, not local dbg. */
free_allocated_ranges(base);
_dwarf_error(dbg, error, DW_DLE_DEBUG_RANGES_OUT_OF_MEM);
return (DW_DLV_ERROR);
}
curre = base;
*rangesbuf = ranges_data_out;
*listlen = entry_count;
for (copyindex = 0; curre && (copyindex < entry_count);
++copyindex,++ranges_data_out) {
*ranges_data_out = curre->cur;
curre = curre->next;
}
/* ASSERT: curre == NULL */
free_allocated_ranges(base);
base = 0;
/* Callers will often not care about the bytes used. */
if (actual_offset) {
*actual_offset = rangesoffset;
}
if (bytecount) {
*bytecount = rangeptr - beginrangeptr;
}
return DW_DLV_OK;
}
int dwarf_get_ranges(Dwarf_Debug dbg,
Dwarf_Off rangesoffset,
Dwarf_Ranges ** rangesbuf,
Dwarf_Signed * listlen,
Dwarf_Unsigned * bytecount,
Dwarf_Error * error)
{
Dwarf_Die die = 0;
int res = dwarf_get_ranges_a(dbg,rangesoffset,die,
rangesbuf,listlen,bytecount,error);
return res;
}
void
dwarf_ranges_dealloc(Dwarf_Debug dbg, Dwarf_Ranges * rangesbuf,
UNUSEDARG Dwarf_Signed rangecount)
{
dwarf_dealloc(dbg,rangesbuf, DW_DLA_RANGES);
}
|