* ppc-sysv-tdep.c (ppc_sysv_abi_push_dummy_call): Add support for
[external/binutils.git] / gdb / ppc-sysv-tdep.c
1 /* Target-dependent code for PowerPC systems using the SVR4 ABI
2    for GDB, the GNU debugger.
3
4    Copyright (C) 2000, 2001, 2002, 2003, 2005, 2007, 2008
5    Free Software Foundation, Inc.
6
7    This file is part of GDB.
8
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.
13
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.
18
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/>.  */
21
22 #include "defs.h"
23 #include "gdbcore.h"
24 #include "inferior.h"
25 #include "regcache.h"
26 #include "value.h"
27 #include "gdb_string.h"
28 #include "gdb_assert.h"
29 #include "ppc-tdep.h"
30 #include "target.h"
31 #include "objfiles.h"
32 #include "infcall.h"
33
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. 
40
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,
43    starting from r4. */
44
45 CORE_ADDR
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)
50 {
51   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
52   ULONGEST saved_sp;
53   int argspace = 0;             /* 0 is an initial wrong guess.  */
54   int write_pass;
55
56   gdb_assert (tdep->wordsize == 4);
57
58   regcache_cooked_read_unsigned (regcache, gdbarch_sp_regnum (gdbarch),
59                                  &saved_sp);
60
61   /* Go through the argument list twice.
62
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
67      address.
68
69      Pass 2: Replay the same computation but this time also write the
70      values out to the target.  */
71
72   for (write_pass = 0; write_pass < 2; write_pass++)
73     {
74       int argno;
75       /* Next available floating point register for float and double
76          arguments.  */
77       int freg = 1;
78       /* Next available general register for non-float, non-vector
79          arguments.  */
80       int greg = 3;
81       /* Next available vector register for vector arguments.  */
82       int vreg = 2;
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;
87
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.  */
92       if (struct_return)
93         {
94           if (write_pass)
95             regcache_cooked_write_signed (regcache,
96                                           tdep->ppc_gp0_regnum + greg,
97                                           struct_addr);
98           greg++;
99         }
100
101       for (argno = 0; argno < nargs; argno++)
102         {
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);
107
108           if (TYPE_CODE (type) == TYPE_CODE_FLT && len <= 8
109               && !tdep->soft_float)
110             {
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.  */
114               if (freg <= 8)
115                 {
116                   if (write_pass)
117                     {
118                       /* Always store the floating point value using
119                          the register's floating-point format.  */
120                       gdb_byte regval[MAX_REGISTER_SIZE];
121                       struct type *regtype
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,
126                                              regval);
127                     }
128                   freg++;
129                 }
130               else
131                 {
132                   /* SysV ABI converts floats to doubles before
133                      writing them to an 8 byte aligned stack location.  */
134                   argoffset = align_up (argoffset, 8);
135                   if (write_pass)
136                     {
137                       char memval[8];
138                       convert_typed_floating (val, type, memval,
139                                               builtin_type_ieee_double);
140                       write_memory (sp + argoffset, val, len);
141                     }
142                   argoffset += 8;
143                 }
144             }
145           else if (TYPE_CODE (type) == TYPE_CODE_FLT
146                    && len == 16
147                    && !tdep->soft_float
148                    && (gdbarch_long_double_format (gdbarch)
149                        == floatformats_ibm_long_double))
150             {
151               /* IBM long double passed in two FP registers if
152                  available, otherwise 8-byte aligned stack.  */
153               if (freg <= 7)
154                 {
155                   if (write_pass)
156                     {
157                       regcache_cooked_write (regcache,
158                                              tdep->ppc_fp0_regnum + freg,
159                                              val);
160                       regcache_cooked_write (regcache,
161                                              tdep->ppc_fp0_regnum + freg + 1,
162                                              val + 8);
163                     }
164                   freg += 2;
165                 }
166               else
167                 {
168                   argoffset = align_up (argoffset, 8);
169                   if (write_pass)
170                     write_memory (sp + argoffset, val, len);
171                   argoffset += 16;
172                 }
173             }
174           else if (len == 8
175                    && (TYPE_CODE (type) == TYPE_CODE_INT        /* long long */
176                        || TYPE_CODE (type) == TYPE_CODE_FLT))   /* double */
177             {
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.  */
183               if (greg > 9)
184                 {
185                   /* Just in case GREG was 10.  */
186                   greg = 11;
187                   argoffset = align_up (argoffset, 8);
188                   if (write_pass)
189                     write_memory (sp + argoffset, val, len);
190                   argoffset += 8;
191                 }
192               else
193                 {
194                   /* Must start on an odd register - r3/r4 etc.  */
195                   if ((greg & 1) == 0)
196                     greg++;
197                   if (write_pass)
198                     {
199                       regcache_cooked_write (regcache,
200                                              tdep->ppc_gp0_regnum + greg + 0,
201                                              val + 0);
202                       regcache_cooked_write (regcache,
203                                              tdep->ppc_gp0_regnum + greg + 1,
204                                              val + 4);
205                     }
206                   greg += 2;
207                 }
208             }
209           else if (len == 16 && TYPE_CODE (type) == TYPE_CODE_FLT
210                    && (gdbarch_long_double_format (gdbarch)
211                        == floatformats_ibm_long_double))
212             {
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.  */
216               if (greg > 7)
217                 {
218                   greg = 11;
219                   argoffset = align_up (argoffset, 8);
220                   if (write_pass)
221                     write_memory (sp + argoffset, val, len);
222                   argoffset += 16;
223                 }
224               else
225                 {
226                   if (write_pass)
227                     {
228                       regcache_cooked_write (regcache,
229                                              tdep->ppc_gp0_regnum + greg + 0,
230                                              val + 0);
231                       regcache_cooked_write (regcache,
232                                              tdep->ppc_gp0_regnum + greg + 1,
233                                              val + 4);
234                       regcache_cooked_write (regcache,
235                                              tdep->ppc_gp0_regnum + greg + 2,
236                                              val + 8);
237                       regcache_cooked_write (regcache,
238                                              tdep->ppc_gp0_regnum + greg + 3,
239                                              val + 12);
240                     }
241                   greg += 4;
242                 }
243             }
244           else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && len <= 8
245                    && !tdep->soft_float)
246             {
247               /* 32-bit and 64-bit decimal floats go in f1 .. f8.  They can
248                  end up in memory.  */
249
250               if (freg <= 8)
251                 {
252                   if (write_pass)
253                     {
254                       gdb_byte regval[MAX_REGISTER_SIZE];
255                       const gdb_byte *p;
256
257                       /* 32-bit decimal floats are right aligned in the
258                          doubleword.  */
259                       if (TYPE_LENGTH (type) == 4)
260                       {
261                         memcpy (regval + 4, val, 4);
262                         p = regval;
263                       }
264                       else
265                         p = val;
266
267                       regcache_cooked_write (regcache,
268                           tdep->ppc_fp0_regnum + freg, p);
269                     }
270
271                   freg++;
272                 }
273               else
274                 {
275                   argoffset = align_up (argoffset, len);
276
277                   if (write_pass)
278                     /* Write value in the stack's parameter save area.  */
279                     write_memory (sp + argoffset, val, len);
280
281                   argoffset += len;
282                 }
283             }
284           else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && len == 16
285                    && !tdep->soft_float)
286             {
287               /* 128-bit decimal floats go in f2 .. f7, always in even/odd
288                  pairs.  They can end up in memory, using two doublewords.  */
289
290               if (freg <= 6)
291                 {
292                   /* Make sure freg is even.  */
293                   freg += freg & 1;
294
295                   if (write_pass)
296                     {
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);
301                     }
302                 }
303               else
304                 {
305                   argoffset = align_up (argoffset, 8);
306
307                   if (write_pass)
308                     write_memory (sp + argoffset, val, 16);
309
310                   argoffset += 16;
311                 }
312
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.  */
317               freg += 2;
318             }
319           else if (len == 16
320                    && TYPE_CODE (type) == TYPE_CODE_ARRAY
321                    && TYPE_VECTOR (type)
322                    && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
323             {
324               /* Vector parameter passed in an Altivec register, or
325                  when that runs out, 16 byte aligned stack location.  */
326               if (vreg <= 13)
327                 {
328                   if (write_pass)
329                     regcache_cooked_write (regcache,
330                                            tdep->ppc_vr0_regnum + vreg, val);
331                   vreg++;
332                 }
333               else
334                 {
335                   argoffset = align_up (argoffset, 16);
336                   if (write_pass)
337                     write_memory (sp + argoffset, val, 16);
338                   argoffset += 16;
339                 }
340             }
341           else if (len == 8
342                    && TYPE_CODE (type) == TYPE_CODE_ARRAY
343                    && TYPE_VECTOR (type)
344                    && tdep->vector_abi == POWERPC_VEC_SPE)
345             {
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.  */
353               if (greg <= 10)
354                 {
355                   if (write_pass)
356                     regcache_cooked_write (regcache,
357                                            tdep->ppc_ev0_regnum + greg, val);
358                   greg++;
359                 }
360               else
361                 {
362                   argoffset = align_up (argoffset, 8);
363                   if (write_pass)
364                     write_memory (sp + argoffset, val, 8);
365                   argoffset += 8;
366                 }
367             }
368           else
369             {
370               /* Reduce the parameter down to something that fits in a
371                  "word".  */
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)
377                 {
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)
382                       && len >= 16)
383                     structoffset = align_up (structoffset, 16);
384                   else
385                     structoffset = align_up (structoffset, 8);
386
387                   if (write_pass)
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,
392                                           sp + structoffset);
393                   structoffset += len;
394                 }
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));
399               else
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.  */
404               if (greg <= 10)
405                 {
406                   if (write_pass)
407                     regcache_cooked_write (regcache,
408                                            tdep->ppc_gp0_regnum + greg, word);
409                   greg++;
410                 }
411               else
412                 {
413                   argoffset = align_up (argoffset, tdep->wordsize);
414                   if (write_pass)
415                     write_memory (sp + argoffset, word, tdep->wordsize);
416                   argoffset += tdep->wordsize;
417                 }
418             }
419         }
420
421       /* Compute the actual stack space requirements.  */
422       if (!write_pass)
423         {
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);
430         }
431
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
437          shouldn't hurt.  */
438       if (write_pass)
439         {
440           ULONGEST cr;
441
442           regcache_cooked_read_unsigned (regcache, tdep->ppc_cr_regnum, &cr);
443           if (freg > 1)
444             cr |= 0x02000000;
445           else
446             cr &= ~0x02000000;
447           regcache_cooked_write_unsigned (regcache, tdep->ppc_cr_regnum, cr);
448         }
449     }
450
451   /* Update %sp.   */
452   regcache_cooked_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
453
454   /* Write the backchain (it occupies WORDSIZED bytes).  */
455   write_memory_signed_integer (sp, tdep->wordsize, saved_sp);
456
457   /* Point the inferior function call's return address at the dummy's
458      breakpoint.  */
459   regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
460
461   return sp;
462 }
463
464 /* Handle the return-value conventions for Decimal Floating Point values
465    in both ppc32 and ppc64, which are the same.  */
466 static int
467 get_decimal_float_return_value (struct gdbarch *gdbarch, struct type *valtype,
468                                 struct regcache *regcache, gdb_byte *readbuf,
469                                 const gdb_byte *writebuf)
470 {
471   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
472
473   gdb_assert (TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT);
474
475   /* 32-bit and 64-bit decimal floats in f1.  */
476   if (TYPE_LENGTH (valtype) <= 8)
477     {
478       if (writebuf != NULL)
479         {
480           gdb_byte regval[MAX_REGISTER_SIZE];
481           const gdb_byte *p;
482
483           /* 32-bit decimal float is right aligned in the doubleword.  */
484           if (TYPE_LENGTH (valtype) == 4)
485             {
486               memcpy (regval + 4, writebuf, 4);
487               p = regval;
488             }
489           else
490             p = writebuf;
491
492           regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, p);
493         }
494       if (readbuf != NULL)
495         {
496           regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, readbuf);
497
498           /* Left align 32-bit decimal float.  */
499           if (TYPE_LENGTH (valtype) == 4)
500             memcpy (readbuf, readbuf + 4, 4);
501         }
502     }
503   /* 128-bit decimal floats in f2,f3.  */
504   else if (TYPE_LENGTH (valtype) == 16)
505     {
506       if (writebuf != NULL || readbuf != NULL)
507         {
508           int i;
509
510           for (i = 0; i < 2; i++)
511             {
512               if (writebuf != NULL)
513                 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 2 + i,
514                                        writebuf + i * 8);
515               if (readbuf != NULL)
516                 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 2 + i,
517                                       readbuf + i * 8);
518             }
519         }
520     }
521   else
522     /* Can't happen.  */
523     internal_error (__FILE__, __LINE__, "Unknown decimal float size.");
524
525   return RETURN_VALUE_REGISTER_CONVENTION;
526 }
527
528 /* Handle the return-value conventions specified by the SysV 32-bit
529    PowerPC ABI (including all the supplements):
530
531    no floating-point: floating-point values returned using 32-bit
532    general-purpose registers.
533
534    Altivec: 128-bit vectors returned using vector registers.
535
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.
539
540    GCC (broken): Small struct values right (instead of left) aligned
541    when returned in general-purpose registers.  */
542
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)
547 {
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)
553     {
554       if (readbuf)
555         {
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);
563         }
564       if (writebuf)
565         {
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);
572         }
573       return RETURN_VALUE_REGISTER_CONVENTION;
574     }
575   if (TYPE_CODE (type) == TYPE_CODE_FLT
576       && TYPE_LENGTH (type) == 16
577       && !tdep->soft_float
578       && (gdbarch_long_double_format (gdbarch) == floatformats_ibm_long_double))
579     {
580       /* IBM long double stored in f1 and f2.  */
581       if (readbuf)
582         {
583           regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, readbuf);
584           regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 2,
585                                 readbuf + 8);
586         }
587       if (writebuf)
588         {
589           regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, writebuf);
590           regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 2,
591                                  writebuf + 8);
592         }
593       return RETURN_VALUE_REGISTER_CONVENTION;
594     }
595   if (TYPE_CODE (type) == TYPE_CODE_FLT
596       && TYPE_LENGTH (type) == 16
597       && (gdbarch_long_double_format (gdbarch) == floatformats_ibm_long_double))
598     {
599       /* Soft-float IBM long double stored in r3, r4, r5, r6.  */
600       if (readbuf)
601         {
602           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, readbuf);
603           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
604                                 readbuf + 4);
605           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 5,
606                                 readbuf + 8);
607           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 6,
608                                 readbuf + 12);
609         }
610       if (writebuf)
611         {
612           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
613           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
614                                  writebuf + 4);
615           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 5,
616                                  writebuf + 8);
617           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 6,
618                                  writebuf + 12);
619         }
620       return RETURN_VALUE_REGISTER_CONVENTION;
621     }
622   if ((TYPE_CODE (type) == TYPE_CODE_INT && TYPE_LENGTH (type) == 8)
623       || (TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8))
624     {
625       if (readbuf)
626         {
627           /* A long long, or a double stored in the 32 bit r3/r4.  */
628           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
629                                 readbuf + 0);
630           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
631                                 readbuf + 4);
632         }
633       if (writebuf)
634         {
635           /* A long long, or a double stored in the 32 bit r3/r4.  */
636           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
637                                  writebuf + 0);
638           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
639                                  writebuf + 4);
640         }
641       return RETURN_VALUE_REGISTER_CONVENTION;
642     }
643   if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && !tdep->soft_float)
644     return get_decimal_float_return_value (gdbarch, type, regcache, readbuf,
645                                            writebuf);
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)
653     {
654       if (readbuf)
655         {
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.  */
659           ULONGEST regval;
660           regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
661                                          &regval);
662           store_unsigned_integer (readbuf, TYPE_LENGTH (type), regval);
663         }
664       if (writebuf)
665         {
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));
670         }
671       return RETURN_VALUE_REGISTER_CONVENTION;
672     }
673   if (TYPE_LENGTH (type) == 16
674       && TYPE_CODE (type) == TYPE_CODE_ARRAY
675       && TYPE_VECTOR (type)
676       && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
677     {
678       if (readbuf)
679         {
680           /* Altivec places the return value in "v2".  */
681           regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
682         }
683       if (writebuf)
684         {
685           /* Altivec places the return value in "v2".  */
686           regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
687         }
688       return RETURN_VALUE_REGISTER_CONVENTION;
689     }
690   if (TYPE_LENGTH (type) == 16
691       && TYPE_CODE (type) == TYPE_CODE_ARRAY
692       && TYPE_VECTOR (type)
693       && tdep->vector_abi == POWERPC_VEC_GENERIC)
694     {
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.  */
698       if (readbuf)
699         {
700           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
701                                 readbuf + 0);
702           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
703                                 readbuf + 4);
704           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 5,
705                                 readbuf + 8);
706           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 6,
707                                 readbuf + 12);
708         }
709       if (writebuf)
710         {
711           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
712                                  writebuf + 0);
713           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
714                                  writebuf + 4);
715           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 5,
716                                  writebuf + 8);
717           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 6,
718                                  writebuf + 12);
719         }
720       return RETURN_VALUE_REGISTER_CONVENTION;
721     }
722   if (TYPE_LENGTH (type) == 8
723       && TYPE_CODE (type) == TYPE_CODE_ARRAY
724       && TYPE_VECTOR (type)
725       && tdep->vector_abi == POWERPC_VEC_SPE)
726     {
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.  */
732       if (readbuf)
733         regcache_cooked_read (regcache, tdep->ppc_ev0_regnum + 3, readbuf);
734       if (writebuf)
735         regcache_cooked_write (regcache, tdep->ppc_ev0_regnum + 3, writebuf);
736       return RETURN_VALUE_REGISTER_CONVENTION;
737     }
738   if (broken_gcc && TYPE_LENGTH (type) <= 8)
739     {
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;
746
747       if (readbuf)
748         {
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);
755         }
756       if (writebuf)
757         {
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);
765         }
766
767       return RETURN_VALUE_REGISTER_CONVENTION;
768     }
769   if (TYPE_LENGTH (type) <= 8)
770     {
771       if (readbuf)
772         {
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));
783         }
784       if (writebuf)
785         {
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);
797         }
798       return RETURN_VALUE_REGISTER_CONVENTION;
799     }
800   return RETURN_VALUE_STRUCT_CONVENTION;
801 }
802
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)
807 {
808   return do_ppc_sysv_return_value (gdbarch, valtype, regcache, readbuf,
809                                    writebuf, 0);
810 }
811
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)
817 {
818   return do_ppc_sysv_return_value (gdbarch, valtype, regcache, readbuf,
819                                    writebuf, 1);
820 }
821
822 /* The helper function for 64-bit SYSV push_dummy_call.  Converts the
823    function's code address back into the function's descriptor
824    address.
825
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").  */
832
833 static int
834 convert_code_addr_to_desc_addr (CORE_ADDR code_addr, CORE_ADDR *desc_addr)
835 {
836   struct obj_section *dot_fn_section;
837   struct minimal_symbol *dot_fn;
838   struct minimal_symbol *fn;
839   CORE_ADDR toc;
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] != '.')
844     return 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)
849     return 0;
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);
856   if (fn == NULL)
857     return 0;
858   /* Found a descriptor.  */
859   (*desc_addr) = SYMBOL_VALUE_ADDRESS (fn);
860   return 1;
861 }
862
863 /* Pass the arguments in either registers, or in the stack. Using the
864    ppc 64 bit SysV ABI.
865
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. */
869
870 CORE_ADDR
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)
875 {
876   CORE_ADDR func_addr = find_function_addr (function, NULL);
877   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
878   ULONGEST back_chain;
879   /* See for-loop comment below.  */
880   int write_pass;
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);
895
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));
900
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),
905                                  &back_chain);
906
907   /* Go through the argument list twice.
908
909      Pass 1: Compute the function call's stack space and register
910      requirements.
911
912      Pass 2: Replay the same computation but this time also write the
913      values out to the target.  */
914
915   for (write_pass = 0; write_pass < 2; write_pass++)
916     {
917       int argno;
918       /* Next available floating point register for float and double
919          arguments.  */
920       int freg = 1;
921       /* Next available general register for non-vector (but possibly
922          float) arguments.  */
923       int greg = 3;
924       /* Next available vector register for vector arguments.  */
925       int vreg = 2;
926       /* The address, at which the next general purpose parameter
927          (integer, struct, float, ...) should be saved.  */
928       CORE_ADDR gparam;
929       /* Address, at which the next Altivec vector parameter should be
930          saved.  */
931       CORE_ADDR vparam;
932
933       if (!write_pass)
934         {
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
938              requires.  */
939           gparam = 0;
940           vparam = 0;
941         }
942       else
943         {
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);
952         }
953
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.  */
959       if (struct_return)
960         {
961           if (write_pass)
962             regcache_cooked_write_signed (regcache,
963                                           tdep->ppc_gp0_regnum + greg,
964                                           struct_addr);
965           greg++;
966           gparam = align_up (gparam + tdep->wordsize, tdep->wordsize);
967         }
968
969       for (argno = 0; argno < nargs; argno++)
970         {
971           struct value *arg = args[argno];
972           struct type *type = check_typedef (value_type (arg));
973           const bfd_byte *val = value_contents (arg);
974           if (TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) <= 8)
975             {
976               /* Floats and Doubles go in f1 .. f13.  They also
977                  consume a left aligned GREG,, and can end up in
978                  memory.  */
979               if (write_pass)
980                 {
981                   if (freg <= 13)
982                     {
983                       gdb_byte regval[MAX_REGISTER_SIZE];
984                       struct type *regtype
985                         = register_type (gdbarch, tdep->ppc_fp0_regnum);
986                       convert_typed_floating (val, type, regval, regtype);
987                       regcache_cooked_write (regcache,
988                                              tdep->ppc_fp0_regnum + freg,
989                                              regval);
990                     }
991                   if (greg <= 10)
992                     {
993                       /* The ABI states "Single precision floating
994                          point values are mapped to the first word in
995                          a single doubleword" and "... floating point
996                          values mapped to the first eight doublewords
997                          of the parameter save area are also passed in
998                          general registers").
999
1000                          This code interprets that to mean: store it,
1001                          left aligned, in the general register.  */
1002                       gdb_byte regval[MAX_REGISTER_SIZE];
1003                       memset (regval, 0, sizeof regval);
1004                       memcpy (regval, val, TYPE_LENGTH (type));
1005                       regcache_cooked_write (regcache,
1006                                              tdep->ppc_gp0_regnum + greg,
1007                                              regval);
1008                     }
1009                   write_memory (gparam, val, TYPE_LENGTH (type));
1010                 }
1011               /* Always consume parameter stack space.  */
1012               freg++;
1013               greg++;
1014               gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize);
1015             }
1016           else if (TYPE_CODE (type) == TYPE_CODE_FLT
1017                    && TYPE_LENGTH (type) == 16
1018                    && (gdbarch_long_double_format (gdbarch)
1019                        == floatformats_ibm_long_double))
1020             {
1021               /* IBM long double stored in two doublewords of the
1022                  parameter save area and corresponding registers.  */
1023               if (write_pass)
1024                 {
1025                   if (!tdep->soft_float && freg <= 13)
1026                     {
1027                       regcache_cooked_write (regcache,
1028                                              tdep->ppc_fp0_regnum + freg,
1029                                              val);
1030                       if (freg <= 12)
1031                         regcache_cooked_write (regcache,
1032                                                tdep->ppc_fp0_regnum + freg + 1,
1033                                                val + 8);
1034                     }
1035                   if (greg <= 10)
1036                     {
1037                       regcache_cooked_write (regcache,
1038                                              tdep->ppc_gp0_regnum + greg,
1039                                              val);
1040                       if (greg <= 9)
1041                         regcache_cooked_write (regcache,
1042                                                tdep->ppc_gp0_regnum + greg + 1,
1043                                                val + 8);
1044                     }
1045                   write_memory (gparam, val, TYPE_LENGTH (type));
1046                 }
1047               freg += 2;
1048               greg += 2;
1049               gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize);
1050             }
1051           else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT
1052                    && TYPE_LENGTH (type) <= 8)
1053             {
1054               /* 32-bit and 64-bit decimal floats go in f1 .. f13.  They can
1055                  end up in memory.  */
1056               if (write_pass)
1057                 {
1058                   gdb_byte regval[MAX_REGISTER_SIZE];
1059                   const gdb_byte *p;
1060
1061                   /* 32-bit decimal floats are right aligned in the
1062                      doubleword.  */
1063                   if (TYPE_LENGTH (type) == 4)
1064                     {
1065                       memcpy (regval + 4, val, 4);
1066                       p = regval;
1067                     }
1068                   else
1069                     p = val;
1070
1071                   /* Write value in the stack's parameter save area.  */
1072                   write_memory (gparam, p, 8);
1073
1074                   if (freg <= 13)
1075                     regcache_cooked_write (regcache,
1076                                            tdep->ppc_fp0_regnum + freg, p);
1077                 }
1078
1079               freg++;
1080               greg++;
1081               /* Always consume parameter stack space.  */
1082               gparam = align_up (gparam + 8, tdep->wordsize);
1083             }
1084           else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT &&
1085                    TYPE_LENGTH (type) == 16)
1086             {
1087               /* 128-bit decimal floats go in f2 .. f12, always in even/odd
1088                  pairs.  They can end up in memory, using two doublewords.  */
1089               if (write_pass)
1090                 {
1091                   if (freg <= 12)
1092                     {
1093                       /* Make sure freg is even.  */
1094                       freg += freg & 1;
1095                       regcache_cooked_write (regcache,
1096                                              tdep->ppc_fp0_regnum + freg, val);
1097                       regcache_cooked_write (regcache,
1098                           tdep->ppc_fp0_regnum + freg + 1, val + 8);
1099                     }
1100
1101                   write_memory (gparam, val, TYPE_LENGTH (type));
1102                 }
1103
1104               freg += 2;
1105               greg += 2;
1106               gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize);
1107             }
1108           else if (TYPE_LENGTH (type) == 16 && TYPE_VECTOR (type)
1109                    && TYPE_CODE (type) == TYPE_CODE_ARRAY
1110                    && tdep->ppc_vr0_regnum >= 0)
1111             {
1112               /* In the Altivec ABI, vectors go in the vector
1113                  registers v2 .. v13, or when that runs out, a vector
1114                  annex which goes above all the normal parameters.
1115                  NOTE: cagney/2003-09-21: This is a guess based on the
1116                  PowerOpen Altivec ABI.  */
1117               if (vreg <= 13)
1118                 {
1119                   if (write_pass)
1120                     regcache_cooked_write (regcache,
1121                                            tdep->ppc_vr0_regnum + vreg, val);
1122                   vreg++;
1123                 }
1124               else
1125                 {
1126                   if (write_pass)
1127                     write_memory (vparam, val, TYPE_LENGTH (type));
1128                   vparam = align_up (vparam + TYPE_LENGTH (type), 16);
1129                 }
1130             }
1131           else if ((TYPE_CODE (type) == TYPE_CODE_INT
1132                     || TYPE_CODE (type) == TYPE_CODE_ENUM
1133                     || TYPE_CODE (type) == TYPE_CODE_PTR)
1134                    && TYPE_LENGTH (type) <= 8)
1135             {
1136               /* Scalars and Pointers get sign[un]extended and go in
1137                  gpr3 .. gpr10.  They can also end up in memory.  */
1138               if (write_pass)
1139                 {
1140                   /* Sign extend the value, then store it unsigned.  */
1141                   ULONGEST word = unpack_long (type, val);
1142                   /* Convert any function code addresses into
1143                      descriptors.  */
1144                   if (TYPE_CODE (type) == TYPE_CODE_PTR
1145                       && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC)
1146                     {
1147                       CORE_ADDR desc = word;
1148                       convert_code_addr_to_desc_addr (word, &desc);
1149                       word = desc;
1150                     }
1151                   if (greg <= 10)
1152                     regcache_cooked_write_unsigned (regcache,
1153                                                     tdep->ppc_gp0_regnum +
1154                                                     greg, word);
1155                   write_memory_unsigned_integer (gparam, tdep->wordsize,
1156                                                  word);
1157                 }
1158               greg++;
1159               gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize);
1160             }
1161           else
1162             {
1163               int byte;
1164               for (byte = 0; byte < TYPE_LENGTH (type);
1165                    byte += tdep->wordsize)
1166                 {
1167                   if (write_pass && greg <= 10)
1168                     {
1169                       gdb_byte regval[MAX_REGISTER_SIZE];
1170                       int len = TYPE_LENGTH (type) - byte;
1171                       if (len > tdep->wordsize)
1172                         len = tdep->wordsize;
1173                       memset (regval, 0, sizeof regval);
1174                       /* The ABI (version 1.9) specifies that values
1175                          smaller than one doubleword are right-aligned
1176                          and those larger are left-aligned.  GCC
1177                          versions before 3.4 implemented this
1178                          incorrectly; see
1179                          <http://gcc.gnu.org/gcc-3.4/powerpc-abi.html>.  */
1180                       if (byte == 0)
1181                         memcpy (regval + tdep->wordsize - len,
1182                                 val + byte, len);
1183                       else
1184                         memcpy (regval, val + byte, len);
1185                       regcache_cooked_write (regcache, greg, regval);
1186                     }
1187                   greg++;
1188                 }
1189               if (write_pass)
1190                 /* WARNING: cagney/2003-09-21: Strictly speaking, this
1191                    isn't necessary, unfortunately, GCC appears to get
1192                    "struct convention" parameter passing wrong putting
1193                    odd sized structures in memory instead of in a
1194                    register.  Work around this by always writing the
1195                    value to memory.  Fortunately, doing this
1196                    simplifies the code.  */
1197                 write_memory (gparam, val, TYPE_LENGTH (type));
1198               if (freg <= 13
1199                   && TYPE_CODE (type) == TYPE_CODE_STRUCT
1200                   && TYPE_NFIELDS (type) == 1
1201                   && TYPE_LENGTH (type) <= 16)
1202                 {
1203                   /* The ABI (version 1.9) specifies that structs
1204                      containing a single floating-point value, at any
1205                      level of nesting of single-member structs, are
1206                      passed in floating-point registers.  */
1207                   while (TYPE_CODE (type) == TYPE_CODE_STRUCT
1208                          && TYPE_NFIELDS (type) == 1)
1209                     type = check_typedef (TYPE_FIELD_TYPE (type, 0));
1210                   if (TYPE_CODE (type) == TYPE_CODE_FLT)
1211                     {
1212                       if (TYPE_LENGTH (type) <= 8)
1213                         {
1214                           if (write_pass)
1215                             {
1216                               gdb_byte regval[MAX_REGISTER_SIZE];
1217                               struct type *regtype
1218                                 = register_type (gdbarch,
1219                                                  tdep->ppc_fp0_regnum);
1220                               convert_typed_floating (val, type, regval,
1221                                                       regtype);
1222                               regcache_cooked_write (regcache,
1223                                                      (tdep->ppc_fp0_regnum
1224                                                       + freg),
1225                                                      regval);
1226                             }
1227                           freg++;
1228                         }
1229                       else if (TYPE_LENGTH (type) == 16
1230                                && (gdbarch_long_double_format (gdbarch)
1231                                    == floatformats_ibm_long_double))
1232                         {
1233                           if (write_pass)
1234                             {
1235                               regcache_cooked_write (regcache,
1236                                                      (tdep->ppc_fp0_regnum
1237                                                       + freg),
1238                                                      val);
1239                               if (freg <= 12)
1240                                 regcache_cooked_write (regcache,
1241                                                        (tdep->ppc_fp0_regnum
1242                                                         + freg + 1),
1243                                                        val + 8);
1244                             }
1245                           freg += 2;
1246                         }
1247                     }
1248                 }
1249               /* Always consume parameter stack space.  */
1250               gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize);
1251             }
1252         }
1253
1254       if (!write_pass)
1255         {
1256           /* Save the true region sizes ready for the second pass.  */
1257           vparam_size = vparam;
1258           /* Make certain that the general parameter save area is at
1259              least the minimum 8 registers (or doublewords) in size.  */
1260           if (greg < 8)
1261             gparam_size = 8 * tdep->wordsize;
1262           else
1263             gparam_size = gparam;
1264         }
1265     }
1266
1267   /* Update %sp.   */
1268   regcache_cooked_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
1269
1270   /* Write the backchain (it occupies WORDSIZED bytes).  */
1271   write_memory_signed_integer (sp, tdep->wordsize, back_chain);
1272
1273   /* Point the inferior function call's return address at the dummy's
1274      breakpoint.  */
1275   regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
1276
1277   /* Use the func_addr to find the descriptor, and use that to find
1278      the TOC.  */
1279   {
1280     CORE_ADDR desc_addr;
1281     if (convert_code_addr_to_desc_addr (func_addr, &desc_addr))
1282       {
1283         /* The TOC is the second double word in the descriptor.  */
1284         CORE_ADDR toc =
1285           read_memory_unsigned_integer (desc_addr + tdep->wordsize,
1286                                         tdep->wordsize);
1287         regcache_cooked_write_unsigned (regcache,
1288                                         tdep->ppc_gp0_regnum + 2, toc);
1289       }
1290   }
1291
1292   return sp;
1293 }
1294
1295
1296 /* The 64 bit ABI return value convention.
1297
1298    Return non-zero if the return-value is stored in a register, return
1299    0 if the return-value is instead stored on the stack (a.k.a.,
1300    struct return convention).
1301
1302    For a return-value stored in a register: when WRITEBUF is non-NULL,
1303    copy the buffer to the corresponding register return-value location
1304    location; when READBUF is non-NULL, fill the buffer from the
1305    corresponding register return-value location.  */
1306 enum return_value_convention
1307 ppc64_sysv_abi_return_value (struct gdbarch *gdbarch, struct type *valtype,
1308                              struct regcache *regcache, gdb_byte *readbuf,
1309                              const gdb_byte *writebuf)
1310 {
1311   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1312
1313   /* This function exists to support a calling convention that
1314      requires floating-point registers.  It shouldn't be used on
1315      processors that lack them.  */
1316   gdb_assert (ppc_floating_point_unit_p (gdbarch));
1317
1318   /* Floats and doubles in F1.  */
1319   if (TYPE_CODE (valtype) == TYPE_CODE_FLT && TYPE_LENGTH (valtype) <= 8)
1320     {
1321       gdb_byte regval[MAX_REGISTER_SIZE];
1322       struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
1323       if (writebuf != NULL)
1324         {
1325           convert_typed_floating (writebuf, valtype, regval, regtype);
1326           regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
1327         }
1328       if (readbuf != NULL)
1329         {
1330           regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
1331           convert_typed_floating (regval, regtype, readbuf, valtype);
1332         }
1333       return RETURN_VALUE_REGISTER_CONVENTION;
1334     }
1335   if (TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT)
1336     return get_decimal_float_return_value (gdbarch, valtype, regcache, readbuf,
1337                                            writebuf);
1338   /* Integers in r3.  */
1339   if ((TYPE_CODE (valtype) == TYPE_CODE_INT
1340        || TYPE_CODE (valtype) == TYPE_CODE_ENUM)
1341       && TYPE_LENGTH (valtype) <= 8)
1342     {
1343       if (writebuf != NULL)
1344         {
1345           /* Be careful to sign extend the value.  */
1346           regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
1347                                           unpack_long (valtype, writebuf));
1348         }
1349       if (readbuf != NULL)
1350         {
1351           /* Extract the integer from r3.  Since this is truncating the
1352              value, there isn't a sign extension problem.  */
1353           ULONGEST regval;
1354           regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
1355                                          &regval);
1356           store_unsigned_integer (readbuf, TYPE_LENGTH (valtype), regval);
1357         }
1358       return RETURN_VALUE_REGISTER_CONVENTION;
1359     }
1360   /* All pointers live in r3.  */
1361   if (TYPE_CODE (valtype) == TYPE_CODE_PTR)
1362     {
1363       /* All pointers live in r3.  */
1364       if (writebuf != NULL)
1365         regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
1366       if (readbuf != NULL)
1367         regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, readbuf);
1368       return RETURN_VALUE_REGISTER_CONVENTION;
1369     }
1370   /* Array type has more than one use.  */
1371   if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
1372     {
1373       /* Small character arrays are returned, right justified, in r3.  */
1374       if (TYPE_LENGTH (valtype) <= 8
1375         && TYPE_CODE (TYPE_TARGET_TYPE (valtype)) == TYPE_CODE_INT
1376         && TYPE_LENGTH (TYPE_TARGET_TYPE (valtype)) == 1)
1377         {
1378           int offset = (register_size (gdbarch, tdep->ppc_gp0_regnum + 3)
1379                        - TYPE_LENGTH (valtype));
1380           if (writebuf != NULL)
1381            regcache_cooked_write_part (regcache, tdep->ppc_gp0_regnum + 3,
1382                                       offset, TYPE_LENGTH (valtype), writebuf);
1383           if (readbuf != NULL)
1384            regcache_cooked_read_part (regcache, tdep->ppc_gp0_regnum + 3,
1385                                       offset, TYPE_LENGTH (valtype), readbuf);
1386           return RETURN_VALUE_REGISTER_CONVENTION;
1387         }
1388       /* A VMX vector is returned in v2.  */
1389       if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY
1390         && TYPE_VECTOR (valtype) && tdep->ppc_vr0_regnum >= 0)
1391         {
1392           if (readbuf)
1393             regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
1394           if (writebuf)
1395             regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
1396           return RETURN_VALUE_REGISTER_CONVENTION;
1397         }
1398     }
1399   /* Big floating point values get stored in adjacent floating
1400      point registers, starting with F1.  */
1401   if (TYPE_CODE (valtype) == TYPE_CODE_FLT
1402       && (TYPE_LENGTH (valtype) == 16 || TYPE_LENGTH (valtype) == 32))
1403     {
1404       if (writebuf || readbuf != NULL)
1405         {
1406           int i;
1407           for (i = 0; i < TYPE_LENGTH (valtype) / 8; i++)
1408             {
1409               if (writebuf != NULL)
1410                 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1 + i,
1411                                        (const bfd_byte *) writebuf + i * 8);
1412               if (readbuf != NULL)
1413                 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1 + i,
1414                                       (bfd_byte *) readbuf + i * 8);
1415             }
1416         }
1417       return RETURN_VALUE_REGISTER_CONVENTION;
1418     }
1419   /* Complex values get returned in f1:f2, need to convert.  */
1420   if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX
1421       && (TYPE_LENGTH (valtype) == 8 || TYPE_LENGTH (valtype) == 16))
1422     {
1423       if (regcache != NULL)
1424         {
1425           int i;
1426           for (i = 0; i < 2; i++)
1427             {
1428               gdb_byte regval[MAX_REGISTER_SIZE];
1429               struct type *regtype =
1430                 register_type (gdbarch, tdep->ppc_fp0_regnum);
1431               if (writebuf != NULL)
1432                 {
1433                   convert_typed_floating ((const bfd_byte *) writebuf +
1434                                           i * (TYPE_LENGTH (valtype) / 2),
1435                                           valtype, regval, regtype);
1436                   regcache_cooked_write (regcache,
1437                                          tdep->ppc_fp0_regnum + 1 + i,
1438                                          regval);
1439                 }
1440               if (readbuf != NULL)
1441                 {
1442                   regcache_cooked_read (regcache,
1443                                         tdep->ppc_fp0_regnum + 1 + i,
1444                                         regval);
1445                   convert_typed_floating (regval, regtype,
1446                                           (bfd_byte *) readbuf +
1447                                           i * (TYPE_LENGTH (valtype) / 2),
1448                                           valtype);
1449                 }
1450             }
1451         }
1452       return RETURN_VALUE_REGISTER_CONVENTION;
1453     }
1454   /* Big complex values get stored in f1:f4.  */
1455   if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX && TYPE_LENGTH (valtype) == 32)
1456     {
1457       if (regcache != NULL)
1458         {
1459           int i;
1460           for (i = 0; i < 4; i++)
1461             {
1462               if (writebuf != NULL)
1463                 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1 + i,
1464                                        (const bfd_byte *) writebuf + i * 8);
1465               if (readbuf != NULL)
1466                 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1 + i,
1467                                       (bfd_byte *) readbuf + i * 8);
1468             }
1469         }
1470       return RETURN_VALUE_REGISTER_CONVENTION;
1471     }
1472   return RETURN_VALUE_STRUCT_CONVENTION;
1473 }
1474
1475 CORE_ADDR
1476 ppc64_sysv_abi_adjust_breakpoint_address (struct gdbarch *gdbarch,
1477                                           CORE_ADDR bpaddr)
1478 {
1479   /* PPC64 SYSV specifies that the minimal-symbol "FN" should point at
1480      a function-descriptor while the corresponding minimal-symbol
1481      ".FN" should point at the entry point.  Consequently, a command
1482      like "break FN" applied to an object file with only minimal
1483      symbols, will insert the breakpoint into the descriptor at "FN"
1484      and not the function at ".FN".  Avoid this confusion by adjusting
1485      any attempt to set a descriptor breakpoint into a corresponding
1486      function breakpoint.  Note that GDB warns the user when this
1487      adjustment is applied - that's ok as otherwise the user will have
1488      no way of knowing why their breakpoint at "FN" resulted in the
1489      program stopping at ".FN".  */
1490   return gdbarch_convert_from_func_ptr_addr (gdbarch, bpaddr, &current_target);
1491 }