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