OSDN Git Service

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