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