xref: /dflybsd-src/contrib/gdb-7/gdb/dwarf2expr.c (revision c50c785cb49e9377ca78104c5540c7b33f768771)
15796c8dcSSimon Schubert /* DWARF 2 Expression Evaluator.
25796c8dcSSimon Schubert 
3*c50c785cSJohn Marino    Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2010, 2011
45796c8dcSSimon Schubert    Free Software Foundation, Inc.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    Contributed by Daniel Berlin (dan@dberlin.org)
75796c8dcSSimon Schubert 
85796c8dcSSimon Schubert    This file is part of GDB.
95796c8dcSSimon Schubert 
105796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
115796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
125796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
135796c8dcSSimon Schubert    (at your option) any later version.
145796c8dcSSimon Schubert 
155796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
165796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
175796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
185796c8dcSSimon Schubert    GNU General Public License for more details.
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
215796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
225796c8dcSSimon Schubert 
235796c8dcSSimon Schubert #include "defs.h"
245796c8dcSSimon Schubert #include "symtab.h"
255796c8dcSSimon Schubert #include "gdbtypes.h"
265796c8dcSSimon Schubert #include "value.h"
275796c8dcSSimon Schubert #include "gdbcore.h"
285796c8dcSSimon Schubert #include "dwarf2.h"
295796c8dcSSimon Schubert #include "dwarf2expr.h"
305796c8dcSSimon Schubert #include "gdb_assert.h"
315796c8dcSSimon Schubert 
325796c8dcSSimon Schubert /* Local prototypes.  */
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert static void execute_stack_op (struct dwarf_expr_context *,
35cf7f2e2dSJohn Marino 			      const gdb_byte *, const gdb_byte *);
365796c8dcSSimon Schubert 
375796c8dcSSimon Schubert /* Create a new context for the expression evaluator.  */
385796c8dcSSimon Schubert 
395796c8dcSSimon Schubert struct dwarf_expr_context *
405796c8dcSSimon Schubert new_dwarf_expr_context (void)
415796c8dcSSimon Schubert {
425796c8dcSSimon Schubert   struct dwarf_expr_context *retval;
43cf7f2e2dSJohn Marino 
445796c8dcSSimon Schubert   retval = xcalloc (1, sizeof (struct dwarf_expr_context));
455796c8dcSSimon Schubert   retval->stack_len = 0;
465796c8dcSSimon Schubert   retval->stack_allocated = 10;
47cf7f2e2dSJohn Marino   retval->stack = xmalloc (retval->stack_allocated
48cf7f2e2dSJohn Marino 			   * sizeof (struct dwarf_stack_value));
495796c8dcSSimon Schubert   retval->num_pieces = 0;
505796c8dcSSimon Schubert   retval->pieces = 0;
515796c8dcSSimon Schubert   retval->max_recursion_depth = 0x100;
525796c8dcSSimon Schubert   return retval;
535796c8dcSSimon Schubert }
545796c8dcSSimon Schubert 
555796c8dcSSimon Schubert /* Release the memory allocated to CTX.  */
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert void
585796c8dcSSimon Schubert free_dwarf_expr_context (struct dwarf_expr_context *ctx)
595796c8dcSSimon Schubert {
605796c8dcSSimon Schubert   xfree (ctx->stack);
615796c8dcSSimon Schubert   xfree (ctx->pieces);
625796c8dcSSimon Schubert   xfree (ctx);
635796c8dcSSimon Schubert }
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert /* Helper for make_cleanup_free_dwarf_expr_context.  */
665796c8dcSSimon Schubert 
675796c8dcSSimon Schubert static void
685796c8dcSSimon Schubert free_dwarf_expr_context_cleanup (void *arg)
695796c8dcSSimon Schubert {
705796c8dcSSimon Schubert   free_dwarf_expr_context (arg);
715796c8dcSSimon Schubert }
725796c8dcSSimon Schubert 
735796c8dcSSimon Schubert /* Return a cleanup that calls free_dwarf_expr_context.  */
745796c8dcSSimon Schubert 
755796c8dcSSimon Schubert struct cleanup *
765796c8dcSSimon Schubert make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context *ctx)
775796c8dcSSimon Schubert {
785796c8dcSSimon Schubert   return make_cleanup (free_dwarf_expr_context_cleanup, ctx);
795796c8dcSSimon Schubert }
805796c8dcSSimon Schubert 
815796c8dcSSimon Schubert /* Expand the memory allocated to CTX's stack to contain at least
825796c8dcSSimon Schubert    NEED more elements than are currently used.  */
835796c8dcSSimon Schubert 
845796c8dcSSimon Schubert static void
855796c8dcSSimon Schubert dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need)
865796c8dcSSimon Schubert {
875796c8dcSSimon Schubert   if (ctx->stack_len + need > ctx->stack_allocated)
885796c8dcSSimon Schubert     {
895796c8dcSSimon Schubert       size_t newlen = ctx->stack_len + need + 10;
90cf7f2e2dSJohn Marino 
915796c8dcSSimon Schubert       ctx->stack = xrealloc (ctx->stack,
925796c8dcSSimon Schubert 			     newlen * sizeof (struct dwarf_stack_value));
935796c8dcSSimon Schubert       ctx->stack_allocated = newlen;
945796c8dcSSimon Schubert     }
955796c8dcSSimon Schubert }
965796c8dcSSimon Schubert 
975796c8dcSSimon Schubert /* Push VALUE onto CTX's stack.  */
985796c8dcSSimon Schubert 
995796c8dcSSimon Schubert void
100cf7f2e2dSJohn Marino dwarf_expr_push (struct dwarf_expr_context *ctx, ULONGEST value,
1015796c8dcSSimon Schubert 		 int in_stack_memory)
1025796c8dcSSimon Schubert {
1035796c8dcSSimon Schubert   struct dwarf_stack_value *v;
1045796c8dcSSimon Schubert 
105cf7f2e2dSJohn Marino   /* We keep all stack elements within the range defined by the
106cf7f2e2dSJohn Marino      DWARF address size.  */
107cf7f2e2dSJohn Marino   if (ctx->addr_size < sizeof (ULONGEST))
108cf7f2e2dSJohn Marino     value &= ((ULONGEST) 1 << (ctx->addr_size * HOST_CHAR_BIT)) - 1;
109cf7f2e2dSJohn Marino 
1105796c8dcSSimon Schubert   dwarf_expr_grow_stack (ctx, 1);
1115796c8dcSSimon Schubert   v = &ctx->stack[ctx->stack_len++];
1125796c8dcSSimon Schubert   v->value = value;
1135796c8dcSSimon Schubert   v->in_stack_memory = in_stack_memory;
1145796c8dcSSimon Schubert }
1155796c8dcSSimon Schubert 
1165796c8dcSSimon Schubert /* Pop the top item off of CTX's stack.  */
1175796c8dcSSimon Schubert 
1185796c8dcSSimon Schubert void
1195796c8dcSSimon Schubert dwarf_expr_pop (struct dwarf_expr_context *ctx)
1205796c8dcSSimon Schubert {
1215796c8dcSSimon Schubert   if (ctx->stack_len <= 0)
1225796c8dcSSimon Schubert     error (_("dwarf expression stack underflow"));
1235796c8dcSSimon Schubert   ctx->stack_len--;
1245796c8dcSSimon Schubert }
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert /* Retrieve the N'th item on CTX's stack.  */
1275796c8dcSSimon Schubert 
128cf7f2e2dSJohn Marino ULONGEST
1295796c8dcSSimon Schubert dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n)
1305796c8dcSSimon Schubert {
1315796c8dcSSimon Schubert   if (ctx->stack_len <= n)
132*c50c785cSJohn Marino      error (_("Asked for position %d of stack, "
133*c50c785cSJohn Marino 	      "stack only has %d elements on it."),
1345796c8dcSSimon Schubert 	    n, ctx->stack_len);
1355796c8dcSSimon Schubert   return ctx->stack[ctx->stack_len - (1 + n)].value;
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert }
1385796c8dcSSimon Schubert 
139cf7f2e2dSJohn Marino /* Retrieve the N'th item on CTX's stack, converted to an address.  */
140cf7f2e2dSJohn Marino 
141cf7f2e2dSJohn Marino CORE_ADDR
142cf7f2e2dSJohn Marino dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n)
143cf7f2e2dSJohn Marino {
144cf7f2e2dSJohn Marino   ULONGEST result = dwarf_expr_fetch (ctx, n);
145cf7f2e2dSJohn Marino 
146cf7f2e2dSJohn Marino   /* For most architectures, calling extract_unsigned_integer() alone
147cf7f2e2dSJohn Marino      is sufficient for extracting an address.  However, some
148cf7f2e2dSJohn Marino      architectures (e.g. MIPS) use signed addresses and using
149cf7f2e2dSJohn Marino      extract_unsigned_integer() will not produce a correct
150cf7f2e2dSJohn Marino      result.  Make sure we invoke gdbarch_integer_to_address()
151cf7f2e2dSJohn Marino      for those architectures which require it.  */
152cf7f2e2dSJohn Marino   if (gdbarch_integer_to_address_p (ctx->gdbarch))
153cf7f2e2dSJohn Marino     {
154cf7f2e2dSJohn Marino       enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
155cf7f2e2dSJohn Marino       gdb_byte *buf = alloca (ctx->addr_size);
156cf7f2e2dSJohn Marino       struct type *int_type;
157cf7f2e2dSJohn Marino 
158cf7f2e2dSJohn Marino       switch (ctx->addr_size)
159cf7f2e2dSJohn Marino 	{
160cf7f2e2dSJohn Marino 	case 2:
161cf7f2e2dSJohn Marino 	  int_type = builtin_type (ctx->gdbarch)->builtin_uint16;
162cf7f2e2dSJohn Marino 	  break;
163cf7f2e2dSJohn Marino 	case 4:
164cf7f2e2dSJohn Marino 	  int_type = builtin_type (ctx->gdbarch)->builtin_uint32;
165cf7f2e2dSJohn Marino 	  break;
166cf7f2e2dSJohn Marino 	case 8:
167cf7f2e2dSJohn Marino 	  int_type = builtin_type (ctx->gdbarch)->builtin_uint64;
168cf7f2e2dSJohn Marino 	  break;
169cf7f2e2dSJohn Marino 	default:
170cf7f2e2dSJohn Marino 	  internal_error (__FILE__, __LINE__,
171cf7f2e2dSJohn Marino 			  _("Unsupported address size.\n"));
172cf7f2e2dSJohn Marino 	}
173cf7f2e2dSJohn Marino 
174cf7f2e2dSJohn Marino       store_unsigned_integer (buf, ctx->addr_size, byte_order, result);
175cf7f2e2dSJohn Marino       return gdbarch_integer_to_address (ctx->gdbarch, int_type, buf);
176cf7f2e2dSJohn Marino     }
177cf7f2e2dSJohn Marino 
178cf7f2e2dSJohn Marino   return (CORE_ADDR) result;
179cf7f2e2dSJohn Marino }
180cf7f2e2dSJohn Marino 
1815796c8dcSSimon Schubert /* Retrieve the in_stack_memory flag of the N'th item on CTX's stack.  */
1825796c8dcSSimon Schubert 
1835796c8dcSSimon Schubert int
1845796c8dcSSimon Schubert dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n)
1855796c8dcSSimon Schubert {
1865796c8dcSSimon Schubert   if (ctx->stack_len <= n)
187*c50c785cSJohn Marino      error (_("Asked for position %d of stack, "
188*c50c785cSJohn Marino 	      "stack only has %d elements on it."),
1895796c8dcSSimon Schubert 	    n, ctx->stack_len);
1905796c8dcSSimon Schubert   return ctx->stack[ctx->stack_len - (1 + n)].in_stack_memory;
1915796c8dcSSimon Schubert 
1925796c8dcSSimon Schubert }
1935796c8dcSSimon Schubert 
194cf7f2e2dSJohn Marino /* Return true if the expression stack is empty.  */
195cf7f2e2dSJohn Marino 
196cf7f2e2dSJohn Marino static int
197cf7f2e2dSJohn Marino dwarf_expr_stack_empty_p (struct dwarf_expr_context *ctx)
198cf7f2e2dSJohn Marino {
199cf7f2e2dSJohn Marino   return ctx->stack_len == 0;
200cf7f2e2dSJohn Marino }
201cf7f2e2dSJohn Marino 
2025796c8dcSSimon Schubert /* Add a new piece to CTX's piece list.  */
2035796c8dcSSimon Schubert static void
204cf7f2e2dSJohn Marino add_piece (struct dwarf_expr_context *ctx, ULONGEST size, ULONGEST offset)
2055796c8dcSSimon Schubert {
2065796c8dcSSimon Schubert   struct dwarf_expr_piece *p;
2075796c8dcSSimon Schubert 
2085796c8dcSSimon Schubert   ctx->num_pieces++;
2095796c8dcSSimon Schubert 
2105796c8dcSSimon Schubert   ctx->pieces = xrealloc (ctx->pieces,
2115796c8dcSSimon Schubert 			  (ctx->num_pieces
2125796c8dcSSimon Schubert 			   * sizeof (struct dwarf_expr_piece)));
2135796c8dcSSimon Schubert 
2145796c8dcSSimon Schubert   p = &ctx->pieces[ctx->num_pieces - 1];
2155796c8dcSSimon Schubert   p->location = ctx->location;
2165796c8dcSSimon Schubert   p->size = size;
217cf7f2e2dSJohn Marino   p->offset = offset;
218cf7f2e2dSJohn Marino 
2195796c8dcSSimon Schubert   if (p->location == DWARF_VALUE_LITERAL)
2205796c8dcSSimon Schubert     {
2215796c8dcSSimon Schubert       p->v.literal.data = ctx->data;
2225796c8dcSSimon Schubert       p->v.literal.length = ctx->len;
2235796c8dcSSimon Schubert     }
224cf7f2e2dSJohn Marino   else if (dwarf_expr_stack_empty_p (ctx))
225cf7f2e2dSJohn Marino     {
226cf7f2e2dSJohn Marino       p->location = DWARF_VALUE_OPTIMIZED_OUT;
227cf7f2e2dSJohn Marino       /* Also reset the context's location, for our callers.  This is
228cf7f2e2dSJohn Marino 	 a somewhat strange approach, but this lets us avoid setting
229cf7f2e2dSJohn Marino 	 the location to DWARF_VALUE_MEMORY in all the individual
230cf7f2e2dSJohn Marino 	 cases in the evaluator.  */
231cf7f2e2dSJohn Marino       ctx->location = DWARF_VALUE_OPTIMIZED_OUT;
232cf7f2e2dSJohn Marino     }
233cf7f2e2dSJohn Marino   else if (p->location == DWARF_VALUE_MEMORY)
234cf7f2e2dSJohn Marino     {
235cf7f2e2dSJohn Marino       p->v.mem.addr = dwarf_expr_fetch_address (ctx, 0);
236cf7f2e2dSJohn Marino       p->v.mem.in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
237cf7f2e2dSJohn Marino     }
238*c50c785cSJohn Marino   else if (p->location == DWARF_VALUE_IMPLICIT_POINTER)
239*c50c785cSJohn Marino     {
240*c50c785cSJohn Marino       p->v.ptr.die = ctx->len;
241*c50c785cSJohn Marino       p->v.ptr.offset = (LONGEST) dwarf_expr_fetch (ctx, 0);
242*c50c785cSJohn Marino     }
2435796c8dcSSimon Schubert   else
2445796c8dcSSimon Schubert     {
245cf7f2e2dSJohn Marino       p->v.value = dwarf_expr_fetch (ctx, 0);
2465796c8dcSSimon Schubert     }
2475796c8dcSSimon Schubert }
2485796c8dcSSimon Schubert 
2495796c8dcSSimon Schubert /* Evaluate the expression at ADDR (LEN bytes long) using the context
2505796c8dcSSimon Schubert    CTX.  */
2515796c8dcSSimon Schubert 
2525796c8dcSSimon Schubert void
253cf7f2e2dSJohn Marino dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr,
254cf7f2e2dSJohn Marino 		 size_t len)
2555796c8dcSSimon Schubert {
2565796c8dcSSimon Schubert   int old_recursion_depth = ctx->recursion_depth;
2575796c8dcSSimon Schubert 
2585796c8dcSSimon Schubert   execute_stack_op (ctx, addr, addr + len);
2595796c8dcSSimon Schubert 
2605796c8dcSSimon Schubert   /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here.  */
2615796c8dcSSimon Schubert 
2625796c8dcSSimon Schubert   gdb_assert (ctx->recursion_depth == old_recursion_depth);
2635796c8dcSSimon Schubert }
2645796c8dcSSimon Schubert 
2655796c8dcSSimon Schubert /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
2665796c8dcSSimon Schubert    by R, and return the new value of BUF.  Verify that it doesn't extend
2675796c8dcSSimon Schubert    past BUF_END.  */
2685796c8dcSSimon Schubert 
269cf7f2e2dSJohn Marino const gdb_byte *
270cf7f2e2dSJohn Marino read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end, ULONGEST * r)
2715796c8dcSSimon Schubert {
2725796c8dcSSimon Schubert   unsigned shift = 0;
2735796c8dcSSimon Schubert   ULONGEST result = 0;
2745796c8dcSSimon Schubert   gdb_byte byte;
2755796c8dcSSimon Schubert 
2765796c8dcSSimon Schubert   while (1)
2775796c8dcSSimon Schubert     {
2785796c8dcSSimon Schubert       if (buf >= buf_end)
2795796c8dcSSimon Schubert 	error (_("read_uleb128: Corrupted DWARF expression."));
2805796c8dcSSimon Schubert 
2815796c8dcSSimon Schubert       byte = *buf++;
282*c50c785cSJohn Marino       result |= ((ULONGEST) (byte & 0x7f)) << shift;
2835796c8dcSSimon Schubert       if ((byte & 0x80) == 0)
2845796c8dcSSimon Schubert 	break;
2855796c8dcSSimon Schubert       shift += 7;
2865796c8dcSSimon Schubert     }
2875796c8dcSSimon Schubert   *r = result;
2885796c8dcSSimon Schubert   return buf;
2895796c8dcSSimon Schubert }
2905796c8dcSSimon Schubert 
2915796c8dcSSimon Schubert /* Decode the signed LEB128 constant at BUF into the variable pointed to
2925796c8dcSSimon Schubert    by R, and return the new value of BUF.  Verify that it doesn't extend
2935796c8dcSSimon Schubert    past BUF_END.  */
2945796c8dcSSimon Schubert 
295cf7f2e2dSJohn Marino const gdb_byte *
296cf7f2e2dSJohn Marino read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end, LONGEST * r)
2975796c8dcSSimon Schubert {
2985796c8dcSSimon Schubert   unsigned shift = 0;
2995796c8dcSSimon Schubert   LONGEST result = 0;
3005796c8dcSSimon Schubert   gdb_byte byte;
3015796c8dcSSimon Schubert 
3025796c8dcSSimon Schubert   while (1)
3035796c8dcSSimon Schubert     {
3045796c8dcSSimon Schubert       if (buf >= buf_end)
3055796c8dcSSimon Schubert 	error (_("read_sleb128: Corrupted DWARF expression."));
3065796c8dcSSimon Schubert 
3075796c8dcSSimon Schubert       byte = *buf++;
308*c50c785cSJohn Marino       result |= ((ULONGEST) (byte & 0x7f)) << shift;
3095796c8dcSSimon Schubert       shift += 7;
3105796c8dcSSimon Schubert       if ((byte & 0x80) == 0)
3115796c8dcSSimon Schubert 	break;
3125796c8dcSSimon Schubert     }
3135796c8dcSSimon Schubert   if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
3145796c8dcSSimon Schubert     result |= -(1 << shift);
3155796c8dcSSimon Schubert 
3165796c8dcSSimon Schubert   *r = result;
3175796c8dcSSimon Schubert   return buf;
3185796c8dcSSimon Schubert }
3195796c8dcSSimon Schubert 
3205796c8dcSSimon Schubert 
3215796c8dcSSimon Schubert /* Check that the current operator is either at the end of an
3225796c8dcSSimon Schubert    expression, or that it is followed by a composition operator.  */
3235796c8dcSSimon Schubert 
324cf7f2e2dSJohn Marino void
325cf7f2e2dSJohn Marino dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
326cf7f2e2dSJohn Marino 				const char *op_name)
3275796c8dcSSimon Schubert {
3285796c8dcSSimon Schubert   /* It seems like DW_OP_GNU_uninit should be handled here.  However,
3295796c8dcSSimon Schubert      it doesn't seem to make sense for DW_OP_*_value, and it was not
3305796c8dcSSimon Schubert      checked at the other place that this function is called.  */
3315796c8dcSSimon Schubert   if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece)
3325796c8dcSSimon Schubert     error (_("DWARF-2 expression error: `%s' operations must be "
3335796c8dcSSimon Schubert 	     "used either alone or in conjuction with DW_OP_piece "
3345796c8dcSSimon Schubert 	     "or DW_OP_bit_piece."),
3355796c8dcSSimon Schubert 	   op_name);
3365796c8dcSSimon Schubert }
3375796c8dcSSimon Schubert 
3385796c8dcSSimon Schubert /* The engine for the expression evaluator.  Using the context in CTX,
3395796c8dcSSimon Schubert    evaluate the expression between OP_PTR and OP_END.  */
3405796c8dcSSimon Schubert 
3415796c8dcSSimon Schubert static void
3425796c8dcSSimon Schubert execute_stack_op (struct dwarf_expr_context *ctx,
343cf7f2e2dSJohn Marino 		  const gdb_byte *op_ptr, const gdb_byte *op_end)
3445796c8dcSSimon Schubert {
345cf7f2e2dSJohn Marino #define sign_ext(x) ((LONGEST) (((x) ^ sign_bit) - sign_bit))
346cf7f2e2dSJohn Marino   ULONGEST sign_bit = (ctx->addr_size >= sizeof (ULONGEST) ? 0
347cf7f2e2dSJohn Marino 		       : ((ULONGEST) 1) << (ctx->addr_size * 8 - 1));
3485796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
349cf7f2e2dSJohn Marino 
3505796c8dcSSimon Schubert   ctx->location = DWARF_VALUE_MEMORY;
3515796c8dcSSimon Schubert   ctx->initialized = 1;  /* Default is initialized.  */
3525796c8dcSSimon Schubert 
3535796c8dcSSimon Schubert   if (ctx->recursion_depth > ctx->max_recursion_depth)
3545796c8dcSSimon Schubert     error (_("DWARF-2 expression error: Loop detected (%d)."),
3555796c8dcSSimon Schubert 	   ctx->recursion_depth);
3565796c8dcSSimon Schubert   ctx->recursion_depth++;
3575796c8dcSSimon Schubert 
3585796c8dcSSimon Schubert   while (op_ptr < op_end)
3595796c8dcSSimon Schubert     {
3605796c8dcSSimon Schubert       enum dwarf_location_atom op = *op_ptr++;
361cf7f2e2dSJohn Marino       ULONGEST result;
3625796c8dcSSimon Schubert       /* Assume the value is not in stack memory.
3635796c8dcSSimon Schubert 	 Code that knows otherwise sets this to 1.
3645796c8dcSSimon Schubert 	 Some arithmetic on stack addresses can probably be assumed to still
3655796c8dcSSimon Schubert 	 be a stack address, but we skip this complication for now.
3665796c8dcSSimon Schubert 	 This is just an optimization, so it's always ok to punt
3675796c8dcSSimon Schubert 	 and leave this as 0.  */
3685796c8dcSSimon Schubert       int in_stack_memory = 0;
3695796c8dcSSimon Schubert       ULONGEST uoffset, reg;
3705796c8dcSSimon Schubert       LONGEST offset;
3715796c8dcSSimon Schubert 
3725796c8dcSSimon Schubert       switch (op)
3735796c8dcSSimon Schubert 	{
3745796c8dcSSimon Schubert 	case DW_OP_lit0:
3755796c8dcSSimon Schubert 	case DW_OP_lit1:
3765796c8dcSSimon Schubert 	case DW_OP_lit2:
3775796c8dcSSimon Schubert 	case DW_OP_lit3:
3785796c8dcSSimon Schubert 	case DW_OP_lit4:
3795796c8dcSSimon Schubert 	case DW_OP_lit5:
3805796c8dcSSimon Schubert 	case DW_OP_lit6:
3815796c8dcSSimon Schubert 	case DW_OP_lit7:
3825796c8dcSSimon Schubert 	case DW_OP_lit8:
3835796c8dcSSimon Schubert 	case DW_OP_lit9:
3845796c8dcSSimon Schubert 	case DW_OP_lit10:
3855796c8dcSSimon Schubert 	case DW_OP_lit11:
3865796c8dcSSimon Schubert 	case DW_OP_lit12:
3875796c8dcSSimon Schubert 	case DW_OP_lit13:
3885796c8dcSSimon Schubert 	case DW_OP_lit14:
3895796c8dcSSimon Schubert 	case DW_OP_lit15:
3905796c8dcSSimon Schubert 	case DW_OP_lit16:
3915796c8dcSSimon Schubert 	case DW_OP_lit17:
3925796c8dcSSimon Schubert 	case DW_OP_lit18:
3935796c8dcSSimon Schubert 	case DW_OP_lit19:
3945796c8dcSSimon Schubert 	case DW_OP_lit20:
3955796c8dcSSimon Schubert 	case DW_OP_lit21:
3965796c8dcSSimon Schubert 	case DW_OP_lit22:
3975796c8dcSSimon Schubert 	case DW_OP_lit23:
3985796c8dcSSimon Schubert 	case DW_OP_lit24:
3995796c8dcSSimon Schubert 	case DW_OP_lit25:
4005796c8dcSSimon Schubert 	case DW_OP_lit26:
4015796c8dcSSimon Schubert 	case DW_OP_lit27:
4025796c8dcSSimon Schubert 	case DW_OP_lit28:
4035796c8dcSSimon Schubert 	case DW_OP_lit29:
4045796c8dcSSimon Schubert 	case DW_OP_lit30:
4055796c8dcSSimon Schubert 	case DW_OP_lit31:
4065796c8dcSSimon Schubert 	  result = op - DW_OP_lit0;
4075796c8dcSSimon Schubert 	  break;
4085796c8dcSSimon Schubert 
4095796c8dcSSimon Schubert 	case DW_OP_addr:
410cf7f2e2dSJohn Marino 	  result = extract_unsigned_integer (op_ptr,
411cf7f2e2dSJohn Marino 					     ctx->addr_size, byte_order);
4125796c8dcSSimon Schubert 	  op_ptr += ctx->addr_size;
413cf7f2e2dSJohn Marino 	  /* Some versions of GCC emit DW_OP_addr before
414cf7f2e2dSJohn Marino 	     DW_OP_GNU_push_tls_address.  In this case the value is an
415cf7f2e2dSJohn Marino 	     index, not an address.  We don't support things like
416cf7f2e2dSJohn Marino 	     branching between the address and the TLS op.  */
417cf7f2e2dSJohn Marino 	  if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
418cf7f2e2dSJohn Marino 	    result += ctx->offset;
4195796c8dcSSimon Schubert 	  break;
4205796c8dcSSimon Schubert 
4215796c8dcSSimon Schubert 	case DW_OP_const1u:
4225796c8dcSSimon Schubert 	  result = extract_unsigned_integer (op_ptr, 1, byte_order);
4235796c8dcSSimon Schubert 	  op_ptr += 1;
4245796c8dcSSimon Schubert 	  break;
4255796c8dcSSimon Schubert 	case DW_OP_const1s:
4265796c8dcSSimon Schubert 	  result = extract_signed_integer (op_ptr, 1, byte_order);
4275796c8dcSSimon Schubert 	  op_ptr += 1;
4285796c8dcSSimon Schubert 	  break;
4295796c8dcSSimon Schubert 	case DW_OP_const2u:
4305796c8dcSSimon Schubert 	  result = extract_unsigned_integer (op_ptr, 2, byte_order);
4315796c8dcSSimon Schubert 	  op_ptr += 2;
4325796c8dcSSimon Schubert 	  break;
4335796c8dcSSimon Schubert 	case DW_OP_const2s:
4345796c8dcSSimon Schubert 	  result = extract_signed_integer (op_ptr, 2, byte_order);
4355796c8dcSSimon Schubert 	  op_ptr += 2;
4365796c8dcSSimon Schubert 	  break;
4375796c8dcSSimon Schubert 	case DW_OP_const4u:
4385796c8dcSSimon Schubert 	  result = extract_unsigned_integer (op_ptr, 4, byte_order);
4395796c8dcSSimon Schubert 	  op_ptr += 4;
4405796c8dcSSimon Schubert 	  break;
4415796c8dcSSimon Schubert 	case DW_OP_const4s:
4425796c8dcSSimon Schubert 	  result = extract_signed_integer (op_ptr, 4, byte_order);
4435796c8dcSSimon Schubert 	  op_ptr += 4;
4445796c8dcSSimon Schubert 	  break;
4455796c8dcSSimon Schubert 	case DW_OP_const8u:
4465796c8dcSSimon Schubert 	  result = extract_unsigned_integer (op_ptr, 8, byte_order);
4475796c8dcSSimon Schubert 	  op_ptr += 8;
4485796c8dcSSimon Schubert 	  break;
4495796c8dcSSimon Schubert 	case DW_OP_const8s:
4505796c8dcSSimon Schubert 	  result = extract_signed_integer (op_ptr, 8, byte_order);
4515796c8dcSSimon Schubert 	  op_ptr += 8;
4525796c8dcSSimon Schubert 	  break;
4535796c8dcSSimon Schubert 	case DW_OP_constu:
4545796c8dcSSimon Schubert 	  op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
4555796c8dcSSimon Schubert 	  result = uoffset;
4565796c8dcSSimon Schubert 	  break;
4575796c8dcSSimon Schubert 	case DW_OP_consts:
4585796c8dcSSimon Schubert 	  op_ptr = read_sleb128 (op_ptr, op_end, &offset);
4595796c8dcSSimon Schubert 	  result = offset;
4605796c8dcSSimon Schubert 	  break;
4615796c8dcSSimon Schubert 
4625796c8dcSSimon Schubert 	/* The DW_OP_reg operations are required to occur alone in
4635796c8dcSSimon Schubert 	   location expressions.  */
4645796c8dcSSimon Schubert 	case DW_OP_reg0:
4655796c8dcSSimon Schubert 	case DW_OP_reg1:
4665796c8dcSSimon Schubert 	case DW_OP_reg2:
4675796c8dcSSimon Schubert 	case DW_OP_reg3:
4685796c8dcSSimon Schubert 	case DW_OP_reg4:
4695796c8dcSSimon Schubert 	case DW_OP_reg5:
4705796c8dcSSimon Schubert 	case DW_OP_reg6:
4715796c8dcSSimon Schubert 	case DW_OP_reg7:
4725796c8dcSSimon Schubert 	case DW_OP_reg8:
4735796c8dcSSimon Schubert 	case DW_OP_reg9:
4745796c8dcSSimon Schubert 	case DW_OP_reg10:
4755796c8dcSSimon Schubert 	case DW_OP_reg11:
4765796c8dcSSimon Schubert 	case DW_OP_reg12:
4775796c8dcSSimon Schubert 	case DW_OP_reg13:
4785796c8dcSSimon Schubert 	case DW_OP_reg14:
4795796c8dcSSimon Schubert 	case DW_OP_reg15:
4805796c8dcSSimon Schubert 	case DW_OP_reg16:
4815796c8dcSSimon Schubert 	case DW_OP_reg17:
4825796c8dcSSimon Schubert 	case DW_OP_reg18:
4835796c8dcSSimon Schubert 	case DW_OP_reg19:
4845796c8dcSSimon Schubert 	case DW_OP_reg20:
4855796c8dcSSimon Schubert 	case DW_OP_reg21:
4865796c8dcSSimon Schubert 	case DW_OP_reg22:
4875796c8dcSSimon Schubert 	case DW_OP_reg23:
4885796c8dcSSimon Schubert 	case DW_OP_reg24:
4895796c8dcSSimon Schubert 	case DW_OP_reg25:
4905796c8dcSSimon Schubert 	case DW_OP_reg26:
4915796c8dcSSimon Schubert 	case DW_OP_reg27:
4925796c8dcSSimon Schubert 	case DW_OP_reg28:
4935796c8dcSSimon Schubert 	case DW_OP_reg29:
4945796c8dcSSimon Schubert 	case DW_OP_reg30:
4955796c8dcSSimon Schubert 	case DW_OP_reg31:
4965796c8dcSSimon Schubert 	  if (op_ptr != op_end
4975796c8dcSSimon Schubert 	      && *op_ptr != DW_OP_piece
498cf7f2e2dSJohn Marino 	      && *op_ptr != DW_OP_bit_piece
4995796c8dcSSimon Schubert 	      && *op_ptr != DW_OP_GNU_uninit)
5005796c8dcSSimon Schubert 	    error (_("DWARF-2 expression error: DW_OP_reg operations must be "
501cf7f2e2dSJohn Marino 		     "used either alone or in conjuction with DW_OP_piece "
502cf7f2e2dSJohn Marino 		     "or DW_OP_bit_piece."));
5035796c8dcSSimon Schubert 
5045796c8dcSSimon Schubert 	  result = op - DW_OP_reg0;
5055796c8dcSSimon Schubert 	  ctx->location = DWARF_VALUE_REGISTER;
5065796c8dcSSimon Schubert 	  break;
5075796c8dcSSimon Schubert 
5085796c8dcSSimon Schubert 	case DW_OP_regx:
5095796c8dcSSimon Schubert 	  op_ptr = read_uleb128 (op_ptr, op_end, &reg);
510cf7f2e2dSJohn Marino 	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
5115796c8dcSSimon Schubert 
5125796c8dcSSimon Schubert 	  result = reg;
5135796c8dcSSimon Schubert 	  ctx->location = DWARF_VALUE_REGISTER;
5145796c8dcSSimon Schubert 	  break;
5155796c8dcSSimon Schubert 
5165796c8dcSSimon Schubert 	case DW_OP_implicit_value:
5175796c8dcSSimon Schubert 	  {
5185796c8dcSSimon Schubert 	    ULONGEST len;
519cf7f2e2dSJohn Marino 
5205796c8dcSSimon Schubert 	    op_ptr = read_uleb128 (op_ptr, op_end, &len);
5215796c8dcSSimon Schubert 	    if (op_ptr + len > op_end)
5225796c8dcSSimon Schubert 	      error (_("DW_OP_implicit_value: too few bytes available."));
5235796c8dcSSimon Schubert 	    ctx->len = len;
5245796c8dcSSimon Schubert 	    ctx->data = op_ptr;
5255796c8dcSSimon Schubert 	    ctx->location = DWARF_VALUE_LITERAL;
5265796c8dcSSimon Schubert 	    op_ptr += len;
527cf7f2e2dSJohn Marino 	    dwarf_expr_require_composition (op_ptr, op_end,
528cf7f2e2dSJohn Marino 					    "DW_OP_implicit_value");
5295796c8dcSSimon Schubert 	  }
5305796c8dcSSimon Schubert 	  goto no_push;
5315796c8dcSSimon Schubert 
5325796c8dcSSimon Schubert 	case DW_OP_stack_value:
5335796c8dcSSimon Schubert 	  ctx->location = DWARF_VALUE_STACK;
534cf7f2e2dSJohn Marino 	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
5355796c8dcSSimon Schubert 	  goto no_push;
5365796c8dcSSimon Schubert 
537*c50c785cSJohn Marino 	case DW_OP_GNU_implicit_pointer:
538*c50c785cSJohn Marino 	  {
539*c50c785cSJohn Marino 	    ULONGEST die;
540*c50c785cSJohn Marino 	    LONGEST len;
541*c50c785cSJohn Marino 
542*c50c785cSJohn Marino 	    /* The referred-to DIE.  */
543*c50c785cSJohn Marino 	    ctx->len = extract_unsigned_integer (op_ptr, ctx->addr_size,
544*c50c785cSJohn Marino 						 byte_order);
545*c50c785cSJohn Marino 	    op_ptr += ctx->addr_size;
546*c50c785cSJohn Marino 
547*c50c785cSJohn Marino 	    /* The byte offset into the data.  */
548*c50c785cSJohn Marino 	    op_ptr = read_sleb128 (op_ptr, op_end, &len);
549*c50c785cSJohn Marino 	    result = (ULONGEST) len;
550*c50c785cSJohn Marino 
551*c50c785cSJohn Marino 	    ctx->location = DWARF_VALUE_IMPLICIT_POINTER;
552*c50c785cSJohn Marino 	    dwarf_expr_require_composition (op_ptr, op_end,
553*c50c785cSJohn Marino 					    "DW_OP_GNU_implicit_pointer");
554*c50c785cSJohn Marino 	  }
555*c50c785cSJohn Marino 	  break;
556*c50c785cSJohn Marino 
5575796c8dcSSimon Schubert 	case DW_OP_breg0:
5585796c8dcSSimon Schubert 	case DW_OP_breg1:
5595796c8dcSSimon Schubert 	case DW_OP_breg2:
5605796c8dcSSimon Schubert 	case DW_OP_breg3:
5615796c8dcSSimon Schubert 	case DW_OP_breg4:
5625796c8dcSSimon Schubert 	case DW_OP_breg5:
5635796c8dcSSimon Schubert 	case DW_OP_breg6:
5645796c8dcSSimon Schubert 	case DW_OP_breg7:
5655796c8dcSSimon Schubert 	case DW_OP_breg8:
5665796c8dcSSimon Schubert 	case DW_OP_breg9:
5675796c8dcSSimon Schubert 	case DW_OP_breg10:
5685796c8dcSSimon Schubert 	case DW_OP_breg11:
5695796c8dcSSimon Schubert 	case DW_OP_breg12:
5705796c8dcSSimon Schubert 	case DW_OP_breg13:
5715796c8dcSSimon Schubert 	case DW_OP_breg14:
5725796c8dcSSimon Schubert 	case DW_OP_breg15:
5735796c8dcSSimon Schubert 	case DW_OP_breg16:
5745796c8dcSSimon Schubert 	case DW_OP_breg17:
5755796c8dcSSimon Schubert 	case DW_OP_breg18:
5765796c8dcSSimon Schubert 	case DW_OP_breg19:
5775796c8dcSSimon Schubert 	case DW_OP_breg20:
5785796c8dcSSimon Schubert 	case DW_OP_breg21:
5795796c8dcSSimon Schubert 	case DW_OP_breg22:
5805796c8dcSSimon Schubert 	case DW_OP_breg23:
5815796c8dcSSimon Schubert 	case DW_OP_breg24:
5825796c8dcSSimon Schubert 	case DW_OP_breg25:
5835796c8dcSSimon Schubert 	case DW_OP_breg26:
5845796c8dcSSimon Schubert 	case DW_OP_breg27:
5855796c8dcSSimon Schubert 	case DW_OP_breg28:
5865796c8dcSSimon Schubert 	case DW_OP_breg29:
5875796c8dcSSimon Schubert 	case DW_OP_breg30:
5885796c8dcSSimon Schubert 	case DW_OP_breg31:
5895796c8dcSSimon Schubert 	  {
5905796c8dcSSimon Schubert 	    op_ptr = read_sleb128 (op_ptr, op_end, &offset);
5915796c8dcSSimon Schubert 	    result = (ctx->read_reg) (ctx->baton, op - DW_OP_breg0);
5925796c8dcSSimon Schubert 	    result += offset;
5935796c8dcSSimon Schubert 	  }
5945796c8dcSSimon Schubert 	  break;
5955796c8dcSSimon Schubert 	case DW_OP_bregx:
5965796c8dcSSimon Schubert 	  {
5975796c8dcSSimon Schubert 	    op_ptr = read_uleb128 (op_ptr, op_end, &reg);
5985796c8dcSSimon Schubert 	    op_ptr = read_sleb128 (op_ptr, op_end, &offset);
5995796c8dcSSimon Schubert 	    result = (ctx->read_reg) (ctx->baton, reg);
6005796c8dcSSimon Schubert 	    result += offset;
6015796c8dcSSimon Schubert 	  }
6025796c8dcSSimon Schubert 	  break;
6035796c8dcSSimon Schubert 	case DW_OP_fbreg:
6045796c8dcSSimon Schubert 	  {
605cf7f2e2dSJohn Marino 	    const gdb_byte *datastart;
6065796c8dcSSimon Schubert 	    size_t datalen;
6075796c8dcSSimon Schubert 	    unsigned int before_stack_len;
6085796c8dcSSimon Schubert 
6095796c8dcSSimon Schubert 	    op_ptr = read_sleb128 (op_ptr, op_end, &offset);
6105796c8dcSSimon Schubert 	    /* Rather than create a whole new context, we simply
6115796c8dcSSimon Schubert 	       record the stack length before execution, then reset it
6125796c8dcSSimon Schubert 	       afterwards, effectively erasing whatever the recursive
6135796c8dcSSimon Schubert 	       call put there.  */
6145796c8dcSSimon Schubert 	    before_stack_len = ctx->stack_len;
6155796c8dcSSimon Schubert 	    /* FIXME: cagney/2003-03-26: This code should be using
6165796c8dcSSimon Schubert                get_frame_base_address(), and then implement a dwarf2
6175796c8dcSSimon Schubert                specific this_base method.  */
6185796c8dcSSimon Schubert 	    (ctx->get_frame_base) (ctx->baton, &datastart, &datalen);
6195796c8dcSSimon Schubert 	    dwarf_expr_eval (ctx, datastart, datalen);
620cf7f2e2dSJohn Marino 	    if (ctx->location == DWARF_VALUE_MEMORY)
621cf7f2e2dSJohn Marino 	      result = dwarf_expr_fetch_address (ctx, 0);
622cf7f2e2dSJohn Marino 	    else if (ctx->location == DWARF_VALUE_REGISTER)
623cf7f2e2dSJohn Marino 	      result = (ctx->read_reg) (ctx->baton, dwarf_expr_fetch (ctx, 0));
624cf7f2e2dSJohn Marino 	    else
625*c50c785cSJohn Marino 	      error (_("Not implemented: computing frame "
626*c50c785cSJohn Marino 		       "base using explicit value operator"));
6275796c8dcSSimon Schubert 	    result = result + offset;
6285796c8dcSSimon Schubert 	    in_stack_memory = 1;
6295796c8dcSSimon Schubert 	    ctx->stack_len = before_stack_len;
6305796c8dcSSimon Schubert 	    ctx->location = DWARF_VALUE_MEMORY;
6315796c8dcSSimon Schubert 	  }
6325796c8dcSSimon Schubert 	  break;
6335796c8dcSSimon Schubert 
6345796c8dcSSimon Schubert 	case DW_OP_dup:
6355796c8dcSSimon Schubert 	  result = dwarf_expr_fetch (ctx, 0);
6365796c8dcSSimon Schubert 	  in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
6375796c8dcSSimon Schubert 	  break;
6385796c8dcSSimon Schubert 
6395796c8dcSSimon Schubert 	case DW_OP_drop:
6405796c8dcSSimon Schubert 	  dwarf_expr_pop (ctx);
6415796c8dcSSimon Schubert 	  goto no_push;
6425796c8dcSSimon Schubert 
6435796c8dcSSimon Schubert 	case DW_OP_pick:
6445796c8dcSSimon Schubert 	  offset = *op_ptr++;
6455796c8dcSSimon Schubert 	  result = dwarf_expr_fetch (ctx, offset);
6465796c8dcSSimon Schubert 	  in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, offset);
6475796c8dcSSimon Schubert 	  break;
6485796c8dcSSimon Schubert 
6495796c8dcSSimon Schubert 	case DW_OP_swap:
6505796c8dcSSimon Schubert 	  {
6515796c8dcSSimon Schubert 	    struct dwarf_stack_value t1, t2;
6525796c8dcSSimon Schubert 
6535796c8dcSSimon Schubert 	    if (ctx->stack_len < 2)
654*c50c785cSJohn Marino 	       error (_("Not enough elements for "
655*c50c785cSJohn Marino 			"DW_OP_swap.  Need 2, have %d."),
6565796c8dcSSimon Schubert 		      ctx->stack_len);
6575796c8dcSSimon Schubert 	    t1 = ctx->stack[ctx->stack_len - 1];
6585796c8dcSSimon Schubert 	    t2 = ctx->stack[ctx->stack_len - 2];
6595796c8dcSSimon Schubert 	    ctx->stack[ctx->stack_len - 1] = t2;
6605796c8dcSSimon Schubert 	    ctx->stack[ctx->stack_len - 2] = t1;
6615796c8dcSSimon Schubert 	    goto no_push;
6625796c8dcSSimon Schubert 	  }
6635796c8dcSSimon Schubert 
6645796c8dcSSimon Schubert 	case DW_OP_over:
6655796c8dcSSimon Schubert 	  result = dwarf_expr_fetch (ctx, 1);
6665796c8dcSSimon Schubert 	  in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 1);
6675796c8dcSSimon Schubert 	  break;
6685796c8dcSSimon Schubert 
6695796c8dcSSimon Schubert 	case DW_OP_rot:
6705796c8dcSSimon Schubert 	  {
6715796c8dcSSimon Schubert 	    struct dwarf_stack_value t1, t2, t3;
6725796c8dcSSimon Schubert 
6735796c8dcSSimon Schubert 	    if (ctx->stack_len < 3)
674*c50c785cSJohn Marino 	       error (_("Not enough elements for "
675*c50c785cSJohn Marino 			"DW_OP_rot.  Need 3, have %d."),
6765796c8dcSSimon Schubert 		      ctx->stack_len);
6775796c8dcSSimon Schubert 	    t1 = ctx->stack[ctx->stack_len - 1];
6785796c8dcSSimon Schubert 	    t2 = ctx->stack[ctx->stack_len - 2];
6795796c8dcSSimon Schubert 	    t3 = ctx->stack[ctx->stack_len - 3];
6805796c8dcSSimon Schubert 	    ctx->stack[ctx->stack_len - 1] = t2;
6815796c8dcSSimon Schubert 	    ctx->stack[ctx->stack_len - 2] = t3;
6825796c8dcSSimon Schubert 	    ctx->stack[ctx->stack_len - 3] = t1;
6835796c8dcSSimon Schubert 	    goto no_push;
6845796c8dcSSimon Schubert 	  }
6855796c8dcSSimon Schubert 
6865796c8dcSSimon Schubert 	case DW_OP_deref:
6875796c8dcSSimon Schubert 	case DW_OP_deref_size:
688cf7f2e2dSJohn Marino 	  {
689cf7f2e2dSJohn Marino 	    int addr_size = (op == DW_OP_deref ? ctx->addr_size : *op_ptr++);
690cf7f2e2dSJohn Marino 	    gdb_byte *buf = alloca (addr_size);
691cf7f2e2dSJohn Marino 	    CORE_ADDR addr = dwarf_expr_fetch_address (ctx, 0);
692cf7f2e2dSJohn Marino 	    dwarf_expr_pop (ctx);
693cf7f2e2dSJohn Marino 
694cf7f2e2dSJohn Marino 	    (ctx->read_mem) (ctx->baton, buf, addr, addr_size);
695cf7f2e2dSJohn Marino 	    result = extract_unsigned_integer (buf, addr_size, byte_order);
696cf7f2e2dSJohn Marino 	    break;
697cf7f2e2dSJohn Marino 	  }
698cf7f2e2dSJohn Marino 
6995796c8dcSSimon Schubert 	case DW_OP_abs:
7005796c8dcSSimon Schubert 	case DW_OP_neg:
7015796c8dcSSimon Schubert 	case DW_OP_not:
7025796c8dcSSimon Schubert 	case DW_OP_plus_uconst:
7035796c8dcSSimon Schubert 	  /* Unary operations.  */
7045796c8dcSSimon Schubert 	  result = dwarf_expr_fetch (ctx, 0);
7055796c8dcSSimon Schubert 	  dwarf_expr_pop (ctx);
7065796c8dcSSimon Schubert 
7075796c8dcSSimon Schubert 	  switch (op)
7085796c8dcSSimon Schubert 	    {
7095796c8dcSSimon Schubert 	    case DW_OP_abs:
710cf7f2e2dSJohn Marino 	      if (sign_ext (result) < 0)
7115796c8dcSSimon Schubert 		result = -result;
7125796c8dcSSimon Schubert 	      break;
7135796c8dcSSimon Schubert 	    case DW_OP_neg:
7145796c8dcSSimon Schubert 	      result = -result;
7155796c8dcSSimon Schubert 	      break;
7165796c8dcSSimon Schubert 	    case DW_OP_not:
7175796c8dcSSimon Schubert 	      result = ~result;
7185796c8dcSSimon Schubert 	      break;
7195796c8dcSSimon Schubert 	    case DW_OP_plus_uconst:
7205796c8dcSSimon Schubert 	      op_ptr = read_uleb128 (op_ptr, op_end, &reg);
7215796c8dcSSimon Schubert 	      result += reg;
7225796c8dcSSimon Schubert 	      break;
7235796c8dcSSimon Schubert 	    }
7245796c8dcSSimon Schubert 	  break;
7255796c8dcSSimon Schubert 
7265796c8dcSSimon Schubert 	case DW_OP_and:
7275796c8dcSSimon Schubert 	case DW_OP_div:
7285796c8dcSSimon Schubert 	case DW_OP_minus:
7295796c8dcSSimon Schubert 	case DW_OP_mod:
7305796c8dcSSimon Schubert 	case DW_OP_mul:
7315796c8dcSSimon Schubert 	case DW_OP_or:
7325796c8dcSSimon Schubert 	case DW_OP_plus:
7335796c8dcSSimon Schubert 	case DW_OP_shl:
7345796c8dcSSimon Schubert 	case DW_OP_shr:
7355796c8dcSSimon Schubert 	case DW_OP_shra:
7365796c8dcSSimon Schubert 	case DW_OP_xor:
7375796c8dcSSimon Schubert 	case DW_OP_le:
7385796c8dcSSimon Schubert 	case DW_OP_ge:
7395796c8dcSSimon Schubert 	case DW_OP_eq:
7405796c8dcSSimon Schubert 	case DW_OP_lt:
7415796c8dcSSimon Schubert 	case DW_OP_gt:
7425796c8dcSSimon Schubert 	case DW_OP_ne:
7435796c8dcSSimon Schubert 	  {
744cf7f2e2dSJohn Marino 	    /* Binary operations.  */
745cf7f2e2dSJohn Marino 	    ULONGEST first, second;
7465796c8dcSSimon Schubert 
7475796c8dcSSimon Schubert 	    second = dwarf_expr_fetch (ctx, 0);
7485796c8dcSSimon Schubert 	    dwarf_expr_pop (ctx);
7495796c8dcSSimon Schubert 
7505796c8dcSSimon Schubert 	    first = dwarf_expr_fetch (ctx, 0);
7515796c8dcSSimon Schubert 	    dwarf_expr_pop (ctx);
7525796c8dcSSimon Schubert 
7535796c8dcSSimon Schubert 	    switch (op)
7545796c8dcSSimon Schubert 	      {
7555796c8dcSSimon Schubert 	      case DW_OP_and:
756cf7f2e2dSJohn Marino 		result = first & second;
7575796c8dcSSimon Schubert 		break;
7585796c8dcSSimon Schubert 	      case DW_OP_div:
759cf7f2e2dSJohn Marino 		if (!second)
760cf7f2e2dSJohn Marino 		  error (_("Division by zero"));
761cf7f2e2dSJohn Marino 		result = sign_ext (first) / sign_ext (second);
7625796c8dcSSimon Schubert                 break;
7635796c8dcSSimon Schubert 	      case DW_OP_minus:
764cf7f2e2dSJohn Marino 		result = first - second;
7655796c8dcSSimon Schubert 		break;
7665796c8dcSSimon Schubert 	      case DW_OP_mod:
767cf7f2e2dSJohn Marino 		if (!second)
768cf7f2e2dSJohn Marino 		  error (_("Division by zero"));
769cf7f2e2dSJohn Marino 		result = first % second;
7705796c8dcSSimon Schubert 		break;
7715796c8dcSSimon Schubert 	      case DW_OP_mul:
772cf7f2e2dSJohn Marino 		result = first * second;
7735796c8dcSSimon Schubert 		break;
7745796c8dcSSimon Schubert 	      case DW_OP_or:
775cf7f2e2dSJohn Marino 		result = first | second;
7765796c8dcSSimon Schubert 		break;
7775796c8dcSSimon Schubert 	      case DW_OP_plus:
778cf7f2e2dSJohn Marino 		result = first + second;
7795796c8dcSSimon Schubert 		break;
7805796c8dcSSimon Schubert 	      case DW_OP_shl:
781cf7f2e2dSJohn Marino 		result = first << second;
7825796c8dcSSimon Schubert 		break;
7835796c8dcSSimon Schubert 	      case DW_OP_shr:
784cf7f2e2dSJohn Marino 		result = first >> second;
7855796c8dcSSimon Schubert                 break;
7865796c8dcSSimon Schubert 	      case DW_OP_shra:
787cf7f2e2dSJohn Marino 		result = sign_ext (first) >> second;
7885796c8dcSSimon Schubert 		break;
7895796c8dcSSimon Schubert 	      case DW_OP_xor:
790cf7f2e2dSJohn Marino 		result = first ^ second;
7915796c8dcSSimon Schubert 		break;
7925796c8dcSSimon Schubert 	      case DW_OP_le:
793cf7f2e2dSJohn Marino 		result = sign_ext (first) <= sign_ext (second);
7945796c8dcSSimon Schubert 		break;
7955796c8dcSSimon Schubert 	      case DW_OP_ge:
796cf7f2e2dSJohn Marino 		result = sign_ext (first) >= sign_ext (second);
7975796c8dcSSimon Schubert 		break;
7985796c8dcSSimon Schubert 	      case DW_OP_eq:
799cf7f2e2dSJohn Marino 		result = sign_ext (first) == sign_ext (second);
8005796c8dcSSimon Schubert 		break;
8015796c8dcSSimon Schubert 	      case DW_OP_lt:
802cf7f2e2dSJohn Marino 		result = sign_ext (first) < sign_ext (second);
8035796c8dcSSimon Schubert 		break;
8045796c8dcSSimon Schubert 	      case DW_OP_gt:
805cf7f2e2dSJohn Marino 		result = sign_ext (first) > sign_ext (second);
8065796c8dcSSimon Schubert 		break;
8075796c8dcSSimon Schubert 	      case DW_OP_ne:
808cf7f2e2dSJohn Marino 		result = sign_ext (first) != sign_ext (second);
8095796c8dcSSimon Schubert 		break;
8105796c8dcSSimon Schubert 	      default:
8115796c8dcSSimon Schubert 		internal_error (__FILE__, __LINE__,
8125796c8dcSSimon Schubert 				_("Can't be reached."));
8135796c8dcSSimon Schubert 	      }
8145796c8dcSSimon Schubert 	  }
8155796c8dcSSimon Schubert 	  break;
8165796c8dcSSimon Schubert 
8175796c8dcSSimon Schubert 	case DW_OP_call_frame_cfa:
8185796c8dcSSimon Schubert 	  result = (ctx->get_frame_cfa) (ctx->baton);
8195796c8dcSSimon Schubert 	  in_stack_memory = 1;
8205796c8dcSSimon Schubert 	  break;
8215796c8dcSSimon Schubert 
8225796c8dcSSimon Schubert 	case DW_OP_GNU_push_tls_address:
8235796c8dcSSimon Schubert 	  /* Variable is at a constant offset in the thread-local
8245796c8dcSSimon Schubert 	  storage block into the objfile for the current thread and
8255796c8dcSSimon Schubert 	  the dynamic linker module containing this expression.  Here
8265796c8dcSSimon Schubert 	  we return returns the offset from that base.  The top of the
8275796c8dcSSimon Schubert 	  stack has the offset from the beginning of the thread
8285796c8dcSSimon Schubert 	  control block at which the variable is located.  Nothing
8295796c8dcSSimon Schubert 	  should follow this operator, so the top of stack would be
8305796c8dcSSimon Schubert 	  returned.  */
8315796c8dcSSimon Schubert 	  result = dwarf_expr_fetch (ctx, 0);
8325796c8dcSSimon Schubert 	  dwarf_expr_pop (ctx);
8335796c8dcSSimon Schubert 	  result = (ctx->get_tls_address) (ctx->baton, result);
8345796c8dcSSimon Schubert 	  break;
8355796c8dcSSimon Schubert 
8365796c8dcSSimon Schubert 	case DW_OP_skip:
8375796c8dcSSimon Schubert 	  offset = extract_signed_integer (op_ptr, 2, byte_order);
8385796c8dcSSimon Schubert 	  op_ptr += 2;
8395796c8dcSSimon Schubert 	  op_ptr += offset;
8405796c8dcSSimon Schubert 	  goto no_push;
8415796c8dcSSimon Schubert 
8425796c8dcSSimon Schubert 	case DW_OP_bra:
8435796c8dcSSimon Schubert 	  offset = extract_signed_integer (op_ptr, 2, byte_order);
8445796c8dcSSimon Schubert 	  op_ptr += 2;
8455796c8dcSSimon Schubert 	  if (dwarf_expr_fetch (ctx, 0) != 0)
8465796c8dcSSimon Schubert 	    op_ptr += offset;
8475796c8dcSSimon Schubert 	  dwarf_expr_pop (ctx);
8485796c8dcSSimon Schubert 	  goto no_push;
8495796c8dcSSimon Schubert 
8505796c8dcSSimon Schubert 	case DW_OP_nop:
8515796c8dcSSimon Schubert 	  goto no_push;
8525796c8dcSSimon Schubert 
8535796c8dcSSimon Schubert         case DW_OP_piece:
8545796c8dcSSimon Schubert           {
8555796c8dcSSimon Schubert             ULONGEST size;
8565796c8dcSSimon Schubert 
8575796c8dcSSimon Schubert             /* Record the piece.  */
8585796c8dcSSimon Schubert             op_ptr = read_uleb128 (op_ptr, op_end, &size);
859cf7f2e2dSJohn Marino 	    add_piece (ctx, 8 * size, 0);
8605796c8dcSSimon Schubert 
8615796c8dcSSimon Schubert             /* Pop off the address/regnum, and reset the location
8625796c8dcSSimon Schubert 	       type.  */
863cf7f2e2dSJohn Marino 	    if (ctx->location != DWARF_VALUE_LITERAL
864cf7f2e2dSJohn Marino 		&& ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
865cf7f2e2dSJohn Marino 	      dwarf_expr_pop (ctx);
866cf7f2e2dSJohn Marino             ctx->location = DWARF_VALUE_MEMORY;
867cf7f2e2dSJohn Marino           }
868cf7f2e2dSJohn Marino           goto no_push;
869cf7f2e2dSJohn Marino 
870cf7f2e2dSJohn Marino 	case DW_OP_bit_piece:
871cf7f2e2dSJohn Marino 	  {
872cf7f2e2dSJohn Marino 	    ULONGEST size, offset;
873cf7f2e2dSJohn Marino 
874cf7f2e2dSJohn Marino             /* Record the piece.  */
875cf7f2e2dSJohn Marino 	    op_ptr = read_uleb128 (op_ptr, op_end, &size);
876cf7f2e2dSJohn Marino 	    op_ptr = read_uleb128 (op_ptr, op_end, &offset);
877cf7f2e2dSJohn Marino 	    add_piece (ctx, size, offset);
878cf7f2e2dSJohn Marino 
879cf7f2e2dSJohn Marino             /* Pop off the address/regnum, and reset the location
880cf7f2e2dSJohn Marino 	       type.  */
881cf7f2e2dSJohn Marino 	    if (ctx->location != DWARF_VALUE_LITERAL
882cf7f2e2dSJohn Marino 		&& ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
8835796c8dcSSimon Schubert 	      dwarf_expr_pop (ctx);
8845796c8dcSSimon Schubert             ctx->location = DWARF_VALUE_MEMORY;
8855796c8dcSSimon Schubert 	  }
8865796c8dcSSimon Schubert 	  goto no_push;
8875796c8dcSSimon Schubert 
8885796c8dcSSimon Schubert 	case DW_OP_GNU_uninit:
8895796c8dcSSimon Schubert 	  if (op_ptr != op_end)
8905796c8dcSSimon Schubert 	    error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
8915796c8dcSSimon Schubert 		   "be the very last op."));
8925796c8dcSSimon Schubert 
8935796c8dcSSimon Schubert 	  ctx->initialized = 0;
8945796c8dcSSimon Schubert 	  goto no_push;
8955796c8dcSSimon Schubert 
896cf7f2e2dSJohn Marino 	case DW_OP_call2:
897cf7f2e2dSJohn Marino 	  result = extract_unsigned_integer (op_ptr, 2, byte_order);
898cf7f2e2dSJohn Marino 	  op_ptr += 2;
899cf7f2e2dSJohn Marino 	  ctx->dwarf_call (ctx, result);
900cf7f2e2dSJohn Marino 	  goto no_push;
901cf7f2e2dSJohn Marino 
902cf7f2e2dSJohn Marino 	case DW_OP_call4:
903cf7f2e2dSJohn Marino 	  result = extract_unsigned_integer (op_ptr, 4, byte_order);
904cf7f2e2dSJohn Marino 	  op_ptr += 4;
905cf7f2e2dSJohn Marino 	  ctx->dwarf_call (ctx, result);
906cf7f2e2dSJohn Marino 	  goto no_push;
907cf7f2e2dSJohn Marino 
908*c50c785cSJohn Marino 	case DW_OP_GNU_entry_value:
909*c50c785cSJohn Marino 	  /* This operation is not yet supported by GDB.  */
910*c50c785cSJohn Marino 	  ctx->location = DWARF_VALUE_OPTIMIZED_OUT;
911*c50c785cSJohn Marino 	  ctx->stack_len = 0;
912*c50c785cSJohn Marino 	  ctx->num_pieces = 0;
913*c50c785cSJohn Marino 	  goto abort_expression;
914*c50c785cSJohn Marino 
9155796c8dcSSimon Schubert 	default:
9165796c8dcSSimon Schubert 	  error (_("Unhandled dwarf expression opcode 0x%x"), op);
9175796c8dcSSimon Schubert 	}
9185796c8dcSSimon Schubert 
9195796c8dcSSimon Schubert       /* Most things push a result value.  */
9205796c8dcSSimon Schubert       dwarf_expr_push (ctx, result, in_stack_memory);
921*c50c785cSJohn Marino     no_push:
922*c50c785cSJohn Marino       ;
9235796c8dcSSimon Schubert     }
9245796c8dcSSimon Schubert 
925*c50c785cSJohn Marino   /* To simplify our main caller, if the result is an implicit
926*c50c785cSJohn Marino      pointer, then make a pieced value.  This is ok because we can't
927*c50c785cSJohn Marino      have implicit pointers in contexts where pieces are invalid.  */
928*c50c785cSJohn Marino   if (ctx->location == DWARF_VALUE_IMPLICIT_POINTER)
929*c50c785cSJohn Marino     add_piece (ctx, 8 * ctx->addr_size, 0);
930*c50c785cSJohn Marino 
931*c50c785cSJohn Marino abort_expression:
9325796c8dcSSimon Schubert   ctx->recursion_depth--;
9335796c8dcSSimon Schubert   gdb_assert (ctx->recursion_depth >= 0);
934cf7f2e2dSJohn Marino #undef sign_ext
9355796c8dcSSimon Schubert }
936