ef0b2cd5ef54ddb1280d0192e121ce1284ee0b09
[external/binutils.git] / gdb / alpha-tdep.c
1 /* Target-dependent code for the ALPHA architecture, for GDB, the GNU Debugger.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
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 2 of the License, or
10    (at your option) any later version.
11
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.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "frame.h"
24 #include "inferior.h"
25 #include "symtab.h"
26 #include "value.h"
27 #include "gdbcmd.h"
28 #include "gdbcore.h"
29 #include "dis-asm.h"
30 #include "symfile.h"
31 #include "objfiles.h"
32 #include "gdb_string.h"
33 #include "linespec.h"
34 #include "regcache.h"
35 #include "doublest.h"
36 #include "arch-utils.h"
37
38 #include "elf-bfd.h"
39
40 #include "alpha-tdep.h"
41
42 static gdbarch_init_ftype alpha_gdbarch_init;
43
44 static gdbarch_register_name_ftype alpha_register_name;
45 static gdbarch_register_raw_size_ftype alpha_register_raw_size;
46 static gdbarch_register_virtual_size_ftype alpha_register_virtual_size;
47 static gdbarch_register_virtual_type_ftype alpha_register_virtual_type;
48 static gdbarch_register_byte_ftype alpha_register_byte;
49 static gdbarch_cannot_fetch_register_ftype alpha_cannot_fetch_register;
50 static gdbarch_cannot_store_register_ftype alpha_cannot_store_register;
51 static gdbarch_register_convertible_ftype alpha_register_convertible;
52 static gdbarch_register_convert_to_virtual_ftype
53     alpha_register_convert_to_virtual;
54 static gdbarch_register_convert_to_raw_ftype alpha_register_convert_to_raw;
55 static gdbarch_store_struct_return_ftype alpha_store_struct_return;
56 static gdbarch_extract_return_value_ftype alpha_extract_return_value;
57 static gdbarch_store_return_value_ftype alpha_store_return_value;
58 static gdbarch_extract_struct_value_address_ftype
59     alpha_extract_struct_value_address;
60 static gdbarch_use_struct_convention_ftype alpha_use_struct_convention;
61
62 static gdbarch_frame_args_address_ftype alpha_frame_args_address;
63 static gdbarch_frame_locals_address_ftype alpha_frame_locals_address;
64
65 static gdbarch_skip_prologue_ftype alpha_skip_prologue;
66 static gdbarch_get_saved_register_ftype alpha_get_saved_register;
67 static gdbarch_saved_pc_after_call_ftype alpha_saved_pc_after_call;
68 static gdbarch_frame_chain_ftype alpha_frame_chain;
69 static gdbarch_frame_saved_pc_ftype alpha_frame_saved_pc;
70 static gdbarch_frame_init_saved_regs_ftype alpha_frame_init_saved_regs;
71
72 static gdbarch_push_arguments_ftype alpha_push_arguments;
73 static gdbarch_push_dummy_frame_ftype alpha_push_dummy_frame;
74 static gdbarch_pop_frame_ftype alpha_pop_frame;
75 static gdbarch_fix_call_dummy_ftype alpha_fix_call_dummy;
76 static gdbarch_init_frame_pc_first_ftype alpha_init_frame_pc_first;
77 static gdbarch_init_extra_frame_info_ftype alpha_init_extra_frame_info;
78
79 struct frame_extra_info
80   {
81     alpha_extra_func_info_t proc_desc;
82     int localoff;
83     int pc_reg;
84   };
85
86 /* FIXME: Some of this code should perhaps be merged with mips-tdep.c.  */
87
88 /* Prototypes for local functions. */
89
90 static void alpha_find_saved_regs (struct frame_info *);
91
92 static alpha_extra_func_info_t push_sigtramp_desc (CORE_ADDR low_addr);
93
94 static CORE_ADDR read_next_frame_reg (struct frame_info *, int);
95
96 static CORE_ADDR heuristic_proc_start (CORE_ADDR);
97
98 static alpha_extra_func_info_t heuristic_proc_desc (CORE_ADDR,
99                                                     CORE_ADDR,
100                                                     struct frame_info *);
101
102 static alpha_extra_func_info_t find_proc_desc (CORE_ADDR,
103                                                struct frame_info *);
104
105 #if 0
106 static int alpha_in_lenient_prologue (CORE_ADDR, CORE_ADDR);
107 #endif
108
109 static void reinit_frame_cache_sfunc (char *, int, struct cmd_list_element *);
110
111 static CORE_ADDR after_prologue (CORE_ADDR pc,
112                                  alpha_extra_func_info_t proc_desc);
113
114 static int alpha_in_prologue (CORE_ADDR pc,
115                               alpha_extra_func_info_t proc_desc);
116
117 static int alpha_about_to_return (CORE_ADDR pc);
118
119 void _initialize_alpha_tdep (void);
120
121 /* Heuristic_proc_start may hunt through the text section for a long
122    time across a 2400 baud serial line.  Allows the user to limit this
123    search.  */
124 static unsigned int heuristic_fence_post = 0;
125 /* *INDENT-OFF* */
126 /* Layout of a stack frame on the alpha:
127
128                 |                               |
129  pdr members:   |  7th ... nth arg,             |
130                 |  `pushed' by caller.          |
131                 |                               |
132 ----------------|-------------------------------|<--  old_sp == vfp
133    ^  ^  ^  ^   |                               |
134    |  |  |  |   |                               |
135    |  |localoff |  Copies of 1st .. 6th         |
136    |  |  |  |   |  argument if necessary.       |
137    |  |  |  v   |                               |
138    |  |  |  --- |-------------------------------|<-- FRAME_LOCALS_ADDRESS
139    |  |  |      |                               |
140    |  |  |      |  Locals and temporaries.      |
141    |  |  |      |                               |
142    |  |  |      |-------------------------------|
143    |  |  |      |                               |
144    |-fregoffset |  Saved float registers.       |
145    |  |  |      |  F9                           |
146    |  |  |      |   .                           |
147    |  |  |      |   .                           |
148    |  |  |      |  F2                           |
149    |  |  v      |                               |
150    |  |  -------|-------------------------------|
151    |  |         |                               |
152    |  |         |  Saved registers.             |
153    |  |         |  S6                           |
154    |-regoffset  |   .                           |
155    |  |         |   .                           |
156    |  |         |  S0                           |
157    |  |         |  pdr.pcreg                    |
158    |  v         |                               |
159    |  ----------|-------------------------------|
160    |            |                               |
161  frameoffset    |  Argument build area, gets    |
162    |            |  7th ... nth arg for any      |
163    |            |  called procedure.            |
164    v            |                               |
165    -------------|-------------------------------|<-- sp
166                 |                               |
167 */
168 /* *INDENT-ON* */
169
170 #define PROC_LOW_ADDR(proc) ((proc)->pdr.adr)   /* least address */
171 /* These next two fields are kind of being hijacked.  I wonder if
172    iline is too small for the values it needs to hold, if GDB is
173    running on a 32-bit host.  */
174 #define PROC_HIGH_ADDR(proc) ((proc)->pdr.iline)        /* upper address bound */
175 #define PROC_DUMMY_FRAME(proc) ((proc)->pdr.cbLineOffset)       /*CALL_DUMMY frame */
176 #define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
177 #define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
178 #define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
179 #define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
180 #define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
181 #define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
182 #define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
183 #define PROC_LOCALOFF(proc) ((proc)->pdr.localoff)
184 #define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
185 #define _PROC_MAGIC_ 0x0F0F0F0F
186 #define PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym == _PROC_MAGIC_)
187 #define SET_PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym = _PROC_MAGIC_)
188
189 struct linked_proc_info
190   {
191     struct alpha_extra_func_info info;
192     struct linked_proc_info *next;
193   }
194  *linked_proc_desc_table = NULL;
195 \f
196 int
197 alpha_osf_in_sigtramp (CORE_ADDR pc, char *func_name)
198 {
199   return (func_name != NULL && STREQ ("__sigtramp", func_name));
200 }
201
202 static CORE_ADDR
203 alpha_frame_past_sigtramp_frame (struct frame_info *frame, CORE_ADDR pc)
204 {
205   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
206
207   if (tdep->skip_sigtramp_frame != NULL)
208     return (tdep->skip_sigtramp_frame (frame, pc));
209
210   return (0);
211 }
212
213 static LONGEST
214 alpha_dynamic_sigtramp_offset (CORE_ADDR pc)
215 {
216   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
217
218   /* Must be provided by OS/ABI variant code if supported. */
219   if (tdep->dynamic_sigtramp_offset != NULL)
220     return (tdep->dynamic_sigtramp_offset (pc));
221
222   return (-1);
223 }
224
225 #define ALPHA_PROC_SIGTRAMP_MAGIC 0x0e0f0f0f
226
227 /* Return TRUE if the procedure descriptor PROC is a procedure
228    descriptor that refers to a dynamically generated signal
229    trampoline routine.  */
230 static int
231 alpha_proc_desc_is_dyn_sigtramp (struct alpha_extra_func_info *proc)
232 {
233   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
234
235   if (tdep->dynamic_sigtramp_offset != NULL)
236     return (proc->pdr.isym == ALPHA_PROC_SIGTRAMP_MAGIC);
237
238   return (0);
239 }
240
241 static void
242 alpha_set_proc_desc_is_dyn_sigtramp (struct alpha_extra_func_info *proc)
243 {
244   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
245
246   if (tdep->dynamic_sigtramp_offset != NULL)
247     proc->pdr.isym = ALPHA_PROC_SIGTRAMP_MAGIC;
248 }
249
250 /* Dynamically create a signal-handler caller procedure descriptor for
251    the signal-handler return code starting at address LOW_ADDR.  The
252    descriptor is added to the linked_proc_desc_table.  */
253
254 static alpha_extra_func_info_t
255 push_sigtramp_desc (CORE_ADDR low_addr)
256 {
257   struct linked_proc_info *link;
258   alpha_extra_func_info_t proc_desc;
259
260   link = (struct linked_proc_info *)
261     xmalloc (sizeof (struct linked_proc_info));
262   link->next = linked_proc_desc_table;
263   linked_proc_desc_table = link;
264
265   proc_desc = &link->info;
266
267   proc_desc->numargs = 0;
268   PROC_LOW_ADDR (proc_desc) = low_addr;
269   PROC_HIGH_ADDR (proc_desc) = low_addr + 3 * 4;
270   PROC_DUMMY_FRAME (proc_desc) = 0;
271   PROC_FRAME_OFFSET (proc_desc) = 0x298;        /* sizeof(struct sigcontext_struct) */
272   PROC_FRAME_REG (proc_desc) = SP_REGNUM;
273   PROC_REG_MASK (proc_desc) = 0xffff;
274   PROC_FREG_MASK (proc_desc) = 0xffff;
275   PROC_PC_REG (proc_desc) = 26;
276   PROC_LOCALOFF (proc_desc) = 0;
277   alpha_set_proc_desc_is_dyn_sigtramp (proc_desc);
278   return (proc_desc);
279 }
280 \f
281
282 static char *
283 alpha_register_name (int regno)
284 {
285   static char *register_names[] =
286   {
287     "v0",   "t0",   "t1",   "t2",   "t3",   "t4",   "t5",   "t6",
288     "t7",   "s0",   "s1",   "s2",   "s3",   "s4",   "s5",   "fp",
289     "a0",   "a1",   "a2",   "a3",   "a4",   "a5",   "t8",   "t9",
290     "t10",  "t11",  "ra",   "t12",  "at",   "gp",   "sp",   "zero",
291     "f0",   "f1",   "f2",   "f3",   "f4",   "f5",   "f6",   "f7",
292     "f8",   "f9",   "f10",  "f11",  "f12",  "f13",  "f14",  "f15",
293     "f16",  "f17",  "f18",  "f19",  "f20",  "f21",  "f22",  "f23",
294     "f24",  "f25",  "f26",  "f27",  "f28",  "f29",  "f30",  "fpcr",
295     "pc",   "vfp",
296   };
297
298   if (regno < 0)
299     return (NULL);
300   if (regno >= (sizeof(register_names) / sizeof(*register_names)))
301     return (NULL);
302   return (register_names[regno]);
303 }
304
305 static int
306 alpha_cannot_fetch_register (int regno)
307 {
308   return (regno == FP_REGNUM || regno == ALPHA_ZERO_REGNUM);
309 }
310
311 static int
312 alpha_cannot_store_register (int regno)
313 {
314   return (regno == FP_REGNUM || regno == ALPHA_ZERO_REGNUM);
315 }
316
317 static int
318 alpha_register_convertible (int regno)
319 {
320   return (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 31);
321 }
322
323 static struct type *
324 alpha_register_virtual_type (int regno)
325 {
326   return ((regno >= FP0_REGNUM && regno < (FP0_REGNUM+31))
327           ? builtin_type_double : builtin_type_long);
328 }
329
330 static int
331 alpha_register_byte (int regno)
332 {
333   return (regno * 8);
334 }
335
336 static int
337 alpha_register_raw_size (int regno)
338 {
339   return 8;
340 }
341
342 static int
343 alpha_register_virtual_size (int regno)
344 {
345   return 8;
346 }
347 \f
348
349 /* Guaranteed to set frame->saved_regs to some values (it never leaves it
350    NULL).  */
351
352 static void
353 alpha_find_saved_regs (struct frame_info *frame)
354 {
355   int ireg;
356   CORE_ADDR reg_position;
357   unsigned long mask;
358   alpha_extra_func_info_t proc_desc;
359   int returnreg;
360
361   frame_saved_regs_zalloc (frame);
362
363   /* If it is the frame for __sigtramp, the saved registers are located
364      in a sigcontext structure somewhere on the stack. __sigtramp
365      passes a pointer to the sigcontext structure on the stack.
366      If the stack layout for __sigtramp changes, or if sigcontext offsets
367      change, we might have to update this code.  */
368 #ifndef SIGFRAME_PC_OFF
369 #define SIGFRAME_PC_OFF         (2 * 8)
370 #define SIGFRAME_REGSAVE_OFF    (4 * 8)
371 #define SIGFRAME_FPREGSAVE_OFF  (SIGFRAME_REGSAVE_OFF + 32 * 8 + 8)
372 #endif
373   if (frame->signal_handler_caller)
374     {
375       CORE_ADDR sigcontext_addr;
376
377       sigcontext_addr = SIGCONTEXT_ADDR (frame);
378       for (ireg = 0; ireg < 32; ireg++)
379         {
380           reg_position = sigcontext_addr + SIGFRAME_REGSAVE_OFF + ireg * 8;
381           frame->saved_regs[ireg] = reg_position;
382         }
383       for (ireg = 0; ireg < 32; ireg++)
384         {
385           reg_position = sigcontext_addr + SIGFRAME_FPREGSAVE_OFF + ireg * 8;
386           frame->saved_regs[FP0_REGNUM + ireg] = reg_position;
387         }
388       frame->saved_regs[PC_REGNUM] = sigcontext_addr + SIGFRAME_PC_OFF;
389       return;
390     }
391
392   proc_desc = frame->extra_info->proc_desc;
393   if (proc_desc == NULL)
394     /* I'm not sure how/whether this can happen.  Normally when we can't
395        find a proc_desc, we "synthesize" one using heuristic_proc_desc
396        and set the saved_regs right away.  */
397     return;
398
399   /* Fill in the offsets for the registers which gen_mask says
400      were saved.  */
401
402   reg_position = frame->frame + PROC_REG_OFFSET (proc_desc);
403   mask = PROC_REG_MASK (proc_desc);
404
405   returnreg = PROC_PC_REG (proc_desc);
406
407   /* Note that RA is always saved first, regardless of its actual
408      register number.  */
409   if (mask & (1 << returnreg))
410     {
411       frame->saved_regs[returnreg] = reg_position;
412       reg_position += 8;
413       mask &= ~(1 << returnreg);        /* Clear bit for RA so we
414                                            don't save again later. */
415     }
416
417   for (ireg = 0; ireg <= 31; ++ireg)
418     if (mask & (1 << ireg))
419       {
420         frame->saved_regs[ireg] = reg_position;
421         reg_position += 8;
422       }
423
424   /* Fill in the offsets for the registers which float_mask says
425      were saved.  */
426
427   reg_position = frame->frame + PROC_FREG_OFFSET (proc_desc);
428   mask = PROC_FREG_MASK (proc_desc);
429
430   for (ireg = 0; ireg <= 31; ++ireg)
431     if (mask & (1 << ireg))
432       {
433         frame->saved_regs[FP0_REGNUM + ireg] = reg_position;
434         reg_position += 8;
435       }
436
437   frame->saved_regs[PC_REGNUM] = frame->saved_regs[returnreg];
438 }
439
440 static void
441 alpha_frame_init_saved_regs (struct frame_info *fi)
442 {
443   if (fi->saved_regs == NULL)
444     alpha_find_saved_regs (fi);
445   fi->saved_regs[SP_REGNUM] = fi->frame;
446 }
447
448 static void
449 alpha_init_frame_pc_first (int fromleaf, struct frame_info *prev)
450 {
451   prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) :
452               prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
453 }
454
455 static CORE_ADDR
456 read_next_frame_reg (struct frame_info *fi, int regno)
457 {
458   for (; fi; fi = fi->next)
459     {
460       /* We have to get the saved sp from the sigcontext
461          if it is a signal handler frame.  */
462       if (regno == SP_REGNUM && !fi->signal_handler_caller)
463         return fi->frame;
464       else
465         {
466           if (fi->saved_regs == NULL)
467             alpha_find_saved_regs (fi);
468           if (fi->saved_regs[regno])
469             return read_memory_integer (fi->saved_regs[regno], 8);
470         }
471     }
472   return read_register (regno);
473 }
474
475 static CORE_ADDR
476 alpha_frame_saved_pc (struct frame_info *frame)
477 {
478   alpha_extra_func_info_t proc_desc = frame->extra_info->proc_desc;
479   /* We have to get the saved pc from the sigcontext
480      if it is a signal handler frame.  */
481   int pcreg = frame->signal_handler_caller ? PC_REGNUM
482                                            : frame->extra_info->pc_reg;
483
484   if (proc_desc && PROC_DESC_IS_DUMMY (proc_desc))
485     return read_memory_integer (frame->frame - 8, 8);
486
487   return read_next_frame_reg (frame, pcreg);
488 }
489
490 static void
491 alpha_get_saved_register (char *raw_buffer,
492                           int *optimized,
493                           CORE_ADDR *addrp,
494                           struct frame_info *frame,
495                           int regnum,
496                           enum lval_type *lval)
497 {
498   CORE_ADDR addr;
499
500   if (!target_has_registers)
501     error ("No registers.");
502
503   /* Normal systems don't optimize out things with register numbers.  */
504   if (optimized != NULL)
505     *optimized = 0;
506   addr = find_saved_register (frame, regnum);
507   if (addr != 0)
508     {
509       if (lval != NULL)
510         *lval = lval_memory;
511       if (regnum == SP_REGNUM)
512         {
513           if (raw_buffer != NULL)
514             {
515               /* Put it back in target format.  */
516               store_address (raw_buffer, REGISTER_RAW_SIZE (regnum),
517                              (LONGEST) addr);
518             }
519           if (addrp != NULL)
520             *addrp = 0;
521           return;
522         }
523       if (raw_buffer != NULL)
524         target_read_memory (addr, raw_buffer, REGISTER_RAW_SIZE (regnum));
525     }
526   else
527     {
528       if (lval != NULL)
529         *lval = lval_register;
530       addr = REGISTER_BYTE (regnum);
531       if (raw_buffer != NULL)
532         read_register_gen (regnum, raw_buffer);
533     }
534   if (addrp != NULL)
535     *addrp = addr;
536 }
537
538 static CORE_ADDR
539 alpha_saved_pc_after_call (struct frame_info *frame)
540 {
541   CORE_ADDR pc = frame->pc;
542   CORE_ADDR tmp;
543   alpha_extra_func_info_t proc_desc;
544   int pcreg;
545
546   /* Skip over shared library trampoline if necessary.  */
547   tmp = SKIP_TRAMPOLINE_CODE (pc);
548   if (tmp != 0)
549     pc = tmp;
550
551   proc_desc = find_proc_desc (pc, frame->next);
552   pcreg = proc_desc ? PROC_PC_REG (proc_desc) : ALPHA_RA_REGNUM;
553
554   if (frame->signal_handler_caller)
555     return alpha_frame_saved_pc (frame);
556   else
557     return read_register (pcreg);
558 }
559
560
561 static struct alpha_extra_func_info temp_proc_desc;
562 static CORE_ADDR temp_saved_regs[ALPHA_NUM_REGS];
563
564 /* Nonzero if instruction at PC is a return instruction.  "ret
565    $zero,($ra),1" on alpha. */
566
567 static int
568 alpha_about_to_return (CORE_ADDR pc)
569 {
570   return read_memory_integer (pc, 4) == 0x6bfa8001;
571 }
572
573
574
575 /* This fencepost looks highly suspicious to me.  Removing it also
576    seems suspicious as it could affect remote debugging across serial
577    lines.  */
578
579 static CORE_ADDR
580 heuristic_proc_start (CORE_ADDR pc)
581 {
582   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
583   CORE_ADDR start_pc = pc;
584   CORE_ADDR fence = start_pc - heuristic_fence_post;
585
586   if (start_pc == 0)
587     return 0;
588
589   if (heuristic_fence_post == UINT_MAX
590       || fence < tdep->vm_min_address)
591     fence = tdep->vm_min_address;
592
593   /* search back for previous return */
594   for (start_pc -= 4;; start_pc -= 4)
595     if (start_pc < fence)
596       {
597         /* It's not clear to me why we reach this point when
598            stop_soon_quietly, but with this test, at least we
599            don't print out warnings for every child forked (eg, on
600            decstation).  22apr93 rich@cygnus.com.  */
601         if (!stop_soon_quietly)
602           {
603             static int blurb_printed = 0;
604
605             if (fence == tdep->vm_min_address)
606               warning ("Hit beginning of text section without finding");
607             else
608               warning ("Hit heuristic-fence-post without finding");
609
610             warning ("enclosing function for address 0x%s", paddr_nz (pc));
611             if (!blurb_printed)
612               {
613                 printf_filtered ("\
614 This warning occurs if you are debugging a function without any symbols\n\
615 (for example, in a stripped executable).  In that case, you may wish to\n\
616 increase the size of the search with the `set heuristic-fence-post' command.\n\
617 \n\
618 Otherwise, you told GDB there was a function where there isn't one, or\n\
619 (more likely) you have encountered a bug in GDB.\n");
620                 blurb_printed = 1;
621               }
622           }
623
624         return 0;
625       }
626     else if (alpha_about_to_return (start_pc))
627       break;
628
629   start_pc += 4;                /* skip return */
630   return start_pc;
631 }
632
633 static alpha_extra_func_info_t
634 heuristic_proc_desc (CORE_ADDR start_pc, CORE_ADDR limit_pc,
635                      struct frame_info *next_frame)
636 {
637   CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
638   CORE_ADDR cur_pc;
639   int frame_size;
640   int has_frame_reg = 0;
641   unsigned long reg_mask = 0;
642   int pcreg = -1;
643
644   if (start_pc == 0)
645     return NULL;
646   memset (&temp_proc_desc, '\0', sizeof (temp_proc_desc));
647   memset (&temp_saved_regs, '\0', SIZEOF_FRAME_SAVED_REGS);
648   PROC_LOW_ADDR (&temp_proc_desc) = start_pc;
649
650   if (start_pc + 200 < limit_pc)
651     limit_pc = start_pc + 200;
652   frame_size = 0;
653   for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4)
654     {
655       char buf[4];
656       unsigned long word;
657       int status;
658
659       status = read_memory_nobpt (cur_pc, buf, 4);
660       if (status)
661         memory_error (status, cur_pc);
662       word = extract_unsigned_integer (buf, 4);
663
664       if ((word & 0xffff0000) == 0x23de0000)    /* lda $sp,n($sp) */
665         {
666           if (word & 0x8000)
667             frame_size += (-word) & 0xffff;
668           else
669             /* Exit loop if a positive stack adjustment is found, which
670                usually means that the stack cleanup code in the function
671                epilogue is reached.  */
672             break;
673         }
674       else if ((word & 0xfc1f0000) == 0xb41e0000        /* stq reg,n($sp) */
675                && (word & 0xffff0000) != 0xb7fe0000)    /* reg != $zero */
676         {
677           int reg = (word & 0x03e00000) >> 21;
678           reg_mask |= 1 << reg;
679           temp_saved_regs[reg] = sp + (short) word;
680
681           /* Starting with OSF/1-3.2C, the system libraries are shipped
682              without local symbols, but they still contain procedure
683              descriptors without a symbol reference. GDB is currently
684              unable to find these procedure descriptors and uses
685              heuristic_proc_desc instead.
686              As some low level compiler support routines (__div*, __add*)
687              use a non-standard return address register, we have to
688              add some heuristics to determine the return address register,
689              or stepping over these routines will fail.
690              Usually the return address register is the first register
691              saved on the stack, but assembler optimization might
692              rearrange the register saves.
693              So we recognize only a few registers (t7, t9, ra) within
694              the procedure prologue as valid return address registers.
695              If we encounter a return instruction, we extract the
696              the return address register from it.
697
698              FIXME: Rewriting GDB to access the procedure descriptors,
699              e.g. via the minimal symbol table, might obviate this hack.  */
700           if (pcreg == -1
701               && cur_pc < (start_pc + 80)
702               && (reg == ALPHA_T7_REGNUM || reg == ALPHA_T9_REGNUM
703                   || reg == ALPHA_RA_REGNUM))
704             pcreg = reg;
705         }
706       else if ((word & 0xffe0ffff) == 0x6be08001)       /* ret zero,reg,1 */
707         pcreg = (word >> 16) & 0x1f;
708       else if (word == 0x47de040f)      /* bis sp,sp fp */
709         has_frame_reg = 1;
710     }
711   if (pcreg == -1)
712     {
713       /* If we haven't found a valid return address register yet,
714          keep searching in the procedure prologue.  */
715       while (cur_pc < (limit_pc + 80) && cur_pc < (start_pc + 80))
716         {
717           char buf[4];
718           unsigned long word;
719
720           if (read_memory_nobpt (cur_pc, buf, 4))
721             break;
722           cur_pc += 4;
723           word = extract_unsigned_integer (buf, 4);
724
725           if ((word & 0xfc1f0000) == 0xb41e0000         /* stq reg,n($sp) */
726               && (word & 0xffff0000) != 0xb7fe0000)     /* reg != $zero */
727             {
728               int reg = (word & 0x03e00000) >> 21;
729               if (reg == ALPHA_T7_REGNUM || reg == ALPHA_T9_REGNUM
730                   || reg == ALPHA_RA_REGNUM)
731                 {
732                   pcreg = reg;
733                   break;
734                 }
735             }
736           else if ((word & 0xffe0ffff) == 0x6be08001)   /* ret zero,reg,1 */
737             {
738               pcreg = (word >> 16) & 0x1f;
739               break;
740             }
741         }
742     }
743
744   if (has_frame_reg)
745     PROC_FRAME_REG (&temp_proc_desc) = ALPHA_GCC_FP_REGNUM;
746   else
747     PROC_FRAME_REG (&temp_proc_desc) = SP_REGNUM;
748   PROC_FRAME_OFFSET (&temp_proc_desc) = frame_size;
749   PROC_REG_MASK (&temp_proc_desc) = reg_mask;
750   PROC_PC_REG (&temp_proc_desc) = (pcreg == -1) ? ALPHA_RA_REGNUM : pcreg;
751   PROC_LOCALOFF (&temp_proc_desc) = 0;  /* XXX - bogus */
752   return &temp_proc_desc;
753 }
754
755 /* This returns the PC of the first inst after the prologue.  If we can't
756    find the prologue, then return 0.  */
757
758 static CORE_ADDR
759 after_prologue (CORE_ADDR pc, alpha_extra_func_info_t proc_desc)
760 {
761   struct symtab_and_line sal;
762   CORE_ADDR func_addr, func_end;
763
764   if (!proc_desc)
765     proc_desc = find_proc_desc (pc, NULL);
766
767   if (proc_desc)
768     {
769       if (alpha_proc_desc_is_dyn_sigtramp (proc_desc))
770         return PROC_LOW_ADDR (proc_desc);       /* "prologue" is in kernel */
771
772       /* If function is frameless, then we need to do it the hard way.  I
773          strongly suspect that frameless always means prologueless... */
774       if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
775           && PROC_FRAME_OFFSET (proc_desc) == 0)
776         return 0;
777     }
778
779   if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
780     return 0;                   /* Unknown */
781
782   sal = find_pc_line (func_addr, 0);
783
784   if (sal.end < func_end)
785     return sal.end;
786
787   /* The line after the prologue is after the end of the function.  In this
788      case, tell the caller to find the prologue the hard way.  */
789
790   return 0;
791 }
792
793 /* Return non-zero if we *might* be in a function prologue.  Return zero if we
794    are definitively *not* in a function prologue.  */
795
796 static int
797 alpha_in_prologue (CORE_ADDR pc, alpha_extra_func_info_t proc_desc)
798 {
799   CORE_ADDR after_prologue_pc;
800
801   after_prologue_pc = after_prologue (pc, proc_desc);
802
803   if (after_prologue_pc == 0
804       || pc < after_prologue_pc)
805     return 1;
806   else
807     return 0;
808 }
809
810 static alpha_extra_func_info_t
811 find_proc_desc (CORE_ADDR pc, struct frame_info *next_frame)
812 {
813   alpha_extra_func_info_t proc_desc;
814   struct block *b;
815   struct symbol *sym;
816   CORE_ADDR startaddr;
817
818   /* Try to get the proc_desc from the linked call dummy proc_descs
819      if the pc is in the call dummy.
820      This is hairy. In the case of nested dummy calls we have to find the
821      right proc_desc, but we might not yet know the frame for the dummy
822      as it will be contained in the proc_desc we are searching for.
823      So we have to find the proc_desc whose frame is closest to the current
824      stack pointer.  */
825
826   if (PC_IN_CALL_DUMMY (pc, 0, 0))
827     {
828       struct linked_proc_info *link;
829       CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
830       alpha_extra_func_info_t found_proc_desc = NULL;
831       long min_distance = LONG_MAX;
832
833       for (link = linked_proc_desc_table; link; link = link->next)
834         {
835           long distance = (CORE_ADDR) PROC_DUMMY_FRAME (&link->info) - sp;
836           if (distance > 0 && distance < min_distance)
837             {
838               min_distance = distance;
839               found_proc_desc = &link->info;
840             }
841         }
842       if (found_proc_desc != NULL)
843         return found_proc_desc;
844     }
845
846   b = block_for_pc (pc);
847
848   find_pc_partial_function (pc, NULL, &startaddr, NULL);
849   if (b == NULL)
850     sym = NULL;
851   else
852     {
853       if (startaddr > BLOCK_START (b))
854         /* This is the "pathological" case referred to in a comment in
855            print_frame_info.  It might be better to move this check into
856            symbol reading.  */
857         sym = NULL;
858       else
859         sym = lookup_symbol (MIPS_EFI_SYMBOL_NAME, b, LABEL_NAMESPACE,
860                              0, NULL);
861     }
862
863   /* If we never found a PDR for this function in symbol reading, then
864      examine prologues to find the information.  */
865   if (sym && ((mips_extra_func_info_t) SYMBOL_VALUE (sym))->pdr.framereg == -1)
866     sym = NULL;
867
868   if (sym)
869     {
870       /* IF this is the topmost frame AND
871        * (this proc does not have debugging information OR
872        * the PC is in the procedure prologue)
873        * THEN create a "heuristic" proc_desc (by analyzing
874        * the actual code) to replace the "official" proc_desc.
875        */
876       proc_desc = (alpha_extra_func_info_t) SYMBOL_VALUE (sym);
877       if (next_frame == NULL)
878         {
879           if (PROC_DESC_IS_DUMMY (proc_desc) || alpha_in_prologue (pc, proc_desc))
880             {
881               alpha_extra_func_info_t found_heuristic =
882               heuristic_proc_desc (PROC_LOW_ADDR (proc_desc),
883                                    pc, next_frame);
884               if (found_heuristic)
885                 {
886                   PROC_LOCALOFF (found_heuristic) =
887                     PROC_LOCALOFF (proc_desc);
888                   PROC_PC_REG (found_heuristic) = PROC_PC_REG (proc_desc);
889                   proc_desc = found_heuristic;
890                 }
891             }
892         }
893     }
894   else
895     {
896       long offset;
897
898       /* Is linked_proc_desc_table really necessary?  It only seems to be used
899          by procedure call dummys.  However, the procedures being called ought
900          to have their own proc_descs, and even if they don't,
901          heuristic_proc_desc knows how to create them! */
902
903       register struct linked_proc_info *link;
904       for (link = linked_proc_desc_table; link; link = link->next)
905         if (PROC_LOW_ADDR (&link->info) <= pc
906             && PROC_HIGH_ADDR (&link->info) > pc)
907           return &link->info;
908
909       /* If PC is inside a dynamically generated sigtramp handler,
910          create and push a procedure descriptor for that code: */
911       offset = alpha_dynamic_sigtramp_offset (pc);
912       if (offset >= 0)
913         return push_sigtramp_desc (pc - offset);
914
915       /* If heuristic_fence_post is non-zero, determine the procedure
916          start address by examining the instructions.
917          This allows us to find the start address of static functions which
918          have no symbolic information, as startaddr would have been set to
919          the preceding global function start address by the
920          find_pc_partial_function call above.  */
921       if (startaddr == 0 || heuristic_fence_post != 0)
922         startaddr = heuristic_proc_start (pc);
923
924       proc_desc =
925         heuristic_proc_desc (startaddr, pc, next_frame);
926     }
927   return proc_desc;
928 }
929
930 alpha_extra_func_info_t cached_proc_desc;
931
932 static CORE_ADDR
933 alpha_frame_chain (struct frame_info *frame)
934 {
935   alpha_extra_func_info_t proc_desc;
936   CORE_ADDR saved_pc = FRAME_SAVED_PC (frame);
937
938   if (saved_pc == 0 || inside_entry_file (saved_pc))
939     return 0;
940
941   proc_desc = find_proc_desc (saved_pc, frame);
942   if (!proc_desc)
943     return 0;
944
945   cached_proc_desc = proc_desc;
946
947   /* Fetch the frame pointer for a dummy frame from the procedure
948      descriptor.  */
949   if (PROC_DESC_IS_DUMMY (proc_desc))
950     return (CORE_ADDR) PROC_DUMMY_FRAME (proc_desc);
951
952   /* If no frame pointer and frame size is zero, we must be at end
953      of stack (or otherwise hosed).  If we don't check frame size,
954      we loop forever if we see a zero size frame.  */
955   if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
956       && PROC_FRAME_OFFSET (proc_desc) == 0
957   /* The previous frame from a sigtramp frame might be frameless
958      and have frame size zero.  */
959       && !frame->signal_handler_caller)
960     return alpha_frame_past_sigtramp_frame (frame, saved_pc);
961   else
962     return read_next_frame_reg (frame, PROC_FRAME_REG (proc_desc))
963       + PROC_FRAME_OFFSET (proc_desc);
964 }
965
966 void
967 alpha_print_extra_frame_info (struct frame_info *fi)
968 {
969   if (fi
970       && fi->extra_info
971       && fi->extra_info->proc_desc
972       && fi->extra_info->proc_desc->pdr.framereg < NUM_REGS)
973     printf_filtered (" frame pointer is at %s+%s\n",
974                      REGISTER_NAME (fi->extra_info->proc_desc->pdr.framereg),
975                      paddr_d (fi->extra_info->proc_desc->pdr.frameoffset));
976 }
977
978 static void
979 alpha_init_extra_frame_info (int fromleaf, struct frame_info *frame)
980 {
981   /* Use proc_desc calculated in frame_chain */
982   alpha_extra_func_info_t proc_desc =
983   frame->next ? cached_proc_desc : find_proc_desc (frame->pc, frame->next);
984
985   frame->extra_info = (struct frame_extra_info *)
986     frame_obstack_alloc (sizeof (struct frame_extra_info));
987
988   frame->saved_regs = NULL;
989   frame->extra_info->localoff = 0;
990   frame->extra_info->pc_reg = ALPHA_RA_REGNUM;
991   frame->extra_info->proc_desc = proc_desc == &temp_proc_desc ? 0 : proc_desc;
992   if (proc_desc)
993     {
994       /* Get the locals offset and the saved pc register from the
995          procedure descriptor, they are valid even if we are in the
996          middle of the prologue.  */
997       frame->extra_info->localoff = PROC_LOCALOFF (proc_desc);
998       frame->extra_info->pc_reg = PROC_PC_REG (proc_desc);
999
1000       /* Fixup frame-pointer - only needed for top frame */
1001
1002       /* Fetch the frame pointer for a dummy frame from the procedure
1003          descriptor.  */
1004       if (PROC_DESC_IS_DUMMY (proc_desc))
1005         frame->frame = (CORE_ADDR) PROC_DUMMY_FRAME (proc_desc);
1006
1007       /* This may not be quite right, if proc has a real frame register.
1008          Get the value of the frame relative sp, procedure might have been
1009          interrupted by a signal at it's very start.  */
1010       else if (frame->pc == PROC_LOW_ADDR (proc_desc)
1011                && !alpha_proc_desc_is_dyn_sigtramp (proc_desc))
1012         frame->frame = read_next_frame_reg (frame->next, SP_REGNUM);
1013       else
1014         frame->frame = read_next_frame_reg (frame->next, PROC_FRAME_REG (proc_desc))
1015           + PROC_FRAME_OFFSET (proc_desc);
1016
1017       if (proc_desc == &temp_proc_desc)
1018         {
1019           char *name;
1020
1021           /* Do not set the saved registers for a sigtramp frame,
1022              alpha_find_saved_registers will do that for us.
1023              We can't use frame->signal_handler_caller, it is not yet set.  */
1024           find_pc_partial_function (frame->pc, &name,
1025                                     (CORE_ADDR *) NULL, (CORE_ADDR *) NULL);
1026           if (!PC_IN_SIGTRAMP (frame->pc, name))
1027             {
1028               frame->saved_regs = (CORE_ADDR *)
1029                 frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS);
1030               memcpy (frame->saved_regs, temp_saved_regs,
1031                       SIZEOF_FRAME_SAVED_REGS);
1032               frame->saved_regs[PC_REGNUM]
1033                 = frame->saved_regs[ALPHA_RA_REGNUM];
1034             }
1035         }
1036     }
1037 }
1038
1039 static CORE_ADDR
1040 alpha_frame_locals_address (struct frame_info *fi)
1041 {
1042   return (fi->frame - fi->extra_info->localoff);
1043 }
1044
1045 static CORE_ADDR
1046 alpha_frame_args_address (struct frame_info *fi)
1047 {
1048   return (fi->frame - (ALPHA_NUM_ARG_REGS * 8));
1049 }
1050
1051 /* ALPHA stack frames are almost impenetrable.  When execution stops,
1052    we basically have to look at symbol information for the function
1053    that we stopped in, which tells us *which* register (if any) is
1054    the base of the frame pointer, and what offset from that register
1055    the frame itself is at.  
1056
1057    This presents a problem when trying to examine a stack in memory
1058    (that isn't executing at the moment), using the "frame" command.  We
1059    don't have a PC, nor do we have any registers except SP.
1060
1061    This routine takes two arguments, SP and PC, and tries to make the
1062    cached frames look as if these two arguments defined a frame on the
1063    cache.  This allows the rest of info frame to extract the important
1064    arguments without difficulty.  */
1065
1066 struct frame_info *
1067 alpha_setup_arbitrary_frame (int argc, CORE_ADDR *argv)
1068 {
1069   if (argc != 2)
1070     error ("ALPHA frame specifications require two arguments: sp and pc");
1071
1072   return create_new_frame (argv[0], argv[1]);
1073 }
1074
1075 /* The alpha passes the first six arguments in the registers, the rest on
1076    the stack. The register arguments are eventually transferred to the
1077    argument transfer area immediately below the stack by the called function
1078    anyway. So we `push' at least six arguments on the stack, `reload' the
1079    argument registers and then adjust the stack pointer to point past the
1080    sixth argument. This algorithm simplifies the passing of a large struct
1081    which extends from the registers to the stack.
1082    If the called function is returning a structure, the address of the
1083    structure to be returned is passed as a hidden first argument.  */
1084
1085 static CORE_ADDR
1086 alpha_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
1087                       int struct_return, CORE_ADDR struct_addr)
1088 {
1089   int i;
1090   int accumulate_size = struct_return ? 8 : 0;
1091   int arg_regs_size = ALPHA_NUM_ARG_REGS * 8;
1092   struct alpha_arg
1093     {
1094       char *contents;
1095       int len;
1096       int offset;
1097     };
1098   struct alpha_arg *alpha_args =
1099   (struct alpha_arg *) alloca (nargs * sizeof (struct alpha_arg));
1100   register struct alpha_arg *m_arg;
1101   char raw_buffer[sizeof (CORE_ADDR)];
1102   int required_arg_regs;
1103
1104   for (i = 0, m_arg = alpha_args; i < nargs; i++, m_arg++)
1105     {
1106       struct value *arg = args[i];
1107       struct type *arg_type = check_typedef (VALUE_TYPE (arg));
1108       /* Cast argument to long if necessary as the compiler does it too.  */
1109       switch (TYPE_CODE (arg_type))
1110         {
1111         case TYPE_CODE_INT:
1112         case TYPE_CODE_BOOL:
1113         case TYPE_CODE_CHAR:
1114         case TYPE_CODE_RANGE:
1115         case TYPE_CODE_ENUM:
1116           if (TYPE_LENGTH (arg_type) < TYPE_LENGTH (builtin_type_long))
1117             {
1118               arg_type = builtin_type_long;
1119               arg = value_cast (arg_type, arg);
1120             }
1121           break;
1122         default:
1123           break;
1124         }
1125       m_arg->len = TYPE_LENGTH (arg_type);
1126       m_arg->offset = accumulate_size;
1127       accumulate_size = (accumulate_size + m_arg->len + 7) & ~7;
1128       m_arg->contents = VALUE_CONTENTS (arg);
1129     }
1130
1131   /* Determine required argument register loads, loading an argument register
1132      is expensive as it uses three ptrace calls.  */
1133   required_arg_regs = accumulate_size / 8;
1134   if (required_arg_regs > ALPHA_NUM_ARG_REGS)
1135     required_arg_regs = ALPHA_NUM_ARG_REGS;
1136
1137   /* Make room for the arguments on the stack.  */
1138   if (accumulate_size < arg_regs_size)
1139     accumulate_size = arg_regs_size;
1140   sp -= accumulate_size;
1141
1142   /* Keep sp aligned to a multiple of 16 as the compiler does it too.  */
1143   sp &= ~15;
1144
1145   /* `Push' arguments on the stack.  */
1146   for (i = nargs; m_arg--, --i >= 0;)
1147     write_memory (sp + m_arg->offset, m_arg->contents, m_arg->len);
1148   if (struct_return)
1149     {
1150       store_address (raw_buffer, sizeof (CORE_ADDR), struct_addr);
1151       write_memory (sp, raw_buffer, sizeof (CORE_ADDR));
1152     }
1153
1154   /* Load the argument registers.  */
1155   for (i = 0; i < required_arg_regs; i++)
1156     {
1157       LONGEST val;
1158
1159       val = read_memory_integer (sp + i * 8, 8);
1160       write_register (ALPHA_A0_REGNUM + i, val);
1161       write_register (ALPHA_FPA0_REGNUM + i, val);
1162     }
1163
1164   return sp + arg_regs_size;
1165 }
1166
1167 static void
1168 alpha_push_dummy_frame (void)
1169 {
1170   int ireg;
1171   struct linked_proc_info *link;
1172   alpha_extra_func_info_t proc_desc;
1173   CORE_ADDR sp = read_register (SP_REGNUM);
1174   CORE_ADDR save_address;
1175   char raw_buffer[ALPHA_MAX_REGISTER_RAW_SIZE];
1176   unsigned long mask;
1177
1178   link = (struct linked_proc_info *) xmalloc (sizeof (struct linked_proc_info));
1179   link->next = linked_proc_desc_table;
1180   linked_proc_desc_table = link;
1181
1182   proc_desc = &link->info;
1183
1184   /*
1185    * The registers we must save are all those not preserved across
1186    * procedure calls.
1187    * In addition, we must save the PC and RA.
1188    *
1189    * Dummy frame layout:
1190    *  (high memory)
1191    *    Saved PC
1192    *    Saved F30
1193    *    ...
1194    *    Saved F0
1195    *    Saved R29
1196    *    ...
1197    *    Saved R0
1198    *    Saved R26 (RA)
1199    *    Parameter build area
1200    *  (low memory)
1201    */
1202
1203 /* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<31. */
1204 #define MASK(i,j) ((((LONGEST)1 << ((j)+1)) - 1) ^ (((LONGEST)1 << (i)) - 1))
1205 #define GEN_REG_SAVE_MASK (MASK(0,8) | MASK(16,29))
1206 #define GEN_REG_SAVE_COUNT 24
1207 #define FLOAT_REG_SAVE_MASK (MASK(0,1) | MASK(10,30))
1208 #define FLOAT_REG_SAVE_COUNT 23
1209   /* The special register is the PC as we have no bit for it in the save masks.
1210      alpha_frame_saved_pc knows where the pc is saved in a dummy frame.  */
1211 #define SPECIAL_REG_SAVE_COUNT 1
1212
1213   PROC_REG_MASK (proc_desc) = GEN_REG_SAVE_MASK;
1214   PROC_FREG_MASK (proc_desc) = FLOAT_REG_SAVE_MASK;
1215   /* PROC_REG_OFFSET is the offset from the dummy frame to the saved RA,
1216      but keep SP aligned to a multiple of 16.  */
1217   PROC_REG_OFFSET (proc_desc) =
1218     -((8 * (SPECIAL_REG_SAVE_COUNT
1219             + GEN_REG_SAVE_COUNT
1220             + FLOAT_REG_SAVE_COUNT)
1221        + 15) & ~15);
1222   PROC_FREG_OFFSET (proc_desc) =
1223     PROC_REG_OFFSET (proc_desc) + 8 * GEN_REG_SAVE_COUNT;
1224
1225   /* Save general registers.
1226      The return address register is the first saved register, all other
1227      registers follow in ascending order.
1228      The PC is saved immediately below the SP.  */
1229   save_address = sp + PROC_REG_OFFSET (proc_desc);
1230   store_address (raw_buffer, 8, read_register (ALPHA_RA_REGNUM));
1231   write_memory (save_address, raw_buffer, 8);
1232   save_address += 8;
1233   mask = PROC_REG_MASK (proc_desc) & 0xffffffffL;
1234   for (ireg = 0; mask; ireg++, mask >>= 1)
1235     if (mask & 1)
1236       {
1237         if (ireg == ALPHA_RA_REGNUM)
1238           continue;
1239         store_address (raw_buffer, 8, read_register (ireg));
1240         write_memory (save_address, raw_buffer, 8);
1241         save_address += 8;
1242       }
1243
1244   store_address (raw_buffer, 8, read_register (PC_REGNUM));
1245   write_memory (sp - 8, raw_buffer, 8);
1246
1247   /* Save floating point registers.  */
1248   save_address = sp + PROC_FREG_OFFSET (proc_desc);
1249   mask = PROC_FREG_MASK (proc_desc) & 0xffffffffL;
1250   for (ireg = 0; mask; ireg++, mask >>= 1)
1251     if (mask & 1)
1252       {
1253         store_address (raw_buffer, 8, read_register (ireg + FP0_REGNUM));
1254         write_memory (save_address, raw_buffer, 8);
1255         save_address += 8;
1256       }
1257
1258   /* Set and save the frame address for the dummy.  
1259      This is tricky. The only registers that are suitable for a frame save
1260      are those that are preserved across procedure calls (s0-s6). But if
1261      a read system call is interrupted and then a dummy call is made
1262      (see testsuite/gdb.t17/interrupt.exp) the dummy call hangs till the read
1263      is satisfied. Then it returns with the s0-s6 registers set to the values
1264      on entry to the read system call and our dummy frame pointer would be
1265      destroyed. So we save the dummy frame in the proc_desc and handle the
1266      retrieval of the frame pointer of a dummy specifically. The frame register
1267      is set to the virtual frame (pseudo) register, it's value will always
1268      be read as zero and will help us to catch any errors in the dummy frame
1269      retrieval code.  */
1270   PROC_DUMMY_FRAME (proc_desc) = sp;
1271   PROC_FRAME_REG (proc_desc) = FP_REGNUM;
1272   PROC_FRAME_OFFSET (proc_desc) = 0;
1273   sp += PROC_REG_OFFSET (proc_desc);
1274   write_register (SP_REGNUM, sp);
1275
1276   PROC_LOW_ADDR (proc_desc) = CALL_DUMMY_ADDRESS ();
1277   PROC_HIGH_ADDR (proc_desc) = PROC_LOW_ADDR (proc_desc) + 4;
1278
1279   SET_PROC_DESC_IS_DUMMY (proc_desc);
1280   PROC_PC_REG (proc_desc) = ALPHA_RA_REGNUM;
1281 }
1282
1283 static void
1284 alpha_pop_frame (void)
1285 {
1286   register int regnum;
1287   struct frame_info *frame = get_current_frame ();
1288   CORE_ADDR new_sp = frame->frame;
1289
1290   alpha_extra_func_info_t proc_desc = frame->extra_info->proc_desc;
1291
1292   /* we need proc_desc to know how to restore the registers;
1293      if it is NULL, construct (a temporary) one */
1294   if (proc_desc == NULL)
1295     proc_desc = find_proc_desc (frame->pc, frame->next);
1296
1297   /* Question: should we copy this proc_desc and save it in
1298      frame->proc_desc?  If we do, who will free it?
1299      For now, we don't save a copy... */
1300
1301   write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
1302   if (frame->saved_regs == NULL)
1303     alpha_find_saved_regs (frame);
1304   if (proc_desc)
1305     {
1306       for (regnum = 32; --regnum >= 0;)
1307         if (PROC_REG_MASK (proc_desc) & (1 << regnum))
1308           write_register (regnum,
1309                           read_memory_integer (frame->saved_regs[regnum],
1310                                                8));
1311       for (regnum = 32; --regnum >= 0;)
1312         if (PROC_FREG_MASK (proc_desc) & (1 << regnum))
1313           write_register (regnum + FP0_REGNUM,
1314            read_memory_integer (frame->saved_regs[regnum + FP0_REGNUM], 8));
1315     }
1316   write_register (SP_REGNUM, new_sp);
1317   flush_cached_frames ();
1318
1319   if (proc_desc && (PROC_DESC_IS_DUMMY (proc_desc)
1320                     || alpha_proc_desc_is_dyn_sigtramp (proc_desc)))
1321     {
1322       struct linked_proc_info *pi_ptr, *prev_ptr;
1323
1324       for (pi_ptr = linked_proc_desc_table, prev_ptr = NULL;
1325            pi_ptr != NULL;
1326            prev_ptr = pi_ptr, pi_ptr = pi_ptr->next)
1327         {
1328           if (&pi_ptr->info == proc_desc)
1329             break;
1330         }
1331
1332       if (pi_ptr == NULL)
1333         error ("Can't locate dummy extra frame info\n");
1334
1335       if (prev_ptr != NULL)
1336         prev_ptr->next = pi_ptr->next;
1337       else
1338         linked_proc_desc_table = pi_ptr->next;
1339
1340       xfree (pi_ptr);
1341     }
1342 }
1343 \f
1344 /* To skip prologues, I use this predicate.  Returns either PC itself
1345    if the code at PC does not look like a function prologue; otherwise
1346    returns an address that (if we're lucky) follows the prologue.  If
1347    LENIENT, then we must skip everything which is involved in setting
1348    up the frame (it's OK to skip more, just so long as we don't skip
1349    anything which might clobber the registers which are being saved.
1350    Currently we must not skip more on the alpha, but we might need the
1351    lenient stuff some day.  */
1352
1353 static CORE_ADDR
1354 alpha_skip_prologue_internal (CORE_ADDR pc, int lenient)
1355 {
1356   unsigned long inst;
1357   int offset;
1358   CORE_ADDR post_prologue_pc;
1359   char buf[4];
1360
1361 #ifdef GDB_TARGET_HAS_SHARED_LIBS
1362   /* Silently return the unaltered pc upon memory errors.
1363      This could happen on OSF/1 if decode_line_1 tries to skip the
1364      prologue for quickstarted shared library functions when the
1365      shared library is not yet mapped in.
1366      Reading target memory is slow over serial lines, so we perform
1367      this check only if the target has shared libraries.  */
1368   if (target_read_memory (pc, buf, 4))
1369     return pc;
1370 #endif
1371
1372   /* See if we can determine the end of the prologue via the symbol table.
1373      If so, then return either PC, or the PC after the prologue, whichever
1374      is greater.  */
1375
1376   post_prologue_pc = after_prologue (pc, NULL);
1377
1378   if (post_prologue_pc != 0)
1379     return max (pc, post_prologue_pc);
1380
1381   /* Can't determine prologue from the symbol table, need to examine
1382      instructions.  */
1383
1384   /* Skip the typical prologue instructions. These are the stack adjustment
1385      instruction and the instructions that save registers on the stack
1386      or in the gcc frame.  */
1387   for (offset = 0; offset < 100; offset += 4)
1388     {
1389       int status;
1390
1391       status = read_memory_nobpt (pc + offset, buf, 4);
1392       if (status)
1393         memory_error (status, pc + offset);
1394       inst = extract_unsigned_integer (buf, 4);
1395
1396       /* The alpha has no delay slots. But let's keep the lenient stuff,
1397          we might need it for something else in the future.  */
1398       if (lenient && 0)
1399         continue;
1400
1401       if ((inst & 0xffff0000) == 0x27bb0000)    /* ldah $gp,n($t12) */
1402         continue;
1403       if ((inst & 0xffff0000) == 0x23bd0000)    /* lda $gp,n($gp) */
1404         continue;
1405       if ((inst & 0xffff0000) == 0x23de0000)    /* lda $sp,n($sp) */
1406         continue;
1407       if ((inst & 0xffe01fff) == 0x43c0153e)    /* subq $sp,n,$sp */
1408         continue;
1409
1410       if ((inst & 0xfc1f0000) == 0xb41e0000
1411           && (inst & 0xffff0000) != 0xb7fe0000)
1412         continue;               /* stq reg,n($sp) */
1413       /* reg != $zero */
1414       if ((inst & 0xfc1f0000) == 0x9c1e0000
1415           && (inst & 0xffff0000) != 0x9ffe0000)
1416         continue;               /* stt reg,n($sp) */
1417       /* reg != $zero */
1418       if (inst == 0x47de040f)   /* bis sp,sp,fp */
1419         continue;
1420
1421       break;
1422     }
1423   return pc + offset;
1424 }
1425
1426 static CORE_ADDR
1427 alpha_skip_prologue (CORE_ADDR addr)
1428 {
1429   return (alpha_skip_prologue_internal (addr, 0));
1430 }
1431
1432 #if 0
1433 /* Is address PC in the prologue (loosely defined) for function at
1434    STARTADDR?  */
1435
1436 static int
1437 alpha_in_lenient_prologue (CORE_ADDR startaddr, CORE_ADDR pc)
1438 {
1439   CORE_ADDR end_prologue = alpha_skip_prologue_internal (startaddr, 1);
1440   return pc >= startaddr && pc < end_prologue;
1441 }
1442 #endif
1443
1444 /* The alpha needs a conversion between register and memory format if
1445    the register is a floating point register and
1446    memory format is float, as the register format must be double
1447    or
1448    memory format is an integer with 4 bytes or less, as the representation
1449    of integers in floating point registers is different. */
1450 static void
1451 alpha_register_convert_to_virtual (int regnum, struct type *valtype,
1452                                    char *raw_buffer, char *virtual_buffer)
1453 {
1454   if (TYPE_LENGTH (valtype) >= REGISTER_RAW_SIZE (regnum))
1455     {
1456       memcpy (virtual_buffer, raw_buffer, REGISTER_VIRTUAL_SIZE (regnum));
1457       return;
1458     }
1459
1460   if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1461     {
1462       double d = extract_floating (raw_buffer, REGISTER_RAW_SIZE (regnum));
1463       store_floating (virtual_buffer, TYPE_LENGTH (valtype), d);
1464     }
1465   else if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 4)
1466     {
1467       ULONGEST l;
1468       l = extract_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (regnum));
1469       l = ((l >> 32) & 0xc0000000) | ((l >> 29) & 0x3fffffff);
1470       store_unsigned_integer (virtual_buffer, TYPE_LENGTH (valtype), l);
1471     }
1472   else
1473     error ("Cannot retrieve value from floating point register");
1474 }
1475
1476 static void
1477 alpha_register_convert_to_raw (struct type *valtype, int regnum,
1478                                char *virtual_buffer, char *raw_buffer)
1479 {
1480   if (TYPE_LENGTH (valtype) >= REGISTER_RAW_SIZE (regnum))
1481     {
1482       memcpy (raw_buffer, virtual_buffer, REGISTER_RAW_SIZE (regnum));
1483       return;
1484     }
1485
1486   if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1487     {
1488       double d = extract_floating (virtual_buffer, TYPE_LENGTH (valtype));
1489       store_floating (raw_buffer, REGISTER_RAW_SIZE (regnum), d);
1490     }
1491   else if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 4)
1492     {
1493       ULONGEST l;
1494       if (TYPE_UNSIGNED (valtype))
1495         l = extract_unsigned_integer (virtual_buffer, TYPE_LENGTH (valtype));
1496       else
1497         l = extract_signed_integer (virtual_buffer, TYPE_LENGTH (valtype));
1498       l = ((l & 0xc0000000) << 32) | ((l & 0x3fffffff) << 29);
1499       store_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (regnum), l);
1500     }
1501   else
1502     error ("Cannot store value in floating point register");
1503 }
1504
1505 /* Given a return value in `regbuf' with a type `valtype', 
1506    extract and copy its value into `valbuf'.  */
1507
1508 static void
1509 alpha_extract_return_value (struct type *valtype,
1510                             char regbuf[REGISTER_BYTES], char *valbuf)
1511 {
1512   if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1513     alpha_register_convert_to_virtual (FP0_REGNUM, valtype,
1514                                        regbuf + REGISTER_BYTE (FP0_REGNUM),
1515                                        valbuf);
1516   else
1517     memcpy (valbuf, regbuf + REGISTER_BYTE (ALPHA_V0_REGNUM),
1518             TYPE_LENGTH (valtype));
1519 }
1520
1521 /* Given a return value in `regbuf' with a type `valtype', 
1522    write its value into the appropriate register.  */
1523
1524 static void
1525 alpha_store_return_value (struct type *valtype, char *valbuf)
1526 {
1527   char raw_buffer[ALPHA_MAX_REGISTER_RAW_SIZE];
1528   int regnum = ALPHA_V0_REGNUM;
1529   int length = TYPE_LENGTH (valtype);
1530
1531   if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1532     {
1533       regnum = FP0_REGNUM;
1534       length = REGISTER_RAW_SIZE (regnum);
1535       alpha_register_convert_to_raw (valtype, regnum, valbuf, raw_buffer);
1536     }
1537   else
1538     memcpy (raw_buffer, valbuf, length);
1539
1540   write_register_bytes (REGISTER_BYTE (regnum), raw_buffer, length);
1541 }
1542
1543 /* Just like reinit_frame_cache, but with the right arguments to be
1544    callable as an sfunc.  */
1545
1546 static void
1547 reinit_frame_cache_sfunc (char *args, int from_tty, struct cmd_list_element *c)
1548 {
1549   reinit_frame_cache ();
1550 }
1551
1552 /* This is the definition of CALL_DUMMY_ADDRESS.  It's a heuristic that is used
1553    to find a convenient place in the text segment to stick a breakpoint to
1554    detect the completion of a target function call (ala call_function_by_hand).
1555  */
1556
1557 CORE_ADDR
1558 alpha_call_dummy_address (void)
1559 {
1560   CORE_ADDR entry;
1561   struct minimal_symbol *sym;
1562
1563   entry = entry_point_address ();
1564
1565   if (entry != 0)
1566     return entry;
1567
1568   sym = lookup_minimal_symbol ("_Prelude", NULL, symfile_objfile);
1569
1570   if (!sym || MSYMBOL_TYPE (sym) != mst_text)
1571     return 0;
1572   else
1573     return SYMBOL_VALUE_ADDRESS (sym) + 4;
1574 }
1575
1576 static void
1577 alpha_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
1578                       struct value **args, struct type *type, int gcc_p)
1579 {
1580   CORE_ADDR bp_address = CALL_DUMMY_ADDRESS ();
1581
1582   if (bp_address == 0)
1583     error ("no place to put call");
1584   write_register (ALPHA_RA_REGNUM, bp_address);
1585   write_register (ALPHA_T12_REGNUM, fun);
1586 }
1587
1588 /* On the Alpha, the call dummy code is nevery copied to user space
1589    (see alpha_fix_call_dummy() above).  The contents of this do not
1590    matter.  */
1591 LONGEST alpha_call_dummy_words[] = { 0 };
1592
1593 static int
1594 alpha_use_struct_convention (int gcc_p, struct type *type)
1595 {
1596   /* Structures are returned by ref in extra arg0.  */
1597   return 1;
1598 }
1599
1600 static void
1601 alpha_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
1602 {
1603   /* Store the address of the place in which to copy the structure the
1604      subroutine will return.  Handled by alpha_push_arguments.  */
1605 }
1606
1607 static CORE_ADDR
1608 alpha_extract_struct_value_address (char *regbuf)
1609 {
1610   return (extract_address (regbuf + REGISTER_BYTE (ALPHA_V0_REGNUM),
1611                            REGISTER_RAW_SIZE (ALPHA_V0_REGNUM)));
1612 }
1613
1614 /* alpha_software_single_step() is called just before we want to resume
1615    the inferior, if we want to single-step it but there is no hardware
1616    or kernel single-step support (NetBSD on Alpha, for example).  We find
1617    the target of the coming instruction and breakpoint it.
1618
1619    single_step is also called just after the inferior stops.  If we had
1620    set up a simulated single-step, we undo our damage.  */
1621
1622 static CORE_ADDR
1623 alpha_next_pc (CORE_ADDR pc)
1624 {
1625   unsigned int insn;
1626   unsigned int op;
1627   int offset;
1628   LONGEST rav;
1629
1630   insn = read_memory_unsigned_integer (pc, sizeof (insn));
1631
1632   /* Opcode is top 6 bits. */
1633   op = (insn >> 26) & 0x3f;
1634
1635   if (op == 0x1a)
1636     {
1637       /* Jump format: target PC is:
1638          RB & ~3  */
1639       return (read_register ((insn >> 16) & 0x1f) & ~3);
1640     }
1641
1642   if ((op & 0x30) == 0x30)
1643     {
1644       /* Branch format: target PC is:
1645          (new PC) + (4 * sext(displacement))  */
1646       if (op == 0x30 ||         /* BR */
1647           op == 0x34)           /* BSR */
1648         {
1649  branch_taken:
1650           offset = (insn & 0x001fffff);
1651           if (offset & 0x00100000)
1652             offset  |= 0xffe00000;
1653           offset *= 4;
1654           return (pc + 4 + offset);
1655         }
1656
1657       /* Need to determine if branch is taken; read RA.  */
1658       rav = (LONGEST) read_register ((insn >> 21) & 0x1f);
1659       switch (op)
1660         {
1661         case 0x38:              /* BLBC */
1662           if ((rav & 1) == 0)
1663             goto branch_taken;
1664           break;
1665         case 0x3c:              /* BLBS */
1666           if (rav & 1)
1667             goto branch_taken;
1668           break;
1669         case 0x39:              /* BEQ */
1670           if (rav == 0)
1671             goto branch_taken;
1672           break;
1673         case 0x3d:              /* BNE */
1674           if (rav != 0)
1675             goto branch_taken;
1676           break;
1677         case 0x3a:              /* BLT */
1678           if (rav < 0)
1679             goto branch_taken;
1680           break;
1681         case 0x3b:              /* BLE */
1682           if (rav <= 0)
1683             goto branch_taken;
1684           break;
1685         case 0x3f:              /* BGT */
1686           if (rav > 0)
1687             goto branch_taken;
1688           break;
1689         case 0x3e:              /* BGE */
1690           if (rav >= 0)
1691             goto branch_taken;
1692           break;
1693         }
1694     }
1695
1696   /* Not a branch or branch not taken; target PC is:
1697      pc + 4  */
1698   return (pc + 4);
1699 }
1700
1701 void
1702 alpha_software_single_step (enum target_signal sig, int insert_breakpoints_p)
1703 {
1704   static CORE_ADDR next_pc;
1705   typedef char binsn_quantum[BREAKPOINT_MAX];
1706   static binsn_quantum break_mem;
1707   CORE_ADDR pc;
1708
1709   if (insert_breakpoints_p)
1710     {
1711       pc = read_pc ();
1712       next_pc = alpha_next_pc (pc);
1713
1714       target_insert_breakpoint (next_pc, break_mem);
1715     }
1716   else
1717     {
1718       target_remove_breakpoint (next_pc, break_mem);
1719       write_pc (next_pc);
1720     }
1721 }
1722
1723 \f
1724 /* This table matches the indices assigned to enum alpha_abi.  Keep
1725    them in sync.  */
1726 static const char * const alpha_abi_names[] =
1727 {
1728   "<unknown>",
1729   "OSF/1",
1730   "GNU/Linux",
1731   "FreeBSD",
1732   "NetBSD",
1733   NULL
1734 };
1735
1736 static void
1737 process_note_abi_tag_sections (bfd *abfd, asection *sect, void *obj)
1738 {
1739   enum alpha_abi *os_ident_ptr = obj;
1740   const char *name;
1741   unsigned int sectsize;
1742
1743   name = bfd_get_section_name (abfd, sect);
1744   sectsize = bfd_section_size (abfd, sect);
1745
1746   if (strcmp (name, ".note.ABI-tag") == 0 && sectsize > 0)
1747     {
1748       unsigned int name_length, data_length, note_type;
1749       char *note;
1750
1751       /* If the section is larger than this, it's probably not what we are
1752          looking for.  */
1753       if (sectsize > 128)
1754         sectsize = 128;
1755
1756       note = alloca (sectsize);
1757
1758       bfd_get_section_contents (abfd, sect, note,
1759                                 (file_ptr) 0, (bfd_size_type) sectsize);
1760
1761       name_length = bfd_h_get_32 (abfd, note);
1762       data_length = bfd_h_get_32 (abfd, note + 4);
1763       note_type   = bfd_h_get_32 (abfd, note + 8);
1764
1765       if (name_length == 4 && data_length == 16 && note_type == 1
1766           && strcmp (note + 12, "GNU") == 0)
1767         {
1768           int os_number = bfd_h_get_32 (abfd, note + 16);
1769
1770           /* The case numbers are from abi-tags in glibc.  */
1771           switch (os_number)
1772             {
1773             case 0 :
1774               *os_ident_ptr = ALPHA_ABI_LINUX;
1775               break;
1776
1777             case 1 :
1778               internal_error
1779                 (__FILE__, __LINE__,
1780                  "process_note_abi_sections: Hurd objects not supported");
1781               break;
1782
1783             case 2 :
1784               internal_error
1785                 (__FILE__, __LINE__,
1786                  "process_note_abi_sections: Solaris objects not supported");
1787               break;
1788
1789             default :
1790               internal_error
1791                 (__FILE__, __LINE__,
1792                  "process_note_abi_sections: unknown OS number %d",
1793                  os_number);
1794               break;
1795             }
1796         }
1797     }
1798   /* NetBSD uses a similar trick.  */
1799   else if (strcmp (name, ".note.netbsd.ident") == 0 && sectsize > 0)
1800     {
1801       unsigned int name_length, desc_length, note_type;
1802       char *note;
1803
1804       /* If the section is larger than this, it's probably not what we are
1805          looking for.  */
1806       if (sectsize > 128)
1807         sectsize = 128;
1808
1809       note = alloca (sectsize);
1810
1811       bfd_get_section_contents (abfd, sect, note,
1812                                  (file_ptr) 0, (bfd_size_type) sectsize);
1813       
1814       name_length = bfd_h_get_32 (abfd, note);
1815       desc_length = bfd_h_get_32 (abfd, note + 4);
1816       note_type   = bfd_h_get_32 (abfd, note + 8);
1817
1818       if (name_length == 7 && desc_length == 4 && note_type == 1
1819           && strcmp (note + 12, "NetBSD") == 0)
1820         /* XXX Should we check the version here?
1821            Probably not necessary yet.  */
1822         *os_ident_ptr = ALPHA_ABI_NETBSD;
1823     }
1824 }
1825
1826 static int
1827 get_elfosabi (bfd *abfd)
1828 {
1829   int elfosabi;
1830   enum alpha_abi alpha_abi = ALPHA_ABI_UNKNOWN;
1831
1832   elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
1833
1834   /* When elfosabi is 0 (ELFOSABI_NONE), this is supposed to indicate
1835      what we're on a SYSV system.  However, GNU/Linux uses a note section
1836      to record OS/ABI info, but leaves e_ident[EI_OSABI] zero.  So we
1837      have to check the note sections too.  */
1838   if (elfosabi == 0)
1839     {
1840       bfd_map_over_sections (abfd,
1841                              process_note_abi_tag_sections,
1842                              &alpha_abi);
1843     }
1844
1845   if (alpha_abi != ALPHA_ABI_UNKNOWN)
1846     return alpha_abi;
1847
1848   switch (elfosabi)
1849     {
1850     case ELFOSABI_NONE:
1851       /* Leave it as unknown.  */
1852       break;
1853
1854     case ELFOSABI_NETBSD:
1855       return ALPHA_ABI_NETBSD;
1856
1857     case ELFOSABI_FREEBSD:
1858       return ALPHA_ABI_FREEBSD;
1859
1860     case ELFOSABI_LINUX:
1861       return ALPHA_ABI_LINUX;
1862     }
1863
1864   return ALPHA_ABI_UNKNOWN;
1865 }
1866
1867 struct alpha_abi_handler
1868 {
1869   struct alpha_abi_handler *next;
1870   enum alpha_abi abi;
1871   void (*init_abi)(struct gdbarch_info, struct gdbarch *);
1872 };
1873
1874 struct alpha_abi_handler *alpha_abi_handler_list = NULL;
1875
1876 void
1877 alpha_gdbarch_register_os_abi (enum alpha_abi abi,
1878                                void (*init_abi)(struct gdbarch_info,
1879                                                 struct gdbarch *))
1880 {
1881   struct alpha_abi_handler **handler_p;
1882
1883   for (handler_p = &alpha_abi_handler_list; *handler_p != NULL;
1884        handler_p = &(*handler_p)->next)
1885     {
1886       if ((*handler_p)->abi == abi)
1887         {
1888           internal_error
1889             (__FILE__, __LINE__,
1890              "alpha_gdbarch_register_os_abi: A handler for this ABI variant "
1891              "(%d) has already been registered", (int) abi);
1892           /* If user wants to continue, override previous definition.  */
1893           (*handler_p)->init_abi = init_abi;
1894           return;
1895         }
1896     }
1897
1898   (*handler_p)
1899     = (struct alpha_abi_handler *) xmalloc (sizeof (struct alpha_abi_handler));
1900   (*handler_p)->next = NULL;
1901   (*handler_p)->abi = abi;
1902   (*handler_p)->init_abi = init_abi;
1903 }
1904
1905 /* Initialize the current architecture based on INFO.  If possible, re-use an
1906    architecture from ARCHES, which is a list of architectures already created
1907    during this debugging session.
1908
1909    Called e.g. at program startup, when reading a core file, and when reading
1910    a binary file.  */
1911
1912 static struct gdbarch *
1913 alpha_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1914 {
1915   struct gdbarch_tdep *tdep;
1916   struct gdbarch *gdbarch;
1917   enum alpha_abi alpha_abi = ALPHA_ABI_UNKNOWN;
1918   struct alpha_abi_handler *abi_handler;
1919
1920   /* Try to determine the ABI of the object we are loading.  */
1921
1922   if (info.abfd != NULL)
1923     {
1924       switch (bfd_get_flavour (info.abfd))
1925         {
1926         case bfd_target_elf_flavour:
1927           alpha_abi = get_elfosabi (info.abfd);
1928           break;
1929
1930         case bfd_target_ecoff_flavour:
1931           /* Assume it's OSF/1.  */
1932           alpha_abi = ALPHA_ABI_OSF1;
1933           break;
1934
1935         default:
1936           /* Not sure what to do here, leave the ABI as unknown.  */
1937           break;
1938         }
1939     }
1940
1941   /* Find a candidate among extant architectures.  */
1942   for (arches = gdbarch_list_lookup_by_info (arches, &info);
1943        arches != NULL;
1944        arches = gdbarch_list_lookup_by_info (arches->next, &info))
1945     {
1946       /* Make sure the ABI selection matches.  */
1947       tdep = gdbarch_tdep (arches->gdbarch);
1948       if (tdep && tdep->alpha_abi == alpha_abi)
1949         return arches->gdbarch;
1950     }
1951
1952   tdep = xmalloc (sizeof (struct gdbarch_tdep));
1953   gdbarch = gdbarch_alloc (&info, tdep);
1954
1955   tdep->alpha_abi = alpha_abi;
1956   if (alpha_abi < ALPHA_ABI_INVALID)
1957     tdep->abi_name = alpha_abi_names[alpha_abi];
1958   else
1959     {
1960       internal_error (__FILE__, __LINE__, "Invalid setting of alpha_abi %d",
1961                       (int) alpha_abi);
1962       tdep->abi_name = "<invalid>";
1963     }
1964
1965   /* Lowest text address.  This is used by heuristic_proc_start() to
1966      decide when to stop looking.  */
1967   tdep->vm_min_address = (CORE_ADDR) 0x120000000;
1968
1969   tdep->dynamic_sigtramp_offset = NULL;
1970   tdep->skip_sigtramp_frame = NULL;
1971
1972   /* Type sizes */
1973   set_gdbarch_short_bit (gdbarch, 16);
1974   set_gdbarch_int_bit (gdbarch, 32);
1975   set_gdbarch_long_bit (gdbarch, 64);
1976   set_gdbarch_long_long_bit (gdbarch, 64);
1977   set_gdbarch_float_bit (gdbarch, 32);
1978   set_gdbarch_double_bit (gdbarch, 64);
1979   set_gdbarch_long_double_bit (gdbarch, 64);
1980   set_gdbarch_ptr_bit (gdbarch, 64);
1981
1982   /* Register info */
1983   set_gdbarch_num_regs (gdbarch, ALPHA_NUM_REGS);
1984   set_gdbarch_sp_regnum (gdbarch, ALPHA_SP_REGNUM);
1985   set_gdbarch_fp_regnum (gdbarch, ALPHA_FP_REGNUM);
1986   set_gdbarch_pc_regnum (gdbarch, ALPHA_PC_REGNUM);
1987   set_gdbarch_fp0_regnum (gdbarch, ALPHA_FP0_REGNUM);
1988
1989   set_gdbarch_register_name (gdbarch, alpha_register_name);
1990   set_gdbarch_register_size (gdbarch, ALPHA_REGISTER_SIZE);
1991   set_gdbarch_register_bytes (gdbarch, ALPHA_REGISTER_BYTES);
1992   set_gdbarch_register_byte (gdbarch, alpha_register_byte);
1993   set_gdbarch_register_raw_size (gdbarch, alpha_register_raw_size);
1994   set_gdbarch_max_register_raw_size (gdbarch, ALPHA_MAX_REGISTER_RAW_SIZE);
1995   set_gdbarch_register_virtual_size (gdbarch, alpha_register_virtual_size);
1996   set_gdbarch_max_register_virtual_size (gdbarch,
1997                                          ALPHA_MAX_REGISTER_VIRTUAL_SIZE);
1998   set_gdbarch_register_virtual_type (gdbarch, alpha_register_virtual_type);
1999
2000   set_gdbarch_cannot_fetch_register (gdbarch, alpha_cannot_fetch_register);
2001   set_gdbarch_cannot_store_register (gdbarch, alpha_cannot_store_register);
2002
2003   set_gdbarch_register_convertible (gdbarch, alpha_register_convertible);
2004   set_gdbarch_register_convert_to_virtual (gdbarch,
2005                                            alpha_register_convert_to_virtual);
2006   set_gdbarch_register_convert_to_raw (gdbarch, alpha_register_convert_to_raw);
2007
2008   set_gdbarch_skip_prologue (gdbarch, alpha_skip_prologue);
2009
2010   set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
2011   set_gdbarch_frameless_function_invocation (gdbarch,
2012                                     generic_frameless_function_invocation_not);
2013
2014   set_gdbarch_saved_pc_after_call (gdbarch, alpha_saved_pc_after_call);
2015
2016   set_gdbarch_frame_chain (gdbarch, alpha_frame_chain);
2017   set_gdbarch_frame_chain_valid (gdbarch, func_frame_chain_valid);
2018   set_gdbarch_frame_saved_pc (gdbarch, alpha_frame_saved_pc);
2019
2020   set_gdbarch_frame_init_saved_regs (gdbarch, alpha_frame_init_saved_regs);
2021   set_gdbarch_get_saved_register (gdbarch, alpha_get_saved_register);
2022
2023   set_gdbarch_use_struct_convention (gdbarch, alpha_use_struct_convention);
2024   set_gdbarch_extract_return_value (gdbarch, alpha_extract_return_value);
2025
2026   set_gdbarch_store_struct_return (gdbarch, alpha_store_struct_return);
2027   set_gdbarch_store_return_value (gdbarch, alpha_store_return_value);
2028   set_gdbarch_extract_struct_value_address (gdbarch,
2029                                             alpha_extract_struct_value_address);
2030
2031   /* Settings for calling functions in the inferior.  */
2032   set_gdbarch_use_generic_dummy_frames (gdbarch, 0);
2033   set_gdbarch_call_dummy_length (gdbarch, 0);
2034   set_gdbarch_push_arguments (gdbarch, alpha_push_arguments);
2035   set_gdbarch_pop_frame (gdbarch, alpha_pop_frame);
2036
2037   /* On the Alpha, the call dummy code is never copied to user space,
2038      stopping the user call is achieved via a bp_call_dummy breakpoint.
2039      But we need a fake CALL_DUMMY definition to enable the proper
2040      call_function_by_hand and to avoid zero length array warnings.  */
2041   set_gdbarch_call_dummy_p (gdbarch, 1);
2042   set_gdbarch_call_dummy_words (gdbarch, alpha_call_dummy_words);
2043   set_gdbarch_sizeof_call_dummy_words (gdbarch, 0);
2044   set_gdbarch_frame_args_address (gdbarch, alpha_frame_args_address);
2045   set_gdbarch_frame_locals_address (gdbarch, alpha_frame_locals_address);
2046   set_gdbarch_init_extra_frame_info (gdbarch, alpha_init_extra_frame_info);
2047
2048   /* Alpha OSF/1 inhibits execution of code on the stack.  But there is
2049      no need for a dummy on the Alpha.  PUSH_ARGUMENTS takes care of all
2050      argument handling and bp_call_dummy takes care of stopping the dummy.  */
2051   set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
2052   set_gdbarch_call_dummy_address (gdbarch, alpha_call_dummy_address);
2053   set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
2054   set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
2055   set_gdbarch_call_dummy_start_offset (gdbarch, 0);
2056   set_gdbarch_pc_in_call_dummy (gdbarch, pc_in_call_dummy_at_entry_point);
2057   set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
2058   set_gdbarch_push_dummy_frame (gdbarch, alpha_push_dummy_frame);
2059   set_gdbarch_fix_call_dummy (gdbarch, alpha_fix_call_dummy);
2060   set_gdbarch_init_frame_pc (gdbarch, init_frame_pc_noop);
2061   set_gdbarch_init_frame_pc_first (gdbarch, alpha_init_frame_pc_first);
2062
2063   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2064   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
2065
2066   /* Floats are always passed as doubles.  */
2067   set_gdbarch_coerce_float_to_double (gdbarch,
2068                                       standard_coerce_float_to_double);
2069
2070   set_gdbarch_decr_pc_after_break (gdbarch, 4);
2071   set_gdbarch_frame_args_skip (gdbarch, 0);
2072
2073   /* Hook in ABI-specific overrides, if they have been registered.  */
2074   if (alpha_abi == ALPHA_ABI_UNKNOWN)
2075     {
2076       /* Don't complain about not knowing the ABI variant if we don't
2077          have an inferior.  */
2078       if (info.abfd)
2079         fprintf_filtered
2080           (gdb_stderr, "GDB doesn't recognize the ABI of the inferior.  "
2081            "Attempting to continue with the default Alpha settings");
2082     }
2083   else
2084     {
2085       for (abi_handler = alpha_abi_handler_list; abi_handler != NULL;
2086            abi_handler = abi_handler->next)
2087         if (abi_handler->abi == alpha_abi)
2088           break;
2089
2090       if (abi_handler)
2091         abi_handler->init_abi (info, gdbarch);
2092       else
2093         {
2094           /* We assume that if GDB_MULTI_ARCH is less than
2095              GDB_MULTI_ARCH_TM that an ABI variant can be supported by
2096              overriding definitions in this file.  */
2097           if (GDB_MULTI_ARCH > GDB_MULTI_ARCH_PARTIAL)
2098             fprintf_filtered
2099               (gdb_stderr,
2100                "A handler for the ABI variant \"%s\" is not built into this "
2101                "configuration of GDB.  "
2102                "Attempting to continue with the default Alpha settings",
2103                alpha_abi_names[alpha_abi]);
2104         }
2105     }
2106
2107   return gdbarch;
2108 }
2109
2110 static void
2111 alpha_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
2112 {
2113   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
2114
2115   if (tdep == NULL)
2116     return;
2117
2118   if (tdep->abi_name != NULL)
2119     fprintf_unfiltered (file, "alpha_dump_tdep: ABI = %s\n", tdep->abi_name);
2120   else
2121     internal_error (__FILE__, __LINE__,
2122                     "alpha_dump_tdep: illegal setting of tdep->alpha_abi (%d)",
2123                     (int) tdep->alpha_abi);
2124
2125   fprintf_unfiltered (file,
2126                       "alpha_dump_tdep: vm_min_address = 0x%lx\n",
2127                       (long) tdep->vm_min_address);
2128 }
2129
2130 void
2131 _initialize_alpha_tdep (void)
2132 {
2133   struct cmd_list_element *c;
2134
2135   gdbarch_register (bfd_arch_alpha, alpha_gdbarch_init, alpha_dump_tdep);
2136
2137   tm_print_insn = print_insn_alpha;
2138
2139   /* Let the user set the fence post for heuristic_proc_start.  */
2140
2141   /* We really would like to have both "0" and "unlimited" work, but
2142      command.c doesn't deal with that.  So make it a var_zinteger
2143      because the user can always use "999999" or some such for unlimited.  */
2144   c = add_set_cmd ("heuristic-fence-post", class_support, var_zinteger,
2145                    (char *) &heuristic_fence_post,
2146                    "\
2147 Set the distance searched for the start of a function.\n\
2148 If you are debugging a stripped executable, GDB needs to search through the\n\
2149 program for the start of a function.  This command sets the distance of the\n\
2150 search.  The only need to set it is when debugging a stripped executable.",
2151                    &setlist);
2152   /* We need to throw away the frame cache when we set this, since it
2153      might change our ability to get backtraces.  */
2154   set_cmd_sfunc (c, reinit_frame_cache_sfunc);
2155   add_show_from_set (c, &showlist);
2156 }