Automatic date update in version.in
[binutils-gdb.git] / gdb / dwarf2 / expr.c
blobd3e3e97ba079dda3ce941e90cf1ee3977c87c420
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/>. */
22 #include "defs.h"
23 #include "block.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "value.h"
27 #include "gdbcore.h"
28 #include "dwarf2.h"
29 #include "dwarf2/expr.h"
30 #include "dwarf2/loc.h"
31 #include "dwarf2/read.h"
32 #include "frame.h"
33 #include "gdbsupport/underlying.h"
34 #include "gdbarch.h"
35 #include "objfiles.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. */
51 static void
52 ensure_have_frame (frame_info *frame, const char *op_name)
54 if (frame == nullptr)
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. */
61 static void
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. */
72 static size_t
73 bits_to_bytes (ULONGEST start, ULONGEST n_bits)
75 return (start % HOST_CHAR_BIT + n_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
78 /* See expr.h. */
80 CORE_ADDR
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);
89 struct piece_closure
91 /* Reference count. */
92 int refc = 0;
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
109 PIECES. */
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 *frame)
117 piece_closure *c = new piece_closure;
119 c->refc = 1;
120 /* We must capture this here due to sharing of DWARF state. */
121 c->per_objfile = per_objfile;
122 c->per_cu = per_cu;
123 c->pieces = std::move (pieces);
124 if (frame == nullptr)
125 c->frame_id = null_frame_id;
126 else
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);
133 return c;
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. */
144 static bool
145 rw_pieced_value (value *v, value *from, bool check_optimized)
147 int i;
148 LONGEST offset = 0, max_offset;
149 gdb_byte *v_contents;
150 const gdb_byte *from_contents;
151 piece_closure *c
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);
157 if (from != nullptr)
159 from_contents = value_contents (from).data ();
160 v_contents = nullptr;
162 else
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"));
168 if (check_optimized)
169 v_contents = nullptr;
170 else
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))
179 + value_bitpos (v));
180 if (from != nullptr
181 && (type_byte_order (value_type (from))
182 == BFD_ENDIAN_BIG))
184 /* Use the least significant bits of FROM. */
185 max_offset = 8 * value_type (from)->length ();
186 offset = max_offset - value_bitsize (v);
188 else
189 max_offset = value_bitsize (v);
191 else
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;
207 switch (p->location)
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);
215 int optim, unavail;
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);
223 else
224 bits_to_skip += p->offset;
226 this_size = bits_to_bytes (bits_to_skip, this_size_bits);
227 buffer.resize (this_size);
229 if (from == nullptr)
231 /* Read mode. */
232 if (!get_frame_register_bytes (frame, gdb_regnum,
233 bits_to_skip / 8,
234 buffer, &optim, &unavail))
236 if (optim)
238 if (check_optimized)
239 return true;
240 mark_value_bits_optimized_out (v, offset,
241 this_size_bits);
243 if (unavail && !check_optimized)
244 mark_value_bits_unavailable (v, offset,
245 this_size_bits);
246 break;
249 if (!check_optimized)
250 copy_bitwise (v_contents, offset,
251 buffer.data (), bits_to_skip % 8,
252 this_size_bits, bits_big_endian);
254 else
256 /* Write mode. */
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,
262 bits_to_skip / 8,
263 buffer, &optim, &unavail);
264 if (optim)
265 throw_error (OPTIMIZED_OUT_ERROR,
266 _("Can't do read-modify-write to "
267 "update bitfield; containing word "
268 "has been optimized out"));
269 if (unavail)
270 throw_error (NOT_AVAILABLE_ERROR,
271 _("Can't do read-modify-write to "
272 "update bitfield; containing word "
273 "is unavailable"));
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,
280 bits_to_skip / 8,
281 buffer);
284 break;
286 case DWARF_VALUE_MEMORY:
288 if (check_optimized)
289 break;
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
296 && offset % 8 == 0)
298 /* Everything is byte-aligned; no buffer needed. */
299 if (from != nullptr)
300 write_memory_with_notification (start_addr,
301 (from_contents
302 + offset / 8),
303 this_size_bits / 8);
304 else
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,
309 this_size_bits / 8);
310 break;
313 this_size = bits_to_bytes (bits_to_skip, this_size_bits);
314 buffer.resize (this_size);
316 if (from == nullptr)
318 /* Read mode. */
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);
327 else
329 /* Write mode. */
330 if (bits_to_skip % 8 != 0 || this_size_bits % 8 != 0)
332 if (this_size <= 8)
334 /* Perform a single read for small sizes. */
335 read_memory (start_addr, buffer.data (),
336 this_size);
338 else
340 /* Only the first and last bytes can possibly have
341 any bits reused. */
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,
352 buffer.data (),
353 this_size);
356 break;
358 case DWARF_VALUE_STACK:
360 if (check_optimized)
361 break;
363 if (from != nullptr)
365 mark_value_bits_optimized_out (v, offset, this_size_bits);
366 break;
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)
375 break;
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;
380 else
381 bits_to_skip += p->offset;
383 copy_bitwise (v_contents, offset,
384 value_contents_all (p->v.value).data (),
385 bits_to_skip,
386 this_size_bits, bits_big_endian);
388 break;
390 case DWARF_VALUE_LITERAL:
392 if (check_optimized)
393 break;
395 if (from != nullptr)
397 mark_value_bits_optimized_out (v, offset, this_size_bits);
398 break;
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)
407 break;
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,
413 n, bits_big_endian);
415 break;
417 case DWARF_VALUE_IMPLICIT_POINTER:
418 if (from != nullptr)
420 mark_value_bits_optimized_out (v, offset, this_size_bits);
421 break;
424 /* These bits show up as zeros -- but do not cause the value to
425 be considered optimized-out. */
426 break;
428 case DWARF_VALUE_OPTIMIZED_OUT:
429 if (check_optimized)
430 return true;
431 mark_value_bits_optimized_out (v, offset, this_size_bits);
432 break;
434 default:
435 internal_error (__FILE__, __LINE__, _("invalid location type"));
438 offset += this_size_bits;
439 bits_to_skip = 0;
442 return false;
445 static void
446 read_pieced_value (value *v)
448 rw_pieced_value (v, nullptr, false);
451 static void
452 write_pieced_value (value *to, value *from)
454 rw_pieced_value (to, from, false);
457 static bool
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. */
466 static int
467 check_pieced_synthetic_pointer (const value *value, LONGEST bit_offset,
468 int bit_length)
470 piece_closure *c = (piece_closure *) value_computed_closure (value);
471 int i;
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;
482 if (bit_offset > 0)
484 if (bit_offset >= this_size_bits)
486 bit_offset -= this_size_bits;
487 continue;
490 bit_length -= this_size_bits - bit_offset;
491 bit_offset = 0;
493 else
494 bit_length -= this_size_bits;
496 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
497 return 0;
500 return 1;
503 /* An implementation of an lval_funcs method to indirect through a
504 pointer. This handles the synthetic pointer case when needed. */
506 static value *
507 indirect_pieced_value (value *value)
509 piece_closure *c
510 = (piece_closure *) value_computed_closure (value);
511 int i;
512 dwarf_expr_piece *piece = NULL;
514 struct type *type = check_typedef (value_type (value));
515 if (type->code () != TYPE_CODE_PTR)
516 return NULL;
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;
528 if (bit_offset > 0)
530 if (bit_offset >= this_size_bits)
532 bit_offset -= this_size_bits;
533 continue;
536 bit_length -= this_size_bits - bit_offset;
537 bit_offset = 0;
539 else
540 bit_length -= this_size_bits;
542 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
543 return NULL;
545 if (bit_length != 0)
546 error (_("Invalid use of DW_OP_implicit_pointer"));
548 piece = p;
549 break;
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));
566 LONGEST byte_offset
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++
576 references. */
578 static value *
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);
588 frame_info *frame
589 = get_selected_frame (_("No frame selected."));
591 /* gdb represents synthetic pointers as pieced values with a single
592 piece. */
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);
601 else
603 /* Else: not a synthetic reference; do nothing. */
604 return NULL;
608 static void *
609 copy_pieced_value_closure (const value *v)
611 piece_closure *c = (piece_closure *) value_computed_closure (v);
613 ++c->refc;
614 return c;
617 static void
618 free_pieced_value_closure (value *v)
620 piece_closure *c = (piece_closure *) value_computed_closure (v);
622 --c->refc;
623 if (c->refc == 0)
625 for (dwarf_expr_piece &p : c->pieces)
626 if (p.location == DWARF_VALUE_STACK)
627 value_decref (p.v.value);
629 delete c;
633 /* Functions for accessing a variable described by DW_OP_piece. */
634 static const struct lval_funcs pieced_value_funcs = {
635 read_pieced_value,
636 write_pieced_value,
637 is_optimized_out_pieced_value,
638 indirect_pieced_value,
639 coerce_pieced_ref,
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. */
649 static value *
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,
657 &var_name);
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)
674 return result;
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,
680 type, true);
683 /* Return the type used for DWARF operations where the type is
684 unspecified in the DWARF spec. Only certain sizes are
685 supported. */
687 struct type *
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);
694 int ndx;
696 if (this->m_addr_size == 2)
697 ndx = 0;
698 else if (this->m_addr_size == 4)
699 ndx = 1;
700 else if (this->m_addr_size == 8)
701 ndx = 2;
702 else
703 error (_("Unsupported address size in DWARF expressions: %d bits"),
704 8 * this->m_addr_size);
706 if (types->dw_types[ndx] == NULL)
707 types->dw_types[ndx]
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,
717 int addr_size)
718 : m_addr_size (addr_size),
719 m_per_objfile (per_objfile)
723 /* Push VALUE onto the stack. */
725 void
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. */
733 void
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. */
741 void
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. */
752 struct value *
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;
762 /* See expr.h. */
764 void
765 dwarf_expr_context::get_frame_base (const gdb_byte **start,
766 size_t * length)
768 ensure_have_frame (this->m_frame, "DW_OP_fbreg");
770 const block *bl = get_frame_block (this->m_frame, NULL);
772 if (bl == 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
777 inlined function. */
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),
787 start, length);
790 /* See expr.h. */
792 struct type *
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"));
804 return result;
807 /* See expr.h. */
809 void
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);
832 /* See expr.h. */
834 void
835 dwarf_expr_context::read_mem (gdb_byte *buf, CORE_ADDR addr,
836 size_t length)
838 if (length == 0)
839 return;
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);
850 return;
854 read_memory (addr, buf, length);
857 /* See expr.h. */
859 void
860 dwarf_expr_context::push_dwarf_reg_entry_value (call_site_parameter_kind kind,
861 call_site_parameter_u kind_u,
862 int deref_size)
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,
872 &caller_per_cu,
873 &caller_per_objfile);
874 const gdb_byte *data_src
875 = deref_size == -1 ? parameter->value : parameter->data_value;
876 size_t size
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,
891 caller_frame);
892 scoped_restore save_per_cu = make_scoped_restore (&this->m_per_cu,
893 caller_per_cu);
894 scoped_restore save_addr_info = make_scoped_restore (&this->m_addr_info,
895 nullptr);
896 scoped_restore save_per_objfile = make_scoped_restore (&this->m_per_objfile,
897 caller_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);
905 /* See expr.h. */
907 value *
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 ();
914 if (type == nullptr)
915 type = address_type ();
917 if (subobj_type == nullptr)
918 subobj_type = type;
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
932 outer type. */
933 if (bit_size > 8 * type->length ())
934 invalid_synthetic_pointer ();
936 piece_closure *c
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);
943 else
945 /* If AS_LVAL is false, means that the implicit conversion
946 from a location description to value is expected. */
947 if (!as_lval)
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);
955 int dwarf_regnum
956 = longest_to_int (value_as_long (this->fetch (0)));
957 int gdb_regnum = dwarf_reg_to_regnum_or_error (f_arch,
958 dwarf_regnum);
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,
966 this->m_frame);
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 ());
978 retval = tmp;
981 break;
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 ())
999 case TYPE_CODE_FUNC:
1000 case TYPE_CODE_METHOD:
1001 ptr_type = builtin_type (arch)->builtin_func_ptr;
1002 break;
1003 default:
1004 ptr_type = builtin_type (arch)->builtin_data_ptr;
1005 break;
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);
1014 break;
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));
1035 break;
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);
1048 break;
1050 case DWARF_VALUE_OPTIMIZED_OUT:
1051 retval = allocate_optimized_out_value (subobj_type);
1052 break;
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. */
1059 default:
1060 internal_error (__FILE__, __LINE__, _("invalid location type"));
1064 set_value_initialized (retval, this->m_initialized);
1066 return retval;
1069 /* See expr.h. */
1071 value *
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;
1082 eval (addr, len);
1083 return fetch_result (type, subobj_type, subobj_offset, as_lval);
1086 /* Require that TYPE be an integral type; throw an exception if not. */
1088 static void
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
1098 type. */
1100 static struct type *
1101 get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
1103 switch (type->length ())
1105 case 1:
1106 return builtin_type (gdbarch)->builtin_uint8;
1107 case 2:
1108 return builtin_type (gdbarch)->builtin_uint16;
1109 case 4:
1110 return builtin_type (gdbarch)->builtin_uint32;
1111 case 8:
1112 return builtin_type (gdbarch)->builtin_uint64;
1113 default:
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
1120 type. */
1122 static struct type *
1123 get_signed_type (struct gdbarch *gdbarch, struct type *type)
1125 switch (type->length ())
1127 case 1:
1128 return builtin_type (gdbarch)->builtin_int8;
1129 case 2:
1130 return builtin_type (gdbarch)->builtin_int16;
1131 case 4:
1132 return builtin_type (gdbarch)->builtin_int32;
1133 case 8:
1134 return builtin_type (gdbarch)->builtin_int64;
1135 default:
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. */
1143 CORE_ADDR
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);
1149 ULONGEST result;
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. */
1175 bool
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. */
1187 bool
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. */
1194 void
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;
1201 p.size = size;
1202 p.offset = offset;
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));
1230 else
1232 p.v.value = fetch (0);
1236 /* Evaluate the expression at ADDR (LEN bytes long). */
1238 void
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. */
1252 const gdb_byte *
1253 safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
1254 uint64_t *r)
1256 buf = gdb_read_uleb128 (buf, buf_end, r);
1257 if (buf == NULL)
1258 error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
1259 return buf;
1262 /* Helper to read a sleb128 value or throw an error. */
1264 const gdb_byte *
1265 safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
1266 int64_t *r)
1268 buf = gdb_read_sleb128 (buf, buf_end, r);
1269 if (buf == NULL)
1270 error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
1271 return buf;
1274 const gdb_byte *
1275 safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
1277 buf = gdb_skip_leb128 (buf, buf_end);
1278 if (buf == NULL)
1279 error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
1280 return buf;
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). */
1288 void
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."),
1297 op_name);
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
1302 types. */
1304 static int
1305 base_types_equal_p (struct type *t1, struct type *t2)
1307 if (t1->code () != t2->code ())
1308 return 0;
1309 if (t1->is_unsigned () != t2->is_unsigned ())
1310 return 0;
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)
1320 uint64_t dwarf_reg;
1322 if (buf_end <= buf)
1323 return -1;
1324 if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
1326 if (buf_end - buf != 1)
1327 return -1;
1328 return *buf - DW_OP_reg0;
1331 if (*buf == DW_OP_regval_type || *buf == DW_OP_GNU_regval_type)
1333 buf++;
1334 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
1335 if (buf == NULL)
1336 return -1;
1337 buf = gdb_skip_leb128 (buf, buf_end);
1338 if (buf == NULL)
1339 return -1;
1341 else if (*buf == DW_OP_regx)
1343 buf++;
1344 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
1345 if (buf == NULL)
1346 return -1;
1348 else
1349 return -1;
1350 if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
1351 return -1;
1352 return 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)
1364 uint64_t dwarf_reg;
1365 int64_t offset;
1367 if (buf_end <= buf)
1368 return -1;
1370 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
1372 dwarf_reg = *buf - DW_OP_breg0;
1373 buf++;
1374 if (buf >= buf_end)
1375 return -1;
1377 else if (*buf == DW_OP_bregx)
1379 buf++;
1380 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
1381 if (buf == NULL)
1382 return -1;
1383 if ((int) dwarf_reg != dwarf_reg)
1384 return -1;
1386 else
1387 return -1;
1389 buf = gdb_read_sleb128 (buf, buf_end, &offset);
1390 if (buf == NULL)
1391 return -1;
1392 if (offset != 0)
1393 return -1;
1395 if (*buf == DW_OP_deref)
1397 buf++;
1398 *deref_size_return = -1;
1400 else if (*buf == DW_OP_deref_size)
1402 buf++;
1403 if (buf >= buf_end)
1404 return -1;
1405 *deref_size_return = *buf++;
1407 else
1408 return -1;
1410 if (buf != buf_end)
1411 return -1;
1413 return dwarf_reg;
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)
1423 int64_t fb_offset;
1425 if (buf_end <= buf)
1426 return 0;
1428 if (*buf != DW_OP_fbreg)
1429 return 0;
1430 buf++;
1432 buf = gdb_read_sleb128 (buf, buf_end, &fb_offset);
1433 if (buf == NULL)
1434 return 0;
1435 *fb_offset_return = fb_offset;
1436 if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
1437 return 0;
1439 return 1;
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)
1450 uint64_t dwarf_reg;
1451 int64_t sp_offset;
1453 if (buf_end <= buf)
1454 return 0;
1455 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
1457 dwarf_reg = *buf - DW_OP_breg0;
1458 buf++;
1460 else
1462 if (*buf != DW_OP_bregx)
1463 return 0;
1464 buf++;
1465 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
1466 if (buf == NULL)
1467 return 0;
1470 if (dwarf_reg_to_regnum (gdbarch, dwarf_reg)
1471 != gdbarch_sp_regnum (gdbarch))
1472 return 0;
1474 buf = gdb_read_sleb128 (buf, buf_end, &sp_offset);
1475 if (buf == NULL)
1476 return 0;
1477 *sp_offset_return = sp_offset;
1478 if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
1479 return 0;
1481 return 1;
1484 /* The engine for the expression evaluator. Using the context in this
1485 object, evaluate the expression between OP_PTR and OP_END. */
1487 void
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
1499 CU. */
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++;
1513 ULONGEST result;
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;
1522 int64_t offset;
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. */
1527 QUIT;
1529 switch (op)
1531 case DW_OP_lit0:
1532 case DW_OP_lit1:
1533 case DW_OP_lit2:
1534 case DW_OP_lit3:
1535 case DW_OP_lit4:
1536 case DW_OP_lit5:
1537 case DW_OP_lit6:
1538 case DW_OP_lit7:
1539 case DW_OP_lit8:
1540 case DW_OP_lit9:
1541 case DW_OP_lit10:
1542 case DW_OP_lit11:
1543 case DW_OP_lit12:
1544 case DW_OP_lit13:
1545 case DW_OP_lit14:
1546 case DW_OP_lit15:
1547 case DW_OP_lit16:
1548 case DW_OP_lit17:
1549 case DW_OP_lit18:
1550 case DW_OP_lit19:
1551 case DW_OP_lit20:
1552 case DW_OP_lit21:
1553 case DW_OP_lit22:
1554 case DW_OP_lit23:
1555 case DW_OP_lit24:
1556 case DW_OP_lit25:
1557 case DW_OP_lit26:
1558 case DW_OP_lit27:
1559 case DW_OP_lit28:
1560 case DW_OP_lit29:
1561 case DW_OP_lit30:
1562 case DW_OP_lit31:
1563 result = op - DW_OP_lit0;
1564 result_val = value_from_ulongest (address_type, result);
1565 break;
1567 case DW_OP_addr:
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);
1578 break;
1580 case DW_OP_addrx:
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,
1586 uoffset);
1587 result += this->m_per_objfile->objfile->text_section_offset ();
1588 result_val = value_from_ulongest (address_type, result);
1589 break;
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,
1595 uoffset);
1596 result_val = value_from_ulongest (address_type, result);
1597 break;
1599 case DW_OP_const1u:
1600 result = extract_unsigned_integer (op_ptr, 1, byte_order);
1601 result_val = value_from_ulongest (address_type, result);
1602 op_ptr += 1;
1603 break;
1604 case DW_OP_const1s:
1605 result = extract_signed_integer (op_ptr, 1, byte_order);
1606 result_val = value_from_ulongest (address_type, result);
1607 op_ptr += 1;
1608 break;
1609 case DW_OP_const2u:
1610 result = extract_unsigned_integer (op_ptr, 2, byte_order);
1611 result_val = value_from_ulongest (address_type, result);
1612 op_ptr += 2;
1613 break;
1614 case DW_OP_const2s:
1615 result = extract_signed_integer (op_ptr, 2, byte_order);
1616 result_val = value_from_ulongest (address_type, result);
1617 op_ptr += 2;
1618 break;
1619 case DW_OP_const4u:
1620 result = extract_unsigned_integer (op_ptr, 4, byte_order);
1621 result_val = value_from_ulongest (address_type, result);
1622 op_ptr += 4;
1623 break;
1624 case DW_OP_const4s:
1625 result = extract_signed_integer (op_ptr, 4, byte_order);
1626 result_val = value_from_ulongest (address_type, result);
1627 op_ptr += 4;
1628 break;
1629 case DW_OP_const8u:
1630 result = extract_unsigned_integer (op_ptr, 8, byte_order);
1631 result_val = value_from_ulongest (address_type, result);
1632 op_ptr += 8;
1633 break;
1634 case DW_OP_const8s:
1635 result = extract_signed_integer (op_ptr, 8, byte_order);
1636 result_val = value_from_ulongest (address_type, result);
1637 op_ptr += 8;
1638 break;
1639 case DW_OP_constu:
1640 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1641 result = uoffset;
1642 result_val = value_from_ulongest (address_type, result);
1643 break;
1644 case DW_OP_consts:
1645 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
1646 result = offset;
1647 result_val = value_from_ulongest (address_type, result);
1648 break;
1650 /* The DW_OP_reg operations are required to occur alone in
1651 location expressions. */
1652 case DW_OP_reg0:
1653 case DW_OP_reg1:
1654 case DW_OP_reg2:
1655 case DW_OP_reg3:
1656 case DW_OP_reg4:
1657 case DW_OP_reg5:
1658 case DW_OP_reg6:
1659 case DW_OP_reg7:
1660 case DW_OP_reg8:
1661 case DW_OP_reg9:
1662 case DW_OP_reg10:
1663 case DW_OP_reg11:
1664 case DW_OP_reg12:
1665 case DW_OP_reg13:
1666 case DW_OP_reg14:
1667 case DW_OP_reg15:
1668 case DW_OP_reg16:
1669 case DW_OP_reg17:
1670 case DW_OP_reg18:
1671 case DW_OP_reg19:
1672 case DW_OP_reg20:
1673 case DW_OP_reg21:
1674 case DW_OP_reg22:
1675 case DW_OP_reg23:
1676 case DW_OP_reg24:
1677 case DW_OP_reg25:
1678 case DW_OP_reg26:
1679 case DW_OP_reg27:
1680 case DW_OP_reg28:
1681 case DW_OP_reg29:
1682 case DW_OP_reg30:
1683 case DW_OP_reg31:
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;
1689 break;
1691 case DW_OP_regx:
1692 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
1693 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
1695 result = reg;
1696 result_val = value_from_ulongest (address_type, result);
1697 this->m_location = DWARF_VALUE_REGISTER;
1698 break;
1700 case DW_OP_implicit_value:
1702 uint64_t len;
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."));
1707 this->m_len = len;
1708 this->m_data = op_ptr;
1709 this->m_location = DWARF_VALUE_LITERAL;
1710 op_ptr += len;
1711 dwarf_expr_require_composition (op_ptr, op_end,
1712 "DW_OP_implicit_value");
1714 goto no_push;
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");
1719 goto no_push;
1721 case DW_OP_implicit_pointer:
1722 case DW_OP_GNU_implicit_pointer:
1724 int64_t len;
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,
1731 byte_order);
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");
1743 break;
1745 case DW_OP_breg0:
1746 case DW_OP_breg1:
1747 case DW_OP_breg2:
1748 case DW_OP_breg3:
1749 case DW_OP_breg4:
1750 case DW_OP_breg5:
1751 case DW_OP_breg6:
1752 case DW_OP_breg7:
1753 case DW_OP_breg8:
1754 case DW_OP_breg9:
1755 case DW_OP_breg10:
1756 case DW_OP_breg11:
1757 case DW_OP_breg12:
1758 case DW_OP_breg13:
1759 case DW_OP_breg14:
1760 case DW_OP_breg15:
1761 case DW_OP_breg16:
1762 case DW_OP_breg17:
1763 case DW_OP_breg18:
1764 case DW_OP_breg19:
1765 case DW_OP_breg20:
1766 case DW_OP_breg21:
1767 case DW_OP_breg22:
1768 case DW_OP_breg23:
1769 case DW_OP_breg24:
1770 case DW_OP_breg25:
1771 case DW_OP_breg26:
1772 case DW_OP_breg27:
1773 case DW_OP_breg28:
1774 case DW_OP_breg29:
1775 case DW_OP_breg30:
1776 case DW_OP_breg31:
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);
1782 result += offset;
1783 result_val = value_from_ulongest (address_type, result);
1785 break;
1786 case DW_OP_bregx:
1788 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
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);
1793 result += offset;
1794 result_val = value_from_ulongest (address_type, result);
1796 break;
1797 case DW_OP_fbreg:
1799 const gdb_byte *datastart;
1800 size_t datalen;
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)
1819 result
1820 = read_addr_from_reg (this->m_frame, value_as_long (fetch (0)));
1821 else
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;
1833 break;
1835 case DW_OP_dup:
1836 result_val = fetch (0);
1837 in_stack_memory = fetch_in_stack_memory (0);
1838 break;
1840 case DW_OP_drop:
1841 pop ();
1842 goto no_push;
1844 case DW_OP_pick:
1845 offset = *op_ptr++;
1846 result_val = fetch (offset);
1847 in_stack_memory = fetch_in_stack_memory (offset);
1848 break;
1850 case DW_OP_swap:
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];
1859 std::swap (t1, t2);
1860 goto no_push;
1863 case DW_OP_over:
1864 result_val = fetch (1);
1865 in_stack_memory = fetch_in_stack_memory (1);
1866 break;
1868 case DW_OP_rot:
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;
1881 goto no_push;
1884 case DW_OP_deref:
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);
1892 struct type *type;
1894 pop ();
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);
1902 else
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)
1911 ULONGEST datum =
1912 extract_unsigned_integer (buf, addr_size, byte_order);
1914 buf = (gdb_byte *) alloca (type->length ());
1915 store_unsigned_integer (buf, type->length (),
1916 byte_order, datum);
1919 result_val = value_from_contents_and_address (type, buf, addr);
1920 break;
1923 case DW_OP_abs:
1924 case DW_OP_neg:
1925 case DW_OP_not:
1926 case DW_OP_plus_uconst:
1928 /* Unary operations. */
1929 result_val = fetch (0);
1930 pop ();
1932 switch (op)
1934 case DW_OP_abs:
1935 if (value_less (result_val,
1936 value_zero (value_type (result_val), not_lval)))
1937 result_val = value_neg (result_val);
1938 break;
1939 case DW_OP_neg:
1940 result_val = value_neg (result_val);
1941 break;
1942 case DW_OP_not:
1943 dwarf_require_integral (value_type (result_val));
1944 result_val = value_complement (result_val);
1945 break;
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, &reg);
1950 result += reg;
1951 result_val = value_from_ulongest (address_type, result);
1952 break;
1955 break;
1957 case DW_OP_and:
1958 case DW_OP_div:
1959 case DW_OP_minus:
1960 case DW_OP_mod:
1961 case DW_OP_mul:
1962 case DW_OP_or:
1963 case DW_OP_plus:
1964 case DW_OP_shl:
1965 case DW_OP_shr:
1966 case DW_OP_shra:
1967 case DW_OP_xor:
1968 case DW_OP_le:
1969 case DW_OP_ge:
1970 case DW_OP_eq:
1971 case DW_OP_lt:
1972 case DW_OP_gt:
1973 case DW_OP_ne:
1975 /* Binary operations. */
1976 struct value *first, *second;
1978 second = fetch (0);
1979 pop ();
1981 first = fetch (0);
1982 pop ();
1984 if (! base_types_equal_p (value_type (first), value_type (second)))
1985 error (_("Incompatible types on DWARF stack"));
1987 switch (op)
1989 case DW_OP_and:
1990 dwarf_require_integral (value_type (first));
1991 dwarf_require_integral (value_type (second));
1992 result_val = value_binop (first, second, BINOP_BITWISE_AND);
1993 break;
1994 case DW_OP_div:
1995 result_val = value_binop (first, second, BINOP_DIV);
1996 break;
1997 case DW_OP_minus:
1998 result_val = value_binop (first, second, BINOP_SUB);
1999 break;
2000 case DW_OP_mod:
2002 int cast_back = 0;
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
2007 math. */
2008 if (orig_type == address_type)
2010 struct type *utype = get_unsigned_type (arch, orig_type);
2012 cast_back = 1;
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);
2019 if (cast_back)
2020 result_val = value_cast (orig_type, result_val);
2022 break;
2023 case DW_OP_mul:
2024 result_val = value_binop (first, second, BINOP_MUL);
2025 break;
2026 case DW_OP_or:
2027 dwarf_require_integral (value_type (first));
2028 dwarf_require_integral (value_type (second));
2029 result_val = value_binop (first, second, BINOP_BITWISE_IOR);
2030 break;
2031 case DW_OP_plus:
2032 result_val = value_binop (first, second, BINOP_ADD);
2033 break;
2034 case DW_OP_shl:
2035 dwarf_require_integral (value_type (first));
2036 dwarf_require_integral (value_type (second));
2037 result_val = value_binop (first, second, BINOP_LSH);
2038 break;
2039 case DW_OP_shr:
2040 dwarf_require_integral (value_type (first));
2041 dwarf_require_integral (value_type (second));
2042 if (!value_type (first)->is_unsigned ())
2044 struct type *utype
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
2052 with. */
2053 if (value_type (result_val) != value_type (second))
2054 result_val = value_cast (value_type (second), result_val);
2055 break;
2056 case DW_OP_shra:
2057 dwarf_require_integral (value_type (first));
2058 dwarf_require_integral (value_type (second));
2059 if (value_type (first)->is_unsigned ())
2061 struct type *stype
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
2069 with. */
2070 if (value_type (result_val) != value_type (second))
2071 result_val = value_cast (value_type (second), result_val);
2072 break;
2073 case DW_OP_xor:
2074 dwarf_require_integral (value_type (first));
2075 dwarf_require_integral (value_type (second));
2076 result_val = value_binop (first, second, BINOP_BITWISE_XOR);
2077 break;
2078 case DW_OP_le:
2079 /* A <= B is !(B < A). */
2080 result = ! value_less (second, first);
2081 result_val = value_from_ulongest (address_type, result);
2082 break;
2083 case DW_OP_ge:
2084 /* A >= B is !(A < B). */
2085 result = ! value_less (first, second);
2086 result_val = value_from_ulongest (address_type, result);
2087 break;
2088 case DW_OP_eq:
2089 result = value_equal (first, second);
2090 result_val = value_from_ulongest (address_type, result);
2091 break;
2092 case DW_OP_lt:
2093 result = value_less (first, second);
2094 result_val = value_from_ulongest (address_type, result);
2095 break;
2096 case DW_OP_gt:
2097 /* A > B is B < A. */
2098 result = value_less (second, first);
2099 result_val = value_from_ulongest (address_type, result);
2100 break;
2101 case DW_OP_ne:
2102 result = ! value_equal (first, second);
2103 result_val = value_from_ulongest (address_type, result);
2104 break;
2105 default:
2106 internal_error (__FILE__, __LINE__,
2107 _("Can't be reached."));
2110 break;
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;
2118 break;
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
2129 returned. */
2130 result = value_as_long (fetch (0));
2131 pop ();
2132 result = target_translate_tls_address (this->m_per_objfile->objfile,
2133 result);
2134 result_val = value_from_ulongest (address_type, result);
2135 break;
2137 case DW_OP_skip:
2138 offset = extract_signed_integer (op_ptr, 2, byte_order);
2139 op_ptr += 2;
2140 op_ptr += offset;
2141 goto no_push;
2143 case DW_OP_bra:
2145 struct value *val;
2147 offset = extract_signed_integer (op_ptr, 2, byte_order);
2148 op_ptr += 2;
2149 val = fetch (0);
2150 dwarf_require_integral (value_type (val));
2151 if (value_as_long (val) != 0)
2152 op_ptr += offset;
2153 pop ();
2155 goto no_push;
2157 case DW_OP_nop:
2158 goto no_push;
2160 case DW_OP_piece:
2162 uint64_t size;
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
2169 type. */
2170 if (this->m_location != DWARF_VALUE_LITERAL
2171 && this->m_location != DWARF_VALUE_OPTIMIZED_OUT)
2172 pop ();
2173 this->m_location = DWARF_VALUE_MEMORY;
2175 goto no_push;
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
2187 type. */
2188 if (this->m_location != DWARF_VALUE_LITERAL
2189 && this->m_location != DWARF_VALUE_OPTIMIZED_OUT)
2190 pop ();
2191 this->m_location = DWARF_VALUE_MEMORY;
2193 goto no_push;
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;
2201 goto no_push;
2203 case DW_OP_call2:
2205 cu_offset cu_off
2206 = (cu_offset) extract_unsigned_integer (op_ptr, 2, byte_order);
2207 op_ptr += 2;
2208 this->dwarf_call (cu_off);
2210 goto no_push;
2212 case DW_OP_call4:
2214 cu_offset cu_off
2215 = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order);
2216 op_ptr += 4;
2217 this->dwarf_call (cu_off);
2219 goto no_push;
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,
2228 ref_addr_size,
2229 byte_order);
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);
2235 break;
2237 case DW_OP_entry_value:
2238 case DW_OP_GNU_entry_value:
2240 uint64_t len;
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)
2251 op_ptr += len;
2252 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG,
2253 kind_u,
2254 -1 /* deref_size */);
2255 goto no_push;
2258 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr,
2259 op_ptr + len,
2260 &deref_size);
2261 if (kind_u.dwarf_reg != -1)
2263 if (deref_size == -1)
2264 deref_size = this->m_addr_size;
2265 op_ptr += len;
2266 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG,
2267 kind_u, deref_size);
2268 goto no_push;
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;
2280 kind_u.param_cu_off
2281 = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order);
2282 op_ptr += 4;
2283 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_PARAM_OFFSET,
2284 kind_u,
2285 -1 /* deref_size */);
2287 goto no_push;
2289 case DW_OP_const_type:
2290 case DW_OP_GNU_const_type:
2292 int n;
2293 const gdb_byte *data;
2294 struct type *type;
2296 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
2297 cu_offset type_die_cu_off = (cu_offset) uoffset;
2299 n = *op_ptr++;
2300 data = op_ptr;
2301 op_ptr += n;
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);
2310 break;
2312 case DW_OP_regval_type:
2313 case DW_OP_GNU_regval_type:
2315 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
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);
2322 int regnum
2323 = dwarf_reg_to_regnum_or_error (get_frame_arch (this->m_frame),
2324 reg);
2325 result_val = value_from_register (type, regnum, this->m_frame);
2327 break;
2329 case DW_OP_convert:
2330 case DW_OP_GNU_convert:
2331 case DW_OP_reinterpret:
2332 case DW_OP_GNU_reinterpret:
2334 struct type *type;
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;
2341 else
2342 type = get_base_type (type_die_cu_off);
2344 result_val = fetch (0);
2345 pop ();
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))
2351 /* Nothing. */
2353 else if (type->length ()
2354 != value_type (result_val)->length ())
2355 error (_("DW_OP_reinterpret has wrong size"));
2356 else
2357 result_val
2358 = value_from_contents (type,
2359 value_contents_all (result_val).data ());
2361 break;
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."));
2370 result_val
2371 = value_from_ulongest (address_type, this->m_addr_info->addr);
2372 break;
2374 default:
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);
2381 no_push:
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);