41384993c26ab38acc59c7f222a0a46859d6a3f0
[binutils-gdb.git] / gdb / rs6000-aix-tdep.c
1 /* Native support code for PPC AIX, for GDB the GNU debugger.
2
3 Copyright (C) 2006-2022 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 (struct frame_info *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 (struct frame_info *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 (struct frame_info *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 struct frame_info *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 (type);
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 memcpy (word, value_contents (arg).data (), len);
404 regcache->cooked_write (tdep->ppc_gp0_regnum + 3 +ii, word);
405 }
406 ++argno;
407 }
408
409 ran_out_of_registers_for_arguments:
410
411 regcache_cooked_read_unsigned (regcache,
412 gdbarch_sp_regnum (gdbarch),
413 &saved_sp);
414
415 /* Location for 8 parameters are always reserved. */
416 sp -= wordsize * 8;
417
418 /* Another six words for back chain, TOC register, link register, etc. */
419 sp -= wordsize * 6;
420
421 /* Stack pointer must be quadword aligned. */
422 sp &= -16;
423
424 /* If there are more arguments, allocate space for them in
425 the stack, then push them starting from the ninth one. */
426
427 if ((argno < nargs) || argbytes)
428 {
429 int space = 0, jj;
430
431 if (argbytes)
432 {
433 space += ((len - argbytes + 3) & -4);
434 jj = argno + 1;
435 }
436 else
437 jj = argno;
438
439 for (; jj < nargs; ++jj)
440 {
441 struct value *val = args[jj];
442 space += ((TYPE_LENGTH (value_type (val))) + 3) & -4;
443 }
444
445 /* Add location required for the rest of the parameters. */
446 space = (space + 15) & -16;
447 sp -= space;
448
449 /* This is another instance we need to be concerned about
450 securing our stack space. If we write anything underneath %sp
451 (r1), we might conflict with the kernel who thinks he is free
452 to use this area. So, update %sp first before doing anything
453 else. */
454
455 regcache_raw_write_signed (regcache,
456 gdbarch_sp_regnum (gdbarch), sp);
457
458 /* If the last argument copied into the registers didn't fit there
459 completely, push the rest of it into stack. */
460
461 if (argbytes)
462 {
463 write_memory (sp + 24 + (ii * 4),
464 value_contents (arg).data () + argbytes,
465 len - argbytes);
466 ++argno;
467 ii += ((len - argbytes + 3) & -4) / 4;
468 }
469
470 /* Push the rest of the arguments into stack. */
471 for (; argno < nargs; ++argno)
472 {
473
474 arg = args[argno];
475 type = check_typedef (value_type (arg));
476 len = TYPE_LENGTH (type);
477
478
479 /* Float types should be passed in fpr's, as well as in the
480 stack. */
481 if (type->code () == TYPE_CODE_FLT && f_argno < 13)
482 {
483
484 gdb_assert (len <= 8);
485
486 regcache->cooked_write (tdep->ppc_fp0_regnum + 1 + f_argno,
487 value_contents (arg).data ());
488 ++f_argno;
489 }
490
491 write_memory (sp + 24 + (ii * 4), value_contents (arg).data (), len);
492 ii += ((len + 3) & -4) / 4;
493 }
494 }
495
496 /* Set the stack pointer. According to the ABI, the SP is meant to
497 be set _before_ the corresponding stack space is used. On AIX,
498 this even applies when the target has been completely stopped!
499 Not doing this can lead to conflicts with the kernel which thinks
500 that it still has control over this not-yet-allocated stack
501 region. */
502 regcache_raw_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
503
504 /* Set back chain properly. */
505 store_unsigned_integer (tmp_buffer, wordsize, byte_order, saved_sp);
506 write_memory (sp, tmp_buffer, wordsize);
507
508 /* Point the inferior function call's return address at the dummy's
509 breakpoint. */
510 regcache_raw_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
511
512 /* Set the TOC register value. */
513 regcache_raw_write_signed (regcache, tdep->ppc_toc_regnum,
514 solib_aix_get_toc_value (func_addr));
515
516 target_store_registers (regcache, -1);
517 return sp;
518 }
519
520 static enum return_value_convention
521 rs6000_return_value (struct gdbarch *gdbarch, struct value *function,
522 struct type *valtype, struct regcache *regcache,
523 gdb_byte *readbuf, const gdb_byte *writebuf)
524 {
525 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
526 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
527
528 /* The calling convention this function implements assumes the
529 processor has floating-point registers. We shouldn't be using it
530 on PowerPC variants that lack them. */
531 gdb_assert (ppc_floating_point_unit_p (gdbarch));
532
533 /* AltiVec extension: Functions that declare a vector data type as a
534 return value place that return value in VR2. */
535 if (valtype->code () == TYPE_CODE_ARRAY && valtype->is_vector ()
536 && TYPE_LENGTH (valtype) == 16)
537 {
538 if (readbuf)
539 regcache->cooked_read (tdep->ppc_vr0_regnum + 2, readbuf);
540 if (writebuf)
541 regcache->cooked_write (tdep->ppc_vr0_regnum + 2, writebuf);
542
543 return RETURN_VALUE_REGISTER_CONVENTION;
544 }
545
546 /* If the called subprogram returns an aggregate, there exists an
547 implicit first argument, whose value is the address of a caller-
548 allocated buffer into which the callee is assumed to store its
549 return value. All explicit parameters are appropriately
550 relabeled. */
551 if (valtype->code () == TYPE_CODE_STRUCT
552 || valtype->code () == TYPE_CODE_UNION
553 || valtype->code () == TYPE_CODE_ARRAY)
554 return RETURN_VALUE_STRUCT_CONVENTION;
555
556 /* Scalar floating-point values are returned in FPR1 for float or
557 double, and in FPR1:FPR2 for quadword precision. Fortran
558 complex*8 and complex*16 are returned in FPR1:FPR2, and
559 complex*32 is returned in FPR1:FPR4. */
560 if (valtype->code () == TYPE_CODE_FLT
561 && (TYPE_LENGTH (valtype) == 4 || TYPE_LENGTH (valtype) == 8))
562 {
563 struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
564 gdb_byte regval[8];
565
566 /* FIXME: kettenis/2007-01-01: Add support for quadword
567 precision and complex. */
568
569 if (readbuf)
570 {
571 regcache->cooked_read (tdep->ppc_fp0_regnum + 1, regval);
572 target_float_convert (regval, regtype, readbuf, valtype);
573 }
574 if (writebuf)
575 {
576 target_float_convert (writebuf, valtype, regval, regtype);
577 regcache->cooked_write (tdep->ppc_fp0_regnum + 1, regval);
578 }
579
580 return RETURN_VALUE_REGISTER_CONVENTION;
581 }
582
583 /* Values of the types int, long, short, pointer, and char (length
584 is less than or equal to four bytes), as well as bit values of
585 lengths less than or equal to 32 bits, must be returned right
586 justified in GPR3 with signed values sign extended and unsigned
587 values zero extended, as necessary. */
588 if (TYPE_LENGTH (valtype) <= tdep->wordsize)
589 {
590 if (readbuf)
591 {
592 ULONGEST regval;
593
594 /* For reading we don't have to worry about sign extension. */
595 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
596 &regval);
597 store_unsigned_integer (readbuf, TYPE_LENGTH (valtype), byte_order,
598 regval);
599 }
600 if (writebuf)
601 {
602 /* For writing, use unpack_long since that should handle any
603 required sign extension. */
604 regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
605 unpack_long (valtype, writebuf));
606 }
607
608 return RETURN_VALUE_REGISTER_CONVENTION;
609 }
610
611 /* Eight-byte non-floating-point scalar values must be returned in
612 GPR3:GPR4. */
613
614 if (TYPE_LENGTH (valtype) == 8)
615 {
616 gdb_assert (valtype->code () != TYPE_CODE_FLT);
617 gdb_assert (tdep->wordsize == 4);
618
619 if (readbuf)
620 {
621 gdb_byte regval[8];
622
623 regcache->cooked_read (tdep->ppc_gp0_regnum + 3, regval);
624 regcache->cooked_read (tdep->ppc_gp0_regnum + 4, regval + 4);
625 memcpy (readbuf, regval, 8);
626 }
627 if (writebuf)
628 {
629 regcache->cooked_write (tdep->ppc_gp0_regnum + 3, writebuf);
630 regcache->cooked_write (tdep->ppc_gp0_regnum + 4, writebuf + 4);
631 }
632
633 return RETURN_VALUE_REGISTER_CONVENTION;
634 }
635
636 return RETURN_VALUE_STRUCT_CONVENTION;
637 }
638
639 /* Support for CONVERT_FROM_FUNC_PTR_ADDR (ARCH, ADDR, TARG).
640
641 Usually a function pointer's representation is simply the address
642 of the function. On the RS/6000 however, a function pointer is
643 represented by a pointer to an OPD entry. This OPD entry contains
644 three words, the first word is the address of the function, the
645 second word is the TOC pointer (r2), and the third word is the
646 static chain value. Throughout GDB it is currently assumed that a
647 function pointer contains the address of the function, which is not
648 easy to fix. In addition, the conversion of a function address to
649 a function pointer would require allocation of an OPD entry in the
650 inferior's memory space, with all its drawbacks. To be able to
651 call C++ virtual methods in the inferior (which are called via
652 function pointers), find_function_addr uses this function to get the
653 function address from a function pointer. */
654
655 /* Return real function address if ADDR (a function pointer) is in the data
656 space and is therefore a special function pointer. */
657
658 static CORE_ADDR
659 rs6000_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
660 CORE_ADDR addr,
661 struct target_ops *targ)
662 {
663 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
664 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
665 struct obj_section *s;
666
667 s = find_pc_section (addr);
668
669 /* Normally, functions live inside a section that is executable.
670 So, if ADDR points to a non-executable section, then treat it
671 as a function descriptor and return the target address iff
672 the target address itself points to a section that is executable. */
673 if (s && (s->the_bfd_section->flags & SEC_CODE) == 0)
674 {
675 CORE_ADDR pc = 0;
676 struct obj_section *pc_section;
677
678 try
679 {
680 pc = read_memory_unsigned_integer (addr, tdep->wordsize, byte_order);
681 }
682 catch (const gdb_exception_error &e)
683 {
684 /* An error occured during reading. Probably a memory error
685 due to the section not being loaded yet. This address
686 cannot be a function descriptor. */
687 return addr;
688 }
689
690 pc_section = find_pc_section (pc);
691
692 if (pc_section && (pc_section->the_bfd_section->flags & SEC_CODE))
693 return pc;
694 }
695
696 return addr;
697 }
698
699
700 /* Calculate the destination of a branch/jump. Return -1 if not a branch. */
701
702 static CORE_ADDR
703 branch_dest (struct regcache *regcache, int opcode, int instr,
704 CORE_ADDR pc, CORE_ADDR safety)
705 {
706 struct gdbarch *gdbarch = regcache->arch ();
707 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
708 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
709 CORE_ADDR dest;
710 int immediate;
711 int absolute;
712 int ext_op;
713
714 absolute = (int) ((instr >> 1) & 1);
715
716 switch (opcode)
717 {
718 case 18:
719 immediate = ((instr & ~3) << 6) >> 6; /* br unconditional */
720 if (absolute)
721 dest = immediate;
722 else
723 dest = pc + immediate;
724 break;
725
726 case 16:
727 immediate = ((instr & ~3) << 16) >> 16; /* br conditional */
728 if (absolute)
729 dest = immediate;
730 else
731 dest = pc + immediate;
732 break;
733
734 case 19:
735 ext_op = (instr >> 1) & 0x3ff;
736
737 if (ext_op == 16) /* br conditional register */
738 {
739 dest = regcache_raw_get_unsigned (regcache, tdep->ppc_lr_regnum) & ~3;
740
741 /* If we are about to return from a signal handler, dest is
742 something like 0x3c90. The current frame is a signal handler
743 caller frame, upon completion of the sigreturn system call
744 execution will return to the saved PC in the frame. */
745 if (dest < AIX_TEXT_SEGMENT_BASE)
746 {
747 struct frame_info *frame = get_current_frame ();
748
749 dest = read_memory_unsigned_integer
750 (get_frame_base (frame) + SIG_FRAME_PC_OFFSET,
751 tdep->wordsize, byte_order);
752 }
753 }
754
755 else if (ext_op == 528) /* br cond to count reg */
756 {
757 dest = regcache_raw_get_unsigned (regcache,
758 tdep->ppc_ctr_regnum) & ~3;
759
760 /* If we are about to execute a system call, dest is something
761 like 0x22fc or 0x3b00. Upon completion the system call
762 will return to the address in the link register. */
763 if (dest < AIX_TEXT_SEGMENT_BASE)
764 dest = regcache_raw_get_unsigned (regcache,
765 tdep->ppc_lr_regnum) & ~3;
766 }
767 else
768 return -1;
769 break;
770
771 default:
772 return -1;
773 }
774 return (dest < AIX_TEXT_SEGMENT_BASE) ? safety : dest;
775 }
776
777 /* AIX does not support PT_STEP. Simulate it. */
778
779 static std::vector<CORE_ADDR>
780 rs6000_software_single_step (struct regcache *regcache)
781 {
782 struct gdbarch *gdbarch = regcache->arch ();
783 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
784 int ii, insn;
785 CORE_ADDR loc;
786 CORE_ADDR breaks[2];
787 int opcode;
788
789 loc = regcache_read_pc (regcache);
790
791 insn = read_memory_integer (loc, 4, byte_order);
792
793 std::vector<CORE_ADDR> next_pcs = ppc_deal_with_atomic_sequence (regcache);
794 if (!next_pcs.empty ())
795 return next_pcs;
796
797 breaks[0] = loc + PPC_INSN_SIZE;
798 opcode = insn >> 26;
799 breaks[1] = branch_dest (regcache, opcode, insn, loc, breaks[0]);
800
801 /* Don't put two breakpoints on the same address. */
802 if (breaks[1] == breaks[0])
803 breaks[1] = -1;
804
805 for (ii = 0; ii < 2; ++ii)
806 {
807 /* ignore invalid breakpoint. */
808 if (breaks[ii] == -1)
809 continue;
810
811 next_pcs.push_back (breaks[ii]);
812 }
813
814 errno = 0; /* FIXME, don't ignore errors! */
815 /* What errors? {read,write}_memory call error(). */
816 return next_pcs;
817 }
818
819 /* Implement the "auto_wide_charset" gdbarch method for this platform. */
820
821 static const char *
822 rs6000_aix_auto_wide_charset (void)
823 {
824 return "UTF-16";
825 }
826
827 /* Implement an osabi sniffer for RS6000/AIX.
828
829 This function assumes that ABFD's flavour is XCOFF. In other words,
830 it should be registered as a sniffer for bfd_target_xcoff_flavour
831 objfiles only. A failed assertion will be raised if this condition
832 is not met. */
833
834 static enum gdb_osabi
835 rs6000_aix_osabi_sniffer (bfd *abfd)
836 {
837 gdb_assert (bfd_get_flavour (abfd) == bfd_target_xcoff_flavour);
838
839 /* The only noticeable difference between Lynx178 XCOFF files and
840 AIX XCOFF files comes from the fact that there are no shared
841 libraries on Lynx178. On AIX, we are betting that an executable
842 linked with no shared library will never exist. */
843 if (xcoff_get_n_import_files (abfd) <= 0)
844 return GDB_OSABI_UNKNOWN;
845
846 return GDB_OSABI_AIX;
847 }
848
849 /* A structure encoding the offset and size of a field within
850 a struct. */
851
852 struct ldinfo_field
853 {
854 int offset;
855 int size;
856 };
857
858 /* A structure describing the layout of all the fields of interest
859 in AIX's struct ld_info. Each field in this struct corresponds
860 to the field of the same name in struct ld_info. */
861
862 struct ld_info_desc
863 {
864 struct ldinfo_field ldinfo_next;
865 struct ldinfo_field ldinfo_fd;
866 struct ldinfo_field ldinfo_textorg;
867 struct ldinfo_field ldinfo_textsize;
868 struct ldinfo_field ldinfo_dataorg;
869 struct ldinfo_field ldinfo_datasize;
870 struct ldinfo_field ldinfo_filename;
871 };
872
873 /* The following data has been generated by compiling and running
874 the following program on AIX 5.3. */
875
876 #if 0
877 #include <stddef.h>
878 #include <stdio.h>
879 #define __LDINFO_PTRACE32__
880 #define __LDINFO_PTRACE64__
881 #include <sys/ldr.h>
882
883 #define pinfo(type,member) \
884 { \
885 struct type ldi = {0}; \
886 \
887 printf (" {%d, %d},\t/* %s */\n", \
888 offsetof (struct type, member), \
889 sizeof (ldi.member), \
890 #member); \
891 } \
892 while (0)
893
894 int
895 main (void)
896 {
897 printf ("static const struct ld_info_desc ld_info32_desc =\n{\n");
898 pinfo (__ld_info32, ldinfo_next);
899 pinfo (__ld_info32, ldinfo_fd);
900 pinfo (__ld_info32, ldinfo_textorg);
901 pinfo (__ld_info32, ldinfo_textsize);
902 pinfo (__ld_info32, ldinfo_dataorg);
903 pinfo (__ld_info32, ldinfo_datasize);
904 pinfo (__ld_info32, ldinfo_filename);
905 printf ("};\n");
906
907 printf ("\n");
908
909 printf ("static const struct ld_info_desc ld_info64_desc =\n{\n");
910 pinfo (__ld_info64, ldinfo_next);
911 pinfo (__ld_info64, ldinfo_fd);
912 pinfo (__ld_info64, ldinfo_textorg);
913 pinfo (__ld_info64, ldinfo_textsize);
914 pinfo (__ld_info64, ldinfo_dataorg);
915 pinfo (__ld_info64, ldinfo_datasize);
916 pinfo (__ld_info64, ldinfo_filename);
917 printf ("};\n");
918
919 return 0;
920 }
921 #endif /* 0 */
922
923 /* Layout of the 32bit version of struct ld_info. */
924
925 static const struct ld_info_desc ld_info32_desc =
926 {
927 {0, 4}, /* ldinfo_next */
928 {4, 4}, /* ldinfo_fd */
929 {8, 4}, /* ldinfo_textorg */
930 {12, 4}, /* ldinfo_textsize */
931 {16, 4}, /* ldinfo_dataorg */
932 {20, 4}, /* ldinfo_datasize */
933 {24, 2}, /* ldinfo_filename */
934 };
935
936 /* Layout of the 64bit version of struct ld_info. */
937
938 static const struct ld_info_desc ld_info64_desc =
939 {
940 {0, 4}, /* ldinfo_next */
941 {8, 4}, /* ldinfo_fd */
942 {16, 8}, /* ldinfo_textorg */
943 {24, 8}, /* ldinfo_textsize */
944 {32, 8}, /* ldinfo_dataorg */
945 {40, 8}, /* ldinfo_datasize */
946 {48, 2}, /* ldinfo_filename */
947 };
948
949 /* A structured representation of one entry read from the ld_info
950 binary data provided by the AIX loader. */
951
952 struct ld_info
953 {
954 ULONGEST next;
955 int fd;
956 CORE_ADDR textorg;
957 ULONGEST textsize;
958 CORE_ADDR dataorg;
959 ULONGEST datasize;
960 char *filename;
961 char *member_name;
962 };
963
964 /* Return a struct ld_info object corresponding to the entry at
965 LDI_BUF.
966
967 Note that the filename and member_name strings still point
968 to the data in LDI_BUF. So LDI_BUF must not be deallocated
969 while the struct ld_info object returned is in use. */
970
971 static struct ld_info
972 rs6000_aix_extract_ld_info (struct gdbarch *gdbarch,
973 const gdb_byte *ldi_buf)
974 {
975 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
976 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
977 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
978 const struct ld_info_desc desc
979 = tdep->wordsize == 8 ? ld_info64_desc : ld_info32_desc;
980 struct ld_info info;
981
982 info.next = extract_unsigned_integer (ldi_buf + desc.ldinfo_next.offset,
983 desc.ldinfo_next.size,
984 byte_order);
985 info.fd = extract_signed_integer (ldi_buf + desc.ldinfo_fd.offset,
986 desc.ldinfo_fd.size,
987 byte_order);
988 info.textorg = extract_typed_address (ldi_buf + desc.ldinfo_textorg.offset,
989 ptr_type);
990 info.textsize
991 = extract_unsigned_integer (ldi_buf + desc.ldinfo_textsize.offset,
992 desc.ldinfo_textsize.size,
993 byte_order);
994 info.dataorg = extract_typed_address (ldi_buf + desc.ldinfo_dataorg.offset,
995 ptr_type);
996 info.datasize
997 = extract_unsigned_integer (ldi_buf + desc.ldinfo_datasize.offset,
998 desc.ldinfo_datasize.size,
999 byte_order);
1000 info.filename = (char *) ldi_buf + desc.ldinfo_filename.offset;
1001 info.member_name = info.filename + strlen (info.filename) + 1;
1002
1003 return info;
1004 }
1005
1006 /* Append to OBJSTACK an XML string description of the shared library
1007 corresponding to LDI, following the TARGET_OBJECT_LIBRARIES_AIX
1008 format. */
1009
1010 static void
1011 rs6000_aix_shared_library_to_xml (struct ld_info *ldi,
1012 struct obstack *obstack)
1013 {
1014 obstack_grow_str (obstack, "<library name=\"");
1015 std::string p = xml_escape_text (ldi->filename);
1016 obstack_grow_str (obstack, p.c_str ());
1017 obstack_grow_str (obstack, "\"");
1018
1019 if (ldi->member_name[0] != '\0')
1020 {
1021 obstack_grow_str (obstack, " member=\"");
1022 p = xml_escape_text (ldi->member_name);
1023 obstack_grow_str (obstack, p.c_str ());
1024 obstack_grow_str (obstack, "\"");
1025 }
1026
1027 obstack_grow_str (obstack, " text_addr=\"");
1028 obstack_grow_str (obstack, core_addr_to_string (ldi->textorg));
1029 obstack_grow_str (obstack, "\"");
1030
1031 obstack_grow_str (obstack, " text_size=\"");
1032 obstack_grow_str (obstack, pulongest (ldi->textsize));
1033 obstack_grow_str (obstack, "\"");
1034
1035 obstack_grow_str (obstack, " data_addr=\"");
1036 obstack_grow_str (obstack, core_addr_to_string (ldi->dataorg));
1037 obstack_grow_str (obstack, "\"");
1038
1039 obstack_grow_str (obstack, " data_size=\"");
1040 obstack_grow_str (obstack, pulongest (ldi->datasize));
1041 obstack_grow_str (obstack, "\"");
1042
1043 obstack_grow_str (obstack, "></library>");
1044 }
1045
1046 /* Convert the ld_info binary data provided by the AIX loader into
1047 an XML representation following the TARGET_OBJECT_LIBRARIES_AIX
1048 format.
1049
1050 LDI_BUF is a buffer containing the ld_info data.
1051 READBUF, OFFSET and LEN follow the same semantics as target_ops'
1052 to_xfer_partial target_ops method.
1053
1054 If CLOSE_LDINFO_FD is nonzero, then this routine also closes
1055 the ldinfo_fd file descriptor. This is useful when the ldinfo
1056 data is obtained via ptrace, as ptrace opens a file descriptor
1057 for each and every entry; but we cannot use this descriptor
1058 as the consumer of the XML library list might live in a different
1059 process. */
1060
1061 ULONGEST
1062 rs6000_aix_ld_info_to_xml (struct gdbarch *gdbarch, const gdb_byte *ldi_buf,
1063 gdb_byte *readbuf, ULONGEST offset, ULONGEST len,
1064 int close_ldinfo_fd)
1065 {
1066 struct obstack obstack;
1067 const char *buf;
1068 ULONGEST len_avail;
1069
1070 obstack_init (&obstack);
1071 obstack_grow_str (&obstack, "<library-list-aix version=\"1.0\">\n");
1072
1073 while (1)
1074 {
1075 struct ld_info ldi = rs6000_aix_extract_ld_info (gdbarch, ldi_buf);
1076
1077 rs6000_aix_shared_library_to_xml (&ldi, &obstack);
1078 if (close_ldinfo_fd)
1079 close (ldi.fd);
1080
1081 if (!ldi.next)
1082 break;
1083 ldi_buf = ldi_buf + ldi.next;
1084 }
1085
1086 obstack_grow_str0 (&obstack, "</library-list-aix>\n");
1087
1088 buf = (const char *) obstack_finish (&obstack);
1089 len_avail = strlen (buf);
1090 if (offset >= len_avail)
1091 len= 0;
1092 else
1093 {
1094 if (len > len_avail - offset)
1095 len = len_avail - offset;
1096 memcpy (readbuf, buf + offset, len);
1097 }
1098
1099 obstack_free (&obstack, NULL);
1100 return len;
1101 }
1102
1103 /* Implement the core_xfer_shared_libraries_aix gdbarch method. */
1104
1105 static ULONGEST
1106 rs6000_aix_core_xfer_shared_libraries_aix (struct gdbarch *gdbarch,
1107 gdb_byte *readbuf,
1108 ULONGEST offset,
1109 ULONGEST len)
1110 {
1111 struct bfd_section *ldinfo_sec;
1112 int ldinfo_size;
1113
1114 ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo");
1115 if (ldinfo_sec == NULL)
1116 error (_("cannot find .ldinfo section from core file: %s"),
1117 bfd_errmsg (bfd_get_error ()));
1118 ldinfo_size = bfd_section_size (ldinfo_sec);
1119
1120 gdb::byte_vector ldinfo_buf (ldinfo_size);
1121
1122 if (! bfd_get_section_contents (core_bfd, ldinfo_sec,
1123 ldinfo_buf.data (), 0, ldinfo_size))
1124 error (_("unable to read .ldinfo section from core file: %s"),
1125 bfd_errmsg (bfd_get_error ()));
1126
1127 return rs6000_aix_ld_info_to_xml (gdbarch, ldinfo_buf.data (), readbuf,
1128 offset, len, 0);
1129 }
1130
1131 static void
1132 rs6000_aix_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
1133 {
1134 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1135
1136 /* RS6000/AIX does not support PT_STEP. Has to be simulated. */
1137 set_gdbarch_software_single_step (gdbarch, rs6000_software_single_step);
1138
1139 /* Displaced stepping is currently not supported in combination with
1140 software single-stepping. These override the values set by
1141 rs6000_gdbarch_init. */
1142 set_gdbarch_displaced_step_copy_insn (gdbarch, NULL);
1143 set_gdbarch_displaced_step_fixup (gdbarch, NULL);
1144 set_gdbarch_displaced_step_prepare (gdbarch, NULL);
1145 set_gdbarch_displaced_step_finish (gdbarch, NULL);
1146
1147 set_gdbarch_push_dummy_call (gdbarch, rs6000_push_dummy_call);
1148 set_gdbarch_return_value (gdbarch, rs6000_return_value);
1149 set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1150
1151 /* Handle RS/6000 function pointers (which are really function
1152 descriptors). */
1153 set_gdbarch_convert_from_func_ptr_addr
1154 (gdbarch, rs6000_convert_from_func_ptr_addr);
1155
1156 /* Core file support. */
1157 set_gdbarch_iterate_over_regset_sections
1158 (gdbarch, rs6000_aix_iterate_over_regset_sections);
1159 set_gdbarch_core_xfer_shared_libraries_aix
1160 (gdbarch, rs6000_aix_core_xfer_shared_libraries_aix);
1161
1162 if (tdep->wordsize == 8)
1163 tdep->lr_frame_offset = 16;
1164 else
1165 tdep->lr_frame_offset = 8;
1166
1167 if (tdep->wordsize == 4)
1168 /* PowerOpen / AIX 32 bit. The saved area or red zone consists of
1169 19 4 byte GPRS + 18 8 byte FPRs giving a total of 220 bytes.
1170 Problem is, 220 isn't frame (16 byte) aligned. Round it up to
1171 224. */
1172 set_gdbarch_frame_red_zone_size (gdbarch, 224);
1173 else
1174 set_gdbarch_frame_red_zone_size (gdbarch, 0);
1175
1176 if (tdep->wordsize == 8)
1177 set_gdbarch_wchar_bit (gdbarch, 32);
1178 else
1179 set_gdbarch_wchar_bit (gdbarch, 16);
1180 set_gdbarch_wchar_signed (gdbarch, 0);
1181 set_gdbarch_auto_wide_charset (gdbarch, rs6000_aix_auto_wide_charset);
1182
1183 set_solib_ops (gdbarch, &solib_aix_so_ops);
1184 frame_unwind_append_unwinder (gdbarch, &aix_sighandle_frame_unwind);
1185 }
1186
1187 void _initialize_rs6000_aix_tdep ();
1188 void
1189 _initialize_rs6000_aix_tdep ()
1190 {
1191 gdbarch_register_osabi_sniffer (bfd_arch_rs6000,
1192 bfd_target_xcoff_flavour,
1193 rs6000_aix_osabi_sniffer);
1194 gdbarch_register_osabi_sniffer (bfd_arch_powerpc,
1195 bfd_target_xcoff_flavour,
1196 rs6000_aix_osabi_sniffer);
1197
1198 gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_AIX,
1199 rs6000_aix_init_osabi);
1200 gdbarch_register_osabi (bfd_arch_powerpc, 0, GDB_OSABI_AIX,
1201 rs6000_aix_init_osabi);
1202 }
1203