2
1

700-debian_cp-pass-by-reference.patch 16 KB

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