| 1 | /* DWARF 2 Expression Evaluator. |
| 2 | |
| 3 | Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008 |
| 4 | Free Software Foundation, Inc. |
| 5 | |
| 6 | Contributed by Daniel Berlin (dan@dberlin.org) |
| 7 | |
| 8 | This file is part of GDB. |
| 9 | |
| 10 | This program is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation; either version 3 of the License, or |
| 13 | (at your option) any later version. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 22 | |
| 23 | #include "defs.h" |
| 24 | #include "symtab.h" |
| 25 | #include "gdbtypes.h" |
| 26 | #include "value.h" |
| 27 | #include "gdbcore.h" |
| 28 | #include "elf/dwarf2.h" |
| 29 | #include "dwarf2expr.h" |
| 30 | #include "gdb_assert.h" |
| 31 | |
| 32 | /* Local prototypes. */ |
| 33 | |
| 34 | static void execute_stack_op (struct dwarf_expr_context *, |
| 35 | gdb_byte *, gdb_byte *); |
| 36 | static struct type *unsigned_address_type (int); |
| 37 | |
| 38 | /* Create a new context for the expression evaluator. */ |
| 39 | |
| 40 | struct dwarf_expr_context * |
| 41 | new_dwarf_expr_context (void) |
| 42 | { |
| 43 | struct dwarf_expr_context *retval; |
| 44 | retval = xcalloc (1, sizeof (struct dwarf_expr_context)); |
| 45 | retval->stack_len = 0; |
| 46 | retval->stack_allocated = 10; |
| 47 | retval->stack = xmalloc (retval->stack_allocated * sizeof (CORE_ADDR)); |
| 48 | retval->num_pieces = 0; |
| 49 | retval->pieces = 0; |
| 50 | retval->max_recursion_depth = 0x100; |
| 51 | return retval; |
| 52 | } |
| 53 | |
| 54 | /* Release the memory allocated to CTX. */ |
| 55 | |
| 56 | void |
| 57 | free_dwarf_expr_context (struct dwarf_expr_context *ctx) |
| 58 | { |
| 59 | xfree (ctx->stack); |
| 60 | xfree (ctx->pieces); |
| 61 | xfree (ctx); |
| 62 | } |
| 63 | |
| 64 | /* Expand the memory allocated to CTX's stack to contain at least |
| 65 | NEED more elements than are currently used. */ |
| 66 | |
| 67 | static void |
| 68 | dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need) |
| 69 | { |
| 70 | if (ctx->stack_len + need > ctx->stack_allocated) |
| 71 | { |
| 72 | size_t newlen = ctx->stack_len + need + 10; |
| 73 | ctx->stack = xrealloc (ctx->stack, |
| 74 | newlen * sizeof (CORE_ADDR)); |
| 75 | ctx->stack_allocated = newlen; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /* Push VALUE onto CTX's stack. */ |
| 80 | |
| 81 | void |
| 82 | dwarf_expr_push (struct dwarf_expr_context *ctx, CORE_ADDR value) |
| 83 | { |
| 84 | dwarf_expr_grow_stack (ctx, 1); |
| 85 | ctx->stack[ctx->stack_len++] = value; |
| 86 | } |
| 87 | |
| 88 | /* Pop the top item off of CTX's stack. */ |
| 89 | |
| 90 | void |
| 91 | dwarf_expr_pop (struct dwarf_expr_context *ctx) |
| 92 | { |
| 93 | if (ctx->stack_len <= 0) |
| 94 | error (_("dwarf expression stack underflow")); |
| 95 | ctx->stack_len--; |
| 96 | } |
| 97 | |
| 98 | /* Retrieve the N'th item on CTX's stack. */ |
| 99 | |
| 100 | CORE_ADDR |
| 101 | dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n) |
| 102 | { |
| 103 | if (ctx->stack_len <= n) |
| 104 | error (_("Asked for position %d of stack, stack only has %d elements on it."), |
| 105 | n, ctx->stack_len); |
| 106 | return ctx->stack[ctx->stack_len - (1 + n)]; |
| 107 | |
| 108 | } |
| 109 | |
| 110 | /* Add a new piece to CTX's piece list. */ |
| 111 | static void |
| 112 | add_piece (struct dwarf_expr_context *ctx, |
| 113 | int in_reg, CORE_ADDR value, ULONGEST size) |
| 114 | { |
| 115 | struct dwarf_expr_piece *p; |
| 116 | |
| 117 | ctx->num_pieces++; |
| 118 | |
| 119 | if (ctx->pieces) |
| 120 | ctx->pieces = xrealloc (ctx->pieces, |
| 121 | (ctx->num_pieces |
| 122 | * sizeof (struct dwarf_expr_piece))); |
| 123 | else |
| 124 | ctx->pieces = xmalloc (ctx->num_pieces |
| 125 | * sizeof (struct dwarf_expr_piece)); |
| 126 | |
| 127 | p = &ctx->pieces[ctx->num_pieces - 1]; |
| 128 | p->in_reg = in_reg; |
| 129 | p->value = value; |
| 130 | p->size = size; |
| 131 | } |
| 132 | |
| 133 | /* Evaluate the expression at ADDR (LEN bytes long) using the context |
| 134 | CTX. */ |
| 135 | |
| 136 | void |
| 137 | dwarf_expr_eval (struct dwarf_expr_context *ctx, gdb_byte *addr, size_t len) |
| 138 | { |
| 139 | int old_recursion_depth = ctx->recursion_depth; |
| 140 | |
| 141 | execute_stack_op (ctx, addr, addr + len); |
| 142 | |
| 143 | /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here. */ |
| 144 | |
| 145 | gdb_assert (ctx->recursion_depth == old_recursion_depth); |
| 146 | } |
| 147 | |
| 148 | /* Decode the unsigned LEB128 constant at BUF into the variable pointed to |
| 149 | by R, and return the new value of BUF. Verify that it doesn't extend |
| 150 | past BUF_END. */ |
| 151 | |
| 152 | gdb_byte * |
| 153 | read_uleb128 (gdb_byte *buf, gdb_byte *buf_end, ULONGEST * r) |
| 154 | { |
| 155 | unsigned shift = 0; |
| 156 | ULONGEST result = 0; |
| 157 | gdb_byte byte; |
| 158 | |
| 159 | while (1) |
| 160 | { |
| 161 | if (buf >= buf_end) |
| 162 | error (_("read_uleb128: Corrupted DWARF expression.")); |
| 163 | |
| 164 | byte = *buf++; |
| 165 | result |= (byte & 0x7f) << shift; |
| 166 | if ((byte & 0x80) == 0) |
| 167 | break; |
| 168 | shift += 7; |
| 169 | } |
| 170 | *r = result; |
| 171 | return buf; |
| 172 | } |
| 173 | |
| 174 | /* Decode the signed LEB128 constant at BUF into the variable pointed to |
| 175 | by R, and return the new value of BUF. Verify that it doesn't extend |
| 176 | past BUF_END. */ |
| 177 | |
| 178 | gdb_byte * |
| 179 | read_sleb128 (gdb_byte *buf, gdb_byte *buf_end, LONGEST * r) |
| 180 | { |
| 181 | unsigned shift = 0; |
| 182 | LONGEST result = 0; |
| 183 | gdb_byte byte; |
| 184 | |
| 185 | while (1) |
| 186 | { |
| 187 | if (buf >= buf_end) |
| 188 | error (_("read_sleb128: Corrupted DWARF expression.")); |
| 189 | |
| 190 | byte = *buf++; |
| 191 | result |= (byte & 0x7f) << shift; |
| 192 | shift += 7; |
| 193 | if ((byte & 0x80) == 0) |
| 194 | break; |
| 195 | } |
| 196 | if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0) |
| 197 | result |= -(1 << shift); |
| 198 | |
| 199 | *r = result; |
| 200 | return buf; |
| 201 | } |
| 202 | |
| 203 | /* Read an address of size ADDR_SIZE from BUF, and verify that it |
| 204 | doesn't extend past BUF_END. */ |
| 205 | |
| 206 | CORE_ADDR |
| 207 | dwarf2_read_address (gdb_byte *buf, gdb_byte *buf_end, int addr_size) |
| 208 | { |
| 209 | CORE_ADDR result; |
| 210 | |
| 211 | if (buf_end - buf < addr_size) |
| 212 | error (_("dwarf2_read_address: Corrupted DWARF expression.")); |
| 213 | |
| 214 | /* For most architectures, calling extract_unsigned_integer() alone |
| 215 | is sufficient for extracting an address. However, some |
| 216 | architectures (e.g. MIPS) use signed addresses and using |
| 217 | extract_unsigned_integer() will not produce a correct |
| 218 | result. Turning the unsigned integer into a value and then |
| 219 | decomposing that value as an address will cause |
| 220 | gdbarch_integer_to_address() to be invoked for those |
| 221 | architectures which require it. Thus, using value_as_address() |
| 222 | will produce the correct result for both types of architectures. |
| 223 | |
| 224 | One concern regarding the use of values for this purpose is |
| 225 | efficiency. Obviously, these extra calls will take more time to |
| 226 | execute and creating a value takes more space, space which will |
| 227 | have to be garbage collected at a later time. If constructing |
| 228 | and then decomposing a value for this purpose proves to be too |
| 229 | inefficient, then gdbarch_integer_to_address() can be called |
| 230 | directly. |
| 231 | |
| 232 | The use of `unsigned_address_type' in the code below refers to |
| 233 | the type of buf and has no bearing on the signedness of the |
| 234 | address being returned. */ |
| 235 | |
| 236 | result = value_as_address (value_from_longest |
| 237 | (unsigned_address_type (addr_size), |
| 238 | extract_unsigned_integer (buf, addr_size))); |
| 239 | return result; |
| 240 | } |
| 241 | |
| 242 | /* Return the type of an address of size ADDR_SIZE, |
| 243 | for unsigned arithmetic. */ |
| 244 | |
| 245 | static struct type * |
| 246 | unsigned_address_type (int addr_size) |
| 247 | { |
| 248 | switch (addr_size) |
| 249 | { |
| 250 | case 2: |
| 251 | return builtin_type_uint16; |
| 252 | case 4: |
| 253 | return builtin_type_uint32; |
| 254 | case 8: |
| 255 | return builtin_type_uint64; |
| 256 | default: |
| 257 | internal_error (__FILE__, __LINE__, |
| 258 | _("Unsupported address size.\n")); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* Return the type of an address of size ADDR_SIZE, |
| 263 | for signed arithmetic. */ |
| 264 | |
| 265 | static struct type * |
| 266 | signed_address_type (int addr_size) |
| 267 | { |
| 268 | switch (addr_size) |
| 269 | { |
| 270 | case 2: |
| 271 | return builtin_type_int16; |
| 272 | case 4: |
| 273 | return builtin_type_int32; |
| 274 | case 8: |
| 275 | return builtin_type_int64; |
| 276 | default: |
| 277 | internal_error (__FILE__, __LINE__, |
| 278 | _("Unsupported address size.\n")); |
| 279 | } |
| 280 | } |
| 281 | \f |
| 282 | /* The engine for the expression evaluator. Using the context in CTX, |
| 283 | evaluate the expression between OP_PTR and OP_END. */ |
| 284 | |
| 285 | static void |
| 286 | execute_stack_op (struct dwarf_expr_context *ctx, |
| 287 | gdb_byte *op_ptr, gdb_byte *op_end) |
| 288 | { |
| 289 | ctx->in_reg = 0; |
| 290 | ctx->initialized = 1; /* Default is initialized. */ |
| 291 | |
| 292 | if (ctx->recursion_depth > ctx->max_recursion_depth) |
| 293 | error (_("DWARF-2 expression error: Loop detected (%d)."), |
| 294 | ctx->recursion_depth); |
| 295 | ctx->recursion_depth++; |
| 296 | |
| 297 | while (op_ptr < op_end) |
| 298 | { |
| 299 | enum dwarf_location_atom op = *op_ptr++; |
| 300 | CORE_ADDR result; |
| 301 | ULONGEST uoffset, reg; |
| 302 | LONGEST offset; |
| 303 | |
| 304 | switch (op) |
| 305 | { |
| 306 | case DW_OP_lit0: |
| 307 | case DW_OP_lit1: |
| 308 | case DW_OP_lit2: |
| 309 | case DW_OP_lit3: |
| 310 | case DW_OP_lit4: |
| 311 | case DW_OP_lit5: |
| 312 | case DW_OP_lit6: |
| 313 | case DW_OP_lit7: |
| 314 | case DW_OP_lit8: |
| 315 | case DW_OP_lit9: |
| 316 | case DW_OP_lit10: |
| 317 | case DW_OP_lit11: |
| 318 | case DW_OP_lit12: |
| 319 | case DW_OP_lit13: |
| 320 | case DW_OP_lit14: |
| 321 | case DW_OP_lit15: |
| 322 | case DW_OP_lit16: |
| 323 | case DW_OP_lit17: |
| 324 | case DW_OP_lit18: |
| 325 | case DW_OP_lit19: |
| 326 | case DW_OP_lit20: |
| 327 | case DW_OP_lit21: |
| 328 | case DW_OP_lit22: |
| 329 | case DW_OP_lit23: |
| 330 | case DW_OP_lit24: |
| 331 | case DW_OP_lit25: |
| 332 | case DW_OP_lit26: |
| 333 | case DW_OP_lit27: |
| 334 | case DW_OP_lit28: |
| 335 | case DW_OP_lit29: |
| 336 | case DW_OP_lit30: |
| 337 | case DW_OP_lit31: |
| 338 | result = op - DW_OP_lit0; |
| 339 | break; |
| 340 | |
| 341 | case DW_OP_addr: |
| 342 | result = dwarf2_read_address (op_ptr, op_end, ctx->addr_size); |
| 343 | op_ptr += ctx->addr_size; |
| 344 | break; |
| 345 | |
| 346 | case DW_OP_const1u: |
| 347 | result = extract_unsigned_integer (op_ptr, 1); |
| 348 | op_ptr += 1; |
| 349 | break; |
| 350 | case DW_OP_const1s: |
| 351 | result = extract_signed_integer (op_ptr, 1); |
| 352 | op_ptr += 1; |
| 353 | break; |
| 354 | case DW_OP_const2u: |
| 355 | result = extract_unsigned_integer (op_ptr, 2); |
| 356 | op_ptr += 2; |
| 357 | break; |
| 358 | case DW_OP_const2s: |
| 359 | result = extract_signed_integer (op_ptr, 2); |
| 360 | op_ptr += 2; |
| 361 | break; |
| 362 | case DW_OP_const4u: |
| 363 | result = extract_unsigned_integer (op_ptr, 4); |
| 364 | op_ptr += 4; |
| 365 | break; |
| 366 | case DW_OP_const4s: |
| 367 | result = extract_signed_integer (op_ptr, 4); |
| 368 | op_ptr += 4; |
| 369 | break; |
| 370 | case DW_OP_const8u: |
| 371 | result = extract_unsigned_integer (op_ptr, 8); |
| 372 | op_ptr += 8; |
| 373 | break; |
| 374 | case DW_OP_const8s: |
| 375 | result = extract_signed_integer (op_ptr, 8); |
| 376 | op_ptr += 8; |
| 377 | break; |
| 378 | case DW_OP_constu: |
| 379 | op_ptr = read_uleb128 (op_ptr, op_end, &uoffset); |
| 380 | result = uoffset; |
| 381 | break; |
| 382 | case DW_OP_consts: |
| 383 | op_ptr = read_sleb128 (op_ptr, op_end, &offset); |
| 384 | result = offset; |
| 385 | break; |
| 386 | |
| 387 | /* The DW_OP_reg operations are required to occur alone in |
| 388 | location expressions. */ |
| 389 | case DW_OP_reg0: |
| 390 | case DW_OP_reg1: |
| 391 | case DW_OP_reg2: |
| 392 | case DW_OP_reg3: |
| 393 | case DW_OP_reg4: |
| 394 | case DW_OP_reg5: |
| 395 | case DW_OP_reg6: |
| 396 | case DW_OP_reg7: |
| 397 | case DW_OP_reg8: |
| 398 | case DW_OP_reg9: |
| 399 | case DW_OP_reg10: |
| 400 | case DW_OP_reg11: |
| 401 | case DW_OP_reg12: |
| 402 | case DW_OP_reg13: |
| 403 | case DW_OP_reg14: |
| 404 | case DW_OP_reg15: |
| 405 | case DW_OP_reg16: |
| 406 | case DW_OP_reg17: |
| 407 | case DW_OP_reg18: |
| 408 | case DW_OP_reg19: |
| 409 | case DW_OP_reg20: |
| 410 | case DW_OP_reg21: |
| 411 | case DW_OP_reg22: |
| 412 | case DW_OP_reg23: |
| 413 | case DW_OP_reg24: |
| 414 | case DW_OP_reg25: |
| 415 | case DW_OP_reg26: |
| 416 | case DW_OP_reg27: |
| 417 | case DW_OP_reg28: |
| 418 | case DW_OP_reg29: |
| 419 | case DW_OP_reg30: |
| 420 | case DW_OP_reg31: |
| 421 | if (op_ptr != op_end |
| 422 | && *op_ptr != DW_OP_piece |
| 423 | && *op_ptr != DW_OP_GNU_uninit) |
| 424 | error (_("DWARF-2 expression error: DW_OP_reg operations must be " |
| 425 | "used either alone or in conjuction with DW_OP_piece.")); |
| 426 | |
| 427 | result = op - DW_OP_reg0; |
| 428 | ctx->in_reg = 1; |
| 429 | |
| 430 | break; |
| 431 | |
| 432 | case DW_OP_regx: |
| 433 | op_ptr = read_uleb128 (op_ptr, op_end, ®); |
| 434 | if (op_ptr != op_end && *op_ptr != DW_OP_piece) |
| 435 | error (_("DWARF-2 expression error: DW_OP_reg operations must be " |
| 436 | "used either alone or in conjuction with DW_OP_piece.")); |
| 437 | |
| 438 | result = reg; |
| 439 | ctx->in_reg = 1; |
| 440 | break; |
| 441 | |
| 442 | case DW_OP_breg0: |
| 443 | case DW_OP_breg1: |
| 444 | case DW_OP_breg2: |
| 445 | case DW_OP_breg3: |
| 446 | case DW_OP_breg4: |
| 447 | case DW_OP_breg5: |
| 448 | case DW_OP_breg6: |
| 449 | case DW_OP_breg7: |
| 450 | case DW_OP_breg8: |
| 451 | case DW_OP_breg9: |
| 452 | case DW_OP_breg10: |
| 453 | case DW_OP_breg11: |
| 454 | case DW_OP_breg12: |
| 455 | case DW_OP_breg13: |
| 456 | case DW_OP_breg14: |
| 457 | case DW_OP_breg15: |
| 458 | case DW_OP_breg16: |
| 459 | case DW_OP_breg17: |
| 460 | case DW_OP_breg18: |
| 461 | case DW_OP_breg19: |
| 462 | case DW_OP_breg20: |
| 463 | case DW_OP_breg21: |
| 464 | case DW_OP_breg22: |
| 465 | case DW_OP_breg23: |
| 466 | case DW_OP_breg24: |
| 467 | case DW_OP_breg25: |
| 468 | case DW_OP_breg26: |
| 469 | case DW_OP_breg27: |
| 470 | case DW_OP_breg28: |
| 471 | case DW_OP_breg29: |
| 472 | case DW_OP_breg30: |
| 473 | case DW_OP_breg31: |
| 474 | { |
| 475 | op_ptr = read_sleb128 (op_ptr, op_end, &offset); |
| 476 | result = (ctx->read_reg) (ctx->baton, op - DW_OP_breg0); |
| 477 | result += offset; |
| 478 | } |
| 479 | break; |
| 480 | case DW_OP_bregx: |
| 481 | { |
| 482 | op_ptr = read_uleb128 (op_ptr, op_end, ®); |
| 483 | op_ptr = read_sleb128 (op_ptr, op_end, &offset); |
| 484 | result = (ctx->read_reg) (ctx->baton, reg); |
| 485 | result += offset; |
| 486 | } |
| 487 | break; |
| 488 | case DW_OP_fbreg: |
| 489 | { |
| 490 | gdb_byte *datastart; |
| 491 | size_t datalen; |
| 492 | unsigned int before_stack_len; |
| 493 | |
| 494 | op_ptr = read_sleb128 (op_ptr, op_end, &offset); |
| 495 | /* Rather than create a whole new context, we simply |
| 496 | record the stack length before execution, then reset it |
| 497 | afterwards, effectively erasing whatever the recursive |
| 498 | call put there. */ |
| 499 | before_stack_len = ctx->stack_len; |
| 500 | /* FIXME: cagney/2003-03-26: This code should be using |
| 501 | get_frame_base_address(), and then implement a dwarf2 |
| 502 | specific this_base method. */ |
| 503 | (ctx->get_frame_base) (ctx->baton, &datastart, &datalen); |
| 504 | dwarf_expr_eval (ctx, datastart, datalen); |
| 505 | result = dwarf_expr_fetch (ctx, 0); |
| 506 | if (ctx->in_reg) |
| 507 | result = (ctx->read_reg) (ctx->baton, result); |
| 508 | result = result + offset; |
| 509 | ctx->stack_len = before_stack_len; |
| 510 | ctx->in_reg = 0; |
| 511 | } |
| 512 | break; |
| 513 | case DW_OP_dup: |
| 514 | result = dwarf_expr_fetch (ctx, 0); |
| 515 | break; |
| 516 | |
| 517 | case DW_OP_drop: |
| 518 | dwarf_expr_pop (ctx); |
| 519 | goto no_push; |
| 520 | |
| 521 | case DW_OP_pick: |
| 522 | offset = *op_ptr++; |
| 523 | result = dwarf_expr_fetch (ctx, offset); |
| 524 | break; |
| 525 | |
| 526 | case DW_OP_over: |
| 527 | result = dwarf_expr_fetch (ctx, 1); |
| 528 | break; |
| 529 | |
| 530 | case DW_OP_rot: |
| 531 | { |
| 532 | CORE_ADDR t1, t2, t3; |
| 533 | |
| 534 | if (ctx->stack_len < 3) |
| 535 | error (_("Not enough elements for DW_OP_rot. Need 3, have %d."), |
| 536 | ctx->stack_len); |
| 537 | t1 = ctx->stack[ctx->stack_len - 1]; |
| 538 | t2 = ctx->stack[ctx->stack_len - 2]; |
| 539 | t3 = ctx->stack[ctx->stack_len - 3]; |
| 540 | ctx->stack[ctx->stack_len - 1] = t2; |
| 541 | ctx->stack[ctx->stack_len - 2] = t3; |
| 542 | ctx->stack[ctx->stack_len - 3] = t1; |
| 543 | goto no_push; |
| 544 | } |
| 545 | |
| 546 | case DW_OP_deref: |
| 547 | case DW_OP_deref_size: |
| 548 | case DW_OP_abs: |
| 549 | case DW_OP_neg: |
| 550 | case DW_OP_not: |
| 551 | case DW_OP_plus_uconst: |
| 552 | /* Unary operations. */ |
| 553 | result = dwarf_expr_fetch (ctx, 0); |
| 554 | dwarf_expr_pop (ctx); |
| 555 | |
| 556 | switch (op) |
| 557 | { |
| 558 | case DW_OP_deref: |
| 559 | { |
| 560 | gdb_byte *buf = alloca (ctx->addr_size); |
| 561 | (ctx->read_mem) (ctx->baton, buf, result, ctx->addr_size); |
| 562 | result = dwarf2_read_address (buf, buf + ctx->addr_size, |
| 563 | ctx->addr_size); |
| 564 | } |
| 565 | break; |
| 566 | |
| 567 | case DW_OP_deref_size: |
| 568 | { |
| 569 | int addr_size = *op_ptr++; |
| 570 | gdb_byte *buf = alloca (addr_size); |
| 571 | (ctx->read_mem) (ctx->baton, buf, result, addr_size); |
| 572 | result = dwarf2_read_address (buf, buf + addr_size, |
| 573 | addr_size); |
| 574 | } |
| 575 | break; |
| 576 | |
| 577 | case DW_OP_abs: |
| 578 | if ((signed int) result < 0) |
| 579 | result = -result; |
| 580 | break; |
| 581 | case DW_OP_neg: |
| 582 | result = -result; |
| 583 | break; |
| 584 | case DW_OP_not: |
| 585 | result = ~result; |
| 586 | break; |
| 587 | case DW_OP_plus_uconst: |
| 588 | op_ptr = read_uleb128 (op_ptr, op_end, ®); |
| 589 | result += reg; |
| 590 | break; |
| 591 | } |
| 592 | break; |
| 593 | |
| 594 | case DW_OP_and: |
| 595 | case DW_OP_div: |
| 596 | case DW_OP_minus: |
| 597 | case DW_OP_mod: |
| 598 | case DW_OP_mul: |
| 599 | case DW_OP_or: |
| 600 | case DW_OP_plus: |
| 601 | case DW_OP_shl: |
| 602 | case DW_OP_shr: |
| 603 | case DW_OP_shra: |
| 604 | case DW_OP_xor: |
| 605 | case DW_OP_le: |
| 606 | case DW_OP_ge: |
| 607 | case DW_OP_eq: |
| 608 | case DW_OP_lt: |
| 609 | case DW_OP_gt: |
| 610 | case DW_OP_ne: |
| 611 | { |
| 612 | /* Binary operations. Use the value engine to do computations in |
| 613 | the right width. */ |
| 614 | CORE_ADDR first, second; |
| 615 | enum exp_opcode binop; |
| 616 | struct value *val1, *val2; |
| 617 | |
| 618 | second = dwarf_expr_fetch (ctx, 0); |
| 619 | dwarf_expr_pop (ctx); |
| 620 | |
| 621 | first = dwarf_expr_fetch (ctx, 0); |
| 622 | dwarf_expr_pop (ctx); |
| 623 | |
| 624 | val1 = value_from_longest |
| 625 | (unsigned_address_type (ctx->addr_size), first); |
| 626 | val2 = value_from_longest |
| 627 | (unsigned_address_type (ctx->addr_size), second); |
| 628 | |
| 629 | switch (op) |
| 630 | { |
| 631 | case DW_OP_and: |
| 632 | binop = BINOP_BITWISE_AND; |
| 633 | break; |
| 634 | case DW_OP_div: |
| 635 | binop = BINOP_DIV; |
| 636 | break; |
| 637 | case DW_OP_minus: |
| 638 | binop = BINOP_SUB; |
| 639 | break; |
| 640 | case DW_OP_mod: |
| 641 | binop = BINOP_MOD; |
| 642 | break; |
| 643 | case DW_OP_mul: |
| 644 | binop = BINOP_MUL; |
| 645 | break; |
| 646 | case DW_OP_or: |
| 647 | binop = BINOP_BITWISE_IOR; |
| 648 | break; |
| 649 | case DW_OP_plus: |
| 650 | binop = BINOP_ADD; |
| 651 | break; |
| 652 | case DW_OP_shl: |
| 653 | binop = BINOP_LSH; |
| 654 | break; |
| 655 | case DW_OP_shr: |
| 656 | binop = BINOP_RSH; |
| 657 | break; |
| 658 | case DW_OP_shra: |
| 659 | binop = BINOP_RSH; |
| 660 | val1 = value_from_longest |
| 661 | (signed_address_type (ctx->addr_size), first); |
| 662 | break; |
| 663 | case DW_OP_xor: |
| 664 | binop = BINOP_BITWISE_XOR; |
| 665 | break; |
| 666 | case DW_OP_le: |
| 667 | binop = BINOP_LEQ; |
| 668 | break; |
| 669 | case DW_OP_ge: |
| 670 | binop = BINOP_GEQ; |
| 671 | break; |
| 672 | case DW_OP_eq: |
| 673 | binop = BINOP_EQUAL; |
| 674 | break; |
| 675 | case DW_OP_lt: |
| 676 | binop = BINOP_LESS; |
| 677 | break; |
| 678 | case DW_OP_gt: |
| 679 | binop = BINOP_GTR; |
| 680 | break; |
| 681 | case DW_OP_ne: |
| 682 | binop = BINOP_NOTEQUAL; |
| 683 | break; |
| 684 | default: |
| 685 | internal_error (__FILE__, __LINE__, |
| 686 | _("Can't be reached.")); |
| 687 | } |
| 688 | result = value_as_long (value_binop (val1, val2, binop)); |
| 689 | } |
| 690 | break; |
| 691 | |
| 692 | case DW_OP_GNU_push_tls_address: |
| 693 | /* Variable is at a constant offset in the thread-local |
| 694 | storage block into the objfile for the current thread and |
| 695 | the dynamic linker module containing this expression. Here |
| 696 | we return returns the offset from that base. The top of the |
| 697 | stack has the offset from the beginning of the thread |
| 698 | control block at which the variable is located. Nothing |
| 699 | should follow this operator, so the top of stack would be |
| 700 | returned. */ |
| 701 | result = dwarf_expr_fetch (ctx, 0); |
| 702 | dwarf_expr_pop (ctx); |
| 703 | result = (ctx->get_tls_address) (ctx->baton, result); |
| 704 | break; |
| 705 | |
| 706 | case DW_OP_skip: |
| 707 | offset = extract_signed_integer (op_ptr, 2); |
| 708 | op_ptr += 2; |
| 709 | op_ptr += offset; |
| 710 | goto no_push; |
| 711 | |
| 712 | case DW_OP_bra: |
| 713 | offset = extract_signed_integer (op_ptr, 2); |
| 714 | op_ptr += 2; |
| 715 | if (dwarf_expr_fetch (ctx, 0) != 0) |
| 716 | op_ptr += offset; |
| 717 | dwarf_expr_pop (ctx); |
| 718 | goto no_push; |
| 719 | |
| 720 | case DW_OP_nop: |
| 721 | goto no_push; |
| 722 | |
| 723 | case DW_OP_piece: |
| 724 | { |
| 725 | ULONGEST size; |
| 726 | CORE_ADDR addr_or_regnum; |
| 727 | |
| 728 | /* Record the piece. */ |
| 729 | op_ptr = read_uleb128 (op_ptr, op_end, &size); |
| 730 | addr_or_regnum = dwarf_expr_fetch (ctx, 0); |
| 731 | add_piece (ctx, ctx->in_reg, addr_or_regnum, size); |
| 732 | |
| 733 | /* Pop off the address/regnum, and clear the in_reg flag. */ |
| 734 | dwarf_expr_pop (ctx); |
| 735 | ctx->in_reg = 0; |
| 736 | } |
| 737 | goto no_push; |
| 738 | |
| 739 | case DW_OP_GNU_uninit: |
| 740 | if (op_ptr != op_end) |
| 741 | error (_("DWARF-2 expression error: DW_OP_GNU_unint must always " |
| 742 | "be the very last op.")); |
| 743 | |
| 744 | ctx->initialized = 0; |
| 745 | goto no_push; |
| 746 | |
| 747 | default: |
| 748 | error (_("Unhandled dwarf expression opcode 0x%x"), op); |
| 749 | } |
| 750 | |
| 751 | /* Most things push a result value. */ |
| 752 | dwarf_expr_push (ctx, result); |
| 753 | no_push:; |
| 754 | } |
| 755 | |
| 756 | ctx->recursion_depth--; |
| 757 | gdb_assert (ctx->recursion_depth >= 0); |
| 758 | } |