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