]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/vax-tdep.c
From Cary Coutant: Correct generation of RELATIVE relocs.
[thirdparty/binutils-gdb.git] / gdb / vax-tdep.c
CommitLineData
0543f387 1/* Target-dependent code for the VAX.
464e0365 2
6aba47ca
DJ
3 Copyright (C) 1986, 1989, 1991, 1992, 1995, 1996, 1998, 1999, 2000, 2002,
4 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b 11 (at your option) any later version.
c906108c 12
c5aa993b
JM
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.
c906108c 17
c5aa993b 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
20
21#include "defs.h"
0543f387
MK
22#include "arch-utils.h"
23#include "dis-asm.h"
0a3e99f6 24#include "floatformat.h"
c11c3a98 25#include "frame.h"
7def7fef
MK
26#include "frame-base.h"
27#include "frame-unwind.h"
0543f387
MK
28#include "gdbcore.h"
29#include "gdbtypes.h"
4be87837 30#include "osabi.h"
0543f387 31#include "regcache.h"
8b910bab 32#include "regset.h"
0543f387
MK
33#include "trad-frame.h"
34#include "value.h"
8b910bab
MK
35
36#include "gdb_string.h"
f267bd6a
JT
37
38#include "vax-tdep.h"
39
5e6b39ff
MK
40/* Return the name of register REGNUM. */
41
fa88f677 42static const char *
5e6b39ff 43vax_register_name (int regnum)
51eb8b08
JT
44{
45 static char *register_names[] =
46 {
5e6b39ff
MK
47 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
48 "r8", "r9", "r10", "r11", "ap", "fp", "sp", "pc",
51eb8b08
JT
49 "ps",
50 };
51
5e6b39ff
MK
52 if (regnum >= 0 && regnum < ARRAY_SIZE (register_names))
53 return register_names[regnum];
51eb8b08 54
5e6b39ff 55 return NULL;
51eb8b08
JT
56}
57
5e6b39ff
MK
58/* Return the GDB type object for the "standard" data type of data in
59 register REGNUM. */
51eb8b08 60
f267bd6a 61static struct type *
5e6b39ff 62vax_register_type (struct gdbarch *gdbarch, int regnum)
51eb8b08 63{
5e6b39ff 64 return builtin_type_int;
51eb8b08
JT
65}
66\f
8b910bab
MK
67/* Core file support. */
68
69/* Supply register REGNUM from the buffer specified by GREGS and LEN
70 in the general-purpose register set REGSET to register cache
71 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
72
73static void
74vax_supply_gregset (const struct regset *regset, struct regcache *regcache,
75 int regnum, const void *gregs, size_t len)
76{
8cc49c48 77 const gdb_byte *regs = gregs;
8b910bab
MK
78 int i;
79
80 for (i = 0; i < VAX_NUM_REGS; i++)
81 {
82 if (regnum == i || regnum == -1)
83 regcache_raw_supply (regcache, i, regs + i * 4);
84 }
85}
52efde73 86
8b910bab
MK
87/* VAX register set. */
88
89static struct regset vax_gregset =
90{
91 NULL,
92 vax_supply_gregset
93};
94
95/* Return the appropriate register set for the core section identified
96 by SECT_NAME and SECT_SIZE. */
97
98static const struct regset *
99vax_regset_from_core_section (struct gdbarch *gdbarch,
100 const char *sect_name, size_t sect_size)
101{
102 if (strcmp (sect_name, ".reg") == 0 && sect_size >= VAX_NUM_REGS * 4)
103 return &vax_gregset;
104
105 return NULL;
106}
107\f
4cd80476 108/* The VAX UNIX calling convention uses R1 to pass a structure return
67b441e1
MK
109 value address instead of passing it as a first (hidden) argument as
110 the VMS calling convention suggests. */
111
7346c184
MK
112static CORE_ADDR
113vax_store_arguments (struct regcache *regcache, int nargs,
67b441e1 114 struct value **args, CORE_ADDR sp)
52efde73 115{
8cc49c48 116 gdb_byte buf[4];
7346c184
MK
117 int count = 0;
118 int i;
52efde73 119
7346c184
MK
120 /* We create an argument list on the stack, and make the argument
121 pointer to it. */
52efde73 122
7346c184
MK
123 /* Push arguments in reverse order. */
124 for (i = nargs - 1; i >= 0; i--)
52efde73 125 {
4754a64e 126 int len = TYPE_LENGTH (value_enclosing_type (args[i]));
7346c184
MK
127
128 sp -= (len + 3) & ~3;
129 count += (len + 3) / 4;
46615f07 130 write_memory (sp, value_contents_all (args[i]), len);
52efde73 131 }
a33f7558 132
7346c184
MK
133 /* Push argument count. */
134 sp -= 4;
135 store_unsigned_integer (buf, 4, count);
136 write_memory (sp, buf, 4);
a33f7558 137
7346c184
MK
138 /* Update the argument pointer. */
139 store_unsigned_integer (buf, 4, sp);
140 regcache_cooked_write (regcache, VAX_AP_REGNUM, buf);
a33f7558 141
7346c184
MK
142 return sp;
143}
144
145static CORE_ADDR
7d9b040b 146vax_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
7346c184
MK
147 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
148 struct value **args, CORE_ADDR sp, int struct_return,
149 CORE_ADDR struct_addr)
150{
151 CORE_ADDR fp = sp;
8cc49c48 152 gdb_byte buf[4];
7346c184
MK
153
154 /* Set up the function arguments. */
67b441e1
MK
155 sp = vax_store_arguments (regcache, nargs, args, sp);
156
157 /* Store return value address. */
158 if (struct_return)
159 regcache_cooked_write_unsigned (regcache, VAX_R1_REGNUM, struct_addr);
7346c184
MK
160
161 /* Store return address in the PC slot. */
162 sp -= 4;
163 store_unsigned_integer (buf, 4, bp_addr);
164 write_memory (sp, buf, 4);
165
166 /* Store the (fake) frame pointer in the FP slot. */
167 sp -= 4;
168 store_unsigned_integer (buf, 4, fp);
169 write_memory (sp, buf, 4);
170
171 /* Skip the AP slot. */
172 sp -= 4;
173
174 /* Store register save mask and control bits. */
175 sp -= 4;
176 store_unsigned_integer (buf, 4, 0);
177 write_memory (sp, buf, 4);
178
179 /* Store condition handler. */
180 sp -= 4;
181 store_unsigned_integer (buf, 4, 0);
182 write_memory (sp, buf, 4);
183
184 /* Update the stack pointer and frame pointer. */
185 store_unsigned_integer (buf, 4, sp);
186 regcache_cooked_write (regcache, VAX_SP_REGNUM, buf);
187 regcache_cooked_write (regcache, VAX_FP_REGNUM, buf);
188
189 /* Return the saved (fake) frame pointer. */
190 return fp;
191}
192
193static struct frame_id
194vax_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
a33f7558 195{
7346c184
MK
196 CORE_ADDR fp;
197
198 fp = frame_unwind_register_unsigned (next_frame, VAX_FP_REGNUM);
199 return frame_id_build (fp, frame_pc_unwind (next_frame));
a33f7558 200}
ab62c900 201\f
7346c184 202
67b441e1
MK
203static enum return_value_convention
204vax_return_value (struct gdbarch *gdbarch, struct type *type,
459f20b9
MK
205 struct regcache *regcache, gdb_byte *readbuf,
206 const gdb_byte *writebuf)
ea74468c 207{
67b441e1 208 int len = TYPE_LENGTH (type);
8cc49c48 209 gdb_byte buf[8];
ea74468c 210
67b441e1 211 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
caed1a45 212 || TYPE_CODE (type) == TYPE_CODE_UNION
67b441e1 213 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
e5483145
MK
214 {
215 /* The default on VAX is to return structures in static memory.
216 Consequently a function must return the address where we can
217 find the return value. */
218
219 if (readbuf)
220 {
221 ULONGEST addr;
222
223 regcache_raw_read_unsigned (regcache, VAX_R0_REGNUM, &addr);
224 read_memory (addr, readbuf, len);
225 }
226
227 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
228 }
ea74468c 229
67b441e1
MK
230 if (readbuf)
231 {
232 /* Read the contents of R0 and (if necessary) R1. */
233 regcache_cooked_read (regcache, VAX_R0_REGNUM, buf);
234 if (len > 4)
235 regcache_cooked_read (regcache, VAX_R1_REGNUM, buf + 4);
236 memcpy (readbuf, buf, len);
237 }
238 if (writebuf)
239 {
240 /* Read the contents to R0 and (if necessary) R1. */
241 memcpy (buf, writebuf, len);
242 regcache_cooked_write (regcache, VAX_R0_REGNUM, buf);
243 if (len > 4)
244 regcache_cooked_write (regcache, VAX_R1_REGNUM, buf + 4);
245 }
246
247 return RETURN_VALUE_REGISTER_CONVENTION;
ea74468c 248}
ea74468c 249\f
5e6b39ff
MK
250
251/* Use the program counter to determine the contents and size of a
252 breakpoint instruction. Return a pointer to a string of bytes that
253 encode a breakpoint instruction, store the length of the string in
254 *LEN and optionally adjust *PC to point to the correct memory
255 location for inserting the breakpoint. */
256
8cc49c48 257static const gdb_byte *
5e6b39ff 258vax_breakpoint_from_pc (CORE_ADDR *pc, int *len)
1d049c5e 259{
8cc49c48 260 static gdb_byte break_insn[] = { 3 };
1d049c5e 261
5e6b39ff
MK
262 *len = sizeof (break_insn);
263 return break_insn;
1d049c5e
JT
264}
265\f
b83266a0
SS
266/* Advance PC across any function entry prologue instructions
267 to reach some "real" code. */
268
f267bd6a 269static CORE_ADDR
fba45db2 270vax_skip_prologue (CORE_ADDR pc)
b83266a0 271{
8cc49c48 272 gdb_byte op = read_memory_unsigned_integer (pc, 1);
0543f387 273
b83266a0 274 if (op == 0x11)
c5aa993b 275 pc += 2; /* skip brb */
b83266a0 276 if (op == 0x31)
c5aa993b 277 pc += 3; /* skip brw */
b83266a0 278 if (op == 0xC2
0543f387 279 && (read_memory_unsigned_integer (pc + 2, 1)) == 0x5E)
c5aa993b 280 pc += 3; /* skip subl2 */
b83266a0 281 if (op == 0x9E
0543f387
MK
282 && (read_memory_unsigned_integer (pc + 1, 1)) == 0xAE
283 && (read_memory_unsigned_integer (pc + 3, 1)) == 0x5E)
c5aa993b 284 pc += 4; /* skip movab */
b83266a0 285 if (op == 0x9E
0543f387
MK
286 && (read_memory_unsigned_integer (pc + 1, 1)) == 0xCE
287 && (read_memory_unsigned_integer (pc + 4, 1)) == 0x5E)
c5aa993b 288 pc += 5; /* skip movab */
b83266a0 289 if (op == 0x9E
0543f387
MK
290 && (read_memory_unsigned_integer (pc + 1, 1)) == 0xEE
291 && (read_memory_unsigned_integer (pc + 6, 1)) == 0x5E)
c5aa993b 292 pc += 7; /* skip movab */
0543f387 293
b83266a0
SS
294 return pc;
295}
7def7fef
MK
296\f
297
298/* Unwinding the stack is relatively easy since the VAX has a
299 dedicated frame pointer, and frames are set up automatically as the
300 result of a function call. Most of the relevant information can be
301 inferred from the documentation of the Procedure Call Instructions
302 in the VAX MACRO and Instruction Set Reference Manual. */
303
304struct vax_frame_cache
305{
306 /* Base address. */
307 CORE_ADDR base;
308
309 /* Table of saved registers. */
310 struct trad_frame_saved_reg *saved_regs;
311};
312
313struct vax_frame_cache *
314vax_frame_cache (struct frame_info *next_frame, void **this_cache)
315{
316 struct vax_frame_cache *cache;
317 CORE_ADDR addr;
318 ULONGEST mask;
319 int regnum;
320
321 if (*this_cache)
322 return *this_cache;
323
324 /* Allocate a new cache. */
325 cache = FRAME_OBSTACK_ZALLOC (struct vax_frame_cache);
326 cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
327
328 /* The frame pointer is used as the base for the frame. */
329 cache->base = frame_unwind_register_unsigned (next_frame, VAX_FP_REGNUM);
330 if (cache->base == 0)
331 return cache;
332
333 /* The register save mask and control bits determine the layout of
334 the stack frame. */
335 mask = get_frame_memory_unsigned (next_frame, cache->base + 4, 4) >> 16;
336
337 /* These are always saved. */
338 cache->saved_regs[VAX_PC_REGNUM].addr = cache->base + 16;
339 cache->saved_regs[VAX_FP_REGNUM].addr = cache->base + 12;
340 cache->saved_regs[VAX_AP_REGNUM].addr = cache->base + 8;
341 cache->saved_regs[VAX_PS_REGNUM].addr = cache->base + 4;
342
343 /* Scan the register save mask and record the location of the saved
344 registers. */
345 addr = cache->base + 20;
346 for (regnum = 0; regnum < VAX_AP_REGNUM; regnum++)
347 {
348 if (mask & (1 << regnum))
349 {
350 cache->saved_regs[regnum].addr = addr;
351 addr += 4;
352 }
353 }
354
355 /* The CALLS/CALLG flag determines whether this frame has a General
356 Argument List or a Stack Argument List. */
357 if (mask & (1 << 13))
358 {
359 ULONGEST numarg;
360
361 /* This is a procedure with Stack Argument List. Adjust the
e36ad527 362 stack address for the arguments that were pushed onto the
7def7fef
MK
363 stack. The return instruction will automatically pop the
364 arguments from the stack. */
365 numarg = get_frame_memory_unsigned (next_frame, addr, 1);
366 addr += 4 + numarg * 4;
367 }
368
369 /* Bits 1:0 of the stack pointer were saved in the control bits. */
370 trad_frame_set_value (cache->saved_regs, VAX_SP_REGNUM, addr + (mask >> 14));
371
372 return cache;
373}
374
375static void
376vax_frame_this_id (struct frame_info *next_frame, void **this_cache,
377 struct frame_id *this_id)
378{
379 struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
380
381 /* This marks the outermost frame. */
382 if (cache->base == 0)
383 return;
384
93d42b30
DJ
385 (*this_id) = frame_id_build (cache->base,
386 frame_func_unwind (next_frame, NORMAL_FRAME));
7def7fef
MK
387}
388
389static void
390vax_frame_prev_register (struct frame_info *next_frame, void **this_cache,
391 int regnum, int *optimizedp,
392 enum lval_type *lvalp, CORE_ADDR *addrp,
81e51e70 393 int *realnump, gdb_byte *valuep)
7def7fef
MK
394{
395 struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
396
1f67027d
AC
397 trad_frame_get_prev_register (next_frame, cache->saved_regs, regnum,
398 optimizedp, lvalp, addrp, realnump, valuep);
7def7fef
MK
399}
400
401static const struct frame_unwind vax_frame_unwind =
402{
403 NORMAL_FRAME,
404 vax_frame_this_id,
405 vax_frame_prev_register
406};
407
408static const struct frame_unwind *
409vax_frame_sniffer (struct frame_info *next_frame)
410{
411 return &vax_frame_unwind;
412}
413\f
414
415static CORE_ADDR
416vax_frame_base_address (struct frame_info *next_frame, void **this_cache)
417{
418 struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
419
420 return cache->base;
421}
b83266a0 422
f267bd6a 423static CORE_ADDR
7def7fef
MK
424vax_frame_args_address (struct frame_info *next_frame, void **this_cache)
425{
426 return frame_unwind_register_unsigned (next_frame, VAX_AP_REGNUM);
427}
428
429static const struct frame_base vax_frame_base =
a33f7558 430{
7def7fef
MK
431 &vax_frame_unwind,
432 vax_frame_base_address,
433 vax_frame_base_address,
434 vax_frame_args_address
435};
436
437/* Return number of arguments for FRAME. */
438
439static int
440vax_frame_num_args (struct frame_info *frame)
441{
442 CORE_ADDR args;
443
444 /* Assume that the argument pointer for the outermost frame is
445 hosed, as is the case on NetBSD/vax ELF. */
a54f9a00 446 if (get_frame_base_address (frame) == 0)
7def7fef
MK
447 return 0;
448
449 args = get_frame_register_unsigned (frame, VAX_AP_REGNUM);
450 return get_frame_memory_unsigned (frame, args, 1);
451}
452
453static CORE_ADDR
454vax_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
455{
456 return frame_unwind_register_unsigned (next_frame, VAX_PC_REGNUM);
a33f7558
JT
457}
458\f
7def7fef 459
f267bd6a
JT
460/* Initialize the current architecture based on INFO. If possible, re-use an
461 architecture from ARCHES, which is a list of architectures already created
462 during this debugging session.
463
464 Called e.g. at program startup, when reading a core file, and when reading
465 a binary file. */
466
467static struct gdbarch *
468vax_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
469{
470 struct gdbarch *gdbarch;
471
4be87837
DJ
472 /* If there is already a candidate, use it. */
473 arches = gdbarch_list_lookup_by_info (arches, &info);
474 if (arches != NULL)
475 return arches->gdbarch;
f267bd6a 476
4be87837 477 gdbarch = gdbarch_alloc (&info, NULL);
4791e091 478
8da61cc4
DJ
479 set_gdbarch_float_format (gdbarch, floatformats_vax_f);
480 set_gdbarch_double_format (gdbarch, floatformats_vax_d);
481 set_gdbarch_long_double_format (gdbarch, floatformats_vax_d);
482 set_gdbarch_long_double_bit (gdbarch, 64);
0a3e99f6 483
f267bd6a
JT
484 /* Register info */
485 set_gdbarch_num_regs (gdbarch, VAX_NUM_REGS);
5e6b39ff
MK
486 set_gdbarch_register_name (gdbarch, vax_register_name);
487 set_gdbarch_register_type (gdbarch, vax_register_type);
f267bd6a 488 set_gdbarch_sp_regnum (gdbarch, VAX_SP_REGNUM);
f267bd6a
JT
489 set_gdbarch_pc_regnum (gdbarch, VAX_PC_REGNUM);
490 set_gdbarch_ps_regnum (gdbarch, VAX_PS_REGNUM);
491
8b910bab
MK
492 set_gdbarch_regset_from_core_section
493 (gdbarch, vax_regset_from_core_section);
494
f267bd6a
JT
495 /* Frame and stack info */
496 set_gdbarch_skip_prologue (gdbarch, vax_skip_prologue);
f267bd6a 497 set_gdbarch_frame_num_args (gdbarch, vax_frame_num_args);
f267bd6a
JT
498 set_gdbarch_frame_args_skip (gdbarch, 4);
499
5e6b39ff 500 /* Stack grows downward. */
f267bd6a
JT
501 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
502
503 /* Return value info */
67b441e1 504 set_gdbarch_return_value (gdbarch, vax_return_value);
f267bd6a 505
7346c184
MK
506 /* Call dummy code. */
507 set_gdbarch_push_dummy_call (gdbarch, vax_push_dummy_call);
508 set_gdbarch_unwind_dummy_id (gdbarch, vax_unwind_dummy_id);
f267bd6a
JT
509
510 /* Breakpoint info */
1d049c5e 511 set_gdbarch_breakpoint_from_pc (gdbarch, vax_breakpoint_from_pc);
f267bd6a
JT
512
513 /* Misc info */
782263ab 514 set_gdbarch_deprecated_function_start_offset (gdbarch, 2);
1d049c5e 515 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
f267bd6a 516
0543f387
MK
517 set_gdbarch_print_insn (gdbarch, print_insn_vax);
518
7def7fef
MK
519 set_gdbarch_unwind_pc (gdbarch, vax_unwind_pc);
520
521 frame_base_set_default (gdbarch, &vax_frame_base);
522
4791e091 523 /* Hook in ABI-specific overrides, if they have been registered. */
4be87837 524 gdbarch_init_osabi (info, gdbarch);
4791e091 525
7def7fef
MK
526 frame_unwind_append_sniffer (gdbarch, vax_frame_sniffer);
527
f267bd6a
JT
528 return (gdbarch);
529}
c906108c 530
0543f387
MK
531/* Provide a prototype to silence -Wmissing-prototypes. */
532void _initialize_vax_tdep (void);
a78f21af 533
c906108c 534void
fba45db2 535_initialize_vax_tdep (void)
c906108c 536{
4be87837 537 gdbarch_register (bfd_arch_vax, vax_gdbarch_init, NULL);
c906108c 538}