2 * Copyright (c) 2007 John Birrell (jb@freebsd.org)
3 * Copyright (c) 2014 Kai Wang
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include "_libdwarf.h"
30 ELFTC_VCSID("$Id: libdwarf_loc.c 3070 2014-06-23 03:08:33Z kaiwang27 $");
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.
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)
54 * Process each byte. If an error occurs, then the
55 * count will be set to -1.
63 lbuf->ld_s[count].lr_atom = *p;
64 lbuf->ld_s[count].lr_offset = p - ps;
68 /* Operations with no operands. */
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:
177 /* Operations with 1-byte operands. */
180 case DW_OP_deref_size:
181 case DW_OP_xderef_size:
186 operand1 = (int8_t) *p++;
189 /* Operations with 2-byte operands. */
194 operand1 = dbg->decode(&p, 2);
198 operand1 = (int16_t) dbg->decode(&p, 2);
201 /* Operations with 4-byte operands. */
204 case DW_OP_GNU_parameter_ref:
205 operand1 = dbg->decode(&p, 4);
209 operand1 = (int32_t) dbg->decode(&p, 4);
212 /* Operations with 8-byte operands. */
215 operand1 = dbg->decode(&p, 8);
218 /* Operations with an unsigned LEB128 operand. */
220 case DW_OP_plus_uconst:
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);
229 /* Operations with a signed LEB128 operand. */
264 operand1 = _dwarf_decode_sleb128(&p);
268 * Oeration with two unsigned LEB128 operands.
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);
277 * Operations with an unsigned LEB128 operand
278 * followed by a signed LEB128 operand.
281 operand1 = _dwarf_decode_uleb128(&p);
282 operand2 = _dwarf_decode_sleb128(&p);
286 * Operation with an unsigned LEB128 operand
287 * representing the size of a block, followed
288 * by the block content.
290 * Store the size of the block in the operand1
291 * and a pointer to the block in the operand2.
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;
300 /* Target address size operand. */
302 case DW_OP_GNU_addr_index:
303 case DW_OP_GNU_const_index:
304 operand1 = dbg->decode(&p, pointer_size);
307 /* Offset size operand. */
309 operand1 = dbg->decode(&p, offset_size);
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.
317 case DW_OP_GNU_encoded_addr:
321 operand1 = dbg->decode(&p, s);
325 * Operand1: DIE offset (size depending on DWARF version)
326 * DWARF2: pointer size
327 * DWARF{3,4}: offset size
331 case DW_OP_GNU_implicit_pointer:
333 operand1 = dbg->decode(&p, pointer_size);
335 operand1 = dbg->decode(&p, offset_size);
336 operand2 = _dwarf_decode_sleb128(&p);
340 * Operand1: DIE offset (ULEB128)
341 * Operand2: pointer to a block. The block's first byte
344 case DW_OP_GNU_const_type:
345 operand1 = _dwarf_decode_uleb128(&p);
346 operand2 = (Dwarf_Unsigned) (uintptr_t) p;
351 /* All other operations cause an error. */
358 lbuf->ld_s[count].lr_number = operand1;
359 lbuf->ld_s[count].lr_number2 = operand2;
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)
379 if (out != NULL && end != NULL) {
384 pe = &buf[sizeof(buf)];
388 /* Operations with no operands. */
489 case DW_OP_GNU_push_tls_address:
493 /* Operations with 1-byte operands. */
497 case DW_OP_deref_size:
498 case DW_OP_xderef_size:
500 *p++ = (uint8_t) operand1;
503 /* Operations with 2-byte operands. */
510 dbg->write(p, &offset, operand1, 2);
514 /* Operations with 4-byte operands. */
519 dbg->write(p, &offset, operand1, 4);
523 /* Operations with 8-byte operands. */
528 dbg->write(p, &offset, operand1, 8);
532 /* Operations with an unsigned LEB128 operand. */
534 case DW_OP_plus_uconst:
538 len = _dwarf_write_uleb128(p, pe, operand1);
543 /* Operations with a signed LEB128 operand. */
579 len = _dwarf_write_sleb128(p, pe, operand1);
585 * Operations with an unsigned LEB128 operand
586 * followed by a signed LEB128 operand.
590 len = _dwarf_write_uleb128(p, pe, operand1);
593 len = _dwarf_write_sleb128(p, pe, operand2);
598 /* Target address size operand. */
602 dbg->write(p, &offset, operand1, dbg->dbg_pointer_size);
603 p += dbg->dbg_pointer_size;
606 /* All other operations cause an error. */
608 DWARF_SET_ERROR(dbg, error, DW_DLE_LOC_EXPR_BAD);
609 return (DW_DLE_LOC_EXPR_BAD);
615 return (DW_DLE_NONE);
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)
625 assert(llbuf != NULL);
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);
636 llbuf->ld_cents = num;
638 return (DW_DLE_NONE);
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);
645 (void) _dwarf_loc_fill_loc(dbg, llbuf, pointer_size, offset_size,
646 version, in, in_len);
648 return (DW_DLE_NONE);
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)
656 Dwarf_Locdesc *llbuf;
659 if ((llbuf = malloc(sizeof(Dwarf_Locdesc))) == NULL) {
660 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
661 return (DW_DLE_MEMORY);
664 llbuf->ld_hipc = ~0ULL;
667 ret = _dwarf_loc_fill_locdesc(dbg, llbuf, in, in_len, pointer_size,
668 offset_size, version, error);
669 if (ret != DW_DLE_NONE) {
680 _dwarf_loc_add(Dwarf_Die die, Dwarf_Attribute at, Dwarf_Error *error)
686 assert(at->at_ld == NULL);
687 assert(at->u[1].u8p != NULL);
688 assert(at->u[0].u64 > 0);
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);