Home | History | Annotate | Line # | Download | only in gdb
      1 /* Motorola m68k target-dependent support for GNU/Linux.
      2 
      3    Copyright (C) 1996-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 "extract-store-integer.h"
     21 #include "gdbcore.h"
     22 #include "frame.h"
     23 #include "target.h"
     24 #include "gdbtypes.h"
     25 #include "osabi.h"
     26 #include "regcache.h"
     27 #include "objfiles.h"
     28 #include "symtab.h"
     29 #include "m68k-tdep.h"
     30 #include "trad-frame.h"
     31 #include "frame-unwind.h"
     32 #include "glibc-tdep.h"
     33 #include "solib-svr4.h"
     34 #include "auxv.h"
     35 #include "observable.h"
     36 #include "elf/common.h"
     37 #include "linux-tdep.h"
     38 #include "regset.h"
     39 
     40 /* Offsets (in target ints) into jmp_buf.  */
     42 
     43 #define M68K_LINUX_JB_ELEMENT_SIZE 4
     44 #define M68K_LINUX_JB_PC 7
     45 
     46 /* Check whether insn1 and insn2 are parts of a signal trampoline.  */
     47 
     48 #define IS_SIGTRAMP(insn1, insn2)					\
     49   (/* addaw #20,sp; moveq #119,d0; trap #0 */				\
     50    (insn1 == 0xdefc0014 && insn2 == 0x70774e40)				\
     51    /* moveq #119,d0; trap #0 */						\
     52    || insn1 == 0x70774e40)
     53 
     54 #define IS_RT_SIGTRAMP(insn1, insn2)					\
     55   (/* movel #173,d0; trap #0 */						\
     56    (insn1 == 0x203c0000 && insn2 == 0x00ad4e40)				\
     57    /* moveq #82,d0; notb d0; trap #0 */					\
     58    || (insn1 == 0x70524600 && (insn2 >> 16) == 0x4e40))
     59 
     60 /* Return non-zero if THIS_FRAME corresponds to a signal trampoline.  For
     61    the sake of m68k_linux_get_sigtramp_info we also distinguish between
     62    non-RT and RT signal trampolines.  */
     63 
     64 static int
     65 m68k_linux_pc_in_sigtramp (const frame_info_ptr &this_frame)
     66 {
     67   struct gdbarch *gdbarch = get_frame_arch (this_frame);
     68   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
     69   gdb_byte buf[12];
     70   unsigned long insn0, insn1, insn2;
     71   CORE_ADDR pc = get_frame_pc (this_frame);
     72 
     73   if (!safe_frame_unwind_memory (this_frame, pc - 4, {buf, sizeof (buf)}))
     74     return 0;
     75   insn1 = extract_unsigned_integer (buf + 4, 4, byte_order);
     76   insn2 = extract_unsigned_integer (buf + 8, 4, byte_order);
     77   if (IS_SIGTRAMP (insn1, insn2))
     78     return 1;
     79   if (IS_RT_SIGTRAMP (insn1, insn2))
     80     return 2;
     81 
     82   insn0 = extract_unsigned_integer (buf, 4, byte_order);
     83   if (IS_SIGTRAMP (insn0, insn1))
     84     return 1;
     85   if (IS_RT_SIGTRAMP (insn0, insn1))
     86     return 2;
     87 
     88   insn0 = ((insn0 << 16) & 0xffffffff) | (insn1 >> 16);
     89   insn1 = ((insn1 << 16) & 0xffffffff) | (insn2 >> 16);
     90   if (IS_SIGTRAMP (insn0, insn1))
     91     return 1;
     92   if (IS_RT_SIGTRAMP (insn0, insn1))
     93     return 2;
     94 
     95   return 0;
     96 }
     97 
     98 /* From <asm/sigcontext.h>.  */
     99 static int m68k_linux_sigcontext_reg_offset[M68K_NUM_REGS] =
    100 {
    101   2 * 4,			/* %d0 */
    102   3 * 4,			/* %d1 */
    103   -1,				/* %d2 */
    104   -1,				/* %d3 */
    105   -1,				/* %d4 */
    106   -1,				/* %d5 */
    107   -1,				/* %d6 */
    108   -1,				/* %d7 */
    109   4 * 4,			/* %a0 */
    110   5 * 4,			/* %a1 */
    111   -1,				/* %a2 */
    112   -1,				/* %a3 */
    113   -1,				/* %a4 */
    114   -1,				/* %a5 */
    115   -1,				/* %fp */
    116   1 * 4,			/* %sp */
    117   6 * 4,			/* %sr */
    118   6 * 4 + 2,			/* %pc */
    119   8 * 4,			/* %fp0 */
    120   11 * 4,			/* %fp1 */
    121   -1,				/* %fp2 */
    122   -1,				/* %fp3 */
    123   -1,				/* %fp4 */
    124   -1,				/* %fp5 */
    125   -1,				/* %fp6 */
    126   -1,				/* %fp7 */
    127   14 * 4,			/* %fpcr */
    128   15 * 4,			/* %fpsr */
    129   16 * 4			/* %fpiaddr */
    130 };
    131 
    132 static int m68k_uclinux_sigcontext_reg_offset[M68K_NUM_REGS] =
    133 {
    134   2 * 4,			/* %d0 */
    135   3 * 4,			/* %d1 */
    136   -1,				/* %d2 */
    137   -1,				/* %d3 */
    138   -1,				/* %d4 */
    139   -1,				/* %d5 */
    140   -1,				/* %d6 */
    141   -1,				/* %d7 */
    142   4 * 4,			/* %a0 */
    143   5 * 4,			/* %a1 */
    144   -1,				/* %a2 */
    145   -1,				/* %a3 */
    146   -1,				/* %a4 */
    147   6 * 4,			/* %a5 */
    148   -1,				/* %fp */
    149   1 * 4,			/* %sp */
    150   7 * 4,			/* %sr */
    151   7 * 4 + 2,			/* %pc */
    152   -1,				/* %fp0 */
    153   -1,				/* %fp1 */
    154   -1,				/* %fp2 */
    155   -1,				/* %fp3 */
    156   -1,				/* %fp4 */
    157   -1,				/* %fp5 */
    158   -1,				/* %fp6 */
    159   -1,				/* %fp7 */
    160   -1,				/* %fpcr */
    161   -1,				/* %fpsr */
    162   -1				/* %fpiaddr */
    163 };
    164 
    165 /* From <asm/ucontext.h>.  */
    166 static int m68k_linux_ucontext_reg_offset[M68K_NUM_REGS] =
    167 {
    168   6 * 4,			/* %d0 */
    169   7 * 4,			/* %d1 */
    170   8 * 4,			/* %d2 */
    171   9 * 4,			/* %d3 */
    172   10 * 4,			/* %d4 */
    173   11 * 4,			/* %d5 */
    174   12 * 4,			/* %d6 */
    175   13 * 4,			/* %d7 */
    176   14 * 4,			/* %a0 */
    177   15 * 4,			/* %a1 */
    178   16 * 4,			/* %a2 */
    179   17 * 4,			/* %a3 */
    180   18 * 4,			/* %a4 */
    181   19 * 4,			/* %a5 */
    182   20 * 4,			/* %fp */
    183   21 * 4,			/* %sp */
    184   23 * 4,			/* %sr */
    185   22 * 4,			/* %pc */
    186   27 * 4,			/* %fp0 */
    187   30 * 4,			/* %fp1 */
    188   33 * 4,			/* %fp2 */
    189   36 * 4,			/* %fp3 */
    190   39 * 4,			/* %fp4 */
    191   42 * 4,			/* %fp5 */
    192   45 * 4,			/* %fp6 */
    193   48 * 4,			/* %fp7 */
    194   24 * 4,			/* %fpcr */
    195   25 * 4,			/* %fpsr */
    196   26 * 4			/* %fpiaddr */
    197 };
    198 
    199 
    200 /* Get info about saved registers in sigtramp.  */
    201 
    202 struct m68k_linux_sigtramp_info
    203 {
    204   /* Address of sigcontext.  */
    205   CORE_ADDR sigcontext_addr;
    206 
    207   /* Offset of registers in `struct sigcontext'.  */
    208   int *sc_reg_offset;
    209 };
    210 
    211 /* Nonzero if running on uClinux.  */
    212 static int target_is_uclinux;
    213 
    214 static void
    215 m68k_linux_inferior_created (inferior *inf)
    216 {
    217   /* Record that we will need to re-evaluate whether we are running on a
    218      uClinux or normal GNU/Linux target (see m68k_linux_get_sigtramp_info).  */
    219   target_is_uclinux = -1;
    220 }
    221 
    222 static struct m68k_linux_sigtramp_info
    223 m68k_linux_get_sigtramp_info (const frame_info_ptr &this_frame)
    224 {
    225   struct gdbarch *gdbarch = get_frame_arch (this_frame);
    226   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    227   CORE_ADDR sp;
    228   struct m68k_linux_sigtramp_info info;
    229 
    230   /* Determine whether we are running on a uClinux or normal GNU/Linux
    231      target so we can use the correct sigcontext layouts.  */
    232   if (target_is_uclinux == -1)
    233     target_is_uclinux = linux_is_uclinux ();
    234 
    235   sp = get_frame_register_unsigned (this_frame, M68K_SP_REGNUM);
    236 
    237   /* Get sigcontext address, it is the third parameter on the stack.  */
    238   info.sigcontext_addr = read_memory_unsigned_integer (sp + 8, 4, byte_order);
    239 
    240   if (m68k_linux_pc_in_sigtramp (this_frame) == 2)
    241     info.sc_reg_offset = m68k_linux_ucontext_reg_offset;
    242   else
    243     info.sc_reg_offset = (target_is_uclinux
    244 			  ? m68k_uclinux_sigcontext_reg_offset
    245 			  : m68k_linux_sigcontext_reg_offset);
    246   return info;
    247 }
    248 
    249 /* Signal trampolines.  */
    250 
    251 static struct trad_frame_cache *
    252 m68k_linux_sigtramp_frame_cache (const frame_info_ptr &this_frame,
    253 				 void **this_cache)
    254 {
    255   struct frame_id this_id;
    256   struct trad_frame_cache *cache;
    257   struct gdbarch *gdbarch = get_frame_arch (this_frame);
    258   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    259   struct m68k_linux_sigtramp_info info;
    260   gdb_byte buf[4];
    261   int i;
    262 
    263   if (*this_cache)
    264     return (struct trad_frame_cache *) *this_cache;
    265 
    266   cache = trad_frame_cache_zalloc (this_frame);
    267 
    268   /* FIXME: cagney/2004-05-01: This is is long standing broken code.
    269      The frame ID's code address should be the start-address of the
    270      signal trampoline and not the current PC within that
    271      trampoline.  */
    272   get_frame_register (this_frame, M68K_SP_REGNUM, buf);
    273   /* See the end of m68k_push_dummy_call.  */
    274   this_id = frame_id_build (extract_unsigned_integer (buf, 4, byte_order)
    275 			    - 4 + 8, get_frame_pc (this_frame));
    276   trad_frame_set_id (cache, this_id);
    277 
    278   info = m68k_linux_get_sigtramp_info (this_frame);
    279 
    280   for (i = 0; i < M68K_NUM_REGS; i++)
    281     if (info.sc_reg_offset[i] != -1)
    282       trad_frame_set_reg_addr (cache, i,
    283 			       info.sigcontext_addr + info.sc_reg_offset[i]);
    284 
    285   *this_cache = cache;
    286   return cache;
    287 }
    288 
    289 static void
    290 m68k_linux_sigtramp_frame_this_id (const frame_info_ptr &this_frame,
    291 				   void **this_cache,
    292 				   struct frame_id *this_id)
    293 {
    294   struct trad_frame_cache *cache =
    295     m68k_linux_sigtramp_frame_cache (this_frame, this_cache);
    296   trad_frame_get_id (cache, this_id);
    297 }
    298 
    299 static struct value *
    300 m68k_linux_sigtramp_frame_prev_register (const frame_info_ptr &this_frame,
    301 					 void **this_cache,
    302 					 int regnum)
    303 {
    304   /* Make sure we've initialized the cache.  */
    305   struct trad_frame_cache *cache =
    306     m68k_linux_sigtramp_frame_cache (this_frame, this_cache);
    307   return trad_frame_get_register (cache, this_frame, regnum);
    308 }
    309 
    310 static int
    311 m68k_linux_sigtramp_frame_sniffer (const struct frame_unwind *self,
    312 				   const frame_info_ptr &this_frame,
    313 				   void **this_prologue_cache)
    314 {
    315   return m68k_linux_pc_in_sigtramp (this_frame);
    316 }
    317 
    318 static const struct frame_unwind m68k_linux_sigtramp_frame_unwind =
    319 {
    320   "m68k linux sigtramp",
    321   SIGTRAMP_FRAME,
    322   default_frame_unwind_stop_reason,
    323   m68k_linux_sigtramp_frame_this_id,
    324   m68k_linux_sigtramp_frame_prev_register,
    325   NULL,
    326   m68k_linux_sigtramp_frame_sniffer
    327 };
    328 
    329 /* Register maps for supply/collect regset functions.  */
    330 
    331 static const struct regcache_map_entry m68k_linux_gregmap[] =
    332   {
    333     { 7, M68K_D1_REGNUM, 4 },	/* d1 ... d7 */
    334     { 7, M68K_A0_REGNUM, 4 },	/* a0 ... a6 */
    335     { 1, M68K_D0_REGNUM, 4 },
    336     { 1, M68K_SP_REGNUM, 4 },
    337     { 1, REGCACHE_MAP_SKIP, 4 }, /* orig_d0 (skip) */
    338     { 1, M68K_PS_REGNUM, 4 },
    339     { 1, M68K_PC_REGNUM, 4 },
    340     /* Ignore 16-bit fields 'fmtvec' and '__fill'.  */
    341     { 0 }
    342   };
    343 
    344 #define M68K_LINUX_GREGS_SIZE (20 * 4)
    345 
    346 static const struct regcache_map_entry m68k_linux_fpregmap[] =
    347   {
    348     { 8, M68K_FP0_REGNUM, 12 },	/* fp0 ... fp7 */
    349     { 1, M68K_FPC_REGNUM, 4 },
    350     { 1, M68K_FPS_REGNUM, 4 },
    351     { 1, M68K_FPI_REGNUM, 4 },
    352     { 0 }
    353   };
    354 
    355 #define M68K_LINUX_FPREGS_SIZE (27 * 4)
    356 
    357 /* Register sets. */
    358 
    359 static const struct regset m68k_linux_gregset =
    360   {
    361     m68k_linux_gregmap,
    362     regcache_supply_regset, regcache_collect_regset
    363   };
    364 
    365 static const struct regset m68k_linux_fpregset =
    366   {
    367     m68k_linux_fpregmap,
    368     regcache_supply_regset, regcache_collect_regset
    369   };
    370 
    371 /* Iterate over core file register note sections.  */
    372 
    373 static void
    374 m68k_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
    375 					 iterate_over_regset_sections_cb *cb,
    376 					 void *cb_data,
    377 					 const struct regcache *regcache)
    378 {
    379   cb (".reg", M68K_LINUX_GREGS_SIZE, M68K_LINUX_GREGS_SIZE, &m68k_linux_gregset,
    380       NULL, cb_data);
    381   cb (".reg2", M68K_LINUX_FPREGS_SIZE, M68K_LINUX_FPREGS_SIZE,
    382       &m68k_linux_fpregset, NULL, cb_data);
    383 }
    384 
    385 static void
    386 m68k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
    387 {
    388   m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
    389 
    390   linux_init_abi (info, gdbarch, 0);
    391 
    392   tdep->jb_pc = M68K_LINUX_JB_PC;
    393   tdep->jb_elt_size = M68K_LINUX_JB_ELEMENT_SIZE;
    394 
    395   /* GNU/Linux uses a calling convention that's similar to SVR4.  It
    396      returns integer values in %d0/%d1, pointer values in %a0 and
    397      floating values in %fp0, just like SVR4, but uses %a1 to pass the
    398      address to store a structure value.  It also returns small
    399      structures in registers instead of memory.  */
    400   m68k_svr4_init_abi (info, gdbarch);
    401   tdep->struct_value_regnum = M68K_A1_REGNUM;
    402   tdep->struct_return = reg_struct_return;
    403 
    404   set_gdbarch_decr_pc_after_break (gdbarch, 2);
    405 
    406   frame_unwind_append_unwinder (gdbarch, &m68k_linux_sigtramp_frame_unwind);
    407 
    408   /* Shared library handling.  */
    409 
    410   /* GNU/Linux uses SVR4-style shared libraries.  */
    411   set_solib_svr4_fetch_link_map_offsets (gdbarch,
    412 					 linux_ilp32_fetch_link_map_offsets);
    413 
    414   /* GNU/Linux uses the dynamic linker included in the GNU C Library.  */
    415   set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
    416 
    417   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
    418 
    419   /* Core file support. */
    420   set_gdbarch_iterate_over_regset_sections
    421     (gdbarch, m68k_linux_iterate_over_regset_sections);
    422 
    423   /* Enable TLS support.  */
    424   set_gdbarch_fetch_tls_load_module_address (gdbarch,
    425 					     svr4_fetch_objfile_link_map);
    426 }
    427 
    428 void _initialize_m68k_linux_tdep ();
    429 void
    430 _initialize_m68k_linux_tdep ()
    431 {
    432   gdbarch_register_osabi (bfd_arch_m68k, 0, GDB_OSABI_LINUX,
    433 			  m68k_linux_init_abi);
    434   gdb::observers::inferior_created.attach (m68k_linux_inferior_created,
    435 					   "m68k-linux-tdep");
    436 }
    437