1 /* Target-dependent code for GDB, the GNU debugger.
3 Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 Contributed by D.J. Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
6 for IBM Deutschland Entwicklung GmbH, IBM Corporation.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include "arch-utils.h"
35 #include "../bfd/bfd.h"
36 #include "floatformat.h"
38 #include "trad-frame.h"
39 #include "frame-base.h"
40 #include "frame-unwind.h"
41 #include "dwarf2-frame.h"
42 #include "reggroups.h"
45 #include "gdb_assert.h"
47 #include "solib-svr4.h" /* For struct link_map_offsets. */
49 #include "s390-tdep.h"
52 /* The tdep structure. */
57 enum { ABI_LINUX_S390, ABI_LINUX_ZSERIES } abi;
59 /* Core file register sets. */
60 const struct regset *gregset;
63 const struct regset *fpregset;
68 /* Register information. */
70 struct s390_register_info
76 static struct s390_register_info s390_register_info[S390_NUM_TOTAL_REGS] =
78 /* Program Status Word. */
79 { "pswm", &builtin_type_long },
80 { "pswa", &builtin_type_long },
82 /* General Purpose Registers. */
83 { "r0", &builtin_type_long },
84 { "r1", &builtin_type_long },
85 { "r2", &builtin_type_long },
86 { "r3", &builtin_type_long },
87 { "r4", &builtin_type_long },
88 { "r5", &builtin_type_long },
89 { "r6", &builtin_type_long },
90 { "r7", &builtin_type_long },
91 { "r8", &builtin_type_long },
92 { "r9", &builtin_type_long },
93 { "r10", &builtin_type_long },
94 { "r11", &builtin_type_long },
95 { "r12", &builtin_type_long },
96 { "r13", &builtin_type_long },
97 { "r14", &builtin_type_long },
98 { "r15", &builtin_type_long },
100 /* Access Registers. */
101 { "acr0", &builtin_type_int },
102 { "acr1", &builtin_type_int },
103 { "acr2", &builtin_type_int },
104 { "acr3", &builtin_type_int },
105 { "acr4", &builtin_type_int },
106 { "acr5", &builtin_type_int },
107 { "acr6", &builtin_type_int },
108 { "acr7", &builtin_type_int },
109 { "acr8", &builtin_type_int },
110 { "acr9", &builtin_type_int },
111 { "acr10", &builtin_type_int },
112 { "acr11", &builtin_type_int },
113 { "acr12", &builtin_type_int },
114 { "acr13", &builtin_type_int },
115 { "acr14", &builtin_type_int },
116 { "acr15", &builtin_type_int },
118 /* Floating Point Control Word. */
119 { "fpc", &builtin_type_int },
121 /* Floating Point Registers. */
122 { "f0", &builtin_type_double },
123 { "f1", &builtin_type_double },
124 { "f2", &builtin_type_double },
125 { "f3", &builtin_type_double },
126 { "f4", &builtin_type_double },
127 { "f5", &builtin_type_double },
128 { "f6", &builtin_type_double },
129 { "f7", &builtin_type_double },
130 { "f8", &builtin_type_double },
131 { "f9", &builtin_type_double },
132 { "f10", &builtin_type_double },
133 { "f11", &builtin_type_double },
134 { "f12", &builtin_type_double },
135 { "f13", &builtin_type_double },
136 { "f14", &builtin_type_double },
137 { "f15", &builtin_type_double },
139 /* Pseudo registers. */
140 { "pc", &builtin_type_void_func_ptr },
141 { "cc", &builtin_type_int },
144 /* Return the name of register REGNUM. */
146 s390_register_name (int regnum)
148 gdb_assert (regnum >= 0 && regnum < S390_NUM_TOTAL_REGS);
149 return s390_register_info[regnum].name;
152 /* Return the GDB type object for the "standard" data type of data in
155 s390_register_type (struct gdbarch *gdbarch, int regnum)
157 gdb_assert (regnum >= 0 && regnum < S390_NUM_TOTAL_REGS);
158 return *s390_register_info[regnum].type;
161 /* DWARF Register Mapping. */
163 static int s390_dwarf_regmap[] =
165 /* General Purpose Registers. */
166 S390_R0_REGNUM, S390_R1_REGNUM, S390_R2_REGNUM, S390_R3_REGNUM,
167 S390_R4_REGNUM, S390_R5_REGNUM, S390_R6_REGNUM, S390_R7_REGNUM,
168 S390_R8_REGNUM, S390_R9_REGNUM, S390_R10_REGNUM, S390_R11_REGNUM,
169 S390_R12_REGNUM, S390_R13_REGNUM, S390_R14_REGNUM, S390_R15_REGNUM,
171 /* Floating Point Registers. */
172 S390_F0_REGNUM, S390_F2_REGNUM, S390_F4_REGNUM, S390_F6_REGNUM,
173 S390_F1_REGNUM, S390_F3_REGNUM, S390_F5_REGNUM, S390_F7_REGNUM,
174 S390_F8_REGNUM, S390_F10_REGNUM, S390_F12_REGNUM, S390_F14_REGNUM,
175 S390_F9_REGNUM, S390_F11_REGNUM, S390_F13_REGNUM, S390_F15_REGNUM,
177 /* Control Registers (not mapped). */
178 -1, -1, -1, -1, -1, -1, -1, -1,
179 -1, -1, -1, -1, -1, -1, -1, -1,
181 /* Access Registers. */
182 S390_A0_REGNUM, S390_A1_REGNUM, S390_A2_REGNUM, S390_A3_REGNUM,
183 S390_A4_REGNUM, S390_A5_REGNUM, S390_A6_REGNUM, S390_A7_REGNUM,
184 S390_A8_REGNUM, S390_A9_REGNUM, S390_A10_REGNUM, S390_A11_REGNUM,
185 S390_A12_REGNUM, S390_A13_REGNUM, S390_A14_REGNUM, S390_A15_REGNUM,
187 /* Program Status Word. */
192 /* Convert DWARF register number REG to the appropriate register
193 number used by GDB. */
195 s390_dwarf_reg_to_regnum (int reg)
199 if (reg >= 0 || reg < ARRAY_SIZE (s390_dwarf_regmap))
200 regnum = s390_dwarf_regmap[reg];
203 warning ("Unmapped DWARF Register #%d encountered\n", reg);
208 /* Pseudo registers - PC and condition code. */
211 s390_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
212 int regnum, void *buf)
219 regcache_raw_read_unsigned (regcache, S390_PSWA_REGNUM, &val);
220 store_unsigned_integer (buf, 4, val & 0x7fffffff);
224 regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &val);
225 store_unsigned_integer (buf, 4, (val >> 12) & 3);
229 internal_error (__FILE__, __LINE__, "invalid regnum");
234 s390_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
235 int regnum, const void *buf)
242 val = extract_unsigned_integer (buf, 4);
243 regcache_raw_read_unsigned (regcache, S390_PSWA_REGNUM, &psw);
244 psw = (psw & 0x80000000) | (val & 0x7fffffff);
245 regcache_raw_write_unsigned (regcache, S390_PSWA_REGNUM, psw);
249 val = extract_unsigned_integer (buf, 4);
250 regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &psw);
251 psw = (psw & ~((ULONGEST)3 << 12)) | ((val & 3) << 12);
252 regcache_raw_write_unsigned (regcache, S390_PSWM_REGNUM, psw);
256 internal_error (__FILE__, __LINE__, "invalid regnum");
261 s390x_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
262 int regnum, void *buf)
269 regcache_raw_read (regcache, S390_PSWA_REGNUM, buf);
273 regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &val);
274 store_unsigned_integer (buf, 4, (val >> 44) & 3);
278 internal_error (__FILE__, __LINE__, "invalid regnum");
283 s390x_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
284 int regnum, const void *buf)
291 regcache_raw_write (regcache, S390_PSWA_REGNUM, buf);
295 val = extract_unsigned_integer (buf, 4);
296 regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &psw);
297 psw = (psw & ~((ULONGEST)3 << 44)) | ((val & 3) << 44);
298 regcache_raw_write_unsigned (regcache, S390_PSWM_REGNUM, psw);
302 internal_error (__FILE__, __LINE__, "invalid regnum");
306 /* 'float' values are stored in the upper half of floating-point
307 registers, even though we are otherwise a big-endian platform. */
310 s390_convert_register_p (int regno, struct type *type)
312 return (regno >= S390_F0_REGNUM && regno <= S390_F15_REGNUM)
313 && TYPE_LENGTH (type) < 8;
317 s390_register_to_value (struct frame_info *frame, int regnum,
318 struct type *valtype, void *out)
321 int len = TYPE_LENGTH (valtype);
322 gdb_assert (len < 8);
324 get_frame_register (frame, regnum, in);
325 memcpy (out, in, len);
329 s390_value_to_register (struct frame_info *frame, int regnum,
330 struct type *valtype, const void *in)
333 int len = TYPE_LENGTH (valtype);
334 gdb_assert (len < 8);
337 memcpy (out, in, len);
338 put_frame_register (frame, regnum, out);
341 /* Register groups. */
344 s390_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
345 struct reggroup *group)
347 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
349 /* Registers displayed via 'info regs'. */
350 if (group == general_reggroup)
351 return (regnum >= S390_R0_REGNUM && regnum <= S390_R15_REGNUM)
352 || regnum == S390_PC_REGNUM
353 || regnum == S390_CC_REGNUM;
355 /* Registers displayed via 'info float'. */
356 if (group == float_reggroup)
357 return (regnum >= S390_F0_REGNUM && regnum <= S390_F15_REGNUM)
358 || regnum == S390_FPC_REGNUM;
360 /* Registers that need to be saved/restored in order to
361 push or pop frames. */
362 if (group == save_reggroup || group == restore_reggroup)
363 return regnum != S390_PSWM_REGNUM && regnum != S390_PSWA_REGNUM;
365 return default_register_reggroup_p (gdbarch, regnum, group);
369 /* Core file register sets. */
371 int s390_regmap_gregset[S390_NUM_REGS] =
373 /* Program Status Word. */
375 /* General Purpose Registers. */
376 0x08, 0x0c, 0x10, 0x14,
377 0x18, 0x1c, 0x20, 0x24,
378 0x28, 0x2c, 0x30, 0x34,
379 0x38, 0x3c, 0x40, 0x44,
380 /* Access Registers. */
381 0x48, 0x4c, 0x50, 0x54,
382 0x58, 0x5c, 0x60, 0x64,
383 0x68, 0x6c, 0x70, 0x74,
384 0x78, 0x7c, 0x80, 0x84,
385 /* Floating Point Control Word. */
387 /* Floating Point Registers. */
388 -1, -1, -1, -1, -1, -1, -1, -1,
389 -1, -1, -1, -1, -1, -1, -1, -1,
392 int s390x_regmap_gregset[S390_NUM_REGS] =
395 /* General Purpose Registers. */
396 0x10, 0x18, 0x20, 0x28,
397 0x30, 0x38, 0x40, 0x48,
398 0x50, 0x58, 0x60, 0x68,
399 0x70, 0x78, 0x80, 0x88,
400 /* Access Registers. */
401 0x90, 0x94, 0x98, 0x9c,
402 0xa0, 0xa4, 0xa8, 0xac,
403 0xb0, 0xb4, 0xb8, 0xbc,
404 0xc0, 0xc4, 0xc8, 0xcc,
405 /* Floating Point Control Word. */
407 /* Floating Point Registers. */
408 -1, -1, -1, -1, -1, -1, -1, -1,
409 -1, -1, -1, -1, -1, -1, -1, -1,
412 int s390_regmap_fpregset[S390_NUM_REGS] =
414 /* Program Status Word. */
416 /* General Purpose Registers. */
417 -1, -1, -1, -1, -1, -1, -1, -1,
418 -1, -1, -1, -1, -1, -1, -1, -1,
419 /* Access Registers. */
420 -1, -1, -1, -1, -1, -1, -1, -1,
421 -1, -1, -1, -1, -1, -1, -1, -1,
422 /* Floating Point Control Word. */
424 /* Floating Point Registers. */
425 0x08, 0x10, 0x18, 0x20,
426 0x28, 0x30, 0x38, 0x40,
427 0x48, 0x50, 0x58, 0x60,
428 0x68, 0x70, 0x78, 0x80,
431 /* Supply register REGNUM from the register set REGSET to register cache
432 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
434 s390_supply_regset (const struct regset *regset, struct regcache *regcache,
435 int regnum, const void *regs, size_t len)
437 const int *offset = regset->descr;
440 for (i = 0; i < S390_NUM_REGS; i++)
442 if ((regnum == i || regnum == -1) && offset[i] != -1)
443 regcache_raw_supply (regcache, i, (const char *)regs + offset[i]);
447 static const struct regset s390_gregset = {
452 static const struct regset s390x_gregset = {
453 s390x_regmap_gregset,
457 static const struct regset s390_fpregset = {
458 s390_regmap_fpregset,
462 /* Return the appropriate register set for the core section identified
463 by SECT_NAME and SECT_SIZE. */
464 const struct regset *
465 s390_regset_from_core_section (struct gdbarch *gdbarch,
466 const char *sect_name, size_t sect_size)
468 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
470 if (strcmp (sect_name, ".reg") == 0 && sect_size == tdep->sizeof_gregset)
471 return tdep->gregset;
473 if (strcmp (sect_name, ".reg2") == 0 && sect_size == tdep->sizeof_fpregset)
474 return tdep->fpregset;
480 /* Prologue analysis. */
482 /* When we analyze a prologue, we're really doing 'abstract
483 interpretation' or 'pseudo-evaluation': running the function's code
484 in simulation, but using conservative approximations of the values
485 it would have when it actually runs. For example, if our function
486 starts with the instruction:
488 ahi r1, 42 # add halfword immediate 42 to r1
490 we don't know exactly what value will be in r1 after executing this
491 instruction, but we do know it'll be 42 greater than its original
494 If we then see an instruction like:
496 ahi r1, 22 # add halfword immediate 22 to r1
498 we still don't know what r1's value is, but again, we can say it is
499 now 64 greater than its original value.
501 If the next instruction were:
503 lr r2, r1 # set r2 to r1's value
505 then we can say that r2's value is now the original value of r1
508 Of course, this can only go so far before it gets unreasonable. If
509 we wanted to be able to say anything about the value of r1 after
512 xr r1, r3 # exclusive-or r1 and r3, place result in r1
514 then things would get pretty complex. But remember, we're just
515 doing a conservative approximation; if exclusive-or instructions
516 aren't relevant to prologues, we can just say r1's value is now
517 'unknown'. We can ignore things that are too complex, if that loss
518 of information is acceptable for our application.
520 Once you've reached an instruction that you don't know how to
521 simulate, you stop. Now you examine the state of the registers and
522 stack slots you've kept track of. For example:
524 - To see how large your stack frame is, just check the value of sp;
525 if it's the original value of sp minus a constant, then that
526 constant is the stack frame's size. If the sp's value has been
527 marked as 'unknown', then that means the prologue has done
528 something too complex for us to track, and we don't know the
531 - To see whether we've saved the SP in the current frame's back
532 chain slot, we just check whether the current value of the back
533 chain stack slot is the original value of the sp.
535 Sure, this takes some work. But prologue analyzers aren't
536 quick-and-simple pattern patching to recognize a few fixed prologue
537 forms any more; they're big, hairy functions. Along with inferior
538 function calls, prologue analysis accounts for a substantial
539 portion of the time needed to stabilize a GDB port. So I think
540 it's worthwhile to look for an approach that will be easier to
541 understand and maintain. In the approach used here:
543 - It's easier to see that the analyzer is correct: you just see
544 whether the analyzer properly (albiet conservatively) simulates
545 the effect of each instruction.
547 - It's easier to extend the analyzer: you can add support for new
548 instructions, and know that you haven't broken anything that
549 wasn't already broken before.
551 - It's orthogonal: to gather new information, you don't need to
552 complicate the code for each instruction. As long as your domain
553 of conservative values is already detailed enough to tell you
554 what you need, then all the existing instruction simulations are
555 already gathering the right data for you.
557 A 'struct prologue_value' is a conservative approximation of the
558 real value the register or stack slot will have. */
560 struct prologue_value {
562 /* What sort of value is this? This determines the interpretation
563 of subsequent fields. */
566 /* We don't know anything about the value. This is also used for
567 values we could have kept track of, when doing so would have
568 been too complex and we don't want to bother. The bottom of
572 /* A known constant. K is its value. */
575 /* The value that register REG originally had *UPON ENTRY TO THE
576 FUNCTION*, plus K. If K is zero, this means, obviously, just
577 the value REG had upon entry to the function. REG is a GDB
578 register number. Before we start interpreting, we initialize
579 every register R to { pv_register, R, 0 }. */
584 /* The meanings of the following fields depend on 'kind'; see the
585 comments for the specific 'kind' values. */
591 /* Set V to be unknown. */
593 pv_set_to_unknown (struct prologue_value *v)
595 v->kind = pv_unknown;
599 /* Set V to the constant K. */
601 pv_set_to_constant (struct prologue_value *v, CORE_ADDR k)
603 v->kind = pv_constant;
608 /* Set V to the original value of register REG, plus K. */
610 pv_set_to_register (struct prologue_value *v, int reg, CORE_ADDR k)
612 v->kind = pv_register;
618 /* If one of *A and *B is a constant, and the other isn't, swap the
619 pointers as necessary to ensure that *B points to the constant.
620 This can reduce the number of cases we need to analyze in the
623 pv_constant_last (struct prologue_value **a,
624 struct prologue_value **b)
626 if ((*a)->kind == pv_constant
627 && (*b)->kind != pv_constant)
629 struct prologue_value *temp = *a;
636 /* Set SUM to the sum of A and B. SUM, A, and B may point to the same
637 'struct prologue_value' object. */
639 pv_add (struct prologue_value *sum,
640 struct prologue_value *a,
641 struct prologue_value *b)
643 pv_constant_last (&a, &b);
645 /* We can handle adding constants to registers, and other constants. */
646 if (b->kind == pv_constant
647 && (a->kind == pv_register
648 || a->kind == pv_constant))
651 sum->reg = a->reg; /* not meaningful if a is pv_constant, but
653 sum->k = a->k + b->k;
656 /* Anything else we don't know how to add. We don't have a
657 representation for, say, the sum of two registers, or a multiple
658 of a register's value (adding a register to itself). */
660 sum->kind = pv_unknown;
664 /* Add the constant K to V. */
666 pv_add_constant (struct prologue_value *v, CORE_ADDR k)
668 struct prologue_value pv_k;
670 /* Rather than thinking of all the cases we can and can't handle,
671 we'll just let pv_add take care of that for us. */
672 pv_set_to_constant (&pv_k, k);
673 pv_add (v, v, &pv_k);
677 /* Subtract B from A, and put the result in DIFF.
679 This isn't quite the same as negating B and adding it to A, since
680 we don't have a representation for the negation of anything but a
681 constant. For example, we can't negate { pv_register, R1, 10 },
682 but we do know that { pv_register, R1, 10 } minus { pv_register,
683 R1, 5 } is { pv_constant, <ignored>, 5 }.
685 This means, for example, that we can subtract two stack addresses;
686 they're both relative to the original SP. Since the frame pointer
687 is set based on the SP, its value will be the original SP plus some
688 constant (probably zero), so we can use its value just fine. */
690 pv_subtract (struct prologue_value *diff,
691 struct prologue_value *a,
692 struct prologue_value *b)
694 pv_constant_last (&a, &b);
696 /* We can subtract a constant from another constant, or from a
698 if (b->kind == pv_constant
699 && (a->kind == pv_register
700 || a->kind == pv_constant))
702 diff->kind = a->kind;
703 diff->reg = a->reg; /* not always meaningful, but harmless */
704 diff->k = a->k - b->k;
707 /* We can subtract a register from itself, yielding a constant. */
708 else if (a->kind == pv_register
709 && b->kind == pv_register
712 diff->kind = pv_constant;
713 diff->k = a->k - b->k;
716 /* We don't know how to subtract anything else. */
718 diff->kind = pv_unknown;
722 /* Set AND to the logical and of A and B. */
724 pv_logical_and (struct prologue_value *and,
725 struct prologue_value *a,
726 struct prologue_value *b)
728 pv_constant_last (&a, &b);
730 /* We can 'and' two constants. */
731 if (a->kind == pv_constant
732 && b->kind == pv_constant)
734 and->kind = pv_constant;
735 and->k = a->k & b->k;
738 /* We can 'and' anything with the constant zero. */
739 else if (b->kind == pv_constant
742 and->kind = pv_constant;
746 /* We can 'and' anything with ~0. */
747 else if (b->kind == pv_constant
748 && b->k == ~ (CORE_ADDR) 0)
751 /* We can 'and' a register with itself. */
752 else if (a->kind == pv_register
753 && b->kind == pv_register
758 /* Otherwise, we don't know. */
760 pv_set_to_unknown (and);
764 /* Return non-zero iff A and B are identical expressions.
766 This is not the same as asking if the two values are equal; the
767 result of such a comparison would have to be a pv_boolean, and
768 asking whether two 'unknown' values were equal would give you
769 pv_maybe. Same for comparing, say, { pv_register, R1, 0 } and {
770 pv_register, R2, 0}. Instead, this is asking whether the two
771 representations are the same. */
773 pv_is_identical (struct prologue_value *a,
774 struct prologue_value *b)
776 if (a->kind != b->kind)
784 return (a->k == b->k);
786 return (a->reg == b->reg && a->k == b->k);
793 /* Return non-zero if A is the original value of register number R
794 plus K, zero otherwise. */
796 pv_is_register (struct prologue_value *a, int r, CORE_ADDR k)
798 return (a->kind == pv_register
804 /* Decoding S/390 instructions. */
806 /* Named opcode values for the S/390 instructions we recognize. Some
807 instructions have their opcode split across two fields; those are the
808 op1_* and op2_* enums. */
811 op1_lhi = 0xa7, op2_lhi = 0x08,
812 op1_lghi = 0xa7, op2_lghi = 0x09,
816 op1_ly = 0xe3, op2_ly = 0x58,
817 op1_lg = 0xe3, op2_lg = 0x04,
819 op1_lmy = 0xeb, op2_lmy = 0x98,
820 op1_lmg = 0xeb, op2_lmg = 0x04,
822 op1_sty = 0xe3, op2_sty = 0x50,
823 op1_stg = 0xe3, op2_stg = 0x24,
826 op1_stmy = 0xeb, op2_stmy = 0x90,
827 op1_stmg = 0xeb, op2_stmg = 0x24,
828 op1_aghi = 0xa7, op2_aghi = 0x0b,
829 op1_ahi = 0xa7, op2_ahi = 0x0a,
833 op1_ay = 0xe3, op2_ay = 0x5a,
834 op1_ag = 0xe3, op2_ag = 0x08,
838 op1_sy = 0xe3, op2_sy = 0x5b,
839 op1_sg = 0xe3, op2_sg = 0x09,
843 op1_lay = 0xe3, op2_lay = 0x71,
844 op1_larl = 0xc0, op2_larl = 0x00,
849 op1_bras = 0xa7, op2_bras = 0x05,
850 op1_brasl= 0xc0, op2_brasl= 0x05,
851 op1_brc = 0xa7, op2_brc = 0x04,
852 op1_brcl = 0xc0, op2_brcl = 0x04,
856 /* Read a single instruction from address AT. */
858 #define S390_MAX_INSTR_SIZE 6
860 s390_readinstruction (bfd_byte instr[], CORE_ADDR at)
862 static int s390_instrlen[] = { 2, 4, 4, 6 };
865 if (deprecated_read_memory_nobpt (at, &instr[0], 2))
867 instrlen = s390_instrlen[instr[0] >> 6];
870 if (deprecated_read_memory_nobpt (at + 2, &instr[2], instrlen - 2))
877 /* The functions below are for recognizing and decoding S/390
878 instructions of various formats. Each of them checks whether INSN
879 is an instruction of the given format, with the specified opcodes.
880 If it is, it sets the remaining arguments to the values of the
881 instruction's fields, and returns a non-zero value; otherwise, it
884 These functions' arguments appear in the order they appear in the
885 instruction, not in the machine-language form. So, opcodes always
886 come first, even though they're sometimes scattered around the
887 instructions. And displacements appear before base and extension
888 registers, as they do in the assembly syntax, not at the end, as
889 they do in the machine language. */
891 is_ri (bfd_byte *insn, int op1, int op2, unsigned int *r1, int *i2)
893 if (insn[0] == op1 && (insn[1] & 0xf) == op2)
895 *r1 = (insn[1] >> 4) & 0xf;
896 /* i2 is a 16-bit signed quantity. */
897 *i2 = (((insn[2] << 8) | insn[3]) ^ 0x8000) - 0x8000;
906 is_ril (bfd_byte *insn, int op1, int op2,
907 unsigned int *r1, int *i2)
909 if (insn[0] == op1 && (insn[1] & 0xf) == op2)
911 *r1 = (insn[1] >> 4) & 0xf;
912 /* i2 is a signed quantity. If the host 'int' is 32 bits long,
913 no sign extension is necessary, but we don't want to assume
915 *i2 = (((insn[2] << 24)
918 | (insn[5])) ^ 0x80000000) - 0x80000000;
927 is_rr (bfd_byte *insn, int op, unsigned int *r1, unsigned int *r2)
931 *r1 = (insn[1] >> 4) & 0xf;
941 is_rre (bfd_byte *insn, int op, unsigned int *r1, unsigned int *r2)
943 if (((insn[0] << 8) | insn[1]) == op)
945 /* Yes, insn[3]. insn[2] is unused in RRE format. */
946 *r1 = (insn[3] >> 4) & 0xf;
956 is_rs (bfd_byte *insn, int op,
957 unsigned int *r1, unsigned int *r3, unsigned int *d2, unsigned int *b2)
961 *r1 = (insn[1] >> 4) & 0xf;
963 *b2 = (insn[2] >> 4) & 0xf;
964 *d2 = ((insn[2] & 0xf) << 8) | insn[3];
973 is_rsy (bfd_byte *insn, int op1, int op2,
974 unsigned int *r1, unsigned int *r3, unsigned int *d2, unsigned int *b2)
979 *r1 = (insn[1] >> 4) & 0xf;
981 *b2 = (insn[2] >> 4) & 0xf;
982 /* The 'long displacement' is a 20-bit signed integer. */
983 *d2 = ((((insn[2] & 0xf) << 8) | insn[3] | (insn[4] << 12))
984 ^ 0x80000) - 0x80000;
993 is_rx (bfd_byte *insn, int op,
994 unsigned int *r1, unsigned int *d2, unsigned int *x2, unsigned int *b2)
998 *r1 = (insn[1] >> 4) & 0xf;
1000 *b2 = (insn[2] >> 4) & 0xf;
1001 *d2 = ((insn[2] & 0xf) << 8) | insn[3];
1010 is_rxy (bfd_byte *insn, int op1, int op2,
1011 unsigned int *r1, unsigned int *d2, unsigned int *x2, unsigned int *b2)
1016 *r1 = (insn[1] >> 4) & 0xf;
1017 *x2 = insn[1] & 0xf;
1018 *b2 = (insn[2] >> 4) & 0xf;
1019 /* The 'long displacement' is a 20-bit signed integer. */
1020 *d2 = ((((insn[2] & 0xf) << 8) | insn[3] | (insn[4] << 12))
1021 ^ 0x80000) - 0x80000;
1029 /* Set ADDR to the effective address for an X-style instruction, like:
1033 Here, X2 and B2 are registers, and D2 is a signed 20-bit
1034 constant; the effective address is the sum of all three. If either
1035 X2 or B2 are zero, then it doesn't contribute to the sum --- this
1036 means that r0 can't be used as either X2 or B2.
1038 GPR is an array of general register values, indexed by GPR number,
1039 not GDB register number. */
1041 compute_x_addr (struct prologue_value *addr,
1042 struct prologue_value *gpr,
1043 int d2, unsigned int x2, unsigned int b2)
1045 /* We can't just add stuff directly in addr; it might alias some of
1046 the registers we need to read. */
1047 struct prologue_value result;
1049 pv_set_to_constant (&result, d2);
1051 pv_add (&result, &result, &gpr[x2]);
1053 pv_add (&result, &result, &gpr[b2]);
1059 #define S390_NUM_GPRS 16
1060 #define S390_NUM_FPRS 16
1062 struct s390_prologue_data {
1064 /* The size of a GPR or FPR. */
1068 /* The general-purpose registers. */
1069 struct prologue_value gpr[S390_NUM_GPRS];
1071 /* The floating-point registers. */
1072 struct prologue_value fpr[S390_NUM_FPRS];
1074 /* The offset relative to the CFA where the incoming GPR N was saved
1075 by the function prologue. 0 if not saved or unknown. */
1076 int gpr_slot[S390_NUM_GPRS];
1078 /* Likewise for FPRs. */
1079 int fpr_slot[S390_NUM_FPRS];
1081 /* Nonzero if the backchain was saved. This is assumed to be the
1082 case when the incoming SP is saved at the current SP location. */
1083 int back_chain_saved_p;
1086 /* Do a SIZE-byte store of VALUE to ADDR. */
1088 s390_store (struct prologue_value *addr,
1090 struct prologue_value *value,
1091 struct s390_prologue_data *data)
1093 struct prologue_value cfa, offset;
1096 /* Check whether we are storing the backchain. */
1097 pv_subtract (&offset, &data->gpr[S390_SP_REGNUM - S390_R0_REGNUM], addr);
1099 if (offset.kind == pv_constant && offset.k == 0)
1100 if (size == data->gpr_size
1101 && pv_is_register (value, S390_SP_REGNUM, 0))
1103 data->back_chain_saved_p = 1;
1108 /* Check whether we are storing a register into the stack. */
1109 pv_set_to_register (&cfa, S390_SP_REGNUM, 16 * data->gpr_size + 32);
1110 pv_subtract (&offset, &cfa, addr);
1112 if (offset.kind == pv_constant
1113 && offset.k < INT_MAX && offset.k > 0
1114 && offset.k % data->gpr_size == 0)
1116 /* If we are storing the original value of a register, we want to
1117 record the CFA offset. If the same register is stored multiple
1118 times, the stack slot with the highest address counts. */
1120 for (i = 0; i < S390_NUM_GPRS; i++)
1121 if (size == data->gpr_size
1122 && pv_is_register (value, S390_R0_REGNUM + i, 0))
1123 if (data->gpr_slot[i] == 0
1124 || data->gpr_slot[i] > offset.k)
1126 data->gpr_slot[i] = offset.k;
1130 for (i = 0; i < S390_NUM_FPRS; i++)
1131 if (size == data->fpr_size
1132 && pv_is_register (value, S390_F0_REGNUM + i, 0))
1133 if (data->fpr_slot[i] == 0
1134 || data->fpr_slot[i] > offset.k)
1136 data->fpr_slot[i] = offset.k;
1142 /* Note: If this is some store we cannot identify, you might think we
1143 should forget our cached values, as any of those might have been hit.
1145 However, we make the assumption that the register save areas are only
1146 ever stored to once in any given function, and we do recognize these
1147 stores. Thus every store we cannot recognize does not hit our data. */
1150 /* Do a SIZE-byte load from ADDR into VALUE. */
1152 s390_load (struct prologue_value *addr,
1154 struct prologue_value *value,
1155 struct s390_prologue_data *data)
1157 struct prologue_value cfa, offset;
1160 /* If it's a load from an in-line constant pool, then we can
1161 simulate that, under the assumption that the code isn't
1162 going to change between the time the processor actually
1163 executed it creating the current frame, and the time when
1164 we're analyzing the code to unwind past that frame. */
1165 if (addr->kind == pv_constant)
1167 struct section_table *secp;
1168 secp = target_section_by_addr (¤t_target, addr->k);
1170 && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1173 pv_set_to_constant (value, read_memory_integer (addr->k, size));
1178 /* Check whether we are accessing one of our save slots. */
1179 pv_set_to_register (&cfa, S390_SP_REGNUM, 16 * data->gpr_size + 32);
1180 pv_subtract (&offset, &cfa, addr);
1182 if (offset.kind == pv_constant
1183 && offset.k < INT_MAX && offset.k > 0)
1185 for (i = 0; i < S390_NUM_GPRS; i++)
1186 if (offset.k == data->gpr_slot[i])
1188 pv_set_to_register (value, S390_R0_REGNUM + i, 0);
1192 for (i = 0; i < S390_NUM_FPRS; i++)
1193 if (offset.k == data->fpr_slot[i])
1195 pv_set_to_register (value, S390_F0_REGNUM + i, 0);
1200 /* Otherwise, we don't know the value. */
1201 pv_set_to_unknown (value);
1205 /* Analyze the prologue of the function starting at START_PC,
1206 continuing at most until CURRENT_PC. Initialize DATA to
1207 hold all information we find out about the state of the registers
1208 and stack slots. Return the address of the instruction after
1209 the last one that changed the SP, FP, or back chain; or zero
1212 s390_analyze_prologue (struct gdbarch *gdbarch,
1214 CORE_ADDR current_pc,
1215 struct s390_prologue_data *data)
1217 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1219 /* Our return value:
1220 The address of the instruction after the last one that changed
1221 the SP, FP, or back chain; zero if we got an error trying to
1223 CORE_ADDR result = start_pc;
1225 /* The current PC for our abstract interpretation. */
1228 /* The address of the next instruction after that. */
1231 /* Set up everything's initial value. */
1235 /* For the purpose of prologue tracking, we consider the GPR size to
1236 be equal to the ABI word size, even if it is actually larger
1237 (i.e. when running a 32-bit binary under a 64-bit kernel). */
1238 data->gpr_size = word_size;
1241 for (i = 0; i < S390_NUM_GPRS; i++)
1242 pv_set_to_register (&data->gpr[i], S390_R0_REGNUM + i, 0);
1244 for (i = 0; i < S390_NUM_FPRS; i++)
1245 pv_set_to_register (&data->fpr[i], S390_F0_REGNUM + i, 0);
1247 for (i = 0; i < S390_NUM_GPRS; i++)
1248 data->gpr_slot[i] = 0;
1250 for (i = 0; i < S390_NUM_FPRS; i++)
1251 data->fpr_slot[i] = 0;
1253 data->back_chain_saved_p = 0;
1256 /* Start interpreting instructions, until we hit the frame's
1257 current PC or the first branch instruction. */
1258 for (pc = start_pc; pc > 0 && pc < current_pc; pc = next_pc)
1260 bfd_byte insn[S390_MAX_INSTR_SIZE];
1261 int insn_len = s390_readinstruction (insn, pc);
1263 /* Fields for various kinds of instructions. */
1264 unsigned int b2, r1, r2, x2, r3;
1267 /* The values of SP and FP before this instruction,
1268 for detecting instructions that change them. */
1269 struct prologue_value pre_insn_sp, pre_insn_fp;
1270 /* Likewise for the flag whether the back chain was saved. */
1271 int pre_insn_back_chain_saved_p;
1273 /* If we got an error trying to read the instruction, report it. */
1280 next_pc = pc + insn_len;
1282 pre_insn_sp = data->gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1283 pre_insn_fp = data->gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
1284 pre_insn_back_chain_saved_p = data->back_chain_saved_p;
1286 /* LHI r1, i2 --- load halfword immediate */
1288 && is_ri (insn, op1_lhi, op2_lhi, &r1, &i2))
1289 pv_set_to_constant (&data->gpr[r1], i2);
1291 /* LGHI r1, i2 --- load halfword immediate (64-bit version) */
1292 else if (word_size == 8
1293 && is_ri (insn, op1_lghi, op2_lghi, &r1, &i2))
1294 pv_set_to_constant (&data->gpr[r1], i2);
1296 /* LR r1, r2 --- load from register */
1297 else if (word_size == 4
1298 && is_rr (insn, op_lr, &r1, &r2))
1299 data->gpr[r1] = data->gpr[r2];
1301 /* LGR r1, r2 --- load from register (64-bit version) */
1302 else if (word_size == 8
1303 && is_rre (insn, op_lgr, &r1, &r2))
1304 data->gpr[r1] = data->gpr[r2];
1306 /* L r1, d2(x2, b2) --- load */
1307 else if (word_size == 4
1308 && is_rx (insn, op_l, &r1, &d2, &x2, &b2))
1310 struct prologue_value addr;
1312 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1313 s390_load (&addr, 4, &data->gpr[r1], data);
1316 /* LY r1, d2(x2, b2) --- load (long-displacement version) */
1317 else if (word_size == 4
1318 && is_rxy (insn, op1_ly, op2_ly, &r1, &d2, &x2, &b2))
1320 struct prologue_value addr;
1322 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1323 s390_load (&addr, 4, &data->gpr[r1], data);
1326 /* LG r1, d2(x2, b2) --- load (64-bit version) */
1327 else if (word_size == 8
1328 && is_rxy (insn, op1_lg, op2_lg, &r1, &d2, &x2, &b2))
1330 struct prologue_value addr;
1332 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1333 s390_load (&addr, 8, &data->gpr[r1], data);
1336 /* ST r1, d2(x2, b2) --- store */
1337 else if (word_size == 4
1338 && is_rx (insn, op_st, &r1, &d2, &x2, &b2))
1340 struct prologue_value addr;
1342 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1343 s390_store (&addr, 4, &data->gpr[r1], data);
1346 /* STY r1, d2(x2, b2) --- store (long-displacement version) */
1347 else if (word_size == 4
1348 && is_rxy (insn, op1_sty, op2_sty, &r1, &d2, &x2, &b2))
1350 struct prologue_value addr;
1352 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1353 s390_store (&addr, 4, &data->gpr[r1], data);
1356 /* STG r1, d2(x2, b2) --- store (64-bit version) */
1357 else if (word_size == 8
1358 && is_rxy (insn, op1_stg, op2_stg, &r1, &d2, &x2, &b2))
1360 struct prologue_value addr;
1362 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1363 s390_store (&addr, 8, &data->gpr[r1], data);
1366 /* STD r1, d2(x2,b2) --- store floating-point register */
1367 else if (is_rx (insn, op_std, &r1, &d2, &x2, &b2))
1369 struct prologue_value addr;
1371 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1372 s390_store (&addr, 8, &data->fpr[r1], data);
1375 /* STM r1, r3, d2(b2) --- store multiple */
1376 else if (word_size == 4
1377 && is_rs (insn, op_stm, &r1, &r3, &d2, &b2))
1381 struct prologue_value addr;
1383 for (regnum = r1, offset = 0;
1385 regnum++, offset += 4)
1387 compute_x_addr (&addr, data->gpr, d2 + offset, 0, b2);
1388 s390_store (&addr, 4, &data->gpr[regnum], data);
1392 /* STMY r1, r3, d2(b2) --- store multiple (long-displacement version) */
1393 else if (word_size == 4
1394 && is_rsy (insn, op1_stmy, op2_stmy, &r1, &r3, &d2, &b2))
1398 struct prologue_value addr;
1400 for (regnum = r1, offset = 0;
1402 regnum++, offset += 4)
1404 compute_x_addr (&addr, data->gpr, d2 + offset, 0, b2);
1405 s390_store (&addr, 4, &data->gpr[regnum], data);
1409 /* STMG r1, r3, d2(b2) --- store multiple (64-bit version) */
1410 else if (word_size == 8
1411 && is_rsy (insn, op1_stmg, op2_stmg, &r1, &r3, &d2, &b2))
1415 struct prologue_value addr;
1417 for (regnum = r1, offset = 0;
1419 regnum++, offset += 8)
1421 compute_x_addr (&addr, data->gpr, d2 + offset, 0, b2);
1422 s390_store (&addr, 8, &data->gpr[regnum], data);
1426 /* AHI r1, i2 --- add halfword immediate */
1427 else if (word_size == 4
1428 && is_ri (insn, op1_ahi, op2_ahi, &r1, &i2))
1429 pv_add_constant (&data->gpr[r1], i2);
1431 /* AGHI r1, i2 --- add halfword immediate (64-bit version) */
1432 else if (word_size == 8
1433 && is_ri (insn, op1_aghi, op2_aghi, &r1, &i2))
1434 pv_add_constant (&data->gpr[r1], i2);
1436 /* AR r1, r2 -- add register */
1437 else if (word_size == 4
1438 && is_rr (insn, op_ar, &r1, &r2))
1439 pv_add (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1441 /* AGR r1, r2 -- add register (64-bit version) */
1442 else if (word_size == 8
1443 && is_rre (insn, op_agr, &r1, &r2))
1444 pv_add (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1446 /* A r1, d2(x2, b2) -- add */
1447 else if (word_size == 4
1448 && is_rx (insn, op_a, &r1, &d2, &x2, &b2))
1450 struct prologue_value addr;
1451 struct prologue_value value;
1453 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1454 s390_load (&addr, 4, &value, data);
1456 pv_add (&data->gpr[r1], &data->gpr[r1], &value);
1459 /* AY r1, d2(x2, b2) -- add (long-displacement version) */
1460 else if (word_size == 4
1461 && is_rxy (insn, op1_ay, op2_ay, &r1, &d2, &x2, &b2))
1463 struct prologue_value addr;
1464 struct prologue_value value;
1466 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1467 s390_load (&addr, 4, &value, data);
1469 pv_add (&data->gpr[r1], &data->gpr[r1], &value);
1472 /* AG r1, d2(x2, b2) -- add (64-bit version) */
1473 else if (word_size == 8
1474 && is_rxy (insn, op1_ag, op2_ag, &r1, &d2, &x2, &b2))
1476 struct prologue_value addr;
1477 struct prologue_value value;
1479 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1480 s390_load (&addr, 8, &value, data);
1482 pv_add (&data->gpr[r1], &data->gpr[r1], &value);
1485 /* SR r1, r2 -- subtract register */
1486 else if (word_size == 4
1487 && is_rr (insn, op_sr, &r1, &r2))
1488 pv_subtract (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1490 /* SGR r1, r2 -- subtract register (64-bit version) */
1491 else if (word_size == 8
1492 && is_rre (insn, op_sgr, &r1, &r2))
1493 pv_subtract (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1495 /* S r1, d2(x2, b2) -- subtract */
1496 else if (word_size == 4
1497 && is_rx (insn, op_s, &r1, &d2, &x2, &b2))
1499 struct prologue_value addr;
1500 struct prologue_value value;
1502 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1503 s390_load (&addr, 4, &value, data);
1505 pv_subtract (&data->gpr[r1], &data->gpr[r1], &value);
1508 /* SY r1, d2(x2, b2) -- subtract (long-displacement version) */
1509 else if (word_size == 4
1510 && is_rxy (insn, op1_sy, op2_sy, &r1, &d2, &x2, &b2))
1512 struct prologue_value addr;
1513 struct prologue_value value;
1515 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1516 s390_load (&addr, 4, &value, data);
1518 pv_subtract (&data->gpr[r1], &data->gpr[r1], &value);
1521 /* SG r1, d2(x2, b2) -- subtract (64-bit version) */
1522 else if (word_size == 8
1523 && is_rxy (insn, op1_sg, op2_sg, &r1, &d2, &x2, &b2))
1525 struct prologue_value addr;
1526 struct prologue_value value;
1528 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1529 s390_load (&addr, 8, &value, data);
1531 pv_subtract (&data->gpr[r1], &data->gpr[r1], &value);
1534 /* NR r1, r2 --- logical and */
1535 else if (word_size == 4
1536 && is_rr (insn, op_nr, &r1, &r2))
1537 pv_logical_and (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1539 /* NGR r1, r2 >--- logical and (64-bit version) */
1540 else if (word_size == 8
1541 && is_rre (insn, op_ngr, &r1, &r2))
1542 pv_logical_and (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1544 /* LA r1, d2(x2, b2) --- load address */
1545 else if (is_rx (insn, op_la, &r1, &d2, &x2, &b2))
1546 compute_x_addr (&data->gpr[r1], data->gpr, d2, x2, b2);
1548 /* LAY r1, d2(x2, b2) --- load address (long-displacement version) */
1549 else if (is_rxy (insn, op1_lay, op2_lay, &r1, &d2, &x2, &b2))
1550 compute_x_addr (&data->gpr[r1], data->gpr, d2, x2, b2);
1552 /* LARL r1, i2 --- load address relative long */
1553 else if (is_ril (insn, op1_larl, op2_larl, &r1, &i2))
1554 pv_set_to_constant (&data->gpr[r1], pc + i2 * 2);
1556 /* BASR r1, 0 --- branch and save
1557 Since r2 is zero, this saves the PC in r1, but doesn't branch. */
1558 else if (is_rr (insn, op_basr, &r1, &r2)
1560 pv_set_to_constant (&data->gpr[r1], next_pc);
1562 /* BRAS r1, i2 --- branch relative and save */
1563 else if (is_ri (insn, op1_bras, op2_bras, &r1, &i2))
1565 pv_set_to_constant (&data->gpr[r1], next_pc);
1566 next_pc = pc + i2 * 2;
1568 /* We'd better not interpret any backward branches. We'll
1574 /* Terminate search when hitting any other branch instruction. */
1575 else if (is_rr (insn, op_basr, &r1, &r2)
1576 || is_rx (insn, op_bas, &r1, &d2, &x2, &b2)
1577 || is_rr (insn, op_bcr, &r1, &r2)
1578 || is_rx (insn, op_bc, &r1, &d2, &x2, &b2)
1579 || is_ri (insn, op1_brc, op2_brc, &r1, &i2)
1580 || is_ril (insn, op1_brcl, op2_brcl, &r1, &i2)
1581 || is_ril (insn, op1_brasl, op2_brasl, &r2, &i2))
1585 /* An instruction we don't know how to simulate. The only
1586 safe thing to do would be to set every value we're tracking
1587 to 'unknown'. Instead, we'll be optimistic: we assume that
1588 we *can* interpret every instruction that the compiler uses
1589 to manipulate any of the data we're interested in here --
1590 then we can just ignore anything else. */
1593 /* Record the address after the last instruction that changed
1594 the FP, SP, or backlink. Ignore instructions that changed
1595 them back to their original values --- those are probably
1596 restore instructions. (The back chain is never restored,
1599 struct prologue_value *sp = &data->gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1600 struct prologue_value *fp = &data->gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
1602 if ((! pv_is_identical (&pre_insn_sp, sp)
1603 && ! pv_is_register (sp, S390_SP_REGNUM, 0))
1604 || (! pv_is_identical (&pre_insn_fp, fp)
1605 && ! pv_is_register (fp, S390_FRAME_REGNUM, 0))
1606 || pre_insn_back_chain_saved_p != data->back_chain_saved_p)
1614 /* Advance PC across any function entry prologue instructions to reach
1615 some "real" code. */
1617 s390_skip_prologue (CORE_ADDR pc)
1619 struct s390_prologue_data data;
1621 skip_pc = s390_analyze_prologue (current_gdbarch, pc, (CORE_ADDR)-1, &data);
1622 return skip_pc ? skip_pc : pc;
1625 /* Return true if we are in the functin's epilogue, i.e. after the
1626 instruction that destroyed the function's stack frame. */
1628 s390_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
1630 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1632 /* In frameless functions, there's not frame to destroy and thus
1633 we don't care about the epilogue.
1635 In functions with frame, the epilogue sequence is a pair of
1636 a LM-type instruction that restores (amongst others) the
1637 return register %r14 and the stack pointer %r15, followed
1638 by a branch 'br %r14' --or equivalent-- that effects the
1641 In that situation, this function needs to return 'true' in
1642 exactly one case: when pc points to that branch instruction.
1644 Thus we try to disassemble the one instructions immediately
1645 preceeding pc and check whether it is an LM-type instruction
1646 modifying the stack pointer.
1648 Note that disassembling backwards is not reliable, so there
1649 is a slight chance of false positives here ... */
1652 unsigned int r1, r3, b2;
1656 && !deprecated_read_memory_nobpt (pc - 4, insn, 4)
1657 && is_rs (insn, op_lm, &r1, &r3, &d2, &b2)
1658 && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
1662 && !deprecated_read_memory_nobpt (pc - 6, insn, 6)
1663 && is_rsy (insn, op1_lmy, op2_lmy, &r1, &r3, &d2, &b2)
1664 && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
1668 && !deprecated_read_memory_nobpt (pc - 6, insn, 6)
1669 && is_rsy (insn, op1_lmg, op2_lmg, &r1, &r3, &d2, &b2)
1670 && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
1677 /* Normal stack frames. */
1679 struct s390_unwind_cache {
1682 CORE_ADDR frame_base;
1683 CORE_ADDR local_base;
1685 struct trad_frame_saved_reg *saved_regs;
1689 s390_prologue_frame_unwind_cache (struct frame_info *next_frame,
1690 struct s390_unwind_cache *info)
1692 struct gdbarch *gdbarch = get_frame_arch (next_frame);
1693 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1694 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1695 struct s390_prologue_data data;
1696 struct prologue_value *fp = &data.gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
1697 struct prologue_value *sp = &data.gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1707 /* Try to find the function start address. If we can't find it, we don't
1708 bother searching for it -- with modern compilers this would be mostly
1709 pointless anyway. Trust that we'll either have valid DWARF-2 CFI data
1710 or else a valid backchain ... */
1711 func = frame_func_unwind (next_frame);
1715 /* Try to analyze the prologue. */
1716 result = s390_analyze_prologue (gdbarch, func,
1717 frame_pc_unwind (next_frame), &data);
1721 /* If this was successful, we should have found the instruction that
1722 sets the stack pointer register to the previous value of the stack
1723 pointer minus the frame size. */
1724 if (sp->kind != pv_register || sp->reg != S390_SP_REGNUM)
1727 /* A frame size of zero at this point can mean either a real
1728 frameless function, or else a failure to find the prologue.
1729 Perform some sanity checks to verify we really have a
1730 frameless function. */
1733 /* If the next frame is a NORMAL_FRAME, this frame *cannot* have frame
1734 size zero. This is only possible if the next frame is a sentinel
1735 frame, a dummy frame, or a signal trampoline frame. */
1736 /* FIXME: cagney/2004-05-01: This sanity check shouldn't be
1737 needed, instead the code should simpliy rely on its
1739 if (get_frame_type (next_frame) == NORMAL_FRAME)
1742 /* If we really have a frameless function, %r14 must be valid
1743 -- in particular, it must point to a different function. */
1744 reg = frame_unwind_register_unsigned (next_frame, S390_RETADDR_REGNUM);
1745 reg = gdbarch_addr_bits_remove (gdbarch, reg) - 1;
1746 if (get_pc_function_start (reg) == func)
1748 /* However, there is one case where it *is* valid for %r14
1749 to point to the same function -- if this is a recursive
1750 call, and we have stopped in the prologue *before* the
1751 stack frame was allocated.
1753 Recognize this case by looking ahead a bit ... */
1755 struct s390_prologue_data data2;
1756 struct prologue_value *sp = &data2.gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1758 if (!(s390_analyze_prologue (gdbarch, func, (CORE_ADDR)-1, &data2)
1759 && sp->kind == pv_register
1760 && sp->reg == S390_SP_REGNUM
1767 /* OK, we've found valid prologue data. */
1770 /* If the frame pointer originally also holds the same value
1771 as the stack pointer, we're probably using it. If it holds
1772 some other value -- even a constant offset -- it is most
1773 likely used as temp register. */
1774 if (pv_is_identical (sp, fp))
1775 frame_pointer = S390_FRAME_REGNUM;
1777 frame_pointer = S390_SP_REGNUM;
1779 /* If we've detected a function with stack frame, we'll still have to
1780 treat it as frameless if we're currently within the function epilog
1781 code at a point where the frame pointer has already been restored.
1782 This can only happen in an innermost frame. */
1783 /* FIXME: cagney/2004-05-01: This sanity check shouldn't be needed,
1784 instead the code should simpliy rely on its analysis. */
1785 if (size > 0 && get_frame_type (next_frame) != NORMAL_FRAME)
1787 /* See the comment in s390_in_function_epilogue_p on why this is
1788 not completely reliable ... */
1789 if (s390_in_function_epilogue_p (gdbarch, frame_pc_unwind (next_frame)))
1791 memset (&data, 0, sizeof (data));
1793 frame_pointer = S390_SP_REGNUM;
1797 /* Once we know the frame register and the frame size, we can unwind
1798 the current value of the frame register from the next frame, and
1799 add back the frame size to arrive that the previous frame's
1800 stack pointer value. */
1801 prev_sp = frame_unwind_register_unsigned (next_frame, frame_pointer) + size;
1802 cfa = prev_sp + 16*word_size + 32;
1804 /* Record the addresses of all register spill slots the prologue parser
1805 has recognized. Consider only registers defined as call-saved by the
1806 ABI; for call-clobbered registers the parser may have recognized
1809 for (i = 6; i <= 15; i++)
1810 if (data.gpr_slot[i] != 0)
1811 info->saved_regs[S390_R0_REGNUM + i].addr = cfa - data.gpr_slot[i];
1815 case ABI_LINUX_S390:
1816 if (data.fpr_slot[4] != 0)
1817 info->saved_regs[S390_F4_REGNUM].addr = cfa - data.fpr_slot[4];
1818 if (data.fpr_slot[6] != 0)
1819 info->saved_regs[S390_F6_REGNUM].addr = cfa - data.fpr_slot[6];
1822 case ABI_LINUX_ZSERIES:
1823 for (i = 8; i <= 15; i++)
1824 if (data.fpr_slot[i] != 0)
1825 info->saved_regs[S390_F0_REGNUM + i].addr = cfa - data.fpr_slot[i];
1829 /* Function return will set PC to %r14. */
1830 info->saved_regs[S390_PC_REGNUM] = info->saved_regs[S390_RETADDR_REGNUM];
1832 /* In frameless functions, we unwind simply by moving the return
1833 address to the PC. However, if we actually stored to the
1834 save area, use that -- we might only think the function frameless
1835 because we're in the middle of the prologue ... */
1837 && !trad_frame_addr_p (info->saved_regs, S390_PC_REGNUM))
1839 info->saved_regs[S390_PC_REGNUM].realreg = S390_RETADDR_REGNUM;
1842 /* Another sanity check: unless this is a frameless function,
1843 we should have found spill slots for SP and PC.
1844 If not, we cannot unwind further -- this happens e.g. in
1845 libc's thread_start routine. */
1848 if (!trad_frame_addr_p (info->saved_regs, S390_SP_REGNUM)
1849 || !trad_frame_addr_p (info->saved_regs, S390_PC_REGNUM))
1853 /* We use the current value of the frame register as local_base,
1854 and the top of the register save area as frame_base. */
1857 info->frame_base = prev_sp + 16*word_size + 32;
1858 info->local_base = prev_sp - size;
1866 s390_backchain_frame_unwind_cache (struct frame_info *next_frame,
1867 struct s390_unwind_cache *info)
1869 struct gdbarch *gdbarch = get_frame_arch (next_frame);
1870 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1871 CORE_ADDR backchain;
1875 /* Get the backchain. */
1876 reg = frame_unwind_register_unsigned (next_frame, S390_SP_REGNUM);
1877 backchain = read_memory_unsigned_integer (reg, word_size);
1879 /* A zero backchain terminates the frame chain. As additional
1880 sanity check, let's verify that the spill slot for SP in the
1881 save area pointed to by the backchain in fact links back to
1884 && safe_read_memory_integer (backchain + 15*word_size, word_size, &sp)
1885 && (CORE_ADDR)sp == backchain)
1887 /* We don't know which registers were saved, but it will have
1888 to be at least %r14 and %r15. This will allow us to continue
1889 unwinding, but other prev-frame registers may be incorrect ... */
1890 info->saved_regs[S390_SP_REGNUM].addr = backchain + 15*word_size;
1891 info->saved_regs[S390_RETADDR_REGNUM].addr = backchain + 14*word_size;
1893 /* Function return will set PC to %r14. */
1894 info->saved_regs[S390_PC_REGNUM] = info->saved_regs[S390_RETADDR_REGNUM];
1896 /* We use the current value of the frame register as local_base,
1897 and the top of the register save area as frame_base. */
1898 info->frame_base = backchain + 16*word_size + 32;
1899 info->local_base = reg;
1902 info->func = frame_pc_unwind (next_frame);
1905 static struct s390_unwind_cache *
1906 s390_frame_unwind_cache (struct frame_info *next_frame,
1907 void **this_prologue_cache)
1909 struct s390_unwind_cache *info;
1910 if (*this_prologue_cache)
1911 return *this_prologue_cache;
1913 info = FRAME_OBSTACK_ZALLOC (struct s390_unwind_cache);
1914 *this_prologue_cache = info;
1915 info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
1917 info->frame_base = -1;
1918 info->local_base = -1;
1920 /* Try to use prologue analysis to fill the unwind cache.
1921 If this fails, fall back to reading the stack backchain. */
1922 if (!s390_prologue_frame_unwind_cache (next_frame, info))
1923 s390_backchain_frame_unwind_cache (next_frame, info);
1929 s390_frame_this_id (struct frame_info *next_frame,
1930 void **this_prologue_cache,
1931 struct frame_id *this_id)
1933 struct s390_unwind_cache *info
1934 = s390_frame_unwind_cache (next_frame, this_prologue_cache);
1936 if (info->frame_base == -1)
1939 *this_id = frame_id_build (info->frame_base, info->func);
1943 s390_frame_prev_register (struct frame_info *next_frame,
1944 void **this_prologue_cache,
1945 int regnum, int *optimizedp,
1946 enum lval_type *lvalp, CORE_ADDR *addrp,
1947 int *realnump, void *bufferp)
1949 struct s390_unwind_cache *info
1950 = s390_frame_unwind_cache (next_frame, this_prologue_cache);
1951 trad_frame_get_prev_register (next_frame, info->saved_regs, regnum,
1952 optimizedp, lvalp, addrp, realnump, bufferp);
1955 static const struct frame_unwind s390_frame_unwind = {
1958 s390_frame_prev_register
1961 static const struct frame_unwind *
1962 s390_frame_sniffer (struct frame_info *next_frame)
1964 return &s390_frame_unwind;
1968 /* Code stubs and their stack frames. For things like PLTs and NULL
1969 function calls (where there is no true frame and the return address
1970 is in the RETADDR register). */
1972 struct s390_stub_unwind_cache
1974 CORE_ADDR frame_base;
1975 struct trad_frame_saved_reg *saved_regs;
1978 static struct s390_stub_unwind_cache *
1979 s390_stub_frame_unwind_cache (struct frame_info *next_frame,
1980 void **this_prologue_cache)
1982 struct gdbarch *gdbarch = get_frame_arch (next_frame);
1983 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1984 struct s390_stub_unwind_cache *info;
1987 if (*this_prologue_cache)
1988 return *this_prologue_cache;
1990 info = FRAME_OBSTACK_ZALLOC (struct s390_stub_unwind_cache);
1991 *this_prologue_cache = info;
1992 info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
1994 /* The return address is in register %r14. */
1995 info->saved_regs[S390_PC_REGNUM].realreg = S390_RETADDR_REGNUM;
1997 /* Retrieve stack pointer and determine our frame base. */
1998 reg = frame_unwind_register_unsigned (next_frame, S390_SP_REGNUM);
1999 info->frame_base = reg + 16*word_size + 32;
2005 s390_stub_frame_this_id (struct frame_info *next_frame,
2006 void **this_prologue_cache,
2007 struct frame_id *this_id)
2009 struct s390_stub_unwind_cache *info
2010 = s390_stub_frame_unwind_cache (next_frame, this_prologue_cache);
2011 *this_id = frame_id_build (info->frame_base, frame_pc_unwind (next_frame));
2015 s390_stub_frame_prev_register (struct frame_info *next_frame,
2016 void **this_prologue_cache,
2017 int regnum, int *optimizedp,
2018 enum lval_type *lvalp, CORE_ADDR *addrp,
2019 int *realnump, void *bufferp)
2021 struct s390_stub_unwind_cache *info
2022 = s390_stub_frame_unwind_cache (next_frame, this_prologue_cache);
2023 trad_frame_get_prev_register (next_frame, info->saved_regs, regnum,
2024 optimizedp, lvalp, addrp, realnump, bufferp);
2027 static const struct frame_unwind s390_stub_frame_unwind = {
2029 s390_stub_frame_this_id,
2030 s390_stub_frame_prev_register
2033 static const struct frame_unwind *
2034 s390_stub_frame_sniffer (struct frame_info *next_frame)
2036 CORE_ADDR pc = frame_pc_unwind (next_frame);
2037 bfd_byte insn[S390_MAX_INSTR_SIZE];
2039 /* If the current PC points to non-readable memory, we assume we
2040 have trapped due to an invalid function pointer call. We handle
2041 the non-existing current function like a PLT stub. */
2042 if (in_plt_section (pc, NULL)
2043 || s390_readinstruction (insn, pc) < 0)
2044 return &s390_stub_frame_unwind;
2049 /* Signal trampoline stack frames. */
2051 struct s390_sigtramp_unwind_cache {
2052 CORE_ADDR frame_base;
2053 struct trad_frame_saved_reg *saved_regs;
2056 static struct s390_sigtramp_unwind_cache *
2057 s390_sigtramp_frame_unwind_cache (struct frame_info *next_frame,
2058 void **this_prologue_cache)
2060 struct gdbarch *gdbarch = get_frame_arch (next_frame);
2061 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2062 struct s390_sigtramp_unwind_cache *info;
2063 ULONGEST this_sp, prev_sp;
2064 CORE_ADDR next_ra, next_cfa, sigreg_ptr;
2067 if (*this_prologue_cache)
2068 return *this_prologue_cache;
2070 info = FRAME_OBSTACK_ZALLOC (struct s390_sigtramp_unwind_cache);
2071 *this_prologue_cache = info;
2072 info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
2074 this_sp = frame_unwind_register_unsigned (next_frame, S390_SP_REGNUM);
2075 next_ra = frame_pc_unwind (next_frame);
2076 next_cfa = this_sp + 16*word_size + 32;
2078 /* New-style RT frame:
2079 retcode + alignment (8 bytes)
2081 ucontext (contains sigregs at offset 5 words) */
2082 if (next_ra == next_cfa)
2084 sigreg_ptr = next_cfa + 8 + 128 + align_up (5*word_size, 8);
2087 /* Old-style RT frame and all non-RT frames:
2088 old signal mask (8 bytes)
2089 pointer to sigregs */
2092 sigreg_ptr = read_memory_unsigned_integer (next_cfa + 8, word_size);
2095 /* The sigregs structure looks like this:
2104 /* Let's ignore the PSW mask, it will not be restored anyway. */
2105 sigreg_ptr += word_size;
2107 /* Next comes the PSW address. */
2108 info->saved_regs[S390_PC_REGNUM].addr = sigreg_ptr;
2109 sigreg_ptr += word_size;
2111 /* Then the GPRs. */
2112 for (i = 0; i < 16; i++)
2114 info->saved_regs[S390_R0_REGNUM + i].addr = sigreg_ptr;
2115 sigreg_ptr += word_size;
2118 /* Then the ACRs. */
2119 for (i = 0; i < 16; i++)
2121 info->saved_regs[S390_A0_REGNUM + i].addr = sigreg_ptr;
2125 /* The floating-point control word. */
2126 info->saved_regs[S390_FPC_REGNUM].addr = sigreg_ptr;
2129 /* And finally the FPRs. */
2130 for (i = 0; i < 16; i++)
2132 info->saved_regs[S390_F0_REGNUM + i].addr = sigreg_ptr;
2136 /* Restore the previous frame's SP. */
2137 prev_sp = read_memory_unsigned_integer (
2138 info->saved_regs[S390_SP_REGNUM].addr,
2141 /* Determine our frame base. */
2142 info->frame_base = prev_sp + 16*word_size + 32;
2148 s390_sigtramp_frame_this_id (struct frame_info *next_frame,
2149 void **this_prologue_cache,
2150 struct frame_id *this_id)
2152 struct s390_sigtramp_unwind_cache *info
2153 = s390_sigtramp_frame_unwind_cache (next_frame, this_prologue_cache);
2154 *this_id = frame_id_build (info->frame_base, frame_pc_unwind (next_frame));
2158 s390_sigtramp_frame_prev_register (struct frame_info *next_frame,
2159 void **this_prologue_cache,
2160 int regnum, int *optimizedp,
2161 enum lval_type *lvalp, CORE_ADDR *addrp,
2162 int *realnump, void *bufferp)
2164 struct s390_sigtramp_unwind_cache *info
2165 = s390_sigtramp_frame_unwind_cache (next_frame, this_prologue_cache);
2166 trad_frame_get_prev_register (next_frame, info->saved_regs, regnum,
2167 optimizedp, lvalp, addrp, realnump, bufferp);
2170 static const struct frame_unwind s390_sigtramp_frame_unwind = {
2172 s390_sigtramp_frame_this_id,
2173 s390_sigtramp_frame_prev_register
2176 static const struct frame_unwind *
2177 s390_sigtramp_frame_sniffer (struct frame_info *next_frame)
2179 CORE_ADDR pc = frame_pc_unwind (next_frame);
2180 bfd_byte sigreturn[2];
2182 if (deprecated_read_memory_nobpt (pc, sigreturn, 2))
2185 if (sigreturn[0] != 0x0a /* svc */)
2188 if (sigreturn[1] != 119 /* sigreturn */
2189 && sigreturn[1] != 173 /* rt_sigreturn */)
2192 return &s390_sigtramp_frame_unwind;
2196 /* Frame base handling. */
2199 s390_frame_base_address (struct frame_info *next_frame, void **this_cache)
2201 struct s390_unwind_cache *info
2202 = s390_frame_unwind_cache (next_frame, this_cache);
2203 return info->frame_base;
2207 s390_local_base_address (struct frame_info *next_frame, void **this_cache)
2209 struct s390_unwind_cache *info
2210 = s390_frame_unwind_cache (next_frame, this_cache);
2211 return info->local_base;
2214 static const struct frame_base s390_frame_base = {
2216 s390_frame_base_address,
2217 s390_local_base_address,
2218 s390_local_base_address
2222 s390_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
2225 pc = frame_unwind_register_unsigned (next_frame, S390_PC_REGNUM);
2226 return gdbarch_addr_bits_remove (gdbarch, pc);
2230 s390_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
2233 sp = frame_unwind_register_unsigned (next_frame, S390_SP_REGNUM);
2234 return gdbarch_addr_bits_remove (gdbarch, sp);
2238 /* DWARF-2 frame support. */
2241 s390_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
2242 struct dwarf2_frame_state_reg *reg)
2244 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2248 case ABI_LINUX_S390:
2249 /* Call-saved registers. */
2250 if ((regnum >= S390_R6_REGNUM && regnum <= S390_R15_REGNUM)
2251 || regnum == S390_F4_REGNUM
2252 || regnum == S390_F6_REGNUM)
2253 reg->how = DWARF2_FRAME_REG_SAME_VALUE;
2255 /* Call-clobbered registers. */
2256 else if ((regnum >= S390_R0_REGNUM && regnum <= S390_R5_REGNUM)
2257 || (regnum >= S390_F0_REGNUM && regnum <= S390_F15_REGNUM
2258 && regnum != S390_F4_REGNUM && regnum != S390_F6_REGNUM))
2259 reg->how = DWARF2_FRAME_REG_UNDEFINED;
2261 /* The return address column. */
2262 else if (regnum == S390_PC_REGNUM)
2263 reg->how = DWARF2_FRAME_REG_RA;
2266 case ABI_LINUX_ZSERIES:
2267 /* Call-saved registers. */
2268 if ((regnum >= S390_R6_REGNUM && regnum <= S390_R15_REGNUM)
2269 || (regnum >= S390_F8_REGNUM && regnum <= S390_F15_REGNUM))
2270 reg->how = DWARF2_FRAME_REG_SAME_VALUE;
2272 /* Call-clobbered registers. */
2273 else if ((regnum >= S390_R0_REGNUM && regnum <= S390_R5_REGNUM)
2274 || (regnum >= S390_F0_REGNUM && regnum <= S390_F7_REGNUM))
2275 reg->how = DWARF2_FRAME_REG_UNDEFINED;
2277 /* The return address column. */
2278 else if (regnum == S390_PC_REGNUM)
2279 reg->how = DWARF2_FRAME_REG_RA;
2285 /* Dummy function calls. */
2287 /* Return non-zero if TYPE is an integer-like type, zero otherwise.
2288 "Integer-like" types are those that should be passed the way
2289 integers are: integers, enums, ranges, characters, and booleans. */
2291 is_integer_like (struct type *type)
2293 enum type_code code = TYPE_CODE (type);
2295 return (code == TYPE_CODE_INT
2296 || code == TYPE_CODE_ENUM
2297 || code == TYPE_CODE_RANGE
2298 || code == TYPE_CODE_CHAR
2299 || code == TYPE_CODE_BOOL);
2302 /* Return non-zero if TYPE is a pointer-like type, zero otherwise.
2303 "Pointer-like" types are those that should be passed the way
2304 pointers are: pointers and references. */
2306 is_pointer_like (struct type *type)
2308 enum type_code code = TYPE_CODE (type);
2310 return (code == TYPE_CODE_PTR
2311 || code == TYPE_CODE_REF);
2315 /* Return non-zero if TYPE is a `float singleton' or `double
2316 singleton', zero otherwise.
2318 A `T singleton' is a struct type with one member, whose type is
2319 either T or a `T singleton'. So, the following are all float
2323 struct { struct { float x; } x; };
2324 struct { struct { struct { float x; } x; } x; };
2328 All such structures are passed as if they were floats or doubles,
2329 as the (revised) ABI says. */
2331 is_float_singleton (struct type *type)
2333 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
2335 struct type *singleton_type = TYPE_FIELD_TYPE (type, 0);
2336 CHECK_TYPEDEF (singleton_type);
2338 return (TYPE_CODE (singleton_type) == TYPE_CODE_FLT
2339 || is_float_singleton (singleton_type));
2346 /* Return non-zero if TYPE is a struct-like type, zero otherwise.
2347 "Struct-like" types are those that should be passed as structs are:
2350 As an odd quirk, not mentioned in the ABI, GCC passes float and
2351 double singletons as if they were a plain float, double, etc. (The
2352 corresponding union types are handled normally.) So we exclude
2353 those types here. *shrug* */
2355 is_struct_like (struct type *type)
2357 enum type_code code = TYPE_CODE (type);
2359 return (code == TYPE_CODE_UNION
2360 || (code == TYPE_CODE_STRUCT && ! is_float_singleton (type)));
2364 /* Return non-zero if TYPE is a float-like type, zero otherwise.
2365 "Float-like" types are those that should be passed as
2366 floating-point values are.
2368 You'd think this would just be floats, doubles, long doubles, etc.
2369 But as an odd quirk, not mentioned in the ABI, GCC passes float and
2370 double singletons as if they were a plain float, double, etc. (The
2371 corresponding union types are handled normally.) So we include
2372 those types here. *shrug* */
2374 is_float_like (struct type *type)
2376 return (TYPE_CODE (type) == TYPE_CODE_FLT
2377 || is_float_singleton (type));
2382 is_power_of_two (unsigned int n)
2384 return ((n & (n - 1)) == 0);
2387 /* Return non-zero if TYPE should be passed as a pointer to a copy,
2390 s390_function_arg_pass_by_reference (struct type *type)
2392 unsigned length = TYPE_LENGTH (type);
2396 /* FIXME: All complex and vector types are also returned by reference. */
2397 return is_struct_like (type) && !is_power_of_two (length);
2400 /* Return non-zero if TYPE should be passed in a float register
2403 s390_function_arg_float (struct type *type)
2405 unsigned length = TYPE_LENGTH (type);
2409 return is_float_like (type);
2412 /* Return non-zero if TYPE should be passed in an integer register
2413 (or a pair of integer registers) if possible. */
2415 s390_function_arg_integer (struct type *type)
2417 unsigned length = TYPE_LENGTH (type);
2421 return is_integer_like (type)
2422 || is_pointer_like (type)
2423 || (is_struct_like (type) && is_power_of_two (length));
2426 /* Return ARG, a `SIMPLE_ARG', sign-extended or zero-extended to a full
2427 word as required for the ABI. */
2429 extend_simple_arg (struct value *arg)
2431 struct type *type = VALUE_TYPE (arg);
2433 /* Even structs get passed in the least significant bits of the
2434 register / memory word. It's not really right to extract them as
2435 an integer, but it does take care of the extension. */
2436 if (TYPE_UNSIGNED (type))
2437 return extract_unsigned_integer (VALUE_CONTENTS (arg),
2438 TYPE_LENGTH (type));
2440 return extract_signed_integer (VALUE_CONTENTS (arg),
2441 TYPE_LENGTH (type));
2445 /* Return the alignment required by TYPE. */
2447 alignment_of (struct type *type)
2451 if (is_integer_like (type)
2452 || is_pointer_like (type)
2453 || TYPE_CODE (type) == TYPE_CODE_FLT)
2454 alignment = TYPE_LENGTH (type);
2455 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2456 || TYPE_CODE (type) == TYPE_CODE_UNION)
2461 for (i = 0; i < TYPE_NFIELDS (type); i++)
2463 int field_alignment = alignment_of (TYPE_FIELD_TYPE (type, i));
2465 if (field_alignment > alignment)
2466 alignment = field_alignment;
2472 /* Check that everything we ever return is a power of two. Lots of
2473 code doesn't want to deal with aligning things to arbitrary
2475 gdb_assert ((alignment & (alignment - 1)) == 0);
2481 /* Put the actual parameter values pointed to by ARGS[0..NARGS-1] in
2482 place to be passed to a function, as specified by the "GNU/Linux
2483 for S/390 ELF Application Binary Interface Supplement".
2485 SP is the current stack pointer. We must put arguments, links,
2486 padding, etc. whereever they belong, and return the new stack
2489 If STRUCT_RETURN is non-zero, then the function we're calling is
2490 going to return a structure by value; STRUCT_ADDR is the address of
2491 a block we've allocated for it on the stack.
2493 Our caller has taken care of any type promotions needed to satisfy
2494 prototypes or the old K&R argument-passing rules. */
2496 s390_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
2497 struct regcache *regcache, CORE_ADDR bp_addr,
2498 int nargs, struct value **args, CORE_ADDR sp,
2499 int struct_return, CORE_ADDR struct_addr)
2501 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2502 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2506 /* If the i'th argument is passed as a reference to a copy, then
2507 copy_addr[i] is the address of the copy we made. */
2508 CORE_ADDR *copy_addr = alloca (nargs * sizeof (CORE_ADDR));
2510 /* Build the reference-to-copy area. */
2511 for (i = 0; i < nargs; i++)
2513 struct value *arg = args[i];
2514 struct type *type = VALUE_TYPE (arg);
2515 unsigned length = TYPE_LENGTH (type);
2517 if (s390_function_arg_pass_by_reference (type))
2520 sp = align_down (sp, alignment_of (type));
2521 write_memory (sp, VALUE_CONTENTS (arg), length);
2526 /* Reserve space for the parameter area. As a conservative
2527 simplification, we assume that everything will be passed on the
2528 stack. Since every argument larger than 8 bytes will be
2529 passed by reference, we use this simple upper bound. */
2532 /* After all that, make sure it's still aligned on an eight-byte
2534 sp = align_down (sp, 8);
2536 /* Finally, place the actual parameters, working from SP towards
2537 higher addresses. The code above is supposed to reserve enough
2542 CORE_ADDR starg = sp;
2544 /* A struct is returned using general register 2. */
2547 regcache_cooked_write_unsigned (regcache, S390_R0_REGNUM + gr,
2552 for (i = 0; i < nargs; i++)
2554 struct value *arg = args[i];
2555 struct type *type = VALUE_TYPE (arg);
2556 unsigned length = TYPE_LENGTH (type);
2558 if (s390_function_arg_pass_by_reference (type))
2562 regcache_cooked_write_unsigned (regcache, S390_R0_REGNUM + gr,
2568 write_memory_unsigned_integer (starg, word_size, copy_addr[i]);
2572 else if (s390_function_arg_float (type))
2574 /* The GNU/Linux for S/390 ABI uses FPRs 0 and 2 to pass arguments,
2575 the GNU/Linux for zSeries ABI uses 0, 2, 4, and 6. */
2576 if (fr <= (tdep->abi == ABI_LINUX_S390 ? 2 : 6))
2578 /* When we store a single-precision value in an FP register,
2579 it occupies the leftmost bits. */
2580 regcache_cooked_write_part (regcache, S390_F0_REGNUM + fr,
2581 0, length, VALUE_CONTENTS (arg));
2586 /* When we store a single-precision value in a stack slot,
2587 it occupies the rightmost bits. */
2588 starg = align_up (starg + length, word_size);
2589 write_memory (starg - length, VALUE_CONTENTS (arg), length);
2592 else if (s390_function_arg_integer (type) && length <= word_size)
2596 /* Integer arguments are always extended to word size. */
2597 regcache_cooked_write_signed (regcache, S390_R0_REGNUM + gr,
2598 extend_simple_arg (arg));
2603 /* Integer arguments are always extended to word size. */
2604 write_memory_signed_integer (starg, word_size,
2605 extend_simple_arg (arg));
2609 else if (s390_function_arg_integer (type) && length == 2*word_size)
2613 regcache_cooked_write (regcache, S390_R0_REGNUM + gr,
2614 VALUE_CONTENTS (arg));
2615 regcache_cooked_write (regcache, S390_R0_REGNUM + gr + 1,
2616 VALUE_CONTENTS (arg) + word_size);
2621 /* If we skipped r6 because we couldn't fit a DOUBLE_ARG
2622 in it, then don't go back and use it again later. */
2625 write_memory (starg, VALUE_CONTENTS (arg), length);
2630 internal_error (__FILE__, __LINE__, "unknown argument type");
2634 /* Allocate the standard frame areas: the register save area, the
2635 word reserved for the compiler (which seems kind of meaningless),
2636 and the back chain pointer. */
2637 sp -= 16*word_size + 32;
2639 /* Store return address. */
2640 regcache_cooked_write_unsigned (regcache, S390_RETADDR_REGNUM, bp_addr);
2642 /* Store updated stack pointer. */
2643 regcache_cooked_write_unsigned (regcache, S390_SP_REGNUM, sp);
2645 /* We need to return the 'stack part' of the frame ID,
2646 which is actually the top of the register save area. */
2647 return sp + 16*word_size + 32;
2650 /* Assuming NEXT_FRAME->prev is a dummy, return the frame ID of that
2651 dummy frame. The frame ID's base needs to match the TOS value
2652 returned by push_dummy_call, and the PC match the dummy frame's
2654 static struct frame_id
2655 s390_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
2657 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2658 CORE_ADDR sp = s390_unwind_sp (gdbarch, next_frame);
2660 return frame_id_build (sp + 16*word_size + 32,
2661 frame_pc_unwind (next_frame));
2665 s390_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
2667 /* Both the 32- and 64-bit ABI's say that the stack pointer should
2668 always be aligned on an eight-byte boundary. */
2673 /* Function return value access. */
2675 static enum return_value_convention
2676 s390_return_value_convention (struct gdbarch *gdbarch, struct type *type)
2678 int length = TYPE_LENGTH (type);
2680 return RETURN_VALUE_STRUCT_CONVENTION;
2682 switch (TYPE_CODE (type))
2684 case TYPE_CODE_STRUCT:
2685 case TYPE_CODE_UNION:
2686 case TYPE_CODE_ARRAY:
2687 return RETURN_VALUE_STRUCT_CONVENTION;
2690 return RETURN_VALUE_REGISTER_CONVENTION;
2694 static enum return_value_convention
2695 s390_return_value (struct gdbarch *gdbarch, struct type *type,
2696 struct regcache *regcache, void *out, const void *in)
2698 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2699 int length = TYPE_LENGTH (type);
2700 enum return_value_convention rvc =
2701 s390_return_value_convention (gdbarch, type);
2706 case RETURN_VALUE_REGISTER_CONVENTION:
2707 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2709 /* When we store a single-precision value in an FP register,
2710 it occupies the leftmost bits. */
2711 regcache_cooked_write_part (regcache, S390_F0_REGNUM,
2714 else if (length <= word_size)
2716 /* Integer arguments are always extended to word size. */
2717 if (TYPE_UNSIGNED (type))
2718 regcache_cooked_write_unsigned (regcache, S390_R2_REGNUM,
2719 extract_unsigned_integer (in, length));
2721 regcache_cooked_write_signed (regcache, S390_R2_REGNUM,
2722 extract_signed_integer (in, length));
2724 else if (length == 2*word_size)
2726 regcache_cooked_write (regcache, S390_R2_REGNUM, in);
2727 regcache_cooked_write (regcache, S390_R3_REGNUM,
2728 (const char *)in + word_size);
2731 internal_error (__FILE__, __LINE__, "invalid return type");
2734 case RETURN_VALUE_STRUCT_CONVENTION:
2735 error ("Cannot set function return value.");
2743 case RETURN_VALUE_REGISTER_CONVENTION:
2744 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2746 /* When we store a single-precision value in an FP register,
2747 it occupies the leftmost bits. */
2748 regcache_cooked_read_part (regcache, S390_F0_REGNUM,
2751 else if (length <= word_size)
2753 /* Integer arguments occupy the rightmost bits. */
2754 regcache_cooked_read_part (regcache, S390_R2_REGNUM,
2755 word_size - length, length, out);
2757 else if (length == 2*word_size)
2759 regcache_cooked_read (regcache, S390_R2_REGNUM, out);
2760 regcache_cooked_read (regcache, S390_R3_REGNUM,
2761 (char *)out + word_size);
2764 internal_error (__FILE__, __LINE__, "invalid return type");
2767 case RETURN_VALUE_STRUCT_CONVENTION:
2768 error ("Function return value unknown.");
2779 static const unsigned char *
2780 s390_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
2782 static unsigned char breakpoint[] = { 0x0, 0x1 };
2784 *lenptr = sizeof (breakpoint);
2789 /* Address handling. */
2792 s390_addr_bits_remove (CORE_ADDR addr)
2794 return addr & 0x7fffffff;
2798 s390_address_class_type_flags (int byte_size, int dwarf2_addr_class)
2801 return TYPE_FLAG_ADDRESS_CLASS_1;
2807 s390_address_class_type_flags_to_name (struct gdbarch *gdbarch, int type_flags)
2809 if (type_flags & TYPE_FLAG_ADDRESS_CLASS_1)
2816 s390_address_class_name_to_type_flags (struct gdbarch *gdbarch, const char *name,
2817 int *type_flags_ptr)
2819 if (strcmp (name, "mode32") == 0)
2821 *type_flags_ptr = TYPE_FLAG_ADDRESS_CLASS_1;
2829 /* Link map offsets. */
2831 static struct link_map_offsets *
2832 s390_svr4_fetch_link_map_offsets (void)
2834 static struct link_map_offsets lmo;
2835 static struct link_map_offsets *lmp = NULL;
2841 lmo.r_debug_size = 8;
2843 lmo.r_map_offset = 4;
2846 lmo.link_map_size = 20;
2848 lmo.l_addr_offset = 0;
2849 lmo.l_addr_size = 4;
2851 lmo.l_name_offset = 4;
2852 lmo.l_name_size = 4;
2854 lmo.l_next_offset = 12;
2855 lmo.l_next_size = 4;
2857 lmo.l_prev_offset = 16;
2858 lmo.l_prev_size = 4;
2864 static struct link_map_offsets *
2865 s390x_svr4_fetch_link_map_offsets (void)
2867 static struct link_map_offsets lmo;
2868 static struct link_map_offsets *lmp = NULL;
2874 lmo.r_debug_size = 16; /* All we need. */
2876 lmo.r_map_offset = 8;
2879 lmo.link_map_size = 40; /* All we need. */
2881 lmo.l_addr_offset = 0;
2882 lmo.l_addr_size = 8;
2884 lmo.l_name_offset = 8;
2885 lmo.l_name_size = 8;
2887 lmo.l_next_offset = 24;
2888 lmo.l_next_size = 8;
2890 lmo.l_prev_offset = 32;
2891 lmo.l_prev_size = 8;
2898 /* Set up gdbarch struct. */
2900 static struct gdbarch *
2901 s390_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2903 struct gdbarch *gdbarch;
2904 struct gdbarch_tdep *tdep;
2906 /* First see if there is already a gdbarch that can satisfy the request. */
2907 arches = gdbarch_list_lookup_by_info (arches, &info);
2909 return arches->gdbarch;
2911 /* None found: is the request for a s390 architecture? */
2912 if (info.bfd_arch_info->arch != bfd_arch_s390)
2913 return NULL; /* No; then it's not for us. */
2915 /* Yes: create a new gdbarch for the specified machine type. */
2916 tdep = XCALLOC (1, struct gdbarch_tdep);
2917 gdbarch = gdbarch_alloc (&info, tdep);
2919 set_gdbarch_believe_pcc_promotion (gdbarch, 0);
2920 set_gdbarch_char_signed (gdbarch, 0);
2922 /* Amount PC must be decremented by after a breakpoint. This is
2923 often the number of bytes returned by BREAKPOINT_FROM_PC but not
2925 set_gdbarch_decr_pc_after_break (gdbarch, 2);
2926 /* Stack grows downward. */
2927 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2928 set_gdbarch_breakpoint_from_pc (gdbarch, s390_breakpoint_from_pc);
2929 set_gdbarch_skip_prologue (gdbarch, s390_skip_prologue);
2930 set_gdbarch_in_function_epilogue_p (gdbarch, s390_in_function_epilogue_p);
2932 set_gdbarch_pc_regnum (gdbarch, S390_PC_REGNUM);
2933 set_gdbarch_sp_regnum (gdbarch, S390_SP_REGNUM);
2934 set_gdbarch_fp0_regnum (gdbarch, S390_F0_REGNUM);
2935 set_gdbarch_num_regs (gdbarch, S390_NUM_REGS);
2936 set_gdbarch_num_pseudo_regs (gdbarch, S390_NUM_PSEUDO_REGS);
2937 set_gdbarch_register_name (gdbarch, s390_register_name);
2938 set_gdbarch_register_type (gdbarch, s390_register_type);
2939 set_gdbarch_stab_reg_to_regnum (gdbarch, s390_dwarf_reg_to_regnum);
2940 set_gdbarch_dwarf_reg_to_regnum (gdbarch, s390_dwarf_reg_to_regnum);
2941 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, s390_dwarf_reg_to_regnum);
2942 set_gdbarch_convert_register_p (gdbarch, s390_convert_register_p);
2943 set_gdbarch_register_to_value (gdbarch, s390_register_to_value);
2944 set_gdbarch_value_to_register (gdbarch, s390_value_to_register);
2945 set_gdbarch_register_reggroup_p (gdbarch, s390_register_reggroup_p);
2946 set_gdbarch_regset_from_core_section (gdbarch,
2947 s390_regset_from_core_section);
2949 /* Inferior function calls. */
2950 set_gdbarch_push_dummy_call (gdbarch, s390_push_dummy_call);
2951 set_gdbarch_unwind_dummy_id (gdbarch, s390_unwind_dummy_id);
2952 set_gdbarch_frame_align (gdbarch, s390_frame_align);
2953 set_gdbarch_return_value (gdbarch, s390_return_value);
2955 /* Frame handling. */
2956 set_gdbarch_in_solib_call_trampoline (gdbarch, in_plt_section);
2957 dwarf2_frame_set_init_reg (gdbarch, s390_dwarf2_frame_init_reg);
2958 frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);
2959 frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
2960 frame_unwind_append_sniffer (gdbarch, s390_stub_frame_sniffer);
2961 frame_unwind_append_sniffer (gdbarch, s390_sigtramp_frame_sniffer);
2962 frame_unwind_append_sniffer (gdbarch, s390_frame_sniffer);
2963 frame_base_set_default (gdbarch, &s390_frame_base);
2964 set_gdbarch_unwind_pc (gdbarch, s390_unwind_pc);
2965 set_gdbarch_unwind_sp (gdbarch, s390_unwind_sp);
2967 switch (info.bfd_arch_info->mach)
2969 case bfd_mach_s390_31:
2970 tdep->abi = ABI_LINUX_S390;
2972 tdep->gregset = &s390_gregset;
2973 tdep->sizeof_gregset = s390_sizeof_gregset;
2974 tdep->fpregset = &s390_fpregset;
2975 tdep->sizeof_fpregset = s390_sizeof_fpregset;
2977 set_gdbarch_addr_bits_remove (gdbarch, s390_addr_bits_remove);
2978 set_gdbarch_pseudo_register_read (gdbarch, s390_pseudo_register_read);
2979 set_gdbarch_pseudo_register_write (gdbarch, s390_pseudo_register_write);
2980 set_solib_svr4_fetch_link_map_offsets (gdbarch,
2981 s390_svr4_fetch_link_map_offsets);
2984 case bfd_mach_s390_64:
2985 tdep->abi = ABI_LINUX_ZSERIES;
2987 tdep->gregset = &s390x_gregset;
2988 tdep->sizeof_gregset = s390x_sizeof_gregset;
2989 tdep->fpregset = &s390_fpregset;
2990 tdep->sizeof_fpregset = s390_sizeof_fpregset;
2992 set_gdbarch_long_bit (gdbarch, 64);
2993 set_gdbarch_long_long_bit (gdbarch, 64);
2994 set_gdbarch_ptr_bit (gdbarch, 64);
2995 set_gdbarch_pseudo_register_read (gdbarch, s390x_pseudo_register_read);
2996 set_gdbarch_pseudo_register_write (gdbarch, s390x_pseudo_register_write);
2997 set_solib_svr4_fetch_link_map_offsets (gdbarch,
2998 s390x_svr4_fetch_link_map_offsets);
2999 set_gdbarch_address_class_type_flags (gdbarch,
3000 s390_address_class_type_flags);
3001 set_gdbarch_address_class_type_flags_to_name (gdbarch,
3002 s390_address_class_type_flags_to_name);
3003 set_gdbarch_address_class_name_to_type_flags (gdbarch,
3004 s390_address_class_name_to_type_flags);
3008 set_gdbarch_print_insn (gdbarch, print_insn_s390);
3015 extern initialize_file_ftype _initialize_s390_tdep; /* -Wmissing-prototypes */
3018 _initialize_s390_tdep (void)
3021 /* Hook us into the gdbarch mechanism. */
3022 register_gdbarch_init (bfd_arch_s390, s390_gdbarch_init);