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