1 /* DWARF 2 Expression Evaluator.
3 Copyright (C) 2001-2019 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"
30 #include "common/underlying.h"
32 /* Cookie for gdbarch data. */
34 static struct gdbarch_data *dwarf_arch_cookie;
36 /* This holds gdbarch-specific types used by the DWARF expression
37 evaluator. See comments in execute_stack_op. */
39 struct dwarf_gdbarch_types
41 struct type *dw_types[3];
44 /* Allocate and fill in dwarf_gdbarch_types for an arch. */
47 dwarf_gdbarch_types_init (struct gdbarch *gdbarch)
49 struct dwarf_gdbarch_types *types
50 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct dwarf_gdbarch_types);
52 /* The types themselves are lazily initialized. */
57 /* Return the type used for DWARF operations where the type is
58 unspecified in the DWARF spec. Only certain sizes are
62 dwarf_expr_context::address_type () const
64 struct dwarf_gdbarch_types *types
65 = (struct dwarf_gdbarch_types *) gdbarch_data (this->gdbarch,
69 if (this->addr_size == 2)
71 else if (this->addr_size == 4)
73 else if (this->addr_size == 8)
76 error (_("Unsupported address size in DWARF expressions: %d bits"),
79 if (types->dw_types[ndx] == NULL)
81 = arch_integer_type (this->gdbarch,
83 0, "<signed DWARF address type>");
85 return types->dw_types[ndx];
88 /* Create a new context for the expression evaluator. */
90 dwarf_expr_context::dwarf_expr_context ()
96 max_recursion_depth (0x100),
97 location (DWARF_VALUE_MEMORY),
104 /* Push VALUE onto the stack. */
107 dwarf_expr_context::push (struct value *value, bool in_stack_memory)
109 stack.emplace_back (value, in_stack_memory);
112 /* Push VALUE onto the stack. */
115 dwarf_expr_context::push_address (CORE_ADDR value, bool in_stack_memory)
117 push (value_from_ulongest (address_type (), value), in_stack_memory);
120 /* Pop the top item off of the stack. */
123 dwarf_expr_context::pop ()
126 error (_("dwarf expression stack underflow"));
131 /* Retrieve the N'th item on the stack. */
134 dwarf_expr_context::fetch (int n)
136 if (stack.size () <= n)
137 error (_("Asked for position %d of stack, "
138 "stack only has %zu elements on it."),
140 return stack[stack.size () - (1 + n)].value;
143 /* Require that TYPE be an integral type; throw an exception if not. */
146 dwarf_require_integral (struct type *type)
148 if (TYPE_CODE (type) != TYPE_CODE_INT
149 && TYPE_CODE (type) != TYPE_CODE_CHAR
150 && TYPE_CODE (type) != TYPE_CODE_BOOL)
151 error (_("integral type expected in DWARF expression"));
154 /* Return the unsigned form of TYPE. TYPE is necessarily an integral
158 get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
160 switch (TYPE_LENGTH (type))
163 return builtin_type (gdbarch)->builtin_uint8;
165 return builtin_type (gdbarch)->builtin_uint16;
167 return builtin_type (gdbarch)->builtin_uint32;
169 return builtin_type (gdbarch)->builtin_uint64;
171 error (_("no unsigned variant found for type, while evaluating "
172 "DWARF expression"));
176 /* Return the signed form of TYPE. TYPE is necessarily an integral
180 get_signed_type (struct gdbarch *gdbarch, struct type *type)
182 switch (TYPE_LENGTH (type))
185 return builtin_type (gdbarch)->builtin_int8;
187 return builtin_type (gdbarch)->builtin_int16;
189 return builtin_type (gdbarch)->builtin_int32;
191 return builtin_type (gdbarch)->builtin_int64;
193 error (_("no signed variant found for type, while evaluating "
194 "DWARF expression"));
198 /* Retrieve the N'th item on the stack, converted to an address. */
201 dwarf_expr_context::fetch_address (int n)
203 struct value *result_val = fetch (n);
204 enum bfd_endian byte_order = gdbarch_byte_order (this->gdbarch);
207 dwarf_require_integral (value_type (result_val));
208 result = extract_unsigned_integer (value_contents (result_val),
209 TYPE_LENGTH (value_type (result_val)),
212 /* For most architectures, calling extract_unsigned_integer() alone
213 is sufficient for extracting an address. However, some
214 architectures (e.g. MIPS) use signed addresses and using
215 extract_unsigned_integer() will not produce a correct
216 result. Make sure we invoke gdbarch_integer_to_address()
217 for those architectures which require it. */
218 if (gdbarch_integer_to_address_p (this->gdbarch))
220 gdb_byte *buf = (gdb_byte *) alloca (this->addr_size);
221 struct type *int_type = get_unsigned_type (this->gdbarch,
222 value_type (result_val));
224 store_unsigned_integer (buf, this->addr_size, byte_order, result);
225 return gdbarch_integer_to_address (this->gdbarch, int_type, buf);
228 return (CORE_ADDR) result;
231 /* Retrieve the in_stack_memory flag of the N'th item on the stack. */
234 dwarf_expr_context::fetch_in_stack_memory (int n)
236 if (stack.size () <= n)
237 error (_("Asked for position %d of stack, "
238 "stack only has %zu elements on it."),
240 return stack[stack.size () - (1 + n)].in_stack_memory;
243 /* Return true if the expression stack is empty. */
246 dwarf_expr_context::stack_empty_p () const
248 return stack.empty ();
251 /* Add a new piece to the dwarf_expr_context's piece list. */
253 dwarf_expr_context::add_piece (ULONGEST size, ULONGEST offset)
255 this->pieces.emplace_back ();
256 dwarf_expr_piece &p = this->pieces.back ();
258 p.location = this->location;
262 if (p.location == DWARF_VALUE_LITERAL)
264 p.v.literal.data = this->data;
265 p.v.literal.length = this->len;
267 else if (stack_empty_p ())
269 p.location = DWARF_VALUE_OPTIMIZED_OUT;
270 /* Also reset the context's location, for our callers. This is
271 a somewhat strange approach, but this lets us avoid setting
272 the location to DWARF_VALUE_MEMORY in all the individual
273 cases in the evaluator. */
274 this->location = DWARF_VALUE_OPTIMIZED_OUT;
276 else if (p.location == DWARF_VALUE_MEMORY)
278 p.v.mem.addr = fetch_address (0);
279 p.v.mem.in_stack_memory = fetch_in_stack_memory (0);
281 else if (p.location == DWARF_VALUE_IMPLICIT_POINTER)
283 p.v.ptr.die_sect_off = (sect_offset) this->len;
284 p.v.ptr.offset = value_as_long (fetch (0));
286 else if (p.location == DWARF_VALUE_REGISTER)
287 p.v.regno = value_as_long (fetch (0));
290 p.v.value = fetch (0);
294 /* Evaluate the expression at ADDR (LEN bytes long). */
297 dwarf_expr_context::eval (const gdb_byte *addr, size_t len)
299 int old_recursion_depth = this->recursion_depth;
301 execute_stack_op (addr, addr + len);
303 /* RECURSION_DEPTH becomes invalid if an exception was thrown here. */
305 gdb_assert (this->recursion_depth == old_recursion_depth);
308 /* Helper to read a uleb128 value or throw an error. */
311 safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
314 buf = gdb_read_uleb128 (buf, buf_end, r);
316 error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
320 /* Helper to read a sleb128 value or throw an error. */
323 safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
326 buf = gdb_read_sleb128 (buf, buf_end, r);
328 error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
333 safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
335 buf = gdb_skip_leb128 (buf, buf_end);
337 error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
342 /* Check that the current operator is either at the end of an
343 expression, or that it is followed by a composition operator or by
344 DW_OP_GNU_uninit (which should terminate the expression). */
347 dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
350 if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece
351 && *op_ptr != DW_OP_GNU_uninit)
352 error (_("DWARF-2 expression error: `%s' operations must be "
353 "used either alone or in conjunction with DW_OP_piece "
354 "or DW_OP_bit_piece."),
358 /* Return true iff the types T1 and T2 are "the same". This only does
359 checks that might reasonably be needed to compare DWARF base
363 base_types_equal_p (struct type *t1, struct type *t2)
365 if (TYPE_CODE (t1) != TYPE_CODE (t2))
367 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
369 return TYPE_LENGTH (t1) == TYPE_LENGTH (t2);
372 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
373 DWARF register number. Otherwise return -1. */
376 dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end)
382 if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
384 if (buf_end - buf != 1)
386 return *buf - DW_OP_reg0;
389 if (*buf == DW_OP_regval_type || *buf == DW_OP_GNU_regval_type)
392 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
395 buf = gdb_skip_leb128 (buf, buf_end);
399 else if (*buf == DW_OP_regx)
402 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
408 if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
413 /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
414 DW_OP_deref* return the DWARF register number. Otherwise return -1.
415 DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
416 size from DW_OP_deref_size. */
419 dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end,
420 CORE_ADDR *deref_size_return)
428 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
430 dwarf_reg = *buf - DW_OP_breg0;
435 else if (*buf == DW_OP_bregx)
438 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
441 if ((int) dwarf_reg != dwarf_reg)
447 buf = gdb_read_sleb128 (buf, buf_end, &offset);
453 if (*buf == DW_OP_deref)
456 *deref_size_return = -1;
458 else if (*buf == DW_OP_deref_size)
463 *deref_size_return = *buf++;
474 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
475 in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */
478 dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
479 CORE_ADDR *fb_offset_return)
486 if (*buf != DW_OP_fbreg)
490 buf = gdb_read_sleb128 (buf, buf_end, &fb_offset);
493 *fb_offset_return = fb_offset;
494 if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
500 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
501 in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0.
502 The matched SP register number depends on GDBARCH. */
505 dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
506 const gdb_byte *buf_end, CORE_ADDR *sp_offset_return)
513 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
515 dwarf_reg = *buf - DW_OP_breg0;
520 if (*buf != DW_OP_bregx)
523 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
528 if (dwarf_reg_to_regnum (gdbarch, dwarf_reg)
529 != gdbarch_sp_regnum (gdbarch))
532 buf = gdb_read_sleb128 (buf, buf_end, &sp_offset);
535 *sp_offset_return = sp_offset;
536 if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
542 /* The engine for the expression evaluator. Using the context in this
543 object, evaluate the expression between OP_PTR and OP_END. */
546 dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr,
547 const gdb_byte *op_end)
549 enum bfd_endian byte_order = gdbarch_byte_order (this->gdbarch);
550 /* Old-style "untyped" DWARF values need special treatment in a
551 couple of places, specifically DW_OP_mod and DW_OP_shr. We need
552 a special type for these values so we can distinguish them from
553 values that have an explicit type, because explicitly-typed
554 values do not need special treatment. This special type must be
555 different (in the `==' sense) from any base type coming from the
557 struct type *address_type = this->address_type ();
559 this->location = DWARF_VALUE_MEMORY;
560 this->initialized = 1; /* Default is initialized. */
562 if (this->recursion_depth > this->max_recursion_depth)
563 error (_("DWARF-2 expression error: Loop detected (%d)."),
564 this->recursion_depth);
565 this->recursion_depth++;
567 while (op_ptr < op_end)
569 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr++;
571 /* Assume the value is not in stack memory.
572 Code that knows otherwise sets this to true.
573 Some arithmetic on stack addresses can probably be assumed to still
574 be a stack address, but we skip this complication for now.
575 This is just an optimization, so it's always ok to punt
576 and leave this as false. */
577 bool in_stack_memory = false;
578 uint64_t uoffset, reg;
580 struct value *result_val = NULL;
582 /* The DWARF expression might have a bug causing an infinite
583 loop. In that case, quitting is the only way out. */
620 result = op - DW_OP_lit0;
621 result_val = value_from_ulongest (address_type, result);
625 result = extract_unsigned_integer (op_ptr,
626 this->addr_size, byte_order);
627 op_ptr += this->addr_size;
628 /* Some versions of GCC emit DW_OP_addr before
629 DW_OP_GNU_push_tls_address. In this case the value is an
630 index, not an address. We don't support things like
631 branching between the address and the TLS op. */
632 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
633 result += this->offset;
634 result_val = value_from_ulongest (address_type, result);
638 case DW_OP_GNU_addr_index:
639 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
640 result = this->get_addr_index (uoffset);
641 result += this->offset;
642 result_val = value_from_ulongest (address_type, result);
644 case DW_OP_GNU_const_index:
645 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
646 result = this->get_addr_index (uoffset);
647 result_val = value_from_ulongest (address_type, result);
651 result = extract_unsigned_integer (op_ptr, 1, byte_order);
652 result_val = value_from_ulongest (address_type, result);
656 result = extract_signed_integer (op_ptr, 1, byte_order);
657 result_val = value_from_ulongest (address_type, result);
661 result = extract_unsigned_integer (op_ptr, 2, byte_order);
662 result_val = value_from_ulongest (address_type, result);
666 result = extract_signed_integer (op_ptr, 2, byte_order);
667 result_val = value_from_ulongest (address_type, result);
671 result = extract_unsigned_integer (op_ptr, 4, byte_order);
672 result_val = value_from_ulongest (address_type, result);
676 result = extract_signed_integer (op_ptr, 4, byte_order);
677 result_val = value_from_ulongest (address_type, result);
681 result = extract_unsigned_integer (op_ptr, 8, byte_order);
682 result_val = value_from_ulongest (address_type, result);
686 result = extract_signed_integer (op_ptr, 8, byte_order);
687 result_val = value_from_ulongest (address_type, result);
691 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
693 result_val = value_from_ulongest (address_type, result);
696 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
698 result_val = value_from_ulongest (address_type, result);
701 /* The DW_OP_reg operations are required to occur alone in
702 location expressions. */
735 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_reg");
737 result = op - DW_OP_reg0;
738 result_val = value_from_ulongest (address_type, result);
739 this->location = DWARF_VALUE_REGISTER;
743 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
744 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
747 result_val = value_from_ulongest (address_type, result);
748 this->location = DWARF_VALUE_REGISTER;
751 case DW_OP_implicit_value:
755 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
756 if (op_ptr + len > op_end)
757 error (_("DW_OP_implicit_value: too few bytes available."));
760 this->location = DWARF_VALUE_LITERAL;
762 dwarf_expr_require_composition (op_ptr, op_end,
763 "DW_OP_implicit_value");
767 case DW_OP_stack_value:
768 this->location = DWARF_VALUE_STACK;
769 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
772 case DW_OP_implicit_pointer:
773 case DW_OP_GNU_implicit_pointer:
777 if (this->ref_addr_size == -1)
778 error (_("DWARF-2 expression error: DW_OP_implicit_pointer "
779 "is not allowed in frame context"));
781 /* The referred-to DIE of sect_offset kind. */
782 this->len = extract_unsigned_integer (op_ptr, this->ref_addr_size,
784 op_ptr += this->ref_addr_size;
786 /* The byte offset into the data. */
787 op_ptr = safe_read_sleb128 (op_ptr, op_end, &len);
788 result = (ULONGEST) len;
789 result_val = value_from_ulongest (address_type, result);
791 this->location = DWARF_VALUE_IMPLICIT_POINTER;
792 dwarf_expr_require_composition (op_ptr, op_end,
793 "DW_OP_implicit_pointer");
830 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
831 result = this->read_addr_from_reg (op - DW_OP_breg0);
833 result_val = value_from_ulongest (address_type, result);
838 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
839 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
840 result = this->read_addr_from_reg (reg);
842 result_val = value_from_ulongest (address_type, result);
847 const gdb_byte *datastart;
850 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
852 /* Rather than create a whole new context, we simply
853 backup the current stack locally and install a new empty stack,
854 then reset it afterwards, effectively erasing whatever the
855 recursive call put there. */
856 std::vector<dwarf_stack_value> saved_stack = std::move (stack);
859 /* FIXME: cagney/2003-03-26: This code should be using
860 get_frame_base_address(), and then implement a dwarf2
861 specific this_base method. */
862 this->get_frame_base (&datastart, &datalen);
863 eval (datastart, datalen);
864 if (this->location == DWARF_VALUE_MEMORY)
865 result = fetch_address (0);
866 else if (this->location == DWARF_VALUE_REGISTER)
867 result = this->read_addr_from_reg (value_as_long (fetch (0)));
869 error (_("Not implemented: computing frame "
870 "base using explicit value operator"));
871 result = result + offset;
872 result_val = value_from_ulongest (address_type, result);
873 in_stack_memory = true;
875 /* Restore the content of the original stack. */
876 stack = std::move (saved_stack);
878 this->location = DWARF_VALUE_MEMORY;
883 result_val = fetch (0);
884 in_stack_memory = fetch_in_stack_memory (0);
893 result_val = fetch (offset);
894 in_stack_memory = fetch_in_stack_memory (offset);
899 if (stack.size () < 2)
900 error (_("Not enough elements for "
901 "DW_OP_swap. Need 2, have %zu."),
904 dwarf_stack_value &t1 = stack[stack.size () - 1];
905 dwarf_stack_value &t2 = stack[stack.size () - 2];
911 result_val = fetch (1);
912 in_stack_memory = fetch_in_stack_memory (1);
917 if (stack.size () < 3)
918 error (_("Not enough elements for "
919 "DW_OP_rot. Need 3, have %zu."),
922 dwarf_stack_value temp = stack[stack.size () - 1];
923 stack[stack.size () - 1] = stack[stack.size () - 2];
924 stack[stack.size () - 2] = stack[stack.size () - 3];
925 stack[stack.size () - 3] = temp;
930 case DW_OP_deref_size:
931 case DW_OP_deref_type:
932 case DW_OP_GNU_deref_type:
934 int addr_size = (op == DW_OP_deref ? this->addr_size : *op_ptr++);
935 gdb_byte *buf = (gdb_byte *) alloca (addr_size);
936 CORE_ADDR addr = fetch_address (0);
941 if (op == DW_OP_deref_type || op == DW_OP_GNU_deref_type)
943 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
944 cu_offset type_die_cu_off = (cu_offset) uoffset;
945 type = get_base_type (type_die_cu_off, 0);
950 this->read_mem (buf, addr, addr_size);
952 /* If the size of the object read from memory is different
953 from the type length, we need to zero-extend it. */
954 if (TYPE_LENGTH (type) != addr_size)
957 extract_unsigned_integer (buf, addr_size, byte_order);
959 buf = (gdb_byte *) alloca (TYPE_LENGTH (type));
960 store_unsigned_integer (buf, TYPE_LENGTH (type),
964 result_val = value_from_contents_and_address (type, buf, addr);
971 case DW_OP_plus_uconst:
973 /* Unary operations. */
974 result_val = fetch (0);
980 if (value_less (result_val,
981 value_zero (value_type (result_val), not_lval)))
982 result_val = value_neg (result_val);
985 result_val = value_neg (result_val);
988 dwarf_require_integral (value_type (result_val));
989 result_val = value_complement (result_val);
991 case DW_OP_plus_uconst:
992 dwarf_require_integral (value_type (result_val));
993 result = value_as_long (result_val);
994 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
996 result_val = value_from_ulongest (address_type, result);
1020 /* Binary operations. */
1021 struct value *first, *second;
1029 if (! base_types_equal_p (value_type (first), value_type (second)))
1030 error (_("Incompatible types on DWARF stack"));
1035 dwarf_require_integral (value_type (first));
1036 dwarf_require_integral (value_type (second));
1037 result_val = value_binop (first, second, BINOP_BITWISE_AND);
1040 result_val = value_binop (first, second, BINOP_DIV);
1043 result_val = value_binop (first, second, BINOP_SUB);
1048 struct type *orig_type = value_type (first);
1050 /* We have to special-case "old-style" untyped values
1051 -- these must have mod computed using unsigned
1053 if (orig_type == address_type)
1056 = get_unsigned_type (this->gdbarch, orig_type);
1059 first = value_cast (utype, first);
1060 second = value_cast (utype, second);
1062 /* Note that value_binop doesn't handle float or
1063 decimal float here. This seems unimportant. */
1064 result_val = value_binop (first, second, BINOP_MOD);
1066 result_val = value_cast (orig_type, result_val);
1070 result_val = value_binop (first, second, BINOP_MUL);
1073 dwarf_require_integral (value_type (first));
1074 dwarf_require_integral (value_type (second));
1075 result_val = value_binop (first, second, BINOP_BITWISE_IOR);
1078 result_val = value_binop (first, second, BINOP_ADD);
1081 dwarf_require_integral (value_type (first));
1082 dwarf_require_integral (value_type (second));
1083 result_val = value_binop (first, second, BINOP_LSH);
1086 dwarf_require_integral (value_type (first));
1087 dwarf_require_integral (value_type (second));
1088 if (!TYPE_UNSIGNED (value_type (first)))
1091 = get_unsigned_type (this->gdbarch, value_type (first));
1093 first = value_cast (utype, first);
1096 result_val = value_binop (first, second, BINOP_RSH);
1097 /* Make sure we wind up with the same type we started
1099 if (value_type (result_val) != value_type (second))
1100 result_val = value_cast (value_type (second), result_val);
1103 dwarf_require_integral (value_type (first));
1104 dwarf_require_integral (value_type (second));
1105 if (TYPE_UNSIGNED (value_type (first)))
1108 = get_signed_type (this->gdbarch, value_type (first));
1110 first = value_cast (stype, first);
1113 result_val = value_binop (first, second, BINOP_RSH);
1114 /* Make sure we wind up with the same type we started
1116 if (value_type (result_val) != value_type (second))
1117 result_val = value_cast (value_type (second), result_val);
1120 dwarf_require_integral (value_type (first));
1121 dwarf_require_integral (value_type (second));
1122 result_val = value_binop (first, second, BINOP_BITWISE_XOR);
1125 /* A <= B is !(B < A). */
1126 result = ! value_less (second, first);
1127 result_val = value_from_ulongest (address_type, result);
1130 /* A >= B is !(A < B). */
1131 result = ! value_less (first, second);
1132 result_val = value_from_ulongest (address_type, result);
1135 result = value_equal (first, second);
1136 result_val = value_from_ulongest (address_type, result);
1139 result = value_less (first, second);
1140 result_val = value_from_ulongest (address_type, result);
1143 /* A > B is B < A. */
1144 result = value_less (second, first);
1145 result_val = value_from_ulongest (address_type, result);
1148 result = ! value_equal (first, second);
1149 result_val = value_from_ulongest (address_type, result);
1152 internal_error (__FILE__, __LINE__,
1153 _("Can't be reached."));
1158 case DW_OP_call_frame_cfa:
1159 result = this->get_frame_cfa ();
1160 result_val = value_from_ulongest (address_type, result);
1161 in_stack_memory = true;
1164 case DW_OP_GNU_push_tls_address:
1165 case DW_OP_form_tls_address:
1166 /* Variable is at a constant offset in the thread-local
1167 storage block into the objfile for the current thread and
1168 the dynamic linker module containing this expression. Here
1169 we return returns the offset from that base. The top of the
1170 stack has the offset from the beginning of the thread
1171 control block at which the variable is located. Nothing
1172 should follow this operator, so the top of stack would be
1174 result = value_as_long (fetch (0));
1176 result = this->get_tls_address (result);
1177 result_val = value_from_ulongest (address_type, result);
1181 offset = extract_signed_integer (op_ptr, 2, byte_order);
1190 offset = extract_signed_integer (op_ptr, 2, byte_order);
1193 dwarf_require_integral (value_type (val));
1194 if (value_as_long (val) != 0)
1207 /* Record the piece. */
1208 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1209 add_piece (8 * size, 0);
1211 /* Pop off the address/regnum, and reset the location
1213 if (this->location != DWARF_VALUE_LITERAL
1214 && this->location != DWARF_VALUE_OPTIMIZED_OUT)
1216 this->location = DWARF_VALUE_MEMORY;
1220 case DW_OP_bit_piece:
1222 uint64_t size, uleb_offset;
1224 /* Record the piece. */
1225 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1226 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uleb_offset);
1227 add_piece (size, uleb_offset);
1229 /* Pop off the address/regnum, and reset the location
1231 if (this->location != DWARF_VALUE_LITERAL
1232 && this->location != DWARF_VALUE_OPTIMIZED_OUT)
1234 this->location = DWARF_VALUE_MEMORY;
1238 case DW_OP_GNU_uninit:
1239 if (op_ptr != op_end)
1240 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
1241 "be the very last op."));
1243 this->initialized = 0;
1249 = (cu_offset) extract_unsigned_integer (op_ptr, 2, byte_order);
1251 this->dwarf_call (cu_off);
1258 = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order);
1260 this->dwarf_call (cu_off);
1264 case DW_OP_GNU_variable_value:
1266 sect_offset sect_off
1267 = (sect_offset) extract_unsigned_integer (op_ptr,
1268 this->ref_addr_size,
1270 op_ptr += this->ref_addr_size;
1271 result_val = this->dwarf_variable_value (sect_off);
1275 case DW_OP_entry_value:
1276 case DW_OP_GNU_entry_value:
1279 CORE_ADDR deref_size;
1280 union call_site_parameter_u kind_u;
1282 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
1283 if (op_ptr + len > op_end)
1284 error (_("DW_OP_entry_value: too few bytes available."));
1286 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len);
1287 if (kind_u.dwarf_reg != -1)
1290 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG,
1292 -1 /* deref_size */);
1296 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr,
1299 if (kind_u.dwarf_reg != -1)
1301 if (deref_size == -1)
1302 deref_size = this->addr_size;
1304 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG,
1305 kind_u, deref_size);
1309 error (_("DWARF-2 expression error: DW_OP_entry_value is "
1310 "supported only for single DW_OP_reg* "
1311 "or for DW_OP_breg*(0)+DW_OP_deref*"));
1314 case DW_OP_GNU_parameter_ref:
1316 union call_site_parameter_u kind_u;
1319 = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order);
1321 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_PARAM_OFFSET,
1323 -1 /* deref_size */);
1327 case DW_OP_const_type:
1328 case DW_OP_GNU_const_type:
1331 const gdb_byte *data;
1334 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1335 cu_offset type_die_cu_off = (cu_offset) uoffset;
1341 type = get_base_type (type_die_cu_off, n);
1342 result_val = value_from_contents (type, data);
1346 case DW_OP_regval_type:
1347 case DW_OP_GNU_regval_type:
1351 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1352 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1353 cu_offset type_die_cu_off = (cu_offset) uoffset;
1355 type = get_base_type (type_die_cu_off, 0);
1356 result_val = this->get_reg_value (type, reg);
1361 case DW_OP_GNU_convert:
1362 case DW_OP_reinterpret:
1363 case DW_OP_GNU_reinterpret:
1367 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1368 cu_offset type_die_cu_off = (cu_offset) uoffset;
1370 if (to_underlying (type_die_cu_off) == 0)
1371 type = address_type;
1373 type = get_base_type (type_die_cu_off, 0);
1375 result_val = fetch (0);
1378 if (op == DW_OP_convert || op == DW_OP_GNU_convert)
1379 result_val = value_cast (type, result_val);
1380 else if (type == value_type (result_val))
1384 else if (TYPE_LENGTH (type)
1385 != TYPE_LENGTH (value_type (result_val)))
1386 error (_("DW_OP_reinterpret has wrong size"));
1389 = value_from_contents (type,
1390 value_contents_all (result_val));
1394 case DW_OP_push_object_address:
1395 /* Return the address of the object we are currently observing. */
1396 result = this->get_object_address ();
1397 result_val = value_from_ulongest (address_type, result);
1401 error (_("Unhandled dwarf expression opcode 0x%x"), op);
1404 /* Most things push a result value. */
1405 gdb_assert (result_val != NULL);
1406 push (result_val, in_stack_memory);
1411 /* To simplify our main caller, if the result is an implicit
1412 pointer, then make a pieced value. This is ok because we can't
1413 have implicit pointers in contexts where pieces are invalid. */
1414 if (this->location == DWARF_VALUE_IMPLICIT_POINTER)
1415 add_piece (8 * this->addr_size, 0);
1417 this->recursion_depth--;
1418 gdb_assert (this->recursion_depth >= 0);
1422 _initialize_dwarf2expr (void)
1425 = gdbarch_data_register_post_init (dwarf_gdbarch_types_init);