OSDN Git Service

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