1 /* DWARF 2 Expression Evaluator.
3 Copyright (C) 2001-2016 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"
31 /* Cookie for gdbarch data. */
33 static struct gdbarch_data
*dwarf_arch_cookie
;
35 /* This holds gdbarch-specific types used by the DWARF expression
36 evaluator. See comments in execute_stack_op. */
38 struct dwarf_gdbarch_types
40 struct type
*dw_types
[3];
43 /* Allocate and fill in dwarf_gdbarch_types for an arch. */
46 dwarf_gdbarch_types_init (struct gdbarch
*gdbarch
)
48 struct dwarf_gdbarch_types
*types
49 = GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct dwarf_gdbarch_types
);
51 /* The types themselves are lazily initialized. */
56 /* Return the type used for DWARF operations where the type is
57 unspecified in the DWARF spec. Only certain sizes are
61 dwarf_expr_context::address_type () const
63 struct dwarf_gdbarch_types
*types
64 = (struct dwarf_gdbarch_types
*) gdbarch_data (this->gdbarch
,
68 if (this->addr_size
== 2)
70 else if (this->addr_size
== 4)
72 else if (this->addr_size
== 8)
75 error (_("Unsupported address size in DWARF expressions: %d bits"),
78 if (types
->dw_types
[ndx
] == NULL
)
80 = arch_integer_type (this->gdbarch
,
82 0, "<signed DWARF address type>");
84 return types
->dw_types
[ndx
];
87 /* Create a new context for the expression evaluator. */
89 dwarf_expr_context::dwarf_expr_context ()
98 max_recursion_depth (0x100),
99 location (DWARF_VALUE_MEMORY
),
106 this->stack
= XNEWVEC (struct dwarf_stack_value
, this->stack_allocated
);
109 /* Clean up a dwarf_expr_context. */
111 dwarf_expr_context::~dwarf_expr_context ()
114 xfree (this->pieces
);
117 /* Expand the memory allocated stack to contain at least
118 NEED more elements than are currently used. */
121 dwarf_expr_context::grow_stack (size_t need
)
123 if (this->stack_len
+ need
> this->stack_allocated
)
125 size_t newlen
= this->stack_len
+ need
+ 10;
127 this->stack
= XRESIZEVEC (struct dwarf_stack_value
, this->stack
, newlen
);
128 this->stack_allocated
= newlen
;
132 /* Push VALUE onto the stack. */
135 dwarf_expr_context::push (struct value
*value
, int in_stack_memory
)
137 struct dwarf_stack_value
*v
;
140 v
= &this->stack
[this->stack_len
++];
142 v
->in_stack_memory
= in_stack_memory
;
145 /* Push VALUE onto the stack. */
148 dwarf_expr_context::push_address (CORE_ADDR value
, int in_stack_memory
)
150 push (value_from_ulongest (address_type (), value
), in_stack_memory
);
153 /* Pop the top item off of the stack. */
156 dwarf_expr_context::pop ()
158 if (this->stack_len
<= 0)
159 error (_("dwarf expression stack underflow"));
163 /* Retrieve the N'th item on the stack. */
166 dwarf_expr_context::fetch (int n
)
168 if (this->stack_len
<= n
)
169 error (_("Asked for position %d of stack, "
170 "stack only has %d elements on it."),
172 return this->stack
[this->stack_len
- (1 + n
)].value
;
175 /* Require that TYPE be an integral type; throw an exception if not. */
178 dwarf_require_integral (struct type
*type
)
180 if (TYPE_CODE (type
) != TYPE_CODE_INT
181 && TYPE_CODE (type
) != TYPE_CODE_CHAR
182 && TYPE_CODE (type
) != TYPE_CODE_BOOL
)
183 error (_("integral type expected in DWARF expression"));
186 /* Return the unsigned form of TYPE. TYPE is necessarily an integral
190 get_unsigned_type (struct gdbarch
*gdbarch
, struct type
*type
)
192 switch (TYPE_LENGTH (type
))
195 return builtin_type (gdbarch
)->builtin_uint8
;
197 return builtin_type (gdbarch
)->builtin_uint16
;
199 return builtin_type (gdbarch
)->builtin_uint32
;
201 return builtin_type (gdbarch
)->builtin_uint64
;
203 error (_("no unsigned variant found for type, while evaluating "
204 "DWARF expression"));
208 /* Return the signed form of TYPE. TYPE is necessarily an integral
212 get_signed_type (struct gdbarch
*gdbarch
, struct type
*type
)
214 switch (TYPE_LENGTH (type
))
217 return builtin_type (gdbarch
)->builtin_int8
;
219 return builtin_type (gdbarch
)->builtin_int16
;
221 return builtin_type (gdbarch
)->builtin_int32
;
223 return builtin_type (gdbarch
)->builtin_int64
;
225 error (_("no signed variant found for type, while evaluating "
226 "DWARF expression"));
230 /* Retrieve the N'th item on the stack, converted to an address. */
233 dwarf_expr_context::fetch_address (int n
)
235 struct value
*result_val
= fetch (n
);
236 enum bfd_endian byte_order
= gdbarch_byte_order (this->gdbarch
);
239 dwarf_require_integral (value_type (result_val
));
240 result
= extract_unsigned_integer (value_contents (result_val
),
241 TYPE_LENGTH (value_type (result_val
)),
244 /* For most architectures, calling extract_unsigned_integer() alone
245 is sufficient for extracting an address. However, some
246 architectures (e.g. MIPS) use signed addresses and using
247 extract_unsigned_integer() will not produce a correct
248 result. Make sure we invoke gdbarch_integer_to_address()
249 for those architectures which require it. */
250 if (gdbarch_integer_to_address_p (this->gdbarch
))
252 gdb_byte
*buf
= (gdb_byte
*) alloca (this->addr_size
);
253 struct type
*int_type
= get_unsigned_type (this->gdbarch
,
254 value_type (result_val
));
256 store_unsigned_integer (buf
, this->addr_size
, byte_order
, result
);
257 return gdbarch_integer_to_address (this->gdbarch
, int_type
, buf
);
260 return (CORE_ADDR
) result
;
263 /* Retrieve the in_stack_memory flag of the N'th item on the stack. */
266 dwarf_expr_context::fetch_in_stack_memory (int n
)
268 if (this->stack_len
<= n
)
269 error (_("Asked for position %d of stack, "
270 "stack only has %d elements on it."),
272 return this->stack
[this->stack_len
- (1 + n
)].in_stack_memory
;
275 /* Return true if the expression stack is empty. */
278 dwarf_expr_context::stack_empty_p () const
280 return this->stack_len
== 0;
283 /* Add a new piece to the dwarf_expr_context's piece list. */
285 dwarf_expr_context::add_piece (ULONGEST size
, ULONGEST offset
)
287 struct dwarf_expr_piece
*p
;
292 = XRESIZEVEC (struct dwarf_expr_piece
, this->pieces
, this->num_pieces
);
294 p
= &this->pieces
[this->num_pieces
- 1];
295 p
->location
= this->location
;
299 if (p
->location
== DWARF_VALUE_LITERAL
)
301 p
->v
.literal
.data
= this->data
;
302 p
->v
.literal
.length
= this->len
;
304 else if (stack_empty_p ())
306 p
->location
= DWARF_VALUE_OPTIMIZED_OUT
;
307 /* Also reset the context's location, for our callers. This is
308 a somewhat strange approach, but this lets us avoid setting
309 the location to DWARF_VALUE_MEMORY in all the individual
310 cases in the evaluator. */
311 this->location
= DWARF_VALUE_OPTIMIZED_OUT
;
313 else if (p
->location
== DWARF_VALUE_MEMORY
)
315 p
->v
.mem
.addr
= fetch_address (0);
316 p
->v
.mem
.in_stack_memory
= fetch_in_stack_memory (0);
318 else if (p
->location
== DWARF_VALUE_IMPLICIT_POINTER
)
320 p
->v
.ptr
.die
.sect_off
= this->len
;
321 p
->v
.ptr
.offset
= value_as_long (fetch (0));
323 else if (p
->location
== DWARF_VALUE_REGISTER
)
324 p
->v
.regno
= value_as_long (fetch (0));
327 p
->v
.value
= fetch (0);
331 /* Evaluate the expression at ADDR (LEN bytes long). */
334 dwarf_expr_context::eval (const gdb_byte
*addr
, size_t len
)
336 int old_recursion_depth
= this->recursion_depth
;
338 execute_stack_op (addr
, addr
+ len
);
340 /* RECURSION_DEPTH becomes invalid if an exception was thrown here. */
342 gdb_assert (this->recursion_depth
== old_recursion_depth
);
345 /* Helper to read a uleb128 value or throw an error. */
348 safe_read_uleb128 (const gdb_byte
*buf
, const gdb_byte
*buf_end
,
351 buf
= gdb_read_uleb128 (buf
, buf_end
, r
);
353 error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
357 /* Helper to read a sleb128 value or throw an error. */
360 safe_read_sleb128 (const gdb_byte
*buf
, const gdb_byte
*buf_end
,
363 buf
= gdb_read_sleb128 (buf
, buf_end
, r
);
365 error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
370 safe_skip_leb128 (const gdb_byte
*buf
, const gdb_byte
*buf_end
)
372 buf
= gdb_skip_leb128 (buf
, buf_end
);
374 error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
379 /* Check that the current operator is either at the end of an
380 expression, or that it is followed by a composition operator or by
381 DW_OP_GNU_uninit (which should terminate the expression). */
384 dwarf_expr_require_composition (const gdb_byte
*op_ptr
, const gdb_byte
*op_end
,
387 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
&& *op_ptr
!= DW_OP_bit_piece
388 && *op_ptr
!= DW_OP_GNU_uninit
)
389 error (_("DWARF-2 expression error: `%s' operations must be "
390 "used either alone or in conjunction with DW_OP_piece "
391 "or DW_OP_bit_piece."),
395 /* Return true iff the types T1 and T2 are "the same". This only does
396 checks that might reasonably be needed to compare DWARF base
400 base_types_equal_p (struct type
*t1
, struct type
*t2
)
402 if (TYPE_CODE (t1
) != TYPE_CODE (t2
))
404 if (TYPE_UNSIGNED (t1
) != TYPE_UNSIGNED (t2
))
406 return TYPE_LENGTH (t1
) == TYPE_LENGTH (t2
);
409 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
410 DWARF register number. Otherwise return -1. */
413 dwarf_block_to_dwarf_reg (const gdb_byte
*buf
, const gdb_byte
*buf_end
)
419 if (*buf
>= DW_OP_reg0
&& *buf
<= DW_OP_reg31
)
421 if (buf_end
- buf
!= 1)
423 return *buf
- DW_OP_reg0
;
426 if (*buf
== DW_OP_GNU_regval_type
)
429 buf
= gdb_read_uleb128 (buf
, buf_end
, &dwarf_reg
);
432 buf
= gdb_skip_leb128 (buf
, buf_end
);
436 else if (*buf
== DW_OP_regx
)
439 buf
= gdb_read_uleb128 (buf
, buf_end
, &dwarf_reg
);
445 if (buf
!= buf_end
|| (int) dwarf_reg
!= dwarf_reg
)
450 /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
451 DW_OP_deref* return the DWARF register number. Otherwise return -1.
452 DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
453 size from DW_OP_deref_size. */
456 dwarf_block_to_dwarf_reg_deref (const gdb_byte
*buf
, const gdb_byte
*buf_end
,
457 CORE_ADDR
*deref_size_return
)
465 if (*buf
>= DW_OP_breg0
&& *buf
<= DW_OP_breg31
)
467 dwarf_reg
= *buf
- DW_OP_breg0
;
472 else if (*buf
== DW_OP_bregx
)
475 buf
= gdb_read_uleb128 (buf
, buf_end
, &dwarf_reg
);
478 if ((int) dwarf_reg
!= dwarf_reg
)
484 buf
= gdb_read_sleb128 (buf
, buf_end
, &offset
);
490 if (*buf
== DW_OP_deref
)
493 *deref_size_return
= -1;
495 else if (*buf
== DW_OP_deref_size
)
500 *deref_size_return
= *buf
++;
511 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
512 in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */
515 dwarf_block_to_fb_offset (const gdb_byte
*buf
, const gdb_byte
*buf_end
,
516 CORE_ADDR
*fb_offset_return
)
523 if (*buf
!= DW_OP_fbreg
)
527 buf
= gdb_read_sleb128 (buf
, buf_end
, &fb_offset
);
530 *fb_offset_return
= fb_offset
;
531 if (buf
!= buf_end
|| fb_offset
!= (LONGEST
) *fb_offset_return
)
537 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
538 in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0.
539 The matched SP register number depends on GDBARCH. */
542 dwarf_block_to_sp_offset (struct gdbarch
*gdbarch
, const gdb_byte
*buf
,
543 const gdb_byte
*buf_end
, CORE_ADDR
*sp_offset_return
)
550 if (*buf
>= DW_OP_breg0
&& *buf
<= DW_OP_breg31
)
552 dwarf_reg
= *buf
- DW_OP_breg0
;
557 if (*buf
!= DW_OP_bregx
)
560 buf
= gdb_read_uleb128 (buf
, buf_end
, &dwarf_reg
);
565 if (dwarf_reg_to_regnum (gdbarch
, dwarf_reg
)
566 != gdbarch_sp_regnum (gdbarch
))
569 buf
= gdb_read_sleb128 (buf
, buf_end
, &sp_offset
);
572 *sp_offset_return
= sp_offset
;
573 if (buf
!= buf_end
|| sp_offset
!= (LONGEST
) *sp_offset_return
)
579 /* The engine for the expression evaluator. Using the context in this
580 object, evaluate the expression between OP_PTR and OP_END. */
583 dwarf_expr_context::execute_stack_op (const gdb_byte
*op_ptr
,
584 const gdb_byte
*op_end
)
586 enum bfd_endian byte_order
= gdbarch_byte_order (this->gdbarch
);
587 /* Old-style "untyped" DWARF values need special treatment in a
588 couple of places, specifically DW_OP_mod and DW_OP_shr. We need
589 a special type for these values so we can distinguish them from
590 values that have an explicit type, because explicitly-typed
591 values do not need special treatment. This special type must be
592 different (in the `==' sense) from any base type coming from the
594 struct type
*address_type
= this->address_type ();
596 this->location
= DWARF_VALUE_MEMORY
;
597 this->initialized
= 1; /* Default is initialized. */
599 if (this->recursion_depth
> this->max_recursion_depth
)
600 error (_("DWARF-2 expression error: Loop detected (%d)."),
601 this->recursion_depth
);
602 this->recursion_depth
++;
604 while (op_ptr
< op_end
)
606 enum dwarf_location_atom op
= (enum dwarf_location_atom
) *op_ptr
++;
608 /* Assume the value is not in stack memory.
609 Code that knows otherwise sets this to 1.
610 Some arithmetic on stack addresses can probably be assumed to still
611 be a stack address, but we skip this complication for now.
612 This is just an optimization, so it's always ok to punt
613 and leave this as 0. */
614 int in_stack_memory
= 0;
615 uint64_t uoffset
, reg
;
617 struct value
*result_val
= NULL
;
619 /* The DWARF expression might have a bug causing an infinite
620 loop. In that case, quitting is the only way out. */
657 result
= op
- DW_OP_lit0
;
658 result_val
= value_from_ulongest (address_type
, result
);
662 result
= extract_unsigned_integer (op_ptr
,
663 this->addr_size
, byte_order
);
664 op_ptr
+= this->addr_size
;
665 /* Some versions of GCC emit DW_OP_addr before
666 DW_OP_GNU_push_tls_address. In this case the value is an
667 index, not an address. We don't support things like
668 branching between the address and the TLS op. */
669 if (op_ptr
>= op_end
|| *op_ptr
!= DW_OP_GNU_push_tls_address
)
670 result
+= this->offset
;
671 result_val
= value_from_ulongest (address_type
, result
);
674 case DW_OP_GNU_addr_index
:
675 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
676 result
= this->get_addr_index (uoffset
);
677 result
+= this->offset
;
678 result_val
= value_from_ulongest (address_type
, result
);
680 case DW_OP_GNU_const_index
:
681 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
682 result
= this->get_addr_index (uoffset
);
683 result_val
= value_from_ulongest (address_type
, result
);
687 result
= extract_unsigned_integer (op_ptr
, 1, byte_order
);
688 result_val
= value_from_ulongest (address_type
, result
);
692 result
= extract_signed_integer (op_ptr
, 1, byte_order
);
693 result_val
= value_from_ulongest (address_type
, result
);
697 result
= extract_unsigned_integer (op_ptr
, 2, byte_order
);
698 result_val
= value_from_ulongest (address_type
, result
);
702 result
= extract_signed_integer (op_ptr
, 2, byte_order
);
703 result_val
= value_from_ulongest (address_type
, result
);
707 result
= extract_unsigned_integer (op_ptr
, 4, byte_order
);
708 result_val
= value_from_ulongest (address_type
, result
);
712 result
= extract_signed_integer (op_ptr
, 4, byte_order
);
713 result_val
= value_from_ulongest (address_type
, result
);
717 result
= extract_unsigned_integer (op_ptr
, 8, byte_order
);
718 result_val
= value_from_ulongest (address_type
, result
);
722 result
= extract_signed_integer (op_ptr
, 8, byte_order
);
723 result_val
= value_from_ulongest (address_type
, result
);
727 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
729 result_val
= value_from_ulongest (address_type
, result
);
732 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
734 result_val
= value_from_ulongest (address_type
, result
);
737 /* The DW_OP_reg operations are required to occur alone in
738 location expressions. */
771 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_reg");
773 result
= op
- DW_OP_reg0
;
774 result_val
= value_from_ulongest (address_type
, result
);
775 this->location
= DWARF_VALUE_REGISTER
;
779 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
780 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
783 result_val
= value_from_ulongest (address_type
, result
);
784 this->location
= DWARF_VALUE_REGISTER
;
787 case DW_OP_implicit_value
:
791 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &len
);
792 if (op_ptr
+ len
> op_end
)
793 error (_("DW_OP_implicit_value: too few bytes available."));
796 this->location
= DWARF_VALUE_LITERAL
;
798 dwarf_expr_require_composition (op_ptr
, op_end
,
799 "DW_OP_implicit_value");
803 case DW_OP_stack_value
:
804 this->location
= DWARF_VALUE_STACK
;
805 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_stack_value");
808 case DW_OP_GNU_implicit_pointer
:
812 if (this->ref_addr_size
== -1)
813 error (_("DWARF-2 expression error: DW_OP_GNU_implicit_pointer "
814 "is not allowed in frame context"));
816 /* The referred-to DIE of sect_offset kind. */
817 this->len
= extract_unsigned_integer (op_ptr
, this->ref_addr_size
,
819 op_ptr
+= this->ref_addr_size
;
821 /* The byte offset into the data. */
822 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &len
);
823 result
= (ULONGEST
) len
;
824 result_val
= value_from_ulongest (address_type
, result
);
826 this->location
= DWARF_VALUE_IMPLICIT_POINTER
;
827 dwarf_expr_require_composition (op_ptr
, op_end
,
828 "DW_OP_GNU_implicit_pointer");
865 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
866 result
= this->read_addr_from_reg (op
- DW_OP_breg0
);
868 result_val
= value_from_ulongest (address_type
, result
);
873 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
874 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
875 result
= this->read_addr_from_reg (reg
);
877 result_val
= value_from_ulongest (address_type
, result
);
882 const gdb_byte
*datastart
;
884 unsigned int before_stack_len
;
886 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
887 /* Rather than create a whole new context, we simply
888 record the stack length before execution, then reset it
889 afterwards, effectively erasing whatever the recursive
891 before_stack_len
= this->stack_len
;
892 /* FIXME: cagney/2003-03-26: This code should be using
893 get_frame_base_address(), and then implement a dwarf2
894 specific this_base method. */
895 this->get_frame_base (&datastart
, &datalen
);
896 eval (datastart
, datalen
);
897 if (this->location
== DWARF_VALUE_MEMORY
)
898 result
= fetch_address (0);
899 else if (this->location
== DWARF_VALUE_REGISTER
)
900 result
= this->read_addr_from_reg (value_as_long (fetch (0)));
902 error (_("Not implemented: computing frame "
903 "base using explicit value operator"));
904 result
= result
+ offset
;
905 result_val
= value_from_ulongest (address_type
, result
);
907 this->stack_len
= before_stack_len
;
908 this->location
= DWARF_VALUE_MEMORY
;
913 result_val
= fetch (0);
914 in_stack_memory
= fetch_in_stack_memory (0);
923 result_val
= fetch (offset
);
924 in_stack_memory
= fetch_in_stack_memory (offset
);
929 struct dwarf_stack_value t1
, t2
;
931 if (this->stack_len
< 2)
932 error (_("Not enough elements for "
933 "DW_OP_swap. Need 2, have %d."),
935 t1
= this->stack
[this->stack_len
- 1];
936 t2
= this->stack
[this->stack_len
- 2];
937 this->stack
[this->stack_len
- 1] = t2
;
938 this->stack
[this->stack_len
- 2] = t1
;
943 result_val
= fetch (1);
944 in_stack_memory
= fetch_in_stack_memory (1);
949 struct dwarf_stack_value t1
, t2
, t3
;
951 if (this->stack_len
< 3)
952 error (_("Not enough elements for "
953 "DW_OP_rot. Need 3, have %d."),
955 t1
= this->stack
[this->stack_len
- 1];
956 t2
= this->stack
[this->stack_len
- 2];
957 t3
= this->stack
[this->stack_len
- 3];
958 this->stack
[this->stack_len
- 1] = t2
;
959 this->stack
[this->stack_len
- 2] = t3
;
960 this->stack
[this->stack_len
- 3] = t1
;
965 case DW_OP_deref_size
:
966 case DW_OP_GNU_deref_type
:
968 int addr_size
= (op
== DW_OP_deref
? this->addr_size
: *op_ptr
++);
969 gdb_byte
*buf
= (gdb_byte
*) alloca (addr_size
);
970 CORE_ADDR addr
= fetch_address (0);
975 if (op
== DW_OP_GNU_deref_type
)
979 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
980 type_die
.cu_off
= uoffset
;
981 type
= get_base_type (type_die
, 0);
986 this->read_mem (buf
, addr
, addr_size
);
988 /* If the size of the object read from memory is different
989 from the type length, we need to zero-extend it. */
990 if (TYPE_LENGTH (type
) != addr_size
)
993 extract_unsigned_integer (buf
, addr_size
, byte_order
);
995 buf
= (gdb_byte
*) alloca (TYPE_LENGTH (type
));
996 store_unsigned_integer (buf
, TYPE_LENGTH (type
),
1000 result_val
= value_from_contents_and_address (type
, buf
, addr
);
1007 case DW_OP_plus_uconst
:
1009 /* Unary operations. */
1010 result_val
= fetch (0);
1016 if (value_less (result_val
,
1017 value_zero (value_type (result_val
), not_lval
)))
1018 result_val
= value_neg (result_val
);
1021 result_val
= value_neg (result_val
);
1024 dwarf_require_integral (value_type (result_val
));
1025 result_val
= value_complement (result_val
);
1027 case DW_OP_plus_uconst
:
1028 dwarf_require_integral (value_type (result_val
));
1029 result
= value_as_long (result_val
);
1030 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
1032 result_val
= value_from_ulongest (address_type
, result
);
1056 /* Binary operations. */
1057 struct value
*first
, *second
;
1065 if (! base_types_equal_p (value_type (first
), value_type (second
)))
1066 error (_("Incompatible types on DWARF stack"));
1071 dwarf_require_integral (value_type (first
));
1072 dwarf_require_integral (value_type (second
));
1073 result_val
= value_binop (first
, second
, BINOP_BITWISE_AND
);
1076 result_val
= value_binop (first
, second
, BINOP_DIV
);
1079 result_val
= value_binop (first
, second
, BINOP_SUB
);
1084 struct type
*orig_type
= value_type (first
);
1086 /* We have to special-case "old-style" untyped values
1087 -- these must have mod computed using unsigned
1089 if (orig_type
== address_type
)
1092 = get_unsigned_type (this->gdbarch
, orig_type
);
1095 first
= value_cast (utype
, first
);
1096 second
= value_cast (utype
, second
);
1098 /* Note that value_binop doesn't handle float or
1099 decimal float here. This seems unimportant. */
1100 result_val
= value_binop (first
, second
, BINOP_MOD
);
1102 result_val
= value_cast (orig_type
, result_val
);
1106 result_val
= value_binop (first
, second
, BINOP_MUL
);
1109 dwarf_require_integral (value_type (first
));
1110 dwarf_require_integral (value_type (second
));
1111 result_val
= value_binop (first
, second
, BINOP_BITWISE_IOR
);
1114 result_val
= value_binop (first
, second
, BINOP_ADD
);
1117 dwarf_require_integral (value_type (first
));
1118 dwarf_require_integral (value_type (second
));
1119 result_val
= value_binop (first
, second
, BINOP_LSH
);
1122 dwarf_require_integral (value_type (first
));
1123 dwarf_require_integral (value_type (second
));
1124 if (!TYPE_UNSIGNED (value_type (first
)))
1127 = get_unsigned_type (this->gdbarch
, value_type (first
));
1129 first
= value_cast (utype
, first
);
1132 result_val
= value_binop (first
, second
, BINOP_RSH
);
1133 /* Make sure we wind up with the same type we started
1135 if (value_type (result_val
) != value_type (second
))
1136 result_val
= value_cast (value_type (second
), result_val
);
1139 dwarf_require_integral (value_type (first
));
1140 dwarf_require_integral (value_type (second
));
1141 if (TYPE_UNSIGNED (value_type (first
)))
1144 = get_signed_type (this->gdbarch
, value_type (first
));
1146 first
= value_cast (stype
, first
);
1149 result_val
= value_binop (first
, second
, BINOP_RSH
);
1150 /* Make sure we wind up with the same type we started
1152 if (value_type (result_val
) != value_type (second
))
1153 result_val
= value_cast (value_type (second
), result_val
);
1156 dwarf_require_integral (value_type (first
));
1157 dwarf_require_integral (value_type (second
));
1158 result_val
= value_binop (first
, second
, BINOP_BITWISE_XOR
);
1161 /* A <= B is !(B < A). */
1162 result
= ! value_less (second
, first
);
1163 result_val
= value_from_ulongest (address_type
, result
);
1166 /* A >= B is !(A < B). */
1167 result
= ! value_less (first
, second
);
1168 result_val
= value_from_ulongest (address_type
, result
);
1171 result
= value_equal (first
, second
);
1172 result_val
= value_from_ulongest (address_type
, result
);
1175 result
= value_less (first
, second
);
1176 result_val
= value_from_ulongest (address_type
, result
);
1179 /* A > B is B < A. */
1180 result
= value_less (second
, first
);
1181 result_val
= value_from_ulongest (address_type
, result
);
1184 result
= ! value_equal (first
, second
);
1185 result_val
= value_from_ulongest (address_type
, result
);
1188 internal_error (__FILE__
, __LINE__
,
1189 _("Can't be reached."));
1194 case DW_OP_call_frame_cfa
:
1195 result
= this->get_frame_cfa ();
1196 result_val
= value_from_ulongest (address_type
, result
);
1197 in_stack_memory
= 1;
1200 case DW_OP_GNU_push_tls_address
:
1201 case DW_OP_form_tls_address
:
1202 /* Variable is at a constant offset in the thread-local
1203 storage block into the objfile for the current thread and
1204 the dynamic linker module containing this expression. Here
1205 we return returns the offset from that base. The top of the
1206 stack has the offset from the beginning of the thread
1207 control block at which the variable is located. Nothing
1208 should follow this operator, so the top of stack would be
1210 result
= value_as_long (fetch (0));
1212 result
= this->get_tls_address (result
);
1213 result_val
= value_from_ulongest (address_type
, result
);
1217 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
1226 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
1229 dwarf_require_integral (value_type (val
));
1230 if (value_as_long (val
) != 0)
1243 /* Record the piece. */
1244 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &size
);
1245 add_piece (8 * size
, 0);
1247 /* Pop off the address/regnum, and reset the location
1249 if (this->location
!= DWARF_VALUE_LITERAL
1250 && this->location
!= DWARF_VALUE_OPTIMIZED_OUT
)
1252 this->location
= DWARF_VALUE_MEMORY
;
1256 case DW_OP_bit_piece
:
1258 uint64_t size
, offset
;
1260 /* Record the piece. */
1261 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &size
);
1262 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &offset
);
1263 add_piece (size
, offset
);
1265 /* Pop off the address/regnum, and reset the location
1267 if (this->location
!= DWARF_VALUE_LITERAL
1268 && this->location
!= DWARF_VALUE_OPTIMIZED_OUT
)
1270 this->location
= DWARF_VALUE_MEMORY
;
1274 case DW_OP_GNU_uninit
:
1275 if (op_ptr
!= op_end
)
1276 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
1277 "be the very last op."));
1279 this->initialized
= 0;
1286 offset
.cu_off
= extract_unsigned_integer (op_ptr
, 2, byte_order
);
1288 this->dwarf_call (offset
);
1296 offset
.cu_off
= extract_unsigned_integer (op_ptr
, 4, byte_order
);
1298 this->dwarf_call (offset
);
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_GNU_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_GNU_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
;
1344 kind_u
.param_offset
.cu_off
= extract_unsigned_integer (op_ptr
, 4,
1347 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_PARAM_OFFSET
,
1349 -1 /* deref_size */);
1353 case DW_OP_GNU_const_type
:
1357 const gdb_byte
*data
;
1360 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
1361 type_die
.cu_off
= uoffset
;
1366 type
= get_base_type (type_die
, n
);
1367 result_val
= value_from_contents (type
, data
);
1371 case DW_OP_GNU_regval_type
:
1376 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
1377 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
1378 type_die
.cu_off
= uoffset
;
1380 type
= get_base_type (type_die
, 0);
1381 result_val
= this->get_reg_value (type
, reg
);
1385 case DW_OP_GNU_convert
:
1386 case DW_OP_GNU_reinterpret
:
1391 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
1392 type_die
.cu_off
= uoffset
;
1394 if (type_die
.cu_off
== 0)
1395 type
= address_type
;
1397 type
= get_base_type (type_die
, 0);
1399 result_val
= fetch (0);
1402 if (op
== DW_OP_GNU_convert
)
1403 result_val
= value_cast (type
, result_val
);
1404 else if (type
== value_type (result_val
))
1408 else if (TYPE_LENGTH (type
)
1409 != TYPE_LENGTH (value_type (result_val
)))
1410 error (_("DW_OP_GNU_reinterpret has wrong size"));
1413 = value_from_contents (type
,
1414 value_contents_all (result_val
));
1418 case DW_OP_push_object_address
:
1419 /* Return the address of the object we are currently observing. */
1420 result
= this->get_object_address ();
1421 result_val
= value_from_ulongest (address_type
, result
);
1425 error (_("Unhandled dwarf expression opcode 0x%x"), op
);
1428 /* Most things push a result value. */
1429 gdb_assert (result_val
!= NULL
);
1430 push (result_val
, in_stack_memory
);
1435 /* To simplify our main caller, if the result is an implicit
1436 pointer, then make a pieced value. This is ok because we can't
1437 have implicit pointers in contexts where pieces are invalid. */
1438 if (this->location
== DWARF_VALUE_IMPLICIT_POINTER
)
1439 add_piece (8 * this->addr_size
, 0);
1442 this->recursion_depth
--;
1443 gdb_assert (this->recursion_depth
>= 0);
1446 /* Provide a prototype to silence -Wmissing-prototypes. */
1447 extern initialize_file_ftype _initialize_dwarf2expr
;
1450 _initialize_dwarf2expr (void)
1453 = gdbarch_data_register_post_init (dwarf_gdbarch_types_init
);