PPC: Migrate from 'regset_from_core_section' to 'iterate_over_regset_sections'
[external/binutils.git] / gdb / rs6000-aix-tdep.c
1 /* Native support code for PPC AIX, for GDB the GNU debugger.
2
3    Copyright (C) 2006-2014 Free Software Foundation, Inc.
4
5    Free Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "osabi.h"
24 #include "regcache.h"
25 #include "regset.h"
26 #include "gdbtypes.h"
27 #include "gdbcore.h"
28 #include "target.h"
29 #include "value.h"
30 #include "infcall.h"
31 #include "objfiles.h"
32 #include "breakpoint.h"
33 #include "rs6000-tdep.h"
34 #include "ppc-tdep.h"
35 #include "rs6000-aix-tdep.h"
36 #include "exceptions.h"
37 #include "xcoffread.h"
38 #include "solib.h"
39 #include "solib-aix.h"
40 #include "xml-utils.h"
41
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
47    frame.
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
52
53
54 /* Core file support.  */
55
56 static struct ppc_reg_offsets rs6000_aix32_reg_offsets =
57 {
58   /* General-purpose registers.  */
59   208, /* r0_offset */
60   4,  /* gpr_size */
61   4,  /* xr_size */
62   24, /* pc_offset */
63   28, /* ps_offset */
64   32, /* cr_offset */
65   36, /* lr_offset */
66   40, /* ctr_offset */
67   44, /* xer_offset */
68   48, /* mq_offset */
69
70   /* Floating-point registers.  */
71   336, /* f0_offset */
72   56, /* fpscr_offset */
73   4,  /* fpscr_size */
74
75   /* AltiVec registers.  */
76   -1, /* vr0_offset */
77   -1, /* vscr_offset */
78   -1 /* vrsave_offset */
79 };
80
81 static struct ppc_reg_offsets rs6000_aix64_reg_offsets =
82 {
83   /* General-purpose registers.  */
84   0, /* r0_offset */
85   8,  /* gpr_size */
86   4,  /* xr_size */
87   264, /* pc_offset */
88   256, /* ps_offset */
89   288, /* cr_offset */
90   272, /* lr_offset */
91   280, /* ctr_offset */
92   292, /* xer_offset */
93   -1, /* mq_offset */
94
95   /* Floating-point registers.  */
96   312, /* f0_offset */
97   296, /* fpscr_offset */
98   4,  /* fpscr_size */
99
100   /* AltiVec registers.  */
101   -1, /* vr0_offset */
102   -1, /* vscr_offset */
103   -1 /* vrsave_offset */
104 };
105
106
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.  */
110
111 static void
112 rs6000_aix_supply_regset (const struct regset *regset,
113                           struct regcache *regcache, int regnum,
114                           const void *gregs, size_t len)
115 {
116   ppc_supply_gregset (regset, regcache, regnum, gregs, len);
117   ppc_supply_fpregset (regset, regcache, regnum, gregs, len);
118 }
119
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
123    REGSET.  */
124
125 static void
126 rs6000_aix_collect_regset (const struct regset *regset,
127                            const struct regcache *regcache, int regnum,
128                            void *gregs, size_t len)
129 {
130   ppc_collect_gregset (regset, regcache, regnum, gregs, len);
131   ppc_collect_fpregset (regset, regcache, regnum, gregs, len);
132 }
133
134 /* AIX register set.  */
135
136 static const struct regset rs6000_aix32_regset =
137 {
138   &rs6000_aix32_reg_offsets,
139   rs6000_aix_supply_regset,
140   rs6000_aix_collect_regset,
141 };
142
143 static const struct regset rs6000_aix64_regset =
144 {
145   &rs6000_aix64_reg_offsets,
146   rs6000_aix_supply_regset,
147   rs6000_aix_collect_regset,
148 };
149
150 /* Iterate over core file register note sections.  */
151
152 static void
153 rs6000_aix_iterate_over_regset_sections (struct gdbarch *gdbarch,
154                                          iterate_over_regset_sections_cb *cb,
155                                          void *cb_data,
156                                          const struct regcache *regcache)
157 {
158   if (gdbarch_tdep (gdbarch)->wordsize == 4)
159     cb (".reg", 592, &rs6000_aix32_regset, NULL, cb_data);
160   else
161     cb (".reg", 576, &rs6000_aix64_regset, NULL, cb_data);
162 }
163
164
165 /* Pass the arguments in either registers, or in the stack.  In RS/6000,
166    the first eight words of the argument list (that might be less than
167    eight parameters if some parameters occupy more than one word) are
168    passed in r3..r10 registers.  Float and double parameters are
169    passed in fpr's, in addition to that.  Rest of the parameters if any
170    are passed in user stack.  There might be cases in which half of the
171    parameter is copied into registers, the other half is pushed into
172    stack.
173
174    Stack must be aligned on 64-bit boundaries when synthesizing
175    function calls.
176
177    If the function is returning a structure, then the return address is passed
178    in r3, then the first 7 words of the parameters can be passed in registers,
179    starting from r4.  */
180
181 static CORE_ADDR
182 rs6000_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
183                         struct regcache *regcache, CORE_ADDR bp_addr,
184                         int nargs, struct value **args, CORE_ADDR sp,
185                         int struct_return, CORE_ADDR struct_addr)
186 {
187   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
188   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
189   int ii;
190   int len = 0;
191   int argno;                    /* current argument number */
192   int argbytes;                 /* current argument byte */
193   gdb_byte tmp_buffer[50];
194   int f_argno = 0;              /* current floating point argno */
195   int wordsize = gdbarch_tdep (gdbarch)->wordsize;
196   CORE_ADDR func_addr = find_function_addr (function, NULL);
197
198   struct value *arg = 0;
199   struct type *type;
200
201   ULONGEST saved_sp;
202
203   /* The calling convention this function implements assumes the
204      processor has floating-point registers.  We shouldn't be using it
205      on PPC variants that lack them.  */
206   gdb_assert (ppc_floating_point_unit_p (gdbarch));
207
208   /* The first eight words of ther arguments are passed in registers.
209      Copy them appropriately.  */
210   ii = 0;
211
212   /* If the function is returning a `struct', then the first word
213      (which will be passed in r3) is used for struct return address.
214      In that case we should advance one word and start from r4
215      register to copy parameters.  */
216   if (struct_return)
217     {
218       regcache_raw_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
219                                    struct_addr);
220       ii++;
221     }
222
223 /* effectively indirect call... gcc does...
224
225    return_val example( float, int);
226
227    eabi: 
228    float in fp0, int in r3
229    offset of stack on overflow 8/16
230    for varargs, must go by type.
231    power open:
232    float in r3&r4, int in r5
233    offset of stack on overflow different 
234    both: 
235    return in r3 or f0.  If no float, must study how gcc emulates floats;
236    pay attention to arg promotion.
237    User may have to cast\args to handle promotion correctly 
238    since gdb won't know if prototype supplied or not.  */
239
240   for (argno = 0, argbytes = 0; argno < nargs && ii < 8; ++ii)
241     {
242       int reg_size = register_size (gdbarch, ii + 3);
243
244       arg = args[argno];
245       type = check_typedef (value_type (arg));
246       len = TYPE_LENGTH (type);
247
248       if (TYPE_CODE (type) == TYPE_CODE_FLT)
249         {
250           /* Floating point arguments are passed in fpr's, as well as gpr's.
251              There are 13 fpr's reserved for passing parameters.  At this point
252              there is no way we would run out of them.
253
254              Always store the floating point value using the register's
255              floating-point format.  */
256           const int fp_regnum = tdep->ppc_fp0_regnum + 1 + f_argno;
257           gdb_byte reg_val[MAX_REGISTER_SIZE];
258           struct type *reg_type = register_type (gdbarch, fp_regnum);
259
260           gdb_assert (len <= 8);
261
262           convert_typed_floating (value_contents (arg), type,
263                                   reg_val, reg_type);
264           regcache_cooked_write (regcache, fp_regnum, reg_val);
265           ++f_argno;
266         }
267
268       if (len > reg_size)
269         {
270
271           /* Argument takes more than one register.  */
272           while (argbytes < len)
273             {
274               gdb_byte word[MAX_REGISTER_SIZE];
275               memset (word, 0, reg_size);
276               memcpy (word,
277                       ((char *) value_contents (arg)) + argbytes,
278                       (len - argbytes) > reg_size
279                         ? reg_size : len - argbytes);
280               regcache_cooked_write (regcache,
281                                     tdep->ppc_gp0_regnum + 3 + ii,
282                                     word);
283               ++ii, argbytes += reg_size;
284
285               if (ii >= 8)
286                 goto ran_out_of_registers_for_arguments;
287             }
288           argbytes = 0;
289           --ii;
290         }
291       else
292         {
293           /* Argument can fit in one register.  No problem.  */
294           int adj = gdbarch_byte_order (gdbarch)
295                     == BFD_ENDIAN_BIG ? reg_size - len : 0;
296           gdb_byte word[MAX_REGISTER_SIZE];
297
298           memset (word, 0, reg_size);
299           memcpy (word, value_contents (arg), len);
300           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3 +ii, word);
301         }
302       ++argno;
303     }
304
305 ran_out_of_registers_for_arguments:
306
307   regcache_cooked_read_unsigned (regcache,
308                                  gdbarch_sp_regnum (gdbarch),
309                                  &saved_sp);
310
311   /* Location for 8 parameters are always reserved.  */
312   sp -= wordsize * 8;
313
314   /* Another six words for back chain, TOC register, link register, etc.  */
315   sp -= wordsize * 6;
316
317   /* Stack pointer must be quadword aligned.  */
318   sp &= -16;
319
320   /* If there are more arguments, allocate space for them in 
321      the stack, then push them starting from the ninth one.  */
322
323   if ((argno < nargs) || argbytes)
324     {
325       int space = 0, jj;
326
327       if (argbytes)
328         {
329           space += ((len - argbytes + 3) & -4);
330           jj = argno + 1;
331         }
332       else
333         jj = argno;
334
335       for (; jj < nargs; ++jj)
336         {
337           struct value *val = args[jj];
338           space += ((TYPE_LENGTH (value_type (val))) + 3) & -4;
339         }
340
341       /* Add location required for the rest of the parameters.  */
342       space = (space + 15) & -16;
343       sp -= space;
344
345       /* This is another instance we need to be concerned about
346          securing our stack space.  If we write anything underneath %sp
347          (r1), we might conflict with the kernel who thinks he is free
348          to use this area.  So, update %sp first before doing anything
349          else.  */
350
351       regcache_raw_write_signed (regcache,
352                                  gdbarch_sp_regnum (gdbarch), sp);
353
354       /* If the last argument copied into the registers didn't fit there 
355          completely, push the rest of it into stack.  */
356
357       if (argbytes)
358         {
359           write_memory (sp + 24 + (ii * 4),
360                         value_contents (arg) + argbytes,
361                         len - argbytes);
362           ++argno;
363           ii += ((len - argbytes + 3) & -4) / 4;
364         }
365
366       /* Push the rest of the arguments into stack.  */
367       for (; argno < nargs; ++argno)
368         {
369
370           arg = args[argno];
371           type = check_typedef (value_type (arg));
372           len = TYPE_LENGTH (type);
373
374
375           /* Float types should be passed in fpr's, as well as in the
376              stack.  */
377           if (TYPE_CODE (type) == TYPE_CODE_FLT && f_argno < 13)
378             {
379
380               gdb_assert (len <= 8);
381
382               regcache_cooked_write (regcache,
383                                      tdep->ppc_fp0_regnum + 1 + f_argno,
384                                      value_contents (arg));
385               ++f_argno;
386             }
387
388           write_memory (sp + 24 + (ii * 4), value_contents (arg), len);
389           ii += ((len + 3) & -4) / 4;
390         }
391     }
392
393   /* Set the stack pointer.  According to the ABI, the SP is meant to
394      be set _before_ the corresponding stack space is used.  On AIX,
395      this even applies when the target has been completely stopped!
396      Not doing this can lead to conflicts with the kernel which thinks
397      that it still has control over this not-yet-allocated stack
398      region.  */
399   regcache_raw_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
400
401   /* Set back chain properly.  */
402   store_unsigned_integer (tmp_buffer, wordsize, byte_order, saved_sp);
403   write_memory (sp, tmp_buffer, wordsize);
404
405   /* Point the inferior function call's return address at the dummy's
406      breakpoint.  */
407   regcache_raw_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
408
409   /* Set the TOC register value.  */
410   regcache_raw_write_signed (regcache, tdep->ppc_toc_regnum,
411                              solib_aix_get_toc_value (func_addr));
412
413   target_store_registers (regcache, -1);
414   return sp;
415 }
416
417 static enum return_value_convention
418 rs6000_return_value (struct gdbarch *gdbarch, struct value *function,
419                      struct type *valtype, struct regcache *regcache,
420                      gdb_byte *readbuf, const gdb_byte *writebuf)
421 {
422   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
423   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
424
425   /* The calling convention this function implements assumes the
426      processor has floating-point registers.  We shouldn't be using it
427      on PowerPC variants that lack them.  */
428   gdb_assert (ppc_floating_point_unit_p (gdbarch));
429
430   /* AltiVec extension: Functions that declare a vector data type as a
431      return value place that return value in VR2.  */
432   if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
433       && TYPE_LENGTH (valtype) == 16)
434     {
435       if (readbuf)
436         regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
437       if (writebuf)
438         regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
439
440       return RETURN_VALUE_REGISTER_CONVENTION;
441     }
442
443   /* If the called subprogram returns an aggregate, there exists an
444      implicit first argument, whose value is the address of a caller-
445      allocated buffer into which the callee is assumed to store its
446      return value.  All explicit parameters are appropriately
447      relabeled.  */
448   if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
449       || TYPE_CODE (valtype) == TYPE_CODE_UNION
450       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
451     return RETURN_VALUE_STRUCT_CONVENTION;
452
453   /* Scalar floating-point values are returned in FPR1 for float or
454      double, and in FPR1:FPR2 for quadword precision.  Fortran
455      complex*8 and complex*16 are returned in FPR1:FPR2, and
456      complex*32 is returned in FPR1:FPR4.  */
457   if (TYPE_CODE (valtype) == TYPE_CODE_FLT
458       && (TYPE_LENGTH (valtype) == 4 || TYPE_LENGTH (valtype) == 8))
459     {
460       struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
461       gdb_byte regval[8];
462
463       /* FIXME: kettenis/2007-01-01: Add support for quadword
464          precision and complex.  */
465
466       if (readbuf)
467         {
468           regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
469           convert_typed_floating (regval, regtype, readbuf, valtype);
470         }
471       if (writebuf)
472         {
473           convert_typed_floating (writebuf, valtype, regval, regtype);
474           regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
475         }
476
477       return RETURN_VALUE_REGISTER_CONVENTION;
478   }
479
480   /* Values of the types int, long, short, pointer, and char (length
481      is less than or equal to four bytes), as well as bit values of
482      lengths less than or equal to 32 bits, must be returned right
483      justified in GPR3 with signed values sign extended and unsigned
484      values zero extended, as necessary.  */
485   if (TYPE_LENGTH (valtype) <= tdep->wordsize)
486     {
487       if (readbuf)
488         {
489           ULONGEST regval;
490
491           /* For reading we don't have to worry about sign extension.  */
492           regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
493                                          &regval);
494           store_unsigned_integer (readbuf, TYPE_LENGTH (valtype), byte_order,
495                                   regval);
496         }
497       if (writebuf)
498         {
499           /* For writing, use unpack_long since that should handle any
500              required sign extension.  */
501           regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
502                                           unpack_long (valtype, writebuf));
503         }
504
505       return RETURN_VALUE_REGISTER_CONVENTION;
506     }
507
508   /* Eight-byte non-floating-point scalar values must be returned in
509      GPR3:GPR4.  */
510
511   if (TYPE_LENGTH (valtype) == 8)
512     {
513       gdb_assert (TYPE_CODE (valtype) != TYPE_CODE_FLT);
514       gdb_assert (tdep->wordsize == 4);
515
516       if (readbuf)
517         {
518           gdb_byte regval[8];
519
520           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, regval);
521           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
522                                 regval + 4);
523           memcpy (readbuf, regval, 8);
524         }
525       if (writebuf)
526         {
527           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
528           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
529                                  writebuf + 4);
530         }
531
532       return RETURN_VALUE_REGISTER_CONVENTION;
533     }
534
535   return RETURN_VALUE_STRUCT_CONVENTION;
536 }
537
538 /* Support for CONVERT_FROM_FUNC_PTR_ADDR (ARCH, ADDR, TARG).
539
540    Usually a function pointer's representation is simply the address
541    of the function.  On the RS/6000 however, a function pointer is
542    represented by a pointer to an OPD entry.  This OPD entry contains
543    three words, the first word is the address of the function, the
544    second word is the TOC pointer (r2), and the third word is the
545    static chain value.  Throughout GDB it is currently assumed that a
546    function pointer contains the address of the function, which is not
547    easy to fix.  In addition, the conversion of a function address to
548    a function pointer would require allocation of an OPD entry in the
549    inferior's memory space, with all its drawbacks.  To be able to
550    call C++ virtual methods in the inferior (which are called via
551    function pointers), find_function_addr uses this function to get the
552    function address from a function pointer.  */
553
554 /* Return real function address if ADDR (a function pointer) is in the data
555    space and is therefore a special function pointer.  */
556
557 static CORE_ADDR
558 rs6000_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
559                                    CORE_ADDR addr,
560                                    struct target_ops *targ)
561 {
562   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
563   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
564   struct obj_section *s;
565
566   s = find_pc_section (addr);
567
568   /* Normally, functions live inside a section that is executable.
569      So, if ADDR points to a non-executable section, then treat it
570      as a function descriptor and return the target address iff
571      the target address itself points to a section that is executable.  */
572   if (s && (s->the_bfd_section->flags & SEC_CODE) == 0)
573     {
574       CORE_ADDR pc = 0;
575       struct obj_section *pc_section;
576       volatile struct gdb_exception e;
577
578       TRY_CATCH (e, RETURN_MASK_ERROR)
579         {
580           pc = read_memory_unsigned_integer (addr, tdep->wordsize, byte_order);
581         }
582       if (e.reason < 0)
583         {
584           /* An error occured during reading.  Probably a memory error
585              due to the section not being loaded yet.  This address
586              cannot be a function descriptor.  */
587           return addr;
588         }
589       pc_section = find_pc_section (pc);
590
591       if (pc_section && (pc_section->the_bfd_section->flags & SEC_CODE))
592         return pc;
593     }
594
595   return addr;
596 }
597
598
599 /* Calculate the destination of a branch/jump.  Return -1 if not a branch.  */
600
601 static CORE_ADDR
602 branch_dest (struct frame_info *frame, int opcode, int instr,
603              CORE_ADDR pc, CORE_ADDR safety)
604 {
605   struct gdbarch *gdbarch = get_frame_arch (frame);
606   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
607   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
608   CORE_ADDR dest;
609   int immediate;
610   int absolute;
611   int ext_op;
612
613   absolute = (int) ((instr >> 1) & 1);
614
615   switch (opcode)
616     {
617     case 18:
618       immediate = ((instr & ~3) << 6) >> 6;     /* br unconditional */
619       if (absolute)
620         dest = immediate;
621       else
622         dest = pc + immediate;
623       break;
624
625     case 16:
626       immediate = ((instr & ~3) << 16) >> 16;   /* br conditional */
627       if (absolute)
628         dest = immediate;
629       else
630         dest = pc + immediate;
631       break;
632
633     case 19:
634       ext_op = (instr >> 1) & 0x3ff;
635
636       if (ext_op == 16)         /* br conditional register */
637         {
638           dest = get_frame_register_unsigned (frame, tdep->ppc_lr_regnum) & ~3;
639
640           /* If we are about to return from a signal handler, dest is
641              something like 0x3c90.  The current frame is a signal handler
642              caller frame, upon completion of the sigreturn system call
643              execution will return to the saved PC in the frame.  */
644           if (dest < AIX_TEXT_SEGMENT_BASE)
645             dest = read_memory_unsigned_integer
646                      (get_frame_base (frame) + SIG_FRAME_PC_OFFSET,
647                       tdep->wordsize, byte_order);
648         }
649
650       else if (ext_op == 528)   /* br cond to count reg */
651         {
652           dest = get_frame_register_unsigned (frame,
653                                               tdep->ppc_ctr_regnum) & ~3;
654
655           /* If we are about to execute a system call, dest is something
656              like 0x22fc or 0x3b00.  Upon completion the system call
657              will return to the address in the link register.  */
658           if (dest < AIX_TEXT_SEGMENT_BASE)
659             dest = get_frame_register_unsigned (frame,
660                                                 tdep->ppc_lr_regnum) & ~3;
661         }
662       else
663         return -1;
664       break;
665
666     default:
667       return -1;
668     }
669   return (dest < AIX_TEXT_SEGMENT_BASE) ? safety : dest;
670 }
671
672 /* AIX does not support PT_STEP.  Simulate it.  */
673
674 static int
675 rs6000_software_single_step (struct frame_info *frame)
676 {
677   struct gdbarch *gdbarch = get_frame_arch (frame);
678   struct address_space *aspace = get_frame_address_space (frame);
679   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
680   int ii, insn;
681   CORE_ADDR loc;
682   CORE_ADDR breaks[2];
683   int opcode;
684
685   loc = get_frame_pc (frame);
686
687   insn = read_memory_integer (loc, 4, byte_order);
688
689   if (ppc_deal_with_atomic_sequence (frame))
690     return 1;
691   
692   breaks[0] = loc + PPC_INSN_SIZE;
693   opcode = insn >> 26;
694   breaks[1] = branch_dest (frame, opcode, insn, loc, breaks[0]);
695
696   /* Don't put two breakpoints on the same address.  */
697   if (breaks[1] == breaks[0])
698     breaks[1] = -1;
699
700   for (ii = 0; ii < 2; ++ii)
701     {
702       /* ignore invalid breakpoint.  */
703       if (breaks[ii] == -1)
704         continue;
705       insert_single_step_breakpoint (gdbarch, aspace, breaks[ii]);
706     }
707
708   errno = 0;                    /* FIXME, don't ignore errors!  */
709   /* What errors?  {read,write}_memory call error().  */
710   return 1;
711 }
712
713 /* Implement the "auto_wide_charset" gdbarch method for this platform.  */
714
715 static const char *
716 rs6000_aix_auto_wide_charset (void)
717 {
718   return "UTF-16";
719 }
720
721 /* Implement an osabi sniffer for RS6000/AIX.
722
723    This function assumes that ABFD's flavour is XCOFF.  In other words,
724    it should be registered as a sniffer for bfd_target_xcoff_flavour
725    objfiles only.  A failed assertion will be raised if this condition
726    is not met.  */
727
728 static enum gdb_osabi
729 rs6000_aix_osabi_sniffer (bfd *abfd)
730 {
731   gdb_assert (bfd_get_flavour (abfd) == bfd_target_xcoff_flavour);
732
733   /* The only noticeable difference between Lynx178 XCOFF files and
734      AIX XCOFF files comes from the fact that there are no shared
735      libraries on Lynx178.  On AIX, we are betting that an executable
736      linked with no shared library will never exist.  */
737   if (xcoff_get_n_import_files (abfd) <= 0)
738     return GDB_OSABI_UNKNOWN;
739
740   return GDB_OSABI_AIX;
741 }
742
743 /* A structure encoding the offset and size of a field within
744    a struct.  */
745
746 struct field_info
747 {
748   int offset;
749   int size;
750 };
751
752 /* A structure describing the layout of all the fields of interest
753    in AIX's struct ld_info.  Each field in this struct corresponds
754    to the field of the same name in struct ld_info.  */
755
756 struct ld_info_desc
757 {
758   struct field_info ldinfo_next;
759   struct field_info ldinfo_fd;
760   struct field_info ldinfo_textorg;
761   struct field_info ldinfo_textsize;
762   struct field_info ldinfo_dataorg;
763   struct field_info ldinfo_datasize;
764   struct field_info ldinfo_filename;
765 };
766
767 /* The following data has been generated by compiling and running
768    the following program on AIX 5.3.  */
769
770 #if 0
771 #include <stddef.h>
772 #include <stdio.h>
773 #define __LDINFO_PTRACE32__
774 #define __LDINFO_PTRACE64__
775 #include <sys/ldr.h>
776
777 #define pinfo(type,member)                  \
778   {                                         \
779     struct type ldi = {0};                  \
780                                             \
781     printf ("  {%d, %d},\t/* %s */\n",      \
782             offsetof (struct type, member), \
783             sizeof (ldi.member),            \
784             #member);                       \
785   }                                         \
786   while (0)
787
788 int
789 main (void)
790 {
791   printf ("static const struct ld_info_desc ld_info32_desc =\n{\n");
792   pinfo (__ld_info32, ldinfo_next);
793   pinfo (__ld_info32, ldinfo_fd);
794   pinfo (__ld_info32, ldinfo_textorg);
795   pinfo (__ld_info32, ldinfo_textsize);
796   pinfo (__ld_info32, ldinfo_dataorg);
797   pinfo (__ld_info32, ldinfo_datasize);
798   pinfo (__ld_info32, ldinfo_filename);
799   printf ("};\n");
800
801   printf ("\n");
802
803   printf ("static const struct ld_info_desc ld_info64_desc =\n{\n");
804   pinfo (__ld_info64, ldinfo_next);
805   pinfo (__ld_info64, ldinfo_fd);
806   pinfo (__ld_info64, ldinfo_textorg);
807   pinfo (__ld_info64, ldinfo_textsize);
808   pinfo (__ld_info64, ldinfo_dataorg);
809   pinfo (__ld_info64, ldinfo_datasize);
810   pinfo (__ld_info64, ldinfo_filename);
811   printf ("};\n");
812
813   return 0;
814 }
815 #endif /* 0 */
816
817 /* Layout of the 32bit version of struct ld_info.  */
818
819 static const struct ld_info_desc ld_info32_desc =
820 {
821   {0, 4},       /* ldinfo_next */
822   {4, 4},       /* ldinfo_fd */
823   {8, 4},       /* ldinfo_textorg */
824   {12, 4},      /* ldinfo_textsize */
825   {16, 4},      /* ldinfo_dataorg */
826   {20, 4},      /* ldinfo_datasize */
827   {24, 2},      /* ldinfo_filename */
828 };
829
830 /* Layout of the 64bit version of struct ld_info.  */
831
832 static const struct ld_info_desc ld_info64_desc =
833 {
834   {0, 4},       /* ldinfo_next */
835   {8, 4},       /* ldinfo_fd */
836   {16, 8},      /* ldinfo_textorg */
837   {24, 8},      /* ldinfo_textsize */
838   {32, 8},      /* ldinfo_dataorg */
839   {40, 8},      /* ldinfo_datasize */
840   {48, 2},      /* ldinfo_filename */
841 };
842
843 /* A structured representation of one entry read from the ld_info
844    binary data provided by the AIX loader.  */
845
846 struct ld_info
847 {
848   ULONGEST next;
849   int fd;
850   CORE_ADDR textorg;
851   ULONGEST textsize;
852   CORE_ADDR dataorg;
853   ULONGEST datasize;
854   char *filename;
855   char *member_name;
856 };
857
858 /* Return a struct ld_info object corresponding to the entry at
859    LDI_BUF.
860
861    Note that the filename and member_name strings still point
862    to the data in LDI_BUF.  So LDI_BUF must not be deallocated
863    while the struct ld_info object returned is in use.  */
864
865 static struct ld_info
866 rs6000_aix_extract_ld_info (struct gdbarch *gdbarch,
867                             const gdb_byte *ldi_buf)
868 {
869   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
870   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
871   struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
872   const struct ld_info_desc desc
873     = tdep->wordsize == 8 ? ld_info64_desc : ld_info32_desc;
874   struct ld_info info;
875
876   info.next = extract_unsigned_integer (ldi_buf + desc.ldinfo_next.offset,
877                                         desc.ldinfo_next.size,
878                                         byte_order);
879   info.fd = extract_signed_integer (ldi_buf + desc.ldinfo_fd.offset,
880                                     desc.ldinfo_fd.size,
881                                     byte_order);
882   info.textorg = extract_typed_address (ldi_buf + desc.ldinfo_textorg.offset,
883                                         ptr_type);
884   info.textsize
885     = extract_unsigned_integer (ldi_buf + desc.ldinfo_textsize.offset,
886                                 desc.ldinfo_textsize.size,
887                                 byte_order);
888   info.dataorg = extract_typed_address (ldi_buf + desc.ldinfo_dataorg.offset,
889                                         ptr_type);
890   info.datasize
891     = extract_unsigned_integer (ldi_buf + desc.ldinfo_datasize.offset,
892                                 desc.ldinfo_datasize.size,
893                                 byte_order);
894   info.filename = (char *) ldi_buf + desc.ldinfo_filename.offset;
895   info.member_name = info.filename + strlen (info.filename) + 1;
896
897   return info;
898 }
899
900 /* Append to OBJSTACK an XML string description of the shared library
901    corresponding to LDI, following the TARGET_OBJECT_LIBRARIES_AIX
902    format.  */
903
904 static void
905 rs6000_aix_shared_library_to_xml (struct ld_info *ldi,
906                                   struct obstack *obstack)
907 {
908   char *p;
909
910   obstack_grow_str (obstack, "<library name=\"");
911   p = xml_escape_text (ldi->filename);
912   obstack_grow_str (obstack, p);
913   xfree (p);
914   obstack_grow_str (obstack, "\"");
915
916   if (ldi->member_name[0] != '\0')
917     {
918       obstack_grow_str (obstack, " member=\"");
919       p = xml_escape_text (ldi->member_name);
920       obstack_grow_str (obstack, p);
921       xfree (p);
922       obstack_grow_str (obstack, "\"");
923     }
924
925   obstack_grow_str (obstack, " text_addr=\"");
926   obstack_grow_str (obstack, core_addr_to_string (ldi->textorg));
927   obstack_grow_str (obstack, "\"");
928
929   obstack_grow_str (obstack, " text_size=\"");
930   obstack_grow_str (obstack, pulongest (ldi->textsize));
931   obstack_grow_str (obstack, "\"");
932
933   obstack_grow_str (obstack, " data_addr=\"");
934   obstack_grow_str (obstack, core_addr_to_string (ldi->dataorg));
935   obstack_grow_str (obstack, "\"");
936
937   obstack_grow_str (obstack, " data_size=\"");
938   obstack_grow_str (obstack, pulongest (ldi->datasize));
939   obstack_grow_str (obstack, "\"");
940
941   obstack_grow_str (obstack, "></library>");
942 }
943
944 /* Convert the ld_info binary data provided by the AIX loader into
945    an XML representation following the TARGET_OBJECT_LIBRARIES_AIX
946    format.
947
948    LDI_BUF is a buffer containing the ld_info data.
949    READBUF, OFFSET and LEN follow the same semantics as target_ops'
950    to_xfer_partial target_ops method.
951
952    If CLOSE_LDINFO_FD is nonzero, then this routine also closes
953    the ldinfo_fd file descriptor.  This is useful when the ldinfo
954    data is obtained via ptrace, as ptrace opens a file descriptor
955    for each and every entry; but we cannot use this descriptor
956    as the consumer of the XML library list might live in a different
957    process.  */
958
959 ULONGEST
960 rs6000_aix_ld_info_to_xml (struct gdbarch *gdbarch, const gdb_byte *ldi_buf,
961                            gdb_byte *readbuf, ULONGEST offset, ULONGEST len,
962                            int close_ldinfo_fd)
963 {
964   struct obstack obstack;
965   const char *buf;
966   ULONGEST len_avail;
967
968   obstack_init (&obstack);
969   obstack_grow_str (&obstack, "<library-list-aix version=\"1.0\">\n");
970
971   while (1)
972     {
973       struct ld_info ldi = rs6000_aix_extract_ld_info (gdbarch, ldi_buf);
974
975       rs6000_aix_shared_library_to_xml (&ldi, &obstack);
976       if (close_ldinfo_fd)
977         close (ldi.fd);
978
979       if (!ldi.next)
980         break;
981       ldi_buf = ldi_buf + ldi.next;
982     }
983
984   obstack_grow_str0 (&obstack, "</library-list-aix>\n");
985
986   buf = obstack_finish (&obstack);
987   len_avail = strlen (buf);
988   if (offset >= len_avail)
989     len= 0;
990   else
991     {
992       if (len > len_avail - offset)
993         len = len_avail - offset;
994       memcpy (readbuf, buf + offset, len);
995     }
996
997   obstack_free (&obstack, NULL);
998   return len;
999 }
1000
1001 /* Implement the core_xfer_shared_libraries_aix gdbarch method.  */
1002
1003 static ULONGEST
1004 rs6000_aix_core_xfer_shared_libraries_aix (struct gdbarch *gdbarch,
1005                                            gdb_byte *readbuf,
1006                                            ULONGEST offset,
1007                                            ULONGEST len)
1008 {
1009   struct bfd_section *ldinfo_sec;
1010   int ldinfo_size;
1011   gdb_byte *ldinfo_buf;
1012   struct cleanup *cleanup;
1013   LONGEST result;
1014
1015   ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo");
1016   if (ldinfo_sec == NULL)
1017     error (_("cannot find .ldinfo section from core file: %s"),
1018            bfd_errmsg (bfd_get_error ()));
1019   ldinfo_size = bfd_get_section_size (ldinfo_sec);
1020
1021   ldinfo_buf = xmalloc (ldinfo_size);
1022   cleanup = make_cleanup (xfree, ldinfo_buf);
1023
1024   if (! bfd_get_section_contents (core_bfd, ldinfo_sec,
1025                                   ldinfo_buf, 0, ldinfo_size))
1026     error (_("unable to read .ldinfo section from core file: %s"),
1027           bfd_errmsg (bfd_get_error ()));
1028
1029   result = rs6000_aix_ld_info_to_xml (gdbarch, ldinfo_buf, readbuf,
1030                                       offset, len, 0);
1031
1032   do_cleanups (cleanup);
1033   return result;
1034 }
1035
1036 static void
1037 rs6000_aix_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
1038 {
1039   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1040
1041   /* RS6000/AIX does not support PT_STEP.  Has to be simulated.  */
1042   set_gdbarch_software_single_step (gdbarch, rs6000_software_single_step);
1043
1044   /* Displaced stepping is currently not supported in combination with
1045      software single-stepping.  */
1046   set_gdbarch_displaced_step_copy_insn (gdbarch, NULL);
1047   set_gdbarch_displaced_step_fixup (gdbarch, NULL);
1048   set_gdbarch_displaced_step_free_closure (gdbarch, NULL);
1049   set_gdbarch_displaced_step_location (gdbarch, NULL);
1050
1051   set_gdbarch_push_dummy_call (gdbarch, rs6000_push_dummy_call);
1052   set_gdbarch_return_value (gdbarch, rs6000_return_value);
1053   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1054
1055   /* Handle RS/6000 function pointers (which are really function
1056      descriptors).  */
1057   set_gdbarch_convert_from_func_ptr_addr
1058     (gdbarch, rs6000_convert_from_func_ptr_addr);
1059
1060   /* Core file support.  */
1061   set_gdbarch_iterate_over_regset_sections
1062     (gdbarch, rs6000_aix_iterate_over_regset_sections);
1063   set_gdbarch_core_xfer_shared_libraries_aix
1064     (gdbarch, rs6000_aix_core_xfer_shared_libraries_aix);
1065
1066   if (tdep->wordsize == 8)
1067     tdep->lr_frame_offset = 16;
1068   else
1069     tdep->lr_frame_offset = 8;
1070
1071   if (tdep->wordsize == 4)
1072     /* PowerOpen / AIX 32 bit.  The saved area or red zone consists of
1073        19 4 byte GPRS + 18 8 byte FPRs giving a total of 220 bytes.
1074        Problem is, 220 isn't frame (16 byte) aligned.  Round it up to
1075        224.  */
1076     set_gdbarch_frame_red_zone_size (gdbarch, 224);
1077   else
1078     set_gdbarch_frame_red_zone_size (gdbarch, 0);
1079
1080   set_gdbarch_auto_wide_charset (gdbarch, rs6000_aix_auto_wide_charset);
1081
1082   set_solib_ops (gdbarch, &solib_aix_so_ops);
1083 }
1084
1085 /* Provide a prototype to silence -Wmissing-prototypes.  */
1086 extern initialize_file_ftype _initialize_rs6000_aix_tdep;
1087
1088 void
1089 _initialize_rs6000_aix_tdep (void)
1090 {
1091   gdbarch_register_osabi_sniffer (bfd_arch_rs6000,
1092                                   bfd_target_xcoff_flavour,
1093                                   rs6000_aix_osabi_sniffer);
1094   gdbarch_register_osabi_sniffer (bfd_arch_powerpc,
1095                                   bfd_target_xcoff_flavour,
1096                                   rs6000_aix_osabi_sniffer);
1097
1098   gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_AIX,
1099                           rs6000_aix_init_osabi);
1100   gdbarch_register_osabi (bfd_arch_powerpc, 0, GDB_OSABI_AIX,
1101                           rs6000_aix_init_osabi);
1102 }
1103