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 
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 
68 
69 /* Construct types for ISA-specific registers.  */
70 static struct type *
m68k_ps_type(struct gdbarch * gdbarch)71 m68k_ps_type (struct gdbarch *gdbarch)
72 {
73   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_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 *
m68881_ext_type(struct gdbarch * gdbarch)100 m68881_ext_type (struct gdbarch *gdbarch)
101 {
102   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
103 
104   if (!tdep->m68881_ext_type)
105     {
106       type_allocator alloc (gdbarch);
107       tdep->m68881_ext_type
108           = init_float_type (alloc, -1, "builtin_type_m68881_ext",
109                                  floatformats_m68881_ext);
110     }
111 
112   return tdep->m68881_ext_type;
113 }
114 
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
121    address.  */
122 
123 static struct type *
m68k_register_type(struct gdbarch * gdbarch,int regnum)124 m68k_register_type (struct gdbarch *gdbarch, int regnum)
125 {
126   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
127 
128   if (tdep->fpregs_present)
129     {
130       if (regnum >= gdbarch_fp0_regnum (gdbarch)
131             && regnum <= gdbarch_fp0_regnum (gdbarch) + 7)
132           {
133             if (tdep->flavour == m68k_coldfire_flavour)
134               return builtin_type (gdbarch)->builtin_double;
135             else
136               return m68881_ext_type (gdbarch);
137           }
138 
139       if (regnum == M68K_FPI_REGNUM)
140           return builtin_type (gdbarch)->builtin_func_ptr;
141 
142       if (regnum == M68K_FPC_REGNUM || regnum == M68K_FPS_REGNUM)
143           return builtin_type (gdbarch)->builtin_int32;
144     }
145   else
146     {
147       if (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FPI_REGNUM)
148           return builtin_type (gdbarch)->builtin_int0;
149     }
150 
151   if (regnum == gdbarch_pc_regnum (gdbarch))
152     return builtin_type (gdbarch)->builtin_func_ptr;
153 
154   if (regnum >= M68K_A0_REGNUM && regnum <= M68K_A0_REGNUM + 7)
155     return builtin_type (gdbarch)->builtin_data_ptr;
156 
157   if (regnum == M68K_PS_REGNUM)
158     return m68k_ps_type (gdbarch);
159 
160   return builtin_type (gdbarch)->builtin_int32;
161 }
162 
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",
166     "ps", "pc",
167     "fp0", "fp1", "fp2", "fp3", "fp4", "fp5", "fp6", "fp7",
168     "fpcontrol", "fpstatus", "fpiaddr"
169   };
170 
171 /* Function: m68k_register_name
172    Returns the name of the standard m68k register regnum.  */
173 
174 static const char *
m68k_register_name(struct gdbarch * gdbarch,int regnum)175 m68k_register_name (struct gdbarch *gdbarch, int regnum)
176 {
177   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
178 
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)
182     return "";
183   else
184     return m68k_register_names[regnum];
185 }
186 
187 /* Return nonzero if a value of type TYPE stored in register REGNUM
188    needs any special handling.  */
189 
190 static int
m68k_convert_register_p(struct gdbarch * gdbarch,int regnum,struct type * type)191 m68k_convert_register_p (struct gdbarch *gdbarch,
192                                int regnum, struct type *type)
193 {
194   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_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
m68k_register_to_value(const frame_info_ptr & frame,int regnum,struct type * type,gdb_byte * to,int * optimizedp,int * unavailablep)208 m68k_register_to_value (const frame_info_ptr &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   auto from_view
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,
223                                          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
m68k_value_to_register(const frame_info_ptr & frame,int regnum,struct type * type,const gdb_byte * from)235 m68k_value_to_register (const frame_info_ptr &frame, int regnum,
236                               struct type *type, const gdb_byte *from)
237 {
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);
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   auto to_view = gdb::make_array_view (to, fpreg_type->length ());
253   put_frame_register (get_next_frame_sentinel_okay (frame), regnum, to_view);
254 }
255 
256 
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.
266 
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'.
276 
277    GNU/Linux returns values in the same way as SVR4 does, but uses %a1
278    for passing the structure return value buffer.
279 
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.
285 
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'
289    as appropriate.  */
290 
291 /* Read a function return value of TYPE from REGCACHE, and copy that
292    into VALBUF.  */
293 
294 static void
m68k_extract_return_value(struct type * type,struct regcache * regcache,gdb_byte * valbuf)295 m68k_extract_return_value (struct type *type, struct regcache *regcache,
296                                  gdb_byte *valbuf)
297 {
298   int len = type->length ();
299   gdb_byte buf[M68K_MAX_REGISTER_SIZE];
300 
301   if (type->code () == TYPE_CODE_PTR && len == 4)
302     {
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);
306     }
307   else if (len <= 4)
308     {
309       regcache->raw_read (M68K_D0_REGNUM, buf);
310       memcpy (valbuf, buf + (4 - len), len);
311     }
312   else if (len <= 8)
313     {
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));
317     }
318   else
319     internal_error (_("Cannot extract return value of %d bytes long."), len);
320 }
321 
322 static void
m68k_svr4_extract_return_value(struct type * type,struct regcache * regcache,gdb_byte * valbuf)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 = gdbarch_tdep<m68k_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
m68k_store_return_value(struct type * type,struct regcache * regcache,const gdb_byte * valbuf)343 m68k_store_return_value (struct type *type, struct regcache *regcache,
344                                const gdb_byte *valbuf)
345 {
346   int len = type->length ();
347 
348   if (type->code () == TYPE_CODE_PTR && len == 4)
349     {
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);
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 (_("Cannot store return value of %d bytes long."), len);
366 }
367 
368 static void
m68k_svr4_store_return_value(struct type * type,struct regcache * regcache,const gdb_byte * valbuf)369 m68k_svr4_store_return_value (struct type *type, struct regcache *regcache,
370                                     const gdb_byte *valbuf)
371 {
372   struct gdbarch *gdbarch = regcache->arch ();
373   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
374 
375   if (tdep->float_return && type->code () == TYPE_CODE_FLT)
376     {
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);
381     }
382   else
383     m68k_store_return_value (type, regcache, valbuf);
384 }
385 
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
388    GDBARCH.  */
389 
390 static int
m68k_reg_struct_return_p(struct gdbarch * gdbarch,struct type * type)391 m68k_reg_struct_return_p (struct gdbarch *gdbarch, struct type *type)
392 {
393   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
394   enum type_code code = type->code ();
395   int len = type->length ();
396 
397   gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION
398                 || code == TYPE_CODE_COMPLEX || code == TYPE_CODE_ARRAY);
399 
400   if (tdep->struct_return == pcc_struct_return)
401     return 0;
402 
403   const bool is_vector = code == TYPE_CODE_ARRAY && type->is_vector ();
404 
405   if (is_vector
406       && check_typedef (type->target_type ())->code () == TYPE_CODE_FLT)
407     return 0;
408 
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.
414 
415      We reject vectors here, as experimentally this gives the correct
416      answer.  */
417   if (!is_vector && (len == 2 || len == 4 || len == 8))
418     return type_align (type) >= 2;
419 
420   return (len == 1 || len == 2 || len == 4 || len == 8);
421 }
422 
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.  */
428 
429 static enum return_value_convention
m68k_return_value(struct gdbarch * gdbarch,struct value * function,struct type * type,struct regcache * regcache,gdb_byte * readbuf,const gdb_byte * writebuf)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)
433 {
434   enum type_code code = type->code ();
435 
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))
441     {
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.  */
445 
446       if (readbuf)
447           {
448             ULONGEST addr;
449 
450             regcache_raw_read_unsigned (regcache, M68K_D0_REGNUM, &addr);
451             read_memory (addr, readbuf, type->length ());
452           }
453 
454       return RETURN_VALUE_ABI_RETURNS_ADDRESS;
455     }
456 
457   if (readbuf)
458     m68k_extract_return_value (type, regcache, readbuf);
459   if (writebuf)
460     m68k_store_return_value (type, regcache, writebuf);
461 
462   return RETURN_VALUE_REGISTER_CONVENTION;
463 }
464 
465 static enum return_value_convention
m68k_svr4_return_value(struct gdbarch * gdbarch,struct value * function,struct type * type,struct regcache * regcache,gdb_byte * readbuf,const gdb_byte * writebuf)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)
469 {
470   enum type_code code = type->code ();
471   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
472 
473   /* Aggregates with a single member are always returned like their
474      sole element.  */
475   if ((code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION)
476       && type->num_fields () == 1)
477     {
478       type = check_typedef (type->field (0).type ());
479       return m68k_svr4_return_value (gdbarch, function, type, regcache,
480                                              readbuf, writebuf);
481     }
482 
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))
490     {
491       /* The System V ABI says that:
492 
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
496            register %a0."
497 
498            So the ABI guarantees that we can always find the return
499            value just after the function has returned.
500 
501            However, GCC also implements the "embedded" ABI.  That ABI
502            does not preserve %a0 across calls, but does write the value
503            back to %d0.  */
504 
505       if (readbuf)
506           {
507             ULONGEST addr;
508 
509             regcache_raw_read_unsigned (regcache, tdep->pointer_result_regnum,
510                                               &addr);
511             read_memory (addr, readbuf, type->length ());
512           }
513 
514       return RETURN_VALUE_ABI_RETURNS_ADDRESS;
515     }
516 
517   if (readbuf)
518     m68k_svr4_extract_return_value (type, regcache, readbuf);
519   if (writebuf)
520     m68k_svr4_store_return_value (type, regcache, writebuf);
521 
522   return RETURN_VALUE_REGISTER_CONVENTION;
523 }
524 
525 
526 /* Always align the frame to a 4-byte boundary.  This is required on
527    coldfire and harmless on the rest.  */
528 
529 static CORE_ADDR
m68k_frame_align(struct gdbarch * gdbarch,CORE_ADDR sp)530 m68k_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
531 {
532   /* Align the stack to four bytes.  */
533   return sp & ~3;
534 }
535 
536 static CORE_ADDR
m68k_push_dummy_call(struct gdbarch * gdbarch,struct value * function,struct regcache * regcache,CORE_ADDR bp_addr,int nargs,struct value ** args,CORE_ADDR sp,function_call_return_method return_method,CORE_ADDR struct_addr)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)
542 {
543   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
544   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
545   gdb_byte buf[4];
546   int i;
547 
548   /* Push arguments in reverse order.  */
549   for (i = nargs - 1; i >= 0; i--)
550     {
551       struct type *value_type = args[i]->enclosing_type ();
552       int len = value_type->length ();
553       int container_len = (len + 3) & ~3;
554       int offset;
555 
556       /* Non-scalars bigger than 4 bytes are left aligned, others are
557            right aligned.  */
558       if ((value_type->code () == TYPE_CODE_STRUCT
559              || value_type->code () == TYPE_CODE_UNION
560              || value_type->code () == TYPE_CODE_ARRAY)
561             && len > 4)
562           offset = 0;
563       else
564           offset = container_len - len;
565       sp -= container_len;
566       write_memory (sp + offset, args[i]->contents_all ().data (), len);
567     }
568 
569   /* Store struct value address.  */
570   if (return_method == return_method_struct)
571     {
572       store_unsigned_integer (buf, 4, byte_order, struct_addr);
573       regcache->cooked_write (tdep->struct_value_regnum, buf);
574     }
575 
576   /* Store return address.  */
577   sp -= 4;
578   store_unsigned_integer (buf, 4, byte_order, bp_addr);
579   write_memory (sp, buf, 4);
580 
581   /* Finally, update the stack pointer...  */
582   store_unsigned_integer (buf, 4, byte_order, sp);
583   regcache->cooked_write (M68K_SP_REGNUM, buf);
584 
585   /* ...and fake a frame pointer.  */
586   regcache->cooked_write (M68K_FP_REGNUM, buf);
587 
588   /* DWARF2/GCC uses the stack address *before* the function call as a
589      frame's CFA.  */
590   return sp + 8;
591 }
592 
593 /* Convert a dwarf or dwarf2 regnumber to a GDB regnum.  */
594 
595 static int
m68k_dwarf_reg_to_regnum(struct gdbarch * gdbarch,int num)596 m68k_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int num)
597 {
598   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
599 
600   if (num < 8)
601     /* d0..7 */
602     return (num - 0) + M68K_D0_REGNUM;
603   else if (num < 16)
604     /* a0..7 */
605     return (num - 8) + M68K_A0_REGNUM;
606   else if (num < 24 && tdep->fpregs_present)
607     /* fp0..7 */
608     return (num - 16) + M68K_FP0_REGNUM;
609   else if (num == 25)
610     /* pc */
611     return M68K_PC_REGNUM;
612   else
613     return -1;
614 }
615 
616 
617 struct m68k_frame_cache
618 {
619   /* Base address.  */
620   CORE_ADDR base;
621   CORE_ADDR sp_offset;
622   CORE_ADDR pc;
623 
624   /* Saved registers.  */
625   CORE_ADDR saved_regs[M68K_NUM_REGS];
626   CORE_ADDR saved_sp;
627 
628   /* Stack space reserved for local variables.  */
629   long locals;
630 };
631 
632 /* Allocate and initialize a frame cache.  */
633 
634 static struct m68k_frame_cache *
m68k_alloc_frame_cache(void)635 m68k_alloc_frame_cache (void)
636 {
637   struct m68k_frame_cache *cache;
638   int i;
639 
640   cache = FRAME_OBSTACK_ZALLOC (struct m68k_frame_cache);
641 
642   /* Base address.  */
643   cache->base = 0;
644   cache->sp_offset = -4;
645   cache->pc = 0;
646 
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;
651 
652   /* Frameless until proven otherwise.  */
653   cache->locals = -1;
654 
655   return cache;
656 }
657 
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.  */
663 
664 static CORE_ADDR
m68k_analyze_frame_setup(struct gdbarch * gdbarch,CORE_ADDR pc,CORE_ADDR current_pc,struct m68k_frame_cache * cache)665 m68k_analyze_frame_setup (struct gdbarch *gdbarch,
666                                 CORE_ADDR pc, CORE_ADDR current_pc,
667                                 struct m68k_frame_cache *cache)
668 {
669   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
670   int op;
671 
672   if (pc >= current_pc)
673     return current_pc;
674 
675   op = read_memory_unsigned_integer (pc, 2, byte_order);
676 
677   if (op == P_LINKW_FP || op == P_LINKL_FP || op == P_PEA_FP)
678     {
679       cache->saved_regs[M68K_FP_REGNUM] = 0;
680       cache->sp_offset += 4;
681       if (op == P_LINKW_FP)
682           {
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);
686 
687             if (pc + 4 < current_pc && cache->locals == 0)
688               {
689                 op = read_memory_unsigned_integer (pc + 4, 2, byte_order);
690                 if (op == P_ADDAL_SP)
691                     {
692                       cache->locals = read_memory_integer (pc + 6, 4, byte_order);
693                       return pc + 10;
694                     }
695               }
696 
697             return pc + 4;
698           }
699       else if (op == P_LINKL_FP)
700           {
701             /* link.l %fp, #-N */
702             cache->locals = -read_memory_integer (pc + 2, 4, byte_order);
703             return pc + 6;
704           }
705       else
706           {
707             /* pea (%fp); movea.l %sp, %fp */
708             cache->locals = 0;
709 
710             if (pc + 2 < current_pc)
711               {
712                 op = read_memory_unsigned_integer (pc + 2, 2, byte_order);
713 
714                 if (op == P_MOVEAL_SP_FP)
715                     {
716                       /* move.l %sp, %fp */
717                       return pc + 4;
718                     }
719               }
720 
721             return pc + 2;
722           }
723     }
724   else if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
725     {
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)
730           {
731             op = read_memory_unsigned_integer (pc + 2, 2, byte_order);
732             if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
733               {
734                 cache->locals += (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
735                 return pc + 4;
736               }
737           }
738       return pc + 2;
739     }
740   else if (op == P_ADDAW_SP || op == P_LEA_SP_SP)
741     {
742       /* adda.w #-N,%sp */
743       /* lea (-N,%sp),%sp */
744       cache->locals = -read_memory_integer (pc + 2, 2, byte_order);
745       return pc + 4;
746     }
747   else if (op == P_ADDAL_SP)
748     {
749       /* adda.l #-N,%sp */
750       cache->locals = -read_memory_integer (pc + 2, 4, byte_order);
751       return pc + 6;
752     }
753 
754   return pc;
755 }
756 
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.  */
761 
762 static CORE_ADDR
m68k_analyze_register_saves(struct gdbarch * gdbarch,CORE_ADDR pc,CORE_ADDR current_pc,struct m68k_frame_cache * cache)763 m68k_analyze_register_saves (struct gdbarch *gdbarch, CORE_ADDR pc,
764                                    CORE_ADDR current_pc,
765                                    struct m68k_frame_cache *cache)
766 {
767   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
768   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
769 
770   if (cache->locals >= 0)
771     {
772       CORE_ADDR offset;
773       int op;
774       int i, mask, regno;
775 
776       offset = -4 - cache->locals;
777       while (pc < current_pc)
778           {
779             op = read_memory_unsigned_integer (pc, 2, byte_order);
780             if (op == P_FMOVEMX_SP
781                 && tdep->fpregs_present)
782               {
783                 /* fmovem.x REGS,-(%sp) */
784                 op = read_memory_unsigned_integer (pc + 2, 2, byte_order);
785                 if ((op & 0xff00) == 0xe000)
786                     {
787                       mask = op & 0xff;
788                       for (i = 0; i < 16; i++, mask >>= 1)
789                         {
790                           if (mask & 1)
791                               {
792                                 cache->saved_regs[i + M68K_FP0_REGNUM] = offset;
793                                 offset -= 12;
794                               }
795                         }
796                       pc += 4;
797                     }
798                 else
799                     break;
800               }
801             else if ((op & 0177760) == P_MOVEL_SP)
802               {
803                 /* move.l %R,-(%sp) */
804                 regno = op & 017;
805                 cache->saved_regs[regno] = offset;
806                 offset -= 4;
807                 pc += 2;
808               }
809             else if (op == P_MOVEML_SP)
810               {
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)
814                     {
815                       if (mask & 1)
816                         {
817                           cache->saved_regs[15 - i] = offset;
818                           offset -= 4;
819                         }
820                     }
821                 pc += 4;
822               }
823             else
824               break;
825           }
826     }
827 
828   return pc;
829 }
830 
831 
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.
835 
836    We handle all cases that can be generated by gcc.
837 
838    For allocating a stack frame:
839 
840    link.w %a6,#-N
841    link.l %a6,#-N
842    pea (%fp); move.l %sp,%fp
843    link.w %a6,#0; add.l #-N,%sp
844    subq.l #N,%sp
845    subq.w #N,%sp
846    subq.w #8,%sp; subq.w #N-8,%sp
847    add.w #-N,%sp
848    lea (-N,%sp),%sp
849    add.l #-N,%sp
850 
851    For saving registers:
852 
853    fmovem.x REGS,-(%sp)
854    move.l R1,-(%sp)
855    move.l R1,-(%sp); move.l R2,-(%sp)
856    movem.l REGS,-(%sp)
857 
858    For setting up the PIC register:
859 
860    lea (%pc,N),%a5
861 
862    */
863 
864 static CORE_ADDR
m68k_analyze_prologue(struct gdbarch * gdbarch,CORE_ADDR pc,CORE_ADDR current_pc,struct m68k_frame_cache * cache)865 m68k_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
866                            CORE_ADDR current_pc, struct m68k_frame_cache *cache)
867 {
868   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
869   unsigned int op;
870 
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)
874     return current_pc;
875 
876   /* Check for GOT setup.  */
877   op = read_memory_unsigned_integer (pc, 4, byte_order);
878   if (op == P_LEA_PC_A5)
879     {
880       /* lea (%pc,N),%a5 */
881       return pc + 8;
882     }
883 
884   return pc;
885 }
886 
887 /* Return PC of first real instruction.  */
888 
889 static CORE_ADDR
m68k_skip_prologue(struct gdbarch * gdbarch,CORE_ADDR start_pc)890 m68k_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
891 {
892   struct m68k_frame_cache cache;
893   CORE_ADDR pc;
894 
895   cache.locals = -1;
896   pc = m68k_analyze_prologue (gdbarch, start_pc, (CORE_ADDR) -1, &cache);
897   if (cache.locals < 0)
898     return start_pc;
899   return pc;
900 }
901 
902 static CORE_ADDR
m68k_unwind_pc(struct gdbarch * gdbarch,const frame_info_ptr & next_frame)903 m68k_unwind_pc (struct gdbarch *gdbarch, const frame_info_ptr &next_frame)
904 {
905   gdb_byte buf[8];
906 
907   frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
908   return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
909 }
910 
911 /* Normal frames.  */
912 
913 static struct m68k_frame_cache *
m68k_frame_cache(const frame_info_ptr & this_frame,void ** this_cache)914 m68k_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
915 {
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;
919   gdb_byte buf[4];
920   int i;
921 
922   if (*this_cache)
923     return (struct m68k_frame_cache *) *this_cache;
924 
925   cache = m68k_alloc_frame_cache ();
926   *this_cache = cache;
927 
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.  */
936 
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)
940     return cache;
941 
942   /* For normal frames, %pc is stored at 4(%fp).  */
943   cache->saved_regs[M68K_PC_REGNUM] = 4;
944 
945   cache->pc = get_frame_func (this_frame);
946   if (cache->pc != 0)
947     m68k_analyze_prologue (get_frame_arch (this_frame), cache->pc,
948                                  get_frame_pc (this_frame), cache);
949 
950   if (cache->locals < 0)
951     {
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.  */
959 
960       get_frame_register (this_frame, M68K_SP_REGNUM, buf);
961       cache->base = extract_unsigned_integer (buf, 4, byte_order)
962                         + cache->sp_offset;
963     }
964 
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;
968 
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;
974 
975   return cache;
976 }
977 
978 static void
m68k_frame_this_id(const frame_info_ptr & this_frame,void ** this_cache,struct frame_id * this_id)979 m68k_frame_this_id (const frame_info_ptr &this_frame, void **this_cache,
980                         struct frame_id *this_id)
981 {
982   struct m68k_frame_cache *cache = m68k_frame_cache (this_frame, this_cache);
983 
984   /* This marks the outermost frame.  */
985   if (cache->base == 0)
986     return;
987 
988   /* See the end of m68k_push_dummy_call.  */
989   *this_id = frame_id_build (cache->base + 8, cache->pc);
990 }
991 
992 static struct value *
m68k_frame_prev_register(const frame_info_ptr & this_frame,void ** this_cache,int regnum)993 m68k_frame_prev_register (const frame_info_ptr &this_frame, void **this_cache,
994                                 int regnum)
995 {
996   struct m68k_frame_cache *cache = m68k_frame_cache (this_frame, this_cache);
997 
998   gdb_assert (regnum >= 0);
999 
1000   if (regnum == M68K_SP_REGNUM && cache->saved_sp)
1001     return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
1002 
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]);
1006 
1007   return frame_unwind_got_register (this_frame, regnum, regnum);
1008 }
1009 
1010 static const struct frame_unwind m68k_frame_unwind =
1011 {
1012   "m68k prologue",
1013   NORMAL_FRAME,
1014   default_frame_unwind_stop_reason,
1015   m68k_frame_this_id,
1016   m68k_frame_prev_register,
1017   NULL,
1018   default_frame_sniffer
1019 };
1020 
1021 static CORE_ADDR
m68k_frame_base_address(const frame_info_ptr & this_frame,void ** this_cache)1022 m68k_frame_base_address (const frame_info_ptr &this_frame, void **this_cache)
1023 {
1024   struct m68k_frame_cache *cache = m68k_frame_cache (this_frame, this_cache);
1025 
1026   return cache->base;
1027 }
1028 
1029 static const struct frame_base m68k_frame_base =
1030 {
1031   &m68k_frame_unwind,
1032   m68k_frame_base_address,
1033   m68k_frame_base_address,
1034   m68k_frame_base_address
1035 };
1036 
1037 static struct frame_id
m68k_dummy_id(struct gdbarch * gdbarch,const frame_info_ptr & this_frame)1038 m68k_dummy_id (struct gdbarch *gdbarch, const frame_info_ptr &this_frame)
1039 {
1040   CORE_ADDR fp;
1041 
1042   fp = get_frame_register_unsigned (this_frame, M68K_FP_REGNUM);
1043 
1044   /* See the end of m68k_push_dummy_call.  */
1045   return frame_id_build (fp + 8, get_frame_pc (this_frame));
1046 }
1047 
1048 
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.  */
1053 
1054 static int
m68k_get_longjmp_target(const frame_info_ptr & frame,CORE_ADDR * pc)1055 m68k_get_longjmp_target (const frame_info_ptr &frame, CORE_ADDR *pc)
1056 {
1057   gdb_byte *buf;
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);
1062 
1063   if (tdep->jb_pc < 0)
1064     {
1065       internal_error (_("m68k_get_longjmp_target: not implemented"));
1066       return 0;
1067     }
1068 
1069   buf = (gdb_byte *) alloca (gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT);
1070   sp = get_frame_register_unsigned (frame, gdbarch_sp_regnum (gdbarch));
1071 
1072   if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack.  */
1073                                 buf, gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT))
1074     return 0;
1075 
1076   jb_addr = extract_unsigned_integer (buf, gdbarch_ptr_bit (gdbarch)
1077                                                        / TARGET_CHAR_BIT, byte_order);
1078 
1079   if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
1080                                 gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT),
1081                                 byte_order)
1082     return 0;
1083 
1084   *pc = extract_unsigned_integer (buf, gdbarch_ptr_bit (gdbarch)
1085                                                    / TARGET_CHAR_BIT, byte_order);
1086   return 1;
1087 }
1088 
1089 
1090 /* This is the implementation of gdbarch method
1091    return_in_first_hidden_param_p.  */
1092 
1093 static int
m68k_return_in_first_hidden_param_p(struct gdbarch * gdbarch,struct type * type)1094 m68k_return_in_first_hidden_param_p (struct gdbarch *gdbarch,
1095                                              struct type *type)
1096 {
1097   return 0;
1098 }
1099 
1100 /* System V Release 4 (SVR4).  */
1101 
1102 void
m68k_svr4_init_abi(struct gdbarch_info info,struct gdbarch * gdbarch)1103 m68k_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1104 {
1105   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
1106 
1107   /* SVR4 uses a different calling convention.  */
1108   set_gdbarch_return_value (gdbarch, m68k_svr4_return_value);
1109 
1110   /* SVR4 uses %a0 instead of %a1.  */
1111   tdep->struct_value_regnum = M68K_A0_REGNUM;
1112 
1113   /* SVR4 returns pointers in %a0.  */
1114   tdep->pointer_result_regnum = M68K_A0_REGNUM;
1115 }
1116 
1117 /* GCC's m68k "embedded" ABI.  This is like the SVR4 ABI, but pointer
1118    values are returned in %d0, not %a0.  */
1119 
1120 static void
m68k_embedded_init_abi(struct gdbarch_info info,struct gdbarch * gdbarch)1121 m68k_embedded_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1122 {
1123   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
1124 
1125   m68k_svr4_init_abi (info, gdbarch);
1126   tdep->pointer_result_regnum = M68K_D0_REGNUM;
1127 }
1128 
1129 
1130 
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.  */
1134 
1135 static struct gdbarch *
m68k_gdbarch_init(struct gdbarch_info info,struct gdbarch_list * arches)1136 m68k_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1137 {
1138   struct gdbarch_list *best_arch;
1139   tdesc_arch_data_up tdesc_data;
1140   int i;
1141   enum m68k_flavour flavour = m68k_no_flavour;
1142   int has_fp = 1;
1143   const struct floatformat **long_double_format = floatformats_m68881_ext;
1144 
1145   /* Check any target description for validity.  */
1146   if (tdesc_has_registers (info.target_desc))
1147     {
1148       const struct tdesc_feature *feature;
1149       int valid_p;
1150 
1151       feature = tdesc_find_feature (info.target_desc,
1152                                             "org.gnu.gdb.m68k.core");
1153 
1154       if (feature == NULL)
1155           {
1156             feature = tdesc_find_feature (info.target_desc,
1157                                                   "org.gnu.gdb.coldfire.core");
1158             if (feature != NULL)
1159               flavour = m68k_coldfire_flavour;
1160           }
1161 
1162       if (feature == NULL)
1163           {
1164             feature = tdesc_find_feature (info.target_desc,
1165                                                   "org.gnu.gdb.fido.core");
1166             if (feature != NULL)
1167               flavour = m68k_fido_flavour;
1168           }
1169 
1170       if (feature == NULL)
1171           return NULL;
1172 
1173       tdesc_data = tdesc_data_alloc ();
1174 
1175       valid_p = 1;
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]);
1179 
1180       if (!valid_p)
1181           return NULL;
1182 
1183       feature = tdesc_find_feature (info.target_desc,
1184                                             "org.gnu.gdb.coldfire.fp");
1185       if (feature != NULL)
1186           {
1187             valid_p = 1;
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]);
1191             if (!valid_p)
1192               return NULL;
1193           }
1194       else
1195           has_fp = 0;
1196     }
1197 
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.  */
1201 
1202   if (info.bfd_arch_info && info.bfd_arch_info->mach != 0)
1203     {
1204       const bfd_arch_info_type *coldfire_arch =
1205           bfd_lookup_arch (bfd_arch_m68k, bfd_mach_mcf_isa_a_nodiv);
1206 
1207       if (coldfire_arch
1208             && ((*info.bfd_arch_info->compatible)
1209                 (info.bfd_arch_info, coldfire_arch)))
1210           flavour = m68k_coldfire_flavour;
1211     }
1212 
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)
1218     float_return = 1;
1219 #ifdef HAVE_ELF
1220   if (info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
1221     {
1222       int fp_abi = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_GNU,
1223                                                        Tag_GNU_M68K_ABI_FP);
1224       if (fp_abi == 1)
1225           float_return = 1;
1226       else if (fp_abi == 2)
1227           float_return = 0;
1228     }
1229 #endif /* HAVE_ELF */
1230 
1231   /* If there is already a candidate, use it.  */
1232   for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
1233        best_arch != NULL;
1234        best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
1235     {
1236       m68k_gdbarch_tdep *tdep
1237           = gdbarch_tdep<m68k_gdbarch_tdep> (best_arch->gdbarch);
1238 
1239       if (flavour != tdep->flavour)
1240           continue;
1241 
1242       if (has_fp != tdep->fpregs_present)
1243           continue;
1244 
1245       if (float_return != tdep->float_return)
1246           continue;
1247 
1248       break;
1249     }
1250 
1251   if (best_arch != NULL)
1252     return best_arch->gdbarch;
1253 
1254   gdbarch *gdbarch
1255     = gdbarch_alloc (&info, gdbarch_tdep_up (new m68k_gdbarch_tdep));
1256   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
1257 
1258   tdep->fpregs_present = has_fp;
1259   tdep->float_return = float_return;
1260   tdep->flavour = flavour;
1261 
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);
1266 
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);
1270 
1271   /* Stack grows down.  */
1272   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1273   set_gdbarch_frame_align (gdbarch, m68k_frame_align);
1274 
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);
1278 
1279   set_gdbarch_frame_args_skip (gdbarch, 8);
1280   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, m68k_dwarf_reg_to_regnum);
1281 
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);
1291 
1292   if (has_fp)
1293     set_gdbarch_fp0_regnum (gdbarch, M68K_FP0_REGNUM);
1294 
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);
1300 
1301 #if defined JB_PC && defined JB_ELEMENT_SIZE
1302   tdep->jb_pc = JB_PC;
1303   tdep->jb_elt_size = JB_ELEMENT_SIZE;
1304 #else
1305   tdep->jb_pc = -1;
1306 #endif
1307   tdep->pointer_result_regnum = M68K_D0_REGNUM;
1308   tdep->struct_value_regnum = M68K_A1_REGNUM;
1309   tdep->struct_return = reg_struct_return;
1310 
1311   /* Frame unwinder.  */
1312   set_gdbarch_dummy_id (gdbarch, m68k_dummy_id);
1313   set_gdbarch_unwind_pc (gdbarch, m68k_unwind_pc);
1314 
1315   /* Hook in the DWARF CFI frame unwinder.  */
1316   dwarf2_append_unwinders (gdbarch);
1317 
1318   frame_base_set_default (gdbarch, &m68k_frame_base);
1319 
1320   /* Hook in ABI-specific overrides, if they have been registered.  */
1321   gdbarch_init_osabi (info, gdbarch);
1322 
1323   /* Now we have tuned the configuration, set a few final things,
1324      based on what the OS ABI has told us.  */
1325 
1326   if (tdep->jb_pc >= 0)
1327     set_gdbarch_get_longjmp_target (gdbarch, m68k_get_longjmp_target);
1328 
1329   frame_unwind_append_unwinder (gdbarch, &m68k_frame_unwind);
1330 
1331   if (tdesc_data != nullptr)
1332     tdesc_use_registers (gdbarch, info.target_desc, std::move (tdesc_data));
1333 
1334   return gdbarch;
1335 }
1336 
1337 
1338 static void
m68k_dump_tdep(struct gdbarch * gdbarch,struct ui_file * file)1339 m68k_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
1340 {
1341   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
1342 
1343   if (tdep == NULL)
1344     return;
1345 }
1346 
1347 /* OSABI sniffer for m68k.  */
1348 
1349 static enum gdb_osabi
m68k_osabi_sniffer(bfd * abfd)1350 m68k_osabi_sniffer (bfd *abfd)
1351 {
1352   /* XXX NetBSD uses ELFOSABI_NONE == ELFOSABI_SYSV. Therefore, do not
1353      fall back to EABI here.  */
1354 #ifndef __NetBSD__
1355   unsigned int elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
1356 
1357   if (elfosabi == ELFOSABI_NONE)
1358     return GDB_OSABI_SVR4;
1359 #endif
1360 
1361   return GDB_OSABI_UNKNOWN;
1362 }
1363 
1364 void _initialize_m68k_tdep ();
1365 void
_initialize_m68k_tdep()1366 _initialize_m68k_tdep ()
1367 {
1368   gdbarch_register (bfd_arch_m68k, m68k_gdbarch_init, m68k_dump_tdep);
1369 
1370   gdbarch_register_osabi_sniffer (bfd_arch_m68k, bfd_target_elf_flavour,
1371                                           m68k_osabi_sniffer);
1372   gdbarch_register_osabi (bfd_arch_m68k, 0, GDB_OSABI_SVR4,
1373                                 m68k_embedded_init_abi);
1374 }
1375