Automatic date update in version.in
[binutils-gdb.git] / gdb / m68k-linux-tdep.c
blob4874db720db9e110a141fd5bf8d8264affafe06e
1 /* Motorola m68k target-dependent support for GNU/Linux.
3 Copyright (C) 1996-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 "gdbcore.h"
21 #include "frame.h"
22 #include "target.h"
23 #include "gdbtypes.h"
24 #include "osabi.h"
25 #include "regcache.h"
26 #include "objfiles.h"
27 #include "symtab.h"
28 #include "m68k-tdep.h"
29 #include "trad-frame.h"
30 #include "frame-unwind.h"
31 #include "glibc-tdep.h"
32 #include "solib-svr4.h"
33 #include "auxv.h"
34 #include "observable.h"
35 #include "elf/common.h"
36 #include "linux-tdep.h"
37 #include "regset.h"
39 /* Offsets (in target ints) into jmp_buf. */
41 #define M68K_LINUX_JB_ELEMENT_SIZE 4
42 #define M68K_LINUX_JB_PC 7
44 /* Check whether insn1 and insn2 are parts of a signal trampoline. */
46 #define IS_SIGTRAMP(insn1, insn2) \
47 (/* addaw #20,sp; moveq #119,d0; trap #0 */ \
48 (insn1 == 0xdefc0014 && insn2 == 0x70774e40) \
49 /* moveq #119,d0; trap #0 */ \
50 || insn1 == 0x70774e40)
52 #define IS_RT_SIGTRAMP(insn1, insn2) \
53 (/* movel #173,d0; trap #0 */ \
54 (insn1 == 0x203c0000 && insn2 == 0x00ad4e40) \
55 /* moveq #82,d0; notb d0; trap #0 */ \
56 || (insn1 == 0x70524600 && (insn2 >> 16) == 0x4e40))
58 /* Return non-zero if THIS_FRAME corresponds to a signal trampoline. For
59 the sake of m68k_linux_get_sigtramp_info we also distinguish between
60 non-RT and RT signal trampolines. */
62 static int
63 m68k_linux_pc_in_sigtramp (const frame_info_ptr &this_frame)
65 struct gdbarch *gdbarch = get_frame_arch (this_frame);
66 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
67 gdb_byte buf[12];
68 unsigned long insn0, insn1, insn2;
69 CORE_ADDR pc = get_frame_pc (this_frame);
71 if (!safe_frame_unwind_memory (this_frame, pc - 4, {buf, sizeof (buf)}))
72 return 0;
73 insn1 = extract_unsigned_integer (buf + 4, 4, byte_order);
74 insn2 = extract_unsigned_integer (buf + 8, 4, byte_order);
75 if (IS_SIGTRAMP (insn1, insn2))
76 return 1;
77 if (IS_RT_SIGTRAMP (insn1, insn2))
78 return 2;
80 insn0 = extract_unsigned_integer (buf, 4, byte_order);
81 if (IS_SIGTRAMP (insn0, insn1))
82 return 1;
83 if (IS_RT_SIGTRAMP (insn0, insn1))
84 return 2;
86 insn0 = ((insn0 << 16) & 0xffffffff) | (insn1 >> 16);
87 insn1 = ((insn1 << 16) & 0xffffffff) | (insn2 >> 16);
88 if (IS_SIGTRAMP (insn0, insn1))
89 return 1;
90 if (IS_RT_SIGTRAMP (insn0, insn1))
91 return 2;
93 return 0;
96 /* From <asm/sigcontext.h>. */
97 static int m68k_linux_sigcontext_reg_offset[M68K_NUM_REGS] =
99 2 * 4, /* %d0 */
100 3 * 4, /* %d1 */
101 -1, /* %d2 */
102 -1, /* %d3 */
103 -1, /* %d4 */
104 -1, /* %d5 */
105 -1, /* %d6 */
106 -1, /* %d7 */
107 4 * 4, /* %a0 */
108 5 * 4, /* %a1 */
109 -1, /* %a2 */
110 -1, /* %a3 */
111 -1, /* %a4 */
112 -1, /* %a5 */
113 -1, /* %fp */
114 1 * 4, /* %sp */
115 6 * 4, /* %sr */
116 6 * 4 + 2, /* %pc */
117 8 * 4, /* %fp0 */
118 11 * 4, /* %fp1 */
119 -1, /* %fp2 */
120 -1, /* %fp3 */
121 -1, /* %fp4 */
122 -1, /* %fp5 */
123 -1, /* %fp6 */
124 -1, /* %fp7 */
125 14 * 4, /* %fpcr */
126 15 * 4, /* %fpsr */
127 16 * 4 /* %fpiaddr */
130 static int m68k_uclinux_sigcontext_reg_offset[M68K_NUM_REGS] =
132 2 * 4, /* %d0 */
133 3 * 4, /* %d1 */
134 -1, /* %d2 */
135 -1, /* %d3 */
136 -1, /* %d4 */
137 -1, /* %d5 */
138 -1, /* %d6 */
139 -1, /* %d7 */
140 4 * 4, /* %a0 */
141 5 * 4, /* %a1 */
142 -1, /* %a2 */
143 -1, /* %a3 */
144 -1, /* %a4 */
145 6 * 4, /* %a5 */
146 -1, /* %fp */
147 1 * 4, /* %sp */
148 7 * 4, /* %sr */
149 7 * 4 + 2, /* %pc */
150 -1, /* %fp0 */
151 -1, /* %fp1 */
152 -1, /* %fp2 */
153 -1, /* %fp3 */
154 -1, /* %fp4 */
155 -1, /* %fp5 */
156 -1, /* %fp6 */
157 -1, /* %fp7 */
158 -1, /* %fpcr */
159 -1, /* %fpsr */
160 -1 /* %fpiaddr */
163 /* From <asm/ucontext.h>. */
164 static int m68k_linux_ucontext_reg_offset[M68K_NUM_REGS] =
166 6 * 4, /* %d0 */
167 7 * 4, /* %d1 */
168 8 * 4, /* %d2 */
169 9 * 4, /* %d3 */
170 10 * 4, /* %d4 */
171 11 * 4, /* %d5 */
172 12 * 4, /* %d6 */
173 13 * 4, /* %d7 */
174 14 * 4, /* %a0 */
175 15 * 4, /* %a1 */
176 16 * 4, /* %a2 */
177 17 * 4, /* %a3 */
178 18 * 4, /* %a4 */
179 19 * 4, /* %a5 */
180 20 * 4, /* %fp */
181 21 * 4, /* %sp */
182 23 * 4, /* %sr */
183 22 * 4, /* %pc */
184 27 * 4, /* %fp0 */
185 30 * 4, /* %fp1 */
186 33 * 4, /* %fp2 */
187 36 * 4, /* %fp3 */
188 39 * 4, /* %fp4 */
189 42 * 4, /* %fp5 */
190 45 * 4, /* %fp6 */
191 48 * 4, /* %fp7 */
192 24 * 4, /* %fpcr */
193 25 * 4, /* %fpsr */
194 26 * 4 /* %fpiaddr */
198 /* Get info about saved registers in sigtramp. */
200 struct m68k_linux_sigtramp_info
202 /* Address of sigcontext. */
203 CORE_ADDR sigcontext_addr;
205 /* Offset of registers in `struct sigcontext'. */
206 int *sc_reg_offset;
209 /* Nonzero if running on uClinux. */
210 static int target_is_uclinux;
212 static void
213 m68k_linux_inferior_created (inferior *inf)
215 /* Record that we will need to re-evaluate whether we are running on a
216 uClinux or normal GNU/Linux target (see m68k_linux_get_sigtramp_info). */
217 target_is_uclinux = -1;
220 static struct m68k_linux_sigtramp_info
221 m68k_linux_get_sigtramp_info (const frame_info_ptr &this_frame)
223 struct gdbarch *gdbarch = get_frame_arch (this_frame);
224 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
225 CORE_ADDR sp;
226 struct m68k_linux_sigtramp_info info;
228 /* Determine whether we are running on a uClinux or normal GNU/Linux
229 target so we can use the correct sigcontext layouts. */
230 if (target_is_uclinux == -1)
231 target_is_uclinux = linux_is_uclinux ();
233 sp = get_frame_register_unsigned (this_frame, M68K_SP_REGNUM);
235 /* Get sigcontext address, it is the third parameter on the stack. */
236 info.sigcontext_addr = read_memory_unsigned_integer (sp + 8, 4, byte_order);
238 if (m68k_linux_pc_in_sigtramp (this_frame) == 2)
239 info.sc_reg_offset = m68k_linux_ucontext_reg_offset;
240 else
241 info.sc_reg_offset = (target_is_uclinux
242 ? m68k_uclinux_sigcontext_reg_offset
243 : m68k_linux_sigcontext_reg_offset);
244 return info;
247 /* Signal trampolines. */
249 static struct trad_frame_cache *
250 m68k_linux_sigtramp_frame_cache (const frame_info_ptr &this_frame,
251 void **this_cache)
253 struct frame_id this_id;
254 struct trad_frame_cache *cache;
255 struct gdbarch *gdbarch = get_frame_arch (this_frame);
256 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
257 struct m68k_linux_sigtramp_info info;
258 gdb_byte buf[4];
259 int i;
261 if (*this_cache)
262 return (struct trad_frame_cache *) *this_cache;
264 cache = trad_frame_cache_zalloc (this_frame);
266 /* FIXME: cagney/2004-05-01: This is is long standing broken code.
267 The frame ID's code address should be the start-address of the
268 signal trampoline and not the current PC within that
269 trampoline. */
270 get_frame_register (this_frame, M68K_SP_REGNUM, buf);
271 /* See the end of m68k_push_dummy_call. */
272 this_id = frame_id_build (extract_unsigned_integer (buf, 4, byte_order)
273 - 4 + 8, get_frame_pc (this_frame));
274 trad_frame_set_id (cache, this_id);
276 info = m68k_linux_get_sigtramp_info (this_frame);
278 for (i = 0; i < M68K_NUM_REGS; i++)
279 if (info.sc_reg_offset[i] != -1)
280 trad_frame_set_reg_addr (cache, i,
281 info.sigcontext_addr + info.sc_reg_offset[i]);
283 *this_cache = cache;
284 return cache;
287 static void
288 m68k_linux_sigtramp_frame_this_id (const frame_info_ptr &this_frame,
289 void **this_cache,
290 struct frame_id *this_id)
292 struct trad_frame_cache *cache =
293 m68k_linux_sigtramp_frame_cache (this_frame, this_cache);
294 trad_frame_get_id (cache, this_id);
297 static struct value *
298 m68k_linux_sigtramp_frame_prev_register (const frame_info_ptr &this_frame,
299 void **this_cache,
300 int regnum)
302 /* Make sure we've initialized the cache. */
303 struct trad_frame_cache *cache =
304 m68k_linux_sigtramp_frame_cache (this_frame, this_cache);
305 return trad_frame_get_register (cache, this_frame, regnum);
308 static int
309 m68k_linux_sigtramp_frame_sniffer (const struct frame_unwind *self,
310 const frame_info_ptr &this_frame,
311 void **this_prologue_cache)
313 return m68k_linux_pc_in_sigtramp (this_frame);
316 static const struct frame_unwind m68k_linux_sigtramp_frame_unwind =
318 "m68k linux sigtramp",
319 SIGTRAMP_FRAME,
320 default_frame_unwind_stop_reason,
321 m68k_linux_sigtramp_frame_this_id,
322 m68k_linux_sigtramp_frame_prev_register,
323 NULL,
324 m68k_linux_sigtramp_frame_sniffer
327 /* Register maps for supply/collect regset functions. */
329 static const struct regcache_map_entry m68k_linux_gregmap[] =
331 { 7, M68K_D1_REGNUM, 4 }, /* d1 ... d7 */
332 { 7, M68K_A0_REGNUM, 4 }, /* a0 ... a6 */
333 { 1, M68K_D0_REGNUM, 4 },
334 { 1, M68K_SP_REGNUM, 4 },
335 { 1, REGCACHE_MAP_SKIP, 4 }, /* orig_d0 (skip) */
336 { 1, M68K_PS_REGNUM, 4 },
337 { 1, M68K_PC_REGNUM, 4 },
338 /* Ignore 16-bit fields 'fmtvec' and '__fill'. */
339 { 0 }
342 #define M68K_LINUX_GREGS_SIZE (20 * 4)
344 static const struct regcache_map_entry m68k_linux_fpregmap[] =
346 { 8, M68K_FP0_REGNUM, 12 }, /* fp0 ... fp7 */
347 { 1, M68K_FPC_REGNUM, 4 },
348 { 1, M68K_FPS_REGNUM, 4 },
349 { 1, M68K_FPI_REGNUM, 4 },
350 { 0 }
353 #define M68K_LINUX_FPREGS_SIZE (27 * 4)
355 /* Register sets. */
357 static const struct regset m68k_linux_gregset =
359 m68k_linux_gregmap,
360 regcache_supply_regset, regcache_collect_regset
363 static const struct regset m68k_linux_fpregset =
365 m68k_linux_fpregmap,
366 regcache_supply_regset, regcache_collect_regset
369 /* Iterate over core file register note sections. */
371 static void
372 m68k_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
373 iterate_over_regset_sections_cb *cb,
374 void *cb_data,
375 const struct regcache *regcache)
377 cb (".reg", M68K_LINUX_GREGS_SIZE, M68K_LINUX_GREGS_SIZE, &m68k_linux_gregset,
378 NULL, cb_data);
379 cb (".reg2", M68K_LINUX_FPREGS_SIZE, M68K_LINUX_FPREGS_SIZE,
380 &m68k_linux_fpregset, NULL, cb_data);
383 static void
384 m68k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
386 m68k_gdbarch_tdep *tdep = gdbarch_tdep<m68k_gdbarch_tdep> (gdbarch);
388 linux_init_abi (info, gdbarch, 0);
390 tdep->jb_pc = M68K_LINUX_JB_PC;
391 tdep->jb_elt_size = M68K_LINUX_JB_ELEMENT_SIZE;
393 /* GNU/Linux uses a calling convention that's similar to SVR4. It
394 returns integer values in %d0/%d1, pointer values in %a0 and
395 floating values in %fp0, just like SVR4, but uses %a1 to pass the
396 address to store a structure value. It also returns small
397 structures in registers instead of memory. */
398 m68k_svr4_init_abi (info, gdbarch);
399 tdep->struct_value_regnum = M68K_A1_REGNUM;
400 tdep->struct_return = reg_struct_return;
402 set_gdbarch_decr_pc_after_break (gdbarch, 2);
404 frame_unwind_append_unwinder (gdbarch, &m68k_linux_sigtramp_frame_unwind);
406 /* Shared library handling. */
408 /* GNU/Linux uses SVR4-style shared libraries. */
409 set_solib_svr4_fetch_link_map_offsets (gdbarch,
410 linux_ilp32_fetch_link_map_offsets);
412 /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
413 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
415 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
417 /* Core file support. */
418 set_gdbarch_iterate_over_regset_sections
419 (gdbarch, m68k_linux_iterate_over_regset_sections);
421 /* Enable TLS support. */
422 set_gdbarch_fetch_tls_load_module_address (gdbarch,
423 svr4_fetch_objfile_link_map);
426 void _initialize_m68k_linux_tdep ();
427 void
428 _initialize_m68k_linux_tdep ()
430 gdbarch_register_osabi (bfd_arch_m68k, 0, GDB_OSABI_LINUX,
431 m68k_linux_init_abi);
432 gdb::observers::inferior_created.attach (m68k_linux_inferior_created,
433 "m68k-linux-tdep");