gdb/ChangeLog:
[external/binutils.git] / gdb / findvar.c
1 /* Find a variable's value in memory, for GDB, the GNU debugger.
2    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3    1996, 1997, 1998, 1999, 2000, 2001
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 #include "defs.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "frame.h"
27 #include "value.h"
28 #include "gdbcore.h"
29 #include "inferior.h"
30 #include "target.h"
31 #include "gdb_string.h"
32 #include "gdb_assert.h"
33 #include "floatformat.h"
34 #include "symfile.h"            /* for overlay functions */
35 #include "regcache.h"
36
37 /* Basic byte-swapping routines.  GDB has needed these for a long time...
38    All extract a target-format integer at ADDR which is LEN bytes long.  */
39
40 #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
41   /* 8 bit characters are a pretty safe assumption these days, so we
42      assume it throughout all these swapping routines.  If we had to deal with
43      9 bit characters, we would need to make len be in bits and would have
44      to re-write these routines...  */
45 you lose
46 #endif
47
48 LONGEST
49 extract_signed_integer (void *addr, int len)
50 {
51   LONGEST retval;
52   unsigned char *p;
53   unsigned char *startaddr = (unsigned char *) addr;
54   unsigned char *endaddr = startaddr + len;
55
56   if (len > (int) sizeof (LONGEST))
57     error ("\
58 That operation is not available on integers of more than %d bytes.",
59            sizeof (LONGEST));
60
61   /* Start at the most significant end of the integer, and work towards
62      the least significant.  */
63   if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
64     {
65       p = startaddr;
66       /* Do the sign extension once at the start.  */
67       retval = ((LONGEST) * p ^ 0x80) - 0x80;
68       for (++p; p < endaddr; ++p)
69         retval = (retval << 8) | *p;
70     }
71   else
72     {
73       p = endaddr - 1;
74       /* Do the sign extension once at the start.  */
75       retval = ((LONGEST) * p ^ 0x80) - 0x80;
76       for (--p; p >= startaddr; --p)
77         retval = (retval << 8) | *p;
78     }
79   return retval;
80 }
81
82 ULONGEST
83 extract_unsigned_integer (void *addr, int len)
84 {
85   ULONGEST retval;
86   unsigned char *p;
87   unsigned char *startaddr = (unsigned char *) addr;
88   unsigned char *endaddr = startaddr + len;
89
90   if (len > (int) sizeof (ULONGEST))
91     error ("\
92 That operation is not available on integers of more than %d bytes.",
93            sizeof (ULONGEST));
94
95   /* Start at the most significant end of the integer, and work towards
96      the least significant.  */
97   retval = 0;
98   if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
99     {
100       for (p = startaddr; p < endaddr; ++p)
101         retval = (retval << 8) | *p;
102     }
103   else
104     {
105       for (p = endaddr - 1; p >= startaddr; --p)
106         retval = (retval << 8) | *p;
107     }
108   return retval;
109 }
110
111 /* Sometimes a long long unsigned integer can be extracted as a
112    LONGEST value.  This is done so that we can print these values
113    better.  If this integer can be converted to a LONGEST, this
114    function returns 1 and sets *PVAL.  Otherwise it returns 0.  */
115
116 int
117 extract_long_unsigned_integer (void *addr, int orig_len, LONGEST *pval)
118 {
119   char *p, *first_addr;
120   int len;
121
122   len = orig_len;
123   if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
124     {
125       for (p = (char *) addr;
126            len > (int) sizeof (LONGEST) && p < (char *) addr + orig_len;
127            p++)
128         {
129           if (*p == 0)
130             len--;
131           else
132             break;
133         }
134       first_addr = p;
135     }
136   else
137     {
138       first_addr = (char *) addr;
139       for (p = (char *) addr + orig_len - 1;
140            len > (int) sizeof (LONGEST) && p >= (char *) addr;
141            p--)
142         {
143           if (*p == 0)
144             len--;
145           else
146             break;
147         }
148     }
149
150   if (len <= (int) sizeof (LONGEST))
151     {
152       *pval = (LONGEST) extract_unsigned_integer (first_addr,
153                                                   sizeof (LONGEST));
154       return 1;
155     }
156
157   return 0;
158 }
159
160
161 /* Treat the LEN bytes at ADDR as a target-format address, and return
162    that address.  ADDR is a buffer in the GDB process, not in the
163    inferior.
164
165    This function should only be used by target-specific code.  It
166    assumes that a pointer has the same representation as that thing's
167    address represented as an integer.  Some machines use word
168    addresses, or similarly munged things, for certain types of
169    pointers, so that assumption doesn't hold everywhere.
170
171    Common code should use extract_typed_address instead, or something
172    else based on POINTER_TO_ADDRESS.  */
173
174 CORE_ADDR
175 extract_address (void *addr, int len)
176 {
177   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
178      whether we want this to be true eventually.  */
179   return (CORE_ADDR) extract_unsigned_integer (addr, len);
180 }
181
182
183 /* Treat the bytes at BUF as a pointer of type TYPE, and return the
184    address it represents.  */
185 CORE_ADDR
186 extract_typed_address (void *buf, struct type *type)
187 {
188   if (TYPE_CODE (type) != TYPE_CODE_PTR
189       && TYPE_CODE (type) != TYPE_CODE_REF)
190     internal_error (__FILE__, __LINE__,
191                     "extract_typed_address: "
192                     "type is not a pointer or reference");
193
194   return POINTER_TO_ADDRESS (type, buf);
195 }
196
197
198 void
199 store_signed_integer (void *addr, int len, LONGEST val)
200 {
201   unsigned char *p;
202   unsigned char *startaddr = (unsigned char *) addr;
203   unsigned char *endaddr = startaddr + len;
204
205   /* Start at the least significant end of the integer, and work towards
206      the most significant.  */
207   if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
208     {
209       for (p = endaddr - 1; p >= startaddr; --p)
210         {
211           *p = val & 0xff;
212           val >>= 8;
213         }
214     }
215   else
216     {
217       for (p = startaddr; p < endaddr; ++p)
218         {
219           *p = val & 0xff;
220           val >>= 8;
221         }
222     }
223 }
224
225 void
226 store_unsigned_integer (void *addr, int len, ULONGEST val)
227 {
228   unsigned char *p;
229   unsigned char *startaddr = (unsigned char *) addr;
230   unsigned char *endaddr = startaddr + len;
231
232   /* Start at the least significant end of the integer, and work towards
233      the most significant.  */
234   if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
235     {
236       for (p = endaddr - 1; p >= startaddr; --p)
237         {
238           *p = val & 0xff;
239           val >>= 8;
240         }
241     }
242   else
243     {
244       for (p = startaddr; p < endaddr; ++p)
245         {
246           *p = val & 0xff;
247           val >>= 8;
248         }
249     }
250 }
251
252 /* Store the address VAL as a LEN-byte value in target byte order at
253    ADDR.  ADDR is a buffer in the GDB process, not in the inferior.
254
255    This function should only be used by target-specific code.  It
256    assumes that a pointer has the same representation as that thing's
257    address represented as an integer.  Some machines use word
258    addresses, or similarly munged things, for certain types of
259    pointers, so that assumption doesn't hold everywhere.
260
261    Common code should use store_typed_address instead, or something else
262    based on ADDRESS_TO_POINTER.  */
263 void
264 store_address (void *addr, int len, LONGEST val)
265 {
266   store_unsigned_integer (addr, len, val);
267 }
268
269
270 /* Store the address ADDR as a pointer of type TYPE at BUF, in target
271    form.  */
272 void
273 store_typed_address (void *buf, struct type *type, CORE_ADDR addr)
274 {
275   if (TYPE_CODE (type) != TYPE_CODE_PTR
276       && TYPE_CODE (type) != TYPE_CODE_REF)
277     internal_error (__FILE__, __LINE__,
278                     "store_typed_address: "
279                     "type is not a pointer or reference");
280
281   ADDRESS_TO_POINTER (type, buf, addr);
282 }
283
284
285
286 /* Return a `value' with the contents of (virtual or cooked) register
287    REGNUM as found in the specified FRAME.  The register's type is
288    determined by REGISTER_VIRTUAL_TYPE.
289
290    NOTE: returns NULL if register value is not available.  Caller will
291    check return value or die!  */
292
293 struct value *
294 value_of_register (int regnum, struct frame_info *frame)
295 {
296   CORE_ADDR addr;
297   int optim;
298   struct value *reg_val;
299   char *raw_buffer = (char*) alloca (MAX_REGISTER_RAW_SIZE);
300   enum lval_type lval;
301
302   get_saved_register (raw_buffer, &optim, &addr,
303                       frame, regnum, &lval);
304
305   if (register_cached (regnum) < 0)
306     return NULL;                /* register value not available */
307
308   reg_val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
309
310   /* Convert raw data to virtual format if necessary.  */
311
312   if (REGISTER_CONVERTIBLE (regnum))
313     {
314       REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
315                                    raw_buffer, VALUE_CONTENTS_RAW (reg_val));
316     }
317   else if (REGISTER_RAW_SIZE (regnum) == REGISTER_VIRTUAL_SIZE (regnum))
318     memcpy (VALUE_CONTENTS_RAW (reg_val), raw_buffer,
319             REGISTER_RAW_SIZE (regnum));
320   else
321     internal_error (__FILE__, __LINE__,
322                     "Register \"%s\" (%d) has conflicting raw (%d) and virtual (%d) size",
323                     REGISTER_NAME (regnum),
324                     regnum,
325                     REGISTER_RAW_SIZE (regnum),
326                     REGISTER_VIRTUAL_SIZE (regnum));
327   VALUE_LVAL (reg_val) = lval;
328   VALUE_ADDRESS (reg_val) = addr;
329   VALUE_REGNO (reg_val) = regnum;
330   VALUE_OPTIMIZED_OUT (reg_val) = optim;
331   return reg_val;
332 }
333
334 /* Given a pointer of type TYPE in target form in BUF, return the
335    address it represents.  */
336 CORE_ADDR
337 unsigned_pointer_to_address (struct type *type, void *buf)
338 {
339   return extract_address (buf, TYPE_LENGTH (type));
340 }
341
342 CORE_ADDR
343 signed_pointer_to_address (struct type *type, void *buf)
344 {
345   return extract_signed_integer (buf, TYPE_LENGTH (type));
346 }
347
348 /* Given an address, store it as a pointer of type TYPE in target
349    format in BUF.  */
350 void
351 unsigned_address_to_pointer (struct type *type, void *buf, CORE_ADDR addr)
352 {
353   store_address (buf, TYPE_LENGTH (type), addr);
354 }
355
356 void
357 address_to_signed_pointer (struct type *type, void *buf, CORE_ADDR addr)
358 {
359   store_signed_integer (buf, TYPE_LENGTH (type), addr);
360 }
361 \f
362 /* Will calling read_var_value or locate_var_value on SYM end
363    up caring what frame it is being evaluated relative to?  SYM must
364    be non-NULL.  */
365 int
366 symbol_read_needs_frame (struct symbol *sym)
367 {
368   switch (SYMBOL_CLASS (sym))
369     {
370       /* All cases listed explicitly so that gcc -Wall will detect it if
371          we failed to consider one.  */
372     case LOC_REGISTER:
373     case LOC_ARG:
374     case LOC_REF_ARG:
375     case LOC_REGPARM:
376     case LOC_REGPARM_ADDR:
377     case LOC_LOCAL:
378     case LOC_LOCAL_ARG:
379     case LOC_BASEREG:
380     case LOC_BASEREG_ARG:
381     case LOC_THREAD_LOCAL_STATIC:
382       return 1;
383
384     case LOC_UNDEF:
385     case LOC_CONST:
386     case LOC_STATIC:
387     case LOC_INDIRECT:
388     case LOC_TYPEDEF:
389
390     case LOC_LABEL:
391       /* Getting the address of a label can be done independently of the block,
392          even if some *uses* of that address wouldn't work so well without
393          the right frame.  */
394
395     case LOC_BLOCK:
396     case LOC_CONST_BYTES:
397     case LOC_UNRESOLVED:
398     case LOC_OPTIMIZED_OUT:
399       return 0;
400     }
401   return 1;
402 }
403
404 /* Given a struct symbol for a variable,
405    and a stack frame id, read the value of the variable
406    and return a (pointer to a) struct value containing the value. 
407    If the variable cannot be found, return a zero pointer.
408    If FRAME is NULL, use the selected_frame.  */
409
410 struct value *
411 read_var_value (register struct symbol *var, struct frame_info *frame)
412 {
413   register struct value *v;
414   struct type *type = SYMBOL_TYPE (var);
415   CORE_ADDR addr;
416   register int len;
417
418   v = allocate_value (type);
419   VALUE_LVAL (v) = lval_memory; /* The most likely possibility.  */
420   VALUE_BFD_SECTION (v) = SYMBOL_BFD_SECTION (var);
421
422   len = TYPE_LENGTH (type);
423
424   if (frame == NULL)
425     frame = selected_frame;
426
427   switch (SYMBOL_CLASS (var))
428     {
429     case LOC_CONST:
430       /* Put the constant back in target format.  */
431       store_signed_integer (VALUE_CONTENTS_RAW (v), len,
432                             (LONGEST) SYMBOL_VALUE (var));
433       VALUE_LVAL (v) = not_lval;
434       return v;
435
436     case LOC_LABEL:
437       /* Put the constant back in target format.  */
438       if (overlay_debugging)
439         {
440           CORE_ADDR addr
441             = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
442                                         SYMBOL_BFD_SECTION (var));
443           store_typed_address (VALUE_CONTENTS_RAW (v), type, addr);
444         }
445       else
446         store_typed_address (VALUE_CONTENTS_RAW (v), type,
447                               SYMBOL_VALUE_ADDRESS (var));
448       VALUE_LVAL (v) = not_lval;
449       return v;
450
451     case LOC_CONST_BYTES:
452       {
453         char *bytes_addr;
454         bytes_addr = SYMBOL_VALUE_BYTES (var);
455         memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
456         VALUE_LVAL (v) = not_lval;
457         return v;
458       }
459
460     case LOC_STATIC:
461       if (overlay_debugging)
462         addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
463                                          SYMBOL_BFD_SECTION (var));
464       else
465         addr = SYMBOL_VALUE_ADDRESS (var);
466       break;
467
468     case LOC_INDIRECT:
469       {
470         /* The import slot does not have a real address in it from the
471            dynamic loader (dld.sl on HP-UX), if the target hasn't
472            begun execution yet, so check for that. */
473         CORE_ADDR locaddr;
474         struct value *loc;
475         if (!target_has_execution)
476           error ("\
477 Attempt to access variable defined in different shared object or load module when\n\
478 addresses have not been bound by the dynamic loader. Try again when executable is running.");
479
480         locaddr = SYMBOL_VALUE_ADDRESS (var);
481         loc = value_at (lookup_pointer_type (type), locaddr, NULL);
482         addr = value_as_address (loc);
483       }
484
485     case LOC_ARG:
486       if (frame == NULL)
487         return 0;
488       addr = FRAME_ARGS_ADDRESS (frame);
489       if (!addr)
490         return 0;
491       addr += SYMBOL_VALUE (var);
492       break;
493
494     case LOC_REF_ARG:
495       {
496         struct value *ref;
497         CORE_ADDR argref;
498         if (frame == NULL)
499           return 0;
500         argref = FRAME_ARGS_ADDRESS (frame);
501         if (!argref)
502           return 0;
503         argref += SYMBOL_VALUE (var);
504         ref = value_at (lookup_pointer_type (type), argref, NULL);
505         addr = value_as_address (ref);
506         break;
507       }
508
509     case LOC_LOCAL:
510     case LOC_LOCAL_ARG:
511       if (frame == NULL)
512         return 0;
513       addr = FRAME_LOCALS_ADDRESS (frame);
514       addr += SYMBOL_VALUE (var);
515       break;
516
517     case LOC_BASEREG:
518     case LOC_BASEREG_ARG:
519     case LOC_THREAD_LOCAL_STATIC:
520       {
521         struct value *regval;
522
523         regval = value_from_register (lookup_pointer_type (type),
524                                       SYMBOL_BASEREG (var), frame);
525         if (regval == NULL)
526           error ("Value of base register not available.");
527         addr = value_as_address (regval);
528         addr += SYMBOL_VALUE (var);
529         break;
530       }
531
532     case LOC_TYPEDEF:
533       error ("Cannot look up value of a typedef");
534       break;
535
536     case LOC_BLOCK:
537       if (overlay_debugging)
538         VALUE_ADDRESS (v) = symbol_overlayed_address
539           (BLOCK_START (SYMBOL_BLOCK_VALUE (var)), SYMBOL_BFD_SECTION (var));
540       else
541         VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
542       return v;
543
544     case LOC_REGISTER:
545     case LOC_REGPARM:
546     case LOC_REGPARM_ADDR:
547       {
548         struct block *b;
549         int regno = SYMBOL_VALUE (var);
550         struct value *regval;
551
552         if (frame == NULL)
553           return 0;
554         b = get_frame_block (frame, 0);
555
556         if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
557           {
558             regval = value_from_register (lookup_pointer_type (type),
559                                           regno,
560                                           frame);
561
562             if (regval == NULL)
563               error ("Value of register variable not available.");
564
565             addr = value_as_address (regval);
566             VALUE_LVAL (v) = lval_memory;
567           }
568         else
569           {
570             regval = value_from_register (type, regno, frame);
571
572             if (regval == NULL)
573               error ("Value of register variable not available.");
574             return regval;
575           }
576       }
577       break;
578
579     case LOC_UNRESOLVED:
580       {
581         struct minimal_symbol *msym;
582
583         msym = lookup_minimal_symbol (SYMBOL_NAME (var), NULL, NULL);
584         if (msym == NULL)
585           return 0;
586         if (overlay_debugging)
587           addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (msym),
588                                            SYMBOL_BFD_SECTION (msym));
589         else
590           addr = SYMBOL_VALUE_ADDRESS (msym);
591       }
592       break;
593
594     case LOC_OPTIMIZED_OUT:
595       VALUE_LVAL (v) = not_lval;
596       VALUE_OPTIMIZED_OUT (v) = 1;
597       return v;
598
599     default:
600       error ("Cannot look up value of a botched symbol.");
601       break;
602     }
603
604   VALUE_ADDRESS (v) = addr;
605   VALUE_LAZY (v) = 1;
606   return v;
607 }
608
609 /* Return a value of type TYPE, stored in register REGNUM, in frame
610    FRAME.
611
612    NOTE: returns NULL if register value is not available.
613    Caller will check return value or die!  */
614
615 struct value *
616 value_from_register (struct type *type, int regnum, struct frame_info *frame)
617 {
618   char *raw_buffer = (char*) alloca (MAX_REGISTER_RAW_SIZE);
619   CORE_ADDR addr;
620   int optim;
621   struct value *v = allocate_value (type);
622   char *value_bytes = 0;
623   int value_bytes_copied = 0;
624   int num_storage_locs;
625   enum lval_type lval;
626   int len;
627
628   CHECK_TYPEDEF (type);
629   len = TYPE_LENGTH (type);
630
631   VALUE_REGNO (v) = regnum;
632
633   num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
634                       ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
635                       1);
636
637   if (num_storage_locs > 1
638 #ifdef GDB_TARGET_IS_H8500
639       || TYPE_CODE (type) == TYPE_CODE_PTR
640 #endif
641     )
642     {
643       /* Value spread across multiple storage locations.  */
644
645       int local_regnum;
646       int mem_stor = 0, reg_stor = 0;
647       int mem_tracking = 1;
648       CORE_ADDR last_addr = 0;
649       CORE_ADDR first_addr = 0;
650
651       value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
652
653       /* Copy all of the data out, whereever it may be.  */
654
655 #ifdef GDB_TARGET_IS_H8500
656 /* This piece of hideosity is required because the H8500 treats registers
657    differently depending upon whether they are used as pointers or not.  As a
658    pointer, a register needs to have a page register tacked onto the front.
659    An alternate way to do this would be to have gcc output different register
660    numbers for the pointer & non-pointer form of the register.  But, it
661    doesn't, so we're stuck with this.  */
662
663       if (TYPE_CODE (type) == TYPE_CODE_PTR
664           && len > 2)
665         {
666           int page_regnum;
667
668           switch (regnum)
669             {
670             case R0_REGNUM:
671             case R1_REGNUM:
672             case R2_REGNUM:
673             case R3_REGNUM:
674               page_regnum = SEG_D_REGNUM;
675               break;
676             case R4_REGNUM:
677             case R5_REGNUM:
678               page_regnum = SEG_E_REGNUM;
679               break;
680             case R6_REGNUM:
681             case R7_REGNUM:
682               page_regnum = SEG_T_REGNUM;
683               break;
684             }
685
686           value_bytes[0] = 0;
687           get_saved_register (value_bytes + 1,
688                               &optim,
689                               &addr,
690                               frame,
691                               page_regnum,
692                               &lval);
693
694           if (register_cached (page_regnum) == -1)
695             return NULL;        /* register value not available */
696
697           if (lval == lval_register)
698             reg_stor++;
699           else
700             mem_stor++;
701           first_addr = addr;
702           last_addr = addr;
703
704           get_saved_register (value_bytes + 2,
705                               &optim,
706                               &addr,
707                               frame,
708                               regnum,
709                               &lval);
710
711           if (register_cached (regnum) == -1)
712             return NULL;        /* register value not available */
713
714           if (lval == lval_register)
715             reg_stor++;
716           else
717             {
718               mem_stor++;
719               mem_tracking = mem_tracking && (addr == last_addr);
720             }
721           last_addr = addr;
722         }
723       else
724 #endif /* GDB_TARGET_IS_H8500 */
725         for (local_regnum = regnum;
726              value_bytes_copied < len;
727              (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
728               ++local_regnum))
729           {
730             get_saved_register (value_bytes + value_bytes_copied,
731                                 &optim,
732                                 &addr,
733                                 frame,
734                                 local_regnum,
735                                 &lval);
736
737             if (register_cached (local_regnum) == -1)
738               return NULL;      /* register value not available */
739
740             if (regnum == local_regnum)
741               first_addr = addr;
742             if (lval == lval_register)
743               reg_stor++;
744             else
745               {
746                 mem_stor++;
747
748                 mem_tracking =
749                   (mem_tracking
750                    && (regnum == local_regnum
751                        || addr == last_addr));
752               }
753             last_addr = addr;
754           }
755
756       if ((reg_stor && mem_stor)
757           || (mem_stor && !mem_tracking))
758         /* Mixed storage; all of the hassle we just went through was
759            for some good purpose.  */
760         {
761           VALUE_LVAL (v) = lval_reg_frame_relative;
762           VALUE_FRAME (v) = FRAME_FP (frame);
763           VALUE_FRAME_REGNUM (v) = regnum;
764         }
765       else if (mem_stor)
766         {
767           VALUE_LVAL (v) = lval_memory;
768           VALUE_ADDRESS (v) = first_addr;
769         }
770       else if (reg_stor)
771         {
772           VALUE_LVAL (v) = lval_register;
773           VALUE_ADDRESS (v) = first_addr;
774         }
775       else
776         internal_error (__FILE__, __LINE__,
777                         "value_from_register: Value not stored anywhere!");
778
779       VALUE_OPTIMIZED_OUT (v) = optim;
780
781       /* Any structure stored in more than one register will always be
782          an integral number of registers.  Otherwise, you'd need to do
783          some fiddling with the last register copied here for little
784          endian machines.  */
785
786       /* Copy into the contents section of the value.  */
787       memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len);
788
789       /* Finally do any conversion necessary when extracting this
790          type from more than one register.  */
791 #ifdef REGISTER_CONVERT_TO_TYPE
792       REGISTER_CONVERT_TO_TYPE (regnum, type, VALUE_CONTENTS_RAW (v));
793 #endif
794       return v;
795     }
796
797   /* Data is completely contained within a single register.  Locate the
798      register's contents in a real register or in core;
799      read the data in raw format.  */
800
801   get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval);
802
803   if (register_cached (regnum) == -1)
804     return NULL;                /* register value not available */
805
806   VALUE_OPTIMIZED_OUT (v) = optim;
807   VALUE_LVAL (v) = lval;
808   VALUE_ADDRESS (v) = addr;
809
810   /* Convert raw data to virtual format if necessary.  */
811
812   if (REGISTER_CONVERTIBLE (regnum))
813     {
814       REGISTER_CONVERT_TO_VIRTUAL (regnum, type,
815                                    raw_buffer, VALUE_CONTENTS_RAW (v));
816     }
817   else
818     {
819       /* Raw and virtual formats are the same for this register.  */
820
821       if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG && len < REGISTER_RAW_SIZE (regnum))
822         {
823           /* Big-endian, and we want less than full size.  */
824           VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
825         }
826
827       memcpy (VALUE_CONTENTS_RAW (v), raw_buffer + VALUE_OFFSET (v), len);
828     }
829
830   return v;
831 }
832 \f
833 /* Given a struct symbol for a variable or function,
834    and a stack frame id, 
835    return a (pointer to a) struct value containing the properly typed
836    address.  */
837
838 struct value *
839 locate_var_value (register struct symbol *var, struct frame_info *frame)
840 {
841   CORE_ADDR addr = 0;
842   struct type *type = SYMBOL_TYPE (var);
843   struct value *lazy_value;
844
845   /* Evaluate it first; if the result is a memory address, we're fine.
846      Lazy evaluation pays off here. */
847
848   lazy_value = read_var_value (var, frame);
849   if (lazy_value == 0)
850     error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
851
852   if (VALUE_LAZY (lazy_value)
853       || TYPE_CODE (type) == TYPE_CODE_FUNC)
854     {
855       struct value *val;
856
857       addr = VALUE_ADDRESS (lazy_value);
858       val = value_from_pointer (lookup_pointer_type (type), addr);
859       VALUE_BFD_SECTION (val) = VALUE_BFD_SECTION (lazy_value);
860       return val;
861     }
862
863   /* Not a memory address; check what the problem was.  */
864   switch (VALUE_LVAL (lazy_value))
865     {
866     case lval_register:
867         gdb_assert (REGISTER_NAME (VALUE_REGNO (lazy_value)) != NULL
868                     && *REGISTER_NAME (VALUE_REGNO (lazy_value)) != '\0');
869       error("Address requested for identifier "
870             "\"%s\" which is in register $%s",
871             SYMBOL_SOURCE_NAME (var), 
872             REGISTER_NAME (VALUE_REGNO (lazy_value)));
873       break;
874
875     case lval_reg_frame_relative:
876         gdb_assert (REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value)) != NULL
877                     && *REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value)) != '\0');
878       error("Address requested for identifier "
879             "\"%s\" which is in frame register $%s",
880             SYMBOL_SOURCE_NAME (var), 
881             REGISTER_NAME (VALUE_FRAME_REGNUM (lazy_value)));
882       break;
883
884     default:
885       error ("Can't take address of \"%s\" which isn't an lvalue.",
886              SYMBOL_SOURCE_NAME (var));
887       break;
888     }
889   return 0;                     /* For lint -- never reached */
890 }