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