gdb/hppa-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for the HP PA-RISC architecture.

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

  3.    Contributed by the Center for Software Science at the
  4.    University of Utah (pa-gdb-bugs@cs.utah.edu).

  5.    This file is part of GDB.

  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 3 of the License, or
  9.    (at your option) any later version.

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

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

  16. #include "defs.h"
  17. #include "bfd.h"
  18. #include "inferior.h"
  19. #include "regcache.h"
  20. #include "completer.h"
  21. #include "osabi.h"
  22. #include "arch-utils.h"
  23. /* For argument passing to the inferior.  */
  24. #include "symtab.h"
  25. #include "dis-asm.h"
  26. #include "trad-frame.h"
  27. #include "frame-unwind.h"
  28. #include "frame-base.h"

  29. #include "gdbcore.h"
  30. #include "gdbcmd.h"
  31. #include "gdbtypes.h"
  32. #include "objfiles.h"
  33. #include "hppa-tdep.h"

  34. static int hppa_debug = 0;

  35. /* Some local constants.  */
  36. static const int hppa32_num_regs = 128;
  37. static const int hppa64_num_regs = 96;

  38. /* hppa-specific object data -- unwind and solib info.
  39.    TODO/maybe: think about splitting this into two parts; the unwind data is
  40.    common to all hppa targets, but is only used in this file; we can register
  41.    that separately and make this static. The solib data is probably hpux-
  42.    specific, so we can create a separate extern objfile_data that is registered
  43.    by hppa-hpux-tdep.c and shared with pa64solib.c and somsolib.c.  */
  44. const struct objfile_data *hppa_objfile_priv_data = NULL;

  45. /* Get at various relevent fields of an instruction word.  */
  46. #define MASK_5 0x1f
  47. #define MASK_11 0x7ff
  48. #define MASK_14 0x3fff
  49. #define MASK_21 0x1fffff

  50. /* Sizes (in bytes) of the native unwind entries.  */
  51. #define UNWIND_ENTRY_SIZE 16
  52. #define STUB_UNWIND_ENTRY_SIZE 8

  53. /* Routines to extract various sized constants out of hppa
  54.    instructions.  */

  55. /* This assumes that no garbage lies outside of the lower bits of
  56.    value.  */

  57. static int
  58. hppa_sign_extend (unsigned val, unsigned bits)
  59. {
  60.   return (int) (val >> (bits - 1) ? (-1 << bits) | val : val);
  61. }

  62. /* For many immediate values the sign bit is the low bit!  */

  63. static int
  64. hppa_low_hppa_sign_extend (unsigned val, unsigned bits)
  65. {
  66.   return (int) ((val & 0x1 ? (-1 << (bits - 1)) : 0) | val >> 1);
  67. }

  68. /* Extract the bits at positions between FROM and TO, using HP's numbering
  69.    (MSB = 0).  */

  70. int
  71. hppa_get_field (unsigned word, int from, int to)
  72. {
  73.   return ((word) >> (31 - (to)) & ((1 << ((to) - (from) + 1)) - 1));
  74. }

  75. /* Extract the immediate field from a ld{bhw}s instruction.  */

  76. int
  77. hppa_extract_5_load (unsigned word)
  78. {
  79.   return hppa_low_hppa_sign_extend (word >> 16 & MASK_5, 5);
  80. }

  81. /* Extract the immediate field from a break instruction.  */

  82. unsigned
  83. hppa_extract_5r_store (unsigned word)
  84. {
  85.   return (word & MASK_5);
  86. }

  87. /* Extract the immediate field from a {sr}sm instruction.  */

  88. unsigned
  89. hppa_extract_5R_store (unsigned word)
  90. {
  91.   return (word >> 16 & MASK_5);
  92. }

  93. /* Extract a 14 bit immediate field.  */

  94. int
  95. hppa_extract_14 (unsigned word)
  96. {
  97.   return hppa_low_hppa_sign_extend (word & MASK_14, 14);
  98. }

  99. /* Extract a 21 bit constant.  */

  100. int
  101. hppa_extract_21 (unsigned word)
  102. {
  103.   int val;

  104.   word &= MASK_21;
  105.   word <<= 11;
  106.   val = hppa_get_field (word, 20, 20);
  107.   val <<= 11;
  108.   val |= hppa_get_field (word, 9, 19);
  109.   val <<= 2;
  110.   val |= hppa_get_field (word, 5, 6);
  111.   val <<= 5;
  112.   val |= hppa_get_field (word, 0, 4);
  113.   val <<= 2;
  114.   val |= hppa_get_field (word, 7, 8);
  115.   return hppa_sign_extend (val, 21) << 11;
  116. }

  117. /* extract a 17 bit constant from branch instructions, returning the
  118.    19 bit signed value.  */

  119. int
  120. hppa_extract_17 (unsigned word)
  121. {
  122.   return hppa_sign_extend (hppa_get_field (word, 19, 28) |
  123.                       hppa_get_field (word, 29, 29) << 10 |
  124.                       hppa_get_field (word, 11, 15) << 11 |
  125.                       (word & 0x1) << 16, 17) << 2;
  126. }

  127. CORE_ADDR
  128. hppa_symbol_address(const char *sym)
  129. {
  130.   struct bound_minimal_symbol minsym;

  131.   minsym = lookup_minimal_symbol (sym, NULL, NULL);
  132.   if (minsym.minsym)
  133.     return BMSYMBOL_VALUE_ADDRESS (minsym);
  134.   else
  135.     return (CORE_ADDR)-1;
  136. }

  137. struct hppa_objfile_private *
  138. hppa_init_objfile_priv_data (struct objfile *objfile)
  139. {
  140.   struct hppa_objfile_private *priv;

  141.   priv = (struct hppa_objfile_private *)
  142.            obstack_alloc (&objfile->objfile_obstack,
  143.                          sizeof (struct hppa_objfile_private));
  144.   set_objfile_data (objfile, hppa_objfile_priv_data, priv);
  145.   memset (priv, 0, sizeof (*priv));

  146.   return priv;
  147. }


  148. /* Compare the start address for two unwind entries returning 1 if
  149.    the first address is larger than the second, -1 if the second is
  150.    larger than the first, and zero if they are equal.  */

  151. static int
  152. compare_unwind_entries (const void *arg1, const void *arg2)
  153. {
  154.   const struct unwind_table_entry *a = arg1;
  155.   const struct unwind_table_entry *b = arg2;

  156.   if (a->region_start > b->region_start)
  157.     return 1;
  158.   else if (a->region_start < b->region_start)
  159.     return -1;
  160.   else
  161.     return 0;
  162. }

  163. static void
  164. record_text_segment_lowaddr (bfd *abfd, asection *section, void *data)
  165. {
  166.   if ((section->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
  167.        == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
  168.     {
  169.       bfd_vma value = section->vma - section->filepos;
  170.       CORE_ADDR *low_text_segment_address = (CORE_ADDR *)data;

  171.       if (value < *low_text_segment_address)
  172.           *low_text_segment_address = value;
  173.     }
  174. }

  175. static void
  176. internalize_unwinds (struct objfile *objfile, struct unwind_table_entry *table,
  177.                      asection *section, unsigned int entries,
  178.                      size_t size, CORE_ADDR text_offset)
  179. {
  180.   /* We will read the unwind entries into temporary memory, then
  181.      fill in the actual unwind table.  */

  182.   if (size > 0)
  183.     {
  184.       struct gdbarch *gdbarch = get_objfile_arch (objfile);
  185.       unsigned long tmp;
  186.       unsigned i;
  187.       char *buf = alloca (size);
  188.       CORE_ADDR low_text_segment_address;

  189.       /* For ELF targets, then unwinds are supposed to
  190.          be segment relative offsets instead of absolute addresses.

  191.          Note that when loading a shared library (text_offset != 0) the
  192.          unwinds are already relative to the text_offset that will be
  193.          passed in.  */
  194.       if (gdbarch_tdep (gdbarch)->is_elf && text_offset == 0)
  195.         {
  196.           low_text_segment_address = -1;

  197.           bfd_map_over_sections (objfile->obfd,
  198.                                  record_text_segment_lowaddr,
  199.                                  &low_text_segment_address);

  200.           text_offset = low_text_segment_address;
  201.         }
  202.       else if (gdbarch_tdep (gdbarch)->solib_get_text_base)
  203.         {
  204.           text_offset = gdbarch_tdep (gdbarch)->solib_get_text_base (objfile);
  205.         }

  206.       bfd_get_section_contents (objfile->obfd, section, buf, 0, size);

  207.       /* Now internalize the information being careful to handle host/target
  208.          endian issues.  */
  209.       for (i = 0; i < entries; i++)
  210.         {
  211.           table[i].region_start = bfd_get_32 (objfile->obfd,
  212.                                               (bfd_byte *) buf);
  213.           table[i].region_start += text_offset;
  214.           buf += 4;
  215.           table[i].region_end = bfd_get_32 (objfile->obfd, (bfd_byte *) buf);
  216.           table[i].region_end += text_offset;
  217.           buf += 4;
  218.           tmp = bfd_get_32 (objfile->obfd, (bfd_byte *) buf);
  219.           buf += 4;
  220.           table[i].Cannot_unwind = (tmp >> 31) & 0x1;
  221.           table[i].Millicode = (tmp >> 30) & 0x1;
  222.           table[i].Millicode_save_sr0 = (tmp >> 29) & 0x1;
  223.           table[i].Region_description = (tmp >> 27) & 0x3;
  224.           table[i].reserved = (tmp >> 26) & 0x1;
  225.           table[i].Entry_SR = (tmp >> 25) & 0x1;
  226.           table[i].Entry_FR = (tmp >> 21) & 0xf;
  227.           table[i].Entry_GR = (tmp >> 16) & 0x1f;
  228.           table[i].Args_stored = (tmp >> 15) & 0x1;
  229.           table[i].Variable_Frame = (tmp >> 14) & 0x1;
  230.           table[i].Separate_Package_Body = (tmp >> 13) & 0x1;
  231.           table[i].Frame_Extension_Millicode = (tmp >> 12) & 0x1;
  232.           table[i].Stack_Overflow_Check = (tmp >> 11) & 0x1;
  233.           table[i].Two_Instruction_SP_Increment = (tmp >> 10) & 0x1;
  234.           table[i].sr4export = (tmp >> 9) & 0x1;
  235.           table[i].cxx_info = (tmp >> 8) & 0x1;
  236.           table[i].cxx_try_catch = (tmp >> 7) & 0x1;
  237.           table[i].sched_entry_seq = (tmp >> 6) & 0x1;
  238.           table[i].reserved1 = (tmp >> 5) & 0x1;
  239.           table[i].Save_SP = (tmp >> 4) & 0x1;
  240.           table[i].Save_RP = (tmp >> 3) & 0x1;
  241.           table[i].Save_MRP_in_frame = (tmp >> 2) & 0x1;
  242.           table[i].save_r19 = (tmp >> 1) & 0x1;
  243.           table[i].Cleanup_defined = tmp & 0x1;
  244.           tmp = bfd_get_32 (objfile->obfd, (bfd_byte *) buf);
  245.           buf += 4;
  246.           table[i].MPE_XL_interrupt_marker = (tmp >> 31) & 0x1;
  247.           table[i].HP_UX_interrupt_marker = (tmp >> 30) & 0x1;
  248.           table[i].Large_frame = (tmp >> 29) & 0x1;
  249.           table[i].alloca_frame = (tmp >> 28) & 0x1;
  250.           table[i].reserved2 = (tmp >> 27) & 0x1;
  251.           table[i].Total_frame_size = tmp & 0x7ffffff;

  252.           /* Stub unwinds are handled elsewhere.  */
  253.           table[i].stub_unwind.stub_type = 0;
  254.           table[i].stub_unwind.padding = 0;
  255.         }
  256.     }
  257. }

  258. /* Read in the backtrace information stored in the `$UNWIND_START$' section of
  259.    the object file.  This info is used mainly by find_unwind_entry() to find
  260.    out the stack frame size and frame pointer used by procedures.  We put
  261.    everything on the psymbol obstack in the objfile so that it automatically
  262.    gets freed when the objfile is destroyed.  */

  263. static void
  264. read_unwind_info (struct objfile *objfile)
  265. {
  266.   asection *unwind_sec, *stub_unwind_sec;
  267.   size_t unwind_size, stub_unwind_size, total_size;
  268.   unsigned index, unwind_entries;
  269.   unsigned stub_entries, total_entries;
  270.   CORE_ADDR text_offset;
  271.   struct hppa_unwind_info *ui;
  272.   struct hppa_objfile_private *obj_private;

  273.   text_offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
  274.   ui = (struct hppa_unwind_info *) obstack_alloc (&objfile->objfile_obstack,
  275.                                            sizeof (struct hppa_unwind_info));

  276.   ui->table = NULL;
  277.   ui->cache = NULL;
  278.   ui->last = -1;

  279.   /* For reasons unknown the HP PA64 tools generate multiple unwinder
  280.      sections in a single executable.  So we just iterate over every
  281.      section in the BFD looking for unwinder sections intead of trying
  282.      to do a lookup with bfd_get_section_by_name.

  283.      First determine the total size of the unwind tables so that we
  284.      can allocate memory in a nice big hunk.  */
  285.   total_entries = 0;
  286.   for (unwind_sec = objfile->obfd->sections;
  287.        unwind_sec;
  288.        unwind_sec = unwind_sec->next)
  289.     {
  290.       if (strcmp (unwind_sec->name, "$UNWIND_START$") == 0
  291.           || strcmp (unwind_sec->name, ".PARISC.unwind") == 0)
  292.         {
  293.           unwind_size = bfd_section_size (objfile->obfd, unwind_sec);
  294.           unwind_entries = unwind_size / UNWIND_ENTRY_SIZE;

  295.           total_entries += unwind_entries;
  296.         }
  297.     }

  298.   /* Now compute the size of the stub unwinds.  Note the ELF tools do not
  299.      use stub unwinds at the current time.  */
  300.   stub_unwind_sec = bfd_get_section_by_name (objfile->obfd, "$UNWIND_END$");

  301.   if (stub_unwind_sec)
  302.     {
  303.       stub_unwind_size = bfd_section_size (objfile->obfd, stub_unwind_sec);
  304.       stub_entries = stub_unwind_size / STUB_UNWIND_ENTRY_SIZE;
  305.     }
  306.   else
  307.     {
  308.       stub_unwind_size = 0;
  309.       stub_entries = 0;
  310.     }

  311.   /* Compute total number of unwind entries and their total size.  */
  312.   total_entries += stub_entries;
  313.   total_size = total_entries * sizeof (struct unwind_table_entry);

  314.   /* Allocate memory for the unwind table.  */
  315.   ui->table = (struct unwind_table_entry *)
  316.     obstack_alloc (&objfile->objfile_obstack, total_size);
  317.   ui->last = total_entries - 1;

  318.   /* Now read in each unwind section and internalize the standard unwind
  319.      entries.  */
  320.   index = 0;
  321.   for (unwind_sec = objfile->obfd->sections;
  322.        unwind_sec;
  323.        unwind_sec = unwind_sec->next)
  324.     {
  325.       if (strcmp (unwind_sec->name, "$UNWIND_START$") == 0
  326.           || strcmp (unwind_sec->name, ".PARISC.unwind") == 0)
  327.         {
  328.           unwind_size = bfd_section_size (objfile->obfd, unwind_sec);
  329.           unwind_entries = unwind_size / UNWIND_ENTRY_SIZE;

  330.           internalize_unwinds (objfile, &ui->table[index], unwind_sec,
  331.                                unwind_entries, unwind_size, text_offset);
  332.           index += unwind_entries;
  333.         }
  334.     }

  335.   /* Now read in and internalize the stub unwind entries.  */
  336.   if (stub_unwind_size > 0)
  337.     {
  338.       unsigned int i;
  339.       char *buf = alloca (stub_unwind_size);

  340.       /* Read in the stub unwind entries.  */
  341.       bfd_get_section_contents (objfile->obfd, stub_unwind_sec, buf,
  342.                                 0, stub_unwind_size);

  343.       /* Now convert them into regular unwind entries.  */
  344.       for (i = 0; i < stub_entries; i++, index++)
  345.         {
  346.           /* Clear out the next unwind entry.  */
  347.           memset (&ui->table[index], 0, sizeof (struct unwind_table_entry));

  348.           /* Convert offset & size into region_start and region_end.
  349.              Stuff away the stub type into "reserved" fields.  */
  350.           ui->table[index].region_start = bfd_get_32 (objfile->obfd,
  351.                                                       (bfd_byte *) buf);
  352.           ui->table[index].region_start += text_offset;
  353.           buf += 4;
  354.           ui->table[index].stub_unwind.stub_type = bfd_get_8 (objfile->obfd,
  355.                                                           (bfd_byte *) buf);
  356.           buf += 2;
  357.           ui->table[index].region_end
  358.             = ui->table[index].region_start + 4 *
  359.             (bfd_get_16 (objfile->obfd, (bfd_byte *) buf) - 1);
  360.           buf += 2;
  361.         }

  362.     }

  363.   /* Unwind table needs to be kept sorted.  */
  364.   qsort (ui->table, total_entries, sizeof (struct unwind_table_entry),
  365.          compare_unwind_entries);

  366.   /* Keep a pointer to the unwind information.  */
  367.   obj_private = (struct hppa_objfile_private *)
  368.                 objfile_data (objfile, hppa_objfile_priv_data);
  369.   if (obj_private == NULL)
  370.     obj_private = hppa_init_objfile_priv_data (objfile);

  371.   obj_private->unwind_info = ui;
  372. }

  373. /* Lookup the unwind (stack backtrace) info for the given PC.  We search all
  374.    of the objfiles seeking the unwind table entry for this PC.  Each objfile
  375.    contains a sorted list of struct unwind_table_entry.  Since we do a binary
  376.    search of the unwind tables, we depend upon them to be sorted.  */

  377. struct unwind_table_entry *
  378. find_unwind_entry (CORE_ADDR pc)
  379. {
  380.   int first, middle, last;
  381.   struct objfile *objfile;
  382.   struct hppa_objfile_private *priv;

  383.   if (hppa_debug)
  384.     fprintf_unfiltered (gdb_stdlog, "{ find_unwind_entry %s -> ",
  385.                         hex_string (pc));

  386.   /* A function at address 0?  Not in HP-UX!  */
  387.   if (pc == (CORE_ADDR) 0)
  388.     {
  389.       if (hppa_debug)
  390.         fprintf_unfiltered (gdb_stdlog, "NULL }\n");
  391.       return NULL;
  392.     }

  393.   ALL_OBJFILES (objfile)
  394.   {
  395.     struct hppa_unwind_info *ui;
  396.     ui = NULL;
  397.     priv = objfile_data (objfile, hppa_objfile_priv_data);
  398.     if (priv)
  399.       ui = ((struct hppa_objfile_private *) priv)->unwind_info;

  400.     if (!ui)
  401.       {
  402.         read_unwind_info (objfile);
  403.         priv = objfile_data (objfile, hppa_objfile_priv_data);
  404.         if (priv == NULL)
  405.           error (_("Internal error reading unwind information."));
  406.         ui = ((struct hppa_objfile_private *) priv)->unwind_info;
  407.       }

  408.     /* First, check the cache.  */

  409.     if (ui->cache
  410.         && pc >= ui->cache->region_start
  411.         && pc <= ui->cache->region_end)
  412.       {
  413.         if (hppa_debug)
  414.           fprintf_unfiltered (gdb_stdlog, "%s (cached) }\n",
  415.             hex_string ((uintptr_t) ui->cache));
  416.         return ui->cache;
  417.       }

  418.     /* Not in the cache, do a binary search.  */

  419.     first = 0;
  420.     last = ui->last;

  421.     while (first <= last)
  422.       {
  423.         middle = (first + last) / 2;
  424.         if (pc >= ui->table[middle].region_start
  425.             && pc <= ui->table[middle].region_end)
  426.           {
  427.             ui->cache = &ui->table[middle];
  428.             if (hppa_debug)
  429.               fprintf_unfiltered (gdb_stdlog, "%s }\n",
  430.                 hex_string ((uintptr_t) ui->cache));
  431.             return &ui->table[middle];
  432.           }

  433.         if (pc < ui->table[middle].region_start)
  434.           last = middle - 1;
  435.         else
  436.           first = middle + 1;
  437.       }
  438.   }                                /* ALL_OBJFILES() */

  439.   if (hppa_debug)
  440.     fprintf_unfiltered (gdb_stdlog, "NULL (not found) }\n");

  441.   return NULL;
  442. }

  443. /* The epilogue is defined here as the area either on the `bv' instruction
  444.    itself or an instruction which destroys the function's stack frame.

  445.    We do not assume that the epilogue is at the end of a function as we can
  446.    also have return sequences in the middle of a function.  */
  447. static int
  448. hppa_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  449. {
  450.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  451.   unsigned long status;
  452.   unsigned int inst;
  453.   gdb_byte buf[4];

  454.   status = target_read_memory (pc, buf, 4);
  455.   if (status != 0)
  456.     return 0;

  457.   inst = extract_unsigned_integer (buf, 4, byte_order);

  458.   /* The most common way to perform a stack adjustment ldo X(sp),sp
  459.      We are destroying a stack frame if the offset is negative.  */
  460.   if ((inst & 0xffffc000) == 0x37de0000
  461.       && hppa_extract_14 (inst) < 0)
  462.     return 1;

  463.   /* ldw,mb D(sp),X or ldd,mb D(sp),X */
  464.   if (((inst & 0x0fc010e0) == 0x0fc010e0
  465.        || (inst & 0x0fc010e0) == 0x0fc010e0)
  466.       && hppa_extract_14 (inst) < 0)
  467.     return 1;

  468.   /* bv %r0(%rp) or bv,n %r0(%rp) */
  469.   if (inst == 0xe840c000 || inst == 0xe840c002)
  470.     return 1;

  471.   return 0;
  472. }

  473. static const unsigned char *
  474. hppa_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
  475. {
  476.   static const unsigned char breakpoint[] = {0x00, 0x01, 0x00, 0x04};
  477.   (*len) = sizeof (breakpoint);
  478.   return breakpoint;
  479. }

  480. /* Return the name of a register.  */

  481. static const char *
  482. hppa32_register_name (struct gdbarch *gdbarch, int i)
  483. {
  484.   static char *names[] = {
  485.     "flags""r1",      "rp",     "r3",
  486.     "r4",     "r5",      "r6",     "r7",
  487.     "r8",     "r9",      "r10",    "r11",
  488.     "r12",    "r13",     "r14",    "r15",
  489.     "r16",    "r17",     "r18",    "r19",
  490.     "r20",    "r21",     "r22",    "r23",
  491.     "r24",    "r25",     "r26",    "dp",
  492.     "ret0",   "ret1",    "sp",     "r31",
  493.     "sar",    "pcoqh",   "pcsqh""pcoqt",
  494.     "pcsqt""eiem",    "iir",    "isr",
  495.     "ior",    "ipsw",    "goto",   "sr4",
  496.     "sr0",    "sr1",     "sr2",    "sr3",
  497.     "sr5",    "sr6",     "sr7",    "cr0",
  498.     "cr8",    "cr9",     "ccr",    "cr12",
  499.     "cr13",   "cr24",    "cr25",   "cr26",
  500.     "mpsfu_high","mpsfu_low","mpsfu_ovflo","pad",
  501.     "fpsr",    "fpe1",   "fpe2",   "fpe3",
  502.     "fpe4",   "fpe5",    "fpe6",   "fpe7",
  503.     "fr4",     "fr4R",   "fr5",    "fr5R",
  504.     "fr6",    "fr6R",    "fr7",    "fr7R",
  505.     "fr8",     "fr8R",   "fr9",    "fr9R",
  506.     "fr10",   "fr10R",   "fr11",   "fr11R",
  507.     "fr12",    "fr12R""fr13",   "fr13R",
  508.     "fr14",   "fr14R",   "fr15",   "fr15R",
  509.     "fr16",    "fr16R""fr17",   "fr17R",
  510.     "fr18",   "fr18R",   "fr19",   "fr19R",
  511.     "fr20",    "fr20R""fr21",   "fr21R",
  512.     "fr22",   "fr22R",   "fr23",   "fr23R",
  513.     "fr24",    "fr24R""fr25",   "fr25R",
  514.     "fr26",   "fr26R",   "fr27",   "fr27R",
  515.     "fr28",    "fr28R""fr29",   "fr29R",
  516.     "fr30",   "fr30R",   "fr31",   "fr31R"
  517.   };
  518.   if (i < 0 || i >= (sizeof (names) / sizeof (*names)))
  519.     return NULL;
  520.   else
  521.     return names[i];
  522. }

  523. static const char *
  524. hppa64_register_name (struct gdbarch *gdbarch, int i)
  525. {
  526.   static char *names[] = {
  527.     "flags""r1",      "rp",     "r3",
  528.     "r4",     "r5",      "r6",     "r7",
  529.     "r8",     "r9",      "r10",    "r11",
  530.     "r12",    "r13",     "r14",    "r15",
  531.     "r16",    "r17",     "r18",    "r19",
  532.     "r20",    "r21",     "r22",    "r23",
  533.     "r24",    "r25",     "r26",    "dp",
  534.     "ret0",   "ret1",    "sp",     "r31",
  535.     "sar",    "pcoqh",   "pcsqh""pcoqt",
  536.     "pcsqt""eiem",    "iir",    "isr",
  537.     "ior",    "ipsw",    "goto",   "sr4",
  538.     "sr0",    "sr1",     "sr2",    "sr3",
  539.     "sr5",    "sr6",     "sr7",    "cr0",
  540.     "cr8",    "cr9",     "ccr",    "cr12",
  541.     "cr13",   "cr24",    "cr25",   "cr26",
  542.     "mpsfu_high","mpsfu_low","mpsfu_ovflo","pad",
  543.     "fpsr",    "fpe1",   "fpe2",   "fpe3",
  544.     "fr4",    "fr5",     "fr6",    "fr7",
  545.     "fr8",     "fr9",    "fr10",   "fr11",
  546.     "fr12",   "fr13",    "fr14",   "fr15",
  547.     "fr16",    "fr17",   "fr18",   "fr19",
  548.     "fr20",   "fr21",    "fr22",   "fr23",
  549.     "fr24",    "fr25",   "fr26",   "fr27",
  550.     "fr28""fr29",    "fr30",   "fr31"
  551.   };
  552.   if (i < 0 || i >= (sizeof (names) / sizeof (*names)))
  553.     return NULL;
  554.   else
  555.     return names[i];
  556. }

  557. /* Map dwarf DBX register numbers to GDB register numbers.  */
  558. static int
  559. hppa64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  560. {
  561.   /* The general registers and the sar are the same in both sets.  */
  562.   if (reg <= 32)
  563.     return reg;

  564.   /* fr4-fr31 are mapped from 72 in steps of 2.  */
  565.   if (reg >= 72 && reg < 72 + 28 * 2 && !(reg & 1))
  566.     return HPPA64_FP4_REGNUM + (reg - 72) / 2;

  567.   warning (_("Unmapped DWARF DBX Register #%d encountered."), reg);
  568.   return -1;
  569. }

  570. /* This function pushes a stack frame with arguments as part of the
  571.    inferior function calling mechanism.

  572.    This is the version of the function for the 32-bit PA machines, in
  573.    which later arguments appear at lower addresses.  (The stack always
  574.    grows towards higher addresses.)

  575.    We simply allocate the appropriate amount of stack space and put
  576.    arguments into their proper slots.  */

  577. static CORE_ADDR
  578. hppa32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  579.                         struct regcache *regcache, CORE_ADDR bp_addr,
  580.                         int nargs, struct value **args, CORE_ADDR sp,
  581.                         int struct_return, CORE_ADDR struct_addr)
  582. {
  583.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  584.   /* Stack base address at which any pass-by-reference parameters are
  585.      stored.  */
  586.   CORE_ADDR struct_end = 0;
  587.   /* Stack base address at which the first parameter is stored.  */
  588.   CORE_ADDR param_end = 0;

  589.   /* The inner most end of the stack after all the parameters have
  590.      been pushed.  */
  591.   CORE_ADDR new_sp = 0;

  592.   /* Two passes.  First pass computes the location of everything,
  593.      second pass writes the bytes out.  */
  594.   int write_pass;

  595.   /* Global pointer (r19) of the function we are trying to call.  */
  596.   CORE_ADDR gp;

  597.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  598.   for (write_pass = 0; write_pass < 2; write_pass++)
  599.     {
  600.       CORE_ADDR struct_ptr = 0;
  601.       /* The first parameter goes into sp-36, each stack slot is 4-bytes.
  602.          struct_ptr is adjusted for each argument below, so the first
  603.          argument will end up at sp-36.  */
  604.       CORE_ADDR param_ptr = 32;
  605.       int i;
  606.       int small_struct = 0;

  607.       for (i = 0; i < nargs; i++)
  608.         {
  609.           struct value *arg = args[i];
  610.           struct type *type = check_typedef (value_type (arg));
  611.           /* The corresponding parameter that is pushed onto the
  612.              stack, and [possibly] passed in a register.  */
  613.           gdb_byte param_val[8];
  614.           int param_len;
  615.           memset (param_val, 0, sizeof param_val);
  616.           if (TYPE_LENGTH (type) > 8)
  617.             {
  618.               /* Large parameter, pass by reference.  Store the value
  619.                  in "struct" area and then pass its address.  */
  620.               param_len = 4;
  621.               struct_ptr += align_up (TYPE_LENGTH (type), 8);
  622.               if (write_pass)
  623.                 write_memory (struct_end - struct_ptr, value_contents (arg),
  624.                               TYPE_LENGTH (type));
  625.               store_unsigned_integer (param_val, 4, byte_order,
  626.                                       struct_end - struct_ptr);
  627.             }
  628.           else if (TYPE_CODE (type) == TYPE_CODE_INT
  629.                    || TYPE_CODE (type) == TYPE_CODE_ENUM)
  630.             {
  631.               /* Integer value store, right aligned.  "unpack_long"
  632.                  takes care of any sign-extension problems.  */
  633.               param_len = align_up (TYPE_LENGTH (type), 4);
  634.               store_unsigned_integer (param_val, param_len, byte_order,
  635.                                       unpack_long (type,
  636.                                                    value_contents (arg)));
  637.             }
  638.           else if (TYPE_CODE (type) == TYPE_CODE_FLT)
  639.             {
  640.               /* Floating point value store, right aligned.  */
  641.               param_len = align_up (TYPE_LENGTH (type), 4);
  642.               memcpy (param_val, value_contents (arg), param_len);
  643.             }
  644.           else
  645.             {
  646.               param_len = align_up (TYPE_LENGTH (type), 4);

  647.               /* Small struct value are stored right-aligned.  */
  648.               memcpy (param_val + param_len - TYPE_LENGTH (type),
  649.                       value_contents (arg), TYPE_LENGTH (type));

  650.               /* Structures of size 5, 6 and 7 bytes are special in that
  651.                  the higher-ordered word is stored in the lower-ordered
  652.                  argument, and even though it is a 8-byte quantity the
  653.                  registers need not be 8-byte aligned.  */
  654.               if (param_len > 4 && param_len < 8)
  655.                 small_struct = 1;
  656.             }

  657.           param_ptr += param_len;
  658.           if (param_len == 8 && !small_struct)
  659.             param_ptr = align_up (param_ptr, 8);

  660.           /* First 4 non-FP arguments are passed in gr26-gr23.
  661.              First 4 32-bit FP arguments are passed in fr4L-fr7L.
  662.              First 2 64-bit FP arguments are passed in fr5 and fr7.

  663.              The rest go on the stack, starting at sp-36, towards lower
  664.              addresses.  8-byte arguments must be aligned to a 8-byte
  665.              stack boundary.  */
  666.           if (write_pass)
  667.             {
  668.               write_memory (param_end - param_ptr, param_val, param_len);

  669.               /* There are some cases when we don't know the type
  670.                  expected by the callee (e.g. for variadic functions), so
  671.                  pass the parameters in both general and fp regs.  */
  672.               if (param_ptr <= 48)
  673.                 {
  674.                   int grreg = 26 - (param_ptr - 36) / 4;
  675.                   int fpLreg = 72 + (param_ptr - 36) / 4 * 2;
  676.                   int fpreg = 74 + (param_ptr - 32) / 8 * 4;

  677.                   regcache_cooked_write (regcache, grreg, param_val);
  678.                   regcache_cooked_write (regcache, fpLreg, param_val);

  679.                   if (param_len > 4)
  680.                     {
  681.                       regcache_cooked_write (regcache, grreg + 1,
  682.                                              param_val + 4);

  683.                       regcache_cooked_write (regcache, fpreg, param_val);
  684.                       regcache_cooked_write (regcache, fpreg + 1,
  685.                                              param_val + 4);
  686.                     }
  687.                 }
  688.             }
  689.         }

  690.       /* Update the various stack pointers.  */
  691.       if (!write_pass)
  692.         {
  693.           struct_end = sp + align_up (struct_ptr, 64);
  694.           /* PARAM_PTR already accounts for all the arguments passed
  695.              by the user.  However, the ABI mandates minimum stack
  696.              space allocations for outgoing arguments.  The ABI also
  697.              mandates minimum stack alignments which we must
  698.              preserve.  */
  699.           param_end = struct_end + align_up (param_ptr, 64);
  700.         }
  701.     }

  702.   /* If a structure has to be returned, set up register 28 to hold its
  703.      address.  */
  704.   if (struct_return)
  705.     regcache_cooked_write_unsigned (regcache, 28, struct_addr);

  706.   gp = tdep->find_global_pointer (gdbarch, function);

  707.   if (gp != 0)
  708.     regcache_cooked_write_unsigned (regcache, 19, gp);

  709.   /* Set the return address.  */
  710.   if (!gdbarch_push_dummy_code_p (gdbarch))
  711.     regcache_cooked_write_unsigned (regcache, HPPA_RP_REGNUM, bp_addr);

  712.   /* Update the Stack Pointer.  */
  713.   regcache_cooked_write_unsigned (regcache, HPPA_SP_REGNUM, param_end);

  714.   return param_end;
  715. }

  716. /* The 64-bit PA-RISC calling conventions are documented in "64-Bit
  717.    Runtime Architecture for PA-RISC 2.0", which is distributed as part
  718.    as of the HP-UX Software Transition Kit (STK).  This implementation
  719.    is based on version 3.3, dated October 6, 1997.  */

  720. /* Check whether TYPE is an "Integral or Pointer Scalar Type".  */

  721. static int
  722. hppa64_integral_or_pointer_p (const struct type *type)
  723. {
  724.   switch (TYPE_CODE (type))
  725.     {
  726.     case TYPE_CODE_INT:
  727.     case TYPE_CODE_BOOL:
  728.     case TYPE_CODE_CHAR:
  729.     case TYPE_CODE_ENUM:
  730.     case TYPE_CODE_RANGE:
  731.       {
  732.         int len = TYPE_LENGTH (type);
  733.         return (len == 1 || len == 2 || len == 4 || len == 8);
  734.       }
  735.     case TYPE_CODE_PTR:
  736.     case TYPE_CODE_REF:
  737.       return (TYPE_LENGTH (type) == 8);
  738.     default:
  739.       break;
  740.     }

  741.   return 0;
  742. }

  743. /* Check whether TYPE is a "Floating Scalar Type".  */

  744. static int
  745. hppa64_floating_p (const struct type *type)
  746. {
  747.   switch (TYPE_CODE (type))
  748.     {
  749.     case TYPE_CODE_FLT:
  750.       {
  751.         int len = TYPE_LENGTH (type);
  752.         return (len == 4 || len == 8 || len == 16);
  753.       }
  754.     default:
  755.       break;
  756.     }

  757.   return 0;
  758. }

  759. /* If CODE points to a function entry address, try to look up the corresponding
  760.    function descriptor and return its address instead.  If CODE is not a
  761.    function entry address, then just return it unchanged.  */
  762. static CORE_ADDR
  763. hppa64_convert_code_addr_to_fptr (struct gdbarch *gdbarch, CORE_ADDR code)
  764. {
  765.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  766.   struct obj_section *sec, *opd;

  767.   sec = find_pc_section (code);

  768.   if (!sec)
  769.     return code;

  770.   /* If CODE is in a data section, assume it's already a fptr.  */
  771.   if (!(sec->the_bfd_section->flags & SEC_CODE))
  772.     return code;

  773.   ALL_OBJFILE_OSECTIONS (sec->objfile, opd)
  774.     {
  775.       if (strcmp (opd->the_bfd_section->name, ".opd") == 0)
  776.         break;
  777.     }

  778.   if (opd < sec->objfile->sections_end)
  779.     {
  780.       CORE_ADDR addr;

  781.       for (addr = obj_section_addr (opd);
  782.            addr < obj_section_endaddr (opd);
  783.            addr += 2 * 8)
  784.         {
  785.           ULONGEST opdaddr;
  786.           gdb_byte tmp[8];

  787.           if (target_read_memory (addr, tmp, sizeof (tmp)))
  788.               break;
  789.           opdaddr = extract_unsigned_integer (tmp, sizeof (tmp), byte_order);

  790.           if (opdaddr == code)
  791.             return addr - 16;
  792.         }
  793.     }

  794.   return code;
  795. }

  796. static CORE_ADDR
  797. hppa64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  798.                         struct regcache *regcache, CORE_ADDR bp_addr,
  799.                         int nargs, struct value **args, CORE_ADDR sp,
  800.                         int struct_return, CORE_ADDR struct_addr)
  801. {
  802.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  803.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  804.   int i, offset = 0;
  805.   CORE_ADDR gp;

  806.   /* "The outgoing parameter area [...] must be aligned at a 16-byte
  807.      boundary."  */
  808.   sp = align_up (sp, 16);

  809.   for (i = 0; i < nargs; i++)
  810.     {
  811.       struct value *arg = args[i];
  812.       struct type *type = value_type (arg);
  813.       int len = TYPE_LENGTH (type);
  814.       const bfd_byte *valbuf;
  815.       bfd_byte fptrbuf[8];
  816.       int regnum;

  817.       /* "Each parameter begins on a 64-bit (8-byte) boundary."  */
  818.       offset = align_up (offset, 8);

  819.       if (hppa64_integral_or_pointer_p (type))
  820.         {
  821.           /* "Integral scalar parameters smaller than 64 bits are
  822.              padded on the left (i.e., the value is in the
  823.              least-significant bits of the 64-bit storage unit, and
  824.              the high-order bits are undefined)."  Therefore we can
  825.              safely sign-extend them.  */
  826.           if (len < 8)
  827.             {
  828.               arg = value_cast (builtin_type (gdbarch)->builtin_int64, arg);
  829.               len = 8;
  830.             }
  831.         }
  832.       else if (hppa64_floating_p (type))
  833.         {
  834.           if (len > 8)
  835.             {
  836.               /* "Quad-precision (128-bit) floating-point scalar
  837.                  parameters are aligned on a 16-byte boundary."  */
  838.               offset = align_up (offset, 16);

  839.               /* "Double-extended- and quad-precision floating-point
  840.                  parameters within the first 64 bytes of the parameter
  841.                  list are always passed in general registers."  */
  842.             }
  843.           else
  844.             {
  845.               if (len == 4)
  846.                 {
  847.                   /* "Single-precision (32-bit) floating-point scalar
  848.                      parameters are padded on the left with 32 bits of
  849.                      garbage (i.e., the floating-point value is in the
  850.                      least-significant 32 bits of a 64-bit storage
  851.                      unit)."  */
  852.                   offset += 4;
  853.                 }

  854.               /* "Single- and double-precision floating-point
  855.                  parameters in this area are passed according to the
  856.                  available formal parameter information in a function
  857.                  prototype.  [...]  If no prototype is in scope,
  858.                  floating-point parameters must be passed both in the
  859.                  corresponding general registers and in the
  860.                  corresponding floating-point registers."  */
  861.               regnum = HPPA64_FP4_REGNUM + offset / 8;

  862.               if (regnum < HPPA64_FP4_REGNUM + 8)
  863.                 {
  864.                   /* "Single-precision floating-point parameters, when
  865.                      passed in floating-point registers, are passed in
  866.                      the right halves of the floating point registers;
  867.                      the left halves are unused."  */
  868.                   regcache_cooked_write_part (regcache, regnum, offset % 8,
  869.                                               len, value_contents (arg));
  870.                 }
  871.             }
  872.         }
  873.       else
  874.         {
  875.           if (len > 8)
  876.             {
  877.               /* "Aggregates larger than 8 bytes are aligned on a
  878.                  16-byte boundary, possibly leaving an unused argument
  879.                  slot, which is filled with garbage.  If necessary,
  880.                  they are padded on the right (with garbage), to a
  881.                  multiple of 8 bytes."  */
  882.               offset = align_up (offset, 16);
  883.             }
  884.         }

  885.       /* If we are passing a function pointer, make sure we pass a function
  886.          descriptor instead of the function entry address.  */
  887.       if (TYPE_CODE (type) == TYPE_CODE_PTR
  888.           && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC)
  889.         {
  890.           ULONGEST codeptr, fptr;

  891.           codeptr = unpack_long (type, value_contents (arg));
  892.           fptr = hppa64_convert_code_addr_to_fptr (gdbarch, codeptr);
  893.           store_unsigned_integer (fptrbuf, TYPE_LENGTH (type), byte_order,
  894.                                   fptr);
  895.           valbuf = fptrbuf;
  896.         }
  897.       else
  898.         {
  899.           valbuf = value_contents (arg);
  900.         }

  901.       /* Always store the argument in memory.  */
  902.       write_memory (sp + offset, valbuf, len);

  903.       regnum = HPPA_ARG0_REGNUM - offset / 8;
  904.       while (regnum > HPPA_ARG0_REGNUM - 8 && len > 0)
  905.         {
  906.           regcache_cooked_write_part (regcache, regnum,
  907.                                       offset % 8, min (len, 8), valbuf);
  908.           offset += min (len, 8);
  909.           valbuf += min (len, 8);
  910.           len -= min (len, 8);
  911.           regnum--;
  912.         }

  913.       offset += len;
  914.     }

  915.   /* Set up GR29 (%ret1) to hold the argument pointer (ap).  */
  916.   regcache_cooked_write_unsigned (regcache, HPPA_RET1_REGNUM, sp + 64);

  917.   /* Allocate the outgoing parameter area.  Make sure the outgoing
  918.      parameter area is multiple of 16 bytes in length.  */
  919.   sp += max (align_up (offset, 16), 64);

  920.   /* Allocate 32-bytes of scratch space.  The documentation doesn't
  921.      mention this, but it seems to be needed.  */
  922.   sp += 32;

  923.   /* Allocate the frame marker area.  */
  924.   sp += 16;

  925.   /* If a structure has to be returned, set up GR 28 (%ret0) to hold
  926.      its address.  */
  927.   if (struct_return)
  928.     regcache_cooked_write_unsigned (regcache, HPPA_RET0_REGNUM, struct_addr);

  929.   /* Set up GR27 (%dp) to hold the global pointer (gp).  */
  930.   gp = tdep->find_global_pointer (gdbarch, function);
  931.   if (gp != 0)
  932.     regcache_cooked_write_unsigned (regcache, HPPA_DP_REGNUM, gp);

  933.   /* Set up GR2 (%rp) to hold the return pointer (rp).  */
  934.   if (!gdbarch_push_dummy_code_p (gdbarch))
  935.     regcache_cooked_write_unsigned (regcache, HPPA_RP_REGNUM, bp_addr);

  936.   /* Set up GR30 to hold the stack pointer (sp).  */
  937.   regcache_cooked_write_unsigned (regcache, HPPA_SP_REGNUM, sp);

  938.   return sp;
  939. }


  940. /* Handle 32/64-bit struct return conventions.  */

  941. static enum return_value_convention
  942. hppa32_return_value (struct gdbarch *gdbarch, struct value *function,
  943.                      struct type *type, struct regcache *regcache,
  944.                      gdb_byte *readbuf, const gdb_byte *writebuf)
  945. {
  946.   if (TYPE_LENGTH (type) <= 2 * 4)
  947.     {
  948.       /* The value always lives in the right hand end of the register
  949.          (or register pair)?  */
  950.       int b;
  951.       int reg = TYPE_CODE (type) == TYPE_CODE_FLT ? HPPA_FP4_REGNUM : 28;
  952.       int part = TYPE_LENGTH (type) % 4;
  953.       /* The left hand register contains only part of the value,
  954.          transfer that first so that the rest can be xfered as entire
  955.          4-byte registers.  */
  956.       if (part > 0)
  957.         {
  958.           if (readbuf != NULL)
  959.             regcache_cooked_read_part (regcache, reg, 4 - part,
  960.                                        part, readbuf);
  961.           if (writebuf != NULL)
  962.             regcache_cooked_write_part (regcache, reg, 4 - part,
  963.                                         part, writebuf);
  964.           reg++;
  965.         }
  966.       /* Now transfer the remaining register values.  */
  967.       for (b = part; b < TYPE_LENGTH (type); b += 4)
  968.         {
  969.           if (readbuf != NULL)
  970.             regcache_cooked_read (regcache, reg, readbuf + b);
  971.           if (writebuf != NULL)
  972.             regcache_cooked_write (regcache, reg, writebuf + b);
  973.           reg++;
  974.         }
  975.       return RETURN_VALUE_REGISTER_CONVENTION;
  976.     }
  977.   else
  978.     return RETURN_VALUE_STRUCT_CONVENTION;
  979. }

  980. static enum return_value_convention
  981. hppa64_return_value (struct gdbarch *gdbarch, struct value *function,
  982.                      struct type *type, struct regcache *regcache,
  983.                      gdb_byte *readbuf, const gdb_byte *writebuf)
  984. {
  985.   int len = TYPE_LENGTH (type);
  986.   int regnum, offset;

  987.   if (len > 16)
  988.     {
  989.       /* All return values larget than 128 bits must be aggregate
  990.          return values.  */
  991.       gdb_assert (!hppa64_integral_or_pointer_p (type));
  992.       gdb_assert (!hppa64_floating_p (type));

  993.       /* "Aggregate return values larger than 128 bits are returned in
  994.          a buffer allocated by the caller.  The address of the buffer
  995.          must be passed in GR 28."  */
  996.       return RETURN_VALUE_STRUCT_CONVENTION;
  997.     }

  998.   if (hppa64_integral_or_pointer_p (type))
  999.     {
  1000.       /* "Integral return values are returned in GR 28.  Values
  1001.          smaller than 64 bits are padded on the left (with garbage)."  */
  1002.       regnum = HPPA_RET0_REGNUM;
  1003.       offset = 8 - len;
  1004.     }
  1005.   else if (hppa64_floating_p (type))
  1006.     {
  1007.       if (len > 8)
  1008.         {
  1009.           /* "Double-extended- and quad-precision floating-point
  1010.              values are returned in GRs 28 and 29.  The sign,
  1011.              exponent, and most-significant bits of the mantissa are
  1012.              returned in GR 28; the least-significant bits of the
  1013.              mantissa are passed in GR 29.  For double-extended
  1014.              precision values, GR 29 is padded on the right with 48
  1015.              bits of garbage."  */
  1016.           regnum = HPPA_RET0_REGNUM;
  1017.           offset = 0;
  1018.         }
  1019.       else
  1020.         {
  1021.           /* "Single-precision and double-precision floating-point
  1022.              return values are returned in FR 4R (single precision) or
  1023.              FR 4 (double-precision)."  */
  1024.           regnum = HPPA64_FP4_REGNUM;
  1025.           offset = 8 - len;
  1026.         }
  1027.     }
  1028.   else
  1029.     {
  1030.       /* "Aggregate return values up to 64 bits in size are returned
  1031.          in GR 28.  Aggregates smaller than 64 bits are left aligned
  1032.          in the register; the pad bits on the right are undefined."

  1033.          "Aggregate return values between 65 and 128 bits are returned
  1034.          in GRs 28 and 29.  The first 64 bits are placed in GR 28, and
  1035.          the remaining bits are placed, left aligned, in GR 29.  The
  1036.          pad bits on the right of GR 29 (if any) are undefined."  */
  1037.       regnum = HPPA_RET0_REGNUM;
  1038.       offset = 0;
  1039.     }

  1040.   if (readbuf)
  1041.     {
  1042.       while (len > 0)
  1043.         {
  1044.           regcache_cooked_read_part (regcache, regnum, offset,
  1045.                                      min (len, 8), readbuf);
  1046.           readbuf += min (len, 8);
  1047.           len -= min (len, 8);
  1048.           regnum++;
  1049.         }
  1050.     }

  1051.   if (writebuf)
  1052.     {
  1053.       while (len > 0)
  1054.         {
  1055.           regcache_cooked_write_part (regcache, regnum, offset,
  1056.                                       min (len, 8), writebuf);
  1057.           writebuf += min (len, 8);
  1058.           len -= min (len, 8);
  1059.           regnum++;
  1060.         }
  1061.     }

  1062.   return RETURN_VALUE_REGISTER_CONVENTION;
  1063. }


  1064. static CORE_ADDR
  1065. hppa32_convert_from_func_ptr_addr (struct gdbarch *gdbarch, CORE_ADDR addr,
  1066.                                    struct target_ops *targ)
  1067. {
  1068.   if (addr & 2)
  1069.     {
  1070.       struct type *func_ptr_type = builtin_type (gdbarch)->builtin_func_ptr;
  1071.       CORE_ADDR plabel = addr & ~3;
  1072.       return read_memory_typed_address (plabel, func_ptr_type);
  1073.     }

  1074.   return addr;
  1075. }

  1076. static CORE_ADDR
  1077. hppa32_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
  1078. {
  1079.   /* HP frames are 64-byte (or cache line) aligned (yes that's _byte_
  1080.      and not _bit_)!  */
  1081.   return align_up (addr, 64);
  1082. }

  1083. /* Force all frames to 16-byte alignment.  Better safe than sorry.  */

  1084. static CORE_ADDR
  1085. hppa64_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
  1086. {
  1087.   /* Just always 16-byte align.  */
  1088.   return align_up (addr, 16);
  1089. }

  1090. CORE_ADDR
  1091. hppa_read_pc (struct regcache *regcache)
  1092. {
  1093.   ULONGEST ipsw;
  1094.   ULONGEST pc;

  1095.   regcache_cooked_read_unsigned (regcache, HPPA_IPSW_REGNUM, &ipsw);
  1096.   regcache_cooked_read_unsigned (regcache, HPPA_PCOQ_HEAD_REGNUM, &pc);

  1097.   /* If the current instruction is nullified, then we are effectively
  1098.      still executing the previous instruction.  Pretend we are still
  1099.      there.  This is needed when single stepping; if the nullified
  1100.      instruction is on a different line, we don't want GDB to think
  1101.      we've stepped onto that line.  */
  1102.   if (ipsw & 0x00200000)
  1103.     pc -= 4;

  1104.   return pc & ~0x3;
  1105. }

  1106. void
  1107. hppa_write_pc (struct regcache *regcache, CORE_ADDR pc)
  1108. {
  1109.   regcache_cooked_write_unsigned (regcache, HPPA_PCOQ_HEAD_REGNUM, pc);
  1110.   regcache_cooked_write_unsigned (regcache, HPPA_PCOQ_TAIL_REGNUM, pc + 4);
  1111. }

  1112. /* For the given instruction (INST), return any adjustment it makes
  1113.    to the stack pointer or zero for no adjustment.

  1114.    This only handles instructions commonly found in prologues.  */

  1115. static int
  1116. prologue_inst_adjust_sp (unsigned long inst)
  1117. {
  1118.   /* This must persist across calls.  */
  1119.   static int save_high21;

  1120.   /* The most common way to perform a stack adjustment ldo X(sp),sp */
  1121.   if ((inst & 0xffffc000) == 0x37de0000)
  1122.     return hppa_extract_14 (inst);

  1123.   /* stwm X,D(sp) */
  1124.   if ((inst & 0xffe00000) == 0x6fc00000)
  1125.     return hppa_extract_14 (inst);

  1126.   /* std,ma X,D(sp) */
  1127.   if ((inst & 0xffe00008) == 0x73c00008)
  1128.     return (inst & 0x1 ? -1 << 13 : 0) | (((inst >> 4) & 0x3ff) << 3);

  1129.   /* addil high21,%r30; ldo low11,(%r1),%r30)
  1130.      save high bits in save_high21 for later use.  */
  1131.   if ((inst & 0xffe00000) == 0x2bc00000)
  1132.     {
  1133.       save_high21 = hppa_extract_21 (inst);
  1134.       return 0;
  1135.     }

  1136.   if ((inst & 0xffff0000) == 0x343e0000)
  1137.     return save_high21 + hppa_extract_14 (inst);

  1138.   /* fstws as used by the HP compilers.  */
  1139.   if ((inst & 0xffffffe0) == 0x2fd01220)
  1140.     return hppa_extract_5_load (inst);

  1141.   /* No adjustment.  */
  1142.   return 0;
  1143. }

  1144. /* Return nonzero if INST is a branch of some kind, else return zero.  */

  1145. static int
  1146. is_branch (unsigned long inst)
  1147. {
  1148.   switch (inst >> 26)
  1149.     {
  1150.     case 0x20:
  1151.     case 0x21:
  1152.     case 0x22:
  1153.     case 0x23:
  1154.     case 0x27:
  1155.     case 0x28:
  1156.     case 0x29:
  1157.     case 0x2a:
  1158.     case 0x2b:
  1159.     case 0x2f:
  1160.     case 0x30:
  1161.     case 0x31:
  1162.     case 0x32:
  1163.     case 0x33:
  1164.     case 0x38:
  1165.     case 0x39:
  1166.     case 0x3a:
  1167.     case 0x3b:
  1168.       return 1;

  1169.     default:
  1170.       return 0;
  1171.     }
  1172. }

  1173. /* Return the register number for a GR which is saved by INST or
  1174.    zero it INST does not save a GR.  */

  1175. static int
  1176. inst_saves_gr (unsigned long inst)
  1177. {
  1178.   /* Does it look like a stw?  */
  1179.   if ((inst >> 26) == 0x1a || (inst >> 26) == 0x1b
  1180.       || (inst >> 26) == 0x1f
  1181.       || ((inst >> 26) == 0x1f
  1182.           && ((inst >> 6) == 0xa)))
  1183.     return hppa_extract_5R_store (inst);

  1184.   /* Does it look like a std?  */
  1185.   if ((inst >> 26) == 0x1c
  1186.       || ((inst >> 26) == 0x03
  1187.           && ((inst >> 6) & 0xf) == 0xb))
  1188.     return hppa_extract_5R_store (inst);

  1189.   /* Does it look like a stwm?  GCC & HPC may use this in prologues.  */
  1190.   if ((inst >> 26) == 0x1b)
  1191.     return hppa_extract_5R_store (inst);

  1192.   /* Does it look like sth or stb?  HPC versions 9.0 and later use these
  1193.      too.  */
  1194.   if ((inst >> 26) == 0x19 || (inst >> 26) == 0x18
  1195.       || ((inst >> 26) == 0x3
  1196.           && (((inst >> 6) & 0xf) == 0x8
  1197.               || (inst >> 6) & 0xf) == 0x9))
  1198.     return hppa_extract_5R_store (inst);

  1199.   return 0;
  1200. }

  1201. /* Return the register number for a FR which is saved by INST or
  1202.    zero it INST does not save a FR.

  1203.    Note we only care about full 64bit register stores (that's the only
  1204.    kind of stores the prologue will use).

  1205.    FIXME: What about argument stores with the HP compiler in ANSI mode? */

  1206. static int
  1207. inst_saves_fr (unsigned long inst)
  1208. {
  1209.   /* Is this an FSTD?  */
  1210.   if ((inst & 0xfc00dfc0) == 0x2c001200)
  1211.     return hppa_extract_5r_store (inst);
  1212.   if ((inst & 0xfc000002) == 0x70000002)
  1213.     return hppa_extract_5R_store (inst);
  1214.   /* Is this an FSTW?  */
  1215.   if ((inst & 0xfc00df80) == 0x24001200)
  1216.     return hppa_extract_5r_store (inst);
  1217.   if ((inst & 0xfc000002) == 0x7c000000)
  1218.     return hppa_extract_5R_store (inst);
  1219.   return 0;
  1220. }

  1221. /* Advance PC across any function entry prologue instructions
  1222.    to reach some "real" code.

  1223.    Use information in the unwind table to determine what exactly should
  1224.    be in the prologue.  */


  1225. static CORE_ADDR
  1226. skip_prologue_hard_way (struct gdbarch *gdbarch, CORE_ADDR pc,
  1227.                         int stop_before_branch)
  1228. {
  1229.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1230.   gdb_byte buf[4];
  1231.   CORE_ADDR orig_pc = pc;
  1232.   unsigned long inst, stack_remaining, save_gr, save_fr, save_rp, save_sp;
  1233.   unsigned long args_stored, status, i, restart_gr, restart_fr;
  1234.   struct unwind_table_entry *u;
  1235.   int final_iteration;

  1236.   restart_gr = 0;
  1237.   restart_fr = 0;

  1238. restart:
  1239.   u = find_unwind_entry (pc);
  1240.   if (!u)
  1241.     return pc;

  1242.   /* If we are not at the beginning of a function, then return now.  */
  1243.   if ((pc & ~0x3) != u->region_start)
  1244.     return pc;

  1245.   /* This is how much of a frame adjustment we need to account for.  */
  1246.   stack_remaining = u->Total_frame_size << 3;

  1247.   /* Magic register saves we want to know about.  */
  1248.   save_rp = u->Save_RP;
  1249.   save_sp = u->Save_SP;

  1250.   /* An indication that args may be stored into the stack.  Unfortunately
  1251.      the HPUX compilers tend to set this in cases where no args were
  1252.      stored too!.  */
  1253.   args_stored = 1;

  1254.   /* Turn the Entry_GR field into a bitmask.  */
  1255.   save_gr = 0;
  1256.   for (i = 3; i < u->Entry_GR + 3; i++)
  1257.     {
  1258.       /* Frame pointer gets saved into a special location.  */
  1259.       if (u->Save_SP && i == HPPA_FP_REGNUM)
  1260.         continue;

  1261.       save_gr |= (1 << i);
  1262.     }
  1263.   save_gr &= ~restart_gr;

  1264.   /* Turn the Entry_FR field into a bitmask too.  */
  1265.   save_fr = 0;
  1266.   for (i = 12; i < u->Entry_FR + 12; i++)
  1267.     save_fr |= (1 << i);
  1268.   save_fr &= ~restart_fr;

  1269.   final_iteration = 0;

  1270.   /* Loop until we find everything of interest or hit a branch.

  1271.      For unoptimized GCC code and for any HP CC code this will never ever
  1272.      examine any user instructions.

  1273.      For optimzied GCC code we're faced with problems.  GCC will schedule
  1274.      its prologue and make prologue instructions available for delay slot
  1275.      filling.  The end result is user code gets mixed in with the prologue
  1276.      and a prologue instruction may be in the delay slot of the first branch
  1277.      or call.

  1278.      Some unexpected things are expected with debugging optimized code, so
  1279.      we allow this routine to walk past user instructions in optimized
  1280.      GCC code.  */
  1281.   while (save_gr || save_fr || save_rp || save_sp || stack_remaining > 0
  1282.          || args_stored)
  1283.     {
  1284.       unsigned int reg_num;
  1285.       unsigned long old_stack_remaining, old_save_gr, old_save_fr;
  1286.       unsigned long old_save_rp, old_save_sp, next_inst;

  1287.       /* Save copies of all the triggers so we can compare them later
  1288.          (only for HPC).  */
  1289.       old_save_gr = save_gr;
  1290.       old_save_fr = save_fr;
  1291.       old_save_rp = save_rp;
  1292.       old_save_sp = save_sp;
  1293.       old_stack_remaining = stack_remaining;

  1294.       status = target_read_memory (pc, buf, 4);
  1295.       inst = extract_unsigned_integer (buf, 4, byte_order);

  1296.       /* Yow! */
  1297.       if (status != 0)
  1298.         return pc;

  1299.       /* Note the interesting effects of this instruction.  */
  1300.       stack_remaining -= prologue_inst_adjust_sp (inst);

  1301.       /* There are limited ways to store the return pointer into the
  1302.          stack.  */
  1303.       if (inst == 0x6bc23fd9 || inst == 0x0fc212c1 || inst == 0x73c23fe1)
  1304.         save_rp = 0;

  1305.       /* These are the only ways we save SP into the stack.  At this time
  1306.          the HP compilers never bother to save SP into the stack.  */
  1307.       if ((inst & 0xffffc000) == 0x6fc10000
  1308.           || (inst & 0xffffc00c) == 0x73c10008)
  1309.         save_sp = 0;

  1310.       /* Are we loading some register with an offset from the argument
  1311.          pointer?  */
  1312.       if ((inst & 0xffe00000) == 0x37a00000
  1313.           || (inst & 0xffffffe0) == 0x081d0240)
  1314.         {
  1315.           pc += 4;
  1316.           continue;
  1317.         }

  1318.       /* Account for general and floating-point register saves.  */
  1319.       reg_num = inst_saves_gr (inst);
  1320.       save_gr &= ~(1 << reg_num);

  1321.       /* Ugh.  Also account for argument stores into the stack.
  1322.          Unfortunately args_stored only tells us that some arguments
  1323.          where stored into the stack.  Not how many or what kind!

  1324.          This is a kludge as on the HP compiler sets this bit and it
  1325.          never does prologue scheduling.  So once we see one, skip past
  1326.          all of them.   We have similar code for the fp arg stores below.

  1327.          FIXME.  Can still die if we have a mix of GR and FR argument
  1328.          stores!  */
  1329.       if (reg_num >= (gdbarch_ptr_bit (gdbarch) == 64 ? 19 : 23)
  1330.           && reg_num <= 26)
  1331.         {
  1332.           while (reg_num >= (gdbarch_ptr_bit (gdbarch) == 64 ? 19 : 23)
  1333.                  && reg_num <= 26)
  1334.             {
  1335.               pc += 4;
  1336.               status = target_read_memory (pc, buf, 4);
  1337.               inst = extract_unsigned_integer (buf, 4, byte_order);
  1338.               if (status != 0)
  1339.                 return pc;
  1340.               reg_num = inst_saves_gr (inst);
  1341.             }
  1342.           args_stored = 0;
  1343.           continue;
  1344.         }

  1345.       reg_num = inst_saves_fr (inst);
  1346.       save_fr &= ~(1 << reg_num);

  1347.       status = target_read_memory (pc + 4, buf, 4);
  1348.       next_inst = extract_unsigned_integer (buf, 4, byte_order);

  1349.       /* Yow! */
  1350.       if (status != 0)
  1351.         return pc;

  1352.       /* We've got to be read to handle the ldo before the fp register
  1353.          save.  */
  1354.       if ((inst & 0xfc000000) == 0x34000000
  1355.           && inst_saves_fr (next_inst) >= 4
  1356.           && inst_saves_fr (next_inst)
  1357.                <= (gdbarch_ptr_bit (gdbarch) == 64 ? 11 : 7))
  1358.         {
  1359.           /* So we drop into the code below in a reasonable state.  */
  1360.           reg_num = inst_saves_fr (next_inst);
  1361.           pc -= 4;
  1362.         }

  1363.       /* Ugh.  Also account for argument stores into the stack.
  1364.          This is a kludge as on the HP compiler sets this bit and it
  1365.          never does prologue scheduling.  So once we see one, skip past
  1366.          all of them.  */
  1367.       if (reg_num >= 4
  1368.           && reg_num <= (gdbarch_ptr_bit (gdbarch) == 64 ? 11 : 7))
  1369.         {
  1370.           while (reg_num >= 4
  1371.                  && reg_num
  1372.                       <= (gdbarch_ptr_bit (gdbarch) == 64 ? 11 : 7))
  1373.             {
  1374.               pc += 8;
  1375.               status = target_read_memory (pc, buf, 4);
  1376.               inst = extract_unsigned_integer (buf, 4, byte_order);
  1377.               if (status != 0)
  1378.                 return pc;
  1379.               if ((inst & 0xfc000000) != 0x34000000)
  1380.                 break;
  1381.               status = target_read_memory (pc + 4, buf, 4);
  1382.               next_inst = extract_unsigned_integer (buf, 4, byte_order);
  1383.               if (status != 0)
  1384.                 return pc;
  1385.               reg_num = inst_saves_fr (next_inst);
  1386.             }
  1387.           args_stored = 0;
  1388.           continue;
  1389.         }

  1390.       /* Quit if we hit any kind of branch.  This can happen if a prologue
  1391.          instruction is in the delay slot of the first call/branch.  */
  1392.       if (is_branch (inst) && stop_before_branch)
  1393.         break;

  1394.       /* What a crock.  The HP compilers set args_stored even if no
  1395.          arguments were stored into the stack (boo hiss).  This could
  1396.          cause this code to then skip a bunch of user insns (up to the
  1397.          first branch).

  1398.          To combat this we try to identify when args_stored was bogusly
  1399.          set and clear it.   We only do this when args_stored is nonzero,
  1400.          all other resources are accounted for, and nothing changed on
  1401.          this pass.  */
  1402.       if (args_stored
  1403.        && !(save_gr || save_fr || save_rp || save_sp || stack_remaining > 0)
  1404.           && old_save_gr == save_gr && old_save_fr == save_fr
  1405.           && old_save_rp == save_rp && old_save_sp == save_sp
  1406.           && old_stack_remaining == stack_remaining)
  1407.         break;

  1408.       /* Bump the PC.  */
  1409.       pc += 4;

  1410.       /* !stop_before_branch, so also look at the insn in the delay slot
  1411.          of the branch.  */
  1412.       if (final_iteration)
  1413.         break;
  1414.       if (is_branch (inst))
  1415.         final_iteration = 1;
  1416.     }

  1417.   /* We've got a tenative location for the end of the prologue.  However
  1418.      because of limitations in the unwind descriptor mechanism we may
  1419.      have went too far into user code looking for the save of a register
  1420.      that does not exist.  So, if there registers we expected to be saved
  1421.      but never were, mask them out and restart.

  1422.      This should only happen in optimized code, and should be very rare.  */
  1423.   if (save_gr || (save_fr && !(restart_fr || restart_gr)))
  1424.     {
  1425.       pc = orig_pc;
  1426.       restart_gr = save_gr;
  1427.       restart_fr = save_fr;
  1428.       goto restart;
  1429.     }

  1430.   return pc;
  1431. }


  1432. /* Return the address of the PC after the last prologue instruction if
  1433.    we can determine it from the debug symbols.  Else return zero.  */

  1434. static CORE_ADDR
  1435. after_prologue (CORE_ADDR pc)
  1436. {
  1437.   struct symtab_and_line sal;
  1438.   CORE_ADDR func_addr, func_end;

  1439.   /* If we can not find the symbol in the partial symbol table, then
  1440.      there is no hope we can determine the function's start address
  1441.      with this code.  */
  1442.   if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  1443.     return 0;

  1444.   /* Get the line associated with FUNC_ADDR.  */
  1445.   sal = find_pc_line (func_addr, 0);

  1446.   /* There are only two cases to consider.  First, the end of the source line
  1447.      is within the function bounds.  In that case we return the end of the
  1448.      source line.  Second is the end of the source line extends beyond the
  1449.      bounds of the current function.  We need to use the slow code to
  1450.      examine instructions in that case.

  1451.      Anything else is simply a bug elsewhere.  Fixing it here is absolutely
  1452.      the wrong thing to do.  In fact, it should be entirely possible for this
  1453.      function to always return zero since the slow instruction scanning code
  1454.      is supposed to *always* work.  If it does not, then it is a bug.  */
  1455.   if (sal.end < func_end)
  1456.     return sal.end;
  1457.   else
  1458.     return 0;
  1459. }

  1460. /* To skip prologues, I use this predicate.  Returns either PC itself
  1461.    if the code at PC does not look like a function prologue; otherwise
  1462.    returns an address that (if we're lucky) follows the prologue.

  1463.    hppa_skip_prologue is called by gdb to place a breakpoint in a function.
  1464.    It doesn't necessarily skips all the insns in the prologue.  In fact
  1465.    we might not want to skip all the insns because a prologue insn may
  1466.    appear in the delay slot of the first branch, and we don't want to
  1467.    skip over the branch in that case.  */

  1468. static CORE_ADDR
  1469. hppa_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  1470. {
  1471.   CORE_ADDR post_prologue_pc;

  1472.   /* See if we can determine the end of the prologue via the symbol table.
  1473.      If so, then return either PC, or the PC after the prologue, whichever
  1474.      is greater.  */

  1475.   post_prologue_pc = after_prologue (pc);

  1476.   /* If after_prologue returned a useful address, then use it.  Else
  1477.      fall back on the instruction skipping code.

  1478.      Some folks have claimed this causes problems because the breakpoint
  1479.      may be the first instruction of the prologue.  If that happens, then
  1480.      the instruction skipping code has a bug that needs to be fixed.  */
  1481.   if (post_prologue_pc != 0)
  1482.     return max (pc, post_prologue_pc);
  1483.   else
  1484.     return (skip_prologue_hard_way (gdbarch, pc, 1));
  1485. }

  1486. /* Return an unwind entry that falls within the frame's code block.  */

  1487. static struct unwind_table_entry *
  1488. hppa_find_unwind_entry_in_block (struct frame_info *this_frame)
  1489. {
  1490.   CORE_ADDR pc = get_frame_address_in_block (this_frame);

  1491.   /* FIXME drow/20070101: Calling gdbarch_addr_bits_remove on the
  1492.      result of get_frame_address_in_block implies a problem.
  1493.      The bits should have been removed earlier, before the return
  1494.      value of gdbarch_unwind_pc.  That might be happening already;
  1495.      if it isn't, it should be fixed.  Then this call can be
  1496.      removed.  */
  1497.   pc = gdbarch_addr_bits_remove (get_frame_arch (this_frame), pc);
  1498.   return find_unwind_entry (pc);
  1499. }

  1500. struct hppa_frame_cache
  1501. {
  1502.   CORE_ADDR base;
  1503.   struct trad_frame_saved_reg *saved_regs;
  1504. };

  1505. static struct hppa_frame_cache *
  1506. hppa_frame_cache (struct frame_info *this_frame, void **this_cache)
  1507. {
  1508.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1509.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1510.   int word_size = gdbarch_ptr_bit (gdbarch) / 8;
  1511.   struct hppa_frame_cache *cache;
  1512.   long saved_gr_mask;
  1513.   long saved_fr_mask;
  1514.   long frame_size;
  1515.   struct unwind_table_entry *u;
  1516.   CORE_ADDR prologue_end;
  1517.   int fp_in_r1 = 0;
  1518.   int i;

  1519.   if (hppa_debug)
  1520.     fprintf_unfiltered (gdb_stdlog, "{ hppa_frame_cache (frame=%d) -> ",
  1521.       frame_relative_level(this_frame));

  1522.   if ((*this_cache) != NULL)
  1523.     {
  1524.       if (hppa_debug)
  1525.         fprintf_unfiltered (gdb_stdlog, "base=%s (cached) }",
  1526.           paddress (gdbarch, ((struct hppa_frame_cache *)*this_cache)->base));
  1527.       return (*this_cache);
  1528.     }
  1529.   cache = FRAME_OBSTACK_ZALLOC (struct hppa_frame_cache);
  1530.   (*this_cache) = cache;
  1531.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  1532.   /* Yow! */
  1533.   u = hppa_find_unwind_entry_in_block (this_frame);
  1534.   if (!u)
  1535.     {
  1536.       if (hppa_debug)
  1537.         fprintf_unfiltered (gdb_stdlog, "base=NULL (no unwind entry) }");
  1538.       return (*this_cache);
  1539.     }

  1540.   /* Turn the Entry_GR field into a bitmask.  */
  1541.   saved_gr_mask = 0;
  1542.   for (i = 3; i < u->Entry_GR + 3; i++)
  1543.     {
  1544.       /* Frame pointer gets saved into a special location.  */
  1545.       if (u->Save_SP && i == HPPA_FP_REGNUM)
  1546.         continue;

  1547.       saved_gr_mask |= (1 << i);
  1548.     }

  1549.   /* Turn the Entry_FR field into a bitmask too.  */
  1550.   saved_fr_mask = 0;
  1551.   for (i = 12; i < u->Entry_FR + 12; i++)
  1552.     saved_fr_mask |= (1 << i);

  1553.   /* Loop until we find everything of interest or hit a branch.

  1554.      For unoptimized GCC code and for any HP CC code this will never ever
  1555.      examine any user instructions.

  1556.      For optimized GCC code we're faced with problems.  GCC will schedule
  1557.      its prologue and make prologue instructions available for delay slot
  1558.      filling.  The end result is user code gets mixed in with the prologue
  1559.      and a prologue instruction may be in the delay slot of the first branch
  1560.      or call.

  1561.      Some unexpected things are expected with debugging optimized code, so
  1562.      we allow this routine to walk past user instructions in optimized
  1563.      GCC code.  */
  1564.   {
  1565.     int final_iteration = 0;
  1566.     CORE_ADDR pc, start_pc, end_pc;
  1567.     int looking_for_sp = u->Save_SP;
  1568.     int looking_for_rp = u->Save_RP;
  1569.     int fp_loc = -1;

  1570.     /* We have to use skip_prologue_hard_way instead of just
  1571.        skip_prologue_using_sal, in case we stepped into a function without
  1572.        symbol information.  hppa_skip_prologue also bounds the returned
  1573.        pc by the passed in pc, so it will not return a pc in the next
  1574.        function.

  1575.        We used to call hppa_skip_prologue to find the end of the prologue,
  1576.        but if some non-prologue instructions get scheduled into the prologue,
  1577.        and the program is compiled with debug information, the "easy" way
  1578.        in hppa_skip_prologue will return a prologue end that is too early
  1579.        for us to notice any potential frame adjustments.  */

  1580.     /* We used to use get_frame_func to locate the beginning of the
  1581.        function to pass to skip_prologue.  However, when objects are
  1582.        compiled without debug symbols, get_frame_func can return the wrong
  1583.        function (or 0).  We can do better than that by using unwind records.
  1584.        This only works if the Region_description of the unwind record
  1585.        indicates that it includes the entry point of the function.
  1586.        HP compilers sometimes generate unwind records for regions that
  1587.        do not include the entry or exit point of a function.  GNU tools
  1588.        do not do this.  */

  1589.     if ((u->Region_description & 0x2) == 0)
  1590.       start_pc = u->region_start;
  1591.     else
  1592.       start_pc = get_frame_func (this_frame);

  1593.     prologue_end = skip_prologue_hard_way (gdbarch, start_pc, 0);
  1594.     end_pc = get_frame_pc (this_frame);

  1595.     if (prologue_end != 0 && end_pc > prologue_end)
  1596.       end_pc = prologue_end;

  1597.     frame_size = 0;

  1598.     for (pc = start_pc;
  1599.          ((saved_gr_mask || saved_fr_mask
  1600.            || looking_for_sp || looking_for_rp
  1601.            || frame_size < (u->Total_frame_size << 3))
  1602.           && pc < end_pc);
  1603.          pc += 4)
  1604.       {
  1605.         int reg;
  1606.         gdb_byte buf4[4];
  1607.         long inst;

  1608.         if (!safe_frame_unwind_memory (this_frame, pc, buf4, sizeof buf4))
  1609.           {
  1610.             error (_("Cannot read instruction at %s."),
  1611.                    paddress (gdbarch, pc));
  1612.             return (*this_cache);
  1613.           }

  1614.         inst = extract_unsigned_integer (buf4, sizeof buf4, byte_order);

  1615.         /* Note the interesting effects of this instruction.  */
  1616.         frame_size += prologue_inst_adjust_sp (inst);

  1617.         /* There are limited ways to store the return pointer into the
  1618.            stack.  */
  1619.         if (inst == 0x6bc23fd9) /* stw rp,-0x14(sr0,sp) */
  1620.           {
  1621.             looking_for_rp = 0;
  1622.             cache->saved_regs[HPPA_RP_REGNUM].addr = -20;
  1623.           }
  1624.         else if (inst == 0x6bc23fd1) /* stw rp,-0x18(sr0,sp) */
  1625.           {
  1626.             looking_for_rp = 0;
  1627.             cache->saved_regs[HPPA_RP_REGNUM].addr = -24;
  1628.           }
  1629.         else if (inst == 0x0fc212c1
  1630.                  || inst == 0x73c23fe1) /* std rp,-0x10(sr0,sp) */
  1631.           {
  1632.             looking_for_rp = 0;
  1633.             cache->saved_regs[HPPA_RP_REGNUM].addr = -16;
  1634.           }

  1635.         /* Check to see if we saved SP into the stack.  This also
  1636.            happens to indicate the location of the saved frame
  1637.            pointer.  */
  1638.         if ((inst & 0xffffc000) == 0x6fc10000  /* stw,ma r1,N(sr0,sp) */
  1639.             || (inst & 0xffffc00c) == 0x73c10008) /* std,ma r1,N(sr0,sp) */
  1640.           {
  1641.             looking_for_sp = 0;
  1642.             cache->saved_regs[HPPA_FP_REGNUM].addr = 0;
  1643.           }
  1644.         else if (inst == 0x08030241) /* copy %r3, %r1 */
  1645.           {
  1646.             fp_in_r1 = 1;
  1647.           }

  1648.         /* Account for general and floating-point register saves.  */
  1649.         reg = inst_saves_gr (inst);
  1650.         if (reg >= 3 && reg <= 18
  1651.             && (!u->Save_SP || reg != HPPA_FP_REGNUM))
  1652.           {
  1653.             saved_gr_mask &= ~(1 << reg);
  1654.             if ((inst >> 26) == 0x1b && hppa_extract_14 (inst) >= 0)
  1655.               /* stwm with a positive displacement is a _post_
  1656.                  _modify_.  */
  1657.               cache->saved_regs[reg].addr = 0;
  1658.             else if ((inst & 0xfc00000c) == 0x70000008)
  1659.               /* A std has explicit post_modify forms.  */
  1660.               cache->saved_regs[reg].addr = 0;
  1661.             else
  1662.               {
  1663.                 CORE_ADDR offset;

  1664.                 if ((inst >> 26) == 0x1c)
  1665.                   offset = (inst & 0x1 ? -1 << 13 : 0)
  1666.                     | (((inst >> 4) & 0x3ff) << 3);
  1667.                 else if ((inst >> 26) == 0x03)
  1668.                   offset = hppa_low_hppa_sign_extend (inst & 0x1f, 5);
  1669.                 else
  1670.                   offset = hppa_extract_14 (inst);

  1671.                 /* Handle code with and without frame pointers.  */
  1672.                 if (u->Save_SP)
  1673.                   cache->saved_regs[reg].addr = offset;
  1674.                 else
  1675.                   cache->saved_regs[reg].addr
  1676.                     = (u->Total_frame_size << 3) + offset;
  1677.               }
  1678.           }

  1679.         /* GCC handles callee saved FP regs a little differently.

  1680.            It emits an instruction to put the value of the start of
  1681.            the FP store area into %r1.  It then uses fstds,ma with a
  1682.            basereg of %r1 for the stores.

  1683.            HP CC emits them at the current stack pointer modifying the
  1684.            stack pointer as it stores each register.  */

  1685.         /* ldo X(%r3),%r1 or ldo X(%r30),%r1.  */
  1686.         if ((inst & 0xffffc000) == 0x34610000
  1687.             || (inst & 0xffffc000) == 0x37c10000)
  1688.           fp_loc = hppa_extract_14 (inst);

  1689.         reg = inst_saves_fr (inst);
  1690.         if (reg >= 12 && reg <= 21)
  1691.           {
  1692.             /* Note +4 braindamage below is necessary because the FP
  1693.                status registers are internally 8 registers rather than
  1694.                the expected 4 registers.  */
  1695.             saved_fr_mask &= ~(1 << reg);
  1696.             if (fp_loc == -1)
  1697.               {
  1698.                 /* 1st HP CC FP register store.  After this
  1699.                    instruction we've set enough state that the GCC and
  1700.                    HPCC code are both handled in the same manner.  */
  1701.                 cache->saved_regs[reg + HPPA_FP4_REGNUM + 4].addr = 0;
  1702.                 fp_loc = 8;
  1703.               }
  1704.             else
  1705.               {
  1706.                 cache->saved_regs[reg + HPPA_FP0_REGNUM + 4].addr = fp_loc;
  1707.                 fp_loc += 8;
  1708.               }
  1709.           }

  1710.         /* Quit if we hit any kind of branch the previous iteration.  */
  1711.         if (final_iteration)
  1712.           break;
  1713.         /* We want to look precisely one instruction beyond the branch
  1714.            if we have not found everything yet.  */
  1715.         if (is_branch (inst))
  1716.           final_iteration = 1;
  1717.       }
  1718.   }

  1719.   {
  1720.     /* The frame base always represents the value of %sp at entry to
  1721.        the current function (and is thus equivalent to the "saved"
  1722.        stack pointer.  */
  1723.     CORE_ADDR this_sp = get_frame_register_unsigned (this_frame,
  1724.                                                      HPPA_SP_REGNUM);
  1725.     CORE_ADDR fp;

  1726.     if (hppa_debug)
  1727.       fprintf_unfiltered (gdb_stdlog, " (this_sp=%s, pc=%s, "
  1728.                           "prologue_end=%s) ",
  1729.                           paddress (gdbarch, this_sp),
  1730.                           paddress (gdbarch, get_frame_pc (this_frame)),
  1731.                           paddress (gdbarch, prologue_end));

  1732.      /* Check to see if a frame pointer is available, and use it for
  1733.         frame unwinding if it is.

  1734.         There are some situations where we need to rely on the frame
  1735.         pointer to do stack unwinding.  For example, if a function calls
  1736.         alloca (), the stack pointer can get adjusted inside the body of
  1737.         the function.  In this case, the ABI requires that the compiler
  1738.         maintain a frame pointer for the function.

  1739.         The unwind record has a flag (alloca_frame) that indicates that
  1740.         a function has a variable frame; unfortunately, gcc/binutils
  1741.         does not set this flag.  Instead, whenever a frame pointer is used
  1742.         and saved on the stack, the Save_SP flag is set.  We use this to
  1743.         decide whether to use the frame pointer for unwinding.

  1744.         TODO: For the HP compiler, maybe we should use the alloca_frame flag
  1745.         instead of Save_SP.  */

  1746.      fp = get_frame_register_unsigned (this_frame, HPPA_FP_REGNUM);

  1747.      if (u->alloca_frame)
  1748.        fp -= u->Total_frame_size << 3;

  1749.      if (get_frame_pc (this_frame) >= prologue_end
  1750.          && (u->Save_SP || u->alloca_frame) && fp != 0)
  1751.       {
  1752.          cache->base = fp;

  1753.          if (hppa_debug)
  1754.           fprintf_unfiltered (gdb_stdlog, " (base=%s) [frame pointer]",
  1755.                               paddress (gdbarch, cache->base));
  1756.       }
  1757.      else if (u->Save_SP
  1758.               && trad_frame_addr_p (cache->saved_regs, HPPA_SP_REGNUM))
  1759.       {
  1760.             /* Both we're expecting the SP to be saved and the SP has been
  1761.                saved.  The entry SP value is saved at this frame's SP
  1762.                address.  */
  1763.             cache->base = read_memory_integer (this_sp, word_size, byte_order);

  1764.             if (hppa_debug)
  1765.               fprintf_unfiltered (gdb_stdlog, " (base=%s) [saved]",
  1766.                                   paddress (gdbarch, cache->base));
  1767.       }
  1768.     else
  1769.       {
  1770.         /* The prologue has been slowly allocating stack space.  Adjust
  1771.            the SP back.  */
  1772.         cache->base = this_sp - frame_size;
  1773.         if (hppa_debug)
  1774.           fprintf_unfiltered (gdb_stdlog, " (base=%s) [unwind adjust]",
  1775.                               paddress (gdbarch, cache->base));

  1776.       }
  1777.     trad_frame_set_value (cache->saved_regs, HPPA_SP_REGNUM, cache->base);
  1778.   }

  1779.   /* The PC is found in the "return register", "Millicode" uses "r31"
  1780.      as the return register while normal code uses "rp".  */
  1781.   if (u->Millicode)
  1782.     {
  1783.       if (trad_frame_addr_p (cache->saved_regs, 31))
  1784.         {
  1785.           cache->saved_regs[HPPA_PCOQ_HEAD_REGNUM] = cache->saved_regs[31];
  1786.           if (hppa_debug)
  1787.             fprintf_unfiltered (gdb_stdlog, " (pc=r31) [stack] } ");
  1788.         }
  1789.       else
  1790.         {
  1791.           ULONGEST r31 = get_frame_register_unsigned (this_frame, 31);
  1792.           trad_frame_set_value (cache->saved_regs, HPPA_PCOQ_HEAD_REGNUM, r31);
  1793.           if (hppa_debug)
  1794.             fprintf_unfiltered (gdb_stdlog, " (pc=r31) [frame] } ");
  1795.         }
  1796.     }
  1797.   else
  1798.     {
  1799.       if (trad_frame_addr_p (cache->saved_regs, HPPA_RP_REGNUM))
  1800.         {
  1801.           cache->saved_regs[HPPA_PCOQ_HEAD_REGNUM] =
  1802.             cache->saved_regs[HPPA_RP_REGNUM];
  1803.           if (hppa_debug)
  1804.             fprintf_unfiltered (gdb_stdlog, " (pc=rp) [stack] } ");
  1805.         }
  1806.       else
  1807.         {
  1808.           ULONGEST rp = get_frame_register_unsigned (this_frame,
  1809.                                                      HPPA_RP_REGNUM);
  1810.           trad_frame_set_value (cache->saved_regs, HPPA_PCOQ_HEAD_REGNUM, rp);
  1811.           if (hppa_debug)
  1812.             fprintf_unfiltered (gdb_stdlog, " (pc=rp) [frame] } ");
  1813.         }
  1814.     }

  1815.   /* If Save_SP is set, then we expect the frame pointer to be saved in the
  1816.      frame.  However, there is a one-insn window where we haven't saved it
  1817.      yet, but we've already clobbered it.  Detect this case and fix it up.

  1818.      The prologue sequence for frame-pointer functions is:
  1819.         0: stw %rp, -20(%sp)
  1820.         4: copy %r3, %r1
  1821.         8: copy %sp, %r3
  1822.         c: stw,ma %r1, XX(%sp)

  1823.      So if we are at offset c, the r3 value that we want is not yet saved
  1824.      on the stack, but it's been overwritten.  The prologue analyzer will
  1825.      set fp_in_r1 when it sees the copy insn so we know to get the value
  1826.      from r1 instead.  */
  1827.   if (u->Save_SP && !trad_frame_addr_p (cache->saved_regs, HPPA_FP_REGNUM)
  1828.       && fp_in_r1)
  1829.     {
  1830.       ULONGEST r1 = get_frame_register_unsigned (this_frame, 1);
  1831.       trad_frame_set_value (cache->saved_regs, HPPA_FP_REGNUM, r1);
  1832.     }

  1833.   {
  1834.     /* Convert all the offsets into addresses.  */
  1835.     int reg;
  1836.     for (reg = 0; reg < gdbarch_num_regs (gdbarch); reg++)
  1837.       {
  1838.         if (trad_frame_addr_p (cache->saved_regs, reg))
  1839.           cache->saved_regs[reg].addr += cache->base;
  1840.       }
  1841.   }

  1842.   {
  1843.     struct gdbarch_tdep *tdep;

  1844.     tdep = gdbarch_tdep (gdbarch);

  1845.     if (tdep->unwind_adjust_stub)
  1846.       tdep->unwind_adjust_stub (this_frame, cache->base, cache->saved_regs);
  1847.   }

  1848.   if (hppa_debug)
  1849.     fprintf_unfiltered (gdb_stdlog, "base=%s }",
  1850.       paddress (gdbarch, ((struct hppa_frame_cache *)*this_cache)->base));
  1851.   return (*this_cache);
  1852. }

  1853. static void
  1854. hppa_frame_this_id (struct frame_info *this_frame, void **this_cache,
  1855.                     struct frame_id *this_id)
  1856. {
  1857.   struct hppa_frame_cache *info;
  1858.   CORE_ADDR pc = get_frame_pc (this_frame);
  1859.   struct unwind_table_entry *u;

  1860.   info = hppa_frame_cache (this_frame, this_cache);
  1861.   u = hppa_find_unwind_entry_in_block (this_frame);

  1862.   (*this_id) = frame_id_build (info->base, u->region_start);
  1863. }

  1864. static struct value *
  1865. hppa_frame_prev_register (struct frame_info *this_frame,
  1866.                           void **this_cache, int regnum)
  1867. {
  1868.   struct hppa_frame_cache *info = hppa_frame_cache (this_frame, this_cache);

  1869.   return hppa_frame_prev_register_helper (this_frame,
  1870.                                           info->saved_regs, regnum);
  1871. }

  1872. static int
  1873. hppa_frame_unwind_sniffer (const struct frame_unwind *self,
  1874.                            struct frame_info *this_frame, void **this_cache)
  1875. {
  1876.   if (hppa_find_unwind_entry_in_block (this_frame))
  1877.     return 1;

  1878.   return 0;
  1879. }

  1880. static const struct frame_unwind hppa_frame_unwind =
  1881. {
  1882.   NORMAL_FRAME,
  1883.   default_frame_unwind_stop_reason,
  1884.   hppa_frame_this_id,
  1885.   hppa_frame_prev_register,
  1886.   NULL,
  1887.   hppa_frame_unwind_sniffer
  1888. };

  1889. /* This is a generic fallback frame unwinder that kicks in if we fail all
  1890.    the other ones.  Normally we would expect the stub and regular unwinder
  1891.    to work, but in some cases we might hit a function that just doesn't
  1892.    have any unwind information available.  In this case we try to do
  1893.    unwinding solely based on code reading.  This is obviously going to be
  1894.    slow, so only use this as a last resort.  Currently this will only
  1895.    identify the stack and pc for the frame.  */

  1896. static struct hppa_frame_cache *
  1897. hppa_fallback_frame_cache (struct frame_info *this_frame, void **this_cache)
  1898. {
  1899.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1900.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1901.   struct hppa_frame_cache *cache;
  1902.   unsigned int frame_size = 0;
  1903.   int found_rp = 0;
  1904.   CORE_ADDR start_pc;

  1905.   if (hppa_debug)
  1906.     fprintf_unfiltered (gdb_stdlog,
  1907.                         "{ hppa_fallback_frame_cache (frame=%d) -> ",
  1908.                         frame_relative_level (this_frame));

  1909.   cache = FRAME_OBSTACK_ZALLOC (struct hppa_frame_cache);
  1910.   (*this_cache) = cache;
  1911.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  1912.   start_pc = get_frame_func (this_frame);
  1913.   if (start_pc)
  1914.     {
  1915.       CORE_ADDR cur_pc = get_frame_pc (this_frame);
  1916.       CORE_ADDR pc;

  1917.       for (pc = start_pc; pc < cur_pc; pc += 4)
  1918.         {
  1919.           unsigned int insn;

  1920.           insn = read_memory_unsigned_integer (pc, 4, byte_order);
  1921.           frame_size += prologue_inst_adjust_sp (insn);

  1922.           /* There are limited ways to store the return pointer into the
  1923.              stack.  */
  1924.           if (insn == 0x6bc23fd9) /* stw rp,-0x14(sr0,sp) */
  1925.             {
  1926.               cache->saved_regs[HPPA_RP_REGNUM].addr = -20;
  1927.               found_rp = 1;
  1928.             }
  1929.           else if (insn == 0x0fc212c1
  1930.                    || insn == 0x73c23fe1) /* std rp,-0x10(sr0,sp) */
  1931.             {
  1932.               cache->saved_regs[HPPA_RP_REGNUM].addr = -16;
  1933.               found_rp = 1;
  1934.             }
  1935.         }
  1936.     }

  1937.   if (hppa_debug)
  1938.     fprintf_unfiltered (gdb_stdlog, " frame_size=%d, found_rp=%d }\n",
  1939.                         frame_size, found_rp);

  1940.   cache->base = get_frame_register_unsigned (this_frame, HPPA_SP_REGNUM);
  1941.   cache->base -= frame_size;
  1942.   trad_frame_set_value (cache->saved_regs, HPPA_SP_REGNUM, cache->base);

  1943.   if (trad_frame_addr_p (cache->saved_regs, HPPA_RP_REGNUM))
  1944.     {
  1945.       cache->saved_regs[HPPA_RP_REGNUM].addr += cache->base;
  1946.       cache->saved_regs[HPPA_PCOQ_HEAD_REGNUM] =
  1947.         cache->saved_regs[HPPA_RP_REGNUM];
  1948.     }
  1949.   else
  1950.     {
  1951.       ULONGEST rp;
  1952.       rp = get_frame_register_unsigned (this_frame, HPPA_RP_REGNUM);
  1953.       trad_frame_set_value (cache->saved_regs, HPPA_PCOQ_HEAD_REGNUM, rp);
  1954.     }

  1955.   return cache;
  1956. }

  1957. static void
  1958. hppa_fallback_frame_this_id (struct frame_info *this_frame, void **this_cache,
  1959.                              struct frame_id *this_id)
  1960. {
  1961.   struct hppa_frame_cache *info =
  1962.     hppa_fallback_frame_cache (this_frame, this_cache);

  1963.   (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
  1964. }

  1965. static struct value *
  1966. hppa_fallback_frame_prev_register (struct frame_info *this_frame,
  1967.                                    void **this_cache, int regnum)
  1968. {
  1969.   struct hppa_frame_cache *info
  1970.     = hppa_fallback_frame_cache (this_frame, this_cache);

  1971.   return hppa_frame_prev_register_helper (this_frame,
  1972.                                           info->saved_regs, regnum);
  1973. }

  1974. static const struct frame_unwind hppa_fallback_frame_unwind =
  1975. {
  1976.   NORMAL_FRAME,
  1977.   default_frame_unwind_stop_reason,
  1978.   hppa_fallback_frame_this_id,
  1979.   hppa_fallback_frame_prev_register,
  1980.   NULL,
  1981.   default_frame_sniffer
  1982. };

  1983. /* Stub frames, used for all kinds of call stubs.  */
  1984. struct hppa_stub_unwind_cache
  1985. {
  1986.   CORE_ADDR base;
  1987.   struct trad_frame_saved_reg *saved_regs;
  1988. };

  1989. static struct hppa_stub_unwind_cache *
  1990. hppa_stub_frame_unwind_cache (struct frame_info *this_frame,
  1991.                               void **this_cache)
  1992. {
  1993.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1994.   struct hppa_stub_unwind_cache *info;
  1995.   struct unwind_table_entry *u;

  1996.   if (*this_cache)
  1997.     return *this_cache;

  1998.   info = FRAME_OBSTACK_ZALLOC (struct hppa_stub_unwind_cache);
  1999.   *this_cache = info;
  2000.   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);

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

  2002.   if (gdbarch_osabi (gdbarch) == GDB_OSABI_HPUX_SOM)
  2003.     {
  2004.       /* HPUX uses export stubs in function calls; the export stub clobbers
  2005.          the return value of the caller, and, later restores it from the
  2006.          stack.  */
  2007.       u = find_unwind_entry (get_frame_pc (this_frame));

  2008.       if (u && u->stub_unwind.stub_type == EXPORT)
  2009.         {
  2010.           info->saved_regs[HPPA_PCOQ_HEAD_REGNUM].addr = info->base - 24;

  2011.           return info;
  2012.         }
  2013.     }

  2014.   /* By default we assume that stubs do not change the rp.  */
  2015.   info->saved_regs[HPPA_PCOQ_HEAD_REGNUM].realreg = HPPA_RP_REGNUM;

  2016.   return info;
  2017. }

  2018. static void
  2019. hppa_stub_frame_this_id (struct frame_info *this_frame,
  2020.                          void **this_prologue_cache,
  2021.                          struct frame_id *this_id)
  2022. {
  2023.   struct hppa_stub_unwind_cache *info
  2024.     = hppa_stub_frame_unwind_cache (this_frame, this_prologue_cache);

  2025.   if (info)
  2026.     *this_id = frame_id_build (info->base, get_frame_func (this_frame));
  2027. }

  2028. static struct value *
  2029. hppa_stub_frame_prev_register (struct frame_info *this_frame,
  2030.                                void **this_prologue_cache, int regnum)
  2031. {
  2032.   struct hppa_stub_unwind_cache *info
  2033.     = hppa_stub_frame_unwind_cache (this_frame, this_prologue_cache);

  2034.   if (info == NULL)
  2035.     error (_("Requesting registers from null frame."));

  2036.   return hppa_frame_prev_register_helper (this_frame,
  2037.                                           info->saved_regs, regnum);
  2038. }

  2039. static int
  2040. hppa_stub_unwind_sniffer (const struct frame_unwind *self,
  2041.                           struct frame_info *this_frame,
  2042.                           void **this_cache)
  2043. {
  2044.   CORE_ADDR pc = get_frame_address_in_block (this_frame);
  2045.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2046.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  2047.   if (pc == 0
  2048.       || (tdep->in_solib_call_trampoline != NULL
  2049.           && tdep->in_solib_call_trampoline (gdbarch, pc))
  2050.       || gdbarch_in_solib_return_trampoline (gdbarch, pc, NULL))
  2051.     return 1;
  2052.   return 0;
  2053. }

  2054. static const struct frame_unwind hppa_stub_frame_unwind = {
  2055.   NORMAL_FRAME,
  2056.   default_frame_unwind_stop_reason,
  2057.   hppa_stub_frame_this_id,
  2058.   hppa_stub_frame_prev_register,
  2059.   NULL,
  2060.   hppa_stub_unwind_sniffer
  2061. };

  2062. static struct frame_id
  2063. hppa_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  2064. {
  2065.   return frame_id_build (get_frame_register_unsigned (this_frame,
  2066.                                                       HPPA_SP_REGNUM),
  2067.                          get_frame_pc (this_frame));
  2068. }

  2069. CORE_ADDR
  2070. hppa_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  2071. {
  2072.   ULONGEST ipsw;
  2073.   CORE_ADDR pc;

  2074.   ipsw = frame_unwind_register_unsigned (next_frame, HPPA_IPSW_REGNUM);
  2075.   pc = frame_unwind_register_unsigned (next_frame, HPPA_PCOQ_HEAD_REGNUM);

  2076.   /* If the current instruction is nullified, then we are effectively
  2077.      still executing the previous instruction.  Pretend we are still
  2078.      there.  This is needed when single stepping; if the nullified
  2079.      instruction is on a different line, we don't want GDB to think
  2080.      we've stepped onto that line.  */
  2081.   if (ipsw & 0x00200000)
  2082.     pc -= 4;

  2083.   return pc & ~0x3;
  2084. }

  2085. /* Return the minimal symbol whose name is NAME and stub type is STUB_TYPE.
  2086.    Return NULL if no such symbol was found.  */

  2087. struct bound_minimal_symbol
  2088. hppa_lookup_stub_minimal_symbol (const char *name,
  2089.                                  enum unwind_stub_types stub_type)
  2090. {
  2091.   struct objfile *objfile;
  2092.   struct minimal_symbol *msym;
  2093.   struct bound_minimal_symbol result = { NULL, NULL };

  2094.   ALL_MSYMBOLS (objfile, msym)
  2095.     {
  2096.       if (strcmp (MSYMBOL_LINKAGE_NAME (msym), name) == 0)
  2097.         {
  2098.           struct unwind_table_entry *u;

  2099.           u = find_unwind_entry (MSYMBOL_VALUE (msym));
  2100.           if (u != NULL && u->stub_unwind.stub_type == stub_type)
  2101.             {
  2102.               result.objfile = objfile;
  2103.               result.minsym = msym;
  2104.               return result;
  2105.             }
  2106.         }
  2107.     }

  2108.   return result;
  2109. }

  2110. static void
  2111. unwind_command (char *exp, int from_tty)
  2112. {
  2113.   CORE_ADDR address;
  2114.   struct unwind_table_entry *u;

  2115.   /* If we have an expression, evaluate it and use it as the address.  */

  2116.   if (exp != 0 && *exp != 0)
  2117.     address = parse_and_eval_address (exp);
  2118.   else
  2119.     return;

  2120.   u = find_unwind_entry (address);

  2121.   if (!u)
  2122.     {
  2123.       printf_unfiltered ("Can't find unwind table entry for %s\n", exp);
  2124.       return;
  2125.     }

  2126.   printf_unfiltered ("unwind_table_entry (%s):\n", host_address_to_string (u));

  2127.   printf_unfiltered ("\tregion_start = %s\n", hex_string (u->region_start));
  2128.   gdb_flush (gdb_stdout);

  2129.   printf_unfiltered ("\tregion_end = %s\n", hex_string (u->region_end));
  2130.   gdb_flush (gdb_stdout);

  2131. #define pif(FLD) if (u->FLD) printf_unfiltered (" "#FLD);

  2132.   printf_unfiltered ("\n\tflags =");
  2133.   pif (Cannot_unwind);
  2134.   pif (Millicode);
  2135.   pif (Millicode_save_sr0);
  2136.   pif (Entry_SR);
  2137.   pif (Args_stored);
  2138.   pif (Variable_Frame);
  2139.   pif (Separate_Package_Body);
  2140.   pif (Frame_Extension_Millicode);
  2141.   pif (Stack_Overflow_Check);
  2142.   pif (Two_Instruction_SP_Increment);
  2143.   pif (sr4export);
  2144.   pif (cxx_info);
  2145.   pif (cxx_try_catch);
  2146.   pif (sched_entry_seq);
  2147.   pif (Save_SP);
  2148.   pif (Save_RP);
  2149.   pif (Save_MRP_in_frame);
  2150.   pif (save_r19);
  2151.   pif (Cleanup_defined);
  2152.   pif (MPE_XL_interrupt_marker);
  2153.   pif (HP_UX_interrupt_marker);
  2154.   pif (Large_frame);
  2155.   pif (alloca_frame);

  2156.   putchar_unfiltered ('\n');

  2157. #define pin(FLD) printf_unfiltered ("\t"#FLD" = 0x%x\n", u->FLD);

  2158.   pin (Region_description);
  2159.   pin (Entry_FR);
  2160.   pin (Entry_GR);
  2161.   pin (Total_frame_size);

  2162.   if (u->stub_unwind.stub_type)
  2163.     {
  2164.       printf_unfiltered ("\tstub type = ");
  2165.       switch (u->stub_unwind.stub_type)
  2166.         {
  2167.           case LONG_BRANCH:
  2168.             printf_unfiltered ("long branch\n");
  2169.             break;
  2170.           case PARAMETER_RELOCATION:
  2171.             printf_unfiltered ("parameter relocation\n");
  2172.             break;
  2173.           case EXPORT:
  2174.             printf_unfiltered ("export\n");
  2175.             break;
  2176.           case IMPORT:
  2177.             printf_unfiltered ("import\n");
  2178.             break;
  2179.           case IMPORT_SHLIB:
  2180.             printf_unfiltered ("import shlib\n");
  2181.             break;
  2182.           default:
  2183.             printf_unfiltered ("unknown (%d)\n", u->stub_unwind.stub_type);
  2184.         }
  2185.     }
  2186. }

  2187. /* Return the GDB type object for the "standard" data type of data in
  2188.    register REGNUM.  */

  2189. static struct type *
  2190. hppa32_register_type (struct gdbarch *gdbarch, int regnum)
  2191. {
  2192.    if (regnum < HPPA_FP4_REGNUM)
  2193.      return builtin_type (gdbarch)->builtin_uint32;
  2194.    else
  2195.      return builtin_type (gdbarch)->builtin_float;
  2196. }

  2197. static struct type *
  2198. hppa64_register_type (struct gdbarch *gdbarch, int regnum)
  2199. {
  2200.    if (regnum < HPPA64_FP4_REGNUM)
  2201.      return builtin_type (gdbarch)->builtin_uint64;
  2202.    else
  2203.      return builtin_type (gdbarch)->builtin_double;
  2204. }

  2205. /* Return non-zero if REGNUM is not a register available to the user
  2206.    through ptrace/ttrace.  */

  2207. static int
  2208. hppa32_cannot_store_register (struct gdbarch *gdbarch, int regnum)
  2209. {
  2210.   return (regnum == 0
  2211.           || regnum == HPPA_PCSQ_HEAD_REGNUM
  2212.           || (regnum >= HPPA_PCSQ_TAIL_REGNUM && regnum < HPPA_IPSW_REGNUM)
  2213.           || (regnum > HPPA_IPSW_REGNUM && regnum < HPPA_FP4_REGNUM));
  2214. }

  2215. static int
  2216. hppa32_cannot_fetch_register (struct gdbarch *gdbarch, int regnum)
  2217. {
  2218.   /* cr26 and cr27 are readable (but not writable) from userspace.  */
  2219.   if (regnum == HPPA_CR26_REGNUM || regnum == HPPA_CR27_REGNUM)
  2220.     return 0;
  2221.   else
  2222.     return hppa32_cannot_store_register (gdbarch, regnum);
  2223. }

  2224. static int
  2225. hppa64_cannot_store_register (struct gdbarch *gdbarch, int regnum)
  2226. {
  2227.   return (regnum == 0
  2228.           || regnum == HPPA_PCSQ_HEAD_REGNUM
  2229.           || (regnum >= HPPA_PCSQ_TAIL_REGNUM && regnum < HPPA_IPSW_REGNUM)
  2230.           || (regnum > HPPA_IPSW_REGNUM && regnum < HPPA64_FP4_REGNUM));
  2231. }

  2232. static int
  2233. hppa64_cannot_fetch_register (struct gdbarch *gdbarch, int regnum)
  2234. {
  2235.   /* cr26 and cr27 are readable (but not writable) from userspace.  */
  2236.   if (regnum == HPPA_CR26_REGNUM || regnum == HPPA_CR27_REGNUM)
  2237.     return 0;
  2238.   else
  2239.     return hppa64_cannot_store_register (gdbarch, regnum);
  2240. }

  2241. static CORE_ADDR
  2242. hppa_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR addr)
  2243. {
  2244.   /* The low two bits of the PC on the PA contain the privilege level.
  2245.      Some genius implementing a (non-GCC) compiler apparently decided
  2246.      this means that "addresses" in a text section therefore include a
  2247.      privilege level, and thus symbol tables should contain these bits.
  2248.      This seems like a bonehead thing to do--anyway, it seems to work
  2249.      for our purposes to just ignore those bits.  */

  2250.   return (addr &= ~0x3);
  2251. }

  2252. /* Get the ARGIth function argument for the current function.  */

  2253. static CORE_ADDR
  2254. hppa_fetch_pointer_argument (struct frame_info *frame, int argi,
  2255.                              struct type *type)
  2256. {
  2257.   return get_frame_register_unsigned (frame, HPPA_R0_REGNUM + 26 - argi);
  2258. }

  2259. static enum register_status
  2260. hppa_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
  2261.                            int regnum, gdb_byte *buf)
  2262. {
  2263.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2264.   ULONGEST tmp;
  2265.   enum register_status status;

  2266.   status = regcache_raw_read_unsigned (regcache, regnum, &tmp);
  2267.   if (status == REG_VALID)
  2268.     {
  2269.       if (regnum == HPPA_PCOQ_HEAD_REGNUM || regnum == HPPA_PCOQ_TAIL_REGNUM)
  2270.         tmp &= ~0x3;
  2271.       store_unsigned_integer (buf, sizeof tmp, byte_order, tmp);
  2272.     }
  2273.   return status;
  2274. }

  2275. static CORE_ADDR
  2276. hppa_find_global_pointer (struct gdbarch *gdbarch, struct value *function)
  2277. {
  2278.   return 0;
  2279. }

  2280. struct value *
  2281. hppa_frame_prev_register_helper (struct frame_info *this_frame,
  2282.                                  struct trad_frame_saved_reg saved_regs[],
  2283.                                  int regnum)
  2284. {
  2285.   struct gdbarch *arch = get_frame_arch (this_frame);
  2286.   enum bfd_endian byte_order = gdbarch_byte_order (arch);

  2287.   if (regnum == HPPA_PCOQ_TAIL_REGNUM)
  2288.     {
  2289.       int size = register_size (arch, HPPA_PCOQ_HEAD_REGNUM);
  2290.       CORE_ADDR pc;
  2291.       struct value *pcoq_val =
  2292.         trad_frame_get_prev_register (this_frame, saved_regs,
  2293.                                       HPPA_PCOQ_HEAD_REGNUM);

  2294.       pc = extract_unsigned_integer (value_contents_all (pcoq_val),
  2295.                                      size, byte_order);
  2296.       return frame_unwind_got_constant (this_frame, regnum, pc + 4);
  2297.     }

  2298.   /* Make sure the "flags" register is zero in all unwound frames.
  2299.      The "flags" registers is a HP-UX specific wart, and only the code
  2300.      in hppa-hpux-tdep.c depends on it.  However, it is easier to deal
  2301.      with it here.  This shouldn't affect other systems since those
  2302.      should provide zero for the "flags" register anyway.  */
  2303.   if (regnum == HPPA_FLAGS_REGNUM)
  2304.     return frame_unwind_got_constant (this_frame, regnum, 0);

  2305.   return trad_frame_get_prev_register (this_frame, saved_regs, regnum);
  2306. }


  2307. /* An instruction to match.  */
  2308. struct insn_pattern
  2309. {
  2310.   unsigned int data;            /* See if it matches this....  */
  2311.   unsigned int mask;            /* ... with this mask.  */
  2312. };

  2313. /* See bfd/elf32-hppa.c */
  2314. static struct insn_pattern hppa_long_branch_stub[] = {
  2315.   /* ldil LR'xxx,%r1 */
  2316.   { 0x20200000, 0xffe00000 },
  2317.   /* be,n RR'xxx(%sr4,%r1) */
  2318.   { 0xe0202002, 0xffe02002 },
  2319.   { 0, 0 }
  2320. };

  2321. static struct insn_pattern hppa_long_branch_pic_stub[] = {
  2322.   /* b,l .+8, %r1 */
  2323.   { 0xe8200000, 0xffe00000 },
  2324.   /* addil LR'xxx - ($PIC_pcrel$0 - 4), %r1 */
  2325.   { 0x28200000, 0xffe00000 },
  2326.   /* be,n RR'xxxx - ($PIC_pcrel$0 - 8)(%sr4, %r1) */
  2327.   { 0xe0202002, 0xffe02002 },
  2328.   { 0, 0 }
  2329. };

  2330. static struct insn_pattern hppa_import_stub[] = {
  2331.   /* addil LR'xxx, %dp */
  2332.   { 0x2b600000, 0xffe00000 },
  2333.   /* ldw RR'xxx(%r1), %r21 */
  2334.   { 0x48350000, 0xffffb000 },
  2335.   /* bv %r0(%r21) */
  2336.   { 0xeaa0c000, 0xffffffff },
  2337.   /* ldw RR'xxx+4(%r1), %r19 */
  2338.   { 0x48330000, 0xffffb000 },
  2339.   { 0, 0 }
  2340. };

  2341. static struct insn_pattern hppa_import_pic_stub[] = {
  2342.   /* addil LR'xxx,%r19 */
  2343.   { 0x2a600000, 0xffe00000 },
  2344.   /* ldw RR'xxx(%r1),%r21 */
  2345.   { 0x48350000, 0xffffb000 },
  2346.   /* bv %r0(%r21) */
  2347.   { 0xeaa0c000, 0xffffffff },
  2348.   /* ldw RR'xxx+4(%r1),%r19 */
  2349.   { 0x48330000, 0xffffb000 },
  2350.   { 0, 0 },
  2351. };

  2352. static struct insn_pattern hppa_plt_stub[] = {
  2353.   /* b,l 1b, %r20 - 1b is 3 insns before here */
  2354.   { 0xea9f1fdd, 0xffffffff },
  2355.   /* depi 0,31,2,%r20 */
  2356.   { 0xd6801c1e, 0xffffffff },
  2357.   { 0, 0 }
  2358. };

  2359. /* Maximum number of instructions on the patterns above.  */
  2360. #define HPPA_MAX_INSN_PATTERN_LEN        4

  2361. /* Return non-zero if the instructions at PC match the series
  2362.    described in PATTERN, or zero otherwise.  PATTERN is an array of
  2363.    'struct insn_pattern' objects, terminated by an entry whose mask is
  2364.    zero.

  2365.    When the match is successful, fill INSN[i] with what PATTERN[i]
  2366.    matched.  */

  2367. static int
  2368. hppa_match_insns (struct gdbarch *gdbarch, CORE_ADDR pc,
  2369.                   struct insn_pattern *pattern, unsigned int *insn)
  2370. {
  2371.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2372.   CORE_ADDR npc = pc;
  2373.   int i;

  2374.   for (i = 0; pattern[i].mask; i++)
  2375.     {
  2376.       gdb_byte buf[HPPA_INSN_SIZE];

  2377.       target_read_memory (npc, buf, HPPA_INSN_SIZE);
  2378.       insn[i] = extract_unsigned_integer (buf, HPPA_INSN_SIZE, byte_order);
  2379.       if ((insn[i] & pattern[i].mask) == pattern[i].data)
  2380.         npc += 4;
  2381.       else
  2382.         return 0;
  2383.     }

  2384.   return 1;
  2385. }

  2386. /* This relaxed version of the insstruction matcher allows us to match
  2387.    from somewhere inside the pattern, by looking backwards in the
  2388.    instruction scheme.  */

  2389. static int
  2390. hppa_match_insns_relaxed (struct gdbarch *gdbarch, CORE_ADDR pc,
  2391.                           struct insn_pattern *pattern, unsigned int *insn)
  2392. {
  2393.   int offset, len = 0;

  2394.   while (pattern[len].mask)
  2395.     len++;

  2396.   for (offset = 0; offset < len; offset++)
  2397.     if (hppa_match_insns (gdbarch, pc - offset * HPPA_INSN_SIZE,
  2398.                           pattern, insn))
  2399.       return 1;

  2400.   return 0;
  2401. }

  2402. static int
  2403. hppa_in_dyncall (CORE_ADDR pc)
  2404. {
  2405.   struct unwind_table_entry *u;

  2406.   u = find_unwind_entry (hppa_symbol_address ("$$dyncall"));
  2407.   if (!u)
  2408.     return 0;

  2409.   return (pc >= u->region_start && pc <= u->region_end);
  2410. }

  2411. int
  2412. hppa_in_solib_call_trampoline (struct gdbarch *gdbarch, CORE_ADDR pc)
  2413. {
  2414.   unsigned int insn[HPPA_MAX_INSN_PATTERN_LEN];
  2415.   struct unwind_table_entry *u;

  2416.   if (in_plt_section (pc) || hppa_in_dyncall (pc))
  2417.     return 1;

  2418.   /* The GNU toolchain produces linker stubs without unwind
  2419.      information.  Since the pattern matching for linker stubs can be
  2420.      quite slow, so bail out if we do have an unwind entry.  */

  2421.   u = find_unwind_entry (pc);
  2422.   if (u != NULL)
  2423.     return 0;

  2424.   return
  2425.     (hppa_match_insns_relaxed (gdbarch, pc, hppa_import_stub, insn)
  2426.      || hppa_match_insns_relaxed (gdbarch, pc, hppa_import_pic_stub, insn)
  2427.      || hppa_match_insns_relaxed (gdbarch, pc, hppa_long_branch_stub, insn)
  2428.      || hppa_match_insns_relaxed (gdbarch, pc,
  2429.                                   hppa_long_branch_pic_stub, insn));
  2430. }

  2431. /* This code skips several kind of "trampolines" used on PA-RISC
  2432.    systems: $$dyncall, import stubs and PLT stubs.  */

  2433. CORE_ADDR
  2434. hppa_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
  2435. {
  2436.   struct gdbarch *gdbarch = get_frame_arch (frame);
  2437.   struct type *func_ptr_type = builtin_type (gdbarch)->builtin_func_ptr;

  2438.   unsigned int insn[HPPA_MAX_INSN_PATTERN_LEN];
  2439.   int dp_rel;

  2440.   /* $$dyncall handles both PLABELs and direct addresses.  */
  2441.   if (hppa_in_dyncall (pc))
  2442.     {
  2443.       pc = get_frame_register_unsigned (frame, HPPA_R0_REGNUM + 22);

  2444.       /* PLABELs have bit 30 set; if it's a PLABEL, then dereference it.  */
  2445.       if (pc & 0x2)
  2446.         pc = read_memory_typed_address (pc & ~0x3, func_ptr_type);

  2447.       return pc;
  2448.     }

  2449.   dp_rel = hppa_match_insns (gdbarch, pc, hppa_import_stub, insn);
  2450.   if (dp_rel || hppa_match_insns (gdbarch, pc, hppa_import_pic_stub, insn))
  2451.     {
  2452.       /* Extract the target address from the addil/ldw sequence.  */
  2453.       pc = hppa_extract_21 (insn[0]) + hppa_extract_14 (insn[1]);

  2454.       if (dp_rel)
  2455.         pc += get_frame_register_unsigned (frame, HPPA_DP_REGNUM);
  2456.       else
  2457.         pc += get_frame_register_unsigned (frame, HPPA_R0_REGNUM + 19);

  2458.       /* fallthrough */
  2459.     }

  2460.   if (in_plt_section (pc))
  2461.     {
  2462.       pc = read_memory_typed_address (pc, func_ptr_type);

  2463.       /* If the PLT slot has not yet been resolved, the target will be
  2464.          the PLT stub.  */
  2465.       if (in_plt_section (pc))
  2466.         {
  2467.           /* Sanity check: are we pointing to the PLT stub?  */
  2468.             if (!hppa_match_insns (gdbarch, pc, hppa_plt_stub, insn))
  2469.             {
  2470.               warning (_("Cannot resolve PLT stub at %s."),
  2471.                        paddress (gdbarch, pc));
  2472.               return 0;
  2473.             }

  2474.           /* This should point to the fixup routine.  */
  2475.           pc = read_memory_typed_address (pc + 8, func_ptr_type);
  2476.         }
  2477.     }

  2478.   return pc;
  2479. }


  2480. /* Here is a table of C type sizes on hppa with various compiles
  2481.    and options.  I measured this on PA 9000/800 with HP-UX 11.11
  2482.    and these compilers:

  2483.      /usr/ccs/bin/cc    HP92453-01 A.11.01.21
  2484.      /opt/ansic/bin/cc  HP92453-01 B.11.11.28706.GP
  2485.      /opt/aCC/bin/aCC   B3910B A.03.45
  2486.      gcc                gcc 3.3.2 native hppa2.0w-hp-hpux11.11

  2487.      cc            : 1 2 4 4 8 : 4 8 -- : 4 4
  2488.      ansic +DA1.1  : 1 2 4 4 8 : 4 8 16 : 4 4
  2489.      ansic +DA2.0  : 1 2 4 4 8 : 4 8 16 : 4 4
  2490.      ansic +DA2.0W : 1 2 4 8 8 : 4 8 16 : 8 8
  2491.      acc   +DA1.1  : 1 2 4 4 8 : 4 8 16 : 4 4
  2492.      acc   +DA2.0  : 1 2 4 4 8 : 4 8 16 : 4 4
  2493.      acc   +DA2.0W : 1 2 4 8 8 : 4 8 16 : 8 8
  2494.      gcc           : 1 2 4 4 8 : 4 8 16 : 4 4

  2495.    Each line is:

  2496.      compiler and options
  2497.      char, short, int, long, long long
  2498.      float, double, long double
  2499.      char *, void (*)()

  2500.    So all these compilers use either ILP32 or LP64 model.
  2501.    TODO: gcc has more options so it needs more investigation.

  2502.    For floating point types, see:

  2503.      http://docs.hp.com/hpux/pdf/B3906-90006.pdf
  2504.      HP-UX floating-point guide, hpux 11.00

  2505.    -- chastain 2003-12-18  */

  2506. static struct gdbarch *
  2507. hppa_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  2508. {
  2509.   struct gdbarch_tdep *tdep;
  2510.   struct gdbarch *gdbarch;

  2511.   /* Try to determine the ABI of the object we are loading.  */
  2512.   if (info.abfd != NULL && info.osabi == GDB_OSABI_UNKNOWN)
  2513.     {
  2514.       /* If it's a SOM file, assume it's HP/UX SOM.  */
  2515.       if (bfd_get_flavour (info.abfd) == bfd_target_som_flavour)
  2516.         info.osabi = GDB_OSABI_HPUX_SOM;
  2517.     }

  2518.   /* find a candidate among the list of pre-declared architectures.  */
  2519.   arches = gdbarch_list_lookup_by_info (arches, &info);
  2520.   if (arches != NULL)
  2521.     return (arches->gdbarch);

  2522.   /* If none found, then allocate and initialize one.  */
  2523.   tdep = XCNEW (struct gdbarch_tdep);
  2524.   gdbarch = gdbarch_alloc (&info, tdep);

  2525.   /* Determine from the bfd_arch_info structure if we are dealing with
  2526.      a 32 or 64 bits architecture.  If the bfd_arch_info is not available,
  2527.      then default to a 32bit machine.  */
  2528.   if (info.bfd_arch_info != NULL)
  2529.     tdep->bytes_per_address =
  2530.       info.bfd_arch_info->bits_per_address / info.bfd_arch_info->bits_per_byte;
  2531.   else
  2532.     tdep->bytes_per_address = 4;

  2533.   tdep->find_global_pointer = hppa_find_global_pointer;

  2534.   /* Some parts of the gdbarch vector depend on whether we are running
  2535.      on a 32 bits or 64 bits target.  */
  2536.   switch (tdep->bytes_per_address)
  2537.     {
  2538.       case 4:
  2539.         set_gdbarch_num_regs (gdbarch, hppa32_num_regs);
  2540.         set_gdbarch_register_name (gdbarch, hppa32_register_name);
  2541.         set_gdbarch_register_type (gdbarch, hppa32_register_type);
  2542.         set_gdbarch_cannot_store_register (gdbarch,
  2543.                                            hppa32_cannot_store_register);
  2544.         set_gdbarch_cannot_fetch_register (gdbarch,
  2545.                                            hppa32_cannot_fetch_register);
  2546.         break;
  2547.       case 8:
  2548.         set_gdbarch_num_regs (gdbarch, hppa64_num_regs);
  2549.         set_gdbarch_register_name (gdbarch, hppa64_register_name);
  2550.         set_gdbarch_register_type (gdbarch, hppa64_register_type);
  2551.         set_gdbarch_dwarf2_reg_to_regnum (gdbarch, hppa64_dwarf_reg_to_regnum);
  2552.         set_gdbarch_cannot_store_register (gdbarch,
  2553.                                            hppa64_cannot_store_register);
  2554.         set_gdbarch_cannot_fetch_register (gdbarch,
  2555.                                            hppa64_cannot_fetch_register);
  2556.         break;
  2557.       default:
  2558.         internal_error (__FILE__, __LINE__, _("Unsupported address size: %d"),
  2559.                         tdep->bytes_per_address);
  2560.     }

  2561.   set_gdbarch_long_bit (gdbarch, tdep->bytes_per_address * TARGET_CHAR_BIT);
  2562.   set_gdbarch_ptr_bit (gdbarch, tdep->bytes_per_address * TARGET_CHAR_BIT);

  2563.   /* The following gdbarch vector elements are the same in both ILP32
  2564.      and LP64, but might show differences some day.  */
  2565.   set_gdbarch_long_long_bit (gdbarch, 64);
  2566.   set_gdbarch_long_double_bit (gdbarch, 128);
  2567.   set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);

  2568.   /* The following gdbarch vector elements do not depend on the address
  2569.      size, or in any other gdbarch element previously set.  */
  2570.   set_gdbarch_skip_prologue (gdbarch, hppa_skip_prologue);
  2571.   set_gdbarch_in_function_epilogue_p (gdbarch,
  2572.                                       hppa_in_function_epilogue_p);
  2573.   set_gdbarch_inner_than (gdbarch, core_addr_greaterthan);
  2574.   set_gdbarch_sp_regnum (gdbarch, HPPA_SP_REGNUM);
  2575.   set_gdbarch_fp0_regnum (gdbarch, HPPA_FP0_REGNUM);
  2576.   set_gdbarch_addr_bits_remove (gdbarch, hppa_addr_bits_remove);
  2577.   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
  2578.   set_gdbarch_read_pc (gdbarch, hppa_read_pc);
  2579.   set_gdbarch_write_pc (gdbarch, hppa_write_pc);

  2580.   /* Helper for function argument information.  */
  2581.   set_gdbarch_fetch_pointer_argument (gdbarch, hppa_fetch_pointer_argument);

  2582.   set_gdbarch_print_insn (gdbarch, print_insn_hppa);

  2583.   /* When a hardware watchpoint triggers, we'll move the inferior past
  2584.      it by removing all eventpoints; stepping past the instruction
  2585.      that caused the trigger; reinserting eventpoints; and checking
  2586.      whether any watched location changed.  */
  2587.   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);

  2588.   /* Inferior function call methods.  */
  2589.   switch (tdep->bytes_per_address)
  2590.     {
  2591.     case 4:
  2592.       set_gdbarch_push_dummy_call (gdbarch, hppa32_push_dummy_call);
  2593.       set_gdbarch_frame_align (gdbarch, hppa32_frame_align);
  2594.       set_gdbarch_convert_from_func_ptr_addr
  2595.         (gdbarch, hppa32_convert_from_func_ptr_addr);
  2596.       break;
  2597.     case 8:
  2598.       set_gdbarch_push_dummy_call (gdbarch, hppa64_push_dummy_call);
  2599.       set_gdbarch_frame_align (gdbarch, hppa64_frame_align);
  2600.       break;
  2601.     default:
  2602.       internal_error (__FILE__, __LINE__, _("bad switch"));
  2603.     }

  2604.   /* Struct return methods.  */
  2605.   switch (tdep->bytes_per_address)
  2606.     {
  2607.     case 4:
  2608.       set_gdbarch_return_value (gdbarch, hppa32_return_value);
  2609.       break;
  2610.     case 8:
  2611.       set_gdbarch_return_value (gdbarch, hppa64_return_value);
  2612.       break;
  2613.     default:
  2614.       internal_error (__FILE__, __LINE__, _("bad switch"));
  2615.     }

  2616.   set_gdbarch_breakpoint_from_pc (gdbarch, hppa_breakpoint_from_pc);
  2617.   set_gdbarch_pseudo_register_read (gdbarch, hppa_pseudo_register_read);

  2618.   /* Frame unwind methods.  */
  2619.   set_gdbarch_dummy_id (gdbarch, hppa_dummy_id);
  2620.   set_gdbarch_unwind_pc (gdbarch, hppa_unwind_pc);

  2621.   /* Hook in ABI-specific overrides, if they have been registered.  */
  2622.   gdbarch_init_osabi (info, gdbarch);

  2623.   /* Hook in the default unwinders.  */
  2624.   frame_unwind_append_unwinder (gdbarch, &hppa_stub_frame_unwind);
  2625.   frame_unwind_append_unwinder (gdbarch, &hppa_frame_unwind);
  2626.   frame_unwind_append_unwinder (gdbarch, &hppa_fallback_frame_unwind);

  2627.   return gdbarch;
  2628. }

  2629. static void
  2630. hppa_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
  2631. {
  2632.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  2633.   fprintf_unfiltered (file, "bytes_per_address = %d\n",
  2634.                       tdep->bytes_per_address);
  2635.   fprintf_unfiltered (file, "elf = %s\n", tdep->is_elf ? "yes" : "no");
  2636. }

  2637. /* Provide a prototype to silence -Wmissing-prototypes.  */
  2638. extern initialize_file_ftype _initialize_hppa_tdep;

  2639. void
  2640. _initialize_hppa_tdep (void)
  2641. {
  2642.   struct cmd_list_element *c;

  2643.   gdbarch_register (bfd_arch_hppa, hppa_gdbarch_init, hppa_dump_tdep);

  2644.   hppa_objfile_priv_data = register_objfile_data ();

  2645.   add_cmd ("unwind", class_maintenance, unwind_command,
  2646.            _("Print unwind table entry at given address."),
  2647.            &maintenanceprintlist);

  2648.   /* Debug this files internals.  */
  2649.   add_setshow_boolean_cmd ("hppa", class_maintenance, &hppa_debug, _("\
  2650. Set whether hppa target specific debugging information should be displayed."),
  2651.                            _("\
  2652. Show whether hppa target specific debugging information is displayed."), _("\
  2653. This flag controls whether hppa target specific debugging information is\n\
  2654. displayed.  This information is particularly useful for debugging frame\n\
  2655. unwinding problems."),
  2656.                            NULL,
  2657.                            NULL, /* FIXME: i18n: hppa debug flag is %s.  */
  2658.                            &setdebuglist, &showdebuglist);
  2659. }