1 /* DWARF 2 Expression Evaluator.
3 Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008
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/>. */
28 #include "elf/dwarf2.h"
29 #include "dwarf2expr.h"
31 /* Local prototypes. */
33 static void execute_stack_op (struct dwarf_expr_context
*,
34 gdb_byte
*, gdb_byte
*);
35 static struct type
*unsigned_address_type (int);
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
;
43 retval
= xcalloc (1, sizeof (struct dwarf_expr_context
));
44 retval
->stack_len
= 0;
45 retval
->stack_allocated
= 10;
46 retval
->stack
= xmalloc (retval
->stack_allocated
* sizeof (CORE_ADDR
));
47 retval
->num_pieces
= 0;
52 /* Release the memory allocated to CTX. */
55 free_dwarf_expr_context (struct dwarf_expr_context
*ctx
)
62 /* Expand the memory allocated to CTX's stack to contain at least
63 NEED more elements than are currently used. */
66 dwarf_expr_grow_stack (struct dwarf_expr_context
*ctx
, size_t need
)
68 if (ctx
->stack_len
+ need
> ctx
->stack_allocated
)
70 size_t newlen
= ctx
->stack_len
+ need
+ 10;
71 ctx
->stack
= xrealloc (ctx
->stack
,
72 newlen
* sizeof (CORE_ADDR
));
73 ctx
->stack_allocated
= newlen
;
77 /* Push VALUE onto CTX's stack. */
80 dwarf_expr_push (struct dwarf_expr_context
*ctx
, CORE_ADDR value
)
82 dwarf_expr_grow_stack (ctx
, 1);
83 ctx
->stack
[ctx
->stack_len
++] = value
;
86 /* Pop the top item off of CTX's stack. */
89 dwarf_expr_pop (struct dwarf_expr_context
*ctx
)
91 if (ctx
->stack_len
<= 0)
92 error (_("dwarf expression stack underflow"));
96 /* Retrieve the N'th item on CTX's stack. */
99 dwarf_expr_fetch (struct dwarf_expr_context
*ctx
, int n
)
101 if (ctx
->stack_len
<= n
)
102 error (_("Asked for position %d of stack, stack only has %d elements on it."),
104 return ctx
->stack
[ctx
->stack_len
- (1 + n
)];
108 /* Add a new piece to CTX's piece list. */
110 add_piece (struct dwarf_expr_context
*ctx
,
111 int in_reg
, CORE_ADDR value
, ULONGEST size
)
113 struct dwarf_expr_piece
*p
;
118 ctx
->pieces
= xrealloc (ctx
->pieces
,
120 * sizeof (struct dwarf_expr_piece
)));
122 ctx
->pieces
= xmalloc (ctx
->num_pieces
123 * sizeof (struct dwarf_expr_piece
));
125 p
= &ctx
->pieces
[ctx
->num_pieces
- 1];
131 /* Evaluate the expression at ADDR (LEN bytes long) using the context
135 dwarf_expr_eval (struct dwarf_expr_context
*ctx
, gdb_byte
*addr
, size_t len
)
137 execute_stack_op (ctx
, addr
, addr
+ len
);
140 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
141 by R, and return the new value of BUF. Verify that it doesn't extend
145 read_uleb128 (gdb_byte
*buf
, gdb_byte
*buf_end
, ULONGEST
* r
)
154 error (_("read_uleb128: Corrupted DWARF expression."));
157 result
|= (byte
& 0x7f) << shift
;
158 if ((byte
& 0x80) == 0)
166 /* Decode the signed LEB128 constant at BUF into the variable pointed to
167 by R, and return the new value of BUF. Verify that it doesn't extend
171 read_sleb128 (gdb_byte
*buf
, gdb_byte
*buf_end
, LONGEST
* r
)
180 error (_("read_sleb128: Corrupted DWARF expression."));
183 result
|= (byte
& 0x7f) << shift
;
185 if ((byte
& 0x80) == 0)
188 if (shift
< (sizeof (*r
) * 8) && (byte
& 0x40) != 0)
189 result
|= -(1 << shift
);
195 /* Read an address of size ADDR_SIZE from BUF, and verify that it
196 doesn't extend past BUF_END. */
199 dwarf2_read_address (gdb_byte
*buf
, gdb_byte
*buf_end
, int addr_size
)
203 if (buf_end
- buf
< addr_size
)
204 error (_("dwarf2_read_address: Corrupted DWARF expression."));
206 /* For most architectures, calling extract_unsigned_integer() alone
207 is sufficient for extracting an address. However, some
208 architectures (e.g. MIPS) use signed addresses and using
209 extract_unsigned_integer() will not produce a correct
210 result. Turning the unsigned integer into a value and then
211 decomposing that value as an address will cause
212 gdbarch_integer_to_address() to be invoked for those
213 architectures which require it. Thus, using value_as_address()
214 will produce the correct result for both types of architectures.
216 One concern regarding the use of values for this purpose is
217 efficiency. Obviously, these extra calls will take more time to
218 execute and creating a value takes more space, space which will
219 have to be garbage collected at a later time. If constructing
220 and then decomposing a value for this purpose proves to be too
221 inefficient, then gdbarch_integer_to_address() can be called
224 The use of `unsigned_address_type' in the code below refers to
225 the type of buf and has no bearing on the signedness of the
226 address being returned. */
228 result
= value_as_address (value_from_longest
229 (unsigned_address_type (addr_size
),
230 extract_unsigned_integer (buf
, addr_size
)));
234 /* Return the type of an address of size ADDR_SIZE,
235 for unsigned arithmetic. */
238 unsigned_address_type (int addr_size
)
243 return builtin_type_uint16
;
245 return builtin_type_uint32
;
247 return builtin_type_uint64
;
249 internal_error (__FILE__
, __LINE__
,
250 _("Unsupported address size.\n"));
254 /* Return the type of an address of size ADDR_SIZE,
255 for signed arithmetic. */
258 signed_address_type (int addr_size
)
263 return builtin_type_int16
;
265 return builtin_type_int32
;
267 return builtin_type_int64
;
269 internal_error (__FILE__
, __LINE__
,
270 _("Unsupported address size.\n"));
274 /* The engine for the expression evaluator. Using the context in CTX,
275 evaluate the expression between OP_PTR and OP_END. */
278 execute_stack_op (struct dwarf_expr_context
*ctx
,
279 gdb_byte
*op_ptr
, gdb_byte
*op_end
)
282 ctx
->initialized
= 1; /* Default is initialized. */
284 while (op_ptr
< op_end
)
286 enum dwarf_location_atom op
= *op_ptr
++;
288 ULONGEST uoffset
, reg
;
325 result
= op
- DW_OP_lit0
;
329 result
= dwarf2_read_address (op_ptr
, op_end
, ctx
->addr_size
);
330 op_ptr
+= ctx
->addr_size
;
334 result
= extract_unsigned_integer (op_ptr
, 1);
338 result
= extract_signed_integer (op_ptr
, 1);
342 result
= extract_unsigned_integer (op_ptr
, 2);
346 result
= extract_signed_integer (op_ptr
, 2);
350 result
= extract_unsigned_integer (op_ptr
, 4);
354 result
= extract_signed_integer (op_ptr
, 4);
358 result
= extract_unsigned_integer (op_ptr
, 8);
362 result
= extract_signed_integer (op_ptr
, 8);
366 op_ptr
= read_uleb128 (op_ptr
, op_end
, &uoffset
);
370 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
374 /* The DW_OP_reg operations are required to occur alone in
375 location expressions. */
409 && *op_ptr
!= DW_OP_piece
410 && *op_ptr
!= DW_OP_GNU_uninit
)
411 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
412 "used either alone or in conjuction with DW_OP_piece."));
414 result
= op
- DW_OP_reg0
;
420 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
421 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
)
422 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
423 "used either alone or in conjuction with DW_OP_piece."));
462 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
463 result
= (ctx
->read_reg
) (ctx
->baton
, op
- DW_OP_breg0
);
469 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
470 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
471 result
= (ctx
->read_reg
) (ctx
->baton
, reg
);
479 unsigned int before_stack_len
;
481 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
482 /* Rather than create a whole new context, we simply
483 record the stack length before execution, then reset it
484 afterwards, effectively erasing whatever the recursive
486 before_stack_len
= ctx
->stack_len
;
487 /* FIXME: cagney/2003-03-26: This code should be using
488 get_frame_base_address(), and then implement a dwarf2
489 specific this_base method. */
490 (ctx
->get_frame_base
) (ctx
->baton
, &datastart
, &datalen
);
491 dwarf_expr_eval (ctx
, datastart
, datalen
);
492 result
= dwarf_expr_fetch (ctx
, 0);
494 result
= (ctx
->read_reg
) (ctx
->baton
, result
);
495 result
= result
+ offset
;
496 ctx
->stack_len
= before_stack_len
;
501 result
= dwarf_expr_fetch (ctx
, 0);
505 dwarf_expr_pop (ctx
);
510 result
= dwarf_expr_fetch (ctx
, offset
);
514 result
= dwarf_expr_fetch (ctx
, 1);
519 CORE_ADDR t1
, t2
, t3
;
521 if (ctx
->stack_len
< 3)
522 error (_("Not enough elements for DW_OP_rot. Need 3, have %d."),
524 t1
= ctx
->stack
[ctx
->stack_len
- 1];
525 t2
= ctx
->stack
[ctx
->stack_len
- 2];
526 t3
= ctx
->stack
[ctx
->stack_len
- 3];
527 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
528 ctx
->stack
[ctx
->stack_len
- 2] = t3
;
529 ctx
->stack
[ctx
->stack_len
- 3] = t1
;
534 case DW_OP_deref_size
:
538 case DW_OP_plus_uconst
:
539 /* Unary operations. */
540 result
= dwarf_expr_fetch (ctx
, 0);
541 dwarf_expr_pop (ctx
);
547 gdb_byte
*buf
= alloca (ctx
->addr_size
);
548 (ctx
->read_mem
) (ctx
->baton
, buf
, result
, ctx
->addr_size
);
549 result
= dwarf2_read_address (buf
, buf
+ ctx
->addr_size
,
554 case DW_OP_deref_size
:
556 int addr_size
= *op_ptr
++;
557 gdb_byte
*buf
= alloca (addr_size
);
558 (ctx
->read_mem
) (ctx
->baton
, buf
, result
, addr_size
);
559 result
= dwarf2_read_address (buf
, buf
+ addr_size
,
565 if ((signed int) result
< 0)
574 case DW_OP_plus_uconst
:
575 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
599 /* Binary operations. Use the value engine to do computations in
601 CORE_ADDR first
, second
;
602 enum exp_opcode binop
;
603 struct value
*val1
, *val2
;
605 second
= dwarf_expr_fetch (ctx
, 0);
606 dwarf_expr_pop (ctx
);
608 first
= dwarf_expr_fetch (ctx
, 0);
609 dwarf_expr_pop (ctx
);
611 val1
= value_from_longest
612 (unsigned_address_type (ctx
->addr_size
), first
);
613 val2
= value_from_longest
614 (unsigned_address_type (ctx
->addr_size
), second
);
619 binop
= BINOP_BITWISE_AND
;
634 binop
= BINOP_BITWISE_IOR
;
647 val1
= value_from_longest
648 (signed_address_type (ctx
->addr_size
), first
);
651 binop
= BINOP_BITWISE_XOR
;
669 binop
= BINOP_NOTEQUAL
;
672 internal_error (__FILE__
, __LINE__
,
673 _("Can't be reached."));
675 result
= value_as_long (value_binop (val1
, val2
, binop
));
679 case DW_OP_GNU_push_tls_address
:
680 /* Variable is at a constant offset in the thread-local
681 storage block into the objfile for the current thread and
682 the dynamic linker module containing this expression. Here
683 we return returns the offset from that base. The top of the
684 stack has the offset from the beginning of the thread
685 control block at which the variable is located. Nothing
686 should follow this operator, so the top of stack would be
688 result
= dwarf_expr_fetch (ctx
, 0);
689 dwarf_expr_pop (ctx
);
690 result
= (ctx
->get_tls_address
) (ctx
->baton
, result
);
694 offset
= extract_signed_integer (op_ptr
, 2);
700 offset
= extract_signed_integer (op_ptr
, 2);
702 if (dwarf_expr_fetch (ctx
, 0) != 0)
704 dwarf_expr_pop (ctx
);
713 CORE_ADDR addr_or_regnum
;
715 /* Record the piece. */
716 op_ptr
= read_uleb128 (op_ptr
, op_end
, &size
);
717 addr_or_regnum
= dwarf_expr_fetch (ctx
, 0);
718 add_piece (ctx
, ctx
->in_reg
, addr_or_regnum
, size
);
720 /* Pop off the address/regnum, and clear the in_reg flag. */
721 dwarf_expr_pop (ctx
);
726 case DW_OP_GNU_uninit
:
727 if (op_ptr
!= op_end
)
728 error (_("DWARF-2 expression error: DW_OP_GNU_unint must always "
729 "be the very last op."));
731 ctx
->initialized
= 0;
735 error (_("Unhandled dwarf expression opcode 0x%x"), op
);
738 /* Most things push a result value. */
739 dwarf_expr_push (ctx
, result
);