Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / amd64-obsd-tdep.c
blobf6f63bbf8ed0d12a0c76dc5edd46076e1dd1fcbb
1 /* Target-dependent code for OpenBSD/amd64.
3 Copyright (C) 2003-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 "frame.h"
21 #include "frame-unwind.h"
22 #include "gdbcore.h"
23 #include "symtab.h"
24 #include "objfiles.h"
25 #include "osabi.h"
26 #include "regcache.h"
27 #include "regset.h"
28 #include "target.h"
29 #include "trad-frame.h"
31 #include "obsd-tdep.h"
32 #include "amd64-tdep.h"
33 #include "i387-tdep.h"
34 #include "gdbsupport/x86-xstate.h"
35 #include "solib-svr4.h"
36 #include "bsd-uthread.h"
38 /* Support for signal handlers. */
40 /* Default page size. */
41 static const int amd64obsd_page_size = 4096;
43 /* Return whether THIS_FRAME corresponds to an OpenBSD sigtramp
44 routine. */
46 static int
47 amd64obsd_sigtramp_p (const frame_info_ptr &this_frame)
49 CORE_ADDR pc = get_frame_pc (this_frame);
50 CORE_ADDR start_pc = (pc & ~(amd64obsd_page_size - 1));
51 const gdb_byte osigreturn[] =
53 0x48, 0xc7, 0xc0,
54 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
55 0xcd, 0x80 /* int $0x80 */
57 const gdb_byte sigreturn[] =
59 0x48, 0xc7, 0xc0,
60 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
61 0x0f, 0x05 /* syscall */
63 size_t buflen = (sizeof sigreturn) + 1;
64 gdb_byte *buf;
65 const char *name;
67 /* If the function has a valid symbol name, it isn't a
68 trampoline. */
69 find_pc_partial_function (pc, &name, NULL, NULL);
70 if (name != NULL)
71 return 0;
73 /* If the function lives in a valid section (even without a starting
74 point) it isn't a trampoline. */
75 if (find_pc_section (pc) != NULL)
76 return 0;
78 /* If we can't read the instructions at START_PC, return zero. */
79 buf = (gdb_byte *) alloca ((sizeof sigreturn) + 1);
80 if (!safe_frame_unwind_memory (this_frame, start_pc + 6, {buf, buflen}))
81 return 0;
83 /* Check for sigreturn(2). Depending on how the assembler encoded
84 the `movq %rsp, %rdi' instruction, the code starts at offset 6 or
85 7. OpenBSD 5.0 and later use the `syscall' instruction. Older
86 versions use `int $0x80'. Check for both. */
87 if (memcmp (buf, sigreturn, sizeof sigreturn)
88 && memcmp (buf + 1, sigreturn, sizeof sigreturn)
89 && memcmp (buf, osigreturn, sizeof osigreturn)
90 && memcmp (buf + 1, osigreturn, sizeof osigreturn))
91 return 0;
93 return 1;
96 /* Assuming THIS_FRAME is for a BSD sigtramp routine, return the
97 address of the associated sigcontext structure. */
99 static CORE_ADDR
100 amd64obsd_sigcontext_addr (const frame_info_ptr &this_frame)
102 CORE_ADDR pc = get_frame_pc (this_frame);
103 ULONGEST offset = (pc & (amd64obsd_page_size - 1));
105 /* The %rsp register points at `struct sigcontext' upon entry of a
106 signal trampoline. The relevant part of the trampoline is
108 call *%rax
109 movq %rsp, %rdi
110 pushq %rdi
111 movq $SYS_sigreturn,%rax
112 int $0x80
114 (see /usr/src/sys/arch/amd64/amd64/locore.S). The `pushq'
115 instruction clobbers %rsp, but its value is saved in `%rdi'. */
117 if (offset > 5)
118 return get_frame_register_unsigned (this_frame, AMD64_RDI_REGNUM);
119 else
120 return get_frame_register_unsigned (this_frame, AMD64_RSP_REGNUM);
123 /* OpenBSD 3.5 or later. */
125 /* Mapping between the general-purpose registers in `struct reg'
126 format and GDB's register cache layout. */
128 /* From <machine/reg.h>. */
129 int amd64obsd_r_reg_offset[] =
131 14 * 8, /* %rax */
132 13 * 8, /* %rbx */
133 3 * 8, /* %rcx */
134 2 * 8, /* %rdx */
135 1 * 8, /* %rsi */
136 0 * 8, /* %rdi */
137 12 * 8, /* %rbp */
138 15 * 8, /* %rsp */
139 4 * 8, /* %r8 .. */
140 5 * 8,
141 6 * 8,
142 7 * 8,
143 8 * 8,
144 9 * 8,
145 10 * 8,
146 11 * 8, /* ... %r15 */
147 16 * 8, /* %rip */
148 17 * 8, /* %eflags */
149 18 * 8, /* %cs */
150 19 * 8, /* %ss */
151 20 * 8, /* %ds */
152 21 * 8, /* %es */
153 22 * 8, /* %fs */
154 23 * 8 /* %gs */
157 /* From <machine/signal.h>. */
158 static int amd64obsd_sc_reg_offset[] =
160 14 * 8, /* %rax */
161 13 * 8, /* %rbx */
162 3 * 8, /* %rcx */
163 2 * 8, /* %rdx */
164 1 * 8, /* %rsi */
165 0 * 8, /* %rdi */
166 12 * 8, /* %rbp */
167 24 * 8, /* %rsp */
168 4 * 8, /* %r8 ... */
169 5 * 8,
170 6 * 8,
171 7 * 8,
172 8 * 8,
173 9 * 8,
174 10 * 8,
175 11 * 8, /* ... %r15 */
176 21 * 8, /* %rip */
177 23 * 8, /* %eflags */
178 22 * 8, /* %cs */
179 25 * 8, /* %ss */
180 18 * 8, /* %ds */
181 17 * 8, /* %es */
182 16 * 8, /* %fs */
183 15 * 8 /* %gs */
186 /* From /usr/src/lib/libpthread/arch/amd64/uthread_machdep.c. */
187 static int amd64obsd_uthread_reg_offset[] =
189 19 * 8, /* %rax */
190 16 * 8, /* %rbx */
191 18 * 8, /* %rcx */
192 17 * 8, /* %rdx */
193 14 * 8, /* %rsi */
194 13 * 8, /* %rdi */
195 15 * 8, /* %rbp */
196 -1, /* %rsp */
197 12 * 8, /* %r8 ... */
198 11 * 8,
199 10 * 8,
200 9 * 8,
201 8 * 8,
202 7 * 8,
203 6 * 8,
204 5 * 8, /* ... %r15 */
205 20 * 8, /* %rip */
206 4 * 8, /* %eflags */
207 21 * 8, /* %cs */
208 -1, /* %ss */
209 3 * 8, /* %ds */
210 2 * 8, /* %es */
211 1 * 8, /* %fs */
212 0 * 8 /* %gs */
215 /* Offset within the thread structure where we can find the saved
216 stack pointer (%esp). */
217 #define AMD64OBSD_UTHREAD_RSP_OFFSET 400
219 static void
220 amd64obsd_supply_uthread (struct regcache *regcache,
221 int regnum, CORE_ADDR addr)
223 struct gdbarch *gdbarch = regcache->arch ();
224 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
225 CORE_ADDR sp_addr = addr + AMD64OBSD_UTHREAD_RSP_OFFSET;
226 CORE_ADDR sp = 0;
227 gdb_byte buf[8];
228 int i;
230 gdb_assert (regnum >= -1);
232 if (regnum == -1 || regnum == AMD64_RSP_REGNUM)
234 int offset;
236 /* Fetch stack pointer from thread structure. */
237 sp = read_memory_unsigned_integer (sp_addr, 8, byte_order);
239 /* Adjust the stack pointer such that it looks as if we just
240 returned from _thread_machdep_switch. */
241 offset = amd64obsd_uthread_reg_offset[AMD64_RIP_REGNUM] + 8;
242 store_unsigned_integer (buf, 8, byte_order, sp + offset);
243 regcache->raw_supply (AMD64_RSP_REGNUM, buf);
246 for (i = 0; i < ARRAY_SIZE (amd64obsd_uthread_reg_offset); i++)
248 if (amd64obsd_uthread_reg_offset[i] != -1
249 && (regnum == -1 || regnum == i))
251 /* Fetch stack pointer from thread structure (if we didn't
252 do so already). */
253 if (sp == 0)
254 sp = read_memory_unsigned_integer (sp_addr, 8, byte_order);
256 /* Read the saved register from the stack frame. */
257 read_memory (sp + amd64obsd_uthread_reg_offset[i], buf, 8);
258 regcache->raw_supply (i, buf);
263 static void
264 amd64obsd_collect_uthread (const struct regcache *regcache,
265 int regnum, CORE_ADDR addr)
267 struct gdbarch *gdbarch = regcache->arch ();
268 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
269 CORE_ADDR sp_addr = addr + AMD64OBSD_UTHREAD_RSP_OFFSET;
270 CORE_ADDR sp = 0;
271 gdb_byte buf[8];
272 int i;
274 gdb_assert (regnum >= -1);
276 if (regnum == -1 || regnum == AMD64_RSP_REGNUM)
278 int offset;
280 /* Calculate the stack pointer (frame pointer) that will be
281 stored into the thread structure. */
282 offset = amd64obsd_uthread_reg_offset[AMD64_RIP_REGNUM] + 8;
283 regcache->raw_collect (AMD64_RSP_REGNUM, buf);
284 sp = extract_unsigned_integer (buf, 8, byte_order) - offset;
286 /* Store the stack pointer. */
287 write_memory_unsigned_integer (sp_addr, 8, byte_order, sp);
289 /* The stack pointer was (potentially) modified. Make sure we
290 build a proper stack frame. */
291 regnum = -1;
294 for (i = 0; i < ARRAY_SIZE (amd64obsd_uthread_reg_offset); i++)
296 if (amd64obsd_uthread_reg_offset[i] != -1
297 && (regnum == -1 || regnum == i))
299 /* Fetch stack pointer from thread structure (if we didn't
300 calculate it already). */
301 if (sp == 0)
302 sp = read_memory_unsigned_integer (sp_addr, 8, byte_order);
304 /* Write the register into the stack frame. */
305 regcache->raw_collect (i, buf);
306 write_memory (sp + amd64obsd_uthread_reg_offset[i], buf, 8);
310 /* Kernel debugging support. */
312 /* From <machine/frame.h>. Easy since `struct trapframe' matches
313 `struct sigcontext'. */
314 #define amd64obsd_tf_reg_offset amd64obsd_sc_reg_offset
316 static struct trad_frame_cache *
317 amd64obsd_trapframe_cache (const frame_info_ptr &this_frame, void **this_cache)
319 struct gdbarch *gdbarch = get_frame_arch (this_frame);
320 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
321 struct trad_frame_cache *cache;
322 CORE_ADDR func, sp, addr;
323 ULONGEST cs;
324 const char *name;
325 int i;
327 if (*this_cache)
328 return (struct trad_frame_cache *) *this_cache;
330 cache = trad_frame_cache_zalloc (this_frame);
331 *this_cache = cache;
333 func = get_frame_func (this_frame);
334 sp = get_frame_register_unsigned (this_frame, AMD64_RSP_REGNUM);
336 find_pc_partial_function (func, &name, NULL, NULL);
337 if (name && startswith (name, "Xintr"))
338 addr = sp + 8; /* It's an interrupt frame. */
339 else
340 addr = sp;
342 for (i = 0; i < ARRAY_SIZE (amd64obsd_tf_reg_offset); i++)
343 if (amd64obsd_tf_reg_offset[i] != -1)
344 trad_frame_set_reg_addr (cache, i, addr + amd64obsd_tf_reg_offset[i]);
346 /* Read %cs from trap frame. */
347 addr += amd64obsd_tf_reg_offset[AMD64_CS_REGNUM];
348 cs = read_memory_unsigned_integer (addr, 8, byte_order);
349 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
351 /* Trap from user space; terminate backtrace. */
352 trad_frame_set_id (cache, outer_frame_id);
354 else
356 /* Construct the frame ID using the function start. */
357 trad_frame_set_id (cache, frame_id_build (sp + 16, func));
360 return cache;
363 static void
364 amd64obsd_trapframe_this_id (const frame_info_ptr &this_frame,
365 void **this_cache, struct frame_id *this_id)
367 struct trad_frame_cache *cache =
368 amd64obsd_trapframe_cache (this_frame, this_cache);
370 trad_frame_get_id (cache, this_id);
373 static struct value *
374 amd64obsd_trapframe_prev_register (const frame_info_ptr &this_frame,
375 void **this_cache, int regnum)
377 struct trad_frame_cache *cache =
378 amd64obsd_trapframe_cache (this_frame, this_cache);
380 return trad_frame_get_register (cache, this_frame, regnum);
383 static int
384 amd64obsd_trapframe_sniffer (const struct frame_unwind *self,
385 const frame_info_ptr &this_frame,
386 void **this_prologue_cache)
388 ULONGEST cs;
389 const char *name;
391 /* Check Current Privilege Level and bail out if we're not executing
392 in kernel space. */
393 cs = get_frame_register_unsigned (this_frame, AMD64_CS_REGNUM);
394 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
395 return 0;
397 find_pc_partial_function (get_frame_pc (this_frame), &name, NULL, NULL);
398 return (name && ((strcmp (name, "calltrap") == 0)
399 || (strcmp (name, "osyscall1") == 0)
400 || (strcmp (name, "Xsyscall") == 0)
401 || (startswith (name, "Xintr"))));
404 static const struct frame_unwind amd64obsd_trapframe_unwind =
406 /* FIXME: kettenis/20051219: This really is more like an interrupt
407 frame, but SIGTRAMP_FRAME would print <signal handler called>,
408 which really is not what we want here. */
409 "amd64 openbsd trap",
410 NORMAL_FRAME,
411 default_frame_unwind_stop_reason,
412 amd64obsd_trapframe_this_id,
413 amd64obsd_trapframe_prev_register,
414 NULL,
415 amd64obsd_trapframe_sniffer
419 static void
420 amd64obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
422 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
424 amd64_init_abi (info, gdbarch,
425 amd64_target_description (X86_XSTATE_SSE_MASK, true));
426 obsd_init_abi (info, gdbarch);
428 /* Initialize general-purpose register set details. */
429 tdep->gregset_reg_offset = amd64obsd_r_reg_offset;
430 tdep->gregset_num_regs = ARRAY_SIZE (amd64obsd_r_reg_offset);
431 tdep->sizeof_gregset = 24 * 8;
433 tdep->jb_pc_offset = 7 * 8;
435 tdep->sigtramp_p = amd64obsd_sigtramp_p;
436 tdep->sigcontext_addr = amd64obsd_sigcontext_addr;
437 tdep->sc_reg_offset = amd64obsd_sc_reg_offset;
438 tdep->sc_num_regs = ARRAY_SIZE (amd64obsd_sc_reg_offset);
440 /* OpenBSD provides a user-level threads implementation. */
441 bsd_uthread_set_supply_uthread (gdbarch, amd64obsd_supply_uthread);
442 bsd_uthread_set_collect_uthread (gdbarch, amd64obsd_collect_uthread);
444 /* OpenBSD uses SVR4-style shared libraries. */
445 set_solib_svr4_fetch_link_map_offsets
446 (gdbarch, svr4_lp64_fetch_link_map_offsets);
448 /* Unwind kernel trap frames correctly. */
449 frame_unwind_prepend_unwinder (gdbarch, &amd64obsd_trapframe_unwind);
452 void _initialize_amd64obsd_tdep ();
453 void
454 _initialize_amd64obsd_tdep ()
456 /* The OpenBSD/amd64 native dependent code makes this assumption. */
457 gdb_assert (ARRAY_SIZE (amd64obsd_r_reg_offset) == AMD64_NUM_GREGS);
459 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
460 GDB_OSABI_OPENBSD, amd64obsd_init_abi);