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