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