Various gdb_byte related changes for FRV targets.
[platform/upstream/binutils.git] / gdb / frv-linux-tdep.c
1 /* Target-dependent code for GNU/Linux running on the Fujitsu FR-V,
2    for GDB.
3    Copyright 2004 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 2 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, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "target.h"
24 #include "frame.h"
25 #include "osabi.h"
26 #include "elf-bfd.h"
27 #include "elf/frv.h"
28 #include "frv-tdep.h"
29 #include "trad-frame.h"
30 #include "frame-unwind.h"
31
32 /* Define the size (in bytes) of an FR-V instruction.  */
33 static const int frv_instr_size = 4;
34
35 enum {
36   NORMAL_SIGTRAMP = 1,
37   RT_SIGTRAMP = 2
38 };
39
40 static int
41 frv_linux_pc_in_sigtramp (CORE_ADDR pc, char *name)
42 {
43   char buf[frv_instr_size];
44   LONGEST instr;
45   int retval = 0;
46
47   if (target_read_memory (pc, buf, sizeof buf) != 0)
48     return 0;
49
50   instr = extract_unsigned_integer (buf, sizeof buf);
51
52   if (instr == 0x8efc0077)      /* setlos #__NR_sigreturn, gr7 */
53     retval = NORMAL_SIGTRAMP;
54   else if (instr -= 0x8efc00ad) /* setlos #__NR_rt_sigreturn, gr7 */
55     retval = RT_SIGTRAMP;
56   else
57     return 0;
58
59   if (target_read_memory (pc + frv_instr_size, buf, sizeof buf) != 0)
60     return 0;
61   instr = extract_unsigned_integer (buf, sizeof buf);
62   if (instr != 0xc0700000)      /* tira gr0, 0 */
63     return 0;
64
65   /* If we get this far, we'll return a non-zero value, either
66      NORMAL_SIGTRAMP (1) or RT_SIGTRAMP (2).  */
67   return retval;
68 }
69
70 /* Given NEXT_FRAME, the "callee" frame of the sigtramp frame that we
71    wish to decode, and REGNO, one of the frv register numbers defined
72    in frv-tdep.h, return the address of the saved register (corresponding
73    to REGNO) in the sigtramp frame.  Return -1 if the register is not
74    found in the sigtramp frame.  The magic numbers in the code below
75    were computed by examining the following kernel structs:
76
77    From arch/frv/kernel/signal.c:
78
79       struct sigframe
80       {
81               void (*pretcode)(void);
82               int sig;
83               struct sigcontext sc;
84               unsigned long extramask[_NSIG_WORDS-1];
85               uint32_t retcode[2];
86       };
87
88       struct rt_sigframe
89       {
90               void (*pretcode)(void);
91               int sig;
92               struct siginfo *pinfo;
93               void *puc;
94               struct siginfo info;
95               struct ucontext uc;
96               uint32_t retcode[2];
97       };
98
99    From include/asm-frv/ucontext.h:
100
101       struct ucontext {
102               unsigned long             uc_flags;
103               struct ucontext           *uc_link;
104               stack_t                   uc_stack;
105               struct sigcontext uc_mcontext;
106               sigset_t          uc_sigmask;
107       };
108
109    From include/asm-frv/signal.h:
110
111       typedef struct sigaltstack {
112               void *ss_sp;
113               int ss_flags;
114               size_t ss_size;
115       } stack_t;
116
117    From include/asm-frv/sigcontext.h:
118
119       struct sigcontext {
120               struct user_context       sc_context;
121               unsigned long             sc_oldmask;
122       } __attribute__((aligned(8)));
123
124    From include/asm-frv/registers.h:
125       struct user_int_regs
126       {
127               unsigned long             psr;
128               unsigned long             isr;
129               unsigned long             ccr;
130               unsigned long             cccr;
131               unsigned long             lr;
132               unsigned long             lcr;
133               unsigned long             pc;
134               unsigned long             __status;
135               unsigned long             syscallno;
136               unsigned long             orig_gr8;
137               unsigned long             gner[2];
138               unsigned long long        iacc[1];
139
140               union {
141                       unsigned long     tbr;
142                       unsigned long     gr[64];
143               };
144       };
145
146       struct user_fpmedia_regs
147       {
148               unsigned long     fr[64];
149               unsigned long     fner[2];
150               unsigned long     msr[2];
151               unsigned long     acc[8];
152               unsigned char     accg[8];
153               unsigned long     fsr[1];
154       };
155
156       struct user_context
157       {
158               struct user_int_regs              i;
159               struct user_fpmedia_regs  f;
160
161               void *extension;
162       } __attribute__((aligned(8)));  */
163
164 static LONGEST
165 frv_linux_sigcontext_reg_addr (struct frame_info *next_frame, int regno,
166                                CORE_ADDR *sc_addr_cache_ptr)
167 {
168   CORE_ADDR sc_addr;
169
170   if (sc_addr_cache_ptr && *sc_addr_cache_ptr)
171     {
172       sc_addr = *sc_addr_cache_ptr;
173     }
174   else
175     {
176       CORE_ADDR pc, sp;
177       char buf[4];
178       int tramp_type;
179
180       pc = frame_pc_unwind (next_frame);
181       tramp_type = frv_linux_pc_in_sigtramp (pc, 0);
182
183       frame_unwind_register (next_frame, sp_regnum, buf);
184       sp = extract_unsigned_integer (buf, sizeof buf);
185
186       if (tramp_type == NORMAL_SIGTRAMP)
187         {
188           /* For a normal sigtramp frame, the sigcontext struct starts
189              at SP + 8.  */
190           sc_addr = sp + 8;
191         }
192       else if (tramp_type == RT_SIGTRAMP)
193         {
194           /* For a realtime sigtramp frame, SP + 12 contains a pointer
195              to a ucontext struct.  The ucontext struct contains a
196              sigcontext struct starting 24 bytes in.  (The offset of
197              uc_mcontext within struct ucontext is derived as follows: 
198              stack_t is a 12-byte struct and struct sigcontext is
199              8-byte aligned.  This gives an offset of 8 + 12 + 4 (for
200              padding) = 24.) */
201           if (target_read_memory (sp + 12, buf, sizeof buf) != 0)
202             {
203               warning (_("Can't read realtime sigtramp frame."));
204               return 0;
205             }
206           sc_addr = extract_unsigned_integer (buf, sizeof buf);
207           sc_addr += 24;
208         }
209       else
210         internal_error (__FILE__, __LINE__, _("not a signal trampoline"));
211
212       if (sc_addr_cache_ptr)
213         *sc_addr_cache_ptr = sc_addr;
214     }
215
216   switch (regno)
217     {
218     case psr_regnum :
219       return sc_addr + 0;
220     /* sc_addr + 4 has "isr", the Integer Status Register.  */
221     case ccr_regnum :
222       return sc_addr + 8;
223     case cccr_regnum :
224       return sc_addr + 12;
225     case lr_regnum :
226       return sc_addr + 16;
227     case lcr_regnum :
228       return sc_addr + 20;
229     case pc_regnum :
230       return sc_addr + 24;
231     /* sc_addr + 28 is __status, the exception status.
232        sc_addr + 32 is syscallno, the syscall number or -1.
233        sc_addr + 36 is orig_gr8, the original syscall arg #1.
234        sc_addr + 40 is gner[0].
235        sc_addr + 44 is gner[1]. */
236     case iacc0h_regnum :
237       return sc_addr + 48;
238     case iacc0l_regnum :
239       return sc_addr + 52;
240     default : 
241       if (first_gpr_regnum <= regno && regno <= last_gpr_regnum)
242         return sc_addr + 56 + 4 * (regno - first_gpr_regnum);
243       else if (first_fpr_regnum <= regno && regno <= last_fpr_regnum)
244         return sc_addr + 312 + 4 * (regno - first_fpr_regnum);
245       else
246         return -1;  /* not saved. */
247     }
248 }
249
250 /* Signal trampolines.  */
251
252 static struct trad_frame_cache *
253 frv_linux_sigtramp_frame_cache (struct frame_info *next_frame, void **this_cache)
254 {
255   struct trad_frame_cache *cache;
256   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
257   CORE_ADDR addr;
258   char buf[4];
259   int regnum;
260   CORE_ADDR sc_addr_cache_val = 0;
261   struct frame_id this_id;
262
263   if (*this_cache)
264     return *this_cache;
265
266   cache = trad_frame_cache_zalloc (next_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   frame_unwind_register (next_frame, sp_regnum, buf);
273   this_id = frame_id_build (extract_unsigned_integer (buf, sizeof buf),
274                             frame_pc_unwind (next_frame));
275   trad_frame_set_id (cache, this_id);
276
277   for (regnum = 0; regnum < frv_num_regs; regnum++)
278     {
279       LONGEST reg_addr = frv_linux_sigcontext_reg_addr (next_frame, regnum,
280                                                         &sc_addr_cache_val);
281       if (reg_addr != -1)
282         trad_frame_set_reg_addr (cache, regnum, reg_addr);
283     }
284
285   *this_cache = cache;
286   return cache;
287 }
288
289 static void
290 frv_linux_sigtramp_frame_this_id (struct frame_info *next_frame, void **this_cache,
291                              struct frame_id *this_id)
292 {
293   struct trad_frame_cache *cache =
294     frv_linux_sigtramp_frame_cache (next_frame, this_cache);
295   trad_frame_get_id (cache, this_id);
296 }
297
298 static void
299 frv_linux_sigtramp_frame_prev_register (struct frame_info *next_frame,
300                                    void **this_cache,
301                                    int regnum, int *optimizedp,
302                                    enum lval_type *lvalp, CORE_ADDR *addrp,
303                                    int *realnump, gdb_byte *valuep)
304 {
305   /* Make sure we've initialized the cache.  */
306   struct trad_frame_cache *cache =
307     frv_linux_sigtramp_frame_cache (next_frame, this_cache);
308   trad_frame_get_register (cache, next_frame, regnum, optimizedp, lvalp,
309                            addrp, realnump, valuep);
310 }
311
312 static const struct frame_unwind frv_linux_sigtramp_frame_unwind =
313 {
314   SIGTRAMP_FRAME,
315   frv_linux_sigtramp_frame_this_id,
316   frv_linux_sigtramp_frame_prev_register
317 };
318
319 static const struct frame_unwind *
320 frv_linux_sigtramp_frame_sniffer (struct frame_info *next_frame)
321 {
322   CORE_ADDR pc = frame_pc_unwind (next_frame);
323   char *name;
324
325   find_pc_partial_function (pc, &name, NULL, NULL);
326   if (frv_linux_pc_in_sigtramp (pc, name))
327     return &frv_linux_sigtramp_frame_unwind;
328
329   return NULL;
330 }
331
332 static void
333 frv_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
334 {
335   /* Set the sigtramp frame sniffer.  */
336   frame_unwind_append_sniffer (gdbarch, frv_linux_sigtramp_frame_sniffer); 
337 }
338
339 static enum gdb_osabi
340 frv_linux_elf_osabi_sniffer (bfd *abfd)
341 {
342   int elf_flags;
343
344   elf_flags = elf_elfheader (abfd)->e_flags;
345
346   /* Assume GNU/Linux if using the FDPIC ABI.  If/when another OS shows
347      up that uses this ABI, we'll need to start using .note sections
348      or some such.  */
349   if (elf_flags & EF_FRV_FDPIC)
350     return GDB_OSABI_LINUX;
351   else
352     return GDB_OSABI_UNKNOWN;
353 }
354
355 /* Provide a prototype to silence -Wmissing-prototypes.  */
356 void _initialize_frv_linux_tdep (void);
357
358 void
359 _initialize_frv_linux_tdep (void)
360 {
361   gdbarch_register_osabi (bfd_arch_frv, 0, GDB_OSABI_LINUX, frv_linux_init_abi);
362   gdbarch_register_osabi_sniffer (bfd_arch_frv,
363                                   bfd_target_elf_flavour,
364                                   frv_linux_elf_osabi_sniffer);
365 }