1 /* Target-dependent code for the Motorola 68000 series.
3 Copyright (C) 1990-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "dwarf2/frame.h"
23 #include "frame-base.h"
24 #include "frame-unwind.h"
31 #include "arch-utils.h"
34 #include "target-descriptions.h"
35 #include "floatformat.h"
36 #include "target-float.h"
40 #include "m68k-tdep.h"
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
57 /* Offset from SP to first arg on stack at first instruction of a function. */
58 #define SP_ARG0 (1 * 4)
60 #if !defined (BPT_VECTOR)
61 #define BPT_VECTOR 0xf
64 constexpr gdb_byte m68k_break_insn
[] = {0x4e, (0x40 | BPT_VECTOR
)};
66 typedef BP_MANIPULATION (m68k_break_insn
) m68k_breakpoint
;
69 /* Construct types for ISA-specific registers. */
71 m68k_ps_type (struct gdbarch
*gdbarch
)
73 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
75 if (!tdep
->m68k_ps_type
)
79 type
= arch_flags_type (gdbarch
, "builtin_type_m68k_ps", 32);
80 append_flags_type_flag (type
, 0, "C");
81 append_flags_type_flag (type
, 1, "V");
82 append_flags_type_flag (type
, 2, "Z");
83 append_flags_type_flag (type
, 3, "N");
84 append_flags_type_flag (type
, 4, "X");
85 append_flags_type_flag (type
, 8, "I0");
86 append_flags_type_flag (type
, 9, "I1");
87 append_flags_type_flag (type
, 10, "I2");
88 append_flags_type_flag (type
, 12, "M");
89 append_flags_type_flag (type
, 13, "S");
90 append_flags_type_flag (type
, 14, "T0");
91 append_flags_type_flag (type
, 15, "T1");
93 tdep
->m68k_ps_type
= type
;
96 return tdep
->m68k_ps_type
;
100 m68881_ext_type (struct gdbarch
*gdbarch
)
102 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
104 if (!tdep
->m68881_ext_type
)
105 tdep
->m68881_ext_type
106 = arch_float_type (gdbarch
, -1, "builtin_type_m68881_ext",
107 floatformats_m68881_ext
);
109 return tdep
->m68881_ext_type
;
112 /* Return the GDB type object for the "standard" data type of data in
113 register N. This should be int for D0-D7, SR, FPCONTROL and
114 FPSTATUS, long double for FP0-FP7, and void pointer for all others
115 (A0-A7, PC, FPIADDR). Note, for registers which contain
116 addresses return pointer to void, not pointer to char, because we
117 don't want to attempt to print the string after printing the
121 m68k_register_type (struct gdbarch
*gdbarch
, int regnum
)
123 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
125 if (tdep
->fpregs_present
)
127 if (regnum
>= gdbarch_fp0_regnum (gdbarch
)
128 && regnum
<= gdbarch_fp0_regnum (gdbarch
) + 7)
130 if (tdep
->flavour
== m68k_coldfire_flavour
)
131 return builtin_type (gdbarch
)->builtin_double
;
133 return m68881_ext_type (gdbarch
);
136 if (regnum
== M68K_FPI_REGNUM
)
137 return builtin_type (gdbarch
)->builtin_func_ptr
;
139 if (regnum
== M68K_FPC_REGNUM
|| regnum
== M68K_FPS_REGNUM
)
140 return builtin_type (gdbarch
)->builtin_int32
;
144 if (regnum
>= M68K_FP0_REGNUM
&& regnum
<= M68K_FPI_REGNUM
)
145 return builtin_type (gdbarch
)->builtin_int0
;
148 if (regnum
== gdbarch_pc_regnum (gdbarch
))
149 return builtin_type (gdbarch
)->builtin_func_ptr
;
151 if (regnum
>= M68K_A0_REGNUM
&& regnum
<= M68K_A0_REGNUM
+ 7)
152 return builtin_type (gdbarch
)->builtin_data_ptr
;
154 if (regnum
== M68K_PS_REGNUM
)
155 return m68k_ps_type (gdbarch
);
157 return builtin_type (gdbarch
)->builtin_int32
;
160 static const char * const m68k_register_names
[] = {
161 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
162 "a0", "a1", "a2", "a3", "a4", "a5", "fp", "sp",
164 "fp0", "fp1", "fp2", "fp3", "fp4", "fp5", "fp6", "fp7",
165 "fpcontrol", "fpstatus", "fpiaddr"
168 /* Function: m68k_register_name
169 Returns the name of the standard m68k register regnum. */
172 m68k_register_name (struct gdbarch
*gdbarch
, int regnum
)
174 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
176 gdb_static_assert (ARRAY_SIZE (m68k_register_names
) == M68K_NUM_REGS
);
177 if (regnum
>= M68K_FP0_REGNUM
&& regnum
<= M68K_FPI_REGNUM
178 && tdep
->fpregs_present
== 0)
181 return m68k_register_names
[regnum
];
184 /* Return nonzero if a value of type TYPE stored in register REGNUM
185 needs any special handling. */
188 m68k_convert_register_p (struct gdbarch
*gdbarch
,
189 int regnum
, struct type
*type
)
191 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
193 if (!tdep
->fpregs_present
)
195 return (regnum
>= M68K_FP0_REGNUM
&& regnum
<= M68K_FP0_REGNUM
+ 7
196 /* We only support floating-point values. */
197 && type
->code () == TYPE_CODE_FLT
198 && type
!= register_type (gdbarch
, M68K_FP0_REGNUM
));
201 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
202 return its contents in TO. */
205 m68k_register_to_value (frame_info_ptr frame
, int regnum
,
206 struct type
*type
, gdb_byte
*to
,
207 int *optimizedp
, int *unavailablep
)
209 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
210 gdb_byte from
[M68K_MAX_REGISTER_SIZE
];
211 struct type
*fpreg_type
= register_type (gdbarch
, M68K_FP0_REGNUM
);
213 gdb_assert (type
->code () == TYPE_CODE_FLT
);
215 /* Convert to TYPE. */
216 if (!get_frame_register_bytes (frame
, regnum
, 0,
217 gdb::make_array_view (from
,
218 register_size (gdbarch
,
220 optimizedp
, unavailablep
))
223 target_float_convert (from
, fpreg_type
, to
, type
);
224 *optimizedp
= *unavailablep
= 0;
228 /* Write the contents FROM of a value of type TYPE into register
229 REGNUM in frame FRAME. */
232 m68k_value_to_register (frame_info_ptr frame
, int regnum
,
233 struct type
*type
, const gdb_byte
*from
)
235 gdb_byte to
[M68K_MAX_REGISTER_SIZE
];
236 struct type
*fpreg_type
= register_type (get_frame_arch (frame
),
239 /* We only support floating-point values. */
240 if (type
->code () != TYPE_CODE_FLT
)
242 warning (_("Cannot convert non-floating-point type "
243 "to floating-point register value."));
247 /* Convert from TYPE. */
248 target_float_convert (from
, type
, to
, fpreg_type
);
249 put_frame_register (frame
, regnum
, to
);
253 /* There is a fair number of calling conventions that are in somewhat
254 wide use. The 68000/08/10 don't support an FPU, not even as a
255 coprocessor. All function return values are stored in %d0/%d1.
256 Structures are returned in a static buffer, a pointer to which is
257 returned in %d0. This means that functions returning a structure
258 are not re-entrant. To avoid this problem some systems use a
259 convention where the caller passes a pointer to a buffer in %a1
260 where the return values is to be stored. This convention is the
261 default, and is implemented in the function m68k_return_value.
263 The 68020/030/040/060 do support an FPU, either as a coprocessor
264 (68881/2) or built-in (68040/68060). That's why System V release 4
265 (SVR4) introduces a new calling convention specified by the SVR4
266 psABI. Integer values are returned in %d0/%d1, pointer return
267 values in %a0 and floating values in %fp0. When calling functions
268 returning a structure the caller should pass a pointer to a buffer
269 for the return value in %a0. This convention is implemented in the
270 function m68k_svr4_return_value, and by appropriately setting the
271 struct_value_regnum member of `struct gdbarch_tdep'.
273 GNU/Linux returns values in the same way as SVR4 does, but uses %a1
274 for passing the structure return value buffer.
276 GCC can also generate code where small structures are returned in
277 %d0/%d1 instead of in memory by using -freg-struct-return. This is
278 the default on NetBSD a.out, OpenBSD and GNU/Linux and several
279 embedded systems. This convention is implemented by setting the
280 struct_return member of `struct gdbarch_tdep' to reg_struct_return.
282 GCC also has an "embedded" ABI. This works like the SVR4 ABI,
283 except that pointers are returned in %D0. This is implemented by
284 setting the pointer_result_regnum member of `struct gdbarch_tdep'
287 /* Read a function return value of TYPE from REGCACHE, and copy that
291 m68k_extract_return_value (struct type
*type
, struct regcache
*regcache
,
294 int len
= type
->length ();
295 gdb_byte buf
[M68K_MAX_REGISTER_SIZE
];
297 if (type
->code () == TYPE_CODE_PTR
&& len
== 4)
299 struct gdbarch
*gdbarch
= regcache
->arch ();
300 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
301 regcache
->raw_read (tdep
->pointer_result_regnum
, valbuf
);
305 regcache
->raw_read (M68K_D0_REGNUM
, buf
);
306 memcpy (valbuf
, buf
+ (4 - len
), len
);
310 regcache
->raw_read (M68K_D0_REGNUM
, buf
);
311 memcpy (valbuf
, buf
+ (8 - len
), len
- 4);
312 regcache
->raw_read (M68K_D1_REGNUM
, valbuf
+ (len
- 4));
315 internal_error (_("Cannot extract return value of %d bytes long."), len
);
319 m68k_svr4_extract_return_value (struct type
*type
, struct regcache
*regcache
,
322 gdb_byte buf
[M68K_MAX_REGISTER_SIZE
];
323 struct gdbarch
*gdbarch
= regcache
->arch ();
324 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
326 if (tdep
->float_return
&& type
->code () == TYPE_CODE_FLT
)
328 struct type
*fpreg_type
= register_type (gdbarch
, M68K_FP0_REGNUM
);
329 regcache
->raw_read (M68K_FP0_REGNUM
, buf
);
330 target_float_convert (buf
, fpreg_type
, valbuf
, type
);
333 m68k_extract_return_value (type
, regcache
, valbuf
);
336 /* Write a function return value of TYPE from VALBUF into REGCACHE. */
339 m68k_store_return_value (struct type
*type
, struct regcache
*regcache
,
340 const gdb_byte
*valbuf
)
342 int len
= type
->length ();
344 if (type
->code () == TYPE_CODE_PTR
&& len
== 4)
346 struct gdbarch
*gdbarch
= regcache
->arch ();
347 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
348 regcache
->raw_write (tdep
->pointer_result_regnum
, valbuf
);
349 /* gdb historically also set D0 in the SVR4 case. */
350 if (tdep
->pointer_result_regnum
!= M68K_D0_REGNUM
)
351 regcache
->raw_write (M68K_D0_REGNUM
, valbuf
);
354 regcache
->raw_write_part (M68K_D0_REGNUM
, 4 - len
, len
, valbuf
);
357 regcache
->raw_write_part (M68K_D0_REGNUM
, 8 - len
, len
- 4, valbuf
);
358 regcache
->raw_write (M68K_D1_REGNUM
, valbuf
+ (len
- 4));
361 internal_error (_("Cannot store return value of %d bytes long."), len
);
365 m68k_svr4_store_return_value (struct type
*type
, struct regcache
*regcache
,
366 const gdb_byte
*valbuf
)
368 struct gdbarch
*gdbarch
= regcache
->arch ();
369 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
371 if (tdep
->float_return
&& type
->code () == TYPE_CODE_FLT
)
373 struct type
*fpreg_type
= register_type (gdbarch
, M68K_FP0_REGNUM
);
374 gdb_byte buf
[M68K_MAX_REGISTER_SIZE
];
375 target_float_convert (valbuf
, type
, buf
, fpreg_type
);
376 regcache
->raw_write (M68K_FP0_REGNUM
, buf
);
379 m68k_store_return_value (type
, regcache
, valbuf
);
382 /* Return non-zero if TYPE, which is assumed to be a structure, union or
383 complex type, should be returned in registers for architecture
387 m68k_reg_struct_return_p (struct gdbarch
*gdbarch
, struct type
*type
)
389 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
390 enum type_code code
= type
->code ();
391 int len
= type
->length ();
393 gdb_assert (code
== TYPE_CODE_STRUCT
|| code
== TYPE_CODE_UNION
394 || code
== TYPE_CODE_COMPLEX
|| code
== TYPE_CODE_ARRAY
);
396 if (tdep
->struct_return
== pcc_struct_return
)
399 const bool is_vector
= code
== TYPE_CODE_ARRAY
&& type
->is_vector ();
402 && check_typedef (type
->target_type ())->code () == TYPE_CODE_FLT
)
405 /* According to m68k_return_in_memory in the m68k GCC back-end,
406 strange things happen for small aggregate types. Aggregate types
407 with only one component are always returned like the type of the
408 component. Aggregate types whose size is 2, 4, or 8 are returned
409 in registers if their natural alignment is at least 16 bits.
411 We reject vectors here, as experimentally this gives the correct
413 if (!is_vector
&& (len
== 2 || len
== 4 || len
== 8))
414 return type_align (type
) >= 2;
416 return (len
== 1 || len
== 2 || len
== 4 || len
== 8);
419 /* Determine, for architecture GDBARCH, how a return value of TYPE
420 should be returned. If it is supposed to be returned in registers,
421 and READBUF is non-zero, read the appropriate value from REGCACHE,
422 and copy it into READBUF. If WRITEBUF is non-zero, write the value
423 from WRITEBUF into REGCACHE. */
425 static enum return_value_convention
426 m68k_return_value (struct gdbarch
*gdbarch
, struct value
*function
,
427 struct type
*type
, struct regcache
*regcache
,
428 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
430 enum type_code code
= type
->code ();
432 /* GCC returns a `long double' in memory too. */
433 if (((code
== TYPE_CODE_STRUCT
|| code
== TYPE_CODE_UNION
434 || code
== TYPE_CODE_COMPLEX
|| code
== TYPE_CODE_ARRAY
)
435 && !m68k_reg_struct_return_p (gdbarch
, type
))
436 || (code
== TYPE_CODE_FLT
&& type
->length () == 12))
438 /* The default on m68k is to return structures in static memory.
439 Consequently a function must return the address where we can
440 find the return value. */
446 regcache_raw_read_unsigned (regcache
, M68K_D0_REGNUM
, &addr
);
447 read_memory (addr
, readbuf
, type
->length ());
450 return RETURN_VALUE_ABI_RETURNS_ADDRESS
;
454 m68k_extract_return_value (type
, regcache
, readbuf
);
456 m68k_store_return_value (type
, regcache
, writebuf
);
458 return RETURN_VALUE_REGISTER_CONVENTION
;
461 static enum return_value_convention
462 m68k_svr4_return_value (struct gdbarch
*gdbarch
, struct value
*function
,
463 struct type
*type
, struct regcache
*regcache
,
464 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
466 enum type_code code
= type
->code ();
467 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
469 /* Aggregates with a single member are always returned like their
471 if ((code
== TYPE_CODE_STRUCT
|| code
== TYPE_CODE_UNION
)
472 && type
->num_fields () == 1)
474 type
= check_typedef (type
->field (0).type ());
475 return m68k_svr4_return_value (gdbarch
, function
, type
, regcache
,
479 if (((code
== TYPE_CODE_STRUCT
|| code
== TYPE_CODE_UNION
480 || code
== TYPE_CODE_COMPLEX
|| code
== TYPE_CODE_ARRAY
)
481 && !m68k_reg_struct_return_p (gdbarch
, type
))
482 /* GCC may return a `long double' in memory too. */
483 || (!tdep
->float_return
484 && code
== TYPE_CODE_FLT
485 && type
->length () == 12))
487 /* The System V ABI says that:
489 "A function returning a structure or union also sets %a0 to
490 the value it finds in %a0. Thus when the caller receives
491 control again, the address of the returned object resides in
494 So the ABI guarantees that we can always find the return
495 value just after the function has returned.
497 However, GCC also implements the "embedded" ABI. That ABI
498 does not preserve %a0 across calls, but does write the value
505 regcache_raw_read_unsigned (regcache
, tdep
->pointer_result_regnum
,
507 read_memory (addr
, readbuf
, type
->length ());
510 return RETURN_VALUE_ABI_RETURNS_ADDRESS
;
514 m68k_svr4_extract_return_value (type
, regcache
, readbuf
);
516 m68k_svr4_store_return_value (type
, regcache
, writebuf
);
518 return RETURN_VALUE_REGISTER_CONVENTION
;
522 /* Always align the frame to a 4-byte boundary. This is required on
523 coldfire and harmless on the rest. */
526 m68k_frame_align (struct gdbarch
*gdbarch
, CORE_ADDR sp
)
528 /* Align the stack to four bytes. */
533 m68k_push_dummy_call (struct gdbarch
*gdbarch
, struct value
*function
,
534 struct regcache
*regcache
, CORE_ADDR bp_addr
, int nargs
,
535 struct value
**args
, CORE_ADDR sp
,
536 function_call_return_method return_method
,
537 CORE_ADDR struct_addr
)
539 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
540 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
544 /* Push arguments in reverse order. */
545 for (i
= nargs
- 1; i
>= 0; i
--)
547 struct type
*value_type
= value_enclosing_type (args
[i
]);
548 int len
= value_type
->length ();
549 int container_len
= (len
+ 3) & ~3;
552 /* Non-scalars bigger than 4 bytes are left aligned, others are
554 if ((value_type
->code () == TYPE_CODE_STRUCT
555 || value_type
->code () == TYPE_CODE_UNION
556 || value_type
->code () == TYPE_CODE_ARRAY
)
560 offset
= container_len
- len
;
562 write_memory (sp
+ offset
, value_contents_all (args
[i
]).data (), len
);
565 /* Store struct value address. */
566 if (return_method
== return_method_struct
)
568 store_unsigned_integer (buf
, 4, byte_order
, struct_addr
);
569 regcache
->cooked_write (tdep
->struct_value_regnum
, buf
);
572 /* Store return address. */
574 store_unsigned_integer (buf
, 4, byte_order
, bp_addr
);
575 write_memory (sp
, buf
, 4);
577 /* Finally, update the stack pointer... */
578 store_unsigned_integer (buf
, 4, byte_order
, sp
);
579 regcache
->cooked_write (M68K_SP_REGNUM
, buf
);
581 /* ...and fake a frame pointer. */
582 regcache
->cooked_write (M68K_FP_REGNUM
, buf
);
584 /* DWARF2/GCC uses the stack address *before* the function call as a
589 /* Convert a dwarf or dwarf2 regnumber to a GDB regnum. */
592 m68k_dwarf_reg_to_regnum (struct gdbarch
*gdbarch
, int num
)
594 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
598 return (num
- 0) + M68K_D0_REGNUM
;
601 return (num
- 8) + M68K_A0_REGNUM
;
602 else if (num
< 24 && tdep
->fpregs_present
)
604 return (num
- 16) + M68K_FP0_REGNUM
;
607 return M68K_PC_REGNUM
;
613 struct m68k_frame_cache
620 /* Saved registers. */
621 CORE_ADDR saved_regs
[M68K_NUM_REGS
];
624 /* Stack space reserved for local variables. */
628 /* Allocate and initialize a frame cache. */
630 static struct m68k_frame_cache
*
631 m68k_alloc_frame_cache (void)
633 struct m68k_frame_cache
*cache
;
636 cache
= FRAME_OBSTACK_ZALLOC (struct m68k_frame_cache
);
640 cache
->sp_offset
= -4;
643 /* Saved registers. We initialize these to -1 since zero is a valid
644 offset (that's where %fp is supposed to be stored). */
645 for (i
= 0; i
< M68K_NUM_REGS
; i
++)
646 cache
->saved_regs
[i
] = -1;
648 /* Frameless until proven otherwise. */
654 /* Check whether PC points at a code that sets up a new stack frame.
655 If so, it updates CACHE and returns the address of the first
656 instruction after the sequence that sets removes the "hidden"
657 argument from the stack or CURRENT_PC, whichever is smaller.
658 Otherwise, return PC. */
661 m68k_analyze_frame_setup (struct gdbarch
*gdbarch
,
662 CORE_ADDR pc
, CORE_ADDR current_pc
,
663 struct m68k_frame_cache
*cache
)
665 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
668 if (pc
>= current_pc
)
671 op
= read_memory_unsigned_integer (pc
, 2, byte_order
);
673 if (op
== P_LINKW_FP
|| op
== P_LINKL_FP
|| op
== P_PEA_FP
)
675 cache
->saved_regs
[M68K_FP_REGNUM
] = 0;
676 cache
->sp_offset
+= 4;
677 if (op
== P_LINKW_FP
)
679 /* link.w %fp, #-N */
680 /* link.w %fp, #0; adda.l #-N, %sp */
681 cache
->locals
= -read_memory_integer (pc
+ 2, 2, byte_order
);
683 if (pc
+ 4 < current_pc
&& cache
->locals
== 0)
685 op
= read_memory_unsigned_integer (pc
+ 4, 2, byte_order
);
686 if (op
== P_ADDAL_SP
)
688 cache
->locals
= read_memory_integer (pc
+ 6, 4, byte_order
);
695 else if (op
== P_LINKL_FP
)
697 /* link.l %fp, #-N */
698 cache
->locals
= -read_memory_integer (pc
+ 2, 4, byte_order
);
703 /* pea (%fp); movea.l %sp, %fp */
706 if (pc
+ 2 < current_pc
)
708 op
= read_memory_unsigned_integer (pc
+ 2, 2, byte_order
);
710 if (op
== P_MOVEAL_SP_FP
)
712 /* move.l %sp, %fp */
720 else if ((op
& 0170777) == P_SUBQW_SP
|| (op
& 0170777) == P_SUBQL_SP
)
722 /* subq.[wl] #N,%sp */
723 /* subq.[wl] #8,%sp; subq.[wl] #N,%sp */
724 cache
->locals
= (op
& 07000) == 0 ? 8 : (op
& 07000) >> 9;
725 if (pc
+ 2 < current_pc
)
727 op
= read_memory_unsigned_integer (pc
+ 2, 2, byte_order
);
728 if ((op
& 0170777) == P_SUBQW_SP
|| (op
& 0170777) == P_SUBQL_SP
)
730 cache
->locals
+= (op
& 07000) == 0 ? 8 : (op
& 07000) >> 9;
736 else if (op
== P_ADDAW_SP
|| op
== P_LEA_SP_SP
)
739 /* lea (-N,%sp),%sp */
740 cache
->locals
= -read_memory_integer (pc
+ 2, 2, byte_order
);
743 else if (op
== P_ADDAL_SP
)
746 cache
->locals
= -read_memory_integer (pc
+ 2, 4, byte_order
);
753 /* Check whether PC points at code that saves registers on the stack.
754 If so, it updates CACHE and returns the address of the first
755 instruction after the register saves or CURRENT_PC, whichever is
756 smaller. Otherwise, return PC. */
759 m68k_analyze_register_saves (struct gdbarch
*gdbarch
, CORE_ADDR pc
,
760 CORE_ADDR current_pc
,
761 struct m68k_frame_cache
*cache
)
763 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
764 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
766 if (cache
->locals
>= 0)
772 offset
= -4 - cache
->locals
;
773 while (pc
< current_pc
)
775 op
= read_memory_unsigned_integer (pc
, 2, byte_order
);
776 if (op
== P_FMOVEMX_SP
777 && tdep
->fpregs_present
)
779 /* fmovem.x REGS,-(%sp) */
780 op
= read_memory_unsigned_integer (pc
+ 2, 2, byte_order
);
781 if ((op
& 0xff00) == 0xe000)
784 for (i
= 0; i
< 16; i
++, mask
>>= 1)
788 cache
->saved_regs
[i
+ M68K_FP0_REGNUM
] = offset
;
797 else if ((op
& 0177760) == P_MOVEL_SP
)
799 /* move.l %R,-(%sp) */
801 cache
->saved_regs
[regno
] = offset
;
805 else if (op
== P_MOVEML_SP
)
807 /* movem.l REGS,-(%sp) */
808 mask
= read_memory_unsigned_integer (pc
+ 2, 2, byte_order
);
809 for (i
= 0; i
< 16; i
++, mask
>>= 1)
813 cache
->saved_regs
[15 - i
] = offset
;
828 /* Do a full analysis of the prologue at PC and update CACHE
829 accordingly. Bail out early if CURRENT_PC is reached. Return the
830 address where the analysis stopped.
832 We handle all cases that can be generated by gcc.
834 For allocating a stack frame:
838 pea (%fp); move.l %sp,%fp
839 link.w %a6,#0; add.l #-N,%sp
842 subq.w #8,%sp; subq.w #N-8,%sp
847 For saving registers:
851 move.l R1,-(%sp); move.l R2,-(%sp)
854 For setting up the PIC register:
861 m68k_analyze_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
,
862 CORE_ADDR current_pc
, struct m68k_frame_cache
*cache
)
864 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
867 pc
= m68k_analyze_frame_setup (gdbarch
, pc
, current_pc
, cache
);
868 pc
= m68k_analyze_register_saves (gdbarch
, pc
, current_pc
, cache
);
869 if (pc
>= current_pc
)
872 /* Check for GOT setup. */
873 op
= read_memory_unsigned_integer (pc
, 4, byte_order
);
874 if (op
== P_LEA_PC_A5
)
876 /* lea (%pc,N),%a5 */
883 /* Return PC of first real instruction. */
886 m68k_skip_prologue (struct gdbarch
*gdbarch
, CORE_ADDR start_pc
)
888 struct m68k_frame_cache cache
;
892 pc
= m68k_analyze_prologue (gdbarch
, start_pc
, (CORE_ADDR
) -1, &cache
);
893 if (cache
.locals
< 0)
899 m68k_unwind_pc (struct gdbarch
*gdbarch
, frame_info_ptr next_frame
)
903 frame_unwind_register (next_frame
, gdbarch_pc_regnum (gdbarch
), buf
);
904 return extract_typed_address (buf
, builtin_type (gdbarch
)->builtin_func_ptr
);
909 static struct m68k_frame_cache
*
910 m68k_frame_cache (frame_info_ptr this_frame
, void **this_cache
)
912 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
913 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
914 struct m68k_frame_cache
*cache
;
919 return (struct m68k_frame_cache
*) *this_cache
;
921 cache
= m68k_alloc_frame_cache ();
924 /* In principle, for normal frames, %fp holds the frame pointer,
925 which holds the base address for the current stack frame.
926 However, for functions that don't need it, the frame pointer is
927 optional. For these "frameless" functions the frame pointer is
928 actually the frame pointer of the calling frame. Signal
929 trampolines are just a special case of a "frameless" function.
930 They (usually) share their frame pointer with the frame that was
931 in progress when the signal occurred. */
933 get_frame_register (this_frame
, M68K_FP_REGNUM
, buf
);
934 cache
->base
= extract_unsigned_integer (buf
, 4, byte_order
);
935 if (cache
->base
== 0)
938 /* For normal frames, %pc is stored at 4(%fp). */
939 cache
->saved_regs
[M68K_PC_REGNUM
] = 4;
941 cache
->pc
= get_frame_func (this_frame
);
943 m68k_analyze_prologue (get_frame_arch (this_frame
), cache
->pc
,
944 get_frame_pc (this_frame
), cache
);
946 if (cache
->locals
< 0)
948 /* We didn't find a valid frame, which means that CACHE->base
949 currently holds the frame pointer for our calling frame. If
950 we're at the start of a function, or somewhere half-way its
951 prologue, the function's frame probably hasn't been fully
952 setup yet. Try to reconstruct the base address for the stack
953 frame by looking at the stack pointer. For truly "frameless"
954 functions this might work too. */
956 get_frame_register (this_frame
, M68K_SP_REGNUM
, buf
);
957 cache
->base
= extract_unsigned_integer (buf
, 4, byte_order
)
961 /* Now that we have the base address for the stack frame we can
962 calculate the value of %sp in the calling frame. */
963 cache
->saved_sp
= cache
->base
+ 8;
965 /* Adjust all the saved registers such that they contain addresses
966 instead of offsets. */
967 for (i
= 0; i
< M68K_NUM_REGS
; i
++)
968 if (cache
->saved_regs
[i
] != -1)
969 cache
->saved_regs
[i
] += cache
->base
;
975 m68k_frame_this_id (frame_info_ptr this_frame
, void **this_cache
,
976 struct frame_id
*this_id
)
978 struct m68k_frame_cache
*cache
= m68k_frame_cache (this_frame
, this_cache
);
980 /* This marks the outermost frame. */
981 if (cache
->base
== 0)
984 /* See the end of m68k_push_dummy_call. */
985 *this_id
= frame_id_build (cache
->base
+ 8, cache
->pc
);
988 static struct value
*
989 m68k_frame_prev_register (frame_info_ptr this_frame
, void **this_cache
,
992 struct m68k_frame_cache
*cache
= m68k_frame_cache (this_frame
, this_cache
);
994 gdb_assert (regnum
>= 0);
996 if (regnum
== M68K_SP_REGNUM
&& cache
->saved_sp
)
997 return frame_unwind_got_constant (this_frame
, regnum
, cache
->saved_sp
);
999 if (regnum
< M68K_NUM_REGS
&& cache
->saved_regs
[regnum
] != -1)
1000 return frame_unwind_got_memory (this_frame
, regnum
,
1001 cache
->saved_regs
[regnum
]);
1003 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
1006 static const struct frame_unwind m68k_frame_unwind
=
1010 default_frame_unwind_stop_reason
,
1012 m68k_frame_prev_register
,
1014 default_frame_sniffer
1018 m68k_frame_base_address (frame_info_ptr this_frame
, void **this_cache
)
1020 struct m68k_frame_cache
*cache
= m68k_frame_cache (this_frame
, this_cache
);
1025 static const struct frame_base m68k_frame_base
=
1028 m68k_frame_base_address
,
1029 m68k_frame_base_address
,
1030 m68k_frame_base_address
1033 static struct frame_id
1034 m68k_dummy_id (struct gdbarch
*gdbarch
, frame_info_ptr this_frame
)
1038 fp
= get_frame_register_unsigned (this_frame
, M68K_FP_REGNUM
);
1040 /* See the end of m68k_push_dummy_call. */
1041 return frame_id_build (fp
+ 8, get_frame_pc (this_frame
));
1045 /* Figure out where the longjmp will land. Slurp the args out of the stack.
1046 We expect the first arg to be a pointer to the jmp_buf structure from which
1047 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
1048 This routine returns true on success. */
1051 m68k_get_longjmp_target (frame_info_ptr frame
, CORE_ADDR
*pc
)
1054 CORE_ADDR sp
, jb_addr
;
1055 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1056 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
1057 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1059 if (tdep
->jb_pc
< 0)
1061 internal_error (_("m68k_get_longjmp_target: not implemented"));
1065 buf
= (gdb_byte
*) alloca (gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
);
1066 sp
= get_frame_register_unsigned (frame
, gdbarch_sp_regnum (gdbarch
));
1068 if (target_read_memory (sp
+ SP_ARG0
, /* Offset of first arg on stack. */
1069 buf
, gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
))
1072 jb_addr
= extract_unsigned_integer (buf
, gdbarch_ptr_bit (gdbarch
)
1073 / TARGET_CHAR_BIT
, byte_order
);
1075 if (target_read_memory (jb_addr
+ tdep
->jb_pc
* tdep
->jb_elt_size
, buf
,
1076 gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
),
1080 *pc
= extract_unsigned_integer (buf
, gdbarch_ptr_bit (gdbarch
)
1081 / TARGET_CHAR_BIT
, byte_order
);
1086 /* This is the implementation of gdbarch method
1087 return_in_first_hidden_param_p. */
1090 m68k_return_in_first_hidden_param_p (struct gdbarch
*gdbarch
,
1096 /* System V Release 4 (SVR4). */
1099 m68k_svr4_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1101 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
1103 /* SVR4 uses a different calling convention. */
1104 set_gdbarch_return_value (gdbarch
, m68k_svr4_return_value
);
1106 /* SVR4 uses %a0 instead of %a1. */
1107 tdep
->struct_value_regnum
= M68K_A0_REGNUM
;
1109 /* SVR4 returns pointers in %a0. */
1110 tdep
->pointer_result_regnum
= M68K_A0_REGNUM
;
1113 /* GCC's m68k "embedded" ABI. This is like the SVR4 ABI, but pointer
1114 values are returned in %d0, not %a0. */
1117 m68k_embedded_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1119 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
1121 m68k_svr4_init_abi (info
, gdbarch
);
1122 tdep
->pointer_result_regnum
= M68K_D0_REGNUM
;
1127 /* Function: m68k_gdbarch_init
1128 Initializer function for the m68k gdbarch vector.
1129 Called by gdbarch. Sets up the gdbarch vector(s) for this target. */
1131 static struct gdbarch
*
1132 m68k_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
1134 struct gdbarch
*gdbarch
;
1135 struct gdbarch_list
*best_arch
;
1136 tdesc_arch_data_up tdesc_data
;
1138 enum m68k_flavour flavour
= m68k_no_flavour
;
1140 const struct floatformat
**long_double_format
= floatformats_m68881_ext
;
1142 /* Check any target description for validity. */
1143 if (tdesc_has_registers (info
.target_desc
))
1145 const struct tdesc_feature
*feature
;
1148 feature
= tdesc_find_feature (info
.target_desc
,
1149 "org.gnu.gdb.m68k.core");
1151 if (feature
== NULL
)
1153 feature
= tdesc_find_feature (info
.target_desc
,
1154 "org.gnu.gdb.coldfire.core");
1155 if (feature
!= NULL
)
1156 flavour
= m68k_coldfire_flavour
;
1159 if (feature
== NULL
)
1161 feature
= tdesc_find_feature (info
.target_desc
,
1162 "org.gnu.gdb.fido.core");
1163 if (feature
!= NULL
)
1164 flavour
= m68k_fido_flavour
;
1167 if (feature
== NULL
)
1170 tdesc_data
= tdesc_data_alloc ();
1173 for (i
= 0; i
<= M68K_PC_REGNUM
; i
++)
1174 valid_p
&= tdesc_numbered_register (feature
, tdesc_data
.get (), i
,
1175 m68k_register_names
[i
]);
1180 feature
= tdesc_find_feature (info
.target_desc
,
1181 "org.gnu.gdb.coldfire.fp");
1182 if (feature
!= NULL
)
1185 for (i
= M68K_FP0_REGNUM
; i
<= M68K_FPI_REGNUM
; i
++)
1186 valid_p
&= tdesc_numbered_register (feature
, tdesc_data
.get (), i
,
1187 m68k_register_names
[i
]);
1195 /* The mechanism for returning floating values from function
1196 and the type of long double depend on whether we're
1197 on ColdFire or standard m68k. */
1199 if (info
.bfd_arch_info
&& info
.bfd_arch_info
->mach
!= 0)
1201 const bfd_arch_info_type
*coldfire_arch
=
1202 bfd_lookup_arch (bfd_arch_m68k
, bfd_mach_mcf_isa_a_nodiv
);
1205 && ((*info
.bfd_arch_info
->compatible
)
1206 (info
.bfd_arch_info
, coldfire_arch
)))
1207 flavour
= m68k_coldfire_flavour
;
1210 /* Try to figure out if the arch uses floating registers to return
1211 floating point values from functions. On ColdFire, floating
1212 point values are returned in D0. */
1213 int float_return
= 0;
1214 if (has_fp
&& flavour
!= m68k_coldfire_flavour
)
1217 if (info
.abfd
&& bfd_get_flavour (info
.abfd
) == bfd_target_elf_flavour
)
1219 int fp_abi
= bfd_elf_get_obj_attr_int (info
.abfd
, OBJ_ATTR_GNU
,
1220 Tag_GNU_M68K_ABI_FP
);
1223 else if (fp_abi
== 2)
1226 #endif /* HAVE_ELF */
1228 /* If there is already a candidate, use it. */
1229 for (best_arch
= gdbarch_list_lookup_by_info (arches
, &info
);
1231 best_arch
= gdbarch_list_lookup_by_info (best_arch
->next
, &info
))
1233 m68k_gdbarch_tdep
*tdep
1234 = gdbarch_tdep
<m68k_gdbarch_tdep
> (best_arch
->gdbarch
);
1236 if (flavour
!= tdep
->flavour
)
1239 if (has_fp
!= tdep
->fpregs_present
)
1242 if (float_return
!= tdep
->float_return
)
1248 if (best_arch
!= NULL
)
1249 return best_arch
->gdbarch
;
1251 m68k_gdbarch_tdep
*tdep
= new m68k_gdbarch_tdep
;
1252 gdbarch
= gdbarch_alloc (&info
, tdep
);
1253 tdep
->fpregs_present
= has_fp
;
1254 tdep
->float_return
= float_return
;
1255 tdep
->flavour
= flavour
;
1257 if (flavour
== m68k_coldfire_flavour
|| flavour
== m68k_fido_flavour
)
1258 long_double_format
= floatformats_ieee_double
;
1259 set_gdbarch_long_double_format (gdbarch
, long_double_format
);
1260 set_gdbarch_long_double_bit (gdbarch
, long_double_format
[0]->totalsize
);
1262 set_gdbarch_skip_prologue (gdbarch
, m68k_skip_prologue
);
1263 set_gdbarch_breakpoint_kind_from_pc (gdbarch
, m68k_breakpoint::kind_from_pc
);
1264 set_gdbarch_sw_breakpoint_from_kind (gdbarch
, m68k_breakpoint::bp_from_kind
);
1266 /* Stack grows down. */
1267 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
1268 set_gdbarch_frame_align (gdbarch
, m68k_frame_align
);
1270 set_gdbarch_believe_pcc_promotion (gdbarch
, 1);
1271 if (flavour
== m68k_coldfire_flavour
|| flavour
== m68k_fido_flavour
)
1272 set_gdbarch_decr_pc_after_break (gdbarch
, 2);
1274 set_gdbarch_frame_args_skip (gdbarch
, 8);
1275 set_gdbarch_dwarf2_reg_to_regnum (gdbarch
, m68k_dwarf_reg_to_regnum
);
1277 set_gdbarch_register_type (gdbarch
, m68k_register_type
);
1278 set_gdbarch_register_name (gdbarch
, m68k_register_name
);
1279 set_gdbarch_num_regs (gdbarch
, M68K_NUM_REGS
);
1280 set_gdbarch_sp_regnum (gdbarch
, M68K_SP_REGNUM
);
1281 set_gdbarch_pc_regnum (gdbarch
, M68K_PC_REGNUM
);
1282 set_gdbarch_ps_regnum (gdbarch
, M68K_PS_REGNUM
);
1283 set_gdbarch_convert_register_p (gdbarch
, m68k_convert_register_p
);
1284 set_gdbarch_register_to_value (gdbarch
, m68k_register_to_value
);
1285 set_gdbarch_value_to_register (gdbarch
, m68k_value_to_register
);
1288 set_gdbarch_fp0_regnum (gdbarch
, M68K_FP0_REGNUM
);
1290 /* Function call & return. */
1291 set_gdbarch_push_dummy_call (gdbarch
, m68k_push_dummy_call
);
1292 set_gdbarch_return_value (gdbarch
, m68k_return_value
);
1293 set_gdbarch_return_in_first_hidden_param_p (gdbarch
,
1294 m68k_return_in_first_hidden_param_p
);
1296 #if defined JB_PC && defined JB_ELEMENT_SIZE
1297 tdep
->jb_pc
= JB_PC
;
1298 tdep
->jb_elt_size
= JB_ELEMENT_SIZE
;
1302 tdep
->pointer_result_regnum
= M68K_D0_REGNUM
;
1303 tdep
->struct_value_regnum
= M68K_A1_REGNUM
;
1304 tdep
->struct_return
= reg_struct_return
;
1306 /* Frame unwinder. */
1307 set_gdbarch_dummy_id (gdbarch
, m68k_dummy_id
);
1308 set_gdbarch_unwind_pc (gdbarch
, m68k_unwind_pc
);
1310 /* Hook in the DWARF CFI frame unwinder. */
1311 dwarf2_append_unwinders (gdbarch
);
1313 frame_base_set_default (gdbarch
, &m68k_frame_base
);
1315 /* Hook in ABI-specific overrides, if they have been registered. */
1316 gdbarch_init_osabi (info
, gdbarch
);
1318 /* Now we have tuned the configuration, set a few final things,
1319 based on what the OS ABI has told us. */
1321 if (tdep
->jb_pc
>= 0)
1322 set_gdbarch_get_longjmp_target (gdbarch
, m68k_get_longjmp_target
);
1324 frame_unwind_append_unwinder (gdbarch
, &m68k_frame_unwind
);
1326 if (tdesc_data
!= nullptr)
1327 tdesc_use_registers (gdbarch
, info
.target_desc
, std::move (tdesc_data
));
1334 m68k_dump_tdep (struct gdbarch
*gdbarch
, struct ui_file
*file
)
1336 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
1342 /* OSABI sniffer for m68k. */
1344 static enum gdb_osabi
1345 m68k_osabi_sniffer (bfd
*abfd
)
1347 unsigned int elfosabi
= elf_elfheader (abfd
)->e_ident
[EI_OSABI
];
1349 if (elfosabi
== ELFOSABI_NONE
)
1350 return GDB_OSABI_SVR4
;
1352 return GDB_OSABI_UNKNOWN
;
1355 void _initialize_m68k_tdep ();
1357 _initialize_m68k_tdep ()
1359 gdbarch_register (bfd_arch_m68k
, m68k_gdbarch_init
, m68k_dump_tdep
);
1361 gdbarch_register_osabi_sniffer (bfd_arch_m68k
, bfd_target_elf_flavour
,
1362 m68k_osabi_sniffer
);
1363 gdbarch_register_osabi (bfd_arch_m68k
, 0, GDB_OSABI_SVR4
,
1364 m68k_embedded_init_abi
);