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 /* Helper for make_cleanup_free_dwarf_expr_context. */
67 free_dwarf_expr_context_cleanup (void *arg
)
69 free_dwarf_expr_context (arg
);
72 /* Return a cleanup that calls free_dwarf_expr_context. */
75 make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context
*ctx
)
77 return make_cleanup (free_dwarf_expr_context_cleanup
, ctx
);
80 /* Expand the memory allocated to CTX's stack to contain at least
81 NEED more elements than are currently used. */
84 dwarf_expr_grow_stack (struct dwarf_expr_context
*ctx
, size_t need
)
86 if (ctx
->stack_len
+ need
> ctx
->stack_allocated
)
88 size_t newlen
= ctx
->stack_len
+ need
+ 10;
89 ctx
->stack
= xrealloc (ctx
->stack
,
90 newlen
* sizeof (CORE_ADDR
));
91 ctx
->stack_allocated
= newlen
;
95 /* Push VALUE onto CTX's stack. */
98 dwarf_expr_push (struct dwarf_expr_context
*ctx
, CORE_ADDR value
)
100 dwarf_expr_grow_stack (ctx
, 1);
101 ctx
->stack
[ctx
->stack_len
++] = value
;
104 /* Pop the top item off of CTX's stack. */
107 dwarf_expr_pop (struct dwarf_expr_context
*ctx
)
109 if (ctx
->stack_len
<= 0)
110 error (_("dwarf expression stack underflow"));
114 /* Retrieve the N'th item on CTX's stack. */
117 dwarf_expr_fetch (struct dwarf_expr_context
*ctx
, int n
)
119 if (ctx
->stack_len
<= n
)
120 error (_("Asked for position %d of stack, stack only has %d elements on it."),
122 return ctx
->stack
[ctx
->stack_len
- (1 + n
)];
126 /* Add a new piece to CTX's piece list. */
128 add_piece (struct dwarf_expr_context
*ctx
,
129 int in_reg
, CORE_ADDR value
, ULONGEST size
)
131 struct dwarf_expr_piece
*p
;
136 ctx
->pieces
= xrealloc (ctx
->pieces
,
138 * sizeof (struct dwarf_expr_piece
)));
140 ctx
->pieces
= xmalloc (ctx
->num_pieces
141 * sizeof (struct dwarf_expr_piece
));
143 p
= &ctx
->pieces
[ctx
->num_pieces
- 1];
149 /* Evaluate the expression at ADDR (LEN bytes long) using the context
153 dwarf_expr_eval (struct dwarf_expr_context
*ctx
, gdb_byte
*addr
, size_t len
)
155 int old_recursion_depth
= ctx
->recursion_depth
;
157 execute_stack_op (ctx
, addr
, addr
+ len
);
159 /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here. */
161 gdb_assert (ctx
->recursion_depth
== old_recursion_depth
);
164 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
165 by R, and return the new value of BUF. Verify that it doesn't extend
169 read_uleb128 (gdb_byte
*buf
, gdb_byte
*buf_end
, ULONGEST
* r
)
178 error (_("read_uleb128: Corrupted DWARF expression."));
181 result
|= (byte
& 0x7f) << shift
;
182 if ((byte
& 0x80) == 0)
190 /* Decode the signed LEB128 constant at BUF into the variable pointed to
191 by R, and return the new value of BUF. Verify that it doesn't extend
195 read_sleb128 (gdb_byte
*buf
, gdb_byte
*buf_end
, LONGEST
* r
)
204 error (_("read_sleb128: Corrupted DWARF expression."));
207 result
|= (byte
& 0x7f) << shift
;
209 if ((byte
& 0x80) == 0)
212 if (shift
< (sizeof (*r
) * 8) && (byte
& 0x40) != 0)
213 result
|= -(1 << shift
);
219 /* Read an address of size ADDR_SIZE from BUF, and verify that it
220 doesn't extend past BUF_END. */
223 dwarf2_read_address (struct gdbarch
*gdbarch
, gdb_byte
*buf
,
224 gdb_byte
*buf_end
, int addr_size
)
226 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
229 if (buf_end
- buf
< addr_size
)
230 error (_("dwarf2_read_address: Corrupted DWARF expression."));
232 /* For most architectures, calling extract_unsigned_integer() alone
233 is sufficient for extracting an address. However, some
234 architectures (e.g. MIPS) use signed addresses and using
235 extract_unsigned_integer() will not produce a correct
236 result. Make sure we invoke gdbarch_integer_to_address()
237 for those architectures which require it.
239 The use of `unsigned_address_type' in the code below refers to
240 the type of buf and has no bearing on the signedness of the
241 address being returned. */
243 if (gdbarch_integer_to_address_p (gdbarch
))
244 return gdbarch_integer_to_address
245 (gdbarch
, unsigned_address_type (gdbarch
, addr_size
), buf
);
247 return extract_unsigned_integer (buf
, addr_size
, byte_order
);
250 /* Return the type of an address of size ADDR_SIZE,
251 for unsigned arithmetic. */
254 unsigned_address_type (struct gdbarch
*gdbarch
, int addr_size
)
259 return builtin_type (gdbarch
)->builtin_uint16
;
261 return builtin_type (gdbarch
)->builtin_uint32
;
263 return builtin_type (gdbarch
)->builtin_uint64
;
265 internal_error (__FILE__
, __LINE__
,
266 _("Unsupported address size.\n"));
270 /* Return the type of an address of size ADDR_SIZE,
271 for signed arithmetic. */
274 signed_address_type (struct gdbarch
*gdbarch
, int addr_size
)
279 return builtin_type (gdbarch
)->builtin_int16
;
281 return builtin_type (gdbarch
)->builtin_int32
;
283 return builtin_type (gdbarch
)->builtin_int64
;
285 internal_error (__FILE__
, __LINE__
,
286 _("Unsupported address size.\n"));
290 /* The engine for the expression evaluator. Using the context in CTX,
291 evaluate the expression between OP_PTR and OP_END. */
294 execute_stack_op (struct dwarf_expr_context
*ctx
,
295 gdb_byte
*op_ptr
, gdb_byte
*op_end
)
297 enum bfd_endian byte_order
= gdbarch_byte_order (ctx
->gdbarch
);
300 ctx
->initialized
= 1; /* Default is initialized. */
302 if (ctx
->recursion_depth
> ctx
->max_recursion_depth
)
303 error (_("DWARF-2 expression error: Loop detected (%d)."),
304 ctx
->recursion_depth
);
305 ctx
->recursion_depth
++;
307 while (op_ptr
< op_end
)
309 enum dwarf_location_atom op
= *op_ptr
++;
311 ULONGEST uoffset
, reg
;
348 result
= op
- DW_OP_lit0
;
352 result
= dwarf2_read_address (ctx
->gdbarch
,
353 op_ptr
, op_end
, ctx
->addr_size
);
354 op_ptr
+= ctx
->addr_size
;
358 result
= extract_unsigned_integer (op_ptr
, 1, byte_order
);
362 result
= extract_signed_integer (op_ptr
, 1, byte_order
);
366 result
= extract_unsigned_integer (op_ptr
, 2, byte_order
);
370 result
= extract_signed_integer (op_ptr
, 2, byte_order
);
374 result
= extract_unsigned_integer (op_ptr
, 4, byte_order
);
378 result
= extract_signed_integer (op_ptr
, 4, byte_order
);
382 result
= extract_unsigned_integer (op_ptr
, 8, byte_order
);
386 result
= extract_signed_integer (op_ptr
, 8, byte_order
);
390 op_ptr
= read_uleb128 (op_ptr
, op_end
, &uoffset
);
394 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
398 /* The DW_OP_reg operations are required to occur alone in
399 location expressions. */
433 && *op_ptr
!= DW_OP_piece
434 && *op_ptr
!= DW_OP_GNU_uninit
)
435 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
436 "used either alone or in conjuction with DW_OP_piece."));
438 result
= op
- DW_OP_reg0
;
444 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
445 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
)
446 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
447 "used either alone or in conjuction with DW_OP_piece."));
486 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
487 result
= (ctx
->read_reg
) (ctx
->baton
, op
- DW_OP_breg0
);
493 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
494 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
495 result
= (ctx
->read_reg
) (ctx
->baton
, reg
);
503 unsigned int before_stack_len
;
505 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
506 /* Rather than create a whole new context, we simply
507 record the stack length before execution, then reset it
508 afterwards, effectively erasing whatever the recursive
510 before_stack_len
= ctx
->stack_len
;
511 /* FIXME: cagney/2003-03-26: This code should be using
512 get_frame_base_address(), and then implement a dwarf2
513 specific this_base method. */
514 (ctx
->get_frame_base
) (ctx
->baton
, &datastart
, &datalen
);
515 dwarf_expr_eval (ctx
, datastart
, datalen
);
516 result
= dwarf_expr_fetch (ctx
, 0);
518 result
= (ctx
->read_reg
) (ctx
->baton
, result
);
519 result
= result
+ offset
;
520 ctx
->stack_len
= before_stack_len
;
525 result
= dwarf_expr_fetch (ctx
, 0);
529 dwarf_expr_pop (ctx
);
534 result
= dwarf_expr_fetch (ctx
, offset
);
541 if (ctx
->stack_len
< 2)
542 error (_("Not enough elements for DW_OP_swap. Need 2, have %d."),
544 t1
= ctx
->stack
[ctx
->stack_len
- 1];
545 t2
= ctx
->stack
[ctx
->stack_len
- 2];
546 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
547 ctx
->stack
[ctx
->stack_len
- 2] = t1
;
552 result
= dwarf_expr_fetch (ctx
, 1);
557 CORE_ADDR t1
, t2
, t3
;
559 if (ctx
->stack_len
< 3)
560 error (_("Not enough elements for DW_OP_rot. Need 3, have %d."),
562 t1
= ctx
->stack
[ctx
->stack_len
- 1];
563 t2
= ctx
->stack
[ctx
->stack_len
- 2];
564 t3
= ctx
->stack
[ctx
->stack_len
- 3];
565 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
566 ctx
->stack
[ctx
->stack_len
- 2] = t3
;
567 ctx
->stack
[ctx
->stack_len
- 3] = t1
;
572 case DW_OP_deref_size
:
576 case DW_OP_plus_uconst
:
577 /* Unary operations. */
578 result
= dwarf_expr_fetch (ctx
, 0);
579 dwarf_expr_pop (ctx
);
585 gdb_byte
*buf
= alloca (ctx
->addr_size
);
586 (ctx
->read_mem
) (ctx
->baton
, buf
, result
, ctx
->addr_size
);
587 result
= dwarf2_read_address (ctx
->gdbarch
,
588 buf
, buf
+ ctx
->addr_size
,
593 case DW_OP_deref_size
:
595 int addr_size
= *op_ptr
++;
596 gdb_byte
*buf
= alloca (addr_size
);
597 (ctx
->read_mem
) (ctx
->baton
, buf
, result
, addr_size
);
598 result
= dwarf2_read_address (ctx
->gdbarch
,
599 buf
, buf
+ addr_size
,
605 if ((signed int) result
< 0)
614 case DW_OP_plus_uconst
:
615 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
639 /* Binary operations. Use the value engine to do computations in
641 CORE_ADDR first
, second
;
642 enum exp_opcode binop
;
643 struct value
*val1
, *val2
;
644 struct type
*stype
, *utype
;
646 second
= dwarf_expr_fetch (ctx
, 0);
647 dwarf_expr_pop (ctx
);
649 first
= dwarf_expr_fetch (ctx
, 0);
650 dwarf_expr_pop (ctx
);
652 utype
= unsigned_address_type (ctx
->gdbarch
, ctx
->addr_size
);
653 stype
= signed_address_type (ctx
->gdbarch
, ctx
->addr_size
);
654 val1
= value_from_longest (utype
, first
);
655 val2
= value_from_longest (utype
, second
);
660 binop
= BINOP_BITWISE_AND
;
675 binop
= BINOP_BITWISE_IOR
;
688 val1
= value_from_longest (stype
, first
);
691 binop
= BINOP_BITWISE_XOR
;
709 binop
= BINOP_NOTEQUAL
;
712 internal_error (__FILE__
, __LINE__
,
713 _("Can't be reached."));
715 result
= value_as_long (value_binop (val1
, val2
, binop
));
719 case DW_OP_GNU_push_tls_address
:
720 /* Variable is at a constant offset in the thread-local
721 storage block into the objfile for the current thread and
722 the dynamic linker module containing this expression. Here
723 we return returns the offset from that base. The top of the
724 stack has the offset from the beginning of the thread
725 control block at which the variable is located. Nothing
726 should follow this operator, so the top of stack would be
728 result
= dwarf_expr_fetch (ctx
, 0);
729 dwarf_expr_pop (ctx
);
730 result
= (ctx
->get_tls_address
) (ctx
->baton
, result
);
734 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
740 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
742 if (dwarf_expr_fetch (ctx
, 0) != 0)
744 dwarf_expr_pop (ctx
);
753 CORE_ADDR addr_or_regnum
;
755 /* Record the piece. */
756 op_ptr
= read_uleb128 (op_ptr
, op_end
, &size
);
757 addr_or_regnum
= dwarf_expr_fetch (ctx
, 0);
758 add_piece (ctx
, ctx
->in_reg
, addr_or_regnum
, size
);
760 /* Pop off the address/regnum, and clear the in_reg flag. */
761 dwarf_expr_pop (ctx
);
766 case DW_OP_GNU_uninit
:
767 if (op_ptr
!= op_end
)
768 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
769 "be the very last op."));
771 ctx
->initialized
= 0;
775 error (_("Unhandled dwarf expression opcode 0x%x"), op
);
778 /* Most things push a result value. */
779 dwarf_expr_push (ctx
, result
);
783 ctx
->recursion_depth
--;
784 gdb_assert (ctx
->recursion_depth
>= 0);