Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / i386-obsd-tdep.c
blob6e31567328eba493997bdfe15ee61f5bd0653f19
1 /* Target-dependent code for OpenBSD/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 "frame-unwind.h"
23 #include "gdbcore.h"
24 #include "regcache.h"
25 #include "regset.h"
26 #include "symtab.h"
27 #include "objfiles.h"
28 #include "osabi.h"
29 #include "target.h"
30 #include "trad-frame.h"
32 #include "obsd-tdep.h"
33 #include "i386-tdep.h"
34 #include "i387-tdep.h"
35 #include "solib-svr4.h"
36 #include "bsd-uthread.h"
38 /* Support for signal handlers. */
40 /* Since OpenBSD 3.2, the sigtramp routine is mapped at a random page
41 in virtual memory. The randomness makes it somewhat tricky to
42 detect it, but fortunately we can rely on the fact that the start
43 of the sigtramp routine is page-aligned. We recognize the
44 trampoline by looking for the code that invokes the sigreturn
45 system call. The offset where we can find that code varies from
46 release to release.
48 By the way, the mapping mentioned above is read-only, so you cannot
49 place a breakpoint in the signal trampoline. */
51 /* Default page size. */
52 static const int i386obsd_page_size = 4096;
54 /* Offset for sigreturn(2). */
55 static const int i386obsd_sigreturn_offset[] = {
56 0x0a, /* OpenBSD 3.2 */
57 0x14, /* OpenBSD 3.6 */
58 0x3a, /* OpenBSD 3.8 */
62 /* Return whether THIS_FRAME corresponds to an OpenBSD sigtramp
63 routine. */
65 static int
66 i386obsd_sigtramp_p (const frame_info_ptr &this_frame)
68 CORE_ADDR pc = get_frame_pc (this_frame);
69 CORE_ADDR start_pc = (pc & ~(i386obsd_page_size - 1));
70 /* The call sequence invoking sigreturn(2). */
71 const gdb_byte sigreturn[] =
73 0xb8,
74 0x67, 0x00, 0x00, 0x00, /* movl $SYS_sigreturn, %eax */
75 0xcd, 0x80 /* int $0x80 */
77 size_t buflen = sizeof sigreturn;
78 const int *offset;
79 gdb_byte *buf;
80 const char *name;
82 /* If the function has a valid symbol name, it isn't a
83 trampoline. */
84 find_pc_partial_function (pc, &name, NULL, NULL);
85 if (name != NULL)
86 return 0;
88 /* If the function lives in a valid section (even without a starting
89 point) it isn't a trampoline. */
90 if (find_pc_section (pc) != NULL)
91 return 0;
93 /* Allocate buffer. */
94 buf = (gdb_byte *) alloca (buflen);
96 /* Loop over all offsets. */
97 for (offset = i386obsd_sigreturn_offset; *offset != -1; offset++)
99 /* If we can't read the instructions, return zero. */
100 if (!safe_frame_unwind_memory (this_frame, start_pc + *offset,
101 {buf, buflen}))
102 return 0;
104 /* Check for sigreturn(2). */
105 if (memcmp (buf, sigreturn, buflen) == 0)
106 return 1;
109 return 0;
112 /* Mapping between the general-purpose registers in `struct reg'
113 format and GDB's register cache layout. */
115 /* From <machine/reg.h>. */
116 static int i386obsd_r_reg_offset[] =
118 0 * 4, /* %eax */
119 1 * 4, /* %ecx */
120 2 * 4, /* %edx */
121 3 * 4, /* %ebx */
122 4 * 4, /* %esp */
123 5 * 4, /* %ebp */
124 6 * 4, /* %esi */
125 7 * 4, /* %edi */
126 8 * 4, /* %eip */
127 9 * 4, /* %eflags */
128 10 * 4, /* %cs */
129 11 * 4, /* %ss */
130 12 * 4, /* %ds */
131 13 * 4, /* %es */
132 14 * 4, /* %fs */
133 15 * 4 /* %gs */
138 /* Sigtramp routine location for OpenBSD 3.1 and earlier releases. */
139 CORE_ADDR i386obsd_sigtramp_start_addr = 0xbfbfdf20;
140 CORE_ADDR i386obsd_sigtramp_end_addr = 0xbfbfdff0;
142 /* From <machine/signal.h>. */
143 int i386obsd_sc_reg_offset[I386_NUM_GREGS] =
145 10 * 4, /* %eax */
146 9 * 4, /* %ecx */
147 8 * 4, /* %edx */
148 7 * 4, /* %ebx */
149 14 * 4, /* %esp */
150 6 * 4, /* %ebp */
151 5 * 4, /* %esi */
152 4 * 4, /* %edi */
153 11 * 4, /* %eip */
154 13 * 4, /* %eflags */
155 12 * 4, /* %cs */
156 15 * 4, /* %ss */
157 3 * 4, /* %ds */
158 2 * 4, /* %es */
159 1 * 4, /* %fs */
160 0 * 4 /* %gs */
163 /* From /usr/src/lib/libpthread/arch/i386/uthread_machdep.c. */
164 static int i386obsd_uthread_reg_offset[] =
166 11 * 4, /* %eax */
167 10 * 4, /* %ecx */
168 9 * 4, /* %edx */
169 8 * 4, /* %ebx */
170 -1, /* %esp */
171 6 * 4, /* %ebp */
172 5 * 4, /* %esi */
173 4 * 4, /* %edi */
174 12 * 4, /* %eip */
175 -1, /* %eflags */
176 13 * 4, /* %cs */
177 -1, /* %ss */
178 3 * 4, /* %ds */
179 2 * 4, /* %es */
180 1 * 4, /* %fs */
181 0 * 4 /* %gs */
184 /* Offset within the thread structure where we can find the saved
185 stack pointer (%esp). */
186 #define I386OBSD_UTHREAD_ESP_OFFSET 176
188 static void
189 i386obsd_supply_uthread (struct regcache *regcache,
190 int regnum, CORE_ADDR addr)
192 struct gdbarch *gdbarch = regcache->arch ();
193 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
194 CORE_ADDR sp_addr = addr + I386OBSD_UTHREAD_ESP_OFFSET;
195 CORE_ADDR sp = 0;
196 gdb_byte buf[4];
197 int i;
199 gdb_assert (regnum >= -1);
201 if (regnum == -1 || regnum == I386_ESP_REGNUM)
203 int offset;
205 /* Fetch stack pointer from thread structure. */
206 sp = read_memory_unsigned_integer (sp_addr, 4, byte_order);
208 /* Adjust the stack pointer such that it looks as if we just
209 returned from _thread_machdep_switch. */
210 offset = i386obsd_uthread_reg_offset[I386_EIP_REGNUM] + 4;
211 store_unsigned_integer (buf, 4, byte_order, sp + offset);
212 regcache->raw_supply (I386_ESP_REGNUM, buf);
215 for (i = 0; i < ARRAY_SIZE (i386obsd_uthread_reg_offset); i++)
217 if (i386obsd_uthread_reg_offset[i] != -1
218 && (regnum == -1 || regnum == i))
220 /* Fetch stack pointer from thread structure (if we didn't
221 do so already). */
222 if (sp == 0)
223 sp = read_memory_unsigned_integer (sp_addr, 4, byte_order);
225 /* Read the saved register from the stack frame. */
226 read_memory (sp + i386obsd_uthread_reg_offset[i], buf, 4);
227 regcache->raw_supply (i, buf);
232 static void
233 i386obsd_collect_uthread (const struct regcache *regcache,
234 int regnum, CORE_ADDR addr)
236 struct gdbarch *gdbarch = regcache->arch ();
237 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
238 CORE_ADDR sp_addr = addr + I386OBSD_UTHREAD_ESP_OFFSET;
239 CORE_ADDR sp = 0;
240 gdb_byte buf[4];
241 int i;
243 gdb_assert (regnum >= -1);
245 if (regnum == -1 || regnum == I386_ESP_REGNUM)
247 int offset;
249 /* Calculate the stack pointer (frame pointer) that will be
250 stored into the thread structure. */
251 offset = i386obsd_uthread_reg_offset[I386_EIP_REGNUM] + 4;
252 regcache->raw_collect (I386_ESP_REGNUM, buf);
253 sp = extract_unsigned_integer (buf, 4, byte_order) - offset;
255 /* Store the stack pointer. */
256 write_memory_unsigned_integer (sp_addr, 4, byte_order, sp);
258 /* The stack pointer was (potentially) modified. Make sure we
259 build a proper stack frame. */
260 regnum = -1;
263 for (i = 0; i < ARRAY_SIZE (i386obsd_uthread_reg_offset); i++)
265 if (i386obsd_uthread_reg_offset[i] != -1
266 && (regnum == -1 || regnum == i))
268 /* Fetch stack pointer from thread structure (if we didn't
269 calculate it already). */
270 if (sp == 0)
271 sp = read_memory_unsigned_integer (sp_addr, 4, byte_order);
273 /* Write the register into the stack frame. */
274 regcache->raw_collect (i, buf);
275 write_memory (sp + i386obsd_uthread_reg_offset[i], buf, 4);
280 /* Kernel debugging support. */
282 /* From <machine/frame.h>. Note that %esp and %ess are only saved in
283 a trap frame when entering the kernel from user space. */
284 static int i386obsd_tf_reg_offset[] =
286 10 * 4, /* %eax */
287 9 * 4, /* %ecx */
288 8 * 4, /* %edx */
289 7 * 4, /* %ebx */
290 -1, /* %esp */
291 6 * 4, /* %ebp */
292 5 * 4, /* %esi */
293 4 * 4, /* %edi */
294 13 * 4, /* %eip */
295 15 * 4, /* %eflags */
296 14 * 4, /* %cs */
297 -1, /* %ss */
298 3 * 4, /* %ds */
299 2 * 4, /* %es */
300 0 * 4, /* %fs */
301 1 * 4 /* %gs */
304 static struct trad_frame_cache *
305 i386obsd_trapframe_cache (const frame_info_ptr &this_frame, void **this_cache)
307 struct gdbarch *gdbarch = get_frame_arch (this_frame);
308 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
309 struct trad_frame_cache *cache;
310 CORE_ADDR func, sp, addr;
311 ULONGEST cs;
312 const char *name;
313 int i;
315 if (*this_cache)
316 return (struct trad_frame_cache *) *this_cache;
318 cache = trad_frame_cache_zalloc (this_frame);
319 *this_cache = cache;
321 func = get_frame_func (this_frame);
322 sp = get_frame_register_unsigned (this_frame, I386_ESP_REGNUM);
324 find_pc_partial_function (func, &name, NULL, NULL);
325 if (name && startswith (name, "Xintr"))
326 addr = sp + 8; /* It's an interrupt frame. */
327 else
328 addr = sp;
330 for (i = 0; i < ARRAY_SIZE (i386obsd_tf_reg_offset); i++)
331 if (i386obsd_tf_reg_offset[i] != -1)
332 trad_frame_set_reg_addr (cache, i, addr + i386obsd_tf_reg_offset[i]);
334 /* Read %cs from trap frame. */
335 addr += i386obsd_tf_reg_offset[I386_CS_REGNUM];
336 cs = read_memory_unsigned_integer (addr, 4, byte_order);
337 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
339 /* Trap from user space; terminate backtrace. */
340 trad_frame_set_id (cache, outer_frame_id);
342 else
344 /* Construct the frame ID using the function start. */
345 trad_frame_set_id (cache, frame_id_build (sp + 8, func));
348 return cache;
351 static void
352 i386obsd_trapframe_this_id (const frame_info_ptr &this_frame,
353 void **this_cache, struct frame_id *this_id)
355 struct trad_frame_cache *cache =
356 i386obsd_trapframe_cache (this_frame, this_cache);
358 trad_frame_get_id (cache, this_id);
361 static struct value *
362 i386obsd_trapframe_prev_register (const frame_info_ptr &this_frame,
363 void **this_cache, int regnum)
365 struct trad_frame_cache *cache =
366 i386obsd_trapframe_cache (this_frame, this_cache);
368 return trad_frame_get_register (cache, this_frame, regnum);
371 static int
372 i386obsd_trapframe_sniffer (const struct frame_unwind *self,
373 const frame_info_ptr &this_frame,
374 void **this_prologue_cache)
376 ULONGEST cs;
377 const char *name;
379 /* Check Current Privilege Level and bail out if we're not executing
380 in kernel space. */
381 cs = get_frame_register_unsigned (this_frame, I386_CS_REGNUM);
382 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
383 return 0;
385 find_pc_partial_function (get_frame_pc (this_frame), &name, NULL, NULL);
386 return (name && (strcmp (name, "calltrap") == 0
387 || strcmp (name, "syscall1") == 0
388 || startswith (name, "Xintr")
389 || startswith (name, "Xsoft")));
392 static const struct frame_unwind i386obsd_trapframe_unwind = {
393 "i386 openbsd trap",
394 /* FIXME: kettenis/20051219: This really is more like an interrupt
395 frame, but SIGTRAMP_FRAME would print <signal handler called>,
396 which really is not what we want here. */
397 NORMAL_FRAME,
398 default_frame_unwind_stop_reason,
399 i386obsd_trapframe_this_id,
400 i386obsd_trapframe_prev_register,
401 NULL,
402 i386obsd_trapframe_sniffer
406 static void
407 i386obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
409 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
411 /* Obviously OpenBSD is BSD-based. */
412 i386bsd_init_abi (info, gdbarch);
413 obsd_init_abi (info, gdbarch);
414 i386_elf_init_abi (info, gdbarch);
416 /* OpenBSD has a different `struct reg'. */
417 tdep->gregset_reg_offset = i386obsd_r_reg_offset;
418 tdep->gregset_num_regs = ARRAY_SIZE (i386obsd_r_reg_offset);
419 tdep->sizeof_gregset = 16 * 4;
421 /* OpenBSD uses -freg-struct-return by default. */
422 tdep->struct_return = reg_struct_return;
424 /* OpenBSD uses a different memory layout. */
425 tdep->sigtramp_start = i386obsd_sigtramp_start_addr;
426 tdep->sigtramp_end = i386obsd_sigtramp_end_addr;
427 tdep->sigtramp_p = i386obsd_sigtramp_p;
429 /* OpenBSD has a `struct sigcontext' that's different from the
430 original 4.3 BSD. */
431 tdep->sc_reg_offset = i386obsd_sc_reg_offset;
432 tdep->sc_num_regs = ARRAY_SIZE (i386obsd_sc_reg_offset);
434 /* OpenBSD provides a user-level threads implementation. */
435 bsd_uthread_set_supply_uthread (gdbarch, i386obsd_supply_uthread);
436 bsd_uthread_set_collect_uthread (gdbarch, i386obsd_collect_uthread);
438 /* Unwind kernel trap frames correctly. */
439 frame_unwind_prepend_unwinder (gdbarch, &i386obsd_trapframe_unwind);
441 /* OpenBSD ELF uses SVR4-style shared libraries. */
442 set_solib_svr4_fetch_link_map_offsets
443 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
446 void _initialize_i386obsd_tdep ();
447 void
448 _initialize_i386obsd_tdep ()
450 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_OPENBSD,
451 i386obsd_init_abi);