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