1 /* Target-dependent code for PowerPC systems using the SVR4 ABI
2 for GDB, the GNU debugger.
4 Copyright (C) 2000, 2001, 2002, 2003, 2005, 2007, 2008
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "gdb_string.h"
28 #include "gdb_assert.h"
34 /* Pass the arguments in either registers, or in the stack. Using the
35 ppc sysv ABI, the first eight words of the argument list (that might
36 be less than eight parameters if some parameters occupy more than one
37 word) are passed in r3..r10 registers. float and double parameters are
38 passed in fpr's, in addition to that. Rest of the parameters if any
39 are passed in user stack.
41 If the function is returning a structure, then the return address is passed
42 in r3, then the first 7 words of the parametes can be passed in registers,
46 ppc_sysv_abi_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
47 struct regcache *regcache, CORE_ADDR bp_addr,
48 int nargs, struct value **args, CORE_ADDR sp,
49 int struct_return, CORE_ADDR struct_addr)
51 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
53 int argspace = 0; /* 0 is an initial wrong guess. */
56 gdb_assert (tdep->wordsize == 4);
58 regcache_cooked_read_unsigned (regcache, gdbarch_sp_regnum (gdbarch),
61 /* Go through the argument list twice.
63 Pass 1: Figure out how much new stack space is required for
64 arguments and pushed values. Unlike the PowerOpen ABI, the SysV
65 ABI doesn't reserve any extra space for parameters which are put
66 in registers, but does always push structures and then pass their
69 Pass 2: Replay the same computation but this time also write the
70 values out to the target. */
72 for (write_pass = 0; write_pass < 2; write_pass++)
75 /* Next available floating point register for float and double
78 /* Next available general register for non-float, non-vector
81 /* Next available vector register for vector arguments. */
83 /* Arguments start above the "LR save word" and "Back chain". */
84 int argoffset = 2 * tdep->wordsize;
85 /* Structures start after the arguments. */
86 int structoffset = argoffset + argspace;
88 /* If the function is returning a `struct', then the first word
89 (which will be passed in r3) is used for struct return
90 address. In that case we should advance one word and start
91 from r4 register to copy parameters. */
95 regcache_cooked_write_signed (regcache,
96 tdep->ppc_gp0_regnum + greg,
101 for (argno = 0; argno < nargs; argno++)
103 struct value *arg = args[argno];
104 struct type *type = check_typedef (value_type (arg));
105 int len = TYPE_LENGTH (type);
106 const bfd_byte *val = value_contents (arg);
108 if (TYPE_CODE (type) == TYPE_CODE_FLT && len <= 8
109 && !tdep->soft_float)
111 /* Floating point value converted to "double" then
112 passed in an FP register, when the registers run out,
113 8 byte aligned stack is used. */
118 /* Always store the floating point value using
119 the register's floating-point format. */
120 gdb_byte regval[MAX_REGISTER_SIZE];
122 = register_type (gdbarch, tdep->ppc_fp0_regnum + freg);
123 convert_typed_floating (val, type, regval, regtype);
124 regcache_cooked_write (regcache,
125 tdep->ppc_fp0_regnum + freg,
132 /* SysV ABI converts floats to doubles before
133 writing them to an 8 byte aligned stack location. */
134 argoffset = align_up (argoffset, 8);
138 convert_typed_floating (val, type, memval,
139 builtin_type_ieee_double);
140 write_memory (sp + argoffset, val, len);
145 else if (TYPE_CODE (type) == TYPE_CODE_FLT
148 && (gdbarch_long_double_format (gdbarch)
149 == floatformats_ibm_long_double))
151 /* IBM long double passed in two FP registers if
152 available, otherwise 8-byte aligned stack. */
157 regcache_cooked_write (regcache,
158 tdep->ppc_fp0_regnum + freg,
160 regcache_cooked_write (regcache,
161 tdep->ppc_fp0_regnum + freg + 1,
168 argoffset = align_up (argoffset, 8);
170 write_memory (sp + argoffset, val, len);
175 && (TYPE_CODE (type) == TYPE_CODE_INT /* long long */
176 || TYPE_CODE (type) == TYPE_CODE_FLT)) /* double */
178 /* "long long" or soft-float "double" passed in an odd/even
179 register pair with the low addressed word in the odd
180 register and the high addressed word in the even
181 register, or when the registers run out an 8 byte
182 aligned stack location. */
185 /* Just in case GREG was 10. */
187 argoffset = align_up (argoffset, 8);
189 write_memory (sp + argoffset, val, len);
194 /* Must start on an odd register - r3/r4 etc. */
199 regcache_cooked_write (regcache,
200 tdep->ppc_gp0_regnum + greg + 0,
202 regcache_cooked_write (regcache,
203 tdep->ppc_gp0_regnum + greg + 1,
209 else if (len == 16 && TYPE_CODE (type) == TYPE_CODE_FLT
210 && (gdbarch_long_double_format (gdbarch)
211 == floatformats_ibm_long_double))
213 /* Soft-float IBM long double passed in four consecutive
214 registers, or on the stack. The registers are not
215 necessarily odd/even pairs. */
219 argoffset = align_up (argoffset, 8);
221 write_memory (sp + argoffset, val, len);
228 regcache_cooked_write (regcache,
229 tdep->ppc_gp0_regnum + greg + 0,
231 regcache_cooked_write (regcache,
232 tdep->ppc_gp0_regnum + greg + 1,
234 regcache_cooked_write (regcache,
235 tdep->ppc_gp0_regnum + greg + 2,
237 regcache_cooked_write (regcache,
238 tdep->ppc_gp0_regnum + greg + 3,
244 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && len <= 8
245 && !tdep->soft_float)
247 /* 32-bit and 64-bit decimal floats go in f1 .. f8. They can
254 gdb_byte regval[MAX_REGISTER_SIZE];
257 /* 32-bit decimal floats are right aligned in the
259 if (TYPE_LENGTH (type) == 4)
261 memcpy (regval + 4, val, 4);
267 regcache_cooked_write (regcache,
268 tdep->ppc_fp0_regnum + freg, p);
275 argoffset = align_up (argoffset, len);
278 /* Write value in the stack's parameter save area. */
279 write_memory (sp + argoffset, val, len);
284 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && len == 16
285 && !tdep->soft_float)
287 /* 128-bit decimal floats go in f2 .. f7, always in even/odd
288 pairs. They can end up in memory, using two doublewords. */
292 /* Make sure freg is even. */
297 regcache_cooked_write (regcache,
298 tdep->ppc_fp0_regnum + freg, val);
299 regcache_cooked_write (regcache,
300 tdep->ppc_fp0_regnum + freg + 1, val + 8);
305 argoffset = align_up (argoffset, 8);
308 write_memory (sp + argoffset, val, 16);
313 /* If a 128-bit decimal float goes to the stack because only f7
314 and f8 are free (thus there's no even/odd register pair
315 available), these registers should be marked as occupied.
316 Hence we increase freg even when writing to memory. */
320 && TYPE_CODE (type) == TYPE_CODE_ARRAY
321 && TYPE_VECTOR (type)
322 && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
324 /* Vector parameter passed in an Altivec register, or
325 when that runs out, 16 byte aligned stack location. */
329 regcache_cooked_write (regcache,
330 tdep->ppc_vr0_regnum + vreg, val);
335 argoffset = align_up (argoffset, 16);
337 write_memory (sp + argoffset, val, 16);
342 && TYPE_CODE (type) == TYPE_CODE_ARRAY
343 && TYPE_VECTOR (type)
344 && tdep->vector_abi == POWERPC_VEC_SPE)
346 /* Vector parameter passed in an e500 register, or when
347 that runs out, 8 byte aligned stack location. Note
348 that since e500 vector and general purpose registers
349 both map onto the same underlying register set, a
350 "greg" and not a "vreg" is consumed here. A cooked
351 write stores the value in the correct locations
352 within the raw register cache. */
356 regcache_cooked_write (regcache,
357 tdep->ppc_ev0_regnum + greg, val);
362 argoffset = align_up (argoffset, 8);
364 write_memory (sp + argoffset, val, 8);
370 /* Reduce the parameter down to something that fits in a
372 gdb_byte word[MAX_REGISTER_SIZE];
373 memset (word, 0, MAX_REGISTER_SIZE);
374 if (len > tdep->wordsize
375 || TYPE_CODE (type) == TYPE_CODE_STRUCT
376 || TYPE_CODE (type) == TYPE_CODE_UNION)
378 /* Structs and large values are put in an
379 aligned stack slot ... */
380 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
381 && TYPE_VECTOR (type)
383 structoffset = align_up (structoffset, 16);
385 structoffset = align_up (structoffset, 8);
388 write_memory (sp + structoffset, val, len);
389 /* ... and then a "word" pointing to that address is
390 passed as the parameter. */
391 store_unsigned_integer (word, tdep->wordsize,
395 else if (TYPE_CODE (type) == TYPE_CODE_INT)
396 /* Sign or zero extend the "int" into a "word". */
397 store_unsigned_integer (word, tdep->wordsize,
398 unpack_long (type, val));
400 /* Always goes in the low address. */
401 memcpy (word, val, len);
402 /* Store that "word" in a register, or on the stack.
403 The words have "4" byte alignment. */
407 regcache_cooked_write (regcache,
408 tdep->ppc_gp0_regnum + greg, word);
413 argoffset = align_up (argoffset, tdep->wordsize);
415 write_memory (sp + argoffset, word, tdep->wordsize);
416 argoffset += tdep->wordsize;
421 /* Compute the actual stack space requirements. */
424 /* Remember the amount of space needed by the arguments. */
425 argspace = argoffset;
426 /* Allocate space for both the arguments and the structures. */
427 sp -= (argoffset + structoffset);
428 /* Ensure that the stack is still 16 byte aligned. */
429 sp = align_down (sp, 16);
432 /* The psABI says that "A caller of a function that takes a
433 variable argument list shall set condition register bit 6 to
434 1 if it passes one or more arguments in the floating-point
435 registers. It is strongly recommended that the caller set the
436 bit to 0 otherwise..." Doing this for normal functions too
442 regcache_cooked_read_unsigned (regcache, tdep->ppc_cr_regnum, &cr);
447 regcache_cooked_write_unsigned (regcache, tdep->ppc_cr_regnum, cr);
452 regcache_cooked_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
454 /* Write the backchain (it occupies WORDSIZED bytes). */
455 write_memory_signed_integer (sp, tdep->wordsize, saved_sp);
457 /* Point the inferior function call's return address at the dummy's
459 regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
464 /* Handle the return-value conventions for Decimal Floating Point values
465 in both ppc32 and ppc64, which are the same. */
467 get_decimal_float_return_value (struct gdbarch *gdbarch, struct type *valtype,
468 struct regcache *regcache, gdb_byte *readbuf,
469 const gdb_byte *writebuf)
471 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
473 gdb_assert (TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT);
475 /* 32-bit and 64-bit decimal floats in f1. */
476 if (TYPE_LENGTH (valtype) <= 8)
478 if (writebuf != NULL)
480 gdb_byte regval[MAX_REGISTER_SIZE];
483 /* 32-bit decimal float is right aligned in the doubleword. */
484 if (TYPE_LENGTH (valtype) == 4)
486 memcpy (regval + 4, writebuf, 4);
492 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, p);
496 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, readbuf);
498 /* Left align 32-bit decimal float. */
499 if (TYPE_LENGTH (valtype) == 4)
500 memcpy (readbuf, readbuf + 4, 4);
503 /* 128-bit decimal floats in f2,f3. */
504 else if (TYPE_LENGTH (valtype) == 16)
506 if (writebuf != NULL || readbuf != NULL)
510 for (i = 0; i < 2; i++)
512 if (writebuf != NULL)
513 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 2 + i,
516 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 2 + i,
523 internal_error (__FILE__, __LINE__, "Unknown decimal float size.");
525 return RETURN_VALUE_REGISTER_CONVENTION;
528 /* Handle the return-value conventions specified by the SysV 32-bit
529 PowerPC ABI (including all the supplements):
531 no floating-point: floating-point values returned using 32-bit
532 general-purpose registers.
534 Altivec: 128-bit vectors returned using vector registers.
536 e500: 64-bit vectors returned using the full full 64 bit EV
537 register, floating-point values returned using 32-bit
538 general-purpose registers.
540 GCC (broken): Small struct values right (instead of left) aligned
541 when returned in general-purpose registers. */
543 static enum return_value_convention
544 do_ppc_sysv_return_value (struct gdbarch *gdbarch, struct type *type,
545 struct regcache *regcache, gdb_byte *readbuf,
546 const gdb_byte *writebuf, int broken_gcc)
548 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
549 gdb_assert (tdep->wordsize == 4);
550 if (TYPE_CODE (type) == TYPE_CODE_FLT
551 && TYPE_LENGTH (type) <= 8
552 && !tdep->soft_float)
556 /* Floats and doubles stored in "f1". Convert the value to
557 the required type. */
558 gdb_byte regval[MAX_REGISTER_SIZE];
559 struct type *regtype = register_type (gdbarch,
560 tdep->ppc_fp0_regnum + 1);
561 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
562 convert_typed_floating (regval, regtype, readbuf, type);
566 /* Floats and doubles stored in "f1". Convert the value to
567 the register's "double" type. */
568 gdb_byte regval[MAX_REGISTER_SIZE];
569 struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
570 convert_typed_floating (writebuf, type, regval, regtype);
571 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
573 return RETURN_VALUE_REGISTER_CONVENTION;
575 if (TYPE_CODE (type) == TYPE_CODE_FLT
576 && TYPE_LENGTH (type) == 16
578 && (gdbarch_long_double_format (gdbarch) == floatformats_ibm_long_double))
580 /* IBM long double stored in f1 and f2. */
583 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, readbuf);
584 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 2,
589 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, writebuf);
590 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 2,
593 return RETURN_VALUE_REGISTER_CONVENTION;
595 if (TYPE_CODE (type) == TYPE_CODE_FLT
596 && TYPE_LENGTH (type) == 16
597 && (gdbarch_long_double_format (gdbarch) == floatformats_ibm_long_double))
599 /* Soft-float IBM long double stored in r3, r4, r5, r6. */
602 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, readbuf);
603 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
605 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 5,
607 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 6,
612 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
613 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
615 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 5,
617 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 6,
620 return RETURN_VALUE_REGISTER_CONVENTION;
622 if ((TYPE_CODE (type) == TYPE_CODE_INT && TYPE_LENGTH (type) == 8)
623 || (TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8))
627 /* A long long, or a double stored in the 32 bit r3/r4. */
628 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
630 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
635 /* A long long, or a double stored in the 32 bit r3/r4. */
636 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
638 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
641 return RETURN_VALUE_REGISTER_CONVENTION;
643 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && !tdep->soft_float)
644 return get_decimal_float_return_value (gdbarch, type, regcache, readbuf,
646 else if ((TYPE_CODE (type) == TYPE_CODE_INT
647 || TYPE_CODE (type) == TYPE_CODE_CHAR
648 || TYPE_CODE (type) == TYPE_CODE_BOOL
649 || TYPE_CODE (type) == TYPE_CODE_PTR
650 || TYPE_CODE (type) == TYPE_CODE_REF
651 || TYPE_CODE (type) == TYPE_CODE_ENUM)
652 && TYPE_LENGTH (type) <= tdep->wordsize)
656 /* Some sort of integer stored in r3. Since TYPE isn't
657 bigger than the register, sign extension isn't a problem
658 - just do everything unsigned. */
660 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
662 store_unsigned_integer (readbuf, TYPE_LENGTH (type), regval);
666 /* Some sort of integer stored in r3. Use unpack_long since
667 that should handle any required sign extension. */
668 regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
669 unpack_long (type, writebuf));
671 return RETURN_VALUE_REGISTER_CONVENTION;
673 if (TYPE_LENGTH (type) == 16
674 && TYPE_CODE (type) == TYPE_CODE_ARRAY
675 && TYPE_VECTOR (type)
676 && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
680 /* Altivec places the return value in "v2". */
681 regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
685 /* Altivec places the return value in "v2". */
686 regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
688 return RETURN_VALUE_REGISTER_CONVENTION;
690 if (TYPE_LENGTH (type) == 16
691 && TYPE_CODE (type) == TYPE_CODE_ARRAY
692 && TYPE_VECTOR (type)
693 && tdep->vector_abi == POWERPC_VEC_GENERIC)
695 /* GCC -maltivec -mabi=no-altivec returns vectors in r3/r4/r5/r6.
696 GCC without AltiVec returns them in memory, but it warns about
697 ABI risks in that case; we don't try to support it. */
700 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
702 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
704 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 5,
706 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 6,
711 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
713 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
715 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 5,
717 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 6,
720 return RETURN_VALUE_REGISTER_CONVENTION;
722 if (TYPE_LENGTH (type) == 8
723 && TYPE_CODE (type) == TYPE_CODE_ARRAY
724 && TYPE_VECTOR (type)
725 && tdep->vector_abi == POWERPC_VEC_SPE)
727 /* The e500 ABI places return values for the 64-bit DSP types
728 (__ev64_opaque__) in r3. However, in GDB-speak, ev3
729 corresponds to the entire r3 value for e500, whereas GDB's r3
730 only corresponds to the least significant 32-bits. So place
731 the 64-bit DSP type's value in ev3. */
733 regcache_cooked_read (regcache, tdep->ppc_ev0_regnum + 3, readbuf);
735 regcache_cooked_write (regcache, tdep->ppc_ev0_regnum + 3, writebuf);
736 return RETURN_VALUE_REGISTER_CONVENTION;
738 if (broken_gcc && TYPE_LENGTH (type) <= 8)
740 /* GCC screwed up for structures or unions whose size is less
741 than or equal to 8 bytes.. Instead of left-aligning, it
742 right-aligns the data into the buffer formed by r3, r4. */
743 gdb_byte regvals[MAX_REGISTER_SIZE * 2];
744 int len = TYPE_LENGTH (type);
745 int offset = (2 * tdep->wordsize - len) % tdep->wordsize;
749 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
750 regvals + 0 * tdep->wordsize);
751 if (len > tdep->wordsize)
752 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
753 regvals + 1 * tdep->wordsize);
754 memcpy (readbuf, regvals + offset, len);
758 memset (regvals, 0, sizeof regvals);
759 memcpy (regvals + offset, writebuf, len);
760 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
761 regvals + 0 * tdep->wordsize);
762 if (len > tdep->wordsize)
763 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
764 regvals + 1 * tdep->wordsize);
767 return RETURN_VALUE_REGISTER_CONVENTION;
769 if (TYPE_LENGTH (type) <= 8)
773 /* This matches SVr4 PPC, it does not match GCC. */
774 /* The value is right-padded to 8 bytes and then loaded, as
775 two "words", into r3/r4. */
776 gdb_byte regvals[MAX_REGISTER_SIZE * 2];
777 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
778 regvals + 0 * tdep->wordsize);
779 if (TYPE_LENGTH (type) > tdep->wordsize)
780 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
781 regvals + 1 * tdep->wordsize);
782 memcpy (readbuf, regvals, TYPE_LENGTH (type));
786 /* This matches SVr4 PPC, it does not match GCC. */
787 /* The value is padded out to 8 bytes and then loaded, as
788 two "words" into r3/r4. */
789 gdb_byte regvals[MAX_REGISTER_SIZE * 2];
790 memset (regvals, 0, sizeof regvals);
791 memcpy (regvals, writebuf, TYPE_LENGTH (type));
792 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
793 regvals + 0 * tdep->wordsize);
794 if (TYPE_LENGTH (type) > tdep->wordsize)
795 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
796 regvals + 1 * tdep->wordsize);
798 return RETURN_VALUE_REGISTER_CONVENTION;
800 return RETURN_VALUE_STRUCT_CONVENTION;
803 enum return_value_convention
804 ppc_sysv_abi_return_value (struct gdbarch *gdbarch, struct type *valtype,
805 struct regcache *regcache, gdb_byte *readbuf,
806 const gdb_byte *writebuf)
808 return do_ppc_sysv_return_value (gdbarch, valtype, regcache, readbuf,
812 enum return_value_convention
813 ppc_sysv_abi_broken_return_value (struct gdbarch *gdbarch,
814 struct type *valtype,
815 struct regcache *regcache,
816 gdb_byte *readbuf, const gdb_byte *writebuf)
818 return do_ppc_sysv_return_value (gdbarch, valtype, regcache, readbuf,
822 /* The helper function for 64-bit SYSV push_dummy_call. Converts the
823 function's code address back into the function's descriptor
826 Find a value for the TOC register. Every symbol should have both
827 ".FN" and "FN" in the minimal symbol table. "FN" points at the
828 FN's descriptor, while ".FN" points at the entry point (which
829 matches FUNC_ADDR). Need to reverse from FUNC_ADDR back to the
830 FN's descriptor address (while at the same time being careful to
831 find "FN" in the same object file as ".FN"). */
834 convert_code_addr_to_desc_addr (CORE_ADDR code_addr, CORE_ADDR *desc_addr)
836 struct obj_section *dot_fn_section;
837 struct minimal_symbol *dot_fn;
838 struct minimal_symbol *fn;
840 /* Find the minimal symbol that corresponds to CODE_ADDR (should
841 have a name of the form ".FN"). */
842 dot_fn = lookup_minimal_symbol_by_pc (code_addr);
843 if (dot_fn == NULL || SYMBOL_LINKAGE_NAME (dot_fn)[0] != '.')
845 /* Get the section that contains CODE_ADDR. Need this for the
846 "objfile" that it contains. */
847 dot_fn_section = find_pc_section (code_addr);
848 if (dot_fn_section == NULL || dot_fn_section->objfile == NULL)
850 /* Now find the corresponding "FN" (dropping ".") minimal symbol's
851 address. Only look for the minimal symbol in ".FN"'s object file
852 - avoids problems when two object files (i.e., shared libraries)
853 contain a minimal symbol with the same name. */
854 fn = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (dot_fn) + 1, NULL,
855 dot_fn_section->objfile);
858 /* Found a descriptor. */
859 (*desc_addr) = SYMBOL_VALUE_ADDRESS (fn);
863 /* Pass the arguments in either registers, or in the stack. Using the
866 This implements a dumbed down version of the ABI. It always writes
867 values to memory, GPR and FPR, even when not necessary. Doing this
868 greatly simplifies the logic. */
871 ppc64_sysv_abi_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
872 struct regcache *regcache, CORE_ADDR bp_addr,
873 int nargs, struct value **args, CORE_ADDR sp,
874 int struct_return, CORE_ADDR struct_addr)
876 CORE_ADDR func_addr = find_function_addr (function, NULL);
877 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
879 /* See for-loop comment below. */
881 /* Size of the Altivec's vector parameter region, the final value is
882 computed in the for-loop below. */
883 LONGEST vparam_size = 0;
884 /* Size of the general parameter region, the final value is computed
885 in the for-loop below. */
886 LONGEST gparam_size = 0;
887 /* Kevin writes ... I don't mind seeing tdep->wordsize used in the
888 calls to align_up(), align_down(), etc. because this makes it
889 easier to reuse this code (in a copy/paste sense) in the future,
890 but it is a 64-bit ABI and asserting that the wordsize is 8 bytes
891 at some point makes it easier to verify that this function is
892 correct without having to do a non-local analysis to figure out
893 the possible values of tdep->wordsize. */
894 gdb_assert (tdep->wordsize == 8);
896 /* This function exists to support a calling convention that
897 requires floating-point registers. It shouldn't be used on
898 processors that lack them. */
899 gdb_assert (ppc_floating_point_unit_p (gdbarch));
901 /* By this stage in the proceedings, SP has been decremented by "red
902 zone size" + "struct return size". Fetch the stack-pointer from
903 before this and use that as the BACK_CHAIN. */
904 regcache_cooked_read_unsigned (regcache, gdbarch_sp_regnum (gdbarch),
907 /* Go through the argument list twice.
909 Pass 1: Compute the function call's stack space and register
912 Pass 2: Replay the same computation but this time also write the
913 values out to the target. */
915 for (write_pass = 0; write_pass < 2; write_pass++)
918 /* Next available floating point register for float and double
921 /* Next available general register for non-vector (but possibly
924 /* Next available vector register for vector arguments. */
926 /* The address, at which the next general purpose parameter
927 (integer, struct, float, ...) should be saved. */
929 /* Address, at which the next Altivec vector parameter should be
935 /* During the first pass, GPARAM and VPARAM are more like
936 offsets (start address zero) than addresses. That way
937 the accumulate the total stack space each region
944 /* Decrement the stack pointer making space for the Altivec
945 and general on-stack parameters. Set vparam and gparam
946 to their corresponding regions. */
947 vparam = align_down (sp - vparam_size, 16);
948 gparam = align_down (vparam - gparam_size, 16);
949 /* Add in space for the TOC, link editor double word,
950 compiler double word, LR save area, CR save area. */
951 sp = align_down (gparam - 48, 16);
954 /* If the function is returning a `struct', then there is an
955 extra hidden parameter (which will be passed in r3)
956 containing the address of that struct.. In that case we
957 should advance one word and start from r4 register to copy
958 parameters. This also consumes one on-stack parameter slot. */
962 regcache_cooked_write_signed (regcache,
963 tdep->ppc_gp0_regnum + greg,
966 gparam = align_up (gparam + tdep->wordsize, tdep->wordsize);
969 for (argno = 0; argno < nargs; argno++)
971 struct value *arg = args[argno];
972 struct type *type = check_typedef (value_type (arg));
973 const bfd_byte *val = value_contents (arg);
975 if (TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) <= 8)
977 /* Floats and Doubles go in f1 .. f13. They also
978 consume a left aligned GREG,, and can end up in
982 gdb_byte regval[MAX_REGISTER_SIZE];
985 /* Version 1.7 of the 64-bit PowerPC ELF ABI says:
987 "Single precision floating point values are mapped to
988 the first word in a single doubleword."
990 And version 1.9 says:
992 "Single precision floating point values are mapped to
993 the second word in a single doubleword."
995 GDB then writes single precision floating point values
996 at both words in a doubleword, to support both ABIs. */
997 if (TYPE_LENGTH (type) == 4)
999 memcpy (regval, val, 4);
1000 memcpy (regval + 4, val, 4);
1006 /* Write value in the stack's parameter save area. */
1007 write_memory (gparam, p, 8);
1011 struct type *regtype
1012 = register_type (gdbarch, tdep->ppc_fp0_regnum);
1014 convert_typed_floating (val, type, regval, regtype);
1015 regcache_cooked_write (regcache,
1016 tdep->ppc_fp0_regnum + freg,
1020 regcache_cooked_write (regcache,
1021 tdep->ppc_gp0_regnum + greg,
1027 /* Always consume parameter stack space. */
1028 gparam = align_up (gparam + 8, tdep->wordsize);
1030 else if (TYPE_CODE (type) == TYPE_CODE_FLT
1031 && TYPE_LENGTH (type) == 16
1032 && (gdbarch_long_double_format (gdbarch)
1033 == floatformats_ibm_long_double))
1035 /* IBM long double stored in two doublewords of the
1036 parameter save area and corresponding registers. */
1039 if (!tdep->soft_float && freg <= 13)
1041 regcache_cooked_write (regcache,
1042 tdep->ppc_fp0_regnum + freg,
1045 regcache_cooked_write (regcache,
1046 tdep->ppc_fp0_regnum + freg + 1,
1051 regcache_cooked_write (regcache,
1052 tdep->ppc_gp0_regnum + greg,
1055 regcache_cooked_write (regcache,
1056 tdep->ppc_gp0_regnum + greg + 1,
1059 write_memory (gparam, val, TYPE_LENGTH (type));
1063 gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize);
1065 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT
1066 && TYPE_LENGTH (type) <= 8)
1068 /* 32-bit and 64-bit decimal floats go in f1 .. f13. They can
1069 end up in memory. */
1072 gdb_byte regval[MAX_REGISTER_SIZE];
1075 /* 32-bit decimal floats are right aligned in the
1077 if (TYPE_LENGTH (type) == 4)
1079 memcpy (regval + 4, val, 4);
1085 /* Write value in the stack's parameter save area. */
1086 write_memory (gparam, p, 8);
1089 regcache_cooked_write (regcache,
1090 tdep->ppc_fp0_regnum + freg, p);
1095 /* Always consume parameter stack space. */
1096 gparam = align_up (gparam + 8, tdep->wordsize);
1098 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT &&
1099 TYPE_LENGTH (type) == 16)
1101 /* 128-bit decimal floats go in f2 .. f12, always in even/odd
1102 pairs. They can end up in memory, using two doublewords. */
1107 /* Make sure freg is even. */
1109 regcache_cooked_write (regcache,
1110 tdep->ppc_fp0_regnum + freg, val);
1111 regcache_cooked_write (regcache,
1112 tdep->ppc_fp0_regnum + freg + 1, val + 8);
1115 write_memory (gparam, val, TYPE_LENGTH (type));
1120 gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize);
1122 else if (TYPE_LENGTH (type) == 16 && TYPE_VECTOR (type)
1123 && TYPE_CODE (type) == TYPE_CODE_ARRAY
1124 && tdep->ppc_vr0_regnum >= 0)
1126 /* In the Altivec ABI, vectors go in the vector
1127 registers v2 .. v13, or when that runs out, a vector
1128 annex which goes above all the normal parameters.
1129 NOTE: cagney/2003-09-21: This is a guess based on the
1130 PowerOpen Altivec ABI. */
1134 regcache_cooked_write (regcache,
1135 tdep->ppc_vr0_regnum + vreg, val);
1141 write_memory (vparam, val, TYPE_LENGTH (type));
1142 vparam = align_up (vparam + TYPE_LENGTH (type), 16);
1145 else if ((TYPE_CODE (type) == TYPE_CODE_INT
1146 || TYPE_CODE (type) == TYPE_CODE_ENUM
1147 || TYPE_CODE (type) == TYPE_CODE_PTR)
1148 && TYPE_LENGTH (type) <= 8)
1150 /* Scalars and Pointers get sign[un]extended and go in
1151 gpr3 .. gpr10. They can also end up in memory. */
1154 /* Sign extend the value, then store it unsigned. */
1155 ULONGEST word = unpack_long (type, val);
1156 /* Convert any function code addresses into
1158 if (TYPE_CODE (type) == TYPE_CODE_PTR
1159 && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC)
1161 CORE_ADDR desc = word;
1162 convert_code_addr_to_desc_addr (word, &desc);
1166 regcache_cooked_write_unsigned (regcache,
1167 tdep->ppc_gp0_regnum +
1169 write_memory_unsigned_integer (gparam, tdep->wordsize,
1173 gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize);
1178 for (byte = 0; byte < TYPE_LENGTH (type);
1179 byte += tdep->wordsize)
1181 if (write_pass && greg <= 10)
1183 gdb_byte regval[MAX_REGISTER_SIZE];
1184 int len = TYPE_LENGTH (type) - byte;
1185 if (len > tdep->wordsize)
1186 len = tdep->wordsize;
1187 memset (regval, 0, sizeof regval);
1188 /* The ABI (version 1.9) specifies that values
1189 smaller than one doubleword are right-aligned
1190 and those larger are left-aligned. GCC
1191 versions before 3.4 implemented this
1193 <http://gcc.gnu.org/gcc-3.4/powerpc-abi.html>. */
1195 memcpy (regval + tdep->wordsize - len,
1198 memcpy (regval, val + byte, len);
1199 regcache_cooked_write (regcache, greg, regval);
1204 /* WARNING: cagney/2003-09-21: Strictly speaking, this
1205 isn't necessary, unfortunately, GCC appears to get
1206 "struct convention" parameter passing wrong putting
1207 odd sized structures in memory instead of in a
1208 register. Work around this by always writing the
1209 value to memory. Fortunately, doing this
1210 simplifies the code. */
1211 write_memory (gparam, val, TYPE_LENGTH (type));
1213 && TYPE_CODE (type) == TYPE_CODE_STRUCT
1214 && TYPE_NFIELDS (type) == 1
1215 && TYPE_LENGTH (type) <= 16)
1217 /* The ABI (version 1.9) specifies that structs
1218 containing a single floating-point value, at any
1219 level of nesting of single-member structs, are
1220 passed in floating-point registers. */
1221 while (TYPE_CODE (type) == TYPE_CODE_STRUCT
1222 && TYPE_NFIELDS (type) == 1)
1223 type = check_typedef (TYPE_FIELD_TYPE (type, 0));
1224 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1226 if (TYPE_LENGTH (type) <= 8)
1230 gdb_byte regval[MAX_REGISTER_SIZE];
1231 struct type *regtype
1232 = register_type (gdbarch,
1233 tdep->ppc_fp0_regnum);
1234 convert_typed_floating (val, type, regval,
1236 regcache_cooked_write (regcache,
1237 (tdep->ppc_fp0_regnum
1243 else if (TYPE_LENGTH (type) == 16
1244 && (gdbarch_long_double_format (gdbarch)
1245 == floatformats_ibm_long_double))
1249 regcache_cooked_write (regcache,
1250 (tdep->ppc_fp0_regnum
1254 regcache_cooked_write (regcache,
1255 (tdep->ppc_fp0_regnum
1263 /* Always consume parameter stack space. */
1264 gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize);
1270 /* Save the true region sizes ready for the second pass. */
1271 vparam_size = vparam;
1272 /* Make certain that the general parameter save area is at
1273 least the minimum 8 registers (or doublewords) in size. */
1275 gparam_size = 8 * tdep->wordsize;
1277 gparam_size = gparam;
1282 regcache_cooked_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
1284 /* Write the backchain (it occupies WORDSIZED bytes). */
1285 write_memory_signed_integer (sp, tdep->wordsize, back_chain);
1287 /* Point the inferior function call's return address at the dummy's
1289 regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
1291 /* Use the func_addr to find the descriptor, and use that to find
1294 CORE_ADDR desc_addr;
1295 if (convert_code_addr_to_desc_addr (func_addr, &desc_addr))
1297 /* The TOC is the second double word in the descriptor. */
1299 read_memory_unsigned_integer (desc_addr + tdep->wordsize,
1301 regcache_cooked_write_unsigned (regcache,
1302 tdep->ppc_gp0_regnum + 2, toc);
1310 /* The 64 bit ABI return value convention.
1312 Return non-zero if the return-value is stored in a register, return
1313 0 if the return-value is instead stored on the stack (a.k.a.,
1314 struct return convention).
1316 For a return-value stored in a register: when WRITEBUF is non-NULL,
1317 copy the buffer to the corresponding register return-value location
1318 location; when READBUF is non-NULL, fill the buffer from the
1319 corresponding register return-value location. */
1320 enum return_value_convention
1321 ppc64_sysv_abi_return_value (struct gdbarch *gdbarch, struct type *valtype,
1322 struct regcache *regcache, gdb_byte *readbuf,
1323 const gdb_byte *writebuf)
1325 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1327 /* This function exists to support a calling convention that
1328 requires floating-point registers. It shouldn't be used on
1329 processors that lack them. */
1330 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1332 /* Floats and doubles in F1. */
1333 if (TYPE_CODE (valtype) == TYPE_CODE_FLT && TYPE_LENGTH (valtype) <= 8)
1335 gdb_byte regval[MAX_REGISTER_SIZE];
1336 struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
1337 if (writebuf != NULL)
1339 convert_typed_floating (writebuf, valtype, regval, regtype);
1340 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
1342 if (readbuf != NULL)
1344 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
1345 convert_typed_floating (regval, regtype, readbuf, valtype);
1347 return RETURN_VALUE_REGISTER_CONVENTION;
1349 if (TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT)
1350 return get_decimal_float_return_value (gdbarch, valtype, regcache, readbuf,
1352 /* Integers in r3. */
1353 if ((TYPE_CODE (valtype) == TYPE_CODE_INT
1354 || TYPE_CODE (valtype) == TYPE_CODE_ENUM)
1355 && TYPE_LENGTH (valtype) <= 8)
1357 if (writebuf != NULL)
1359 /* Be careful to sign extend the value. */
1360 regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
1361 unpack_long (valtype, writebuf));
1363 if (readbuf != NULL)
1365 /* Extract the integer from r3. Since this is truncating the
1366 value, there isn't a sign extension problem. */
1368 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
1370 store_unsigned_integer (readbuf, TYPE_LENGTH (valtype), regval);
1372 return RETURN_VALUE_REGISTER_CONVENTION;
1374 /* All pointers live in r3. */
1375 if (TYPE_CODE (valtype) == TYPE_CODE_PTR)
1377 /* All pointers live in r3. */
1378 if (writebuf != NULL)
1379 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
1380 if (readbuf != NULL)
1381 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, readbuf);
1382 return RETURN_VALUE_REGISTER_CONVENTION;
1384 /* Array type has more than one use. */
1385 if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
1387 /* Small character arrays are returned, right justified, in r3. */
1388 if (TYPE_LENGTH (valtype) <= 8
1389 && TYPE_CODE (TYPE_TARGET_TYPE (valtype)) == TYPE_CODE_INT
1390 && TYPE_LENGTH (TYPE_TARGET_TYPE (valtype)) == 1)
1392 int offset = (register_size (gdbarch, tdep->ppc_gp0_regnum + 3)
1393 - TYPE_LENGTH (valtype));
1394 if (writebuf != NULL)
1395 regcache_cooked_write_part (regcache, tdep->ppc_gp0_regnum + 3,
1396 offset, TYPE_LENGTH (valtype), writebuf);
1397 if (readbuf != NULL)
1398 regcache_cooked_read_part (regcache, tdep->ppc_gp0_regnum + 3,
1399 offset, TYPE_LENGTH (valtype), readbuf);
1400 return RETURN_VALUE_REGISTER_CONVENTION;
1402 /* A VMX vector is returned in v2. */
1403 if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY
1404 && TYPE_VECTOR (valtype) && tdep->ppc_vr0_regnum >= 0)
1407 regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
1409 regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
1410 return RETURN_VALUE_REGISTER_CONVENTION;
1413 /* Big floating point values get stored in adjacent floating
1414 point registers, starting with F1. */
1415 if (TYPE_CODE (valtype) == TYPE_CODE_FLT
1416 && (TYPE_LENGTH (valtype) == 16 || TYPE_LENGTH (valtype) == 32))
1418 if (writebuf || readbuf != NULL)
1421 for (i = 0; i < TYPE_LENGTH (valtype) / 8; i++)
1423 if (writebuf != NULL)
1424 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1 + i,
1425 (const bfd_byte *) writebuf + i * 8);
1426 if (readbuf != NULL)
1427 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1 + i,
1428 (bfd_byte *) readbuf + i * 8);
1431 return RETURN_VALUE_REGISTER_CONVENTION;
1433 /* Complex values get returned in f1:f2, need to convert. */
1434 if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX
1435 && (TYPE_LENGTH (valtype) == 8 || TYPE_LENGTH (valtype) == 16))
1437 if (regcache != NULL)
1440 for (i = 0; i < 2; i++)
1442 gdb_byte regval[MAX_REGISTER_SIZE];
1443 struct type *regtype =
1444 register_type (gdbarch, tdep->ppc_fp0_regnum);
1445 if (writebuf != NULL)
1447 convert_typed_floating ((const bfd_byte *) writebuf +
1448 i * (TYPE_LENGTH (valtype) / 2),
1449 valtype, regval, regtype);
1450 regcache_cooked_write (regcache,
1451 tdep->ppc_fp0_regnum + 1 + i,
1454 if (readbuf != NULL)
1456 regcache_cooked_read (regcache,
1457 tdep->ppc_fp0_regnum + 1 + i,
1459 convert_typed_floating (regval, regtype,
1460 (bfd_byte *) readbuf +
1461 i * (TYPE_LENGTH (valtype) / 2),
1466 return RETURN_VALUE_REGISTER_CONVENTION;
1468 /* Big complex values get stored in f1:f4. */
1469 if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX && TYPE_LENGTH (valtype) == 32)
1471 if (regcache != NULL)
1474 for (i = 0; i < 4; i++)
1476 if (writebuf != NULL)
1477 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1 + i,
1478 (const bfd_byte *) writebuf + i * 8);
1479 if (readbuf != NULL)
1480 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1 + i,
1481 (bfd_byte *) readbuf + i * 8);
1484 return RETURN_VALUE_REGISTER_CONVENTION;
1486 return RETURN_VALUE_STRUCT_CONVENTION;
1490 ppc64_sysv_abi_adjust_breakpoint_address (struct gdbarch *gdbarch,
1493 /* PPC64 SYSV specifies that the minimal-symbol "FN" should point at
1494 a function-descriptor while the corresponding minimal-symbol
1495 ".FN" should point at the entry point. Consequently, a command
1496 like "break FN" applied to an object file with only minimal
1497 symbols, will insert the breakpoint into the descriptor at "FN"
1498 and not the function at ".FN". Avoid this confusion by adjusting
1499 any attempt to set a descriptor breakpoint into a corresponding
1500 function breakpoint. Note that GDB warns the user when this
1501 adjustment is applied - that's ok as otherwise the user will have
1502 no way of knowing why their breakpoint at "FN" resulted in the
1503 program stopping at ".FN". */
1504 return gdbarch_convert_from_func_ptr_addr (gdbarch, bpaddr, ¤t_target);