1 This patch needs to be submitted for the FSF. Also, there may be testcases
2 already in the GDB testsuite (currently disabled) that it would probably fix.
4 Index: gdb-6.3/gdb/infcall.c
5 ===================================================================
6 --- gdb-6.3.orig/gdb/infcall.c 2004-10-08 04:15:56.000000000 -0400
7 +++ gdb-6.3/gdb/infcall.c 2004-11-10 12:30:07.000000000 -0500
9 #include "gdb_string.h"
11 #include "dummy-frame.h"
14 /* NOTE: cagney/2003-04-16: What's the future of this code?
16 @@ -297,8 +298,8 @@ call_function_by_hand (struct value *fun
20 - struct type *value_type;
21 - unsigned char struct_return;
22 + struct type *value_type, *target_value_type;
23 + unsigned char struct_return = 0, cp_struct_return = 0;
24 CORE_ADDR struct_addr = 0;
25 struct regcache *retbuf;
26 struct cleanup *retbuf_cleanup;
27 @@ -312,6 +313,7 @@ call_function_by_hand (struct value *fun
28 struct regcache *caller_regcache;
29 struct cleanup *caller_regcache_cleanup;
30 struct frame_id dummy_id;
31 + struct cleanup *args_cleanup;
33 if (!target_has_execution)
35 @@ -410,10 +412,31 @@ call_function_by_hand (struct value *fun
36 using_gcc = (b == NULL ? 2 : BLOCK_GCC_COMPILED (b));
39 - /* Are we returning a value using a structure return or a normal
41 + /* Are we returning a value using a structure return (passing a
42 + hidden argument pointing to storage) or a normal value return?
43 + There are two cases: C++ ABI mandated structure return and
44 + target ABI structure return. The variable STRUCT_RETURN only
45 + describes the latter. The C++ version is handled by passing
46 + the return location as the first parameter to the function,
47 + even preceding "this". This is different from the target
48 + ABI version, which is target-specific; for instance, on ia64
49 + the first argument is passed in out0 but the hidden structure
50 + return pointer would normally be passed in r8. */
52 - struct_return = using_struct_return (value_type, using_gcc);
53 + if (current_language->la_language == language_cplus
54 + && cp_pass_by_reference (value_type))
56 + cp_struct_return = 1;
58 + /* Tell the target specific argument pushing routine not to
60 + target_value_type = builtin_type_void;
64 + struct_return = using_struct_return (value_type, using_gcc);
65 + target_value_type = value_type;
68 /* Determine the location of the breakpoint (and possibly other
69 stuff) that the called function will return to. The SPARC, for a
70 @@ -432,7 +455,7 @@ call_function_by_hand (struct value *fun
71 if (INNER_THAN (1, 2))
73 sp = push_dummy_code (current_gdbarch, sp, funaddr,
74 - using_gcc, args, nargs, value_type,
75 + using_gcc, args, nargs, target_value_type,
79 @@ -440,7 +463,7 @@ call_function_by_hand (struct value *fun
82 sp = push_dummy_code (current_gdbarch, sp, funaddr,
83 - using_gcc, args, nargs, value_type,
84 + using_gcc, args, nargs, target_value_type,
88 @@ -507,9 +530,15 @@ call_function_by_hand (struct value *fun
89 param_type = TYPE_FIELD_TYPE (ftype, i);
94 args[i] = value_arg_coerce (args[i], param_type, prototyped);
96 + /* FIXME: Is current_language the right language? */
97 + if (current_language->la_language == language_cplus
98 + && param_type != NULL
99 + && cp_pass_by_reference (param_type))
100 + args[i] = value_addr (args[i]);
102 /* elz: this code is to handle the case in which the function
103 to be called has a pointer to function as parameter and the
104 corresponding actual argument is the address of a function
105 @@ -607,7 +636,7 @@ You must use a pointer to function type
106 stack, if necessary. Make certain that the value is correctly
110 + if (struct_return || cp_struct_return)
112 int len = TYPE_LENGTH (value_type);
113 if (INNER_THAN (1, 2))
114 @@ -632,6 +661,22 @@ You must use a pointer to function type
118 + if (cp_struct_return)
120 + struct value **new_args;
122 + /* Add the new argument to the front of the argument list. */
123 + new_args = xmalloc (sizeof (struct value *) * (nargs + 1));
124 + new_args[0] = value_from_pointer (lookup_pointer_type (value_type),
126 + memcpy (&new_args[1], &args[0], sizeof (struct value *) * nargs);
129 + args_cleanup = make_cleanup (xfree, args);
132 + args_cleanup = make_cleanup (null_cleanup, NULL);
134 /* Create the dummy stack frame. Pass in the call dummy address as,
135 presumably, the ABI code knows where, in the call dummy, the
136 return address should be pointed. */
137 @@ -649,6 +694,8 @@ You must use a pointer to function type
139 error ("This target does not support function calls");
141 + do_cleanups (args_cleanup);
143 /* Set up a frame ID for the dummy frame so we can pass it to
144 set_momentary_breakpoint. We need to give the breakpoint a frame
145 ID so that the breakpoint code can correctly re-identify the
146 @@ -839,11 +886,7 @@ the function call).", name);
147 /* Figure out the value returned by the function, return that. */
149 struct value *retval;
150 - if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
151 - /* If the function returns void, don't bother fetching the
153 - retval = allocate_value (value_type);
154 - else if (struct_return)
155 + if (struct_return || cp_struct_return)
156 /* NOTE: cagney/2003-09-27: This assumes that PUSH_DUMMY_CALL
157 has correctly stored STRUCT_ADDR in the target. In the past
158 that hasn't been the case, the old MIPS PUSH_ARGUMENTS
159 @@ -853,6 +896,10 @@ the function call).", name);
160 "struct return convention", check that PUSH_DUMMY_CALL isn't
162 retval = value_at (value_type, struct_addr, NULL);
163 + else if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
164 + /* If the function returns void, don't bother fetching the
166 + retval = allocate_value (value_type);
169 /* This code only handles "register convention". */
170 Index: gdb-6.3/gdb/cp-abi.h
171 ===================================================================
172 --- gdb-6.3.orig/gdb/cp-abi.h 2003-04-12 13:41:25.000000000 -0400
173 +++ gdb-6.3/gdb/cp-abi.h 2004-11-10 12:30:07.000000000 -0500
175 /* Abstraction of various C++ ABI's we support, and the info we need
177 Contributed by Daniel Berlin <dberlin@redhat.com>
178 - Copyright 2001 Free Software Foundation, Inc.
179 + Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
181 This file is part of GDB.
183 @@ -145,6 +145,10 @@ extern struct type *value_rtti_type (str
184 extern int baseclass_offset (struct type *type, int index, char *valaddr,
187 +/* Return non-zero if an argument of type TYPE should be passed by reference
188 + instead of value. */
189 +extern int cp_pass_by_reference (struct type *type);
193 const char *shortname;
194 @@ -162,6 +166,7 @@ struct cp_abi_ops
196 int (*baseclass_offset) (struct type *type, int index, char *valaddr,
198 + int (*pass_by_reference) (struct type *type);
202 Index: gdb-6.3/gdb/cp-abi.c
203 ===================================================================
204 --- gdb-6.3.orig/gdb/cp-abi.c 2003-11-26 17:04:00.000000000 -0500
205 +++ gdb-6.3/gdb/cp-abi.c 2004-11-10 12:30:07.000000000 -0500
207 /* Generic code for supporting multiple C++ ABI's
208 - Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
209 + Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
211 This file is part of GDB.
213 @@ -94,6 +94,14 @@ value_rtti_type (struct value *v, int *f
214 return (*current_cp_abi.rtti_type) (v, full, top, using_enc);
218 +cp_pass_by_reference (struct type *type)
220 + if ((current_cp_abi.pass_by_reference) == NULL)
222 + return (*current_cp_abi.pass_by_reference) (type);
225 /* Set the current C++ ABI to SHORT_NAME. */
228 Index: gdb-6.3/gdb/gnu-v3-abi.c
229 ===================================================================
230 --- gdb-6.3.orig/gdb/gnu-v3-abi.c 2004-03-15 15:38:08.000000000 -0500
231 +++ gdb-6.3/gdb/gnu-v3-abi.c 2004-11-10 12:30:07.000000000 -0500
233 /* Abstraction of GNU v3 abi.
234 Contributed by Jim Blandy <jimb@redhat.com>
236 - Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
237 + Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
239 This file is part of GDB.
241 @@ -419,6 +419,84 @@ gnuv3_baseclass_offset (struct type *typ
245 +/* Return nonzero if a type should be passed by reference.
247 + The rule in the v3 ABI document comes from section 3.1.1. If the
248 + type has a non-trivial copy constructor or destructor, then the
249 + caller must make a copy (by calling the copy constructor if there
250 + is one or perform the copy itself otherwise), pass the address of
251 + the copy, and then destroy the temporary (if necessary).
253 + For return values with non-trivial copy constructors or
254 + destructors, space will be allocated in the caller, and a pointer
255 + will be passed as the first argument (preceding "this").
257 + We don't have a bulletproof mechanism for determining whether a
258 + constructor or destructor is trivial. For GCC and DWARF2 debug
259 + information, we can check the artificial flag.
261 + We don't do anything with the constructors or destructors yet,
262 + but we have to get the argument passing right anyway. */
264 +gnuv3_pass_by_reference (struct type *type)
266 + int fieldnum, fieldelem, basenum;
268 + CHECK_TYPEDEF (type);
270 + /* We're only interested in things that can have methods. */
271 + if (TYPE_CODE (type) != TYPE_CODE_STRUCT
272 + && TYPE_CODE (type) != TYPE_CODE_CLASS
273 + && TYPE_CODE (type) != TYPE_CODE_UNION)
276 + for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
277 + for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
280 + struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum);
281 + char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
282 + struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
284 + /* If this function is marked as artificial, it is compiler-generated,
285 + and we assume it is trivial. */
286 + if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem))
289 + /* If we've found a destructor, we must pass this by reference. */
290 + if (name[0] == '~')
293 + /* If the mangled name of this method doesn't indicate that it
294 + is a constructor, we're not interested.
296 + FIXME drow/2004-05-27: We could do this using the name of
297 + the method and the name of the class instead of dealing
298 + with the mangled name. We don't have a convenient function
299 + to strip off both leading scope qualifiers and trailing
300 + template arguments yet. */
301 + if (!is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem)))
304 + /* If this method takes two arguments, and the second argument is
305 + a reference to this class, then it is a copy constructor. */
306 + if (TYPE_NFIELDS (fieldtype) == 2
307 + && TYPE_CODE (TYPE_FIELD_TYPE (fieldtype, 1)) == TYPE_CODE_REF
308 + && check_typedef (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (fieldtype, 1))) == type)
312 + /* Even if all the constructors and destructors were artificial, one
313 + of them may have invoked a non-artificial constructor or
314 + destructor in a base class. If any base class needs to be passed
315 + by reference, so does this class. */
316 + for (basenum = 0; basenum < TYPE_N_BASECLASSES (type); basenum++)
317 + if (gnuv3_pass_by_reference (TYPE_BASECLASS (type, basenum)))
324 init_gnuv3_ops (void)
326 @@ -434,6 +512,7 @@ init_gnuv3_ops (void)
327 gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
328 gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
329 gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
330 + gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
333 extern initialize_file_ftype _initialize_gnu_v3_abi; /* -Wmissing-prototypes */
334 Index: gdb-6.3/gdb/hpacc-abi.c
335 ===================================================================
336 --- gdb-6.3.orig/gdb/hpacc-abi.c 2003-06-08 14:27:13.000000000 -0400
337 +++ gdb-6.3/gdb/hpacc-abi.c 2004-11-10 12:30:07.000000000 -0500
339 Most of the real code is from HP, i've just fiddled it to fit in
340 the C++ ABI abstraction framework.
342 - Copyright 2001 Free Software Foundation, Inc.
343 + Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
345 This file is part of GDB.
347 Index: gdb-6.3/gdb/Makefile.in
348 ===================================================================
349 --- gdb-6.3.orig/gdb/Makefile.in 2004-11-10 12:30:06.000000000 -0500
350 +++ gdb-6.3/gdb/Makefile.in 2004-11-10 12:30:07.000000000 -0500
351 @@ -2073,7 +2073,7 @@ ia64-tdep.o: ia64-tdep.c $(defs_h) $(inf
352 infcall.o: infcall.c $(defs_h) $(breakpoint_h) $(target_h) $(regcache_h) \
353 $(inferior_h) $(gdb_assert_h) $(block_h) $(gdbcore_h) $(language_h) \
354 $(objfiles_h) $(gdbcmd_h) $(command_h) $(gdb_string_h) $(infcall_h) \
356 + $(dummy_frame_h) $(cp_abi_h)
357 inf-child.o: inf-child.c $(defs_h) $(regcache_h) $(memattr_h) $(symtab_h) \
358 $(target_h) $(inferior_h) $(gdb_string_h)
359 infcmd.o: infcmd.c $(defs_h) $(gdb_string_h) $(symtab_h) $(gdbtypes_h) \
360 Index: gdb-6.3/gdb/testsuite/gdb.cp/pass-by-ref.exp
361 ===================================================================
362 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
363 +++ gdb-6.3/gdb/testsuite/gdb.cp/pass-by-ref.exp 2004-11-11 09:48:00.498518899 -0500
365 +# This testcase is part of GDB, the GNU debugger.
367 +# Copyright 2004 Free Software Foundation, Inc.
369 +# This program is free software; you can redistribute it and/or modify
370 +# it under the terms of the GNU General Public License as published by
371 +# the Free Software Foundation; either version 2 of the License, or
372 +# (at your option) any later version.
374 +# This program is distributed in the hope that it will be useful,
375 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
376 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
377 +# GNU General Public License for more details.
379 +# You should have received a copy of the GNU General Public License
380 +# along with this program; if not, write to the Free Software
381 +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
383 +# Check that GDB can call C++ functions whose parameters have
384 +# object type, but are passed by reference.
386 +set testfile "pass-by-ref"
387 +set srcfile ${testfile}.cc
388 +set binfile ${objdir}/${subdir}/${testfile}
389 +if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
395 +gdb_reinitialize_dir $srcdir/$subdir
398 +if ![runto_main] then {
402 +gdb_test "print foo (global_obj)" " = 3" "call function"
403 Index: gdb-6.3/gdb/testsuite/gdb.cp/pass-by-ref.cc
404 ===================================================================
405 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
406 +++ gdb-6.3/gdb/testsuite/gdb.cp/pass-by-ref.cc 2004-11-11 09:44:17.815014667 -0500
408 +/* This testcase is part of GDB, the GNU debugger.
410 + Copyright 2004 Free Software Foundation, Inc.
412 + This program is free software; you can redistribute it and/or modify
413 + it under the terms of the GNU General Public License as published by
414 + the Free Software Foundation; either version 2 of the License, or
415 + (at your option) any later version.
417 + This program is distributed in the hope that it will be useful,
418 + but WITHOUT ANY WARRANTY; without even the implied warranty of
419 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
420 + GNU General Public License for more details.
422 + You should have received a copy of the GNU General Public License
423 + along with this program; if not, write to the Free Software
424 + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
437 + return arg.var[0] + arg.var[1];
446 +Obj::Obj (const Obj &obj)
448 + var[0] = obj.var[0];
449 + var[1] = obj.var[1];
462 + int bar = foo (global_obj);