* config/rs6000/tm-rs6000.h (FP0_REGNUM): Document that this
[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 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 #include "defs.h"
24 #include "gdbcore.h"
25 #include "inferior.h"
26 #include "regcache.h"
27 #include "value.h"
28 #include "gdb_string.h"
29 #include "gdb_assert.h"
30 #include "ppc-tdep.h"
31 #include "target.h"
32 #include "objfiles.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, CORE_ADDR func_addr,
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 (current_gdbarch);
52   const CORE_ADDR saved_sp = read_sp ();
53   int argspace = 0;             /* 0 is an initial wrong guess.  */
54   int write_pass;
55
56   /* Go through the argument list twice.
57
58      Pass 1: Figure out how much new stack space is required for
59      arguments and pushed values.  Unlike the PowerOpen ABI, the SysV
60      ABI doesn't reserve any extra space for parameters which are put
61      in registers, but does always push structures and then pass their
62      address.
63
64      Pass 2: Replay the same computation but this time also write the
65      values out to the target.  */
66
67   for (write_pass = 0; write_pass < 2; write_pass++)
68     {
69       int argno;
70       /* Next available floating point register for float and double
71          arguments.  */
72       int freg = 1;
73       /* Next available general register for non-float, non-vector
74          arguments.  */
75       int greg = 3;
76       /* Next available vector register for vector arguments.  */
77       int vreg = 2;
78       /* Arguments start above the "LR save word" and "Back chain".  */
79       int argoffset = 2 * tdep->wordsize;
80       /* Structures start after the arguments.  */
81       int structoffset = argoffset + argspace;
82
83       /* If the function is returning a `struct', then the first word
84          (which will be passed in r3) is used for struct return
85          address.  In that case we should advance one word and start
86          from r4 register to copy parameters.  */
87       if (struct_return)
88         {
89           if (write_pass)
90             regcache_cooked_write_signed (regcache,
91                                           tdep->ppc_gp0_regnum + greg,
92                                           struct_addr);
93           greg++;
94         }
95
96       for (argno = 0; argno < nargs; argno++)
97         {
98           struct value *arg = args[argno];
99           struct type *type = check_typedef (VALUE_TYPE (arg));
100           int len = TYPE_LENGTH (type);
101           char *val = VALUE_CONTENTS (arg);
102
103           if (TYPE_CODE (type) == TYPE_CODE_FLT
104               && ppc_floating_point_unit_p (current_gdbarch) && len <= 8)
105             {
106               /* Floating point value converted to "double" then
107                  passed in an FP register, when the registers run out,
108                  8 byte aligned stack is used.  */
109               if (freg <= 8)
110                 {
111                   if (write_pass)
112                     {
113                       /* Always store the floating point value using
114                          the register's floating-point format.  */
115                       char regval[MAX_REGISTER_SIZE];
116                       struct type *regtype
117                         = register_type (gdbarch, tdep->ppc_fp0_regnum + freg);
118                       convert_typed_floating (val, type, regval, regtype);
119                       regcache_cooked_write (regcache,
120                                              tdep->ppc_fp0_regnum + freg,
121                                              regval);
122                     }
123                   freg++;
124                 }
125               else
126                 {
127                   /* SysV ABI converts floats to doubles before
128                      writing them to an 8 byte aligned stack location.  */
129                   argoffset = align_up (argoffset, 8);
130                   if (write_pass)
131                     {
132                       char memval[8];
133                       struct type *memtype;
134                       switch (TARGET_BYTE_ORDER)
135                         {
136                         case BFD_ENDIAN_BIG:
137                           memtype = builtin_type_ieee_double_big;
138                           break;
139                         case BFD_ENDIAN_LITTLE:
140                           memtype = builtin_type_ieee_double_little;
141                           break;
142                         default:
143                           internal_error (__FILE__, __LINE__, "bad switch");
144                         }
145                       convert_typed_floating (val, type, memval, memtype);
146                       write_memory (sp + argoffset, val, len);
147                     }
148                   argoffset += 8;
149                 }
150             }
151           else if (len == 8 && (TYPE_CODE (type) == TYPE_CODE_INT       /* long long */
152                                 || (!ppc_floating_point_unit_p (current_gdbarch) && TYPE_CODE (type) == TYPE_CODE_FLT)))        /* double */
153             {
154               /* "long long" or "double" passed in an odd/even
155                  register pair with the low addressed word in the odd
156                  register and the high addressed word in the even
157                  register, or when the registers run out an 8 byte
158                  aligned stack location.  */
159               if (greg > 9)
160                 {
161                   /* Just in case GREG was 10.  */
162                   greg = 11;
163                   argoffset = align_up (argoffset, 8);
164                   if (write_pass)
165                     write_memory (sp + argoffset, val, len);
166                   argoffset += 8;
167                 }
168               else if (tdep->wordsize == 8)
169                 {
170                   if (write_pass)
171                     regcache_cooked_write (regcache,
172                                            tdep->ppc_gp0_regnum + greg, val);
173                   greg += 1;
174                 }
175               else
176                 {
177                   /* Must start on an odd register - r3/r4 etc.  */
178                   if ((greg & 1) == 0)
179                     greg++;
180                   if (write_pass)
181                     {
182                       regcache_cooked_write (regcache,
183                                              tdep->ppc_gp0_regnum + greg + 0,
184                                              val + 0);
185                       regcache_cooked_write (regcache,
186                                              tdep->ppc_gp0_regnum + greg + 1,
187                                              val + 4);
188                     }
189                   greg += 2;
190                 }
191             }
192           else if (len == 16
193                    && TYPE_CODE (type) == TYPE_CODE_ARRAY
194                    && TYPE_VECTOR (type) && tdep->ppc_vr0_regnum >= 0)
195             {
196               /* Vector parameter passed in an Altivec register, or
197                  when that runs out, 16 byte aligned stack location.  */
198               if (vreg <= 13)
199                 {
200                   if (write_pass)
201                     regcache_cooked_write (current_regcache,
202                                            tdep->ppc_vr0_regnum + vreg, val);
203                   vreg++;
204                 }
205               else
206                 {
207                   argoffset = align_up (argoffset, 16);
208                   if (write_pass)
209                     write_memory (sp + argoffset, val, 16);
210                   argoffset += 16;
211                 }
212             }
213           else if (len == 8
214                    && TYPE_CODE (type) == TYPE_CODE_ARRAY
215                    && TYPE_VECTOR (type) && tdep->ppc_ev0_regnum >= 0)
216             {
217               /* Vector parameter passed in an e500 register, or when
218                  that runs out, 8 byte aligned stack location.  Note
219                  that since e500 vector and general purpose registers
220                  both map onto the same underlying register set, a
221                  "greg" and not a "vreg" is consumed here.  A cooked
222                  write stores the value in the correct locations
223                  within the raw register cache.  */
224               if (greg <= 10)
225                 {
226                   if (write_pass)
227                     regcache_cooked_write (current_regcache,
228                                            tdep->ppc_ev0_regnum + greg, val);
229                   greg++;
230                 }
231               else
232                 {
233                   argoffset = align_up (argoffset, 8);
234                   if (write_pass)
235                     write_memory (sp + argoffset, val, 8);
236                   argoffset += 8;
237                 }
238             }
239           else
240             {
241               /* Reduce the parameter down to something that fits in a
242                  "word".  */
243               char word[MAX_REGISTER_SIZE];
244               memset (word, 0, MAX_REGISTER_SIZE);
245               if (len > tdep->wordsize
246                   || TYPE_CODE (type) == TYPE_CODE_STRUCT
247                   || TYPE_CODE (type) == TYPE_CODE_UNION)
248                 {
249                   /* Structs and large values are put on an 8 byte
250                      aligned stack ... */
251                   structoffset = align_up (structoffset, 8);
252                   if (write_pass)
253                     write_memory (sp + structoffset, val, len);
254                   /* ... and then a "word" pointing to that address is
255                      passed as the parameter.  */
256                   store_unsigned_integer (word, tdep->wordsize,
257                                           sp + structoffset);
258                   structoffset += len;
259                 }
260               else if (TYPE_CODE (type) == TYPE_CODE_INT)
261                 /* Sign or zero extend the "int" into a "word".  */
262                 store_unsigned_integer (word, tdep->wordsize,
263                                         unpack_long (type, val));
264               else
265                 /* Always goes in the low address.  */
266                 memcpy (word, val, len);
267               /* Store that "word" in a register, or on the stack.
268                  The words have "4" byte alignment.  */
269               if (greg <= 10)
270                 {
271                   if (write_pass)
272                     regcache_cooked_write (regcache,
273                                            tdep->ppc_gp0_regnum + greg, word);
274                   greg++;
275                 }
276               else
277                 {
278                   argoffset = align_up (argoffset, tdep->wordsize);
279                   if (write_pass)
280                     write_memory (sp + argoffset, word, tdep->wordsize);
281                   argoffset += tdep->wordsize;
282                 }
283             }
284         }
285
286       /* Compute the actual stack space requirements.  */
287       if (!write_pass)
288         {
289           /* Remember the amount of space needed by the arguments.  */
290           argspace = argoffset;
291           /* Allocate space for both the arguments and the structures.  */
292           sp -= (argoffset + structoffset);
293           /* Ensure that the stack is still 16 byte aligned.  */
294           sp = align_down (sp, 16);
295         }
296     }
297
298   /* Update %sp.   */
299   regcache_cooked_write_signed (regcache, SP_REGNUM, sp);
300
301   /* Write the backchain (it occupies WORDSIZED bytes).  */
302   write_memory_signed_integer (sp, tdep->wordsize, saved_sp);
303
304   /* Point the inferior function call's return address at the dummy's
305      breakpoint.  */
306   regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
307
308   return sp;
309 }
310
311 /* Handle the return-value conventions specified by the SysV 32-bit
312    PowerPC ABI (including all the supplements):
313
314    no floating-point: floating-point values returned using 32-bit
315    general-purpose registers.
316
317    Altivec: 128-bit vectors returned using vector registers.
318
319    e500: 64-bit vectors returned using the full full 64 bit EV
320    register, floating-point values returned using 32-bit
321    general-purpose registers.
322
323    GCC (broken): Small struct values right (instead of left) aligned
324    when returned in general-purpose registers.  */
325
326 static enum return_value_convention
327 do_ppc_sysv_return_value (struct gdbarch *gdbarch, struct type *type,
328                           struct regcache *regcache, void *readbuf,
329                           const void *writebuf, int broken_gcc)
330 {
331   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
332   gdb_assert (tdep->wordsize == 4);
333   if (TYPE_CODE (type) == TYPE_CODE_FLT
334       && TYPE_LENGTH (type) <= 8
335       && ppc_floating_point_unit_p (gdbarch))
336     {
337       if (readbuf)
338         {
339           /* Floats and doubles stored in "f1".  Convert the value to
340              the required type.  */
341           char regval[MAX_REGISTER_SIZE];
342           struct type *regtype = register_type (gdbarch,
343                                                 tdep->ppc_fp0_regnum + 1);
344           regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
345           convert_typed_floating (regval, regtype, readbuf, type);
346         }
347       if (writebuf)
348         {
349           /* Floats and doubles stored in "f1".  Convert the value to
350              the register's "double" type.  */
351           char regval[MAX_REGISTER_SIZE];
352           struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
353           convert_typed_floating (writebuf, type, regval, regtype);
354           regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
355         }
356       return RETURN_VALUE_REGISTER_CONVENTION;
357     }
358   if ((TYPE_CODE (type) == TYPE_CODE_INT && TYPE_LENGTH (type) == 8)
359       || (TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8))
360     {
361       if (readbuf)
362         {
363           /* A long long, or a double stored in the 32 bit r3/r4.  */
364           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
365                                 (bfd_byte *) readbuf + 0);
366           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
367                                 (bfd_byte *) readbuf + 4);
368         }
369       if (writebuf)
370         {
371           /* A long long, or a double stored in the 32 bit r3/r4.  */
372           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
373                                  (const bfd_byte *) writebuf + 0);
374           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
375                                  (const bfd_byte *) writebuf + 4);
376         }
377       return RETURN_VALUE_REGISTER_CONVENTION;
378     }
379   if (TYPE_CODE (type) == TYPE_CODE_INT
380       && TYPE_LENGTH (type) <= tdep->wordsize)
381     {
382       if (readbuf)
383         {
384           /* Some sort of integer stored in r3.  Since TYPE isn't
385              bigger than the register, sign extension isn't a problem
386              - just do everything unsigned.  */
387           ULONGEST regval;
388           regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
389                                          &regval);
390           store_unsigned_integer (readbuf, TYPE_LENGTH (type), regval);
391         }
392       if (writebuf)
393         {
394           /* Some sort of integer stored in r3.  Use unpack_long since
395              that should handle any required sign extension.  */
396           regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
397                                           unpack_long (type, writebuf));
398         }
399       return RETURN_VALUE_REGISTER_CONVENTION;
400     }
401   if (TYPE_LENGTH (type) == 16
402       && TYPE_CODE (type) == TYPE_CODE_ARRAY
403       && TYPE_VECTOR (type) && tdep->ppc_vr0_regnum >= 0)
404     {
405       if (readbuf)
406         {
407           /* Altivec places the return value in "v2".  */
408           regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
409         }
410       if (writebuf)
411         {
412           /* Altivec places the return value in "v2".  */
413           regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
414         }
415       return RETURN_VALUE_REGISTER_CONVENTION;
416     }
417   if (TYPE_LENGTH (type) == 8
418       && TYPE_CODE (type) == TYPE_CODE_ARRAY
419       && TYPE_VECTOR (type) && tdep->ppc_ev0_regnum >= 0)
420     {
421       /* The e500 ABI places return values for the 64-bit DSP types
422          (__ev64_opaque__) in r3.  However, in GDB-speak, ev3
423          corresponds to the entire r3 value for e500, whereas GDB's r3
424          only corresponds to the least significant 32-bits.  So place
425          the 64-bit DSP type's value in ev3.  */
426       if (readbuf)
427         regcache_cooked_read (regcache, tdep->ppc_ev0_regnum + 3, readbuf);
428       if (writebuf)
429         regcache_cooked_write (regcache, tdep->ppc_ev0_regnum + 3, writebuf);
430       return RETURN_VALUE_REGISTER_CONVENTION;
431     }
432   if (broken_gcc && TYPE_LENGTH (type) <= 8)
433     {
434       if (readbuf)
435         {
436           /* GCC screwed up.  The last register isn't "left" aligned.
437              Need to extract the least significant part of each
438              register and then store that.  */
439           /* Transfer any full words.  */
440           int word = 0;
441           while (1)
442             {
443               ULONGEST reg;
444               int len = TYPE_LENGTH (type) - word * tdep->wordsize;
445               if (len <= 0)
446                 break;
447               if (len > tdep->wordsize)
448                 len = tdep->wordsize;
449               regcache_cooked_read_unsigned (regcache,
450                                              tdep->ppc_gp0_regnum + 3 + word,
451                                              &reg);
452               store_unsigned_integer (((bfd_byte *) readbuf
453                                        + word * tdep->wordsize), len, reg);
454               word++;
455             }
456         }
457       if (writebuf)
458         {
459           /* GCC screwed up.  The last register isn't "left" aligned.
460              Need to extract the least significant part of each
461              register and then store that.  */
462           /* Transfer any full words.  */
463           int word = 0;
464           while (1)
465             {
466               ULONGEST reg;
467               int len = TYPE_LENGTH (type) - word * tdep->wordsize;
468               if (len <= 0)
469                 break;
470               if (len > tdep->wordsize)
471                 len = tdep->wordsize;
472               reg = extract_unsigned_integer (((const bfd_byte *) writebuf
473                                                + word * tdep->wordsize), len);
474               regcache_cooked_write_unsigned (regcache,
475                                               tdep->ppc_gp0_regnum + 3 + word,
476                                               reg);
477               word++;
478             }
479         }
480       return RETURN_VALUE_REGISTER_CONVENTION;
481     }
482   if (TYPE_LENGTH (type) <= 8)
483     {
484       if (readbuf)
485         {
486           /* This matches SVr4 PPC, it does not match GCC.  */
487           /* The value is right-padded to 8 bytes and then loaded, as
488              two "words", into r3/r4.  */
489           char regvals[MAX_REGISTER_SIZE * 2];
490           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
491                                 regvals + 0 * tdep->wordsize);
492           if (TYPE_LENGTH (type) > tdep->wordsize)
493             regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
494                                   regvals + 1 * tdep->wordsize);
495           memcpy (readbuf, regvals, TYPE_LENGTH (type));
496         }
497       if (writebuf)
498         {
499           /* This matches SVr4 PPC, it does not match GCC.  */
500           /* The value is padded out to 8 bytes and then loaded, as
501              two "words" into r3/r4.  */
502           char regvals[MAX_REGISTER_SIZE * 2];
503           memset (regvals, 0, sizeof regvals);
504           memcpy (regvals, writebuf, TYPE_LENGTH (type));
505           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
506                                  regvals + 0 * tdep->wordsize);
507           if (TYPE_LENGTH (type) > tdep->wordsize)
508             regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
509                                    regvals + 1 * tdep->wordsize);
510         }
511       return RETURN_VALUE_REGISTER_CONVENTION;
512     }
513   return RETURN_VALUE_STRUCT_CONVENTION;
514 }
515
516 enum return_value_convention
517 ppc_sysv_abi_return_value (struct gdbarch *gdbarch, struct type *valtype,
518                            struct regcache *regcache, void *readbuf,
519                            const void *writebuf)
520 {
521   return do_ppc_sysv_return_value (gdbarch, valtype, regcache, readbuf,
522                                    writebuf, 0);
523 }
524
525 enum return_value_convention
526 ppc_sysv_abi_broken_return_value (struct gdbarch *gdbarch,
527                                   struct type *valtype,
528                                   struct regcache *regcache,
529                                   void *readbuf, const void *writebuf)
530 {
531   return do_ppc_sysv_return_value (gdbarch, valtype, regcache, readbuf,
532                                    writebuf, 1);
533 }
534
535 /* Pass the arguments in either registers, or in the stack. Using the
536    ppc 64 bit SysV ABI.
537
538    This implements a dumbed down version of the ABI.  It always writes
539    values to memory, GPR and FPR, even when not necessary.  Doing this
540    greatly simplifies the logic. */
541
542 CORE_ADDR
543 ppc64_sysv_abi_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr,
544                                 struct regcache *regcache, CORE_ADDR bp_addr,
545                                 int nargs, struct value **args, CORE_ADDR sp,
546                                 int struct_return, CORE_ADDR struct_addr)
547 {
548   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
549   /* By this stage in the proceedings, SP has been decremented by "red
550      zone size" + "struct return size".  Fetch the stack-pointer from
551      before this and use that as the BACK_CHAIN.  */
552   const CORE_ADDR back_chain = read_sp ();
553   /* See for-loop comment below.  */
554   int write_pass;
555   /* Size of the Altivec's vector parameter region, the final value is
556      computed in the for-loop below.  */
557   LONGEST vparam_size = 0;
558   /* Size of the general parameter region, the final value is computed
559      in the for-loop below.  */
560   LONGEST gparam_size = 0;
561   /* Kevin writes ... I don't mind seeing tdep->wordsize used in the
562      calls to align_up(), align_down(), etc.  because this makes it
563      easier to reuse this code (in a copy/paste sense) in the future,
564      but it is a 64-bit ABI and asserting that the wordsize is 8 bytes
565      at some point makes it easier to verify that this function is
566      correct without having to do a non-local analysis to figure out
567      the possible values of tdep->wordsize.  */
568   gdb_assert (tdep->wordsize == 8);
569
570   /* Go through the argument list twice.
571
572      Pass 1: Compute the function call's stack space and register
573      requirements.
574
575      Pass 2: Replay the same computation but this time also write the
576      values out to the target.  */
577
578   for (write_pass = 0; write_pass < 2; write_pass++)
579     {
580       int argno;
581       /* Next available floating point register for float and double
582          arguments.  */
583       int freg = 1;
584       /* Next available general register for non-vector (but possibly
585          float) arguments.  */
586       int greg = 3;
587       /* Next available vector register for vector arguments.  */
588       int vreg = 2;
589       /* The address, at which the next general purpose parameter
590          (integer, struct, float, ...) should be saved.  */
591       CORE_ADDR gparam;
592       /* Address, at which the next Altivec vector parameter should be
593          saved.  */
594       CORE_ADDR vparam;
595
596       if (!write_pass)
597         {
598           /* During the first pass, GPARAM and VPARAM are more like
599              offsets (start address zero) than addresses.  That way
600              the accumulate the total stack space each region
601              requires.  */
602           gparam = 0;
603           vparam = 0;
604         }
605       else
606         {
607           /* Decrement the stack pointer making space for the Altivec
608              and general on-stack parameters.  Set vparam and gparam
609              to their corresponding regions.  */
610           vparam = align_down (sp - vparam_size, 16);
611           gparam = align_down (vparam - gparam_size, 16);
612           /* Add in space for the TOC, link editor double word,
613              compiler double word, LR save area, CR save area.  */
614           sp = align_down (gparam - 48, 16);
615         }
616
617       /* If the function is returning a `struct', then there is an
618          extra hidden parameter (which will be passed in r3)
619          containing the address of that struct..  In that case we
620          should advance one word and start from r4 register to copy
621          parameters.  This also consumes one on-stack parameter slot.  */
622       if (struct_return)
623         {
624           if (write_pass)
625             regcache_cooked_write_signed (regcache,
626                                           tdep->ppc_gp0_regnum + greg,
627                                           struct_addr);
628           greg++;
629           gparam = align_up (gparam + tdep->wordsize, tdep->wordsize);
630         }
631
632       for (argno = 0; argno < nargs; argno++)
633         {
634           struct value *arg = args[argno];
635           struct type *type = check_typedef (VALUE_TYPE (arg));
636           char *val = VALUE_CONTENTS (arg);
637           if (TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) <= 8)
638             {
639               /* Floats and Doubles go in f1 .. f13.  They also
640                  consume a left aligned GREG,, and can end up in
641                  memory.  */
642               if (write_pass)
643                 {
644                   if (ppc_floating_point_unit_p (current_gdbarch)
645                       && freg <= 13)
646                     {
647                       char regval[MAX_REGISTER_SIZE];
648                       struct type *regtype
649                         = register_type (gdbarch, tdep->ppc_fp0_regnum);
650                       convert_typed_floating (val, type, regval, regtype);
651                       regcache_cooked_write (regcache,
652                                              tdep->ppc_fp0_regnum + freg,
653                                              regval);
654                     }
655                   if (greg <= 10)
656                     {
657                       /* The ABI states "Single precision floating
658                          point values are mapped to the first word in
659                          a single doubleword" and "... floating point
660                          values mapped to the first eight doublewords
661                          of the parameter save area are also passed in
662                          general registers").
663
664                          This code interprets that to mean: store it,
665                          left aligned, in the general register.  */
666                       char regval[MAX_REGISTER_SIZE];
667                       memset (regval, 0, sizeof regval);
668                       memcpy (regval, val, TYPE_LENGTH (type));
669                       regcache_cooked_write (regcache,
670                                              tdep->ppc_gp0_regnum + greg,
671                                              regval);
672                     }
673                   write_memory (gparam, val, TYPE_LENGTH (type));
674                 }
675               /* Always consume parameter stack space.  */
676               freg++;
677               greg++;
678               gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize);
679             }
680           else if (TYPE_LENGTH (type) == 16 && TYPE_VECTOR (type)
681                    && TYPE_CODE (type) == TYPE_CODE_ARRAY
682                    && tdep->ppc_vr0_regnum >= 0)
683             {
684               /* In the Altivec ABI, vectors go in the vector
685                  registers v2 .. v13, or when that runs out, a vector
686                  annex which goes above all the normal parameters.
687                  NOTE: cagney/2003-09-21: This is a guess based on the
688                  PowerOpen Altivec ABI.  */
689               if (vreg <= 13)
690                 {
691                   if (write_pass)
692                     regcache_cooked_write (regcache,
693                                            tdep->ppc_vr0_regnum + vreg, val);
694                   vreg++;
695                 }
696               else
697                 {
698                   if (write_pass)
699                     write_memory (vparam, val, TYPE_LENGTH (type));
700                   vparam = align_up (vparam + TYPE_LENGTH (type), 16);
701                 }
702             }
703           else if ((TYPE_CODE (type) == TYPE_CODE_INT
704                     || TYPE_CODE (type) == TYPE_CODE_ENUM)
705                    && TYPE_LENGTH (type) <= 8)
706             {
707               /* Scalars get sign[un]extended and go in gpr3 .. gpr10.
708                  They can also end up in memory.  */
709               if (write_pass)
710                 {
711                   /* Sign extend the value, then store it unsigned.  */
712                   ULONGEST word = unpack_long (type, val);
713                   if (greg <= 10)
714                     regcache_cooked_write_unsigned (regcache,
715                                                     tdep->ppc_gp0_regnum +
716                                                     greg, word);
717                   write_memory_unsigned_integer (gparam, tdep->wordsize,
718                                                  word);
719                 }
720               greg++;
721               gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize);
722             }
723           else
724             {
725               int byte;
726               for (byte = 0; byte < TYPE_LENGTH (type);
727                    byte += tdep->wordsize)
728                 {
729                   if (write_pass && greg <= 10)
730                     {
731                       char regval[MAX_REGISTER_SIZE];
732                       int len = TYPE_LENGTH (type) - byte;
733                       if (len > tdep->wordsize)
734                         len = tdep->wordsize;
735                       memset (regval, 0, sizeof regval);
736                       /* WARNING: cagney/2003-09-21: As best I can
737                          tell, the ABI specifies that the value should
738                          be left aligned.  Unfortunately, GCC doesn't
739                          do this - it instead right aligns even sized
740                          values and puts odd sized values on the
741                          stack.  Work around that by putting both a
742                          left and right aligned value into the
743                          register (hopefully no one notices :-^).
744                          Arrrgh!  */
745                       /* Left aligned (8 byte values such as pointers
746                          fill the buffer).  */
747                       memcpy (regval, val + byte, len);
748                       /* Right aligned (but only if even).  */
749                       if (len == 1 || len == 2 || len == 4)
750                         memcpy (regval + tdep->wordsize - len,
751                                 val + byte, len);
752                       regcache_cooked_write (regcache, greg, regval);
753                     }
754                   greg++;
755                 }
756               if (write_pass)
757                 /* WARNING: cagney/2003-09-21: Strictly speaking, this
758                    isn't necessary, unfortunately, GCC appears to get
759                    "struct convention" parameter passing wrong putting
760                    odd sized structures in memory instead of in a
761                    register.  Work around this by always writing the
762                    value to memory.  Fortunately, doing this
763                    simplifies the code.  */
764                 write_memory (gparam, val, TYPE_LENGTH (type));
765               /* Always consume parameter stack space.  */
766               gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize);
767             }
768         }
769
770       if (!write_pass)
771         {
772           /* Save the true region sizes ready for the second pass.  */
773           vparam_size = vparam;
774           /* Make certain that the general parameter save area is at
775              least the minimum 8 registers (or doublewords) in size.  */
776           if (greg < 8)
777             gparam_size = 8 * tdep->wordsize;
778           else
779             gparam_size = gparam;
780         }
781     }
782
783   /* Update %sp.   */
784   regcache_cooked_write_signed (regcache, SP_REGNUM, sp);
785
786   /* Write the backchain (it occupies WORDSIZED bytes).  */
787   write_memory_signed_integer (sp, tdep->wordsize, back_chain);
788
789   /* Point the inferior function call's return address at the dummy's
790      breakpoint.  */
791   regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
792
793   /* Find a value for the TOC register.  Every symbol should have both
794      ".FN" and "FN" in the minimal symbol table.  "FN" points at the
795      FN's descriptor, while ".FN" points at the entry point (which
796      matches FUNC_ADDR).  Need to reverse from FUNC_ADDR back to the
797      FN's descriptor address (while at the same time being careful to
798      find "FN" in the same object file as ".FN").  */
799   {
800     /* Find the minimal symbol that corresponds to FUNC_ADDR (should
801        have the name ".FN").  */
802     struct minimal_symbol *dot_fn = lookup_minimal_symbol_by_pc (func_addr);
803     if (dot_fn != NULL && SYMBOL_LINKAGE_NAME (dot_fn)[0] == '.')
804       {
805         /* Get the section that contains FUNC_ADR.  Need this for the
806            "objfile" that it contains.  */
807         struct obj_section *dot_fn_section = find_pc_section (func_addr);
808         if (dot_fn_section != NULL && dot_fn_section->objfile != NULL)
809           {
810             /* Now find the corresponding "FN" (dropping ".") minimal
811                symbol's address.  Only look for the minimal symbol in
812                ".FN"'s object file - avoids problems when two object
813                files (i.e., shared libraries) contain a minimal symbol
814                with the same name.  */
815             struct minimal_symbol *fn =
816               lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (dot_fn) + 1, NULL,
817                                      dot_fn_section->objfile);
818             if (fn != NULL)
819               {
820                 /* Got the address of that descriptor.  The TOC is the
821                    second double word.  */
822                 CORE_ADDR toc =
823                   read_memory_unsigned_integer (SYMBOL_VALUE_ADDRESS (fn)
824                                                 + tdep->wordsize,
825                                                 tdep->wordsize);
826                 regcache_cooked_write_unsigned (regcache,
827                                                 tdep->ppc_gp0_regnum + 2, toc);
828               }
829           }
830       }
831   }
832
833   return sp;
834 }
835
836
837 /* The 64 bit ABI retun value convention.
838
839    Return non-zero if the return-value is stored in a register, return
840    0 if the return-value is instead stored on the stack (a.k.a.,
841    struct return convention).
842
843    For a return-value stored in a register: when WRITEBUF is non-NULL,
844    copy the buffer to the corresponding register return-value location
845    location; when READBUF is non-NULL, fill the buffer from the
846    corresponding register return-value location.  */
847 enum return_value_convention
848 ppc64_sysv_abi_return_value (struct gdbarch *gdbarch, struct type *valtype,
849                              struct regcache *regcache, void *readbuf,
850                              const void *writebuf)
851 {
852   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
853   /* Floats and doubles in F1.  */
854   if (TYPE_CODE (valtype) == TYPE_CODE_FLT && TYPE_LENGTH (valtype) <= 8)
855     {
856       char regval[MAX_REGISTER_SIZE];
857       struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
858       if (writebuf != NULL)
859         {
860           convert_typed_floating (writebuf, valtype, regval, regtype);
861           regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
862         }
863       if (readbuf != NULL)
864         {
865           regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
866           convert_typed_floating (regval, regtype, readbuf, valtype);
867         }
868       return RETURN_VALUE_REGISTER_CONVENTION;
869     }
870   if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 8)
871     {
872       /* Integers in r3.  */
873       if (writebuf != NULL)
874         {
875           /* Be careful to sign extend the value.  */
876           regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
877                                           unpack_long (valtype, writebuf));
878         }
879       if (readbuf != NULL)
880         {
881           /* Extract the integer from r3.  Since this is truncating the
882              value, there isn't a sign extension problem.  */
883           ULONGEST regval;
884           regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
885                                          &regval);
886           store_unsigned_integer (readbuf, TYPE_LENGTH (valtype), regval);
887         }
888       return RETURN_VALUE_REGISTER_CONVENTION;
889     }
890   /* All pointers live in r3.  */
891   if (TYPE_CODE (valtype) == TYPE_CODE_PTR)
892     {
893       /* All pointers live in r3.  */
894       if (writebuf != NULL)
895         regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
896       if (readbuf != NULL)
897         regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, readbuf);
898       return RETURN_VALUE_REGISTER_CONVENTION;
899     }
900   if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY
901       && TYPE_LENGTH (valtype) <= 8
902       && TYPE_CODE (TYPE_TARGET_TYPE (valtype)) == TYPE_CODE_INT
903       && TYPE_LENGTH (TYPE_TARGET_TYPE (valtype)) == 1)
904     {
905       /* Small character arrays are returned, right justified, in r3.  */
906       int offset = (register_size (gdbarch, tdep->ppc_gp0_regnum + 3)
907                     - TYPE_LENGTH (valtype));
908       if (writebuf != NULL)
909         regcache_cooked_write_part (regcache, tdep->ppc_gp0_regnum + 3,
910                                     offset, TYPE_LENGTH (valtype), writebuf);
911       if (readbuf != NULL)
912         regcache_cooked_read_part (regcache, tdep->ppc_gp0_regnum + 3,
913                                    offset, TYPE_LENGTH (valtype), readbuf);
914       return RETURN_VALUE_REGISTER_CONVENTION;
915     }
916   /* Big floating point values get stored in adjacent floating
917      point registers.  */
918   if (TYPE_CODE (valtype) == TYPE_CODE_FLT
919       && (TYPE_LENGTH (valtype) == 16 || TYPE_LENGTH (valtype) == 32))
920     {
921       if (writebuf || readbuf != NULL)
922         {
923           int i;
924           for (i = 0; i < TYPE_LENGTH (valtype) / 8; i++)
925             {
926               if (writebuf != NULL)
927                 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1 + i,
928                                        (const bfd_byte *) writebuf + i * 8);
929               if (readbuf != NULL)
930                 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1 + i,
931                                       (bfd_byte *) readbuf + i * 8);
932             }
933         }
934       return RETURN_VALUE_REGISTER_CONVENTION;
935     }
936   /* Complex values get returned in f1:f2, need to convert.  */
937   if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX
938       && (TYPE_LENGTH (valtype) == 8 || TYPE_LENGTH (valtype) == 16))
939     {
940       if (regcache != NULL)
941         {
942           int i;
943           for (i = 0; i < 2; i++)
944             {
945               char regval[MAX_REGISTER_SIZE];
946               struct type *regtype =
947                 register_type (current_gdbarch, tdep->ppc_fp0_regnum);
948               if (writebuf != NULL)
949                 {
950                   convert_typed_floating ((const bfd_byte *) writebuf +
951                                           i * (TYPE_LENGTH (valtype) / 2),
952                                           valtype, regval, regtype);
953                   regcache_cooked_write (regcache,
954                                          tdep->ppc_fp0_regnum + 1 + i,
955                                          regval);
956                 }
957               if (readbuf != NULL)
958                 {
959                   regcache_cooked_read (regcache,
960                                         tdep->ppc_fp0_regnum + 1 + i,
961                                         regval);
962                   convert_typed_floating (regval, regtype,
963                                           (bfd_byte *) readbuf +
964                                           i * (TYPE_LENGTH (valtype) / 2),
965                                           valtype);
966                 }
967             }
968         }
969       return RETURN_VALUE_REGISTER_CONVENTION;
970     }
971   /* Big complex values get stored in f1:f4.  */
972   if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX && TYPE_LENGTH (valtype) == 32)
973     {
974       if (regcache != NULL)
975         {
976           int i;
977           for (i = 0; i < 4; i++)
978             {
979               if (writebuf != NULL)
980                 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1 + i,
981                                        (const bfd_byte *) writebuf + i * 8);
982               if (readbuf != NULL)
983                 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1 + i,
984                                       (bfd_byte *) readbuf + i * 8);
985             }
986         }
987       return RETURN_VALUE_REGISTER_CONVENTION;
988     }
989   return RETURN_VALUE_STRUCT_CONVENTION;
990 }
991
992 CORE_ADDR
993 ppc64_sysv_abi_adjust_breakpoint_address (struct gdbarch *gdbarch,
994                                           CORE_ADDR bpaddr)
995 {
996   /* PPC64 SYSV specifies that the minimal-symbol "FN" should point at
997      a function-descriptor while the corresponding minimal-symbol
998      ".FN" should point at the entry point.  Consequently, a command
999      like "break FN" applied to an object file with only minimal
1000      symbols, will insert the breakpoint into the descriptor at "FN"
1001      and not the function at ".FN".  Avoid this confusion by adjusting
1002      any attempt to set a descriptor breakpoint into a corresponding
1003      function breakpoint.  Note that GDB warns the user when this
1004      adjustment is applied - that's ok as otherwise the user will have
1005      no way of knowing why their breakpoint at "FN" resulted in the
1006      program stopping at ".FN".  */
1007   return gdbarch_convert_from_func_ptr_addr (gdbarch, bpaddr, &current_target);
1008 }