OSDN Git Service

* libc/posix/execvp.c (execvp): Check path for
[pf3gnuchains/pf3gnuchains4x.git] / gdb / fr30-tdep.c
1 /* Target-dependent code for the Fujitsu FR30.
2    Copyright 1999, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "obstack.h"
24 #include "target.h"
25 #include "value.h"
26 #include "bfd.h"
27 #include "gdb_string.h"
28 #include "gdbcore.h"
29 #include "symfile.h"
30
31 /* Function: pop_frame
32    This routine gets called when either the user uses the `return'
33    command, or the call dummy breakpoint gets hit.  */
34
35 void
36 fr30_pop_frame ()
37 {
38   struct frame_info *frame = get_current_frame();
39   int regnum;
40   CORE_ADDR sp = read_register(SP_REGNUM);
41
42   if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame))
43     generic_pop_dummy_frame ();
44   else
45     {
46       write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
47
48       for (regnum = 0; regnum < NUM_REGS; regnum++)
49         if (frame->fsr.regs[regnum] != 0) {
50           write_register (regnum,
51                   read_memory_unsigned_integer (frame->fsr.regs[regnum],
52                         REGISTER_RAW_SIZE(regnum)));
53         }
54       write_register (SP_REGNUM, sp + frame->framesize);
55     }
56   flush_cached_frames ();
57 }
58
59 /* Function: skip_prologue
60    Return the address of the first code past the prologue of the function.  */
61
62 CORE_ADDR
63 fr30_skip_prologue(CORE_ADDR pc)
64 {
65   CORE_ADDR func_addr, func_end;
66
67   /* See what the symbol table says */
68
69   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
70     {
71       struct symtab_and_line sal;
72
73       sal = find_pc_line (func_addr, 0);
74
75       if (sal.line != 0 && sal.end < func_end) {
76         return sal.end;
77         }
78     }
79
80 /* Either we didn't find the start of this function (nothing we can do),
81    or there's no line info, or the line after the prologue is after
82    the end of the function (there probably isn't a prologue). */
83
84   return pc;
85 }
86
87
88 /* Function: push_arguments
89    Setup arguments and RP for a call to the target.  First four args
90    go in FIRST_ARGREG -> LAST_ARGREG, subsequent args go on stack...
91    Structs are passed by reference.  XXX not right now Z.R.
92    64 bit quantities (doubles and long longs) may be split between
93    the regs and the stack.
94    When calling a function that returns a struct, a pointer to the struct
95    is passed in as a secret first argument (always in FIRST_ARGREG).
96
97    Stack space for the args has NOT been allocated: that job is up to us.
98 */
99
100 CORE_ADDR
101 fr30_push_arguments(nargs, args, sp, struct_return, struct_addr)
102      int         nargs;
103      value_ptr * args;
104      CORE_ADDR   sp;
105      int         struct_return;
106      CORE_ADDR   struct_addr;
107 {
108   int argreg;
109   int argnum;
110   int stack_offset;
111   struct stack_arg {
112       char *val;
113       int len;
114       int offset;
115     };
116   struct stack_arg *stack_args =
117       (struct stack_arg*)alloca (nargs * sizeof (struct stack_arg));
118   int nstack_args = 0;
119
120   argreg = FIRST_ARGREG;
121
122   /* the struct_return pointer occupies the first parameter-passing reg */
123   if (struct_return)
124       write_register (argreg++, struct_addr);
125
126   stack_offset = 0;
127
128   /* Process args from left to right.  Store as many as allowed in
129         registers, save the rest to be pushed on the stack */
130   for(argnum = 0; argnum < nargs; argnum++)
131     {
132       char *         val;
133       value_ptr      arg = args[argnum];
134       struct type *  arg_type = check_typedef (VALUE_TYPE (arg));
135       struct type *  target_type = TYPE_TARGET_TYPE (arg_type);
136       int            len = TYPE_LENGTH (arg_type);
137       enum type_code typecode = TYPE_CODE (arg_type);
138       CORE_ADDR      regval;
139       int newarg;
140
141       val = (char *) VALUE_CONTENTS (arg);
142
143         {
144           /* Copy the argument to general registers or the stack in
145              register-sized pieces.  Large arguments are split between
146              registers and stack.  */
147           while (len > 0)
148             {
149               if (argreg <= LAST_ARGREG)
150                 {
151                   int partial_len = len < REGISTER_SIZE ? len : REGISTER_SIZE;
152                   regval = extract_address (val, partial_len);
153
154                   /* It's a simple argument being passed in a general
155                      register.  */
156                   write_register (argreg, regval);
157                   argreg++;
158                   len -= partial_len;
159                   val += partial_len;
160                 }
161               else
162                 {
163                   /* keep for later pushing */
164                   stack_args[nstack_args].val = val;
165                   stack_args[nstack_args++].len = len;
166                   break;
167                 }
168             }
169         }
170     }
171     /* now do the real stack pushing, process args right to left */
172     while(nstack_args--)
173       {
174         sp -= stack_args[nstack_args].len;
175         write_memory(sp, stack_args[nstack_args].val,
176                 stack_args[nstack_args].len);
177       }
178
179   /* Return adjusted stack pointer.  */
180   return sp;
181 }
182
183 _initialize_fr30_tdep()
184 {
185         extern int print_insn_fr30(bfd_vma, disassemble_info *);
186
187         tm_print_insn = print_insn_fr30;
188 }
189
190 /* Function: check_prologue_cache
191    Check if prologue for this frame's PC has already been scanned.
192    If it has, copy the relevant information about that prologue and
193    return non-zero.  Otherwise do not copy anything and return zero.
194
195    The information saved in the cache includes:
196      * the frame register number;
197      * the size of the stack frame;
198      * the offsets of saved regs (relative to the old SP); and
199      * the offset from the stack pointer to the frame pointer
200
201    The cache contains only one entry, since this is adequate
202    for the typical sequence of prologue scan requests we get.
203    When performing a backtrace, GDB will usually ask to scan
204    the same function twice in a row (once to get the frame chain,
205    and once to fill in the extra frame information).
206 */
207
208 static struct frame_info prologue_cache;
209
210 static int
211 check_prologue_cache (fi)
212      struct frame_info * fi;
213 {
214   int i;
215
216   if (fi->pc == prologue_cache.pc)
217     {
218       fi->framereg = prologue_cache.framereg;
219       fi->framesize = prologue_cache.framesize;
220       fi->frameoffset = prologue_cache.frameoffset;
221       for (i = 0; i <= NUM_REGS; i++)
222         fi->fsr.regs[i] = prologue_cache.fsr.regs[i];
223       return 1;
224     }
225   else
226     return 0;
227 }
228
229
230 /* Function: save_prologue_cache
231    Copy the prologue information from fi to the prologue cache.
232 */
233
234 static void
235 save_prologue_cache (fi)
236      struct frame_info * fi;
237 {
238   int i;
239
240   prologue_cache.pc          = fi->pc;
241   prologue_cache.framereg    = fi->framereg;
242   prologue_cache.framesize   = fi->framesize;
243   prologue_cache.frameoffset = fi->frameoffset;
244   
245   for (i = 0; i <= NUM_REGS; i++) {
246     prologue_cache.fsr.regs[i] = fi->fsr.regs[i];
247   }
248 }
249
250
251 /* Function: scan_prologue
252    Scan the prologue of the function that contains PC, and record what
253    we find in PI.  PI->fsr must be zeroed by the called.  Returns the
254    pc after the prologue.  Note that the addresses saved in pi->fsr
255    are actually just frame relative (negative offsets from the frame
256    pointer).  This is because we don't know the actual value of the
257    frame pointer yet.  In some circumstances, the frame pointer can't
258    be determined till after we have scanned the prologue.  */
259
260 static void
261 fr30_scan_prologue (fi)
262      struct frame_info * fi;
263 {
264   int sp_offset, fp_offset;
265   CORE_ADDR prologue_start, prologue_end, current_pc;
266
267   /* Check if this function is already in the cache of frame information. */
268   if (check_prologue_cache (fi))
269     return;
270
271   /* Assume there is no frame until proven otherwise.  */
272   fi->framereg    = SP_REGNUM;
273   fi->framesize   = 0;
274   fi->frameoffset = 0;
275
276   /* Find the function prologue.  If we can't find the function in
277      the symbol table, peek in the stack frame to find the PC.  */
278   if (find_pc_partial_function (fi->pc, NULL, &prologue_start, &prologue_end))
279     {
280       /* Assume the prologue is everything between the first instruction
281          in the function and the first source line.  */
282       struct symtab_and_line sal = find_pc_line (prologue_start, 0);
283
284       if (sal.line == 0)                /* no line info, use current PC */
285         prologue_end = fi->pc;
286       else if (sal.end < prologue_end)  /* next line begins after fn end */
287         prologue_end = sal.end;         /* (probably means no prologue)  */
288     }
289   else
290     {
291       /* XXX Z.R. What now??? The following is entirely bogus */
292       prologue_start = (read_memory_integer (fi->frame, 4) & 0x03fffffc) - 12;
293       prologue_end = prologue_start + 40;
294     }
295
296   /* Now search the prologue looking for instructions that set up the
297      frame pointer, adjust the stack pointer, and save registers.  */
298
299   sp_offset = fp_offset = 0;
300   for (current_pc = prologue_start; current_pc < prologue_end; current_pc += 2)
301     {
302       unsigned int insn;
303
304       insn = read_memory_unsigned_integer (current_pc, 2);
305
306       if ((insn & 0xfe00) == 0x8e00)                    /* stm0 or stm1 */
307         {
308           int reg, mask = insn & 0xff;
309
310           /* scan in one sweep - create virtual 16-bit mask from either insn's mask */
311           if((insn & 0x0100) == 0)
312             {
313               mask <<= 8;  /* stm0 - move to upper byte in virtual mask */
314             }
315
316           /* Calculate offsets of saved registers (to be turned later into addresses). */
317           for (reg = R4_REGNUM; reg <= R11_REGNUM; reg++)
318             if (mask & (1 << (15 - reg)))
319               {
320                 sp_offset -= 4;
321                 fi->fsr.regs[reg] = sp_offset;
322               }
323         }
324       else if((insn & 0xfff0) == 0x1700)                /* st rx,@-r15 */
325         {
326           int reg = insn & 0xf;
327
328           sp_offset -= 4;
329           fi->fsr.regs[reg] = sp_offset;
330         }
331       else if((insn & 0xff00) == 0x0f00)                /* enter */
332         {
333           fp_offset = fi->fsr.regs[FP_REGNUM] = sp_offset - 4;
334           sp_offset -= 4 * (insn & 0xff);
335           fi->framereg = FP_REGNUM;
336         }
337       else if(insn == 0x1781)                           /* st rp,@-sp */
338         {
339                 sp_offset -= 4;
340                 fi->fsr.regs[RP_REGNUM] = sp_offset;
341         }
342       else if(insn == 0x170e)                           /* st fp,@-sp */
343         {
344                 sp_offset -= 4;
345                 fi->fsr.regs[FP_REGNUM] = sp_offset;
346         }
347       else if(insn == 0x8bfe)                           /* mov sp,fp */
348         {
349           fi->framereg = FP_REGNUM;
350         }
351       else if((insn & 0xff00) == 0xa300)                /* addsp xx */
352         {
353           sp_offset += 4 * (signed char)(insn & 0xff);
354         }
355       else if((insn & 0xff0f) == 0x9b00 &&              /* ldi:20 xx,r0 */
356         read_memory_unsigned_integer(current_pc+4, 2)
357           == 0xac0f)                                    /* sub r0,sp */
358         {
359           /* large stack adjustment */
360           sp_offset -= (((insn & 0xf0) << 12) | read_memory_unsigned_integer(current_pc+2, 2));
361           current_pc += 4;
362         }
363       else if(insn == 0x9f80 &&                         /* ldi:32 xx,r0 */
364         read_memory_unsigned_integer(current_pc+6, 2)
365           == 0xac0f)                                    /* sub r0,sp */
366         {
367           /* large stack adjustment */
368           sp_offset -=
369                 (read_memory_unsigned_integer(current_pc+2, 2) << 16 |
370                         read_memory_unsigned_integer(current_pc+4, 2));
371           current_pc += 6;
372         }
373     }
374
375   /* The frame size is just the negative of the offset (from the original SP)
376      of the last thing thing we pushed on the stack.  The frame offset is
377      [new FP] - [new SP].  */
378   fi->framesize = -sp_offset;
379   fi->frameoffset = fp_offset - sp_offset;
380   
381   save_prologue_cache (fi);
382 }
383
384 /* Function: init_extra_frame_info
385    Setup the frame's frame pointer, pc, and frame addresses for saved
386    registers.  Most of the work is done in scan_prologue().
387
388    Note that when we are called for the last frame (currently active frame),
389    that fi->pc and fi->frame will already be setup.  However, fi->frame will
390    be valid only if this routine uses FP.  For previous frames, fi-frame will
391    always be correct (since that is derived from fr30_frame_chain ()).
392
393    We can be called with the PC in the call dummy under two circumstances.
394    First, during normal backtracing, second, while figuring out the frame
395    pointer just prior to calling the target function (see run_stack_dummy).  */
396
397 void
398 fr30_init_extra_frame_info (fi)
399      struct frame_info * fi;
400 {
401   int reg;
402
403   if (fi->next)
404     fi->pc = FRAME_SAVED_PC (fi->next);
405
406   memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
407
408   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
409     {
410       /* We need to setup fi->frame here because run_stack_dummy gets it wrong
411          by assuming it's always FP.  */
412       fi->frame       = generic_read_register_dummy (fi->pc, fi->frame, SP_REGNUM);
413       fi->framesize   = 0;
414       fi->frameoffset = 0;
415       return;
416     }
417       fr30_scan_prologue (fi);
418
419       if (!fi->next)                    /* this is the innermost frame? */
420         fi->frame = read_register (fi->framereg);
421       else                              /* not the innermost frame */
422         /* If we have an FP,  the callee saved it. */
423         if (fi->framereg == FP_REGNUM)
424           if (fi->next->fsr.regs[fi->framereg] != 0)
425             fi->frame = read_memory_integer (fi->next->fsr.regs[fi->framereg],
426                                              4);
427       /* Calculate actual addresses of saved registers using offsets determined
428          by fr30_scan_prologue.  */
429       for (reg = 0; reg < NUM_REGS; reg++)
430         if (fi->fsr.regs[reg] != 0) {
431           fi->fsr.regs[reg] += fi->frame + fi->framesize - fi->frameoffset;
432         }
433 }
434
435 /* Function: find_callers_reg
436    Find REGNUM on the stack.  Otherwise, it's in an active register.
437    One thing we might want to do here is to check REGNUM against the
438    clobber mask, and somehow flag it as invalid if it isn't saved on
439    the stack somewhere.  This would provide a graceful failure mode
440    when trying to get the value of caller-saves registers for an inner
441    frame.  */
442
443 CORE_ADDR
444 fr30_find_callers_reg (fi, regnum)
445      struct frame_info *fi;
446      int regnum;
447 {
448   for (; fi; fi = fi->next)
449     if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
450       return generic_read_register_dummy (fi->pc, fi->frame, regnum);
451     else if (fi->fsr.regs[regnum] != 0)
452       return read_memory_unsigned_integer (fi->fsr.regs[regnum], 
453                                            REGISTER_RAW_SIZE(regnum));
454
455   return read_register (regnum);
456 }
457
458
459 /* Function: frame_chain
460    Figure out the frame prior to FI.  Unfortunately, this involves
461    scanning the prologue of the caller, which will also be done
462    shortly by fr30_init_extra_frame_info.  For the dummy frame, we
463    just return the stack pointer that was in use at the time the
464    function call was made.  */
465
466
467 CORE_ADDR
468 fr30_frame_chain (fi)
469      struct frame_info * fi;
470 {
471   CORE_ADDR fn_start, callers_pc, fp;
472   struct frame_info caller_fi;
473   int framereg;
474
475   /* is this a dummy frame? */
476   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
477     return fi->frame;   /* dummy frame same as caller's frame */
478
479   /* is caller-of-this a dummy frame? */
480   callers_pc = FRAME_SAVED_PC(fi);  /* find out who called us: */
481   fp = fr30_find_callers_reg (fi, FP_REGNUM);
482   if (PC_IN_CALL_DUMMY (callers_pc, fp, fp))    
483     return fp;          /* dummy frame's frame may bear no relation to ours */
484
485   if (find_pc_partial_function (fi->pc, 0, &fn_start, 0))
486     if (fn_start == entry_point_address ())
487       return 0;         /* in _start fn, don't chain further */
488
489   framereg = fi->framereg;
490
491   /* If the caller is the startup code, we're at the end of the chain.  */
492   if (find_pc_partial_function (callers_pc, 0, &fn_start, 0))
493     if (fn_start == entry_point_address ())
494       return 0;
495
496   memset (& caller_fi, 0, sizeof (caller_fi));
497   caller_fi.pc = callers_pc;
498   fr30_scan_prologue (& caller_fi);
499   framereg = caller_fi.framereg;
500
501   /* If the caller used a frame register, return its value.
502      Otherwise, return the caller's stack pointer.  */
503   if (framereg == FP_REGNUM)
504     return fr30_find_callers_reg (fi, framereg);
505   else
506     return fi->frame + fi->framesize;
507 }
508
509 /* Function: frame_saved_pc 
510    Find the caller of this frame.  We do this by seeing if RP_REGNUM
511    is saved in the stack anywhere, otherwise we get it from the
512    registers.  If the inner frame is a dummy frame, return its PC
513    instead of RP, because that's where "caller" of the dummy-frame
514    will be found.  */
515
516 CORE_ADDR
517 fr30_frame_saved_pc (fi)
518      struct frame_info *fi;
519 {
520   if (PC_IN_CALL_DUMMY(fi->pc, fi->frame, fi->frame))
521     return generic_read_register_dummy(fi->pc, fi->frame, PC_REGNUM);
522   else
523     return fr30_find_callers_reg (fi, RP_REGNUM);
524 }
525
526 /* Function: fix_call_dummy
527    Pokes the callee function's address into the CALL_DUMMY assembly stub.
528    Assumes that the CALL_DUMMY looks like this:
529         jarl <offset24>, r31
530         trap
531    */
532
533 int
534 fr30_fix_call_dummy (dummy, sp, fun, nargs, args, type, gcc_p)
535      char *dummy;
536      CORE_ADDR sp;
537      CORE_ADDR fun;
538      int nargs;
539      value_ptr *args;
540      struct type *type;
541      int gcc_p;
542 {
543   long offset24;
544
545   offset24 = (long) fun - (long) entry_point_address ();
546   offset24 &= 0x3fffff;
547   offset24 |= 0xff800000;       /* jarl <offset24>, r31 */
548
549   store_unsigned_integer ((unsigned int *)&dummy[2], 2, offset24 & 0xffff);
550   store_unsigned_integer ((unsigned int *)&dummy[0], 2, offset24 >> 16);
551   return 0;
552 }