1 /* Target-dependent code for the Motorola 68000 series.
3 Copyright (C) 1990-2024 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/>. */
20 #include "dwarf2/frame.h"
21 #include "extract-store-integer.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
)
106 type_allocator
alloc (gdbarch
);
107 tdep
->m68881_ext_type
108 = init_float_type (alloc
, -1, "builtin_type_m68881_ext",
109 floatformats_m68881_ext
);
112 return tdep
->m68881_ext_type
;
115 /* Return the GDB type object for the "standard" data type of data in
116 register N. This should be int for D0-D7, SR, FPCONTROL and
117 FPSTATUS, long double for FP0-FP7, and void pointer for all others
118 (A0-A7, PC, FPIADDR). Note, for registers which contain
119 addresses return pointer to void, not pointer to char, because we
120 don't want to attempt to print the string after printing the
124 m68k_register_type (struct gdbarch
*gdbarch
, int regnum
)
126 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
128 if (tdep
->fpregs_present
)
130 if (regnum
>= gdbarch_fp0_regnum (gdbarch
)
131 && regnum
<= gdbarch_fp0_regnum (gdbarch
) + 7)
133 if (tdep
->flavour
== m68k_coldfire_flavour
)
134 return builtin_type (gdbarch
)->builtin_double
;
136 return m68881_ext_type (gdbarch
);
139 if (regnum
== M68K_FPI_REGNUM
)
140 return builtin_type (gdbarch
)->builtin_func_ptr
;
142 if (regnum
== M68K_FPC_REGNUM
|| regnum
== M68K_FPS_REGNUM
)
143 return builtin_type (gdbarch
)->builtin_int32
;
147 if (regnum
>= M68K_FP0_REGNUM
&& regnum
<= M68K_FPI_REGNUM
)
148 return builtin_type (gdbarch
)->builtin_int0
;
151 if (regnum
== gdbarch_pc_regnum (gdbarch
))
152 return builtin_type (gdbarch
)->builtin_func_ptr
;
154 if (regnum
>= M68K_A0_REGNUM
&& regnum
<= M68K_A0_REGNUM
+ 7)
155 return builtin_type (gdbarch
)->builtin_data_ptr
;
157 if (regnum
== M68K_PS_REGNUM
)
158 return m68k_ps_type (gdbarch
);
160 return builtin_type (gdbarch
)->builtin_int32
;
163 static const char * const m68k_register_names
[] = {
164 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
165 "a0", "a1", "a2", "a3", "a4", "a5", "fp", "sp",
167 "fp0", "fp1", "fp2", "fp3", "fp4", "fp5", "fp6", "fp7",
168 "fpcontrol", "fpstatus", "fpiaddr"
171 /* Function: m68k_register_name
172 Returns the name of the standard m68k register regnum. */
175 m68k_register_name (struct gdbarch
*gdbarch
, int regnum
)
177 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
179 static_assert (ARRAY_SIZE (m68k_register_names
) == M68K_NUM_REGS
);
180 if (regnum
>= M68K_FP0_REGNUM
&& regnum
<= M68K_FPI_REGNUM
181 && tdep
->fpregs_present
== 0)
184 return m68k_register_names
[regnum
];
187 /* Return nonzero if a value of type TYPE stored in register REGNUM
188 needs any special handling. */
191 m68k_convert_register_p (struct gdbarch
*gdbarch
,
192 int regnum
, struct type
*type
)
194 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
196 if (!tdep
->fpregs_present
)
198 return (regnum
>= M68K_FP0_REGNUM
&& regnum
<= M68K_FP0_REGNUM
+ 7
199 /* We only support floating-point values. */
200 && type
->code () == TYPE_CODE_FLT
201 && type
!= register_type (gdbarch
, M68K_FP0_REGNUM
));
204 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
205 return its contents in TO. */
208 m68k_register_to_value (const frame_info_ptr
&frame
, int regnum
,
209 struct type
*type
, gdb_byte
*to
,
210 int *optimizedp
, int *unavailablep
)
212 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
213 gdb_byte from
[M68K_MAX_REGISTER_SIZE
];
214 struct type
*fpreg_type
= register_type (gdbarch
, M68K_FP0_REGNUM
);
216 gdb_assert (type
->code () == TYPE_CODE_FLT
);
218 /* Convert to TYPE. */
220 = gdb::make_array_view (from
, register_size (gdbarch
, regnum
));
221 frame_info_ptr next_frame
= get_next_frame_sentinel_okay (frame
);
222 if (!get_frame_register_bytes (next_frame
, regnum
, 0, from_view
, optimizedp
,
226 target_float_convert (from
, fpreg_type
, to
, type
);
227 *optimizedp
= *unavailablep
= 0;
231 /* Write the contents FROM of a value of type TYPE into register
232 REGNUM in frame FRAME. */
235 m68k_value_to_register (const frame_info_ptr
&frame
, int regnum
,
236 struct type
*type
, const gdb_byte
*from
)
238 gdb_byte to
[M68K_MAX_REGISTER_SIZE
];
239 gdbarch
*arch
= get_frame_arch (frame
);
240 struct type
*fpreg_type
= register_type (arch
, M68K_FP0_REGNUM
);
242 /* We only support floating-point values. */
243 if (type
->code () != TYPE_CODE_FLT
)
245 warning (_("Cannot convert non-floating-point type "
246 "to floating-point register value."));
250 /* Convert from TYPE. */
251 target_float_convert (from
, type
, to
, fpreg_type
);
252 auto to_view
= gdb::make_array_view (to
, fpreg_type
->length ());
253 put_frame_register (get_next_frame_sentinel_okay (frame
), regnum
, to_view
);
257 /* There is a fair number of calling conventions that are in somewhat
258 wide use. The 68000/08/10 don't support an FPU, not even as a
259 coprocessor. All function return values are stored in %d0/%d1.
260 Structures are returned in a static buffer, a pointer to which is
261 returned in %d0. This means that functions returning a structure
262 are not re-entrant. To avoid this problem some systems use a
263 convention where the caller passes a pointer to a buffer in %a1
264 where the return values is to be stored. This convention is the
265 default, and is implemented in the function m68k_return_value.
267 The 68020/030/040/060 do support an FPU, either as a coprocessor
268 (68881/2) or built-in (68040/68060). That's why System V release 4
269 (SVR4) introduces a new calling convention specified by the SVR4
270 psABI. Integer values are returned in %d0/%d1, pointer return
271 values in %a0 and floating values in %fp0. When calling functions
272 returning a structure the caller should pass a pointer to a buffer
273 for the return value in %a0. This convention is implemented in the
274 function m68k_svr4_return_value, and by appropriately setting the
275 struct_value_regnum member of `struct gdbarch_tdep'.
277 GNU/Linux returns values in the same way as SVR4 does, but uses %a1
278 for passing the structure return value buffer.
280 GCC can also generate code where small structures are returned in
281 %d0/%d1 instead of in memory by using -freg-struct-return. This is
282 the default on NetBSD a.out, OpenBSD and GNU/Linux and several
283 embedded systems. This convention is implemented by setting the
284 struct_return member of `struct gdbarch_tdep' to reg_struct_return.
286 GCC also has an "embedded" ABI. This works like the SVR4 ABI,
287 except that pointers are returned in %D0. This is implemented by
288 setting the pointer_result_regnum member of `struct gdbarch_tdep'
291 /* Read a function return value of TYPE from REGCACHE, and copy that
295 m68k_extract_return_value (struct type
*type
, struct regcache
*regcache
,
298 int len
= type
->length ();
299 gdb_byte buf
[M68K_MAX_REGISTER_SIZE
];
301 if (type
->code () == TYPE_CODE_PTR
&& len
== 4)
303 struct gdbarch
*gdbarch
= regcache
->arch ();
304 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
305 regcache
->raw_read (tdep
->pointer_result_regnum
, valbuf
);
309 regcache
->raw_read (M68K_D0_REGNUM
, buf
);
310 memcpy (valbuf
, buf
+ (4 - len
), len
);
314 regcache
->raw_read (M68K_D0_REGNUM
, buf
);
315 memcpy (valbuf
, buf
+ (8 - len
), len
- 4);
316 regcache
->raw_read (M68K_D1_REGNUM
, valbuf
+ (len
- 4));
319 internal_error (_("Cannot extract return value of %d bytes long."), len
);
323 m68k_svr4_extract_return_value (struct type
*type
, struct regcache
*regcache
,
326 gdb_byte buf
[M68K_MAX_REGISTER_SIZE
];
327 struct gdbarch
*gdbarch
= regcache
->arch ();
328 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
330 if (tdep
->float_return
&& type
->code () == TYPE_CODE_FLT
)
332 struct type
*fpreg_type
= register_type (gdbarch
, M68K_FP0_REGNUM
);
333 regcache
->raw_read (M68K_FP0_REGNUM
, buf
);
334 target_float_convert (buf
, fpreg_type
, valbuf
, type
);
337 m68k_extract_return_value (type
, regcache
, valbuf
);
340 /* Write a function return value of TYPE from VALBUF into REGCACHE. */
343 m68k_store_return_value (struct type
*type
, struct regcache
*regcache
,
344 const gdb_byte
*valbuf
)
346 int len
= type
->length ();
348 if (type
->code () == TYPE_CODE_PTR
&& len
== 4)
350 struct gdbarch
*gdbarch
= regcache
->arch ();
351 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
352 regcache
->raw_write (tdep
->pointer_result_regnum
, valbuf
);
353 /* gdb historically also set D0 in the SVR4 case. */
354 if (tdep
->pointer_result_regnum
!= M68K_D0_REGNUM
)
355 regcache
->raw_write (M68K_D0_REGNUM
, valbuf
);
358 regcache
->raw_write_part (M68K_D0_REGNUM
, 4 - len
, len
, valbuf
);
361 regcache
->raw_write_part (M68K_D0_REGNUM
, 8 - len
, len
- 4, valbuf
);
362 regcache
->raw_write (M68K_D1_REGNUM
, valbuf
+ (len
- 4));
365 internal_error (_("Cannot store return value of %d bytes long."), len
);
369 m68k_svr4_store_return_value (struct type
*type
, struct regcache
*regcache
,
370 const gdb_byte
*valbuf
)
372 struct gdbarch
*gdbarch
= regcache
->arch ();
373 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
375 if (tdep
->float_return
&& type
->code () == TYPE_CODE_FLT
)
377 struct type
*fpreg_type
= register_type (gdbarch
, M68K_FP0_REGNUM
);
378 gdb_byte buf
[M68K_MAX_REGISTER_SIZE
];
379 target_float_convert (valbuf
, type
, buf
, fpreg_type
);
380 regcache
->raw_write (M68K_FP0_REGNUM
, buf
);
383 m68k_store_return_value (type
, regcache
, valbuf
);
386 /* Return non-zero if TYPE, which is assumed to be a structure, union or
387 complex type, should be returned in registers for architecture
391 m68k_reg_struct_return_p (struct gdbarch
*gdbarch
, struct type
*type
)
393 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
394 enum type_code code
= type
->code ();
395 int len
= type
->length ();
397 gdb_assert (code
== TYPE_CODE_STRUCT
|| code
== TYPE_CODE_UNION
398 || code
== TYPE_CODE_COMPLEX
|| code
== TYPE_CODE_ARRAY
);
400 if (tdep
->struct_return
== pcc_struct_return
)
403 const bool is_vector
= code
== TYPE_CODE_ARRAY
&& type
->is_vector ();
406 && check_typedef (type
->target_type ())->code () == TYPE_CODE_FLT
)
409 /* According to m68k_return_in_memory in the m68k GCC back-end,
410 strange things happen for small aggregate types. Aggregate types
411 with only one component are always returned like the type of the
412 component. Aggregate types whose size is 2, 4, or 8 are returned
413 in registers if their natural alignment is at least 16 bits.
415 We reject vectors here, as experimentally this gives the correct
417 if (!is_vector
&& (len
== 2 || len
== 4 || len
== 8))
418 return type_align (type
) >= 2;
420 return (len
== 1 || len
== 2 || len
== 4 || len
== 8);
423 /* Determine, for architecture GDBARCH, how a return value of TYPE
424 should be returned. If it is supposed to be returned in registers,
425 and READBUF is non-zero, read the appropriate value from REGCACHE,
426 and copy it into READBUF. If WRITEBUF is non-zero, write the value
427 from WRITEBUF into REGCACHE. */
429 static enum return_value_convention
430 m68k_return_value (struct gdbarch
*gdbarch
, struct value
*function
,
431 struct type
*type
, struct regcache
*regcache
,
432 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
434 enum type_code code
= type
->code ();
436 /* GCC returns a `long double' in memory too. */
437 if (((code
== TYPE_CODE_STRUCT
|| code
== TYPE_CODE_UNION
438 || code
== TYPE_CODE_COMPLEX
|| code
== TYPE_CODE_ARRAY
)
439 && !m68k_reg_struct_return_p (gdbarch
, type
))
440 || (code
== TYPE_CODE_FLT
&& type
->length () == 12))
442 /* The default on m68k is to return structures in static memory.
443 Consequently a function must return the address where we can
444 find the return value. */
450 regcache_raw_read_unsigned (regcache
, M68K_D0_REGNUM
, &addr
);
451 read_memory (addr
, readbuf
, type
->length ());
454 return RETURN_VALUE_ABI_RETURNS_ADDRESS
;
458 m68k_extract_return_value (type
, regcache
, readbuf
);
460 m68k_store_return_value (type
, regcache
, writebuf
);
462 return RETURN_VALUE_REGISTER_CONVENTION
;
465 static enum return_value_convention
466 m68k_svr4_return_value (struct gdbarch
*gdbarch
, struct value
*function
,
467 struct type
*type
, struct regcache
*regcache
,
468 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
470 enum type_code code
= type
->code ();
471 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
473 /* Aggregates with a single member are always returned like their
475 if ((code
== TYPE_CODE_STRUCT
|| code
== TYPE_CODE_UNION
)
476 && type
->num_fields () == 1)
478 type
= check_typedef (type
->field (0).type ());
479 return m68k_svr4_return_value (gdbarch
, function
, type
, regcache
,
483 if (((code
== TYPE_CODE_STRUCT
|| code
== TYPE_CODE_UNION
484 || code
== TYPE_CODE_COMPLEX
|| code
== TYPE_CODE_ARRAY
)
485 && !m68k_reg_struct_return_p (gdbarch
, type
))
486 /* GCC may return a `long double' in memory too. */
487 || (!tdep
->float_return
488 && code
== TYPE_CODE_FLT
489 && type
->length () == 12))
491 /* The System V ABI says that:
493 "A function returning a structure or union also sets %a0 to
494 the value it finds in %a0. Thus when the caller receives
495 control again, the address of the returned object resides in
498 So the ABI guarantees that we can always find the return
499 value just after the function has returned.
501 However, GCC also implements the "embedded" ABI. That ABI
502 does not preserve %a0 across calls, but does write the value
509 regcache_raw_read_unsigned (regcache
, tdep
->pointer_result_regnum
,
511 read_memory (addr
, readbuf
, type
->length ());
514 return RETURN_VALUE_ABI_RETURNS_ADDRESS
;
518 m68k_svr4_extract_return_value (type
, regcache
, readbuf
);
520 m68k_svr4_store_return_value (type
, regcache
, writebuf
);
522 return RETURN_VALUE_REGISTER_CONVENTION
;
526 /* Always align the frame to a 4-byte boundary. This is required on
527 coldfire and harmless on the rest. */
530 m68k_frame_align (struct gdbarch
*gdbarch
, CORE_ADDR sp
)
532 /* Align the stack to four bytes. */
537 m68k_push_dummy_call (struct gdbarch
*gdbarch
, struct value
*function
,
538 struct regcache
*regcache
, CORE_ADDR bp_addr
, int nargs
,
539 struct value
**args
, CORE_ADDR sp
,
540 function_call_return_method return_method
,
541 CORE_ADDR struct_addr
)
543 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
544 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
548 /* Push arguments in reverse order. */
549 for (i
= nargs
- 1; i
>= 0; i
--)
551 struct type
*value_type
= args
[i
]->enclosing_type ();
552 int len
= value_type
->length ();
553 int container_len
= (len
+ 3) & ~3;
556 /* Non-scalars bigger than 4 bytes are left aligned, others are
558 if ((value_type
->code () == TYPE_CODE_STRUCT
559 || value_type
->code () == TYPE_CODE_UNION
560 || value_type
->code () == TYPE_CODE_ARRAY
)
564 offset
= container_len
- len
;
566 write_memory (sp
+ offset
, args
[i
]->contents_all ().data (), len
);
569 /* Store struct value address. */
570 if (return_method
== return_method_struct
)
572 store_unsigned_integer (buf
, 4, byte_order
, struct_addr
);
573 regcache
->cooked_write (tdep
->struct_value_regnum
, buf
);
576 /* Store return address. */
578 store_unsigned_integer (buf
, 4, byte_order
, bp_addr
);
579 write_memory (sp
, buf
, 4);
581 /* Finally, update the stack pointer... */
582 store_unsigned_integer (buf
, 4, byte_order
, sp
);
583 regcache
->cooked_write (M68K_SP_REGNUM
, buf
);
585 /* ...and fake a frame pointer. */
586 regcache
->cooked_write (M68K_FP_REGNUM
, buf
);
588 /* DWARF2/GCC uses the stack address *before* the function call as a
593 /* Convert a dwarf or dwarf2 regnumber to a GDB regnum. */
596 m68k_dwarf_reg_to_regnum (struct gdbarch
*gdbarch
, int num
)
598 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
602 return (num
- 0) + M68K_D0_REGNUM
;
605 return (num
- 8) + M68K_A0_REGNUM
;
606 else if (num
< 24 && tdep
->fpregs_present
)
608 return (num
- 16) + M68K_FP0_REGNUM
;
611 return M68K_PC_REGNUM
;
617 struct m68k_frame_cache
624 /* Saved registers. */
625 CORE_ADDR saved_regs
[M68K_NUM_REGS
];
628 /* Stack space reserved for local variables. */
632 /* Allocate and initialize a frame cache. */
634 static struct m68k_frame_cache
*
635 m68k_alloc_frame_cache (void)
637 struct m68k_frame_cache
*cache
;
640 cache
= FRAME_OBSTACK_ZALLOC (struct m68k_frame_cache
);
644 cache
->sp_offset
= -4;
647 /* Saved registers. We initialize these to -1 since zero is a valid
648 offset (that's where %fp is supposed to be stored). */
649 for (i
= 0; i
< M68K_NUM_REGS
; i
++)
650 cache
->saved_regs
[i
] = -1;
652 /* Frameless until proven otherwise. */
658 /* Check whether PC points at a code that sets up a new stack frame.
659 If so, it updates CACHE and returns the address of the first
660 instruction after the sequence that sets removes the "hidden"
661 argument from the stack or CURRENT_PC, whichever is smaller.
662 Otherwise, return PC. */
665 m68k_analyze_frame_setup (struct gdbarch
*gdbarch
,
666 CORE_ADDR pc
, CORE_ADDR current_pc
,
667 struct m68k_frame_cache
*cache
)
669 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
672 if (pc
>= current_pc
)
675 op
= read_memory_unsigned_integer (pc
, 2, byte_order
);
677 if (op
== P_LINKW_FP
|| op
== P_LINKL_FP
|| op
== P_PEA_FP
)
679 cache
->saved_regs
[M68K_FP_REGNUM
] = 0;
680 cache
->sp_offset
+= 4;
681 if (op
== P_LINKW_FP
)
683 /* link.w %fp, #-N */
684 /* link.w %fp, #0; adda.l #-N, %sp */
685 cache
->locals
= -read_memory_integer (pc
+ 2, 2, byte_order
);
687 if (pc
+ 4 < current_pc
&& cache
->locals
== 0)
689 op
= read_memory_unsigned_integer (pc
+ 4, 2, byte_order
);
690 if (op
== P_ADDAL_SP
)
692 cache
->locals
= read_memory_integer (pc
+ 6, 4, byte_order
);
699 else if (op
== P_LINKL_FP
)
701 /* link.l %fp, #-N */
702 cache
->locals
= -read_memory_integer (pc
+ 2, 4, byte_order
);
707 /* pea (%fp); movea.l %sp, %fp */
710 if (pc
+ 2 < current_pc
)
712 op
= read_memory_unsigned_integer (pc
+ 2, 2, byte_order
);
714 if (op
== P_MOVEAL_SP_FP
)
716 /* move.l %sp, %fp */
724 else if ((op
& 0170777) == P_SUBQW_SP
|| (op
& 0170777) == P_SUBQL_SP
)
726 /* subq.[wl] #N,%sp */
727 /* subq.[wl] #8,%sp; subq.[wl] #N,%sp */
728 cache
->locals
= (op
& 07000) == 0 ? 8 : (op
& 07000) >> 9;
729 if (pc
+ 2 < current_pc
)
731 op
= read_memory_unsigned_integer (pc
+ 2, 2, byte_order
);
732 if ((op
& 0170777) == P_SUBQW_SP
|| (op
& 0170777) == P_SUBQL_SP
)
734 cache
->locals
+= (op
& 07000) == 0 ? 8 : (op
& 07000) >> 9;
740 else if (op
== P_ADDAW_SP
|| op
== P_LEA_SP_SP
)
743 /* lea (-N,%sp),%sp */
744 cache
->locals
= -read_memory_integer (pc
+ 2, 2, byte_order
);
747 else if (op
== P_ADDAL_SP
)
750 cache
->locals
= -read_memory_integer (pc
+ 2, 4, byte_order
);
757 /* Check whether PC points at code that saves registers on the stack.
758 If so, it updates CACHE and returns the address of the first
759 instruction after the register saves or CURRENT_PC, whichever is
760 smaller. Otherwise, return PC. */
763 m68k_analyze_register_saves (struct gdbarch
*gdbarch
, CORE_ADDR pc
,
764 CORE_ADDR current_pc
,
765 struct m68k_frame_cache
*cache
)
767 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
768 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
770 if (cache
->locals
>= 0)
776 offset
= -4 - cache
->locals
;
777 while (pc
< current_pc
)
779 op
= read_memory_unsigned_integer (pc
, 2, byte_order
);
780 if (op
== P_FMOVEMX_SP
781 && tdep
->fpregs_present
)
783 /* fmovem.x REGS,-(%sp) */
784 op
= read_memory_unsigned_integer (pc
+ 2, 2, byte_order
);
785 if ((op
& 0xff00) == 0xe000)
788 for (i
= 0; i
< 16; i
++, mask
>>= 1)
792 cache
->saved_regs
[i
+ M68K_FP0_REGNUM
] = offset
;
801 else if ((op
& 0177760) == P_MOVEL_SP
)
803 /* move.l %R,-(%sp) */
805 cache
->saved_regs
[regno
] = offset
;
809 else if (op
== P_MOVEML_SP
)
811 /* movem.l REGS,-(%sp) */
812 mask
= read_memory_unsigned_integer (pc
+ 2, 2, byte_order
);
813 for (i
= 0; i
< 16; i
++, mask
>>= 1)
817 cache
->saved_regs
[15 - i
] = offset
;
832 /* Do a full analysis of the prologue at PC and update CACHE
833 accordingly. Bail out early if CURRENT_PC is reached. Return the
834 address where the analysis stopped.
836 We handle all cases that can be generated by gcc.
838 For allocating a stack frame:
842 pea (%fp); move.l %sp,%fp
843 link.w %a6,#0; add.l #-N,%sp
846 subq.w #8,%sp; subq.w #N-8,%sp
851 For saving registers:
855 move.l R1,-(%sp); move.l R2,-(%sp)
858 For setting up the PIC register:
865 m68k_analyze_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
,
866 CORE_ADDR current_pc
, struct m68k_frame_cache
*cache
)
868 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
871 pc
= m68k_analyze_frame_setup (gdbarch
, pc
, current_pc
, cache
);
872 pc
= m68k_analyze_register_saves (gdbarch
, pc
, current_pc
, cache
);
873 if (pc
>= current_pc
)
876 /* Check for GOT setup. */
877 op
= read_memory_unsigned_integer (pc
, 4, byte_order
);
878 if (op
== P_LEA_PC_A5
)
880 /* lea (%pc,N),%a5 */
887 /* Return PC of first real instruction. */
890 m68k_skip_prologue (struct gdbarch
*gdbarch
, CORE_ADDR start_pc
)
892 struct m68k_frame_cache cache
;
896 pc
= m68k_analyze_prologue (gdbarch
, start_pc
, (CORE_ADDR
) -1, &cache
);
897 if (cache
.locals
< 0)
903 m68k_unwind_pc (struct gdbarch
*gdbarch
, const frame_info_ptr
&next_frame
)
907 frame_unwind_register (next_frame
, gdbarch_pc_regnum (gdbarch
), buf
);
908 return extract_typed_address (buf
, builtin_type (gdbarch
)->builtin_func_ptr
);
913 static struct m68k_frame_cache
*
914 m68k_frame_cache (const frame_info_ptr
&this_frame
, void **this_cache
)
916 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
917 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
918 struct m68k_frame_cache
*cache
;
923 return (struct m68k_frame_cache
*) *this_cache
;
925 cache
= m68k_alloc_frame_cache ();
928 /* In principle, for normal frames, %fp holds the frame pointer,
929 which holds the base address for the current stack frame.
930 However, for functions that don't need it, the frame pointer is
931 optional. For these "frameless" functions the frame pointer is
932 actually the frame pointer of the calling frame. Signal
933 trampolines are just a special case of a "frameless" function.
934 They (usually) share their frame pointer with the frame that was
935 in progress when the signal occurred. */
937 get_frame_register (this_frame
, M68K_FP_REGNUM
, buf
);
938 cache
->base
= extract_unsigned_integer (buf
, 4, byte_order
);
939 if (cache
->base
== 0)
942 /* For normal frames, %pc is stored at 4(%fp). */
943 cache
->saved_regs
[M68K_PC_REGNUM
] = 4;
945 cache
->pc
= get_frame_func (this_frame
);
947 m68k_analyze_prologue (get_frame_arch (this_frame
), cache
->pc
,
948 get_frame_pc (this_frame
), cache
);
950 if (cache
->locals
< 0)
952 /* We didn't find a valid frame, which means that CACHE->base
953 currently holds the frame pointer for our calling frame. If
954 we're at the start of a function, or somewhere half-way its
955 prologue, the function's frame probably hasn't been fully
956 setup yet. Try to reconstruct the base address for the stack
957 frame by looking at the stack pointer. For truly "frameless"
958 functions this might work too. */
960 get_frame_register (this_frame
, M68K_SP_REGNUM
, buf
);
961 cache
->base
= extract_unsigned_integer (buf
, 4, byte_order
)
965 /* Now that we have the base address for the stack frame we can
966 calculate the value of %sp in the calling frame. */
967 cache
->saved_sp
= cache
->base
+ 8;
969 /* Adjust all the saved registers such that they contain addresses
970 instead of offsets. */
971 for (i
= 0; i
< M68K_NUM_REGS
; i
++)
972 if (cache
->saved_regs
[i
] != -1)
973 cache
->saved_regs
[i
] += cache
->base
;
979 m68k_frame_this_id (const frame_info_ptr
&this_frame
, void **this_cache
,
980 struct frame_id
*this_id
)
982 struct m68k_frame_cache
*cache
= m68k_frame_cache (this_frame
, this_cache
);
984 /* This marks the outermost frame. */
985 if (cache
->base
== 0)
988 /* See the end of m68k_push_dummy_call. */
989 *this_id
= frame_id_build (cache
->base
+ 8, cache
->pc
);
992 static struct value
*
993 m68k_frame_prev_register (const frame_info_ptr
&this_frame
, void **this_cache
,
996 struct m68k_frame_cache
*cache
= m68k_frame_cache (this_frame
, this_cache
);
998 gdb_assert (regnum
>= 0);
1000 if (regnum
== M68K_SP_REGNUM
&& cache
->saved_sp
)
1001 return frame_unwind_got_constant (this_frame
, regnum
, cache
->saved_sp
);
1003 if (regnum
< M68K_NUM_REGS
&& cache
->saved_regs
[regnum
] != -1)
1004 return frame_unwind_got_memory (this_frame
, regnum
,
1005 cache
->saved_regs
[regnum
]);
1007 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
1010 static const struct frame_unwind m68k_frame_unwind
=
1014 default_frame_unwind_stop_reason
,
1016 m68k_frame_prev_register
,
1018 default_frame_sniffer
1022 m68k_frame_base_address (const frame_info_ptr
&this_frame
, void **this_cache
)
1024 struct m68k_frame_cache
*cache
= m68k_frame_cache (this_frame
, this_cache
);
1029 static const struct frame_base m68k_frame_base
=
1032 m68k_frame_base_address
,
1033 m68k_frame_base_address
,
1034 m68k_frame_base_address
1037 static struct frame_id
1038 m68k_dummy_id (struct gdbarch
*gdbarch
, const frame_info_ptr
&this_frame
)
1042 fp
= get_frame_register_unsigned (this_frame
, M68K_FP_REGNUM
);
1044 /* See the end of m68k_push_dummy_call. */
1045 return frame_id_build (fp
+ 8, get_frame_pc (this_frame
));
1049 /* Figure out where the longjmp will land. Slurp the args out of the stack.
1050 We expect the first arg to be a pointer to the jmp_buf structure from which
1051 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
1052 This routine returns true on success. */
1055 m68k_get_longjmp_target (const frame_info_ptr
&frame
, CORE_ADDR
*pc
)
1058 CORE_ADDR sp
, jb_addr
;
1059 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1060 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
1061 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1063 if (tdep
->jb_pc
< 0)
1065 internal_error (_("m68k_get_longjmp_target: not implemented"));
1069 buf
= (gdb_byte
*) alloca (gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
);
1070 sp
= get_frame_register_unsigned (frame
, gdbarch_sp_regnum (gdbarch
));
1072 if (target_read_memory (sp
+ SP_ARG0
, /* Offset of first arg on stack. */
1073 buf
, gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
))
1076 jb_addr
= extract_unsigned_integer (buf
, gdbarch_ptr_bit (gdbarch
)
1077 / TARGET_CHAR_BIT
, byte_order
);
1079 if (target_read_memory (jb_addr
+ tdep
->jb_pc
* tdep
->jb_elt_size
, buf
,
1080 gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
),
1084 *pc
= extract_unsigned_integer (buf
, gdbarch_ptr_bit (gdbarch
)
1085 / TARGET_CHAR_BIT
, byte_order
);
1090 /* This is the implementation of gdbarch method
1091 return_in_first_hidden_param_p. */
1094 m68k_return_in_first_hidden_param_p (struct gdbarch
*gdbarch
,
1100 /* System V Release 4 (SVR4). */
1103 m68k_svr4_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1105 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
1107 /* SVR4 uses a different calling convention. */
1108 set_gdbarch_return_value (gdbarch
, m68k_svr4_return_value
);
1110 /* SVR4 uses %a0 instead of %a1. */
1111 tdep
->struct_value_regnum
= M68K_A0_REGNUM
;
1113 /* SVR4 returns pointers in %a0. */
1114 tdep
->pointer_result_regnum
= M68K_A0_REGNUM
;
1117 /* GCC's m68k "embedded" ABI. This is like the SVR4 ABI, but pointer
1118 values are returned in %d0, not %a0. */
1121 m68k_embedded_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1123 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
1125 m68k_svr4_init_abi (info
, gdbarch
);
1126 tdep
->pointer_result_regnum
= M68K_D0_REGNUM
;
1131 /* Function: m68k_gdbarch_init
1132 Initializer function for the m68k gdbarch vector.
1133 Called by gdbarch. Sets up the gdbarch vector(s) for this target. */
1135 static struct gdbarch
*
1136 m68k_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
1138 struct gdbarch_list
*best_arch
;
1139 tdesc_arch_data_up tdesc_data
;
1141 enum m68k_flavour flavour
= m68k_no_flavour
;
1143 const struct floatformat
**long_double_format
= floatformats_m68881_ext
;
1145 /* Check any target description for validity. */
1146 if (tdesc_has_registers (info
.target_desc
))
1148 const struct tdesc_feature
*feature
;
1151 feature
= tdesc_find_feature (info
.target_desc
,
1152 "org.gnu.gdb.m68k.core");
1154 if (feature
== NULL
)
1156 feature
= tdesc_find_feature (info
.target_desc
,
1157 "org.gnu.gdb.coldfire.core");
1158 if (feature
!= NULL
)
1159 flavour
= m68k_coldfire_flavour
;
1162 if (feature
== NULL
)
1164 feature
= tdesc_find_feature (info
.target_desc
,
1165 "org.gnu.gdb.fido.core");
1166 if (feature
!= NULL
)
1167 flavour
= m68k_fido_flavour
;
1170 if (feature
== NULL
)
1173 tdesc_data
= tdesc_data_alloc ();
1176 for (i
= 0; i
<= M68K_PC_REGNUM
; i
++)
1177 valid_p
&= tdesc_numbered_register (feature
, tdesc_data
.get (), i
,
1178 m68k_register_names
[i
]);
1183 feature
= tdesc_find_feature (info
.target_desc
,
1184 "org.gnu.gdb.coldfire.fp");
1185 if (feature
!= NULL
)
1188 for (i
= M68K_FP0_REGNUM
; i
<= M68K_FPI_REGNUM
; i
++)
1189 valid_p
&= tdesc_numbered_register (feature
, tdesc_data
.get (), i
,
1190 m68k_register_names
[i
]);
1198 /* The mechanism for returning floating values from function
1199 and the type of long double depend on whether we're
1200 on ColdFire or standard m68k. */
1202 if (info
.bfd_arch_info
&& info
.bfd_arch_info
->mach
!= 0)
1204 const bfd_arch_info_type
*coldfire_arch
=
1205 bfd_lookup_arch (bfd_arch_m68k
, bfd_mach_mcf_isa_a_nodiv
);
1208 && ((*info
.bfd_arch_info
->compatible
)
1209 (info
.bfd_arch_info
, coldfire_arch
)))
1210 flavour
= m68k_coldfire_flavour
;
1213 /* Try to figure out if the arch uses floating registers to return
1214 floating point values from functions. On ColdFire, floating
1215 point values are returned in D0. */
1216 int float_return
= 0;
1217 if (has_fp
&& flavour
!= m68k_coldfire_flavour
)
1220 if (info
.abfd
&& bfd_get_flavour (info
.abfd
) == bfd_target_elf_flavour
)
1222 int fp_abi
= bfd_elf_get_obj_attr_int (info
.abfd
, OBJ_ATTR_GNU
,
1223 Tag_GNU_M68K_ABI_FP
);
1226 else if (fp_abi
== 2)
1229 #endif /* HAVE_ELF */
1231 /* If there is already a candidate, use it. */
1232 for (best_arch
= gdbarch_list_lookup_by_info (arches
, &info
);
1234 best_arch
= gdbarch_list_lookup_by_info (best_arch
->next
, &info
))
1236 m68k_gdbarch_tdep
*tdep
1237 = gdbarch_tdep
<m68k_gdbarch_tdep
> (best_arch
->gdbarch
);
1239 if (flavour
!= tdep
->flavour
)
1242 if (has_fp
!= tdep
->fpregs_present
)
1245 if (float_return
!= tdep
->float_return
)
1251 if (best_arch
!= NULL
)
1252 return best_arch
->gdbarch
;
1255 = gdbarch_alloc (&info
, gdbarch_tdep_up (new m68k_gdbarch_tdep
));
1256 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
1258 tdep
->fpregs_present
= has_fp
;
1259 tdep
->float_return
= float_return
;
1260 tdep
->flavour
= flavour
;
1262 if (flavour
== m68k_coldfire_flavour
|| flavour
== m68k_fido_flavour
)
1263 long_double_format
= floatformats_ieee_double
;
1264 set_gdbarch_long_double_format (gdbarch
, long_double_format
);
1265 set_gdbarch_long_double_bit (gdbarch
, long_double_format
[0]->totalsize
);
1267 set_gdbarch_skip_prologue (gdbarch
, m68k_skip_prologue
);
1268 set_gdbarch_breakpoint_kind_from_pc (gdbarch
, m68k_breakpoint::kind_from_pc
);
1269 set_gdbarch_sw_breakpoint_from_kind (gdbarch
, m68k_breakpoint::bp_from_kind
);
1271 /* Stack grows down. */
1272 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
1273 set_gdbarch_frame_align (gdbarch
, m68k_frame_align
);
1275 set_gdbarch_believe_pcc_promotion (gdbarch
, 1);
1276 if (flavour
== m68k_coldfire_flavour
|| flavour
== m68k_fido_flavour
)
1277 set_gdbarch_decr_pc_after_break (gdbarch
, 2);
1279 set_gdbarch_frame_args_skip (gdbarch
, 8);
1280 set_gdbarch_dwarf2_reg_to_regnum (gdbarch
, m68k_dwarf_reg_to_regnum
);
1282 set_gdbarch_register_type (gdbarch
, m68k_register_type
);
1283 set_gdbarch_register_name (gdbarch
, m68k_register_name
);
1284 set_gdbarch_num_regs (gdbarch
, M68K_NUM_REGS
);
1285 set_gdbarch_sp_regnum (gdbarch
, M68K_SP_REGNUM
);
1286 set_gdbarch_pc_regnum (gdbarch
, M68K_PC_REGNUM
);
1287 set_gdbarch_ps_regnum (gdbarch
, M68K_PS_REGNUM
);
1288 set_gdbarch_convert_register_p (gdbarch
, m68k_convert_register_p
);
1289 set_gdbarch_register_to_value (gdbarch
, m68k_register_to_value
);
1290 set_gdbarch_value_to_register (gdbarch
, m68k_value_to_register
);
1293 set_gdbarch_fp0_regnum (gdbarch
, M68K_FP0_REGNUM
);
1295 /* Function call & return. */
1296 set_gdbarch_push_dummy_call (gdbarch
, m68k_push_dummy_call
);
1297 set_gdbarch_return_value (gdbarch
, m68k_return_value
);
1298 set_gdbarch_return_in_first_hidden_param_p (gdbarch
,
1299 m68k_return_in_first_hidden_param_p
);
1301 #if defined JB_PC && defined JB_ELEMENT_SIZE
1302 tdep
->jb_pc
= JB_PC
;
1303 tdep
->jb_elt_size
= JB_ELEMENT_SIZE
;
1307 tdep
->pointer_result_regnum
= M68K_D0_REGNUM
;
1308 tdep
->struct_value_regnum
= M68K_A1_REGNUM
;
1309 tdep
->struct_return
= reg_struct_return
;
1311 /* Frame unwinder. */
1312 set_gdbarch_dummy_id (gdbarch
, m68k_dummy_id
);
1313 set_gdbarch_unwind_pc (gdbarch
, m68k_unwind_pc
);
1315 /* Hook in the DWARF CFI frame unwinder. */
1316 dwarf2_append_unwinders (gdbarch
);
1318 frame_base_set_default (gdbarch
, &m68k_frame_base
);
1320 /* Hook in ABI-specific overrides, if they have been registered. */
1321 gdbarch_init_osabi (info
, gdbarch
);
1323 /* Now we have tuned the configuration, set a few final things,
1324 based on what the OS ABI has told us. */
1326 if (tdep
->jb_pc
>= 0)
1327 set_gdbarch_get_longjmp_target (gdbarch
, m68k_get_longjmp_target
);
1329 frame_unwind_append_unwinder (gdbarch
, &m68k_frame_unwind
);
1331 if (tdesc_data
!= nullptr)
1332 tdesc_use_registers (gdbarch
, info
.target_desc
, std::move (tdesc_data
));
1339 m68k_dump_tdep (struct gdbarch
*gdbarch
, struct ui_file
*file
)
1341 m68k_gdbarch_tdep
*tdep
= gdbarch_tdep
<m68k_gdbarch_tdep
> (gdbarch
);
1347 /* OSABI sniffer for m68k. */
1349 static enum gdb_osabi
1350 m68k_osabi_sniffer (bfd
*abfd
)
1352 unsigned int elfosabi
= elf_elfheader (abfd
)->e_ident
[EI_OSABI
];
1353 enum gdb_osabi osabi
= GDB_OSABI_UNKNOWN
;
1355 if (elfosabi
== ELFOSABI_NONE
)
1357 /* Check note sections. */
1358 for (asection
*sect
: gdb_bfd_sections (abfd
))
1359 generic_elf_osabi_sniff_abi_tag_sections (abfd
, sect
, &osabi
);
1361 if (osabi
== GDB_OSABI_UNKNOWN
)
1362 osabi
= GDB_OSABI_SVR4
;
1368 void _initialize_m68k_tdep ();
1370 _initialize_m68k_tdep ()
1372 gdbarch_register (bfd_arch_m68k
, m68k_gdbarch_init
, m68k_dump_tdep
);
1374 gdbarch_register_osabi_sniffer (bfd_arch_m68k
, bfd_target_elf_flavour
,
1375 m68k_osabi_sniffer
);
1376 gdbarch_register_osabi (bfd_arch_m68k
, 0, GDB_OSABI_SVR4
,
1377 m68k_embedded_init_abi
);