2007-06-13 Markus Deuling <deuling@de.ibm.com>
[deliverable/binutils-gdb.git] / gdb / m68k-tdep.c
1 /* Target-dependent code for the Motorola 68000 series.
2
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007 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., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 #include "defs.h"
24 #include "dwarf2-frame.h"
25 #include "frame.h"
26 #include "frame-base.h"
27 #include "frame-unwind.h"
28 #include "gdbtypes.h"
29 #include "symtab.h"
30 #include "gdbcore.h"
31 #include "value.h"
32 #include "gdb_string.h"
33 #include "gdb_assert.h"
34 #include "inferior.h"
35 #include "regcache.h"
36 #include "arch-utils.h"
37 #include "osabi.h"
38 #include "dis-asm.h"
39
40 #include "m68k-tdep.h"
41 \f
42
43 #define P_LINKL_FP 0x480e
44 #define P_LINKW_FP 0x4e56
45 #define P_PEA_FP 0x4856
46 #define P_MOVEAL_SP_FP 0x2c4f
47 #define P_ADDAW_SP 0xdefc
48 #define P_ADDAL_SP 0xdffc
49 #define P_SUBQW_SP 0x514f
50 #define P_SUBQL_SP 0x518f
51 #define P_LEA_SP_SP 0x4fef
52 #define P_LEA_PC_A5 0x4bfb0170
53 #define P_FMOVEMX_SP 0xf227
54 #define P_MOVEL_SP 0x2f00
55 #define P_MOVEML_SP 0x48e7
56
57 /* Offset from SP to first arg on stack at first instruction of a function */
58 #define SP_ARG0 (1 * 4)
59
60 #if !defined (BPT_VECTOR)
61 #define BPT_VECTOR 0xf
62 #endif
63
64 static const gdb_byte *
65 m68k_local_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
66 {
67 static gdb_byte break_insn[] = {0x4e, (0x40 | BPT_VECTOR)};
68 *lenptr = sizeof (break_insn);
69 return break_insn;
70 }
71
72 /* Return the GDB type object for the "standard" data type of data in
73 register N. This should be int for D0-D7, SR, FPCONTROL and
74 FPSTATUS, long double for FP0-FP7, and void pointer for all others
75 (A0-A7, PC, FPIADDR). Note, for registers which contain
76 addresses return pointer to void, not pointer to char, because we
77 don't want to attempt to print the string after printing the
78 address. */
79
80 static struct type *
81 m68k_register_type (struct gdbarch *gdbarch, int regnum)
82 {
83 if (regnum >= FP0_REGNUM && regnum <= FP0_REGNUM + 7)
84 return builtin_type_m68881_ext;
85
86 if (regnum == M68K_FPI_REGNUM || regnum == PC_REGNUM)
87 return builtin_type_void_func_ptr;
88
89 if (regnum == M68K_FPC_REGNUM || regnum == M68K_FPS_REGNUM
90 || regnum == PS_REGNUM)
91 return builtin_type_int32;
92
93 if (regnum >= M68K_A0_REGNUM && regnum <= M68K_A0_REGNUM + 7)
94 return builtin_type_void_data_ptr;
95
96 return builtin_type_int32;
97 }
98
99 /* Function: m68k_register_name
100 Returns the name of the standard m68k register regnum. */
101
102 static const char *
103 m68k_register_name (int regnum)
104 {
105 static char *register_names[] = {
106 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
107 "a0", "a1", "a2", "a3", "a4", "a5", "fp", "sp",
108 "ps", "pc",
109 "fp0", "fp1", "fp2", "fp3", "fp4", "fp5", "fp6", "fp7",
110 "fpcontrol", "fpstatus", "fpiaddr", "fpcode", "fpflags"
111 };
112
113 if (regnum < 0 || regnum >= ARRAY_SIZE (register_names))
114 internal_error (__FILE__, __LINE__,
115 _("m68k_register_name: illegal register number %d"), regnum);
116 else
117 return register_names[regnum];
118 }
119 \f
120 /* Return nonzero if a value of type TYPE stored in register REGNUM
121 needs any special handling. */
122
123 static int
124 m68k_convert_register_p (int regnum, struct type *type)
125 {
126 return (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FP0_REGNUM + 7);
127 }
128
129 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
130 return its contents in TO. */
131
132 static void
133 m68k_register_to_value (struct frame_info *frame, int regnum,
134 struct type *type, gdb_byte *to)
135 {
136 gdb_byte from[M68K_MAX_REGISTER_SIZE];
137
138 /* We only support floating-point values. */
139 if (TYPE_CODE (type) != TYPE_CODE_FLT)
140 {
141 warning (_("Cannot convert floating-point register value "
142 "to non-floating-point type."));
143 return;
144 }
145
146 /* Convert to TYPE. This should be a no-op if TYPE is equivalent to
147 the extended floating-point format used by the FPU. */
148 get_frame_register (frame, regnum, from);
149 convert_typed_floating (from, builtin_type_m68881_ext, to, type);
150 }
151
152 /* Write the contents FROM of a value of type TYPE into register
153 REGNUM in frame FRAME. */
154
155 static void
156 m68k_value_to_register (struct frame_info *frame, int regnum,
157 struct type *type, const gdb_byte *from)
158 {
159 gdb_byte to[M68K_MAX_REGISTER_SIZE];
160
161 /* We only support floating-point values. */
162 if (TYPE_CODE (type) != TYPE_CODE_FLT)
163 {
164 warning (_("Cannot convert non-floating-point type "
165 "to floating-point register value."));
166 return;
167 }
168
169 /* Convert from TYPE. This should be a no-op if TYPE is equivalent
170 to the extended floating-point format used by the FPU. */
171 convert_typed_floating (from, type, to, builtin_type_m68881_ext);
172 put_frame_register (frame, regnum, to);
173 }
174
175 \f
176 /* There is a fair number of calling conventions that are in somewhat
177 wide use. The 68000/08/10 don't support an FPU, not even as a
178 coprocessor. All function return values are stored in %d0/%d1.
179 Structures are returned in a static buffer, a pointer to which is
180 returned in %d0. This means that functions returning a structure
181 are not re-entrant. To avoid this problem some systems use a
182 convention where the caller passes a pointer to a buffer in %a1
183 where the return values is to be stored. This convention is the
184 default, and is implemented in the function m68k_return_value.
185
186 The 68020/030/040/060 do support an FPU, either as a coprocessor
187 (68881/2) or built-in (68040/68060). That's why System V release 4
188 (SVR4) instroduces a new calling convention specified by the SVR4
189 psABI. Integer values are returned in %d0/%d1, pointer return
190 values in %a0 and floating values in %fp0. When calling functions
191 returning a structure the caller should pass a pointer to a buffer
192 for the return value in %a0. This convention is implemented in the
193 function m68k_svr4_return_value, and by appropriately setting the
194 struct_value_regnum member of `struct gdbarch_tdep'.
195
196 GNU/Linux returns values in the same way as SVR4 does, but uses %a1
197 for passing the structure return value buffer.
198
199 GCC can also generate code where small structures are returned in
200 %d0/%d1 instead of in memory by using -freg-struct-return. This is
201 the default on NetBSD a.out, OpenBSD and GNU/Linux and several
202 embedded systems. This convention is implemented by setting the
203 struct_return member of `struct gdbarch_tdep' to reg_struct_return. */
204
205 /* Read a function return value of TYPE from REGCACHE, and copy that
206 into VALBUF. */
207
208 static void
209 m68k_extract_return_value (struct type *type, struct regcache *regcache,
210 gdb_byte *valbuf)
211 {
212 int len = TYPE_LENGTH (type);
213 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
214
215 if (len <= 4)
216 {
217 regcache_raw_read (regcache, M68K_D0_REGNUM, buf);
218 memcpy (valbuf, buf + (4 - len), len);
219 }
220 else if (len <= 8)
221 {
222 regcache_raw_read (regcache, M68K_D0_REGNUM, buf);
223 memcpy (valbuf, buf + (8 - len), len - 4);
224 regcache_raw_read (regcache, M68K_D1_REGNUM, valbuf + (len - 4));
225 }
226 else
227 internal_error (__FILE__, __LINE__,
228 _("Cannot extract return value of %d bytes long."), len);
229 }
230
231 static void
232 m68k_svr4_extract_return_value (struct type *type, struct regcache *regcache,
233 gdb_byte *valbuf)
234 {
235 int len = TYPE_LENGTH (type);
236 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
237
238 if (TYPE_CODE (type) == TYPE_CODE_FLT)
239 {
240 regcache_raw_read (regcache, M68K_FP0_REGNUM, buf);
241 convert_typed_floating (buf, builtin_type_m68881_ext, valbuf, type);
242 }
243 else if (TYPE_CODE (type) == TYPE_CODE_PTR && len == 4)
244 regcache_raw_read (regcache, M68K_A0_REGNUM, valbuf);
245 else
246 m68k_extract_return_value (type, regcache, valbuf);
247 }
248
249 /* Write a function return value of TYPE from VALBUF into REGCACHE. */
250
251 static void
252 m68k_store_return_value (struct type *type, struct regcache *regcache,
253 const gdb_byte *valbuf)
254 {
255 int len = TYPE_LENGTH (type);
256
257 if (len <= 4)
258 regcache_raw_write_part (regcache, M68K_D0_REGNUM, 4 - len, len, valbuf);
259 else if (len <= 8)
260 {
261 regcache_raw_write_part (regcache, M68K_D0_REGNUM, 8 - len,
262 len - 4, valbuf);
263 regcache_raw_write (regcache, M68K_D1_REGNUM, valbuf + (len - 4));
264 }
265 else
266 internal_error (__FILE__, __LINE__,
267 _("Cannot store return value of %d bytes long."), len);
268 }
269
270 static void
271 m68k_svr4_store_return_value (struct type *type, struct regcache *regcache,
272 const gdb_byte *valbuf)
273 {
274 int len = TYPE_LENGTH (type);
275
276 if (TYPE_CODE (type) == TYPE_CODE_FLT)
277 {
278 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
279 convert_typed_floating (valbuf, type, buf, builtin_type_m68881_ext);
280 regcache_raw_write (regcache, M68K_FP0_REGNUM, buf);
281 }
282 else if (TYPE_CODE (type) == TYPE_CODE_PTR && len == 4)
283 {
284 regcache_raw_write (regcache, M68K_A0_REGNUM, valbuf);
285 regcache_raw_write (regcache, M68K_D0_REGNUM, valbuf);
286 }
287 else
288 m68k_store_return_value (type, regcache, valbuf);
289 }
290
291 /* Return non-zero if TYPE, which is assumed to be a structure or
292 union type, should be returned in registers for architecture
293 GDBARCH. */
294
295 static int
296 m68k_reg_struct_return_p (struct gdbarch *gdbarch, struct type *type)
297 {
298 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
299 enum type_code code = TYPE_CODE (type);
300 int len = TYPE_LENGTH (type);
301
302 gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
303
304 if (tdep->struct_return == pcc_struct_return)
305 return 0;
306
307 return (len == 1 || len == 2 || len == 4 || len == 8);
308 }
309
310 /* Determine, for architecture GDBARCH, how a return value of TYPE
311 should be returned. If it is supposed to be returned in registers,
312 and READBUF is non-zero, read the appropriate value from REGCACHE,
313 and copy it into READBUF. If WRITEBUF is non-zero, write the value
314 from WRITEBUF into REGCACHE. */
315
316 static enum return_value_convention
317 m68k_return_value (struct gdbarch *gdbarch, struct type *type,
318 struct regcache *regcache, gdb_byte *readbuf,
319 const gdb_byte *writebuf)
320 {
321 enum type_code code = TYPE_CODE (type);
322
323 /* GCC returns a `long double' in memory too. */
324 if (((code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION)
325 && !m68k_reg_struct_return_p (gdbarch, type))
326 || (code == TYPE_CODE_FLT && TYPE_LENGTH (type) == 12))
327 {
328 /* The default on m68k is to return structures in static memory.
329 Consequently a function must return the address where we can
330 find the return value. */
331
332 if (readbuf)
333 {
334 ULONGEST addr;
335
336 regcache_raw_read_unsigned (regcache, M68K_D0_REGNUM, &addr);
337 read_memory (addr, readbuf, TYPE_LENGTH (type));
338 }
339
340 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
341 }
342
343 if (readbuf)
344 m68k_extract_return_value (type, regcache, readbuf);
345 if (writebuf)
346 m68k_store_return_value (type, regcache, writebuf);
347
348 return RETURN_VALUE_REGISTER_CONVENTION;
349 }
350
351 static enum return_value_convention
352 m68k_svr4_return_value (struct gdbarch *gdbarch, struct type *type,
353 struct regcache *regcache, gdb_byte *readbuf,
354 const gdb_byte *writebuf)
355 {
356 enum type_code code = TYPE_CODE (type);
357
358 if ((code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION)
359 && !m68k_reg_struct_return_p (gdbarch, type))
360 {
361 /* The System V ABI says that:
362
363 "A function returning a structure or union also sets %a0 to
364 the value it finds in %a0. Thus when the caller receives
365 control again, the address of the returned object resides in
366 register %a0."
367
368 So the ABI guarantees that we can always find the return
369 value just after the function has returned. */
370
371 if (readbuf)
372 {
373 ULONGEST addr;
374
375 regcache_raw_read_unsigned (regcache, M68K_A0_REGNUM, &addr);
376 read_memory (addr, readbuf, TYPE_LENGTH (type));
377 }
378
379 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
380 }
381
382 /* This special case is for structures consisting of a single
383 `float' or `double' member. These structures are returned in
384 %fp0. For these structures, we call ourselves recursively,
385 changing TYPE into the type of the first member of the structure.
386 Since that should work for all structures that have only one
387 member, we don't bother to check the member's type here. */
388 if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
389 {
390 type = check_typedef (TYPE_FIELD_TYPE (type, 0));
391 return m68k_svr4_return_value (gdbarch, type, regcache,
392 readbuf, writebuf);
393 }
394
395 if (readbuf)
396 m68k_svr4_extract_return_value (type, regcache, readbuf);
397 if (writebuf)
398 m68k_svr4_store_return_value (type, regcache, writebuf);
399
400 return RETURN_VALUE_REGISTER_CONVENTION;
401 }
402 \f
403
404 /* Always align the frame to a 4-byte boundary. This is required on
405 coldfire and harmless on the rest. */
406
407 static CORE_ADDR
408 m68k_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
409 {
410 /* Align the stack to four bytes. */
411 return sp & ~3;
412 }
413
414 static CORE_ADDR
415 m68k_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
416 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
417 struct value **args, CORE_ADDR sp, int struct_return,
418 CORE_ADDR struct_addr)
419 {
420 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
421 gdb_byte buf[4];
422 int i;
423
424 /* Push arguments in reverse order. */
425 for (i = nargs - 1; i >= 0; i--)
426 {
427 struct type *value_type = value_enclosing_type (args[i]);
428 int len = TYPE_LENGTH (value_type);
429 int container_len = (len + 3) & ~3;
430 int offset;
431
432 /* Non-scalars bigger than 4 bytes are left aligned, others are
433 right aligned. */
434 if ((TYPE_CODE (value_type) == TYPE_CODE_STRUCT
435 || TYPE_CODE (value_type) == TYPE_CODE_UNION
436 || TYPE_CODE (value_type) == TYPE_CODE_ARRAY)
437 && len > 4)
438 offset = 0;
439 else
440 offset = container_len - len;
441 sp -= container_len;
442 write_memory (sp + offset, value_contents_all (args[i]), len);
443 }
444
445 /* Store struct value address. */
446 if (struct_return)
447 {
448 store_unsigned_integer (buf, 4, struct_addr);
449 regcache_cooked_write (regcache, tdep->struct_value_regnum, buf);
450 }
451
452 /* Store return address. */
453 sp -= 4;
454 store_unsigned_integer (buf, 4, bp_addr);
455 write_memory (sp, buf, 4);
456
457 /* Finally, update the stack pointer... */
458 store_unsigned_integer (buf, 4, sp);
459 regcache_cooked_write (regcache, M68K_SP_REGNUM, buf);
460
461 /* ...and fake a frame pointer. */
462 regcache_cooked_write (regcache, M68K_FP_REGNUM, buf);
463
464 /* DWARF2/GCC uses the stack address *before* the function call as a
465 frame's CFA. */
466 return sp + 8;
467 }
468
469 /* Convert a dwarf or dwarf2 regnumber to a GDB regnum. */
470
471 static int
472 m68k_dwarf_reg_to_regnum (int num)
473 {
474 if (num < 8)
475 /* d0..7 */
476 return (num - 0) + M68K_D0_REGNUM;
477 else if (num < 16)
478 /* a0..7 */
479 return (num - 8) + M68K_A0_REGNUM;
480 else if (num < 24)
481 /* fp0..7 */
482 return (num - 16) + M68K_FP0_REGNUM;
483 else if (num == 25)
484 /* pc */
485 return M68K_PC_REGNUM;
486 else
487 return gdbarch_num_regs (current_gdbarch)
488 + gdbarch_num_pseudo_regs (current_gdbarch);
489 }
490
491 \f
492 struct m68k_frame_cache
493 {
494 /* Base address. */
495 CORE_ADDR base;
496 CORE_ADDR sp_offset;
497 CORE_ADDR pc;
498
499 /* Saved registers. */
500 CORE_ADDR saved_regs[M68K_NUM_REGS];
501 CORE_ADDR saved_sp;
502
503 /* Stack space reserved for local variables. */
504 long locals;
505 };
506
507 /* Allocate and initialize a frame cache. */
508
509 static struct m68k_frame_cache *
510 m68k_alloc_frame_cache (void)
511 {
512 struct m68k_frame_cache *cache;
513 int i;
514
515 cache = FRAME_OBSTACK_ZALLOC (struct m68k_frame_cache);
516
517 /* Base address. */
518 cache->base = 0;
519 cache->sp_offset = -4;
520 cache->pc = 0;
521
522 /* Saved registers. We initialize these to -1 since zero is a valid
523 offset (that's where %fp is supposed to be stored). */
524 for (i = 0; i < M68K_NUM_REGS; i++)
525 cache->saved_regs[i] = -1;
526
527 /* Frameless until proven otherwise. */
528 cache->locals = -1;
529
530 return cache;
531 }
532
533 /* Check whether PC points at a code that sets up a new stack frame.
534 If so, it updates CACHE and returns the address of the first
535 instruction after the sequence that sets removes the "hidden"
536 argument from the stack or CURRENT_PC, whichever is smaller.
537 Otherwise, return PC. */
538
539 static CORE_ADDR
540 m68k_analyze_frame_setup (CORE_ADDR pc, CORE_ADDR current_pc,
541 struct m68k_frame_cache *cache)
542 {
543 int op;
544
545 if (pc >= current_pc)
546 return current_pc;
547
548 op = read_memory_unsigned_integer (pc, 2);
549
550 if (op == P_LINKW_FP || op == P_LINKL_FP || op == P_PEA_FP)
551 {
552 cache->saved_regs[M68K_FP_REGNUM] = 0;
553 cache->sp_offset += 4;
554 if (op == P_LINKW_FP)
555 {
556 /* link.w %fp, #-N */
557 /* link.w %fp, #0; adda.l #-N, %sp */
558 cache->locals = -read_memory_integer (pc + 2, 2);
559
560 if (pc + 4 < current_pc && cache->locals == 0)
561 {
562 op = read_memory_unsigned_integer (pc + 4, 2);
563 if (op == P_ADDAL_SP)
564 {
565 cache->locals = read_memory_integer (pc + 6, 4);
566 return pc + 10;
567 }
568 }
569
570 return pc + 4;
571 }
572 else if (op == P_LINKL_FP)
573 {
574 /* link.l %fp, #-N */
575 cache->locals = -read_memory_integer (pc + 2, 4);
576 return pc + 6;
577 }
578 else
579 {
580 /* pea (%fp); movea.l %sp, %fp */
581 cache->locals = 0;
582
583 if (pc + 2 < current_pc)
584 {
585 op = read_memory_unsigned_integer (pc + 2, 2);
586
587 if (op == P_MOVEAL_SP_FP)
588 {
589 /* move.l %sp, %fp */
590 return pc + 4;
591 }
592 }
593
594 return pc + 2;
595 }
596 }
597 else if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
598 {
599 /* subq.[wl] #N,%sp */
600 /* subq.[wl] #8,%sp; subq.[wl] #N,%sp */
601 cache->locals = (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
602 if (pc + 2 < current_pc)
603 {
604 op = read_memory_unsigned_integer (pc + 2, 2);
605 if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
606 {
607 cache->locals += (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
608 return pc + 4;
609 }
610 }
611 return pc + 2;
612 }
613 else if (op == P_ADDAW_SP || op == P_LEA_SP_SP)
614 {
615 /* adda.w #-N,%sp */
616 /* lea (-N,%sp),%sp */
617 cache->locals = -read_memory_integer (pc + 2, 2);
618 return pc + 4;
619 }
620 else if (op == P_ADDAL_SP)
621 {
622 /* adda.l #-N,%sp */
623 cache->locals = -read_memory_integer (pc + 2, 4);
624 return pc + 6;
625 }
626
627 return pc;
628 }
629
630 /* Check whether PC points at code that saves registers on the stack.
631 If so, it updates CACHE and returns the address of the first
632 instruction after the register saves or CURRENT_PC, whichever is
633 smaller. Otherwise, return PC. */
634
635 static CORE_ADDR
636 m68k_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
637 struct m68k_frame_cache *cache)
638 {
639 if (cache->locals >= 0)
640 {
641 CORE_ADDR offset;
642 int op;
643 int i, mask, regno;
644
645 offset = -4 - cache->locals;
646 while (pc < current_pc)
647 {
648 op = read_memory_unsigned_integer (pc, 2);
649 if (op == P_FMOVEMX_SP)
650 {
651 /* fmovem.x REGS,-(%sp) */
652 op = read_memory_unsigned_integer (pc + 2, 2);
653 if ((op & 0xff00) == 0xe000)
654 {
655 mask = op & 0xff;
656 for (i = 0; i < 16; i++, mask >>= 1)
657 {
658 if (mask & 1)
659 {
660 cache->saved_regs[i + M68K_FP0_REGNUM] = offset;
661 offset -= 12;
662 }
663 }
664 pc += 4;
665 }
666 else
667 break;
668 }
669 else if ((op & 0177760) == P_MOVEL_SP)
670 {
671 /* move.l %R,-(%sp) */
672 regno = op & 017;
673 cache->saved_regs[regno] = offset;
674 offset -= 4;
675 pc += 2;
676 }
677 else if (op == P_MOVEML_SP)
678 {
679 /* movem.l REGS,-(%sp) */
680 mask = read_memory_unsigned_integer (pc + 2, 2);
681 for (i = 0; i < 16; i++, mask >>= 1)
682 {
683 if (mask & 1)
684 {
685 cache->saved_regs[15 - i] = offset;
686 offset -= 4;
687 }
688 }
689 pc += 4;
690 }
691 else
692 break;
693 }
694 }
695
696 return pc;
697 }
698
699
700 /* Do a full analysis of the prologue at PC and update CACHE
701 accordingly. Bail out early if CURRENT_PC is reached. Return the
702 address where the analysis stopped.
703
704 We handle all cases that can be generated by gcc.
705
706 For allocating a stack frame:
707
708 link.w %a6,#-N
709 link.l %a6,#-N
710 pea (%fp); move.l %sp,%fp
711 link.w %a6,#0; add.l #-N,%sp
712 subq.l #N,%sp
713 subq.w #N,%sp
714 subq.w #8,%sp; subq.w #N-8,%sp
715 add.w #-N,%sp
716 lea (-N,%sp),%sp
717 add.l #-N,%sp
718
719 For saving registers:
720
721 fmovem.x REGS,-(%sp)
722 move.l R1,-(%sp)
723 move.l R1,-(%sp); move.l R2,-(%sp)
724 movem.l REGS,-(%sp)
725
726 For setting up the PIC register:
727
728 lea (%pc,N),%a5
729
730 */
731
732 static CORE_ADDR
733 m68k_analyze_prologue (CORE_ADDR pc, CORE_ADDR current_pc,
734 struct m68k_frame_cache *cache)
735 {
736 unsigned int op;
737
738 pc = m68k_analyze_frame_setup (pc, current_pc, cache);
739 pc = m68k_analyze_register_saves (pc, current_pc, cache);
740 if (pc >= current_pc)
741 return current_pc;
742
743 /* Check for GOT setup. */
744 op = read_memory_unsigned_integer (pc, 4);
745 if (op == P_LEA_PC_A5)
746 {
747 /* lea (%pc,N),%a5 */
748 return pc + 6;
749 }
750
751 return pc;
752 }
753
754 /* Return PC of first real instruction. */
755
756 static CORE_ADDR
757 m68k_skip_prologue (CORE_ADDR start_pc)
758 {
759 struct m68k_frame_cache cache;
760 CORE_ADDR pc;
761 int op;
762
763 cache.locals = -1;
764 pc = m68k_analyze_prologue (start_pc, (CORE_ADDR) -1, &cache);
765 if (cache.locals < 0)
766 return start_pc;
767 return pc;
768 }
769
770 static CORE_ADDR
771 m68k_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
772 {
773 gdb_byte buf[8];
774
775 frame_unwind_register (next_frame, PC_REGNUM, buf);
776 return extract_typed_address (buf, builtin_type_void_func_ptr);
777 }
778 \f
779 /* Normal frames. */
780
781 static struct m68k_frame_cache *
782 m68k_frame_cache (struct frame_info *next_frame, void **this_cache)
783 {
784 struct m68k_frame_cache *cache;
785 gdb_byte buf[4];
786 int i;
787
788 if (*this_cache)
789 return *this_cache;
790
791 cache = m68k_alloc_frame_cache ();
792 *this_cache = cache;
793
794 /* In principle, for normal frames, %fp holds the frame pointer,
795 which holds the base address for the current stack frame.
796 However, for functions that don't need it, the frame pointer is
797 optional. For these "frameless" functions the frame pointer is
798 actually the frame pointer of the calling frame. Signal
799 trampolines are just a special case of a "frameless" function.
800 They (usually) share their frame pointer with the frame that was
801 in progress when the signal occurred. */
802
803 frame_unwind_register (next_frame, M68K_FP_REGNUM, buf);
804 cache->base = extract_unsigned_integer (buf, 4);
805 if (cache->base == 0)
806 return cache;
807
808 /* For normal frames, %pc is stored at 4(%fp). */
809 cache->saved_regs[M68K_PC_REGNUM] = 4;
810
811 cache->pc = frame_func_unwind (next_frame, NORMAL_FRAME);
812 if (cache->pc != 0)
813 m68k_analyze_prologue (cache->pc, frame_pc_unwind (next_frame), cache);
814
815 if (cache->locals < 0)
816 {
817 /* We didn't find a valid frame, which means that CACHE->base
818 currently holds the frame pointer for our calling frame. If
819 we're at the start of a function, or somewhere half-way its
820 prologue, the function's frame probably hasn't been fully
821 setup yet. Try to reconstruct the base address for the stack
822 frame by looking at the stack pointer. For truly "frameless"
823 functions this might work too. */
824
825 frame_unwind_register (next_frame, M68K_SP_REGNUM, buf);
826 cache->base = extract_unsigned_integer (buf, 4) + cache->sp_offset;
827 }
828
829 /* Now that we have the base address for the stack frame we can
830 calculate the value of %sp in the calling frame. */
831 cache->saved_sp = cache->base + 8;
832
833 /* Adjust all the saved registers such that they contain addresses
834 instead of offsets. */
835 for (i = 0; i < M68K_NUM_REGS; i++)
836 if (cache->saved_regs[i] != -1)
837 cache->saved_regs[i] += cache->base;
838
839 return cache;
840 }
841
842 static void
843 m68k_frame_this_id (struct frame_info *next_frame, void **this_cache,
844 struct frame_id *this_id)
845 {
846 struct m68k_frame_cache *cache = m68k_frame_cache (next_frame, this_cache);
847
848 /* This marks the outermost frame. */
849 if (cache->base == 0)
850 return;
851
852 /* See the end of m68k_push_dummy_call. */
853 *this_id = frame_id_build (cache->base + 8, cache->pc);
854 }
855
856 static void
857 m68k_frame_prev_register (struct frame_info *next_frame, void **this_cache,
858 int regnum, int *optimizedp,
859 enum lval_type *lvalp, CORE_ADDR *addrp,
860 int *realnump, gdb_byte *valuep)
861 {
862 struct m68k_frame_cache *cache = m68k_frame_cache (next_frame, this_cache);
863
864 gdb_assert (regnum >= 0);
865
866 if (regnum == M68K_SP_REGNUM && cache->saved_sp)
867 {
868 *optimizedp = 0;
869 *lvalp = not_lval;
870 *addrp = 0;
871 *realnump = -1;
872 if (valuep)
873 {
874 /* Store the value. */
875 store_unsigned_integer (valuep, 4, cache->saved_sp);
876 }
877 return;
878 }
879
880 if (regnum < M68K_NUM_REGS && cache->saved_regs[regnum] != -1)
881 {
882 *optimizedp = 0;
883 *lvalp = lval_memory;
884 *addrp = cache->saved_regs[regnum];
885 *realnump = -1;
886 if (valuep)
887 {
888 /* Read the value in from memory. */
889 read_memory (*addrp, valuep,
890 register_size (current_gdbarch, regnum));
891 }
892 return;
893 }
894
895 *optimizedp = 0;
896 *lvalp = lval_register;
897 *addrp = 0;
898 *realnump = regnum;
899 if (valuep)
900 frame_unwind_register (next_frame, (*realnump), valuep);
901 }
902
903 static const struct frame_unwind m68k_frame_unwind =
904 {
905 NORMAL_FRAME,
906 m68k_frame_this_id,
907 m68k_frame_prev_register
908 };
909
910 static const struct frame_unwind *
911 m68k_frame_sniffer (struct frame_info *next_frame)
912 {
913 return &m68k_frame_unwind;
914 }
915 \f
916 static CORE_ADDR
917 m68k_frame_base_address (struct frame_info *next_frame, void **this_cache)
918 {
919 struct m68k_frame_cache *cache = m68k_frame_cache (next_frame, this_cache);
920
921 return cache->base;
922 }
923
924 static const struct frame_base m68k_frame_base =
925 {
926 &m68k_frame_unwind,
927 m68k_frame_base_address,
928 m68k_frame_base_address,
929 m68k_frame_base_address
930 };
931
932 static struct frame_id
933 m68k_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
934 {
935 gdb_byte buf[4];
936 CORE_ADDR fp;
937
938 frame_unwind_register (next_frame, M68K_FP_REGNUM, buf);
939 fp = extract_unsigned_integer (buf, 4);
940
941 /* See the end of m68k_push_dummy_call. */
942 return frame_id_build (fp + 8, frame_pc_unwind (next_frame));
943 }
944 \f
945
946 /* Figure out where the longjmp will land. Slurp the args out of the stack.
947 We expect the first arg to be a pointer to the jmp_buf structure from which
948 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
949 This routine returns true on success. */
950
951 static int
952 m68k_get_longjmp_target (CORE_ADDR *pc)
953 {
954 gdb_byte *buf;
955 CORE_ADDR sp, jb_addr;
956 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
957
958 if (tdep->jb_pc < 0)
959 {
960 internal_error (__FILE__, __LINE__,
961 _("m68k_get_longjmp_target: not implemented"));
962 return 0;
963 }
964
965 buf = alloca (gdbarch_ptr_bit (current_gdbarch) / TARGET_CHAR_BIT);
966 sp = read_register (SP_REGNUM);
967
968 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
969 buf,
970 gdbarch_ptr_bit (current_gdbarch) / TARGET_CHAR_BIT))
971 return 0;
972
973 jb_addr = extract_unsigned_integer (buf, gdbarch_ptr_bit (current_gdbarch)
974 / TARGET_CHAR_BIT);
975
976 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
977 gdbarch_ptr_bit (current_gdbarch) / TARGET_CHAR_BIT))
978 return 0;
979
980 *pc = extract_unsigned_integer (buf, gdbarch_ptr_bit (current_gdbarch)
981 / TARGET_CHAR_BIT);
982 return 1;
983 }
984 \f
985
986 /* System V Release 4 (SVR4). */
987
988 void
989 m68k_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
990 {
991 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
992
993 /* SVR4 uses a different calling convention. */
994 set_gdbarch_return_value (gdbarch, m68k_svr4_return_value);
995
996 /* SVR4 uses %a0 instead of %a1. */
997 tdep->struct_value_regnum = M68K_A0_REGNUM;
998 }
999 \f
1000
1001 /* Function: m68k_gdbarch_init
1002 Initializer function for the m68k gdbarch vector.
1003 Called by gdbarch. Sets up the gdbarch vector(s) for this target. */
1004
1005 static struct gdbarch *
1006 m68k_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1007 {
1008 struct gdbarch_tdep *tdep = NULL;
1009 struct gdbarch *gdbarch;
1010
1011 /* find a candidate among the list of pre-declared architectures. */
1012 arches = gdbarch_list_lookup_by_info (arches, &info);
1013 if (arches != NULL)
1014 return (arches->gdbarch);
1015
1016 tdep = xmalloc (sizeof (struct gdbarch_tdep));
1017 gdbarch = gdbarch_alloc (&info, tdep);
1018
1019 set_gdbarch_long_double_format (gdbarch, floatformats_m68881_ext);
1020 set_gdbarch_long_double_bit (gdbarch, 96);
1021
1022 set_gdbarch_skip_prologue (gdbarch, m68k_skip_prologue);
1023 set_gdbarch_breakpoint_from_pc (gdbarch, m68k_local_breakpoint_from_pc);
1024
1025 /* Stack grows down. */
1026 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1027 set_gdbarch_frame_align (gdbarch, m68k_frame_align);
1028
1029 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
1030
1031 set_gdbarch_frame_args_skip (gdbarch, 8);
1032 set_gdbarch_dwarf_reg_to_regnum (gdbarch, m68k_dwarf_reg_to_regnum);
1033 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, m68k_dwarf_reg_to_regnum);
1034
1035 set_gdbarch_register_type (gdbarch, m68k_register_type);
1036 set_gdbarch_register_name (gdbarch, m68k_register_name);
1037 set_gdbarch_num_regs (gdbarch, M68K_NUM_REGS);
1038 set_gdbarch_sp_regnum (gdbarch, M68K_SP_REGNUM);
1039 set_gdbarch_pc_regnum (gdbarch, M68K_PC_REGNUM);
1040 set_gdbarch_ps_regnum (gdbarch, M68K_PS_REGNUM);
1041 set_gdbarch_fp0_regnum (gdbarch, M68K_FP0_REGNUM);
1042 set_gdbarch_convert_register_p (gdbarch, m68k_convert_register_p);
1043 set_gdbarch_register_to_value (gdbarch, m68k_register_to_value);
1044 set_gdbarch_value_to_register (gdbarch, m68k_value_to_register);
1045
1046 set_gdbarch_push_dummy_call (gdbarch, m68k_push_dummy_call);
1047 set_gdbarch_return_value (gdbarch, m68k_return_value);
1048
1049 /* Disassembler. */
1050 set_gdbarch_print_insn (gdbarch, print_insn_m68k);
1051
1052 #if defined JB_PC && defined JB_ELEMENT_SIZE
1053 tdep->jb_pc = JB_PC;
1054 tdep->jb_elt_size = JB_ELEMENT_SIZE;
1055 #else
1056 tdep->jb_pc = -1;
1057 #endif
1058 tdep->struct_value_regnum = M68K_A1_REGNUM;
1059 tdep->struct_return = reg_struct_return;
1060
1061 /* Frame unwinder. */
1062 set_gdbarch_unwind_dummy_id (gdbarch, m68k_unwind_dummy_id);
1063 set_gdbarch_unwind_pc (gdbarch, m68k_unwind_pc);
1064
1065 /* Hook in the DWARF CFI frame unwinder. */
1066 frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);
1067
1068 frame_base_set_default (gdbarch, &m68k_frame_base);
1069
1070 /* Hook in ABI-specific overrides, if they have been registered. */
1071 gdbarch_init_osabi (info, gdbarch);
1072
1073 /* Now we have tuned the configuration, set a few final things,
1074 based on what the OS ABI has told us. */
1075
1076 if (tdep->jb_pc >= 0)
1077 set_gdbarch_get_longjmp_target (gdbarch, m68k_get_longjmp_target);
1078
1079 frame_unwind_append_sniffer (gdbarch, m68k_frame_sniffer);
1080
1081 return gdbarch;
1082 }
1083
1084
1085 static void
1086 m68k_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
1087 {
1088 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1089
1090 if (tdep == NULL)
1091 return;
1092 }
1093
1094 extern initialize_file_ftype _initialize_m68k_tdep; /* -Wmissing-prototypes */
1095
1096 void
1097 _initialize_m68k_tdep (void)
1098 {
1099 gdbarch_register (bfd_arch_m68k, m68k_gdbarch_init, m68k_dump_tdep);
1100 }
This page took 0.054475 seconds and 5 git commands to generate.