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
));
48 /* Release the memory allocated to CTX. */
51 free_dwarf_expr_context (struct dwarf_expr_context
*ctx
)
57 /* Expand the memory allocated to CTX's stack to contain at least
58 NEED more elements than are currently used. */
61 dwarf_expr_grow_stack (struct dwarf_expr_context
*ctx
, size_t need
)
63 if (ctx
->stack_len
+ need
> ctx
->stack_allocated
)
65 size_t newlen
= ctx
->stack_len
+ need
+ 10;
66 ctx
->stack
= xrealloc (ctx
->stack
,
67 newlen
* sizeof (CORE_ADDR
));
68 ctx
->stack_allocated
= newlen
;
72 /* Push VALUE onto CTX's stack. */
75 dwarf_expr_push (struct dwarf_expr_context
*ctx
, CORE_ADDR value
)
77 dwarf_expr_grow_stack (ctx
, 1);
78 ctx
->stack
[ctx
->stack_len
++] = value
;
81 /* Pop the top item off of CTX's stack. */
84 dwarf_expr_pop (struct dwarf_expr_context
*ctx
)
86 if (ctx
->stack_len
<= 0)
87 error ("dwarf expression stack underflow");
91 /* Retrieve the N'th item on CTX's stack. */
94 dwarf_expr_fetch (struct dwarf_expr_context
*ctx
, int n
)
96 if (ctx
->stack_len
< n
)
97 error ("Asked for position %d of stack, stack only has %d elements on it\n",
99 return ctx
->stack
[ctx
->stack_len
- (1 + n
)];
103 /* Evaluate the expression at ADDR (LEN bytes long) using the context
107 dwarf_expr_eval (struct dwarf_expr_context
*ctx
, unsigned char *addr
,
110 execute_stack_op (ctx
, addr
, addr
+ len
);
113 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
114 by R, and return the new value of BUF. Verify that it doesn't extend
118 read_uleb128 (unsigned char *buf
, unsigned char *buf_end
, ULONGEST
* r
)
127 error ("read_uleb128: Corrupted DWARF expression.");
130 result
|= (byte
& 0x7f) << shift
;
131 if ((byte
& 0x80) == 0)
139 /* Decode the signed 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_sleb128 (unsigned char *buf
, unsigned char *buf_end
, LONGEST
* r
)
153 error ("read_sleb128: Corrupted DWARF expression.");
156 result
|= (byte
& 0x7f) << shift
;
158 if ((byte
& 0x80) == 0)
161 if (shift
< (sizeof (*r
) * 8) && (byte
& 0x40) != 0)
162 result
|= -(1 << shift
);
168 /* Read an address from BUF, and verify that it doesn't extend past
169 BUF_END. The address is returned, and *BYTES_READ is set to the
170 number of bytes read from BUF. */
173 dwarf2_read_address (unsigned char *buf
, unsigned char *buf_end
, int *bytes_read
)
177 if (buf_end
- buf
< TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
)
178 error ("dwarf2_read_address: Corrupted DWARF expression.");
180 *bytes_read
= TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
;
181 result
= extract_address (buf
, TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
);
185 /* Return the type of an address, for unsigned arithmetic. */
188 unsigned_address_type (void)
190 switch (TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
)
193 return builtin_type_uint16
;
195 return builtin_type_uint32
;
197 return builtin_type_uint64
;
199 internal_error (__FILE__
, __LINE__
,
200 "Unsupported address size.\n");
204 /* Return the type of an address, for signed arithmetic. */
207 signed_address_type (void)
209 switch (TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
)
212 return builtin_type_int16
;
214 return builtin_type_int32
;
216 return builtin_type_int64
;
218 internal_error (__FILE__
, __LINE__
,
219 "Unsupported address size.\n");
223 /* The engine for the expression evaluator. Using the context in CTX,
224 evaluate the expression between OP_PTR and OP_END. */
227 execute_stack_op (struct dwarf_expr_context
*ctx
, unsigned char *op_ptr
,
228 unsigned char *op_end
)
232 while (op_ptr
< op_end
)
234 enum dwarf_location_atom op
= *op_ptr
++;
236 ULONGEST uoffset
, reg
;
274 result
= op
- DW_OP_lit0
;
278 result
= dwarf2_read_address (op_ptr
, op_end
, &bytes_read
);
279 op_ptr
+= bytes_read
;
283 result
= extract_unsigned_integer (op_ptr
, 1);
287 result
= extract_signed_integer (op_ptr
, 1);
291 result
= extract_unsigned_integer (op_ptr
, 2);
295 result
= extract_signed_integer (op_ptr
, 2);
299 result
= extract_unsigned_integer (op_ptr
, 4);
303 result
= extract_signed_integer (op_ptr
, 4);
307 result
= extract_unsigned_integer (op_ptr
, 8);
311 result
= extract_signed_integer (op_ptr
, 8);
315 op_ptr
= read_uleb128 (op_ptr
, op_end
, &uoffset
);
319 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
323 /* The DW_OP_reg operations are required to occur alone in
324 location expressions. */
357 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
)
358 error ("DWARF-2 expression error: DW_OP_reg operations must be "
359 "used either alone or in conjuction with DW_OP_piece.");
361 result
= op
- DW_OP_reg0
;
367 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
368 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
)
369 error ("DWARF-2 expression error: DW_OP_reg operations must be "
370 "used either alone or in conjuction with DW_OP_piece.");
409 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
410 result
= (ctx
->read_reg
) (ctx
->baton
, op
- DW_OP_breg0
);
416 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
417 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
418 result
= (ctx
->read_reg
) (ctx
->baton
, reg
);
424 unsigned char *datastart
;
426 unsigned int before_stack_len
;
428 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
429 /* Rather than create a whole new context, we simply
430 record the stack length before execution, then reset it
431 afterwards, effectively erasing whatever the recursive
433 before_stack_len
= ctx
->stack_len
;
434 /* FIXME: cagney/2003-03-26: This code should be using
435 get_frame_base_address(), and then implement a dwarf2
436 specific this_base method. */
437 (ctx
->get_frame_base
) (ctx
->baton
, &datastart
, &datalen
);
438 dwarf_expr_eval (ctx
, datastart
, datalen
);
439 result
= dwarf_expr_fetch (ctx
, 0);
441 result
= (ctx
->read_reg
) (ctx
->baton
, result
);
444 char *buf
= alloca (TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
);
447 (ctx
->read_mem
) (ctx
->baton
, buf
, result
,
448 TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
);
449 result
= dwarf2_read_address (buf
,
450 buf
+ (TARGET_ADDR_BIT
454 result
= result
+ offset
;
455 ctx
->stack_len
= before_stack_len
;
460 result
= dwarf_expr_fetch (ctx
, 0);
464 dwarf_expr_pop (ctx
);
469 result
= dwarf_expr_fetch (ctx
, offset
);
473 result
= dwarf_expr_fetch (ctx
, 1);
478 CORE_ADDR t1
, t2
, t3
;
480 if (ctx
->stack_len
< 3)
481 error ("Not enough elements for DW_OP_rot. Need 3, have %d\n",
483 t1
= ctx
->stack
[ctx
->stack_len
- 1];
484 t2
= ctx
->stack
[ctx
->stack_len
- 2];
485 t3
= ctx
->stack
[ctx
->stack_len
- 3];
486 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
487 ctx
->stack
[ctx
->stack_len
- 2] = t3
;
488 ctx
->stack
[ctx
->stack_len
- 3] = t1
;
493 case DW_OP_deref_size
:
497 case DW_OP_plus_uconst
:
498 /* Unary operations. */
499 result
= dwarf_expr_fetch (ctx
, 0);
500 dwarf_expr_pop (ctx
);
506 char *buf
= alloca (TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
);
509 (ctx
->read_mem
) (ctx
->baton
, buf
, result
,
510 TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
);
511 result
= dwarf2_read_address (buf
,
512 buf
+ (TARGET_ADDR_BIT
518 case DW_OP_deref_size
:
520 char *buf
= alloca (TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
);
523 (ctx
->read_mem
) (ctx
->baton
, buf
, result
, *op_ptr
++);
524 result
= dwarf2_read_address (buf
,
525 buf
+ (TARGET_ADDR_BIT
532 if ((signed int) result
< 0)
541 case DW_OP_plus_uconst
:
542 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
566 /* Binary operations. Use the value engine to do computations in
568 CORE_ADDR first
, second
;
569 enum exp_opcode binop
;
570 struct value
*val1
, *val2
;
572 second
= dwarf_expr_fetch (ctx
, 0);
573 dwarf_expr_pop (ctx
);
575 first
= dwarf_expr_fetch (ctx
, 1);
576 dwarf_expr_pop (ctx
);
578 val1
= value_from_longest (unsigned_address_type (), first
);
579 val2
= value_from_longest (unsigned_address_type (), second
);
584 binop
= BINOP_BITWISE_AND
;
598 binop
= BINOP_BITWISE_IOR
;
610 val1
= value_from_longest (signed_address_type (), first
);
613 binop
= BINOP_BITWISE_XOR
;
631 binop
= BINOP_NOTEQUAL
;
634 internal_error (__FILE__
, __LINE__
,
635 "Can't be reached.");
637 result
= value_as_long (value_binop (val1
, val2
, binop
));
641 case DW_OP_GNU_push_tls_address
:
642 result
= dwarf_expr_fetch (ctx
, 0);
643 dwarf_expr_pop (ctx
);
644 result
= (ctx
->get_tls_address
) (ctx
->baton
, result
);
648 offset
= extract_signed_integer (op_ptr
, 2);
654 offset
= extract_signed_integer (op_ptr
, 2);
656 if (dwarf_expr_fetch (ctx
, 0) != 0)
658 dwarf_expr_pop (ctx
);
665 error ("Unhandled dwarf expression opcode");
668 /* Most things push a result value. */
669 dwarf_expr_push (ctx
, result
);