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),
107 this->stack = XNEWVEC (struct dwarf_stack_value, this->stack_allocated);
110 /* Clean up a dwarf_expr_context. */
112 dwarf_expr_context::~dwarf_expr_context ()
115 xfree (this->pieces);
118 /* Expand the memory allocated stack to contain at least
119 NEED more elements than are currently used. */
122 dwarf_expr_context::grow_stack (size_t need)
124 if (this->stack_len + need > this->stack_allocated)
126 size_t newlen = this->stack_len + need + 10;
128 this->stack = XRESIZEVEC (struct dwarf_stack_value, this->stack, newlen);
129 this->stack_allocated = newlen;
133 /* Push VALUE onto the stack. */
136 dwarf_expr_context::push (struct value *value, int in_stack_memory)
138 struct dwarf_stack_value *v;
141 v = &this->stack[this->stack_len++];
143 v->in_stack_memory = in_stack_memory;
146 /* Push VALUE onto the stack. */
149 dwarf_expr_context::push_address (CORE_ADDR value, int in_stack_memory)
151 push (value_from_ulongest (address_type (), value), in_stack_memory);
154 /* Pop the top item off of the stack. */
157 dwarf_expr_context::pop ()
159 if (this->stack_len <= 0)
160 error (_("dwarf expression stack underflow"));
164 /* Retrieve the N'th item on the stack. */
167 dwarf_expr_context::fetch (int n)
169 if (this->stack_len <= n)
170 error (_("Asked for position %d of stack, "
171 "stack only has %d elements on it."),
173 return this->stack[this->stack_len - (1 + n)].value;
176 /* Require that TYPE be an integral type; throw an exception if not. */
179 dwarf_require_integral (struct type *type)
181 if (TYPE_CODE (type) != TYPE_CODE_INT
182 && TYPE_CODE (type) != TYPE_CODE_CHAR
183 && TYPE_CODE (type) != TYPE_CODE_BOOL)
184 error (_("integral type expected in DWARF expression"));
187 /* Return the unsigned form of TYPE. TYPE is necessarily an integral
191 get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
193 switch (TYPE_LENGTH (type))
196 return builtin_type (gdbarch)->builtin_uint8;
198 return builtin_type (gdbarch)->builtin_uint16;
200 return builtin_type (gdbarch)->builtin_uint32;
202 return builtin_type (gdbarch)->builtin_uint64;
204 error (_("no unsigned variant found for type, while evaluating "
205 "DWARF expression"));
209 /* Return the signed form of TYPE. TYPE is necessarily an integral
213 get_signed_type (struct gdbarch *gdbarch, struct type *type)
215 switch (TYPE_LENGTH (type))
218 return builtin_type (gdbarch)->builtin_int8;
220 return builtin_type (gdbarch)->builtin_int16;
222 return builtin_type (gdbarch)->builtin_int32;
224 return builtin_type (gdbarch)->builtin_int64;
226 error (_("no signed variant found for type, while evaluating "
227 "DWARF expression"));
231 /* Retrieve the N'th item on the stack, converted to an address. */
234 dwarf_expr_context::fetch_address (int n)
236 struct value *result_val = fetch (n);
237 enum bfd_endian byte_order = gdbarch_byte_order (this->gdbarch);
240 dwarf_require_integral (value_type (result_val));
241 result = extract_unsigned_integer (value_contents (result_val),
242 TYPE_LENGTH (value_type (result_val)),
245 /* For most architectures, calling extract_unsigned_integer() alone
246 is sufficient for extracting an address. However, some
247 architectures (e.g. MIPS) use signed addresses and using
248 extract_unsigned_integer() will not produce a correct
249 result. Make sure we invoke gdbarch_integer_to_address()
250 for those architectures which require it. */
251 if (gdbarch_integer_to_address_p (this->gdbarch))
253 gdb_byte *buf = (gdb_byte *) alloca (this->addr_size);
254 struct type *int_type = get_unsigned_type (this->gdbarch,
255 value_type (result_val));
257 store_unsigned_integer (buf, this->addr_size, byte_order, result);
258 return gdbarch_integer_to_address (this->gdbarch, int_type, buf);
261 return (CORE_ADDR) result;
264 /* Retrieve the in_stack_memory flag of the N'th item on the stack. */
267 dwarf_expr_context::fetch_in_stack_memory (int n)
269 if (this->stack_len <= n)
270 error (_("Asked for position %d of stack, "
271 "stack only has %d elements on it."),
273 return this->stack[this->stack_len - (1 + n)].in_stack_memory;
276 /* Return true if the expression stack is empty. */
279 dwarf_expr_context::stack_empty_p () const
281 return this->stack_len == 0;
284 /* Add a new piece to the dwarf_expr_context's piece list. */
286 dwarf_expr_context::add_piece (ULONGEST size, ULONGEST offset)
288 struct dwarf_expr_piece *p;
293 = XRESIZEVEC (struct dwarf_expr_piece, this->pieces, this->num_pieces);
295 p = &this->pieces[this->num_pieces - 1];
296 p->location = this->location;
300 if (p->location == DWARF_VALUE_LITERAL)
302 p->v.literal.data = this->data;
303 p->v.literal.length = this->len;
305 else if (stack_empty_p ())
307 p->location = DWARF_VALUE_OPTIMIZED_OUT;
308 /* Also reset the context's location, for our callers. This is
309 a somewhat strange approach, but this lets us avoid setting
310 the location to DWARF_VALUE_MEMORY in all the individual
311 cases in the evaluator. */
312 this->location = DWARF_VALUE_OPTIMIZED_OUT;
314 else if (p->location == DWARF_VALUE_MEMORY)
316 p->v.mem.addr = fetch_address (0);
317 p->v.mem.in_stack_memory = fetch_in_stack_memory (0);
319 else if (p->location == DWARF_VALUE_IMPLICIT_POINTER)
321 p->v.ptr.die_sect_off = (sect_offset) this->len;
322 p->v.ptr.offset = value_as_long (fetch (0));
324 else if (p->location == DWARF_VALUE_REGISTER)
325 p->v.regno = value_as_long (fetch (0));
328 p->v.value = fetch (0);
332 /* Evaluate the expression at ADDR (LEN bytes long). */
335 dwarf_expr_context::eval (const gdb_byte *addr, size_t len)
337 int old_recursion_depth = this->recursion_depth;
339 execute_stack_op (addr, addr + len);
341 /* RECURSION_DEPTH becomes invalid if an exception was thrown here. */
343 gdb_assert (this->recursion_depth == old_recursion_depth);
346 /* Helper to read a uleb128 value or throw an error. */
349 safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
352 buf = gdb_read_uleb128 (buf, buf_end, r);
354 error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
358 /* Helper to read a sleb128 value or throw an error. */
361 safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
364 buf = gdb_read_sleb128 (buf, buf_end, r);
366 error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
371 safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
373 buf = gdb_skip_leb128 (buf, buf_end);
375 error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
380 /* Check that the current operator is either at the end of an
381 expression, or that it is followed by a composition operator or by
382 DW_OP_GNU_uninit (which should terminate the expression). */
385 dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
388 if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece
389 && *op_ptr != DW_OP_GNU_uninit)
390 error (_("DWARF-2 expression error: `%s' operations must be "
391 "used either alone or in conjunction with DW_OP_piece "
392 "or DW_OP_bit_piece."),
396 /* Return true iff the types T1 and T2 are "the same". This only does
397 checks that might reasonably be needed to compare DWARF base
401 base_types_equal_p (struct type *t1, struct type *t2)
403 if (TYPE_CODE (t1) != TYPE_CODE (t2))
405 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
407 return TYPE_LENGTH (t1) == TYPE_LENGTH (t2);
410 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
411 DWARF register number. Otherwise return -1. */
414 dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end)
420 if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
422 if (buf_end - buf != 1)
424 return *buf - DW_OP_reg0;
427 if (*buf == DW_OP_regval_type || *buf == DW_OP_GNU_regval_type)
430 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
433 buf = gdb_skip_leb128 (buf, buf_end);
437 else if (*buf == DW_OP_regx)
440 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
446 if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
451 /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
452 DW_OP_deref* return the DWARF register number. Otherwise return -1.
453 DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
454 size from DW_OP_deref_size. */
457 dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end,
458 CORE_ADDR *deref_size_return)
466 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
468 dwarf_reg = *buf - DW_OP_breg0;
473 else if (*buf == DW_OP_bregx)
476 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
479 if ((int) dwarf_reg != dwarf_reg)
485 buf = gdb_read_sleb128 (buf, buf_end, &offset);
491 if (*buf == DW_OP_deref)
494 *deref_size_return = -1;
496 else if (*buf == DW_OP_deref_size)
501 *deref_size_return = *buf++;
512 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
513 in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */
516 dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
517 CORE_ADDR *fb_offset_return)
524 if (*buf != DW_OP_fbreg)
528 buf = gdb_read_sleb128 (buf, buf_end, &fb_offset);
531 *fb_offset_return = fb_offset;
532 if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
538 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
539 in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0.
540 The matched SP register number depends on GDBARCH. */
543 dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
544 const gdb_byte *buf_end, CORE_ADDR *sp_offset_return)
551 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
553 dwarf_reg = *buf - DW_OP_breg0;
558 if (*buf != DW_OP_bregx)
561 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
566 if (dwarf_reg_to_regnum (gdbarch, dwarf_reg)
567 != gdbarch_sp_regnum (gdbarch))
570 buf = gdb_read_sleb128 (buf, buf_end, &sp_offset);
573 *sp_offset_return = sp_offset;
574 if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
580 /* The engine for the expression evaluator. Using the context in this
581 object, evaluate the expression between OP_PTR and OP_END. */
584 dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr,
585 const gdb_byte *op_end)
587 enum bfd_endian byte_order = gdbarch_byte_order (this->gdbarch);
588 /* Old-style "untyped" DWARF values need special treatment in a
589 couple of places, specifically DW_OP_mod and DW_OP_shr. We need
590 a special type for these values so we can distinguish them from
591 values that have an explicit type, because explicitly-typed
592 values do not need special treatment. This special type must be
593 different (in the `==' sense) from any base type coming from the
595 struct type *address_type = this->address_type ();
597 this->location = DWARF_VALUE_MEMORY;
598 this->initialized = 1; /* Default is initialized. */
600 if (this->recursion_depth > this->max_recursion_depth)
601 error (_("DWARF-2 expression error: Loop detected (%d)."),
602 this->recursion_depth);
603 this->recursion_depth++;
605 while (op_ptr < op_end)
607 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr++;
609 /* Assume the value is not in stack memory.
610 Code that knows otherwise sets this to 1.
611 Some arithmetic on stack addresses can probably be assumed to still
612 be a stack address, but we skip this complication for now.
613 This is just an optimization, so it's always ok to punt
614 and leave this as 0. */
615 int in_stack_memory = 0;
616 uint64_t uoffset, reg;
618 struct value *result_val = NULL;
620 /* The DWARF expression might have a bug causing an infinite
621 loop. In that case, quitting is the only way out. */
658 result = op - DW_OP_lit0;
659 result_val = value_from_ulongest (address_type, result);
663 result = extract_unsigned_integer (op_ptr,
664 this->addr_size, byte_order);
665 op_ptr += this->addr_size;
666 /* Some versions of GCC emit DW_OP_addr before
667 DW_OP_GNU_push_tls_address. In this case the value is an
668 index, not an address. We don't support things like
669 branching between the address and the TLS op. */
670 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
671 result += this->offset;
672 result_val = value_from_ulongest (address_type, result);
675 case DW_OP_GNU_addr_index:
676 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
677 result = this->get_addr_index (uoffset);
678 result += this->offset;
679 result_val = value_from_ulongest (address_type, result);
681 case DW_OP_GNU_const_index:
682 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
683 result = this->get_addr_index (uoffset);
684 result_val = value_from_ulongest (address_type, result);
688 result = extract_unsigned_integer (op_ptr, 1, byte_order);
689 result_val = value_from_ulongest (address_type, result);
693 result = extract_signed_integer (op_ptr, 1, byte_order);
694 result_val = value_from_ulongest (address_type, result);
698 result = extract_unsigned_integer (op_ptr, 2, byte_order);
699 result_val = value_from_ulongest (address_type, result);
703 result = extract_signed_integer (op_ptr, 2, byte_order);
704 result_val = value_from_ulongest (address_type, result);
708 result = extract_unsigned_integer (op_ptr, 4, byte_order);
709 result_val = value_from_ulongest (address_type, result);
713 result = extract_signed_integer (op_ptr, 4, byte_order);
714 result_val = value_from_ulongest (address_type, result);
718 result = extract_unsigned_integer (op_ptr, 8, byte_order);
719 result_val = value_from_ulongest (address_type, result);
723 result = extract_signed_integer (op_ptr, 8, byte_order);
724 result_val = value_from_ulongest (address_type, result);
728 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
730 result_val = value_from_ulongest (address_type, result);
733 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
735 result_val = value_from_ulongest (address_type, result);
738 /* The DW_OP_reg operations are required to occur alone in
739 location expressions. */
772 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_reg");
774 result = op - DW_OP_reg0;
775 result_val = value_from_ulongest (address_type, result);
776 this->location = DWARF_VALUE_REGISTER;
780 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
781 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
784 result_val = value_from_ulongest (address_type, result);
785 this->location = DWARF_VALUE_REGISTER;
788 case DW_OP_implicit_value:
792 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
793 if (op_ptr + len > op_end)
794 error (_("DW_OP_implicit_value: too few bytes available."));
797 this->location = DWARF_VALUE_LITERAL;
799 dwarf_expr_require_composition (op_ptr, op_end,
800 "DW_OP_implicit_value");
804 case DW_OP_stack_value:
805 this->location = DWARF_VALUE_STACK;
806 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
809 case DW_OP_implicit_pointer:
810 case DW_OP_GNU_implicit_pointer:
814 if (this->ref_addr_size == -1)
815 error (_("DWARF-2 expression error: DW_OP_implicit_pointer "
816 "is not allowed in frame context"));
818 /* The referred-to DIE of sect_offset kind. */
819 this->len = extract_unsigned_integer (op_ptr, this->ref_addr_size,
821 op_ptr += this->ref_addr_size;
823 /* The byte offset into the data. */
824 op_ptr = safe_read_sleb128 (op_ptr, op_end, &len);
825 result = (ULONGEST) len;
826 result_val = value_from_ulongest (address_type, result);
828 this->location = DWARF_VALUE_IMPLICIT_POINTER;
829 dwarf_expr_require_composition (op_ptr, op_end,
830 "DW_OP_implicit_pointer");
867 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
868 result = this->read_addr_from_reg (op - DW_OP_breg0);
870 result_val = value_from_ulongest (address_type, result);
875 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
876 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
877 result = this->read_addr_from_reg (reg);
879 result_val = value_from_ulongest (address_type, result);
884 const gdb_byte *datastart;
886 unsigned int before_stack_len;
888 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
889 /* Rather than create a whole new context, we simply
890 record the stack length before execution, then reset it
891 afterwards, effectively erasing whatever the recursive
893 before_stack_len = this->stack_len;
894 /* FIXME: cagney/2003-03-26: This code should be using
895 get_frame_base_address(), and then implement a dwarf2
896 specific this_base method. */
897 this->get_frame_base (&datastart, &datalen);
898 eval (datastart, datalen);
899 if (this->location == DWARF_VALUE_MEMORY)
900 result = fetch_address (0);
901 else if (this->location == DWARF_VALUE_REGISTER)
902 result = this->read_addr_from_reg (value_as_long (fetch (0)));
904 error (_("Not implemented: computing frame "
905 "base using explicit value operator"));
906 result = result + offset;
907 result_val = value_from_ulongest (address_type, result);
909 this->stack_len = before_stack_len;
910 this->location = DWARF_VALUE_MEMORY;
915 result_val = fetch (0);
916 in_stack_memory = fetch_in_stack_memory (0);
925 result_val = fetch (offset);
926 in_stack_memory = fetch_in_stack_memory (offset);
931 struct dwarf_stack_value t1, t2;
933 if (this->stack_len < 2)
934 error (_("Not enough elements for "
935 "DW_OP_swap. Need 2, have %d."),
937 t1 = this->stack[this->stack_len - 1];
938 t2 = this->stack[this->stack_len - 2];
939 this->stack[this->stack_len - 1] = t2;
940 this->stack[this->stack_len - 2] = t1;
945 result_val = fetch (1);
946 in_stack_memory = fetch_in_stack_memory (1);
951 struct dwarf_stack_value t1, t2, t3;
953 if (this->stack_len < 3)
954 error (_("Not enough elements for "
955 "DW_OP_rot. Need 3, have %d."),
957 t1 = this->stack[this->stack_len - 1];
958 t2 = this->stack[this->stack_len - 2];
959 t3 = this->stack[this->stack_len - 3];
960 this->stack[this->stack_len - 1] = t2;
961 this->stack[this->stack_len - 2] = t3;
962 this->stack[this->stack_len - 3] = t1;
967 case DW_OP_deref_size:
968 case DW_OP_deref_type:
969 case DW_OP_GNU_deref_type:
971 int addr_size = (op == DW_OP_deref ? this->addr_size : *op_ptr++);
972 gdb_byte *buf = (gdb_byte *) alloca (addr_size);
973 CORE_ADDR addr = fetch_address (0);
978 if (op == DW_OP_deref_type || op == DW_OP_GNU_deref_type)
980 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
981 cu_offset type_die_cu_off = (cu_offset) uoffset;
982 type = get_base_type (type_die_cu_off, 0);
987 this->read_mem (buf, addr, addr_size);
989 /* If the size of the object read from memory is different
990 from the type length, we need to zero-extend it. */
991 if (TYPE_LENGTH (type) != addr_size)
994 extract_unsigned_integer (buf, addr_size, byte_order);
996 buf = (gdb_byte *) alloca (TYPE_LENGTH (type));
997 store_unsigned_integer (buf, TYPE_LENGTH (type),
1001 result_val = value_from_contents_and_address (type, buf, addr);
1008 case DW_OP_plus_uconst:
1010 /* Unary operations. */
1011 result_val = fetch (0);
1017 if (value_less (result_val,
1018 value_zero (value_type (result_val), not_lval)))
1019 result_val = value_neg (result_val);
1022 result_val = value_neg (result_val);
1025 dwarf_require_integral (value_type (result_val));
1026 result_val = value_complement (result_val);
1028 case DW_OP_plus_uconst:
1029 dwarf_require_integral (value_type (result_val));
1030 result = value_as_long (result_val);
1031 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1033 result_val = value_from_ulongest (address_type, result);
1057 /* Binary operations. */
1058 struct value *first, *second;
1066 if (! base_types_equal_p (value_type (first), value_type (second)))
1067 error (_("Incompatible types on DWARF stack"));
1072 dwarf_require_integral (value_type (first));
1073 dwarf_require_integral (value_type (second));
1074 result_val = value_binop (first, second, BINOP_BITWISE_AND);
1077 result_val = value_binop (first, second, BINOP_DIV);
1080 result_val = value_binop (first, second, BINOP_SUB);
1085 struct type *orig_type = value_type (first);
1087 /* We have to special-case "old-style" untyped values
1088 -- these must have mod computed using unsigned
1090 if (orig_type == address_type)
1093 = get_unsigned_type (this->gdbarch, orig_type);
1096 first = value_cast (utype, first);
1097 second = value_cast (utype, second);
1099 /* Note that value_binop doesn't handle float or
1100 decimal float here. This seems unimportant. */
1101 result_val = value_binop (first, second, BINOP_MOD);
1103 result_val = value_cast (orig_type, result_val);
1107 result_val = value_binop (first, second, BINOP_MUL);
1110 dwarf_require_integral (value_type (first));
1111 dwarf_require_integral (value_type (second));
1112 result_val = value_binop (first, second, BINOP_BITWISE_IOR);
1115 result_val = value_binop (first, second, BINOP_ADD);
1118 dwarf_require_integral (value_type (first));
1119 dwarf_require_integral (value_type (second));
1120 result_val = value_binop (first, second, BINOP_LSH);
1123 dwarf_require_integral (value_type (first));
1124 dwarf_require_integral (value_type (second));
1125 if (!TYPE_UNSIGNED (value_type (first)))
1128 = get_unsigned_type (this->gdbarch, value_type (first));
1130 first = value_cast (utype, first);
1133 result_val = value_binop (first, second, BINOP_RSH);
1134 /* Make sure we wind up with the same type we started
1136 if (value_type (result_val) != value_type (second))
1137 result_val = value_cast (value_type (second), result_val);
1140 dwarf_require_integral (value_type (first));
1141 dwarf_require_integral (value_type (second));
1142 if (TYPE_UNSIGNED (value_type (first)))
1145 = get_signed_type (this->gdbarch, value_type (first));
1147 first = value_cast (stype, first);
1150 result_val = value_binop (first, second, BINOP_RSH);
1151 /* Make sure we wind up with the same type we started
1153 if (value_type (result_val) != value_type (second))
1154 result_val = value_cast (value_type (second), result_val);
1157 dwarf_require_integral (value_type (first));
1158 dwarf_require_integral (value_type (second));
1159 result_val = value_binop (first, second, BINOP_BITWISE_XOR);
1162 /* A <= B is !(B < A). */
1163 result = ! value_less (second, first);
1164 result_val = value_from_ulongest (address_type, result);
1167 /* A >= B is !(A < B). */
1168 result = ! value_less (first, second);
1169 result_val = value_from_ulongest (address_type, result);
1172 result = value_equal (first, second);
1173 result_val = value_from_ulongest (address_type, result);
1176 result = value_less (first, second);
1177 result_val = value_from_ulongest (address_type, result);
1180 /* A > B is B < A. */
1181 result = value_less (second, first);
1182 result_val = value_from_ulongest (address_type, result);
1185 result = ! value_equal (first, second);
1186 result_val = value_from_ulongest (address_type, result);
1189 internal_error (__FILE__, __LINE__,
1190 _("Can't be reached."));
1195 case DW_OP_call_frame_cfa:
1196 result = this->get_frame_cfa ();
1197 result_val = value_from_ulongest (address_type, result);
1198 in_stack_memory = 1;
1201 case DW_OP_GNU_push_tls_address:
1202 case DW_OP_form_tls_address:
1203 /* Variable is at a constant offset in the thread-local
1204 storage block into the objfile for the current thread and
1205 the dynamic linker module containing this expression. Here
1206 we return returns the offset from that base. The top of the
1207 stack has the offset from the beginning of the thread
1208 control block at which the variable is located. Nothing
1209 should follow this operator, so the top of stack would be
1211 result = value_as_long (fetch (0));
1213 result = this->get_tls_address (result);
1214 result_val = value_from_ulongest (address_type, result);
1218 offset = extract_signed_integer (op_ptr, 2, byte_order);
1227 offset = extract_signed_integer (op_ptr, 2, byte_order);
1230 dwarf_require_integral (value_type (val));
1231 if (value_as_long (val) != 0)
1244 /* Record the piece. */
1245 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1246 add_piece (8 * size, 0);
1248 /* Pop off the address/regnum, and reset the location
1250 if (this->location != DWARF_VALUE_LITERAL
1251 && this->location != DWARF_VALUE_OPTIMIZED_OUT)
1253 this->location = DWARF_VALUE_MEMORY;
1257 case DW_OP_bit_piece:
1259 uint64_t size, offset;
1261 /* Record the piece. */
1262 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1263 op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
1264 add_piece (size, offset);
1266 /* Pop off the address/regnum, and reset the location
1268 if (this->location != DWARF_VALUE_LITERAL
1269 && this->location != DWARF_VALUE_OPTIMIZED_OUT)
1271 this->location = DWARF_VALUE_MEMORY;
1275 case DW_OP_GNU_uninit:
1276 if (op_ptr != op_end)
1277 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
1278 "be the very last op."));
1280 this->initialized = 0;
1286 = (cu_offset) extract_unsigned_integer (op_ptr, 2, byte_order);
1288 this->dwarf_call (cu_off);
1295 = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order);
1297 this->dwarf_call (cu_off);
1301 case DW_OP_entry_value:
1302 case DW_OP_GNU_entry_value:
1305 CORE_ADDR deref_size;
1306 union call_site_parameter_u kind_u;
1308 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
1309 if (op_ptr + len > op_end)
1310 error (_("DW_OP_entry_value: too few bytes available."));
1312 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len);
1313 if (kind_u.dwarf_reg != -1)
1316 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG,
1318 -1 /* deref_size */);
1322 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr,
1325 if (kind_u.dwarf_reg != -1)
1327 if (deref_size == -1)
1328 deref_size = this->addr_size;
1330 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG,
1331 kind_u, deref_size);
1335 error (_("DWARF-2 expression error: DW_OP_entry_value is "
1336 "supported only for single DW_OP_reg* "
1337 "or for DW_OP_breg*(0)+DW_OP_deref*"));
1340 case DW_OP_GNU_parameter_ref:
1342 union call_site_parameter_u kind_u;
1345 = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order);
1347 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_PARAM_OFFSET,
1349 -1 /* deref_size */);
1353 case DW_OP_const_type:
1354 case DW_OP_GNU_const_type:
1357 const gdb_byte *data;
1360 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1361 cu_offset type_die_cu_off = (cu_offset) uoffset;
1367 type = get_base_type (type_die_cu_off, n);
1368 result_val = value_from_contents (type, data);
1372 case DW_OP_regval_type:
1373 case DW_OP_GNU_regval_type:
1377 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1378 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1379 cu_offset type_die_cu_off = (cu_offset) uoffset;
1381 type = get_base_type (type_die_cu_off, 0);
1382 result_val = this->get_reg_value (type, reg);
1387 case DW_OP_GNU_convert:
1388 case DW_OP_reinterpret:
1389 case DW_OP_GNU_reinterpret:
1393 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1394 cu_offset type_die_cu_off = (cu_offset) uoffset;
1396 if (to_underlying (type_die_cu_off) == 0)
1397 type = address_type;
1399 type = get_base_type (type_die_cu_off, 0);
1401 result_val = fetch (0);
1404 if (op == DW_OP_convert || op == DW_OP_GNU_convert)
1405 result_val = value_cast (type, result_val);
1406 else if (type == value_type (result_val))
1410 else if (TYPE_LENGTH (type)
1411 != TYPE_LENGTH (value_type (result_val)))
1412 error (_("DW_OP_reinterpret has wrong size"));
1415 = value_from_contents (type,
1416 value_contents_all (result_val));
1420 case DW_OP_push_object_address:
1421 /* Return the address of the object we are currently observing. */
1422 result = this->get_object_address ();
1423 result_val = value_from_ulongest (address_type, result);
1427 error (_("Unhandled dwarf expression opcode 0x%x"), op);
1430 /* Most things push a result value. */
1431 gdb_assert (result_val != NULL);
1432 push (result_val, in_stack_memory);
1437 /* To simplify our main caller, if the result is an implicit
1438 pointer, then make a pieced value. This is ok because we can't
1439 have implicit pointers in contexts where pieces are invalid. */
1440 if (this->location == DWARF_VALUE_IMPLICIT_POINTER)
1441 add_piece (8 * this->addr_size, 0);
1444 this->recursion_depth--;
1445 gdb_assert (this->recursion_depth >= 0);
1449 _initialize_dwarf2expr (void)
1452 = gdbarch_data_register_post_init (dwarf_gdbarch_types_init);