1 /* DWARF 2 Expression Evaluator.
3 Copyright (C) 2001-2003, 2005, 2007-2012 Free Software Foundation,
6 Contributed by Daniel Berlin (dan@dberlin.org)
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "dwarf2expr.h"
30 #include "gdb_assert.h"
32 /* Local prototypes. */
34 static void execute_stack_op (struct dwarf_expr_context *,
35 const gdb_byte *, const gdb_byte *);
37 /* Cookie for gdbarch data. */
39 static struct gdbarch_data *dwarf_arch_cookie;
41 /* This holds gdbarch-specific types used by the DWARF expression
42 evaluator. See comments in execute_stack_op. */
44 struct dwarf_gdbarch_types
46 struct type *dw_types[3];
49 /* Allocate and fill in dwarf_gdbarch_types for an arch. */
52 dwarf_gdbarch_types_init (struct gdbarch *gdbarch)
54 struct dwarf_gdbarch_types *types
55 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct dwarf_gdbarch_types);
57 /* The types themselves are lazily initialized. */
62 /* Return the type used for DWARF operations where the type is
63 unspecified in the DWARF spec. Only certain sizes are
67 dwarf_expr_address_type (struct dwarf_expr_context *ctx)
69 struct dwarf_gdbarch_types *types = gdbarch_data (ctx->gdbarch,
73 if (ctx->addr_size == 2)
75 else if (ctx->addr_size == 4)
77 else if (ctx->addr_size == 8)
80 error (_("Unsupported address size in DWARF expressions: %d bits"),
83 if (types->dw_types[ndx] == NULL)
85 = arch_integer_type (ctx->gdbarch,
87 0, "<signed DWARF address type>");
89 return types->dw_types[ndx];
92 /* Create a new context for the expression evaluator. */
94 struct dwarf_expr_context *
95 new_dwarf_expr_context (void)
97 struct dwarf_expr_context *retval;
99 retval = xcalloc (1, sizeof (struct dwarf_expr_context));
100 retval->stack_len = 0;
101 retval->stack_allocated = 10;
102 retval->stack = xmalloc (retval->stack_allocated
103 * sizeof (struct dwarf_stack_value));
104 retval->num_pieces = 0;
106 retval->max_recursion_depth = 0x100;
110 /* Release the memory allocated to CTX. */
113 free_dwarf_expr_context (struct dwarf_expr_context *ctx)
120 /* Helper for make_cleanup_free_dwarf_expr_context. */
123 free_dwarf_expr_context_cleanup (void *arg)
125 free_dwarf_expr_context (arg);
128 /* Return a cleanup that calls free_dwarf_expr_context. */
131 make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context *ctx)
133 return make_cleanup (free_dwarf_expr_context_cleanup, ctx);
136 /* Expand the memory allocated to CTX's stack to contain at least
137 NEED more elements than are currently used. */
140 dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need)
142 if (ctx->stack_len + need > ctx->stack_allocated)
144 size_t newlen = ctx->stack_len + need + 10;
146 ctx->stack = xrealloc (ctx->stack,
147 newlen * sizeof (struct dwarf_stack_value));
148 ctx->stack_allocated = newlen;
152 /* Push VALUE onto CTX's stack. */
155 dwarf_expr_push (struct dwarf_expr_context *ctx, struct value *value,
158 struct dwarf_stack_value *v;
160 dwarf_expr_grow_stack (ctx, 1);
161 v = &ctx->stack[ctx->stack_len++];
163 v->in_stack_memory = in_stack_memory;
166 /* Push VALUE onto CTX's stack. */
169 dwarf_expr_push_address (struct dwarf_expr_context *ctx, CORE_ADDR value,
172 dwarf_expr_push (ctx,
173 value_from_ulongest (dwarf_expr_address_type (ctx), value),
177 /* Pop the top item off of CTX's stack. */
180 dwarf_expr_pop (struct dwarf_expr_context *ctx)
182 if (ctx->stack_len <= 0)
183 error (_("dwarf expression stack underflow"));
187 /* Retrieve the N'th item on CTX's stack. */
190 dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n)
192 if (ctx->stack_len <= n)
193 error (_("Asked for position %d of stack, "
194 "stack only has %d elements on it."),
196 return ctx->stack[ctx->stack_len - (1 + n)].value;
199 /* Require that TYPE be an integral type; throw an exception if not. */
202 dwarf_require_integral (struct type *type)
204 if (TYPE_CODE (type) != TYPE_CODE_INT
205 && TYPE_CODE (type) != TYPE_CODE_CHAR
206 && TYPE_CODE (type) != TYPE_CODE_BOOL)
207 error (_("integral type expected in DWARF expression"));
210 /* Return the unsigned form of TYPE. TYPE is necessarily an integral
214 get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
216 switch (TYPE_LENGTH (type))
219 return builtin_type (gdbarch)->builtin_uint8;
221 return builtin_type (gdbarch)->builtin_uint16;
223 return builtin_type (gdbarch)->builtin_uint32;
225 return builtin_type (gdbarch)->builtin_uint64;
227 error (_("no unsigned variant found for type, while evaluating "
228 "DWARF expression"));
232 /* Return the signed form of TYPE. TYPE is necessarily an integral
236 get_signed_type (struct gdbarch *gdbarch, struct type *type)
238 switch (TYPE_LENGTH (type))
241 return builtin_type (gdbarch)->builtin_int8;
243 return builtin_type (gdbarch)->builtin_int16;
245 return builtin_type (gdbarch)->builtin_int32;
247 return builtin_type (gdbarch)->builtin_int64;
249 error (_("no signed variant found for type, while evaluating "
250 "DWARF expression"));
254 /* Retrieve the N'th item on CTX's stack, converted to an address. */
257 dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n)
259 struct value *result_val = dwarf_expr_fetch (ctx, n);
260 enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
263 dwarf_require_integral (value_type (result_val));
264 result = extract_unsigned_integer (value_contents (result_val),
265 TYPE_LENGTH (value_type (result_val)),
268 /* For most architectures, calling extract_unsigned_integer() alone
269 is sufficient for extracting an address. However, some
270 architectures (e.g. MIPS) use signed addresses and using
271 extract_unsigned_integer() will not produce a correct
272 result. Make sure we invoke gdbarch_integer_to_address()
273 for those architectures which require it. */
274 if (gdbarch_integer_to_address_p (ctx->gdbarch))
276 gdb_byte *buf = alloca (ctx->addr_size);
277 struct type *int_type = get_unsigned_type (ctx->gdbarch,
278 value_type (result_val));
280 store_unsigned_integer (buf, ctx->addr_size, byte_order, result);
281 return gdbarch_integer_to_address (ctx->gdbarch, int_type, buf);
284 return (CORE_ADDR) result;
287 /* Retrieve the in_stack_memory flag of the N'th item on CTX's stack. */
290 dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n)
292 if (ctx->stack_len <= n)
293 error (_("Asked for position %d of stack, "
294 "stack only has %d elements on it."),
296 return ctx->stack[ctx->stack_len - (1 + n)].in_stack_memory;
299 /* Return true if the expression stack is empty. */
302 dwarf_expr_stack_empty_p (struct dwarf_expr_context *ctx)
304 return ctx->stack_len == 0;
307 /* Add a new piece to CTX's piece list. */
309 add_piece (struct dwarf_expr_context *ctx, ULONGEST size, ULONGEST offset)
311 struct dwarf_expr_piece *p;
315 ctx->pieces = xrealloc (ctx->pieces,
317 * sizeof (struct dwarf_expr_piece)));
319 p = &ctx->pieces[ctx->num_pieces - 1];
320 p->location = ctx->location;
324 if (p->location == DWARF_VALUE_LITERAL)
326 p->v.literal.data = ctx->data;
327 p->v.literal.length = ctx->len;
329 else if (dwarf_expr_stack_empty_p (ctx))
331 p->location = DWARF_VALUE_OPTIMIZED_OUT;
332 /* Also reset the context's location, for our callers. This is
333 a somewhat strange approach, but this lets us avoid setting
334 the location to DWARF_VALUE_MEMORY in all the individual
335 cases in the evaluator. */
336 ctx->location = DWARF_VALUE_OPTIMIZED_OUT;
338 else if (p->location == DWARF_VALUE_MEMORY)
340 p->v.mem.addr = dwarf_expr_fetch_address (ctx, 0);
341 p->v.mem.in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
343 else if (p->location == DWARF_VALUE_IMPLICIT_POINTER)
345 p->v.ptr.die.cu_off = ctx->len;
346 p->v.ptr.offset = value_as_long (dwarf_expr_fetch (ctx, 0));
348 else if (p->location == DWARF_VALUE_REGISTER)
349 p->v.regno = value_as_long (dwarf_expr_fetch (ctx, 0));
352 p->v.value = dwarf_expr_fetch (ctx, 0);
356 /* Evaluate the expression at ADDR (LEN bytes long) using the context
360 dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr,
363 int old_recursion_depth = ctx->recursion_depth;
365 execute_stack_op (ctx, addr, addr + len);
367 /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here. */
369 gdb_assert (ctx->recursion_depth == old_recursion_depth);
372 /* Helper to read a uleb128 value or throw an error. */
375 safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
378 buf = gdb_read_uleb128 (buf, buf_end, r);
380 error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
384 /* Helper to read a sleb128 value or throw an error. */
387 safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
390 buf = gdb_read_sleb128 (buf, buf_end, r);
392 error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
397 safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
399 buf = gdb_skip_leb128 (buf, buf_end);
401 error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
406 /* Check that the current operator is either at the end of an
407 expression, or that it is followed by a composition operator. */
410 dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
413 /* It seems like DW_OP_GNU_uninit should be handled here. However,
414 it doesn't seem to make sense for DW_OP_*_value, and it was not
415 checked at the other place that this function is called. */
416 if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece)
417 error (_("DWARF-2 expression error: `%s' operations must be "
418 "used either alone or in conjunction with DW_OP_piece "
419 "or DW_OP_bit_piece."),
423 /* Return true iff the types T1 and T2 are "the same". This only does
424 checks that might reasonably be needed to compare DWARF base
428 base_types_equal_p (struct type *t1, struct type *t2)
430 if (TYPE_CODE (t1) != TYPE_CODE (t2))
432 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
434 return TYPE_LENGTH (t1) == TYPE_LENGTH (t2);
437 /* A convenience function to call get_base_type on CTX and return the
438 result. DIE is the DIE whose type we need. SIZE is non-zero if
439 this function should verify that the resulting type has the correct
443 dwarf_get_base_type (struct dwarf_expr_context *ctx, cu_offset die, int size)
447 if (ctx->funcs->get_base_type)
449 result = ctx->funcs->get_base_type (ctx, die);
451 error (_("Could not find type for DW_OP_GNU_const_type"));
452 if (size != 0 && TYPE_LENGTH (result) != size)
453 error (_("DW_OP_GNU_const_type has different sizes for type and data"));
456 /* Anything will do. */
457 result = builtin_type (ctx->gdbarch)->builtin_int;
462 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
463 DWARF register number. Otherwise return -1. */
466 dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end)
472 if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
474 if (buf_end - buf != 1)
476 return *buf - DW_OP_reg0;
479 if (*buf == DW_OP_GNU_regval_type)
482 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
485 buf = gdb_skip_leb128 (buf, buf_end);
489 else if (*buf == DW_OP_regx)
492 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
498 if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
503 /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
504 DW_OP_deref* return the DWARF register number. Otherwise return -1.
505 DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
506 size from DW_OP_deref_size. */
509 dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end,
510 CORE_ADDR *deref_size_return)
518 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
520 dwarf_reg = *buf - DW_OP_breg0;
525 else if (*buf == DW_OP_bregx)
528 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
531 if ((int) dwarf_reg != dwarf_reg)
537 buf = gdb_read_sleb128 (buf, buf_end, &offset);
543 if (*buf == DW_OP_deref)
546 *deref_size_return = -1;
548 else if (*buf == DW_OP_deref_size)
553 *deref_size_return = *buf++;
564 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
565 in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */
568 dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
569 CORE_ADDR *fb_offset_return)
576 if (*buf != DW_OP_fbreg)
580 buf = gdb_read_sleb128 (buf, buf_end, &fb_offset);
583 *fb_offset_return = fb_offset;
584 if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
590 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
591 in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0.
592 The matched SP register number depends on GDBARCH. */
595 dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
596 const gdb_byte *buf_end, CORE_ADDR *sp_offset_return)
603 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
605 dwarf_reg = *buf - DW_OP_breg0;
610 if (*buf != DW_OP_bregx)
613 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
618 if (gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_reg)
619 != gdbarch_sp_regnum (gdbarch))
622 buf = gdb_read_sleb128 (buf, buf_end, &sp_offset);
625 *sp_offset_return = sp_offset;
626 if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
632 /* The engine for the expression evaluator. Using the context in CTX,
633 evaluate the expression between OP_PTR and OP_END. */
636 execute_stack_op (struct dwarf_expr_context *ctx,
637 const gdb_byte *op_ptr, const gdb_byte *op_end)
639 enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
640 /* Old-style "untyped" DWARF values need special treatment in a
641 couple of places, specifically DW_OP_mod and DW_OP_shr. We need
642 a special type for these values so we can distinguish them from
643 values that have an explicit type, because explicitly-typed
644 values do not need special treatment. This special type must be
645 different (in the `==' sense) from any base type coming from the
647 struct type *address_type = dwarf_expr_address_type (ctx);
649 ctx->location = DWARF_VALUE_MEMORY;
650 ctx->initialized = 1; /* Default is initialized. */
652 if (ctx->recursion_depth > ctx->max_recursion_depth)
653 error (_("DWARF-2 expression error: Loop detected (%d)."),
654 ctx->recursion_depth);
655 ctx->recursion_depth++;
657 while (op_ptr < op_end)
659 enum dwarf_location_atom op = *op_ptr++;
661 /* Assume the value is not in stack memory.
662 Code that knows otherwise sets this to 1.
663 Some arithmetic on stack addresses can probably be assumed to still
664 be a stack address, but we skip this complication for now.
665 This is just an optimization, so it's always ok to punt
666 and leave this as 0. */
667 int in_stack_memory = 0;
668 uint64_t uoffset, reg;
670 struct value *result_val = NULL;
672 /* The DWARF expression might have a bug causing an infinite
673 loop. In that case, quitting is the only way out. */
710 result = op - DW_OP_lit0;
711 result_val = value_from_ulongest (address_type, result);
715 result = extract_unsigned_integer (op_ptr,
716 ctx->addr_size, byte_order);
717 op_ptr += ctx->addr_size;
718 /* Some versions of GCC emit DW_OP_addr before
719 DW_OP_GNU_push_tls_address. In this case the value is an
720 index, not an address. We don't support things like
721 branching between the address and the TLS op. */
722 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
723 result += ctx->offset;
724 result_val = value_from_ulongest (address_type, result);
727 case DW_OP_GNU_addr_index:
728 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
729 result = (ctx->funcs->get_addr_index) (ctx->baton, uoffset);
730 result_val = value_from_ulongest (address_type, result);
734 result = extract_unsigned_integer (op_ptr, 1, byte_order);
735 result_val = value_from_ulongest (address_type, result);
739 result = extract_signed_integer (op_ptr, 1, byte_order);
740 result_val = value_from_ulongest (address_type, result);
744 result = extract_unsigned_integer (op_ptr, 2, byte_order);
745 result_val = value_from_ulongest (address_type, result);
749 result = extract_signed_integer (op_ptr, 2, byte_order);
750 result_val = value_from_ulongest (address_type, result);
754 result = extract_unsigned_integer (op_ptr, 4, byte_order);
755 result_val = value_from_ulongest (address_type, result);
759 result = extract_signed_integer (op_ptr, 4, byte_order);
760 result_val = value_from_ulongest (address_type, result);
764 result = extract_unsigned_integer (op_ptr, 8, byte_order);
765 result_val = value_from_ulongest (address_type, result);
769 result = extract_signed_integer (op_ptr, 8, byte_order);
770 result_val = value_from_ulongest (address_type, result);
774 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
776 result_val = value_from_ulongest (address_type, result);
779 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
781 result_val = value_from_ulongest (address_type, result);
784 /* The DW_OP_reg operations are required to occur alone in
785 location expressions. */
819 && *op_ptr != DW_OP_piece
820 && *op_ptr != DW_OP_bit_piece
821 && *op_ptr != DW_OP_GNU_uninit)
822 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
823 "used either alone or in conjunction with DW_OP_piece "
824 "or DW_OP_bit_piece."));
826 result = op - DW_OP_reg0;
827 result_val = value_from_ulongest (address_type, result);
828 ctx->location = DWARF_VALUE_REGISTER;
832 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
833 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
836 result_val = value_from_ulongest (address_type, result);
837 ctx->location = DWARF_VALUE_REGISTER;
840 case DW_OP_implicit_value:
844 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
845 if (op_ptr + len > op_end)
846 error (_("DW_OP_implicit_value: too few bytes available."));
849 ctx->location = DWARF_VALUE_LITERAL;
851 dwarf_expr_require_composition (op_ptr, op_end,
852 "DW_OP_implicit_value");
856 case DW_OP_stack_value:
857 ctx->location = DWARF_VALUE_STACK;
858 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
861 case DW_OP_GNU_implicit_pointer:
865 if (ctx->ref_addr_size == -1)
866 error (_("DWARF-2 expression error: DW_OP_GNU_implicit_pointer "
867 "is not allowed in frame context"));
869 /* The referred-to DIE of cu_offset kind. */
870 ctx->len = extract_unsigned_integer (op_ptr, ctx->ref_addr_size,
872 op_ptr += ctx->ref_addr_size;
874 /* The byte offset into the data. */
875 op_ptr = safe_read_sleb128 (op_ptr, op_end, &len);
876 result = (ULONGEST) len;
877 result_val = value_from_ulongest (address_type, result);
879 ctx->location = DWARF_VALUE_IMPLICIT_POINTER;
880 dwarf_expr_require_composition (op_ptr, op_end,
881 "DW_OP_GNU_implicit_pointer");
918 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
919 result = (ctx->funcs->read_reg) (ctx->baton, op - DW_OP_breg0);
921 result_val = value_from_ulongest (address_type, result);
926 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
927 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
928 result = (ctx->funcs->read_reg) (ctx->baton, reg);
930 result_val = value_from_ulongest (address_type, result);
935 const gdb_byte *datastart;
937 unsigned int before_stack_len;
939 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
940 /* Rather than create a whole new context, we simply
941 record the stack length before execution, then reset it
942 afterwards, effectively erasing whatever the recursive
944 before_stack_len = ctx->stack_len;
945 /* FIXME: cagney/2003-03-26: This code should be using
946 get_frame_base_address(), and then implement a dwarf2
947 specific this_base method. */
948 (ctx->funcs->get_frame_base) (ctx->baton, &datastart, &datalen);
949 dwarf_expr_eval (ctx, datastart, datalen);
950 if (ctx->location == DWARF_VALUE_MEMORY)
951 result = dwarf_expr_fetch_address (ctx, 0);
952 else if (ctx->location == DWARF_VALUE_REGISTER)
953 result = (ctx->funcs->read_reg) (ctx->baton,
954 value_as_long (dwarf_expr_fetch (ctx, 0)));
956 error (_("Not implemented: computing frame "
957 "base using explicit value operator"));
958 result = result + offset;
959 result_val = value_from_ulongest (address_type, result);
961 ctx->stack_len = before_stack_len;
962 ctx->location = DWARF_VALUE_MEMORY;
967 result_val = dwarf_expr_fetch (ctx, 0);
968 in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
972 dwarf_expr_pop (ctx);
977 result_val = dwarf_expr_fetch (ctx, offset);
978 in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, offset);
983 struct dwarf_stack_value t1, t2;
985 if (ctx->stack_len < 2)
986 error (_("Not enough elements for "
987 "DW_OP_swap. Need 2, have %d."),
989 t1 = ctx->stack[ctx->stack_len - 1];
990 t2 = ctx->stack[ctx->stack_len - 2];
991 ctx->stack[ctx->stack_len - 1] = t2;
992 ctx->stack[ctx->stack_len - 2] = t1;
997 result_val = dwarf_expr_fetch (ctx, 1);
998 in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 1);
1003 struct dwarf_stack_value t1, t2, t3;
1005 if (ctx->stack_len < 3)
1006 error (_("Not enough elements for "
1007 "DW_OP_rot. Need 3, have %d."),
1009 t1 = ctx->stack[ctx->stack_len - 1];
1010 t2 = ctx->stack[ctx->stack_len - 2];
1011 t3 = ctx->stack[ctx->stack_len - 3];
1012 ctx->stack[ctx->stack_len - 1] = t2;
1013 ctx->stack[ctx->stack_len - 2] = t3;
1014 ctx->stack[ctx->stack_len - 3] = t1;
1019 case DW_OP_deref_size:
1020 case DW_OP_GNU_deref_type:
1022 int addr_size = (op == DW_OP_deref ? ctx->addr_size : *op_ptr++);
1023 gdb_byte *buf = alloca (addr_size);
1024 CORE_ADDR addr = dwarf_expr_fetch_address (ctx, 0);
1027 dwarf_expr_pop (ctx);
1029 if (op == DW_OP_GNU_deref_type)
1033 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1034 type_die.cu_off = uoffset;
1035 type = dwarf_get_base_type (ctx, type_die, 0);
1038 type = address_type;
1040 (ctx->funcs->read_mem) (ctx->baton, buf, addr, addr_size);
1042 /* If the size of the object read from memory is different
1043 from the type length, we need to zero-extend it. */
1044 if (TYPE_LENGTH (type) != addr_size)
1047 extract_unsigned_integer (buf, addr_size, byte_order);
1049 buf = alloca (TYPE_LENGTH (type));
1050 store_unsigned_integer (buf, TYPE_LENGTH (type),
1051 byte_order, result);
1054 result_val = value_from_contents_and_address (type, buf, addr);
1061 case DW_OP_plus_uconst:
1063 /* Unary operations. */
1064 result_val = dwarf_expr_fetch (ctx, 0);
1065 dwarf_expr_pop (ctx);
1070 if (value_less (result_val,
1071 value_zero (value_type (result_val), not_lval)))
1072 result_val = value_neg (result_val);
1075 result_val = value_neg (result_val);
1078 dwarf_require_integral (value_type (result_val));
1079 result_val = value_complement (result_val);
1081 case DW_OP_plus_uconst:
1082 dwarf_require_integral (value_type (result_val));
1083 result = value_as_long (result_val);
1084 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1086 result_val = value_from_ulongest (address_type, result);
1110 /* Binary operations. */
1111 struct value *first, *second;
1113 second = dwarf_expr_fetch (ctx, 0);
1114 dwarf_expr_pop (ctx);
1116 first = dwarf_expr_fetch (ctx, 0);
1117 dwarf_expr_pop (ctx);
1119 if (! base_types_equal_p (value_type (first), value_type (second)))
1120 error (_("Incompatible types on DWARF stack"));
1125 dwarf_require_integral (value_type (first));
1126 dwarf_require_integral (value_type (second));
1127 result_val = value_binop (first, second, BINOP_BITWISE_AND);
1130 result_val = value_binop (first, second, BINOP_DIV);
1133 result_val = value_binop (first, second, BINOP_SUB);
1138 struct type *orig_type = value_type (first);
1140 /* We have to special-case "old-style" untyped values
1141 -- these must have mod computed using unsigned
1143 if (orig_type == address_type)
1146 = get_unsigned_type (ctx->gdbarch, orig_type);
1149 first = value_cast (utype, first);
1150 second = value_cast (utype, second);
1152 /* Note that value_binop doesn't handle float or
1153 decimal float here. This seems unimportant. */
1154 result_val = value_binop (first, second, BINOP_MOD);
1156 result_val = value_cast (orig_type, result_val);
1160 result_val = value_binop (first, second, BINOP_MUL);
1163 dwarf_require_integral (value_type (first));
1164 dwarf_require_integral (value_type (second));
1165 result_val = value_binop (first, second, BINOP_BITWISE_IOR);
1168 result_val = value_binop (first, second, BINOP_ADD);
1171 dwarf_require_integral (value_type (first));
1172 dwarf_require_integral (value_type (second));
1173 result_val = value_binop (first, second, BINOP_LSH);
1176 dwarf_require_integral (value_type (first));
1177 dwarf_require_integral (value_type (second));
1178 if (!TYPE_UNSIGNED (value_type (first)))
1181 = get_unsigned_type (ctx->gdbarch, value_type (first));
1183 first = value_cast (utype, first);
1186 result_val = value_binop (first, second, BINOP_RSH);
1187 /* Make sure we wind up with the same type we started
1189 if (value_type (result_val) != value_type (second))
1190 result_val = value_cast (value_type (second), result_val);
1193 dwarf_require_integral (value_type (first));
1194 dwarf_require_integral (value_type (second));
1195 if (TYPE_UNSIGNED (value_type (first)))
1198 = get_signed_type (ctx->gdbarch, value_type (first));
1200 first = value_cast (stype, first);
1203 result_val = value_binop (first, second, BINOP_RSH);
1204 /* Make sure we wind up with the same type we started
1206 if (value_type (result_val) != value_type (second))
1207 result_val = value_cast (value_type (second), result_val);
1210 dwarf_require_integral (value_type (first));
1211 dwarf_require_integral (value_type (second));
1212 result_val = value_binop (first, second, BINOP_BITWISE_XOR);
1215 /* A <= B is !(B < A). */
1216 result = ! value_less (second, first);
1217 result_val = value_from_ulongest (address_type, result);
1220 /* A >= B is !(A < B). */
1221 result = ! value_less (first, second);
1222 result_val = value_from_ulongest (address_type, result);
1225 result = value_equal (first, second);
1226 result_val = value_from_ulongest (address_type, result);
1229 result = value_less (first, second);
1230 result_val = value_from_ulongest (address_type, result);
1233 /* A > B is B < A. */
1234 result = value_less (second, first);
1235 result_val = value_from_ulongest (address_type, result);
1238 result = ! value_equal (first, second);
1239 result_val = value_from_ulongest (address_type, result);
1242 internal_error (__FILE__, __LINE__,
1243 _("Can't be reached."));
1248 case DW_OP_call_frame_cfa:
1249 result = (ctx->funcs->get_frame_cfa) (ctx->baton);
1250 result_val = value_from_ulongest (address_type, result);
1251 in_stack_memory = 1;
1254 case DW_OP_GNU_push_tls_address:
1255 /* Variable is at a constant offset in the thread-local
1256 storage block into the objfile for the current thread and
1257 the dynamic linker module containing this expression. Here
1258 we return returns the offset from that base. The top of the
1259 stack has the offset from the beginning of the thread
1260 control block at which the variable is located. Nothing
1261 should follow this operator, so the top of stack would be
1263 result = value_as_long (dwarf_expr_fetch (ctx, 0));
1264 dwarf_expr_pop (ctx);
1265 result = (ctx->funcs->get_tls_address) (ctx->baton, result);
1266 result_val = value_from_ulongest (address_type, result);
1270 offset = extract_signed_integer (op_ptr, 2, byte_order);
1279 offset = extract_signed_integer (op_ptr, 2, byte_order);
1281 val = dwarf_expr_fetch (ctx, 0);
1282 dwarf_require_integral (value_type (val));
1283 if (value_as_long (val) != 0)
1285 dwarf_expr_pop (ctx);
1296 /* Record the piece. */
1297 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1298 add_piece (ctx, 8 * size, 0);
1300 /* Pop off the address/regnum, and reset the location
1302 if (ctx->location != DWARF_VALUE_LITERAL
1303 && ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
1304 dwarf_expr_pop (ctx);
1305 ctx->location = DWARF_VALUE_MEMORY;
1309 case DW_OP_bit_piece:
1311 uint64_t size, offset;
1313 /* Record the piece. */
1314 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1315 op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
1316 add_piece (ctx, size, offset);
1318 /* Pop off the address/regnum, and reset the location
1320 if (ctx->location != DWARF_VALUE_LITERAL
1321 && ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
1322 dwarf_expr_pop (ctx);
1323 ctx->location = DWARF_VALUE_MEMORY;
1327 case DW_OP_GNU_uninit:
1328 if (op_ptr != op_end)
1329 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
1330 "be the very last op."));
1332 ctx->initialized = 0;
1339 offset.cu_off = extract_unsigned_integer (op_ptr, 2, byte_order);
1341 ctx->funcs->dwarf_call (ctx, offset);
1349 offset.cu_off = extract_unsigned_integer (op_ptr, 4, byte_order);
1351 ctx->funcs->dwarf_call (ctx, offset);
1355 case DW_OP_GNU_entry_value:
1358 CORE_ADDR deref_size;
1359 union call_site_parameter_u kind_u;
1361 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
1362 if (op_ptr + len > op_end)
1363 error (_("DW_OP_GNU_entry_value: too few bytes available."));
1365 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len);
1366 if (kind_u.dwarf_reg != -1)
1369 ctx->funcs->push_dwarf_reg_entry_value (ctx,
1370 CALL_SITE_PARAMETER_DWARF_REG,
1372 -1 /* deref_size */);
1376 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr,
1379 if (kind_u.dwarf_reg != -1)
1381 if (deref_size == -1)
1382 deref_size = ctx->addr_size;
1384 ctx->funcs->push_dwarf_reg_entry_value (ctx,
1385 CALL_SITE_PARAMETER_DWARF_REG,
1386 kind_u, deref_size);
1390 error (_("DWARF-2 expression error: DW_OP_GNU_entry_value is "
1391 "supported only for single DW_OP_reg* "
1392 "or for DW_OP_breg*(0)+DW_OP_deref*"));
1395 case DW_OP_GNU_parameter_ref:
1397 union call_site_parameter_u kind_u;
1399 kind_u.param_offset.cu_off = extract_unsigned_integer (op_ptr, 4,
1402 ctx->funcs->push_dwarf_reg_entry_value (ctx,
1403 CALL_SITE_PARAMETER_PARAM_OFFSET,
1405 -1 /* deref_size */);
1409 case DW_OP_GNU_const_type:
1413 const gdb_byte *data;
1416 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1417 type_die.cu_off = uoffset;
1422 type = dwarf_get_base_type (ctx, type_die, n);
1423 result_val = value_from_contents (type, data);
1427 case DW_OP_GNU_regval_type:
1432 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1433 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1434 type_die.cu_off = uoffset;
1436 type = dwarf_get_base_type (ctx, type_die, 0);
1437 result = (ctx->funcs->read_reg) (ctx->baton, reg);
1438 result_val = value_from_ulongest (address_type, result);
1439 result_val = value_from_contents (type,
1440 value_contents_all (result_val));
1444 case DW_OP_GNU_convert:
1445 case DW_OP_GNU_reinterpret:
1450 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1451 type_die.cu_off = uoffset;
1453 if (type_die.cu_off == 0)
1454 type = address_type;
1456 type = dwarf_get_base_type (ctx, type_die, 0);
1458 result_val = dwarf_expr_fetch (ctx, 0);
1459 dwarf_expr_pop (ctx);
1461 if (op == DW_OP_GNU_convert)
1462 result_val = value_cast (type, result_val);
1463 else if (type == value_type (result_val))
1467 else if (TYPE_LENGTH (type)
1468 != TYPE_LENGTH (value_type (result_val)))
1469 error (_("DW_OP_GNU_reinterpret has wrong size"));
1472 = value_from_contents (type,
1473 value_contents_all (result_val));
1478 error (_("Unhandled dwarf expression opcode 0x%x"), op);
1481 /* Most things push a result value. */
1482 gdb_assert (result_val != NULL);
1483 dwarf_expr_push (ctx, result_val, in_stack_memory);
1488 /* To simplify our main caller, if the result is an implicit
1489 pointer, then make a pieced value. This is ok because we can't
1490 have implicit pointers in contexts where pieces are invalid. */
1491 if (ctx->location == DWARF_VALUE_IMPLICIT_POINTER)
1492 add_piece (ctx, 8 * ctx->addr_size, 0);
1495 ctx->recursion_depth--;
1496 gdb_assert (ctx->recursion_depth >= 0);
1499 /* Stub dwarf_expr_context_funcs.get_frame_base implementation. */
1502 ctx_no_get_frame_base (void *baton, const gdb_byte **start, size_t *length)
1504 error (_("%s is invalid in this context"), "DW_OP_fbreg");
1507 /* Stub dwarf_expr_context_funcs.get_frame_cfa implementation. */
1510 ctx_no_get_frame_cfa (void *baton)
1512 error (_("%s is invalid in this context"), "DW_OP_call_frame_cfa");
1515 /* Stub dwarf_expr_context_funcs.get_frame_pc implementation. */
1518 ctx_no_get_frame_pc (void *baton)
1520 error (_("%s is invalid in this context"), "DW_OP_GNU_implicit_pointer");
1523 /* Stub dwarf_expr_context_funcs.get_tls_address implementation. */
1526 ctx_no_get_tls_address (void *baton, CORE_ADDR offset)
1528 error (_("%s is invalid in this context"), "DW_OP_GNU_push_tls_address");
1531 /* Stub dwarf_expr_context_funcs.dwarf_call implementation. */
1534 ctx_no_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
1536 error (_("%s is invalid in this context"), "DW_OP_call*");
1539 /* Stub dwarf_expr_context_funcs.get_base_type implementation. */
1542 ctx_no_get_base_type (struct dwarf_expr_context *ctx, cu_offset die)
1544 error (_("Support for typed DWARF is not supported in this context"));
1547 /* Stub dwarf_expr_context_funcs.push_dwarf_block_entry_value
1551 ctx_no_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
1552 enum call_site_parameter_kind kind,
1553 union call_site_parameter_u kind_u,
1556 internal_error (__FILE__, __LINE__,
1557 _("Support for DW_OP_GNU_entry_value is unimplemented"));
1560 /* Stub dwarf_expr_context_funcs.get_addr_index implementation. */
1563 ctx_no_get_addr_index (void *baton, unsigned int index)
1565 error (_("%s is invalid in this context"), "DW_OP_GNU_addr_index");
1568 /* Provide a prototype to silence -Wmissing-prototypes. */
1569 extern initialize_file_ftype _initialize_dwarf2expr;
1572 _initialize_dwarf2expr (void)
1575 = gdbarch_data_register_post_init (dwarf_gdbarch_types_init);