1*b725ae77Skettenis /* Abstraction of HP aCC ABI.
2*b725ae77Skettenis Contributed by Daniel Berlin <dberlin@redhat.com>
3*b725ae77Skettenis Most of the real code is from HP, i've just fiddled it to fit in
4*b725ae77Skettenis the C++ ABI abstraction framework.
5*b725ae77Skettenis
6*b725ae77Skettenis Copyright 2001 Free Software Foundation, Inc.
7*b725ae77Skettenis
8*b725ae77Skettenis This file is part of GDB.
9*b725ae77Skettenis
10*b725ae77Skettenis This program is free software; you can redistribute it and/or
11*b725ae77Skettenis modify
12*b725ae77Skettenis it under the terms of the GNU General Public License as published
13*b725ae77Skettenis by
14*b725ae77Skettenis the Free Software Foundation; either version 2 of the License, or
15*b725ae77Skettenis (at your option) any later version.
16*b725ae77Skettenis
17*b725ae77Skettenis This program is distributed in the hope that it will be useful,
18*b725ae77Skettenis but WITHOUT ANY WARRANTY; without even the implied warranty of
19*b725ae77Skettenis MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20*b725ae77Skettenis GNU General Public License for more details.
21*b725ae77Skettenis
22*b725ae77Skettenis You should have received a copy of the GNU General Public License
23*b725ae77Skettenis along with this program; if not, write to the Free Software
24*b725ae77Skettenis Foundation, Inc., 59 Temple Place - Suite 330,
25*b725ae77Skettenis Boston, MA 02111-1307, USA. */
26*b725ae77Skettenis
27*b725ae77Skettenis #include "defs.h"
28*b725ae77Skettenis #include "value.h"
29*b725ae77Skettenis #include "gdb_regex.h"
30*b725ae77Skettenis #include "gdb_string.h"
31*b725ae77Skettenis #include "gdbtypes.h"
32*b725ae77Skettenis #include "gdbcore.h"
33*b725ae77Skettenis #include "cp-abi.h"
34*b725ae77Skettenis
35*b725ae77Skettenis struct cp_abi_ops hpacc_abi_ops;
36*b725ae77Skettenis
37*b725ae77Skettenis /* It appears the is_*_name stuff is never used when we try the hpACC
38*b725ae77Skettenis * ABI. As such, I have no clue what the real answers are. Shouldn't
39*b725ae77Skettenis * have any more effect than it does now. */
40*b725ae77Skettenis static regex_t constructor_pattern;
41*b725ae77Skettenis static regex_t destructor_pattern;
42*b725ae77Skettenis static regex_t operator_pattern;
43*b725ae77Skettenis
44*b725ae77Skettenis static enum dtor_kinds
hpacc_is_destructor_name(const char * name)45*b725ae77Skettenis hpacc_is_destructor_name (const char *name)
46*b725ae77Skettenis {
47*b725ae77Skettenis if (regexec (&destructor_pattern, name, 0, 0, 0) == 0)
48*b725ae77Skettenis return complete_object_dtor;
49*b725ae77Skettenis else
50*b725ae77Skettenis return 0;
51*b725ae77Skettenis }
52*b725ae77Skettenis
53*b725ae77Skettenis static enum ctor_kinds
hpacc_is_constructor_name(const char * name)54*b725ae77Skettenis hpacc_is_constructor_name (const char *name)
55*b725ae77Skettenis {
56*b725ae77Skettenis if (regexec (&constructor_pattern, name, 0, 0, 0) == 0)
57*b725ae77Skettenis return complete_object_ctor;
58*b725ae77Skettenis else
59*b725ae77Skettenis return 0;
60*b725ae77Skettenis }
61*b725ae77Skettenis
62*b725ae77Skettenis static int
hpacc_is_operator_name(const char * name)63*b725ae77Skettenis hpacc_is_operator_name (const char *name)
64*b725ae77Skettenis {
65*b725ae77Skettenis return regexec (&operator_pattern, name, 0, 0, 0) == 0;
66*b725ae77Skettenis }
67*b725ae77Skettenis
68*b725ae77Skettenis static int
hpacc_is_vtable_name(const char * name)69*b725ae77Skettenis hpacc_is_vtable_name (const char *name)
70*b725ae77Skettenis {
71*b725ae77Skettenis return strcmp (name,
72*b725ae77Skettenis "This will never match anything, please fill it in") == 0;
73*b725ae77Skettenis }
74*b725ae77Skettenis
75*b725ae77Skettenis /* Return a virtual function as a value.
76*b725ae77Skettenis ARG1 is the object which provides the virtual function
77*b725ae77Skettenis table pointer. *ARG1P is side-effected in calling this function.
78*b725ae77Skettenis F is the list of member functions which contains the desired virtual
79*b725ae77Skettenis function.
80*b725ae77Skettenis J is an index into F which provides the desired virtual function.
81*b725ae77Skettenis
82*b725ae77Skettenis TYPE is the type in which F is located. */
83*b725ae77Skettenis static struct value *
hpacc_virtual_fn_field(struct value ** arg1p,struct fn_field * f,int j,struct type * type,int offset)84*b725ae77Skettenis hpacc_virtual_fn_field (struct value **arg1p, struct fn_field * f, int j,
85*b725ae77Skettenis struct type * type, int offset)
86*b725ae77Skettenis {
87*b725ae77Skettenis struct value *arg1 = *arg1p;
88*b725ae77Skettenis struct type *type1 = check_typedef (VALUE_TYPE (arg1));
89*b725ae77Skettenis
90*b725ae77Skettenis /* Deal with HP/Taligent runtime model for virtual functions */
91*b725ae77Skettenis struct value *vp;
92*b725ae77Skettenis struct value *argp; /* arg1 cast to base */
93*b725ae77Skettenis CORE_ADDR coreptr; /* pointer to target address */
94*b725ae77Skettenis int class_index; /* which class segment pointer to use */
95*b725ae77Skettenis struct type *ftype = TYPE_FN_FIELD_TYPE (f, j); /* method type */
96*b725ae77Skettenis
97*b725ae77Skettenis argp = value_cast (type, *arg1p);
98*b725ae77Skettenis
99*b725ae77Skettenis if (VALUE_ADDRESS (argp) == 0)
100*b725ae77Skettenis error ("Address of object is null; object may not have been created.");
101*b725ae77Skettenis
102*b725ae77Skettenis /* pai: FIXME -- 32x64 possible problem? */
103*b725ae77Skettenis /* First word (4 bytes) in object layout is the vtable pointer */
104*b725ae77Skettenis coreptr = *(CORE_ADDR *) (VALUE_CONTENTS (argp)); /* pai: (temp) */
105*b725ae77Skettenis /* + offset + VALUE_EMBEDDED_OFFSET (argp)); */
106*b725ae77Skettenis
107*b725ae77Skettenis if (!coreptr)
108*b725ae77Skettenis error
109*b725ae77Skettenis ("Virtual table pointer is null for object; object may not have been created.");
110*b725ae77Skettenis
111*b725ae77Skettenis /* pai/1997-05-09
112*b725ae77Skettenis * FIXME: The code here currently handles only
113*b725ae77Skettenis * the non-RRBC case of the Taligent/HP runtime spec; when RRBC
114*b725ae77Skettenis * is introduced, the condition for the "if" below will have to
115*b725ae77Skettenis * be changed to be a test for the RRBC case. */
116*b725ae77Skettenis
117*b725ae77Skettenis if (1)
118*b725ae77Skettenis {
119*b725ae77Skettenis /* Non-RRBC case; the virtual function pointers are stored at fixed
120*b725ae77Skettenis * offsets in the virtual table. */
121*b725ae77Skettenis
122*b725ae77Skettenis /* Retrieve the offset in the virtual table from the debug
123*b725ae77Skettenis * info. The offset of the vfunc's entry is in words from
124*b725ae77Skettenis * the beginning of the vtable; but first we have to adjust
125*b725ae77Skettenis * by HP_ACC_VFUNC_START to account for other entries */
126*b725ae77Skettenis
127*b725ae77Skettenis /* pai: FIXME: 32x64 problem here, a word may be 8 bytes in
128*b725ae77Skettenis * which case the multiplier should be 8 and values should be long */
129*b725ae77Skettenis vp = value_at (builtin_type_int,
130*b725ae77Skettenis coreptr + 4 * (TYPE_FN_FIELD_VOFFSET (f, j) +
131*b725ae77Skettenis HP_ACC_VFUNC_START), NULL);
132*b725ae77Skettenis
133*b725ae77Skettenis coreptr = *(CORE_ADDR *) (VALUE_CONTENTS (vp));
134*b725ae77Skettenis /* coreptr now contains the address of the virtual function */
135*b725ae77Skettenis /* (Actually, it contains the pointer to the plabel for the function. */
136*b725ae77Skettenis }
137*b725ae77Skettenis else
138*b725ae77Skettenis {
139*b725ae77Skettenis /* RRBC case; the virtual function pointers are found by double
140*b725ae77Skettenis * indirection through the class segment tables. */
141*b725ae77Skettenis
142*b725ae77Skettenis /* Choose class segment depending on type we were passed */
143*b725ae77Skettenis class_index = class_index_in_primary_list (type);
144*b725ae77Skettenis
145*b725ae77Skettenis /* Find class segment pointer. These are in the vtable slots after
146*b725ae77Skettenis * some other entries, so adjust by HP_ACC_VFUNC_START for that. */
147*b725ae77Skettenis /* pai: FIXME 32x64 problem here, if words are 8 bytes long
148*b725ae77Skettenis * the multiplier below has to be 8 and value should be long. */
149*b725ae77Skettenis vp = value_at (builtin_type_int,
150*b725ae77Skettenis coreptr + 4 * (HP_ACC_VFUNC_START + class_index), NULL);
151*b725ae77Skettenis /* Indirect once more, offset by function index */
152*b725ae77Skettenis /* pai: FIXME 32x64 problem here, again multiplier could be 8 and value long */
153*b725ae77Skettenis coreptr =
154*b725ae77Skettenis *(CORE_ADDR *) (VALUE_CONTENTS (vp) +
155*b725ae77Skettenis 4 * TYPE_FN_FIELD_VOFFSET (f, j));
156*b725ae77Skettenis vp = value_at (builtin_type_int, coreptr, NULL);
157*b725ae77Skettenis coreptr = *(CORE_ADDR *) (VALUE_CONTENTS (vp));
158*b725ae77Skettenis
159*b725ae77Skettenis /* coreptr now contains the address of the virtual function */
160*b725ae77Skettenis /* (Actually, it contains the pointer to the plabel for the function.) */
161*b725ae77Skettenis
162*b725ae77Skettenis }
163*b725ae77Skettenis
164*b725ae77Skettenis if (!coreptr)
165*b725ae77Skettenis error ("Address of virtual function is null; error in virtual table?");
166*b725ae77Skettenis
167*b725ae77Skettenis /* Wrap this addr in a value and return pointer */
168*b725ae77Skettenis vp = allocate_value (ftype);
169*b725ae77Skettenis VALUE_TYPE (vp) = ftype;
170*b725ae77Skettenis VALUE_ADDRESS (vp) = coreptr;
171*b725ae77Skettenis
172*b725ae77Skettenis /* pai: (temp) do we need the value_ind stuff in value_fn_field? */
173*b725ae77Skettenis return vp;
174*b725ae77Skettenis }
175*b725ae77Skettenis
176*b725ae77Skettenis
177*b725ae77Skettenis static struct type *
hpacc_value_rtti_type(struct value * v,int * full,int * top,int * using_enc)178*b725ae77Skettenis hpacc_value_rtti_type (struct value *v, int *full, int *top, int *using_enc)
179*b725ae77Skettenis {
180*b725ae77Skettenis struct type *known_type;
181*b725ae77Skettenis struct type *rtti_type;
182*b725ae77Skettenis CORE_ADDR coreptr;
183*b725ae77Skettenis struct value *vp;
184*b725ae77Skettenis int using_enclosing = 0;
185*b725ae77Skettenis long top_offset = 0;
186*b725ae77Skettenis char rtti_type_name[256];
187*b725ae77Skettenis
188*b725ae77Skettenis if (full)
189*b725ae77Skettenis *full = 0;
190*b725ae77Skettenis if (top)
191*b725ae77Skettenis *top = -1;
192*b725ae77Skettenis if (using_enc)
193*b725ae77Skettenis *using_enc = 0;
194*b725ae77Skettenis
195*b725ae77Skettenis /* Get declared type */
196*b725ae77Skettenis known_type = VALUE_TYPE (v);
197*b725ae77Skettenis CHECK_TYPEDEF (known_type);
198*b725ae77Skettenis /* RTTI works only or class objects */
199*b725ae77Skettenis if (TYPE_CODE (known_type) != TYPE_CODE_CLASS)
200*b725ae77Skettenis return NULL;
201*b725ae77Skettenis
202*b725ae77Skettenis /* If neither the declared type nor the enclosing type of the
203*b725ae77Skettenis * value structure has a HP ANSI C++ style virtual table,
204*b725ae77Skettenis * we can't do anything. */
205*b725ae77Skettenis if (!TYPE_HAS_VTABLE (known_type))
206*b725ae77Skettenis {
207*b725ae77Skettenis known_type = VALUE_ENCLOSING_TYPE (v);
208*b725ae77Skettenis CHECK_TYPEDEF (known_type);
209*b725ae77Skettenis if ((TYPE_CODE (known_type) != TYPE_CODE_CLASS) ||
210*b725ae77Skettenis !TYPE_HAS_VTABLE (known_type))
211*b725ae77Skettenis return NULL; /* No RTTI, or not HP-compiled types */
212*b725ae77Skettenis CHECK_TYPEDEF (known_type);
213*b725ae77Skettenis using_enclosing = 1;
214*b725ae77Skettenis }
215*b725ae77Skettenis
216*b725ae77Skettenis if (using_enclosing && using_enc)
217*b725ae77Skettenis *using_enc = 1;
218*b725ae77Skettenis
219*b725ae77Skettenis /* First get the virtual table address */
220*b725ae77Skettenis coreptr = *(CORE_ADDR *) ((VALUE_CONTENTS_ALL (v))
221*b725ae77Skettenis + VALUE_OFFSET (v)
222*b725ae77Skettenis + (using_enclosing
223*b725ae77Skettenis ? 0
224*b725ae77Skettenis : VALUE_EMBEDDED_OFFSET (v)));
225*b725ae77Skettenis if (coreptr == 0)
226*b725ae77Skettenis /* return silently -- maybe called on gdb-generated value */
227*b725ae77Skettenis return NULL;
228*b725ae77Skettenis
229*b725ae77Skettenis /* Fetch the top offset of the object */
230*b725ae77Skettenis /* FIXME possible 32x64 problem with pointer size & arithmetic */
231*b725ae77Skettenis vp = value_at (builtin_type_int,
232*b725ae77Skettenis coreptr + 4 * HP_ACC_TOP_OFFSET_OFFSET,
233*b725ae77Skettenis VALUE_BFD_SECTION (v));
234*b725ae77Skettenis top_offset = value_as_long (vp);
235*b725ae77Skettenis if (top)
236*b725ae77Skettenis *top = top_offset;
237*b725ae77Skettenis
238*b725ae77Skettenis /* Fetch the typeinfo pointer */
239*b725ae77Skettenis /* FIXME possible 32x64 problem with pointer size & arithmetic */
240*b725ae77Skettenis vp = value_at (builtin_type_int, coreptr + 4 * HP_ACC_TYPEINFO_OFFSET,
241*b725ae77Skettenis VALUE_BFD_SECTION (v));
242*b725ae77Skettenis /* Indirect through the typeinfo pointer and retrieve the pointer
243*b725ae77Skettenis * to the string name */
244*b725ae77Skettenis coreptr = *(CORE_ADDR *) (VALUE_CONTENTS (vp));
245*b725ae77Skettenis if (!coreptr)
246*b725ae77Skettenis error ("Retrieved null typeinfo pointer in trying to determine "
247*b725ae77Skettenis "run-time type");
248*b725ae77Skettenis /* 4 -> offset of name field */
249*b725ae77Skettenis vp = value_at (builtin_type_int, coreptr + 4, VALUE_BFD_SECTION (v));
250*b725ae77Skettenis /* FIXME possible 32x64 problem */
251*b725ae77Skettenis
252*b725ae77Skettenis coreptr = *(CORE_ADDR *) (VALUE_CONTENTS (vp));
253*b725ae77Skettenis
254*b725ae77Skettenis read_memory_string (coreptr, rtti_type_name, 256);
255*b725ae77Skettenis
256*b725ae77Skettenis if (strlen (rtti_type_name) == 0)
257*b725ae77Skettenis error ("Retrieved null type name from typeinfo");
258*b725ae77Skettenis
259*b725ae77Skettenis /* search for type */
260*b725ae77Skettenis rtti_type = lookup_typename (rtti_type_name, (struct block *) 0, 1);
261*b725ae77Skettenis
262*b725ae77Skettenis if (!rtti_type)
263*b725ae77Skettenis error ("Could not find run-time type: invalid type name %s in typeinfo??",
264*b725ae77Skettenis rtti_type_name);
265*b725ae77Skettenis CHECK_TYPEDEF (rtti_type);
266*b725ae77Skettenis #if 0
267*b725ae77Skettenis printf ("RTTI type name %s, tag %s, full? %d\n", TYPE_NAME (rtti_type),
268*b725ae77Skettenis TYPE_TAG_NAME (rtti_type), full ? *full : -1);
269*b725ae77Skettenis #endif
270*b725ae77Skettenis /* Check whether we have the entire object */
271*b725ae77Skettenis if (full /* Non-null pointer passed */
272*b725ae77Skettenis &&
273*b725ae77Skettenis /* Either we checked on the whole object in hand and found the
274*b725ae77Skettenis top offset to be zero */
275*b725ae77Skettenis (((top_offset == 0) &&
276*b725ae77Skettenis using_enclosing &&
277*b725ae77Skettenis TYPE_LENGTH (known_type) == TYPE_LENGTH (rtti_type))
278*b725ae77Skettenis ||
279*b725ae77Skettenis /* Or we checked on the embedded object and top offset was the
280*b725ae77Skettenis same as the embedded offset */
281*b725ae77Skettenis ((top_offset == VALUE_EMBEDDED_OFFSET (v)) &&
282*b725ae77Skettenis !using_enclosing &&
283*b725ae77Skettenis TYPE_LENGTH (VALUE_ENCLOSING_TYPE (v)) == TYPE_LENGTH (rtti_type))))
284*b725ae77Skettenis
285*b725ae77Skettenis *full = 1;
286*b725ae77Skettenis
287*b725ae77Skettenis return rtti_type;
288*b725ae77Skettenis }
289*b725ae77Skettenis
290*b725ae77Skettenis extern int gnuv2_baseclass_offset (struct type *type, int index,
291*b725ae77Skettenis char *valaddr, CORE_ADDR address);
292*b725ae77Skettenis
293*b725ae77Skettenis static void
init_hpacc_ops(void)294*b725ae77Skettenis init_hpacc_ops (void)
295*b725ae77Skettenis {
296*b725ae77Skettenis hpacc_abi_ops.shortname = "hpaCC";
297*b725ae77Skettenis hpacc_abi_ops.longname = "HP aCC ABI";
298*b725ae77Skettenis hpacc_abi_ops.doc = "HP aCC ABI";
299*b725ae77Skettenis hpacc_abi_ops.is_destructor_name = hpacc_is_destructor_name;
300*b725ae77Skettenis hpacc_abi_ops.is_constructor_name = hpacc_is_constructor_name;
301*b725ae77Skettenis hpacc_abi_ops.is_vtable_name = hpacc_is_vtable_name;
302*b725ae77Skettenis hpacc_abi_ops.is_operator_name = hpacc_is_operator_name;
303*b725ae77Skettenis hpacc_abi_ops.virtual_fn_field = hpacc_virtual_fn_field;
304*b725ae77Skettenis hpacc_abi_ops.rtti_type = hpacc_value_rtti_type;
305*b725ae77Skettenis /* It seems that this function is specific to GNU G++ < 3.0.
306*b725ae77Skettenis However, it is called for data members even in the HP
307*b725ae77Skettenis case (although not for member functions).
308*b725ae77Skettenis FIXME: Is that correct? */
309*b725ae77Skettenis hpacc_abi_ops.baseclass_offset = gnuv2_baseclass_offset;
310*b725ae77Skettenis }
311*b725ae77Skettenis
312*b725ae77Skettenis extern initialize_file_ftype _initialize_hpacc_abi; /* -Wmissing-prototypes */
313*b725ae77Skettenis
314*b725ae77Skettenis void
_initialize_hpacc_abi(void)315*b725ae77Skettenis _initialize_hpacc_abi (void)
316*b725ae77Skettenis {
317*b725ae77Skettenis init_hpacc_ops ();
318*b725ae77Skettenis
319*b725ae77Skettenis regcomp (&constructor_pattern,
320*b725ae77Skettenis "^This will never match anything, please fill it in$", REG_NOSUB);
321*b725ae77Skettenis
322*b725ae77Skettenis regcomp (&destructor_pattern,
323*b725ae77Skettenis "^This will never match anything, please fill it in$", REG_NOSUB);
324*b725ae77Skettenis
325*b725ae77Skettenis regcomp (&operator_pattern,
326*b725ae77Skettenis "^This will never match anything, please fill it in$", REG_NOSUB);
327*b725ae77Skettenis
328*b725ae77Skettenis register_cp_abi (&hpacc_abi_ops);
329*b725ae77Skettenis }
330