Adapt .gdb_index writer to new DWARF scanner
[binutils-gdb.git] / gdb / loongarch-linux-tdep.c
1 /* Target-dependent code for GNU/Linux on LoongArch processors.
2
3 Copyright (C) 2022 Free Software Foundation, Inc.
4 Contributed by Loongson Ltd.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "glibc-tdep.h"
23 #include "inferior.h"
24 #include "linux-tdep.h"
25 #include "loongarch-tdep.h"
26 #include "solib-svr4.h"
27 #include "target-descriptions.h"
28 #include "trad-frame.h"
29 #include "tramp-frame.h"
30
31 /* Unpack an elf_gregset_t into GDB's register cache. */
32
33 static void
34 loongarch_supply_gregset (const struct regset *r,
35 struct regcache *regcache, int regno,
36 const void *gprs, size_t len)
37 {
38 loongarch_gdbarch_tdep *tdep
39 = (loongarch_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
40 auto regs = tdep->regs;
41
42 int regsize = register_size (regcache->arch (), regs.r);
43 const gdb_byte *buf = nullptr;
44
45 if (regno == -1)
46 {
47 /* Set $r0 = 0. */
48 regcache->raw_supply_zeroed (regs.r);
49
50 for (int i = 1; i < 32; i++)
51 {
52 buf = (const gdb_byte*) gprs + regsize * i;
53 regcache->raw_supply (regs.r + i, (const void *) buf);
54 }
55
56 /* Size base (pc) = regsize * regs.pc. */
57 buf = (const gdb_byte*) gprs + regsize * regs.pc;
58 regcache->raw_supply (regs.pc, (const void *) buf);
59
60 /* Size base (badv) = regsize * regs.badv. */
61 buf = (const gdb_byte*) gprs + regsize * regs.badv;
62 regcache->raw_supply (regs.badv, (const void *) buf);
63 }
64 else if (regs.r == regno)
65 regcache->raw_supply_zeroed (regno);
66 else if ((regs.r < regno && regno < regs.r + 32)
67 || regs.pc == regno || regs.badv == regno)
68 {
69 /* Offset offset (regno) = regsize * (regno - regs.r). */
70 buf = (const gdb_byte*) gprs + regsize * (regno - regs.r);
71 regcache->raw_supply (regno, (const void *) buf);
72 }
73 }
74
75 /* Pack the GDB's register cache value into an elf_gregset_t. */
76
77 static void
78 loongarch_fill_gregset (const struct regset *r,
79 const struct regcache *regcache, int regno,
80 void *gprs, size_t len)
81 {
82 loongarch_gdbarch_tdep *tdep
83 = (loongarch_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
84 auto regs = tdep->regs;
85 int regsize = register_size (regcache->arch (), regs.r);
86 gdb_byte *buf = nullptr;
87
88 if (regno == -1)
89 {
90 for (int i = 0; i < 32; i++)
91 {
92 buf = (gdb_byte *) gprs + regsize * i;
93 regcache->raw_collect (regs.r + i, (void *) buf);
94 }
95
96 /* Size base (pc) = regsize * regs.pc. */
97 buf = (gdb_byte *) gprs + regsize * regs.pc;
98 regcache->raw_collect (regs.pc, (void *) buf);
99
100 /* Size base (badv) = regsize * regs.badv. */
101 buf = (gdb_byte *) gprs + regsize * regs.badv;
102 regcache->raw_collect (regs.badv, (void *) buf);
103 }
104 else if ((regs.r <= regno && regno < regs.r + 32)
105 || regs.pc == regno || regs.badv == regno)
106 {
107 /* Offset offset (regno) = regsize * (regno - regs.r). */
108 buf = (gdb_byte *) gprs + regsize * (regno - regs.r);
109 regcache->raw_collect (regno, (void *) buf);
110 }
111 }
112
113 /* Register set definitions. */
114
115 const struct regset loongarch_gregset =
116 {
117 nullptr,
118 loongarch_supply_gregset,
119 loongarch_fill_gregset,
120 };
121
122 /* Implement the "init" method of struct tramp_frame. */
123
124 #define LOONGARCH_RT_SIGFRAME_UCONTEXT_OFFSET 128
125 #define LOONGARCH_UCONTEXT_SIGCONTEXT_OFFSET 176
126
127 static void
128 loongarch_linux_rt_sigframe_init (const struct tramp_frame *self,
129 struct frame_info *this_frame,
130 struct trad_frame_cache *this_cache,
131 CORE_ADDR func)
132 {
133 struct gdbarch *gdbarch = get_frame_arch (this_frame);
134 loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);
135 auto regs = tdep->regs;
136
137 CORE_ADDR frame_sp = get_frame_sp (this_frame);
138 CORE_ADDR sigcontext_base = (frame_sp + LOONGARCH_RT_SIGFRAME_UCONTEXT_OFFSET
139 + LOONGARCH_UCONTEXT_SIGCONTEXT_OFFSET);
140
141 trad_frame_set_reg_addr (this_cache, regs.pc, sigcontext_base);
142 for (int i = 0; i < 32; i++)
143 trad_frame_set_reg_addr (this_cache, regs.r + i, sigcontext_base + 8 + i * 8);
144
145 trad_frame_set_id (this_cache, frame_id_build (frame_sp, func));
146 }
147
148 /* li.w a7, __NR_rt_sigreturn */
149 #define LOONGARCH_INST_LIW_A7_RT_SIGRETURN 0x03822c0b
150 /* syscall 0 */
151 #define LOONGARCH_INST_SYSCALL 0x002b0000
152
153 static const struct tramp_frame loongarch_linux_rt_sigframe =
154 {
155 SIGTRAMP_FRAME,
156 4,
157 {
158 { LOONGARCH_INST_LIW_A7_RT_SIGRETURN, ULONGEST_MAX },
159 { LOONGARCH_INST_SYSCALL, ULONGEST_MAX },
160 { TRAMP_SENTINEL_INSN, ULONGEST_MAX }
161 },
162 loongarch_linux_rt_sigframe_init,
163 NULL
164 };
165
166 /* Initialize LoongArch Linux ABI info. */
167
168 static void
169 loongarch_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
170 {
171 linux_init_abi (info, gdbarch, 0);
172
173 set_solib_svr4_fetch_link_map_offsets (gdbarch,
174 info.bfd_arch_info->bits_per_address == 32
175 ? linux_ilp32_fetch_link_map_offsets
176 : linux_lp64_fetch_link_map_offsets);
177
178 /* GNU/Linux uses SVR4-style shared libraries. */
179 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
180
181 /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
182 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
183
184 /* Enable TLS support. */
185 set_gdbarch_fetch_tls_load_module_address (gdbarch, svr4_fetch_objfile_link_map);
186
187 /* Prepend tramp frame unwinder for signal. */
188 tramp_frame_prepend_unwinder (gdbarch, &loongarch_linux_rt_sigframe);
189 }
190
191 /* Initialize LoongArch Linux target support. */
192
193 void _initialize_loongarch_linux_tdep ();
194 void
195 _initialize_loongarch_linux_tdep ()
196 {
197 gdbarch_register_osabi (bfd_arch_loongarch, bfd_mach_loongarch32,
198 GDB_OSABI_LINUX, loongarch_linux_init_abi);
199 gdbarch_register_osabi (bfd_arch_loongarch, bfd_mach_loongarch64,
200 GDB_OSABI_LINUX, loongarch_linux_init_abi);
201 }