1 /* DWARF 2 Expression Evaluator.
3 Copyright (C) 2001, 2002, 2003, 2005, 2007 Free Software Foundation, Inc.
5 Contributed by Daniel Berlin (dan@dberlin.org)
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "elf/dwarf2.h"
28 #include "dwarf2expr.h"
30 /* Local prototypes. */
32 static void execute_stack_op (struct dwarf_expr_context
*,
33 gdb_byte
*, gdb_byte
*);
34 static struct type
*unsigned_address_type (void);
36 /* Create a new context for the expression evaluator. */
38 struct dwarf_expr_context
*
39 new_dwarf_expr_context (void)
41 struct dwarf_expr_context
*retval
;
42 retval
= xcalloc (1, sizeof (struct dwarf_expr_context
));
43 retval
->stack_len
= 0;
44 retval
->stack_allocated
= 10;
45 retval
->stack
= xmalloc (retval
->stack_allocated
* sizeof (CORE_ADDR
));
46 retval
->num_pieces
= 0;
51 /* Release the memory allocated to CTX. */
54 free_dwarf_expr_context (struct dwarf_expr_context
*ctx
)
61 /* Expand the memory allocated to CTX's stack to contain at least
62 NEED more elements than are currently used. */
65 dwarf_expr_grow_stack (struct dwarf_expr_context
*ctx
, size_t need
)
67 if (ctx
->stack_len
+ need
> ctx
->stack_allocated
)
69 size_t newlen
= ctx
->stack_len
+ need
+ 10;
70 ctx
->stack
= xrealloc (ctx
->stack
,
71 newlen
* sizeof (CORE_ADDR
));
72 ctx
->stack_allocated
= newlen
;
76 /* Push VALUE onto CTX's stack. */
79 dwarf_expr_push (struct dwarf_expr_context
*ctx
, CORE_ADDR value
)
81 dwarf_expr_grow_stack (ctx
, 1);
82 ctx
->stack
[ctx
->stack_len
++] = value
;
85 /* Pop the top item off of CTX's stack. */
88 dwarf_expr_pop (struct dwarf_expr_context
*ctx
)
90 if (ctx
->stack_len
<= 0)
91 error (_("dwarf expression stack underflow"));
95 /* Retrieve the N'th item on CTX's stack. */
98 dwarf_expr_fetch (struct dwarf_expr_context
*ctx
, int n
)
100 if (ctx
->stack_len
<= n
)
101 error (_("Asked for position %d of stack, stack only has %d elements on it."),
103 return ctx
->stack
[ctx
->stack_len
- (1 + n
)];
107 /* Add a new piece to CTX's piece list. */
109 add_piece (struct dwarf_expr_context
*ctx
,
110 int in_reg
, CORE_ADDR value
, ULONGEST size
)
112 struct dwarf_expr_piece
*p
;
117 ctx
->pieces
= xrealloc (ctx
->pieces
,
119 * sizeof (struct dwarf_expr_piece
)));
121 ctx
->pieces
= xmalloc (ctx
->num_pieces
122 * sizeof (struct dwarf_expr_piece
));
124 p
= &ctx
->pieces
[ctx
->num_pieces
- 1];
130 /* Evaluate the expression at ADDR (LEN bytes long) using the context
134 dwarf_expr_eval (struct dwarf_expr_context
*ctx
, gdb_byte
*addr
, size_t len
)
136 execute_stack_op (ctx
, addr
, addr
+ len
);
139 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
140 by R, and return the new value of BUF. Verify that it doesn't extend
144 read_uleb128 (gdb_byte
*buf
, gdb_byte
*buf_end
, ULONGEST
* r
)
153 error (_("read_uleb128: Corrupted DWARF expression."));
156 result
|= (byte
& 0x7f) << shift
;
157 if ((byte
& 0x80) == 0)
165 /* Decode the signed LEB128 constant at BUF into the variable pointed to
166 by R, and return the new value of BUF. Verify that it doesn't extend
170 read_sleb128 (gdb_byte
*buf
, gdb_byte
*buf_end
, LONGEST
* r
)
179 error (_("read_sleb128: Corrupted DWARF expression."));
182 result
|= (byte
& 0x7f) << shift
;
184 if ((byte
& 0x80) == 0)
187 if (shift
< (sizeof (*r
) * 8) && (byte
& 0x40) != 0)
188 result
|= -(1 << shift
);
194 /* Read an address from BUF, and verify that it doesn't extend past
195 BUF_END. The address is returned, and *BYTES_READ is set to the
196 number of bytes read from BUF. */
199 dwarf2_read_address (gdb_byte
*buf
, gdb_byte
*buf_end
, int *bytes_read
)
203 if (buf_end
- buf
< gdbarch_addr_bit (current_gdbarch
) / TARGET_CHAR_BIT
)
204 error (_("dwarf2_read_address: Corrupted DWARF expression."));
206 *bytes_read
= gdbarch_addr_bit (current_gdbarch
) / TARGET_CHAR_BIT
;
208 /* For most architectures, calling extract_unsigned_integer() alone
209 is sufficient for extracting an address. However, some
210 architectures (e.g. MIPS) use signed addresses and using
211 extract_unsigned_integer() will not produce a correct
212 result. Turning the unsigned integer into a value and then
213 decomposing that value as an address will cause
214 gdbarch_integer_to_address() to be invoked for those
215 architectures which require it. Thus, using value_as_address()
216 will produce the correct result for both types of architectures.
218 One concern regarding the use of values for this purpose is
219 efficiency. Obviously, these extra calls will take more time to
220 execute and creating a value takes more space, space which will
221 have to be garbage collected at a later time. If constructing
222 and then decomposing a value for this purpose proves to be too
223 inefficient, then gdbarch_integer_to_address() can be called
226 The use of `unsigned_address_type' in the code below refers to
227 the type of buf and has no bearing on the signedness of the
228 address being returned. */
230 result
= value_as_address (value_from_longest
231 (unsigned_address_type (),
232 extract_unsigned_integer
234 gdbarch_addr_bit (current_gdbarch
)
235 / TARGET_CHAR_BIT
)));
240 /* Return the type of an address, for unsigned arithmetic. */
243 unsigned_address_type (void)
245 switch (gdbarch_addr_bit (current_gdbarch
) / TARGET_CHAR_BIT
)
248 return builtin_type_uint16
;
250 return builtin_type_uint32
;
252 return builtin_type_uint64
;
254 internal_error (__FILE__
, __LINE__
,
255 _("Unsupported address size.\n"));
259 /* Return the type of an address, for signed arithmetic. */
262 signed_address_type (void)
264 switch (gdbarch_addr_bit (current_gdbarch
) / TARGET_CHAR_BIT
)
267 return builtin_type_int16
;
269 return builtin_type_int32
;
271 return builtin_type_int64
;
273 internal_error (__FILE__
, __LINE__
,
274 _("Unsupported address size.\n"));
278 /* The engine for the expression evaluator. Using the context in CTX,
279 evaluate the expression between OP_PTR and OP_END. */
282 execute_stack_op (struct dwarf_expr_context
*ctx
,
283 gdb_byte
*op_ptr
, gdb_byte
*op_end
)
286 ctx
->initialized
= 1; /* Default is initialized. */
288 while (op_ptr
< op_end
)
290 enum dwarf_location_atom op
= *op_ptr
++;
292 ULONGEST uoffset
, reg
;
330 result
= op
- DW_OP_lit0
;
334 result
= dwarf2_read_address (op_ptr
, op_end
, &bytes_read
);
335 op_ptr
+= bytes_read
;
339 result
= extract_unsigned_integer (op_ptr
, 1);
343 result
= extract_signed_integer (op_ptr
, 1);
347 result
= extract_unsigned_integer (op_ptr
, 2);
351 result
= extract_signed_integer (op_ptr
, 2);
355 result
= extract_unsigned_integer (op_ptr
, 4);
359 result
= extract_signed_integer (op_ptr
, 4);
363 result
= extract_unsigned_integer (op_ptr
, 8);
367 result
= extract_signed_integer (op_ptr
, 8);
371 op_ptr
= read_uleb128 (op_ptr
, op_end
, &uoffset
);
375 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
379 /* The DW_OP_reg operations are required to occur alone in
380 location expressions. */
414 && *op_ptr
!= DW_OP_piece
415 && *op_ptr
!= DW_OP_GNU_uninit
)
416 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
417 "used either alone or in conjuction with DW_OP_piece."));
419 result
= op
- DW_OP_reg0
;
425 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
426 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
)
427 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
428 "used either alone or in conjuction with DW_OP_piece."));
467 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
468 result
= (ctx
->read_reg
) (ctx
->baton
, op
- DW_OP_breg0
);
474 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
475 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
476 result
= (ctx
->read_reg
) (ctx
->baton
, reg
);
484 unsigned int before_stack_len
;
486 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
487 /* Rather than create a whole new context, we simply
488 record the stack length before execution, then reset it
489 afterwards, effectively erasing whatever the recursive
491 before_stack_len
= ctx
->stack_len
;
492 /* FIXME: cagney/2003-03-26: This code should be using
493 get_frame_base_address(), and then implement a dwarf2
494 specific this_base method. */
495 (ctx
->get_frame_base
) (ctx
->baton
, &datastart
, &datalen
);
496 dwarf_expr_eval (ctx
, datastart
, datalen
);
497 result
= dwarf_expr_fetch (ctx
, 0);
499 result
= (ctx
->read_reg
) (ctx
->baton
, result
);
500 result
= result
+ offset
;
501 ctx
->stack_len
= before_stack_len
;
506 result
= dwarf_expr_fetch (ctx
, 0);
510 dwarf_expr_pop (ctx
);
515 result
= dwarf_expr_fetch (ctx
, offset
);
519 result
= dwarf_expr_fetch (ctx
, 1);
524 CORE_ADDR t1
, t2
, t3
;
526 if (ctx
->stack_len
< 3)
527 error (_("Not enough elements for DW_OP_rot. Need 3, have %d."),
529 t1
= ctx
->stack
[ctx
->stack_len
- 1];
530 t2
= ctx
->stack
[ctx
->stack_len
- 2];
531 t3
= ctx
->stack
[ctx
->stack_len
- 3];
532 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
533 ctx
->stack
[ctx
->stack_len
- 2] = t3
;
534 ctx
->stack
[ctx
->stack_len
- 3] = t1
;
539 case DW_OP_deref_size
:
543 case DW_OP_plus_uconst
:
544 /* Unary operations. */
545 result
= dwarf_expr_fetch (ctx
, 0);
546 dwarf_expr_pop (ctx
);
552 gdb_byte
*buf
= alloca (gdbarch_addr_bit (current_gdbarch
)
556 (ctx
->read_mem
) (ctx
->baton
, buf
, result
,
557 gdbarch_addr_bit (current_gdbarch
)
559 result
= dwarf2_read_address (buf
,
560 buf
+ (gdbarch_addr_bit
567 case DW_OP_deref_size
:
570 = alloca (gdbarch_addr_bit (current_gdbarch
)
574 (ctx
->read_mem
) (ctx
->baton
, buf
, result
, *op_ptr
++);
575 result
= dwarf2_read_address (buf
,
576 buf
+ (gdbarch_addr_bit
584 if ((signed int) result
< 0)
593 case DW_OP_plus_uconst
:
594 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
618 /* Binary operations. Use the value engine to do computations in
620 CORE_ADDR first
, second
;
621 enum exp_opcode binop
;
622 struct value
*val1
, *val2
;
624 second
= dwarf_expr_fetch (ctx
, 0);
625 dwarf_expr_pop (ctx
);
627 first
= dwarf_expr_fetch (ctx
, 0);
628 dwarf_expr_pop (ctx
);
630 val1
= value_from_longest (unsigned_address_type (), first
);
631 val2
= value_from_longest (unsigned_address_type (), second
);
636 binop
= BINOP_BITWISE_AND
;
651 binop
= BINOP_BITWISE_IOR
;
664 val1
= value_from_longest (signed_address_type (), first
);
667 binop
= BINOP_BITWISE_XOR
;
685 binop
= BINOP_NOTEQUAL
;
688 internal_error (__FILE__
, __LINE__
,
689 _("Can't be reached."));
691 result
= value_as_long (value_binop (val1
, val2
, binop
));
695 case DW_OP_GNU_push_tls_address
:
696 /* Variable is at a constant offset in the thread-local
697 storage block into the objfile for the current thread and
698 the dynamic linker module containing this expression. Here
699 we return returns the offset from that base. The top of the
700 stack has the offset from the beginning of the thread
701 control block at which the variable is located. Nothing
702 should follow this operator, so the top of stack would be
704 result
= dwarf_expr_fetch (ctx
, 0);
705 dwarf_expr_pop (ctx
);
706 result
= (ctx
->get_tls_address
) (ctx
->baton
, result
);
710 offset
= extract_signed_integer (op_ptr
, 2);
716 offset
= extract_signed_integer (op_ptr
, 2);
718 if (dwarf_expr_fetch (ctx
, 0) != 0)
720 dwarf_expr_pop (ctx
);
729 CORE_ADDR addr_or_regnum
;
731 /* Record the piece. */
732 op_ptr
= read_uleb128 (op_ptr
, op_end
, &size
);
733 addr_or_regnum
= dwarf_expr_fetch (ctx
, 0);
734 add_piece (ctx
, ctx
->in_reg
, addr_or_regnum
, size
);
736 /* Pop off the address/regnum, and clear the in_reg flag. */
737 dwarf_expr_pop (ctx
);
742 case DW_OP_GNU_uninit
:
743 if (op_ptr
!= op_end
)
744 error (_("DWARF-2 expression error: DW_OP_GNU_unint must always "
745 "be the very last op."));
747 ctx
->initialized
= 0;
751 error (_("Unhandled dwarf expression opcode 0x%x"), op
);
754 /* Most things push a result value. */
755 dwarf_expr_push (ctx
, result
);