rs6000-lynx178-tdep.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* Copyright (C) 2012-2022 Free Software Foundation, Inc.
  2. This file is part of GDB.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include "defs.h"
  14. #include "osabi.h"
  15. #include "regcache.h"
  16. #include "gdbcore.h"
  17. #include "gdbtypes.h"
  18. #include "infcall.h"
  19. #include "ppc-tdep.h"
  20. #include "target-float.h"
  21. #include "value.h"
  22. #include "xcoffread.h"
  23. /* Implement the "push_dummy_call" gdbarch method. */
  24. static CORE_ADDR
  25. rs6000_lynx178_push_dummy_call (struct gdbarch *gdbarch,
  26. struct value *function,
  27. struct regcache *regcache, CORE_ADDR bp_addr,
  28. int nargs, struct value **args, CORE_ADDR sp,
  29. function_call_return_method return_method,
  30. CORE_ADDR struct_addr)
  31. {
  32. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  33. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  34. int ii;
  35. int len = 0;
  36. int argno; /* current argument number */
  37. int argbytes; /* current argument byte */
  38. gdb_byte tmp_buffer[50];
  39. int f_argno = 0; /* current floating point argno */
  40. int wordsize = tdep->wordsize;
  41. struct value *arg = 0;
  42. struct type *type;
  43. ULONGEST saved_sp;
  44. /* The calling convention this function implements assumes the
  45. processor has floating-point registers. We shouldn't be using it
  46. on PPC variants that lack them. */
  47. gdb_assert (ppc_floating_point_unit_p (gdbarch));
  48. /* The first eight words of ther arguments are passed in registers.
  49. Copy them appropriately. */
  50. ii = 0;
  51. /* If the function is returning a `struct', then the first word
  52. (which will be passed in r3) is used for struct return address.
  53. In that case we should advance one word and start from r4
  54. register to copy parameters. */
  55. if (return_method == return_method_struct)
  56. {
  57. regcache_raw_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
  58. struct_addr);
  59. ii++;
  60. }
  61. /* Effectively indirect call... gcc does...
  62. return_val example( float, int);
  63. eabi:
  64. float in fp0, int in r3
  65. offset of stack on overflow 8/16
  66. for varargs, must go by type.
  67. power open:
  68. float in r3&r4, int in r5
  69. offset of stack on overflow different
  70. both:
  71. return in r3 or f0. If no float, must study how gcc emulates floats;
  72. pay attention to arg promotion.
  73. User may have to cast\args to handle promotion correctly
  74. since gdb won't know if prototype supplied or not. */
  75. for (argno = 0, argbytes = 0; argno < nargs && ii < 8; ++ii)
  76. {
  77. int reg_size = register_size (gdbarch, ii + 3);
  78. arg = args[argno];
  79. type = check_typedef (value_type (arg));
  80. len = TYPE_LENGTH (type);
  81. if (type->code () == TYPE_CODE_FLT)
  82. {
  83. /* Floating point arguments are passed in fpr's, as well as gpr's.
  84. There are 13 fpr's reserved for passing parameters. At this point
  85. there is no way we would run out of them.
  86. Always store the floating point value using the register's
  87. floating-point format. */
  88. const int fp_regnum = tdep->ppc_fp0_regnum + 1 + f_argno;
  89. gdb_byte reg_val[PPC_MAX_REGISTER_SIZE];
  90. struct type *reg_type = register_type (gdbarch, fp_regnum);
  91. gdb_assert (len <= 8);
  92. target_float_convert (value_contents (arg).data (), type, reg_val,
  93. reg_type);
  94. regcache->cooked_write (fp_regnum, reg_val);
  95. ++f_argno;
  96. }
  97. if (len > reg_size)
  98. {
  99. /* Argument takes more than one register. */
  100. while (argbytes < len)
  101. {
  102. gdb_byte word[PPC_MAX_REGISTER_SIZE];
  103. memset (word, 0, reg_size);
  104. memcpy (word,
  105. ((char *) value_contents (arg).data ()) + argbytes,
  106. (len - argbytes) > reg_size
  107. ? reg_size : len - argbytes);
  108. regcache->cooked_write (tdep->ppc_gp0_regnum + 3 + ii, word);
  109. ++ii, argbytes += reg_size;
  110. if (ii >= 8)
  111. goto ran_out_of_registers_for_arguments;
  112. }
  113. argbytes = 0;
  114. --ii;
  115. }
  116. else
  117. {
  118. /* Argument can fit in one register. No problem. */
  119. gdb_byte word[PPC_MAX_REGISTER_SIZE];
  120. memset (word, 0, reg_size);
  121. memcpy (word, value_contents (arg).data (), len);
  122. regcache->cooked_write (tdep->ppc_gp0_regnum + 3 +ii, word);
  123. }
  124. ++argno;
  125. }
  126. ran_out_of_registers_for_arguments:
  127. regcache_cooked_read_unsigned (regcache,
  128. gdbarch_sp_regnum (gdbarch),
  129. &saved_sp);
  130. /* Location for 8 parameters are always reserved. */
  131. sp -= wordsize * 8;
  132. /* Another six words for back chain, TOC register, link register, etc. */
  133. sp -= wordsize * 6;
  134. /* Stack pointer must be quadword aligned. */
  135. sp = align_down (sp, 16);
  136. /* If there are more arguments, allocate space for them in
  137. the stack, then push them starting from the ninth one. */
  138. if ((argno < nargs) || argbytes)
  139. {
  140. int space = 0, jj;
  141. if (argbytes)
  142. {
  143. space += align_up (len - argbytes, 4);
  144. jj = argno + 1;
  145. }
  146. else
  147. jj = argno;
  148. for (; jj < nargs; ++jj)
  149. {
  150. struct value *val = args[jj];
  151. space += align_up (TYPE_LENGTH (value_type (val)), 4);
  152. }
  153. /* Add location required for the rest of the parameters. */
  154. space = align_up (space, 16);
  155. sp -= space;
  156. /* This is another instance we need to be concerned about
  157. securing our stack space. If we write anything underneath %sp
  158. (r1), we might conflict with the kernel who thinks he is free
  159. to use this area. So, update %sp first before doing anything
  160. else. */
  161. regcache_raw_write_signed (regcache,
  162. gdbarch_sp_regnum (gdbarch), sp);
  163. /* If the last argument copied into the registers didn't fit there
  164. completely, push the rest of it into stack. */
  165. if (argbytes)
  166. {
  167. write_memory (sp + 24 + (ii * 4),
  168. value_contents (arg).data () + argbytes,
  169. len - argbytes);
  170. ++argno;
  171. ii += align_up (len - argbytes, 4) / 4;
  172. }
  173. /* Push the rest of the arguments into stack. */
  174. for (; argno < nargs; ++argno)
  175. {
  176. arg = args[argno];
  177. type = check_typedef (value_type (arg));
  178. len = TYPE_LENGTH (type);
  179. /* Float types should be passed in fpr's, as well as in the
  180. stack. */
  181. if (type->code () == TYPE_CODE_FLT && f_argno < 13)
  182. {
  183. gdb_assert (len <= 8);
  184. regcache->cooked_write (tdep->ppc_fp0_regnum + 1 + f_argno,
  185. value_contents (arg).data ());
  186. ++f_argno;
  187. }
  188. write_memory (sp + 24 + (ii * 4), value_contents (arg).data (), len);
  189. ii += align_up (len, 4) / 4;
  190. }
  191. }
  192. /* Set the stack pointer. According to the ABI, the SP is meant to
  193. be set _before_ the corresponding stack space is used. On AIX,
  194. this even applies when the target has been completely stopped!
  195. Not doing this can lead to conflicts with the kernel which thinks
  196. that it still has control over this not-yet-allocated stack
  197. region. */
  198. regcache_raw_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
  199. /* Set back chain properly. */
  200. store_unsigned_integer (tmp_buffer, wordsize, byte_order, saved_sp);
  201. write_memory (sp, tmp_buffer, wordsize);
  202. /* Point the inferior function call's return address at the dummy's
  203. breakpoint. */
  204. regcache_raw_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
  205. target_store_registers (regcache, -1);
  206. return sp;
  207. }
  208. /* Implement the "return_value" gdbarch method. */
  209. static enum return_value_convention
  210. rs6000_lynx178_return_value (struct gdbarch *gdbarch, struct value *function,
  211. struct type *valtype, struct regcache *regcache,
  212. gdb_byte *readbuf, const gdb_byte *writebuf)
  213. {
  214. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  215. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  216. /* The calling convention this function implements assumes the
  217. processor has floating-point registers. We shouldn't be using it
  218. on PowerPC variants that lack them. */
  219. gdb_assert (ppc_floating_point_unit_p (gdbarch));
  220. /* AltiVec extension: Functions that declare a vector data type as a
  221. return value place that return value in VR2. */
  222. if (valtype->code () == TYPE_CODE_ARRAY && valtype->is_vector ()
  223. && TYPE_LENGTH (valtype) == 16)
  224. {
  225. if (readbuf)
  226. regcache->cooked_read (tdep->ppc_vr0_regnum + 2, readbuf);
  227. if (writebuf)
  228. regcache->cooked_write (tdep->ppc_vr0_regnum + 2, writebuf);
  229. return RETURN_VALUE_REGISTER_CONVENTION;
  230. }
  231. /* If the called subprogram returns an aggregate, there exists an
  232. implicit first argument, whose value is the address of a caller-
  233. allocated buffer into which the callee is assumed to store its
  234. return value. All explicit parameters are appropriately
  235. relabeled. */
  236. if (valtype->code () == TYPE_CODE_STRUCT
  237. || valtype->code () == TYPE_CODE_UNION
  238. || valtype->code () == TYPE_CODE_ARRAY)
  239. return RETURN_VALUE_STRUCT_CONVENTION;
  240. /* Scalar floating-point values are returned in FPR1 for float or
  241. double, and in FPR1:FPR2 for quadword precision. Fortran
  242. complex*8 and complex*16 are returned in FPR1:FPR2, and
  243. complex*32 is returned in FPR1:FPR4. */
  244. if (valtype->code () == TYPE_CODE_FLT
  245. && (TYPE_LENGTH (valtype) == 4 || TYPE_LENGTH (valtype) == 8))
  246. {
  247. struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
  248. gdb_byte regval[8];
  249. /* FIXME: kettenis/2007-01-01: Add support for quadword
  250. precision and complex. */
  251. if (readbuf)
  252. {
  253. regcache->cooked_read (tdep->ppc_fp0_regnum + 1, regval);
  254. target_float_convert (regval, regtype, readbuf, valtype);
  255. }
  256. if (writebuf)
  257. {
  258. target_float_convert (writebuf, valtype, regval, regtype);
  259. regcache->cooked_write (tdep->ppc_fp0_regnum + 1, regval);
  260. }
  261. return RETURN_VALUE_REGISTER_CONVENTION;
  262. }
  263. /* Values of the types int, long, short, pointer, and char (length
  264. is less than or equal to four bytes), as well as bit values of
  265. lengths less than or equal to 32 bits, must be returned right
  266. justified in GPR3 with signed values sign extended and unsigned
  267. values zero extended, as necessary. */
  268. if (TYPE_LENGTH (valtype) <= tdep->wordsize)
  269. {
  270. if (readbuf)
  271. {
  272. ULONGEST regval;
  273. /* For reading we don't have to worry about sign extension. */
  274. regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
  275. &regval);
  276. store_unsigned_integer (readbuf, TYPE_LENGTH (valtype), byte_order,
  277. regval);
  278. }
  279. if (writebuf)
  280. {
  281. /* For writing, use unpack_long since that should handle any
  282. required sign extension. */
  283. regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
  284. unpack_long (valtype, writebuf));
  285. }
  286. return RETURN_VALUE_REGISTER_CONVENTION;
  287. }
  288. /* Eight-byte non-floating-point scalar values must be returned in
  289. GPR3:GPR4. */
  290. if (TYPE_LENGTH (valtype) == 8)
  291. {
  292. gdb_assert (valtype->code () != TYPE_CODE_FLT);
  293. gdb_assert (tdep->wordsize == 4);
  294. if (readbuf)
  295. {
  296. gdb_byte regval[8];
  297. regcache->cooked_read (tdep->ppc_gp0_regnum + 3, regval);
  298. regcache->cooked_read (tdep->ppc_gp0_regnum + 4, regval + 4);
  299. memcpy (readbuf, regval, 8);
  300. }
  301. if (writebuf)
  302. {
  303. regcache->cooked_write (tdep->ppc_gp0_regnum + 3, writebuf);
  304. regcache->cooked_write (tdep->ppc_gp0_regnum + 4, writebuf + 4);
  305. }
  306. return RETURN_VALUE_REGISTER_CONVENTION;
  307. }
  308. return RETURN_VALUE_STRUCT_CONVENTION;
  309. }
  310. /* PowerPC Lynx178 OSABI sniffer. */
  311. static enum gdb_osabi
  312. rs6000_lynx178_osabi_sniffer (bfd *abfd)
  313. {
  314. if (bfd_get_flavour (abfd) != bfd_target_xcoff_flavour)
  315. return GDB_OSABI_UNKNOWN;
  316. /* The only noticeable difference between Lynx178 XCOFF files and
  317. AIX XCOFF files comes from the fact that there are no shared
  318. libraries on Lynx178. So if the number of import files is
  319. different from zero, it cannot be a Lynx178 binary. */
  320. if (xcoff_get_n_import_files (abfd) != 0)
  321. return GDB_OSABI_UNKNOWN;
  322. return GDB_OSABI_LYNXOS178;
  323. }
  324. /* Callback for powerpc-lynx178 initialization. */
  325. static void
  326. rs6000_lynx178_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
  327. {
  328. set_gdbarch_push_dummy_call (gdbarch, rs6000_lynx178_push_dummy_call);
  329. set_gdbarch_return_value (gdbarch, rs6000_lynx178_return_value);
  330. set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  331. }
  332. void _initialize_rs6000_lynx178_tdep ();
  333. void
  334. _initialize_rs6000_lynx178_tdep ()
  335. {
  336. gdbarch_register_osabi_sniffer (bfd_arch_rs6000,
  337. bfd_target_xcoff_flavour,
  338. rs6000_lynx178_osabi_sniffer);
  339. gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_LYNXOS178,
  340. rs6000_lynx178_init_osabi);
  341. }