1 /* Target-dependent code for GDB, the GNU debugger.
3 Copyright 2001, 2002, 2003, 2004, 2005 Free Software Foundation,
6 Contributed by D.J. Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
7 for IBM Deutschland Entwicklung GmbH, IBM Corporation.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "arch-utils.h"
36 #include "../bfd/bfd.h"
37 #include "floatformat.h"
39 #include "trad-frame.h"
40 #include "frame-base.h"
41 #include "frame-unwind.h"
42 #include "dwarf2-frame.h"
43 #include "reggroups.h"
46 #include "gdb_assert.h"
48 #include "solib-svr4.h" /* For struct link_map_offsets. */
50 #include "s390-tdep.h"
53 /* The tdep structure. */
58 enum { ABI_LINUX_S390, ABI_LINUX_ZSERIES } abi;
60 /* Core file register sets. */
61 const struct regset *gregset;
64 const struct regset *fpregset;
69 /* Register information. */
71 struct s390_register_info
77 static struct s390_register_info s390_register_info[S390_NUM_TOTAL_REGS] =
79 /* Program Status Word. */
80 { "pswm", &builtin_type_long },
81 { "pswa", &builtin_type_long },
83 /* General Purpose Registers. */
84 { "r0", &builtin_type_long },
85 { "r1", &builtin_type_long },
86 { "r2", &builtin_type_long },
87 { "r3", &builtin_type_long },
88 { "r4", &builtin_type_long },
89 { "r5", &builtin_type_long },
90 { "r6", &builtin_type_long },
91 { "r7", &builtin_type_long },
92 { "r8", &builtin_type_long },
93 { "r9", &builtin_type_long },
94 { "r10", &builtin_type_long },
95 { "r11", &builtin_type_long },
96 { "r12", &builtin_type_long },
97 { "r13", &builtin_type_long },
98 { "r14", &builtin_type_long },
99 { "r15", &builtin_type_long },
101 /* Access Registers. */
102 { "acr0", &builtin_type_int },
103 { "acr1", &builtin_type_int },
104 { "acr2", &builtin_type_int },
105 { "acr3", &builtin_type_int },
106 { "acr4", &builtin_type_int },
107 { "acr5", &builtin_type_int },
108 { "acr6", &builtin_type_int },
109 { "acr7", &builtin_type_int },
110 { "acr8", &builtin_type_int },
111 { "acr9", &builtin_type_int },
112 { "acr10", &builtin_type_int },
113 { "acr11", &builtin_type_int },
114 { "acr12", &builtin_type_int },
115 { "acr13", &builtin_type_int },
116 { "acr14", &builtin_type_int },
117 { "acr15", &builtin_type_int },
119 /* Floating Point Control Word. */
120 { "fpc", &builtin_type_int },
122 /* Floating Point Registers. */
123 { "f0", &builtin_type_double },
124 { "f1", &builtin_type_double },
125 { "f2", &builtin_type_double },
126 { "f3", &builtin_type_double },
127 { "f4", &builtin_type_double },
128 { "f5", &builtin_type_double },
129 { "f6", &builtin_type_double },
130 { "f7", &builtin_type_double },
131 { "f8", &builtin_type_double },
132 { "f9", &builtin_type_double },
133 { "f10", &builtin_type_double },
134 { "f11", &builtin_type_double },
135 { "f12", &builtin_type_double },
136 { "f13", &builtin_type_double },
137 { "f14", &builtin_type_double },
138 { "f15", &builtin_type_double },
140 /* Pseudo registers. */
141 { "pc", &builtin_type_void_func_ptr },
142 { "cc", &builtin_type_int },
145 /* Return the name of register REGNUM. */
147 s390_register_name (int regnum)
149 gdb_assert (regnum >= 0 && regnum < S390_NUM_TOTAL_REGS);
150 return s390_register_info[regnum].name;
153 /* Return the GDB type object for the "standard" data type of data in
156 s390_register_type (struct gdbarch *gdbarch, int regnum)
158 gdb_assert (regnum >= 0 && regnum < S390_NUM_TOTAL_REGS);
159 return *s390_register_info[regnum].type;
162 /* DWARF Register Mapping. */
164 static int s390_dwarf_regmap[] =
166 /* General Purpose Registers. */
167 S390_R0_REGNUM, S390_R1_REGNUM, S390_R2_REGNUM, S390_R3_REGNUM,
168 S390_R4_REGNUM, S390_R5_REGNUM, S390_R6_REGNUM, S390_R7_REGNUM,
169 S390_R8_REGNUM, S390_R9_REGNUM, S390_R10_REGNUM, S390_R11_REGNUM,
170 S390_R12_REGNUM, S390_R13_REGNUM, S390_R14_REGNUM, S390_R15_REGNUM,
172 /* Floating Point Registers. */
173 S390_F0_REGNUM, S390_F2_REGNUM, S390_F4_REGNUM, S390_F6_REGNUM,
174 S390_F1_REGNUM, S390_F3_REGNUM, S390_F5_REGNUM, S390_F7_REGNUM,
175 S390_F8_REGNUM, S390_F10_REGNUM, S390_F12_REGNUM, S390_F14_REGNUM,
176 S390_F9_REGNUM, S390_F11_REGNUM, S390_F13_REGNUM, S390_F15_REGNUM,
178 /* Control Registers (not mapped). */
179 -1, -1, -1, -1, -1, -1, -1, -1,
180 -1, -1, -1, -1, -1, -1, -1, -1,
182 /* Access Registers. */
183 S390_A0_REGNUM, S390_A1_REGNUM, S390_A2_REGNUM, S390_A3_REGNUM,
184 S390_A4_REGNUM, S390_A5_REGNUM, S390_A6_REGNUM, S390_A7_REGNUM,
185 S390_A8_REGNUM, S390_A9_REGNUM, S390_A10_REGNUM, S390_A11_REGNUM,
186 S390_A12_REGNUM, S390_A13_REGNUM, S390_A14_REGNUM, S390_A15_REGNUM,
188 /* Program Status Word. */
193 /* Convert DWARF register number REG to the appropriate register
194 number used by GDB. */
196 s390_dwarf_reg_to_regnum (int reg)
200 if (reg >= 0 && reg < ARRAY_SIZE (s390_dwarf_regmap))
201 regnum = s390_dwarf_regmap[reg];
204 warning (_("Unmapped DWARF Register #%d encountered."), reg);
209 /* Pseudo registers - PC and condition code. */
212 s390_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
213 int regnum, gdb_byte *buf)
220 regcache_raw_read_unsigned (regcache, S390_PSWA_REGNUM, &val);
221 store_unsigned_integer (buf, 4, val & 0x7fffffff);
225 regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &val);
226 store_unsigned_integer (buf, 4, (val >> 12) & 3);
230 internal_error (__FILE__, __LINE__, _("invalid regnum"));
235 s390_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
236 int regnum, const gdb_byte *buf)
243 val = extract_unsigned_integer (buf, 4);
244 regcache_raw_read_unsigned (regcache, S390_PSWA_REGNUM, &psw);
245 psw = (psw & 0x80000000) | (val & 0x7fffffff);
246 regcache_raw_write_unsigned (regcache, S390_PSWA_REGNUM, psw);
250 val = extract_unsigned_integer (buf, 4);
251 regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &psw);
252 psw = (psw & ~((ULONGEST)3 << 12)) | ((val & 3) << 12);
253 regcache_raw_write_unsigned (regcache, S390_PSWM_REGNUM, psw);
257 internal_error (__FILE__, __LINE__, _("invalid regnum"));
262 s390x_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
263 int regnum, gdb_byte *buf)
270 regcache_raw_read (regcache, S390_PSWA_REGNUM, buf);
274 regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &val);
275 store_unsigned_integer (buf, 4, (val >> 44) & 3);
279 internal_error (__FILE__, __LINE__, _("invalid regnum"));
284 s390x_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
285 int regnum, const gdb_byte *buf)
292 regcache_raw_write (regcache, S390_PSWA_REGNUM, buf);
296 val = extract_unsigned_integer (buf, 4);
297 regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &psw);
298 psw = (psw & ~((ULONGEST)3 << 44)) | ((val & 3) << 44);
299 regcache_raw_write_unsigned (regcache, S390_PSWM_REGNUM, psw);
303 internal_error (__FILE__, __LINE__, _("invalid regnum"));
307 /* 'float' values are stored in the upper half of floating-point
308 registers, even though we are otherwise a big-endian platform. */
311 s390_convert_register_p (int regno, struct type *type)
313 return (regno >= S390_F0_REGNUM && regno <= S390_F15_REGNUM)
314 && TYPE_LENGTH (type) < 8;
318 s390_register_to_value (struct frame_info *frame, int regnum,
319 struct type *valtype, gdb_byte *out)
322 int len = TYPE_LENGTH (valtype);
323 gdb_assert (len < 8);
325 get_frame_register (frame, regnum, in);
326 memcpy (out, in, len);
330 s390_value_to_register (struct frame_info *frame, int regnum,
331 struct type *valtype, const gdb_byte *in)
334 int len = TYPE_LENGTH (valtype);
335 gdb_assert (len < 8);
338 memcpy (out, in, len);
339 put_frame_register (frame, regnum, out);
342 /* Register groups. */
345 s390_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
346 struct reggroup *group)
348 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
350 /* Registers displayed via 'info regs'. */
351 if (group == general_reggroup)
352 return (regnum >= S390_R0_REGNUM && regnum <= S390_R15_REGNUM)
353 || regnum == S390_PC_REGNUM
354 || regnum == S390_CC_REGNUM;
356 /* Registers displayed via 'info float'. */
357 if (group == float_reggroup)
358 return (regnum >= S390_F0_REGNUM && regnum <= S390_F15_REGNUM)
359 || regnum == S390_FPC_REGNUM;
361 /* Registers that need to be saved/restored in order to
362 push or pop frames. */
363 if (group == save_reggroup || group == restore_reggroup)
364 return regnum != S390_PSWM_REGNUM && regnum != S390_PSWA_REGNUM;
366 return default_register_reggroup_p (gdbarch, regnum, group);
370 /* Core file register sets. */
372 int s390_regmap_gregset[S390_NUM_REGS] =
374 /* Program Status Word. */
376 /* General Purpose Registers. */
377 0x08, 0x0c, 0x10, 0x14,
378 0x18, 0x1c, 0x20, 0x24,
379 0x28, 0x2c, 0x30, 0x34,
380 0x38, 0x3c, 0x40, 0x44,
381 /* Access Registers. */
382 0x48, 0x4c, 0x50, 0x54,
383 0x58, 0x5c, 0x60, 0x64,
384 0x68, 0x6c, 0x70, 0x74,
385 0x78, 0x7c, 0x80, 0x84,
386 /* Floating Point Control Word. */
388 /* Floating Point Registers. */
389 -1, -1, -1, -1, -1, -1, -1, -1,
390 -1, -1, -1, -1, -1, -1, -1, -1,
393 int s390x_regmap_gregset[S390_NUM_REGS] =
396 /* General Purpose Registers. */
397 0x10, 0x18, 0x20, 0x28,
398 0x30, 0x38, 0x40, 0x48,
399 0x50, 0x58, 0x60, 0x68,
400 0x70, 0x78, 0x80, 0x88,
401 /* Access Registers. */
402 0x90, 0x94, 0x98, 0x9c,
403 0xa0, 0xa4, 0xa8, 0xac,
404 0xb0, 0xb4, 0xb8, 0xbc,
405 0xc0, 0xc4, 0xc8, 0xcc,
406 /* Floating Point Control Word. */
408 /* Floating Point Registers. */
409 -1, -1, -1, -1, -1, -1, -1, -1,
410 -1, -1, -1, -1, -1, -1, -1, -1,
413 int s390_regmap_fpregset[S390_NUM_REGS] =
415 /* Program Status Word. */
417 /* General Purpose Registers. */
418 -1, -1, -1, -1, -1, -1, -1, -1,
419 -1, -1, -1, -1, -1, -1, -1, -1,
420 /* Access Registers. */
421 -1, -1, -1, -1, -1, -1, -1, -1,
422 -1, -1, -1, -1, -1, -1, -1, -1,
423 /* Floating Point Control Word. */
425 /* Floating Point Registers. */
426 0x08, 0x10, 0x18, 0x20,
427 0x28, 0x30, 0x38, 0x40,
428 0x48, 0x50, 0x58, 0x60,
429 0x68, 0x70, 0x78, 0x80,
432 /* Supply register REGNUM from the register set REGSET to register cache
433 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
435 s390_supply_regset (const struct regset *regset, struct regcache *regcache,
436 int regnum, const void *regs, size_t len)
438 const int *offset = regset->descr;
441 for (i = 0; i < S390_NUM_REGS; i++)
443 if ((regnum == i || regnum == -1) && offset[i] != -1)
444 regcache_raw_supply (regcache, i, (const char *)regs + offset[i]);
448 static const struct regset s390_gregset = {
453 static const struct regset s390x_gregset = {
454 s390x_regmap_gregset,
458 static const struct regset s390_fpregset = {
459 s390_regmap_fpregset,
463 /* Return the appropriate register set for the core section identified
464 by SECT_NAME and SECT_SIZE. */
465 const struct regset *
466 s390_regset_from_core_section (struct gdbarch *gdbarch,
467 const char *sect_name, size_t sect_size)
469 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
471 if (strcmp (sect_name, ".reg") == 0 && sect_size == tdep->sizeof_gregset)
472 return tdep->gregset;
474 if (strcmp (sect_name, ".reg2") == 0 && sect_size == tdep->sizeof_fpregset)
475 return tdep->fpregset;
481 /* Prologue analysis. */
483 /* When we analyze a prologue, we're really doing 'abstract
484 interpretation' or 'pseudo-evaluation': running the function's code
485 in simulation, but using conservative approximations of the values
486 it would have when it actually runs. For example, if our function
487 starts with the instruction:
489 ahi r1, 42 # add halfword immediate 42 to r1
491 we don't know exactly what value will be in r1 after executing this
492 instruction, but we do know it'll be 42 greater than its original
495 If we then see an instruction like:
497 ahi r1, 22 # add halfword immediate 22 to r1
499 we still don't know what r1's value is, but again, we can say it is
500 now 64 greater than its original value.
502 If the next instruction were:
504 lr r2, r1 # set r2 to r1's value
506 then we can say that r2's value is now the original value of r1
509 Of course, this can only go so far before it gets unreasonable. If
510 we wanted to be able to say anything about the value of r1 after
513 xr r1, r3 # exclusive-or r1 and r3, place result in r1
515 then things would get pretty complex. But remember, we're just
516 doing a conservative approximation; if exclusive-or instructions
517 aren't relevant to prologues, we can just say r1's value is now
518 'unknown'. We can ignore things that are too complex, if that loss
519 of information is acceptable for our application.
521 Once you've reached an instruction that you don't know how to
522 simulate, you stop. Now you examine the state of the registers and
523 stack slots you've kept track of. For example:
525 - To see how large your stack frame is, just check the value of sp;
526 if it's the original value of sp minus a constant, then that
527 constant is the stack frame's size. If the sp's value has been
528 marked as 'unknown', then that means the prologue has done
529 something too complex for us to track, and we don't know the
532 - To see whether we've saved the SP in the current frame's back
533 chain slot, we just check whether the current value of the back
534 chain stack slot is the original value of the sp.
536 Sure, this takes some work. But prologue analyzers aren't
537 quick-and-simple pattern patching to recognize a few fixed prologue
538 forms any more; they're big, hairy functions. Along with inferior
539 function calls, prologue analysis accounts for a substantial
540 portion of the time needed to stabilize a GDB port. So I think
541 it's worthwhile to look for an approach that will be easier to
542 understand and maintain. In the approach used here:
544 - It's easier to see that the analyzer is correct: you just see
545 whether the analyzer properly (albiet conservatively) simulates
546 the effect of each instruction.
548 - It's easier to extend the analyzer: you can add support for new
549 instructions, and know that you haven't broken anything that
550 wasn't already broken before.
552 - It's orthogonal: to gather new information, you don't need to
553 complicate the code for each instruction. As long as your domain
554 of conservative values is already detailed enough to tell you
555 what you need, then all the existing instruction simulations are
556 already gathering the right data for you.
558 A 'struct prologue_value' is a conservative approximation of the
559 real value the register or stack slot will have. */
561 struct prologue_value {
563 /* What sort of value is this? This determines the interpretation
564 of subsequent fields. */
567 /* We don't know anything about the value. This is also used for
568 values we could have kept track of, when doing so would have
569 been too complex and we don't want to bother. The bottom of
573 /* A known constant. K is its value. */
576 /* The value that register REG originally had *UPON ENTRY TO THE
577 FUNCTION*, plus K. If K is zero, this means, obviously, just
578 the value REG had upon entry to the function. REG is a GDB
579 register number. Before we start interpreting, we initialize
580 every register R to { pv_register, R, 0 }. */
585 /* The meanings of the following fields depend on 'kind'; see the
586 comments for the specific 'kind' values. */
592 /* Set V to be unknown. */
594 pv_set_to_unknown (struct prologue_value *v)
596 v->kind = pv_unknown;
600 /* Set V to the constant K. */
602 pv_set_to_constant (struct prologue_value *v, CORE_ADDR k)
604 v->kind = pv_constant;
609 /* Set V to the original value of register REG, plus K. */
611 pv_set_to_register (struct prologue_value *v, int reg, CORE_ADDR k)
613 v->kind = pv_register;
619 /* If one of *A and *B is a constant, and the other isn't, swap the
620 pointers as necessary to ensure that *B points to the constant.
621 This can reduce the number of cases we need to analyze in the
624 pv_constant_last (struct prologue_value **a,
625 struct prologue_value **b)
627 if ((*a)->kind == pv_constant
628 && (*b)->kind != pv_constant)
630 struct prologue_value *temp = *a;
637 /* Set SUM to the sum of A and B. SUM, A, and B may point to the same
638 'struct prologue_value' object. */
640 pv_add (struct prologue_value *sum,
641 struct prologue_value *a,
642 struct prologue_value *b)
644 pv_constant_last (&a, &b);
646 /* We can handle adding constants to registers, and other constants. */
647 if (b->kind == pv_constant
648 && (a->kind == pv_register
649 || a->kind == pv_constant))
652 sum->reg = a->reg; /* not meaningful if a is pv_constant, but
654 sum->k = a->k + b->k;
657 /* Anything else we don't know how to add. We don't have a
658 representation for, say, the sum of two registers, or a multiple
659 of a register's value (adding a register to itself). */
661 sum->kind = pv_unknown;
665 /* Add the constant K to V. */
667 pv_add_constant (struct prologue_value *v, CORE_ADDR k)
669 struct prologue_value pv_k;
671 /* Rather than thinking of all the cases we can and can't handle,
672 we'll just let pv_add take care of that for us. */
673 pv_set_to_constant (&pv_k, k);
674 pv_add (v, v, &pv_k);
678 /* Subtract B from A, and put the result in DIFF.
680 This isn't quite the same as negating B and adding it to A, since
681 we don't have a representation for the negation of anything but a
682 constant. For example, we can't negate { pv_register, R1, 10 },
683 but we do know that { pv_register, R1, 10 } minus { pv_register,
684 R1, 5 } is { pv_constant, <ignored>, 5 }.
686 This means, for example, that we can subtract two stack addresses;
687 they're both relative to the original SP. Since the frame pointer
688 is set based on the SP, its value will be the original SP plus some
689 constant (probably zero), so we can use its value just fine. */
691 pv_subtract (struct prologue_value *diff,
692 struct prologue_value *a,
693 struct prologue_value *b)
695 pv_constant_last (&a, &b);
697 /* We can subtract a constant from another constant, or from a
699 if (b->kind == pv_constant
700 && (a->kind == pv_register
701 || a->kind == pv_constant))
703 diff->kind = a->kind;
704 diff->reg = a->reg; /* not always meaningful, but harmless */
705 diff->k = a->k - b->k;
708 /* We can subtract a register from itself, yielding a constant. */
709 else if (a->kind == pv_register
710 && b->kind == pv_register
713 diff->kind = pv_constant;
714 diff->k = a->k - b->k;
717 /* We don't know how to subtract anything else. */
719 diff->kind = pv_unknown;
723 /* Set AND to the logical and of A and B. */
725 pv_logical_and (struct prologue_value *and,
726 struct prologue_value *a,
727 struct prologue_value *b)
729 pv_constant_last (&a, &b);
731 /* We can 'and' two constants. */
732 if (a->kind == pv_constant
733 && b->kind == pv_constant)
735 and->kind = pv_constant;
736 and->k = a->k & b->k;
739 /* We can 'and' anything with the constant zero. */
740 else if (b->kind == pv_constant
743 and->kind = pv_constant;
747 /* We can 'and' anything with ~0. */
748 else if (b->kind == pv_constant
749 && b->k == ~ (CORE_ADDR) 0)
752 /* We can 'and' a register with itself. */
753 else if (a->kind == pv_register
754 && b->kind == pv_register
759 /* Otherwise, we don't know. */
761 pv_set_to_unknown (and);
765 /* Return non-zero iff A and B are identical expressions.
767 This is not the same as asking if the two values are equal; the
768 result of such a comparison would have to be a pv_boolean, and
769 asking whether two 'unknown' values were equal would give you
770 pv_maybe. Same for comparing, say, { pv_register, R1, 0 } and {
771 pv_register, R2, 0}. Instead, this is asking whether the two
772 representations are the same. */
774 pv_is_identical (struct prologue_value *a,
775 struct prologue_value *b)
777 if (a->kind != b->kind)
785 return (a->k == b->k);
787 return (a->reg == b->reg && a->k == b->k);
794 /* Return non-zero if A is the original value of register number R
795 plus K, zero otherwise. */
797 pv_is_register (struct prologue_value *a, int r, CORE_ADDR k)
799 return (a->kind == pv_register
805 /* Decoding S/390 instructions. */
807 /* Named opcode values for the S/390 instructions we recognize. Some
808 instructions have their opcode split across two fields; those are the
809 op1_* and op2_* enums. */
812 op1_lhi = 0xa7, op2_lhi = 0x08,
813 op1_lghi = 0xa7, op2_lghi = 0x09,
817 op1_ly = 0xe3, op2_ly = 0x58,
818 op1_lg = 0xe3, op2_lg = 0x04,
820 op1_lmy = 0xeb, op2_lmy = 0x98,
821 op1_lmg = 0xeb, op2_lmg = 0x04,
823 op1_sty = 0xe3, op2_sty = 0x50,
824 op1_stg = 0xe3, op2_stg = 0x24,
827 op1_stmy = 0xeb, op2_stmy = 0x90,
828 op1_stmg = 0xeb, op2_stmg = 0x24,
829 op1_aghi = 0xa7, op2_aghi = 0x0b,
830 op1_ahi = 0xa7, op2_ahi = 0x0a,
834 op1_ay = 0xe3, op2_ay = 0x5a,
835 op1_ag = 0xe3, op2_ag = 0x08,
839 op1_sy = 0xe3, op2_sy = 0x5b,
840 op1_sg = 0xe3, op2_sg = 0x09,
844 op1_lay = 0xe3, op2_lay = 0x71,
845 op1_larl = 0xc0, op2_larl = 0x00,
850 op1_bras = 0xa7, op2_bras = 0x05,
851 op1_brasl= 0xc0, op2_brasl= 0x05,
852 op1_brc = 0xa7, op2_brc = 0x04,
853 op1_brcl = 0xc0, op2_brcl = 0x04,
857 /* Read a single instruction from address AT. */
859 #define S390_MAX_INSTR_SIZE 6
861 s390_readinstruction (bfd_byte instr[], CORE_ADDR at)
863 static int s390_instrlen[] = { 2, 4, 4, 6 };
866 if (deprecated_read_memory_nobpt (at, &instr[0], 2))
868 instrlen = s390_instrlen[instr[0] >> 6];
871 if (deprecated_read_memory_nobpt (at + 2, &instr[2], instrlen - 2))
878 /* The functions below are for recognizing and decoding S/390
879 instructions of various formats. Each of them checks whether INSN
880 is an instruction of the given format, with the specified opcodes.
881 If it is, it sets the remaining arguments to the values of the
882 instruction's fields, and returns a non-zero value; otherwise, it
885 These functions' arguments appear in the order they appear in the
886 instruction, not in the machine-language form. So, opcodes always
887 come first, even though they're sometimes scattered around the
888 instructions. And displacements appear before base and extension
889 registers, as they do in the assembly syntax, not at the end, as
890 they do in the machine language. */
892 is_ri (bfd_byte *insn, int op1, int op2, unsigned int *r1, int *i2)
894 if (insn[0] == op1 && (insn[1] & 0xf) == op2)
896 *r1 = (insn[1] >> 4) & 0xf;
897 /* i2 is a 16-bit signed quantity. */
898 *i2 = (((insn[2] << 8) | insn[3]) ^ 0x8000) - 0x8000;
907 is_ril (bfd_byte *insn, int op1, int op2,
908 unsigned int *r1, int *i2)
910 if (insn[0] == op1 && (insn[1] & 0xf) == op2)
912 *r1 = (insn[1] >> 4) & 0xf;
913 /* i2 is a signed quantity. If the host 'int' is 32 bits long,
914 no sign extension is necessary, but we don't want to assume
916 *i2 = (((insn[2] << 24)
919 | (insn[5])) ^ 0x80000000) - 0x80000000;
928 is_rr (bfd_byte *insn, int op, unsigned int *r1, unsigned int *r2)
932 *r1 = (insn[1] >> 4) & 0xf;
942 is_rre (bfd_byte *insn, int op, unsigned int *r1, unsigned int *r2)
944 if (((insn[0] << 8) | insn[1]) == op)
946 /* Yes, insn[3]. insn[2] is unused in RRE format. */
947 *r1 = (insn[3] >> 4) & 0xf;
957 is_rs (bfd_byte *insn, int op,
958 unsigned int *r1, unsigned int *r3, unsigned int *d2, unsigned int *b2)
962 *r1 = (insn[1] >> 4) & 0xf;
964 *b2 = (insn[2] >> 4) & 0xf;
965 *d2 = ((insn[2] & 0xf) << 8) | insn[3];
974 is_rsy (bfd_byte *insn, int op1, int op2,
975 unsigned int *r1, unsigned int *r3, unsigned int *d2, unsigned int *b2)
980 *r1 = (insn[1] >> 4) & 0xf;
982 *b2 = (insn[2] >> 4) & 0xf;
983 /* The 'long displacement' is a 20-bit signed integer. */
984 *d2 = ((((insn[2] & 0xf) << 8) | insn[3] | (insn[4] << 12))
985 ^ 0x80000) - 0x80000;
994 is_rx (bfd_byte *insn, int op,
995 unsigned int *r1, unsigned int *d2, unsigned int *x2, unsigned int *b2)
999 *r1 = (insn[1] >> 4) & 0xf;
1000 *x2 = insn[1] & 0xf;
1001 *b2 = (insn[2] >> 4) & 0xf;
1002 *d2 = ((insn[2] & 0xf) << 8) | insn[3];
1011 is_rxy (bfd_byte *insn, int op1, int op2,
1012 unsigned int *r1, unsigned int *d2, unsigned int *x2, unsigned int *b2)
1017 *r1 = (insn[1] >> 4) & 0xf;
1018 *x2 = insn[1] & 0xf;
1019 *b2 = (insn[2] >> 4) & 0xf;
1020 /* The 'long displacement' is a 20-bit signed integer. */
1021 *d2 = ((((insn[2] & 0xf) << 8) | insn[3] | (insn[4] << 12))
1022 ^ 0x80000) - 0x80000;
1030 /* Set ADDR to the effective address for an X-style instruction, like:
1034 Here, X2 and B2 are registers, and D2 is a signed 20-bit
1035 constant; the effective address is the sum of all three. If either
1036 X2 or B2 are zero, then it doesn't contribute to the sum --- this
1037 means that r0 can't be used as either X2 or B2.
1039 GPR is an array of general register values, indexed by GPR number,
1040 not GDB register number. */
1042 compute_x_addr (struct prologue_value *addr,
1043 struct prologue_value *gpr,
1044 int d2, unsigned int x2, unsigned int b2)
1046 /* We can't just add stuff directly in addr; it might alias some of
1047 the registers we need to read. */
1048 struct prologue_value result;
1050 pv_set_to_constant (&result, d2);
1052 pv_add (&result, &result, &gpr[x2]);
1054 pv_add (&result, &result, &gpr[b2]);
1060 #define S390_NUM_GPRS 16
1061 #define S390_NUM_FPRS 16
1063 struct s390_prologue_data {
1065 /* The size of a GPR or FPR. */
1069 /* The general-purpose registers. */
1070 struct prologue_value gpr[S390_NUM_GPRS];
1072 /* The floating-point registers. */
1073 struct prologue_value fpr[S390_NUM_FPRS];
1075 /* The offset relative to the CFA where the incoming GPR N was saved
1076 by the function prologue. 0 if not saved or unknown. */
1077 int gpr_slot[S390_NUM_GPRS];
1079 /* Likewise for FPRs. */
1080 int fpr_slot[S390_NUM_FPRS];
1082 /* Nonzero if the backchain was saved. This is assumed to be the
1083 case when the incoming SP is saved at the current SP location. */
1084 int back_chain_saved_p;
1087 /* Do a SIZE-byte store of VALUE to ADDR. */
1089 s390_store (struct prologue_value *addr,
1091 struct prologue_value *value,
1092 struct s390_prologue_data *data)
1094 struct prologue_value cfa, offset;
1097 /* Check whether we are storing the backchain. */
1098 pv_subtract (&offset, &data->gpr[S390_SP_REGNUM - S390_R0_REGNUM], addr);
1100 if (offset.kind == pv_constant && offset.k == 0)
1101 if (size == data->gpr_size
1102 && pv_is_register (value, S390_SP_REGNUM, 0))
1104 data->back_chain_saved_p = 1;
1109 /* Check whether we are storing a register into the stack. */
1110 pv_set_to_register (&cfa, S390_SP_REGNUM, 16 * data->gpr_size + 32);
1111 pv_subtract (&offset, &cfa, addr);
1113 if (offset.kind == pv_constant
1114 && offset.k < INT_MAX && offset.k > 0
1115 && offset.k % data->gpr_size == 0)
1117 /* If we are storing the original value of a register, we want to
1118 record the CFA offset. If the same register is stored multiple
1119 times, the stack slot with the highest address counts. */
1121 for (i = 0; i < S390_NUM_GPRS; i++)
1122 if (size == data->gpr_size
1123 && pv_is_register (value, S390_R0_REGNUM + i, 0))
1124 if (data->gpr_slot[i] == 0
1125 || data->gpr_slot[i] > offset.k)
1127 data->gpr_slot[i] = offset.k;
1131 for (i = 0; i < S390_NUM_FPRS; i++)
1132 if (size == data->fpr_size
1133 && pv_is_register (value, S390_F0_REGNUM + i, 0))
1134 if (data->fpr_slot[i] == 0
1135 || data->fpr_slot[i] > offset.k)
1137 data->fpr_slot[i] = offset.k;
1143 /* Note: If this is some store we cannot identify, you might think we
1144 should forget our cached values, as any of those might have been hit.
1146 However, we make the assumption that the register save areas are only
1147 ever stored to once in any given function, and we do recognize these
1148 stores. Thus every store we cannot recognize does not hit our data. */
1151 /* Do a SIZE-byte load from ADDR into VALUE. */
1153 s390_load (struct prologue_value *addr,
1155 struct prologue_value *value,
1156 struct s390_prologue_data *data)
1158 struct prologue_value cfa, offset;
1161 /* If it's a load from an in-line constant pool, then we can
1162 simulate that, under the assumption that the code isn't
1163 going to change between the time the processor actually
1164 executed it creating the current frame, and the time when
1165 we're analyzing the code to unwind past that frame. */
1166 if (addr->kind == pv_constant)
1168 struct section_table *secp;
1169 secp = target_section_by_addr (¤t_target, addr->k);
1171 && (bfd_get_section_flags (secp->bfd, secp->the_bfd_section)
1174 pv_set_to_constant (value, read_memory_integer (addr->k, size));
1179 /* Check whether we are accessing one of our save slots. */
1180 pv_set_to_register (&cfa, S390_SP_REGNUM, 16 * data->gpr_size + 32);
1181 pv_subtract (&offset, &cfa, addr);
1183 if (offset.kind == pv_constant
1184 && offset.k < INT_MAX && offset.k > 0)
1186 for (i = 0; i < S390_NUM_GPRS; i++)
1187 if (offset.k == data->gpr_slot[i])
1189 pv_set_to_register (value, S390_R0_REGNUM + i, 0);
1193 for (i = 0; i < S390_NUM_FPRS; i++)
1194 if (offset.k == data->fpr_slot[i])
1196 pv_set_to_register (value, S390_F0_REGNUM + i, 0);
1201 /* Otherwise, we don't know the value. */
1202 pv_set_to_unknown (value);
1206 /* Analyze the prologue of the function starting at START_PC,
1207 continuing at most until CURRENT_PC. Initialize DATA to
1208 hold all information we find out about the state of the registers
1209 and stack slots. Return the address of the instruction after
1210 the last one that changed the SP, FP, or back chain; or zero
1213 s390_analyze_prologue (struct gdbarch *gdbarch,
1215 CORE_ADDR current_pc,
1216 struct s390_prologue_data *data)
1218 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1220 /* Our return value:
1221 The address of the instruction after the last one that changed
1222 the SP, FP, or back chain; zero if we got an error trying to
1224 CORE_ADDR result = start_pc;
1226 /* The current PC for our abstract interpretation. */
1229 /* The address of the next instruction after that. */
1232 /* Set up everything's initial value. */
1236 /* For the purpose of prologue tracking, we consider the GPR size to
1237 be equal to the ABI word size, even if it is actually larger
1238 (i.e. when running a 32-bit binary under a 64-bit kernel). */
1239 data->gpr_size = word_size;
1242 for (i = 0; i < S390_NUM_GPRS; i++)
1243 pv_set_to_register (&data->gpr[i], S390_R0_REGNUM + i, 0);
1245 for (i = 0; i < S390_NUM_FPRS; i++)
1246 pv_set_to_register (&data->fpr[i], S390_F0_REGNUM + i, 0);
1248 for (i = 0; i < S390_NUM_GPRS; i++)
1249 data->gpr_slot[i] = 0;
1251 for (i = 0; i < S390_NUM_FPRS; i++)
1252 data->fpr_slot[i] = 0;
1254 data->back_chain_saved_p = 0;
1257 /* Start interpreting instructions, until we hit the frame's
1258 current PC or the first branch instruction. */
1259 for (pc = start_pc; pc > 0 && pc < current_pc; pc = next_pc)
1261 bfd_byte insn[S390_MAX_INSTR_SIZE];
1262 int insn_len = s390_readinstruction (insn, pc);
1264 /* Fields for various kinds of instructions. */
1265 unsigned int b2, r1, r2, x2, r3;
1268 /* The values of SP and FP before this instruction,
1269 for detecting instructions that change them. */
1270 struct prologue_value pre_insn_sp, pre_insn_fp;
1271 /* Likewise for the flag whether the back chain was saved. */
1272 int pre_insn_back_chain_saved_p;
1274 /* If we got an error trying to read the instruction, report it. */
1281 next_pc = pc + insn_len;
1283 pre_insn_sp = data->gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1284 pre_insn_fp = data->gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
1285 pre_insn_back_chain_saved_p = data->back_chain_saved_p;
1287 /* LHI r1, i2 --- load halfword immediate */
1289 && is_ri (insn, op1_lhi, op2_lhi, &r1, &i2))
1290 pv_set_to_constant (&data->gpr[r1], i2);
1292 /* LGHI r1, i2 --- load halfword immediate (64-bit version) */
1293 else if (word_size == 8
1294 && is_ri (insn, op1_lghi, op2_lghi, &r1, &i2))
1295 pv_set_to_constant (&data->gpr[r1], i2);
1297 /* LR r1, r2 --- load from register */
1298 else if (word_size == 4
1299 && is_rr (insn, op_lr, &r1, &r2))
1300 data->gpr[r1] = data->gpr[r2];
1302 /* LGR r1, r2 --- load from register (64-bit version) */
1303 else if (word_size == 8
1304 && is_rre (insn, op_lgr, &r1, &r2))
1305 data->gpr[r1] = data->gpr[r2];
1307 /* L r1, d2(x2, b2) --- load */
1308 else if (word_size == 4
1309 && is_rx (insn, op_l, &r1, &d2, &x2, &b2))
1311 struct prologue_value addr;
1313 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1314 s390_load (&addr, 4, &data->gpr[r1], data);
1317 /* LY r1, d2(x2, b2) --- load (long-displacement version) */
1318 else if (word_size == 4
1319 && is_rxy (insn, op1_ly, op2_ly, &r1, &d2, &x2, &b2))
1321 struct prologue_value addr;
1323 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1324 s390_load (&addr, 4, &data->gpr[r1], data);
1327 /* LG r1, d2(x2, b2) --- load (64-bit version) */
1328 else if (word_size == 8
1329 && is_rxy (insn, op1_lg, op2_lg, &r1, &d2, &x2, &b2))
1331 struct prologue_value addr;
1333 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1334 s390_load (&addr, 8, &data->gpr[r1], data);
1337 /* ST r1, d2(x2, b2) --- store */
1338 else if (word_size == 4
1339 && is_rx (insn, op_st, &r1, &d2, &x2, &b2))
1341 struct prologue_value addr;
1343 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1344 s390_store (&addr, 4, &data->gpr[r1], data);
1347 /* STY r1, d2(x2, b2) --- store (long-displacement version) */
1348 else if (word_size == 4
1349 && is_rxy (insn, op1_sty, op2_sty, &r1, &d2, &x2, &b2))
1351 struct prologue_value addr;
1353 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1354 s390_store (&addr, 4, &data->gpr[r1], data);
1357 /* STG r1, d2(x2, b2) --- store (64-bit version) */
1358 else if (word_size == 8
1359 && is_rxy (insn, op1_stg, op2_stg, &r1, &d2, &x2, &b2))
1361 struct prologue_value addr;
1363 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1364 s390_store (&addr, 8, &data->gpr[r1], data);
1367 /* STD r1, d2(x2,b2) --- store floating-point register */
1368 else if (is_rx (insn, op_std, &r1, &d2, &x2, &b2))
1370 struct prologue_value addr;
1372 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1373 s390_store (&addr, 8, &data->fpr[r1], data);
1376 /* STM r1, r3, d2(b2) --- store multiple */
1377 else if (word_size == 4
1378 && is_rs (insn, op_stm, &r1, &r3, &d2, &b2))
1382 struct prologue_value addr;
1384 for (regnum = r1, offset = 0;
1386 regnum++, offset += 4)
1388 compute_x_addr (&addr, data->gpr, d2 + offset, 0, b2);
1389 s390_store (&addr, 4, &data->gpr[regnum], data);
1393 /* STMY r1, r3, d2(b2) --- store multiple (long-displacement version) */
1394 else if (word_size == 4
1395 && is_rsy (insn, op1_stmy, op2_stmy, &r1, &r3, &d2, &b2))
1399 struct prologue_value addr;
1401 for (regnum = r1, offset = 0;
1403 regnum++, offset += 4)
1405 compute_x_addr (&addr, data->gpr, d2 + offset, 0, b2);
1406 s390_store (&addr, 4, &data->gpr[regnum], data);
1410 /* STMG r1, r3, d2(b2) --- store multiple (64-bit version) */
1411 else if (word_size == 8
1412 && is_rsy (insn, op1_stmg, op2_stmg, &r1, &r3, &d2, &b2))
1416 struct prologue_value addr;
1418 for (regnum = r1, offset = 0;
1420 regnum++, offset += 8)
1422 compute_x_addr (&addr, data->gpr, d2 + offset, 0, b2);
1423 s390_store (&addr, 8, &data->gpr[regnum], data);
1427 /* AHI r1, i2 --- add halfword immediate */
1428 else if (word_size == 4
1429 && is_ri (insn, op1_ahi, op2_ahi, &r1, &i2))
1430 pv_add_constant (&data->gpr[r1], i2);
1432 /* AGHI r1, i2 --- add halfword immediate (64-bit version) */
1433 else if (word_size == 8
1434 && is_ri (insn, op1_aghi, op2_aghi, &r1, &i2))
1435 pv_add_constant (&data->gpr[r1], i2);
1437 /* AR r1, r2 -- add register */
1438 else if (word_size == 4
1439 && is_rr (insn, op_ar, &r1, &r2))
1440 pv_add (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1442 /* AGR r1, r2 -- add register (64-bit version) */
1443 else if (word_size == 8
1444 && is_rre (insn, op_agr, &r1, &r2))
1445 pv_add (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1447 /* A r1, d2(x2, b2) -- add */
1448 else if (word_size == 4
1449 && is_rx (insn, op_a, &r1, &d2, &x2, &b2))
1451 struct prologue_value addr;
1452 struct prologue_value value;
1454 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1455 s390_load (&addr, 4, &value, data);
1457 pv_add (&data->gpr[r1], &data->gpr[r1], &value);
1460 /* AY r1, d2(x2, b2) -- add (long-displacement version) */
1461 else if (word_size == 4
1462 && is_rxy (insn, op1_ay, op2_ay, &r1, &d2, &x2, &b2))
1464 struct prologue_value addr;
1465 struct prologue_value value;
1467 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1468 s390_load (&addr, 4, &value, data);
1470 pv_add (&data->gpr[r1], &data->gpr[r1], &value);
1473 /* AG r1, d2(x2, b2) -- add (64-bit version) */
1474 else if (word_size == 8
1475 && is_rxy (insn, op1_ag, op2_ag, &r1, &d2, &x2, &b2))
1477 struct prologue_value addr;
1478 struct prologue_value value;
1480 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1481 s390_load (&addr, 8, &value, data);
1483 pv_add (&data->gpr[r1], &data->gpr[r1], &value);
1486 /* SR r1, r2 -- subtract register */
1487 else if (word_size == 4
1488 && is_rr (insn, op_sr, &r1, &r2))
1489 pv_subtract (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1491 /* SGR r1, r2 -- subtract register (64-bit version) */
1492 else if (word_size == 8
1493 && is_rre (insn, op_sgr, &r1, &r2))
1494 pv_subtract (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1496 /* S r1, d2(x2, b2) -- subtract */
1497 else if (word_size == 4
1498 && is_rx (insn, op_s, &r1, &d2, &x2, &b2))
1500 struct prologue_value addr;
1501 struct prologue_value value;
1503 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1504 s390_load (&addr, 4, &value, data);
1506 pv_subtract (&data->gpr[r1], &data->gpr[r1], &value);
1509 /* SY r1, d2(x2, b2) -- subtract (long-displacement version) */
1510 else if (word_size == 4
1511 && is_rxy (insn, op1_sy, op2_sy, &r1, &d2, &x2, &b2))
1513 struct prologue_value addr;
1514 struct prologue_value value;
1516 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1517 s390_load (&addr, 4, &value, data);
1519 pv_subtract (&data->gpr[r1], &data->gpr[r1], &value);
1522 /* SG r1, d2(x2, b2) -- subtract (64-bit version) */
1523 else if (word_size == 8
1524 && is_rxy (insn, op1_sg, op2_sg, &r1, &d2, &x2, &b2))
1526 struct prologue_value addr;
1527 struct prologue_value value;
1529 compute_x_addr (&addr, data->gpr, d2, x2, b2);
1530 s390_load (&addr, 8, &value, data);
1532 pv_subtract (&data->gpr[r1], &data->gpr[r1], &value);
1535 /* NR r1, r2 --- logical and */
1536 else if (word_size == 4
1537 && is_rr (insn, op_nr, &r1, &r2))
1538 pv_logical_and (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1540 /* NGR r1, r2 >--- logical and (64-bit version) */
1541 else if (word_size == 8
1542 && is_rre (insn, op_ngr, &r1, &r2))
1543 pv_logical_and (&data->gpr[r1], &data->gpr[r1], &data->gpr[r2]);
1545 /* LA r1, d2(x2, b2) --- load address */
1546 else if (is_rx (insn, op_la, &r1, &d2, &x2, &b2))
1547 compute_x_addr (&data->gpr[r1], data->gpr, d2, x2, b2);
1549 /* LAY r1, d2(x2, b2) --- load address (long-displacement version) */
1550 else if (is_rxy (insn, op1_lay, op2_lay, &r1, &d2, &x2, &b2))
1551 compute_x_addr (&data->gpr[r1], data->gpr, d2, x2, b2);
1553 /* LARL r1, i2 --- load address relative long */
1554 else if (is_ril (insn, op1_larl, op2_larl, &r1, &i2))
1555 pv_set_to_constant (&data->gpr[r1], pc + i2 * 2);
1557 /* BASR r1, 0 --- branch and save
1558 Since r2 is zero, this saves the PC in r1, but doesn't branch. */
1559 else if (is_rr (insn, op_basr, &r1, &r2)
1561 pv_set_to_constant (&data->gpr[r1], next_pc);
1563 /* BRAS r1, i2 --- branch relative and save */
1564 else if (is_ri (insn, op1_bras, op2_bras, &r1, &i2))
1566 pv_set_to_constant (&data->gpr[r1], next_pc);
1567 next_pc = pc + i2 * 2;
1569 /* We'd better not interpret any backward branches. We'll
1575 /* Terminate search when hitting any other branch instruction. */
1576 else if (is_rr (insn, op_basr, &r1, &r2)
1577 || is_rx (insn, op_bas, &r1, &d2, &x2, &b2)
1578 || is_rr (insn, op_bcr, &r1, &r2)
1579 || is_rx (insn, op_bc, &r1, &d2, &x2, &b2)
1580 || is_ri (insn, op1_brc, op2_brc, &r1, &i2)
1581 || is_ril (insn, op1_brcl, op2_brcl, &r1, &i2)
1582 || is_ril (insn, op1_brasl, op2_brasl, &r2, &i2))
1586 /* An instruction we don't know how to simulate. The only
1587 safe thing to do would be to set every value we're tracking
1588 to 'unknown'. Instead, we'll be optimistic: we assume that
1589 we *can* interpret every instruction that the compiler uses
1590 to manipulate any of the data we're interested in here --
1591 then we can just ignore anything else. */
1594 /* Record the address after the last instruction that changed
1595 the FP, SP, or backlink. Ignore instructions that changed
1596 them back to their original values --- those are probably
1597 restore instructions. (The back chain is never restored,
1600 struct prologue_value *sp = &data->gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1601 struct prologue_value *fp = &data->gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
1603 if ((! pv_is_identical (&pre_insn_sp, sp)
1604 && ! pv_is_register (sp, S390_SP_REGNUM, 0))
1605 || (! pv_is_identical (&pre_insn_fp, fp)
1606 && ! pv_is_register (fp, S390_FRAME_REGNUM, 0))
1607 || pre_insn_back_chain_saved_p != data->back_chain_saved_p)
1615 /* Advance PC across any function entry prologue instructions to reach
1616 some "real" code. */
1618 s390_skip_prologue (CORE_ADDR pc)
1620 struct s390_prologue_data data;
1622 skip_pc = s390_analyze_prologue (current_gdbarch, pc, (CORE_ADDR)-1, &data);
1623 return skip_pc ? skip_pc : pc;
1626 /* Return true if we are in the functin's epilogue, i.e. after the
1627 instruction that destroyed the function's stack frame. */
1629 s390_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
1631 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1633 /* In frameless functions, there's not frame to destroy and thus
1634 we don't care about the epilogue.
1636 In functions with frame, the epilogue sequence is a pair of
1637 a LM-type instruction that restores (amongst others) the
1638 return register %r14 and the stack pointer %r15, followed
1639 by a branch 'br %r14' --or equivalent-- that effects the
1642 In that situation, this function needs to return 'true' in
1643 exactly one case: when pc points to that branch instruction.
1645 Thus we try to disassemble the one instructions immediately
1646 preceeding pc and check whether it is an LM-type instruction
1647 modifying the stack pointer.
1649 Note that disassembling backwards is not reliable, so there
1650 is a slight chance of false positives here ... */
1653 unsigned int r1, r3, b2;
1657 && !deprecated_read_memory_nobpt (pc - 4, insn, 4)
1658 && is_rs (insn, op_lm, &r1, &r3, &d2, &b2)
1659 && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
1663 && !deprecated_read_memory_nobpt (pc - 6, insn, 6)
1664 && is_rsy (insn, op1_lmy, op2_lmy, &r1, &r3, &d2, &b2)
1665 && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
1669 && !deprecated_read_memory_nobpt (pc - 6, insn, 6)
1670 && is_rsy (insn, op1_lmg, op2_lmg, &r1, &r3, &d2, &b2)
1671 && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
1678 /* Normal stack frames. */
1680 struct s390_unwind_cache {
1683 CORE_ADDR frame_base;
1684 CORE_ADDR local_base;
1686 struct trad_frame_saved_reg *saved_regs;
1690 s390_prologue_frame_unwind_cache (struct frame_info *next_frame,
1691 struct s390_unwind_cache *info)
1693 struct gdbarch *gdbarch = get_frame_arch (next_frame);
1694 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1695 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1696 struct s390_prologue_data data;
1697 struct prologue_value *fp = &data.gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
1698 struct prologue_value *sp = &data.gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1708 /* Try to find the function start address. If we can't find it, we don't
1709 bother searching for it -- with modern compilers this would be mostly
1710 pointless anyway. Trust that we'll either have valid DWARF-2 CFI data
1711 or else a valid backchain ... */
1712 func = frame_func_unwind (next_frame);
1716 /* Try to analyze the prologue. */
1717 result = s390_analyze_prologue (gdbarch, func,
1718 frame_pc_unwind (next_frame), &data);
1722 /* If this was successful, we should have found the instruction that
1723 sets the stack pointer register to the previous value of the stack
1724 pointer minus the frame size. */
1725 if (sp->kind != pv_register || sp->reg != S390_SP_REGNUM)
1728 /* A frame size of zero at this point can mean either a real
1729 frameless function, or else a failure to find the prologue.
1730 Perform some sanity checks to verify we really have a
1731 frameless function. */
1734 /* If the next frame is a NORMAL_FRAME, this frame *cannot* have frame
1735 size zero. This is only possible if the next frame is a sentinel
1736 frame, a dummy frame, or a signal trampoline frame. */
1737 /* FIXME: cagney/2004-05-01: This sanity check shouldn't be
1738 needed, instead the code should simpliy rely on its
1740 if (get_frame_type (next_frame) == NORMAL_FRAME)
1743 /* If we really have a frameless function, %r14 must be valid
1744 -- in particular, it must point to a different function. */
1745 reg = frame_unwind_register_unsigned (next_frame, S390_RETADDR_REGNUM);
1746 reg = gdbarch_addr_bits_remove (gdbarch, reg) - 1;
1747 if (get_pc_function_start (reg) == func)
1749 /* However, there is one case where it *is* valid for %r14
1750 to point to the same function -- if this is a recursive
1751 call, and we have stopped in the prologue *before* the
1752 stack frame was allocated.
1754 Recognize this case by looking ahead a bit ... */
1756 struct s390_prologue_data data2;
1757 struct prologue_value *sp = &data2.gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1759 if (!(s390_analyze_prologue (gdbarch, func, (CORE_ADDR)-1, &data2)
1760 && sp->kind == pv_register
1761 && sp->reg == S390_SP_REGNUM
1768 /* OK, we've found valid prologue data. */
1771 /* If the frame pointer originally also holds the same value
1772 as the stack pointer, we're probably using it. If it holds
1773 some other value -- even a constant offset -- it is most
1774 likely used as temp register. */
1775 if (pv_is_identical (sp, fp))
1776 frame_pointer = S390_FRAME_REGNUM;
1778 frame_pointer = S390_SP_REGNUM;
1780 /* If we've detected a function with stack frame, we'll still have to
1781 treat it as frameless if we're currently within the function epilog
1782 code at a point where the frame pointer has already been restored.
1783 This can only happen in an innermost frame. */
1784 /* FIXME: cagney/2004-05-01: This sanity check shouldn't be needed,
1785 instead the code should simpliy rely on its analysis. */
1786 if (size > 0 && get_frame_type (next_frame) != NORMAL_FRAME)
1788 /* See the comment in s390_in_function_epilogue_p on why this is
1789 not completely reliable ... */
1790 if (s390_in_function_epilogue_p (gdbarch, frame_pc_unwind (next_frame)))
1792 memset (&data, 0, sizeof (data));
1794 frame_pointer = S390_SP_REGNUM;
1798 /* Once we know the frame register and the frame size, we can unwind
1799 the current value of the frame register from the next frame, and
1800 add back the frame size to arrive that the previous frame's
1801 stack pointer value. */
1802 prev_sp = frame_unwind_register_unsigned (next_frame, frame_pointer) + size;
1803 cfa = prev_sp + 16*word_size + 32;
1805 /* Record the addresses of all register spill slots the prologue parser
1806 has recognized. Consider only registers defined as call-saved by the
1807 ABI; for call-clobbered registers the parser may have recognized
1810 for (i = 6; i <= 15; i++)
1811 if (data.gpr_slot[i] != 0)
1812 info->saved_regs[S390_R0_REGNUM + i].addr = cfa - data.gpr_slot[i];
1816 case ABI_LINUX_S390:
1817 if (data.fpr_slot[4] != 0)
1818 info->saved_regs[S390_F4_REGNUM].addr = cfa - data.fpr_slot[4];
1819 if (data.fpr_slot[6] != 0)
1820 info->saved_regs[S390_F6_REGNUM].addr = cfa - data.fpr_slot[6];
1823 case ABI_LINUX_ZSERIES:
1824 for (i = 8; i <= 15; i++)
1825 if (data.fpr_slot[i] != 0)
1826 info->saved_regs[S390_F0_REGNUM + i].addr = cfa - data.fpr_slot[i];
1830 /* Function return will set PC to %r14. */
1831 info->saved_regs[S390_PC_REGNUM] = info->saved_regs[S390_RETADDR_REGNUM];
1833 /* In frameless functions, we unwind simply by moving the return
1834 address to the PC. However, if we actually stored to the
1835 save area, use that -- we might only think the function frameless
1836 because we're in the middle of the prologue ... */
1838 && !trad_frame_addr_p (info->saved_regs, S390_PC_REGNUM))
1840 info->saved_regs[S390_PC_REGNUM].realreg = S390_RETADDR_REGNUM;
1843 /* Another sanity check: unless this is a frameless function,
1844 we should have found spill slots for SP and PC.
1845 If not, we cannot unwind further -- this happens e.g. in
1846 libc's thread_start routine. */
1849 if (!trad_frame_addr_p (info->saved_regs, S390_SP_REGNUM)
1850 || !trad_frame_addr_p (info->saved_regs, S390_PC_REGNUM))
1854 /* We use the current value of the frame register as local_base,
1855 and the top of the register save area as frame_base. */
1858 info->frame_base = prev_sp + 16*word_size + 32;
1859 info->local_base = prev_sp - size;
1867 s390_backchain_frame_unwind_cache (struct frame_info *next_frame,
1868 struct s390_unwind_cache *info)
1870 struct gdbarch *gdbarch = get_frame_arch (next_frame);
1871 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1872 CORE_ADDR backchain;
1876 /* Get the backchain. */
1877 reg = frame_unwind_register_unsigned (next_frame, S390_SP_REGNUM);
1878 backchain = read_memory_unsigned_integer (reg, word_size);
1880 /* A zero backchain terminates the frame chain. As additional
1881 sanity check, let's verify that the spill slot for SP in the
1882 save area pointed to by the backchain in fact links back to
1885 && safe_read_memory_integer (backchain + 15*word_size, word_size, &sp)
1886 && (CORE_ADDR)sp == backchain)
1888 /* We don't know which registers were saved, but it will have
1889 to be at least %r14 and %r15. This will allow us to continue
1890 unwinding, but other prev-frame registers may be incorrect ... */
1891 info->saved_regs[S390_SP_REGNUM].addr = backchain + 15*word_size;
1892 info->saved_regs[S390_RETADDR_REGNUM].addr = backchain + 14*word_size;
1894 /* Function return will set PC to %r14. */
1895 info->saved_regs[S390_PC_REGNUM] = info->saved_regs[S390_RETADDR_REGNUM];
1897 /* We use the current value of the frame register as local_base,
1898 and the top of the register save area as frame_base. */
1899 info->frame_base = backchain + 16*word_size + 32;
1900 info->local_base = reg;
1903 info->func = frame_pc_unwind (next_frame);
1906 static struct s390_unwind_cache *
1907 s390_frame_unwind_cache (struct frame_info *next_frame,
1908 void **this_prologue_cache)
1910 struct s390_unwind_cache *info;
1911 if (*this_prologue_cache)
1912 return *this_prologue_cache;
1914 info = FRAME_OBSTACK_ZALLOC (struct s390_unwind_cache);
1915 *this_prologue_cache = info;
1916 info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
1918 info->frame_base = -1;
1919 info->local_base = -1;
1921 /* Try to use prologue analysis to fill the unwind cache.
1922 If this fails, fall back to reading the stack backchain. */
1923 if (!s390_prologue_frame_unwind_cache (next_frame, info))
1924 s390_backchain_frame_unwind_cache (next_frame, info);
1930 s390_frame_this_id (struct frame_info *next_frame,
1931 void **this_prologue_cache,
1932 struct frame_id *this_id)
1934 struct s390_unwind_cache *info
1935 = s390_frame_unwind_cache (next_frame, this_prologue_cache);
1937 if (info->frame_base == -1)
1940 *this_id = frame_id_build (info->frame_base, info->func);
1944 s390_frame_prev_register (struct frame_info *next_frame,
1945 void **this_prologue_cache,
1946 int regnum, int *optimizedp,
1947 enum lval_type *lvalp, CORE_ADDR *addrp,
1948 int *realnump, gdb_byte *bufferp)
1950 struct s390_unwind_cache *info
1951 = s390_frame_unwind_cache (next_frame, this_prologue_cache);
1952 trad_frame_get_prev_register (next_frame, info->saved_regs, regnum,
1953 optimizedp, lvalp, addrp, realnump, bufferp);
1956 static const struct frame_unwind s390_frame_unwind = {
1959 s390_frame_prev_register
1962 static const struct frame_unwind *
1963 s390_frame_sniffer (struct frame_info *next_frame)
1965 return &s390_frame_unwind;
1969 /* Code stubs and their stack frames. For things like PLTs and NULL
1970 function calls (where there is no true frame and the return address
1971 is in the RETADDR register). */
1973 struct s390_stub_unwind_cache
1975 CORE_ADDR frame_base;
1976 struct trad_frame_saved_reg *saved_regs;
1979 static struct s390_stub_unwind_cache *
1980 s390_stub_frame_unwind_cache (struct frame_info *next_frame,
1981 void **this_prologue_cache)
1983 struct gdbarch *gdbarch = get_frame_arch (next_frame);
1984 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1985 struct s390_stub_unwind_cache *info;
1988 if (*this_prologue_cache)
1989 return *this_prologue_cache;
1991 info = FRAME_OBSTACK_ZALLOC (struct s390_stub_unwind_cache);
1992 *this_prologue_cache = info;
1993 info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
1995 /* The return address is in register %r14. */
1996 info->saved_regs[S390_PC_REGNUM].realreg = S390_RETADDR_REGNUM;
1998 /* Retrieve stack pointer and determine our frame base. */
1999 reg = frame_unwind_register_unsigned (next_frame, S390_SP_REGNUM);
2000 info->frame_base = reg + 16*word_size + 32;
2006 s390_stub_frame_this_id (struct frame_info *next_frame,
2007 void **this_prologue_cache,
2008 struct frame_id *this_id)
2010 struct s390_stub_unwind_cache *info
2011 = s390_stub_frame_unwind_cache (next_frame, this_prologue_cache);
2012 *this_id = frame_id_build (info->frame_base, frame_pc_unwind (next_frame));
2016 s390_stub_frame_prev_register (struct frame_info *next_frame,
2017 void **this_prologue_cache,
2018 int regnum, int *optimizedp,
2019 enum lval_type *lvalp, CORE_ADDR *addrp,
2020 int *realnump, gdb_byte *bufferp)
2022 struct s390_stub_unwind_cache *info
2023 = s390_stub_frame_unwind_cache (next_frame, this_prologue_cache);
2024 trad_frame_get_prev_register (next_frame, info->saved_regs, regnum,
2025 optimizedp, lvalp, addrp, realnump, bufferp);
2028 static const struct frame_unwind s390_stub_frame_unwind = {
2030 s390_stub_frame_this_id,
2031 s390_stub_frame_prev_register
2034 static const struct frame_unwind *
2035 s390_stub_frame_sniffer (struct frame_info *next_frame)
2037 CORE_ADDR pc = frame_pc_unwind (next_frame);
2038 bfd_byte insn[S390_MAX_INSTR_SIZE];
2040 /* If the current PC points to non-readable memory, we assume we
2041 have trapped due to an invalid function pointer call. We handle
2042 the non-existing current function like a PLT stub. */
2043 if (in_plt_section (pc, NULL)
2044 || s390_readinstruction (insn, pc) < 0)
2045 return &s390_stub_frame_unwind;
2050 /* Signal trampoline stack frames. */
2052 struct s390_sigtramp_unwind_cache {
2053 CORE_ADDR frame_base;
2054 struct trad_frame_saved_reg *saved_regs;
2057 static struct s390_sigtramp_unwind_cache *
2058 s390_sigtramp_frame_unwind_cache (struct frame_info *next_frame,
2059 void **this_prologue_cache)
2061 struct gdbarch *gdbarch = get_frame_arch (next_frame);
2062 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2063 struct s390_sigtramp_unwind_cache *info;
2064 ULONGEST this_sp, prev_sp;
2065 CORE_ADDR next_ra, next_cfa, sigreg_ptr;
2068 if (*this_prologue_cache)
2069 return *this_prologue_cache;
2071 info = FRAME_OBSTACK_ZALLOC (struct s390_sigtramp_unwind_cache);
2072 *this_prologue_cache = info;
2073 info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
2075 this_sp = frame_unwind_register_unsigned (next_frame, S390_SP_REGNUM);
2076 next_ra = frame_pc_unwind (next_frame);
2077 next_cfa = this_sp + 16*word_size + 32;
2079 /* New-style RT frame:
2080 retcode + alignment (8 bytes)
2082 ucontext (contains sigregs at offset 5 words) */
2083 if (next_ra == next_cfa)
2085 sigreg_ptr = next_cfa + 8 + 128 + align_up (5*word_size, 8);
2088 /* Old-style RT frame and all non-RT frames:
2089 old signal mask (8 bytes)
2090 pointer to sigregs */
2093 sigreg_ptr = read_memory_unsigned_integer (next_cfa + 8, word_size);
2096 /* The sigregs structure looks like this:
2105 /* Let's ignore the PSW mask, it will not be restored anyway. */
2106 sigreg_ptr += word_size;
2108 /* Next comes the PSW address. */
2109 info->saved_regs[S390_PC_REGNUM].addr = sigreg_ptr;
2110 sigreg_ptr += word_size;
2112 /* Then the GPRs. */
2113 for (i = 0; i < 16; i++)
2115 info->saved_regs[S390_R0_REGNUM + i].addr = sigreg_ptr;
2116 sigreg_ptr += word_size;
2119 /* Then the ACRs. */
2120 for (i = 0; i < 16; i++)
2122 info->saved_regs[S390_A0_REGNUM + i].addr = sigreg_ptr;
2126 /* The floating-point control word. */
2127 info->saved_regs[S390_FPC_REGNUM].addr = sigreg_ptr;
2130 /* And finally the FPRs. */
2131 for (i = 0; i < 16; i++)
2133 info->saved_regs[S390_F0_REGNUM + i].addr = sigreg_ptr;
2137 /* Restore the previous frame's SP. */
2138 prev_sp = read_memory_unsigned_integer (
2139 info->saved_regs[S390_SP_REGNUM].addr,
2142 /* Determine our frame base. */
2143 info->frame_base = prev_sp + 16*word_size + 32;
2149 s390_sigtramp_frame_this_id (struct frame_info *next_frame,
2150 void **this_prologue_cache,
2151 struct frame_id *this_id)
2153 struct s390_sigtramp_unwind_cache *info
2154 = s390_sigtramp_frame_unwind_cache (next_frame, this_prologue_cache);
2155 *this_id = frame_id_build (info->frame_base, frame_pc_unwind (next_frame));
2159 s390_sigtramp_frame_prev_register (struct frame_info *next_frame,
2160 void **this_prologue_cache,
2161 int regnum, int *optimizedp,
2162 enum lval_type *lvalp, CORE_ADDR *addrp,
2163 int *realnump, gdb_byte *bufferp)
2165 struct s390_sigtramp_unwind_cache *info
2166 = s390_sigtramp_frame_unwind_cache (next_frame, this_prologue_cache);
2167 trad_frame_get_prev_register (next_frame, info->saved_regs, regnum,
2168 optimizedp, lvalp, addrp, realnump, bufferp);
2171 static const struct frame_unwind s390_sigtramp_frame_unwind = {
2173 s390_sigtramp_frame_this_id,
2174 s390_sigtramp_frame_prev_register
2177 static const struct frame_unwind *
2178 s390_sigtramp_frame_sniffer (struct frame_info *next_frame)
2180 CORE_ADDR pc = frame_pc_unwind (next_frame);
2181 bfd_byte sigreturn[2];
2183 if (deprecated_read_memory_nobpt (pc, sigreturn, 2))
2186 if (sigreturn[0] != 0x0a /* svc */)
2189 if (sigreturn[1] != 119 /* sigreturn */
2190 && sigreturn[1] != 173 /* rt_sigreturn */)
2193 return &s390_sigtramp_frame_unwind;
2197 /* Frame base handling. */
2200 s390_frame_base_address (struct frame_info *next_frame, void **this_cache)
2202 struct s390_unwind_cache *info
2203 = s390_frame_unwind_cache (next_frame, this_cache);
2204 return info->frame_base;
2208 s390_local_base_address (struct frame_info *next_frame, void **this_cache)
2210 struct s390_unwind_cache *info
2211 = s390_frame_unwind_cache (next_frame, this_cache);
2212 return info->local_base;
2215 static const struct frame_base s390_frame_base = {
2217 s390_frame_base_address,
2218 s390_local_base_address,
2219 s390_local_base_address
2223 s390_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
2226 pc = frame_unwind_register_unsigned (next_frame, S390_PC_REGNUM);
2227 return gdbarch_addr_bits_remove (gdbarch, pc);
2231 s390_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
2234 sp = frame_unwind_register_unsigned (next_frame, S390_SP_REGNUM);
2235 return gdbarch_addr_bits_remove (gdbarch, sp);
2239 /* DWARF-2 frame support. */
2242 s390_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
2243 struct dwarf2_frame_state_reg *reg)
2245 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2249 case ABI_LINUX_S390:
2250 /* Call-saved registers. */
2251 if ((regnum >= S390_R6_REGNUM && regnum <= S390_R15_REGNUM)
2252 || regnum == S390_F4_REGNUM
2253 || regnum == S390_F6_REGNUM)
2254 reg->how = DWARF2_FRAME_REG_SAME_VALUE;
2256 /* Call-clobbered registers. */
2257 else if ((regnum >= S390_R0_REGNUM && regnum <= S390_R5_REGNUM)
2258 || (regnum >= S390_F0_REGNUM && regnum <= S390_F15_REGNUM
2259 && regnum != S390_F4_REGNUM && regnum != S390_F6_REGNUM))
2260 reg->how = DWARF2_FRAME_REG_UNDEFINED;
2262 /* The return address column. */
2263 else if (regnum == S390_PC_REGNUM)
2264 reg->how = DWARF2_FRAME_REG_RA;
2267 case ABI_LINUX_ZSERIES:
2268 /* Call-saved registers. */
2269 if ((regnum >= S390_R6_REGNUM && regnum <= S390_R15_REGNUM)
2270 || (regnum >= S390_F8_REGNUM && regnum <= S390_F15_REGNUM))
2271 reg->how = DWARF2_FRAME_REG_SAME_VALUE;
2273 /* Call-clobbered registers. */
2274 else if ((regnum >= S390_R0_REGNUM && regnum <= S390_R5_REGNUM)
2275 || (regnum >= S390_F0_REGNUM && regnum <= S390_F7_REGNUM))
2276 reg->how = DWARF2_FRAME_REG_UNDEFINED;
2278 /* The return address column. */
2279 else if (regnum == S390_PC_REGNUM)
2280 reg->how = DWARF2_FRAME_REG_RA;
2286 /* Dummy function calls. */
2288 /* Return non-zero if TYPE is an integer-like type, zero otherwise.
2289 "Integer-like" types are those that should be passed the way
2290 integers are: integers, enums, ranges, characters, and booleans. */
2292 is_integer_like (struct type *type)
2294 enum type_code code = TYPE_CODE (type);
2296 return (code == TYPE_CODE_INT
2297 || code == TYPE_CODE_ENUM
2298 || code == TYPE_CODE_RANGE
2299 || code == TYPE_CODE_CHAR
2300 || code == TYPE_CODE_BOOL);
2303 /* Return non-zero if TYPE is a pointer-like type, zero otherwise.
2304 "Pointer-like" types are those that should be passed the way
2305 pointers are: pointers and references. */
2307 is_pointer_like (struct type *type)
2309 enum type_code code = TYPE_CODE (type);
2311 return (code == TYPE_CODE_PTR
2312 || code == TYPE_CODE_REF);
2316 /* Return non-zero if TYPE is a `float singleton' or `double
2317 singleton', zero otherwise.
2319 A `T singleton' is a struct type with one member, whose type is
2320 either T or a `T singleton'. So, the following are all float
2324 struct { struct { float x; } x; };
2325 struct { struct { struct { float x; } x; } x; };
2329 All such structures are passed as if they were floats or doubles,
2330 as the (revised) ABI says. */
2332 is_float_singleton (struct type *type)
2334 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
2336 struct type *singleton_type = TYPE_FIELD_TYPE (type, 0);
2337 CHECK_TYPEDEF (singleton_type);
2339 return (TYPE_CODE (singleton_type) == TYPE_CODE_FLT
2340 || is_float_singleton (singleton_type));
2347 /* Return non-zero if TYPE is a struct-like type, zero otherwise.
2348 "Struct-like" types are those that should be passed as structs are:
2351 As an odd quirk, not mentioned in the ABI, GCC passes float and
2352 double singletons as if they were a plain float, double, etc. (The
2353 corresponding union types are handled normally.) So we exclude
2354 those types here. *shrug* */
2356 is_struct_like (struct type *type)
2358 enum type_code code = TYPE_CODE (type);
2360 return (code == TYPE_CODE_UNION
2361 || (code == TYPE_CODE_STRUCT && ! is_float_singleton (type)));
2365 /* Return non-zero if TYPE is a float-like type, zero otherwise.
2366 "Float-like" types are those that should be passed as
2367 floating-point values are.
2369 You'd think this would just be floats, doubles, long doubles, etc.
2370 But as an odd quirk, not mentioned in the ABI, GCC passes float and
2371 double singletons as if they were a plain float, double, etc. (The
2372 corresponding union types are handled normally.) So we include
2373 those types here. *shrug* */
2375 is_float_like (struct type *type)
2377 return (TYPE_CODE (type) == TYPE_CODE_FLT
2378 || is_float_singleton (type));
2383 is_power_of_two (unsigned int n)
2385 return ((n & (n - 1)) == 0);
2388 /* Return non-zero if TYPE should be passed as a pointer to a copy,
2391 s390_function_arg_pass_by_reference (struct type *type)
2393 unsigned length = TYPE_LENGTH (type);
2397 /* FIXME: All complex and vector types are also returned by reference. */
2398 return is_struct_like (type) && !is_power_of_two (length);
2401 /* Return non-zero if TYPE should be passed in a float register
2404 s390_function_arg_float (struct type *type)
2406 unsigned length = TYPE_LENGTH (type);
2410 return is_float_like (type);
2413 /* Return non-zero if TYPE should be passed in an integer register
2414 (or a pair of integer registers) if possible. */
2416 s390_function_arg_integer (struct type *type)
2418 unsigned length = TYPE_LENGTH (type);
2422 return is_integer_like (type)
2423 || is_pointer_like (type)
2424 || (is_struct_like (type) && is_power_of_two (length));
2427 /* Return ARG, a `SIMPLE_ARG', sign-extended or zero-extended to a full
2428 word as required for the ABI. */
2430 extend_simple_arg (struct value *arg)
2432 struct type *type = value_type (arg);
2434 /* Even structs get passed in the least significant bits of the
2435 register / memory word. It's not really right to extract them as
2436 an integer, but it does take care of the extension. */
2437 if (TYPE_UNSIGNED (type))
2438 return extract_unsigned_integer (value_contents (arg),
2439 TYPE_LENGTH (type));
2441 return extract_signed_integer (value_contents (arg),
2442 TYPE_LENGTH (type));
2446 /* Return the alignment required by TYPE. */
2448 alignment_of (struct type *type)
2452 if (is_integer_like (type)
2453 || is_pointer_like (type)
2454 || TYPE_CODE (type) == TYPE_CODE_FLT)
2455 alignment = TYPE_LENGTH (type);
2456 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2457 || TYPE_CODE (type) == TYPE_CODE_UNION)
2462 for (i = 0; i < TYPE_NFIELDS (type); i++)
2464 int field_alignment = alignment_of (TYPE_FIELD_TYPE (type, i));
2466 if (field_alignment > alignment)
2467 alignment = field_alignment;
2473 /* Check that everything we ever return is a power of two. Lots of
2474 code doesn't want to deal with aligning things to arbitrary
2476 gdb_assert ((alignment & (alignment - 1)) == 0);
2482 /* Put the actual parameter values pointed to by ARGS[0..NARGS-1] in
2483 place to be passed to a function, as specified by the "GNU/Linux
2484 for S/390 ELF Application Binary Interface Supplement".
2486 SP is the current stack pointer. We must put arguments, links,
2487 padding, etc. whereever they belong, and return the new stack
2490 If STRUCT_RETURN is non-zero, then the function we're calling is
2491 going to return a structure by value; STRUCT_ADDR is the address of
2492 a block we've allocated for it on the stack.
2494 Our caller has taken care of any type promotions needed to satisfy
2495 prototypes or the old K&R argument-passing rules. */
2497 s390_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
2498 struct regcache *regcache, CORE_ADDR bp_addr,
2499 int nargs, struct value **args, CORE_ADDR sp,
2500 int struct_return, CORE_ADDR struct_addr)
2502 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2503 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2507 /* If the i'th argument is passed as a reference to a copy, then
2508 copy_addr[i] is the address of the copy we made. */
2509 CORE_ADDR *copy_addr = alloca (nargs * sizeof (CORE_ADDR));
2511 /* Build the reference-to-copy area. */
2512 for (i = 0; i < nargs; i++)
2514 struct value *arg = args[i];
2515 struct type *type = value_type (arg);
2516 unsigned length = TYPE_LENGTH (type);
2518 if (s390_function_arg_pass_by_reference (type))
2521 sp = align_down (sp, alignment_of (type));
2522 write_memory (sp, value_contents (arg), length);
2527 /* Reserve space for the parameter area. As a conservative
2528 simplification, we assume that everything will be passed on the
2529 stack. Since every argument larger than 8 bytes will be
2530 passed by reference, we use this simple upper bound. */
2533 /* After all that, make sure it's still aligned on an eight-byte
2535 sp = align_down (sp, 8);
2537 /* Finally, place the actual parameters, working from SP towards
2538 higher addresses. The code above is supposed to reserve enough
2543 CORE_ADDR starg = sp;
2545 /* A struct is returned using general register 2. */
2548 regcache_cooked_write_unsigned (regcache, S390_R0_REGNUM + gr,
2553 for (i = 0; i < nargs; i++)
2555 struct value *arg = args[i];
2556 struct type *type = value_type (arg);
2557 unsigned length = TYPE_LENGTH (type);
2559 if (s390_function_arg_pass_by_reference (type))
2563 regcache_cooked_write_unsigned (regcache, S390_R0_REGNUM + gr,
2569 write_memory_unsigned_integer (starg, word_size, copy_addr[i]);
2573 else if (s390_function_arg_float (type))
2575 /* The GNU/Linux for S/390 ABI uses FPRs 0 and 2 to pass arguments,
2576 the GNU/Linux for zSeries ABI uses 0, 2, 4, and 6. */
2577 if (fr <= (tdep->abi == ABI_LINUX_S390 ? 2 : 6))
2579 /* When we store a single-precision value in an FP register,
2580 it occupies the leftmost bits. */
2581 regcache_cooked_write_part (regcache, S390_F0_REGNUM + fr,
2582 0, length, value_contents (arg));
2587 /* When we store a single-precision value in a stack slot,
2588 it occupies the rightmost bits. */
2589 starg = align_up (starg + length, word_size);
2590 write_memory (starg - length, value_contents (arg), length);
2593 else if (s390_function_arg_integer (type) && length <= word_size)
2597 /* Integer arguments are always extended to word size. */
2598 regcache_cooked_write_signed (regcache, S390_R0_REGNUM + gr,
2599 extend_simple_arg (arg));
2604 /* Integer arguments are always extended to word size. */
2605 write_memory_signed_integer (starg, word_size,
2606 extend_simple_arg (arg));
2610 else if (s390_function_arg_integer (type) && length == 2*word_size)
2614 regcache_cooked_write (regcache, S390_R0_REGNUM + gr,
2615 value_contents (arg));
2616 regcache_cooked_write (regcache, S390_R0_REGNUM + gr + 1,
2617 value_contents (arg) + word_size);
2622 /* If we skipped r6 because we couldn't fit a DOUBLE_ARG
2623 in it, then don't go back and use it again later. */
2626 write_memory (starg, value_contents (arg), length);
2631 internal_error (__FILE__, __LINE__, _("unknown argument type"));
2635 /* Allocate the standard frame areas: the register save area, the
2636 word reserved for the compiler (which seems kind of meaningless),
2637 and the back chain pointer. */
2638 sp -= 16*word_size + 32;
2640 /* Store return address. */
2641 regcache_cooked_write_unsigned (regcache, S390_RETADDR_REGNUM, bp_addr);
2643 /* Store updated stack pointer. */
2644 regcache_cooked_write_unsigned (regcache, S390_SP_REGNUM, sp);
2646 /* We need to return the 'stack part' of the frame ID,
2647 which is actually the top of the register save area. */
2648 return sp + 16*word_size + 32;
2651 /* Assuming NEXT_FRAME->prev is a dummy, return the frame ID of that
2652 dummy frame. The frame ID's base needs to match the TOS value
2653 returned by push_dummy_call, and the PC match the dummy frame's
2655 static struct frame_id
2656 s390_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
2658 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2659 CORE_ADDR sp = s390_unwind_sp (gdbarch, next_frame);
2661 return frame_id_build (sp + 16*word_size + 32,
2662 frame_pc_unwind (next_frame));
2666 s390_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
2668 /* Both the 32- and 64-bit ABI's say that the stack pointer should
2669 always be aligned on an eight-byte boundary. */
2674 /* Function return value access. */
2676 static enum return_value_convention
2677 s390_return_value_convention (struct gdbarch *gdbarch, struct type *type)
2679 int length = TYPE_LENGTH (type);
2681 return RETURN_VALUE_STRUCT_CONVENTION;
2683 switch (TYPE_CODE (type))
2685 case TYPE_CODE_STRUCT:
2686 case TYPE_CODE_UNION:
2687 case TYPE_CODE_ARRAY:
2688 return RETURN_VALUE_STRUCT_CONVENTION;
2691 return RETURN_VALUE_REGISTER_CONVENTION;
2695 static enum return_value_convention
2696 s390_return_value (struct gdbarch *gdbarch, struct type *type,
2697 struct regcache *regcache, gdb_byte *out,
2700 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2701 int length = TYPE_LENGTH (type);
2702 enum return_value_convention rvc =
2703 s390_return_value_convention (gdbarch, type);
2708 case RETURN_VALUE_REGISTER_CONVENTION:
2709 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2711 /* When we store a single-precision value in an FP register,
2712 it occupies the leftmost bits. */
2713 regcache_cooked_write_part (regcache, S390_F0_REGNUM,
2716 else if (length <= word_size)
2718 /* Integer arguments are always extended to word size. */
2719 if (TYPE_UNSIGNED (type))
2720 regcache_cooked_write_unsigned (regcache, S390_R2_REGNUM,
2721 extract_unsigned_integer (in, length));
2723 regcache_cooked_write_signed (regcache, S390_R2_REGNUM,
2724 extract_signed_integer (in, length));
2726 else if (length == 2*word_size)
2728 regcache_cooked_write (regcache, S390_R2_REGNUM, in);
2729 regcache_cooked_write (regcache, S390_R3_REGNUM, in + word_size);
2732 internal_error (__FILE__, __LINE__, _("invalid return type"));
2735 case RETURN_VALUE_STRUCT_CONVENTION:
2736 error (_("Cannot set function return value."));
2744 case RETURN_VALUE_REGISTER_CONVENTION:
2745 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2747 /* When we store a single-precision value in an FP register,
2748 it occupies the leftmost bits. */
2749 regcache_cooked_read_part (regcache, S390_F0_REGNUM,
2752 else if (length <= word_size)
2754 /* Integer arguments occupy the rightmost bits. */
2755 regcache_cooked_read_part (regcache, S390_R2_REGNUM,
2756 word_size - length, length, out);
2758 else if (length == 2*word_size)
2760 regcache_cooked_read (regcache, S390_R2_REGNUM, out);
2761 regcache_cooked_read (regcache, S390_R3_REGNUM, 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 gdb_byte *
2780 s390_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
2782 static const gdb_byte 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 dwarf2_frame_set_init_reg (gdbarch, s390_dwarf2_frame_init_reg);
2957 frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);
2958 frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
2959 frame_unwind_append_sniffer (gdbarch, s390_stub_frame_sniffer);
2960 frame_unwind_append_sniffer (gdbarch, s390_sigtramp_frame_sniffer);
2961 frame_unwind_append_sniffer (gdbarch, s390_frame_sniffer);
2962 frame_base_set_default (gdbarch, &s390_frame_base);
2963 set_gdbarch_unwind_pc (gdbarch, s390_unwind_pc);
2964 set_gdbarch_unwind_sp (gdbarch, s390_unwind_sp);
2966 switch (info.bfd_arch_info->mach)
2968 case bfd_mach_s390_31:
2969 tdep->abi = ABI_LINUX_S390;
2971 tdep->gregset = &s390_gregset;
2972 tdep->sizeof_gregset = s390_sizeof_gregset;
2973 tdep->fpregset = &s390_fpregset;
2974 tdep->sizeof_fpregset = s390_sizeof_fpregset;
2976 set_gdbarch_addr_bits_remove (gdbarch, s390_addr_bits_remove);
2977 set_gdbarch_pseudo_register_read (gdbarch, s390_pseudo_register_read);
2978 set_gdbarch_pseudo_register_write (gdbarch, s390_pseudo_register_write);
2979 set_solib_svr4_fetch_link_map_offsets (gdbarch,
2980 s390_svr4_fetch_link_map_offsets);
2983 case bfd_mach_s390_64:
2984 tdep->abi = ABI_LINUX_ZSERIES;
2986 tdep->gregset = &s390x_gregset;
2987 tdep->sizeof_gregset = s390x_sizeof_gregset;
2988 tdep->fpregset = &s390_fpregset;
2989 tdep->sizeof_fpregset = s390_sizeof_fpregset;
2991 set_gdbarch_long_bit (gdbarch, 64);
2992 set_gdbarch_long_long_bit (gdbarch, 64);
2993 set_gdbarch_ptr_bit (gdbarch, 64);
2994 set_gdbarch_pseudo_register_read (gdbarch, s390x_pseudo_register_read);
2995 set_gdbarch_pseudo_register_write (gdbarch, s390x_pseudo_register_write);
2996 set_solib_svr4_fetch_link_map_offsets (gdbarch,
2997 s390x_svr4_fetch_link_map_offsets);
2998 set_gdbarch_address_class_type_flags (gdbarch,
2999 s390_address_class_type_flags);
3000 set_gdbarch_address_class_type_flags_to_name (gdbarch,
3001 s390_address_class_type_flags_to_name);
3002 set_gdbarch_address_class_name_to_type_flags (gdbarch,
3003 s390_address_class_name_to_type_flags);
3007 set_gdbarch_print_insn (gdbarch, print_insn_s390);
3009 /* Enable TLS support. */
3010 set_gdbarch_fetch_tls_load_module_address (gdbarch,
3011 svr4_fetch_objfile_link_map);
3018 extern initialize_file_ftype _initialize_s390_tdep; /* -Wmissing-prototypes */
3021 _initialize_s390_tdep (void)
3024 /* Hook us into the gdbarch mechanism. */
3025 register_gdbarch_init (bfd_arch_s390, s390_gdbarch_init);