Add NaCl (NativeClient) specific classes Target_mips_nacl and
[external/binutils.git] / gdb / i386obsd-tdep.c
1 /* Target-dependent code for OpenBSD/i386.
2
3    Copyright (C) 1988-2014 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 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 "arch-utils.h"
22 #include "frame.h"
23 #include "frame-unwind.h"
24 #include "gdbcore.h"
25 #include "regcache.h"
26 #include "regset.h"
27 #include "symtab.h"
28 #include "objfiles.h"
29 #include "osabi.h"
30 #include "target.h"
31 #include "trad-frame.h"
32
33 #include "obsd-tdep.h"
34 #include "i386-tdep.h"
35 #include "i387-tdep.h"
36 #include "solib-svr4.h"
37 #include "bsd-uthread.h"
38
39 /* Support for signal handlers.  */
40
41 /* Since OpenBSD 3.2, the sigtramp routine is mapped at a random page
42    in virtual memory.  The randomness makes it somewhat tricky to
43    detect it, but fortunately we can rely on the fact that the start
44    of the sigtramp routine is page-aligned.  We recognize the
45    trampoline by looking for the code that invokes the sigreturn
46    system call.  The offset where we can find that code varies from
47    release to release.
48
49    By the way, the mapping mentioned above is read-only, so you cannot
50    place a breakpoint in the signal trampoline.  */
51
52 /* Default page size.  */
53 static const int i386obsd_page_size = 4096;
54
55 /* Offset for sigreturn(2).  */
56 static const int i386obsd_sigreturn_offset[] = {
57   0x0a,                         /* OpenBSD 3.2 */
58   0x14,                         /* OpenBSD 3.6 */
59   0x3a,                         /* OpenBSD 3.8 */
60   -1
61 };
62
63 /* Return whether THIS_FRAME corresponds to an OpenBSD sigtramp
64    routine.  */
65
66 static int
67 i386obsd_sigtramp_p (struct frame_info *this_frame)
68 {
69   CORE_ADDR pc = get_frame_pc (this_frame);
70   CORE_ADDR start_pc = (pc & ~(i386obsd_page_size - 1));
71   /* The call sequence invoking sigreturn(2).  */
72   const gdb_byte sigreturn[] =
73   {
74     0xb8,
75     0x67, 0x00, 0x00, 0x00,     /* movl $SYS_sigreturn, %eax */
76     0xcd, 0x80                  /* int $0x80 */
77   };
78   size_t buflen = sizeof sigreturn;
79   const int *offset;
80   gdb_byte *buf;
81   const char *name;
82
83   /* If the function has a valid symbol name, it isn't a
84      trampoline.  */
85   find_pc_partial_function (pc, &name, NULL, NULL);
86   if (name != NULL)
87     return 0;
88
89   /* If the function lives in a valid section (even without a starting
90      point) it isn't a trampoline.  */
91   if (find_pc_section (pc) != NULL)
92     return 0;
93
94   /* Allocate buffer.  */
95   buf = alloca (buflen);
96
97   /* Loop over all offsets.  */
98   for (offset = i386obsd_sigreturn_offset; *offset != -1; offset++)
99     {
100       /* If we can't read the instructions, return zero.  */
101       if (!safe_frame_unwind_memory (this_frame, start_pc + *offset,
102                                      buf, buflen))
103         return 0;
104
105       /* Check for sigreturn(2).  */
106       if (memcmp (buf, sigreturn, buflen) == 0)
107         return 1;
108     }
109
110   return 0;
111 }
112 \f
113 /* Mapping between the general-purpose registers in `struct reg'
114    format and GDB's register cache layout.  */
115
116 /* From <machine/reg.h>.  */
117 static int i386obsd_r_reg_offset[] =
118 {
119   0 * 4,                        /* %eax */
120   1 * 4,                        /* %ecx */
121   2 * 4,                        /* %edx */
122   3 * 4,                        /* %ebx */
123   4 * 4,                        /* %esp */
124   5 * 4,                        /* %ebp */
125   6 * 4,                        /* %esi */
126   7 * 4,                        /* %edi */
127   8 * 4,                        /* %eip */
128   9 * 4,                        /* %eflags */
129   10 * 4,                       /* %cs */
130   11 * 4,                       /* %ss */
131   12 * 4,                       /* %ds */
132   13 * 4,                       /* %es */
133   14 * 4,                       /* %fs */
134   15 * 4                        /* %gs */
135 };
136
137 static void
138 i386obsd_aout_supply_regset (const struct regset *regset,
139                              struct regcache *regcache, int regnum,
140                              const void *regs, size_t len)
141 {
142   struct gdbarch *gdbarch = get_regcache_arch (regcache);
143   const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
144   const gdb_byte *gregs = regs;
145
146   gdb_assert (len >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE);
147
148   i386_supply_gregset (regset, regcache, regnum, regs, tdep->sizeof_gregset);
149   i387_supply_fsave (regcache, regnum, gregs + tdep->sizeof_gregset);
150 }
151
152 static const struct regset i386obsd_aout_gregset =
153   {
154     NULL, i386obsd_aout_supply_regset, NULL
155   };
156
157 static const struct regset *
158 i386obsd_aout_regset_from_core_section (struct gdbarch *gdbarch,
159                                         const char *sect_name,
160                                         size_t sect_size)
161 {
162   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
163
164   /* OpenBSD a.out core dumps don't use seperate register sets for the
165      general-purpose and floating-point registers.  */
166
167   if (strcmp (sect_name, ".reg") == 0
168       && sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE)
169     return &i386obsd_aout_gregset;
170
171   return NULL;
172 }
173 \f
174
175 /* Sigtramp routine location for OpenBSD 3.1 and earlier releases.  */
176 CORE_ADDR i386obsd_sigtramp_start_addr = 0xbfbfdf20;
177 CORE_ADDR i386obsd_sigtramp_end_addr = 0xbfbfdff0;
178
179 /* From <machine/signal.h>.  */
180 int i386obsd_sc_reg_offset[I386_NUM_GREGS] =
181 {
182   10 * 4,                       /* %eax */
183   9 * 4,                        /* %ecx */
184   8 * 4,                        /* %edx */
185   7 * 4,                        /* %ebx */
186   14 * 4,                       /* %esp */
187   6 * 4,                        /* %ebp */
188   5 * 4,                        /* %esi */
189   4 * 4,                        /* %edi */
190   11 * 4,                       /* %eip */
191   13 * 4,                       /* %eflags */
192   12 * 4,                       /* %cs */
193   15 * 4,                       /* %ss */
194   3 * 4,                        /* %ds */
195   2 * 4,                        /* %es */
196   1 * 4,                        /* %fs */
197   0 * 4                         /* %gs */
198 };
199
200 /* From /usr/src/lib/libpthread/arch/i386/uthread_machdep.c.  */
201 static int i386obsd_uthread_reg_offset[] =
202 {
203   11 * 4,                       /* %eax */
204   10 * 4,                       /* %ecx */
205   9 * 4,                        /* %edx */
206   8 * 4,                        /* %ebx */
207   -1,                           /* %esp */
208   6 * 4,                        /* %ebp */
209   5 * 4,                        /* %esi */
210   4 * 4,                        /* %edi */
211   12 * 4,                       /* %eip */
212   -1,                           /* %eflags */
213   13 * 4,                       /* %cs */
214   -1,                           /* %ss */
215   3 * 4,                        /* %ds */
216   2 * 4,                        /* %es */
217   1 * 4,                        /* %fs */
218   0 * 4                         /* %gs */
219 };
220
221 /* Offset within the thread structure where we can find the saved
222    stack pointer (%esp).  */
223 #define I386OBSD_UTHREAD_ESP_OFFSET     176
224
225 static void
226 i386obsd_supply_uthread (struct regcache *regcache,
227                          int regnum, CORE_ADDR addr)
228 {
229   struct gdbarch *gdbarch = get_regcache_arch (regcache);
230   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
231   CORE_ADDR sp_addr = addr + I386OBSD_UTHREAD_ESP_OFFSET;
232   CORE_ADDR sp = 0;
233   gdb_byte buf[4];
234   int i;
235
236   gdb_assert (regnum >= -1);
237
238   if (regnum == -1 || regnum == I386_ESP_REGNUM)
239     {
240       int offset;
241
242       /* Fetch stack pointer from thread structure.  */
243       sp = read_memory_unsigned_integer (sp_addr, 4, byte_order);
244
245       /* Adjust the stack pointer such that it looks as if we just
246          returned from _thread_machdep_switch.  */
247       offset = i386obsd_uthread_reg_offset[I386_EIP_REGNUM] + 4;
248       store_unsigned_integer (buf, 4, byte_order, sp + offset);
249       regcache_raw_supply (regcache, I386_ESP_REGNUM, buf);
250     }
251
252   for (i = 0; i < ARRAY_SIZE (i386obsd_uthread_reg_offset); i++)
253     {
254       if (i386obsd_uthread_reg_offset[i] != -1
255           && (regnum == -1 || regnum == i))
256         {
257           /* Fetch stack pointer from thread structure (if we didn't
258              do so already).  */
259           if (sp == 0)
260             sp = read_memory_unsigned_integer (sp_addr, 4, byte_order);
261
262           /* Read the saved register from the stack frame.  */
263           read_memory (sp + i386obsd_uthread_reg_offset[i], buf, 4);
264           regcache_raw_supply (regcache, i, buf);
265         }
266     }
267 }
268
269 static void
270 i386obsd_collect_uthread (const struct regcache *regcache,
271                           int regnum, CORE_ADDR addr)
272 {
273   struct gdbarch *gdbarch = get_regcache_arch (regcache);
274   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
275   CORE_ADDR sp_addr = addr + I386OBSD_UTHREAD_ESP_OFFSET;
276   CORE_ADDR sp = 0;
277   gdb_byte buf[4];
278   int i;
279
280   gdb_assert (regnum >= -1);
281
282   if (regnum == -1 || regnum == I386_ESP_REGNUM)
283     {
284       int offset;
285
286       /* Calculate the stack pointer (frame pointer) that will be
287          stored into the thread structure.  */
288       offset = i386obsd_uthread_reg_offset[I386_EIP_REGNUM] + 4;
289       regcache_raw_collect (regcache, I386_ESP_REGNUM, buf);
290       sp = extract_unsigned_integer (buf, 4, byte_order) - offset;
291
292       /* Store the stack pointer.  */
293       write_memory_unsigned_integer (sp_addr, 4, byte_order, sp);
294
295       /* The stack pointer was (potentially) modified.  Make sure we
296          build a proper stack frame.  */
297       regnum = -1;
298     }
299
300   for (i = 0; i < ARRAY_SIZE (i386obsd_uthread_reg_offset); i++)
301     {
302       if (i386obsd_uthread_reg_offset[i] != -1
303           && (regnum == -1 || regnum == i))
304         {
305           /* Fetch stack pointer from thread structure (if we didn't
306              calculate it already).  */
307           if (sp == 0)
308             sp = read_memory_unsigned_integer (sp_addr, 4, byte_order);
309
310           /* Write the register into the stack frame.  */
311           regcache_raw_collect (regcache, i, buf);
312           write_memory (sp + i386obsd_uthread_reg_offset[i], buf, 4);
313         }
314     }
315 }
316 \f
317 /* Kernel debugging support.  */
318
319 /* From <machine/frame.h>.  Note that %esp and %ess are only saved in
320    a trap frame when entering the kernel from user space.  */
321 static int i386obsd_tf_reg_offset[] =
322 {
323   10 * 4,                       /* %eax */
324   9 * 4,                        /* %ecx */
325   8 * 4,                        /* %edx */
326   7 * 4,                        /* %ebx */
327   -1,                           /* %esp */
328   6 * 4,                        /* %ebp */
329   5 * 4,                        /* %esi */
330   4 * 4,                        /* %edi */
331   13 * 4,                       /* %eip */
332   15 * 4,                       /* %eflags */
333   14 * 4,                       /* %cs */
334   -1,                           /* %ss */
335   3 * 4,                        /* %ds */
336   2 * 4,                        /* %es */
337   0 * 4,                        /* %fs */
338   1 * 4                         /* %gs */
339 };
340
341 static struct trad_frame_cache *
342 i386obsd_trapframe_cache (struct frame_info *this_frame, void **this_cache)
343 {
344   struct gdbarch *gdbarch = get_frame_arch (this_frame);
345   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
346   struct trad_frame_cache *cache;
347   CORE_ADDR func, sp, addr;
348   ULONGEST cs;
349   const char *name;
350   int i;
351
352   if (*this_cache)
353     return *this_cache;
354
355   cache = trad_frame_cache_zalloc (this_frame);
356   *this_cache = cache;
357
358   func = get_frame_func (this_frame);
359   sp = get_frame_register_unsigned (this_frame, I386_ESP_REGNUM);
360
361   find_pc_partial_function (func, &name, NULL, NULL);
362   if (name && strncmp (name, "Xintr", 5) == 0)
363     addr = sp + 8;              /* It's an interrupt frame.  */
364   else
365     addr = sp;
366
367   for (i = 0; i < ARRAY_SIZE (i386obsd_tf_reg_offset); i++)
368     if (i386obsd_tf_reg_offset[i] != -1)
369       trad_frame_set_reg_addr (cache, i, addr + i386obsd_tf_reg_offset[i]);
370
371   /* Read %cs from trap frame.  */
372   addr += i386obsd_tf_reg_offset[I386_CS_REGNUM];
373   cs = read_memory_unsigned_integer (addr, 4, byte_order);
374   if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
375     {
376       /* Trap from user space; terminate backtrace.  */
377       trad_frame_set_id (cache, outer_frame_id);
378     }
379   else
380     {
381       /* Construct the frame ID using the function start.  */
382       trad_frame_set_id (cache, frame_id_build (sp + 8, func));
383     }
384
385   return cache;
386 }
387
388 static void
389 i386obsd_trapframe_this_id (struct frame_info *this_frame,
390                             void **this_cache, struct frame_id *this_id)
391 {
392   struct trad_frame_cache *cache =
393     i386obsd_trapframe_cache (this_frame, this_cache);
394   
395   trad_frame_get_id (cache, this_id);
396 }
397
398 static struct value *
399 i386obsd_trapframe_prev_register (struct frame_info *this_frame,
400                                   void **this_cache, int regnum)
401 {
402   struct trad_frame_cache *cache =
403     i386obsd_trapframe_cache (this_frame, this_cache);
404
405   return trad_frame_get_register (cache, this_frame, regnum);
406 }
407
408 static int
409 i386obsd_trapframe_sniffer (const struct frame_unwind *self,
410                             struct frame_info *this_frame,
411                             void **this_prologue_cache)
412 {
413   ULONGEST cs;
414   const char *name;
415
416   /* Check Current Privilege Level and bail out if we're not executing
417      in kernel space.  */
418   cs = get_frame_register_unsigned (this_frame, I386_CS_REGNUM);
419   if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
420     return 0;
421
422   find_pc_partial_function (get_frame_pc (this_frame), &name, NULL, NULL);
423   return (name && (strcmp (name, "calltrap") == 0
424                    || strcmp (name, "syscall1") == 0
425                    || strncmp (name, "Xintr", 5) == 0
426                    || strncmp (name, "Xsoft", 5) == 0));
427 }
428
429 static const struct frame_unwind i386obsd_trapframe_unwind = {
430   /* FIXME: kettenis/20051219: This really is more like an interrupt
431      frame, but SIGTRAMP_FRAME would print <signal handler called>,
432      which really is not what we want here.  */
433   NORMAL_FRAME,
434   default_frame_unwind_stop_reason,
435   i386obsd_trapframe_this_id,
436   i386obsd_trapframe_prev_register,
437   NULL,
438   i386obsd_trapframe_sniffer
439 };
440 \f
441
442 static void 
443 i386obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
444 {
445   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
446
447   /* Obviously OpenBSD is BSD-based.  */
448   i386bsd_init_abi (info, gdbarch);
449   obsd_init_abi (info, gdbarch);
450
451   /* OpenBSD has a different `struct reg'.  */
452   tdep->gregset_reg_offset = i386obsd_r_reg_offset;
453   tdep->gregset_num_regs = ARRAY_SIZE (i386obsd_r_reg_offset);
454   tdep->sizeof_gregset = 16 * 4;
455
456   /* OpenBSD uses -freg-struct-return by default.  */
457   tdep->struct_return = reg_struct_return;
458
459   /* OpenBSD uses a different memory layout.  */
460   tdep->sigtramp_start = i386obsd_sigtramp_start_addr;
461   tdep->sigtramp_end = i386obsd_sigtramp_end_addr;
462   tdep->sigtramp_p = i386obsd_sigtramp_p;
463
464   /* OpenBSD has a `struct sigcontext' that's different from the
465      original 4.3 BSD.  */
466   tdep->sc_reg_offset = i386obsd_sc_reg_offset;
467   tdep->sc_num_regs = ARRAY_SIZE (i386obsd_sc_reg_offset);
468
469   /* OpenBSD provides a user-level threads implementation.  */
470   bsd_uthread_set_supply_uthread (gdbarch, i386obsd_supply_uthread);
471   bsd_uthread_set_collect_uthread (gdbarch, i386obsd_collect_uthread);
472
473   /* Unwind kernel trap frames correctly.  */
474   frame_unwind_prepend_unwinder (gdbarch, &i386obsd_trapframe_unwind);
475 }
476
477 /* OpenBSD a.out.  */
478
479 static void
480 i386obsd_aout_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
481 {
482   i386obsd_init_abi (info, gdbarch);
483
484   /* OpenBSD a.out has a single register set.  */
485   set_gdbarch_regset_from_core_section
486     (gdbarch, i386obsd_aout_regset_from_core_section);
487 }
488
489 /* OpenBSD ELF.  */
490
491 static void
492 i386obsd_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
493 {
494   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
495
496   /* It's still OpenBSD.  */
497   i386obsd_init_abi (info, gdbarch);
498
499   /* But ELF-based.  */
500   i386_elf_init_abi (info, gdbarch);
501
502   /* OpenBSD ELF uses SVR4-style shared libraries.  */
503   set_solib_svr4_fetch_link_map_offsets
504     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
505 }
506 \f
507
508 /* Provide a prototype to silence -Wmissing-prototypes.  */
509 void _initialize_i386obsd_tdep (void);
510
511 void
512 _initialize_i386obsd_tdep (void)
513 {
514   /* FIXME: kettenis/20021020: Since OpenBSD/i386 binaries are
515      indistingushable from NetBSD/i386 a.out binaries, building a GDB
516      that should support both these targets will probably not work as
517      expected.  */
518 #define GDB_OSABI_OPENBSD_AOUT GDB_OSABI_NETBSD_AOUT
519
520   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_OPENBSD_AOUT,
521                           i386obsd_aout_init_abi);
522   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_OPENBSD_ELF,
523                           i386obsd_elf_init_abi);
524 }