1 /* DWARF 2 Expression Evaluator.
3 Copyright (C) 2001-2016 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 "dwarf2loc.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
69 = (struct dwarf_gdbarch_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 dwarf_expr_context::dwarf_expr_context ()
103 max_recursion_depth (0x100),
104 location (DWARF_VALUE_MEMORY),
111 this->stack = XNEWVEC (struct dwarf_stack_value, this->stack_allocated);
114 /* Clean up a dwarf_expr_context. */
116 dwarf_expr_context::~dwarf_expr_context ()
119 xfree (this->pieces);
122 /* Expand the memory allocated to CTX's stack to contain at least
123 NEED more elements than are currently used. */
126 dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need)
128 if (ctx->stack_len + need > ctx->stack_allocated)
130 size_t newlen = ctx->stack_len + need + 10;
132 ctx->stack = XRESIZEVEC (struct dwarf_stack_value, ctx->stack, newlen);
133 ctx->stack_allocated = newlen;
137 /* Push VALUE onto CTX's stack. */
140 dwarf_expr_push (struct dwarf_expr_context *ctx, struct value *value,
143 struct dwarf_stack_value *v;
145 dwarf_expr_grow_stack (ctx, 1);
146 v = &ctx->stack[ctx->stack_len++];
148 v->in_stack_memory = in_stack_memory;
151 /* Push VALUE onto CTX's stack. */
154 dwarf_expr_push_address (struct dwarf_expr_context *ctx, CORE_ADDR value,
157 dwarf_expr_push (ctx,
158 value_from_ulongest (dwarf_expr_address_type (ctx), value),
162 /* Pop the top item off of CTX's stack. */
165 dwarf_expr_pop (struct dwarf_expr_context *ctx)
167 if (ctx->stack_len <= 0)
168 error (_("dwarf expression stack underflow"));
172 /* Retrieve the N'th item on CTX's stack. */
175 dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n)
177 if (ctx->stack_len <= n)
178 error (_("Asked for position %d of stack, "
179 "stack only has %d elements on it."),
181 return ctx->stack[ctx->stack_len - (1 + n)].value;
184 /* Require that TYPE be an integral type; throw an exception if not. */
187 dwarf_require_integral (struct type *type)
189 if (TYPE_CODE (type) != TYPE_CODE_INT
190 && TYPE_CODE (type) != TYPE_CODE_CHAR
191 && TYPE_CODE (type) != TYPE_CODE_BOOL)
192 error (_("integral type expected in DWARF expression"));
195 /* Return the unsigned form of TYPE. TYPE is necessarily an integral
199 get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
201 switch (TYPE_LENGTH (type))
204 return builtin_type (gdbarch)->builtin_uint8;
206 return builtin_type (gdbarch)->builtin_uint16;
208 return builtin_type (gdbarch)->builtin_uint32;
210 return builtin_type (gdbarch)->builtin_uint64;
212 error (_("no unsigned variant found for type, while evaluating "
213 "DWARF expression"));
217 /* Return the signed form of TYPE. TYPE is necessarily an integral
221 get_signed_type (struct gdbarch *gdbarch, struct type *type)
223 switch (TYPE_LENGTH (type))
226 return builtin_type (gdbarch)->builtin_int8;
228 return builtin_type (gdbarch)->builtin_int16;
230 return builtin_type (gdbarch)->builtin_int32;
232 return builtin_type (gdbarch)->builtin_int64;
234 error (_("no signed variant found for type, while evaluating "
235 "DWARF expression"));
239 /* Retrieve the N'th item on CTX's stack, converted to an address. */
242 dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n)
244 struct value *result_val = dwarf_expr_fetch (ctx, n);
245 enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
248 dwarf_require_integral (value_type (result_val));
249 result = extract_unsigned_integer (value_contents (result_val),
250 TYPE_LENGTH (value_type (result_val)),
253 /* For most architectures, calling extract_unsigned_integer() alone
254 is sufficient for extracting an address. However, some
255 architectures (e.g. MIPS) use signed addresses and using
256 extract_unsigned_integer() will not produce a correct
257 result. Make sure we invoke gdbarch_integer_to_address()
258 for those architectures which require it. */
259 if (gdbarch_integer_to_address_p (ctx->gdbarch))
261 gdb_byte *buf = (gdb_byte *) alloca (ctx->addr_size);
262 struct type *int_type = get_unsigned_type (ctx->gdbarch,
263 value_type (result_val));
265 store_unsigned_integer (buf, ctx->addr_size, byte_order, result);
266 return gdbarch_integer_to_address (ctx->gdbarch, int_type, buf);
269 return (CORE_ADDR) result;
272 /* Retrieve the in_stack_memory flag of the N'th item on CTX's stack. */
275 dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n)
277 if (ctx->stack_len <= n)
278 error (_("Asked for position %d of stack, "
279 "stack only has %d elements on it."),
281 return ctx->stack[ctx->stack_len - (1 + n)].in_stack_memory;
284 /* Return true if the expression stack is empty. */
287 dwarf_expr_stack_empty_p (struct dwarf_expr_context *ctx)
289 return ctx->stack_len == 0;
292 /* Add a new piece to CTX's piece list. */
294 add_piece (struct dwarf_expr_context *ctx, ULONGEST size, ULONGEST offset)
296 struct dwarf_expr_piece *p;
301 = XRESIZEVEC (struct dwarf_expr_piece, ctx->pieces, ctx->num_pieces);
303 p = &ctx->pieces[ctx->num_pieces - 1];
304 p->location = ctx->location;
308 if (p->location == DWARF_VALUE_LITERAL)
310 p->v.literal.data = ctx->data;
311 p->v.literal.length = ctx->len;
313 else if (dwarf_expr_stack_empty_p (ctx))
315 p->location = DWARF_VALUE_OPTIMIZED_OUT;
316 /* Also reset the context's location, for our callers. This is
317 a somewhat strange approach, but this lets us avoid setting
318 the location to DWARF_VALUE_MEMORY in all the individual
319 cases in the evaluator. */
320 ctx->location = DWARF_VALUE_OPTIMIZED_OUT;
322 else if (p->location == DWARF_VALUE_MEMORY)
324 p->v.mem.addr = dwarf_expr_fetch_address (ctx, 0);
325 p->v.mem.in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
327 else if (p->location == DWARF_VALUE_IMPLICIT_POINTER)
329 p->v.ptr.die.sect_off = ctx->len;
330 p->v.ptr.offset = value_as_long (dwarf_expr_fetch (ctx, 0));
332 else if (p->location == DWARF_VALUE_REGISTER)
333 p->v.regno = value_as_long (dwarf_expr_fetch (ctx, 0));
336 p->v.value = dwarf_expr_fetch (ctx, 0);
340 /* Evaluate the expression at ADDR (LEN bytes long) using the context
344 dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr,
347 int old_recursion_depth = ctx->recursion_depth;
349 execute_stack_op (ctx, addr, addr + len);
351 /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here. */
353 gdb_assert (ctx->recursion_depth == old_recursion_depth);
356 /* Helper to read a uleb128 value or throw an error. */
359 safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
362 buf = gdb_read_uleb128 (buf, buf_end, r);
364 error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
368 /* Helper to read a sleb128 value or throw an error. */
371 safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
374 buf = gdb_read_sleb128 (buf, buf_end, r);
376 error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
381 safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
383 buf = gdb_skip_leb128 (buf, buf_end);
385 error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
390 /* Check that the current operator is either at the end of an
391 expression, or that it is followed by a composition operator or by
392 DW_OP_GNU_uninit (which should terminate the expression). */
395 dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
398 if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece
399 && *op_ptr != DW_OP_GNU_uninit)
400 error (_("DWARF-2 expression error: `%s' operations must be "
401 "used either alone or in conjunction with DW_OP_piece "
402 "or DW_OP_bit_piece."),
406 /* Return true iff the types T1 and T2 are "the same". This only does
407 checks that might reasonably be needed to compare DWARF base
411 base_types_equal_p (struct type *t1, struct type *t2)
413 if (TYPE_CODE (t1) != TYPE_CODE (t2))
415 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
417 return TYPE_LENGTH (t1) == TYPE_LENGTH (t2);
420 /* A convenience function to call get_base_type on CTX and return the
421 result. DIE is the DIE whose type we need. SIZE is non-zero if
422 this function should verify that the resulting type has the correct
426 dwarf_get_base_type (struct dwarf_expr_context *ctx, cu_offset die, int size)
430 if (ctx->funcs->get_base_type)
432 result = ctx->funcs->get_base_type (ctx, die);
434 error (_("Could not find type for DW_OP_GNU_const_type"));
435 if (size != 0 && TYPE_LENGTH (result) != size)
436 error (_("DW_OP_GNU_const_type has different sizes for type and data"));
439 /* Anything will do. */
440 result = builtin_type (ctx->gdbarch)->builtin_int;
445 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
446 DWARF register number. Otherwise return -1. */
449 dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end)
455 if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
457 if (buf_end - buf != 1)
459 return *buf - DW_OP_reg0;
462 if (*buf == DW_OP_GNU_regval_type)
465 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
468 buf = gdb_skip_leb128 (buf, buf_end);
472 else if (*buf == DW_OP_regx)
475 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
481 if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
486 /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
487 DW_OP_deref* return the DWARF register number. Otherwise return -1.
488 DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
489 size from DW_OP_deref_size. */
492 dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end,
493 CORE_ADDR *deref_size_return)
501 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
503 dwarf_reg = *buf - DW_OP_breg0;
508 else if (*buf == DW_OP_bregx)
511 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
514 if ((int) dwarf_reg != dwarf_reg)
520 buf = gdb_read_sleb128 (buf, buf_end, &offset);
526 if (*buf == DW_OP_deref)
529 *deref_size_return = -1;
531 else if (*buf == DW_OP_deref_size)
536 *deref_size_return = *buf++;
547 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
548 in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */
551 dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
552 CORE_ADDR *fb_offset_return)
559 if (*buf != DW_OP_fbreg)
563 buf = gdb_read_sleb128 (buf, buf_end, &fb_offset);
566 *fb_offset_return = fb_offset;
567 if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
573 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
574 in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0.
575 The matched SP register number depends on GDBARCH. */
578 dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
579 const gdb_byte *buf_end, CORE_ADDR *sp_offset_return)
586 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
588 dwarf_reg = *buf - DW_OP_breg0;
593 if (*buf != DW_OP_bregx)
596 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
601 if (dwarf_reg_to_regnum (gdbarch, dwarf_reg)
602 != gdbarch_sp_regnum (gdbarch))
605 buf = gdb_read_sleb128 (buf, buf_end, &sp_offset);
608 *sp_offset_return = sp_offset;
609 if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
615 /* The engine for the expression evaluator. Using the context in CTX,
616 evaluate the expression between OP_PTR and OP_END. */
619 execute_stack_op (struct dwarf_expr_context *ctx,
620 const gdb_byte *op_ptr, const gdb_byte *op_end)
622 enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
623 /* Old-style "untyped" DWARF values need special treatment in a
624 couple of places, specifically DW_OP_mod and DW_OP_shr. We need
625 a special type for these values so we can distinguish them from
626 values that have an explicit type, because explicitly-typed
627 values do not need special treatment. This special type must be
628 different (in the `==' sense) from any base type coming from the
630 struct type *address_type = dwarf_expr_address_type (ctx);
632 ctx->location = DWARF_VALUE_MEMORY;
633 ctx->initialized = 1; /* Default is initialized. */
635 if (ctx->recursion_depth > ctx->max_recursion_depth)
636 error (_("DWARF-2 expression error: Loop detected (%d)."),
637 ctx->recursion_depth);
638 ctx->recursion_depth++;
640 while (op_ptr < op_end)
642 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr++;
644 /* Assume the value is not in stack memory.
645 Code that knows otherwise sets this to 1.
646 Some arithmetic on stack addresses can probably be assumed to still
647 be a stack address, but we skip this complication for now.
648 This is just an optimization, so it's always ok to punt
649 and leave this as 0. */
650 int in_stack_memory = 0;
651 uint64_t uoffset, reg;
653 struct value *result_val = NULL;
655 /* The DWARF expression might have a bug causing an infinite
656 loop. In that case, quitting is the only way out. */
693 result = op - DW_OP_lit0;
694 result_val = value_from_ulongest (address_type, result);
698 result = extract_unsigned_integer (op_ptr,
699 ctx->addr_size, byte_order);
700 op_ptr += ctx->addr_size;
701 /* Some versions of GCC emit DW_OP_addr before
702 DW_OP_GNU_push_tls_address. In this case the value is an
703 index, not an address. We don't support things like
704 branching between the address and the TLS op. */
705 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
706 result += ctx->offset;
707 result_val = value_from_ulongest (address_type, result);
710 case DW_OP_GNU_addr_index:
711 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
712 result = (ctx->funcs->get_addr_index) (ctx->baton, uoffset);
713 result += ctx->offset;
714 result_val = value_from_ulongest (address_type, result);
716 case DW_OP_GNU_const_index:
717 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
718 result = (ctx->funcs->get_addr_index) (ctx->baton, uoffset);
719 result_val = value_from_ulongest (address_type, result);
723 result = extract_unsigned_integer (op_ptr, 1, byte_order);
724 result_val = value_from_ulongest (address_type, result);
728 result = extract_signed_integer (op_ptr, 1, byte_order);
729 result_val = value_from_ulongest (address_type, result);
733 result = extract_unsigned_integer (op_ptr, 2, byte_order);
734 result_val = value_from_ulongest (address_type, result);
738 result = extract_signed_integer (op_ptr, 2, byte_order);
739 result_val = value_from_ulongest (address_type, result);
743 result = extract_unsigned_integer (op_ptr, 4, byte_order);
744 result_val = value_from_ulongest (address_type, result);
748 result = extract_signed_integer (op_ptr, 4, byte_order);
749 result_val = value_from_ulongest (address_type, result);
753 result = extract_unsigned_integer (op_ptr, 8, byte_order);
754 result_val = value_from_ulongest (address_type, result);
758 result = extract_signed_integer (op_ptr, 8, byte_order);
759 result_val = value_from_ulongest (address_type, result);
763 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
765 result_val = value_from_ulongest (address_type, result);
768 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
770 result_val = value_from_ulongest (address_type, result);
773 /* The DW_OP_reg operations are required to occur alone in
774 location expressions. */
807 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_reg");
809 result = op - DW_OP_reg0;
810 result_val = value_from_ulongest (address_type, result);
811 ctx->location = DWARF_VALUE_REGISTER;
815 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
816 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
819 result_val = value_from_ulongest (address_type, result);
820 ctx->location = DWARF_VALUE_REGISTER;
823 case DW_OP_implicit_value:
827 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
828 if (op_ptr + len > op_end)
829 error (_("DW_OP_implicit_value: too few bytes available."));
832 ctx->location = DWARF_VALUE_LITERAL;
834 dwarf_expr_require_composition (op_ptr, op_end,
835 "DW_OP_implicit_value");
839 case DW_OP_stack_value:
840 ctx->location = DWARF_VALUE_STACK;
841 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
844 case DW_OP_GNU_implicit_pointer:
848 if (ctx->ref_addr_size == -1)
849 error (_("DWARF-2 expression error: DW_OP_GNU_implicit_pointer "
850 "is not allowed in frame context"));
852 /* The referred-to DIE of sect_offset kind. */
853 ctx->len = extract_unsigned_integer (op_ptr, ctx->ref_addr_size,
855 op_ptr += ctx->ref_addr_size;
857 /* The byte offset into the data. */
858 op_ptr = safe_read_sleb128 (op_ptr, op_end, &len);
859 result = (ULONGEST) len;
860 result_val = value_from_ulongest (address_type, result);
862 ctx->location = DWARF_VALUE_IMPLICIT_POINTER;
863 dwarf_expr_require_composition (op_ptr, op_end,
864 "DW_OP_GNU_implicit_pointer");
901 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
902 result = (ctx->funcs->read_addr_from_reg) (ctx->baton,
905 result_val = value_from_ulongest (address_type, result);
910 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
911 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
912 result = (ctx->funcs->read_addr_from_reg) (ctx->baton, reg);
914 result_val = value_from_ulongest (address_type, result);
919 const gdb_byte *datastart;
921 unsigned int before_stack_len;
923 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
924 /* Rather than create a whole new context, we simply
925 record the stack length before execution, then reset it
926 afterwards, effectively erasing whatever the recursive
928 before_stack_len = ctx->stack_len;
929 /* FIXME: cagney/2003-03-26: This code should be using
930 get_frame_base_address(), and then implement a dwarf2
931 specific this_base method. */
932 (ctx->funcs->get_frame_base) (ctx->baton, &datastart, &datalen);
933 dwarf_expr_eval (ctx, datastart, datalen);
934 if (ctx->location == DWARF_VALUE_MEMORY)
935 result = dwarf_expr_fetch_address (ctx, 0);
936 else if (ctx->location == DWARF_VALUE_REGISTER)
937 result = (ctx->funcs->read_addr_from_reg)
939 value_as_long (dwarf_expr_fetch (ctx, 0)));
941 error (_("Not implemented: computing frame "
942 "base using explicit value operator"));
943 result = result + offset;
944 result_val = value_from_ulongest (address_type, result);
946 ctx->stack_len = before_stack_len;
947 ctx->location = DWARF_VALUE_MEMORY;
952 result_val = dwarf_expr_fetch (ctx, 0);
953 in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
957 dwarf_expr_pop (ctx);
962 result_val = dwarf_expr_fetch (ctx, offset);
963 in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, offset);
968 struct dwarf_stack_value t1, t2;
970 if (ctx->stack_len < 2)
971 error (_("Not enough elements for "
972 "DW_OP_swap. Need 2, have %d."),
974 t1 = ctx->stack[ctx->stack_len - 1];
975 t2 = ctx->stack[ctx->stack_len - 2];
976 ctx->stack[ctx->stack_len - 1] = t2;
977 ctx->stack[ctx->stack_len - 2] = t1;
982 result_val = dwarf_expr_fetch (ctx, 1);
983 in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 1);
988 struct dwarf_stack_value t1, t2, t3;
990 if (ctx->stack_len < 3)
991 error (_("Not enough elements for "
992 "DW_OP_rot. Need 3, have %d."),
994 t1 = ctx->stack[ctx->stack_len - 1];
995 t2 = ctx->stack[ctx->stack_len - 2];
996 t3 = ctx->stack[ctx->stack_len - 3];
997 ctx->stack[ctx->stack_len - 1] = t2;
998 ctx->stack[ctx->stack_len - 2] = t3;
999 ctx->stack[ctx->stack_len - 3] = t1;
1004 case DW_OP_deref_size:
1005 case DW_OP_GNU_deref_type:
1007 int addr_size = (op == DW_OP_deref ? ctx->addr_size : *op_ptr++);
1008 gdb_byte *buf = (gdb_byte *) alloca (addr_size);
1009 CORE_ADDR addr = dwarf_expr_fetch_address (ctx, 0);
1012 dwarf_expr_pop (ctx);
1014 if (op == DW_OP_GNU_deref_type)
1018 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1019 type_die.cu_off = uoffset;
1020 type = dwarf_get_base_type (ctx, type_die, 0);
1023 type = address_type;
1025 (ctx->funcs->read_mem) (ctx->baton, buf, addr, addr_size);
1027 /* If the size of the object read from memory is different
1028 from the type length, we need to zero-extend it. */
1029 if (TYPE_LENGTH (type) != addr_size)
1032 extract_unsigned_integer (buf, addr_size, byte_order);
1034 buf = (gdb_byte *) alloca (TYPE_LENGTH (type));
1035 store_unsigned_integer (buf, TYPE_LENGTH (type),
1036 byte_order, result);
1039 result_val = value_from_contents_and_address (type, buf, addr);
1046 case DW_OP_plus_uconst:
1048 /* Unary operations. */
1049 result_val = dwarf_expr_fetch (ctx, 0);
1050 dwarf_expr_pop (ctx);
1055 if (value_less (result_val,
1056 value_zero (value_type (result_val), not_lval)))
1057 result_val = value_neg (result_val);
1060 result_val = value_neg (result_val);
1063 dwarf_require_integral (value_type (result_val));
1064 result_val = value_complement (result_val);
1066 case DW_OP_plus_uconst:
1067 dwarf_require_integral (value_type (result_val));
1068 result = value_as_long (result_val);
1069 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1071 result_val = value_from_ulongest (address_type, result);
1095 /* Binary operations. */
1096 struct value *first, *second;
1098 second = dwarf_expr_fetch (ctx, 0);
1099 dwarf_expr_pop (ctx);
1101 first = dwarf_expr_fetch (ctx, 0);
1102 dwarf_expr_pop (ctx);
1104 if (! base_types_equal_p (value_type (first), value_type (second)))
1105 error (_("Incompatible types on DWARF stack"));
1110 dwarf_require_integral (value_type (first));
1111 dwarf_require_integral (value_type (second));
1112 result_val = value_binop (first, second, BINOP_BITWISE_AND);
1115 result_val = value_binop (first, second, BINOP_DIV);
1118 result_val = value_binop (first, second, BINOP_SUB);
1123 struct type *orig_type = value_type (first);
1125 /* We have to special-case "old-style" untyped values
1126 -- these must have mod computed using unsigned
1128 if (orig_type == address_type)
1131 = get_unsigned_type (ctx->gdbarch, orig_type);
1134 first = value_cast (utype, first);
1135 second = value_cast (utype, second);
1137 /* Note that value_binop doesn't handle float or
1138 decimal float here. This seems unimportant. */
1139 result_val = value_binop (first, second, BINOP_MOD);
1141 result_val = value_cast (orig_type, result_val);
1145 result_val = value_binop (first, second, BINOP_MUL);
1148 dwarf_require_integral (value_type (first));
1149 dwarf_require_integral (value_type (second));
1150 result_val = value_binop (first, second, BINOP_BITWISE_IOR);
1153 result_val = value_binop (first, second, BINOP_ADD);
1156 dwarf_require_integral (value_type (first));
1157 dwarf_require_integral (value_type (second));
1158 result_val = value_binop (first, second, BINOP_LSH);
1161 dwarf_require_integral (value_type (first));
1162 dwarf_require_integral (value_type (second));
1163 if (!TYPE_UNSIGNED (value_type (first)))
1166 = get_unsigned_type (ctx->gdbarch, value_type (first));
1168 first = value_cast (utype, first);
1171 result_val = value_binop (first, second, BINOP_RSH);
1172 /* Make sure we wind up with the same type we started
1174 if (value_type (result_val) != value_type (second))
1175 result_val = value_cast (value_type (second), result_val);
1178 dwarf_require_integral (value_type (first));
1179 dwarf_require_integral (value_type (second));
1180 if (TYPE_UNSIGNED (value_type (first)))
1183 = get_signed_type (ctx->gdbarch, value_type (first));
1185 first = value_cast (stype, first);
1188 result_val = value_binop (first, second, BINOP_RSH);
1189 /* Make sure we wind up with the same type we started
1191 if (value_type (result_val) != value_type (second))
1192 result_val = value_cast (value_type (second), result_val);
1195 dwarf_require_integral (value_type (first));
1196 dwarf_require_integral (value_type (second));
1197 result_val = value_binop (first, second, BINOP_BITWISE_XOR);
1200 /* A <= B is !(B < A). */
1201 result = ! value_less (second, first);
1202 result_val = value_from_ulongest (address_type, result);
1205 /* A >= B is !(A < B). */
1206 result = ! value_less (first, second);
1207 result_val = value_from_ulongest (address_type, result);
1210 result = value_equal (first, second);
1211 result_val = value_from_ulongest (address_type, result);
1214 result = value_less (first, second);
1215 result_val = value_from_ulongest (address_type, result);
1218 /* A > B is B < A. */
1219 result = value_less (second, first);
1220 result_val = value_from_ulongest (address_type, result);
1223 result = ! value_equal (first, second);
1224 result_val = value_from_ulongest (address_type, result);
1227 internal_error (__FILE__, __LINE__,
1228 _("Can't be reached."));
1233 case DW_OP_call_frame_cfa:
1234 result = (ctx->funcs->get_frame_cfa) (ctx->baton);
1235 result_val = value_from_ulongest (address_type, result);
1236 in_stack_memory = 1;
1239 case DW_OP_GNU_push_tls_address:
1240 case DW_OP_form_tls_address:
1241 /* Variable is at a constant offset in the thread-local
1242 storage block into the objfile for the current thread and
1243 the dynamic linker module containing this expression. Here
1244 we return returns the offset from that base. The top of the
1245 stack has the offset from the beginning of the thread
1246 control block at which the variable is located. Nothing
1247 should follow this operator, so the top of stack would be
1249 result = value_as_long (dwarf_expr_fetch (ctx, 0));
1250 dwarf_expr_pop (ctx);
1251 result = (ctx->funcs->get_tls_address) (ctx->baton, result);
1252 result_val = value_from_ulongest (address_type, result);
1256 offset = extract_signed_integer (op_ptr, 2, byte_order);
1265 offset = extract_signed_integer (op_ptr, 2, byte_order);
1267 val = dwarf_expr_fetch (ctx, 0);
1268 dwarf_require_integral (value_type (val));
1269 if (value_as_long (val) != 0)
1271 dwarf_expr_pop (ctx);
1282 /* Record the piece. */
1283 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1284 add_piece (ctx, 8 * size, 0);
1286 /* Pop off the address/regnum, and reset the location
1288 if (ctx->location != DWARF_VALUE_LITERAL
1289 && ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
1290 dwarf_expr_pop (ctx);
1291 ctx->location = DWARF_VALUE_MEMORY;
1295 case DW_OP_bit_piece:
1297 uint64_t size, offset;
1299 /* Record the piece. */
1300 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1301 op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
1302 add_piece (ctx, size, offset);
1304 /* Pop off the address/regnum, and reset the location
1306 if (ctx->location != DWARF_VALUE_LITERAL
1307 && ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
1308 dwarf_expr_pop (ctx);
1309 ctx->location = DWARF_VALUE_MEMORY;
1313 case DW_OP_GNU_uninit:
1314 if (op_ptr != op_end)
1315 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
1316 "be the very last op."));
1318 ctx->initialized = 0;
1325 offset.cu_off = extract_unsigned_integer (op_ptr, 2, byte_order);
1327 ctx->funcs->dwarf_call (ctx, offset);
1335 offset.cu_off = extract_unsigned_integer (op_ptr, 4, byte_order);
1337 ctx->funcs->dwarf_call (ctx, offset);
1341 case DW_OP_GNU_entry_value:
1344 CORE_ADDR deref_size;
1345 union call_site_parameter_u kind_u;
1347 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
1348 if (op_ptr + len > op_end)
1349 error (_("DW_OP_GNU_entry_value: too few bytes available."));
1351 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len);
1352 if (kind_u.dwarf_reg != -1)
1355 ctx->funcs->push_dwarf_reg_entry_value (ctx,
1356 CALL_SITE_PARAMETER_DWARF_REG,
1358 -1 /* deref_size */);
1362 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr,
1365 if (kind_u.dwarf_reg != -1)
1367 if (deref_size == -1)
1368 deref_size = ctx->addr_size;
1370 ctx->funcs->push_dwarf_reg_entry_value (ctx,
1371 CALL_SITE_PARAMETER_DWARF_REG,
1372 kind_u, deref_size);
1376 error (_("DWARF-2 expression error: DW_OP_GNU_entry_value is "
1377 "supported only for single DW_OP_reg* "
1378 "or for DW_OP_breg*(0)+DW_OP_deref*"));
1381 case DW_OP_GNU_parameter_ref:
1383 union call_site_parameter_u kind_u;
1385 kind_u.param_offset.cu_off = extract_unsigned_integer (op_ptr, 4,
1388 ctx->funcs->push_dwarf_reg_entry_value (ctx,
1389 CALL_SITE_PARAMETER_PARAM_OFFSET,
1391 -1 /* deref_size */);
1395 case DW_OP_GNU_const_type:
1399 const gdb_byte *data;
1402 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1403 type_die.cu_off = uoffset;
1408 type = dwarf_get_base_type (ctx, type_die, n);
1409 result_val = value_from_contents (type, data);
1413 case DW_OP_GNU_regval_type:
1418 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1419 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1420 type_die.cu_off = uoffset;
1422 type = dwarf_get_base_type (ctx, type_die, 0);
1423 result_val = ctx->funcs->get_reg_value (ctx->baton, type, reg);
1427 case DW_OP_GNU_convert:
1428 case DW_OP_GNU_reinterpret:
1433 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1434 type_die.cu_off = uoffset;
1436 if (type_die.cu_off == 0)
1437 type = address_type;
1439 type = dwarf_get_base_type (ctx, type_die, 0);
1441 result_val = dwarf_expr_fetch (ctx, 0);
1442 dwarf_expr_pop (ctx);
1444 if (op == DW_OP_GNU_convert)
1445 result_val = value_cast (type, result_val);
1446 else if (type == value_type (result_val))
1450 else if (TYPE_LENGTH (type)
1451 != TYPE_LENGTH (value_type (result_val)))
1452 error (_("DW_OP_GNU_reinterpret has wrong size"));
1455 = value_from_contents (type,
1456 value_contents_all (result_val));
1460 case DW_OP_push_object_address:
1461 /* Return the address of the object we are currently observing. */
1462 result = (ctx->funcs->get_object_address) (ctx->baton);
1463 result_val = value_from_ulongest (address_type, result);
1467 error (_("Unhandled dwarf expression opcode 0x%x"), op);
1470 /* Most things push a result value. */
1471 gdb_assert (result_val != NULL);
1472 dwarf_expr_push (ctx, result_val, in_stack_memory);
1477 /* To simplify our main caller, if the result is an implicit
1478 pointer, then make a pieced value. This is ok because we can't
1479 have implicit pointers in contexts where pieces are invalid. */
1480 if (ctx->location == DWARF_VALUE_IMPLICIT_POINTER)
1481 add_piece (ctx, 8 * ctx->addr_size, 0);
1484 ctx->recursion_depth--;
1485 gdb_assert (ctx->recursion_depth >= 0);
1488 /* Stub dwarf_expr_context_funcs.get_frame_base implementation. */
1491 ctx_no_get_frame_base (void *baton, const gdb_byte **start, size_t *length)
1493 error (_("%s is invalid in this context"), "DW_OP_fbreg");
1496 /* Stub dwarf_expr_context_funcs.get_frame_cfa implementation. */
1499 ctx_no_get_frame_cfa (void *baton)
1501 error (_("%s is invalid in this context"), "DW_OP_call_frame_cfa");
1504 /* Stub dwarf_expr_context_funcs.get_frame_pc implementation. */
1507 ctx_no_get_frame_pc (void *baton)
1509 error (_("%s is invalid in this context"), "DW_OP_GNU_implicit_pointer");
1512 /* Stub dwarf_expr_context_funcs.get_tls_address implementation. */
1515 ctx_no_get_tls_address (void *baton, CORE_ADDR offset)
1517 error (_("%s is invalid in this context"), "DW_OP_form_tls_address");
1520 /* Stub dwarf_expr_context_funcs.dwarf_call implementation. */
1523 ctx_no_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
1525 error (_("%s is invalid in this context"), "DW_OP_call*");
1528 /* Stub dwarf_expr_context_funcs.get_base_type implementation. */
1531 ctx_no_get_base_type (struct dwarf_expr_context *ctx, cu_offset die)
1533 error (_("Support for typed DWARF is not supported in this context"));
1536 /* Stub dwarf_expr_context_funcs.push_dwarf_block_entry_value
1540 ctx_no_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
1541 enum call_site_parameter_kind kind,
1542 union call_site_parameter_u kind_u,
1545 internal_error (__FILE__, __LINE__,
1546 _("Support for DW_OP_GNU_entry_value is unimplemented"));
1549 /* Stub dwarf_expr_context_funcs.get_addr_index implementation. */
1552 ctx_no_get_addr_index (void *baton, unsigned int index)
1554 error (_("%s is invalid in this context"), "DW_OP_GNU_addr_index");
1557 /* Provide a prototype to silence -Wmissing-prototypes. */
1558 extern initialize_file_ftype _initialize_dwarf2expr;
1561 _initialize_dwarf2expr (void)
1564 = gdbarch_data_register_post_init (dwarf_gdbarch_types_init);