dwarf_arange.c 13.2 KB
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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
#include <stdio.h>
#include "dwarf_incl.h"
#include "dwarf_arange.h"


/*
    This function returns the count of the number of
    aranges in the .debug_aranges section.  It sets
    aranges to point to a block of Dwarf_Arange's 
    describing the arange's.  It returns DW_DLV_ERROR
    on error.

    Must be identical in most aspects to
	dwarf_get_aranges_addr_offsets!
*/
int
dwarf_get_aranges (
    Dwarf_Debug		dbg,
    Dwarf_Arange	**aranges,
    Dwarf_Signed       *returned_count,
    Dwarf_Error		*error
)
{
	/* Sweeps the .debug_aranges section. */
    Dwarf_Small		*arange_ptr;

	/* 
	    Start of arange header.  Used for rounding offset of
	    arange_ptr to twice the tuple size.  Libdwarf requirement.
	*/
    Dwarf_Small		*header_ptr;

	/* Length of current set of aranges. */
    Dwarf_Unsigned	length;

	/* Version of .debug_aranges header. */
    Dwarf_Half		version;

        /* Offset of current set of aranges into .debug_info. */
    Dwarf_Off	        info_offset;

	/* Size in bytes of addresses in target. */
    Dwarf_Small		address_size;

	/* Size in bytes of segment offsets in target. */
    Dwarf_Small		segment_size;

    Dwarf_Small		remainder;

	/* Count of total number of aranges. */
    Dwarf_Unsigned	arange_count = 0;

	/* Start address of arange. */
    Dwarf_Addr		range_address;

	/* Length of arange. */
    Dwarf_Unsigned	range_length;

    Dwarf_Arange	arange, *arange_block;

    Dwarf_Unsigned	i;

	/* Used to chain Dwarf_Aranges structs. */
    Dwarf_Chain		curr_chain, prev_chain, head_chain = NULL;

    /* ***** BEGIN CODE ***** */

    if (dbg == NULL)
	{_dwarf_error(NULL, error, DW_DLE_DBG_NULL); return(DW_DLV_ERROR);}

    if (dbg->de_debug_aranges == NULL) {
	return(DW_DLV_NO_ENTRY);
    }

    arange_ptr = dbg->de_debug_aranges;
    do {
	header_ptr = arange_ptr;

	    /* Length of aranges for this compilation-unit. */
	READ_UNALIGNED(length, arange_ptr, dbg->de_length_size);
	arange_ptr += dbg->de_length_size;

	READ_UNALIGNED(version, arange_ptr, sizeof(Dwarf_Half));
	arange_ptr += sizeof(Dwarf_Half);
	length = length - sizeof(Dwarf_Half);
	if (version != CURRENT_VERSION_STAMP) {
	    _dwarf_error(dbg, error, DW_DLE_VERSION_STAMP_ERROR);
	    return(DW_DLV_ERROR);
	}

	READ_UNALIGNED(info_offset, arange_ptr, dbg->de_length_size);
	arange_ptr += dbg->de_length_size;
	length = length - dbg->de_length_size;
	if (info_offset >= dbg->de_debug_info_size) {
	    _dwarf_error(dbg, error, DW_DLE_ARANGE_OFFSET_BAD);
	    return(DW_DLV_ERROR);
	}

        address_size = *(Dwarf_Small *)arange_ptr;
	arange_ptr = arange_ptr + sizeof(Dwarf_Small);
	length = length - sizeof(Dwarf_Small);

	segment_size = *(Dwarf_Small *)arange_ptr;
	arange_ptr = arange_ptr + sizeof(Dwarf_Small);
	length = length - sizeof(Dwarf_Small);
	if (segment_size != 0) {
	    _dwarf_error(dbg, error, DW_DLE_SEGMENT_SIZE_BAD);
	    return(DW_DLV_ERROR);
	}

	    /* Round arange_ptr offset to next multiple of address_size. */
        remainder = (Dwarf_Unsigned)(arange_ptr-header_ptr) % 
	    (2 * address_size);
	if (remainder != 0) {
	    arange_ptr = arange_ptr + (2*address_size) - remainder;
	    length = length - ((2*address_size) - remainder);
	}

	do {
	    READ_UNALIGNED(range_address, arange_ptr, dbg->de_length_size);
	    arange_ptr += dbg->de_length_size;
            length = length - dbg->de_length_size;

	    READ_UNALIGNED(range_length, arange_ptr, dbg->de_length_size);
	    arange_ptr += dbg->de_length_size;
            length = length - dbg->de_length_size;

	    if (range_address != 0 || range_length != 0) {

	        arange = (Dwarf_Arange)
		    _dwarf_get_alloc(dbg, DW_DLA_ARANGE, 1);
	        if (arange == NULL) {
		    _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL); 
		    return(DW_DLV_ERROR);
	        }

		arange->ar_address = range_address;
		arange->ar_length = range_length;
		arange->ar_info_offset = info_offset;
		arange->ar_dbg = dbg;
		arange_count++;

		curr_chain = (Dwarf_Chain)
		    _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
		if (curr_chain == NULL) {
		    _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
		    return(DW_DLV_ERROR);
		}

		curr_chain->ch_item = arange;
		if (head_chain == NULL) 
		    head_chain = prev_chain = curr_chain;
		else {
		    prev_chain->ch_next = curr_chain;
		    prev_chain = curr_chain;
		}
	    }
	} while (range_address != 0 || range_length != 0);

	if (length != 0) {
	    _dwarf_error(dbg, error, DW_DLE_ARANGE_LENGTH_BAD);
	    return(DW_DLV_ERROR);
	}

    } while (arange_ptr < dbg->de_debug_aranges + dbg->de_debug_aranges_size);

    if (arange_ptr != dbg->de_debug_aranges + dbg->de_debug_aranges_size) {
	_dwarf_error(dbg, error, DW_DLE_ARANGE_DECODE_ERROR);
	return(DW_DLV_ERROR);
    }

    arange_block = (Dwarf_Arange *)
	_dwarf_get_alloc(dbg, DW_DLA_LIST, arange_count);
    if (arange_block == NULL) {
	_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
	return(DW_DLV_ERROR);
    }

    curr_chain = head_chain;
    for (i = 0; i < arange_count; i++) {
	*(arange_block + i) = curr_chain->ch_item;
	prev_chain = curr_chain;
	curr_chain = curr_chain->ch_next;
	dwarf_dealloc(dbg, prev_chain, DW_DLA_CHAIN);
    }

    *aranges = arange_block;
    *returned_count = (arange_count);
    return DW_DLV_OK;
}

/*
    This function returns DW_DLV_OK if it succeeds
    and DW_DLV_ERR or DW_DLV_OK otherwise.
    count is set to the number of addresses in the
    .debug_aranges section. 
    For each address, the corresponding element in
    an array is set to the address itself(aranges) and
    the section offset (offsets).
    Must be identical in most aspects to
	dwarf_get_aranges!
*/
int
_dwarf_get_aranges_addr_offsets(
    Dwarf_Debug		dbg,
    Dwarf_Addr	        **addrs,
    Dwarf_Off           **offsets,
    Dwarf_Signed        *count,
    Dwarf_Error		*error
)
{
	/* Sweeps the .debug_aranges section. */
    Dwarf_Small		*arange_ptr;
    Dwarf_Small		*arange_start_ptr;

	/* 
	    Start of arange header.  Used for rounding offset of
	    arange_ptr to twice the tuple size.  Libdwarf requirement.
	*/
    Dwarf_Small		*header_ptr;

	/* Length of current set of aranges. */
    Dwarf_Unsigned	length;

	/* Version of .debug_aranges header. */
    Dwarf_Half		version;

        /* Offset of current set of aranges into .debug_info. */
    Dwarf_Off	        info_offset;

	/* Size in bytes of addresses in target. */
    Dwarf_Small		address_size;

	/* Size in bytes of segment offsets in target. */
    Dwarf_Small		segment_size;

    Dwarf_Small		remainder;

	/* Count of total number of aranges. */
    Dwarf_Unsigned	arange_count = 0;

	/* Start address of arange. */
    Dwarf_Addr		range_address;

	/* Length of arange. */
    Dwarf_Unsigned	range_length;

    Dwarf_Arange	arange;

    Dwarf_Unsigned	i;

	/* Used to chain Dwarf_Aranges structs. */
    Dwarf_Chain		curr_chain, prev_chain, head_chain = NULL;

    Dwarf_Addr * arange_addrs;
    Dwarf_Off  * arange_offsets;

    /* ***** BEGIN CODE ***** */

    if (error != NULL) *error = NULL;

    if (dbg == NULL)
	{_dwarf_error(NULL, error, DW_DLE_DBG_NULL); return(DW_DLV_ERROR);}

    if (dbg->de_debug_aranges == NULL) {
	return(DW_DLV_NO_ENTRY);
    }

    arange_ptr = dbg->de_debug_aranges;
    do {
	header_ptr = arange_ptr;

	    /* Length of aranges for this compilation-unit. */
	READ_UNALIGNED(length, arange_ptr, dbg->de_length_size);
	arange_ptr += dbg->de_length_size;

	READ_UNALIGNED(version, arange_ptr, sizeof(Dwarf_Half));
	arange_ptr += sizeof(Dwarf_Half);
	length = length - sizeof(Dwarf_Half);
	if (version != CURRENT_VERSION_STAMP) {
	    _dwarf_error(dbg, error, DW_DLE_VERSION_STAMP_ERROR);
	    return(DW_DLV_ERROR);
	}

	READ_UNALIGNED(info_offset, arange_ptr, dbg->de_length_size);
	arange_ptr += dbg->de_length_size;
	length = length - dbg->de_length_size;
	if (info_offset >= dbg->de_debug_info_size) {
	    _dwarf_error(dbg, error, DW_DLE_ARANGE_OFFSET_BAD);
	    return(DW_DLV_ERROR);
	}

        address_size = *(Dwarf_Small *)arange_ptr;
	arange_ptr = arange_ptr + sizeof(Dwarf_Small);
	length = length - sizeof(Dwarf_Small);

	segment_size = *(Dwarf_Small *)arange_ptr;
	arange_ptr = arange_ptr + sizeof(Dwarf_Small);
	length = length - sizeof(Dwarf_Small);
	if (segment_size != 0) {
	    _dwarf_error(dbg, error, DW_DLE_SEGMENT_SIZE_BAD);
	    return(DW_DLV_ERROR);
	}

	    /* Round arange_ptr offset to next multiple of address_size. */
        remainder = (Dwarf_Unsigned)(arange_ptr-header_ptr) % 
	    (2 * address_size);
	if (remainder != 0) {
	    arange_ptr = arange_ptr + (2*address_size) - remainder;
	    length = length - ((2*address_size) - remainder);
	}

	do {
	    arange_start_ptr = arange_ptr;
	    READ_UNALIGNED(range_address, arange_ptr, dbg->de_length_size);
	    arange_ptr += dbg->de_length_size;
            length = length - dbg->de_length_size;

	    READ_UNALIGNED(range_length, arange_ptr, dbg->de_length_size);
	    arange_ptr += dbg->de_length_size;
            length = length - dbg->de_length_size;

	    if (range_address != 0 || range_length != 0) {

	        arange = (Dwarf_Arange)
		    _dwarf_get_alloc(dbg, DW_DLA_ARANGE, 1);
	        if (arange == NULL) {
		    _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL); 
		    return(DW_DLV_ERROR);
	        }

		arange->ar_address = range_address;
		arange->ar_length = range_length;
		arange->ar_info_offset = arange_start_ptr - dbg->de_debug_aranges;
		arange->ar_dbg = dbg;
		arange_count++;

		curr_chain = (Dwarf_Chain)
		    _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
		if (curr_chain == NULL) {
		    _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
		    return(DW_DLV_ERROR);
		}

		curr_chain->ch_item = arange;
		if (head_chain == NULL) 
		    head_chain = prev_chain = curr_chain;
		else {
		    prev_chain->ch_next = curr_chain;
		    prev_chain = curr_chain;
		}
	    }
	} while (range_address != 0 || range_length != 0);

	if (length != 0) {
	    _dwarf_error(dbg, error, DW_DLE_ARANGE_LENGTH_BAD);
	    return(DW_DLV_ERROR);
	}

    } while (arange_ptr < dbg->de_debug_aranges + dbg->de_debug_aranges_size);

    if (arange_ptr != dbg->de_debug_aranges + dbg->de_debug_aranges_size) {
	_dwarf_error(dbg, error, DW_DLE_ARANGE_DECODE_ERROR);
	return(DW_DLV_ERROR);
    }

    arange_addrs = (Dwarf_Addr *)
	_dwarf_get_alloc(dbg, DW_DLA_ADDR, arange_count);
    if (arange_addrs == NULL) {
	_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
	return(DW_DLV_ERROR);
    }
    arange_offsets = (Dwarf_Off *)
	_dwarf_get_alloc(dbg, DW_DLA_ADDR, arange_count);
    if (arange_offsets == NULL) {
	_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
	return(DW_DLV_ERROR);
    }

    curr_chain = head_chain;
    for (i = 0; i < arange_count; i++) {
	Dwarf_Arange   ar = curr_chain->ch_item;
	arange_addrs[i] = ar->ar_address;
	arange_offsets[i] = ar->ar_info_offset;
	prev_chain = curr_chain;
	curr_chain = curr_chain->ch_next;
	dwarf_dealloc(dbg, ar, DW_DLA_ARANGE);
	dwarf_dealloc(dbg, prev_chain, DW_DLA_CHAIN);
    }
    *count = arange_count;
    *offsets = arange_offsets;
    *addrs = arange_addrs;
    return(DW_DLV_OK);
}


/*
    This function takes a pointer to a block
    of Dwarf_Arange's, and a count of the
    length of the block.  It checks if the
    given address is within the range of an
    address range in the block.  If yes, it
    returns the appropriate Dwarf_Arange.
    Otherwise, it returns DW_DLV_ERROR.
*/
int
dwarf_get_arange (
    Dwarf_Arange	*aranges,
    Dwarf_Unsigned	arange_count,
    Dwarf_Addr	        address,
    Dwarf_Arange       *returned_arange,
    Dwarf_Error		*error
)
{
    Dwarf_Arange	curr_arange;
    Dwarf_Unsigned	i;

    if (aranges == NULL) {
	_dwarf_error(NULL, error, DW_DLE_ARANGES_NULL);
	return(DW_DLV_ERROR);
    }

    for (i = 0; i < arange_count; i++) {
	curr_arange = *(aranges + i);
	if (address >= curr_arange->ar_address &&
	    address < curr_arange->ar_address + curr_arange->ar_length) {
	    *returned_arange = curr_arange;
    	    return(DW_DLV_OK);
        }
    }

    return(DW_DLV_NO_ENTRY);
}


/*
    This function takes an Dwarf_Arange,
    and returns the offset of the first
    die in the compilation-unit that the
    arange belongs to.  Returns DW_DLV_ERROR
    on error.
*/
int
dwarf_get_cu_die_offset (
    Dwarf_Arange	arange,
    Dwarf_Off      *    returned_offset,
    Dwarf_Error		*error
)
{
    if (arange == NULL) {
	_dwarf_error(NULL, error, DW_DLE_ARANGE_NULL); 
	return(DW_DLV_ERROR);
    }

    *returned_offset =
	(arange->ar_info_offset + arange->ar_dbg->de_length_size + 
	CU_VERSION_STAMP_SIZE + arange->ar_dbg->de_length_size + 
	CU_ADDRESS_SIZE_SIZE);
    return DW_DLV_OK;
}


/*
    This function takes a Dwarf_Arange, and returns
    true if it is not NULL.  It also stores the start
    address of the range in *start, the length of the
    range in *length, and the offset of the first die
    in the compilation-unit in *cu_die_offset.  It
    returns false on error.
*/
int
dwarf_get_arange_info (
    Dwarf_Arange	arange,
    Dwarf_Addr		*start,
    Dwarf_Unsigned	*length,
    Dwarf_Off		*cu_die_offset,
    Dwarf_Error		*error
)
{
    if (arange == NULL) {
	_dwarf_error(NULL, error, DW_DLE_ARANGE_NULL); 
	return(DW_DLV_ERROR);
    }

    if (start != NULL) *start = arange->ar_address;
    if (length != NULL) *length = arange->ar_length;
    if (cu_die_offset != NULL)
        *cu_die_offset = arange->ar_info_offset + 
	    arange->ar_dbg->de_length_size + CU_VERSION_STAMP_SIZE + 
	    arange->ar_dbg->de_length_size + CU_ADDRESS_SIZE_SIZE;

    return(DW_DLV_OK);
}