1 /* DWARF 2 Expression Evaluator.
3 Copyright (C) 2001-2017 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 ()
99 max_recursion_depth (0x100),
100 location (DWARF_VALUE_MEMORY),
105 this->stack = XNEWVEC (struct dwarf_stack_value, this->stack_allocated);
108 /* Clean up a dwarf_expr_context. */
110 dwarf_expr_context::~dwarf_expr_context ()
115 /* Expand the memory allocated stack to contain at least
116 NEED more elements than are currently used. */
119 dwarf_expr_context::grow_stack (size_t need)
121 if (this->stack_len + need > this->stack_allocated)
123 size_t newlen = this->stack_len + need + 10;
125 this->stack = XRESIZEVEC (struct dwarf_stack_value, this->stack, newlen);
126 this->stack_allocated = newlen;
130 /* Push VALUE onto the stack. */
133 dwarf_expr_context::push (struct value *value, bool in_stack_memory)
135 struct dwarf_stack_value *v;
138 v = &this->stack[this->stack_len++];
140 v->in_stack_memory = in_stack_memory;
143 /* Push VALUE onto the stack. */
146 dwarf_expr_context::push_address (CORE_ADDR value, bool in_stack_memory)
148 push (value_from_ulongest (address_type (), value), in_stack_memory);
151 /* Pop the top item off of the stack. */
154 dwarf_expr_context::pop ()
156 if (this->stack_len <= 0)
157 error (_("dwarf expression stack underflow"));
161 /* Retrieve the N'th item on the stack. */
164 dwarf_expr_context::fetch (int n)
166 if (this->stack_len <= n)
167 error (_("Asked for position %d of stack, "
168 "stack only has %d elements on it."),
170 return this->stack[this->stack_len - (1 + n)].value;
173 /* Require that TYPE be an integral type; throw an exception if not. */
176 dwarf_require_integral (struct type *type)
178 if (TYPE_CODE (type) != TYPE_CODE_INT
179 && TYPE_CODE (type) != TYPE_CODE_CHAR
180 && TYPE_CODE (type) != TYPE_CODE_BOOL)
181 error (_("integral type expected in DWARF expression"));
184 /* Return the unsigned form of TYPE. TYPE is necessarily an integral
188 get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
190 switch (TYPE_LENGTH (type))
193 return builtin_type (gdbarch)->builtin_uint8;
195 return builtin_type (gdbarch)->builtin_uint16;
197 return builtin_type (gdbarch)->builtin_uint32;
199 return builtin_type (gdbarch)->builtin_uint64;
201 error (_("no unsigned variant found for type, while evaluating "
202 "DWARF expression"));
206 /* Return the signed form of TYPE. TYPE is necessarily an integral
210 get_signed_type (struct gdbarch *gdbarch, struct type *type)
212 switch (TYPE_LENGTH (type))
215 return builtin_type (gdbarch)->builtin_int8;
217 return builtin_type (gdbarch)->builtin_int16;
219 return builtin_type (gdbarch)->builtin_int32;
221 return builtin_type (gdbarch)->builtin_int64;
223 error (_("no signed variant found for type, while evaluating "
224 "DWARF expression"));
228 /* Retrieve the N'th item on the stack, converted to an address. */
231 dwarf_expr_context::fetch_address (int n)
233 struct value *result_val = fetch (n);
234 enum bfd_endian byte_order = gdbarch_byte_order (this->gdbarch);
237 dwarf_require_integral (value_type (result_val));
238 result = extract_unsigned_integer (value_contents (result_val),
239 TYPE_LENGTH (value_type (result_val)),
242 /* For most architectures, calling extract_unsigned_integer() alone
243 is sufficient for extracting an address. However, some
244 architectures (e.g. MIPS) use signed addresses and using
245 extract_unsigned_integer() will not produce a correct
246 result. Make sure we invoke gdbarch_integer_to_address()
247 for those architectures which require it. */
248 if (gdbarch_integer_to_address_p (this->gdbarch))
250 gdb_byte *buf = (gdb_byte *) alloca (this->addr_size);
251 struct type *int_type = get_unsigned_type (this->gdbarch,
252 value_type (result_val));
254 store_unsigned_integer (buf, this->addr_size, byte_order, result);
255 return gdbarch_integer_to_address (this->gdbarch, int_type, buf);
258 return (CORE_ADDR) result;
261 /* Retrieve the in_stack_memory flag of the N'th item on the stack. */
264 dwarf_expr_context::fetch_in_stack_memory (int n)
266 if (this->stack_len <= n)
267 error (_("Asked for position %d of stack, "
268 "stack only has %d elements on it."),
270 return this->stack[this->stack_len - (1 + n)].in_stack_memory;
273 /* Return true if the expression stack is empty. */
276 dwarf_expr_context::stack_empty_p () const
278 return this->stack_len == 0;
281 /* Add a new piece to the dwarf_expr_context's piece list. */
283 dwarf_expr_context::add_piece (ULONGEST size, ULONGEST offset)
285 this->pieces.emplace_back ();
286 dwarf_expr_piece &p = this->pieces.back ();
288 p.location = this->location;
292 if (p.location == DWARF_VALUE_LITERAL)
294 p.v.literal.data = this->data;
295 p.v.literal.length = this->len;
297 else if (stack_empty_p ())
299 p.location = DWARF_VALUE_OPTIMIZED_OUT;
300 /* Also reset the context's location, for our callers. This is
301 a somewhat strange approach, but this lets us avoid setting
302 the location to DWARF_VALUE_MEMORY in all the individual
303 cases in the evaluator. */
304 this->location = DWARF_VALUE_OPTIMIZED_OUT;
306 else if (p.location == DWARF_VALUE_MEMORY)
308 p.v.mem.addr = fetch_address (0);
309 p.v.mem.in_stack_memory = fetch_in_stack_memory (0);
311 else if (p.location == DWARF_VALUE_IMPLICIT_POINTER)
313 p.v.ptr.die_sect_off = (sect_offset) this->len;
314 p.v.ptr.offset = value_as_long (fetch (0));
316 else if (p.location == DWARF_VALUE_REGISTER)
317 p.v.regno = value_as_long (fetch (0));
320 p.v.value = fetch (0);
324 /* Evaluate the expression at ADDR (LEN bytes long). */
327 dwarf_expr_context::eval (const gdb_byte *addr, size_t len)
329 int old_recursion_depth = this->recursion_depth;
331 execute_stack_op (addr, addr + len);
333 /* RECURSION_DEPTH becomes invalid if an exception was thrown here. */
335 gdb_assert (this->recursion_depth == old_recursion_depth);
338 /* Helper to read a uleb128 value or throw an error. */
341 safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
344 buf = gdb_read_uleb128 (buf, buf_end, r);
346 error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
350 /* Helper to read a sleb128 value or throw an error. */
353 safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
356 buf = gdb_read_sleb128 (buf, buf_end, r);
358 error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
363 safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
365 buf = gdb_skip_leb128 (buf, buf_end);
367 error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
372 /* Check that the current operator is either at the end of an
373 expression, or that it is followed by a composition operator or by
374 DW_OP_GNU_uninit (which should terminate the expression). */
377 dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
380 if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece
381 && *op_ptr != DW_OP_GNU_uninit)
382 error (_("DWARF-2 expression error: `%s' operations must be "
383 "used either alone or in conjunction with DW_OP_piece "
384 "or DW_OP_bit_piece."),
388 /* Return true iff the types T1 and T2 are "the same". This only does
389 checks that might reasonably be needed to compare DWARF base
393 base_types_equal_p (struct type *t1, struct type *t2)
395 if (TYPE_CODE (t1) != TYPE_CODE (t2))
397 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
399 return TYPE_LENGTH (t1) == TYPE_LENGTH (t2);
402 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
403 DWARF register number. Otherwise return -1. */
406 dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end)
412 if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
414 if (buf_end - buf != 1)
416 return *buf - DW_OP_reg0;
419 if (*buf == DW_OP_regval_type || *buf == DW_OP_GNU_regval_type)
422 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
425 buf = gdb_skip_leb128 (buf, buf_end);
429 else if (*buf == DW_OP_regx)
432 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
438 if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
443 /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
444 DW_OP_deref* return the DWARF register number. Otherwise return -1.
445 DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
446 size from DW_OP_deref_size. */
449 dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end,
450 CORE_ADDR *deref_size_return)
458 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
460 dwarf_reg = *buf - DW_OP_breg0;
465 else if (*buf == DW_OP_bregx)
468 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
471 if ((int) dwarf_reg != dwarf_reg)
477 buf = gdb_read_sleb128 (buf, buf_end, &offset);
483 if (*buf == DW_OP_deref)
486 *deref_size_return = -1;
488 else if (*buf == DW_OP_deref_size)
493 *deref_size_return = *buf++;
504 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
505 in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */
508 dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
509 CORE_ADDR *fb_offset_return)
516 if (*buf != DW_OP_fbreg)
520 buf = gdb_read_sleb128 (buf, buf_end, &fb_offset);
523 *fb_offset_return = fb_offset;
524 if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
530 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
531 in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0.
532 The matched SP register number depends on GDBARCH. */
535 dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
536 const gdb_byte *buf_end, CORE_ADDR *sp_offset_return)
543 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
545 dwarf_reg = *buf - DW_OP_breg0;
550 if (*buf != DW_OP_bregx)
553 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
558 if (dwarf_reg_to_regnum (gdbarch, dwarf_reg)
559 != gdbarch_sp_regnum (gdbarch))
562 buf = gdb_read_sleb128 (buf, buf_end, &sp_offset);
565 *sp_offset_return = sp_offset;
566 if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
572 /* The engine for the expression evaluator. Using the context in this
573 object, evaluate the expression between OP_PTR and OP_END. */
576 dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr,
577 const gdb_byte *op_end)
579 enum bfd_endian byte_order = gdbarch_byte_order (this->gdbarch);
580 /* Old-style "untyped" DWARF values need special treatment in a
581 couple of places, specifically DW_OP_mod and DW_OP_shr. We need
582 a special type for these values so we can distinguish them from
583 values that have an explicit type, because explicitly-typed
584 values do not need special treatment. This special type must be
585 different (in the `==' sense) from any base type coming from the
587 struct type *address_type = this->address_type ();
589 this->location = DWARF_VALUE_MEMORY;
590 this->initialized = 1; /* Default is initialized. */
592 if (this->recursion_depth > this->max_recursion_depth)
593 error (_("DWARF-2 expression error: Loop detected (%d)."),
594 this->recursion_depth);
595 this->recursion_depth++;
597 while (op_ptr < op_end)
599 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr++;
601 /* Assume the value is not in stack memory.
602 Code that knows otherwise sets this to true.
603 Some arithmetic on stack addresses can probably be assumed to still
604 be a stack address, but we skip this complication for now.
605 This is just an optimization, so it's always ok to punt
606 and leave this as false. */
607 bool in_stack_memory = false;
608 uint64_t uoffset, reg;
610 struct value *result_val = NULL;
612 /* The DWARF expression might have a bug causing an infinite
613 loop. In that case, quitting is the only way out. */
650 result = op - DW_OP_lit0;
651 result_val = value_from_ulongest (address_type, result);
655 result = extract_unsigned_integer (op_ptr,
656 this->addr_size, byte_order);
657 op_ptr += this->addr_size;
658 /* Some versions of GCC emit DW_OP_addr before
659 DW_OP_GNU_push_tls_address. In this case the value is an
660 index, not an address. We don't support things like
661 branching between the address and the TLS op. */
662 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
663 result += this->offset;
664 result_val = value_from_ulongest (address_type, result);
667 case DW_OP_GNU_addr_index:
668 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
669 result = this->get_addr_index (uoffset);
670 result += this->offset;
671 result_val = value_from_ulongest (address_type, result);
673 case DW_OP_GNU_const_index:
674 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
675 result = this->get_addr_index (uoffset);
676 result_val = value_from_ulongest (address_type, result);
680 result = extract_unsigned_integer (op_ptr, 1, byte_order);
681 result_val = value_from_ulongest (address_type, result);
685 result = extract_signed_integer (op_ptr, 1, byte_order);
686 result_val = value_from_ulongest (address_type, result);
690 result = extract_unsigned_integer (op_ptr, 2, byte_order);
691 result_val = value_from_ulongest (address_type, result);
695 result = extract_signed_integer (op_ptr, 2, byte_order);
696 result_val = value_from_ulongest (address_type, result);
700 result = extract_unsigned_integer (op_ptr, 4, byte_order);
701 result_val = value_from_ulongest (address_type, result);
705 result = extract_signed_integer (op_ptr, 4, byte_order);
706 result_val = value_from_ulongest (address_type, result);
710 result = extract_unsigned_integer (op_ptr, 8, byte_order);
711 result_val = value_from_ulongest (address_type, result);
715 result = extract_signed_integer (op_ptr, 8, byte_order);
716 result_val = value_from_ulongest (address_type, result);
720 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
722 result_val = value_from_ulongest (address_type, result);
725 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
727 result_val = value_from_ulongest (address_type, result);
730 /* The DW_OP_reg operations are required to occur alone in
731 location expressions. */
764 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_reg");
766 result = op - DW_OP_reg0;
767 result_val = value_from_ulongest (address_type, result);
768 this->location = DWARF_VALUE_REGISTER;
772 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
773 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
776 result_val = value_from_ulongest (address_type, result);
777 this->location = DWARF_VALUE_REGISTER;
780 case DW_OP_implicit_value:
784 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
785 if (op_ptr + len > op_end)
786 error (_("DW_OP_implicit_value: too few bytes available."));
789 this->location = DWARF_VALUE_LITERAL;
791 dwarf_expr_require_composition (op_ptr, op_end,
792 "DW_OP_implicit_value");
796 case DW_OP_stack_value:
797 this->location = DWARF_VALUE_STACK;
798 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
801 case DW_OP_implicit_pointer:
802 case DW_OP_GNU_implicit_pointer:
806 if (this->ref_addr_size == -1)
807 error (_("DWARF-2 expression error: DW_OP_implicit_pointer "
808 "is not allowed in frame context"));
810 /* The referred-to DIE of sect_offset kind. */
811 this->len = extract_unsigned_integer (op_ptr, this->ref_addr_size,
813 op_ptr += this->ref_addr_size;
815 /* The byte offset into the data. */
816 op_ptr = safe_read_sleb128 (op_ptr, op_end, &len);
817 result = (ULONGEST) len;
818 result_val = value_from_ulongest (address_type, result);
820 this->location = DWARF_VALUE_IMPLICIT_POINTER;
821 dwarf_expr_require_composition (op_ptr, op_end,
822 "DW_OP_implicit_pointer");
859 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
860 result = this->read_addr_from_reg (op - DW_OP_breg0);
862 result_val = value_from_ulongest (address_type, result);
867 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
868 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
869 result = this->read_addr_from_reg (reg);
871 result_val = value_from_ulongest (address_type, result);
876 const gdb_byte *datastart;
878 unsigned int before_stack_len;
880 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
881 /* Rather than create a whole new context, we simply
882 record the stack length before execution, then reset it
883 afterwards, effectively erasing whatever the recursive
885 before_stack_len = this->stack_len;
886 /* FIXME: cagney/2003-03-26: This code should be using
887 get_frame_base_address(), and then implement a dwarf2
888 specific this_base method. */
889 this->get_frame_base (&datastart, &datalen);
890 eval (datastart, datalen);
891 if (this->location == DWARF_VALUE_MEMORY)
892 result = fetch_address (0);
893 else if (this->location == DWARF_VALUE_REGISTER)
894 result = this->read_addr_from_reg (value_as_long (fetch (0)));
896 error (_("Not implemented: computing frame "
897 "base using explicit value operator"));
898 result = result + offset;
899 result_val = value_from_ulongest (address_type, result);
900 in_stack_memory = true;
901 this->stack_len = before_stack_len;
902 this->location = DWARF_VALUE_MEMORY;
907 result_val = fetch (0);
908 in_stack_memory = fetch_in_stack_memory (0);
917 result_val = fetch (offset);
918 in_stack_memory = fetch_in_stack_memory (offset);
923 struct dwarf_stack_value t1, t2;
925 if (this->stack_len < 2)
926 error (_("Not enough elements for "
927 "DW_OP_swap. Need 2, have %d."),
929 t1 = this->stack[this->stack_len - 1];
930 t2 = this->stack[this->stack_len - 2];
931 this->stack[this->stack_len - 1] = t2;
932 this->stack[this->stack_len - 2] = t1;
937 result_val = fetch (1);
938 in_stack_memory = fetch_in_stack_memory (1);
943 struct dwarf_stack_value t1, t2, t3;
945 if (this->stack_len < 3)
946 error (_("Not enough elements for "
947 "DW_OP_rot. Need 3, have %d."),
949 t1 = this->stack[this->stack_len - 1];
950 t2 = this->stack[this->stack_len - 2];
951 t3 = this->stack[this->stack_len - 3];
952 this->stack[this->stack_len - 1] = t2;
953 this->stack[this->stack_len - 2] = t3;
954 this->stack[this->stack_len - 3] = t1;
959 case DW_OP_deref_size:
960 case DW_OP_deref_type:
961 case DW_OP_GNU_deref_type:
963 int addr_size = (op == DW_OP_deref ? this->addr_size : *op_ptr++);
964 gdb_byte *buf = (gdb_byte *) alloca (addr_size);
965 CORE_ADDR addr = fetch_address (0);
970 if (op == DW_OP_deref_type || op == DW_OP_GNU_deref_type)
972 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
973 cu_offset type_die_cu_off = (cu_offset) uoffset;
974 type = get_base_type (type_die_cu_off, 0);
979 this->read_mem (buf, addr, addr_size);
981 /* If the size of the object read from memory is different
982 from the type length, we need to zero-extend it. */
983 if (TYPE_LENGTH (type) != addr_size)
986 extract_unsigned_integer (buf, addr_size, byte_order);
988 buf = (gdb_byte *) alloca (TYPE_LENGTH (type));
989 store_unsigned_integer (buf, TYPE_LENGTH (type),
993 result_val = value_from_contents_and_address (type, buf, addr);
1000 case DW_OP_plus_uconst:
1002 /* Unary operations. */
1003 result_val = fetch (0);
1009 if (value_less (result_val,
1010 value_zero (value_type (result_val), not_lval)))
1011 result_val = value_neg (result_val);
1014 result_val = value_neg (result_val);
1017 dwarf_require_integral (value_type (result_val));
1018 result_val = value_complement (result_val);
1020 case DW_OP_plus_uconst:
1021 dwarf_require_integral (value_type (result_val));
1022 result = value_as_long (result_val);
1023 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1025 result_val = value_from_ulongest (address_type, result);
1049 /* Binary operations. */
1050 struct value *first, *second;
1058 if (! base_types_equal_p (value_type (first), value_type (second)))
1059 error (_("Incompatible types on DWARF stack"));
1064 dwarf_require_integral (value_type (first));
1065 dwarf_require_integral (value_type (second));
1066 result_val = value_binop (first, second, BINOP_BITWISE_AND);
1069 result_val = value_binop (first, second, BINOP_DIV);
1072 result_val = value_binop (first, second, BINOP_SUB);
1077 struct type *orig_type = value_type (first);
1079 /* We have to special-case "old-style" untyped values
1080 -- these must have mod computed using unsigned
1082 if (orig_type == address_type)
1085 = get_unsigned_type (this->gdbarch, orig_type);
1088 first = value_cast (utype, first);
1089 second = value_cast (utype, second);
1091 /* Note that value_binop doesn't handle float or
1092 decimal float here. This seems unimportant. */
1093 result_val = value_binop (first, second, BINOP_MOD);
1095 result_val = value_cast (orig_type, result_val);
1099 result_val = value_binop (first, second, BINOP_MUL);
1102 dwarf_require_integral (value_type (first));
1103 dwarf_require_integral (value_type (second));
1104 result_val = value_binop (first, second, BINOP_BITWISE_IOR);
1107 result_val = value_binop (first, second, BINOP_ADD);
1110 dwarf_require_integral (value_type (first));
1111 dwarf_require_integral (value_type (second));
1112 result_val = value_binop (first, second, BINOP_LSH);
1115 dwarf_require_integral (value_type (first));
1116 dwarf_require_integral (value_type (second));
1117 if (!TYPE_UNSIGNED (value_type (first)))
1120 = get_unsigned_type (this->gdbarch, value_type (first));
1122 first = value_cast (utype, first);
1125 result_val = value_binop (first, second, BINOP_RSH);
1126 /* Make sure we wind up with the same type we started
1128 if (value_type (result_val) != value_type (second))
1129 result_val = value_cast (value_type (second), result_val);
1132 dwarf_require_integral (value_type (first));
1133 dwarf_require_integral (value_type (second));
1134 if (TYPE_UNSIGNED (value_type (first)))
1137 = get_signed_type (this->gdbarch, value_type (first));
1139 first = value_cast (stype, first);
1142 result_val = value_binop (first, second, BINOP_RSH);
1143 /* Make sure we wind up with the same type we started
1145 if (value_type (result_val) != value_type (second))
1146 result_val = value_cast (value_type (second), result_val);
1149 dwarf_require_integral (value_type (first));
1150 dwarf_require_integral (value_type (second));
1151 result_val = value_binop (first, second, BINOP_BITWISE_XOR);
1154 /* A <= B is !(B < A). */
1155 result = ! value_less (second, first);
1156 result_val = value_from_ulongest (address_type, result);
1159 /* A >= B is !(A < B). */
1160 result = ! value_less (first, second);
1161 result_val = value_from_ulongest (address_type, result);
1164 result = value_equal (first, second);
1165 result_val = value_from_ulongest (address_type, result);
1168 result = value_less (first, second);
1169 result_val = value_from_ulongest (address_type, result);
1172 /* A > B is B < A. */
1173 result = value_less (second, first);
1174 result_val = value_from_ulongest (address_type, result);
1177 result = ! value_equal (first, second);
1178 result_val = value_from_ulongest (address_type, result);
1181 internal_error (__FILE__, __LINE__,
1182 _("Can't be reached."));
1187 case DW_OP_call_frame_cfa:
1188 result = this->get_frame_cfa ();
1189 result_val = value_from_ulongest (address_type, result);
1190 in_stack_memory = true;
1193 case DW_OP_GNU_push_tls_address:
1194 case DW_OP_form_tls_address:
1195 /* Variable is at a constant offset in the thread-local
1196 storage block into the objfile for the current thread and
1197 the dynamic linker module containing this expression. Here
1198 we return returns the offset from that base. The top of the
1199 stack has the offset from the beginning of the thread
1200 control block at which the variable is located. Nothing
1201 should follow this operator, so the top of stack would be
1203 result = value_as_long (fetch (0));
1205 result = this->get_tls_address (result);
1206 result_val = value_from_ulongest (address_type, result);
1210 offset = extract_signed_integer (op_ptr, 2, byte_order);
1219 offset = extract_signed_integer (op_ptr, 2, byte_order);
1222 dwarf_require_integral (value_type (val));
1223 if (value_as_long (val) != 0)
1236 /* Record the piece. */
1237 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1238 add_piece (8 * size, 0);
1240 /* Pop off the address/regnum, and reset the location
1242 if (this->location != DWARF_VALUE_LITERAL
1243 && this->location != DWARF_VALUE_OPTIMIZED_OUT)
1245 this->location = DWARF_VALUE_MEMORY;
1249 case DW_OP_bit_piece:
1251 uint64_t size, offset;
1253 /* Record the piece. */
1254 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1255 op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
1256 add_piece (size, offset);
1258 /* Pop off the address/regnum, and reset the location
1260 if (this->location != DWARF_VALUE_LITERAL
1261 && this->location != DWARF_VALUE_OPTIMIZED_OUT)
1263 this->location = DWARF_VALUE_MEMORY;
1267 case DW_OP_GNU_uninit:
1268 if (op_ptr != op_end)
1269 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
1270 "be the very last op."));
1272 this->initialized = 0;
1278 = (cu_offset) extract_unsigned_integer (op_ptr, 2, byte_order);
1280 this->dwarf_call (cu_off);
1287 = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order);
1289 this->dwarf_call (cu_off);
1293 case DW_OP_entry_value:
1294 case DW_OP_GNU_entry_value:
1297 CORE_ADDR deref_size;
1298 union call_site_parameter_u kind_u;
1300 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
1301 if (op_ptr + len > op_end)
1302 error (_("DW_OP_entry_value: too few bytes available."));
1304 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len);
1305 if (kind_u.dwarf_reg != -1)
1308 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG,
1310 -1 /* deref_size */);
1314 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr,
1317 if (kind_u.dwarf_reg != -1)
1319 if (deref_size == -1)
1320 deref_size = this->addr_size;
1322 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG,
1323 kind_u, deref_size);
1327 error (_("DWARF-2 expression error: DW_OP_entry_value is "
1328 "supported only for single DW_OP_reg* "
1329 "or for DW_OP_breg*(0)+DW_OP_deref*"));
1332 case DW_OP_GNU_parameter_ref:
1334 union call_site_parameter_u kind_u;
1337 = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order);
1339 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_PARAM_OFFSET,
1341 -1 /* deref_size */);
1345 case DW_OP_const_type:
1346 case DW_OP_GNU_const_type:
1349 const gdb_byte *data;
1352 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1353 cu_offset type_die_cu_off = (cu_offset) uoffset;
1359 type = get_base_type (type_die_cu_off, n);
1360 result_val = value_from_contents (type, data);
1364 case DW_OP_regval_type:
1365 case DW_OP_GNU_regval_type:
1369 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1370 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1371 cu_offset type_die_cu_off = (cu_offset) uoffset;
1373 type = get_base_type (type_die_cu_off, 0);
1374 result_val = this->get_reg_value (type, reg);
1379 case DW_OP_GNU_convert:
1380 case DW_OP_reinterpret:
1381 case DW_OP_GNU_reinterpret:
1385 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1386 cu_offset type_die_cu_off = (cu_offset) uoffset;
1388 if (to_underlying (type_die_cu_off) == 0)
1389 type = address_type;
1391 type = get_base_type (type_die_cu_off, 0);
1393 result_val = fetch (0);
1396 if (op == DW_OP_convert || op == DW_OP_GNU_convert)
1397 result_val = value_cast (type, result_val);
1398 else if (type == value_type (result_val))
1402 else if (TYPE_LENGTH (type)
1403 != TYPE_LENGTH (value_type (result_val)))
1404 error (_("DW_OP_reinterpret has wrong size"));
1407 = value_from_contents (type,
1408 value_contents_all (result_val));
1412 case DW_OP_push_object_address:
1413 /* Return the address of the object we are currently observing. */
1414 result = this->get_object_address ();
1415 result_val = value_from_ulongest (address_type, result);
1419 error (_("Unhandled dwarf expression opcode 0x%x"), op);
1422 /* Most things push a result value. */
1423 gdb_assert (result_val != NULL);
1424 push (result_val, in_stack_memory);
1429 /* To simplify our main caller, if the result is an implicit
1430 pointer, then make a pieced value. This is ok because we can't
1431 have implicit pointers in contexts where pieces are invalid. */
1432 if (this->location == DWARF_VALUE_IMPLICIT_POINTER)
1433 add_piece (8 * this->addr_size, 0);
1436 this->recursion_depth--;
1437 gdb_assert (this->recursion_depth >= 0);
1441 _initialize_dwarf2expr (void)
1444 = gdbarch_data_register_post_init (dwarf_gdbarch_types_init);