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 const gdb_byte
*, const gdb_byte
*);
37 /* Create a new context for the expression evaluator. */
39 struct dwarf_expr_context
*
40 new_dwarf_expr_context (void)
42 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;
91 ctx
->stack
= xrealloc (ctx
->stack
,
92 newlen
* sizeof (struct dwarf_stack_value
));
93 ctx
->stack_allocated
= newlen
;
97 /* Push VALUE onto CTX's stack. */
100 dwarf_expr_push (struct dwarf_expr_context
*ctx
, ULONGEST value
,
103 struct dwarf_stack_value
*v
;
105 /* We keep all stack elements within the range defined by the
106 DWARF address size. */
107 if (ctx
->addr_size
< sizeof (ULONGEST
))
108 value
&= ((ULONGEST
) 1 << (ctx
->addr_size
* HOST_CHAR_BIT
)) - 1;
110 dwarf_expr_grow_stack (ctx
, 1);
111 v
= &ctx
->stack
[ctx
->stack_len
++];
113 v
->in_stack_memory
= in_stack_memory
;
116 /* Pop the top item off of CTX's stack. */
119 dwarf_expr_pop (struct dwarf_expr_context
*ctx
)
121 if (ctx
->stack_len
<= 0)
122 error (_("dwarf expression stack underflow"));
126 /* Retrieve the N'th item on CTX's stack. */
129 dwarf_expr_fetch (struct dwarf_expr_context
*ctx
, int n
)
131 if (ctx
->stack_len
<= n
)
132 error (_("Asked for position %d of stack, stack only has %d elements on it."),
134 return ctx
->stack
[ctx
->stack_len
- (1 + n
)].value
;
138 /* Retrieve the N'th item on CTX's stack, converted to an address. */
141 dwarf_expr_fetch_address (struct dwarf_expr_context
*ctx
, int n
)
143 ULONGEST result
= dwarf_expr_fetch (ctx
, n
);
145 /* For most architectures, calling extract_unsigned_integer() alone
146 is sufficient for extracting an address. However, some
147 architectures (e.g. MIPS) use signed addresses and using
148 extract_unsigned_integer() will not produce a correct
149 result. Make sure we invoke gdbarch_integer_to_address()
150 for those architectures which require it. */
151 if (gdbarch_integer_to_address_p (ctx
->gdbarch
))
153 enum bfd_endian byte_order
= gdbarch_byte_order (ctx
->gdbarch
);
154 gdb_byte
*buf
= alloca (ctx
->addr_size
);
155 struct type
*int_type
;
157 switch (ctx
->addr_size
)
160 int_type
= builtin_type (ctx
->gdbarch
)->builtin_uint16
;
163 int_type
= builtin_type (ctx
->gdbarch
)->builtin_uint32
;
166 int_type
= builtin_type (ctx
->gdbarch
)->builtin_uint64
;
169 internal_error (__FILE__
, __LINE__
,
170 _("Unsupported address size.\n"));
173 store_unsigned_integer (buf
, ctx
->addr_size
, byte_order
, result
);
174 return gdbarch_integer_to_address (ctx
->gdbarch
, int_type
, buf
);
177 return (CORE_ADDR
) result
;
180 /* Retrieve the in_stack_memory flag of the N'th item on CTX's stack. */
183 dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context
*ctx
, int n
)
185 if (ctx
->stack_len
<= n
)
186 error (_("Asked for position %d of stack, stack only has %d elements on it."),
188 return ctx
->stack
[ctx
->stack_len
- (1 + n
)].in_stack_memory
;
192 /* Return true if the expression stack is empty. */
195 dwarf_expr_stack_empty_p (struct dwarf_expr_context
*ctx
)
197 return ctx
->stack_len
== 0;
200 /* Add a new piece to CTX's piece list. */
202 add_piece (struct dwarf_expr_context
*ctx
, ULONGEST size
, ULONGEST offset
)
204 struct dwarf_expr_piece
*p
;
208 ctx
->pieces
= xrealloc (ctx
->pieces
,
210 * sizeof (struct dwarf_expr_piece
)));
212 p
= &ctx
->pieces
[ctx
->num_pieces
- 1];
213 p
->location
= ctx
->location
;
217 if (p
->location
== DWARF_VALUE_LITERAL
)
219 p
->v
.literal
.data
= ctx
->data
;
220 p
->v
.literal
.length
= ctx
->len
;
222 else if (dwarf_expr_stack_empty_p (ctx
))
224 p
->location
= DWARF_VALUE_OPTIMIZED_OUT
;
225 /* Also reset the context's location, for our callers. This is
226 a somewhat strange approach, but this lets us avoid setting
227 the location to DWARF_VALUE_MEMORY in all the individual
228 cases in the evaluator. */
229 ctx
->location
= DWARF_VALUE_OPTIMIZED_OUT
;
231 else if (p
->location
== DWARF_VALUE_MEMORY
)
233 p
->v
.mem
.addr
= dwarf_expr_fetch_address (ctx
, 0);
234 p
->v
.mem
.in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 0);
238 p
->v
.value
= dwarf_expr_fetch (ctx
, 0);
242 /* Evaluate the expression at ADDR (LEN bytes long) using the context
246 dwarf_expr_eval (struct dwarf_expr_context
*ctx
, const gdb_byte
*addr
,
249 int old_recursion_depth
= ctx
->recursion_depth
;
251 execute_stack_op (ctx
, addr
, addr
+ len
);
253 /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here. */
255 gdb_assert (ctx
->recursion_depth
== old_recursion_depth
);
258 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
259 by R, and return the new value of BUF. Verify that it doesn't extend
263 read_uleb128 (const gdb_byte
*buf
, const gdb_byte
*buf_end
, ULONGEST
* r
)
272 error (_("read_uleb128: Corrupted DWARF expression."));
275 result
|= (byte
& 0x7f) << shift
;
276 if ((byte
& 0x80) == 0)
284 /* Decode the signed LEB128 constant at BUF into the variable pointed to
285 by R, and return the new value of BUF. Verify that it doesn't extend
289 read_sleb128 (const gdb_byte
*buf
, const gdb_byte
*buf_end
, LONGEST
* r
)
298 error (_("read_sleb128: Corrupted DWARF expression."));
301 result
|= (byte
& 0x7f) << shift
;
303 if ((byte
& 0x80) == 0)
306 if (shift
< (sizeof (*r
) * 8) && (byte
& 0x40) != 0)
307 result
|= -(1 << shift
);
314 /* Check that the current operator is either at the end of an
315 expression, or that it is followed by a composition operator. */
318 dwarf_expr_require_composition (const gdb_byte
*op_ptr
, const gdb_byte
*op_end
,
321 /* It seems like DW_OP_GNU_uninit should be handled here. However,
322 it doesn't seem to make sense for DW_OP_*_value, and it was not
323 checked at the other place that this function is called. */
324 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
&& *op_ptr
!= DW_OP_bit_piece
)
325 error (_("DWARF-2 expression error: `%s' operations must be "
326 "used either alone or in conjuction with DW_OP_piece "
327 "or DW_OP_bit_piece."),
331 /* The engine for the expression evaluator. Using the context in CTX,
332 evaluate the expression between OP_PTR and OP_END. */
335 execute_stack_op (struct dwarf_expr_context
*ctx
,
336 const gdb_byte
*op_ptr
, const gdb_byte
*op_end
)
338 #define sign_ext(x) ((LONGEST) (((x) ^ sign_bit) - sign_bit))
339 ULONGEST sign_bit
= (ctx
->addr_size
>= sizeof (ULONGEST
) ? 0
340 : ((ULONGEST
) 1) << (ctx
->addr_size
* 8 - 1));
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
= extract_unsigned_integer (op_ptr
,
404 ctx
->addr_size
, byte_order
);
405 op_ptr
+= ctx
->addr_size
;
406 /* Some versions of GCC emit DW_OP_addr before
407 DW_OP_GNU_push_tls_address. In this case the value is an
408 index, not an address. We don't support things like
409 branching between the address and the TLS op. */
410 if (op_ptr
>= op_end
|| *op_ptr
!= DW_OP_GNU_push_tls_address
)
411 result
+= ctx
->offset
;
415 result
= extract_unsigned_integer (op_ptr
, 1, byte_order
);
419 result
= extract_signed_integer (op_ptr
, 1, byte_order
);
423 result
= extract_unsigned_integer (op_ptr
, 2, byte_order
);
427 result
= extract_signed_integer (op_ptr
, 2, byte_order
);
431 result
= extract_unsigned_integer (op_ptr
, 4, byte_order
);
435 result
= extract_signed_integer (op_ptr
, 4, byte_order
);
439 result
= extract_unsigned_integer (op_ptr
, 8, byte_order
);
443 result
= extract_signed_integer (op_ptr
, 8, byte_order
);
447 op_ptr
= read_uleb128 (op_ptr
, op_end
, &uoffset
);
451 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
455 /* The DW_OP_reg operations are required to occur alone in
456 location expressions. */
490 && *op_ptr
!= DW_OP_piece
491 && *op_ptr
!= DW_OP_bit_piece
492 && *op_ptr
!= DW_OP_GNU_uninit
)
493 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
494 "used either alone or in conjuction with DW_OP_piece "
495 "or DW_OP_bit_piece."));
497 result
= op
- DW_OP_reg0
;
498 ctx
->location
= DWARF_VALUE_REGISTER
;
502 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
503 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
506 ctx
->location
= DWARF_VALUE_REGISTER
;
509 case DW_OP_implicit_value
:
513 op_ptr
= read_uleb128 (op_ptr
, op_end
, &len
);
514 if (op_ptr
+ len
> op_end
)
515 error (_("DW_OP_implicit_value: too few bytes available."));
518 ctx
->location
= DWARF_VALUE_LITERAL
;
520 dwarf_expr_require_composition (op_ptr
, op_end
,
521 "DW_OP_implicit_value");
525 case DW_OP_stack_value
:
526 ctx
->location
= DWARF_VALUE_STACK
;
527 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_stack_value");
563 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
564 result
= (ctx
->read_reg
) (ctx
->baton
, op
- DW_OP_breg0
);
570 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
571 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
572 result
= (ctx
->read_reg
) (ctx
->baton
, reg
);
578 const gdb_byte
*datastart
;
580 unsigned int before_stack_len
;
582 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
583 /* Rather than create a whole new context, we simply
584 record the stack length before execution, then reset it
585 afterwards, effectively erasing whatever the recursive
587 before_stack_len
= ctx
->stack_len
;
588 /* FIXME: cagney/2003-03-26: This code should be using
589 get_frame_base_address(), and then implement a dwarf2
590 specific this_base method. */
591 (ctx
->get_frame_base
) (ctx
->baton
, &datastart
, &datalen
);
592 dwarf_expr_eval (ctx
, datastart
, datalen
);
593 if (ctx
->location
== DWARF_VALUE_MEMORY
)
594 result
= dwarf_expr_fetch_address (ctx
, 0);
595 else if (ctx
->location
== DWARF_VALUE_REGISTER
)
596 result
= (ctx
->read_reg
) (ctx
->baton
, dwarf_expr_fetch (ctx
, 0));
598 error (_("Not implemented: computing frame base using explicit value operator"));
599 result
= result
+ offset
;
601 ctx
->stack_len
= before_stack_len
;
602 ctx
->location
= DWARF_VALUE_MEMORY
;
607 result
= dwarf_expr_fetch (ctx
, 0);
608 in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 0);
612 dwarf_expr_pop (ctx
);
617 result
= dwarf_expr_fetch (ctx
, offset
);
618 in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, offset
);
623 struct dwarf_stack_value t1
, t2
;
625 if (ctx
->stack_len
< 2)
626 error (_("Not enough elements for DW_OP_swap. Need 2, have %d."),
628 t1
= ctx
->stack
[ctx
->stack_len
- 1];
629 t2
= ctx
->stack
[ctx
->stack_len
- 2];
630 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
631 ctx
->stack
[ctx
->stack_len
- 2] = t1
;
636 result
= dwarf_expr_fetch (ctx
, 1);
637 in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 1);
642 struct dwarf_stack_value t1
, t2
, t3
;
644 if (ctx
->stack_len
< 3)
645 error (_("Not enough elements for DW_OP_rot. Need 3, have %d."),
647 t1
= ctx
->stack
[ctx
->stack_len
- 1];
648 t2
= ctx
->stack
[ctx
->stack_len
- 2];
649 t3
= ctx
->stack
[ctx
->stack_len
- 3];
650 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
651 ctx
->stack
[ctx
->stack_len
- 2] = t3
;
652 ctx
->stack
[ctx
->stack_len
- 3] = t1
;
657 case DW_OP_deref_size
:
659 int addr_size
= (op
== DW_OP_deref
? ctx
->addr_size
: *op_ptr
++);
660 gdb_byte
*buf
= alloca (addr_size
);
661 CORE_ADDR addr
= dwarf_expr_fetch_address (ctx
, 0);
662 dwarf_expr_pop (ctx
);
664 (ctx
->read_mem
) (ctx
->baton
, buf
, addr
, addr_size
);
665 result
= extract_unsigned_integer (buf
, addr_size
, byte_order
);
672 case DW_OP_plus_uconst
:
673 /* Unary operations. */
674 result
= dwarf_expr_fetch (ctx
, 0);
675 dwarf_expr_pop (ctx
);
680 if (sign_ext (result
) < 0)
689 case DW_OP_plus_uconst
:
690 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
714 /* Binary operations. */
715 ULONGEST first
, second
;
717 second
= dwarf_expr_fetch (ctx
, 0);
718 dwarf_expr_pop (ctx
);
720 first
= dwarf_expr_fetch (ctx
, 0);
721 dwarf_expr_pop (ctx
);
726 result
= first
& second
;
730 error (_("Division by zero"));
731 result
= sign_ext (first
) / sign_ext (second
);
734 result
= first
- second
;
738 error (_("Division by zero"));
739 result
= first
% second
;
742 result
= first
* second
;
745 result
= first
| second
;
748 result
= first
+ second
;
751 result
= first
<< second
;
754 result
= first
>> second
;
757 result
= sign_ext (first
) >> second
;
760 result
= first
^ second
;
763 result
= sign_ext (first
) <= sign_ext (second
);
766 result
= sign_ext (first
) >= sign_ext (second
);
769 result
= sign_ext (first
) == sign_ext (second
);
772 result
= sign_ext (first
) < sign_ext (second
);
775 result
= sign_ext (first
) > sign_ext (second
);
778 result
= sign_ext (first
) != sign_ext (second
);
781 internal_error (__FILE__
, __LINE__
,
782 _("Can't be reached."));
787 case DW_OP_call_frame_cfa
:
788 result
= (ctx
->get_frame_cfa
) (ctx
->baton
);
792 case DW_OP_GNU_push_tls_address
:
793 /* Variable is at a constant offset in the thread-local
794 storage block into the objfile for the current thread and
795 the dynamic linker module containing this expression. Here
796 we return returns the offset from that base. The top of the
797 stack has the offset from the beginning of the thread
798 control block at which the variable is located. Nothing
799 should follow this operator, so the top of stack would be
801 result
= dwarf_expr_fetch (ctx
, 0);
802 dwarf_expr_pop (ctx
);
803 result
= (ctx
->get_tls_address
) (ctx
->baton
, result
);
807 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
813 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
815 if (dwarf_expr_fetch (ctx
, 0) != 0)
817 dwarf_expr_pop (ctx
);
827 /* Record the piece. */
828 op_ptr
= read_uleb128 (op_ptr
, op_end
, &size
);
829 add_piece (ctx
, 8 * size
, 0);
831 /* Pop off the address/regnum, and reset the location
833 if (ctx
->location
!= DWARF_VALUE_LITERAL
834 && ctx
->location
!= DWARF_VALUE_OPTIMIZED_OUT
)
835 dwarf_expr_pop (ctx
);
836 ctx
->location
= DWARF_VALUE_MEMORY
;
840 case DW_OP_bit_piece
:
842 ULONGEST size
, offset
;
844 /* Record the piece. */
845 op_ptr
= read_uleb128 (op_ptr
, op_end
, &size
);
846 op_ptr
= read_uleb128 (op_ptr
, op_end
, &offset
);
847 add_piece (ctx
, size
, offset
);
849 /* Pop off the address/regnum, and reset the location
851 if (ctx
->location
!= DWARF_VALUE_LITERAL
852 && ctx
->location
!= DWARF_VALUE_OPTIMIZED_OUT
)
853 dwarf_expr_pop (ctx
);
854 ctx
->location
= DWARF_VALUE_MEMORY
;
858 case DW_OP_GNU_uninit
:
859 if (op_ptr
!= op_end
)
860 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
861 "be the very last op."));
863 ctx
->initialized
= 0;
867 result
= extract_unsigned_integer (op_ptr
, 2, byte_order
);
869 ctx
->dwarf_call (ctx
, result
);
873 result
= extract_unsigned_integer (op_ptr
, 4, byte_order
);
875 ctx
->dwarf_call (ctx
, result
);
879 error (_("Unhandled dwarf expression opcode 0x%x"), op
);
882 /* Most things push a result value. */
883 dwarf_expr_push (ctx
, result
, in_stack_memory
);
887 ctx
->recursion_depth
--;
888 gdb_assert (ctx
->recursion_depth
>= 0);