* breakpoint.c:
[platform/upstream/binutils.git] / gdb / sparc-sol2-tdep.c
1 /* Target-dependent code for Solaris SPARC.
2
3    Copyright (C) 2003, 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., 51 Franklin Street, Fifth Floor,
20    Boston, MA 02110-1301, USA.  */
21
22 #include "defs.h"
23 #include "frame.h"
24 #include "frame-unwind.h"
25 #include "gdbcore.h"
26 #include "symtab.h"
27 #include "objfiles.h"
28 #include "osabi.h"
29 #include "regcache.h"
30 #include "target.h"
31 #include "trad-frame.h"
32
33 #include "gdb_assert.h"
34 #include "gdb_string.h"
35
36 #include "sparc-tdep.h"
37 #include "solib-svr4.h"
38
39 /* From <sys/regset.h>.  */
40 const struct sparc_gregset sparc32_sol2_gregset =
41 {
42   32 * 4,                       /* %psr */
43   33 * 4,                       /* %pc */
44   34 * 4,                       /* %npc */
45   35 * 4,                       /* %y */
46   36 * 4,                       /* %wim */
47   37 * 4,                       /* %tbr */
48   1 * 4,                        /* %g1 */
49   16 * 4,                       /* %l0 */
50 };
51 \f
52
53 /* The Solaris signal trampolines reside in libc.  For normal signals,
54    the function `sigacthandler' is used.  This signal trampoline will
55    call the signal handler using the System V calling convention,
56    where the third argument is a pointer to an instance of
57    `ucontext_t', which has a member `uc_mcontext' that contains the
58    saved registers.  Incidentally, the kernel passes the `ucontext_t'
59    pointer as the third argument of the signal trampoline too, and
60    `sigacthandler' simply passes it on. However, if you link your
61    program with "-L/usr/ucblib -R/usr/ucblib -lucb", the function
62    `ucbsigvechandler' will be used, which invokes the using the BSD
63    convention, where the third argument is a pointer to an instance of
64    `struct sigcontext'.  It is the `ucbsigvechandler' function that
65    converts the `ucontext_t' to a `sigcontext', and back.  Unless the
66    signal handler modifies the `struct sigcontext' we can safely
67    ignore this.  */
68
69 int
70 sparc_sol2_pc_in_sigtramp (CORE_ADDR pc, char *name)
71 {
72   return (name && (strcmp (name, "sigacthandler") == 0
73                    || strcmp (name, "ucbsigvechandler") == 0
74                    || strcmp (name, "__sighndlr") == 0));
75 }
76
77 static struct sparc_frame_cache *
78 sparc32_sol2_sigtramp_frame_cache (struct frame_info *next_frame,
79                                    void **this_cache)
80 {
81   struct sparc_frame_cache *cache;
82   CORE_ADDR mcontext_addr, addr;
83   int regnum;
84
85   if (*this_cache)
86     return *this_cache;
87
88   cache = sparc_frame_cache (next_frame, this_cache);
89   gdb_assert (cache == *this_cache);
90
91   cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
92
93   /* The third argument is a pointer to an instance of `ucontext_t',
94      which has a member `uc_mcontext' that contains the saved
95      registers.  */
96   regnum = (cache->frameless_p ? SPARC_O2_REGNUM : SPARC_I2_REGNUM);
97   mcontext_addr = frame_unwind_register_unsigned (next_frame, regnum) + 40;
98
99   cache->saved_regs[SPARC32_PSR_REGNUM].addr = mcontext_addr + 0 * 4;
100   cache->saved_regs[SPARC32_PC_REGNUM].addr = mcontext_addr + 1 * 4;
101   cache->saved_regs[SPARC32_NPC_REGNUM].addr = mcontext_addr + 2 * 4;
102   cache->saved_regs[SPARC32_Y_REGNUM].addr = mcontext_addr + 3 * 4;
103
104   /* Since %g0 is always zero, keep the identity encoding.  */
105   for (regnum = SPARC_G1_REGNUM, addr = mcontext_addr + 4 * 4;
106        regnum <= SPARC_O7_REGNUM; regnum++, addr += 4)
107     cache->saved_regs[regnum].addr = addr;
108
109   if (get_frame_memory_unsigned (next_frame, mcontext_addr + 19 * 4, 4))
110     {
111       /* The register windows haven't been flushed.  */
112       for (regnum = SPARC_L0_REGNUM; regnum <= SPARC_I7_REGNUM; regnum++)
113         trad_frame_set_unknown (cache->saved_regs, regnum);
114     }
115   else
116     {
117       addr = cache->saved_regs[SPARC_SP_REGNUM].addr;
118       addr = get_frame_memory_unsigned (next_frame, addr, 4);
119       for (regnum = SPARC_L0_REGNUM;
120            regnum <= SPARC_I7_REGNUM; regnum++, addr += 4)
121         cache->saved_regs[regnum].addr = addr;
122     }
123
124   return cache;
125 }
126
127 static void
128 sparc32_sol2_sigtramp_frame_this_id (struct frame_info *next_frame,
129                                      void **this_cache,
130                                      struct frame_id *this_id)
131 {
132   struct sparc_frame_cache *cache =
133     sparc32_sol2_sigtramp_frame_cache (next_frame, this_cache);
134
135   (*this_id) = frame_id_build (cache->base, cache->pc);
136 }
137
138 static void
139 sparc32_sol2_sigtramp_frame_prev_register (struct frame_info *next_frame,
140                                            void **this_cache,
141                                            int regnum, int *optimizedp,
142                                            enum lval_type *lvalp,
143                                            CORE_ADDR *addrp,
144                                            int *realnump, gdb_byte *valuep)
145 {
146   struct sparc_frame_cache *cache =
147     sparc32_sol2_sigtramp_frame_cache (next_frame, this_cache);
148
149   trad_frame_get_prev_register (next_frame, cache->saved_regs, regnum,
150                                 optimizedp, lvalp, addrp, realnump, valuep);
151 }
152
153 static const struct frame_unwind sparc32_sol2_sigtramp_frame_unwind =
154 {
155   SIGTRAMP_FRAME,
156   sparc32_sol2_sigtramp_frame_this_id,
157   sparc32_sol2_sigtramp_frame_prev_register
158 };
159
160 static const struct frame_unwind *
161 sparc32_sol2_sigtramp_frame_sniffer (struct frame_info *next_frame)
162 {
163   CORE_ADDR pc = frame_pc_unwind (next_frame);
164   char *name;
165
166   find_pc_partial_function (pc, &name, NULL, NULL);
167   if (sparc_sol2_pc_in_sigtramp (pc, name))
168     return &sparc32_sol2_sigtramp_frame_unwind;
169
170   return NULL;
171 }
172 \f
173
174 void
175 sparc32_sol2_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
176 {
177   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
178
179   /* Solaris has SVR4-style shared libraries...  */
180   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
181   set_solib_svr4_fetch_link_map_offsets
182     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
183
184   /* ...which means that we need some special handling when doing
185      prologue analysis.  */
186   tdep->plt_entry_size = 12;
187
188   /* Solaris has kernel-assisted single-stepping support.  */
189   set_gdbarch_software_single_step (gdbarch, NULL);
190
191   frame_unwind_append_sniffer (gdbarch, sparc32_sol2_sigtramp_frame_sniffer);
192 }
193 \f
194
195 /* Provide a prototype to silence -Wmissing-prototypes.  */
196 void _initialize_sparc_sol2_tdep (void);
197
198 void
199 _initialize_sparc_sol2_tdep (void)
200 {
201   gdbarch_register_osabi (bfd_arch_sparc, 0,
202                           GDB_OSABI_SOLARIS, sparc32_sol2_init_abi);
203 }