xref: /dflybsd-src/contrib/gdb-7/gdb/findvar.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Find a variable's value in memory, for GDB, the GNU debugger.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1986-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "symtab.h"
225796c8dcSSimon Schubert #include "gdbtypes.h"
235796c8dcSSimon Schubert #include "frame.h"
245796c8dcSSimon Schubert #include "value.h"
255796c8dcSSimon Schubert #include "gdbcore.h"
265796c8dcSSimon Schubert #include "inferior.h"
275796c8dcSSimon Schubert #include "target.h"
285796c8dcSSimon Schubert #include "gdb_string.h"
295796c8dcSSimon Schubert #include "gdb_assert.h"
305796c8dcSSimon Schubert #include "floatformat.h"
315796c8dcSSimon Schubert #include "symfile.h"		/* for overlay functions */
325796c8dcSSimon Schubert #include "regcache.h"
335796c8dcSSimon Schubert #include "user-regs.h"
345796c8dcSSimon Schubert #include "block.h"
355796c8dcSSimon Schubert #include "objfiles.h"
36*ef5ccd6cSJohn Marino #include "language.h"
375796c8dcSSimon Schubert 
38cf7f2e2dSJohn Marino /* Basic byte-swapping routines.  All 'extract' functions return a
39cf7f2e2dSJohn Marino    host-format integer from a target-format integer at ADDR which is
40cf7f2e2dSJohn Marino    LEN bytes long.  */
415796c8dcSSimon Schubert 
425796c8dcSSimon Schubert #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
435796c8dcSSimon Schubert   /* 8 bit characters are a pretty safe assumption these days, so we
445796c8dcSSimon Schubert      assume it throughout all these swapping routines.  If we had to deal with
455796c8dcSSimon Schubert      9 bit characters, we would need to make len be in bits and would have
465796c8dcSSimon Schubert      to re-write these routines...  */
475796c8dcSSimon Schubert you lose
485796c8dcSSimon Schubert #endif
495796c8dcSSimon Schubert 
505796c8dcSSimon Schubert LONGEST
extract_signed_integer(const gdb_byte * addr,int len,enum bfd_endian byte_order)515796c8dcSSimon Schubert extract_signed_integer (const gdb_byte *addr, int len,
525796c8dcSSimon Schubert 			enum bfd_endian byte_order)
535796c8dcSSimon Schubert {
545796c8dcSSimon Schubert   LONGEST retval;
555796c8dcSSimon Schubert   const unsigned char *p;
565796c8dcSSimon Schubert   const unsigned char *startaddr = addr;
575796c8dcSSimon Schubert   const unsigned char *endaddr = startaddr + len;
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert   if (len > (int) sizeof (LONGEST))
605796c8dcSSimon Schubert     error (_("\
615796c8dcSSimon Schubert That operation is not available on integers of more than %d bytes."),
625796c8dcSSimon Schubert 	   (int) sizeof (LONGEST));
635796c8dcSSimon Schubert 
645796c8dcSSimon Schubert   /* Start at the most significant end of the integer, and work towards
655796c8dcSSimon Schubert      the least significant.  */
665796c8dcSSimon Schubert   if (byte_order == BFD_ENDIAN_BIG)
675796c8dcSSimon Schubert     {
685796c8dcSSimon Schubert       p = startaddr;
695796c8dcSSimon Schubert       /* Do the sign extension once at the start.  */
705796c8dcSSimon Schubert       retval = ((LONGEST) * p ^ 0x80) - 0x80;
715796c8dcSSimon Schubert       for (++p; p < endaddr; ++p)
725796c8dcSSimon Schubert 	retval = (retval << 8) | *p;
735796c8dcSSimon Schubert     }
745796c8dcSSimon Schubert   else
755796c8dcSSimon Schubert     {
765796c8dcSSimon Schubert       p = endaddr - 1;
775796c8dcSSimon Schubert       /* Do the sign extension once at the start.  */
785796c8dcSSimon Schubert       retval = ((LONGEST) * p ^ 0x80) - 0x80;
795796c8dcSSimon Schubert       for (--p; p >= startaddr; --p)
805796c8dcSSimon Schubert 	retval = (retval << 8) | *p;
815796c8dcSSimon Schubert     }
825796c8dcSSimon Schubert   return retval;
835796c8dcSSimon Schubert }
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert ULONGEST
extract_unsigned_integer(const gdb_byte * addr,int len,enum bfd_endian byte_order)865796c8dcSSimon Schubert extract_unsigned_integer (const gdb_byte *addr, int len,
875796c8dcSSimon Schubert 			  enum bfd_endian byte_order)
885796c8dcSSimon Schubert {
895796c8dcSSimon Schubert   ULONGEST retval;
905796c8dcSSimon Schubert   const unsigned char *p;
915796c8dcSSimon Schubert   const unsigned char *startaddr = addr;
925796c8dcSSimon Schubert   const unsigned char *endaddr = startaddr + len;
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert   if (len > (int) sizeof (ULONGEST))
955796c8dcSSimon Schubert     error (_("\
965796c8dcSSimon Schubert That operation is not available on integers of more than %d bytes."),
975796c8dcSSimon Schubert 	   (int) sizeof (ULONGEST));
985796c8dcSSimon Schubert 
995796c8dcSSimon Schubert   /* Start at the most significant end of the integer, and work towards
1005796c8dcSSimon Schubert      the least significant.  */
1015796c8dcSSimon Schubert   retval = 0;
1025796c8dcSSimon Schubert   if (byte_order == BFD_ENDIAN_BIG)
1035796c8dcSSimon Schubert     {
1045796c8dcSSimon Schubert       for (p = startaddr; p < endaddr; ++p)
1055796c8dcSSimon Schubert 	retval = (retval << 8) | *p;
1065796c8dcSSimon Schubert     }
1075796c8dcSSimon Schubert   else
1085796c8dcSSimon Schubert     {
1095796c8dcSSimon Schubert       for (p = endaddr - 1; p >= startaddr; --p)
1105796c8dcSSimon Schubert 	retval = (retval << 8) | *p;
1115796c8dcSSimon Schubert     }
1125796c8dcSSimon Schubert   return retval;
1135796c8dcSSimon Schubert }
1145796c8dcSSimon Schubert 
1155796c8dcSSimon Schubert /* Sometimes a long long unsigned integer can be extracted as a
1165796c8dcSSimon Schubert    LONGEST value.  This is done so that we can print these values
1175796c8dcSSimon Schubert    better.  If this integer can be converted to a LONGEST, this
1185796c8dcSSimon Schubert    function returns 1 and sets *PVAL.  Otherwise it returns 0.  */
1195796c8dcSSimon Schubert 
1205796c8dcSSimon Schubert int
extract_long_unsigned_integer(const gdb_byte * addr,int orig_len,enum bfd_endian byte_order,LONGEST * pval)1215796c8dcSSimon Schubert extract_long_unsigned_integer (const gdb_byte *addr, int orig_len,
1225796c8dcSSimon Schubert 			       enum bfd_endian byte_order, LONGEST *pval)
1235796c8dcSSimon Schubert {
1245796c8dcSSimon Schubert   const gdb_byte *p;
1255796c8dcSSimon Schubert   const gdb_byte *first_addr;
1265796c8dcSSimon Schubert   int len;
1275796c8dcSSimon Schubert 
1285796c8dcSSimon Schubert   len = orig_len;
1295796c8dcSSimon Schubert   if (byte_order == BFD_ENDIAN_BIG)
1305796c8dcSSimon Schubert     {
1315796c8dcSSimon Schubert       for (p = addr;
1325796c8dcSSimon Schubert 	   len > (int) sizeof (LONGEST) && p < addr + orig_len;
1335796c8dcSSimon Schubert 	   p++)
1345796c8dcSSimon Schubert 	{
1355796c8dcSSimon Schubert 	  if (*p == 0)
1365796c8dcSSimon Schubert 	    len--;
1375796c8dcSSimon Schubert 	  else
1385796c8dcSSimon Schubert 	    break;
1395796c8dcSSimon Schubert 	}
1405796c8dcSSimon Schubert       first_addr = p;
1415796c8dcSSimon Schubert     }
1425796c8dcSSimon Schubert   else
1435796c8dcSSimon Schubert     {
1445796c8dcSSimon Schubert       first_addr = addr;
1455796c8dcSSimon Schubert       for (p = addr + orig_len - 1;
1465796c8dcSSimon Schubert 	   len > (int) sizeof (LONGEST) && p >= addr;
1475796c8dcSSimon Schubert 	   p--)
1485796c8dcSSimon Schubert 	{
1495796c8dcSSimon Schubert 	  if (*p == 0)
1505796c8dcSSimon Schubert 	    len--;
1515796c8dcSSimon Schubert 	  else
1525796c8dcSSimon Schubert 	    break;
1535796c8dcSSimon Schubert 	}
1545796c8dcSSimon Schubert     }
1555796c8dcSSimon Schubert 
1565796c8dcSSimon Schubert   if (len <= (int) sizeof (LONGEST))
1575796c8dcSSimon Schubert     {
1585796c8dcSSimon Schubert       *pval = (LONGEST) extract_unsigned_integer (first_addr,
1595796c8dcSSimon Schubert 						  sizeof (LONGEST),
1605796c8dcSSimon Schubert 						  byte_order);
1615796c8dcSSimon Schubert       return 1;
1625796c8dcSSimon Schubert     }
1635796c8dcSSimon Schubert 
1645796c8dcSSimon Schubert   return 0;
1655796c8dcSSimon Schubert }
1665796c8dcSSimon Schubert 
1675796c8dcSSimon Schubert 
1685796c8dcSSimon Schubert /* Treat the bytes at BUF as a pointer of type TYPE, and return the
1695796c8dcSSimon Schubert    address it represents.  */
1705796c8dcSSimon Schubert CORE_ADDR
extract_typed_address(const gdb_byte * buf,struct type * type)1715796c8dcSSimon Schubert extract_typed_address (const gdb_byte *buf, struct type *type)
1725796c8dcSSimon Schubert {
1735796c8dcSSimon Schubert   if (TYPE_CODE (type) != TYPE_CODE_PTR
1745796c8dcSSimon Schubert       && TYPE_CODE (type) != TYPE_CODE_REF)
1755796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
1765796c8dcSSimon Schubert 		    _("extract_typed_address: "
1775796c8dcSSimon Schubert 		    "type is not a pointer or reference"));
1785796c8dcSSimon Schubert 
1795796c8dcSSimon Schubert   return gdbarch_pointer_to_address (get_type_arch (type), type, buf);
1805796c8dcSSimon Schubert }
1815796c8dcSSimon Schubert 
182cf7f2e2dSJohn Marino /* All 'store' functions accept a host-format integer and store a
183cf7f2e2dSJohn Marino    target-format integer at ADDR which is LEN bytes long.  */
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert void
store_signed_integer(gdb_byte * addr,int len,enum bfd_endian byte_order,LONGEST val)1865796c8dcSSimon Schubert store_signed_integer (gdb_byte *addr, int len,
1875796c8dcSSimon Schubert 		      enum bfd_endian byte_order, LONGEST val)
1885796c8dcSSimon Schubert {
1895796c8dcSSimon Schubert   gdb_byte *p;
1905796c8dcSSimon Schubert   gdb_byte *startaddr = addr;
1915796c8dcSSimon Schubert   gdb_byte *endaddr = startaddr + len;
1925796c8dcSSimon Schubert 
1935796c8dcSSimon Schubert   /* Start at the least significant end of the integer, and work towards
1945796c8dcSSimon Schubert      the most significant.  */
1955796c8dcSSimon Schubert   if (byte_order == BFD_ENDIAN_BIG)
1965796c8dcSSimon Schubert     {
1975796c8dcSSimon Schubert       for (p = endaddr - 1; p >= startaddr; --p)
1985796c8dcSSimon Schubert 	{
1995796c8dcSSimon Schubert 	  *p = val & 0xff;
2005796c8dcSSimon Schubert 	  val >>= 8;
2015796c8dcSSimon Schubert 	}
2025796c8dcSSimon Schubert     }
2035796c8dcSSimon Schubert   else
2045796c8dcSSimon Schubert     {
2055796c8dcSSimon Schubert       for (p = startaddr; p < endaddr; ++p)
2065796c8dcSSimon Schubert 	{
2075796c8dcSSimon Schubert 	  *p = val & 0xff;
2085796c8dcSSimon Schubert 	  val >>= 8;
2095796c8dcSSimon Schubert 	}
2105796c8dcSSimon Schubert     }
2115796c8dcSSimon Schubert }
2125796c8dcSSimon Schubert 
2135796c8dcSSimon Schubert void
store_unsigned_integer(gdb_byte * addr,int len,enum bfd_endian byte_order,ULONGEST val)2145796c8dcSSimon Schubert store_unsigned_integer (gdb_byte *addr, int len,
2155796c8dcSSimon Schubert 			enum bfd_endian byte_order, ULONGEST val)
2165796c8dcSSimon Schubert {
2175796c8dcSSimon Schubert   unsigned char *p;
2185796c8dcSSimon Schubert   unsigned char *startaddr = (unsigned char *) addr;
2195796c8dcSSimon Schubert   unsigned char *endaddr = startaddr + len;
2205796c8dcSSimon Schubert 
2215796c8dcSSimon Schubert   /* Start at the least significant end of the integer, and work towards
2225796c8dcSSimon Schubert      the most significant.  */
2235796c8dcSSimon Schubert   if (byte_order == BFD_ENDIAN_BIG)
2245796c8dcSSimon Schubert     {
2255796c8dcSSimon Schubert       for (p = endaddr - 1; p >= startaddr; --p)
2265796c8dcSSimon Schubert 	{
2275796c8dcSSimon Schubert 	  *p = val & 0xff;
2285796c8dcSSimon Schubert 	  val >>= 8;
2295796c8dcSSimon Schubert 	}
2305796c8dcSSimon Schubert     }
2315796c8dcSSimon Schubert   else
2325796c8dcSSimon Schubert     {
2335796c8dcSSimon Schubert       for (p = startaddr; p < endaddr; ++p)
2345796c8dcSSimon Schubert 	{
2355796c8dcSSimon Schubert 	  *p = val & 0xff;
2365796c8dcSSimon Schubert 	  val >>= 8;
2375796c8dcSSimon Schubert 	}
2385796c8dcSSimon Schubert     }
2395796c8dcSSimon Schubert }
2405796c8dcSSimon Schubert 
2415796c8dcSSimon Schubert /* Store the address ADDR as a pointer of type TYPE at BUF, in target
2425796c8dcSSimon Schubert    form.  */
2435796c8dcSSimon Schubert void
store_typed_address(gdb_byte * buf,struct type * type,CORE_ADDR addr)2445796c8dcSSimon Schubert store_typed_address (gdb_byte *buf, struct type *type, CORE_ADDR addr)
2455796c8dcSSimon Schubert {
2465796c8dcSSimon Schubert   if (TYPE_CODE (type) != TYPE_CODE_PTR
2475796c8dcSSimon Schubert       && TYPE_CODE (type) != TYPE_CODE_REF)
2485796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
2495796c8dcSSimon Schubert 		    _("store_typed_address: "
2505796c8dcSSimon Schubert 		    "type is not a pointer or reference"));
2515796c8dcSSimon Schubert 
2525796c8dcSSimon Schubert   gdbarch_address_to_pointer (get_type_arch (type), type, buf, addr);
2535796c8dcSSimon Schubert }
2545796c8dcSSimon Schubert 
2555796c8dcSSimon Schubert 
2565796c8dcSSimon Schubert 
2575796c8dcSSimon Schubert /* Return a `value' with the contents of (virtual or cooked) register
2585796c8dcSSimon Schubert    REGNUM as found in the specified FRAME.  The register's type is
2595796c8dcSSimon Schubert    determined by register_type().  */
2605796c8dcSSimon Schubert 
2615796c8dcSSimon Schubert struct value *
value_of_register(int regnum,struct frame_info * frame)2625796c8dcSSimon Schubert value_of_register (int regnum, struct frame_info *frame)
2635796c8dcSSimon Schubert {
2645796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (frame);
2655796c8dcSSimon Schubert   CORE_ADDR addr;
2665796c8dcSSimon Schubert   int optim;
267c50c785cSJohn Marino   int unavail;
2685796c8dcSSimon Schubert   struct value *reg_val;
2695796c8dcSSimon Schubert   int realnum;
2705796c8dcSSimon Schubert   gdb_byte raw_buffer[MAX_REGISTER_SIZE];
2715796c8dcSSimon Schubert   enum lval_type lval;
2725796c8dcSSimon Schubert 
2735796c8dcSSimon Schubert   /* User registers lie completely outside of the range of normal
2745796c8dcSSimon Schubert      registers.  Catch them early so that the target never sees them.  */
2755796c8dcSSimon Schubert   if (regnum >= gdbarch_num_regs (gdbarch)
2765796c8dcSSimon Schubert 		+ gdbarch_num_pseudo_regs (gdbarch))
2775796c8dcSSimon Schubert     return value_of_user_reg (regnum, frame);
2785796c8dcSSimon Schubert 
279c50c785cSJohn Marino   frame_register (frame, regnum, &optim, &unavail,
280c50c785cSJohn Marino 		  &lval, &addr, &realnum, raw_buffer);
2815796c8dcSSimon Schubert 
2825796c8dcSSimon Schubert   reg_val = allocate_value (register_type (gdbarch, regnum));
2835796c8dcSSimon Schubert 
284c50c785cSJohn Marino   if (!optim && !unavail)
2855796c8dcSSimon Schubert     memcpy (value_contents_raw (reg_val), raw_buffer,
2865796c8dcSSimon Schubert 	    register_size (gdbarch, regnum));
287c50c785cSJohn Marino   else
288c50c785cSJohn Marino     memset (value_contents_raw (reg_val), 0,
289c50c785cSJohn Marino 	    register_size (gdbarch, regnum));
290c50c785cSJohn Marino 
2915796c8dcSSimon Schubert   VALUE_LVAL (reg_val) = lval;
2925796c8dcSSimon Schubert   set_value_address (reg_val, addr);
2935796c8dcSSimon Schubert   VALUE_REGNUM (reg_val) = regnum;
2945796c8dcSSimon Schubert   set_value_optimized_out (reg_val, optim);
295c50c785cSJohn Marino   if (unavail)
296c50c785cSJohn Marino     mark_value_bytes_unavailable (reg_val, 0, register_size (gdbarch, regnum));
2975796c8dcSSimon Schubert   VALUE_FRAME_ID (reg_val) = get_frame_id (frame);
2985796c8dcSSimon Schubert   return reg_val;
2995796c8dcSSimon Schubert }
3005796c8dcSSimon Schubert 
3015796c8dcSSimon Schubert /* Return a `value' with the contents of (virtual or cooked) register
3025796c8dcSSimon Schubert    REGNUM as found in the specified FRAME.  The register's type is
3035796c8dcSSimon Schubert    determined by register_type().  The value is not fetched.  */
3045796c8dcSSimon Schubert 
3055796c8dcSSimon Schubert struct value *
value_of_register_lazy(struct frame_info * frame,int regnum)3065796c8dcSSimon Schubert value_of_register_lazy (struct frame_info *frame, int regnum)
3075796c8dcSSimon Schubert {
3085796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (frame);
3095796c8dcSSimon Schubert   struct value *reg_val;
3105796c8dcSSimon Schubert 
3115796c8dcSSimon Schubert   gdb_assert (regnum < (gdbarch_num_regs (gdbarch)
3125796c8dcSSimon Schubert 			+ gdbarch_num_pseudo_regs (gdbarch)));
3135796c8dcSSimon Schubert 
3145796c8dcSSimon Schubert   /* We should have a valid (i.e. non-sentinel) frame.  */
3155796c8dcSSimon Schubert   gdb_assert (frame_id_p (get_frame_id (frame)));
3165796c8dcSSimon Schubert 
317c50c785cSJohn Marino   reg_val = allocate_value_lazy (register_type (gdbarch, regnum));
3185796c8dcSSimon Schubert   VALUE_LVAL (reg_val) = lval_register;
3195796c8dcSSimon Schubert   VALUE_REGNUM (reg_val) = regnum;
3205796c8dcSSimon Schubert   VALUE_FRAME_ID (reg_val) = get_frame_id (frame);
3215796c8dcSSimon Schubert   return reg_val;
3225796c8dcSSimon Schubert }
3235796c8dcSSimon Schubert 
3245796c8dcSSimon Schubert /* Given a pointer of type TYPE in target form in BUF, return the
3255796c8dcSSimon Schubert    address it represents.  */
3265796c8dcSSimon Schubert CORE_ADDR
unsigned_pointer_to_address(struct gdbarch * gdbarch,struct type * type,const gdb_byte * buf)3275796c8dcSSimon Schubert unsigned_pointer_to_address (struct gdbarch *gdbarch,
3285796c8dcSSimon Schubert 			     struct type *type, const gdb_byte *buf)
3295796c8dcSSimon Schubert {
3305796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
331cf7f2e2dSJohn Marino 
3325796c8dcSSimon Schubert   return extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
3335796c8dcSSimon Schubert }
3345796c8dcSSimon Schubert 
3355796c8dcSSimon Schubert CORE_ADDR
signed_pointer_to_address(struct gdbarch * gdbarch,struct type * type,const gdb_byte * buf)3365796c8dcSSimon Schubert signed_pointer_to_address (struct gdbarch *gdbarch,
3375796c8dcSSimon Schubert 			   struct type *type, const gdb_byte *buf)
3385796c8dcSSimon Schubert {
3395796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
340cf7f2e2dSJohn Marino 
3415796c8dcSSimon Schubert   return extract_signed_integer (buf, TYPE_LENGTH (type), byte_order);
3425796c8dcSSimon Schubert }
3435796c8dcSSimon Schubert 
3445796c8dcSSimon Schubert /* Given an address, store it as a pointer of type TYPE in target
3455796c8dcSSimon Schubert    format in BUF.  */
3465796c8dcSSimon Schubert void
unsigned_address_to_pointer(struct gdbarch * gdbarch,struct type * type,gdb_byte * buf,CORE_ADDR addr)3475796c8dcSSimon Schubert unsigned_address_to_pointer (struct gdbarch *gdbarch, struct type *type,
3485796c8dcSSimon Schubert 			     gdb_byte *buf, CORE_ADDR addr)
3495796c8dcSSimon Schubert {
3505796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
351cf7f2e2dSJohn Marino 
3525796c8dcSSimon Schubert   store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr);
3535796c8dcSSimon Schubert }
3545796c8dcSSimon Schubert 
3555796c8dcSSimon Schubert void
address_to_signed_pointer(struct gdbarch * gdbarch,struct type * type,gdb_byte * buf,CORE_ADDR addr)3565796c8dcSSimon Schubert address_to_signed_pointer (struct gdbarch *gdbarch, struct type *type,
3575796c8dcSSimon Schubert 			   gdb_byte *buf, CORE_ADDR addr)
3585796c8dcSSimon Schubert {
3595796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
360cf7f2e2dSJohn Marino 
3615796c8dcSSimon Schubert   store_signed_integer (buf, TYPE_LENGTH (type), byte_order, addr);
3625796c8dcSSimon Schubert }
3635796c8dcSSimon Schubert 
3645796c8dcSSimon Schubert /* Will calling read_var_value or locate_var_value on SYM end
3655796c8dcSSimon Schubert    up caring what frame it is being evaluated relative to?  SYM must
3665796c8dcSSimon Schubert    be non-NULL.  */
3675796c8dcSSimon Schubert int
symbol_read_needs_frame(struct symbol * sym)3685796c8dcSSimon Schubert symbol_read_needs_frame (struct symbol *sym)
3695796c8dcSSimon Schubert {
3705796c8dcSSimon Schubert   switch (SYMBOL_CLASS (sym))
3715796c8dcSSimon Schubert     {
3725796c8dcSSimon Schubert       /* All cases listed explicitly so that gcc -Wall will detect it if
3735796c8dcSSimon Schubert          we failed to consider one.  */
3745796c8dcSSimon Schubert     case LOC_COMPUTED:
3755796c8dcSSimon Schubert       /* FIXME: cagney/2004-01-26: It should be possible to
3765796c8dcSSimon Schubert 	 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
3775796c8dcSSimon Schubert 	 Unfortunately DWARF 2 stores the frame-base (instead of the
3785796c8dcSSimon Schubert 	 function) location in a function's symbol.  Oops!  For the
3795796c8dcSSimon Schubert 	 moment enable this when/where applicable.  */
3805796c8dcSSimon Schubert       return SYMBOL_COMPUTED_OPS (sym)->read_needs_frame (sym);
3815796c8dcSSimon Schubert 
3825796c8dcSSimon Schubert     case LOC_REGISTER:
3835796c8dcSSimon Schubert     case LOC_ARG:
3845796c8dcSSimon Schubert     case LOC_REF_ARG:
3855796c8dcSSimon Schubert     case LOC_REGPARM_ADDR:
3865796c8dcSSimon Schubert     case LOC_LOCAL:
3875796c8dcSSimon Schubert       return 1;
3885796c8dcSSimon Schubert 
3895796c8dcSSimon Schubert     case LOC_UNDEF:
3905796c8dcSSimon Schubert     case LOC_CONST:
3915796c8dcSSimon Schubert     case LOC_STATIC:
3925796c8dcSSimon Schubert     case LOC_TYPEDEF:
3935796c8dcSSimon Schubert 
3945796c8dcSSimon Schubert     case LOC_LABEL:
3955796c8dcSSimon Schubert       /* Getting the address of a label can be done independently of the block,
3965796c8dcSSimon Schubert          even if some *uses* of that address wouldn't work so well without
3975796c8dcSSimon Schubert          the right frame.  */
3985796c8dcSSimon Schubert 
3995796c8dcSSimon Schubert     case LOC_BLOCK:
4005796c8dcSSimon Schubert     case LOC_CONST_BYTES:
4015796c8dcSSimon Schubert     case LOC_UNRESOLVED:
4025796c8dcSSimon Schubert     case LOC_OPTIMIZED_OUT:
4035796c8dcSSimon Schubert       return 0;
4045796c8dcSSimon Schubert     }
4055796c8dcSSimon Schubert   return 1;
4065796c8dcSSimon Schubert }
4075796c8dcSSimon Schubert 
408*ef5ccd6cSJohn Marino /* Private data to be used with minsym_lookup_iterator_cb.  */
409*ef5ccd6cSJohn Marino 
410*ef5ccd6cSJohn Marino struct minsym_lookup_data
411*ef5ccd6cSJohn Marino {
412*ef5ccd6cSJohn Marino   /* The name of the minimal symbol we are searching for.  */
413*ef5ccd6cSJohn Marino   const char *name;
414*ef5ccd6cSJohn Marino 
415*ef5ccd6cSJohn Marino   /* The field where the callback should store the minimal symbol
416*ef5ccd6cSJohn Marino      if found.  It should be initialized to NULL before the search
417*ef5ccd6cSJohn Marino      is started.  */
418*ef5ccd6cSJohn Marino   struct minimal_symbol *result;
419*ef5ccd6cSJohn Marino };
420*ef5ccd6cSJohn Marino 
421*ef5ccd6cSJohn Marino /* A callback function for gdbarch_iterate_over_objfiles_in_search_order.
422*ef5ccd6cSJohn Marino    It searches by name for a minimal symbol within the given OBJFILE.
423*ef5ccd6cSJohn Marino    The arguments are passed via CB_DATA, which in reality is a pointer
424*ef5ccd6cSJohn Marino    to struct minsym_lookup_data.  */
425*ef5ccd6cSJohn Marino 
426*ef5ccd6cSJohn Marino static int
minsym_lookup_iterator_cb(struct objfile * objfile,void * cb_data)427*ef5ccd6cSJohn Marino minsym_lookup_iterator_cb (struct objfile *objfile, void *cb_data)
428*ef5ccd6cSJohn Marino {
429*ef5ccd6cSJohn Marino   struct minsym_lookup_data *data = (struct minsym_lookup_data *) cb_data;
430*ef5ccd6cSJohn Marino 
431*ef5ccd6cSJohn Marino   gdb_assert (data->result == NULL);
432*ef5ccd6cSJohn Marino 
433*ef5ccd6cSJohn Marino   data->result = lookup_minimal_symbol (data->name, NULL, objfile);
434*ef5ccd6cSJohn Marino 
435*ef5ccd6cSJohn Marino   /* The iterator should stop iff a match was found.  */
436*ef5ccd6cSJohn Marino   return (data->result != NULL);
437*ef5ccd6cSJohn Marino }
438*ef5ccd6cSJohn Marino 
439*ef5ccd6cSJohn Marino /* A default implementation for the "la_read_var_value" hook in
440*ef5ccd6cSJohn Marino    the language vector which should work in most situations.  */
4415796c8dcSSimon Schubert 
4425796c8dcSSimon Schubert struct value *
default_read_var_value(struct symbol * var,struct frame_info * frame)443*ef5ccd6cSJohn Marino default_read_var_value (struct symbol *var, struct frame_info *frame)
4445796c8dcSSimon Schubert {
4455796c8dcSSimon Schubert   struct value *v;
4465796c8dcSSimon Schubert   struct type *type = SYMBOL_TYPE (var);
4475796c8dcSSimon Schubert   CORE_ADDR addr;
4485796c8dcSSimon Schubert 
449c50c785cSJohn Marino   /* Call check_typedef on our type to make sure that, if TYPE is
450c50c785cSJohn Marino      a TYPE_CODE_TYPEDEF, its length is set to the length of the target type
451c50c785cSJohn Marino      instead of zero.  However, we do not replace the typedef type by the
452c50c785cSJohn Marino      target type, because we want to keep the typedef in order to be able to
453c50c785cSJohn Marino      set the returned value type description correctly.  */
454c50c785cSJohn Marino   check_typedef (type);
4555796c8dcSSimon Schubert 
4565796c8dcSSimon Schubert   if (symbol_read_needs_frame (var))
4575796c8dcSSimon Schubert     gdb_assert (frame);
4585796c8dcSSimon Schubert 
4595796c8dcSSimon Schubert   switch (SYMBOL_CLASS (var))
4605796c8dcSSimon Schubert     {
4615796c8dcSSimon Schubert     case LOC_CONST:
4625796c8dcSSimon Schubert       /* Put the constant back in target format.  */
463c50c785cSJohn Marino       v = allocate_value (type);
464*ef5ccd6cSJohn Marino       store_signed_integer (value_contents_raw (v), TYPE_LENGTH (type),
4655796c8dcSSimon Schubert 			    gdbarch_byte_order (get_type_arch (type)),
4665796c8dcSSimon Schubert 			    (LONGEST) SYMBOL_VALUE (var));
4675796c8dcSSimon Schubert       VALUE_LVAL (v) = not_lval;
4685796c8dcSSimon Schubert       return v;
4695796c8dcSSimon Schubert 
4705796c8dcSSimon Schubert     case LOC_LABEL:
4715796c8dcSSimon Schubert       /* Put the constant back in target format.  */
472c50c785cSJohn Marino       v = allocate_value (type);
4735796c8dcSSimon Schubert       if (overlay_debugging)
4745796c8dcSSimon Schubert 	{
4755796c8dcSSimon Schubert 	  CORE_ADDR addr
4765796c8dcSSimon Schubert 	    = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
4775796c8dcSSimon Schubert 					SYMBOL_OBJ_SECTION (var));
478cf7f2e2dSJohn Marino 
4795796c8dcSSimon Schubert 	  store_typed_address (value_contents_raw (v), type, addr);
4805796c8dcSSimon Schubert 	}
4815796c8dcSSimon Schubert       else
4825796c8dcSSimon Schubert 	store_typed_address (value_contents_raw (v), type,
4835796c8dcSSimon Schubert 			      SYMBOL_VALUE_ADDRESS (var));
4845796c8dcSSimon Schubert       VALUE_LVAL (v) = not_lval;
4855796c8dcSSimon Schubert       return v;
4865796c8dcSSimon Schubert 
4875796c8dcSSimon Schubert     case LOC_CONST_BYTES:
488c50c785cSJohn Marino       v = allocate_value (type);
489*ef5ccd6cSJohn Marino       memcpy (value_contents_raw (v), SYMBOL_VALUE_BYTES (var),
490*ef5ccd6cSJohn Marino 	      TYPE_LENGTH (type));
4915796c8dcSSimon Schubert       VALUE_LVAL (v) = not_lval;
4925796c8dcSSimon Schubert       return v;
4935796c8dcSSimon Schubert 
4945796c8dcSSimon Schubert     case LOC_STATIC:
495c50c785cSJohn Marino       v = allocate_value_lazy (type);
4965796c8dcSSimon Schubert       if (overlay_debugging)
4975796c8dcSSimon Schubert 	addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
4985796c8dcSSimon Schubert 					 SYMBOL_OBJ_SECTION (var));
4995796c8dcSSimon Schubert       else
5005796c8dcSSimon Schubert 	addr = SYMBOL_VALUE_ADDRESS (var);
5015796c8dcSSimon Schubert       break;
5025796c8dcSSimon Schubert 
5035796c8dcSSimon Schubert     case LOC_ARG:
5045796c8dcSSimon Schubert       addr = get_frame_args_address (frame);
5055796c8dcSSimon Schubert       if (!addr)
506a45ae5f8SJohn Marino 	error (_("Unknown argument list address for `%s'."),
507a45ae5f8SJohn Marino 	       SYMBOL_PRINT_NAME (var));
5085796c8dcSSimon Schubert       addr += SYMBOL_VALUE (var);
509c50c785cSJohn Marino       v = allocate_value_lazy (type);
5105796c8dcSSimon Schubert       break;
5115796c8dcSSimon Schubert 
5125796c8dcSSimon Schubert     case LOC_REF_ARG:
5135796c8dcSSimon Schubert       {
5145796c8dcSSimon Schubert 	struct value *ref;
5155796c8dcSSimon Schubert 	CORE_ADDR argref;
516cf7f2e2dSJohn Marino 
5175796c8dcSSimon Schubert 	argref = get_frame_args_address (frame);
5185796c8dcSSimon Schubert 	if (!argref)
519a45ae5f8SJohn Marino 	  error (_("Unknown argument list address for `%s'."),
520a45ae5f8SJohn Marino 		 SYMBOL_PRINT_NAME (var));
5215796c8dcSSimon Schubert 	argref += SYMBOL_VALUE (var);
5225796c8dcSSimon Schubert 	ref = value_at (lookup_pointer_type (type), argref);
5235796c8dcSSimon Schubert 	addr = value_as_address (ref);
524c50c785cSJohn Marino 	v = allocate_value_lazy (type);
5255796c8dcSSimon Schubert 	break;
5265796c8dcSSimon Schubert       }
5275796c8dcSSimon Schubert 
5285796c8dcSSimon Schubert     case LOC_LOCAL:
5295796c8dcSSimon Schubert       addr = get_frame_locals_address (frame);
5305796c8dcSSimon Schubert       addr += SYMBOL_VALUE (var);
531c50c785cSJohn Marino       v = allocate_value_lazy (type);
5325796c8dcSSimon Schubert       break;
5335796c8dcSSimon Schubert 
5345796c8dcSSimon Schubert     case LOC_TYPEDEF:
535a45ae5f8SJohn Marino       error (_("Cannot look up value of a typedef `%s'."),
536a45ae5f8SJohn Marino 	     SYMBOL_PRINT_NAME (var));
5375796c8dcSSimon Schubert       break;
5385796c8dcSSimon Schubert 
5395796c8dcSSimon Schubert     case LOC_BLOCK:
540c50c785cSJohn Marino       v = allocate_value_lazy (type);
5415796c8dcSSimon Schubert       if (overlay_debugging)
542c50c785cSJohn Marino 	addr = symbol_overlayed_address
543c50c785cSJohn Marino 	  (BLOCK_START (SYMBOL_BLOCK_VALUE (var)), SYMBOL_OBJ_SECTION (var));
5445796c8dcSSimon Schubert       else
545c50c785cSJohn Marino 	addr = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
546c50c785cSJohn Marino       break;
5475796c8dcSSimon Schubert 
5485796c8dcSSimon Schubert     case LOC_REGISTER:
5495796c8dcSSimon Schubert     case LOC_REGPARM_ADDR:
5505796c8dcSSimon Schubert       {
5515796c8dcSSimon Schubert 	int regno = SYMBOL_REGISTER_OPS (var)
5525796c8dcSSimon Schubert 		      ->register_number (var, get_frame_arch (frame));
5535796c8dcSSimon Schubert 	struct value *regval;
5545796c8dcSSimon Schubert 
5555796c8dcSSimon Schubert 	if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
5565796c8dcSSimon Schubert 	  {
5575796c8dcSSimon Schubert 	    regval = value_from_register (lookup_pointer_type (type),
5585796c8dcSSimon Schubert 					  regno,
5595796c8dcSSimon Schubert 					  frame);
5605796c8dcSSimon Schubert 
5615796c8dcSSimon Schubert 	    if (regval == NULL)
562a45ae5f8SJohn Marino 	      error (_("Value of register variable not available for `%s'."),
563a45ae5f8SJohn Marino 	             SYMBOL_PRINT_NAME (var));
5645796c8dcSSimon Schubert 
5655796c8dcSSimon Schubert 	    addr = value_as_address (regval);
566c50c785cSJohn Marino 	    v = allocate_value_lazy (type);
5675796c8dcSSimon Schubert 	  }
5685796c8dcSSimon Schubert 	else
5695796c8dcSSimon Schubert 	  {
5705796c8dcSSimon Schubert 	    regval = value_from_register (type, regno, frame);
5715796c8dcSSimon Schubert 
5725796c8dcSSimon Schubert 	    if (regval == NULL)
573a45ae5f8SJohn Marino 	      error (_("Value of register variable not available for `%s'."),
574a45ae5f8SJohn Marino 	             SYMBOL_PRINT_NAME (var));
5755796c8dcSSimon Schubert 	    return regval;
5765796c8dcSSimon Schubert 	  }
5775796c8dcSSimon Schubert       }
5785796c8dcSSimon Schubert       break;
5795796c8dcSSimon Schubert 
5805796c8dcSSimon Schubert     case LOC_COMPUTED:
5815796c8dcSSimon Schubert       /* FIXME: cagney/2004-01-26: It should be possible to
5825796c8dcSSimon Schubert 	 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
5835796c8dcSSimon Schubert 	 Unfortunately DWARF 2 stores the frame-base (instead of the
5845796c8dcSSimon Schubert 	 function) location in a function's symbol.  Oops!  For the
5855796c8dcSSimon Schubert 	 moment enable this when/where applicable.  */
5865796c8dcSSimon Schubert       return SYMBOL_COMPUTED_OPS (var)->read_variable (var, frame);
5875796c8dcSSimon Schubert 
5885796c8dcSSimon Schubert     case LOC_UNRESOLVED:
5895796c8dcSSimon Schubert       {
590*ef5ccd6cSJohn Marino 	struct minsym_lookup_data lookup_data;
5915796c8dcSSimon Schubert 	struct minimal_symbol *msym;
5925796c8dcSSimon Schubert 	struct obj_section *obj_section;
5935796c8dcSSimon Schubert 
594*ef5ccd6cSJohn Marino 	memset (&lookup_data, 0, sizeof (lookup_data));
595*ef5ccd6cSJohn Marino 	lookup_data.name = SYMBOL_LINKAGE_NAME (var);
596*ef5ccd6cSJohn Marino 
597*ef5ccd6cSJohn Marino 	gdbarch_iterate_over_objfiles_in_search_order
598*ef5ccd6cSJohn Marino 	  (get_objfile_arch (SYMBOL_SYMTAB (var)->objfile),
599*ef5ccd6cSJohn Marino 	   minsym_lookup_iterator_cb, &lookup_data,
600*ef5ccd6cSJohn Marino 	   SYMBOL_SYMTAB (var)->objfile);
601*ef5ccd6cSJohn Marino 	msym = lookup_data.result;
602*ef5ccd6cSJohn Marino 
6035796c8dcSSimon Schubert 	if (msym == NULL)
604a45ae5f8SJohn Marino 	  error (_("No global symbol \"%s\"."), SYMBOL_LINKAGE_NAME (var));
6055796c8dcSSimon Schubert 	if (overlay_debugging)
6065796c8dcSSimon Schubert 	  addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym),
6075796c8dcSSimon Schubert 					   SYMBOL_OBJ_SECTION (msym));
6085796c8dcSSimon Schubert 	else
6095796c8dcSSimon Schubert 	  addr = SYMBOL_VALUE_ADDRESS (msym);
6105796c8dcSSimon Schubert 
6115796c8dcSSimon Schubert 	obj_section = SYMBOL_OBJ_SECTION (msym);
6125796c8dcSSimon Schubert 	if (obj_section
6135796c8dcSSimon Schubert 	    && (obj_section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
6145796c8dcSSimon Schubert 	  addr = target_translate_tls_address (obj_section->objfile, addr);
615c50c785cSJohn Marino 	v = allocate_value_lazy (type);
6165796c8dcSSimon Schubert       }
6175796c8dcSSimon Schubert       break;
6185796c8dcSSimon Schubert 
6195796c8dcSSimon Schubert     case LOC_OPTIMIZED_OUT:
620a45ae5f8SJohn Marino       return allocate_optimized_out_value (type);
6215796c8dcSSimon Schubert 
6225796c8dcSSimon Schubert     default:
623a45ae5f8SJohn Marino       error (_("Cannot look up value of a botched symbol `%s'."),
624a45ae5f8SJohn Marino 	     SYMBOL_PRINT_NAME (var));
6255796c8dcSSimon Schubert       break;
6265796c8dcSSimon Schubert     }
6275796c8dcSSimon Schubert 
628c50c785cSJohn Marino   VALUE_LVAL (v) = lval_memory;
6295796c8dcSSimon Schubert   set_value_address (v, addr);
6305796c8dcSSimon Schubert   return v;
6315796c8dcSSimon Schubert }
6325796c8dcSSimon Schubert 
633*ef5ccd6cSJohn Marino /* Calls VAR's language la_read_var_value hook with the given arguments.  */
634*ef5ccd6cSJohn Marino 
635*ef5ccd6cSJohn Marino struct value *
read_var_value(struct symbol * var,struct frame_info * frame)636*ef5ccd6cSJohn Marino read_var_value (struct symbol *var, struct frame_info *frame)
637*ef5ccd6cSJohn Marino {
638*ef5ccd6cSJohn Marino   const struct language_defn *lang = language_def (SYMBOL_LANGUAGE (var));
639*ef5ccd6cSJohn Marino 
640*ef5ccd6cSJohn Marino   gdb_assert (lang != NULL);
641*ef5ccd6cSJohn Marino   gdb_assert (lang->la_read_var_value != NULL);
642*ef5ccd6cSJohn Marino 
643*ef5ccd6cSJohn Marino   return lang->la_read_var_value (var, frame);
644*ef5ccd6cSJohn Marino }
645*ef5ccd6cSJohn Marino 
6465796c8dcSSimon Schubert /* Install default attributes for register values.  */
6475796c8dcSSimon Schubert 
6485796c8dcSSimon Schubert struct value *
default_value_from_register(struct type * type,int regnum,struct frame_info * frame)6495796c8dcSSimon Schubert default_value_from_register (struct type *type, int regnum,
6505796c8dcSSimon Schubert 			     struct frame_info *frame)
6515796c8dcSSimon Schubert {
6525796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (frame);
6535796c8dcSSimon Schubert   int len = TYPE_LENGTH (type);
6545796c8dcSSimon Schubert   struct value *value = allocate_value (type);
6555796c8dcSSimon Schubert 
6565796c8dcSSimon Schubert   VALUE_LVAL (value) = lval_register;
6575796c8dcSSimon Schubert   VALUE_FRAME_ID (value) = get_frame_id (frame);
6585796c8dcSSimon Schubert   VALUE_REGNUM (value) = regnum;
6595796c8dcSSimon Schubert 
6605796c8dcSSimon Schubert   /* Any structure stored in more than one register will always be
6615796c8dcSSimon Schubert      an integral number of registers.  Otherwise, you need to do
6625796c8dcSSimon Schubert      some fiddling with the last register copied here for little
6635796c8dcSSimon Schubert      endian machines.  */
6645796c8dcSSimon Schubert   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
6655796c8dcSSimon Schubert       && len < register_size (gdbarch, regnum))
6665796c8dcSSimon Schubert     /* Big-endian, and we want less than full size.  */
6675796c8dcSSimon Schubert     set_value_offset (value, register_size (gdbarch, regnum) - len);
6685796c8dcSSimon Schubert   else
6695796c8dcSSimon Schubert     set_value_offset (value, 0);
6705796c8dcSSimon Schubert 
6715796c8dcSSimon Schubert   return value;
6725796c8dcSSimon Schubert }
6735796c8dcSSimon Schubert 
674a45ae5f8SJohn Marino /* VALUE must be an lval_register value.  If regnum is the value's
675a45ae5f8SJohn Marino    associated register number, and len the length of the values type,
676a45ae5f8SJohn Marino    read one or more registers in FRAME, starting with register REGNUM,
677*ef5ccd6cSJohn Marino    until we've read LEN bytes.
678*ef5ccd6cSJohn Marino 
679*ef5ccd6cSJohn Marino    If any of the registers we try to read are optimized out, then mark the
680*ef5ccd6cSJohn Marino    complete resulting value as optimized out.  */
681a45ae5f8SJohn Marino 
682a45ae5f8SJohn Marino void
read_frame_register_value(struct value * value,struct frame_info * frame)683a45ae5f8SJohn Marino read_frame_register_value (struct value *value, struct frame_info *frame)
684a45ae5f8SJohn Marino {
685a45ae5f8SJohn Marino   struct gdbarch *gdbarch = get_frame_arch (frame);
686a45ae5f8SJohn Marino   int offset = 0;
687a45ae5f8SJohn Marino   int reg_offset = value_offset (value);
688a45ae5f8SJohn Marino   int regnum = VALUE_REGNUM (value);
689a45ae5f8SJohn Marino   int len = TYPE_LENGTH (check_typedef (value_type (value)));
690a45ae5f8SJohn Marino 
691a45ae5f8SJohn Marino   gdb_assert (VALUE_LVAL (value) == lval_register);
692a45ae5f8SJohn Marino 
693a45ae5f8SJohn Marino   /* Skip registers wholly inside of REG_OFFSET.  */
694a45ae5f8SJohn Marino   while (reg_offset >= register_size (gdbarch, regnum))
695a45ae5f8SJohn Marino     {
696a45ae5f8SJohn Marino       reg_offset -= register_size (gdbarch, regnum);
697a45ae5f8SJohn Marino       regnum++;
698a45ae5f8SJohn Marino     }
699a45ae5f8SJohn Marino 
700a45ae5f8SJohn Marino   /* Copy the data.  */
701a45ae5f8SJohn Marino   while (len > 0)
702a45ae5f8SJohn Marino     {
703a45ae5f8SJohn Marino       struct value *regval = get_frame_register_value (frame, regnum);
704a45ae5f8SJohn Marino       int reg_len = TYPE_LENGTH (value_type (regval)) - reg_offset;
705a45ae5f8SJohn Marino 
706*ef5ccd6cSJohn Marino       if (value_optimized_out (regval))
707*ef5ccd6cSJohn Marino 	{
708*ef5ccd6cSJohn Marino 	  set_value_optimized_out (value, 1);
709*ef5ccd6cSJohn Marino 	  break;
710*ef5ccd6cSJohn Marino 	}
711*ef5ccd6cSJohn Marino 
712a45ae5f8SJohn Marino       /* If the register length is larger than the number of bytes
713a45ae5f8SJohn Marino          remaining to copy, then only copy the appropriate bytes.  */
714a45ae5f8SJohn Marino       if (reg_len > len)
715a45ae5f8SJohn Marino 	reg_len = len;
716a45ae5f8SJohn Marino 
717a45ae5f8SJohn Marino       value_contents_copy (value, offset, regval, reg_offset, reg_len);
718a45ae5f8SJohn Marino 
719a45ae5f8SJohn Marino       offset += reg_len;
720a45ae5f8SJohn Marino       len -= reg_len;
721a45ae5f8SJohn Marino       reg_offset = 0;
722a45ae5f8SJohn Marino       regnum++;
723a45ae5f8SJohn Marino     }
724a45ae5f8SJohn Marino }
725a45ae5f8SJohn Marino 
7265796c8dcSSimon Schubert /* Return a value of type TYPE, stored in register REGNUM, in frame FRAME.  */
7275796c8dcSSimon Schubert 
7285796c8dcSSimon Schubert struct value *
value_from_register(struct type * type,int regnum,struct frame_info * frame)7295796c8dcSSimon Schubert value_from_register (struct type *type, int regnum, struct frame_info *frame)
7305796c8dcSSimon Schubert {
7315796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (frame);
7325796c8dcSSimon Schubert   struct type *type1 = check_typedef (type);
7335796c8dcSSimon Schubert   struct value *v;
7345796c8dcSSimon Schubert 
7355796c8dcSSimon Schubert   if (gdbarch_convert_register_p (gdbarch, regnum, type1))
7365796c8dcSSimon Schubert     {
737a45ae5f8SJohn Marino       int optim, unavail, ok;
738a45ae5f8SJohn Marino 
7395796c8dcSSimon Schubert       /* The ISA/ABI need to something weird when obtaining the
7405796c8dcSSimon Schubert          specified value from this register.  It might need to
7415796c8dcSSimon Schubert          re-order non-adjacent, starting with REGNUM (see MIPS and
7425796c8dcSSimon Schubert          i386).  It might need to convert the [float] register into
7435796c8dcSSimon Schubert          the corresponding [integer] type (see Alpha).  The assumption
7445796c8dcSSimon Schubert          is that gdbarch_register_to_value populates the entire value
7455796c8dcSSimon Schubert          including the location.  */
7465796c8dcSSimon Schubert       v = allocate_value (type);
7475796c8dcSSimon Schubert       VALUE_LVAL (v) = lval_register;
7485796c8dcSSimon Schubert       VALUE_FRAME_ID (v) = get_frame_id (frame);
7495796c8dcSSimon Schubert       VALUE_REGNUM (v) = regnum;
750c50c785cSJohn Marino       ok = gdbarch_register_to_value (gdbarch, frame, regnum, type1,
751c50c785cSJohn Marino 				      value_contents_raw (v), &optim,
752c50c785cSJohn Marino 				      &unavail);
753c50c785cSJohn Marino 
754c50c785cSJohn Marino       if (!ok)
755c50c785cSJohn Marino 	{
756c50c785cSJohn Marino 	  if (optim)
757c50c785cSJohn Marino 	    set_value_optimized_out (v, 1);
758c50c785cSJohn Marino 	  if (unavail)
759c50c785cSJohn Marino 	    mark_value_bytes_unavailable (v, 0, TYPE_LENGTH (type));
760c50c785cSJohn Marino 	}
761a45ae5f8SJohn Marino     }
762a45ae5f8SJohn Marino   else
763a45ae5f8SJohn Marino     {
764a45ae5f8SJohn Marino       /* Construct the value.  */
765a45ae5f8SJohn Marino       v = gdbarch_value_from_register (gdbarch, type, regnum, frame);
766a45ae5f8SJohn Marino 
767a45ae5f8SJohn Marino       /* Get the data.  */
768a45ae5f8SJohn Marino       read_frame_register_value (v, frame);
769a45ae5f8SJohn Marino     }
770c50c785cSJohn Marino 
7715796c8dcSSimon Schubert   return v;
7725796c8dcSSimon Schubert }
7735796c8dcSSimon Schubert 
7745796c8dcSSimon Schubert /* Return contents of register REGNUM in frame FRAME as address,
7755796c8dcSSimon Schubert    interpreted as value of type TYPE.   Will abort if register
7765796c8dcSSimon Schubert    value is not available.  */
7775796c8dcSSimon Schubert 
7785796c8dcSSimon Schubert CORE_ADDR
address_from_register(struct type * type,int regnum,struct frame_info * frame)7795796c8dcSSimon Schubert address_from_register (struct type *type, int regnum, struct frame_info *frame)
7805796c8dcSSimon Schubert {
7815796c8dcSSimon Schubert   struct value *value;
7825796c8dcSSimon Schubert   CORE_ADDR result;
7835796c8dcSSimon Schubert 
7845796c8dcSSimon Schubert   value = value_from_register (type, regnum, frame);
7855796c8dcSSimon Schubert   gdb_assert (value);
7865796c8dcSSimon Schubert 
7875796c8dcSSimon Schubert   result = value_as_address (value);
7885796c8dcSSimon Schubert   release_value (value);
7895796c8dcSSimon Schubert   value_free (value);
7905796c8dcSSimon Schubert 
7915796c8dcSSimon Schubert   return result;
7925796c8dcSSimon Schubert }
793a45ae5f8SJohn Marino 
794