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