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