Home | History | Annotate | Line # | Download | only in gdb
m68k-tdep.c revision 1.1.1.7
      1 /* Target-dependent code for the Motorola 68000 series.
      2 
      3    Copyright (C) 1990-2023 Free Software Foundation, Inc.
      4 
      5    This file is part of GDB.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19 
     20 #include "defs.h"
     21 #include "dwarf2/frame.h"
     22 #include "frame.h"
     23 #include "frame-base.h"
     24 #include "frame-unwind.h"
     25 #include "gdbtypes.h"
     26 #include "symtab.h"
     27 #include "gdbcore.h"
     28 #include "value.h"
     29 #include "inferior.h"
     30 #include "regcache.h"
     31 #include "arch-utils.h"
     32 #include "osabi.h"
     33 #include "dis-asm.h"
     34 #include "target-descriptions.h"
     35 #include "floatformat.h"
     36 #include "target-float.h"
     37 #include "elf-bfd.h"
     38 #include "elf/m68k.h"
     39 
     40 #include "m68k-tdep.h"
     41 
     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     tdep->m68881_ext_type
    108       = arch_float_type (gdbarch, -1, "builtin_type_m68881_ext",
    109 			 floatformats_m68881_ext);
    110 
    111   return tdep->m68881_ext_type;
    112 }
    113 
    114 /* Return the GDB type object for the "standard" data type of data in
    115    register N.  This should be int for D0-D7, SR, FPCONTROL and
    116    FPSTATUS, long double for FP0-FP7, and void pointer for all others
    117    (A0-A7, PC, FPIADDR).  Note, for registers which contain
    118    addresses return pointer to void, not pointer to char, because we
    119    don't want to attempt to print the string after printing the
    120    address.  */
    121 
    122 static struct type *
    123 m68k_register_type (struct gdbarch *gdbarch, int regnum)
    124 {
    125   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
    126 
    127   if (tdep->fpregs_present)
    128     {
    129       if (regnum >= gdbarch_fp0_regnum (gdbarch)
    130 	  && regnum <= gdbarch_fp0_regnum (gdbarch) + 7)
    131 	{
    132 	  if (tdep->flavour == m68k_coldfire_flavour)
    133 	    return builtin_type (gdbarch)->builtin_double;
    134 	  else
    135 	    return m68881_ext_type (gdbarch);
    136 	}
    137 
    138       if (regnum == M68K_FPI_REGNUM)
    139 	return builtin_type (gdbarch)->builtin_func_ptr;
    140 
    141       if (regnum == M68K_FPC_REGNUM || regnum == M68K_FPS_REGNUM)
    142 	return builtin_type (gdbarch)->builtin_int32;
    143     }
    144   else
    145     {
    146       if (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FPI_REGNUM)
    147 	return builtin_type (gdbarch)->builtin_int0;
    148     }
    149 
    150   if (regnum == gdbarch_pc_regnum (gdbarch))
    151     return builtin_type (gdbarch)->builtin_func_ptr;
    152 
    153   if (regnum >= M68K_A0_REGNUM && regnum <= M68K_A0_REGNUM + 7)
    154     return builtin_type (gdbarch)->builtin_data_ptr;
    155 
    156   if (regnum == M68K_PS_REGNUM)
    157     return m68k_ps_type (gdbarch);
    158 
    159   return builtin_type (gdbarch)->builtin_int32;
    160 }
    161 
    162 static const char * const m68k_register_names[] = {
    163     "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
    164     "a0", "a1", "a2", "a3", "a4", "a5", "fp", "sp",
    165     "ps", "pc",
    166     "fp0", "fp1", "fp2", "fp3", "fp4", "fp5", "fp6", "fp7",
    167     "fpcontrol", "fpstatus", "fpiaddr"
    168   };
    169 
    170 /* Function: m68k_register_name
    171    Returns the name of the standard m68k register regnum.  */
    172 
    173 static const char *
    174 m68k_register_name (struct gdbarch *gdbarch, int regnum)
    175 {
    176   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
    177 
    178   gdb_static_assert (ARRAY_SIZE (m68k_register_names) == M68K_NUM_REGS);
    179   if (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FPI_REGNUM
    180       && tdep->fpregs_present == 0)
    181     return "";
    182   else
    183     return m68k_register_names[regnum];
    184 }
    185 
    186 /* Return nonzero if a value of type TYPE stored in register REGNUM
    188    needs any special handling.  */
    189 
    190 static int
    191 m68k_convert_register_p (struct gdbarch *gdbarch,
    192 			 int regnum, struct type *type)
    193 {
    194   m68k_gdbarch_tdep *tdep = 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
    208 m68k_register_to_value (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   if (!get_frame_register_bytes (frame, regnum, 0,
    220 				 gdb::make_array_view (from,
    221 						       register_size (gdbarch,
    222 								      regnum)),
    223 				 optimizedp, unavailablep))
    224     return 0;
    225 
    226   target_float_convert (from, fpreg_type, to, type);
    227   *optimizedp = *unavailablep = 0;
    228   return 1;
    229 }
    230 
    231 /* Write the contents FROM of a value of type TYPE into register
    232    REGNUM in frame FRAME.  */
    233 
    234 static void
    235 m68k_value_to_register (frame_info_ptr frame, int regnum,
    236 			struct type *type, const gdb_byte *from)
    237 {
    238   gdb_byte to[M68K_MAX_REGISTER_SIZE];
    239   struct type *fpreg_type = register_type (get_frame_arch (frame),
    240 					   M68K_FP0_REGNUM);
    241 
    242   /* We only support floating-point values.  */
    243   if (type->code () != TYPE_CODE_FLT)
    244     {
    245       warning (_("Cannot convert non-floating-point type "
    246 	       "to floating-point register value."));
    247       return;
    248     }
    249 
    250   /* Convert from TYPE.  */
    251   target_float_convert (from, type, to, fpreg_type);
    252   put_frame_register (frame, regnum, to);
    253 }
    254 
    255 
    256 /* 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
    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
    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
    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
    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
    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
    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
    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 
    527 /* Always align the frame to a 4-byte boundary.  This is required on
    528    coldfire and harmless on the rest.  */
    529 
    530 static CORE_ADDR
    531 m68k_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
    532 {
    533   /* Align the stack to four bytes.  */
    534   return sp & ~3;
    535 }
    536 
    537 static CORE_ADDR
    538 m68k_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
    539 		      struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
    540 		      struct value **args, CORE_ADDR sp,
    541 		      function_call_return_method return_method,
    542 		      CORE_ADDR struct_addr)
    543 {
    544   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
    545   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    546   gdb_byte buf[4];
    547   int i;
    548 
    549   /* Push arguments in reverse order.  */
    550   for (i = nargs - 1; i >= 0; i--)
    551     {
    552       struct type *value_type = value_enclosing_type (args[i]);
    553       int len = value_type->length ();
    554       int container_len = (len + 3) & ~3;
    555       int offset;
    556 
    557       /* Non-scalars bigger than 4 bytes are left aligned, others are
    558 	 right aligned.  */
    559       if ((value_type->code () == TYPE_CODE_STRUCT
    560 	   || value_type->code () == TYPE_CODE_UNION
    561 	   || value_type->code () == TYPE_CODE_ARRAY)
    562 	  && len > 4)
    563 	offset = 0;
    564       else
    565 	offset = container_len - len;
    566       sp -= container_len;
    567       write_memory (sp + offset, value_contents_all (args[i]).data (), len);
    568     }
    569 
    570   /* Store struct value address.  */
    571   if (return_method == return_method_struct)
    572     {
    573       store_unsigned_integer (buf, 4, byte_order, struct_addr);
    574       regcache->cooked_write (tdep->struct_value_regnum, buf);
    575     }
    576 
    577   /* Store return address.  */
    578   sp -= 4;
    579   store_unsigned_integer (buf, 4, byte_order, bp_addr);
    580   write_memory (sp, buf, 4);
    581 
    582   /* Finally, update the stack pointer...  */
    583   store_unsigned_integer (buf, 4, byte_order, sp);
    584   regcache->cooked_write (M68K_SP_REGNUM, buf);
    585 
    586   /* ...and fake a frame pointer.  */
    587   regcache->cooked_write (M68K_FP_REGNUM, buf);
    588 
    589   /* DWARF2/GCC uses the stack address *before* the function call as a
    590      frame's CFA.  */
    591   return sp + 8;
    592 }
    593 
    594 /* Convert a dwarf or dwarf2 regnumber to a GDB regnum.  */
    595 
    596 static int
    597 m68k_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int num)
    598 {
    599   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
    600 
    601   if (num < 8)
    602     /* d0..7 */
    603     return (num - 0) + M68K_D0_REGNUM;
    604   else if (num < 16)
    605     /* a0..7 */
    606     return (num - 8) + M68K_A0_REGNUM;
    607   else if (num < 24 && tdep->fpregs_present)
    608     /* fp0..7 */
    609     return (num - 16) + M68K_FP0_REGNUM;
    610   else if (num == 25)
    611     /* pc */
    612     return M68K_PC_REGNUM;
    613   else
    614     return -1;
    615 }
    616 
    617 
    618 struct m68k_frame_cache
    620 {
    621   /* Base address.  */
    622   CORE_ADDR base;
    623   CORE_ADDR sp_offset;
    624   CORE_ADDR pc;
    625 
    626   /* Saved registers.  */
    627   CORE_ADDR saved_regs[M68K_NUM_REGS];
    628   CORE_ADDR saved_sp;
    629 
    630   /* Stack space reserved for local variables.  */
    631   long locals;
    632 };
    633 
    634 /* Allocate and initialize a frame cache.  */
    635 
    636 static struct m68k_frame_cache *
    637 m68k_alloc_frame_cache (void)
    638 {
    639   struct m68k_frame_cache *cache;
    640   int i;
    641 
    642   cache = FRAME_OBSTACK_ZALLOC (struct m68k_frame_cache);
    643 
    644   /* Base address.  */
    645   cache->base = 0;
    646   cache->sp_offset = -4;
    647   cache->pc = 0;
    648 
    649   /* Saved registers.  We initialize these to -1 since zero is a valid
    650      offset (that's where %fp is supposed to be stored).  */
    651   for (i = 0; i < M68K_NUM_REGS; i++)
    652     cache->saved_regs[i] = -1;
    653 
    654   /* Frameless until proven otherwise.  */
    655   cache->locals = -1;
    656 
    657   return cache;
    658 }
    659 
    660 /* Check whether PC points at a code that sets up a new stack frame.
    661    If so, it updates CACHE and returns the address of the first
    662    instruction after the sequence that sets removes the "hidden"
    663    argument from the stack or CURRENT_PC, whichever is smaller.
    664    Otherwise, return PC.  */
    665 
    666 static CORE_ADDR
    667 m68k_analyze_frame_setup (struct gdbarch *gdbarch,
    668 			  CORE_ADDR pc, CORE_ADDR current_pc,
    669 			  struct m68k_frame_cache *cache)
    670 {
    671   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    672   int op;
    673 
    674   if (pc >= current_pc)
    675     return current_pc;
    676 
    677   op = read_memory_unsigned_integer (pc, 2, byte_order);
    678 
    679   if (op == P_LINKW_FP || op == P_LINKL_FP || op == P_PEA_FP)
    680     {
    681       cache->saved_regs[M68K_FP_REGNUM] = 0;
    682       cache->sp_offset += 4;
    683       if (op == P_LINKW_FP)
    684 	{
    685 	  /* link.w %fp, #-N */
    686 	  /* link.w %fp, #0; adda.l #-N, %sp */
    687 	  cache->locals = -read_memory_integer (pc + 2, 2, byte_order);
    688 
    689 	  if (pc + 4 < current_pc && cache->locals == 0)
    690 	    {
    691 	      op = read_memory_unsigned_integer (pc + 4, 2, byte_order);
    692 	      if (op == P_ADDAL_SP)
    693 		{
    694 		  cache->locals = read_memory_integer (pc + 6, 4, byte_order);
    695 		  return pc + 10;
    696 		}
    697 	    }
    698 
    699 	  return pc + 4;
    700 	}
    701       else if (op == P_LINKL_FP)
    702 	{
    703 	  /* link.l %fp, #-N */
    704 	  cache->locals = -read_memory_integer (pc + 2, 4, byte_order);
    705 	  return pc + 6;
    706 	}
    707       else
    708 	{
    709 	  /* pea (%fp); movea.l %sp, %fp */
    710 	  cache->locals = 0;
    711 
    712 	  if (pc + 2 < current_pc)
    713 	    {
    714 	      op = read_memory_unsigned_integer (pc + 2, 2, byte_order);
    715 
    716 	      if (op == P_MOVEAL_SP_FP)
    717 		{
    718 		  /* move.l %sp, %fp */
    719 		  return pc + 4;
    720 		}
    721 	    }
    722 
    723 	  return pc + 2;
    724 	}
    725     }
    726   else if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
    727     {
    728       /* subq.[wl] #N,%sp */
    729       /* subq.[wl] #8,%sp; subq.[wl] #N,%sp */
    730       cache->locals = (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
    731       if (pc + 2 < current_pc)
    732 	{
    733 	  op = read_memory_unsigned_integer (pc + 2, 2, byte_order);
    734 	  if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
    735 	    {
    736 	      cache->locals += (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
    737 	      return pc + 4;
    738 	    }
    739 	}
    740       return pc + 2;
    741     }
    742   else if (op == P_ADDAW_SP || op == P_LEA_SP_SP)
    743     {
    744       /* adda.w #-N,%sp */
    745       /* lea (-N,%sp),%sp */
    746       cache->locals = -read_memory_integer (pc + 2, 2, byte_order);
    747       return pc + 4;
    748     }
    749   else if (op == P_ADDAL_SP)
    750     {
    751       /* adda.l #-N,%sp */
    752       cache->locals = -read_memory_integer (pc + 2, 4, byte_order);
    753       return pc + 6;
    754     }
    755 
    756   return pc;
    757 }
    758 
    759 /* Check whether PC points at code that saves registers on the stack.
    760    If so, it updates CACHE and returns the address of the first
    761    instruction after the register saves or CURRENT_PC, whichever is
    762    smaller.  Otherwise, return PC.  */
    763 
    764 static CORE_ADDR
    765 m68k_analyze_register_saves (struct gdbarch *gdbarch, CORE_ADDR pc,
    766 			     CORE_ADDR current_pc,
    767 			     struct m68k_frame_cache *cache)
    768 {
    769   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    770   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
    771 
    772   if (cache->locals >= 0)
    773     {
    774       CORE_ADDR offset;
    775       int op;
    776       int i, mask, regno;
    777 
    778       offset = -4 - cache->locals;
    779       while (pc < current_pc)
    780 	{
    781 	  op = read_memory_unsigned_integer (pc, 2, byte_order);
    782 	  if (op == P_FMOVEMX_SP
    783 	      && tdep->fpregs_present)
    784 	    {
    785 	      /* fmovem.x REGS,-(%sp) */
    786 	      op = read_memory_unsigned_integer (pc + 2, 2, byte_order);
    787 	      if ((op & 0xff00) == 0xe000)
    788 		{
    789 		  mask = op & 0xff;
    790 		  for (i = 0; i < 16; i++, mask >>= 1)
    791 		    {
    792 		      if (mask & 1)
    793 			{
    794 			  cache->saved_regs[i + M68K_FP0_REGNUM] = offset;
    795 			  offset -= 12;
    796 			}
    797 		    }
    798 		  pc += 4;
    799 		}
    800 	      else
    801 		break;
    802 	    }
    803 	  else if ((op & 0177760) == P_MOVEL_SP)
    804 	    {
    805 	      /* move.l %R,-(%sp) */
    806 	      regno = op & 017;
    807 	      cache->saved_regs[regno] = offset;
    808 	      offset -= 4;
    809 	      pc += 2;
    810 	    }
    811 	  else if (op == P_MOVEML_SP)
    812 	    {
    813 	      /* movem.l REGS,-(%sp) */
    814 	      mask = read_memory_unsigned_integer (pc + 2, 2, byte_order);
    815 	      for (i = 0; i < 16; i++, mask >>= 1)
    816 		{
    817 		  if (mask & 1)
    818 		    {
    819 		      cache->saved_regs[15 - i] = offset;
    820 		      offset -= 4;
    821 		    }
    822 		}
    823 	      pc += 4;
    824 	    }
    825 	  else
    826 	    break;
    827 	}
    828     }
    829 
    830   return pc;
    831 }
    832 
    833 
    834 /* Do a full analysis of the prologue at PC and update CACHE
    835    accordingly.  Bail out early if CURRENT_PC is reached.  Return the
    836    address where the analysis stopped.
    837 
    838    We handle all cases that can be generated by gcc.
    839 
    840    For allocating a stack frame:
    841 
    842    link.w %a6,#-N
    843    link.l %a6,#-N
    844    pea (%fp); move.l %sp,%fp
    845    link.w %a6,#0; add.l #-N,%sp
    846    subq.l #N,%sp
    847    subq.w #N,%sp
    848    subq.w #8,%sp; subq.w #N-8,%sp
    849    add.w #-N,%sp
    850    lea (-N,%sp),%sp
    851    add.l #-N,%sp
    852 
    853    For saving registers:
    854 
    855    fmovem.x REGS,-(%sp)
    856    move.l R1,-(%sp)
    857    move.l R1,-(%sp); move.l R2,-(%sp)
    858    movem.l REGS,-(%sp)
    859 
    860    For setting up the PIC register:
    861 
    862    lea (%pc,N),%a5
    863 
    864    */
    865 
    866 static CORE_ADDR
    867 m68k_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
    868 		       CORE_ADDR current_pc, struct m68k_frame_cache *cache)
    869 {
    870   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    871   unsigned int op;
    872 
    873   pc = m68k_analyze_frame_setup (gdbarch, pc, current_pc, cache);
    874   pc = m68k_analyze_register_saves (gdbarch, pc, current_pc, cache);
    875   if (pc >= current_pc)
    876     return current_pc;
    877 
    878   /* Check for GOT setup.  */
    879   op = read_memory_unsigned_integer (pc, 4, byte_order);
    880   if (op == P_LEA_PC_A5)
    881     {
    882       /* lea (%pc,N),%a5 */
    883       return pc + 8;
    884     }
    885 
    886   return pc;
    887 }
    888 
    889 /* Return PC of first real instruction.  */
    890 
    891 static CORE_ADDR
    892 m68k_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
    893 {
    894   struct m68k_frame_cache cache;
    895   CORE_ADDR pc;
    896 
    897   cache.locals = -1;
    898   pc = m68k_analyze_prologue (gdbarch, start_pc, (CORE_ADDR) -1, &cache);
    899   if (cache.locals < 0)
    900     return start_pc;
    901   return pc;
    902 }
    903 
    904 static CORE_ADDR
    905 m68k_unwind_pc (struct gdbarch *gdbarch, frame_info_ptr next_frame)
    906 {
    907   gdb_byte buf[8];
    908 
    909   frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
    910   return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
    911 }
    912 
    913 /* Normal frames.  */
    915 
    916 static struct m68k_frame_cache *
    917 m68k_frame_cache (frame_info_ptr this_frame, void **this_cache)
    918 {
    919   struct gdbarch *gdbarch = get_frame_arch (this_frame);
    920   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    921   struct m68k_frame_cache *cache;
    922   gdb_byte buf[4];
    923   int i;
    924 
    925   if (*this_cache)
    926     return (struct m68k_frame_cache *) *this_cache;
    927 
    928   cache = m68k_alloc_frame_cache ();
    929   *this_cache = cache;
    930 
    931   /* In principle, for normal frames, %fp holds the frame pointer,
    932      which holds the base address for the current stack frame.
    933      However, for functions that don't need it, the frame pointer is
    934      optional.  For these "frameless" functions the frame pointer is
    935      actually the frame pointer of the calling frame.  Signal
    936      trampolines are just a special case of a "frameless" function.
    937      They (usually) share their frame pointer with the frame that was
    938      in progress when the signal occurred.  */
    939 
    940   get_frame_register (this_frame, M68K_FP_REGNUM, buf);
    941   cache->base = extract_unsigned_integer (buf, 4, byte_order);
    942   if (cache->base == 0)
    943     return cache;
    944 
    945   /* For normal frames, %pc is stored at 4(%fp).  */
    946   cache->saved_regs[M68K_PC_REGNUM] = 4;
    947 
    948   cache->pc = get_frame_func (this_frame);
    949   if (cache->pc != 0)
    950     m68k_analyze_prologue (get_frame_arch (this_frame), cache->pc,
    951 			   get_frame_pc (this_frame), cache);
    952 
    953   if (cache->locals < 0)
    954     {
    955       /* We didn't find a valid frame, which means that CACHE->base
    956 	 currently holds the frame pointer for our calling frame.  If
    957 	 we're at the start of a function, or somewhere half-way its
    958 	 prologue, the function's frame probably hasn't been fully
    959 	 setup yet.  Try to reconstruct the base address for the stack
    960 	 frame by looking at the stack pointer.  For truly "frameless"
    961 	 functions this might work too.  */
    962 
    963       get_frame_register (this_frame, M68K_SP_REGNUM, buf);
    964       cache->base = extract_unsigned_integer (buf, 4, byte_order)
    965 		    + cache->sp_offset;
    966     }
    967 
    968   /* Now that we have the base address for the stack frame we can
    969      calculate the value of %sp in the calling frame.  */
    970   cache->saved_sp = cache->base + 8;
    971 
    972   /* Adjust all the saved registers such that they contain addresses
    973      instead of offsets.  */
    974   for (i = 0; i < M68K_NUM_REGS; i++)
    975     if (cache->saved_regs[i] != -1)
    976       cache->saved_regs[i] += cache->base;
    977 
    978   return cache;
    979 }
    980 
    981 static void
    982 m68k_frame_this_id (frame_info_ptr this_frame, void **this_cache,
    983 		    struct frame_id *this_id)
    984 {
    985   struct m68k_frame_cache *cache = m68k_frame_cache (this_frame, this_cache);
    986 
    987   /* This marks the outermost frame.  */
    988   if (cache->base == 0)
    989     return;
    990 
    991   /* See the end of m68k_push_dummy_call.  */
    992   *this_id = frame_id_build (cache->base + 8, cache->pc);
    993 }
    994 
    995 static struct value *
    996 m68k_frame_prev_register (frame_info_ptr this_frame, void **this_cache,
    997 			  int regnum)
    998 {
    999   struct m68k_frame_cache *cache = m68k_frame_cache (this_frame, this_cache);
   1000 
   1001   gdb_assert (regnum >= 0);
   1002 
   1003   if (regnum == M68K_SP_REGNUM && cache->saved_sp)
   1004     return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
   1005 
   1006   if (regnum < M68K_NUM_REGS && cache->saved_regs[regnum] != -1)
   1007     return frame_unwind_got_memory (this_frame, regnum,
   1008 				    cache->saved_regs[regnum]);
   1009 
   1010   return frame_unwind_got_register (this_frame, regnum, regnum);
   1011 }
   1012 
   1013 static const struct frame_unwind m68k_frame_unwind =
   1014 {
   1015   "m68k prologue",
   1016   NORMAL_FRAME,
   1017   default_frame_unwind_stop_reason,
   1018   m68k_frame_this_id,
   1019   m68k_frame_prev_register,
   1020   NULL,
   1021   default_frame_sniffer
   1022 };
   1023 
   1024 static CORE_ADDR
   1026 m68k_frame_base_address (frame_info_ptr this_frame, void **this_cache)
   1027 {
   1028   struct m68k_frame_cache *cache = m68k_frame_cache (this_frame, this_cache);
   1029 
   1030   return cache->base;
   1031 }
   1032 
   1033 static const struct frame_base m68k_frame_base =
   1034 {
   1035   &m68k_frame_unwind,
   1036   m68k_frame_base_address,
   1037   m68k_frame_base_address,
   1038   m68k_frame_base_address
   1039 };
   1040 
   1041 static struct frame_id
   1042 m68k_dummy_id (struct gdbarch *gdbarch, frame_info_ptr this_frame)
   1043 {
   1044   CORE_ADDR fp;
   1045 
   1046   fp = get_frame_register_unsigned (this_frame, M68K_FP_REGNUM);
   1047 
   1048   /* See the end of m68k_push_dummy_call.  */
   1049   return frame_id_build (fp + 8, get_frame_pc (this_frame));
   1050 }
   1051 
   1052 
   1054 /* Figure out where the longjmp will land.  Slurp the args out of the stack.
   1055    We expect the first arg to be a pointer to the jmp_buf structure from which
   1056    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
   1057    This routine returns true on success.  */
   1058 
   1059 static int
   1060 m68k_get_longjmp_target (frame_info_ptr frame, CORE_ADDR *pc)
   1061 {
   1062   gdb_byte *buf;
   1063   CORE_ADDR sp, jb_addr;
   1064   struct gdbarch *gdbarch = get_frame_arch (frame);
   1065   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
   1066   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   1067 
   1068   if (tdep->jb_pc < 0)
   1069     {
   1070       internal_error (_("m68k_get_longjmp_target: not implemented"));
   1071       return 0;
   1072     }
   1073 
   1074   buf = (gdb_byte *) alloca (gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT);
   1075   sp = get_frame_register_unsigned (frame, gdbarch_sp_regnum (gdbarch));
   1076 
   1077   if (target_read_memory (sp + SP_ARG0,	/* Offset of first arg on stack.  */
   1078 			  buf, gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT))
   1079     return 0;
   1080 
   1081   jb_addr = extract_unsigned_integer (buf, gdbarch_ptr_bit (gdbarch)
   1082 					     / TARGET_CHAR_BIT, byte_order);
   1083 
   1084   if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
   1085 			  gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT),
   1086 			  byte_order)
   1087     return 0;
   1088 
   1089   *pc = extract_unsigned_integer (buf, gdbarch_ptr_bit (gdbarch)
   1090 					 / TARGET_CHAR_BIT, byte_order);
   1091   return 1;
   1092 }
   1093 
   1094 
   1096 /* This is the implementation of gdbarch method
   1097    return_in_first_hidden_param_p.  */
   1098 
   1099 static int
   1100 m68k_return_in_first_hidden_param_p (struct gdbarch *gdbarch,
   1101 				     struct type *type)
   1102 {
   1103   return 0;
   1104 }
   1105 
   1106 /* System V Release 4 (SVR4).  */
   1107 
   1108 void
   1109 m68k_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   1110 {
   1111   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
   1112 
   1113   /* SVR4 uses a different calling convention.  */
   1114   set_gdbarch_return_value (gdbarch, m68k_svr4_return_value);
   1115 
   1116   /* SVR4 uses %a0 instead of %a1.  */
   1117   tdep->struct_value_regnum = M68K_A0_REGNUM;
   1118 
   1119   /* SVR4 returns pointers in %a0.  */
   1120   tdep->pointer_result_regnum = M68K_A0_REGNUM;
   1121 }
   1122 
   1123 /* GCC's m68k "embedded" ABI.  This is like the SVR4 ABI, but pointer
   1124    values are returned in %d0, not %a0.  */
   1125 
   1126 static void
   1127 m68k_embedded_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   1128 {
   1129   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
   1130 
   1131   m68k_svr4_init_abi (info, gdbarch);
   1132   tdep->pointer_result_regnum = M68K_D0_REGNUM;
   1133 }
   1134 
   1135 
   1136 
   1138 /* Function: m68k_gdbarch_init
   1139    Initializer function for the m68k gdbarch vector.
   1140    Called by gdbarch.  Sets up the gdbarch vector(s) for this target.  */
   1141 
   1142 static struct gdbarch *
   1143 m68k_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   1144 {
   1145   struct gdbarch *gdbarch;
   1146   struct gdbarch_list *best_arch;
   1147   tdesc_arch_data_up tdesc_data;
   1148   int i;
   1149   enum m68k_flavour flavour = m68k_no_flavour;
   1150   int has_fp = 1;
   1151   const struct floatformat **long_double_format = floatformats_m68881_ext;
   1152 
   1153   /* Check any target description for validity.  */
   1154   if (tdesc_has_registers (info.target_desc))
   1155     {
   1156       const struct tdesc_feature *feature;
   1157       int valid_p;
   1158 
   1159       feature = tdesc_find_feature (info.target_desc,
   1160 				    "org.gnu.gdb.m68k.core");
   1161 
   1162       if (feature == NULL)
   1163 	{
   1164 	  feature = tdesc_find_feature (info.target_desc,
   1165 					"org.gnu.gdb.coldfire.core");
   1166 	  if (feature != NULL)
   1167 	    flavour = m68k_coldfire_flavour;
   1168 	}
   1169 
   1170       if (feature == NULL)
   1171 	{
   1172 	  feature = tdesc_find_feature (info.target_desc,
   1173 					"org.gnu.gdb.fido.core");
   1174 	  if (feature != NULL)
   1175 	    flavour = m68k_fido_flavour;
   1176 	}
   1177 
   1178       if (feature == NULL)
   1179 	return NULL;
   1180 
   1181       tdesc_data = tdesc_data_alloc ();
   1182 
   1183       valid_p = 1;
   1184       for (i = 0; i <= M68K_PC_REGNUM; i++)
   1185 	valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
   1186 					    m68k_register_names[i]);
   1187 
   1188       if (!valid_p)
   1189 	return NULL;
   1190 
   1191       feature = tdesc_find_feature (info.target_desc,
   1192 				    "org.gnu.gdb.coldfire.fp");
   1193       if (feature != NULL)
   1194 	{
   1195 	  valid_p = 1;
   1196 	  for (i = M68K_FP0_REGNUM; i <= M68K_FPI_REGNUM; i++)
   1197 	    valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
   1198 						m68k_register_names[i]);
   1199 	  if (!valid_p)
   1200 	    return NULL;
   1201 	}
   1202       else
   1203 	has_fp = 0;
   1204     }
   1205 
   1206   /* The mechanism for returning floating values from function
   1207      and the type of long double depend on whether we're
   1208      on ColdFire or standard m68k.  */
   1209 
   1210   if (info.bfd_arch_info && info.bfd_arch_info->mach != 0)
   1211     {
   1212       const bfd_arch_info_type *coldfire_arch =
   1213 	bfd_lookup_arch (bfd_arch_m68k, bfd_mach_mcf_isa_a_nodiv);
   1214 
   1215       if (coldfire_arch
   1216 	  && ((*info.bfd_arch_info->compatible)
   1217 	      (info.bfd_arch_info, coldfire_arch)))
   1218 	flavour = m68k_coldfire_flavour;
   1219     }
   1220 
   1221   /* Try to figure out if the arch uses floating registers to return
   1222      floating point values from functions.  On ColdFire, floating
   1223      point values are returned in D0.  */
   1224   int float_return = 0;
   1225   if (has_fp && flavour != m68k_coldfire_flavour)
   1226     float_return = 1;
   1227 #ifdef HAVE_ELF
   1228   if (info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
   1229     {
   1230       int fp_abi = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_GNU,
   1231 					     Tag_GNU_M68K_ABI_FP);
   1232       if (fp_abi == 1)
   1233 	float_return = 1;
   1234       else if (fp_abi == 2)
   1235 	float_return = 0;
   1236     }
   1237 #endif /* HAVE_ELF */
   1238 
   1239   /* If there is already a candidate, use it.  */
   1240   for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
   1241        best_arch != NULL;
   1242        best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
   1243     {
   1244       m68k_gdbarch_tdep *tdep
   1245 	= gdbarch_tdep<m68k_gdbarch_tdep> (best_arch->gdbarch);
   1246 
   1247       if (flavour != tdep->flavour)
   1248 	continue;
   1249 
   1250       if (has_fp != tdep->fpregs_present)
   1251 	continue;
   1252 
   1253       if (float_return != tdep->float_return)
   1254 	continue;
   1255 
   1256       break;
   1257     }
   1258 
   1259   if (best_arch != NULL)
   1260     return best_arch->gdbarch;
   1261 
   1262   m68k_gdbarch_tdep *tdep = new m68k_gdbarch_tdep;
   1263   gdbarch = gdbarch_alloc (&info, tdep);
   1264   tdep->fpregs_present = has_fp;
   1265   tdep->float_return = float_return;
   1266   tdep->flavour = flavour;
   1267 
   1268   if (flavour == m68k_coldfire_flavour || flavour == m68k_fido_flavour)
   1269     long_double_format = floatformats_ieee_double;
   1270   set_gdbarch_long_double_format (gdbarch, long_double_format);
   1271   set_gdbarch_long_double_bit (gdbarch, long_double_format[0]->totalsize);
   1272 
   1273   set_gdbarch_skip_prologue (gdbarch, m68k_skip_prologue);
   1274   set_gdbarch_breakpoint_kind_from_pc (gdbarch, m68k_breakpoint::kind_from_pc);
   1275   set_gdbarch_sw_breakpoint_from_kind (gdbarch, m68k_breakpoint::bp_from_kind);
   1276 
   1277   /* Stack grows down.  */
   1278   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
   1279   set_gdbarch_frame_align (gdbarch, m68k_frame_align);
   1280 
   1281   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
   1282   if (flavour == m68k_coldfire_flavour || flavour == m68k_fido_flavour)
   1283     set_gdbarch_decr_pc_after_break (gdbarch, 2);
   1284 
   1285   set_gdbarch_frame_args_skip (gdbarch, 8);
   1286   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, m68k_dwarf_reg_to_regnum);
   1287 
   1288   set_gdbarch_register_type (gdbarch, m68k_register_type);
   1289   set_gdbarch_register_name (gdbarch, m68k_register_name);
   1290   set_gdbarch_num_regs (gdbarch, M68K_NUM_REGS);
   1291   set_gdbarch_sp_regnum (gdbarch, M68K_SP_REGNUM);
   1292   set_gdbarch_pc_regnum (gdbarch, M68K_PC_REGNUM);
   1293   set_gdbarch_ps_regnum (gdbarch, M68K_PS_REGNUM);
   1294   set_gdbarch_convert_register_p (gdbarch, m68k_convert_register_p);
   1295   set_gdbarch_register_to_value (gdbarch,  m68k_register_to_value);
   1296   set_gdbarch_value_to_register (gdbarch, m68k_value_to_register);
   1297 
   1298   if (has_fp)
   1299     set_gdbarch_fp0_regnum (gdbarch, M68K_FP0_REGNUM);
   1300 
   1301   /* Function call & return.  */
   1302   set_gdbarch_push_dummy_call (gdbarch, m68k_push_dummy_call);
   1303   set_gdbarch_return_value (gdbarch, m68k_return_value);
   1304   set_gdbarch_return_in_first_hidden_param_p (gdbarch,
   1305 					      m68k_return_in_first_hidden_param_p);
   1306 
   1307 #if defined JB_PC && defined JB_ELEMENT_SIZE
   1308   tdep->jb_pc = JB_PC;
   1309   tdep->jb_elt_size = JB_ELEMENT_SIZE;
   1310 #else
   1311   tdep->jb_pc = -1;
   1312 #endif
   1313   tdep->pointer_result_regnum = M68K_D0_REGNUM;
   1314   tdep->struct_value_regnum = M68K_A1_REGNUM;
   1315   tdep->struct_return = reg_struct_return;
   1316 
   1317   /* Frame unwinder.  */
   1318   set_gdbarch_dummy_id (gdbarch, m68k_dummy_id);
   1319   set_gdbarch_unwind_pc (gdbarch, m68k_unwind_pc);
   1320 
   1321   /* Hook in the DWARF CFI frame unwinder.  */
   1322   dwarf2_append_unwinders (gdbarch);
   1323 
   1324   frame_base_set_default (gdbarch, &m68k_frame_base);
   1325 
   1326   /* Hook in ABI-specific overrides, if they have been registered.  */
   1327   gdbarch_init_osabi (info, gdbarch);
   1328 
   1329   /* Now we have tuned the configuration, set a few final things,
   1330      based on what the OS ABI has told us.  */
   1331 
   1332   if (tdep->jb_pc >= 0)
   1333     set_gdbarch_get_longjmp_target (gdbarch, m68k_get_longjmp_target);
   1334 
   1335   frame_unwind_append_unwinder (gdbarch, &m68k_frame_unwind);
   1336 
   1337   if (tdesc_data != nullptr)
   1338     tdesc_use_registers (gdbarch, info.target_desc, std::move (tdesc_data));
   1339 
   1340   return gdbarch;
   1341 }
   1342 
   1343 
   1344 static void
   1345 m68k_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
   1346 {
   1347   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
   1348 
   1349   if (tdep == NULL)
   1350     return;
   1351 }
   1352 
   1353 /* OSABI sniffer for m68k.  */
   1354 
   1355 static enum gdb_osabi
   1356 m68k_osabi_sniffer (bfd *abfd)
   1357 {
   1358   /* XXX NetBSD uses ELFOSABI_NONE == ELFOSABI_SYSV. Therefore, do not
   1359      fall back to EABI here.  */
   1360 #ifndef __NetBSD__
   1361   unsigned int elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
   1362 
   1363   if (elfosabi == ELFOSABI_NONE)
   1364     return GDB_OSABI_SVR4;
   1365 #endif
   1366 
   1367   return GDB_OSABI_UNKNOWN;
   1368 }
   1369 
   1370 void _initialize_m68k_tdep ();
   1371 void
   1372 _initialize_m68k_tdep ()
   1373 {
   1374   gdbarch_register (bfd_arch_m68k, m68k_gdbarch_init, m68k_dump_tdep);
   1375 
   1376   gdbarch_register_osabi_sniffer (bfd_arch_m68k, bfd_target_elf_flavour,
   1377 				  m68k_osabi_sniffer);
   1378   gdbarch_register_osabi (bfd_arch_m68k, 0, GDB_OSABI_SVR4,
   1379 			  m68k_embedded_init_abi);
   1380 }
   1381