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 "gdbsupport/underlying.h"
33 /* Cookie for gdbarch data. */
35 static struct gdbarch_data
*dwarf_arch_cookie
;
37 /* This holds gdbarch-specific types used by the DWARF expression
38 evaluator. See comments in execute_stack_op. */
40 struct dwarf_gdbarch_types
42 struct type
*dw_types
[3];
45 /* Allocate and fill in dwarf_gdbarch_types for an arch. */
48 dwarf_gdbarch_types_init (struct gdbarch
*gdbarch
)
50 struct dwarf_gdbarch_types
*types
51 = GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct dwarf_gdbarch_types
);
53 /* The types themselves are lazily initialized. */
58 /* Return the type used for DWARF operations where the type is
59 unspecified in the DWARF spec. Only certain sizes are
63 dwarf_expr_context::address_type () const
65 struct dwarf_gdbarch_types
*types
66 = (struct dwarf_gdbarch_types
*) gdbarch_data (this->gdbarch
,
70 if (this->addr_size
== 2)
72 else if (this->addr_size
== 4)
74 else if (this->addr_size
== 8)
77 error (_("Unsupported address size in DWARF expressions: %d bits"),
80 if (types
->dw_types
[ndx
] == NULL
)
82 = arch_integer_type (this->gdbarch
,
84 0, "<signed DWARF address type>");
86 return types
->dw_types
[ndx
];
89 /* Create a new context for the expression evaluator. */
91 dwarf_expr_context::dwarf_expr_context ()
97 max_recursion_depth (0x100),
98 location (DWARF_VALUE_MEMORY
),
105 /* Push VALUE onto the stack. */
108 dwarf_expr_context::push (struct value
*value
, bool in_stack_memory
)
110 stack
.emplace_back (value
, in_stack_memory
);
113 /* Push VALUE onto the stack. */
116 dwarf_expr_context::push_address (CORE_ADDR value
, bool in_stack_memory
)
118 push (value_from_ulongest (address_type (), value
), in_stack_memory
);
121 /* Pop the top item off of the stack. */
124 dwarf_expr_context::pop ()
127 error (_("dwarf expression stack underflow"));
132 /* Retrieve the N'th item on the stack. */
135 dwarf_expr_context::fetch (int n
)
137 if (stack
.size () <= n
)
138 error (_("Asked for position %d of stack, "
139 "stack only has %zu elements on it."),
141 return stack
[stack
.size () - (1 + n
)].value
;
144 /* Require that TYPE be an integral type; throw an exception if not. */
147 dwarf_require_integral (struct type
*type
)
149 if (TYPE_CODE (type
) != TYPE_CODE_INT
150 && TYPE_CODE (type
) != TYPE_CODE_CHAR
151 && TYPE_CODE (type
) != TYPE_CODE_BOOL
)
152 error (_("integral type expected in DWARF expression"));
155 /* Return the unsigned form of TYPE. TYPE is necessarily an integral
159 get_unsigned_type (struct gdbarch
*gdbarch
, struct type
*type
)
161 switch (TYPE_LENGTH (type
))
164 return builtin_type (gdbarch
)->builtin_uint8
;
166 return builtin_type (gdbarch
)->builtin_uint16
;
168 return builtin_type (gdbarch
)->builtin_uint32
;
170 return builtin_type (gdbarch
)->builtin_uint64
;
172 error (_("no unsigned variant found for type, while evaluating "
173 "DWARF expression"));
177 /* Return the signed form of TYPE. TYPE is necessarily an integral
181 get_signed_type (struct gdbarch
*gdbarch
, struct type
*type
)
183 switch (TYPE_LENGTH (type
))
186 return builtin_type (gdbarch
)->builtin_int8
;
188 return builtin_type (gdbarch
)->builtin_int16
;
190 return builtin_type (gdbarch
)->builtin_int32
;
192 return builtin_type (gdbarch
)->builtin_int64
;
194 error (_("no signed variant found for type, while evaluating "
195 "DWARF expression"));
199 /* Retrieve the N'th item on the stack, converted to an address. */
202 dwarf_expr_context::fetch_address (int n
)
204 struct value
*result_val
= fetch (n
);
205 enum bfd_endian byte_order
= gdbarch_byte_order (this->gdbarch
);
208 dwarf_require_integral (value_type (result_val
));
209 result
= extract_unsigned_integer (value_contents (result_val
),
210 TYPE_LENGTH (value_type (result_val
)),
213 /* For most architectures, calling extract_unsigned_integer() alone
214 is sufficient for extracting an address. However, some
215 architectures (e.g. MIPS) use signed addresses and using
216 extract_unsigned_integer() will not produce a correct
217 result. Make sure we invoke gdbarch_integer_to_address()
218 for those architectures which require it. */
219 if (gdbarch_integer_to_address_p (this->gdbarch
))
221 gdb_byte
*buf
= (gdb_byte
*) alloca (this->addr_size
);
222 struct type
*int_type
= get_unsigned_type (this->gdbarch
,
223 value_type (result_val
));
225 store_unsigned_integer (buf
, this->addr_size
, byte_order
, result
);
226 return gdbarch_integer_to_address (this->gdbarch
, int_type
, buf
);
229 return (CORE_ADDR
) result
;
232 /* Retrieve the in_stack_memory flag of the N'th item on the stack. */
235 dwarf_expr_context::fetch_in_stack_memory (int n
)
237 if (stack
.size () <= n
)
238 error (_("Asked for position %d of stack, "
239 "stack only has %zu elements on it."),
241 return stack
[stack
.size () - (1 + n
)].in_stack_memory
;
244 /* Return true if the expression stack is empty. */
247 dwarf_expr_context::stack_empty_p () const
249 return stack
.empty ();
252 /* Add a new piece to the dwarf_expr_context's piece list. */
254 dwarf_expr_context::add_piece (ULONGEST size
, ULONGEST offset
)
256 this->pieces
.emplace_back ();
257 dwarf_expr_piece
&p
= this->pieces
.back ();
259 p
.location
= this->location
;
263 if (p
.location
== DWARF_VALUE_LITERAL
)
265 p
.v
.literal
.data
= this->data
;
266 p
.v
.literal
.length
= this->len
;
268 else if (stack_empty_p ())
270 p
.location
= DWARF_VALUE_OPTIMIZED_OUT
;
271 /* Also reset the context's location, for our callers. This is
272 a somewhat strange approach, but this lets us avoid setting
273 the location to DWARF_VALUE_MEMORY in all the individual
274 cases in the evaluator. */
275 this->location
= DWARF_VALUE_OPTIMIZED_OUT
;
277 else if (p
.location
== DWARF_VALUE_MEMORY
)
279 p
.v
.mem
.addr
= fetch_address (0);
280 p
.v
.mem
.in_stack_memory
= fetch_in_stack_memory (0);
282 else if (p
.location
== DWARF_VALUE_IMPLICIT_POINTER
)
284 p
.v
.ptr
.die_sect_off
= (sect_offset
) this->len
;
285 p
.v
.ptr
.offset
= value_as_long (fetch (0));
287 else if (p
.location
== DWARF_VALUE_REGISTER
)
288 p
.v
.regno
= value_as_long (fetch (0));
291 p
.v
.value
= fetch (0);
295 /* Evaluate the expression at ADDR (LEN bytes long). */
298 dwarf_expr_context::eval (const gdb_byte
*addr
, size_t len
)
300 int old_recursion_depth
= this->recursion_depth
;
302 execute_stack_op (addr
, addr
+ len
);
304 /* RECURSION_DEPTH becomes invalid if an exception was thrown here. */
306 gdb_assert (this->recursion_depth
== old_recursion_depth
);
309 /* Helper to read a uleb128 value or throw an error. */
312 safe_read_uleb128 (const gdb_byte
*buf
, const gdb_byte
*buf_end
,
315 buf
= gdb_read_uleb128 (buf
, buf_end
, r
);
317 error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
321 /* Helper to read a sleb128 value or throw an error. */
324 safe_read_sleb128 (const gdb_byte
*buf
, const gdb_byte
*buf_end
,
327 buf
= gdb_read_sleb128 (buf
, buf_end
, r
);
329 error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
334 safe_skip_leb128 (const gdb_byte
*buf
, const gdb_byte
*buf_end
)
336 buf
= gdb_skip_leb128 (buf
, buf_end
);
338 error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
343 /* Check that the current operator is either at the end of an
344 expression, or that it is followed by a composition operator or by
345 DW_OP_GNU_uninit (which should terminate the expression). */
348 dwarf_expr_require_composition (const gdb_byte
*op_ptr
, const gdb_byte
*op_end
,
351 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
&& *op_ptr
!= DW_OP_bit_piece
352 && *op_ptr
!= DW_OP_GNU_uninit
)
353 error (_("DWARF-2 expression error: `%s' operations must be "
354 "used either alone or in conjunction with DW_OP_piece "
355 "or DW_OP_bit_piece."),
359 /* Return true iff the types T1 and T2 are "the same". This only does
360 checks that might reasonably be needed to compare DWARF base
364 base_types_equal_p (struct type
*t1
, struct type
*t2
)
366 if (TYPE_CODE (t1
) != TYPE_CODE (t2
))
368 if (TYPE_UNSIGNED (t1
) != TYPE_UNSIGNED (t2
))
370 return TYPE_LENGTH (t1
) == TYPE_LENGTH (t2
);
373 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
374 DWARF register number. Otherwise return -1. */
377 dwarf_block_to_dwarf_reg (const gdb_byte
*buf
, const gdb_byte
*buf_end
)
383 if (*buf
>= DW_OP_reg0
&& *buf
<= DW_OP_reg31
)
385 if (buf_end
- buf
!= 1)
387 return *buf
- DW_OP_reg0
;
390 if (*buf
== DW_OP_regval_type
|| *buf
== DW_OP_GNU_regval_type
)
393 buf
= gdb_read_uleb128 (buf
, buf_end
, &dwarf_reg
);
396 buf
= gdb_skip_leb128 (buf
, buf_end
);
400 else if (*buf
== DW_OP_regx
)
403 buf
= gdb_read_uleb128 (buf
, buf_end
, &dwarf_reg
);
409 if (buf
!= buf_end
|| (int) dwarf_reg
!= dwarf_reg
)
414 /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
415 DW_OP_deref* return the DWARF register number. Otherwise return -1.
416 DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
417 size from DW_OP_deref_size. */
420 dwarf_block_to_dwarf_reg_deref (const gdb_byte
*buf
, const gdb_byte
*buf_end
,
421 CORE_ADDR
*deref_size_return
)
429 if (*buf
>= DW_OP_breg0
&& *buf
<= DW_OP_breg31
)
431 dwarf_reg
= *buf
- DW_OP_breg0
;
436 else if (*buf
== DW_OP_bregx
)
439 buf
= gdb_read_uleb128 (buf
, buf_end
, &dwarf_reg
);
442 if ((int) dwarf_reg
!= dwarf_reg
)
448 buf
= gdb_read_sleb128 (buf
, buf_end
, &offset
);
454 if (*buf
== DW_OP_deref
)
457 *deref_size_return
= -1;
459 else if (*buf
== DW_OP_deref_size
)
464 *deref_size_return
= *buf
++;
475 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
476 in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */
479 dwarf_block_to_fb_offset (const gdb_byte
*buf
, const gdb_byte
*buf_end
,
480 CORE_ADDR
*fb_offset_return
)
487 if (*buf
!= DW_OP_fbreg
)
491 buf
= gdb_read_sleb128 (buf
, buf_end
, &fb_offset
);
494 *fb_offset_return
= fb_offset
;
495 if (buf
!= buf_end
|| fb_offset
!= (LONGEST
) *fb_offset_return
)
501 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
502 in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0.
503 The matched SP register number depends on GDBARCH. */
506 dwarf_block_to_sp_offset (struct gdbarch
*gdbarch
, const gdb_byte
*buf
,
507 const gdb_byte
*buf_end
, CORE_ADDR
*sp_offset_return
)
514 if (*buf
>= DW_OP_breg0
&& *buf
<= DW_OP_breg31
)
516 dwarf_reg
= *buf
- DW_OP_breg0
;
521 if (*buf
!= DW_OP_bregx
)
524 buf
= gdb_read_uleb128 (buf
, buf_end
, &dwarf_reg
);
529 if (dwarf_reg_to_regnum (gdbarch
, dwarf_reg
)
530 != gdbarch_sp_regnum (gdbarch
))
533 buf
= gdb_read_sleb128 (buf
, buf_end
, &sp_offset
);
536 *sp_offset_return
= sp_offset
;
537 if (buf
!= buf_end
|| sp_offset
!= (LONGEST
) *sp_offset_return
)
543 /* The engine for the expression evaluator. Using the context in this
544 object, evaluate the expression between OP_PTR and OP_END. */
547 dwarf_expr_context::execute_stack_op (const gdb_byte
*op_ptr
,
548 const gdb_byte
*op_end
)
550 enum bfd_endian byte_order
= gdbarch_byte_order (this->gdbarch
);
551 /* Old-style "untyped" DWARF values need special treatment in a
552 couple of places, specifically DW_OP_mod and DW_OP_shr. We need
553 a special type for these values so we can distinguish them from
554 values that have an explicit type, because explicitly-typed
555 values do not need special treatment. This special type must be
556 different (in the `==' sense) from any base type coming from the
558 struct type
*address_type
= this->address_type ();
560 this->location
= DWARF_VALUE_MEMORY
;
561 this->initialized
= 1; /* Default is initialized. */
563 if (this->recursion_depth
> this->max_recursion_depth
)
564 error (_("DWARF-2 expression error: Loop detected (%d)."),
565 this->recursion_depth
);
566 this->recursion_depth
++;
568 while (op_ptr
< op_end
)
570 enum dwarf_location_atom op
= (enum dwarf_location_atom
) *op_ptr
++;
572 /* Assume the value is not in stack memory.
573 Code that knows otherwise sets this to true.
574 Some arithmetic on stack addresses can probably be assumed to still
575 be a stack address, but we skip this complication for now.
576 This is just an optimization, so it's always ok to punt
577 and leave this as false. */
578 bool in_stack_memory
= false;
579 uint64_t uoffset
, reg
;
581 struct value
*result_val
= NULL
;
583 /* The DWARF expression might have a bug causing an infinite
584 loop. In that case, quitting is the only way out. */
621 result
= op
- DW_OP_lit0
;
622 result_val
= value_from_ulongest (address_type
, result
);
626 result
= extract_unsigned_integer (op_ptr
,
627 this->addr_size
, byte_order
);
628 op_ptr
+= this->addr_size
;
629 /* Some versions of GCC emit DW_OP_addr before
630 DW_OP_GNU_push_tls_address. In this case the value is an
631 index, not an address. We don't support things like
632 branching between the address and the TLS op. */
633 if (op_ptr
>= op_end
|| *op_ptr
!= DW_OP_GNU_push_tls_address
)
634 result
+= this->offset
;
635 result_val
= value_from_ulongest (address_type
, result
);
639 case DW_OP_GNU_addr_index
:
640 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
641 result
= this->get_addr_index (uoffset
);
642 result
+= this->offset
;
643 result_val
= value_from_ulongest (address_type
, result
);
645 case DW_OP_GNU_const_index
:
646 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
647 result
= this->get_addr_index (uoffset
);
648 result_val
= value_from_ulongest (address_type
, result
);
652 result
= extract_unsigned_integer (op_ptr
, 1, byte_order
);
653 result_val
= value_from_ulongest (address_type
, result
);
657 result
= extract_signed_integer (op_ptr
, 1, byte_order
);
658 result_val
= value_from_ulongest (address_type
, result
);
662 result
= extract_unsigned_integer (op_ptr
, 2, byte_order
);
663 result_val
= value_from_ulongest (address_type
, result
);
667 result
= extract_signed_integer (op_ptr
, 2, byte_order
);
668 result_val
= value_from_ulongest (address_type
, result
);
672 result
= extract_unsigned_integer (op_ptr
, 4, byte_order
);
673 result_val
= value_from_ulongest (address_type
, result
);
677 result
= extract_signed_integer (op_ptr
, 4, byte_order
);
678 result_val
= value_from_ulongest (address_type
, result
);
682 result
= extract_unsigned_integer (op_ptr
, 8, byte_order
);
683 result_val
= value_from_ulongest (address_type
, result
);
687 result
= extract_signed_integer (op_ptr
, 8, byte_order
);
688 result_val
= value_from_ulongest (address_type
, result
);
692 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
694 result_val
= value_from_ulongest (address_type
, result
);
697 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
699 result_val
= value_from_ulongest (address_type
, result
);
702 /* The DW_OP_reg operations are required to occur alone in
703 location expressions. */
736 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_reg");
738 result
= op
- DW_OP_reg0
;
739 result_val
= value_from_ulongest (address_type
, result
);
740 this->location
= DWARF_VALUE_REGISTER
;
744 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
745 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
748 result_val
= value_from_ulongest (address_type
, result
);
749 this->location
= DWARF_VALUE_REGISTER
;
752 case DW_OP_implicit_value
:
756 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &len
);
757 if (op_ptr
+ len
> op_end
)
758 error (_("DW_OP_implicit_value: too few bytes available."));
761 this->location
= DWARF_VALUE_LITERAL
;
763 dwarf_expr_require_composition (op_ptr
, op_end
,
764 "DW_OP_implicit_value");
768 case DW_OP_stack_value
:
769 this->location
= DWARF_VALUE_STACK
;
770 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_stack_value");
773 case DW_OP_implicit_pointer
:
774 case DW_OP_GNU_implicit_pointer
:
778 if (this->ref_addr_size
== -1)
779 error (_("DWARF-2 expression error: DW_OP_implicit_pointer "
780 "is not allowed in frame context"));
782 /* The referred-to DIE of sect_offset kind. */
783 this->len
= extract_unsigned_integer (op_ptr
, this->ref_addr_size
,
785 op_ptr
+= this->ref_addr_size
;
787 /* The byte offset into the data. */
788 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &len
);
789 result
= (ULONGEST
) len
;
790 result_val
= value_from_ulongest (address_type
, result
);
792 this->location
= DWARF_VALUE_IMPLICIT_POINTER
;
793 dwarf_expr_require_composition (op_ptr
, op_end
,
794 "DW_OP_implicit_pointer");
831 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
832 result
= this->read_addr_from_reg (op
- DW_OP_breg0
);
834 result_val
= value_from_ulongest (address_type
, result
);
839 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
840 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
841 result
= this->read_addr_from_reg (reg
);
843 result_val
= value_from_ulongest (address_type
, result
);
848 const gdb_byte
*datastart
;
851 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
853 /* Rather than create a whole new context, we simply
854 backup the current stack locally and install a new empty stack,
855 then reset it afterwards, effectively erasing whatever the
856 recursive call put there. */
857 std::vector
<dwarf_stack_value
> saved_stack
= std::move (stack
);
860 /* FIXME: cagney/2003-03-26: This code should be using
861 get_frame_base_address(), and then implement a dwarf2
862 specific this_base method. */
863 this->get_frame_base (&datastart
, &datalen
);
864 eval (datastart
, datalen
);
865 if (this->location
== DWARF_VALUE_MEMORY
)
866 result
= fetch_address (0);
867 else if (this->location
== DWARF_VALUE_REGISTER
)
868 result
= this->read_addr_from_reg (value_as_long (fetch (0)));
870 error (_("Not implemented: computing frame "
871 "base using explicit value operator"));
872 result
= result
+ offset
;
873 result_val
= value_from_ulongest (address_type
, result
);
874 in_stack_memory
= true;
876 /* Restore the content of the original stack. */
877 stack
= std::move (saved_stack
);
879 this->location
= DWARF_VALUE_MEMORY
;
884 result_val
= fetch (0);
885 in_stack_memory
= fetch_in_stack_memory (0);
894 result_val
= fetch (offset
);
895 in_stack_memory
= fetch_in_stack_memory (offset
);
900 if (stack
.size () < 2)
901 error (_("Not enough elements for "
902 "DW_OP_swap. Need 2, have %zu."),
905 dwarf_stack_value
&t1
= stack
[stack
.size () - 1];
906 dwarf_stack_value
&t2
= stack
[stack
.size () - 2];
912 result_val
= fetch (1);
913 in_stack_memory
= fetch_in_stack_memory (1);
918 if (stack
.size () < 3)
919 error (_("Not enough elements for "
920 "DW_OP_rot. Need 3, have %zu."),
923 dwarf_stack_value temp
= stack
[stack
.size () - 1];
924 stack
[stack
.size () - 1] = stack
[stack
.size () - 2];
925 stack
[stack
.size () - 2] = stack
[stack
.size () - 3];
926 stack
[stack
.size () - 3] = temp
;
931 case DW_OP_deref_size
:
932 case DW_OP_deref_type
:
933 case DW_OP_GNU_deref_type
:
935 int addr_size
= (op
== DW_OP_deref
? this->addr_size
: *op_ptr
++);
936 gdb_byte
*buf
= (gdb_byte
*) alloca (addr_size
);
937 CORE_ADDR addr
= fetch_address (0);
942 if (op
== DW_OP_deref_type
|| op
== DW_OP_GNU_deref_type
)
944 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
945 cu_offset type_die_cu_off
= (cu_offset
) uoffset
;
946 type
= get_base_type (type_die_cu_off
, 0);
951 this->read_mem (buf
, addr
, addr_size
);
953 /* If the size of the object read from memory is different
954 from the type length, we need to zero-extend it. */
955 if (TYPE_LENGTH (type
) != addr_size
)
958 extract_unsigned_integer (buf
, addr_size
, byte_order
);
960 buf
= (gdb_byte
*) alloca (TYPE_LENGTH (type
));
961 store_unsigned_integer (buf
, TYPE_LENGTH (type
),
965 result_val
= value_from_contents_and_address (type
, buf
, addr
);
972 case DW_OP_plus_uconst
:
974 /* Unary operations. */
975 result_val
= fetch (0);
981 if (value_less (result_val
,
982 value_zero (value_type (result_val
), not_lval
)))
983 result_val
= value_neg (result_val
);
986 result_val
= value_neg (result_val
);
989 dwarf_require_integral (value_type (result_val
));
990 result_val
= value_complement (result_val
);
992 case DW_OP_plus_uconst
:
993 dwarf_require_integral (value_type (result_val
));
994 result
= value_as_long (result_val
);
995 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
997 result_val
= value_from_ulongest (address_type
, result
);
1021 /* Binary operations. */
1022 struct value
*first
, *second
;
1030 if (! base_types_equal_p (value_type (first
), value_type (second
)))
1031 error (_("Incompatible types on DWARF stack"));
1036 dwarf_require_integral (value_type (first
));
1037 dwarf_require_integral (value_type (second
));
1038 result_val
= value_binop (first
, second
, BINOP_BITWISE_AND
);
1041 result_val
= value_binop (first
, second
, BINOP_DIV
);
1044 result_val
= value_binop (first
, second
, BINOP_SUB
);
1049 struct type
*orig_type
= value_type (first
);
1051 /* We have to special-case "old-style" untyped values
1052 -- these must have mod computed using unsigned
1054 if (orig_type
== address_type
)
1057 = get_unsigned_type (this->gdbarch
, orig_type
);
1060 first
= value_cast (utype
, first
);
1061 second
= value_cast (utype
, second
);
1063 /* Note that value_binop doesn't handle float or
1064 decimal float here. This seems unimportant. */
1065 result_val
= value_binop (first
, second
, BINOP_MOD
);
1067 result_val
= value_cast (orig_type
, result_val
);
1071 result_val
= value_binop (first
, second
, BINOP_MUL
);
1074 dwarf_require_integral (value_type (first
));
1075 dwarf_require_integral (value_type (second
));
1076 result_val
= value_binop (first
, second
, BINOP_BITWISE_IOR
);
1079 result_val
= value_binop (first
, second
, BINOP_ADD
);
1082 dwarf_require_integral (value_type (first
));
1083 dwarf_require_integral (value_type (second
));
1084 result_val
= value_binop (first
, second
, BINOP_LSH
);
1087 dwarf_require_integral (value_type (first
));
1088 dwarf_require_integral (value_type (second
));
1089 if (!TYPE_UNSIGNED (value_type (first
)))
1092 = get_unsigned_type (this->gdbarch
, value_type (first
));
1094 first
= value_cast (utype
, first
);
1097 result_val
= value_binop (first
, second
, BINOP_RSH
);
1098 /* Make sure we wind up with the same type we started
1100 if (value_type (result_val
) != value_type (second
))
1101 result_val
= value_cast (value_type (second
), result_val
);
1104 dwarf_require_integral (value_type (first
));
1105 dwarf_require_integral (value_type (second
));
1106 if (TYPE_UNSIGNED (value_type (first
)))
1109 = get_signed_type (this->gdbarch
, value_type (first
));
1111 first
= value_cast (stype
, first
);
1114 result_val
= value_binop (first
, second
, BINOP_RSH
);
1115 /* Make sure we wind up with the same type we started
1117 if (value_type (result_val
) != value_type (second
))
1118 result_val
= value_cast (value_type (second
), result_val
);
1121 dwarf_require_integral (value_type (first
));
1122 dwarf_require_integral (value_type (second
));
1123 result_val
= value_binop (first
, second
, BINOP_BITWISE_XOR
);
1126 /* A <= B is !(B < A). */
1127 result
= ! value_less (second
, first
);
1128 result_val
= value_from_ulongest (address_type
, result
);
1131 /* A >= B is !(A < B). */
1132 result
= ! value_less (first
, second
);
1133 result_val
= value_from_ulongest (address_type
, result
);
1136 result
= value_equal (first
, second
);
1137 result_val
= value_from_ulongest (address_type
, result
);
1140 result
= value_less (first
, second
);
1141 result_val
= value_from_ulongest (address_type
, result
);
1144 /* A > B is B < A. */
1145 result
= value_less (second
, first
);
1146 result_val
= value_from_ulongest (address_type
, result
);
1149 result
= ! value_equal (first
, second
);
1150 result_val
= value_from_ulongest (address_type
, result
);
1153 internal_error (__FILE__
, __LINE__
,
1154 _("Can't be reached."));
1159 case DW_OP_call_frame_cfa
:
1160 result
= this->get_frame_cfa ();
1161 result_val
= value_from_ulongest (address_type
, result
);
1162 in_stack_memory
= true;
1165 case DW_OP_GNU_push_tls_address
:
1166 case DW_OP_form_tls_address
:
1167 /* Variable is at a constant offset in the thread-local
1168 storage block into the objfile for the current thread and
1169 the dynamic linker module containing this expression. Here
1170 we return returns the offset from that base. The top of the
1171 stack has the offset from the beginning of the thread
1172 control block at which the variable is located. Nothing
1173 should follow this operator, so the top of stack would be
1175 result
= value_as_long (fetch (0));
1177 result
= this->get_tls_address (result
);
1178 result_val
= value_from_ulongest (address_type
, result
);
1182 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
1191 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
1194 dwarf_require_integral (value_type (val
));
1195 if (value_as_long (val
) != 0)
1208 /* Record the piece. */
1209 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &size
);
1210 add_piece (8 * size
, 0);
1212 /* Pop off the address/regnum, and reset the location
1214 if (this->location
!= DWARF_VALUE_LITERAL
1215 && this->location
!= DWARF_VALUE_OPTIMIZED_OUT
)
1217 this->location
= DWARF_VALUE_MEMORY
;
1221 case DW_OP_bit_piece
:
1223 uint64_t size
, uleb_offset
;
1225 /* Record the piece. */
1226 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &size
);
1227 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uleb_offset
);
1228 add_piece (size
, uleb_offset
);
1230 /* Pop off the address/regnum, and reset the location
1232 if (this->location
!= DWARF_VALUE_LITERAL
1233 && this->location
!= DWARF_VALUE_OPTIMIZED_OUT
)
1235 this->location
= DWARF_VALUE_MEMORY
;
1239 case DW_OP_GNU_uninit
:
1240 if (op_ptr
!= op_end
)
1241 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
1242 "be the very last op."));
1244 this->initialized
= 0;
1250 = (cu_offset
) extract_unsigned_integer (op_ptr
, 2, byte_order
);
1252 this->dwarf_call (cu_off
);
1259 = (cu_offset
) extract_unsigned_integer (op_ptr
, 4, byte_order
);
1261 this->dwarf_call (cu_off
);
1265 case DW_OP_GNU_variable_value
:
1267 sect_offset sect_off
1268 = (sect_offset
) extract_unsigned_integer (op_ptr
,
1269 this->ref_addr_size
,
1271 op_ptr
+= this->ref_addr_size
;
1272 result_val
= this->dwarf_variable_value (sect_off
);
1276 case DW_OP_entry_value
:
1277 case DW_OP_GNU_entry_value
:
1280 CORE_ADDR deref_size
;
1281 union call_site_parameter_u kind_u
;
1283 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &len
);
1284 if (op_ptr
+ len
> op_end
)
1285 error (_("DW_OP_entry_value: too few bytes available."));
1287 kind_u
.dwarf_reg
= dwarf_block_to_dwarf_reg (op_ptr
, op_ptr
+ len
);
1288 if (kind_u
.dwarf_reg
!= -1)
1291 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG
,
1293 -1 /* deref_size */);
1297 kind_u
.dwarf_reg
= dwarf_block_to_dwarf_reg_deref (op_ptr
,
1300 if (kind_u
.dwarf_reg
!= -1)
1302 if (deref_size
== -1)
1303 deref_size
= this->addr_size
;
1305 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG
,
1306 kind_u
, deref_size
);
1310 error (_("DWARF-2 expression error: DW_OP_entry_value is "
1311 "supported only for single DW_OP_reg* "
1312 "or for DW_OP_breg*(0)+DW_OP_deref*"));
1315 case DW_OP_GNU_parameter_ref
:
1317 union call_site_parameter_u kind_u
;
1320 = (cu_offset
) extract_unsigned_integer (op_ptr
, 4, byte_order
);
1322 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_PARAM_OFFSET
,
1324 -1 /* deref_size */);
1328 case DW_OP_const_type
:
1329 case DW_OP_GNU_const_type
:
1332 const gdb_byte
*data
;
1335 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
1336 cu_offset type_die_cu_off
= (cu_offset
) uoffset
;
1342 type
= get_base_type (type_die_cu_off
, n
);
1343 result_val
= value_from_contents (type
, data
);
1347 case DW_OP_regval_type
:
1348 case DW_OP_GNU_regval_type
:
1352 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
1353 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
1354 cu_offset type_die_cu_off
= (cu_offset
) uoffset
;
1356 type
= get_base_type (type_die_cu_off
, 0);
1357 result_val
= this->get_reg_value (type
, reg
);
1362 case DW_OP_GNU_convert
:
1363 case DW_OP_reinterpret
:
1364 case DW_OP_GNU_reinterpret
:
1368 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
1369 cu_offset type_die_cu_off
= (cu_offset
) uoffset
;
1371 if (to_underlying (type_die_cu_off
) == 0)
1372 type
= address_type
;
1374 type
= get_base_type (type_die_cu_off
, 0);
1376 result_val
= fetch (0);
1379 if (op
== DW_OP_convert
|| op
== DW_OP_GNU_convert
)
1380 result_val
= value_cast (type
, result_val
);
1381 else if (type
== value_type (result_val
))
1385 else if (TYPE_LENGTH (type
)
1386 != TYPE_LENGTH (value_type (result_val
)))
1387 error (_("DW_OP_reinterpret has wrong size"));
1390 = value_from_contents (type
,
1391 value_contents_all (result_val
));
1395 case DW_OP_push_object_address
:
1396 /* Return the address of the object we are currently observing. */
1397 result
= this->get_object_address ();
1398 result_val
= value_from_ulongest (address_type
, result
);
1402 error (_("Unhandled dwarf expression opcode 0x%x"), op
);
1405 /* Most things push a result value. */
1406 gdb_assert (result_val
!= NULL
);
1407 push (result_val
, in_stack_memory
);
1412 /* To simplify our main caller, if the result is an implicit
1413 pointer, then make a pieced value. This is ok because we can't
1414 have implicit pointers in contexts where pieces are invalid. */
1415 if (this->location
== DWARF_VALUE_IMPLICIT_POINTER
)
1416 add_piece (8 * this->addr_size
, 0);
1418 this->recursion_depth
--;
1419 gdb_assert (this->recursion_depth
>= 0);
1423 _initialize_dwarf2expr (void)
1426 = gdbarch_data_register_post_init (dwarf_gdbarch_types_init
);