1 /* DWARF 2 Expression Evaluator.
3 Copyright (C) 2001-2015 Free Software Foundation, Inc.
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"
30 /* Local prototypes. */
32 static void execute_stack_op (struct dwarf_expr_context *,
33 const gdb_byte *, const gdb_byte *);
35 /* Cookie for gdbarch data. */
37 static struct gdbarch_data *dwarf_arch_cookie;
39 /* This holds gdbarch-specific types used by the DWARF expression
40 evaluator. See comments in execute_stack_op. */
42 struct dwarf_gdbarch_types
44 struct type *dw_types[3];
47 /* Allocate and fill in dwarf_gdbarch_types for an arch. */
50 dwarf_gdbarch_types_init (struct gdbarch *gdbarch)
52 struct dwarf_gdbarch_types *types
53 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct dwarf_gdbarch_types);
55 /* The types themselves are lazily initialized. */
60 /* Return the type used for DWARF operations where the type is
61 unspecified in the DWARF spec. Only certain sizes are
65 dwarf_expr_address_type (struct dwarf_expr_context *ctx)
67 struct dwarf_gdbarch_types *types = gdbarch_data (ctx->gdbarch,
71 if (ctx->addr_size == 2)
73 else if (ctx->addr_size == 4)
75 else if (ctx->addr_size == 8)
78 error (_("Unsupported address size in DWARF expressions: %d bits"),
81 if (types->dw_types[ndx] == NULL)
83 = arch_integer_type (ctx->gdbarch,
85 0, "<signed DWARF address type>");
87 return types->dw_types[ndx];
90 /* Create a new context for the expression evaluator. */
92 struct dwarf_expr_context *
93 new_dwarf_expr_context (void)
95 struct dwarf_expr_context *retval;
97 retval = XCNEW (struct dwarf_expr_context);
98 retval->stack_len = 0;
99 retval->stack_allocated = 10;
100 retval->stack = XNEWVEC (struct dwarf_stack_value, retval->stack_allocated);
101 retval->num_pieces = 0;
103 retval->max_recursion_depth = 0x100;
107 /* Release the memory allocated to CTX. */
110 free_dwarf_expr_context (struct dwarf_expr_context *ctx)
117 /* Helper for make_cleanup_free_dwarf_expr_context. */
120 free_dwarf_expr_context_cleanup (void *arg)
122 free_dwarf_expr_context (arg);
125 /* Return a cleanup that calls free_dwarf_expr_context. */
128 make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context *ctx)
130 return make_cleanup (free_dwarf_expr_context_cleanup, ctx);
133 /* Expand the memory allocated to CTX's stack to contain at least
134 NEED more elements than are currently used. */
137 dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need)
139 if (ctx->stack_len + need > ctx->stack_allocated)
141 size_t newlen = ctx->stack_len + need + 10;
143 ctx->stack = XRESIZEVEC (struct dwarf_stack_value, ctx->stack, newlen);
144 ctx->stack_allocated = newlen;
148 /* Push VALUE onto CTX's stack. */
151 dwarf_expr_push (struct dwarf_expr_context *ctx, struct value *value,
154 struct dwarf_stack_value *v;
156 dwarf_expr_grow_stack (ctx, 1);
157 v = &ctx->stack[ctx->stack_len++];
159 v->in_stack_memory = in_stack_memory;
162 /* Push VALUE onto CTX's stack. */
165 dwarf_expr_push_address (struct dwarf_expr_context *ctx, CORE_ADDR value,
168 dwarf_expr_push (ctx,
169 value_from_ulongest (dwarf_expr_address_type (ctx), value),
173 /* Pop the top item off of CTX's stack. */
176 dwarf_expr_pop (struct dwarf_expr_context *ctx)
178 if (ctx->stack_len <= 0)
179 error (_("dwarf expression stack underflow"));
183 /* Retrieve the N'th item on CTX's stack. */
186 dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n)
188 if (ctx->stack_len <= n)
189 error (_("Asked for position %d of stack, "
190 "stack only has %d elements on it."),
192 return ctx->stack[ctx->stack_len - (1 + n)].value;
195 /* Require that TYPE be an integral type; throw an exception if not. */
198 dwarf_require_integral (struct type *type)
200 if (TYPE_CODE (type) != TYPE_CODE_INT
201 && TYPE_CODE (type) != TYPE_CODE_CHAR
202 && TYPE_CODE (type) != TYPE_CODE_BOOL)
203 error (_("integral type expected in DWARF expression"));
206 /* Return the unsigned form of TYPE. TYPE is necessarily an integral
210 get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
212 switch (TYPE_LENGTH (type))
215 return builtin_type (gdbarch)->builtin_uint8;
217 return builtin_type (gdbarch)->builtin_uint16;
219 return builtin_type (gdbarch)->builtin_uint32;
221 return builtin_type (gdbarch)->builtin_uint64;
223 error (_("no unsigned variant found for type, while evaluating "
224 "DWARF expression"));
228 /* Return the signed form of TYPE. TYPE is necessarily an integral
232 get_signed_type (struct gdbarch *gdbarch, struct type *type)
234 switch (TYPE_LENGTH (type))
237 return builtin_type (gdbarch)->builtin_int8;
239 return builtin_type (gdbarch)->builtin_int16;
241 return builtin_type (gdbarch)->builtin_int32;
243 return builtin_type (gdbarch)->builtin_int64;
245 error (_("no signed variant found for type, while evaluating "
246 "DWARF expression"));
250 /* Retrieve the N'th item on CTX's stack, converted to an address. */
253 dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n)
255 struct value *result_val = dwarf_expr_fetch (ctx, n);
256 enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
259 dwarf_require_integral (value_type (result_val));
260 result = extract_unsigned_integer (value_contents (result_val),
261 TYPE_LENGTH (value_type (result_val)),
264 /* For most architectures, calling extract_unsigned_integer() alone
265 is sufficient for extracting an address. However, some
266 architectures (e.g. MIPS) use signed addresses and using
267 extract_unsigned_integer() will not produce a correct
268 result. Make sure we invoke gdbarch_integer_to_address()
269 for those architectures which require it. */
270 if (gdbarch_integer_to_address_p (ctx->gdbarch))
272 gdb_byte *buf = (gdb_byte *) alloca (ctx->addr_size);
273 struct type *int_type = get_unsigned_type (ctx->gdbarch,
274 value_type (result_val));
276 store_unsigned_integer (buf, ctx->addr_size, byte_order, result);
277 return gdbarch_integer_to_address (ctx->gdbarch, int_type, buf);
280 return (CORE_ADDR) result;
283 /* Retrieve the in_stack_memory flag of the N'th item on CTX's stack. */
286 dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n)
288 if (ctx->stack_len <= n)
289 error (_("Asked for position %d of stack, "
290 "stack only has %d elements on it."),
292 return ctx->stack[ctx->stack_len - (1 + n)].in_stack_memory;
295 /* Return true if the expression stack is empty. */
298 dwarf_expr_stack_empty_p (struct dwarf_expr_context *ctx)
300 return ctx->stack_len == 0;
303 /* Add a new piece to CTX's piece list. */
305 add_piece (struct dwarf_expr_context *ctx, ULONGEST size, ULONGEST offset)
307 struct dwarf_expr_piece *p;
312 = XRESIZEVEC (struct dwarf_expr_piece, ctx->pieces, ctx->num_pieces);
314 p = &ctx->pieces[ctx->num_pieces - 1];
315 p->location = ctx->location;
319 if (p->location == DWARF_VALUE_LITERAL)
321 p->v.literal.data = ctx->data;
322 p->v.literal.length = ctx->len;
324 else if (dwarf_expr_stack_empty_p (ctx))
326 p->location = DWARF_VALUE_OPTIMIZED_OUT;
327 /* Also reset the context's location, for our callers. This is
328 a somewhat strange approach, but this lets us avoid setting
329 the location to DWARF_VALUE_MEMORY in all the individual
330 cases in the evaluator. */
331 ctx->location = DWARF_VALUE_OPTIMIZED_OUT;
333 else if (p->location == DWARF_VALUE_MEMORY)
335 p->v.mem.addr = dwarf_expr_fetch_address (ctx, 0);
336 p->v.mem.in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
338 else if (p->location == DWARF_VALUE_IMPLICIT_POINTER)
340 p->v.ptr.die.sect_off = ctx->len;
341 p->v.ptr.offset = value_as_long (dwarf_expr_fetch (ctx, 0));
343 else if (p->location == DWARF_VALUE_REGISTER)
344 p->v.regno = value_as_long (dwarf_expr_fetch (ctx, 0));
347 p->v.value = dwarf_expr_fetch (ctx, 0);
351 /* Evaluate the expression at ADDR (LEN bytes long) using the context
355 dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr,
358 int old_recursion_depth = ctx->recursion_depth;
360 execute_stack_op (ctx, addr, addr + len);
362 /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here. */
364 gdb_assert (ctx->recursion_depth == old_recursion_depth);
367 /* Helper to read a uleb128 value or throw an error. */
370 safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
373 buf = gdb_read_uleb128 (buf, buf_end, r);
375 error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
379 /* Helper to read a sleb128 value or throw an error. */
382 safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
385 buf = gdb_read_sleb128 (buf, buf_end, r);
387 error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
392 safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
394 buf = gdb_skip_leb128 (buf, buf_end);
396 error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
401 /* Check that the current operator is either at the end of an
402 expression, or that it is followed by a composition operator. */
405 dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
408 /* It seems like DW_OP_GNU_uninit should be handled here. However,
409 it doesn't seem to make sense for DW_OP_*_value, and it was not
410 checked at the other place that this function is called. */
411 if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece)
412 error (_("DWARF-2 expression error: `%s' operations must be "
413 "used either alone or in conjunction with DW_OP_piece "
414 "or DW_OP_bit_piece."),
418 /* Return true iff the types T1 and T2 are "the same". This only does
419 checks that might reasonably be needed to compare DWARF base
423 base_types_equal_p (struct type *t1, struct type *t2)
425 if (TYPE_CODE (t1) != TYPE_CODE (t2))
427 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
429 return TYPE_LENGTH (t1) == TYPE_LENGTH (t2);
432 /* A convenience function to call get_base_type on CTX and return the
433 result. DIE is the DIE whose type we need. SIZE is non-zero if
434 this function should verify that the resulting type has the correct
438 dwarf_get_base_type (struct dwarf_expr_context *ctx, cu_offset die, int size)
442 if (ctx->funcs->get_base_type)
444 result = ctx->funcs->get_base_type (ctx, die);
446 error (_("Could not find type for DW_OP_GNU_const_type"));
447 if (size != 0 && TYPE_LENGTH (result) != size)
448 error (_("DW_OP_GNU_const_type has different sizes for type and data"));
451 /* Anything will do. */
452 result = builtin_type (ctx->gdbarch)->builtin_int;
457 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
458 DWARF register number. Otherwise return -1. */
461 dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end)
467 if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
469 if (buf_end - buf != 1)
471 return *buf - DW_OP_reg0;
474 if (*buf == DW_OP_GNU_regval_type)
477 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
480 buf = gdb_skip_leb128 (buf, buf_end);
484 else if (*buf == DW_OP_regx)
487 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
493 if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
498 /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
499 DW_OP_deref* return the DWARF register number. Otherwise return -1.
500 DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
501 size from DW_OP_deref_size. */
504 dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end,
505 CORE_ADDR *deref_size_return)
513 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
515 dwarf_reg = *buf - DW_OP_breg0;
520 else if (*buf == DW_OP_bregx)
523 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
526 if ((int) dwarf_reg != dwarf_reg)
532 buf = gdb_read_sleb128 (buf, buf_end, &offset);
538 if (*buf == DW_OP_deref)
541 *deref_size_return = -1;
543 else if (*buf == DW_OP_deref_size)
548 *deref_size_return = *buf++;
559 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
560 in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */
563 dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
564 CORE_ADDR *fb_offset_return)
571 if (*buf != DW_OP_fbreg)
575 buf = gdb_read_sleb128 (buf, buf_end, &fb_offset);
578 *fb_offset_return = fb_offset;
579 if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
585 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
586 in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0.
587 The matched SP register number depends on GDBARCH. */
590 dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
591 const gdb_byte *buf_end, CORE_ADDR *sp_offset_return)
598 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
600 dwarf_reg = *buf - DW_OP_breg0;
605 if (*buf != DW_OP_bregx)
608 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
613 if (gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_reg)
614 != gdbarch_sp_regnum (gdbarch))
617 buf = gdb_read_sleb128 (buf, buf_end, &sp_offset);
620 *sp_offset_return = sp_offset;
621 if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
627 /* The engine for the expression evaluator. Using the context in CTX,
628 evaluate the expression between OP_PTR and OP_END. */
631 execute_stack_op (struct dwarf_expr_context *ctx,
632 const gdb_byte *op_ptr, const gdb_byte *op_end)
634 enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
635 /* Old-style "untyped" DWARF values need special treatment in a
636 couple of places, specifically DW_OP_mod and DW_OP_shr. We need
637 a special type for these values so we can distinguish them from
638 values that have an explicit type, because explicitly-typed
639 values do not need special treatment. This special type must be
640 different (in the `==' sense) from any base type coming from the
642 struct type *address_type = dwarf_expr_address_type (ctx);
644 ctx->location = DWARF_VALUE_MEMORY;
645 ctx->initialized = 1; /* Default is initialized. */
647 if (ctx->recursion_depth > ctx->max_recursion_depth)
648 error (_("DWARF-2 expression error: Loop detected (%d)."),
649 ctx->recursion_depth);
650 ctx->recursion_depth++;
652 while (op_ptr < op_end)
654 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr++;
656 /* Assume the value is not in stack memory.
657 Code that knows otherwise sets this to 1.
658 Some arithmetic on stack addresses can probably be assumed to still
659 be a stack address, but we skip this complication for now.
660 This is just an optimization, so it's always ok to punt
661 and leave this as 0. */
662 int in_stack_memory = 0;
663 uint64_t uoffset, reg;
665 struct value *result_val = NULL;
667 /* The DWARF expression might have a bug causing an infinite
668 loop. In that case, quitting is the only way out. */
705 result = op - DW_OP_lit0;
706 result_val = value_from_ulongest (address_type, result);
710 result = extract_unsigned_integer (op_ptr,
711 ctx->addr_size, byte_order);
712 op_ptr += ctx->addr_size;
713 /* Some versions of GCC emit DW_OP_addr before
714 DW_OP_GNU_push_tls_address. In this case the value is an
715 index, not an address. We don't support things like
716 branching between the address and the TLS op. */
717 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
718 result += ctx->offset;
719 result_val = value_from_ulongest (address_type, result);
722 case DW_OP_GNU_addr_index:
723 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
724 result = (ctx->funcs->get_addr_index) (ctx->baton, uoffset);
725 result += ctx->offset;
726 result_val = value_from_ulongest (address_type, result);
728 case DW_OP_GNU_const_index:
729 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
730 result = (ctx->funcs->get_addr_index) (ctx->baton, uoffset);
731 result_val = value_from_ulongest (address_type, result);
735 result = extract_unsigned_integer (op_ptr, 1, byte_order);
736 result_val = value_from_ulongest (address_type, result);
740 result = extract_signed_integer (op_ptr, 1, byte_order);
741 result_val = value_from_ulongest (address_type, result);
745 result = extract_unsigned_integer (op_ptr, 2, byte_order);
746 result_val = value_from_ulongest (address_type, result);
750 result = extract_signed_integer (op_ptr, 2, byte_order);
751 result_val = value_from_ulongest (address_type, result);
755 result = extract_unsigned_integer (op_ptr, 4, byte_order);
756 result_val = value_from_ulongest (address_type, result);
760 result = extract_signed_integer (op_ptr, 4, byte_order);
761 result_val = value_from_ulongest (address_type, result);
765 result = extract_unsigned_integer (op_ptr, 8, byte_order);
766 result_val = value_from_ulongest (address_type, result);
770 result = extract_signed_integer (op_ptr, 8, byte_order);
771 result_val = value_from_ulongest (address_type, result);
775 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
777 result_val = value_from_ulongest (address_type, result);
780 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
782 result_val = value_from_ulongest (address_type, result);
785 /* The DW_OP_reg operations are required to occur alone in
786 location expressions. */
820 && *op_ptr != DW_OP_piece
821 && *op_ptr != DW_OP_bit_piece
822 && *op_ptr != DW_OP_GNU_uninit)
823 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
824 "used either alone or in conjunction with DW_OP_piece "
825 "or DW_OP_bit_piece."));
827 result = op - DW_OP_reg0;
828 result_val = value_from_ulongest (address_type, result);
829 ctx->location = DWARF_VALUE_REGISTER;
833 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
834 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
837 result_val = value_from_ulongest (address_type, result);
838 ctx->location = DWARF_VALUE_REGISTER;
841 case DW_OP_implicit_value:
845 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
846 if (op_ptr + len > op_end)
847 error (_("DW_OP_implicit_value: too few bytes available."));
850 ctx->location = DWARF_VALUE_LITERAL;
852 dwarf_expr_require_composition (op_ptr, op_end,
853 "DW_OP_implicit_value");
857 case DW_OP_stack_value:
858 ctx->location = DWARF_VALUE_STACK;
859 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
862 case DW_OP_GNU_implicit_pointer:
866 if (ctx->ref_addr_size == -1)
867 error (_("DWARF-2 expression error: DW_OP_GNU_implicit_pointer "
868 "is not allowed in frame context"));
870 /* The referred-to DIE of sect_offset kind. */
871 ctx->len = extract_unsigned_integer (op_ptr, ctx->ref_addr_size,
873 op_ptr += ctx->ref_addr_size;
875 /* The byte offset into the data. */
876 op_ptr = safe_read_sleb128 (op_ptr, op_end, &len);
877 result = (ULONGEST) len;
878 result_val = value_from_ulongest (address_type, result);
880 ctx->location = DWARF_VALUE_IMPLICIT_POINTER;
881 dwarf_expr_require_composition (op_ptr, op_end,
882 "DW_OP_GNU_implicit_pointer");
919 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
920 result = (ctx->funcs->read_addr_from_reg) (ctx->baton,
923 result_val = value_from_ulongest (address_type, result);
928 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
929 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
930 result = (ctx->funcs->read_addr_from_reg) (ctx->baton, reg);
932 result_val = value_from_ulongest (address_type, result);
937 const gdb_byte *datastart;
939 unsigned int before_stack_len;
941 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
942 /* Rather than create a whole new context, we simply
943 record the stack length before execution, then reset it
944 afterwards, effectively erasing whatever the recursive
946 before_stack_len = ctx->stack_len;
947 /* FIXME: cagney/2003-03-26: This code should be using
948 get_frame_base_address(), and then implement a dwarf2
949 specific this_base method. */
950 (ctx->funcs->get_frame_base) (ctx->baton, &datastart, &datalen);
951 dwarf_expr_eval (ctx, datastart, datalen);
952 if (ctx->location == DWARF_VALUE_MEMORY)
953 result = dwarf_expr_fetch_address (ctx, 0);
954 else if (ctx->location == DWARF_VALUE_REGISTER)
955 result = (ctx->funcs->read_addr_from_reg)
957 value_as_long (dwarf_expr_fetch (ctx, 0)));
959 error (_("Not implemented: computing frame "
960 "base using explicit value operator"));
961 result = result + offset;
962 result_val = value_from_ulongest (address_type, result);
964 ctx->stack_len = before_stack_len;
965 ctx->location = DWARF_VALUE_MEMORY;
970 result_val = dwarf_expr_fetch (ctx, 0);
971 in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
975 dwarf_expr_pop (ctx);
980 result_val = dwarf_expr_fetch (ctx, offset);
981 in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, offset);
986 struct dwarf_stack_value t1, t2;
988 if (ctx->stack_len < 2)
989 error (_("Not enough elements for "
990 "DW_OP_swap. Need 2, have %d."),
992 t1 = ctx->stack[ctx->stack_len - 1];
993 t2 = ctx->stack[ctx->stack_len - 2];
994 ctx->stack[ctx->stack_len - 1] = t2;
995 ctx->stack[ctx->stack_len - 2] = t1;
1000 result_val = dwarf_expr_fetch (ctx, 1);
1001 in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 1);
1006 struct dwarf_stack_value t1, t2, t3;
1008 if (ctx->stack_len < 3)
1009 error (_("Not enough elements for "
1010 "DW_OP_rot. Need 3, have %d."),
1012 t1 = ctx->stack[ctx->stack_len - 1];
1013 t2 = ctx->stack[ctx->stack_len - 2];
1014 t3 = ctx->stack[ctx->stack_len - 3];
1015 ctx->stack[ctx->stack_len - 1] = t2;
1016 ctx->stack[ctx->stack_len - 2] = t3;
1017 ctx->stack[ctx->stack_len - 3] = t1;
1022 case DW_OP_deref_size:
1023 case DW_OP_GNU_deref_type:
1025 int addr_size = (op == DW_OP_deref ? ctx->addr_size : *op_ptr++);
1026 gdb_byte *buf = (gdb_byte *) alloca (addr_size);
1027 CORE_ADDR addr = dwarf_expr_fetch_address (ctx, 0);
1030 dwarf_expr_pop (ctx);
1032 if (op == DW_OP_GNU_deref_type)
1036 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1037 type_die.cu_off = uoffset;
1038 type = dwarf_get_base_type (ctx, type_die, 0);
1041 type = address_type;
1043 (ctx->funcs->read_mem) (ctx->baton, buf, addr, addr_size);
1045 /* If the size of the object read from memory is different
1046 from the type length, we need to zero-extend it. */
1047 if (TYPE_LENGTH (type) != addr_size)
1050 extract_unsigned_integer (buf, addr_size, byte_order);
1052 buf = (gdb_byte *) alloca (TYPE_LENGTH (type));
1053 store_unsigned_integer (buf, TYPE_LENGTH (type),
1054 byte_order, result);
1057 result_val = value_from_contents_and_address (type, buf, addr);
1064 case DW_OP_plus_uconst:
1066 /* Unary operations. */
1067 result_val = dwarf_expr_fetch (ctx, 0);
1068 dwarf_expr_pop (ctx);
1073 if (value_less (result_val,
1074 value_zero (value_type (result_val), not_lval)))
1075 result_val = value_neg (result_val);
1078 result_val = value_neg (result_val);
1081 dwarf_require_integral (value_type (result_val));
1082 result_val = value_complement (result_val);
1084 case DW_OP_plus_uconst:
1085 dwarf_require_integral (value_type (result_val));
1086 result = value_as_long (result_val);
1087 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1089 result_val = value_from_ulongest (address_type, result);
1113 /* Binary operations. */
1114 struct value *first, *second;
1116 second = dwarf_expr_fetch (ctx, 0);
1117 dwarf_expr_pop (ctx);
1119 first = dwarf_expr_fetch (ctx, 0);
1120 dwarf_expr_pop (ctx);
1122 if (! base_types_equal_p (value_type (first), value_type (second)))
1123 error (_("Incompatible types on DWARF stack"));
1128 dwarf_require_integral (value_type (first));
1129 dwarf_require_integral (value_type (second));
1130 result_val = value_binop (first, second, BINOP_BITWISE_AND);
1133 result_val = value_binop (first, second, BINOP_DIV);
1136 result_val = value_binop (first, second, BINOP_SUB);
1141 struct type *orig_type = value_type (first);
1143 /* We have to special-case "old-style" untyped values
1144 -- these must have mod computed using unsigned
1146 if (orig_type == address_type)
1149 = get_unsigned_type (ctx->gdbarch, orig_type);
1152 first = value_cast (utype, first);
1153 second = value_cast (utype, second);
1155 /* Note that value_binop doesn't handle float or
1156 decimal float here. This seems unimportant. */
1157 result_val = value_binop (first, second, BINOP_MOD);
1159 result_val = value_cast (orig_type, result_val);
1163 result_val = value_binop (first, second, BINOP_MUL);
1166 dwarf_require_integral (value_type (first));
1167 dwarf_require_integral (value_type (second));
1168 result_val = value_binop (first, second, BINOP_BITWISE_IOR);
1171 result_val = value_binop (first, second, BINOP_ADD);
1174 dwarf_require_integral (value_type (first));
1175 dwarf_require_integral (value_type (second));
1176 result_val = value_binop (first, second, BINOP_LSH);
1179 dwarf_require_integral (value_type (first));
1180 dwarf_require_integral (value_type (second));
1181 if (!TYPE_UNSIGNED (value_type (first)))
1184 = get_unsigned_type (ctx->gdbarch, value_type (first));
1186 first = value_cast (utype, first);
1189 result_val = value_binop (first, second, BINOP_RSH);
1190 /* Make sure we wind up with the same type we started
1192 if (value_type (result_val) != value_type (second))
1193 result_val = value_cast (value_type (second), result_val);
1196 dwarf_require_integral (value_type (first));
1197 dwarf_require_integral (value_type (second));
1198 if (TYPE_UNSIGNED (value_type (first)))
1201 = get_signed_type (ctx->gdbarch, value_type (first));
1203 first = value_cast (stype, first);
1206 result_val = value_binop (first, second, BINOP_RSH);
1207 /* Make sure we wind up with the same type we started
1209 if (value_type (result_val) != value_type (second))
1210 result_val = value_cast (value_type (second), result_val);
1213 dwarf_require_integral (value_type (first));
1214 dwarf_require_integral (value_type (second));
1215 result_val = value_binop (first, second, BINOP_BITWISE_XOR);
1218 /* A <= B is !(B < A). */
1219 result = ! value_less (second, first);
1220 result_val = value_from_ulongest (address_type, result);
1223 /* A >= B is !(A < B). */
1224 result = ! value_less (first, second);
1225 result_val = value_from_ulongest (address_type, result);
1228 result = value_equal (first, second);
1229 result_val = value_from_ulongest (address_type, result);
1232 result = value_less (first, second);
1233 result_val = value_from_ulongest (address_type, result);
1236 /* A > B is B < A. */
1237 result = value_less (second, first);
1238 result_val = value_from_ulongest (address_type, result);
1241 result = ! value_equal (first, second);
1242 result_val = value_from_ulongest (address_type, result);
1245 internal_error (__FILE__, __LINE__,
1246 _("Can't be reached."));
1251 case DW_OP_call_frame_cfa:
1252 result = (ctx->funcs->get_frame_cfa) (ctx->baton);
1253 result_val = value_from_ulongest (address_type, result);
1254 in_stack_memory = 1;
1257 case DW_OP_GNU_push_tls_address:
1258 /* Variable is at a constant offset in the thread-local
1259 storage block into the objfile for the current thread and
1260 the dynamic linker module containing this expression. Here
1261 we return returns the offset from that base. The top of the
1262 stack has the offset from the beginning of the thread
1263 control block at which the variable is located. Nothing
1264 should follow this operator, so the top of stack would be
1266 result = value_as_long (dwarf_expr_fetch (ctx, 0));
1267 dwarf_expr_pop (ctx);
1268 result = (ctx->funcs->get_tls_address) (ctx->baton, result);
1269 result_val = value_from_ulongest (address_type, result);
1273 offset = extract_signed_integer (op_ptr, 2, byte_order);
1282 offset = extract_signed_integer (op_ptr, 2, byte_order);
1284 val = dwarf_expr_fetch (ctx, 0);
1285 dwarf_require_integral (value_type (val));
1286 if (value_as_long (val) != 0)
1288 dwarf_expr_pop (ctx);
1299 /* Record the piece. */
1300 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1301 add_piece (ctx, 8 * size, 0);
1303 /* Pop off the address/regnum, and reset the location
1305 if (ctx->location != DWARF_VALUE_LITERAL
1306 && ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
1307 dwarf_expr_pop (ctx);
1308 ctx->location = DWARF_VALUE_MEMORY;
1312 case DW_OP_bit_piece:
1314 uint64_t size, offset;
1316 /* Record the piece. */
1317 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1318 op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
1319 add_piece (ctx, size, offset);
1321 /* Pop off the address/regnum, and reset the location
1323 if (ctx->location != DWARF_VALUE_LITERAL
1324 && ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
1325 dwarf_expr_pop (ctx);
1326 ctx->location = DWARF_VALUE_MEMORY;
1330 case DW_OP_GNU_uninit:
1331 if (op_ptr != op_end)
1332 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
1333 "be the very last op."));
1335 ctx->initialized = 0;
1342 offset.cu_off = extract_unsigned_integer (op_ptr, 2, byte_order);
1344 ctx->funcs->dwarf_call (ctx, offset);
1352 offset.cu_off = extract_unsigned_integer (op_ptr, 4, byte_order);
1354 ctx->funcs->dwarf_call (ctx, offset);
1358 case DW_OP_GNU_entry_value:
1361 CORE_ADDR deref_size;
1362 union call_site_parameter_u kind_u;
1364 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
1365 if (op_ptr + len > op_end)
1366 error (_("DW_OP_GNU_entry_value: too few bytes available."));
1368 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len);
1369 if (kind_u.dwarf_reg != -1)
1372 ctx->funcs->push_dwarf_reg_entry_value (ctx,
1373 CALL_SITE_PARAMETER_DWARF_REG,
1375 -1 /* deref_size */);
1379 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr,
1382 if (kind_u.dwarf_reg != -1)
1384 if (deref_size == -1)
1385 deref_size = ctx->addr_size;
1387 ctx->funcs->push_dwarf_reg_entry_value (ctx,
1388 CALL_SITE_PARAMETER_DWARF_REG,
1389 kind_u, deref_size);
1393 error (_("DWARF-2 expression error: DW_OP_GNU_entry_value is "
1394 "supported only for single DW_OP_reg* "
1395 "or for DW_OP_breg*(0)+DW_OP_deref*"));
1398 case DW_OP_GNU_parameter_ref:
1400 union call_site_parameter_u kind_u;
1402 kind_u.param_offset.cu_off = extract_unsigned_integer (op_ptr, 4,
1405 ctx->funcs->push_dwarf_reg_entry_value (ctx,
1406 CALL_SITE_PARAMETER_PARAM_OFFSET,
1408 -1 /* deref_size */);
1412 case DW_OP_GNU_const_type:
1416 const gdb_byte *data;
1419 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1420 type_die.cu_off = uoffset;
1425 type = dwarf_get_base_type (ctx, type_die, n);
1426 result_val = value_from_contents (type, data);
1430 case DW_OP_GNU_regval_type:
1435 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1436 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1437 type_die.cu_off = uoffset;
1439 type = dwarf_get_base_type (ctx, type_die, 0);
1440 result_val = ctx->funcs->get_reg_value (ctx->baton, type, reg);
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));
1477 case DW_OP_push_object_address:
1478 /* Return the address of the object we are currently observing. */
1479 result = (ctx->funcs->get_object_address) (ctx->baton);
1480 result_val = value_from_ulongest (address_type, result);
1484 error (_("Unhandled dwarf expression opcode 0x%x"), op);
1487 /* Most things push a result value. */
1488 gdb_assert (result_val != NULL);
1489 dwarf_expr_push (ctx, result_val, in_stack_memory);
1494 /* To simplify our main caller, if the result is an implicit
1495 pointer, then make a pieced value. This is ok because we can't
1496 have implicit pointers in contexts where pieces are invalid. */
1497 if (ctx->location == DWARF_VALUE_IMPLICIT_POINTER)
1498 add_piece (ctx, 8 * ctx->addr_size, 0);
1501 ctx->recursion_depth--;
1502 gdb_assert (ctx->recursion_depth >= 0);
1505 /* Stub dwarf_expr_context_funcs.get_frame_base implementation. */
1508 ctx_no_get_frame_base (void *baton, const gdb_byte **start, size_t *length)
1510 error (_("%s is invalid in this context"), "DW_OP_fbreg");
1513 /* Stub dwarf_expr_context_funcs.get_frame_cfa implementation. */
1516 ctx_no_get_frame_cfa (void *baton)
1518 error (_("%s is invalid in this context"), "DW_OP_call_frame_cfa");
1521 /* Stub dwarf_expr_context_funcs.get_frame_pc implementation. */
1524 ctx_no_get_frame_pc (void *baton)
1526 error (_("%s is invalid in this context"), "DW_OP_GNU_implicit_pointer");
1529 /* Stub dwarf_expr_context_funcs.get_tls_address implementation. */
1532 ctx_no_get_tls_address (void *baton, CORE_ADDR offset)
1534 error (_("%s is invalid in this context"), "DW_OP_GNU_push_tls_address");
1537 /* Stub dwarf_expr_context_funcs.dwarf_call implementation. */
1540 ctx_no_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
1542 error (_("%s is invalid in this context"), "DW_OP_call*");
1545 /* Stub dwarf_expr_context_funcs.get_base_type implementation. */
1548 ctx_no_get_base_type (struct dwarf_expr_context *ctx, cu_offset die)
1550 error (_("Support for typed DWARF is not supported in this context"));
1553 /* Stub dwarf_expr_context_funcs.push_dwarf_block_entry_value
1557 ctx_no_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
1558 enum call_site_parameter_kind kind,
1559 union call_site_parameter_u kind_u,
1562 internal_error (__FILE__, __LINE__,
1563 _("Support for DW_OP_GNU_entry_value is unimplemented"));
1566 /* Stub dwarf_expr_context_funcs.get_addr_index implementation. */
1569 ctx_no_get_addr_index (void *baton, unsigned int index)
1571 error (_("%s is invalid in this context"), "DW_OP_GNU_addr_index");
1574 /* Provide a prototype to silence -Wmissing-prototypes. */
1575 extern initialize_file_ftype _initialize_dwarf2expr;
1578 _initialize_dwarf2expr (void)
1581 = gdbarch_data_register_post_init (dwarf_gdbarch_types_init);