b08fb20a37ac304e86ff07b917f8ff7a4d9bc310
[deliverable/binutils-gdb.git] / gdb / alpha-tdep.c
1 /* Target-dependent code for the ALPHA architecture, for GDB, the GNU Debugger.
2 Copyright 1993, 1994 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "symtab.h"
24 #include "value.h"
25 #include "gdbcmd.h"
26 #include "gdbcore.h"
27 #include "dis-asm.h"
28 #include "symfile.h"
29 #include "objfiles.h"
30
31 /* FIXME: Some of this code should perhaps be merged with mips-tdep.c. */
32
33 /* FIXME: Put this declaration in frame.h. */
34 extern struct obstack frame_cache_obstack;
35 \f
36
37 /* Forward declarations. */
38
39 static CORE_ADDR read_next_frame_reg PARAMS ((struct frame_info *, int));
40
41 static CORE_ADDR heuristic_proc_start PARAMS ((CORE_ADDR));
42
43 static alpha_extra_func_info_t heuristic_proc_desc PARAMS ((CORE_ADDR,
44 CORE_ADDR,
45 struct frame_info *));
46
47 static alpha_extra_func_info_t find_proc_desc PARAMS ((CORE_ADDR,
48 struct frame_info *));
49
50 #if 0
51 static int alpha_in_lenient_prologue PARAMS ((CORE_ADDR, CORE_ADDR));
52 #endif
53
54 static void reinit_frame_cache_sfunc PARAMS ((char *, int,
55 struct cmd_list_element *));
56
57 static CORE_ADDR after_prologue PARAMS ((CORE_ADDR pc,
58 alpha_extra_func_info_t proc_desc));
59
60 static int in_prologue PARAMS ((CORE_ADDR pc,
61 alpha_extra_func_info_t proc_desc));
62
63 /* Heuristic_proc_start may hunt through the text section for a long
64 time across a 2400 baud serial line. Allows the user to limit this
65 search. */
66 static unsigned int heuristic_fence_post = 0;
67
68 /* Layout of a stack frame on the alpha:
69
70 | |
71 pdr members: | 7th ... nth arg, |
72 | `pushed' by caller. |
73 | |
74 ----------------|-------------------------------|<-- old_sp == vfp
75 ^ ^ ^ ^ | |
76 | | | | | |
77 | |localoff | Copies of 1st .. 6th |
78 | | | | | argument if necessary. |
79 | | | v | |
80 | | | --- |-------------------------------|<-- FRAME_LOCALS_ADDRESS
81 | | | | |
82 | | | | Locals and temporaries. |
83 | | | | |
84 | | | |-------------------------------|
85 | | | | |
86 |-fregoffset | Saved float registers. |
87 | | | | F9 |
88 | | | | . |
89 | | | | . |
90 | | | | F2 |
91 | | v | |
92 | | -------|-------------------------------|
93 | | | |
94 | | | Saved registers. |
95 | | | S6 |
96 |-regoffset | . |
97 | | | . |
98 | | | S0 |
99 | | | pdr.pcreg |
100 | v | |
101 | ----------|-------------------------------|
102 | | |
103 frameoffset | Argument build area, gets |
104 | | 7th ... nth arg for any |
105 | | called procedure. |
106 v | |
107 -------------|-------------------------------|<-- sp
108 | |
109 */
110
111 #define PROC_LOW_ADDR(proc) ((proc)->pdr.adr) /* least address */
112 #define PROC_HIGH_ADDR(proc) ((proc)->pdr.iline) /* upper address bound */
113 #define PROC_DUMMY_FRAME(proc) ((proc)->pdr.iopt) /* frame for CALL_DUMMY */
114 #define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
115 #define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
116 #define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
117 #define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
118 #define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
119 #define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
120 #define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
121 #define PROC_LOCALOFF(proc) ((proc)->pdr.localoff)
122 #define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
123 #define _PROC_MAGIC_ 0x0F0F0F0F
124 #define PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym == _PROC_MAGIC_)
125 #define SET_PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym = _PROC_MAGIC_)
126
127 struct linked_proc_info
128 {
129 struct alpha_extra_func_info info;
130 struct linked_proc_info *next;
131 } *linked_proc_desc_table = NULL;
132
133 \f
134 /* Guaranteed to set fci->saved_regs to some values (it never leaves it
135 NULL). */
136
137 void
138 alpha_find_saved_regs (frame)
139 struct frame_info *frame;
140 {
141 int ireg;
142 CORE_ADDR reg_position;
143 unsigned long mask;
144 alpha_extra_func_info_t proc_desc;
145 int returnreg;
146
147 frame->saved_regs = (struct frame_saved_regs *)
148 obstack_alloc (&frame_cache_obstack, sizeof(struct frame_saved_regs));
149 memset (frame->saved_regs, 0, sizeof (struct frame_saved_regs));
150
151 proc_desc = frame->proc_desc;
152 if (proc_desc == NULL)
153 /* I'm not sure how/whether this can happen. Normally when we can't
154 find a proc_desc, we "synthesize" one using heuristic_proc_desc
155 and set the saved_regs right away. */
156 return;
157
158 /* Fill in the offsets for the registers which gen_mask says
159 were saved. */
160
161 reg_position = frame->frame + PROC_REG_OFFSET (proc_desc);
162 mask = PROC_REG_MASK (proc_desc);
163
164 returnreg = PROC_PC_REG (proc_desc);
165
166 /* Note that RA is always saved first, regardless of it's actual
167 register number. */
168 if (mask & (1 << returnreg))
169 {
170 frame->saved_regs->regs[returnreg] = reg_position;
171 reg_position += 8;
172 mask &= ~(1 << returnreg); /* Clear bit for RA so we
173 don't save again later. */
174 }
175
176 for (ireg = 0; ireg <= 31 ; ++ireg)
177 if (mask & (1 << ireg))
178 {
179 frame->saved_regs->regs[ireg] = reg_position;
180 reg_position += 8;
181 }
182
183 /* Fill in the offsets for the registers which float_mask says
184 were saved. */
185
186 reg_position = frame->frame + PROC_FREG_OFFSET (proc_desc);
187 mask = PROC_FREG_MASK (proc_desc);
188
189 for (ireg = 0; ireg <= 31 ; ++ireg)
190 if (mask & (1 << ireg))
191 {
192 frame->saved_regs->regs[FP0_REGNUM+ireg] = reg_position;
193 reg_position += 8;
194 }
195
196 frame->saved_regs->regs[PC_REGNUM] = frame->saved_regs->regs[returnreg];
197 }
198
199 static CORE_ADDR
200 read_next_frame_reg(fi, regno)
201 struct frame_info *fi;
202 int regno;
203 {
204 /* If it is the frame for sigtramp we have a pointer to the sigcontext
205 on the stack.
206 If the stack layout for __sigtramp changes or if sigcontext offsets
207 change we might have to update this code. */
208 #ifndef SIGFRAME_PC_OFF
209 #define SIGFRAME_PC_OFF (2 * 8)
210 #define SIGFRAME_REGSAVE_OFF (4 * 8)
211 #endif
212 for (; fi; fi = fi->next)
213 {
214 if (fi->signal_handler_caller)
215 {
216 int offset;
217 CORE_ADDR sigcontext_addr = read_memory_integer(fi->frame, 8);
218
219 if (regno == PC_REGNUM)
220 offset = SIGFRAME_PC_OFF;
221 else if (regno < 32)
222 offset = SIGFRAME_REGSAVE_OFF + regno * 8;
223 else
224 return 0;
225 return read_memory_integer(sigcontext_addr + offset, 8);
226 }
227 else if (regno == SP_REGNUM)
228 return fi->frame;
229 else
230 {
231 if (fi->saved_regs == NULL)
232 alpha_find_saved_regs (fi);
233 if (fi->saved_regs->regs[regno])
234 return read_memory_integer(fi->saved_regs->regs[regno], 8);
235 }
236 }
237 return read_register(regno);
238 }
239
240 CORE_ADDR
241 alpha_frame_saved_pc(frame)
242 struct frame_info *frame;
243 {
244 alpha_extra_func_info_t proc_desc = frame->proc_desc;
245 /* We have to get the saved pc from the sigcontext
246 if it is a signal handler frame. */
247 int pcreg = frame->signal_handler_caller ? PC_REGNUM
248 : (proc_desc ? PROC_PC_REG(proc_desc) : RA_REGNUM);
249
250 if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
251 return read_memory_integer(frame->frame - 8, 8);
252
253 return read_next_frame_reg(frame, pcreg);
254 }
255
256 CORE_ADDR
257 alpha_saved_pc_after_call (frame)
258 struct frame_info *frame;
259 {
260 alpha_extra_func_info_t proc_desc = find_proc_desc (frame->pc, frame->next);
261 int pcreg = proc_desc ? PROC_PC_REG (proc_desc) : RA_REGNUM;
262
263 return read_register (pcreg);
264 }
265
266
267 static struct alpha_extra_func_info temp_proc_desc;
268 static struct frame_saved_regs temp_saved_regs;
269
270 /* This fencepost looks highly suspicious to me. Removing it also
271 seems suspicious as it could affect remote debugging across serial
272 lines. */
273
274 static CORE_ADDR
275 heuristic_proc_start(pc)
276 CORE_ADDR pc;
277 {
278 CORE_ADDR start_pc = pc;
279 CORE_ADDR fence = start_pc - heuristic_fence_post;
280
281 if (start_pc == 0) return 0;
282
283 if (heuristic_fence_post == UINT_MAX
284 || fence < VM_MIN_ADDRESS)
285 fence = VM_MIN_ADDRESS;
286
287 /* search back for previous return */
288 for (start_pc -= 4; ; start_pc -= 4)
289 if (start_pc < fence)
290 {
291 /* It's not clear to me why we reach this point when
292 stop_soon_quietly, but with this test, at least we
293 don't print out warnings for every child forked (eg, on
294 decstation). 22apr93 rich@cygnus.com. */
295 if (!stop_soon_quietly)
296 {
297 static int blurb_printed = 0;
298
299 if (fence == VM_MIN_ADDRESS)
300 warning("Hit beginning of text section without finding");
301 else
302 warning("Hit heuristic-fence-post without finding");
303
304 warning("enclosing function for address 0x%lx", pc);
305 if (!blurb_printed)
306 {
307 printf_filtered ("\
308 This warning occurs if you are debugging a function without any symbols\n\
309 (for example, in a stripped executable). In that case, you may wish to\n\
310 increase the size of the search with the `set heuristic-fence-post' command.\n\
311 \n\
312 Otherwise, you told GDB there was a function where there isn't one, or\n\
313 (more likely) you have encountered a bug in GDB.\n");
314 blurb_printed = 1;
315 }
316 }
317
318 return 0;
319 }
320 else if (ABOUT_TO_RETURN(start_pc))
321 break;
322
323 start_pc += 4; /* skip return */
324 return start_pc;
325 }
326
327 static alpha_extra_func_info_t
328 heuristic_proc_desc(start_pc, limit_pc, next_frame)
329 CORE_ADDR start_pc, limit_pc;
330 struct frame_info *next_frame;
331 {
332 CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
333 CORE_ADDR cur_pc;
334 int frame_size;
335 int has_frame_reg = 0;
336 unsigned long reg_mask = 0;
337
338 if (start_pc == 0)
339 return NULL;
340 memset (&temp_proc_desc, '\0', sizeof(temp_proc_desc));
341 memset (&temp_saved_regs, '\0', sizeof(struct frame_saved_regs));
342 PROC_LOW_ADDR (&temp_proc_desc) = start_pc;
343
344 if (start_pc + 200 < limit_pc)
345 limit_pc = start_pc + 200;
346 frame_size = 0;
347 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4)
348 {
349 char buf[4];
350 unsigned long word;
351 int status;
352
353 status = read_memory_nobpt (cur_pc, buf, 4);
354 if (status)
355 memory_error (status, cur_pc);
356 word = extract_unsigned_integer (buf, 4);
357
358 if ((word & 0xffff0000) == 0x23de0000) /* lda $sp,n($sp) */
359 frame_size += (-word) & 0xffff;
360 else if ((word & 0xfc1f0000) == 0xb41e0000 /* stq reg,n($sp) */
361 && (word & 0xffff0000) != 0xb7fe0000) /* reg != $zero */
362 {
363 int reg = (word & 0x03e00000) >> 21;
364 reg_mask |= 1 << reg;
365 temp_saved_regs.regs[reg] = sp + (short)word;
366 }
367 else if (word == 0x47de040f) /* bis sp,sp fp */
368 has_frame_reg = 1;
369 }
370 if (has_frame_reg)
371 PROC_FRAME_REG(&temp_proc_desc) = GCC_FP_REGNUM;
372 else
373 PROC_FRAME_REG(&temp_proc_desc) = SP_REGNUM;
374 PROC_FRAME_OFFSET(&temp_proc_desc) = frame_size;
375 PROC_REG_MASK(&temp_proc_desc) = reg_mask;
376 PROC_PC_REG(&temp_proc_desc) = RA_REGNUM;
377 PROC_LOCALOFF(&temp_proc_desc) = 0; /* XXX - bogus */
378 return &temp_proc_desc;
379 }
380
381 /* This returns the PC of the first inst after the prologue. If we can't
382 find the prologue, then return 0. */
383
384 static CORE_ADDR
385 after_prologue (pc, proc_desc)
386 CORE_ADDR pc;
387 alpha_extra_func_info_t proc_desc;
388 {
389 struct symtab_and_line sal;
390 CORE_ADDR func_addr, func_end;
391
392 if (!proc_desc)
393 proc_desc = find_proc_desc (pc, NULL);
394
395 if (proc_desc)
396 {
397 /* If function is frameless, then we need to do it the hard way. I
398 strongly suspect that frameless always means prologueless... */
399 if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
400 && PROC_FRAME_OFFSET (proc_desc) == 0)
401 return 0;
402 }
403
404 if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
405 return 0; /* Unknown */
406
407 sal = find_pc_line (func_addr, 0);
408
409 if (sal.end < func_end)
410 return sal.end;
411
412 /* The line after the prologue is after the end of the function. In this
413 case, tell the caller to find the prologue the hard way. */
414
415 return 0;
416 }
417
418 /* Return non-zero if we *might* be in a function prologue. Return zero if we
419 are definatly *not* in a function prologue. */
420
421 static int
422 in_prologue (pc, proc_desc)
423 CORE_ADDR pc;
424 alpha_extra_func_info_t proc_desc;
425 {
426 CORE_ADDR after_prologue_pc;
427
428 after_prologue_pc = after_prologue (pc, proc_desc);
429
430 if (after_prologue_pc == 0
431 || pc < after_prologue_pc)
432 return 1;
433 else
434 return 0;
435 }
436
437 static alpha_extra_func_info_t
438 find_proc_desc (pc, next_frame)
439 CORE_ADDR pc;
440 struct frame_info *next_frame;
441 {
442 alpha_extra_func_info_t proc_desc;
443 struct block *b;
444 struct symbol *sym;
445 CORE_ADDR startaddr;
446
447 /* Try to get the proc_desc from the linked call dummy proc_descs
448 if the pc is in the call dummy.
449 This is hairy. In the case of nested dummy calls we have to find the
450 right proc_desc, but we might not yet know the frame for the dummy
451 as it will be contained in the proc_desc we are searching for.
452 So we have to find the proc_desc whose frame is closest to the current
453 stack pointer. */
454
455 if (PC_IN_CALL_DUMMY (pc, 0, 0))
456 {
457 struct linked_proc_info *link;
458 CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
459 alpha_extra_func_info_t found_proc_desc = NULL;
460 long min_distance = LONG_MAX;
461
462 for (link = linked_proc_desc_table; link; link = link->next)
463 {
464 long distance = (CORE_ADDR) PROC_DUMMY_FRAME (&link->info) - sp;
465 if (distance > 0 && distance < min_distance)
466 {
467 min_distance = distance;
468 found_proc_desc = &link->info;
469 }
470 }
471 if (found_proc_desc != NULL)
472 return found_proc_desc;
473 }
474
475 b = block_for_pc(pc);
476
477 find_pc_partial_function (pc, NULL, &startaddr, NULL);
478 if (b == NULL)
479 sym = NULL;
480 else
481 {
482 if (startaddr > BLOCK_START (b))
483 /* This is the "pathological" case referred to in a comment in
484 print_frame_info. It might be better to move this check into
485 symbol reading. */
486 sym = NULL;
487 else
488 sym = lookup_symbol (MIPS_EFI_SYMBOL_NAME, b, LABEL_NAMESPACE,
489 0, NULL);
490 }
491
492 if (sym)
493 {
494 /* IF this is the topmost frame AND
495 * (this proc does not have debugging information OR
496 * the PC is in the procedure prologue)
497 * THEN create a "heuristic" proc_desc (by analyzing
498 * the actual code) to replace the "official" proc_desc.
499 */
500 proc_desc = (alpha_extra_func_info_t)SYMBOL_VALUE(sym);
501 if (next_frame == NULL)
502 {
503 if (PROC_DESC_IS_DUMMY (proc_desc) || in_prologue (pc, proc_desc))
504 {
505 alpha_extra_func_info_t found_heuristic =
506 heuristic_proc_desc (PROC_LOW_ADDR (proc_desc),
507 pc, next_frame);
508 PROC_LOCALOFF (found_heuristic) = PROC_LOCALOFF (proc_desc);
509 if (found_heuristic)
510 proc_desc = found_heuristic;
511 }
512 }
513 }
514 else
515 {
516 /* Is linked_proc_desc_table really necessary? It only seems to be used
517 by procedure call dummys. However, the procedures being called ought
518 to have their own proc_descs, and even if they don't,
519 heuristic_proc_desc knows how to create them! */
520
521 register struct linked_proc_info *link;
522 for (link = linked_proc_desc_table; link; link = link->next)
523 if (PROC_LOW_ADDR(&link->info) <= pc
524 && PROC_HIGH_ADDR(&link->info) > pc)
525 return &link->info;
526
527 if (startaddr == 0)
528 startaddr = heuristic_proc_start (pc);
529
530 proc_desc =
531 heuristic_proc_desc (startaddr, pc, next_frame);
532 }
533 return proc_desc;
534 }
535
536 alpha_extra_func_info_t cached_proc_desc;
537
538 CORE_ADDR
539 alpha_frame_chain(frame)
540 struct frame_info *frame;
541 {
542 alpha_extra_func_info_t proc_desc;
543 CORE_ADDR saved_pc = FRAME_SAVED_PC(frame);
544
545 if (saved_pc == 0 || inside_entry_file (saved_pc))
546 return 0;
547
548 proc_desc = find_proc_desc(saved_pc, frame);
549 if (!proc_desc)
550 return 0;
551
552 cached_proc_desc = proc_desc;
553
554 /* Fetch the frame pointer for a dummy frame from the procedure
555 descriptor. */
556 if (PROC_DESC_IS_DUMMY(proc_desc))
557 return (CORE_ADDR) PROC_DUMMY_FRAME(proc_desc);
558
559 /* If no frame pointer and frame size is zero, we must be at end
560 of stack (or otherwise hosed). If we don't check frame size,
561 we loop forever if we see a zero size frame. */
562 if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
563 && PROC_FRAME_OFFSET (proc_desc) == 0
564 /* The previous frame from a sigtramp frame might be frameless
565 and have frame size zero. */
566 && !frame->signal_handler_caller)
567 {
568 /* The alpha __sigtramp routine is frameless and has a frame size
569 of zero, but we are able to backtrace through it. */
570 char *name;
571 find_pc_partial_function (saved_pc, &name,
572 (CORE_ADDR *)NULL, (CORE_ADDR *)NULL);
573 if (IN_SIGTRAMP (saved_pc, name))
574 return frame->frame;
575 else
576 return 0;
577 }
578 else
579 return read_next_frame_reg(frame, PROC_FRAME_REG(proc_desc))
580 + PROC_FRAME_OFFSET(proc_desc);
581 }
582
583 void
584 init_extra_frame_info (frame)
585 struct frame_info *frame;
586 {
587 /* Use proc_desc calculated in frame_chain */
588 alpha_extra_func_info_t proc_desc =
589 frame->next ? cached_proc_desc : find_proc_desc(frame->pc, frame->next);
590
591 frame->saved_regs = NULL;
592 frame->proc_desc =
593 proc_desc == &temp_proc_desc ? 0 : proc_desc;
594 if (proc_desc)
595 {
596 /* Get the locals offset from the procedure descriptor, it is valid
597 even if we are in the middle of the prologue. */
598 frame->localoff = PROC_LOCALOFF(proc_desc);
599
600 /* Fixup frame-pointer - only needed for top frame */
601
602 /* Fetch the frame pointer for a dummy frame from the procedure
603 descriptor. */
604 if (PROC_DESC_IS_DUMMY(proc_desc))
605 frame->frame = (CORE_ADDR) PROC_DUMMY_FRAME(proc_desc);
606
607 /* This may not be quite right, if proc has a real frame register.
608 Get the value of the frame relative sp, procedure might have been
609 interrupted by a signal at it's very start. */
610 else if (frame->pc == PROC_LOW_ADDR (proc_desc) && !PROC_DESC_IS_DUMMY (proc_desc))
611 frame->frame = read_next_frame_reg (frame->next, SP_REGNUM);
612 else
613 frame->frame = read_next_frame_reg (frame->next, PROC_FRAME_REG (proc_desc))
614 + PROC_FRAME_OFFSET (proc_desc);
615
616 if (proc_desc == &temp_proc_desc)
617 {
618 frame->saved_regs = (struct frame_saved_regs*)
619 obstack_alloc (&frame_cache_obstack,
620 sizeof (struct frame_saved_regs));
621 *frame->saved_regs = temp_saved_regs;
622 frame->saved_regs->regs[PC_REGNUM] = frame->saved_regs->regs[RA_REGNUM];
623 }
624 }
625 }
626
627 /* ALPHA stack frames are almost impenetrable. When execution stops,
628 we basically have to look at symbol information for the function
629 that we stopped in, which tells us *which* register (if any) is
630 the base of the frame pointer, and what offset from that register
631 the frame itself is at.
632
633 This presents a problem when trying to examine a stack in memory
634 (that isn't executing at the moment), using the "frame" command. We
635 don't have a PC, nor do we have any registers except SP.
636
637 This routine takes two arguments, SP and PC, and tries to make the
638 cached frames look as if these two arguments defined a frame on the
639 cache. This allows the rest of info frame to extract the important
640 arguments without difficulty. */
641
642 struct frame_info *
643 setup_arbitrary_frame (argc, argv)
644 int argc;
645 CORE_ADDR *argv;
646 {
647 if (argc != 2)
648 error ("ALPHA frame specifications require two arguments: sp and pc");
649
650 return create_new_frame (argv[0], argv[1]);
651 }
652
653 /* The alpha passes the first six arguments in the registers, the rest on
654 the stack. The register arguments are eventually transferred to the
655 argument transfer area immediately below the stack by the called function
656 anyway. So we `push' at least six arguments on the stack, `reload' the
657 argument registers and then adjust the stack pointer to point past the
658 sixth argument. This algorithm simplifies the passing of a large struct
659 which extends from the registers to the stack.
660 If the called function is returning a structure, the address of the
661 structure to be returned is passed as a hidden first argument. */
662
663 CORE_ADDR
664 alpha_push_arguments (nargs, args, sp, struct_return, struct_addr)
665 int nargs;
666 value_ptr *args;
667 CORE_ADDR sp;
668 int struct_return;
669 CORE_ADDR struct_addr;
670 {
671 register i;
672 int accumulate_size = struct_return ? 8 : 0;
673 int arg_regs_size = ALPHA_NUM_ARG_REGS * 8;
674 struct alpha_arg { char *contents; int len; int offset; };
675 struct alpha_arg *alpha_args =
676 (struct alpha_arg*)alloca (nargs * sizeof (struct alpha_arg));
677 register struct alpha_arg *m_arg;
678 char raw_buffer[sizeof (CORE_ADDR)];
679 int required_arg_regs;
680
681 for (i = 0, m_arg = alpha_args; i < nargs; i++, m_arg++)
682 {
683 value_ptr arg = args[i];
684 /* Cast argument to long if necessary as the compiler does it too. */
685 if (TYPE_LENGTH (VALUE_TYPE (arg)) < TYPE_LENGTH (builtin_type_long))
686 arg = value_cast (builtin_type_long, arg);
687 m_arg->len = TYPE_LENGTH (VALUE_TYPE (arg));
688 m_arg->offset = accumulate_size;
689 accumulate_size = (accumulate_size + m_arg->len + 7) & ~7;
690 m_arg->contents = VALUE_CONTENTS(arg);
691 }
692
693 /* Determine required argument register loads, loading an argument register
694 is expensive as it uses three ptrace calls. */
695 required_arg_regs = accumulate_size / 8;
696 if (required_arg_regs > ALPHA_NUM_ARG_REGS)
697 required_arg_regs = ALPHA_NUM_ARG_REGS;
698
699 /* Make room for the arguments on the stack. */
700 if (accumulate_size < arg_regs_size)
701 accumulate_size = arg_regs_size;
702 sp -= accumulate_size;
703
704 /* Keep sp aligned to a multiple of 16 as the compiler does it too. */
705 sp &= ~15;
706
707 /* `Push' arguments on the stack. */
708 for (i = nargs; m_arg--, --i >= 0; )
709 write_memory(sp + m_arg->offset, m_arg->contents, m_arg->len);
710 if (struct_return)
711 {
712 store_address (raw_buffer, sizeof (CORE_ADDR), struct_addr);
713 write_memory (sp, raw_buffer, sizeof (CORE_ADDR));
714 }
715
716 /* Load the argument registers. */
717 for (i = 0; i < required_arg_regs; i++)
718 {
719 LONGEST val;
720
721 val = read_memory_integer (sp + i * 8, 8);
722 write_register (A0_REGNUM + i, val);
723 write_register (FPA0_REGNUM + i, val);
724 }
725
726 return sp + arg_regs_size;
727 }
728
729 void
730 alpha_push_dummy_frame()
731 {
732 int ireg;
733 struct linked_proc_info *link;
734 alpha_extra_func_info_t proc_desc;
735 CORE_ADDR sp = read_register (SP_REGNUM);
736 CORE_ADDR save_address;
737 char raw_buffer[MAX_REGISTER_RAW_SIZE];
738 unsigned long mask;
739
740 link = (struct linked_proc_info *) xmalloc(sizeof (struct linked_proc_info));
741 link->next = linked_proc_desc_table;
742 linked_proc_desc_table = link;
743
744 proc_desc = &link->info;
745
746 /*
747 * The registers we must save are all those not preserved across
748 * procedure calls.
749 * In addition, we must save the PC and RA.
750 *
751 * Dummy frame layout:
752 * (high memory)
753 * Saved PC
754 * Saved F30
755 * ...
756 * Saved F0
757 * Saved R29
758 * ...
759 * Saved R0
760 * Saved R26 (RA)
761 * Parameter build area
762 * (low memory)
763 */
764
765 /* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<31. */
766 #define MASK(i,j) (((1L << ((j)+1)) - 1) ^ ((1L << (i)) - 1))
767 #define GEN_REG_SAVE_MASK (MASK(0,8) | MASK(16,29))
768 #define GEN_REG_SAVE_COUNT 24
769 #define FLOAT_REG_SAVE_MASK (MASK(0,1) | MASK(10,30))
770 #define FLOAT_REG_SAVE_COUNT 23
771 /* The special register is the PC as we have no bit for it in the save masks.
772 alpha_frame_saved_pc knows where the pc is saved in a dummy frame. */
773 #define SPECIAL_REG_SAVE_COUNT 1
774
775 PROC_REG_MASK(proc_desc) = GEN_REG_SAVE_MASK;
776 PROC_FREG_MASK(proc_desc) = FLOAT_REG_SAVE_MASK;
777 /* PROC_REG_OFFSET is the offset from the dummy frame to the saved RA,
778 but keep SP aligned to a multiple of 16. */
779 PROC_REG_OFFSET(proc_desc) =
780 - ((8 * (SPECIAL_REG_SAVE_COUNT
781 + GEN_REG_SAVE_COUNT
782 + FLOAT_REG_SAVE_COUNT)
783 + 15) & ~15);
784 PROC_FREG_OFFSET(proc_desc) =
785 PROC_REG_OFFSET(proc_desc) + 8 * GEN_REG_SAVE_COUNT;
786
787 /* Save general registers.
788 The return address register is the first saved register, all other
789 registers follow in ascending order.
790 The PC is saved immediately below the SP. */
791 save_address = sp + PROC_REG_OFFSET(proc_desc);
792 store_address (raw_buffer, 8, read_register (RA_REGNUM));
793 write_memory (save_address, raw_buffer, 8);
794 save_address += 8;
795 mask = PROC_REG_MASK(proc_desc) & 0xffffffffL;
796 for (ireg = 0; mask; ireg++, mask >>= 1)
797 if (mask & 1)
798 {
799 if (ireg == RA_REGNUM)
800 continue;
801 store_address (raw_buffer, 8, read_register (ireg));
802 write_memory (save_address, raw_buffer, 8);
803 save_address += 8;
804 }
805
806 store_address (raw_buffer, 8, read_register (PC_REGNUM));
807 write_memory (sp - 8, raw_buffer, 8);
808
809 /* Save floating point registers. */
810 save_address = sp + PROC_FREG_OFFSET(proc_desc);
811 mask = PROC_FREG_MASK(proc_desc) & 0xffffffffL;
812 for (ireg = 0; mask; ireg++, mask >>= 1)
813 if (mask & 1)
814 {
815 store_address (raw_buffer, 8, read_register (ireg + FP0_REGNUM));
816 write_memory (save_address, raw_buffer, 8);
817 save_address += 8;
818 }
819
820 /* Set and save the frame address for the dummy.
821 This is tricky. The only registers that are suitable for a frame save
822 are those that are preserved across procedure calls (s0-s6). But if
823 a read system call is interrupted and then a dummy call is made
824 (see testsuite/gdb.t17/interrupt.exp) the dummy call hangs till the read
825 is satisfied. Then it returns with the s0-s6 registers set to the values
826 on entry to the read system call and our dummy frame pointer would be
827 destroyed. So we save the dummy frame in the proc_desc and handle the
828 retrieval of the frame pointer of a dummy specifically. The frame register
829 is set to the virtual frame (pseudo) register, it's value will always
830 be read as zero and will help us to catch any errors in the dummy frame
831 retrieval code. */
832 PROC_DUMMY_FRAME(proc_desc) = sp;
833 PROC_FRAME_REG(proc_desc) = FP_REGNUM;
834 PROC_FRAME_OFFSET(proc_desc) = 0;
835 sp += PROC_REG_OFFSET(proc_desc);
836 write_register (SP_REGNUM, sp);
837
838 PROC_LOW_ADDR(proc_desc) = CALL_DUMMY_ADDRESS ();
839 PROC_HIGH_ADDR(proc_desc) = PROC_LOW_ADDR(proc_desc) + 4;
840
841 SET_PROC_DESC_IS_DUMMY(proc_desc);
842 PROC_PC_REG(proc_desc) = RA_REGNUM;
843 }
844
845 void
846 alpha_pop_frame()
847 {
848 register int regnum;
849 struct frame_info *frame = get_current_frame ();
850 CORE_ADDR new_sp = frame->frame;
851
852 alpha_extra_func_info_t proc_desc = frame->proc_desc;
853
854 write_register (PC_REGNUM, FRAME_SAVED_PC(frame));
855 if (frame->saved_regs == NULL)
856 alpha_find_saved_regs (frame);
857 if (proc_desc)
858 {
859 for (regnum = 32; --regnum >= 0; )
860 if (PROC_REG_MASK(proc_desc) & (1 << regnum))
861 write_register (regnum,
862 read_memory_integer (frame->saved_regs->regs[regnum],
863 8));
864 for (regnum = 32; --regnum >= 0; )
865 if (PROC_FREG_MASK(proc_desc) & (1 << regnum))
866 write_register (regnum + FP0_REGNUM,
867 read_memory_integer (frame->saved_regs->regs[regnum + FP0_REGNUM], 8));
868 }
869 write_register (SP_REGNUM, new_sp);
870 flush_cached_frames ();
871
872 if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
873 {
874 struct linked_proc_info *pi_ptr, *prev_ptr;
875
876 for (pi_ptr = linked_proc_desc_table, prev_ptr = NULL;
877 pi_ptr != NULL;
878 prev_ptr = pi_ptr, pi_ptr = pi_ptr->next)
879 {
880 if (&pi_ptr->info == proc_desc)
881 break;
882 }
883
884 if (pi_ptr == NULL)
885 error ("Can't locate dummy extra frame info\n");
886
887 if (prev_ptr != NULL)
888 prev_ptr->next = pi_ptr->next;
889 else
890 linked_proc_desc_table = pi_ptr->next;
891
892 free (pi_ptr);
893 }
894 }
895 \f
896 /* To skip prologues, I use this predicate. Returns either PC itself
897 if the code at PC does not look like a function prologue; otherwise
898 returns an address that (if we're lucky) follows the prologue. If
899 LENIENT, then we must skip everything which is involved in setting
900 up the frame (it's OK to skip more, just so long as we don't skip
901 anything which might clobber the registers which are being saved.
902 Currently we must not skip more on the alpha, but we might the lenient
903 stuff some day. */
904
905 CORE_ADDR
906 alpha_skip_prologue (pc, lenient)
907 CORE_ADDR pc;
908 int lenient;
909 {
910 unsigned long inst;
911 int offset;
912 CORE_ADDR post_prologue_pc;
913 char buf[4];
914
915 #ifdef GDB_TARGET_HAS_SHARED_LIBS
916 /* Silently return the unaltered pc upon memory errors.
917 This could happen on OSF/1 if decode_line_1 tries to skip the
918 prologue for quickstarted shared library functions when the
919 shared library is not yet mapped in.
920 Reading target memory is slow over serial lines, so we perform
921 this check only if the target has shared libraries. */
922 if (target_read_memory (pc, buf, 4))
923 return pc;
924 #endif
925
926 /* See if we can determine the end of the prologue via the symbol table.
927 If so, then return either PC, or the PC after the prologue, whichever
928 is greater. */
929
930 post_prologue_pc = after_prologue (pc, NULL);
931
932 if (post_prologue_pc != 0)
933 return max (pc, post_prologue_pc);
934
935 /* Can't determine prologue from the symbol table, need to examine
936 instructions. */
937
938 /* Skip the typical prologue instructions. These are the stack adjustment
939 instruction and the instructions that save registers on the stack
940 or in the gcc frame. */
941 for (offset = 0; offset < 100; offset += 4)
942 {
943 int status;
944
945 status = read_memory_nobpt (pc + offset, buf, 4);
946 if (status)
947 memory_error (status, pc + offset);
948 inst = extract_unsigned_integer (buf, 4);
949
950 /* The alpha has no delay slots. But let's keep the lenient stuff,
951 we might need it for something else in the future. */
952 if (lenient && 0)
953 continue;
954
955 if ((inst & 0xffff0000) == 0x27bb0000) /* ldah $gp,n($t12) */
956 continue;
957 if ((inst & 0xffff0000) == 0x23bd0000) /* lda $gp,n($gp) */
958 continue;
959 if ((inst & 0xffff0000) == 0x23de0000) /* lda $sp,n($sp) */
960 continue;
961 else if ((inst & 0xfc1f0000) == 0xb41e0000
962 && (inst & 0xffff0000) != 0xb7fe0000)
963 continue; /* stq reg,n($sp) */
964 /* reg != $zero */
965 else if ((inst & 0xfc1f0000) == 0x9c1e0000
966 && (inst & 0xffff0000) != 0x9ffe0000)
967 continue; /* stt reg,n($sp) */
968 /* reg != $zero */
969 else if (inst == 0x47de040f) /* bis sp,sp,fp */
970 continue;
971 else
972 break;
973 }
974 return pc + offset;
975 }
976
977 #if 0
978 /* Is address PC in the prologue (loosely defined) for function at
979 STARTADDR? */
980
981 static int
982 alpha_in_lenient_prologue (startaddr, pc)
983 CORE_ADDR startaddr;
984 CORE_ADDR pc;
985 {
986 CORE_ADDR end_prologue = alpha_skip_prologue (startaddr, 1);
987 return pc >= startaddr && pc < end_prologue;
988 }
989 #endif
990
991 /* The alpha needs a conversion between register and memory format if
992 the register is a floating point register and
993 memory format is float, as the register format must be double
994 or
995 memory format is an integer with 4 bytes or less, as the representation
996 of integers in floating point registers is different. */
997 void
998 alpha_register_convert_to_virtual (regnum, valtype, raw_buffer, virtual_buffer)
999 int regnum;
1000 struct type *valtype;
1001 char *raw_buffer;
1002 char *virtual_buffer;
1003 {
1004 if (TYPE_LENGTH (valtype) >= REGISTER_RAW_SIZE (regnum))
1005 {
1006 memcpy (virtual_buffer, raw_buffer, REGISTER_VIRTUAL_SIZE (regnum));
1007 return;
1008 }
1009
1010 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1011 {
1012 double d = extract_floating (raw_buffer, REGISTER_RAW_SIZE (regnum));
1013 store_floating (virtual_buffer, TYPE_LENGTH (valtype), d);
1014 }
1015 else if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 4)
1016 {
1017 unsigned LONGEST l;
1018 l = extract_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (regnum));
1019 l = ((l >> 32) & 0xc0000000) | ((l >> 29) & 0x3fffffff);
1020 store_unsigned_integer (virtual_buffer, TYPE_LENGTH (valtype), l);
1021 }
1022 else
1023 error ("Cannot retrieve value from floating point register");
1024 }
1025
1026 void
1027 alpha_register_convert_to_raw (valtype, regnum, virtual_buffer, raw_buffer)
1028 struct type *valtype;
1029 int regnum;
1030 char *virtual_buffer;
1031 char *raw_buffer;
1032 {
1033 if (TYPE_LENGTH (valtype) >= REGISTER_RAW_SIZE (regnum))
1034 {
1035 memcpy (raw_buffer, virtual_buffer, REGISTER_RAW_SIZE (regnum));
1036 return;
1037 }
1038
1039 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1040 {
1041 double d = extract_floating (virtual_buffer, TYPE_LENGTH (valtype));
1042 store_floating (raw_buffer, REGISTER_RAW_SIZE (regnum), d);
1043 }
1044 else if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 4)
1045 {
1046 unsigned LONGEST l;
1047 if (TYPE_UNSIGNED (valtype))
1048 l = extract_unsigned_integer (virtual_buffer, TYPE_LENGTH (valtype));
1049 else
1050 l = extract_signed_integer (virtual_buffer, TYPE_LENGTH (valtype));
1051 l = ((l & 0xc0000000) << 32) | ((l & 0x3fffffff) << 29);
1052 store_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (regnum), l);
1053 }
1054 else
1055 error ("Cannot store value in floating point register");
1056 }
1057
1058 /* Given a return value in `regbuf' with a type `valtype',
1059 extract and copy its value into `valbuf'. */
1060
1061 void
1062 alpha_extract_return_value (valtype, regbuf, valbuf)
1063 struct type *valtype;
1064 char regbuf[REGISTER_BYTES];
1065 char *valbuf;
1066 {
1067 int regnum;
1068
1069 regnum = TYPE_CODE (valtype) == TYPE_CODE_FLT ? FP0_REGNUM : V0_REGNUM;
1070
1071 memcpy (valbuf, regbuf + REGISTER_BYTE (regnum), TYPE_LENGTH (valtype));
1072 }
1073
1074 /* Given a return value in `regbuf' with a type `valtype',
1075 write its value into the appropriate register. */
1076
1077 void
1078 alpha_store_return_value (valtype, valbuf)
1079 struct type *valtype;
1080 char *valbuf;
1081 {
1082 int regnum;
1083 char raw_buffer[MAX_REGISTER_RAW_SIZE];
1084
1085 regnum = TYPE_CODE (valtype) == TYPE_CODE_FLT ? FP0_REGNUM : V0_REGNUM;
1086 memcpy(raw_buffer, valbuf, TYPE_LENGTH (valtype));
1087
1088 write_register_bytes(REGISTER_BYTE (regnum), raw_buffer, TYPE_LENGTH (valtype));
1089 }
1090
1091 /* Just like reinit_frame_cache, but with the right arguments to be
1092 callable as an sfunc. */
1093
1094 static void
1095 reinit_frame_cache_sfunc (args, from_tty, c)
1096 char *args;
1097 int from_tty;
1098 struct cmd_list_element *c;
1099 {
1100 reinit_frame_cache ();
1101 }
1102
1103 /* This is the definition of CALL_DUMMY_ADDRESS. It's a heuristic that is used
1104 to find a convenient place in the text segment to stick a breakpoint to
1105 detect the completion of a target function call (ala call_function_by_hand).
1106 */
1107
1108 CORE_ADDR
1109 alpha_call_dummy_address ()
1110 {
1111 CORE_ADDR entry;
1112 struct minimal_symbol *sym;
1113
1114 entry = entry_point_address ();
1115
1116 if (entry != 0)
1117 return entry;
1118
1119 sym = lookup_minimal_symbol ("_Prelude", NULL, symfile_objfile);
1120
1121 if (!sym || MSYMBOL_TYPE (sym) != mst_text)
1122 return 0;
1123 else
1124 return SYMBOL_VALUE_ADDRESS (sym) + 4;
1125 }
1126
1127 void
1128 _initialize_alpha_tdep ()
1129 {
1130 struct cmd_list_element *c;
1131
1132 tm_print_insn = print_insn_alpha;
1133
1134 /* Let the user set the fence post for heuristic_proc_start. */
1135
1136 /* We really would like to have both "0" and "unlimited" work, but
1137 command.c doesn't deal with that. So make it a var_zinteger
1138 because the user can always use "999999" or some such for unlimited. */
1139 c = add_set_cmd ("heuristic-fence-post", class_support, var_zinteger,
1140 (char *) &heuristic_fence_post,
1141 "\
1142 Set the distance searched for the start of a function.\n\
1143 If you are debugging a stripped executable, GDB needs to search through the\n\
1144 program for the start of a function. This command sets the distance of the\n\
1145 search. The only need to set it is when debugging a stripped executable.",
1146 &setlist);
1147 /* We need to throw away the frame cache when we set this, since it
1148 might change our ability to get backtraces. */
1149 c->function.sfunc = reinit_frame_cache_sfunc;
1150 add_show_from_set (c, &showlist);
1151 }
This page took 0.055102 seconds and 4 git commands to generate.