Import gdb-7.10.1
[dragonfly.git] / contrib / gdb-7 / gdb / findvar.c
1 /* Find a variable's value in memory, for GDB, the GNU debugger.
2
3    Copyright (C) 1986-2015 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
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 3 of the License, or
10    (at your option) any later version.
11
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.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
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 #include "floatformat.h"
29 #include "symfile.h"            /* for overlay functions */
30 #include "regcache.h"
31 #include "user-regs.h"
32 #include "block.h"
33 #include "objfiles.h"
34 #include "language.h"
35
36 /* Basic byte-swapping routines.  All 'extract' functions return a
37    host-format integer from a target-format integer at ADDR which is
38    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 (const gdb_byte *addr, int len,
50                         enum bfd_endian byte_order)
51 {
52   LONGEST retval;
53   const unsigned char *p;
54   const unsigned char *startaddr = addr;
55   const unsigned char *endaddr = startaddr + len;
56
57   if (len > (int) sizeof (LONGEST))
58     error (_("\
59 That operation is not available on integers of more than %d bytes."),
60            (int) sizeof (LONGEST));
61
62   /* Start at the most significant end of the integer, and work towards
63      the least significant.  */
64   if (byte_order == BFD_ENDIAN_BIG)
65     {
66       p = startaddr;
67       /* Do the sign extension once at the start.  */
68       retval = ((LONGEST) * p ^ 0x80) - 0x80;
69       for (++p; p < endaddr; ++p)
70         retval = (retval << 8) | *p;
71     }
72   else
73     {
74       p = endaddr - 1;
75       /* Do the sign extension once at the start.  */
76       retval = ((LONGEST) * p ^ 0x80) - 0x80;
77       for (--p; p >= startaddr; --p)
78         retval = (retval << 8) | *p;
79     }
80   return retval;
81 }
82
83 ULONGEST
84 extract_unsigned_integer (const gdb_byte *addr, int len,
85                           enum bfd_endian byte_order)
86 {
87   ULONGEST retval;
88   const unsigned char *p;
89   const unsigned char *startaddr = addr;
90   const unsigned char *endaddr = startaddr + len;
91
92   if (len > (int) sizeof (ULONGEST))
93     error (_("\
94 That operation is not available on integers of more than %d bytes."),
95            (int) sizeof (ULONGEST));
96
97   /* Start at the most significant end of the integer, and work towards
98      the least significant.  */
99   retval = 0;
100   if (byte_order == BFD_ENDIAN_BIG)
101     {
102       for (p = startaddr; p < endaddr; ++p)
103         retval = (retval << 8) | *p;
104     }
105   else
106     {
107       for (p = endaddr - 1; p >= startaddr; --p)
108         retval = (retval << 8) | *p;
109     }
110   return retval;
111 }
112
113 /* Sometimes a long long unsigned integer can be extracted as a
114    LONGEST value.  This is done so that we can print these values
115    better.  If this integer can be converted to a LONGEST, this
116    function returns 1 and sets *PVAL.  Otherwise it returns 0.  */
117
118 int
119 extract_long_unsigned_integer (const gdb_byte *addr, int orig_len,
120                                enum bfd_endian byte_order, LONGEST *pval)
121 {
122   const gdb_byte *p;
123   const gdb_byte *first_addr;
124   int len;
125
126   len = orig_len;
127   if (byte_order == BFD_ENDIAN_BIG)
128     {
129       for (p = addr;
130            len > (int) sizeof (LONGEST) && p < addr + orig_len;
131            p++)
132         {
133           if (*p == 0)
134             len--;
135           else
136             break;
137         }
138       first_addr = p;
139     }
140   else
141     {
142       first_addr = addr;
143       for (p = addr + orig_len - 1;
144            len > (int) sizeof (LONGEST) && p >= addr;
145            p--)
146         {
147           if (*p == 0)
148             len--;
149           else
150             break;
151         }
152     }
153
154   if (len <= (int) sizeof (LONGEST))
155     {
156       *pval = (LONGEST) extract_unsigned_integer (first_addr,
157                                                   sizeof (LONGEST),
158                                                   byte_order);
159       return 1;
160     }
161
162   return 0;
163 }
164
165
166 /* Treat the bytes at BUF as a pointer of type TYPE, and return the
167    address it represents.  */
168 CORE_ADDR
169 extract_typed_address (const gdb_byte *buf, struct type *type)
170 {
171   if (TYPE_CODE (type) != TYPE_CODE_PTR
172       && TYPE_CODE (type) != TYPE_CODE_REF)
173     internal_error (__FILE__, __LINE__,
174                     _("extract_typed_address: "
175                     "type is not a pointer or reference"));
176
177   return gdbarch_pointer_to_address (get_type_arch (type), type, buf);
178 }
179
180 /* All 'store' functions accept a host-format integer and store a
181    target-format integer at ADDR which is LEN bytes long.  */
182
183 void
184 store_signed_integer (gdb_byte *addr, int len,
185                       enum bfd_endian byte_order, LONGEST val)
186 {
187   gdb_byte *p;
188   gdb_byte *startaddr = addr;
189   gdb_byte *endaddr = startaddr + len;
190
191   /* Start at the least significant end of the integer, and work towards
192      the most significant.  */
193   if (byte_order == BFD_ENDIAN_BIG)
194     {
195       for (p = endaddr - 1; p >= startaddr; --p)
196         {
197           *p = val & 0xff;
198           val >>= 8;
199         }
200     }
201   else
202     {
203       for (p = startaddr; p < endaddr; ++p)
204         {
205           *p = val & 0xff;
206           val >>= 8;
207         }
208     }
209 }
210
211 void
212 store_unsigned_integer (gdb_byte *addr, int len,
213                         enum bfd_endian byte_order, ULONGEST val)
214 {
215   unsigned char *p;
216   unsigned char *startaddr = (unsigned char *) addr;
217   unsigned char *endaddr = startaddr + len;
218
219   /* Start at the least significant end of the integer, and work towards
220      the most significant.  */
221   if (byte_order == BFD_ENDIAN_BIG)
222     {
223       for (p = endaddr - 1; p >= startaddr; --p)
224         {
225           *p = val & 0xff;
226           val >>= 8;
227         }
228     }
229   else
230     {
231       for (p = startaddr; p < endaddr; ++p)
232         {
233           *p = val & 0xff;
234           val >>= 8;
235         }
236     }
237 }
238
239 /* Store the address ADDR as a pointer of type TYPE at BUF, in target
240    form.  */
241 void
242 store_typed_address (gdb_byte *buf, struct type *type, CORE_ADDR addr)
243 {
244   if (TYPE_CODE (type) != TYPE_CODE_PTR
245       && TYPE_CODE (type) != TYPE_CODE_REF)
246     internal_error (__FILE__, __LINE__,
247                     _("store_typed_address: "
248                     "type is not a pointer or reference"));
249
250   gdbarch_address_to_pointer (get_type_arch (type), type, buf, addr);
251 }
252
253
254
255 /* Return a `value' with the contents of (virtual or cooked) register
256    REGNUM as found in the specified FRAME.  The register's type is
257    determined by register_type().  */
258
259 struct value *
260 value_of_register (int regnum, struct frame_info *frame)
261 {
262   struct gdbarch *gdbarch = get_frame_arch (frame);
263   struct value *reg_val;
264
265   /* User registers lie completely outside of the range of normal
266      registers.  Catch them early so that the target never sees them.  */
267   if (regnum >= gdbarch_num_regs (gdbarch)
268                 + gdbarch_num_pseudo_regs (gdbarch))
269     return value_of_user_reg (regnum, frame);
270
271   reg_val = value_of_register_lazy (frame, regnum);
272   value_fetch_lazy (reg_val);
273   return reg_val;
274 }
275
276 /* Return a `value' with the contents of (virtual or cooked) register
277    REGNUM as found in the specified FRAME.  The register's type is
278    determined by register_type().  The value is not fetched.  */
279
280 struct value *
281 value_of_register_lazy (struct frame_info *frame, int regnum)
282 {
283   struct gdbarch *gdbarch = get_frame_arch (frame);
284   struct value *reg_val;
285
286   gdb_assert (regnum < (gdbarch_num_regs (gdbarch)
287                         + gdbarch_num_pseudo_regs (gdbarch)));
288
289   /* We should have a valid (i.e. non-sentinel) frame.  */
290   gdb_assert (frame_id_p (get_frame_id (frame)));
291
292   reg_val = allocate_value_lazy (register_type (gdbarch, regnum));
293   VALUE_LVAL (reg_val) = lval_register;
294   VALUE_REGNUM (reg_val) = regnum;
295   VALUE_FRAME_ID (reg_val) = get_frame_id (frame);
296   return reg_val;
297 }
298
299 /* Given a pointer of type TYPE in target form in BUF, return the
300    address it represents.  */
301 CORE_ADDR
302 unsigned_pointer_to_address (struct gdbarch *gdbarch,
303                              struct type *type, const gdb_byte *buf)
304 {
305   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
306
307   return extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
308 }
309
310 CORE_ADDR
311 signed_pointer_to_address (struct gdbarch *gdbarch,
312                            struct type *type, const gdb_byte *buf)
313 {
314   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
315
316   return extract_signed_integer (buf, TYPE_LENGTH (type), byte_order);
317 }
318
319 /* Given an address, store it as a pointer of type TYPE in target
320    format in BUF.  */
321 void
322 unsigned_address_to_pointer (struct gdbarch *gdbarch, struct type *type,
323                              gdb_byte *buf, CORE_ADDR addr)
324 {
325   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
326
327   store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr);
328 }
329
330 void
331 address_to_signed_pointer (struct gdbarch *gdbarch, struct type *type,
332                            gdb_byte *buf, CORE_ADDR addr)
333 {
334   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
335
336   store_signed_integer (buf, TYPE_LENGTH (type), byte_order, addr);
337 }
338 \f
339 /* Will calling read_var_value or locate_var_value on SYM end
340    up caring what frame it is being evaluated relative to?  SYM must
341    be non-NULL.  */
342 int
343 symbol_read_needs_frame (struct symbol *sym)
344 {
345   if (SYMBOL_COMPUTED_OPS (sym) != NULL)
346     return SYMBOL_COMPUTED_OPS (sym)->read_needs_frame (sym);
347
348   switch (SYMBOL_CLASS (sym))
349     {
350       /* All cases listed explicitly so that gcc -Wall will detect it if
351          we failed to consider one.  */
352     case LOC_COMPUTED:
353       gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
354
355     case LOC_REGISTER:
356     case LOC_ARG:
357     case LOC_REF_ARG:
358     case LOC_REGPARM_ADDR:
359     case LOC_LOCAL:
360       return 1;
361
362     case LOC_UNDEF:
363     case LOC_CONST:
364     case LOC_STATIC:
365     case LOC_TYPEDEF:
366
367     case LOC_LABEL:
368       /* Getting the address of a label can be done independently of the block,
369          even if some *uses* of that address wouldn't work so well without
370          the right frame.  */
371
372     case LOC_BLOCK:
373     case LOC_CONST_BYTES:
374     case LOC_UNRESOLVED:
375     case LOC_OPTIMIZED_OUT:
376       return 0;
377     }
378   return 1;
379 }
380
381 /* Private data to be used with minsym_lookup_iterator_cb.  */
382
383 struct minsym_lookup_data
384 {
385   /* The name of the minimal symbol we are searching for.  */
386   const char *name;
387
388   /* The field where the callback should store the minimal symbol
389      if found.  It should be initialized to NULL before the search
390      is started.  */
391   struct bound_minimal_symbol result;
392 };
393
394 /* A callback function for gdbarch_iterate_over_objfiles_in_search_order.
395    It searches by name for a minimal symbol within the given OBJFILE.
396    The arguments are passed via CB_DATA, which in reality is a pointer
397    to struct minsym_lookup_data.  */
398
399 static int
400 minsym_lookup_iterator_cb (struct objfile *objfile, void *cb_data)
401 {
402   struct minsym_lookup_data *data = (struct minsym_lookup_data *) cb_data;
403
404   gdb_assert (data->result.minsym == NULL);
405
406   data->result = lookup_minimal_symbol (data->name, NULL, objfile);
407
408   /* The iterator should stop iff a match was found.  */
409   return (data->result.minsym != NULL);
410 }
411
412 /* A default implementation for the "la_read_var_value" hook in
413    the language vector which should work in most situations.  */
414
415 struct value *
416 default_read_var_value (struct symbol *var, struct frame_info *frame)
417 {
418   struct value *v;
419   struct type *type = SYMBOL_TYPE (var);
420   CORE_ADDR addr;
421
422   /* Call check_typedef on our type to make sure that, if TYPE is
423      a TYPE_CODE_TYPEDEF, its length is set to the length of the target type
424      instead of zero.  However, we do not replace the typedef type by the
425      target type, because we want to keep the typedef in order to be able to
426      set the returned value type description correctly.  */
427   check_typedef (type);
428
429   if (symbol_read_needs_frame (var))
430     gdb_assert (frame);
431
432   if (SYMBOL_COMPUTED_OPS (var) != NULL)
433     return SYMBOL_COMPUTED_OPS (var)->read_variable (var, frame);
434
435   switch (SYMBOL_CLASS (var))
436     {
437     case LOC_CONST:
438       if (is_dynamic_type (type))
439         {
440           /* Value is a constant byte-sequence and needs no memory access.  */
441           type = resolve_dynamic_type (type, NULL, /* Unused address.  */ 0);
442         }
443       /* Put the constant back in target format. */
444       v = allocate_value (type);
445       store_signed_integer (value_contents_raw (v), TYPE_LENGTH (type),
446                             gdbarch_byte_order (get_type_arch (type)),
447                             (LONGEST) SYMBOL_VALUE (var));
448       VALUE_LVAL (v) = not_lval;
449       return v;
450
451     case LOC_LABEL:
452       /* Put the constant back in target format.  */
453       v = allocate_value (type);
454       if (overlay_debugging)
455         {
456           CORE_ADDR addr
457             = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
458                                         SYMBOL_OBJ_SECTION (symbol_objfile (var),
459                                                             var));
460
461           store_typed_address (value_contents_raw (v), type, addr);
462         }
463       else
464         store_typed_address (value_contents_raw (v), type,
465                               SYMBOL_VALUE_ADDRESS (var));
466       VALUE_LVAL (v) = not_lval;
467       return v;
468
469     case LOC_CONST_BYTES:
470       if (is_dynamic_type (type))
471         {
472           /* Value is a constant byte-sequence and needs no memory access.  */
473           type = resolve_dynamic_type (type, NULL, /* Unused address.  */ 0);
474         }
475       v = allocate_value (type);
476       memcpy (value_contents_raw (v), SYMBOL_VALUE_BYTES (var),
477               TYPE_LENGTH (type));
478       VALUE_LVAL (v) = not_lval;
479       return v;
480
481     case LOC_STATIC:
482       if (overlay_debugging)
483         addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
484                                          SYMBOL_OBJ_SECTION (symbol_objfile (var),
485                                                              var));
486       else
487         addr = SYMBOL_VALUE_ADDRESS (var);
488       break;
489
490     case LOC_ARG:
491       addr = get_frame_args_address (frame);
492       if (!addr)
493         error (_("Unknown argument list address for `%s'."),
494                SYMBOL_PRINT_NAME (var));
495       addr += SYMBOL_VALUE (var);
496       break;
497
498     case LOC_REF_ARG:
499       {
500         struct value *ref;
501         CORE_ADDR argref;
502
503         argref = get_frame_args_address (frame);
504         if (!argref)
505           error (_("Unknown argument list address for `%s'."),
506                  SYMBOL_PRINT_NAME (var));
507         argref += SYMBOL_VALUE (var);
508         ref = value_at (lookup_pointer_type (type), argref);
509         addr = value_as_address (ref);
510         break;
511       }
512
513     case LOC_LOCAL:
514       addr = get_frame_locals_address (frame);
515       addr += SYMBOL_VALUE (var);
516       break;
517
518     case LOC_TYPEDEF:
519       error (_("Cannot look up value of a typedef `%s'."),
520              SYMBOL_PRINT_NAME (var));
521       break;
522
523     case LOC_BLOCK:
524       if (overlay_debugging)
525         addr = symbol_overlayed_address
526           (BLOCK_START (SYMBOL_BLOCK_VALUE (var)),
527            SYMBOL_OBJ_SECTION (symbol_objfile (var), var));
528       else
529         addr = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
530       break;
531
532     case LOC_REGISTER:
533     case LOC_REGPARM_ADDR:
534       {
535         int regno = SYMBOL_REGISTER_OPS (var)
536                       ->register_number (var, get_frame_arch (frame));
537         struct value *regval;
538
539         if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
540           {
541             regval = value_from_register (lookup_pointer_type (type),
542                                           regno,
543                                           frame);
544
545             if (regval == NULL)
546               error (_("Value of register variable not available for `%s'."),
547                      SYMBOL_PRINT_NAME (var));
548
549             addr = value_as_address (regval);
550           }
551         else
552           {
553             regval = value_from_register (type, regno, frame);
554
555             if (regval == NULL)
556               error (_("Value of register variable not available for `%s'."),
557                      SYMBOL_PRINT_NAME (var));
558             return regval;
559           }
560       }
561       break;
562
563     case LOC_COMPUTED:
564       gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
565
566     case LOC_UNRESOLVED:
567       {
568         struct minsym_lookup_data lookup_data;
569         struct minimal_symbol *msym;
570         struct obj_section *obj_section;
571
572         memset (&lookup_data, 0, sizeof (lookup_data));
573         lookup_data.name = SYMBOL_LINKAGE_NAME (var);
574
575         gdbarch_iterate_over_objfiles_in_search_order
576           (symbol_arch (var),
577            minsym_lookup_iterator_cb, &lookup_data,
578            symbol_objfile (var));
579         msym = lookup_data.result.minsym;
580
581         if (msym == NULL)
582           error (_("No global symbol \"%s\"."), SYMBOL_LINKAGE_NAME (var));
583         if (overlay_debugging)
584           addr = symbol_overlayed_address (BMSYMBOL_VALUE_ADDRESS (lookup_data.result),
585                                            MSYMBOL_OBJ_SECTION (lookup_data.result.objfile,
586                                                                 msym));
587         else
588           addr = BMSYMBOL_VALUE_ADDRESS (lookup_data.result);
589
590         obj_section = MSYMBOL_OBJ_SECTION (lookup_data.result.objfile, msym);
591         if (obj_section
592             && (obj_section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
593           addr = target_translate_tls_address (obj_section->objfile, addr);
594       }
595       break;
596
597     case LOC_OPTIMIZED_OUT:
598       return allocate_optimized_out_value (type);
599
600     default:
601       error (_("Cannot look up value of a botched symbol `%s'."),
602              SYMBOL_PRINT_NAME (var));
603       break;
604     }
605
606   v = value_at_lazy (type, addr);
607   return v;
608 }
609
610 /* Calls VAR's language la_read_var_value hook with the given arguments.  */
611
612 struct value *
613 read_var_value (struct symbol *var, struct frame_info *frame)
614 {
615   const struct language_defn *lang = language_def (SYMBOL_LANGUAGE (var));
616
617   gdb_assert (lang != NULL);
618   gdb_assert (lang->la_read_var_value != NULL);
619
620   return lang->la_read_var_value (var, frame);
621 }
622
623 /* Install default attributes for register values.  */
624
625 struct value *
626 default_value_from_register (struct gdbarch *gdbarch, struct type *type,
627                              int regnum, struct frame_id frame_id)
628 {
629   int len = TYPE_LENGTH (type);
630   struct value *value = allocate_value (type);
631
632   VALUE_LVAL (value) = lval_register;
633   VALUE_FRAME_ID (value) = frame_id;
634   VALUE_REGNUM (value) = regnum;
635
636   /* Any structure stored in more than one register will always be
637      an integral number of registers.  Otherwise, you need to do
638      some fiddling with the last register copied here for little
639      endian machines.  */
640   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
641       && len < register_size (gdbarch, regnum))
642     /* Big-endian, and we want less than full size.  */
643     set_value_offset (value, register_size (gdbarch, regnum) - len);
644   else
645     set_value_offset (value, 0);
646
647   return value;
648 }
649
650 /* VALUE must be an lval_register value.  If regnum is the value's
651    associated register number, and len the length of the values type,
652    read one or more registers in FRAME, starting with register REGNUM,
653    until we've read LEN bytes.
654
655    If any of the registers we try to read are optimized out, then mark the
656    complete resulting value as optimized out.  */
657
658 void
659 read_frame_register_value (struct value *value, struct frame_info *frame)
660 {
661   struct gdbarch *gdbarch = get_frame_arch (frame);
662   int offset = 0;
663   int reg_offset = value_offset (value);
664   int regnum = VALUE_REGNUM (value);
665   int len = TYPE_LENGTH (check_typedef (value_type (value)));
666
667   gdb_assert (VALUE_LVAL (value) == lval_register);
668
669   /* Skip registers wholly inside of REG_OFFSET.  */
670   while (reg_offset >= register_size (gdbarch, regnum))
671     {
672       reg_offset -= register_size (gdbarch, regnum);
673       regnum++;
674     }
675
676   /* Copy the data.  */
677   while (len > 0)
678     {
679       struct value *regval = get_frame_register_value (frame, regnum);
680       int reg_len = TYPE_LENGTH (value_type (regval)) - reg_offset;
681
682       /* If the register length is larger than the number of bytes
683          remaining to copy, then only copy the appropriate bytes.  */
684       if (reg_len > len)
685         reg_len = len;
686
687       value_contents_copy (value, offset, regval, reg_offset, reg_len);
688
689       offset += reg_len;
690       len -= reg_len;
691       reg_offset = 0;
692       regnum++;
693     }
694 }
695
696 /* Return a value of type TYPE, stored in register REGNUM, in frame FRAME.  */
697
698 struct value *
699 value_from_register (struct type *type, int regnum, struct frame_info *frame)
700 {
701   struct gdbarch *gdbarch = get_frame_arch (frame);
702   struct type *type1 = check_typedef (type);
703   struct value *v;
704
705   if (gdbarch_convert_register_p (gdbarch, regnum, type1))
706     {
707       int optim, unavail, ok;
708
709       /* The ISA/ABI need to something weird when obtaining the
710          specified value from this register.  It might need to
711          re-order non-adjacent, starting with REGNUM (see MIPS and
712          i386).  It might need to convert the [float] register into
713          the corresponding [integer] type (see Alpha).  The assumption
714          is that gdbarch_register_to_value populates the entire value
715          including the location.  */
716       v = allocate_value (type);
717       VALUE_LVAL (v) = lval_register;
718       VALUE_FRAME_ID (v) = get_frame_id (frame);
719       VALUE_REGNUM (v) = regnum;
720       ok = gdbarch_register_to_value (gdbarch, frame, regnum, type1,
721                                       value_contents_raw (v), &optim,
722                                       &unavail);
723
724       if (!ok)
725         {
726           if (optim)
727             mark_value_bytes_optimized_out (v, 0, TYPE_LENGTH (type));
728           if (unavail)
729             mark_value_bytes_unavailable (v, 0, TYPE_LENGTH (type));
730         }
731     }
732   else
733     {
734       /* Construct the value.  */
735       v = gdbarch_value_from_register (gdbarch, type,
736                                        regnum, get_frame_id (frame));
737
738       /* Get the data.  */
739       read_frame_register_value (v, frame);
740     }
741
742   return v;
743 }
744
745 /* Return contents of register REGNUM in frame FRAME as address.
746    Will abort if register value is not available.  */
747
748 CORE_ADDR
749 address_from_register (int regnum, struct frame_info *frame)
750 {
751   struct gdbarch *gdbarch = get_frame_arch (frame);
752   struct type *type = builtin_type (gdbarch)->builtin_data_ptr;
753   struct value *value;
754   CORE_ADDR result;
755
756   /* This routine may be called during early unwinding, at a time
757      where the ID of FRAME is not yet known.  Calling value_from_register
758      would therefore abort in get_frame_id.  However, since we only need
759      a temporary value that is never used as lvalue, we actually do not
760      really need to set its VALUE_FRAME_ID.  Therefore, we re-implement
761      the core of value_from_register, but use the null_frame_id.  */
762
763   /* Some targets require a special conversion routine even for plain
764      pointer types.  Avoid constructing a value object in those cases.  */
765   if (gdbarch_convert_register_p (gdbarch, regnum, type))
766     {
767       gdb_byte *buf = alloca (TYPE_LENGTH (type));
768       int optim, unavail, ok;
769
770       ok = gdbarch_register_to_value (gdbarch, frame, regnum, type,
771                                       buf, &optim, &unavail);
772       if (!ok)
773         {
774           /* This function is used while computing a location expression.
775              Complain about the value being optimized out, rather than
776              letting value_as_address complain about some random register
777              the expression depends on not being saved.  */
778           error_value_optimized_out ();
779         }
780
781       return unpack_long (type, buf);
782     }
783
784   value = gdbarch_value_from_register (gdbarch, type, regnum, null_frame_id);
785   read_frame_register_value (value, frame);
786
787   if (value_optimized_out (value))
788     {
789       /* This function is used while computing a location expression.
790          Complain about the value being optimized out, rather than
791          letting value_as_address complain about some random register
792          the expression depends on not being saved.  */
793       error_value_optimized_out ();
794     }
795
796   result = value_as_address (value);
797   release_value (value);
798   value_free (value);
799
800   return result;
801 }
802