*** empty log message ***
[binutils-gdb.git] / gdb / dwarf2loc.c
index 2bd45d9edd64acea0996af4b122559eca69935f5..79d22770d5f0696081dffa95a61e8b0643a72c4f 100644 (file)
@@ -44,7 +44,7 @@
 
 static void
 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
-                        gdb_byte **start, size_t *length);
+                        const gdb_byte **start, size_t *length);
 
 /* A helper function for dealing with location lists.  Given a
    symbol baton (BATON) and a pc value (PC), find the appropriate
@@ -54,12 +54,12 @@ dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
    For now, only return the first matching location expression; there
    can be more than one in the list.  */
 
-static gdb_byte *
+static const gdb_byte *
 find_location_expression (struct dwarf2_loclist_baton *baton,
                          size_t *locexpr_length, CORE_ADDR pc)
 {
   CORE_ADDR low, high;
-  gdb_byte *loc_ptr, *buf_end;
+  const gdb_byte *loc_ptr, *buf_end;
   int length;
   struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
@@ -153,7 +153,7 @@ dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
    describing the frame base.  Return a pointer to it in START and
    its length in LENGTH.  */
 static void
-dwarf_expr_frame_base (void *baton, gdb_byte **start, size_t * length)
+dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
 {
   /* FIXME: cagney/2003-03-26: This code should be using
      get_frame_base_address(), and then implement a dwarf2 specific
@@ -178,7 +178,7 @@ dwarf_expr_frame_base (void *baton, gdb_byte **start, size_t * length)
 
 static void
 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
-                        gdb_byte **start, size_t *length)
+                        const gdb_byte **start, size_t *length)
 {
   if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
     *start = NULL;
@@ -192,6 +192,7 @@ dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
   else
     {
       struct dwarf2_locexpr_baton *symbaton;
+
       symbaton = SYMBOL_LOCATION_BATON (framefunc);
       if (symbaton != NULL)
        {
@@ -214,6 +215,7 @@ static CORE_ADDR
 dwarf_expr_frame_cfa (void *baton)
 {
   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
+
   return dwarf2_frame_cfa (debaton->frame);
 }
 
@@ -229,6 +231,9 @@ dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
 
 struct piece_closure
 {
+  /* Reference count.  */
+  int refc;
+
   /* The number of pieces used to describe this variable.  */
   int n_pieces;
 
@@ -248,6 +253,7 @@ allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces,
 {
   struct piece_closure *c = XZALLOC (struct piece_closure);
 
+  c->refc = 1;
   c->n_pieces = n_pieces;
   c->addr_size = addr_size;
   c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
@@ -257,19 +263,245 @@ allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces,
   return c;
 }
 
+/* The lowest-level function to extract bits from a byte buffer.
+   SOURCE is the buffer.  It is updated if we read to the end of a
+   byte.
+   SOURCE_OFFSET_BITS is the offset of the first bit to read.  It is
+   updated to reflect the number of bits actually read.
+   NBITS is the number of bits we want to read.  It is updated to
+   reflect the number of bits actually read.  This function may read
+   fewer bits.
+   BITS_BIG_ENDIAN is taken directly from gdbarch.
+   This function returns the extracted bits.  */
+
+static unsigned int
+extract_bits_primitive (const gdb_byte **source,
+                       unsigned int *source_offset_bits,
+                       int *nbits, int bits_big_endian)
+{
+  unsigned int avail, mask, datum;
+
+  gdb_assert (*source_offset_bits < 8);
+
+  avail = 8 - *source_offset_bits;
+  if (avail > *nbits)
+    avail = *nbits;
+
+  mask = (1 << avail) - 1;
+  datum = **source;
+  if (bits_big_endian)
+    datum >>= 8 - (*source_offset_bits + *nbits);
+  else
+    datum >>= *source_offset_bits;
+  datum &= mask;
+
+  *nbits -= avail;
+  *source_offset_bits += avail;
+  if (*source_offset_bits >= 8)
+    {
+      *source_offset_bits -= 8;
+      ++*source;
+    }
+
+  return datum;
+}
+
+/* Extract some bits from a source buffer and move forward in the
+   buffer.
+   
+   SOURCE is the source buffer.  It is updated as bytes are read.
+   SOURCE_OFFSET_BITS is the offset into SOURCE.  It is updated as
+   bits are read.
+   NBITS is the number of bits to read.
+   BITS_BIG_ENDIAN is taken directly from gdbarch.
+   
+   This function returns the bits that were read.  */
+
+static unsigned int
+extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
+             int nbits, int bits_big_endian)
+{
+  unsigned int datum;
+
+  gdb_assert (nbits > 0 && nbits <= 8);
+
+  datum = extract_bits_primitive (source, source_offset_bits, &nbits,
+                                 bits_big_endian);
+  if (nbits > 0)
+    {
+      unsigned int more;
+
+      more = extract_bits_primitive (source, source_offset_bits, &nbits,
+                                    bits_big_endian);
+      if (bits_big_endian)
+       datum <<= nbits;
+      else
+       more <<= nbits;
+      datum |= more;
+    }
+
+  return datum;
+}
+
+/* Write some bits into a buffer and move forward in the buffer.
+   
+   DATUM is the bits to write.  The low-order bits of DATUM are used.
+   DEST is the destination buffer.  It is updated as bytes are
+   written.
+   DEST_OFFSET_BITS is the bit offset in DEST at which writing is
+   done.
+   NBITS is the number of valid bits in DATUM.
+   BITS_BIG_ENDIAN is taken directly from gdbarch.  */
+
+static void
+insert_bits (unsigned int datum,
+            gdb_byte *dest, unsigned int dest_offset_bits,
+            int nbits, int bits_big_endian)
+{
+  unsigned int mask;
+
+  gdb_assert (dest_offset_bits >= 0 && dest_offset_bits + nbits <= 8);
+
+  mask = (1 << nbits) - 1;
+  if (bits_big_endian)
+    {
+      datum <<= 8 - (dest_offset_bits + nbits);
+      mask <<= 8 - (dest_offset_bits + nbits);
+    }
+  else
+    {
+      datum <<= dest_offset_bits;
+      mask <<= dest_offset_bits;
+    }
+
+  gdb_assert ((datum & ~mask) == 0);
+
+  *dest = (*dest & ~mask) | datum;
+}
+
+/* Copy bits from a source to a destination.
+   
+   DEST is where the bits should be written.
+   DEST_OFFSET_BITS is the bit offset into DEST.
+   SOURCE is the source of bits.
+   SOURCE_OFFSET_BITS is the bit offset into SOURCE.
+   BIT_COUNT is the number of bits to copy.
+   BITS_BIG_ENDIAN is taken directly from gdbarch.  */
+
+static void
+copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
+             const gdb_byte *source, unsigned int source_offset_bits,
+             unsigned int bit_count,
+             int bits_big_endian)
+{
+  unsigned int dest_avail;
+  int datum;
+
+  /* Reduce everything to byte-size pieces.  */
+  dest += dest_offset_bits / 8;
+  dest_offset_bits %= 8;
+  source += source_offset_bits / 8;
+  source_offset_bits %= 8;
+
+  dest_avail = 8 - dest_offset_bits % 8;
+
+  /* See if we can fill the first destination byte.  */
+  if (dest_avail < bit_count)
+    {
+      datum = extract_bits (&source, &source_offset_bits, dest_avail,
+                           bits_big_endian);
+      insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
+      ++dest;
+      dest_offset_bits = 0;
+      bit_count -= dest_avail;
+    }
+
+  /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
+     than 8 bits remaining.  */
+  gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
+  for (; bit_count >= 8; bit_count -= 8)
+    {
+      datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
+      *dest++ = (gdb_byte) datum;
+    }
+
+  /* Finally, we may have a few leftover bits.  */
+  gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
+  if (bit_count > 0)
+    {
+      datum = extract_bits (&source, &source_offset_bits, bit_count,
+                           bits_big_endian);
+      insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
+    }
+}
+
 static void
 read_pieced_value (struct value *v)
 {
   int i;
   long offset = 0;
+  ULONGEST bits_to_skip;
   gdb_byte *contents;
   struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
+  size_t type_len;
+  size_t buffer_size = 0;
+  char *buffer = NULL;
+  struct cleanup *cleanup;
+  int bits_big_endian
+    = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
+
+  if (value_type (v) != value_enclosing_type (v))
+    internal_error (__FILE__, __LINE__,
+                   _("Should not be able to create a lazy value with "
+                     "an enclosing type"));
+
+  cleanup = make_cleanup (free_current_contents, &buffer);
 
   contents = value_contents_raw (v);
-  for (i = 0; i < c->n_pieces; i++)
+  bits_to_skip = 8 * value_offset (v);
+  type_len = 8 * TYPE_LENGTH (value_type (v));
+
+  for (i = 0; i < c->n_pieces && offset < type_len; i++)
     {
       struct dwarf_expr_piece *p = &c->pieces[i];
+      size_t this_size, this_size_bits;
+      long dest_offset_bits, source_offset_bits, source_offset;
+      const gdb_byte *intermediate_buffer;
+
+      /* Compute size, source, and destination offsets for copying, in
+        bits.  */
+      this_size_bits = p->size;
+      if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
+       {
+         bits_to_skip -= this_size_bits;
+         continue;
+       }
+      if (this_size_bits > type_len - offset)
+       this_size_bits = type_len - offset;
+      if (bits_to_skip > 0)
+       {
+         dest_offset_bits = 0;
+         source_offset_bits = bits_to_skip;
+         this_size_bits -= bits_to_skip;
+         bits_to_skip = 0;
+       }
+      else
+       {
+         dest_offset_bits = offset;
+         source_offset_bits = 0;
+       }
+
+      this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
+      source_offset = source_offset_bits / 8;
+      if (buffer_size < this_size)
+       {
+         buffer_size = this_size;
+         buffer = xrealloc (buffer, buffer_size);
+       }
+      intermediate_buffer = buffer;
+
+      /* Copy from the source to DEST_BUFFER.  */
       switch (p->location)
        {
        case DWARF_VALUE_REGISTER:
@@ -277,17 +509,22 @@ read_pieced_value (struct value *v)
            struct gdbarch *arch = get_frame_arch (frame);
            int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch,
                                                           p->v.expr.value);
-           int reg_offset = 0;
+           int reg_offset = source_offset;
 
            if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
-               && p->size < register_size (arch, gdb_regnum))
-             /* Big-endian, and we want less than full size.  */
-             reg_offset = register_size (arch, gdb_regnum) - p->size;
+               && this_size < register_size (arch, gdb_regnum))
+             {
+               /* Big-endian, and we want less than full size.  */
+               reg_offset = register_size (arch, gdb_regnum) - this_size;
+               /* We want the lower-order THIS_SIZE_BITS of the bytes
+                  we extract from the register.  */
+               source_offset_bits += 8 * this_size - this_size_bits;
+             }
 
            if (gdb_regnum != -1)
              {
                get_frame_register_bytes (frame, gdb_regnum, reg_offset, 
-                                         p->size, contents + offset);
+                                         this_size, buffer);
              }
            else
              {
@@ -299,37 +536,76 @@ read_pieced_value (struct value *v)
 
        case DWARF_VALUE_MEMORY:
          if (p->v.expr.in_stack_memory)
-           read_stack (p->v.expr.value, contents + offset, p->size);
+           read_stack (p->v.expr.value + source_offset, buffer, this_size);
          else
-           read_memory (p->v.expr.value, contents + offset, p->size);
+           read_memory (p->v.expr.value + source_offset, buffer, this_size);
          break;
 
        case DWARF_VALUE_STACK:
          {
            struct gdbarch *gdbarch = get_type_arch (value_type (v));
-           size_t n = p->size;
-           if (n > c->addr_size)
-             n = c->addr_size;
-           store_unsigned_integer (contents + offset, n,
-                                   gdbarch_byte_order (gdbarch),
-                                   p->v.expr.value);
+           size_t n = this_size;
+
+           if (n > c->addr_size - source_offset)
+             n = (c->addr_size >= source_offset
+                  ? c->addr_size - source_offset
+                  : 0);
+           if (n == 0)
+             {
+               /* Nothing.  */
+             }
+           else if (source_offset == 0)
+             store_unsigned_integer (buffer, n,
+                                     gdbarch_byte_order (gdbarch),
+                                     p->v.expr.value);
+           else
+             {
+               gdb_byte bytes[sizeof (ULONGEST)];
+
+               store_unsigned_integer (bytes, n + source_offset,
+                                       gdbarch_byte_order (gdbarch),
+                                       p->v.expr.value);
+               memcpy (buffer, bytes + source_offset, n);
+             }
          }
          break;
 
        case DWARF_VALUE_LITERAL:
          {
-           size_t n = p->size;
-           if (n > p->v.literal.length)
-             n = p->v.literal.length;
-           memcpy (contents + offset, p->v.literal.data, n);
+           size_t n = this_size;
+
+           if (n > p->v.literal.length - source_offset)
+             n = (p->v.literal.length >= source_offset
+                  ? p->v.literal.length - source_offset
+                  : 0);
+           if (n != 0)
+             intermediate_buffer = p->v.literal.data + source_offset;
          }
          break;
 
+       case DWARF_VALUE_OPTIMIZED_OUT:
+         /* We just leave the bits empty for now.  This is not ideal
+            but gdb currently does not have a nice way to represent
+            optimized-out pieces.  */
+         warning (_("bits %ld-%ld in computed object were optimized out; "
+                    "replacing with zeroes"),
+                  offset,
+                  offset + (long) this_size_bits);
+         break;
+
        default:
          internal_error (__FILE__, __LINE__, _("invalid location type"));
        }
-      offset += p->size;
+
+      if (p->location != DWARF_VALUE_OPTIMIZED_OUT)
+       copy_bitwise (contents, dest_offset_bits,
+                     intermediate_buffer, source_offset_bits % 8,
+                     this_size_bits, bits_big_endian);
+
+      offset += this_size_bits;
     }
+
+  do_cleanups (cleanup);
 }
 
 static void
@@ -337,9 +613,16 @@ write_pieced_value (struct value *to, struct value *from)
 {
   int i;
   long offset = 0;
-  gdb_byte *contents;
+  ULONGEST bits_to_skip;
+  const gdb_byte *contents;
   struct piece_closure *c = (struct piece_closure *) value_computed_closure (to);
   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
+  size_t type_len;
+  size_t buffer_size = 0;
+  char *buffer = NULL;
+  struct cleanup *cleanup;
+  int bits_big_endian
+    = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
 
   if (frame == NULL)
     {
@@ -347,27 +630,86 @@ write_pieced_value (struct value *to, struct value *from)
       return;
     }
 
-  contents = value_contents_raw (from);
-  for (i = 0; i < c->n_pieces; i++)
+  cleanup = make_cleanup (free_current_contents, &buffer);
+
+  contents = value_contents (from);
+  bits_to_skip = 8 * value_offset (to);
+  type_len = 8 * TYPE_LENGTH (value_type (to));
+  for (i = 0; i < c->n_pieces && offset < type_len; i++)
     {
       struct dwarf_expr_piece *p = &c->pieces[i];
+      size_t this_size_bits, this_size;
+      long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
+      int need_bitwise;
+      const gdb_byte *source_buffer;
+
+      this_size_bits = p->size;
+      if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
+       {
+         bits_to_skip -= this_size_bits;
+         continue;
+       }
+      if (this_size_bits > type_len - offset)
+       this_size_bits = type_len - offset;
+      if (bits_to_skip > 0)
+       {
+         dest_offset_bits = bits_to_skip;
+         source_offset_bits = 0;
+         this_size_bits -= bits_to_skip;
+         bits_to_skip = 0;
+       }
+      else
+       {
+         dest_offset_bits = 0;
+         source_offset_bits = offset;
+       }
+
+      this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
+      source_offset = source_offset_bits / 8;
+      dest_offset = dest_offset_bits / 8;
+      if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
+       {
+         source_buffer = contents + source_offset;
+         need_bitwise = 0;
+       }
+      else
+       {
+         if (buffer_size < this_size)
+           {
+             buffer_size = this_size;
+             buffer = xrealloc (buffer, buffer_size);
+           }
+         source_buffer = buffer;
+         need_bitwise = 1;
+       }
+
       switch (p->location)
        {
        case DWARF_VALUE_REGISTER:
          {
            struct gdbarch *arch = get_frame_arch (frame);
            int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.expr.value);
-           int reg_offset = 0;
+           int reg_offset = dest_offset;
 
            if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
-               && p->size < register_size (arch, gdb_regnum))
+               && this_size <= register_size (arch, gdb_regnum))
              /* Big-endian, and we want less than full size.  */
-             reg_offset = register_size (arch, gdb_regnum) - p->size;
+             reg_offset = register_size (arch, gdb_regnum) - this_size;
 
            if (gdb_regnum != -1)
              {
+               if (need_bitwise)
+                 {
+                   get_frame_register_bytes (frame, gdb_regnum, reg_offset,
+                                             this_size, buffer);
+                   copy_bitwise (buffer, dest_offset_bits,
+                                 contents, source_offset_bits,
+                                 this_size_bits,
+                                 bits_big_endian);
+                 }
+
                put_frame_register_bytes (frame, gdb_regnum, reg_offset, 
-                                         p->size, contents + offset);
+                                         this_size, source_buffer);
              }
            else
              {
@@ -377,14 +719,31 @@ write_pieced_value (struct value *to, struct value *from)
          }
          break;
        case DWARF_VALUE_MEMORY:
-         write_memory (p->v.expr.value, contents + offset, p->size);
+         if (need_bitwise)
+           {
+             /* Only the first and last bytes can possibly have any
+                bits reused.  */
+             read_memory (p->v.expr.value + dest_offset, buffer, 1);
+             read_memory (p->v.expr.value + dest_offset + this_size - 1,
+                          buffer + this_size - 1, 1);
+             copy_bitwise (buffer, dest_offset_bits,
+                           contents, source_offset_bits,
+                           this_size_bits,
+                           bits_big_endian);
+           }
+
+         write_memory (p->v.expr.value + dest_offset,
+                       source_buffer, this_size);
          break;
        default:
          set_value_optimized_out (to, 1);
-         return;
+         goto done;
        }
-      offset += p->size;
+      offset += this_size_bits;
     }
+
+ done:
+  do_cleanups (cleanup);
 }
 
 static void *
@@ -392,7 +751,8 @@ copy_pieced_value_closure (struct value *v)
 {
   struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
   
-  return allocate_piece_closure (c->n_pieces, c->pieces, c->addr_size);
+  ++c->refc;
+  return c;
 }
 
 static void
@@ -400,8 +760,12 @@ free_pieced_value_closure (struct value *v)
 {
   struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
 
-  xfree (c->pieces);
-  xfree (c);
+  --c->refc;
+  if (c->refc == 0)
+    {
+      xfree (c->pieces);
+      xfree (c);
+    }
 }
 
 /* Functions for accessing a variable described by DW_OP_piece.  */
@@ -413,11 +777,12 @@ static struct lval_funcs pieced_value_funcs = {
 };
 
 /* Evaluate a location description, starting at DATA and with length
-   SIZE, to find the current location of variable VAR in the context
+   SIZE, to find the current location of variable of TYPE in the context
    of FRAME.  */
+
 static struct value *
-dwarf2_evaluate_loc_desc (struct symbol *var, struct frame_info *frame,
-                         gdb_byte *data, unsigned short size,
+dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
+                         const gdb_byte *data, unsigned short size,
                          struct dwarf2_per_cu_data *per_cu)
 {
   struct value *retval;
@@ -427,7 +792,7 @@ dwarf2_evaluate_loc_desc (struct symbol *var, struct frame_info *frame,
 
   if (size == 0)
     {
-      retval = allocate_value (SYMBOL_TYPE (var));
+      retval = allocate_value (type);
       VALUE_LVAL (retval) = not_lval;
       set_value_optimized_out (retval, 1);
       return retval;
@@ -456,9 +821,7 @@ dwarf2_evaluate_loc_desc (struct symbol *var, struct frame_info *frame,
 
       c = allocate_piece_closure (ctx->num_pieces, ctx->pieces,
                                  ctx->addr_size);
-      retval = allocate_computed_value (SYMBOL_TYPE (var),
-                                       &pieced_value_funcs,
-                                       c);
+      retval = allocate_computed_value (type, &pieced_value_funcs, c);
       VALUE_FRAME_ID (retval) = frame_id;
     }
   else
@@ -470,16 +833,12 @@ dwarf2_evaluate_loc_desc (struct symbol *var, struct frame_info *frame,
            struct gdbarch *arch = get_frame_arch (frame);
            CORE_ADDR dwarf_regnum = dwarf_expr_fetch (ctx, 0);
            int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
+
            if (gdb_regnum != -1)
-             {
-               retval = value_from_register (SYMBOL_TYPE (var),
-                                             gdb_regnum, frame);
-             }
+             retval = value_from_register (type, gdb_regnum, frame);
            else
-             {
-               error (_("Unable to access DWARF register number %s"),
-                      paddress (arch, dwarf_regnum));
-             }
+             error (_("Unable to access DWARF register number %s"),
+                    paddress (arch, dwarf_regnum));
          }
          break;
 
@@ -488,7 +847,7 @@ dwarf2_evaluate_loc_desc (struct symbol *var, struct frame_info *frame,
            CORE_ADDR address = dwarf_expr_fetch (ctx, 0);
            int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
 
-           retval = allocate_value (SYMBOL_TYPE (var));
+           retval = allocate_value (type);
            VALUE_LVAL (retval) = lval_memory;
            set_value_lazy (retval, 1);
            if (in_stack_memory)
@@ -503,10 +862,10 @@ dwarf2_evaluate_loc_desc (struct symbol *var, struct frame_info *frame,
            bfd_byte *contents;
            size_t n = ctx->addr_size;
 
-           retval = allocate_value (SYMBOL_TYPE (var));
+           retval = allocate_value (type);
            contents = value_contents_raw (retval);
-           if (n > TYPE_LENGTH (SYMBOL_TYPE (var)))
-             n = TYPE_LENGTH (SYMBOL_TYPE (var));
+           if (n > TYPE_LENGTH (type))
+             n = TYPE_LENGTH (type);
            store_unsigned_integer (contents, n,
                                    gdbarch_byte_order (ctx->gdbarch),
                                    value);
@@ -518,14 +877,17 @@ dwarf2_evaluate_loc_desc (struct symbol *var, struct frame_info *frame,
            bfd_byte *contents;
            size_t n = ctx->len;
 
-           retval = allocate_value (SYMBOL_TYPE (var));
+           retval = allocate_value (type);
            contents = value_contents_raw (retval);
-           if (n > TYPE_LENGTH (SYMBOL_TYPE (var)))
-             n = TYPE_LENGTH (SYMBOL_TYPE (var));
+           if (n > TYPE_LENGTH (type))
+             n = TYPE_LENGTH (type);
            memcpy (contents, ctx->data, n);
          }
          break;
 
+         /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
+            it can only be encountered when making a piece.  */
+       case DWARF_VALUE_OPTIMIZED_OUT:
        default:
          internal_error (__FILE__, __LINE__, _("invalid location type"));
        }
@@ -550,6 +912,7 @@ static CORE_ADDR
 needs_frame_read_reg (void *baton, int regnum)
 {
   struct needs_frame_baton *nf_baton = baton;
+
   nf_baton->needs_frame = 1;
   return 1;
 }
@@ -563,7 +926,7 @@ needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
 
 /* Frame-relative accesses do require a frame.  */
 static void
-needs_frame_frame_base (void *baton, gdb_byte **start, size_t * length)
+needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
 {
   static gdb_byte lit0 = DW_OP_lit0;
   struct needs_frame_baton *nf_baton = baton;
@@ -580,6 +943,7 @@ static CORE_ADDR
 needs_frame_frame_cfa (void *baton)
 {
   struct needs_frame_baton *nf_baton = baton;
+
   nf_baton->needs_frame = 1;
   return 1;
 }
@@ -589,6 +953,7 @@ static CORE_ADDR
 needs_frame_tls_address (void *baton, CORE_ADDR offset)
 {
   struct needs_frame_baton *nf_baton = baton;
+
   nf_baton->needs_frame = 1;
   return 1;
 }
@@ -597,7 +962,7 @@ needs_frame_tls_address (void *baton, CORE_ADDR offset)
    requires a frame to evaluate.  */
 
 static int
-dwarf2_loc_desc_needs_frame (gdb_byte *data, unsigned short size,
+dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
                             struct dwarf2_per_cu_data *per_cu)
 {
   struct needs_frame_baton baton;
@@ -661,12 +1026,12 @@ struct axs_var_loc
   LONGEST offset;
 };
 
-static gdb_byte *
+static const gdb_byte *
 dwarf2_tracepoint_var_loc (struct symbol *symbol,
                           struct agent_expr *ax,
                           struct axs_var_loc *loc,
                           struct gdbarch *gdbarch,
-                          gdb_byte *data, gdb_byte *end)
+                          const gdb_byte *data, const gdb_byte *end)
 {
   if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
     {
@@ -677,6 +1042,7 @@ dwarf2_tracepoint_var_loc (struct symbol *symbol,
   else if (data[0] == DW_OP_regx)
     {
       ULONGEST reg;
+
       data = read_uleb128 (data + 1, end, &reg);
       loc->kind = axs_lvalue_register;
       loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
@@ -687,7 +1053,7 @@ dwarf2_tracepoint_var_loc (struct symbol *symbol,
       struct symbol *framefunc;
       int frame_reg = 0;
       LONGEST frame_offset;
-      gdb_byte *base_data;
+      const gdb_byte *base_data;
       size_t base_size;
       LONGEST base_offset = 0;
 
@@ -706,8 +1072,8 @@ dwarf2_tracepoint_var_loc (struct symbol *symbol,
 
       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
        {
-         gdb_byte *buf_end;
-         
+         const gdb_byte *buf_end;
+
          frame_reg = base_data[0] - DW_OP_breg0;
          buf_end = read_sleb128 (base_data + 1,
                                  base_data + base_size, &base_offset);
@@ -786,9 +1152,9 @@ dwarf2_tracepoint_var_access (struct agent_expr *ax,
 static void
 dwarf2_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
                           struct agent_expr *ax, struct axs_value *value,
-                          gdb_byte *data, int size)
+                          const gdb_byte *data, int size)
 {
-  gdb_byte *end = data + size;
+  const gdb_byte *end = data + size;
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   /* In practice, a variable is not going to be spread across
      dozens of registers or memory locations.  If someone comes up
@@ -908,8 +1274,9 @@ locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
 {
   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
   struct value *val;
-  val = dwarf2_evaluate_loc_desc (symbol, frame, dlbaton->data, dlbaton->size,
-                                 dlbaton->per_cu);
+
+  val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
+                                 dlbaton->size, dlbaton->per_cu);
 
   return val;
 }
@@ -919,6 +1286,7 @@ static int
 locexpr_read_needs_frame (struct symbol *symbol)
 {
   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
+
   return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
                                      dlbaton->per_cu);
 }
@@ -926,10 +1294,11 @@ locexpr_read_needs_frame (struct symbol *symbol)
 /* Describe a single piece of a location, returning an updated
    position in the bytecode sequence.  */
 
-static gdb_byte *
+static const gdb_byte *
 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
                                 CORE_ADDR addr, struct objfile *objfile,
-                                gdb_byte *data, int size, unsigned int addr_size)
+                                const gdb_byte *data, int size,
+                                unsigned int addr_size)
 {
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
   int regno;
@@ -956,7 +1325,7 @@ locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
       struct symbol *framefunc;
       int frame_reg = 0;
       LONGEST frame_offset;
-      gdb_byte *base_data;
+      const gdb_byte *base_data;
       size_t base_size;
       LONGEST base_offset = 0;
 
@@ -976,7 +1345,7 @@ locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
 
       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
        {
-         gdb_byte *buf_end;
+         const gdb_byte *buf_end;
          
          frame_reg = base_data[0] - DW_OP_breg0;
          buf_end = read_sleb128 (base_data + 1,
@@ -1043,6 +1412,7 @@ locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
                                              data + 1,
                                              data + size - 1,
                                              addr_size);
+
       fprintf_filtered (stream, 
                        _("a thread-local variable at offset %s "
                          "in the thread-local storage for `%s'"),
@@ -1062,10 +1432,11 @@ locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
 
 static void
 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
-                            struct ui_file *stream, gdb_byte *data, int size,
+                            struct ui_file *stream,
+                            const gdb_byte *data, int size,
                             struct objfile *objfile, unsigned int addr_size)
 {
-  gdb_byte *end = data + size;
+  const gdb_byte *end = data + size;
   int piece_done = 0, first_piece = 1, bad = 0;
 
   /* A multi-piece description consists of multiple sequences of bytes
@@ -1153,7 +1524,7 @@ loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
 {
   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
   struct value *val;
-  gdb_byte *data;
+  const gdb_byte *data;
   size_t size;
 
   data = find_location_expression (dlbaton, &size,
@@ -1166,7 +1537,7 @@ loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
       set_value_optimized_out (val, 1);
     }
   else
-    val = dwarf2_evaluate_loc_desc (symbol, frame, data, size,
+    val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
                                    dlbaton->per_cu);
 
   return val;
@@ -1195,7 +1566,7 @@ loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
 {
   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
   CORE_ADDR low, high;
-  gdb_byte *loc_ptr, *buf_end;
+  const gdb_byte *loc_ptr, *buf_end;
   int length, first = 1;
   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
@@ -1277,7 +1648,7 @@ loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
                            struct agent_expr *ax, struct axs_value *value)
 {
   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
-  gdb_byte *data;
+  const gdb_byte *data;
   size_t size;
 
   data = find_location_expression (dlbaton, &size, ax->scope);