1/* Target-dependent code for OpenBSD/sparc.
2
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 "floatformat.h"
24#include "frame.h"
25#include "frame-unwind.h"
26#include "osabi.h"
27#include "solib-svr4.h"
28#include "symtab.h"
29#include "trad-frame.h"
30
31#include "gdb_assert.h"
32
33#include "sparc-tdep.h"
34#include "nbsd-tdep.h"
35
36/* Signal trampolines.  */
37
38/* The OpenBSD kernel maps the signal trampoline at some random
39   location in user space, which means that the traditional BSD way of
40   detecting it won't work.
41
42   The signal trampoline will be mapped at an address that is page
43   aligned.  We recognize the signal trampoline by the looking for the
44   sigreturn system call.  */
45
46static const int sparc32obsd_page_size = 4096;
47
48static int
49sparc32obsd_pc_in_sigtramp (CORE_ADDR pc, char *name)
50{
51  CORE_ADDR start_pc = (pc & ~(sparc32obsd_page_size - 1));
52  unsigned long insn;
53
54  if (name)
55    return 0;
56
57  /* Check for "restore %g0, SYS_sigreturn, %g1".  */
58  insn = sparc_fetch_instruction (start_pc + 0xec);
59  if (insn != 0x83e82067)
60    return 0;
61
62  /* Check for "t ST_SYSCALL".  */
63  insn = sparc_fetch_instruction (start_pc + 0xf4);
64  if (insn != 0x91d02000)
65    return 0;
66
67  return 1;
68}
69
70static struct sparc_frame_cache *
71sparc32obsd_frame_cache (struct frame_info *next_frame, void **this_cache)
72{
73  struct sparc_frame_cache *cache;
74  CORE_ADDR addr;
75
76  if (*this_cache)
77    return *this_cache;
78
79  cache = sparc_frame_cache (next_frame, this_cache);
80  gdb_assert (cache == *this_cache);
81
82  /* If we couldn't find the frame's function, we're probably dealing
83     with an on-stack signal trampoline.  */
84  if (cache->pc == 0)
85    {
86      cache->pc = frame_pc_unwind (next_frame);
87      cache->pc &= ~(sparc32obsd_page_size - 1);
88
89      /* Since we couldn't find the frame's function, the cache was
90         initialized under the assumption that we're frameless.  */
91      cache->frameless_p = 0;
92      addr = frame_unwind_register_unsigned (next_frame, SPARC_FP_REGNUM);
93      cache->base = addr;
94    }
95
96  cache->saved_regs = sparc32nbsd_sigcontext_saved_regs (next_frame);
97
98  return cache;
99}
100
101static void
102sparc32obsd_frame_this_id (struct frame_info *next_frame, void **this_cache,
103			   struct frame_id *this_id)
104{
105  struct sparc_frame_cache *cache =
106    sparc32obsd_frame_cache (next_frame, this_cache);
107
108  (*this_id) = frame_id_build (cache->base, cache->pc);
109}
110
111static void
112sparc32obsd_frame_prev_register (struct frame_info *next_frame,
113				 void **this_cache,
114				 int regnum, int *optimizedp,
115				 enum lval_type *lvalp, CORE_ADDR *addrp,
116				 int *realnump, void *valuep)
117{
118  struct sparc_frame_cache *cache =
119    sparc32obsd_frame_cache (next_frame, this_cache);
120
121  trad_frame_prev_register (next_frame, cache->saved_regs, regnum,
122			    optimizedp, lvalp, addrp, realnump, valuep);
123}
124
125static const struct frame_unwind sparc32obsd_frame_unwind =
126{
127  SIGTRAMP_FRAME,
128  sparc32obsd_frame_this_id,
129  sparc32obsd_frame_prev_register
130};
131
132static const struct frame_unwind *
133sparc32obsd_sigtramp_frame_sniffer (struct frame_info *next_frame)
134{
135  CORE_ADDR pc = frame_pc_unwind (next_frame);
136  char *name;
137
138  find_pc_partial_function (pc, &name, NULL, NULL);
139  if (sparc32obsd_pc_in_sigtramp (pc, name))
140    return &sparc32obsd_frame_unwind;
141
142  return NULL;
143}
144
145
146static void
147sparc32obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
148{
149  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
150
151  /* OpenBSD doesn't support the 128-bit `long double' from the psABI.  */
152  set_gdbarch_long_double_bit (gdbarch, 64);
153  set_gdbarch_long_double_format (gdbarch, &floatformat_ieee_double_big);
154
155  set_gdbarch_pc_in_sigtramp (gdbarch, sparc32obsd_pc_in_sigtramp);
156  frame_unwind_append_sniffer (gdbarch, sparc32obsd_sigtramp_frame_sniffer);
157
158  set_solib_svr4_fetch_link_map_offsets
159    (gdbarch, nbsd_ilp32_solib_svr4_fetch_link_map_offsets);
160}
161
162
163/* Provide a prototype to silence -Wmissing-prototypes.  */
164void _initialize_sparc32obsd_tdep (void);
165
166void
167_initialize_sparc32obsd_tdep (void)
168{
169  gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_OPENBSD_ELF,
170			  sparc32obsd_init_abi);
171}
172