gdb/ia64-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for the IA-64 for GDB, the GNU debugger.

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

  3.    This file is part of GDB.

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

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

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

  14. #include "defs.h"
  15. #include "inferior.h"
  16. #include "gdbcore.h"
  17. #include "arch-utils.h"
  18. #include "floatformat.h"
  19. #include "gdbtypes.h"
  20. #include "regcache.h"
  21. #include "reggroups.h"
  22. #include "frame.h"
  23. #include "frame-base.h"
  24. #include "frame-unwind.h"
  25. #include "doublest.h"
  26. #include "value.h"
  27. #include "objfiles.h"
  28. #include "elf/common.h"                /* for DT_PLTGOT value */
  29. #include "elf-bfd.h"
  30. #include "dis-asm.h"
  31. #include "infcall.h"
  32. #include "osabi.h"
  33. #include "ia64-tdep.h"
  34. #include "cp-abi.h"

  35. #ifdef HAVE_LIBUNWIND_IA64_H
  36. #include "elf/ia64.h"           /* for PT_IA_64_UNWIND value */
  37. #include "ia64-libunwind-tdep.h"

  38. /* Note: KERNEL_START is supposed to be an address which is not going
  39.          to ever contain any valid unwind info.  For ia64 linux, the choice
  40.          of 0xc000000000000000 is fairly safe since that's uncached space.

  41.          We use KERNEL_START as follows: after obtaining the kernel's
  42.          unwind table via getunwind(), we project its unwind data into
  43.          address-range KERNEL_START-(KERNEL_START+ktab_size) and then
  44.          when ia64_access_mem() sees a memory access to this
  45.          address-range, we redirect it to ktab instead.

  46.          None of this hackery is needed with a modern kernel/libcs
  47.          which uses the kernel virtual DSO to provide access to the
  48.          kernel's unwind info.  In that case, ktab_size remains 0 and
  49.          hence the value of KERNEL_START doesn't matter.  */

  50. #define KERNEL_START 0xc000000000000000ULL

  51. static size_t ktab_size = 0;
  52. struct ia64_table_entry
  53.   {
  54.     uint64_t start_offset;
  55.     uint64_t end_offset;
  56.     uint64_t info_offset;
  57.   };

  58. static struct ia64_table_entry *ktab = NULL;

  59. #endif

  60. /* An enumeration of the different IA-64 instruction types.  */

  61. typedef enum instruction_type
  62. {
  63.   A,                        /* Integer ALU ;    I-unit or M-unit */
  64.   I,                        /* Non-ALU integer; I-unit */
  65.   M,                        /* Memory ;         M-unit */
  66.   F,                        /* Floating-point ; F-unit */
  67.   B,                        /* Branch ;         B-unit */
  68.   L,                        /* Extended (L+X) ; I-unit */
  69.   X,                        /* Extended (L+X) ; I-unit */
  70.   undefined                /* undefined or reserved */
  71. } instruction_type;

  72. /* We represent IA-64 PC addresses as the value of the instruction
  73.    pointer or'd with some bit combination in the low nibble which
  74.    represents the slot number in the bundle addressed by the
  75.    instruction pointer.  The problem is that the Linux kernel
  76.    multiplies its slot numbers (for exceptions) by one while the
  77.    disassembler multiplies its slot numbers by 6.  In addition, I've
  78.    heard it said that the simulator uses 1 as the multiplier.

  79.    I've fixed the disassembler so that the bytes_per_line field will
  80.    be the slot multiplier.  If bytes_per_line comes in as zero, it
  81.    is set to six (which is how it was set up initially). -- objdump
  82.    displays pretty disassembly dumps with this value.  For our purposes,
  83.    we'll set bytes_per_line to SLOT_MULTIPLIER. This is okay since we
  84.    never want to also display the raw bytes the way objdump does.  */

  85. #define SLOT_MULTIPLIER 1

  86. /* Length in bytes of an instruction bundle.  */

  87. #define BUNDLE_LEN 16

  88. /* See the saved memory layout comment for ia64_memory_insert_breakpoint.  */

  89. #if BREAKPOINT_MAX < BUNDLE_LEN - 2
  90. # error "BREAKPOINT_MAX < BUNDLE_LEN - 2"
  91. #endif

  92. static gdbarch_init_ftype ia64_gdbarch_init;

  93. static gdbarch_register_name_ftype ia64_register_name;
  94. static gdbarch_register_type_ftype ia64_register_type;
  95. static gdbarch_breakpoint_from_pc_ftype ia64_breakpoint_from_pc;
  96. static gdbarch_skip_prologue_ftype ia64_skip_prologue;
  97. static struct type *is_float_or_hfa_type (struct type *t);
  98. static CORE_ADDR ia64_find_global_pointer (struct gdbarch *gdbarch,
  99.                                            CORE_ADDR faddr);

  100. #define NUM_IA64_RAW_REGS 462

  101. static int sp_regnum = IA64_GR12_REGNUM;
  102. static int fp_regnum = IA64_VFP_REGNUM;
  103. static int lr_regnum = IA64_VRAP_REGNUM;

  104. /* NOTE: we treat the register stack registers r32-r127 as
  105.    pseudo-registers because they may not be accessible via the ptrace
  106.    register get/set interfaces.  */

  107. enum pseudo_regs { FIRST_PSEUDO_REGNUM = NUM_IA64_RAW_REGS,
  108.                    VBOF_REGNUM = IA64_NAT127_REGNUM + 1, V32_REGNUM,
  109.                    V127_REGNUM = V32_REGNUM + 95,
  110.                    VP0_REGNUM, VP16_REGNUM = VP0_REGNUM + 16,
  111.                    VP63_REGNUM = VP0_REGNUM + 63, LAST_PSEUDO_REGNUM };

  112. /* Array of register names; There should be ia64_num_regs strings in
  113.    the initializer.  */

  114. static char *ia64_register_names[] =
  115. { "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
  116.   "r8",   "r9",   "r10""r11""r12""r13""r14""r15",
  117.   "r16""r17""r18""r19""r20""r21""r22""r23",
  118.   "r24""r25""r26""r27""r28""r29""r30""r31",
  119.   "",     "",     "",     "",     "",     "",     "",     "",
  120.   "",     "",     "",     "",     "",     "",     "",     "",
  121.   "",     "",     "",     "",     "",     "",     "",     "",
  122.   "",     "",     "",     "",     "",     "",     "",     "",
  123.   "",     "",     "",     "",     "",     "",     "",     "",
  124.   "",     "",     "",     "",     "",     "",     "",     "",
  125.   "",     "",     "",     "",     "",     "",     "",     "",
  126.   "",     "",     "",     "",     "",     "",     "",     "",
  127.   "",     "",     "",     "",     "",     "",     "",     "",
  128.   "",     "",     "",     "",     "",     "",     "",     "",
  129.   "",     "",     "",     "",     "",     "",     "",     "",
  130.   "",     "",     "",     "",     "",     "",     "",     "",

  131.   "f0",   "f1",   "f2",   "f3",   "f4",   "f5",   "f6",   "f7",
  132.   "f8",   "f9",   "f10""f11""f12""f13""f14""f15",
  133.   "f16""f17""f18""f19""f20""f21""f22""f23",
  134.   "f24""f25""f26""f27""f28""f29""f30""f31",
  135.   "f32""f33""f34""f35""f36""f37""f38""f39",
  136.   "f40""f41""f42""f43""f44""f45""f46""f47",
  137.   "f48""f49""f50""f51""f52""f53""f54""f55",
  138.   "f56""f57""f58""f59""f60""f61""f62""f63",
  139.   "f64""f65""f66""f67""f68""f69""f70""f71",
  140.   "f72""f73""f74""f75""f76""f77""f78""f79",
  141.   "f80""f81""f82""f83""f84""f85""f86""f87",
  142.   "f88""f89""f90""f91""f92""f93""f94""f95",
  143.   "f96""f97""f98""f99""f100", "f101", "f102", "f103",
  144.   "f104", "f105", "f106", "f107", "f108", "f109", "f110", "f111",
  145.   "f112", "f113", "f114", "f115", "f116", "f117", "f118", "f119",
  146.   "f120", "f121", "f122", "f123", "f124", "f125", "f126", "f127",

  147.   "",     "",     "",     "",     "",     "",     "",     "",
  148.   "",     "",     "",     "",     "",     "",     "",     "",
  149.   "",     "",     "",     "",     "",     "",     "",     "",
  150.   "",     "",     "",     "",     "",     "",     "",     "",
  151.   "",     "",     "",     "",     "",     "",     "",     "",
  152.   "",     "",     "",     "",     "",     "",     "",     "",
  153.   "",     "",     "",     "",     "",     "",     "",     "",
  154.   "",     "",     "",     "",     "",     "",     "",     "",

  155.   "b0",   "b1",   "b2",   "b3",   "b4",   "b5",   "b6",   "b7",

  156.   "vfp", "vrap",

  157.   "pr", "ip", "psr", "cfm",

  158.   "kr0",   "kr1",   "kr2",   "kr3",   "kr4",   "kr5",   "kr6",   "kr7",
  159.   "", "", "", "", "", "", "", "",
  160.   "rsc", "bsp", "bspstore", "rnat",
  161.   "", "fcr", "", "",
  162.   "eflag", "csd", "ssd", "cflg", "fsr", "fir", "fdr""",
  163.   "ccv", "", "", "", "unat", "", "", "",
  164.   "fpsr", "", "", "", "itc",
  165.   "", "", "", "", "", "", "", "", "", "",
  166.   "", "", "", "", "", "", "", "", "",
  167.   "pfs", "lc", "ec",
  168.   "", "", "", "", "", "", "", "", "", "",
  169.   "", "", "", "", "", "", "", "", "", "",
  170.   "", "", "", "", "", "", "", "", "", "",
  171.   "", "", "", "", "", "", "", "", "", "",
  172.   "", "", "", "", "", "", "", "", "", "",
  173.   "", "", "", "", "", "", "", "", "", "",
  174.   "",
  175.   "nat0""nat1""nat2""nat3""nat4""nat5""nat6""nat7",
  176.   "nat8""nat9""nat10", "nat11", "nat12", "nat13", "nat14", "nat15",
  177.   "nat16", "nat17", "nat18", "nat19", "nat20", "nat21", "nat22", "nat23",
  178.   "nat24", "nat25", "nat26", "nat27", "nat28", "nat29", "nat30", "nat31",
  179.   "nat32", "nat33", "nat34", "nat35", "nat36", "nat37", "nat38", "nat39",
  180.   "nat40", "nat41", "nat42", "nat43", "nat44", "nat45", "nat46", "nat47",
  181.   "nat48", "nat49", "nat50", "nat51", "nat52", "nat53", "nat54", "nat55",
  182.   "nat56", "nat57", "nat58", "nat59", "nat60", "nat61", "nat62", "nat63",
  183.   "nat64", "nat65", "nat66", "nat67", "nat68", "nat69", "nat70", "nat71",
  184.   "nat72", "nat73", "nat74", "nat75", "nat76", "nat77", "nat78", "nat79",
  185.   "nat80", "nat81", "nat82", "nat83", "nat84", "nat85", "nat86", "nat87",
  186.   "nat88", "nat89", "nat90", "nat91", "nat92", "nat93", "nat94", "nat95",
  187.   "nat96", "nat97", "nat98", "nat99", "nat100","nat101","nat102","nat103",
  188.   "nat104","nat105","nat106","nat107","nat108","nat109","nat110","nat111",
  189.   "nat112","nat113","nat114","nat115","nat116","nat117","nat118","nat119",
  190.   "nat120","nat121","nat122","nat123","nat124","nat125","nat126","nat127",

  191.   "bof",

  192.   "r32""r33""r34""r35""r36""r37""r38""r39",
  193.   "r40""r41""r42""r43""r44""r45""r46""r47",
  194.   "r48""r49""r50""r51""r52""r53""r54""r55",
  195.   "r56""r57""r58""r59""r60""r61""r62""r63",
  196.   "r64""r65""r66""r67""r68""r69""r70""r71",
  197.   "r72""r73""r74""r75""r76""r77""r78""r79",
  198.   "r80""r81""r82""r83""r84""r85""r86""r87",
  199.   "r88""r89""r90""r91""r92""r93""r94""r95",
  200.   "r96""r97""r98""r99""r100", "r101", "r102", "r103",
  201.   "r104", "r105", "r106", "r107", "r108", "r109", "r110", "r111",
  202.   "r112", "r113", "r114", "r115", "r116", "r117", "r118", "r119",
  203.   "r120", "r121", "r122", "r123", "r124", "r125", "r126", "r127",

  204.   "p0",   "p1",   "p2",   "p3",   "p4",   "p5",   "p6",   "p7",
  205.   "p8",   "p9",   "p10""p11""p12""p13""p14""p15",
  206.   "p16""p17""p18""p19""p20""p21""p22""p23",
  207.   "p24""p25""p26""p27""p28""p29""p30""p31",
  208.   "p32""p33""p34""p35""p36""p37""p38""p39",
  209.   "p40""p41""p42""p43""p44""p45""p46""p47",
  210.   "p48""p49""p50""p51""p52""p53""p54""p55",
  211.   "p56""p57""p58""p59""p60""p61""p62""p63",
  212. };

  213. struct ia64_frame_cache
  214. {
  215.   CORE_ADDR base;       /* frame pointer base for frame */
  216.   CORE_ADDR pc;                /* function start pc for frame */
  217.   CORE_ADDR saved_sp;        /* stack pointer for frame */
  218.   CORE_ADDR bsp;        /* points at r32 for the current frame */
  219.   CORE_ADDR cfm;        /* cfm value for current frame */
  220.   CORE_ADDR prev_cfm;   /* cfm value for previous frame */
  221.   int   frameless;
  222.   int   sof;                /* Size of frame  (decoded from cfm value).  */
  223.   int        sol;                /* Size of locals (decoded from cfm value).  */
  224.   int        sor;                /* Number of rotating registers (decoded from
  225.                            cfm value).  */
  226.   CORE_ADDR after_prologue;
  227.   /* Address of first instruction after the last
  228.      prologue instruction;  Note that there may
  229.      be instructions from the function's body
  230.      intermingled with the prologue.  */
  231.   int mem_stack_frame_size;
  232.   /* Size of the memory stack frame (may be zero),
  233.      or -1 if it has not been determined yet.  */
  234.   int        fp_reg;                /* Register number (if any) used a frame pointer
  235.                            for this frame.  0 if no register is being used
  236.                            as the frame pointer.  */

  237.   /* Saved registers.  */
  238.   CORE_ADDR saved_regs[NUM_IA64_RAW_REGS];

  239. };

  240. static int
  241. floatformat_valid (const struct floatformat *fmt, const void *from)
  242. {
  243.   return 1;
  244. }

  245. static const struct floatformat floatformat_ia64_ext_little =
  246. {
  247.   floatformat_little, 82, 0, 1, 17, 65535, 0x1ffff, 18, 64,
  248.   floatformat_intbit_yes, "floatformat_ia64_ext_little", floatformat_valid, NULL
  249. };

  250. static const struct floatformat floatformat_ia64_ext_big =
  251. {
  252.   floatformat_big, 82, 46, 47, 17, 65535, 0x1ffff, 64, 64,
  253.   floatformat_intbit_yes, "floatformat_ia64_ext_big", floatformat_valid
  254. };

  255. static const struct floatformat *floatformats_ia64_ext[2] =
  256. {
  257.   &floatformat_ia64_ext_big,
  258.   &floatformat_ia64_ext_little
  259. };

  260. static struct type *
  261. ia64_ext_type (struct gdbarch *gdbarch)
  262. {
  263.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  264.   if (!tdep->ia64_ext_type)
  265.     tdep->ia64_ext_type
  266.       = arch_float_type (gdbarch, 128, "builtin_type_ia64_ext",
  267.                          floatformats_ia64_ext);

  268.   return tdep->ia64_ext_type;
  269. }

  270. static int
  271. ia64_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  272.                           struct reggroup *group)
  273. {
  274.   int vector_p;
  275.   int float_p;
  276.   int raw_p;
  277.   if (group == all_reggroup)
  278.     return 1;
  279.   vector_p = TYPE_VECTOR (register_type (gdbarch, regnum));
  280.   float_p = TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT;
  281.   raw_p = regnum < NUM_IA64_RAW_REGS;
  282.   if (group == float_reggroup)
  283.     return float_p;
  284.   if (group == vector_reggroup)
  285.     return vector_p;
  286.   if (group == general_reggroup)
  287.     return (!vector_p && !float_p);
  288.   if (group == save_reggroup || group == restore_reggroup)
  289.     return raw_p;
  290.   return 0;
  291. }

  292. static const char *
  293. ia64_register_name (struct gdbarch *gdbarch, int reg)
  294. {
  295.   return ia64_register_names[reg];
  296. }

  297. struct type *
  298. ia64_register_type (struct gdbarch *arch, int reg)
  299. {
  300.   if (reg >= IA64_FR0_REGNUM && reg <= IA64_FR127_REGNUM)
  301.     return ia64_ext_type (arch);
  302.   else
  303.     return builtin_type (arch)->builtin_long;
  304. }

  305. static int
  306. ia64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  307. {
  308.   if (reg >= IA64_GR32_REGNUM && reg <= IA64_GR127_REGNUM)
  309.     return V32_REGNUM + (reg - IA64_GR32_REGNUM);
  310.   return reg;
  311. }


  312. /* Extract ``len'' bits from an instruction bundle starting at
  313.    bit ``from''.  */

  314. static long long
  315. extract_bit_field (const gdb_byte *bundle, int from, int len)
  316. {
  317.   long long result = 0LL;
  318.   int to = from + len;
  319.   int from_byte = from / 8;
  320.   int to_byte = to / 8;
  321.   unsigned char *b = (unsigned char *) bundle;
  322.   unsigned char c;
  323.   int lshift;
  324.   int i;

  325.   c = b[from_byte];
  326.   if (from_byte == to_byte)
  327.     c = ((unsigned char) (c << (8 - to % 8))) >> (8 - to % 8);
  328.   result = c >> (from % 8);
  329.   lshift = 8 - (from % 8);

  330.   for (i = from_byte+1; i < to_byte; i++)
  331.     {
  332.       result |= ((long long) b[i]) << lshift;
  333.       lshift += 8;
  334.     }

  335.   if (from_byte < to_byte && (to % 8 != 0))
  336.     {
  337.       c = b[to_byte];
  338.       c = ((unsigned char) (c << (8 - to % 8))) >> (8 - to % 8);
  339.       result |= ((long long) c) << lshift;
  340.     }

  341.   return result;
  342. }

  343. /* Replace the specified bits in an instruction bundle.  */

  344. static void
  345. replace_bit_field (gdb_byte *bundle, long long val, int from, int len)
  346. {
  347.   int to = from + len;
  348.   int from_byte = from / 8;
  349.   int to_byte = to / 8;
  350.   unsigned char *b = (unsigned char *) bundle;
  351.   unsigned char c;

  352.   if (from_byte == to_byte)
  353.     {
  354.       unsigned char left, right;
  355.       c = b[from_byte];
  356.       left = (c >> (to % 8)) << (to % 8);
  357.       right = ((unsigned char) (c << (8 - from % 8))) >> (8 - from % 8);
  358.       c = (unsigned char) (val & 0xff);
  359.       c = (unsigned char) (c << (from % 8 + 8 - to % 8)) >> (8 - to % 8);
  360.       c |= right | left;
  361.       b[from_byte] = c;
  362.     }
  363.   else
  364.     {
  365.       int i;
  366.       c = b[from_byte];
  367.       c = ((unsigned char) (c << (8 - from % 8))) >> (8 - from % 8);
  368.       c = c | (val << (from % 8));
  369.       b[from_byte] = c;
  370.       val >>= 8 - from % 8;

  371.       for (i = from_byte+1; i < to_byte; i++)
  372.         {
  373.           c = val & 0xff;
  374.           val >>= 8;
  375.           b[i] = c;
  376.         }

  377.       if (to % 8 != 0)
  378.         {
  379.           unsigned char cv = (unsigned char) val;
  380.           c = b[to_byte];
  381.           c = c >> (to % 8) << (to % 8);
  382.           c |= ((unsigned char) (cv << (8 - to % 8))) >> (8 - to % 8);
  383.           b[to_byte] = c;
  384.         }
  385.     }
  386. }

  387. /* Return the contents of slot N (for N = 0, 1, or 2) in
  388.    and instruction bundle.  */

  389. static long long
  390. slotN_contents (gdb_byte *bundle, int slotnum)
  391. {
  392.   return extract_bit_field (bundle, 5+41*slotnum, 41);
  393. }

  394. /* Store an instruction in an instruction bundle.  */

  395. static void
  396. replace_slotN_contents (gdb_byte *bundle, long long instr, int slotnum)
  397. {
  398.   replace_bit_field (bundle, instr, 5+41*slotnum, 41);
  399. }

  400. static const enum instruction_type template_encoding_table[32][3] =
  401. {
  402.   { M, I, I },                                /* 00 */
  403.   { M, I, I },                                /* 01 */
  404.   { M, I, I },                                /* 02 */
  405.   { M, I, I },                                /* 03 */
  406.   { M, L, X },                                /* 04 */
  407.   { M, L, X },                                /* 05 */
  408.   { undefined, undefined, undefined },  /* 06 */
  409.   { undefined, undefined, undefined },  /* 07 */
  410.   { M, M, I },                                /* 08 */
  411.   { M, M, I },                                /* 09 */
  412.   { M, M, I },                                /* 0A */
  413.   { M, M, I },                                /* 0B */
  414.   { M, F, I },                                /* 0C */
  415.   { M, F, I },                                /* 0D */
  416.   { M, M, F },                                /* 0E */
  417.   { M, M, F },                                /* 0F */
  418.   { M, I, B },                                /* 10 */
  419.   { M, I, B },                                /* 11 */
  420.   { M, B, B },                                /* 12 */
  421.   { M, B, B },                                /* 13 */
  422.   { undefined, undefined, undefined },  /* 14 */
  423.   { undefined, undefined, undefined },  /* 15 */
  424.   { B, B, B },                                /* 16 */
  425.   { B, B, B },                                /* 17 */
  426.   { M, M, B },                                /* 18 */
  427.   { M, M, B },                                /* 19 */
  428.   { undefined, undefined, undefined },  /* 1A */
  429.   { undefined, undefined, undefined },  /* 1B */
  430.   { M, F, B },                                /* 1C */
  431.   { M, F, B },                                /* 1D */
  432.   { undefined, undefined, undefined },  /* 1E */
  433.   { undefined, undefined, undefined },  /* 1F */
  434. };

  435. /* Fetch and (partially) decode an instruction at ADDR and return the
  436.    address of the next instruction to fetch.  */

  437. static CORE_ADDR
  438. fetch_instruction (CORE_ADDR addr, instruction_type *it, long long *instr)
  439. {
  440.   gdb_byte bundle[BUNDLE_LEN];
  441.   int slotnum = (int) (addr & 0x0f) / SLOT_MULTIPLIER;
  442.   long long template;
  443.   int val;

  444.   /* Warn about slot numbers greater than 2.  We used to generate
  445.      an error here on the assumption that the user entered an invalid
  446.      address.  But, sometimes GDB itself requests an invalid address.
  447.      This can (easily) happen when execution stops in a function for
  448.      which there are no symbols.  The prologue scanner will attempt to
  449.      find the beginning of the function - if the nearest symbol
  450.      happens to not be aligned on a bundle boundary (16 bytes), the
  451.      resulting starting address will cause GDB to think that the slot
  452.      number is too large.

  453.      So we warn about it and set the slot number to zero.  It is
  454.      not necessarily a fatal condition, particularly if debugging
  455.      at the assembly language level.  */
  456.   if (slotnum > 2)
  457.     {
  458.       warning (_("Can't fetch instructions for slot numbers greater than 2.\n"
  459.                "Using slot 0 instead"));
  460.       slotnum = 0;
  461.     }

  462.   addr &= ~0x0f;

  463.   val = target_read_memory (addr, bundle, BUNDLE_LEN);

  464.   if (val != 0)
  465.     return 0;

  466.   *instr = slotN_contents (bundle, slotnum);
  467.   template = extract_bit_field (bundle, 0, 5);
  468.   *it = template_encoding_table[(int)template][slotnum];

  469.   if (slotnum == 2 || (slotnum == 1 && *it == L))
  470.     addr += 16;
  471.   else
  472.     addr += (slotnum + 1) * SLOT_MULTIPLIER;

  473.   return addr;
  474. }

  475. /* There are 5 different break instructions (break.i, break.b,
  476.    break.m, break.f, and break.x), but they all have the same
  477.    encoding.  (The five bit template in the low five bits of the
  478.    instruction bundle distinguishes one from another.)

  479.    The runtime architecture manual specifies that break instructions
  480.    used for debugging purposes must have the upper two bits of the 21
  481.    bit immediate set to a 0 and a 1 respectively.  A breakpoint
  482.    instruction encodes the most significant bit of its 21 bit
  483.    immediate at bit 36 of the 41 bit instruction.  The penultimate msb
  484.    is at bit 25 which leads to the pattern below.

  485.    Originally, I had this set up to do, e.g, a "break.i 0x80000"  But
  486.    it turns out that 0x80000 was used as the syscall break in the early
  487.    simulators.  So I changed the pattern slightly to do "break.i 0x080001"
  488.    instead.  But that didn't work either (I later found out that this
  489.    pattern was used by the simulator that I was using.)  So I ended up
  490.    using the pattern seen below.

  491.    SHADOW_CONTENTS has byte-based addressing (PLACED_ADDRESS and SHADOW_LEN)
  492.    while we need bit-based addressing as the instructions length is 41 bits and
  493.    we must not modify/corrupt the adjacent slots in the same bundle.
  494.    Fortunately we may store larger memory incl. the adjacent bits with the
  495.    original memory content (not the possibly already stored breakpoints there).
  496.    We need to be careful in ia64_memory_remove_breakpoint to always restore
  497.    only the specific bits of this instruction ignoring any adjacent stored
  498.    bits.

  499.    We use the original addressing with the low nibble in the range <0..2> which
  500.    gets incorrectly interpreted by generic non-ia64 breakpoint_restore_shadows
  501.    as the direct byte offset of SHADOW_CONTENTS.  We store whole BUNDLE_LEN
  502.    bytes just without these two possibly skipped bytes to not to exceed to the
  503.    next bundle.

  504.    If we would like to store the whole bundle to SHADOW_CONTENTS we would have
  505.    to store already the base address (`address & ~0x0f') into PLACED_ADDRESS.
  506.    In such case there is no other place where to store
  507.    SLOTNUM (`adress & 0x0f', value in the range <0..2>).  We need to know
  508.    SLOTNUM in ia64_memory_remove_breakpoint.

  509.    There is one special case where we need to be extra careful:
  510.    L-X instructions, which are instructions that occupy 2 slots
  511.    (The L part is always in slot 1, and the X part is always in
  512.    slot 2).  We must refuse to insert breakpoints for an address
  513.    that points at slot 2 of a bundle where an L-X instruction is
  514.    present, since there is logically no instruction at that address.
  515.    However, to make things more interesting, the opcode of L-X
  516.    instructions is located in slot 2.  This means that, to insert
  517.    a breakpoint at an address that points to slot 1, we actually
  518.    need to write the breakpoint in slot 2!  Slot 1 is actually
  519.    the extended operand, so writing the breakpoint there would not
  520.    have the desired effect.  Another side-effect of this issue
  521.    is that we need to make sure that the shadow contents buffer
  522.    does save byte 15 of our instruction bundle (this is the tail
  523.    end of slot 2, which wouldn't be saved if we were to insert
  524.    the breakpoint in slot 1).

  525.    ia64 16-byte bundle layout:
  526.    | 5 bits | slot 0 with 41 bits | slot 1 with 41 bits | slot 2 with 41 bits |

  527.    The current addressing used by the code below:
  528.    original PC   placed_address   placed_size             required    covered
  529.                                   == bp_tgt->shadow_len   reqd \subset covered
  530.    0xABCDE0      0xABCDE0         0x10                    <0x0...0x5> <0x0..0xF>
  531.    0xABCDE1      0xABCDE1         0xF                     <0x5...0xA> <0x1..0xF>
  532.    0xABCDE2      0xABCDE2         0xE                     <0xA...0xF> <0x2..0xF>

  533.    L-X instructions are treated a little specially, as explained above:
  534.    0xABCDE1      0xABCDE1         0xF                     <0xA...0xF> <0x1..0xF>

  535.    `objdump -d' and some other tools show a bit unjustified offsets:
  536.    original PC   byte where starts the instruction   objdump offset
  537.    0xABCDE0      0xABCDE0                            0xABCDE0
  538.    0xABCDE1      0xABCDE5                            0xABCDE6
  539.    0xABCDE2      0xABCDEA                            0xABCDEC
  540.    */

  541. #define IA64_BREAKPOINT 0x00003333300LL

  542. static int
  543. ia64_memory_insert_breakpoint (struct gdbarch *gdbarch,
  544.                                struct bp_target_info *bp_tgt)
  545. {
  546.   CORE_ADDR addr = bp_tgt->placed_address = bp_tgt->reqstd_address;
  547.   gdb_byte bundle[BUNDLE_LEN];
  548.   int slotnum = (int) (addr & 0x0f) / SLOT_MULTIPLIER, shadow_slotnum;
  549.   long long instr_breakpoint;
  550.   int val;
  551.   int template;
  552.   struct cleanup *cleanup;

  553.   if (slotnum > 2)
  554.     error (_("Can't insert breakpoint for slot numbers greater than 2."));

  555.   addr &= ~0x0f;

  556.   /* Enable the automatic memory restoration from breakpoints while
  557.      we read our instruction bundle for the purpose of SHADOW_CONTENTS.
  558.      Otherwise, we could possibly store into the shadow parts of the adjacent
  559.      placed breakpoints.  It is due to our SHADOW_CONTENTS overlapping the real
  560.      breakpoint instruction bits region.  */
  561.   cleanup = make_show_memory_breakpoints_cleanup (0);
  562.   val = target_read_memory (addr, bundle, BUNDLE_LEN);
  563.   if (val != 0)
  564.     {
  565.       do_cleanups (cleanup);
  566.       return val;
  567.     }

  568.   /* SHADOW_SLOTNUM saves the original slot number as expected by the caller
  569.      for addressing the SHADOW_CONTENTS placement.  */
  570.   shadow_slotnum = slotnum;

  571.   /* Always cover the last byte of the bundle in case we are inserting
  572.      a breakpoint on an L-X instruction.  */
  573.   bp_tgt->shadow_len = BUNDLE_LEN - shadow_slotnum;

  574.   template = extract_bit_field (bundle, 0, 5);
  575.   if (template_encoding_table[template][slotnum] == X)
  576.     {
  577.       /* X unit types can only be used in slot 2, and are actually
  578.          part of a 2-slot L-X instruction.  We cannot break at this
  579.          address, as this is the second half of an instruction that
  580.          lives in slot 1 of that bundle.  */
  581.       gdb_assert (slotnum == 2);
  582.       error (_("Can't insert breakpoint for non-existing slot X"));
  583.     }
  584.   if (template_encoding_table[template][slotnum] == L)
  585.     {
  586.       /* L unit types can only be used in slot 1.  But the associated
  587.          opcode for that instruction is in slot 2, so bump the slot number
  588.          accordingly.  */
  589.       gdb_assert (slotnum == 1);
  590.       slotnum = 2;
  591.     }

  592.   /* Store the whole bundle, except for the initial skipped bytes by the slot
  593.      number interpreted as bytes offset in PLACED_ADDRESS.  */
  594.   memcpy (bp_tgt->shadow_contents, bundle + shadow_slotnum,
  595.           bp_tgt->shadow_len);

  596.   /* Re-read the same bundle as above except that, this time, read it in order
  597.      to compute the new bundle inside which we will be inserting the
  598.      breakpoint.  Therefore, disable the automatic memory restoration from
  599.      breakpoints while we read our instruction bundle.  Otherwise, the general
  600.      restoration mechanism kicks in and we would possibly remove parts of the
  601.      adjacent placed breakpoints.  It is due to our SHADOW_CONTENTS overlapping
  602.      the real breakpoint instruction bits region.  */
  603.   make_show_memory_breakpoints_cleanup (1);
  604.   val = target_read_memory (addr, bundle, BUNDLE_LEN);
  605.   if (val != 0)
  606.     {
  607.       do_cleanups (cleanup);
  608.       return val;
  609.     }

  610.   /* Breakpoints already present in the code will get deteacted and not get
  611.      reinserted by bp_loc_is_permanent.  Multiple breakpoints at the same
  612.      location cannot induce the internal error as they are optimized into
  613.      a single instance by update_global_location_list.  */
  614.   instr_breakpoint = slotN_contents (bundle, slotnum);
  615.   if (instr_breakpoint == IA64_BREAKPOINT)
  616.     internal_error (__FILE__, __LINE__,
  617.                     _("Address %s already contains a breakpoint."),
  618.                     paddress (gdbarch, bp_tgt->placed_address));
  619.   replace_slotN_contents (bundle, IA64_BREAKPOINT, slotnum);

  620.   bp_tgt->placed_size = bp_tgt->shadow_len;

  621.   val = target_write_memory (addr + shadow_slotnum, bundle + shadow_slotnum,
  622.                              bp_tgt->shadow_len);

  623.   do_cleanups (cleanup);
  624.   return val;
  625. }

  626. static int
  627. ia64_memory_remove_breakpoint (struct gdbarch *gdbarch,
  628.                                struct bp_target_info *bp_tgt)
  629. {
  630.   CORE_ADDR addr = bp_tgt->placed_address;
  631.   gdb_byte bundle_mem[BUNDLE_LEN], bundle_saved[BUNDLE_LEN];
  632.   int slotnum = (addr & 0x0f) / SLOT_MULTIPLIER, shadow_slotnum;
  633.   long long instr_breakpoint, instr_saved;
  634.   int val;
  635.   int template;
  636.   struct cleanup *cleanup;

  637.   addr &= ~0x0f;

  638.   /* Disable the automatic memory restoration from breakpoints while
  639.      we read our instruction bundle.  Otherwise, the general restoration
  640.      mechanism kicks in and we would possibly remove parts of the adjacent
  641.      placed breakpoints.  It is due to our SHADOW_CONTENTS overlapping the real
  642.      breakpoint instruction bits region.  */
  643.   cleanup = make_show_memory_breakpoints_cleanup (1);
  644.   val = target_read_memory (addr, bundle_mem, BUNDLE_LEN);
  645.   if (val != 0)
  646.     {
  647.       do_cleanups (cleanup);
  648.       return val;
  649.     }

  650.   /* SHADOW_SLOTNUM saves the original slot number as expected by the caller
  651.      for addressing the SHADOW_CONTENTS placement.  */
  652.   shadow_slotnum = slotnum;

  653.   template = extract_bit_field (bundle_mem, 0, 5);
  654.   if (template_encoding_table[template][slotnum] == X)
  655.     {
  656.       /* X unit types can only be used in slot 2, and are actually
  657.          part of a 2-slot L-X instruction.  We refuse to insert
  658.          breakpoints at this address, so there should be no reason
  659.          for us attempting to remove one there, except if the program's
  660.          code somehow got modified in memory.  */
  661.       gdb_assert (slotnum == 2);
  662.       warning (_("Cannot remove breakpoint at address %s from non-existing "
  663.                  "X-type slot, memory has changed underneath"),
  664.                paddress (gdbarch, bp_tgt->placed_address));
  665.       do_cleanups (cleanup);
  666.       return -1;
  667.     }
  668.   if (template_encoding_table[template][slotnum] == L)
  669.     {
  670.       /* L unit types can only be used in slot 1.  But the breakpoint
  671.          was actually saved using slot 2, so update the slot number
  672.          accordingly.  */
  673.       gdb_assert (slotnum == 1);
  674.       slotnum = 2;
  675.     }

  676.   gdb_assert (bp_tgt->placed_size == BUNDLE_LEN - shadow_slotnum);
  677.   gdb_assert (bp_tgt->placed_size == bp_tgt->shadow_len);

  678.   instr_breakpoint = slotN_contents (bundle_mem, slotnum);
  679.   if (instr_breakpoint != IA64_BREAKPOINT)
  680.     {
  681.       warning (_("Cannot remove breakpoint at address %s, "
  682.                  "no break instruction at such address."),
  683.                paddress (gdbarch, bp_tgt->placed_address));
  684.       do_cleanups (cleanup);
  685.       return -1;
  686.     }

  687.   /* Extract the original saved instruction from SLOTNUM normalizing its
  688.      bit-shift for INSTR_SAVED.  */
  689.   memcpy (bundle_saved, bundle_mem, BUNDLE_LEN);
  690.   memcpy (bundle_saved + shadow_slotnum, bp_tgt->shadow_contents,
  691.           bp_tgt->shadow_len);
  692.   instr_saved = slotN_contents (bundle_saved, slotnum);

  693.   /* In BUNDLE_MEM, be careful to modify only the bits belonging to SLOTNUM
  694.      and not any of the other ones that are stored in SHADOW_CONTENTS.  */
  695.   replace_slotN_contents (bundle_mem, instr_saved, slotnum);
  696.   val = target_write_raw_memory (addr, bundle_mem, BUNDLE_LEN);

  697.   do_cleanups (cleanup);
  698.   return val;
  699. }

  700. /* As gdbarch_breakpoint_from_pc ranges have byte granularity and ia64
  701.    instruction slots ranges are bit-granular (41 bits) we have to provide an
  702.    extended range as described for ia64_memory_insert_breakpoint.  We also take
  703.    care of preserving the `break' instruction 21-bit (or 62-bit) parameter to
  704.    make a match for permanent breakpoints.  */

  705. static const gdb_byte *
  706. ia64_breakpoint_from_pc (struct gdbarch *gdbarch,
  707.                          CORE_ADDR *pcptr, int *lenptr)
  708. {
  709.   CORE_ADDR addr = *pcptr;
  710.   static gdb_byte bundle[BUNDLE_LEN];
  711.   int slotnum = (int) (*pcptr & 0x0f) / SLOT_MULTIPLIER, shadow_slotnum;
  712.   long long instr_fetched;
  713.   int val;
  714.   int template;
  715.   struct cleanup *cleanup;

  716.   if (slotnum > 2)
  717.     error (_("Can't insert breakpoint for slot numbers greater than 2."));

  718.   addr &= ~0x0f;

  719.   /* Enable the automatic memory restoration from breakpoints while
  720.      we read our instruction bundle to match bp_loc_is_permanent.  */
  721.   cleanup = make_show_memory_breakpoints_cleanup (0);
  722.   val = target_read_memory (addr, bundle, BUNDLE_LEN);
  723.   do_cleanups (cleanup);

  724.   /* The memory might be unreachable.  This can happen, for instance,
  725.      when the user inserts a breakpoint at an invalid address.  */
  726.   if (val != 0)
  727.     return NULL;

  728.   /* SHADOW_SLOTNUM saves the original slot number as expected by the caller
  729.      for addressing the SHADOW_CONTENTS placement.  */
  730.   shadow_slotnum = slotnum;

  731.   /* Cover always the last byte of the bundle for the L-X slot case.  */
  732.   *lenptr = BUNDLE_LEN - shadow_slotnum;

  733.   /* Check for L type instruction in slot 1, if present then bump up the slot
  734.      number to the slot 2.  */
  735.   template = extract_bit_field (bundle, 0, 5);
  736.   if (template_encoding_table[template][slotnum] == X)
  737.     {
  738.       gdb_assert (slotnum == 2);
  739.       error (_("Can't insert breakpoint for non-existing slot X"));
  740.     }
  741.   if (template_encoding_table[template][slotnum] == L)
  742.     {
  743.       gdb_assert (slotnum == 1);
  744.       slotnum = 2;
  745.     }

  746.   /* A break instruction has its all its opcode bits cleared except for
  747.      the parameter value.  For L+X slot pair we are at the X slot (slot 2) so
  748.      we should not touch the L slot - the upper 41 bits of the parameter.  */
  749.   instr_fetched = slotN_contents (bundle, slotnum);
  750.   instr_fetched &= 0x1003ffffc0LL;
  751.   replace_slotN_contents (bundle, instr_fetched, slotnum);

  752.   return bundle + shadow_slotnum;
  753. }

  754. static CORE_ADDR
  755. ia64_read_pc (struct regcache *regcache)
  756. {
  757.   ULONGEST psr_value, pc_value;
  758.   int slot_num;

  759.   regcache_cooked_read_unsigned (regcache, IA64_PSR_REGNUM, &psr_value);
  760.   regcache_cooked_read_unsigned (regcache, IA64_IP_REGNUM, &pc_value);
  761.   slot_num = (psr_value >> 41) & 3;

  762.   return pc_value | (slot_num * SLOT_MULTIPLIER);
  763. }

  764. void
  765. ia64_write_pc (struct regcache *regcache, CORE_ADDR new_pc)
  766. {
  767.   int slot_num = (int) (new_pc & 0xf) / SLOT_MULTIPLIER;
  768.   ULONGEST psr_value;

  769.   regcache_cooked_read_unsigned (regcache, IA64_PSR_REGNUM, &psr_value);
  770.   psr_value &= ~(3LL << 41);
  771.   psr_value |= (ULONGEST)(slot_num & 0x3) << 41;

  772.   new_pc &= ~0xfLL;

  773.   regcache_cooked_write_unsigned (regcache, IA64_PSR_REGNUM, psr_value);
  774.   regcache_cooked_write_unsigned (regcache, IA64_IP_REGNUM, new_pc);
  775. }

  776. #define IS_NaT_COLLECTION_ADDR(addr) ((((addr) >> 3) & 0x3f) == 0x3f)

  777. /* Returns the address of the slot that's NSLOTS slots away from
  778.    the address ADDR.  NSLOTS may be positive or negative.  */
  779. static CORE_ADDR
  780. rse_address_add(CORE_ADDR addr, int nslots)
  781. {
  782.   CORE_ADDR new_addr;
  783.   int mandatory_nat_slots = nslots / 63;
  784.   int direction = nslots < 0 ? -1 : 1;

  785.   new_addr = addr + 8 * (nslots + mandatory_nat_slots);

  786.   if ((new_addr >> 9)  != ((addr + 8 * 64 * mandatory_nat_slots) >> 9))
  787.     new_addr += 8 * direction;

  788.   if (IS_NaT_COLLECTION_ADDR(new_addr))
  789.     new_addr += 8 * direction;

  790.   return new_addr;
  791. }

  792. static enum register_status
  793. ia64_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
  794.                            int regnum, gdb_byte *buf)
  795. {
  796.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  797.   enum register_status status;

  798.   if (regnum >= V32_REGNUM && regnum <= V127_REGNUM)
  799.     {
  800. #ifdef HAVE_LIBUNWIND_IA64_H
  801.       /* First try and use the libunwind special reg accessor,
  802.          otherwise fallback to standard logic.  */
  803.       if (!libunwind_is_initialized ()
  804.           || libunwind_get_reg_special (gdbarch, regcache, regnum, buf) != 0)
  805. #endif
  806.         {
  807.           /* The fallback position is to assume that r32-r127 are
  808.              found sequentially in memory starting at $bof.  This
  809.              isn't always true, but without libunwind, this is the
  810.              best we can do.  */
  811.           enum register_status status;
  812.           ULONGEST cfm;
  813.           ULONGEST bsp;
  814.           CORE_ADDR reg;

  815.           status = regcache_cooked_read_unsigned (regcache,
  816.                                                   IA64_BSP_REGNUM, &bsp);
  817.           if (status != REG_VALID)
  818.             return status;

  819.           status = regcache_cooked_read_unsigned (regcache,
  820.                                                   IA64_CFM_REGNUM, &cfm);
  821.           if (status != REG_VALID)
  822.             return status;

  823.           /* The bsp points at the end of the register frame so we
  824.              subtract the size of frame from it to get start of
  825.              register frame.  */
  826.           bsp = rse_address_add (bsp, -(cfm & 0x7f));

  827.           if ((cfm & 0x7f) > regnum - V32_REGNUM)
  828.             {
  829.               ULONGEST reg_addr = rse_address_add (bsp, (regnum - V32_REGNUM));
  830.               reg = read_memory_integer ((CORE_ADDR)reg_addr, 8, byte_order);
  831.               store_unsigned_integer (buf, register_size (gdbarch, regnum),
  832.                                       byte_order, reg);
  833.             }
  834.           else
  835.             store_unsigned_integer (buf, register_size (gdbarch, regnum),
  836.                                     byte_order, 0);
  837.         }
  838.     }
  839.   else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT31_REGNUM)
  840.     {
  841.       ULONGEST unatN_val;
  842.       ULONGEST unat;
  843.       status = regcache_cooked_read_unsigned (regcache, IA64_UNAT_REGNUM, &unat);
  844.       if (status != REG_VALID)
  845.         return status;
  846.       unatN_val = (unat & (1LL << (regnum - IA64_NAT0_REGNUM))) != 0;
  847.       store_unsigned_integer (buf, register_size (gdbarch, regnum),
  848.                               byte_order, unatN_val);
  849.     }
  850.   else if (IA64_NAT32_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
  851.     {
  852.       ULONGEST natN_val = 0;
  853.       ULONGEST bsp;
  854.       ULONGEST cfm;
  855.       CORE_ADDR gr_addr = 0;
  856.       status = regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
  857.       if (status != REG_VALID)
  858.         return status;
  859.       status = regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
  860.       if (status != REG_VALID)
  861.         return status;

  862.       /* The bsp points at the end of the register frame so we
  863.          subtract the size of frame from it to get start of register frame.  */
  864.       bsp = rse_address_add (bsp, -(cfm & 0x7f));

  865.       if ((cfm & 0x7f) > regnum - V32_REGNUM)
  866.         gr_addr = rse_address_add (bsp, (regnum - V32_REGNUM));

  867.       if (gr_addr != 0)
  868.         {
  869.           /* Compute address of nat collection bits.  */
  870.           CORE_ADDR nat_addr = gr_addr | 0x1f8;
  871.           CORE_ADDR nat_collection;
  872.           int nat_bit;
  873.           /* If our nat collection address is bigger than bsp, we have to get
  874.              the nat collection from rnat.  Otherwise, we fetch the nat
  875.              collection from the computed address.  */
  876.           if (nat_addr >= bsp)
  877.             regcache_cooked_read_unsigned (regcache, IA64_RNAT_REGNUM,
  878.                                            &nat_collection);
  879.           else
  880.             nat_collection = read_memory_integer (nat_addr, 8, byte_order);
  881.           nat_bit = (gr_addr >> 3) & 0x3f;
  882.           natN_val = (nat_collection >> nat_bit) & 1;
  883.         }

  884.       store_unsigned_integer (buf, register_size (gdbarch, regnum),
  885.                               byte_order, natN_val);
  886.     }
  887.   else if (regnum == VBOF_REGNUM)
  888.     {
  889.       /* A virtual register frame start is provided for user convenience.
  890.          It can be calculated as the bsp - sof (sizeof frame).  */
  891.       ULONGEST bsp, vbsp;
  892.       ULONGEST cfm;
  893.       status = regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
  894.       if (status != REG_VALID)
  895.         return status;
  896.       status = regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
  897.       if (status != REG_VALID)
  898.         return status;

  899.       /* The bsp points at the end of the register frame so we
  900.          subtract the size of frame from it to get beginning of frame.  */
  901.       vbsp = rse_address_add (bsp, -(cfm & 0x7f));
  902.       store_unsigned_integer (buf, register_size (gdbarch, regnum),
  903.                               byte_order, vbsp);
  904.     }
  905.   else if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
  906.     {
  907.       ULONGEST pr;
  908.       ULONGEST cfm;
  909.       ULONGEST prN_val;
  910.       status = regcache_cooked_read_unsigned (regcache, IA64_PR_REGNUM, &pr);
  911.       if (status != REG_VALID)
  912.         return status;
  913.       status = regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
  914.       if (status != REG_VALID)
  915.         return status;

  916.       if (VP16_REGNUM <= regnum && regnum <= VP63_REGNUM)
  917.         {
  918.           /* Fetch predicate register rename base from current frame
  919.              marker for this frame.  */
  920.           int rrb_pr = (cfm >> 32) & 0x3f;

  921.           /* Adjust the register number to account for register rotation.  */
  922.           regnum = VP16_REGNUM
  923.                  + ((regnum - VP16_REGNUM) + rrb_pr) % 48;
  924.         }
  925.       prN_val = (pr & (1LL << (regnum - VP0_REGNUM))) != 0;
  926.       store_unsigned_integer (buf, register_size (gdbarch, regnum),
  927.                               byte_order, prN_val);
  928.     }
  929.   else
  930.     memset (buf, 0, register_size (gdbarch, regnum));

  931.   return REG_VALID;
  932. }

  933. static void
  934. ia64_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
  935.                             int regnum, const gdb_byte *buf)
  936. {
  937.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  938.   if (regnum >= V32_REGNUM && regnum <= V127_REGNUM)
  939.     {
  940.       ULONGEST bsp;
  941.       ULONGEST cfm;
  942.       regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
  943.       regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);

  944.       bsp = rse_address_add (bsp, -(cfm & 0x7f));

  945.       if ((cfm & 0x7f) > regnum - V32_REGNUM)
  946.         {
  947.           ULONGEST reg_addr = rse_address_add (bsp, (regnum - V32_REGNUM));
  948.           write_memory (reg_addr, (void *) buf, 8);
  949.         }
  950.     }
  951.   else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT31_REGNUM)
  952.     {
  953.       ULONGEST unatN_val, unat, unatN_mask;
  954.       regcache_cooked_read_unsigned (regcache, IA64_UNAT_REGNUM, &unat);
  955.       unatN_val = extract_unsigned_integer (buf, register_size (gdbarch,
  956.                                                                 regnum),
  957.                                             byte_order);
  958.       unatN_mask = (1LL << (regnum - IA64_NAT0_REGNUM));
  959.       if (unatN_val == 0)
  960.         unat &= ~unatN_mask;
  961.       else if (unatN_val == 1)
  962.         unat |= unatN_mask;
  963.       regcache_cooked_write_unsigned (regcache, IA64_UNAT_REGNUM, unat);
  964.     }
  965.   else if (IA64_NAT32_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
  966.     {
  967.       ULONGEST natN_val;
  968.       ULONGEST bsp;
  969.       ULONGEST cfm;
  970.       CORE_ADDR gr_addr = 0;
  971.       regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
  972.       regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);

  973.       /* The bsp points at the end of the register frame so we
  974.          subtract the size of frame from it to get start of register frame.  */
  975.       bsp = rse_address_add (bsp, -(cfm & 0x7f));

  976.       if ((cfm & 0x7f) > regnum - V32_REGNUM)
  977.         gr_addr = rse_address_add (bsp, (regnum - V32_REGNUM));

  978.       natN_val = extract_unsigned_integer (buf, register_size (gdbarch,
  979.                                                                regnum),
  980.                                            byte_order);

  981.       if (gr_addr != 0 && (natN_val == 0 || natN_val == 1))
  982.         {
  983.           /* Compute address of nat collection bits.  */
  984.           CORE_ADDR nat_addr = gr_addr | 0x1f8;
  985.           CORE_ADDR nat_collection;
  986.           int natN_bit = (gr_addr >> 3) & 0x3f;
  987.           ULONGEST natN_mask = (1LL << natN_bit);
  988.           /* If our nat collection address is bigger than bsp, we have to get
  989.              the nat collection from rnat.  Otherwise, we fetch the nat
  990.              collection from the computed address.  */
  991.           if (nat_addr >= bsp)
  992.             {
  993.               regcache_cooked_read_unsigned (regcache,
  994.                                              IA64_RNAT_REGNUM,
  995.                                              &nat_collection);
  996.               if (natN_val)
  997.                 nat_collection |= natN_mask;
  998.               else
  999.                 nat_collection &= ~natN_mask;
  1000.               regcache_cooked_write_unsigned (regcache, IA64_RNAT_REGNUM,
  1001.                                               nat_collection);
  1002.             }
  1003.           else
  1004.             {
  1005.               gdb_byte nat_buf[8];
  1006.               nat_collection = read_memory_integer (nat_addr, 8, byte_order);
  1007.               if (natN_val)
  1008.                 nat_collection |= natN_mask;
  1009.               else
  1010.                 nat_collection &= ~natN_mask;
  1011.               store_unsigned_integer (nat_buf, register_size (gdbarch, regnum),
  1012.                                       byte_order, nat_collection);
  1013.               write_memory (nat_addr, nat_buf, 8);
  1014.             }
  1015.         }
  1016.     }
  1017.   else if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
  1018.     {
  1019.       ULONGEST pr;
  1020.       ULONGEST cfm;
  1021.       ULONGEST prN_val;
  1022.       ULONGEST prN_mask;

  1023.       regcache_cooked_read_unsigned (regcache, IA64_PR_REGNUM, &pr);
  1024.       regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);

  1025.       if (VP16_REGNUM <= regnum && regnum <= VP63_REGNUM)
  1026.         {
  1027.           /* Fetch predicate register rename base from current frame
  1028.              marker for this frame.  */
  1029.           int rrb_pr = (cfm >> 32) & 0x3f;

  1030.           /* Adjust the register number to account for register rotation.  */
  1031.           regnum = VP16_REGNUM
  1032.                  + ((regnum - VP16_REGNUM) + rrb_pr) % 48;
  1033.         }
  1034.       prN_val = extract_unsigned_integer (buf, register_size (gdbarch, regnum),
  1035.                                           byte_order);
  1036.       prN_mask = (1LL << (regnum - VP0_REGNUM));
  1037.       if (prN_val == 0)
  1038.         pr &= ~prN_mask;
  1039.       else if (prN_val == 1)
  1040.         pr |= prN_mask;
  1041.       regcache_cooked_write_unsigned (regcache, IA64_PR_REGNUM, pr);
  1042.     }
  1043. }

  1044. /* The ia64 needs to convert between various ieee floating-point formats
  1045.    and the special ia64 floating point register format.  */

  1046. static int
  1047. ia64_convert_register_p (struct gdbarch *gdbarch, int regno, struct type *type)
  1048. {
  1049.   return (regno >= IA64_FR0_REGNUM && regno <= IA64_FR127_REGNUM
  1050.           && type != ia64_ext_type (gdbarch));
  1051. }

  1052. static int
  1053. ia64_register_to_value (struct frame_info *frame, int regnum,
  1054.                         struct type *valtype, gdb_byte *out,
  1055.                         int *optimizedp, int *unavailablep)
  1056. {
  1057.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1058.   gdb_byte in[MAX_REGISTER_SIZE];

  1059.   /* Convert to TYPE.  */
  1060.   if (!get_frame_register_bytes (frame, regnum, 0,
  1061.                                  register_size (gdbarch, regnum),
  1062.                                  in, optimizedp, unavailablep))
  1063.     return 0;

  1064.   convert_typed_floating (in, ia64_ext_type (gdbarch), out, valtype);
  1065.   *optimizedp = *unavailablep = 0;
  1066.   return 1;
  1067. }

  1068. static void
  1069. ia64_value_to_register (struct frame_info *frame, int regnum,
  1070.                          struct type *valtype, const gdb_byte *in)
  1071. {
  1072.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1073.   gdb_byte out[MAX_REGISTER_SIZE];
  1074.   convert_typed_floating (in, valtype, out, ia64_ext_type (gdbarch));
  1075.   put_frame_register (frame, regnum, out);
  1076. }


  1077. /* Limit the number of skipped non-prologue instructions since examining
  1078.    of the prologue is expensive.  */
  1079. static int max_skip_non_prologue_insns = 40;

  1080. /* Given PC representing the starting address of a function, and
  1081.    LIM_PC which is the (sloppy) limit to which to scan when looking
  1082.    for a prologue, attempt to further refine this limit by using
  1083.    the line data in the symbol table.  If successful, a better guess
  1084.    on where the prologue ends is returned, otherwise the previous
  1085.    value of lim_pc is returned.  TRUST_LIMIT is a pointer to a flag
  1086.    which will be set to indicate whether the returned limit may be
  1087.    used with no further scanning in the event that the function is
  1088.    frameless.  */

  1089. /* FIXME: cagney/2004-02-14: This function and logic have largely been
  1090.    superseded by skip_prologue_using_sal.  */

  1091. static CORE_ADDR
  1092. refine_prologue_limit (CORE_ADDR pc, CORE_ADDR lim_pc, int *trust_limit)
  1093. {
  1094.   struct symtab_and_line prologue_sal;
  1095.   CORE_ADDR start_pc = pc;
  1096.   CORE_ADDR end_pc;

  1097.   /* The prologue can not possibly go past the function end itself,
  1098.      so we can already adjust LIM_PC accordingly.  */
  1099.   if (find_pc_partial_function (pc, NULL, NULL, &end_pc) && end_pc < lim_pc)
  1100.     lim_pc = end_pc;

  1101.   /* Start off not trusting the limit.  */
  1102.   *trust_limit = 0;

  1103.   prologue_sal = find_pc_line (pc, 0);
  1104.   if (prologue_sal.line != 0)
  1105.     {
  1106.       int i;
  1107.       CORE_ADDR addr = prologue_sal.end;

  1108.       /* Handle the case in which compiler's optimizer/scheduler
  1109.          has moved instructions into the prologue.  We scan ahead
  1110.          in the function looking for address ranges whose corresponding
  1111.          line number is less than or equal to the first one that we
  1112.          found for the function.  (It can be less than when the
  1113.          scheduler puts a body instruction before the first prologue
  1114.          instruction.)  */
  1115.       for (i = 2 * max_skip_non_prologue_insns;
  1116.            i > 0 && (lim_pc == 0 || addr < lim_pc);
  1117.            i--)
  1118.         {
  1119.           struct symtab_and_line sal;

  1120.           sal = find_pc_line (addr, 0);
  1121.           if (sal.line == 0)
  1122.             break;
  1123.           if (sal.line <= prologue_sal.line
  1124.               && sal.symtab == prologue_sal.symtab)
  1125.             {
  1126.               prologue_sal = sal;
  1127.             }
  1128.           addr = sal.end;
  1129.         }

  1130.       if (lim_pc == 0 || prologue_sal.end < lim_pc)
  1131.         {
  1132.           lim_pc = prologue_sal.end;
  1133.           if (start_pc == get_pc_function_start (lim_pc))
  1134.             *trust_limit = 1;
  1135.         }
  1136.     }
  1137.   return lim_pc;
  1138. }

  1139. #define isScratch(_regnum_) ((_regnum_) == 2 || (_regnum_) == 3 \
  1140.   || (8 <= (_regnum_) && (_regnum_) <= 11) \
  1141.   || (14 <= (_regnum_) && (_regnum_) <= 31))
  1142. #define imm9(_instr_) \
  1143.   ( ((((_instr_) & 0x01000000000LL) ? -1 : 0) << 8) \
  1144.    | (((_instr_) & 0x00008000000LL) >> 20) \
  1145.    | (((_instr_) & 0x00000001fc0LL) >> 6))

  1146. /* Allocate and initialize a frame cache.  */

  1147. static struct ia64_frame_cache *
  1148. ia64_alloc_frame_cache (void)
  1149. {
  1150.   struct ia64_frame_cache *cache;
  1151.   int i;

  1152.   cache = FRAME_OBSTACK_ZALLOC (struct ia64_frame_cache);

  1153.   /* Base address.  */
  1154.   cache->base = 0;
  1155.   cache->pc = 0;
  1156.   cache->cfm = 0;
  1157.   cache->prev_cfm = 0;
  1158.   cache->sof = 0;
  1159.   cache->sol = 0;
  1160.   cache->sor = 0;
  1161.   cache->bsp = 0;
  1162.   cache->fp_reg = 0;
  1163.   cache->frameless = 1;

  1164.   for (i = 0; i < NUM_IA64_RAW_REGS; i++)
  1165.     cache->saved_regs[i] = 0;

  1166.   return cache;
  1167. }

  1168. static CORE_ADDR
  1169. examine_prologue (CORE_ADDR pc, CORE_ADDR lim_pc,
  1170.                   struct frame_info *this_frame,
  1171.                   struct ia64_frame_cache *cache)
  1172. {
  1173.   CORE_ADDR next_pc;
  1174.   CORE_ADDR last_prologue_pc = pc;
  1175.   instruction_type it;
  1176.   long long instr;
  1177.   int cfm_reg  = 0;
  1178.   int ret_reg  = 0;
  1179.   int fp_reg   = 0;
  1180.   int unat_save_reg = 0;
  1181.   int pr_save_reg = 0;
  1182.   int mem_stack_frame_size = 0;
  1183.   int spill_reg   = 0;
  1184.   CORE_ADDR spill_addr = 0;
  1185.   char instores[8];
  1186.   char infpstores[8];
  1187.   char reg_contents[256];
  1188.   int trust_limit;
  1189.   int frameless = 1;
  1190.   int i;
  1191.   CORE_ADDR addr;
  1192.   gdb_byte buf[8];
  1193.   CORE_ADDR bof, sor, sol, sof, cfm, rrb_gr;

  1194.   memset (instores, 0, sizeof instores);
  1195.   memset (infpstores, 0, sizeof infpstores);
  1196.   memset (reg_contents, 0, sizeof reg_contents);

  1197.   if (cache->after_prologue != 0
  1198.       && cache->after_prologue <= lim_pc)
  1199.     return cache->after_prologue;

  1200.   lim_pc = refine_prologue_limit (pc, lim_pc, &trust_limit);
  1201.   next_pc = fetch_instruction (pc, &it, &instr);

  1202.   /* We want to check if we have a recognizable function start before we
  1203.      look ahead for a prologue.  */
  1204.   if (pc < lim_pc && next_pc
  1205.       && it == M && ((instr & 0x1ee0000003fLL) == 0x02c00000000LL))
  1206.     {
  1207.       /* alloc - start of a regular function.  */
  1208.       int sor = (int) ((instr & 0x00078000000LL) >> 27);
  1209.       int sol = (int) ((instr & 0x00007f00000LL) >> 20);
  1210.       int sof = (int) ((instr & 0x000000fe000LL) >> 13);
  1211.       int rN = (int) ((instr & 0x00000001fc0LL) >> 6);

  1212.       /* Verify that the current cfm matches what we think is the
  1213.          function start.  If we have somehow jumped within a function,
  1214.          we do not want to interpret the prologue and calculate the
  1215.          addresses of various registers such as the return address.
  1216.          We will instead treat the frame as frameless.  */
  1217.       if (!this_frame ||
  1218.           (sof == (cache->cfm & 0x7f) &&
  1219.            sol == ((cache->cfm >> 7) & 0x7f)))
  1220.         frameless = 0;

  1221.       cfm_reg = rN;
  1222.       last_prologue_pc = next_pc;
  1223.       pc = next_pc;
  1224.     }
  1225.   else
  1226.     {
  1227.       /* Look for a leaf routine.  */
  1228.       if (pc < lim_pc && next_pc
  1229.           && (it == I || it == M)
  1230.           && ((instr & 0x1ee00000000LL) == 0x10800000000LL))
  1231.         {
  1232.           /* adds rN = imm14, rM   (or mov rN, rM  when imm14 is 0) */
  1233.           int imm = (int) ((((instr & 0x01000000000LL) ? -1 : 0) << 13)
  1234.                            | ((instr & 0x001f8000000LL) >> 20)
  1235.                            | ((instr & 0x000000fe000LL) >> 13));
  1236.           int rM = (int) ((instr & 0x00007f00000LL) >> 20);
  1237.           int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
  1238.           int qp = (int) (instr & 0x0000000003fLL);
  1239.           if (qp == 0 && rN == 2 && imm == 0 && rM == 12 && fp_reg == 0)
  1240.             {
  1241.               /* mov r2, r12 - beginning of leaf routine.  */
  1242.               fp_reg = rN;
  1243.               last_prologue_pc = next_pc;
  1244.             }
  1245.         }

  1246.       /* If we don't recognize a regular function or leaf routine, we are
  1247.          done.  */
  1248.       if (!fp_reg)
  1249.         {
  1250.           pc = lim_pc;
  1251.           if (trust_limit)
  1252.             last_prologue_pc = lim_pc;
  1253.         }
  1254.     }

  1255.   /* Loop, looking for prologue instructions, keeping track of
  1256.      where preserved registers were spilled.  */
  1257.   while (pc < lim_pc)
  1258.     {
  1259.       next_pc = fetch_instruction (pc, &it, &instr);
  1260.       if (next_pc == 0)
  1261.         break;

  1262.       if (it == B && ((instr & 0x1e1f800003fLL) != 0x04000000000LL))
  1263.         {
  1264.           /* Exit loop upon hitting a non-nop branch instruction.  */
  1265.           if (trust_limit)
  1266.             lim_pc = pc;
  1267.           break;
  1268.         }
  1269.       else if (((instr & 0x3fLL) != 0LL) &&
  1270.                (frameless || ret_reg != 0))
  1271.         {
  1272.           /* Exit loop upon hitting a predicated instruction if
  1273.              we already have the return register or if we are frameless.  */
  1274.           if (trust_limit)
  1275.             lim_pc = pc;
  1276.           break;
  1277.         }
  1278.       else if (it == I && ((instr & 0x1eff8000000LL) == 0x00188000000LL))
  1279.         {
  1280.           /* Move from BR */
  1281.           int b2 = (int) ((instr & 0x0000000e000LL) >> 13);
  1282.           int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
  1283.           int qp = (int) (instr & 0x0000000003f);

  1284.           if (qp == 0 && b2 == 0 && rN >= 32 && ret_reg == 0)
  1285.             {
  1286.               ret_reg = rN;
  1287.               last_prologue_pc = next_pc;
  1288.             }
  1289.         }
  1290.       else if ((it == I || it == M)
  1291.           && ((instr & 0x1ee00000000LL) == 0x10800000000LL))
  1292.         {
  1293.           /* adds rN = imm14, rM   (or mov rN, rM  when imm14 is 0) */
  1294.           int imm = (int) ((((instr & 0x01000000000LL) ? -1 : 0) << 13)
  1295.                            | ((instr & 0x001f8000000LL) >> 20)
  1296.                            | ((instr & 0x000000fe000LL) >> 13));
  1297.           int rM = (int) ((instr & 0x00007f00000LL) >> 20);
  1298.           int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
  1299.           int qp = (int) (instr & 0x0000000003fLL);

  1300.           if (qp == 0 && rN >= 32 && imm == 0 && rM == 12 && fp_reg == 0)
  1301.             {
  1302.               /* mov rN, r12 */
  1303.               fp_reg = rN;
  1304.               last_prologue_pc = next_pc;
  1305.             }
  1306.           else if (qp == 0 && rN == 12 && rM == 12)
  1307.             {
  1308.               /* adds r12, -mem_stack_frame_size, r12 */
  1309.               mem_stack_frame_size -= imm;
  1310.               last_prologue_pc = next_pc;
  1311.             }
  1312.           else if (qp == 0 && rN == 2
  1313.                 && ((rM == fp_reg && fp_reg != 0) || rM == 12))
  1314.             {
  1315.               gdb_byte buf[MAX_REGISTER_SIZE];
  1316.               CORE_ADDR saved_sp = 0;
  1317.               /* adds r2, spilloffset, rFramePointer
  1318.                    or
  1319.                  adds r2, spilloffset, r12

  1320.                  Get ready for stf.spill or st8.spill instructions.
  1321.                  The address to start spilling at is loaded into r2.
  1322.                  FIXME:  Why r2?  That's what gcc currently uses; it
  1323.                  could well be different for other compilers.  */

  1324.               /* Hmm...  whether or not this will work will depend on
  1325.                  where the pc is.  If it's still early in the prologue
  1326.                  this'll be wrong.  FIXME */
  1327.               if (this_frame)
  1328.                 {
  1329.                   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1330.                   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1331.                   get_frame_register (this_frame, sp_regnum, buf);
  1332.                   saved_sp = extract_unsigned_integer (buf, 8, byte_order);
  1333.                 }
  1334.               spill_addr  = saved_sp
  1335.                           + (rM == 12 ? 0 : mem_stack_frame_size)
  1336.                           + imm;
  1337.               spill_reg   = rN;
  1338.               last_prologue_pc = next_pc;
  1339.             }
  1340.           else if (qp == 0 && rM >= 32 && rM < 40 && !instores[rM-32] &&
  1341.                    rN < 256 && imm == 0)
  1342.             {
  1343.               /* mov rN, rM where rM is an input register.  */
  1344.               reg_contents[rN] = rM;
  1345.               last_prologue_pc = next_pc;
  1346.             }
  1347.           else if (frameless && qp == 0 && rN == fp_reg && imm == 0 &&
  1348.                    rM == 2)
  1349.             {
  1350.               /* mov r12, r2 */
  1351.               last_prologue_pc = next_pc;
  1352.               break;
  1353.             }
  1354.         }
  1355.       else if (it == M
  1356.             && (   ((instr & 0x1efc0000000LL) == 0x0eec0000000LL)
  1357.                 || ((instr & 0x1ffc8000000LL) == 0x0cec0000000LL) ))
  1358.         {
  1359.           /* stf.spill [rN] = fM, imm9
  1360.              or
  1361.              stf.spill [rN] = fM  */

  1362.           int imm = imm9(instr);
  1363.           int rN = (int) ((instr & 0x00007f00000LL) >> 20);
  1364.           int fM = (int) ((instr & 0x000000fe000LL) >> 13);
  1365.           int qp = (int) (instr & 0x0000000003fLL);
  1366.           if (qp == 0 && rN == spill_reg && spill_addr != 0
  1367.               && ((2 <= fM && fM <= 5) || (16 <= fM && fM <= 31)))
  1368.             {
  1369.               cache->saved_regs[IA64_FR0_REGNUM + fM] = spill_addr;

  1370.               if ((instr & 0x1efc0000000LL) == 0x0eec0000000LL)
  1371.                 spill_addr += imm;
  1372.               else
  1373.                 spill_addr = 0;                /* last one; must be done.  */
  1374.               last_prologue_pc = next_pc;
  1375.             }
  1376.         }
  1377.       else if ((it == M && ((instr & 0x1eff8000000LL) == 0x02110000000LL))
  1378.             || (it == I && ((instr & 0x1eff8000000LL) == 0x00050000000LL)) )
  1379.         {
  1380.           /* mov.m rN = arM
  1381.                or
  1382.              mov.i rN = arM */

  1383.           int arM = (int) ((instr & 0x00007f00000LL) >> 20);
  1384.           int rN  = (int) ((instr & 0x00000001fc0LL) >> 6);
  1385.           int qp  = (int) (instr & 0x0000000003fLL);
  1386.           if (qp == 0 && isScratch (rN) && arM == 36 /* ar.unat */)
  1387.             {
  1388.               /* We have something like "mov.m r3 = ar.unat".  Remember the
  1389.                  r3 (or whatever) and watch for a store of this register...  */
  1390.               unat_save_reg = rN;
  1391.               last_prologue_pc = next_pc;
  1392.             }
  1393.         }
  1394.       else if (it == I && ((instr & 0x1eff8000000LL) == 0x00198000000LL))
  1395.         {
  1396.           /* mov rN = pr */
  1397.           int rN  = (int) ((instr & 0x00000001fc0LL) >> 6);
  1398.           int qp  = (int) (instr & 0x0000000003fLL);
  1399.           if (qp == 0 && isScratch (rN))
  1400.             {
  1401.               pr_save_reg = rN;
  1402.               last_prologue_pc = next_pc;
  1403.             }
  1404.         }
  1405.       else if (it == M
  1406.             && (   ((instr & 0x1ffc8000000LL) == 0x08cc0000000LL)
  1407.                 || ((instr & 0x1efc0000000LL) == 0x0acc0000000LL)))
  1408.         {
  1409.           /* st8 [rN] = rM
  1410.               or
  1411.              st8 [rN] = rM, imm9 */
  1412.           int rN = (int) ((instr & 0x00007f00000LL) >> 20);
  1413.           int rM = (int) ((instr & 0x000000fe000LL) >> 13);
  1414.           int qp = (int) (instr & 0x0000000003fLL);
  1415.           int indirect = rM < 256 ? reg_contents[rM] : 0;
  1416.           if (qp == 0 && rN == spill_reg && spill_addr != 0
  1417.               && (rM == unat_save_reg || rM == pr_save_reg))
  1418.             {
  1419.               /* We've found a spill of either the UNAT register or the PR
  1420.                  register.  (Well, not exactly; what we've actually found is
  1421.                  a spill of the register that UNAT or PR was moved to).
  1422.                  Record that fact and move on...  */
  1423.               if (rM == unat_save_reg)
  1424.                 {
  1425.                   /* Track UNAT register.  */
  1426.                   cache->saved_regs[IA64_UNAT_REGNUM] = spill_addr;
  1427.                   unat_save_reg = 0;
  1428.                 }
  1429.               else
  1430.                 {
  1431.                   /* Track PR register.  */
  1432.                   cache->saved_regs[IA64_PR_REGNUM] = spill_addr;
  1433.                   pr_save_reg = 0;
  1434.                 }
  1435.               if ((instr & 0x1efc0000000LL) == 0x0acc0000000LL)
  1436.                 /* st8 [rN] = rM, imm9 */
  1437.                 spill_addr += imm9(instr);
  1438.               else
  1439.                 spill_addr = 0;                /* Must be done spilling.  */
  1440.               last_prologue_pc = next_pc;
  1441.             }
  1442.           else if (qp == 0 && 32 <= rM && rM < 40 && !instores[rM-32])
  1443.             {
  1444.               /* Allow up to one store of each input register.  */
  1445.               instores[rM-32] = 1;
  1446.               last_prologue_pc = next_pc;
  1447.             }
  1448.           else if (qp == 0 && 32 <= indirect && indirect < 40 &&
  1449.                    !instores[indirect-32])
  1450.             {
  1451.               /* Allow an indirect store of an input register.  */
  1452.               instores[indirect-32] = 1;
  1453.               last_prologue_pc = next_pc;
  1454.             }
  1455.         }
  1456.       else if (it == M && ((instr & 0x1ff08000000LL) == 0x08c00000000LL))
  1457.         {
  1458.           /* One of
  1459.                st1 [rN] = rM
  1460.                st2 [rN] = rM
  1461.                st4 [rN] = rM
  1462.                st8 [rN] = rM
  1463.              Note that the st8 case is handled in the clause above.

  1464.              Advance over stores of input registersOne store per input
  1465.              register is permitted.  */
  1466.           int rM = (int) ((instr & 0x000000fe000LL) >> 13);
  1467.           int qp = (int) (instr & 0x0000000003fLL);
  1468.           int indirect = rM < 256 ? reg_contents[rM] : 0;
  1469.           if (qp == 0 && 32 <= rM && rM < 40 && !instores[rM-32])
  1470.             {
  1471.               instores[rM-32] = 1;
  1472.               last_prologue_pc = next_pc;
  1473.             }
  1474.           else if (qp == 0 && 32 <= indirect && indirect < 40 &&
  1475.                    !instores[indirect-32])
  1476.             {
  1477.               /* Allow an indirect store of an input register.  */
  1478.               instores[indirect-32] = 1;
  1479.               last_prologue_pc = next_pc;
  1480.             }
  1481.         }
  1482.       else if (it == M && ((instr & 0x1ff88000000LL) == 0x0cc80000000LL))
  1483.         {
  1484.           /* Either
  1485.                stfs [rN] = fM
  1486.              or
  1487.                stfd [rN] = fM

  1488.              Advance over stores of floating point input registers.  Again
  1489.              one store per register is permitted.  */
  1490.           int fM = (int) ((instr & 0x000000fe000LL) >> 13);
  1491.           int qp = (int) (instr & 0x0000000003fLL);
  1492.           if (qp == 0 && 8 <= fM && fM < 16 && !infpstores[fM - 8])
  1493.             {
  1494.               infpstores[fM-8] = 1;
  1495.               last_prologue_pc = next_pc;
  1496.             }
  1497.         }
  1498.       else if (it == M
  1499.             && (   ((instr & 0x1ffc8000000LL) == 0x08ec0000000LL)
  1500.                 || ((instr & 0x1efc0000000LL) == 0x0aec0000000LL)))
  1501.         {
  1502.           /* st8.spill [rN] = rM
  1503.                or
  1504.              st8.spill [rN] = rM, imm9 */
  1505.           int rN = (int) ((instr & 0x00007f00000LL) >> 20);
  1506.           int rM = (int) ((instr & 0x000000fe000LL) >> 13);
  1507.           int qp = (int) (instr & 0x0000000003fLL);
  1508.           if (qp == 0 && rN == spill_reg && 4 <= rM && rM <= 7)
  1509.             {
  1510.               /* We've found a spill of one of the preserved general purpose
  1511.                  regs.  Record the spill address and advance the spill
  1512.                  register if appropriate.  */
  1513.               cache->saved_regs[IA64_GR0_REGNUM + rM] = spill_addr;
  1514.               if ((instr & 0x1efc0000000LL) == 0x0aec0000000LL)
  1515.                 /* st8.spill [rN] = rM, imm9 */
  1516.                 spill_addr += imm9(instr);
  1517.               else
  1518.                 spill_addr = 0;                /* Done spilling.  */
  1519.               last_prologue_pc = next_pc;
  1520.             }
  1521.         }

  1522.       pc = next_pc;
  1523.     }

  1524.   /* If not frameless and we aren't called by skip_prologue, then we need
  1525.      to calculate registers for the previous frame which will be needed
  1526.      later.  */

  1527.   if (!frameless && this_frame)
  1528.     {
  1529.       struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1530.       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  1531.       /* Extract the size of the rotating portion of the stack
  1532.          frame and the register rename base from the current
  1533.          frame marker.  */
  1534.       cfm = cache->cfm;
  1535.       sor = cache->sor;
  1536.       sof = cache->sof;
  1537.       sol = cache->sol;
  1538.       rrb_gr = (cfm >> 18) & 0x7f;

  1539.       /* Find the bof (beginning of frame).  */
  1540.       bof = rse_address_add (cache->bsp, -sof);

  1541.       for (i = 0, addr = bof;
  1542.            i < sof;
  1543.            i++, addr += 8)
  1544.         {
  1545.           if (IS_NaT_COLLECTION_ADDR (addr))
  1546.             {
  1547.               addr += 8;
  1548.             }
  1549.           if (i+32 == cfm_reg)
  1550.             cache->saved_regs[IA64_CFM_REGNUM] = addr;
  1551.           if (i+32 == ret_reg)
  1552.             cache->saved_regs[IA64_VRAP_REGNUM] = addr;
  1553.           if (i+32 == fp_reg)
  1554.             cache->saved_regs[IA64_VFP_REGNUM] = addr;
  1555.         }

  1556.       /* For the previous argument registers we require the previous bof.
  1557.          If we can't find the previous cfm, then we can do nothing.  */
  1558.       cfm = 0;
  1559.       if (cache->saved_regs[IA64_CFM_REGNUM] != 0)
  1560.         {
  1561.           cfm = read_memory_integer (cache->saved_regs[IA64_CFM_REGNUM],
  1562.                                      8, byte_order);
  1563.         }
  1564.       else if (cfm_reg != 0)
  1565.         {
  1566.           get_frame_register (this_frame, cfm_reg, buf);
  1567.           cfm = extract_unsigned_integer (buf, 8, byte_order);
  1568.         }
  1569.       cache->prev_cfm = cfm;

  1570.       if (cfm != 0)
  1571.         {
  1572.           sor = ((cfm >> 14) & 0xf) * 8;
  1573.           sof = (cfm & 0x7f);
  1574.           sol = (cfm >> 7) & 0x7f;
  1575.           rrb_gr = (cfm >> 18) & 0x7f;

  1576.           /* The previous bof only requires subtraction of the sol (size of
  1577.              locals) due to the overlap between output and input of
  1578.              subsequent frames.  */
  1579.           bof = rse_address_add (bof, -sol);

  1580.           for (i = 0, addr = bof;
  1581.                i < sof;
  1582.                i++, addr += 8)
  1583.             {
  1584.               if (IS_NaT_COLLECTION_ADDR (addr))
  1585.                 {
  1586.                   addr += 8;
  1587.                 }
  1588.               if (i < sor)
  1589.                 cache->saved_regs[IA64_GR32_REGNUM
  1590.                                   + ((i + (sor - rrb_gr)) % sor)]
  1591.                   = addr;
  1592.               else
  1593.                 cache->saved_regs[IA64_GR32_REGNUM + i] = addr;
  1594.             }

  1595.         }
  1596.     }

  1597.   /* Try and trust the lim_pc value whenever possible.  */
  1598.   if (trust_limit && lim_pc >= last_prologue_pc)
  1599.     last_prologue_pc = lim_pc;

  1600.   cache->frameless = frameless;
  1601.   cache->after_prologue = last_prologue_pc;
  1602.   cache->mem_stack_frame_size = mem_stack_frame_size;
  1603.   cache->fp_reg = fp_reg;

  1604.   return last_prologue_pc;
  1605. }

  1606. CORE_ADDR
  1607. ia64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  1608. {
  1609.   struct ia64_frame_cache cache;
  1610.   cache.base = 0;
  1611.   cache.after_prologue = 0;
  1612.   cache.cfm = 0;
  1613.   cache.bsp = 0;

  1614.   /* Call examine_prologue with - as third argument since we don't
  1615.      have a next frame pointer to send.  */
  1616.   return examine_prologue (pc, pc+1024, 0, &cache);
  1617. }


  1618. /* Normal frames.  */

  1619. static struct ia64_frame_cache *
  1620. ia64_frame_cache (struct frame_info *this_frame, void **this_cache)
  1621. {
  1622.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1623.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1624.   struct ia64_frame_cache *cache;
  1625.   gdb_byte buf[8];
  1626.   CORE_ADDR cfm, psr;

  1627.   if (*this_cache)
  1628.     return *this_cache;

  1629.   cache = ia64_alloc_frame_cache ();
  1630.   *this_cache = cache;

  1631.   get_frame_register (this_frame, sp_regnum, buf);
  1632.   cache->saved_sp = extract_unsigned_integer (buf, 8, byte_order);

  1633.   /* We always want the bsp to point to the end of frame.
  1634.      This way, we can always get the beginning of frame (bof)
  1635.      by subtracting frame size.  */
  1636.   get_frame_register (this_frame, IA64_BSP_REGNUM, buf);
  1637.   cache->bsp = extract_unsigned_integer (buf, 8, byte_order);

  1638.   get_frame_register (this_frame, IA64_PSR_REGNUM, buf);
  1639.   psr = extract_unsigned_integer (buf, 8, byte_order);

  1640.   get_frame_register (this_frame, IA64_CFM_REGNUM, buf);
  1641.   cfm = extract_unsigned_integer (buf, 8, byte_order);

  1642.   cache->sof = (cfm & 0x7f);
  1643.   cache->sol = (cfm >> 7) & 0x7f;
  1644.   cache->sor = ((cfm >> 14) & 0xf) * 8;

  1645.   cache->cfm = cfm;

  1646.   cache->pc = get_frame_func (this_frame);

  1647.   if (cache->pc != 0)
  1648.     examine_prologue (cache->pc, get_frame_pc (this_frame), this_frame, cache);

  1649.   cache->base = cache->saved_sp + cache->mem_stack_frame_size;

  1650.   return cache;
  1651. }

  1652. static void
  1653. ia64_frame_this_id (struct frame_info *this_frame, void **this_cache,
  1654.                     struct frame_id *this_id)
  1655. {
  1656.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1657.   struct ia64_frame_cache *cache =
  1658.     ia64_frame_cache (this_frame, this_cache);

  1659.   /* If outermost frame, mark with null frame id.  */
  1660.   if (cache->base != 0)
  1661.     (*this_id) = frame_id_build_special (cache->base, cache->pc, cache->bsp);
  1662.   if (gdbarch_debug >= 1)
  1663.     fprintf_unfiltered (gdb_stdlog,
  1664.                         "regular frame id: code %s, stack %s, "
  1665.                         "special %s, this_frame %s\n",
  1666.                         paddress (gdbarch, this_id->code_addr),
  1667.                         paddress (gdbarch, this_id->stack_addr),
  1668.                         paddress (gdbarch, cache->bsp),
  1669.                         host_address_to_string (this_frame));
  1670. }

  1671. static struct value *
  1672. ia64_frame_prev_register (struct frame_info *this_frame, void **this_cache,
  1673.                           int regnum)
  1674. {
  1675.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1676.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1677.   struct ia64_frame_cache *cache = ia64_frame_cache (this_frame, this_cache);
  1678.   gdb_byte buf[8];

  1679.   gdb_assert (regnum >= 0);

  1680.   if (!target_has_registers)
  1681.     error (_("No registers."));

  1682.   if (regnum == gdbarch_sp_regnum (gdbarch))
  1683.     return frame_unwind_got_constant (this_frame, regnum, cache->base);

  1684.   else if (regnum == IA64_BSP_REGNUM)
  1685.     {
  1686.       struct value *val;
  1687.       CORE_ADDR prev_cfm, bsp, prev_bsp;

  1688.       /* We want to calculate the previous bsp as the end of the previous
  1689.          register stack frame.  This corresponds to what the hardware bsp
  1690.          register will be if we pop the frame back which is why we might
  1691.          have been called.  We know the beginning of the current frame is
  1692.          cache->bsp - cache->sof.  This value in the previous frame points
  1693.          to the start of the output registers.  We can calculate the end of
  1694.          that frame by adding the size of output:
  1695.             (sof (size of frame) - sol (size of locals)).  */
  1696.       val = ia64_frame_prev_register (this_frame, this_cache, IA64_CFM_REGNUM);
  1697.       prev_cfm = extract_unsigned_integer (value_contents_all (val),
  1698.                                            8, byte_order);
  1699.       bsp = rse_address_add (cache->bsp, -(cache->sof));
  1700.       prev_bsp =
  1701.         rse_address_add (bsp, (prev_cfm & 0x7f) - ((prev_cfm >> 7) & 0x7f));

  1702.       return frame_unwind_got_constant (this_frame, regnum, prev_bsp);
  1703.     }

  1704.   else if (regnum == IA64_CFM_REGNUM)
  1705.     {
  1706.       CORE_ADDR addr = cache->saved_regs[IA64_CFM_REGNUM];

  1707.       if (addr != 0)
  1708.         return frame_unwind_got_memory (this_frame, regnum, addr);

  1709.       if (cache->prev_cfm)
  1710.         return frame_unwind_got_constant (this_frame, regnum, cache->prev_cfm);

  1711.       if (cache->frameless)
  1712.         return frame_unwind_got_register (this_frame, IA64_PFS_REGNUM,
  1713.                                           IA64_PFS_REGNUM);
  1714.       return frame_unwind_got_register (this_frame, regnum, 0);
  1715.     }

  1716.   else if (regnum == IA64_VFP_REGNUM)
  1717.     {
  1718.       /* If the function in question uses an automatic register (r32-r127)
  1719.          for the frame pointer, it'll be found by ia64_find_saved_register()
  1720.          above.  If the function lacks one of these frame pointers, we can
  1721.          still provide a value since we know the size of the frame.  */
  1722.       return frame_unwind_got_constant (this_frame, regnum, cache->base);
  1723.     }

  1724.   else if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
  1725.     {
  1726.       struct value *pr_val;
  1727.       ULONGEST prN;

  1728.       pr_val = ia64_frame_prev_register (this_frame, this_cache,
  1729.                                          IA64_PR_REGNUM);
  1730.       if (VP16_REGNUM <= regnum && regnum <= VP63_REGNUM)
  1731.         {
  1732.           /* Fetch predicate register rename base from current frame
  1733.              marker for this frame.  */
  1734.           int rrb_pr = (cache->cfm >> 32) & 0x3f;

  1735.           /* Adjust the register number to account for register rotation.  */
  1736.           regnum = VP16_REGNUM + ((regnum - VP16_REGNUM) + rrb_pr) % 48;
  1737.         }
  1738.       prN = extract_bit_field (value_contents_all (pr_val),
  1739.                                regnum - VP0_REGNUM, 1);
  1740.       return frame_unwind_got_constant (this_frame, regnum, prN);
  1741.     }

  1742.   else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT31_REGNUM)
  1743.     {
  1744.       struct value *unat_val;
  1745.       ULONGEST unatN;
  1746.       unat_val = ia64_frame_prev_register (this_frame, this_cache,
  1747.                                            IA64_UNAT_REGNUM);
  1748.       unatN = extract_bit_field (value_contents_all (unat_val),
  1749.                                  regnum - IA64_NAT0_REGNUM, 1);
  1750.       return frame_unwind_got_constant (this_frame, regnum, unatN);
  1751.     }

  1752.   else if (IA64_NAT32_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
  1753.     {
  1754.       int natval = 0;
  1755.       /* Find address of general register corresponding to nat bit we're
  1756.          interested in.  */
  1757.       CORE_ADDR gr_addr;

  1758.       gr_addr = cache->saved_regs[regnum - IA64_NAT0_REGNUM + IA64_GR0_REGNUM];

  1759.       if (gr_addr != 0)
  1760.         {
  1761.           /* Compute address of nat collection bits.  */
  1762.           CORE_ADDR nat_addr = gr_addr | 0x1f8;
  1763.           CORE_ADDR bsp;
  1764.           CORE_ADDR nat_collection;
  1765.           int nat_bit;

  1766.           /* If our nat collection address is bigger than bsp, we have to get
  1767.              the nat collection from rnat.  Otherwise, we fetch the nat
  1768.              collection from the computed address.  */
  1769.           get_frame_register (this_frame, IA64_BSP_REGNUM, buf);
  1770.           bsp = extract_unsigned_integer (buf, 8, byte_order);
  1771.           if (nat_addr >= bsp)
  1772.             {
  1773.               get_frame_register (this_frame, IA64_RNAT_REGNUM, buf);
  1774.               nat_collection = extract_unsigned_integer (buf, 8, byte_order);
  1775.             }
  1776.           else
  1777.             nat_collection = read_memory_integer (nat_addr, 8, byte_order);
  1778.           nat_bit = (gr_addr >> 3) & 0x3f;
  1779.           natval = (nat_collection >> nat_bit) & 1;
  1780.         }

  1781.       return frame_unwind_got_constant (this_frame, regnum, natval);
  1782.     }

  1783.   else if (regnum == IA64_IP_REGNUM)
  1784.     {
  1785.       CORE_ADDR pc = 0;
  1786.       CORE_ADDR addr = cache->saved_regs[IA64_VRAP_REGNUM];

  1787.       if (addr != 0)
  1788.         {
  1789.           read_memory (addr, buf, register_size (gdbarch, IA64_IP_REGNUM));
  1790.           pc = extract_unsigned_integer (buf, 8, byte_order);
  1791.         }
  1792.       else if (cache->frameless)
  1793.         {
  1794.           get_frame_register (this_frame, IA64_BR0_REGNUM, buf);
  1795.           pc = extract_unsigned_integer (buf, 8, byte_order);
  1796.         }
  1797.       pc &= ~0xf;
  1798.       return frame_unwind_got_constant (this_frame, regnum, pc);
  1799.     }

  1800.   else if (regnum == IA64_PSR_REGNUM)
  1801.     {
  1802.       /* We don't know how to get the complete previous PSR, but we need it
  1803.          for the slot information when we unwind the pc (pc is formed of IP
  1804.          register plus slot information from PSR).  To get the previous
  1805.          slot information, we mask it off the return address.  */
  1806.       ULONGEST slot_num = 0;
  1807.       CORE_ADDR pc = 0;
  1808.       CORE_ADDR psr = 0;
  1809.       CORE_ADDR addr = cache->saved_regs[IA64_VRAP_REGNUM];

  1810.       get_frame_register (this_frame, IA64_PSR_REGNUM, buf);
  1811.       psr = extract_unsigned_integer (buf, 8, byte_order);

  1812.       if (addr != 0)
  1813.         {
  1814.           read_memory (addr, buf, register_size (gdbarch, IA64_IP_REGNUM));
  1815.           pc = extract_unsigned_integer (buf, 8, byte_order);
  1816.         }
  1817.       else if (cache->frameless)
  1818.         {
  1819.           get_frame_register (this_frame, IA64_BR0_REGNUM, buf);
  1820.           pc = extract_unsigned_integer (buf, 8, byte_order);
  1821.         }
  1822.       psr &= ~(3LL << 41);
  1823.       slot_num = pc & 0x3LL;
  1824.       psr |= (CORE_ADDR)slot_num << 41;
  1825.       return frame_unwind_got_constant (this_frame, regnum, psr);
  1826.     }

  1827.   else if (regnum == IA64_BR0_REGNUM)
  1828.     {
  1829.       CORE_ADDR addr = cache->saved_regs[IA64_BR0_REGNUM];

  1830.       if (addr != 0)
  1831.         return frame_unwind_got_memory (this_frame, regnum, addr);

  1832.       return frame_unwind_got_constant (this_frame, regnum, 0);
  1833.     }

  1834.   else if ((regnum >= IA64_GR32_REGNUM && regnum <= IA64_GR127_REGNUM)
  1835.            || (regnum >= V32_REGNUM && regnum <= V127_REGNUM))
  1836.     {
  1837.       CORE_ADDR addr = 0;

  1838.       if (regnum >= V32_REGNUM)
  1839.         regnum = IA64_GR32_REGNUM + (regnum - V32_REGNUM);
  1840.       addr = cache->saved_regs[regnum];
  1841.       if (addr != 0)
  1842.         return frame_unwind_got_memory (this_frame, regnum, addr);

  1843.       if (cache->frameless)
  1844.         {
  1845.           struct value *reg_val;
  1846.           CORE_ADDR prev_cfm, prev_bsp, prev_bof;

  1847.           /* FIXME: brobecker/2008-05-01: Doesn't this seem redundant
  1848.              with the same code above?  */
  1849.           if (regnum >= V32_REGNUM)
  1850.             regnum = IA64_GR32_REGNUM + (regnum - V32_REGNUM);
  1851.           reg_val = ia64_frame_prev_register (this_frame, this_cache,
  1852.                                               IA64_CFM_REGNUM);
  1853.           prev_cfm = extract_unsigned_integer (value_contents_all (reg_val),
  1854.                                                8, byte_order);
  1855.           reg_val = ia64_frame_prev_register (this_frame, this_cache,
  1856.                                               IA64_BSP_REGNUM);
  1857.           prev_bsp = extract_unsigned_integer (value_contents_all (reg_val),
  1858.                                                8, byte_order);
  1859.           prev_bof = rse_address_add (prev_bsp, -(prev_cfm & 0x7f));

  1860.           addr = rse_address_add (prev_bof, (regnum - IA64_GR32_REGNUM));
  1861.           return frame_unwind_got_memory (this_frame, regnum, addr);
  1862.         }

  1863.       return frame_unwind_got_constant (this_frame, regnum, 0);
  1864.     }

  1865.   else /* All other registers.  */
  1866.     {
  1867.       CORE_ADDR addr = 0;

  1868.       if (IA64_FR32_REGNUM <= regnum && regnum <= IA64_FR127_REGNUM)
  1869.         {
  1870.           /* Fetch floating point register rename base from current
  1871.              frame marker for this frame.  */
  1872.           int rrb_fr = (cache->cfm >> 25) & 0x7f;

  1873.           /* Adjust the floating point register number to account for
  1874.              register rotation.  */
  1875.           regnum = IA64_FR32_REGNUM
  1876.                  + ((regnum - IA64_FR32_REGNUM) + rrb_fr) % 96;
  1877.         }

  1878.       /* If we have stored a memory address, access the register.  */
  1879.       addr = cache->saved_regs[regnum];
  1880.       if (addr != 0)
  1881.         return frame_unwind_got_memory (this_frame, regnum, addr);
  1882.       /* Otherwise, punt and get the current value of the register.  */
  1883.       else
  1884.         return frame_unwind_got_register (this_frame, regnum, regnum);
  1885.     }
  1886. }

  1887. static const struct frame_unwind ia64_frame_unwind =
  1888. {
  1889.   NORMAL_FRAME,
  1890.   default_frame_unwind_stop_reason,
  1891.   &ia64_frame_this_id,
  1892.   &ia64_frame_prev_register,
  1893.   NULL,
  1894.   default_frame_sniffer
  1895. };

  1896. /* Signal trampolines.  */

  1897. static void
  1898. ia64_sigtramp_frame_init_saved_regs (struct frame_info *this_frame,
  1899.                                      struct ia64_frame_cache *cache)
  1900. {
  1901.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1902.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  1903.   if (tdep->sigcontext_register_address)
  1904.     {
  1905.       int regno;

  1906.       cache->saved_regs[IA64_VRAP_REGNUM]
  1907.         = tdep->sigcontext_register_address (gdbarch, cache->base,
  1908.                                              IA64_IP_REGNUM);
  1909.       cache->saved_regs[IA64_CFM_REGNUM]
  1910.         = tdep->sigcontext_register_address (gdbarch, cache->base,
  1911.                                              IA64_CFM_REGNUM);
  1912.       cache->saved_regs[IA64_PSR_REGNUM]
  1913.         = tdep->sigcontext_register_address (gdbarch, cache->base,
  1914.                                              IA64_PSR_REGNUM);
  1915.       cache->saved_regs[IA64_BSP_REGNUM]
  1916.         = tdep->sigcontext_register_address (gdbarch, cache->base,
  1917.                                              IA64_BSP_REGNUM);
  1918.       cache->saved_regs[IA64_RNAT_REGNUM]
  1919.         = tdep->sigcontext_register_address (gdbarch, cache->base,
  1920.                                              IA64_RNAT_REGNUM);
  1921.       cache->saved_regs[IA64_CCV_REGNUM]
  1922.         = tdep->sigcontext_register_address (gdbarch, cache->base,
  1923.                                              IA64_CCV_REGNUM);
  1924.       cache->saved_regs[IA64_UNAT_REGNUM]
  1925.         = tdep->sigcontext_register_address (gdbarch, cache->base,
  1926.                                              IA64_UNAT_REGNUM);
  1927.       cache->saved_regs[IA64_FPSR_REGNUM]
  1928.         = tdep->sigcontext_register_address (gdbarch, cache->base,
  1929.                                              IA64_FPSR_REGNUM);
  1930.       cache->saved_regs[IA64_PFS_REGNUM]
  1931.         = tdep->sigcontext_register_address (gdbarch, cache->base,
  1932.                                              IA64_PFS_REGNUM);
  1933.       cache->saved_regs[IA64_LC_REGNUM]
  1934.         = tdep->sigcontext_register_address (gdbarch, cache->base,
  1935.                                              IA64_LC_REGNUM);

  1936.       for (regno = IA64_GR1_REGNUM; regno <= IA64_GR31_REGNUM; regno++)
  1937.         cache->saved_regs[regno] =
  1938.           tdep->sigcontext_register_address (gdbarch, cache->base, regno);
  1939.       for (regno = IA64_BR0_REGNUM; regno <= IA64_BR7_REGNUM; regno++)
  1940.         cache->saved_regs[regno] =
  1941.           tdep->sigcontext_register_address (gdbarch, cache->base, regno);
  1942.       for (regno = IA64_FR2_REGNUM; regno <= IA64_FR31_REGNUM; regno++)
  1943.         cache->saved_regs[regno] =
  1944.           tdep->sigcontext_register_address (gdbarch, cache->base, regno);
  1945.     }
  1946. }

  1947. static struct ia64_frame_cache *
  1948. ia64_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
  1949. {
  1950.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1951.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1952.   struct ia64_frame_cache *cache;
  1953.   gdb_byte buf[8];

  1954.   if (*this_cache)
  1955.     return *this_cache;

  1956.   cache = ia64_alloc_frame_cache ();

  1957.   get_frame_register (this_frame, sp_regnum, buf);
  1958.   /* Note that frame size is hard-coded below.  We cannot calculate it
  1959.      via prologue examination.  */
  1960.   cache->base = extract_unsigned_integer (buf, 8, byte_order) + 16;

  1961.   get_frame_register (this_frame, IA64_BSP_REGNUM, buf);
  1962.   cache->bsp = extract_unsigned_integer (buf, 8, byte_order);

  1963.   get_frame_register (this_frame, IA64_CFM_REGNUM, buf);
  1964.   cache->cfm = extract_unsigned_integer (buf, 8, byte_order);
  1965.   cache->sof = cache->cfm & 0x7f;

  1966.   ia64_sigtramp_frame_init_saved_regs (this_frame, cache);

  1967.   *this_cache = cache;
  1968.   return cache;
  1969. }

  1970. static void
  1971. ia64_sigtramp_frame_this_id (struct frame_info *this_frame,
  1972.                              void **this_cache, struct frame_id *this_id)
  1973. {
  1974.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1975.   struct ia64_frame_cache *cache =
  1976.     ia64_sigtramp_frame_cache (this_frame, this_cache);

  1977.   (*this_id) = frame_id_build_special (cache->base,
  1978.                                        get_frame_pc (this_frame),
  1979.                                        cache->bsp);
  1980.   if (gdbarch_debug >= 1)
  1981.     fprintf_unfiltered (gdb_stdlog,
  1982.                         "sigtramp frame id: code %s, stack %s, "
  1983.                         "special %s, this_frame %s\n",
  1984.                         paddress (gdbarch, this_id->code_addr),
  1985.                         paddress (gdbarch, this_id->stack_addr),
  1986.                         paddress (gdbarch, cache->bsp),
  1987.                         host_address_to_string (this_frame));
  1988. }

  1989. static struct value *
  1990. ia64_sigtramp_frame_prev_register (struct frame_info *this_frame,
  1991.                                    void **this_cache, int regnum)
  1992. {
  1993.   gdb_byte buf[MAX_REGISTER_SIZE];

  1994.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1995.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1996.   struct ia64_frame_cache *cache =
  1997.     ia64_sigtramp_frame_cache (this_frame, this_cache);

  1998.   gdb_assert (regnum >= 0);

  1999.   if (!target_has_registers)
  2000.     error (_("No registers."));

  2001.   if (regnum == IA64_IP_REGNUM)
  2002.     {
  2003.       CORE_ADDR pc = 0;
  2004.       CORE_ADDR addr = cache->saved_regs[IA64_VRAP_REGNUM];

  2005.       if (addr != 0)
  2006.         {
  2007.           read_memory (addr, buf, register_size (gdbarch, IA64_IP_REGNUM));
  2008.           pc = extract_unsigned_integer (buf, 8, byte_order);
  2009.         }
  2010.       pc &= ~0xf;
  2011.       return frame_unwind_got_constant (this_frame, regnum, pc);
  2012.     }

  2013.   else if ((regnum >= IA64_GR32_REGNUM && regnum <= IA64_GR127_REGNUM)
  2014.            || (regnum >= V32_REGNUM && regnum <= V127_REGNUM))
  2015.     {
  2016.       CORE_ADDR addr = 0;

  2017.       if (regnum >= V32_REGNUM)
  2018.         regnum = IA64_GR32_REGNUM + (regnum - V32_REGNUM);
  2019.       addr = cache->saved_regs[regnum];
  2020.       if (addr != 0)
  2021.         return frame_unwind_got_memory (this_frame, regnum, addr);

  2022.       return frame_unwind_got_constant (this_frame, regnum, 0);
  2023.     }

  2024.   else  /* All other registers not listed above.  */
  2025.     {
  2026.       CORE_ADDR addr = cache->saved_regs[regnum];

  2027.       if (addr != 0)
  2028.         return frame_unwind_got_memory (this_frame, regnum, addr);

  2029.       return frame_unwind_got_constant (this_frame, regnum, 0);
  2030.     }
  2031. }

  2032. static int
  2033. ia64_sigtramp_frame_sniffer (const struct frame_unwind *self,
  2034.                              struct frame_info *this_frame,
  2035.                              void **this_cache)
  2036. {
  2037.   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
  2038.   if (tdep->pc_in_sigtramp)
  2039.     {
  2040.       CORE_ADDR pc = get_frame_pc (this_frame);

  2041.       if (tdep->pc_in_sigtramp (pc))
  2042.         return 1;
  2043.     }

  2044.   return 0;
  2045. }

  2046. static const struct frame_unwind ia64_sigtramp_frame_unwind =
  2047. {
  2048.   SIGTRAMP_FRAME,
  2049.   default_frame_unwind_stop_reason,
  2050.   ia64_sigtramp_frame_this_id,
  2051.   ia64_sigtramp_frame_prev_register,
  2052.   NULL,
  2053.   ia64_sigtramp_frame_sniffer
  2054. };



  2055. static CORE_ADDR
  2056. ia64_frame_base_address (struct frame_info *this_frame, void **this_cache)
  2057. {
  2058.   struct ia64_frame_cache *cache = ia64_frame_cache (this_frame, this_cache);

  2059.   return cache->base;
  2060. }

  2061. static const struct frame_base ia64_frame_base =
  2062. {
  2063.   &ia64_frame_unwind,
  2064.   ia64_frame_base_address,
  2065.   ia64_frame_base_address,
  2066.   ia64_frame_base_address
  2067. };

  2068. #ifdef HAVE_LIBUNWIND_IA64_H

  2069. struct ia64_unwind_table_entry
  2070.   {
  2071.     unw_word_t start_offset;
  2072.     unw_word_t end_offset;
  2073.     unw_word_t info_offset;
  2074.   };

  2075. static __inline__ uint64_t
  2076. ia64_rse_slot_num (uint64_t addr)
  2077. {
  2078.   return (addr >> 3) & 0x3f;
  2079. }

  2080. /* Skip over a designated number of registers in the backing
  2081.    store, remembering every 64th position is for NAT.  */
  2082. static __inline__ uint64_t
  2083. ia64_rse_skip_regs (uint64_t addr, long num_regs)
  2084. {
  2085.   long delta = ia64_rse_slot_num(addr) + num_regs;

  2086.   if (num_regs < 0)
  2087.     delta -= 0x3e;
  2088.   return addr + ((num_regs + delta/0x3f) << 3);
  2089. }

  2090. /* Gdb ia64-libunwind-tdep callback function to convert from an ia64 gdb
  2091.    register number to a libunwind register number.  */
  2092. static int
  2093. ia64_gdb2uw_regnum (int regnum)
  2094. {
  2095.   if (regnum == sp_regnum)
  2096.     return UNW_IA64_SP;
  2097.   else if (regnum == IA64_BSP_REGNUM)
  2098.     return UNW_IA64_BSP;
  2099.   else if ((unsigned) (regnum - IA64_GR0_REGNUM) < 128)
  2100.     return UNW_IA64_GR + (regnum - IA64_GR0_REGNUM);
  2101.   else if ((unsigned) (regnum - V32_REGNUM) < 95)
  2102.     return UNW_IA64_GR + 32 + (regnum - V32_REGNUM);
  2103.   else if ((unsigned) (regnum - IA64_FR0_REGNUM) < 128)
  2104.     return UNW_IA64_FR + (regnum - IA64_FR0_REGNUM);
  2105.   else if ((unsigned) (regnum - IA64_PR0_REGNUM) < 64)
  2106.     return -1;
  2107.   else if ((unsigned) (regnum - IA64_BR0_REGNUM) < 8)
  2108.     return UNW_IA64_BR + (regnum - IA64_BR0_REGNUM);
  2109.   else if (regnum == IA64_PR_REGNUM)
  2110.     return UNW_IA64_PR;
  2111.   else if (regnum == IA64_IP_REGNUM)
  2112.     return UNW_REG_IP;
  2113.   else if (regnum == IA64_CFM_REGNUM)
  2114.     return UNW_IA64_CFM;
  2115.   else if ((unsigned) (regnum - IA64_AR0_REGNUM) < 128)
  2116.     return UNW_IA64_AR + (regnum - IA64_AR0_REGNUM);
  2117.   else if ((unsigned) (regnum - IA64_NAT0_REGNUM) < 128)
  2118.     return UNW_IA64_NAT + (regnum - IA64_NAT0_REGNUM);
  2119.   else
  2120.     return -1;
  2121. }

  2122. /* Gdb ia64-libunwind-tdep callback function to convert from a libunwind
  2123.    register number to a ia64 gdb register number.  */
  2124. static int
  2125. ia64_uw2gdb_regnum (int uw_regnum)
  2126. {
  2127.   if (uw_regnum == UNW_IA64_SP)
  2128.     return sp_regnum;
  2129.   else if (uw_regnum == UNW_IA64_BSP)
  2130.     return IA64_BSP_REGNUM;
  2131.   else if ((unsigned) (uw_regnum - UNW_IA64_GR) < 32)
  2132.     return IA64_GR0_REGNUM + (uw_regnum - UNW_IA64_GR);
  2133.   else if ((unsigned) (uw_regnum - UNW_IA64_GR) < 128)
  2134.     return V32_REGNUM + (uw_regnum - (IA64_GR0_REGNUM + 32));
  2135.   else if ((unsigned) (uw_regnum - UNW_IA64_FR) < 128)
  2136.     return IA64_FR0_REGNUM + (uw_regnum - UNW_IA64_FR);
  2137.   else if ((unsigned) (uw_regnum - UNW_IA64_BR) < 8)
  2138.     return IA64_BR0_REGNUM + (uw_regnum - UNW_IA64_BR);
  2139.   else if (uw_regnum == UNW_IA64_PR)
  2140.     return IA64_PR_REGNUM;
  2141.   else if (uw_regnum == UNW_REG_IP)
  2142.     return IA64_IP_REGNUM;
  2143.   else if (uw_regnum == UNW_IA64_CFM)
  2144.     return IA64_CFM_REGNUM;
  2145.   else if ((unsigned) (uw_regnum - UNW_IA64_AR) < 128)
  2146.     return IA64_AR0_REGNUM + (uw_regnum - UNW_IA64_AR);
  2147.   else if ((unsigned) (uw_regnum - UNW_IA64_NAT) < 128)
  2148.     return IA64_NAT0_REGNUM + (uw_regnum - UNW_IA64_NAT);
  2149.   else
  2150.     return -1;
  2151. }

  2152. /* Gdb ia64-libunwind-tdep callback function to reveal if register is
  2153.    a float register or not.  */
  2154. static int
  2155. ia64_is_fpreg (int uw_regnum)
  2156. {
  2157.   return unw_is_fpreg (uw_regnum);
  2158. }

  2159. /* Libunwind callback accessor function for general registers.  */
  2160. static int
  2161. ia64_access_reg (unw_addr_space_t as, unw_regnum_t uw_regnum, unw_word_t *val,
  2162.                  int write, void *arg)
  2163. {
  2164.   int regnum = ia64_uw2gdb_regnum (uw_regnum);
  2165.   unw_word_t bsp, sof, sol, cfm, psr, ip;
  2166.   struct frame_info *this_frame = arg;
  2167.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2168.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2169.   long new_sof, old_sof;
  2170.   gdb_byte buf[MAX_REGISTER_SIZE];

  2171.   /* We never call any libunwind routines that need to write registers.  */
  2172.   gdb_assert (!write);

  2173.   switch (uw_regnum)
  2174.     {
  2175.       case UNW_REG_IP:
  2176.         /* Libunwind expects to see the pc value which means the slot number
  2177.            from the psr must be merged with the ip word address.  */
  2178.         get_frame_register (this_frame, IA64_IP_REGNUM, buf);
  2179.         ip = extract_unsigned_integer (buf, 8, byte_order);
  2180.         get_frame_register (this_frame, IA64_PSR_REGNUM, buf);
  2181.         psr = extract_unsigned_integer (buf, 8, byte_order);
  2182.         *val = ip | ((psr >> 41) & 0x3);
  2183.         break;

  2184.       case UNW_IA64_AR_BSP:
  2185.         /* Libunwind expects to see the beginning of the current
  2186.            register frame so we must account for the fact that
  2187.            ptrace() will return a value for bsp that points *after*
  2188.            the current register frame.  */
  2189.         get_frame_register (this_frame, IA64_BSP_REGNUM, buf);
  2190.         bsp = extract_unsigned_integer (buf, 8, byte_order);
  2191.         get_frame_register (this_frame, IA64_CFM_REGNUM, buf);
  2192.         cfm = extract_unsigned_integer (buf, 8, byte_order);
  2193.         sof = gdbarch_tdep (gdbarch)->size_of_register_frame (this_frame, cfm);
  2194.         *val = ia64_rse_skip_regs (bsp, -sof);
  2195.         break;

  2196.       case UNW_IA64_AR_BSPSTORE:
  2197.         /* Libunwind wants bspstore to be after the current register frame.
  2198.            This is what ptrace() and gdb treats as the regular bsp value.  */
  2199.         get_frame_register (this_frame, IA64_BSP_REGNUM, buf);
  2200.         *val = extract_unsigned_integer (buf, 8, byte_order);
  2201.         break;

  2202.       default:
  2203.         /* For all other registers, just unwind the value directly.  */
  2204.         get_frame_register (this_frame, regnum, buf);
  2205.         *val = extract_unsigned_integer (buf, 8, byte_order);
  2206.         break;
  2207.     }

  2208.   if (gdbarch_debug >= 1)
  2209.     fprintf_unfiltered (gdb_stdlog,
  2210.                         "  access_reg: from cache: %4s=%s\n",
  2211.                         (((unsigned) regnum <= IA64_NAT127_REGNUM)
  2212.                         ? ia64_register_names[regnum] : "r??"),
  2213.                         paddress (gdbarch, *val));
  2214.   return 0;
  2215. }

  2216. /* Libunwind callback accessor function for floating-point registers.  */
  2217. static int
  2218. ia64_access_fpreg (unw_addr_space_t as, unw_regnum_t uw_regnum,
  2219.                    unw_fpreg_t *val, int write, void *arg)
  2220. {
  2221.   int regnum = ia64_uw2gdb_regnum (uw_regnum);
  2222.   struct frame_info *this_frame = arg;

  2223.   /* We never call any libunwind routines that need to write registers.  */
  2224.   gdb_assert (!write);

  2225.   get_frame_register (this_frame, regnum, (gdb_byte *) val);

  2226.   return 0;
  2227. }

  2228. /* Libunwind callback accessor function for top-level rse registers.  */
  2229. static int
  2230. ia64_access_rse_reg (unw_addr_space_t as, unw_regnum_t uw_regnum,
  2231.                      unw_word_t *val, int write, void *arg)
  2232. {
  2233.   int regnum = ia64_uw2gdb_regnum (uw_regnum);
  2234.   unw_word_t bsp, sof, sol, cfm, psr, ip;
  2235.   struct regcache *regcache = arg;
  2236.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  2237.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2238.   long new_sof, old_sof;
  2239.   gdb_byte buf[MAX_REGISTER_SIZE];

  2240.   /* We never call any libunwind routines that need to write registers.  */
  2241.   gdb_assert (!write);

  2242.   switch (uw_regnum)
  2243.     {
  2244.       case UNW_REG_IP:
  2245.         /* Libunwind expects to see the pc value which means the slot number
  2246.            from the psr must be merged with the ip word address.  */
  2247.         regcache_cooked_read (regcache, IA64_IP_REGNUM, buf);
  2248.         ip = extract_unsigned_integer (buf, 8, byte_order);
  2249.         regcache_cooked_read (regcache, IA64_PSR_REGNUM, buf);
  2250.         psr = extract_unsigned_integer (buf, 8, byte_order);
  2251.         *val = ip | ((psr >> 41) & 0x3);
  2252.         break;

  2253.       case UNW_IA64_AR_BSP:
  2254.         /* Libunwind expects to see the beginning of the current
  2255.            register frame so we must account for the fact that
  2256.            ptrace() will return a value for bsp that points *after*
  2257.            the current register frame.  */
  2258.         regcache_cooked_read (regcache, IA64_BSP_REGNUM, buf);
  2259.         bsp = extract_unsigned_integer (buf, 8, byte_order);
  2260.         regcache_cooked_read (regcache, IA64_CFM_REGNUM, buf);
  2261.         cfm = extract_unsigned_integer (buf, 8, byte_order);
  2262.         sof = (cfm & 0x7f);
  2263.         *val = ia64_rse_skip_regs (bsp, -sof);
  2264.         break;

  2265.       case UNW_IA64_AR_BSPSTORE:
  2266.         /* Libunwind wants bspstore to be after the current register frame.
  2267.            This is what ptrace() and gdb treats as the regular bsp value.  */
  2268.         regcache_cooked_read (regcache, IA64_BSP_REGNUM, buf);
  2269.         *val = extract_unsigned_integer (buf, 8, byte_order);
  2270.         break;

  2271.       default:
  2272.         /* For all other registers, just unwind the value directly.  */
  2273.         regcache_cooked_read (regcache, regnum, buf);
  2274.         *val = extract_unsigned_integer (buf, 8, byte_order);
  2275.         break;
  2276.     }

  2277.   if (gdbarch_debug >= 1)
  2278.     fprintf_unfiltered (gdb_stdlog,
  2279.                         "  access_rse_reg: from cache: %4s=%s\n",
  2280.                         (((unsigned) regnum <= IA64_NAT127_REGNUM)
  2281.                          ? ia64_register_names[regnum] : "r??"),
  2282.                         paddress (gdbarch, *val));

  2283.   return 0;
  2284. }

  2285. /* Libunwind callback accessor function for top-level fp registers.  */
  2286. static int
  2287. ia64_access_rse_fpreg (unw_addr_space_t as, unw_regnum_t uw_regnum,
  2288.                        unw_fpreg_t *val, int write, void *arg)
  2289. {
  2290.   int regnum = ia64_uw2gdb_regnum (uw_regnum);
  2291.   struct regcache *regcache = arg;

  2292.   /* We never call any libunwind routines that need to write registers.  */
  2293.   gdb_assert (!write);

  2294.   regcache_cooked_read (regcache, regnum, (gdb_byte *) val);

  2295.   return 0;
  2296. }

  2297. /* Libunwind callback accessor function for accessing memory.  */
  2298. static int
  2299. ia64_access_mem (unw_addr_space_t as,
  2300.                  unw_word_t addr, unw_word_t *val,
  2301.                  int write, void *arg)
  2302. {
  2303.   if (addr - KERNEL_START < ktab_size)
  2304.     {
  2305.       unw_word_t *laddr = (unw_word_t*) ((char *) ktab
  2306.                           + (addr - KERNEL_START));

  2307.       if (write)
  2308.         *laddr = *val;
  2309.       else
  2310.         *val = *laddr;
  2311.       return 0;
  2312.     }

  2313.   /* XXX do we need to normalize byte-order here?  */
  2314.   if (write)
  2315.     return target_write_memory (addr, (gdb_byte *) val, sizeof (unw_word_t));
  2316.   else
  2317.     return target_read_memory (addr, (gdb_byte *) val, sizeof (unw_word_t));
  2318. }

  2319. /* Call low-level function to access the kernel unwind table.  */
  2320. static LONGEST
  2321. getunwind_table (gdb_byte **buf_p)
  2322. {
  2323.   LONGEST x;

  2324.   /* FIXME drow/2005-09-10: This code used to call
  2325.      ia64_linux_xfer_unwind_table directly to fetch the unwind table
  2326.      for the currently running ia64-linux kernel.  That data should
  2327.      come from the core file and be accessed via the auxv vector; if
  2328.      we want to preserve fall back to the running kernel's table, then
  2329.      we should find a way to override the corefile layer's
  2330.      xfer_partial method.  */

  2331.   x = target_read_alloc (&current_target, TARGET_OBJECT_UNWIND_TABLE,
  2332.                          NULL, buf_p);

  2333.   return x;
  2334. }

  2335. /* Get the kernel unwind table.  */
  2336. static int
  2337. get_kernel_table (unw_word_t ip, unw_dyn_info_t *di)
  2338. {
  2339.   static struct ia64_table_entry *etab;

  2340.   if (!ktab)
  2341.     {
  2342.       gdb_byte *ktab_buf;
  2343.       LONGEST size;

  2344.       size = getunwind_table (&ktab_buf);
  2345.       if (size <= 0)
  2346.         return -UNW_ENOINFO;

  2347.       ktab = (struct ia64_table_entry *) ktab_buf;
  2348.       ktab_size = size;

  2349.       for (etab = ktab; etab->start_offset; ++etab)
  2350.         etab->info_offset += KERNEL_START;
  2351.     }

  2352.   if (ip < ktab[0].start_offset || ip >= etab[-1].end_offset)
  2353.     return -UNW_ENOINFO;

  2354.   di->format = UNW_INFO_FORMAT_TABLE;
  2355.   di->gp = 0;
  2356.   di->start_ip = ktab[0].start_offset;
  2357.   di->end_ip = etab[-1].end_offset;
  2358.   di->u.ti.name_ptr = (unw_word_t) "<kernel>";
  2359.   di->u.ti.segbase = 0;
  2360.   di->u.ti.table_len = ((char *) etab - (char *) ktab) / sizeof (unw_word_t);
  2361.   di->u.ti.table_data = (unw_word_t *) ktab;

  2362.   if (gdbarch_debug >= 1)
  2363.     fprintf_unfiltered (gdb_stdlog, "get_kernel_table: found table `%s': "
  2364.                         "segbase=%s, length=%s, gp=%s\n",
  2365.                         (char *) di->u.ti.name_ptr,
  2366.                         hex_string (di->u.ti.segbase),
  2367.                         pulongest (di->u.ti.table_len),
  2368.                         hex_string (di->gp));
  2369.   return 0;
  2370. }

  2371. /* Find the unwind table entry for a specified address.  */
  2372. static int
  2373. ia64_find_unwind_table (struct objfile *objfile, unw_word_t ip,
  2374.                         unw_dyn_info_t *dip, void **buf)
  2375. {
  2376.   Elf_Internal_Phdr *phdr, *p_text = NULL, *p_unwind = NULL;
  2377.   Elf_Internal_Ehdr *ehdr;
  2378.   unw_word_t segbase = 0;
  2379.   CORE_ADDR load_base;
  2380.   bfd *bfd;
  2381.   int i;

  2382.   bfd = objfile->obfd;

  2383.   ehdr = elf_tdata (bfd)->elf_header;
  2384.   phdr = elf_tdata (bfd)->phdr;

  2385.   load_base = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));

  2386.   for (i = 0; i < ehdr->e_phnum; ++i)
  2387.     {
  2388.       switch (phdr[i].p_type)
  2389.         {
  2390.         case PT_LOAD:
  2391.           if ((unw_word_t) (ip - load_base - phdr[i].p_vaddr)
  2392.               < phdr[i].p_memsz)
  2393.             p_text = phdr + i;
  2394.           break;

  2395.         case PT_IA_64_UNWIND:
  2396.           p_unwind = phdr + i;
  2397.           break;

  2398.         default:
  2399.           break;
  2400.         }
  2401.     }

  2402.   if (!p_text || !p_unwind)
  2403.     return -UNW_ENOINFO;

  2404.   /* Verify that the segment that contains the IP also contains
  2405.      the static unwind table.  If not, we may be in the Linux kernel's
  2406.      DSO gate page in which case the unwind table is another segment.
  2407.      Otherwise, we are dealing with runtime-generated code, for which we
  2408.      have no info here.  */
  2409.   segbase = p_text->p_vaddr + load_base;

  2410.   if ((p_unwind->p_vaddr - p_text->p_vaddr) >= p_text->p_memsz)
  2411.     {
  2412.       int ok = 0;
  2413.       for (i = 0; i < ehdr->e_phnum; ++i)
  2414.         {
  2415.           if (phdr[i].p_type == PT_LOAD
  2416.               && (p_unwind->p_vaddr - phdr[i].p_vaddr) < phdr[i].p_memsz)
  2417.             {
  2418.               ok = 1;
  2419.               /* Get the segbase from the section containing the
  2420.                  libunwind table.  */
  2421.               segbase = phdr[i].p_vaddr + load_base;
  2422.             }
  2423.         }
  2424.       if (!ok)
  2425.         return -UNW_ENOINFO;
  2426.     }

  2427.   dip->start_ip = p_text->p_vaddr + load_base;
  2428.   dip->end_ip = dip->start_ip + p_text->p_memsz;
  2429.   dip->gp = ia64_find_global_pointer (get_objfile_arch (objfile), ip);
  2430.   dip->format = UNW_INFO_FORMAT_REMOTE_TABLE;
  2431.   dip->u.rti.name_ptr = (unw_word_t) bfd_get_filename (bfd);
  2432.   dip->u.rti.segbase = segbase;
  2433.   dip->u.rti.table_len = p_unwind->p_memsz / sizeof (unw_word_t);
  2434.   dip->u.rti.table_data = p_unwind->p_vaddr + load_base;

  2435.   return 0;
  2436. }

  2437. /* Libunwind callback accessor function to acquire procedure unwind-info.  */
  2438. static int
  2439. ia64_find_proc_info_x (unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
  2440.                        int need_unwind_info, void *arg)
  2441. {
  2442.   struct obj_section *sec = find_pc_section (ip);
  2443.   unw_dyn_info_t di;
  2444.   int ret;
  2445.   void *buf = NULL;

  2446.   if (!sec)
  2447.     {
  2448.       /* XXX This only works if the host and the target architecture are
  2449.          both ia64 and if the have (more or less) the same kernel
  2450.          version.  */
  2451.       if (get_kernel_table (ip, &di) < 0)
  2452.         return -UNW_ENOINFO;

  2453.       if (gdbarch_debug >= 1)
  2454.         fprintf_unfiltered (gdb_stdlog, "ia64_find_proc_info_x: %s -> "
  2455.                             "(name=`%s',segbase=%s,start=%s,end=%s,gp=%s,"
  2456.                             "length=%s,data=%s)\n",
  2457.                             hex_string (ip), (char *)di.u.ti.name_ptr,
  2458.                             hex_string (di.u.ti.segbase),
  2459.                             hex_string (di.start_ip), hex_string (di.end_ip),
  2460.                             hex_string (di.gp),
  2461.                             pulongest (di.u.ti.table_len),
  2462.                             hex_string ((CORE_ADDR)di.u.ti.table_data));
  2463.     }
  2464.   else
  2465.     {
  2466.       ret = ia64_find_unwind_table (sec->objfile, ip, &di, &buf);
  2467.       if (ret < 0)
  2468.         return ret;

  2469.       if (gdbarch_debug >= 1)
  2470.         fprintf_unfiltered (gdb_stdlog, "ia64_find_proc_info_x: %s -> "
  2471.                             "(name=`%s',segbase=%s,start=%s,end=%s,gp=%s,"
  2472.                             "length=%s,data=%s)\n",
  2473.                             hex_string (ip), (char *)di.u.rti.name_ptr,
  2474.                             hex_string (di.u.rti.segbase),
  2475.                             hex_string (di.start_ip), hex_string (di.end_ip),
  2476.                             hex_string (di.gp),
  2477.                             pulongest (di.u.rti.table_len),
  2478.                             hex_string (di.u.rti.table_data));
  2479.     }

  2480.   ret = libunwind_search_unwind_table (&as, ip, &di, pi, need_unwind_info,
  2481.                                        arg);

  2482.   /* We no longer need the dyn info storage so free it.  */
  2483.   xfree (buf);

  2484.   return ret;
  2485. }

  2486. /* Libunwind callback accessor function for cleanup.  */
  2487. static void
  2488. ia64_put_unwind_info (unw_addr_space_t as,
  2489.                       unw_proc_info_t *pip, void *arg)
  2490. {
  2491.   /* Nothing required for now.  */
  2492. }

  2493. /* Libunwind callback accessor function to get head of the dynamic
  2494.    unwind-info registration list.  */
  2495. static int
  2496. ia64_get_dyn_info_list (unw_addr_space_t as,
  2497.                         unw_word_t *dilap, void *arg)
  2498. {
  2499.   struct obj_section *text_sec;
  2500.   struct objfile *objfile;
  2501.   unw_word_t ip, addr;
  2502.   unw_dyn_info_t di;
  2503.   int ret;

  2504.   if (!libunwind_is_initialized ())
  2505.     return -UNW_ENOINFO;

  2506.   for (objfile = object_files; objfile; objfile = objfile->next)
  2507.     {
  2508.       void *buf = NULL;

  2509.       text_sec = objfile->sections + SECT_OFF_TEXT (objfile);
  2510.       ip = obj_section_addr (text_sec);
  2511.       ret = ia64_find_unwind_table (objfile, ip, &di, &buf);
  2512.       if (ret >= 0)
  2513.         {
  2514.           addr = libunwind_find_dyn_list (as, &di, arg);
  2515.           /* We no longer need the dyn info storage so free it.  */
  2516.           xfree (buf);

  2517.           if (addr)
  2518.             {
  2519.               if (gdbarch_debug >= 1)
  2520.                 fprintf_unfiltered (gdb_stdlog,
  2521.                                     "dynamic unwind table in objfile %s "
  2522.                                     "at %s (gp=%s)\n",
  2523.                                     bfd_get_filename (objfile->obfd),
  2524.                                     hex_string (addr), hex_string (di.gp));
  2525.               *dilap = addr;
  2526.               return 0;
  2527.             }
  2528.         }
  2529.     }
  2530.   return -UNW_ENOINFO;
  2531. }


  2532. /* Frame interface functions for libunwind.  */

  2533. static void
  2534. ia64_libunwind_frame_this_id (struct frame_info *this_frame, void **this_cache,
  2535.                               struct frame_id *this_id)
  2536. {
  2537.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2538.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2539.   struct frame_id id = outer_frame_id;
  2540.   gdb_byte buf[8];
  2541.   CORE_ADDR bsp;

  2542.   libunwind_frame_this_id (this_frame, this_cache, &id);
  2543.   if (frame_id_eq (id, outer_frame_id))
  2544.     {
  2545.       (*this_id) = outer_frame_id;
  2546.       return;
  2547.     }

  2548.   /* We must add the bsp as the special address for frame comparison
  2549.      purposes.  */
  2550.   get_frame_register (this_frame, IA64_BSP_REGNUM, buf);
  2551.   bsp = extract_unsigned_integer (buf, 8, byte_order);

  2552.   (*this_id) = frame_id_build_special (id.stack_addr, id.code_addr, bsp);

  2553.   if (gdbarch_debug >= 1)
  2554.     fprintf_unfiltered (gdb_stdlog,
  2555.                         "libunwind frame id: code %s, stack %s, "
  2556.                         "special %s, this_frame %s\n",
  2557.                         paddress (gdbarch, id.code_addr),
  2558.                         paddress (gdbarch, id.stack_addr),
  2559.                         paddress (gdbarch, bsp),
  2560.                         host_address_to_string (this_frame));
  2561. }

  2562. static struct value *
  2563. ia64_libunwind_frame_prev_register (struct frame_info *this_frame,
  2564.                                     void **this_cache, int regnum)
  2565. {
  2566.   int reg = regnum;
  2567.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2568.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2569.   struct value *val;

  2570.   if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
  2571.     reg = IA64_PR_REGNUM;
  2572.   else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
  2573.     reg = IA64_UNAT_REGNUM;

  2574.   /* Let libunwind do most of the work.  */
  2575.   val = libunwind_frame_prev_register (this_frame, this_cache, reg);

  2576.   if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
  2577.     {
  2578.       ULONGEST prN_val;

  2579.       if (VP16_REGNUM <= regnum && regnum <= VP63_REGNUM)
  2580.         {
  2581.           int rrb_pr = 0;
  2582.           ULONGEST cfm;
  2583.           gdb_byte buf[MAX_REGISTER_SIZE];

  2584.           /* Fetch predicate register rename base from current frame
  2585.              marker for this frame.  */
  2586.           get_frame_register (this_frame, IA64_CFM_REGNUM, buf);
  2587.           cfm = extract_unsigned_integer (buf, 8, byte_order);
  2588.           rrb_pr = (cfm >> 32) & 0x3f;

  2589.           /* Adjust the register number to account for register rotation.  */
  2590.           regnum = VP16_REGNUM + ((regnum - VP16_REGNUM) + rrb_pr) % 48;
  2591.         }
  2592.       prN_val = extract_bit_field (value_contents_all (val),
  2593.                                    regnum - VP0_REGNUM, 1);
  2594.       return frame_unwind_got_constant (this_frame, regnum, prN_val);
  2595.     }

  2596.   else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
  2597.     {
  2598.       ULONGEST unatN_val;

  2599.       unatN_val = extract_bit_field (value_contents_all (val),
  2600.                                      regnum - IA64_NAT0_REGNUM, 1);
  2601.       return frame_unwind_got_constant (this_frame, regnum, unatN_val);
  2602.     }

  2603.   else if (regnum == IA64_BSP_REGNUM)
  2604.     {
  2605.       struct value *cfm_val;
  2606.       CORE_ADDR prev_bsp, prev_cfm;

  2607.       /* We want to calculate the previous bsp as the end of the previous
  2608.          register stack frame.  This corresponds to what the hardware bsp
  2609.          register will be if we pop the frame back which is why we might
  2610.          have been called.  We know that libunwind will pass us back the
  2611.          beginning of the current frame so we should just add sof to it.  */
  2612.       prev_bsp = extract_unsigned_integer (value_contents_all (val),
  2613.                                            8, byte_order);
  2614.       cfm_val = libunwind_frame_prev_register (this_frame, this_cache,
  2615.                                                IA64_CFM_REGNUM);
  2616.       prev_cfm = extract_unsigned_integer (value_contents_all (cfm_val),
  2617.                                            8, byte_order);
  2618.       prev_bsp = rse_address_add (prev_bsp, (prev_cfm & 0x7f));

  2619.       return frame_unwind_got_constant (this_frame, regnum, prev_bsp);
  2620.     }
  2621.   else
  2622.     return val;
  2623. }

  2624. static int
  2625. ia64_libunwind_frame_sniffer (const struct frame_unwind *self,
  2626.                               struct frame_info *this_frame,
  2627.                               void **this_cache)
  2628. {
  2629.   if (libunwind_is_initialized ()
  2630.       && libunwind_frame_sniffer (self, this_frame, this_cache))
  2631.     return 1;

  2632.   return 0;
  2633. }

  2634. static const struct frame_unwind ia64_libunwind_frame_unwind =
  2635. {
  2636.   NORMAL_FRAME,
  2637.   default_frame_unwind_stop_reason,
  2638.   ia64_libunwind_frame_this_id,
  2639.   ia64_libunwind_frame_prev_register,
  2640.   NULL,
  2641.   ia64_libunwind_frame_sniffer,
  2642.   libunwind_frame_dealloc_cache
  2643. };

  2644. static void
  2645. ia64_libunwind_sigtramp_frame_this_id (struct frame_info *this_frame,
  2646.                                        void **this_cache,
  2647.                                        struct frame_id *this_id)
  2648. {
  2649.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2650.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2651.   gdb_byte buf[8];
  2652.   CORE_ADDR bsp;
  2653.   struct frame_id id = outer_frame_id;
  2654.   CORE_ADDR prev_ip;

  2655.   libunwind_frame_this_id (this_frame, this_cache, &id);
  2656.   if (frame_id_eq (id, outer_frame_id))
  2657.     {
  2658.       (*this_id) = outer_frame_id;
  2659.       return;
  2660.     }

  2661.   /* We must add the bsp as the special address for frame comparison
  2662.      purposes.  */
  2663.   get_frame_register (this_frame, IA64_BSP_REGNUM, buf);
  2664.   bsp = extract_unsigned_integer (buf, 8, byte_order);

  2665.   /* For a sigtramp frame, we don't make the check for previous ip being 0.  */
  2666.   (*this_id) = frame_id_build_special (id.stack_addr, id.code_addr, bsp);

  2667.   if (gdbarch_debug >= 1)
  2668.     fprintf_unfiltered (gdb_stdlog,
  2669.                         "libunwind sigtramp frame id: code %s, "
  2670.                         "stack %s, special %s, this_frame %s\n",
  2671.                         paddress (gdbarch, id.code_addr),
  2672.                         paddress (gdbarch, id.stack_addr),
  2673.                         paddress (gdbarch, bsp),
  2674.                         host_address_to_string (this_frame));
  2675. }

  2676. static struct value *
  2677. ia64_libunwind_sigtramp_frame_prev_register (struct frame_info *this_frame,
  2678.                                              void **this_cache, int regnum)
  2679. {
  2680.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2681.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2682.   struct value *prev_ip_val;
  2683.   CORE_ADDR prev_ip;

  2684.   /* If the previous frame pc value is 0, then we want to use the SIGCONTEXT
  2685.      method of getting previous registers.  */
  2686.   prev_ip_val = libunwind_frame_prev_register (this_frame, this_cache,
  2687.                                                IA64_IP_REGNUM);
  2688.   prev_ip = extract_unsigned_integer (value_contents_all (prev_ip_val),
  2689.                                       8, byte_order);

  2690.   if (prev_ip == 0)
  2691.     {
  2692.       void *tmp_cache = NULL;
  2693.       return ia64_sigtramp_frame_prev_register (this_frame, &tmp_cache,
  2694.                                                 regnum);
  2695.     }
  2696.   else
  2697.     return ia64_libunwind_frame_prev_register (this_frame, this_cache, regnum);
  2698. }

  2699. static int
  2700. ia64_libunwind_sigtramp_frame_sniffer (const struct frame_unwind *self,
  2701.                                        struct frame_info *this_frame,
  2702.                                        void **this_cache)
  2703. {
  2704.   if (libunwind_is_initialized ())
  2705.     {
  2706.       if (libunwind_sigtramp_frame_sniffer (self, this_frame, this_cache))
  2707.         return 1;
  2708.       return 0;
  2709.     }
  2710.   else
  2711.     return ia64_sigtramp_frame_sniffer (self, this_frame, this_cache);
  2712. }

  2713. static const struct frame_unwind ia64_libunwind_sigtramp_frame_unwind =
  2714. {
  2715.   SIGTRAMP_FRAME,
  2716.   default_frame_unwind_stop_reason,
  2717.   ia64_libunwind_sigtramp_frame_this_id,
  2718.   ia64_libunwind_sigtramp_frame_prev_register,
  2719.   NULL,
  2720.   ia64_libunwind_sigtramp_frame_sniffer
  2721. };

  2722. /* Set of libunwind callback acccessor functions.  */
  2723. unw_accessors_t ia64_unw_accessors =
  2724. {
  2725.   ia64_find_proc_info_x,
  2726.   ia64_put_unwind_info,
  2727.   ia64_get_dyn_info_list,
  2728.   ia64_access_mem,
  2729.   ia64_access_reg,
  2730.   ia64_access_fpreg,
  2731.   /* resume */
  2732.   /* get_proc_name */
  2733. };

  2734. /* Set of special libunwind callback acccessor functions specific for accessing
  2735.    the rse registers.  At the top of the stack, we want libunwind to figure out
  2736.    how to read r32 - r127.  Though usually they are found sequentially in
  2737.    memory starting from $bof, this is not always true.  */
  2738. unw_accessors_t ia64_unw_rse_accessors =
  2739. {
  2740.   ia64_find_proc_info_x,
  2741.   ia64_put_unwind_info,
  2742.   ia64_get_dyn_info_list,
  2743.   ia64_access_mem,
  2744.   ia64_access_rse_reg,
  2745.   ia64_access_rse_fpreg,
  2746.   /* resume */
  2747.   /* get_proc_name */
  2748. };

  2749. /* Set of ia64-libunwind-tdep gdb callbacks and data for generic
  2750.    ia64-libunwind-tdep code to use.  */
  2751. struct libunwind_descr ia64_libunwind_descr =
  2752. {
  2753.   ia64_gdb2uw_regnum,
  2754.   ia64_uw2gdb_regnum,
  2755.   ia64_is_fpreg,
  2756.   &ia64_unw_accessors,
  2757.   &ia64_unw_rse_accessors,
  2758. };

  2759. #endif /* HAVE_LIBUNWIND_IA64_H  */

  2760. static int
  2761. ia64_use_struct_convention (struct type *type)
  2762. {
  2763.   struct type *float_elt_type;

  2764.   /* Don't use the struct convention for anything but structure,
  2765.      union, or array types.  */
  2766.   if (!(TYPE_CODE (type) == TYPE_CODE_STRUCT
  2767.         || TYPE_CODE (type) == TYPE_CODE_UNION
  2768.         || TYPE_CODE (type) == TYPE_CODE_ARRAY))
  2769.     return 0;

  2770.   /* HFAs are structures (or arrays) consisting entirely of floating
  2771.      point values of the same length.  Up to 8 of these are returned
  2772.      in registers.  Don't use the struct convention when this is the
  2773.      case.  */
  2774.   float_elt_type = is_float_or_hfa_type (type);
  2775.   if (float_elt_type != NULL
  2776.       && TYPE_LENGTH (type) / TYPE_LENGTH (float_elt_type) <= 8)
  2777.     return 0;

  2778.   /* Other structs of length 32 or less are returned in r8-r11.
  2779.      Don't use the struct convention for those either.  */
  2780.   return TYPE_LENGTH (type) > 32;
  2781. }

  2782. /* Return non-zero if TYPE is a structure or union type.  */

  2783. static int
  2784. ia64_struct_type_p (const struct type *type)
  2785. {
  2786.   return (TYPE_CODE (type) == TYPE_CODE_STRUCT
  2787.           || TYPE_CODE (type) == TYPE_CODE_UNION);
  2788. }

  2789. static void
  2790. ia64_extract_return_value (struct type *type, struct regcache *regcache,
  2791.                            gdb_byte *valbuf)
  2792. {
  2793.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  2794.   struct type *float_elt_type;

  2795.   float_elt_type = is_float_or_hfa_type (type);
  2796.   if (float_elt_type != NULL)
  2797.     {
  2798.       gdb_byte from[MAX_REGISTER_SIZE];
  2799.       int offset = 0;
  2800.       int regnum = IA64_FR8_REGNUM;
  2801.       int n = TYPE_LENGTH (type) / TYPE_LENGTH (float_elt_type);

  2802.       while (n-- > 0)
  2803.         {
  2804.           regcache_cooked_read (regcache, regnum, from);
  2805.           convert_typed_floating (from, ia64_ext_type (gdbarch),
  2806.                                   (char *)valbuf + offset, float_elt_type);
  2807.           offset += TYPE_LENGTH (float_elt_type);
  2808.           regnum++;
  2809.         }
  2810.     }
  2811.   else if (!ia64_struct_type_p (type) && TYPE_LENGTH (type) < 8)
  2812.     {
  2813.       /* This is an integral value, and its size is less than 8 bytes.
  2814.          These values are LSB-aligned, so extract the relevant bytes,
  2815.          and copy them into VALBUF.  */
  2816.       /* brobecker/2005-12-30: Actually, all integral values are LSB aligned,
  2817.          so I suppose we should also add handling here for integral values
  2818.          whose size is greater than 8.  But I wasn't able to create such
  2819.          a type, neither in C nor in Ada, so not worrying about these yet.  */
  2820.       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2821.       ULONGEST val;

  2822.       regcache_cooked_read_unsigned (regcache, IA64_GR8_REGNUM, &val);
  2823.       store_unsigned_integer (valbuf, TYPE_LENGTH (type), byte_order, val);
  2824.     }
  2825.   else
  2826.     {
  2827.       ULONGEST val;
  2828.       int offset = 0;
  2829.       int regnum = IA64_GR8_REGNUM;
  2830.       int reglen = TYPE_LENGTH (register_type (gdbarch, IA64_GR8_REGNUM));
  2831.       int n = TYPE_LENGTH (type) / reglen;
  2832.       int m = TYPE_LENGTH (type) % reglen;

  2833.       while (n-- > 0)
  2834.         {
  2835.           ULONGEST val;
  2836.           regcache_cooked_read_unsigned (regcache, regnum, &val);
  2837.           memcpy ((char *)valbuf + offset, &val, reglen);
  2838.           offset += reglen;
  2839.           regnum++;
  2840.         }

  2841.       if (m)
  2842.         {
  2843.           regcache_cooked_read_unsigned (regcache, regnum, &val);
  2844.           memcpy ((char *)valbuf + offset, &val, m);
  2845.         }
  2846.     }
  2847. }

  2848. static void
  2849. ia64_store_return_value (struct type *type, struct regcache *regcache,
  2850.                          const gdb_byte *valbuf)
  2851. {
  2852.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  2853.   struct type *float_elt_type;

  2854.   float_elt_type = is_float_or_hfa_type (type);
  2855.   if (float_elt_type != NULL)
  2856.     {
  2857.       gdb_byte to[MAX_REGISTER_SIZE];
  2858.       int offset = 0;
  2859.       int regnum = IA64_FR8_REGNUM;
  2860.       int n = TYPE_LENGTH (type) / TYPE_LENGTH (float_elt_type);

  2861.       while (n-- > 0)
  2862.         {
  2863.           convert_typed_floating ((char *)valbuf + offset, float_elt_type,
  2864.                                   to, ia64_ext_type (gdbarch));
  2865.           regcache_cooked_write (regcache, regnum, to);
  2866.           offset += TYPE_LENGTH (float_elt_type);
  2867.           regnum++;
  2868.         }
  2869.     }
  2870.   else
  2871.     {
  2872.       ULONGEST val;
  2873.       int offset = 0;
  2874.       int regnum = IA64_GR8_REGNUM;
  2875.       int reglen = TYPE_LENGTH (register_type (gdbarch, IA64_GR8_REGNUM));
  2876.       int n = TYPE_LENGTH (type) / reglen;
  2877.       int m = TYPE_LENGTH (type) % reglen;

  2878.       while (n-- > 0)
  2879.         {
  2880.           ULONGEST val;
  2881.           memcpy (&val, (char *)valbuf + offset, reglen);
  2882.           regcache_cooked_write_unsigned (regcache, regnum, val);
  2883.           offset += reglen;
  2884.           regnum++;
  2885.         }

  2886.       if (m)
  2887.         {
  2888.           memcpy (&val, (char *)valbuf + offset, m);
  2889.           regcache_cooked_write_unsigned (regcache, regnum, val);
  2890.         }
  2891.     }
  2892. }

  2893. static enum return_value_convention
  2894. ia64_return_value (struct gdbarch *gdbarch, struct value *function,
  2895.                    struct type *valtype, struct regcache *regcache,
  2896.                    gdb_byte *readbuf, const gdb_byte *writebuf)
  2897. {
  2898.   int struct_return = ia64_use_struct_convention (valtype);

  2899.   if (writebuf != NULL)
  2900.     {
  2901.       gdb_assert (!struct_return);
  2902.       ia64_store_return_value (valtype, regcache, writebuf);
  2903.     }

  2904.   if (readbuf != NULL)
  2905.     {
  2906.       gdb_assert (!struct_return);
  2907.       ia64_extract_return_value (valtype, regcache, readbuf);
  2908.     }

  2909.   if (struct_return)
  2910.     return RETURN_VALUE_STRUCT_CONVENTION;
  2911.   else
  2912.     return RETURN_VALUE_REGISTER_CONVENTION;
  2913. }

  2914. static int
  2915. is_float_or_hfa_type_recurse (struct type *t, struct type **etp)
  2916. {
  2917.   switch (TYPE_CODE (t))
  2918.     {
  2919.     case TYPE_CODE_FLT:
  2920.       if (*etp)
  2921.         return TYPE_LENGTH (*etp) == TYPE_LENGTH (t);
  2922.       else
  2923.         {
  2924.           *etp = t;
  2925.           return 1;
  2926.         }
  2927.       break;
  2928.     case TYPE_CODE_ARRAY:
  2929.       return
  2930.         is_float_or_hfa_type_recurse (check_typedef (TYPE_TARGET_TYPE (t)),
  2931.                                       etp);
  2932.       break;
  2933.     case TYPE_CODE_STRUCT:
  2934.       {
  2935.         int i;

  2936.         for (i = 0; i < TYPE_NFIELDS (t); i++)
  2937.           if (!is_float_or_hfa_type_recurse
  2938.               (check_typedef (TYPE_FIELD_TYPE (t, i)), etp))
  2939.             return 0;
  2940.         return 1;
  2941.       }
  2942.       break;
  2943.     default:
  2944.       return 0;
  2945.       break;
  2946.     }
  2947. }

  2948. /* Determine if the given type is one of the floating point types or
  2949.    and HFA (which is a struct, array, or combination thereof whose
  2950.    bottom-most elements are all of the same floating point type).  */

  2951. static struct type *
  2952. is_float_or_hfa_type (struct type *t)
  2953. {
  2954.   struct type *et = 0;

  2955.   return is_float_or_hfa_type_recurse (t, &et) ? et : 0;
  2956. }


  2957. /* Return 1 if the alignment of T is such that the next even slot
  2958.    should be used.  Return 0, if the next available slot should
  2959.    be used.  (See section 8.5.1 of the IA-64 Software Conventions
  2960.    and Runtime manual).  */

  2961. static int
  2962. slot_alignment_is_next_even (struct type *t)
  2963. {
  2964.   switch (TYPE_CODE (t))
  2965.     {
  2966.     case TYPE_CODE_INT:
  2967.     case TYPE_CODE_FLT:
  2968.       if (TYPE_LENGTH (t) > 8)
  2969.         return 1;
  2970.       else
  2971.         return 0;
  2972.     case TYPE_CODE_ARRAY:
  2973.       return
  2974.         slot_alignment_is_next_even (check_typedef (TYPE_TARGET_TYPE (t)));
  2975.     case TYPE_CODE_STRUCT:
  2976.       {
  2977.         int i;

  2978.         for (i = 0; i < TYPE_NFIELDS (t); i++)
  2979.           if (slot_alignment_is_next_even
  2980.               (check_typedef (TYPE_FIELD_TYPE (t, i))))
  2981.             return 1;
  2982.         return 0;
  2983.       }
  2984.     default:
  2985.       return 0;
  2986.     }
  2987. }

  2988. /* Attempt to find (and return) the global pointer for the given
  2989.    function.

  2990.    This is a rather nasty bit of code searchs for the .dynamic section
  2991.    in the objfile corresponding to the pc of the function we're trying
  2992.    to call.  Once it finds the addresses at which the .dynamic section
  2993.    lives in the child process, it scans the Elf64_Dyn entries for a
  2994.    DT_PLTGOT tag.  If it finds one of these, the corresponding
  2995.    d_un.d_ptr value is the global pointer.  */

  2996. static CORE_ADDR
  2997. ia64_find_global_pointer_from_dynamic_section (struct gdbarch *gdbarch,
  2998.                                                CORE_ADDR faddr)
  2999. {
  3000.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  3001.   struct obj_section *faddr_sect;

  3002.   faddr_sect = find_pc_section (faddr);
  3003.   if (faddr_sect != NULL)
  3004.     {
  3005.       struct obj_section *osect;

  3006.       ALL_OBJFILE_OSECTIONS (faddr_sect->objfile, osect)
  3007.         {
  3008.           if (strcmp (osect->the_bfd_section->name, ".dynamic") == 0)
  3009.             break;
  3010.         }

  3011.       if (osect < faddr_sect->objfile->sections_end)
  3012.         {
  3013.           CORE_ADDR addr, endaddr;

  3014.           addr = obj_section_addr (osect);
  3015.           endaddr = obj_section_endaddr (osect);

  3016.           while (addr < endaddr)
  3017.             {
  3018.               int status;
  3019.               LONGEST tag;
  3020.               gdb_byte buf[8];

  3021.               status = target_read_memory (addr, buf, sizeof (buf));
  3022.               if (status != 0)
  3023.                 break;
  3024.               tag = extract_signed_integer (buf, sizeof (buf), byte_order);

  3025.               if (tag == DT_PLTGOT)
  3026.                 {
  3027.                   CORE_ADDR global_pointer;

  3028.                   status = target_read_memory (addr + 8, buf, sizeof (buf));
  3029.                   if (status != 0)
  3030.                     break;
  3031.                   global_pointer = extract_unsigned_integer (buf, sizeof (buf),
  3032.                                                              byte_order);

  3033.                   /* The payoff...  */
  3034.                   return global_pointer;
  3035.                 }

  3036.               if (tag == DT_NULL)
  3037.                 break;

  3038.               addr += 16;
  3039.             }
  3040.         }
  3041.     }
  3042.   return 0;
  3043. }

  3044. /* Attempt to find (and return) the global pointer for the given
  3045.    function.  We first try the find_global_pointer_from_solib routine
  3046.    from the gdbarch tdep vector, if provided.  And if that does not
  3047.    work, then we try ia64_find_global_pointer_from_dynamic_section.  */

  3048. static CORE_ADDR
  3049. ia64_find_global_pointer (struct gdbarch *gdbarch, CORE_ADDR faddr)
  3050. {
  3051.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  3052.   CORE_ADDR addr = 0;

  3053.   if (tdep->find_global_pointer_from_solib)
  3054.     addr = tdep->find_global_pointer_from_solib (gdbarch, faddr);
  3055.   if (addr == 0)
  3056.     addr = ia64_find_global_pointer_from_dynamic_section (gdbarch, faddr);
  3057.   return addr;
  3058. }

  3059. /* Given a function's address, attempt to find (and return) the
  3060.    corresponding (canonical) function descriptor.  Return 0 if
  3061.    not found.  */
  3062. static CORE_ADDR
  3063. find_extant_func_descr (struct gdbarch *gdbarch, CORE_ADDR faddr)
  3064. {
  3065.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  3066.   struct obj_section *faddr_sect;

  3067.   /* Return early if faddr is already a function descriptor.  */
  3068.   faddr_sect = find_pc_section (faddr);
  3069.   if (faddr_sect && strcmp (faddr_sect->the_bfd_section->name, ".opd") == 0)
  3070.     return faddr;

  3071.   if (faddr_sect != NULL)
  3072.     {
  3073.       struct obj_section *osect;
  3074.       ALL_OBJFILE_OSECTIONS (faddr_sect->objfile, osect)
  3075.         {
  3076.           if (strcmp (osect->the_bfd_section->name, ".opd") == 0)
  3077.             break;
  3078.         }

  3079.       if (osect < faddr_sect->objfile->sections_end)
  3080.         {
  3081.           CORE_ADDR addr, endaddr;

  3082.           addr = obj_section_addr (osect);
  3083.           endaddr = obj_section_endaddr (osect);

  3084.           while (addr < endaddr)
  3085.             {
  3086.               int status;
  3087.               LONGEST faddr2;
  3088.               gdb_byte buf[8];

  3089.               status = target_read_memory (addr, buf, sizeof (buf));
  3090.               if (status != 0)
  3091.                 break;
  3092.               faddr2 = extract_signed_integer (buf, sizeof (buf), byte_order);

  3093.               if (faddr == faddr2)
  3094.                 return addr;

  3095.               addr += 16;
  3096.             }
  3097.         }
  3098.     }
  3099.   return 0;
  3100. }

  3101. /* Attempt to find a function descriptor corresponding to the
  3102.    given address.  If none is found, construct one on the
  3103.    stack using the address at fdaptr.  */

  3104. static CORE_ADDR
  3105. find_func_descr (struct regcache *regcache, CORE_ADDR faddr, CORE_ADDR *fdaptr)
  3106. {
  3107.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  3108.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  3109.   CORE_ADDR fdesc;

  3110.   fdesc = find_extant_func_descr (gdbarch, faddr);

  3111.   if (fdesc == 0)
  3112.     {
  3113.       ULONGEST global_pointer;
  3114.       gdb_byte buf[16];

  3115.       fdesc = *fdaptr;
  3116.       *fdaptr += 16;

  3117.       global_pointer = ia64_find_global_pointer (gdbarch, faddr);

  3118.       if (global_pointer == 0)
  3119.         regcache_cooked_read_unsigned (regcache,
  3120.                                        IA64_GR1_REGNUM, &global_pointer);

  3121.       store_unsigned_integer (buf, 8, byte_order, faddr);
  3122.       store_unsigned_integer (buf + 8, 8, byte_order, global_pointer);

  3123.       write_memory (fdesc, buf, 16);
  3124.     }

  3125.   return fdesc;
  3126. }

  3127. /* Use the following routine when printing out function pointers
  3128.    so the user can see the function address rather than just the
  3129.    function descriptor.  */
  3130. static CORE_ADDR
  3131. ia64_convert_from_func_ptr_addr (struct gdbarch *gdbarch, CORE_ADDR addr,
  3132.                                  struct target_ops *targ)
  3133. {
  3134.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  3135.   struct obj_section *s;
  3136.   gdb_byte buf[8];

  3137.   s = find_pc_section (addr);

  3138.   /* check if ADDR points to a function descriptor.  */
  3139.   if (s && strcmp (s->the_bfd_section->name, ".opd") == 0)
  3140.     return read_memory_unsigned_integer (addr, 8, byte_order);

  3141.   /* Normally, functions live inside a section that is executable.
  3142.      So, if ADDR points to a non-executable section, then treat it
  3143.      as a function descriptor and return the target address iff
  3144.      the target address itself points to a section that is executable.
  3145.      Check first the memory of the whole length of 8 bytes is readable.  */
  3146.   if (s && (s->the_bfd_section->flags & SEC_CODE) == 0
  3147.       && target_read_memory (addr, buf, 8) == 0)
  3148.     {
  3149.       CORE_ADDR pc = extract_unsigned_integer (buf, 8, byte_order);
  3150.       struct obj_section *pc_section = find_pc_section (pc);

  3151.       if (pc_section && (pc_section->the_bfd_section->flags & SEC_CODE))
  3152.         return pc;
  3153.     }

  3154.   /* There are also descriptors embedded in vtables.  */
  3155.   if (s)
  3156.     {
  3157.       struct bound_minimal_symbol minsym;

  3158.       minsym = lookup_minimal_symbol_by_pc (addr);

  3159.       if (minsym.minsym
  3160.           && is_vtable_name (MSYMBOL_LINKAGE_NAME (minsym.minsym)))
  3161.         return read_memory_unsigned_integer (addr, 8, byte_order);
  3162.     }

  3163.   return addr;
  3164. }

  3165. static CORE_ADDR
  3166. ia64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  3167. {
  3168.   return sp & ~0xfLL;
  3169. }

  3170. /* The default "allocate_new_rse_frame" ia64_infcall_ops routine for ia64.  */

  3171. static void
  3172. ia64_allocate_new_rse_frame (struct regcache *regcache, ULONGEST bsp, int sof)
  3173. {
  3174.   ULONGEST cfm, pfs, new_bsp;

  3175.   regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);

  3176.   new_bsp = rse_address_add (bsp, sof);
  3177.   regcache_cooked_write_unsigned (regcache, IA64_BSP_REGNUM, new_bsp);

  3178.   regcache_cooked_read_unsigned (regcache, IA64_PFS_REGNUM, &pfs);
  3179.   pfs &= 0xc000000000000000LL;
  3180.   pfs |= (cfm & 0xffffffffffffLL);
  3181.   regcache_cooked_write_unsigned (regcache, IA64_PFS_REGNUM, pfs);

  3182.   cfm &= 0xc000000000000000LL;
  3183.   cfm |= sof;
  3184.   regcache_cooked_write_unsigned (regcache, IA64_CFM_REGNUM, cfm);
  3185. }

  3186. /* The default "store_argument_in_slot" ia64_infcall_ops routine for
  3187.    ia64.  */

  3188. static void
  3189. ia64_store_argument_in_slot (struct regcache *regcache, CORE_ADDR bsp,
  3190.                              int slotnum, gdb_byte *buf)
  3191. {
  3192.   write_memory (rse_address_add (bsp, slotnum), buf, 8);
  3193. }

  3194. /* The default "set_function_addr" ia64_infcall_ops routine for ia64.  */

  3195. static void
  3196. ia64_set_function_addr (struct regcache *regcache, CORE_ADDR func_addr)
  3197. {
  3198.   /* Nothing needed.  */
  3199. }

  3200. static CORE_ADDR
  3201. ia64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  3202.                       struct regcache *regcache, CORE_ADDR bp_addr,
  3203.                       int nargs, struct value **args, CORE_ADDR sp,
  3204.                       int struct_return, CORE_ADDR struct_addr)
  3205. {
  3206.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  3207.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  3208.   int argno;
  3209.   struct value *arg;
  3210.   struct type *type;
  3211.   int len, argoffset;
  3212.   int nslots, rseslots, memslots, slotnum, nfuncargs;
  3213.   int floatreg;
  3214.   ULONGEST bsp;
  3215.   CORE_ADDR funcdescaddr, pc, global_pointer;
  3216.   CORE_ADDR func_addr = find_function_addr (function, NULL);

  3217.   nslots = 0;
  3218.   nfuncargs = 0;
  3219.   /* Count the number of slots needed for the arguments.  */
  3220.   for (argno = 0; argno < nargs; argno++)
  3221.     {
  3222.       arg = args[argno];
  3223.       type = check_typedef (value_type (arg));
  3224.       len = TYPE_LENGTH (type);

  3225.       if ((nslots & 1) && slot_alignment_is_next_even (type))
  3226.         nslots++;

  3227.       if (TYPE_CODE (type) == TYPE_CODE_FUNC)
  3228.         nfuncargs++;

  3229.       nslots += (len + 7) / 8;
  3230.     }

  3231.   /* Divvy up the slots between the RSE and the memory stack.  */
  3232.   rseslots = (nslots > 8) ? 8 : nslots;
  3233.   memslots = nslots - rseslots;

  3234.   /* Allocate a new RSE frame.  */
  3235.   regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
  3236.   tdep->infcall_ops.allocate_new_rse_frame (regcache, bsp, rseslots);

  3237.   /* We will attempt to find function descriptors in the .opd segment,
  3238.      but if we can't we'll construct them ourselves.  That being the
  3239.      case, we'll need to reserve space on the stack for them.  */
  3240.   funcdescaddr = sp - nfuncargs * 16;
  3241.   funcdescaddr &= ~0xfLL;

  3242.   /* Adjust the stack pointer to it's new value.  The calling conventions
  3243.      require us to have 16 bytes of scratch, plus whatever space is
  3244.      necessary for the memory slots and our function descriptors.  */
  3245.   sp = sp - 16 - (memslots + nfuncargs) * 8;
  3246.   sp &= ~0xfLL;                                /* Maintain 16 byte alignment.  */

  3247.   /* Place the arguments where they belong.  The arguments will be
  3248.      either placed in the RSE backing store or on the memory stack.
  3249.      In addition, floating point arguments or HFAs are placed in
  3250.      floating point registers.  */
  3251.   slotnum = 0;
  3252.   floatreg = IA64_FR8_REGNUM;
  3253.   for (argno = 0; argno < nargs; argno++)
  3254.     {
  3255.       struct type *float_elt_type;

  3256.       arg = args[argno];
  3257.       type = check_typedef (value_type (arg));
  3258.       len = TYPE_LENGTH (type);

  3259.       /* Special handling for function parameters.  */
  3260.       if (len == 8
  3261.           && TYPE_CODE (type) == TYPE_CODE_PTR
  3262.           && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC)
  3263.         {
  3264.           gdb_byte val_buf[8];
  3265.           ULONGEST faddr = extract_unsigned_integer (value_contents (arg),
  3266.                                                      8, byte_order);
  3267.           store_unsigned_integer (val_buf, 8, byte_order,
  3268.                                   find_func_descr (regcache, faddr,
  3269.                                                    &funcdescaddr));
  3270.           if (slotnum < rseslots)
  3271.             tdep->infcall_ops.store_argument_in_slot (regcache, bsp,
  3272.                                                       slotnum, val_buf);
  3273.           else
  3274.             write_memory (sp + 16 + 8 * (slotnum - rseslots), val_buf, 8);
  3275.           slotnum++;
  3276.           continue;
  3277.         }

  3278.       /* Normal slots.  */

  3279.       /* Skip odd slot if necessary...  */
  3280.       if ((slotnum & 1) && slot_alignment_is_next_even (type))
  3281.         slotnum++;

  3282.       argoffset = 0;
  3283.       while (len > 0)
  3284.         {
  3285.           gdb_byte val_buf[8];

  3286.           memset (val_buf, 0, 8);
  3287.           if (!ia64_struct_type_p (type) && len < 8)
  3288.             {
  3289.               /* Integral types are LSB-aligned, so we have to be careful
  3290.                  to insert the argument on the correct side of the buffer.
  3291.                  This is why we use store_unsigned_integer.  */
  3292.               store_unsigned_integer
  3293.                 (val_buf, 8, byte_order,
  3294.                  extract_unsigned_integer (value_contents (arg), len,
  3295.                                            byte_order));
  3296.             }
  3297.           else
  3298.             {
  3299.               /* This is either an 8bit integral type, or an aggregate.
  3300.                  For 8bit integral type, there is no problem, we just
  3301.                  copy the value over.

  3302.                  For aggregates, the only potentially tricky portion
  3303.                  is to write the last one if it is less than 8 bytes.
  3304.                  In this case, the data is Byte0-aligned.  Happy news,
  3305.                  this means that we don't need to differentiate the
  3306.                  handling of 8byte blocks and less-than-8bytes blocks.  */
  3307.               memcpy (val_buf, value_contents (arg) + argoffset,
  3308.                       (len > 8) ? 8 : len);
  3309.             }

  3310.           if (slotnum < rseslots)
  3311.             tdep->infcall_ops.store_argument_in_slot (regcache, bsp,
  3312.                                                       slotnum, val_buf);
  3313.           else
  3314.             write_memory (sp + 16 + 8 * (slotnum - rseslots), val_buf, 8);

  3315.           argoffset += 8;
  3316.           len -= 8;
  3317.           slotnum++;
  3318.         }

  3319.       /* Handle floating point types (including HFAs).  */
  3320.       float_elt_type = is_float_or_hfa_type (type);
  3321.       if (float_elt_type != NULL)
  3322.         {
  3323.           argoffset = 0;
  3324.           len = TYPE_LENGTH (type);
  3325.           while (len > 0 && floatreg < IA64_FR16_REGNUM)
  3326.             {
  3327.               char to[MAX_REGISTER_SIZE];
  3328.               convert_typed_floating (value_contents (arg) + argoffset,
  3329.                                       float_elt_type, to,
  3330.                                       ia64_ext_type (gdbarch));
  3331.               regcache_cooked_write (regcache, floatreg, (void *)to);
  3332.               floatreg++;
  3333.               argoffset += TYPE_LENGTH (float_elt_type);
  3334.               len -= TYPE_LENGTH (float_elt_type);
  3335.             }
  3336.         }
  3337.     }

  3338.   /* Store the struct return value in r8 if necessary.  */
  3339.   if (struct_return)
  3340.     {
  3341.       regcache_cooked_write_unsigned (regcache, IA64_GR8_REGNUM,
  3342.                                       (ULONGEST) struct_addr);
  3343.     }

  3344.   global_pointer = ia64_find_global_pointer (gdbarch, func_addr);

  3345.   if (global_pointer != 0)
  3346.     regcache_cooked_write_unsigned (regcache, IA64_GR1_REGNUM, global_pointer);

  3347.   /* The following is not necessary on HP-UX, because we're using
  3348.      a dummy code sequence pushed on the stack to make the call, and
  3349.      this sequence doesn't need b0 to be set in order for our dummy
  3350.      breakpoint to be hit.  Nonetheless, this doesn't interfere, and
  3351.      it's needed for other OSes, so we do this unconditionaly.  */
  3352.   regcache_cooked_write_unsigned (regcache, IA64_BR0_REGNUM, bp_addr);

  3353.   regcache_cooked_write_unsigned (regcache, sp_regnum, sp);

  3354.   tdep->infcall_ops.set_function_addr (regcache, func_addr);

  3355.   return sp;
  3356. }

  3357. static const struct ia64_infcall_ops ia64_infcall_ops =
  3358. {
  3359.   ia64_allocate_new_rse_frame,
  3360.   ia64_store_argument_in_slot,
  3361.   ia64_set_function_addr
  3362. };

  3363. static struct frame_id
  3364. ia64_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  3365. {
  3366.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  3367.   gdb_byte buf[8];
  3368.   CORE_ADDR sp, bsp;

  3369.   get_frame_register (this_frame, sp_regnum, buf);
  3370.   sp = extract_unsigned_integer (buf, 8, byte_order);

  3371.   get_frame_register (this_frame, IA64_BSP_REGNUM, buf);
  3372.   bsp = extract_unsigned_integer (buf, 8, byte_order);

  3373.   if (gdbarch_debug >= 1)
  3374.     fprintf_unfiltered (gdb_stdlog,
  3375.                         "dummy frame id: code %s, stack %s, special %s\n",
  3376.                         paddress (gdbarch, get_frame_pc (this_frame)),
  3377.                         paddress (gdbarch, sp), paddress (gdbarch, bsp));

  3378.   return frame_id_build_special (sp, get_frame_pc (this_frame), bsp);
  3379. }

  3380. static CORE_ADDR
  3381. ia64_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  3382. {
  3383.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  3384.   gdb_byte buf[8];
  3385.   CORE_ADDR ip, psr, pc;

  3386.   frame_unwind_register (next_frame, IA64_IP_REGNUM, buf);
  3387.   ip = extract_unsigned_integer (buf, 8, byte_order);
  3388.   frame_unwind_register (next_frame, IA64_PSR_REGNUM, buf);
  3389.   psr = extract_unsigned_integer (buf, 8, byte_order);

  3390.   pc = (ip & ~0xf) | ((psr >> 41) & 3);
  3391.   return pc;
  3392. }

  3393. static int
  3394. ia64_print_insn (bfd_vma memaddr, struct disassemble_info *info)
  3395. {
  3396.   info->bytes_per_line = SLOT_MULTIPLIER;
  3397.   return print_insn_ia64 (memaddr, info);
  3398. }

  3399. /* The default "size_of_register_frame" gdbarch_tdep routine for ia64.  */

  3400. static int
  3401. ia64_size_of_register_frame (struct frame_info *this_frame, ULONGEST cfm)
  3402. {
  3403.   return (cfm & 0x7f);
  3404. }

  3405. static struct gdbarch *
  3406. ia64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  3407. {
  3408.   struct gdbarch *gdbarch;
  3409.   struct gdbarch_tdep *tdep;

  3410.   /* If there is already a candidate, use it.  */
  3411.   arches = gdbarch_list_lookup_by_info (arches, &info);
  3412.   if (arches != NULL)
  3413.     return arches->gdbarch;

  3414.   tdep = xzalloc (sizeof (struct gdbarch_tdep));
  3415.   gdbarch = gdbarch_alloc (&info, tdep);

  3416.   tdep->size_of_register_frame = ia64_size_of_register_frame;

  3417.   /* According to the ia64 specs, instructions that store long double
  3418.      floats in memory use a long-double format different than that
  3419.      used in the floating registers.  The memory format matches the
  3420.      x86 extended float format which is 80 bits.  An OS may choose to
  3421.      use this format (e.g. GNU/Linux) or choose to use a different
  3422.      format for storing long doubles (e.g. HPUX).  In the latter case,
  3423.      the setting of the format may be moved/overridden in an
  3424.      OS-specific tdep file.  */
  3425.   set_gdbarch_long_double_format (gdbarch, floatformats_i387_ext);

  3426.   set_gdbarch_short_bit (gdbarch, 16);
  3427.   set_gdbarch_int_bit (gdbarch, 32);
  3428.   set_gdbarch_long_bit (gdbarch, 64);
  3429.   set_gdbarch_long_long_bit (gdbarch, 64);
  3430.   set_gdbarch_float_bit (gdbarch, 32);
  3431.   set_gdbarch_double_bit (gdbarch, 64);
  3432.   set_gdbarch_long_double_bit (gdbarch, 128);
  3433.   set_gdbarch_ptr_bit (gdbarch, 64);

  3434.   set_gdbarch_num_regs (gdbarch, NUM_IA64_RAW_REGS);
  3435.   set_gdbarch_num_pseudo_regs (gdbarch,
  3436.                                LAST_PSEUDO_REGNUM - FIRST_PSEUDO_REGNUM);
  3437.   set_gdbarch_sp_regnum (gdbarch, sp_regnum);
  3438.   set_gdbarch_fp0_regnum (gdbarch, IA64_FR0_REGNUM);

  3439.   set_gdbarch_register_name (gdbarch, ia64_register_name);
  3440.   set_gdbarch_register_type (gdbarch, ia64_register_type);

  3441.   set_gdbarch_pseudo_register_read (gdbarch, ia64_pseudo_register_read);
  3442.   set_gdbarch_pseudo_register_write (gdbarch, ia64_pseudo_register_write);
  3443.   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, ia64_dwarf_reg_to_regnum);
  3444.   set_gdbarch_register_reggroup_p (gdbarch, ia64_register_reggroup_p);
  3445.   set_gdbarch_convert_register_p (gdbarch, ia64_convert_register_p);
  3446.   set_gdbarch_register_to_value (gdbarch, ia64_register_to_value);
  3447.   set_gdbarch_value_to_register (gdbarch, ia64_value_to_register);

  3448.   set_gdbarch_skip_prologue (gdbarch, ia64_skip_prologue);

  3449.   set_gdbarch_return_value (gdbarch, ia64_return_value);

  3450.   set_gdbarch_memory_insert_breakpoint (gdbarch,
  3451.                                         ia64_memory_insert_breakpoint);
  3452.   set_gdbarch_memory_remove_breakpoint (gdbarch,
  3453.                                         ia64_memory_remove_breakpoint);
  3454.   set_gdbarch_breakpoint_from_pc (gdbarch, ia64_breakpoint_from_pc);
  3455.   set_gdbarch_read_pc (gdbarch, ia64_read_pc);
  3456.   set_gdbarch_write_pc (gdbarch, ia64_write_pc);

  3457.   /* Settings for calling functions in the inferior.  */
  3458.   set_gdbarch_push_dummy_call (gdbarch, ia64_push_dummy_call);
  3459.   tdep->infcall_ops = ia64_infcall_ops;
  3460.   set_gdbarch_frame_align (gdbarch, ia64_frame_align);
  3461.   set_gdbarch_dummy_id (gdbarch, ia64_dummy_id);

  3462.   set_gdbarch_unwind_pc (gdbarch, ia64_unwind_pc);
  3463. #ifdef HAVE_LIBUNWIND_IA64_H
  3464.   frame_unwind_append_unwinder (gdbarch,
  3465.                                 &ia64_libunwind_sigtramp_frame_unwind);
  3466.   frame_unwind_append_unwinder (gdbarch, &ia64_libunwind_frame_unwind);
  3467.   frame_unwind_append_unwinder (gdbarch, &ia64_sigtramp_frame_unwind);
  3468.   libunwind_frame_set_descr (gdbarch, &ia64_libunwind_descr);
  3469. #else
  3470.   frame_unwind_append_unwinder (gdbarch, &ia64_sigtramp_frame_unwind);
  3471. #endif
  3472.   frame_unwind_append_unwinder (gdbarch, &ia64_frame_unwind);
  3473.   frame_base_set_default (gdbarch, &ia64_frame_base);

  3474.   /* Settings that should be unnecessary.  */
  3475.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);

  3476.   set_gdbarch_print_insn (gdbarch, ia64_print_insn);
  3477.   set_gdbarch_convert_from_func_ptr_addr (gdbarch,
  3478.                                           ia64_convert_from_func_ptr_addr);

  3479.   /* The virtual table contains 16-byte descriptors, not pointers to
  3480.      descriptors.  */
  3481.   set_gdbarch_vtable_function_descriptors (gdbarch, 1);

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

  3484.   return gdbarch;
  3485. }

  3486. extern initialize_file_ftype _initialize_ia64_tdep; /* -Wmissing-prototypes */

  3487. void
  3488. _initialize_ia64_tdep (void)
  3489. {
  3490.   gdbarch_register (bfd_arch_ia64, ia64_gdbarch_init, NULL);
  3491. }