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