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