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