e635e8f1a8a7a3affd4a6bc2c6578835a8471d78
[external/binutils.git] / gdb / gnu-v2-abi.c
1 /* Abstraction of GNU v2 abi.
2
3    Copyright (C) 2001-2003, 2005, 2007-2012 Free Software Foundation,
4    Inc.
5
6    Contributed by Daniel Berlin <dberlin@redhat.com>
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24 #include "gdb_string.h"
25 #include "symtab.h"
26 #include "gdbtypes.h"
27 #include "value.h"
28 #include "demangle.h"
29 #include "gdb-demangle.h"
30 #include "cp-abi.h"
31 #include "cp-support.h"
32 #include "exceptions.h"
33
34 #include <ctype.h>
35
36 struct cp_abi_ops gnu_v2_abi_ops;
37
38 static int vb_match (struct type *, int, struct type *);
39
40 static enum dtor_kinds
41 gnuv2_is_destructor_name (const char *name)
42 {
43   if ((name[0] == '_' && is_cplus_marker (name[1]) && name[2] == '_')
44       || strncmp (name, "__dt__", 6) == 0)
45     return complete_object_dtor;
46   else
47     return 0;
48 }
49
50 static enum ctor_kinds
51 gnuv2_is_constructor_name (const char *name)
52 {
53   if ((name[0] == '_' && name[1] == '_'
54        && (isdigit (name[2]) || strchr ("Qt", name[2])))
55       || strncmp (name, "__ct__", 6) == 0)
56     return complete_object_ctor;
57   else
58     return 0;
59 }
60
61 static int
62 gnuv2_is_vtable_name (const char *name)
63 {
64   return (((name)[0] == '_'
65            && (((name)[1] == 'V' && (name)[2] == 'T')
66                || ((name)[1] == 'v' && (name)[2] == 't'))
67            && is_cplus_marker ((name)[3])) ||
68           ((name)[0] == '_' && (name)[1] == '_'
69            && (name)[2] == 'v' && (name)[3] == 't' && (name)[4] == '_'));
70 }
71
72 static int
73 gnuv2_is_operator_name (const char *name)
74 {
75   return strncmp (name, "operator", 8) == 0;
76 }
77
78 \f
79 /* Return a virtual function as a value.
80    ARG1 is the object which provides the virtual function
81    table pointer.  *ARG1P is side-effected in calling this function.
82    F is the list of member functions which contains the desired virtual
83    function.
84    J is an index into F which provides the desired virtual function.
85
86    TYPE is the type in which F is located.  */
87 static struct value *
88 gnuv2_virtual_fn_field (struct value **arg1p, struct fn_field * f, int j,
89                         struct type * type, int offset)
90 {
91   struct value *arg1 = *arg1p;
92   struct type *type1 = check_typedef (value_type (arg1));
93   struct type *entry_type;
94   /* First, get the virtual function table pointer.  That comes
95      with a strange type, so cast it to type `pointer to long' (which
96      should serve just fine as a function type).  Then, index into
97      the table, and convert final value to appropriate function type.  */
98   struct value *entry;
99   struct value *vfn;
100   struct value *vtbl;
101   LONGEST vi = (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j);
102   struct type *fcontext = TYPE_FN_FIELD_FCONTEXT (f, j);
103   struct type *context;
104   struct type *context_vptr_basetype;
105   int context_vptr_fieldno;
106
107   if (fcontext == NULL)
108     /* We don't have an fcontext (e.g. the program was compiled with
109        g++ version 1).  Try to get the vtbl from the TYPE_VPTR_BASETYPE.
110        This won't work right for multiple inheritance, but at least we
111        should do as well as GDB 3.x did.  */
112     fcontext = TYPE_VPTR_BASETYPE (type);
113   context = lookup_pointer_type (fcontext);
114   /* Now context is a pointer to the basetype containing the vtbl.  */
115   if (TYPE_TARGET_TYPE (context) != type1)
116     {
117       struct value *tmp = value_cast (context, value_addr (arg1));
118
119       arg1 = value_ind (tmp);
120       type1 = check_typedef (value_type (arg1));
121     }
122
123   context = type1;
124   /* Now context is the basetype containing the vtbl.  */
125
126   /* This type may have been defined before its virtual function table
127      was.  If so, fill in the virtual function table entry for the
128      type now.  */
129   context_vptr_fieldno = get_vptr_fieldno (context, &context_vptr_basetype);
130   /* FIXME: What to do if vptr_fieldno is still -1?  */
131
132   /* The virtual function table is now an array of structures
133      which have the form { int16 offset, delta; void *pfn; }.  */
134   vtbl = value_primitive_field (arg1, 0, context_vptr_fieldno,
135                                 context_vptr_basetype);
136
137   /* With older versions of g++, the vtbl field pointed to an array
138      of structures.  Nowadays it points directly to the structure.  */
139   if (TYPE_CODE (value_type (vtbl)) == TYPE_CODE_PTR
140       && TYPE_CODE (TYPE_TARGET_TYPE (value_type (vtbl))) == TYPE_CODE_ARRAY)
141     {
142       /* Handle the case where the vtbl field points to an
143          array of structures.  */
144       vtbl = value_ind (vtbl);
145
146       /* Index into the virtual function table.  This is hard-coded because
147          looking up a field is not cheap, and it may be important to save
148          time, e.g. if the user has set a conditional breakpoint calling
149          a virtual function.  */
150       entry = value_subscript (vtbl, vi);
151     }
152   else
153     {
154       /* Handle the case where the vtbl field points directly to a
155          structure.  */
156       vtbl = value_ptradd (vtbl, vi);
157       entry = value_ind (vtbl);
158     }
159
160   entry_type = check_typedef (value_type (entry));
161
162   if (TYPE_CODE (entry_type) == TYPE_CODE_STRUCT)
163     {
164       /* Move the `this' pointer according to the virtual function table.  */
165       set_value_offset (arg1, value_offset (arg1)
166                         + value_as_long (value_field (entry, 0)));
167
168       if (!value_lazy (arg1))
169         {
170           set_value_lazy (arg1, 1);
171           value_fetch_lazy (arg1);
172         }
173
174       vfn = value_field (entry, 2);
175     }
176   else if (TYPE_CODE (entry_type) == TYPE_CODE_PTR)
177     vfn = entry;
178   else
179     error (_("I'm confused:  virtual function table has bad type"));
180   /* Reinstantiate the function pointer with the correct type.  */
181   deprecated_set_value_type (vfn,
182                              lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j)));
183
184   *arg1p = arg1;
185   return vfn;
186 }
187
188
189 static struct type *
190 gnuv2_value_rtti_type (struct value *v, int *full, int *top, int *using_enc)
191 {
192   struct type *known_type;
193   struct type *rtti_type;
194   CORE_ADDR vtbl;
195   struct minimal_symbol *minsym;
196   char *demangled_name, *p;
197   const char *linkage_name;
198   struct type *btype;
199   struct type *known_type_vptr_basetype;
200   int known_type_vptr_fieldno;
201
202   if (full)
203     *full = 0;
204   if (top)
205     *top = -1;
206   if (using_enc)
207     *using_enc = 0;
208
209   /* Get declared type.  */
210   known_type = value_type (v);
211   CHECK_TYPEDEF (known_type);
212   /* RTTI works only or class objects.  */
213   if (TYPE_CODE (known_type) != TYPE_CODE_CLASS)
214     return NULL;
215
216   /* Plan on this changing in the future as i get around to setting
217      the vtables properly for G++ compiled stuff.  Also, I'll be using
218      the type info functions, which are always right.  Deal with it
219      until then.  */
220
221   /* Try to get the vptr basetype, fieldno.  */
222   known_type_vptr_fieldno = get_vptr_fieldno (known_type,
223                                               &known_type_vptr_basetype);
224
225   /* If we can't find it, give up.  */
226   if (known_type_vptr_fieldno < 0)
227     return NULL;
228
229   /* Make sure our basetype and known type match, otherwise, cast
230      so we can get at the vtable properly.  */
231   btype = known_type_vptr_basetype;
232   CHECK_TYPEDEF (btype);
233   if (btype != known_type )
234     {
235       v = value_cast (btype, v);
236       if (using_enc)
237         *using_enc=1;
238     }
239   /* We can't use value_ind here, because it would want to use RTTI, and
240      we'd waste a bunch of time figuring out we already know the type.
241      Besides, we don't care about the type, just the actual pointer.  */
242   if (value_address (value_field (v, known_type_vptr_fieldno)) == 0)
243     return NULL;
244
245   vtbl = value_as_address (value_field (v, known_type_vptr_fieldno));
246
247   /* Try to find a symbol that is the vtable.  */
248   minsym=lookup_minimal_symbol_by_pc(vtbl);
249   if (minsym==NULL
250       || (linkage_name=SYMBOL_LINKAGE_NAME (minsym))==NULL
251       || !is_vtable_name (linkage_name))
252     return NULL;
253
254   /* If we just skip the prefix, we get screwed by namespaces.  */
255   demangled_name=cplus_demangle(linkage_name,DMGL_PARAMS|DMGL_ANSI);
256   p = strchr (demangled_name, ' ');
257   if (p)
258     *p = '\0';
259
260   /* Lookup the type for the name.  */
261   /* FIXME: chastain/2003-11-26: block=NULL is bogus.  See pr gdb/1465.  */
262   rtti_type = cp_lookup_rtti_type (demangled_name, NULL);
263   if (rtti_type == NULL)
264     return NULL;
265
266   if (TYPE_N_BASECLASSES(rtti_type) > 1 &&  full && (*full) != 1)
267     {
268       if (top)
269         *top = TYPE_BASECLASS_BITPOS (rtti_type,
270                                       TYPE_VPTR_FIELDNO(rtti_type)) / 8;
271       if (top && ((*top) >0))
272         {
273           if (TYPE_LENGTH(rtti_type) > TYPE_LENGTH(known_type))
274             {
275               if (full)
276                 *full=0;
277             }
278           else
279             {
280               if (full)
281                 *full=1;
282             }
283         }
284     }
285   else
286     {
287       if (full)
288         *full=1;
289     }
290
291   return rtti_type;
292 }
293
294 /* Return true if the INDEXth field of TYPE is a virtual baseclass
295    pointer which is for the base class whose type is BASECLASS.  */
296
297 static int
298 vb_match (struct type *type, int index, struct type *basetype)
299 {
300   struct type *fieldtype;
301   const char *name = TYPE_FIELD_NAME (type, index);
302   const char *field_class_name = NULL;
303
304   if (*name != '_')
305     return 0;
306   /* gcc 2.4 uses _vb$.  */
307   if (name[1] == 'v' && name[2] == 'b' && is_cplus_marker (name[3]))
308     field_class_name = name + 4;
309   /* gcc 2.5 will use __vb_.  */
310   if (name[1] == '_' && name[2] == 'v' && name[3] == 'b' && name[4] == '_')
311     field_class_name = name + 5;
312
313   if (field_class_name == NULL)
314     /* This field is not a virtual base class pointer.  */
315     return 0;
316
317   /* It's a virtual baseclass pointer, now we just need to find out whether
318      it is for this baseclass.  */
319   fieldtype = TYPE_FIELD_TYPE (type, index);
320   if (fieldtype == NULL
321       || TYPE_CODE (fieldtype) != TYPE_CODE_PTR)
322     /* "Can't happen".  */
323     return 0;
324
325   /* What we check for is that either the types are equal (needed for
326      nameless types) or have the same name.  This is ugly, and a more
327      elegant solution should be devised (which would probably just push
328      the ugliness into symbol reading unless we change the stabs format).  */
329   if (TYPE_TARGET_TYPE (fieldtype) == basetype)
330     return 1;
331
332   if (TYPE_NAME (basetype) != NULL
333       && TYPE_NAME (TYPE_TARGET_TYPE (fieldtype)) != NULL
334       && strcmp (TYPE_NAME (basetype),
335                  TYPE_NAME (TYPE_TARGET_TYPE (fieldtype))) == 0)
336     return 1;
337   return 0;
338 }
339
340 /* Compute the offset of the baseclass which is the INDEXth baseclass
341    of class TYPE, for value at VALADDR (in host) at ADDRESS (in
342    target).  The result is the offset of the baseclass value relative
343    to (the address of)(ARG) + OFFSET.  */
344
345 static int
346 gnuv2_baseclass_offset (struct type *type, int index,
347                         const bfd_byte *valaddr, int embedded_offset,
348                         CORE_ADDR address, const struct value *val)
349 {
350   struct type *basetype = TYPE_BASECLASS (type, index);
351
352   if (BASETYPE_VIA_VIRTUAL (type, index))
353     {
354       /* Must hunt for the pointer to this virtual baseclass.  */
355       int i, len = TYPE_NFIELDS (type);
356       int n_baseclasses = TYPE_N_BASECLASSES (type);
357
358       /* First look for the virtual baseclass pointer
359          in the fields.  */
360       for (i = n_baseclasses; i < len; i++)
361         {
362           if (vb_match (type, i, basetype))
363             {
364               struct type *field_type;
365               int field_offset;
366               int field_length;
367               CORE_ADDR addr;
368
369               field_type = check_typedef (TYPE_FIELD_TYPE (type, i));
370               field_offset = TYPE_FIELD_BITPOS (type, i) / 8;
371               field_length = TYPE_LENGTH (field_type);
372
373               if (!value_bytes_available (val, embedded_offset + field_offset,
374                                           field_length))
375                 throw_error (NOT_AVAILABLE_ERROR,
376                              _("Virtual baseclass pointer is not available"));
377
378               addr = unpack_pointer (field_type,
379                                      valaddr + embedded_offset + field_offset);
380
381               return addr - (LONGEST) address + embedded_offset;
382             }
383         }
384       /* Not in the fields, so try looking through the baseclasses.  */
385       for (i = index + 1; i < n_baseclasses; i++)
386         {
387           /* Don't go through baseclass_offset, as that wraps
388              exceptions, thus, inner exceptions would be wrapped more
389              than once.  */
390           int boffset =
391             gnuv2_baseclass_offset (type, i, valaddr,
392                                     embedded_offset, address, val);
393
394           if (boffset)
395             return boffset;
396         }
397
398       error (_("Baseclass offset not found"));
399     }
400
401   /* Baseclass is easily computed.  */
402   return TYPE_BASECLASS_BITPOS (type, index) / 8;
403 }
404
405 static void
406 init_gnuv2_ops (void)
407 {
408   gnu_v2_abi_ops.shortname = "gnu-v2";
409   gnu_v2_abi_ops.longname = "GNU G++ Version 2 ABI";
410   gnu_v2_abi_ops.doc = "G++ Version 2 ABI";
411   gnu_v2_abi_ops.is_destructor_name = gnuv2_is_destructor_name;
412   gnu_v2_abi_ops.is_constructor_name = gnuv2_is_constructor_name;
413   gnu_v2_abi_ops.is_vtable_name = gnuv2_is_vtable_name;
414   gnu_v2_abi_ops.is_operator_name = gnuv2_is_operator_name;
415   gnu_v2_abi_ops.virtual_fn_field = gnuv2_virtual_fn_field;
416   gnu_v2_abi_ops.rtti_type = gnuv2_value_rtti_type;
417   gnu_v2_abi_ops.baseclass_offset = gnuv2_baseclass_offset;
418 }
419
420 extern initialize_file_ftype _initialize_gnu_v2_abi; /* -Wmissing-prototypes */
421
422 void
423 _initialize_gnu_v2_abi (void)
424 {
425   init_gnuv2_ops ();
426   register_cp_abi (&gnu_v2_abi_ops);
427   set_cp_abi_as_auto_default (gnu_v2_abi_ops.shortname);
428 }