* nto-tdep.h: Include osabi.h. Prototypes for generic Neutrino
[platform/upstream/binutils.git] / gdb / i386-nto-tdep.c
1 /* Target-dependent code for QNX Neutrino x86.
2
3    Copyright 2003, 2004 Free Software Foundation, Inc.
4
5    Contributed by QNX Software Systems Ltd.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24 #include "gdb_string.h"
25 #include "gdb_assert.h"
26 #include "defs.h"
27 #include "frame.h"
28 #include "target.h"
29 #include "regcache.h"
30 #include "solib-svr4.h"
31 #include "i386-tdep.h"
32 #include "nto-tdep.h"
33 #include "osabi.h"
34 #include "i387-tdep.h"
35
36 #ifndef X86_CPU_FXSR
37 #define X86_CPU_FXSR (1L << 12)
38 #endif
39
40 /* Why 13?  Look in our /usr/include/x86/context.h header at the
41    x86_cpu_registers structure and you'll see an 'exx' junk register
42    that is just filler.  Don't ask me, ask the kernel guys.  */
43 #define NUM_GPREGS 13
44
45 /* Map a GDB register number to an offset in the reg structure.  */
46 static int regmap[] = {
47   (7 * 4),                      /* eax */
48   (6 * 4),                      /* ecx */
49   (5 * 4),                      /* edx */
50   (4 * 4),                      /* ebx */
51   (11 * 4),                     /* esp */
52   (2 * 4),                      /* epb */
53   (1 * 4),                      /* esi */
54   (0 * 4),                      /* edi */
55   (8 * 4),                      /* eip */
56   (10 * 4),                     /* eflags */
57   (9 * 4),                      /* cs */
58   (12 * 4),                     /* ss */
59   (-1 * 4)                      /* filler */
60 };
61
62 static struct nto_target_ops i386_nto_target;
63
64 /* Given a gdb regno, return the offset into Neutrino's register structure
65    or -1 if register is unknown.  */
66 static int
67 nto_reg_offset (int regno)
68 {
69   return (regno >= 0 && regno < NUM_GPREGS) ? regmap[regno] : -1;
70 }
71
72 static void
73 i386nto_supply_gregset (char *gpregs)
74 {
75   unsigned regno;
76   int empty = 0;
77
78   for (regno = 0; regno < I386_NUM_GREGS; regno++)
79     {
80       int offset = nto_reg_offset (regno);
81       if (offset == -1)
82         regcache_raw_supply (current_regcache, regno, (char *) &empty);
83       else
84         regcache_raw_supply (current_regcache, regno, gpregs + offset);
85     }
86 }
87
88 static void
89 i386nto_supply_fpregset (char *fpregs)
90 {
91   if (nto_cpuinfo_valid && nto_cpuinfo_flags | X86_CPU_FXSR)
92     i387_supply_fxsave (current_regcache, -1, fpregs);
93   else
94     i387_supply_fsave (current_regcache, -1, fpregs);
95 }
96
97 static void
98 i386nto_supply_regset (int regset, char *data)
99 {
100   switch (regset)
101     {
102     case NTO_REG_GENERAL:       /* QNX has different ordering of GP regs than GDB.  */
103       i386nto_supply_gregset (data);
104       break;
105     case NTO_REG_FLOAT:
106       i386nto_supply_fpregset (data);
107       break;
108     }
109 }
110
111 static int
112 i386nto_regset_id (int regno)
113 {
114   if (regno == -1)
115     return NTO_REG_END;
116   else if (regno < I386_NUM_GREGS)
117     return NTO_REG_GENERAL;
118   else if (regno < I386_NUM_GREGS + I386_NUM_FREGS)
119     return NTO_REG_FLOAT;
120
121   return -1;                    /* Error.  */
122 }
123
124 static int
125 i386nto_register_area (int regno, int regset, unsigned *off)
126 {
127   int len;
128
129   *off = 0;
130   if (regset == NTO_REG_GENERAL)
131     {
132       if (regno == -1)
133         return NUM_GPREGS * 4;
134
135       *off = nto_reg_offset (regno);
136       if (*off == -1)
137         return 0;
138       return 4;
139     }
140   else if (regset == NTO_REG_FLOAT)
141     {
142       unsigned off_adjust, regsize, regset_size;
143
144       if (nto_cpuinfo_valid && nto_cpuinfo_flags | X86_CPU_FXSR)
145         {
146           off_adjust = 32;
147           regsize = 16;
148           regset_size = 512;
149         }
150       else
151         {
152           off_adjust = 28;
153           regsize = 10;
154           regset_size = 128;
155         }
156
157       if (regno == -1)
158         return regset_size;
159
160       *off = (regno - FP0_REGNUM) * regsize + off_adjust;
161       return 10;
162       /* Why 10 instead of regsize?  GDB only stores 10 bytes per FP
163          register so if we're sending a register back to the target,
164          we only want pdebug to write 10 bytes so as not to clobber
165          the reserved 6 bytes in the fxsave structure.  */
166     }
167   return -1;
168 }
169
170 static int
171 i386nto_regset_fill (int regset, char *data)
172 {
173   if (regset == NTO_REG_GENERAL)
174     {
175       int regno;
176
177       for (regno = 0; regno < NUM_GPREGS; regno++)
178         {
179           int offset = nto_reg_offset (regno);
180           if (offset != -1)
181             regcache_raw_collect (current_regcache, regno, data + offset);
182         }
183     }
184   else if (regset == NTO_REG_FLOAT)
185     {
186       if (nto_cpuinfo_valid && nto_cpuinfo_flags | X86_CPU_FXSR)
187         i387_fill_fxsave (data, -1);
188       else
189         i387_fill_fsave (data, -1);
190     }
191   else
192     return -1;
193
194   return 0;
195 }
196
197 static struct link_map_offsets *
198 i386nto_svr4_fetch_link_map_offsets (void)
199 {
200   static struct link_map_offsets lmo;
201   static struct link_map_offsets *lmp = NULL;
202
203   if (lmp == NULL)
204     {
205       lmp = &lmo;
206
207       lmo.r_debug_size = 8;     /* The actual size is 20 bytes, but
208                                    only 8 bytes are used.  */
209       lmo.r_map_offset = 4;
210       lmo.r_map_size = 4;
211
212       lmo.link_map_size = 20;   /* The actual size is 552 bytes, but
213                                    only 20 bytes are used.  */
214       lmo.l_addr_offset = 0;
215       lmo.l_addr_size = 4;
216
217       lmo.l_name_offset = 4;
218       lmo.l_name_size = 4;
219
220       lmo.l_next_offset = 12;
221       lmo.l_next_size = 4;
222
223       lmo.l_prev_offset = 16;
224       lmo.l_prev_size = 4;
225     }
226
227   return lmp;
228 }
229
230 /* Return whether the frame preceding NEXT_FRAME corresponds to a QNX
231    Neutrino sigtramp routine.  */
232
233 static int
234 i386nto_sigtramp_p (struct frame_info *next_frame)
235 {
236   CORE_ADDR pc = frame_pc_unwind (next_frame);
237   char *name;
238
239   find_pc_partial_function (pc, &name, NULL, NULL);
240   return name && strcmp ("__signalstub", name) == 0;
241 }
242
243 #define I386_NTO_SIGCONTEXT_OFFSET 136
244
245 /* Assuming NEXT_FRAME is a frame following a QNX Neutrino sigtramp
246    routine, return the address of the associated sigcontext structure.  */
247
248 static CORE_ADDR
249 i386nto_sigcontext_addr (struct frame_info *next_frame)
250 {
251   char buf[4];
252   CORE_ADDR sp;
253
254   frame_unwind_register (next_frame, I386_ESP_REGNUM, buf);
255   sp = extract_unsigned_integer (buf, 4);
256
257   return sp + I386_NTO_SIGCONTEXT_OFFSET;
258 }
259
260 static void
261 init_i386nto_ops (void)
262 {
263   i386_nto_target.regset_id = i386nto_regset_id;
264   i386_nto_target.supply_gregset = i386nto_supply_gregset;
265   i386_nto_target.supply_fpregset = i386nto_supply_fpregset;
266   i386_nto_target.supply_altregset = nto_dummy_supply_regset;
267   i386_nto_target.supply_regset = i386nto_supply_regset;
268   i386_nto_target.register_area = i386nto_register_area;
269   i386_nto_target.regset_fill = i386nto_regset_fill;
270   i386_nto_target.fetch_link_map_offsets =
271     i386nto_svr4_fetch_link_map_offsets;
272 }
273
274 static void
275 i386nto_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
276 {
277   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
278
279   /* Deal with our strange signals.  */
280   nto_initialize_signals ();
281
282   /* NTO uses ELF.  */
283   i386_elf_init_abi (info, gdbarch);
284
285   /* Neutrino rewinds to look more normal.  Need to override the i386
286      default which is [unfortunately] to decrement the PC.  */
287   set_gdbarch_decr_pc_after_break (gdbarch, 0);
288
289   /* NTO has shared libraries.  */
290   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
291
292   tdep->sigtramp_p = i386nto_sigtramp_p;
293   tdep->sigcontext_addr = i386nto_sigcontext_addr;
294   tdep->sc_pc_offset = 56;
295   tdep->sc_sp_offset = 68;
296
297   /* Setjmp()'s return PC saved in EDX (5).  */
298   tdep->jb_pc_offset = 20;      /* 5x32 bit ints in.  */
299
300   set_solib_svr4_fetch_link_map_offsets (gdbarch,
301                                          i386nto_svr4_fetch_link_map_offsets);
302
303   /* Our loader handles solib relocations slightly differently than svr4.  */
304   TARGET_SO_RELOCATE_SECTION_ADDRESSES = nto_relocate_section_addresses;
305
306   /* Supply a nice function to find our solibs.  */
307   TARGET_SO_FIND_AND_OPEN_SOLIB = nto_find_and_open_solib;
308
309   /* Our linker code is in libc.  */
310   TARGET_SO_IN_DYNSYM_RESOLVE_CODE = nto_in_dynsym_resolve_code;
311
312   nto_set_target (&i386_nto_target);
313 }
314
315 void
316 _initialize_i386nto_tdep (void)
317 {
318   init_i386nto_ops ();
319   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_QNXNTO,
320                           i386nto_init_abi);
321   gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_elf_flavour,
322                                   nto_elf_osabi_sniffer);
323 }