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