Import gdb 7.3 into vendor branch
[dragonfly.git] / contrib / gdb-7 / gdb / gnu-v3-abi.c
1 /* Abstraction of GNU v3 abi.
2    Contributed by Jim Blandy <jimb@redhat.com>
3
4    Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5    Free Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "value.h"
24 #include "cp-abi.h"
25 #include "cp-support.h"
26 #include "demangle.h"
27 #include "objfiles.h"
28 #include "valprint.h"
29 #include "c-lang.h"
30
31 #include "gdb_assert.h"
32 #include "gdb_string.h"
33
34 static struct cp_abi_ops gnu_v3_abi_ops;
35
36 static int
37 gnuv3_is_vtable_name (const char *name)
38 {
39   return strncmp (name, "_ZTV", 4) == 0;
40 }
41
42 static int
43 gnuv3_is_operator_name (const char *name)
44 {
45   return strncmp (name, "operator", 8) == 0;
46 }
47
48
49 /* To help us find the components of a vtable, we build ourselves a
50    GDB type object representing the vtable structure.  Following the
51    V3 ABI, it goes something like this:
52
53    struct gdb_gnu_v3_abi_vtable {
54
55      / * An array of virtual call and virtual base offsets.  The real
56          length of this array depends on the class hierarchy; we use
57          negative subscripts to access the elements.  Yucky, but
58          better than the alternatives.  * /
59      ptrdiff_t vcall_and_vbase_offsets[0];
60
61      / * The offset from a virtual pointer referring to this table
62          to the top of the complete object.  * /
63      ptrdiff_t offset_to_top;
64
65      / * The type_info pointer for this class.  This is really a
66          std::type_info *, but GDB doesn't really look at the
67          type_info object itself, so we don't bother to get the type
68          exactly right.  * /
69      void *type_info;
70
71      / * Virtual table pointers in objects point here.  * /
72
73      / * Virtual function pointers.  Like the vcall/vbase array, the
74          real length of this table depends on the class hierarchy.  * /
75      void (*virtual_functions[0]) ();
76
77    };
78
79    The catch, of course, is that the exact layout of this table
80    depends on the ABI --- word size, endianness, alignment, etc.  So
81    the GDB type object is actually a per-architecture kind of thing.
82
83    vtable_type_gdbarch_data is a gdbarch per-architecture data pointer
84    which refers to the struct type * for this structure, laid out
85    appropriately for the architecture.  */
86 static struct gdbarch_data *vtable_type_gdbarch_data;
87
88
89 /* Human-readable names for the numbers of the fields above.  */
90 enum {
91   vtable_field_vcall_and_vbase_offsets,
92   vtable_field_offset_to_top,
93   vtable_field_type_info,
94   vtable_field_virtual_functions
95 };
96
97
98 /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable',
99    described above, laid out appropriately for ARCH.
100
101    We use this function as the gdbarch per-architecture data
102    initialization function.  */
103 static void *
104 build_gdb_vtable_type (struct gdbarch *arch)
105 {
106   struct type *t;
107   struct field *field_list, *field;
108   int offset;
109
110   struct type *void_ptr_type
111     = builtin_type (arch)->builtin_data_ptr;
112   struct type *ptr_to_void_fn_type
113     = builtin_type (arch)->builtin_func_ptr;
114
115   /* ARCH can't give us the true ptrdiff_t type, so we guess.  */
116   struct type *ptrdiff_type
117     = arch_integer_type (arch, gdbarch_ptr_bit (arch), 0, "ptrdiff_t");
118
119   /* We assume no padding is necessary, since GDB doesn't know
120      anything about alignment at the moment.  If this assumption bites
121      us, we should add a gdbarch method which, given a type, returns
122      the alignment that type requires, and then use that here.  */
123
124   /* Build the field list.  */
125   field_list = xmalloc (sizeof (struct field [4]));
126   memset (field_list, 0, sizeof (struct field [4]));
127   field = &field_list[0];
128   offset = 0;
129
130   /* ptrdiff_t vcall_and_vbase_offsets[0]; */
131   FIELD_NAME (*field) = "vcall_and_vbase_offsets";
132   FIELD_TYPE (*field) = lookup_array_range_type (ptrdiff_type, 0, -1);
133   FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
134   offset += TYPE_LENGTH (FIELD_TYPE (*field));
135   field++;
136
137   /* ptrdiff_t offset_to_top; */
138   FIELD_NAME (*field) = "offset_to_top";
139   FIELD_TYPE (*field) = ptrdiff_type;
140   FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
141   offset += TYPE_LENGTH (FIELD_TYPE (*field));
142   field++;
143
144   /* void *type_info; */
145   FIELD_NAME (*field) = "type_info";
146   FIELD_TYPE (*field) = void_ptr_type;
147   FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
148   offset += TYPE_LENGTH (FIELD_TYPE (*field));
149   field++;
150
151   /* void (*virtual_functions[0]) (); */
152   FIELD_NAME (*field) = "virtual_functions";
153   FIELD_TYPE (*field) = lookup_array_range_type (ptr_to_void_fn_type, 0, -1);
154   FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
155   offset += TYPE_LENGTH (FIELD_TYPE (*field));
156   field++;
157
158   /* We assumed in the allocation above that there were four fields.  */
159   gdb_assert (field == (field_list + 4));
160
161   t = arch_type (arch, TYPE_CODE_STRUCT, offset, NULL);
162   TYPE_NFIELDS (t) = field - field_list;
163   TYPE_FIELDS (t) = field_list;
164   TYPE_TAG_NAME (t) = "gdb_gnu_v3_abi_vtable";
165   INIT_CPLUS_SPECIFIC (t);
166
167   return t;
168 }
169
170
171 /* Return the ptrdiff_t type used in the vtable type.  */
172 static struct type *
173 vtable_ptrdiff_type (struct gdbarch *gdbarch)
174 {
175   struct type *vtable_type = gdbarch_data (gdbarch, vtable_type_gdbarch_data);
176
177   /* The "offset_to_top" field has the appropriate (ptrdiff_t) type.  */
178   return TYPE_FIELD_TYPE (vtable_type, vtable_field_offset_to_top);
179 }
180
181 /* Return the offset from the start of the imaginary `struct
182    gdb_gnu_v3_abi_vtable' object to the vtable's "address point"
183    (i.e., where objects' virtual table pointers point).  */
184 static int
185 vtable_address_point_offset (struct gdbarch *gdbarch)
186 {
187   struct type *vtable_type = gdbarch_data (gdbarch, vtable_type_gdbarch_data);
188
189   return (TYPE_FIELD_BITPOS (vtable_type, vtable_field_virtual_functions)
190           / TARGET_CHAR_BIT);
191 }
192
193
194 /* Determine whether structure TYPE is a dynamic class.  Cache the
195    result.  */
196
197 static int
198 gnuv3_dynamic_class (struct type *type)
199 {
200   int fieldnum, fieldelem;
201
202   if (TYPE_CPLUS_DYNAMIC (type))
203     return TYPE_CPLUS_DYNAMIC (type) == 1;
204
205   ALLOCATE_CPLUS_STRUCT_TYPE (type);
206
207   for (fieldnum = 0; fieldnum < TYPE_N_BASECLASSES (type); fieldnum++)
208     if (BASETYPE_VIA_VIRTUAL (type, fieldnum)
209         || gnuv3_dynamic_class (TYPE_FIELD_TYPE (type, fieldnum)))
210       {
211         TYPE_CPLUS_DYNAMIC (type) = 1;
212         return 1;
213       }
214
215   for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
216     for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
217          fieldelem++)
218       {
219         struct fn_field *f = TYPE_FN_FIELDLIST1 (type, fieldnum);
220
221         if (TYPE_FN_FIELD_VIRTUAL_P (f, fieldelem))
222           {
223             TYPE_CPLUS_DYNAMIC (type) = 1;
224             return 1;
225           }
226       }
227
228   TYPE_CPLUS_DYNAMIC (type) = -1;
229   return 0;
230 }
231
232 /* Find the vtable for a value of CONTAINER_TYPE located at
233    CONTAINER_ADDR.  Return a value of the correct vtable type for this
234    architecture, or NULL if CONTAINER does not have a vtable.  */
235
236 static struct value *
237 gnuv3_get_vtable (struct gdbarch *gdbarch,
238                   struct type *container_type, CORE_ADDR container_addr)
239 {
240   struct type *vtable_type = gdbarch_data (gdbarch,
241                                            vtable_type_gdbarch_data);
242   struct type *vtable_pointer_type;
243   struct value *vtable_pointer;
244   CORE_ADDR vtable_address;
245
246   /* If this type does not have a virtual table, don't read the first
247      field.  */
248   if (!gnuv3_dynamic_class (check_typedef (container_type)))
249     return NULL;
250
251   /* We do not consult the debug information to find the virtual table.
252      The ABI specifies that it is always at offset zero in any class,
253      and debug information may not represent it.
254
255      We avoid using value_contents on principle, because the object might
256      be large.  */
257
258   /* Find the type "pointer to virtual table".  */
259   vtable_pointer_type = lookup_pointer_type (vtable_type);
260
261   /* Load it from the start of the class.  */
262   vtable_pointer = value_at (vtable_pointer_type, container_addr);
263   vtable_address = value_as_address (vtable_pointer);
264
265   /* Correct it to point at the start of the virtual table, rather
266      than the address point.  */
267   return value_at_lazy (vtable_type,
268                         vtable_address
269                         - vtable_address_point_offset (gdbarch));
270 }
271
272
273 static struct type *
274 gnuv3_rtti_type (struct value *value,
275                  int *full_p, int *top_p, int *using_enc_p)
276 {
277   struct gdbarch *gdbarch;
278   struct type *values_type = check_typedef (value_type (value));
279   struct value *vtable;
280   struct minimal_symbol *vtable_symbol;
281   const char *vtable_symbol_name;
282   const char *class_name;
283   struct type *run_time_type;
284   LONGEST offset_to_top;
285
286   /* We only have RTTI for class objects.  */
287   if (TYPE_CODE (values_type) != TYPE_CODE_CLASS)
288     return NULL;
289
290   /* Determine architecture.  */
291   gdbarch = get_type_arch (values_type);
292
293   if (using_enc_p)
294     *using_enc_p = 0;
295
296   vtable = gnuv3_get_vtable (gdbarch, value_type (value),
297                              value_as_address (value_addr (value)));
298   if (vtable == NULL)
299     return NULL;
300
301   /* Find the linker symbol for this vtable.  */
302   vtable_symbol
303     = lookup_minimal_symbol_by_pc (value_address (vtable)
304                                    + value_embedded_offset (vtable));
305   if (! vtable_symbol)
306     return NULL;
307   
308   /* The symbol's demangled name should be something like "vtable for
309      CLASS", where CLASS is the name of the run-time type of VALUE.
310      If we didn't like this approach, we could instead look in the
311      type_info object itself to get the class name.  But this way
312      should work just as well, and doesn't read target memory.  */
313   vtable_symbol_name = SYMBOL_DEMANGLED_NAME (vtable_symbol);
314   if (vtable_symbol_name == NULL
315       || strncmp (vtable_symbol_name, "vtable for ", 11))
316     {
317       warning (_("can't find linker symbol for virtual table for `%s' value"),
318                TYPE_NAME (values_type));
319       if (vtable_symbol_name)
320         warning (_("  found `%s' instead"), vtable_symbol_name);
321       return NULL;
322     }
323   class_name = vtable_symbol_name + 11;
324
325   /* Try to look up the class name as a type name.  */
326   /* FIXME: chastain/2003-11-26: block=NULL is bogus.  See pr gdb/1465.  */
327   run_time_type = cp_lookup_rtti_type (class_name, NULL);
328   if (run_time_type == NULL)
329     return NULL;
330
331   /* Get the offset from VALUE to the top of the complete object.
332      NOTE: this is the reverse of the meaning of *TOP_P.  */
333   offset_to_top
334     = value_as_long (value_field (vtable, vtable_field_offset_to_top));
335
336   if (full_p)
337     *full_p = (- offset_to_top == value_embedded_offset (value)
338                && (TYPE_LENGTH (value_enclosing_type (value))
339                    >= TYPE_LENGTH (run_time_type)));
340   if (top_p)
341     *top_p = - offset_to_top;
342   return run_time_type;
343 }
344
345 /* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual
346    function, of type FNTYPE.  */
347
348 static struct value *
349 gnuv3_get_virtual_fn (struct gdbarch *gdbarch, struct value *container,
350                       struct type *fntype, int vtable_index)
351 {
352   struct value *vtable, *vfn;
353
354   /* Every class with virtual functions must have a vtable.  */
355   vtable = gnuv3_get_vtable (gdbarch, value_type (container),
356                              value_as_address (value_addr (container)));
357   gdb_assert (vtable != NULL);
358
359   /* Fetch the appropriate function pointer from the vtable.  */
360   vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
361                          vtable_index);
362
363   /* If this architecture uses function descriptors directly in the vtable,
364      then the address of the vtable entry is actually a "function pointer"
365      (i.e. points to the descriptor).  We don't need to scale the index
366      by the size of a function descriptor; GCC does that before outputing
367      debug information.  */
368   if (gdbarch_vtable_function_descriptors (gdbarch))
369     vfn = value_addr (vfn);
370
371   /* Cast the function pointer to the appropriate type.  */
372   vfn = value_cast (lookup_pointer_type (fntype), vfn);
373
374   return vfn;
375 }
376
377 /* GNU v3 implementation of value_virtual_fn_field.  See cp-abi.h
378    for a description of the arguments.  */
379
380 static struct value *
381 gnuv3_virtual_fn_field (struct value **value_p,
382                         struct fn_field *f, int j,
383                         struct type *vfn_base, int offset)
384 {
385   struct type *values_type = check_typedef (value_type (*value_p));
386   struct gdbarch *gdbarch;
387
388   /* Some simple sanity checks.  */
389   if (TYPE_CODE (values_type) != TYPE_CODE_CLASS)
390     error (_("Only classes can have virtual functions."));
391
392   /* Determine architecture.  */
393   gdbarch = get_type_arch (values_type);
394
395   /* Cast our value to the base class which defines this virtual
396      function.  This takes care of any necessary `this'
397      adjustments.  */
398   if (vfn_base != values_type)
399     *value_p = value_cast (vfn_base, *value_p);
400
401   return gnuv3_get_virtual_fn (gdbarch, *value_p, TYPE_FN_FIELD_TYPE (f, j),
402                                TYPE_FN_FIELD_VOFFSET (f, j));
403 }
404
405 /* Compute the offset of the baseclass which is
406    the INDEXth baseclass of class TYPE,
407    for value at VALADDR (in host) at ADDRESS (in target).
408    The result is the offset of the baseclass value relative
409    to (the address of)(ARG) + OFFSET.
410
411    -1 is returned on error.  */
412
413 static int
414 gnuv3_baseclass_offset (struct type *type, int index,
415                         const bfd_byte *valaddr, int embedded_offset,
416                         CORE_ADDR address, const struct value *val)
417 {
418   struct gdbarch *gdbarch;
419   struct type *ptr_type;
420   struct value *vtable;
421   struct value *vbase_array;
422   long int cur_base_offset, base_offset;
423
424   /* Determine architecture.  */
425   gdbarch = get_type_arch (type);
426   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
427
428   /* If it isn't a virtual base, this is easy.  The offset is in the
429      type definition.  */
430   if (!BASETYPE_VIA_VIRTUAL (type, index))
431     return TYPE_BASECLASS_BITPOS (type, index) / 8;
432
433   /* To access a virtual base, we need to use the vbase offset stored in
434      our vtable.  Recent GCC versions provide this information.  If it isn't
435      available, we could get what we needed from RTTI, or from drawing the
436      complete inheritance graph based on the debug info.  Neither is
437      worthwhile.  */
438   cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8;
439   if (cur_base_offset >= - vtable_address_point_offset (gdbarch))
440     error (_("Expected a negative vbase offset (old compiler?)"));
441
442   cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch);
443   if ((- cur_base_offset) % TYPE_LENGTH (ptr_type) != 0)
444     error (_("Misaligned vbase offset."));
445   cur_base_offset = cur_base_offset / ((int) TYPE_LENGTH (ptr_type));
446
447   vtable = gnuv3_get_vtable (gdbarch, type, address + embedded_offset);
448   gdb_assert (vtable != NULL);
449   vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets);
450   base_offset = value_as_long (value_subscript (vbase_array, cur_base_offset));
451   return base_offset;
452 }
453
454 /* Locate a virtual method in DOMAIN or its non-virtual base classes
455    which has virtual table index VOFFSET.  The method has an associated
456    "this" adjustment of ADJUSTMENT bytes.  */
457
458 static const char *
459 gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset,
460                       LONGEST adjustment)
461 {
462   int i;
463
464   /* Search this class first.  */
465   if (adjustment == 0)
466     {
467       int len;
468
469       len = TYPE_NFN_FIELDS (domain);
470       for (i = 0; i < len; i++)
471         {
472           int len2, j;
473           struct fn_field *f;
474
475           f = TYPE_FN_FIELDLIST1 (domain, i);
476           len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
477
478           check_stub_method_group (domain, i);
479           for (j = 0; j < len2; j++)
480             if (TYPE_FN_FIELD_VOFFSET (f, j) == voffset)
481               return TYPE_FN_FIELD_PHYSNAME (f, j);
482         }
483     }
484
485   /* Next search non-virtual bases.  If it's in a virtual base,
486      we're out of luck.  */
487   for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
488     {
489       int pos;
490       struct type *basetype;
491
492       if (BASETYPE_VIA_VIRTUAL (domain, i))
493         continue;
494
495       pos = TYPE_BASECLASS_BITPOS (domain, i) / 8;
496       basetype = TYPE_FIELD_TYPE (domain, i);
497       /* Recurse with a modified adjustment.  We don't need to adjust
498          voffset.  */
499       if (adjustment >= pos && adjustment < pos + TYPE_LENGTH (basetype))
500         return gnuv3_find_method_in (basetype, voffset, adjustment - pos);
501     }
502
503   return NULL;
504 }
505
506 /* Decode GNU v3 method pointer.  */
507
508 static int
509 gnuv3_decode_method_ptr (struct gdbarch *gdbarch,
510                          const gdb_byte *contents,
511                          CORE_ADDR *value_p,
512                          LONGEST *adjustment_p)
513 {
514   struct type *funcptr_type = builtin_type (gdbarch)->builtin_func_ptr;
515   struct type *offset_type = vtable_ptrdiff_type (gdbarch);
516   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
517   CORE_ADDR ptr_value;
518   LONGEST voffset, adjustment;
519   int vbit;
520
521   /* Extract the pointer to member.  The first element is either a pointer
522      or a vtable offset.  For pointers, we need to use extract_typed_address
523      to allow the back-end to convert the pointer to a GDB address -- but
524      vtable offsets we must handle as integers.  At this point, we do not
525      yet know which case we have, so we extract the value under both
526      interpretations and choose the right one later on.  */
527   ptr_value = extract_typed_address (contents, funcptr_type);
528   voffset = extract_signed_integer (contents,
529                                     TYPE_LENGTH (funcptr_type), byte_order);
530   contents += TYPE_LENGTH (funcptr_type);
531   adjustment = extract_signed_integer (contents,
532                                        TYPE_LENGTH (offset_type), byte_order);
533
534   if (!gdbarch_vbit_in_delta (gdbarch))
535     {
536       vbit = voffset & 1;
537       voffset = voffset ^ vbit;
538     }
539   else
540     {
541       vbit = adjustment & 1;
542       adjustment = adjustment >> 1;
543     }
544
545   *value_p = vbit? voffset : ptr_value;
546   *adjustment_p = adjustment;
547   return vbit;
548 }
549
550 /* GNU v3 implementation of cplus_print_method_ptr.  */
551
552 static void
553 gnuv3_print_method_ptr (const gdb_byte *contents,
554                         struct type *type,
555                         struct ui_file *stream)
556 {
557   struct type *domain = TYPE_DOMAIN_TYPE (type);
558   struct gdbarch *gdbarch = get_type_arch (domain);
559   CORE_ADDR ptr_value;
560   LONGEST adjustment;
561   int vbit;
562
563   /* Extract the pointer to member.  */
564   vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
565
566   /* Check for NULL.  */
567   if (ptr_value == 0 && vbit == 0)
568     {
569       fprintf_filtered (stream, "NULL");
570       return;
571     }
572
573   /* Search for a virtual method.  */
574   if (vbit)
575     {
576       CORE_ADDR voffset;
577       const char *physname;
578
579       /* It's a virtual table offset, maybe in this class.  Search
580          for a field with the correct vtable offset.  First convert it
581          to an index, as used in TYPE_FN_FIELD_VOFFSET.  */
582       voffset = ptr_value / TYPE_LENGTH (vtable_ptrdiff_type (gdbarch));
583
584       physname = gnuv3_find_method_in (domain, voffset, adjustment);
585
586       /* If we found a method, print that.  We don't bother to disambiguate
587          possible paths to the method based on the adjustment.  */
588       if (physname)
589         {
590           char *demangled_name = cplus_demangle (physname,
591                                                  DMGL_ANSI | DMGL_PARAMS);
592
593           fprintf_filtered (stream, "&virtual ");
594           if (demangled_name == NULL)
595             fputs_filtered (physname, stream);
596           else
597             {
598               fputs_filtered (demangled_name, stream);
599               xfree (demangled_name);
600             }
601           return;
602         }
603     }
604   else if (ptr_value != 0)
605     {
606       /* Found a non-virtual function: print out the type.  */
607       fputs_filtered ("(", stream);
608       c_print_type (type, "", stream, -1, 0);
609       fputs_filtered (") ", stream);
610     }
611
612   /* We didn't find it; print the raw data.  */
613   if (vbit)
614     {
615       fprintf_filtered (stream, "&virtual table offset ");
616       print_longest (stream, 'd', 1, ptr_value);
617     }
618   else
619     print_address_demangle (gdbarch, ptr_value, stream, demangle);
620
621   if (adjustment)
622     {
623       fprintf_filtered (stream, ", this adjustment ");
624       print_longest (stream, 'd', 1, adjustment);
625     }
626 }
627
628 /* GNU v3 implementation of cplus_method_ptr_size.  */
629
630 static int
631 gnuv3_method_ptr_size (struct type *type)
632 {
633   struct gdbarch *gdbarch = get_type_arch (type);
634
635   return 2 * TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
636 }
637
638 /* GNU v3 implementation of cplus_make_method_ptr.  */
639
640 static void
641 gnuv3_make_method_ptr (struct type *type, gdb_byte *contents,
642                        CORE_ADDR value, int is_virtual)
643 {
644   struct gdbarch *gdbarch = get_type_arch (type);
645   int size = TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
646   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
647
648   /* FIXME drow/2006-12-24: The adjustment of "this" is currently
649      always zero, since the method pointer is of the correct type.
650      But if the method pointer came from a base class, this is
651      incorrect - it should be the offset to the base.  The best
652      fix might be to create the pointer to member pointing at the
653      base class and cast it to the derived class, but that requires
654      support for adjusting pointers to members when casting them -
655      not currently supported by GDB.  */
656
657   if (!gdbarch_vbit_in_delta (gdbarch))
658     {
659       store_unsigned_integer (contents, size, byte_order, value | is_virtual);
660       store_unsigned_integer (contents + size, size, byte_order, 0);
661     }
662   else
663     {
664       store_unsigned_integer (contents, size, byte_order, value);
665       store_unsigned_integer (contents + size, size, byte_order, is_virtual);
666     }
667 }
668
669 /* GNU v3 implementation of cplus_method_ptr_to_value.  */
670
671 static struct value *
672 gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
673 {
674   struct gdbarch *gdbarch;
675   const gdb_byte *contents = value_contents (method_ptr);
676   CORE_ADDR ptr_value;
677   struct type *domain_type, *final_type, *method_type;
678   LONGEST adjustment;
679   int vbit;
680
681   domain_type = TYPE_DOMAIN_TYPE (check_typedef (value_type (method_ptr)));
682   final_type = lookup_pointer_type (domain_type);
683
684   method_type = TYPE_TARGET_TYPE (check_typedef (value_type (method_ptr)));
685
686   /* Extract the pointer to member.  */
687   gdbarch = get_type_arch (domain_type);
688   vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
689
690   /* First convert THIS to match the containing type of the pointer to
691      member.  This cast may adjust the value of THIS.  */
692   *this_p = value_cast (final_type, *this_p);
693
694   /* Then apply whatever adjustment is necessary.  This creates a somewhat
695      strange pointer: it claims to have type FINAL_TYPE, but in fact it
696      might not be a valid FINAL_TYPE.  For instance, it might be a
697      base class of FINAL_TYPE.  And if it's not the primary base class,
698      then printing it out as a FINAL_TYPE object would produce some pretty
699      garbage.
700
701      But we don't really know the type of the first argument in
702      METHOD_TYPE either, which is why this happens.  We can't
703      dereference this later as a FINAL_TYPE, but once we arrive in the
704      called method we'll have debugging information for the type of
705      "this" - and that'll match the value we produce here.
706
707      You can provoke this case by casting a Base::* to a Derived::*, for
708      instance.  */
709   *this_p = value_cast (builtin_type (gdbarch)->builtin_data_ptr, *this_p);
710   *this_p = value_ptradd (*this_p, adjustment);
711   *this_p = value_cast (final_type, *this_p);
712
713   if (vbit)
714     {
715       LONGEST voffset;
716
717       voffset = ptr_value / TYPE_LENGTH (vtable_ptrdiff_type (gdbarch));
718       return gnuv3_get_virtual_fn (gdbarch, value_ind (*this_p),
719                                    method_type, voffset);
720     }
721   else
722     return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
723 }
724
725 /* Determine if we are currently in a C++ thunk.  If so, get the address
726    of the routine we are thunking to and continue to there instead.  */
727
728 static CORE_ADDR 
729 gnuv3_skip_trampoline (struct frame_info *frame, CORE_ADDR stop_pc)
730 {
731   CORE_ADDR real_stop_pc, method_stop_pc;
732   struct gdbarch *gdbarch = get_frame_arch (frame);
733   struct minimal_symbol *thunk_sym, *fn_sym;
734   struct obj_section *section;
735   char *thunk_name, *fn_name;
736   
737   real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
738   if (real_stop_pc == 0)
739     real_stop_pc = stop_pc;
740
741   /* Find the linker symbol for this potential thunk.  */
742   thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc);
743   section = find_pc_section (real_stop_pc);
744   if (thunk_sym == NULL || section == NULL)
745     return 0;
746
747   /* The symbol's demangled name should be something like "virtual
748      thunk to FUNCTION", where FUNCTION is the name of the function
749      being thunked to.  */
750   thunk_name = SYMBOL_DEMANGLED_NAME (thunk_sym);
751   if (thunk_name == NULL || strstr (thunk_name, " thunk to ") == NULL)
752     return 0;
753
754   fn_name = strstr (thunk_name, " thunk to ") + strlen (" thunk to ");
755   fn_sym = lookup_minimal_symbol (fn_name, NULL, section->objfile);
756   if (fn_sym == NULL)
757     return 0;
758
759   method_stop_pc = SYMBOL_VALUE_ADDRESS (fn_sym);
760   real_stop_pc = gdbarch_skip_trampoline_code
761                    (gdbarch, frame, method_stop_pc);
762   if (real_stop_pc == 0)
763     real_stop_pc = method_stop_pc;
764
765   return real_stop_pc;
766 }
767
768 /* Return nonzero if a type should be passed by reference.
769
770    The rule in the v3 ABI document comes from section 3.1.1.  If the
771    type has a non-trivial copy constructor or destructor, then the
772    caller must make a copy (by calling the copy constructor if there
773    is one or perform the copy itself otherwise), pass the address of
774    the copy, and then destroy the temporary (if necessary).
775
776    For return values with non-trivial copy constructors or
777    destructors, space will be allocated in the caller, and a pointer
778    will be passed as the first argument (preceding "this").
779
780    We don't have a bulletproof mechanism for determining whether a
781    constructor or destructor is trivial.  For GCC and DWARF2 debug
782    information, we can check the artificial flag.
783
784    We don't do anything with the constructors or destructors,
785    but we have to get the argument passing right anyway.  */
786 static int
787 gnuv3_pass_by_reference (struct type *type)
788 {
789   int fieldnum, fieldelem;
790
791   CHECK_TYPEDEF (type);
792
793   /* We're only interested in things that can have methods.  */
794   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
795       && TYPE_CODE (type) != TYPE_CODE_CLASS
796       && TYPE_CODE (type) != TYPE_CODE_UNION)
797     return 0;
798
799   for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
800     for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
801          fieldelem++)
802       {
803         struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum);
804         char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
805         struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
806
807         /* If this function is marked as artificial, it is compiler-generated,
808            and we assume it is trivial.  */
809         if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem))
810           continue;
811
812         /* If we've found a destructor, we must pass this by reference.  */
813         if (name[0] == '~')
814           return 1;
815
816         /* If the mangled name of this method doesn't indicate that it
817            is a constructor, we're not interested.
818
819            FIXME drow/2007-09-23: We could do this using the name of
820            the method and the name of the class instead of dealing
821            with the mangled name.  We don't have a convenient function
822            to strip off both leading scope qualifiers and trailing
823            template arguments yet.  */
824         if (!is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem)))
825           continue;
826
827         /* If this method takes two arguments, and the second argument is
828            a reference to this class, then it is a copy constructor.  */
829         if (TYPE_NFIELDS (fieldtype) == 2
830             && TYPE_CODE (TYPE_FIELD_TYPE (fieldtype, 1)) == TYPE_CODE_REF
831             && check_typedef (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (fieldtype,
832                                                                  1))) == type)
833           return 1;
834       }
835
836   /* Even if all the constructors and destructors were artificial, one
837      of them may have invoked a non-artificial constructor or
838      destructor in a base class.  If any base class needs to be passed
839      by reference, so does this class.  Similarly for members, which
840      are constructed whenever this class is.  We do not need to worry
841      about recursive loops here, since we are only looking at members
842      of complete class type.  Also ignore any static members.  */
843   for (fieldnum = 0; fieldnum < TYPE_NFIELDS (type); fieldnum++)
844     if (! field_is_static (&TYPE_FIELD (type, fieldnum))
845         && gnuv3_pass_by_reference (TYPE_FIELD_TYPE (type, fieldnum)))
846       return 1;
847
848   return 0;
849 }
850
851 static void
852 init_gnuv3_ops (void)
853 {
854   vtable_type_gdbarch_data
855     = gdbarch_data_register_post_init (build_gdb_vtable_type);
856
857   gnu_v3_abi_ops.shortname = "gnu-v3";
858   gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI";
859   gnu_v3_abi_ops.doc = "G++ Version 3 ABI";
860   gnu_v3_abi_ops.is_destructor_name =
861     (enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor;
862   gnu_v3_abi_ops.is_constructor_name =
863     (enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor;
864   gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name;
865   gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name;
866   gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
867   gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
868   gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
869   gnu_v3_abi_ops.print_method_ptr = gnuv3_print_method_ptr;
870   gnu_v3_abi_ops.method_ptr_size = gnuv3_method_ptr_size;
871   gnu_v3_abi_ops.make_method_ptr = gnuv3_make_method_ptr;
872   gnu_v3_abi_ops.method_ptr_to_value = gnuv3_method_ptr_to_value;
873   gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline;
874   gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
875 }
876
877 extern initialize_file_ftype _initialize_gnu_v3_abi; /* -Wmissing-prototypes */
878
879 void
880 _initialize_gnu_v3_abi (void)
881 {
882   init_gnuv3_ops ();
883
884   register_cp_abi (&gnu_v3_abi_ops);
885 }