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 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 *);
36 static struct type *unsigned_address_type (void);
38 /* Create a new context for the expression evaluator. */
40 struct dwarf_expr_context *
41 new_dwarf_expr_context (void)
43 struct dwarf_expr_context *retval;
44 retval = xcalloc (1, sizeof (struct dwarf_expr_context));
45 retval->stack_len = 0;
46 retval->stack_allocated = 10;
47 retval->stack = xmalloc (retval->stack_allocated * sizeof (CORE_ADDR));
48 retval->num_pieces = 0;
53 /* Release the memory allocated to CTX. */
56 free_dwarf_expr_context (struct dwarf_expr_context *ctx)
63 /* Expand the memory allocated to CTX's stack to contain at least
64 NEED more elements than are currently used. */
67 dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need)
69 if (ctx->stack_len + need > ctx->stack_allocated)
71 size_t newlen = ctx->stack_len + need + 10;
72 ctx->stack = xrealloc (ctx->stack,
73 newlen * sizeof (CORE_ADDR));
74 ctx->stack_allocated = newlen;
78 /* Push VALUE onto CTX's stack. */
81 dwarf_expr_push (struct dwarf_expr_context *ctx, CORE_ADDR value)
83 dwarf_expr_grow_stack (ctx, 1);
84 ctx->stack[ctx->stack_len++] = value;
87 /* Pop the top item off of CTX's stack. */
90 dwarf_expr_pop (struct dwarf_expr_context *ctx)
92 if (ctx->stack_len <= 0)
93 error (_("dwarf expression stack underflow"));
97 /* Retrieve the N'th item on CTX's stack. */
100 dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n)
102 if (ctx->stack_len <= n)
103 error (_("Asked for position %d of stack, stack only has %d elements on it."),
105 return ctx->stack[ctx->stack_len - (1 + n)];
109 /* Add a new piece to CTX's piece list. */
111 add_piece (struct dwarf_expr_context *ctx,
112 int in_reg, CORE_ADDR value, ULONGEST size)
114 struct dwarf_expr_piece *p;
119 ctx->pieces = xrealloc (ctx->pieces,
121 * sizeof (struct dwarf_expr_piece)));
123 ctx->pieces = xmalloc (ctx->num_pieces
124 * sizeof (struct dwarf_expr_piece));
126 p = &ctx->pieces[ctx->num_pieces - 1];
132 /* Evaluate the expression at ADDR (LEN bytes long) using the context
136 dwarf_expr_eval (struct dwarf_expr_context *ctx, gdb_byte *addr, size_t len)
138 execute_stack_op (ctx, addr, addr + len);
141 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
142 by R, and return the new value of BUF. Verify that it doesn't extend
146 read_uleb128 (gdb_byte *buf, gdb_byte *buf_end, ULONGEST * r)
155 error (_("read_uleb128: Corrupted DWARF expression."));
158 result |= (byte & 0x7f) << shift;
159 if ((byte & 0x80) == 0)
167 /* Decode the signed LEB128 constant at BUF into the variable pointed to
168 by R, and return the new value of BUF. Verify that it doesn't extend
172 read_sleb128 (gdb_byte *buf, gdb_byte *buf_end, LONGEST * r)
181 error (_("read_sleb128: Corrupted DWARF expression."));
184 result |= (byte & 0x7f) << shift;
186 if ((byte & 0x80) == 0)
189 if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
190 result |= -(1 << shift);
196 /* Read an address from BUF, and verify that it doesn't extend past
197 BUF_END. The address is returned, and *BYTES_READ is set to the
198 number of bytes read from BUF. */
201 dwarf2_read_address (gdb_byte *buf, gdb_byte *buf_end, int *bytes_read)
205 if (buf_end - buf < gdbarch_addr_bit (current_gdbarch) / TARGET_CHAR_BIT)
206 error (_("dwarf2_read_address: Corrupted DWARF expression."));
208 *bytes_read = gdbarch_addr_bit (current_gdbarch) / TARGET_CHAR_BIT;
210 /* For most architectures, calling extract_unsigned_integer() alone
211 is sufficient for extracting an address. However, some
212 architectures (e.g. MIPS) use signed addresses and using
213 extract_unsigned_integer() will not produce a correct
214 result. Turning the unsigned integer into a value and then
215 decomposing that value as an address will cause
216 gdbarch_integer_to_address() to be invoked for those
217 architectures which require it. Thus, using value_as_address()
218 will produce the correct result for both types of architectures.
220 One concern regarding the use of values for this purpose is
221 efficiency. Obviously, these extra calls will take more time to
222 execute and creating a value takes more space, space which will
223 have to be garbage collected at a later time. If constructing
224 and then decomposing a value for this purpose proves to be too
225 inefficient, then gdbarch_integer_to_address() can be called
228 The use of `unsigned_address_type' in the code below refers to
229 the type of buf and has no bearing on the signedness of the
230 address being returned. */
232 result = value_as_address (value_from_longest
233 (unsigned_address_type (),
234 extract_unsigned_integer
236 gdbarch_addr_bit (current_gdbarch)
237 / TARGET_CHAR_BIT)));
242 /* Return the type of an address, for unsigned arithmetic. */
245 unsigned_address_type (void)
247 switch (gdbarch_addr_bit (current_gdbarch) / TARGET_CHAR_BIT)
250 return builtin_type_uint16;
252 return builtin_type_uint32;
254 return builtin_type_uint64;
256 internal_error (__FILE__, __LINE__,
257 _("Unsupported address size.\n"));
261 /* Return the type of an address, for signed arithmetic. */
264 signed_address_type (void)
266 switch (gdbarch_addr_bit (current_gdbarch) / TARGET_CHAR_BIT)
269 return builtin_type_int16;
271 return builtin_type_int32;
273 return builtin_type_int64;
275 internal_error (__FILE__, __LINE__,
276 _("Unsupported address size.\n"));
280 /* The engine for the expression evaluator. Using the context in CTX,
281 evaluate the expression between OP_PTR and OP_END. */
284 execute_stack_op (struct dwarf_expr_context *ctx,
285 gdb_byte *op_ptr, gdb_byte *op_end)
288 ctx->initialized = 1; /* Default is initialized. */
290 while (op_ptr < op_end)
292 enum dwarf_location_atom op = *op_ptr++;
294 ULONGEST uoffset, reg;
332 result = op - DW_OP_lit0;
336 result = dwarf2_read_address (op_ptr, op_end, &bytes_read);
337 op_ptr += bytes_read;
341 result = extract_unsigned_integer (op_ptr, 1);
345 result = extract_signed_integer (op_ptr, 1);
349 result = extract_unsigned_integer (op_ptr, 2);
353 result = extract_signed_integer (op_ptr, 2);
357 result = extract_unsigned_integer (op_ptr, 4);
361 result = extract_signed_integer (op_ptr, 4);
365 result = extract_unsigned_integer (op_ptr, 8);
369 result = extract_signed_integer (op_ptr, 8);
373 op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
377 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
381 /* The DW_OP_reg operations are required to occur alone in
382 location expressions. */
416 && *op_ptr != DW_OP_piece
417 && *op_ptr != DW_OP_GNU_uninit)
418 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
419 "used either alone or in conjuction with DW_OP_piece."));
421 result = op - DW_OP_reg0;
427 op_ptr = read_uleb128 (op_ptr, op_end, ®);
428 if (op_ptr != op_end && *op_ptr != DW_OP_piece)
429 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
430 "used either alone or in conjuction with DW_OP_piece."));
469 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
470 result = (ctx->read_reg) (ctx->baton, op - DW_OP_breg0);
476 op_ptr = read_uleb128 (op_ptr, op_end, ®);
477 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
478 result = (ctx->read_reg) (ctx->baton, reg);
486 unsigned int before_stack_len;
488 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
489 /* Rather than create a whole new context, we simply
490 record the stack length before execution, then reset it
491 afterwards, effectively erasing whatever the recursive
493 before_stack_len = ctx->stack_len;
494 /* FIXME: cagney/2003-03-26: This code should be using
495 get_frame_base_address(), and then implement a dwarf2
496 specific this_base method. */
497 (ctx->get_frame_base) (ctx->baton, &datastart, &datalen);
498 dwarf_expr_eval (ctx, datastart, datalen);
499 result = dwarf_expr_fetch (ctx, 0);
501 result = (ctx->read_reg) (ctx->baton, result);
502 result = result + offset;
503 ctx->stack_len = before_stack_len;
508 result = dwarf_expr_fetch (ctx, 0);
512 dwarf_expr_pop (ctx);
517 result = dwarf_expr_fetch (ctx, offset);
521 result = dwarf_expr_fetch (ctx, 1);
526 CORE_ADDR t1, t2, t3;
528 if (ctx->stack_len < 3)
529 error (_("Not enough elements for DW_OP_rot. Need 3, have %d."),
531 t1 = ctx->stack[ctx->stack_len - 1];
532 t2 = ctx->stack[ctx->stack_len - 2];
533 t3 = ctx->stack[ctx->stack_len - 3];
534 ctx->stack[ctx->stack_len - 1] = t2;
535 ctx->stack[ctx->stack_len - 2] = t3;
536 ctx->stack[ctx->stack_len - 3] = t1;
541 case DW_OP_deref_size:
545 case DW_OP_plus_uconst:
546 /* Unary operations. */
547 result = dwarf_expr_fetch (ctx, 0);
548 dwarf_expr_pop (ctx);
554 gdb_byte *buf = alloca (gdbarch_addr_bit (current_gdbarch)
558 (ctx->read_mem) (ctx->baton, buf, result,
559 gdbarch_addr_bit (current_gdbarch)
561 result = dwarf2_read_address (buf,
562 buf + (gdbarch_addr_bit
569 case DW_OP_deref_size:
572 = alloca (gdbarch_addr_bit (current_gdbarch)
576 (ctx->read_mem) (ctx->baton, buf, result, *op_ptr++);
577 result = dwarf2_read_address (buf,
578 buf + (gdbarch_addr_bit
586 if ((signed int) result < 0)
595 case DW_OP_plus_uconst:
596 op_ptr = read_uleb128 (op_ptr, op_end, ®);
620 /* Binary operations. Use the value engine to do computations in
622 CORE_ADDR first, second;
623 enum exp_opcode binop;
624 struct value *val1, *val2;
626 second = dwarf_expr_fetch (ctx, 0);
627 dwarf_expr_pop (ctx);
629 first = dwarf_expr_fetch (ctx, 0);
630 dwarf_expr_pop (ctx);
632 val1 = value_from_longest (unsigned_address_type (), first);
633 val2 = value_from_longest (unsigned_address_type (), second);
638 binop = BINOP_BITWISE_AND;
653 binop = BINOP_BITWISE_IOR;
666 val1 = value_from_longest (signed_address_type (), first);
669 binop = BINOP_BITWISE_XOR;
687 binop = BINOP_NOTEQUAL;
690 internal_error (__FILE__, __LINE__,
691 _("Can't be reached."));
693 result = value_as_long (value_binop (val1, val2, binop));
697 case DW_OP_GNU_push_tls_address:
698 /* Variable is at a constant offset in the thread-local
699 storage block into the objfile for the current thread and
700 the dynamic linker module containing this expression. Here
701 we return returns the offset from that base. The top of the
702 stack has the offset from the beginning of the thread
703 control block at which the variable is located. Nothing
704 should follow this operator, so the top of stack would be
706 result = dwarf_expr_fetch (ctx, 0);
707 dwarf_expr_pop (ctx);
708 result = (ctx->get_tls_address) (ctx->baton, result);
712 offset = extract_signed_integer (op_ptr, 2);
718 offset = extract_signed_integer (op_ptr, 2);
720 if (dwarf_expr_fetch (ctx, 0) != 0)
722 dwarf_expr_pop (ctx);
731 CORE_ADDR addr_or_regnum;
733 /* Record the piece. */
734 op_ptr = read_uleb128 (op_ptr, op_end, &size);
735 addr_or_regnum = dwarf_expr_fetch (ctx, 0);
736 add_piece (ctx, ctx->in_reg, addr_or_regnum, size);
738 /* Pop off the address/regnum, and clear the in_reg flag. */
739 dwarf_expr_pop (ctx);
744 case DW_OP_GNU_uninit:
745 if (op_ptr != op_end)
746 error (_("DWARF-2 expression error: DW_OP_GNU_unint must always "
747 "be the very last op."));
749 ctx->initialized = 0;
753 error (_("Unhandled dwarf expression opcode 0x%x"), op);
756 /* Most things push a result value. */
757 dwarf_expr_push (ctx, result);