1 /* DWARF 2 Expression Evaluator.
3 Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2010
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
;
45 retval
= xcalloc (1, sizeof (struct dwarf_expr_context
));
46 retval
->stack_len
= 0;
47 retval
->stack_allocated
= 10;
48 retval
->stack
= xmalloc (retval
->stack_allocated
49 * sizeof (struct dwarf_stack_value
));
50 retval
->num_pieces
= 0;
52 retval
->max_recursion_depth
= 0x100;
56 /* Release the memory allocated to CTX. */
59 free_dwarf_expr_context (struct dwarf_expr_context
*ctx
)
66 /* Helper for make_cleanup_free_dwarf_expr_context. */
69 free_dwarf_expr_context_cleanup (void *arg
)
71 free_dwarf_expr_context (arg
);
74 /* Return a cleanup that calls free_dwarf_expr_context. */
77 make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context
*ctx
)
79 return make_cleanup (free_dwarf_expr_context_cleanup
, ctx
);
82 /* Expand the memory allocated to CTX's stack to contain at least
83 NEED more elements than are currently used. */
86 dwarf_expr_grow_stack (struct dwarf_expr_context
*ctx
, size_t need
)
88 if (ctx
->stack_len
+ need
> ctx
->stack_allocated
)
90 size_t newlen
= ctx
->stack_len
+ need
+ 10;
92 ctx
->stack
= xrealloc (ctx
->stack
,
93 newlen
* sizeof (struct dwarf_stack_value
));
94 ctx
->stack_allocated
= newlen
;
98 /* Push VALUE onto CTX's stack. */
101 dwarf_expr_push (struct dwarf_expr_context
*ctx
, CORE_ADDR value
,
104 struct dwarf_stack_value
*v
;
106 dwarf_expr_grow_stack (ctx
, 1);
107 v
= &ctx
->stack
[ctx
->stack_len
++];
109 v
->in_stack_memory
= in_stack_memory
;
112 /* Pop the top item off of CTX's stack. */
115 dwarf_expr_pop (struct dwarf_expr_context
*ctx
)
117 if (ctx
->stack_len
<= 0)
118 error (_("dwarf expression stack underflow"));
122 /* Retrieve the N'th item on CTX's stack. */
125 dwarf_expr_fetch (struct dwarf_expr_context
*ctx
, int n
)
127 if (ctx
->stack_len
<= n
)
128 error (_("Asked for position %d of stack, stack only has %d elements on it."),
130 return ctx
->stack
[ctx
->stack_len
- (1 + n
)].value
;
134 /* Retrieve the in_stack_memory flag of the N'th item on CTX's stack. */
137 dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context
*ctx
, int n
)
139 if (ctx
->stack_len
<= n
)
140 error (_("Asked for position %d of stack, stack only has %d elements on it."),
142 return ctx
->stack
[ctx
->stack_len
- (1 + n
)].in_stack_memory
;
146 /* Add a new piece to CTX's piece list. */
148 add_piece (struct dwarf_expr_context
*ctx
, ULONGEST size
)
150 struct dwarf_expr_piece
*p
;
155 ctx
->pieces
= xrealloc (ctx
->pieces
,
157 * sizeof (struct dwarf_expr_piece
)));
159 ctx
->pieces
= xmalloc (ctx
->num_pieces
160 * sizeof (struct dwarf_expr_piece
));
162 p
= &ctx
->pieces
[ctx
->num_pieces
- 1];
163 p
->location
= ctx
->location
;
165 if (p
->location
== DWARF_VALUE_LITERAL
)
167 p
->v
.literal
.data
= ctx
->data
;
168 p
->v
.literal
.length
= ctx
->len
;
172 p
->v
.expr
.value
= dwarf_expr_fetch (ctx
, 0);
173 p
->v
.expr
.in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 0);
177 /* Evaluate the expression at ADDR (LEN bytes long) using the context
181 dwarf_expr_eval (struct dwarf_expr_context
*ctx
, gdb_byte
*addr
, size_t len
)
183 int old_recursion_depth
= ctx
->recursion_depth
;
185 execute_stack_op (ctx
, addr
, addr
+ len
);
187 /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here. */
189 gdb_assert (ctx
->recursion_depth
== old_recursion_depth
);
192 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
193 by R, and return the new value of BUF. Verify that it doesn't extend
197 read_uleb128 (gdb_byte
*buf
, gdb_byte
*buf_end
, ULONGEST
* r
)
206 error (_("read_uleb128: Corrupted DWARF expression."));
209 result
|= (byte
& 0x7f) << shift
;
210 if ((byte
& 0x80) == 0)
218 /* Decode the signed LEB128 constant at BUF into the variable pointed to
219 by R, and return the new value of BUF. Verify that it doesn't extend
223 read_sleb128 (gdb_byte
*buf
, gdb_byte
*buf_end
, LONGEST
* r
)
232 error (_("read_sleb128: Corrupted DWARF expression."));
235 result
|= (byte
& 0x7f) << shift
;
237 if ((byte
& 0x80) == 0)
240 if (shift
< (sizeof (*r
) * 8) && (byte
& 0x40) != 0)
241 result
|= -(1 << shift
);
247 /* Read an address of size ADDR_SIZE from BUF, and verify that it
248 doesn't extend past BUF_END. */
251 dwarf2_read_address (struct gdbarch
*gdbarch
, gdb_byte
*buf
,
252 gdb_byte
*buf_end
, int addr_size
)
254 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
256 if (buf_end
- buf
< addr_size
)
257 error (_("dwarf2_read_address: Corrupted DWARF expression."));
259 /* For most architectures, calling extract_unsigned_integer() alone
260 is sufficient for extracting an address. However, some
261 architectures (e.g. MIPS) use signed addresses and using
262 extract_unsigned_integer() will not produce a correct
263 result. Make sure we invoke gdbarch_integer_to_address()
264 for those architectures which require it.
266 The use of `unsigned_address_type' in the code below refers to
267 the type of buf and has no bearing on the signedness of the
268 address being returned. */
270 if (gdbarch_integer_to_address_p (gdbarch
))
271 return gdbarch_integer_to_address
272 (gdbarch
, unsigned_address_type (gdbarch
, addr_size
), buf
);
274 return extract_unsigned_integer (buf
, addr_size
, byte_order
);
277 /* Return the type of an address of size ADDR_SIZE,
278 for unsigned arithmetic. */
281 unsigned_address_type (struct gdbarch
*gdbarch
, int addr_size
)
286 return builtin_type (gdbarch
)->builtin_uint16
;
288 return builtin_type (gdbarch
)->builtin_uint32
;
290 return builtin_type (gdbarch
)->builtin_uint64
;
292 internal_error (__FILE__
, __LINE__
,
293 _("Unsupported address size.\n"));
297 /* Return the type of an address of size ADDR_SIZE,
298 for signed arithmetic. */
301 signed_address_type (struct gdbarch
*gdbarch
, int addr_size
)
306 return builtin_type (gdbarch
)->builtin_int16
;
308 return builtin_type (gdbarch
)->builtin_int32
;
310 return builtin_type (gdbarch
)->builtin_int64
;
312 internal_error (__FILE__
, __LINE__
,
313 _("Unsupported address size.\n"));
318 /* Check that the current operator is either at the end of an
319 expression, or that it is followed by a composition operator. */
322 require_composition (gdb_byte
*op_ptr
, gdb_byte
*op_end
, const char *op_name
)
324 /* It seems like DW_OP_GNU_uninit should be handled here. However,
325 it doesn't seem to make sense for DW_OP_*_value, and it was not
326 checked at the other place that this function is called. */
327 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
&& *op_ptr
!= DW_OP_bit_piece
)
328 error (_("DWARF-2 expression error: `%s' operations must be "
329 "used either alone or in conjuction with DW_OP_piece "
330 "or DW_OP_bit_piece."),
334 /* The engine for the expression evaluator. Using the context in CTX,
335 evaluate the expression between OP_PTR and OP_END. */
338 execute_stack_op (struct dwarf_expr_context
*ctx
,
339 gdb_byte
*op_ptr
, gdb_byte
*op_end
)
341 enum bfd_endian byte_order
= gdbarch_byte_order (ctx
->gdbarch
);
343 ctx
->location
= DWARF_VALUE_MEMORY
;
344 ctx
->initialized
= 1; /* Default is initialized. */
346 if (ctx
->recursion_depth
> ctx
->max_recursion_depth
)
347 error (_("DWARF-2 expression error: Loop detected (%d)."),
348 ctx
->recursion_depth
);
349 ctx
->recursion_depth
++;
351 while (op_ptr
< op_end
)
353 enum dwarf_location_atom op
= *op_ptr
++;
355 /* Assume the value is not in stack memory.
356 Code that knows otherwise sets this to 1.
357 Some arithmetic on stack addresses can probably be assumed to still
358 be a stack address, but we skip this complication for now.
359 This is just an optimization, so it's always ok to punt
360 and leave this as 0. */
361 int in_stack_memory
= 0;
362 ULONGEST uoffset
, reg
;
399 result
= op
- DW_OP_lit0
;
403 result
= dwarf2_read_address (ctx
->gdbarch
,
404 op_ptr
, op_end
, ctx
->addr_size
);
405 op_ptr
+= ctx
->addr_size
;
409 result
= extract_unsigned_integer (op_ptr
, 1, byte_order
);
413 result
= extract_signed_integer (op_ptr
, 1, byte_order
);
417 result
= extract_unsigned_integer (op_ptr
, 2, byte_order
);
421 result
= extract_signed_integer (op_ptr
, 2, byte_order
);
425 result
= extract_unsigned_integer (op_ptr
, 4, byte_order
);
429 result
= extract_signed_integer (op_ptr
, 4, byte_order
);
433 result
= extract_unsigned_integer (op_ptr
, 8, byte_order
);
437 result
= extract_signed_integer (op_ptr
, 8, byte_order
);
441 op_ptr
= read_uleb128 (op_ptr
, op_end
, &uoffset
);
445 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
449 /* The DW_OP_reg operations are required to occur alone in
450 location expressions. */
484 && *op_ptr
!= DW_OP_piece
485 && *op_ptr
!= DW_OP_GNU_uninit
)
486 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
487 "used either alone or in conjuction with DW_OP_piece."));
489 result
= op
- DW_OP_reg0
;
490 ctx
->location
= DWARF_VALUE_REGISTER
;
494 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
495 require_composition (op_ptr
, op_end
, "DW_OP_regx");
498 ctx
->location
= DWARF_VALUE_REGISTER
;
501 case DW_OP_implicit_value
:
505 op_ptr
= read_uleb128 (op_ptr
, op_end
, &len
);
506 if (op_ptr
+ len
> op_end
)
507 error (_("DW_OP_implicit_value: too few bytes available."));
510 ctx
->location
= DWARF_VALUE_LITERAL
;
512 require_composition (op_ptr
, op_end
, "DW_OP_implicit_value");
516 case DW_OP_stack_value
:
517 ctx
->location
= DWARF_VALUE_STACK
;
518 require_composition (op_ptr
, op_end
, "DW_OP_stack_value");
554 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
555 result
= (ctx
->read_reg
) (ctx
->baton
, op
- DW_OP_breg0
);
561 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
562 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
563 result
= (ctx
->read_reg
) (ctx
->baton
, reg
);
571 unsigned int before_stack_len
;
573 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
574 /* Rather than create a whole new context, we simply
575 record the stack length before execution, then reset it
576 afterwards, effectively erasing whatever the recursive
578 before_stack_len
= ctx
->stack_len
;
579 /* FIXME: cagney/2003-03-26: This code should be using
580 get_frame_base_address(), and then implement a dwarf2
581 specific this_base method. */
582 (ctx
->get_frame_base
) (ctx
->baton
, &datastart
, &datalen
);
583 dwarf_expr_eval (ctx
, datastart
, datalen
);
584 if (ctx
->location
== DWARF_VALUE_LITERAL
585 || ctx
->location
== DWARF_VALUE_STACK
)
586 error (_("Not implemented: computing frame base using explicit value operator"));
587 result
= dwarf_expr_fetch (ctx
, 0);
588 if (ctx
->location
== DWARF_VALUE_REGISTER
)
589 result
= (ctx
->read_reg
) (ctx
->baton
, result
);
590 result
= result
+ offset
;
592 ctx
->stack_len
= before_stack_len
;
593 ctx
->location
= DWARF_VALUE_MEMORY
;
598 result
= dwarf_expr_fetch (ctx
, 0);
599 in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 0);
603 dwarf_expr_pop (ctx
);
608 result
= dwarf_expr_fetch (ctx
, offset
);
609 in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, offset
);
614 struct dwarf_stack_value t1
, t2
;
616 if (ctx
->stack_len
< 2)
617 error (_("Not enough elements for DW_OP_swap. Need 2, have %d."),
619 t1
= ctx
->stack
[ctx
->stack_len
- 1];
620 t2
= ctx
->stack
[ctx
->stack_len
- 2];
621 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
622 ctx
->stack
[ctx
->stack_len
- 2] = t1
;
627 result
= dwarf_expr_fetch (ctx
, 1);
628 in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 1);
633 struct dwarf_stack_value t1
, t2
, t3
;
635 if (ctx
->stack_len
< 3)
636 error (_("Not enough elements for DW_OP_rot. Need 3, have %d."),
638 t1
= ctx
->stack
[ctx
->stack_len
- 1];
639 t2
= ctx
->stack
[ctx
->stack_len
- 2];
640 t3
= ctx
->stack
[ctx
->stack_len
- 3];
641 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
642 ctx
->stack
[ctx
->stack_len
- 2] = t3
;
643 ctx
->stack
[ctx
->stack_len
- 3] = t1
;
648 case DW_OP_deref_size
:
652 case DW_OP_plus_uconst
:
653 /* Unary operations. */
654 result
= dwarf_expr_fetch (ctx
, 0);
655 dwarf_expr_pop (ctx
);
661 gdb_byte
*buf
= alloca (ctx
->addr_size
);
663 (ctx
->read_mem
) (ctx
->baton
, buf
, result
, ctx
->addr_size
);
664 result
= dwarf2_read_address (ctx
->gdbarch
,
665 buf
, buf
+ ctx
->addr_size
,
670 case DW_OP_deref_size
:
672 int addr_size
= *op_ptr
++;
673 gdb_byte
*buf
= alloca (addr_size
);
675 (ctx
->read_mem
) (ctx
->baton
, buf
, result
, addr_size
);
676 result
= dwarf2_read_address (ctx
->gdbarch
,
677 buf
, buf
+ addr_size
,
683 if ((signed int) result
< 0)
692 case DW_OP_plus_uconst
:
693 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
717 /* Binary operations. Use the value engine to do computations in
719 CORE_ADDR first
, second
;
720 enum exp_opcode binop
;
721 struct value
*val1
= NULL
, *val2
= NULL
;
722 struct type
*stype
, *utype
;
724 second
= dwarf_expr_fetch (ctx
, 0);
725 dwarf_expr_pop (ctx
);
727 first
= dwarf_expr_fetch (ctx
, 0);
728 dwarf_expr_pop (ctx
);
730 utype
= unsigned_address_type (ctx
->gdbarch
, ctx
->addr_size
);
731 stype
= signed_address_type (ctx
->gdbarch
, ctx
->addr_size
);
736 binop
= BINOP_BITWISE_AND
;
740 val1
= value_from_longest (stype
, first
);
741 val2
= value_from_longest (stype
, second
);
753 binop
= BINOP_BITWISE_IOR
;
766 val1
= value_from_longest (stype
, first
);
769 binop
= BINOP_BITWISE_XOR
;
773 val1
= value_from_longest (stype
, first
);
774 val2
= value_from_longest (stype
, second
);
778 val1
= value_from_longest (stype
, first
);
779 val2
= value_from_longest (stype
, second
);
783 val1
= value_from_longest (stype
, first
);
784 val2
= value_from_longest (stype
, second
);
788 val1
= value_from_longest (stype
, first
);
789 val2
= value_from_longest (stype
, second
);
793 val1
= value_from_longest (stype
, first
);
794 val2
= value_from_longest (stype
, second
);
797 binop
= BINOP_NOTEQUAL
;
798 val1
= value_from_longest (stype
, first
);
799 val2
= value_from_longest (stype
, second
);
802 internal_error (__FILE__
, __LINE__
,
803 _("Can't be reached."));
806 /* We use unsigned operands by default. */
808 val1
= value_from_longest (utype
, first
);
810 val2
= value_from_longest (utype
, second
);
812 result
= value_as_long (value_binop (val1
, val2
, binop
));
816 case DW_OP_call_frame_cfa
:
817 result
= (ctx
->get_frame_cfa
) (ctx
->baton
);
821 case DW_OP_GNU_push_tls_address
:
822 /* Variable is at a constant offset in the thread-local
823 storage block into the objfile for the current thread and
824 the dynamic linker module containing this expression. Here
825 we return returns the offset from that base. The top of the
826 stack has the offset from the beginning of the thread
827 control block at which the variable is located. Nothing
828 should follow this operator, so the top of stack would be
830 result
= dwarf_expr_fetch (ctx
, 0);
831 dwarf_expr_pop (ctx
);
832 result
= (ctx
->get_tls_address
) (ctx
->baton
, result
);
836 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
842 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
844 if (dwarf_expr_fetch (ctx
, 0) != 0)
846 dwarf_expr_pop (ctx
);
856 /* Record the piece. */
857 op_ptr
= read_uleb128 (op_ptr
, op_end
, &size
);
858 add_piece (ctx
, size
);
860 /* Pop off the address/regnum, and reset the location
862 if (ctx
->location
!= DWARF_VALUE_LITERAL
)
863 dwarf_expr_pop (ctx
);
864 ctx
->location
= DWARF_VALUE_MEMORY
;
868 case DW_OP_GNU_uninit
:
869 if (op_ptr
!= op_end
)
870 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
871 "be the very last op."));
873 ctx
->initialized
= 0;
877 error (_("Unhandled dwarf expression opcode 0x%x"), op
);
880 /* Most things push a result value. */
881 dwarf_expr_push (ctx
, result
, in_stack_memory
);
885 ctx
->recursion_depth
--;
886 gdb_assert (ctx
->recursion_depth
>= 0);