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