1 /* Dwarf2 Expression Evaluator
2 Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin (dan@dberlin.org)
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
27 #include "elf/dwarf2.h"
28 #include "dwarf2expr.h"
30 /* Local prototypes. */
32 static void execute_stack_op (struct dwarf_expr_context
*,
33 unsigned char *, unsigned char *);
35 /* Create a new context for the expression evaluator. */
37 struct dwarf_expr_context
*
38 new_dwarf_expr_context (void)
40 struct dwarf_expr_context
*retval
;
41 retval
= xcalloc (1, sizeof (struct dwarf_expr_context
));
42 retval
->stack_len
= 0;
43 retval
->stack_allocated
= 10;
44 retval
->stack
= xmalloc (retval
->stack_allocated
* sizeof (CORE_ADDR
));
45 retval
->num_pieces
= 0;
50 /* Release the memory allocated to CTX. */
53 free_dwarf_expr_context (struct dwarf_expr_context
*ctx
)
60 /* Expand the memory allocated to CTX's stack to contain at least
61 NEED more elements than are currently used. */
64 dwarf_expr_grow_stack (struct dwarf_expr_context
*ctx
, size_t need
)
66 if (ctx
->stack_len
+ need
> ctx
->stack_allocated
)
68 size_t newlen
= ctx
->stack_len
+ need
+ 10;
69 ctx
->stack
= xrealloc (ctx
->stack
,
70 newlen
* sizeof (CORE_ADDR
));
71 ctx
->stack_allocated
= newlen
;
75 /* Push VALUE onto CTX's stack. */
78 dwarf_expr_push (struct dwarf_expr_context
*ctx
, CORE_ADDR value
)
80 dwarf_expr_grow_stack (ctx
, 1);
81 ctx
->stack
[ctx
->stack_len
++] = value
;
84 /* Pop the top item off of CTX's stack. */
87 dwarf_expr_pop (struct dwarf_expr_context
*ctx
)
89 if (ctx
->stack_len
<= 0)
90 error (_("dwarf expression stack underflow"));
94 /* Retrieve the N'th item on CTX's stack. */
97 dwarf_expr_fetch (struct dwarf_expr_context
*ctx
, int n
)
99 if (ctx
->stack_len
< n
)
100 error (_("Asked for position %d of stack, stack only has %d elements on it."),
102 return ctx
->stack
[ctx
->stack_len
- (1 + n
)];
106 /* Add a new piece to CTX's piece list. */
108 add_piece (struct dwarf_expr_context
*ctx
,
109 int in_reg
, CORE_ADDR value
, ULONGEST size
)
111 struct dwarf_expr_piece
*p
;
116 ctx
->pieces
= xrealloc (ctx
->pieces
,
118 * sizeof (struct dwarf_expr_piece
)));
120 ctx
->pieces
= xmalloc (ctx
->num_pieces
121 * sizeof (struct dwarf_expr_piece
));
123 p
= &ctx
->pieces
[ctx
->num_pieces
- 1];
129 /* Evaluate the expression at ADDR (LEN bytes long) using the context
133 dwarf_expr_eval (struct dwarf_expr_context
*ctx
, unsigned char *addr
,
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 (unsigned char *buf
, unsigned char *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 (unsigned char *buf
, unsigned char *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 (unsigned char *buf
, unsigned char *buf_end
, int *bytes_read
)
203 if (buf_end
- buf
< TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
)
204 error (_("dwarf2_read_address: Corrupted DWARF expression."));
206 *bytes_read
= TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
;
207 /* NOTE: cagney/2003-05-22: This extract is assuming that a DWARF 2
208 address is always unsigned. That may or may not be true. */
209 result
= extract_unsigned_integer (buf
, TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
);
213 /* Return the type of an address, for unsigned arithmetic. */
216 unsigned_address_type (void)
218 switch (TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
)
221 return builtin_type_uint16
;
223 return builtin_type_uint32
;
225 return builtin_type_uint64
;
227 internal_error (__FILE__
, __LINE__
,
228 _("Unsupported address size.\n"));
232 /* Return the type of an address, for signed arithmetic. */
235 signed_address_type (void)
237 switch (TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
)
240 return builtin_type_int16
;
242 return builtin_type_int32
;
244 return builtin_type_int64
;
246 internal_error (__FILE__
, __LINE__
,
247 _("Unsupported address size.\n"));
251 /* The engine for the expression evaluator. Using the context in CTX,
252 evaluate the expression between OP_PTR and OP_END. */
255 execute_stack_op (struct dwarf_expr_context
*ctx
, unsigned char *op_ptr
,
256 unsigned char *op_end
)
260 while (op_ptr
< op_end
)
262 enum dwarf_location_atom op
= *op_ptr
++;
264 ULONGEST uoffset
, reg
;
302 result
= op
- DW_OP_lit0
;
306 result
= dwarf2_read_address (op_ptr
, op_end
, &bytes_read
);
307 op_ptr
+= bytes_read
;
311 result
= extract_unsigned_integer (op_ptr
, 1);
315 result
= extract_signed_integer (op_ptr
, 1);
319 result
= extract_unsigned_integer (op_ptr
, 2);
323 result
= extract_signed_integer (op_ptr
, 2);
327 result
= extract_unsigned_integer (op_ptr
, 4);
331 result
= extract_signed_integer (op_ptr
, 4);
335 result
= extract_unsigned_integer (op_ptr
, 8);
339 result
= extract_signed_integer (op_ptr
, 8);
343 op_ptr
= read_uleb128 (op_ptr
, op_end
, &uoffset
);
347 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
351 /* The DW_OP_reg operations are required to occur alone in
352 location expressions. */
385 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
)
386 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
387 "used either alone or in conjuction with DW_OP_piece."));
389 result
= op
- DW_OP_reg0
;
395 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
396 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
)
397 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
398 "used either alone or in conjuction with DW_OP_piece."));
437 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
438 result
= (ctx
->read_reg
) (ctx
->baton
, op
- DW_OP_breg0
);
444 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
445 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
446 result
= (ctx
->read_reg
) (ctx
->baton
, reg
);
452 unsigned char *datastart
;
454 unsigned int before_stack_len
;
456 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
457 /* Rather than create a whole new context, we simply
458 record the stack length before execution, then reset it
459 afterwards, effectively erasing whatever the recursive
461 before_stack_len
= ctx
->stack_len
;
462 /* FIXME: cagney/2003-03-26: This code should be using
463 get_frame_base_address(), and then implement a dwarf2
464 specific this_base method. */
465 (ctx
->get_frame_base
) (ctx
->baton
, &datastart
, &datalen
);
466 dwarf_expr_eval (ctx
, datastart
, datalen
);
467 result
= dwarf_expr_fetch (ctx
, 0);
469 result
= (ctx
->read_reg
) (ctx
->baton
, result
);
470 result
= result
+ offset
;
471 ctx
->stack_len
= before_stack_len
;
476 result
= dwarf_expr_fetch (ctx
, 0);
480 dwarf_expr_pop (ctx
);
485 result
= dwarf_expr_fetch (ctx
, offset
);
489 result
= dwarf_expr_fetch (ctx
, 1);
494 CORE_ADDR t1
, t2
, t3
;
496 if (ctx
->stack_len
< 3)
497 error (_("Not enough elements for DW_OP_rot. Need 3, have %d."),
499 t1
= ctx
->stack
[ctx
->stack_len
- 1];
500 t2
= ctx
->stack
[ctx
->stack_len
- 2];
501 t3
= ctx
->stack
[ctx
->stack_len
- 3];
502 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
503 ctx
->stack
[ctx
->stack_len
- 2] = t3
;
504 ctx
->stack
[ctx
->stack_len
- 3] = t1
;
509 case DW_OP_deref_size
:
513 case DW_OP_plus_uconst
:
514 /* Unary operations. */
515 result
= dwarf_expr_fetch (ctx
, 0);
516 dwarf_expr_pop (ctx
);
522 char *buf
= alloca (TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
);
525 (ctx
->read_mem
) (ctx
->baton
, buf
, result
,
526 TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
);
527 result
= dwarf2_read_address (buf
,
528 buf
+ (TARGET_ADDR_BIT
534 case DW_OP_deref_size
:
536 char *buf
= alloca (TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
);
539 (ctx
->read_mem
) (ctx
->baton
, buf
, result
, *op_ptr
++);
540 result
= dwarf2_read_address (buf
,
541 buf
+ (TARGET_ADDR_BIT
548 if ((signed int) result
< 0)
557 case DW_OP_plus_uconst
:
558 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
582 /* Binary operations. Use the value engine to do computations in
584 CORE_ADDR first
, second
;
585 enum exp_opcode binop
;
586 struct value
*val1
, *val2
;
588 second
= dwarf_expr_fetch (ctx
, 0);
589 dwarf_expr_pop (ctx
);
591 first
= dwarf_expr_fetch (ctx
, 0);
592 dwarf_expr_pop (ctx
);
594 val1
= value_from_longest (unsigned_address_type (), first
);
595 val2
= value_from_longest (unsigned_address_type (), second
);
600 binop
= BINOP_BITWISE_AND
;
615 binop
= BINOP_BITWISE_IOR
;
628 val1
= value_from_longest (signed_address_type (), first
);
631 binop
= BINOP_BITWISE_XOR
;
649 binop
= BINOP_NOTEQUAL
;
652 internal_error (__FILE__
, __LINE__
,
653 _("Can't be reached."));
655 result
= value_as_long (value_binop (val1
, val2
, binop
));
659 case DW_OP_GNU_push_tls_address
:
660 /* Variable is at a constant offset in the thread-local
661 storage block into the objfile for the current thread and
662 the dynamic linker module containing this expression. Here
663 we return returns the offset from that base. The top of the
664 stack has the offset from the beginning of the thread
665 control block at which the variable is located. Nothing
666 should follow this operator, so the top of stack would be
668 result
= dwarf_expr_fetch (ctx
, 0);
669 dwarf_expr_pop (ctx
);
670 result
= (ctx
->get_tls_address
) (ctx
->baton
, result
);
674 offset
= extract_signed_integer (op_ptr
, 2);
680 offset
= extract_signed_integer (op_ptr
, 2);
682 if (dwarf_expr_fetch (ctx
, 0) != 0)
684 dwarf_expr_pop (ctx
);
693 CORE_ADDR addr_or_regnum
;
695 /* Record the piece. */
696 op_ptr
= read_uleb128 (op_ptr
, op_end
, &size
);
697 addr_or_regnum
= dwarf_expr_fetch (ctx
, 0);
698 add_piece (ctx
, ctx
->in_reg
, addr_or_regnum
, size
);
700 /* Pop off the address/regnum, and clear the in_reg flag. */
701 dwarf_expr_pop (ctx
);
707 error (_("Unhandled dwarf expression opcode 0x%x"), op
);
710 /* Most things push a result value. */
711 dwarf_expr_push (ctx
, result
);