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