119370Spst/* Find a variable's value in memory, for GDB, the GNU debugger.
219370Spst
3130803Smarcel   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4130803Smarcel   1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004 Free Software
5130803Smarcel   Foundation, Inc.
6130803Smarcel
798944Sobrien   This file is part of GDB.
819370Spst
998944Sobrien   This program is free software; you can redistribute it and/or modify
1098944Sobrien   it under the terms of the GNU General Public License as published by
1198944Sobrien   the Free Software Foundation; either version 2 of the License, or
1298944Sobrien   (at your option) any later version.
1319370Spst
1498944Sobrien   This program is distributed in the hope that it will be useful,
1598944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1698944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1798944Sobrien   GNU General Public License for more details.
1819370Spst
1998944Sobrien   You should have received a copy of the GNU General Public License
2098944Sobrien   along with this program; if not, write to the Free Software
2198944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2298944Sobrien   Boston, MA 02111-1307, USA.  */
2319370Spst
2419370Spst#include "defs.h"
2519370Spst#include "symtab.h"
2619370Spst#include "gdbtypes.h"
2719370Spst#include "frame.h"
2819370Spst#include "value.h"
2919370Spst#include "gdbcore.h"
3019370Spst#include "inferior.h"
3119370Spst#include "target.h"
3219370Spst#include "gdb_string.h"
3398944Sobrien#include "gdb_assert.h"
3446283Sdfr#include "floatformat.h"
3598944Sobrien#include "symfile.h"		/* for overlay functions */
3698944Sobrien#include "regcache.h"
37130803Smarcel#include "user-regs.h"
38130803Smarcel#include "block.h"
3919370Spst
4019370Spst/* Basic byte-swapping routines.  GDB has needed these for a long time...
4119370Spst   All extract a target-format integer at ADDR which is LEN bytes long.  */
4219370Spst
4319370Spst#if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
4419370Spst  /* 8 bit characters are a pretty safe assumption these days, so we
4519370Spst     assume it throughout all these swapping routines.  If we had to deal with
4619370Spst     9 bit characters, we would need to make len be in bits and would have
4719370Spst     to re-write these routines...  */
4898944Sobrienyou lose
4919370Spst#endif
5019370Spst
5119370SpstLONGEST
52130803Smarcelextract_signed_integer (const void *addr, int len)
5319370Spst{
5419370Spst  LONGEST retval;
55130803Smarcel  const unsigned char *p;
56130803Smarcel  const unsigned char *startaddr = addr;
57130803Smarcel  const unsigned char *endaddr = startaddr + len;
5819370Spst
5919370Spst  if (len > (int) sizeof (LONGEST))
6019370Spst    error ("\
6119370SpstThat operation is not available on integers of more than %d bytes.",
62130803Smarcel	   (int) sizeof (LONGEST));
6319370Spst
6419370Spst  /* Start at the most significant end of the integer, and work towards
6519370Spst     the least significant.  */
6698944Sobrien  if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
6719370Spst    {
6819370Spst      p = startaddr;
6919370Spst      /* Do the sign extension once at the start.  */
7098944Sobrien      retval = ((LONGEST) * p ^ 0x80) - 0x80;
7119370Spst      for (++p; p < endaddr; ++p)
7219370Spst	retval = (retval << 8) | *p;
7319370Spst    }
7419370Spst  else
7519370Spst    {
7619370Spst      p = endaddr - 1;
7719370Spst      /* Do the sign extension once at the start.  */
7898944Sobrien      retval = ((LONGEST) * p ^ 0x80) - 0x80;
7919370Spst      for (--p; p >= startaddr; --p)
8019370Spst	retval = (retval << 8) | *p;
8119370Spst    }
8219370Spst  return retval;
8319370Spst}
8419370Spst
8546283SdfrULONGEST
86130803Smarcelextract_unsigned_integer (const void *addr, int len)
8719370Spst{
8846283Sdfr  ULONGEST retval;
89130803Smarcel  const unsigned char *p;
90130803Smarcel  const unsigned char *startaddr = addr;
91130803Smarcel  const unsigned char *endaddr = startaddr + len;
9219370Spst
9346283Sdfr  if (len > (int) sizeof (ULONGEST))
9419370Spst    error ("\
9519370SpstThat operation is not available on integers of more than %d bytes.",
96130803Smarcel	   (int) sizeof (ULONGEST));
9719370Spst
9819370Spst  /* Start at the most significant end of the integer, and work towards
9919370Spst     the least significant.  */
10019370Spst  retval = 0;
10198944Sobrien  if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
10219370Spst    {
10319370Spst      for (p = startaddr; p < endaddr; ++p)
10419370Spst	retval = (retval << 8) | *p;
10519370Spst    }
10619370Spst  else
10719370Spst    {
10819370Spst      for (p = endaddr - 1; p >= startaddr; --p)
10919370Spst	retval = (retval << 8) | *p;
11019370Spst    }
11119370Spst  return retval;
11219370Spst}
11319370Spst
11419370Spst/* Sometimes a long long unsigned integer can be extracted as a
11519370Spst   LONGEST value.  This is done so that we can print these values
11619370Spst   better.  If this integer can be converted to a LONGEST, this
11719370Spst   function returns 1 and sets *PVAL.  Otherwise it returns 0.  */
11819370Spst
11919370Spstint
120130803Smarcelextract_long_unsigned_integer (const void *addr, int orig_len, LONGEST *pval)
12119370Spst{
12219370Spst  char *p, *first_addr;
12319370Spst  int len;
12419370Spst
12519370Spst  len = orig_len;
12698944Sobrien  if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
12719370Spst    {
12819370Spst      for (p = (char *) addr;
12919370Spst	   len > (int) sizeof (LONGEST) && p < (char *) addr + orig_len;
13019370Spst	   p++)
13119370Spst	{
13219370Spst	  if (*p == 0)
13319370Spst	    len--;
13419370Spst	  else
13519370Spst	    break;
13619370Spst	}
13719370Spst      first_addr = p;
13819370Spst    }
13919370Spst  else
14019370Spst    {
14119370Spst      first_addr = (char *) addr;
14219370Spst      for (p = (char *) addr + orig_len - 1;
14319370Spst	   len > (int) sizeof (LONGEST) && p >= (char *) addr;
14419370Spst	   p--)
14519370Spst	{
14619370Spst	  if (*p == 0)
14719370Spst	    len--;
14819370Spst	  else
14919370Spst	    break;
15019370Spst	}
15119370Spst    }
15219370Spst
15319370Spst  if (len <= (int) sizeof (LONGEST))
15419370Spst    {
15519370Spst      *pval = (LONGEST) extract_unsigned_integer (first_addr,
15619370Spst						  sizeof (LONGEST));
15719370Spst      return 1;
15819370Spst    }
15919370Spst
16019370Spst  return 0;
16119370Spst}
16219370Spst
16398944Sobrien
16498944Sobrien/* Treat the bytes at BUF as a pointer of type TYPE, and return the
16598944Sobrien   address it represents.  */
16698944SobrienCORE_ADDR
167130803Smarcelextract_typed_address (const void *buf, struct type *type)
16898944Sobrien{
16998944Sobrien  if (TYPE_CODE (type) != TYPE_CODE_PTR
17098944Sobrien      && TYPE_CODE (type) != TYPE_CODE_REF)
17198944Sobrien    internal_error (__FILE__, __LINE__,
17298944Sobrien		    "extract_typed_address: "
17398944Sobrien		    "type is not a pointer or reference");
17498944Sobrien
17598944Sobrien  return POINTER_TO_ADDRESS (type, buf);
17698944Sobrien}
17798944Sobrien
17898944Sobrien
17919370Spstvoid
18098944Sobrienstore_signed_integer (void *addr, int len, LONGEST val)
18119370Spst{
18219370Spst  unsigned char *p;
18398944Sobrien  unsigned char *startaddr = (unsigned char *) addr;
18419370Spst  unsigned char *endaddr = startaddr + len;
18519370Spst
18619370Spst  /* Start at the least significant end of the integer, and work towards
18719370Spst     the most significant.  */
18898944Sobrien  if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
18919370Spst    {
19019370Spst      for (p = endaddr - 1; p >= startaddr; --p)
19119370Spst	{
19219370Spst	  *p = val & 0xff;
19319370Spst	  val >>= 8;
19419370Spst	}
19519370Spst    }
19619370Spst  else
19719370Spst    {
19819370Spst      for (p = startaddr; p < endaddr; ++p)
19919370Spst	{
20019370Spst	  *p = val & 0xff;
20119370Spst	  val >>= 8;
20219370Spst	}
20319370Spst    }
20419370Spst}
20519370Spst
20619370Spstvoid
20798944Sobrienstore_unsigned_integer (void *addr, int len, ULONGEST val)
20819370Spst{
20919370Spst  unsigned char *p;
21098944Sobrien  unsigned char *startaddr = (unsigned char *) addr;
21119370Spst  unsigned char *endaddr = startaddr + len;
21219370Spst
21319370Spst  /* Start at the least significant end of the integer, and work towards
21419370Spst     the most significant.  */
21598944Sobrien  if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
21619370Spst    {
21719370Spst      for (p = endaddr - 1; p >= startaddr; --p)
21819370Spst	{
21919370Spst	  *p = val & 0xff;
22019370Spst	  val >>= 8;
22119370Spst	}
22219370Spst    }
22319370Spst  else
22419370Spst    {
22519370Spst      for (p = startaddr; p < endaddr; ++p)
22619370Spst	{
22719370Spst	  *p = val & 0xff;
22819370Spst	  val >>= 8;
22919370Spst	}
23019370Spst    }
23119370Spst}
23219370Spst
23398944Sobrien/* Store the address ADDR as a pointer of type TYPE at BUF, in target
23498944Sobrien   form.  */
23519370Spstvoid
23698944Sobrienstore_typed_address (void *buf, struct type *type, CORE_ADDR addr)
23719370Spst{
23898944Sobrien  if (TYPE_CODE (type) != TYPE_CODE_PTR
23998944Sobrien      && TYPE_CODE (type) != TYPE_CODE_REF)
24098944Sobrien    internal_error (__FILE__, __LINE__,
24198944Sobrien		    "store_typed_address: "
24298944Sobrien		    "type is not a pointer or reference");
24319370Spst
24498944Sobrien  ADDRESS_TO_POINTER (type, buf, addr);
24519370Spst}
24619370Spst
24719370Spst
24819370Spst
249130803Smarcel/* Return a `value' with the contents of (virtual or cooked) register
250130803Smarcel   REGNUM as found in the specified FRAME.  The register's type is
251130803Smarcel   determined by register_type().
25219370Spst
253130803Smarcel   NOTE: returns NULL if register value is not available.  Caller will
254130803Smarcel   check return value or die!  */
25546283Sdfr
25698944Sobrienstruct value *
257130803Smarcelvalue_of_register (int regnum, struct frame_info *frame)
25819370Spst{
25919370Spst  CORE_ADDR addr;
26019370Spst  int optim;
26198944Sobrien  struct value *reg_val;
262130803Smarcel  int realnum;
263130803Smarcel  char raw_buffer[MAX_REGISTER_SIZE];
26419370Spst  enum lval_type lval;
26519370Spst
266130803Smarcel  /* User registers lie completely outside of the range of normal
267130803Smarcel     registers.  Catch them early so that the target never sees them.  */
268130803Smarcel  if (regnum >= NUM_REGS + NUM_PSEUDO_REGS)
269130803Smarcel    return value_of_user_reg (regnum, frame);
27019370Spst
271130803Smarcel  frame_register (frame, regnum, &optim, &lval, &addr, &realnum, raw_buffer);
272130803Smarcel
273130803Smarcel  /* FIXME: cagney/2002-05-15: This test is just bogus.
274130803Smarcel
275130803Smarcel     It indicates that the target failed to supply a value for a
276130803Smarcel     register because it was "not available" at this time.  Problem
277130803Smarcel     is, the target still has the register and so get saved_register()
278130803Smarcel     may be returning a value saved on the stack.  */
279130803Smarcel
28098944Sobrien  if (register_cached (regnum) < 0)
28198944Sobrien    return NULL;		/* register value not available */
28246283Sdfr
283130803Smarcel  reg_val = allocate_value (register_type (current_gdbarch, regnum));
28419370Spst
28519370Spst  /* Convert raw data to virtual format if necessary.  */
28619370Spst
287130803Smarcel  if (DEPRECATED_REGISTER_CONVERTIBLE_P ()
288130803Smarcel      && DEPRECATED_REGISTER_CONVERTIBLE (regnum))
28919370Spst    {
290130803Smarcel      DEPRECATED_REGISTER_CONVERT_TO_VIRTUAL (regnum, register_type (current_gdbarch, regnum),
291130803Smarcel					      raw_buffer, VALUE_CONTENTS_RAW (reg_val));
29219370Spst    }
293130803Smarcel  else if (DEPRECATED_REGISTER_RAW_SIZE (regnum) == DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum))
29498944Sobrien    memcpy (VALUE_CONTENTS_RAW (reg_val), raw_buffer,
295130803Smarcel	    DEPRECATED_REGISTER_RAW_SIZE (regnum));
29619370Spst  else
29798944Sobrien    internal_error (__FILE__, __LINE__,
29898944Sobrien		    "Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size",
29998944Sobrien		    REGISTER_NAME (regnum),
30098944Sobrien		    regnum,
301130803Smarcel		    DEPRECATED_REGISTER_RAW_SIZE (regnum),
302130803Smarcel		    DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum));
30319370Spst  VALUE_LVAL (reg_val) = lval;
30419370Spst  VALUE_ADDRESS (reg_val) = addr;
30519370Spst  VALUE_REGNO (reg_val) = regnum;
30619370Spst  VALUE_OPTIMIZED_OUT (reg_val) = optim;
30719370Spst  return reg_val;
30819370Spst}
30919370Spst
31098944Sobrien/* Given a pointer of type TYPE in target form in BUF, return the
31198944Sobrien   address it represents.  */
31219370SpstCORE_ADDR
313130803Smarcelunsigned_pointer_to_address (struct type *type, const void *buf)
31419370Spst{
315130803Smarcel  return extract_unsigned_integer (buf, TYPE_LENGTH (type));
31619370Spst}
31719370Spst
31819370SpstCORE_ADDR
319130803Smarcelsigned_pointer_to_address (struct type *type, const void *buf)
32019370Spst{
32198944Sobrien  return extract_signed_integer (buf, TYPE_LENGTH (type));
32219370Spst}
32319370Spst
32498944Sobrien/* Given an address, store it as a pointer of type TYPE in target
32598944Sobrien   format in BUF.  */
32619370Spstvoid
32798944Sobrienunsigned_address_to_pointer (struct type *type, void *buf, CORE_ADDR addr)
32819370Spst{
329130803Smarcel  store_unsigned_integer (buf, TYPE_LENGTH (type), addr);
33019370Spst}
33119370Spst
33246283Sdfrvoid
33398944Sobrienaddress_to_signed_pointer (struct type *type, void *buf, CORE_ADDR addr)
33419370Spst{
33598944Sobrien  store_signed_integer (buf, TYPE_LENGTH (type), addr);
33619370Spst}
33719370Spst
33819370Spst/* Will calling read_var_value or locate_var_value on SYM end
33919370Spst   up caring what frame it is being evaluated relative to?  SYM must
34019370Spst   be non-NULL.  */
34119370Spstint
34298944Sobriensymbol_read_needs_frame (struct symbol *sym)
34319370Spst{
34419370Spst  switch (SYMBOL_CLASS (sym))
34519370Spst    {
34619370Spst      /* All cases listed explicitly so that gcc -Wall will detect it if
34798944Sobrien         we failed to consider one.  */
348130803Smarcel    case LOC_COMPUTED:
349130803Smarcel    case LOC_COMPUTED_ARG:
350130803Smarcel      /* FIXME: cagney/2004-01-26: It should be possible to
351130803Smarcel	 unconditionally call the SYMBOL_OPS method when available.
352130803Smarcel	 Unfortunately DWARF 2 stores the frame-base (instead of the
353130803Smarcel	 function) location in a function's symbol.  Oops!  For the
354130803Smarcel	 moment enable this when/where applicable.  */
355130803Smarcel      return SYMBOL_OPS (sym)->read_needs_frame (sym);
356130803Smarcel
35719370Spst    case LOC_REGISTER:
35819370Spst    case LOC_ARG:
35919370Spst    case LOC_REF_ARG:
36019370Spst    case LOC_REGPARM:
36119370Spst    case LOC_REGPARM_ADDR:
36219370Spst    case LOC_LOCAL:
36319370Spst    case LOC_LOCAL_ARG:
36419370Spst    case LOC_BASEREG:
36519370Spst    case LOC_BASEREG_ARG:
366130803Smarcel    case LOC_HP_THREAD_LOCAL_STATIC:
36719370Spst      return 1;
36819370Spst
36919370Spst    case LOC_UNDEF:
37019370Spst    case LOC_CONST:
37119370Spst    case LOC_STATIC:
37246283Sdfr    case LOC_INDIRECT:
37319370Spst    case LOC_TYPEDEF:
37419370Spst
37519370Spst    case LOC_LABEL:
37619370Spst      /* Getting the address of a label can be done independently of the block,
37798944Sobrien         even if some *uses* of that address wouldn't work so well without
37898944Sobrien         the right frame.  */
37919370Spst
38019370Spst    case LOC_BLOCK:
38119370Spst    case LOC_CONST_BYTES:
38219370Spst    case LOC_UNRESOLVED:
38319370Spst    case LOC_OPTIMIZED_OUT:
38419370Spst      return 0;
38519370Spst    }
38619370Spst  return 1;
38719370Spst}
38819370Spst
38919370Spst/* Given a struct symbol for a variable,
39019370Spst   and a stack frame id, read the value of the variable
39119370Spst   and return a (pointer to a) struct value containing the value.
39219370Spst   If the variable cannot be found, return a zero pointer.
393130803Smarcel   If FRAME is NULL, use the deprecated_selected_frame.  */
39419370Spst
39598944Sobrienstruct value *
396130803Smarcelread_var_value (struct symbol *var, struct frame_info *frame)
39719370Spst{
398130803Smarcel  struct value *v;
39919370Spst  struct type *type = SYMBOL_TYPE (var);
40019370Spst  CORE_ADDR addr;
401130803Smarcel  int len;
40219370Spst
40319370Spst  v = allocate_value (type);
40419370Spst  VALUE_LVAL (v) = lval_memory;	/* The most likely possibility.  */
40546283Sdfr  VALUE_BFD_SECTION (v) = SYMBOL_BFD_SECTION (var);
40646283Sdfr
40719370Spst  len = TYPE_LENGTH (type);
40819370Spst
409130803Smarcel
410130803Smarcel  /* FIXME drow/2003-09-06: this call to the selected frame should be
411130803Smarcel     pushed upwards to the callers.  */
41298944Sobrien  if (frame == NULL)
413130803Smarcel    frame = deprecated_safe_get_selected_frame ();
41419370Spst
41519370Spst  switch (SYMBOL_CLASS (var))
41619370Spst    {
41719370Spst    case LOC_CONST:
41819370Spst      /* Put the constant back in target format.  */
41919370Spst      store_signed_integer (VALUE_CONTENTS_RAW (v), len,
42019370Spst			    (LONGEST) SYMBOL_VALUE (var));
42119370Spst      VALUE_LVAL (v) = not_lval;
42219370Spst      return v;
42319370Spst
42419370Spst    case LOC_LABEL:
42519370Spst      /* Put the constant back in target format.  */
42646283Sdfr      if (overlay_debugging)
42798944Sobrien	{
42898944Sobrien	  CORE_ADDR addr
42998944Sobrien	    = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
43098944Sobrien					SYMBOL_BFD_SECTION (var));
43198944Sobrien	  store_typed_address (VALUE_CONTENTS_RAW (v), type, addr);
43298944Sobrien	}
43346283Sdfr      else
43498944Sobrien	store_typed_address (VALUE_CONTENTS_RAW (v), type,
43598944Sobrien			      SYMBOL_VALUE_ADDRESS (var));
43619370Spst      VALUE_LVAL (v) = not_lval;
43719370Spst      return v;
43819370Spst
43919370Spst    case LOC_CONST_BYTES:
44019370Spst      {
44119370Spst	char *bytes_addr;
44219370Spst	bytes_addr = SYMBOL_VALUE_BYTES (var);
44319370Spst	memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
44419370Spst	VALUE_LVAL (v) = not_lval;
44519370Spst	return v;
44619370Spst      }
44719370Spst
44819370Spst    case LOC_STATIC:
44946283Sdfr      if (overlay_debugging)
45046283Sdfr	addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
45146283Sdfr					 SYMBOL_BFD_SECTION (var));
45246283Sdfr      else
45346283Sdfr	addr = SYMBOL_VALUE_ADDRESS (var);
45446283Sdfr      break;
45546283Sdfr
45646283Sdfr    case LOC_INDIRECT:
45798944Sobrien      {
45898944Sobrien	/* The import slot does not have a real address in it from the
45998944Sobrien	   dynamic loader (dld.sl on HP-UX), if the target hasn't
46098944Sobrien	   begun execution yet, so check for that. */
46198944Sobrien	CORE_ADDR locaddr;
46298944Sobrien	struct value *loc;
46398944Sobrien	if (!target_has_execution)
46498944Sobrien	  error ("\
46546283SdfrAttempt to access variable defined in different shared object or load module when\n\
46646283Sdfraddresses have not been bound by the dynamic loader. Try again when executable is running.");
46719370Spst
46898944Sobrien	locaddr = SYMBOL_VALUE_ADDRESS (var);
46998944Sobrien	loc = value_at (lookup_pointer_type (type), locaddr, NULL);
47098944Sobrien	addr = value_as_address (loc);
47198944Sobrien      }
47298944Sobrien
47319370Spst    case LOC_ARG:
47419370Spst      if (frame == NULL)
47519370Spst	return 0;
476130803Smarcel      addr = get_frame_args_address (frame);
47719370Spst      if (!addr)
47819370Spst	return 0;
47919370Spst      addr += SYMBOL_VALUE (var);
48019370Spst      break;
48119370Spst
48219370Spst    case LOC_REF_ARG:
48398944Sobrien      {
48498944Sobrien	struct value *ref;
48598944Sobrien	CORE_ADDR argref;
48698944Sobrien	if (frame == NULL)
48798944Sobrien	  return 0;
488130803Smarcel	argref = get_frame_args_address (frame);
48998944Sobrien	if (!argref)
49098944Sobrien	  return 0;
49198944Sobrien	argref += SYMBOL_VALUE (var);
49298944Sobrien	ref = value_at (lookup_pointer_type (type), argref, NULL);
49398944Sobrien	addr = value_as_address (ref);
49498944Sobrien	break;
49598944Sobrien      }
49619370Spst
49719370Spst    case LOC_LOCAL:
49819370Spst    case LOC_LOCAL_ARG:
49919370Spst      if (frame == NULL)
50019370Spst	return 0;
501130803Smarcel      addr = get_frame_locals_address (frame);
50219370Spst      addr += SYMBOL_VALUE (var);
50319370Spst      break;
50419370Spst
50519370Spst    case LOC_BASEREG:
50619370Spst    case LOC_BASEREG_ARG:
507130803Smarcel    case LOC_HP_THREAD_LOCAL_STATIC:
50819370Spst      {
50998944Sobrien	struct value *regval;
51098944Sobrien
51198944Sobrien	regval = value_from_register (lookup_pointer_type (type),
51298944Sobrien				      SYMBOL_BASEREG (var), frame);
51398944Sobrien	if (regval == NULL)
51498944Sobrien	  error ("Value of base register not available.");
51598944Sobrien	addr = value_as_address (regval);
51619370Spst	addr += SYMBOL_VALUE (var);
51719370Spst	break;
51819370Spst      }
51998944Sobrien
52019370Spst    case LOC_TYPEDEF:
52119370Spst      error ("Cannot look up value of a typedef");
52219370Spst      break;
52319370Spst
52419370Spst    case LOC_BLOCK:
52546283Sdfr      if (overlay_debugging)
52698944Sobrien	VALUE_ADDRESS (v) = symbol_overlayed_address
52746283Sdfr	  (BLOCK_START (SYMBOL_BLOCK_VALUE (var)), SYMBOL_BFD_SECTION (var));
52846283Sdfr      else
52946283Sdfr	VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
53019370Spst      return v;
53119370Spst
53219370Spst    case LOC_REGISTER:
53319370Spst    case LOC_REGPARM:
53419370Spst    case LOC_REGPARM_ADDR:
53519370Spst      {
53619370Spst	struct block *b;
53746283Sdfr	int regno = SYMBOL_VALUE (var);
53898944Sobrien	struct value *regval;
53919370Spst
54019370Spst	if (frame == NULL)
54119370Spst	  return 0;
542130803Smarcel	b = get_frame_block (frame, 0);
54319370Spst
54419370Spst	if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
54519370Spst	  {
54646283Sdfr	    regval = value_from_register (lookup_pointer_type (type),
54798944Sobrien					  regno,
54846283Sdfr					  frame);
54946283Sdfr
55046283Sdfr	    if (regval == NULL)
55146283Sdfr	      error ("Value of register variable not available.");
55246283Sdfr
55398944Sobrien	    addr = value_as_address (regval);
55419370Spst	    VALUE_LVAL (v) = lval_memory;
55519370Spst	  }
55619370Spst	else
55746283Sdfr	  {
55846283Sdfr	    regval = value_from_register (type, regno, frame);
55946283Sdfr
56046283Sdfr	    if (regval == NULL)
56146283Sdfr	      error ("Value of register variable not available.");
56246283Sdfr	    return regval;
56346283Sdfr	  }
56419370Spst      }
56519370Spst      break;
56619370Spst
567130803Smarcel    case LOC_COMPUTED:
568130803Smarcel    case LOC_COMPUTED_ARG:
569130803Smarcel      /* FIXME: cagney/2004-01-26: It should be possible to
570130803Smarcel	 unconditionally call the SYMBOL_OPS method when available.
571130803Smarcel	 Unfortunately DWARF 2 stores the frame-base (instead of the
572130803Smarcel	 function) location in a function's symbol.  Oops!  For the
573130803Smarcel	 moment enable this when/where applicable.  */
574130803Smarcel      if (frame == 0 && SYMBOL_OPS (var)->read_needs_frame (var))
575130803Smarcel	return 0;
576130803Smarcel      return SYMBOL_OPS (var)->read_variable (var, frame);
577130803Smarcel
57819370Spst    case LOC_UNRESOLVED:
57919370Spst      {
58019370Spst	struct minimal_symbol *msym;
58119370Spst
582130803Smarcel	msym = lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (var), NULL, NULL);
58319370Spst	if (msym == NULL)
58419370Spst	  return 0;
58546283Sdfr	if (overlay_debugging)
58646283Sdfr	  addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym),
58746283Sdfr					   SYMBOL_BFD_SECTION (msym));
58846283Sdfr	else
58946283Sdfr	  addr = SYMBOL_VALUE_ADDRESS (msym);
59019370Spst      }
59119370Spst      break;
59219370Spst
59319370Spst    case LOC_OPTIMIZED_OUT:
59419370Spst      VALUE_LVAL (v) = not_lval;
59519370Spst      VALUE_OPTIMIZED_OUT (v) = 1;
59619370Spst      return v;
59719370Spst
59819370Spst    default:
59919370Spst      error ("Cannot look up value of a botched symbol.");
60019370Spst      break;
60119370Spst    }
60219370Spst
60319370Spst  VALUE_ADDRESS (v) = addr;
60419370Spst  VALUE_LAZY (v) = 1;
60519370Spst  return v;
60619370Spst}
60719370Spst
60819370Spst/* Return a value of type TYPE, stored in register REGNUM, in frame
60998944Sobrien   FRAME.
61019370Spst
61146283Sdfr   NOTE: returns NULL if register value is not available.
61246283Sdfr   Caller will check return value or die!  */
61346283Sdfr
61498944Sobrienstruct value *
61598944Sobrienvalue_from_register (struct type *type, int regnum, struct frame_info *frame)
61619370Spst{
617130803Smarcel  struct gdbarch *gdbarch = get_frame_arch (frame);
61898944Sobrien  struct value *v = allocate_value (type);
61919370Spst  CHECK_TYPEDEF (type);
62019370Spst
621130803Smarcel  if (TYPE_LENGTH (type) == 0)
622130803Smarcel    {
623130803Smarcel      /* It doesn't matter much what we return for this: since the
624130803Smarcel         length is zero, it could be anything.  But if allowed to see
625130803Smarcel         a zero-length type, the register-finding loop below will set
626130803Smarcel         neither mem_stor nor reg_stor, and then report an internal
627130803Smarcel         error.
62819370Spst
629130803Smarcel         Zero-length types can legitimately arise from declarations
630130803Smarcel         like 'struct {}' (a GCC extension, not valid ISO C).  GDB may
631130803Smarcel         also create them when it finds bogus debugging information;
632130803Smarcel         for example, in GCC 2.95.4 and binutils 2.11.93.0.2, the
633130803Smarcel         STABS BINCL->EXCL compression process can create bad type
634130803Smarcel         numbers.  GDB reads these as TYPE_CODE_UNDEF types, with zero
635130803Smarcel         length.  (That bug is actually the only known way to get a
636130803Smarcel         zero-length value allocated to a register --- which is what
637130803Smarcel         it takes to make it here.)
63819370Spst
639130803Smarcel         We'll just attribute the value to the original register.  */
640130803Smarcel      VALUE_LVAL (v) = lval_register;
641130803Smarcel      VALUE_ADDRESS (v) = regnum;
642130803Smarcel      VALUE_REGNO (v) = regnum;
643130803Smarcel    }
644130803Smarcel  else if (CONVERT_REGISTER_P (regnum, type))
64519370Spst    {
646130803Smarcel      /* The ISA/ABI need to something weird when obtaining the
647130803Smarcel         specified value from this register.  It might need to
648130803Smarcel         re-order non-adjacent, starting with REGNUM (see MIPS and
649130803Smarcel         i386).  It might need to convert the [float] register into
650130803Smarcel         the corresponding [integer] type (see Alpha).  The assumption
651130803Smarcel         is that REGISTER_TO_VALUE populates the entire value
652130803Smarcel         including the location.  */
653130803Smarcel      REGISTER_TO_VALUE (frame, regnum, type, VALUE_CONTENTS_RAW (v));
654130803Smarcel      VALUE_LVAL (v) = lval_reg_frame_relative;
655130803Smarcel      VALUE_FRAME_ID (v) = get_frame_id (frame);
656130803Smarcel      VALUE_FRAME_REGNUM (v) = regnum;
657130803Smarcel    }
658130803Smarcel  else
659130803Smarcel    {
66019370Spst      int local_regnum;
66119370Spst      int mem_stor = 0, reg_stor = 0;
66219370Spst      int mem_tracking = 1;
66319370Spst      CORE_ADDR last_addr = 0;
66419370Spst      CORE_ADDR first_addr = 0;
665130803Smarcel      int first_realnum = regnum;
666130803Smarcel      int len = TYPE_LENGTH (type);
667130803Smarcel      int value_bytes_copied;
668130803Smarcel      int optimized = 0;
669130803Smarcel      char *value_bytes = (char *) alloca (len + MAX_REGISTER_SIZE);
67019370Spst
67119370Spst      /* Copy all of the data out, whereever it may be.  */
672130803Smarcel      for (local_regnum = regnum, value_bytes_copied = 0;
673130803Smarcel	   value_bytes_copied < len;
674130803Smarcel	   (value_bytes_copied += DEPRECATED_REGISTER_RAW_SIZE (local_regnum),
675130803Smarcel	    ++local_regnum))
67619370Spst	{
677130803Smarcel	  int realnum;
678130803Smarcel	  int optim;
679130803Smarcel	  enum lval_type lval;
680130803Smarcel	  CORE_ADDR addr;
681130803Smarcel	  frame_register (frame, local_regnum, &optim, &lval, &addr,
682130803Smarcel			  &realnum, value_bytes + value_bytes_copied);
683130803Smarcel	  optimized += optim;
684130803Smarcel	  if (register_cached (local_regnum) == -1)
685130803Smarcel	    return NULL;	/* register value not available */
686130803Smarcel
687130803Smarcel	  if (regnum == local_regnum)
68819370Spst	    {
689130803Smarcel	      first_addr = addr;
690130803Smarcel	      first_realnum = realnum;
69119370Spst	    }
69219370Spst	  if (lval == lval_register)
69319370Spst	    reg_stor++;
69419370Spst	  else
69519370Spst	    {
69619370Spst	      mem_stor++;
697130803Smarcel
698130803Smarcel	      mem_tracking = (mem_tracking
699130803Smarcel			      && (regnum == local_regnum
700130803Smarcel				  || addr == last_addr));
70119370Spst	    }
70219370Spst	  last_addr = addr;
70319370Spst	}
704130803Smarcel
705130803Smarcel      /* FIXME: cagney/2003-06-04: Shouldn't this always use
706130803Smarcel         lval_reg_frame_relative?  If it doesn't and the register's
707130803Smarcel         location changes (say after a resume) then this value is
708130803Smarcel         going to have wrong information.  */
70919370Spst      if ((reg_stor && mem_stor)
71019370Spst	  || (mem_stor && !mem_tracking))
71119370Spst	/* Mixed storage; all of the hassle we just went through was
71219370Spst	   for some good purpose.  */
71319370Spst	{
71419370Spst	  VALUE_LVAL (v) = lval_reg_frame_relative;
715130803Smarcel	  VALUE_FRAME_ID (v) = get_frame_id (frame);
71619370Spst	  VALUE_FRAME_REGNUM (v) = regnum;
71719370Spst	}
71819370Spst      else if (mem_stor)
71919370Spst	{
72019370Spst	  VALUE_LVAL (v) = lval_memory;
72119370Spst	  VALUE_ADDRESS (v) = first_addr;
72219370Spst	}
72319370Spst      else if (reg_stor)
72419370Spst	{
72519370Spst	  VALUE_LVAL (v) = lval_register;
72619370Spst	  VALUE_ADDRESS (v) = first_addr;
727130803Smarcel	  VALUE_REGNO (v) = first_realnum;
72819370Spst	}
72919370Spst      else
73098944Sobrien	internal_error (__FILE__, __LINE__,
73198944Sobrien			"value_from_register: Value not stored anywhere!");
732130803Smarcel
733130803Smarcel      VALUE_OPTIMIZED_OUT (v) = optimized;
734130803Smarcel
73519370Spst      /* Any structure stored in more than one register will always be
736130803Smarcel         an integral number of registers.  Otherwise, you need to do
73798944Sobrien         some fiddling with the last register copied here for little
73898944Sobrien         endian machines.  */
739130803Smarcel      if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
740130803Smarcel	  && len < DEPRECATED_REGISTER_RAW_SIZE (regnum))
741130803Smarcel	/* Big-endian, and we want less than full size.  */
742130803Smarcel	VALUE_OFFSET (v) = DEPRECATED_REGISTER_RAW_SIZE (regnum) - len;
743130803Smarcel      else
744130803Smarcel	VALUE_OFFSET (v) = 0;
745130803Smarcel      memcpy (VALUE_CONTENTS_RAW (v), value_bytes + VALUE_OFFSET (v), len);
74619370Spst    }
74719370Spst  return v;
74819370Spst}
749130803Smarcel
75019370Spst
75119370Spst/* Given a struct symbol for a variable or function,
75219370Spst   and a stack frame id,
75319370Spst   return a (pointer to a) struct value containing the properly typed
75419370Spst   address.  */
75519370Spst
75698944Sobrienstruct value *
757130803Smarcellocate_var_value (struct symbol *var, struct frame_info *frame)
75819370Spst{
75919370Spst  CORE_ADDR addr = 0;
76019370Spst  struct type *type = SYMBOL_TYPE (var);
76198944Sobrien  struct value *lazy_value;
76219370Spst
76319370Spst  /* Evaluate it first; if the result is a memory address, we're fine.
76419370Spst     Lazy evaluation pays off here. */
76519370Spst
76619370Spst  lazy_value = read_var_value (var, frame);
76719370Spst  if (lazy_value == 0)
768130803Smarcel    error ("Address of \"%s\" is unknown.", SYMBOL_PRINT_NAME (var));
76919370Spst
77019370Spst  if (VALUE_LAZY (lazy_value)
77119370Spst      || TYPE_CODE (type) == TYPE_CODE_FUNC)
77219370Spst    {
77398944Sobrien      struct value *val;
77446283Sdfr
77519370Spst      addr = VALUE_ADDRESS (lazy_value);
77698944Sobrien      val = value_from_pointer (lookup_pointer_type (type), addr);
77746283Sdfr      VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (lazy_value);
77846283Sdfr      return val;
77919370Spst    }
78019370Spst
78119370Spst  /* Not a memory address; check what the problem was.  */
78298944Sobrien  switch (VALUE_LVAL (lazy_value))
78319370Spst    {
78419370Spst    case lval_register:
78598944Sobrien	gdb_assert (REGISTER_NAME (VALUE_REGNO (lazy_value)) != NULL
78698944Sobrien	            && *REGISTER_NAME (VALUE_REGNO (lazy_value)) != '\0');
78798944Sobrien      error("Address requested for identifier "
78898944Sobrien	    "\"%s\" which is in register $%s",
789130803Smarcel            SYMBOL_PRINT_NAME (var),
79098944Sobrien	    REGISTER_NAME (VALUE_REGNO (lazy_value)));
79198944Sobrien      break;
79298944Sobrien
79319370Spst    case lval_reg_frame_relative:
79498944Sobrien	gdb_assert (REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value)) != NULL
79598944Sobrien	            && *REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value)) != '\0');
79698944Sobrien      error("Address requested for identifier "
79798944Sobrien	    "\"%s\" which is in frame register $%s",
798130803Smarcel            SYMBOL_PRINT_NAME (var),
79998944Sobrien	    REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value)));
80019370Spst      break;
80119370Spst
80219370Spst    default:
80319370Spst      error ("Can't take address of \"%s\" which isn't an lvalue.",
804130803Smarcel	     SYMBOL_PRINT_NAME (var));
80519370Spst      break;
80619370Spst    }
80798944Sobrien  return 0;			/* For lint -- never reached */
80819370Spst}
809