* s390-tdep.c (s390_gdbarch_init): Revert accidentally committed
[deliverable/binutils-gdb.git] / gdb / s390-tdep.c
1 /* Target-dependent code for GDB, the GNU debugger.
2 Copyright 2001 Free Software Foundation, Inc.
3 Contributed by D.J. Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
4 for IBM Deutschland Entwicklung GmbH, IBM Corporation.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22
23 #define S390_TDEP /* for special macros in tm-s390.h */
24 #include <defs.h>
25 #include "arch-utils.h"
26 #include "frame.h"
27 #include "inferior.h"
28 #include "symtab.h"
29 #include "target.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "symfile.h"
33 #include "objfiles.h"
34 #include "tm.h"
35 #include "../bfd/bfd.h"
36 #include "floatformat.h"
37 #include "regcache.h"
38 #include "value.h"
39 #include "gdb_assert.h"
40
41
42
43
44 /* Number of bytes of storage in the actual machine representation
45 for register N. */
46 int
47 s390_register_raw_size (int reg_nr)
48 {
49 if (S390_FP0_REGNUM <= reg_nr
50 && reg_nr < S390_FP0_REGNUM + S390_NUM_FPRS)
51 return S390_FPR_SIZE;
52 else
53 return 4;
54 }
55
56 int
57 s390x_register_raw_size (int reg_nr)
58 {
59 return (reg_nr == S390_FPC_REGNUM)
60 || (reg_nr >= S390_FIRST_ACR && reg_nr <= S390_LAST_ACR) ? 4 : 8;
61 }
62
63 int
64 s390_cannot_fetch_register (int regno)
65 {
66 return (regno >= S390_FIRST_CR && regno < (S390_FIRST_CR + 9)) ||
67 (regno >= (S390_FIRST_CR + 12) && regno <= S390_LAST_CR);
68 }
69
70 int
71 s390_register_byte (int reg_nr)
72 {
73 if (reg_nr <= S390_GP_LAST_REGNUM)
74 return reg_nr * S390_GPR_SIZE;
75 if (reg_nr <= S390_LAST_ACR)
76 return S390_ACR0_OFFSET + (((reg_nr) - S390_FIRST_ACR) * S390_ACR_SIZE);
77 if (reg_nr <= S390_LAST_CR)
78 return S390_CR0_OFFSET + (((reg_nr) - S390_FIRST_CR) * S390_CR_SIZE);
79 if (reg_nr == S390_FPC_REGNUM)
80 return S390_FPC_OFFSET;
81 else
82 return S390_FP0_OFFSET + (((reg_nr) - S390_FP0_REGNUM) * S390_FPR_SIZE);
83 }
84
85 #ifndef GDBSERVER
86 #define S390_MAX_INSTR_SIZE (6)
87 #define S390_SYSCALL_OPCODE (0x0a)
88 #define S390_SYSCALL_SIZE (2)
89 #define S390_SIGCONTEXT_SREGS_OFFSET (8)
90 #define S390X_SIGCONTEXT_SREGS_OFFSET (8)
91 #define S390_SIGREGS_FP0_OFFSET (144)
92 #define S390X_SIGREGS_FP0_OFFSET (216)
93 #define S390_UC_MCONTEXT_OFFSET (256)
94 #define S390X_UC_MCONTEXT_OFFSET (344)
95 #define S390_STACK_FRAME_OVERHEAD (GDB_TARGET_IS_ESAME ? 160:96)
96 #define S390_SIGNAL_FRAMESIZE (GDB_TARGET_IS_ESAME ? 160:96)
97 #define s390_NR_sigreturn 119
98 #define s390_NR_rt_sigreturn 173
99
100
101
102 struct frame_extra_info
103 {
104 int initialised;
105 int good_prologue;
106 CORE_ADDR function_start;
107 CORE_ADDR skip_prologue_function_start;
108 CORE_ADDR saved_pc_valid;
109 CORE_ADDR saved_pc;
110 CORE_ADDR sig_fixed_saved_pc_valid;
111 CORE_ADDR sig_fixed_saved_pc;
112 CORE_ADDR frame_pointer_saved_pc; /* frame pointer needed for alloca */
113 CORE_ADDR stack_bought; /* amount we decrement the stack pointer by */
114 CORE_ADDR sigcontext;
115 };
116
117
118 static CORE_ADDR s390_frame_saved_pc_nofix (struct frame_info *fi);
119
120 int
121 s390_readinstruction (bfd_byte instr[], CORE_ADDR at,
122 struct disassemble_info *info)
123 {
124 int instrlen;
125
126 static int s390_instrlen[] = {
127 2,
128 4,
129 4,
130 6
131 };
132 if ((*info->read_memory_func) (at, &instr[0], 2, info))
133 return -1;
134 instrlen = s390_instrlen[instr[0] >> 6];
135 if ((*info->read_memory_func) (at + 2, &instr[2], instrlen - 2, info))
136 return -1;
137 return instrlen;
138 }
139
140 static void
141 s390_memset_extra_info (struct frame_extra_info *fextra_info)
142 {
143 memset (fextra_info, 0, sizeof (struct frame_extra_info));
144 }
145
146
147
148 char *
149 s390_register_name (int reg_nr)
150 {
151 static char *register_names[] = {
152 "pswm", "pswa",
153 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
154 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
155 "acr0", "acr1", "acr2", "acr3", "acr4", "acr5", "acr6", "acr7",
156 "acr8", "acr9", "acr10", "acr11", "acr12", "acr13", "acr14", "acr15",
157 "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
158 "cr8", "cr9", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15",
159 "fpc",
160 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
161 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15"
162 };
163
164 if (reg_nr >= S390_LAST_REGNUM)
165 return NULL;
166 return register_names[reg_nr];
167 }
168
169
170
171
172 int
173 s390_stab_reg_to_regnum (int regno)
174 {
175 return regno >= 64 ? S390_PSWM_REGNUM - 64 :
176 regno >= 48 ? S390_FIRST_ACR - 48 :
177 regno >= 32 ? S390_FIRST_CR - 32 :
178 regno <= 15 ? (regno + 2) :
179 S390_FP0_REGNUM + ((regno - 16) & 8) + (((regno - 16) & 3) << 1) +
180 (((regno - 16) & 4) >> 2);
181 }
182
183
184 /* Return true if REGIDX is the number of a register used to pass
185 arguments, false otherwise. */
186 static int
187 is_arg_reg (int regidx)
188 {
189 return 2 <= regidx && regidx <= 6;
190 }
191
192
193 /* s390_get_frame_info based on Hartmuts
194 prologue definition in
195 gcc-2.8.1/config/l390/linux.c
196
197 It reads one instruction at a time & based on whether
198 it looks like prologue code or not it makes a decision on
199 whether the prologue is over, there are various state machines
200 in the code to determine if the prologue code is possilby valid.
201
202 This is done to hopefully allow the code survive minor revs of
203 calling conventions.
204
205 */
206
207 int
208 s390_get_frame_info (CORE_ADDR pc, struct frame_extra_info *fextra_info,
209 struct frame_info *fi, int init_extra_info)
210 {
211 #define CONST_POOL_REGIDX 13
212 #define GOT_REGIDX 12
213 bfd_byte instr[S390_MAX_INSTR_SIZE];
214 CORE_ADDR test_pc = pc, test_pc2;
215 CORE_ADDR orig_sp = 0, save_reg_addr = 0, *saved_regs = NULL;
216 int valid_prologue, good_prologue = 0;
217 int gprs_saved[S390_NUM_GPRS];
218 int fprs_saved[S390_NUM_FPRS];
219 int regidx, instrlen;
220 int const_pool_state;
221 int varargs_state;
222 int loop_cnt, gdb_gpr_store, gdb_fpr_store;
223 int offset, expected_offset;
224 int err = 0;
225 disassemble_info info;
226
227 /* Have we seen an instruction initializing the frame pointer yet?
228 If we've seen an `lr %r11, %r15', then frame_pointer_found is
229 non-zero, and frame_pointer_regidx == 11. Otherwise,
230 frame_pointer_found is zero and frame_pointer_regidx is 15,
231 indicating that we're using the stack pointer as our frame
232 pointer. */
233 int frame_pointer_found = 0;
234 int frame_pointer_regidx = 0xf;
235
236 /* What we've seen so far regarding saving the back chain link:
237 0 -- nothing yet; sp still has the same value it had at the entry
238 point. Since not all functions allocate frames, this is a
239 valid state for the prologue to finish in.
240 1 -- We've saved the original sp in some register other than the
241 frame pointer (hard-coded to be %r11, yuck).
242 save_link_regidx is the register we saved it in.
243 2 -- We've seen the initial `bras' instruction of the sequence for
244 reserving more than 32k of stack:
245 bras %rX, .+8
246 .long N
247 s %r15, 0(%rX)
248 where %rX is not the constant pool register.
249 subtract_sp_regidx is %rX, and fextra_info->stack_bought is N.
250 3 -- We've reserved space for a new stack frame. This means we
251 either saw a simple `ahi %r15,-N' in state 1, or the final
252 `s %r15, ...' in state 2.
253 4 -- The frame and link are now fully initialized. We've
254 reserved space for the new stack frame, and stored the old
255 stack pointer captured in the back chain pointer field. */
256 int save_link_state = 0;
257 int save_link_regidx, subtract_sp_regidx;
258
259 /* What we've seen so far regarding r12 --- the GOT (Global Offset
260 Table) pointer. We expect to see `l %r12, N(%r13)', which loads
261 r12 with the offset from the constant pool to the GOT, and then
262 an `ar %r12, %r13', which adds the constant pool address,
263 yielding the GOT's address. Here's what got_state means:
264 0 -- seen nothing
265 1 -- seen `l %r12, N(%r13)', but no `ar'
266 2 -- seen load and add, so GOT pointer is totally initialized
267 When got_state is 1, then got_load_addr is the address of the
268 load instruction, and got_load_len is the length of that
269 instruction. */
270 int got_state= 0;
271 CORE_ADDR got_load_addr = 0, got_load_len = 0;
272
273 const_pool_state = varargs_state = 0;
274
275 memset (gprs_saved, 0, sizeof (gprs_saved));
276 memset (fprs_saved, 0, sizeof (fprs_saved));
277 info.read_memory_func = dis_asm_read_memory;
278
279 save_link_regidx = subtract_sp_regidx = 0;
280 if (fextra_info)
281 {
282 if (fi && fi->frame)
283 {
284 if (! init_extra_info && fextra_info->initialised)
285 orig_sp = fi->frame + fextra_info->stack_bought;
286 saved_regs = fi->saved_regs;
287 }
288 if (init_extra_info || !fextra_info->initialised)
289 {
290 s390_memset_extra_info (fextra_info);
291 fextra_info->function_start = pc;
292 fextra_info->initialised = 1;
293 }
294 }
295 instrlen = 0;
296 do
297 {
298 valid_prologue = 0;
299 test_pc += instrlen;
300 /* add the previous instruction len */
301 instrlen = s390_readinstruction (instr, test_pc, &info);
302 if (instrlen < 0)
303 {
304 good_prologue = 0;
305 err = -1;
306 break;
307 }
308 /* We probably are in a glibc syscall */
309 if (instr[0] == S390_SYSCALL_OPCODE && test_pc == pc)
310 {
311 good_prologue = 1;
312 if (saved_regs && fextra_info && fi->next && fi->next->extra_info
313 && fi->next->extra_info->sigcontext)
314 {
315 /* We are backtracing from a signal handler */
316 save_reg_addr = fi->next->extra_info->sigcontext +
317 REGISTER_BYTE (S390_GP0_REGNUM);
318 for (regidx = 0; regidx < S390_NUM_GPRS; regidx++)
319 {
320 saved_regs[S390_GP0_REGNUM + regidx] = save_reg_addr;
321 save_reg_addr += S390_GPR_SIZE;
322 }
323 save_reg_addr = fi->next->extra_info->sigcontext +
324 (GDB_TARGET_IS_ESAME ? S390X_SIGREGS_FP0_OFFSET :
325 S390_SIGREGS_FP0_OFFSET);
326 for (regidx = 0; regidx < S390_NUM_FPRS; regidx++)
327 {
328 saved_regs[S390_FP0_REGNUM + regidx] = save_reg_addr;
329 save_reg_addr += S390_FPR_SIZE;
330 }
331 }
332 break;
333 }
334 if (save_link_state == 0)
335 {
336 /* check for a stack relative STMG or STM */
337 if (((GDB_TARGET_IS_ESAME &&
338 ((instr[0] == 0xeb) && (instr[5] == 0x24))) ||
339 (instr[0] == 0x90)) && ((instr[2] >> 4) == 0xf))
340 {
341 regidx = (instr[1] >> 4);
342 if (regidx < 6)
343 varargs_state = 1;
344 offset = ((instr[2] & 0xf) << 8) + instr[3];
345 expected_offset =
346 S390_GPR6_STACK_OFFSET + (S390_GPR_SIZE * (regidx - 6));
347 if (offset != expected_offset)
348 {
349 good_prologue = 0;
350 break;
351 }
352 if (saved_regs)
353 save_reg_addr = orig_sp + offset;
354 for (; regidx <= (instr[1] & 0xf); regidx++)
355 {
356 if (gprs_saved[regidx])
357 {
358 good_prologue = 0;
359 break;
360 }
361 good_prologue = 1;
362 gprs_saved[regidx] = 1;
363 if (saved_regs)
364 {
365 saved_regs[S390_GP0_REGNUM + regidx] = save_reg_addr;
366 save_reg_addr += S390_GPR_SIZE;
367 }
368 }
369 valid_prologue = 1;
370 continue;
371 }
372 }
373 /* check for a stack relative STG or ST */
374 if ((save_link_state == 0 || save_link_state == 3) &&
375 ((GDB_TARGET_IS_ESAME &&
376 ((instr[0] == 0xe3) && (instr[5] == 0x24))) ||
377 (instr[0] == 0x50)) && ((instr[2] >> 4) == 0xf))
378 {
379 regidx = instr[1] >> 4;
380 offset = ((instr[2] & 0xf) << 8) + instr[3];
381 if (offset == 0)
382 {
383 if (save_link_state == 3 && regidx == save_link_regidx)
384 {
385 save_link_state = 4;
386 valid_prologue = 1;
387 continue;
388 }
389 else
390 break;
391 }
392 if (regidx < 6)
393 varargs_state = 1;
394 expected_offset =
395 S390_GPR6_STACK_OFFSET + (S390_GPR_SIZE * (regidx - 6));
396 if (offset != expected_offset)
397 {
398 good_prologue = 0;
399 break;
400 }
401 if (gprs_saved[regidx])
402 {
403 good_prologue = 0;
404 break;
405 }
406 good_prologue = 1;
407 gprs_saved[regidx] = 1;
408 if (saved_regs)
409 {
410 save_reg_addr = orig_sp + offset;
411 saved_regs[S390_GP0_REGNUM + regidx] = save_reg_addr;
412 }
413 valid_prologue = 1;
414 continue;
415 }
416
417 /* Check for an fp-relative STG, ST, or STM. This is probably
418 spilling an argument from a register out into a stack slot.
419 This could be a user instruction, but if we haven't included
420 any other suspicious instructions in the prologue, this
421 could only be an initializing store, which isn't too bad to
422 skip. The consequences of not including arg-to-stack spills
423 are more serious, though --- you don't see the proper values
424 of the arguments. */
425 if ((save_link_state == 3 || save_link_state == 4)
426 && ((instr[0] == 0x50 /* st %rA, D(%rX,%rB) */
427 && (instr[1] & 0xf) == 0 /* %rX is zero, no index reg */
428 && is_arg_reg ((instr[1] >> 4) & 0xf)
429 && ((instr[2] >> 4) & 0xf) == frame_pointer_regidx)
430 || (instr[0] == 0x90 /* stm %rA, %rB, D(%rC) */
431 && is_arg_reg ((instr[1] >> 4) & 0xf)
432 && is_arg_reg (instr[1] & 0xf)
433 && ((instr[2] >> 4) & 0xf) == frame_pointer_regidx)))
434 {
435 valid_prologue = 1;
436 continue;
437 }
438
439 /* check for STD */
440 if (instr[0] == 0x60 && (instr[2] >> 4) == 0xf)
441 {
442 regidx = instr[1] >> 4;
443 if (regidx == 0 || regidx == 2)
444 varargs_state = 1;
445 if (fprs_saved[regidx])
446 {
447 good_prologue = 0;
448 break;
449 }
450 fprs_saved[regidx] = 1;
451 if (saved_regs)
452 {
453 save_reg_addr = orig_sp + (((instr[2] & 0xf) << 8) + instr[3]);
454 saved_regs[S390_FP0_REGNUM + regidx] = save_reg_addr;
455 }
456 valid_prologue = 1;
457 continue;
458 }
459
460
461 if (const_pool_state == 0)
462 {
463
464 if (GDB_TARGET_IS_ESAME)
465 {
466 /* Check for larl CONST_POOL_REGIDX,offset on ESAME */
467 if ((instr[0] == 0xc0)
468 && (instr[1] == (CONST_POOL_REGIDX << 4)))
469 {
470 const_pool_state = 2;
471 valid_prologue = 1;
472 continue;
473 }
474 }
475 else
476 {
477 /* Check for BASR gpr13,gpr0 used to load constant pool pointer to r13 in old compiler */
478 if (instr[0] == 0xd && (instr[1] & 0xf) == 0
479 && ((instr[1] >> 4) == CONST_POOL_REGIDX))
480 {
481 const_pool_state = 1;
482 valid_prologue = 1;
483 continue;
484 }
485 }
486 /* Check for new fangled bras %r13,newpc to load new constant pool */
487 /* embedded in code, older pre abi compilers also emitted this stuff. */
488 if ((instr[0] == 0xa7) && ((instr[1] & 0xf) == 0x5) &&
489 ((instr[1] >> 4) == CONST_POOL_REGIDX)
490 && ((instr[2] & 0x80) == 0))
491 {
492 const_pool_state = 2;
493 test_pc +=
494 (((((instr[2] & 0xf) << 8) + instr[3]) << 1) - instrlen);
495 valid_prologue = 1;
496 continue;
497 }
498 }
499 /* Check for AGHI or AHI CONST_POOL_REGIDX,val */
500 if (const_pool_state == 1 && (instr[0] == 0xa7) &&
501 ((GDB_TARGET_IS_ESAME &&
502 (instr[1] == ((CONST_POOL_REGIDX << 4) | 0xb))) ||
503 (instr[1] == ((CONST_POOL_REGIDX << 4) | 0xa))))
504 {
505 const_pool_state = 2;
506 valid_prologue = 1;
507 continue;
508 }
509 /* Check for LGR or LR gprx,15 */
510 if ((GDB_TARGET_IS_ESAME &&
511 instr[0] == 0xb9 && instr[1] == 0x04 && (instr[3] & 0xf) == 0xf) ||
512 (instr[0] == 0x18 && (instr[1] & 0xf) == 0xf))
513 {
514 if (GDB_TARGET_IS_ESAME)
515 regidx = instr[3] >> 4;
516 else
517 regidx = instr[1] >> 4;
518 if (save_link_state == 0 && regidx != 0xb)
519 {
520 /* Almost defintely code for
521 decrementing the stack pointer
522 ( i.e. a non leaf function
523 or else leaf with locals ) */
524 save_link_regidx = regidx;
525 save_link_state = 1;
526 valid_prologue = 1;
527 continue;
528 }
529 /* We use this frame pointer for alloca
530 unfortunately we need to assume its gpr11
531 otherwise we would need a smarter prologue
532 walker. */
533 if (!frame_pointer_found && regidx == 0xb)
534 {
535 frame_pointer_regidx = 0xb;
536 frame_pointer_found = 1;
537 if (fextra_info)
538 fextra_info->frame_pointer_saved_pc = test_pc;
539 valid_prologue = 1;
540 continue;
541 }
542 }
543 /* Check for AHI or AGHI gpr15,val */
544 if (save_link_state == 1 && (instr[0] == 0xa7) &&
545 ((GDB_TARGET_IS_ESAME && (instr[1] == 0xfb)) || (instr[1] == 0xfa)))
546 {
547 if (fextra_info)
548 fextra_info->stack_bought =
549 -extract_signed_integer (&instr[2], 2);
550 save_link_state = 3;
551 valid_prologue = 1;
552 continue;
553 }
554 /* Alternatively check for the complex construction for
555 buying more than 32k of stack
556 BRAS gprx,.+8
557 long val
558 s %r15,0(%gprx) gprx currently r1 */
559 if ((save_link_state == 1) && (instr[0] == 0xa7)
560 && ((instr[1] & 0xf) == 0x5) && (instr[2] == 0)
561 && (instr[3] == 0x4) && ((instr[1] >> 4) != CONST_POOL_REGIDX))
562 {
563 subtract_sp_regidx = instr[1] >> 4;
564 save_link_state = 2;
565 if (fextra_info)
566 target_read_memory (test_pc + instrlen,
567 (char *) &fextra_info->stack_bought,
568 sizeof (fextra_info->stack_bought));
569 test_pc += 4;
570 valid_prologue = 1;
571 continue;
572 }
573 if (save_link_state == 2 && instr[0] == 0x5b
574 && instr[1] == 0xf0 &&
575 instr[2] == (subtract_sp_regidx << 4) && instr[3] == 0)
576 {
577 save_link_state = 3;
578 valid_prologue = 1;
579 continue;
580 }
581 /* check for LA gprx,offset(15) used for varargs */
582 if ((instr[0] == 0x41) && ((instr[2] >> 4) == 0xf) &&
583 ((instr[1] & 0xf) == 0))
584 {
585 /* some code uses gpr7 to point to outgoing args */
586 if (((instr[1] >> 4) == 7) && (save_link_state == 0) &&
587 ((instr[2] & 0xf) == 0)
588 && (instr[3] == S390_STACK_FRAME_OVERHEAD))
589 {
590 valid_prologue = 1;
591 continue;
592 }
593 if (varargs_state == 1)
594 {
595 varargs_state = 2;
596 valid_prologue = 1;
597 continue;
598 }
599 }
600 /* Check for a GOT load */
601
602 if (GDB_TARGET_IS_ESAME)
603 {
604 /* Check for larl GOT_REGIDX, on ESAME */
605 if ((got_state == 0) && (instr[0] == 0xc0)
606 && (instr[1] == (GOT_REGIDX << 4)))
607 {
608 got_state = 2;
609 valid_prologue = 1;
610 continue;
611 }
612 }
613 else
614 {
615 /* check for l GOT_REGIDX,x(CONST_POOL_REGIDX) */
616 if (got_state == 0 && const_pool_state == 2 && instr[0] == 0x58
617 && (instr[2] == (CONST_POOL_REGIDX << 4))
618 && ((instr[1] >> 4) == GOT_REGIDX))
619 {
620 got_state = 1;
621 got_load_addr = test_pc;
622 got_load_len = instrlen;
623 valid_prologue = 1;
624 continue;
625 }
626 /* Check for subsequent ar got_regidx,basr_regidx */
627 if (got_state == 1 && instr[0] == 0x1a &&
628 instr[1] == ((GOT_REGIDX << 4) | CONST_POOL_REGIDX))
629 {
630 got_state = 2;
631 valid_prologue = 1;
632 continue;
633 }
634 }
635 }
636 while (valid_prologue && good_prologue);
637 if (good_prologue)
638 {
639 /* If this function doesn't reference the global offset table,
640 then the compiler may use r12 for other things. If the last
641 instruction we saw was a load of r12 from the constant pool,
642 with no subsequent add to make the address PC-relative, then
643 the load was probably a genuine body instruction; don't treat
644 it as part of the prologue. */
645 if (got_state == 1
646 && got_load_addr + got_load_len == test_pc)
647 {
648 test_pc = got_load_addr;
649 instrlen = got_load_len;
650 }
651
652 good_prologue = (((const_pool_state == 0) || (const_pool_state == 2)) &&
653 ((save_link_state == 0) || (save_link_state == 4)) &&
654 ((varargs_state == 0) || (varargs_state == 2)));
655 }
656 if (fextra_info)
657 {
658 fextra_info->good_prologue = good_prologue;
659 fextra_info->skip_prologue_function_start =
660 (good_prologue ? test_pc : pc);
661 }
662 if (saved_regs)
663 /* The SP's element of the saved_regs array holds the old SP,
664 not the address at which it is saved. */
665 saved_regs[S390_SP_REGNUM] = orig_sp;
666 return err;
667 }
668
669
670 int
671 s390_check_function_end (CORE_ADDR pc)
672 {
673 bfd_byte instr[S390_MAX_INSTR_SIZE];
674 disassemble_info info;
675 int regidx, instrlen;
676
677 info.read_memory_func = dis_asm_read_memory;
678 instrlen = s390_readinstruction (instr, pc, &info);
679 if (instrlen < 0)
680 return -1;
681 /* check for BR */
682 if (instrlen != 2 || instr[0] != 07 || (instr[1] >> 4) != 0xf)
683 return 0;
684 regidx = instr[1] & 0xf;
685 /* Check for LMG or LG */
686 instrlen =
687 s390_readinstruction (instr, pc - (GDB_TARGET_IS_ESAME ? 6 : 4), &info);
688 if (instrlen < 0)
689 return -1;
690 if (GDB_TARGET_IS_ESAME)
691 {
692
693 if (instrlen != 6 || instr[0] != 0xeb || instr[5] != 0x4)
694 return 0;
695 }
696 else if (instrlen != 4 || instr[0] != 0x98)
697 {
698 return 0;
699 }
700 if ((instr[2] >> 4) != 0xf)
701 return 0;
702 if (regidx == 14)
703 return 1;
704 instrlen = s390_readinstruction (instr, pc - (GDB_TARGET_IS_ESAME ? 12 : 8),
705 &info);
706 if (instrlen < 0)
707 return -1;
708 if (GDB_TARGET_IS_ESAME)
709 {
710 /* Check for LG */
711 if (instrlen != 6 || instr[0] != 0xe3 || instr[5] != 0x4)
712 return 0;
713 }
714 else
715 {
716 /* Check for L */
717 if (instrlen != 4 || instr[0] != 0x58)
718 return 0;
719 }
720 if (instr[2] >> 4 != 0xf)
721 return 0;
722 if (instr[1] >> 4 != regidx)
723 return 0;
724 return 1;
725 }
726
727 static CORE_ADDR
728 s390_sniff_pc_function_start (CORE_ADDR pc, struct frame_info *fi)
729 {
730 CORE_ADDR function_start, test_function_start;
731 int loop_cnt, err, function_end;
732 struct frame_extra_info fextra_info;
733 function_start = get_pc_function_start (pc);
734
735 if (function_start == 0)
736 {
737 test_function_start = pc;
738 if (test_function_start & 1)
739 return 0; /* This has to be bogus */
740 loop_cnt = 0;
741 do
742 {
743
744 err =
745 s390_get_frame_info (test_function_start, &fextra_info, fi, 1);
746 loop_cnt++;
747 test_function_start -= 2;
748 function_end = s390_check_function_end (test_function_start);
749 }
750 while (!(function_end == 1 || err || loop_cnt >= 4096 ||
751 (fextra_info.good_prologue)));
752 if (fextra_info.good_prologue)
753 function_start = fextra_info.function_start;
754 else if (function_end == 1)
755 function_start = test_function_start;
756 }
757 return function_start;
758 }
759
760
761
762 CORE_ADDR
763 s390_function_start (struct frame_info *fi)
764 {
765 CORE_ADDR function_start = 0;
766
767 if (fi->extra_info && fi->extra_info->initialised)
768 function_start = fi->extra_info->function_start;
769 else if (fi->pc)
770 function_start = get_pc_function_start (fi->pc);
771 return function_start;
772 }
773
774
775
776
777 int
778 s390_frameless_function_invocation (struct frame_info *fi)
779 {
780 struct frame_extra_info fextra_info, *fextra_info_ptr;
781 int frameless = 0;
782
783 if (fi->next == NULL) /* no may be frameless */
784 {
785 if (fi->extra_info)
786 fextra_info_ptr = fi->extra_info;
787 else
788 {
789 fextra_info_ptr = &fextra_info;
790 s390_get_frame_info (s390_sniff_pc_function_start (fi->pc, fi),
791 fextra_info_ptr, fi, 1);
792 }
793 frameless = ((fextra_info_ptr->stack_bought == 0));
794 }
795 return frameless;
796
797 }
798
799
800 static int
801 s390_is_sigreturn (CORE_ADDR pc, struct frame_info *sighandler_fi,
802 CORE_ADDR *sregs, CORE_ADDR *sigcaller_pc)
803 {
804 bfd_byte instr[S390_MAX_INSTR_SIZE];
805 disassemble_info info;
806 int instrlen;
807 CORE_ADDR scontext;
808 int retval = 0;
809 CORE_ADDR orig_sp;
810 CORE_ADDR temp_sregs;
811
812 scontext = temp_sregs = 0;
813
814 info.read_memory_func = dis_asm_read_memory;
815 instrlen = s390_readinstruction (instr, pc, &info);
816 if (sigcaller_pc)
817 *sigcaller_pc = 0;
818 if (((instrlen == S390_SYSCALL_SIZE) &&
819 (instr[0] == S390_SYSCALL_OPCODE)) &&
820 ((instr[1] == s390_NR_sigreturn) || (instr[1] == s390_NR_rt_sigreturn)))
821 {
822 if (sighandler_fi)
823 {
824 if (s390_frameless_function_invocation (sighandler_fi))
825 orig_sp = sighandler_fi->frame;
826 else
827 orig_sp = ADDR_BITS_REMOVE ((CORE_ADDR)
828 read_memory_integer (sighandler_fi->
829 frame,
830 S390_GPR_SIZE));
831 if (orig_sp && sigcaller_pc)
832 {
833 scontext = orig_sp + S390_SIGNAL_FRAMESIZE;
834 if (pc == scontext && instr[1] == s390_NR_rt_sigreturn)
835 {
836 /* We got a new style rt_signal */
837 /* get address of read ucontext->uc_mcontext */
838 temp_sregs = orig_sp + (GDB_TARGET_IS_ESAME ?
839 S390X_UC_MCONTEXT_OFFSET :
840 S390_UC_MCONTEXT_OFFSET);
841 }
842 else
843 {
844 /* read sigcontext->sregs */
845 temp_sregs = ADDR_BITS_REMOVE ((CORE_ADDR)
846 read_memory_integer (scontext
847 +
848 (GDB_TARGET_IS_ESAME
849 ?
850 S390X_SIGCONTEXT_SREGS_OFFSET
851 :
852 S390_SIGCONTEXT_SREGS_OFFSET),
853 S390_GPR_SIZE));
854
855 }
856 /* read sigregs->psw.addr */
857 *sigcaller_pc =
858 ADDR_BITS_REMOVE ((CORE_ADDR)
859 read_memory_integer (temp_sregs +
860 REGISTER_BYTE
861 (S390_PC_REGNUM),
862 S390_PSW_ADDR_SIZE));
863 }
864 }
865 retval = 1;
866 }
867 if (sregs)
868 *sregs = temp_sregs;
869 return retval;
870 }
871
872 /*
873 We need to do something better here but this will keep us out of trouble
874 for the moment.
875 For some reason the blockframe.c calls us with fi->next->fromleaf
876 so this seems of little use to us. */
877 void
878 s390_init_frame_pc_first (int next_fromleaf, struct frame_info *fi)
879 {
880 CORE_ADDR sigcaller_pc;
881
882 fi->pc = 0;
883 if (next_fromleaf)
884 {
885 fi->pc = ADDR_BITS_REMOVE (read_register (S390_RETADDR_REGNUM));
886 /* fix signal handlers */
887 }
888 else if (fi->next && fi->next->pc)
889 fi->pc = s390_frame_saved_pc_nofix (fi->next);
890 if (fi->pc && fi->next && fi->next->frame &&
891 s390_is_sigreturn (fi->pc, fi->next, NULL, &sigcaller_pc))
892 {
893 fi->pc = sigcaller_pc;
894 }
895
896 }
897
898 void
899 s390_init_extra_frame_info (int fromleaf, struct frame_info *fi)
900 {
901 fi->extra_info = frame_obstack_alloc (sizeof (struct frame_extra_info));
902 if (fi->pc)
903 s390_get_frame_info (s390_sniff_pc_function_start (fi->pc, fi),
904 fi->extra_info, fi, 1);
905 else
906 s390_memset_extra_info (fi->extra_info);
907 }
908
909 /* If saved registers of frame FI are not known yet, read and cache them.
910 &FEXTRA_INFOP contains struct frame_extra_info; TDATAP can be NULL,
911 in which case the framedata are read. */
912
913 void
914 s390_frame_init_saved_regs (struct frame_info *fi)
915 {
916
917 int quick;
918
919 if (fi->saved_regs == NULL)
920 {
921 /* zalloc memsets the saved regs */
922 frame_saved_regs_zalloc (fi);
923 if (fi->pc)
924 {
925 quick = (fi->extra_info && fi->extra_info->initialised
926 && fi->extra_info->good_prologue);
927 s390_get_frame_info (quick ? fi->extra_info->function_start :
928 s390_sniff_pc_function_start (fi->pc, fi),
929 fi->extra_info, fi, !quick);
930 }
931 }
932 }
933
934
935
936 CORE_ADDR
937 s390_frame_args_address (struct frame_info *fi)
938 {
939
940 /* Apparently gdb already knows gdb_args_offset itself */
941 return fi->frame;
942 }
943
944
945 static CORE_ADDR
946 s390_frame_saved_pc_nofix (struct frame_info *fi)
947 {
948 if (fi->extra_info && fi->extra_info->saved_pc_valid)
949 return fi->extra_info->saved_pc;
950
951 if (generic_find_dummy_frame (fi->pc, fi->frame))
952 return generic_read_register_dummy (fi->pc, fi->frame, S390_PC_REGNUM);
953
954 s390_frame_init_saved_regs (fi);
955 if (fi->extra_info)
956 {
957 fi->extra_info->saved_pc_valid = 1;
958 if (fi->extra_info->good_prologue)
959 {
960 if (fi->saved_regs[S390_RETADDR_REGNUM])
961 {
962 return (fi->extra_info->saved_pc =
963 ADDR_BITS_REMOVE (read_memory_integer
964 (fi->saved_regs[S390_RETADDR_REGNUM],
965 S390_GPR_SIZE)));
966 }
967 else
968 return read_register (S390_RETADDR_REGNUM);
969 }
970 }
971 return 0;
972 }
973
974 CORE_ADDR
975 s390_frame_saved_pc (struct frame_info *fi)
976 {
977 CORE_ADDR saved_pc = 0, sig_pc;
978
979 if (fi->extra_info && fi->extra_info->sig_fixed_saved_pc_valid)
980 return fi->extra_info->sig_fixed_saved_pc;
981 saved_pc = s390_frame_saved_pc_nofix (fi);
982
983 if (fi->extra_info)
984 {
985 fi->extra_info->sig_fixed_saved_pc_valid = 1;
986 if (saved_pc)
987 {
988 if (s390_is_sigreturn (saved_pc, fi, NULL, &sig_pc))
989 saved_pc = sig_pc;
990 }
991 fi->extra_info->sig_fixed_saved_pc = saved_pc;
992 }
993 return saved_pc;
994 }
995
996
997
998
999 /* We want backtraces out of signal handlers so we don't
1000 set thisframe->signal_handler_caller to 1 */
1001
1002 CORE_ADDR
1003 s390_frame_chain (struct frame_info *thisframe)
1004 {
1005 CORE_ADDR prev_fp = 0;
1006
1007 if (thisframe->prev && thisframe->prev->frame)
1008 prev_fp = thisframe->prev->frame;
1009 else if (generic_find_dummy_frame (thisframe->pc, thisframe->frame))
1010 return generic_read_register_dummy (thisframe->pc, thisframe->frame,
1011 S390_SP_REGNUM);
1012 else
1013 {
1014 int sigreturn = 0;
1015 CORE_ADDR sregs = 0;
1016 struct frame_extra_info prev_fextra_info;
1017
1018 memset (&prev_fextra_info, 0, sizeof (prev_fextra_info));
1019 if (thisframe->pc)
1020 {
1021 CORE_ADDR saved_pc, sig_pc;
1022
1023 saved_pc = s390_frame_saved_pc_nofix (thisframe);
1024 if (saved_pc)
1025 {
1026 if ((sigreturn =
1027 s390_is_sigreturn (saved_pc, thisframe, &sregs, &sig_pc)))
1028 saved_pc = sig_pc;
1029 s390_get_frame_info (s390_sniff_pc_function_start
1030 (saved_pc, NULL), &prev_fextra_info, NULL,
1031 1);
1032 }
1033 }
1034 if (sigreturn)
1035 {
1036 /* read sigregs,regs.gprs[11 or 15] */
1037 prev_fp = read_memory_integer (sregs +
1038 REGISTER_BYTE (S390_GP0_REGNUM +
1039 (prev_fextra_info.
1040 frame_pointer_saved_pc
1041 ? 11 : 15)),
1042 S390_GPR_SIZE);
1043 thisframe->extra_info->sigcontext = sregs;
1044 }
1045 else
1046 {
1047 if (thisframe->saved_regs)
1048 {
1049 int regno;
1050
1051 if (prev_fextra_info.frame_pointer_saved_pc
1052 && thisframe->saved_regs[S390_FRAME_REGNUM])
1053 regno = S390_FRAME_REGNUM;
1054 else
1055 regno = S390_SP_REGNUM;
1056
1057 if (thisframe->saved_regs[regno])
1058 {
1059 /* The SP's entry of `saved_regs' is special. */
1060 if (regno == S390_SP_REGNUM)
1061 prev_fp = thisframe->saved_regs[regno];
1062 else
1063 prev_fp =
1064 read_memory_integer (thisframe->saved_regs[regno],
1065 S390_GPR_SIZE);
1066 }
1067 }
1068 }
1069 }
1070 return ADDR_BITS_REMOVE (prev_fp);
1071 }
1072
1073 /*
1074 Whether struct frame_extra_info is actually needed I'll have to figure
1075 out as our frames are similar to rs6000 there is a possibility
1076 i386 dosen't need it. */
1077
1078
1079
1080 /* a given return value in `regbuf' with a type `valtype', extract and copy its
1081 value into `valbuf' */
1082 void
1083 s390_extract_return_value (struct type *valtype, char *regbuf, char *valbuf)
1084 {
1085 /* floats and doubles are returned in fpr0. fpr's have a size of 8 bytes.
1086 We need to truncate the return value into float size (4 byte) if
1087 necessary. */
1088 int len = TYPE_LENGTH (valtype);
1089
1090 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1091 memcpy (valbuf, &regbuf[REGISTER_BYTE (S390_FP0_REGNUM)], len);
1092 else
1093 {
1094 int offset = 0;
1095 /* return value is copied starting from r2. */
1096 if (TYPE_LENGTH (valtype) < S390_GPR_SIZE)
1097 offset = S390_GPR_SIZE - TYPE_LENGTH (valtype);
1098 memcpy (valbuf,
1099 regbuf + REGISTER_BYTE (S390_GP0_REGNUM + 2) + offset,
1100 TYPE_LENGTH (valtype));
1101 }
1102 }
1103
1104
1105 static char *
1106 s390_promote_integer_argument (struct type *valtype, char *valbuf,
1107 char *reg_buff, int *arglen)
1108 {
1109 char *value = valbuf;
1110 int len = TYPE_LENGTH (valtype);
1111
1112 if (len < S390_GPR_SIZE)
1113 {
1114 /* We need to upgrade this value to a register to pass it correctly */
1115 int idx, diff = S390_GPR_SIZE - len, negative =
1116 (!TYPE_UNSIGNED (valtype) && value[0] & 0x80);
1117 for (idx = 0; idx < S390_GPR_SIZE; idx++)
1118 {
1119 reg_buff[idx] = (idx < diff ? (negative ? 0xff : 0x0) :
1120 value[idx - diff]);
1121 }
1122 value = reg_buff;
1123 *arglen = S390_GPR_SIZE;
1124 }
1125 else
1126 {
1127 if (len & (S390_GPR_SIZE - 1))
1128 {
1129 fprintf_unfiltered (gdb_stderr,
1130 "s390_promote_integer_argument detected an argument not "
1131 "a multiple of S390_GPR_SIZE & greater than S390_GPR_SIZE "
1132 "we might not deal with this correctly.\n");
1133 }
1134 *arglen = len;
1135 }
1136
1137 return (value);
1138 }
1139
1140 void
1141 s390_store_return_value (struct type *valtype, char *valbuf)
1142 {
1143 int arglen;
1144 char *reg_buff = alloca (max (S390_FPR_SIZE, REGISTER_SIZE)), *value;
1145
1146 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1147 {
1148 if (TYPE_LENGTH (valtype) == 4
1149 || TYPE_LENGTH (valtype) == 8)
1150 write_register_bytes (REGISTER_BYTE (S390_FP0_REGNUM), valbuf,
1151 TYPE_LENGTH (valtype));
1152 else
1153 error ("GDB is unable to return `long double' values "
1154 "on this architecture.");
1155 }
1156 else
1157 {
1158 value =
1159 s390_promote_integer_argument (valtype, valbuf, reg_buff, &arglen);
1160 /* Everything else is returned in GPR2 and up. */
1161 write_register_bytes (REGISTER_BYTE (S390_GP0_REGNUM + 2), value,
1162 arglen);
1163 }
1164 }
1165 static int
1166 gdb_print_insn_s390 (bfd_vma memaddr, disassemble_info * info)
1167 {
1168 bfd_byte instrbuff[S390_MAX_INSTR_SIZE];
1169 int instrlen, cnt;
1170
1171 instrlen = s390_readinstruction (instrbuff, (CORE_ADDR) memaddr, info);
1172 if (instrlen < 0)
1173 {
1174 (*info->memory_error_func) (instrlen, memaddr, info);
1175 return -1;
1176 }
1177 for (cnt = 0; cnt < instrlen; cnt++)
1178 info->fprintf_func (info->stream, "%02X ", instrbuff[cnt]);
1179 for (cnt = instrlen; cnt < S390_MAX_INSTR_SIZE; cnt++)
1180 info->fprintf_func (info->stream, " ");
1181 instrlen = print_insn_s390 (memaddr, info);
1182 return instrlen;
1183 }
1184
1185
1186
1187 /* Not the most efficent code in the world */
1188 int
1189 s390_fp_regnum ()
1190 {
1191 int regno = S390_SP_REGNUM;
1192 struct frame_extra_info fextra_info;
1193
1194 CORE_ADDR pc = ADDR_BITS_REMOVE (read_register (S390_PC_REGNUM));
1195
1196 s390_get_frame_info (s390_sniff_pc_function_start (pc, NULL), &fextra_info,
1197 NULL, 1);
1198 if (fextra_info.frame_pointer_saved_pc)
1199 regno = S390_FRAME_REGNUM;
1200 return regno;
1201 }
1202
1203 CORE_ADDR
1204 s390_read_fp ()
1205 {
1206 return read_register (s390_fp_regnum ());
1207 }
1208
1209
1210 void
1211 s390_write_fp (CORE_ADDR val)
1212 {
1213 write_register (s390_fp_regnum (), val);
1214 }
1215
1216
1217 static void
1218 s390_pop_frame_regular (struct frame_info *frame)
1219 {
1220 int regnum;
1221
1222 write_register (S390_PC_REGNUM, FRAME_SAVED_PC (frame));
1223
1224 /* Restore any saved registers. */
1225 for (regnum = 0; regnum < NUM_REGS; regnum++)
1226 if (frame->saved_regs[regnum] != 0)
1227 {
1228 ULONGEST value;
1229
1230 value = read_memory_unsigned_integer (frame->saved_regs[regnum],
1231 REGISTER_RAW_SIZE (regnum));
1232 write_register (regnum, value);
1233 }
1234
1235 /* Actually cut back the stack. Remember that the SP's element of
1236 saved_regs is the old SP itself, not the address at which it is
1237 saved. */
1238 write_register (S390_SP_REGNUM, frame->saved_regs[S390_SP_REGNUM]);
1239
1240 /* Throw away any cached frame information. */
1241 flush_cached_frames ();
1242 }
1243
1244
1245 /* Destroy the innermost (Top-Of-Stack) stack frame, restoring the
1246 machine state that was in effect before the frame was created.
1247 Used in the contexts of the "return" command, and of
1248 target function calls from the debugger. */
1249 void
1250 s390_pop_frame ()
1251 {
1252 /* This function checks for and handles generic dummy frames, and
1253 calls back to our function for ordinary frames. */
1254 generic_pop_current_frame (s390_pop_frame_regular);
1255 }
1256
1257
1258 /* Return non-zero if TYPE is an integer-like type, zero otherwise.
1259 "Integer-like" types are those that should be passed the way
1260 integers are: integers, enums, ranges, characters, and booleans. */
1261 static int
1262 is_integer_like (struct type *type)
1263 {
1264 enum type_code code = TYPE_CODE (type);
1265
1266 return (code == TYPE_CODE_INT
1267 || code == TYPE_CODE_ENUM
1268 || code == TYPE_CODE_RANGE
1269 || code == TYPE_CODE_CHAR
1270 || code == TYPE_CODE_BOOL);
1271 }
1272
1273
1274 /* Return non-zero if TYPE is a pointer-like type, zero otherwise.
1275 "Pointer-like" types are those that should be passed the way
1276 pointers are: pointers and references. */
1277 static int
1278 is_pointer_like (struct type *type)
1279 {
1280 enum type_code code = TYPE_CODE (type);
1281
1282 return (code == TYPE_CODE_PTR
1283 || code == TYPE_CODE_REF);
1284 }
1285
1286
1287 /* Return non-zero if TYPE is a `float singleton' or `double
1288 singleton', zero otherwise.
1289
1290 A `T singleton' is a struct type with one member, whose type is
1291 either T or a `T singleton'. So, the following are all float
1292 singletons:
1293
1294 struct { float x };
1295 struct { struct { float x; } x; };
1296 struct { struct { struct { float x; } x; } x; };
1297
1298 ... and so on.
1299
1300 WHY THE HECK DO WE CARE ABOUT THIS??? Well, it turns out that GCC
1301 passes all float singletons and double singletons as if they were
1302 simply floats or doubles. This is *not* what the ABI says it
1303 should do. */
1304 static int
1305 is_float_singleton (struct type *type)
1306 {
1307 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
1308 && TYPE_NFIELDS (type) == 1
1309 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_FLT
1310 || is_float_singleton (TYPE_FIELD_TYPE (type, 0))));
1311 }
1312
1313
1314 /* Return non-zero if TYPE is a struct-like type, zero otherwise.
1315 "Struct-like" types are those that should be passed as structs are:
1316 structs and unions.
1317
1318 As an odd quirk, not mentioned in the ABI, GCC passes float and
1319 double singletons as if they were a plain float, double, etc. (The
1320 corresponding union types are handled normally.) So we exclude
1321 those types here. *shrug* */
1322 static int
1323 is_struct_like (struct type *type)
1324 {
1325 enum type_code code = TYPE_CODE (type);
1326
1327 return (code == TYPE_CODE_UNION
1328 || (code == TYPE_CODE_STRUCT && ! is_float_singleton (type)));
1329 }
1330
1331
1332 /* Return non-zero if TYPE is a float-like type, zero otherwise.
1333 "Float-like" types are those that should be passed as
1334 floating-point values are.
1335
1336 You'd think this would just be floats, doubles, long doubles, etc.
1337 But as an odd quirk, not mentioned in the ABI, GCC passes float and
1338 double singletons as if they were a plain float, double, etc. (The
1339 corresponding union types are handled normally.) So we exclude
1340 those types here. *shrug* */
1341 static int
1342 is_float_like (struct type *type)
1343 {
1344 return (TYPE_CODE (type) == TYPE_CODE_FLT
1345 || is_float_singleton (type));
1346 }
1347
1348
1349 /* Return non-zero if TYPE is considered a `DOUBLE_OR_FLOAT', as
1350 defined by the parameter passing conventions described in the
1351 "Linux for S/390 ELF Application Binary Interface Supplement".
1352 Otherwise, return zero. */
1353 static int
1354 is_double_or_float (struct type *type)
1355 {
1356 return (is_float_like (type)
1357 && (TYPE_LENGTH (type) == 4
1358 || TYPE_LENGTH (type) == 8));
1359 }
1360
1361
1362 /* Return non-zero if TYPE is considered a `SIMPLE_ARG', as defined by
1363 the parameter passing conventions described in the "Linux for S/390
1364 ELF Application Binary Interface Supplement". Return zero otherwise. */
1365 static int
1366 is_simple_arg (struct type *type)
1367 {
1368 unsigned length = TYPE_LENGTH (type);
1369
1370 /* This is almost a direct translation of the ABI's language, except
1371 that we have to exclude 8-byte structs; those are DOUBLE_ARGs. */
1372 return ((is_integer_like (type) && length <= 4)
1373 || is_pointer_like (type)
1374 || (is_struct_like (type) && length != 8)
1375 || (is_float_like (type) && length == 16));
1376 }
1377
1378
1379 /* Return non-zero if TYPE should be passed as a pointer to a copy,
1380 zero otherwise. TYPE must be a SIMPLE_ARG, as recognized by
1381 `is_simple_arg'. */
1382 static int
1383 pass_by_copy_ref (struct type *type)
1384 {
1385 unsigned length = TYPE_LENGTH (type);
1386
1387 return ((is_struct_like (type) && length != 1 && length != 2 && length != 4)
1388 || (is_float_like (type) && length == 16));
1389 }
1390
1391
1392 /* Return ARG, a `SIMPLE_ARG', sign-extended or zero-extended to a full
1393 word as required for the ABI. */
1394 static LONGEST
1395 extend_simple_arg (struct value *arg)
1396 {
1397 struct type *type = VALUE_TYPE (arg);
1398
1399 /* Even structs get passed in the least significant bits of the
1400 register / memory word. It's not really right to extract them as
1401 an integer, but it does take care of the extension. */
1402 if (TYPE_UNSIGNED (type))
1403 return extract_unsigned_integer (VALUE_CONTENTS (arg),
1404 TYPE_LENGTH (type));
1405 else
1406 return extract_signed_integer (VALUE_CONTENTS (arg),
1407 TYPE_LENGTH (type));
1408 }
1409
1410
1411 /* Return non-zero if TYPE is a `DOUBLE_ARG', as defined by the
1412 parameter passing conventions described in the "Linux for S/390 ELF
1413 Application Binary Interface Supplement". Return zero otherwise. */
1414 static int
1415 is_double_arg (struct type *type)
1416 {
1417 unsigned length = TYPE_LENGTH (type);
1418
1419 return ((is_integer_like (type)
1420 || is_struct_like (type))
1421 && length == 8);
1422 }
1423
1424
1425 /* Round ADDR up to the next N-byte boundary. N must be a power of
1426 two. */
1427 static CORE_ADDR
1428 round_up (CORE_ADDR addr, int n)
1429 {
1430 /* Check that N is really a power of two. */
1431 gdb_assert (n && (n & (n-1)) == 0);
1432 return ((addr + n - 1) & -n);
1433 }
1434
1435
1436 /* Round ADDR down to the next N-byte boundary. N must be a power of
1437 two. */
1438 static CORE_ADDR
1439 round_down (CORE_ADDR addr, int n)
1440 {
1441 /* Check that N is really a power of two. */
1442 gdb_assert (n && (n & (n-1)) == 0);
1443 return (addr & -n);
1444 }
1445
1446
1447 /* Return the alignment required by TYPE. */
1448 static int
1449 alignment_of (struct type *type)
1450 {
1451 int alignment;
1452
1453 if (is_integer_like (type)
1454 || is_pointer_like (type)
1455 || TYPE_CODE (type) == TYPE_CODE_FLT)
1456 alignment = TYPE_LENGTH (type);
1457 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
1458 || TYPE_CODE (type) == TYPE_CODE_UNION)
1459 {
1460 int i;
1461
1462 alignment = 1;
1463 for (i = 0; i < TYPE_NFIELDS (type); i++)
1464 {
1465 int field_alignment = alignment_of (TYPE_FIELD_TYPE (type, i));
1466
1467 if (field_alignment > alignment)
1468 alignment = field_alignment;
1469 }
1470 }
1471 else
1472 alignment = 1;
1473
1474 /* Check that everything we ever return is a power of two. Lots of
1475 code doesn't want to deal with aligning things to arbitrary
1476 boundaries. */
1477 gdb_assert ((alignment & (alignment - 1)) == 0);
1478
1479 return alignment;
1480 }
1481
1482
1483 /* Put the actual parameter values pointed to by ARGS[0..NARGS-1] in
1484 place to be passed to a function, as specified by the "Linux for
1485 S/390 ELF Application Binary Interface Supplement".
1486
1487 SP is the current stack pointer. We must put arguments, links,
1488 padding, etc. whereever they belong, and return the new stack
1489 pointer value.
1490
1491 If STRUCT_RETURN is non-zero, then the function we're calling is
1492 going to return a structure by value; STRUCT_ADDR is the address of
1493 a block we've allocated for it on the stack.
1494
1495 Our caller has taken care of any type promotions needed to satisfy
1496 prototypes or the old K&R argument-passing rules. */
1497 CORE_ADDR
1498 s390_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
1499 int struct_return, CORE_ADDR struct_addr)
1500 {
1501 int i;
1502 int pointer_size = (TARGET_PTR_BIT / TARGET_CHAR_BIT);
1503
1504 /* The number of arguments passed by reference-to-copy. */
1505 int num_copies;
1506
1507 /* If the i'th argument is passed as a reference to a copy, then
1508 copy_addr[i] is the address of the copy we made. */
1509 CORE_ADDR *copy_addr = alloca (nargs * sizeof (CORE_ADDR));
1510
1511 /* Build the reference-to-copy area. */
1512 num_copies = 0;
1513 for (i = 0; i < nargs; i++)
1514 {
1515 struct value *arg = args[i];
1516 struct type *type = VALUE_TYPE (arg);
1517 unsigned length = TYPE_LENGTH (type);
1518
1519 if (is_simple_arg (type)
1520 && pass_by_copy_ref (type))
1521 {
1522 sp -= length;
1523 sp = round_down (sp, alignment_of (type));
1524 write_memory (sp, VALUE_CONTENTS (arg), length);
1525 copy_addr[i] = sp;
1526 num_copies++;
1527 }
1528 }
1529
1530 /* Reserve space for the parameter area. As a conservative
1531 simplification, we assume that everything will be passed on the
1532 stack. */
1533 {
1534 int i;
1535
1536 for (i = 0; i < nargs; i++)
1537 {
1538 struct value *arg = args[i];
1539 struct type *type = VALUE_TYPE (arg);
1540 int length = TYPE_LENGTH (type);
1541
1542 sp = round_down (sp, alignment_of (type));
1543
1544 /* SIMPLE_ARG values get extended to 32 bits. Assume every
1545 argument is. */
1546 if (length < 4) length = 4;
1547 sp -= length;
1548 }
1549 }
1550
1551 /* Include space for any reference-to-copy pointers. */
1552 sp = round_down (sp, pointer_size);
1553 sp -= num_copies * pointer_size;
1554
1555 /* After all that, make sure it's still aligned on an eight-byte
1556 boundary. */
1557 sp = round_down (sp, 8);
1558
1559 /* Finally, place the actual parameters, working from SP towards
1560 higher addresses. The code above is supposed to reserve enough
1561 space for this. */
1562 {
1563 int fr = 0;
1564 int gr = 2;
1565 CORE_ADDR starg = sp;
1566
1567 for (i = 0; i < nargs; i++)
1568 {
1569 struct value *arg = args[i];
1570 struct type *type = VALUE_TYPE (arg);
1571
1572 if (is_double_or_float (type)
1573 && fr <= 2)
1574 {
1575 /* When we store a single-precision value in an FP register,
1576 it occupies the leftmost bits. */
1577 write_register_bytes (REGISTER_BYTE (S390_FP0_REGNUM + fr),
1578 VALUE_CONTENTS (arg),
1579 TYPE_LENGTH (type));
1580 fr += 2;
1581 }
1582 else if (is_simple_arg (type)
1583 && gr <= 6)
1584 {
1585 /* Do we need to pass a pointer to our copy of this
1586 argument? */
1587 if (pass_by_copy_ref (type))
1588 write_register (S390_GP0_REGNUM + gr, copy_addr[i]);
1589 else
1590 write_register (S390_GP0_REGNUM + gr, extend_simple_arg (arg));
1591
1592 gr++;
1593 }
1594 else if (is_double_arg (type)
1595 && gr <= 5)
1596 {
1597 write_register_gen (S390_GP0_REGNUM + gr,
1598 VALUE_CONTENTS (arg));
1599 write_register_gen (S390_GP0_REGNUM + gr + 1,
1600 VALUE_CONTENTS (arg) + 4);
1601 gr += 2;
1602 }
1603 else
1604 {
1605 /* The `OTHER' case. */
1606 enum type_code code = TYPE_CODE (type);
1607 unsigned length = TYPE_LENGTH (type);
1608
1609 /* If we skipped r6 because we couldn't fit a DOUBLE_ARG
1610 in it, then don't go back and use it again later. */
1611 if (is_double_arg (type) && gr == 6)
1612 gr = 7;
1613
1614 if (is_simple_arg (type))
1615 {
1616 /* Simple args are always either extended to 32 bits,
1617 or pointers. */
1618 starg = round_up (starg, 4);
1619
1620 /* Do we need to pass a pointer to our copy of this
1621 argument? */
1622 if (pass_by_copy_ref (type))
1623 write_memory_signed_integer (starg, pointer_size,
1624 copy_addr[i]);
1625 else
1626 /* Simple args are always extended to 32 bits. */
1627 write_memory_signed_integer (starg, 4,
1628 extend_simple_arg (arg));
1629 starg += 4;
1630 }
1631 else
1632 {
1633 /* You'd think we should say:
1634 starg = round_up (starg, alignment_of (type));
1635 Unfortunately, GCC seems to simply align the stack on
1636 a four-byte boundary, even when passing doubles. */
1637 starg = round_up (starg, 4);
1638 write_memory (starg, VALUE_CONTENTS (arg), length);
1639 starg += length;
1640 }
1641 }
1642 }
1643 }
1644
1645 /* Allocate the standard frame areas: the register save area, the
1646 word reserved for the compiler (which seems kind of meaningless),
1647 and the back chain pointer. */
1648 sp -= 96;
1649
1650 /* Write the back chain pointer into the first word of the stack
1651 frame. This will help us get backtraces from within functions
1652 called from GDB. */
1653 write_memory_unsigned_integer (sp, (TARGET_PTR_BIT / TARGET_CHAR_BIT),
1654 read_fp ());
1655
1656 return sp;
1657 }
1658
1659
1660 static int
1661 s390_use_struct_convention (int gcc_p, struct type *value_type)
1662 {
1663 enum type_code code = TYPE_CODE (value_type);
1664
1665 return (code == TYPE_CODE_STRUCT
1666 || code == TYPE_CODE_UNION);
1667 }
1668
1669
1670 /* Return the GDB type object for the "standard" data type
1671 of data in register N. */
1672 struct type *
1673 s390_register_virtual_type (int regno)
1674 {
1675 return ((unsigned) regno - S390_FPC_REGNUM) <
1676 S390_NUM_FPRS ? builtin_type_double : builtin_type_int;
1677 }
1678
1679
1680 struct type *
1681 s390x_register_virtual_type (int regno)
1682 {
1683 return (regno == S390_FPC_REGNUM) ||
1684 (regno >= S390_FIRST_ACR && regno <= S390_LAST_ACR) ? builtin_type_int :
1685 (regno >= S390_FP0_REGNUM) ? builtin_type_double : builtin_type_long;
1686 }
1687
1688
1689
1690 void
1691 s390_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
1692 {
1693 write_register (S390_GP0_REGNUM + 2, addr);
1694 }
1695
1696
1697
1698 static unsigned char *
1699 s390_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
1700 {
1701 static unsigned char breakpoint[] = { 0x0, 0x1 };
1702
1703 *lenptr = sizeof (breakpoint);
1704 return breakpoint;
1705 }
1706
1707 /* Advance PC across any function entry prologue instructions to reach some
1708 "real" code. */
1709 CORE_ADDR
1710 s390_skip_prologue (CORE_ADDR pc)
1711 {
1712 struct frame_extra_info fextra_info;
1713
1714 s390_get_frame_info (pc, &fextra_info, NULL, 1);
1715 return fextra_info.skip_prologue_function_start;
1716 }
1717
1718 /* Immediately after a function call, return the saved pc.
1719 Can't go through the frames for this because on some machines
1720 the new frame is not set up until the new function executes
1721 some instructions. */
1722 CORE_ADDR
1723 s390_saved_pc_after_call (struct frame_info *frame)
1724 {
1725 return ADDR_BITS_REMOVE (read_register (S390_RETADDR_REGNUM));
1726 }
1727
1728 static CORE_ADDR
1729 s390_addr_bits_remove (CORE_ADDR addr)
1730 {
1731 return (addr) & 0x7fffffff;
1732 }
1733
1734
1735 static CORE_ADDR
1736 s390_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
1737 {
1738 write_register (S390_RETADDR_REGNUM, CALL_DUMMY_ADDRESS ());
1739 return sp;
1740 }
1741
1742 struct gdbarch *
1743 s390_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1744 {
1745 static LONGEST s390_call_dummy_words[] = { 0 };
1746 struct gdbarch *gdbarch;
1747 struct gdbarch_tdep *tdep;
1748 int elf_flags;
1749
1750 /* First see if there is already a gdbarch that can satisfy the request. */
1751 arches = gdbarch_list_lookup_by_info (arches, &info);
1752 if (arches != NULL)
1753 return arches->gdbarch;
1754
1755 /* None found: is the request for a s390 architecture? */
1756 if (info.bfd_arch_info->arch != bfd_arch_s390)
1757 return NULL; /* No; then it's not for us. */
1758
1759 /* Yes: create a new gdbarch for the specified machine type. */
1760 gdbarch = gdbarch_alloc (&info, NULL);
1761
1762 set_gdbarch_believe_pcc_promotion (gdbarch, 0);
1763
1764 set_gdbarch_frame_args_skip (gdbarch, 0);
1765 set_gdbarch_frame_args_address (gdbarch, s390_frame_args_address);
1766 set_gdbarch_frame_chain (gdbarch, s390_frame_chain);
1767 set_gdbarch_frame_init_saved_regs (gdbarch, s390_frame_init_saved_regs);
1768 set_gdbarch_frame_locals_address (gdbarch, s390_frame_args_address);
1769 /* We can't do this */
1770 set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
1771 set_gdbarch_store_struct_return (gdbarch, s390_store_struct_return);
1772 set_gdbarch_extract_return_value (gdbarch, s390_extract_return_value);
1773 set_gdbarch_store_return_value (gdbarch, s390_store_return_value);
1774 /* Amount PC must be decremented by after a breakpoint.
1775 This is often the number of bytes in BREAKPOINT
1776 but not always. */
1777 set_gdbarch_decr_pc_after_break (gdbarch, 2);
1778 set_gdbarch_pop_frame (gdbarch, s390_pop_frame);
1779 set_gdbarch_ieee_float (gdbarch, 1);
1780 /* Stack grows downward. */
1781 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1782 /* Offset from address of function to start of its code.
1783 Zero on most machines. */
1784 set_gdbarch_function_start_offset (gdbarch, 0);
1785 set_gdbarch_max_register_raw_size (gdbarch, 8);
1786 set_gdbarch_max_register_virtual_size (gdbarch, 8);
1787 set_gdbarch_breakpoint_from_pc (gdbarch, s390_breakpoint_from_pc);
1788 set_gdbarch_skip_prologue (gdbarch, s390_skip_prologue);
1789 set_gdbarch_init_extra_frame_info (gdbarch, s390_init_extra_frame_info);
1790 set_gdbarch_init_frame_pc_first (gdbarch, s390_init_frame_pc_first);
1791 set_gdbarch_read_fp (gdbarch, s390_read_fp);
1792 set_gdbarch_write_fp (gdbarch, s390_write_fp);
1793 /* This function that tells us whether the function invocation represented
1794 by FI does not have a frame on the stack associated with it. If it
1795 does not, FRAMELESS is set to 1, else 0. */
1796 set_gdbarch_frameless_function_invocation (gdbarch,
1797 s390_frameless_function_invocation);
1798 /* Return saved PC from a frame */
1799 set_gdbarch_frame_saved_pc (gdbarch, s390_frame_saved_pc);
1800 /* FRAME_CHAIN takes a frame's nominal address
1801 and produces the frame's chain-pointer. */
1802 set_gdbarch_frame_chain (gdbarch, s390_frame_chain);
1803 set_gdbarch_saved_pc_after_call (gdbarch, s390_saved_pc_after_call);
1804 set_gdbarch_register_byte (gdbarch, s390_register_byte);
1805 set_gdbarch_pc_regnum (gdbarch, S390_PC_REGNUM);
1806 set_gdbarch_sp_regnum (gdbarch, S390_SP_REGNUM);
1807 set_gdbarch_fp_regnum (gdbarch, S390_FP_REGNUM);
1808 set_gdbarch_fp0_regnum (gdbarch, S390_FP0_REGNUM);
1809 set_gdbarch_num_regs (gdbarch, S390_NUM_REGS);
1810 set_gdbarch_cannot_fetch_register (gdbarch, s390_cannot_fetch_register);
1811 set_gdbarch_cannot_store_register (gdbarch, s390_cannot_fetch_register);
1812 set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register);
1813 set_gdbarch_use_struct_convention (gdbarch, s390_use_struct_convention);
1814 set_gdbarch_frame_chain_valid (gdbarch, func_frame_chain_valid);
1815 set_gdbarch_register_name (gdbarch, s390_register_name);
1816 set_gdbarch_stab_reg_to_regnum (gdbarch, s390_stab_reg_to_regnum);
1817 set_gdbarch_dwarf_reg_to_regnum (gdbarch, s390_stab_reg_to_regnum);
1818 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, s390_stab_reg_to_regnum);
1819 set_gdbarch_extract_struct_value_address
1820 (gdbarch, generic_cannot_extract_struct_value_address);
1821
1822 /* Parameters for inferior function calls. */
1823 set_gdbarch_call_dummy_p (gdbarch, 1);
1824 set_gdbarch_use_generic_dummy_frames (gdbarch, 1);
1825 set_gdbarch_call_dummy_length (gdbarch, 0);
1826 set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1827 set_gdbarch_call_dummy_address (gdbarch, entry_point_address);
1828 set_gdbarch_call_dummy_start_offset (gdbarch, 0);
1829 set_gdbarch_pc_in_call_dummy (gdbarch, pc_in_call_dummy_at_entry_point);
1830 set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame);
1831 set_gdbarch_push_arguments (gdbarch, s390_push_arguments);
1832 set_gdbarch_save_dummy_frame_tos (gdbarch, generic_save_dummy_frame_tos);
1833 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1834 set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
1835 set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1836 set_gdbarch_fix_call_dummy (gdbarch, generic_fix_call_dummy);
1837 set_gdbarch_push_return_address (gdbarch, s390_push_return_address);
1838 set_gdbarch_sizeof_call_dummy_words (gdbarch,
1839 sizeof (s390_call_dummy_words));
1840 set_gdbarch_call_dummy_words (gdbarch, s390_call_dummy_words);
1841 set_gdbarch_coerce_float_to_double (gdbarch,
1842 standard_coerce_float_to_double);
1843
1844 switch (info.bfd_arch_info->mach)
1845 {
1846 case bfd_mach_s390_esa:
1847 set_gdbarch_register_size (gdbarch, 4);
1848 set_gdbarch_register_raw_size (gdbarch, s390_register_raw_size);
1849 set_gdbarch_register_virtual_size (gdbarch, s390_register_raw_size);
1850 set_gdbarch_register_virtual_type (gdbarch, s390_register_virtual_type);
1851
1852 set_gdbarch_addr_bits_remove (gdbarch, s390_addr_bits_remove);
1853 set_gdbarch_register_bytes (gdbarch, S390_REGISTER_BYTES);
1854 break;
1855 case bfd_mach_s390_esame:
1856 set_gdbarch_register_size (gdbarch, 8);
1857 set_gdbarch_register_raw_size (gdbarch, s390x_register_raw_size);
1858 set_gdbarch_register_virtual_size (gdbarch, s390x_register_raw_size);
1859 set_gdbarch_register_virtual_type (gdbarch,
1860 s390x_register_virtual_type);
1861
1862 set_gdbarch_long_bit (gdbarch, 64);
1863 set_gdbarch_long_long_bit (gdbarch, 64);
1864 set_gdbarch_ptr_bit (gdbarch, 64);
1865 set_gdbarch_register_bytes (gdbarch, S390X_REGISTER_BYTES);
1866 break;
1867 }
1868
1869 return gdbarch;
1870 }
1871
1872
1873
1874 void
1875 _initialize_s390_tdep ()
1876 {
1877
1878 /* Hook us into the gdbarch mechanism. */
1879 register_gdbarch_init (bfd_arch_s390, s390_gdbarch_init);
1880 if (!tm_print_insn) /* Someone may have already set it */
1881 tm_print_insn = gdb_print_insn_s390;
1882 }
1883
1884 #endif /* GDBSERVER */
This page took 0.120334 seconds and 5 git commands to generate.