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