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