1 /* DWARF 2 Expression Evaluator.
3 Copyright (C) 2001, 2002, 2003, 2005 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 2 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, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
29 #include "elf/dwarf2.h"
30 #include "dwarf2expr.h"
32 /* Local prototypes. */
34 static void execute_stack_op (struct dwarf_expr_context
*,
35 gdb_byte
*, 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
;
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 from BUF, and verify that it doesn't extend past
196 BUF_END. The address is returned, and *BYTES_READ is set to the
197 number of bytes read from BUF. */
200 dwarf2_read_address (gdb_byte
*buf
, gdb_byte
*buf_end
, int *bytes_read
)
204 if (buf_end
- buf
< TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
)
205 error (_("dwarf2_read_address: Corrupted DWARF expression."));
207 *bytes_read
= TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
;
208 /* NOTE: cagney/2003-05-22: This extract is assuming that a DWARF 2
209 address is always unsigned. That may or may not be true. */
210 result
= extract_unsigned_integer (buf
, TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
);
214 /* Return the type of an address, for unsigned arithmetic. */
217 unsigned_address_type (void)
219 switch (TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
)
222 return builtin_type_uint16
;
224 return builtin_type_uint32
;
226 return builtin_type_uint64
;
228 internal_error (__FILE__
, __LINE__
,
229 _("Unsupported address size.\n"));
233 /* Return the type of an address, for signed arithmetic. */
236 signed_address_type (void)
238 switch (TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
)
241 return builtin_type_int16
;
243 return builtin_type_int32
;
245 return builtin_type_int64
;
247 internal_error (__FILE__
, __LINE__
,
248 _("Unsupported address size.\n"));
252 /* The engine for the expression evaluator. Using the context in CTX,
253 evaluate the expression between OP_PTR and OP_END. */
256 execute_stack_op (struct dwarf_expr_context
*ctx
,
257 gdb_byte
*op_ptr
, gdb_byte
*op_end
)
261 while (op_ptr
< op_end
)
263 enum dwarf_location_atom op
= *op_ptr
++;
265 ULONGEST uoffset
, reg
;
303 result
= op
- DW_OP_lit0
;
307 result
= dwarf2_read_address (op_ptr
, op_end
, &bytes_read
);
308 op_ptr
+= bytes_read
;
312 result
= extract_unsigned_integer (op_ptr
, 1);
316 result
= extract_signed_integer (op_ptr
, 1);
320 result
= extract_unsigned_integer (op_ptr
, 2);
324 result
= extract_signed_integer (op_ptr
, 2);
328 result
= extract_unsigned_integer (op_ptr
, 4);
332 result
= extract_signed_integer (op_ptr
, 4);
336 result
= extract_unsigned_integer (op_ptr
, 8);
340 result
= extract_signed_integer (op_ptr
, 8);
344 op_ptr
= read_uleb128 (op_ptr
, op_end
, &uoffset
);
348 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
352 /* The DW_OP_reg operations are required to occur alone in
353 location expressions. */
386 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
)
387 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
388 "used either alone or in conjuction with DW_OP_piece."));
390 result
= op
- DW_OP_reg0
;
396 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
397 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
)
398 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
399 "used either alone or in conjuction with DW_OP_piece."));
438 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
439 result
= (ctx
->read_reg
) (ctx
->baton
, op
- DW_OP_breg0
);
445 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
446 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
447 result
= (ctx
->read_reg
) (ctx
->baton
, reg
);
455 unsigned int before_stack_len
;
457 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
458 /* Rather than create a whole new context, we simply
459 record the stack length before execution, then reset it
460 afterwards, effectively erasing whatever the recursive
462 before_stack_len
= ctx
->stack_len
;
463 /* FIXME: cagney/2003-03-26: This code should be using
464 get_frame_base_address(), and then implement a dwarf2
465 specific this_base method. */
466 (ctx
->get_frame_base
) (ctx
->baton
, &datastart
, &datalen
);
467 dwarf_expr_eval (ctx
, datastart
, datalen
);
468 result
= dwarf_expr_fetch (ctx
, 0);
470 result
= (ctx
->read_reg
) (ctx
->baton
, result
);
471 result
= result
+ offset
;
472 ctx
->stack_len
= before_stack_len
;
477 result
= dwarf_expr_fetch (ctx
, 0);
481 dwarf_expr_pop (ctx
);
486 result
= dwarf_expr_fetch (ctx
, offset
);
490 result
= dwarf_expr_fetch (ctx
, 1);
495 CORE_ADDR t1
, t2
, t3
;
497 if (ctx
->stack_len
< 3)
498 error (_("Not enough elements for DW_OP_rot. Need 3, have %d."),
500 t1
= ctx
->stack
[ctx
->stack_len
- 1];
501 t2
= ctx
->stack
[ctx
->stack_len
- 2];
502 t3
= ctx
->stack
[ctx
->stack_len
- 3];
503 ctx
->stack
[ctx
->stack_len
- 1] = t2
;
504 ctx
->stack
[ctx
->stack_len
- 2] = t3
;
505 ctx
->stack
[ctx
->stack_len
- 3] = t1
;
510 case DW_OP_deref_size
:
514 case DW_OP_plus_uconst
:
515 /* Unary operations. */
516 result
= dwarf_expr_fetch (ctx
, 0);
517 dwarf_expr_pop (ctx
);
523 gdb_byte
*buf
= alloca (TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
);
526 (ctx
->read_mem
) (ctx
->baton
, buf
, result
,
527 TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
);
528 result
= dwarf2_read_address (buf
,
529 buf
+ (TARGET_ADDR_BIT
535 case DW_OP_deref_size
:
537 gdb_byte
*buf
= alloca (TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
);
540 (ctx
->read_mem
) (ctx
->baton
, buf
, result
, *op_ptr
++);
541 result
= dwarf2_read_address (buf
,
542 buf
+ (TARGET_ADDR_BIT
549 if ((signed int) result
< 0)
558 case DW_OP_plus_uconst
:
559 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
583 /* Binary operations. Use the value engine to do computations in
585 CORE_ADDR first
, second
;
586 enum exp_opcode binop
;
587 struct value
*val1
, *val2
;
589 second
= dwarf_expr_fetch (ctx
, 0);
590 dwarf_expr_pop (ctx
);
592 first
= dwarf_expr_fetch (ctx
, 0);
593 dwarf_expr_pop (ctx
);
595 val1
= value_from_longest (unsigned_address_type (), first
);
596 val2
= value_from_longest (unsigned_address_type (), second
);
601 binop
= BINOP_BITWISE_AND
;
616 binop
= BINOP_BITWISE_IOR
;
629 val1
= value_from_longest (signed_address_type (), first
);
632 binop
= BINOP_BITWISE_XOR
;
650 binop
= BINOP_NOTEQUAL
;
653 internal_error (__FILE__
, __LINE__
,
654 _("Can't be reached."));
656 result
= value_as_long (value_binop (val1
, val2
, binop
));
660 case DW_OP_GNU_push_tls_address
:
661 /* Variable is at a constant offset in the thread-local
662 storage block into the objfile for the current thread and
663 the dynamic linker module containing this expression. Here
664 we return returns the offset from that base. The top of the
665 stack has the offset from the beginning of the thread
666 control block at which the variable is located. Nothing
667 should follow this operator, so the top of stack would be
669 result
= dwarf_expr_fetch (ctx
, 0);
670 dwarf_expr_pop (ctx
);
671 result
= (ctx
->get_tls_address
) (ctx
->baton
, result
);
675 offset
= extract_signed_integer (op_ptr
, 2);
681 offset
= extract_signed_integer (op_ptr
, 2);
683 if (dwarf_expr_fetch (ctx
, 0) != 0)
685 dwarf_expr_pop (ctx
);
694 CORE_ADDR addr_or_regnum
;
696 /* Record the piece. */
697 op_ptr
= read_uleb128 (op_ptr
, op_end
, &size
);
698 addr_or_regnum
= dwarf_expr_fetch (ctx
, 0);
699 add_piece (ctx
, ctx
->in_reg
, addr_or_regnum
, size
);
701 /* Pop off the address/regnum, and clear the in_reg flag. */
702 dwarf_expr_pop (ctx
);
708 error (_("Unhandled dwarf expression opcode 0x%x"), op
);
711 /* Most things push a result value. */
712 dwarf_expr_push (ctx
, result
);