1 /* DWARF 2 Expression Evaluator.
3 Copyright (C) 2001-2022 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/>. */
29 #include "dwarf2/expr.h"
30 #include "dwarf2/loc.h"
31 #include "dwarf2/read.h"
33 #include "gdbsupport/underlying.h"
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 /* Cookie for gdbarch data. */
47 static const registry
<gdbarch
>::key
<dwarf_gdbarch_types
> dwarf_arch_cookie
;
49 /* Ensure that a FRAME is defined, throw an exception otherwise. */
52 ensure_have_frame (frame_info
*frame
, const char *op_name
)
55 throw_error (GENERIC_ERROR
,
56 _("%s evaluation requires a frame."), op_name
);
59 /* Ensure that a PER_CU is defined and throw an exception otherwise. */
62 ensure_have_per_cu (dwarf2_per_cu_data
*per_cu
, const char* op_name
)
64 if (per_cu
== nullptr)
65 throw_error (GENERIC_ERROR
,
66 _("%s evaluation requires a compilation unit."), op_name
);
69 /* Return the number of bytes overlapping a contiguous chunk of N_BITS
70 bits whose first bit is located at bit offset START. */
73 bits_to_bytes (ULONGEST start
, ULONGEST n_bits
)
75 return (start
% HOST_CHAR_BIT
+ n_bits
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
;
81 read_addr_from_reg (frame_info
*frame
, int reg
)
83 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
84 int regnum
= dwarf_reg_to_regnum_or_error (gdbarch
, reg
);
86 return address_from_register (regnum
, frame
);
91 /* Reference count. */
94 /* The objfile from which this closure's expression came. */
95 dwarf2_per_objfile
*per_objfile
= nullptr;
97 /* The CU from which this closure's expression came. */
98 dwarf2_per_cu_data
*per_cu
= nullptr;
100 /* The pieces describing this variable. */
101 std::vector
<dwarf_expr_piece
> pieces
;
103 /* Frame ID of frame to which a register value is relative, used
104 only by DWARF_VALUE_REGISTER. */
105 struct frame_id frame_id
;
108 /* Allocate a closure for a value formed from separately-described
111 static piece_closure
*
112 allocate_piece_closure (dwarf2_per_cu_data
*per_cu
,
113 dwarf2_per_objfile
*per_objfile
,
114 std::vector
<dwarf_expr_piece
> &&pieces
,
117 piece_closure
*c
= new piece_closure
;
120 /* We must capture this here due to sharing of DWARF state. */
121 c
->per_objfile
= per_objfile
;
123 c
->pieces
= std::move (pieces
);
124 if (frame
== nullptr)
125 c
->frame_id
= null_frame_id
;
127 c
->frame_id
= get_frame_id (frame
);
129 for (dwarf_expr_piece
&piece
: c
->pieces
)
130 if (piece
.location
== DWARF_VALUE_STACK
)
131 value_incref (piece
.v
.value
);
136 /* Read or write a pieced value V. If FROM != NULL, operate in "write
137 mode": copy FROM into the pieces comprising V. If FROM == NULL,
138 operate in "read mode": fetch the contents of the (lazy) value V by
139 composing it from its pieces. If CHECK_OPTIMIZED is true, then no
140 reading or writing is done; instead the return value of this
141 function is true if any piece is optimized out. When
142 CHECK_OPTIMIZED is true, FROM must be nullptr. */
145 rw_pieced_value (value
*v
, value
*from
, bool check_optimized
)
148 LONGEST offset
= 0, max_offset
;
149 gdb_byte
*v_contents
;
150 const gdb_byte
*from_contents
;
152 = (piece_closure
*) value_computed_closure (v
);
153 gdb::byte_vector buffer
;
154 bool bits_big_endian
= type_byte_order (value_type (v
)) == BFD_ENDIAN_BIG
;
156 gdb_assert (!check_optimized
|| from
== nullptr);
159 from_contents
= value_contents (from
).data ();
160 v_contents
= nullptr;
164 if (value_type (v
) != value_enclosing_type (v
))
165 internal_error (__FILE__
, __LINE__
,
166 _("Should not be able to create a lazy value with "
167 "an enclosing type"));
169 v_contents
= nullptr;
171 v_contents
= value_contents_raw (v
).data ();
172 from_contents
= nullptr;
175 ULONGEST bits_to_skip
= 8 * value_offset (v
);
176 if (value_bitsize (v
))
178 bits_to_skip
+= (8 * value_offset (value_parent (v
))
181 && (type_byte_order (value_type (from
))
184 /* Use the least significant bits of FROM. */
185 max_offset
= 8 * value_type (from
)->length ();
186 offset
= max_offset
- value_bitsize (v
);
189 max_offset
= value_bitsize (v
);
192 max_offset
= 8 * value_type (v
)->length ();
194 /* Advance to the first non-skipped piece. */
195 for (i
= 0; i
< c
->pieces
.size () && bits_to_skip
>= c
->pieces
[i
].size
; i
++)
196 bits_to_skip
-= c
->pieces
[i
].size
;
198 for (; i
< c
->pieces
.size () && offset
< max_offset
; i
++)
200 dwarf_expr_piece
*p
= &c
->pieces
[i
];
201 size_t this_size_bits
, this_size
;
203 this_size_bits
= p
->size
- bits_to_skip
;
204 if (this_size_bits
> max_offset
- offset
)
205 this_size_bits
= max_offset
- offset
;
209 case DWARF_VALUE_REGISTER
:
211 frame_info
*frame
= frame_find_by_id (c
->frame_id
);
212 gdbarch
*arch
= get_frame_arch (frame
);
213 int gdb_regnum
= dwarf_reg_to_regnum_or_error (arch
, p
->v
.regno
);
214 ULONGEST reg_bits
= 8 * register_size (arch
, gdb_regnum
);
217 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
218 && p
->offset
+ p
->size
< reg_bits
)
220 /* Big-endian, and we want less than full size. */
221 bits_to_skip
+= reg_bits
- (p
->offset
+ p
->size
);
224 bits_to_skip
+= p
->offset
;
226 this_size
= bits_to_bytes (bits_to_skip
, this_size_bits
);
227 buffer
.resize (this_size
);
232 if (!get_frame_register_bytes (frame
, gdb_regnum
,
234 buffer
, &optim
, &unavail
))
240 mark_value_bits_optimized_out (v
, offset
,
243 if (unavail
&& !check_optimized
)
244 mark_value_bits_unavailable (v
, offset
,
249 if (!check_optimized
)
250 copy_bitwise (v_contents
, offset
,
251 buffer
.data (), bits_to_skip
% 8,
252 this_size_bits
, bits_big_endian
);
257 if (bits_to_skip
% 8 != 0 || this_size_bits
% 8 != 0)
259 /* Data is copied non-byte-aligned into the register.
260 Need some bits from original register value. */
261 get_frame_register_bytes (frame
, gdb_regnum
,
263 buffer
, &optim
, &unavail
);
265 throw_error (OPTIMIZED_OUT_ERROR
,
266 _("Can't do read-modify-write to "
267 "update bitfield; containing word "
268 "has been optimized out"));
270 throw_error (NOT_AVAILABLE_ERROR
,
271 _("Can't do read-modify-write to "
272 "update bitfield; containing word "
276 copy_bitwise (buffer
.data (), bits_to_skip
% 8,
277 from_contents
, offset
,
278 this_size_bits
, bits_big_endian
);
279 put_frame_register_bytes (frame
, gdb_regnum
,
286 case DWARF_VALUE_MEMORY
:
291 bits_to_skip
+= p
->offset
;
293 CORE_ADDR start_addr
= p
->v
.mem
.addr
+ bits_to_skip
/ 8;
295 if (bits_to_skip
% 8 == 0 && this_size_bits
% 8 == 0
298 /* Everything is byte-aligned; no buffer needed. */
300 write_memory_with_notification (start_addr
,
305 read_value_memory (v
, offset
,
306 p
->v
.mem
.in_stack_memory
,
307 p
->v
.mem
.addr
+ bits_to_skip
/ 8,
308 v_contents
+ offset
/ 8,
313 this_size
= bits_to_bytes (bits_to_skip
, this_size_bits
);
314 buffer
.resize (this_size
);
319 read_value_memory (v
, offset
,
320 p
->v
.mem
.in_stack_memory
,
321 p
->v
.mem
.addr
+ bits_to_skip
/ 8,
322 buffer
.data (), this_size
);
323 copy_bitwise (v_contents
, offset
,
324 buffer
.data (), bits_to_skip
% 8,
325 this_size_bits
, bits_big_endian
);
330 if (bits_to_skip
% 8 != 0 || this_size_bits
% 8 != 0)
334 /* Perform a single read for small sizes. */
335 read_memory (start_addr
, buffer
.data (),
340 /* Only the first and last bytes can possibly have
342 read_memory (start_addr
, buffer
.data (), 1);
343 read_memory (start_addr
+ this_size
- 1,
344 &buffer
[this_size
- 1], 1);
348 copy_bitwise (buffer
.data (), bits_to_skip
% 8,
349 from_contents
, offset
,
350 this_size_bits
, bits_big_endian
);
351 write_memory_with_notification (start_addr
,
358 case DWARF_VALUE_STACK
:
365 mark_value_bits_optimized_out (v
, offset
, this_size_bits
);
369 gdbarch
*objfile_gdbarch
= c
->per_objfile
->objfile
->arch ();
370 ULONGEST stack_value_size_bits
371 = 8 * value_type (p
->v
.value
)->length ();
373 /* Use zeroes if piece reaches beyond stack value. */
374 if (p
->offset
+ p
->size
> stack_value_size_bits
)
377 /* Piece is anchored at least significant bit end. */
378 if (gdbarch_byte_order (objfile_gdbarch
) == BFD_ENDIAN_BIG
)
379 bits_to_skip
+= stack_value_size_bits
- p
->offset
- p
->size
;
381 bits_to_skip
+= p
->offset
;
383 copy_bitwise (v_contents
, offset
,
384 value_contents_all (p
->v
.value
).data (),
386 this_size_bits
, bits_big_endian
);
390 case DWARF_VALUE_LITERAL
:
397 mark_value_bits_optimized_out (v
, offset
, this_size_bits
);
401 ULONGEST literal_size_bits
= 8 * p
->v
.literal
.length
;
402 size_t n
= this_size_bits
;
404 /* Cut off at the end of the implicit value. */
405 bits_to_skip
+= p
->offset
;
406 if (bits_to_skip
>= literal_size_bits
)
408 if (n
> literal_size_bits
- bits_to_skip
)
409 n
= literal_size_bits
- bits_to_skip
;
411 copy_bitwise (v_contents
, offset
,
412 p
->v
.literal
.data
, bits_to_skip
,
417 case DWARF_VALUE_IMPLICIT_POINTER
:
420 mark_value_bits_optimized_out (v
, offset
, this_size_bits
);
424 /* These bits show up as zeros -- but do not cause the value to
425 be considered optimized-out. */
428 case DWARF_VALUE_OPTIMIZED_OUT
:
431 mark_value_bits_optimized_out (v
, offset
, this_size_bits
);
435 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
438 offset
+= this_size_bits
;
446 read_pieced_value (value
*v
)
448 rw_pieced_value (v
, nullptr, false);
452 write_pieced_value (value
*to
, value
*from
)
454 rw_pieced_value (to
, from
, false);
458 is_optimized_out_pieced_value (value
*v
)
460 return rw_pieced_value (v
, nullptr, true);
463 /* An implementation of an lval_funcs method to see whether a value is
464 a synthetic pointer. */
467 check_pieced_synthetic_pointer (const value
*value
, LONGEST bit_offset
,
470 piece_closure
*c
= (piece_closure
*) value_computed_closure (value
);
473 bit_offset
+= 8 * value_offset (value
);
474 if (value_bitsize (value
))
475 bit_offset
+= value_bitpos (value
);
477 for (i
= 0; i
< c
->pieces
.size () && bit_length
> 0; i
++)
479 dwarf_expr_piece
*p
= &c
->pieces
[i
];
480 size_t this_size_bits
= p
->size
;
484 if (bit_offset
>= this_size_bits
)
486 bit_offset
-= this_size_bits
;
490 bit_length
-= this_size_bits
- bit_offset
;
494 bit_length
-= this_size_bits
;
496 if (p
->location
!= DWARF_VALUE_IMPLICIT_POINTER
)
503 /* An implementation of an lval_funcs method to indirect through a
504 pointer. This handles the synthetic pointer case when needed. */
507 indirect_pieced_value (value
*value
)
510 = (piece_closure
*) value_computed_closure (value
);
512 dwarf_expr_piece
*piece
= NULL
;
514 struct type
*type
= check_typedef (value_type (value
));
515 if (type
->code () != TYPE_CODE_PTR
)
518 int bit_length
= 8 * type
->length ();
519 LONGEST bit_offset
= 8 * value_offset (value
);
520 if (value_bitsize (value
))
521 bit_offset
+= value_bitpos (value
);
523 for (i
= 0; i
< c
->pieces
.size () && bit_length
> 0; i
++)
525 dwarf_expr_piece
*p
= &c
->pieces
[i
];
526 size_t this_size_bits
= p
->size
;
530 if (bit_offset
>= this_size_bits
)
532 bit_offset
-= this_size_bits
;
536 bit_length
-= this_size_bits
- bit_offset
;
540 bit_length
-= this_size_bits
;
542 if (p
->location
!= DWARF_VALUE_IMPLICIT_POINTER
)
546 error (_("Invalid use of DW_OP_implicit_pointer"));
552 gdb_assert (piece
!= NULL
&& c
->per_cu
!= nullptr);
553 frame_info
*frame
= get_selected_frame (_("No frame selected."));
555 /* This is an offset requested by GDB, such as value subscripts.
556 However, due to how synthetic pointers are implemented, this is
557 always presented to us as a pointer type. This means we have to
558 sign-extend it manually as appropriate. Use raw
559 extract_signed_integer directly rather than value_as_address and
560 sign extend afterwards on architectures that would need it
561 (mostly everywhere except MIPS, which has signed addresses) as
562 the later would go through gdbarch_pointer_to_address and thus
563 return a CORE_ADDR with high bits set on architectures that
564 encode address spaces and other things in CORE_ADDR. */
565 bfd_endian byte_order
= gdbarch_byte_order (get_frame_arch (frame
));
567 = extract_signed_integer (value_contents (value
), byte_order
);
568 byte_offset
+= piece
->v
.ptr
.offset
;
570 return indirect_synthetic_pointer (piece
->v
.ptr
.die_sect_off
,
571 byte_offset
, c
->per_cu
,
572 c
->per_objfile
, frame
, type
);
575 /* Implementation of the coerce_ref method of lval_funcs for synthetic C++
579 coerce_pieced_ref (const value
*value
)
581 struct type
*type
= check_typedef (value_type (value
));
583 if (value_bits_synthetic_pointer (value
, value_embedded_offset (value
),
584 TARGET_CHAR_BIT
* type
->length ()))
586 const piece_closure
*closure
587 = (piece_closure
*) value_computed_closure (value
);
589 = get_selected_frame (_("No frame selected."));
591 /* gdb represents synthetic pointers as pieced values with a single
593 gdb_assert (closure
!= NULL
);
594 gdb_assert (closure
->pieces
.size () == 1);
596 return indirect_synthetic_pointer
597 (closure
->pieces
[0].v
.ptr
.die_sect_off
,
598 closure
->pieces
[0].v
.ptr
.offset
,
599 closure
->per_cu
, closure
->per_objfile
, frame
, type
);
603 /* Else: not a synthetic reference; do nothing. */
609 copy_pieced_value_closure (const value
*v
)
611 piece_closure
*c
= (piece_closure
*) value_computed_closure (v
);
618 free_pieced_value_closure (value
*v
)
620 piece_closure
*c
= (piece_closure
*) value_computed_closure (v
);
625 for (dwarf_expr_piece
&p
: c
->pieces
)
626 if (p
.location
== DWARF_VALUE_STACK
)
627 value_decref (p
.v
.value
);
633 /* Functions for accessing a variable described by DW_OP_piece. */
634 static const struct lval_funcs pieced_value_funcs
= {
637 is_optimized_out_pieced_value
,
638 indirect_pieced_value
,
640 check_pieced_synthetic_pointer
,
641 copy_pieced_value_closure
,
642 free_pieced_value_closure
645 /* Given context CTX, section offset SECT_OFF, and compilation unit
646 data PER_CU, execute the "variable value" operation on the DIE
647 found at SECT_OFF. */
650 sect_variable_value (sect_offset sect_off
,
651 dwarf2_per_cu_data
*per_cu
,
652 dwarf2_per_objfile
*per_objfile
)
654 const char *var_name
= nullptr;
655 struct type
*die_type
656 = dwarf2_fetch_die_type_sect_off (sect_off
, per_cu
, per_objfile
,
659 if (die_type
== NULL
)
660 error (_("Bad DW_OP_GNU_variable_value DIE."));
662 /* Note: Things still work when the following test is removed. This
663 test and error is here to conform to the proposed specification. */
664 if (die_type
->code () != TYPE_CODE_INT
665 && die_type
->code () != TYPE_CODE_ENUM
666 && die_type
->code () != TYPE_CODE_RANGE
667 && die_type
->code () != TYPE_CODE_PTR
)
668 error (_("Type of DW_OP_GNU_variable_value DIE must be an integer or pointer."));
670 if (var_name
!= nullptr)
672 value
*result
= compute_var_value (var_name
);
673 if (result
!= nullptr)
677 struct type
*type
= lookup_pointer_type (die_type
);
678 frame_info
*frame
= get_selected_frame (_("No frame selected."));
679 return indirect_synthetic_pointer (sect_off
, 0, per_cu
, per_objfile
, frame
,
683 /* Return the type used for DWARF operations where the type is
684 unspecified in the DWARF spec. Only certain sizes are
688 dwarf_expr_context::address_type () const
690 gdbarch
*arch
= this->m_per_objfile
->objfile
->arch ();
691 dwarf_gdbarch_types
*types
= dwarf_arch_cookie
.get (arch
);
692 if (types
== nullptr)
693 types
= dwarf_arch_cookie
.emplace (arch
);
696 if (this->m_addr_size
== 2)
698 else if (this->m_addr_size
== 4)
700 else if (this->m_addr_size
== 8)
703 error (_("Unsupported address size in DWARF expressions: %d bits"),
704 8 * this->m_addr_size
);
706 if (types
->dw_types
[ndx
] == NULL
)
708 = arch_integer_type (arch
, 8 * this->m_addr_size
,
709 0, "<signed DWARF address type>");
711 return types
->dw_types
[ndx
];
714 /* Create a new context for the expression evaluator. */
716 dwarf_expr_context::dwarf_expr_context (dwarf2_per_objfile
*per_objfile
,
718 : m_addr_size (addr_size
),
719 m_per_objfile (per_objfile
)
723 /* Push VALUE onto the stack. */
726 dwarf_expr_context::push (struct value
*value
, bool in_stack_memory
)
728 this->m_stack
.emplace_back (value
, in_stack_memory
);
731 /* Push VALUE onto the stack. */
734 dwarf_expr_context::push_address (CORE_ADDR value
, bool in_stack_memory
)
736 push (value_from_ulongest (address_type (), value
), in_stack_memory
);
739 /* Pop the top item off of the stack. */
742 dwarf_expr_context::pop ()
744 if (this->m_stack
.empty ())
745 error (_("dwarf expression stack underflow"));
747 this->m_stack
.pop_back ();
750 /* Retrieve the N'th item on the stack. */
753 dwarf_expr_context::fetch (int n
)
755 if (this->m_stack
.size () <= n
)
756 error (_("Asked for position %d of stack, "
757 "stack only has %zu elements on it."),
758 n
, this->m_stack
.size ());
759 return this->m_stack
[this->m_stack
.size () - (1 + n
)].value
;
765 dwarf_expr_context::get_frame_base (const gdb_byte
**start
,
768 ensure_have_frame (this->m_frame
, "DW_OP_fbreg");
770 const block
*bl
= get_frame_block (this->m_frame
, NULL
);
773 error (_("frame address is not available."));
775 /* Use block_linkage_function, which returns a real (not inlined)
776 function, instead of get_frame_function, which may return an
778 symbol
*framefunc
= block_linkage_function (bl
);
780 /* If we found a frame-relative symbol then it was certainly within
781 some function associated with a frame. If we can't find the frame,
782 something has gone wrong. */
783 gdb_assert (framefunc
!= NULL
);
785 func_get_frame_base_dwarf_block (framefunc
,
786 get_frame_address_in_block (this->m_frame
),
793 dwarf_expr_context::get_base_type (cu_offset die_cu_off
)
795 if (this->m_per_cu
== nullptr)
796 return builtin_type (this->m_per_objfile
->objfile
->arch ())->builtin_int
;
798 struct type
*result
= dwarf2_get_die_type (die_cu_off
, this->m_per_cu
,
799 this->m_per_objfile
);
801 if (result
== nullptr)
802 error (_("Could not find type for operation"));
810 dwarf_expr_context::dwarf_call (cu_offset die_cu_off
)
812 ensure_have_per_cu (this->m_per_cu
, "DW_OP_call");
814 frame_info
*frame
= this->m_frame
;
816 auto get_pc_from_frame
= [frame
] ()
818 ensure_have_frame (frame
, "DW_OP_call");
819 return get_frame_address_in_block (frame
);
822 dwarf2_locexpr_baton block
823 = dwarf2_fetch_die_loc_cu_off (die_cu_off
, this->m_per_cu
,
824 this->m_per_objfile
, get_pc_from_frame
);
826 /* DW_OP_call_ref is currently not supported. */
827 gdb_assert (block
.per_cu
== this->m_per_cu
);
829 this->eval (block
.data
, block
.size
);
835 dwarf_expr_context::read_mem (gdb_byte
*buf
, CORE_ADDR addr
,
841 /* Prefer the passed-in memory, if it exists. */
842 if (this->m_addr_info
!= nullptr)
844 CORE_ADDR offset
= addr
- this->m_addr_info
->addr
;
846 if (offset
< this->m_addr_info
->valaddr
.size ()
847 && offset
+ length
<= this->m_addr_info
->valaddr
.size ())
849 memcpy (buf
, this->m_addr_info
->valaddr
.data (), length
);
854 read_memory (addr
, buf
, length
);
860 dwarf_expr_context::push_dwarf_reg_entry_value (call_site_parameter_kind kind
,
861 call_site_parameter_u kind_u
,
864 ensure_have_per_cu (this->m_per_cu
, "DW_OP_entry_value");
865 ensure_have_frame (this->m_frame
, "DW_OP_entry_value");
867 dwarf2_per_cu_data
*caller_per_cu
;
868 dwarf2_per_objfile
*caller_per_objfile
;
869 frame_info
*caller_frame
= get_prev_frame (this->m_frame
);
870 call_site_parameter
*parameter
871 = dwarf_expr_reg_to_entry_parameter (this->m_frame
, kind
, kind_u
,
873 &caller_per_objfile
);
874 const gdb_byte
*data_src
875 = deref_size
== -1 ? parameter
->value
: parameter
->data_value
;
877 = deref_size
== -1 ? parameter
->value_size
: parameter
->data_value_size
;
879 /* DEREF_SIZE size is not verified here. */
880 if (data_src
== nullptr)
881 throw_error (NO_ENTRY_VALUE_ERROR
,
882 _("Cannot resolve DW_AT_call_data_value"));
884 /* We are about to evaluate an expression in the context of the caller
885 of the current frame. This evaluation context may be different from
886 the current (callee's) context), so temporarily set the caller's context.
888 It is possible for the caller to be from a different objfile from the
889 callee if the call is made through a function pointer. */
890 scoped_restore save_frame
= make_scoped_restore (&this->m_frame
,
892 scoped_restore save_per_cu
= make_scoped_restore (&this->m_per_cu
,
894 scoped_restore save_addr_info
= make_scoped_restore (&this->m_addr_info
,
896 scoped_restore save_per_objfile
= make_scoped_restore (&this->m_per_objfile
,
899 scoped_restore save_addr_size
= make_scoped_restore (&this->m_addr_size
);
900 this->m_addr_size
= this->m_per_cu
->addr_size ();
902 this->eval (data_src
, size
);
908 dwarf_expr_context::fetch_result (struct type
*type
, struct type
*subobj_type
,
909 LONGEST subobj_offset
, bool as_lval
)
911 value
*retval
= nullptr;
912 gdbarch
*arch
= this->m_per_objfile
->objfile
->arch ();
915 type
= address_type ();
917 if (subobj_type
== nullptr)
920 /* Ensure that, if TYPE or SUBOBJ_TYPE are typedefs, their length is filled
921 in instead of being zero. */
922 check_typedef (type
);
923 check_typedef (subobj_type
);
925 if (this->m_pieces
.size () > 0)
927 ULONGEST bit_size
= 0;
929 for (dwarf_expr_piece
&piece
: this->m_pieces
)
930 bit_size
+= piece
.size
;
931 /* Complain if the expression is larger than the size of the
933 if (bit_size
> 8 * type
->length ())
934 invalid_synthetic_pointer ();
937 = allocate_piece_closure (this->m_per_cu
, this->m_per_objfile
,
938 std::move (this->m_pieces
), this->m_frame
);
939 retval
= allocate_computed_value (subobj_type
,
940 &pieced_value_funcs
, c
);
941 set_value_offset (retval
, subobj_offset
);
945 /* If AS_LVAL is false, means that the implicit conversion
946 from a location description to value is expected. */
948 this->m_location
= DWARF_VALUE_STACK
;
950 switch (this->m_location
)
952 case DWARF_VALUE_REGISTER
:
954 gdbarch
*f_arch
= get_frame_arch (this->m_frame
);
956 = longest_to_int (value_as_long (this->fetch (0)));
957 int gdb_regnum
= dwarf_reg_to_regnum_or_error (f_arch
,
960 if (subobj_offset
!= 0)
961 error (_("cannot use offset on synthetic pointer to register"));
963 gdb_assert (this->m_frame
!= NULL
);
965 retval
= value_from_register (subobj_type
, gdb_regnum
,
967 if (value_optimized_out (retval
))
969 /* This means the register has undefined value / was
970 not saved. As we're computing the location of some
971 variable etc. in the program, not a value for
972 inspecting a register ($pc, $sp, etc.), return a
973 generic optimized out value instead, so that we show
974 <optimized out> instead of <not saved>. */
975 value
*tmp
= allocate_value (subobj_type
);
976 value_contents_copy (tmp
, 0, retval
, 0,
977 subobj_type
->length ());
983 case DWARF_VALUE_MEMORY
:
985 struct type
*ptr_type
;
986 CORE_ADDR address
= this->fetch_address (0);
987 bool in_stack_memory
= this->fetch_in_stack_memory (0);
989 /* DW_OP_deref_size (and possibly other operations too) may
990 create a pointer instead of an address. Ideally, the
991 pointer to address conversion would be performed as part
992 of those operations, but the type of the object to
993 which the address refers is not known at the time of
994 the operation. Therefore, we do the conversion here
995 since the type is readily available. */
997 switch (subobj_type
->code ())
1000 case TYPE_CODE_METHOD
:
1001 ptr_type
= builtin_type (arch
)->builtin_func_ptr
;
1004 ptr_type
= builtin_type (arch
)->builtin_data_ptr
;
1007 address
= value_as_address (value_from_pointer (ptr_type
, address
));
1009 retval
= value_at_lazy (subobj_type
,
1010 address
+ subobj_offset
);
1011 if (in_stack_memory
)
1012 set_value_stack (retval
, 1);
1016 case DWARF_VALUE_STACK
:
1018 value
*val
= this->fetch (0);
1019 size_t n
= value_type (val
)->length ();
1020 size_t len
= subobj_type
->length ();
1021 size_t max
= type
->length ();
1023 if (subobj_offset
+ len
> max
)
1024 invalid_synthetic_pointer ();
1026 retval
= allocate_value (subobj_type
);
1028 /* The given offset is relative to the actual object. */
1029 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
)
1030 subobj_offset
+= n
- max
;
1032 copy (value_contents_all (val
).slice (subobj_offset
, len
),
1033 value_contents_raw (retval
));
1037 case DWARF_VALUE_LITERAL
:
1039 size_t n
= subobj_type
->length ();
1041 if (subobj_offset
+ n
> this->m_len
)
1042 invalid_synthetic_pointer ();
1044 retval
= allocate_value (subobj_type
);
1045 bfd_byte
*contents
= value_contents_raw (retval
).data ();
1046 memcpy (contents
, this->m_data
+ subobj_offset
, n
);
1050 case DWARF_VALUE_OPTIMIZED_OUT
:
1051 retval
= allocate_optimized_out_value (subobj_type
);
1054 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
1055 operation by execute_stack_op. */
1056 case DWARF_VALUE_IMPLICIT_POINTER
:
1057 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
1058 it can only be encountered when making a piece. */
1060 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
1064 set_value_initialized (retval
, this->m_initialized
);
1072 dwarf_expr_context::evaluate (const gdb_byte
*addr
, size_t len
, bool as_lval
,
1073 dwarf2_per_cu_data
*per_cu
, frame_info
*frame
,
1074 const struct property_addr_info
*addr_info
,
1075 struct type
*type
, struct type
*subobj_type
,
1076 LONGEST subobj_offset
)
1078 this->m_per_cu
= per_cu
;
1079 this->m_frame
= frame
;
1080 this->m_addr_info
= addr_info
;
1083 return fetch_result (type
, subobj_type
, subobj_offset
, as_lval
);
1086 /* Require that TYPE be an integral type; throw an exception if not. */
1089 dwarf_require_integral (struct type
*type
)
1091 if (type
->code () != TYPE_CODE_INT
1092 && type
->code () != TYPE_CODE_CHAR
1093 && type
->code () != TYPE_CODE_BOOL
)
1094 error (_("integral type expected in DWARF expression"));
1097 /* Return the unsigned form of TYPE. TYPE is necessarily an integral
1100 static struct type
*
1101 get_unsigned_type (struct gdbarch
*gdbarch
, struct type
*type
)
1103 switch (type
->length ())
1106 return builtin_type (gdbarch
)->builtin_uint8
;
1108 return builtin_type (gdbarch
)->builtin_uint16
;
1110 return builtin_type (gdbarch
)->builtin_uint32
;
1112 return builtin_type (gdbarch
)->builtin_uint64
;
1114 error (_("no unsigned variant found for type, while evaluating "
1115 "DWARF expression"));
1119 /* Return the signed form of TYPE. TYPE is necessarily an integral
1122 static struct type
*
1123 get_signed_type (struct gdbarch
*gdbarch
, struct type
*type
)
1125 switch (type
->length ())
1128 return builtin_type (gdbarch
)->builtin_int8
;
1130 return builtin_type (gdbarch
)->builtin_int16
;
1132 return builtin_type (gdbarch
)->builtin_int32
;
1134 return builtin_type (gdbarch
)->builtin_int64
;
1136 error (_("no signed variant found for type, while evaluating "
1137 "DWARF expression"));
1141 /* Retrieve the N'th item on the stack, converted to an address. */
1144 dwarf_expr_context::fetch_address (int n
)
1146 gdbarch
*arch
= this->m_per_objfile
->objfile
->arch ();
1147 value
*result_val
= fetch (n
);
1148 bfd_endian byte_order
= gdbarch_byte_order (arch
);
1151 dwarf_require_integral (value_type (result_val
));
1152 result
= extract_unsigned_integer (value_contents (result_val
), byte_order
);
1154 /* For most architectures, calling extract_unsigned_integer() alone
1155 is sufficient for extracting an address. However, some
1156 architectures (e.g. MIPS) use signed addresses and using
1157 extract_unsigned_integer() will not produce a correct
1158 result. Make sure we invoke gdbarch_integer_to_address()
1159 for those architectures which require it. */
1160 if (gdbarch_integer_to_address_p (arch
))
1162 gdb_byte
*buf
= (gdb_byte
*) alloca (this->m_addr_size
);
1163 type
*int_type
= get_unsigned_type (arch
,
1164 value_type (result_val
));
1166 store_unsigned_integer (buf
, this->m_addr_size
, byte_order
, result
);
1167 return gdbarch_integer_to_address (arch
, int_type
, buf
);
1170 return (CORE_ADDR
) result
;
1173 /* Retrieve the in_stack_memory flag of the N'th item on the stack. */
1176 dwarf_expr_context::fetch_in_stack_memory (int n
)
1178 if (this->m_stack
.size () <= n
)
1179 error (_("Asked for position %d of stack, "
1180 "stack only has %zu elements on it."),
1181 n
, this->m_stack
.size ());
1182 return this->m_stack
[this->m_stack
.size () - (1 + n
)].in_stack_memory
;
1185 /* Return true if the expression stack is empty. */
1188 dwarf_expr_context::stack_empty_p () const
1190 return m_stack
.empty ();
1193 /* Add a new piece to the dwarf_expr_context's piece list. */
1195 dwarf_expr_context::add_piece (ULONGEST size
, ULONGEST offset
)
1197 this->m_pieces
.emplace_back ();
1198 dwarf_expr_piece
&p
= this->m_pieces
.back ();
1200 p
.location
= this->m_location
;
1204 if (p
.location
== DWARF_VALUE_LITERAL
)
1206 p
.v
.literal
.data
= this->m_data
;
1207 p
.v
.literal
.length
= this->m_len
;
1209 else if (stack_empty_p ())
1211 p
.location
= DWARF_VALUE_OPTIMIZED_OUT
;
1212 /* Also reset the context's location, for our callers. This is
1213 a somewhat strange approach, but this lets us avoid setting
1214 the location to DWARF_VALUE_MEMORY in all the individual
1215 cases in the evaluator. */
1216 this->m_location
= DWARF_VALUE_OPTIMIZED_OUT
;
1218 else if (p
.location
== DWARF_VALUE_MEMORY
)
1220 p
.v
.mem
.addr
= fetch_address (0);
1221 p
.v
.mem
.in_stack_memory
= fetch_in_stack_memory (0);
1223 else if (p
.location
== DWARF_VALUE_IMPLICIT_POINTER
)
1225 p
.v
.ptr
.die_sect_off
= (sect_offset
) this->m_len
;
1226 p
.v
.ptr
.offset
= value_as_long (fetch (0));
1228 else if (p
.location
== DWARF_VALUE_REGISTER
)
1229 p
.v
.regno
= value_as_long (fetch (0));
1232 p
.v
.value
= fetch (0);
1236 /* Evaluate the expression at ADDR (LEN bytes long). */
1239 dwarf_expr_context::eval (const gdb_byte
*addr
, size_t len
)
1241 int old_recursion_depth
= this->m_recursion_depth
;
1243 execute_stack_op (addr
, addr
+ len
);
1245 /* RECURSION_DEPTH becomes invalid if an exception was thrown here. */
1247 gdb_assert (this->m_recursion_depth
== old_recursion_depth
);
1250 /* Helper to read a uleb128 value or throw an error. */
1253 safe_read_uleb128 (const gdb_byte
*buf
, const gdb_byte
*buf_end
,
1256 buf
= gdb_read_uleb128 (buf
, buf_end
, r
);
1258 error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
1262 /* Helper to read a sleb128 value or throw an error. */
1265 safe_read_sleb128 (const gdb_byte
*buf
, const gdb_byte
*buf_end
,
1268 buf
= gdb_read_sleb128 (buf
, buf_end
, r
);
1270 error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
1275 safe_skip_leb128 (const gdb_byte
*buf
, const gdb_byte
*buf_end
)
1277 buf
= gdb_skip_leb128 (buf
, buf_end
);
1279 error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
1284 /* Check that the current operator is either at the end of an
1285 expression, or that it is followed by a composition operator or by
1286 DW_OP_GNU_uninit (which should terminate the expression). */
1289 dwarf_expr_require_composition (const gdb_byte
*op_ptr
, const gdb_byte
*op_end
,
1290 const char *op_name
)
1292 if (op_ptr
!= op_end
&& *op_ptr
!= DW_OP_piece
&& *op_ptr
!= DW_OP_bit_piece
1293 && *op_ptr
!= DW_OP_GNU_uninit
)
1294 error (_("DWARF-2 expression error: `%s' operations must be "
1295 "used either alone or in conjunction with DW_OP_piece "
1296 "or DW_OP_bit_piece."),
1300 /* Return true iff the types T1 and T2 are "the same". This only does
1301 checks that might reasonably be needed to compare DWARF base
1305 base_types_equal_p (struct type
*t1
, struct type
*t2
)
1307 if (t1
->code () != t2
->code ())
1309 if (t1
->is_unsigned () != t2
->is_unsigned ())
1311 return t1
->length () == t2
->length ();
1314 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
1315 DWARF register number. Otherwise return -1. */
1318 dwarf_block_to_dwarf_reg (const gdb_byte
*buf
, const gdb_byte
*buf_end
)
1324 if (*buf
>= DW_OP_reg0
&& *buf
<= DW_OP_reg31
)
1326 if (buf_end
- buf
!= 1)
1328 return *buf
- DW_OP_reg0
;
1331 if (*buf
== DW_OP_regval_type
|| *buf
== DW_OP_GNU_regval_type
)
1334 buf
= gdb_read_uleb128 (buf
, buf_end
, &dwarf_reg
);
1337 buf
= gdb_skip_leb128 (buf
, buf_end
);
1341 else if (*buf
== DW_OP_regx
)
1344 buf
= gdb_read_uleb128 (buf
, buf_end
, &dwarf_reg
);
1350 if (buf
!= buf_end
|| (int) dwarf_reg
!= dwarf_reg
)
1355 /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
1356 DW_OP_deref* return the DWARF register number. Otherwise return -1.
1357 DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
1358 size from DW_OP_deref_size. */
1361 dwarf_block_to_dwarf_reg_deref (const gdb_byte
*buf
, const gdb_byte
*buf_end
,
1362 CORE_ADDR
*deref_size_return
)
1370 if (*buf
>= DW_OP_breg0
&& *buf
<= DW_OP_breg31
)
1372 dwarf_reg
= *buf
- DW_OP_breg0
;
1377 else if (*buf
== DW_OP_bregx
)
1380 buf
= gdb_read_uleb128 (buf
, buf_end
, &dwarf_reg
);
1383 if ((int) dwarf_reg
!= dwarf_reg
)
1389 buf
= gdb_read_sleb128 (buf
, buf_end
, &offset
);
1395 if (*buf
== DW_OP_deref
)
1398 *deref_size_return
= -1;
1400 else if (*buf
== DW_OP_deref_size
)
1405 *deref_size_return
= *buf
++;
1416 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
1417 in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */
1420 dwarf_block_to_fb_offset (const gdb_byte
*buf
, const gdb_byte
*buf_end
,
1421 CORE_ADDR
*fb_offset_return
)
1428 if (*buf
!= DW_OP_fbreg
)
1432 buf
= gdb_read_sleb128 (buf
, buf_end
, &fb_offset
);
1435 *fb_offset_return
= fb_offset
;
1436 if (buf
!= buf_end
|| fb_offset
!= (LONGEST
) *fb_offset_return
)
1442 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
1443 in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0.
1444 The matched SP register number depends on GDBARCH. */
1447 dwarf_block_to_sp_offset (struct gdbarch
*gdbarch
, const gdb_byte
*buf
,
1448 const gdb_byte
*buf_end
, CORE_ADDR
*sp_offset_return
)
1455 if (*buf
>= DW_OP_breg0
&& *buf
<= DW_OP_breg31
)
1457 dwarf_reg
= *buf
- DW_OP_breg0
;
1462 if (*buf
!= DW_OP_bregx
)
1465 buf
= gdb_read_uleb128 (buf
, buf_end
, &dwarf_reg
);
1470 if (dwarf_reg_to_regnum (gdbarch
, dwarf_reg
)
1471 != gdbarch_sp_regnum (gdbarch
))
1474 buf
= gdb_read_sleb128 (buf
, buf_end
, &sp_offset
);
1477 *sp_offset_return
= sp_offset
;
1478 if (buf
!= buf_end
|| sp_offset
!= (LONGEST
) *sp_offset_return
)
1484 /* The engine for the expression evaluator. Using the context in this
1485 object, evaluate the expression between OP_PTR and OP_END. */
1488 dwarf_expr_context::execute_stack_op (const gdb_byte
*op_ptr
,
1489 const gdb_byte
*op_end
)
1491 gdbarch
*arch
= this->m_per_objfile
->objfile
->arch ();
1492 bfd_endian byte_order
= gdbarch_byte_order (arch
);
1493 /* Old-style "untyped" DWARF values need special treatment in a
1494 couple of places, specifically DW_OP_mod and DW_OP_shr. We need
1495 a special type for these values so we can distinguish them from
1496 values that have an explicit type, because explicitly-typed
1497 values do not need special treatment. This special type must be
1498 different (in the `==' sense) from any base type coming from the
1500 type
*address_type
= this->address_type ();
1502 this->m_location
= DWARF_VALUE_MEMORY
;
1503 this->m_initialized
= 1; /* Default is initialized. */
1505 if (this->m_recursion_depth
> this->m_max_recursion_depth
)
1506 error (_("DWARF-2 expression error: Loop detected (%d)."),
1507 this->m_recursion_depth
);
1508 this->m_recursion_depth
++;
1510 while (op_ptr
< op_end
)
1512 dwarf_location_atom op
= (dwarf_location_atom
) *op_ptr
++;
1514 /* Assume the value is not in stack memory.
1515 Code that knows otherwise sets this to true.
1516 Some arithmetic on stack addresses can probably be assumed to still
1517 be a stack address, but we skip this complication for now.
1518 This is just an optimization, so it's always ok to punt
1519 and leave this as false. */
1520 bool in_stack_memory
= false;
1521 uint64_t uoffset
, reg
;
1523 value
*result_val
= NULL
;
1525 /* The DWARF expression might have a bug causing an infinite
1526 loop. In that case, quitting is the only way out. */
1563 result
= op
- DW_OP_lit0
;
1564 result_val
= value_from_ulongest (address_type
, result
);
1568 result
= extract_unsigned_integer (op_ptr
,
1569 this->m_addr_size
, byte_order
);
1570 op_ptr
+= this->m_addr_size
;
1571 /* Some versions of GCC emit DW_OP_addr before
1572 DW_OP_GNU_push_tls_address. In this case the value is an
1573 index, not an address. We don't support things like
1574 branching between the address and the TLS op. */
1575 if (op_ptr
>= op_end
|| *op_ptr
!= DW_OP_GNU_push_tls_address
)
1576 result
+= this->m_per_objfile
->objfile
->text_section_offset ();
1577 result_val
= value_from_ulongest (address_type
, result
);
1581 case DW_OP_GNU_addr_index
:
1582 ensure_have_per_cu (this->m_per_cu
, "DW_OP_addrx");
1584 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
1585 result
= dwarf2_read_addr_index (this->m_per_cu
, this->m_per_objfile
,
1587 result
+= this->m_per_objfile
->objfile
->text_section_offset ();
1588 result_val
= value_from_ulongest (address_type
, result
);
1590 case DW_OP_GNU_const_index
:
1591 ensure_have_per_cu (this->m_per_cu
, "DW_OP_GNU_const_index");
1593 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
1594 result
= dwarf2_read_addr_index (this->m_per_cu
, this->m_per_objfile
,
1596 result_val
= value_from_ulongest (address_type
, result
);
1600 result
= extract_unsigned_integer (op_ptr
, 1, byte_order
);
1601 result_val
= value_from_ulongest (address_type
, result
);
1605 result
= extract_signed_integer (op_ptr
, 1, byte_order
);
1606 result_val
= value_from_ulongest (address_type
, result
);
1610 result
= extract_unsigned_integer (op_ptr
, 2, byte_order
);
1611 result_val
= value_from_ulongest (address_type
, result
);
1615 result
= extract_signed_integer (op_ptr
, 2, byte_order
);
1616 result_val
= value_from_ulongest (address_type
, result
);
1620 result
= extract_unsigned_integer (op_ptr
, 4, byte_order
);
1621 result_val
= value_from_ulongest (address_type
, result
);
1625 result
= extract_signed_integer (op_ptr
, 4, byte_order
);
1626 result_val
= value_from_ulongest (address_type
, result
);
1630 result
= extract_unsigned_integer (op_ptr
, 8, byte_order
);
1631 result_val
= value_from_ulongest (address_type
, result
);
1635 result
= extract_signed_integer (op_ptr
, 8, byte_order
);
1636 result_val
= value_from_ulongest (address_type
, result
);
1640 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
1642 result_val
= value_from_ulongest (address_type
, result
);
1645 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
1647 result_val
= value_from_ulongest (address_type
, result
);
1650 /* The DW_OP_reg operations are required to occur alone in
1651 location expressions. */
1684 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_reg");
1686 result
= op
- DW_OP_reg0
;
1687 result_val
= value_from_ulongest (address_type
, result
);
1688 this->m_location
= DWARF_VALUE_REGISTER
;
1692 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
1693 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
1696 result_val
= value_from_ulongest (address_type
, result
);
1697 this->m_location
= DWARF_VALUE_REGISTER
;
1700 case DW_OP_implicit_value
:
1704 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &len
);
1705 if (op_ptr
+ len
> op_end
)
1706 error (_("DW_OP_implicit_value: too few bytes available."));
1708 this->m_data
= op_ptr
;
1709 this->m_location
= DWARF_VALUE_LITERAL
;
1711 dwarf_expr_require_composition (op_ptr
, op_end
,
1712 "DW_OP_implicit_value");
1716 case DW_OP_stack_value
:
1717 this->m_location
= DWARF_VALUE_STACK
;
1718 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_stack_value");
1721 case DW_OP_implicit_pointer
:
1722 case DW_OP_GNU_implicit_pointer
:
1725 ensure_have_per_cu (this->m_per_cu
, "DW_OP_implicit_pointer");
1727 int ref_addr_size
= this->m_per_cu
->ref_addr_size ();
1729 /* The referred-to DIE of sect_offset kind. */
1730 this->m_len
= extract_unsigned_integer (op_ptr
, ref_addr_size
,
1732 op_ptr
+= ref_addr_size
;
1734 /* The byte offset into the data. */
1735 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &len
);
1736 result
= (ULONGEST
) len
;
1737 result_val
= value_from_ulongest (address_type
, result
);
1739 this->m_location
= DWARF_VALUE_IMPLICIT_POINTER
;
1740 dwarf_expr_require_composition (op_ptr
, op_end
,
1741 "DW_OP_implicit_pointer");
1778 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
1779 ensure_have_frame (this->m_frame
, "DW_OP_breg");
1781 result
= read_addr_from_reg (this->m_frame
, op
- DW_OP_breg0
);
1783 result_val
= value_from_ulongest (address_type
, result
);
1788 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
1789 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
1790 ensure_have_frame (this->m_frame
, "DW_OP_bregx");
1792 result
= read_addr_from_reg (this->m_frame
, reg
);
1794 result_val
= value_from_ulongest (address_type
, result
);
1799 const gdb_byte
*datastart
;
1802 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
1804 /* Rather than create a whole new context, we simply
1805 backup the current stack locally and install a new empty stack,
1806 then reset it afterwards, effectively erasing whatever the
1807 recursive call put there. */
1808 std::vector
<dwarf_stack_value
> saved_stack
= std::move (this->m_stack
);
1809 this->m_stack
.clear ();
1811 /* FIXME: cagney/2003-03-26: This code should be using
1812 get_frame_base_address(), and then implement a dwarf2
1813 specific this_base method. */
1814 this->get_frame_base (&datastart
, &datalen
);
1815 eval (datastart
, datalen
);
1816 if (this->m_location
== DWARF_VALUE_MEMORY
)
1817 result
= fetch_address (0);
1818 else if (this->m_location
== DWARF_VALUE_REGISTER
)
1820 = read_addr_from_reg (this->m_frame
, value_as_long (fetch (0)));
1822 error (_("Not implemented: computing frame "
1823 "base using explicit value operator"));
1824 result
= result
+ offset
;
1825 result_val
= value_from_ulongest (address_type
, result
);
1826 in_stack_memory
= true;
1828 /* Restore the content of the original stack. */
1829 this->m_stack
= std::move (saved_stack
);
1831 this->m_location
= DWARF_VALUE_MEMORY
;
1836 result_val
= fetch (0);
1837 in_stack_memory
= fetch_in_stack_memory (0);
1846 result_val
= fetch (offset
);
1847 in_stack_memory
= fetch_in_stack_memory (offset
);
1852 if (this->m_stack
.size () < 2)
1853 error (_("Not enough elements for "
1854 "DW_OP_swap. Need 2, have %zu."),
1855 this->m_stack
.size ());
1857 dwarf_stack_value
&t1
= this->m_stack
[this->m_stack
.size () - 1];
1858 dwarf_stack_value
&t2
= this->m_stack
[this->m_stack
.size () - 2];
1864 result_val
= fetch (1);
1865 in_stack_memory
= fetch_in_stack_memory (1);
1870 if (this->m_stack
.size () < 3)
1871 error (_("Not enough elements for "
1872 "DW_OP_rot. Need 3, have %zu."),
1873 this->m_stack
.size ());
1875 dwarf_stack_value temp
= this->m_stack
[this->m_stack
.size () - 1];
1876 this->m_stack
[this->m_stack
.size () - 1]
1877 = this->m_stack
[this->m_stack
.size () - 2];
1878 this->m_stack
[this->m_stack
.size () - 2]
1879 = this->m_stack
[this->m_stack
.size () - 3];
1880 this->m_stack
[this->m_stack
.size () - 3] = temp
;
1885 case DW_OP_deref_size
:
1886 case DW_OP_deref_type
:
1887 case DW_OP_GNU_deref_type
:
1889 int addr_size
= (op
== DW_OP_deref
? this->m_addr_size
: *op_ptr
++);
1890 gdb_byte
*buf
= (gdb_byte
*) alloca (addr_size
);
1891 CORE_ADDR addr
= fetch_address (0);
1896 if (op
== DW_OP_deref_type
|| op
== DW_OP_GNU_deref_type
)
1898 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
1899 cu_offset type_die_cu_off
= (cu_offset
) uoffset
;
1900 type
= get_base_type (type_die_cu_off
);
1903 type
= address_type
;
1905 this->read_mem (buf
, addr
, addr_size
);
1907 /* If the size of the object read from memory is different
1908 from the type length, we need to zero-extend it. */
1909 if (type
->length () != addr_size
)
1912 extract_unsigned_integer (buf
, addr_size
, byte_order
);
1914 buf
= (gdb_byte
*) alloca (type
->length ());
1915 store_unsigned_integer (buf
, type
->length (),
1919 result_val
= value_from_contents_and_address (type
, buf
, addr
);
1926 case DW_OP_plus_uconst
:
1928 /* Unary operations. */
1929 result_val
= fetch (0);
1935 if (value_less (result_val
,
1936 value_zero (value_type (result_val
), not_lval
)))
1937 result_val
= value_neg (result_val
);
1940 result_val
= value_neg (result_val
);
1943 dwarf_require_integral (value_type (result_val
));
1944 result_val
= value_complement (result_val
);
1946 case DW_OP_plus_uconst
:
1947 dwarf_require_integral (value_type (result_val
));
1948 result
= value_as_long (result_val
);
1949 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
1951 result_val
= value_from_ulongest (address_type
, result
);
1975 /* Binary operations. */
1976 struct value
*first
, *second
;
1984 if (! base_types_equal_p (value_type (first
), value_type (second
)))
1985 error (_("Incompatible types on DWARF stack"));
1990 dwarf_require_integral (value_type (first
));
1991 dwarf_require_integral (value_type (second
));
1992 result_val
= value_binop (first
, second
, BINOP_BITWISE_AND
);
1995 result_val
= value_binop (first
, second
, BINOP_DIV
);
1998 result_val
= value_binop (first
, second
, BINOP_SUB
);
2003 struct type
*orig_type
= value_type (first
);
2005 /* We have to special-case "old-style" untyped values
2006 -- these must have mod computed using unsigned
2008 if (orig_type
== address_type
)
2010 struct type
*utype
= get_unsigned_type (arch
, orig_type
);
2013 first
= value_cast (utype
, first
);
2014 second
= value_cast (utype
, second
);
2016 /* Note that value_binop doesn't handle float or
2017 decimal float here. This seems unimportant. */
2018 result_val
= value_binop (first
, second
, BINOP_MOD
);
2020 result_val
= value_cast (orig_type
, result_val
);
2024 result_val
= value_binop (first
, second
, BINOP_MUL
);
2027 dwarf_require_integral (value_type (first
));
2028 dwarf_require_integral (value_type (second
));
2029 result_val
= value_binop (first
, second
, BINOP_BITWISE_IOR
);
2032 result_val
= value_binop (first
, second
, BINOP_ADD
);
2035 dwarf_require_integral (value_type (first
));
2036 dwarf_require_integral (value_type (second
));
2037 result_val
= value_binop (first
, second
, BINOP_LSH
);
2040 dwarf_require_integral (value_type (first
));
2041 dwarf_require_integral (value_type (second
));
2042 if (!value_type (first
)->is_unsigned ())
2045 = get_unsigned_type (arch
, value_type (first
));
2047 first
= value_cast (utype
, first
);
2050 result_val
= value_binop (first
, second
, BINOP_RSH
);
2051 /* Make sure we wind up with the same type we started
2053 if (value_type (result_val
) != value_type (second
))
2054 result_val
= value_cast (value_type (second
), result_val
);
2057 dwarf_require_integral (value_type (first
));
2058 dwarf_require_integral (value_type (second
));
2059 if (value_type (first
)->is_unsigned ())
2062 = get_signed_type (arch
, value_type (first
));
2064 first
= value_cast (stype
, first
);
2067 result_val
= value_binop (first
, second
, BINOP_RSH
);
2068 /* Make sure we wind up with the same type we started
2070 if (value_type (result_val
) != value_type (second
))
2071 result_val
= value_cast (value_type (second
), result_val
);
2074 dwarf_require_integral (value_type (first
));
2075 dwarf_require_integral (value_type (second
));
2076 result_val
= value_binop (first
, second
, BINOP_BITWISE_XOR
);
2079 /* A <= B is !(B < A). */
2080 result
= ! value_less (second
, first
);
2081 result_val
= value_from_ulongest (address_type
, result
);
2084 /* A >= B is !(A < B). */
2085 result
= ! value_less (first
, second
);
2086 result_val
= value_from_ulongest (address_type
, result
);
2089 result
= value_equal (first
, second
);
2090 result_val
= value_from_ulongest (address_type
, result
);
2093 result
= value_less (first
, second
);
2094 result_val
= value_from_ulongest (address_type
, result
);
2097 /* A > B is B < A. */
2098 result
= value_less (second
, first
);
2099 result_val
= value_from_ulongest (address_type
, result
);
2102 result
= ! value_equal (first
, second
);
2103 result_val
= value_from_ulongest (address_type
, result
);
2106 internal_error (__FILE__
, __LINE__
,
2107 _("Can't be reached."));
2112 case DW_OP_call_frame_cfa
:
2113 ensure_have_frame (this->m_frame
, "DW_OP_call_frame_cfa");
2115 result
= dwarf2_frame_cfa (this->m_frame
);
2116 result_val
= value_from_ulongest (address_type
, result
);
2117 in_stack_memory
= true;
2120 case DW_OP_GNU_push_tls_address
:
2121 case DW_OP_form_tls_address
:
2122 /* Variable is at a constant offset in the thread-local
2123 storage block into the objfile for the current thread and
2124 the dynamic linker module containing this expression. Here
2125 we return returns the offset from that base. The top of the
2126 stack has the offset from the beginning of the thread
2127 control block at which the variable is located. Nothing
2128 should follow this operator, so the top of stack would be
2130 result
= value_as_long (fetch (0));
2132 result
= target_translate_tls_address (this->m_per_objfile
->objfile
,
2134 result_val
= value_from_ulongest (address_type
, result
);
2138 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
2147 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
2150 dwarf_require_integral (value_type (val
));
2151 if (value_as_long (val
) != 0)
2164 /* Record the piece. */
2165 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &size
);
2166 add_piece (8 * size
, 0);
2168 /* Pop off the address/regnum, and reset the location
2170 if (this->m_location
!= DWARF_VALUE_LITERAL
2171 && this->m_location
!= DWARF_VALUE_OPTIMIZED_OUT
)
2173 this->m_location
= DWARF_VALUE_MEMORY
;
2177 case DW_OP_bit_piece
:
2179 uint64_t size
, uleb_offset
;
2181 /* Record the piece. */
2182 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &size
);
2183 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uleb_offset
);
2184 add_piece (size
, uleb_offset
);
2186 /* Pop off the address/regnum, and reset the location
2188 if (this->m_location
!= DWARF_VALUE_LITERAL
2189 && this->m_location
!= DWARF_VALUE_OPTIMIZED_OUT
)
2191 this->m_location
= DWARF_VALUE_MEMORY
;
2195 case DW_OP_GNU_uninit
:
2196 if (op_ptr
!= op_end
)
2197 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
2198 "be the very last op."));
2200 this->m_initialized
= 0;
2206 = (cu_offset
) extract_unsigned_integer (op_ptr
, 2, byte_order
);
2208 this->dwarf_call (cu_off
);
2215 = (cu_offset
) extract_unsigned_integer (op_ptr
, 4, byte_order
);
2217 this->dwarf_call (cu_off
);
2221 case DW_OP_GNU_variable_value
:
2223 ensure_have_per_cu (this->m_per_cu
, "DW_OP_GNU_variable_value");
2224 int ref_addr_size
= this->m_per_cu
->ref_addr_size ();
2226 sect_offset sect_off
2227 = (sect_offset
) extract_unsigned_integer (op_ptr
,
2230 op_ptr
+= ref_addr_size
;
2231 result_val
= sect_variable_value (sect_off
, this->m_per_cu
,
2232 this->m_per_objfile
);
2233 result_val
= value_cast (address_type
, result_val
);
2237 case DW_OP_entry_value
:
2238 case DW_OP_GNU_entry_value
:
2241 CORE_ADDR deref_size
;
2242 union call_site_parameter_u kind_u
;
2244 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &len
);
2245 if (op_ptr
+ len
> op_end
)
2246 error (_("DW_OP_entry_value: too few bytes available."));
2248 kind_u
.dwarf_reg
= dwarf_block_to_dwarf_reg (op_ptr
, op_ptr
+ len
);
2249 if (kind_u
.dwarf_reg
!= -1)
2252 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG
,
2254 -1 /* deref_size */);
2258 kind_u
.dwarf_reg
= dwarf_block_to_dwarf_reg_deref (op_ptr
,
2261 if (kind_u
.dwarf_reg
!= -1)
2263 if (deref_size
== -1)
2264 deref_size
= this->m_addr_size
;
2266 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG
,
2267 kind_u
, deref_size
);
2271 error (_("DWARF-2 expression error: DW_OP_entry_value is "
2272 "supported only for single DW_OP_reg* "
2273 "or for DW_OP_breg*(0)+DW_OP_deref*"));
2276 case DW_OP_GNU_parameter_ref
:
2278 union call_site_parameter_u kind_u
;
2281 = (cu_offset
) extract_unsigned_integer (op_ptr
, 4, byte_order
);
2283 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_PARAM_OFFSET
,
2285 -1 /* deref_size */);
2289 case DW_OP_const_type
:
2290 case DW_OP_GNU_const_type
:
2293 const gdb_byte
*data
;
2296 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
2297 cu_offset type_die_cu_off
= (cu_offset
) uoffset
;
2303 type
= get_base_type (type_die_cu_off
);
2305 if (type
->length () != n
)
2306 error (_("DW_OP_const_type has different sizes for type and data"));
2308 result_val
= value_from_contents (type
, data
);
2312 case DW_OP_regval_type
:
2313 case DW_OP_GNU_regval_type
:
2315 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
2316 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
2317 cu_offset type_die_cu_off
= (cu_offset
) uoffset
;
2319 ensure_have_frame (this->m_frame
, "DW_OP_regval_type");
2321 struct type
*type
= get_base_type (type_die_cu_off
);
2323 = dwarf_reg_to_regnum_or_error (get_frame_arch (this->m_frame
),
2325 result_val
= value_from_register (type
, regnum
, this->m_frame
);
2330 case DW_OP_GNU_convert
:
2331 case DW_OP_reinterpret
:
2332 case DW_OP_GNU_reinterpret
:
2336 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
2337 cu_offset type_die_cu_off
= (cu_offset
) uoffset
;
2339 if (to_underlying (type_die_cu_off
) == 0)
2340 type
= address_type
;
2342 type
= get_base_type (type_die_cu_off
);
2344 result_val
= fetch (0);
2347 if (op
== DW_OP_convert
|| op
== DW_OP_GNU_convert
)
2348 result_val
= value_cast (type
, result_val
);
2349 else if (type
== value_type (result_val
))
2353 else if (type
->length ()
2354 != value_type (result_val
)->length ())
2355 error (_("DW_OP_reinterpret has wrong size"));
2358 = value_from_contents (type
,
2359 value_contents_all (result_val
).data ());
2363 case DW_OP_push_object_address
:
2364 /* Return the address of the object we are currently observing. */
2365 if (this->m_addr_info
== nullptr
2366 || (this->m_addr_info
->valaddr
.data () == nullptr
2367 && this->m_addr_info
->addr
== 0))
2368 error (_("Location address is not set."));
2371 = value_from_ulongest (address_type
, this->m_addr_info
->addr
);
2375 error (_("Unhandled dwarf expression opcode 0x%x"), op
);
2378 /* Most things push a result value. */
2379 gdb_assert (result_val
!= NULL
);
2380 push (result_val
, in_stack_memory
);
2385 /* To simplify our main caller, if the result is an implicit
2386 pointer, then make a pieced value. This is ok because we can't
2387 have implicit pointers in contexts where pieces are invalid. */
2388 if (this->m_location
== DWARF_VALUE_IMPLICIT_POINTER
)
2389 add_piece (8 * this->m_addr_size
, 0);
2391 this->m_recursion_depth
--;
2392 gdb_assert (this->m_recursion_depth
>= 0);