1 /* DWARF 2 Expression Evaluator.
3 Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 Contributed by Daniel Berlin (dan@dberlin.org)
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "dwarf2expr.h"
30 #include "gdb_assert.h"
32 /* Local prototypes. */
34 static void execute_stack_op (struct dwarf_expr_context *,
35 gdb_byte *, gdb_byte *);
36 static struct type *unsigned_address_type (struct gdbarch *, int);
38 /* Create a new context for the expression evaluator. */
40 struct dwarf_expr_context *
41 new_dwarf_expr_context (void)
43 struct dwarf_expr_context *retval;
44 retval = xcalloc (1, sizeof (struct dwarf_expr_context));
45 retval->stack_len = 0;
46 retval->stack_allocated = 10;
47 retval->stack = xmalloc (retval->stack_allocated * sizeof (CORE_ADDR));
48 retval->num_pieces = 0;
50 retval->max_recursion_depth = 0x100;
54 /* Release the memory allocated to CTX. */
57 free_dwarf_expr_context (struct dwarf_expr_context *ctx)
64 /* Expand the memory allocated to CTX's stack to contain at least
65 NEED more elements than are currently used. */
68 dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need)
70 if (ctx->stack_len + need > ctx->stack_allocated)
72 size_t newlen = ctx->stack_len + need + 10;
73 ctx->stack = xrealloc (ctx->stack,
74 newlen * sizeof (CORE_ADDR));
75 ctx->stack_allocated = newlen;
79 /* Push VALUE onto CTX's stack. */
82 dwarf_expr_push (struct dwarf_expr_context *ctx, CORE_ADDR value)
84 dwarf_expr_grow_stack (ctx, 1);
85 ctx->stack[ctx->stack_len++] = value;
88 /* Pop the top item off of CTX's stack. */
91 dwarf_expr_pop (struct dwarf_expr_context *ctx)
93 if (ctx->stack_len <= 0)
94 error (_("dwarf expression stack underflow"));
98 /* Retrieve the N'th item on CTX's stack. */
101 dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n)
103 if (ctx->stack_len <= n)
104 error (_("Asked for position %d of stack, stack only has %d elements on it."),
106 return ctx->stack[ctx->stack_len - (1 + n)];
110 /* Add a new piece to CTX's piece list. */
112 add_piece (struct dwarf_expr_context *ctx,
113 int in_reg, CORE_ADDR value, ULONGEST size)
115 struct dwarf_expr_piece *p;
120 ctx->pieces = xrealloc (ctx->pieces,
122 * sizeof (struct dwarf_expr_piece)));
124 ctx->pieces = xmalloc (ctx->num_pieces
125 * sizeof (struct dwarf_expr_piece));
127 p = &ctx->pieces[ctx->num_pieces - 1];
133 /* Evaluate the expression at ADDR (LEN bytes long) using the context
137 dwarf_expr_eval (struct dwarf_expr_context *ctx, gdb_byte *addr, size_t len)
139 int old_recursion_depth = ctx->recursion_depth;
141 execute_stack_op (ctx, addr, addr + len);
143 /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here. */
145 gdb_assert (ctx->recursion_depth == old_recursion_depth);
148 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
149 by R, and return the new value of BUF. Verify that it doesn't extend
153 read_uleb128 (gdb_byte *buf, gdb_byte *buf_end, ULONGEST * r)
162 error (_("read_uleb128: Corrupted DWARF expression."));
165 result |= (byte & 0x7f) << shift;
166 if ((byte & 0x80) == 0)
174 /* Decode the signed LEB128 constant at BUF into the variable pointed to
175 by R, and return the new value of BUF. Verify that it doesn't extend
179 read_sleb128 (gdb_byte *buf, gdb_byte *buf_end, LONGEST * r)
188 error (_("read_sleb128: Corrupted DWARF expression."));
191 result |= (byte & 0x7f) << shift;
193 if ((byte & 0x80) == 0)
196 if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
197 result |= -(1 << shift);
203 /* Read an address of size ADDR_SIZE from BUF, and verify that it
204 doesn't extend past BUF_END. */
207 dwarf2_read_address (struct gdbarch *gdbarch, gdb_byte *buf,
208 gdb_byte *buf_end, int addr_size)
210 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
213 if (buf_end - buf < addr_size)
214 error (_("dwarf2_read_address: Corrupted DWARF expression."));
216 /* For most architectures, calling extract_unsigned_integer() alone
217 is sufficient for extracting an address. However, some
218 architectures (e.g. MIPS) use signed addresses and using
219 extract_unsigned_integer() will not produce a correct
220 result. Make sure we invoke gdbarch_integer_to_address()
221 for those architectures which require it.
223 The use of `unsigned_address_type' in the code below refers to
224 the type of buf and has no bearing on the signedness of the
225 address being returned. */
227 if (gdbarch_integer_to_address_p (gdbarch))
228 return gdbarch_integer_to_address
229 (gdbarch, unsigned_address_type (gdbarch, addr_size), buf);
231 return extract_unsigned_integer (buf, addr_size, byte_order);
234 /* Return the type of an address of size ADDR_SIZE,
235 for unsigned arithmetic. */
238 unsigned_address_type (struct gdbarch *gdbarch, int addr_size)
243 return builtin_type (gdbarch)->builtin_uint16;
245 return builtin_type (gdbarch)->builtin_uint32;
247 return builtin_type (gdbarch)->builtin_uint64;
249 internal_error (__FILE__, __LINE__,
250 _("Unsupported address size.\n"));
254 /* Return the type of an address of size ADDR_SIZE,
255 for signed arithmetic. */
258 signed_address_type (struct gdbarch *gdbarch, int addr_size)
263 return builtin_type (gdbarch)->builtin_int16;
265 return builtin_type (gdbarch)->builtin_int32;
267 return builtin_type (gdbarch)->builtin_int64;
269 internal_error (__FILE__, __LINE__,
270 _("Unsupported address size.\n"));
274 /* The engine for the expression evaluator. Using the context in CTX,
275 evaluate the expression between OP_PTR and OP_END. */
278 execute_stack_op (struct dwarf_expr_context *ctx,
279 gdb_byte *op_ptr, gdb_byte *op_end)
281 enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
284 ctx->initialized = 1; /* Default is initialized. */
286 if (ctx->recursion_depth > ctx->max_recursion_depth)
287 error (_("DWARF-2 expression error: Loop detected (%d)."),
288 ctx->recursion_depth);
289 ctx->recursion_depth++;
291 while (op_ptr < op_end)
293 enum dwarf_location_atom op = *op_ptr++;
295 ULONGEST uoffset, reg;
332 result = op - DW_OP_lit0;
336 result = dwarf2_read_address (ctx->gdbarch,
337 op_ptr, op_end, ctx->addr_size);
338 op_ptr += ctx->addr_size;
342 result = extract_unsigned_integer (op_ptr, 1, byte_order);
346 result = extract_signed_integer (op_ptr, 1, byte_order);
350 result = extract_unsigned_integer (op_ptr, 2, byte_order);
354 result = extract_signed_integer (op_ptr, 2, byte_order);
358 result = extract_unsigned_integer (op_ptr, 4, byte_order);
362 result = extract_signed_integer (op_ptr, 4, byte_order);
366 result = extract_unsigned_integer (op_ptr, 8, byte_order);
370 result = extract_signed_integer (op_ptr, 8, byte_order);
374 op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
378 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
382 /* The DW_OP_reg operations are required to occur alone in
383 location expressions. */
417 && *op_ptr != DW_OP_piece
418 && *op_ptr != DW_OP_GNU_uninit)
419 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
420 "used either alone or in conjuction with DW_OP_piece."));
422 result = op - DW_OP_reg0;
428 op_ptr = read_uleb128 (op_ptr, op_end, ®);
429 if (op_ptr != op_end && *op_ptr != DW_OP_piece)
430 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
431 "used either alone or in conjuction with DW_OP_piece."));
470 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
471 result = (ctx->read_reg) (ctx->baton, op - DW_OP_breg0);
477 op_ptr = read_uleb128 (op_ptr, op_end, ®);
478 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
479 result = (ctx->read_reg) (ctx->baton, reg);
487 unsigned int before_stack_len;
489 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
490 /* Rather than create a whole new context, we simply
491 record the stack length before execution, then reset it
492 afterwards, effectively erasing whatever the recursive
494 before_stack_len = ctx->stack_len;
495 /* FIXME: cagney/2003-03-26: This code should be using
496 get_frame_base_address(), and then implement a dwarf2
497 specific this_base method. */
498 (ctx->get_frame_base) (ctx->baton, &datastart, &datalen);
499 dwarf_expr_eval (ctx, datastart, datalen);
500 result = dwarf_expr_fetch (ctx, 0);
502 result = (ctx->read_reg) (ctx->baton, result);
503 result = result + offset;
504 ctx->stack_len = before_stack_len;
509 result = dwarf_expr_fetch (ctx, 0);
513 dwarf_expr_pop (ctx);
518 result = dwarf_expr_fetch (ctx, offset);
525 if (ctx->stack_len < 2)
526 error (_("Not enough elements for DW_OP_swap. Need 2, have %d."),
528 t1 = ctx->stack[ctx->stack_len - 1];
529 t2 = ctx->stack[ctx->stack_len - 2];
530 ctx->stack[ctx->stack_len - 1] = t2;
531 ctx->stack[ctx->stack_len - 2] = t1;
536 result = dwarf_expr_fetch (ctx, 1);
541 CORE_ADDR t1, t2, t3;
543 if (ctx->stack_len < 3)
544 error (_("Not enough elements for DW_OP_rot. Need 3, have %d."),
546 t1 = ctx->stack[ctx->stack_len - 1];
547 t2 = ctx->stack[ctx->stack_len - 2];
548 t3 = ctx->stack[ctx->stack_len - 3];
549 ctx->stack[ctx->stack_len - 1] = t2;
550 ctx->stack[ctx->stack_len - 2] = t3;
551 ctx->stack[ctx->stack_len - 3] = t1;
556 case DW_OP_deref_size:
560 case DW_OP_plus_uconst:
561 /* Unary operations. */
562 result = dwarf_expr_fetch (ctx, 0);
563 dwarf_expr_pop (ctx);
569 gdb_byte *buf = alloca (ctx->addr_size);
570 (ctx->read_mem) (ctx->baton, buf, result, ctx->addr_size);
571 result = dwarf2_read_address (ctx->gdbarch,
572 buf, buf + ctx->addr_size,
577 case DW_OP_deref_size:
579 int addr_size = *op_ptr++;
580 gdb_byte *buf = alloca (addr_size);
581 (ctx->read_mem) (ctx->baton, buf, result, addr_size);
582 result = dwarf2_read_address (ctx->gdbarch,
583 buf, buf + addr_size,
589 if ((signed int) result < 0)
598 case DW_OP_plus_uconst:
599 op_ptr = read_uleb128 (op_ptr, op_end, ®);
623 /* Binary operations. Use the value engine to do computations in
625 CORE_ADDR first, second;
626 enum exp_opcode binop;
627 struct value *val1, *val2;
628 struct type *stype, *utype;
630 second = dwarf_expr_fetch (ctx, 0);
631 dwarf_expr_pop (ctx);
633 first = dwarf_expr_fetch (ctx, 0);
634 dwarf_expr_pop (ctx);
636 utype = unsigned_address_type (ctx->gdbarch, ctx->addr_size);
637 stype = signed_address_type (ctx->gdbarch, ctx->addr_size);
638 val1 = value_from_longest (utype, first);
639 val2 = value_from_longest (utype, second);
644 binop = BINOP_BITWISE_AND;
659 binop = BINOP_BITWISE_IOR;
672 val1 = value_from_longest (stype, first);
675 binop = BINOP_BITWISE_XOR;
693 binop = BINOP_NOTEQUAL;
696 internal_error (__FILE__, __LINE__,
697 _("Can't be reached."));
699 result = value_as_long (value_binop (val1, val2, binop));
703 case DW_OP_GNU_push_tls_address:
704 /* Variable is at a constant offset in the thread-local
705 storage block into the objfile for the current thread and
706 the dynamic linker module containing this expression. Here
707 we return returns the offset from that base. The top of the
708 stack has the offset from the beginning of the thread
709 control block at which the variable is located. Nothing
710 should follow this operator, so the top of stack would be
712 result = dwarf_expr_fetch (ctx, 0);
713 dwarf_expr_pop (ctx);
714 result = (ctx->get_tls_address) (ctx->baton, result);
718 offset = extract_signed_integer (op_ptr, 2, byte_order);
724 offset = extract_signed_integer (op_ptr, 2, byte_order);
726 if (dwarf_expr_fetch (ctx, 0) != 0)
728 dwarf_expr_pop (ctx);
737 CORE_ADDR addr_or_regnum;
739 /* Record the piece. */
740 op_ptr = read_uleb128 (op_ptr, op_end, &size);
741 addr_or_regnum = dwarf_expr_fetch (ctx, 0);
742 add_piece (ctx, ctx->in_reg, addr_or_regnum, size);
744 /* Pop off the address/regnum, and clear the in_reg flag. */
745 dwarf_expr_pop (ctx);
750 case DW_OP_GNU_uninit:
751 if (op_ptr != op_end)
752 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
753 "be the very last op."));
755 ctx->initialized = 0;
759 error (_("Unhandled dwarf expression opcode 0x%x"), op);
762 /* Most things push a result value. */
763 dwarf_expr_push (ctx, result);
767 ctx->recursion_depth--;
768 gdb_assert (ctx->recursion_depth >= 0);