2001-10-04 Frank Ch. Eigler <fche@redhat.com>
[binutils-gdb.git] / gdb / rs6000-tdep.c
index 252ea777873168fcec28d2e29151700334585613..82c18c1dac2e9210aba04013c97ce9ce07724731 100644 (file)
@@ -1,5 +1,6 @@
 /* Target-dependent code for GDB, the GNU debugger.
-   Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
+   Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
+   1998, 1999, 2000, 2001
    Free Software Foundation, Inc.
 
    This file is part of GDB.
@@ -28,8 +29,9 @@
 #include "gdbcmd.h"
 #include "symfile.h"
 #include "objfiles.h"
-#include "xcoffsolib.h"
 #include "arch-utils.h"
+#include "regcache.h"
+#include "doublest.h"
 
 #include "bfd/libbfd.h"                /* for bfd_default_set_arch_mach */
 #include "coff/internal.h"     /* for libcoff.h */
@@ -298,7 +300,8 @@ rs6000_breakpoint_from_pc (CORE_ADDR *bp_addr, int *bp_size)
 /* AIX does not support PT_STEP. Simulate it. */
 
 void
-rs6000_software_single_step (unsigned int signal, int insert_breakpoints_p)
+rs6000_software_single_step (enum target_signal signal,
+                            int insert_breakpoints_p)
 {
 #define        INSNLEN(OPCODE)  4
 
@@ -381,11 +384,63 @@ rs6000_software_single_step (unsigned int signal, int insert_breakpoints_p)
 
 #define GET_SRC_REG(x) (((x) >> 21) & 0x1f)
 
+/* Limit the number of skipped non-prologue instructions, as the examining
+   of the prologue is expensive.  */
+static int max_skip_non_prologue_insns = 10;
+
+/* Given PC representing the starting address of a function, and
+   LIM_PC which is the (sloppy) limit to which to scan when looking
+   for a prologue, attempt to further refine this limit by using
+   the line data in the symbol table.  If successful, a better guess
+   on where the prologue ends is returned, otherwise the previous
+   value of lim_pc is returned.  */
+static CORE_ADDR
+refine_prologue_limit (CORE_ADDR pc, CORE_ADDR lim_pc)
+{
+  struct symtab_and_line prologue_sal;
+
+  prologue_sal = find_pc_line (pc, 0);
+  if (prologue_sal.line != 0)
+    {
+      int i;
+      CORE_ADDR addr = prologue_sal.end;
+
+      /* Handle the case in which compiler's optimizer/scheduler
+         has moved instructions into the prologue.  We scan ahead
+        in the function looking for address ranges whose corresponding
+        line number is less than or equal to the first one that we
+        found for the function.  (It can be less than when the
+        scheduler puts a body instruction before the first prologue
+        instruction.)  */
+      for (i = 2 * max_skip_non_prologue_insns; 
+           i > 0 && (lim_pc == 0 || addr < lim_pc);
+          i--)
+        {
+         struct symtab_and_line sal;
+
+         sal = find_pc_line (addr, 0);
+         if (sal.line == 0)
+           break;
+         if (sal.line <= prologue_sal.line 
+             && sal.symtab == prologue_sal.symtab)
+           {
+             prologue_sal = sal;
+           }
+         addr = sal.end;
+       }
+
+      if (lim_pc == 0 || prologue_sal.end < lim_pc)
+       lim_pc = prologue_sal.end;
+    }
+  return lim_pc;
+}
+
+
 static CORE_ADDR
 skip_prologue (CORE_ADDR pc, CORE_ADDR lim_pc, struct rs6000_framedata *fdata)
 {
   CORE_ADDR orig_pc = pc;
-  CORE_ADDR last_prologue_pc;
+  CORE_ADDR last_prologue_pc = pc;
   char buf[4];
   unsigned long op;
   long offset = 0;
@@ -395,6 +450,22 @@ skip_prologue (CORE_ADDR pc, CORE_ADDR lim_pc, struct rs6000_framedata *fdata)
   int framep = 0;
   int minimal_toc_loaded = 0;
   int prev_insn_was_prologue_insn = 1;
+  int num_skip_non_prologue_insns = 0;
+
+  /* Attempt to find the end of the prologue when no limit is specified.
+     Note that refine_prologue_limit() has been written so that it may
+     be used to "refine" the limits of non-zero PC values too, but this
+     is only safe if we 1) trust the line information provided by the
+     compiler and 2) iterate enough to actually find the end of the
+     prologue.  
+     
+     It may become a good idea at some point (for both performance and
+     accuracy) to unconditionally call refine_prologue_limit().  But,
+     until we can make a clear determination that this is beneficial,
+     we'll play it safe and only use it to obtain a limit when none
+     has been specified.  */
+  if (lim_pc == 0)
+    lim_pc = refine_prologue_limit (pc, lim_pc);
 
   memset (fdata, 0, sizeof (struct rs6000_framedata));
   fdata->saved_gpr = -1;
@@ -403,19 +474,22 @@ skip_prologue (CORE_ADDR pc, CORE_ADDR lim_pc, struct rs6000_framedata *fdata)
   fdata->frameless = 1;
   fdata->nosavedpc = 1;
 
-  pc -= 4;
-  while (lim_pc == 0 || pc < lim_pc - 4)
+  for (;; pc += 4)
     {
-      pc += 4;
-
       /* Sometimes it isn't clear if an instruction is a prologue
          instruction or not.  When we encounter one of these ambiguous
         cases, we'll set prev_insn_was_prologue_insn to 0 (false).
         Otherwise, we'll assume that it really is a prologue instruction. */
       if (prev_insn_was_prologue_insn)
        last_prologue_pc = pc;
+
+      /* Stop scanning if we've hit the limit.  */
+      if (lim_pc != 0 && pc >= lim_pc)
+       break;
+
       prev_insn_was_prologue_insn = 1;
 
+      /* Fetch the instruction and convert it to an integer.  */
       if (target_read_memory (pc, buf, 4))
        break;
       op = extract_signed_integer (buf, 4);
@@ -629,7 +703,31 @@ skip_prologue (CORE_ADDR pc, CORE_ADDR lim_pc, struct rs6000_framedata *fdata)
        }
       else
        {
-         break;
+         /* Not a recognized prologue instruction.
+            Handle optimizer code motions into the prologue by continuing
+            the search if we have no valid frame yet or if the return
+            address is not yet saved in the frame.  */
+         if (fdata->frameless == 0
+             && (lr_reg == -1 || fdata->nosavedpc == 0))
+           break;
+
+         if (op == 0x4e800020          /* blr */
+             || op == 0x4e800420)      /* bctr */
+           /* Do not scan past epilogue in frameless functions or
+              trampolines.  */
+           break;
+         if ((op & 0xf4000000) == 0x40000000) /* bxx */
+           /* Never skip branches. */
+           break;
+
+         if (num_skip_non_prologue_insns++ > max_skip_non_prologue_insns)
+           /* Do not scan too many insns, scanning insns is expensive with
+              remote targets.  */
+           break;
+
+         /* Continue scanning.  */
+         prev_insn_was_prologue_insn = 0;
+         continue;
        }
     }
 
@@ -656,7 +754,7 @@ skip_prologue (CORE_ADDR pc, CORE_ADDR lim_pc, struct rs6000_framedata *fdata)
             function as well. */
 
          tmp = find_pc_misc_function (pc);
-         if (tmp >= 0 && STREQ (misc_function_vector[tmp].name, "main"))
+         if (tmp >= 0 && STREQ (misc_function_vector[tmp].name, main_name ()))
            return pc + 8;
        }
     }
@@ -748,7 +846,7 @@ rs6000_pop_frame (void)
 
 static void
 rs6000_fix_call_dummy (char *dummyname, CORE_ADDR pc, CORE_ADDR fun,
-                      int nargs, value_ptr *args, struct type *type,
+                      int nargs, struct value **args, struct type *type,
                       int gcc_p)
 {
 #define        TOC_ADDR_OFFSET         20
@@ -781,7 +879,7 @@ rs6000_fix_call_dummy (char *dummyname, CORE_ADDR pc, CORE_ADDR fun,
    starting from r4. */
 
 static CORE_ADDR
-rs6000_push_arguments (int nargs, value_ptr *args, CORE_ADDR sp,
+rs6000_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
                       int struct_return, CORE_ADDR struct_addr)
 {
   int ii;
@@ -792,7 +890,7 @@ rs6000_push_arguments (int nargs, value_ptr *args, CORE_ADDR sp,
   int f_argno = 0;             /* current floating point argno */
   int wordsize = TDEP->wordsize;
 
-  value_ptr arg = 0;
+  struct value *arg = 0;
   struct type *type;
 
   CORE_ADDR saved_sp;
@@ -911,7 +1009,7 @@ ran_out_of_registers_for_arguments:
 
       for (; jj < nargs; ++jj)
        {
-         value_ptr val = args[jj];
+         struct value *val = args[jj];
          space += ((TYPE_LENGTH (VALUE_TYPE (val))) + 3) & -4;
        }
 
@@ -1035,19 +1133,55 @@ rs6000_extract_return_value (struct type *valtype, char *regbuf, char *valbuf)
 
 static CORE_ADDR rs6000_struct_return_address;
 
-/* Indirect function calls use a piece of trampoline code to do context
-   switching, i.e. to set the new TOC table. Skip such code if we are on
-   its first instruction (as when we have single-stepped to here). 
-   Also skip shared library trampoline code (which is different from
+/* Return whether handle_inferior_event() should proceed through code
+   starting at PC in function NAME when stepping.
+
+   The AIX -bbigtoc linker option generates functions @FIX0, @FIX1, etc. to
+   handle memory references that are too distant to fit in instructions
+   generated by the compiler.  For example, if 'foo' in the following
+   instruction:
+
+     lwz r9,foo(r2)
+
+   is greater than 32767, the linker might replace the lwz with a branch to
+   somewhere in @FIX1 that does the load in 2 instructions and then branches
+   back to where execution should continue.
+
+   GDB should silently step over @FIX code, just like AIX dbx does.
+   Unfortunately, the linker uses the "b" instruction for the branches,
+   meaning that the link register doesn't get set.  Therefore, GDB's usual
+   step_over_function() mechanism won't work.
+
+   Instead, use the IN_SOLIB_RETURN_TRAMPOLINE and SKIP_TRAMPOLINE_CODE hooks
+   in handle_inferior_event() to skip past @FIX code.  */
+
+int
+rs6000_in_solib_return_trampoline (CORE_ADDR pc, char *name)
+{
+  return name && !strncmp (name, "@FIX", 4);
+}
+
+/* Skip code that the user doesn't want to see when stepping:
+
+   1. Indirect function calls use a piece of trampoline code to do context
+   switching, i.e. to set the new TOC table.  Skip such code if we are on
+   its first instruction (as when we have single-stepped to here).
+
+   2. Skip shared library trampoline code (which is different from
    indirect function call trampolines).
+
+   3. Skip bigtoc fixup code.
+
    Result is desired PC to step until, or NULL if we are not in
-   trampoline code.  */
+   code that should be skipped.  */
 
 CORE_ADDR
 rs6000_skip_trampoline_code (CORE_ADDR pc)
 {
   register unsigned int ii, op;
+  int rel;
   CORE_ADDR solib_target_pc;
+  struct minimal_symbol *msymbol;
 
   static unsigned trampoline_code[] =
   {
@@ -1061,6 +1195,21 @@ rs6000_skip_trampoline_code (CORE_ADDR pc)
     0
   };
 
+  /* Check for bigtoc fixup code.  */
+  msymbol = lookup_minimal_symbol_by_pc (pc);
+  if (msymbol && rs6000_in_solib_return_trampoline (pc, SYMBOL_NAME (msymbol)))
+    {
+      /* Double-check that the third instruction from PC is relative "b".  */
+      op = read_memory_integer (pc + 8, 4);
+      if ((op & 0xfc000003) == 0x48000000)
+       {
+         /* Extract bits 6-29 as a signed 24-bit relative word address and
+            add it to the containing PC.  */
+         rel = ((int)(op << 6) >> 6);
+         return pc + 8 + rel;
+       }
+    }
+
   /* If pc is in a shared library trampoline, return its target.  */
   solib_target_pc = find_solib_trampoline_target (pc);
   if (solib_target_pc)
@@ -1945,8 +2094,9 @@ process_note_abi_tag_sections (bfd *abfd, asection *sect, void *obj)
              *os_ident_ptr = ELFOSABI_SOLARIS;
              break;
            default :
-             internal_error (
-               "process_note_abi_sections: unknown OS number %d", os_number);
+             internal_error (__FILE__, __LINE__,
+                             "process_note_abi_sections: unknown OS number %d",
+                             os_number);
              break;
            }
        }
@@ -2013,8 +2163,8 @@ rs6000_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
 
   osabi = get_elfosabi (info.abfd);
 
-  /* Check word size.  If INFO is from a binary file, infer it from that,
-     else use the previously-inferred size. */
+  /* Check word size.  If INFO is from a binary file, infer it from
+     that, else choose a likely default. */
   if (from_xcoff_exec)
     {
       if (xcoff_data (info.abfd)->xcoff64)
@@ -2031,11 +2181,7 @@ rs6000_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
     }
   else
     {
-      tdep = TDEP;
-      if (tdep)
-       wordsize = tdep->wordsize;
-      else
-       wordsize = 4;
+      wordsize = 4;
     }
 
   /* Find a candidate among extant architectures. */
@@ -2061,7 +2207,7 @@ rs6000_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
 
   if (!from_xcoff_exec)
     {
-      arch = info.bfd_architecture;
+      arch = info.bfd_arch_info->arch;
       mach = info.bfd_arch_info->mach;
     }
   else