1 /*- 2 * Copyright (c) 2007 John Birrell (jb@freebsd.org) 3 * Copyright (c) 2014 Kai Wang 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include "_libdwarf.h" 29 30 ELFTC_VCSID("$Id: libdwarf_loc.c 3070 2014-06-23 03:08:33Z kaiwang27 $"); 31 32 /* 33 * Given an array of bytes of length 'len' representing a 34 * DWARF expression, compute the number of operations based 35 * on there being one byte describing the operation and 36 * zero or more bytes of operands as defined in the standard 37 * for each operation type. Also, if lbuf is non-null, store 38 * the opcode and oprand in it. 39 */ 40 static int 41 _dwarf_loc_fill_loc(Dwarf_Debug dbg, Dwarf_Locdesc *lbuf, uint8_t pointer_size, 42 uint8_t offset_size, uint8_t version, uint8_t *p, int len) 43 { 44 int count; 45 uint64_t operand1; 46 uint64_t operand2; 47 uint8_t *ps, *pe, s; 48 49 count = 0; 50 ps = p; 51 pe = p + len; 52 53 /* 54 * Process each byte. If an error occurs, then the 55 * count will be set to -1. 56 */ 57 while (p < pe) { 58 59 operand1 = 0; 60 operand2 = 0; 61 62 if (lbuf != NULL) { 63 lbuf->ld_s[count].lr_atom = *p; 64 lbuf->ld_s[count].lr_offset = p - ps; 65 } 66 67 switch (*p++) { 68 /* Operations with no operands. */ 69 case DW_OP_deref: 70 case DW_OP_reg0: 71 case DW_OP_reg1: 72 case DW_OP_reg2: 73 case DW_OP_reg3: 74 case DW_OP_reg4: 75 case DW_OP_reg5: 76 case DW_OP_reg6: 77 case DW_OP_reg7: 78 case DW_OP_reg8: 79 case DW_OP_reg9: 80 case DW_OP_reg10: 81 case DW_OP_reg11: 82 case DW_OP_reg12: 83 case DW_OP_reg13: 84 case DW_OP_reg14: 85 case DW_OP_reg15: 86 case DW_OP_reg16: 87 case DW_OP_reg17: 88 case DW_OP_reg18: 89 case DW_OP_reg19: 90 case DW_OP_reg20: 91 case DW_OP_reg21: 92 case DW_OP_reg22: 93 case DW_OP_reg23: 94 case DW_OP_reg24: 95 case DW_OP_reg25: 96 case DW_OP_reg26: 97 case DW_OP_reg27: 98 case DW_OP_reg28: 99 case DW_OP_reg29: 100 case DW_OP_reg30: 101 case DW_OP_reg31: 102 103 case DW_OP_lit0: 104 case DW_OP_lit1: 105 case DW_OP_lit2: 106 case DW_OP_lit3: 107 case DW_OP_lit4: 108 case DW_OP_lit5: 109 case DW_OP_lit6: 110 case DW_OP_lit7: 111 case DW_OP_lit8: 112 case DW_OP_lit9: 113 case DW_OP_lit10: 114 case DW_OP_lit11: 115 case DW_OP_lit12: 116 case DW_OP_lit13: 117 case DW_OP_lit14: 118 case DW_OP_lit15: 119 case DW_OP_lit16: 120 case DW_OP_lit17: 121 case DW_OP_lit18: 122 case DW_OP_lit19: 123 case DW_OP_lit20: 124 case DW_OP_lit21: 125 case DW_OP_lit22: 126 case DW_OP_lit23: 127 case DW_OP_lit24: 128 case DW_OP_lit25: 129 case DW_OP_lit26: 130 case DW_OP_lit27: 131 case DW_OP_lit28: 132 case DW_OP_lit29: 133 case DW_OP_lit30: 134 case DW_OP_lit31: 135 136 case DW_OP_dup: 137 case DW_OP_drop: 138 139 case DW_OP_over: 140 141 case DW_OP_swap: 142 case DW_OP_rot: 143 case DW_OP_xderef: 144 145 case DW_OP_abs: 146 case DW_OP_and: 147 case DW_OP_div: 148 case DW_OP_minus: 149 case DW_OP_mod: 150 case DW_OP_mul: 151 case DW_OP_neg: 152 case DW_OP_not: 153 case DW_OP_or: 154 case DW_OP_plus: 155 156 case DW_OP_shl: 157 case DW_OP_shr: 158 case DW_OP_shra: 159 case DW_OP_xor: 160 161 case DW_OP_eq: 162 case DW_OP_ge: 163 case DW_OP_gt: 164 case DW_OP_le: 165 case DW_OP_lt: 166 case DW_OP_ne: 167 168 case DW_OP_nop: 169 case DW_OP_push_object_address: 170 case DW_OP_form_tls_address: 171 case DW_OP_call_frame_cfa: 172 case DW_OP_stack_value: 173 case DW_OP_GNU_push_tls_address: 174 case DW_OP_GNU_uninit: 175 break; 176 177 /* Operations with 1-byte operands. */ 178 case DW_OP_const1u: 179 case DW_OP_pick: 180 case DW_OP_deref_size: 181 case DW_OP_xderef_size: 182 operand1 = *p++; 183 break; 184 185 case DW_OP_const1s: 186 operand1 = (int8_t) *p++; 187 break; 188 189 /* Operations with 2-byte operands. */ 190 case DW_OP_call2: 191 case DW_OP_const2u: 192 case DW_OP_bra: 193 case DW_OP_skip: 194 operand1 = dbg->decode(&p, 2); 195 break; 196 197 case DW_OP_const2s: 198 operand1 = (int16_t) dbg->decode(&p, 2); 199 break; 200 201 /* Operations with 4-byte operands. */ 202 case DW_OP_call4: 203 case DW_OP_const4u: 204 case DW_OP_GNU_parameter_ref: 205 operand1 = dbg->decode(&p, 4); 206 break; 207 208 case DW_OP_const4s: 209 operand1 = (int32_t) dbg->decode(&p, 4); 210 break; 211 212 /* Operations with 8-byte operands. */ 213 case DW_OP_const8u: 214 case DW_OP_const8s: 215 operand1 = dbg->decode(&p, 8); 216 break; 217 218 /* Operations with an unsigned LEB128 operand. */ 219 case DW_OP_constu: 220 case DW_OP_plus_uconst: 221 case DW_OP_regx: 222 case DW_OP_piece: 223 case DW_OP_GNU_deref_type: 224 case DW_OP_GNU_convert: 225 case DW_OP_GNU_reinterpret: 226 operand1 = _dwarf_decode_uleb128(&p); 227 break; 228 229 /* Operations with a signed LEB128 operand. */ 230 case DW_OP_consts: 231 case DW_OP_breg0: 232 case DW_OP_breg1: 233 case DW_OP_breg2: 234 case DW_OP_breg3: 235 case DW_OP_breg4: 236 case DW_OP_breg5: 237 case DW_OP_breg6: 238 case DW_OP_breg7: 239 case DW_OP_breg8: 240 case DW_OP_breg9: 241 case DW_OP_breg10: 242 case DW_OP_breg11: 243 case DW_OP_breg12: 244 case DW_OP_breg13: 245 case DW_OP_breg14: 246 case DW_OP_breg15: 247 case DW_OP_breg16: 248 case DW_OP_breg17: 249 case DW_OP_breg18: 250 case DW_OP_breg19: 251 case DW_OP_breg20: 252 case DW_OP_breg21: 253 case DW_OP_breg22: 254 case DW_OP_breg23: 255 case DW_OP_breg24: 256 case DW_OP_breg25: 257 case DW_OP_breg26: 258 case DW_OP_breg27: 259 case DW_OP_breg28: 260 case DW_OP_breg29: 261 case DW_OP_breg30: 262 case DW_OP_breg31: 263 case DW_OP_fbreg: 264 operand1 = _dwarf_decode_sleb128(&p); 265 break; 266 267 /* 268 * Oeration with two unsigned LEB128 operands. 269 */ 270 case DW_OP_bit_piece: 271 case DW_OP_GNU_regval_type: 272 operand1 = _dwarf_decode_uleb128(&p); 273 operand2 = _dwarf_decode_uleb128(&p); 274 break; 275 276 /* 277 * Operations with an unsigned LEB128 operand 278 * followed by a signed LEB128 operand. 279 */ 280 case DW_OP_bregx: 281 operand1 = _dwarf_decode_uleb128(&p); 282 operand2 = _dwarf_decode_sleb128(&p); 283 break; 284 285 /* 286 * Operation with an unsigned LEB128 operand 287 * representing the size of a block, followed 288 * by the block content. 289 * 290 * Store the size of the block in the operand1 291 * and a pointer to the block in the operand2. 292 */ 293 case DW_OP_implicit_value: 294 case DW_OP_GNU_entry_value: 295 operand1 = _dwarf_decode_uleb128(&p); 296 operand2 = (Dwarf_Unsigned) (uintptr_t) p; 297 p += operand1; 298 break; 299 300 /* Target address size operand. */ 301 case DW_OP_addr: 302 case DW_OP_GNU_addr_index: 303 case DW_OP_GNU_const_index: 304 operand1 = dbg->decode(&p, pointer_size); 305 break; 306 307 /* Offset size operand. */ 308 case DW_OP_call_ref: 309 operand1 = dbg->decode(&p, offset_size); 310 break; 311 312 /* 313 * The first byte is address byte length, followed by 314 * the address value. If the length is 0, the address 315 * size is the same as target pointer size. 316 */ 317 case DW_OP_GNU_encoded_addr: 318 s = *p++; 319 if (s == 0) 320 s = pointer_size; 321 operand1 = dbg->decode(&p, s); 322 break; 323 324 /* 325 * Operand1: DIE offset (size depending on DWARF version) 326 * DWARF2: pointer size 327 * DWARF{3,4}: offset size 328 * 329 * Operand2: SLEB128 330 */ 331 case DW_OP_GNU_implicit_pointer: 332 if (version == 2) 333 operand1 = dbg->decode(&p, pointer_size); 334 else 335 operand1 = dbg->decode(&p, offset_size); 336 operand2 = _dwarf_decode_sleb128(&p); 337 break; 338 339 /* 340 * Operand1: DIE offset (ULEB128) 341 * Operand2: pointer to a block. The block's first byte 342 * is its size. 343 */ 344 case DW_OP_GNU_const_type: 345 operand1 = _dwarf_decode_uleb128(&p); 346 operand2 = (Dwarf_Unsigned) (uintptr_t) p; 347 s = *p++; 348 p += s; 349 break; 350 351 /* All other operations cause an error. */ 352 default: 353 count = -1; 354 goto done; 355 } 356 357 if (lbuf != NULL) { 358 lbuf->ld_s[count].lr_number = operand1; 359 lbuf->ld_s[count].lr_number2 = operand2; 360 } 361 362 count++; 363 } 364 365 done: 366 return (count); 367 } 368 369 int 370 _dwarf_loc_expr_add_atom(Dwarf_Debug dbg, uint8_t *out, uint8_t *end, 371 Dwarf_Small atom, Dwarf_Unsigned operand1, Dwarf_Unsigned operand2, 372 int *length, Dwarf_Error *error) 373 { 374 uint8_t buf[64]; 375 uint8_t *p, *pe; 376 uint64_t offset; 377 int len; 378 379 if (out != NULL && end != NULL) { 380 p = out; 381 pe = end; 382 } else { 383 p = out = buf; 384 pe = &buf[sizeof(buf)]; 385 } 386 387 switch (atom) { 388 /* Operations with no operands. */ 389 case DW_OP_deref: 390 case DW_OP_reg0: 391 case DW_OP_reg1: 392 case DW_OP_reg2: 393 case DW_OP_reg3: 394 case DW_OP_reg4: 395 case DW_OP_reg5: 396 case DW_OP_reg6: 397 case DW_OP_reg7: 398 case DW_OP_reg8: 399 case DW_OP_reg9: 400 case DW_OP_reg10: 401 case DW_OP_reg11: 402 case DW_OP_reg12: 403 case DW_OP_reg13: 404 case DW_OP_reg14: 405 case DW_OP_reg15: 406 case DW_OP_reg16: 407 case DW_OP_reg17: 408 case DW_OP_reg18: 409 case DW_OP_reg19: 410 case DW_OP_reg20: 411 case DW_OP_reg21: 412 case DW_OP_reg22: 413 case DW_OP_reg23: 414 case DW_OP_reg24: 415 case DW_OP_reg25: 416 case DW_OP_reg26: 417 case DW_OP_reg27: 418 case DW_OP_reg28: 419 case DW_OP_reg29: 420 case DW_OP_reg30: 421 case DW_OP_reg31: 422 423 case DW_OP_lit0: 424 case DW_OP_lit1: 425 case DW_OP_lit2: 426 case DW_OP_lit3: 427 case DW_OP_lit4: 428 case DW_OP_lit5: 429 case DW_OP_lit6: 430 case DW_OP_lit7: 431 case DW_OP_lit8: 432 case DW_OP_lit9: 433 case DW_OP_lit10: 434 case DW_OP_lit11: 435 case DW_OP_lit12: 436 case DW_OP_lit13: 437 case DW_OP_lit14: 438 case DW_OP_lit15: 439 case DW_OP_lit16: 440 case DW_OP_lit17: 441 case DW_OP_lit18: 442 case DW_OP_lit19: 443 case DW_OP_lit20: 444 case DW_OP_lit21: 445 case DW_OP_lit22: 446 case DW_OP_lit23: 447 case DW_OP_lit24: 448 case DW_OP_lit25: 449 case DW_OP_lit26: 450 case DW_OP_lit27: 451 case DW_OP_lit28: 452 case DW_OP_lit29: 453 case DW_OP_lit30: 454 case DW_OP_lit31: 455 456 case DW_OP_dup: 457 case DW_OP_drop: 458 459 case DW_OP_over: 460 461 case DW_OP_swap: 462 case DW_OP_rot: 463 case DW_OP_xderef: 464 465 case DW_OP_abs: 466 case DW_OP_and: 467 case DW_OP_div: 468 case DW_OP_minus: 469 case DW_OP_mod: 470 case DW_OP_mul: 471 case DW_OP_neg: 472 case DW_OP_not: 473 case DW_OP_or: 474 case DW_OP_plus: 475 476 case DW_OP_shl: 477 case DW_OP_shr: 478 case DW_OP_shra: 479 case DW_OP_xor: 480 481 case DW_OP_eq: 482 case DW_OP_ge: 483 case DW_OP_gt: 484 case DW_OP_le: 485 case DW_OP_lt: 486 case DW_OP_ne: 487 488 case DW_OP_nop: 489 case DW_OP_GNU_push_tls_address: 490 *p++ = atom; 491 break; 492 493 /* Operations with 1-byte operands. */ 494 case DW_OP_const1u: 495 case DW_OP_const1s: 496 case DW_OP_pick: 497 case DW_OP_deref_size: 498 case DW_OP_xderef_size: 499 *p++ = atom; 500 *p++ = (uint8_t) operand1; 501 break; 502 503 /* Operations with 2-byte operands. */ 504 case DW_OP_const2u: 505 case DW_OP_const2s: 506 case DW_OP_bra: 507 case DW_OP_skip: 508 *p++ = atom; 509 offset = 0; 510 dbg->write(p, &offset, operand1, 2); 511 p += 2; 512 break; 513 514 /* Operations with 4-byte operands. */ 515 case DW_OP_const4u: 516 case DW_OP_const4s: 517 *p++ = atom; 518 offset = 0; 519 dbg->write(p, &offset, operand1, 4); 520 p += 4; 521 break; 522 523 /* Operations with 8-byte operands. */ 524 case DW_OP_const8u: 525 case DW_OP_const8s: 526 *p++ = atom; 527 offset = 0; 528 dbg->write(p, &offset, operand1, 8); 529 p += 8; 530 break; 531 532 /* Operations with an unsigned LEB128 operand. */ 533 case DW_OP_constu: 534 case DW_OP_plus_uconst: 535 case DW_OP_regx: 536 case DW_OP_piece: 537 *p++ = atom; 538 len = _dwarf_write_uleb128(p, pe, operand1); 539 assert(len > 0); 540 p += len; 541 break; 542 543 /* Operations with a signed LEB128 operand. */ 544 case DW_OP_consts: 545 case DW_OP_breg0: 546 case DW_OP_breg1: 547 case DW_OP_breg2: 548 case DW_OP_breg3: 549 case DW_OP_breg4: 550 case DW_OP_breg5: 551 case DW_OP_breg6: 552 case DW_OP_breg7: 553 case DW_OP_breg8: 554 case DW_OP_breg9: 555 case DW_OP_breg10: 556 case DW_OP_breg11: 557 case DW_OP_breg12: 558 case DW_OP_breg13: 559 case DW_OP_breg14: 560 case DW_OP_breg15: 561 case DW_OP_breg16: 562 case DW_OP_breg17: 563 case DW_OP_breg18: 564 case DW_OP_breg19: 565 case DW_OP_breg20: 566 case DW_OP_breg21: 567 case DW_OP_breg22: 568 case DW_OP_breg23: 569 case DW_OP_breg24: 570 case DW_OP_breg25: 571 case DW_OP_breg26: 572 case DW_OP_breg27: 573 case DW_OP_breg28: 574 case DW_OP_breg29: 575 case DW_OP_breg30: 576 case DW_OP_breg31: 577 case DW_OP_fbreg: 578 *p++ = atom; 579 len = _dwarf_write_sleb128(p, pe, operand1); 580 assert(len > 0); 581 p += len; 582 break; 583 584 /* 585 * Operations with an unsigned LEB128 operand 586 * followed by a signed LEB128 operand. 587 */ 588 case DW_OP_bregx: 589 *p++ = atom; 590 len = _dwarf_write_uleb128(p, pe, operand1); 591 assert(len > 0); 592 p += len; 593 len = _dwarf_write_sleb128(p, pe, operand2); 594 assert(len > 0); 595 p += len; 596 break; 597 598 /* Target address size operand. */ 599 case DW_OP_addr: 600 *p++ = atom; 601 offset = 0; 602 dbg->write(p, &offset, operand1, dbg->dbg_pointer_size); 603 p += dbg->dbg_pointer_size; 604 break; 605 606 /* All other operations cause an error. */ 607 default: 608 DWARF_SET_ERROR(dbg, error, DW_DLE_LOC_EXPR_BAD); 609 return (DW_DLE_LOC_EXPR_BAD); 610 } 611 612 if (length) 613 *length = p - out; 614 615 return (DW_DLE_NONE); 616 } 617 618 int 619 _dwarf_loc_fill_locdesc(Dwarf_Debug dbg, Dwarf_Locdesc *llbuf, uint8_t *in, 620 uint64_t in_len, uint8_t pointer_size, uint8_t offset_size, 621 uint8_t version, Dwarf_Error *error) 622 { 623 int num; 624 625 assert(llbuf != NULL); 626 assert(in != NULL); 627 assert(in_len > 0); 628 629 /* Compute the number of locations. */ 630 if ((num = _dwarf_loc_fill_loc(dbg, NULL, pointer_size, offset_size, 631 version, in, in_len)) < 0) { 632 DWARF_SET_ERROR(dbg, error, DW_DLE_LOC_EXPR_BAD); 633 return (DW_DLE_LOC_EXPR_BAD); 634 } 635 636 llbuf->ld_cents = num; 637 if (num <= 0) 638 return (DW_DLE_NONE); 639 640 if ((llbuf->ld_s = calloc(num, sizeof(Dwarf_Loc))) == NULL) { 641 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 642 return (DW_DLE_MEMORY); 643 } 644 645 (void) _dwarf_loc_fill_loc(dbg, llbuf, pointer_size, offset_size, 646 version, in, in_len); 647 648 return (DW_DLE_NONE); 649 } 650 651 int 652 _dwarf_loc_fill_locexpr(Dwarf_Debug dbg, Dwarf_Locdesc **ret_llbuf, uint8_t *in, 653 uint64_t in_len, uint8_t pointer_size, uint8_t offset_size, 654 uint8_t version, Dwarf_Error *error) 655 { 656 Dwarf_Locdesc *llbuf; 657 int ret; 658 659 if ((llbuf = malloc(sizeof(Dwarf_Locdesc))) == NULL) { 660 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 661 return (DW_DLE_MEMORY); 662 } 663 llbuf->ld_lopc = 0; 664 llbuf->ld_hipc = ~0ULL; 665 llbuf->ld_s = NULL; 666 667 ret = _dwarf_loc_fill_locdesc(dbg, llbuf, in, in_len, pointer_size, 668 offset_size, version, error); 669 if (ret != DW_DLE_NONE) { 670 free(llbuf); 671 return (ret); 672 } 673 674 *ret_llbuf = llbuf; 675 676 return (ret); 677 } 678 679 int 680 _dwarf_loc_add(Dwarf_Die die, Dwarf_Attribute at, Dwarf_Error *error) 681 { 682 Dwarf_Debug dbg; 683 Dwarf_CU cu; 684 int ret; 685 686 assert(at->at_ld == NULL); 687 assert(at->u[1].u8p != NULL); 688 assert(at->u[0].u64 > 0); 689 690 cu = die->die_cu; 691 assert(cu != NULL); 692 693 dbg = cu->cu_dbg; 694 assert(dbg != NULL); 695 696 ret = _dwarf_loc_fill_locexpr(dbg, &at->at_ld, at->u[1].u8p, 697 at->u[0].u64, cu->cu_pointer_size, cu->cu_length_size == 4 ? 4 : 8, 698 cu->cu_version, error); 699 700 return (ret); 701 } 702