17d62b00eSchristos /* DWARF 2 Expression Evaluator. 27d62b00eSchristos 3*6881a400Schristos Copyright (C) 2001-2023 Free Software Foundation, Inc. 47d62b00eSchristos 57d62b00eSchristos Contributed by Daniel Berlin (dan@dberlin.org) 67d62b00eSchristos 77d62b00eSchristos This file is part of GDB. 87d62b00eSchristos 97d62b00eSchristos This program is free software; you can redistribute it and/or modify 107d62b00eSchristos it under the terms of the GNU General Public License as published by 117d62b00eSchristos the Free Software Foundation; either version 3 of the License, or 127d62b00eSchristos (at your option) any later version. 137d62b00eSchristos 147d62b00eSchristos This program is distributed in the hope that it will be useful, 157d62b00eSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 167d62b00eSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 177d62b00eSchristos GNU General Public License for more details. 187d62b00eSchristos 197d62b00eSchristos You should have received a copy of the GNU General Public License 207d62b00eSchristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 217d62b00eSchristos 227d62b00eSchristos #include "defs.h" 23*6881a400Schristos #include "block.h" 247d62b00eSchristos #include "symtab.h" 257d62b00eSchristos #include "gdbtypes.h" 267d62b00eSchristos #include "value.h" 277d62b00eSchristos #include "gdbcore.h" 287d62b00eSchristos #include "dwarf2.h" 297d62b00eSchristos #include "dwarf2/expr.h" 307d62b00eSchristos #include "dwarf2/loc.h" 317d62b00eSchristos #include "dwarf2/read.h" 32*6881a400Schristos #include "frame.h" 337d62b00eSchristos #include "gdbsupport/underlying.h" 347d62b00eSchristos #include "gdbarch.h" 35*6881a400Schristos #include "objfiles.h" 367d62b00eSchristos 377d62b00eSchristos /* This holds gdbarch-specific types used by the DWARF expression 387d62b00eSchristos evaluator. See comments in execute_stack_op. */ 397d62b00eSchristos 407d62b00eSchristos struct dwarf_gdbarch_types 417d62b00eSchristos { 42*6881a400Schristos struct type *dw_types[3] {}; 437d62b00eSchristos }; 447d62b00eSchristos 45*6881a400Schristos /* Cookie for gdbarch data. */ 46*6881a400Schristos 47*6881a400Schristos static const registry<gdbarch>::key<dwarf_gdbarch_types> dwarf_arch_cookie; 48*6881a400Schristos 49*6881a400Schristos /* Ensure that a FRAME is defined, throw an exception otherwise. */ 50*6881a400Schristos 51*6881a400Schristos static void 52*6881a400Schristos ensure_have_frame (frame_info_ptr frame, const char *op_name) 53*6881a400Schristos { 54*6881a400Schristos if (frame == nullptr) 55*6881a400Schristos throw_error (GENERIC_ERROR, 56*6881a400Schristos _("%s evaluation requires a frame."), op_name); 57*6881a400Schristos } 58*6881a400Schristos 59*6881a400Schristos /* Ensure that a PER_CU is defined and throw an exception otherwise. */ 60*6881a400Schristos 61*6881a400Schristos static void 62*6881a400Schristos ensure_have_per_cu (dwarf2_per_cu_data *per_cu, const char* op_name) 63*6881a400Schristos { 64*6881a400Schristos if (per_cu == nullptr) 65*6881a400Schristos throw_error (GENERIC_ERROR, 66*6881a400Schristos _("%s evaluation requires a compilation unit."), op_name); 67*6881a400Schristos } 68*6881a400Schristos 69*6881a400Schristos /* Return the number of bytes overlapping a contiguous chunk of N_BITS 70*6881a400Schristos bits whose first bit is located at bit offset START. */ 71*6881a400Schristos 72*6881a400Schristos static size_t 73*6881a400Schristos bits_to_bytes (ULONGEST start, ULONGEST n_bits) 74*6881a400Schristos { 75*6881a400Schristos return (start % HOST_CHAR_BIT + n_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT; 76*6881a400Schristos } 77*6881a400Schristos 78*6881a400Schristos /* See expr.h. */ 79*6881a400Schristos 80*6881a400Schristos CORE_ADDR 81*6881a400Schristos read_addr_from_reg (frame_info_ptr frame, int reg) 82*6881a400Schristos { 83*6881a400Schristos struct gdbarch *gdbarch = get_frame_arch (frame); 84*6881a400Schristos int regnum = dwarf_reg_to_regnum_or_error (gdbarch, reg); 85*6881a400Schristos 86*6881a400Schristos return address_from_register (regnum, frame); 87*6881a400Schristos } 88*6881a400Schristos 89*6881a400Schristos struct piece_closure 90*6881a400Schristos { 91*6881a400Schristos /* Reference count. */ 92*6881a400Schristos int refc = 0; 93*6881a400Schristos 94*6881a400Schristos /* The objfile from which this closure's expression came. */ 95*6881a400Schristos dwarf2_per_objfile *per_objfile = nullptr; 96*6881a400Schristos 97*6881a400Schristos /* The CU from which this closure's expression came. */ 98*6881a400Schristos dwarf2_per_cu_data *per_cu = nullptr; 99*6881a400Schristos 100*6881a400Schristos /* The pieces describing this variable. */ 101*6881a400Schristos std::vector<dwarf_expr_piece> pieces; 102*6881a400Schristos 103*6881a400Schristos /* Frame ID of frame to which a register value is relative, used 104*6881a400Schristos only by DWARF_VALUE_REGISTER. */ 105*6881a400Schristos struct frame_id frame_id; 106*6881a400Schristos }; 107*6881a400Schristos 108*6881a400Schristos /* Allocate a closure for a value formed from separately-described 109*6881a400Schristos PIECES. */ 110*6881a400Schristos 111*6881a400Schristos static piece_closure * 112*6881a400Schristos allocate_piece_closure (dwarf2_per_cu_data *per_cu, 113*6881a400Schristos dwarf2_per_objfile *per_objfile, 114*6881a400Schristos std::vector<dwarf_expr_piece> &&pieces, 115*6881a400Schristos frame_info_ptr frame) 116*6881a400Schristos { 117*6881a400Schristos piece_closure *c = new piece_closure; 118*6881a400Schristos 119*6881a400Schristos c->refc = 1; 120*6881a400Schristos /* We must capture this here due to sharing of DWARF state. */ 121*6881a400Schristos c->per_objfile = per_objfile; 122*6881a400Schristos c->per_cu = per_cu; 123*6881a400Schristos c->pieces = std::move (pieces); 124*6881a400Schristos if (frame == nullptr) 125*6881a400Schristos c->frame_id = null_frame_id; 126*6881a400Schristos else 127*6881a400Schristos c->frame_id = get_frame_id (frame); 128*6881a400Schristos 129*6881a400Schristos for (dwarf_expr_piece &piece : c->pieces) 130*6881a400Schristos if (piece.location == DWARF_VALUE_STACK) 131*6881a400Schristos value_incref (piece.v.value); 132*6881a400Schristos 133*6881a400Schristos return c; 134*6881a400Schristos } 135*6881a400Schristos 136*6881a400Schristos /* Read or write a pieced value V. If FROM != NULL, operate in "write 137*6881a400Schristos mode": copy FROM into the pieces comprising V. If FROM == NULL, 138*6881a400Schristos operate in "read mode": fetch the contents of the (lazy) value V by 139*6881a400Schristos composing it from its pieces. If CHECK_OPTIMIZED is true, then no 140*6881a400Schristos reading or writing is done; instead the return value of this 141*6881a400Schristos function is true if any piece is optimized out. When 142*6881a400Schristos CHECK_OPTIMIZED is true, FROM must be nullptr. */ 143*6881a400Schristos 144*6881a400Schristos static bool 145*6881a400Schristos rw_pieced_value (value *v, value *from, bool check_optimized) 146*6881a400Schristos { 147*6881a400Schristos int i; 148*6881a400Schristos LONGEST offset = 0, max_offset; 149*6881a400Schristos gdb_byte *v_contents; 150*6881a400Schristos const gdb_byte *from_contents; 151*6881a400Schristos piece_closure *c 152*6881a400Schristos = (piece_closure *) value_computed_closure (v); 153*6881a400Schristos gdb::byte_vector buffer; 154*6881a400Schristos bool bits_big_endian = type_byte_order (value_type (v)) == BFD_ENDIAN_BIG; 155*6881a400Schristos 156*6881a400Schristos gdb_assert (!check_optimized || from == nullptr); 157*6881a400Schristos if (from != nullptr) 158*6881a400Schristos { 159*6881a400Schristos from_contents = value_contents (from).data (); 160*6881a400Schristos v_contents = nullptr; 161*6881a400Schristos } 162*6881a400Schristos else 163*6881a400Schristos { 164*6881a400Schristos if (value_type (v) != value_enclosing_type (v)) 165*6881a400Schristos internal_error (_("Should not be able to create a lazy value with " 166*6881a400Schristos "an enclosing type")); 167*6881a400Schristos if (check_optimized) 168*6881a400Schristos v_contents = nullptr; 169*6881a400Schristos else 170*6881a400Schristos v_contents = value_contents_raw (v).data (); 171*6881a400Schristos from_contents = nullptr; 172*6881a400Schristos } 173*6881a400Schristos 174*6881a400Schristos ULONGEST bits_to_skip = 8 * value_offset (v); 175*6881a400Schristos if (value_bitsize (v)) 176*6881a400Schristos { 177*6881a400Schristos bits_to_skip += (8 * value_offset (value_parent (v)) 178*6881a400Schristos + value_bitpos (v)); 179*6881a400Schristos if (from != nullptr 180*6881a400Schristos && (type_byte_order (value_type (from)) 181*6881a400Schristos == BFD_ENDIAN_BIG)) 182*6881a400Schristos { 183*6881a400Schristos /* Use the least significant bits of FROM. */ 184*6881a400Schristos max_offset = 8 * value_type (from)->length (); 185*6881a400Schristos offset = max_offset - value_bitsize (v); 186*6881a400Schristos } 187*6881a400Schristos else 188*6881a400Schristos max_offset = value_bitsize (v); 189*6881a400Schristos } 190*6881a400Schristos else 191*6881a400Schristos max_offset = 8 * value_type (v)->length (); 192*6881a400Schristos 193*6881a400Schristos /* Advance to the first non-skipped piece. */ 194*6881a400Schristos for (i = 0; i < c->pieces.size () && bits_to_skip >= c->pieces[i].size; i++) 195*6881a400Schristos bits_to_skip -= c->pieces[i].size; 196*6881a400Schristos 197*6881a400Schristos for (; i < c->pieces.size () && offset < max_offset; i++) 198*6881a400Schristos { 199*6881a400Schristos dwarf_expr_piece *p = &c->pieces[i]; 200*6881a400Schristos size_t this_size_bits, this_size; 201*6881a400Schristos 202*6881a400Schristos this_size_bits = p->size - bits_to_skip; 203*6881a400Schristos if (this_size_bits > max_offset - offset) 204*6881a400Schristos this_size_bits = max_offset - offset; 205*6881a400Schristos 206*6881a400Schristos switch (p->location) 207*6881a400Schristos { 208*6881a400Schristos case DWARF_VALUE_REGISTER: 209*6881a400Schristos { 210*6881a400Schristos frame_info_ptr frame = frame_find_by_id (c->frame_id); 211*6881a400Schristos gdbarch *arch = get_frame_arch (frame); 212*6881a400Schristos int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, p->v.regno); 213*6881a400Schristos ULONGEST reg_bits = 8 * register_size (arch, gdb_regnum); 214*6881a400Schristos int optim, unavail; 215*6881a400Schristos 216*6881a400Schristos if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG 217*6881a400Schristos && p->offset + p->size < reg_bits) 218*6881a400Schristos { 219*6881a400Schristos /* Big-endian, and we want less than full size. */ 220*6881a400Schristos bits_to_skip += reg_bits - (p->offset + p->size); 221*6881a400Schristos } 222*6881a400Schristos else 223*6881a400Schristos bits_to_skip += p->offset; 224*6881a400Schristos 225*6881a400Schristos this_size = bits_to_bytes (bits_to_skip, this_size_bits); 226*6881a400Schristos buffer.resize (this_size); 227*6881a400Schristos 228*6881a400Schristos if (from == nullptr) 229*6881a400Schristos { 230*6881a400Schristos /* Read mode. */ 231*6881a400Schristos if (!get_frame_register_bytes (frame, gdb_regnum, 232*6881a400Schristos bits_to_skip / 8, 233*6881a400Schristos buffer, &optim, &unavail)) 234*6881a400Schristos { 235*6881a400Schristos if (optim) 236*6881a400Schristos { 237*6881a400Schristos if (check_optimized) 238*6881a400Schristos return true; 239*6881a400Schristos mark_value_bits_optimized_out (v, offset, 240*6881a400Schristos this_size_bits); 241*6881a400Schristos } 242*6881a400Schristos if (unavail && !check_optimized) 243*6881a400Schristos mark_value_bits_unavailable (v, offset, 244*6881a400Schristos this_size_bits); 245*6881a400Schristos break; 246*6881a400Schristos } 247*6881a400Schristos 248*6881a400Schristos if (!check_optimized) 249*6881a400Schristos copy_bitwise (v_contents, offset, 250*6881a400Schristos buffer.data (), bits_to_skip % 8, 251*6881a400Schristos this_size_bits, bits_big_endian); 252*6881a400Schristos } 253*6881a400Schristos else 254*6881a400Schristos { 255*6881a400Schristos /* Write mode. */ 256*6881a400Schristos if (bits_to_skip % 8 != 0 || this_size_bits % 8 != 0) 257*6881a400Schristos { 258*6881a400Schristos /* Data is copied non-byte-aligned into the register. 259*6881a400Schristos Need some bits from original register value. */ 260*6881a400Schristos get_frame_register_bytes (frame, gdb_regnum, 261*6881a400Schristos bits_to_skip / 8, 262*6881a400Schristos buffer, &optim, &unavail); 263*6881a400Schristos if (optim) 264*6881a400Schristos throw_error (OPTIMIZED_OUT_ERROR, 265*6881a400Schristos _("Can't do read-modify-write to " 266*6881a400Schristos "update bitfield; containing word " 267*6881a400Schristos "has been optimized out")); 268*6881a400Schristos if (unavail) 269*6881a400Schristos throw_error (NOT_AVAILABLE_ERROR, 270*6881a400Schristos _("Can't do read-modify-write to " 271*6881a400Schristos "update bitfield; containing word " 272*6881a400Schristos "is unavailable")); 273*6881a400Schristos } 274*6881a400Schristos 275*6881a400Schristos copy_bitwise (buffer.data (), bits_to_skip % 8, 276*6881a400Schristos from_contents, offset, 277*6881a400Schristos this_size_bits, bits_big_endian); 278*6881a400Schristos put_frame_register_bytes (frame, gdb_regnum, 279*6881a400Schristos bits_to_skip / 8, 280*6881a400Schristos buffer); 281*6881a400Schristos } 282*6881a400Schristos } 283*6881a400Schristos break; 284*6881a400Schristos 285*6881a400Schristos case DWARF_VALUE_MEMORY: 286*6881a400Schristos { 287*6881a400Schristos if (check_optimized) 288*6881a400Schristos break; 289*6881a400Schristos 290*6881a400Schristos bits_to_skip += p->offset; 291*6881a400Schristos 292*6881a400Schristos CORE_ADDR start_addr = p->v.mem.addr + bits_to_skip / 8; 293*6881a400Schristos 294*6881a400Schristos if (bits_to_skip % 8 == 0 && this_size_bits % 8 == 0 295*6881a400Schristos && offset % 8 == 0) 296*6881a400Schristos { 297*6881a400Schristos /* Everything is byte-aligned; no buffer needed. */ 298*6881a400Schristos if (from != nullptr) 299*6881a400Schristos write_memory_with_notification (start_addr, 300*6881a400Schristos (from_contents 301*6881a400Schristos + offset / 8), 302*6881a400Schristos this_size_bits / 8); 303*6881a400Schristos else 304*6881a400Schristos read_value_memory (v, offset, 305*6881a400Schristos p->v.mem.in_stack_memory, 306*6881a400Schristos p->v.mem.addr + bits_to_skip / 8, 307*6881a400Schristos v_contents + offset / 8, 308*6881a400Schristos this_size_bits / 8); 309*6881a400Schristos break; 310*6881a400Schristos } 311*6881a400Schristos 312*6881a400Schristos this_size = bits_to_bytes (bits_to_skip, this_size_bits); 313*6881a400Schristos buffer.resize (this_size); 314*6881a400Schristos 315*6881a400Schristos if (from == nullptr) 316*6881a400Schristos { 317*6881a400Schristos /* Read mode. */ 318*6881a400Schristos read_value_memory (v, offset, 319*6881a400Schristos p->v.mem.in_stack_memory, 320*6881a400Schristos p->v.mem.addr + bits_to_skip / 8, 321*6881a400Schristos buffer.data (), this_size); 322*6881a400Schristos copy_bitwise (v_contents, offset, 323*6881a400Schristos buffer.data (), bits_to_skip % 8, 324*6881a400Schristos this_size_bits, bits_big_endian); 325*6881a400Schristos } 326*6881a400Schristos else 327*6881a400Schristos { 328*6881a400Schristos /* Write mode. */ 329*6881a400Schristos if (bits_to_skip % 8 != 0 || this_size_bits % 8 != 0) 330*6881a400Schristos { 331*6881a400Schristos if (this_size <= 8) 332*6881a400Schristos { 333*6881a400Schristos /* Perform a single read for small sizes. */ 334*6881a400Schristos read_memory (start_addr, buffer.data (), 335*6881a400Schristos this_size); 336*6881a400Schristos } 337*6881a400Schristos else 338*6881a400Schristos { 339*6881a400Schristos /* Only the first and last bytes can possibly have 340*6881a400Schristos any bits reused. */ 341*6881a400Schristos read_memory (start_addr, buffer.data (), 1); 342*6881a400Schristos read_memory (start_addr + this_size - 1, 343*6881a400Schristos &buffer[this_size - 1], 1); 344*6881a400Schristos } 345*6881a400Schristos } 346*6881a400Schristos 347*6881a400Schristos copy_bitwise (buffer.data (), bits_to_skip % 8, 348*6881a400Schristos from_contents, offset, 349*6881a400Schristos this_size_bits, bits_big_endian); 350*6881a400Schristos write_memory_with_notification (start_addr, 351*6881a400Schristos buffer.data (), 352*6881a400Schristos this_size); 353*6881a400Schristos } 354*6881a400Schristos } 355*6881a400Schristos break; 356*6881a400Schristos 357*6881a400Schristos case DWARF_VALUE_STACK: 358*6881a400Schristos { 359*6881a400Schristos if (check_optimized) 360*6881a400Schristos break; 361*6881a400Schristos 362*6881a400Schristos if (from != nullptr) 363*6881a400Schristos { 364*6881a400Schristos mark_value_bits_optimized_out (v, offset, this_size_bits); 365*6881a400Schristos break; 366*6881a400Schristos } 367*6881a400Schristos 368*6881a400Schristos gdbarch *objfile_gdbarch = c->per_objfile->objfile->arch (); 369*6881a400Schristos ULONGEST stack_value_size_bits 370*6881a400Schristos = 8 * value_type (p->v.value)->length (); 371*6881a400Schristos 372*6881a400Schristos /* Use zeroes if piece reaches beyond stack value. */ 373*6881a400Schristos if (p->offset + p->size > stack_value_size_bits) 374*6881a400Schristos break; 375*6881a400Schristos 376*6881a400Schristos /* Piece is anchored at least significant bit end. */ 377*6881a400Schristos if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG) 378*6881a400Schristos bits_to_skip += stack_value_size_bits - p->offset - p->size; 379*6881a400Schristos else 380*6881a400Schristos bits_to_skip += p->offset; 381*6881a400Schristos 382*6881a400Schristos copy_bitwise (v_contents, offset, 383*6881a400Schristos value_contents_all (p->v.value).data (), 384*6881a400Schristos bits_to_skip, 385*6881a400Schristos this_size_bits, bits_big_endian); 386*6881a400Schristos } 387*6881a400Schristos break; 388*6881a400Schristos 389*6881a400Schristos case DWARF_VALUE_LITERAL: 390*6881a400Schristos { 391*6881a400Schristos if (check_optimized) 392*6881a400Schristos break; 393*6881a400Schristos 394*6881a400Schristos if (from != nullptr) 395*6881a400Schristos { 396*6881a400Schristos mark_value_bits_optimized_out (v, offset, this_size_bits); 397*6881a400Schristos break; 398*6881a400Schristos } 399*6881a400Schristos 400*6881a400Schristos ULONGEST literal_size_bits = 8 * p->v.literal.length; 401*6881a400Schristos size_t n = this_size_bits; 402*6881a400Schristos 403*6881a400Schristos /* Cut off at the end of the implicit value. */ 404*6881a400Schristos bits_to_skip += p->offset; 405*6881a400Schristos if (bits_to_skip >= literal_size_bits) 406*6881a400Schristos break; 407*6881a400Schristos if (n > literal_size_bits - bits_to_skip) 408*6881a400Schristos n = literal_size_bits - bits_to_skip; 409*6881a400Schristos 410*6881a400Schristos copy_bitwise (v_contents, offset, 411*6881a400Schristos p->v.literal.data, bits_to_skip, 412*6881a400Schristos n, bits_big_endian); 413*6881a400Schristos } 414*6881a400Schristos break; 415*6881a400Schristos 416*6881a400Schristos case DWARF_VALUE_IMPLICIT_POINTER: 417*6881a400Schristos if (from != nullptr) 418*6881a400Schristos { 419*6881a400Schristos mark_value_bits_optimized_out (v, offset, this_size_bits); 420*6881a400Schristos break; 421*6881a400Schristos } 422*6881a400Schristos 423*6881a400Schristos /* These bits show up as zeros -- but do not cause the value to 424*6881a400Schristos be considered optimized-out. */ 425*6881a400Schristos break; 426*6881a400Schristos 427*6881a400Schristos case DWARF_VALUE_OPTIMIZED_OUT: 428*6881a400Schristos if (check_optimized) 429*6881a400Schristos return true; 430*6881a400Schristos mark_value_bits_optimized_out (v, offset, this_size_bits); 431*6881a400Schristos break; 432*6881a400Schristos 433*6881a400Schristos default: 434*6881a400Schristos internal_error (_("invalid location type")); 435*6881a400Schristos } 436*6881a400Schristos 437*6881a400Schristos offset += this_size_bits; 438*6881a400Schristos bits_to_skip = 0; 439*6881a400Schristos } 440*6881a400Schristos 441*6881a400Schristos return false; 442*6881a400Schristos } 443*6881a400Schristos 444*6881a400Schristos static void 445*6881a400Schristos read_pieced_value (value *v) 446*6881a400Schristos { 447*6881a400Schristos rw_pieced_value (v, nullptr, false); 448*6881a400Schristos } 449*6881a400Schristos 450*6881a400Schristos static void 451*6881a400Schristos write_pieced_value (value *to, value *from) 452*6881a400Schristos { 453*6881a400Schristos rw_pieced_value (to, from, false); 454*6881a400Schristos } 455*6881a400Schristos 456*6881a400Schristos static bool 457*6881a400Schristos is_optimized_out_pieced_value (value *v) 458*6881a400Schristos { 459*6881a400Schristos return rw_pieced_value (v, nullptr, true); 460*6881a400Schristos } 461*6881a400Schristos 462*6881a400Schristos /* An implementation of an lval_funcs method to see whether a value is 463*6881a400Schristos a synthetic pointer. */ 464*6881a400Schristos 465*6881a400Schristos static int 466*6881a400Schristos check_pieced_synthetic_pointer (const value *value, LONGEST bit_offset, 467*6881a400Schristos int bit_length) 468*6881a400Schristos { 469*6881a400Schristos piece_closure *c = (piece_closure *) value_computed_closure (value); 470*6881a400Schristos int i; 471*6881a400Schristos 472*6881a400Schristos bit_offset += 8 * value_offset (value); 473*6881a400Schristos if (value_bitsize (value)) 474*6881a400Schristos bit_offset += value_bitpos (value); 475*6881a400Schristos 476*6881a400Schristos for (i = 0; i < c->pieces.size () && bit_length > 0; i++) 477*6881a400Schristos { 478*6881a400Schristos dwarf_expr_piece *p = &c->pieces[i]; 479*6881a400Schristos size_t this_size_bits = p->size; 480*6881a400Schristos 481*6881a400Schristos if (bit_offset > 0) 482*6881a400Schristos { 483*6881a400Schristos if (bit_offset >= this_size_bits) 484*6881a400Schristos { 485*6881a400Schristos bit_offset -= this_size_bits; 486*6881a400Schristos continue; 487*6881a400Schristos } 488*6881a400Schristos 489*6881a400Schristos bit_length -= this_size_bits - bit_offset; 490*6881a400Schristos bit_offset = 0; 491*6881a400Schristos } 492*6881a400Schristos else 493*6881a400Schristos bit_length -= this_size_bits; 494*6881a400Schristos 495*6881a400Schristos if (p->location != DWARF_VALUE_IMPLICIT_POINTER) 496*6881a400Schristos return 0; 497*6881a400Schristos } 498*6881a400Schristos 499*6881a400Schristos return 1; 500*6881a400Schristos } 501*6881a400Schristos 502*6881a400Schristos /* An implementation of an lval_funcs method to indirect through a 503*6881a400Schristos pointer. This handles the synthetic pointer case when needed. */ 504*6881a400Schristos 505*6881a400Schristos static value * 506*6881a400Schristos indirect_pieced_value (value *value) 507*6881a400Schristos { 508*6881a400Schristos piece_closure *c 509*6881a400Schristos = (piece_closure *) value_computed_closure (value); 510*6881a400Schristos int i; 511*6881a400Schristos dwarf_expr_piece *piece = NULL; 512*6881a400Schristos 513*6881a400Schristos struct type *type = check_typedef (value_type (value)); 514*6881a400Schristos if (type->code () != TYPE_CODE_PTR) 515*6881a400Schristos return NULL; 516*6881a400Schristos 517*6881a400Schristos int bit_length = 8 * type->length (); 518*6881a400Schristos LONGEST bit_offset = 8 * value_offset (value); 519*6881a400Schristos if (value_bitsize (value)) 520*6881a400Schristos bit_offset += value_bitpos (value); 521*6881a400Schristos 522*6881a400Schristos for (i = 0; i < c->pieces.size () && bit_length > 0; i++) 523*6881a400Schristos { 524*6881a400Schristos dwarf_expr_piece *p = &c->pieces[i]; 525*6881a400Schristos size_t this_size_bits = p->size; 526*6881a400Schristos 527*6881a400Schristos if (bit_offset > 0) 528*6881a400Schristos { 529*6881a400Schristos if (bit_offset >= this_size_bits) 530*6881a400Schristos { 531*6881a400Schristos bit_offset -= this_size_bits; 532*6881a400Schristos continue; 533*6881a400Schristos } 534*6881a400Schristos 535*6881a400Schristos bit_length -= this_size_bits - bit_offset; 536*6881a400Schristos bit_offset = 0; 537*6881a400Schristos } 538*6881a400Schristos else 539*6881a400Schristos bit_length -= this_size_bits; 540*6881a400Schristos 541*6881a400Schristos if (p->location != DWARF_VALUE_IMPLICIT_POINTER) 542*6881a400Schristos return NULL; 543*6881a400Schristos 544*6881a400Schristos if (bit_length != 0) 545*6881a400Schristos error (_("Invalid use of DW_OP_implicit_pointer")); 546*6881a400Schristos 547*6881a400Schristos piece = p; 548*6881a400Schristos break; 549*6881a400Schristos } 550*6881a400Schristos 551*6881a400Schristos gdb_assert (piece != NULL && c->per_cu != nullptr); 552*6881a400Schristos frame_info_ptr frame = get_selected_frame (_("No frame selected.")); 553*6881a400Schristos 554*6881a400Schristos /* This is an offset requested by GDB, such as value subscripts. 555*6881a400Schristos However, due to how synthetic pointers are implemented, this is 556*6881a400Schristos always presented to us as a pointer type. This means we have to 557*6881a400Schristos sign-extend it manually as appropriate. Use raw 558*6881a400Schristos extract_signed_integer directly rather than value_as_address and 559*6881a400Schristos sign extend afterwards on architectures that would need it 560*6881a400Schristos (mostly everywhere except MIPS, which has signed addresses) as 561*6881a400Schristos the later would go through gdbarch_pointer_to_address and thus 562*6881a400Schristos return a CORE_ADDR with high bits set on architectures that 563*6881a400Schristos encode address spaces and other things in CORE_ADDR. */ 564*6881a400Schristos bfd_endian byte_order = gdbarch_byte_order (get_frame_arch (frame)); 565*6881a400Schristos LONGEST byte_offset 566*6881a400Schristos = extract_signed_integer (value_contents (value), byte_order); 567*6881a400Schristos byte_offset += piece->v.ptr.offset; 568*6881a400Schristos 569*6881a400Schristos return indirect_synthetic_pointer (piece->v.ptr.die_sect_off, 570*6881a400Schristos byte_offset, c->per_cu, 571*6881a400Schristos c->per_objfile, frame, type); 572*6881a400Schristos } 573*6881a400Schristos 574*6881a400Schristos /* Implementation of the coerce_ref method of lval_funcs for synthetic C++ 575*6881a400Schristos references. */ 576*6881a400Schristos 577*6881a400Schristos static value * 578*6881a400Schristos coerce_pieced_ref (const value *value) 579*6881a400Schristos { 580*6881a400Schristos struct type *type = check_typedef (value_type (value)); 581*6881a400Schristos 582*6881a400Schristos if (value_bits_synthetic_pointer (value, value_embedded_offset (value), 583*6881a400Schristos TARGET_CHAR_BIT * type->length ())) 584*6881a400Schristos { 585*6881a400Schristos const piece_closure *closure 586*6881a400Schristos = (piece_closure *) value_computed_closure (value); 587*6881a400Schristos frame_info_ptr frame 588*6881a400Schristos = get_selected_frame (_("No frame selected.")); 589*6881a400Schristos 590*6881a400Schristos /* gdb represents synthetic pointers as pieced values with a single 591*6881a400Schristos piece. */ 592*6881a400Schristos gdb_assert (closure != NULL); 593*6881a400Schristos gdb_assert (closure->pieces.size () == 1); 594*6881a400Schristos 595*6881a400Schristos return indirect_synthetic_pointer 596*6881a400Schristos (closure->pieces[0].v.ptr.die_sect_off, 597*6881a400Schristos closure->pieces[0].v.ptr.offset, 598*6881a400Schristos closure->per_cu, closure->per_objfile, frame, type); 599*6881a400Schristos } 600*6881a400Schristos else 601*6881a400Schristos { 602*6881a400Schristos /* Else: not a synthetic reference; do nothing. */ 603*6881a400Schristos return NULL; 604*6881a400Schristos } 605*6881a400Schristos } 6067d62b00eSchristos 6077d62b00eSchristos static void * 608*6881a400Schristos copy_pieced_value_closure (const value *v) 6097d62b00eSchristos { 610*6881a400Schristos piece_closure *c = (piece_closure *) value_computed_closure (v); 6117d62b00eSchristos 612*6881a400Schristos ++c->refc; 613*6881a400Schristos return c; 614*6881a400Schristos } 6157d62b00eSchristos 616*6881a400Schristos static void 617*6881a400Schristos free_pieced_value_closure (value *v) 618*6881a400Schristos { 619*6881a400Schristos piece_closure *c = (piece_closure *) value_computed_closure (v); 620*6881a400Schristos 621*6881a400Schristos --c->refc; 622*6881a400Schristos if (c->refc == 0) 623*6881a400Schristos { 624*6881a400Schristos for (dwarf_expr_piece &p : c->pieces) 625*6881a400Schristos if (p.location == DWARF_VALUE_STACK) 626*6881a400Schristos value_decref (p.v.value); 627*6881a400Schristos 628*6881a400Schristos delete c; 629*6881a400Schristos } 630*6881a400Schristos } 631*6881a400Schristos 632*6881a400Schristos /* Functions for accessing a variable described by DW_OP_piece. */ 633*6881a400Schristos static const struct lval_funcs pieced_value_funcs = { 634*6881a400Schristos read_pieced_value, 635*6881a400Schristos write_pieced_value, 636*6881a400Schristos is_optimized_out_pieced_value, 637*6881a400Schristos indirect_pieced_value, 638*6881a400Schristos coerce_pieced_ref, 639*6881a400Schristos check_pieced_synthetic_pointer, 640*6881a400Schristos copy_pieced_value_closure, 641*6881a400Schristos free_pieced_value_closure 642*6881a400Schristos }; 643*6881a400Schristos 644*6881a400Schristos /* Given context CTX, section offset SECT_OFF, and compilation unit 645*6881a400Schristos data PER_CU, execute the "variable value" operation on the DIE 646*6881a400Schristos found at SECT_OFF. */ 647*6881a400Schristos 648*6881a400Schristos static value * 649*6881a400Schristos sect_variable_value (sect_offset sect_off, 650*6881a400Schristos dwarf2_per_cu_data *per_cu, 651*6881a400Schristos dwarf2_per_objfile *per_objfile) 652*6881a400Schristos { 653*6881a400Schristos const char *var_name = nullptr; 654*6881a400Schristos struct type *die_type 655*6881a400Schristos = dwarf2_fetch_die_type_sect_off (sect_off, per_cu, per_objfile, 656*6881a400Schristos &var_name); 657*6881a400Schristos 658*6881a400Schristos if (die_type == NULL) 659*6881a400Schristos error (_("Bad DW_OP_GNU_variable_value DIE.")); 660*6881a400Schristos 661*6881a400Schristos /* Note: Things still work when the following test is removed. This 662*6881a400Schristos test and error is here to conform to the proposed specification. */ 663*6881a400Schristos if (die_type->code () != TYPE_CODE_INT 664*6881a400Schristos && die_type->code () != TYPE_CODE_ENUM 665*6881a400Schristos && die_type->code () != TYPE_CODE_RANGE 666*6881a400Schristos && die_type->code () != TYPE_CODE_PTR) 667*6881a400Schristos error (_("Type of DW_OP_GNU_variable_value DIE must be an integer or pointer.")); 668*6881a400Schristos 669*6881a400Schristos if (var_name != nullptr) 670*6881a400Schristos { 671*6881a400Schristos value *result = compute_var_value (var_name); 672*6881a400Schristos if (result != nullptr) 673*6881a400Schristos return result; 674*6881a400Schristos } 675*6881a400Schristos 676*6881a400Schristos struct type *type = lookup_pointer_type (die_type); 677*6881a400Schristos frame_info_ptr frame = get_selected_frame (_("No frame selected.")); 678*6881a400Schristos return indirect_synthetic_pointer (sect_off, 0, per_cu, per_objfile, frame, 679*6881a400Schristos type, true); 6807d62b00eSchristos } 6817d62b00eSchristos 6827d62b00eSchristos /* Return the type used for DWARF operations where the type is 6837d62b00eSchristos unspecified in the DWARF spec. Only certain sizes are 6847d62b00eSchristos supported. */ 6857d62b00eSchristos 6867d62b00eSchristos struct type * 6877d62b00eSchristos dwarf_expr_context::address_type () const 6887d62b00eSchristos { 689*6881a400Schristos gdbarch *arch = this->m_per_objfile->objfile->arch (); 690*6881a400Schristos dwarf_gdbarch_types *types = dwarf_arch_cookie.get (arch); 691*6881a400Schristos if (types == nullptr) 692*6881a400Schristos types = dwarf_arch_cookie.emplace (arch); 6937d62b00eSchristos int ndx; 6947d62b00eSchristos 695*6881a400Schristos if (this->m_addr_size == 2) 6967d62b00eSchristos ndx = 0; 697*6881a400Schristos else if (this->m_addr_size == 4) 6987d62b00eSchristos ndx = 1; 699*6881a400Schristos else if (this->m_addr_size == 8) 7007d62b00eSchristos ndx = 2; 7017d62b00eSchristos else 7027d62b00eSchristos error (_("Unsupported address size in DWARF expressions: %d bits"), 703*6881a400Schristos 8 * this->m_addr_size); 7047d62b00eSchristos 7057d62b00eSchristos if (types->dw_types[ndx] == NULL) 7067d62b00eSchristos types->dw_types[ndx] 707*6881a400Schristos = arch_integer_type (arch, 8 * this->m_addr_size, 7087d62b00eSchristos 0, "<signed DWARF address type>"); 7097d62b00eSchristos 7107d62b00eSchristos return types->dw_types[ndx]; 7117d62b00eSchristos } 7127d62b00eSchristos 7137d62b00eSchristos /* Create a new context for the expression evaluator. */ 7147d62b00eSchristos 715*6881a400Schristos dwarf_expr_context::dwarf_expr_context (dwarf2_per_objfile *per_objfile, 716*6881a400Schristos int addr_size) 717*6881a400Schristos : m_addr_size (addr_size), 718*6881a400Schristos m_per_objfile (per_objfile) 7197d62b00eSchristos { 7207d62b00eSchristos } 7217d62b00eSchristos 7227d62b00eSchristos /* Push VALUE onto the stack. */ 7237d62b00eSchristos 7247d62b00eSchristos void 7257d62b00eSchristos dwarf_expr_context::push (struct value *value, bool in_stack_memory) 7267d62b00eSchristos { 727*6881a400Schristos this->m_stack.emplace_back (value, in_stack_memory); 7287d62b00eSchristos } 7297d62b00eSchristos 7307d62b00eSchristos /* Push VALUE onto the stack. */ 7317d62b00eSchristos 7327d62b00eSchristos void 7337d62b00eSchristos dwarf_expr_context::push_address (CORE_ADDR value, bool in_stack_memory) 7347d62b00eSchristos { 7357d62b00eSchristos push (value_from_ulongest (address_type (), value), in_stack_memory); 7367d62b00eSchristos } 7377d62b00eSchristos 7387d62b00eSchristos /* Pop the top item off of the stack. */ 7397d62b00eSchristos 7407d62b00eSchristos void 7417d62b00eSchristos dwarf_expr_context::pop () 7427d62b00eSchristos { 743*6881a400Schristos if (this->m_stack.empty ()) 7447d62b00eSchristos error (_("dwarf expression stack underflow")); 7457d62b00eSchristos 746*6881a400Schristos this->m_stack.pop_back (); 7477d62b00eSchristos } 7487d62b00eSchristos 7497d62b00eSchristos /* Retrieve the N'th item on the stack. */ 7507d62b00eSchristos 7517d62b00eSchristos struct value * 7527d62b00eSchristos dwarf_expr_context::fetch (int n) 7537d62b00eSchristos { 754*6881a400Schristos if (this->m_stack.size () <= n) 7557d62b00eSchristos error (_("Asked for position %d of stack, " 7567d62b00eSchristos "stack only has %zu elements on it."), 757*6881a400Schristos n, this->m_stack.size ()); 758*6881a400Schristos return this->m_stack[this->m_stack.size () - (1 + n)].value; 759*6881a400Schristos } 760*6881a400Schristos 761*6881a400Schristos /* See expr.h. */ 762*6881a400Schristos 763*6881a400Schristos void 764*6881a400Schristos dwarf_expr_context::get_frame_base (const gdb_byte **start, 765*6881a400Schristos size_t * length) 766*6881a400Schristos { 767*6881a400Schristos ensure_have_frame (this->m_frame, "DW_OP_fbreg"); 768*6881a400Schristos 769*6881a400Schristos const block *bl = get_frame_block (this->m_frame, NULL); 770*6881a400Schristos 771*6881a400Schristos if (bl == NULL) 772*6881a400Schristos error (_("frame address is not available.")); 773*6881a400Schristos 774*6881a400Schristos /* Use block_linkage_function, which returns a real (not inlined) 775*6881a400Schristos function, instead of get_frame_function, which may return an 776*6881a400Schristos inlined function. */ 777*6881a400Schristos symbol *framefunc = block_linkage_function (bl); 778*6881a400Schristos 779*6881a400Schristos /* If we found a frame-relative symbol then it was certainly within 780*6881a400Schristos some function associated with a frame. If we can't find the frame, 781*6881a400Schristos something has gone wrong. */ 782*6881a400Schristos gdb_assert (framefunc != NULL); 783*6881a400Schristos 784*6881a400Schristos func_get_frame_base_dwarf_block (framefunc, 785*6881a400Schristos get_frame_address_in_block (this->m_frame), 786*6881a400Schristos start, length); 787*6881a400Schristos } 788*6881a400Schristos 789*6881a400Schristos /* See expr.h. */ 790*6881a400Schristos 791*6881a400Schristos struct type * 792*6881a400Schristos dwarf_expr_context::get_base_type (cu_offset die_cu_off) 793*6881a400Schristos { 794*6881a400Schristos if (this->m_per_cu == nullptr) 795*6881a400Schristos return builtin_type (this->m_per_objfile->objfile->arch ())->builtin_int; 796*6881a400Schristos 797*6881a400Schristos struct type *result = dwarf2_get_die_type (die_cu_off, this->m_per_cu, 798*6881a400Schristos this->m_per_objfile); 799*6881a400Schristos 800*6881a400Schristos if (result == nullptr) 801*6881a400Schristos error (_("Could not find type for operation")); 802*6881a400Schristos 803*6881a400Schristos return result; 804*6881a400Schristos } 805*6881a400Schristos 806*6881a400Schristos /* See expr.h. */ 807*6881a400Schristos 808*6881a400Schristos void 809*6881a400Schristos dwarf_expr_context::dwarf_call (cu_offset die_cu_off) 810*6881a400Schristos { 811*6881a400Schristos ensure_have_per_cu (this->m_per_cu, "DW_OP_call"); 812*6881a400Schristos 813*6881a400Schristos frame_info_ptr frame = this->m_frame; 814*6881a400Schristos 815*6881a400Schristos auto get_pc_from_frame = [frame] () 816*6881a400Schristos { 817*6881a400Schristos ensure_have_frame (frame, "DW_OP_call"); 818*6881a400Schristos return get_frame_address_in_block (frame); 819*6881a400Schristos }; 820*6881a400Schristos 821*6881a400Schristos dwarf2_locexpr_baton block 822*6881a400Schristos = dwarf2_fetch_die_loc_cu_off (die_cu_off, this->m_per_cu, 823*6881a400Schristos this->m_per_objfile, get_pc_from_frame); 824*6881a400Schristos 825*6881a400Schristos /* DW_OP_call_ref is currently not supported. */ 826*6881a400Schristos gdb_assert (block.per_cu == this->m_per_cu); 827*6881a400Schristos 828*6881a400Schristos this->eval (block.data, block.size); 829*6881a400Schristos } 830*6881a400Schristos 831*6881a400Schristos /* See expr.h. */ 832*6881a400Schristos 833*6881a400Schristos void 834*6881a400Schristos dwarf_expr_context::read_mem (gdb_byte *buf, CORE_ADDR addr, 835*6881a400Schristos size_t length) 836*6881a400Schristos { 837*6881a400Schristos if (length == 0) 838*6881a400Schristos return; 839*6881a400Schristos 840*6881a400Schristos /* Prefer the passed-in memory, if it exists. */ 841*6881a400Schristos if (this->m_addr_info != nullptr) 842*6881a400Schristos { 843*6881a400Schristos CORE_ADDR offset = addr - this->m_addr_info->addr; 844*6881a400Schristos 845*6881a400Schristos if (offset < this->m_addr_info->valaddr.size () 846*6881a400Schristos && offset + length <= this->m_addr_info->valaddr.size ()) 847*6881a400Schristos { 848*6881a400Schristos memcpy (buf, this->m_addr_info->valaddr.data (), length); 849*6881a400Schristos return; 850*6881a400Schristos } 851*6881a400Schristos } 852*6881a400Schristos 853*6881a400Schristos read_memory (addr, buf, length); 854*6881a400Schristos } 855*6881a400Schristos 856*6881a400Schristos /* See expr.h. */ 857*6881a400Schristos 858*6881a400Schristos void 859*6881a400Schristos dwarf_expr_context::push_dwarf_reg_entry_value (call_site_parameter_kind kind, 860*6881a400Schristos call_site_parameter_u kind_u, 861*6881a400Schristos int deref_size) 862*6881a400Schristos { 863*6881a400Schristos ensure_have_per_cu (this->m_per_cu, "DW_OP_entry_value"); 864*6881a400Schristos ensure_have_frame (this->m_frame, "DW_OP_entry_value"); 865*6881a400Schristos 866*6881a400Schristos dwarf2_per_cu_data *caller_per_cu; 867*6881a400Schristos dwarf2_per_objfile *caller_per_objfile; 868*6881a400Schristos frame_info_ptr caller_frame = get_prev_frame (this->m_frame); 869*6881a400Schristos call_site_parameter *parameter 870*6881a400Schristos = dwarf_expr_reg_to_entry_parameter (this->m_frame, kind, kind_u, 871*6881a400Schristos &caller_per_cu, 872*6881a400Schristos &caller_per_objfile); 873*6881a400Schristos const gdb_byte *data_src 874*6881a400Schristos = deref_size == -1 ? parameter->value : parameter->data_value; 875*6881a400Schristos size_t size 876*6881a400Schristos = deref_size == -1 ? parameter->value_size : parameter->data_value_size; 877*6881a400Schristos 878*6881a400Schristos /* DEREF_SIZE size is not verified here. */ 879*6881a400Schristos if (data_src == nullptr) 880*6881a400Schristos throw_error (NO_ENTRY_VALUE_ERROR, 881*6881a400Schristos _("Cannot resolve DW_AT_call_data_value")); 882*6881a400Schristos 883*6881a400Schristos /* We are about to evaluate an expression in the context of the caller 884*6881a400Schristos of the current frame. This evaluation context may be different from 885*6881a400Schristos the current (callee's) context), so temporarily set the caller's context. 886*6881a400Schristos 887*6881a400Schristos It is possible for the caller to be from a different objfile from the 888*6881a400Schristos callee if the call is made through a function pointer. */ 889*6881a400Schristos scoped_restore save_frame = make_scoped_restore (&this->m_frame, 890*6881a400Schristos caller_frame); 891*6881a400Schristos scoped_restore save_per_cu = make_scoped_restore (&this->m_per_cu, 892*6881a400Schristos caller_per_cu); 893*6881a400Schristos scoped_restore save_addr_info = make_scoped_restore (&this->m_addr_info, 894*6881a400Schristos nullptr); 895*6881a400Schristos scoped_restore save_per_objfile = make_scoped_restore (&this->m_per_objfile, 896*6881a400Schristos caller_per_objfile); 897*6881a400Schristos 898*6881a400Schristos scoped_restore save_addr_size = make_scoped_restore (&this->m_addr_size); 899*6881a400Schristos this->m_addr_size = this->m_per_cu->addr_size (); 900*6881a400Schristos 901*6881a400Schristos this->eval (data_src, size); 902*6881a400Schristos } 903*6881a400Schristos 904*6881a400Schristos /* See expr.h. */ 905*6881a400Schristos 906*6881a400Schristos value * 907*6881a400Schristos dwarf_expr_context::fetch_result (struct type *type, struct type *subobj_type, 908*6881a400Schristos LONGEST subobj_offset, bool as_lval) 909*6881a400Schristos { 910*6881a400Schristos value *retval = nullptr; 911*6881a400Schristos gdbarch *arch = this->m_per_objfile->objfile->arch (); 912*6881a400Schristos 913*6881a400Schristos if (type == nullptr) 914*6881a400Schristos type = address_type (); 915*6881a400Schristos 916*6881a400Schristos if (subobj_type == nullptr) 917*6881a400Schristos subobj_type = type; 918*6881a400Schristos 919*6881a400Schristos /* Ensure that, if TYPE or SUBOBJ_TYPE are typedefs, their length is filled 920*6881a400Schristos in instead of being zero. */ 921*6881a400Schristos check_typedef (type); 922*6881a400Schristos check_typedef (subobj_type); 923*6881a400Schristos 924*6881a400Schristos if (this->m_pieces.size () > 0) 925*6881a400Schristos { 926*6881a400Schristos ULONGEST bit_size = 0; 927*6881a400Schristos 928*6881a400Schristos for (dwarf_expr_piece &piece : this->m_pieces) 929*6881a400Schristos bit_size += piece.size; 930*6881a400Schristos /* Complain if the expression is larger than the size of the 931*6881a400Schristos outer type. */ 932*6881a400Schristos if (bit_size > 8 * type->length ()) 933*6881a400Schristos invalid_synthetic_pointer (); 934*6881a400Schristos 935*6881a400Schristos piece_closure *c 936*6881a400Schristos = allocate_piece_closure (this->m_per_cu, this->m_per_objfile, 937*6881a400Schristos std::move (this->m_pieces), this->m_frame); 938*6881a400Schristos retval = allocate_computed_value (subobj_type, 939*6881a400Schristos &pieced_value_funcs, c); 940*6881a400Schristos set_value_offset (retval, subobj_offset); 941*6881a400Schristos } 942*6881a400Schristos else 943*6881a400Schristos { 944*6881a400Schristos /* If AS_LVAL is false, means that the implicit conversion 945*6881a400Schristos from a location description to value is expected. */ 946*6881a400Schristos if (!as_lval) 947*6881a400Schristos this->m_location = DWARF_VALUE_STACK; 948*6881a400Schristos 949*6881a400Schristos switch (this->m_location) 950*6881a400Schristos { 951*6881a400Schristos case DWARF_VALUE_REGISTER: 952*6881a400Schristos { 953*6881a400Schristos gdbarch *f_arch = get_frame_arch (this->m_frame); 954*6881a400Schristos int dwarf_regnum 955*6881a400Schristos = longest_to_int (value_as_long (this->fetch (0))); 956*6881a400Schristos int gdb_regnum = dwarf_reg_to_regnum_or_error (f_arch, 957*6881a400Schristos dwarf_regnum); 958*6881a400Schristos 959*6881a400Schristos if (subobj_offset != 0) 960*6881a400Schristos error (_("cannot use offset on synthetic pointer to register")); 961*6881a400Schristos 962*6881a400Schristos gdb_assert (this->m_frame != NULL); 963*6881a400Schristos 964*6881a400Schristos retval = value_from_register (subobj_type, gdb_regnum, 965*6881a400Schristos this->m_frame); 966*6881a400Schristos if (value_optimized_out (retval)) 967*6881a400Schristos { 968*6881a400Schristos /* This means the register has undefined value / was 969*6881a400Schristos not saved. As we're computing the location of some 970*6881a400Schristos variable etc. in the program, not a value for 971*6881a400Schristos inspecting a register ($pc, $sp, etc.), return a 972*6881a400Schristos generic optimized out value instead, so that we show 973*6881a400Schristos <optimized out> instead of <not saved>. */ 974*6881a400Schristos value *tmp = allocate_value (subobj_type); 975*6881a400Schristos value_contents_copy (tmp, 0, retval, 0, 976*6881a400Schristos subobj_type->length ()); 977*6881a400Schristos retval = tmp; 978*6881a400Schristos } 979*6881a400Schristos } 980*6881a400Schristos break; 981*6881a400Schristos 982*6881a400Schristos case DWARF_VALUE_MEMORY: 983*6881a400Schristos { 984*6881a400Schristos struct type *ptr_type; 985*6881a400Schristos CORE_ADDR address = this->fetch_address (0); 986*6881a400Schristos bool in_stack_memory = this->fetch_in_stack_memory (0); 987*6881a400Schristos 988*6881a400Schristos /* DW_OP_deref_size (and possibly other operations too) may 989*6881a400Schristos create a pointer instead of an address. Ideally, the 990*6881a400Schristos pointer to address conversion would be performed as part 991*6881a400Schristos of those operations, but the type of the object to 992*6881a400Schristos which the address refers is not known at the time of 993*6881a400Schristos the operation. Therefore, we do the conversion here 994*6881a400Schristos since the type is readily available. */ 995*6881a400Schristos 996*6881a400Schristos switch (subobj_type->code ()) 997*6881a400Schristos { 998*6881a400Schristos case TYPE_CODE_FUNC: 999*6881a400Schristos case TYPE_CODE_METHOD: 1000*6881a400Schristos ptr_type = builtin_type (arch)->builtin_func_ptr; 1001*6881a400Schristos break; 1002*6881a400Schristos default: 1003*6881a400Schristos ptr_type = builtin_type (arch)->builtin_data_ptr; 1004*6881a400Schristos break; 1005*6881a400Schristos } 1006*6881a400Schristos address = value_as_address (value_from_pointer (ptr_type, address)); 1007*6881a400Schristos 1008*6881a400Schristos retval = value_at_lazy (subobj_type, 1009*6881a400Schristos address + subobj_offset); 1010*6881a400Schristos if (in_stack_memory) 1011*6881a400Schristos set_value_stack (retval, 1); 1012*6881a400Schristos } 1013*6881a400Schristos break; 1014*6881a400Schristos 1015*6881a400Schristos case DWARF_VALUE_STACK: 1016*6881a400Schristos { 1017*6881a400Schristos value *val = this->fetch (0); 1018*6881a400Schristos size_t n = value_type (val)->length (); 1019*6881a400Schristos size_t len = subobj_type->length (); 1020*6881a400Schristos size_t max = type->length (); 1021*6881a400Schristos 1022*6881a400Schristos if (subobj_offset + len > max) 1023*6881a400Schristos invalid_synthetic_pointer (); 1024*6881a400Schristos 1025*6881a400Schristos retval = allocate_value (subobj_type); 1026*6881a400Schristos 1027*6881a400Schristos /* The given offset is relative to the actual object. */ 1028*6881a400Schristos if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG) 1029*6881a400Schristos subobj_offset += n - max; 1030*6881a400Schristos 1031*6881a400Schristos copy (value_contents_all (val).slice (subobj_offset, len), 1032*6881a400Schristos value_contents_raw (retval)); 1033*6881a400Schristos } 1034*6881a400Schristos break; 1035*6881a400Schristos 1036*6881a400Schristos case DWARF_VALUE_LITERAL: 1037*6881a400Schristos { 1038*6881a400Schristos size_t n = subobj_type->length (); 1039*6881a400Schristos 1040*6881a400Schristos if (subobj_offset + n > this->m_len) 1041*6881a400Schristos invalid_synthetic_pointer (); 1042*6881a400Schristos 1043*6881a400Schristos retval = allocate_value (subobj_type); 1044*6881a400Schristos bfd_byte *contents = value_contents_raw (retval).data (); 1045*6881a400Schristos memcpy (contents, this->m_data + subobj_offset, n); 1046*6881a400Schristos } 1047*6881a400Schristos break; 1048*6881a400Schristos 1049*6881a400Schristos case DWARF_VALUE_OPTIMIZED_OUT: 1050*6881a400Schristos retval = allocate_optimized_out_value (subobj_type); 1051*6881a400Schristos break; 1052*6881a400Schristos 1053*6881a400Schristos /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced 1054*6881a400Schristos operation by execute_stack_op. */ 1055*6881a400Schristos case DWARF_VALUE_IMPLICIT_POINTER: 1056*6881a400Schristos /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context -- 1057*6881a400Schristos it can only be encountered when making a piece. */ 1058*6881a400Schristos default: 1059*6881a400Schristos internal_error (_("invalid location type")); 1060*6881a400Schristos } 1061*6881a400Schristos } 1062*6881a400Schristos 1063*6881a400Schristos set_value_initialized (retval, this->m_initialized); 1064*6881a400Schristos 1065*6881a400Schristos return retval; 1066*6881a400Schristos } 1067*6881a400Schristos 1068*6881a400Schristos /* See expr.h. */ 1069*6881a400Schristos 1070*6881a400Schristos value * 1071*6881a400Schristos dwarf_expr_context::evaluate (const gdb_byte *addr, size_t len, bool as_lval, 1072*6881a400Schristos dwarf2_per_cu_data *per_cu, frame_info_ptr frame, 1073*6881a400Schristos const struct property_addr_info *addr_info, 1074*6881a400Schristos struct type *type, struct type *subobj_type, 1075*6881a400Schristos LONGEST subobj_offset) 1076*6881a400Schristos { 1077*6881a400Schristos this->m_per_cu = per_cu; 1078*6881a400Schristos this->m_frame = frame; 1079*6881a400Schristos this->m_addr_info = addr_info; 1080*6881a400Schristos 1081*6881a400Schristos eval (addr, len); 1082*6881a400Schristos return fetch_result (type, subobj_type, subobj_offset, as_lval); 10837d62b00eSchristos } 10847d62b00eSchristos 10857d62b00eSchristos /* Require that TYPE be an integral type; throw an exception if not. */ 10867d62b00eSchristos 10877d62b00eSchristos static void 10887d62b00eSchristos dwarf_require_integral (struct type *type) 10897d62b00eSchristos { 10907d62b00eSchristos if (type->code () != TYPE_CODE_INT 10917d62b00eSchristos && type->code () != TYPE_CODE_CHAR 10927d62b00eSchristos && type->code () != TYPE_CODE_BOOL) 10937d62b00eSchristos error (_("integral type expected in DWARF expression")); 10947d62b00eSchristos } 10957d62b00eSchristos 10967d62b00eSchristos /* Return the unsigned form of TYPE. TYPE is necessarily an integral 10977d62b00eSchristos type. */ 10987d62b00eSchristos 10997d62b00eSchristos static struct type * 11007d62b00eSchristos get_unsigned_type (struct gdbarch *gdbarch, struct type *type) 11017d62b00eSchristos { 1102*6881a400Schristos switch (type->length ()) 11037d62b00eSchristos { 11047d62b00eSchristos case 1: 11057d62b00eSchristos return builtin_type (gdbarch)->builtin_uint8; 11067d62b00eSchristos case 2: 11077d62b00eSchristos return builtin_type (gdbarch)->builtin_uint16; 11087d62b00eSchristos case 4: 11097d62b00eSchristos return builtin_type (gdbarch)->builtin_uint32; 11107d62b00eSchristos case 8: 11117d62b00eSchristos return builtin_type (gdbarch)->builtin_uint64; 11127d62b00eSchristos default: 11137d62b00eSchristos error (_("no unsigned variant found for type, while evaluating " 11147d62b00eSchristos "DWARF expression")); 11157d62b00eSchristos } 11167d62b00eSchristos } 11177d62b00eSchristos 11187d62b00eSchristos /* Return the signed form of TYPE. TYPE is necessarily an integral 11197d62b00eSchristos type. */ 11207d62b00eSchristos 11217d62b00eSchristos static struct type * 11227d62b00eSchristos get_signed_type (struct gdbarch *gdbarch, struct type *type) 11237d62b00eSchristos { 1124*6881a400Schristos switch (type->length ()) 11257d62b00eSchristos { 11267d62b00eSchristos case 1: 11277d62b00eSchristos return builtin_type (gdbarch)->builtin_int8; 11287d62b00eSchristos case 2: 11297d62b00eSchristos return builtin_type (gdbarch)->builtin_int16; 11307d62b00eSchristos case 4: 11317d62b00eSchristos return builtin_type (gdbarch)->builtin_int32; 11327d62b00eSchristos case 8: 11337d62b00eSchristos return builtin_type (gdbarch)->builtin_int64; 11347d62b00eSchristos default: 11357d62b00eSchristos error (_("no signed variant found for type, while evaluating " 11367d62b00eSchristos "DWARF expression")); 11377d62b00eSchristos } 11387d62b00eSchristos } 11397d62b00eSchristos 11407d62b00eSchristos /* Retrieve the N'th item on the stack, converted to an address. */ 11417d62b00eSchristos 11427d62b00eSchristos CORE_ADDR 11437d62b00eSchristos dwarf_expr_context::fetch_address (int n) 11447d62b00eSchristos { 1145*6881a400Schristos gdbarch *arch = this->m_per_objfile->objfile->arch (); 1146*6881a400Schristos value *result_val = fetch (n); 1147*6881a400Schristos bfd_endian byte_order = gdbarch_byte_order (arch); 11487d62b00eSchristos ULONGEST result; 11497d62b00eSchristos 11507d62b00eSchristos dwarf_require_integral (value_type (result_val)); 1151*6881a400Schristos result = extract_unsigned_integer (value_contents (result_val), byte_order); 11527d62b00eSchristos 11537d62b00eSchristos /* For most architectures, calling extract_unsigned_integer() alone 11547d62b00eSchristos is sufficient for extracting an address. However, some 11557d62b00eSchristos architectures (e.g. MIPS) use signed addresses and using 11567d62b00eSchristos extract_unsigned_integer() will not produce a correct 11577d62b00eSchristos result. Make sure we invoke gdbarch_integer_to_address() 11587d62b00eSchristos for those architectures which require it. */ 1159*6881a400Schristos if (gdbarch_integer_to_address_p (arch)) 11607d62b00eSchristos { 1161*6881a400Schristos gdb_byte *buf = (gdb_byte *) alloca (this->m_addr_size); 1162*6881a400Schristos type *int_type = get_unsigned_type (arch, 11637d62b00eSchristos value_type (result_val)); 11647d62b00eSchristos 1165*6881a400Schristos store_unsigned_integer (buf, this->m_addr_size, byte_order, result); 1166*6881a400Schristos return gdbarch_integer_to_address (arch, int_type, buf); 11677d62b00eSchristos } 11687d62b00eSchristos 11697d62b00eSchristos return (CORE_ADDR) result; 11707d62b00eSchristos } 11717d62b00eSchristos 11727d62b00eSchristos /* Retrieve the in_stack_memory flag of the N'th item on the stack. */ 11737d62b00eSchristos 11747d62b00eSchristos bool 11757d62b00eSchristos dwarf_expr_context::fetch_in_stack_memory (int n) 11767d62b00eSchristos { 1177*6881a400Schristos if (this->m_stack.size () <= n) 11787d62b00eSchristos error (_("Asked for position %d of stack, " 11797d62b00eSchristos "stack only has %zu elements on it."), 1180*6881a400Schristos n, this->m_stack.size ()); 1181*6881a400Schristos return this->m_stack[this->m_stack.size () - (1 + n)].in_stack_memory; 11827d62b00eSchristos } 11837d62b00eSchristos 11847d62b00eSchristos /* Return true if the expression stack is empty. */ 11857d62b00eSchristos 11867d62b00eSchristos bool 11877d62b00eSchristos dwarf_expr_context::stack_empty_p () const 11887d62b00eSchristos { 1189*6881a400Schristos return m_stack.empty (); 11907d62b00eSchristos } 11917d62b00eSchristos 11927d62b00eSchristos /* Add a new piece to the dwarf_expr_context's piece list. */ 11937d62b00eSchristos void 11947d62b00eSchristos dwarf_expr_context::add_piece (ULONGEST size, ULONGEST offset) 11957d62b00eSchristos { 1196*6881a400Schristos this->m_pieces.emplace_back (); 1197*6881a400Schristos dwarf_expr_piece &p = this->m_pieces.back (); 11987d62b00eSchristos 1199*6881a400Schristos p.location = this->m_location; 12007d62b00eSchristos p.size = size; 12017d62b00eSchristos p.offset = offset; 12027d62b00eSchristos 12037d62b00eSchristos if (p.location == DWARF_VALUE_LITERAL) 12047d62b00eSchristos { 1205*6881a400Schristos p.v.literal.data = this->m_data; 1206*6881a400Schristos p.v.literal.length = this->m_len; 12077d62b00eSchristos } 12087d62b00eSchristos else if (stack_empty_p ()) 12097d62b00eSchristos { 12107d62b00eSchristos p.location = DWARF_VALUE_OPTIMIZED_OUT; 12117d62b00eSchristos /* Also reset the context's location, for our callers. This is 12127d62b00eSchristos a somewhat strange approach, but this lets us avoid setting 12137d62b00eSchristos the location to DWARF_VALUE_MEMORY in all the individual 12147d62b00eSchristos cases in the evaluator. */ 1215*6881a400Schristos this->m_location = DWARF_VALUE_OPTIMIZED_OUT; 12167d62b00eSchristos } 12177d62b00eSchristos else if (p.location == DWARF_VALUE_MEMORY) 12187d62b00eSchristos { 12197d62b00eSchristos p.v.mem.addr = fetch_address (0); 12207d62b00eSchristos p.v.mem.in_stack_memory = fetch_in_stack_memory (0); 12217d62b00eSchristos } 12227d62b00eSchristos else if (p.location == DWARF_VALUE_IMPLICIT_POINTER) 12237d62b00eSchristos { 1224*6881a400Schristos p.v.ptr.die_sect_off = (sect_offset) this->m_len; 12257d62b00eSchristos p.v.ptr.offset = value_as_long (fetch (0)); 12267d62b00eSchristos } 12277d62b00eSchristos else if (p.location == DWARF_VALUE_REGISTER) 12287d62b00eSchristos p.v.regno = value_as_long (fetch (0)); 12297d62b00eSchristos else 12307d62b00eSchristos { 12317d62b00eSchristos p.v.value = fetch (0); 12327d62b00eSchristos } 12337d62b00eSchristos } 12347d62b00eSchristos 12357d62b00eSchristos /* Evaluate the expression at ADDR (LEN bytes long). */ 12367d62b00eSchristos 12377d62b00eSchristos void 12387d62b00eSchristos dwarf_expr_context::eval (const gdb_byte *addr, size_t len) 12397d62b00eSchristos { 1240*6881a400Schristos int old_recursion_depth = this->m_recursion_depth; 12417d62b00eSchristos 12427d62b00eSchristos execute_stack_op (addr, addr + len); 12437d62b00eSchristos 12447d62b00eSchristos /* RECURSION_DEPTH becomes invalid if an exception was thrown here. */ 12457d62b00eSchristos 1246*6881a400Schristos gdb_assert (this->m_recursion_depth == old_recursion_depth); 12477d62b00eSchristos } 12487d62b00eSchristos 12497d62b00eSchristos /* Helper to read a uleb128 value or throw an error. */ 12507d62b00eSchristos 12517d62b00eSchristos const gdb_byte * 12527d62b00eSchristos safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end, 12537d62b00eSchristos uint64_t *r) 12547d62b00eSchristos { 12557d62b00eSchristos buf = gdb_read_uleb128 (buf, buf_end, r); 12567d62b00eSchristos if (buf == NULL) 12577d62b00eSchristos error (_("DWARF expression error: ran off end of buffer reading uleb128 value")); 12587d62b00eSchristos return buf; 12597d62b00eSchristos } 12607d62b00eSchristos 12617d62b00eSchristos /* Helper to read a sleb128 value or throw an error. */ 12627d62b00eSchristos 12637d62b00eSchristos const gdb_byte * 12647d62b00eSchristos safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end, 12657d62b00eSchristos int64_t *r) 12667d62b00eSchristos { 12677d62b00eSchristos buf = gdb_read_sleb128 (buf, buf_end, r); 12687d62b00eSchristos if (buf == NULL) 12697d62b00eSchristos error (_("DWARF expression error: ran off end of buffer reading sleb128 value")); 12707d62b00eSchristos return buf; 12717d62b00eSchristos } 12727d62b00eSchristos 12737d62b00eSchristos const gdb_byte * 12747d62b00eSchristos safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end) 12757d62b00eSchristos { 12767d62b00eSchristos buf = gdb_skip_leb128 (buf, buf_end); 12777d62b00eSchristos if (buf == NULL) 12787d62b00eSchristos error (_("DWARF expression error: ran off end of buffer reading leb128 value")); 12797d62b00eSchristos return buf; 12807d62b00eSchristos } 12817d62b00eSchristos 12827d62b00eSchristos 12837d62b00eSchristos /* Check that the current operator is either at the end of an 12847d62b00eSchristos expression, or that it is followed by a composition operator or by 12857d62b00eSchristos DW_OP_GNU_uninit (which should terminate the expression). */ 12867d62b00eSchristos 12877d62b00eSchristos void 12887d62b00eSchristos dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end, 12897d62b00eSchristos const char *op_name) 12907d62b00eSchristos { 12917d62b00eSchristos if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece 12927d62b00eSchristos && *op_ptr != DW_OP_GNU_uninit) 12937d62b00eSchristos error (_("DWARF-2 expression error: `%s' operations must be " 12947d62b00eSchristos "used either alone or in conjunction with DW_OP_piece " 12957d62b00eSchristos "or DW_OP_bit_piece."), 12967d62b00eSchristos op_name); 12977d62b00eSchristos } 12987d62b00eSchristos 12997d62b00eSchristos /* Return true iff the types T1 and T2 are "the same". This only does 13007d62b00eSchristos checks that might reasonably be needed to compare DWARF base 13017d62b00eSchristos types. */ 13027d62b00eSchristos 13037d62b00eSchristos static int 13047d62b00eSchristos base_types_equal_p (struct type *t1, struct type *t2) 13057d62b00eSchristos { 13067d62b00eSchristos if (t1->code () != t2->code ()) 13077d62b00eSchristos return 0; 1308*6881a400Schristos if (t1->is_unsigned () != t2->is_unsigned ()) 13097d62b00eSchristos return 0; 1310*6881a400Schristos return t1->length () == t2->length (); 13117d62b00eSchristos } 13127d62b00eSchristos 13137d62b00eSchristos /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the 13147d62b00eSchristos DWARF register number. Otherwise return -1. */ 13157d62b00eSchristos 13167d62b00eSchristos int 13177d62b00eSchristos dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end) 13187d62b00eSchristos { 13197d62b00eSchristos uint64_t dwarf_reg; 13207d62b00eSchristos 13217d62b00eSchristos if (buf_end <= buf) 13227d62b00eSchristos return -1; 13237d62b00eSchristos if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31) 13247d62b00eSchristos { 13257d62b00eSchristos if (buf_end - buf != 1) 13267d62b00eSchristos return -1; 13277d62b00eSchristos return *buf - DW_OP_reg0; 13287d62b00eSchristos } 13297d62b00eSchristos 13307d62b00eSchristos if (*buf == DW_OP_regval_type || *buf == DW_OP_GNU_regval_type) 13317d62b00eSchristos { 13327d62b00eSchristos buf++; 13337d62b00eSchristos buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg); 13347d62b00eSchristos if (buf == NULL) 13357d62b00eSchristos return -1; 13367d62b00eSchristos buf = gdb_skip_leb128 (buf, buf_end); 13377d62b00eSchristos if (buf == NULL) 13387d62b00eSchristos return -1; 13397d62b00eSchristos } 13407d62b00eSchristos else if (*buf == DW_OP_regx) 13417d62b00eSchristos { 13427d62b00eSchristos buf++; 13437d62b00eSchristos buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg); 13447d62b00eSchristos if (buf == NULL) 13457d62b00eSchristos return -1; 13467d62b00eSchristos } 13477d62b00eSchristos else 13487d62b00eSchristos return -1; 13497d62b00eSchristos if (buf != buf_end || (int) dwarf_reg != dwarf_reg) 13507d62b00eSchristos return -1; 13517d62b00eSchristos return dwarf_reg; 13527d62b00eSchristos } 13537d62b00eSchristos 13547d62b00eSchristos /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and 13557d62b00eSchristos DW_OP_deref* return the DWARF register number. Otherwise return -1. 13567d62b00eSchristos DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the 13577d62b00eSchristos size from DW_OP_deref_size. */ 13587d62b00eSchristos 13597d62b00eSchristos int 13607d62b00eSchristos dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end, 13617d62b00eSchristos CORE_ADDR *deref_size_return) 13627d62b00eSchristos { 13637d62b00eSchristos uint64_t dwarf_reg; 13647d62b00eSchristos int64_t offset; 13657d62b00eSchristos 13667d62b00eSchristos if (buf_end <= buf) 13677d62b00eSchristos return -1; 13687d62b00eSchristos 13697d62b00eSchristos if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31) 13707d62b00eSchristos { 13717d62b00eSchristos dwarf_reg = *buf - DW_OP_breg0; 13727d62b00eSchristos buf++; 13737d62b00eSchristos if (buf >= buf_end) 13747d62b00eSchristos return -1; 13757d62b00eSchristos } 13767d62b00eSchristos else if (*buf == DW_OP_bregx) 13777d62b00eSchristos { 13787d62b00eSchristos buf++; 13797d62b00eSchristos buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg); 13807d62b00eSchristos if (buf == NULL) 13817d62b00eSchristos return -1; 13827d62b00eSchristos if ((int) dwarf_reg != dwarf_reg) 13837d62b00eSchristos return -1; 13847d62b00eSchristos } 13857d62b00eSchristos else 13867d62b00eSchristos return -1; 13877d62b00eSchristos 13887d62b00eSchristos buf = gdb_read_sleb128 (buf, buf_end, &offset); 13897d62b00eSchristos if (buf == NULL) 13907d62b00eSchristos return -1; 13917d62b00eSchristos if (offset != 0) 13927d62b00eSchristos return -1; 13937d62b00eSchristos 13947d62b00eSchristos if (*buf == DW_OP_deref) 13957d62b00eSchristos { 13967d62b00eSchristos buf++; 13977d62b00eSchristos *deref_size_return = -1; 13987d62b00eSchristos } 13997d62b00eSchristos else if (*buf == DW_OP_deref_size) 14007d62b00eSchristos { 14017d62b00eSchristos buf++; 14027d62b00eSchristos if (buf >= buf_end) 14037d62b00eSchristos return -1; 14047d62b00eSchristos *deref_size_return = *buf++; 14057d62b00eSchristos } 14067d62b00eSchristos else 14077d62b00eSchristos return -1; 14087d62b00eSchristos 14097d62b00eSchristos if (buf != buf_end) 14107d62b00eSchristos return -1; 14117d62b00eSchristos 14127d62b00eSchristos return dwarf_reg; 14137d62b00eSchristos } 14147d62b00eSchristos 14157d62b00eSchristos /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill 14167d62b00eSchristos in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */ 14177d62b00eSchristos 14187d62b00eSchristos int 14197d62b00eSchristos dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end, 14207d62b00eSchristos CORE_ADDR *fb_offset_return) 14217d62b00eSchristos { 14227d62b00eSchristos int64_t fb_offset; 14237d62b00eSchristos 14247d62b00eSchristos if (buf_end <= buf) 14257d62b00eSchristos return 0; 14267d62b00eSchristos 14277d62b00eSchristos if (*buf != DW_OP_fbreg) 14287d62b00eSchristos return 0; 14297d62b00eSchristos buf++; 14307d62b00eSchristos 14317d62b00eSchristos buf = gdb_read_sleb128 (buf, buf_end, &fb_offset); 14327d62b00eSchristos if (buf == NULL) 14337d62b00eSchristos return 0; 14347d62b00eSchristos *fb_offset_return = fb_offset; 14357d62b00eSchristos if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return) 14367d62b00eSchristos return 0; 14377d62b00eSchristos 14387d62b00eSchristos return 1; 14397d62b00eSchristos } 14407d62b00eSchristos 14417d62b00eSchristos /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill 14427d62b00eSchristos in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. 14437d62b00eSchristos The matched SP register number depends on GDBARCH. */ 14447d62b00eSchristos 14457d62b00eSchristos int 14467d62b00eSchristos dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf, 14477d62b00eSchristos const gdb_byte *buf_end, CORE_ADDR *sp_offset_return) 14487d62b00eSchristos { 14497d62b00eSchristos uint64_t dwarf_reg; 14507d62b00eSchristos int64_t sp_offset; 14517d62b00eSchristos 14527d62b00eSchristos if (buf_end <= buf) 14537d62b00eSchristos return 0; 14547d62b00eSchristos if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31) 14557d62b00eSchristos { 14567d62b00eSchristos dwarf_reg = *buf - DW_OP_breg0; 14577d62b00eSchristos buf++; 14587d62b00eSchristos } 14597d62b00eSchristos else 14607d62b00eSchristos { 14617d62b00eSchristos if (*buf != DW_OP_bregx) 14627d62b00eSchristos return 0; 14637d62b00eSchristos buf++; 14647d62b00eSchristos buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg); 14657d62b00eSchristos if (buf == NULL) 14667d62b00eSchristos return 0; 14677d62b00eSchristos } 14687d62b00eSchristos 14697d62b00eSchristos if (dwarf_reg_to_regnum (gdbarch, dwarf_reg) 14707d62b00eSchristos != gdbarch_sp_regnum (gdbarch)) 14717d62b00eSchristos return 0; 14727d62b00eSchristos 14737d62b00eSchristos buf = gdb_read_sleb128 (buf, buf_end, &sp_offset); 14747d62b00eSchristos if (buf == NULL) 14757d62b00eSchristos return 0; 14767d62b00eSchristos *sp_offset_return = sp_offset; 14777d62b00eSchristos if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return) 14787d62b00eSchristos return 0; 14797d62b00eSchristos 14807d62b00eSchristos return 1; 14817d62b00eSchristos } 14827d62b00eSchristos 14837d62b00eSchristos /* The engine for the expression evaluator. Using the context in this 14847d62b00eSchristos object, evaluate the expression between OP_PTR and OP_END. */ 14857d62b00eSchristos 14867d62b00eSchristos void 14877d62b00eSchristos dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr, 14887d62b00eSchristos const gdb_byte *op_end) 14897d62b00eSchristos { 1490*6881a400Schristos gdbarch *arch = this->m_per_objfile->objfile->arch (); 1491*6881a400Schristos bfd_endian byte_order = gdbarch_byte_order (arch); 14927d62b00eSchristos /* Old-style "untyped" DWARF values need special treatment in a 14937d62b00eSchristos couple of places, specifically DW_OP_mod and DW_OP_shr. We need 14947d62b00eSchristos a special type for these values so we can distinguish them from 14957d62b00eSchristos values that have an explicit type, because explicitly-typed 14967d62b00eSchristos values do not need special treatment. This special type must be 14977d62b00eSchristos different (in the `==' sense) from any base type coming from the 14987d62b00eSchristos CU. */ 1499*6881a400Schristos type *address_type = this->address_type (); 15007d62b00eSchristos 1501*6881a400Schristos this->m_location = DWARF_VALUE_MEMORY; 1502*6881a400Schristos this->m_initialized = 1; /* Default is initialized. */ 15037d62b00eSchristos 1504*6881a400Schristos if (this->m_recursion_depth > this->m_max_recursion_depth) 15057d62b00eSchristos error (_("DWARF-2 expression error: Loop detected (%d)."), 1506*6881a400Schristos this->m_recursion_depth); 1507*6881a400Schristos this->m_recursion_depth++; 15087d62b00eSchristos 15097d62b00eSchristos while (op_ptr < op_end) 15107d62b00eSchristos { 1511*6881a400Schristos dwarf_location_atom op = (dwarf_location_atom) *op_ptr++; 15127d62b00eSchristos ULONGEST result; 15137d62b00eSchristos /* Assume the value is not in stack memory. 15147d62b00eSchristos Code that knows otherwise sets this to true. 15157d62b00eSchristos Some arithmetic on stack addresses can probably be assumed to still 15167d62b00eSchristos be a stack address, but we skip this complication for now. 15177d62b00eSchristos This is just an optimization, so it's always ok to punt 15187d62b00eSchristos and leave this as false. */ 15197d62b00eSchristos bool in_stack_memory = false; 15207d62b00eSchristos uint64_t uoffset, reg; 15217d62b00eSchristos int64_t offset; 1522*6881a400Schristos value *result_val = NULL; 15237d62b00eSchristos 15247d62b00eSchristos /* The DWARF expression might have a bug causing an infinite 15257d62b00eSchristos loop. In that case, quitting is the only way out. */ 15267d62b00eSchristos QUIT; 15277d62b00eSchristos 15287d62b00eSchristos switch (op) 15297d62b00eSchristos { 15307d62b00eSchristos case DW_OP_lit0: 15317d62b00eSchristos case DW_OP_lit1: 15327d62b00eSchristos case DW_OP_lit2: 15337d62b00eSchristos case DW_OP_lit3: 15347d62b00eSchristos case DW_OP_lit4: 15357d62b00eSchristos case DW_OP_lit5: 15367d62b00eSchristos case DW_OP_lit6: 15377d62b00eSchristos case DW_OP_lit7: 15387d62b00eSchristos case DW_OP_lit8: 15397d62b00eSchristos case DW_OP_lit9: 15407d62b00eSchristos case DW_OP_lit10: 15417d62b00eSchristos case DW_OP_lit11: 15427d62b00eSchristos case DW_OP_lit12: 15437d62b00eSchristos case DW_OP_lit13: 15447d62b00eSchristos case DW_OP_lit14: 15457d62b00eSchristos case DW_OP_lit15: 15467d62b00eSchristos case DW_OP_lit16: 15477d62b00eSchristos case DW_OP_lit17: 15487d62b00eSchristos case DW_OP_lit18: 15497d62b00eSchristos case DW_OP_lit19: 15507d62b00eSchristos case DW_OP_lit20: 15517d62b00eSchristos case DW_OP_lit21: 15527d62b00eSchristos case DW_OP_lit22: 15537d62b00eSchristos case DW_OP_lit23: 15547d62b00eSchristos case DW_OP_lit24: 15557d62b00eSchristos case DW_OP_lit25: 15567d62b00eSchristos case DW_OP_lit26: 15577d62b00eSchristos case DW_OP_lit27: 15587d62b00eSchristos case DW_OP_lit28: 15597d62b00eSchristos case DW_OP_lit29: 15607d62b00eSchristos case DW_OP_lit30: 15617d62b00eSchristos case DW_OP_lit31: 15627d62b00eSchristos result = op - DW_OP_lit0; 15637d62b00eSchristos result_val = value_from_ulongest (address_type, result); 15647d62b00eSchristos break; 15657d62b00eSchristos 15667d62b00eSchristos case DW_OP_addr: 15677d62b00eSchristos result = extract_unsigned_integer (op_ptr, 1568*6881a400Schristos this->m_addr_size, byte_order); 1569*6881a400Schristos op_ptr += this->m_addr_size; 15707d62b00eSchristos /* Some versions of GCC emit DW_OP_addr before 15717d62b00eSchristos DW_OP_GNU_push_tls_address. In this case the value is an 15727d62b00eSchristos index, not an address. We don't support things like 15737d62b00eSchristos branching between the address and the TLS op. */ 15747d62b00eSchristos if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address) 1575*6881a400Schristos result += this->m_per_objfile->objfile->text_section_offset (); 15767d62b00eSchristos result_val = value_from_ulongest (address_type, result); 15777d62b00eSchristos break; 15787d62b00eSchristos 15797d62b00eSchristos case DW_OP_addrx: 15807d62b00eSchristos case DW_OP_GNU_addr_index: 1581*6881a400Schristos ensure_have_per_cu (this->m_per_cu, "DW_OP_addrx"); 1582*6881a400Schristos 15837d62b00eSchristos op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset); 1584*6881a400Schristos result = dwarf2_read_addr_index (this->m_per_cu, this->m_per_objfile, 1585*6881a400Schristos uoffset); 1586*6881a400Schristos result += this->m_per_objfile->objfile->text_section_offset (); 15877d62b00eSchristos result_val = value_from_ulongest (address_type, result); 15887d62b00eSchristos break; 15897d62b00eSchristos case DW_OP_GNU_const_index: 1590*6881a400Schristos ensure_have_per_cu (this->m_per_cu, "DW_OP_GNU_const_index"); 1591*6881a400Schristos 15927d62b00eSchristos op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset); 1593*6881a400Schristos result = dwarf2_read_addr_index (this->m_per_cu, this->m_per_objfile, 1594*6881a400Schristos uoffset); 15957d62b00eSchristos result_val = value_from_ulongest (address_type, result); 15967d62b00eSchristos break; 15977d62b00eSchristos 15987d62b00eSchristos case DW_OP_const1u: 15997d62b00eSchristos result = extract_unsigned_integer (op_ptr, 1, byte_order); 16007d62b00eSchristos result_val = value_from_ulongest (address_type, result); 16017d62b00eSchristos op_ptr += 1; 16027d62b00eSchristos break; 16037d62b00eSchristos case DW_OP_const1s: 16047d62b00eSchristos result = extract_signed_integer (op_ptr, 1, byte_order); 16057d62b00eSchristos result_val = value_from_ulongest (address_type, result); 16067d62b00eSchristos op_ptr += 1; 16077d62b00eSchristos break; 16087d62b00eSchristos case DW_OP_const2u: 16097d62b00eSchristos result = extract_unsigned_integer (op_ptr, 2, byte_order); 16107d62b00eSchristos result_val = value_from_ulongest (address_type, result); 16117d62b00eSchristos op_ptr += 2; 16127d62b00eSchristos break; 16137d62b00eSchristos case DW_OP_const2s: 16147d62b00eSchristos result = extract_signed_integer (op_ptr, 2, byte_order); 16157d62b00eSchristos result_val = value_from_ulongest (address_type, result); 16167d62b00eSchristos op_ptr += 2; 16177d62b00eSchristos break; 16187d62b00eSchristos case DW_OP_const4u: 16197d62b00eSchristos result = extract_unsigned_integer (op_ptr, 4, byte_order); 16207d62b00eSchristos result_val = value_from_ulongest (address_type, result); 16217d62b00eSchristos op_ptr += 4; 16227d62b00eSchristos break; 16237d62b00eSchristos case DW_OP_const4s: 16247d62b00eSchristos result = extract_signed_integer (op_ptr, 4, byte_order); 16257d62b00eSchristos result_val = value_from_ulongest (address_type, result); 16267d62b00eSchristos op_ptr += 4; 16277d62b00eSchristos break; 16287d62b00eSchristos case DW_OP_const8u: 16297d62b00eSchristos result = extract_unsigned_integer (op_ptr, 8, byte_order); 16307d62b00eSchristos result_val = value_from_ulongest (address_type, result); 16317d62b00eSchristos op_ptr += 8; 16327d62b00eSchristos break; 16337d62b00eSchristos case DW_OP_const8s: 16347d62b00eSchristos result = extract_signed_integer (op_ptr, 8, byte_order); 16357d62b00eSchristos result_val = value_from_ulongest (address_type, result); 16367d62b00eSchristos op_ptr += 8; 16377d62b00eSchristos break; 16387d62b00eSchristos case DW_OP_constu: 16397d62b00eSchristos op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset); 16407d62b00eSchristos result = uoffset; 16417d62b00eSchristos result_val = value_from_ulongest (address_type, result); 16427d62b00eSchristos break; 16437d62b00eSchristos case DW_OP_consts: 16447d62b00eSchristos op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset); 16457d62b00eSchristos result = offset; 16467d62b00eSchristos result_val = value_from_ulongest (address_type, result); 16477d62b00eSchristos break; 16487d62b00eSchristos 16497d62b00eSchristos /* The DW_OP_reg operations are required to occur alone in 16507d62b00eSchristos location expressions. */ 16517d62b00eSchristos case DW_OP_reg0: 16527d62b00eSchristos case DW_OP_reg1: 16537d62b00eSchristos case DW_OP_reg2: 16547d62b00eSchristos case DW_OP_reg3: 16557d62b00eSchristos case DW_OP_reg4: 16567d62b00eSchristos case DW_OP_reg5: 16577d62b00eSchristos case DW_OP_reg6: 16587d62b00eSchristos case DW_OP_reg7: 16597d62b00eSchristos case DW_OP_reg8: 16607d62b00eSchristos case DW_OP_reg9: 16617d62b00eSchristos case DW_OP_reg10: 16627d62b00eSchristos case DW_OP_reg11: 16637d62b00eSchristos case DW_OP_reg12: 16647d62b00eSchristos case DW_OP_reg13: 16657d62b00eSchristos case DW_OP_reg14: 16667d62b00eSchristos case DW_OP_reg15: 16677d62b00eSchristos case DW_OP_reg16: 16687d62b00eSchristos case DW_OP_reg17: 16697d62b00eSchristos case DW_OP_reg18: 16707d62b00eSchristos case DW_OP_reg19: 16717d62b00eSchristos case DW_OP_reg20: 16727d62b00eSchristos case DW_OP_reg21: 16737d62b00eSchristos case DW_OP_reg22: 16747d62b00eSchristos case DW_OP_reg23: 16757d62b00eSchristos case DW_OP_reg24: 16767d62b00eSchristos case DW_OP_reg25: 16777d62b00eSchristos case DW_OP_reg26: 16787d62b00eSchristos case DW_OP_reg27: 16797d62b00eSchristos case DW_OP_reg28: 16807d62b00eSchristos case DW_OP_reg29: 16817d62b00eSchristos case DW_OP_reg30: 16827d62b00eSchristos case DW_OP_reg31: 16837d62b00eSchristos dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_reg"); 16847d62b00eSchristos 16857d62b00eSchristos result = op - DW_OP_reg0; 16867d62b00eSchristos result_val = value_from_ulongest (address_type, result); 1687*6881a400Schristos this->m_location = DWARF_VALUE_REGISTER; 16887d62b00eSchristos break; 16897d62b00eSchristos 16907d62b00eSchristos case DW_OP_regx: 16917d62b00eSchristos op_ptr = safe_read_uleb128 (op_ptr, op_end, ®); 16927d62b00eSchristos dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx"); 16937d62b00eSchristos 16947d62b00eSchristos result = reg; 16957d62b00eSchristos result_val = value_from_ulongest (address_type, result); 1696*6881a400Schristos this->m_location = DWARF_VALUE_REGISTER; 16977d62b00eSchristos break; 16987d62b00eSchristos 16997d62b00eSchristos case DW_OP_implicit_value: 17007d62b00eSchristos { 17017d62b00eSchristos uint64_t len; 17027d62b00eSchristos 17037d62b00eSchristos op_ptr = safe_read_uleb128 (op_ptr, op_end, &len); 17047d62b00eSchristos if (op_ptr + len > op_end) 17057d62b00eSchristos error (_("DW_OP_implicit_value: too few bytes available.")); 1706*6881a400Schristos this->m_len = len; 1707*6881a400Schristos this->m_data = op_ptr; 1708*6881a400Schristos this->m_location = DWARF_VALUE_LITERAL; 17097d62b00eSchristos op_ptr += len; 17107d62b00eSchristos dwarf_expr_require_composition (op_ptr, op_end, 17117d62b00eSchristos "DW_OP_implicit_value"); 17127d62b00eSchristos } 17137d62b00eSchristos goto no_push; 17147d62b00eSchristos 17157d62b00eSchristos case DW_OP_stack_value: 1716*6881a400Schristos this->m_location = DWARF_VALUE_STACK; 17177d62b00eSchristos dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value"); 17187d62b00eSchristos goto no_push; 17197d62b00eSchristos 17207d62b00eSchristos case DW_OP_implicit_pointer: 17217d62b00eSchristos case DW_OP_GNU_implicit_pointer: 17227d62b00eSchristos { 17237d62b00eSchristos int64_t len; 1724*6881a400Schristos ensure_have_per_cu (this->m_per_cu, "DW_OP_implicit_pointer"); 17257d62b00eSchristos 1726*6881a400Schristos int ref_addr_size = this->m_per_cu->ref_addr_size (); 17277d62b00eSchristos 17287d62b00eSchristos /* The referred-to DIE of sect_offset kind. */ 1729*6881a400Schristos this->m_len = extract_unsigned_integer (op_ptr, ref_addr_size, 17307d62b00eSchristos byte_order); 1731*6881a400Schristos op_ptr += ref_addr_size; 17327d62b00eSchristos 17337d62b00eSchristos /* The byte offset into the data. */ 17347d62b00eSchristos op_ptr = safe_read_sleb128 (op_ptr, op_end, &len); 17357d62b00eSchristos result = (ULONGEST) len; 17367d62b00eSchristos result_val = value_from_ulongest (address_type, result); 17377d62b00eSchristos 1738*6881a400Schristos this->m_location = DWARF_VALUE_IMPLICIT_POINTER; 17397d62b00eSchristos dwarf_expr_require_composition (op_ptr, op_end, 17407d62b00eSchristos "DW_OP_implicit_pointer"); 17417d62b00eSchristos } 17427d62b00eSchristos break; 17437d62b00eSchristos 17447d62b00eSchristos case DW_OP_breg0: 17457d62b00eSchristos case DW_OP_breg1: 17467d62b00eSchristos case DW_OP_breg2: 17477d62b00eSchristos case DW_OP_breg3: 17487d62b00eSchristos case DW_OP_breg4: 17497d62b00eSchristos case DW_OP_breg5: 17507d62b00eSchristos case DW_OP_breg6: 17517d62b00eSchristos case DW_OP_breg7: 17527d62b00eSchristos case DW_OP_breg8: 17537d62b00eSchristos case DW_OP_breg9: 17547d62b00eSchristos case DW_OP_breg10: 17557d62b00eSchristos case DW_OP_breg11: 17567d62b00eSchristos case DW_OP_breg12: 17577d62b00eSchristos case DW_OP_breg13: 17587d62b00eSchristos case DW_OP_breg14: 17597d62b00eSchristos case DW_OP_breg15: 17607d62b00eSchristos case DW_OP_breg16: 17617d62b00eSchristos case DW_OP_breg17: 17627d62b00eSchristos case DW_OP_breg18: 17637d62b00eSchristos case DW_OP_breg19: 17647d62b00eSchristos case DW_OP_breg20: 17657d62b00eSchristos case DW_OP_breg21: 17667d62b00eSchristos case DW_OP_breg22: 17677d62b00eSchristos case DW_OP_breg23: 17687d62b00eSchristos case DW_OP_breg24: 17697d62b00eSchristos case DW_OP_breg25: 17707d62b00eSchristos case DW_OP_breg26: 17717d62b00eSchristos case DW_OP_breg27: 17727d62b00eSchristos case DW_OP_breg28: 17737d62b00eSchristos case DW_OP_breg29: 17747d62b00eSchristos case DW_OP_breg30: 17757d62b00eSchristos case DW_OP_breg31: 17767d62b00eSchristos { 17777d62b00eSchristos op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset); 1778*6881a400Schristos ensure_have_frame (this->m_frame, "DW_OP_breg"); 1779*6881a400Schristos 1780*6881a400Schristos result = read_addr_from_reg (this->m_frame, op - DW_OP_breg0); 17817d62b00eSchristos result += offset; 17827d62b00eSchristos result_val = value_from_ulongest (address_type, result); 17837d62b00eSchristos } 17847d62b00eSchristos break; 17857d62b00eSchristos case DW_OP_bregx: 17867d62b00eSchristos { 17877d62b00eSchristos op_ptr = safe_read_uleb128 (op_ptr, op_end, ®); 17887d62b00eSchristos op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset); 1789*6881a400Schristos ensure_have_frame (this->m_frame, "DW_OP_bregx"); 1790*6881a400Schristos 1791*6881a400Schristos result = read_addr_from_reg (this->m_frame, reg); 17927d62b00eSchristos result += offset; 17937d62b00eSchristos result_val = value_from_ulongest (address_type, result); 17947d62b00eSchristos } 17957d62b00eSchristos break; 17967d62b00eSchristos case DW_OP_fbreg: 17977d62b00eSchristos { 17987d62b00eSchristos const gdb_byte *datastart; 17997d62b00eSchristos size_t datalen; 18007d62b00eSchristos 18017d62b00eSchristos op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset); 18027d62b00eSchristos 18037d62b00eSchristos /* Rather than create a whole new context, we simply 18047d62b00eSchristos backup the current stack locally and install a new empty stack, 18057d62b00eSchristos then reset it afterwards, effectively erasing whatever the 18067d62b00eSchristos recursive call put there. */ 1807*6881a400Schristos std::vector<dwarf_stack_value> saved_stack = std::move (this->m_stack); 1808*6881a400Schristos this->m_stack.clear (); 18097d62b00eSchristos 18107d62b00eSchristos /* FIXME: cagney/2003-03-26: This code should be using 18117d62b00eSchristos get_frame_base_address(), and then implement a dwarf2 18127d62b00eSchristos specific this_base method. */ 18137d62b00eSchristos this->get_frame_base (&datastart, &datalen); 18147d62b00eSchristos eval (datastart, datalen); 1815*6881a400Schristos if (this->m_location == DWARF_VALUE_MEMORY) 18167d62b00eSchristos result = fetch_address (0); 1817*6881a400Schristos else if (this->m_location == DWARF_VALUE_REGISTER) 1818*6881a400Schristos result 1819*6881a400Schristos = read_addr_from_reg (this->m_frame, value_as_long (fetch (0))); 18207d62b00eSchristos else 18217d62b00eSchristos error (_("Not implemented: computing frame " 18227d62b00eSchristos "base using explicit value operator")); 18237d62b00eSchristos result = result + offset; 18247d62b00eSchristos result_val = value_from_ulongest (address_type, result); 18257d62b00eSchristos in_stack_memory = true; 18267d62b00eSchristos 18277d62b00eSchristos /* Restore the content of the original stack. */ 1828*6881a400Schristos this->m_stack = std::move (saved_stack); 18297d62b00eSchristos 1830*6881a400Schristos this->m_location = DWARF_VALUE_MEMORY; 18317d62b00eSchristos } 18327d62b00eSchristos break; 18337d62b00eSchristos 18347d62b00eSchristos case DW_OP_dup: 18357d62b00eSchristos result_val = fetch (0); 18367d62b00eSchristos in_stack_memory = fetch_in_stack_memory (0); 18377d62b00eSchristos break; 18387d62b00eSchristos 18397d62b00eSchristos case DW_OP_drop: 18407d62b00eSchristos pop (); 18417d62b00eSchristos goto no_push; 18427d62b00eSchristos 18437d62b00eSchristos case DW_OP_pick: 18447d62b00eSchristos offset = *op_ptr++; 18457d62b00eSchristos result_val = fetch (offset); 18467d62b00eSchristos in_stack_memory = fetch_in_stack_memory (offset); 18477d62b00eSchristos break; 18487d62b00eSchristos 18497d62b00eSchristos case DW_OP_swap: 18507d62b00eSchristos { 1851*6881a400Schristos if (this->m_stack.size () < 2) 18527d62b00eSchristos error (_("Not enough elements for " 18537d62b00eSchristos "DW_OP_swap. Need 2, have %zu."), 1854*6881a400Schristos this->m_stack.size ()); 18557d62b00eSchristos 1856*6881a400Schristos dwarf_stack_value &t1 = this->m_stack[this->m_stack.size () - 1]; 1857*6881a400Schristos dwarf_stack_value &t2 = this->m_stack[this->m_stack.size () - 2]; 18587d62b00eSchristos std::swap (t1, t2); 18597d62b00eSchristos goto no_push; 18607d62b00eSchristos } 18617d62b00eSchristos 18627d62b00eSchristos case DW_OP_over: 18637d62b00eSchristos result_val = fetch (1); 18647d62b00eSchristos in_stack_memory = fetch_in_stack_memory (1); 18657d62b00eSchristos break; 18667d62b00eSchristos 18677d62b00eSchristos case DW_OP_rot: 18687d62b00eSchristos { 1869*6881a400Schristos if (this->m_stack.size () < 3) 18707d62b00eSchristos error (_("Not enough elements for " 18717d62b00eSchristos "DW_OP_rot. Need 3, have %zu."), 1872*6881a400Schristos this->m_stack.size ()); 18737d62b00eSchristos 1874*6881a400Schristos dwarf_stack_value temp = this->m_stack[this->m_stack.size () - 1]; 1875*6881a400Schristos this->m_stack[this->m_stack.size () - 1] 1876*6881a400Schristos = this->m_stack[this->m_stack.size () - 2]; 1877*6881a400Schristos this->m_stack[this->m_stack.size () - 2] 1878*6881a400Schristos = this->m_stack[this->m_stack.size () - 3]; 1879*6881a400Schristos this->m_stack[this->m_stack.size () - 3] = temp; 18807d62b00eSchristos goto no_push; 18817d62b00eSchristos } 18827d62b00eSchristos 18837d62b00eSchristos case DW_OP_deref: 18847d62b00eSchristos case DW_OP_deref_size: 18857d62b00eSchristos case DW_OP_deref_type: 18867d62b00eSchristos case DW_OP_GNU_deref_type: 18877d62b00eSchristos { 1888*6881a400Schristos int addr_size = (op == DW_OP_deref ? this->m_addr_size : *op_ptr++); 18897d62b00eSchristos gdb_byte *buf = (gdb_byte *) alloca (addr_size); 18907d62b00eSchristos CORE_ADDR addr = fetch_address (0); 18917d62b00eSchristos struct type *type; 18927d62b00eSchristos 18937d62b00eSchristos pop (); 18947d62b00eSchristos 18957d62b00eSchristos if (op == DW_OP_deref_type || op == DW_OP_GNU_deref_type) 18967d62b00eSchristos { 18977d62b00eSchristos op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset); 18987d62b00eSchristos cu_offset type_die_cu_off = (cu_offset) uoffset; 1899*6881a400Schristos type = get_base_type (type_die_cu_off); 19007d62b00eSchristos } 19017d62b00eSchristos else 19027d62b00eSchristos type = address_type; 19037d62b00eSchristos 19047d62b00eSchristos this->read_mem (buf, addr, addr_size); 19057d62b00eSchristos 19067d62b00eSchristos /* If the size of the object read from memory is different 19077d62b00eSchristos from the type length, we need to zero-extend it. */ 1908*6881a400Schristos if (type->length () != addr_size) 19097d62b00eSchristos { 19107d62b00eSchristos ULONGEST datum = 19117d62b00eSchristos extract_unsigned_integer (buf, addr_size, byte_order); 19127d62b00eSchristos 1913*6881a400Schristos buf = (gdb_byte *) alloca (type->length ()); 1914*6881a400Schristos store_unsigned_integer (buf, type->length (), 19157d62b00eSchristos byte_order, datum); 19167d62b00eSchristos } 19177d62b00eSchristos 19187d62b00eSchristos result_val = value_from_contents_and_address (type, buf, addr); 19197d62b00eSchristos break; 19207d62b00eSchristos } 19217d62b00eSchristos 19227d62b00eSchristos case DW_OP_abs: 19237d62b00eSchristos case DW_OP_neg: 19247d62b00eSchristos case DW_OP_not: 19257d62b00eSchristos case DW_OP_plus_uconst: 19267d62b00eSchristos { 19277d62b00eSchristos /* Unary operations. */ 19287d62b00eSchristos result_val = fetch (0); 19297d62b00eSchristos pop (); 19307d62b00eSchristos 19317d62b00eSchristos switch (op) 19327d62b00eSchristos { 19337d62b00eSchristos case DW_OP_abs: 19347d62b00eSchristos if (value_less (result_val, 19357d62b00eSchristos value_zero (value_type (result_val), not_lval))) 19367d62b00eSchristos result_val = value_neg (result_val); 19377d62b00eSchristos break; 19387d62b00eSchristos case DW_OP_neg: 19397d62b00eSchristos result_val = value_neg (result_val); 19407d62b00eSchristos break; 19417d62b00eSchristos case DW_OP_not: 19427d62b00eSchristos dwarf_require_integral (value_type (result_val)); 19437d62b00eSchristos result_val = value_complement (result_val); 19447d62b00eSchristos break; 19457d62b00eSchristos case DW_OP_plus_uconst: 19467d62b00eSchristos dwarf_require_integral (value_type (result_val)); 19477d62b00eSchristos result = value_as_long (result_val); 19487d62b00eSchristos op_ptr = safe_read_uleb128 (op_ptr, op_end, ®); 19497d62b00eSchristos result += reg; 19507d62b00eSchristos result_val = value_from_ulongest (address_type, result); 19517d62b00eSchristos break; 19527d62b00eSchristos } 19537d62b00eSchristos } 19547d62b00eSchristos break; 19557d62b00eSchristos 19567d62b00eSchristos case DW_OP_and: 19577d62b00eSchristos case DW_OP_div: 19587d62b00eSchristos case DW_OP_minus: 19597d62b00eSchristos case DW_OP_mod: 19607d62b00eSchristos case DW_OP_mul: 19617d62b00eSchristos case DW_OP_or: 19627d62b00eSchristos case DW_OP_plus: 19637d62b00eSchristos case DW_OP_shl: 19647d62b00eSchristos case DW_OP_shr: 19657d62b00eSchristos case DW_OP_shra: 19667d62b00eSchristos case DW_OP_xor: 19677d62b00eSchristos case DW_OP_le: 19687d62b00eSchristos case DW_OP_ge: 19697d62b00eSchristos case DW_OP_eq: 19707d62b00eSchristos case DW_OP_lt: 19717d62b00eSchristos case DW_OP_gt: 19727d62b00eSchristos case DW_OP_ne: 19737d62b00eSchristos { 19747d62b00eSchristos /* Binary operations. */ 19757d62b00eSchristos struct value *first, *second; 19767d62b00eSchristos 19777d62b00eSchristos second = fetch (0); 19787d62b00eSchristos pop (); 19797d62b00eSchristos 19807d62b00eSchristos first = fetch (0); 19817d62b00eSchristos pop (); 19827d62b00eSchristos 19837d62b00eSchristos if (! base_types_equal_p (value_type (first), value_type (second))) 19847d62b00eSchristos error (_("Incompatible types on DWARF stack")); 19857d62b00eSchristos 19867d62b00eSchristos switch (op) 19877d62b00eSchristos { 19887d62b00eSchristos case DW_OP_and: 19897d62b00eSchristos dwarf_require_integral (value_type (first)); 19907d62b00eSchristos dwarf_require_integral (value_type (second)); 19917d62b00eSchristos result_val = value_binop (first, second, BINOP_BITWISE_AND); 19927d62b00eSchristos break; 19937d62b00eSchristos case DW_OP_div: 19947d62b00eSchristos result_val = value_binop (first, second, BINOP_DIV); 19957d62b00eSchristos break; 19967d62b00eSchristos case DW_OP_minus: 19977d62b00eSchristos result_val = value_binop (first, second, BINOP_SUB); 19987d62b00eSchristos break; 19997d62b00eSchristos case DW_OP_mod: 20007d62b00eSchristos { 20017d62b00eSchristos int cast_back = 0; 20027d62b00eSchristos struct type *orig_type = value_type (first); 20037d62b00eSchristos 20047d62b00eSchristos /* We have to special-case "old-style" untyped values 20057d62b00eSchristos -- these must have mod computed using unsigned 20067d62b00eSchristos math. */ 20077d62b00eSchristos if (orig_type == address_type) 20087d62b00eSchristos { 2009*6881a400Schristos struct type *utype = get_unsigned_type (arch, orig_type); 20107d62b00eSchristos 20117d62b00eSchristos cast_back = 1; 20127d62b00eSchristos first = value_cast (utype, first); 20137d62b00eSchristos second = value_cast (utype, second); 20147d62b00eSchristos } 20157d62b00eSchristos /* Note that value_binop doesn't handle float or 20167d62b00eSchristos decimal float here. This seems unimportant. */ 20177d62b00eSchristos result_val = value_binop (first, second, BINOP_MOD); 20187d62b00eSchristos if (cast_back) 20197d62b00eSchristos result_val = value_cast (orig_type, result_val); 20207d62b00eSchristos } 20217d62b00eSchristos break; 20227d62b00eSchristos case DW_OP_mul: 20237d62b00eSchristos result_val = value_binop (first, second, BINOP_MUL); 20247d62b00eSchristos break; 20257d62b00eSchristos case DW_OP_or: 20267d62b00eSchristos dwarf_require_integral (value_type (first)); 20277d62b00eSchristos dwarf_require_integral (value_type (second)); 20287d62b00eSchristos result_val = value_binop (first, second, BINOP_BITWISE_IOR); 20297d62b00eSchristos break; 20307d62b00eSchristos case DW_OP_plus: 20317d62b00eSchristos result_val = value_binop (first, second, BINOP_ADD); 20327d62b00eSchristos break; 20337d62b00eSchristos case DW_OP_shl: 20347d62b00eSchristos dwarf_require_integral (value_type (first)); 20357d62b00eSchristos dwarf_require_integral (value_type (second)); 20367d62b00eSchristos result_val = value_binop (first, second, BINOP_LSH); 20377d62b00eSchristos break; 20387d62b00eSchristos case DW_OP_shr: 20397d62b00eSchristos dwarf_require_integral (value_type (first)); 20407d62b00eSchristos dwarf_require_integral (value_type (second)); 2041*6881a400Schristos if (!value_type (first)->is_unsigned ()) 20427d62b00eSchristos { 20437d62b00eSchristos struct type *utype 2044*6881a400Schristos = get_unsigned_type (arch, value_type (first)); 20457d62b00eSchristos 20467d62b00eSchristos first = value_cast (utype, first); 20477d62b00eSchristos } 20487d62b00eSchristos 20497d62b00eSchristos result_val = value_binop (first, second, BINOP_RSH); 20507d62b00eSchristos /* Make sure we wind up with the same type we started 20517d62b00eSchristos with. */ 20527d62b00eSchristos if (value_type (result_val) != value_type (second)) 20537d62b00eSchristos result_val = value_cast (value_type (second), result_val); 20547d62b00eSchristos break; 20557d62b00eSchristos case DW_OP_shra: 20567d62b00eSchristos dwarf_require_integral (value_type (first)); 20577d62b00eSchristos dwarf_require_integral (value_type (second)); 2058*6881a400Schristos if (value_type (first)->is_unsigned ()) 20597d62b00eSchristos { 20607d62b00eSchristos struct type *stype 2061*6881a400Schristos = get_signed_type (arch, value_type (first)); 20627d62b00eSchristos 20637d62b00eSchristos first = value_cast (stype, first); 20647d62b00eSchristos } 20657d62b00eSchristos 20667d62b00eSchristos result_val = value_binop (first, second, BINOP_RSH); 20677d62b00eSchristos /* Make sure we wind up with the same type we started 20687d62b00eSchristos with. */ 20697d62b00eSchristos if (value_type (result_val) != value_type (second)) 20707d62b00eSchristos result_val = value_cast (value_type (second), result_val); 20717d62b00eSchristos break; 20727d62b00eSchristos case DW_OP_xor: 20737d62b00eSchristos dwarf_require_integral (value_type (first)); 20747d62b00eSchristos dwarf_require_integral (value_type (second)); 20757d62b00eSchristos result_val = value_binop (first, second, BINOP_BITWISE_XOR); 20767d62b00eSchristos break; 20777d62b00eSchristos case DW_OP_le: 20787d62b00eSchristos /* A <= B is !(B < A). */ 20797d62b00eSchristos result = ! value_less (second, first); 20807d62b00eSchristos result_val = value_from_ulongest (address_type, result); 20817d62b00eSchristos break; 20827d62b00eSchristos case DW_OP_ge: 20837d62b00eSchristos /* A >= B is !(A < B). */ 20847d62b00eSchristos result = ! value_less (first, second); 20857d62b00eSchristos result_val = value_from_ulongest (address_type, result); 20867d62b00eSchristos break; 20877d62b00eSchristos case DW_OP_eq: 20887d62b00eSchristos result = value_equal (first, second); 20897d62b00eSchristos result_val = value_from_ulongest (address_type, result); 20907d62b00eSchristos break; 20917d62b00eSchristos case DW_OP_lt: 20927d62b00eSchristos result = value_less (first, second); 20937d62b00eSchristos result_val = value_from_ulongest (address_type, result); 20947d62b00eSchristos break; 20957d62b00eSchristos case DW_OP_gt: 20967d62b00eSchristos /* A > B is B < A. */ 20977d62b00eSchristos result = value_less (second, first); 20987d62b00eSchristos result_val = value_from_ulongest (address_type, result); 20997d62b00eSchristos break; 21007d62b00eSchristos case DW_OP_ne: 21017d62b00eSchristos result = ! value_equal (first, second); 21027d62b00eSchristos result_val = value_from_ulongest (address_type, result); 21037d62b00eSchristos break; 21047d62b00eSchristos default: 2105*6881a400Schristos internal_error (_("Can't be reached.")); 21067d62b00eSchristos } 21077d62b00eSchristos } 21087d62b00eSchristos break; 21097d62b00eSchristos 21107d62b00eSchristos case DW_OP_call_frame_cfa: 2111*6881a400Schristos ensure_have_frame (this->m_frame, "DW_OP_call_frame_cfa"); 2112*6881a400Schristos 2113*6881a400Schristos result = dwarf2_frame_cfa (this->m_frame); 21147d62b00eSchristos result_val = value_from_ulongest (address_type, result); 21157d62b00eSchristos in_stack_memory = true; 21167d62b00eSchristos break; 21177d62b00eSchristos 21187d62b00eSchristos case DW_OP_GNU_push_tls_address: 21197d62b00eSchristos case DW_OP_form_tls_address: 21207d62b00eSchristos /* Variable is at a constant offset in the thread-local 21217d62b00eSchristos storage block into the objfile for the current thread and 21227d62b00eSchristos the dynamic linker module containing this expression. Here 21237d62b00eSchristos we return returns the offset from that base. The top of the 21247d62b00eSchristos stack has the offset from the beginning of the thread 21257d62b00eSchristos control block at which the variable is located. Nothing 21267d62b00eSchristos should follow this operator, so the top of stack would be 21277d62b00eSchristos returned. */ 21287d62b00eSchristos result = value_as_long (fetch (0)); 21297d62b00eSchristos pop (); 2130*6881a400Schristos result = target_translate_tls_address (this->m_per_objfile->objfile, 2131*6881a400Schristos result); 21327d62b00eSchristos result_val = value_from_ulongest (address_type, result); 21337d62b00eSchristos break; 21347d62b00eSchristos 21357d62b00eSchristos case DW_OP_skip: 21367d62b00eSchristos offset = extract_signed_integer (op_ptr, 2, byte_order); 21377d62b00eSchristos op_ptr += 2; 21387d62b00eSchristos op_ptr += offset; 21397d62b00eSchristos goto no_push; 21407d62b00eSchristos 21417d62b00eSchristos case DW_OP_bra: 21427d62b00eSchristos { 21437d62b00eSchristos struct value *val; 21447d62b00eSchristos 21457d62b00eSchristos offset = extract_signed_integer (op_ptr, 2, byte_order); 21467d62b00eSchristos op_ptr += 2; 21477d62b00eSchristos val = fetch (0); 21487d62b00eSchristos dwarf_require_integral (value_type (val)); 21497d62b00eSchristos if (value_as_long (val) != 0) 21507d62b00eSchristos op_ptr += offset; 21517d62b00eSchristos pop (); 21527d62b00eSchristos } 21537d62b00eSchristos goto no_push; 21547d62b00eSchristos 21557d62b00eSchristos case DW_OP_nop: 21567d62b00eSchristos goto no_push; 21577d62b00eSchristos 21587d62b00eSchristos case DW_OP_piece: 21597d62b00eSchristos { 21607d62b00eSchristos uint64_t size; 21617d62b00eSchristos 21627d62b00eSchristos /* Record the piece. */ 21637d62b00eSchristos op_ptr = safe_read_uleb128 (op_ptr, op_end, &size); 21647d62b00eSchristos add_piece (8 * size, 0); 21657d62b00eSchristos 21667d62b00eSchristos /* Pop off the address/regnum, and reset the location 21677d62b00eSchristos type. */ 2168*6881a400Schristos if (this->m_location != DWARF_VALUE_LITERAL 2169*6881a400Schristos && this->m_location != DWARF_VALUE_OPTIMIZED_OUT) 21707d62b00eSchristos pop (); 2171*6881a400Schristos this->m_location = DWARF_VALUE_MEMORY; 21727d62b00eSchristos } 21737d62b00eSchristos goto no_push; 21747d62b00eSchristos 21757d62b00eSchristos case DW_OP_bit_piece: 21767d62b00eSchristos { 21777d62b00eSchristos uint64_t size, uleb_offset; 21787d62b00eSchristos 21797d62b00eSchristos /* Record the piece. */ 21807d62b00eSchristos op_ptr = safe_read_uleb128 (op_ptr, op_end, &size); 21817d62b00eSchristos op_ptr = safe_read_uleb128 (op_ptr, op_end, &uleb_offset); 21827d62b00eSchristos add_piece (size, uleb_offset); 21837d62b00eSchristos 21847d62b00eSchristos /* Pop off the address/regnum, and reset the location 21857d62b00eSchristos type. */ 2186*6881a400Schristos if (this->m_location != DWARF_VALUE_LITERAL 2187*6881a400Schristos && this->m_location != DWARF_VALUE_OPTIMIZED_OUT) 21887d62b00eSchristos pop (); 2189*6881a400Schristos this->m_location = DWARF_VALUE_MEMORY; 21907d62b00eSchristos } 21917d62b00eSchristos goto no_push; 21927d62b00eSchristos 21937d62b00eSchristos case DW_OP_GNU_uninit: 21947d62b00eSchristos if (op_ptr != op_end) 21957d62b00eSchristos error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always " 21967d62b00eSchristos "be the very last op.")); 21977d62b00eSchristos 2198*6881a400Schristos this->m_initialized = 0; 21997d62b00eSchristos goto no_push; 22007d62b00eSchristos 22017d62b00eSchristos case DW_OP_call2: 22027d62b00eSchristos { 22037d62b00eSchristos cu_offset cu_off 22047d62b00eSchristos = (cu_offset) extract_unsigned_integer (op_ptr, 2, byte_order); 22057d62b00eSchristos op_ptr += 2; 22067d62b00eSchristos this->dwarf_call (cu_off); 22077d62b00eSchristos } 22087d62b00eSchristos goto no_push; 22097d62b00eSchristos 22107d62b00eSchristos case DW_OP_call4: 22117d62b00eSchristos { 22127d62b00eSchristos cu_offset cu_off 22137d62b00eSchristos = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order); 22147d62b00eSchristos op_ptr += 4; 22157d62b00eSchristos this->dwarf_call (cu_off); 22167d62b00eSchristos } 22177d62b00eSchristos goto no_push; 22187d62b00eSchristos 22197d62b00eSchristos case DW_OP_GNU_variable_value: 22207d62b00eSchristos { 2221*6881a400Schristos ensure_have_per_cu (this->m_per_cu, "DW_OP_GNU_variable_value"); 2222*6881a400Schristos int ref_addr_size = this->m_per_cu->ref_addr_size (); 2223*6881a400Schristos 22247d62b00eSchristos sect_offset sect_off 22257d62b00eSchristos = (sect_offset) extract_unsigned_integer (op_ptr, 2226*6881a400Schristos ref_addr_size, 22277d62b00eSchristos byte_order); 2228*6881a400Schristos op_ptr += ref_addr_size; 2229*6881a400Schristos result_val = sect_variable_value (sect_off, this->m_per_cu, 2230*6881a400Schristos this->m_per_objfile); 2231*6881a400Schristos result_val = value_cast (address_type, result_val); 22327d62b00eSchristos } 22337d62b00eSchristos break; 22347d62b00eSchristos 22357d62b00eSchristos case DW_OP_entry_value: 22367d62b00eSchristos case DW_OP_GNU_entry_value: 22377d62b00eSchristos { 22387d62b00eSchristos uint64_t len; 22397d62b00eSchristos CORE_ADDR deref_size; 22407d62b00eSchristos union call_site_parameter_u kind_u; 22417d62b00eSchristos 22427d62b00eSchristos op_ptr = safe_read_uleb128 (op_ptr, op_end, &len); 22437d62b00eSchristos if (op_ptr + len > op_end) 22447d62b00eSchristos error (_("DW_OP_entry_value: too few bytes available.")); 22457d62b00eSchristos 22467d62b00eSchristos kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len); 22477d62b00eSchristos if (kind_u.dwarf_reg != -1) 22487d62b00eSchristos { 22497d62b00eSchristos op_ptr += len; 22507d62b00eSchristos this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG, 22517d62b00eSchristos kind_u, 22527d62b00eSchristos -1 /* deref_size */); 22537d62b00eSchristos goto no_push; 22547d62b00eSchristos } 22557d62b00eSchristos 22567d62b00eSchristos kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr, 22577d62b00eSchristos op_ptr + len, 22587d62b00eSchristos &deref_size); 22597d62b00eSchristos if (kind_u.dwarf_reg != -1) 22607d62b00eSchristos { 22617d62b00eSchristos if (deref_size == -1) 2262*6881a400Schristos deref_size = this->m_addr_size; 22637d62b00eSchristos op_ptr += len; 22647d62b00eSchristos this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG, 22657d62b00eSchristos kind_u, deref_size); 22667d62b00eSchristos goto no_push; 22677d62b00eSchristos } 22687d62b00eSchristos 22697d62b00eSchristos error (_("DWARF-2 expression error: DW_OP_entry_value is " 22707d62b00eSchristos "supported only for single DW_OP_reg* " 22717d62b00eSchristos "or for DW_OP_breg*(0)+DW_OP_deref*")); 22727d62b00eSchristos } 22737d62b00eSchristos 22747d62b00eSchristos case DW_OP_GNU_parameter_ref: 22757d62b00eSchristos { 22767d62b00eSchristos union call_site_parameter_u kind_u; 22777d62b00eSchristos 22787d62b00eSchristos kind_u.param_cu_off 22797d62b00eSchristos = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order); 22807d62b00eSchristos op_ptr += 4; 22817d62b00eSchristos this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_PARAM_OFFSET, 22827d62b00eSchristos kind_u, 22837d62b00eSchristos -1 /* deref_size */); 22847d62b00eSchristos } 22857d62b00eSchristos goto no_push; 22867d62b00eSchristos 22877d62b00eSchristos case DW_OP_const_type: 22887d62b00eSchristos case DW_OP_GNU_const_type: 22897d62b00eSchristos { 22907d62b00eSchristos int n; 22917d62b00eSchristos const gdb_byte *data; 22927d62b00eSchristos struct type *type; 22937d62b00eSchristos 22947d62b00eSchristos op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset); 22957d62b00eSchristos cu_offset type_die_cu_off = (cu_offset) uoffset; 22967d62b00eSchristos 22977d62b00eSchristos n = *op_ptr++; 22987d62b00eSchristos data = op_ptr; 22997d62b00eSchristos op_ptr += n; 23007d62b00eSchristos 2301*6881a400Schristos type = get_base_type (type_die_cu_off); 2302*6881a400Schristos 2303*6881a400Schristos if (type->length () != n) 2304*6881a400Schristos error (_("DW_OP_const_type has different sizes for type and data")); 2305*6881a400Schristos 23067d62b00eSchristos result_val = value_from_contents (type, data); 23077d62b00eSchristos } 23087d62b00eSchristos break; 23097d62b00eSchristos 23107d62b00eSchristos case DW_OP_regval_type: 23117d62b00eSchristos case DW_OP_GNU_regval_type: 23127d62b00eSchristos { 23137d62b00eSchristos op_ptr = safe_read_uleb128 (op_ptr, op_end, ®); 23147d62b00eSchristos op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset); 23157d62b00eSchristos cu_offset type_die_cu_off = (cu_offset) uoffset; 23167d62b00eSchristos 2317*6881a400Schristos ensure_have_frame (this->m_frame, "DW_OP_regval_type"); 2318*6881a400Schristos 2319*6881a400Schristos struct type *type = get_base_type (type_die_cu_off); 2320*6881a400Schristos int regnum 2321*6881a400Schristos = dwarf_reg_to_regnum_or_error (get_frame_arch (this->m_frame), 2322*6881a400Schristos reg); 2323*6881a400Schristos result_val = value_from_register (type, regnum, this->m_frame); 23247d62b00eSchristos } 23257d62b00eSchristos break; 23267d62b00eSchristos 23277d62b00eSchristos case DW_OP_convert: 23287d62b00eSchristos case DW_OP_GNU_convert: 23297d62b00eSchristos case DW_OP_reinterpret: 23307d62b00eSchristos case DW_OP_GNU_reinterpret: 23317d62b00eSchristos { 23327d62b00eSchristos struct type *type; 23337d62b00eSchristos 23347d62b00eSchristos op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset); 23357d62b00eSchristos cu_offset type_die_cu_off = (cu_offset) uoffset; 23367d62b00eSchristos 23377d62b00eSchristos if (to_underlying (type_die_cu_off) == 0) 23387d62b00eSchristos type = address_type; 23397d62b00eSchristos else 2340*6881a400Schristos type = get_base_type (type_die_cu_off); 23417d62b00eSchristos 23427d62b00eSchristos result_val = fetch (0); 23437d62b00eSchristos pop (); 23447d62b00eSchristos 23457d62b00eSchristos if (op == DW_OP_convert || op == DW_OP_GNU_convert) 23467d62b00eSchristos result_val = value_cast (type, result_val); 23477d62b00eSchristos else if (type == value_type (result_val)) 23487d62b00eSchristos { 23497d62b00eSchristos /* Nothing. */ 23507d62b00eSchristos } 2351*6881a400Schristos else if (type->length () 2352*6881a400Schristos != value_type (result_val)->length ()) 23537d62b00eSchristos error (_("DW_OP_reinterpret has wrong size")); 23547d62b00eSchristos else 23557d62b00eSchristos result_val 23567d62b00eSchristos = value_from_contents (type, 2357*6881a400Schristos value_contents_all (result_val).data ()); 23587d62b00eSchristos } 23597d62b00eSchristos break; 23607d62b00eSchristos 23617d62b00eSchristos case DW_OP_push_object_address: 23627d62b00eSchristos /* Return the address of the object we are currently observing. */ 2363*6881a400Schristos if (this->m_addr_info == nullptr 2364*6881a400Schristos || (this->m_addr_info->valaddr.data () == nullptr 2365*6881a400Schristos && this->m_addr_info->addr == 0)) 2366*6881a400Schristos error (_("Location address is not set.")); 2367*6881a400Schristos 2368*6881a400Schristos result_val 2369*6881a400Schristos = value_from_ulongest (address_type, this->m_addr_info->addr); 23707d62b00eSchristos break; 23717d62b00eSchristos 23727d62b00eSchristos default: 23737d62b00eSchristos error (_("Unhandled dwarf expression opcode 0x%x"), op); 23747d62b00eSchristos } 23757d62b00eSchristos 23767d62b00eSchristos /* Most things push a result value. */ 23777d62b00eSchristos gdb_assert (result_val != NULL); 23787d62b00eSchristos push (result_val, in_stack_memory); 23797d62b00eSchristos no_push: 23807d62b00eSchristos ; 23817d62b00eSchristos } 23827d62b00eSchristos 23837d62b00eSchristos /* To simplify our main caller, if the result is an implicit 23847d62b00eSchristos pointer, then make a pieced value. This is ok because we can't 23857d62b00eSchristos have implicit pointers in contexts where pieces are invalid. */ 2386*6881a400Schristos if (this->m_location == DWARF_VALUE_IMPLICIT_POINTER) 2387*6881a400Schristos add_piece (8 * this->m_addr_size, 0); 23887d62b00eSchristos 2389*6881a400Schristos this->m_recursion_depth--; 2390*6881a400Schristos gdb_assert (this->m_recursion_depth >= 0); 23917d62b00eSchristos } 2392