1 /* Copyright (C) 2012-2013 Free Software Foundation, Inc.
3 This file is part of GDB.
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.
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.
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/>. */
26 #include "xcoffread.h"
28 /* Implement the "push_dummy_call" gdbarch method. */
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
)
37 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
38 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
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
);
48 struct value
*arg
= 0;
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
));
58 /* The first eight words of ther arguments are passed in registers.
59 Copy them appropriately. */
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. */
68 regcache_raw_write_unsigned (regcache
, tdep
->ppc_gp0_regnum
+ 3,
73 /* Effectively indirect call... gcc does...
75 return_val example( float, int);
78 float in fp0, int in r3
79 offset of stack on overflow 8/16
80 for varargs, must go by type.
82 float in r3&r4, int in r5
83 offset of stack on overflow different
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. */
90 for (argno
= 0, argbytes
= 0; argno
< nargs
&& ii
< 8; ++ii
)
92 int reg_size
= register_size (gdbarch
, ii
+ 3);
95 type
= check_typedef (value_type (arg
));
96 len
= TYPE_LENGTH (type
);
98 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
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. */
105 gdb_assert (len
<= 8);
107 regcache_cooked_write (regcache
,
108 tdep
->ppc_fp0_regnum
+ 1 + f_argno
,
109 value_contents (arg
));
116 /* Argument takes more than one register. */
117 while (argbytes
< len
)
119 gdb_byte word
[MAX_REGISTER_SIZE
];
120 memset (word
, 0, reg_size
);
122 ((char *) value_contents (arg
)) + argbytes
,
123 (len
- argbytes
) > reg_size
124 ? reg_size
: len
- argbytes
);
125 regcache_cooked_write (regcache
,
126 tdep
->ppc_gp0_regnum
+ 3 + ii
,
128 ++ii
, argbytes
+= reg_size
;
131 goto ran_out_of_registers_for_arguments
;
138 /* Argument can fit in one register. No problem. */
139 int adj
= gdbarch_byte_order (gdbarch
)
140 == BFD_ENDIAN_BIG
? reg_size
- len
: 0;
141 gdb_byte word
[MAX_REGISTER_SIZE
];
143 memset (word
, 0, reg_size
);
144 memcpy (word
, value_contents (arg
), len
);
145 regcache_cooked_write (regcache
, tdep
->ppc_gp0_regnum
+ 3 +ii
, word
);
150 ran_out_of_registers_for_arguments
:
152 regcache_cooked_read_unsigned (regcache
,
153 gdbarch_sp_regnum (gdbarch
),
156 /* Location for 8 parameters are always reserved. */
159 /* Another six words for back chain, TOC register, link register, etc. */
162 /* Stack pointer must be quadword aligned. */
163 sp
= align_down (sp
, 16);
165 /* If there are more arguments, allocate space for them in
166 the stack, then push them starting from the ninth one. */
168 if ((argno
< nargs
) || argbytes
)
174 space
+= align_up (len
- argbytes
, 4);
180 for (; jj
< nargs
; ++jj
)
182 struct value
*val
= args
[jj
];
184 space
+= align_up (TYPE_LENGTH (value_type (val
)), 4);
187 /* Add location required for the rest of the parameters. */
188 space
= align_up (space
, 16);
191 /* This is another instance we need to be concerned about
192 securing our stack space. If we write anything underneath %sp
193 (r1), we might conflict with the kernel who thinks he is free
194 to use this area. So, update %sp first before doing anything
197 regcache_raw_write_signed (regcache
,
198 gdbarch_sp_regnum (gdbarch
), sp
);
200 /* If the last argument copied into the registers didn't fit there
201 completely, push the rest of it into stack. */
205 write_memory (sp
+ 24 + (ii
* 4),
206 value_contents (arg
) + argbytes
,
209 ii
+= align_up (len
- argbytes
, 4) / 4;
212 /* Push the rest of the arguments into stack. */
213 for (; argno
< nargs
; ++argno
)
217 type
= check_typedef (value_type (arg
));
218 len
= TYPE_LENGTH (type
);
221 /* Float types should be passed in fpr's, as well as in the
223 if (TYPE_CODE (type
) == TYPE_CODE_FLT
&& f_argno
< 13)
226 gdb_assert (len
<= 8);
228 regcache_cooked_write (regcache
,
229 tdep
->ppc_fp0_regnum
+ 1 + f_argno
,
230 value_contents (arg
));
234 write_memory (sp
+ 24 + (ii
* 4), value_contents (arg
), len
);
235 ii
+= align_up (len
, 4) / 4;
239 /* Set the stack pointer. According to the ABI, the SP is meant to
240 be set _before_ the corresponding stack space is used. On AIX,
241 this even applies when the target has been completely stopped!
242 Not doing this can lead to conflicts with the kernel which thinks
243 that it still has control over this not-yet-allocated stack
245 regcache_raw_write_signed (regcache
, gdbarch_sp_regnum (gdbarch
), sp
);
247 /* Set back chain properly. */
248 store_unsigned_integer (tmp_buffer
, wordsize
, byte_order
, saved_sp
);
249 write_memory (sp
, tmp_buffer
, wordsize
);
251 /* Point the inferior function call's return address at the dummy's
253 regcache_raw_write_signed (regcache
, tdep
->ppc_lr_regnum
, bp_addr
);
255 target_store_registers (regcache
, -1);
259 /* Implement the "return_value" gdbarch method. */
261 static enum return_value_convention
262 rs6000_lynx178_return_value (struct gdbarch
*gdbarch
, struct value
*function
,
263 struct type
*valtype
, struct regcache
*regcache
,
264 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
266 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
267 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
269 /* The calling convention this function implements assumes the
270 processor has floating-point registers. We shouldn't be using it
271 on PowerPC variants that lack them. */
272 gdb_assert (ppc_floating_point_unit_p (gdbarch
));
274 /* AltiVec extension: Functions that declare a vector data type as a
275 return value place that return value in VR2. */
276 if (TYPE_CODE (valtype
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (valtype
)
277 && TYPE_LENGTH (valtype
) == 16)
280 regcache_cooked_read (regcache
, tdep
->ppc_vr0_regnum
+ 2, readbuf
);
282 regcache_cooked_write (regcache
, tdep
->ppc_vr0_regnum
+ 2, writebuf
);
284 return RETURN_VALUE_REGISTER_CONVENTION
;
287 /* If the called subprogram returns an aggregate, there exists an
288 implicit first argument, whose value is the address of a caller-
289 allocated buffer into which the callee is assumed to store its
290 return value. All explicit parameters are appropriately
292 if (TYPE_CODE (valtype
) == TYPE_CODE_STRUCT
293 || TYPE_CODE (valtype
) == TYPE_CODE_UNION
294 || TYPE_CODE (valtype
) == TYPE_CODE_ARRAY
)
295 return RETURN_VALUE_STRUCT_CONVENTION
;
297 /* Scalar floating-point values are returned in FPR1 for float or
298 double, and in FPR1:FPR2 for quadword precision. Fortran
299 complex*8 and complex*16 are returned in FPR1:FPR2, and
300 complex*32 is returned in FPR1:FPR4. */
301 if (TYPE_CODE (valtype
) == TYPE_CODE_FLT
302 && (TYPE_LENGTH (valtype
) == 4 || TYPE_LENGTH (valtype
) == 8))
304 struct type
*regtype
= register_type (gdbarch
, tdep
->ppc_fp0_regnum
);
307 /* FIXME: kettenis/2007-01-01: Add support for quadword
308 precision and complex. */
312 regcache_cooked_read (regcache
, tdep
->ppc_fp0_regnum
+ 1, regval
);
313 convert_typed_floating (regval
, regtype
, readbuf
, valtype
);
317 convert_typed_floating (writebuf
, valtype
, regval
, regtype
);
318 regcache_cooked_write (regcache
, tdep
->ppc_fp0_regnum
+ 1, regval
);
321 return RETURN_VALUE_REGISTER_CONVENTION
;
324 /* Values of the types int, long, short, pointer, and char (length
325 is less than or equal to four bytes), as well as bit values of
326 lengths less than or equal to 32 bits, must be returned right
327 justified in GPR3 with signed values sign extended and unsigned
328 values zero extended, as necessary. */
329 if (TYPE_LENGTH (valtype
) <= tdep
->wordsize
)
335 /* For reading we don't have to worry about sign extension. */
336 regcache_cooked_read_unsigned (regcache
, tdep
->ppc_gp0_regnum
+ 3,
338 store_unsigned_integer (readbuf
, TYPE_LENGTH (valtype
), byte_order
,
343 /* For writing, use unpack_long since that should handle any
344 required sign extension. */
345 regcache_cooked_write_unsigned (regcache
, tdep
->ppc_gp0_regnum
+ 3,
346 unpack_long (valtype
, writebuf
));
349 return RETURN_VALUE_REGISTER_CONVENTION
;
352 /* Eight-byte non-floating-point scalar values must be returned in
355 if (TYPE_LENGTH (valtype
) == 8)
357 gdb_assert (TYPE_CODE (valtype
) != TYPE_CODE_FLT
);
358 gdb_assert (tdep
->wordsize
== 4);
364 regcache_cooked_read (regcache
, tdep
->ppc_gp0_regnum
+ 3, regval
);
365 regcache_cooked_read (regcache
, tdep
->ppc_gp0_regnum
+ 4,
367 memcpy (readbuf
, regval
, 8);
371 regcache_cooked_write (regcache
, tdep
->ppc_gp0_regnum
+ 3, writebuf
);
372 regcache_cooked_write (regcache
, tdep
->ppc_gp0_regnum
+ 4,
376 return RETURN_VALUE_REGISTER_CONVENTION
;
379 return RETURN_VALUE_STRUCT_CONVENTION
;
382 /* PowerPC Lynx178 OSABI sniffer. */
384 static enum gdb_osabi
385 rs6000_lynx178_osabi_sniffer (bfd
*abfd
)
387 if (bfd_get_flavour (abfd
) != bfd_target_xcoff_flavour
)
388 return GDB_OSABI_UNKNOWN
;
390 /* The only noticeable difference between Lynx178 XCOFF files and
391 AIX XCOFF files comes from the fact that there are no shared
392 libraries on Lynx178. So if the number of import files is
393 different from zero, it cannot be a Lynx178 binary. */
394 if (xcoff_get_n_import_files (abfd
) != 0)
395 return GDB_OSABI_UNKNOWN
;
397 return GDB_OSABI_LYNXOS178
;
400 /* Callback for powerpc-lynx178 initialization. */
403 rs6000_lynx178_init_osabi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
405 set_gdbarch_push_dummy_call (gdbarch
, rs6000_lynx178_push_dummy_call
);
406 set_gdbarch_return_value (gdbarch
, rs6000_lynx178_return_value
);
407 set_gdbarch_long_double_bit (gdbarch
, 8 * TARGET_CHAR_BIT
);
410 /* -Wmissing-prototypes. */
411 extern initialize_file_ftype _initialize_rs6000_lynx178_tdep
;
414 _initialize_rs6000_lynx178_tdep (void)
416 gdbarch_register_osabi_sniffer (bfd_arch_rs6000
,
417 bfd_target_xcoff_flavour
,
418 rs6000_lynx178_osabi_sniffer
);
419 gdbarch_register_osabi (bfd_arch_rs6000
, 0, GDB_OSABI_LYNXOS178
,
420 rs6000_lynx178_init_osabi
);