1 /* DWARF 2 Expression Evaluator.
3 Copyright (C) 2001-2022 Free Software Foundation, Inc.
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_ptr 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_ptr 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,
115 frame_info_ptr frame)
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 (_("Should not be able to create a lazy value with "
166 "an enclosing type"));
168 v_contents = nullptr;
170 v_contents = value_contents_raw (v).data ();
171 from_contents = nullptr;
174 ULONGEST bits_to_skip = 8 * value_offset (v);
175 if (value_bitsize (v))
177 bits_to_skip += (8 * value_offset (value_parent (v))
180 && (type_byte_order (value_type (from))
183 /* Use the least significant bits of FROM. */
184 max_offset = 8 * value_type (from)->length ();
185 offset = max_offset - value_bitsize (v);
188 max_offset = value_bitsize (v);
191 max_offset = 8 * value_type (v)->length ();
193 /* Advance to the first non-skipped piece. */
194 for (i = 0; i < c->pieces.size () && bits_to_skip >= c->pieces[i].size; i++)
195 bits_to_skip -= c->pieces[i].size;
197 for (; i < c->pieces.size () && offset < max_offset; i++)
199 dwarf_expr_piece *p = &c->pieces[i];
200 size_t this_size_bits, this_size;
202 this_size_bits = p->size - bits_to_skip;
203 if (this_size_bits > max_offset - offset)
204 this_size_bits = max_offset - offset;
208 case DWARF_VALUE_REGISTER:
210 frame_info_ptr frame = frame_find_by_id (c->frame_id);
211 gdbarch *arch = get_frame_arch (frame);
212 int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, p->v.regno);
213 ULONGEST reg_bits = 8 * register_size (arch, gdb_regnum);
216 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
217 && p->offset + p->size < reg_bits)
219 /* Big-endian, and we want less than full size. */
220 bits_to_skip += reg_bits - (p->offset + p->size);
223 bits_to_skip += p->offset;
225 this_size = bits_to_bytes (bits_to_skip, this_size_bits);
226 buffer.resize (this_size);
231 if (!get_frame_register_bytes (frame, gdb_regnum,
233 buffer, &optim, &unavail))
239 mark_value_bits_optimized_out (v, offset,
242 if (unavail && !check_optimized)
243 mark_value_bits_unavailable (v, offset,
248 if (!check_optimized)
249 copy_bitwise (v_contents, offset,
250 buffer.data (), bits_to_skip % 8,
251 this_size_bits, bits_big_endian);
256 if (bits_to_skip % 8 != 0 || this_size_bits % 8 != 0)
258 /* Data is copied non-byte-aligned into the register.
259 Need some bits from original register value. */
260 get_frame_register_bytes (frame, gdb_regnum,
262 buffer, &optim, &unavail);
264 throw_error (OPTIMIZED_OUT_ERROR,
265 _("Can't do read-modify-write to "
266 "update bitfield; containing word "
267 "has been optimized out"));
269 throw_error (NOT_AVAILABLE_ERROR,
270 _("Can't do read-modify-write to "
271 "update bitfield; containing word "
275 copy_bitwise (buffer.data (), bits_to_skip % 8,
276 from_contents, offset,
277 this_size_bits, bits_big_endian);
278 put_frame_register_bytes (frame, gdb_regnum,
285 case DWARF_VALUE_MEMORY:
290 bits_to_skip += p->offset;
292 CORE_ADDR start_addr = p->v.mem.addr + bits_to_skip / 8;
294 if (bits_to_skip % 8 == 0 && this_size_bits % 8 == 0
297 /* Everything is byte-aligned; no buffer needed. */
299 write_memory_with_notification (start_addr,
304 read_value_memory (v, offset,
305 p->v.mem.in_stack_memory,
306 p->v.mem.addr + bits_to_skip / 8,
307 v_contents + offset / 8,
312 this_size = bits_to_bytes (bits_to_skip, this_size_bits);
313 buffer.resize (this_size);
318 read_value_memory (v, offset,
319 p->v.mem.in_stack_memory,
320 p->v.mem.addr + bits_to_skip / 8,
321 buffer.data (), this_size);
322 copy_bitwise (v_contents, offset,
323 buffer.data (), bits_to_skip % 8,
324 this_size_bits, bits_big_endian);
329 if (bits_to_skip % 8 != 0 || this_size_bits % 8 != 0)
333 /* Perform a single read for small sizes. */
334 read_memory (start_addr, buffer.data (),
339 /* Only the first and last bytes can possibly have
341 read_memory (start_addr, buffer.data (), 1);
342 read_memory (start_addr + this_size - 1,
343 &buffer[this_size - 1], 1);
347 copy_bitwise (buffer.data (), bits_to_skip % 8,
348 from_contents, offset,
349 this_size_bits, bits_big_endian);
350 write_memory_with_notification (start_addr,
357 case DWARF_VALUE_STACK:
364 mark_value_bits_optimized_out (v, offset, this_size_bits);
368 gdbarch *objfile_gdbarch = c->per_objfile->objfile->arch ();
369 ULONGEST stack_value_size_bits
370 = 8 * value_type (p->v.value)->length ();
372 /* Use zeroes if piece reaches beyond stack value. */
373 if (p->offset + p->size > stack_value_size_bits)
376 /* Piece is anchored at least significant bit end. */
377 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
378 bits_to_skip += stack_value_size_bits - p->offset - p->size;
380 bits_to_skip += p->offset;
382 copy_bitwise (v_contents, offset,
383 value_contents_all (p->v.value).data (),
385 this_size_bits, bits_big_endian);
389 case DWARF_VALUE_LITERAL:
396 mark_value_bits_optimized_out (v, offset, this_size_bits);
400 ULONGEST literal_size_bits = 8 * p->v.literal.length;
401 size_t n = this_size_bits;
403 /* Cut off at the end of the implicit value. */
404 bits_to_skip += p->offset;
405 if (bits_to_skip >= literal_size_bits)
407 if (n > literal_size_bits - bits_to_skip)
408 n = literal_size_bits - bits_to_skip;
410 copy_bitwise (v_contents, offset,
411 p->v.literal.data, bits_to_skip,
416 case DWARF_VALUE_IMPLICIT_POINTER:
419 mark_value_bits_optimized_out (v, offset, this_size_bits);
423 /* These bits show up as zeros -- but do not cause the value to
424 be considered optimized-out. */
427 case DWARF_VALUE_OPTIMIZED_OUT:
430 mark_value_bits_optimized_out (v, offset, this_size_bits);
434 internal_error (_("invalid location type"));
437 offset += this_size_bits;
445 read_pieced_value (value *v)
447 rw_pieced_value (v, nullptr, false);
451 write_pieced_value (value *to, value *from)
453 rw_pieced_value (to, from, false);
457 is_optimized_out_pieced_value (value *v)
459 return rw_pieced_value (v, nullptr, true);
462 /* An implementation of an lval_funcs method to see whether a value is
463 a synthetic pointer. */
466 check_pieced_synthetic_pointer (const value *value, LONGEST bit_offset,
469 piece_closure *c = (piece_closure *) value_computed_closure (value);
472 bit_offset += 8 * value_offset (value);
473 if (value_bitsize (value))
474 bit_offset += value_bitpos (value);
476 for (i = 0; i < c->pieces.size () && bit_length > 0; i++)
478 dwarf_expr_piece *p = &c->pieces[i];
479 size_t this_size_bits = p->size;
483 if (bit_offset >= this_size_bits)
485 bit_offset -= this_size_bits;
489 bit_length -= this_size_bits - bit_offset;
493 bit_length -= this_size_bits;
495 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
502 /* An implementation of an lval_funcs method to indirect through a
503 pointer. This handles the synthetic pointer case when needed. */
506 indirect_pieced_value (value *value)
509 = (piece_closure *) value_computed_closure (value);
511 dwarf_expr_piece *piece = NULL;
513 struct type *type = check_typedef (value_type (value));
514 if (type->code () != TYPE_CODE_PTR)
517 int bit_length = 8 * type->length ();
518 LONGEST bit_offset = 8 * value_offset (value);
519 if (value_bitsize (value))
520 bit_offset += value_bitpos (value);
522 for (i = 0; i < c->pieces.size () && bit_length > 0; i++)
524 dwarf_expr_piece *p = &c->pieces[i];
525 size_t this_size_bits = p->size;
529 if (bit_offset >= this_size_bits)
531 bit_offset -= this_size_bits;
535 bit_length -= this_size_bits - bit_offset;
539 bit_length -= this_size_bits;
541 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
545 error (_("Invalid use of DW_OP_implicit_pointer"));
551 gdb_assert (piece != NULL && c->per_cu != nullptr);
552 frame_info_ptr frame = get_selected_frame (_("No frame selected."));
554 /* This is an offset requested by GDB, such as value subscripts.
555 However, due to how synthetic pointers are implemented, this is
556 always presented to us as a pointer type. This means we have to
557 sign-extend it manually as appropriate. Use raw
558 extract_signed_integer directly rather than value_as_address and
559 sign extend afterwards on architectures that would need it
560 (mostly everywhere except MIPS, which has signed addresses) as
561 the later would go through gdbarch_pointer_to_address and thus
562 return a CORE_ADDR with high bits set on architectures that
563 encode address spaces and other things in CORE_ADDR. */
564 bfd_endian byte_order = gdbarch_byte_order (get_frame_arch (frame));
566 = extract_signed_integer (value_contents (value), byte_order);
567 byte_offset += piece->v.ptr.offset;
569 return indirect_synthetic_pointer (piece->v.ptr.die_sect_off,
570 byte_offset, c->per_cu,
571 c->per_objfile, frame, type);
574 /* Implementation of the coerce_ref method of lval_funcs for synthetic C++
578 coerce_pieced_ref (const value *value)
580 struct type *type = check_typedef (value_type (value));
582 if (value_bits_synthetic_pointer (value, value_embedded_offset (value),
583 TARGET_CHAR_BIT * type->length ()))
585 const piece_closure *closure
586 = (piece_closure *) value_computed_closure (value);
588 = get_selected_frame (_("No frame selected."));
590 /* gdb represents synthetic pointers as pieced values with a single
592 gdb_assert (closure != NULL);
593 gdb_assert (closure->pieces.size () == 1);
595 return indirect_synthetic_pointer
596 (closure->pieces[0].v.ptr.die_sect_off,
597 closure->pieces[0].v.ptr.offset,
598 closure->per_cu, closure->per_objfile, frame, type);
602 /* Else: not a synthetic reference; do nothing. */
608 copy_pieced_value_closure (const value *v)
610 piece_closure *c = (piece_closure *) value_computed_closure (v);
617 free_pieced_value_closure (value *v)
619 piece_closure *c = (piece_closure *) value_computed_closure (v);
624 for (dwarf_expr_piece &p : c->pieces)
625 if (p.location == DWARF_VALUE_STACK)
626 value_decref (p.v.value);
632 /* Functions for accessing a variable described by DW_OP_piece. */
633 static const struct lval_funcs pieced_value_funcs = {
636 is_optimized_out_pieced_value,
637 indirect_pieced_value,
639 check_pieced_synthetic_pointer,
640 copy_pieced_value_closure,
641 free_pieced_value_closure
644 /* Given context CTX, section offset SECT_OFF, and compilation unit
645 data PER_CU, execute the "variable value" operation on the DIE
646 found at SECT_OFF. */
649 sect_variable_value (sect_offset sect_off,
650 dwarf2_per_cu_data *per_cu,
651 dwarf2_per_objfile *per_objfile)
653 const char *var_name = nullptr;
654 struct type *die_type
655 = dwarf2_fetch_die_type_sect_off (sect_off, per_cu, per_objfile,
658 if (die_type == NULL)
659 error (_("Bad DW_OP_GNU_variable_value DIE."));
661 /* Note: Things still work when the following test is removed. This
662 test and error is here to conform to the proposed specification. */
663 if (die_type->code () != TYPE_CODE_INT
664 && die_type->code () != TYPE_CODE_ENUM
665 && die_type->code () != TYPE_CODE_RANGE
666 && die_type->code () != TYPE_CODE_PTR)
667 error (_("Type of DW_OP_GNU_variable_value DIE must be an integer or pointer."));
669 if (var_name != nullptr)
671 value *result = compute_var_value (var_name);
672 if (result != nullptr)
676 struct type *type = lookup_pointer_type (die_type);
677 frame_info_ptr frame = get_selected_frame (_("No frame selected."));
678 return indirect_synthetic_pointer (sect_off, 0, per_cu, per_objfile, frame,
682 /* Return the type used for DWARF operations where the type is
683 unspecified in the DWARF spec. Only certain sizes are
687 dwarf_expr_context::address_type () const
689 gdbarch *arch = this->m_per_objfile->objfile->arch ();
690 dwarf_gdbarch_types *types = dwarf_arch_cookie.get (arch);
691 if (types == nullptr)
692 types = dwarf_arch_cookie.emplace (arch);
695 if (this->m_addr_size == 2)
697 else if (this->m_addr_size == 4)
699 else if (this->m_addr_size == 8)
702 error (_("Unsupported address size in DWARF expressions: %d bits"),
703 8 * this->m_addr_size);
705 if (types->dw_types[ndx] == NULL)
707 = arch_integer_type (arch, 8 * this->m_addr_size,
708 0, "<signed DWARF address type>");
710 return types->dw_types[ndx];
713 /* Create a new context for the expression evaluator. */
715 dwarf_expr_context::dwarf_expr_context (dwarf2_per_objfile *per_objfile,
717 : m_addr_size (addr_size),
718 m_per_objfile (per_objfile)
722 /* Push VALUE onto the stack. */
725 dwarf_expr_context::push (struct value *value, bool in_stack_memory)
727 this->m_stack.emplace_back (value, in_stack_memory);
730 /* Push VALUE onto the stack. */
733 dwarf_expr_context::push_address (CORE_ADDR value, bool in_stack_memory)
735 push (value_from_ulongest (address_type (), value), in_stack_memory);
738 /* Pop the top item off of the stack. */
741 dwarf_expr_context::pop ()
743 if (this->m_stack.empty ())
744 error (_("dwarf expression stack underflow"));
746 this->m_stack.pop_back ();
749 /* Retrieve the N'th item on the stack. */
752 dwarf_expr_context::fetch (int n)
754 if (this->m_stack.size () <= n)
755 error (_("Asked for position %d of stack, "
756 "stack only has %zu elements on it."),
757 n, this->m_stack.size ());
758 return this->m_stack[this->m_stack.size () - (1 + n)].value;
764 dwarf_expr_context::get_frame_base (const gdb_byte **start,
767 ensure_have_frame (this->m_frame, "DW_OP_fbreg");
769 const block *bl = get_frame_block (this->m_frame, NULL);
772 error (_("frame address is not available."));
774 /* Use block_linkage_function, which returns a real (not inlined)
775 function, instead of get_frame_function, which may return an
777 symbol *framefunc = block_linkage_function (bl);
779 /* If we found a frame-relative symbol then it was certainly within
780 some function associated with a frame. If we can't find the frame,
781 something has gone wrong. */
782 gdb_assert (framefunc != NULL);
784 func_get_frame_base_dwarf_block (framefunc,
785 get_frame_address_in_block (this->m_frame),
792 dwarf_expr_context::get_base_type (cu_offset die_cu_off)
794 if (this->m_per_cu == nullptr)
795 return builtin_type (this->m_per_objfile->objfile->arch ())->builtin_int;
797 struct type *result = dwarf2_get_die_type (die_cu_off, this->m_per_cu,
798 this->m_per_objfile);
800 if (result == nullptr)
801 error (_("Could not find type for operation"));
809 dwarf_expr_context::dwarf_call (cu_offset die_cu_off)
811 ensure_have_per_cu (this->m_per_cu, "DW_OP_call");
813 frame_info_ptr frame = this->m_frame;
815 auto get_pc_from_frame = [frame] ()
817 ensure_have_frame (frame, "DW_OP_call");
818 return get_frame_address_in_block (frame);
821 dwarf2_locexpr_baton block
822 = dwarf2_fetch_die_loc_cu_off (die_cu_off, this->m_per_cu,
823 this->m_per_objfile, get_pc_from_frame);
825 /* DW_OP_call_ref is currently not supported. */
826 gdb_assert (block.per_cu == this->m_per_cu);
828 this->eval (block.data, block.size);
834 dwarf_expr_context::read_mem (gdb_byte *buf, CORE_ADDR addr,
840 /* Prefer the passed-in memory, if it exists. */
841 if (this->m_addr_info != nullptr)
843 CORE_ADDR offset = addr - this->m_addr_info->addr;
845 if (offset < this->m_addr_info->valaddr.size ()
846 && offset + length <= this->m_addr_info->valaddr.size ())
848 memcpy (buf, this->m_addr_info->valaddr.data (), length);
853 read_memory (addr, buf, length);
859 dwarf_expr_context::push_dwarf_reg_entry_value (call_site_parameter_kind kind,
860 call_site_parameter_u kind_u,
863 ensure_have_per_cu (this->m_per_cu, "DW_OP_entry_value");
864 ensure_have_frame (this->m_frame, "DW_OP_entry_value");
866 dwarf2_per_cu_data *caller_per_cu;
867 dwarf2_per_objfile *caller_per_objfile;
868 frame_info_ptr caller_frame = get_prev_frame (this->m_frame);
869 call_site_parameter *parameter
870 = dwarf_expr_reg_to_entry_parameter (this->m_frame, kind, kind_u,
872 &caller_per_objfile);
873 const gdb_byte *data_src
874 = deref_size == -1 ? parameter->value : parameter->data_value;
876 = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
878 /* DEREF_SIZE size is not verified here. */
879 if (data_src == nullptr)
880 throw_error (NO_ENTRY_VALUE_ERROR,
881 _("Cannot resolve DW_AT_call_data_value"));
883 /* We are about to evaluate an expression in the context of the caller
884 of the current frame. This evaluation context may be different from
885 the current (callee's) context), so temporarily set the caller's context.
887 It is possible for the caller to be from a different objfile from the
888 callee if the call is made through a function pointer. */
889 scoped_restore save_frame = make_scoped_restore (&this->m_frame,
891 scoped_restore save_per_cu = make_scoped_restore (&this->m_per_cu,
893 scoped_restore save_addr_info = make_scoped_restore (&this->m_addr_info,
895 scoped_restore save_per_objfile = make_scoped_restore (&this->m_per_objfile,
898 scoped_restore save_addr_size = make_scoped_restore (&this->m_addr_size);
899 this->m_addr_size = this->m_per_cu->addr_size ();
901 this->eval (data_src, size);
907 dwarf_expr_context::fetch_result (struct type *type, struct type *subobj_type,
908 LONGEST subobj_offset, bool as_lval)
910 value *retval = nullptr;
911 gdbarch *arch = this->m_per_objfile->objfile->arch ();
914 type = address_type ();
916 if (subobj_type == nullptr)
919 /* Ensure that, if TYPE or SUBOBJ_TYPE are typedefs, their length is filled
920 in instead of being zero. */
921 check_typedef (type);
922 check_typedef (subobj_type);
924 if (this->m_pieces.size () > 0)
926 ULONGEST bit_size = 0;
928 for (dwarf_expr_piece &piece : this->m_pieces)
929 bit_size += piece.size;
930 /* Complain if the expression is larger than the size of the
932 if (bit_size > 8 * type->length ())
933 invalid_synthetic_pointer ();
936 = allocate_piece_closure (this->m_per_cu, this->m_per_objfile,
937 std::move (this->m_pieces), this->m_frame);
938 retval = allocate_computed_value (subobj_type,
939 &pieced_value_funcs, c);
940 set_value_offset (retval, subobj_offset);
944 /* If AS_LVAL is false, means that the implicit conversion
945 from a location description to value is expected. */
947 this->m_location = DWARF_VALUE_STACK;
949 switch (this->m_location)
951 case DWARF_VALUE_REGISTER:
953 gdbarch *f_arch = get_frame_arch (this->m_frame);
955 = longest_to_int (value_as_long (this->fetch (0)));
956 int gdb_regnum = dwarf_reg_to_regnum_or_error (f_arch,
959 if (subobj_offset != 0)
960 error (_("cannot use offset on synthetic pointer to register"));
962 gdb_assert (this->m_frame != NULL);
964 retval = value_from_register (subobj_type, gdb_regnum,
966 if (value_optimized_out (retval))
968 /* This means the register has undefined value / was
969 not saved. As we're computing the location of some
970 variable etc. in the program, not a value for
971 inspecting a register ($pc, $sp, etc.), return a
972 generic optimized out value instead, so that we show
973 <optimized out> instead of <not saved>. */
974 value *tmp = allocate_value (subobj_type);
975 value_contents_copy (tmp, 0, retval, 0,
976 subobj_type->length ());
982 case DWARF_VALUE_MEMORY:
984 struct type *ptr_type;
985 CORE_ADDR address = this->fetch_address (0);
986 bool in_stack_memory = this->fetch_in_stack_memory (0);
988 /* DW_OP_deref_size (and possibly other operations too) may
989 create a pointer instead of an address. Ideally, the
990 pointer to address conversion would be performed as part
991 of those operations, but the type of the object to
992 which the address refers is not known at the time of
993 the operation. Therefore, we do the conversion here
994 since the type is readily available. */
996 switch (subobj_type->code ())
999 case TYPE_CODE_METHOD:
1000 ptr_type = builtin_type (arch)->builtin_func_ptr;
1003 ptr_type = builtin_type (arch)->builtin_data_ptr;
1006 address = value_as_address (value_from_pointer (ptr_type, address));
1008 retval = value_at_lazy (subobj_type,
1009 address + subobj_offset);
1010 if (in_stack_memory)
1011 set_value_stack (retval, 1);
1015 case DWARF_VALUE_STACK:
1017 value *val = this->fetch (0);
1018 size_t n = value_type (val)->length ();
1019 size_t len = subobj_type->length ();
1020 size_t max = type->length ();
1022 if (subobj_offset + len > max)
1023 invalid_synthetic_pointer ();
1025 retval = allocate_value (subobj_type);
1027 /* The given offset is relative to the actual object. */
1028 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
1029 subobj_offset += n - max;
1031 copy (value_contents_all (val).slice (subobj_offset, len),
1032 value_contents_raw (retval));
1036 case DWARF_VALUE_LITERAL:
1038 size_t n = subobj_type->length ();
1040 if (subobj_offset + n > this->m_len)
1041 invalid_synthetic_pointer ();
1043 retval = allocate_value (subobj_type);
1044 bfd_byte *contents = value_contents_raw (retval).data ();
1045 memcpy (contents, this->m_data + subobj_offset, n);
1049 case DWARF_VALUE_OPTIMIZED_OUT:
1050 retval = allocate_optimized_out_value (subobj_type);
1053 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
1054 operation by execute_stack_op. */
1055 case DWARF_VALUE_IMPLICIT_POINTER:
1056 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
1057 it can only be encountered when making a piece. */
1059 internal_error (_("invalid location type"));
1063 set_value_initialized (retval, this->m_initialized);
1071 dwarf_expr_context::evaluate (const gdb_byte *addr, size_t len, bool as_lval,
1072 dwarf2_per_cu_data *per_cu, frame_info_ptr frame,
1073 const struct property_addr_info *addr_info,
1074 struct type *type, struct type *subobj_type,
1075 LONGEST subobj_offset)
1077 this->m_per_cu = per_cu;
1078 this->m_frame = frame;
1079 this->m_addr_info = addr_info;
1082 return fetch_result (type, subobj_type, subobj_offset, as_lval);
1085 /* Require that TYPE be an integral type; throw an exception if not. */
1088 dwarf_require_integral (struct type *type)
1090 if (type->code () != TYPE_CODE_INT
1091 && type->code () != TYPE_CODE_CHAR
1092 && type->code () != TYPE_CODE_BOOL)
1093 error (_("integral type expected in DWARF expression"));
1096 /* Return the unsigned form of TYPE. TYPE is necessarily an integral
1099 static struct type *
1100 get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
1102 switch (type->length ())
1105 return builtin_type (gdbarch)->builtin_uint8;
1107 return builtin_type (gdbarch)->builtin_uint16;
1109 return builtin_type (gdbarch)->builtin_uint32;
1111 return builtin_type (gdbarch)->builtin_uint64;
1113 error (_("no unsigned variant found for type, while evaluating "
1114 "DWARF expression"));
1118 /* Return the signed form of TYPE. TYPE is necessarily an integral
1121 static struct type *
1122 get_signed_type (struct gdbarch *gdbarch, struct type *type)
1124 switch (type->length ())
1127 return builtin_type (gdbarch)->builtin_int8;
1129 return builtin_type (gdbarch)->builtin_int16;
1131 return builtin_type (gdbarch)->builtin_int32;
1133 return builtin_type (gdbarch)->builtin_int64;
1135 error (_("no signed variant found for type, while evaluating "
1136 "DWARF expression"));
1140 /* Retrieve the N'th item on the stack, converted to an address. */
1143 dwarf_expr_context::fetch_address (int n)
1145 gdbarch *arch = this->m_per_objfile->objfile->arch ();
1146 value *result_val = fetch (n);
1147 bfd_endian byte_order = gdbarch_byte_order (arch);
1150 dwarf_require_integral (value_type (result_val));
1151 result = extract_unsigned_integer (value_contents (result_val), byte_order);
1153 /* For most architectures, calling extract_unsigned_integer() alone
1154 is sufficient for extracting an address. However, some
1155 architectures (e.g. MIPS) use signed addresses and using
1156 extract_unsigned_integer() will not produce a correct
1157 result. Make sure we invoke gdbarch_integer_to_address()
1158 for those architectures which require it. */
1159 if (gdbarch_integer_to_address_p (arch))
1161 gdb_byte *buf = (gdb_byte *) alloca (this->m_addr_size);
1162 type *int_type = get_unsigned_type (arch,
1163 value_type (result_val));
1165 store_unsigned_integer (buf, this->m_addr_size, byte_order, result);
1166 return gdbarch_integer_to_address (arch, int_type, buf);
1169 return (CORE_ADDR) result;
1172 /* Retrieve the in_stack_memory flag of the N'th item on the stack. */
1175 dwarf_expr_context::fetch_in_stack_memory (int n)
1177 if (this->m_stack.size () <= n)
1178 error (_("Asked for position %d of stack, "
1179 "stack only has %zu elements on it."),
1180 n, this->m_stack.size ());
1181 return this->m_stack[this->m_stack.size () - (1 + n)].in_stack_memory;
1184 /* Return true if the expression stack is empty. */
1187 dwarf_expr_context::stack_empty_p () const
1189 return m_stack.empty ();
1192 /* Add a new piece to the dwarf_expr_context's piece list. */
1194 dwarf_expr_context::add_piece (ULONGEST size, ULONGEST offset)
1196 this->m_pieces.emplace_back ();
1197 dwarf_expr_piece &p = this->m_pieces.back ();
1199 p.location = this->m_location;
1203 if (p.location == DWARF_VALUE_LITERAL)
1205 p.v.literal.data = this->m_data;
1206 p.v.literal.length = this->m_len;
1208 else if (stack_empty_p ())
1210 p.location = DWARF_VALUE_OPTIMIZED_OUT;
1211 /* Also reset the context's location, for our callers. This is
1212 a somewhat strange approach, but this lets us avoid setting
1213 the location to DWARF_VALUE_MEMORY in all the individual
1214 cases in the evaluator. */
1215 this->m_location = DWARF_VALUE_OPTIMIZED_OUT;
1217 else if (p.location == DWARF_VALUE_MEMORY)
1219 p.v.mem.addr = fetch_address (0);
1220 p.v.mem.in_stack_memory = fetch_in_stack_memory (0);
1222 else if (p.location == DWARF_VALUE_IMPLICIT_POINTER)
1224 p.v.ptr.die_sect_off = (sect_offset) this->m_len;
1225 p.v.ptr.offset = value_as_long (fetch (0));
1227 else if (p.location == DWARF_VALUE_REGISTER)
1228 p.v.regno = value_as_long (fetch (0));
1231 p.v.value = fetch (0);
1235 /* Evaluate the expression at ADDR (LEN bytes long). */
1238 dwarf_expr_context::eval (const gdb_byte *addr, size_t len)
1240 int old_recursion_depth = this->m_recursion_depth;
1242 execute_stack_op (addr, addr + len);
1244 /* RECURSION_DEPTH becomes invalid if an exception was thrown here. */
1246 gdb_assert (this->m_recursion_depth == old_recursion_depth);
1249 /* Helper to read a uleb128 value or throw an error. */
1252 safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
1255 buf = gdb_read_uleb128 (buf, buf_end, r);
1257 error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
1261 /* Helper to read a sleb128 value or throw an error. */
1264 safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
1267 buf = gdb_read_sleb128 (buf, buf_end, r);
1269 error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
1274 safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
1276 buf = gdb_skip_leb128 (buf, buf_end);
1278 error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
1283 /* Check that the current operator is either at the end of an
1284 expression, or that it is followed by a composition operator or by
1285 DW_OP_GNU_uninit (which should terminate the expression). */
1288 dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
1289 const char *op_name)
1291 if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece
1292 && *op_ptr != DW_OP_GNU_uninit)
1293 error (_("DWARF-2 expression error: `%s' operations must be "
1294 "used either alone or in conjunction with DW_OP_piece "
1295 "or DW_OP_bit_piece."),
1299 /* Return true iff the types T1 and T2 are "the same". This only does
1300 checks that might reasonably be needed to compare DWARF base
1304 base_types_equal_p (struct type *t1, struct type *t2)
1306 if (t1->code () != t2->code ())
1308 if (t1->is_unsigned () != t2->is_unsigned ())
1310 return t1->length () == t2->length ();
1313 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
1314 DWARF register number. Otherwise return -1. */
1317 dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end)
1323 if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
1325 if (buf_end - buf != 1)
1327 return *buf - DW_OP_reg0;
1330 if (*buf == DW_OP_regval_type || *buf == DW_OP_GNU_regval_type)
1333 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
1336 buf = gdb_skip_leb128 (buf, buf_end);
1340 else if (*buf == DW_OP_regx)
1343 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
1349 if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
1354 /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
1355 DW_OP_deref* return the DWARF register number. Otherwise return -1.
1356 DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
1357 size from DW_OP_deref_size. */
1360 dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end,
1361 CORE_ADDR *deref_size_return)
1369 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
1371 dwarf_reg = *buf - DW_OP_breg0;
1376 else if (*buf == DW_OP_bregx)
1379 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
1382 if ((int) dwarf_reg != dwarf_reg)
1388 buf = gdb_read_sleb128 (buf, buf_end, &offset);
1394 if (*buf == DW_OP_deref)
1397 *deref_size_return = -1;
1399 else if (*buf == DW_OP_deref_size)
1404 *deref_size_return = *buf++;
1415 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
1416 in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */
1419 dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
1420 CORE_ADDR *fb_offset_return)
1427 if (*buf != DW_OP_fbreg)
1431 buf = gdb_read_sleb128 (buf, buf_end, &fb_offset);
1434 *fb_offset_return = fb_offset;
1435 if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
1441 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
1442 in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0.
1443 The matched SP register number depends on GDBARCH. */
1446 dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
1447 const gdb_byte *buf_end, CORE_ADDR *sp_offset_return)
1454 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
1456 dwarf_reg = *buf - DW_OP_breg0;
1461 if (*buf != DW_OP_bregx)
1464 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
1469 if (dwarf_reg_to_regnum (gdbarch, dwarf_reg)
1470 != gdbarch_sp_regnum (gdbarch))
1473 buf = gdb_read_sleb128 (buf, buf_end, &sp_offset);
1476 *sp_offset_return = sp_offset;
1477 if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
1483 /* The engine for the expression evaluator. Using the context in this
1484 object, evaluate the expression between OP_PTR and OP_END. */
1487 dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr,
1488 const gdb_byte *op_end)
1490 gdbarch *arch = this->m_per_objfile->objfile->arch ();
1491 bfd_endian byte_order = gdbarch_byte_order (arch);
1492 /* Old-style "untyped" DWARF values need special treatment in a
1493 couple of places, specifically DW_OP_mod and DW_OP_shr. We need
1494 a special type for these values so we can distinguish them from
1495 values that have an explicit type, because explicitly-typed
1496 values do not need special treatment. This special type must be
1497 different (in the `==' sense) from any base type coming from the
1499 type *address_type = this->address_type ();
1501 this->m_location = DWARF_VALUE_MEMORY;
1502 this->m_initialized = 1; /* Default is initialized. */
1504 if (this->m_recursion_depth > this->m_max_recursion_depth)
1505 error (_("DWARF-2 expression error: Loop detected (%d)."),
1506 this->m_recursion_depth);
1507 this->m_recursion_depth++;
1509 while (op_ptr < op_end)
1511 dwarf_location_atom op = (dwarf_location_atom) *op_ptr++;
1513 /* Assume the value is not in stack memory.
1514 Code that knows otherwise sets this to true.
1515 Some arithmetic on stack addresses can probably be assumed to still
1516 be a stack address, but we skip this complication for now.
1517 This is just an optimization, so it's always ok to punt
1518 and leave this as false. */
1519 bool in_stack_memory = false;
1520 uint64_t uoffset, reg;
1522 value *result_val = NULL;
1524 /* The DWARF expression might have a bug causing an infinite
1525 loop. In that case, quitting is the only way out. */
1562 result = op - DW_OP_lit0;
1563 result_val = value_from_ulongest (address_type, result);
1567 result = extract_unsigned_integer (op_ptr,
1568 this->m_addr_size, byte_order);
1569 op_ptr += this->m_addr_size;
1570 /* Some versions of GCC emit DW_OP_addr before
1571 DW_OP_GNU_push_tls_address. In this case the value is an
1572 index, not an address. We don't support things like
1573 branching between the address and the TLS op. */
1574 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
1575 result += this->m_per_objfile->objfile->text_section_offset ();
1576 result_val = value_from_ulongest (address_type, result);
1580 case DW_OP_GNU_addr_index:
1581 ensure_have_per_cu (this->m_per_cu, "DW_OP_addrx");
1583 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1584 result = dwarf2_read_addr_index (this->m_per_cu, this->m_per_objfile,
1586 result += this->m_per_objfile->objfile->text_section_offset ();
1587 result_val = value_from_ulongest (address_type, result);
1589 case DW_OP_GNU_const_index:
1590 ensure_have_per_cu (this->m_per_cu, "DW_OP_GNU_const_index");
1592 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1593 result = dwarf2_read_addr_index (this->m_per_cu, this->m_per_objfile,
1595 result_val = value_from_ulongest (address_type, result);
1599 result = extract_unsigned_integer (op_ptr, 1, byte_order);
1600 result_val = value_from_ulongest (address_type, result);
1604 result = extract_signed_integer (op_ptr, 1, byte_order);
1605 result_val = value_from_ulongest (address_type, result);
1609 result = extract_unsigned_integer (op_ptr, 2, byte_order);
1610 result_val = value_from_ulongest (address_type, result);
1614 result = extract_signed_integer (op_ptr, 2, byte_order);
1615 result_val = value_from_ulongest (address_type, result);
1619 result = extract_unsigned_integer (op_ptr, 4, byte_order);
1620 result_val = value_from_ulongest (address_type, result);
1624 result = extract_signed_integer (op_ptr, 4, byte_order);
1625 result_val = value_from_ulongest (address_type, result);
1629 result = extract_unsigned_integer (op_ptr, 8, byte_order);
1630 result_val = value_from_ulongest (address_type, result);
1634 result = extract_signed_integer (op_ptr, 8, byte_order);
1635 result_val = value_from_ulongest (address_type, result);
1639 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1641 result_val = value_from_ulongest (address_type, result);
1644 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
1646 result_val = value_from_ulongest (address_type, result);
1649 /* The DW_OP_reg operations are required to occur alone in
1650 location expressions. */
1683 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_reg");
1685 result = op - DW_OP_reg0;
1686 result_val = value_from_ulongest (address_type, result);
1687 this->m_location = DWARF_VALUE_REGISTER;
1691 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1692 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
1695 result_val = value_from_ulongest (address_type, result);
1696 this->m_location = DWARF_VALUE_REGISTER;
1699 case DW_OP_implicit_value:
1703 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
1704 if (op_ptr + len > op_end)
1705 error (_("DW_OP_implicit_value: too few bytes available."));
1707 this->m_data = op_ptr;
1708 this->m_location = DWARF_VALUE_LITERAL;
1710 dwarf_expr_require_composition (op_ptr, op_end,
1711 "DW_OP_implicit_value");
1715 case DW_OP_stack_value:
1716 this->m_location = DWARF_VALUE_STACK;
1717 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
1720 case DW_OP_implicit_pointer:
1721 case DW_OP_GNU_implicit_pointer:
1724 ensure_have_per_cu (this->m_per_cu, "DW_OP_implicit_pointer");
1726 int ref_addr_size = this->m_per_cu->ref_addr_size ();
1728 /* The referred-to DIE of sect_offset kind. */
1729 this->m_len = extract_unsigned_integer (op_ptr, ref_addr_size,
1731 op_ptr += ref_addr_size;
1733 /* The byte offset into the data. */
1734 op_ptr = safe_read_sleb128 (op_ptr, op_end, &len);
1735 result = (ULONGEST) len;
1736 result_val = value_from_ulongest (address_type, result);
1738 this->m_location = DWARF_VALUE_IMPLICIT_POINTER;
1739 dwarf_expr_require_composition (op_ptr, op_end,
1740 "DW_OP_implicit_pointer");
1777 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
1778 ensure_have_frame (this->m_frame, "DW_OP_breg");
1780 result = read_addr_from_reg (this->m_frame, op - DW_OP_breg0);
1782 result_val = value_from_ulongest (address_type, result);
1787 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1788 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
1789 ensure_have_frame (this->m_frame, "DW_OP_bregx");
1791 result = read_addr_from_reg (this->m_frame, reg);
1793 result_val = value_from_ulongest (address_type, result);
1798 const gdb_byte *datastart;
1801 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
1803 /* Rather than create a whole new context, we simply
1804 backup the current stack locally and install a new empty stack,
1805 then reset it afterwards, effectively erasing whatever the
1806 recursive call put there. */
1807 std::vector<dwarf_stack_value> saved_stack = std::move (this->m_stack);
1808 this->m_stack.clear ();
1810 /* FIXME: cagney/2003-03-26: This code should be using
1811 get_frame_base_address(), and then implement a dwarf2
1812 specific this_base method. */
1813 this->get_frame_base (&datastart, &datalen);
1814 eval (datastart, datalen);
1815 if (this->m_location == DWARF_VALUE_MEMORY)
1816 result = fetch_address (0);
1817 else if (this->m_location == DWARF_VALUE_REGISTER)
1819 = read_addr_from_reg (this->m_frame, value_as_long (fetch (0)));
1821 error (_("Not implemented: computing frame "
1822 "base using explicit value operator"));
1823 result = result + offset;
1824 result_val = value_from_ulongest (address_type, result);
1825 in_stack_memory = true;
1827 /* Restore the content of the original stack. */
1828 this->m_stack = std::move (saved_stack);
1830 this->m_location = DWARF_VALUE_MEMORY;
1835 result_val = fetch (0);
1836 in_stack_memory = fetch_in_stack_memory (0);
1845 result_val = fetch (offset);
1846 in_stack_memory = fetch_in_stack_memory (offset);
1851 if (this->m_stack.size () < 2)
1852 error (_("Not enough elements for "
1853 "DW_OP_swap. Need 2, have %zu."),
1854 this->m_stack.size ());
1856 dwarf_stack_value &t1 = this->m_stack[this->m_stack.size () - 1];
1857 dwarf_stack_value &t2 = this->m_stack[this->m_stack.size () - 2];
1863 result_val = fetch (1);
1864 in_stack_memory = fetch_in_stack_memory (1);
1869 if (this->m_stack.size () < 3)
1870 error (_("Not enough elements for "
1871 "DW_OP_rot. Need 3, have %zu."),
1872 this->m_stack.size ());
1874 dwarf_stack_value temp = this->m_stack[this->m_stack.size () - 1];
1875 this->m_stack[this->m_stack.size () - 1]
1876 = this->m_stack[this->m_stack.size () - 2];
1877 this->m_stack[this->m_stack.size () - 2]
1878 = this->m_stack[this->m_stack.size () - 3];
1879 this->m_stack[this->m_stack.size () - 3] = temp;
1884 case DW_OP_deref_size:
1885 case DW_OP_deref_type:
1886 case DW_OP_GNU_deref_type:
1888 int addr_size = (op == DW_OP_deref ? this->m_addr_size : *op_ptr++);
1889 gdb_byte *buf = (gdb_byte *) alloca (addr_size);
1890 CORE_ADDR addr = fetch_address (0);
1895 if (op == DW_OP_deref_type || op == DW_OP_GNU_deref_type)
1897 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1898 cu_offset type_die_cu_off = (cu_offset) uoffset;
1899 type = get_base_type (type_die_cu_off);
1902 type = address_type;
1904 this->read_mem (buf, addr, addr_size);
1906 /* If the size of the object read from memory is different
1907 from the type length, we need to zero-extend it. */
1908 if (type->length () != addr_size)
1911 extract_unsigned_integer (buf, addr_size, byte_order);
1913 buf = (gdb_byte *) alloca (type->length ());
1914 store_unsigned_integer (buf, type->length (),
1918 result_val = value_from_contents_and_address (type, buf, addr);
1925 case DW_OP_plus_uconst:
1927 /* Unary operations. */
1928 result_val = fetch (0);
1934 if (value_less (result_val,
1935 value_zero (value_type (result_val), not_lval)))
1936 result_val = value_neg (result_val);
1939 result_val = value_neg (result_val);
1942 dwarf_require_integral (value_type (result_val));
1943 result_val = value_complement (result_val);
1945 case DW_OP_plus_uconst:
1946 dwarf_require_integral (value_type (result_val));
1947 result = value_as_long (result_val);
1948 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1950 result_val = value_from_ulongest (address_type, result);
1974 /* Binary operations. */
1975 struct value *first, *second;
1983 if (! base_types_equal_p (value_type (first), value_type (second)))
1984 error (_("Incompatible types on DWARF stack"));
1989 dwarf_require_integral (value_type (first));
1990 dwarf_require_integral (value_type (second));
1991 result_val = value_binop (first, second, BINOP_BITWISE_AND);
1994 result_val = value_binop (first, second, BINOP_DIV);
1997 result_val = value_binop (first, second, BINOP_SUB);
2002 struct type *orig_type = value_type (first);
2004 /* We have to special-case "old-style" untyped values
2005 -- these must have mod computed using unsigned
2007 if (orig_type == address_type)
2009 struct type *utype = get_unsigned_type (arch, orig_type);
2012 first = value_cast (utype, first);
2013 second = value_cast (utype, second);
2015 /* Note that value_binop doesn't handle float or
2016 decimal float here. This seems unimportant. */
2017 result_val = value_binop (first, second, BINOP_MOD);
2019 result_val = value_cast (orig_type, result_val);
2023 result_val = value_binop (first, second, BINOP_MUL);
2026 dwarf_require_integral (value_type (first));
2027 dwarf_require_integral (value_type (second));
2028 result_val = value_binop (first, second, BINOP_BITWISE_IOR);
2031 result_val = value_binop (first, second, BINOP_ADD);
2034 dwarf_require_integral (value_type (first));
2035 dwarf_require_integral (value_type (second));
2036 result_val = value_binop (first, second, BINOP_LSH);
2039 dwarf_require_integral (value_type (first));
2040 dwarf_require_integral (value_type (second));
2041 if (!value_type (first)->is_unsigned ())
2044 = get_unsigned_type (arch, value_type (first));
2046 first = value_cast (utype, first);
2049 result_val = value_binop (first, second, BINOP_RSH);
2050 /* Make sure we wind up with the same type we started
2052 if (value_type (result_val) != value_type (second))
2053 result_val = value_cast (value_type (second), result_val);
2056 dwarf_require_integral (value_type (first));
2057 dwarf_require_integral (value_type (second));
2058 if (value_type (first)->is_unsigned ())
2061 = get_signed_type (arch, value_type (first));
2063 first = value_cast (stype, first);
2066 result_val = value_binop (first, second, BINOP_RSH);
2067 /* Make sure we wind up with the same type we started
2069 if (value_type (result_val) != value_type (second))
2070 result_val = value_cast (value_type (second), result_val);
2073 dwarf_require_integral (value_type (first));
2074 dwarf_require_integral (value_type (second));
2075 result_val = value_binop (first, second, BINOP_BITWISE_XOR);
2078 /* A <= B is !(B < A). */
2079 result = ! value_less (second, first);
2080 result_val = value_from_ulongest (address_type, result);
2083 /* A >= B is !(A < B). */
2084 result = ! value_less (first, second);
2085 result_val = value_from_ulongest (address_type, result);
2088 result = value_equal (first, second);
2089 result_val = value_from_ulongest (address_type, result);
2092 result = value_less (first, second);
2093 result_val = value_from_ulongest (address_type, result);
2096 /* A > B is B < A. */
2097 result = value_less (second, first);
2098 result_val = value_from_ulongest (address_type, result);
2101 result = ! value_equal (first, second);
2102 result_val = value_from_ulongest (address_type, result);
2105 internal_error (_("Can't be reached."));
2110 case DW_OP_call_frame_cfa:
2111 ensure_have_frame (this->m_frame, "DW_OP_call_frame_cfa");
2113 result = dwarf2_frame_cfa (this->m_frame);
2114 result_val = value_from_ulongest (address_type, result);
2115 in_stack_memory = true;
2118 case DW_OP_GNU_push_tls_address:
2119 case DW_OP_form_tls_address:
2120 /* Variable is at a constant offset in the thread-local
2121 storage block into the objfile for the current thread and
2122 the dynamic linker module containing this expression. Here
2123 we return returns the offset from that base. The top of the
2124 stack has the offset from the beginning of the thread
2125 control block at which the variable is located. Nothing
2126 should follow this operator, so the top of stack would be
2128 result = value_as_long (fetch (0));
2130 result = target_translate_tls_address (this->m_per_objfile->objfile,
2132 result_val = value_from_ulongest (address_type, result);
2136 offset = extract_signed_integer (op_ptr, 2, byte_order);
2145 offset = extract_signed_integer (op_ptr, 2, byte_order);
2148 dwarf_require_integral (value_type (val));
2149 if (value_as_long (val) != 0)
2162 /* Record the piece. */
2163 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
2164 add_piece (8 * size, 0);
2166 /* Pop off the address/regnum, and reset the location
2168 if (this->m_location != DWARF_VALUE_LITERAL
2169 && this->m_location != DWARF_VALUE_OPTIMIZED_OUT)
2171 this->m_location = DWARF_VALUE_MEMORY;
2175 case DW_OP_bit_piece:
2177 uint64_t size, uleb_offset;
2179 /* Record the piece. */
2180 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
2181 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uleb_offset);
2182 add_piece (size, uleb_offset);
2184 /* Pop off the address/regnum, and reset the location
2186 if (this->m_location != DWARF_VALUE_LITERAL
2187 && this->m_location != DWARF_VALUE_OPTIMIZED_OUT)
2189 this->m_location = DWARF_VALUE_MEMORY;
2193 case DW_OP_GNU_uninit:
2194 if (op_ptr != op_end)
2195 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
2196 "be the very last op."));
2198 this->m_initialized = 0;
2204 = (cu_offset) extract_unsigned_integer (op_ptr, 2, byte_order);
2206 this->dwarf_call (cu_off);
2213 = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order);
2215 this->dwarf_call (cu_off);
2219 case DW_OP_GNU_variable_value:
2221 ensure_have_per_cu (this->m_per_cu, "DW_OP_GNU_variable_value");
2222 int ref_addr_size = this->m_per_cu->ref_addr_size ();
2224 sect_offset sect_off
2225 = (sect_offset) extract_unsigned_integer (op_ptr,
2228 op_ptr += ref_addr_size;
2229 result_val = sect_variable_value (sect_off, this->m_per_cu,
2230 this->m_per_objfile);
2231 result_val = value_cast (address_type, result_val);
2235 case DW_OP_entry_value:
2236 case DW_OP_GNU_entry_value:
2239 CORE_ADDR deref_size;
2240 union call_site_parameter_u kind_u;
2242 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
2243 if (op_ptr + len > op_end)
2244 error (_("DW_OP_entry_value: too few bytes available."));
2246 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len);
2247 if (kind_u.dwarf_reg != -1)
2250 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG,
2252 -1 /* deref_size */);
2256 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr,
2259 if (kind_u.dwarf_reg != -1)
2261 if (deref_size == -1)
2262 deref_size = this->m_addr_size;
2264 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG,
2265 kind_u, deref_size);
2269 error (_("DWARF-2 expression error: DW_OP_entry_value is "
2270 "supported only for single DW_OP_reg* "
2271 "or for DW_OP_breg*(0)+DW_OP_deref*"));
2274 case DW_OP_GNU_parameter_ref:
2276 union call_site_parameter_u kind_u;
2279 = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order);
2281 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_PARAM_OFFSET,
2283 -1 /* deref_size */);
2287 case DW_OP_const_type:
2288 case DW_OP_GNU_const_type:
2291 const gdb_byte *data;
2294 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
2295 cu_offset type_die_cu_off = (cu_offset) uoffset;
2301 type = get_base_type (type_die_cu_off);
2303 if (type->length () != n)
2304 error (_("DW_OP_const_type has different sizes for type and data"));
2306 result_val = value_from_contents (type, data);
2310 case DW_OP_regval_type:
2311 case DW_OP_GNU_regval_type:
2313 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
2314 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
2315 cu_offset type_die_cu_off = (cu_offset) uoffset;
2317 ensure_have_frame (this->m_frame, "DW_OP_regval_type");
2319 struct type *type = get_base_type (type_die_cu_off);
2321 = dwarf_reg_to_regnum_or_error (get_frame_arch (this->m_frame),
2323 result_val = value_from_register (type, regnum, this->m_frame);
2328 case DW_OP_GNU_convert:
2329 case DW_OP_reinterpret:
2330 case DW_OP_GNU_reinterpret:
2334 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
2335 cu_offset type_die_cu_off = (cu_offset) uoffset;
2337 if (to_underlying (type_die_cu_off) == 0)
2338 type = address_type;
2340 type = get_base_type (type_die_cu_off);
2342 result_val = fetch (0);
2345 if (op == DW_OP_convert || op == DW_OP_GNU_convert)
2346 result_val = value_cast (type, result_val);
2347 else if (type == value_type (result_val))
2351 else if (type->length ()
2352 != value_type (result_val)->length ())
2353 error (_("DW_OP_reinterpret has wrong size"));
2356 = value_from_contents (type,
2357 value_contents_all (result_val).data ());
2361 case DW_OP_push_object_address:
2362 /* Return the address of the object we are currently observing. */
2363 if (this->m_addr_info == nullptr
2364 || (this->m_addr_info->valaddr.data () == nullptr
2365 && this->m_addr_info->addr == 0))
2366 error (_("Location address is not set."));
2369 = value_from_ulongest (address_type, this->m_addr_info->addr);
2373 error (_("Unhandled dwarf expression opcode 0x%x"), op);
2376 /* Most things push a result value. */
2377 gdb_assert (result_val != NULL);
2378 push (result_val, in_stack_memory);
2383 /* To simplify our main caller, if the result is an implicit
2384 pointer, then make a pieced value. This is ok because we can't
2385 have implicit pointers in contexts where pieces are invalid. */
2386 if (this->m_location == DWARF_VALUE_IMPLICIT_POINTER)
2387 add_piece (8 * this->m_addr_size, 0);
2389 this->m_recursion_depth--;
2390 gdb_assert (this->m_recursion_depth >= 0);