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