Include gdb_assert.h in common-defs.h
[external/binutils.git] / gdb / nios2-linux-tdep.c
1 /* Target-dependent code for GNU/Linux on Nios II.
2    Copyright (C) 2012-2014 Free Software Foundation, Inc.
3    Contributed by Mentor Graphics, 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 "frame.h"
22 #include <string.h>
23 #include "osabi.h"
24 #include "solib-svr4.h"
25 #include "trad-frame.h"
26 #include "tramp-frame.h"
27 #include "symtab.h"
28 #include "regset.h"
29 #include "regcache.h"
30 #include "linux-tdep.h"
31 #include "glibc-tdep.h"
32 #include "nios2-tdep.h"
33
34 #include "features/nios2-linux.c"
35
36 /* Core file and register set support.  */
37
38 /* Map from the normal register enumeration order to the order that
39    registers appear in core files, which corresponds to the order
40    of the register slots in the kernel's struct pt_regs.  */
41
42 static const int reg_offsets[NIOS2_NUM_REGS] =
43 {
44   -1,  8,  9, 10, 11, 12, 13, 14,       /* r0 - r7 */
45   0,  1,  2,  3,  4,  5,  6,  7,        /* r8 - r15 */
46   23, 24, 25, 26, 27, 28, 29, 30,       /* r16 - r23 */
47   -1, -1, 19, 18, 17, 21, -1, 16,       /* et bt gp sp fp ea sstatus ra */
48   21,                                   /* pc */
49   -1, 20, -1, -1, -1, -1, -1, -1,       /* status estatus ...  */
50   -1, -1, -1, -1, -1, -1, -1, -1
51 };
52
53 /* Implement the supply_regset hook for core files.  */
54
55 static void
56 nios2_supply_gregset (const struct regset *regset,
57                       struct regcache *regcache,
58                       int regnum, const void *gregs_buf, size_t len)
59 {
60   const gdb_byte *gregs = gregs_buf;
61   int regno;
62   static const gdb_byte zero_buf[4] = {0, 0, 0, 0};
63
64   for (regno = NIOS2_Z_REGNUM; regno <= NIOS2_MPUACC_REGNUM; regno++)
65     if (regnum == -1 || regnum == regno)
66       {
67         if (reg_offsets[regno] != -1)
68           regcache_raw_supply (regcache, regno,
69                                gregs + 4 * reg_offsets[regno]);
70         else
71           regcache_raw_supply (regcache, regno, zero_buf);
72       }
73 }
74
75 static const struct regset nios2_core_regset =
76 {
77   NULL,
78   nios2_supply_gregset,
79   NULL,
80 };
81
82 /* Implement the regset_from_core_section gdbarch method.  */
83
84 static const struct regset *
85 nios2_regset_from_core_section (struct gdbarch *gdbarch,
86                                 const char *sect_name, size_t sect_size)
87 {
88   if (strcmp (sect_name, ".reg") == 0)
89     return &nios2_core_regset;
90
91   return NULL;
92 }
93
94 /* Initialize a trad-frame cache corresponding to the tramp-frame.
95    FUNC is the address of the instruction TRAMP[0] in memory.  */
96
97 static void
98 nios2_linux_rt_sigreturn_init (const struct tramp_frame *self,
99                                struct frame_info *next_frame,
100                                struct trad_frame_cache *this_cache,
101                                CORE_ADDR func)
102 {
103   CORE_ADDR base = func + 41 * 4;
104   int i;
105
106   for (i = 0; i < 23; i++)
107     trad_frame_set_reg_addr (this_cache, i + 1, base + i * 4);
108   trad_frame_set_reg_addr (this_cache, NIOS2_RA_REGNUM, base + 23 * 4);
109   trad_frame_set_reg_addr (this_cache, NIOS2_FP_REGNUM, base + 24 * 4);
110   trad_frame_set_reg_addr (this_cache, NIOS2_GP_REGNUM, base + 25 * 4);
111   trad_frame_set_reg_addr (this_cache, NIOS2_PC_REGNUM, base + 27 * 4);
112   trad_frame_set_reg_addr (this_cache, NIOS2_SP_REGNUM, base + 28 * 4);
113
114   /* Save a frame ID.  */
115   trad_frame_set_id (this_cache, frame_id_build (base, func));
116 }
117
118 static struct tramp_frame nios2_linux_rt_sigreturn_tramp_frame =
119 {
120   SIGTRAMP_FRAME,
121   4,
122   {
123     { 0x00800004 | (139 << 6), -1 },  /* movi r2,__NR_rt_sigreturn */
124     { 0x003b683a, -1 },               /* trap */
125     { TRAMP_SENTINEL_INSN }
126   },
127   nios2_linux_rt_sigreturn_init
128 };
129
130 /* When FRAME is at a syscall instruction, return the PC of the next
131    instruction to be executed.  */
132
133 static CORE_ADDR
134 nios2_linux_syscall_next_pc (struct frame_info *frame)
135 {
136   CORE_ADDR pc = get_frame_pc (frame);
137   ULONGEST syscall_nr = get_frame_register_unsigned (frame, NIOS2_R2_REGNUM);
138
139   /* If we are about to make a sigreturn syscall, use the unwinder to
140      decode the signal frame.  */
141   if (syscall_nr == 139 /* rt_sigreturn */)
142     return frame_unwind_caller_pc (frame);
143
144   return pc + NIOS2_OPCODE_SIZE;
145 }
146
147 /* Hook function for gdbarch_register_osabi.  */
148
149 static void
150 nios2_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
151 {
152   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
153
154   linux_init_abi (info, gdbarch);
155
156   /* Shared library handling.  */
157   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
158   set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
159
160   set_solib_svr4_fetch_link_map_offsets (gdbarch,
161                                          svr4_ilp32_fetch_link_map_offsets);
162   /* Enable TLS support.  */
163   set_gdbarch_fetch_tls_load_module_address (gdbarch,
164                                              svr4_fetch_objfile_link_map);
165   /* Core file support.  */
166   set_gdbarch_regset_from_core_section (gdbarch,
167                                         nios2_regset_from_core_section);
168   /* Linux signal frame unwinders.  */
169   tramp_frame_prepend_unwinder (gdbarch,
170                                 &nios2_linux_rt_sigreturn_tramp_frame);
171
172   tdep->syscall_next_pc = nios2_linux_syscall_next_pc;
173
174   /* Index of target address word in glibc jmp_buf.  */
175   tdep->jb_pc = 10;
176 }
177
178 /* Provide a prototype to silence -Wmissing-prototypes.  */
179
180 extern initialize_file_ftype _initialize_nios2_linux_tdep;
181
182 void
183 _initialize_nios2_linux_tdep (void)
184 {
185   gdbarch_register_osabi (bfd_arch_nios2, 0, GDB_OSABI_LINUX,
186                           nios2_linux_init_abi);
187
188   initialize_tdesc_nios2_linux ();
189 }