gdb/m68klinux-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Motorola m68k target-dependent support for GNU/Linux.

  2.    Copyright (C) 1996-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "gdbcore.h"
  16. #include "doublest.h"
  17. #include "floatformat.h"
  18. #include "frame.h"
  19. #include "target.h"
  20. #include "gdbtypes.h"
  21. #include "osabi.h"
  22. #include "regcache.h"
  23. #include "objfiles.h"
  24. #include "symtab.h"
  25. #include "m68k-tdep.h"
  26. #include "trad-frame.h"
  27. #include "frame-unwind.h"
  28. #include "glibc-tdep.h"
  29. #include "solib-svr4.h"
  30. #include "auxv.h"
  31. #include "observer.h"
  32. #include "elf/common.h"
  33. #include "linux-tdep.h"
  34. #include "regset.h"

  35. /* Offsets (in target ints) into jmp_buf.  */

  36. #define M68K_LINUX_JB_ELEMENT_SIZE 4
  37. #define M68K_LINUX_JB_PC 7

  38. /* Check whether insn1 and insn2 are parts of a signal trampoline.  */

  39. #define IS_SIGTRAMP(insn1, insn2)                                        \
  40.   (/* addaw #20,sp; moveq #119,d0; trap #0 */                                \
  41.    (insn1 == 0xdefc0014 && insn2 == 0x70774e40)                                \
  42.    /* moveq #119,d0; trap #0 */                                                \
  43.    || insn1 == 0x70774e40)

  44. #define IS_RT_SIGTRAMP(insn1, insn2)                                        \
  45.   (/* movel #173,d0; trap #0 */                                                \
  46.    (insn1 == 0x203c0000 && insn2 == 0x00ad4e40)                                \
  47.    /* moveq #82,d0; notb d0; trap #0 */                                        \
  48.    || (insn1 == 0x70524600 && (insn2 >> 16) == 0x4e40))

  49. /* Return non-zero if THIS_FRAME corresponds to a signal trampoline.  For
  50.    the sake of m68k_linux_get_sigtramp_info we also distinguish between
  51.    non-RT and RT signal trampolines.  */

  52. static int
  53. m68k_linux_pc_in_sigtramp (struct frame_info *this_frame)
  54. {
  55.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  56.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  57.   gdb_byte buf[12];
  58.   unsigned long insn0, insn1, insn2;
  59.   CORE_ADDR pc = get_frame_pc (this_frame);

  60.   if (!safe_frame_unwind_memory (this_frame, pc - 4, buf, sizeof (buf)))
  61.     return 0;
  62.   insn1 = extract_unsigned_integer (buf + 4, 4, byte_order);
  63.   insn2 = extract_unsigned_integer (buf + 8, 4, byte_order);
  64.   if (IS_SIGTRAMP (insn1, insn2))
  65.     return 1;
  66.   if (IS_RT_SIGTRAMP (insn1, insn2))
  67.     return 2;

  68.   insn0 = extract_unsigned_integer (buf, 4, byte_order);
  69.   if (IS_SIGTRAMP (insn0, insn1))
  70.     return 1;
  71.   if (IS_RT_SIGTRAMP (insn0, insn1))
  72.     return 2;

  73.   insn0 = ((insn0 << 16) & 0xffffffff) | (insn1 >> 16);
  74.   insn1 = ((insn1 << 16) & 0xffffffff) | (insn2 >> 16);
  75.   if (IS_SIGTRAMP (insn0, insn1))
  76.     return 1;
  77.   if (IS_RT_SIGTRAMP (insn0, insn1))
  78.     return 2;

  79.   return 0;
  80. }

  81. /* From <asm/sigcontext.h>.  */
  82. static int m68k_linux_sigcontext_reg_offset[M68K_NUM_REGS] =
  83. {
  84.   2 * 4,                        /* %d0 */
  85.   3 * 4,                        /* %d1 */
  86.   -1,                                /* %d2 */
  87.   -1,                                /* %d3 */
  88.   -1,                                /* %d4 */
  89.   -1,                                /* %d5 */
  90.   -1,                                /* %d6 */
  91.   -1,                                /* %d7 */
  92.   4 * 4,                        /* %a0 */
  93.   5 * 4,                        /* %a1 */
  94.   -1,                                /* %a2 */
  95.   -1,                                /* %a3 */
  96.   -1,                                /* %a4 */
  97.   -1,                                /* %a5 */
  98.   -1,                                /* %fp */
  99.   1 * 4,                        /* %sp */
  100.   6 * 4,                        /* %sr */
  101.   6 * 4 + 2,                        /* %pc */
  102.   8 * 4,                        /* %fp0 */
  103.   11 * 4,                        /* %fp1 */
  104.   -1,                                /* %fp2 */
  105.   -1,                                /* %fp3 */
  106.   -1,                                /* %fp4 */
  107.   -1,                                /* %fp5 */
  108.   -1,                                /* %fp6 */
  109.   -1,                                /* %fp7 */
  110.   14 * 4,                        /* %fpcr */
  111.   15 * 4,                        /* %fpsr */
  112.   16 * 4                        /* %fpiaddr */
  113. };

  114. static int m68k_uclinux_sigcontext_reg_offset[M68K_NUM_REGS] =
  115. {
  116.   2 * 4,                        /* %d0 */
  117.   3 * 4,                        /* %d1 */
  118.   -1,                                /* %d2 */
  119.   -1,                                /* %d3 */
  120.   -1,                                /* %d4 */
  121.   -1,                                /* %d5 */
  122.   -1,                                /* %d6 */
  123.   -1,                                /* %d7 */
  124.   4 * 4,                        /* %a0 */
  125.   5 * 4,                        /* %a1 */
  126.   -1,                                /* %a2 */
  127.   -1,                                /* %a3 */
  128.   -1,                                /* %a4 */
  129.   6 * 4,                        /* %a5 */
  130.   -1,                                /* %fp */
  131.   1 * 4,                        /* %sp */
  132.   7 * 4,                        /* %sr */
  133.   7 * 4 + 2,                        /* %pc */
  134.   -1,                                /* %fp0 */
  135.   -1,                                /* %fp1 */
  136.   -1,                                /* %fp2 */
  137.   -1,                                /* %fp3 */
  138.   -1,                                /* %fp4 */
  139.   -1,                                /* %fp5 */
  140.   -1,                                /* %fp6 */
  141.   -1,                                /* %fp7 */
  142.   -1,                                /* %fpcr */
  143.   -1,                                /* %fpsr */
  144.   -1                                /* %fpiaddr */
  145. };

  146. /* From <asm/ucontext.h>.  */
  147. static int m68k_linux_ucontext_reg_offset[M68K_NUM_REGS] =
  148. {
  149.   6 * 4,                        /* %d0 */
  150.   7 * 4,                        /* %d1 */
  151.   8 * 4,                        /* %d2 */
  152.   9 * 4,                        /* %d3 */
  153.   10 * 4,                        /* %d4 */
  154.   11 * 4,                        /* %d5 */
  155.   12 * 4,                        /* %d6 */
  156.   13 * 4,                        /* %d7 */
  157.   14 * 4,                        /* %a0 */
  158.   15 * 4,                        /* %a1 */
  159.   16 * 4,                        /* %a2 */
  160.   17 * 4,                        /* %a3 */
  161.   18 * 4,                        /* %a4 */
  162.   19 * 4,                        /* %a5 */
  163.   20 * 4,                        /* %fp */
  164.   21 * 4,                        /* %sp */
  165.   23 * 4,                        /* %sr */
  166.   22 * 4,                        /* %pc */
  167.   27 * 4,                        /* %fp0 */
  168.   30 * 4,                        /* %fp1 */
  169.   33 * 4,                        /* %fp2 */
  170.   36 * 4,                        /* %fp3 */
  171.   39 * 4,                        /* %fp4 */
  172.   42 * 4,                        /* %fp5 */
  173.   45 * 4,                        /* %fp6 */
  174.   48 * 4,                        /* %fp7 */
  175.   24 * 4,                        /* %fpcr */
  176.   25 * 4,                        /* %fpsr */
  177.   26 * 4                        /* %fpiaddr */
  178. };


  179. /* Get info about saved registers in sigtramp.  */

  180. struct m68k_linux_sigtramp_info
  181. {
  182.   /* Address of sigcontext.  */
  183.   CORE_ADDR sigcontext_addr;

  184.   /* Offset of registers in `struct sigcontext'.  */
  185.   int *sc_reg_offset;
  186. };

  187. /* Nonzero if running on uClinux.  */
  188. static int target_is_uclinux;

  189. static void
  190. m68k_linux_inferior_created (struct target_ops *objfile, int from_tty)
  191. {
  192.   /* Record that we will need to re-evaluate whether we are running on a
  193.      uClinux or normal GNU/Linux target (see m68k_linux_get_sigtramp_info).  */
  194.   target_is_uclinux = -1;
  195. }

  196. static struct m68k_linux_sigtramp_info
  197. m68k_linux_get_sigtramp_info (struct frame_info *this_frame)
  198. {
  199.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  200.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  201.   CORE_ADDR sp;
  202.   struct m68k_linux_sigtramp_info info;

  203.   /* Determine whether we are running on a uClinux or normal GNU/Linux
  204.      target so we can use the correct sigcontext layouts.  */
  205.   if (target_is_uclinux == -1)
  206.     target_is_uclinux = linux_is_uclinux ();

  207.   sp = get_frame_register_unsigned (this_frame, M68K_SP_REGNUM);

  208.   /* Get sigcontext address, it is the third parameter on the stack.  */
  209.   info.sigcontext_addr = read_memory_unsigned_integer (sp + 8, 4, byte_order);

  210.   if (m68k_linux_pc_in_sigtramp (this_frame) == 2)
  211.     info.sc_reg_offset = m68k_linux_ucontext_reg_offset;
  212.   else
  213.     info.sc_reg_offset = (target_is_uclinux
  214.                           ? m68k_uclinux_sigcontext_reg_offset
  215.                           : m68k_linux_sigcontext_reg_offset);
  216.   return info;
  217. }

  218. /* Signal trampolines.  */

  219. static struct trad_frame_cache *
  220. m68k_linux_sigtramp_frame_cache (struct frame_info *this_frame,
  221.                                  void **this_cache)
  222. {
  223.   struct frame_id this_id;
  224.   struct trad_frame_cache *cache;
  225.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  226.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  227.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  228.   struct m68k_linux_sigtramp_info info;
  229.   gdb_byte buf[4];
  230.   int i;

  231.   if (*this_cache)
  232.     return *this_cache;

  233.   cache = trad_frame_cache_zalloc (this_frame);

  234.   /* FIXME: cagney/2004-05-01: This is is long standing broken code.
  235.      The frame ID's code address should be the start-address of the
  236.      signal trampoline and not the current PC within that
  237.      trampoline.  */
  238.   get_frame_register (this_frame, M68K_SP_REGNUM, buf);
  239.   /* See the end of m68k_push_dummy_call.  */
  240.   this_id = frame_id_build (extract_unsigned_integer (buf, 4, byte_order)
  241.                             - 4 + 8, get_frame_pc (this_frame));
  242.   trad_frame_set_id (cache, this_id);

  243.   info = m68k_linux_get_sigtramp_info (this_frame);

  244.   for (i = 0; i < M68K_NUM_REGS; i++)
  245.     if (info.sc_reg_offset[i] != -1)
  246.       trad_frame_set_reg_addr (cache, i,
  247.                                info.sigcontext_addr + info.sc_reg_offset[i]);

  248.   *this_cache = cache;
  249.   return cache;
  250. }

  251. static void
  252. m68k_linux_sigtramp_frame_this_id (struct frame_info *this_frame,
  253.                                    void **this_cache,
  254.                                    struct frame_id *this_id)
  255. {
  256.   struct trad_frame_cache *cache =
  257.     m68k_linux_sigtramp_frame_cache (this_frame, this_cache);
  258.   trad_frame_get_id (cache, this_id);
  259. }

  260. static struct value *
  261. m68k_linux_sigtramp_frame_prev_register (struct frame_info *this_frame,
  262.                                          void **this_cache,
  263.                                          int regnum)
  264. {
  265.   /* Make sure we've initialized the cache.  */
  266.   struct trad_frame_cache *cache =
  267.     m68k_linux_sigtramp_frame_cache (this_frame, this_cache);
  268.   return trad_frame_get_register (cache, this_frame, regnum);
  269. }

  270. static int
  271. m68k_linux_sigtramp_frame_sniffer (const struct frame_unwind *self,
  272.                                    struct frame_info *this_frame,
  273.                                    void **this_prologue_cache)
  274. {
  275.   return m68k_linux_pc_in_sigtramp (this_frame);
  276. }

  277. static const struct frame_unwind m68k_linux_sigtramp_frame_unwind =
  278. {
  279.   SIGTRAMP_FRAME,
  280.   default_frame_unwind_stop_reason,
  281.   m68k_linux_sigtramp_frame_this_id,
  282.   m68k_linux_sigtramp_frame_prev_register,
  283.   NULL,
  284.   m68k_linux_sigtramp_frame_sniffer
  285. };

  286. /* Register maps for supply/collect regset functions.  */

  287. static const struct regcache_map_entry m68k_linux_gregmap[] =
  288.   {
  289.     { 7, M68K_D1_REGNUM, 4 },        /* d1 ... d7 */
  290.     { 7, M68K_A0_REGNUM, 4 },        /* a0 ... a6 */
  291.     { 1, M68K_D0_REGNUM, 4 },
  292.     { 1, M68K_SP_REGNUM, 4 },
  293.     { 1, REGCACHE_MAP_SKIP, 4 }, /* orig_d0 (skip) */
  294.     { 1, M68K_PS_REGNUM, 4 },
  295.     { 1, M68K_PC_REGNUM, 4 },
  296.     /* Ignore 16-bit fields 'fmtvec' and '__fill'.  */
  297.     { 0 }
  298.   };

  299. #define M68K_LINUX_GREGS_SIZE (20 * 4)

  300. static const struct regcache_map_entry m68k_linux_fpregmap[] =
  301.   {
  302.     { 8, M68K_FP0_REGNUM, 12 },        /* fp0 ... fp7 */
  303.     { 1, M68K_FPC_REGNUM, 4 },
  304.     { 1, M68K_FPS_REGNUM, 4 },
  305.     { 1, M68K_FPI_REGNUM, 4 },
  306.     { 0 }
  307.   };

  308. #define M68K_LINUX_FPREGS_SIZE (27 * 4)

  309. /* Register sets. */

  310. static const struct regset m68k_linux_gregset =
  311.   {
  312.     m68k_linux_gregmap,
  313.     regcache_supply_regset, regcache_collect_regset
  314.   };

  315. static const struct regset m68k_linux_fpregset =
  316.   {
  317.     m68k_linux_fpregmap,
  318.     regcache_supply_regset, regcache_collect_regset
  319.   };

  320. /* Iterate over core file register note sections.  */

  321. static void
  322. m68k_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
  323.                                          iterate_over_regset_sections_cb *cb,
  324.                                          void *cb_data,
  325.                                          const struct regcache *regcache)
  326. {
  327.   cb (".reg", M68K_LINUX_GREGS_SIZE, &m68k_linux_gregset, NULL, cb_data);
  328.   cb (".reg2", M68K_LINUX_FPREGS_SIZE, &m68k_linux_fpregset, NULL, cb_data);
  329. }

  330. static void
  331. m68k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  332. {
  333.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  334.   linux_init_abi (info, gdbarch);

  335.   tdep->jb_pc = M68K_LINUX_JB_PC;
  336.   tdep->jb_elt_size = M68K_LINUX_JB_ELEMENT_SIZE;

  337.   /* GNU/Linux uses a calling convention that's similar to SVR4.  It
  338.      returns integer values in %d0/%d1, pointer values in %a0 and
  339.      floating values in %fp0, just like SVR4, but uses %a1 to pass the
  340.      address to store a structure value.  It also returns small
  341.      structures in registers instead of memory.  */
  342.   m68k_svr4_init_abi (info, gdbarch);
  343.   tdep->struct_value_regnum = M68K_A1_REGNUM;
  344.   tdep->struct_return = reg_struct_return;

  345.   set_gdbarch_decr_pc_after_break (gdbarch, 2);

  346.   frame_unwind_append_unwinder (gdbarch, &m68k_linux_sigtramp_frame_unwind);

  347.   /* Shared library handling.  */

  348.   /* GNU/Linux uses SVR4-style shared libraries.  */
  349.   set_solib_svr4_fetch_link_map_offsets (gdbarch,
  350.                                          svr4_ilp32_fetch_link_map_offsets);

  351.   /* GNU/Linux uses the dynamic linker included in the GNU C Library.  */
  352.   set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);

  353.   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);

  354.   /* Core file support. */
  355.   set_gdbarch_iterate_over_regset_sections
  356.     (gdbarch, m68k_linux_iterate_over_regset_sections);

  357.   /* Enable TLS support.  */
  358.   set_gdbarch_fetch_tls_load_module_address (gdbarch,
  359.                                              svr4_fetch_objfile_link_map);

  360.   set_gdbarch_get_siginfo_type (gdbarch, linux_get_siginfo_type);
  361. }

  362. /* Provide a prototype to silence -Wmissing-prototypes.  */
  363. extern initialize_file_ftype _initialize_m68k_linux_tdep;

  364. void
  365. _initialize_m68k_linux_tdep (void)
  366. {
  367.   gdbarch_register_osabi (bfd_arch_m68k, 0, GDB_OSABI_LINUX,
  368.                           m68k_linux_init_abi);
  369.   observer_attach_inferior_created (m68k_linux_inferior_created);
  370. }