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/>. */
28 #include "elf/dwarf2.h"
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 (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)
212 if (buf_end - buf < addr_size)
213 error (_("dwarf2_read_address: Corrupted DWARF expression."));
215 /* For most architectures, calling extract_unsigned_integer() alone
216 is sufficient for extracting an address. However, some
217 architectures (e.g. MIPS) use signed addresses and using
218 extract_unsigned_integer() will not produce a correct
219 result. Make sure we invoke gdbarch_integer_to_address()
220 for those architectures which require it.
222 The use of `unsigned_address_type' in the code below refers to
223 the type of buf and has no bearing on the signedness of the
224 address being returned. */
226 if (gdbarch_integer_to_address_p (gdbarch))
227 return gdbarch_integer_to_address
228 (gdbarch, unsigned_address_type (addr_size), buf);
230 return extract_unsigned_integer (buf, addr_size);
233 /* Return the type of an address of size ADDR_SIZE,
234 for unsigned arithmetic. */
237 unsigned_address_type (int addr_size)
242 return builtin_type_uint16;
244 return builtin_type_uint32;
246 return builtin_type_uint64;
248 internal_error (__FILE__, __LINE__,
249 _("Unsupported address size.\n"));
253 /* Return the type of an address of size ADDR_SIZE,
254 for signed arithmetic. */
257 signed_address_type (int addr_size)
262 return builtin_type_int16;
264 return builtin_type_int32;
266 return builtin_type_int64;
268 internal_error (__FILE__, __LINE__,
269 _("Unsupported address size.\n"));
273 /* The engine for the expression evaluator. Using the context in CTX,
274 evaluate the expression between OP_PTR and OP_END. */
277 execute_stack_op (struct dwarf_expr_context *ctx,
278 gdb_byte *op_ptr, gdb_byte *op_end)
281 ctx->initialized = 1; /* Default is initialized. */
283 if (ctx->recursion_depth > ctx->max_recursion_depth)
284 error (_("DWARF-2 expression error: Loop detected (%d)."),
285 ctx->recursion_depth);
286 ctx->recursion_depth++;
288 while (op_ptr < op_end)
290 enum dwarf_location_atom op = *op_ptr++;
292 ULONGEST uoffset, reg;
329 result = op - DW_OP_lit0;
333 result = dwarf2_read_address (ctx->gdbarch,
334 op_ptr, op_end, ctx->addr_size);
335 op_ptr += ctx->addr_size;
339 result = extract_unsigned_integer (op_ptr, 1);
343 result = extract_signed_integer (op_ptr, 1);
347 result = extract_unsigned_integer (op_ptr, 2);
351 result = extract_signed_integer (op_ptr, 2);
355 result = extract_unsigned_integer (op_ptr, 4);
359 result = extract_signed_integer (op_ptr, 4);
363 result = extract_unsigned_integer (op_ptr, 8);
367 result = extract_signed_integer (op_ptr, 8);
371 op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
375 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
379 /* The DW_OP_reg operations are required to occur alone in
380 location expressions. */
414 && *op_ptr != DW_OP_piece
415 && *op_ptr != DW_OP_GNU_uninit)
416 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
417 "used either alone or in conjuction with DW_OP_piece."));
419 result = op - DW_OP_reg0;
425 op_ptr = read_uleb128 (op_ptr, op_end, ®);
426 if (op_ptr != op_end && *op_ptr != DW_OP_piece)
427 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
428 "used either alone or in conjuction with DW_OP_piece."));
467 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
468 result = (ctx->read_reg) (ctx->baton, op - DW_OP_breg0);
474 op_ptr = read_uleb128 (op_ptr, op_end, ®);
475 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
476 result = (ctx->read_reg) (ctx->baton, reg);
484 unsigned int before_stack_len;
486 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
487 /* Rather than create a whole new context, we simply
488 record the stack length before execution, then reset it
489 afterwards, effectively erasing whatever the recursive
491 before_stack_len = ctx->stack_len;
492 /* FIXME: cagney/2003-03-26: This code should be using
493 get_frame_base_address(), and then implement a dwarf2
494 specific this_base method. */
495 (ctx->get_frame_base) (ctx->baton, &datastart, &datalen);
496 dwarf_expr_eval (ctx, datastart, datalen);
497 result = dwarf_expr_fetch (ctx, 0);
499 result = (ctx->read_reg) (ctx->baton, result);
500 result = result + offset;
501 ctx->stack_len = before_stack_len;
506 result = dwarf_expr_fetch (ctx, 0);
510 dwarf_expr_pop (ctx);
515 result = dwarf_expr_fetch (ctx, offset);
522 if (ctx->stack_len < 2)
523 error (_("Not enough elements for DW_OP_swap. Need 2, have %d."),
525 t1 = ctx->stack[ctx->stack_len - 1];
526 t2 = ctx->stack[ctx->stack_len - 2];
527 ctx->stack[ctx->stack_len - 1] = t2;
528 ctx->stack[ctx->stack_len - 2] = t1;
533 result = dwarf_expr_fetch (ctx, 1);
538 CORE_ADDR t1, t2, t3;
540 if (ctx->stack_len < 3)
541 error (_("Not enough elements for DW_OP_rot. Need 3, have %d."),
543 t1 = ctx->stack[ctx->stack_len - 1];
544 t2 = ctx->stack[ctx->stack_len - 2];
545 t3 = ctx->stack[ctx->stack_len - 3];
546 ctx->stack[ctx->stack_len - 1] = t2;
547 ctx->stack[ctx->stack_len - 2] = t3;
548 ctx->stack[ctx->stack_len - 3] = t1;
553 case DW_OP_deref_size:
557 case DW_OP_plus_uconst:
558 /* Unary operations. */
559 result = dwarf_expr_fetch (ctx, 0);
560 dwarf_expr_pop (ctx);
566 gdb_byte *buf = alloca (ctx->addr_size);
567 (ctx->read_mem) (ctx->baton, buf, result, ctx->addr_size);
568 result = dwarf2_read_address (ctx->gdbarch,
569 buf, buf + ctx->addr_size,
574 case DW_OP_deref_size:
576 int addr_size = *op_ptr++;
577 gdb_byte *buf = alloca (addr_size);
578 (ctx->read_mem) (ctx->baton, buf, result, addr_size);
579 result = dwarf2_read_address (ctx->gdbarch,
580 buf, buf + addr_size,
586 if ((signed int) result < 0)
595 case DW_OP_plus_uconst:
596 op_ptr = read_uleb128 (op_ptr, op_end, ®);
620 /* Binary operations. Use the value engine to do computations in
622 CORE_ADDR first, second;
623 enum exp_opcode binop;
624 struct value *val1, *val2;
626 second = dwarf_expr_fetch (ctx, 0);
627 dwarf_expr_pop (ctx);
629 first = dwarf_expr_fetch (ctx, 0);
630 dwarf_expr_pop (ctx);
632 val1 = value_from_longest
633 (unsigned_address_type (ctx->addr_size), first);
634 val2 = value_from_longest
635 (unsigned_address_type (ctx->addr_size), second);
640 binop = BINOP_BITWISE_AND;
655 binop = BINOP_BITWISE_IOR;
668 val1 = value_from_longest
669 (signed_address_type (ctx->addr_size), first);
672 binop = BINOP_BITWISE_XOR;
690 binop = BINOP_NOTEQUAL;
693 internal_error (__FILE__, __LINE__,
694 _("Can't be reached."));
696 result = value_as_long (value_binop (val1, val2, binop));
700 case DW_OP_GNU_push_tls_address:
701 /* Variable is at a constant offset in the thread-local
702 storage block into the objfile for the current thread and
703 the dynamic linker module containing this expression. Here
704 we return returns the offset from that base. The top of the
705 stack has the offset from the beginning of the thread
706 control block at which the variable is located. Nothing
707 should follow this operator, so the top of stack would be
709 result = dwarf_expr_fetch (ctx, 0);
710 dwarf_expr_pop (ctx);
711 result = (ctx->get_tls_address) (ctx->baton, result);
715 offset = extract_signed_integer (op_ptr, 2);
721 offset = extract_signed_integer (op_ptr, 2);
723 if (dwarf_expr_fetch (ctx, 0) != 0)
725 dwarf_expr_pop (ctx);
734 CORE_ADDR addr_or_regnum;
736 /* Record the piece. */
737 op_ptr = read_uleb128 (op_ptr, op_end, &size);
738 addr_or_regnum = dwarf_expr_fetch (ctx, 0);
739 add_piece (ctx, ctx->in_reg, addr_or_regnum, size);
741 /* Pop off the address/regnum, and clear the in_reg flag. */
742 dwarf_expr_pop (ctx);
747 case DW_OP_GNU_uninit:
748 if (op_ptr != op_end)
749 error (_("DWARF-2 expression error: DW_OP_GNU_unint must always "
750 "be the very last op."));
752 ctx->initialized = 0;
756 error (_("Unhandled dwarf expression opcode 0x%x"), op);
759 /* Most things push a result value. */
760 dwarf_expr_push (ctx, result);
764 ctx->recursion_depth--;
765 gdb_assert (ctx->recursion_depth >= 0);