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