Fix RELOC_FOR_GLOBAL_SYMBOLS macro so that it can cope with user defined symbols...
[binutils-gdb.git] / gdbserver / linux-loongarch-low.cc
blob584ea64a7d95a4391758e850140274b05b0ef39c
1 /* GNU/Linux/LoongArch specific low level interface, for the remote server
2 for GDB.
3 Copyright (C) 2022-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 "linux-low.h"
21 #include "tdesc.h"
22 #include "elf/common.h"
23 #include "arch/loongarch.h"
25 /* Linux target ops definitions for the LoongArch architecture. */
27 class loongarch_target : public linux_process_target
29 public:
31 const regs_info *get_regs_info () override;
33 int breakpoint_kind_from_pc (CORE_ADDR *pcptr) override;
35 const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;
37 protected:
39 void low_arch_setup () override;
41 bool low_cannot_fetch_register (int regno) override;
43 bool low_cannot_store_register (int regno) override;
45 bool low_fetch_register (regcache *regcache, int regno) override;
47 bool low_supports_breakpoints () override;
49 CORE_ADDR low_get_pc (regcache *regcache) override;
51 void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
53 bool low_breakpoint_at (CORE_ADDR pc) override;
56 /* The singleton target ops object. */
58 static loongarch_target the_loongarch_target;
60 bool
61 loongarch_target::low_cannot_fetch_register (int regno)
63 gdb_assert_not_reached ("linux target op low_cannot_fetch_register "
64 "is not implemented by the target");
67 bool
68 loongarch_target::low_cannot_store_register (int regno)
70 gdb_assert_not_reached ("linux target op low_cannot_store_register "
71 "is not implemented by the target");
74 /* Implementation of linux target ops method "low_arch_setup". */
76 void
77 loongarch_target::low_arch_setup ()
79 static const char *expedite_regs[] = { "r3", "pc", NULL };
80 loongarch_gdbarch_features features;
81 target_desc_up tdesc;
83 features.xlen = sizeof (elf_greg_t);
84 tdesc = loongarch_create_target_description (features);
86 if (tdesc->expedite_regs.empty ())
88 init_target_desc (tdesc.get (), expedite_regs);
89 gdb_assert (!tdesc->expedite_regs.empty ());
91 current_process ()->tdesc = tdesc.release ();
94 /* Collect GPRs from REGCACHE into BUF. */
96 static void
97 loongarch_fill_gregset (struct regcache *regcache, void *buf)
99 elf_gregset_t *regset = (elf_gregset_t *) buf;
100 int i;
102 for (i = 1; i < 32; i++)
103 collect_register (regcache, i, *regset + i);
104 collect_register (regcache, LOONGARCH_ORIG_A0_REGNUM, *regset + LOONGARCH_ORIG_A0_REGNUM);
105 collect_register (regcache, LOONGARCH_PC_REGNUM, *regset + LOONGARCH_PC_REGNUM);
106 collect_register (regcache, LOONGARCH_BADV_REGNUM, *regset + LOONGARCH_BADV_REGNUM);
109 /* Supply GPRs from BUF into REGCACHE. */
111 static void
112 loongarch_store_gregset (struct regcache *regcache, const void *buf)
114 const elf_gregset_t *regset = (const elf_gregset_t *) buf;
115 int i;
117 supply_register_zeroed (regcache, 0);
118 for (i = 1; i < 32; i++)
119 supply_register (regcache, i, *regset + i);
120 supply_register (regcache, LOONGARCH_ORIG_A0_REGNUM, *regset + LOONGARCH_ORIG_A0_REGNUM);
121 supply_register (regcache, LOONGARCH_PC_REGNUM, *regset + LOONGARCH_PC_REGNUM);
122 supply_register (regcache, LOONGARCH_BADV_REGNUM, *regset + LOONGARCH_BADV_REGNUM);
125 /* Collect FPRs from REGCACHE into BUF. */
127 static void
128 loongarch_fill_fpregset (struct regcache *regcache, void *buf)
130 gdb_byte *regbuf = nullptr;
131 int fprsize = register_size (regcache->tdesc, LOONGARCH_FIRST_FP_REGNUM);
132 int fccsize = register_size (regcache->tdesc, LOONGARCH_FIRST_FCC_REGNUM);
134 for (int i = 0; i < LOONGARCH_LINUX_NUM_FPREGSET; i++)
136 regbuf = (gdb_byte *)buf + fprsize * i;
137 collect_register (regcache, LOONGARCH_FIRST_FP_REGNUM + i, regbuf);
140 for (int i = 0; i < LOONGARCH_LINUX_NUM_FCC; i++)
142 regbuf = (gdb_byte *)buf + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
143 fccsize * i;
144 collect_register (regcache, LOONGARCH_FIRST_FCC_REGNUM + i, regbuf);
147 regbuf = (gdb_byte *)buf + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
148 fccsize * LOONGARCH_LINUX_NUM_FCC;
149 collect_register (regcache, LOONGARCH_FCSR_REGNUM, regbuf);
152 /* Supply FPRs from BUF into REGCACHE. */
154 static void
155 loongarch_store_fpregset (struct regcache *regcache, const void *buf)
157 const gdb_byte *regbuf = nullptr;
158 int fprsize = register_size (regcache->tdesc, LOONGARCH_FIRST_FP_REGNUM);
159 int fccsize = register_size (regcache->tdesc, LOONGARCH_FIRST_FCC_REGNUM);
161 for (int i = 0; i < LOONGARCH_LINUX_NUM_FPREGSET; i++)
163 regbuf = (const gdb_byte *)buf + fprsize * i;
164 supply_register (regcache, LOONGARCH_FIRST_FP_REGNUM + i, regbuf);
167 for (int i = 0; i < LOONGARCH_LINUX_NUM_FCC; i++)
169 regbuf = (const gdb_byte *)buf + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
170 fccsize * i;
171 supply_register (regcache, LOONGARCH_FIRST_FCC_REGNUM + i, regbuf);
174 regbuf = (const gdb_byte *)buf + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
175 fccsize * LOONGARCH_LINUX_NUM_FCC;
176 supply_register (regcache, LOONGARCH_FCSR_REGNUM, regbuf);
179 /* Collect lsx regs from REGCACHE into BUF. */
181 static void
182 loongarch_fill_lsxregset (struct regcache *regcache, void *buf)
184 elf_lsxregset_t *regset = (elf_lsxregset_t *) buf;
185 int i;
187 for (i = 0; i < LOONGARCH_LINUX_NUM_LSXREGSET; i++)
188 collect_register (regcache, LOONGARCH_FIRST_LSX_REGNUM + i, *regset + i);
191 /* Supply lsx regs from BUF into REGCACHE. */
193 static void
194 loongarch_store_lsxregset (struct regcache *regcache, const void *buf)
196 const elf_lsxregset_t *regset = (const elf_lsxregset_t *) buf;
197 int i;
199 for (i = 0; i < LOONGARCH_LINUX_NUM_LSXREGSET; i++)
200 supply_register (regcache, LOONGARCH_FIRST_LSX_REGNUM + i, *regset + i);
203 /* Collect lasx regs from REGCACHE into BUF. */
205 static void
206 loongarch_fill_lasxregset (struct regcache *regcache, void *buf)
208 elf_lasxregset_t *regset = (elf_lasxregset_t *) buf;
209 int i;
211 for (i = 0; i < LOONGARCH_LINUX_NUM_LASXREGSET; i++)
212 collect_register (regcache, LOONGARCH_FIRST_LASX_REGNUM + i, *regset + i);
215 /* Supply lasx regs from BUF into REGCACHE. */
217 static void
218 loongarch_store_lasxregset (struct regcache *regcache, const void *buf)
220 const elf_lasxregset_t *regset = (const elf_lasxregset_t *) buf;
221 int i;
223 for (i = 0; i < LOONGARCH_LINUX_NUM_LASXREGSET; i++)
224 supply_register (regcache, LOONGARCH_FIRST_LASX_REGNUM + i, *regset + i);
227 /* Collect lbt regs from REGCACHE into BUF. */
229 static void
230 loongarch_fill_lbtregset (struct regcache *regcache, void *buf)
232 gdb_byte *regbuf = (gdb_byte*)buf;
233 int scrsize = register_size (regcache->tdesc, LOONGARCH_FIRST_SCR_REGNUM);
234 int eflagssize = register_size (regcache->tdesc, LOONGARCH_EFLAGS_REGNUM);
235 int i;
237 for (i = 0; i < LOONGARCH_LINUX_NUM_SCR; i++)
238 collect_register (regcache, LOONGARCH_FIRST_SCR_REGNUM + i, regbuf + scrsize * i);
240 collect_register (regcache, LOONGARCH_EFLAGS_REGNUM,
241 regbuf + LOONGARCH_LINUX_NUM_SCR * scrsize);
242 collect_register (regcache, LOONGARCH_FTOP_REGNUM,
243 regbuf + LOONGARCH_LINUX_NUM_SCR * scrsize + eflagssize);
247 /* Supply lbt regs from BUF into REGCACHE. */
249 static void
250 loongarch_store_lbtregset (struct regcache *regcache, const void *buf)
253 gdb_byte *regbuf = (gdb_byte*)buf;
254 int scrsize = register_size (regcache->tdesc, LOONGARCH_FIRST_SCR_REGNUM);
255 int eflagssize = register_size (regcache->tdesc, LOONGARCH_EFLAGS_REGNUM);
256 int i;
258 for (i = 0; i < LOONGARCH_LINUX_NUM_SCR; i++)
259 supply_register (regcache, LOONGARCH_FIRST_SCR_REGNUM + i, regbuf + scrsize * i);
261 supply_register (regcache, LOONGARCH_EFLAGS_REGNUM,
262 regbuf + LOONGARCH_LINUX_NUM_SCR * scrsize);
263 supply_register (regcache, LOONGARCH_FTOP_REGNUM,
264 regbuf + LOONGARCH_LINUX_NUM_SCR * scrsize + eflagssize);
268 /* LoongArch/Linux regsets. */
269 static struct regset_info loongarch_regsets[] = {
270 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS, sizeof (elf_gregset_t),
271 GENERAL_REGS, loongarch_fill_gregset, loongarch_store_gregset },
272 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_FPREGSET, sizeof (elf_fpregset_t),
273 FP_REGS, loongarch_fill_fpregset, loongarch_store_fpregset },
274 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_LARCH_LSX, sizeof (elf_lsxregset_t),
275 OPTIONAL_REGS, loongarch_fill_lsxregset, loongarch_store_lsxregset },
276 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_LARCH_LASX, sizeof (elf_lasxregset_t),
277 OPTIONAL_REGS, loongarch_fill_lasxregset, loongarch_store_lasxregset },
278 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_LARCH_LBT, LOONGARCH_LBT_REGS_SIZE,
279 OPTIONAL_REGS, loongarch_fill_lbtregset, loongarch_store_lbtregset },
280 NULL_REGSET
283 /* LoongArch/Linux regset information. */
284 static struct regsets_info loongarch_regsets_info =
286 loongarch_regsets, /* regsets */
287 0, /* num_regsets */
288 NULL, /* disabled_regsets */
291 /* Definition of linux_target_ops data member "regs_info". */
292 static struct regs_info loongarch_regs =
294 NULL, /* regset_bitmap */
295 NULL, /* usrregs */
296 &loongarch_regsets_info,
299 /* Implementation of linux target ops method "get_regs_info". */
301 const regs_info *
302 loongarch_target::get_regs_info ()
304 return &loongarch_regs;
307 /* Implementation of linux target ops method "low_fetch_register". */
309 bool
310 loongarch_target::low_fetch_register (regcache *regcache, int regno)
312 if (regno != 0)
313 return false;
314 supply_register_zeroed (regcache, 0);
315 return true;
318 bool
319 loongarch_target::low_supports_breakpoints ()
321 return true;
324 /* Implementation of linux target ops method "low_get_pc". */
326 CORE_ADDR
327 loongarch_target::low_get_pc (regcache *regcache)
329 if (register_size (regcache->tdesc, 0) == 8)
330 return linux_get_pc_64bit (regcache);
331 else
332 return linux_get_pc_32bit (regcache);
335 /* Implementation of linux target ops method "low_set_pc". */
337 void
338 loongarch_target::low_set_pc (regcache *regcache, CORE_ADDR newpc)
340 if (register_size (regcache->tdesc, 0) == 8)
341 linux_set_pc_64bit (regcache, newpc);
342 else
343 linux_set_pc_32bit (regcache, newpc);
346 #define loongarch_breakpoint_len 4
348 /* LoongArch BRK software debug mode instruction.
349 This instruction needs to match gdb/loongarch-tdep.c
350 (loongarch_default_breakpoint). */
351 static const gdb_byte loongarch_breakpoint[] = {0x05, 0x00, 0x2a, 0x00};
353 /* Implementation of target ops method "breakpoint_kind_from_pc". */
356 loongarch_target::breakpoint_kind_from_pc (CORE_ADDR *pcptr)
358 return loongarch_breakpoint_len;
361 /* Implementation of target ops method "sw_breakpoint_from_kind". */
363 const gdb_byte *
364 loongarch_target::sw_breakpoint_from_kind (int kind, int *size)
366 *size = loongarch_breakpoint_len;
367 return (const gdb_byte *) &loongarch_breakpoint;
370 /* Implementation of linux target ops method "low_breakpoint_at". */
372 bool
373 loongarch_target::low_breakpoint_at (CORE_ADDR pc)
375 gdb_byte insn[loongarch_breakpoint_len];
377 read_memory (pc, (unsigned char *) &insn, loongarch_breakpoint_len);
378 if (memcmp (insn, loongarch_breakpoint, loongarch_breakpoint_len) == 0)
379 return true;
381 return false;
384 /* The linux target ops object. */
386 linux_process_target *the_linux_target = &the_loongarch_target;
388 /* Initialize the LoongArch/Linux target. */
390 void
391 initialize_low_arch ()
393 initialize_regsets_info (&loongarch_regsets_info);