1 /* DWARF 2 Expression Evaluator.
3 Copyright (C) 2001-2020 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 "dwarf2/expr.h"
29 #include "dwarf2/loc.h"
30 #include "dwarf2/read.h"
31 #include "gdbsupport/underlying.h"
34 /* Cookie for gdbarch data. */
36 static struct gdbarch_data
*dwarf_arch_cookie
;
38 /* This holds gdbarch-specific types used by the DWARF expression
39 evaluator. See comments in execute_stack_op. */
41 struct dwarf_gdbarch_types
43 struct type
*dw_types
[3];
46 /* Allocate and fill in dwarf_gdbarch_types for an arch. */
49 dwarf_gdbarch_types_init (struct gdbarch
*gdbarch
)
51 struct dwarf_gdbarch_types
*types
52 = GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct dwarf_gdbarch_types
);
54 /* The types themselves are lazily initialized. */
59 /* Return the type used for DWARF operations where the type is
60 unspecified in the DWARF spec. Only certain sizes are
64 dwarf_expr_context::address_type () const
66 struct dwarf_gdbarch_types
*types
67 = (struct dwarf_gdbarch_types
*) gdbarch_data (this->gdbarch
,
71 if (this->addr_size
== 2)
73 else if (this->addr_size
== 4)
75 else if (this->addr_size
== 8)
78 error (_("Unsupported address size in DWARF expressions: %d bits"),
81 if (types
->dw_types
[ndx
] == NULL
)
83 = arch_integer_type (this->gdbarch
,
85 0, "<signed DWARF address type>");
87 return types
->dw_types
[ndx
];
90 /* Create a new context for the expression evaluator. */
92 dwarf_expr_context::dwarf_expr_context (dwarf2_per_objfile
*per_objfile
)
97 max_recursion_depth (0x100),
98 location (DWARF_VALUE_MEMORY
),
102 per_objfile (per_objfile
)
106 /* Push VALUE onto the stack. */
109 dwarf_expr_context::push (struct value
*value
, bool in_stack_memory
)
111 stack
.emplace_back (value
, in_stack_memory
);
114 /* Push VALUE onto the stack. */
117 dwarf_expr_context::push_address (CORE_ADDR value
, bool in_stack_memory
)
119 push (value_from_ulongest (address_type (), value
), in_stack_memory
);
122 /* Pop the top item off of the stack. */
125 dwarf_expr_context::pop ()
128 error (_("dwarf expression stack underflow"));
133 /* Retrieve the N'th item on the stack. */
136 dwarf_expr_context::fetch (int n
)
138 if (stack
.size () <= n
)
139 error (_("Asked for position %d of stack, "
140 "stack only has %zu elements on it."),
142 return stack
[stack
.size () - (1 + n
)].value
;
145 /* Require that TYPE be an integral type; throw an exception if not. */
148 dwarf_require_integral (struct type
*type
)
150 if (type
->code () != TYPE_CODE_INT
151 && type
->code () != TYPE_CODE_CHAR
152 && type
->code () != TYPE_CODE_BOOL
)
153 error (_("integral type expected in DWARF expression"));
156 /* Return the unsigned form of TYPE. TYPE is necessarily an integral
160 get_unsigned_type (struct gdbarch
*gdbarch
, struct type
*type
)
162 switch (TYPE_LENGTH (type
))
165 return builtin_type (gdbarch
)->builtin_uint8
;
167 return builtin_type (gdbarch
)->builtin_uint16
;
169 return builtin_type (gdbarch
)->builtin_uint32
;
171 return builtin_type (gdbarch
)->builtin_uint64
;
173 error (_("no unsigned variant found for type, while evaluating "
174 "DWARF expression"));
178 /* Return the signed form of TYPE. TYPE is necessarily an integral
182 get_signed_type (struct gdbarch
*gdbarch
, struct type
*type
)
184 switch (TYPE_LENGTH (type
))
187 return builtin_type (gdbarch
)->builtin_int8
;
189 return builtin_type (gdbarch
)->builtin_int16
;
191 return builtin_type (gdbarch
)->builtin_int32
;
193 return builtin_type (gdbarch
)->builtin_int64
;
195 error (_("no signed variant found for type, while evaluating "
196 "DWARF expression"));
200 /* Retrieve the N'th item on the stack, converted to an address. */
203 dwarf_expr_context::fetch_address (int n
)
205 struct value
*result_val
= fetch (n
);
206 enum bfd_endian byte_order
= gdbarch_byte_order (this->gdbarch
);
209 dwarf_require_integral (value_type (result_val
));
210 result
= extract_unsigned_integer (value_contents (result_val
),
211 TYPE_LENGTH (value_type (result_val
)),
214 /* For most architectures, calling extract_unsigned_integer() alone
215 is sufficient for extracting an address. However, some
216 architectures (e.g. MIPS) use signed addresses and using
217 extract_unsigned_integer() will not produce a correct
218 result. Make sure we invoke gdbarch_integer_to_address()
219 for those architectures which require it. */
220 if (gdbarch_integer_to_address_p (this->gdbarch
))
222 gdb_byte
*buf
= (gdb_byte
*) alloca (this->addr_size
);
223 struct type
*int_type
= get_unsigned_type (this->gdbarch
,
224 value_type (result_val
));
226 store_unsigned_integer (buf
, this->addr_size
, byte_order
, result
);
227 return gdbarch_integer_to_address (this->gdbarch
, int_type
, buf
);
230 return (CORE_ADDR
) result
;
233 /* Retrieve the in_stack_memory flag of the N'th item on the stack. */
236 dwarf_expr_context::fetch_in_stack_memory (int n
)
238 if (stack
.size () <= n
)
239 error (_("Asked for position %d of stack, "
240 "stack only has %zu elements on it."),
242 return stack
[stack
.size () - (1 + n
)].in_stack_memory
;
245 /* Return true if the expression stack is empty. */
248 dwarf_expr_context::stack_empty_p () const
250 return stack
.empty ();
253 /* Add a new piece to the dwarf_expr_context's piece list. */
255 dwarf_expr_context::add_piece (ULONGEST size
, ULONGEST offset
)
257 this->pieces
.emplace_back ();
258 dwarf_expr_piece
&p
= this->pieces
.back ();
260 p
.location
= this->location
;
264 if (p
.location
== DWARF_VALUE_LITERAL
)
266 p
.v
.literal
.data
= this->data
;
267 p
.v
.literal
.length
= this->len
;
269 else if (stack_empty_p ())
271 p
.location
= DWARF_VALUE_OPTIMIZED_OUT
;
272 /* Also reset the context's location, for our callers. This is
273 a somewhat strange approach, but this lets us avoid setting
274 the location to DWARF_VALUE_MEMORY in all the individual
275 cases in the evaluator. */
276 this->location
= DWARF_VALUE_OPTIMIZED_OUT
;
278 else if (p
.location
== DWARF_VALUE_MEMORY
)
280 p
.v
.mem
.addr
= fetch_address (0);
281 p
.v
.mem
.in_stack_memory
= fetch_in_stack_memory (0);
283 else if (p
.location
== DWARF_VALUE_IMPLICIT_POINTER
)
285 p
.v
.ptr
.die_sect_off
= (sect_offset
) this->len
;
286 p
.v
.ptr
.offset
= value_as_long (fetch (0));
288 else if (p
.location
== DWARF_VALUE_REGISTER
)
289 p
.v
.regno
= value_as_long (fetch (0));
292 p
.v
.value
= fetch (0);
296 /* Evaluate the expression at ADDR (LEN bytes long). */
299 dwarf_expr_context::eval (const gdb_byte
*addr
, size_t len
)
301 int old_recursion_depth
= this->recursion_depth
;
303 execute_stack_op (addr
, addr
+ len
);
305 /* RECURSION_DEPTH becomes invalid if an exception was thrown here. */
307 gdb_assert (this->recursion_depth
== old_recursion_depth
);
310 /* Helper to read a uleb128 value or throw an error. */
313 safe_read_uleb128 (const gdb_byte
*buf
, const gdb_byte
*buf_end
,
316 buf
= gdb_read_uleb128 (buf
, buf_end
, r
);
318 error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
322 /* Helper to read a sleb128 value or throw an error. */
325 safe_read_sleb128 (const gdb_byte
*buf
, const gdb_byte
*buf_end
,
328 buf
= gdb_read_sleb128 (buf
, buf_end
, r
);
330 error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
335 safe_skip_leb128 (const gdb_byte
*buf
, const gdb_byte
*buf_end
)
337 buf
= gdb_skip_leb128 (buf
, buf_end
);
339 error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
344 /* Check that the current operator is either at the end of an
345 expression, or that it is followed by a composition operator or by
346 DW_OP_GNU_uninit (which should terminate the expression). */
349 dwarf_expr_require_composition (const gdb_byte
*op_ptr
, const gdb_byte
*op_end
,
352 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
&& *op_ptr
!= DW_OP_bit_piece
353 && *op_ptr
!= DW_OP_GNU_uninit
)
354 error (_("DWARF-2 expression error: `%s' operations must be "
355 "used either alone or in conjunction with DW_OP_piece "
356 "or DW_OP_bit_piece."),
360 /* Return true iff the types T1 and T2 are "the same". This only does
361 checks that might reasonably be needed to compare DWARF base
365 base_types_equal_p (struct type
*t1
, struct type
*t2
)
367 if (t1
->code () != t2
->code ())
369 if (TYPE_UNSIGNED (t1
) != TYPE_UNSIGNED (t2
))
371 return TYPE_LENGTH (t1
) == TYPE_LENGTH (t2
);
374 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
375 DWARF register number. Otherwise return -1. */
378 dwarf_block_to_dwarf_reg (const gdb_byte
*buf
, const gdb_byte
*buf_end
)
384 if (*buf
>= DW_OP_reg0
&& *buf
<= DW_OP_reg31
)
386 if (buf_end
- buf
!= 1)
388 return *buf
- DW_OP_reg0
;
391 if (*buf
== DW_OP_regval_type
|| *buf
== DW_OP_GNU_regval_type
)
394 buf
= gdb_read_uleb128 (buf
, buf_end
, &dwarf_reg
);
397 buf
= gdb_skip_leb128 (buf
, buf_end
);
401 else if (*buf
== DW_OP_regx
)
404 buf
= gdb_read_uleb128 (buf
, buf_end
, &dwarf_reg
);
410 if (buf
!= buf_end
|| (int) dwarf_reg
!= dwarf_reg
)
415 /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
416 DW_OP_deref* return the DWARF register number. Otherwise return -1.
417 DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
418 size from DW_OP_deref_size. */
421 dwarf_block_to_dwarf_reg_deref (const gdb_byte
*buf
, const gdb_byte
*buf_end
,
422 CORE_ADDR
*deref_size_return
)
430 if (*buf
>= DW_OP_breg0
&& *buf
<= DW_OP_breg31
)
432 dwarf_reg
= *buf
- DW_OP_breg0
;
437 else if (*buf
== DW_OP_bregx
)
440 buf
= gdb_read_uleb128 (buf
, buf_end
, &dwarf_reg
);
443 if ((int) dwarf_reg
!= dwarf_reg
)
449 buf
= gdb_read_sleb128 (buf
, buf_end
, &offset
);
455 if (*buf
== DW_OP_deref
)
458 *deref_size_return
= -1;
460 else if (*buf
== DW_OP_deref_size
)
465 *deref_size_return
= *buf
++;
476 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
477 in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */
480 dwarf_block_to_fb_offset (const gdb_byte
*buf
, const gdb_byte
*buf_end
,
481 CORE_ADDR
*fb_offset_return
)
488 if (*buf
!= DW_OP_fbreg
)
492 buf
= gdb_read_sleb128 (buf
, buf_end
, &fb_offset
);
495 *fb_offset_return
= fb_offset
;
496 if (buf
!= buf_end
|| fb_offset
!= (LONGEST
) *fb_offset_return
)
502 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
503 in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0.
504 The matched SP register number depends on GDBARCH. */
507 dwarf_block_to_sp_offset (struct gdbarch
*gdbarch
, const gdb_byte
*buf
,
508 const gdb_byte
*buf_end
, CORE_ADDR
*sp_offset_return
)
515 if (*buf
>= DW_OP_breg0
&& *buf
<= DW_OP_breg31
)
517 dwarf_reg
= *buf
- DW_OP_breg0
;
522 if (*buf
!= DW_OP_bregx
)
525 buf
= gdb_read_uleb128 (buf
, buf_end
, &dwarf_reg
);
530 if (dwarf_reg_to_regnum (gdbarch
, dwarf_reg
)
531 != gdbarch_sp_regnum (gdbarch
))
534 buf
= gdb_read_sleb128 (buf
, buf_end
, &sp_offset
);
537 *sp_offset_return
= sp_offset
;
538 if (buf
!= buf_end
|| sp_offset
!= (LONGEST
) *sp_offset_return
)
544 /* The engine for the expression evaluator. Using the context in this
545 object, evaluate the expression between OP_PTR and OP_END. */
548 dwarf_expr_context::execute_stack_op (const gdb_byte
*op_ptr
,
549 const gdb_byte
*op_end
)
551 enum bfd_endian byte_order
= gdbarch_byte_order (this->gdbarch
);
552 /* Old-style "untyped" DWARF values need special treatment in a
553 couple of places, specifically DW_OP_mod and DW_OP_shr. We need
554 a special type for these values so we can distinguish them from
555 values that have an explicit type, because explicitly-typed
556 values do not need special treatment. This special type must be
557 different (in the `==' sense) from any base type coming from the
559 struct type
*address_type
= this->address_type ();
561 this->location
= DWARF_VALUE_MEMORY
;
562 this->initialized
= 1; /* Default is initialized. */
564 if (this->recursion_depth
> this->max_recursion_depth
)
565 error (_("DWARF-2 expression error: Loop detected (%d)."),
566 this->recursion_depth
);
567 this->recursion_depth
++;
569 while (op_ptr
< op_end
)
571 enum dwarf_location_atom op
= (enum dwarf_location_atom
) *op_ptr
++;
573 /* Assume the value is not in stack memory.
574 Code that knows otherwise sets this to true.
575 Some arithmetic on stack addresses can probably be assumed to still
576 be a stack address, but we skip this complication for now.
577 This is just an optimization, so it's always ok to punt
578 and leave this as false. */
579 bool in_stack_memory
= false;
580 uint64_t uoffset
, reg
;
582 struct value
*result_val
= NULL
;
584 /* The DWARF expression might have a bug causing an infinite
585 loop. In that case, quitting is the only way out. */
622 result
= op
- DW_OP_lit0
;
623 result_val
= value_from_ulongest (address_type
, result
);
627 result
= extract_unsigned_integer (op_ptr
,
628 this->addr_size
, byte_order
);
629 op_ptr
+= this->addr_size
;
630 /* Some versions of GCC emit DW_OP_addr before
631 DW_OP_GNU_push_tls_address. In this case the value is an
632 index, not an address. We don't support things like
633 branching between the address and the TLS op. */
634 if (op_ptr
>= op_end
|| *op_ptr
!= DW_OP_GNU_push_tls_address
)
635 result
+= this->per_objfile
->objfile
->text_section_offset ();
636 result_val
= value_from_ulongest (address_type
, result
);
640 case DW_OP_GNU_addr_index
:
641 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
642 result
= this->get_addr_index (uoffset
);
643 result
+= this->per_objfile
->objfile
->text_section_offset ();
644 result_val
= value_from_ulongest (address_type
, result
);
646 case DW_OP_GNU_const_index
:
647 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
648 result
= this->get_addr_index (uoffset
);
649 result_val
= value_from_ulongest (address_type
, result
);
653 result
= extract_unsigned_integer (op_ptr
, 1, byte_order
);
654 result_val
= value_from_ulongest (address_type
, result
);
658 result
= extract_signed_integer (op_ptr
, 1, byte_order
);
659 result_val
= value_from_ulongest (address_type
, result
);
663 result
= extract_unsigned_integer (op_ptr
, 2, byte_order
);
664 result_val
= value_from_ulongest (address_type
, result
);
668 result
= extract_signed_integer (op_ptr
, 2, byte_order
);
669 result_val
= value_from_ulongest (address_type
, result
);
673 result
= extract_unsigned_integer (op_ptr
, 4, byte_order
);
674 result_val
= value_from_ulongest (address_type
, result
);
678 result
= extract_signed_integer (op_ptr
, 4, byte_order
);
679 result_val
= value_from_ulongest (address_type
, result
);
683 result
= extract_unsigned_integer (op_ptr
, 8, byte_order
);
684 result_val
= value_from_ulongest (address_type
, result
);
688 result
= extract_signed_integer (op_ptr
, 8, byte_order
);
689 result_val
= value_from_ulongest (address_type
, result
);
693 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
695 result_val
= value_from_ulongest (address_type
, result
);
698 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
700 result_val
= value_from_ulongest (address_type
, result
);
703 /* The DW_OP_reg operations are required to occur alone in
704 location expressions. */
737 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_reg");
739 result
= op
- DW_OP_reg0
;
740 result_val
= value_from_ulongest (address_type
, result
);
741 this->location
= DWARF_VALUE_REGISTER
;
745 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
746 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
749 result_val
= value_from_ulongest (address_type
, result
);
750 this->location
= DWARF_VALUE_REGISTER
;
753 case DW_OP_implicit_value
:
757 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &len
);
758 if (op_ptr
+ len
> op_end
)
759 error (_("DW_OP_implicit_value: too few bytes available."));
762 this->location
= DWARF_VALUE_LITERAL
;
764 dwarf_expr_require_composition (op_ptr
, op_end
,
765 "DW_OP_implicit_value");
769 case DW_OP_stack_value
:
770 this->location
= DWARF_VALUE_STACK
;
771 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_stack_value");
774 case DW_OP_implicit_pointer
:
775 case DW_OP_GNU_implicit_pointer
:
779 if (this->ref_addr_size
== -1)
780 error (_("DWARF-2 expression error: DW_OP_implicit_pointer "
781 "is not allowed in frame context"));
783 /* The referred-to DIE of sect_offset kind. */
784 this->len
= extract_unsigned_integer (op_ptr
, this->ref_addr_size
,
786 op_ptr
+= this->ref_addr_size
;
788 /* The byte offset into the data. */
789 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &len
);
790 result
= (ULONGEST
) len
;
791 result_val
= value_from_ulongest (address_type
, result
);
793 this->location
= DWARF_VALUE_IMPLICIT_POINTER
;
794 dwarf_expr_require_composition (op_ptr
, op_end
,
795 "DW_OP_implicit_pointer");
832 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
833 result
= this->read_addr_from_reg (op
- DW_OP_breg0
);
835 result_val
= value_from_ulongest (address_type
, result
);
840 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
841 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
842 result
= this->read_addr_from_reg (reg
);
844 result_val
= value_from_ulongest (address_type
, result
);
849 const gdb_byte
*datastart
;
852 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
854 /* Rather than create a whole new context, we simply
855 backup the current stack locally and install a new empty stack,
856 then reset it afterwards, effectively erasing whatever the
857 recursive call put there. */
858 std::vector
<dwarf_stack_value
> saved_stack
= std::move (stack
);
861 /* FIXME: cagney/2003-03-26: This code should be using
862 get_frame_base_address(), and then implement a dwarf2
863 specific this_base method. */
864 this->get_frame_base (&datastart
, &datalen
);
865 eval (datastart
, datalen
);
866 if (this->location
== DWARF_VALUE_MEMORY
)
867 result
= fetch_address (0);
868 else if (this->location
== DWARF_VALUE_REGISTER
)
869 result
= this->read_addr_from_reg (value_as_long (fetch (0)));
871 error (_("Not implemented: computing frame "
872 "base using explicit value operator"));
873 result
= result
+ offset
;
874 result_val
= value_from_ulongest (address_type
, result
);
875 in_stack_memory
= true;
877 /* Restore the content of the original stack. */
878 stack
= std::move (saved_stack
);
880 this->location
= DWARF_VALUE_MEMORY
;
885 result_val
= fetch (0);
886 in_stack_memory
= fetch_in_stack_memory (0);
895 result_val
= fetch (offset
);
896 in_stack_memory
= fetch_in_stack_memory (offset
);
901 if (stack
.size () < 2)
902 error (_("Not enough elements for "
903 "DW_OP_swap. Need 2, have %zu."),
906 dwarf_stack_value
&t1
= stack
[stack
.size () - 1];
907 dwarf_stack_value
&t2
= stack
[stack
.size () - 2];
913 result_val
= fetch (1);
914 in_stack_memory
= fetch_in_stack_memory (1);
919 if (stack
.size () < 3)
920 error (_("Not enough elements for "
921 "DW_OP_rot. Need 3, have %zu."),
924 dwarf_stack_value temp
= stack
[stack
.size () - 1];
925 stack
[stack
.size () - 1] = stack
[stack
.size () - 2];
926 stack
[stack
.size () - 2] = stack
[stack
.size () - 3];
927 stack
[stack
.size () - 3] = temp
;
932 case DW_OP_deref_size
:
933 case DW_OP_deref_type
:
934 case DW_OP_GNU_deref_type
:
936 int addr_size
= (op
== DW_OP_deref
? this->addr_size
: *op_ptr
++);
937 gdb_byte
*buf
= (gdb_byte
*) alloca (addr_size
);
938 CORE_ADDR addr
= fetch_address (0);
943 if (op
== DW_OP_deref_type
|| op
== DW_OP_GNU_deref_type
)
945 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
946 cu_offset type_die_cu_off
= (cu_offset
) uoffset
;
947 type
= get_base_type (type_die_cu_off
, 0);
952 this->read_mem (buf
, addr
, addr_size
);
954 /* If the size of the object read from memory is different
955 from the type length, we need to zero-extend it. */
956 if (TYPE_LENGTH (type
) != addr_size
)
959 extract_unsigned_integer (buf
, addr_size
, byte_order
);
961 buf
= (gdb_byte
*) alloca (TYPE_LENGTH (type
));
962 store_unsigned_integer (buf
, TYPE_LENGTH (type
),
966 result_val
= value_from_contents_and_address (type
, buf
, addr
);
973 case DW_OP_plus_uconst
:
975 /* Unary operations. */
976 result_val
= fetch (0);
982 if (value_less (result_val
,
983 value_zero (value_type (result_val
), not_lval
)))
984 result_val
= value_neg (result_val
);
987 result_val
= value_neg (result_val
);
990 dwarf_require_integral (value_type (result_val
));
991 result_val
= value_complement (result_val
);
993 case DW_OP_plus_uconst
:
994 dwarf_require_integral (value_type (result_val
));
995 result
= value_as_long (result_val
);
996 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
998 result_val
= value_from_ulongest (address_type
, result
);
1022 /* Binary operations. */
1023 struct value
*first
, *second
;
1031 if (! base_types_equal_p (value_type (first
), value_type (second
)))
1032 error (_("Incompatible types on DWARF stack"));
1037 dwarf_require_integral (value_type (first
));
1038 dwarf_require_integral (value_type (second
));
1039 result_val
= value_binop (first
, second
, BINOP_BITWISE_AND
);
1042 result_val
= value_binop (first
, second
, BINOP_DIV
);
1045 result_val
= value_binop (first
, second
, BINOP_SUB
);
1050 struct type
*orig_type
= value_type (first
);
1052 /* We have to special-case "old-style" untyped values
1053 -- these must have mod computed using unsigned
1055 if (orig_type
== address_type
)
1058 = get_unsigned_type (this->gdbarch
, orig_type
);
1061 first
= value_cast (utype
, first
);
1062 second
= value_cast (utype
, second
);
1064 /* Note that value_binop doesn't handle float or
1065 decimal float here. This seems unimportant. */
1066 result_val
= value_binop (first
, second
, BINOP_MOD
);
1068 result_val
= value_cast (orig_type
, result_val
);
1072 result_val
= value_binop (first
, second
, BINOP_MUL
);
1075 dwarf_require_integral (value_type (first
));
1076 dwarf_require_integral (value_type (second
));
1077 result_val
= value_binop (first
, second
, BINOP_BITWISE_IOR
);
1080 result_val
= value_binop (first
, second
, BINOP_ADD
);
1083 dwarf_require_integral (value_type (first
));
1084 dwarf_require_integral (value_type (second
));
1085 result_val
= value_binop (first
, second
, BINOP_LSH
);
1088 dwarf_require_integral (value_type (first
));
1089 dwarf_require_integral (value_type (second
));
1090 if (!TYPE_UNSIGNED (value_type (first
)))
1093 = get_unsigned_type (this->gdbarch
, value_type (first
));
1095 first
= value_cast (utype
, first
);
1098 result_val
= value_binop (first
, second
, BINOP_RSH
);
1099 /* Make sure we wind up with the same type we started
1101 if (value_type (result_val
) != value_type (second
))
1102 result_val
= value_cast (value_type (second
), result_val
);
1105 dwarf_require_integral (value_type (first
));
1106 dwarf_require_integral (value_type (second
));
1107 if (TYPE_UNSIGNED (value_type (first
)))
1110 = get_signed_type (this->gdbarch
, value_type (first
));
1112 first
= value_cast (stype
, first
);
1115 result_val
= value_binop (first
, second
, BINOP_RSH
);
1116 /* Make sure we wind up with the same type we started
1118 if (value_type (result_val
) != value_type (second
))
1119 result_val
= value_cast (value_type (second
), result_val
);
1122 dwarf_require_integral (value_type (first
));
1123 dwarf_require_integral (value_type (second
));
1124 result_val
= value_binop (first
, second
, BINOP_BITWISE_XOR
);
1127 /* A <= B is !(B < A). */
1128 result
= ! value_less (second
, first
);
1129 result_val
= value_from_ulongest (address_type
, result
);
1132 /* A >= B is !(A < B). */
1133 result
= ! value_less (first
, second
);
1134 result_val
= value_from_ulongest (address_type
, result
);
1137 result
= value_equal (first
, second
);
1138 result_val
= value_from_ulongest (address_type
, result
);
1141 result
= value_less (first
, second
);
1142 result_val
= value_from_ulongest (address_type
, result
);
1145 /* A > B is B < A. */
1146 result
= value_less (second
, first
);
1147 result_val
= value_from_ulongest (address_type
, result
);
1150 result
= ! value_equal (first
, second
);
1151 result_val
= value_from_ulongest (address_type
, result
);
1154 internal_error (__FILE__
, __LINE__
,
1155 _("Can't be reached."));
1160 case DW_OP_call_frame_cfa
:
1161 result
= this->get_frame_cfa ();
1162 result_val
= value_from_ulongest (address_type
, result
);
1163 in_stack_memory
= true;
1166 case DW_OP_GNU_push_tls_address
:
1167 case DW_OP_form_tls_address
:
1168 /* Variable is at a constant offset in the thread-local
1169 storage block into the objfile for the current thread and
1170 the dynamic linker module containing this expression. Here
1171 we return returns the offset from that base. The top of the
1172 stack has the offset from the beginning of the thread
1173 control block at which the variable is located. Nothing
1174 should follow this operator, so the top of stack would be
1176 result
= value_as_long (fetch (0));
1178 result
= this->get_tls_address (result
);
1179 result_val
= value_from_ulongest (address_type
, result
);
1183 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
1192 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
1195 dwarf_require_integral (value_type (val
));
1196 if (value_as_long (val
) != 0)
1209 /* Record the piece. */
1210 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &size
);
1211 add_piece (8 * size
, 0);
1213 /* Pop off the address/regnum, and reset the location
1215 if (this->location
!= DWARF_VALUE_LITERAL
1216 && this->location
!= DWARF_VALUE_OPTIMIZED_OUT
)
1218 this->location
= DWARF_VALUE_MEMORY
;
1222 case DW_OP_bit_piece
:
1224 uint64_t size
, uleb_offset
;
1226 /* Record the piece. */
1227 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &size
);
1228 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uleb_offset
);
1229 add_piece (size
, uleb_offset
);
1231 /* Pop off the address/regnum, and reset the location
1233 if (this->location
!= DWARF_VALUE_LITERAL
1234 && this->location
!= DWARF_VALUE_OPTIMIZED_OUT
)
1236 this->location
= DWARF_VALUE_MEMORY
;
1240 case DW_OP_GNU_uninit
:
1241 if (op_ptr
!= op_end
)
1242 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
1243 "be the very last op."));
1245 this->initialized
= 0;
1251 = (cu_offset
) extract_unsigned_integer (op_ptr
, 2, byte_order
);
1253 this->dwarf_call (cu_off
);
1260 = (cu_offset
) extract_unsigned_integer (op_ptr
, 4, byte_order
);
1262 this->dwarf_call (cu_off
);
1266 case DW_OP_GNU_variable_value
:
1268 sect_offset sect_off
1269 = (sect_offset
) extract_unsigned_integer (op_ptr
,
1270 this->ref_addr_size
,
1272 op_ptr
+= this->ref_addr_size
;
1273 result_val
= this->dwarf_variable_value (sect_off
);
1277 case DW_OP_entry_value
:
1278 case DW_OP_GNU_entry_value
:
1281 CORE_ADDR deref_size
;
1282 union call_site_parameter_u kind_u
;
1284 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &len
);
1285 if (op_ptr
+ len
> op_end
)
1286 error (_("DW_OP_entry_value: too few bytes available."));
1288 kind_u
.dwarf_reg
= dwarf_block_to_dwarf_reg (op_ptr
, op_ptr
+ len
);
1289 if (kind_u
.dwarf_reg
!= -1)
1292 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG
,
1294 -1 /* deref_size */);
1298 kind_u
.dwarf_reg
= dwarf_block_to_dwarf_reg_deref (op_ptr
,
1301 if (kind_u
.dwarf_reg
!= -1)
1303 if (deref_size
== -1)
1304 deref_size
= this->addr_size
;
1306 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG
,
1307 kind_u
, deref_size
);
1311 error (_("DWARF-2 expression error: DW_OP_entry_value is "
1312 "supported only for single DW_OP_reg* "
1313 "or for DW_OP_breg*(0)+DW_OP_deref*"));
1316 case DW_OP_GNU_parameter_ref
:
1318 union call_site_parameter_u kind_u
;
1321 = (cu_offset
) extract_unsigned_integer (op_ptr
, 4, byte_order
);
1323 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_PARAM_OFFSET
,
1325 -1 /* deref_size */);
1329 case DW_OP_const_type
:
1330 case DW_OP_GNU_const_type
:
1333 const gdb_byte
*data
;
1336 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
1337 cu_offset type_die_cu_off
= (cu_offset
) uoffset
;
1343 type
= get_base_type (type_die_cu_off
, n
);
1344 result_val
= value_from_contents (type
, data
);
1348 case DW_OP_regval_type
:
1349 case DW_OP_GNU_regval_type
:
1353 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
1354 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
1355 cu_offset type_die_cu_off
= (cu_offset
) uoffset
;
1357 type
= get_base_type (type_die_cu_off
, 0);
1358 result_val
= this->get_reg_value (type
, reg
);
1363 case DW_OP_GNU_convert
:
1364 case DW_OP_reinterpret
:
1365 case DW_OP_GNU_reinterpret
:
1369 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
1370 cu_offset type_die_cu_off
= (cu_offset
) uoffset
;
1372 if (to_underlying (type_die_cu_off
) == 0)
1373 type
= address_type
;
1375 type
= get_base_type (type_die_cu_off
, 0);
1377 result_val
= fetch (0);
1380 if (op
== DW_OP_convert
|| op
== DW_OP_GNU_convert
)
1381 result_val
= value_cast (type
, result_val
);
1382 else if (type
== value_type (result_val
))
1386 else if (TYPE_LENGTH (type
)
1387 != TYPE_LENGTH (value_type (result_val
)))
1388 error (_("DW_OP_reinterpret has wrong size"));
1391 = value_from_contents (type
,
1392 value_contents_all (result_val
));
1396 case DW_OP_push_object_address
:
1397 /* Return the address of the object we are currently observing. */
1398 result
= this->get_object_address ();
1399 result_val
= value_from_ulongest (address_type
, result
);
1403 error (_("Unhandled dwarf expression opcode 0x%x"), op
);
1406 /* Most things push a result value. */
1407 gdb_assert (result_val
!= NULL
);
1408 push (result_val
, in_stack_memory
);
1413 /* To simplify our main caller, if the result is an implicit
1414 pointer, then make a pieced value. This is ok because we can't
1415 have implicit pointers in contexts where pieces are invalid. */
1416 if (this->location
== DWARF_VALUE_IMPLICIT_POINTER
)
1417 add_piece (8 * this->addr_size
, 0);
1419 this->recursion_depth
--;
1420 gdb_assert (this->recursion_depth
>= 0);
1423 void _initialize_dwarf2expr ();
1425 _initialize_dwarf2expr ()
1428 = gdbarch_data_register_post_init (dwarf_gdbarch_types_init
);