Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / i386-sol2-nat.c
blob20ca9d68cbc80e43356e4148fbaf5bc75796f14f
1 /* Native-dependent code for Solaris x86.
3 Copyright (C) 1988-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "regcache.h"
22 #include <sys/reg.h>
23 #include <sys/procfs.h>
24 #include "gregset.h"
25 #include "target.h"
26 #include "procfs.h"
28 /* This file provids the (temporary) glue between the Solaris x86
29 target dependent code and the machine independent SVR4 /proc
30 support. */
32 /* Solaris 10 (Solaris 2.10, SunOS 5.10) and up support two process
33 data models, the traditional 32-bit data model (ILP32) and the
34 64-bit data model (LP64). The format of /proc depends on the data
35 model of the observer (the controlling process, GDB in our case).
36 The Solaris header files conveniently define PR_MODEL_NATIVE to the
37 data model of the controlling process. If its value is
38 PR_MODEL_LP64, we know that GDB is being compiled as a 64-bit
39 program.
41 Note that a 32-bit GDB won't be able to debug a 64-bit target
42 process using /proc on Solaris. */
44 #if PR_MODEL_NATIVE == PR_MODEL_LP64
46 #include "amd64-nat.h"
47 #include "amd64-tdep.h"
49 /* Mapping between the general-purpose registers in gregset_t format
50 and GDB's register cache layout. */
52 /* From <sys/regset.h>. */
53 static int amd64_sol2_gregset64_reg_offset[] = {
54 14 * 8, /* %rax */
55 11 * 8, /* %rbx */
56 13 * 8, /* %rcx */
57 12 * 8, /* %rdx */
58 9 * 8, /* %rsi */
59 8 * 8, /* %rdi */
60 10 * 8, /* %rbp */
61 20 * 8, /* %rsp */
62 7 * 8, /* %r8 ... */
63 6 * 8,
64 5 * 8,
65 4 * 8,
66 3 * 8,
67 2 * 8,
68 1 * 8,
69 0 * 8, /* ... %r15 */
70 17 * 8, /* %rip */
71 19 * 8, /* %eflags */
72 18 * 8, /* %cs */
73 21 * 8, /* %ss */
74 25 * 8, /* %ds */
75 24 * 8, /* %es */
76 22 * 8, /* %fs */
77 23 * 8 /* %gs */
80 /* 32-bit registers are provided by Solaris in 64-bit format, so just
81 give a subset of the list above. */
82 static int amd64_sol2_gregset32_reg_offset[] = {
83 14 * 8, /* %eax */
84 13 * 8, /* %ecx */
85 12 * 8, /* %edx */
86 11 * 8, /* %ebx */
87 20 * 8, /* %esp */
88 10 * 8, /* %ebp */
89 9 * 8, /* %esi */
90 8 * 8, /* %edi */
91 17 * 8, /* %eip */
92 19 * 8, /* %eflags */
93 18 * 8, /* %cs */
94 21 * 8, /* %ss */
95 25 * 8, /* %ds */
96 24 * 8, /* %es */
97 22 * 8, /* %fs */
98 23 * 8 /* %gs */
101 void
102 supply_gregset (struct regcache *regcache, const prgregset_t *gregs)
104 amd64_supply_native_gregset (regcache, gregs, -1);
107 void
108 supply_fpregset (struct regcache *regcache, const prfpregset_t *fpregs)
110 amd64_supply_fxsave (regcache, -1, fpregs);
113 void
114 fill_gregset (const struct regcache *regcache,
115 prgregset_t *gregs, int regnum)
117 amd64_collect_native_gregset (regcache, gregs, regnum);
120 void
121 fill_fpregset (const struct regcache *regcache,
122 prfpregset_t *fpregs, int regnum)
124 amd64_collect_fxsave (regcache, regnum, fpregs);
127 #else /* PR_MODEL_NATIVE != PR_MODEL_LP64 */
129 #include "i386-tdep.h"
130 #include "i387-tdep.h"
132 /* The `/proc' interface divides the target machine's register set up
133 into two different sets, the general purpose register set (gregset)
134 and the floating-point register set (fpregset).
136 The actual structure is, of course, naturally machine dependent, and is
137 different for each set of registers. For the i386 for example, the
138 general-purpose register set is typically defined by:
140 typedef int gregset_t[19]; (in <sys/regset.h>)
142 #define GS 0 (in <sys/reg.h>)
143 #define FS 1
145 #define UESP 17
146 #define SS 18
148 and the floating-point set by:
150 typedef struct fpregset {
151 union {
152 struct fpchip_state // fp extension state //
154 int state[27]; // 287/387 saved state //
155 int status; // status word saved at //
156 // exception //
157 } fpchip_state;
158 struct fp_emul_space // for emulators //
160 char fp_emul[246];
161 char fp_epad[2];
162 } fp_emul_space;
163 int f_fpregs[62]; // union of the above //
164 } fp_reg_set;
165 long f_wregs[33]; // saved weitek state //
166 } fpregset_t;
168 Incidentally fpchip_state contains the FPU state in the same format
169 as used by the "fsave" instruction, and that's the only thing we
170 support here. I don't know how the emulator stores it state. The
171 Weitek stuff definitely isn't supported.
173 The routines defined here, provide the packing and unpacking of
174 gregset_t and fpregset_t formatted data. */
176 /* Mapping between the general-purpose registers in `/proc'
177 format and GDB's register array layout. */
178 static int regmap[] =
180 11 /* EAX */,
181 10 /* ECX */,
182 9 /* EDX */,
183 8 /* EBX */,
184 17 /* UESP */,
185 6 /* EBP */,
186 5 /* ESI */,
187 4 /* EDI */,
188 14 /* EIP */,
189 16 /* EFL */,
190 15 /* CS */,
191 18 /* SS */,
192 3 /* DS */,
193 2 /* ES */,
194 1 /* FS */,
195 0 /* GS */
198 /* Fill GDB's register array with the general-purpose register values
199 in *GREGSETP. */
201 void
202 supply_gregset (struct regcache *regcache, const gregset_t *gregsetp)
204 const greg_t *regp = (const greg_t *) gregsetp;
205 int regnum;
207 for (regnum = 0; regnum < I386_NUM_GREGS; regnum++)
208 regcache->raw_supply (regnum, regp + regmap[regnum]);
211 /* Fill register REGNUM (if it is a general-purpose register) in
212 *GREGSETPS with the value in GDB's register array. If REGNUM is -1,
213 do this for all registers. */
215 void
216 fill_gregset (const struct regcache *regcache,
217 gregset_t *gregsetp, int regnum)
219 greg_t *regp = (greg_t *) gregsetp;
220 int i;
222 for (i = 0; i < I386_NUM_GREGS; i++)
223 if (regnum == -1 || regnum == i)
224 regcache->raw_collect (i, regp + regmap[i]);
227 /* Fill GDB's register array with the floating-point register values in
228 *FPREGSETP. */
230 void
231 supply_fpregset (struct regcache *regcache, const fpregset_t *fpregsetp)
233 if (gdbarch_fp0_regnum (regcache->arch ()) == 0)
234 return;
236 i387_supply_fsave (regcache, -1, fpregsetp);
239 /* Fill register REGNO (if it is a floating-point register) in
240 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
241 do this for all registers. */
243 void
244 fill_fpregset (const struct regcache *regcache,
245 fpregset_t *fpregsetp, int regno)
247 if (gdbarch_fp0_regnum (regcache->arch ()) == 0)
248 return;
250 i387_collect_fsave (regcache, regno, fpregsetp);
253 #endif
255 void _initialize_amd64_sol2_nat ();
256 void
257 _initialize_amd64_sol2_nat ()
259 #if PR_MODEL_NATIVE == PR_MODEL_LP64
260 amd64_native_gregset32_reg_offset = amd64_sol2_gregset32_reg_offset;
261 amd64_native_gregset32_num_regs =
262 ARRAY_SIZE (amd64_sol2_gregset32_reg_offset);
263 amd64_native_gregset64_reg_offset = amd64_sol2_gregset64_reg_offset;
264 amd64_native_gregset64_num_regs =
265 ARRAY_SIZE (amd64_sol2_gregset64_reg_offset);
266 #endif