* Makefile.in (m68k-tdep.o, m68klinux-tdep.o): Update
[binutils-gdb.git] / gdb / m68k-tdep.c
1 /* Target dependent code for the Motorola 68000 series.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999, 2000, 2001,
3 2002, 2003
4 Free Software Foundation, Inc.
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,
21 Boston, MA 02111-1307, USA. */
22
23 #include "defs.h"
24 #include "frame.h"
25 #include "frame-base.h"
26 #include "frame-unwind.h"
27 #include "symtab.h"
28 #include "gdbcore.h"
29 #include "value.h"
30 #include "gdb_string.h"
31 #include "gdb_assert.h"
32 #include "inferior.h"
33 #include "regcache.h"
34 #include "arch-utils.h"
35 #include "osabi.h"
36
37 #include "m68k-tdep.h"
38 \f
39
40 #define P_LINKL_FP 0x480e
41 #define P_LINKW_FP 0x4e56
42 #define P_PEA_FP 0x4856
43 #define P_MOVEAL_SP_FP 0x2c4f
44 #define P_ADDAW_SP 0xdefc
45 #define P_ADDAL_SP 0xdffc
46 #define P_SUBQW_SP 0x514f
47 #define P_SUBQL_SP 0x518f
48 #define P_LEA_SP_SP 0x4fef
49 #define P_LEA_PC_A5 0x4bfb0170
50 #define P_FMOVEMX_SP 0xf227
51 #define P_MOVEL_SP 0x2f00
52 #define P_MOVEML_SP 0x48e7
53
54
55 #define REGISTER_BYTES_FP (16*4 + 8 + 8*12 + 3*4)
56 #define REGISTER_BYTES_NOFP (16*4 + 8)
57
58 /* Offset from SP to first arg on stack at first instruction of a function */
59 #define SP_ARG0 (1 * 4)
60
61 #if !defined (BPT_VECTOR)
62 #define BPT_VECTOR 0xf
63 #endif
64
65 #if !defined (REMOTE_BPT_VECTOR)
66 #define REMOTE_BPT_VECTOR 1
67 #endif
68
69
70 /* gdbarch_breakpoint_from_pc is set to m68k_local_breakpoint_from_pc
71 so m68k_remote_breakpoint_from_pc is currently not used. */
72
73 static const unsigned char *
74 m68k_remote_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
75 {
76 static unsigned char break_insn[] = {0x4e, (0x40 | REMOTE_BPT_VECTOR)};
77 *lenptr = sizeof (break_insn);
78 return break_insn;
79 }
80
81 static const unsigned char *
82 m68k_local_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
83 {
84 static unsigned char break_insn[] = {0x4e, (0x40 | BPT_VECTOR)};
85 *lenptr = sizeof (break_insn);
86 return break_insn;
87 }
88
89
90 static int
91 m68k_register_bytes_ok (long numbytes)
92 {
93 return ((numbytes == REGISTER_BYTES_FP)
94 || (numbytes == REGISTER_BYTES_NOFP));
95 }
96
97 /* Return the GDB type object for the "standard" data type of data in
98 register N. This should be int for D0-D7, SR, FPCONTROL and
99 FPSTATUS, long double for FP0-FP7, and void pointer for all others
100 (A0-A7, PC, FPIADDR). Note, for registers which contain
101 addresses return pointer to void, not pointer to char, because we
102 don't want to attempt to print the string after printing the
103 address. */
104
105 static struct type *
106 m68k_register_type (struct gdbarch *gdbarch, int regnum)
107 {
108 if (regnum >= FP0_REGNUM && regnum <= FP0_REGNUM + 7)
109 return builtin_type_m68881_ext;
110
111 if (regnum == M68K_FPI_REGNUM || regnum == PC_REGNUM)
112 return builtin_type_void_func_ptr;
113
114 if (regnum == M68K_FPC_REGNUM || regnum == M68K_FPS_REGNUM
115 || regnum == PS_REGNUM)
116 return builtin_type_int32;
117
118 if (regnum >= M68K_A0_REGNUM && regnum <= M68K_A0_REGNUM + 7)
119 return builtin_type_void_data_ptr;
120
121 return builtin_type_int32;
122 }
123
124 /* Function: m68k_register_name
125 Returns the name of the standard m68k register regnum. */
126
127 static const char *
128 m68k_register_name (int regnum)
129 {
130 static char *register_names[] = {
131 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
132 "a0", "a1", "a2", "a3", "a4", "a5", "fp", "sp",
133 "ps", "pc",
134 "fp0", "fp1", "fp2", "fp3", "fp4", "fp5", "fp6", "fp7",
135 "fpcontrol", "fpstatus", "fpiaddr", "fpcode", "fpflags"
136 };
137
138 if (regnum < 0 ||
139 regnum >= sizeof (register_names) / sizeof (register_names[0]))
140 internal_error (__FILE__, __LINE__,
141 "m68k_register_name: illegal register number %d", regnum);
142 else
143 return register_names[regnum];
144 }
145
146 /* Index within `registers' of the first byte of the space for
147 register regnum. */
148
149 static int
150 m68k_register_byte (int regnum)
151 {
152 if (regnum >= M68K_FPC_REGNUM)
153 return (((regnum - M68K_FPC_REGNUM) * 4) + 168);
154 else if (regnum >= FP0_REGNUM)
155 return (((regnum - FP0_REGNUM) * 12) + 72);
156 else
157 return (regnum * 4);
158 }
159 \f
160 /* Extract from an array REGBUF containing the (raw) register state, a
161 function return value of TYPE, and copy that, in virtual format,
162 into VALBUF. */
163
164 static void
165 m68k_extract_return_value (struct type *type, struct regcache *regcache,
166 void *valbuf)
167 {
168 int len = TYPE_LENGTH (type);
169 char buf[M68K_MAX_REGISTER_SIZE];
170
171 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
172 && TYPE_NFIELDS (type) == 1)
173 {
174 m68k_extract_return_value (TYPE_FIELD_TYPE (type, 0), regcache, valbuf);
175 return;
176 }
177
178 if (len <= 4)
179 {
180 regcache_raw_read (regcache, M68K_D0_REGNUM, buf);
181 memcpy (valbuf, buf + (4 - len), len);
182 }
183 else if (len <= 8)
184 {
185 regcache_raw_read (regcache, M68K_D0_REGNUM, buf);
186 memcpy (valbuf, buf + (8 - len), len - 4);
187 regcache_raw_read (regcache, M68K_D1_REGNUM,
188 (char *) valbuf + (len - 4));
189 }
190 else
191 internal_error (__FILE__, __LINE__,
192 "Cannot extract return value of %d bytes long.", len);
193 }
194
195 /* Write into the appropriate registers a function return value stored
196 in VALBUF of type TYPE, given in virtual format. */
197
198 static void
199 m68k_store_return_value (struct type *type, struct regcache *regcache,
200 const void *valbuf)
201 {
202 int len = TYPE_LENGTH (type);
203
204 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
205 && TYPE_NFIELDS (type) == 1)
206 {
207 m68k_store_return_value (TYPE_FIELD_TYPE (type, 0), regcache, valbuf);
208 return;
209 }
210
211 if (len <= 4)
212 regcache_raw_write_part (regcache, M68K_D0_REGNUM, 4 - len, len, valbuf);
213 else if (len <= 8)
214 {
215 regcache_raw_write_part (regcache, M68K_D1_REGNUM, 8 - len,
216 len - 4, valbuf);
217 regcache_raw_write (regcache, M68K_D0_REGNUM,
218 (char *) valbuf + (len - 4));
219 }
220 else
221 internal_error (__FILE__, __LINE__,
222 "Cannot store return value of %d bytes long.", len);
223 }
224
225 /* Extract from REGCACHE, which contains the (raw) register state, the
226 address in which a function should return its structure value, as a
227 CORE_ADDR. */
228
229 static CORE_ADDR
230 m68k_extract_struct_value_address (struct regcache *regcache)
231 {
232 char buf[4];
233
234 regcache_cooked_read (regcache, M68K_D0_REGNUM, buf);
235 return extract_unsigned_integer (buf, 4);
236 }
237
238 /* A function that tells us whether the function invocation represented
239 by fi does not have a frame on the stack associated with it. If it
240 does not, FRAMELESS is set to 1, else 0. */
241
242 static int
243 m68k_frameless_function_invocation (struct frame_info *fi)
244 {
245 if (get_frame_type (fi) == SIGTRAMP_FRAME)
246 return 0;
247 else
248 return frameless_look_for_prologue (fi);
249 }
250
251 int
252 delta68_in_sigtramp (CORE_ADDR pc, char *name)
253 {
254 if (name != NULL)
255 return strcmp (name, "_sigcode") == 0;
256 else
257 return 0;
258 }
259
260 CORE_ADDR
261 delta68_frame_args_address (struct frame_info *frame_info)
262 {
263 /* we assume here that the only frameless functions are the system calls
264 or other functions who do not put anything on the stack. */
265 if (get_frame_type (frame_info) == SIGTRAMP_FRAME)
266 return get_frame_base (frame_info) + 12;
267 else if (frameless_look_for_prologue (frame_info))
268 {
269 /* Check for an interrupted system call */
270 if (get_next_frame (frame_info) && (get_frame_type (get_next_frame (frame_info)) == SIGTRAMP_FRAME))
271 return get_frame_base (get_next_frame (frame_info)) + 16;
272 else
273 return get_frame_base (frame_info) + 4;
274 }
275 else
276 return get_frame_base (frame_info);
277 }
278
279 CORE_ADDR
280 delta68_frame_saved_pc (struct frame_info *frame_info)
281 {
282 return read_memory_unsigned_integer (delta68_frame_args_address (frame_info)
283 + 4, 4);
284 }
285
286 int
287 delta68_frame_num_args (struct frame_info *fi)
288 {
289 int val;
290 CORE_ADDR pc = DEPRECATED_FRAME_SAVED_PC (fi);
291 int insn = read_memory_unsigned_integer (pc, 2);
292 val = 0;
293 if (insn == 0047757 || insn == 0157374) /* lea W(sp),sp or addaw #W,sp */
294 val = read_memory_integer (pc + 2, 2);
295 else if ((insn & 0170777) == 0050217 /* addql #N, sp */
296 || (insn & 0170777) == 0050117) /* addqw */
297 {
298 val = (insn >> 9) & 7;
299 if (val == 0)
300 val = 8;
301 }
302 else if (insn == 0157774) /* addal #WW, sp */
303 val = read_memory_integer (pc + 2, 4);
304 val >>= 2;
305 return val;
306 }
307
308 static CORE_ADDR
309 m68k_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr,
310 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
311 struct value **args, CORE_ADDR sp, int struct_return,
312 CORE_ADDR struct_addr)
313 {
314 char buf[4];
315 int i;
316
317 /* Push arguments in reverse order. */
318 for (i = nargs - 1; i >= 0; i--)
319 {
320 int len = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (args[i]));
321 int container_len = (len + 3) & ~3;
322 int offset = container_len - len;
323
324 sp -= container_len;
325 write_memory (sp + offset, VALUE_CONTENTS_ALL (args[i]), len);
326 }
327
328 /* Push value address. */
329 if (struct_return)
330 {
331 sp -= 4;
332 store_unsigned_integer (buf, 4, struct_addr);
333 write_memory (sp, buf, 4);
334 }
335
336 /* Store return address. */
337 sp -= 4;
338 store_unsigned_integer (buf, 4, bp_addr);
339 write_memory (sp, buf, 4);
340
341 /* Finally, update the stack pointer... */
342 store_unsigned_integer (buf, 4, sp);
343 regcache_cooked_write (regcache, M68K_SP_REGNUM, buf);
344
345 /* ...and fake a frame pointer. */
346 regcache_cooked_write (regcache, M68K_FP_REGNUM, buf);
347
348 /* DWARF2/GCC uses the stack address *before* the function call as a
349 frame's CFA. */
350 return sp + 8;
351 }
352 \f
353 struct m68k_frame_cache
354 {
355 /* Base address. */
356 CORE_ADDR base;
357 CORE_ADDR sp_offset;
358 CORE_ADDR pc;
359
360 /* Saved registers. */
361 CORE_ADDR saved_regs[M68K_NUM_REGS];
362 CORE_ADDR saved_sp;
363
364 /* Stack space reserved for local variables. */
365 long locals;
366 };
367
368 /* Allocate and initialize a frame cache. */
369
370 static struct m68k_frame_cache *
371 m68k_alloc_frame_cache (void)
372 {
373 struct m68k_frame_cache *cache;
374 int i;
375
376 cache = FRAME_OBSTACK_ZALLOC (struct m68k_frame_cache);
377
378 /* Base address. */
379 cache->base = 0;
380 cache->sp_offset = -4;
381 cache->pc = 0;
382
383 /* Saved registers. We initialize these to -1 since zero is a valid
384 offset (that's where %fp is supposed to be stored). */
385 for (i = 0; i < M68K_NUM_REGS; i++)
386 cache->saved_regs[i] = -1;
387
388 /* Frameless until proven otherwise. */
389 cache->locals = -1;
390
391 return cache;
392 }
393
394 /* Check whether PC points at a code that sets up a new stack frame.
395 If so, it updates CACHE and returns the address of the first
396 instruction after the sequence that sets removes the "hidden"
397 argument from the stack or CURRENT_PC, whichever is smaller.
398 Otherwise, return PC. */
399
400 static CORE_ADDR
401 m68k_analyze_frame_setup (CORE_ADDR pc, CORE_ADDR current_pc,
402 struct m68k_frame_cache *cache)
403 {
404 int op;
405
406 if (pc >= current_pc)
407 return current_pc;
408
409 op = read_memory_unsigned_integer (pc, 2);
410
411 if (op == P_LINKW_FP || op == P_LINKL_FP || op == P_PEA_FP)
412 {
413 cache->saved_regs[M68K_FP_REGNUM] = 0;
414 cache->sp_offset += 4;
415 if (op == P_LINKW_FP)
416 {
417 /* link.w %fp, #-N */
418 /* link.w %fp, #0; adda.l #-N, %sp */
419 cache->locals = -read_memory_integer (pc + 2, 2);
420
421 if (pc + 4 < current_pc && cache->locals == 0)
422 {
423 op = read_memory_unsigned_integer (pc + 4, 2);
424 if (op == P_ADDAL_SP)
425 {
426 cache->locals = read_memory_integer (pc + 6, 4);
427 return pc + 10;
428 }
429 }
430
431 return pc + 4;
432 }
433 else if (op == P_LINKL_FP)
434 {
435 /* link.l %fp, #-N */
436 cache->locals = -read_memory_integer (pc + 2, 4);
437 return pc + 6;
438 }
439 else
440 {
441 /* pea (%fp); movea.l %sp, %fp */
442 cache->locals = 0;
443
444 if (pc + 2 < current_pc)
445 {
446 op = read_memory_unsigned_integer (pc + 2, 2);
447
448 if (op == P_MOVEAL_SP_FP)
449 {
450 /* move.l %sp, %fp */
451 return pc + 4;
452 }
453 }
454
455 return pc + 2;
456 }
457 }
458 else if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
459 {
460 /* subq.[wl] #N,%sp */
461 /* subq.[wl] #8,%sp; subq.[wl] #N,%sp */
462 cache->locals = (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
463 if (pc + 2 < current_pc)
464 {
465 op = read_memory_unsigned_integer (pc + 2, 2);
466 if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
467 {
468 cache->locals += (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
469 return pc + 4;
470 }
471 }
472 return pc + 2;
473 }
474 else if (op == P_ADDAW_SP || op == P_LEA_SP_SP)
475 {
476 /* adda.w #-N,%sp */
477 /* lea (-N,%sp),%sp */
478 cache->locals = -read_memory_integer (pc + 2, 2);
479 return pc + 4;
480 }
481 else if (op == P_ADDAL_SP)
482 {
483 /* adda.l #-N,%sp */
484 cache->locals = -read_memory_integer (pc + 2, 4);
485 return pc + 6;
486 }
487
488 return pc;
489 }
490
491 /* Check whether PC points at code that saves registers on the stack.
492 If so, it updates CACHE and returns the address of the first
493 instruction after the register saves or CURRENT_PC, whichever is
494 smaller. Otherwise, return PC. */
495
496 static CORE_ADDR
497 m68k_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
498 struct m68k_frame_cache *cache)
499 {
500 if (cache->locals >= 0)
501 {
502 CORE_ADDR offset;
503 int op;
504 int i, mask, regno;
505
506 offset = -4 - cache->locals;
507 while (pc < current_pc)
508 {
509 op = read_memory_unsigned_integer (pc, 2);
510 if (op == P_FMOVEMX_SP)
511 {
512 /* fmovem.x REGS,-(%sp) */
513 op = read_memory_unsigned_integer (pc + 2, 2);
514 if ((op & 0xff00) == 0xe000)
515 {
516 mask = op & 0xff;
517 for (i = 0; i < 16; i++, mask >>= 1)
518 {
519 if (mask & 1)
520 {
521 cache->saved_regs[i + M68K_FP0_REGNUM] = offset;
522 offset -= 12;
523 }
524 }
525 pc += 4;
526 }
527 else
528 break;
529 }
530 else if ((op & 0170677) == P_MOVEL_SP)
531 {
532 /* move.l %R,-(%sp) */
533 regno = ((op & 07000) >> 9) | ((op & 0100) >> 3);
534 cache->saved_regs[regno] = offset;
535 offset -= 4;
536 pc += 2;
537 }
538 else if (op == P_MOVEML_SP)
539 {
540 /* movem.l REGS,-(%sp) */
541 mask = read_memory_unsigned_integer (pc + 2, 2);
542 for (i = 0; i < 16; i++, mask >>= 1)
543 {
544 if (mask & 1)
545 {
546 cache->saved_regs[15 - i] = offset;
547 offset -= 4;
548 }
549 }
550 pc += 4;
551 }
552 else
553 break;
554 }
555 }
556
557 return pc;
558 }
559
560
561 /* Do a full analysis of the prologue at PC and update CACHE
562 accordingly. Bail out early if CURRENT_PC is reached. Return the
563 address where the analysis stopped.
564
565 We handle all cases that can be generated by gcc.
566
567 For allocating a stack frame:
568
569 link.w %a6,#-N
570 link.l %a6,#-N
571 pea (%fp); move.l %sp,%fp
572 link.w %a6,#0; add.l #-N,%sp
573 subq.l #N,%sp
574 subq.w #N,%sp
575 subq.w #8,%sp; subq.w #N-8,%sp
576 add.w #-N,%sp
577 lea (-N,%sp),%sp
578 add.l #-N,%sp
579
580 For saving registers:
581
582 fmovem.x REGS,-(%sp)
583 move.l R1,-(%sp)
584 move.l R1,-(%sp); move.l R2,-(%sp)
585 movem.l REGS,-(%sp)
586
587 For setting up the PIC register:
588
589 lea (%pc,N),%a5
590
591 */
592
593 static CORE_ADDR
594 m68k_analyze_prologue (CORE_ADDR pc, CORE_ADDR current_pc,
595 struct m68k_frame_cache *cache)
596 {
597 unsigned int op;
598
599 pc = m68k_analyze_frame_setup (pc, current_pc, cache);
600 pc = m68k_analyze_register_saves (pc, current_pc, cache);
601 if (pc >= current_pc)
602 return current_pc;
603
604 /* Check for GOT setup. */
605 op = read_memory_unsigned_integer (pc, 4);
606 if (op == P_LEA_PC_A5)
607 {
608 /* lea (%pc,N),%a5 */
609 return pc + 6;
610 }
611
612 return pc;
613 }
614
615 /* Return PC of first real instruction. */
616
617 static CORE_ADDR
618 m68k_skip_prologue (CORE_ADDR start_pc)
619 {
620 struct m68k_frame_cache cache;
621 CORE_ADDR pc;
622 int op;
623
624 cache.locals = -1;
625 pc = m68k_analyze_prologue (start_pc, (CORE_ADDR) -1, &cache);
626 if (cache.locals < 0)
627 return start_pc;
628 return pc;
629 }
630
631 static CORE_ADDR
632 m68k_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
633 {
634 char buf[8];
635
636 frame_unwind_register (next_frame, PC_REGNUM, buf);
637 return extract_typed_address (buf, builtin_type_void_func_ptr);
638 }
639 \f
640 /* Normal frames. */
641
642 static struct m68k_frame_cache *
643 m68k_frame_cache (struct frame_info *next_frame, void **this_cache)
644 {
645 struct m68k_frame_cache *cache;
646 char buf[4];
647 int i;
648
649 if (*this_cache)
650 return *this_cache;
651
652 cache = m68k_alloc_frame_cache ();
653 *this_cache = cache;
654
655 /* In principle, for normal frames, %fp holds the frame pointer,
656 which holds the base address for the current stack frame.
657 However, for functions that don't need it, the frame pointer is
658 optional. For these "frameless" functions the frame pointer is
659 actually the frame pointer of the calling frame. Signal
660 trampolines are just a special case of a "frameless" function.
661 They (usually) share their frame pointer with the frame that was
662 in progress when the signal occurred. */
663
664 frame_unwind_register (next_frame, M68K_FP_REGNUM, buf);
665 cache->base = extract_unsigned_integer (buf, 4);
666 if (cache->base == 0)
667 return cache;
668
669 /* For normal frames, %pc is stored at 4(%fp). */
670 cache->saved_regs[M68K_PC_REGNUM] = 4;
671
672 cache->pc = frame_func_unwind (next_frame);
673 if (cache->pc != 0)
674 m68k_analyze_prologue (cache->pc, frame_pc_unwind (next_frame), cache);
675
676 if (cache->locals < 0)
677 {
678 /* We didn't find a valid frame, which means that CACHE->base
679 currently holds the frame pointer for our calling frame. If
680 we're at the start of a function, or somewhere half-way its
681 prologue, the function's frame probably hasn't been fully
682 setup yet. Try to reconstruct the base address for the stack
683 frame by looking at the stack pointer. For truly "frameless"
684 functions this might work too. */
685
686 frame_unwind_register (next_frame, M68K_SP_REGNUM, buf);
687 cache->base = extract_unsigned_integer (buf, 4) + cache->sp_offset;
688 }
689
690 /* Now that we have the base address for the stack frame we can
691 calculate the value of %sp in the calling frame. */
692 cache->saved_sp = cache->base + 8;
693
694 /* Adjust all the saved registers such that they contain addresses
695 instead of offsets. */
696 for (i = 0; i < M68K_NUM_REGS; i++)
697 if (cache->saved_regs[i] != -1)
698 cache->saved_regs[i] += cache->base;
699
700 return cache;
701 }
702
703 static void
704 m68k_frame_this_id (struct frame_info *next_frame, void **this_cache,
705 struct frame_id *this_id)
706 {
707 struct m68k_frame_cache *cache = m68k_frame_cache (next_frame, this_cache);
708
709 /* This marks the outermost frame. */
710 if (cache->base == 0)
711 return;
712
713 /* See the end of m68k_push_dummy_call. */
714 *this_id = frame_id_build (cache->base + 8, cache->pc);
715 }
716
717 static void
718 m68k_frame_prev_register (struct frame_info *next_frame, void **this_cache,
719 int regnum, int *optimizedp,
720 enum lval_type *lvalp, CORE_ADDR *addrp,
721 int *realnump, void *valuep)
722 {
723 struct m68k_frame_cache *cache = m68k_frame_cache (next_frame, this_cache);
724
725 gdb_assert (regnum >= 0);
726
727 if (regnum == M68K_SP_REGNUM && cache->saved_sp)
728 {
729 *optimizedp = 0;
730 *lvalp = not_lval;
731 *addrp = 0;
732 *realnump = -1;
733 if (valuep)
734 {
735 /* Store the value. */
736 store_unsigned_integer (valuep, 4, cache->saved_sp);
737 }
738 return;
739 }
740
741 if (regnum < M68K_NUM_REGS && cache->saved_regs[regnum] != -1)
742 {
743 *optimizedp = 0;
744 *lvalp = lval_memory;
745 *addrp = cache->saved_regs[regnum];
746 *realnump = -1;
747 if (valuep)
748 {
749 /* Read the value in from memory. */
750 read_memory (*addrp, valuep,
751 register_size (current_gdbarch, regnum));
752 }
753 return;
754 }
755
756 frame_register_unwind (next_frame, regnum,
757 optimizedp, lvalp, addrp, realnump, valuep);
758 }
759
760 static const struct frame_unwind m68k_frame_unwind =
761 {
762 NORMAL_FRAME,
763 m68k_frame_this_id,
764 m68k_frame_prev_register
765 };
766
767 static const struct frame_unwind *
768 m68k_frame_p (CORE_ADDR pc)
769 {
770 return &m68k_frame_unwind;
771 }
772 \f
773 /* Signal trampolines. */
774
775 static struct m68k_frame_cache *
776 m68k_sigtramp_frame_cache (struct frame_info *next_frame, void **this_cache)
777 {
778 struct m68k_frame_cache *cache;
779 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
780 struct m68k_sigtramp_info info;
781 char buf[4];
782 int i;
783
784 if (*this_cache)
785 return *this_cache;
786
787 cache = m68k_alloc_frame_cache ();
788
789 frame_unwind_register (next_frame, M68K_SP_REGNUM, buf);
790 cache->base = extract_unsigned_integer (buf, 4) - 4;
791
792 info = tdep->get_sigtramp_info (next_frame);
793
794 for (i = 0; i < M68K_NUM_REGS; i++)
795 if (info.sc_reg_offset[i] != -1)
796 cache->saved_regs[i] = info.sigcontext_addr + info.sc_reg_offset[i];
797
798 *this_cache = cache;
799 return cache;
800 }
801
802 static void
803 m68k_sigtramp_frame_this_id (struct frame_info *next_frame, void **this_cache,
804 struct frame_id *this_id)
805 {
806 struct m68k_frame_cache *cache =
807 m68k_sigtramp_frame_cache (next_frame, this_cache);
808
809 /* See the end of m68k_push_dummy_call. */
810 *this_id = frame_id_build (cache->base + 8, frame_pc_unwind (next_frame));
811 }
812
813 static void
814 m68k_sigtramp_frame_prev_register (struct frame_info *next_frame,
815 void **this_cache,
816 int regnum, int *optimizedp,
817 enum lval_type *lvalp, CORE_ADDR *addrp,
818 int *realnump, void *valuep)
819 {
820 /* Make sure we've initialized the cache. */
821 m68k_sigtramp_frame_cache (next_frame, this_cache);
822
823 m68k_frame_prev_register (next_frame, this_cache, regnum,
824 optimizedp, lvalp, addrp, realnump, valuep);
825 }
826
827 static const struct frame_unwind m68k_sigtramp_frame_unwind =
828 {
829 SIGTRAMP_FRAME,
830 m68k_sigtramp_frame_this_id,
831 m68k_sigtramp_frame_prev_register
832 };
833
834 static const struct frame_unwind *
835 m68k_sigtramp_frame_p (CORE_ADDR pc)
836 {
837 char *name;
838
839 /* We shouldn't even bother to try if the OSABI didn't register
840 a get_sigtramp_info handler. */
841 if (!gdbarch_tdep (current_gdbarch)->get_sigtramp_info)
842 return NULL;
843
844 find_pc_partial_function (pc, &name, NULL, NULL);
845 if (PC_IN_SIGTRAMP (pc, name))
846 return &m68k_sigtramp_frame_unwind;
847
848 return NULL;
849 }
850 \f
851 static CORE_ADDR
852 m68k_frame_base_address (struct frame_info *next_frame, void **this_cache)
853 {
854 struct m68k_frame_cache *cache = m68k_frame_cache (next_frame, this_cache);
855
856 return cache->base;
857 }
858
859 static const struct frame_base m68k_frame_base =
860 {
861 &m68k_frame_unwind,
862 m68k_frame_base_address,
863 m68k_frame_base_address,
864 m68k_frame_base_address
865 };
866
867 static struct frame_id
868 m68k_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
869 {
870 char buf[4];
871 CORE_ADDR fp;
872
873 frame_unwind_register (next_frame, M68K_FP_REGNUM, buf);
874 fp = extract_unsigned_integer (buf, 4);
875
876 /* See the end of m68k_push_dummy_call. */
877 return frame_id_build (fp + 8, frame_pc_unwind (next_frame));
878 }
879 \f
880 #ifdef USE_PROC_FS /* Target dependent support for /proc */
881
882 #include <sys/procfs.h>
883
884 /* Prototypes for supply_gregset etc. */
885 #include "gregset.h"
886
887 /* The /proc interface divides the target machine's register set up into
888 two different sets, the general register set (gregset) and the floating
889 point register set (fpregset). For each set, there is an ioctl to get
890 the current register set and another ioctl to set the current values.
891
892 The actual structure passed through the ioctl interface is, of course,
893 naturally machine dependent, and is different for each set of registers.
894 For the m68k for example, the general register set is typically defined
895 by:
896
897 typedef int gregset_t[18];
898
899 #define R_D0 0
900 ...
901 #define R_PS 17
902
903 and the floating point set by:
904
905 typedef struct fpregset {
906 int f_pcr;
907 int f_psr;
908 int f_fpiaddr;
909 int f_fpregs[8][3]; (8 regs, 96 bits each)
910 } fpregset_t;
911
912 These routines provide the packing and unpacking of gregset_t and
913 fpregset_t formatted data.
914
915 */
916
917 /* Atari SVR4 has R_SR but not R_PS */
918
919 #if !defined (R_PS) && defined (R_SR)
920 #define R_PS R_SR
921 #endif
922
923 /* Given a pointer to a general register set in /proc format (gregset_t *),
924 unpack the register contents and supply them as gdb's idea of the current
925 register values. */
926
927 void
928 supply_gregset (gregset_t *gregsetp)
929 {
930 register int regi;
931 register greg_t *regp = (greg_t *) gregsetp;
932
933 for (regi = 0; regi < R_PC; regi++)
934 {
935 supply_register (regi, (char *) (regp + regi));
936 }
937 supply_register (PS_REGNUM, (char *) (regp + R_PS));
938 supply_register (PC_REGNUM, (char *) (regp + R_PC));
939 }
940
941 void
942 fill_gregset (gregset_t *gregsetp, int regno)
943 {
944 register int regi;
945 register greg_t *regp = (greg_t *) gregsetp;
946
947 for (regi = 0; regi < R_PC; regi++)
948 {
949 if (regno == -1 || regno == regi)
950 regcache_collect (regi, regp + regi);
951 }
952 if (regno == -1 || regno == PS_REGNUM)
953 regcache_collect (PS_REGNUM, regp + R_PS);
954 if (regno == -1 || regno == PC_REGNUM)
955 regcache_collect (PC_REGNUM, regp + R_PC);
956 }
957
958 #if defined (FP0_REGNUM)
959
960 /* Given a pointer to a floating point register set in /proc format
961 (fpregset_t *), unpack the register contents and supply them as gdb's
962 idea of the current floating point register values. */
963
964 void
965 supply_fpregset (fpregset_t *fpregsetp)
966 {
967 register int regi;
968 char *from;
969
970 for (regi = FP0_REGNUM; regi < M68K_FPC_REGNUM; regi++)
971 {
972 from = (char *) &(fpregsetp->f_fpregs[regi - FP0_REGNUM][0]);
973 supply_register (regi, from);
974 }
975 supply_register (M68K_FPC_REGNUM, (char *) &(fpregsetp->f_pcr));
976 supply_register (M68K_FPS_REGNUM, (char *) &(fpregsetp->f_psr));
977 supply_register (M68K_FPI_REGNUM, (char *) &(fpregsetp->f_fpiaddr));
978 }
979
980 /* Given a pointer to a floating point register set in /proc format
981 (fpregset_t *), update the register specified by REGNO from gdb's idea
982 of the current floating point register set. If REGNO is -1, update
983 them all. */
984
985 void
986 fill_fpregset (fpregset_t *fpregsetp, int regno)
987 {
988 int regi;
989
990 for (regi = FP0_REGNUM; regi < M68K_FPC_REGNUM; regi++)
991 {
992 if (regno == -1 || regno == regi)
993 regcache_collect (regi, &fpregsetp->f_fpregs[regi - FP0_REGNUM][0]);
994 }
995 if (regno == -1 || regno == M68K_FPC_REGNUM)
996 regcache_collect (M68K_FPC_REGNUM, &fpregsetp->f_pcr);
997 if (regno == -1 || regno == M68K_FPS_REGNUM)
998 regcache_collect (M68K_FPS_REGNUM, &fpregsetp->f_psr);
999 if (regno == -1 || regno == M68K_FPI_REGNUM)
1000 regcache_collect (M68K_FPI_REGNUM, &fpregsetp->f_fpiaddr);
1001 }
1002
1003 #endif /* defined (FP0_REGNUM) */
1004
1005 #endif /* USE_PROC_FS */
1006
1007 /* Figure out where the longjmp will land. Slurp the args out of the stack.
1008 We expect the first arg to be a pointer to the jmp_buf structure from which
1009 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
1010 This routine returns true on success. */
1011
1012 int
1013 m68k_get_longjmp_target (CORE_ADDR *pc)
1014 {
1015 char *buf;
1016 CORE_ADDR sp, jb_addr;
1017 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1018
1019 if (tdep->jb_pc < 0)
1020 {
1021 internal_error (__FILE__, __LINE__,
1022 "m68k_get_longjmp_target: not implemented");
1023 return 0;
1024 }
1025
1026 buf = alloca (TARGET_PTR_BIT / TARGET_CHAR_BIT);
1027 sp = read_register (SP_REGNUM);
1028
1029 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
1030 buf, TARGET_PTR_BIT / TARGET_CHAR_BIT))
1031 return 0;
1032
1033 jb_addr = extract_unsigned_integer (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
1034
1035 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
1036 TARGET_PTR_BIT / TARGET_CHAR_BIT))
1037 return 0;
1038
1039 *pc = extract_unsigned_integer (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
1040 return 1;
1041 }
1042
1043 #ifdef SYSCALL_TRAP
1044 /* Immediately after a function call, return the saved pc before the frame
1045 is setup. For sun3's, we check for the common case of being inside of a
1046 system call, and if so, we know that Sun pushes the call # on the stack
1047 prior to doing the trap. */
1048
1049 static CORE_ADDR
1050 m68k_saved_pc_after_call (struct frame_info *frame)
1051 {
1052 int op;
1053
1054 op = read_memory_unsigned_integer (frame->pc - SYSCALL_TRAP_OFFSET, 2);
1055
1056 if (op == SYSCALL_TRAP)
1057 return read_memory_unsigned_integer (read_register (SP_REGNUM) + 4, 4);
1058 else
1059 return read_memory_unsigned_integer (read_register (SP_REGNUM), 4);
1060 }
1061 #endif /* SYSCALL_TRAP */
1062
1063 /* Function: m68k_gdbarch_init
1064 Initializer function for the m68k gdbarch vector.
1065 Called by gdbarch. Sets up the gdbarch vector(s) for this target. */
1066
1067 static struct gdbarch *
1068 m68k_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1069 {
1070 struct gdbarch_tdep *tdep = NULL;
1071 struct gdbarch *gdbarch;
1072
1073 /* find a candidate among the list of pre-declared architectures. */
1074 arches = gdbarch_list_lookup_by_info (arches, &info);
1075 if (arches != NULL)
1076 return (arches->gdbarch);
1077
1078 tdep = xmalloc (sizeof (struct gdbarch_tdep));
1079 gdbarch = gdbarch_alloc (&info, tdep);
1080
1081 set_gdbarch_long_double_format (gdbarch, &floatformat_m68881_ext);
1082 set_gdbarch_long_double_bit (gdbarch, 96);
1083
1084 set_gdbarch_function_start_offset (gdbarch, 0);
1085
1086 set_gdbarch_skip_prologue (gdbarch, m68k_skip_prologue);
1087 #ifdef SYSCALL_TRAP
1088 set_gdbarch_deprecated_saved_pc_after_call (gdbarch, m68k_saved_pc_after_call);
1089 #endif
1090 set_gdbarch_breakpoint_from_pc (gdbarch, m68k_local_breakpoint_from_pc);
1091
1092 /* Stack grows down. */
1093 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1094 set_gdbarch_parm_boundary (gdbarch, 32);
1095
1096 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
1097 set_gdbarch_decr_pc_after_break (gdbarch, 2);
1098
1099 set_gdbarch_extract_return_value (gdbarch, m68k_extract_return_value);
1100 set_gdbarch_store_return_value (gdbarch, m68k_store_return_value);
1101 set_gdbarch_extract_struct_value_address (gdbarch,
1102 m68k_extract_struct_value_address);
1103
1104 set_gdbarch_frameless_function_invocation (gdbarch,
1105 m68k_frameless_function_invocation);
1106 set_gdbarch_frame_args_skip (gdbarch, 8);
1107
1108 set_gdbarch_register_type (gdbarch, m68k_register_type);
1109 set_gdbarch_register_name (gdbarch, m68k_register_name);
1110 set_gdbarch_num_regs (gdbarch, 29);
1111 set_gdbarch_register_bytes_ok (gdbarch, m68k_register_bytes_ok);
1112 set_gdbarch_sp_regnum (gdbarch, M68K_SP_REGNUM);
1113 set_gdbarch_pc_regnum (gdbarch, M68K_PC_REGNUM);
1114 set_gdbarch_ps_regnum (gdbarch, M68K_PS_REGNUM);
1115 set_gdbarch_fp0_regnum (gdbarch, M68K_FP0_REGNUM);
1116
1117 set_gdbarch_push_dummy_call (gdbarch, m68k_push_dummy_call);
1118
1119 /* Disassembler. */
1120 set_gdbarch_print_insn (gdbarch, print_insn_m68k);
1121
1122 #if defined JB_PC && defined JB_ELEMENT_SIZE
1123 tdep->jb_pc = JB_PC;
1124 tdep->jb_elt_size = JB_ELEMENT_SIZE;
1125 #else
1126 tdep->jb_pc = -1;
1127 #endif
1128 tdep->get_sigtramp_info = NULL;
1129
1130 /* Frame unwinder. */
1131 set_gdbarch_unwind_dummy_id (gdbarch, m68k_unwind_dummy_id);
1132 set_gdbarch_unwind_pc (gdbarch, m68k_unwind_pc);
1133 frame_base_set_default (gdbarch, &m68k_frame_base);
1134
1135 /* Hook in ABI-specific overrides, if they have been registered. */
1136 gdbarch_init_osabi (info, gdbarch);
1137
1138 /* Now we have tuned the configuration, set a few final things,
1139 based on what the OS ABI has told us. */
1140
1141 if (tdep->jb_pc >= 0)
1142 set_gdbarch_get_longjmp_target (gdbarch, m68k_get_longjmp_target);
1143
1144 frame_unwind_append_predicate (gdbarch, m68k_sigtramp_frame_p);
1145 frame_unwind_append_predicate (gdbarch, m68k_frame_p);
1146
1147 return gdbarch;
1148 }
1149
1150
1151 static void
1152 m68k_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
1153 {
1154 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1155
1156 if (tdep == NULL)
1157 return;
1158 }
1159
1160 extern initialize_file_ftype _initialize_m68k_tdep; /* -Wmissing-prototypes */
1161
1162 void
1163 _initialize_m68k_tdep (void)
1164 {
1165 gdbarch_register (bfd_arch_m68k, m68k_gdbarch_init, m68k_dump_tdep);
1166 }