Don't use free_contents in _bfd_elf_slurp_version_tables
[binutils-gdb.git] / gdbserver / linux-or1k-low.cc
blob498955a9629c60a44c2245ebe2ed02874521c9a2
1 /* GNU/Linux/OR1K specific low level interface for the GDB server.
2 Copyright (C) 2021-2023 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "server.h"
20 #include "linux-low.h"
21 #include "elf/common.h"
22 #include "nat/gdb_ptrace.h"
23 #include <endian.h>
24 #include "gdb_proc_service.h"
25 #include <asm/ptrace.h>
27 #ifndef PTRACE_GET_THREAD_AREA
28 #define PTRACE_GET_THREAD_AREA 25
29 #endif
31 /* Linux target op definitions for the OpenRISC architecture. */
33 class or1k_target : public linux_process_target
35 public:
37 const regs_info *get_regs_info () override;
39 const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;
41 protected:
43 void low_arch_setup () override;
45 bool low_cannot_fetch_register (int regno) override;
47 bool low_cannot_store_register (int regno) override;
49 bool low_supports_breakpoints () override;
51 CORE_ADDR low_get_pc (regcache *regcache) override;
53 void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
55 bool low_breakpoint_at (CORE_ADDR pc) override;
58 /* The singleton target ops object. */
60 static or1k_target the_or1k_target;
62 bool
63 or1k_target::low_supports_breakpoints ()
65 return true;
68 CORE_ADDR
69 or1k_target::low_get_pc (regcache *regcache)
71 return linux_get_pc_32bit (regcache);
74 void
75 or1k_target::low_set_pc (regcache *regcache, CORE_ADDR pc)
77 linux_set_pc_32bit (regcache, pc);
80 /* The following definition must agree with the number of registers
81 defined in "struct user_regs" in GLIBC
82 (sysdeps/unix/sysv/linux/or1k/sys/ucontext.h), and also with
83 OR1K_NUM_REGS in GDB proper. */
85 #define or1k_num_regs 35
87 /* Defined in auto-generated file or1k-linux.c. */
89 void init_registers_or1k_linux (void);
90 extern const struct target_desc *tdesc_or1k_linux;
92 /* This union is used to convert between int and byte buffer
93 representations of register contents. */
95 union or1k_register
97 unsigned char buf[4];
98 int reg32;
101 /* Return the ptrace ``address'' of register REGNO. */
103 static int or1k_regmap[] = {
104 -1, 1, 2, 3, 4, 5, 6, 7,
105 8, 9, 10, 11, 12, 13, 14, 15,
106 16, 17, 18, 19, 20, 21, 22, 23,
107 24, 25, 26, 27, 28, 29, 30, 31,
108 -1, /* PC */
109 -1, /* ORIGINAL R11 */
110 -1 /* SYSCALL NO */
113 /* Implement the low_arch_setup linux target ops method. */
115 void
116 or1k_target::low_arch_setup ()
118 current_process ()->tdesc = tdesc_or1k_linux;
121 /* Implement the low_cannot_fetch_register linux target ops method. */
123 bool
124 or1k_target::low_cannot_fetch_register (int regno)
126 return (or1k_regmap[regno] == -1);
129 /* Implement the low_cannot_store_register linux target ops method. */
131 bool
132 or1k_target::low_cannot_store_register (int regno)
134 return (or1k_regmap[regno] == -1);
137 /* Breakpoint support. */
139 static const unsigned int or1k_breakpoint = 0x21000001;
140 #define or1k_breakpoint_len 4
142 /* Implementation of target ops method "sw_breakpoint_from_kind". */
144 const gdb_byte *
145 or1k_target::sw_breakpoint_from_kind (int kind, int *size)
147 *size = or1k_breakpoint_len;
148 return (const gdb_byte *) &or1k_breakpoint;
151 /* Implement the low_breakpoint_at linux target ops method. */
153 bool
154 or1k_target::low_breakpoint_at (CORE_ADDR where)
156 unsigned int insn;
158 read_memory (where, (unsigned char *) &insn, or1k_breakpoint_len);
159 if (insn == or1k_breakpoint)
160 return true;
161 return false;
164 /* Fetch the thread-local storage pointer for libthread_db. */
166 ps_err_e
167 ps_get_thread_area (struct ps_prochandle *ph,
168 lwpid_t lwpid, int idx, void **base)
170 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
171 return PS_ERR;
173 /* IDX is the bias from the thread pointer to the beginning of the
174 thread descriptor. It has to be subtracted due to implementation
175 quirks in libthread_db. */
176 *base = (void *) ((char *) *base - idx);
178 return PS_OK;
181 /* Helper functions to collect/supply a single register REGNO. */
183 static void
184 or1k_collect_register (struct regcache *regcache, int regno,
185 union or1k_register *reg)
187 union or1k_register tmp_reg;
189 collect_register (regcache, regno, &tmp_reg.reg32);
190 reg->reg32 = tmp_reg.reg32;
193 static void
194 or1k_supply_register (struct regcache *regcache, int regno,
195 const union or1k_register *reg)
197 supply_register (regcache, regno, reg->buf);
200 /* We have only a single register set on OpenRISC. */
202 static void
203 or1k_fill_gregset (struct regcache *regcache, void *buf)
205 union or1k_register *regset = (union or1k_register *) buf;
206 int i;
208 for (i = 1; i < or1k_num_regs; i++)
209 or1k_collect_register (regcache, i, regset + i);
212 static void
213 or1k_store_gregset (struct regcache *regcache, const void *buf)
215 const union or1k_register *regset = (union or1k_register *) buf;
216 int i;
218 for (i = 0; i < or1k_num_regs; i++)
219 or1k_supply_register (regcache, i, regset + i);
222 static struct regset_info or1k_regsets[] =
224 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS,
225 or1k_num_regs * 4, GENERAL_REGS,
226 or1k_fill_gregset, or1k_store_gregset },
227 NULL_REGSET
230 static struct regsets_info or1k_regsets_info =
232 or1k_regsets, /* regsets */
233 0, /* num_regsets */
234 NULL, /* disabled_regsets */
237 static struct usrregs_info or1k_usrregs_info =
239 or1k_num_regs,
240 or1k_regmap,
243 static struct regs_info or1k_regs =
245 NULL, /* regset_bitmap */
246 &or1k_usrregs_info,
247 &or1k_regsets_info
250 const regs_info *
251 or1k_target::get_regs_info ()
253 return &or1k_regs;
256 /* The linux target ops object. */
258 linux_process_target *the_linux_target = &the_or1k_target;
260 void
261 initialize_low_arch (void)
263 init_registers_or1k_linux ();
265 initialize_regsets_info (&or1k_regsets_info);