1 /* Target-dependent code for FreeBSD/mips.
3 Copyright (C) 2017-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/>. */
22 #include "trad-frame.h"
23 #include "tramp-frame.h"
25 #include "fbsd-tdep.h"
26 #include "mips-tdep.h"
27 #include "mips-fbsd-tdep.h"
29 #include "solib-svr4.h"
31 /* Core file support. */
33 /* Number of registers in `struct reg' from <machine/reg.h>. The
34 first 38 follow the standard MIPS layout. The 39th holds
35 IC_INT_REG on RM7K and RM9K processors. The 40th is a dummy for
37 #define MIPS_FBSD_NUM_GREGS 40
39 /* Number of registers in `struct fpreg' from <machine/reg.h>. The
40 first 32 hold floating point registers. 33 holds the FSR. The
41 34th holds FIR on FreeBSD 12.0 and newer kernels. On older kernels
42 it was a zero-filled dummy for padding. */
43 #define MIPS_FBSD_NUM_FPREGS 34
45 /* Supply a single register. The register size might not match, so use
46 regcache->raw_supply_integer (). */
49 mips_fbsd_supply_reg (struct regcache
*regcache
, int regnum
, const void *addr
,
52 regcache
->raw_supply_integer (regnum
, (const gdb_byte
*) addr
, len
, true);
55 /* Collect a single register. The register size might not match, so use
56 regcache->raw_collect_integer (). */
59 mips_fbsd_collect_reg (const struct regcache
*regcache
, int regnum
, void *addr
,
62 regcache
->raw_collect_integer (regnum
, (gdb_byte
*) addr
, len
, true);
65 /* Supply the floating-point registers stored in FPREGS to REGCACHE.
66 Each floating-point register in FPREGS is REGSIZE bytes in
70 mips_fbsd_supply_fpregs (struct regcache
*regcache
, int regnum
,
71 const void *fpregs
, size_t regsize
)
73 struct gdbarch
*gdbarch
= regcache
->arch ();
74 const gdb_byte
*regs
= (const gdb_byte
*) fpregs
;
77 fp0num
= mips_regnum (gdbarch
)->fp0
;
78 for (i
= 0; i
<= 32; i
++)
79 if (regnum
== fp0num
+ i
|| regnum
== -1)
80 mips_fbsd_supply_reg (regcache
, fp0num
+ i
,
81 regs
+ i
* regsize
, regsize
);
82 if (regnum
== mips_regnum (gdbarch
)->fp_control_status
|| regnum
== -1)
83 mips_fbsd_supply_reg (regcache
, mips_regnum (gdbarch
)->fp_control_status
,
84 regs
+ 32 * regsize
, regsize
);
85 if ((regnum
== mips_regnum (gdbarch
)->fp_implementation_revision
87 && extract_unsigned_integer (regs
+ 33 * regsize
, regsize
,
88 gdbarch_byte_order (gdbarch
)) != 0)
89 mips_fbsd_supply_reg (regcache
,
90 mips_regnum (gdbarch
)->fp_implementation_revision
,
91 regs
+ 33 * regsize
, regsize
);
94 /* Supply the general-purpose registers stored in GREGS to REGCACHE.
95 Each general-purpose register in GREGS is REGSIZE bytes in
99 mips_fbsd_supply_gregs (struct regcache
*regcache
, int regnum
,
100 const void *gregs
, size_t regsize
)
102 struct gdbarch
*gdbarch
= regcache
->arch ();
103 const gdb_byte
*regs
= (const gdb_byte
*) gregs
;
106 for (i
= 0; i
<= mips_regnum (gdbarch
)->pc
; i
++)
107 if (regnum
== i
|| regnum
== -1)
108 mips_fbsd_supply_reg (regcache
, i
, regs
+ i
* regsize
, regsize
);
111 /* Collect the floating-point registers from REGCACHE and store them
112 in FPREGS. Each floating-point register in FPREGS is REGSIZE bytes
116 mips_fbsd_collect_fpregs (const struct regcache
*regcache
, int regnum
,
117 void *fpregs
, size_t regsize
)
119 struct gdbarch
*gdbarch
= regcache
->arch ();
120 gdb_byte
*regs
= (gdb_byte
*) fpregs
;
123 fp0num
= mips_regnum (gdbarch
)->fp0
;
124 for (i
= 0; i
< 32; i
++)
125 if (regnum
== fp0num
+ i
|| regnum
== -1)
126 mips_fbsd_collect_reg (regcache
, fp0num
+ i
,
127 regs
+ i
* regsize
, regsize
);
128 if (regnum
== mips_regnum (gdbarch
)->fp_control_status
|| regnum
== -1)
129 mips_fbsd_collect_reg (regcache
, mips_regnum (gdbarch
)->fp_control_status
,
130 regs
+ 32 * regsize
, regsize
);
131 if (regnum
== mips_regnum (gdbarch
)->fp_implementation_revision
133 mips_fbsd_collect_reg (regcache
,
134 mips_regnum (gdbarch
)->fp_implementation_revision
,
135 regs
+ 33 * regsize
, regsize
);
138 /* Collect the general-purpose registers from REGCACHE and store them
139 in GREGS. Each general-purpose register in GREGS is REGSIZE bytes
143 mips_fbsd_collect_gregs (const struct regcache
*regcache
, int regnum
,
144 void *gregs
, size_t regsize
)
146 struct gdbarch
*gdbarch
= regcache
->arch ();
147 gdb_byte
*regs
= (gdb_byte
*) gregs
;
150 for (i
= 0; i
<= mips_regnum (gdbarch
)->pc
; i
++)
151 if (regnum
== i
|| regnum
== -1)
152 mips_fbsd_collect_reg (regcache
, i
, regs
+ i
* regsize
, regsize
);
155 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
156 in the floating-point register set REGSET to register cache
157 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
160 mips_fbsd_supply_fpregset (const struct regset
*regset
,
161 struct regcache
*regcache
,
162 int regnum
, const void *fpregs
, size_t len
)
164 size_t regsize
= mips_abi_regsize (regcache
->arch ());
166 gdb_assert (len
>= MIPS_FBSD_NUM_FPREGS
* regsize
);
168 mips_fbsd_supply_fpregs (regcache
, regnum
, fpregs
, regsize
);
171 /* Collect register REGNUM from the register cache REGCACHE and store
172 it in the buffer specified by FPREGS and LEN in the floating-point
173 register set REGSET. If REGNUM is -1, do this for all registers in
177 mips_fbsd_collect_fpregset (const struct regset
*regset
,
178 const struct regcache
*regcache
,
179 int regnum
, void *fpregs
, size_t len
)
181 size_t regsize
= mips_abi_regsize (regcache
->arch ());
183 gdb_assert (len
>= MIPS_FBSD_NUM_FPREGS
* regsize
);
185 mips_fbsd_collect_fpregs (regcache
, regnum
, fpregs
, regsize
);
188 /* Supply register REGNUM from the buffer specified by GREGS and LEN
189 in the general-purpose register set REGSET to register cache
190 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
193 mips_fbsd_supply_gregset (const struct regset
*regset
,
194 struct regcache
*regcache
, int regnum
,
195 const void *gregs
, size_t len
)
197 size_t regsize
= mips_abi_regsize (regcache
->arch ());
199 gdb_assert (len
>= MIPS_FBSD_NUM_GREGS
* regsize
);
201 mips_fbsd_supply_gregs (regcache
, regnum
, gregs
, regsize
);
204 /* Collect register REGNUM from the register cache REGCACHE and store
205 it in the buffer specified by GREGS and LEN in the general-purpose
206 register set REGSET. If REGNUM is -1, do this for all registers in
210 mips_fbsd_collect_gregset (const struct regset
*regset
,
211 const struct regcache
*regcache
,
212 int regnum
, void *gregs
, size_t len
)
214 size_t regsize
= mips_abi_regsize (regcache
->arch ());
216 gdb_assert (len
>= MIPS_FBSD_NUM_GREGS
* regsize
);
218 mips_fbsd_collect_gregs (regcache
, regnum
, gregs
, regsize
);
221 /* FreeBSD/mips register sets. */
223 static const struct regset mips_fbsd_gregset
=
226 mips_fbsd_supply_gregset
,
227 mips_fbsd_collect_gregset
,
230 static const struct regset mips_fbsd_fpregset
=
233 mips_fbsd_supply_fpregset
,
234 mips_fbsd_collect_fpregset
,
237 /* Iterate over core file register note sections. */
240 mips_fbsd_iterate_over_regset_sections (struct gdbarch
*gdbarch
,
241 iterate_over_regset_sections_cb
*cb
,
243 const struct regcache
*regcache
)
245 size_t regsize
= mips_abi_regsize (gdbarch
);
247 cb (".reg", MIPS_FBSD_NUM_GREGS
* regsize
, MIPS_FBSD_NUM_GREGS
* regsize
,
248 &mips_fbsd_gregset
, NULL
, cb_data
);
249 cb (".reg2", MIPS_FBSD_NUM_FPREGS
* regsize
, MIPS_FBSD_NUM_FPREGS
* regsize
,
250 &mips_fbsd_fpregset
, NULL
, cb_data
);
253 /* Signal trampoline support. */
255 #define FBSD_SYS_sigreturn 417
257 #define MIPS_INST_LI_V0_SIGRETURN 0x24020000 + FBSD_SYS_sigreturn
258 #define MIPS_INST_SYSCALL 0x0000000c
259 #define MIPS_INST_BREAK 0x0000000d
261 #define O32_SIGFRAME_UCONTEXT_OFFSET (16)
262 #define O32_SIGSET_T_SIZE (16)
264 #define O32_UCONTEXT_ONSTACK (O32_SIGSET_T_SIZE)
265 #define O32_UCONTEXT_PC (O32_UCONTEXT_ONSTACK + 4)
266 #define O32_UCONTEXT_REGS (O32_UCONTEXT_PC + 4)
267 #define O32_UCONTEXT_SR (O32_UCONTEXT_REGS + 4 * 32)
268 #define O32_UCONTEXT_LO (O32_UCONTEXT_SR + 4)
269 #define O32_UCONTEXT_HI (O32_UCONTEXT_LO + 4)
270 #define O32_UCONTEXT_FPUSED (O32_UCONTEXT_HI + 4)
271 #define O32_UCONTEXT_FPREGS (O32_UCONTEXT_FPUSED + 4)
273 #define O32_UCONTEXT_REG_SIZE 4
276 mips_fbsd_sigframe_init (const struct tramp_frame
*self
,
277 const frame_info_ptr
&this_frame
,
278 struct trad_frame_cache
*cache
,
281 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
282 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
283 CORE_ADDR sp
, ucontext_addr
, addr
;
287 /* We find the appropriate instance of `ucontext_t' at a
288 fixed offset in the signal frame. */
289 sp
= get_frame_register_signed (this_frame
,
290 MIPS_SP_REGNUM
+ gdbarch_num_regs (gdbarch
));
291 ucontext_addr
= sp
+ O32_SIGFRAME_UCONTEXT_OFFSET
;
294 regnum
= mips_regnum (gdbarch
)->pc
;
295 trad_frame_set_reg_addr (cache
,
296 regnum
+ gdbarch_num_regs (gdbarch
),
297 ucontext_addr
+ O32_UCONTEXT_PC
);
300 for (regnum
= MIPS_ZERO_REGNUM
, addr
= ucontext_addr
+ O32_UCONTEXT_REGS
;
301 regnum
<= MIPS_RA_REGNUM
; regnum
++, addr
+= O32_UCONTEXT_REG_SIZE
)
302 trad_frame_set_reg_addr (cache
,
303 regnum
+ gdbarch_num_regs (gdbarch
),
306 regnum
= MIPS_PS_REGNUM
;
307 trad_frame_set_reg_addr (cache
,
308 regnum
+ gdbarch_num_regs (gdbarch
),
309 ucontext_addr
+ O32_UCONTEXT_SR
);
312 regnum
= mips_regnum (gdbarch
)->lo
;
313 trad_frame_set_reg_addr (cache
,
314 regnum
+ gdbarch_num_regs (gdbarch
),
315 ucontext_addr
+ O32_UCONTEXT_LO
);
316 regnum
= mips_regnum (gdbarch
)->hi
;
317 trad_frame_set_reg_addr (cache
,
318 regnum
+ gdbarch_num_regs (gdbarch
),
319 ucontext_addr
+ O32_UCONTEXT_HI
);
321 if (target_read_memory (ucontext_addr
+ O32_UCONTEXT_FPUSED
, buf
, 4) == 0
322 && extract_unsigned_integer (buf
, 4, byte_order
) != 0)
324 for (regnum
= 0, addr
= ucontext_addr
+ O32_UCONTEXT_FPREGS
;
325 regnum
< 32; regnum
++, addr
+= O32_UCONTEXT_REG_SIZE
)
326 trad_frame_set_reg_addr (cache
,
327 regnum
+ gdbarch_fp0_regnum (gdbarch
),
329 trad_frame_set_reg_addr (cache
, mips_regnum (gdbarch
)->fp_control_status
,
333 trad_frame_set_id (cache
, frame_id_build (sp
, func
));
336 #define MIPS_INST_ADDIU_A0_SP_O32 (0x27a40000 \
337 + O32_SIGFRAME_UCONTEXT_OFFSET)
339 static const struct tramp_frame mips_fbsd_sigframe
=
344 { MIPS_INST_ADDIU_A0_SP_O32
, ULONGEST_MAX
}, /* addiu a0, sp, SIGF_UC */
345 { MIPS_INST_LI_V0_SIGRETURN
, ULONGEST_MAX
}, /* li v0, SYS_sigreturn */
346 { MIPS_INST_SYSCALL
, ULONGEST_MAX
}, /* syscall */
347 { MIPS_INST_BREAK
, ULONGEST_MAX
}, /* break */
348 { TRAMP_SENTINEL_INSN
, ULONGEST_MAX
}
350 mips_fbsd_sigframe_init
353 #define N64_SIGFRAME_UCONTEXT_OFFSET (32)
354 #define N64_SIGSET_T_SIZE (16)
356 #define N64_UCONTEXT_ONSTACK (N64_SIGSET_T_SIZE)
357 #define N64_UCONTEXT_PC (N64_UCONTEXT_ONSTACK + 8)
358 #define N64_UCONTEXT_REGS (N64_UCONTEXT_PC + 8)
359 #define N64_UCONTEXT_SR (N64_UCONTEXT_REGS + 8 * 32)
360 #define N64_UCONTEXT_LO (N64_UCONTEXT_SR + 8)
361 #define N64_UCONTEXT_HI (N64_UCONTEXT_LO + 8)
362 #define N64_UCONTEXT_FPUSED (N64_UCONTEXT_HI + 8)
363 #define N64_UCONTEXT_FPREGS (N64_UCONTEXT_FPUSED + 8)
365 #define N64_UCONTEXT_REG_SIZE 8
368 mips64_fbsd_sigframe_init (const struct tramp_frame
*self
,
369 const frame_info_ptr
&this_frame
,
370 struct trad_frame_cache
*cache
,
373 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
374 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
375 CORE_ADDR sp
, ucontext_addr
, addr
;
379 /* We find the appropriate instance of `ucontext_t' at a
380 fixed offset in the signal frame. */
381 sp
= get_frame_register_signed (this_frame
,
382 MIPS_SP_REGNUM
+ gdbarch_num_regs (gdbarch
));
383 ucontext_addr
= sp
+ N64_SIGFRAME_UCONTEXT_OFFSET
;
386 regnum
= mips_regnum (gdbarch
)->pc
;
387 trad_frame_set_reg_addr (cache
,
388 regnum
+ gdbarch_num_regs (gdbarch
),
389 ucontext_addr
+ N64_UCONTEXT_PC
);
392 for (regnum
= MIPS_ZERO_REGNUM
, addr
= ucontext_addr
+ N64_UCONTEXT_REGS
;
393 regnum
<= MIPS_RA_REGNUM
; regnum
++, addr
+= N64_UCONTEXT_REG_SIZE
)
394 trad_frame_set_reg_addr (cache
,
395 regnum
+ gdbarch_num_regs (gdbarch
),
398 regnum
= MIPS_PS_REGNUM
;
399 trad_frame_set_reg_addr (cache
,
400 regnum
+ gdbarch_num_regs (gdbarch
),
401 ucontext_addr
+ N64_UCONTEXT_SR
);
404 regnum
= mips_regnum (gdbarch
)->lo
;
405 trad_frame_set_reg_addr (cache
,
406 regnum
+ gdbarch_num_regs (gdbarch
),
407 ucontext_addr
+ N64_UCONTEXT_LO
);
408 regnum
= mips_regnum (gdbarch
)->hi
;
409 trad_frame_set_reg_addr (cache
,
410 regnum
+ gdbarch_num_regs (gdbarch
),
411 ucontext_addr
+ N64_UCONTEXT_HI
);
413 if (target_read_memory (ucontext_addr
+ N64_UCONTEXT_FPUSED
, buf
, 4) == 0
414 && extract_unsigned_integer (buf
, 4, byte_order
) != 0)
416 for (regnum
= 0, addr
= ucontext_addr
+ N64_UCONTEXT_FPREGS
;
417 regnum
< 32; regnum
++, addr
+= N64_UCONTEXT_REG_SIZE
)
418 trad_frame_set_reg_addr (cache
,
419 regnum
+ gdbarch_fp0_regnum (gdbarch
),
421 trad_frame_set_reg_addr (cache
, mips_regnum (gdbarch
)->fp_control_status
,
425 trad_frame_set_id (cache
, frame_id_build (sp
, func
));
428 #define MIPS_INST_ADDIU_A0_SP_N32 (0x27a40000 \
429 + N64_SIGFRAME_UCONTEXT_OFFSET)
431 static const struct tramp_frame mipsn32_fbsd_sigframe
=
436 { MIPS_INST_ADDIU_A0_SP_N32
, ULONGEST_MAX
}, /* addiu a0, sp, SIGF_UC */
437 { MIPS_INST_LI_V0_SIGRETURN
, ULONGEST_MAX
}, /* li v0, SYS_sigreturn */
438 { MIPS_INST_SYSCALL
, ULONGEST_MAX
}, /* syscall */
439 { MIPS_INST_BREAK
, ULONGEST_MAX
}, /* break */
440 { TRAMP_SENTINEL_INSN
, ULONGEST_MAX
}
442 mips64_fbsd_sigframe_init
445 #define MIPS_INST_DADDIU_A0_SP_N64 (0x67a40000 \
446 + N64_SIGFRAME_UCONTEXT_OFFSET)
448 static const struct tramp_frame mips64_fbsd_sigframe
=
453 { MIPS_INST_DADDIU_A0_SP_N64
, ULONGEST_MAX
}, /* daddiu a0, sp, SIGF_UC */
454 { MIPS_INST_LI_V0_SIGRETURN
, ULONGEST_MAX
}, /* li v0, SYS_sigreturn */
455 { MIPS_INST_SYSCALL
, ULONGEST_MAX
}, /* syscall */
456 { MIPS_INST_BREAK
, ULONGEST_MAX
}, /* break */
457 { TRAMP_SENTINEL_INSN
, ULONGEST_MAX
}
459 mips64_fbsd_sigframe_init
462 /* Shared library support. */
464 /* FreeBSD/mips can use an alternate routine in the runtime linker to
465 resolve functions. */
468 mips_fbsd_skip_solib_resolver (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
470 struct bound_minimal_symbol msym
471 = lookup_bound_minimal_symbol ("_mips_rtld_bind");
472 if (msym
.minsym
!= nullptr && msym
.value_address () == pc
)
473 return frame_unwind_caller_pc (get_current_frame ());
475 return fbsd_skip_solib_resolver (gdbarch
, pc
);
478 /* FreeBSD/mips uses a slightly different `struct link_map' than the
479 other FreeBSD platforms as it includes an additional `l_off'
482 static struct link_map_offsets
*
483 mips_fbsd_ilp32_fetch_link_map_offsets (void)
485 static struct link_map_offsets lmo
;
486 static struct link_map_offsets
*lmp
= NULL
;
492 lmo
.r_version_offset
= 0;
493 lmo
.r_version_size
= 4;
494 lmo
.r_map_offset
= 4;
495 lmo
.r_brk_offset
= 8;
496 lmo
.r_ldsomap_offset
= -1;
497 lmo
.r_next_offset
= -1;
499 lmo
.link_map_size
= 24;
500 lmo
.l_addr_offset
= 0;
501 lmo
.l_name_offset
= 8;
502 lmo
.l_ld_offset
= 12;
503 lmo
.l_next_offset
= 16;
504 lmo
.l_prev_offset
= 20;
510 static struct link_map_offsets
*
511 mips_fbsd_lp64_fetch_link_map_offsets (void)
513 static struct link_map_offsets lmo
;
514 static struct link_map_offsets
*lmp
= NULL
;
520 lmo
.r_version_offset
= 0;
521 lmo
.r_version_size
= 4;
522 lmo
.r_map_offset
= 8;
523 lmo
.r_brk_offset
= 16;
524 lmo
.r_ldsomap_offset
= -1;
525 lmo
.r_next_offset
= -1;
527 lmo
.link_map_size
= 48;
528 lmo
.l_addr_offset
= 0;
529 lmo
.l_name_offset
= 16;
530 lmo
.l_ld_offset
= 24;
531 lmo
.l_next_offset
= 32;
532 lmo
.l_prev_offset
= 40;
539 mips_fbsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
541 enum mips_abi abi
= mips_abi (gdbarch
);
543 /* Generic FreeBSD support. */
544 fbsd_init_abi (info
, gdbarch
);
546 set_gdbarch_software_single_step (gdbarch
, mips_software_single_step
);
551 tramp_frame_prepend_unwinder (gdbarch
, &mips_fbsd_sigframe
);
554 tramp_frame_prepend_unwinder (gdbarch
, &mipsn32_fbsd_sigframe
);
557 tramp_frame_prepend_unwinder (gdbarch
, &mips64_fbsd_sigframe
);
561 set_gdbarch_iterate_over_regset_sections
562 (gdbarch
, mips_fbsd_iterate_over_regset_sections
);
564 set_gdbarch_skip_solib_resolver (gdbarch
, mips_fbsd_skip_solib_resolver
);
566 /* FreeBSD/mips has SVR4-style shared libraries. */
567 set_solib_svr4_fetch_link_map_offsets
568 (gdbarch
, (gdbarch_ptr_bit (gdbarch
) == 32 ?
569 mips_fbsd_ilp32_fetch_link_map_offsets
:
570 mips_fbsd_lp64_fetch_link_map_offsets
));
573 void _initialize_mips_fbsd_tdep ();
575 _initialize_mips_fbsd_tdep ()
577 gdbarch_register_osabi (bfd_arch_mips
, 0, GDB_OSABI_FREEBSD
,