Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / sparc-linux-tdep.c
blobfac4c7d773dbd79e0fd02c7253ef8443035500c0
1 /* Target-dependent code for GNU/Linux SPARC.
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 "dwarf2/frame.h"
21 #include "frame.h"
22 #include "frame-unwind.h"
23 #include "gdbtypes.h"
24 #include "regset.h"
25 #include "gdbarch.h"
26 #include "gdbcore.h"
27 #include "osabi.h"
28 #include "regcache.h"
29 #include "solib-svr4.h"
30 #include "symtab.h"
31 #include "trad-frame.h"
32 #include "tramp-frame.h"
33 #include "xml-syscall.h"
34 #include "linux-tdep.h"
36 /* The syscall's XML filename for sparc 32-bit. */
37 #define XML_SYSCALL_FILENAME_SPARC32 "syscalls/sparc-linux.xml"
39 #include "sparc-tdep.h"
41 /* Signal trampoline support. */
43 static void sparc32_linux_sigframe_init (const struct tramp_frame *self,
44 const frame_info_ptr &this_frame,
45 struct trad_frame_cache *this_cache,
46 CORE_ADDR func);
48 /* GNU/Linux has two flavors of signals. Normal signal handlers, and
49 "realtime" (RT) signals. The RT signals can provide additional
50 information to the signal handler if the SA_SIGINFO flag is set
51 when establishing a signal handler using `sigaction'. It is not
52 unlikely that future versions of GNU/Linux will support SA_SIGINFO
53 for normal signals too. */
55 /* When the sparc Linux kernel calls a signal handler and the
56 SA_RESTORER flag isn't set, the return address points to a bit of
57 code on the stack. This code checks whether the PC appears to be
58 within this bit of code.
60 The instruction sequence for normal signals is encoded below.
61 Checking for the code sequence should be somewhat reliable, because
62 the effect is to call the system call sigreturn. This is unlikely
63 to occur anywhere other than a signal trampoline. */
65 static const struct tramp_frame sparc32_linux_sigframe =
67 SIGTRAMP_FRAME,
70 { 0x821020d8, ULONGEST_MAX }, /* mov __NR_sigreturn, %g1 */
71 { 0x91d02010, ULONGEST_MAX }, /* ta 0x10 */
72 { TRAMP_SENTINEL_INSN, ULONGEST_MAX }
74 sparc32_linux_sigframe_init
77 /* The instruction sequence for RT signals is slightly different. The
78 effect is to call the system call rt_sigreturn. */
80 static const struct tramp_frame sparc32_linux_rt_sigframe =
82 SIGTRAMP_FRAME,
85 { 0x82102065, ULONGEST_MAX }, /* mov __NR_rt_sigreturn, %g1 */
86 { 0x91d02010, ULONGEST_MAX }, /* ta 0x10 */
87 { TRAMP_SENTINEL_INSN, ULONGEST_MAX }
89 sparc32_linux_sigframe_init
92 /* This enum represents the signals' numbers on the SPARC
93 architecture. It just contains the signal definitions which are
94 different from the generic implementation.
96 It is derived from the file <arch/sparc/include/uapi/asm/signal.h>,
97 from the Linux kernel tree. */
99 enum
101 SPARC_LINUX_SIGEMT = 7,
102 SPARC_LINUX_SIGBUS = 10,
103 SPARC_LINUX_SIGSYS = 12,
104 SPARC_LINUX_SIGURG = 16,
105 SPARC_LINUX_SIGSTOP = 17,
106 SPARC_LINUX_SIGTSTP = 18,
107 SPARC_LINUX_SIGCONT = 19,
108 SPARC_LINUX_SIGCHLD = 20,
109 SPARC_LINUX_SIGIO = 23,
110 SPARC_LINUX_SIGPOLL = SPARC_LINUX_SIGIO,
111 SPARC_LINUX_SIGLOST = 29,
112 SPARC_LINUX_SIGPWR = SPARC_LINUX_SIGLOST,
113 SPARC_LINUX_SIGUSR1 = 30,
114 SPARC_LINUX_SIGUSR2 = 31,
117 static void
118 sparc32_linux_sigframe_init (const struct tramp_frame *self,
119 const frame_info_ptr &this_frame,
120 struct trad_frame_cache *this_cache,
121 CORE_ADDR func)
123 CORE_ADDR base, addr, sp_addr;
124 int regnum;
126 base = get_frame_register_unsigned (this_frame, SPARC_O1_REGNUM);
127 if (self == &sparc32_linux_rt_sigframe)
128 base += 128;
130 /* Offsets from <bits/sigcontext.h>. */
132 trad_frame_set_reg_addr (this_cache, SPARC32_PSR_REGNUM, base + 0);
133 trad_frame_set_reg_addr (this_cache, SPARC32_PC_REGNUM, base + 4);
134 trad_frame_set_reg_addr (this_cache, SPARC32_NPC_REGNUM, base + 8);
135 trad_frame_set_reg_addr (this_cache, SPARC32_Y_REGNUM, base + 12);
137 /* Since %g0 is always zero, keep the identity encoding. */
138 addr = base + 20;
139 sp_addr = base + 16 + ((SPARC_SP_REGNUM - SPARC_G0_REGNUM) * 4);
140 for (regnum = SPARC_G1_REGNUM; regnum <= SPARC_O7_REGNUM; regnum++)
142 trad_frame_set_reg_addr (this_cache, regnum, addr);
143 addr += 4;
146 base = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
147 addr = get_frame_memory_unsigned (this_frame, sp_addr, 4);
149 for (regnum = SPARC_L0_REGNUM; regnum <= SPARC_I7_REGNUM; regnum++)
151 trad_frame_set_reg_addr (this_cache, regnum, addr);
152 addr += 4;
154 trad_frame_set_id (this_cache, frame_id_build (base, func));
157 /* Return the address of a system call's alternative return
158 address. */
160 static CORE_ADDR
161 sparc32_linux_step_trap (const frame_info_ptr &frame, unsigned long insn)
163 if (insn == 0x91d02010)
165 ULONGEST sc_num = get_frame_register_unsigned (frame, SPARC_G1_REGNUM);
167 /* __NR_rt_sigreturn is 101 and __NR_sigreturn is 216. */
168 if (sc_num == 101 || sc_num == 216)
170 struct gdbarch *gdbarch = get_frame_arch (frame);
171 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
173 ULONGEST sp, pc_offset;
175 sp = get_frame_register_unsigned (frame, SPARC_SP_REGNUM);
177 /* The kernel puts the sigreturn registers on the stack,
178 and this is where the signal unwinding state is take from
179 when returning from a signal.
181 For __NR_sigreturn, this register area sits 96 bytes from
182 the base of the stack. The saved PC sits 4 bytes into the
183 sigreturn register save area.
185 For __NR_rt_sigreturn a siginfo_t, which is 128 bytes, sits
186 right before the sigreturn register save area. */
188 pc_offset = 96 + 4;
189 if (sc_num == 101)
190 pc_offset += 128;
192 return read_memory_unsigned_integer (sp + pc_offset, 4, byte_order);
196 return 0;
200 const struct sparc_gregmap sparc32_linux_core_gregmap =
202 32 * 4, /* %psr */
203 33 * 4, /* %pc */
204 34 * 4, /* %npc */
205 35 * 4, /* %y */
206 -1, /* %wim */
207 -1, /* %tbr */
208 1 * 4, /* %g1 */
209 16 * 4, /* %l0 */
210 4, /* y size */
214 static void
215 sparc32_linux_supply_core_gregset (const struct regset *regset,
216 struct regcache *regcache,
217 int regnum, const void *gregs, size_t len)
219 sparc32_supply_gregset (&sparc32_linux_core_gregmap,
220 regcache, regnum, gregs);
223 static void
224 sparc32_linux_collect_core_gregset (const struct regset *regset,
225 const struct regcache *regcache,
226 int regnum, void *gregs, size_t len)
228 sparc32_collect_gregset (&sparc32_linux_core_gregmap,
229 regcache, regnum, gregs);
232 static void
233 sparc32_linux_supply_core_fpregset (const struct regset *regset,
234 struct regcache *regcache,
235 int regnum, const void *fpregs, size_t len)
237 sparc32_supply_fpregset (&sparc32_bsd_fpregmap, regcache, regnum, fpregs);
240 static void
241 sparc32_linux_collect_core_fpregset (const struct regset *regset,
242 const struct regcache *regcache,
243 int regnum, void *fpregs, size_t len)
245 sparc32_collect_fpregset (&sparc32_bsd_fpregmap, regcache, regnum, fpregs);
248 /* Set the program counter for process PTID to PC. */
250 #define PSR_SYSCALL 0x00004000
252 static void
253 sparc_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
255 gdbarch *arch = regcache->arch ();
256 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (arch);
257 ULONGEST psr;
259 regcache_cooked_write_unsigned (regcache, tdep->pc_regnum, pc);
260 regcache_cooked_write_unsigned (regcache, tdep->npc_regnum, pc + 4);
262 /* Clear the "in syscall" bit to prevent the kernel from
263 messing with the PCs we just installed, if we happen to be
264 within an interrupted system call that the kernel wants to
265 restart.
267 Note that after we return from the dummy call, the PSR et al.
268 registers will be automatically restored, and the kernel
269 continues to restart the system call at this point. */
270 regcache_cooked_read_unsigned (regcache, SPARC32_PSR_REGNUM, &psr);
271 psr &= ~PSR_SYSCALL;
272 regcache_cooked_write_unsigned (regcache, SPARC32_PSR_REGNUM, psr);
275 static LONGEST
276 sparc32_linux_get_syscall_number (struct gdbarch *gdbarch,
277 thread_info *thread)
279 struct regcache *regcache = get_thread_regcache (thread);
280 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
281 /* The content of a register. */
282 gdb_byte buf[4];
283 /* The result. */
284 LONGEST ret;
286 /* Getting the system call number from the register.
287 When dealing with the sparc architecture, this information
288 is stored at the %g1 register. */
289 regcache->cooked_read (SPARC_G1_REGNUM, buf);
291 ret = extract_signed_integer (buf, 4, byte_order);
293 return ret;
296 /* Implementation of `gdbarch_gdb_signal_from_target', as defined in
297 gdbarch.h. */
299 static enum gdb_signal
300 sparc32_linux_gdb_signal_from_target (struct gdbarch *gdbarch,
301 int signal)
303 switch (signal)
305 case SPARC_LINUX_SIGEMT:
306 return GDB_SIGNAL_EMT;
308 case SPARC_LINUX_SIGBUS:
309 return GDB_SIGNAL_BUS;
311 case SPARC_LINUX_SIGSYS:
312 return GDB_SIGNAL_SYS;
314 case SPARC_LINUX_SIGURG:
315 return GDB_SIGNAL_URG;
317 case SPARC_LINUX_SIGSTOP:
318 return GDB_SIGNAL_STOP;
320 case SPARC_LINUX_SIGTSTP:
321 return GDB_SIGNAL_TSTP;
323 case SPARC_LINUX_SIGCONT:
324 return GDB_SIGNAL_CONT;
326 case SPARC_LINUX_SIGCHLD:
327 return GDB_SIGNAL_CHLD;
329 /* No way to differentiate between SIGIO and SIGPOLL.
330 Therefore, we just handle the first one. */
331 case SPARC_LINUX_SIGIO:
332 return GDB_SIGNAL_IO;
334 /* No way to differentiate between SIGLOST and SIGPWR.
335 Therefore, we just handle the first one. */
336 case SPARC_LINUX_SIGLOST:
337 return GDB_SIGNAL_LOST;
339 case SPARC_LINUX_SIGUSR1:
340 return GDB_SIGNAL_USR1;
342 case SPARC_LINUX_SIGUSR2:
343 return GDB_SIGNAL_USR2;
346 return linux_gdb_signal_from_target (gdbarch, signal);
349 /* Implementation of `gdbarch_gdb_signal_to_target', as defined in
350 gdbarch.h. */
352 static int
353 sparc32_linux_gdb_signal_to_target (struct gdbarch *gdbarch,
354 enum gdb_signal signal)
356 switch (signal)
358 case GDB_SIGNAL_EMT:
359 return SPARC_LINUX_SIGEMT;
361 case GDB_SIGNAL_BUS:
362 return SPARC_LINUX_SIGBUS;
364 case GDB_SIGNAL_SYS:
365 return SPARC_LINUX_SIGSYS;
367 case GDB_SIGNAL_URG:
368 return SPARC_LINUX_SIGURG;
370 case GDB_SIGNAL_STOP:
371 return SPARC_LINUX_SIGSTOP;
373 case GDB_SIGNAL_TSTP:
374 return SPARC_LINUX_SIGTSTP;
376 case GDB_SIGNAL_CONT:
377 return SPARC_LINUX_SIGCONT;
379 case GDB_SIGNAL_CHLD:
380 return SPARC_LINUX_SIGCHLD;
382 case GDB_SIGNAL_IO:
383 return SPARC_LINUX_SIGIO;
385 case GDB_SIGNAL_POLL:
386 return SPARC_LINUX_SIGPOLL;
388 case GDB_SIGNAL_LOST:
389 return SPARC_LINUX_SIGLOST;
391 case GDB_SIGNAL_PWR:
392 return SPARC_LINUX_SIGPWR;
394 case GDB_SIGNAL_USR1:
395 return SPARC_LINUX_SIGUSR1;
397 case GDB_SIGNAL_USR2:
398 return SPARC_LINUX_SIGUSR2;
401 return linux_gdb_signal_to_target (gdbarch, signal);
406 static const struct regset sparc32_linux_gregset =
408 NULL,
409 sparc32_linux_supply_core_gregset,
410 sparc32_linux_collect_core_gregset
413 static const struct regset sparc32_linux_fpregset =
415 NULL,
416 sparc32_linux_supply_core_fpregset,
417 sparc32_linux_collect_core_fpregset
420 static void
421 sparc32_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
423 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch);
425 linux_init_abi (info, gdbarch, 0);
427 tdep->gregset = &sparc32_linux_gregset;
428 tdep->sizeof_gregset = 152;
430 tdep->fpregset = &sparc32_linux_fpregset;
431 tdep->sizeof_fpregset = 396;
433 tramp_frame_prepend_unwinder (gdbarch, &sparc32_linux_sigframe);
434 tramp_frame_prepend_unwinder (gdbarch, &sparc32_linux_rt_sigframe);
436 /* GNU/Linux has SVR4-style shared libraries... */
437 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
438 set_solib_svr4_fetch_link_map_offsets
439 (gdbarch, linux_ilp32_fetch_link_map_offsets);
441 /* ...which means that we need some special handling when doing
442 prologue analysis. */
443 tdep->plt_entry_size = 12;
445 /* Enable TLS support. */
446 set_gdbarch_fetch_tls_load_module_address (gdbarch,
447 svr4_fetch_objfile_link_map);
449 /* Make sure we can single-step over signal return system calls. */
450 tdep->step_trap = sparc32_linux_step_trap;
452 /* Hook in the DWARF CFI frame unwinder. */
453 dwarf2_append_unwinders (gdbarch);
455 set_gdbarch_write_pc (gdbarch, sparc_linux_write_pc);
457 /* Functions for 'catch syscall'. */
458 set_xml_syscall_file_name (gdbarch, XML_SYSCALL_FILENAME_SPARC32);
459 set_gdbarch_get_syscall_number (gdbarch,
460 sparc32_linux_get_syscall_number);
462 set_gdbarch_gdb_signal_from_target (gdbarch,
463 sparc32_linux_gdb_signal_from_target);
464 set_gdbarch_gdb_signal_to_target (gdbarch,
465 sparc32_linux_gdb_signal_to_target);
468 void _initialize_sparc_linux_tdep ();
469 void
470 _initialize_sparc_linux_tdep ()
472 gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_LINUX,
473 sparc32_linux_init_abi);