1 /* DWARF 2 Expression Evaluator.
3 Copyright (C) 2001-2013 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/>. */
28 #include "dwarf2expr.h"
29 #include "gdb_assert.h"
31 /* Local prototypes. */
33 static void execute_stack_op (struct dwarf_expr_context *,
34 const gdb_byte *, const gdb_byte *);
36 /* Cookie for gdbarch data. */
38 static struct gdbarch_data *dwarf_arch_cookie;
40 /* This holds gdbarch-specific types used by the DWARF expression
41 evaluator. See comments in execute_stack_op. */
43 struct dwarf_gdbarch_types
45 struct type *dw_types[3];
48 /* Allocate and fill in dwarf_gdbarch_types for an arch. */
51 dwarf_gdbarch_types_init (struct gdbarch *gdbarch)
53 struct dwarf_gdbarch_types *types
54 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct dwarf_gdbarch_types);
56 /* The types themselves are lazily initialized. */
61 /* Return the type used for DWARF operations where the type is
62 unspecified in the DWARF spec. Only certain sizes are
66 dwarf_expr_address_type (struct dwarf_expr_context *ctx)
68 struct dwarf_gdbarch_types *types = gdbarch_data (ctx->gdbarch,
72 if (ctx->addr_size == 2)
74 else if (ctx->addr_size == 4)
76 else if (ctx->addr_size == 8)
79 error (_("Unsupported address size in DWARF expressions: %d bits"),
82 if (types->dw_types[ndx] == NULL)
84 = arch_integer_type (ctx->gdbarch,
86 0, "<signed DWARF address type>");
88 return types->dw_types[ndx];
91 /* Create a new context for the expression evaluator. */
93 struct dwarf_expr_context *
94 new_dwarf_expr_context (void)
96 struct dwarf_expr_context *retval;
98 retval = xcalloc (1, sizeof (struct dwarf_expr_context));
99 retval->stack_len = 0;
100 retval->stack_allocated = 10;
101 retval->stack = xmalloc (retval->stack_allocated
102 * sizeof (struct dwarf_stack_value));
103 retval->num_pieces = 0;
105 retval->max_recursion_depth = 0x100;
109 /* Release the memory allocated to CTX. */
112 free_dwarf_expr_context (struct dwarf_expr_context *ctx)
119 /* Helper for make_cleanup_free_dwarf_expr_context. */
122 free_dwarf_expr_context_cleanup (void *arg)
124 free_dwarf_expr_context (arg);
127 /* Return a cleanup that calls free_dwarf_expr_context. */
130 make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context *ctx)
132 return make_cleanup (free_dwarf_expr_context_cleanup, ctx);
135 /* Expand the memory allocated to CTX's stack to contain at least
136 NEED more elements than are currently used. */
139 dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need)
141 if (ctx->stack_len + need > ctx->stack_allocated)
143 size_t newlen = ctx->stack_len + need + 10;
145 ctx->stack = xrealloc (ctx->stack,
146 newlen * sizeof (struct dwarf_stack_value));
147 ctx->stack_allocated = newlen;
151 /* Push VALUE onto CTX's stack. */
154 dwarf_expr_push (struct dwarf_expr_context *ctx, struct value *value,
157 struct dwarf_stack_value *v;
159 dwarf_expr_grow_stack (ctx, 1);
160 v = &ctx->stack[ctx->stack_len++];
162 v->in_stack_memory = in_stack_memory;
165 /* Push VALUE onto CTX's stack. */
168 dwarf_expr_push_address (struct dwarf_expr_context *ctx, CORE_ADDR value,
171 dwarf_expr_push (ctx,
172 value_from_ulongest (dwarf_expr_address_type (ctx), value),
176 /* Pop the top item off of CTX's stack. */
179 dwarf_expr_pop (struct dwarf_expr_context *ctx)
181 if (ctx->stack_len <= 0)
182 error (_("dwarf expression stack underflow"));
186 /* Retrieve the N'th item on CTX's stack. */
189 dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n)
191 if (ctx->stack_len <= n)
192 error (_("Asked for position %d of stack, "
193 "stack only has %d elements on it."),
195 return ctx->stack[ctx->stack_len - (1 + n)].value;
198 /* Require that TYPE be an integral type; throw an exception if not. */
201 dwarf_require_integral (struct type *type)
203 if (TYPE_CODE (type) != TYPE_CODE_INT
204 && TYPE_CODE (type) != TYPE_CODE_CHAR
205 && TYPE_CODE (type) != TYPE_CODE_BOOL)
206 error (_("integral type expected in DWARF expression"));
209 /* Return the unsigned form of TYPE. TYPE is necessarily an integral
213 get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
215 switch (TYPE_LENGTH (type))
218 return builtin_type (gdbarch)->builtin_uint8;
220 return builtin_type (gdbarch)->builtin_uint16;
222 return builtin_type (gdbarch)->builtin_uint32;
224 return builtin_type (gdbarch)->builtin_uint64;
226 error (_("no unsigned variant found for type, while evaluating "
227 "DWARF expression"));
231 /* Return the signed form of TYPE. TYPE is necessarily an integral
235 get_signed_type (struct gdbarch *gdbarch, struct type *type)
237 switch (TYPE_LENGTH (type))
240 return builtin_type (gdbarch)->builtin_int8;
242 return builtin_type (gdbarch)->builtin_int16;
244 return builtin_type (gdbarch)->builtin_int32;
246 return builtin_type (gdbarch)->builtin_int64;
248 error (_("no signed variant found for type, while evaluating "
249 "DWARF expression"));
253 /* Retrieve the N'th item on CTX's stack, converted to an address. */
256 dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n)
258 struct value *result_val = dwarf_expr_fetch (ctx, n);
259 enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
262 dwarf_require_integral (value_type (result_val));
263 result = extract_unsigned_integer (value_contents (result_val),
264 TYPE_LENGTH (value_type (result_val)),
267 /* For most architectures, calling extract_unsigned_integer() alone
268 is sufficient for extracting an address. However, some
269 architectures (e.g. MIPS) use signed addresses and using
270 extract_unsigned_integer() will not produce a correct
271 result. Make sure we invoke gdbarch_integer_to_address()
272 for those architectures which require it. */
273 if (gdbarch_integer_to_address_p (ctx->gdbarch))
275 gdb_byte *buf = alloca (ctx->addr_size);
276 struct type *int_type = get_unsigned_type (ctx->gdbarch,
277 value_type (result_val));
279 store_unsigned_integer (buf, ctx->addr_size, byte_order, result);
280 return gdbarch_integer_to_address (ctx->gdbarch, int_type, buf);
283 return (CORE_ADDR) result;
286 /* Retrieve the in_stack_memory flag of the N'th item on CTX's stack. */
289 dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n)
291 if (ctx->stack_len <= n)
292 error (_("Asked for position %d of stack, "
293 "stack only has %d elements on it."),
295 return ctx->stack[ctx->stack_len - (1 + n)].in_stack_memory;
298 /* Return true if the expression stack is empty. */
301 dwarf_expr_stack_empty_p (struct dwarf_expr_context *ctx)
303 return ctx->stack_len == 0;
306 /* Add a new piece to CTX's piece list. */
308 add_piece (struct dwarf_expr_context *ctx, ULONGEST size, ULONGEST offset)
310 struct dwarf_expr_piece *p;
314 ctx->pieces = xrealloc (ctx->pieces,
316 * sizeof (struct dwarf_expr_piece)));
318 p = &ctx->pieces[ctx->num_pieces - 1];
319 p->location = ctx->location;
323 if (p->location == DWARF_VALUE_LITERAL)
325 p->v.literal.data = ctx->data;
326 p->v.literal.length = ctx->len;
328 else if (dwarf_expr_stack_empty_p (ctx))
330 p->location = DWARF_VALUE_OPTIMIZED_OUT;
331 /* Also reset the context's location, for our callers. This is
332 a somewhat strange approach, but this lets us avoid setting
333 the location to DWARF_VALUE_MEMORY in all the individual
334 cases in the evaluator. */
335 ctx->location = DWARF_VALUE_OPTIMIZED_OUT;
337 else if (p->location == DWARF_VALUE_MEMORY)
339 p->v.mem.addr = dwarf_expr_fetch_address (ctx, 0);
340 p->v.mem.in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
342 else if (p->location == DWARF_VALUE_IMPLICIT_POINTER)
344 p->v.ptr.die.sect_off = ctx->len;
345 p->v.ptr.offset = value_as_long (dwarf_expr_fetch (ctx, 0));
347 else if (p->location == DWARF_VALUE_REGISTER)
348 p->v.regno = value_as_long (dwarf_expr_fetch (ctx, 0));
351 p->v.value = dwarf_expr_fetch (ctx, 0);
355 /* Evaluate the expression at ADDR (LEN bytes long) using the context
359 dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr,
362 int old_recursion_depth = ctx->recursion_depth;
364 execute_stack_op (ctx, addr, addr + len);
366 /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here. */
368 gdb_assert (ctx->recursion_depth == old_recursion_depth);
371 /* Helper to read a uleb128 value or throw an error. */
374 safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
377 buf = gdb_read_uleb128 (buf, buf_end, r);
379 error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
383 /* Helper to read a sleb128 value or throw an error. */
386 safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
389 buf = gdb_read_sleb128 (buf, buf_end, r);
391 error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
396 safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
398 buf = gdb_skip_leb128 (buf, buf_end);
400 error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
405 /* Check that the current operator is either at the end of an
406 expression, or that it is followed by a composition operator. */
409 dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
412 /* It seems like DW_OP_GNU_uninit should be handled here. However,
413 it doesn't seem to make sense for DW_OP_*_value, and it was not
414 checked at the other place that this function is called. */
415 if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece)
416 error (_("DWARF-2 expression error: `%s' operations must be "
417 "used either alone or in conjunction with DW_OP_piece "
418 "or DW_OP_bit_piece."),
422 /* Return true iff the types T1 and T2 are "the same". This only does
423 checks that might reasonably be needed to compare DWARF base
427 base_types_equal_p (struct type *t1, struct type *t2)
429 if (TYPE_CODE (t1) != TYPE_CODE (t2))
431 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
433 return TYPE_LENGTH (t1) == TYPE_LENGTH (t2);
436 /* A convenience function to call get_base_type on CTX and return the
437 result. DIE is the DIE whose type we need. SIZE is non-zero if
438 this function should verify that the resulting type has the correct
442 dwarf_get_base_type (struct dwarf_expr_context *ctx, cu_offset die, int size)
446 if (ctx->funcs->get_base_type)
448 result = ctx->funcs->get_base_type (ctx, die);
450 error (_("Could not find type for DW_OP_GNU_const_type"));
451 if (size != 0 && TYPE_LENGTH (result) != size)
452 error (_("DW_OP_GNU_const_type has different sizes for type and data"));
455 /* Anything will do. */
456 result = builtin_type (ctx->gdbarch)->builtin_int;
461 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
462 DWARF register number. Otherwise return -1. */
465 dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end)
471 if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
473 if (buf_end - buf != 1)
475 return *buf - DW_OP_reg0;
478 if (*buf == DW_OP_GNU_regval_type)
481 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
484 buf = gdb_skip_leb128 (buf, buf_end);
488 else if (*buf == DW_OP_regx)
491 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
497 if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
502 /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
503 DW_OP_deref* return the DWARF register number. Otherwise return -1.
504 DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
505 size from DW_OP_deref_size. */
508 dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end,
509 CORE_ADDR *deref_size_return)
517 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
519 dwarf_reg = *buf - DW_OP_breg0;
524 else if (*buf == DW_OP_bregx)
527 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
530 if ((int) dwarf_reg != dwarf_reg)
536 buf = gdb_read_sleb128 (buf, buf_end, &offset);
542 if (*buf == DW_OP_deref)
545 *deref_size_return = -1;
547 else if (*buf == DW_OP_deref_size)
552 *deref_size_return = *buf++;
563 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
564 in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */
567 dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
568 CORE_ADDR *fb_offset_return)
575 if (*buf != DW_OP_fbreg)
579 buf = gdb_read_sleb128 (buf, buf_end, &fb_offset);
582 *fb_offset_return = fb_offset;
583 if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
589 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
590 in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0.
591 The matched SP register number depends on GDBARCH. */
594 dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
595 const gdb_byte *buf_end, CORE_ADDR *sp_offset_return)
602 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
604 dwarf_reg = *buf - DW_OP_breg0;
609 if (*buf != DW_OP_bregx)
612 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
617 if (gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_reg)
618 != gdbarch_sp_regnum (gdbarch))
621 buf = gdb_read_sleb128 (buf, buf_end, &sp_offset);
624 *sp_offset_return = sp_offset;
625 if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
631 /* The engine for the expression evaluator. Using the context in CTX,
632 evaluate the expression between OP_PTR and OP_END. */
635 execute_stack_op (struct dwarf_expr_context *ctx,
636 const gdb_byte *op_ptr, const gdb_byte *op_end)
638 enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
639 /* Old-style "untyped" DWARF values need special treatment in a
640 couple of places, specifically DW_OP_mod and DW_OP_shr. We need
641 a special type for these values so we can distinguish them from
642 values that have an explicit type, because explicitly-typed
643 values do not need special treatment. This special type must be
644 different (in the `==' sense) from any base type coming from the
646 struct type *address_type = dwarf_expr_address_type (ctx);
648 ctx->location = DWARF_VALUE_MEMORY;
649 ctx->initialized = 1; /* Default is initialized. */
651 if (ctx->recursion_depth > ctx->max_recursion_depth)
652 error (_("DWARF-2 expression error: Loop detected (%d)."),
653 ctx->recursion_depth);
654 ctx->recursion_depth++;
656 while (op_ptr < op_end)
658 enum dwarf_location_atom op = *op_ptr++;
660 /* Assume the value is not in stack memory.
661 Code that knows otherwise sets this to 1.
662 Some arithmetic on stack addresses can probably be assumed to still
663 be a stack address, but we skip this complication for now.
664 This is just an optimization, so it's always ok to punt
665 and leave this as 0. */
666 int in_stack_memory = 0;
667 uint64_t uoffset, reg;
669 struct value *result_val = NULL;
671 /* The DWARF expression might have a bug causing an infinite
672 loop. In that case, quitting is the only way out. */
709 result = op - DW_OP_lit0;
710 result_val = value_from_ulongest (address_type, result);
714 result = extract_unsigned_integer (op_ptr,
715 ctx->addr_size, byte_order);
716 op_ptr += ctx->addr_size;
717 /* Some versions of GCC emit DW_OP_addr before
718 DW_OP_GNU_push_tls_address. In this case the value is an
719 index, not an address. We don't support things like
720 branching between the address and the TLS op. */
721 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
722 result += ctx->offset;
723 result_val = value_from_ulongest (address_type, result);
726 case DW_OP_GNU_addr_index:
727 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
728 result = (ctx->funcs->get_addr_index) (ctx->baton, uoffset);
729 result += ctx->offset;
730 result_val = value_from_ulongest (address_type, result);
732 case DW_OP_GNU_const_index:
733 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
734 result = (ctx->funcs->get_addr_index) (ctx->baton, uoffset);
735 result_val = value_from_ulongest (address_type, result);
739 result = extract_unsigned_integer (op_ptr, 1, byte_order);
740 result_val = value_from_ulongest (address_type, result);
744 result = extract_signed_integer (op_ptr, 1, byte_order);
745 result_val = value_from_ulongest (address_type, result);
749 result = extract_unsigned_integer (op_ptr, 2, byte_order);
750 result_val = value_from_ulongest (address_type, result);
754 result = extract_signed_integer (op_ptr, 2, byte_order);
755 result_val = value_from_ulongest (address_type, result);
759 result = extract_unsigned_integer (op_ptr, 4, byte_order);
760 result_val = value_from_ulongest (address_type, result);
764 result = extract_signed_integer (op_ptr, 4, byte_order);
765 result_val = value_from_ulongest (address_type, result);
769 result = extract_unsigned_integer (op_ptr, 8, byte_order);
770 result_val = value_from_ulongest (address_type, result);
774 result = extract_signed_integer (op_ptr, 8, byte_order);
775 result_val = value_from_ulongest (address_type, result);
779 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
781 result_val = value_from_ulongest (address_type, result);
784 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
786 result_val = value_from_ulongest (address_type, result);
789 /* The DW_OP_reg operations are required to occur alone in
790 location expressions. */
824 && *op_ptr != DW_OP_piece
825 && *op_ptr != DW_OP_bit_piece
826 && *op_ptr != DW_OP_GNU_uninit)
827 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
828 "used either alone or in conjunction with DW_OP_piece "
829 "or DW_OP_bit_piece."));
831 result = op - DW_OP_reg0;
832 result_val = value_from_ulongest (address_type, result);
833 ctx->location = DWARF_VALUE_REGISTER;
837 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
838 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
841 result_val = value_from_ulongest (address_type, result);
842 ctx->location = DWARF_VALUE_REGISTER;
845 case DW_OP_implicit_value:
849 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
850 if (op_ptr + len > op_end)
851 error (_("DW_OP_implicit_value: too few bytes available."));
854 ctx->location = DWARF_VALUE_LITERAL;
856 dwarf_expr_require_composition (op_ptr, op_end,
857 "DW_OP_implicit_value");
861 case DW_OP_stack_value:
862 ctx->location = DWARF_VALUE_STACK;
863 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
866 case DW_OP_GNU_implicit_pointer:
870 if (ctx->ref_addr_size == -1)
871 error (_("DWARF-2 expression error: DW_OP_GNU_implicit_pointer "
872 "is not allowed in frame context"));
874 /* The referred-to DIE of sect_offset kind. */
875 ctx->len = extract_unsigned_integer (op_ptr, ctx->ref_addr_size,
877 op_ptr += ctx->ref_addr_size;
879 /* The byte offset into the data. */
880 op_ptr = safe_read_sleb128 (op_ptr, op_end, &len);
881 result = (ULONGEST) len;
882 result_val = value_from_ulongest (address_type, result);
884 ctx->location = DWARF_VALUE_IMPLICIT_POINTER;
885 dwarf_expr_require_composition (op_ptr, op_end,
886 "DW_OP_GNU_implicit_pointer");
923 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
924 result = (ctx->funcs->read_reg) (ctx->baton, op - DW_OP_breg0);
926 result_val = value_from_ulongest (address_type, result);
931 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
932 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
933 result = (ctx->funcs->read_reg) (ctx->baton, reg);
935 result_val = value_from_ulongest (address_type, result);
940 const gdb_byte *datastart;
942 unsigned int before_stack_len;
944 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
945 /* Rather than create a whole new context, we simply
946 record the stack length before execution, then reset it
947 afterwards, effectively erasing whatever the recursive
949 before_stack_len = ctx->stack_len;
950 /* FIXME: cagney/2003-03-26: This code should be using
951 get_frame_base_address(), and then implement a dwarf2
952 specific this_base method. */
953 (ctx->funcs->get_frame_base) (ctx->baton, &datastart, &datalen);
954 dwarf_expr_eval (ctx, datastart, datalen);
955 if (ctx->location == DWARF_VALUE_MEMORY)
956 result = dwarf_expr_fetch_address (ctx, 0);
957 else if (ctx->location == DWARF_VALUE_REGISTER)
958 result = (ctx->funcs->read_reg) (ctx->baton,
959 value_as_long (dwarf_expr_fetch (ctx, 0)));
961 error (_("Not implemented: computing frame "
962 "base using explicit value operator"));
963 result = result + offset;
964 result_val = value_from_ulongest (address_type, result);
966 ctx->stack_len = before_stack_len;
967 ctx->location = DWARF_VALUE_MEMORY;
972 result_val = dwarf_expr_fetch (ctx, 0);
973 in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
977 dwarf_expr_pop (ctx);
982 result_val = dwarf_expr_fetch (ctx, offset);
983 in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, offset);
988 struct dwarf_stack_value t1, t2;
990 if (ctx->stack_len < 2)
991 error (_("Not enough elements for "
992 "DW_OP_swap. Need 2, have %d."),
994 t1 = ctx->stack[ctx->stack_len - 1];
995 t2 = ctx->stack[ctx->stack_len - 2];
996 ctx->stack[ctx->stack_len - 1] = t2;
997 ctx->stack[ctx->stack_len - 2] = t1;
1002 result_val = dwarf_expr_fetch (ctx, 1);
1003 in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 1);
1008 struct dwarf_stack_value t1, t2, t3;
1010 if (ctx->stack_len < 3)
1011 error (_("Not enough elements for "
1012 "DW_OP_rot. Need 3, have %d."),
1014 t1 = ctx->stack[ctx->stack_len - 1];
1015 t2 = ctx->stack[ctx->stack_len - 2];
1016 t3 = ctx->stack[ctx->stack_len - 3];
1017 ctx->stack[ctx->stack_len - 1] = t2;
1018 ctx->stack[ctx->stack_len - 2] = t3;
1019 ctx->stack[ctx->stack_len - 3] = t1;
1024 case DW_OP_deref_size:
1025 case DW_OP_GNU_deref_type:
1027 int addr_size = (op == DW_OP_deref ? ctx->addr_size : *op_ptr++);
1028 gdb_byte *buf = alloca (addr_size);
1029 CORE_ADDR addr = dwarf_expr_fetch_address (ctx, 0);
1032 dwarf_expr_pop (ctx);
1034 if (op == DW_OP_GNU_deref_type)
1038 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1039 type_die.cu_off = uoffset;
1040 type = dwarf_get_base_type (ctx, type_die, 0);
1043 type = address_type;
1045 (ctx->funcs->read_mem) (ctx->baton, buf, addr, addr_size);
1047 /* If the size of the object read from memory is different
1048 from the type length, we need to zero-extend it. */
1049 if (TYPE_LENGTH (type) != addr_size)
1052 extract_unsigned_integer (buf, addr_size, byte_order);
1054 buf = alloca (TYPE_LENGTH (type));
1055 store_unsigned_integer (buf, TYPE_LENGTH (type),
1056 byte_order, result);
1059 result_val = value_from_contents_and_address (type, buf, addr);
1066 case DW_OP_plus_uconst:
1068 /* Unary operations. */
1069 result_val = dwarf_expr_fetch (ctx, 0);
1070 dwarf_expr_pop (ctx);
1075 if (value_less (result_val,
1076 value_zero (value_type (result_val), not_lval)))
1077 result_val = value_neg (result_val);
1080 result_val = value_neg (result_val);
1083 dwarf_require_integral (value_type (result_val));
1084 result_val = value_complement (result_val);
1086 case DW_OP_plus_uconst:
1087 dwarf_require_integral (value_type (result_val));
1088 result = value_as_long (result_val);
1089 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1091 result_val = value_from_ulongest (address_type, result);
1115 /* Binary operations. */
1116 struct value *first, *second;
1118 second = dwarf_expr_fetch (ctx, 0);
1119 dwarf_expr_pop (ctx);
1121 first = dwarf_expr_fetch (ctx, 0);
1122 dwarf_expr_pop (ctx);
1124 if (! base_types_equal_p (value_type (first), value_type (second)))
1125 error (_("Incompatible types on DWARF stack"));
1130 dwarf_require_integral (value_type (first));
1131 dwarf_require_integral (value_type (second));
1132 result_val = value_binop (first, second, BINOP_BITWISE_AND);
1135 result_val = value_binop (first, second, BINOP_DIV);
1138 result_val = value_binop (first, second, BINOP_SUB);
1143 struct type *orig_type = value_type (first);
1145 /* We have to special-case "old-style" untyped values
1146 -- these must have mod computed using unsigned
1148 if (orig_type == address_type)
1151 = get_unsigned_type (ctx->gdbarch, orig_type);
1154 first = value_cast (utype, first);
1155 second = value_cast (utype, second);
1157 /* Note that value_binop doesn't handle float or
1158 decimal float here. This seems unimportant. */
1159 result_val = value_binop (first, second, BINOP_MOD);
1161 result_val = value_cast (orig_type, result_val);
1165 result_val = value_binop (first, second, BINOP_MUL);
1168 dwarf_require_integral (value_type (first));
1169 dwarf_require_integral (value_type (second));
1170 result_val = value_binop (first, second, BINOP_BITWISE_IOR);
1173 result_val = value_binop (first, second, BINOP_ADD);
1176 dwarf_require_integral (value_type (first));
1177 dwarf_require_integral (value_type (second));
1178 result_val = value_binop (first, second, BINOP_LSH);
1181 dwarf_require_integral (value_type (first));
1182 dwarf_require_integral (value_type (second));
1183 if (!TYPE_UNSIGNED (value_type (first)))
1186 = get_unsigned_type (ctx->gdbarch, value_type (first));
1188 first = value_cast (utype, first);
1191 result_val = value_binop (first, second, BINOP_RSH);
1192 /* Make sure we wind up with the same type we started
1194 if (value_type (result_val) != value_type (second))
1195 result_val = value_cast (value_type (second), result_val);
1198 dwarf_require_integral (value_type (first));
1199 dwarf_require_integral (value_type (second));
1200 if (TYPE_UNSIGNED (value_type (first)))
1203 = get_signed_type (ctx->gdbarch, value_type (first));
1205 first = value_cast (stype, first);
1208 result_val = value_binop (first, second, BINOP_RSH);
1209 /* Make sure we wind up with the same type we started
1211 if (value_type (result_val) != value_type (second))
1212 result_val = value_cast (value_type (second), result_val);
1215 dwarf_require_integral (value_type (first));
1216 dwarf_require_integral (value_type (second));
1217 result_val = value_binop (first, second, BINOP_BITWISE_XOR);
1220 /* A <= B is !(B < A). */
1221 result = ! value_less (second, first);
1222 result_val = value_from_ulongest (address_type, result);
1225 /* A >= B is !(A < B). */
1226 result = ! value_less (first, second);
1227 result_val = value_from_ulongest (address_type, result);
1230 result = value_equal (first, second);
1231 result_val = value_from_ulongest (address_type, result);
1234 result = value_less (first, second);
1235 result_val = value_from_ulongest (address_type, result);
1238 /* A > B is B < A. */
1239 result = value_less (second, first);
1240 result_val = value_from_ulongest (address_type, result);
1243 result = ! value_equal (first, second);
1244 result_val = value_from_ulongest (address_type, result);
1247 internal_error (__FILE__, __LINE__,
1248 _("Can't be reached."));
1253 case DW_OP_call_frame_cfa:
1254 result = (ctx->funcs->get_frame_cfa) (ctx->baton);
1255 result_val = value_from_ulongest (address_type, result);
1256 in_stack_memory = 1;
1259 case DW_OP_GNU_push_tls_address:
1260 /* Variable is at a constant offset in the thread-local
1261 storage block into the objfile for the current thread and
1262 the dynamic linker module containing this expression. Here
1263 we return returns the offset from that base. The top of the
1264 stack has the offset from the beginning of the thread
1265 control block at which the variable is located. Nothing
1266 should follow this operator, so the top of stack would be
1268 result = value_as_long (dwarf_expr_fetch (ctx, 0));
1269 dwarf_expr_pop (ctx);
1270 result = (ctx->funcs->get_tls_address) (ctx->baton, result);
1271 result_val = value_from_ulongest (address_type, result);
1275 offset = extract_signed_integer (op_ptr, 2, byte_order);
1284 offset = extract_signed_integer (op_ptr, 2, byte_order);
1286 val = dwarf_expr_fetch (ctx, 0);
1287 dwarf_require_integral (value_type (val));
1288 if (value_as_long (val) != 0)
1290 dwarf_expr_pop (ctx);
1301 /* Record the piece. */
1302 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1303 add_piece (ctx, 8 * size, 0);
1305 /* Pop off the address/regnum, and reset the location
1307 if (ctx->location != DWARF_VALUE_LITERAL
1308 && ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
1309 dwarf_expr_pop (ctx);
1310 ctx->location = DWARF_VALUE_MEMORY;
1314 case DW_OP_bit_piece:
1316 uint64_t size, offset;
1318 /* Record the piece. */
1319 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1320 op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
1321 add_piece (ctx, size, offset);
1323 /* Pop off the address/regnum, and reset the location
1325 if (ctx->location != DWARF_VALUE_LITERAL
1326 && ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
1327 dwarf_expr_pop (ctx);
1328 ctx->location = DWARF_VALUE_MEMORY;
1332 case DW_OP_GNU_uninit:
1333 if (op_ptr != op_end)
1334 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
1335 "be the very last op."));
1337 ctx->initialized = 0;
1344 offset.cu_off = extract_unsigned_integer (op_ptr, 2, byte_order);
1346 ctx->funcs->dwarf_call (ctx, offset);
1354 offset.cu_off = extract_unsigned_integer (op_ptr, 4, byte_order);
1356 ctx->funcs->dwarf_call (ctx, offset);
1360 case DW_OP_GNU_entry_value:
1363 CORE_ADDR deref_size;
1364 union call_site_parameter_u kind_u;
1366 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
1367 if (op_ptr + len > op_end)
1368 error (_("DW_OP_GNU_entry_value: too few bytes available."));
1370 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len);
1371 if (kind_u.dwarf_reg != -1)
1374 ctx->funcs->push_dwarf_reg_entry_value (ctx,
1375 CALL_SITE_PARAMETER_DWARF_REG,
1377 -1 /* deref_size */);
1381 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr,
1384 if (kind_u.dwarf_reg != -1)
1386 if (deref_size == -1)
1387 deref_size = ctx->addr_size;
1389 ctx->funcs->push_dwarf_reg_entry_value (ctx,
1390 CALL_SITE_PARAMETER_DWARF_REG,
1391 kind_u, deref_size);
1395 error (_("DWARF-2 expression error: DW_OP_GNU_entry_value is "
1396 "supported only for single DW_OP_reg* "
1397 "or for DW_OP_breg*(0)+DW_OP_deref*"));
1400 case DW_OP_GNU_parameter_ref:
1402 union call_site_parameter_u kind_u;
1404 kind_u.param_offset.cu_off = extract_unsigned_integer (op_ptr, 4,
1407 ctx->funcs->push_dwarf_reg_entry_value (ctx,
1408 CALL_SITE_PARAMETER_PARAM_OFFSET,
1410 -1 /* deref_size */);
1414 case DW_OP_GNU_const_type:
1418 const gdb_byte *data;
1421 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1422 type_die.cu_off = uoffset;
1427 type = dwarf_get_base_type (ctx, type_die, n);
1428 result_val = value_from_contents (type, data);
1432 case DW_OP_GNU_regval_type:
1437 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1438 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1439 type_die.cu_off = uoffset;
1441 type = dwarf_get_base_type (ctx, type_die, 0);
1442 result_val = ctx->funcs->get_reg_value (ctx->baton, type, reg);
1446 case DW_OP_GNU_convert:
1447 case DW_OP_GNU_reinterpret:
1452 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1453 type_die.cu_off = uoffset;
1455 if (type_die.cu_off == 0)
1456 type = address_type;
1458 type = dwarf_get_base_type (ctx, type_die, 0);
1460 result_val = dwarf_expr_fetch (ctx, 0);
1461 dwarf_expr_pop (ctx);
1463 if (op == DW_OP_GNU_convert)
1464 result_val = value_cast (type, result_val);
1465 else if (type == value_type (result_val))
1469 else if (TYPE_LENGTH (type)
1470 != TYPE_LENGTH (value_type (result_val)))
1471 error (_("DW_OP_GNU_reinterpret has wrong size"));
1474 = value_from_contents (type,
1475 value_contents_all (result_val));
1480 error (_("Unhandled dwarf expression opcode 0x%x"), op);
1483 /* Most things push a result value. */
1484 gdb_assert (result_val != NULL);
1485 dwarf_expr_push (ctx, result_val, in_stack_memory);
1490 /* To simplify our main caller, if the result is an implicit
1491 pointer, then make a pieced value. This is ok because we can't
1492 have implicit pointers in contexts where pieces are invalid. */
1493 if (ctx->location == DWARF_VALUE_IMPLICIT_POINTER)
1494 add_piece (ctx, 8 * ctx->addr_size, 0);
1497 ctx->recursion_depth--;
1498 gdb_assert (ctx->recursion_depth >= 0);
1501 /* Stub dwarf_expr_context_funcs.get_frame_base implementation. */
1504 ctx_no_get_frame_base (void *baton, const gdb_byte **start, size_t *length)
1506 error (_("%s is invalid in this context"), "DW_OP_fbreg");
1509 /* Stub dwarf_expr_context_funcs.get_frame_cfa implementation. */
1512 ctx_no_get_frame_cfa (void *baton)
1514 error (_("%s is invalid in this context"), "DW_OP_call_frame_cfa");
1517 /* Stub dwarf_expr_context_funcs.get_frame_pc implementation. */
1520 ctx_no_get_frame_pc (void *baton)
1522 error (_("%s is invalid in this context"), "DW_OP_GNU_implicit_pointer");
1525 /* Stub dwarf_expr_context_funcs.get_tls_address implementation. */
1528 ctx_no_get_tls_address (void *baton, CORE_ADDR offset)
1530 error (_("%s is invalid in this context"), "DW_OP_GNU_push_tls_address");
1533 /* Stub dwarf_expr_context_funcs.dwarf_call implementation. */
1536 ctx_no_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
1538 error (_("%s is invalid in this context"), "DW_OP_call*");
1541 /* Stub dwarf_expr_context_funcs.get_base_type implementation. */
1544 ctx_no_get_base_type (struct dwarf_expr_context *ctx, cu_offset die)
1546 error (_("Support for typed DWARF is not supported in this context"));
1549 /* Stub dwarf_expr_context_funcs.push_dwarf_block_entry_value
1553 ctx_no_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
1554 enum call_site_parameter_kind kind,
1555 union call_site_parameter_u kind_u,
1558 internal_error (__FILE__, __LINE__,
1559 _("Support for DW_OP_GNU_entry_value is unimplemented"));
1562 /* Stub dwarf_expr_context_funcs.get_addr_index implementation. */
1565 ctx_no_get_addr_index (void *baton, unsigned int index)
1567 error (_("%s is invalid in this context"), "DW_OP_GNU_addr_index");
1570 /* Provide a prototype to silence -Wmissing-prototypes. */
1571 extern initialize_file_ftype _initialize_dwarf2expr;
1574 _initialize_dwarf2expr (void)
1577 = gdbarch_data_register_post_init (dwarf_gdbarch_types_init);