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
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 (current_gdbarch);
53 int argspace = 0; /* 0 is an initial wrong guess. */
56 gdb_assert (tdep->wordsize == 4);
58 regcache_cooked_read_unsigned (regcache,
59 gdbarch_sp_regnum (current_gdbarch),
62 /* Go through the argument list twice.
64 Pass 1: Figure out how much new stack space is required for
65 arguments and pushed values. Unlike the PowerOpen ABI, the SysV
66 ABI doesn't reserve any extra space for parameters which are put
67 in registers, but does always push structures and then pass their
70 Pass 2: Replay the same computation but this time also write the
71 values out to the target. */
73 for (write_pass = 0; write_pass < 2; write_pass++)
76 /* Next available floating point register for float and double
79 /* Next available general register for non-float, non-vector
82 /* Next available vector register for vector arguments. */
84 /* Arguments start above the "LR save word" and "Back chain". */
85 int argoffset = 2 * tdep->wordsize;
86 /* Structures start after the arguments. */
87 int structoffset = argoffset + argspace;
89 /* If the function is returning a `struct', then the first word
90 (which will be passed in r3) is used for struct return
91 address. In that case we should advance one word and start
92 from r4 register to copy parameters. */
96 regcache_cooked_write_signed (regcache,
97 tdep->ppc_gp0_regnum + greg,
102 for (argno = 0; argno < nargs; argno++)
104 struct value *arg = args[argno];
105 struct type *type = check_typedef (value_type (arg));
106 int len = TYPE_LENGTH (type);
107 const bfd_byte *val = value_contents (arg);
109 if (TYPE_CODE (type) == TYPE_CODE_FLT && len <= 8
110 && !tdep->soft_float)
112 /* Floating point value converted to "double" then
113 passed in an FP register, when the registers run out,
114 8 byte aligned stack is used. */
119 /* Always store the floating point value using
120 the register's floating-point format. */
121 gdb_byte regval[MAX_REGISTER_SIZE];
123 = register_type (gdbarch, tdep->ppc_fp0_regnum + freg);
124 convert_typed_floating (val, type, regval, regtype);
125 regcache_cooked_write (regcache,
126 tdep->ppc_fp0_regnum + freg,
133 /* SysV ABI converts floats to doubles before
134 writing them to an 8 byte aligned stack location. */
135 argoffset = align_up (argoffset, 8);
139 convert_typed_floating (val, type, memval,
140 builtin_type_ieee_double);
141 write_memory (sp + argoffset, val, len);
146 else if (TYPE_CODE (type) == TYPE_CODE_FLT
149 && (gdbarch_long_double_format (current_gdbarch)
150 == floatformats_ibm_long_double))
152 /* IBM long double passed in two FP registers if
153 available, otherwise 8-byte aligned stack. */
158 regcache_cooked_write (regcache,
159 tdep->ppc_fp0_regnum + freg,
161 regcache_cooked_write (regcache,
162 tdep->ppc_fp0_regnum + freg + 1,
169 argoffset = align_up (argoffset, 8);
171 write_memory (sp + argoffset, val, len);
176 && (TYPE_CODE (type) == TYPE_CODE_INT /* long long */
177 || TYPE_CODE (type) == TYPE_CODE_FLT)) /* double */
179 /* "long long" or soft-float "double" passed in an odd/even
180 register pair with the low addressed word in the odd
181 register and the high addressed word in the even
182 register, or when the registers run out an 8 byte
183 aligned stack location. */
186 /* Just in case GREG was 10. */
188 argoffset = align_up (argoffset, 8);
190 write_memory (sp + argoffset, val, len);
195 /* Must start on an odd register - r3/r4 etc. */
200 regcache_cooked_write (regcache,
201 tdep->ppc_gp0_regnum + greg + 0,
203 regcache_cooked_write (regcache,
204 tdep->ppc_gp0_regnum + greg + 1,
210 else if (len == 16 && TYPE_CODE (type) == TYPE_CODE_FLT
211 && (gdbarch_long_double_format (current_gdbarch)
212 == floatformats_ibm_long_double))
214 /* Soft-float IBM long double passed in four consecutive
215 registers, or on the stack. The registers are not
216 necessarily odd/even pairs. */
220 argoffset = align_up (argoffset, 8);
222 write_memory (sp + argoffset, val, len);
229 regcache_cooked_write (regcache,
230 tdep->ppc_gp0_regnum + greg + 0,
232 regcache_cooked_write (regcache,
233 tdep->ppc_gp0_regnum + greg + 1,
235 regcache_cooked_write (regcache,
236 tdep->ppc_gp0_regnum + greg + 2,
238 regcache_cooked_write (regcache,
239 tdep->ppc_gp0_regnum + greg + 3,
246 && TYPE_CODE (type) == TYPE_CODE_ARRAY
247 && TYPE_VECTOR (type)
248 && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
250 /* Vector parameter passed in an Altivec register, or
251 when that runs out, 16 byte aligned stack location. */
255 regcache_cooked_write (regcache,
256 tdep->ppc_vr0_regnum + vreg, val);
261 argoffset = align_up (argoffset, 16);
263 write_memory (sp + argoffset, val, 16);
268 && TYPE_CODE (type) == TYPE_CODE_ARRAY
269 && TYPE_VECTOR (type)
270 && tdep->vector_abi == POWERPC_VEC_SPE)
272 /* Vector parameter passed in an e500 register, or when
273 that runs out, 8 byte aligned stack location. Note
274 that since e500 vector and general purpose registers
275 both map onto the same underlying register set, a
276 "greg" and not a "vreg" is consumed here. A cooked
277 write stores the value in the correct locations
278 within the raw register cache. */
282 regcache_cooked_write (regcache,
283 tdep->ppc_ev0_regnum + greg, val);
288 argoffset = align_up (argoffset, 8);
290 write_memory (sp + argoffset, val, 8);
296 /* Reduce the parameter down to something that fits in a
298 gdb_byte word[MAX_REGISTER_SIZE];
299 memset (word, 0, MAX_REGISTER_SIZE);
300 if (len > tdep->wordsize
301 || TYPE_CODE (type) == TYPE_CODE_STRUCT
302 || TYPE_CODE (type) == TYPE_CODE_UNION)
304 /* Structs and large values are put in an
305 aligned stack slot ... */
306 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
307 && TYPE_VECTOR (type)
309 structoffset = align_up (structoffset, 16);
311 structoffset = align_up (structoffset, 8);
314 write_memory (sp + structoffset, val, len);
315 /* ... and then a "word" pointing to that address is
316 passed as the parameter. */
317 store_unsigned_integer (word, tdep->wordsize,
321 else if (TYPE_CODE (type) == TYPE_CODE_INT)
322 /* Sign or zero extend the "int" into a "word". */
323 store_unsigned_integer (word, tdep->wordsize,
324 unpack_long (type, val));
326 /* Always goes in the low address. */
327 memcpy (word, val, len);
328 /* Store that "word" in a register, or on the stack.
329 The words have "4" byte alignment. */
333 regcache_cooked_write (regcache,
334 tdep->ppc_gp0_regnum + greg, word);
339 argoffset = align_up (argoffset, tdep->wordsize);
341 write_memory (sp + argoffset, word, tdep->wordsize);
342 argoffset += tdep->wordsize;
347 /* Compute the actual stack space requirements. */
350 /* Remember the amount of space needed by the arguments. */
351 argspace = argoffset;
352 /* Allocate space for both the arguments and the structures. */
353 sp -= (argoffset + structoffset);
354 /* Ensure that the stack is still 16 byte aligned. */
355 sp = align_down (sp, 16);
358 /* The psABI says that "A caller of a function that takes a
359 variable argument list shall set condition register bit 6 to
360 1 if it passes one or more arguments in the floating-point
361 registers. It is strongly recommended that the caller set the
362 bit to 0 otherwise..." Doing this for normal functions too
368 regcache_cooked_read_unsigned (regcache, tdep->ppc_cr_regnum, &cr);
373 regcache_cooked_write_unsigned (regcache, tdep->ppc_cr_regnum, cr);
378 regcache_cooked_write_signed (regcache,
379 gdbarch_sp_regnum (current_gdbarch), sp);
381 /* Write the backchain (it occupies WORDSIZED bytes). */
382 write_memory_signed_integer (sp, tdep->wordsize, saved_sp);
384 /* Point the inferior function call's return address at the dummy's
386 regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
391 /* Handle the return-value conventions specified by the SysV 32-bit
392 PowerPC ABI (including all the supplements):
394 no floating-point: floating-point values returned using 32-bit
395 general-purpose registers.
397 Altivec: 128-bit vectors returned using vector registers.
399 e500: 64-bit vectors returned using the full full 64 bit EV
400 register, floating-point values returned using 32-bit
401 general-purpose registers.
403 GCC (broken): Small struct values right (instead of left) aligned
404 when returned in general-purpose registers. */
406 static enum return_value_convention
407 do_ppc_sysv_return_value (struct gdbarch *gdbarch, struct type *type,
408 struct regcache *regcache, gdb_byte *readbuf,
409 const gdb_byte *writebuf, int broken_gcc)
411 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
412 gdb_assert (tdep->wordsize == 4);
413 if (TYPE_CODE (type) == TYPE_CODE_FLT
414 && TYPE_LENGTH (type) <= 8
415 && !tdep->soft_float)
419 /* Floats and doubles stored in "f1". Convert the value to
420 the required type. */
421 gdb_byte regval[MAX_REGISTER_SIZE];
422 struct type *regtype = register_type (gdbarch,
423 tdep->ppc_fp0_regnum + 1);
424 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
425 convert_typed_floating (regval, regtype, readbuf, type);
429 /* Floats and doubles stored in "f1". Convert the value to
430 the register's "double" type. */
431 gdb_byte regval[MAX_REGISTER_SIZE];
432 struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
433 convert_typed_floating (writebuf, type, regval, regtype);
434 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
436 return RETURN_VALUE_REGISTER_CONVENTION;
438 if (TYPE_CODE (type) == TYPE_CODE_FLT
439 && TYPE_LENGTH (type) == 16
441 && (gdbarch_long_double_format (current_gdbarch)
442 == floatformats_ibm_long_double))
444 /* IBM long double stored in f1 and f2. */
447 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, readbuf);
448 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 2,
453 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, writebuf);
454 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 2,
457 return RETURN_VALUE_REGISTER_CONVENTION;
459 if (TYPE_CODE (type) == TYPE_CODE_FLT
460 && TYPE_LENGTH (type) == 16
461 && (gdbarch_long_double_format (current_gdbarch)
462 == floatformats_ibm_long_double))
464 /* Soft-float IBM long double stored in r3, r4, r5, r6. */
467 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, readbuf);
468 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
470 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 5,
472 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 6,
477 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
478 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
480 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 5,
482 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 6,
485 return RETURN_VALUE_REGISTER_CONVENTION;
487 if ((TYPE_CODE (type) == TYPE_CODE_INT && TYPE_LENGTH (type) == 8)
488 || (TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8))
492 /* A long long, or a double stored in the 32 bit r3/r4. */
493 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
495 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
500 /* A long long, or a double stored in the 32 bit r3/r4. */
501 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
503 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
506 return RETURN_VALUE_REGISTER_CONVENTION;
508 else if ((TYPE_CODE (type) == TYPE_CODE_INT
509 || TYPE_CODE (type) == TYPE_CODE_CHAR
510 || TYPE_CODE (type) == TYPE_CODE_BOOL
511 || TYPE_CODE (type) == TYPE_CODE_PTR
512 || TYPE_CODE (type) == TYPE_CODE_REF
513 || TYPE_CODE (type) == TYPE_CODE_ENUM)
514 && TYPE_LENGTH (type) <= tdep->wordsize)
518 /* Some sort of integer stored in r3. Since TYPE isn't
519 bigger than the register, sign extension isn't a problem
520 - just do everything unsigned. */
522 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
524 store_unsigned_integer (readbuf, TYPE_LENGTH (type), regval);
528 /* Some sort of integer stored in r3. Use unpack_long since
529 that should handle any required sign extension. */
530 regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
531 unpack_long (type, writebuf));
533 return RETURN_VALUE_REGISTER_CONVENTION;
535 if (TYPE_LENGTH (type) == 16
536 && TYPE_CODE (type) == TYPE_CODE_ARRAY
537 && TYPE_VECTOR (type)
538 && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
542 /* Altivec places the return value in "v2". */
543 regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
547 /* Altivec places the return value in "v2". */
548 regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
550 return RETURN_VALUE_REGISTER_CONVENTION;
552 if (TYPE_LENGTH (type) == 16
553 && TYPE_CODE (type) == TYPE_CODE_ARRAY
554 && TYPE_VECTOR (type)
555 && tdep->vector_abi == POWERPC_VEC_GENERIC)
557 /* GCC -maltivec -mabi=no-altivec returns vectors in r3/r4/r5/r6.
558 GCC without AltiVec returns them in memory, but it warns about
559 ABI risks in that case; we don't try to support it. */
562 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
564 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
566 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 5,
568 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 6,
573 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
575 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
577 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 5,
579 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 6,
582 return RETURN_VALUE_REGISTER_CONVENTION;
584 if (TYPE_LENGTH (type) == 8
585 && TYPE_CODE (type) == TYPE_CODE_ARRAY
586 && TYPE_VECTOR (type)
587 && tdep->vector_abi == POWERPC_VEC_SPE)
589 /* The e500 ABI places return values for the 64-bit DSP types
590 (__ev64_opaque__) in r3. However, in GDB-speak, ev3
591 corresponds to the entire r3 value for e500, whereas GDB's r3
592 only corresponds to the least significant 32-bits. So place
593 the 64-bit DSP type's value in ev3. */
595 regcache_cooked_read (regcache, tdep->ppc_ev0_regnum + 3, readbuf);
597 regcache_cooked_write (regcache, tdep->ppc_ev0_regnum + 3, writebuf);
598 return RETURN_VALUE_REGISTER_CONVENTION;
600 if (broken_gcc && TYPE_LENGTH (type) <= 8)
602 /* GCC screwed up for structures or unions whose size is less
603 than or equal to 8 bytes.. Instead of left-aligning, it
604 right-aligns the data into the buffer formed by r3, r4. */
605 gdb_byte regvals[MAX_REGISTER_SIZE * 2];
606 int len = TYPE_LENGTH (type);
607 int offset = (2 * tdep->wordsize - len) % tdep->wordsize;
611 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
612 regvals + 0 * tdep->wordsize);
613 if (len > tdep->wordsize)
614 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
615 regvals + 1 * tdep->wordsize);
616 memcpy (readbuf, regvals + offset, len);
620 memset (regvals, 0, sizeof regvals);
621 memcpy (regvals + offset, writebuf, len);
622 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
623 regvals + 0 * tdep->wordsize);
624 if (len > tdep->wordsize)
625 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
626 regvals + 1 * tdep->wordsize);
629 return RETURN_VALUE_REGISTER_CONVENTION;
631 if (TYPE_LENGTH (type) <= 8)
635 /* This matches SVr4 PPC, it does not match GCC. */
636 /* The value is right-padded to 8 bytes and then loaded, as
637 two "words", into r3/r4. */
638 gdb_byte regvals[MAX_REGISTER_SIZE * 2];
639 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
640 regvals + 0 * tdep->wordsize);
641 if (TYPE_LENGTH (type) > tdep->wordsize)
642 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
643 regvals + 1 * tdep->wordsize);
644 memcpy (readbuf, regvals, TYPE_LENGTH (type));
648 /* This matches SVr4 PPC, it does not match GCC. */
649 /* The value is padded out to 8 bytes and then loaded, as
650 two "words" into r3/r4. */
651 gdb_byte regvals[MAX_REGISTER_SIZE * 2];
652 memset (regvals, 0, sizeof regvals);
653 memcpy (regvals, writebuf, TYPE_LENGTH (type));
654 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
655 regvals + 0 * tdep->wordsize);
656 if (TYPE_LENGTH (type) > tdep->wordsize)
657 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
658 regvals + 1 * tdep->wordsize);
660 return RETURN_VALUE_REGISTER_CONVENTION;
662 return RETURN_VALUE_STRUCT_CONVENTION;
665 enum return_value_convention
666 ppc_sysv_abi_return_value (struct gdbarch *gdbarch, struct type *valtype,
667 struct regcache *regcache, gdb_byte *readbuf,
668 const gdb_byte *writebuf)
670 return do_ppc_sysv_return_value (gdbarch, valtype, regcache, readbuf,
674 enum return_value_convention
675 ppc_sysv_abi_broken_return_value (struct gdbarch *gdbarch,
676 struct type *valtype,
677 struct regcache *regcache,
678 gdb_byte *readbuf, const gdb_byte *writebuf)
680 return do_ppc_sysv_return_value (gdbarch, valtype, regcache, readbuf,
684 /* The helper function for 64-bit SYSV push_dummy_call. Converts the
685 function's code address back into the function's descriptor
688 Find a value for the TOC register. Every symbol should have both
689 ".FN" and "FN" in the minimal symbol table. "FN" points at the
690 FN's descriptor, while ".FN" points at the entry point (which
691 matches FUNC_ADDR). Need to reverse from FUNC_ADDR back to the
692 FN's descriptor address (while at the same time being careful to
693 find "FN" in the same object file as ".FN"). */
696 convert_code_addr_to_desc_addr (CORE_ADDR code_addr, CORE_ADDR *desc_addr)
698 struct obj_section *dot_fn_section;
699 struct minimal_symbol *dot_fn;
700 struct minimal_symbol *fn;
702 /* Find the minimal symbol that corresponds to CODE_ADDR (should
703 have a name of the form ".FN"). */
704 dot_fn = lookup_minimal_symbol_by_pc (code_addr);
705 if (dot_fn == NULL || SYMBOL_LINKAGE_NAME (dot_fn)[0] != '.')
707 /* Get the section that contains CODE_ADDR. Need this for the
708 "objfile" that it contains. */
709 dot_fn_section = find_pc_section (code_addr);
710 if (dot_fn_section == NULL || dot_fn_section->objfile == NULL)
712 /* Now find the corresponding "FN" (dropping ".") minimal symbol's
713 address. Only look for the minimal symbol in ".FN"'s object file
714 - avoids problems when two object files (i.e., shared libraries)
715 contain a minimal symbol with the same name. */
716 fn = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (dot_fn) + 1, NULL,
717 dot_fn_section->objfile);
720 /* Found a descriptor. */
721 (*desc_addr) = SYMBOL_VALUE_ADDRESS (fn);
725 /* Pass the arguments in either registers, or in the stack. Using the
728 This implements a dumbed down version of the ABI. It always writes
729 values to memory, GPR and FPR, even when not necessary. Doing this
730 greatly simplifies the logic. */
733 ppc64_sysv_abi_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
734 struct regcache *regcache, CORE_ADDR bp_addr,
735 int nargs, struct value **args, CORE_ADDR sp,
736 int struct_return, CORE_ADDR struct_addr)
738 CORE_ADDR func_addr = find_function_addr (function, NULL);
739 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
741 /* See for-loop comment below. */
743 /* Size of the Altivec's vector parameter region, the final value is
744 computed in the for-loop below. */
745 LONGEST vparam_size = 0;
746 /* Size of the general parameter region, the final value is computed
747 in the for-loop below. */
748 LONGEST gparam_size = 0;
749 /* Kevin writes ... I don't mind seeing tdep->wordsize used in the
750 calls to align_up(), align_down(), etc. because this makes it
751 easier to reuse this code (in a copy/paste sense) in the future,
752 but it is a 64-bit ABI and asserting that the wordsize is 8 bytes
753 at some point makes it easier to verify that this function is
754 correct without having to do a non-local analysis to figure out
755 the possible values of tdep->wordsize. */
756 gdb_assert (tdep->wordsize == 8);
758 /* This function exists to support a calling convention that
759 requires floating-point registers. It shouldn't be used on
760 processors that lack them. */
761 gdb_assert (ppc_floating_point_unit_p (gdbarch));
763 /* By this stage in the proceedings, SP has been decremented by "red
764 zone size" + "struct return size". Fetch the stack-pointer from
765 before this and use that as the BACK_CHAIN. */
766 regcache_cooked_read_unsigned (regcache,
767 gdbarch_sp_regnum (current_gdbarch),
770 /* Go through the argument list twice.
772 Pass 1: Compute the function call's stack space and register
775 Pass 2: Replay the same computation but this time also write the
776 values out to the target. */
778 for (write_pass = 0; write_pass < 2; write_pass++)
781 /* Next available floating point register for float and double
784 /* Next available general register for non-vector (but possibly
787 /* Next available vector register for vector arguments. */
789 /* The address, at which the next general purpose parameter
790 (integer, struct, float, ...) should be saved. */
792 /* Address, at which the next Altivec vector parameter should be
798 /* During the first pass, GPARAM and VPARAM are more like
799 offsets (start address zero) than addresses. That way
800 the accumulate the total stack space each region
807 /* Decrement the stack pointer making space for the Altivec
808 and general on-stack parameters. Set vparam and gparam
809 to their corresponding regions. */
810 vparam = align_down (sp - vparam_size, 16);
811 gparam = align_down (vparam - gparam_size, 16);
812 /* Add in space for the TOC, link editor double word,
813 compiler double word, LR save area, CR save area. */
814 sp = align_down (gparam - 48, 16);
817 /* If the function is returning a `struct', then there is an
818 extra hidden parameter (which will be passed in r3)
819 containing the address of that struct.. In that case we
820 should advance one word and start from r4 register to copy
821 parameters. This also consumes one on-stack parameter slot. */
825 regcache_cooked_write_signed (regcache,
826 tdep->ppc_gp0_regnum + greg,
829 gparam = align_up (gparam + tdep->wordsize, tdep->wordsize);
832 for (argno = 0; argno < nargs; argno++)
834 struct value *arg = args[argno];
835 struct type *type = check_typedef (value_type (arg));
836 const bfd_byte *val = value_contents (arg);
837 if (TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) <= 8)
839 /* Floats and Doubles go in f1 .. f13. They also
840 consume a left aligned GREG,, and can end up in
846 gdb_byte regval[MAX_REGISTER_SIZE];
848 = register_type (gdbarch, tdep->ppc_fp0_regnum);
849 convert_typed_floating (val, type, regval, regtype);
850 regcache_cooked_write (regcache,
851 tdep->ppc_fp0_regnum + freg,
856 /* The ABI states "Single precision floating
857 point values are mapped to the first word in
858 a single doubleword" and "... floating point
859 values mapped to the first eight doublewords
860 of the parameter save area are also passed in
863 This code interprets that to mean: store it,
864 left aligned, in the general register. */
865 gdb_byte regval[MAX_REGISTER_SIZE];
866 memset (regval, 0, sizeof regval);
867 memcpy (regval, val, TYPE_LENGTH (type));
868 regcache_cooked_write (regcache,
869 tdep->ppc_gp0_regnum + greg,
872 write_memory (gparam, val, TYPE_LENGTH (type));
874 /* Always consume parameter stack space. */
877 gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize);
879 else if (TYPE_CODE (type) == TYPE_CODE_FLT
880 && TYPE_LENGTH (type) == 16
881 && (gdbarch_long_double_format (current_gdbarch)
882 == floatformats_ibm_long_double))
884 /* IBM long double stored in two doublewords of the
885 parameter save area and corresponding registers. */
888 if (!tdep->soft_float && freg <= 13)
890 regcache_cooked_write (regcache,
891 tdep->ppc_fp0_regnum + freg,
894 regcache_cooked_write (regcache,
895 tdep->ppc_fp0_regnum + freg + 1,
900 regcache_cooked_write (regcache,
901 tdep->ppc_gp0_regnum + greg,
904 regcache_cooked_write (regcache,
905 tdep->ppc_gp0_regnum + greg + 1,
908 write_memory (gparam, val, TYPE_LENGTH (type));
912 gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize);
914 else if (TYPE_LENGTH (type) == 16 && TYPE_VECTOR (type)
915 && TYPE_CODE (type) == TYPE_CODE_ARRAY
916 && tdep->ppc_vr0_regnum >= 0)
918 /* In the Altivec ABI, vectors go in the vector
919 registers v2 .. v13, or when that runs out, a vector
920 annex which goes above all the normal parameters.
921 NOTE: cagney/2003-09-21: This is a guess based on the
922 PowerOpen Altivec ABI. */
926 regcache_cooked_write (regcache,
927 tdep->ppc_vr0_regnum + vreg, val);
933 write_memory (vparam, val, TYPE_LENGTH (type));
934 vparam = align_up (vparam + TYPE_LENGTH (type), 16);
937 else if ((TYPE_CODE (type) == TYPE_CODE_INT
938 || TYPE_CODE (type) == TYPE_CODE_ENUM
939 || TYPE_CODE (type) == TYPE_CODE_PTR)
940 && TYPE_LENGTH (type) <= 8)
942 /* Scalars and Pointers get sign[un]extended and go in
943 gpr3 .. gpr10. They can also end up in memory. */
946 /* Sign extend the value, then store it unsigned. */
947 ULONGEST word = unpack_long (type, val);
948 /* Convert any function code addresses into
950 if (TYPE_CODE (type) == TYPE_CODE_PTR
951 && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC)
953 CORE_ADDR desc = word;
954 convert_code_addr_to_desc_addr (word, &desc);
958 regcache_cooked_write_unsigned (regcache,
959 tdep->ppc_gp0_regnum +
961 write_memory_unsigned_integer (gparam, tdep->wordsize,
965 gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize);
970 for (byte = 0; byte < TYPE_LENGTH (type);
971 byte += tdep->wordsize)
973 if (write_pass && greg <= 10)
975 gdb_byte regval[MAX_REGISTER_SIZE];
976 int len = TYPE_LENGTH (type) - byte;
977 if (len > tdep->wordsize)
978 len = tdep->wordsize;
979 memset (regval, 0, sizeof regval);
980 /* The ABI (version 1.9) specifies that values
981 smaller than one doubleword are right-aligned
982 and those larger are left-aligned. GCC
983 versions before 3.4 implemented this
985 <http://gcc.gnu.org/gcc-3.4/powerpc-abi.html>. */
987 memcpy (regval + tdep->wordsize - len,
990 memcpy (regval, val + byte, len);
991 regcache_cooked_write (regcache, greg, regval);
996 /* WARNING: cagney/2003-09-21: Strictly speaking, this
997 isn't necessary, unfortunately, GCC appears to get
998 "struct convention" parameter passing wrong putting
999 odd sized structures in memory instead of in a
1000 register. Work around this by always writing the
1001 value to memory. Fortunately, doing this
1002 simplifies the code. */
1003 write_memory (gparam, val, TYPE_LENGTH (type));
1005 && TYPE_CODE (type) == TYPE_CODE_STRUCT
1006 && TYPE_NFIELDS (type) == 1
1007 && TYPE_LENGTH (type) <= 16)
1009 /* The ABI (version 1.9) specifies that structs
1010 containing a single floating-point value, at any
1011 level of nesting of single-member structs, are
1012 passed in floating-point registers. */
1013 while (TYPE_CODE (type) == TYPE_CODE_STRUCT
1014 && TYPE_NFIELDS (type) == 1)
1015 type = check_typedef (TYPE_FIELD_TYPE (type, 0));
1016 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1018 if (TYPE_LENGTH (type) <= 8)
1022 gdb_byte regval[MAX_REGISTER_SIZE];
1023 struct type *regtype
1024 = register_type (gdbarch,
1025 tdep->ppc_fp0_regnum);
1026 convert_typed_floating (val, type, regval,
1028 regcache_cooked_write (regcache,
1029 (tdep->ppc_fp0_regnum
1035 else if (TYPE_LENGTH (type) == 16
1036 && (gdbarch_long_double_format (current_gdbarch)
1037 == floatformats_ibm_long_double))
1041 regcache_cooked_write (regcache,
1042 (tdep->ppc_fp0_regnum
1046 regcache_cooked_write (regcache,
1047 (tdep->ppc_fp0_regnum
1055 /* Always consume parameter stack space. */
1056 gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize);
1062 /* Save the true region sizes ready for the second pass. */
1063 vparam_size = vparam;
1064 /* Make certain that the general parameter save area is at
1065 least the minimum 8 registers (or doublewords) in size. */
1067 gparam_size = 8 * tdep->wordsize;
1069 gparam_size = gparam;
1074 regcache_cooked_write_signed (regcache,
1075 gdbarch_sp_regnum (current_gdbarch), sp);
1077 /* Write the backchain (it occupies WORDSIZED bytes). */
1078 write_memory_signed_integer (sp, tdep->wordsize, back_chain);
1080 /* Point the inferior function call's return address at the dummy's
1082 regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
1084 /* Use the func_addr to find the descriptor, and use that to find
1087 CORE_ADDR desc_addr;
1088 if (convert_code_addr_to_desc_addr (func_addr, &desc_addr))
1090 /* The TOC is the second double word in the descriptor. */
1092 read_memory_unsigned_integer (desc_addr + tdep->wordsize,
1094 regcache_cooked_write_unsigned (regcache,
1095 tdep->ppc_gp0_regnum + 2, toc);
1103 /* The 64 bit ABI return value convention.
1105 Return non-zero if the return-value is stored in a register, return
1106 0 if the return-value is instead stored on the stack (a.k.a.,
1107 struct return convention).
1109 For a return-value stored in a register: when WRITEBUF is non-NULL,
1110 copy the buffer to the corresponding register return-value location
1111 location; when READBUF is non-NULL, fill the buffer from the
1112 corresponding register return-value location. */
1113 enum return_value_convention
1114 ppc64_sysv_abi_return_value (struct gdbarch *gdbarch, struct type *valtype,
1115 struct regcache *regcache, gdb_byte *readbuf,
1116 const gdb_byte *writebuf)
1118 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1120 /* This function exists to support a calling convention that
1121 requires floating-point registers. It shouldn't be used on
1122 processors that lack them. */
1123 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1125 /* Floats and doubles in F1. */
1126 if (TYPE_CODE (valtype) == TYPE_CODE_FLT && TYPE_LENGTH (valtype) <= 8)
1128 gdb_byte regval[MAX_REGISTER_SIZE];
1129 struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
1130 if (writebuf != NULL)
1132 convert_typed_floating (writebuf, valtype, regval, regtype);
1133 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
1135 if (readbuf != NULL)
1137 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
1138 convert_typed_floating (regval, regtype, readbuf, valtype);
1140 return RETURN_VALUE_REGISTER_CONVENTION;
1142 /* Integers in r3. */
1143 if ((TYPE_CODE (valtype) == TYPE_CODE_INT
1144 || TYPE_CODE (valtype) == TYPE_CODE_ENUM)
1145 && TYPE_LENGTH (valtype) <= 8)
1147 if (writebuf != NULL)
1149 /* Be careful to sign extend the value. */
1150 regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
1151 unpack_long (valtype, writebuf));
1153 if (readbuf != NULL)
1155 /* Extract the integer from r3. Since this is truncating the
1156 value, there isn't a sign extension problem. */
1158 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
1160 store_unsigned_integer (readbuf, TYPE_LENGTH (valtype), regval);
1162 return RETURN_VALUE_REGISTER_CONVENTION;
1164 /* All pointers live in r3. */
1165 if (TYPE_CODE (valtype) == TYPE_CODE_PTR)
1167 /* All pointers live in r3. */
1168 if (writebuf != NULL)
1169 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
1170 if (readbuf != NULL)
1171 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, readbuf);
1172 return RETURN_VALUE_REGISTER_CONVENTION;
1174 /* Array type has more than one use. */
1175 if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
1177 /* Small character arrays are returned, right justified, in r3. */
1178 if (TYPE_LENGTH (valtype) <= 8
1179 && TYPE_CODE (TYPE_TARGET_TYPE (valtype)) == TYPE_CODE_INT
1180 && TYPE_LENGTH (TYPE_TARGET_TYPE (valtype)) == 1)
1182 int offset = (register_size (gdbarch, tdep->ppc_gp0_regnum + 3)
1183 - TYPE_LENGTH (valtype));
1184 if (writebuf != NULL)
1185 regcache_cooked_write_part (regcache, tdep->ppc_gp0_regnum + 3,
1186 offset, TYPE_LENGTH (valtype), writebuf);
1187 if (readbuf != NULL)
1188 regcache_cooked_read_part (regcache, tdep->ppc_gp0_regnum + 3,
1189 offset, TYPE_LENGTH (valtype), readbuf);
1190 return RETURN_VALUE_REGISTER_CONVENTION;
1192 /* A VMX vector is returned in v2. */
1193 if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY
1194 && TYPE_VECTOR (valtype) && tdep->ppc_vr0_regnum >= 0)
1197 regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
1199 regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
1200 return RETURN_VALUE_REGISTER_CONVENTION;
1203 /* Big floating point values get stored in adjacent floating
1204 point registers, starting with F1. */
1205 if (TYPE_CODE (valtype) == TYPE_CODE_FLT
1206 && (TYPE_LENGTH (valtype) == 16 || TYPE_LENGTH (valtype) == 32))
1208 if (writebuf || readbuf != NULL)
1211 for (i = 0; i < TYPE_LENGTH (valtype) / 8; i++)
1213 if (writebuf != NULL)
1214 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1 + i,
1215 (const bfd_byte *) writebuf + i * 8);
1216 if (readbuf != NULL)
1217 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1 + i,
1218 (bfd_byte *) readbuf + i * 8);
1221 return RETURN_VALUE_REGISTER_CONVENTION;
1223 /* Complex values get returned in f1:f2, need to convert. */
1224 if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX
1225 && (TYPE_LENGTH (valtype) == 8 || TYPE_LENGTH (valtype) == 16))
1227 if (regcache != NULL)
1230 for (i = 0; i < 2; i++)
1232 gdb_byte regval[MAX_REGISTER_SIZE];
1233 struct type *regtype =
1234 register_type (current_gdbarch, tdep->ppc_fp0_regnum);
1235 if (writebuf != NULL)
1237 convert_typed_floating ((const bfd_byte *) writebuf +
1238 i * (TYPE_LENGTH (valtype) / 2),
1239 valtype, regval, regtype);
1240 regcache_cooked_write (regcache,
1241 tdep->ppc_fp0_regnum + 1 + i,
1244 if (readbuf != NULL)
1246 regcache_cooked_read (regcache,
1247 tdep->ppc_fp0_regnum + 1 + i,
1249 convert_typed_floating (regval, regtype,
1250 (bfd_byte *) readbuf +
1251 i * (TYPE_LENGTH (valtype) / 2),
1256 return RETURN_VALUE_REGISTER_CONVENTION;
1258 /* Big complex values get stored in f1:f4. */
1259 if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX && TYPE_LENGTH (valtype) == 32)
1261 if (regcache != NULL)
1264 for (i = 0; i < 4; i++)
1266 if (writebuf != NULL)
1267 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1 + i,
1268 (const bfd_byte *) writebuf + i * 8);
1269 if (readbuf != NULL)
1270 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1 + i,
1271 (bfd_byte *) readbuf + i * 8);
1274 return RETURN_VALUE_REGISTER_CONVENTION;
1276 return RETURN_VALUE_STRUCT_CONVENTION;
1280 ppc64_sysv_abi_adjust_breakpoint_address (struct gdbarch *gdbarch,
1283 /* PPC64 SYSV specifies that the minimal-symbol "FN" should point at
1284 a function-descriptor while the corresponding minimal-symbol
1285 ".FN" should point at the entry point. Consequently, a command
1286 like "break FN" applied to an object file with only minimal
1287 symbols, will insert the breakpoint into the descriptor at "FN"
1288 and not the function at ".FN". Avoid this confusion by adjusting
1289 any attempt to set a descriptor breakpoint into a corresponding
1290 function breakpoint. Note that GDB warns the user when this
1291 adjustment is applied - that's ok as otherwise the user will have
1292 no way of knowing why their breakpoint at "FN" resulted in the
1293 program stopping at ".FN". */
1294 return gdbarch_convert_from_func_ptr_addr (gdbarch, bpaddr, ¤t_target);