]> Git Repo - binutils.git/blob - gdb/mips-tdep.c
* gdb.disasm/hppa.s (addib_tests): Fix typo.
[binutils.git] / gdb / mips-tdep.c
1 /* Target-dependent code for the MIPS architecture, for GDB, the GNU Debugger.
2    Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994
3    Free Software Foundation, Inc.
4    Contributed by Alessandro Forin([email protected]) at CMU
5    and by Per Bothner([email protected]) at U.Wisconsin.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
22
23 #include "defs.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "symtab.h"
27 #include "value.h"
28 #include "gdbcmd.h"
29 #include "language.h"
30 #include "gdbcore.h"
31 #include "symfile.h"
32 #include "objfiles.h"
33
34 #include "opcode/mips.h"
35
36 #define VM_MIN_ADDRESS (unsigned)0x400000
37 \f
38 #if 0
39 static int mips_in_lenient_prologue PARAMS ((CORE_ADDR, CORE_ADDR));
40 #endif
41
42 /* Some MIPS boards don't support floating point, so we permit the
43    user to turn it off.  */
44 int mips_fpu = 1;
45
46 /* Heuristic_proc_start may hunt through the text section for a long
47    time across a 2400 baud serial line.  Allows the user to limit this
48    search.  */
49 static unsigned int heuristic_fence_post = 0;
50
51 #define PROC_LOW_ADDR(proc) ((proc)->pdr.adr) /* least address */
52 #define PROC_HIGH_ADDR(proc) ((proc)->pdr.iline) /* upper address bound */
53 #define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
54 #define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
55 #define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
56 #define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
57 #define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
58 #define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
59 #define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
60 #define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
61 #define _PROC_MAGIC_ 0x0F0F0F0F
62 #define PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym == _PROC_MAGIC_)
63 #define SET_PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym = _PROC_MAGIC_)
64
65 struct linked_proc_info
66 {
67   struct mips_extra_func_info info;
68   struct linked_proc_info *next;
69 } *linked_proc_desc_table = NULL;
70
71 \f
72 #define READ_FRAME_REG(fi, regno) read_next_frame_reg((fi)->next, regno)
73
74 static int
75 read_next_frame_reg(fi, regno)
76      FRAME fi;
77      int regno;
78 {
79   /* If it is the frame for sigtramp we have a complete sigcontext
80      somewhere above the frame and we get the saved registers from there.
81      If the stack layout for sigtramp changes we might have to change these
82      constants and the companion fixup_sigtramp in mdebugread.c  */
83 #ifndef SIGFRAME_BASE
84 /* To satisfy alignment restrictions the sigcontext is located 4 bytes
85    above the sigtramp frame.  */
86 #define SIGFRAME_BASE           4
87 #define SIGFRAME_PC_OFF         (SIGFRAME_BASE + 2 * 4)
88 #define SIGFRAME_REGSAVE_OFF    (SIGFRAME_BASE + 3 * 4)
89 #endif
90 #ifndef SIGFRAME_REG_SIZE
91 #define SIGFRAME_REG_SIZE       4
92 #endif
93   for (; fi; fi = fi->next)
94       if (fi->signal_handler_caller) {
95           int offset;
96           if (regno == PC_REGNUM) offset = SIGFRAME_PC_OFF;
97           else if (regno < 32) offset = (SIGFRAME_REGSAVE_OFF
98                                          + regno * SIGFRAME_REG_SIZE);
99           else return 0;
100           return read_memory_integer(fi->frame + offset, 4);
101       }
102       else if (regno == SP_REGNUM) return fi->frame;
103       else if (fi->saved_regs->regs[regno])
104         return read_memory_integer(fi->saved_regs->regs[regno], 4);
105   return read_register(regno);
106 }
107
108 int
109 mips_frame_saved_pc(frame)
110      FRAME frame;
111 {
112   mips_extra_func_info_t proc_desc = frame->proc_desc;
113   /* We have to get the saved pc from the sigcontext
114      if it is a signal handler frame.  */
115   int pcreg = frame->signal_handler_caller ? PC_REGNUM
116               : (proc_desc ? PROC_PC_REG(proc_desc) : RA_REGNUM);
117
118   if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
119       return read_memory_integer(frame->frame - 4, 4);
120
121   return read_next_frame_reg(frame, pcreg);
122 }
123
124 static struct mips_extra_func_info temp_proc_desc;
125 static struct frame_saved_regs temp_saved_regs;
126
127 /* This fencepost looks highly suspicious to me.  Removing it also
128    seems suspicious as it could affect remote debugging across serial
129    lines.  */
130
131 static CORE_ADDR
132 heuristic_proc_start(pc)
133     CORE_ADDR pc;
134 {
135     CORE_ADDR start_pc = pc;
136     CORE_ADDR fence = start_pc - heuristic_fence_post;
137
138     if (start_pc == 0)  return 0;
139
140     if (heuristic_fence_post == UINT_MAX
141         || fence < VM_MIN_ADDRESS)
142       fence = VM_MIN_ADDRESS;
143
144     /* search back for previous return */
145     for (start_pc -= 4; ; start_pc -= 4)
146         if (start_pc < fence)
147           {
148             /* It's not clear to me why we reach this point when
149                stop_soon_quietly, but with this test, at least we
150                don't print out warnings for every child forked (eg, on
151                decstation).  22apr93 [email protected].  */
152             if (!stop_soon_quietly)
153               {
154                 static int blurb_printed = 0;
155
156                 if (fence == VM_MIN_ADDRESS)
157                   warning("Hit beginning of text section without finding");
158                 else
159                   warning("Hit heuristic-fence-post without finding");
160                 
161                 warning("enclosing function for address 0x%x", pc);
162                 if (!blurb_printed)
163                   {
164                     printf_filtered ("\
165 This warning occurs if you are debugging a function without any symbols\n\
166 (for example, in a stripped executable).  In that case, you may wish to\n\
167 increase the size of the search with the `set heuristic-fence-post' command.\n\
168 \n\
169 Otherwise, you told GDB there was a function where there isn't one, or\n\
170 (more likely) you have encountered a bug in GDB.\n");
171                     blurb_printed = 1;
172                   }
173               }
174
175             return 0; 
176           }
177         else if (ABOUT_TO_RETURN(start_pc))
178             break;
179
180     start_pc += 8; /* skip return, and its delay slot */
181 #if 0
182     /* skip nops (usually 1) 0 - is this */
183     while (start_pc < pc && read_memory_integer (start_pc, 4) == 0)
184         start_pc += 4;
185 #endif
186     return start_pc;
187 }
188
189 static mips_extra_func_info_t
190 heuristic_proc_desc(start_pc, limit_pc, next_frame)
191     CORE_ADDR start_pc, limit_pc;
192     FRAME next_frame;
193 {
194     CORE_ADDR sp = next_frame ? next_frame->frame : read_register (SP_REGNUM);
195     CORE_ADDR cur_pc;
196     int frame_size;
197     int has_frame_reg = 0;
198     int reg30 = 0; /* Value of $r30. Used by gcc for frame-pointer */
199     unsigned long reg_mask = 0;
200
201     if (start_pc == 0) return NULL;
202     memset(&temp_proc_desc, '\0', sizeof(temp_proc_desc));
203     memset(&temp_saved_regs, '\0', sizeof(struct frame_saved_regs));
204     PROC_LOW_ADDR(&temp_proc_desc) = start_pc;
205
206     if (start_pc + 200 < limit_pc) limit_pc = start_pc + 200;
207   restart:
208     frame_size = 0;
209     for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4) {
210         char buf[4];
211         unsigned long word;
212         int status;
213
214         status = read_memory_nobpt (cur_pc, buf, 4); 
215         if (status) memory_error (status, cur_pc);
216         word = extract_unsigned_integer (buf, 4);
217
218         if ((word & 0xFFFF0000) == 0x27bd0000) /* addiu $sp,$sp,-i */
219             frame_size += (-word) & 0xFFFF;
220         else if ((word & 0xFFFF0000) == 0x23bd0000) /* addu $sp,$sp,-i */
221             frame_size += (-word) & 0xFFFF;
222         else if ((word & 0xFFE00000) == 0xafa00000) { /* sw reg,offset($sp) */
223             int reg = (word & 0x001F0000) >> 16;
224             reg_mask |= 1 << reg;
225             temp_saved_regs.regs[reg] = sp + (word & 0xffff);
226         }
227         else if ((word & 0xFFFF0000) == 0x27be0000) { /* addiu $30,$sp,size */
228             if ((word & 0xffff) != frame_size)
229                 reg30 = sp + (word & 0xffff);
230             else if (!has_frame_reg) {
231                 int alloca_adjust;
232                 has_frame_reg = 1;
233                 reg30 = read_next_frame_reg(next_frame, 30);
234                 alloca_adjust = reg30 - (sp + (word & 0xffff));
235                 if (alloca_adjust > 0) {
236                     /* FP > SP + frame_size. This may be because
237                      * of an alloca or somethings similar.
238                      * Fix sp to "pre-alloca" value, and try again.
239                      */
240                     sp += alloca_adjust;
241                     goto restart;
242                 }
243             }
244         }
245         else if ((word & 0xFFE00000) == 0xafc00000) { /* sw reg,offset($30) */
246             int reg = (word & 0x001F0000) >> 16;
247             reg_mask |= 1 << reg;
248             temp_saved_regs.regs[reg] = reg30 + (word & 0xffff);
249         }
250     }
251     if (has_frame_reg) {
252         PROC_FRAME_REG(&temp_proc_desc) = 30;
253         PROC_FRAME_OFFSET(&temp_proc_desc) = 0;
254     }
255     else {
256         PROC_FRAME_REG(&temp_proc_desc) = SP_REGNUM;
257         PROC_FRAME_OFFSET(&temp_proc_desc) = frame_size;
258     }
259     PROC_REG_MASK(&temp_proc_desc) = reg_mask;
260     PROC_PC_REG(&temp_proc_desc) = RA_REGNUM;
261     return &temp_proc_desc;
262 }
263
264 static mips_extra_func_info_t
265 find_proc_desc(pc, next_frame)
266     CORE_ADDR pc;
267     FRAME next_frame;
268 {
269   mips_extra_func_info_t proc_desc;
270   struct block *b = block_for_pc(pc);
271   struct symbol *sym;
272   CORE_ADDR startaddr;
273
274   find_pc_partial_function (pc, NULL, &startaddr, NULL);
275   if (b == NULL)
276     sym = NULL;
277   else
278     {
279       if (startaddr > BLOCK_START (b))
280         /* This is the "pathological" case referred to in a comment in
281            print_frame_info.  It might be better to move this check into
282            symbol reading.  */
283         sym = NULL;
284       else
285         sym = lookup_symbol (MIPS_EFI_SYMBOL_NAME, b, LABEL_NAMESPACE,
286                              0, NULL);
287     }
288
289   if (sym)
290     {
291         /* IF this is the topmost frame AND
292          * (this proc does not have debugging information OR
293          * the PC is in the procedure prologue)
294          * THEN create a "heuristic" proc_desc (by analyzing
295          * the actual code) to replace the "official" proc_desc.
296          */
297         proc_desc = (mips_extra_func_info_t)SYMBOL_VALUE(sym);
298         if (next_frame == NULL) {
299             struct symtab_and_line val;
300             struct symbol *proc_symbol =
301                 PROC_DESC_IS_DUMMY(proc_desc) ? 0 : PROC_SYMBOL(proc_desc);
302
303             if (proc_symbol) {
304                 val = find_pc_line (BLOCK_START
305                                     (SYMBOL_BLOCK_VALUE(proc_symbol)),
306                                     0);
307                 val.pc = val.end ? val.end : pc;
308             }
309             if (!proc_symbol || pc < val.pc) {
310                 mips_extra_func_info_t found_heuristic =
311                     heuristic_proc_desc(PROC_LOW_ADDR(proc_desc),
312                                         pc, next_frame);
313                 if (found_heuristic) proc_desc = found_heuristic;
314             }
315         }
316     }
317   else
318     {
319       /* Is linked_proc_desc_table really necessary?  It only seems to be used
320          by procedure call dummys.  However, the procedures being called ought
321          to have their own proc_descs, and even if they don't,
322          heuristic_proc_desc knows how to create them! */
323
324       register struct linked_proc_info *link;
325       for (link = linked_proc_desc_table; link; link = link->next)
326           if (PROC_LOW_ADDR(&link->info) <= pc
327               && PROC_HIGH_ADDR(&link->info) > pc)
328               return &link->info;
329
330       if (startaddr == 0)
331         startaddr = heuristic_proc_start (pc);
332
333       proc_desc =
334         heuristic_proc_desc (startaddr, pc, next_frame);
335     }
336   return proc_desc;
337 }
338
339 mips_extra_func_info_t cached_proc_desc;
340
341 FRAME_ADDR
342 mips_frame_chain(frame)
343     FRAME frame;
344 {
345     mips_extra_func_info_t proc_desc;
346     CORE_ADDR saved_pc = FRAME_SAVED_PC(frame);
347
348     if (saved_pc == 0 || inside_entry_file (saved_pc))
349       return 0;
350
351     proc_desc = find_proc_desc(saved_pc, frame);
352     if (!proc_desc)
353       return 0;
354
355     cached_proc_desc = proc_desc;
356
357     /* If no frame pointer and frame size is zero, we must be at end
358        of stack (or otherwise hosed).  If we don't check frame size,
359        we loop forever if we see a zero size frame.  */
360     if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
361         && PROC_FRAME_OFFSET (proc_desc) == 0
362         /* The previous frame from a sigtramp frame might be frameless
363            and have frame size zero.  */
364         && !frame->signal_handler_caller)
365       return 0;
366     else
367       return read_next_frame_reg(frame, PROC_FRAME_REG(proc_desc))
368         + PROC_FRAME_OFFSET(proc_desc);
369 }
370
371 void
372 init_extra_frame_info(fci)
373      struct frame_info *fci;
374 {
375   extern struct obstack frame_cache_obstack;
376   /* Use proc_desc calculated in frame_chain */
377   mips_extra_func_info_t proc_desc =
378     fci->next ? cached_proc_desc : find_proc_desc(fci->pc, fci->next);
379
380   fci->saved_regs = (struct frame_saved_regs*)
381     obstack_alloc (&frame_cache_obstack, sizeof(struct frame_saved_regs));
382   memset (fci->saved_regs, 0, sizeof (struct frame_saved_regs));
383   fci->proc_desc =
384     proc_desc == &temp_proc_desc ? 0 : proc_desc;
385   if (proc_desc)
386     {
387       int ireg;
388       CORE_ADDR reg_position;
389       /* r0 bit means kernel trap */
390       int kernel_trap = PROC_REG_MASK(proc_desc) & 1;
391
392       /* Fixup frame-pointer - only needed for top frame */
393       /* This may not be quite right, if proc has a real frame register.
394          Get the value of the frame relative sp, procedure might have been
395          interrupted by a signal at it's very start.  */
396       if (fci->pc == PROC_LOW_ADDR(proc_desc) && !PROC_DESC_IS_DUMMY(proc_desc))
397         fci->frame = READ_FRAME_REG(fci, SP_REGNUM);
398       else
399         fci->frame = READ_FRAME_REG(fci, PROC_FRAME_REG(proc_desc))
400                       + PROC_FRAME_OFFSET(proc_desc);
401
402       if (proc_desc == &temp_proc_desc)
403         *fci->saved_regs = temp_saved_regs;
404       else
405         {
406           /* What registers have been saved?  Bitmasks.  */
407           unsigned long gen_mask, float_mask;
408
409           gen_mask = kernel_trap ? 0xFFFFFFFF : PROC_REG_MASK(proc_desc);
410           float_mask = kernel_trap ? 0xFFFFFFFF : PROC_FREG_MASK(proc_desc);
411
412           if (/* In any frame other than the innermost, we assume that all
413                  registers have been saved.  This assumes that all register
414                  saves in a function happen before the first function
415                  call.  */
416               fci->next == NULL
417
418               /* In a dummy frame we know exactly where things are saved.  */
419               && !PROC_DESC_IS_DUMMY (proc_desc)
420
421               /* Not sure exactly what kernel_trap means, but if it means
422                  the kernel saves the registers without a prologue doing it,
423                  we better not examine the prologue to see whether registers
424                  have been saved yet.  */
425               && !kernel_trap)
426             {
427               /* We need to figure out whether the registers that the proc_desc
428                  claims are saved have been saved yet.  */
429
430               CORE_ADDR addr;
431               int status;
432               char buf[4];
433               unsigned long inst;
434
435               /* Bitmasks; set if we have found a save for the register.  */
436               unsigned long gen_save_found = 0;
437               unsigned long float_save_found = 0;
438
439               for (addr = PROC_LOW_ADDR (proc_desc);
440                    addr < fci->pc && (gen_mask != gen_save_found
441                                       || float_mask != float_save_found);
442                    addr += 4)
443                 {
444                   status = read_memory_nobpt (addr, buf, 4);
445                   if (status)
446                     memory_error (status, addr);
447                   inst = extract_unsigned_integer (buf, 4);
448                   if (/* sw reg,n($sp) */
449                       (inst & 0xffe00000) == 0xafa00000
450
451                       /* sw reg,n($r30) */
452                       || (inst & 0xffe00000) == 0xafc00000)
453                     {
454                       /* It might be possible to use the instruction to
455                          find the offset, rather than the code below which
456                          is based on things being in a certain order in the
457                          frame, but figuring out what the instruction's offset
458                          is relative to might be a little tricky.  */
459                       int reg = (inst & 0x001f0000) >> 16;
460                       gen_save_found |= (1 << reg);
461                     }
462                   else if (/* swc1 freg,n($sp) */
463                            (inst & 0xffe00000) == 0xe7a00000
464
465                            /* swc1 freg,n($r30) */
466                            || (inst & 0xffe00000) == 0xe7c00000)
467                     {
468                       int reg = ((inst & 0x001f0000) >> 16);
469                       float_save_found |= (1 << reg);
470                     }
471                 }
472               gen_mask = gen_save_found;
473               float_mask = float_save_found;
474             }
475
476           /* Fill in the offsets for the registers which gen_mask says
477              were saved.  */
478           reg_position = fci->frame + PROC_REG_OFFSET (proc_desc);
479           for (ireg= 31; gen_mask; --ireg, gen_mask <<= 1)
480             if (gen_mask & 0x80000000)
481               {
482                 fci->saved_regs->regs[ireg] = reg_position;
483                 reg_position -= 4;
484               }
485           /* Fill in the offsets for the registers which float_mask says
486              were saved.  */
487           reg_position = fci->frame + PROC_FREG_OFFSET (proc_desc);
488
489           /* The freg_offset points to where the first *double* register
490              is saved.  So skip to the high-order word. */
491           reg_position += 4;
492           for (ireg = 31; float_mask; --ireg, float_mask <<= 1)
493             if (float_mask & 0x80000000)
494               {
495                 fci->saved_regs->regs[FP0_REGNUM+ireg] = reg_position;
496                 reg_position -= 4;
497               }
498         }
499
500       /* hack: if argument regs are saved, guess these contain args */
501       if ((PROC_REG_MASK(proc_desc) & 0xF0) == 0) fci->num_args = -1;
502       else if ((PROC_REG_MASK(proc_desc) & 0x80) == 0) fci->num_args = 4;
503       else if ((PROC_REG_MASK(proc_desc) & 0x40) == 0) fci->num_args = 3;
504       else if ((PROC_REG_MASK(proc_desc) & 0x20) == 0) fci->num_args = 2;
505       else if ((PROC_REG_MASK(proc_desc) & 0x10) == 0) fci->num_args = 1;
506
507       fci->saved_regs->regs[PC_REGNUM] = fci->saved_regs->regs[RA_REGNUM];
508     }
509 }
510
511 /* MIPS stack frames are almost impenetrable.  When execution stops,
512    we basically have to look at symbol information for the function
513    that we stopped in, which tells us *which* register (if any) is
514    the base of the frame pointer, and what offset from that register
515    the frame itself is at.  
516
517    This presents a problem when trying to examine a stack in memory
518    (that isn't executing at the moment), using the "frame" command.  We
519    don't have a PC, nor do we have any registers except SP.
520
521    This routine takes two arguments, SP and PC, and tries to make the
522    cached frames look as if these two arguments defined a frame on the
523    cache.  This allows the rest of info frame to extract the important
524    arguments without difficulty.  */
525
526 FRAME
527 setup_arbitrary_frame (argc, argv)
528      int argc;
529      FRAME_ADDR *argv;
530 {
531   if (argc != 2)
532     error ("MIPS frame specifications require two arguments: sp and pc");
533
534   return create_new_frame (argv[0], argv[1]);
535 }
536
537
538 CORE_ADDR
539 mips_push_arguments(nargs, args, sp, struct_return, struct_addr)
540   int nargs;
541   value *args;
542   CORE_ADDR sp;
543   int struct_return;
544   CORE_ADDR struct_addr;
545 {
546   register i;
547   int accumulate_size = struct_return ? 4 : 0;
548   struct mips_arg { char *contents; int len; int offset; };
549   struct mips_arg *mips_args =
550       (struct mips_arg*)alloca(nargs * sizeof(struct mips_arg));
551   register struct mips_arg *m_arg;
552   for (i = 0, m_arg = mips_args; i < nargs; i++, m_arg++) {
553     extern value value_arg_coerce();
554     value arg = value_arg_coerce (args[i]);
555     m_arg->len = TYPE_LENGTH (VALUE_TYPE (arg));
556     /* This entire mips-specific routine is because doubles must be aligned
557      * on 8-byte boundaries. It still isn't quite right, because MIPS decided
558      * to align 'struct {int a, b}' on 4-byte boundaries (even though this
559      * breaks their varargs implementation...). A correct solution
560      * requires an simulation of gcc's 'alignof' (and use of 'alignof'
561      * in stdarg.h/varargs.h).
562      */
563     if (m_arg->len > 4) accumulate_size = (accumulate_size + 7) & -8;
564     m_arg->offset = accumulate_size;
565     accumulate_size = (accumulate_size + m_arg->len + 3) & -4;
566     m_arg->contents = VALUE_CONTENTS(arg);
567   }
568   accumulate_size = (accumulate_size + 7) & (-8);
569   if (accumulate_size < 16) accumulate_size = 16; 
570   sp -= accumulate_size;
571   for (i = nargs; m_arg--, --i >= 0; )
572     write_memory(sp + m_arg->offset, m_arg->contents, m_arg->len);
573   if (struct_return)
574     {
575       char buf[TARGET_PTR_BIT / HOST_CHAR_BIT];
576
577       store_address (buf, sizeof buf, struct_addr);
578       write_memory (sp, buf, sizeof buf);
579     }
580   return sp;
581 }
582
583 /* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<31. */
584 #define MASK(i,j) (((1 << ((j)+1))-1) ^ ((1 << (i))-1))
585
586 void
587 mips_push_dummy_frame()
588 {
589   char buffer[MAX_REGISTER_RAW_SIZE];
590   int ireg;
591   struct linked_proc_info *link = (struct linked_proc_info*)
592       xmalloc(sizeof(struct linked_proc_info));
593   mips_extra_func_info_t proc_desc = &link->info;
594   CORE_ADDR sp = read_register (SP_REGNUM);
595   CORE_ADDR save_address;
596   link->next = linked_proc_desc_table;
597   linked_proc_desc_table = link;
598 #define PUSH_FP_REGNUM 16 /* must be a register preserved across calls */
599 #define GEN_REG_SAVE_MASK MASK(1,16)|MASK(24,28)|(1<<31)
600 #define GEN_REG_SAVE_COUNT 22
601 #define FLOAT_REG_SAVE_MASK MASK(0,19)
602 #define FLOAT_REG_SAVE_COUNT 20
603 #define SPECIAL_REG_SAVE_COUNT 4
604   /*
605    * The registers we must save are all those not preserved across
606    * procedure calls. Dest_Reg (see tm-mips.h) must also be saved.
607    * In addition, we must save the PC, and PUSH_FP_REGNUM.
608    * (Ideally, we should also save MDLO/-HI and FP Control/Status reg.)
609    *
610    * Dummy frame layout:
611    *  (high memory)
612    *    Saved PC
613    *    Saved MMHI, MMLO, FPC_CSR
614    *    Saved R31
615    *    Saved R28
616    *    ...
617    *    Saved R1
618    *    Saved D18 (i.e. F19, F18)
619    *    ...
620    *    Saved D0 (i.e. F1, F0)
621    *    CALL_DUMMY (subroutine stub; see tm-mips.h)
622    *    Parameter build area (not yet implemented)
623    *  (low memory)
624    */
625   PROC_REG_MASK(proc_desc) = GEN_REG_SAVE_MASK;
626   PROC_FREG_MASK(proc_desc) = mips_fpu ? FLOAT_REG_SAVE_MASK : 0;
627   PROC_REG_OFFSET(proc_desc) = /* offset of (Saved R31) from FP */
628       -sizeof(long) - 4 * SPECIAL_REG_SAVE_COUNT;
629   PROC_FREG_OFFSET(proc_desc) = /* offset of (Saved D18) from FP */
630       -sizeof(double) - 4 * (SPECIAL_REG_SAVE_COUNT + GEN_REG_SAVE_COUNT);
631   /* save general registers */
632   save_address = sp + PROC_REG_OFFSET(proc_desc);
633   for (ireg = 32; --ireg >= 0; )
634     if (PROC_REG_MASK(proc_desc) & (1 << ireg))
635       {
636         store_unsigned_integer (buffer, REGISTER_RAW_SIZE (ireg),
637                                 read_register (ireg));
638         write_memory (save_address, buffer, REGISTER_RAW_SIZE (ireg));
639         save_address -= 4;
640       }
641   /* save floating-points registers starting with high order word */
642   save_address = sp + PROC_FREG_OFFSET(proc_desc) + 4;
643   for (ireg = 32; --ireg >= 0; )
644     if (PROC_FREG_MASK(proc_desc) & (1 << ireg))
645       {
646         store_unsigned_integer (buffer, 4, read_register (ireg + FP0_REGNUM));
647         write_memory (save_address, buffer, 4);
648         save_address -= 4;
649       }
650   write_register (PUSH_FP_REGNUM, sp);
651   PROC_FRAME_REG(proc_desc) = PUSH_FP_REGNUM;
652   PROC_FRAME_OFFSET(proc_desc) = 0;
653   store_unsigned_integer (buffer, REGISTER_RAW_SIZE (PC_REGNUM),
654                           read_register (PC_REGNUM));
655   write_memory (sp - 4, buffer, REGISTER_RAW_SIZE (PC_REGNUM));
656   store_unsigned_integer (buffer, REGISTER_RAW_SIZE (HI_REGNUM),
657                           read_register (HI_REGNUM));
658   write_memory (sp - 8, buffer, REGISTER_RAW_SIZE (HI_REGNUM));
659   store_unsigned_integer (buffer, REGISTER_RAW_SIZE (LO_REGNUM),
660                           read_register (LO_REGNUM));
661   write_memory (sp - 12, buffer, REGISTER_RAW_SIZE (LO_REGNUM));
662   store_unsigned_integer
663     (buffer,
664      REGISTER_RAW_SIZE (FCRCS_REGNUM),
665      mips_fpu ? read_register (FCRCS_REGNUM) : 0);
666   write_memory (sp - 16, buffer, REGISTER_RAW_SIZE (FCRCS_REGNUM));
667   sp -= 4 * (GEN_REG_SAVE_COUNT
668              + (mips_fpu ? FLOAT_REG_SAVE_COUNT : 0)
669              + SPECIAL_REG_SAVE_COUNT);
670   write_register (SP_REGNUM, sp);
671   PROC_LOW_ADDR(proc_desc) = sp - CALL_DUMMY_SIZE + CALL_DUMMY_START_OFFSET;
672   PROC_HIGH_ADDR(proc_desc) = sp;
673   SET_PROC_DESC_IS_DUMMY(proc_desc);
674   PROC_PC_REG(proc_desc) = RA_REGNUM;
675 }
676
677 void
678 mips_pop_frame()
679 {
680   register int regnum;
681   FRAME frame = get_current_frame ();
682   CORE_ADDR new_sp = frame->frame;
683
684   mips_extra_func_info_t proc_desc = frame->proc_desc;
685
686   write_register (PC_REGNUM, FRAME_SAVED_PC(frame));
687   if (proc_desc)
688     {
689       for (regnum = 32; --regnum >= 0; )
690         if (PROC_REG_MASK(proc_desc) & (1 << regnum))
691           write_register (regnum,
692                           read_memory_integer (frame->saved_regs->regs[regnum],
693                                                4));
694       for (regnum = 32; --regnum >= 0; )
695         if (PROC_FREG_MASK(proc_desc) & (1 << regnum))
696           write_register (regnum + FP0_REGNUM,
697                           read_memory_integer (frame->saved_regs->regs[regnum + FP0_REGNUM], 4));
698     }
699   write_register (SP_REGNUM, new_sp);
700   flush_cached_frames ();
701   /* We let mips_init_extra_frame_info figure out the frame pointer */
702   set_current_frame (create_new_frame (0, read_pc ()));
703
704   if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
705     {
706       struct linked_proc_info *pi_ptr, *prev_ptr;
707
708       for (pi_ptr = linked_proc_desc_table, prev_ptr = NULL;
709            pi_ptr != NULL;
710            prev_ptr = pi_ptr, pi_ptr = pi_ptr->next)
711         {
712           if (&pi_ptr->info == proc_desc)
713             break;
714         }
715
716       if (pi_ptr == NULL)
717         error ("Can't locate dummy extra frame info\n");
718
719       if (prev_ptr != NULL)
720         prev_ptr->next = pi_ptr->next;
721       else
722         linked_proc_desc_table = pi_ptr->next;
723
724       free (pi_ptr);
725
726       write_register (HI_REGNUM, read_memory_integer(new_sp - 8, 4));
727       write_register (LO_REGNUM, read_memory_integer(new_sp - 12, 4));
728       if (mips_fpu)
729         write_register (FCRCS_REGNUM, read_memory_integer(new_sp - 16, 4));
730     }
731 }
732
733 static void
734 mips_print_register (regnum, all)
735      int regnum, all;
736 {
737   unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
738
739   /* Get the data in raw format.  */
740   if (read_relative_register_raw_bytes (regnum, raw_buffer))
741     {
742       printf_filtered ("%s: [Invalid]", reg_names[regnum]);
743       return;
744     }
745
746   /* If an even floating pointer register, also print as double. */
747   if (regnum >= FP0_REGNUM && regnum < FP0_REGNUM+32
748       && !((regnum-FP0_REGNUM) & 1)) {
749     char dbuffer[MAX_REGISTER_RAW_SIZE]; 
750
751     read_relative_register_raw_bytes (regnum, dbuffer);
752     read_relative_register_raw_bytes (regnum+1, dbuffer+4);
753 #ifdef REGISTER_CONVERT_TO_TYPE
754     REGISTER_CONVERT_TO_TYPE(regnum, builtin_type_double, dbuffer);
755 #endif
756     printf_filtered ("(d%d: ", regnum-FP0_REGNUM);
757     val_print (builtin_type_double, dbuffer, 0,
758                gdb_stdout, 0, 1, 0, Val_pretty_default);
759     printf_filtered ("); ");
760   }
761   fputs_filtered (reg_names[regnum], gdb_stdout);
762
763   /* The problem with printing numeric register names (r26, etc.) is that
764      the user can't use them on input.  Probably the best solution is to
765      fix it so that either the numeric or the funky (a2, etc.) names
766      are accepted on input.  */
767   if (regnum < 32)
768     printf_filtered ("(r%d): ", regnum);
769   else
770     printf_filtered (": ");
771
772   /* If virtual format is floating, print it that way.  */
773   if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT
774       && ! INVALID_FLOAT (raw_buffer, REGISTER_VIRTUAL_SIZE(regnum))) {
775     val_print (REGISTER_VIRTUAL_TYPE (regnum), raw_buffer, 0,
776                gdb_stdout, 0, 1, 0, Val_pretty_default);
777   }
778   /* Else print as integer in hex.  */
779   else
780     {
781       long val;
782
783       val = extract_signed_integer (raw_buffer,
784                                     REGISTER_RAW_SIZE (regnum));
785
786       if (val == 0)
787         printf_filtered ("0");
788       else if (all)
789         /* FIXME: We should be printing this in a fixed field width, so that
790            registers line up.  */
791         printf_filtered (local_hex_format(), val);
792       else
793         printf_filtered ("%s=%ld", local_hex_string(val), val);
794     }
795 }
796
797 /* Replacement for generic do_registers_info.  */
798 void
799 mips_do_registers_info (regnum, fpregs)
800      int regnum;
801      int fpregs;
802 {
803   if (regnum != -1) {
804       mips_print_register (regnum, 0);
805       printf_filtered ("\n");
806   }
807   else {
808       for (regnum = 0; regnum < NUM_REGS; ) {
809           if ((!fpregs) && regnum >= FP0_REGNUM && regnum <= FCRIR_REGNUM) {
810             regnum++;
811             continue;
812           }
813           mips_print_register (regnum, 1);
814           regnum++;
815           if ((regnum & 3) == 0 || regnum == NUM_REGS)
816               printf_filtered (";\n");
817           else
818               printf_filtered ("; ");
819       }
820   }
821 }
822 /* Return number of args passed to a frame. described by FIP.
823    Can return -1, meaning no way to tell.  */
824
825 int
826 mips_frame_num_args(fip)
827         FRAME fip;
828 {
829 #if 0
830         struct chain_info_t *p;
831
832         p = mips_find_cached_frame(FRAME_FP(fip));
833         if (p->valid)
834                 return p->the_info.numargs;
835 #endif
836         return -1;
837 }
838 \f
839 #if 0
840 /* Is this a branch with a delay slot?  */
841 static int
842 is_delayed (insn)
843      unsigned long insn;
844 {
845   int i;
846   for (i = 0; i < NUMOPCODES; ++i)
847     if (mips_opcodes[i].pinfo != INSN_MACRO
848         && (insn & mips_opcodes[i].mask) == mips_opcodes[i].match)
849       break;
850   return (i < NUMOPCODES
851           && (mips_opcodes[i].pinfo & (INSN_UNCOND_BRANCH_DELAY
852                                        | INSN_COND_BRANCH_DELAY
853                                        | INSN_COND_BRANCH_LIKELY)));
854 }
855 #endif
856
857 /* To skip prologues, I use this predicate.  Returns either PC itself
858    if the code at PC does not look like a function prologue; otherwise
859    returns an address that (if we're lucky) follows the prologue.  If
860    LENIENT, then we must skip everything which is involved in setting
861    up the frame (it's OK to skip more, just so long as we don't skip
862    anything which might clobber the registers which are being saved.
863    We must skip more in the case where part of the prologue is in the
864    delay slot of a non-prologue instruction).  */
865
866 CORE_ADDR
867 mips_skip_prologue (pc, lenient)
868      CORE_ADDR pc;
869      int lenient;
870 {
871     unsigned long inst;
872     int offset;
873     int seen_sp_adjust = 0;
874
875     /* Skip the typical prologue instructions. These are the stack adjustment
876        instruction and the instructions that save registers on the stack
877        or in the gcc frame.  */
878     for (offset = 0; offset < 100; offset += 4)
879       {
880         char buf[4];
881         int status;
882
883         status = read_memory_nobpt (pc + offset, buf, 4);
884         if (status)
885           memory_error (status, pc + offset);
886         inst = extract_unsigned_integer (buf, 4);
887
888 #if 0
889         if (lenient && is_delayed (inst))
890           continue;
891 #endif
892
893         if ((inst & 0xffff0000) == 0x27bd0000)  /* addiu $sp,$sp,offset */
894             seen_sp_adjust = 1;
895         else if ((inst & 0xFFE00000) == 0xAFA00000 && (inst & 0x001F0000))
896             continue;                           /* sw reg,n($sp) */
897                                                 /* reg != $zero */
898         else if ((inst & 0xFFE00000) == 0xE7A00000) /* swc1 freg,n($sp) */
899             continue;
900         else if ((inst & 0xF3E00000) == 0xA3C00000 && (inst & 0x001F0000))
901                                                 /* sx reg,n($s8) */
902             continue;                           /* reg != $zero */
903         else if (inst == 0x03A0F021)            /* move $s8,$sp */
904             continue;
905         else if ((inst & 0xFF9F07FF) == 0x00800021) /* move reg,$a0-$a3 */
906             continue;
907         else if ((inst & 0xffff0000) == 0x3c1c0000) /* lui $gp,n */
908             continue;
909         else if ((inst & 0xffff0000) == 0x279c0000) /* addiu $gp,$gp,n */
910             continue;
911         else if (inst == 0x0399e021             /* addu $gp,$gp,$t9 */
912                  || inst == 0x033ce021)         /* addu $gp,$t9,$gp */
913           continue;
914         else
915             break;
916     }
917     return pc + offset;
918
919 /* FIXME schauer. The following code seems no longer necessary if we
920    always skip the typical prologue instructions.  */
921
922 #if 0
923     if (seen_sp_adjust)
924       return pc + offset;
925
926     /* Well, it looks like a frameless. Let's make sure.
927        Note that we are not called on the current PC,
928        but on the function`s start PC, and I have definitely
929        seen optimized code that adjusts the SP quite later */
930     b = block_for_pc(pc);
931     if (!b) return pc;
932
933     f = lookup_symbol(MIPS_EFI_SYMBOL_NAME, b, LABEL_NAMESPACE, 0, NULL);
934     if (!f) return pc;
935     /* Ideally, I would like to use the adjusted info
936        from mips_frame_info(), but for all practical
937        purposes it will not matter (and it would require
938        a different definition of SKIP_PROLOGUE())
939
940        Actually, it would not hurt to skip the storing
941        of arguments on the stack as well. */
942     if (((mips_extra_func_info_t)SYMBOL_VALUE(f))->pdr.frameoffset)
943         return pc + 4;
944
945     return pc;
946 #endif
947 }
948
949 #if 0
950 /* The lenient prologue stuff should be superceded by the code in
951    init_extra_frame_info which looks to see whether the stores mentioned
952    in the proc_desc have actually taken place.  */
953
954 /* Is address PC in the prologue (loosely defined) for function at
955    STARTADDR?  */
956
957 static int
958 mips_in_lenient_prologue (startaddr, pc)
959      CORE_ADDR startaddr;
960      CORE_ADDR pc;
961 {
962   CORE_ADDR end_prologue = mips_skip_prologue (startaddr, 1);
963   return pc >= startaddr && pc < end_prologue;
964 }
965 #endif
966
967 /* Given a return value in `regbuf' with a type `valtype', 
968    extract and copy its value into `valbuf'.  */
969 void
970 mips_extract_return_value (valtype, regbuf, valbuf)
971     struct type *valtype;
972     char regbuf[REGISTER_BYTES];
973     char *valbuf;
974 {
975   int regnum;
976   
977   regnum = TYPE_CODE (valtype) == TYPE_CODE_FLT && mips_fpu ? FP0_REGNUM : 2;
978
979   memcpy (valbuf, regbuf + REGISTER_BYTE (regnum), TYPE_LENGTH (valtype));
980 #ifdef REGISTER_CONVERT_TO_TYPE
981   REGISTER_CONVERT_TO_TYPE(regnum, valtype, valbuf);
982 #endif
983 }
984
985 /* Given a return value in `regbuf' with a type `valtype', 
986    write it's value into the appropriate register.  */
987 void
988 mips_store_return_value (valtype, valbuf)
989     struct type *valtype;
990     char *valbuf;
991 {
992   int regnum;
993   char raw_buffer[MAX_REGISTER_RAW_SIZE];
994   
995   regnum = TYPE_CODE (valtype) == TYPE_CODE_FLT && mips_fpu ? FP0_REGNUM : 2;
996   memcpy(raw_buffer, valbuf, TYPE_LENGTH (valtype));
997
998 #ifdef REGISTER_CONVERT_FROM_TYPE
999   REGISTER_CONVERT_FROM_TYPE(regnum, valtype, raw_buffer);
1000 #endif
1001
1002   write_register_bytes(REGISTER_BYTE (regnum), raw_buffer, TYPE_LENGTH (valtype));
1003 }
1004
1005 /* These exist in mdebugread.c.  */
1006 extern CORE_ADDR sigtramp_address, sigtramp_end;
1007 extern void fixup_sigtramp PARAMS ((void));
1008
1009 /* Exported procedure: Is PC in the signal trampoline code */
1010
1011 int
1012 in_sigtramp (pc, ignore)
1013      CORE_ADDR pc;
1014      char *ignore;              /* function name */
1015 {
1016   if (sigtramp_address == 0)
1017     fixup_sigtramp ();
1018   return (pc >= sigtramp_address && pc < sigtramp_end);
1019 }
1020
1021 static void reinit_frame_cache_sfunc PARAMS ((char *, int,
1022                                               struct cmd_list_element *));
1023
1024 /* Just like reinit_frame_cache, but with the right arguments to be
1025    callable as an sfunc.  */
1026 static void
1027 reinit_frame_cache_sfunc (args, from_tty, c)
1028      char *args;
1029      int from_tty;
1030      struct cmd_list_element *c;
1031 {
1032   reinit_frame_cache ();
1033 }
1034
1035 void
1036 _initialize_mips_tdep ()
1037 {
1038   struct cmd_list_element *c;
1039
1040   /* Let the user turn off floating point and set the fence post for
1041      heuristic_proc_start.  */
1042
1043   add_show_from_set
1044     (add_set_cmd ("mipsfpu", class_support, var_boolean,
1045                   (char *) &mips_fpu,
1046                   "Set use of floating point coprocessor.\n\
1047 Turn off to avoid using floating point instructions when calling functions\n\
1048 or dealing with return values.", &setlist),
1049      &showlist);
1050
1051   /* We really would like to have both "0" and "unlimited" work, but
1052      command.c doesn't deal with that.  So make it a var_zinteger
1053      because the user can always use "999999" or some such for unlimited.  */
1054   c = add_set_cmd ("heuristic-fence-post", class_support, var_zinteger,
1055                    (char *) &heuristic_fence_post,
1056                    "\
1057 Set the distance searched for the start of a function.\n\
1058 If you are debugging a stripped executable, GDB needs to search through the\n\
1059 program for the start of a function.  This command sets the distance of the\n\
1060 search.  The only need to set it is when debugging a stripped executable.",
1061                    &setlist);
1062   /* We need to throw away the frame cache when we set this, since it
1063      might change our ability to get backtraces.  */
1064   c->function.sfunc = reinit_frame_cache_sfunc;
1065   add_show_from_set (c, &showlist);
1066 }
This page took 0.086473 seconds and 4 git commands to generate.