dwarf_query.c 12.4 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 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
#include <stdio.h>
#include "dwarf_incl.h"
#include "dwarf_die_deliv.h"

int dwarf_get_address_size(Dwarf_Debug dbg,
        Dwarf_Half *ret_addr_size,
        Dwarf_Error *error)
{
	Dwarf_Half address_size;
	if(dbg == 0) {
	    _dwarf_error(NULL,error,DW_DLE_DBG_NULL);
            return(DW_DLV_ERROR);
	}
	/* length size same as address size */
	address_size = dbg->de_length_size;
	*ret_addr_size = address_size;
	return DW_DLV_OK;
}

int
dwarf_dieoffset (
    Dwarf_Die       die,
    Dwarf_Off      *ret_offset,
    Dwarf_Error     *error
)
{
    CHECK_DIE(die, DW_DLV_ERROR)

    *ret_offset = (die->di_debug_info_ptr - 
	die->di_cu_context->cc_dbg->de_debug_info);
    return DW_DLV_OK;
}


/*
    This function returns the offset of
    the die relative to the start of its
    compilation-unit rather than .debug_info.
    Returns DW_DLV_ERROR on error.
*/
int
dwarf_die_CU_offset (
    Dwarf_Die	    die,
    Dwarf_Off       *cu_off,
    Dwarf_Error	    *error
)
{
    Dwarf_CU_Context	cu_context;

    CHECK_DIE(die, DW_DLV_ERROR)
    cu_context = die->di_cu_context;

    *cu_off = (die->di_debug_info_ptr - cu_context->cc_dbg->de_debug_info -
	cu_context->cc_debug_info_offset);
    return DW_DLV_OK;
}


int
dwarf_tag (
    Dwarf_Die       die,
    Dwarf_Half      *tag,
    Dwarf_Error     *error
)
{
    CHECK_DIE(die, DW_DLV_ERROR)

  
    *tag =(die->di_abbrev_list->ab_tag);
    return DW_DLV_OK;
}


int
dwarf_attrlist (
    Dwarf_Die         die,
    Dwarf_Attribute   **attrbuf,
    Dwarf_Signed      *attrcnt,
    Dwarf_Error       *error
)
{
    Dwarf_Word              attr_count = 0;
    Dwarf_Word	            i;
    Dwarf_Half              attr;
    Dwarf_Half		    attr_form;
    Dwarf_Byte_Ptr          abbrev_ptr;
    Dwarf_Abbrev_List	    abbrev_list;
    Dwarf_Attribute	    new_attr;
    Dwarf_Attribute	    head_attr = NULL, curr_attr;
    Dwarf_Attribute	    *attr_ptr;
    Dwarf_Debug		    dbg;
    Dwarf_Byte_Ptr	    info_ptr;

    CHECK_DIE(die, DW_DLV_ERROR)
    dbg = die->di_cu_context->cc_dbg;

    abbrev_list = _dwarf_get_abbrev_for_code(die->di_cu_context, 
	die->di_abbrev_list->ab_code);
    if (abbrev_list == NULL) {
	_dwarf_error(dbg, error, DW_DLE_DIE_ABBREV_BAD); 
	return(DW_DLV_ERROR);
    }
    abbrev_ptr = abbrev_list->ab_abbrev_ptr;

    info_ptr = die->di_debug_info_ptr;
    SKIP_LEB128_WORD(info_ptr)

    do {
        DECODE_LEB128_UWORD(abbrev_ptr, attr)
        DECODE_LEB128_UWORD(abbrev_ptr, attr_form)

	if (attr != 0) {
            new_attr = (Dwarf_Attribute)_dwarf_get_alloc(dbg, DW_DLA_ATTR, 1);
	    if (new_attr == NULL) {
	        _dwarf_error(dbg,error,DW_DLE_ALLOC_FAIL);
	        return(DW_DLV_ERROR);
	    }

	    new_attr->ar_attribute = attr;
	    new_attr->ar_attribute_form = attr_form;
	    new_attr->ar_cu_context = die->di_cu_context;
	    new_attr->ar_debug_info_ptr = info_ptr;

	    info_ptr += _dwarf_get_size_of_val(dbg, attr_form, info_ptr);

	    if (head_attr == NULL)
	        head_attr = curr_attr = new_attr;
	    else {
	        curr_attr->ar_next = new_attr;
	        curr_attr = new_attr;
	    }
            attr_count++;
	}
    } while (attr != 0 || attr_form != 0);

    if (attr_count == 0) {
	*attrbuf = NULL;
	*attrcnt = 0;
	return(DW_DLV_NO_ENTRY);
    }

    attr_ptr = (Dwarf_Attribute *)
	_dwarf_get_alloc(dbg, DW_DLA_LIST, attr_count);
    if (attr_ptr == NULL) {
	_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
	return(DW_DLV_ERROR);
    }

    curr_attr = head_attr;
    for (i = 0; i < attr_count; i++) {
	*(attr_ptr + i) = curr_attr;
	curr_attr = curr_attr->ar_next;
    }

    *attrbuf = attr_ptr;
    *attrcnt = attr_count;
    return(DW_DLV_OK);
}


/*
    This function takes a die, and an attr, and returns
    a pointer to the start of the value of that attr in
    the given die in the .debug_info section.  The form
    is returned in *attr_form.

    Returns NULL on error, or if attr is not found.
    However, *attr_form is 0 on error, and positive 
    otherwise.
*/
static Dwarf_Byte_Ptr
_dwarf_get_value_ptr (
    Dwarf_Die		die,
    Dwarf_Half		attr,
    Dwarf_Half		*attr_form
)
{
    Dwarf_Byte_Ptr          abbrev_ptr;
    Dwarf_Abbrev_List	    abbrev_list;
    Dwarf_Half	            curr_attr;
    Dwarf_Half	            curr_attr_form;
    Dwarf_Byte_Ptr	    info_ptr;

    abbrev_list = _dwarf_get_abbrev_for_code(die->di_cu_context, 
	die->di_abbrev_list->ab_code);
    if (abbrev_list == NULL) {
	*attr_form = 0;
	return(NULL);
    }
    abbrev_ptr = abbrev_list->ab_abbrev_ptr;

    info_ptr = die->di_debug_info_ptr;
    SKIP_LEB128_WORD(info_ptr)

    do {
        DECODE_LEB128_UWORD(abbrev_ptr, curr_attr)
	DECODE_LEB128_UWORD(abbrev_ptr, curr_attr_form)

        if (curr_attr == attr) {
	    *attr_form = curr_attr_form;
	    return(info_ptr);
	}

        info_ptr += _dwarf_get_size_of_val(die->di_cu_context->cc_dbg, 
            curr_attr_form, info_ptr);
    } while (curr_attr != 0 || curr_attr_form != 0);

    *attr_form = 1;
    return(NULL);
}


int
dwarf_diename (
    Dwarf_Die       die,
    char     **     ret_name,
    Dwarf_Error     *error
)
{
    Dwarf_Half	            attr_form;
    Dwarf_Debug		    dbg;
    Dwarf_Byte_Ptr	    info_ptr;
    Dwarf_Unsigned          string_offset;

    CHECK_DIE(die, DW_DLV_ERROR)

    info_ptr = _dwarf_get_value_ptr(die, DW_AT_name, &attr_form);
    if (info_ptr == NULL) {
	if (attr_form == 0)  {
	    _dwarf_error(die->di_cu_context->cc_dbg, error, DW_DLE_DIE_BAD);
	    return(DW_DLV_ERROR);
	}
	return DW_DLV_NO_ENTRY;
    }

    if (attr_form == DW_FORM_string){
	 *ret_name = (char *)(info_ptr);
	 return DW_DLV_OK;
    }

    dbg = die->di_cu_context->cc_dbg;
    if (attr_form != DW_FORM_strp) {
	_dwarf_error(dbg, error, DW_DLE_ATTR_FORM_BAD);
	return(DW_DLV_ERROR);
    }

    READ_UNALIGNED(string_offset, info_ptr, dbg->de_length_size);

    if (dbg->de_debug_str == NULL) {
	_dwarf_error(dbg, error, DW_DLE_DEBUG_STR_NULL); 
	return(DW_DLV_ERROR);
    }

    if (string_offset >= dbg->de_debug_str_size) {
	_dwarf_error(dbg, error, DW_DLE_STRING_OFFSET_BAD); 
	return(DW_DLV_ERROR);
    }

    *ret_name = (char *)(dbg->de_debug_str + string_offset);
    return DW_DLV_OK;
}


int
dwarf_hasattr (
    Dwarf_Die       die,
    Dwarf_Half      attr,
    Dwarf_Bool      *return_bool,
    Dwarf_Error     *error
)
{
    Dwarf_Half	        attr_form;

    CHECK_DIE(die, DW_DLV_ERROR)

    if (_dwarf_get_value_ptr(die, attr, &attr_form) == NULL) {
	if (attr_form == 0)  {
	    _dwarf_error(die->di_cu_context->cc_dbg, error, DW_DLE_DIE_BAD);
	    return(DW_DLV_ERROR);
	}
        *return_bool = false;
	return DW_DLV_OK;
    }

    *return_bool = (true);
    return DW_DLV_OK;
}


int
dwarf_attr (
    Dwarf_Die       die,
    Dwarf_Half      attr,
    Dwarf_Attribute *    ret_attr,
    Dwarf_Error     *error
)
{
    Dwarf_Half              attr_form;
    Dwarf_Attribute         attrib;
    Dwarf_Byte_Ptr	    info_ptr;
    Dwarf_Debug		    dbg;

    CHECK_DIE(die, DW_DLV_ERROR)
    dbg = die->di_cu_context->cc_dbg;

    info_ptr = _dwarf_get_value_ptr(die, attr, &attr_form);
    if (info_ptr == NULL) {
	if (attr_form == 0) {
		_dwarf_error(dbg, error, DW_DLE_DIE_BAD); 
        	return(DW_DLV_ERROR);
	}
	return DW_DLV_NO_ENTRY;
    }

    attrib = (Dwarf_Attribute)_dwarf_get_alloc(dbg, DW_DLA_ATTR, 1);
    if (attrib == NULL) {
	_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
	return(DW_DLV_ERROR);
    }

    attrib->ar_attribute = attr;
    attrib->ar_attribute_form = attr_form;
    attrib->ar_cu_context = die->di_cu_context;
    attrib->ar_debug_info_ptr = info_ptr;
    *ret_attr = (attrib);
    return DW_DLV_OK;
}


int
dwarf_lowpc (
    Dwarf_Die		die,
    Dwarf_Addr    *return_addr,
    Dwarf_Error		*error
)
{
    Dwarf_Addr		ret_addr;
    Dwarf_Byte_Ptr	info_ptr;
    Dwarf_Half		attr_form;

    CHECK_DIE(die, DW_DLV_ERROR)

    info_ptr = _dwarf_get_value_ptr(die, DW_AT_low_pc, &attr_form);
    if ((info_ptr == NULL && attr_form == 0) ||
	(info_ptr != NULL && attr_form != DW_FORM_addr)) {
	_dwarf_error(die->di_cu_context->cc_dbg, error, DW_DLE_DIE_BAD);
	return(DW_DLV_ERROR);
    }
    if (info_ptr == NULL) {
	return(DW_DLV_NO_ENTRY);
    }

    READ_UNALIGNED(ret_addr, info_ptr, \
	die->di_cu_context->cc_dbg->de_length_size);

    *return_addr = ret_addr;
    return(DW_DLV_OK);
}


int
dwarf_highpc (
    Dwarf_Die		die,
    Dwarf_Addr    *return_addr,
    Dwarf_Error		*error
)
{
    Dwarf_Addr		ret_addr;
    Dwarf_Byte_Ptr	info_ptr;
    Dwarf_Half		attr_form;

    CHECK_DIE(die, DW_DLV_ERROR)

    info_ptr = _dwarf_get_value_ptr(die, DW_AT_high_pc, &attr_form);
    if ((info_ptr == NULL && attr_form == 0) ||
	(info_ptr != NULL && attr_form != DW_FORM_addr)) {
	_dwarf_error(die->di_cu_context->cc_dbg, error, DW_DLE_DIE_BAD);
	return(DW_DLV_ERROR);
    }
    if (info_ptr == NULL) {
	return(DW_DLV_NO_ENTRY);
    }

    READ_UNALIGNED(ret_addr, info_ptr, \
	die->di_cu_context->cc_dbg->de_length_size);

    *return_addr = ret_addr;
    return(DW_DLV_OK);
}


/*
    Takes a die, an attribute attr, and checks if attr 
    occurs in die.  Attr is required to be an attribute
    whose form is in the "constant" class.  If attr occurs 
    in die, the value is returned.  
  Returns DW_DLV_OK, DW_DLV_ERROR, or DW_DLV_NO_ENTRY as
    appropriate. Sets the value thru the pointer return_val.
    This function is meant to do all the 
    processing for dwarf_bytesize, dwarf_bitsize, dwarf_bitoffset, 
    and dwarf_srclang.
*/
static int
_dwarf_die_attr_unsigned_constant (
    Dwarf_Die		die,
    Dwarf_Half		attr,
    Dwarf_Unsigned      *return_val,
    Dwarf_Error		*error
)
{
    Dwarf_Byte_Ptr	info_ptr;
    Dwarf_Half		attr_form;
    Dwarf_Unsigned	ret_value;

    CHECK_DIE(die, DW_DLV_ERROR)

    info_ptr = _dwarf_get_value_ptr(die, attr, &attr_form);
    if (info_ptr != NULL) {
        switch (attr_form) {

	    case DW_FORM_data1 :
	        *return_val =  (*(Dwarf_Small *)info_ptr);
	        return(DW_DLV_OK);

	    case DW_FORM_data2 : 
	        READ_UNALIGNED(ret_value, info_ptr, sizeof(Dwarf_Shalf));
		*return_val = ret_value;
	        return(DW_DLV_OK);

	    case DW_FORM_data4 : 
	        READ_UNALIGNED(ret_value, info_ptr, sizeof(Dwarf_Sword));
		*return_val = ret_value;
	        return(DW_DLV_OK);

	    case DW_FORM_data8 :
	        READ_UNALIGNED(ret_value, info_ptr, sizeof(Dwarf_Unsigned));
		*return_val = ret_value;
	        return(DW_DLV_OK);

	    case DW_FORM_udata :
	        *return_val = (_dwarf_decode_u_leb128(info_ptr, NULL));
	        return(DW_DLV_OK);

	    default :
		_dwarf_error(die->di_cu_context->cc_dbg, error, DW_DLE_DIE_BAD);
		return(DW_DLV_ERROR);
        }
    }
    if (attr_form == 0) {
	    _dwarf_error(die->di_cu_context->cc_dbg, error, DW_DLE_DIE_BAD);
	     return(DW_DLV_ERROR);
    }
    return DW_DLV_NO_ENTRY;
}


int
dwarf_bytesize (
    Dwarf_Die		die,
    Dwarf_Unsigned	* ret_size,
    Dwarf_Error		*error
)
{
   Dwarf_Unsigned luns;
   int res=_dwarf_die_attr_unsigned_constant(die, DW_AT_byte_size,&luns,error);
   *ret_size = luns;
   return res;
}


int
dwarf_bitsize (
    Dwarf_Die		die,
    Dwarf_Unsigned     *ret_size,
    Dwarf_Error		*error
)
{
   Dwarf_Unsigned luns;
   int res;
    res = _dwarf_die_attr_unsigned_constant(die, DW_AT_bit_size,&luns, error);
    *ret_size = luns;
   return res;
}


int
dwarf_bitoffset (
    Dwarf_Die		die,
    Dwarf_Unsigned     *ret_size,
    Dwarf_Error		*error
)
{
   Dwarf_Unsigned luns;
   int res;
    res= _dwarf_die_attr_unsigned_constant(die, DW_AT_bit_offset,&luns, error);
    *ret_size = luns;
   return res;
}


/* Refer section 3.1, page 21 in Dwarf Definition. */
int
dwarf_srclang (
    Dwarf_Die		die,
    Dwarf_Unsigned     *ret_size,
    Dwarf_Error		*error
)
{
   Dwarf_Unsigned luns;
   int res;
    res = _dwarf_die_attr_unsigned_constant(die, DW_AT_language,&luns, error);
    *ret_size = luns;
    return res;
}


/* Refer section 5.4, page 37 in Dwarf Definition. */
int
dwarf_arrayorder (
    Dwarf_Die		die,
    Dwarf_Unsigned     *ret_size,
    Dwarf_Error		*error
)
{
   Dwarf_Unsigned luns;
	int res;
    res =_dwarf_die_attr_unsigned_constant(die, DW_AT_ordering,&luns, error);
    *ret_size = luns;
    return res;
}

/*
	Return DW_DLV_OK if ok
	DW_DLV_ERROR if failure.

	If the die and the attr are not related the result is
	meaningless.
*/
int
dwarf_attr_offset (
    Dwarf_Die       die,
    Dwarf_Attribute attr,
    Dwarf_Off  *    offset, /* return offset thru this ptr */
    Dwarf_Error     *error
)
{
    Dwarf_Off attroff;
    CHECK_DIE(die, DW_DLV_ERROR)

    attroff = (attr->ar_debug_info_ptr - 
		die->di_cu_context->cc_dbg->de_debug_info);
    *offset = attroff;
    return DW_DLV_OK;
}