2 * Copyright (c) 2007 John Birrell (jb@freebsd.org)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include "_libdwarf.h"
33 dwarf_decode_sleb128(uint8_t **dp)
44 ret |= ((b & 0x7f) << shift);
47 } while ((b & 0x80) != 0);
49 if (shift < 64 && (b & 0x40) != 0)
58 dwarf_decode_uleb128(uint8_t **dp)
69 ret |= ((b & 0x7f) << shift);
72 } while ((b & 0x80) != 0);
80 * Given an array of bytes of length 'len' representing a
81 * DWARF expression, compute the number of operations based
82 * on there being one byte describing the operation and
83 * zero or more bytes of operands as defined in the standard
84 * for each operation type.
87 dwarf_op_num(uint8_t pointer_size, uint8_t *p, int len)
92 uint8_t *last = p + len;
95 * Process each byte. If an error occurs, then the
96 * count will be set to -1.
98 while (p < last && count >= 0) {
102 /* Operations with no operands. */
205 /* Operations with 1-byte operands. */
209 case DW_OP_deref_size:
210 case DW_OP_xderef_size:
214 /* Operations with 2-byte operands. */
222 /* Operations with 4-byte operands. */
228 /* Operations with 8-byte operands. */
234 /* Operations with an unsigned LEB128 operand. */
236 case DW_OP_plus_uconst:
239 uval = dwarf_decode_uleb128(&p);
242 /* Operations with a signed LEB128 operand. */
277 sval = dwarf_decode_sleb128(&p);
281 * Operations with an unsigned LEB128 operand
282 * followed by a signed LEB128 operand.
285 uval = dwarf_decode_uleb128(&p);
286 sval = dwarf_decode_sleb128(&p);
289 /* Target address size operand. */
294 /* All other operations cause an error. */
305 dwarf_loc_fill(Dwarf_Locdesc *lbuf, uint8_t pointer_size, uint8_t *p, int len)
308 int ret = DWARF_E_NONE;
311 uint8_t *last = p + len;
314 * Process each byte. If an error occurs, then the
315 * count will be set to -1.
317 while (p < last && ret == DWARF_E_NONE) {
321 lbuf->ld_s[count].lr_atom = *p;
324 /* Operations with no operands. */
427 /* Operations with 1-byte operands. */
431 case DW_OP_deref_size:
432 case DW_OP_xderef_size:
436 /* Operations with 2-byte operands. */
444 /* Operations with 4-byte operands. */
450 /* Operations with 8-byte operands. */
456 /* Operations with an unsigned LEB128 operand. */
458 case DW_OP_plus_uconst:
461 operand1 = dwarf_decode_uleb128(&p);
464 /* Operations with a signed LEB128 operand. */
499 operand1 = dwarf_decode_sleb128(&p);
503 * Operations with an unsigned LEB128 operand
504 * followed by a signed LEB128 operand.
507 operand1 = dwarf_decode_uleb128(&p);
508 operand2 = dwarf_decode_sleb128(&p);
511 /* Target address size operand. */
516 /* All other operations cause an error. */
521 lbuf->ld_s[count].lr_number = operand1;
522 lbuf->ld_s[count].lr_number2 = operand2;
531 dwarf_locdesc(Dwarf_Die die, uint64_t attr, Dwarf_Locdesc **llbuf, Dwarf_Signed *lenp, Dwarf_Error *err)
536 int ret = DWARF_E_NONE;
539 return DWARF_E_ERROR;
541 if (die == NULL || llbuf == NULL || lenp == NULL) {
542 DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
543 return DWARF_E_ARGUMENT;
546 if ((av = dwarf_attrval_find(die, attr)) == NULL) {
547 DWARF_SET_ERROR(err, DWARF_E_NO_ENTRY);
548 ret = DWARF_E_NO_ENTRY;
549 } else if ((lbuf = calloc(sizeof(Dwarf_Locdesc), 1)) == NULL) {
550 DWARF_SET_ERROR(err, DWARF_E_MEMORY);
551 ret = DWARF_E_MEMORY;
554 switch (av->av_form) {
559 /* Compute the number of locations: */
560 if ((num = dwarf_op_num(die->die_cu->cu_pointer_size,
561 av->u[1].u8p, av->u[0].u64)) < 0) {
562 DWARF_SET_ERROR(err, DWARF_E_INVALID_EXPR);
563 ret = DWARF_E_INVALID_EXPR;
565 /* Allocate an array of location structures. */
566 } else if ((lbuf->ld_s =
567 calloc(sizeof(Dwarf_Loc), num)) == NULL) {
568 DWARF_SET_ERROR(err, DWARF_E_MEMORY);
569 ret = DWARF_E_MEMORY;
571 /* Fill the array of location structures. */
572 } else if ((ret = dwarf_loc_fill(lbuf,
573 die->die_cu->cu_pointer_size,
574 av->u[1].u8p, av->u[0].u64)) != DWARF_E_NONE) {
577 /* Only one descriptor is returned. */
581 printf("%s(%d): form %s not handled\n",__func__,
582 __LINE__,get_form_desc(av->av_form));
583 DWARF_SET_ERROR(err, DWARF_E_NOT_IMPLEMENTED);
587 if (ret == DWARF_E_NONE) {
597 dwarf_locdesc_free(Dwarf_Locdesc *lbuf, Dwarf_Error *err)
600 return DWARF_E_ERROR;
603 DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
604 return DWARF_E_ARGUMENT;
607 if (lbuf->ld_s != NULL)