* ldlang.c (lang_size_sections): When no address is given for a
[deliverable/binutils-gdb.git] / gdb / findvar.c
1 /* Find a variable's value in memory, for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "frame.h"
24 #include "value.h"
25 #include "gdbcore.h"
26 #include "inferior.h"
27 #include "target.h"
28
29 /* Basic byte-swapping routines. GDB has needed these for a long time...
30 All extract a target-format integer at ADDR which is LEN bytes long. */
31
32 #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
33 /* 8 bit characters are a pretty safe assumption these days, so we
34 assume it throughout all these swapping routines. If we had to deal with
35 9 bit characters, we would need to make len be in bits and would have
36 to re-write these routines... */
37 you lose
38 #endif
39
40 LONGEST
41 extract_signed_integer (addr, len)
42 PTR addr;
43 int len;
44 {
45 LONGEST retval;
46 unsigned char *p;
47 unsigned char *startaddr = (unsigned char *)addr;
48 unsigned char *endaddr = startaddr + len;
49
50 if (len > sizeof (LONGEST))
51 error ("\
52 That operation is not available on integers of more than %d bytes.",
53 sizeof (LONGEST));
54
55 /* Start at the most significant end of the integer, and work towards
56 the least significant. */
57 #if TARGET_BYTE_ORDER == BIG_ENDIAN
58 p = startaddr;
59 #else
60 p = endaddr - 1;
61 #endif
62 /* Do the sign extension once at the start. */
63 retval = ((LONGEST)*p ^ 0x80) - 0x80;
64 #if TARGET_BYTE_ORDER == BIG_ENDIAN
65 for (++p; p < endaddr; ++p)
66 #else
67 for (--p; p >= startaddr; --p)
68 #endif
69 {
70 retval = (retval << 8) | *p;
71 }
72 return retval;
73 }
74
75 unsigned LONGEST
76 extract_unsigned_integer (addr, len)
77 PTR addr;
78 int len;
79 {
80 unsigned LONGEST retval;
81 unsigned char *p;
82 unsigned char *startaddr = (unsigned char *)addr;
83 unsigned char *endaddr = startaddr + len;
84
85 if (len > sizeof (unsigned LONGEST))
86 error ("\
87 That operation is not available on integers of more than %d bytes.",
88 sizeof (unsigned LONGEST));
89
90 /* Start at the most significant end of the integer, and work towards
91 the least significant. */
92 retval = 0;
93 #if TARGET_BYTE_ORDER == BIG_ENDIAN
94 for (p = startaddr; p < endaddr; ++p)
95 #else
96 for (p = endaddr - 1; p >= startaddr; --p)
97 #endif
98 {
99 retval = (retval << 8) | *p;
100 }
101 return retval;
102 }
103
104 CORE_ADDR
105 extract_address (addr, len)
106 PTR addr;
107 int len;
108 {
109 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
110 whether we want this to be true eventually. */
111 return extract_unsigned_integer (addr, len);
112 }
113
114 void
115 store_signed_integer (addr, len, val)
116 PTR addr;
117 int len;
118 LONGEST val;
119 {
120 unsigned char *p;
121 unsigned char *startaddr = (unsigned char *)addr;
122 unsigned char *endaddr = startaddr + len;
123
124 /* Start at the least significant end of the integer, and work towards
125 the most significant. */
126 #if TARGET_BYTE_ORDER == BIG_ENDIAN
127 for (p = endaddr - 1; p >= startaddr; --p)
128 #else
129 for (p = startaddr; p < endaddr; ++p)
130 #endif
131 {
132 *p = val & 0xff;
133 val >>= 8;
134 }
135 }
136
137 void
138 store_unsigned_integer (addr, len, val)
139 PTR addr;
140 int len;
141 unsigned LONGEST val;
142 {
143 unsigned char *p;
144 unsigned char *startaddr = (unsigned char *)addr;
145 unsigned char *endaddr = startaddr + len;
146
147 /* Start at the least significant end of the integer, and work towards
148 the most significant. */
149 #if TARGET_BYTE_ORDER == BIG_ENDIAN
150 for (p = endaddr - 1; p >= startaddr; --p)
151 #else
152 for (p = startaddr; p < endaddr; ++p)
153 #endif
154 {
155 *p = val & 0xff;
156 val >>= 8;
157 }
158 }
159
160 void
161 store_address (addr, len, val)
162 PTR addr;
163 int len;
164 CORE_ADDR val;
165 {
166 /* Assume a CORE_ADDR can fit in a LONGEST (for now). Not sure
167 whether we want this to be true eventually. */
168 store_unsigned_integer (addr, len, (LONGEST)val);
169 }
170 \f
171 /* Swap LEN bytes at BUFFER between target and host byte-order. This is
172 the wrong way to do byte-swapping because it assumes that you have a way
173 to have a host variable of exactly the right size. Once extract_floating
174 and store_floating have been fixed, this can go away. */
175 #if TARGET_BYTE_ORDER == HOST_BYTE_ORDER
176 #define SWAP_TARGET_AND_HOST(buffer,len)
177 #else /* Target and host byte order differ. */
178 #define SWAP_TARGET_AND_HOST(buffer,len) \
179 { \
180 char tmp; \
181 char *p = (char *)(buffer); \
182 char *q = ((char *)(buffer)) + len - 1; \
183 for (; p < q; p++, q--) \
184 { \
185 tmp = *q; \
186 *q = *p; \
187 *p = tmp; \
188 } \
189 }
190 #endif /* Target and host byte order differ. */
191
192 /* There are many problems with floating point cross-debugging.
193
194 1. These routines only handle byte-swapping, not conversion of
195 formats. So if host is IEEE floating and target is VAX floating,
196 or vice-versa, it loses. This means that we can't (yet) use these
197 routines for extendeds. Extendeds are handled by
198 REGISTER_CONVERTIBLE. What we want is to use floatformat.h, but that
199 doesn't yet handle VAX floating at all.
200
201 2. We can't deal with it if there is more than one floating point
202 format in use. This has to be fixed at the unpack_double level.
203
204 3. We probably should have a LONGEST_DOUBLE or DOUBLEST or whatever
205 we want to call it which is long double where available. */
206
207 double
208 extract_floating (addr, len)
209 PTR addr;
210 int len;
211 {
212 if (len == sizeof (float))
213 {
214 float retval;
215 memcpy (&retval, addr, sizeof (retval));
216 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
217 return retval;
218 }
219 else if (len == sizeof (double))
220 {
221 double retval;
222 memcpy (&retval, addr, sizeof (retval));
223 SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
224 return retval;
225 }
226 else
227 {
228 error ("Can't deal with a floating point number of %d bytes.", len);
229 }
230 }
231
232 void
233 store_floating (addr, len, val)
234 PTR addr;
235 int len;
236 double val;
237 {
238 if (len == sizeof (float))
239 {
240 float floatval = val;
241 SWAP_TARGET_AND_HOST (&floatval, sizeof (floatval));
242 memcpy (addr, &floatval, sizeof (floatval));
243 }
244 else if (len == sizeof (double))
245 {
246 SWAP_TARGET_AND_HOST (&val, sizeof (val));
247 memcpy (addr, &val, sizeof (val));
248 }
249 else
250 {
251 error ("Can't deal with a floating point number of %d bytes.", len);
252 }
253 }
254 \f
255 #if !defined (GET_SAVED_REGISTER)
256
257 /* Return the address in which frame FRAME's value of register REGNUM
258 has been saved in memory. Or return zero if it has not been saved.
259 If REGNUM specifies the SP, the value we return is actually
260 the SP value, not an address where it was saved. */
261
262 CORE_ADDR
263 find_saved_register (frame, regnum)
264 FRAME frame;
265 int regnum;
266 {
267 struct frame_info *fi;
268 struct frame_saved_regs saved_regs;
269
270 register FRAME frame1 = 0;
271 register CORE_ADDR addr = 0;
272
273 if (frame == 0) /* No regs saved if want current frame */
274 return 0;
275
276 #ifdef HAVE_REGISTER_WINDOWS
277 /* We assume that a register in a register window will only be saved
278 in one place (since the name changes and/or disappears as you go
279 towards inner frames), so we only call get_frame_saved_regs on
280 the current frame. This is directly in contradiction to the
281 usage below, which assumes that registers used in a frame must be
282 saved in a lower (more interior) frame. This change is a result
283 of working on a register window machine; get_frame_saved_regs
284 always returns the registers saved within a frame, within the
285 context (register namespace) of that frame. */
286
287 /* However, note that we don't want this to return anything if
288 nothing is saved (if there's a frame inside of this one). Also,
289 callers to this routine asking for the stack pointer want the
290 stack pointer saved for *this* frame; this is returned from the
291 next frame. */
292
293
294 if (REGISTER_IN_WINDOW_P(regnum))
295 {
296 frame1 = get_next_frame (frame);
297 if (!frame1) return 0; /* Registers of this frame are
298 active. */
299
300 /* Get the SP from the next frame in; it will be this
301 current frame. */
302 if (regnum != SP_REGNUM)
303 frame1 = frame;
304
305 fi = get_frame_info (frame1);
306 get_frame_saved_regs (fi, &saved_regs);
307 return saved_regs.regs[regnum]; /* ... which might be zero */
308 }
309 #endif /* HAVE_REGISTER_WINDOWS */
310
311 /* Note that this next routine assumes that registers used in
312 frame x will be saved only in the frame that x calls and
313 frames interior to it. This is not true on the sparc, but the
314 above macro takes care of it, so we should be all right. */
315 while (1)
316 {
317 QUIT;
318 frame1 = get_prev_frame (frame1);
319 if (frame1 == 0 || frame1 == frame)
320 break;
321 fi = get_frame_info (frame1);
322 get_frame_saved_regs (fi, &saved_regs);
323 if (saved_regs.regs[regnum])
324 addr = saved_regs.regs[regnum];
325 }
326
327 return addr;
328 }
329
330 /* Find register number REGNUM relative to FRAME and put its (raw,
331 target format) contents in *RAW_BUFFER. Set *OPTIMIZED if the
332 variable was optimized out (and thus can't be fetched). Set *LVAL
333 to lval_memory, lval_register, or not_lval, depending on whether
334 the value was fetched from memory, from a register, or in a strange
335 and non-modifiable way (e.g. a frame pointer which was calculated
336 rather than fetched). Set *ADDRP to the address, either in memory
337 on as a REGISTER_BYTE offset into the registers array.
338
339 Note that this implementation never sets *LVAL to not_lval. But
340 it can be replaced by defining GET_SAVED_REGISTER and supplying
341 your own.
342
343 The argument RAW_BUFFER must point to aligned memory. */
344
345 void
346 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
347 char *raw_buffer;
348 int *optimized;
349 CORE_ADDR *addrp;
350 FRAME frame;
351 int regnum;
352 enum lval_type *lval;
353 {
354 CORE_ADDR addr;
355 /* Normal systems don't optimize out things with register numbers. */
356 if (optimized != NULL)
357 *optimized = 0;
358 addr = find_saved_register (frame, regnum);
359 if (addr != 0)
360 {
361 if (lval != NULL)
362 *lval = lval_memory;
363 if (regnum == SP_REGNUM)
364 {
365 if (raw_buffer != NULL)
366 {
367 /* Put it back in target format. */
368 store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), addr);
369 }
370 if (addrp != NULL)
371 *addrp = 0;
372 return;
373 }
374 if (raw_buffer != NULL)
375 read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
376 }
377 else
378 {
379 if (lval != NULL)
380 *lval = lval_register;
381 addr = REGISTER_BYTE (regnum);
382 if (raw_buffer != NULL)
383 read_register_gen (regnum, raw_buffer);
384 }
385 if (addrp != NULL)
386 *addrp = addr;
387 }
388 #endif /* GET_SAVED_REGISTER. */
389
390 /* Copy the bytes of register REGNUM, relative to the current stack frame,
391 into our memory at MYADDR, in target byte order.
392 The number of bytes copied is REGISTER_RAW_SIZE (REGNUM).
393
394 Returns 1 if could not be read, 0 if could. */
395
396 int
397 read_relative_register_raw_bytes (regnum, myaddr)
398 int regnum;
399 char *myaddr;
400 {
401 int optim;
402 if (regnum == FP_REGNUM && selected_frame)
403 {
404 /* Put it back in target format. */
405 store_address (myaddr, REGISTER_RAW_SIZE(FP_REGNUM),
406 FRAME_FP(selected_frame));
407 return 0;
408 }
409
410 get_saved_register (myaddr, &optim, (CORE_ADDR *) NULL, selected_frame,
411 regnum, (enum lval_type *)NULL);
412 return optim;
413 }
414
415 /* Return a `value' with the contents of register REGNUM
416 in its virtual format, with the type specified by
417 REGISTER_VIRTUAL_TYPE. */
418
419 value
420 value_of_register (regnum)
421 int regnum;
422 {
423 CORE_ADDR addr;
424 int optim;
425 register value reg_val;
426 char raw_buffer[MAX_REGISTER_RAW_SIZE];
427 enum lval_type lval;
428
429 get_saved_register (raw_buffer, &optim, &addr,
430 selected_frame, regnum, &lval);
431
432 reg_val = allocate_value (REGISTER_VIRTUAL_TYPE (regnum));
433
434 /* Convert raw data to virtual format if necessary. */
435
436 #ifdef REGISTER_CONVERTIBLE
437 if (REGISTER_CONVERTIBLE (regnum))
438 {
439 REGISTER_CONVERT_TO_VIRTUAL (regnum, REGISTER_VIRTUAL_TYPE (regnum),
440 raw_buffer, VALUE_CONTENTS_RAW (reg_val));
441 }
442 else
443 #endif
444 memcpy (VALUE_CONTENTS_RAW (reg_val), raw_buffer,
445 REGISTER_RAW_SIZE (regnum));
446 VALUE_LVAL (reg_val) = lval;
447 VALUE_ADDRESS (reg_val) = addr;
448 VALUE_REGNO (reg_val) = regnum;
449 VALUE_OPTIMIZED_OUT (reg_val) = optim;
450 return reg_val;
451 }
452 \f
453 /* Low level examining and depositing of registers.
454
455 The caller is responsible for making
456 sure that the inferior is stopped before calling the fetching routines,
457 or it will get garbage. (a change from GDB version 3, in which
458 the caller got the value from the last stop). */
459
460 /* Contents of the registers in target byte order.
461 We allocate some extra slop since we do a lot of memcpy's around `registers',
462 and failing-soft is better than failing hard. */
463 char registers[REGISTER_BYTES + /* SLOP */ 256];
464
465 /* Nonzero if that register has been fetched. */
466 char register_valid[NUM_REGS];
467
468 /* Indicate that registers may have changed, so invalidate the cache. */
469 void
470 registers_changed ()
471 {
472 int i;
473 for (i = 0; i < NUM_REGS; i++)
474 register_valid[i] = 0;
475 }
476
477 /* Indicate that all registers have been fetched, so mark them all valid. */
478 void
479 registers_fetched ()
480 {
481 int i;
482 for (i = 0; i < NUM_REGS; i++)
483 register_valid[i] = 1;
484 }
485
486 /* Copy LEN bytes of consecutive data from registers
487 starting with the REGBYTE'th byte of register data
488 into memory at MYADDR. */
489
490 void
491 read_register_bytes (regbyte, myaddr, len)
492 int regbyte;
493 char *myaddr;
494 int len;
495 {
496 /* Fetch all registers. */
497 int i;
498 for (i = 0; i < NUM_REGS; i++)
499 if (!register_valid[i])
500 {
501 target_fetch_registers (-1);
502 break;
503 }
504 if (myaddr != NULL)
505 memcpy (myaddr, &registers[regbyte], len);
506 }
507
508 /* Read register REGNO into memory at MYADDR, which must be large enough
509 for REGISTER_RAW_BYTES (REGNO). Target byte-order.
510 If the register is known to be the size of a CORE_ADDR or smaller,
511 read_register can be used instead. */
512 void
513 read_register_gen (regno, myaddr)
514 int regno;
515 char *myaddr;
516 {
517 if (!register_valid[regno])
518 target_fetch_registers (regno);
519 memcpy (myaddr, &registers[REGISTER_BYTE (regno)],
520 REGISTER_RAW_SIZE (regno));
521 }
522
523 /* Copy LEN bytes of consecutive data from memory at MYADDR
524 into registers starting with the REGBYTE'th byte of register data. */
525
526 void
527 write_register_bytes (regbyte, myaddr, len)
528 int regbyte;
529 char *myaddr;
530 int len;
531 {
532 /* Make sure the entire registers array is valid. */
533 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
534 memcpy (&registers[regbyte], myaddr, len);
535 target_store_registers (-1);
536 }
537
538 /* Return the raw contents of register REGNO, regarding it as an integer. */
539 /* This probably should be returning LONGEST rather than CORE_ADDR. */
540
541 CORE_ADDR
542 read_register (regno)
543 int regno;
544 {
545 if (!register_valid[regno])
546 target_fetch_registers (regno);
547
548 return extract_address (&registers[REGISTER_BYTE (regno)],
549 REGISTER_RAW_SIZE(regno));
550 }
551
552 /* Registers we shouldn't try to store. */
553 #if !defined (CANNOT_STORE_REGISTER)
554 #define CANNOT_STORE_REGISTER(regno) 0
555 #endif
556
557 /* Store VALUE, into the raw contents of register number REGNO. */
558 /* FIXME: The val arg should probably be a LONGEST. */
559
560 void
561 write_register (regno, val)
562 int regno;
563 LONGEST val;
564 {
565 PTR buf;
566 int size;
567
568 /* On the sparc, writing %g0 is a no-op, so we don't even want to change
569 the registers array if something writes to this register. */
570 if (CANNOT_STORE_REGISTER (regno))
571 return;
572
573 size = REGISTER_RAW_SIZE(regno);
574 buf = alloca (size);
575 store_signed_integer (buf, size, (LONGEST) val);
576
577 /* If we have a valid copy of the register, and new value == old value,
578 then don't bother doing the actual store. */
579
580 if (register_valid [regno])
581 {
582 if (memcmp (&registers[REGISTER_BYTE (regno)], buf, size) == 0)
583 return;
584 }
585
586 target_prepare_to_store ();
587
588 memcpy (&registers[REGISTER_BYTE (regno)], buf, size);
589
590 register_valid [regno] = 1;
591
592 target_store_registers (regno);
593 }
594
595 /* Record that register REGNO contains VAL.
596 This is used when the value is obtained from the inferior or core dump,
597 so there is no need to store the value there. */
598
599 void
600 supply_register (regno, val)
601 int regno;
602 char *val;
603 {
604 register_valid[regno] = 1;
605 memcpy (&registers[REGISTER_BYTE (regno)], val, REGISTER_RAW_SIZE (regno));
606
607 /* On some architectures, e.g. HPPA, there are a few stray bits in some
608 registers, that the rest of the code would like to ignore. */
609 #ifdef CLEAN_UP_REGISTER_VALUE
610 CLEAN_UP_REGISTER_VALUE(regno, &registers[REGISTER_BYTE(regno)]);
611 #endif
612 }
613 \f
614 /* Will calling read_var_value or locate_var_value on SYM end
615 up caring what frame it is being evaluated relative to? SYM must
616 be non-NULL. */
617 int
618 symbol_read_needs_frame (sym)
619 struct symbol *sym;
620 {
621 switch (SYMBOL_CLASS (sym))
622 {
623 /* All cases listed explicitly so that gcc -Wall will detect it if
624 we failed to consider one. */
625 case LOC_REGISTER:
626 case LOC_ARG:
627 case LOC_REF_ARG:
628 case LOC_REGPARM:
629 case LOC_REGPARM_ADDR:
630 case LOC_LOCAL:
631 case LOC_LOCAL_ARG:
632 case LOC_BASEREG:
633 case LOC_BASEREG_ARG:
634 return 1;
635
636 case LOC_UNDEF:
637 case LOC_CONST:
638 case LOC_STATIC:
639 case LOC_TYPEDEF:
640
641 case LOC_LABEL:
642 /* Getting the address of a label can be done independently of the block,
643 even if some *uses* of that address wouldn't work so well without
644 the right frame. */
645
646 case LOC_BLOCK:
647 case LOC_CONST_BYTES:
648 case LOC_OPTIMIZED_OUT:
649 return 0;
650 }
651 return 1;
652 }
653
654 /* Given a struct symbol for a variable,
655 and a stack frame id, read the value of the variable
656 and return a (pointer to a) struct value containing the value.
657 If the variable cannot be found, return a zero pointer.
658 If FRAME is NULL, use the selected_frame. */
659
660 value
661 read_var_value (var, frame)
662 register struct symbol *var;
663 FRAME frame;
664 {
665 register value v;
666 struct frame_info *fi;
667 struct type *type = SYMBOL_TYPE (var);
668 CORE_ADDR addr;
669 register int len;
670
671 v = allocate_value (type);
672 VALUE_LVAL (v) = lval_memory; /* The most likely possibility. */
673 len = TYPE_LENGTH (type);
674
675 if (frame == 0) frame = selected_frame;
676
677 switch (SYMBOL_CLASS (var))
678 {
679 case LOC_CONST:
680 /* Put the constant back in target format. */
681 store_signed_integer (VALUE_CONTENTS_RAW (v), len,
682 (LONGEST) SYMBOL_VALUE (var));
683 VALUE_LVAL (v) = not_lval;
684 return v;
685
686 case LOC_LABEL:
687 /* Put the constant back in target format. */
688 store_address (VALUE_CONTENTS_RAW (v), len, SYMBOL_VALUE_ADDRESS (var));
689 VALUE_LVAL (v) = not_lval;
690 return v;
691
692 case LOC_CONST_BYTES:
693 {
694 char *bytes_addr;
695 bytes_addr = SYMBOL_VALUE_BYTES (var);
696 memcpy (VALUE_CONTENTS_RAW (v), bytes_addr, len);
697 VALUE_LVAL (v) = not_lval;
698 return v;
699 }
700
701 case LOC_STATIC:
702 addr = SYMBOL_VALUE_ADDRESS (var);
703 break;
704
705 case LOC_ARG:
706 fi = get_frame_info (frame);
707 if (fi == NULL)
708 return 0;
709 addr = FRAME_ARGS_ADDRESS (fi);
710 if (!addr)
711 {
712 return 0;
713 }
714 addr += SYMBOL_VALUE (var);
715 break;
716
717 case LOC_REF_ARG:
718 fi = get_frame_info (frame);
719 if (fi == NULL)
720 return 0;
721 addr = FRAME_ARGS_ADDRESS (fi);
722 if (!addr)
723 {
724 return 0;
725 }
726 addr += SYMBOL_VALUE (var);
727 addr = read_memory_unsigned_integer
728 (addr, TARGET_PTR_BIT / TARGET_CHAR_BIT);
729 break;
730
731 case LOC_LOCAL:
732 case LOC_LOCAL_ARG:
733 fi = get_frame_info (frame);
734 if (fi == NULL)
735 return 0;
736 addr = FRAME_LOCALS_ADDRESS (fi);
737 addr += SYMBOL_VALUE (var);
738 break;
739
740 case LOC_BASEREG:
741 case LOC_BASEREG_ARG:
742 {
743 char buf[MAX_REGISTER_RAW_SIZE];
744 get_saved_register (buf, NULL, NULL, frame, SYMBOL_BASEREG (var),
745 NULL);
746 addr = extract_address (buf, REGISTER_RAW_SIZE (SYMBOL_BASEREG (var)));
747 addr += SYMBOL_VALUE (var);
748 break;
749 }
750
751 case LOC_TYPEDEF:
752 error ("Cannot look up value of a typedef");
753 break;
754
755 case LOC_BLOCK:
756 VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
757 return v;
758
759 case LOC_REGISTER:
760 case LOC_REGPARM:
761 case LOC_REGPARM_ADDR:
762 {
763 struct block *b;
764
765 if (frame == NULL)
766 return 0;
767 b = get_frame_block (frame);
768
769 v = value_from_register (type, SYMBOL_VALUE (var), frame);
770
771 if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
772 {
773 addr = *(CORE_ADDR *)VALUE_CONTENTS (v);
774 VALUE_LVAL (v) = lval_memory;
775 }
776 else
777 return v;
778 }
779 break;
780
781 case LOC_OPTIMIZED_OUT:
782 VALUE_LVAL (v) = not_lval;
783 VALUE_OPTIMIZED_OUT (v) = 1;
784 return v;
785
786 default:
787 error ("Cannot look up value of a botched symbol.");
788 break;
789 }
790
791 VALUE_ADDRESS (v) = addr;
792 VALUE_LAZY (v) = 1;
793 return v;
794 }
795
796 /* Return a value of type TYPE, stored in register REGNUM, in frame
797 FRAME. */
798
799 value
800 value_from_register (type, regnum, frame)
801 struct type *type;
802 int regnum;
803 FRAME frame;
804 {
805 char raw_buffer [MAX_REGISTER_RAW_SIZE];
806 CORE_ADDR addr;
807 int optim;
808 value v = allocate_value (type);
809 int len = TYPE_LENGTH (type);
810 char *value_bytes = 0;
811 int value_bytes_copied = 0;
812 int num_storage_locs;
813 enum lval_type lval;
814
815 VALUE_REGNO (v) = regnum;
816
817 num_storage_locs = (len > REGISTER_VIRTUAL_SIZE (regnum) ?
818 ((len - 1) / REGISTER_RAW_SIZE (regnum)) + 1 :
819 1);
820
821 if (num_storage_locs > 1
822 #ifdef GDB_TARGET_IS_H8500
823 || TYPE_CODE (type) == TYPE_CODE_PTR
824 #endif
825 )
826 {
827 /* Value spread across multiple storage locations. */
828
829 int local_regnum;
830 int mem_stor = 0, reg_stor = 0;
831 int mem_tracking = 1;
832 CORE_ADDR last_addr = 0;
833 CORE_ADDR first_addr = 0;
834
835 value_bytes = (char *) alloca (len + MAX_REGISTER_RAW_SIZE);
836
837 /* Copy all of the data out, whereever it may be. */
838
839 #ifdef GDB_TARGET_IS_H8500
840 /* This piece of hideosity is required because the H8500 treats registers
841 differently depending upon whether they are used as pointers or not. As a
842 pointer, a register needs to have a page register tacked onto the front.
843 An alternate way to do this would be to have gcc output different register
844 numbers for the pointer & non-pointer form of the register. But, it
845 doesn't, so we're stuck with this. */
846
847 if (TYPE_CODE (type) == TYPE_CODE_PTR
848 && len > 2)
849 {
850 int page_regnum;
851
852 switch (regnum)
853 {
854 case R0_REGNUM: case R1_REGNUM: case R2_REGNUM: case R3_REGNUM:
855 page_regnum = SEG_D_REGNUM;
856 break;
857 case R4_REGNUM: case R5_REGNUM:
858 page_regnum = SEG_E_REGNUM;
859 break;
860 case R6_REGNUM: case R7_REGNUM:
861 page_regnum = SEG_T_REGNUM;
862 break;
863 }
864
865 value_bytes[0] = 0;
866 get_saved_register (value_bytes + 1,
867 &optim,
868 &addr,
869 frame,
870 page_regnum,
871 &lval);
872
873 if (lval == lval_register)
874 reg_stor++;
875 else
876 mem_stor++;
877 first_addr = addr;
878 last_addr = addr;
879
880 get_saved_register (value_bytes + 2,
881 &optim,
882 &addr,
883 frame,
884 regnum,
885 &lval);
886
887 if (lval == lval_register)
888 reg_stor++;
889 else
890 {
891 mem_stor++;
892 mem_tracking = mem_tracking && (addr == last_addr);
893 }
894 last_addr = addr;
895 }
896 else
897 #endif /* GDB_TARGET_IS_H8500 */
898 for (local_regnum = regnum;
899 value_bytes_copied < len;
900 (value_bytes_copied += REGISTER_RAW_SIZE (local_regnum),
901 ++local_regnum))
902 {
903 get_saved_register (value_bytes + value_bytes_copied,
904 &optim,
905 &addr,
906 frame,
907 local_regnum,
908 &lval);
909
910 if (regnum == local_regnum)
911 first_addr = addr;
912 if (lval == lval_register)
913 reg_stor++;
914 else
915 {
916 mem_stor++;
917
918 mem_tracking =
919 (mem_tracking
920 && (regnum == local_regnum
921 || addr == last_addr));
922 }
923 last_addr = addr;
924 }
925
926 if ((reg_stor && mem_stor)
927 || (mem_stor && !mem_tracking))
928 /* Mixed storage; all of the hassle we just went through was
929 for some good purpose. */
930 {
931 VALUE_LVAL (v) = lval_reg_frame_relative;
932 VALUE_FRAME (v) = FRAME_FP (frame);
933 VALUE_FRAME_REGNUM (v) = regnum;
934 }
935 else if (mem_stor)
936 {
937 VALUE_LVAL (v) = lval_memory;
938 VALUE_ADDRESS (v) = first_addr;
939 }
940 else if (reg_stor)
941 {
942 VALUE_LVAL (v) = lval_register;
943 VALUE_ADDRESS (v) = first_addr;
944 }
945 else
946 fatal ("value_from_register: Value not stored anywhere!");
947
948 VALUE_OPTIMIZED_OUT (v) = optim;
949
950 /* Any structure stored in more than one register will always be
951 an integral number of registers. Otherwise, you'd need to do
952 some fiddling with the last register copied here for little
953 endian machines. */
954
955 /* Copy into the contents section of the value. */
956 memcpy (VALUE_CONTENTS_RAW (v), value_bytes, len);
957
958 /* Finally do any conversion necessary when extracting this
959 type from more than one register. */
960 #ifdef REGISTER_CONVERT_TO_TYPE
961 REGISTER_CONVERT_TO_TYPE(regnum, type, VALUE_CONTENTS_RAW(v));
962 #endif
963 return v;
964 }
965
966 /* Data is completely contained within a single register. Locate the
967 register's contents in a real register or in core;
968 read the data in raw format. */
969
970 get_saved_register (raw_buffer, &optim, &addr, frame, regnum, &lval);
971 VALUE_OPTIMIZED_OUT (v) = optim;
972 VALUE_LVAL (v) = lval;
973 VALUE_ADDRESS (v) = addr;
974
975 /* Convert raw data to virtual format if necessary. */
976
977 #ifdef REGISTER_CONVERTIBLE
978 if (REGISTER_CONVERTIBLE (regnum))
979 {
980 REGISTER_CONVERT_TO_VIRTUAL (regnum, type,
981 raw_buffer, VALUE_CONTENTS_RAW (v));
982 }
983 else
984 #endif
985 {
986 /* Raw and virtual formats are the same for this register. */
987
988 #if TARGET_BYTE_ORDER == BIG_ENDIAN
989 if (len < REGISTER_RAW_SIZE (regnum))
990 {
991 /* Big-endian, and we want less than full size. */
992 VALUE_OFFSET (v) = REGISTER_RAW_SIZE (regnum) - len;
993 }
994 #endif
995
996 memcpy (VALUE_CONTENTS_RAW (v), raw_buffer + VALUE_OFFSET (v), len);
997 }
998
999 return v;
1000 }
1001 \f
1002 /* Given a struct symbol for a variable or function,
1003 and a stack frame id,
1004 return a (pointer to a) struct value containing the properly typed
1005 address. */
1006
1007 value
1008 locate_var_value (var, frame)
1009 register struct symbol *var;
1010 FRAME frame;
1011 {
1012 CORE_ADDR addr = 0;
1013 struct type *type = SYMBOL_TYPE (var);
1014 value lazy_value;
1015
1016 /* Evaluate it first; if the result is a memory address, we're fine.
1017 Lazy evaluation pays off here. */
1018
1019 lazy_value = read_var_value (var, frame);
1020 if (lazy_value == 0)
1021 error ("Address of \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
1022
1023 if (VALUE_LAZY (lazy_value)
1024 || TYPE_CODE (type) == TYPE_CODE_FUNC)
1025 {
1026 addr = VALUE_ADDRESS (lazy_value);
1027 return value_from_longest (lookup_pointer_type (type), (LONGEST) addr);
1028 }
1029
1030 /* Not a memory address; check what the problem was. */
1031 switch (VALUE_LVAL (lazy_value))
1032 {
1033 case lval_register:
1034 case lval_reg_frame_relative:
1035 error ("Address requested for identifier \"%s\" which is in a register.",
1036 SYMBOL_SOURCE_NAME (var));
1037 break;
1038
1039 default:
1040 error ("Can't take address of \"%s\" which isn't an lvalue.",
1041 SYMBOL_SOURCE_NAME (var));
1042 break;
1043 }
1044 return 0; /* For lint -- never reached */
1045 }
This page took 0.109689 seconds and 4 git commands to generate.