gdb/hppa-hpux-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for HP-UX on PA-RISC.

  2.    Copyright (C) 2002-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include "gdbcore.h"
  17. #include "osabi.h"
  18. #include "frame.h"
  19. #include "frame-unwind.h"
  20. #include "trad-frame.h"
  21. #include "symtab.h"
  22. #include "objfiles.h"
  23. #include "inferior.h"
  24. #include "infcall.h"
  25. #include "observer.h"
  26. #include "hppa-tdep.h"
  27. #include "solib-som.h"
  28. #include "solib-pa64.h"
  29. #include "regset.h"
  30. #include "regcache.h"

  31. #define IS_32BIT_TARGET(_gdbarch) \
  32.         ((gdbarch_tdep (_gdbarch))->bytes_per_address == 4)

  33. /* Bit in the `ss_flag' member of `struct save_state' that indicates
  34.    that the 64-bit register values are live.  From
  35.    <machine/save_state.h>.  */
  36. #define HPPA_HPUX_SS_WIDEREGS                0x40

  37. /* Offsets of various parts of `struct save_state'.  From
  38.    <machine/save_state.h>.  */
  39. #define HPPA_HPUX_SS_FLAGS_OFFSET        0
  40. #define HPPA_HPUX_SS_NARROW_OFFSET        4
  41. #define HPPA_HPUX_SS_FPBLOCK_OFFSET         256
  42. #define HPPA_HPUX_SS_WIDE_OFFSET        640

  43. /* The size of `struct save_state.  */
  44. #define HPPA_HPUX_SAVE_STATE_SIZE        1152

  45. /* The size of `struct pa89_save_state', which corresponds to PA-RISC
  46.    1.1, the lowest common denominator that we support.  */
  47. #define HPPA_HPUX_PA89_SAVE_STATE_SIZE        512


  48. /* Forward declarations.  */
  49. extern void _initialize_hppa_hpux_tdep (void);
  50. extern initialize_file_ftype _initialize_hppa_hpux_tdep;

  51. /* Return one if PC is in the call path of a trampoline, else return zero.

  52.    Note we return one for *any* call trampoline (long-call, arg-reloc), not
  53.    just shared library trampolines (import, export).  */

  54. static int
  55. hppa32_hpux_in_solib_call_trampoline (struct gdbarch *gdbarch, CORE_ADDR pc)
  56. {
  57.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  58.   struct bound_minimal_symbol minsym;
  59.   struct unwind_table_entry *u;

  60.   /* First see if PC is in one of the two C-library trampolines.  */
  61.   if (pc == hppa_symbol_address("$$dyncall")
  62.       || pc == hppa_symbol_address("_sr4export"))
  63.     return 1;

  64.   minsym = lookup_minimal_symbol_by_pc (pc);
  65.   if (minsym.minsym
  66.       && strcmp (MSYMBOL_LINKAGE_NAME (minsym.minsym), ".stub") == 0)
  67.     return 1;

  68.   /* Get the unwind descriptor corresponding to PC, return zero
  69.      if no unwind was found.  */
  70.   u = find_unwind_entry (pc);
  71.   if (!u)
  72.     return 0;

  73.   /* If this isn't a linker stub, then return now.  */
  74.   if (u->stub_unwind.stub_type == 0)
  75.     return 0;

  76.   /* By definition a long-branch stub is a call stub.  */
  77.   if (u->stub_unwind.stub_type == LONG_BRANCH)
  78.     return 1;

  79.   /* The call and return path execute the same instructions within
  80.      an IMPORT stub!  So an IMPORT stub is both a call and return
  81.      trampoline.  */
  82.   if (u->stub_unwind.stub_type == IMPORT)
  83.     return 1;

  84.   /* Parameter relocation stubs always have a call path and may have a
  85.      return path.  */
  86.   if (u->stub_unwind.stub_type == PARAMETER_RELOCATION
  87.       || u->stub_unwind.stub_type == EXPORT)
  88.     {
  89.       CORE_ADDR addr;

  90.       /* Search forward from the current PC until we hit a branch
  91.          or the end of the stub.  */
  92.       for (addr = pc; addr <= u->region_end; addr += 4)
  93.         {
  94.           unsigned long insn;

  95.           insn = read_memory_integer (addr, 4, byte_order);

  96.           /* Does it look like a bl?  If so then it's the call path, if
  97.              we find a bv or be first, then we're on the return path.  */
  98.           if ((insn & 0xfc00e000) == 0xe8000000)
  99.             return 1;
  100.           else if ((insn & 0xfc00e001) == 0xe800c000
  101.                    || (insn & 0xfc000000) == 0xe0000000)
  102.             return 0;
  103.         }

  104.       /* Should never happen.  */
  105.       warning (_("Unable to find branch in parameter relocation stub."));
  106.       return 0;
  107.     }

  108.   /* Unknown stub type.  For now, just return zero.  */
  109.   return 0;
  110. }

  111. static int
  112. hppa64_hpux_in_solib_call_trampoline (struct gdbarch *gdbarch, CORE_ADDR pc)
  113. {
  114.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  115.   /* PA64 has a completely different stub/trampoline scheme.  Is it
  116.      better?  Maybe.  It's certainly harder to determine with any
  117.      certainty that we are in a stub because we can not refer to the
  118.      unwinders to help.

  119.      The heuristic is simple.  Try to lookup the current PC value in th
  120.      minimal symbol table.  If that fails, then assume we are not in a
  121.      stub and return.

  122.      Then see if the PC value falls within the section bounds for the
  123.      section containing the minimal symbol we found in the first
  124.      step.  If it does, then assume we are not in a stub and return.

  125.      Finally peek at the instructions to see if they look like a stub.  */
  126.   struct bound_minimal_symbol minsym;
  127.   asection *sec;
  128.   CORE_ADDR addr;
  129.   int insn;

  130.   minsym = lookup_minimal_symbol_by_pc (pc);
  131.   if (! minsym.minsym)
  132.     return 0;

  133.   sec = MSYMBOL_OBJ_SECTION (minsym.objfile, minsym.minsym)->the_bfd_section;

  134.   if (bfd_get_section_vma (sec->owner, sec) <= pc
  135.       && pc < (bfd_get_section_vma (sec->owner, sec)
  136.                  + bfd_section_size (sec->owner, sec)))
  137.       return 0;

  138.   /* We might be in a stub.  Peek at the instructions.  Stubs are 3
  139.      instructions long.  */
  140.   insn = read_memory_integer (pc, 4, byte_order);

  141.   /* Find out where we think we are within the stub.  */
  142.   if ((insn & 0xffffc00e) == 0x53610000)
  143.     addr = pc;
  144.   else if ((insn & 0xffffffff) == 0xe820d000)
  145.     addr = pc - 4;
  146.   else if ((insn & 0xffffc00e) == 0x537b0000)
  147.     addr = pc - 8;
  148.   else
  149.     return 0;

  150.   /* Now verify each insn in the range looks like a stub instruction.  */
  151.   insn = read_memory_integer (addr, 4, byte_order);
  152.   if ((insn & 0xffffc00e) != 0x53610000)
  153.     return 0;

  154.   /* Now verify each insn in the range looks like a stub instruction.  */
  155.   insn = read_memory_integer (addr + 4, 4, byte_order);
  156.   if ((insn & 0xffffffff) != 0xe820d000)
  157.     return 0;

  158.   /* Now verify each insn in the range looks like a stub instruction.  */
  159.   insn = read_memory_integer (addr + 8, 4, byte_order);
  160.   if ((insn & 0xffffc00e) != 0x537b0000)
  161.     return 0;

  162.   /* Looks like a stub.  */
  163.   return 1;
  164. }

  165. /* Return one if PC is in the return path of a trampoline, else return zero.

  166.    Note we return one for *any* call trampoline (long-call, arg-reloc), not
  167.    just shared library trampolines (import, export).  */

  168. static int
  169. hppa_hpux_in_solib_return_trampoline (struct gdbarch *gdbarch,
  170.                                       CORE_ADDR pc, const char *name)
  171. {
  172.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  173.   struct unwind_table_entry *u;

  174.   /* Get the unwind descriptor corresponding to PC, return zero
  175.      if no unwind was found.  */
  176.   u = find_unwind_entry (pc);
  177.   if (!u)
  178.     return 0;

  179.   /* If this isn't a linker stub or it's just a long branch stub, then
  180.      return zero.  */
  181.   if (u->stub_unwind.stub_type == 0 || u->stub_unwind.stub_type == LONG_BRANCH)
  182.     return 0;

  183.   /* The call and return path execute the same instructions within
  184.      an IMPORT stub!  So an IMPORT stub is both a call and return
  185.      trampoline.  */
  186.   if (u->stub_unwind.stub_type == IMPORT)
  187.     return 1;

  188.   /* Parameter relocation stubs always have a call path and may have a
  189.      return path.  */
  190.   if (u->stub_unwind.stub_type == PARAMETER_RELOCATION
  191.       || u->stub_unwind.stub_type == EXPORT)
  192.     {
  193.       CORE_ADDR addr;

  194.       /* Search forward from the current PC until we hit a branch
  195.          or the end of the stub.  */
  196.       for (addr = pc; addr <= u->region_end; addr += 4)
  197.         {
  198.           unsigned long insn;

  199.           insn = read_memory_integer (addr, 4, byte_order);

  200.           /* Does it look like a bl?  If so then it's the call path, if
  201.              we find a bv or be first, then we're on the return path.  */
  202.           if ((insn & 0xfc00e000) == 0xe8000000)
  203.             return 0;
  204.           else if ((insn & 0xfc00e001) == 0xe800c000
  205.                    || (insn & 0xfc000000) == 0xe0000000)
  206.             return 1;
  207.         }

  208.       /* Should never happen.  */
  209.       warning (_("Unable to find branch in parameter relocation stub."));
  210.       return 0;
  211.     }

  212.   /* Unknown stub type.  For now, just return zero.  */
  213.   return 0;

  214. }

  215. /* Figure out if PC is in a trampoline, and if so find out where
  216.    the trampoline will jump to.  If not in a trampoline, return zero.

  217.    Simple code examination probably is not a good idea since the code
  218.    sequences in trampolines can also appear in user code.

  219.    We use unwinds and information from the minimal symbol table to
  220.    determine when we're in a trampoline.  This won't work for ELF
  221.    (yet) since it doesn't create stub unwind entries.  Whether or
  222.    not ELF will create stub unwinds or normal unwinds for linker
  223.    stubs is still being debated.

  224.    This should handle simple calls through dyncall or sr4export,
  225.    long calls, argument relocation stubs, and dyncall/sr4export
  226.    calling an argument relocation stub.  It even handles some stubs
  227.    used in dynamic executables.  */

  228. static CORE_ADDR
  229. hppa_hpux_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
  230. {
  231.   struct gdbarch *gdbarch = get_frame_arch (frame);
  232.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  233.   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
  234.   long orig_pc = pc;
  235.   long prev_inst, curr_inst, loc;
  236.   struct bound_minimal_symbol msym;
  237.   struct unwind_table_entry *u;

  238.   /* Addresses passed to dyncall may *NOT* be the actual address
  239.      of the function.  So we may have to do something special.  */
  240.   if (pc == hppa_symbol_address("$$dyncall"))
  241.     {
  242.       pc = (CORE_ADDR) get_frame_register_unsigned (frame, 22);

  243.       /* If bit 30 (counting from the left) is on, then pc is the address of
  244.          the PLT entry for this function, not the address of the function
  245.          itself.  Bit 31 has meaning too, but only for MPE.  */
  246.       if (pc & 0x2)
  247.         pc = (CORE_ADDR) read_memory_integer (pc & ~0x3, word_size,
  248.                                               byte_order);
  249.     }
  250.   if (pc == hppa_symbol_address("$$dyncall_external"))
  251.     {
  252.       pc = (CORE_ADDR) get_frame_register_unsigned (frame, 22);
  253.       pc = (CORE_ADDR) read_memory_integer (pc & ~0x3, word_size, byte_order);
  254.     }
  255.   else if (pc == hppa_symbol_address("_sr4export"))
  256.     pc = (CORE_ADDR) get_frame_register_unsigned (frame, 22);

  257.   /* Get the unwind descriptor corresponding to PC, return zero
  258.      if no unwind was found.  */
  259.   u = find_unwind_entry (pc);
  260.   if (!u)
  261.     return 0;

  262.   /* If this isn't a linker stub, then return now.  */
  263.   /* elz: attention here! (FIXME) because of a compiler/linker
  264.      error, some stubs which should have a non zero stub_unwind.stub_type
  265.      have unfortunately a value of zero.  So this function would return here
  266.      as if we were not in a trampoline.  To fix this, we go look at the partial
  267.      symbol information, which reports this guy as a stub.
  268.      (FIXME): Unfortunately, we are not that lucky: it turns out that the
  269.      partial symbol information is also wrong sometimes.  This is because
  270.      when it is entered (somread.c::som_symtab_read()) it can happen that
  271.      if the type of the symbol (from the som) is Entry, and the symbol is
  272.      in a shared library, then it can also be a trampoline.  This would be OK,
  273.      except that I believe the way they decide if we are ina shared library
  274.      does not work.  SOOOO..., even if we have a regular function w/o
  275.      trampolines its minimal symbol can be assigned type mst_solib_trampoline.
  276.      Also, if we find that the symbol is a real stub, then we fix the unwind
  277.      descriptor, and define the stub type to be EXPORT.
  278.      Hopefully this is correct most of the times.  */
  279.   if (u->stub_unwind.stub_type == 0)
  280.     {

  281. /* elz: NOTE (FIXME!) once the problem with the unwind information is fixed
  282.    we can delete all the code which appears between the lines.  */
  283. /*--------------------------------------------------------------------------*/
  284.       msym = lookup_minimal_symbol_by_pc (pc);

  285.       if (msym.minsym == NULL
  286.           || MSYMBOL_TYPE (msym.minsym) != mst_solib_trampoline)
  287.         return orig_pc == pc ? 0 : pc & ~0x3;

  288.       else if (msym.minsym != NULL
  289.                && MSYMBOL_TYPE (msym.minsym) == mst_solib_trampoline)
  290.         {
  291.           struct objfile *objfile;
  292.           struct minimal_symbol *msymbol;
  293.           int function_found = 0;

  294.           /* Go look if there is another minimal symbol with the same name as
  295.              this one, but with type mst_text.  This would happen if the msym
  296.              is an actual trampoline, in which case there would be another
  297.              symbol with the same name corresponding to the real function.  */

  298.           ALL_MSYMBOLS (objfile, msymbol)
  299.           {
  300.             if (MSYMBOL_TYPE (msymbol) == mst_text
  301.                 && strcmp (MSYMBOL_LINKAGE_NAME (msymbol),
  302.                            MSYMBOL_LINKAGE_NAME (msym.minsym)) == 0)
  303.               {
  304.                 function_found = 1;
  305.                 break;
  306.               }
  307.           }

  308.           if (function_found)
  309.             /* The type of msym is correct (mst_solib_trampoline), but
  310.                the unwind info is wrong, so set it to the correct value.  */
  311.             u->stub_unwind.stub_type = EXPORT;
  312.           else
  313.             /* The stub type info in the unwind is correct (this is not a
  314.                trampoline), but the msym type information is wrong, it
  315.                should be mst_text.  So we need to fix the msym, and also
  316.                get out of this function.  */
  317.             {
  318.               MSYMBOL_TYPE (msym.minsym) = mst_text;
  319.               return orig_pc == pc ? 0 : pc & ~0x3;
  320.             }
  321.         }

  322. /*--------------------------------------------------------------------------*/
  323.     }

  324.   /* It's a stub.  Search for a branch and figure out where it goes.
  325.      Note we have to handle multi insn branch sequences like ldil;ble.
  326.      Most (all?) other branches can be determined by examining the contents
  327.      of certain registers and the stack.  */

  328.   loc = pc;
  329.   curr_inst = 0;
  330.   prev_inst = 0;
  331.   while (1)
  332.     {
  333.       /* Make sure we haven't walked outside the range of this stub.  */
  334.       if (u != find_unwind_entry (loc))
  335.         {
  336.           warning (_("Unable to find branch in linker stub"));
  337.           return orig_pc == pc ? 0 : pc & ~0x3;
  338.         }

  339.       prev_inst = curr_inst;
  340.       curr_inst = read_memory_integer (loc, 4, byte_order);

  341.       /* Does it look like a branch external using %r1?  Then it's the
  342.          branch from the stub to the actual function.  */
  343.       if ((curr_inst & 0xffe0e000) == 0xe0202000)
  344.         {
  345.           /* Yup.  See if the previous instruction loaded
  346.              a value into %r1.  If so compute and return the jump address.  */
  347.           if ((prev_inst & 0xffe00000) == 0x20200000)
  348.             return (hppa_extract_21 (prev_inst)
  349.                     + hppa_extract_17 (curr_inst)) & ~0x3;
  350.           else
  351.             {
  352.               warning (_("Unable to find ldil X,%%r1 "
  353.                          "before ble Y(%%sr4,%%r1)."));
  354.               return orig_pc == pc ? 0 : pc & ~0x3;
  355.             }
  356.         }

  357.       /* Does it look like a be 0(sr0,%r21)? OR
  358.          Does it look like a be, n 0(sr0,%r21)? OR
  359.          Does it look like a bve (r21)? (this is on PA2.0)
  360.          Does it look like a bve, n(r21)? (this is also on PA2.0)
  361.          That's the branch from an
  362.          import stub to an export stub.

  363.          It is impossible to determine the target of the branch via
  364.          simple examination of instructions and/or data (consider
  365.          that the address in the plabel may be the address of the
  366.          bind-on-reference routine in the dynamic loader).

  367.          So we have try an alternative approach.

  368.          Get the name of the symbol at our current location; it should
  369.          be a stub symbol with the same name as the symbol in the
  370.          shared library.

  371.          Then lookup a minimal symbol with the same name; we should
  372.          get the minimal symbol for the target routine in the shared
  373.          library as those take precedence of import/export stubs.  */
  374.       if ((curr_inst == 0xe2a00000) ||
  375.           (curr_inst == 0xe2a00002) ||
  376.           (curr_inst == 0xeaa0d000) ||
  377.           (curr_inst == 0xeaa0d002))
  378.         {
  379.           struct bound_minimal_symbol stubsym;
  380.           struct bound_minimal_symbol libsym;

  381.           stubsym = lookup_minimal_symbol_by_pc (loc);
  382.           if (stubsym.minsym == NULL)
  383.             {
  384.               warning (_("Unable to find symbol for 0x%lx"), loc);
  385.               return orig_pc == pc ? 0 : pc & ~0x3;
  386.             }

  387.           libsym = lookup_minimal_symbol (MSYMBOL_LINKAGE_NAME (stubsym.minsym),
  388.                                           NULL, NULL);
  389.           if (libsym.minsym == NULL)
  390.             {
  391.               warning (_("Unable to find library symbol for %s."),
  392.                        MSYMBOL_PRINT_NAME (stubsym.minsym));
  393.               return orig_pc == pc ? 0 : pc & ~0x3;
  394.             }

  395.           return MSYMBOL_VALUE (libsym.minsym);
  396.         }

  397.       /* Does it look like bl X,%rp or bl X,%r0?  Another way to do a
  398.          branch from the stub to the actual function.  */
  399.       /*elz */
  400.       else if ((curr_inst & 0xffe0e000) == 0xe8400000
  401.                || (curr_inst & 0xffe0e000) == 0xe8000000
  402.                || (curr_inst & 0xffe0e000) == 0xe800A000)
  403.         return (loc + hppa_extract_17 (curr_inst) + 8) & ~0x3;

  404.       /* Does it look like bv (rp)?   Note this depends on the
  405.          current stack pointer being the same as the stack
  406.          pointer in the stub itself!  This is a branch on from the
  407.          stub back to the original caller.  */
  408.       /*else if ((curr_inst & 0xffe0e000) == 0xe840c000) */
  409.       else if ((curr_inst & 0xffe0f000) == 0xe840c000)
  410.         {
  411.           /* Yup.  See if the previous instruction loaded
  412.              rp from sp - 8.  */
  413.           if (prev_inst == 0x4bc23ff1)
  414.             {
  415.               CORE_ADDR sp;
  416.               sp = get_frame_register_unsigned (frame, HPPA_SP_REGNUM);
  417.               return read_memory_integer (sp - 8, 4, byte_order) & ~0x3;
  418.             }
  419.           else
  420.             {
  421.               warning (_("Unable to find restore of %%rp before bv (%%rp)."));
  422.               return orig_pc == pc ? 0 : pc & ~0x3;
  423.             }
  424.         }

  425.       /* elz: added this case to capture the new instruction
  426.          at the end of the return part of an export stub used by
  427.          the PA2.0: BVE, n (rp) */
  428.       else if ((curr_inst & 0xffe0f000) == 0xe840d000)
  429.         {
  430.           return (read_memory_integer
  431.                   (get_frame_register_unsigned (frame, HPPA_SP_REGNUM) - 24,
  432.                    word_size, byte_order)) & ~0x3;
  433.         }

  434.       /* What about be,n 0(sr0,%rp)?  It's just another way we return to
  435.          the original caller from the stub.  Used in dynamic executables.  */
  436.       else if (curr_inst == 0xe0400002)
  437.         {
  438.           /* The value we jump to is sitting in sp - 24.  But that's
  439.              loaded several instructions before the be instruction.
  440.              I guess we could check for the previous instruction being
  441.              mtsp %r1,%sr0 if we want to do sanity checking.  */
  442.           return (read_memory_integer
  443.                   (get_frame_register_unsigned (frame, HPPA_SP_REGNUM) - 24,
  444.                    word_size, byte_order)) & ~0x3;
  445.         }

  446.       /* Haven't found the branch yet, but we're still in the stub.
  447.          Keep looking.  */
  448.       loc += 4;
  449.     }
  450. }

  451. static void
  452. hppa_skip_permanent_breakpoint (struct regcache *regcache)
  453. {
  454.   /* To step over a breakpoint instruction on the PA takes some
  455.      fiddling with the instruction address queue.

  456.      When we stop at a breakpoint, the IA queue front (the instruction
  457.      we're executing now) points at the breakpoint instruction, and
  458.      the IA queue back (the next instruction to execute) points to
  459.      whatever instruction we would execute after the breakpoint, if it
  460.      were an ordinary instruction.  This is the case even if the
  461.      breakpoint is in the delay slot of a branch instruction.

  462.      Clearly, to step past the breakpoint, we need to set the queue
  463.      front to the back.  But what do we put in the back?  What
  464.      instruction comes after that one?  Because of the branch delay
  465.      slot, the next insn is always at the back + 4.  */

  466.   ULONGEST pcoq_tail, pcsq_tail;
  467.   regcache_cooked_read_unsigned (regcache, HPPA_PCOQ_TAIL_REGNUM, &pcoq_tail);
  468.   regcache_cooked_read_unsigned (regcache, HPPA_PCSQ_TAIL_REGNUM, &pcsq_tail);

  469.   regcache_cooked_write_unsigned (regcache, HPPA_PCOQ_HEAD_REGNUM, pcoq_tail);
  470.   regcache_cooked_write_unsigned (regcache, HPPA_PCSQ_HEAD_REGNUM, pcsq_tail);

  471.   regcache_cooked_write_unsigned (regcache,
  472.                                   HPPA_PCOQ_TAIL_REGNUM, pcoq_tail + 4);
  473.   /* We can leave the tail's space the same, since there's no jump.  */
  474. }


  475. /* Signal frames.  */
  476. struct hppa_hpux_sigtramp_unwind_cache
  477. {
  478.   CORE_ADDR base;
  479.   struct trad_frame_saved_reg *saved_regs;
  480. };

  481. static int hppa_hpux_tramp_reg[] = {
  482.   HPPA_SAR_REGNUM,
  483.   HPPA_PCOQ_HEAD_REGNUM,
  484.   HPPA_PCSQ_HEAD_REGNUM,
  485.   HPPA_PCOQ_TAIL_REGNUM,
  486.   HPPA_PCSQ_TAIL_REGNUM,
  487.   HPPA_EIEM_REGNUM,
  488.   HPPA_IIR_REGNUM,
  489.   HPPA_ISR_REGNUM,
  490.   HPPA_IOR_REGNUM,
  491.   HPPA_IPSW_REGNUM,
  492.   -1,
  493.   HPPA_SR4_REGNUM,
  494.   HPPA_SR4_REGNUM + 1,
  495.   HPPA_SR4_REGNUM + 2,
  496.   HPPA_SR4_REGNUM + 3,
  497.   HPPA_SR4_REGNUM + 4,
  498.   HPPA_SR4_REGNUM + 5,
  499.   HPPA_SR4_REGNUM + 6,
  500.   HPPA_SR4_REGNUM + 7,
  501.   HPPA_RCR_REGNUM,
  502.   HPPA_PID0_REGNUM,
  503.   HPPA_PID1_REGNUM,
  504.   HPPA_CCR_REGNUM,
  505.   HPPA_PID2_REGNUM,
  506.   HPPA_PID3_REGNUM,
  507.   HPPA_TR0_REGNUM,
  508.   HPPA_TR0_REGNUM + 1,
  509.   HPPA_TR0_REGNUM + 2,
  510.   HPPA_CR27_REGNUM
  511. };

  512. static struct hppa_hpux_sigtramp_unwind_cache *
  513. hppa_hpux_sigtramp_frame_unwind_cache (struct frame_info *this_frame,
  514.                                        void **this_cache)

  515. {
  516.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  517.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  518.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  519.   struct hppa_hpux_sigtramp_unwind_cache *info;
  520.   unsigned int flag;
  521.   CORE_ADDR sp, scptr, off;
  522.   int i, incr, szoff;

  523.   if (*this_cache)
  524.     return *this_cache;

  525.   info = FRAME_OBSTACK_ZALLOC (struct hppa_hpux_sigtramp_unwind_cache);
  526.   *this_cache = info;
  527.   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  528.   sp = get_frame_register_unsigned (this_frame, HPPA_SP_REGNUM);

  529.   if (IS_32BIT_TARGET (gdbarch))
  530.     scptr = sp - 1352;
  531.   else
  532.     scptr = sp - 1520;

  533.   off = scptr;

  534.   /* See /usr/include/machine/save_state.h for the structure of the
  535.      save_state_t structure.  */

  536.   flag = read_memory_unsigned_integer (scptr + HPPA_HPUX_SS_FLAGS_OFFSET,
  537.                                        4, byte_order);

  538.   if (!(flag & HPPA_HPUX_SS_WIDEREGS))
  539.     {
  540.       /* Narrow registers.  */
  541.       off = scptr + HPPA_HPUX_SS_NARROW_OFFSET;
  542.       incr = 4;
  543.       szoff = 0;
  544.     }
  545.   else
  546.     {
  547.       /* Wide registers.  */
  548.       off = scptr + HPPA_HPUX_SS_WIDE_OFFSET + 8;
  549.       incr = 8;
  550.       szoff = (tdep->bytes_per_address == 4 ? 4 : 0);
  551.     }

  552.   for (i = 1; i < 32; i++)
  553.     {
  554.       info->saved_regs[HPPA_R0_REGNUM + i].addr = off + szoff;
  555.       off += incr;
  556.     }

  557.   for (i = 0; i < ARRAY_SIZE (hppa_hpux_tramp_reg); i++)
  558.     {
  559.       if (hppa_hpux_tramp_reg[i] > 0)
  560.         info->saved_regs[hppa_hpux_tramp_reg[i]].addr = off + szoff;

  561.       off += incr;
  562.     }

  563.   /* TODO: fp regs */

  564.   info->base = get_frame_register_unsigned (this_frame, HPPA_SP_REGNUM);

  565.   return info;
  566. }

  567. static void
  568. hppa_hpux_sigtramp_frame_this_id (struct frame_info *this_frame,
  569.                                    void **this_prologue_cache,
  570.                                    struct frame_id *this_id)
  571. {
  572.   struct hppa_hpux_sigtramp_unwind_cache *info
  573.     = hppa_hpux_sigtramp_frame_unwind_cache (this_frame, this_prologue_cache);

  574.   *this_id = frame_id_build (info->base, get_frame_pc (this_frame));
  575. }

  576. static struct value *
  577. hppa_hpux_sigtramp_frame_prev_register (struct frame_info *this_frame,
  578.                                         void **this_prologue_cache,
  579.                                         int regnum)
  580. {
  581.   struct hppa_hpux_sigtramp_unwind_cache *info
  582.     = hppa_hpux_sigtramp_frame_unwind_cache (this_frame, this_prologue_cache);

  583.   return hppa_frame_prev_register_helper (this_frame,
  584.                                           info->saved_regs, regnum);
  585. }

  586. static int
  587. hppa_hpux_sigtramp_unwind_sniffer (const struct frame_unwind *self,
  588.                                    struct frame_info *this_frame,
  589.                                    void **this_cache)
  590. {
  591.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  592.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  593.   struct unwind_table_entry *u;
  594.   CORE_ADDR pc = get_frame_pc (this_frame);

  595.   u = find_unwind_entry (pc);

  596.   /* If this is an export stub, try to get the unwind descriptor for
  597.      the actual function itself.  */
  598.   if (u && u->stub_unwind.stub_type == EXPORT)
  599.     {
  600.       gdb_byte buf[HPPA_INSN_SIZE];
  601.       unsigned long insn;

  602.       if (!safe_frame_unwind_memory (this_frame, u->region_start,
  603.                                      buf, sizeof buf))
  604.         return 0;

  605.       insn = extract_unsigned_integer (buf, sizeof buf, byte_order);
  606.       if ((insn & 0xffe0e000) == 0xe8400000)
  607.         u = find_unwind_entry(u->region_start + hppa_extract_17 (insn) + 8);
  608.     }

  609.   if (u && u->HP_UX_interrupt_marker)
  610.     return 1;

  611.   return 0;
  612. }

  613. static const struct frame_unwind hppa_hpux_sigtramp_frame_unwind = {
  614.   SIGTRAMP_FRAME,
  615.   default_frame_unwind_stop_reason,
  616.   hppa_hpux_sigtramp_frame_this_id,
  617.   hppa_hpux_sigtramp_frame_prev_register,
  618.   NULL,
  619.   hppa_hpux_sigtramp_unwind_sniffer
  620. };

  621. static CORE_ADDR
  622. hppa32_hpux_find_global_pointer (struct gdbarch *gdbarch,
  623.                                  struct value *function)
  624. {
  625.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  626.   CORE_ADDR faddr;

  627.   faddr = value_as_address (function);

  628.   /* Is this a plabel? If so, dereference it to get the gp value.  */
  629.   if (faddr & 2)
  630.     {
  631.       int status;
  632.       gdb_byte buf[4];

  633.       faddr &= ~3;

  634.       status = target_read_memory (faddr + 4, buf, sizeof (buf));
  635.       if (status == 0)
  636.         return extract_unsigned_integer (buf, sizeof (buf), byte_order);
  637.     }

  638.   return gdbarch_tdep (gdbarch)->solib_get_got_by_pc (faddr);
  639. }

  640. static CORE_ADDR
  641. hppa64_hpux_find_global_pointer (struct gdbarch *gdbarch,
  642.                                  struct value *function)
  643. {
  644.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  645.   CORE_ADDR faddr;
  646.   gdb_byte buf[32];

  647.   faddr = value_as_address (function);

  648.   if (pc_in_section (faddr, ".opd"))
  649.     {
  650.       target_read_memory (faddr, buf, sizeof (buf));
  651.       return extract_unsigned_integer (&buf[24], 8, byte_order);
  652.     }
  653.   else
  654.     {
  655.       return gdbarch_tdep (gdbarch)->solib_get_got_by_pc (faddr);
  656.     }
  657. }

  658. static unsigned int ldsid_pattern[] = {
  659.   0x000010a0, /* ldsid (rX),rY */
  660.   0x00001820, /* mtsp rY,sr0 */
  661.   0xe0000000  /* be,n (sr0,rX) */
  662. };

  663. static CORE_ADDR
  664. hppa_hpux_search_pattern (struct gdbarch *gdbarch,
  665.                           CORE_ADDR start, CORE_ADDR end,
  666.                           unsigned int *patterns, int count)
  667. {
  668.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  669.   int num_insns = (end - start + HPPA_INSN_SIZE) / HPPA_INSN_SIZE;
  670.   unsigned int *insns;
  671.   gdb_byte *buf;
  672.   int offset, i;

  673.   buf = alloca (num_insns * HPPA_INSN_SIZE);
  674.   insns = alloca (num_insns * sizeof (unsigned int));

  675.   read_memory (start, buf, num_insns * HPPA_INSN_SIZE);
  676.   for (i = 0; i < num_insns; i++, buf += HPPA_INSN_SIZE)
  677.     insns[i] = extract_unsigned_integer (buf, HPPA_INSN_SIZE, byte_order);

  678.   for (offset = 0; offset <= num_insns - count; offset++)
  679.     {
  680.       for (i = 0; i < count; i++)
  681.         {
  682.           if ((insns[offset + i] & patterns[i]) != patterns[i])
  683.             break;
  684.         }
  685.       if (i == count)
  686.         break;
  687.     }

  688.   if (offset <= num_insns - count)
  689.     return start + offset * HPPA_INSN_SIZE;
  690.   else
  691.     return 0;
  692. }

  693. static CORE_ADDR
  694. hppa32_hpux_search_dummy_call_sequence (struct gdbarch *gdbarch, CORE_ADDR pc,
  695.                                         int *argreg)
  696. {
  697.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  698.   struct objfile *obj;
  699.   struct obj_section *sec;
  700.   struct hppa_objfile_private *priv;
  701.   struct frame_info *frame;
  702.   struct unwind_table_entry *u;
  703.   CORE_ADDR addr, rp;
  704.   gdb_byte buf[4];
  705.   unsigned int insn;

  706.   sec = find_pc_section (pc);
  707.   obj = sec->objfile;
  708.   priv = objfile_data (obj, hppa_objfile_priv_data);

  709.   if (!priv)
  710.     priv = hppa_init_objfile_priv_data (obj);
  711.   if (!priv)
  712.     error (_("Internal error creating objfile private data."));

  713.   /* Use the cached value if we have one.  */
  714.   if (priv->dummy_call_sequence_addr != 0)
  715.     {
  716.       *argreg = priv->dummy_call_sequence_reg;
  717.       return priv->dummy_call_sequence_addr;
  718.     }

  719.   /* First try a heuristic; if we are in a shared library call, our return
  720.      pointer is likely to point at an export stub.  */
  721.   frame = get_current_frame ();
  722.   rp = frame_unwind_register_unsigned (frame, 2);
  723.   u = find_unwind_entry (rp);
  724.   if (u && u->stub_unwind.stub_type == EXPORT)
  725.     {
  726.       addr = hppa_hpux_search_pattern (gdbarch,
  727.                                        u->region_start, u->region_end,
  728.                                        ldsid_pattern,
  729.                                        ARRAY_SIZE (ldsid_pattern));
  730.       if (addr)
  731.         goto found_pattern;
  732.     }

  733.   /* Next thing to try is to look for an export stub.  */
  734.   if (priv->unwind_info)
  735.     {
  736.       int i;

  737.       for (i = 0; i < priv->unwind_info->last; i++)
  738.         {
  739.           struct unwind_table_entry *u;
  740.           u = &priv->unwind_info->table[i];
  741.           if (u->stub_unwind.stub_type == EXPORT)
  742.             {
  743.               addr = hppa_hpux_search_pattern (gdbarch,
  744.                                                u->region_start, u->region_end,
  745.                                                ldsid_pattern,
  746.                                                ARRAY_SIZE (ldsid_pattern));
  747.               if (addr)
  748.                 {
  749.                   goto found_pattern;
  750.                 }
  751.             }
  752.         }
  753.     }

  754.   /* Finally, if this is the main executable, try to locate a sequence
  755.      from noshlibs */
  756.   addr = hppa_symbol_address ("noshlibs");
  757.   sec = find_pc_section (addr);

  758.   if (sec && sec->objfile == obj)
  759.     {
  760.       CORE_ADDR start, end;

  761.       find_pc_partial_function (addr, NULL, &start, &end);
  762.       if (start != 0 && end != 0)
  763.         {
  764.           addr = hppa_hpux_search_pattern (gdbarch, start, end, ldsid_pattern,
  765.                                            ARRAY_SIZE (ldsid_pattern));
  766.           if (addr)
  767.             goto found_pattern;
  768.         }
  769.     }

  770.   /* Can't find a suitable sequence.  */
  771.   return 0;

  772. found_pattern:
  773.   target_read_memory (addr, buf, sizeof (buf));
  774.   insn = extract_unsigned_integer (buf, sizeof (buf), byte_order);
  775.   priv->dummy_call_sequence_addr = addr;
  776.   priv->dummy_call_sequence_reg = (insn >> 21) & 0x1f;

  777.   *argreg = priv->dummy_call_sequence_reg;
  778.   return priv->dummy_call_sequence_addr;
  779. }

  780. static CORE_ADDR
  781. hppa64_hpux_search_dummy_call_sequence (struct gdbarch *gdbarch, CORE_ADDR pc,
  782.                                         int *argreg)
  783. {
  784.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  785.   struct objfile *obj;
  786.   struct obj_section *sec;
  787.   struct hppa_objfile_private *priv;
  788.   CORE_ADDR addr;
  789.   struct minimal_symbol *msym;

  790.   sec = find_pc_section (pc);
  791.   obj = sec->objfile;
  792.   priv = objfile_data (obj, hppa_objfile_priv_data);

  793.   if (!priv)
  794.     priv = hppa_init_objfile_priv_data (obj);
  795.   if (!priv)
  796.     error (_("Internal error creating objfile private data."));

  797.   /* Use the cached value if we have one.  */
  798.   if (priv->dummy_call_sequence_addr != 0)
  799.     {
  800.       *argreg = priv->dummy_call_sequence_reg;
  801.       return priv->dummy_call_sequence_addr;
  802.     }

  803.   /* FIXME: Without stub unwind information, locating a suitable sequence is
  804.      fairly difficult.  For now, we implement a very naive and inefficient
  805.      scheme; try to read in blocks of code, and look for a "bve,n (rp)"
  806.      instruction.  These are likely to occur at the end of functions, so
  807.      we only look at the last two instructions of each function.  */
  808.   ALL_OBJFILE_MSYMBOLS (obj, msym)
  809.     {
  810.       CORE_ADDR begin, end;
  811.       const char *name;
  812.       gdb_byte buf[2 * HPPA_INSN_SIZE];
  813.       int offset;

  814.       find_pc_partial_function (MSYMBOL_VALUE_ADDRESS (obj, msym), &name,
  815.                                       &begin, &end);

  816.       if (name == NULL || begin == 0 || end == 0)
  817.         continue;

  818.       if (target_read_memory (end - sizeof (buf), buf, sizeof (buf)) == 0)
  819.         {
  820.           for (offset = 0; offset < sizeof (buf); offset++)
  821.             {
  822.               unsigned int insn;

  823.               insn = extract_unsigned_integer (buf + offset,
  824.                                                HPPA_INSN_SIZE, byte_order);
  825.               if (insn == 0xe840d002) /* bve,n (rp) */
  826.                 {
  827.                   addr = (end - sizeof (buf)) + offset;
  828.                   goto found_pattern;
  829.                 }
  830.             }
  831.         }
  832.     }

  833.   /* Can't find a suitable sequence.  */
  834.   return 0;

  835. found_pattern:
  836.   priv->dummy_call_sequence_addr = addr;
  837.   /* Right now we only look for a "bve,l (rp)" sequence, so the register is
  838.      always HPPA_RP_REGNUM.  */
  839.   priv->dummy_call_sequence_reg = HPPA_RP_REGNUM;

  840.   *argreg = priv->dummy_call_sequence_reg;
  841.   return priv->dummy_call_sequence_addr;
  842. }

  843. static CORE_ADDR
  844. hppa_hpux_find_import_stub_for_addr (CORE_ADDR funcaddr)
  845. {
  846.   struct objfile *objfile;
  847.   struct bound_minimal_symbol funsym;
  848.   struct bound_minimal_symbol stubsym;
  849.   CORE_ADDR stubaddr;

  850.   funsym = lookup_minimal_symbol_by_pc (funcaddr);
  851.   stubaddr = 0;

  852.   ALL_OBJFILES (objfile)
  853.     {
  854.       stubsym = lookup_minimal_symbol_solib_trampoline
  855.         (MSYMBOL_LINKAGE_NAME (funsym.minsym), objfile);

  856.       if (stubsym.minsym)
  857.         {
  858.           struct unwind_table_entry *u;

  859.           u = find_unwind_entry (MSYMBOL_VALUE (stubsym.minsym));
  860.           if (u == NULL
  861.               || (u->stub_unwind.stub_type != IMPORT
  862.                   && u->stub_unwind.stub_type != IMPORT_SHLIB))
  863.             continue;

  864.           stubaddr = MSYMBOL_VALUE (stubsym.minsym);

  865.           /* If we found an IMPORT stub, then we can stop searching;
  866.              if we found an IMPORT_SHLIB, we want to continue the search
  867.              in the hopes that we will find an IMPORT stub.  */
  868.           if (u->stub_unwind.stub_type == IMPORT)
  869.             break;
  870.         }
  871.     }

  872.   return stubaddr;
  873. }

  874. static int
  875. hppa_hpux_sr_for_addr (struct gdbarch *gdbarch, CORE_ADDR addr)
  876. {
  877.   int sr;
  878.   /* The space register to use is encoded in the top 2 bits of the address.  */
  879.   sr = addr >> (gdbarch_tdep (gdbarch)->bytes_per_address * 8 - 2);
  880.   return sr + 4;
  881. }

  882. static CORE_ADDR
  883. hppa_hpux_find_dummy_bpaddr (CORE_ADDR addr)
  884. {
  885.   /* In order for us to restore the space register to its starting state,
  886.      we need the dummy trampoline to return to an instruction address in
  887.      the same space as where we started the call.  We used to place the
  888.      breakpoint near the current pc, however, this breaks nested dummy calls
  889.      as the nested call will hit the breakpoint address and terminate
  890.      prematurely.  Instead, we try to look for an address in the same space to
  891.      put the breakpoint.

  892.      This is similar in spirit to putting the breakpoint at the "entry point"
  893.      of an executable.  */

  894.   struct obj_section *sec;
  895.   struct unwind_table_entry *u;
  896.   struct minimal_symbol *msym;
  897.   CORE_ADDR func;

  898.   sec = find_pc_section (addr);
  899.   if (sec)
  900.     {
  901.       /* First try the lowest address in the section; we can use it as long
  902.          as it is "regular" code (i.e. not a stub).  */
  903.       u = find_unwind_entry (obj_section_addr (sec));
  904.       if (!u || u->stub_unwind.stub_type == 0)
  905.         return obj_section_addr (sec);

  906.       /* Otherwise, we need to find a symbol for a regular function.  We
  907.          do this by walking the list of msymbols in the objfile.  The symbol
  908.          we find should not be the same as the function that was passed in.  */

  909.       /* FIXME: this is broken, because we can find a function that will be
  910.          called by the dummy call target function, which will still not
  911.          work.  */

  912.       find_pc_partial_function (addr, NULL, &func, NULL);
  913.       ALL_OBJFILE_MSYMBOLS (sec->objfile, msym)
  914.         {
  915.           u = find_unwind_entry (MSYMBOL_VALUE_ADDRESS (sec->objfile, msym));
  916.           if (func != MSYMBOL_VALUE_ADDRESS (sec->objfile, msym)
  917.               && (!u || u->stub_unwind.stub_type == 0))
  918.             return MSYMBOL_VALUE_ADDRESS (sec->objfile, msym);
  919.         }
  920.     }

  921.   warning (_("Cannot find suitable address to place dummy breakpoint; nested "
  922.              "calls may fail."));
  923.   return addr - 4;
  924. }

  925. static CORE_ADDR
  926. hppa_hpux_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
  927.                            CORE_ADDR funcaddr,
  928.                            struct value **args, int nargs,
  929.                            struct type *value_type,
  930.                            CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
  931.                            struct regcache *regcache)
  932. {
  933.   CORE_ADDR pc, stubaddr;
  934.   int argreg = 0;

  935.   pc = regcache_read_pc (regcache);

  936.   /* Note: we don't want to pass a function descriptor here; push_dummy_call
  937.      fills in the PIC register for us.  */
  938.   funcaddr = gdbarch_convert_from_func_ptr_addr (gdbarch, funcaddr, NULL);

  939.   /* The simple case is where we call a function in the same space that we are
  940.      currently in; in that case we don't really need to do anything.  */
  941.   if (hppa_hpux_sr_for_addr (gdbarch, pc)
  942.       == hppa_hpux_sr_for_addr (gdbarch, funcaddr))
  943.     {
  944.       /* Intraspace call.  */
  945.       *bp_addr = hppa_hpux_find_dummy_bpaddr (pc);
  946.       *real_pc = funcaddr;
  947.       regcache_cooked_write_unsigned (regcache, HPPA_RP_REGNUM, *bp_addr);

  948.       return sp;
  949.     }

  950.   /* In order to make an interspace call, we need to go through a stub.
  951.      gcc supplies an appropriate stub called "__gcc_plt_call", however, if
  952.      an application is compiled with HP compilers then this stub is not
  953.      available.  We used to fallback to "__d_plt_call", however that stub
  954.      is not entirely useful for us because it doesn't do an interspace
  955.      return back to the caller.  Also, on hppa64-hpux, there is no
  956.      __gcc_plt_call available.  In order to keep the code uniform, we
  957.      instead don't use either of these stubs, but instead write our own
  958.      onto the stack.

  959.      A problem arises since the stack is located in a different space than
  960.      code, so in order to branch to a stack stub, we will need to do an
  961.      interspace branch.  Previous versions of gdb did this by modifying code
  962.      at the current pc and doing single-stepping to set the pcsq.  Since this
  963.      is highly undesirable, we use a different scheme:

  964.      All we really need to do the branch to the stub is a short instruction
  965.      sequence like this:

  966.      PA1.1:
  967.                       ldsid (rX),r1
  968.                 mtsp r1,sr0
  969.                 be,n (sr0,rX)

  970.      PA2.0:
  971.                       bve,n (sr0,rX)

  972.      Instead of writing these sequences ourselves, we can find it in
  973.      the instruction stream that belongs to the current space.  While this
  974.      seems difficult at first, we are actually guaranteed to find the sequences
  975.      in several places:

  976.      For 32-bit code:
  977.      - in export stubs for shared libraries
  978.      - in the "noshlibs" routine in the main module

  979.      For 64-bit code:
  980.      - at the end of each "regular" function

  981.      We cache the address of these sequences in the objfile's private data
  982.      since these operations can potentially be quite expensive.

  983.      So, what we do is:
  984.      - write a stack trampoline
  985.      - look for a suitable instruction sequence in the current space
  986.      - point the sequence at the trampoline
  987.      - set the return address of the trampoline to the current space
  988.        (see hppa_hpux_find_dummy_call_bpaddr)
  989.      - set the continuing address of the "dummy code" as the sequence.  */

  990.   if (IS_32BIT_TARGET (gdbarch))
  991.     {
  992. #define INSN(I1, I2, I3, I4) 0x ## I1, 0x ## I2, 0x ## I3, 0x ## I4
  993.      static const gdb_byte hppa32_tramp[] = {
  994.         INSN(0f,df,12,91), /* stw r31,-8(,sp) */
  995.         INSN(02,c0,10,a1), /* ldsid (,r22),r1 */
  996.         INSN(00,01,18,20), /* mtsp r1,sr0 */
  997.         INSN(e6,c0,00,00), /* be,l 0(sr0,r22),%sr0,%r31 */
  998.         INSN(08,1f,02,42), /* copy r31,rp */
  999.         INSN(0f,d1,10,82), /* ldw -8(,sp),rp */
  1000.         INSN(00,40,10,a1), /* ldsid (,rp),r1 */
  1001.         INSN(00,01,18,20), /* mtsp r1,sr0 */
  1002.         INSN(e0,40,00,00), /* be 0(sr0,rp) */
  1003.         INSN(08,00,02,40/* nop */
  1004.       };

  1005.       /* for hppa32, we must call the function through a stub so that on
  1006.          return it can return to the space of our trampoline.  */
  1007.       stubaddr = hppa_hpux_find_import_stub_for_addr (funcaddr);
  1008.       if (stubaddr == 0)
  1009.         error (_("Cannot call external function not referenced by application "
  1010.                "(no import stub).\n"));
  1011.       regcache_cooked_write_unsigned (regcache, 22, stubaddr);

  1012.       write_memory (sp, hppa32_tramp, sizeof (hppa32_tramp));

  1013.       *bp_addr = hppa_hpux_find_dummy_bpaddr (pc);
  1014.       regcache_cooked_write_unsigned (regcache, 31, *bp_addr);

  1015.       *real_pc = hppa32_hpux_search_dummy_call_sequence (gdbarch, pc, &argreg);
  1016.       if (*real_pc == 0)
  1017.         error (_("Cannot make interspace call from here."));

  1018.       regcache_cooked_write_unsigned (regcache, argreg, sp);

  1019.       sp += sizeof (hppa32_tramp);
  1020.     }
  1021.   else
  1022.     {
  1023.       static const gdb_byte hppa64_tramp[] = {
  1024.         INSN(ea,c0,f0,00), /* bve,l (r22),%r2 */
  1025.         INSN(0f,df,12,d1), /* std r31,-8(,sp) */
  1026.         INSN(0f,d1,10,c2), /* ldd -8(,sp),rp */
  1027.         INSN(e8,40,d0,02), /* bve,n (rp) */
  1028.         INSN(08,00,02,40/* nop */
  1029.       };
  1030. #undef INSN

  1031.       /* for hppa64, we don't need to call through a stub; all functions
  1032.          return via a bve.  */
  1033.       regcache_cooked_write_unsigned (regcache, 22, funcaddr);
  1034.       write_memory (sp, hppa64_tramp, sizeof (hppa64_tramp));

  1035.       *bp_addr = pc - 4;
  1036.       regcache_cooked_write_unsigned (regcache, 31, *bp_addr);

  1037.       *real_pc = hppa64_hpux_search_dummy_call_sequence (gdbarch, pc, &argreg);
  1038.       if (*real_pc == 0)
  1039.         error (_("Cannot make interspace call from here."));

  1040.       regcache_cooked_write_unsigned (regcache, argreg, sp);

  1041.       sp += sizeof (hppa64_tramp);
  1042.     }

  1043.   sp = gdbarch_frame_align (gdbarch, sp);

  1044.   return sp;
  1045. }



  1046. static void
  1047. hppa_hpux_supply_ss_narrow (struct regcache *regcache,
  1048.                             int regnum, const gdb_byte *save_state)
  1049. {
  1050.   const gdb_byte *ss_narrow = save_state + HPPA_HPUX_SS_NARROW_OFFSET;
  1051.   int i, offset = 0;

  1052.   for (i = HPPA_R1_REGNUM; i < HPPA_FP0_REGNUM; i++)
  1053.     {
  1054.       if (regnum == i || regnum == -1)
  1055.         regcache_raw_supply (regcache, i, ss_narrow + offset);

  1056.       offset += 4;
  1057.     }
  1058. }

  1059. static void
  1060. hppa_hpux_supply_ss_fpblock (struct regcache *regcache,
  1061.                              int regnum, const gdb_byte *save_state)
  1062. {
  1063.   const gdb_byte *ss_fpblock = save_state + HPPA_HPUX_SS_FPBLOCK_OFFSET;
  1064.   int i, offset = 0;

  1065.   /* FIXME: We view the floating-point state as 64 single-precision
  1066.      registers for 32-bit code, and 32 double-precision register for
  1067.      64-bit code.  This distinction is artificial and should be
  1068.      eliminated.  If that ever happens, we should remove the if-clause
  1069.      below.  */

  1070.   if (register_size (get_regcache_arch (regcache), HPPA_FP0_REGNUM) == 4)
  1071.     {
  1072.       for (i = HPPA_FP0_REGNUM; i < HPPA_FP0_REGNUM + 64; i++)
  1073.         {
  1074.           if (regnum == i || regnum == -1)
  1075.             regcache_raw_supply (regcache, i, ss_fpblock + offset);

  1076.           offset += 4;
  1077.         }
  1078.     }
  1079.   else
  1080.     {
  1081.       for (i = HPPA_FP0_REGNUM; i < HPPA_FP0_REGNUM + 32; i++)
  1082.         {
  1083.           if (regnum == i || regnum == -1)
  1084.             regcache_raw_supply (regcache, i, ss_fpblock + offset);

  1085.           offset += 8;
  1086.         }
  1087.     }
  1088. }

  1089. static void
  1090. hppa_hpux_supply_ss_wide (struct regcache *regcache,
  1091.                           int regnum, const gdb_byte *save_state)
  1092. {
  1093.   const gdb_byte *ss_wide = save_state + HPPA_HPUX_SS_WIDE_OFFSET;
  1094.   int i, offset = 8;

  1095.   if (register_size (get_regcache_arch (regcache), HPPA_R1_REGNUM) == 4)
  1096.     offset += 4;

  1097.   for (i = HPPA_R1_REGNUM; i < HPPA_FP0_REGNUM; i++)
  1098.     {
  1099.       if (regnum == i || regnum == -1)
  1100.         regcache_raw_supply (regcache, i, ss_wide + offset);

  1101.       offset += 8;
  1102.     }
  1103. }

  1104. static void
  1105. hppa_hpux_supply_save_state (const struct regset *regset,
  1106.                              struct regcache *regcache,
  1107.                              int regnum, const void *regs, size_t len)
  1108. {
  1109.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1110.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1111.   const gdb_byte *proc_info = regs;
  1112.   const gdb_byte *save_state = proc_info + 8;
  1113.   ULONGEST flags;

  1114.   flags = extract_unsigned_integer (save_state + HPPA_HPUX_SS_FLAGS_OFFSET,
  1115.                                     4, byte_order);
  1116.   if (regnum == -1 || regnum == HPPA_FLAGS_REGNUM)
  1117.     {
  1118.       size_t size = register_size (gdbarch, HPPA_FLAGS_REGNUM);
  1119.       gdb_byte buf[8];

  1120.       store_unsigned_integer (buf, size, byte_order, flags);
  1121.       regcache_raw_supply (regcache, HPPA_FLAGS_REGNUM, buf);
  1122.     }

  1123.   /* If the SS_WIDEREGS flag is set, we really do need the full
  1124.      `struct save_state'.  */
  1125.   if (flags & HPPA_HPUX_SS_WIDEREGS && len < HPPA_HPUX_SAVE_STATE_SIZE)
  1126.     error (_("Register set contents too small"));

  1127.   if (flags & HPPA_HPUX_SS_WIDEREGS)
  1128.     hppa_hpux_supply_ss_wide (regcache, regnum, save_state);
  1129.   else
  1130.     hppa_hpux_supply_ss_narrow (regcache, regnum, save_state);

  1131.   hppa_hpux_supply_ss_fpblock (regcache, regnum, save_state);
  1132. }

  1133. /* HP-UX register set.  */

  1134. static const struct regset hppa_hpux_regset =
  1135. {
  1136.   NULL,
  1137.   hppa_hpux_supply_save_state
  1138. };

  1139. static void
  1140. hppa_hpux_iterate_over_regset_sections (struct gdbarch *gdbarch,
  1141.                                         iterate_over_regset_sections_cb *cb,
  1142.                                         void *cb_data,
  1143.                                         const struct regcache *regcache)
  1144. {
  1145.   cb (".reg", HPPA_HPUX_PA89_SAVE_STATE_SIZE + 8, &hppa_hpux_regset,
  1146.       NULL, cb_data);
  1147. }


  1148. /* Bit in the `ss_flag' member of `struct save_state' that indicates
  1149.    the state was saved from a system call.  From
  1150.    <machine/save_state.h>.  */
  1151. #define HPPA_HPUX_SS_INSYSCALL        0x02

  1152. static CORE_ADDR
  1153. hppa_hpux_read_pc (struct regcache *regcache)
  1154. {
  1155.   ULONGEST flags;

  1156.   /* If we're currently in a system call return the contents of %r31.  */
  1157.   regcache_cooked_read_unsigned (regcache, HPPA_FLAGS_REGNUM, &flags);
  1158.   if (flags & HPPA_HPUX_SS_INSYSCALL)
  1159.     {
  1160.       ULONGEST pc;
  1161.       regcache_cooked_read_unsigned (regcache, HPPA_R31_REGNUM, &pc);
  1162.       return pc & ~0x3;
  1163.     }

  1164.   return hppa_read_pc (regcache);
  1165. }

  1166. static void
  1167. hppa_hpux_write_pc (struct regcache *regcache, CORE_ADDR pc)
  1168. {
  1169.   ULONGEST flags;

  1170.   /* If we're currently in a system call also write PC into %r31.  */
  1171.   regcache_cooked_read_unsigned (regcache, HPPA_FLAGS_REGNUM, &flags);
  1172.   if (flags & HPPA_HPUX_SS_INSYSCALL)
  1173.     regcache_cooked_write_unsigned (regcache, HPPA_R31_REGNUM, pc | 0x3);

  1174.   hppa_write_pc (regcache, pc);
  1175. }

  1176. static CORE_ADDR
  1177. hppa_hpux_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1178. {
  1179.   ULONGEST flags;

  1180.   /* If we're currently in a system call return the contents of %r31.  */
  1181.   flags = frame_unwind_register_unsigned (next_frame, HPPA_FLAGS_REGNUM);
  1182.   if (flags & HPPA_HPUX_SS_INSYSCALL)
  1183.     return frame_unwind_register_unsigned (next_frame, HPPA_R31_REGNUM) & ~0x3;

  1184.   return hppa_unwind_pc (gdbarch, next_frame);
  1185. }


  1186. /* Given the current value of the pc, check to see if it is inside a stub, and
  1187.    if so, change the value of the pc to point to the caller of the stub.
  1188.    THIS_FRAME is the current frame in the current list of frames.
  1189.    BASE contains to stack frame base of the current frame.
  1190.    SAVE_REGS is the register file stored in the frame cache.  */
  1191. static void
  1192. hppa_hpux_unwind_adjust_stub (struct frame_info *this_frame, CORE_ADDR base,
  1193.                               struct trad_frame_saved_reg *saved_regs)
  1194. {
  1195.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1196.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1197.   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
  1198.   struct value *pcoq_head_val;
  1199.   ULONGEST pcoq_head;
  1200.   CORE_ADDR stubpc;
  1201.   struct unwind_table_entry *u;

  1202.   pcoq_head_val = trad_frame_get_prev_register (this_frame, saved_regs,
  1203.                                                 HPPA_PCOQ_HEAD_REGNUM);
  1204.   pcoq_head =
  1205.     extract_unsigned_integer (value_contents_all (pcoq_head_val),
  1206.                               register_size (gdbarch, HPPA_PCOQ_HEAD_REGNUM),
  1207.                               byte_order);

  1208.   u = find_unwind_entry (pcoq_head);
  1209.   if (u && u->stub_unwind.stub_type == EXPORT)
  1210.     {
  1211.       stubpc = read_memory_integer (base - 24, word_size, byte_order);
  1212.       trad_frame_set_value (saved_regs, HPPA_PCOQ_HEAD_REGNUM, stubpc);
  1213.     }
  1214.   else if (hppa_symbol_address ("__gcc_plt_call")
  1215.            == get_pc_function_start (pcoq_head))
  1216.     {
  1217.       stubpc = read_memory_integer (base - 8, word_size, byte_order);
  1218.       trad_frame_set_value (saved_regs, HPPA_PCOQ_HEAD_REGNUM, stubpc);
  1219.     }
  1220. }

  1221. static void
  1222. hppa_hpux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  1223. {
  1224.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  1225.   if (IS_32BIT_TARGET (gdbarch))
  1226.     tdep->in_solib_call_trampoline = hppa32_hpux_in_solib_call_trampoline;
  1227.   else
  1228.     tdep->in_solib_call_trampoline = hppa64_hpux_in_solib_call_trampoline;

  1229.   tdep->unwind_adjust_stub = hppa_hpux_unwind_adjust_stub;

  1230.   set_gdbarch_in_solib_return_trampoline
  1231.     (gdbarch, hppa_hpux_in_solib_return_trampoline);
  1232.   set_gdbarch_skip_trampoline_code (gdbarch, hppa_hpux_skip_trampoline_code);

  1233.   set_gdbarch_push_dummy_code (gdbarch, hppa_hpux_push_dummy_code);
  1234.   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);

  1235.   set_gdbarch_read_pc (gdbarch, hppa_hpux_read_pc);
  1236.   set_gdbarch_write_pc (gdbarch, hppa_hpux_write_pc);
  1237.   set_gdbarch_unwind_pc (gdbarch, hppa_hpux_unwind_pc);
  1238.   set_gdbarch_skip_permanent_breakpoint
  1239.     (gdbarch, hppa_skip_permanent_breakpoint);

  1240.   set_gdbarch_iterate_over_regset_sections
  1241.     (gdbarch, hppa_hpux_iterate_over_regset_sections);

  1242.   frame_unwind_append_unwinder (gdbarch, &hppa_hpux_sigtramp_frame_unwind);
  1243. }

  1244. static void
  1245. hppa_hpux_som_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  1246. {
  1247.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  1248.   tdep->is_elf = 0;

  1249.   tdep->find_global_pointer = hppa32_hpux_find_global_pointer;

  1250.   hppa_hpux_init_abi (info, gdbarch);
  1251.   som_solib_select (gdbarch);
  1252. }

  1253. static void
  1254. hppa_hpux_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  1255. {
  1256.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  1257.   tdep->is_elf = 1;
  1258.   tdep->find_global_pointer = hppa64_hpux_find_global_pointer;

  1259.   hppa_hpux_init_abi (info, gdbarch);
  1260.   pa64_solib_select (gdbarch);
  1261. }

  1262. static enum gdb_osabi
  1263. hppa_hpux_core_osabi_sniffer (bfd *abfd)
  1264. {
  1265.   if (strcmp (bfd_get_target (abfd), "hpux-core") == 0)
  1266.     return GDB_OSABI_HPUX_SOM;
  1267.   else if (strcmp (bfd_get_target (abfd), "elf64-hppa") == 0)
  1268.     {
  1269.       asection *section;

  1270.       section = bfd_get_section_by_name (abfd, ".kernel");
  1271.       if (section)
  1272.         {
  1273.           bfd_size_type size;
  1274.           char *contents;

  1275.           size = bfd_section_size (abfd, section);
  1276.           contents = alloca (size);
  1277.            if (bfd_get_section_contents (abfd, section, contents,
  1278.                                           (file_ptr) 0, size)
  1279.               && strcmp (contents, "HP-UX") == 0)
  1280.             return GDB_OSABI_HPUX_ELF;
  1281.         }
  1282.     }

  1283.   return GDB_OSABI_UNKNOWN;
  1284. }

  1285. void
  1286. _initialize_hppa_hpux_tdep (void)
  1287. {
  1288.   /* BFD doesn't set a flavour for HP-UX style core files.  It doesn't
  1289.      set the architecture either.  */
  1290.   gdbarch_register_osabi_sniffer (bfd_arch_unknown,
  1291.                                   bfd_target_unknown_flavour,
  1292.                                   hppa_hpux_core_osabi_sniffer);
  1293.   gdbarch_register_osabi_sniffer (bfd_arch_hppa,
  1294.                                   bfd_target_elf_flavour,
  1295.                                   hppa_hpux_core_osabi_sniffer);

  1296.   gdbarch_register_osabi (bfd_arch_hppa, 0, GDB_OSABI_HPUX_SOM,
  1297.                           hppa_hpux_som_init_abi);
  1298.   gdbarch_register_osabi (bfd_arch_hppa, bfd_mach_hppa20w, GDB_OSABI_HPUX_ELF,
  1299.                           hppa_hpux_elf_init_abi);
  1300. }