bcopy -> memcpy
[external/binutils.git] / gdb / rs6000-tdep.c
1 /* Target-dependent code for GDB, the GNU debugger.
2    Copyright 1986, 1987, 1989, 1991, 1992 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 "target.h"
25 #include "gdbcore.h"
26
27 #include "xcoffsolib.h"
28
29 #include <sys/param.h>
30 #include <sys/dir.h>
31 #include <sys/user.h>
32 #include <signal.h>
33 #include <sys/ioctl.h>
34 #include <fcntl.h>
35
36 #include <a.out.h>
37 #include <sys/file.h>
38 #include <sys/stat.h>
39 #include <sys/core.h>
40 #include <sys/ldr.h>
41
42
43 extern struct obstack frame_cache_obstack;
44
45 extern int errno;
46
47 /* Nonzero if we just simulated a single step break. */
48 int one_stepped;
49
50 /* Breakpoint shadows for the single step instructions will be kept here. */
51
52 static struct sstep_breaks {
53   /* Address, or 0 if this is not in use.  */
54   CORE_ADDR address;
55   /* Shadow contents.  */
56   char data[4];
57 } stepBreaks[2];
58
59 /* Static function prototypes */
60
61 static CORE_ADDR
62 find_toc_address PARAMS ((CORE_ADDR pc));
63
64 static CORE_ADDR
65 branch_dest PARAMS ((int opcode, int instr, CORE_ADDR pc, CORE_ADDR safety));
66
67 static void
68 frame_get_cache_fsr PARAMS ((struct frame_info *fi,
69                              struct aix_framedata *fdatap));
70
71 /*
72  * Calculate the destination of a branch/jump.  Return -1 if not a branch.
73  */
74 static CORE_ADDR
75 branch_dest (opcode, instr, pc, safety)
76      int opcode;
77      int instr;
78      CORE_ADDR pc;
79      CORE_ADDR safety;
80 {
81   register long offset;
82   CORE_ADDR dest;
83   int immediate;
84   int absolute;
85   int ext_op;
86
87   absolute = (int) ((instr >> 1) & 1);
88
89   switch (opcode) {
90      case 18    :
91         immediate = ((instr & ~3) << 6) >> 6;   /* br unconditional */
92
93      case 16    :  
94         if (opcode != 18)                       /* br conditional */
95           immediate = ((instr & ~3) << 16) >> 16;
96         if (absolute)
97           dest = immediate;     
98         else
99           dest = pc + immediate;
100         break;
101
102       case 19   :
103         ext_op = (instr>>1) & 0x3ff;
104
105         if (ext_op == 16)                       /* br conditional register */
106           dest = read_register (LR_REGNUM) & ~3;
107
108         else if (ext_op == 528)                 /* br cond to count reg */
109           {
110             dest = read_register (CTR_REGNUM) & ~3;
111
112             /* If we are about to execute a system call, dest is something
113                like 0x22fc or 0x3b00.  Upon completion the system call
114                will return to the address in the link register.  */
115             if (dest < TEXT_SEGMENT_BASE)
116               dest = read_register (LR_REGNUM) & ~3;
117           }
118         else return -1; 
119         break;
120         
121        default: return -1;
122   }
123   return (dest < TEXT_SEGMENT_BASE) ? safety : dest;
124 }
125
126
127
128 /* AIX does not support PT_STEP. Simulate it. */
129
130 void
131 single_step (signal)
132      int signal;
133 {
134 #define INSNLEN(OPCODE)  4
135
136   static char breakp[] = BREAKPOINT;
137   int ii, insn;
138   CORE_ADDR loc;
139   CORE_ADDR breaks[2];
140   int opcode;
141
142   if (!one_stepped) {
143     loc = read_pc ();
144
145     read_memory (loc, (char *) &insn, 4);
146
147     breaks[0] = loc + INSNLEN(insn);
148     opcode = insn >> 26;
149     breaks[1] = branch_dest (opcode, insn, loc, breaks[0]);
150
151     /* Don't put two breakpoints on the same address. */
152     if (breaks[1] == breaks[0])
153       breaks[1] = -1;
154
155     stepBreaks[1].address = 0;
156
157     for (ii=0; ii < 2; ++ii) {
158
159       /* ignore invalid breakpoint. */
160       if ( breaks[ii] == -1)
161         continue;
162
163       read_memory (breaks[ii], stepBreaks[ii].data, 4);
164
165       write_memory (breaks[ii], breakp, 4);
166       stepBreaks[ii].address = breaks[ii];
167     }  
168
169     one_stepped = 1;
170   } else {
171
172     /* remove step breakpoints. */
173     for (ii=0; ii < 2; ++ii)
174       if (stepBreaks[ii].address != 0)
175         write_memory 
176            (stepBreaks[ii].address, stepBreaks[ii].data, 4);
177
178     one_stepped = 0;
179   }
180   errno = 0;                    /* FIXME, don't ignore errors! */
181                         /* What errors?  {read,write}_memory call error().  */
182 }
183
184
185 /* return pc value after skipping a function prologue. */
186
187 skip_prologue (pc)
188 CORE_ADDR pc;
189 {
190   char buf[4];
191   unsigned int tmp;
192   unsigned long op;
193
194   if (target_read_memory (pc, buf, 4))
195     return pc;                  /* Can't access it -- assume no prologue. */
196   op = extract_unsigned_integer (buf, 4);
197
198   /* Assume that subsequent fetches can fail with low probability.  */
199
200   if (op == 0x7c0802a6) {               /* mflr r0 */
201     pc += 4;
202     op = read_memory_integer (pc, 4);
203   }
204
205   if ((op & 0xfc00003e) == 0x7c000026) { /* mfcr Rx */
206     pc += 4;
207     op = read_memory_integer (pc, 4);
208   }
209
210   if ((op & 0xfc000000) == 0x48000000) { /* bl foo, to save fprs??? */
211     pc += 4;
212     op = read_memory_integer (pc, 4);
213
214     /* At this point, make sure this is not a trampoline function
215        (a function that simply calls another functions, and nothing else).
216        If the next is not a nop, this branch was part of the function
217        prologue. */
218
219     if (op == 0x4def7b82 ||             /* crorc 15, 15, 15 */
220         op == 0x0)
221       return pc - 4;                    /* don't skip over this branch */
222   }
223
224   if ((op & 0xfc1f0000) == 0xd8010000) { /* stfd Rx,NUM(r1) */
225     pc += 4;                             /* store floating register double */
226     op = read_memory_integer (pc, 4);
227   }
228
229   if ((op & 0xfc1f0000) == 0xbc010000) { /* stm Rx, NUM(r1) */
230     pc += 4;
231     op = read_memory_integer (pc, 4);
232   }
233
234   while (((tmp = op >> 16) == 0x9001) || /* st   r0, NUM(r1) */
235          (tmp == 0x9421) ||             /* stu  r1, NUM(r1) */
236          (tmp == 0x93e1))               /* st   r31,NUM(r1) */
237   {
238     pc += 4;
239     op = read_memory_integer (pc, 4);
240   }
241
242   while ((tmp = (op >> 22)) == 0x20f) { /* l    r31, ... or */
243     pc += 4;                            /* l    r30, ...    */
244     op = read_memory_integer (pc, 4);
245   }
246
247   /* store parameters into stack */
248   while(
249         (op & 0xfc1f0000) == 0xd8010000 ||      /* stfd Rx,NUM(r1) */
250         (op & 0xfc1f0000) == 0x90010000 ||      /* st r?, NUM(r1)  */
251         (op & 0xfc000000) == 0xfc000000 ||      /* frsp, fp?, .. */
252         (op & 0xd0000000) == 0xd0000000)        /* stfs, fp?, .. */
253     {
254       pc += 4;                                  /* store fpr double */
255       op = read_memory_integer (pc, 4);
256     }
257
258   if (op == 0x603f0000) {                       /* oril r31, r1, 0x0 */
259     pc += 4;                                    /* this happens if r31 is used as */
260     op = read_memory_integer (pc, 4);           /* frame ptr. (gcc does that)     */
261
262     tmp = 0;
263     while ((op >> 16) == (0x907f + tmp)) {      /* st r3, NUM(r31) */
264       pc += 4;                                  /* st r4, NUM(r31), ... */
265       op = read_memory_integer (pc, 4);
266       tmp += 0x20;
267     }
268   }
269 #if 0
270 /* I have problems with skipping over __main() that I need to address
271  * sometime. Previously, I used to use misc_function_vector which
272  * didn't work as well as I wanted to be.  -MGO */
273
274   /* If the first thing after skipping a prolog is a branch to a function,
275      this might be a call to an initializer in main(), introduced by gcc2.
276      We'd like to skip over it as well. Fortunately, xlc does some extra
277      work before calling a function right after a prologue, thus we can
278      single out such gcc2 behaviour. */
279      
280
281   if ((op & 0xfc000001) == 0x48000001) { /* bl foo, an initializer function? */
282     op = read_memory_integer (pc+4, 4);
283
284     if (op == 0x4def7b82) {             /* cror 0xf, 0xf, 0xf (nop) */
285
286       /* check and see if we are in main. If so, skip over this initializer
287          function as well. */
288
289       tmp = find_pc_misc_function (pc);
290       if (tmp >= 0 && STREQ (misc_function_vector [tmp].name, "main"))
291         return pc + 8;
292     }
293   }
294 #endif /* 0 */
295  
296   return pc;
297 }
298
299
300 /*************************************************************************
301   Support for creating pushind a dummy frame into the stack, and popping
302   frames, etc. 
303 *************************************************************************/
304
305 /* The total size of dummy frame is 436, which is;
306
307         32 gpr's        - 128 bytes
308         32 fpr's        - 256   "
309         7  the rest     - 28    "
310         and 24 extra bytes for the callee's link area. The last 24 bytes
311         for the link area might not be necessary, since it will be taken
312         care of by push_arguments(). */
313
314 #define DUMMY_FRAME_SIZE 436
315
316 #define DUMMY_FRAME_ADDR_SIZE 10
317
318 /* Make sure you initialize these in somewhere, in case gdb gives up what it
319    was debugging and starts debugging something else. FIXMEibm */
320
321 static int dummy_frame_count = 0;
322 static int dummy_frame_size = 0;
323 static CORE_ADDR *dummy_frame_addr = 0;
324
325 extern int stop_stack_dummy;
326
327 /* push a dummy frame into stack, save all register. Currently we are saving
328    only gpr's and fpr's, which is not good enough! FIXMEmgo */
329    
330 void
331 push_dummy_frame ()
332 {
333   /* stack pointer.  */
334   CORE_ADDR sp;
335
336   /* link register.  */
337   CORE_ADDR pc;
338   /* Same thing, target byte order.  */
339   char pc_targ[4];
340   
341   int ii;
342
343   target_fetch_registers (-1);
344
345   if (dummy_frame_count >= dummy_frame_size) {
346     dummy_frame_size += DUMMY_FRAME_ADDR_SIZE;
347     if (dummy_frame_addr)
348       dummy_frame_addr = (CORE_ADDR*) xrealloc 
349         (dummy_frame_addr, sizeof(CORE_ADDR) * (dummy_frame_size));
350     else
351       dummy_frame_addr = (CORE_ADDR*) 
352         xmalloc (sizeof(CORE_ADDR) * (dummy_frame_size));
353   }
354   
355   sp = read_register(SP_REGNUM);
356   pc = read_register(PC_REGNUM);
357   memcpy (pc_targ, (char *) &pc, 4);
358
359   dummy_frame_addr [dummy_frame_count++] = sp;
360
361   /* Be careful! If the stack pointer is not decremented first, then kernel 
362      thinks he is free to use the space underneath it. And kernel actually 
363      uses that area for IPC purposes when executing ptrace(2) calls. So 
364      before writing register values into the new frame, decrement and update
365      %sp first in order to secure your frame. */
366
367   write_register (SP_REGNUM, sp-DUMMY_FRAME_SIZE);
368
369   /* gdb relies on the state of current_frame. We'd better update it,
370      otherwise things like do_registers_info() wouldn't work properly! */
371
372   flush_cached_frames ();
373   set_current_frame (create_new_frame (sp-DUMMY_FRAME_SIZE, pc));
374
375   /* save program counter in link register's space. */
376   write_memory (sp+8, pc_targ, 4);
377
378   /* save all floating point and general purpose registers here. */
379
380   /* fpr's, f0..f31 */
381   for (ii = 0; ii < 32; ++ii)
382     write_memory (sp-8-(ii*8), &registers[REGISTER_BYTE (31-ii+FP0_REGNUM)], 8);
383
384   /* gpr's r0..r31 */
385   for (ii=1; ii <=32; ++ii)
386     write_memory (sp-256-(ii*4), &registers[REGISTER_BYTE (32-ii)], 4);
387
388   /* so far, 32*2 + 32 words = 384 bytes have been written. 
389      7 extra registers in our register set: pc, ps, cnd, lr, cnt, xer, mq */
390
391   for (ii=1; ii <= (LAST_SP_REGNUM-FIRST_SP_REGNUM+1); ++ii) {
392     write_memory (sp-384-(ii*4), 
393                &registers[REGISTER_BYTE (FPLAST_REGNUM + ii)], 4);
394   }
395
396   /* Save sp or so called back chain right here. */
397   write_memory (sp-DUMMY_FRAME_SIZE, &sp, 4);
398   sp -= DUMMY_FRAME_SIZE;
399
400   /* And finally, this is the back chain. */
401   write_memory (sp+8, pc_targ, 4);
402 }
403
404
405 /* Pop a dummy frame.
406
407    In rs6000 when we push a dummy frame, we save all of the registers. This
408    is usually done before user calls a function explicitly.
409
410    After a dummy frame is pushed, some instructions are copied into stack,
411    and stack pointer is decremented even more.  Since we don't have a frame
412    pointer to get back to the parent frame of the dummy, we start having
413    trouble poping it.  Therefore, we keep a dummy frame stack, keeping
414    addresses of dummy frames as such.  When poping happens and when we
415    detect that was a dummy frame, we pop it back to its parent by using
416    dummy frame stack (`dummy_frame_addr' array). 
417
418 FIXME:  This whole concept is broken.  You should be able to detect
419 a dummy stack frame *on the user's stack itself*.  When you do,
420 then you know the format of that stack frame -- including its
421 saved SP register!  There should *not* be a separate stack in the
422 GDB process that keeps track of these dummy frames!  -- gnu@cygnus.com Aug92
423  */
424    
425 pop_dummy_frame ()
426 {
427   CORE_ADDR sp, pc;
428   int ii;
429   sp = dummy_frame_addr [--dummy_frame_count];
430
431   /* restore all fpr's. */
432   for (ii = 1; ii <= 32; ++ii)
433     read_memory (sp-(ii*8), &registers[REGISTER_BYTE (32-ii+FP0_REGNUM)], 8);
434
435   /* restore all gpr's */
436   for (ii=1; ii <= 32; ++ii) {
437     read_memory (sp-256-(ii*4), &registers[REGISTER_BYTE (32-ii)], 4);
438   }
439
440   /* restore the rest of the registers. */
441   for (ii=1; ii <=(LAST_SP_REGNUM-FIRST_SP_REGNUM+1); ++ii)
442     read_memory (sp-384-(ii*4),
443                 &registers[REGISTER_BYTE (FPLAST_REGNUM + ii)], 4);
444
445   read_memory (sp-(DUMMY_FRAME_SIZE-8), 
446                                 &registers [REGISTER_BYTE(PC_REGNUM)], 4);
447
448   /* when a dummy frame was being pushed, we had to decrement %sp first, in 
449      order to secure astack space. Thus, saved %sp (or %r1) value, is not the
450      one we should restore. Change it with the one we need. */
451
452   *(int*)&registers [REGISTER_BYTE(FP_REGNUM)] = sp;
453
454   /* Now we can restore all registers. */
455
456   target_store_registers (-1);
457   pc = read_pc ();
458   flush_cached_frames ();
459   set_current_frame (create_new_frame (sp, pc));
460 }
461
462
463 /* pop the innermost frame, go back to the caller. */
464
465 void
466 pop_frame ()
467 {
468   CORE_ADDR pc, lr, sp, prev_sp;                /* %pc, %lr, %sp */
469   struct aix_framedata fdata;
470   FRAME fr = get_current_frame ();
471   int addr, ii;
472
473   pc = read_pc ();
474   sp = FRAME_FP (fr);
475
476   if (stop_stack_dummy && dummy_frame_count) {
477     pop_dummy_frame ();
478     return;
479   }
480
481   /* figure out previous %pc value. If the function is frameless, it is 
482      still in the link register, otherwise walk the frames and retrieve the
483      saved %pc value in the previous frame. */
484
485   addr = get_pc_function_start (fr->pc) + FUNCTION_START_OFFSET;
486   function_frame_info (addr, &fdata);
487
488   prev_sp = read_memory_integer (sp, 4);
489   if (fdata.frameless)
490     lr = read_register (LR_REGNUM);
491   else
492     lr = read_memory_integer (prev_sp+8, 4);
493
494   /* reset %pc value. */
495   write_register (PC_REGNUM, lr);
496
497   /* reset register values if any was saved earlier. */
498   addr = prev_sp - fdata.offset;
499
500   if (fdata.saved_gpr != -1)
501     for (ii=fdata.saved_gpr; ii <= 31; ++ii) {
502       read_memory (addr, &registers [REGISTER_BYTE (ii)], 4);
503       addr += 4;
504     }
505
506   if (fdata.saved_fpr != -1)
507     for (ii=fdata.saved_fpr; ii <= 31; ++ii) {
508       read_memory (addr, &registers [REGISTER_BYTE (ii+FP0_REGNUM)], 8);
509       addr += 8;
510   }
511
512   write_register (SP_REGNUM, prev_sp);
513   target_store_registers (-1);
514   flush_cached_frames ();
515   set_current_frame (create_new_frame (prev_sp, lr));
516 }
517
518
519 /* fixup the call sequence of a dummy function, with the real function address.
520    its argumets will be passed by gdb. */
521
522 void
523 fix_call_dummy(dummyname, pc, fun, nargs, type)
524   char *dummyname;
525   CORE_ADDR pc;
526   CORE_ADDR fun;
527   int nargs;                                    /* not used */
528   int type;                                     /* not used */
529 {
530 #define TOC_ADDR_OFFSET         20
531 #define TARGET_ADDR_OFFSET      28
532
533   int ii;
534   CORE_ADDR target_addr;
535   CORE_ADDR tocvalue;
536
537   target_addr = fun;
538   tocvalue = find_toc_address (target_addr);
539
540   ii  = *(int*)((char*)dummyname + TOC_ADDR_OFFSET);
541   ii = (ii & 0xffff0000) | (tocvalue >> 16);
542   *(int*)((char*)dummyname + TOC_ADDR_OFFSET) = ii;
543
544   ii  = *(int*)((char*)dummyname + TOC_ADDR_OFFSET+4);
545   ii = (ii & 0xffff0000) | (tocvalue & 0x0000ffff);
546   *(int*)((char*)dummyname + TOC_ADDR_OFFSET+4) = ii;
547
548   ii  = *(int*)((char*)dummyname + TARGET_ADDR_OFFSET);
549   ii = (ii & 0xffff0000) | (target_addr >> 16);
550   *(int*)((char*)dummyname + TARGET_ADDR_OFFSET) = ii;
551
552   ii  = *(int*)((char*)dummyname + TARGET_ADDR_OFFSET+4);
553   ii = (ii & 0xffff0000) | (target_addr & 0x0000ffff);
554   *(int*)((char*)dummyname + TARGET_ADDR_OFFSET+4) = ii;
555 }
556
557
558 /* return information about a function frame.
559    in struct aix_frameinfo fdata:
560     - frameless is TRUE, if function does not have a frame.
561     - nosavedpc is TRUE, if function does not save %pc value in its frame.
562     - offset is the number of bytes used in the frame to save registers.
563     - saved_gpr is the number of the first saved gpr.
564     - saved_fpr is the number of the first saved fpr.
565     - alloca_reg is the number of the register used for alloca() handling.
566       Otherwise -1.
567  */
568 void
569 function_frame_info (pc, fdata)
570   CORE_ADDR pc;
571   struct aix_framedata *fdata;
572 {
573   unsigned int tmp;
574   register unsigned int op;
575
576   fdata->offset = 0;
577   fdata->saved_gpr = fdata->saved_fpr = fdata->alloca_reg = -1;
578   fdata->frameless = 1;
579
580   op  = read_memory_integer (pc, 4);
581   if (op == 0x7c0802a6) {               /* mflr r0 */
582     pc += 4;
583     op = read_memory_integer (pc, 4);
584     fdata->nosavedpc = 0;
585     fdata->frameless = 0;
586   }
587   else                          /* else, pc is not saved */
588     fdata->nosavedpc = 1;
589
590   if ((op & 0xfc00003e) == 0x7c000026) { /* mfcr Rx */
591     pc += 4;
592     op = read_memory_integer (pc, 4);
593     fdata->frameless = 0;
594   }
595
596   if ((op & 0xfc000000) == 0x48000000) { /* bl foo, to save fprs??? */
597     pc += 4;
598     op = read_memory_integer (pc, 4);
599     /* At this point, make sure this is not a trampoline function
600        (a function that simply calls another functions, and nothing else).
601        If the next is not a nop, this branch was part of the function
602        prologue. */
603
604     if (op == 0x4def7b82 ||             /* crorc 15, 15, 15 */
605         op == 0x0)
606       return;                           /* prologue is over */
607     fdata->frameless = 0;
608   }
609
610   if ((op & 0xfc1f0000) == 0xd8010000) { /* stfd Rx,NUM(r1) */
611     pc += 4;                             /* store floating register double */
612     op = read_memory_integer (pc, 4);
613     fdata->frameless = 0;
614   }
615
616   if ((op & 0xfc1f0000) == 0xbc010000) { /* stm Rx, NUM(r1) */
617     int tmp2;
618     fdata->saved_gpr = (op >> 21) & 0x1f;
619     tmp2 = op & 0xffff;
620     if (tmp2 > 0x7fff)
621       tmp2 = (~0 &~ 0xffff) | tmp2;
622
623     if (tmp2 < 0) {
624       tmp2 = tmp2 * -1;
625       fdata->saved_fpr = (tmp2 - ((32 - fdata->saved_gpr) * 4)) / 8;
626       if ( fdata->saved_fpr > 0)
627         fdata->saved_fpr = 32 - fdata->saved_fpr;
628       else
629         fdata->saved_fpr = -1;
630     }
631     fdata->offset = tmp2;
632     pc += 4;
633     op = read_memory_integer (pc, 4);
634     fdata->frameless = 0;
635   }
636
637   while (((tmp = op >> 16) == 0x9001) ||        /* st   r0, NUM(r1) */
638          (tmp == 0x9421) ||                     /* stu  r1, NUM(r1) */
639          (tmp == 0x93e1))                       /* st r31, NUM(r1) */
640   {
641     int tmp2;
642
643     /* gcc takes a short cut and uses this instruction to save r31 only. */
644
645     if (tmp == 0x93e1) {
646       if (fdata->offset)
647 /*        fatal ("Unrecognized prolog."); */
648         printf ("Unrecognized prolog!\n");
649
650       fdata->saved_gpr = 31;
651       tmp2 = op & 0xffff;
652       if (tmp2 > 0x7fff) {
653         tmp2 = - ((~0 &~ 0xffff) | tmp2);
654         fdata->saved_fpr = (tmp2 - ((32 - 31) * 4)) / 8;
655         if ( fdata->saved_fpr > 0)
656           fdata->saved_fpr = 32 - fdata->saved_fpr;
657         else
658           fdata->saved_fpr = -1;
659       }
660       fdata->offset = tmp2;
661     }
662     pc += 4;
663     op = read_memory_integer (pc, 4);
664     fdata->frameless = 0;
665   }
666
667   while ((tmp = (op >> 22)) == 0x20f) { /* l    r31, ... or */
668     pc += 4;                            /* l    r30, ...    */
669     op = read_memory_integer (pc, 4);
670     fdata->frameless = 0;
671   }
672
673   /* store parameters into stack */
674   while(
675         (op & 0xfc1f0000) == 0xd8010000 ||      /* stfd Rx,NUM(r1) */
676         (op & 0xfc1f0000) == 0x90010000 ||      /* st r?, NUM(r1)  */
677         (op & 0xfc000000) == 0xfc000000 ||      /* frsp, fp?, .. */
678         (op & 0xd0000000) == 0xd0000000)        /* stfs, fp?, .. */
679     {
680       pc += 4;                                  /* store fpr double */
681       op = read_memory_integer (pc, 4);
682       fdata->frameless = 0;
683     }
684
685   if (op == 0x603f0000) {                       /* oril r31, r1, 0x0 */
686     fdata->alloca_reg = 31;
687     fdata->frameless = 0;
688   }
689 }
690
691
692 /* Pass the arguments in either registers, or in the stack. In RS6000, the first
693    eight words of the argument list (that might be less than eight parameters if
694    some parameters occupy more than one word) are passed in r3..r11 registers.
695    float and double parameters are passed in fpr's, in addition to that. Rest of
696    the parameters if any are passed in user stack. There might be cases in which
697    half of the parameter is copied into registers, the other half is pushed into
698    stack.
699
700    If the function is returning a structure, then the return address is passed
701    in r3, then the first 7 words of the parametes can be passed in registers,
702    starting from r4. */
703
704 CORE_ADDR
705 push_arguments (nargs, args, sp, struct_return, struct_addr)
706   int nargs;
707   value *args;
708   CORE_ADDR sp;
709   int struct_return;
710   CORE_ADDR struct_addr;
711 {
712   int ii, len;
713   int argno;                                    /* current argument number */
714   int argbytes;                                 /* current argument byte */
715   char tmp_buffer [50];
716   value arg;
717   int f_argno = 0;                              /* current floating point argno */
718
719   CORE_ADDR saved_sp, pc;
720
721   if ( dummy_frame_count <= 0)
722     printf ("FATAL ERROR -push_arguments()! frame not found!!\n");
723
724   /* The first eight words of ther arguments are passed in registers. Copy
725      them appropriately.
726
727      If the function is returning a `struct', then the first word (which 
728      will be passed in r3) is used for struct return address. In that
729      case we should advance one word and start from r4 register to copy 
730      parameters. */
731
732   ii =  struct_return ? 1 : 0;
733
734   for (argno=0, argbytes=0; argno < nargs && ii<8; ++ii) {
735
736     arg = value_arg_coerce (args[argno]);
737     len = TYPE_LENGTH (VALUE_TYPE (arg));
738
739     if (TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_FLT) {
740
741       /* floating point arguments are passed in fpr's, as well as gpr's.
742          There are 13 fpr's reserved for passing parameters. At this point
743          there is no way we would run out of them. */
744
745       if (len > 8)
746         printf (
747 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
748
749       memcpy (&registers[REGISTER_BYTE(FP0_REGNUM + 1 + f_argno)], VALUE_CONTENTS (arg), 
750          len);
751       ++f_argno;
752     }
753
754     if (len > 4) {
755
756       /* Argument takes more than one register. */
757       while (argbytes < len) {
758
759         *(int*)&registers[REGISTER_BYTE(ii+3)] = 0;
760         memcpy (&registers[REGISTER_BYTE(ii+3)], 
761                          ((char*)VALUE_CONTENTS (arg))+argbytes, 
762                         (len - argbytes) > 4 ? 4 : len - argbytes);
763         ++ii, argbytes += 4;
764
765         if (ii >= 8)
766           goto ran_out_of_registers_for_arguments;
767       }
768       argbytes = 0;
769       --ii;
770     }
771     else {        /* Argument can fit in one register. No problem. */
772       *(int*)&registers[REGISTER_BYTE(ii+3)] = 0;
773       memcpy (&registers[REGISTER_BYTE(ii+3)], VALUE_CONTENTS (arg), len);
774     }
775     ++argno;
776   }
777
778 ran_out_of_registers_for_arguments:
779
780   /* location for 8 parameters are always reserved. */
781   sp -= 4 * 8;
782
783   /* another six words for back chain, TOC register, link register, etc. */
784   sp -= 24;
785
786   /* if there are more arguments, allocate space for them in 
787      the stack, then push them starting from the ninth one. */
788
789   if ((argno < nargs) || argbytes) {
790     int space = 0, jj;
791     value val;
792
793     if (argbytes) {
794       space += ((len - argbytes + 3) & -4);
795       jj = argno + 1;
796     }
797     else
798       jj = argno;
799
800     for (; jj < nargs; ++jj) {
801       val = value_arg_coerce (args[jj]);
802       space += ((TYPE_LENGTH (VALUE_TYPE (val))) + 3) & -4;
803     }
804
805     /* add location required for the rest of the parameters */
806     space = (space + 7) & -8;
807     sp -= space;
808
809     /* This is another instance we need to be concerned about securing our
810         stack space. If we write anything underneath %sp (r1), we might conflict
811         with the kernel who thinks he is free to use this area. So, update %sp
812         first before doing anything else. */
813
814     write_register (SP_REGNUM, sp);
815
816     /* if the last argument copied into the registers didn't fit there 
817        completely, push the rest of it into stack. */
818
819     if (argbytes) {
820       write_memory (
821         sp+24+(ii*4), ((char*)VALUE_CONTENTS (arg))+argbytes, len - argbytes);
822       ++argno;
823       ii += ((len - argbytes + 3) & -4) / 4;
824     }
825
826     /* push the rest of the arguments into stack. */
827     for (; argno < nargs; ++argno) {
828
829       arg = value_arg_coerce (args[argno]);
830       len = TYPE_LENGTH (VALUE_TYPE (arg));
831
832
833       /* float types should be passed in fpr's, as well as in the stack. */
834       if (TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_FLT && f_argno < 13) {
835
836         if (len > 8)
837           printf (
838 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
839
840         memcpy (&registers[REGISTER_BYTE(FP0_REGNUM + 1 + f_argno)], VALUE_CONTENTS (arg), 
841            len);
842         ++f_argno;
843       }
844
845       write_memory (sp+24+(ii*4), (char *) VALUE_CONTENTS (arg), len);
846       ii += ((len + 3) & -4) / 4;
847     }
848   }
849   else
850     /* Secure stack areas first, before doing anything else. */
851     write_register (SP_REGNUM, sp);
852
853   saved_sp = dummy_frame_addr [dummy_frame_count - 1];
854   read_memory (saved_sp, tmp_buffer, 24);
855   write_memory (sp, tmp_buffer, 24);
856
857     write_memory (sp, &saved_sp, 4);    /* set back chain properly */
858
859   target_store_registers (-1);
860   return sp;
861 }
862
863 /* a given return value in `regbuf' with a type `valtype', extract and copy its
864    value into `valbuf' */
865
866 void
867 extract_return_value (valtype, regbuf, valbuf)
868   struct type *valtype;
869   char regbuf[REGISTER_BYTES];
870   char *valbuf;
871 {
872
873   if (TYPE_CODE (valtype) == TYPE_CODE_FLT) {
874
875     double dd; float ff;
876     /* floats and doubles are returned in fpr1. fpr's have a size of 8 bytes.
877        We need to truncate the return value into float size (4 byte) if
878        necessary. */
879
880     if (TYPE_LENGTH (valtype) > 4)              /* this is a double */
881       memcpy (valbuf, &regbuf[REGISTER_BYTE (FP0_REGNUM + 1)],
882                                                 TYPE_LENGTH (valtype));
883     else {              /* float */
884       memcpy (&dd, &regbuf[REGISTER_BYTE (FP0_REGNUM + 1)], 8);
885       ff = (float)dd;
886       memcpy (valbuf, &ff, sizeof(float));
887     }
888   }
889   else
890     /* return value is copied starting from r3. */
891     memcpy (valbuf, &regbuf[REGISTER_BYTE (3)], TYPE_LENGTH (valtype));
892 }
893
894
895 /* keep structure return address in this variable.
896    FIXME:  This is a horrid kludge which should not be allowed to continue
897    living.  This only allows a single nested call to a structure-returning
898    function.  Come on, guys!  -- gnu@cygnus.com, Aug 92  */
899
900 CORE_ADDR rs6000_struct_return_address;
901
902
903 /* Indirect function calls use a piece of trampoline code to do context
904    switching, i.e. to set the new TOC table. Skip such code if we are on
905    its first instruction (as when we have single-stepped to here). 
906    Result is desired PC to step until, or NULL if we are not in
907    trampoline code.  */
908
909 CORE_ADDR
910 skip_trampoline_code (pc)
911 CORE_ADDR pc;
912 {
913   register unsigned int ii, op;
914
915   static unsigned trampoline_code[] = {
916         0x800b0000,                     /*     l   r0,0x0(r11)  */
917         0x90410014,                     /*    st   r2,0x14(r1)  */
918         0x7c0903a6,                     /* mtctr   r0           */
919         0x804b0004,                     /*     l   r2,0x4(r11)  */
920         0x816b0008,                     /*     l  r11,0x8(r11)  */
921         0x4e800420,                     /*  bctr                */
922         0x4e800020,                     /*    br                */
923         0
924   };
925
926   for (ii=0; trampoline_code[ii]; ++ii) {
927     op  = read_memory_integer (pc + (ii*4), 4);
928     if (op != trampoline_code [ii])
929       return 0;
930   }
931   ii = read_register (11);              /* r11 holds destination addr   */
932   pc = read_memory_integer (ii, 4);     /* (r11) value                  */
933   return pc;
934 }
935
936
937 /* Determines whether the function FI has a frame on the stack or not.
938    Called from the FRAMELESS_FUNCTION_INVOCATION macro in tm.h with a
939    second argument of 0, and from the FRAME_SAVED_PC macro with a
940    second argument of 1.  */
941
942 int
943 frameless_function_invocation (fi, pcsaved)
944 struct frame_info *fi;
945 int pcsaved;
946 {
947   CORE_ADDR func_start;
948   struct aix_framedata fdata;
949
950   if (fi->next != NULL)
951     /* Don't even think about framelessness except on the innermost frame.  */
952     return 0;
953   
954   func_start = get_pc_function_start (fi->pc) + FUNCTION_START_OFFSET;
955
956   /* If we failed to find the start of the function, it is a mistake
957      to inspect the instructions. */
958
959   if (!func_start)
960     return 0;
961
962   function_frame_info (func_start, &fdata);
963   return pcsaved ? fdata.nosavedpc : fdata.frameless;
964 }
965
966
967 /* If saved registers of frame FI are not known yet, read and cache them.
968    &FDATAP contains aix_framedata; TDATAP can be NULL,
969    in which case the framedata are read.  */
970
971 static void
972 frame_get_cache_fsr (fi, fdatap)
973      struct frame_info *fi;
974      struct aix_framedata *fdatap;
975 {
976   int ii;
977   CORE_ADDR frame_addr; 
978   struct aix_framedata work_fdata;
979
980   if (fi->cache_fsr)
981     return;
982   
983   if (fdatap == NULL) {
984     fdatap = &work_fdata;
985     function_frame_info (get_pc_function_start (fi->pc), fdatap);
986   }
987
988   fi->cache_fsr = (struct frame_saved_regs *)
989       obstack_alloc (&frame_cache_obstack, sizeof (struct frame_saved_regs));
990   bzero (fi->cache_fsr, sizeof (struct frame_saved_regs));
991
992   if (fi->prev && fi->prev->frame)
993     frame_addr = fi->prev->frame;
994   else
995     frame_addr = read_memory_integer (fi->frame, 4);
996   
997   /* if != -1, fdatap->saved_fpr is the smallest number of saved_fpr.
998      All fpr's from saved_fpr to fp31 are saved right underneath caller
999      stack pointer, starting from fp31 first. */
1000
1001   if (fdatap->saved_fpr >= 0) {
1002     for (ii=31; ii >= fdatap->saved_fpr; --ii)
1003       fi->cache_fsr->regs [FP0_REGNUM + ii] = frame_addr - ((32 - ii) * 8);
1004     frame_addr -= (32 - fdatap->saved_fpr) * 8;
1005   }
1006
1007   /* if != -1, fdatap->saved_gpr is the smallest number of saved_gpr.
1008      All gpr's from saved_gpr to gpr31 are saved right under saved fprs,
1009      starting from r31 first. */
1010   
1011   if (fdatap->saved_gpr >= 0)
1012     for (ii=31; ii >= fdatap->saved_gpr; --ii)
1013       fi->cache_fsr->regs [ii] = frame_addr - ((32 - ii) * 4);
1014 }
1015
1016 /* Return the address of a frame. This is the inital %sp value when the frame
1017    was first allocated. For functions calling alloca(), it might be saved in
1018    an alloca register. */
1019
1020 CORE_ADDR
1021 frame_initial_stack_address (fi)
1022      struct frame_info *fi;
1023 {
1024   CORE_ADDR tmpaddr;
1025   struct aix_framedata fdata;
1026   struct frame_info *callee_fi;
1027
1028   /* if the initial stack pointer (frame address) of this frame is known,
1029      just return it. */
1030
1031   if (fi->initial_sp)
1032     return fi->initial_sp;
1033
1034   /* find out if this function is using an alloca register.. */
1035
1036   function_frame_info (get_pc_function_start (fi->pc), &fdata);
1037
1038   /* if saved registers of this frame are not known yet, read and cache them. */
1039
1040   if (!fi->cache_fsr)
1041     frame_get_cache_fsr (fi, &fdata);
1042
1043   /* If no alloca register used, then fi->frame is the value of the %sp for
1044      this frame, and it is good enough. */
1045
1046   if (fdata.alloca_reg < 0) {
1047     fi->initial_sp = fi->frame;
1048     return fi->initial_sp;
1049   }
1050
1051   /* This function has an alloca register. If this is the top-most frame
1052      (with the lowest address), the value in alloca register is good. */
1053
1054   if (!fi->next)
1055     return fi->initial_sp = read_register (fdata.alloca_reg);     
1056
1057   /* Otherwise, this is a caller frame. Callee has usually already saved
1058      registers, but there are exceptions (such as when the callee
1059      has no parameters). Find the address in which caller's alloca
1060      register is saved. */
1061
1062   for (callee_fi = fi->next; callee_fi; callee_fi = callee_fi->next) {
1063
1064     if (!callee_fi->cache_fsr)
1065       frame_get_cache_fsr (callee_fi, NULL);
1066
1067     /* this is the address in which alloca register is saved. */
1068
1069     tmpaddr = callee_fi->cache_fsr->regs [fdata.alloca_reg];
1070     if (tmpaddr) {
1071       fi->initial_sp = read_memory_integer (tmpaddr, 4); 
1072       return fi->initial_sp;
1073     }
1074
1075     /* Go look into deeper levels of the frame chain to see if any one of
1076        the callees has saved alloca register. */
1077   }
1078
1079   /* If alloca register was not saved, by the callee (or any of its callees)
1080      then the value in the register is still good. */
1081
1082   return fi->initial_sp = read_register (fdata.alloca_reg);     
1083 }
1084
1085 FRAME_ADDR
1086 rs6000_frame_chain (thisframe)
1087      struct frame_info *thisframe;
1088 {
1089   FRAME_ADDR fp;
1090   if (inside_entry_file ((thisframe)->pc))
1091     return 0;
1092   if (thisframe->signal_handler_caller)
1093     {
1094       /* This was determined by experimentation on AIX 3.2.  Perhaps
1095          it corresponds to some offset in /usr/include/sys/user.h or
1096          something like that.  Using some system include file would
1097          have the advantage of probably being more robust in the face
1098          of OS upgrades, but the disadvantage of being wrong for
1099          cross-debugging.  */
1100
1101 #define SIG_FRAME_FP_OFFSET 284
1102       fp = read_memory_integer (thisframe->frame + SIG_FRAME_FP_OFFSET, 4);
1103     }
1104   else
1105     fp = read_memory_integer ((thisframe)->frame, 4);
1106
1107   return fp;
1108 }
1109
1110 \f
1111 /* xcoff_relocate_symtab -      hook for symbol table relocation.
1112    also reads shared libraries.. */
1113
1114 xcoff_relocate_symtab (pid)
1115 unsigned int pid;
1116 {
1117 #define MAX_LOAD_SEGS 64                /* maximum number of load segments */
1118
1119     struct ld_info *ldi;
1120     int temp;
1121
1122     ldi = (void *) alloca(MAX_LOAD_SEGS * sizeof (*ldi));
1123
1124     /* According to my humble theory, AIX has some timing problems and
1125        when the user stack grows, kernel doesn't update stack info in time
1126        and ptrace calls step on user stack. That is why we sleep here a little,
1127        and give kernel to update its internals. */
1128
1129     usleep (36000);
1130
1131     errno = 0;
1132     ptrace(PT_LDINFO, pid, (PTRACE_ARG3_TYPE) ldi,
1133            MAX_LOAD_SEGS * sizeof(*ldi), ldi);
1134     if (errno) {
1135       perror_with_name ("ptrace ldinfo");
1136       return 0;
1137     }
1138
1139     vmap_ldinfo(ldi);
1140
1141    do {
1142      /* We are allowed to assume CORE_ADDR == pointer.  This code is
1143         native only.  */
1144      add_text_to_loadinfo ((CORE_ADDR) ldi->ldinfo_textorg,
1145                            (CORE_ADDR) ldi->ldinfo_dataorg);
1146     } while (ldi->ldinfo_next
1147              && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));
1148
1149 #if 0
1150   /* Now that we've jumbled things around, re-sort them.  */
1151   sort_minimal_symbols ();
1152 #endif
1153
1154   /* relocate the exec and core sections as well. */
1155   vmap_exec ();
1156 }
1157 \f
1158 /* Keep an array of load segment information and their TOC table addresses.
1159    This info will be useful when calling a shared library function by hand. */
1160    
1161 struct loadinfo {
1162   CORE_ADDR textorg, dataorg;
1163   unsigned long toc_offset;
1164 };
1165
1166 #define LOADINFOLEN     10
1167
1168 static  struct loadinfo *loadinfo = NULL;
1169 static  int     loadinfolen = 0;
1170 static  int     loadinfotocindex = 0;
1171 static  int     loadinfotextindex = 0;
1172
1173
1174 void
1175 xcoff_init_loadinfo ()
1176 {
1177   loadinfotocindex = 0;
1178   loadinfotextindex = 0;
1179
1180   if (loadinfolen == 0) {
1181     loadinfo = (struct loadinfo *)
1182                xmalloc (sizeof (struct loadinfo) * LOADINFOLEN);
1183     loadinfolen = LOADINFOLEN;
1184   }
1185 }
1186
1187
1188 /* FIXME -- this is never called!  */
1189 void
1190 free_loadinfo ()
1191 {
1192   if (loadinfo)
1193     free (loadinfo);
1194   loadinfo = NULL;
1195   loadinfolen = 0;
1196   loadinfotocindex = 0;
1197   loadinfotextindex = 0;
1198 }
1199
1200 /* this is called from xcoffread.c */
1201
1202 void
1203 xcoff_add_toc_to_loadinfo (unsigned long tocoff)
1204 {
1205   while (loadinfotocindex >= loadinfolen) {
1206     loadinfolen += LOADINFOLEN;
1207     loadinfo = (struct loadinfo *)
1208                xrealloc (loadinfo, sizeof(struct loadinfo) * loadinfolen);
1209   }
1210   loadinfo [loadinfotocindex++].toc_offset = tocoff;
1211 }
1212
1213
1214 void
1215 add_text_to_loadinfo (textaddr, dataaddr)
1216      CORE_ADDR textaddr;
1217      CORE_ADDR dataaddr;
1218 {
1219   while (loadinfotextindex >= loadinfolen) {
1220     loadinfolen += LOADINFOLEN;
1221     loadinfo = (struct loadinfo *)
1222                xrealloc (loadinfo, sizeof(struct loadinfo) * loadinfolen);
1223   }
1224   loadinfo [loadinfotextindex].textorg = textaddr;
1225   loadinfo [loadinfotextindex].dataorg = dataaddr;
1226   ++loadinfotextindex;
1227 }
1228
1229
1230 /* FIXME:  This assumes that the "textorg" and "dataorg" elements
1231    of a member of this array are correlated with the "toc_offset"
1232    element of the same member.  But they are sequentially assigned in wildly
1233    different places, and probably there is no correlation.  FIXME!  */
1234
1235 static CORE_ADDR
1236 find_toc_address (pc)
1237      CORE_ADDR pc;
1238 {
1239   int ii, toc_entry, tocbase = 0;
1240
1241   for (ii=0; ii < loadinfotextindex; ++ii)
1242     if (pc > loadinfo[ii].textorg && loadinfo[ii].textorg > tocbase) {
1243       toc_entry = ii;
1244       tocbase = loadinfo[ii].textorg;
1245     }
1246
1247   return loadinfo[toc_entry].dataorg + loadinfo[toc_entry].toc_offset;
1248 }