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 
32 static void execute_stack_op (struct dwarf_expr_context *,
33 			      unsigned char *, unsigned char *);
34 
35 /* Create a new context for the expression evaluator.  */
36 
37 struct dwarf_expr_context *
new_dwarf_expr_context(void)38 new_dwarf_expr_context (void)
39 {
40   struct dwarf_expr_context *retval;
41   retval = xcalloc (1, sizeof (struct dwarf_expr_context));
42   retval->stack_len = 0;
43   retval->stack_allocated = 10;
44   retval->stack = xmalloc (retval->stack_allocated * sizeof (CORE_ADDR));
45   return retval;
46 }
47 
48 /* Release the memory allocated to CTX.  */
49 
50 void
free_dwarf_expr_context(struct dwarf_expr_context * ctx)51 free_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 
60 static void
dwarf_expr_grow_stack(struct dwarf_expr_context * ctx,size_t need)61 dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need)
62 {
63   if (ctx->stack_len + need > ctx->stack_allocated)
64     {
65       size_t newlen = ctx->stack_len + need + 10;
66       ctx->stack = xrealloc (ctx->stack,
67 			     newlen * sizeof (CORE_ADDR));
68       ctx->stack_allocated = newlen;
69     }
70 }
71 
72 /* Push VALUE onto CTX's stack.  */
73 
74 void
dwarf_expr_push(struct dwarf_expr_context * ctx,CORE_ADDR value)75 dwarf_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 
83 void
dwarf_expr_pop(struct dwarf_expr_context * ctx)84 dwarf_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 
93 CORE_ADDR
dwarf_expr_fetch(struct dwarf_expr_context * ctx,int n)94 dwarf_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 
106 void
dwarf_expr_eval(struct dwarf_expr_context * ctx,unsigned char * addr,size_t len)107 dwarf_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 
117 unsigned char *
read_uleb128(unsigned char * buf,unsigned char * buf_end,ULONGEST * r)118 read_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 
143 unsigned char *
read_sleb128(unsigned char * buf,unsigned char * buf_end,LONGEST * r)144 read_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 
172 CORE_ADDR
dwarf2_read_address(unsigned char * buf,unsigned char * buf_end,int * bytes_read)173 dwarf2_read_address (unsigned char *buf, unsigned char *buf_end, int *bytes_read)
174 {
175   CORE_ADDR result;
176 
177   if (buf_end - buf < TARGET_ADDR_BIT / TARGET_CHAR_BIT)
178     error ("dwarf2_read_address: Corrupted DWARF expression.");
179 
180   *bytes_read = TARGET_ADDR_BIT / TARGET_CHAR_BIT;
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);
184   return result;
185 }
186 
187 /* Return the type of an address, for unsigned arithmetic.  */
188 
189 static struct type *
unsigned_address_type(void)190 unsigned_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 
208 static struct type *
signed_address_type(void)209 signed_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 
225 /* The engine for the expression evaluator.  Using the context in CTX,
226    evaluate the expression between OP_PTR and OP_END.  */
227 
228 static void
execute_stack_op(struct dwarf_expr_context * ctx,unsigned char * op_ptr,unsigned char * op_end)229 execute_stack_op (struct dwarf_expr_context *ctx, unsigned char *op_ptr,
230 		  unsigned char *op_end)
231 {
232   ctx->in_reg = 0;
233 
234   while (op_ptr < op_end)
235     {
236       enum dwarf_location_atom op = *op_ptr++;
237       CORE_ADDR result;
238       ULONGEST uoffset, reg;
239       LONGEST offset;
240       int bytes_read;
241 
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:
280 	  result = dwarf2_read_address (op_ptr, op_end, &bytes_read);
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:
359 	  if (op_ptr != op_end && *op_ptr != DW_OP_piece)
360 	    error ("DWARF-2 expression error: DW_OP_reg operations must be "
361 		   "used either alone or in conjuction with DW_OP_piece.");
362 
363 	  result = op - DW_OP_reg0;
364 	  ctx->in_reg = 1;
365 
366 	  break;
367 
368 	case DW_OP_regx:
369 	  op_ptr = read_uleb128 (op_ptr, op_end, &reg);
370 	  if (op_ptr != op_end && *op_ptr != DW_OP_piece)
371 	    error ("DWARF-2 expression error: DW_OP_reg operations must be "
372 		   "used either alone or in conjuction with DW_OP_piece.");
373 
374 	  result = reg;
375 	  ctx->in_reg = 1;
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);
412 	    result = (ctx->read_reg) (ctx->baton, op - DW_OP_breg0);
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);
420 	    result = (ctx->read_reg) (ctx->baton, reg);
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;
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.  */
439 	    (ctx->get_frame_base) (ctx->baton, &datastart, &datalen);
440 	    dwarf_expr_eval (ctx, datastart, datalen);
441 	    result = dwarf_expr_fetch (ctx, 0);
442 	    if (ctx->in_reg)
443 	      result = (ctx->read_reg) (ctx->baton, result);
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);
501 		result = dwarf2_read_address (buf,
502 					      buf + (TARGET_ADDR_BIT
503 						     / TARGET_CHAR_BIT),
504 					      &bytes_read);
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++);
514 		result = dwarf2_read_address (buf,
515 					      buf + (TARGET_ADDR_BIT
516 						     / TARGET_CHAR_BIT),
517 					      &bytes_read);
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 
565 	    first = dwarf_expr_fetch (ctx, 0);
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                 break;
579 	      case DW_OP_minus:
580 		binop = BINOP_SUB;
581 		break;
582 	      case DW_OP_mod:
583 		binop = BINOP_MOD;
584 		break;
585 	      case DW_OP_mul:
586 		binop = BINOP_MUL;
587 		break;
588 	      case DW_OP_or:
589 		binop = BINOP_BITWISE_IOR;
590 		break;
591 	      case DW_OP_plus:
592 		binop = BINOP_ADD;
593 		break;
594 	      case DW_OP_shl:
595 		binop = BINOP_LSH;
596 		break;
597 	      case DW_OP_shr:
598 		binop = BINOP_RSH;
599                 break;
600 	      case DW_OP_shra:
601 		binop = BINOP_RSH;
602 		val1 = value_from_longest (signed_address_type (), first);
603 		break;
604 	      case DW_OP_xor:
605 		binop = BINOP_BITWISE_XOR;
606 		break;
607 	      case DW_OP_le:
608 		binop = BINOP_LEQ;
609 		break;
610 	      case DW_OP_ge:
611 		binop = BINOP_GEQ;
612 		break;
613 	      case DW_OP_eq:
614 		binop = BINOP_EQUAL;
615 		break;
616 	      case DW_OP_lt:
617 		binop = BINOP_LESS;
618 		break;
619 	      case DW_OP_gt:
620 		binop = BINOP_GTR;
621 		break;
622 	      case DW_OP_ne:
623 		binop = BINOP_NOTEQUAL;
624 		break;
625 	      default:
626 		internal_error (__FILE__, __LINE__,
627 				"Can't be reached.");
628 	      }
629 	    result = value_as_long (value_binop (val1, val2, binop));
630 	  }
631 	  break;
632 
633 	case DW_OP_GNU_push_tls_address:
634 	  /* Variable is at a constant offset in the thread-local
635 	  storage block into the objfile for the current thread and
636 	  the dynamic linker module containing this expression. Here
637 	  we return returns the offset from that base.  The top of the
638 	  stack has the offset from the beginning of the thread
639 	  control block at which the variable is located.  Nothing
640 	  should follow this operator, so the top of stack would be
641 	  returned.  */
642 	  result = dwarf_expr_fetch (ctx, 0);
643 	  dwarf_expr_pop (ctx);
644 	  result = (ctx->get_tls_address) (ctx->baton, result);
645 	  break;
646 
647 	case DW_OP_skip:
648 	  offset = extract_signed_integer (op_ptr, 2);
649 	  op_ptr += 2;
650 	  op_ptr += offset;
651 	  goto no_push;
652 
653 	case DW_OP_bra:
654 	  offset = extract_signed_integer (op_ptr, 2);
655 	  op_ptr += 2;
656 	  if (dwarf_expr_fetch (ctx, 0) != 0)
657 	    op_ptr += offset;
658 	  dwarf_expr_pop (ctx);
659 	  goto no_push;
660 
661 	case DW_OP_nop:
662 	  goto no_push;
663 
664 	default:
665 	  error ("Unhandled dwarf expression opcode 0x%x", op);
666 	}
667 
668       /* Most things push a result value.  */
669       dwarf_expr_push (ctx, result);
670     no_push:;
671     }
672 }
673