* dwarf2read.c (dwarf2_symbol_mark_computed): Handle corrupted
[platform/upstream/binutils.git] / gdb / sparcobsd-tdep.c
1 /* Target-dependent code for OpenBSD/sparc.
2
3    Copyright (C) 2004, 2005 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., 51 Franklin Street, Fifth Floor,
20    Boston, MA 02110-1301, USA.  */
21
22 #include "defs.h"
23 #include "floatformat.h"
24 #include "frame.h"
25 #include "frame-unwind.h"
26 #include "gdbcore.h"
27 #include "osabi.h"
28 #include "regcache.h"
29 #include "symtab.h"
30 #include "trad-frame.h"
31
32 #include "gdb_assert.h"
33
34 #include "obsd-tdep.h"
35 #include "sparc-tdep.h"
36 #include "solib-svr4.h"
37 #include "bsd-uthread.h"
38
39 /* Signal trampolines.  */
40
41 /* The OpenBSD kernel maps the signal trampoline at some random
42    location in user space, which means that the traditional BSD way of
43    detecting it won't work.
44
45    The signal trampoline will be mapped at an address that is page
46    aligned.  We recognize the signal trampoline by the looking for the
47    sigreturn system call.  */
48
49 static const int sparc32obsd_page_size = 4096;
50
51 static int
52 sparc32obsd_pc_in_sigtramp (CORE_ADDR pc, char *name)
53 {
54   CORE_ADDR start_pc = (pc & ~(sparc32obsd_page_size - 1));
55   unsigned long insn;
56
57   if (name)
58     return 0;
59
60   /* Check for "restore %g0, SYS_sigreturn, %g1".  */
61   insn = sparc_fetch_instruction (start_pc + 0xec);
62   if (insn != 0x83e82067)
63     return 0;
64
65   /* Check for "t ST_SYSCALL".  */
66   insn = sparc_fetch_instruction (start_pc + 0xf4);
67   if (insn != 0x91d02000)
68     return 0;
69
70   return 1;
71 }
72
73 static struct sparc_frame_cache *
74 sparc32obsd_frame_cache (struct frame_info *next_frame, void **this_cache)
75 {
76   struct sparc_frame_cache *cache;
77   CORE_ADDR addr;
78
79   if (*this_cache)
80     return *this_cache;
81
82   cache = sparc_frame_cache (next_frame, this_cache);
83   gdb_assert (cache == *this_cache);
84
85   /* If we couldn't find the frame's function, we're probably dealing
86      with an on-stack signal trampoline.  */
87   if (cache->pc == 0)
88     {
89       cache->pc = frame_pc_unwind (next_frame);
90       cache->pc &= ~(sparc32obsd_page_size - 1);
91
92       /* Since we couldn't find the frame's function, the cache was
93          initialized under the assumption that we're frameless.  */
94       cache->frameless_p = 0;
95       addr = frame_unwind_register_unsigned (next_frame, SPARC_FP_REGNUM);
96       cache->base = addr;
97     }
98
99   cache->saved_regs = sparc32nbsd_sigcontext_saved_regs (next_frame);
100
101   return cache;
102 }
103
104 static void
105 sparc32obsd_frame_this_id (struct frame_info *next_frame, void **this_cache,
106                            struct frame_id *this_id)
107 {
108   struct sparc_frame_cache *cache =
109     sparc32obsd_frame_cache (next_frame, this_cache);
110
111   (*this_id) = frame_id_build (cache->base, cache->pc);
112 }
113
114 static void
115 sparc32obsd_frame_prev_register (struct frame_info *next_frame,
116                                  void **this_cache,
117                                  int regnum, int *optimizedp,
118                                  enum lval_type *lvalp, CORE_ADDR *addrp,
119                                  int *realnump, gdb_byte *valuep)
120 {
121   struct sparc_frame_cache *cache =
122     sparc32obsd_frame_cache (next_frame, this_cache);
123
124   trad_frame_get_prev_register (next_frame, cache->saved_regs, regnum,
125                                 optimizedp, lvalp, addrp, realnump, valuep);
126 }
127
128 static const struct frame_unwind sparc32obsd_frame_unwind =
129 {
130   SIGTRAMP_FRAME,
131   sparc32obsd_frame_this_id,
132   sparc32obsd_frame_prev_register
133 };
134
135 static const struct frame_unwind *
136 sparc32obsd_sigtramp_frame_sniffer (struct frame_info *next_frame)
137 {
138   CORE_ADDR pc = frame_pc_unwind (next_frame);
139   char *name;
140
141   find_pc_partial_function (pc, &name, NULL, NULL);
142   if (sparc32obsd_pc_in_sigtramp (pc, name))
143     return &sparc32obsd_frame_unwind;
144
145   return NULL;
146 }
147 \f
148
149 /* Offset wthin the thread structure where we can find %fp and %i7.  */
150 #define SPARC32OBSD_UTHREAD_FP_OFFSET   128
151 #define SPARC32OBSD_UTHREAD_PC_OFFSET   132
152
153 static void
154 sparc32obsd_supply_uthread (struct regcache *regcache,
155                             int regnum, CORE_ADDR addr)
156 {
157   CORE_ADDR fp, fp_addr = addr + SPARC32OBSD_UTHREAD_FP_OFFSET;
158   gdb_byte buf[4];
159
160   gdb_assert (regnum >= -1);
161
162   fp = read_memory_unsigned_integer (fp_addr, 4);
163   if (regnum == SPARC_SP_REGNUM || regnum == -1)
164     {
165       store_unsigned_integer (buf, 4, fp);
166       regcache_raw_supply (regcache, SPARC_SP_REGNUM, buf);
167
168       if (regnum == SPARC_SP_REGNUM)
169         return;
170     }
171
172   if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM
173       || regnum == -1)
174     {
175       CORE_ADDR i7, i7_addr = addr + SPARC32OBSD_UTHREAD_PC_OFFSET;
176
177       i7 = read_memory_unsigned_integer (i7_addr, 4);
178       if (regnum == SPARC32_PC_REGNUM || regnum == -1)
179         {
180           store_unsigned_integer (buf, 4, i7 + 8);
181           regcache_raw_supply (regcache, SPARC32_PC_REGNUM, buf);
182         }
183       if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
184         {
185           store_unsigned_integer (buf, 4, i7 + 12);
186           regcache_raw_supply (regcache, SPARC32_NPC_REGNUM, buf);
187         }
188
189       if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
190         return;
191     }
192
193   sparc_supply_rwindow (regcache, fp, regnum);
194 }
195
196 static void
197 sparc32obsd_collect_uthread(const struct regcache *regcache,
198                             int regnum, CORE_ADDR addr)
199 {
200   CORE_ADDR sp;
201   gdb_byte buf[4];
202
203   gdb_assert (regnum >= -1);
204
205   if (regnum == SPARC_SP_REGNUM || regnum == -1)
206     {
207       CORE_ADDR fp_addr = addr + SPARC32OBSD_UTHREAD_FP_OFFSET;
208
209       regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
210       write_memory (fp_addr,buf, 4);
211     }
212
213   if (regnum == SPARC32_PC_REGNUM || regnum == -1)
214     {
215       CORE_ADDR i7, i7_addr = addr + SPARC32OBSD_UTHREAD_PC_OFFSET;
216
217       regcache_raw_collect (regcache, SPARC32_PC_REGNUM, buf);
218       i7 = extract_unsigned_integer (buf, 4) - 8;
219       write_memory_unsigned_integer (i7_addr, 4, i7);
220
221       if (regnum == SPARC32_PC_REGNUM)
222         return;
223     }
224
225   regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
226   sp = extract_unsigned_integer (buf, 4);
227   sparc_collect_rwindow (regcache, sp, regnum);
228 }
229 \f
230
231 static void
232 sparc32obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
233 {
234   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
235
236   /* OpenBSD/sparc is very similar to NetBSD/sparc ELF.  */
237   sparc32nbsd_elf_init_abi (info, gdbarch);
238
239   set_gdbarch_skip_solib_resolver (gdbarch, obsd_skip_solib_resolver);
240
241   frame_unwind_append_sniffer (gdbarch, sparc32obsd_sigtramp_frame_sniffer);
242
243   /* OpenBSD provides a user-level threads implementation.  */
244   bsd_uthread_set_supply_uthread (gdbarch, sparc32obsd_supply_uthread);
245   bsd_uthread_set_collect_uthread (gdbarch, sparc32obsd_collect_uthread);
246 }
247
248 \f
249 /* Provide a prototype to silence -Wmissing-prototypes.  */
250 void _initialize_sparc32obsd_tdep (void);
251
252 void
253 _initialize_sparc32obsd_tdep (void)
254 {
255   gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_OPENBSD_ELF,
256                           sparc32obsd_init_abi);
257 }