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 /* Cookie for gdbarch data. */
33 static struct gdbarch_data *dwarf_arch_cookie;
35 /* This holds gdbarch-specific types used by the DWARF expression
36 evaluator. See comments in execute_stack_op. */
38 struct dwarf_gdbarch_types
40 struct type *dw_types[3];
43 /* Allocate and fill in dwarf_gdbarch_types for an arch. */
46 dwarf_gdbarch_types_init (struct gdbarch *gdbarch)
48 struct dwarf_gdbarch_types *types
49 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct dwarf_gdbarch_types);
51 /* The types themselves are lazily initialized. */
56 /* Return the type used for DWARF operations where the type is
57 unspecified in the DWARF spec. Only certain sizes are
61 dwarf_expr_context::address_type () const
63 struct dwarf_gdbarch_types *types
64 = (struct dwarf_gdbarch_types *) gdbarch_data (this->gdbarch,
68 if (this->addr_size == 2)
70 else if (this->addr_size == 4)
72 else if (this->addr_size == 8)
75 error (_("Unsupported address size in DWARF expressions: %d bits"),
78 if (types->dw_types[ndx] == NULL)
80 = arch_integer_type (this->gdbarch,
82 0, "<signed DWARF address type>");
84 return types->dw_types[ndx];
87 /* Create a new context for the expression evaluator. */
89 dwarf_expr_context::dwarf_expr_context ()
98 max_recursion_depth (0x100),
99 location (DWARF_VALUE_MEMORY),
106 this->stack = XNEWVEC (struct dwarf_stack_value, this->stack_allocated);
109 /* Clean up a dwarf_expr_context. */
111 dwarf_expr_context::~dwarf_expr_context ()
114 xfree (this->pieces);
117 /* Expand the memory allocated stack to contain at least
118 NEED more elements than are currently used. */
121 dwarf_expr_context::grow_stack (size_t need)
123 if (this->stack_len + need > this->stack_allocated)
125 size_t newlen = this->stack_len + need + 10;
127 this->stack = XRESIZEVEC (struct dwarf_stack_value, this->stack, newlen);
128 this->stack_allocated = newlen;
132 /* Push VALUE onto the stack. */
135 dwarf_expr_context::push (struct value *value, int in_stack_memory)
137 struct dwarf_stack_value *v;
140 v = &this->stack[this->stack_len++];
142 v->in_stack_memory = in_stack_memory;
145 /* Push VALUE onto the stack. */
148 dwarf_expr_context::push_address (CORE_ADDR value, int in_stack_memory)
150 push (value_from_ulongest (address_type (), value), in_stack_memory);
153 /* Pop the top item off of the stack. */
156 dwarf_expr_context::pop ()
158 if (this->stack_len <= 0)
159 error (_("dwarf expression stack underflow"));
163 /* Retrieve the N'th item on the stack. */
166 dwarf_expr_context::fetch (int n)
168 if (this->stack_len <= n)
169 error (_("Asked for position %d of stack, "
170 "stack only has %d elements on it."),
172 return this->stack[this->stack_len - (1 + n)].value;
175 /* Require that TYPE be an integral type; throw an exception if not. */
178 dwarf_require_integral (struct type *type)
180 if (TYPE_CODE (type) != TYPE_CODE_INT
181 && TYPE_CODE (type) != TYPE_CODE_CHAR
182 && TYPE_CODE (type) != TYPE_CODE_BOOL)
183 error (_("integral type expected in DWARF expression"));
186 /* Return the unsigned form of TYPE. TYPE is necessarily an integral
190 get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
192 switch (TYPE_LENGTH (type))
195 return builtin_type (gdbarch)->builtin_uint8;
197 return builtin_type (gdbarch)->builtin_uint16;
199 return builtin_type (gdbarch)->builtin_uint32;
201 return builtin_type (gdbarch)->builtin_uint64;
203 error (_("no unsigned variant found for type, while evaluating "
204 "DWARF expression"));
208 /* Return the signed form of TYPE. TYPE is necessarily an integral
212 get_signed_type (struct gdbarch *gdbarch, struct type *type)
214 switch (TYPE_LENGTH (type))
217 return builtin_type (gdbarch)->builtin_int8;
219 return builtin_type (gdbarch)->builtin_int16;
221 return builtin_type (gdbarch)->builtin_int32;
223 return builtin_type (gdbarch)->builtin_int64;
225 error (_("no signed variant found for type, while evaluating "
226 "DWARF expression"));
230 /* Retrieve the N'th item on the stack, converted to an address. */
233 dwarf_expr_context::fetch_address (int n)
235 struct value *result_val = fetch (n);
236 enum bfd_endian byte_order = gdbarch_byte_order (this->gdbarch);
239 dwarf_require_integral (value_type (result_val));
240 result = extract_unsigned_integer (value_contents (result_val),
241 TYPE_LENGTH (value_type (result_val)),
244 /* For most architectures, calling extract_unsigned_integer() alone
245 is sufficient for extracting an address. However, some
246 architectures (e.g. MIPS) use signed addresses and using
247 extract_unsigned_integer() will not produce a correct
248 result. Make sure we invoke gdbarch_integer_to_address()
249 for those architectures which require it. */
250 if (gdbarch_integer_to_address_p (this->gdbarch))
252 gdb_byte *buf = (gdb_byte *) alloca (this->addr_size);
253 struct type *int_type = get_unsigned_type (this->gdbarch,
254 value_type (result_val));
256 store_unsigned_integer (buf, this->addr_size, byte_order, result);
257 return gdbarch_integer_to_address (this->gdbarch, int_type, buf);
260 return (CORE_ADDR) result;
263 /* Retrieve the in_stack_memory flag of the N'th item on the stack. */
266 dwarf_expr_context::fetch_in_stack_memory (int n)
268 if (this->stack_len <= n)
269 error (_("Asked for position %d of stack, "
270 "stack only has %d elements on it."),
272 return this->stack[this->stack_len - (1 + n)].in_stack_memory;
275 /* Return true if the expression stack is empty. */
278 dwarf_expr_context::stack_empty_p () const
280 return this->stack_len == 0;
283 /* Add a new piece to the dwarf_expr_context's piece list. */
285 dwarf_expr_context::add_piece (ULONGEST size, ULONGEST offset)
287 struct dwarf_expr_piece *p;
292 = XRESIZEVEC (struct dwarf_expr_piece, this->pieces, this->num_pieces);
294 p = &this->pieces[this->num_pieces - 1];
295 p->location = this->location;
299 if (p->location == DWARF_VALUE_LITERAL)
301 p->v.literal.data = this->data;
302 p->v.literal.length = this->len;
304 else if (stack_empty_p ())
306 p->location = DWARF_VALUE_OPTIMIZED_OUT;
307 /* Also reset the context's location, for our callers. This is
308 a somewhat strange approach, but this lets us avoid setting
309 the location to DWARF_VALUE_MEMORY in all the individual
310 cases in the evaluator. */
311 this->location = DWARF_VALUE_OPTIMIZED_OUT;
313 else if (p->location == DWARF_VALUE_MEMORY)
315 p->v.mem.addr = fetch_address (0);
316 p->v.mem.in_stack_memory = fetch_in_stack_memory (0);
318 else if (p->location == DWARF_VALUE_IMPLICIT_POINTER)
320 p->v.ptr.die.sect_off = this->len;
321 p->v.ptr.offset = value_as_long (fetch (0));
323 else if (p->location == DWARF_VALUE_REGISTER)
324 p->v.regno = value_as_long (fetch (0));
327 p->v.value = fetch (0);
331 /* Evaluate the expression at ADDR (LEN bytes long). */
334 dwarf_expr_context::eval (const gdb_byte *addr, size_t len)
336 int old_recursion_depth = this->recursion_depth;
338 execute_stack_op (addr, addr + len);
340 /* RECURSION_DEPTH becomes invalid if an exception was thrown here. */
342 gdb_assert (this->recursion_depth == old_recursion_depth);
345 /* Helper to read a uleb128 value or throw an error. */
348 safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
351 buf = gdb_read_uleb128 (buf, buf_end, r);
353 error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
357 /* Helper to read a sleb128 value or throw an error. */
360 safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
363 buf = gdb_read_sleb128 (buf, buf_end, r);
365 error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
370 safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
372 buf = gdb_skip_leb128 (buf, buf_end);
374 error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
379 /* Check that the current operator is either at the end of an
380 expression, or that it is followed by a composition operator or by
381 DW_OP_GNU_uninit (which should terminate the expression). */
384 dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
387 if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece
388 && *op_ptr != DW_OP_GNU_uninit)
389 error (_("DWARF-2 expression error: `%s' operations must be "
390 "used either alone or in conjunction with DW_OP_piece "
391 "or DW_OP_bit_piece."),
395 /* Return true iff the types T1 and T2 are "the same". This only does
396 checks that might reasonably be needed to compare DWARF base
400 base_types_equal_p (struct type *t1, struct type *t2)
402 if (TYPE_CODE (t1) != TYPE_CODE (t2))
404 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
406 return TYPE_LENGTH (t1) == TYPE_LENGTH (t2);
409 /* A convenience function to call get_base_type and return the result.
410 DIE is the DIE whose type we need. SIZE is non-zero if this
411 function should verify that the resulting type has the correct
415 dwarf_expr_context::get_base_type (cu_offset die, int size)
419 if (this->funcs->get_base_type)
421 result = this->funcs->get_base_type (this, die);
423 error (_("Could not find type for DW_OP_GNU_const_type"));
424 if (size != 0 && TYPE_LENGTH (result) != size)
425 error (_("DW_OP_GNU_const_type has different sizes for type and data"));
428 /* Anything will do. */
429 result = builtin_type (this->gdbarch)->builtin_int;
434 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
435 DWARF register number. Otherwise return -1. */
438 dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end)
444 if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
446 if (buf_end - buf != 1)
448 return *buf - DW_OP_reg0;
451 if (*buf == DW_OP_GNU_regval_type)
454 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
457 buf = gdb_skip_leb128 (buf, buf_end);
461 else if (*buf == DW_OP_regx)
464 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
470 if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
475 /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
476 DW_OP_deref* return the DWARF register number. Otherwise return -1.
477 DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
478 size from DW_OP_deref_size. */
481 dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end,
482 CORE_ADDR *deref_size_return)
490 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
492 dwarf_reg = *buf - DW_OP_breg0;
497 else if (*buf == DW_OP_bregx)
500 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
503 if ((int) dwarf_reg != dwarf_reg)
509 buf = gdb_read_sleb128 (buf, buf_end, &offset);
515 if (*buf == DW_OP_deref)
518 *deref_size_return = -1;
520 else if (*buf == DW_OP_deref_size)
525 *deref_size_return = *buf++;
536 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
537 in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */
540 dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
541 CORE_ADDR *fb_offset_return)
548 if (*buf != DW_OP_fbreg)
552 buf = gdb_read_sleb128 (buf, buf_end, &fb_offset);
555 *fb_offset_return = fb_offset;
556 if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
562 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
563 in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0.
564 The matched SP register number depends on GDBARCH. */
567 dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
568 const gdb_byte *buf_end, CORE_ADDR *sp_offset_return)
575 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
577 dwarf_reg = *buf - DW_OP_breg0;
582 if (*buf != DW_OP_bregx)
585 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
590 if (dwarf_reg_to_regnum (gdbarch, dwarf_reg)
591 != gdbarch_sp_regnum (gdbarch))
594 buf = gdb_read_sleb128 (buf, buf_end, &sp_offset);
597 *sp_offset_return = sp_offset;
598 if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
604 /* The engine for the expression evaluator. Using the context in this
605 object, evaluate the expression between OP_PTR and OP_END. */
608 dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr,
609 const gdb_byte *op_end)
611 enum bfd_endian byte_order = gdbarch_byte_order (this->gdbarch);
612 /* Old-style "untyped" DWARF values need special treatment in a
613 couple of places, specifically DW_OP_mod and DW_OP_shr. We need
614 a special type for these values so we can distinguish them from
615 values that have an explicit type, because explicitly-typed
616 values do not need special treatment. This special type must be
617 different (in the `==' sense) from any base type coming from the
619 struct type *address_type = this->address_type ();
621 this->location = DWARF_VALUE_MEMORY;
622 this->initialized = 1; /* Default is initialized. */
624 if (this->recursion_depth > this->max_recursion_depth)
625 error (_("DWARF-2 expression error: Loop detected (%d)."),
626 this->recursion_depth);
627 this->recursion_depth++;
629 while (op_ptr < op_end)
631 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr++;
633 /* Assume the value is not in stack memory.
634 Code that knows otherwise sets this to 1.
635 Some arithmetic on stack addresses can probably be assumed to still
636 be a stack address, but we skip this complication for now.
637 This is just an optimization, so it's always ok to punt
638 and leave this as 0. */
639 int in_stack_memory = 0;
640 uint64_t uoffset, reg;
642 struct value *result_val = NULL;
644 /* The DWARF expression might have a bug causing an infinite
645 loop. In that case, quitting is the only way out. */
682 result = op - DW_OP_lit0;
683 result_val = value_from_ulongest (address_type, result);
687 result = extract_unsigned_integer (op_ptr,
688 this->addr_size, byte_order);
689 op_ptr += this->addr_size;
690 /* Some versions of GCC emit DW_OP_addr before
691 DW_OP_GNU_push_tls_address. In this case the value is an
692 index, not an address. We don't support things like
693 branching between the address and the TLS op. */
694 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
695 result += this->offset;
696 result_val = value_from_ulongest (address_type, result);
699 case DW_OP_GNU_addr_index:
700 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
701 result = (this->funcs->get_addr_index) (this->baton, uoffset);
702 result += this->offset;
703 result_val = value_from_ulongest (address_type, result);
705 case DW_OP_GNU_const_index:
706 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
707 result = (this->funcs->get_addr_index) (this->baton, uoffset);
708 result_val = value_from_ulongest (address_type, result);
712 result = extract_unsigned_integer (op_ptr, 1, byte_order);
713 result_val = value_from_ulongest (address_type, result);
717 result = extract_signed_integer (op_ptr, 1, byte_order);
718 result_val = value_from_ulongest (address_type, result);
722 result = extract_unsigned_integer (op_ptr, 2, byte_order);
723 result_val = value_from_ulongest (address_type, result);
727 result = extract_signed_integer (op_ptr, 2, byte_order);
728 result_val = value_from_ulongest (address_type, result);
732 result = extract_unsigned_integer (op_ptr, 4, byte_order);
733 result_val = value_from_ulongest (address_type, result);
737 result = extract_signed_integer (op_ptr, 4, byte_order);
738 result_val = value_from_ulongest (address_type, result);
742 result = extract_unsigned_integer (op_ptr, 8, byte_order);
743 result_val = value_from_ulongest (address_type, result);
747 result = extract_signed_integer (op_ptr, 8, byte_order);
748 result_val = value_from_ulongest (address_type, result);
752 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
754 result_val = value_from_ulongest (address_type, result);
757 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
759 result_val = value_from_ulongest (address_type, result);
762 /* The DW_OP_reg operations are required to occur alone in
763 location expressions. */
796 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_reg");
798 result = op - DW_OP_reg0;
799 result_val = value_from_ulongest (address_type, result);
800 this->location = DWARF_VALUE_REGISTER;
804 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
805 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
808 result_val = value_from_ulongest (address_type, result);
809 this->location = DWARF_VALUE_REGISTER;
812 case DW_OP_implicit_value:
816 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
817 if (op_ptr + len > op_end)
818 error (_("DW_OP_implicit_value: too few bytes available."));
821 this->location = DWARF_VALUE_LITERAL;
823 dwarf_expr_require_composition (op_ptr, op_end,
824 "DW_OP_implicit_value");
828 case DW_OP_stack_value:
829 this->location = DWARF_VALUE_STACK;
830 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
833 case DW_OP_GNU_implicit_pointer:
837 if (this->ref_addr_size == -1)
838 error (_("DWARF-2 expression error: DW_OP_GNU_implicit_pointer "
839 "is not allowed in frame context"));
841 /* The referred-to DIE of sect_offset kind. */
842 this->len = extract_unsigned_integer (op_ptr, this->ref_addr_size,
844 op_ptr += this->ref_addr_size;
846 /* The byte offset into the data. */
847 op_ptr = safe_read_sleb128 (op_ptr, op_end, &len);
848 result = (ULONGEST) len;
849 result_val = value_from_ulongest (address_type, result);
851 this->location = DWARF_VALUE_IMPLICIT_POINTER;
852 dwarf_expr_require_composition (op_ptr, op_end,
853 "DW_OP_GNU_implicit_pointer");
890 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
891 result = (this->funcs->read_addr_from_reg) (this->baton,
894 result_val = value_from_ulongest (address_type, result);
899 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
900 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
901 result = (this->funcs->read_addr_from_reg) (this->baton, reg);
903 result_val = value_from_ulongest (address_type, result);
908 const gdb_byte *datastart;
910 unsigned int before_stack_len;
912 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
913 /* Rather than create a whole new context, we simply
914 record the stack length before execution, then reset it
915 afterwards, effectively erasing whatever the recursive
917 before_stack_len = this->stack_len;
918 /* FIXME: cagney/2003-03-26: This code should be using
919 get_frame_base_address(), and then implement a dwarf2
920 specific this_base method. */
921 (this->funcs->get_frame_base) (this->baton, &datastart, &datalen);
922 eval (datastart, datalen);
923 if (this->location == DWARF_VALUE_MEMORY)
924 result = fetch_address (0);
925 else if (this->location == DWARF_VALUE_REGISTER)
926 result = (this->funcs->read_addr_from_reg)
928 value_as_long (fetch (0)));
930 error (_("Not implemented: computing frame "
931 "base using explicit value operator"));
932 result = result + offset;
933 result_val = value_from_ulongest (address_type, result);
935 this->stack_len = before_stack_len;
936 this->location = DWARF_VALUE_MEMORY;
941 result_val = fetch (0);
942 in_stack_memory = fetch_in_stack_memory (0);
951 result_val = fetch (offset);
952 in_stack_memory = fetch_in_stack_memory (offset);
957 struct dwarf_stack_value t1, t2;
959 if (this->stack_len < 2)
960 error (_("Not enough elements for "
961 "DW_OP_swap. Need 2, have %d."),
963 t1 = this->stack[this->stack_len - 1];
964 t2 = this->stack[this->stack_len - 2];
965 this->stack[this->stack_len - 1] = t2;
966 this->stack[this->stack_len - 2] = t1;
971 result_val = fetch (1);
972 in_stack_memory = fetch_in_stack_memory (1);
977 struct dwarf_stack_value t1, t2, t3;
979 if (this->stack_len < 3)
980 error (_("Not enough elements for "
981 "DW_OP_rot. Need 3, have %d."),
983 t1 = this->stack[this->stack_len - 1];
984 t2 = this->stack[this->stack_len - 2];
985 t3 = this->stack[this->stack_len - 3];
986 this->stack[this->stack_len - 1] = t2;
987 this->stack[this->stack_len - 2] = t3;
988 this->stack[this->stack_len - 3] = t1;
993 case DW_OP_deref_size:
994 case DW_OP_GNU_deref_type:
996 int addr_size = (op == DW_OP_deref ? this->addr_size : *op_ptr++);
997 gdb_byte *buf = (gdb_byte *) alloca (addr_size);
998 CORE_ADDR addr = fetch_address (0);
1003 if (op == DW_OP_GNU_deref_type)
1007 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1008 type_die.cu_off = uoffset;
1009 type = get_base_type (type_die, 0);
1012 type = address_type;
1014 (this->funcs->read_mem) (this->baton, buf, addr, addr_size);
1016 /* If the size of the object read from memory is different
1017 from the type length, we need to zero-extend it. */
1018 if (TYPE_LENGTH (type) != addr_size)
1021 extract_unsigned_integer (buf, addr_size, byte_order);
1023 buf = (gdb_byte *) alloca (TYPE_LENGTH (type));
1024 store_unsigned_integer (buf, TYPE_LENGTH (type),
1025 byte_order, result);
1028 result_val = value_from_contents_and_address (type, buf, addr);
1035 case DW_OP_plus_uconst:
1037 /* Unary operations. */
1038 result_val = fetch (0);
1044 if (value_less (result_val,
1045 value_zero (value_type (result_val), not_lval)))
1046 result_val = value_neg (result_val);
1049 result_val = value_neg (result_val);
1052 dwarf_require_integral (value_type (result_val));
1053 result_val = value_complement (result_val);
1055 case DW_OP_plus_uconst:
1056 dwarf_require_integral (value_type (result_val));
1057 result = value_as_long (result_val);
1058 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1060 result_val = value_from_ulongest (address_type, result);
1084 /* Binary operations. */
1085 struct value *first, *second;
1093 if (! base_types_equal_p (value_type (first), value_type (second)))
1094 error (_("Incompatible types on DWARF stack"));
1099 dwarf_require_integral (value_type (first));
1100 dwarf_require_integral (value_type (second));
1101 result_val = value_binop (first, second, BINOP_BITWISE_AND);
1104 result_val = value_binop (first, second, BINOP_DIV);
1107 result_val = value_binop (first, second, BINOP_SUB);
1112 struct type *orig_type = value_type (first);
1114 /* We have to special-case "old-style" untyped values
1115 -- these must have mod computed using unsigned
1117 if (orig_type == address_type)
1120 = get_unsigned_type (this->gdbarch, orig_type);
1123 first = value_cast (utype, first);
1124 second = value_cast (utype, second);
1126 /* Note that value_binop doesn't handle float or
1127 decimal float here. This seems unimportant. */
1128 result_val = value_binop (first, second, BINOP_MOD);
1130 result_val = value_cast (orig_type, result_val);
1134 result_val = value_binop (first, second, BINOP_MUL);
1137 dwarf_require_integral (value_type (first));
1138 dwarf_require_integral (value_type (second));
1139 result_val = value_binop (first, second, BINOP_BITWISE_IOR);
1142 result_val = value_binop (first, second, BINOP_ADD);
1145 dwarf_require_integral (value_type (first));
1146 dwarf_require_integral (value_type (second));
1147 result_val = value_binop (first, second, BINOP_LSH);
1150 dwarf_require_integral (value_type (first));
1151 dwarf_require_integral (value_type (second));
1152 if (!TYPE_UNSIGNED (value_type (first)))
1155 = get_unsigned_type (this->gdbarch, value_type (first));
1157 first = value_cast (utype, first);
1160 result_val = value_binop (first, second, BINOP_RSH);
1161 /* Make sure we wind up with the same type we started
1163 if (value_type (result_val) != value_type (second))
1164 result_val = value_cast (value_type (second), result_val);
1167 dwarf_require_integral (value_type (first));
1168 dwarf_require_integral (value_type (second));
1169 if (TYPE_UNSIGNED (value_type (first)))
1172 = get_signed_type (this->gdbarch, value_type (first));
1174 first = value_cast (stype, first);
1177 result_val = value_binop (first, second, BINOP_RSH);
1178 /* Make sure we wind up with the same type we started
1180 if (value_type (result_val) != value_type (second))
1181 result_val = value_cast (value_type (second), result_val);
1184 dwarf_require_integral (value_type (first));
1185 dwarf_require_integral (value_type (second));
1186 result_val = value_binop (first, second, BINOP_BITWISE_XOR);
1189 /* A <= B is !(B < A). */
1190 result = ! value_less (second, first);
1191 result_val = value_from_ulongest (address_type, result);
1194 /* A >= B is !(A < B). */
1195 result = ! value_less (first, second);
1196 result_val = value_from_ulongest (address_type, result);
1199 result = value_equal (first, second);
1200 result_val = value_from_ulongest (address_type, result);
1203 result = value_less (first, second);
1204 result_val = value_from_ulongest (address_type, result);
1207 /* A > B is B < A. */
1208 result = value_less (second, first);
1209 result_val = value_from_ulongest (address_type, result);
1212 result = ! value_equal (first, second);
1213 result_val = value_from_ulongest (address_type, result);
1216 internal_error (__FILE__, __LINE__,
1217 _("Can't be reached."));
1222 case DW_OP_call_frame_cfa:
1223 result = (this->funcs->get_frame_cfa) (this->baton);
1224 result_val = value_from_ulongest (address_type, result);
1225 in_stack_memory = 1;
1228 case DW_OP_GNU_push_tls_address:
1229 case DW_OP_form_tls_address:
1230 /* Variable is at a constant offset in the thread-local
1231 storage block into the objfile for the current thread and
1232 the dynamic linker module containing this expression. Here
1233 we return returns the offset from that base. The top of the
1234 stack has the offset from the beginning of the thread
1235 control block at which the variable is located. Nothing
1236 should follow this operator, so the top of stack would be
1238 result = value_as_long (fetch (0));
1240 result = (this->funcs->get_tls_address) (this->baton, result);
1241 result_val = value_from_ulongest (address_type, result);
1245 offset = extract_signed_integer (op_ptr, 2, byte_order);
1254 offset = extract_signed_integer (op_ptr, 2, byte_order);
1257 dwarf_require_integral (value_type (val));
1258 if (value_as_long (val) != 0)
1271 /* Record the piece. */
1272 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1273 add_piece (8 * size, 0);
1275 /* Pop off the address/regnum, and reset the location
1277 if (this->location != DWARF_VALUE_LITERAL
1278 && this->location != DWARF_VALUE_OPTIMIZED_OUT)
1280 this->location = DWARF_VALUE_MEMORY;
1284 case DW_OP_bit_piece:
1286 uint64_t size, offset;
1288 /* Record the piece. */
1289 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1290 op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
1291 add_piece (size, offset);
1293 /* Pop off the address/regnum, and reset the location
1295 if (this->location != DWARF_VALUE_LITERAL
1296 && this->location != DWARF_VALUE_OPTIMIZED_OUT)
1298 this->location = DWARF_VALUE_MEMORY;
1302 case DW_OP_GNU_uninit:
1303 if (op_ptr != op_end)
1304 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
1305 "be the very last op."));
1307 this->initialized = 0;
1314 offset.cu_off = extract_unsigned_integer (op_ptr, 2, byte_order);
1316 this->funcs->dwarf_call (this, offset);
1324 offset.cu_off = extract_unsigned_integer (op_ptr, 4, byte_order);
1326 this->funcs->dwarf_call (this, offset);
1330 case DW_OP_GNU_entry_value:
1333 CORE_ADDR deref_size;
1334 union call_site_parameter_u kind_u;
1336 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
1337 if (op_ptr + len > op_end)
1338 error (_("DW_OP_GNU_entry_value: too few bytes available."));
1340 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len);
1341 if (kind_u.dwarf_reg != -1)
1344 this->funcs->push_dwarf_reg_entry_value (this,
1345 CALL_SITE_PARAMETER_DWARF_REG,
1347 -1 /* deref_size */);
1351 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr,
1354 if (kind_u.dwarf_reg != -1)
1356 if (deref_size == -1)
1357 deref_size = this->addr_size;
1359 this->funcs->push_dwarf_reg_entry_value (this,
1360 CALL_SITE_PARAMETER_DWARF_REG,
1361 kind_u, deref_size);
1365 error (_("DWARF-2 expression error: DW_OP_GNU_entry_value is "
1366 "supported only for single DW_OP_reg* "
1367 "or for DW_OP_breg*(0)+DW_OP_deref*"));
1370 case DW_OP_GNU_parameter_ref:
1372 union call_site_parameter_u kind_u;
1374 kind_u.param_offset.cu_off = extract_unsigned_integer (op_ptr, 4,
1377 this->funcs->push_dwarf_reg_entry_value (this,
1378 CALL_SITE_PARAMETER_PARAM_OFFSET,
1380 -1 /* deref_size */);
1384 case DW_OP_GNU_const_type:
1388 const gdb_byte *data;
1391 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1392 type_die.cu_off = uoffset;
1397 type = get_base_type (type_die, n);
1398 result_val = value_from_contents (type, data);
1402 case DW_OP_GNU_regval_type:
1407 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1408 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1409 type_die.cu_off = uoffset;
1411 type = get_base_type (type_die, 0);
1412 result_val = this->funcs->get_reg_value (this->baton, type, reg);
1416 case DW_OP_GNU_convert:
1417 case DW_OP_GNU_reinterpret:
1422 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1423 type_die.cu_off = uoffset;
1425 if (type_die.cu_off == 0)
1426 type = address_type;
1428 type = get_base_type (type_die, 0);
1430 result_val = fetch (0);
1433 if (op == DW_OP_GNU_convert)
1434 result_val = value_cast (type, result_val);
1435 else if (type == value_type (result_val))
1439 else if (TYPE_LENGTH (type)
1440 != TYPE_LENGTH (value_type (result_val)))
1441 error (_("DW_OP_GNU_reinterpret has wrong size"));
1444 = value_from_contents (type,
1445 value_contents_all (result_val));
1449 case DW_OP_push_object_address:
1450 /* Return the address of the object we are currently observing. */
1451 result = (this->funcs->get_object_address) (this->baton);
1452 result_val = value_from_ulongest (address_type, result);
1456 error (_("Unhandled dwarf expression opcode 0x%x"), op);
1459 /* Most things push a result value. */
1460 gdb_assert (result_val != NULL);
1461 push (result_val, in_stack_memory);
1466 /* To simplify our main caller, if the result is an implicit
1467 pointer, then make a pieced value. This is ok because we can't
1468 have implicit pointers in contexts where pieces are invalid. */
1469 if (this->location == DWARF_VALUE_IMPLICIT_POINTER)
1470 add_piece (8 * this->addr_size, 0);
1473 this->recursion_depth--;
1474 gdb_assert (this->recursion_depth >= 0);
1477 /* Stub dwarf_expr_context_funcs.get_frame_base implementation. */
1480 ctx_no_get_frame_base (void *baton, const gdb_byte **start, size_t *length)
1482 error (_("%s is invalid in this context"), "DW_OP_fbreg");
1485 /* Stub dwarf_expr_context_funcs.get_frame_cfa implementation. */
1488 ctx_no_get_frame_cfa (void *baton)
1490 error (_("%s is invalid in this context"), "DW_OP_call_frame_cfa");
1493 /* Stub dwarf_expr_context_funcs.get_frame_pc implementation. */
1496 ctx_no_get_frame_pc (void *baton)
1498 error (_("%s is invalid in this context"), "DW_OP_GNU_implicit_pointer");
1501 /* Stub dwarf_expr_context_funcs.get_tls_address implementation. */
1504 ctx_no_get_tls_address (void *baton, CORE_ADDR offset)
1506 error (_("%s is invalid in this context"), "DW_OP_form_tls_address");
1509 /* Stub dwarf_expr_context_funcs.dwarf_call implementation. */
1512 ctx_no_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
1514 error (_("%s is invalid in this context"), "DW_OP_call*");
1517 /* Stub dwarf_expr_context_funcs.get_base_type implementation. */
1520 ctx_no_get_base_type (struct dwarf_expr_context *ctx, cu_offset die)
1522 error (_("Support for typed DWARF is not supported in this context"));
1525 /* Stub dwarf_expr_context_funcs.push_dwarf_block_entry_value
1529 ctx_no_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
1530 enum call_site_parameter_kind kind,
1531 union call_site_parameter_u kind_u,
1534 internal_error (__FILE__, __LINE__,
1535 _("Support for DW_OP_GNU_entry_value is unimplemented"));
1538 /* Stub dwarf_expr_context_funcs.get_addr_index implementation. */
1541 ctx_no_get_addr_index (void *baton, unsigned int index)
1543 error (_("%s is invalid in this context"), "DW_OP_GNU_addr_index");
1546 /* Provide a prototype to silence -Wmissing-prototypes. */
1547 extern initialize_file_ftype _initialize_dwarf2expr;
1550 _initialize_dwarf2expr (void)
1553 = gdbarch_data_register_post_init (dwarf_gdbarch_types_init);