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