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