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 (struct dwarf_stack_value
));
91 ctx
->stack_allocated
= newlen
;
95 /* Push VALUE onto CTX's stack. */
98 dwarf_expr_push (struct dwarf_expr_context
*ctx
, CORE_ADDR value
,
101 struct dwarf_stack_value
*v
;
103 dwarf_expr_grow_stack (ctx
, 1);
104 v
= &ctx
->stack
[ctx
->stack_len
++];
106 v
->in_stack_memory
= in_stack_memory
;
109 /* Pop the top item off of CTX's stack. */
112 dwarf_expr_pop (struct dwarf_expr_context
*ctx
)
114 if (ctx
->stack_len
<= 0)
115 error (_("dwarf expression stack underflow"));
119 /* Retrieve the N'th item on CTX's stack. */
122 dwarf_expr_fetch (struct dwarf_expr_context
*ctx
, int n
)
124 if (ctx
->stack_len
<= n
)
125 error (_("Asked for position %d of stack, stack only has %d elements on it."),
127 return ctx
->stack
[ctx
->stack_len
- (1 + n
)].value
;
131 /* Retrieve the in_stack_memory flag of the N'th item on CTX's stack. */
134 dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context
*ctx
, int n
)
136 if (ctx
->stack_len
<= n
)
137 error (_("Asked for position %d of stack, stack only has %d elements on it."),
139 return ctx
->stack
[ctx
->stack_len
- (1 + n
)].in_stack_memory
;
143 /* Add a new piece to CTX's piece list. */
145 add_piece (struct dwarf_expr_context
*ctx
, ULONGEST size
)
147 struct dwarf_expr_piece
*p
;
152 ctx
->pieces
= xrealloc (ctx
->pieces
,
154 * sizeof (struct dwarf_expr_piece
)));
156 ctx
->pieces
= xmalloc (ctx
->num_pieces
157 * sizeof (struct dwarf_expr_piece
));
159 p
= &ctx
->pieces
[ctx
->num_pieces
- 1];
160 p
->location
= ctx
->location
;
162 if (p
->location
== DWARF_VALUE_LITERAL
)
164 p
->v
.literal
.data
= ctx
->data
;
165 p
->v
.literal
.length
= ctx
->len
;
169 p
->v
.expr
.value
= dwarf_expr_fetch (ctx
, 0);
170 p
->v
.expr
.in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 0);
174 /* Evaluate the expression at ADDR (LEN bytes long) using the context
178 dwarf_expr_eval (struct dwarf_expr_context
*ctx
, gdb_byte
*addr
, size_t len
)
180 int old_recursion_depth
= ctx
->recursion_depth
;
182 execute_stack_op (ctx
, addr
, addr
+ len
);
184 /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here. */
186 gdb_assert (ctx
->recursion_depth
== old_recursion_depth
);
189 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
190 by R, and return the new value of BUF. Verify that it doesn't extend
194 read_uleb128 (gdb_byte
*buf
, gdb_byte
*buf_end
, ULONGEST
* r
)
203 error (_("read_uleb128: Corrupted DWARF expression."));
206 result
|= (byte
& 0x7f) << shift
;
207 if ((byte
& 0x80) == 0)
215 /* Decode the signed LEB128 constant at BUF into the variable pointed to
216 by R, and return the new value of BUF. Verify that it doesn't extend
220 read_sleb128 (gdb_byte
*buf
, gdb_byte
*buf_end
, LONGEST
* r
)
229 error (_("read_sleb128: Corrupted DWARF expression."));
232 result
|= (byte
& 0x7f) << shift
;
234 if ((byte
& 0x80) == 0)
237 if (shift
< (sizeof (*r
) * 8) && (byte
& 0x40) != 0)
238 result
|= -(1 << shift
);
244 /* Read an address of size ADDR_SIZE from BUF, and verify that it
245 doesn't extend past BUF_END. */
248 dwarf2_read_address (struct gdbarch
*gdbarch
, gdb_byte
*buf
,
249 gdb_byte
*buf_end
, int addr_size
)
251 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
254 if (buf_end
- buf
< addr_size
)
255 error (_("dwarf2_read_address: Corrupted DWARF expression."));
257 /* For most architectures, calling extract_unsigned_integer() alone
258 is sufficient for extracting an address. However, some
259 architectures (e.g. MIPS) use signed addresses and using
260 extract_unsigned_integer() will not produce a correct
261 result. Make sure we invoke gdbarch_integer_to_address()
262 for those architectures which require it.
264 The use of `unsigned_address_type' in the code below refers to
265 the type of buf and has no bearing on the signedness of the
266 address being returned. */
268 if (gdbarch_integer_to_address_p (gdbarch
))
269 return gdbarch_integer_to_address
270 (gdbarch
, unsigned_address_type (gdbarch
, addr_size
), buf
);
272 return extract_unsigned_integer (buf
, addr_size
, byte_order
);
275 /* Return the type of an address of size ADDR_SIZE,
276 for unsigned arithmetic. */
279 unsigned_address_type (struct gdbarch
*gdbarch
, int addr_size
)
284 return builtin_type (gdbarch
)->builtin_uint16
;
286 return builtin_type (gdbarch
)->builtin_uint32
;
288 return builtin_type (gdbarch
)->builtin_uint64
;
290 internal_error (__FILE__
, __LINE__
,
291 _("Unsupported address size.\n"));
295 /* Return the type of an address of size ADDR_SIZE,
296 for signed arithmetic. */
299 signed_address_type (struct gdbarch
*gdbarch
, int addr_size
)
304 return builtin_type (gdbarch
)->builtin_int16
;
306 return builtin_type (gdbarch
)->builtin_int32
;
308 return builtin_type (gdbarch
)->builtin_int64
;
310 internal_error (__FILE__
, __LINE__
,
311 _("Unsupported address size.\n"));
316 /* Check that the current operator is either at the end of an
317 expression, or that it is followed by a composition operator. */
320 require_composition (gdb_byte
*op_ptr
, gdb_byte
*op_end
, const char *op_name
)
322 /* It seems like DW_OP_GNU_uninit should be handled here. However,
323 it doesn't seem to make sense for DW_OP_*_value, and it was not
324 checked at the other place that this function is called. */
325 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
&& *op_ptr
!= DW_OP_bit_piece
)
326 error (_("DWARF-2 expression error: `%s' operations must be "
327 "used either alone or in conjuction with DW_OP_piece "
328 "or DW_OP_bit_piece."),
332 /* The engine for the expression evaluator. Using the context in CTX,
333 evaluate the expression between OP_PTR and OP_END. */
336 execute_stack_op (struct dwarf_expr_context
*ctx
,
337 gdb_byte
*op_ptr
, gdb_byte
*op_end
)
339 enum bfd_endian byte_order
= gdbarch_byte_order (ctx
->gdbarch
);
340 ctx
->location
= DWARF_VALUE_MEMORY
;
341 ctx
->initialized
= 1; /* Default is initialized. */
343 if (ctx
->recursion_depth
> ctx
->max_recursion_depth
)
344 error (_("DWARF-2 expression error: Loop detected (%d)."),
345 ctx
->recursion_depth
);
346 ctx
->recursion_depth
++;
348 while (op_ptr
< op_end
)
350 enum dwarf_location_atom op
= *op_ptr
++;
352 /* Assume the value is not in stack memory.
353 Code that knows otherwise sets this to 1.
354 Some arithmetic on stack addresses can probably be assumed to still
355 be a stack address, but we skip this complication for now.
356 This is just an optimization, so it's always ok to punt
357 and leave this as 0. */
358 int in_stack_memory
= 0;
359 ULONGEST uoffset
, reg
;
396 result
= op
- DW_OP_lit0
;
400 result
= dwarf2_read_address (ctx
->gdbarch
,
401 op_ptr
, op_end
, ctx
->addr_size
);
402 op_ptr
+= ctx
->addr_size
;
406 result
= extract_unsigned_integer (op_ptr
, 1, byte_order
);
410 result
= extract_signed_integer (op_ptr
, 1, byte_order
);
414 result
= extract_unsigned_integer (op_ptr
, 2, byte_order
);
418 result
= extract_signed_integer (op_ptr
, 2, byte_order
);
422 result
= extract_unsigned_integer (op_ptr
, 4, byte_order
);
426 result
= extract_signed_integer (op_ptr
, 4, byte_order
);
430 result
= extract_unsigned_integer (op_ptr
, 8, byte_order
);
434 result
= extract_signed_integer (op_ptr
, 8, byte_order
);
438 op_ptr
= read_uleb128 (op_ptr
, op_end
, &uoffset
);
442 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
446 /* The DW_OP_reg operations are required to occur alone in
447 location expressions. */
481 && *op_ptr
!= DW_OP_piece
482 && *op_ptr
!= DW_OP_GNU_uninit
)
483 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
484 "used either alone or in conjuction with DW_OP_piece."));
486 result
= op
- DW_OP_reg0
;
487 ctx
->location
= DWARF_VALUE_REGISTER
;
491 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
492 require_composition (op_ptr
, op_end
, "DW_OP_regx");
495 ctx
->location
= DWARF_VALUE_REGISTER
;
498 case DW_OP_implicit_value
:
501 op_ptr
= read_uleb128 (op_ptr
, op_end
, &len
);
502 if (op_ptr
+ len
> op_end
)
503 error (_("DW_OP_implicit_value: too few bytes available."));
506 ctx
->location
= DWARF_VALUE_LITERAL
;
508 require_composition (op_ptr
, op_end
, "DW_OP_implicit_value");
512 case DW_OP_stack_value
:
513 ctx
->location
= DWARF_VALUE_STACK
;
514 require_composition (op_ptr
, op_end
, "DW_OP_stack_value");
550 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
551 result
= (ctx
->read_reg
) (ctx
->baton
, op
- DW_OP_breg0
);
557 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
558 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
559 result
= (ctx
->read_reg
) (ctx
->baton
, reg
);
567 unsigned int before_stack_len
;
569 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
570 /* Rather than create a whole new context, we simply
571 record the stack length before execution, then reset it
572 afterwards, effectively erasing whatever the recursive
574 before_stack_len
= ctx
->stack_len
;
575 /* FIXME: cagney/2003-03-26: This code should be using
576 get_frame_base_address(), and then implement a dwarf2
577 specific this_base method. */
578 (ctx
->get_frame_base
) (ctx
->baton
, &datastart
, &datalen
);
579 dwarf_expr_eval (ctx
, datastart
, datalen
);
580 if (ctx
->location
== DWARF_VALUE_LITERAL
581 || ctx
->location
== DWARF_VALUE_STACK
)
582 error (_("Not implemented: computing frame base using explicit value operator"));
583 result
= dwarf_expr_fetch (ctx
, 0);
584 if (ctx
->location
== DWARF_VALUE_REGISTER
)
585 result
= (ctx
->read_reg
) (ctx
->baton
, result
);
586 result
= result
+ offset
;
588 ctx
->stack_len
= before_stack_len
;
589 ctx
->location
= DWARF_VALUE_MEMORY
;
594 result
= dwarf_expr_fetch (ctx
, 0);
595 in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 0);
599 dwarf_expr_pop (ctx
);
604 result
= dwarf_expr_fetch (ctx
, offset
);
605 in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, offset
);
610 struct dwarf_stack_value t1
, t2
;
612 if (ctx
->stack_len
< 2)
613 error (_("Not enough elements for DW_OP_swap. Need 2, have %d."),
615 t1
= ctx
->stack
[ctx
->stack_len
- 1];
616 t2
= ctx
->stack
[ctx
->stack_len
- 2];
617 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
618 ctx
->stack
[ctx
->stack_len
- 2] = t1
;
623 result
= dwarf_expr_fetch (ctx
, 1);
624 in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 1);
629 struct dwarf_stack_value t1
, t2
, t3
;
631 if (ctx
->stack_len
< 3)
632 error (_("Not enough elements for DW_OP_rot. Need 3, have %d."),
634 t1
= ctx
->stack
[ctx
->stack_len
- 1];
635 t2
= ctx
->stack
[ctx
->stack_len
- 2];
636 t3
= ctx
->stack
[ctx
->stack_len
- 3];
637 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
638 ctx
->stack
[ctx
->stack_len
- 2] = t3
;
639 ctx
->stack
[ctx
->stack_len
- 3] = t1
;
644 case DW_OP_deref_size
:
648 case DW_OP_plus_uconst
:
649 /* Unary operations. */
650 result
= dwarf_expr_fetch (ctx
, 0);
651 dwarf_expr_pop (ctx
);
657 gdb_byte
*buf
= alloca (ctx
->addr_size
);
658 (ctx
->read_mem
) (ctx
->baton
, buf
, result
, ctx
->addr_size
);
659 result
= dwarf2_read_address (ctx
->gdbarch
,
660 buf
, buf
+ ctx
->addr_size
,
665 case DW_OP_deref_size
:
667 int addr_size
= *op_ptr
++;
668 gdb_byte
*buf
= alloca (addr_size
);
669 (ctx
->read_mem
) (ctx
->baton
, buf
, result
, addr_size
);
670 result
= dwarf2_read_address (ctx
->gdbarch
,
671 buf
, buf
+ addr_size
,
677 if ((signed int) result
< 0)
686 case DW_OP_plus_uconst
:
687 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
711 /* Binary operations. Use the value engine to do computations in
713 CORE_ADDR first
, second
;
714 enum exp_opcode binop
;
715 struct value
*val1
, *val2
;
716 struct type
*stype
, *utype
;
718 second
= dwarf_expr_fetch (ctx
, 0);
719 dwarf_expr_pop (ctx
);
721 first
= dwarf_expr_fetch (ctx
, 0);
722 dwarf_expr_pop (ctx
);
724 utype
= unsigned_address_type (ctx
->gdbarch
, ctx
->addr_size
);
725 stype
= signed_address_type (ctx
->gdbarch
, ctx
->addr_size
);
726 val1
= value_from_longest (utype
, first
);
727 val2
= value_from_longest (utype
, second
);
732 binop
= BINOP_BITWISE_AND
;
747 binop
= BINOP_BITWISE_IOR
;
760 val1
= value_from_longest (stype
, first
);
763 binop
= BINOP_BITWISE_XOR
;
781 binop
= BINOP_NOTEQUAL
;
784 internal_error (__FILE__
, __LINE__
,
785 _("Can't be reached."));
787 result
= value_as_long (value_binop (val1
, val2
, binop
));
791 case DW_OP_call_frame_cfa
:
792 result
= (ctx
->get_frame_cfa
) (ctx
->baton
);
796 case DW_OP_GNU_push_tls_address
:
797 /* Variable is at a constant offset in the thread-local
798 storage block into the objfile for the current thread and
799 the dynamic linker module containing this expression. Here
800 we return returns the offset from that base. The top of the
801 stack has the offset from the beginning of the thread
802 control block at which the variable is located. Nothing
803 should follow this operator, so the top of stack would be
805 result
= dwarf_expr_fetch (ctx
, 0);
806 dwarf_expr_pop (ctx
);
807 result
= (ctx
->get_tls_address
) (ctx
->baton
, result
);
811 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
817 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
819 if (dwarf_expr_fetch (ctx
, 0) != 0)
821 dwarf_expr_pop (ctx
);
831 /* Record the piece. */
832 op_ptr
= read_uleb128 (op_ptr
, op_end
, &size
);
833 add_piece (ctx
, size
);
835 /* Pop off the address/regnum, and reset the location
837 if (ctx
->location
!= DWARF_VALUE_LITERAL
)
838 dwarf_expr_pop (ctx
);
839 ctx
->location
= DWARF_VALUE_MEMORY
;
843 case DW_OP_GNU_uninit
:
844 if (op_ptr
!= op_end
)
845 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
846 "be the very last op."));
848 ctx
->initialized
= 0;
852 error (_("Unhandled dwarf expression opcode 0x%x"), op
);
855 /* Most things push a result value. */
856 dwarf_expr_push (ctx
, result
, in_stack_memory
);
860 ctx
->recursion_depth
--;
861 gdb_assert (ctx
->recursion_depth
>= 0);