OSDN Git Service

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