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
;
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
48 * sizeof (struct dwarf_stack_value
));
49 retval
->num_pieces
= 0;
51 retval
->max_recursion_depth
= 0x100;
55 /* Release the memory allocated to CTX. */
58 free_dwarf_expr_context (struct dwarf_expr_context
*ctx
)
65 /* Helper for make_cleanup_free_dwarf_expr_context. */
68 free_dwarf_expr_context_cleanup (void *arg
)
70 free_dwarf_expr_context (arg
);
73 /* Return a cleanup that calls free_dwarf_expr_context. */
76 make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context
*ctx
)
78 return make_cleanup (free_dwarf_expr_context_cleanup
, ctx
);
81 /* Expand the memory allocated to CTX's stack to contain at least
82 NEED more elements than are currently used. */
85 dwarf_expr_grow_stack (struct dwarf_expr_context
*ctx
, size_t need
)
87 if (ctx
->stack_len
+ need
> ctx
->stack_allocated
)
89 size_t newlen
= ctx
->stack_len
+ need
+ 10;
90 ctx
->stack
= xrealloc (ctx
->stack
,
91 newlen
* sizeof (struct dwarf_stack_value
));
92 ctx
->stack_allocated
= newlen
;
96 /* Push VALUE onto CTX's stack. */
99 dwarf_expr_push (struct dwarf_expr_context
*ctx
, CORE_ADDR value
,
102 struct dwarf_stack_value
*v
;
104 dwarf_expr_grow_stack (ctx
, 1);
105 v
= &ctx
->stack
[ctx
->stack_len
++];
107 v
->in_stack_memory
= in_stack_memory
;
110 /* Pop the top item off of CTX's stack. */
113 dwarf_expr_pop (struct dwarf_expr_context
*ctx
)
115 if (ctx
->stack_len
<= 0)
116 error (_("dwarf expression stack underflow"));
120 /* Retrieve the N'th item on CTX's stack. */
123 dwarf_expr_fetch (struct dwarf_expr_context
*ctx
, int n
)
125 if (ctx
->stack_len
<= n
)
126 error (_("Asked for position %d of stack, stack only has %d elements on it."),
128 return ctx
->stack
[ctx
->stack_len
- (1 + n
)].value
;
132 /* Retrieve the in_stack_memory flag of the N'th item on CTX's stack. */
135 dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context
*ctx
, int n
)
137 if (ctx
->stack_len
<= n
)
138 error (_("Asked for position %d of stack, stack only has %d elements on it."),
140 return ctx
->stack
[ctx
->stack_len
- (1 + n
)].in_stack_memory
;
144 /* Add a new piece to CTX's piece list. */
146 add_piece (struct dwarf_expr_context
*ctx
, ULONGEST size
)
148 struct dwarf_expr_piece
*p
;
153 ctx
->pieces
= xrealloc (ctx
->pieces
,
155 * sizeof (struct dwarf_expr_piece
)));
157 ctx
->pieces
= xmalloc (ctx
->num_pieces
158 * sizeof (struct dwarf_expr_piece
));
160 p
= &ctx
->pieces
[ctx
->num_pieces
- 1];
161 p
->location
= ctx
->location
;
163 if (p
->location
== DWARF_VALUE_LITERAL
)
165 p
->v
.literal
.data
= ctx
->data
;
166 p
->v
.literal
.length
= ctx
->len
;
170 p
->v
.expr
.value
= dwarf_expr_fetch (ctx
, 0);
171 p
->v
.expr
.in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 0);
175 /* Evaluate the expression at ADDR (LEN bytes long) using the context
179 dwarf_expr_eval (struct dwarf_expr_context
*ctx
, gdb_byte
*addr
, size_t len
)
181 int old_recursion_depth
= ctx
->recursion_depth
;
183 execute_stack_op (ctx
, addr
, addr
+ len
);
185 /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here. */
187 gdb_assert (ctx
->recursion_depth
== old_recursion_depth
);
190 /* Decode the unsigned 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_uleb128 (gdb_byte
*buf
, gdb_byte
*buf_end
, ULONGEST
* r
)
204 error (_("read_uleb128: Corrupted DWARF expression."));
207 result
|= (byte
& 0x7f) << shift
;
208 if ((byte
& 0x80) == 0)
216 /* Decode the signed LEB128 constant at BUF into the variable pointed to
217 by R, and return the new value of BUF. Verify that it doesn't extend
221 read_sleb128 (gdb_byte
*buf
, gdb_byte
*buf_end
, LONGEST
* r
)
230 error (_("read_sleb128: Corrupted DWARF expression."));
233 result
|= (byte
& 0x7f) << shift
;
235 if ((byte
& 0x80) == 0)
238 if (shift
< (sizeof (*r
) * 8) && (byte
& 0x40) != 0)
239 result
|= -(1 << shift
);
245 /* Read an address of size ADDR_SIZE from BUF, and verify that it
246 doesn't extend past BUF_END. */
249 dwarf2_read_address (struct gdbarch
*gdbarch
, gdb_byte
*buf
,
250 gdb_byte
*buf_end
, int addr_size
)
252 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
255 if (buf_end
- buf
< addr_size
)
256 error (_("dwarf2_read_address: Corrupted DWARF expression."));
258 /* For most architectures, calling extract_unsigned_integer() alone
259 is sufficient for extracting an address. However, some
260 architectures (e.g. MIPS) use signed addresses and using
261 extract_unsigned_integer() will not produce a correct
262 result. Make sure we invoke gdbarch_integer_to_address()
263 for those architectures which require it.
265 The use of `unsigned_address_type' in the code below refers to
266 the type of buf and has no bearing on the signedness of the
267 address being returned. */
269 if (gdbarch_integer_to_address_p (gdbarch
))
270 return gdbarch_integer_to_address
271 (gdbarch
, unsigned_address_type (gdbarch
, addr_size
), buf
);
273 return extract_unsigned_integer (buf
, addr_size
, byte_order
);
276 /* Return the type of an address of size ADDR_SIZE,
277 for unsigned arithmetic. */
280 unsigned_address_type (struct gdbarch
*gdbarch
, int addr_size
)
285 return builtin_type (gdbarch
)->builtin_uint16
;
287 return builtin_type (gdbarch
)->builtin_uint32
;
289 return builtin_type (gdbarch
)->builtin_uint64
;
291 internal_error (__FILE__
, __LINE__
,
292 _("Unsupported address size.\n"));
296 /* Return the type of an address of size ADDR_SIZE,
297 for signed arithmetic. */
300 signed_address_type (struct gdbarch
*gdbarch
, int addr_size
)
305 return builtin_type (gdbarch
)->builtin_int16
;
307 return builtin_type (gdbarch
)->builtin_int32
;
309 return builtin_type (gdbarch
)->builtin_int64
;
311 internal_error (__FILE__
, __LINE__
,
312 _("Unsupported address size.\n"));
317 /* Check that the current operator is either at the end of an
318 expression, or that it is followed by a composition operator. */
321 require_composition (gdb_byte
*op_ptr
, gdb_byte
*op_end
, const char *op_name
)
323 /* It seems like DW_OP_GNU_uninit should be handled here. However,
324 it doesn't seem to make sense for DW_OP_*_value, and it was not
325 checked at the other place that this function is called. */
326 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
&& *op_ptr
!= DW_OP_bit_piece
)
327 error (_("DWARF-2 expression error: `%s' operations must be "
328 "used either alone or in conjuction with DW_OP_piece "
329 "or DW_OP_bit_piece."),
333 /* The engine for the expression evaluator. Using the context in CTX,
334 evaluate the expression between OP_PTR and OP_END. */
337 execute_stack_op (struct dwarf_expr_context
*ctx
,
338 gdb_byte
*op_ptr
, gdb_byte
*op_end
)
340 enum bfd_endian byte_order
= gdbarch_byte_order (ctx
->gdbarch
);
341 ctx
->location
= DWARF_VALUE_MEMORY
;
342 ctx
->initialized
= 1; /* Default is initialized. */
344 if (ctx
->recursion_depth
> ctx
->max_recursion_depth
)
345 error (_("DWARF-2 expression error: Loop detected (%d)."),
346 ctx
->recursion_depth
);
347 ctx
->recursion_depth
++;
349 while (op_ptr
< op_end
)
351 enum dwarf_location_atom op
= *op_ptr
++;
353 /* Assume the value is not in stack memory.
354 Code that knows otherwise sets this to 1.
355 Some arithmetic on stack addresses can probably be assumed to still
356 be a stack address, but we skip this complication for now.
357 This is just an optimization, so it's always ok to punt
358 and leave this as 0. */
359 int in_stack_memory
= 0;
360 ULONGEST uoffset
, reg
;
397 result
= op
- DW_OP_lit0
;
401 result
= dwarf2_read_address (ctx
->gdbarch
,
402 op_ptr
, op_end
, ctx
->addr_size
);
403 op_ptr
+= ctx
->addr_size
;
407 result
= extract_unsigned_integer (op_ptr
, 1, byte_order
);
411 result
= extract_signed_integer (op_ptr
, 1, byte_order
);
415 result
= extract_unsigned_integer (op_ptr
, 2, byte_order
);
419 result
= extract_signed_integer (op_ptr
, 2, byte_order
);
423 result
= extract_unsigned_integer (op_ptr
, 4, byte_order
);
427 result
= extract_signed_integer (op_ptr
, 4, byte_order
);
431 result
= extract_unsigned_integer (op_ptr
, 8, byte_order
);
435 result
= extract_signed_integer (op_ptr
, 8, byte_order
);
439 op_ptr
= read_uleb128 (op_ptr
, op_end
, &uoffset
);
443 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
447 /* The DW_OP_reg operations are required to occur alone in
448 location expressions. */
482 && *op_ptr
!= DW_OP_piece
483 && *op_ptr
!= DW_OP_GNU_uninit
)
484 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
485 "used either alone or in conjuction with DW_OP_piece."));
487 result
= op
- DW_OP_reg0
;
488 ctx
->location
= DWARF_VALUE_REGISTER
;
492 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
493 require_composition (op_ptr
, op_end
, "DW_OP_regx");
496 ctx
->location
= DWARF_VALUE_REGISTER
;
499 case DW_OP_implicit_value
:
502 op_ptr
= read_uleb128 (op_ptr
, op_end
, &len
);
503 if (op_ptr
+ len
> op_end
)
504 error (_("DW_OP_implicit_value: too few bytes available."));
507 ctx
->location
= DWARF_VALUE_LITERAL
;
509 require_composition (op_ptr
, op_end
, "DW_OP_implicit_value");
513 case DW_OP_stack_value
:
514 ctx
->location
= DWARF_VALUE_STACK
;
515 require_composition (op_ptr
, op_end
, "DW_OP_stack_value");
551 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
552 result
= (ctx
->read_reg
) (ctx
->baton
, op
- DW_OP_breg0
);
558 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
559 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
560 result
= (ctx
->read_reg
) (ctx
->baton
, reg
);
568 unsigned int before_stack_len
;
570 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
571 /* Rather than create a whole new context, we simply
572 record the stack length before execution, then reset it
573 afterwards, effectively erasing whatever the recursive
575 before_stack_len
= ctx
->stack_len
;
576 /* FIXME: cagney/2003-03-26: This code should be using
577 get_frame_base_address(), and then implement a dwarf2
578 specific this_base method. */
579 (ctx
->get_frame_base
) (ctx
->baton
, &datastart
, &datalen
);
580 dwarf_expr_eval (ctx
, datastart
, datalen
);
581 if (ctx
->location
== DWARF_VALUE_LITERAL
582 || ctx
->location
== DWARF_VALUE_STACK
)
583 error (_("Not implemented: computing frame base using explicit value operator"));
584 result
= dwarf_expr_fetch (ctx
, 0);
585 if (ctx
->location
== DWARF_VALUE_REGISTER
)
586 result
= (ctx
->read_reg
) (ctx
->baton
, result
);
587 result
= result
+ offset
;
589 ctx
->stack_len
= before_stack_len
;
590 ctx
->location
= DWARF_VALUE_MEMORY
;
595 result
= dwarf_expr_fetch (ctx
, 0);
596 in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 0);
600 dwarf_expr_pop (ctx
);
605 result
= dwarf_expr_fetch (ctx
, offset
);
606 in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, offset
);
611 struct dwarf_stack_value t1
, t2
;
613 if (ctx
->stack_len
< 2)
614 error (_("Not enough elements for DW_OP_swap. Need 2, have %d."),
616 t1
= ctx
->stack
[ctx
->stack_len
- 1];
617 t2
= ctx
->stack
[ctx
->stack_len
- 2];
618 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
619 ctx
->stack
[ctx
->stack_len
- 2] = t1
;
624 result
= dwarf_expr_fetch (ctx
, 1);
625 in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 1);
630 struct dwarf_stack_value t1
, t2
, t3
;
632 if (ctx
->stack_len
< 3)
633 error (_("Not enough elements for DW_OP_rot. Need 3, have %d."),
635 t1
= ctx
->stack
[ctx
->stack_len
- 1];
636 t2
= ctx
->stack
[ctx
->stack_len
- 2];
637 t3
= ctx
->stack
[ctx
->stack_len
- 3];
638 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
639 ctx
->stack
[ctx
->stack_len
- 2] = t3
;
640 ctx
->stack
[ctx
->stack_len
- 3] = t1
;
645 case DW_OP_deref_size
:
649 case DW_OP_plus_uconst
:
650 /* Unary operations. */
651 result
= dwarf_expr_fetch (ctx
, 0);
652 dwarf_expr_pop (ctx
);
658 gdb_byte
*buf
= alloca (ctx
->addr_size
);
659 (ctx
->read_mem
) (ctx
->baton
, buf
, result
, ctx
->addr_size
);
660 result
= dwarf2_read_address (ctx
->gdbarch
,
661 buf
, buf
+ ctx
->addr_size
,
666 case DW_OP_deref_size
:
668 int addr_size
= *op_ptr
++;
669 gdb_byte
*buf
= alloca (addr_size
);
670 (ctx
->read_mem
) (ctx
->baton
, buf
, result
, addr_size
);
671 result
= dwarf2_read_address (ctx
->gdbarch
,
672 buf
, buf
+ addr_size
,
678 if ((signed int) result
< 0)
687 case DW_OP_plus_uconst
:
688 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
712 /* Binary operations. Use the value engine to do computations in
714 CORE_ADDR first
, second
;
715 enum exp_opcode binop
;
716 struct value
*val1
= NULL
, *val2
= NULL
;
717 struct type
*stype
, *utype
;
719 second
= dwarf_expr_fetch (ctx
, 0);
720 dwarf_expr_pop (ctx
);
722 first
= dwarf_expr_fetch (ctx
, 0);
723 dwarf_expr_pop (ctx
);
725 utype
= unsigned_address_type (ctx
->gdbarch
, ctx
->addr_size
);
726 stype
= signed_address_type (ctx
->gdbarch
, ctx
->addr_size
);
731 binop
= BINOP_BITWISE_AND
;
735 val1
= value_from_longest (stype
, first
);
736 val2
= value_from_longest (stype
, second
);
748 binop
= BINOP_BITWISE_IOR
;
761 val1
= value_from_longest (stype
, first
);
764 binop
= BINOP_BITWISE_XOR
;
768 val1
= value_from_longest (stype
, first
);
769 val2
= value_from_longest (stype
, second
);
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
);
792 binop
= BINOP_NOTEQUAL
;
793 val1
= value_from_longest (stype
, first
);
794 val2
= value_from_longest (stype
, second
);
797 internal_error (__FILE__
, __LINE__
,
798 _("Can't be reached."));
801 /* We use unsigned operands by default. */
803 val1
= value_from_longest (utype
, first
);
805 val2
= value_from_longest (utype
, second
);
807 result
= value_as_long (value_binop (val1
, val2
, binop
));
811 case DW_OP_call_frame_cfa
:
812 result
= (ctx
->get_frame_cfa
) (ctx
->baton
);
816 case DW_OP_GNU_push_tls_address
:
817 /* Variable is at a constant offset in the thread-local
818 storage block into the objfile for the current thread and
819 the dynamic linker module containing this expression. Here
820 we return returns the offset from that base. The top of the
821 stack has the offset from the beginning of the thread
822 control block at which the variable is located. Nothing
823 should follow this operator, so the top of stack would be
825 result
= dwarf_expr_fetch (ctx
, 0);
826 dwarf_expr_pop (ctx
);
827 result
= (ctx
->get_tls_address
) (ctx
->baton
, result
);
831 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
837 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
839 if (dwarf_expr_fetch (ctx
, 0) != 0)
841 dwarf_expr_pop (ctx
);
851 /* Record the piece. */
852 op_ptr
= read_uleb128 (op_ptr
, op_end
, &size
);
853 add_piece (ctx
, size
);
855 /* Pop off the address/regnum, and reset the location
857 if (ctx
->location
!= DWARF_VALUE_LITERAL
)
858 dwarf_expr_pop (ctx
);
859 ctx
->location
= DWARF_VALUE_MEMORY
;
863 case DW_OP_GNU_uninit
:
864 if (op_ptr
!= op_end
)
865 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
866 "be the very last op."));
868 ctx
->initialized
= 0;
872 error (_("Unhandled dwarf expression opcode 0x%x"), op
);
875 /* Most things push a result value. */
876 dwarf_expr_push (ctx
, result
, in_stack_memory
);
880 ctx
->recursion_depth
--;
881 gdb_assert (ctx
->recursion_depth
>= 0);