1 /* DWARF 2 Expression Evaluator.
3 Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2010, 2011
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, "
133 "stack only has %d elements on it."),
135 return ctx
->stack
[ctx
->stack_len
- (1 + n
)].value
;
139 /* Retrieve the N'th item on CTX's stack, converted to an address. */
142 dwarf_expr_fetch_address (struct dwarf_expr_context
*ctx
, int n
)
144 ULONGEST result
= dwarf_expr_fetch (ctx
, n
);
146 /* For most architectures, calling extract_unsigned_integer() alone
147 is sufficient for extracting an address. However, some
148 architectures (e.g. MIPS) use signed addresses and using
149 extract_unsigned_integer() will not produce a correct
150 result. Make sure we invoke gdbarch_integer_to_address()
151 for those architectures which require it. */
152 if (gdbarch_integer_to_address_p (ctx
->gdbarch
))
154 enum bfd_endian byte_order
= gdbarch_byte_order (ctx
->gdbarch
);
155 gdb_byte
*buf
= alloca (ctx
->addr_size
);
156 struct type
*int_type
;
158 switch (ctx
->addr_size
)
161 int_type
= builtin_type (ctx
->gdbarch
)->builtin_uint16
;
164 int_type
= builtin_type (ctx
->gdbarch
)->builtin_uint32
;
167 int_type
= builtin_type (ctx
->gdbarch
)->builtin_uint64
;
170 internal_error (__FILE__
, __LINE__
,
171 _("Unsupported address size.\n"));
174 store_unsigned_integer (buf
, ctx
->addr_size
, byte_order
, result
);
175 return gdbarch_integer_to_address (ctx
->gdbarch
, int_type
, buf
);
178 return (CORE_ADDR
) result
;
181 /* Retrieve the in_stack_memory flag of the N'th item on CTX's stack. */
184 dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context
*ctx
, int n
)
186 if (ctx
->stack_len
<= n
)
187 error (_("Asked for position %d of stack, "
188 "stack only has %d elements on it."),
190 return ctx
->stack
[ctx
->stack_len
- (1 + n
)].in_stack_memory
;
194 /* Return true if the expression stack is empty. */
197 dwarf_expr_stack_empty_p (struct dwarf_expr_context
*ctx
)
199 return ctx
->stack_len
== 0;
202 /* Add a new piece to CTX's piece list. */
204 add_piece (struct dwarf_expr_context
*ctx
, ULONGEST size
, ULONGEST offset
)
206 struct dwarf_expr_piece
*p
;
210 ctx
->pieces
= xrealloc (ctx
->pieces
,
212 * sizeof (struct dwarf_expr_piece
)));
214 p
= &ctx
->pieces
[ctx
->num_pieces
- 1];
215 p
->location
= ctx
->location
;
219 if (p
->location
== DWARF_VALUE_LITERAL
)
221 p
->v
.literal
.data
= ctx
->data
;
222 p
->v
.literal
.length
= ctx
->len
;
224 else if (dwarf_expr_stack_empty_p (ctx
))
226 p
->location
= DWARF_VALUE_OPTIMIZED_OUT
;
227 /* Also reset the context's location, for our callers. This is
228 a somewhat strange approach, but this lets us avoid setting
229 the location to DWARF_VALUE_MEMORY in all the individual
230 cases in the evaluator. */
231 ctx
->location
= DWARF_VALUE_OPTIMIZED_OUT
;
233 else if (p
->location
== DWARF_VALUE_MEMORY
)
235 p
->v
.mem
.addr
= dwarf_expr_fetch_address (ctx
, 0);
236 p
->v
.mem
.in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 0);
238 else if (p
->location
== DWARF_VALUE_IMPLICIT_POINTER
)
240 p
->v
.ptr
.die
= ctx
->len
;
241 p
->v
.ptr
.offset
= (LONGEST
) dwarf_expr_fetch (ctx
, 0);
245 p
->v
.value
= dwarf_expr_fetch (ctx
, 0);
249 /* Evaluate the expression at ADDR (LEN bytes long) using the context
253 dwarf_expr_eval (struct dwarf_expr_context
*ctx
, const gdb_byte
*addr
,
256 int old_recursion_depth
= ctx
->recursion_depth
;
258 execute_stack_op (ctx
, addr
, addr
+ len
);
260 /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here. */
262 gdb_assert (ctx
->recursion_depth
== old_recursion_depth
);
265 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
266 by R, and return the new value of BUF. Verify that it doesn't extend
270 read_uleb128 (const gdb_byte
*buf
, const gdb_byte
*buf_end
, ULONGEST
* r
)
279 error (_("read_uleb128: Corrupted DWARF expression."));
282 result
|= (byte
& 0x7f) << shift
;
283 if ((byte
& 0x80) == 0)
291 /* Decode the signed LEB128 constant at BUF into the variable pointed to
292 by R, and return the new value of BUF. Verify that it doesn't extend
296 read_sleb128 (const gdb_byte
*buf
, const gdb_byte
*buf_end
, LONGEST
* r
)
305 error (_("read_sleb128: Corrupted DWARF expression."));
308 result
|= (byte
& 0x7f) << shift
;
310 if ((byte
& 0x80) == 0)
313 if (shift
< (sizeof (*r
) * 8) && (byte
& 0x40) != 0)
314 result
|= -(1 << shift
);
321 /* Check that the current operator is either at the end of an
322 expression, or that it is followed by a composition operator. */
325 dwarf_expr_require_composition (const gdb_byte
*op_ptr
, const gdb_byte
*op_end
,
328 /* It seems like DW_OP_GNU_uninit should be handled here. However,
329 it doesn't seem to make sense for DW_OP_*_value, and it was not
330 checked at the other place that this function is called. */
331 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
&& *op_ptr
!= DW_OP_bit_piece
)
332 error (_("DWARF-2 expression error: `%s' operations must be "
333 "used either alone or in conjuction with DW_OP_piece "
334 "or DW_OP_bit_piece."),
338 /* The engine for the expression evaluator. Using the context in CTX,
339 evaluate the expression between OP_PTR and OP_END. */
342 execute_stack_op (struct dwarf_expr_context
*ctx
,
343 const gdb_byte
*op_ptr
, const gdb_byte
*op_end
)
345 #define sign_ext(x) ((LONGEST) (((x) ^ sign_bit) - sign_bit))
346 ULONGEST sign_bit
= (ctx
->addr_size
>= sizeof (ULONGEST
) ? 0
347 : ((ULONGEST
) 1) << (ctx
->addr_size
* 8 - 1));
348 enum bfd_endian byte_order
= gdbarch_byte_order (ctx
->gdbarch
);
350 ctx
->location
= DWARF_VALUE_MEMORY
;
351 ctx
->initialized
= 1; /* Default is initialized. */
353 if (ctx
->recursion_depth
> ctx
->max_recursion_depth
)
354 error (_("DWARF-2 expression error: Loop detected (%d)."),
355 ctx
->recursion_depth
);
356 ctx
->recursion_depth
++;
358 while (op_ptr
< op_end
)
360 enum dwarf_location_atom op
= *op_ptr
++;
362 /* Assume the value is not in stack memory.
363 Code that knows otherwise sets this to 1.
364 Some arithmetic on stack addresses can probably be assumed to still
365 be a stack address, but we skip this complication for now.
366 This is just an optimization, so it's always ok to punt
367 and leave this as 0. */
368 int in_stack_memory
= 0;
369 ULONGEST uoffset
, reg
;
406 result
= op
- DW_OP_lit0
;
410 result
= extract_unsigned_integer (op_ptr
,
411 ctx
->addr_size
, byte_order
);
412 op_ptr
+= ctx
->addr_size
;
413 /* Some versions of GCC emit DW_OP_addr before
414 DW_OP_GNU_push_tls_address. In this case the value is an
415 index, not an address. We don't support things like
416 branching between the address and the TLS op. */
417 if (op_ptr
>= op_end
|| *op_ptr
!= DW_OP_GNU_push_tls_address
)
418 result
+= ctx
->offset
;
422 result
= extract_unsigned_integer (op_ptr
, 1, byte_order
);
426 result
= extract_signed_integer (op_ptr
, 1, byte_order
);
430 result
= extract_unsigned_integer (op_ptr
, 2, byte_order
);
434 result
= extract_signed_integer (op_ptr
, 2, byte_order
);
438 result
= extract_unsigned_integer (op_ptr
, 4, byte_order
);
442 result
= extract_signed_integer (op_ptr
, 4, byte_order
);
446 result
= extract_unsigned_integer (op_ptr
, 8, byte_order
);
450 result
= extract_signed_integer (op_ptr
, 8, byte_order
);
454 op_ptr
= read_uleb128 (op_ptr
, op_end
, &uoffset
);
458 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
462 /* The DW_OP_reg operations are required to occur alone in
463 location expressions. */
497 && *op_ptr
!= DW_OP_piece
498 && *op_ptr
!= DW_OP_bit_piece
499 && *op_ptr
!= DW_OP_GNU_uninit
)
500 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
501 "used either alone or in conjuction with DW_OP_piece "
502 "or DW_OP_bit_piece."));
504 result
= op
- DW_OP_reg0
;
505 ctx
->location
= DWARF_VALUE_REGISTER
;
509 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
510 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
513 ctx
->location
= DWARF_VALUE_REGISTER
;
516 case DW_OP_implicit_value
:
520 op_ptr
= read_uleb128 (op_ptr
, op_end
, &len
);
521 if (op_ptr
+ len
> op_end
)
522 error (_("DW_OP_implicit_value: too few bytes available."));
525 ctx
->location
= DWARF_VALUE_LITERAL
;
527 dwarf_expr_require_composition (op_ptr
, op_end
,
528 "DW_OP_implicit_value");
532 case DW_OP_stack_value
:
533 ctx
->location
= DWARF_VALUE_STACK
;
534 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_stack_value");
537 case DW_OP_GNU_implicit_pointer
:
542 /* The referred-to DIE. */
543 ctx
->len
= extract_unsigned_integer (op_ptr
, ctx
->addr_size
,
545 op_ptr
+= ctx
->addr_size
;
547 /* The byte offset into the data. */
548 op_ptr
= read_sleb128 (op_ptr
, op_end
, &len
);
549 result
= (ULONGEST
) len
;
551 ctx
->location
= DWARF_VALUE_IMPLICIT_POINTER
;
552 dwarf_expr_require_composition (op_ptr
, op_end
,
553 "DW_OP_GNU_implicit_pointer");
590 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
591 result
= (ctx
->read_reg
) (ctx
->baton
, op
- DW_OP_breg0
);
597 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
598 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
599 result
= (ctx
->read_reg
) (ctx
->baton
, reg
);
605 const gdb_byte
*datastart
;
607 unsigned int before_stack_len
;
609 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
610 /* Rather than create a whole new context, we simply
611 record the stack length before execution, then reset it
612 afterwards, effectively erasing whatever the recursive
614 before_stack_len
= ctx
->stack_len
;
615 /* FIXME: cagney/2003-03-26: This code should be using
616 get_frame_base_address(), and then implement a dwarf2
617 specific this_base method. */
618 (ctx
->get_frame_base
) (ctx
->baton
, &datastart
, &datalen
);
619 dwarf_expr_eval (ctx
, datastart
, datalen
);
620 if (ctx
->location
== DWARF_VALUE_MEMORY
)
621 result
= dwarf_expr_fetch_address (ctx
, 0);
622 else if (ctx
->location
== DWARF_VALUE_REGISTER
)
623 result
= (ctx
->read_reg
) (ctx
->baton
, dwarf_expr_fetch (ctx
, 0));
625 error (_("Not implemented: computing frame "
626 "base using explicit value operator"));
627 result
= result
+ offset
;
629 ctx
->stack_len
= before_stack_len
;
630 ctx
->location
= DWARF_VALUE_MEMORY
;
635 result
= dwarf_expr_fetch (ctx
, 0);
636 in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 0);
640 dwarf_expr_pop (ctx
);
645 result
= dwarf_expr_fetch (ctx
, offset
);
646 in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, offset
);
651 struct dwarf_stack_value t1
, t2
;
653 if (ctx
->stack_len
< 2)
654 error (_("Not enough elements for "
655 "DW_OP_swap. Need 2, have %d."),
657 t1
= ctx
->stack
[ctx
->stack_len
- 1];
658 t2
= ctx
->stack
[ctx
->stack_len
- 2];
659 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
660 ctx
->stack
[ctx
->stack_len
- 2] = t1
;
665 result
= dwarf_expr_fetch (ctx
, 1);
666 in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 1);
671 struct dwarf_stack_value t1
, t2
, t3
;
673 if (ctx
->stack_len
< 3)
674 error (_("Not enough elements for "
675 "DW_OP_rot. Need 3, have %d."),
677 t1
= ctx
->stack
[ctx
->stack_len
- 1];
678 t2
= ctx
->stack
[ctx
->stack_len
- 2];
679 t3
= ctx
->stack
[ctx
->stack_len
- 3];
680 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
681 ctx
->stack
[ctx
->stack_len
- 2] = t3
;
682 ctx
->stack
[ctx
->stack_len
- 3] = t1
;
687 case DW_OP_deref_size
:
689 int addr_size
= (op
== DW_OP_deref
? ctx
->addr_size
: *op_ptr
++);
690 gdb_byte
*buf
= alloca (addr_size
);
691 CORE_ADDR addr
= dwarf_expr_fetch_address (ctx
, 0);
692 dwarf_expr_pop (ctx
);
694 (ctx
->read_mem
) (ctx
->baton
, buf
, addr
, addr_size
);
695 result
= extract_unsigned_integer (buf
, addr_size
, byte_order
);
702 case DW_OP_plus_uconst
:
703 /* Unary operations. */
704 result
= dwarf_expr_fetch (ctx
, 0);
705 dwarf_expr_pop (ctx
);
710 if (sign_ext (result
) < 0)
719 case DW_OP_plus_uconst
:
720 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
744 /* Binary operations. */
745 ULONGEST first
, second
;
747 second
= dwarf_expr_fetch (ctx
, 0);
748 dwarf_expr_pop (ctx
);
750 first
= dwarf_expr_fetch (ctx
, 0);
751 dwarf_expr_pop (ctx
);
756 result
= first
& second
;
760 error (_("Division by zero"));
761 result
= sign_ext (first
) / sign_ext (second
);
764 result
= first
- second
;
768 error (_("Division by zero"));
769 result
= first
% second
;
772 result
= first
* second
;
775 result
= first
| second
;
778 result
= first
+ second
;
781 result
= first
<< second
;
784 result
= first
>> second
;
787 result
= sign_ext (first
) >> second
;
790 result
= first
^ second
;
793 result
= sign_ext (first
) <= sign_ext (second
);
796 result
= sign_ext (first
) >= sign_ext (second
);
799 result
= sign_ext (first
) == sign_ext (second
);
802 result
= sign_ext (first
) < sign_ext (second
);
805 result
= sign_ext (first
) > sign_ext (second
);
808 result
= sign_ext (first
) != sign_ext (second
);
811 internal_error (__FILE__
, __LINE__
,
812 _("Can't be reached."));
817 case DW_OP_call_frame_cfa
:
818 result
= (ctx
->get_frame_cfa
) (ctx
->baton
);
822 case DW_OP_GNU_push_tls_address
:
823 /* Variable is at a constant offset in the thread-local
824 storage block into the objfile for the current thread and
825 the dynamic linker module containing this expression. Here
826 we return returns the offset from that base. The top of the
827 stack has the offset from the beginning of the thread
828 control block at which the variable is located. Nothing
829 should follow this operator, so the top of stack would be
831 result
= dwarf_expr_fetch (ctx
, 0);
832 dwarf_expr_pop (ctx
);
833 result
= (ctx
->get_tls_address
) (ctx
->baton
, result
);
837 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
843 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
845 if (dwarf_expr_fetch (ctx
, 0) != 0)
847 dwarf_expr_pop (ctx
);
857 /* Record the piece. */
858 op_ptr
= read_uleb128 (op_ptr
, op_end
, &size
);
859 add_piece (ctx
, 8 * size
, 0);
861 /* Pop off the address/regnum, and reset the location
863 if (ctx
->location
!= DWARF_VALUE_LITERAL
864 && ctx
->location
!= DWARF_VALUE_OPTIMIZED_OUT
)
865 dwarf_expr_pop (ctx
);
866 ctx
->location
= DWARF_VALUE_MEMORY
;
870 case DW_OP_bit_piece
:
872 ULONGEST size
, offset
;
874 /* Record the piece. */
875 op_ptr
= read_uleb128 (op_ptr
, op_end
, &size
);
876 op_ptr
= read_uleb128 (op_ptr
, op_end
, &offset
);
877 add_piece (ctx
, size
, offset
);
879 /* Pop off the address/regnum, and reset the location
881 if (ctx
->location
!= DWARF_VALUE_LITERAL
882 && ctx
->location
!= DWARF_VALUE_OPTIMIZED_OUT
)
883 dwarf_expr_pop (ctx
);
884 ctx
->location
= DWARF_VALUE_MEMORY
;
888 case DW_OP_GNU_uninit
:
889 if (op_ptr
!= op_end
)
890 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
891 "be the very last op."));
893 ctx
->initialized
= 0;
897 result
= extract_unsigned_integer (op_ptr
, 2, byte_order
);
899 ctx
->dwarf_call (ctx
, result
);
903 result
= extract_unsigned_integer (op_ptr
, 4, byte_order
);
905 ctx
->dwarf_call (ctx
, result
);
908 case DW_OP_GNU_entry_value
:
909 /* This operation is not yet supported by GDB. */
910 ctx
->location
= DWARF_VALUE_OPTIMIZED_OUT
;
913 goto abort_expression
;
916 error (_("Unhandled dwarf expression opcode 0x%x"), op
);
919 /* Most things push a result value. */
920 dwarf_expr_push (ctx
, result
, in_stack_memory
);
925 /* To simplify our main caller, if the result is an implicit
926 pointer, then make a pieced value. This is ok because we can't
927 have implicit pointers in contexts where pieces are invalid. */
928 if (ctx
->location
== DWARF_VALUE_IMPLICIT_POINTER
)
929 add_piece (ctx
, 8 * ctx
->addr_size
, 0);
932 ctx
->recursion_depth
--;
933 gdb_assert (ctx
->recursion_depth
>= 0);