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