Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / i386-netbsd-tdep.c
bloba4376569627072f741dcfe532f79557f2073816f
1 /* Target-dependent code for NetBSD/i386.
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 "arch-utils.h"
21 #include "frame.h"
22 #include "gdbcore.h"
23 #include "regcache.h"
24 #include "regset.h"
25 #include "osabi.h"
26 #include "symtab.h"
27 #include "trad-frame.h"
28 #include "tramp-frame.h"
30 #include "i386-tdep.h"
31 #include "i387-tdep.h"
32 #include "netbsd-tdep.h"
33 #include "solib-svr4.h"
35 /* From <machine/reg.h>. */
36 static int i386nbsd_r_reg_offset[] =
38 0 * 4, /* %eax */
39 1 * 4, /* %ecx */
40 2 * 4, /* %edx */
41 3 * 4, /* %ebx */
42 4 * 4, /* %esp */
43 5 * 4, /* %ebp */
44 6 * 4, /* %esi */
45 7 * 4, /* %edi */
46 8 * 4, /* %eip */
47 9 * 4, /* %eflags */
48 10 * 4, /* %cs */
49 11 * 4, /* %ss */
50 12 * 4, /* %ds */
51 13 * 4, /* %es */
52 14 * 4, /* %fs */
53 15 * 4 /* %gs */
56 /* From <machine/signal.h>. */
57 static int i386nbsd_sc_reg_offset[] =
59 10 * 4, /* %eax */
60 9 * 4, /* %ecx */
61 8 * 4, /* %edx */
62 7 * 4, /* %ebx */
63 14 * 4, /* %esp */
64 6 * 4, /* %ebp */
65 5 * 4, /* %esi */
66 4 * 4, /* %edi */
67 11 * 4, /* %eip */
68 13 * 4, /* %eflags */
69 12 * 4, /* %cs */
70 15 * 4, /* %ss */
71 3 * 4, /* %ds */
72 2 * 4, /* %es */
73 1 * 4, /* %fs */
74 0 * 4 /* %gs */
77 /* From <machine/mcontext.h>. */
78 static int i386nbsd_mc_reg_offset[] =
80 11 * 4, /* %eax */
81 10 * 4, /* %ecx */
82 9 * 4, /* %edx */
83 8 * 4, /* %ebx */
84 7 * 4, /* %esp */
85 6 * 4, /* %ebp */
86 5 * 4, /* %esi */
87 4 * 4, /* %edi */
88 14 * 4, /* %eip */
89 16 * 4, /* %eflags */
90 15 * 4, /* %cs */
91 18 * 4, /* %ss */
92 3 * 4, /* %ds */
93 2 * 4, /* %es */
94 1 * 4, /* %fs */
95 0 * 4 /* %gs */
98 static void i386nbsd_sigtramp_cache_init (const struct tramp_frame *,
99 const frame_info_ptr &,
100 struct trad_frame_cache *,
101 CORE_ADDR);
103 static const struct tramp_frame i386nbsd_sigtramp_sc16 =
105 SIGTRAMP_FRAME,
108 /* leal 0x10(%esp), %eax */
109 { 0x8d, ULONGEST_MAX },
110 { 0x44, ULONGEST_MAX },
111 { 0x24, ULONGEST_MAX },
112 { 0x10, ULONGEST_MAX },
114 /* pushl %eax */
115 { 0x50, ULONGEST_MAX },
117 /* pushl %eax */
118 { 0x50, ULONGEST_MAX },
120 /* movl $0x127, %eax # __sigreturn14 */
121 { 0xb8, ULONGEST_MAX },
122 { 0x27, ULONGEST_MAX },
123 {0x01, ULONGEST_MAX },
124 {0x00, ULONGEST_MAX },
125 {0x00, ULONGEST_MAX },
127 /* int $0x80 */
128 { 0xcd, ULONGEST_MAX },
129 { 0x80, ULONGEST_MAX},
131 /* movl $0x1, %eax # exit */
132 { 0xb8, ULONGEST_MAX },
133 { 0x01, ULONGEST_MAX },
134 {0x00, ULONGEST_MAX },
135 {0x00, ULONGEST_MAX },
136 {0x00, ULONGEST_MAX },
138 /* int $0x80 */
139 { 0xcd, ULONGEST_MAX },
140 { 0x80, ULONGEST_MAX},
142 { TRAMP_SENTINEL_INSN, ULONGEST_MAX }
144 i386nbsd_sigtramp_cache_init
147 static const struct tramp_frame i386nbsd_sigtramp_sc2 =
149 SIGTRAMP_FRAME,
152 /* leal 0x0c(%esp), %eax */
153 { 0x8d, ULONGEST_MAX },
154 { 0x44, ULONGEST_MAX },
155 { 0x24, ULONGEST_MAX },
156 { 0x0c, ULONGEST_MAX },
157 /* movl %eax, 0x4(%esp) */
158 { 0x89, ULONGEST_MAX },
159 { 0x44, ULONGEST_MAX },
160 { 0x24, ULONGEST_MAX },
161 { 0x04, ULONGEST_MAX },
162 /* movl $0x127, %eax # __sigreturn14 */
163 { 0xb8, ULONGEST_MAX },
164 { 0x27, ULONGEST_MAX },
165 {0x01, ULONGEST_MAX },
166 {0x00, ULONGEST_MAX },
167 {0x00, ULONGEST_MAX },
168 /* int $0x80 */
169 { 0xcd, ULONGEST_MAX },
170 { 0x80, ULONGEST_MAX},
171 /* movl %eax, 0x4(%esp) */
172 { 0x89, ULONGEST_MAX },
173 { 0x44, ULONGEST_MAX },
174 { 0x24, ULONGEST_MAX },
175 { 0x04, ULONGEST_MAX },
176 /* movl $0x1, %eax */
177 { 0xb8, ULONGEST_MAX },
178 { 0x01, ULONGEST_MAX },
179 {0x00, ULONGEST_MAX },
180 {0x00, ULONGEST_MAX },
181 {0x00, ULONGEST_MAX },
182 /* int $0x80 */
183 { 0xcd, ULONGEST_MAX },
184 { 0x80, ULONGEST_MAX},
185 { TRAMP_SENTINEL_INSN, ULONGEST_MAX }
187 i386nbsd_sigtramp_cache_init
190 static const struct tramp_frame i386nbsd_sigtramp_si2 =
192 SIGTRAMP_FRAME,
195 /* movl 8(%esp),%eax */
196 { 0x8b, ULONGEST_MAX },
197 { 0x44, ULONGEST_MAX },
198 { 0x24, ULONGEST_MAX },
199 { 0x08, ULONGEST_MAX },
200 /* movl %eax, 0x4(%esp) */
201 { 0x89, ULONGEST_MAX },
202 { 0x44, ULONGEST_MAX },
203 { 0x24, ULONGEST_MAX },
204 { 0x04, ULONGEST_MAX },
205 /* movl $0x134, %eax # setcontext */
206 { 0xb8, ULONGEST_MAX },
207 { 0x34, ULONGEST_MAX },
208 { 0x01, ULONGEST_MAX },
209 { 0x00, ULONGEST_MAX },
210 { 0x00, ULONGEST_MAX },
211 /* int $0x80 */
212 { 0xcd, ULONGEST_MAX },
213 { 0x80, ULONGEST_MAX },
214 /* movl %eax, 0x4(%esp) */
215 { 0x89, ULONGEST_MAX },
216 { 0x44, ULONGEST_MAX },
217 { 0x24, ULONGEST_MAX },
218 { 0x04, ULONGEST_MAX },
219 /* movl $0x1, %eax */
220 { 0xb8, ULONGEST_MAX },
221 { 0x01, ULONGEST_MAX },
222 { 0x00, ULONGEST_MAX },
223 { 0x00, ULONGEST_MAX },
224 { 0x00, ULONGEST_MAX },
225 /* int $0x80 */
226 { 0xcd, ULONGEST_MAX },
227 { 0x80, ULONGEST_MAX },
228 { TRAMP_SENTINEL_INSN, ULONGEST_MAX }
230 i386nbsd_sigtramp_cache_init
233 static const struct tramp_frame i386nbsd_sigtramp_si31 =
235 SIGTRAMP_FRAME,
238 /* leal 0x8c(%esp), %eax */
239 { 0x8d, ULONGEST_MAX },
240 { 0x84, ULONGEST_MAX },
241 { 0x24, ULONGEST_MAX },
242 { 0x8c, ULONGEST_MAX },
243 { 0x00, ULONGEST_MAX },
244 { 0x00, ULONGEST_MAX },
245 { 0x00, ULONGEST_MAX },
246 /* movl %eax, 0x4(%esp) */
247 { 0x89, ULONGEST_MAX },
248 { 0x44, ULONGEST_MAX },
249 { 0x24, ULONGEST_MAX },
250 { 0x04, ULONGEST_MAX },
251 /* movl $0x134, %eax # setcontext */
252 { 0xb8, ULONGEST_MAX },
253 { 0x34, ULONGEST_MAX },
254 { 0x01, ULONGEST_MAX },
255 { 0x00, ULONGEST_MAX },
256 { 0x00, ULONGEST_MAX },
257 /* int $0x80 */
258 { 0xcd, ULONGEST_MAX },
259 { 0x80, ULONGEST_MAX},
260 /* movl %eax, 0x4(%esp) */
261 { 0x89, ULONGEST_MAX },
262 { 0x44, ULONGEST_MAX },
263 { 0x24, ULONGEST_MAX },
264 { 0x04, ULONGEST_MAX },
265 /* movl $0x1, %eax */
266 { 0xb8, ULONGEST_MAX },
267 { 0x01, ULONGEST_MAX },
268 {0x00, ULONGEST_MAX },
269 {0x00, ULONGEST_MAX },
270 {0x00, ULONGEST_MAX },
271 /* int $0x80 */
272 { 0xcd, ULONGEST_MAX },
273 { 0x80, ULONGEST_MAX},
274 { TRAMP_SENTINEL_INSN, ULONGEST_MAX }
276 i386nbsd_sigtramp_cache_init
279 static const struct tramp_frame i386nbsd_sigtramp_si4 =
281 SIGTRAMP_FRAME,
284 /* leal 0x8c(%esp), %eax */
285 { 0x8d, ULONGEST_MAX },
286 { 0x84, ULONGEST_MAX },
287 { 0x24, ULONGEST_MAX },
288 { 0x8c, ULONGEST_MAX },
289 { 0x00, ULONGEST_MAX },
290 { 0x00, ULONGEST_MAX },
291 { 0x00, ULONGEST_MAX },
292 /* movl %eax, 0x4(%esp) */
293 { 0x89, ULONGEST_MAX },
294 { 0x44, ULONGEST_MAX },
295 { 0x24, ULONGEST_MAX },
296 { 0x04, ULONGEST_MAX },
297 /* movl $0x134, %eax # setcontext */
298 { 0xb8, ULONGEST_MAX },
299 { 0x34, ULONGEST_MAX },
300 { 0x01, ULONGEST_MAX },
301 { 0x00, ULONGEST_MAX },
302 { 0x00, ULONGEST_MAX },
303 /* int $0x80 */
304 { 0xcd, ULONGEST_MAX },
305 { 0x80, ULONGEST_MAX},
306 /* movl $0xffffffff,0x4(%esp) */
307 { 0xc7, ULONGEST_MAX },
308 { 0x44, ULONGEST_MAX },
309 { 0x24, ULONGEST_MAX },
310 { 0x04, ULONGEST_MAX },
311 { 0xff, ULONGEST_MAX },
312 { 0xff, ULONGEST_MAX },
313 { 0xff, ULONGEST_MAX },
314 { 0xff, ULONGEST_MAX },
315 /* movl $0x1, %eax */
316 { 0xb8, ULONGEST_MAX },
317 { 0x01, ULONGEST_MAX },
318 {0x00, ULONGEST_MAX },
319 {0x00, ULONGEST_MAX },
320 {0x00, ULONGEST_MAX },
321 /* int $0x80 */
322 { 0xcd, ULONGEST_MAX },
323 { 0x80, ULONGEST_MAX},
324 { TRAMP_SENTINEL_INSN, ULONGEST_MAX }
326 i386nbsd_sigtramp_cache_init
329 static void
330 i386nbsd_sigtramp_cache_init (const struct tramp_frame *self,
331 const frame_info_ptr &this_frame,
332 struct trad_frame_cache *this_cache,
333 CORE_ADDR func)
335 struct gdbarch *gdbarch = get_frame_arch (this_frame);
336 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
337 CORE_ADDR sp = get_frame_register_unsigned (this_frame, I386_ESP_REGNUM);
338 CORE_ADDR base;
339 int *reg_offset;
340 int num_regs;
341 int i;
343 if (self == &i386nbsd_sigtramp_sc16 || self == &i386nbsd_sigtramp_sc2)
345 reg_offset = i386nbsd_sc_reg_offset;
346 num_regs = ARRAY_SIZE (i386nbsd_sc_reg_offset);
348 /* Read in the sigcontext address. */
349 base = read_memory_unsigned_integer (sp + 8, 4, byte_order);
351 else
353 reg_offset = i386nbsd_mc_reg_offset;
354 num_regs = ARRAY_SIZE (i386nbsd_mc_reg_offset);
356 /* Read in the ucontext address. */
357 base = read_memory_unsigned_integer (sp + 8, 4, byte_order);
358 /* offsetof(ucontext_t, uc_mcontext) == 36 */
359 base += 36;
362 for (i = 0; i < num_regs; i++)
363 if (reg_offset[i] != -1)
364 trad_frame_set_reg_addr (this_cache, i, base + reg_offset[i]);
366 /* Construct the frame ID using the function start. */
367 trad_frame_set_id (this_cache, frame_id_build (sp, func));
371 static void
372 i386nbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
374 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
376 /* Obviously NetBSD is BSD-based. */
377 i386bsd_init_abi (info, gdbarch);
379 nbsd_init_abi (info, gdbarch);
381 /* NetBSD has a different `struct reg'. */
382 tdep->gregset_reg_offset = i386nbsd_r_reg_offset;
383 tdep->gregset_num_regs = ARRAY_SIZE (i386nbsd_r_reg_offset);
384 tdep->sizeof_gregset = 16 * 4;
386 /* NetBSD uses -freg-struct-return by default. */
387 tdep->struct_return = reg_struct_return;
389 /* NetBSD uses tramp_frame sniffers for signal trampolines. */
390 tdep->sigcontext_addr= 0;
391 tdep->sigtramp_start = 0;
392 tdep->sigtramp_end = 0;
393 tdep->sigtramp_p = 0;
394 tdep->sc_reg_offset = 0;
395 tdep->sc_num_regs = 0;
397 tramp_frame_prepend_unwinder (gdbarch, &i386nbsd_sigtramp_sc16);
398 tramp_frame_prepend_unwinder (gdbarch, &i386nbsd_sigtramp_sc2);
399 tramp_frame_prepend_unwinder (gdbarch, &i386nbsd_sigtramp_si2);
400 tramp_frame_prepend_unwinder (gdbarch, &i386nbsd_sigtramp_si31);
401 tramp_frame_prepend_unwinder (gdbarch, &i386nbsd_sigtramp_si4);
404 /* NetBSD ELF. */
406 static void
407 i386nbsdelf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
409 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
411 /* It's still NetBSD. */
412 i386nbsd_init_abi (info, gdbarch);
414 /* But ELF-based. */
415 i386_elf_init_abi (info, gdbarch);
417 /* NetBSD ELF uses SVR4-style shared libraries. */
418 set_solib_svr4_fetch_link_map_offsets
419 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
421 /* NetBSD ELF uses -fpcc-struct-return by default. */
422 tdep->struct_return = pcc_struct_return;
425 void _initialize_i386nbsd_tdep ();
426 void
427 _initialize_i386nbsd_tdep ()
429 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_NETBSD,
430 i386nbsdelf_init_abi);