1 /* Native support code for PPC AIX, for GDB the GNU debugger.
3 Copyright (C) 2006-2013 Free Software Foundation, Inc.
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdb_string.h"
24 #include "gdb_assert.h"
34 #include "breakpoint.h"
35 #include "rs6000-tdep.h"
37 #include "exceptions.h"
38 #include "xcoffread.h"
40 #include "solib-aix.h"
42 /* If the kernel has to deliver a signal, it pushes a sigcontext
43 structure on the stack and then calls the signal handler, passing
44 the address of the sigcontext in an argument register. Usually
45 the signal handler doesn't save this register, so we have to
46 access the sigcontext structure via an offset from the signal handler
48 The following constants were determined by experimentation on AIX 3.2. */
49 #define SIG_FRAME_PC_OFFSET 96
50 #define SIG_FRAME_LR_OFFSET 108
51 #define SIG_FRAME_FP_OFFSET 284
54 /* Core file support. */
56 static struct ppc_reg_offsets rs6000_aix32_reg_offsets =
58 /* General-purpose registers. */
70 /* Floating-point registers. */
72 56, /* fpscr_offset */
75 /* AltiVec registers. */
78 -1 /* vrsave_offset */
81 static struct ppc_reg_offsets rs6000_aix64_reg_offsets =
83 /* General-purpose registers. */
95 /* Floating-point registers. */
97 296, /* fpscr_offset */
100 /* AltiVec registers. */
102 -1, /* vscr_offset */
103 -1 /* vrsave_offset */
107 /* Supply register REGNUM in the general-purpose register set REGSET
108 from the buffer specified by GREGS and LEN to register cache
109 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
112 rs6000_aix_supply_regset (const struct regset *regset,
113 struct regcache *regcache, int regnum,
114 const void *gregs, size_t len)
116 ppc_supply_gregset (regset, regcache, regnum, gregs, len);
117 ppc_supply_fpregset (regset, regcache, regnum, gregs, len);
120 /* Collect register REGNUM in the general-purpose register set
121 REGSET, from register cache REGCACHE into the buffer specified by
122 GREGS and LEN. If REGNUM is -1, do this for all registers in
126 rs6000_aix_collect_regset (const struct regset *regset,
127 const struct regcache *regcache, int regnum,
128 void *gregs, size_t len)
130 ppc_collect_gregset (regset, regcache, regnum, gregs, len);
131 ppc_collect_fpregset (regset, regcache, regnum, gregs, len);
134 /* AIX register set. */
136 static struct regset rs6000_aix32_regset =
138 &rs6000_aix32_reg_offsets,
139 rs6000_aix_supply_regset,
140 rs6000_aix_collect_regset,
143 static struct regset rs6000_aix64_regset =
145 &rs6000_aix64_reg_offsets,
146 rs6000_aix_supply_regset,
147 rs6000_aix_collect_regset,
150 /* Return the appropriate register set for the core section identified
151 by SECT_NAME and SECT_SIZE. */
153 static const struct regset *
154 rs6000_aix_regset_from_core_section (struct gdbarch *gdbarch,
155 const char *sect_name, size_t sect_size)
157 if (gdbarch_tdep (gdbarch)->wordsize == 4)
159 if (strcmp (sect_name, ".reg") == 0 && sect_size >= 592)
160 return &rs6000_aix32_regset;
164 if (strcmp (sect_name, ".reg") == 0 && sect_size >= 576)
165 return &rs6000_aix64_regset;
172 /* Pass the arguments in either registers, or in the stack. In RS/6000,
173 the first eight words of the argument list (that might be less than
174 eight parameters if some parameters occupy more than one word) are
175 passed in r3..r10 registers. Float and double parameters are
176 passed in fpr's, in addition to that. Rest of the parameters if any
177 are passed in user stack. There might be cases in which half of the
178 parameter is copied into registers, the other half is pushed into
181 Stack must be aligned on 64-bit boundaries when synthesizing
184 If the function is returning a structure, then the return address is passed
185 in r3, then the first 7 words of the parameters can be passed in registers,
189 rs6000_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
190 struct regcache *regcache, CORE_ADDR bp_addr,
191 int nargs, struct value **args, CORE_ADDR sp,
192 int struct_return, CORE_ADDR struct_addr)
194 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
195 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
198 int argno; /* current argument number */
199 int argbytes; /* current argument byte */
200 gdb_byte tmp_buffer[50];
201 int f_argno = 0; /* current floating point argno */
202 int wordsize = gdbarch_tdep (gdbarch)->wordsize;
203 CORE_ADDR func_addr = find_function_addr (function, NULL);
205 struct value *arg = 0;
210 /* The calling convention this function implements assumes the
211 processor has floating-point registers. We shouldn't be using it
212 on PPC variants that lack them. */
213 gdb_assert (ppc_floating_point_unit_p (gdbarch));
215 /* The first eight words of ther arguments are passed in registers.
216 Copy them appropriately. */
219 /* If the function is returning a `struct', then the first word
220 (which will be passed in r3) is used for struct return address.
221 In that case we should advance one word and start from r4
222 register to copy parameters. */
225 regcache_raw_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
230 /* effectively indirect call... gcc does...
232 return_val example( float, int);
235 float in fp0, int in r3
236 offset of stack on overflow 8/16
237 for varargs, must go by type.
239 float in r3&r4, int in r5
240 offset of stack on overflow different
242 return in r3 or f0. If no float, must study how gcc emulates floats;
243 pay attention to arg promotion.
244 User may have to cast\args to handle promotion correctly
245 since gdb won't know if prototype supplied or not. */
247 for (argno = 0, argbytes = 0; argno < nargs && ii < 8; ++ii)
249 int reg_size = register_size (gdbarch, ii + 3);
252 type = check_typedef (value_type (arg));
253 len = TYPE_LENGTH (type);
255 if (TYPE_CODE (type) == TYPE_CODE_FLT)
258 /* Floating point arguments are passed in fpr's, as well as gpr's.
259 There are 13 fpr's reserved for passing parameters. At this point
260 there is no way we would run out of them. */
262 gdb_assert (len <= 8);
264 regcache_cooked_write (regcache,
265 tdep->ppc_fp0_regnum + 1 + f_argno,
266 value_contents (arg));
273 /* Argument takes more than one register. */
274 while (argbytes < len)
276 gdb_byte word[MAX_REGISTER_SIZE];
277 memset (word, 0, reg_size);
279 ((char *) value_contents (arg)) + argbytes,
280 (len - argbytes) > reg_size
281 ? reg_size : len - argbytes);
282 regcache_cooked_write (regcache,
283 tdep->ppc_gp0_regnum + 3 + ii,
285 ++ii, argbytes += reg_size;
288 goto ran_out_of_registers_for_arguments;
295 /* Argument can fit in one register. No problem. */
296 int adj = gdbarch_byte_order (gdbarch)
297 == BFD_ENDIAN_BIG ? reg_size - len : 0;
298 gdb_byte word[MAX_REGISTER_SIZE];
300 memset (word, 0, reg_size);
301 memcpy (word, value_contents (arg), len);
302 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3 +ii, word);
307 ran_out_of_registers_for_arguments:
309 regcache_cooked_read_unsigned (regcache,
310 gdbarch_sp_regnum (gdbarch),
313 /* Location for 8 parameters are always reserved. */
316 /* Another six words for back chain, TOC register, link register, etc. */
319 /* Stack pointer must be quadword aligned. */
322 /* If there are more arguments, allocate space for them in
323 the stack, then push them starting from the ninth one. */
325 if ((argno < nargs) || argbytes)
331 space += ((len - argbytes + 3) & -4);
337 for (; jj < nargs; ++jj)
339 struct value *val = args[jj];
340 space += ((TYPE_LENGTH (value_type (val))) + 3) & -4;
343 /* Add location required for the rest of the parameters. */
344 space = (space + 15) & -16;
347 /* This is another instance we need to be concerned about
348 securing our stack space. If we write anything underneath %sp
349 (r1), we might conflict with the kernel who thinks he is free
350 to use this area. So, update %sp first before doing anything
353 regcache_raw_write_signed (regcache,
354 gdbarch_sp_regnum (gdbarch), sp);
356 /* If the last argument copied into the registers didn't fit there
357 completely, push the rest of it into stack. */
361 write_memory (sp + 24 + (ii * 4),
362 value_contents (arg) + argbytes,
365 ii += ((len - argbytes + 3) & -4) / 4;
368 /* Push the rest of the arguments into stack. */
369 for (; argno < nargs; ++argno)
373 type = check_typedef (value_type (arg));
374 len = TYPE_LENGTH (type);
377 /* Float types should be passed in fpr's, as well as in the
379 if (TYPE_CODE (type) == TYPE_CODE_FLT && f_argno < 13)
382 gdb_assert (len <= 8);
384 regcache_cooked_write (regcache,
385 tdep->ppc_fp0_regnum + 1 + f_argno,
386 value_contents (arg));
390 write_memory (sp + 24 + (ii * 4), value_contents (arg), len);
391 ii += ((len + 3) & -4) / 4;
395 /* Set the stack pointer. According to the ABI, the SP is meant to
396 be set _before_ the corresponding stack space is used. On AIX,
397 this even applies when the target has been completely stopped!
398 Not doing this can lead to conflicts with the kernel which thinks
399 that it still has control over this not-yet-allocated stack
401 regcache_raw_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
403 /* Set back chain properly. */
404 store_unsigned_integer (tmp_buffer, wordsize, byte_order, saved_sp);
405 write_memory (sp, tmp_buffer, wordsize);
407 /* Point the inferior function call's return address at the dummy's
409 regcache_raw_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
411 /* Set the TOC register value. */
412 regcache_raw_write_signed (regcache, tdep->ppc_toc_regnum,
413 solib_aix_get_toc_value (func_addr));
415 target_store_registers (regcache, -1);
419 static enum return_value_convention
420 rs6000_return_value (struct gdbarch *gdbarch, struct value *function,
421 struct type *valtype, struct regcache *regcache,
422 gdb_byte *readbuf, const gdb_byte *writebuf)
424 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
425 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
427 /* The calling convention this function implements assumes the
428 processor has floating-point registers. We shouldn't be using it
429 on PowerPC variants that lack them. */
430 gdb_assert (ppc_floating_point_unit_p (gdbarch));
432 /* AltiVec extension: Functions that declare a vector data type as a
433 return value place that return value in VR2. */
434 if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
435 && TYPE_LENGTH (valtype) == 16)
438 regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
440 regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
442 return RETURN_VALUE_REGISTER_CONVENTION;
445 /* If the called subprogram returns an aggregate, there exists an
446 implicit first argument, whose value is the address of a caller-
447 allocated buffer into which the callee is assumed to store its
448 return value. All explicit parameters are appropriately
450 if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
451 || TYPE_CODE (valtype) == TYPE_CODE_UNION
452 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
453 return RETURN_VALUE_STRUCT_CONVENTION;
455 /* Scalar floating-point values are returned in FPR1 for float or
456 double, and in FPR1:FPR2 for quadword precision. Fortran
457 complex*8 and complex*16 are returned in FPR1:FPR2, and
458 complex*32 is returned in FPR1:FPR4. */
459 if (TYPE_CODE (valtype) == TYPE_CODE_FLT
460 && (TYPE_LENGTH (valtype) == 4 || TYPE_LENGTH (valtype) == 8))
462 struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
465 /* FIXME: kettenis/2007-01-01: Add support for quadword
466 precision and complex. */
470 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
471 convert_typed_floating (regval, regtype, readbuf, valtype);
475 convert_typed_floating (writebuf, valtype, regval, regtype);
476 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
479 return RETURN_VALUE_REGISTER_CONVENTION;
482 /* Values of the types int, long, short, pointer, and char (length
483 is less than or equal to four bytes), as well as bit values of
484 lengths less than or equal to 32 bits, must be returned right
485 justified in GPR3 with signed values sign extended and unsigned
486 values zero extended, as necessary. */
487 if (TYPE_LENGTH (valtype) <= tdep->wordsize)
493 /* For reading we don't have to worry about sign extension. */
494 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
496 store_unsigned_integer (readbuf, TYPE_LENGTH (valtype), byte_order,
501 /* For writing, use unpack_long since that should handle any
502 required sign extension. */
503 regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
504 unpack_long (valtype, writebuf));
507 return RETURN_VALUE_REGISTER_CONVENTION;
510 /* Eight-byte non-floating-point scalar values must be returned in
513 if (TYPE_LENGTH (valtype) == 8)
515 gdb_assert (TYPE_CODE (valtype) != TYPE_CODE_FLT);
516 gdb_assert (tdep->wordsize == 4);
522 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, regval);
523 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
525 memcpy (readbuf, regval, 8);
529 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
530 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
534 return RETURN_VALUE_REGISTER_CONVENTION;
537 return RETURN_VALUE_STRUCT_CONVENTION;
540 /* Support for CONVERT_FROM_FUNC_PTR_ADDR (ARCH, ADDR, TARG).
542 Usually a function pointer's representation is simply the address
543 of the function. On the RS/6000 however, a function pointer is
544 represented by a pointer to an OPD entry. This OPD entry contains
545 three words, the first word is the address of the function, the
546 second word is the TOC pointer (r2), and the third word is the
547 static chain value. Throughout GDB it is currently assumed that a
548 function pointer contains the address of the function, which is not
549 easy to fix. In addition, the conversion of a function address to
550 a function pointer would require allocation of an OPD entry in the
551 inferior's memory space, with all its drawbacks. To be able to
552 call C++ virtual methods in the inferior (which are called via
553 function pointers), find_function_addr uses this function to get the
554 function address from a function pointer. */
556 /* Return real function address if ADDR (a function pointer) is in the data
557 space and is therefore a special function pointer. */
560 rs6000_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
562 struct target_ops *targ)
564 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
565 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
566 struct obj_section *s;
568 s = find_pc_section (addr);
570 /* Normally, functions live inside a section that is executable.
571 So, if ADDR points to a non-executable section, then treat it
572 as a function descriptor and return the target address iff
573 the target address itself points to a section that is executable. */
574 if (s && (s->the_bfd_section->flags & SEC_CODE) == 0)
577 struct obj_section *pc_section;
578 volatile struct gdb_exception e;
580 TRY_CATCH (e, RETURN_MASK_ERROR)
582 pc = read_memory_unsigned_integer (addr, tdep->wordsize, byte_order);
586 /* An error occured during reading. Probably a memory error
587 due to the section not being loaded yet. This address
588 cannot be a function descriptor. */
591 pc_section = find_pc_section (pc);
593 if (pc_section && (pc_section->the_bfd_section->flags & SEC_CODE))
601 /* Calculate the destination of a branch/jump. Return -1 if not a branch. */
604 branch_dest (struct frame_info *frame, int opcode, int instr,
605 CORE_ADDR pc, CORE_ADDR safety)
607 struct gdbarch *gdbarch = get_frame_arch (frame);
608 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
609 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
615 absolute = (int) ((instr >> 1) & 1);
620 immediate = ((instr & ~3) << 6) >> 6; /* br unconditional */
624 dest = pc + immediate;
628 immediate = ((instr & ~3) << 16) >> 16; /* br conditional */
632 dest = pc + immediate;
636 ext_op = (instr >> 1) & 0x3ff;
638 if (ext_op == 16) /* br conditional register */
640 dest = get_frame_register_unsigned (frame, tdep->ppc_lr_regnum) & ~3;
642 /* If we are about to return from a signal handler, dest is
643 something like 0x3c90. The current frame is a signal handler
644 caller frame, upon completion of the sigreturn system call
645 execution will return to the saved PC in the frame. */
646 if (dest < AIX_TEXT_SEGMENT_BASE)
647 dest = read_memory_unsigned_integer
648 (get_frame_base (frame) + SIG_FRAME_PC_OFFSET,
649 tdep->wordsize, byte_order);
652 else if (ext_op == 528) /* br cond to count reg */
654 dest = get_frame_register_unsigned (frame,
655 tdep->ppc_ctr_regnum) & ~3;
657 /* If we are about to execute a system call, dest is something
658 like 0x22fc or 0x3b00. Upon completion the system call
659 will return to the address in the link register. */
660 if (dest < AIX_TEXT_SEGMENT_BASE)
661 dest = get_frame_register_unsigned (frame,
662 tdep->ppc_lr_regnum) & ~3;
671 return (dest < AIX_TEXT_SEGMENT_BASE) ? safety : dest;
674 /* AIX does not support PT_STEP. Simulate it. */
677 rs6000_software_single_step (struct frame_info *frame)
679 struct gdbarch *gdbarch = get_frame_arch (frame);
680 struct address_space *aspace = get_frame_address_space (frame);
681 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
687 loc = get_frame_pc (frame);
689 insn = read_memory_integer (loc, 4, byte_order);
691 if (ppc_deal_with_atomic_sequence (frame))
694 breaks[0] = loc + PPC_INSN_SIZE;
696 breaks[1] = branch_dest (frame, opcode, insn, loc, breaks[0]);
698 /* Don't put two breakpoints on the same address. */
699 if (breaks[1] == breaks[0])
702 for (ii = 0; ii < 2; ++ii)
704 /* ignore invalid breakpoint. */
705 if (breaks[ii] == -1)
707 insert_single_step_breakpoint (gdbarch, aspace, breaks[ii]);
710 errno = 0; /* FIXME, don't ignore errors! */
711 /* What errors? {read,write}_memory call error(). */
715 /* Implement the "auto_wide_charset" gdbarch method for this platform. */
718 rs6000_aix_auto_wide_charset (void)
723 /* Implement an osabi sniffer for RS6000/AIX.
725 This function assumes that ABFD's flavour is XCOFF. In other words,
726 it should be registered as a sniffer for bfd_target_xcoff_flavour
727 objfiles only. A failed assertion will be raised if this condition
730 static enum gdb_osabi
731 rs6000_aix_osabi_sniffer (bfd *abfd)
733 gdb_assert (bfd_get_flavour (abfd) == bfd_target_xcoff_flavour);
735 /* The only noticeable difference between Lynx178 XCOFF files and
736 AIX XCOFF files comes from the fact that there are no shared
737 libraries on Lynx178. On AIX, we are betting that an executable
738 linked with no shared library will never exist. */
739 if (xcoff_get_n_import_files (abfd) <= 0)
740 return GDB_OSABI_UNKNOWN;
742 return GDB_OSABI_AIX;
746 rs6000_aix_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
748 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
750 /* RS6000/AIX does not support PT_STEP. Has to be simulated. */
751 set_gdbarch_software_single_step (gdbarch, rs6000_software_single_step);
753 /* Displaced stepping is currently not supported in combination with
754 software single-stepping. */
755 set_gdbarch_displaced_step_copy_insn (gdbarch, NULL);
756 set_gdbarch_displaced_step_fixup (gdbarch, NULL);
757 set_gdbarch_displaced_step_free_closure (gdbarch, NULL);
758 set_gdbarch_displaced_step_location (gdbarch, NULL);
760 set_gdbarch_push_dummy_call (gdbarch, rs6000_push_dummy_call);
761 set_gdbarch_return_value (gdbarch, rs6000_return_value);
762 set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
764 /* Handle RS/6000 function pointers (which are really function
766 set_gdbarch_convert_from_func_ptr_addr
767 (gdbarch, rs6000_convert_from_func_ptr_addr);
769 /* Core file support. */
770 set_gdbarch_regset_from_core_section
771 (gdbarch, rs6000_aix_regset_from_core_section);
773 if (tdep->wordsize == 8)
774 tdep->lr_frame_offset = 16;
776 tdep->lr_frame_offset = 8;
778 if (tdep->wordsize == 4)
779 /* PowerOpen / AIX 32 bit. The saved area or red zone consists of
780 19 4 byte GPRS + 18 8 byte FPRs giving a total of 220 bytes.
781 Problem is, 220 isn't frame (16 byte) aligned. Round it up to
783 set_gdbarch_frame_red_zone_size (gdbarch, 224);
785 set_gdbarch_frame_red_zone_size (gdbarch, 0);
787 set_gdbarch_auto_wide_charset (gdbarch, rs6000_aix_auto_wide_charset);
789 set_solib_ops (gdbarch, &solib_aix_so_ops);
792 /* Provide a prototype to silence -Wmissing-prototypes. */
793 extern initialize_file_ftype _initialize_rs6000_aix_tdep;
796 _initialize_rs6000_aix_tdep (void)
798 gdbarch_register_osabi_sniffer (bfd_arch_rs6000,
799 bfd_target_xcoff_flavour,
800 rs6000_aix_osabi_sniffer);
801 gdbarch_register_osabi_sniffer (bfd_arch_powerpc,
802 bfd_target_xcoff_flavour,
803 rs6000_aix_osabi_sniffer);
805 gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_AIX,
806 rs6000_aix_init_osabi);
807 gdbarch_register_osabi (bfd_arch_powerpc, 0, GDB_OSABI_AIX,
808 rs6000_aix_init_osabi);