OSDN Git Service

*** empty log message ***
[pf3gnuchains/sourceware.git] / gdb / valops.c
index fdbb702..0c07e5c 100644 (file)
@@ -38,7 +38,7 @@
 #include "cp-support.h"
 #include "dfp.h"
 #include "user-regs.h"
-
+#include "tracepoint.h"
 #include <errno.h>
 #include "gdb_string.h"
 #include "gdb_assert.h"
@@ -368,7 +368,7 @@ value_cast (struct type *type, struct value *arg2)
   if (code1 == TYPE_CODE_REF)
     {
       /* We dereference type; then we recurse and finally
-         we generate value of the given reference. Nothing wrong with 
+         we generate value of the given reference.  Nothing wrong with 
         that.  */
       struct type *t1 = check_typedef (type);
       struct type *dereftype = check_typedef (TYPE_TARGET_TYPE (t1));
@@ -652,8 +652,10 @@ value_reinterpret_cast (struct type *type, struct value *arg)
 
 static int
 dynamic_cast_check_1 (struct type *desired_type,
-                     const bfd_byte *contents,
+                     const gdb_byte *valaddr,
+                     int embedded_offset,
                      CORE_ADDR address,
+                     struct value *val,
                      struct type *search_type,
                      CORE_ADDR arg_addr,
                      struct type *arg_type,
@@ -663,25 +665,25 @@ dynamic_cast_check_1 (struct type *desired_type,
 
   for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
     {
-      int offset = baseclass_offset (search_type, i, contents, address);
+      int offset = baseclass_offset (search_type, i, valaddr, embedded_offset,
+                                    address, val);
 
-      if (offset == -1)
-       error (_("virtual baseclass botch"));
       if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
        {
-         if (address + offset >= arg_addr
-             && address + offset < arg_addr + TYPE_LENGTH (arg_type))
+         if (address + embedded_offset + offset >= arg_addr
+             && address + embedded_offset + offset < arg_addr + TYPE_LENGTH (arg_type))
            {
              ++result_count;
              if (!*result)
                *result = value_at_lazy (TYPE_BASECLASS (search_type, i),
-                                        address + offset);
+                                        address + embedded_offset + offset);
            }
        }
       else
        result_count += dynamic_cast_check_1 (desired_type,
-                                             contents + offset,
-                                             address + offset,
+                                             valaddr,
+                                             embedded_offset + offset,
+                                             address, val,
                                              TYPE_BASECLASS (search_type, i),
                                              arg_addr,
                                              arg_type,
@@ -697,8 +699,10 @@ dynamic_cast_check_1 (struct type *desired_type,
 
 static int
 dynamic_cast_check_2 (struct type *desired_type,
-                     const bfd_byte *contents,
+                     const gdb_byte *valaddr,
+                     int embedded_offset,
                      CORE_ADDR address,
+                     struct value *val,
                      struct type *search_type,
                      struct value **result)
 {
@@ -711,20 +715,20 @@ dynamic_cast_check_2 (struct type *desired_type,
       if (! BASETYPE_VIA_PUBLIC (search_type, i))
        continue;
 
-      offset = baseclass_offset (search_type, i, contents, address);
-      if (offset == -1)
-       error (_("virtual baseclass botch"));
+      offset = baseclass_offset (search_type, i, valaddr, embedded_offset,
+                                address, val);
       if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
        {
          ++result_count;
          if (*result == NULL)
            *result = value_at_lazy (TYPE_BASECLASS (search_type, i),
-                                    address + offset);
+                                    address + embedded_offset + offset);
        }
       else
        result_count += dynamic_cast_check_2 (desired_type,
-                                             contents + offset,
-                                             address + offset,
+                                             valaddr,
+                                             embedded_offset + offset,
+                                             address, val,
                                              TYPE_BASECLASS (search_type, i),
                                              result);
     }
@@ -822,7 +826,9 @@ value_dynamic_cast (struct type *type, struct value *arg)
        return tem;
       result = NULL;
       if (dynamic_cast_check_1 (TYPE_TARGET_TYPE (resolved_type),
-                               value_contents (tem), value_address (tem),
+                               value_contents_for_printing (tem),
+                               value_embedded_offset (tem),
+                               value_address (tem), tem,
                                rtti_type, addr,
                                arg_type,
                                &result) == 1)
@@ -834,7 +840,9 @@ value_dynamic_cast (struct type *type, struct value *arg)
   result = NULL;
   if (is_public_ancestor (arg_type, rtti_type)
       && dynamic_cast_check_2 (TYPE_TARGET_TYPE (resolved_type),
-                              value_contents (tem), value_address (tem),
+                              value_contents_for_printing (tem),
+                              value_embedded_offset (tem),
+                              value_address (tem), tem,
                               rtti_type, &result) == 1)
     return value_cast (type,
                       is_ref ? value_ref (result) : value_addr (result));
@@ -917,18 +925,10 @@ get_value_at (struct type *type, CORE_ADDR addr, int lazy)
   if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
     error (_("Attempt to dereference a generic pointer."));
 
-  if (lazy)
-    {
-      val = allocate_value_lazy (type);
-    }
-  else
-    {
-      val = allocate_value (type);
-      read_memory (addr, value_contents_all_raw (val), TYPE_LENGTH (type));
-    }
+  val = value_from_contents_and_address (type, NULL, addr);
 
-  VALUE_LVAL (val) = lval_memory;
-  set_value_address (val, addr);
+  if (!lazy)
+    value_fetch_lazy (val);
 
   return val;
 }
@@ -989,11 +989,7 @@ value_fetch_lazy (struct value *val)
       enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
       struct value *parent = value_parent (val);
       LONGEST offset = value_offset (val);
-      LONGEST num = unpack_bits_as_long (value_type (val),
-                                        (value_contents_for_printing (parent)
-                                         + offset),
-                                        value_bitpos (val),
-                                        value_bitsize (val));
+      LONGEST num;
       int length = TYPE_LENGTH (type);
 
       if (!value_bits_valid (val,
@@ -1001,7 +997,17 @@ value_fetch_lazy (struct value *val)
                             value_bitsize (val)))
        error (_("value has been optimized out"));
 
-      store_signed_integer (value_contents_raw (val), length, byte_order, num);
+      if (!unpack_value_bits_as_long (value_type (val),
+                                     value_contents_for_printing (parent),
+                                     offset,
+                                     value_bitpos (val),
+                                     value_bitsize (val), parent, &num))
+       mark_value_bytes_unavailable (val,
+                                     value_embedded_offset (val),
+                                     length);
+      else
+       store_signed_integer (value_contents_raw (val), length,
+                             byte_order, num);
     }
   else if (VALUE_LVAL (val) == lval_memory)
     {
@@ -1009,12 +1015,8 @@ value_fetch_lazy (struct value *val)
       int length = TYPE_LENGTH (check_typedef (value_enclosing_type (val)));
 
       if (length)
-       {
-         if (value_stack (val))
-           read_stack (addr, value_contents_all_raw (val), length);
-         else
-           read_memory (addr, value_contents_all_raw (val), length);
-       }
+       read_value_memory (val, 0, value_stack (val),
+                          addr, value_contents_all_raw (val), length);
     }
   else if (VALUE_LVAL (val) == lval_register)
     {
@@ -1050,12 +1052,16 @@ value_fetch_lazy (struct value *val)
       if (value_lazy (new_val))
        value_fetch_lazy (new_val);
 
-      /* If the register was not saved, mark it unavailable.  */
+      /* If the register was not saved, mark it optimized out.  */
       if (value_optimized_out (new_val))
        set_value_optimized_out (val, 1);
       else
-       memcpy (value_contents_raw (val), value_contents (new_val),
-               TYPE_LENGTH (type));
+       {
+         set_value_lazy (val, 0);
+         value_contents_copy (val, value_embedded_offset (val),
+                              new_val, value_embedded_offset (new_val),
+                              TYPE_LENGTH (type));
+       }
 
       if (frame_debug)
        {
@@ -1104,13 +1110,98 @@ value_fetch_lazy (struct value *val)
     }
   else if (VALUE_LVAL (val) == lval_computed)
     value_computed_funcs (val)->read (val);
+  else if (value_optimized_out (val))
+    /* Keep it optimized out.  */;
   else
-    internal_error (__FILE__, __LINE__, "Unexpected lazy value type.");
+    internal_error (__FILE__, __LINE__, _("Unexpected lazy value type."));
 
   set_value_lazy (val, 0);
   return 0;
 }
 
+void
+read_value_memory (struct value *val, int embedded_offset,
+                  int stack, CORE_ADDR memaddr,
+                  gdb_byte *buffer, size_t length)
+{
+  if (length)
+    {
+      VEC(mem_range_s) *available_memory;
+
+      if (get_traceframe_number () < 0
+         || !traceframe_available_memory (&available_memory, memaddr, length))
+       {
+         if (stack)
+           read_stack (memaddr, buffer, length);
+         else
+           read_memory (memaddr, buffer, length);
+       }
+      else
+       {
+         struct target_section_table *table;
+         struct cleanup *old_chain;
+         CORE_ADDR unavail;
+         mem_range_s *r;
+         int i;
+
+         /* Fallback to reading from read-only sections.  */
+         table = target_get_section_table (&exec_ops);
+         available_memory =
+           section_table_available_memory (available_memory,
+                                           memaddr, length,
+                                           table->sections,
+                                           table->sections_end);
+
+         old_chain = make_cleanup (VEC_cleanup(mem_range_s),
+                                   &available_memory);
+
+         normalize_mem_ranges (available_memory);
+
+         /* Mark which bytes are unavailable, and read those which
+            are available.  */
+
+         unavail = memaddr;
+
+         for (i = 0;
+              VEC_iterate (mem_range_s, available_memory, i, r);
+              i++)
+           {
+             if (mem_ranges_overlap (r->start, r->length,
+                                     memaddr, length))
+               {
+                 CORE_ADDR lo1, hi1, lo2, hi2;
+                 CORE_ADDR start, end;
+
+                 /* Get the intersection window.  */
+                 lo1 = memaddr;
+                 hi1 = memaddr + length;
+                 lo2 = r->start;
+                 hi2 = r->start + r->length;
+                 start = max (lo1, lo2);
+                 end = min (hi1, hi2);
+
+                 gdb_assert (end - memaddr <= length);
+
+                 if (start > unavail)
+                   mark_value_bytes_unavailable (val,
+                                                 (embedded_offset
+                                                  + unavail - memaddr),
+                                                 start - unavail);
+                 unavail = end;
+
+                 read_memory (start, buffer + start - memaddr, end - start);
+               }
+           }
+
+         if (unavail != memaddr + length)
+           mark_value_bytes_unavailable (val,
+                                         embedded_offset + unavail - memaddr,
+                                         (memaddr + length) - unavail);
+
+         do_cleanups (old_chain);
+       }
+    }
+}
 
 /* Store the contents of FROMVAL into the location of TOVAL.
    Return a new value with the location of TOVAL and contents of FROMVAL.  */
@@ -1240,6 +1331,7 @@ value_assign (struct value *toval, struct value *fromval)
                int offset = value_offset (parent) + value_offset (toval);
                int changed_len;
                gdb_byte buffer[sizeof (LONGEST)];
+               int optim, unavail;
 
                changed_len = (value_bitpos (toval)
                               + value_bitsize (toval)
@@ -1373,12 +1465,13 @@ value_repeat (struct value *arg1, int count)
 
   val = allocate_repeat_value (value_enclosing_type (arg1), count);
 
-  read_memory (value_address (arg1),
-              value_contents_all_raw (val),
-              TYPE_LENGTH (value_enclosing_type (val)));
   VALUE_LVAL (val) = lval_memory;
   set_value_address (val, value_address (arg1));
 
+  read_value_memory (val, 0, value_stack (val), value_address (val),
+                    value_contents_all_raw (val),
+                    TYPE_LENGTH (value_enclosing_type (val)));
+
   return val;
 }
 
@@ -1420,7 +1513,7 @@ address_of_variable (struct symbol *var, struct block *b)
   struct value *val;
 
   /* Evaluate it first; if the result is a memory address, we're fine.
-     Lazy evaluation pays off here. */
+     Lazy evaluation pays off here.  */
 
   val = value_of_variable (var, b);
 
@@ -1528,8 +1621,7 @@ value_coerce_to_target (struct value *val)
    other than array subscripting, where the caller would get back a
    value that had an address somewhere before the actual first element
    of the array, and the information about the lower bound would be
-   lost because of the coercion to pointer type.
- */
+   lost because of the coercion to pointer type.  */
 
 struct value *
 value_coerce_array (struct value *arg1)
@@ -1593,7 +1685,7 @@ value_addr (struct value *arg1)
   if (VALUE_LVAL (arg1) != lval_memory)
     error (_("Attempt to take address of value not located in memory."));
 
-  /* Get target memory address */
+  /* Get target memory address */
   arg2 = value_from_pointer (lookup_pointer_type (value_type (arg1)),
                             (value_address (arg1)
                              + value_embedded_offset (arg1)));
@@ -1667,7 +1759,7 @@ value_ind (struct value *arg1)
        arg2 = value_at_lazy (enc_type, 
                              find_function_addr (arg1, NULL));
       else
-       /* Retrieve the enclosing object pointed to */
+       /* Retrieve the enclosing object pointed to */
        arg2 = value_at_lazy (enc_type, 
                              (value_as_address (arg1)
                               - value_pointed_to_offset (arg1)));
@@ -1687,9 +1779,8 @@ value_ind (struct value *arg1)
   return 0;                    /* For lint -- never reached.  */
 }
 \f
-/* Create a value for an array by allocating space in GDB, copying
-   copying the data into that space, and then setting up an array
-   value.
+/* Create a value for an array by allocating space in GDB, copying the
+   data into that space, and then setting up an array value.
 
    The array bounds are set from LOWBOUND and HIGHBOUND, and the array
    is populated from the values passed in ELEMVEC.
@@ -1731,11 +1822,8 @@ value_array (int lowbound, int highbound, struct value **elemvec)
     {
       val = allocate_value (arraytype);
       for (idx = 0; idx < nelem; idx++)
-       {
-         memcpy (value_contents_all_raw (val) + (idx * typelength),
-                 value_contents_all (elemvec[idx]),
-                 typelength);
-       }
+       value_contents_copy (val, idx * typelength, elemvec[idx], 0,
+                            typelength);
       return val;
     }
 
@@ -1744,9 +1832,7 @@ value_array (int lowbound, int highbound, struct value **elemvec)
 
   val = allocate_value (arraytype);
   for (idx = 0; idx < nelem; idx++)
-    memcpy (value_contents_writeable (val) + (idx * typelength),
-           value_contents_all (elemvec[idx]),
-           typelength);
+    value_contents_copy (val, idx * typelength, elemvec[idx], 0, typelength);
   return val;
 }
 
@@ -1891,7 +1977,7 @@ typecmp (int staticp, int varargs, int nargs,
 }
 
 /* Helper function used by value_struct_elt to recurse through
-   baseclasses.  Look for a field NAME in ARG1. Adjust the address of
+   baseclasses.  Look for a field NAME in ARG1.  Adjust the address of
    ARG1 by OFFSET bytes, and search in it assuming it has (class) type
    TYPE.  If found, return value, else return NULL.
 
@@ -1998,15 +2084,13 @@ search_struct_field (const char *name, struct value *arg1, int offset,
          struct value *v2;
 
          boffset = baseclass_offset (type, i,
-                                     value_contents (arg1) + offset,
-                                     value_address (arg1)
-                                     + value_embedded_offset (arg1)
-                                     + offset);
-         if (boffset == -1)
-           error (_("virtual baseclass botch"));
+                                     value_contents_for_printing (arg1),
+                                     value_embedded_offset (arg1) + offset,
+                                     value_address (arg1),
+                                     arg1);
 
          /* The virtual base class pointer might have been clobbered
-            by the user program. Make sure that it still points to a
+            by the user program.  Make sure that it still points to a
             valid memory location.  */
 
          boffset += value_embedded_offset (arg1) + offset;
@@ -2051,7 +2135,7 @@ search_struct_field (const char *name, struct value *arg1, int offset,
 }
 
 /* Helper function used by value_struct_elt to recurse through
-   baseclasses.  Look for a field NAME in ARG1. Adjust the address of
+   baseclasses.  Look for a field NAME in ARG1.  Adjust the address of
    ARG1 by OFFSET bytes, and search in it assuming it has (class) type
    TYPE.
 
@@ -2073,7 +2157,7 @@ search_struct_method (const char *name, struct value **arg1p,
     {
       char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
 
-      /* FIXME!  May need to check for ARM demangling here */
+      /* FIXME!  May need to check for ARM demangling here */
       if (strncmp (t_field_name, "__", 2) == 0 ||
          strncmp (t_field_name, "op", 2) == 0 ||
          strncmp (t_field_name, "type", 4) == 0)
@@ -2125,32 +2209,44 @@ search_struct_method (const char *name, struct value **arg1p,
   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
     {
       int base_offset;
+      int skip = 0;
+      int this_offset;
 
       if (BASETYPE_VIA_VIRTUAL (type, i))
        {
          struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
+         struct value *base_val;
          const gdb_byte *base_valaddr;
 
          /* The virtual base class pointer might have been
-            clobbered by the user program. Make sure that it
+            clobbered by the user program.  Make sure that it
            still points to a valid memory location.  */
 
          if (offset < 0 || offset >= TYPE_LENGTH (type))
            {
              gdb_byte *tmp = alloca (TYPE_LENGTH (baseclass));
+             CORE_ADDR address = value_address (*arg1p);
 
-             if (target_read_memory (value_address (*arg1p) + offset,
+             if (target_read_memory (address + offset,
                                      tmp, TYPE_LENGTH (baseclass)) != 0)
                error (_("virtual baseclass botch"));
-             base_valaddr = tmp;
+
+             base_val = value_from_contents_and_address (baseclass,
+                                                         tmp,
+                                                         address + offset);
+             base_valaddr = value_contents_for_printing (base_val);
+             this_offset = 0;
            }
          else
-           base_valaddr = value_contents (*arg1p) + offset;
+           {
+             base_val = *arg1p;
+             base_valaddr = value_contents_for_printing (*arg1p);
+             this_offset = offset;
+           }
 
          base_offset = baseclass_offset (type, i, base_valaddr,
-                                         value_address (*arg1p) + offset);
-         if (base_offset == -1)
-           error (_("virtual baseclass botch"));
+                                         this_offset, value_address (base_val),
+                                         base_val);
        }
       else
        {
@@ -2181,7 +2277,7 @@ search_struct_method (const char *name, struct value **arg1p,
    ERR is used in the error message if *ARGP's type is wrong.
 
    C++: ARGS is a list of argument types to aid in the selection of
-   an appropriate method. Also, handle derived types.
+   an appropriate method.  Also, handle derived types.
 
    STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
    where the truthvalue of whether the function that was resolved was
@@ -2286,8 +2382,7 @@ value_struct_elt (struct value **argp, struct value **args,
    NUM_FNS is the number of overloaded instances.
    BASETYPE is set to the actual type of the subobject where the
       method is found.
-   BOFFSET is the offset of the base subobject where the method is found.
-*/
+   BOFFSET is the offset of the base subobject where the method is found.  */
 
 static struct fn_field *
 find_method_list (struct value **argp, const char *method,
@@ -2329,12 +2424,10 @@ find_method_list (struct value **argp, const char *method,
 
       if (BASETYPE_VIA_VIRTUAL (type, i))
        {
-         base_offset = value_offset (*argp) + offset;
          base_offset = baseclass_offset (type, i,
-                                         value_contents (*argp) + base_offset,
-                                         value_address (*argp) + base_offset);
-         if (base_offset == -1)
-           error (_("virtual baseclass botch"));
+                                         value_contents_for_printing (*argp),
+                                         value_offset (*argp) + offset,
+                                         value_address (*argp), *argp);
        }
       else /* Non-virtual base, simply use bit position from debug
              info.  */
@@ -2358,8 +2451,7 @@ find_method_list (struct value **argp, const char *method,
    NUM_FNS is the number of overloaded instances.
    BASETYPE is set to the type of the base subobject that defines the
       method.
-   BOFFSET is the offset of the base subobject which defines the method. 
-*/
+   BOFFSET is the offset of the base subobject which defines the method.  */
 
 struct fn_field *
 value_find_oload_method_list (struct value **argp, const char *method,
@@ -2402,7 +2494,7 @@ value_find_oload_method_list (struct value **argp, const char *method,
      METHOD: for member functions.
      BOTH: used for overload resolution of operators where the
        candidates are expected to be either member or non member
-       functions. In this case the first argument ARGTYPES
+       functions.  In this case the first argument ARGTYPES
        (representing 'this') is expected to be a reference to the
        target object, and will be dereferenced when attempting the
        non-member search.
@@ -2429,8 +2521,7 @@ value_find_oload_method_list (struct value **argp, const char *method,
 
    Note: This function does *not* check the value of
    overload_resolution.  Caller must check it to see whether overload
-   resolution is permitted.
-*/
+   resolution is permitted.  */
 
 int
 find_overload_match (struct type **arg_types, int nargs, 
@@ -2525,9 +2616,9 @@ find_overload_match (struct type **arg_types, int nargs,
     {
       const char *qualified_name = NULL;
 
-      /* If the the overload match is being search for both
-         as a method and non member function, the first argument
-         must now be dereferenced.  */
+      /* If the overload match is being search for both as a method
+         and non member function, the first argument must now be
+         dereferenced.  */
       if (method == BOTH)
        arg_types[0] = TYPE_TARGET_TYPE (arg_types[0]);
 
@@ -2602,9 +2693,9 @@ find_overload_match (struct type **arg_types, int nargs,
       switch (compare_badness (func_badness, method_badness))
         {
          case 0: /* Top two contenders are equally good.  */
-           /* FIXME: GDB does not support the general ambiguous
-            case.  All candidates should be collected and presented
-            the the user.  */
+           /* FIXME: GDB does not support the general ambiguous case.
+            All candidates should be collected and presented the
+            user.  */
            error (_("Ambiguous overload resolution"));
            break;
          case 1: /* Incomparable top contenders.  */
@@ -2770,7 +2861,7 @@ find_oload_champ_namespace_loop (struct type **arg_types, int nargs,
   *oload_syms = NULL;
   *oload_champ_bv = NULL;
 
-  /* First, see if we have a deeper namespace we can search in.  
+  /* First, see if we have a deeper namespace we can search in.
      If we get a good match there, use it.  */
 
   if (qualified_name[next_namespace_len] == ':')
@@ -3086,7 +3177,7 @@ value_aggregate_elt (struct type *curtype, char *name,
 }
 
 /* Compares the two method/function types T1 and T2 for "equality" 
-   with respect to the the methods' parameters.  If the types of the
+   with respect to the methods' parameters.  If the types of the
    two parameter lists are the same, returns 1; 0 otherwise.  This
    comparison may ignore any artificial parameters in T1 if
    SKIP_ARTIFICIAL is non-zero.  This function will ALWAYS skip
@@ -3099,11 +3190,11 @@ compare_parameters (struct type *t1, struct type *t2, int skip_artificial)
 {
   int start = 0;
 
-  if (TYPE_FIELD_ARTIFICIAL (t1, 0))
+  if (TYPE_NFIELDS (t1) > 0 && TYPE_FIELD_ARTIFICIAL (t1, 0))
     ++start;
 
   /* If skipping artificial fields, find the first real field
-     in T1. */
+     in T1.  */
   if (skip_artificial)
     {
       while (start < TYPE_NFIELDS (t1)
@@ -3111,7 +3202,7 @@ compare_parameters (struct type *t1, struct type *t2, int skip_artificial)
        ++start;
     }
 
-  /* Now compare parameters */
+  /* Now compare parameters */
 
   /* Special case: a method taking void.  T1 will contain no
      non-artificial fields, and T2 will contain TYPE_CODE_VOID.  */
@@ -3464,7 +3555,7 @@ value_full_object (struct value *argp,
       return argp;
     }
 
-  /* Check if object is in memory */
+  /* Check if object is in memory */
   if (VALUE_LVAL (argp) != lval_memory)
     {
       warning (_("Couldn't retrieve complete object of RTTI "
@@ -3636,9 +3727,8 @@ value_slice (struct value *array, int lowbound, int length)
       else
        {
          slice = allocate_value (slice_type);
-         memcpy (value_contents_writeable (slice),
-                 value_contents (array) + offset,
-                 TYPE_LENGTH (slice_type));
+         value_contents_copy (slice, 0, array, offset,
+                              TYPE_LENGTH (slice_type));
        }
 
       set_value_component_location (slice, array);