Add gdbarch callback to provide formats for debug info float types
[deliverable/binutils-gdb.git] / gdb / dwarf2expr.c
index 226cb1d9495fffeeb5309147db5a0d22beec6ce2..7eb1982e5a580759bd11473d613b36b9d615e555 100644 (file)
@@ -1,7 +1,6 @@
 /* DWARF 2 Expression Evaluator.
 
-   Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2010, 2011
-   Free Software Foundation, Inc.
+   Copyright (C) 2001-2016 Free Software Foundation, Inc.
 
    Contributed by Daniel Berlin (dan@dberlin.org)
 
@@ -27,7 +26,7 @@
 #include "gdbcore.h"
 #include "dwarf2.h"
 #include "dwarf2expr.h"
-#include "gdb_assert.h"
+#include "dwarf2loc.h"
 
 /* Local prototypes.  */
 
@@ -66,8 +65,9 @@ dwarf_gdbarch_types_init (struct gdbarch *gdbarch)
 static struct type *
 dwarf_expr_address_type (struct dwarf_expr_context *ctx)
 {
-  struct dwarf_gdbarch_types *types = gdbarch_data (ctx->gdbarch,
-                                                   dwarf_arch_cookie);
+  struct dwarf_gdbarch_types *types
+    = (struct dwarf_gdbarch_types *) gdbarch_data (ctx->gdbarch,
+                                                  dwarf_arch_cookie);
   int ndx;
 
   if (ctx->addr_size == 2)
@@ -96,15 +96,13 @@ new_dwarf_expr_context (void)
 {
   struct dwarf_expr_context *retval;
 
-  retval = xcalloc (1, sizeof (struct dwarf_expr_context));
+  retval = XCNEW (struct dwarf_expr_context);
   retval->stack_len = 0;
   retval->stack_allocated = 10;
-  retval->stack = xmalloc (retval->stack_allocated
-                          * sizeof (struct dwarf_stack_value));
+  retval->stack = XNEWVEC (struct dwarf_stack_value, retval->stack_allocated);
   retval->num_pieces = 0;
   retval->pieces = 0;
   retval->max_recursion_depth = 0x100;
-  retval->mark = value_mark ();
   return retval;
 }
 
@@ -113,7 +111,6 @@ new_dwarf_expr_context (void)
 void
 free_dwarf_expr_context (struct dwarf_expr_context *ctx)
 {
-  value_free_to_mark (ctx->mark);
   xfree (ctx->stack);
   xfree (ctx->pieces);
   xfree (ctx);
@@ -124,7 +121,7 @@ free_dwarf_expr_context (struct dwarf_expr_context *ctx)
 static void
 free_dwarf_expr_context_cleanup (void *arg)
 {
-  free_dwarf_expr_context (arg);
+  free_dwarf_expr_context ((struct dwarf_expr_context *) arg);
 }
 
 /* Return a cleanup that calls free_dwarf_expr_context.  */
@@ -145,8 +142,7 @@ dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need)
     {
       size_t newlen = ctx->stack_len + need + 10;
 
-      ctx->stack = xrealloc (ctx->stack,
-                            newlen * sizeof (struct dwarf_stack_value));
+      ctx->stack = XRESIZEVEC (struct dwarf_stack_value, ctx->stack, newlen);
       ctx->stack_allocated = newlen;
     }
 }
@@ -231,6 +227,28 @@ get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
     }
 }
 
+/* Return the signed form of TYPE.  TYPE is necessarily an integral
+   type.  */
+
+static struct type *
+get_signed_type (struct gdbarch *gdbarch, struct type *type)
+{
+  switch (TYPE_LENGTH (type))
+    {
+    case 1:
+      return builtin_type (gdbarch)->builtin_int8;
+    case 2:
+      return builtin_type (gdbarch)->builtin_int16;
+    case 4:
+      return builtin_type (gdbarch)->builtin_int32;
+    case 8:
+      return builtin_type (gdbarch)->builtin_int64;
+    default:
+      error (_("no signed variant found for type, while evaluating "
+              "DWARF expression"));
+    }
+}
+
 /* Retrieve the N'th item on CTX's stack, converted to an address.  */
 
 CORE_ADDR
@@ -253,7 +271,7 @@ dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n)
      for those architectures which require it.  */
   if (gdbarch_integer_to_address_p (ctx->gdbarch))
     {
-      gdb_byte *buf = alloca (ctx->addr_size);
+      gdb_byte *buf = (gdb_byte *) alloca (ctx->addr_size);
       struct type *int_type = get_unsigned_type (ctx->gdbarch,
                                                 value_type (result_val));
 
@@ -292,9 +310,8 @@ add_piece (struct dwarf_expr_context *ctx, ULONGEST size, ULONGEST offset)
 
   ctx->num_pieces++;
 
-  ctx->pieces = xrealloc (ctx->pieces,
-                         (ctx->num_pieces
-                          * sizeof (struct dwarf_expr_piece)));
+  ctx->pieces
+    = XRESIZEVEC (struct dwarf_expr_piece, ctx->pieces, ctx->num_pieces);
 
   p = &ctx->pieces[ctx->num_pieces - 1];
   p->location = ctx->location;
@@ -322,7 +339,7 @@ add_piece (struct dwarf_expr_context *ctx, ULONGEST size, ULONGEST offset)
     }
   else if (p->location == DWARF_VALUE_IMPLICIT_POINTER)
     {
-      p->v.ptr.die = ctx->len;
+      p->v.ptr.die.sect_off = ctx->len;
       p->v.ptr.offset = value_as_long (dwarf_expr_fetch (ctx, 0));
     }
   else if (p->location == DWARF_VALUE_REGISTER)
@@ -349,58 +366,36 @@ dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr,
   gdb_assert (ctx->recursion_depth == old_recursion_depth);
 }
 
-/* Decode the unsigned LEB128 constant at BUF into the variable pointed to
-   by R, and return the new value of BUF.  Verify that it doesn't extend
-   past BUF_END.  */
+/* Helper to read a uleb128 value or throw an error.  */
 
 const gdb_byte *
-read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end, ULONGEST * r)
+safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
+                  uint64_t *r)
 {
-  unsigned shift = 0;
-  ULONGEST result = 0;
-  gdb_byte byte;
-
-  while (1)
-    {
-      if (buf >= buf_end)
-       error (_("read_uleb128: Corrupted DWARF expression."));
-
-      byte = *buf++;
-      result |= (byte & 0x7f) << shift;
-      if ((byte & 0x80) == 0)
-       break;
-      shift += 7;
-    }
-  *r = result;
+  buf = gdb_read_uleb128 (buf, buf_end, r);
+  if (buf == NULL)
+    error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
   return buf;
 }
 
-/* Decode the signed LEB128 constant at BUF into the variable pointed to
-   by R, and return the new value of BUF.  Verify that it doesn't extend
-   past BUF_END.  */
+/* Helper to read a sleb128 value or throw an error.  */
 
 const gdb_byte *
-read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end, LONGEST * r)
+safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
+                  int64_t *r)
 {
-  unsigned shift = 0;
-  LONGEST result = 0;
-  gdb_byte byte;
-
-  while (1)
-    {
-      if (buf >= buf_end)
-       error (_("read_sleb128: Corrupted DWARF expression."));
-
-      byte = *buf++;
-      result |= (byte & 0x7f) << shift;
-      shift += 7;
-      if ((byte & 0x80) == 0)
-       break;
-    }
-  if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
-    result |= -(1 << shift);
+  buf = gdb_read_sleb128 (buf, buf_end, r);
+  if (buf == NULL)
+    error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
+  return buf;
+}
 
-  *r = result;
+const gdb_byte *
+safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
+{
+  buf = gdb_skip_leb128 (buf, buf_end);
+  if (buf == NULL)
+    error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
   return buf;
 }
 \f
@@ -417,7 +412,7 @@ dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
      checked at the other place that this function is called.  */
   if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece)
     error (_("DWARF-2 expression error: `%s' operations must be "
-            "used either alone or in conjuction with DW_OP_piece "
+            "used either alone or in conjunction with DW_OP_piece "
             "or DW_OP_bit_piece."),
           op_name);
 }
@@ -442,13 +437,15 @@ base_types_equal_p (struct type *t1, struct type *t2)
    size.  */
 
 static struct type *
-dwarf_get_base_type (struct dwarf_expr_context *ctx, ULONGEST die, int size)
+dwarf_get_base_type (struct dwarf_expr_context *ctx, cu_offset die, int size)
 {
   struct type *result;
 
-  if (ctx->get_base_type)
+  if (ctx->funcs->get_base_type)
     {
-      result = ctx->get_base_type (ctx, die);
+      result = ctx->funcs->get_base_type (ctx, die);
+      if (result == NULL)
+       error (_("Could not find type for DW_OP_GNU_const_type"));
       if (size != 0 && TYPE_LENGTH (result) != size)
        error (_("DW_OP_GNU_const_type has different sizes for type and data"));
     }
@@ -459,6 +456,176 @@ dwarf_get_base_type (struct dwarf_expr_context *ctx, ULONGEST die, int size)
   return result;
 }
 
+/* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
+   DWARF register number.  Otherwise return -1.  */
+
+int
+dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end)
+{
+  uint64_t dwarf_reg;
+
+  if (buf_end <= buf)
+    return -1;
+  if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
+    {
+      if (buf_end - buf != 1)
+       return -1;
+      return *buf - DW_OP_reg0;
+    }
+
+  if (*buf == DW_OP_GNU_regval_type)
+    {
+      buf++;
+      buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
+      if (buf == NULL)
+       return -1;
+      buf = gdb_skip_leb128 (buf, buf_end);
+      if (buf == NULL)
+       return -1;
+    }
+  else if (*buf == DW_OP_regx)
+    {
+      buf++;
+      buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
+      if (buf == NULL)
+       return -1;
+    }
+  else
+    return -1;
+  if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
+    return -1;
+  return dwarf_reg;
+}
+
+/* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
+   DW_OP_deref* return the DWARF register number.  Otherwise return -1.
+   DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
+   size from DW_OP_deref_size.  */
+
+int
+dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end,
+                               CORE_ADDR *deref_size_return)
+{
+  uint64_t dwarf_reg;
+  int64_t offset;
+
+  if (buf_end <= buf)
+    return -1;
+
+  if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
+    {
+      dwarf_reg = *buf - DW_OP_breg0;
+      buf++;
+      if (buf >= buf_end)
+       return -1;
+    }
+  else if (*buf == DW_OP_bregx)
+    {
+      buf++;
+      buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
+      if (buf == NULL)
+       return -1;
+      if ((int) dwarf_reg != dwarf_reg)
+       return -1;
+    }
+  else
+    return -1;
+
+  buf = gdb_read_sleb128 (buf, buf_end, &offset);
+  if (buf == NULL)
+    return -1;
+  if (offset != 0)
+    return -1;
+
+  if (*buf == DW_OP_deref)
+    {
+      buf++;
+      *deref_size_return = -1;
+    }
+  else if (*buf == DW_OP_deref_size)
+    {
+      buf++;
+      if (buf >= buf_end)
+       return -1;
+      *deref_size_return = *buf++;
+    }
+  else
+    return -1;
+
+  if (buf != buf_end)
+    return -1;
+
+  return dwarf_reg;
+}
+
+/* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
+   in FB_OFFSET_RETURN with the X offset and return 1.  Otherwise return 0.  */
+
+int
+dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
+                         CORE_ADDR *fb_offset_return)
+{
+  int64_t fb_offset;
+
+  if (buf_end <= buf)
+    return 0;
+
+  if (*buf != DW_OP_fbreg)
+    return 0;
+  buf++;
+
+  buf = gdb_read_sleb128 (buf, buf_end, &fb_offset);
+  if (buf == NULL)
+    return 0;
+  *fb_offset_return = fb_offset;
+  if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
+    return 0;
+
+  return 1;
+}
+
+/* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
+   in SP_OFFSET_RETURN with the X offset and return 1.  Otherwise return 0.
+   The matched SP register number depends on GDBARCH.  */
+
+int
+dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
+                         const gdb_byte *buf_end, CORE_ADDR *sp_offset_return)
+{
+  uint64_t dwarf_reg;
+  int64_t sp_offset;
+
+  if (buf_end <= buf)
+    return 0;
+  if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
+    {
+      dwarf_reg = *buf - DW_OP_breg0;
+      buf++;
+    }
+  else
+    {
+      if (*buf != DW_OP_bregx)
+       return 0;
+      buf++;
+      buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
+      if (buf == NULL)
+       return 0;
+    }
+
+  if (dwarf_reg_to_regnum (gdbarch, dwarf_reg)
+      != gdbarch_sp_regnum (gdbarch))
+    return 0;
+
+  buf = gdb_read_sleb128 (buf, buf_end, &sp_offset);
+  if (buf == NULL)
+    return 0;
+  *sp_offset_return = sp_offset;
+  if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
+    return 0;
+
+  return 1;
+}
+
 /* The engine for the expression evaluator.  Using the context in CTX,
    evaluate the expression between OP_PTR and OP_END.  */
 
@@ -486,7 +653,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
 
   while (op_ptr < op_end)
     {
-      enum dwarf_location_atom op = *op_ptr++;
+      enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr++;
       ULONGEST result;
       /* Assume the value is not in stack memory.
         Code that knows otherwise sets this to 1.
@@ -495,10 +662,14 @@ execute_stack_op (struct dwarf_expr_context *ctx,
         This is just an optimization, so it's always ok to punt
         and leave this as 0.  */
       int in_stack_memory = 0;
-      ULONGEST uoffset, reg;
-      LONGEST offset;
+      uint64_t uoffset, reg;
+      int64_t offset;
       struct value *result_val = NULL;
 
+      /* The DWARF expression might have a bug causing an infinite
+        loop.  In that case, quitting is the only way out.  */
+      QUIT;
+
       switch (op)
        {
        case DW_OP_lit0:
@@ -550,6 +721,18 @@ execute_stack_op (struct dwarf_expr_context *ctx,
          result_val = value_from_ulongest (address_type, result);
          break;
 
+       case DW_OP_GNU_addr_index:
+         op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
+         result = (ctx->funcs->get_addr_index) (ctx->baton, uoffset);
+         result += ctx->offset;
+         result_val = value_from_ulongest (address_type, result);
+         break;
+       case DW_OP_GNU_const_index:
+         op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
+         result = (ctx->funcs->get_addr_index) (ctx->baton, uoffset);
+         result_val = value_from_ulongest (address_type, result);
+         break;
+
        case DW_OP_const1u:
          result = extract_unsigned_integer (op_ptr, 1, byte_order);
          result_val = value_from_ulongest (address_type, result);
@@ -591,12 +774,12 @@ execute_stack_op (struct dwarf_expr_context *ctx,
          op_ptr += 8;
          break;
        case DW_OP_constu:
-         op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
+         op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
          result = uoffset;
          result_val = value_from_ulongest (address_type, result);
          break;
        case DW_OP_consts:
-         op_ptr = read_sleb128 (op_ptr, op_end, &offset);
+         op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
          result = offset;
          result_val = value_from_ulongest (address_type, result);
          break;
@@ -640,7 +823,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
              && *op_ptr != DW_OP_bit_piece
              && *op_ptr != DW_OP_GNU_uninit)
            error (_("DWARF-2 expression error: DW_OP_reg operations must be "
-                    "used either alone or in conjuction with DW_OP_piece "
+                    "used either alone or in conjunction with DW_OP_piece "
                     "or DW_OP_bit_piece."));
 
          result = op - DW_OP_reg0;
@@ -649,7 +832,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
          break;
 
        case DW_OP_regx:
-         op_ptr = read_uleb128 (op_ptr, op_end, &reg);
+         op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
          dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
 
          result = reg;
@@ -659,9 +842,9 @@ execute_stack_op (struct dwarf_expr_context *ctx,
 
        case DW_OP_implicit_value:
          {
-           ULONGEST len;
+           uint64_t len;
 
-           op_ptr = read_uleb128 (op_ptr, op_end, &len);
+           op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
            if (op_ptr + len > op_end)
              error (_("DW_OP_implicit_value: too few bytes available."));
            ctx->len = len;
@@ -680,16 +863,19 @@ execute_stack_op (struct dwarf_expr_context *ctx,
 
        case DW_OP_GNU_implicit_pointer:
          {
-           ULONGEST die;
-           LONGEST len;
+           int64_t len;
 
-           /* The referred-to DIE.  */
-           ctx->len = extract_unsigned_integer (op_ptr, ctx->addr_size,
+           if (ctx->ref_addr_size == -1)
+             error (_("DWARF-2 expression error: DW_OP_GNU_implicit_pointer "
+                      "is not allowed in frame context"));
+
+           /* The referred-to DIE of sect_offset kind.  */
+           ctx->len = extract_unsigned_integer (op_ptr, ctx->ref_addr_size,
                                                 byte_order);
-           op_ptr += ctx->addr_size;
+           op_ptr += ctx->ref_addr_size;
 
            /* The byte offset into the data.  */
-           op_ptr = read_sleb128 (op_ptr, op_end, &len);
+           op_ptr = safe_read_sleb128 (op_ptr, op_end, &len);
            result = (ULONGEST) len;
            result_val = value_from_ulongest (address_type, result);
 
@@ -732,17 +918,18 @@ execute_stack_op (struct dwarf_expr_context *ctx,
        case DW_OP_breg30:
        case DW_OP_breg31:
          {
-           op_ptr = read_sleb128 (op_ptr, op_end, &offset);
-           result = (ctx->read_reg) (ctx->baton, op - DW_OP_breg0);
+           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
+           result = (ctx->funcs->read_addr_from_reg) (ctx->baton,
+                                                      op - DW_OP_breg0);
            result += offset;
            result_val = value_from_ulongest (address_type, result);
          }
          break;
        case DW_OP_bregx:
          {
-           op_ptr = read_uleb128 (op_ptr, op_end, &reg);
-           op_ptr = read_sleb128 (op_ptr, op_end, &offset);
-           result = (ctx->read_reg) (ctx->baton, reg);
+           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
+           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
+           result = (ctx->funcs->read_addr_from_reg) (ctx->baton, reg);
            result += offset;
            result_val = value_from_ulongest (address_type, result);
          }
@@ -753,7 +940,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
            size_t datalen;
            unsigned int before_stack_len;
 
-           op_ptr = read_sleb128 (op_ptr, op_end, &offset);
+           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
            /* Rather than create a whole new context, we simply
               record the stack length before execution, then reset it
               afterwards, effectively erasing whatever the recursive
@@ -762,14 +949,14 @@ execute_stack_op (struct dwarf_expr_context *ctx,
            /* FIXME: cagney/2003-03-26: This code should be using
                get_frame_base_address(), and then implement a dwarf2
                specific this_base method.  */
-           (ctx->get_frame_base) (ctx->baton, &datastart, &datalen);
+           (ctx->funcs->get_frame_base) (ctx->baton, &datastart, &datalen);
            dwarf_expr_eval (ctx, datastart, datalen);
            if (ctx->location == DWARF_VALUE_MEMORY)
              result = dwarf_expr_fetch_address (ctx, 0);
            else if (ctx->location == DWARF_VALUE_REGISTER)
-             result
-               = (ctx->read_reg) (ctx->baton,
-                                  value_as_long (dwarf_expr_fetch (ctx, 0)));
+             result = (ctx->funcs->read_addr_from_reg)
+                         (ctx->baton,
+                          value_as_long (dwarf_expr_fetch (ctx, 0)));
            else
              error (_("Not implemented: computing frame "
                       "base using explicit value operator"));
@@ -838,7 +1025,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
        case DW_OP_GNU_deref_type:
          {
            int addr_size = (op == DW_OP_deref ? ctx->addr_size : *op_ptr++);
-           gdb_byte *buf = alloca (addr_size);
+           gdb_byte *buf = (gdb_byte *) alloca (addr_size);
            CORE_ADDR addr = dwarf_expr_fetch_address (ctx, 0);
            struct type *type;
 
@@ -846,15 +1033,29 @@ execute_stack_op (struct dwarf_expr_context *ctx,
 
            if (op == DW_OP_GNU_deref_type)
              {
-               ULONGEST type_die;
+               cu_offset type_die;
 
-               op_ptr = read_uleb128 (op_ptr, op_end, &type_die);
+               op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
+               type_die.cu_off = uoffset;
                type = dwarf_get_base_type (ctx, type_die, 0);
              }
            else
              type = address_type;
 
-           (ctx->read_mem) (ctx->baton, buf, addr, addr_size);
+           (ctx->funcs->read_mem) (ctx->baton, buf, addr, addr_size);
+
+           /* If the size of the object read from memory is different
+              from the type length, we need to zero-extend it.  */
+           if (TYPE_LENGTH (type) != addr_size)
+             {
+               ULONGEST result =
+                 extract_unsigned_integer (buf, addr_size, byte_order);
+
+               buf = (gdb_byte *) alloca (TYPE_LENGTH (type));
+               store_unsigned_integer (buf, TYPE_LENGTH (type),
+                                       byte_order, result);
+             }
+
            result_val = value_from_contents_and_address (type, buf, addr);
            break;
          }
@@ -885,7 +1086,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
              case DW_OP_plus_uconst:
                dwarf_require_integral (value_type (result_val));
                result = value_as_long (result_val);
-               op_ptr = read_uleb128 (op_ptr, op_end, &reg);
+               op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
                result += reg;
                result_val = value_from_ulongest (address_type, result);
                break;
@@ -979,7 +1180,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
              case DW_OP_shr:
                dwarf_require_integral (value_type (first));
                dwarf_require_integral (value_type (second));
-               if (value_type (first) == address_type)
+               if (!TYPE_UNSIGNED (value_type (first)))
                  {
                    struct type *utype
                      = get_unsigned_type (ctx->gdbarch, value_type (first));
@@ -996,7 +1197,19 @@ execute_stack_op (struct dwarf_expr_context *ctx,
              case DW_OP_shra:
                dwarf_require_integral (value_type (first));
                dwarf_require_integral (value_type (second));
+               if (TYPE_UNSIGNED (value_type (first)))
+                 {
+                   struct type *stype
+                     = get_signed_type (ctx->gdbarch, value_type (first));
+
+                   first = value_cast (stype, first);
+                 }
+
                result_val = value_binop (first, second, BINOP_RSH);
+               /* Make sure we wind up with the same type we started
+                  with.  */
+               if (value_type (result_val) != value_type (second))
+                 result_val = value_cast (value_type (second), result_val);
                break;
              case DW_OP_xor:
                dwarf_require_integral (value_type (first));
@@ -1038,12 +1251,13 @@ execute_stack_op (struct dwarf_expr_context *ctx,
          break;
 
        case DW_OP_call_frame_cfa:
-         result = (ctx->get_frame_cfa) (ctx->baton);
+         result = (ctx->funcs->get_frame_cfa) (ctx->baton);
          result_val = value_from_ulongest (address_type, result);
          in_stack_memory = 1;
          break;
 
        case DW_OP_GNU_push_tls_address:
+       case DW_OP_form_tls_address:
          /* Variable is at a constant offset in the thread-local
          storage block into the objfile for the current thread and
          the dynamic linker module containing this expression.  Here
@@ -1054,7 +1268,7 @@ execute_stack_op (struct dwarf_expr_context *ctx,
          returned.  */
          result = value_as_long (dwarf_expr_fetch (ctx, 0));
          dwarf_expr_pop (ctx);
-         result = (ctx->get_tls_address) (ctx->baton, result);
+         result = (ctx->funcs->get_tls_address) (ctx->baton, result);
          result_val = value_from_ulongest (address_type, result);
          break;
 
@@ -1083,10 +1297,10 @@ execute_stack_op (struct dwarf_expr_context *ctx,
 
         case DW_OP_piece:
           {
-            ULONGEST size;
+            uint64_t size;
 
             /* Record the piece.  */
-            op_ptr = read_uleb128 (op_ptr, op_end, &size);
+            op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
            add_piece (ctx, 8 * size, 0);
 
             /* Pop off the address/regnum, and reset the location
@@ -1100,11 +1314,11 @@ execute_stack_op (struct dwarf_expr_context *ctx,
 
        case DW_OP_bit_piece:
          {
-           ULONGEST size, offset;
+           uint64_t size, offset;
 
             /* Record the piece.  */
-           op_ptr = read_uleb128 (op_ptr, op_end, &size);
-           op_ptr = read_uleb128 (op_ptr, op_end, &offset);
+           op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
+           op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
            add_piece (ctx, size, offset);
 
             /* Pop off the address/regnum, and reset the location
@@ -1125,32 +1339,88 @@ execute_stack_op (struct dwarf_expr_context *ctx,
          goto no_push;
 
        case DW_OP_call2:
-         result = extract_unsigned_integer (op_ptr, 2, byte_order);
-         op_ptr += 2;
-         ctx->dwarf_call (ctx, result);
+         {
+           cu_offset offset;
+
+           offset.cu_off = extract_unsigned_integer (op_ptr, 2, byte_order);
+           op_ptr += 2;
+           ctx->funcs->dwarf_call (ctx, offset);
+         }
          goto no_push;
 
        case DW_OP_call4:
-         result = extract_unsigned_integer (op_ptr, 4, byte_order);
-         op_ptr += 4;
-         ctx->dwarf_call (ctx, result);
+         {
+           cu_offset offset;
+
+           offset.cu_off = extract_unsigned_integer (op_ptr, 4, byte_order);
+           op_ptr += 4;
+           ctx->funcs->dwarf_call (ctx, offset);
+         }
          goto no_push;
        
        case DW_OP_GNU_entry_value:
-         /* This operation is not yet supported by GDB.  */
-         ctx->location = DWARF_VALUE_OPTIMIZED_OUT;
-         ctx->stack_len = 0;
-         ctx->num_pieces = 0;
-         goto abort_expression;
+         {
+           uint64_t len;
+           CORE_ADDR deref_size;
+           union call_site_parameter_u kind_u;
+
+           op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
+           if (op_ptr + len > op_end)
+             error (_("DW_OP_GNU_entry_value: too few bytes available."));
+
+           kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len);
+           if (kind_u.dwarf_reg != -1)
+             {
+               op_ptr += len;
+               ctx->funcs->push_dwarf_reg_entry_value (ctx,
+                                                 CALL_SITE_PARAMETER_DWARF_REG,
+                                                       kind_u,
+                                                       -1 /* deref_size */);
+               goto no_push;
+             }
+
+           kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr,
+                                                              op_ptr + len,
+                                                              &deref_size);
+           if (kind_u.dwarf_reg != -1)
+             {
+               if (deref_size == -1)
+                 deref_size = ctx->addr_size;
+               op_ptr += len;
+               ctx->funcs->push_dwarf_reg_entry_value (ctx,
+                                                 CALL_SITE_PARAMETER_DWARF_REG,
+                                                       kind_u, deref_size);
+               goto no_push;
+             }
+
+           error (_("DWARF-2 expression error: DW_OP_GNU_entry_value is "
+                    "supported only for single DW_OP_reg* "
+                    "or for DW_OP_breg*(0)+DW_OP_deref*"));
+         }
+
+       case DW_OP_GNU_parameter_ref:
+         {
+           union call_site_parameter_u kind_u;
+
+           kind_u.param_offset.cu_off = extract_unsigned_integer (op_ptr, 4,
+                                                                  byte_order);
+           op_ptr += 4;
+           ctx->funcs->push_dwarf_reg_entry_value (ctx,
+                                              CALL_SITE_PARAMETER_PARAM_OFFSET,
+                                                   kind_u,
+                                                   -1 /* deref_size */);
+         }
+         goto no_push;
 
        case DW_OP_GNU_const_type:
          {
-           ULONGEST type_die;
+           cu_offset type_die;
            int n;
            const gdb_byte *data;
            struct type *type;
 
-           op_ptr = read_uleb128 (op_ptr, op_end, &type_die);
+           op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
+           type_die.cu_off = uoffset;
            n = *op_ptr++;
            data = op_ptr;
            op_ptr += n;
@@ -1162,27 +1432,31 @@ execute_stack_op (struct dwarf_expr_context *ctx,
 
        case DW_OP_GNU_regval_type:
          {
-           ULONGEST type_die;
+           cu_offset type_die;
            struct type *type;
 
-           op_ptr = read_uleb128 (op_ptr, op_end, &reg);
-           op_ptr = read_uleb128 (op_ptr, op_end, &type_die);
+           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
+           op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
+           type_die.cu_off = uoffset;
 
            type = dwarf_get_base_type (ctx, type_die, 0);
-           result = (ctx->read_reg) (ctx->baton, reg);
-           result_val = value_from_ulongest (type, result);
+           result_val = ctx->funcs->get_reg_value (ctx->baton, type, reg);
          }
          break;
 
        case DW_OP_GNU_convert:
        case DW_OP_GNU_reinterpret:
          {
-           ULONGEST type_die;
+           cu_offset type_die;
            struct type *type;
 
-           op_ptr = read_uleb128 (op_ptr, op_end, &type_die);
+           op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
+           type_die.cu_off = uoffset;
 
-           type = dwarf_get_base_type (ctx, type_die, 0);
+           if (type_die.cu_off == 0)
+             type = address_type;
+           else
+             type = dwarf_get_base_type (ctx, type_die, 0);
 
            result_val = dwarf_expr_fetch (ctx, 0);
            dwarf_expr_pop (ctx);
@@ -1203,6 +1477,12 @@ execute_stack_op (struct dwarf_expr_context *ctx,
          }
          break;
 
+       case DW_OP_push_object_address:
+         /* Return the address of the object we are currently observing.  */
+         result = (ctx->funcs->get_object_address) (ctx->baton);
+         result_val = value_from_ulongest (address_type, result);
+         break;
+
        default:
          error (_("Unhandled dwarf expression opcode 0x%x"), op);
        }
@@ -1225,6 +1505,78 @@ abort_expression:
   gdb_assert (ctx->recursion_depth >= 0);
 }
 
+/* Stub dwarf_expr_context_funcs.get_frame_base implementation.  */
+
+void
+ctx_no_get_frame_base (void *baton, const gdb_byte **start, size_t *length)
+{
+  error (_("%s is invalid in this context"), "DW_OP_fbreg");
+}
+
+/* Stub dwarf_expr_context_funcs.get_frame_cfa implementation.  */
+
+CORE_ADDR
+ctx_no_get_frame_cfa (void *baton)
+{
+  error (_("%s is invalid in this context"), "DW_OP_call_frame_cfa");
+}
+
+/* Stub dwarf_expr_context_funcs.get_frame_pc implementation.  */
+
+CORE_ADDR
+ctx_no_get_frame_pc (void *baton)
+{
+  error (_("%s is invalid in this context"), "DW_OP_GNU_implicit_pointer");
+}
+
+/* Stub dwarf_expr_context_funcs.get_tls_address implementation.  */
+
+CORE_ADDR
+ctx_no_get_tls_address (void *baton, CORE_ADDR offset)
+{
+  error (_("%s is invalid in this context"), "DW_OP_form_tls_address");
+}
+
+/* Stub dwarf_expr_context_funcs.dwarf_call implementation.  */
+
+void
+ctx_no_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
+{
+  error (_("%s is invalid in this context"), "DW_OP_call*");
+}
+
+/* Stub dwarf_expr_context_funcs.get_base_type implementation.  */
+
+struct type *
+ctx_no_get_base_type (struct dwarf_expr_context *ctx, cu_offset die)
+{
+  error (_("Support for typed DWARF is not supported in this context"));
+}
+
+/* Stub dwarf_expr_context_funcs.push_dwarf_block_entry_value
+   implementation.  */
+
+void
+ctx_no_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
+                                  enum call_site_parameter_kind kind,
+                                  union call_site_parameter_u kind_u,
+                                  int deref_size)
+{
+  internal_error (__FILE__, __LINE__,
+                 _("Support for DW_OP_GNU_entry_value is unimplemented"));
+}
+
+/* Stub dwarf_expr_context_funcs.get_addr_index implementation.  */
+
+CORE_ADDR
+ctx_no_get_addr_index (void *baton, unsigned int index)
+{
+  error (_("%s is invalid in this context"), "DW_OP_GNU_addr_index");
+}
+
+/* Provide a prototype to silence -Wmissing-prototypes.  */
+extern initialize_file_ftype _initialize_dwarf2expr;
+
 void
 _initialize_dwarf2expr (void)
 {
This page took 0.043428 seconds and 4 git commands to generate.