Fix test for sections with different VMA<->LMA relationships so that it only applies...
[binutils-gdb.git] / gdb / vax-tdep.c
blobdd00898c298cb9fbf0b0b1759a235eaa48d6ba92
1 /* Target-dependent code for the VAX.
3 Copyright (C) 1986-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 "arch-utils.h"
21 #include "dis-asm.h"
22 #include "frame.h"
23 #include "frame-base.h"
24 #include "frame-unwind.h"
25 #include "gdbcore.h"
26 #include "gdbtypes.h"
27 #include "osabi.h"
28 #include "regcache.h"
29 #include "regset.h"
30 #include "trad-frame.h"
31 #include "value.h"
33 #include "vax-tdep.h"
35 /* Return the name of register REGNUM. */
37 static const char *
38 vax_register_name (struct gdbarch *gdbarch, int regnum)
40 static const char *register_names[] =
42 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
43 "r8", "r9", "r10", "r11", "ap", "fp", "sp", "pc",
44 "ps",
47 static_assert (VAX_NUM_REGS == ARRAY_SIZE (register_names));
48 return register_names[regnum];
51 /* Return the GDB type object for the "standard" data type of data in
52 register REGNUM. */
54 static struct type *
55 vax_register_type (struct gdbarch *gdbarch, int regnum)
57 return builtin_type (gdbarch)->builtin_int;
60 /* Core file support. */
62 /* Supply register REGNUM from the buffer specified by GREGS and LEN
63 in the general-purpose register set REGSET to register cache
64 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
66 static void
67 vax_supply_gregset (const struct regset *regset, struct regcache *regcache,
68 int regnum, const void *gregs, size_t len)
70 const gdb_byte *regs = (const gdb_byte *) gregs;
71 int i;
73 for (i = 0; i < VAX_NUM_REGS; i++)
75 if (regnum == i || regnum == -1)
76 regcache->raw_supply (i, regs + i * 4);
80 /* VAX register set. */
82 static const struct regset vax_gregset =
84 NULL,
85 vax_supply_gregset
88 /* Iterate over core file register note sections. */
90 static void
91 vax_iterate_over_regset_sections (struct gdbarch *gdbarch,
92 iterate_over_regset_sections_cb *cb,
93 void *cb_data,
94 const struct regcache *regcache)
96 cb (".reg", VAX_NUM_REGS * 4, VAX_NUM_REGS * 4, &vax_gregset, NULL, cb_data);
99 /* The VAX UNIX calling convention uses R1 to pass a structure return
100 value address instead of passing it as a first (hidden) argument as
101 the VMS calling convention suggests. */
103 static CORE_ADDR
104 vax_store_arguments (struct regcache *regcache, int nargs,
105 struct value **args, CORE_ADDR sp)
107 struct gdbarch *gdbarch = regcache->arch ();
108 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
109 gdb_byte buf[4];
110 int count = 0;
111 int i;
113 /* We create an argument list on the stack, and make the argument
114 pointer to it. */
116 /* Push arguments in reverse order. */
117 for (i = nargs - 1; i >= 0; i--)
119 int len = args[i]->enclosing_type ()->length ();
121 sp -= (len + 3) & ~3;
122 count += (len + 3) / 4;
123 write_memory (sp, args[i]->contents_all ().data (), len);
126 /* Push argument count. */
127 sp -= 4;
128 store_unsigned_integer (buf, 4, byte_order, count);
129 write_memory (sp, buf, 4);
131 /* Update the argument pointer. */
132 store_unsigned_integer (buf, 4, byte_order, sp);
133 regcache->cooked_write (VAX_AP_REGNUM, buf);
135 return sp;
138 static CORE_ADDR
139 vax_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
140 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
141 struct value **args, CORE_ADDR sp,
142 function_call_return_method return_method,
143 CORE_ADDR struct_addr)
145 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
146 CORE_ADDR fp = sp;
147 gdb_byte buf[4];
149 /* Set up the function arguments. */
150 sp = vax_store_arguments (regcache, nargs, args, sp);
152 /* Store return value address. */
153 if (return_method == return_method_struct)
154 regcache_cooked_write_unsigned (regcache, VAX_R1_REGNUM, struct_addr);
156 /* Store return address in the PC slot. */
157 sp -= 4;
158 store_unsigned_integer (buf, 4, byte_order, bp_addr);
159 write_memory (sp, buf, 4);
161 /* Store the (fake) frame pointer in the FP slot. */
162 sp -= 4;
163 store_unsigned_integer (buf, 4, byte_order, fp);
164 write_memory (sp, buf, 4);
166 /* Skip the AP slot. */
167 sp -= 4;
169 /* Store register save mask and control bits. */
170 sp -= 4;
171 store_unsigned_integer (buf, 4, byte_order, 0);
172 write_memory (sp, buf, 4);
174 /* Store condition handler. */
175 sp -= 4;
176 store_unsigned_integer (buf, 4, byte_order, 0);
177 write_memory (sp, buf, 4);
179 /* Update the stack pointer and frame pointer. */
180 store_unsigned_integer (buf, 4, byte_order, sp);
181 regcache->cooked_write (VAX_SP_REGNUM, buf);
182 regcache->cooked_write (VAX_FP_REGNUM, buf);
184 /* Return the saved (fake) frame pointer. */
185 return fp;
188 static struct frame_id
189 vax_dummy_id (struct gdbarch *gdbarch, const frame_info_ptr &this_frame)
191 CORE_ADDR fp;
193 fp = get_frame_register_unsigned (this_frame, VAX_FP_REGNUM);
194 return frame_id_build (fp, get_frame_pc (this_frame));
198 static enum return_value_convention
199 vax_return_value (struct gdbarch *gdbarch, struct value *function,
200 struct type *type, struct regcache *regcache,
201 gdb_byte *readbuf, const gdb_byte *writebuf)
203 int len = type->length ();
204 gdb_byte buf[8];
206 if (type->code () == TYPE_CODE_STRUCT
207 || type->code () == TYPE_CODE_UNION
208 || type->code () == TYPE_CODE_ARRAY)
210 /* The default on VAX is to return structures in static memory.
211 Consequently a function must return the address where we can
212 find the return value. */
214 if (readbuf)
216 ULONGEST addr;
218 regcache_raw_read_unsigned (regcache, VAX_R0_REGNUM, &addr);
219 read_memory (addr, readbuf, len);
222 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
225 if (readbuf)
227 /* Read the contents of R0 and (if necessary) R1. */
228 regcache->cooked_read (VAX_R0_REGNUM, buf);
229 if (len > 4)
230 regcache->cooked_read (VAX_R1_REGNUM, buf + 4);
231 memcpy (readbuf, buf, len);
233 if (writebuf)
235 /* Read the contents to R0 and (if necessary) R1. */
236 memcpy (buf, writebuf, len);
237 regcache->cooked_write (VAX_R0_REGNUM, buf);
238 if (len > 4)
239 regcache->cooked_write (VAX_R1_REGNUM, buf + 4);
242 return RETURN_VALUE_REGISTER_CONVENTION;
246 /* Use the program counter to determine the contents and size of a
247 breakpoint instruction. Return a pointer to a string of bytes that
248 encode a breakpoint instruction, store the length of the string in
249 *LEN and optionally adjust *PC to point to the correct memory
250 location for inserting the breakpoint. */
252 constexpr gdb_byte vax_break_insn[] = { 3 };
254 typedef BP_MANIPULATION (vax_break_insn) vax_breakpoint;
256 /* Advance PC across any function entry prologue instructions
257 to reach some "real" code. */
259 static CORE_ADDR
260 vax_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
262 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
263 gdb_byte op = read_memory_unsigned_integer (pc, 1, byte_order);
265 if (op == 0x11)
266 pc += 2; /* skip brb */
267 if (op == 0x31)
268 pc += 3; /* skip brw */
269 if (op == 0xC2
270 && read_memory_unsigned_integer (pc + 2, 1, byte_order) == 0x5E)
271 pc += 3; /* skip subl2 */
272 if (op == 0x9E
273 && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xAE
274 && read_memory_unsigned_integer (pc + 3, 1, byte_order) == 0x5E)
275 pc += 4; /* skip movab */
276 if (op == 0x9E
277 && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xCE
278 && read_memory_unsigned_integer (pc + 4, 1, byte_order) == 0x5E)
279 pc += 5; /* skip movab */
280 if (op == 0x9E
281 && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xEE
282 && read_memory_unsigned_integer (pc + 6, 1, byte_order) == 0x5E)
283 pc += 7; /* skip movab */
285 return pc;
289 /* Unwinding the stack is relatively easy since the VAX has a
290 dedicated frame pointer, and frames are set up automatically as the
291 result of a function call. Most of the relevant information can be
292 inferred from the documentation of the Procedure Call Instructions
293 in the VAX MACRO and Instruction Set Reference Manual. */
295 struct vax_frame_cache
297 /* Base address. */
298 CORE_ADDR base;
300 /* Table of saved registers. */
301 trad_frame_saved_reg *saved_regs;
304 static struct vax_frame_cache *
305 vax_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
307 struct vax_frame_cache *cache;
308 CORE_ADDR addr;
309 ULONGEST mask;
310 int regnum;
312 if (*this_cache)
313 return (struct vax_frame_cache *) *this_cache;
315 /* Allocate a new cache. */
316 cache = FRAME_OBSTACK_ZALLOC (struct vax_frame_cache);
317 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
319 /* The frame pointer is used as the base for the frame. */
320 cache->base = get_frame_register_unsigned (this_frame, VAX_FP_REGNUM);
321 if (cache->base == 0)
322 return cache;
324 /* The register save mask and control bits determine the layout of
325 the stack frame. */
326 mask = get_frame_memory_unsigned (this_frame, cache->base + 4, 4) >> 16;
328 /* These are always saved. */
329 cache->saved_regs[VAX_PC_REGNUM].set_addr (cache->base + 16);
330 cache->saved_regs[VAX_FP_REGNUM].set_addr (cache->base + 12);
331 cache->saved_regs[VAX_AP_REGNUM].set_addr (cache->base + 8);
332 cache->saved_regs[VAX_PS_REGNUM].set_addr (cache->base + 4);
334 /* Scan the register save mask and record the location of the saved
335 registers. */
336 addr = cache->base + 20;
337 for (regnum = 0; regnum < VAX_AP_REGNUM; regnum++)
339 if (mask & (1 << regnum))
341 cache->saved_regs[regnum].set_addr (addr);
342 addr += 4;
346 /* The CALLS/CALLG flag determines whether this frame has a General
347 Argument List or a Stack Argument List. */
348 if (mask & (1 << 13))
350 ULONGEST numarg;
352 /* This is a procedure with Stack Argument List. Adjust the
353 stack address for the arguments that were pushed onto the
354 stack. The return instruction will automatically pop the
355 arguments from the stack. */
356 numarg = get_frame_memory_unsigned (this_frame, addr, 1);
357 addr += 4 + numarg * 4;
360 /* Bits 1:0 of the stack pointer were saved in the control bits. */
361 cache->saved_regs[VAX_SP_REGNUM].set_value (addr + (mask >> 14));
363 return cache;
366 static void
367 vax_frame_this_id (const frame_info_ptr &this_frame, void **this_cache,
368 struct frame_id *this_id)
370 struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);
372 /* This marks the outermost frame. */
373 if (cache->base == 0)
374 return;
376 (*this_id) = frame_id_build (cache->base, get_frame_func (this_frame));
379 static struct value *
380 vax_frame_prev_register (const frame_info_ptr &this_frame,
381 void **this_cache, int regnum)
383 struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);
385 return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
388 static const struct frame_unwind vax_frame_unwind =
390 "vax prologue",
391 NORMAL_FRAME,
392 default_frame_unwind_stop_reason,
393 vax_frame_this_id,
394 vax_frame_prev_register,
395 NULL,
396 default_frame_sniffer
400 static CORE_ADDR
401 vax_frame_base_address (const frame_info_ptr &this_frame, void **this_cache)
403 struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);
405 return cache->base;
408 static CORE_ADDR
409 vax_frame_args_address (const frame_info_ptr &this_frame, void **this_cache)
411 return get_frame_register_unsigned (this_frame, VAX_AP_REGNUM);
414 static const struct frame_base vax_frame_base =
416 &vax_frame_unwind,
417 vax_frame_base_address,
418 vax_frame_base_address,
419 vax_frame_args_address
422 /* Return number of arguments for FRAME. */
424 static int
425 vax_frame_num_args (const frame_info_ptr &frame)
427 CORE_ADDR args;
429 /* Assume that the argument pointer for the outermost frame is
430 hosed, as is the case on NetBSD/vax ELF. */
431 if (get_frame_base_address (frame) == 0)
432 return 0;
434 args = get_frame_register_unsigned (frame, VAX_AP_REGNUM);
435 return get_frame_memory_unsigned (frame, args, 1);
440 /* Initialize the current architecture based on INFO. If possible, re-use an
441 architecture from ARCHES, which is a list of architectures already created
442 during this debugging session.
444 Called e.g. at program startup, when reading a core file, and when reading
445 a binary file. */
447 static struct gdbarch *
448 vax_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
450 struct gdbarch *gdbarch;
452 /* If there is already a candidate, use it. */
453 arches = gdbarch_list_lookup_by_info (arches, &info);
454 if (arches != NULL)
455 return arches->gdbarch;
457 gdbarch = gdbarch_alloc (&info, NULL);
459 set_gdbarch_float_format (gdbarch, floatformats_vax_f);
460 set_gdbarch_double_format (gdbarch, floatformats_vax_d);
461 set_gdbarch_long_double_format (gdbarch, floatformats_vax_d);
462 set_gdbarch_long_double_bit (gdbarch, 64);
464 /* Register info */
465 set_gdbarch_num_regs (gdbarch, VAX_NUM_REGS);
466 set_gdbarch_register_name (gdbarch, vax_register_name);
467 set_gdbarch_register_type (gdbarch, vax_register_type);
468 set_gdbarch_sp_regnum (gdbarch, VAX_SP_REGNUM);
469 set_gdbarch_pc_regnum (gdbarch, VAX_PC_REGNUM);
470 set_gdbarch_ps_regnum (gdbarch, VAX_PS_REGNUM);
472 set_gdbarch_iterate_over_regset_sections
473 (gdbarch, vax_iterate_over_regset_sections);
475 /* Frame and stack info */
476 set_gdbarch_skip_prologue (gdbarch, vax_skip_prologue);
477 set_gdbarch_frame_num_args (gdbarch, vax_frame_num_args);
478 set_gdbarch_frame_args_skip (gdbarch, 4);
480 /* Stack grows downward. */
481 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
483 /* Return value info */
484 set_gdbarch_return_value (gdbarch, vax_return_value);
486 /* Call dummy code. */
487 set_gdbarch_push_dummy_call (gdbarch, vax_push_dummy_call);
488 set_gdbarch_dummy_id (gdbarch, vax_dummy_id);
490 /* Breakpoint info */
491 set_gdbarch_breakpoint_kind_from_pc (gdbarch, vax_breakpoint::kind_from_pc);
492 set_gdbarch_sw_breakpoint_from_kind (gdbarch, vax_breakpoint::bp_from_kind);
494 /* Misc info */
495 set_gdbarch_deprecated_function_start_offset (gdbarch, 2);
496 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
498 frame_base_set_default (gdbarch, &vax_frame_base);
500 /* Hook in ABI-specific overrides, if they have been registered. */
501 gdbarch_init_osabi (info, gdbarch);
503 frame_unwind_append_unwinder (gdbarch, &vax_frame_unwind);
505 return (gdbarch);
508 void _initialize_vax_tdep ();
509 void
510 _initialize_vax_tdep ()
512 gdbarch_register (bfd_arch_vax, vax_gdbarch_init, NULL);