*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / ia64-tdep.c
CommitLineData
16461d7d 1/* Target-dependent code for the IA-64 for GDB, the GNU debugger.
ca557f44 2
6aba47ca 3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
13547ab6 4 Free Software Foundation, Inc.
16461d7d
KB
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
16461d7d
KB
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16461d7d
KB
20
21#include "defs.h"
22#include "inferior.h"
16461d7d 23#include "gdbcore.h"
8064c6ae 24#include "arch-utils.h"
16461d7d 25#include "floatformat.h"
e6bb342a 26#include "gdbtypes.h"
4e052eda 27#include "regcache.h"
004d836a
JJ
28#include "reggroups.h"
29#include "frame.h"
30#include "frame-base.h"
31#include "frame-unwind.h"
d16aafd8 32#include "doublest.h"
fd0407d6 33#include "value.h"
bd1ce8ba 34#include "gdb_assert.h"
16461d7d
KB
35#include "objfiles.h"
36#include "elf/common.h" /* for DT_PLTGOT value */
244bc108 37#include "elf-bfd.h"
a89aa300 38#include "dis-asm.h"
7d9b040b 39#include "infcall.h"
b33e8514 40#include "osabi.h"
9fc9f5e2 41#include "ia64-tdep.h"
0d5de010 42#include "cp-abi.h"
16461d7d 43
968d1cb4 44#ifdef HAVE_LIBUNWIND_IA64_H
8973ff21 45#include "elf/ia64.h" /* for PT_IA_64_UNWIND value */
968d1cb4
JJ
46#include "libunwind-frame.h"
47#include "libunwind-ia64.h"
c5a27d9c
JJ
48
49/* Note: KERNEL_START is supposed to be an address which is not going
50 to ever contain any valid unwind info. For ia64 linux, the choice
51 of 0xc000000000000000 is fairly safe since that's uncached space.
52
53 We use KERNEL_START as follows: after obtaining the kernel's
54 unwind table via getunwind(), we project its unwind data into
55 address-range KERNEL_START-(KERNEL_START+ktab_size) and then
56 when ia64_access_mem() sees a memory access to this
57 address-range, we redirect it to ktab instead.
58
59 None of this hackery is needed with a modern kernel/libcs
60 which uses the kernel virtual DSO to provide access to the
61 kernel's unwind info. In that case, ktab_size remains 0 and
62 hence the value of KERNEL_START doesn't matter. */
63
64#define KERNEL_START 0xc000000000000000ULL
65
66static size_t ktab_size = 0;
67struct ia64_table_entry
68 {
69 uint64_t start_offset;
70 uint64_t end_offset;
71 uint64_t info_offset;
72 };
73
74static struct ia64_table_entry *ktab = NULL;
75
968d1cb4
JJ
76#endif
77
698cb3f0
KB
78/* An enumeration of the different IA-64 instruction types. */
79
16461d7d
KB
80typedef enum instruction_type
81{
82 A, /* Integer ALU ; I-unit or M-unit */
83 I, /* Non-ALU integer; I-unit */
84 M, /* Memory ; M-unit */
85 F, /* Floating-point ; F-unit */
86 B, /* Branch ; B-unit */
87 L, /* Extended (L+X) ; I-unit */
88 X, /* Extended (L+X) ; I-unit */
89 undefined /* undefined or reserved */
90} instruction_type;
91
92/* We represent IA-64 PC addresses as the value of the instruction
93 pointer or'd with some bit combination in the low nibble which
94 represents the slot number in the bundle addressed by the
95 instruction pointer. The problem is that the Linux kernel
96 multiplies its slot numbers (for exceptions) by one while the
97 disassembler multiplies its slot numbers by 6. In addition, I've
98 heard it said that the simulator uses 1 as the multiplier.
99
100 I've fixed the disassembler so that the bytes_per_line field will
101 be the slot multiplier. If bytes_per_line comes in as zero, it
102 is set to six (which is how it was set up initially). -- objdump
103 displays pretty disassembly dumps with this value. For our purposes,
104 we'll set bytes_per_line to SLOT_MULTIPLIER. This is okay since we
105 never want to also display the raw bytes the way objdump does. */
106
107#define SLOT_MULTIPLIER 1
108
109/* Length in bytes of an instruction bundle */
110
111#define BUNDLE_LEN 16
112
16461d7d
KB
113static gdbarch_init_ftype ia64_gdbarch_init;
114
115static gdbarch_register_name_ftype ia64_register_name;
004d836a 116static gdbarch_register_type_ftype ia64_register_type;
16461d7d 117static gdbarch_breakpoint_from_pc_ftype ia64_breakpoint_from_pc;
16461d7d 118static gdbarch_skip_prologue_ftype ia64_skip_prologue;
64a5b29c 119static struct type *is_float_or_hfa_type (struct type *t);
b33e8514 120static CORE_ADDR ia64_find_global_pointer (CORE_ADDR faddr);
16461d7d 121
004d836a
JJ
122static struct type *builtin_type_ia64_ext;
123
124#define NUM_IA64_RAW_REGS 462
16461d7d 125
16461d7d
KB
126static int sp_regnum = IA64_GR12_REGNUM;
127static int fp_regnum = IA64_VFP_REGNUM;
128static int lr_regnum = IA64_VRAP_REGNUM;
129
004d836a 130/* NOTE: we treat the register stack registers r32-r127 as pseudo-registers because
4afcc598 131 they may not be accessible via the ptrace register get/set interfaces. */
004d836a
JJ
132enum pseudo_regs { FIRST_PSEUDO_REGNUM = NUM_IA64_RAW_REGS, VBOF_REGNUM = IA64_NAT127_REGNUM + 1, V32_REGNUM,
133 V127_REGNUM = V32_REGNUM + 95,
134 VP0_REGNUM, VP16_REGNUM = VP0_REGNUM + 16, VP63_REGNUM = VP0_REGNUM + 63, LAST_PSEUDO_REGNUM };
16461d7d
KB
135
136/* Array of register names; There should be ia64_num_regs strings in
137 the initializer. */
138
139static char *ia64_register_names[] =
140{ "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
141 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
142 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
143 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
004d836a
JJ
144 "", "", "", "", "", "", "", "",
145 "", "", "", "", "", "", "", "",
146 "", "", "", "", "", "", "", "",
147 "", "", "", "", "", "", "", "",
148 "", "", "", "", "", "", "", "",
149 "", "", "", "", "", "", "", "",
150 "", "", "", "", "", "", "", "",
151 "", "", "", "", "", "", "", "",
152 "", "", "", "", "", "", "", "",
153 "", "", "", "", "", "", "", "",
154 "", "", "", "", "", "", "", "",
155 "", "", "", "", "", "", "", "",
16461d7d
KB
156
157 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
158 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
159 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
160 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
161 "f32", "f33", "f34", "f35", "f36", "f37", "f38", "f39",
162 "f40", "f41", "f42", "f43", "f44", "f45", "f46", "f47",
163 "f48", "f49", "f50", "f51", "f52", "f53", "f54", "f55",
164 "f56", "f57", "f58", "f59", "f60", "f61", "f62", "f63",
165 "f64", "f65", "f66", "f67", "f68", "f69", "f70", "f71",
166 "f72", "f73", "f74", "f75", "f76", "f77", "f78", "f79",
167 "f80", "f81", "f82", "f83", "f84", "f85", "f86", "f87",
168 "f88", "f89", "f90", "f91", "f92", "f93", "f94", "f95",
169 "f96", "f97", "f98", "f99", "f100", "f101", "f102", "f103",
170 "f104", "f105", "f106", "f107", "f108", "f109", "f110", "f111",
171 "f112", "f113", "f114", "f115", "f116", "f117", "f118", "f119",
172 "f120", "f121", "f122", "f123", "f124", "f125", "f126", "f127",
173
004d836a
JJ
174 "", "", "", "", "", "", "", "",
175 "", "", "", "", "", "", "", "",
176 "", "", "", "", "", "", "", "",
177 "", "", "", "", "", "", "", "",
178 "", "", "", "", "", "", "", "",
179 "", "", "", "", "", "", "", "",
180 "", "", "", "", "", "", "", "",
181 "", "", "", "", "", "", "", "",
16461d7d
KB
182
183 "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7",
184
185 "vfp", "vrap",
186
187 "pr", "ip", "psr", "cfm",
188
189 "kr0", "kr1", "kr2", "kr3", "kr4", "kr5", "kr6", "kr7",
190 "", "", "", "", "", "", "", "",
191 "rsc", "bsp", "bspstore", "rnat",
192 "", "fcr", "", "",
193 "eflag", "csd", "ssd", "cflg", "fsr", "fir", "fdr", "",
194 "ccv", "", "", "", "unat", "", "", "",
195 "fpsr", "", "", "", "itc",
196 "", "", "", "", "", "", "", "", "", "",
197 "", "", "", "", "", "", "", "", "",
198 "pfs", "lc", "ec",
199 "", "", "", "", "", "", "", "", "", "",
200 "", "", "", "", "", "", "", "", "", "",
201 "", "", "", "", "", "", "", "", "", "",
202 "", "", "", "", "", "", "", "", "", "",
203 "", "", "", "", "", "", "", "", "", "",
204 "", "", "", "", "", "", "", "", "", "",
205 "",
206 "nat0", "nat1", "nat2", "nat3", "nat4", "nat5", "nat6", "nat7",
207 "nat8", "nat9", "nat10", "nat11", "nat12", "nat13", "nat14", "nat15",
208 "nat16", "nat17", "nat18", "nat19", "nat20", "nat21", "nat22", "nat23",
209 "nat24", "nat25", "nat26", "nat27", "nat28", "nat29", "nat30", "nat31",
210 "nat32", "nat33", "nat34", "nat35", "nat36", "nat37", "nat38", "nat39",
211 "nat40", "nat41", "nat42", "nat43", "nat44", "nat45", "nat46", "nat47",
212 "nat48", "nat49", "nat50", "nat51", "nat52", "nat53", "nat54", "nat55",
213 "nat56", "nat57", "nat58", "nat59", "nat60", "nat61", "nat62", "nat63",
214 "nat64", "nat65", "nat66", "nat67", "nat68", "nat69", "nat70", "nat71",
215 "nat72", "nat73", "nat74", "nat75", "nat76", "nat77", "nat78", "nat79",
216 "nat80", "nat81", "nat82", "nat83", "nat84", "nat85", "nat86", "nat87",
217 "nat88", "nat89", "nat90", "nat91", "nat92", "nat93", "nat94", "nat95",
218 "nat96", "nat97", "nat98", "nat99", "nat100","nat101","nat102","nat103",
219 "nat104","nat105","nat106","nat107","nat108","nat109","nat110","nat111",
220 "nat112","nat113","nat114","nat115","nat116","nat117","nat118","nat119",
221 "nat120","nat121","nat122","nat123","nat124","nat125","nat126","nat127",
004d836a
JJ
222
223 "bof",
224
225 "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
226 "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
227 "r48", "r49", "r50", "r51", "r52", "r53", "r54", "r55",
228 "r56", "r57", "r58", "r59", "r60", "r61", "r62", "r63",
229 "r64", "r65", "r66", "r67", "r68", "r69", "r70", "r71",
230 "r72", "r73", "r74", "r75", "r76", "r77", "r78", "r79",
231 "r80", "r81", "r82", "r83", "r84", "r85", "r86", "r87",
232 "r88", "r89", "r90", "r91", "r92", "r93", "r94", "r95",
233 "r96", "r97", "r98", "r99", "r100", "r101", "r102", "r103",
234 "r104", "r105", "r106", "r107", "r108", "r109", "r110", "r111",
235 "r112", "r113", "r114", "r115", "r116", "r117", "r118", "r119",
236 "r120", "r121", "r122", "r123", "r124", "r125", "r126", "r127",
237
238 "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7",
239 "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15",
240 "p16", "p17", "p18", "p19", "p20", "p21", "p22", "p23",
241 "p24", "p25", "p26", "p27", "p28", "p29", "p30", "p31",
242 "p32", "p33", "p34", "p35", "p36", "p37", "p38", "p39",
243 "p40", "p41", "p42", "p43", "p44", "p45", "p46", "p47",
244 "p48", "p49", "p50", "p51", "p52", "p53", "p54", "p55",
245 "p56", "p57", "p58", "p59", "p60", "p61", "p62", "p63",
16461d7d
KB
246};
247
004d836a
JJ
248struct ia64_frame_cache
249{
250 CORE_ADDR base; /* frame pointer base for frame */
251 CORE_ADDR pc; /* function start pc for frame */
252 CORE_ADDR saved_sp; /* stack pointer for frame */
253 CORE_ADDR bsp; /* points at r32 for the current frame */
254 CORE_ADDR cfm; /* cfm value for current frame */
4afcc598 255 CORE_ADDR prev_cfm; /* cfm value for previous frame */
004d836a
JJ
256 int frameless;
257 int sof; /* Size of frame (decoded from cfm value) */
258 int sol; /* Size of locals (decoded from cfm value) */
259 int sor; /* Number of rotating registers. (decoded from cfm value) */
260 CORE_ADDR after_prologue;
261 /* Address of first instruction after the last
262 prologue instruction; Note that there may
263 be instructions from the function's body
264 intermingled with the prologue. */
265 int mem_stack_frame_size;
266 /* Size of the memory stack frame (may be zero),
267 or -1 if it has not been determined yet. */
268 int fp_reg; /* Register number (if any) used a frame pointer
244bc108 269 for this frame. 0 if no register is being used
16461d7d 270 as the frame pointer. */
004d836a
JJ
271
272 /* Saved registers. */
273 CORE_ADDR saved_regs[NUM_IA64_RAW_REGS];
274
275};
244bc108 276
004d836a
JJ
277int
278ia64_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
279 struct reggroup *group)
16461d7d 280{
004d836a
JJ
281 int vector_p;
282 int float_p;
283 int raw_p;
284 if (group == all_reggroup)
285 return 1;
286 vector_p = TYPE_VECTOR (register_type (gdbarch, regnum));
287 float_p = TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT;
288 raw_p = regnum < NUM_IA64_RAW_REGS;
289 if (group == float_reggroup)
290 return float_p;
291 if (group == vector_reggroup)
292 return vector_p;
293 if (group == general_reggroup)
294 return (!vector_p && !float_p);
295 if (group == save_reggroup || group == restore_reggroup)
296 return raw_p;
297 return 0;
16461d7d
KB
298}
299
004d836a
JJ
300static const char *
301ia64_register_name (int reg)
16461d7d 302{
004d836a 303 return ia64_register_names[reg];
16461d7d
KB
304}
305
004d836a
JJ
306struct type *
307ia64_register_type (struct gdbarch *arch, int reg)
16461d7d 308{
004d836a
JJ
309 if (reg >= IA64_FR0_REGNUM && reg <= IA64_FR127_REGNUM)
310 return builtin_type_ia64_ext;
311 else
312 return builtin_type_long;
16461d7d
KB
313}
314
a78f21af 315static int
004d836a 316ia64_dwarf_reg_to_regnum (int reg)
16461d7d 317{
004d836a
JJ
318 if (reg >= IA64_GR32_REGNUM && reg <= IA64_GR127_REGNUM)
319 return V32_REGNUM + (reg - IA64_GR32_REGNUM);
320 return reg;
16461d7d
KB
321}
322
4afcc598 323static int
2fda21a6 324floatformat_valid (const struct floatformat *fmt, const void *from)
4afcc598
JJ
325{
326 return 1;
327}
328
16461d7d
KB
329const struct floatformat floatformat_ia64_ext =
330{
331 floatformat_little, 82, 0, 1, 17, 65535, 0x1ffff, 18, 64,
4afcc598 332 floatformat_intbit_yes, "floatformat_ia64_ext", floatformat_valid
16461d7d
KB
333};
334
8da61cc4
DJ
335const struct floatformat *floatformats_ia64_ext[2] =
336{
337 &floatformat_ia64_ext,
338 &floatformat_ia64_ext
339};
340
16461d7d
KB
341
342/* Extract ``len'' bits from an instruction bundle starting at
343 bit ``from''. */
344
244bc108 345static long long
16461d7d
KB
346extract_bit_field (char *bundle, int from, int len)
347{
348 long long result = 0LL;
349 int to = from + len;
350 int from_byte = from / 8;
351 int to_byte = to / 8;
352 unsigned char *b = (unsigned char *) bundle;
353 unsigned char c;
354 int lshift;
355 int i;
356
357 c = b[from_byte];
358 if (from_byte == to_byte)
359 c = ((unsigned char) (c << (8 - to % 8))) >> (8 - to % 8);
360 result = c >> (from % 8);
361 lshift = 8 - (from % 8);
362
363 for (i = from_byte+1; i < to_byte; i++)
364 {
365 result |= ((long long) b[i]) << lshift;
366 lshift += 8;
367 }
368
369 if (from_byte < to_byte && (to % 8 != 0))
370 {
371 c = b[to_byte];
372 c = ((unsigned char) (c << (8 - to % 8))) >> (8 - to % 8);
373 result |= ((long long) c) << lshift;
374 }
375
376 return result;
377}
378
379/* Replace the specified bits in an instruction bundle */
380
244bc108 381static void
16461d7d
KB
382replace_bit_field (char *bundle, long long val, int from, int len)
383{
384 int to = from + len;
385 int from_byte = from / 8;
386 int to_byte = to / 8;
387 unsigned char *b = (unsigned char *) bundle;
388 unsigned char c;
389
390 if (from_byte == to_byte)
391 {
392 unsigned char left, right;
393 c = b[from_byte];
394 left = (c >> (to % 8)) << (to % 8);
395 right = ((unsigned char) (c << (8 - from % 8))) >> (8 - from % 8);
396 c = (unsigned char) (val & 0xff);
397 c = (unsigned char) (c << (from % 8 + 8 - to % 8)) >> (8 - to % 8);
398 c |= right | left;
399 b[from_byte] = c;
400 }
401 else
402 {
403 int i;
404 c = b[from_byte];
405 c = ((unsigned char) (c << (8 - from % 8))) >> (8 - from % 8);
406 c = c | (val << (from % 8));
407 b[from_byte] = c;
408 val >>= 8 - from % 8;
409
410 for (i = from_byte+1; i < to_byte; i++)
411 {
412 c = val & 0xff;
413 val >>= 8;
414 b[i] = c;
415 }
416
417 if (to % 8 != 0)
418 {
419 unsigned char cv = (unsigned char) val;
420 c = b[to_byte];
421 c = c >> (to % 8) << (to % 8);
422 c |= ((unsigned char) (cv << (8 - to % 8))) >> (8 - to % 8);
423 b[to_byte] = c;
424 }
425 }
426}
427
428/* Return the contents of slot N (for N = 0, 1, or 2) in
429 and instruction bundle */
430
244bc108 431static long long
2fc3ac7e 432slotN_contents (char *bundle, int slotnum)
16461d7d
KB
433{
434 return extract_bit_field (bundle, 5+41*slotnum, 41);
435}
436
437/* Store an instruction in an instruction bundle */
438
244bc108 439static void
2fc3ac7e 440replace_slotN_contents (char *bundle, long long instr, int slotnum)
16461d7d
KB
441{
442 replace_bit_field (bundle, instr, 5+41*slotnum, 41);
443}
444
64a5b29c 445static enum instruction_type template_encoding_table[32][3] =
16461d7d
KB
446{
447 { M, I, I }, /* 00 */
448 { M, I, I }, /* 01 */
449 { M, I, I }, /* 02 */
450 { M, I, I }, /* 03 */
451 { M, L, X }, /* 04 */
452 { M, L, X }, /* 05 */
453 { undefined, undefined, undefined }, /* 06 */
454 { undefined, undefined, undefined }, /* 07 */
455 { M, M, I }, /* 08 */
456 { M, M, I }, /* 09 */
457 { M, M, I }, /* 0A */
458 { M, M, I }, /* 0B */
459 { M, F, I }, /* 0C */
460 { M, F, I }, /* 0D */
461 { M, M, F }, /* 0E */
462 { M, M, F }, /* 0F */
463 { M, I, B }, /* 10 */
464 { M, I, B }, /* 11 */
465 { M, B, B }, /* 12 */
466 { M, B, B }, /* 13 */
467 { undefined, undefined, undefined }, /* 14 */
468 { undefined, undefined, undefined }, /* 15 */
469 { B, B, B }, /* 16 */
470 { B, B, B }, /* 17 */
471 { M, M, B }, /* 18 */
472 { M, M, B }, /* 19 */
473 { undefined, undefined, undefined }, /* 1A */
474 { undefined, undefined, undefined }, /* 1B */
475 { M, F, B }, /* 1C */
476 { M, F, B }, /* 1D */
477 { undefined, undefined, undefined }, /* 1E */
478 { undefined, undefined, undefined }, /* 1F */
479};
480
481/* Fetch and (partially) decode an instruction at ADDR and return the
482 address of the next instruction to fetch. */
483
484static CORE_ADDR
485fetch_instruction (CORE_ADDR addr, instruction_type *it, long long *instr)
486{
487 char bundle[BUNDLE_LEN];
488 int slotnum = (int) (addr & 0x0f) / SLOT_MULTIPLIER;
489 long long template;
490 int val;
491
c26e1c2b
KB
492 /* Warn about slot numbers greater than 2. We used to generate
493 an error here on the assumption that the user entered an invalid
494 address. But, sometimes GDB itself requests an invalid address.
495 This can (easily) happen when execution stops in a function for
496 which there are no symbols. The prologue scanner will attempt to
497 find the beginning of the function - if the nearest symbol
498 happens to not be aligned on a bundle boundary (16 bytes), the
499 resulting starting address will cause GDB to think that the slot
500 number is too large.
501
502 So we warn about it and set the slot number to zero. It is
503 not necessarily a fatal condition, particularly if debugging
504 at the assembly language level. */
16461d7d 505 if (slotnum > 2)
c26e1c2b 506 {
8a3fe4f8
AC
507 warning (_("Can't fetch instructions for slot numbers greater than 2.\n"
508 "Using slot 0 instead"));
c26e1c2b
KB
509 slotnum = 0;
510 }
16461d7d
KB
511
512 addr &= ~0x0f;
513
514 val = target_read_memory (addr, bundle, BUNDLE_LEN);
515
516 if (val != 0)
517 return 0;
518
519 *instr = slotN_contents (bundle, slotnum);
520 template = extract_bit_field (bundle, 0, 5);
521 *it = template_encoding_table[(int)template][slotnum];
522
64a5b29c 523 if (slotnum == 2 || (slotnum == 1 && *it == L))
16461d7d
KB
524 addr += 16;
525 else
526 addr += (slotnum + 1) * SLOT_MULTIPLIER;
527
528 return addr;
529}
530
531/* There are 5 different break instructions (break.i, break.b,
532 break.m, break.f, and break.x), but they all have the same
533 encoding. (The five bit template in the low five bits of the
534 instruction bundle distinguishes one from another.)
535
536 The runtime architecture manual specifies that break instructions
537 used for debugging purposes must have the upper two bits of the 21
538 bit immediate set to a 0 and a 1 respectively. A breakpoint
539 instruction encodes the most significant bit of its 21 bit
540 immediate at bit 36 of the 41 bit instruction. The penultimate msb
541 is at bit 25 which leads to the pattern below.
542
543 Originally, I had this set up to do, e.g, a "break.i 0x80000" But
544 it turns out that 0x80000 was used as the syscall break in the early
545 simulators. So I changed the pattern slightly to do "break.i 0x080001"
546 instead. But that didn't work either (I later found out that this
547 pattern was used by the simulator that I was using.) So I ended up
548 using the pattern seen below. */
549
550#if 0
aaab4dba 551#define IA64_BREAKPOINT 0x00002000040LL
16461d7d 552#endif
aaab4dba 553#define IA64_BREAKPOINT 0x00003333300LL
16461d7d
KB
554
555static int
8181d85f 556ia64_memory_insert_breakpoint (struct bp_target_info *bp_tgt)
16461d7d 557{
8181d85f 558 CORE_ADDR addr = bp_tgt->placed_address;
16461d7d
KB
559 char bundle[BUNDLE_LEN];
560 int slotnum = (int) (addr & 0x0f) / SLOT_MULTIPLIER;
561 long long instr;
562 int val;
126fa72d 563 int template;
16461d7d
KB
564
565 if (slotnum > 2)
8a3fe4f8 566 error (_("Can't insert breakpoint for slot numbers greater than 2."));
16461d7d
KB
567
568 addr &= ~0x0f;
569
570 val = target_read_memory (addr, bundle, BUNDLE_LEN);
126fa72d
PS
571
572 /* Check for L type instruction in 2nd slot, if present then
573 bump up the slot number to the 3rd slot */
574 template = extract_bit_field (bundle, 0, 5);
575 if (slotnum == 1 && template_encoding_table[template][1] == L)
576 {
577 slotnum = 2;
578 }
579
16461d7d 580 instr = slotN_contents (bundle, slotnum);
8181d85f
DJ
581 memcpy (bp_tgt->shadow_contents, &instr, sizeof (instr));
582 bp_tgt->placed_size = bp_tgt->shadow_len = sizeof (instr);
aaab4dba 583 replace_slotN_contents (bundle, IA64_BREAKPOINT, slotnum);
16461d7d
KB
584 if (val == 0)
585 target_write_memory (addr, bundle, BUNDLE_LEN);
586
587 return val;
588}
589
590static int
8181d85f 591ia64_memory_remove_breakpoint (struct bp_target_info *bp_tgt)
16461d7d 592{
8181d85f 593 CORE_ADDR addr = bp_tgt->placed_address;
16461d7d
KB
594 char bundle[BUNDLE_LEN];
595 int slotnum = (addr & 0x0f) / SLOT_MULTIPLIER;
596 long long instr;
597 int val;
126fa72d 598 int template;
16461d7d
KB
599
600 addr &= ~0x0f;
601
602 val = target_read_memory (addr, bundle, BUNDLE_LEN);
126fa72d
PS
603
604 /* Check for L type instruction in 2nd slot, if present then
605 bump up the slot number to the 3rd slot */
606 template = extract_bit_field (bundle, 0, 5);
607 if (slotnum == 1 && template_encoding_table[template][1] == L)
608 {
609 slotnum = 2;
610 }
611
8181d85f 612 memcpy (&instr, bp_tgt->shadow_contents, sizeof instr);
16461d7d
KB
613 replace_slotN_contents (bundle, instr, slotnum);
614 if (val == 0)
615 target_write_memory (addr, bundle, BUNDLE_LEN);
616
617 return val;
618}
619
620/* We don't really want to use this, but remote.c needs to call it in order
621 to figure out if Z-packets are supported or not. Oh, well. */
f4f9705a 622const unsigned char *
fba45db2 623ia64_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
16461d7d
KB
624{
625 static unsigned char breakpoint[] =
626 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
627 *lenptr = sizeof (breakpoint);
628#if 0
629 *pcptr &= ~0x0f;
630#endif
631 return breakpoint;
632}
633
a78f21af 634static CORE_ADDR
61a1198a 635ia64_read_pc (struct regcache *regcache)
16461d7d 636{
61a1198a
UW
637 ULONGEST psr_value, pc_value;
638 int slot_num;
639
640 regcache_cooked_read_unsigned (regcache, IA64_PSR_REGNUM, &psr_value);
641 regcache_cooked_read_unsigned (regcache, IA64_IP_REGNUM, &pc_value);
642 slot_num = (psr_value >> 41) & 3;
16461d7d
KB
643
644 return pc_value | (slot_num * SLOT_MULTIPLIER);
645}
646
54a5c8d8 647void
61a1198a 648ia64_write_pc (struct regcache *regcache, CORE_ADDR new_pc)
16461d7d
KB
649{
650 int slot_num = (int) (new_pc & 0xf) / SLOT_MULTIPLIER;
61a1198a
UW
651 ULONGEST psr_value;
652
653 regcache_cooked_read_unsigned (regcache, IA64_PSR_REGNUM, &psr_value);
16461d7d 654 psr_value &= ~(3LL << 41);
61a1198a 655 psr_value |= (ULONGEST)(slot_num & 0x3) << 41;
16461d7d
KB
656
657 new_pc &= ~0xfLL;
658
61a1198a
UW
659 regcache_cooked_write_unsigned (regcache, IA64_PSR_REGNUM, psr_value);
660 regcache_cooked_write_unsigned (regcache, IA64_IP_REGNUM, new_pc);
16461d7d
KB
661}
662
663#define IS_NaT_COLLECTION_ADDR(addr) ((((addr) >> 3) & 0x3f) == 0x3f)
664
665/* Returns the address of the slot that's NSLOTS slots away from
666 the address ADDR. NSLOTS may be positive or negative. */
667static CORE_ADDR
668rse_address_add(CORE_ADDR addr, int nslots)
669{
670 CORE_ADDR new_addr;
671 int mandatory_nat_slots = nslots / 63;
672 int direction = nslots < 0 ? -1 : 1;
673
674 new_addr = addr + 8 * (nslots + mandatory_nat_slots);
675
676 if ((new_addr >> 9) != ((addr + 8 * 64 * mandatory_nat_slots) >> 9))
677 new_addr += 8 * direction;
678
679 if (IS_NaT_COLLECTION_ADDR(new_addr))
680 new_addr += 8 * direction;
681
682 return new_addr;
683}
684
004d836a
JJ
685static void
686ia64_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
88d82102 687 int regnum, gdb_byte *buf)
16461d7d 688{
004d836a 689 if (regnum >= V32_REGNUM && regnum <= V127_REGNUM)
244bc108 690 {
88d82102 691#ifdef HAVE_LIBUNWIND_IA64_H
c5a27d9c
JJ
692 /* First try and use the libunwind special reg accessor, otherwise fallback to
693 standard logic. */
694 if (!libunwind_is_initialized ()
45ecac4b 695 || libunwind_get_reg_special (gdbarch, regcache, regnum, buf) != 0)
88d82102 696#endif
004d836a 697 {
c5a27d9c
JJ
698 /* The fallback position is to assume that r32-r127 are found sequentially
699 in memory starting at $bof. This isn't always true, but without libunwind,
700 this is the best we can do. */
701 ULONGEST cfm;
702 ULONGEST bsp;
703 CORE_ADDR reg;
704 regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
705 regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
706
707 /* The bsp points at the end of the register frame so we
708 subtract the size of frame from it to get start of register frame. */
709 bsp = rse_address_add (bsp, -(cfm & 0x7f));
710
711 if ((cfm & 0x7f) > regnum - V32_REGNUM)
712 {
713 ULONGEST reg_addr = rse_address_add (bsp, (regnum - V32_REGNUM));
714 reg = read_memory_integer ((CORE_ADDR)reg_addr, 8);
088568da 715 store_unsigned_integer (buf, register_size (gdbarch, regnum), reg);
c5a27d9c
JJ
716 }
717 else
088568da 718 store_unsigned_integer (buf, register_size (gdbarch, regnum), 0);
004d836a 719 }
004d836a
JJ
720 }
721 else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT31_REGNUM)
722 {
723 ULONGEST unatN_val;
724 ULONGEST unat;
725 regcache_cooked_read_unsigned (regcache, IA64_UNAT_REGNUM, &unat);
726 unatN_val = (unat & (1LL << (regnum - IA64_NAT0_REGNUM))) != 0;
088568da 727 store_unsigned_integer (buf, register_size (gdbarch, regnum), unatN_val);
004d836a
JJ
728 }
729 else if (IA64_NAT32_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
730 {
731 ULONGEST natN_val = 0;
732 ULONGEST bsp;
733 ULONGEST cfm;
734 CORE_ADDR gr_addr = 0;
735 regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
736 regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
737
738 /* The bsp points at the end of the register frame so we
739 subtract the size of frame from it to get start of register frame. */
740 bsp = rse_address_add (bsp, -(cfm & 0x7f));
741
742 if ((cfm & 0x7f) > regnum - V32_REGNUM)
743 gr_addr = rse_address_add (bsp, (regnum - V32_REGNUM));
744
745 if (gr_addr != 0)
746 {
747 /* Compute address of nat collection bits. */
748 CORE_ADDR nat_addr = gr_addr | 0x1f8;
749 CORE_ADDR nat_collection;
750 int nat_bit;
751 /* If our nat collection address is bigger than bsp, we have to get
752 the nat collection from rnat. Otherwise, we fetch the nat
753 collection from the computed address. */
754 if (nat_addr >= bsp)
755 regcache_cooked_read_unsigned (regcache, IA64_RNAT_REGNUM, &nat_collection);
756 else
757 nat_collection = read_memory_integer (nat_addr, 8);
758 nat_bit = (gr_addr >> 3) & 0x3f;
759 natN_val = (nat_collection >> nat_bit) & 1;
760 }
761
088568da 762 store_unsigned_integer (buf, register_size (gdbarch, regnum), natN_val);
244bc108 763 }
004d836a
JJ
764 else if (regnum == VBOF_REGNUM)
765 {
766 /* A virtual register frame start is provided for user convenience.
767 It can be calculated as the bsp - sof (sizeof frame). */
768 ULONGEST bsp, vbsp;
769 ULONGEST cfm;
770 CORE_ADDR reg;
771 regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
772 regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
773
774 /* The bsp points at the end of the register frame so we
775 subtract the size of frame from it to get beginning of frame. */
776 vbsp = rse_address_add (bsp, -(cfm & 0x7f));
088568da 777 store_unsigned_integer (buf, register_size (gdbarch, regnum), vbsp);
004d836a
JJ
778 }
779 else if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
780 {
781 ULONGEST pr;
782 ULONGEST cfm;
783 ULONGEST prN_val;
784 CORE_ADDR reg;
785 regcache_cooked_read_unsigned (regcache, IA64_PR_REGNUM, &pr);
786 regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
787
788 if (VP16_REGNUM <= regnum && regnum <= VP63_REGNUM)
789 {
790 /* Fetch predicate register rename base from current frame
791 marker for this frame. */
792 int rrb_pr = (cfm >> 32) & 0x3f;
793
794 /* Adjust the register number to account for register rotation. */
795 regnum = VP16_REGNUM
796 + ((regnum - VP16_REGNUM) + rrb_pr) % 48;
797 }
798 prN_val = (pr & (1LL << (regnum - VP0_REGNUM))) != 0;
088568da 799 store_unsigned_integer (buf, register_size (gdbarch, regnum), prN_val);
004d836a
JJ
800 }
801 else
088568da 802 memset (buf, 0, register_size (gdbarch, regnum));
16461d7d
KB
803}
804
004d836a
JJ
805static void
806ia64_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
88d82102 807 int regnum, const gdb_byte *buf)
16461d7d 808{
004d836a 809 if (regnum >= V32_REGNUM && regnum <= V127_REGNUM)
244bc108 810 {
004d836a
JJ
811 ULONGEST bsp;
812 ULONGEST cfm;
813 CORE_ADDR reg;
814 regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
815 regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
816
817 bsp = rse_address_add (bsp, -(cfm & 0x7f));
818
819 if ((cfm & 0x7f) > regnum - V32_REGNUM)
820 {
821 ULONGEST reg_addr = rse_address_add (bsp, (regnum - V32_REGNUM));
822 write_memory (reg_addr, (void *)buf, 8);
823 }
824 }
825 else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT31_REGNUM)
826 {
827 ULONGEST unatN_val, unat, unatN_mask;
828 regcache_cooked_read_unsigned (regcache, IA64_UNAT_REGNUM, &unat);
088568da 829 unatN_val = extract_unsigned_integer (buf, register_size (gdbarch, regnum));
004d836a
JJ
830 unatN_mask = (1LL << (regnum - IA64_NAT0_REGNUM));
831 if (unatN_val == 0)
832 unat &= ~unatN_mask;
833 else if (unatN_val == 1)
834 unat |= unatN_mask;
835 regcache_cooked_write_unsigned (regcache, IA64_UNAT_REGNUM, unat);
836 }
837 else if (IA64_NAT32_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
838 {
839 ULONGEST natN_val;
840 ULONGEST bsp;
841 ULONGEST cfm;
842 CORE_ADDR gr_addr = 0;
843 regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
844 regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
845
846 /* The bsp points at the end of the register frame so we
847 subtract the size of frame from it to get start of register frame. */
848 bsp = rse_address_add (bsp, -(cfm & 0x7f));
849
850 if ((cfm & 0x7f) > regnum - V32_REGNUM)
851 gr_addr = rse_address_add (bsp, (regnum - V32_REGNUM));
852
088568da 853 natN_val = extract_unsigned_integer (buf, register_size (gdbarch, regnum));
004d836a
JJ
854
855 if (gr_addr != 0 && (natN_val == 0 || natN_val == 1))
856 {
857 /* Compute address of nat collection bits. */
858 CORE_ADDR nat_addr = gr_addr | 0x1f8;
859 CORE_ADDR nat_collection;
860 int natN_bit = (gr_addr >> 3) & 0x3f;
861 ULONGEST natN_mask = (1LL << natN_bit);
862 /* If our nat collection address is bigger than bsp, we have to get
863 the nat collection from rnat. Otherwise, we fetch the nat
864 collection from the computed address. */
865 if (nat_addr >= bsp)
866 {
867 regcache_cooked_read_unsigned (regcache, IA64_RNAT_REGNUM, &nat_collection);
868 if (natN_val)
869 nat_collection |= natN_mask;
870 else
871 nat_collection &= ~natN_mask;
872 regcache_cooked_write_unsigned (regcache, IA64_RNAT_REGNUM, nat_collection);
873 }
874 else
875 {
876 char nat_buf[8];
877 nat_collection = read_memory_integer (nat_addr, 8);
878 if (natN_val)
879 nat_collection |= natN_mask;
880 else
881 nat_collection &= ~natN_mask;
088568da 882 store_unsigned_integer (nat_buf, register_size (gdbarch, regnum), nat_collection);
004d836a
JJ
883 write_memory (nat_addr, nat_buf, 8);
884 }
885 }
886 }
887 else if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
888 {
889 ULONGEST pr;
890 ULONGEST cfm;
891 ULONGEST prN_val;
892 ULONGEST prN_mask;
893
894 regcache_cooked_read_unsigned (regcache, IA64_PR_REGNUM, &pr);
895 regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
896
897 if (VP16_REGNUM <= regnum && regnum <= VP63_REGNUM)
898 {
899 /* Fetch predicate register rename base from current frame
900 marker for this frame. */
901 int rrb_pr = (cfm >> 32) & 0x3f;
902
903 /* Adjust the register number to account for register rotation. */
904 regnum = VP16_REGNUM
905 + ((regnum - VP16_REGNUM) + rrb_pr) % 48;
906 }
088568da 907 prN_val = extract_unsigned_integer (buf, register_size (gdbarch, regnum));
004d836a
JJ
908 prN_mask = (1LL << (regnum - VP0_REGNUM));
909 if (prN_val == 0)
910 pr &= ~prN_mask;
911 else if (prN_val == 1)
912 pr |= prN_mask;
913 regcache_cooked_write_unsigned (regcache, IA64_PR_REGNUM, pr);
244bc108 914 }
16461d7d
KB
915}
916
004d836a
JJ
917/* The ia64 needs to convert between various ieee floating-point formats
918 and the special ia64 floating point register format. */
919
920static int
921ia64_convert_register_p (int regno, struct type *type)
922{
923 return (regno >= IA64_FR0_REGNUM && regno <= IA64_FR127_REGNUM);
924}
925
926static void
927ia64_register_to_value (struct frame_info *frame, int regnum,
88d82102 928 struct type *valtype, gdb_byte *out)
004d836a
JJ
929{
930 char in[MAX_REGISTER_SIZE];
931 frame_register_read (frame, regnum, in);
932 convert_typed_floating (in, builtin_type_ia64_ext, out, valtype);
933}
934
935static void
936ia64_value_to_register (struct frame_info *frame, int regnum,
88d82102 937 struct type *valtype, const gdb_byte *in)
004d836a
JJ
938{
939 char out[MAX_REGISTER_SIZE];
940 convert_typed_floating (in, valtype, out, builtin_type_ia64_ext);
941 put_frame_register (frame, regnum, out);
942}
943
944
58ab00f9
KB
945/* Limit the number of skipped non-prologue instructions since examining
946 of the prologue is expensive. */
5ea2bd7f 947static int max_skip_non_prologue_insns = 40;
58ab00f9
KB
948
949/* Given PC representing the starting address of a function, and
950 LIM_PC which is the (sloppy) limit to which to scan when looking
951 for a prologue, attempt to further refine this limit by using
952 the line data in the symbol table. If successful, a better guess
953 on where the prologue ends is returned, otherwise the previous
954 value of lim_pc is returned. TRUST_LIMIT is a pointer to a flag
955 which will be set to indicate whether the returned limit may be
956 used with no further scanning in the event that the function is
957 frameless. */
958
634aa483
AC
959/* FIXME: cagney/2004-02-14: This function and logic have largely been
960 superseded by skip_prologue_using_sal. */
961
58ab00f9
KB
962static CORE_ADDR
963refine_prologue_limit (CORE_ADDR pc, CORE_ADDR lim_pc, int *trust_limit)
964{
965 struct symtab_and_line prologue_sal;
966 CORE_ADDR start_pc = pc;
39312971
JB
967 CORE_ADDR end_pc;
968
969 /* The prologue can not possibly go past the function end itself,
970 so we can already adjust LIM_PC accordingly. */
971 if (find_pc_partial_function (pc, NULL, NULL, &end_pc) && end_pc < lim_pc)
972 lim_pc = end_pc;
58ab00f9
KB
973
974 /* Start off not trusting the limit. */
975 *trust_limit = 0;
976
977 prologue_sal = find_pc_line (pc, 0);
978 if (prologue_sal.line != 0)
979 {
980 int i;
981 CORE_ADDR addr = prologue_sal.end;
982
983 /* Handle the case in which compiler's optimizer/scheduler
984 has moved instructions into the prologue. We scan ahead
985 in the function looking for address ranges whose corresponding
986 line number is less than or equal to the first one that we
987 found for the function. (It can be less than when the
988 scheduler puts a body instruction before the first prologue
989 instruction.) */
990 for (i = 2 * max_skip_non_prologue_insns;
991 i > 0 && (lim_pc == 0 || addr < lim_pc);
992 i--)
993 {
994 struct symtab_and_line sal;
995
996 sal = find_pc_line (addr, 0);
997 if (sal.line == 0)
998 break;
999 if (sal.line <= prologue_sal.line
1000 && sal.symtab == prologue_sal.symtab)
1001 {
1002 prologue_sal = sal;
1003 }
1004 addr = sal.end;
1005 }
1006
1007 if (lim_pc == 0 || prologue_sal.end < lim_pc)
1008 {
1009 lim_pc = prologue_sal.end;
1010 if (start_pc == get_pc_function_start (lim_pc))
1011 *trust_limit = 1;
1012 }
1013 }
1014 return lim_pc;
1015}
1016
16461d7d
KB
1017#define isScratch(_regnum_) ((_regnum_) == 2 || (_regnum_) == 3 \
1018 || (8 <= (_regnum_) && (_regnum_) <= 11) \
1019 || (14 <= (_regnum_) && (_regnum_) <= 31))
1020#define imm9(_instr_) \
1021 ( ((((_instr_) & 0x01000000000LL) ? -1 : 0) << 8) \
1022 | (((_instr_) & 0x00008000000LL) >> 20) \
1023 | (((_instr_) & 0x00000001fc0LL) >> 6))
1024
004d836a
JJ
1025/* Allocate and initialize a frame cache. */
1026
1027static struct ia64_frame_cache *
1028ia64_alloc_frame_cache (void)
1029{
1030 struct ia64_frame_cache *cache;
1031 int i;
1032
1033 cache = FRAME_OBSTACK_ZALLOC (struct ia64_frame_cache);
1034
1035 /* Base address. */
1036 cache->base = 0;
1037 cache->pc = 0;
1038 cache->cfm = 0;
4afcc598 1039 cache->prev_cfm = 0;
004d836a
JJ
1040 cache->sof = 0;
1041 cache->sol = 0;
1042 cache->sor = 0;
1043 cache->bsp = 0;
1044 cache->fp_reg = 0;
1045 cache->frameless = 1;
1046
1047 for (i = 0; i < NUM_IA64_RAW_REGS; i++)
1048 cache->saved_regs[i] = 0;
1049
1050 return cache;
1051}
1052
16461d7d 1053static CORE_ADDR
004d836a 1054examine_prologue (CORE_ADDR pc, CORE_ADDR lim_pc, struct frame_info *next_frame, struct ia64_frame_cache *cache)
16461d7d
KB
1055{
1056 CORE_ADDR next_pc;
1057 CORE_ADDR last_prologue_pc = pc;
16461d7d
KB
1058 instruction_type it;
1059 long long instr;
16461d7d
KB
1060 int cfm_reg = 0;
1061 int ret_reg = 0;
1062 int fp_reg = 0;
1063 int unat_save_reg = 0;
1064 int pr_save_reg = 0;
1065 int mem_stack_frame_size = 0;
1066 int spill_reg = 0;
1067 CORE_ADDR spill_addr = 0;
0927a22b
KB
1068 char instores[8];
1069 char infpstores[8];
5ea2bd7f 1070 char reg_contents[256];
58ab00f9 1071 int trust_limit;
004d836a
JJ
1072 int frameless = 1;
1073 int i;
1074 CORE_ADDR addr;
1075 char buf[8];
1076 CORE_ADDR bof, sor, sol, sof, cfm, rrb_gr;
0927a22b
KB
1077
1078 memset (instores, 0, sizeof instores);
1079 memset (infpstores, 0, sizeof infpstores);
5ea2bd7f 1080 memset (reg_contents, 0, sizeof reg_contents);
16461d7d 1081
004d836a
JJ
1082 if (cache->after_prologue != 0
1083 && cache->after_prologue <= lim_pc)
1084 return cache->after_prologue;
16461d7d 1085
58ab00f9 1086 lim_pc = refine_prologue_limit (pc, lim_pc, &trust_limit);
16461d7d 1087 next_pc = fetch_instruction (pc, &it, &instr);
5ea2bd7f
JJ
1088
1089 /* We want to check if we have a recognizable function start before we
1090 look ahead for a prologue. */
16461d7d
KB
1091 if (pc < lim_pc && next_pc
1092 && it == M && ((instr & 0x1ee0000003fLL) == 0x02c00000000LL))
1093 {
5ea2bd7f 1094 /* alloc - start of a regular function. */
16461d7d
KB
1095 int sor = (int) ((instr & 0x00078000000LL) >> 27);
1096 int sol = (int) ((instr & 0x00007f00000LL) >> 20);
1097 int sof = (int) ((instr & 0x000000fe000LL) >> 13);
16461d7d 1098 int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
004d836a
JJ
1099
1100 /* Verify that the current cfm matches what we think is the
1101 function start. If we have somehow jumped within a function,
1102 we do not want to interpret the prologue and calculate the
1103 addresses of various registers such as the return address.
1104 We will instead treat the frame as frameless. */
1105 if (!next_frame ||
1106 (sof == (cache->cfm & 0x7f) &&
1107 sol == ((cache->cfm >> 7) & 0x7f)))
1108 frameless = 0;
1109
16461d7d
KB
1110 cfm_reg = rN;
1111 last_prologue_pc = next_pc;
1112 pc = next_pc;
1113 }
1114 else
58ab00f9 1115 {
5ea2bd7f
JJ
1116 /* Look for a leaf routine. */
1117 if (pc < lim_pc && next_pc
1118 && (it == I || it == M)
1119 && ((instr & 0x1ee00000000LL) == 0x10800000000LL))
1120 {
1121 /* adds rN = imm14, rM (or mov rN, rM when imm14 is 0) */
1122 int imm = (int) ((((instr & 0x01000000000LL) ? -1 : 0) << 13)
1123 | ((instr & 0x001f8000000LL) >> 20)
1124 | ((instr & 0x000000fe000LL) >> 13));
1125 int rM = (int) ((instr & 0x00007f00000LL) >> 20);
1126 int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
1127 int qp = (int) (instr & 0x0000000003fLL);
1128 if (qp == 0 && rN == 2 && imm == 0 && rM == 12 && fp_reg == 0)
1129 {
1130 /* mov r2, r12 - beginning of leaf routine */
1131 fp_reg = rN;
5ea2bd7f
JJ
1132 last_prologue_pc = next_pc;
1133 }
1134 }
1135
1136 /* If we don't recognize a regular function or leaf routine, we are
1137 done. */
1138 if (!fp_reg)
1139 {
1140 pc = lim_pc;
1141 if (trust_limit)
1142 last_prologue_pc = lim_pc;
1143 }
58ab00f9 1144 }
16461d7d
KB
1145
1146 /* Loop, looking for prologue instructions, keeping track of
1147 where preserved registers were spilled. */
1148 while (pc < lim_pc)
1149 {
1150 next_pc = fetch_instruction (pc, &it, &instr);
1151 if (next_pc == 0)
1152 break;
1153
594706e6 1154 if (it == B && ((instr & 0x1e1f800003fLL) != 0x04000000000LL))
0927a22b 1155 {
102d615a
JJ
1156 /* Exit loop upon hitting a non-nop branch instruction. */
1157 if (trust_limit)
1158 lim_pc = pc;
1159 break;
1160 }
1161 else if (((instr & 0x3fLL) != 0LL) &&
1162 (frameless || ret_reg != 0))
1163 {
1164 /* Exit loop upon hitting a predicated instruction if
1165 we already have the return register or if we are frameless. */
5ea2bd7f
JJ
1166 if (trust_limit)
1167 lim_pc = pc;
0927a22b
KB
1168 break;
1169 }
1170 else if (it == I && ((instr & 0x1eff8000000LL) == 0x00188000000LL))
16461d7d
KB
1171 {
1172 /* Move from BR */
1173 int b2 = (int) ((instr & 0x0000000e000LL) >> 13);
1174 int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
1175 int qp = (int) (instr & 0x0000000003f);
1176
1177 if (qp == 0 && b2 == 0 && rN >= 32 && ret_reg == 0)
1178 {
1179 ret_reg = rN;
1180 last_prologue_pc = next_pc;
1181 }
1182 }
1183 else if ((it == I || it == M)
1184 && ((instr & 0x1ee00000000LL) == 0x10800000000LL))
1185 {
1186 /* adds rN = imm14, rM (or mov rN, rM when imm14 is 0) */
1187 int imm = (int) ((((instr & 0x01000000000LL) ? -1 : 0) << 13)
1188 | ((instr & 0x001f8000000LL) >> 20)
1189 | ((instr & 0x000000fe000LL) >> 13));
1190 int rM = (int) ((instr & 0x00007f00000LL) >> 20);
1191 int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
1192 int qp = (int) (instr & 0x0000000003fLL);
1193
1194 if (qp == 0 && rN >= 32 && imm == 0 && rM == 12 && fp_reg == 0)
1195 {
1196 /* mov rN, r12 */
1197 fp_reg = rN;
1198 last_prologue_pc = next_pc;
1199 }
1200 else if (qp == 0 && rN == 12 && rM == 12)
1201 {
1202 /* adds r12, -mem_stack_frame_size, r12 */
1203 mem_stack_frame_size -= imm;
1204 last_prologue_pc = next_pc;
1205 }
1206 else if (qp == 0 && rN == 2
1207 && ((rM == fp_reg && fp_reg != 0) || rM == 12))
1208 {
004d836a
JJ
1209 char buf[MAX_REGISTER_SIZE];
1210 CORE_ADDR saved_sp = 0;
16461d7d
KB
1211 /* adds r2, spilloffset, rFramePointer
1212 or
1213 adds r2, spilloffset, r12
1214
1215 Get ready for stf.spill or st8.spill instructions.
1216 The address to start spilling at is loaded into r2.
1217 FIXME: Why r2? That's what gcc currently uses; it
1218 could well be different for other compilers. */
1219
1220 /* Hmm... whether or not this will work will depend on
1221 where the pc is. If it's still early in the prologue
1222 this'll be wrong. FIXME */
004d836a
JJ
1223 if (next_frame)
1224 {
1225 frame_unwind_register (next_frame, sp_regnum, buf);
1226 saved_sp = extract_unsigned_integer (buf, 8);
1227 }
1228 spill_addr = saved_sp
16461d7d
KB
1229 + (rM == 12 ? 0 : mem_stack_frame_size)
1230 + imm;
1231 spill_reg = rN;
1232 last_prologue_pc = next_pc;
1233 }
5ea2bd7f
JJ
1234 else if (qp == 0 && rM >= 32 && rM < 40 && !instores[rM] &&
1235 rN < 256 && imm == 0)
1236 {
1237 /* mov rN, rM where rM is an input register */
1238 reg_contents[rN] = rM;
1239 last_prologue_pc = next_pc;
1240 }
1241 else if (frameless && qp == 0 && rN == fp_reg && imm == 0 &&
1242 rM == 2)
1243 {
1244 /* mov r12, r2 */
1245 last_prologue_pc = next_pc;
1246 break;
1247 }
16461d7d
KB
1248 }
1249 else if (it == M
1250 && ( ((instr & 0x1efc0000000LL) == 0x0eec0000000LL)
1251 || ((instr & 0x1ffc8000000LL) == 0x0cec0000000LL) ))
1252 {
1253 /* stf.spill [rN] = fM, imm9
1254 or
1255 stf.spill [rN] = fM */
1256
1257 int imm = imm9(instr);
1258 int rN = (int) ((instr & 0x00007f00000LL) >> 20);
1259 int fM = (int) ((instr & 0x000000fe000LL) >> 13);
1260 int qp = (int) (instr & 0x0000000003fLL);
1261 if (qp == 0 && rN == spill_reg && spill_addr != 0
1262 && ((2 <= fM && fM <= 5) || (16 <= fM && fM <= 31)))
1263 {
004d836a 1264 cache->saved_regs[IA64_FR0_REGNUM + fM] = spill_addr;
16461d7d 1265
594706e6 1266 if ((instr & 0x1efc0000000LL) == 0x0eec0000000LL)
16461d7d
KB
1267 spill_addr += imm;
1268 else
1269 spill_addr = 0; /* last one; must be done */
1270 last_prologue_pc = next_pc;
1271 }
1272 }
1273 else if ((it == M && ((instr & 0x1eff8000000LL) == 0x02110000000LL))
1274 || (it == I && ((instr & 0x1eff8000000LL) == 0x00050000000LL)) )
1275 {
1276 /* mov.m rN = arM
1277 or
1278 mov.i rN = arM */
1279
1280 int arM = (int) ((instr & 0x00007f00000LL) >> 20);
1281 int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
1282 int qp = (int) (instr & 0x0000000003fLL);
1283 if (qp == 0 && isScratch (rN) && arM == 36 /* ar.unat */)
1284 {
1285 /* We have something like "mov.m r3 = ar.unat". Remember the
1286 r3 (or whatever) and watch for a store of this register... */
1287 unat_save_reg = rN;
1288 last_prologue_pc = next_pc;
1289 }
1290 }
1291 else if (it == I && ((instr & 0x1eff8000000LL) == 0x00198000000LL))
1292 {
1293 /* mov rN = pr */
1294 int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
1295 int qp = (int) (instr & 0x0000000003fLL);
1296 if (qp == 0 && isScratch (rN))
1297 {
1298 pr_save_reg = rN;
1299 last_prologue_pc = next_pc;
1300 }
1301 }
1302 else if (it == M
1303 && ( ((instr & 0x1ffc8000000LL) == 0x08cc0000000LL)
1304 || ((instr & 0x1efc0000000LL) == 0x0acc0000000LL)))
1305 {
1306 /* st8 [rN] = rM
1307 or
1308 st8 [rN] = rM, imm9 */
1309 int rN = (int) ((instr & 0x00007f00000LL) >> 20);
1310 int rM = (int) ((instr & 0x000000fe000LL) >> 13);
1311 int qp = (int) (instr & 0x0000000003fLL);
5ea2bd7f 1312 int indirect = rM < 256 ? reg_contents[rM] : 0;
16461d7d
KB
1313 if (qp == 0 && rN == spill_reg && spill_addr != 0
1314 && (rM == unat_save_reg || rM == pr_save_reg))
1315 {
1316 /* We've found a spill of either the UNAT register or the PR
1317 register. (Well, not exactly; what we've actually found is
1318 a spill of the register that UNAT or PR was moved to).
1319 Record that fact and move on... */
1320 if (rM == unat_save_reg)
1321 {
1322 /* Track UNAT register */
004d836a 1323 cache->saved_regs[IA64_UNAT_REGNUM] = spill_addr;
16461d7d
KB
1324 unat_save_reg = 0;
1325 }
1326 else
1327 {
1328 /* Track PR register */
004d836a 1329 cache->saved_regs[IA64_PR_REGNUM] = spill_addr;
16461d7d
KB
1330 pr_save_reg = 0;
1331 }
1332 if ((instr & 0x1efc0000000LL) == 0x0acc0000000LL)
1333 /* st8 [rN] = rM, imm9 */
1334 spill_addr += imm9(instr);
1335 else
1336 spill_addr = 0; /* must be done spilling */
1337 last_prologue_pc = next_pc;
1338 }
0927a22b
KB
1339 else if (qp == 0 && 32 <= rM && rM < 40 && !instores[rM-32])
1340 {
1341 /* Allow up to one store of each input register. */
1342 instores[rM-32] = 1;
1343 last_prologue_pc = next_pc;
1344 }
5ea2bd7f
JJ
1345 else if (qp == 0 && 32 <= indirect && indirect < 40 &&
1346 !instores[indirect-32])
1347 {
1348 /* Allow an indirect store of an input register. */
1349 instores[indirect-32] = 1;
1350 last_prologue_pc = next_pc;
1351 }
0927a22b
KB
1352 }
1353 else if (it == M && ((instr & 0x1ff08000000LL) == 0x08c00000000LL))
1354 {
1355 /* One of
1356 st1 [rN] = rM
1357 st2 [rN] = rM
1358 st4 [rN] = rM
1359 st8 [rN] = rM
1360 Note that the st8 case is handled in the clause above.
1361
1362 Advance over stores of input registers. One store per input
1363 register is permitted. */
1364 int rM = (int) ((instr & 0x000000fe000LL) >> 13);
1365 int qp = (int) (instr & 0x0000000003fLL);
5ea2bd7f 1366 int indirect = rM < 256 ? reg_contents[rM] : 0;
0927a22b
KB
1367 if (qp == 0 && 32 <= rM && rM < 40 && !instores[rM-32])
1368 {
1369 instores[rM-32] = 1;
1370 last_prologue_pc = next_pc;
1371 }
5ea2bd7f
JJ
1372 else if (qp == 0 && 32 <= indirect && indirect < 40 &&
1373 !instores[indirect-32])
1374 {
1375 /* Allow an indirect store of an input register. */
1376 instores[indirect-32] = 1;
1377 last_prologue_pc = next_pc;
1378 }
0927a22b
KB
1379 }
1380 else if (it == M && ((instr & 0x1ff88000000LL) == 0x0cc80000000LL))
1381 {
1382 /* Either
1383 stfs [rN] = fM
1384 or
1385 stfd [rN] = fM
1386
1387 Advance over stores of floating point input registers. Again
1388 one store per register is permitted */
1389 int fM = (int) ((instr & 0x000000fe000LL) >> 13);
1390 int qp = (int) (instr & 0x0000000003fLL);
1391 if (qp == 0 && 8 <= fM && fM < 16 && !infpstores[fM - 8])
1392 {
1393 infpstores[fM-8] = 1;
1394 last_prologue_pc = next_pc;
1395 }
16461d7d
KB
1396 }
1397 else if (it == M
1398 && ( ((instr & 0x1ffc8000000LL) == 0x08ec0000000LL)
1399 || ((instr & 0x1efc0000000LL) == 0x0aec0000000LL)))
1400 {
1401 /* st8.spill [rN] = rM
1402 or
1403 st8.spill [rN] = rM, imm9 */
1404 int rN = (int) ((instr & 0x00007f00000LL) >> 20);
1405 int rM = (int) ((instr & 0x000000fe000LL) >> 13);
1406 int qp = (int) (instr & 0x0000000003fLL);
1407 if (qp == 0 && rN == spill_reg && 4 <= rM && rM <= 7)
1408 {
1409 /* We've found a spill of one of the preserved general purpose
1410 regs. Record the spill address and advance the spill
1411 register if appropriate. */
004d836a 1412 cache->saved_regs[IA64_GR0_REGNUM + rM] = spill_addr;
16461d7d
KB
1413 if ((instr & 0x1efc0000000LL) == 0x0aec0000000LL)
1414 /* st8.spill [rN] = rM, imm9 */
1415 spill_addr += imm9(instr);
1416 else
1417 spill_addr = 0; /* Done spilling */
1418 last_prologue_pc = next_pc;
1419 }
1420 }
16461d7d
KB
1421
1422 pc = next_pc;
1423 }
1424
004d836a
JJ
1425 /* If not frameless and we aren't called by skip_prologue, then we need to calculate
1426 registers for the previous frame which will be needed later. */
16461d7d 1427
004d836a 1428 if (!frameless && next_frame)
da50a4b7 1429 {
004d836a
JJ
1430 /* Extract the size of the rotating portion of the stack
1431 frame and the register rename base from the current
1432 frame marker. */
1433 cfm = cache->cfm;
1434 sor = cache->sor;
1435 sof = cache->sof;
1436 sol = cache->sol;
1437 rrb_gr = (cfm >> 18) & 0x7f;
1438
1439 /* Find the bof (beginning of frame). */
1440 bof = rse_address_add (cache->bsp, -sof);
1441
1442 for (i = 0, addr = bof;
1443 i < sof;
1444 i++, addr += 8)
1445 {
1446 if (IS_NaT_COLLECTION_ADDR (addr))
1447 {
1448 addr += 8;
1449 }
1450 if (i+32 == cfm_reg)
1451 cache->saved_regs[IA64_CFM_REGNUM] = addr;
1452 if (i+32 == ret_reg)
1453 cache->saved_regs[IA64_VRAP_REGNUM] = addr;
1454 if (i+32 == fp_reg)
1455 cache->saved_regs[IA64_VFP_REGNUM] = addr;
1456 }
16461d7d 1457
004d836a
JJ
1458 /* For the previous argument registers we require the previous bof.
1459 If we can't find the previous cfm, then we can do nothing. */
4afcc598 1460 cfm = 0;
004d836a
JJ
1461 if (cache->saved_regs[IA64_CFM_REGNUM] != 0)
1462 {
1463 cfm = read_memory_integer (cache->saved_regs[IA64_CFM_REGNUM], 8);
4afcc598
JJ
1464 }
1465 else if (cfm_reg != 0)
1466 {
1467 frame_unwind_register (next_frame, cfm_reg, buf);
1468 cfm = extract_unsigned_integer (buf, 8);
1469 }
1470 cache->prev_cfm = cfm;
1471
1472 if (cfm != 0)
1473 {
004d836a
JJ
1474 sor = ((cfm >> 14) & 0xf) * 8;
1475 sof = (cfm & 0x7f);
1476 sol = (cfm >> 7) & 0x7f;
1477 rrb_gr = (cfm >> 18) & 0x7f;
1478
1479 /* The previous bof only requires subtraction of the sol (size of locals)
1480 due to the overlap between output and input of subsequent frames. */
1481 bof = rse_address_add (bof, -sol);
1482
1483 for (i = 0, addr = bof;
1484 i < sof;
1485 i++, addr += 8)
1486 {
1487 if (IS_NaT_COLLECTION_ADDR (addr))
1488 {
1489 addr += 8;
1490 }
1491 if (i < sor)
1492 cache->saved_regs[IA64_GR32_REGNUM + ((i + (sor - rrb_gr)) % sor)]
1493 = addr;
1494 else
1495 cache->saved_regs[IA64_GR32_REGNUM + i] = addr;
1496 }
1497
1498 }
1499 }
1500
5ea2bd7f
JJ
1501 /* Try and trust the lim_pc value whenever possible. */
1502 if (trust_limit && lim_pc >= last_prologue_pc)
004d836a
JJ
1503 last_prologue_pc = lim_pc;
1504
1505 cache->frameless = frameless;
1506 cache->after_prologue = last_prologue_pc;
1507 cache->mem_stack_frame_size = mem_stack_frame_size;
1508 cache->fp_reg = fp_reg;
5ea2bd7f 1509
16461d7d
KB
1510 return last_prologue_pc;
1511}
1512
1513CORE_ADDR
1514ia64_skip_prologue (CORE_ADDR pc)
1515{
004d836a
JJ
1516 struct ia64_frame_cache cache;
1517 cache.base = 0;
1518 cache.after_prologue = 0;
1519 cache.cfm = 0;
1520 cache.bsp = 0;
1521
1522 /* Call examine_prologue with - as third argument since we don't have a next frame pointer to send. */
1523 return examine_prologue (pc, pc+1024, 0, &cache);
16461d7d
KB
1524}
1525
004d836a
JJ
1526
1527/* Normal frames. */
1528
1529static struct ia64_frame_cache *
1530ia64_frame_cache (struct frame_info *next_frame, void **this_cache)
16461d7d 1531{
004d836a
JJ
1532 struct ia64_frame_cache *cache;
1533 char buf[8];
1534 CORE_ADDR cfm, sof, sol, bsp, psr;
1535 int i;
16461d7d 1536
004d836a
JJ
1537 if (*this_cache)
1538 return *this_cache;
16461d7d 1539
004d836a
JJ
1540 cache = ia64_alloc_frame_cache ();
1541 *this_cache = cache;
16461d7d 1542
004d836a
JJ
1543 frame_unwind_register (next_frame, sp_regnum, buf);
1544 cache->saved_sp = extract_unsigned_integer (buf, 8);
16461d7d 1545
004d836a
JJ
1546 /* We always want the bsp to point to the end of frame.
1547 This way, we can always get the beginning of frame (bof)
1548 by subtracting frame size. */
1549 frame_unwind_register (next_frame, IA64_BSP_REGNUM, buf);
1550 cache->bsp = extract_unsigned_integer (buf, 8);
1551
1552 frame_unwind_register (next_frame, IA64_PSR_REGNUM, buf);
1553 psr = extract_unsigned_integer (buf, 8);
1554
1555 frame_unwind_register (next_frame, IA64_CFM_REGNUM, buf);
1556 cfm = extract_unsigned_integer (buf, 8);
1557
1558 cache->sof = (cfm & 0x7f);
1559 cache->sol = (cfm >> 7) & 0x7f;
1560 cache->sor = ((cfm >> 14) & 0xf) * 8;
1561
1562 cache->cfm = cfm;
1563
93d42b30 1564 cache->pc = frame_func_unwind (next_frame, NORMAL_FRAME);
004d836a
JJ
1565
1566 if (cache->pc != 0)
1567 examine_prologue (cache->pc, frame_pc_unwind (next_frame), next_frame, cache);
1568
1569 cache->base = cache->saved_sp + cache->mem_stack_frame_size;
1570
1571 return cache;
16461d7d
KB
1572}
1573
a78f21af 1574static void
004d836a
JJ
1575ia64_frame_this_id (struct frame_info *next_frame, void **this_cache,
1576 struct frame_id *this_id)
16461d7d 1577{
004d836a
JJ
1578 struct ia64_frame_cache *cache =
1579 ia64_frame_cache (next_frame, this_cache);
16461d7d 1580
c5a27d9c 1581 /* If outermost frame, mark with null frame id. */
004d836a 1582 if (cache->base == 0)
c5a27d9c
JJ
1583 (*this_id) = null_frame_id;
1584 else
1585 (*this_id) = frame_id_build_special (cache->base, cache->pc, cache->bsp);
4afcc598
JJ
1586 if (gdbarch_debug >= 1)
1587 fprintf_unfiltered (gdb_stdlog,
78ced177
JJ
1588 "regular frame id: code 0x%s, stack 0x%s, special 0x%s, next_frame %p\n",
1589 paddr_nz (this_id->code_addr),
1590 paddr_nz (this_id->stack_addr),
1591 paddr_nz (cache->bsp), next_frame);
004d836a 1592}
244bc108 1593
004d836a
JJ
1594static void
1595ia64_frame_prev_register (struct frame_info *next_frame, void **this_cache,
1596 int regnum, int *optimizedp,
1597 enum lval_type *lvalp, CORE_ADDR *addrp,
88d82102 1598 int *realnump, gdb_byte *valuep)
004d836a 1599{
088568da 1600 struct gdbarch *gdbarch = get_frame_arch (next_frame);
004d836a
JJ
1601 struct ia64_frame_cache *cache =
1602 ia64_frame_cache (next_frame, this_cache);
1603 char dummy_valp[MAX_REGISTER_SIZE];
1604 char buf[8];
1605
1606 gdb_assert (regnum >= 0);
244bc108 1607
004d836a 1608 if (!target_has_registers)
8a3fe4f8 1609 error (_("No registers."));
244bc108 1610
004d836a
JJ
1611 *optimizedp = 0;
1612 *addrp = 0;
1613 *lvalp = not_lval;
1614 *realnump = -1;
244bc108 1615
004d836a
JJ
1616 /* Rather than check each time if valuep is non-null, supply a dummy buffer
1617 when valuep is not supplied. */
1618 if (!valuep)
1619 valuep = dummy_valp;
1620
088568da 1621 memset (valuep, 0, register_size (gdbarch, regnum));
004d836a 1622
088568da 1623 if (regnum == gdbarch_sp_regnum (gdbarch))
16461d7d
KB
1624 {
1625 /* Handle SP values for all frames but the topmost. */
088568da 1626 store_unsigned_integer (valuep, register_size (gdbarch, regnum),
004d836a 1627 cache->base);
16461d7d
KB
1628 }
1629 else if (regnum == IA64_BSP_REGNUM)
1630 {
004d836a
JJ
1631 char cfm_valuep[MAX_REGISTER_SIZE];
1632 int cfm_optim;
1633 int cfm_realnum;
1634 enum lval_type cfm_lval;
1635 CORE_ADDR cfm_addr;
1636 CORE_ADDR bsp, prev_cfm, prev_bsp;
1637
1638 /* We want to calculate the previous bsp as the end of the previous register stack frame.
1639 This corresponds to what the hardware bsp register will be if we pop the frame
1640 back which is why we might have been called. We know the beginning of the current
aa2a9a3c 1641 frame is cache->bsp - cache->sof. This value in the previous frame points to
004d836a
JJ
1642 the start of the output registers. We can calculate the end of that frame by adding
1643 the size of output (sof (size of frame) - sol (size of locals)). */
1644 ia64_frame_prev_register (next_frame, this_cache, IA64_CFM_REGNUM,
1645 &cfm_optim, &cfm_lval, &cfm_addr, &cfm_realnum, cfm_valuep);
1646 prev_cfm = extract_unsigned_integer (cfm_valuep, 8);
1647
1648 bsp = rse_address_add (cache->bsp, -(cache->sof));
1649 prev_bsp = rse_address_add (bsp, (prev_cfm & 0x7f) - ((prev_cfm >> 7) & 0x7f));
1650
088568da 1651 store_unsigned_integer (valuep, register_size (gdbarch, regnum),
004d836a
JJ
1652 prev_bsp);
1653 }
1654 else if (regnum == IA64_CFM_REGNUM)
1655 {
4afcc598
JJ
1656 CORE_ADDR addr = cache->saved_regs[IA64_CFM_REGNUM];
1657
1658 if (addr != 0)
004d836a 1659 {
4afcc598
JJ
1660 *lvalp = lval_memory;
1661 *addrp = addr;
088568da 1662 read_memory (addr, valuep, register_size (gdbarch, regnum));
004d836a 1663 }
4afcc598 1664 else if (cache->prev_cfm)
088568da 1665 store_unsigned_integer (valuep, register_size (gdbarch, regnum), cache->prev_cfm);
4afcc598 1666 else if (cache->frameless)
004d836a 1667 {
4afcc598
JJ
1668 CORE_ADDR cfm = 0;
1669 frame_unwind_register (next_frame, IA64_PFS_REGNUM, valuep);
004d836a 1670 }
16461d7d
KB
1671 }
1672 else if (regnum == IA64_VFP_REGNUM)
1673 {
1674 /* If the function in question uses an automatic register (r32-r127)
1675 for the frame pointer, it'll be found by ia64_find_saved_register()
1676 above. If the function lacks one of these frame pointers, we can
004d836a
JJ
1677 still provide a value since we know the size of the frame. */
1678 CORE_ADDR vfp = cache->base;
088568da 1679 store_unsigned_integer (valuep, register_size (gdbarch, IA64_VFP_REGNUM), vfp);
16461d7d 1680 }
004d836a 1681 else if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
16461d7d 1682 {
004d836a 1683 char pr_valuep[MAX_REGISTER_SIZE];
16461d7d 1684 int pr_optim;
004d836a 1685 int pr_realnum;
16461d7d
KB
1686 enum lval_type pr_lval;
1687 CORE_ADDR pr_addr;
004d836a
JJ
1688 ULONGEST prN_val;
1689 ia64_frame_prev_register (next_frame, this_cache, IA64_PR_REGNUM,
1690 &pr_optim, &pr_lval, &pr_addr, &pr_realnum, pr_valuep);
1691 if (VP16_REGNUM <= regnum && regnum <= VP63_REGNUM)
3a854e23
KB
1692 {
1693 /* Fetch predicate register rename base from current frame
004d836a
JJ
1694 marker for this frame. */
1695 int rrb_pr = (cache->cfm >> 32) & 0x3f;
3a854e23 1696
004d836a
JJ
1697 /* Adjust the register number to account for register rotation. */
1698 regnum = VP16_REGNUM
1699 + ((regnum - VP16_REGNUM) + rrb_pr) % 48;
3a854e23 1700 }
004d836a
JJ
1701 prN_val = extract_bit_field ((unsigned char *) pr_valuep,
1702 regnum - VP0_REGNUM, 1);
088568da 1703 store_unsigned_integer (valuep, register_size (gdbarch, regnum), prN_val);
16461d7d
KB
1704 }
1705 else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT31_REGNUM)
1706 {
004d836a 1707 char unat_valuep[MAX_REGISTER_SIZE];
16461d7d 1708 int unat_optim;
004d836a 1709 int unat_realnum;
16461d7d
KB
1710 enum lval_type unat_lval;
1711 CORE_ADDR unat_addr;
004d836a
JJ
1712 ULONGEST unatN_val;
1713 ia64_frame_prev_register (next_frame, this_cache, IA64_UNAT_REGNUM,
1714 &unat_optim, &unat_lval, &unat_addr, &unat_realnum, unat_valuep);
1715 unatN_val = extract_bit_field ((unsigned char *) unat_valuep,
16461d7d 1716 regnum - IA64_NAT0_REGNUM, 1);
088568da 1717 store_unsigned_integer (valuep, register_size (gdbarch, regnum),
16461d7d 1718 unatN_val);
16461d7d
KB
1719 }
1720 else if (IA64_NAT32_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
1721 {
1722 int natval = 0;
1723 /* Find address of general register corresponding to nat bit we're
004d836a
JJ
1724 interested in. */
1725 CORE_ADDR gr_addr;
244bc108 1726
004d836a
JJ
1727 gr_addr = cache->saved_regs[regnum - IA64_NAT0_REGNUM
1728 + IA64_GR0_REGNUM];
1729 if (gr_addr != 0)
244bc108 1730 {
004d836a 1731 /* Compute address of nat collection bits. */
16461d7d 1732 CORE_ADDR nat_addr = gr_addr | 0x1f8;
004d836a 1733 CORE_ADDR bsp;
16461d7d
KB
1734 CORE_ADDR nat_collection;
1735 int nat_bit;
1736 /* If our nat collection address is bigger than bsp, we have to get
1737 the nat collection from rnat. Otherwise, we fetch the nat
004d836a
JJ
1738 collection from the computed address. */
1739 frame_unwind_register (next_frame, IA64_BSP_REGNUM, buf);
1740 bsp = extract_unsigned_integer (buf, 8);
16461d7d 1741 if (nat_addr >= bsp)
004d836a
JJ
1742 {
1743 frame_unwind_register (next_frame, IA64_RNAT_REGNUM, buf);
1744 nat_collection = extract_unsigned_integer (buf, 8);
1745 }
16461d7d
KB
1746 else
1747 nat_collection = read_memory_integer (nat_addr, 8);
1748 nat_bit = (gr_addr >> 3) & 0x3f;
1749 natval = (nat_collection >> nat_bit) & 1;
1750 }
004d836a 1751
088568da 1752 store_unsigned_integer (valuep, register_size (gdbarch, regnum), natval);
244bc108
KB
1753 }
1754 else if (regnum == IA64_IP_REGNUM)
1755 {
004d836a 1756 CORE_ADDR pc = 0;
4afcc598 1757 CORE_ADDR addr = cache->saved_regs[IA64_VRAP_REGNUM];
004d836a 1758
4afcc598 1759 if (addr != 0)
004d836a 1760 {
4afcc598
JJ
1761 *lvalp = lval_memory;
1762 *addrp = addr;
088568da 1763 read_memory (addr, buf, register_size (gdbarch, IA64_IP_REGNUM));
004d836a
JJ
1764 pc = extract_unsigned_integer (buf, 8);
1765 }
4afcc598 1766 else if (cache->frameless)
004d836a 1767 {
4afcc598
JJ
1768 frame_unwind_register (next_frame, IA64_BR0_REGNUM, buf);
1769 pc = extract_unsigned_integer (buf, 8);
244bc108 1770 }
004d836a
JJ
1771 pc &= ~0xf;
1772 store_unsigned_integer (valuep, 8, pc);
244bc108 1773 }
004d836a 1774 else if (regnum == IA64_PSR_REGNUM)
244bc108 1775 {
4afcc598
JJ
1776 /* We don't know how to get the complete previous PSR, but we need it for
1777 the slot information when we unwind the pc (pc is formed of IP register
1778 plus slot information from PSR). To get the previous slot information,
1779 we mask it off the return address. */
004d836a
JJ
1780 ULONGEST slot_num = 0;
1781 CORE_ADDR pc= 0;
1782 CORE_ADDR psr = 0;
4afcc598 1783 CORE_ADDR addr = cache->saved_regs[IA64_VRAP_REGNUM];
004d836a
JJ
1784
1785 frame_unwind_register (next_frame, IA64_PSR_REGNUM, buf);
1786 psr = extract_unsigned_integer (buf, 8);
1787
4afcc598 1788 if (addr != 0)
244bc108 1789 {
4afcc598
JJ
1790 *lvalp = lval_memory;
1791 *addrp = addr;
088568da 1792 read_memory (addr, buf, register_size (gdbarch, IA64_IP_REGNUM));
004d836a 1793 pc = extract_unsigned_integer (buf, 8);
244bc108 1794 }
4afcc598 1795 else if (cache->frameless)
004d836a 1796 {
4afcc598
JJ
1797 CORE_ADDR pc;
1798 frame_unwind_register (next_frame, IA64_BR0_REGNUM, buf);
1799 pc = extract_unsigned_integer (buf, 8);
004d836a
JJ
1800 }
1801 psr &= ~(3LL << 41);
1802 slot_num = pc & 0x3LL;
1803 psr |= (CORE_ADDR)slot_num << 41;
1804 store_unsigned_integer (valuep, 8, psr);
1805 }
4afcc598
JJ
1806 else if (regnum == IA64_BR0_REGNUM)
1807 {
1808 CORE_ADDR br0 = 0;
1809 CORE_ADDR addr = cache->saved_regs[IA64_BR0_REGNUM];
1810 if (addr != 0)
1811 {
1812 *lvalp = lval_memory;
1813 *addrp = addr;
088568da 1814 read_memory (addr, buf, register_size (gdbarch, IA64_BR0_REGNUM));
4afcc598
JJ
1815 br0 = extract_unsigned_integer (buf, 8);
1816 }
1817 store_unsigned_integer (valuep, 8, br0);
1818 }
004d836a
JJ
1819 else if ((regnum >= IA64_GR32_REGNUM && regnum <= IA64_GR127_REGNUM) ||
1820 (regnum >= V32_REGNUM && regnum <= V127_REGNUM))
1821 {
1822 CORE_ADDR addr = 0;
1823 if (regnum >= V32_REGNUM)
1824 regnum = IA64_GR32_REGNUM + (regnum - V32_REGNUM);
1825 addr = cache->saved_regs[regnum];
244bc108
KB
1826 if (addr != 0)
1827 {
004d836a
JJ
1828 *lvalp = lval_memory;
1829 *addrp = addr;
088568da 1830 read_memory (addr, valuep, register_size (gdbarch, regnum));
244bc108 1831 }
004d836a 1832 else if (cache->frameless)
244bc108 1833 {
004d836a
JJ
1834 char r_valuep[MAX_REGISTER_SIZE];
1835 int r_optim;
1836 int r_realnum;
1837 enum lval_type r_lval;
1838 CORE_ADDR r_addr;
1839 CORE_ADDR prev_cfm, prev_bsp, prev_bof;
1840 CORE_ADDR addr = 0;
1841 if (regnum >= V32_REGNUM)
1842 regnum = IA64_GR32_REGNUM + (regnum - V32_REGNUM);
1843 ia64_frame_prev_register (next_frame, this_cache, IA64_CFM_REGNUM,
1844 &r_optim, &r_lval, &r_addr, &r_realnum, r_valuep);
1845 prev_cfm = extract_unsigned_integer (r_valuep, 8);
1846 ia64_frame_prev_register (next_frame, this_cache, IA64_BSP_REGNUM,
1847 &r_optim, &r_lval, &r_addr, &r_realnum, r_valuep);
1848 prev_bsp = extract_unsigned_integer (r_valuep, 8);
1849 prev_bof = rse_address_add (prev_bsp, -(prev_cfm & 0x7f));
1850
1851 addr = rse_address_add (prev_bof, (regnum - IA64_GR32_REGNUM));
1852 *lvalp = lval_memory;
1853 *addrp = addr;
088568da 1854 read_memory (addr, valuep, register_size (gdbarch, regnum));
244bc108 1855 }
16461d7d
KB
1856 }
1857 else
1858 {
004d836a 1859 CORE_ADDR addr = 0;
3a854e23
KB
1860 if (IA64_FR32_REGNUM <= regnum && regnum <= IA64_FR127_REGNUM)
1861 {
1862 /* Fetch floating point register rename base from current
004d836a
JJ
1863 frame marker for this frame. */
1864 int rrb_fr = (cache->cfm >> 25) & 0x7f;
3a854e23
KB
1865
1866 /* Adjust the floating point register number to account for
004d836a 1867 register rotation. */
3a854e23
KB
1868 regnum = IA64_FR32_REGNUM
1869 + ((regnum - IA64_FR32_REGNUM) + rrb_fr) % 96;
1870 }
1871
004d836a
JJ
1872 /* If we have stored a memory address, access the register. */
1873 addr = cache->saved_regs[regnum];
1874 if (addr != 0)
1875 {
1876 *lvalp = lval_memory;
1877 *addrp = addr;
088568da 1878 read_memory (addr, valuep, register_size (gdbarch, regnum));
004d836a
JJ
1879 }
1880 /* Otherwise, punt and get the current value of the register. */
1881 else
1882 frame_unwind_register (next_frame, regnum, valuep);
16461d7d 1883 }
4afcc598
JJ
1884
1885 if (gdbarch_debug >= 1)
1886 fprintf_unfiltered (gdb_stdlog,
78ced177 1887 "regular prev register <%d> <%s> is 0x%s\n", regnum,
4afcc598 1888 (((unsigned) regnum <= IA64_NAT127_REGNUM)
78ced177
JJ
1889 ? ia64_register_names[regnum] : "r??"),
1890 paddr_nz (extract_unsigned_integer (valuep, 8)));
16461d7d 1891}
004d836a
JJ
1892
1893static const struct frame_unwind ia64_frame_unwind =
1894{
1895 NORMAL_FRAME,
1896 &ia64_frame_this_id,
1897 &ia64_frame_prev_register
1898};
1899
1900static const struct frame_unwind *
1901ia64_frame_sniffer (struct frame_info *next_frame)
1902{
1903 return &ia64_frame_unwind;
1904}
1905
1906/* Signal trampolines. */
1907
1908static void
2685572f
UW
1909ia64_sigtramp_frame_init_saved_regs (struct frame_info *next_frame,
1910 struct ia64_frame_cache *cache)
004d836a 1911{
2685572f
UW
1912 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (next_frame));
1913
1914 if (tdep->sigcontext_register_address)
004d836a
JJ
1915 {
1916 int regno;
1917
1918 cache->saved_regs[IA64_VRAP_REGNUM] =
2685572f 1919 tdep->sigcontext_register_address (cache->base, IA64_IP_REGNUM);
004d836a 1920 cache->saved_regs[IA64_CFM_REGNUM] =
2685572f 1921 tdep->sigcontext_register_address (cache->base, IA64_CFM_REGNUM);
004d836a 1922 cache->saved_regs[IA64_PSR_REGNUM] =
2685572f 1923 tdep->sigcontext_register_address (cache->base, IA64_PSR_REGNUM);
004d836a 1924 cache->saved_regs[IA64_BSP_REGNUM] =
2685572f 1925 tdep->sigcontext_register_address (cache->base, IA64_BSP_REGNUM);
004d836a 1926 cache->saved_regs[IA64_RNAT_REGNUM] =
2685572f 1927 tdep->sigcontext_register_address (cache->base, IA64_RNAT_REGNUM);
004d836a 1928 cache->saved_regs[IA64_CCV_REGNUM] =
2685572f 1929 tdep->sigcontext_register_address (cache->base, IA64_CCV_REGNUM);
004d836a 1930 cache->saved_regs[IA64_UNAT_REGNUM] =
2685572f 1931 tdep->sigcontext_register_address (cache->base, IA64_UNAT_REGNUM);
004d836a 1932 cache->saved_regs[IA64_FPSR_REGNUM] =
2685572f 1933 tdep->sigcontext_register_address (cache->base, IA64_FPSR_REGNUM);
004d836a 1934 cache->saved_regs[IA64_PFS_REGNUM] =
2685572f 1935 tdep->sigcontext_register_address (cache->base, IA64_PFS_REGNUM);
004d836a 1936 cache->saved_regs[IA64_LC_REGNUM] =
2685572f 1937 tdep->sigcontext_register_address (cache->base, IA64_LC_REGNUM);
004d836a 1938 for (regno = IA64_GR1_REGNUM; regno <= IA64_GR31_REGNUM; regno++)
4afcc598 1939 cache->saved_regs[regno] =
2685572f 1940 tdep->sigcontext_register_address (cache->base, regno);
004d836a
JJ
1941 for (regno = IA64_BR0_REGNUM; regno <= IA64_BR7_REGNUM; regno++)
1942 cache->saved_regs[regno] =
2685572f 1943 tdep->sigcontext_register_address (cache->base, regno);
932644f0 1944 for (regno = IA64_FR2_REGNUM; regno <= IA64_FR31_REGNUM; regno++)
004d836a 1945 cache->saved_regs[regno] =
2685572f 1946 tdep->sigcontext_register_address (cache->base, regno);
004d836a
JJ
1947 }
1948}
1949
1950static struct ia64_frame_cache *
1951ia64_sigtramp_frame_cache (struct frame_info *next_frame, void **this_cache)
1952{
1953 struct ia64_frame_cache *cache;
1954 CORE_ADDR addr;
1955 char buf[8];
1956 int i;
1957
1958 if (*this_cache)
1959 return *this_cache;
1960
1961 cache = ia64_alloc_frame_cache ();
1962
1963 frame_unwind_register (next_frame, sp_regnum, buf);
4afcc598
JJ
1964 /* Note that frame size is hard-coded below. We cannot calculate it
1965 via prologue examination. */
1966 cache->base = extract_unsigned_integer (buf, 8) + 16;
1967
1968 frame_unwind_register (next_frame, IA64_BSP_REGNUM, buf);
1969 cache->bsp = extract_unsigned_integer (buf, 8);
1970
1971 frame_unwind_register (next_frame, IA64_CFM_REGNUM, buf);
1972 cache->cfm = extract_unsigned_integer (buf, 8);
1973 cache->sof = cache->cfm & 0x7f;
004d836a 1974
2685572f 1975 ia64_sigtramp_frame_init_saved_regs (next_frame, cache);
004d836a
JJ
1976
1977 *this_cache = cache;
1978 return cache;
1979}
1980
1981static void
1982ia64_sigtramp_frame_this_id (struct frame_info *next_frame,
1983 void **this_cache, struct frame_id *this_id)
1984{
1985 struct ia64_frame_cache *cache =
1986 ia64_sigtramp_frame_cache (next_frame, this_cache);
1987
4afcc598
JJ
1988 (*this_id) = frame_id_build_special (cache->base, frame_pc_unwind (next_frame), cache->bsp);
1989 if (gdbarch_debug >= 1)
1990 fprintf_unfiltered (gdb_stdlog,
78ced177
JJ
1991 "sigtramp frame id: code 0x%s, stack 0x%s, special 0x%s, next_frame %p\n",
1992 paddr_nz (this_id->code_addr),
1993 paddr_nz (this_id->stack_addr),
1994 paddr_nz (cache->bsp), next_frame);
004d836a
JJ
1995}
1996
1997static void
1998ia64_sigtramp_frame_prev_register (struct frame_info *next_frame,
1999 void **this_cache,
2000 int regnum, int *optimizedp,
2001 enum lval_type *lvalp, CORE_ADDR *addrp,
88d82102 2002 int *realnump, gdb_byte *valuep)
004d836a 2003{
4afcc598
JJ
2004 char dummy_valp[MAX_REGISTER_SIZE];
2005 char buf[MAX_REGISTER_SIZE];
2006
088568da 2007 struct gdbarch *gdbarch = get_frame_arch (next_frame);
4afcc598
JJ
2008 struct ia64_frame_cache *cache =
2009 ia64_sigtramp_frame_cache (next_frame, this_cache);
2010
2011 gdb_assert (regnum >= 0);
2012
2013 if (!target_has_registers)
8a3fe4f8 2014 error (_("No registers."));
4afcc598
JJ
2015
2016 *optimizedp = 0;
2017 *addrp = 0;
2018 *lvalp = not_lval;
2019 *realnump = -1;
2020
2021 /* Rather than check each time if valuep is non-null, supply a dummy buffer
2022 when valuep is not supplied. */
2023 if (!valuep)
2024 valuep = dummy_valp;
2025
088568da 2026 memset (valuep, 0, register_size (gdbarch, regnum));
4afcc598
JJ
2027
2028 if (regnum == IA64_IP_REGNUM)
2029 {
2030 CORE_ADDR pc = 0;
2031 CORE_ADDR addr = cache->saved_regs[IA64_VRAP_REGNUM];
2032
2033 if (addr != 0)
2034 {
2035 *lvalp = lval_memory;
2036 *addrp = addr;
088568da 2037 read_memory (addr, buf, register_size (gdbarch, IA64_IP_REGNUM));
4afcc598
JJ
2038 pc = extract_unsigned_integer (buf, 8);
2039 }
2040 pc &= ~0xf;
2041 store_unsigned_integer (valuep, 8, pc);
2042 }
2043 else if ((regnum >= IA64_GR32_REGNUM && regnum <= IA64_GR127_REGNUM) ||
2044 (regnum >= V32_REGNUM && regnum <= V127_REGNUM))
2045 {
2046 CORE_ADDR addr = 0;
2047 if (regnum >= V32_REGNUM)
2048 regnum = IA64_GR32_REGNUM + (regnum - V32_REGNUM);
2049 addr = cache->saved_regs[regnum];
2050 if (addr != 0)
2051 {
2052 *lvalp = lval_memory;
2053 *addrp = addr;
088568da 2054 read_memory (addr, valuep, register_size (gdbarch, regnum));
4afcc598
JJ
2055 }
2056 }
2057 else
2058 {
2059 /* All other registers not listed above. */
2060 CORE_ADDR addr = cache->saved_regs[regnum];
2061 if (addr != 0)
2062 {
2063 *lvalp = lval_memory;
2064 *addrp = addr;
088568da 2065 read_memory (addr, valuep, register_size (gdbarch, regnum));
4afcc598
JJ
2066 }
2067 }
004d836a 2068
4afcc598
JJ
2069 if (gdbarch_debug >= 1)
2070 fprintf_unfiltered (gdb_stdlog,
78ced177 2071 "sigtramp prev register <%s> is 0x%s\n",
c5a27d9c
JJ
2072 (regnum < IA64_GR32_REGNUM
2073 || (regnum > IA64_GR127_REGNUM
2074 && regnum < LAST_PSEUDO_REGNUM))
2075 ? ia64_register_names[regnum]
2076 : (regnum < LAST_PSEUDO_REGNUM
2077 ? ia64_register_names[regnum-IA64_GR32_REGNUM+V32_REGNUM]
2078 : "OUT_OF_RANGE"),
78ced177 2079 paddr_nz (extract_unsigned_integer (valuep, 8)));
004d836a
JJ
2080}
2081
2082static const struct frame_unwind ia64_sigtramp_frame_unwind =
2083{
2084 SIGTRAMP_FRAME,
2085 ia64_sigtramp_frame_this_id,
2086 ia64_sigtramp_frame_prev_register
2087};
2088
2089static const struct frame_unwind *
2090ia64_sigtramp_frame_sniffer (struct frame_info *next_frame)
2091{
74174d2e
UW
2092 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (next_frame));
2093 if (tdep->pc_in_sigtramp)
2094 {
2095 CORE_ADDR pc = frame_pc_unwind (next_frame);
004d836a 2096
74174d2e
UW
2097 if (tdep->pc_in_sigtramp (pc))
2098 return &ia64_sigtramp_frame_unwind;
2099 }
004d836a
JJ
2100
2101 return NULL;
2102}
2103\f
2104
2105static CORE_ADDR
2106ia64_frame_base_address (struct frame_info *next_frame, void **this_cache)
2107{
2108 struct ia64_frame_cache *cache =
2109 ia64_frame_cache (next_frame, this_cache);
2110
2111 return cache->base;
2112}
2113
2114static const struct frame_base ia64_frame_base =
2115{
2116 &ia64_frame_unwind,
2117 ia64_frame_base_address,
2118 ia64_frame_base_address,
2119 ia64_frame_base_address
2120};
16461d7d 2121
968d1cb4
JJ
2122#ifdef HAVE_LIBUNWIND_IA64_H
2123
2124struct ia64_unwind_table_entry
2125 {
2126 unw_word_t start_offset;
2127 unw_word_t end_offset;
2128 unw_word_t info_offset;
2129 };
2130
2131static __inline__ uint64_t
2132ia64_rse_slot_num (uint64_t addr)
2133{
2134 return (addr >> 3) & 0x3f;
2135}
2136
2137/* Skip over a designated number of registers in the backing
2138 store, remembering every 64th position is for NAT. */
2139static __inline__ uint64_t
2140ia64_rse_skip_regs (uint64_t addr, long num_regs)
2141{
2142 long delta = ia64_rse_slot_num(addr) + num_regs;
2143
2144 if (num_regs < 0)
2145 delta -= 0x3e;
2146 return addr + ((num_regs + delta/0x3f) << 3);
2147}
2148
2149/* Gdb libunwind-frame callback function to convert from an ia64 gdb register
2150 number to a libunwind register number. */
2151static int
2152ia64_gdb2uw_regnum (int regnum)
2153{
2154 if (regnum == sp_regnum)
2155 return UNW_IA64_SP;
2156 else if (regnum == IA64_BSP_REGNUM)
2157 return UNW_IA64_BSP;
2158 else if ((unsigned) (regnum - IA64_GR0_REGNUM) < 128)
2159 return UNW_IA64_GR + (regnum - IA64_GR0_REGNUM);
2160 else if ((unsigned) (regnum - V32_REGNUM) < 95)
2161 return UNW_IA64_GR + 32 + (regnum - V32_REGNUM);
2162 else if ((unsigned) (regnum - IA64_FR0_REGNUM) < 128)
2163 return UNW_IA64_FR + (regnum - IA64_FR0_REGNUM);
2164 else if ((unsigned) (regnum - IA64_PR0_REGNUM) < 64)
2165 return -1;
2166 else if ((unsigned) (regnum - IA64_BR0_REGNUM) < 8)
2167 return UNW_IA64_BR + (regnum - IA64_BR0_REGNUM);
2168 else if (regnum == IA64_PR_REGNUM)
2169 return UNW_IA64_PR;
2170 else if (regnum == IA64_IP_REGNUM)
2171 return UNW_REG_IP;
2172 else if (regnum == IA64_CFM_REGNUM)
2173 return UNW_IA64_CFM;
2174 else if ((unsigned) (regnum - IA64_AR0_REGNUM) < 128)
2175 return UNW_IA64_AR + (regnum - IA64_AR0_REGNUM);
2176 else if ((unsigned) (regnum - IA64_NAT0_REGNUM) < 128)
2177 return UNW_IA64_NAT + (regnum - IA64_NAT0_REGNUM);
2178 else
2179 return -1;
2180}
2181
2182/* Gdb libunwind-frame callback function to convert from a libunwind register
2183 number to a ia64 gdb register number. */
2184static int
2185ia64_uw2gdb_regnum (int uw_regnum)
2186{
2187 if (uw_regnum == UNW_IA64_SP)
2188 return sp_regnum;
2189 else if (uw_regnum == UNW_IA64_BSP)
2190 return IA64_BSP_REGNUM;
2191 else if ((unsigned) (uw_regnum - UNW_IA64_GR) < 32)
2192 return IA64_GR0_REGNUM + (uw_regnum - UNW_IA64_GR);
2193 else if ((unsigned) (uw_regnum - UNW_IA64_GR) < 128)
2194 return V32_REGNUM + (uw_regnum - (IA64_GR0_REGNUM + 32));
2195 else if ((unsigned) (uw_regnum - UNW_IA64_FR) < 128)
2196 return IA64_FR0_REGNUM + (uw_regnum - UNW_IA64_FR);
2197 else if ((unsigned) (uw_regnum - UNW_IA64_BR) < 8)
2198 return IA64_BR0_REGNUM + (uw_regnum - UNW_IA64_BR);
2199 else if (uw_regnum == UNW_IA64_PR)
2200 return IA64_PR_REGNUM;
2201 else if (uw_regnum == UNW_REG_IP)
2202 return IA64_IP_REGNUM;
2203 else if (uw_regnum == UNW_IA64_CFM)
2204 return IA64_CFM_REGNUM;
2205 else if ((unsigned) (uw_regnum - UNW_IA64_AR) < 128)
2206 return IA64_AR0_REGNUM + (uw_regnum - UNW_IA64_AR);
2207 else if ((unsigned) (uw_regnum - UNW_IA64_NAT) < 128)
2208 return IA64_NAT0_REGNUM + (uw_regnum - UNW_IA64_NAT);
2209 else
2210 return -1;
2211}
2212
2213/* Gdb libunwind-frame callback function to reveal if register is a float
2214 register or not. */
2215static int
2216ia64_is_fpreg (int uw_regnum)
2217{
2218 return unw_is_fpreg (uw_regnum);
2219}
2220
2221/* Libunwind callback accessor function for general registers. */
2222static int
2223ia64_access_reg (unw_addr_space_t as, unw_regnum_t uw_regnum, unw_word_t *val,
2224 int write, void *arg)
2225{
2226 int regnum = ia64_uw2gdb_regnum (uw_regnum);
2227 unw_word_t bsp, sof, sol, cfm, psr, ip;
2228 struct frame_info *next_frame = arg;
2229 long new_sof, old_sof;
2230 char buf[MAX_REGISTER_SIZE];
2231
45ecac4b
UW
2232 /* We never call any libunwind routines that need to write registers. */
2233 gdb_assert (!write);
968d1cb4 2234
45ecac4b 2235 switch (uw_regnum)
968d1cb4 2236 {
45ecac4b
UW
2237 case UNW_REG_IP:
2238 /* Libunwind expects to see the pc value which means the slot number
2239 from the psr must be merged with the ip word address. */
2240 frame_unwind_register (next_frame, IA64_IP_REGNUM, buf);
2241 ip = extract_unsigned_integer (buf, 8);
2242 frame_unwind_register (next_frame, IA64_PSR_REGNUM, buf);
2243 psr = extract_unsigned_integer (buf, 8);
2244 *val = ip | ((psr >> 41) & 0x3);
2245 break;
2246
2247 case UNW_IA64_AR_BSP:
2248 /* Libunwind expects to see the beginning of the current register
2249 frame so we must account for the fact that ptrace() will return a value
2250 for bsp that points *after* the current register frame. */
2251 frame_unwind_register (next_frame, IA64_BSP_REGNUM, buf);
2252 bsp = extract_unsigned_integer (buf, 8);
2253 frame_unwind_register (next_frame, IA64_CFM_REGNUM, buf);
2254 cfm = extract_unsigned_integer (buf, 8);
2255 sof = (cfm & 0x7f);
2256 *val = ia64_rse_skip_regs (bsp, -sof);
2257 break;
968d1cb4 2258
45ecac4b
UW
2259 case UNW_IA64_AR_BSPSTORE:
2260 /* Libunwind wants bspstore to be after the current register frame.
2261 This is what ptrace() and gdb treats as the regular bsp value. */
2262 frame_unwind_register (next_frame, IA64_BSP_REGNUM, buf);
2263 *val = extract_unsigned_integer (buf, 8);
2264 break;
2265
2266 default:
2267 /* For all other registers, just unwind the value directly. */
2268 frame_unwind_register (next_frame, regnum, buf);
2269 *val = extract_unsigned_integer (buf, 8);
2270 break;
968d1cb4 2271 }
45ecac4b
UW
2272
2273 if (gdbarch_debug >= 1)
2274 fprintf_unfiltered (gdb_stdlog,
2275 " access_reg: from cache: %4s=0x%s\n",
2276 (((unsigned) regnum <= IA64_NAT127_REGNUM)
2277 ? ia64_register_names[regnum] : "r??"),
2278 paddr_nz (*val));
968d1cb4
JJ
2279 return 0;
2280}
2281
2282/* Libunwind callback accessor function for floating-point registers. */
2283static int
2284ia64_access_fpreg (unw_addr_space_t as, unw_regnum_t uw_regnum, unw_fpreg_t *val,
2285 int write, void *arg)
2286{
2287 int regnum = ia64_uw2gdb_regnum (uw_regnum);
45ecac4b 2288 struct frame_info *next_frame = arg;
968d1cb4 2289
45ecac4b
UW
2290 /* We never call any libunwind routines that need to write registers. */
2291 gdb_assert (!write);
2292
2293 frame_unwind_register (next_frame, regnum, (char *) val);
2294
968d1cb4
JJ
2295 return 0;
2296}
2297
c5a27d9c
JJ
2298/* Libunwind callback accessor function for top-level rse registers. */
2299static int
2300ia64_access_rse_reg (unw_addr_space_t as, unw_regnum_t uw_regnum, unw_word_t *val,
2301 int write, void *arg)
2302{
2303 int regnum = ia64_uw2gdb_regnum (uw_regnum);
2304 unw_word_t bsp, sof, sol, cfm, psr, ip;
45ecac4b 2305 struct regcache *regcache = arg;
c5a27d9c 2306 long new_sof, old_sof;
45ecac4b 2307 char buf[MAX_REGISTER_SIZE];
c5a27d9c 2308
45ecac4b
UW
2309 /* We never call any libunwind routines that need to write registers. */
2310 gdb_assert (!write);
c5a27d9c 2311
45ecac4b 2312 switch (uw_regnum)
c5a27d9c 2313 {
45ecac4b
UW
2314 case UNW_REG_IP:
2315 /* Libunwind expects to see the pc value which means the slot number
2316 from the psr must be merged with the ip word address. */
2317 regcache_cooked_read (regcache, IA64_IP_REGNUM, buf);
2318 ip = extract_unsigned_integer (buf, 8);
2319 regcache_cooked_read (regcache, IA64_PSR_REGNUM, buf);
2320 psr = extract_unsigned_integer (buf, 8);
2321 *val = ip | ((psr >> 41) & 0x3);
2322 break;
c5a27d9c 2323
45ecac4b
UW
2324 case UNW_IA64_AR_BSP:
2325 /* Libunwind expects to see the beginning of the current register
2326 frame so we must account for the fact that ptrace() will return a value
2327 for bsp that points *after* the current register frame. */
2328 regcache_cooked_read (regcache, IA64_BSP_REGNUM, buf);
2329 bsp = extract_unsigned_integer (buf, 8);
2330 regcache_cooked_read (regcache, IA64_CFM_REGNUM, buf);
2331 cfm = extract_unsigned_integer (buf, 8);
2332 sof = (cfm & 0x7f);
2333 *val = ia64_rse_skip_regs (bsp, -sof);
2334 break;
c5a27d9c 2335
45ecac4b
UW
2336 case UNW_IA64_AR_BSPSTORE:
2337 /* Libunwind wants bspstore to be after the current register frame.
2338 This is what ptrace() and gdb treats as the regular bsp value. */
2339 regcache_cooked_read (regcache, IA64_BSP_REGNUM, buf);
2340 *val = extract_unsigned_integer (buf, 8);
2341 break;
c5a27d9c 2342
45ecac4b
UW
2343 default:
2344 /* For all other registers, just unwind the value directly. */
2345 regcache_cooked_read (regcache, regnum, buf);
2346 *val = extract_unsigned_integer (buf, 8);
2347 break;
c5a27d9c
JJ
2348 }
2349
2350 if (gdbarch_debug >= 1)
2351 fprintf_unfiltered (gdb_stdlog,
2352 " access_rse_reg: from cache: %4s=0x%s\n",
2353 (((unsigned) regnum <= IA64_NAT127_REGNUM)
2354 ? ia64_register_names[regnum] : "r??"),
2355 paddr_nz (*val));
2356
2357 return 0;
2358}
2359
45ecac4b
UW
2360/* Libunwind callback accessor function for top-level fp registers. */
2361static int
2362ia64_access_rse_fpreg (unw_addr_space_t as, unw_regnum_t uw_regnum,
2363 unw_fpreg_t *val, int write, void *arg)
2364{
2365 int regnum = ia64_uw2gdb_regnum (uw_regnum);
2366 struct regcache *regcache = arg;
2367
2368 /* We never call any libunwind routines that need to write registers. */
2369 gdb_assert (!write);
2370
2371 regcache_cooked_read (regcache, regnum, (char *) val);
2372
2373 return 0;
2374}
2375
968d1cb4
JJ
2376/* Libunwind callback accessor function for accessing memory. */
2377static int
2378ia64_access_mem (unw_addr_space_t as,
2379 unw_word_t addr, unw_word_t *val,
2380 int write, void *arg)
2381{
c5a27d9c
JJ
2382 if (addr - KERNEL_START < ktab_size)
2383 {
2384 unw_word_t *laddr = (unw_word_t*) ((char *) ktab
2385 + (addr - KERNEL_START));
2386
2387 if (write)
2388 *laddr = *val;
2389 else
2390 *val = *laddr;
2391 return 0;
2392 }
2393
968d1cb4
JJ
2394 /* XXX do we need to normalize byte-order here? */
2395 if (write)
2396 return target_write_memory (addr, (char *) val, sizeof (unw_word_t));
2397 else
2398 return target_read_memory (addr, (char *) val, sizeof (unw_word_t));
2399}
2400
2401/* Call low-level function to access the kernel unwind table. */
13547ab6
DJ
2402static LONGEST
2403getunwind_table (gdb_byte **buf_p)
968d1cb4
JJ
2404{
2405 LONGEST x;
c5a27d9c 2406
10d6c8cd
DJ
2407 /* FIXME drow/2005-09-10: This code used to call
2408 ia64_linux_xfer_unwind_table directly to fetch the unwind table
2409 for the currently running ia64-linux kernel. That data should
2410 come from the core file and be accessed via the auxv vector; if
2411 we want to preserve fall back to the running kernel's table, then
2412 we should find a way to override the corefile layer's
2413 xfer_partial method. */
968d1cb4 2414
13547ab6
DJ
2415 x = target_read_alloc (&current_target, TARGET_OBJECT_UNWIND_TABLE,
2416 NULL, buf_p);
2417
2418 return x;
968d1cb4 2419}
10d6c8cd 2420
968d1cb4
JJ
2421/* Get the kernel unwind table. */
2422static int
2423get_kernel_table (unw_word_t ip, unw_dyn_info_t *di)
2424{
c5a27d9c 2425 static struct ia64_table_entry *etab;
968d1cb4 2426
c5a27d9c 2427 if (!ktab)
968d1cb4 2428 {
13547ab6 2429 gdb_byte *ktab_buf;
eeec829c 2430 LONGEST size;
13547ab6 2431
eeec829c
DJ
2432 size = getunwind_table (&ktab_buf);
2433 if (size <= 0)
13547ab6 2434 return -UNW_ENOINFO;
eeec829c
DJ
2435
2436 ktab = (struct ia64_table_entry *) ktab_buf;
2437 ktab_size = size;
13547ab6 2438
968d1cb4 2439 for (etab = ktab; etab->start_offset; ++etab)
c5a27d9c 2440 etab->info_offset += KERNEL_START;
968d1cb4
JJ
2441 }
2442
2443 if (ip < ktab[0].start_offset || ip >= etab[-1].end_offset)
2444 return -UNW_ENOINFO;
2445
2446 di->format = UNW_INFO_FORMAT_TABLE;
2447 di->gp = 0;
2448 di->start_ip = ktab[0].start_offset;
2449 di->end_ip = etab[-1].end_offset;
2450 di->u.ti.name_ptr = (unw_word_t) "<kernel>";
2451 di->u.ti.segbase = 0;
2452 di->u.ti.table_len = ((char *) etab - (char *) ktab) / sizeof (unw_word_t);
2453 di->u.ti.table_data = (unw_word_t *) ktab;
2454
2455 if (gdbarch_debug >= 1)
2456 fprintf_unfiltered (gdb_stdlog, "get_kernel_table: found table `%s': "
78ced177
JJ
2457 "segbase=0x%s, length=%s, gp=0x%s\n",
2458 (char *) di->u.ti.name_ptr,
2459 paddr_nz (di->u.ti.segbase),
2460 paddr_u (di->u.ti.table_len),
2461 paddr_nz (di->gp));
968d1cb4
JJ
2462 return 0;
2463}
2464
2465/* Find the unwind table entry for a specified address. */
2466static int
2467ia64_find_unwind_table (struct objfile *objfile, unw_word_t ip,
2468 unw_dyn_info_t *dip, void **buf)
2469{
2470 Elf_Internal_Phdr *phdr, *p_text = NULL, *p_unwind = NULL;
2471 Elf_Internal_Ehdr *ehdr;
2472 unw_word_t segbase = 0;
2473 CORE_ADDR load_base;
2474 bfd *bfd;
2475 int i;
2476
2477 bfd = objfile->obfd;
2478
2479 ehdr = elf_tdata (bfd)->elf_header;
2480 phdr = elf_tdata (bfd)->phdr;
2481
2482 load_base = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2483
2484 for (i = 0; i < ehdr->e_phnum; ++i)
2485 {
2486 switch (phdr[i].p_type)
2487 {
2488 case PT_LOAD:
2489 if ((unw_word_t) (ip - load_base - phdr[i].p_vaddr)
2490 < phdr[i].p_memsz)
2491 p_text = phdr + i;
2492 break;
2493
2494 case PT_IA_64_UNWIND:
2495 p_unwind = phdr + i;
2496 break;
2497
2498 default:
2499 break;
2500 }
2501 }
2502
c5a27d9c 2503 if (!p_text || !p_unwind)
968d1cb4
JJ
2504 return -UNW_ENOINFO;
2505
c5a27d9c
JJ
2506 /* Verify that the segment that contains the IP also contains
2507 the static unwind table. If not, we may be in the Linux kernel's
2508 DSO gate page in which case the unwind table is another segment.
2509 Otherwise, we are dealing with runtime-generated code, for which we
2510 have no info here. */
968d1cb4
JJ
2511 segbase = p_text->p_vaddr + load_base;
2512
c5a27d9c
JJ
2513 if ((p_unwind->p_vaddr - p_text->p_vaddr) >= p_text->p_memsz)
2514 {
2515 int ok = 0;
2516 for (i = 0; i < ehdr->e_phnum; ++i)
2517 {
2518 if (phdr[i].p_type == PT_LOAD
2519 && (p_unwind->p_vaddr - phdr[i].p_vaddr) < phdr[i].p_memsz)
2520 {
2521 ok = 1;
2522 /* Get the segbase from the section containing the
2523 libunwind table. */
2524 segbase = phdr[i].p_vaddr + load_base;
2525 }
2526 }
2527 if (!ok)
2528 return -UNW_ENOINFO;
2529 }
2530
2531 dip->start_ip = p_text->p_vaddr + load_base;
968d1cb4 2532 dip->end_ip = dip->start_ip + p_text->p_memsz;
b33e8514 2533 dip->gp = ia64_find_global_pointer (ip);
503ff15d
KB
2534 dip->format = UNW_INFO_FORMAT_REMOTE_TABLE;
2535 dip->u.rti.name_ptr = (unw_word_t) bfd_get_filename (bfd);
2536 dip->u.rti.segbase = segbase;
2537 dip->u.rti.table_len = p_unwind->p_memsz / sizeof (unw_word_t);
2538 dip->u.rti.table_data = p_unwind->p_vaddr + load_base;
968d1cb4
JJ
2539
2540 return 0;
2541}
2542
2543/* Libunwind callback accessor function to acquire procedure unwind-info. */
2544static int
2545ia64_find_proc_info_x (unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
2546 int need_unwind_info, void *arg)
2547{
2548 struct obj_section *sec = find_pc_section (ip);
2549 unw_dyn_info_t di;
2550 int ret;
2551 void *buf = NULL;
2552
2553 if (!sec)
2554 {
2555 /* XXX This only works if the host and the target architecture are
2556 both ia64 and if the have (more or less) the same kernel
2557 version. */
2558 if (get_kernel_table (ip, &di) < 0)
2559 return -UNW_ENOINFO;
503ff15d
KB
2560
2561 if (gdbarch_debug >= 1)
78ced177
JJ
2562 fprintf_unfiltered (gdb_stdlog, "ia64_find_proc_info_x: 0x%s -> "
2563 "(name=`%s',segbase=0x%s,start=0x%s,end=0x%s,gp=0x%s,"
2564 "length=%s,data=0x%s)\n",
2565 paddr_nz (ip), (char *)di.u.ti.name_ptr,
2566 paddr_nz (di.u.ti.segbase),
2567 paddr_nz (di.start_ip), paddr_nz (di.end_ip),
2568 paddr_nz (di.gp),
2569 paddr_u (di.u.ti.table_len),
2570 paddr_nz ((CORE_ADDR)di.u.ti.table_data));
968d1cb4
JJ
2571 }
2572 else
2573 {
2574 ret = ia64_find_unwind_table (sec->objfile, ip, &di, &buf);
2575 if (ret < 0)
2576 return ret;
968d1cb4 2577
503ff15d 2578 if (gdbarch_debug >= 1)
78ced177
JJ
2579 fprintf_unfiltered (gdb_stdlog, "ia64_find_proc_info_x: 0x%s -> "
2580 "(name=`%s',segbase=0x%s,start=0x%s,end=0x%s,gp=0x%s,"
2581 "length=%s,data=0x%s)\n",
2582 paddr_nz (ip), (char *)di.u.rti.name_ptr,
2583 paddr_nz (di.u.rti.segbase),
2584 paddr_nz (di.start_ip), paddr_nz (di.end_ip),
2585 paddr_nz (di.gp),
2586 paddr_u (di.u.rti.table_len),
2587 paddr_nz (di.u.rti.table_data));
503ff15d 2588 }
968d1cb4 2589
503ff15d
KB
2590 ret = libunwind_search_unwind_table (&as, ip, &di, pi, need_unwind_info,
2591 arg);
968d1cb4
JJ
2592
2593 /* We no longer need the dyn info storage so free it. */
2594 xfree (buf);
2595
2596 return ret;
2597}
2598
2599/* Libunwind callback accessor function for cleanup. */
2600static void
2601ia64_put_unwind_info (unw_addr_space_t as,
2602 unw_proc_info_t *pip, void *arg)
2603{
2604 /* Nothing required for now. */
2605}
2606
2607/* Libunwind callback accessor function to get head of the dynamic
2608 unwind-info registration list. */
2609static int
2610ia64_get_dyn_info_list (unw_addr_space_t as,
2611 unw_word_t *dilap, void *arg)
2612{
2613 struct obj_section *text_sec;
2614 struct objfile *objfile;
2615 unw_word_t ip, addr;
2616 unw_dyn_info_t di;
2617 int ret;
2618
2619 if (!libunwind_is_initialized ())
2620 return -UNW_ENOINFO;
2621
2622 for (objfile = object_files; objfile; objfile = objfile->next)
2623 {
2624 void *buf = NULL;
2625
2626 text_sec = objfile->sections + SECT_OFF_TEXT (objfile);
2627 ip = text_sec->addr;
2628 ret = ia64_find_unwind_table (objfile, ip, &di, &buf);
2629 if (ret >= 0)
2630 {
503ff15d 2631 addr = libunwind_find_dyn_list (as, &di, arg);
968d1cb4
JJ
2632 /* We no longer need the dyn info storage so free it. */
2633 xfree (buf);
2634
2635 if (addr)
2636 {
2637 if (gdbarch_debug >= 1)
2638 fprintf_unfiltered (gdb_stdlog,
2639 "dynamic unwind table in objfile %s "
78ced177 2640 "at 0x%s (gp=0x%s)\n",
968d1cb4 2641 bfd_get_filename (objfile->obfd),
78ced177 2642 paddr_nz (addr), paddr_nz (di.gp));
968d1cb4
JJ
2643 *dilap = addr;
2644 return 0;
2645 }
2646 }
2647 }
2648 return -UNW_ENOINFO;
2649}
2650
2651
2652/* Frame interface functions for libunwind. */
2653
2654static void
2655ia64_libunwind_frame_this_id (struct frame_info *next_frame, void **this_cache,
7166c4a9 2656 struct frame_id *this_id)
968d1cb4
JJ
2657{
2658 char buf[8];
2659 CORE_ADDR bsp;
2660 struct frame_id id;
c5a27d9c
JJ
2661 CORE_ADDR prev_ip, addr;
2662 int realnum, optimized;
2663 enum lval_type lval;
2664
968d1cb4
JJ
2665
2666 libunwind_frame_this_id (next_frame, this_cache, &id);
c5a27d9c
JJ
2667 if (frame_id_eq (id, null_frame_id))
2668 {
2669 (*this_id) = null_frame_id;
2670 return;
2671 }
968d1cb4 2672
c5a27d9c
JJ
2673 /* We must add the bsp as the special address for frame comparison
2674 purposes. */
968d1cb4
JJ
2675 frame_unwind_register (next_frame, IA64_BSP_REGNUM, buf);
2676 bsp = extract_unsigned_integer (buf, 8);
2677
c5a27d9c
JJ
2678 /* If the previous frame pc value is 0, then we are at the end of the stack
2679 and don't want to unwind past this frame. We return a null frame_id to
2680 indicate this. */
2681 libunwind_frame_prev_register (next_frame, this_cache, IA64_IP_REGNUM,
f1b4b38e
AS
2682 &optimized, &lval, &addr, &realnum, buf);
2683 prev_ip = extract_unsigned_integer (buf, 8);
c5a27d9c
JJ
2684
2685 if (prev_ip != 0)
2686 (*this_id) = frame_id_build_special (id.stack_addr, id.code_addr, bsp);
2687 else
2688 (*this_id) = null_frame_id;
968d1cb4
JJ
2689
2690 if (gdbarch_debug >= 1)
2691 fprintf_unfiltered (gdb_stdlog,
78ced177
JJ
2692 "libunwind frame id: code 0x%s, stack 0x%s, special 0x%s, next_frame %p\n",
2693 paddr_nz (id.code_addr), paddr_nz (id.stack_addr),
2694 paddr_nz (bsp), next_frame);
968d1cb4
JJ
2695}
2696
2697static void
2698ia64_libunwind_frame_prev_register (struct frame_info *next_frame,
2699 void **this_cache,
2700 int regnum, int *optimizedp,
2701 enum lval_type *lvalp, CORE_ADDR *addrp,
88d82102 2702 int *realnump, gdb_byte *valuep)
968d1cb4
JJ
2703{
2704 int reg = regnum;
2705
088568da 2706 struct gdbarch *gdbarch = get_frame_arch (next_frame);
968d1cb4
JJ
2707 if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
2708 reg = IA64_PR_REGNUM;
2709 else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
2710 reg = IA64_UNAT_REGNUM;
2711
2712 /* Let libunwind do most of the work. */
2713 libunwind_frame_prev_register (next_frame, this_cache, reg,
2714 optimizedp, lvalp, addrp, realnump, valuep);
2715
6672f2ae
AS
2716 /* No more to do if the value is not supposed to be supplied. */
2717 if (!valuep)
2718 return;
2719
968d1cb4
JJ
2720 if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
2721 {
2722 ULONGEST prN_val;
2723
2724 if (VP16_REGNUM <= regnum && regnum <= VP63_REGNUM)
2725 {
2726 int rrb_pr = 0;
2727 ULONGEST cfm;
2728 unsigned char buf[MAX_REGISTER_SIZE];
2729
2730 /* Fetch predicate register rename base from current frame
2731 marker for this frame. */
2732 frame_unwind_register (next_frame, IA64_CFM_REGNUM, buf);
2733 cfm = extract_unsigned_integer (buf, 8);
2734 rrb_pr = (cfm >> 32) & 0x3f;
2735
2736 /* Adjust the register number to account for register rotation. */
2737 regnum = VP16_REGNUM
2738 + ((regnum - VP16_REGNUM) + rrb_pr) % 48;
2739 }
2740 prN_val = extract_bit_field ((unsigned char *) valuep,
2741 regnum - VP0_REGNUM, 1);
088568da 2742 store_unsigned_integer (valuep, register_size (gdbarch, regnum), prN_val);
968d1cb4
JJ
2743 }
2744 else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
2745 {
2746 ULONGEST unatN_val;
2747
2748 unatN_val = extract_bit_field ((unsigned char *) valuep,
2749 regnum - IA64_NAT0_REGNUM, 1);
088568da 2750 store_unsigned_integer (valuep, register_size (gdbarch, regnum),
968d1cb4
JJ
2751 unatN_val);
2752 }
2753 else if (regnum == IA64_BSP_REGNUM)
2754 {
2755 char cfm_valuep[MAX_REGISTER_SIZE];
2756 int cfm_optim;
2757 int cfm_realnum;
2758 enum lval_type cfm_lval;
2759 CORE_ADDR cfm_addr;
2760 CORE_ADDR bsp, prev_cfm, prev_bsp;
2761
2762 /* We want to calculate the previous bsp as the end of the previous register stack frame.
2763 This corresponds to what the hardware bsp register will be if we pop the frame
2764 back which is why we might have been called. We know that libunwind will pass us back
2765 the beginning of the current frame so we should just add sof to it. */
2766 prev_bsp = extract_unsigned_integer (valuep, 8);
2767 libunwind_frame_prev_register (next_frame, this_cache, IA64_CFM_REGNUM,
2768 &cfm_optim, &cfm_lval, &cfm_addr, &cfm_realnum, cfm_valuep);
2769 prev_cfm = extract_unsigned_integer (cfm_valuep, 8);
2770 prev_bsp = rse_address_add (prev_bsp, (prev_cfm & 0x7f));
2771
088568da 2772 store_unsigned_integer (valuep, register_size (gdbarch, regnum),
968d1cb4
JJ
2773 prev_bsp);
2774 }
2775
2776 if (gdbarch_debug >= 1)
2777 fprintf_unfiltered (gdb_stdlog,
78ced177 2778 "libunwind prev register <%s> is 0x%s\n",
c5a27d9c
JJ
2779 (regnum < IA64_GR32_REGNUM
2780 || (regnum > IA64_GR127_REGNUM
2781 && regnum < LAST_PSEUDO_REGNUM))
2782 ? ia64_register_names[regnum]
2783 : (regnum < LAST_PSEUDO_REGNUM
2784 ? ia64_register_names[regnum-IA64_GR32_REGNUM+V32_REGNUM]
2785 : "OUT_OF_RANGE"),
78ced177 2786 paddr_nz (extract_unsigned_integer (valuep, 8)));
968d1cb4
JJ
2787}
2788
2789static const struct frame_unwind ia64_libunwind_frame_unwind =
2790{
2791 NORMAL_FRAME,
2792 ia64_libunwind_frame_this_id,
272dfcfd
AS
2793 ia64_libunwind_frame_prev_register,
2794 NULL,
2795 NULL,
2796 NULL,
2797 libunwind_frame_dealloc_cache
968d1cb4
JJ
2798};
2799
2800static const struct frame_unwind *
2801ia64_libunwind_frame_sniffer (struct frame_info *next_frame)
2802{
2803 if (libunwind_is_initialized () && libunwind_frame_sniffer (next_frame))
2804 return &ia64_libunwind_frame_unwind;
2805
2806 return NULL;
2807}
2808
c5a27d9c
JJ
2809static void
2810ia64_libunwind_sigtramp_frame_this_id (struct frame_info *next_frame, void **this_cache,
2811 struct frame_id *this_id)
2812{
2813 char buf[8];
2814 CORE_ADDR bsp;
2815 struct frame_id id;
2816 CORE_ADDR prev_ip;
2817
2818 libunwind_frame_this_id (next_frame, this_cache, &id);
2819 if (frame_id_eq (id, null_frame_id))
2820 {
2821 (*this_id) = null_frame_id;
2822 return;
2823 }
2824
2825 /* We must add the bsp as the special address for frame comparison
2826 purposes. */
2827 frame_unwind_register (next_frame, IA64_BSP_REGNUM, buf);
2828 bsp = extract_unsigned_integer (buf, 8);
2829
2830 /* For a sigtramp frame, we don't make the check for previous ip being 0. */
2831 (*this_id) = frame_id_build_special (id.stack_addr, id.code_addr, bsp);
2832
2833 if (gdbarch_debug >= 1)
2834 fprintf_unfiltered (gdb_stdlog,
2835 "libunwind sigtramp frame id: code 0x%s, stack 0x%s, special 0x%s, next_frame %p\n",
2836 paddr_nz (id.code_addr), paddr_nz (id.stack_addr),
2837 paddr_nz (bsp), next_frame);
2838}
2839
2840static void
2841ia64_libunwind_sigtramp_frame_prev_register (struct frame_info *next_frame,
2842 void **this_cache,
2843 int regnum, int *optimizedp,
2844 enum lval_type *lvalp, CORE_ADDR *addrp,
88d82102 2845 int *realnump, gdb_byte *valuep)
c5a27d9c
JJ
2846
2847{
f1b4b38e 2848 gdb_byte buf[8];
c5a27d9c
JJ
2849 CORE_ADDR prev_ip, addr;
2850 int realnum, optimized;
2851 enum lval_type lval;
2852
2853
2854 /* If the previous frame pc value is 0, then we want to use the SIGCONTEXT
2855 method of getting previous registers. */
2856 libunwind_frame_prev_register (next_frame, this_cache, IA64_IP_REGNUM,
f1b4b38e
AS
2857 &optimized, &lval, &addr, &realnum, buf);
2858 prev_ip = extract_unsigned_integer (buf, 8);
c5a27d9c
JJ
2859
2860 if (prev_ip == 0)
2861 {
2862 void *tmp_cache = NULL;
2863 ia64_sigtramp_frame_prev_register (next_frame, &tmp_cache, regnum, optimizedp, lvalp,
2864 addrp, realnump, valuep);
2865 }
2866 else
2867 ia64_libunwind_frame_prev_register (next_frame, this_cache, regnum, optimizedp, lvalp,
2868 addrp, realnump, valuep);
2869}
2870
2871static const struct frame_unwind ia64_libunwind_sigtramp_frame_unwind =
2872{
2873 SIGTRAMP_FRAME,
2874 ia64_libunwind_sigtramp_frame_this_id,
2875 ia64_libunwind_sigtramp_frame_prev_register
2876};
2877
2878static const struct frame_unwind *
2879ia64_libunwind_sigtramp_frame_sniffer (struct frame_info *next_frame)
2880{
2881 if (libunwind_is_initialized ())
2882 {
2883 if (libunwind_sigtramp_frame_sniffer (next_frame))
2884 return &ia64_libunwind_sigtramp_frame_unwind;
2885 return NULL;
2886 }
2887 else
2888 return ia64_sigtramp_frame_sniffer (next_frame);
2889}
2890
968d1cb4
JJ
2891/* Set of libunwind callback acccessor functions. */
2892static unw_accessors_t ia64_unw_accessors =
2893{
2894 ia64_find_proc_info_x,
2895 ia64_put_unwind_info,
2896 ia64_get_dyn_info_list,
2897 ia64_access_mem,
2898 ia64_access_reg,
2899 ia64_access_fpreg,
2900 /* resume */
2901 /* get_proc_name */
2902};
2903
c5a27d9c
JJ
2904/* Set of special libunwind callback acccessor functions specific for accessing
2905 the rse registers. At the top of the stack, we want libunwind to figure out
2906 how to read r32 - r127. Though usually they are found sequentially in memory
2907 starting from $bof, this is not always true. */
2908static unw_accessors_t ia64_unw_rse_accessors =
2909{
2910 ia64_find_proc_info_x,
2911 ia64_put_unwind_info,
2912 ia64_get_dyn_info_list,
2913 ia64_access_mem,
2914 ia64_access_rse_reg,
45ecac4b 2915 ia64_access_rse_fpreg,
c5a27d9c
JJ
2916 /* resume */
2917 /* get_proc_name */
2918};
2919
968d1cb4
JJ
2920/* Set of ia64 gdb libunwind-frame callbacks and data for generic libunwind-frame code to use. */
2921static struct libunwind_descr ia64_libunwind_descr =
2922{
2923 ia64_gdb2uw_regnum,
2924 ia64_uw2gdb_regnum,
2925 ia64_is_fpreg,
2926 &ia64_unw_accessors,
c5a27d9c 2927 &ia64_unw_rse_accessors,
968d1cb4
JJ
2928};
2929
2930#endif /* HAVE_LIBUNWIND_IA64_H */
2931
4c8b6ae0
UW
2932static int
2933ia64_use_struct_convention (struct type *type)
16461d7d 2934{
64a5b29c
KB
2935 struct type *float_elt_type;
2936
4c8b6ae0
UW
2937 /* Don't use the struct convention for anything but structure,
2938 union, or array types. */
2939 if (!(TYPE_CODE (type) == TYPE_CODE_STRUCT
2940 || TYPE_CODE (type) == TYPE_CODE_UNION
2941 || TYPE_CODE (type) == TYPE_CODE_ARRAY))
2942 return 0;
2943
64a5b29c
KB
2944 /* HFAs are structures (or arrays) consisting entirely of floating
2945 point values of the same length. Up to 8 of these are returned
2946 in registers. Don't use the struct convention when this is the
004d836a 2947 case. */
64a5b29c
KB
2948 float_elt_type = is_float_or_hfa_type (type);
2949 if (float_elt_type != NULL
2950 && TYPE_LENGTH (type) / TYPE_LENGTH (float_elt_type) <= 8)
2951 return 0;
2952
2953 /* Other structs of length 32 or less are returned in r8-r11.
004d836a 2954 Don't use the struct convention for those either. */
16461d7d
KB
2955 return TYPE_LENGTH (type) > 32;
2956}
2957
4c8b6ae0 2958static void
2d522557
AC
2959ia64_extract_return_value (struct type *type, struct regcache *regcache,
2960 gdb_byte *valbuf)
16461d7d 2961{
64a5b29c
KB
2962 struct type *float_elt_type;
2963
2964 float_elt_type = is_float_or_hfa_type (type);
2965 if (float_elt_type != NULL)
2966 {
004d836a 2967 char from[MAX_REGISTER_SIZE];
64a5b29c
KB
2968 int offset = 0;
2969 int regnum = IA64_FR8_REGNUM;
2970 int n = TYPE_LENGTH (type) / TYPE_LENGTH (float_elt_type);
2971
2972 while (n-- > 0)
2973 {
004d836a
JJ
2974 regcache_cooked_read (regcache, regnum, from);
2975 convert_typed_floating (from, builtin_type_ia64_ext,
2976 (char *)valbuf + offset, float_elt_type);
64a5b29c
KB
2977 offset += TYPE_LENGTH (float_elt_type);
2978 regnum++;
2979 }
2980 }
16461d7d 2981 else
004d836a
JJ
2982 {
2983 ULONGEST val;
2984 int offset = 0;
2985 int regnum = IA64_GR8_REGNUM;
7b9ee6a8
DJ
2986 int reglen = TYPE_LENGTH (register_type (get_regcache_arch (regcache),
2987 IA64_GR8_REGNUM));
004d836a
JJ
2988 int n = TYPE_LENGTH (type) / reglen;
2989 int m = TYPE_LENGTH (type) % reglen;
16461d7d 2990
004d836a
JJ
2991 while (n-- > 0)
2992 {
2993 ULONGEST val;
2994 regcache_cooked_read_unsigned (regcache, regnum, &val);
2995 memcpy ((char *)valbuf + offset, &val, reglen);
2996 offset += reglen;
2997 regnum++;
2998 }
16461d7d 2999
004d836a
JJ
3000 if (m)
3001 {
3002 regcache_cooked_read_unsigned (regcache, regnum, &val);
3003 memcpy ((char *)valbuf + offset, &val, m);
3004 }
3005 }
16461d7d
KB
3006}
3007
4c8b6ae0
UW
3008static void
3009ia64_store_return_value (struct type *type, struct regcache *regcache,
3010 const gdb_byte *valbuf)
3011{
3012 struct type *float_elt_type;
3013
3014 float_elt_type = is_float_or_hfa_type (type);
3015 if (float_elt_type != NULL)
3016 {
3017 char to[MAX_REGISTER_SIZE];
3018 int offset = 0;
3019 int regnum = IA64_FR8_REGNUM;
3020 int n = TYPE_LENGTH (type) / TYPE_LENGTH (float_elt_type);
3021
3022 while (n-- > 0)
3023 {
3024 convert_typed_floating ((char *)valbuf + offset, float_elt_type,
3025 to, builtin_type_ia64_ext);
3026 regcache_cooked_write (regcache, regnum, to);
3027 offset += TYPE_LENGTH (float_elt_type);
3028 regnum++;
3029 }
3030 }
3031 else
3032 {
3033 ULONGEST val;
3034 int offset = 0;
3035 int regnum = IA64_GR8_REGNUM;
3036 int reglen = TYPE_LENGTH (register_type (get_regcache_arch (regcache),
3037 IA64_GR8_REGNUM));
3038 int n = TYPE_LENGTH (type) / reglen;
3039 int m = TYPE_LENGTH (type) % reglen;
3040
3041 while (n-- > 0)
3042 {
3043 ULONGEST val;
3044 memcpy (&val, (char *)valbuf + offset, reglen);
3045 regcache_cooked_write_unsigned (regcache, regnum, val);
3046 offset += reglen;
3047 regnum++;
3048 }
3049
3050 if (m)
3051 {
3052 memcpy (&val, (char *)valbuf + offset, m);
3053 regcache_cooked_write_unsigned (regcache, regnum, val);
3054 }
3055 }
3056}
3057
3058static enum return_value_convention
3059ia64_return_value (struct gdbarch *gdbarch, struct type *valtype,
3060 struct regcache *regcache, gdb_byte *readbuf,
3061 const gdb_byte *writebuf)
3062{
3063 int struct_return = ia64_use_struct_convention (valtype);
3064
3065 if (writebuf != NULL)
3066 {
3067 gdb_assert (!struct_return);
3068 ia64_store_return_value (valtype, regcache, writebuf);
3069 }
3070
3071 if (readbuf != NULL)
3072 {
3073 gdb_assert (!struct_return);
3074 ia64_extract_return_value (valtype, regcache, readbuf);
3075 }
3076
3077 if (struct_return)
3078 return RETURN_VALUE_STRUCT_CONVENTION;
3079 else
3080 return RETURN_VALUE_REGISTER_CONVENTION;
3081}
16461d7d 3082
64a5b29c
KB
3083static int
3084is_float_or_hfa_type_recurse (struct type *t, struct type **etp)
3085{
3086 switch (TYPE_CODE (t))
3087 {
3088 case TYPE_CODE_FLT:
3089 if (*etp)
3090 return TYPE_LENGTH (*etp) == TYPE_LENGTH (t);
3091 else
3092 {
3093 *etp = t;
3094 return 1;
3095 }
3096 break;
3097 case TYPE_CODE_ARRAY:
98f96ba1
KB
3098 return
3099 is_float_or_hfa_type_recurse (check_typedef (TYPE_TARGET_TYPE (t)),
3100 etp);
64a5b29c
KB
3101 break;
3102 case TYPE_CODE_STRUCT:
3103 {
3104 int i;
3105
3106 for (i = 0; i < TYPE_NFIELDS (t); i++)
98f96ba1
KB
3107 if (!is_float_or_hfa_type_recurse
3108 (check_typedef (TYPE_FIELD_TYPE (t, i)), etp))
64a5b29c
KB
3109 return 0;
3110 return 1;
3111 }
3112 break;
3113 default:
3114 return 0;
3115 break;
3116 }
3117}
3118
3119/* Determine if the given type is one of the floating point types or
3120 and HFA (which is a struct, array, or combination thereof whose
004d836a 3121 bottom-most elements are all of the same floating point type). */
64a5b29c
KB
3122
3123static struct type *
3124is_float_or_hfa_type (struct type *t)
3125{
3126 struct type *et = 0;
3127
3128 return is_float_or_hfa_type_recurse (t, &et) ? et : 0;
3129}
3130
3131
98f96ba1
KB
3132/* Return 1 if the alignment of T is such that the next even slot
3133 should be used. Return 0, if the next available slot should
3134 be used. (See section 8.5.1 of the IA-64 Software Conventions
004d836a 3135 and Runtime manual). */
98f96ba1
KB
3136
3137static int
3138slot_alignment_is_next_even (struct type *t)
3139{
3140 switch (TYPE_CODE (t))
3141 {
3142 case TYPE_CODE_INT:
3143 case TYPE_CODE_FLT:
3144 if (TYPE_LENGTH (t) > 8)
3145 return 1;
3146 else
3147 return 0;
3148 case TYPE_CODE_ARRAY:
3149 return
3150 slot_alignment_is_next_even (check_typedef (TYPE_TARGET_TYPE (t)));
3151 case TYPE_CODE_STRUCT:
3152 {
3153 int i;
3154
3155 for (i = 0; i < TYPE_NFIELDS (t); i++)
3156 if (slot_alignment_is_next_even
3157 (check_typedef (TYPE_FIELD_TYPE (t, i))))
3158 return 1;
3159 return 0;
3160 }
3161 default:
3162 return 0;
3163 }
3164}
3165
64a5b29c
KB
3166/* Attempt to find (and return) the global pointer for the given
3167 function.
3168
3169 This is a rather nasty bit of code searchs for the .dynamic section
3170 in the objfile corresponding to the pc of the function we're trying
3171 to call. Once it finds the addresses at which the .dynamic section
3172 lives in the child process, it scans the Elf64_Dyn entries for a
3173 DT_PLTGOT tag. If it finds one of these, the corresponding
3174 d_un.d_ptr value is the global pointer. */
3175
3176static CORE_ADDR
b33e8514 3177ia64_find_global_pointer (CORE_ADDR faddr)
64a5b29c 3178{
76d689a6 3179 struct obj_section *faddr_sect;
64a5b29c 3180
76d689a6
KB
3181 faddr_sect = find_pc_section (faddr);
3182 if (faddr_sect != NULL)
64a5b29c
KB
3183 {
3184 struct obj_section *osect;
3185
76d689a6 3186 ALL_OBJFILE_OSECTIONS (faddr_sect->objfile, osect)
64a5b29c
KB
3187 {
3188 if (strcmp (osect->the_bfd_section->name, ".dynamic") == 0)
3189 break;
3190 }
3191
76d689a6 3192 if (osect < faddr_sect->objfile->sections_end)
64a5b29c
KB
3193 {
3194 CORE_ADDR addr;
3195
3196 addr = osect->addr;
3197 while (addr < osect->endaddr)
3198 {
3199 int status;
3200 LONGEST tag;
3201 char buf[8];
3202
3203 status = target_read_memory (addr, buf, sizeof (buf));
3204 if (status != 0)
3205 break;
3206 tag = extract_signed_integer (buf, sizeof (buf));
3207
3208 if (tag == DT_PLTGOT)
3209 {
3210 CORE_ADDR global_pointer;
3211
3212 status = target_read_memory (addr + 8, buf, sizeof (buf));
3213 if (status != 0)
3214 break;
7c0b4a20 3215 global_pointer = extract_unsigned_integer (buf, sizeof (buf));
64a5b29c
KB
3216
3217 /* The payoff... */
3218 return global_pointer;
3219 }
3220
3221 if (tag == DT_NULL)
3222 break;
3223
3224 addr += 16;
3225 }
3226 }
3227 }
3228 return 0;
3229}
3230
3231/* Given a function's address, attempt to find (and return) the
3232 corresponding (canonical) function descriptor. Return 0 if
004d836a 3233 not found. */
64a5b29c
KB
3234static CORE_ADDR
3235find_extant_func_descr (CORE_ADDR faddr)
3236{
76d689a6 3237 struct obj_section *faddr_sect;
64a5b29c 3238
004d836a 3239 /* Return early if faddr is already a function descriptor. */
76d689a6
KB
3240 faddr_sect = find_pc_section (faddr);
3241 if (faddr_sect && strcmp (faddr_sect->the_bfd_section->name, ".opd") == 0)
64a5b29c
KB
3242 return faddr;
3243
76d689a6 3244 if (faddr_sect != NULL)
64a5b29c 3245 {
76d689a6
KB
3246 struct obj_section *osect;
3247 ALL_OBJFILE_OSECTIONS (faddr_sect->objfile, osect)
64a5b29c
KB
3248 {
3249 if (strcmp (osect->the_bfd_section->name, ".opd") == 0)
3250 break;
3251 }
3252
76d689a6 3253 if (osect < faddr_sect->objfile->sections_end)
64a5b29c
KB
3254 {
3255 CORE_ADDR addr;
3256
3257 addr = osect->addr;
3258 while (addr < osect->endaddr)
3259 {
3260 int status;
3261 LONGEST faddr2;
3262 char buf[8];
3263
3264 status = target_read_memory (addr, buf, sizeof (buf));
3265 if (status != 0)
3266 break;
3267 faddr2 = extract_signed_integer (buf, sizeof (buf));
3268
3269 if (faddr == faddr2)
3270 return addr;
3271
3272 addr += 16;
3273 }
3274 }
3275 }
3276 return 0;
3277}
3278
3279/* Attempt to find a function descriptor corresponding to the
3280 given address. If none is found, construct one on the
004d836a 3281 stack using the address at fdaptr. */
64a5b29c
KB
3282
3283static CORE_ADDR
9c9acae0 3284find_func_descr (struct regcache *regcache, CORE_ADDR faddr, CORE_ADDR *fdaptr)
64a5b29c
KB
3285{
3286 CORE_ADDR fdesc;
3287
3288 fdesc = find_extant_func_descr (faddr);
3289
3290 if (fdesc == 0)
3291 {
9c9acae0 3292 ULONGEST global_pointer;
64a5b29c
KB
3293 char buf[16];
3294
3295 fdesc = *fdaptr;
3296 *fdaptr += 16;
3297
b33e8514 3298 global_pointer = ia64_find_global_pointer (faddr);
64a5b29c
KB
3299
3300 if (global_pointer == 0)
9c9acae0
UW
3301 regcache_cooked_read_unsigned (regcache,
3302 IA64_GR1_REGNUM, &global_pointer);
64a5b29c 3303
fbd9dcd3
AC
3304 store_unsigned_integer (buf, 8, faddr);
3305 store_unsigned_integer (buf + 8, 8, global_pointer);
64a5b29c
KB
3306
3307 write_memory (fdesc, buf, 16);
3308 }
3309
3310 return fdesc;
3311}
16461d7d 3312
af8b88dd
JJ
3313/* Use the following routine when printing out function pointers
3314 so the user can see the function address rather than just the
3315 function descriptor. */
3316static CORE_ADDR
e2d0e7eb
AC
3317ia64_convert_from_func_ptr_addr (struct gdbarch *gdbarch, CORE_ADDR addr,
3318 struct target_ops *targ)
af8b88dd
JJ
3319{
3320 struct obj_section *s;
3321
3322 s = find_pc_section (addr);
3323
3324 /* check if ADDR points to a function descriptor. */
3325 if (s && strcmp (s->the_bfd_section->name, ".opd") == 0)
3326 return read_memory_unsigned_integer (addr, 8);
3327
0d5de010
DJ
3328 /* There are also descriptors embedded in vtables. */
3329 if (s)
3330 {
3331 struct minimal_symbol *minsym;
3332
3333 minsym = lookup_minimal_symbol_by_pc (addr);
3334
3335 if (minsym && is_vtable_name (SYMBOL_LINKAGE_NAME (minsym)))
3336 return read_memory_unsigned_integer (addr, 8);
3337 }
3338
af8b88dd
JJ
3339 return addr;
3340}
3341
a78f21af 3342static CORE_ADDR
004d836a
JJ
3343ia64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3344{
3345 return sp & ~0xfLL;
3346}
3347
3348static CORE_ADDR
7d9b040b 3349ia64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
8dd5115e
AS
3350 struct regcache *regcache, CORE_ADDR bp_addr,
3351 int nargs, struct value **args, CORE_ADDR sp,
3352 int struct_return, CORE_ADDR struct_addr)
16461d7d
KB
3353{
3354 int argno;
ea7c478f 3355 struct value *arg;
16461d7d
KB
3356 struct type *type;
3357 int len, argoffset;
64a5b29c 3358 int nslots, rseslots, memslots, slotnum, nfuncargs;
16461d7d 3359 int floatreg;
9c9acae0
UW
3360 ULONGEST bsp, cfm, pfs, new_bsp;
3361 CORE_ADDR funcdescaddr, pc, global_pointer;
7d9b040b 3362 CORE_ADDR func_addr = find_function_addr (function, NULL);
16461d7d
KB
3363
3364 nslots = 0;
64a5b29c 3365 nfuncargs = 0;
004d836a 3366 /* Count the number of slots needed for the arguments. */
16461d7d
KB
3367 for (argno = 0; argno < nargs; argno++)
3368 {
3369 arg = args[argno];
4991999e 3370 type = check_typedef (value_type (arg));
16461d7d
KB
3371 len = TYPE_LENGTH (type);
3372
98f96ba1 3373 if ((nslots & 1) && slot_alignment_is_next_even (type))
16461d7d
KB
3374 nslots++;
3375
64a5b29c
KB
3376 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
3377 nfuncargs++;
3378
16461d7d
KB
3379 nslots += (len + 7) / 8;
3380 }
3381
004d836a 3382 /* Divvy up the slots between the RSE and the memory stack. */
16461d7d
KB
3383 rseslots = (nslots > 8) ? 8 : nslots;
3384 memslots = nslots - rseslots;
3385
004d836a 3386 /* Allocate a new RSE frame. */
9c9acae0 3387 regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
16461d7d 3388
9c9acae0 3389 regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
16461d7d 3390 new_bsp = rse_address_add (bsp, rseslots);
9c9acae0 3391 regcache_cooked_write_unsigned (regcache, IA64_BSP_REGNUM, new_bsp);
16461d7d 3392
9c9acae0 3393 regcache_cooked_read_unsigned (regcache, IA64_PFS_REGNUM, &pfs);
16461d7d
KB
3394 pfs &= 0xc000000000000000LL;
3395 pfs |= (cfm & 0xffffffffffffLL);
9c9acae0 3396 regcache_cooked_write_unsigned (regcache, IA64_PFS_REGNUM, pfs);
16461d7d
KB
3397
3398 cfm &= 0xc000000000000000LL;
3399 cfm |= rseslots;
9c9acae0 3400 regcache_cooked_write_unsigned (regcache, IA64_CFM_REGNUM, cfm);
16461d7d 3401
64a5b29c
KB
3402 /* We will attempt to find function descriptors in the .opd segment,
3403 but if we can't we'll construct them ourselves. That being the
004d836a 3404 case, we'll need to reserve space on the stack for them. */
64a5b29c
KB
3405 funcdescaddr = sp - nfuncargs * 16;
3406 funcdescaddr &= ~0xfLL;
3407
3408 /* Adjust the stack pointer to it's new value. The calling conventions
3409 require us to have 16 bytes of scratch, plus whatever space is
004d836a 3410 necessary for the memory slots and our function descriptors. */
64a5b29c 3411 sp = sp - 16 - (memslots + nfuncargs) * 8;
004d836a 3412 sp &= ~0xfLL; /* Maintain 16 byte alignment. */
16461d7d 3413
64a5b29c
KB
3414 /* Place the arguments where they belong. The arguments will be
3415 either placed in the RSE backing store or on the memory stack.
3416 In addition, floating point arguments or HFAs are placed in
004d836a 3417 floating point registers. */
16461d7d
KB
3418 slotnum = 0;
3419 floatreg = IA64_FR8_REGNUM;
3420 for (argno = 0; argno < nargs; argno++)
3421 {
64a5b29c
KB
3422 struct type *float_elt_type;
3423
16461d7d 3424 arg = args[argno];
4991999e 3425 type = check_typedef (value_type (arg));
16461d7d 3426 len = TYPE_LENGTH (type);
64a5b29c 3427
004d836a 3428 /* Special handling for function parameters. */
64a5b29c
KB
3429 if (len == 8
3430 && TYPE_CODE (type) == TYPE_CODE_PTR
3431 && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC)
3432 {
3433 char val_buf[8];
9c9acae0 3434 ULONGEST faddr = extract_unsigned_integer (value_contents (arg), 8);
fbd9dcd3 3435 store_unsigned_integer (val_buf, 8,
9c9acae0 3436 find_func_descr (regcache, faddr,
fbd9dcd3 3437 &funcdescaddr));
64a5b29c
KB
3438 if (slotnum < rseslots)
3439 write_memory (rse_address_add (bsp, slotnum), val_buf, 8);
3440 else
3441 write_memory (sp + 16 + 8 * (slotnum - rseslots), val_buf, 8);
3442 slotnum++;
3443 continue;
3444 }
3445
004d836a 3446 /* Normal slots. */
98f96ba1
KB
3447
3448 /* Skip odd slot if necessary... */
3449 if ((slotnum & 1) && slot_alignment_is_next_even (type))
16461d7d 3450 slotnum++;
98f96ba1 3451
16461d7d
KB
3452 argoffset = 0;
3453 while (len > 0)
3454 {
3455 char val_buf[8];
3456
3457 memset (val_buf, 0, 8);
0fd88904 3458 memcpy (val_buf, value_contents (arg) + argoffset, (len > 8) ? 8 : len);
16461d7d
KB
3459
3460 if (slotnum < rseslots)
3461 write_memory (rse_address_add (bsp, slotnum), val_buf, 8);
3462 else
3463 write_memory (sp + 16 + 8 * (slotnum - rseslots), val_buf, 8);
3464
3465 argoffset += 8;
3466 len -= 8;
3467 slotnum++;
3468 }
64a5b29c 3469
004d836a 3470 /* Handle floating point types (including HFAs). */
64a5b29c
KB
3471 float_elt_type = is_float_or_hfa_type (type);
3472 if (float_elt_type != NULL)
3473 {
3474 argoffset = 0;
3475 len = TYPE_LENGTH (type);
3476 while (len > 0 && floatreg < IA64_FR16_REGNUM)
3477 {
004d836a 3478 char to[MAX_REGISTER_SIZE];
0fd88904 3479 convert_typed_floating (value_contents (arg) + argoffset, float_elt_type,
004d836a
JJ
3480 to, builtin_type_ia64_ext);
3481 regcache_cooked_write (regcache, floatreg, (void *)to);
64a5b29c
KB
3482 floatreg++;
3483 argoffset += TYPE_LENGTH (float_elt_type);
3484 len -= TYPE_LENGTH (float_elt_type);
3485 }
16461d7d
KB
3486 }
3487 }
3488
004d836a 3489 /* Store the struct return value in r8 if necessary. */
16461d7d
KB
3490 if (struct_return)
3491 {
004d836a 3492 regcache_cooked_write_unsigned (regcache, IA64_GR8_REGNUM, (ULONGEST)struct_addr);
16461d7d
KB
3493 }
3494
b33e8514 3495 global_pointer = ia64_find_global_pointer (func_addr);
8dd5115e 3496
004d836a 3497 if (global_pointer != 0)
9c9acae0 3498 regcache_cooked_write_unsigned (regcache, IA64_GR1_REGNUM, global_pointer);
a59fe496 3499
9c9acae0 3500 regcache_cooked_write_unsigned (regcache, IA64_BR0_REGNUM, bp_addr);
16461d7d 3501
9c9acae0 3502 regcache_cooked_write_unsigned (regcache, sp_regnum, sp);
16461d7d
KB
3503
3504 return sp;
3505}
3506
004d836a
JJ
3507static struct frame_id
3508ia64_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
16461d7d 3509{
004d836a 3510 char buf[8];
4afcc598 3511 CORE_ADDR sp, bsp;
004d836a
JJ
3512
3513 frame_unwind_register (next_frame, sp_regnum, buf);
3514 sp = extract_unsigned_integer (buf, 8);
3515
4afcc598
JJ
3516 frame_unwind_register (next_frame, IA64_BSP_REGNUM, buf);
3517 bsp = extract_unsigned_integer (buf, 8);
3518
3519 if (gdbarch_debug >= 1)
3520 fprintf_unfiltered (gdb_stdlog,
78ced177
JJ
3521 "dummy frame id: code 0x%s, stack 0x%s, special 0x%s\n",
3522 paddr_nz (frame_pc_unwind (next_frame)),
3523 paddr_nz (sp), paddr_nz (bsp));
4afcc598
JJ
3524
3525 return frame_id_build_special (sp, frame_pc_unwind (next_frame), bsp);
16461d7d
KB
3526}
3527
004d836a
JJ
3528static CORE_ADDR
3529ia64_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
16461d7d 3530{
004d836a
JJ
3531 char buf[8];
3532 CORE_ADDR ip, psr, pc;
3533
3534 frame_unwind_register (next_frame, IA64_IP_REGNUM, buf);
3535 ip = extract_unsigned_integer (buf, 8);
3536 frame_unwind_register (next_frame, IA64_PSR_REGNUM, buf);
3537 psr = extract_unsigned_integer (buf, 8);
3538
3539 pc = (ip & ~0xf) | ((psr >> 41) & 3);
3540 return pc;
16461d7d
KB
3541}
3542
6926787d
AS
3543static int
3544ia64_print_insn (bfd_vma memaddr, struct disassemble_info *info)
3545{
3546 info->bytes_per_line = SLOT_MULTIPLIER;
3547 return print_insn_ia64 (memaddr, info);
3548}
3549
16461d7d
KB
3550static struct gdbarch *
3551ia64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
3552{
3553 struct gdbarch *gdbarch;
244bc108 3554 struct gdbarch_tdep *tdep;
244bc108 3555
85bf2b91
JJ
3556 /* If there is already a candidate, use it. */
3557 arches = gdbarch_list_lookup_by_info (arches, &info);
3558 if (arches != NULL)
3559 return arches->gdbarch;
16461d7d 3560
244bc108
KB
3561 tdep = xmalloc (sizeof (struct gdbarch_tdep));
3562 gdbarch = gdbarch_alloc (&info, tdep);
244bc108 3563
b33e8514 3564 tdep->sigcontext_register_address = 0;
74174d2e 3565 tdep->pc_in_sigtramp = 0;
698cb3f0 3566
004d836a
JJ
3567 /* Define the ia64 floating-point format to gdb. */
3568 builtin_type_ia64_ext =
3569 init_type (TYPE_CODE_FLT, 128 / 8,
3570 0, "builtin_type_ia64_ext", NULL);
8da61cc4 3571 TYPE_FLOATFORMAT (builtin_type_ia64_ext) = floatformats_ia64_ext;
004d836a 3572
5439edaa
AC
3573 /* According to the ia64 specs, instructions that store long double
3574 floats in memory use a long-double format different than that
3575 used in the floating registers. The memory format matches the
3576 x86 extended float format which is 80 bits. An OS may choose to
3577 use this format (e.g. GNU/Linux) or choose to use a different
3578 format for storing long doubles (e.g. HPUX). In the latter case,
3579 the setting of the format may be moved/overridden in an
3580 OS-specific tdep file. */
8da61cc4 3581 set_gdbarch_long_double_format (gdbarch, floatformats_i387_ext);
32edc941 3582
16461d7d
KB
3583 set_gdbarch_short_bit (gdbarch, 16);
3584 set_gdbarch_int_bit (gdbarch, 32);
3585 set_gdbarch_long_bit (gdbarch, 64);
3586 set_gdbarch_long_long_bit (gdbarch, 64);
3587 set_gdbarch_float_bit (gdbarch, 32);
3588 set_gdbarch_double_bit (gdbarch, 64);
33c08150 3589 set_gdbarch_long_double_bit (gdbarch, 128);
16461d7d
KB
3590 set_gdbarch_ptr_bit (gdbarch, 64);
3591
004d836a
JJ
3592 set_gdbarch_num_regs (gdbarch, NUM_IA64_RAW_REGS);
3593 set_gdbarch_num_pseudo_regs (gdbarch, LAST_PSEUDO_REGNUM - FIRST_PSEUDO_REGNUM);
16461d7d 3594 set_gdbarch_sp_regnum (gdbarch, sp_regnum);
698cb3f0 3595 set_gdbarch_fp0_regnum (gdbarch, IA64_FR0_REGNUM);
16461d7d
KB
3596
3597 set_gdbarch_register_name (gdbarch, ia64_register_name);
004d836a 3598 set_gdbarch_register_type (gdbarch, ia64_register_type);
16461d7d 3599
004d836a
JJ
3600 set_gdbarch_pseudo_register_read (gdbarch, ia64_pseudo_register_read);
3601 set_gdbarch_pseudo_register_write (gdbarch, ia64_pseudo_register_write);
3602 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, ia64_dwarf_reg_to_regnum);
3603 set_gdbarch_register_reggroup_p (gdbarch, ia64_register_reggroup_p);
3604 set_gdbarch_convert_register_p (gdbarch, ia64_convert_register_p);
3605 set_gdbarch_register_to_value (gdbarch, ia64_register_to_value);
3606 set_gdbarch_value_to_register (gdbarch, ia64_value_to_register);
16461d7d 3607
004d836a 3608 set_gdbarch_skip_prologue (gdbarch, ia64_skip_prologue);
16461d7d 3609
4c8b6ae0 3610 set_gdbarch_return_value (gdbarch, ia64_return_value);
16461d7d
KB
3611
3612 set_gdbarch_memory_insert_breakpoint (gdbarch, ia64_memory_insert_breakpoint);
3613 set_gdbarch_memory_remove_breakpoint (gdbarch, ia64_memory_remove_breakpoint);
3614 set_gdbarch_breakpoint_from_pc (gdbarch, ia64_breakpoint_from_pc);
3615 set_gdbarch_read_pc (gdbarch, ia64_read_pc);
b33e8514 3616 set_gdbarch_write_pc (gdbarch, ia64_write_pc);
16461d7d
KB
3617
3618 /* Settings for calling functions in the inferior. */
8dd5115e 3619 set_gdbarch_push_dummy_call (gdbarch, ia64_push_dummy_call);
004d836a
JJ
3620 set_gdbarch_frame_align (gdbarch, ia64_frame_align);
3621 set_gdbarch_unwind_dummy_id (gdbarch, ia64_unwind_dummy_id);
16461d7d 3622
004d836a 3623 set_gdbarch_unwind_pc (gdbarch, ia64_unwind_pc);
968d1cb4 3624#ifdef HAVE_LIBUNWIND_IA64_H
c5a27d9c 3625 frame_unwind_append_sniffer (gdbarch, ia64_libunwind_sigtramp_frame_sniffer);
968d1cb4
JJ
3626 frame_unwind_append_sniffer (gdbarch, ia64_libunwind_frame_sniffer);
3627 libunwind_frame_set_descr (gdbarch, &ia64_libunwind_descr);
c5a27d9c
JJ
3628#else
3629 frame_unwind_append_sniffer (gdbarch, ia64_sigtramp_frame_sniffer);
968d1cb4 3630#endif
004d836a
JJ
3631 frame_unwind_append_sniffer (gdbarch, ia64_frame_sniffer);
3632 frame_base_set_default (gdbarch, &ia64_frame_base);
16461d7d
KB
3633
3634 /* Settings that should be unnecessary. */
3635 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
3636
6926787d 3637 set_gdbarch_print_insn (gdbarch, ia64_print_insn);
af8b88dd 3638 set_gdbarch_convert_from_func_ptr_addr (gdbarch, ia64_convert_from_func_ptr_addr);
6926787d 3639
0d5de010
DJ
3640 /* The virtual table contains 16-byte descriptors, not pointers to
3641 descriptors. */
3642 set_gdbarch_vtable_function_descriptors (gdbarch, 1);
3643
b33e8514
AS
3644 /* Hook in ABI-specific overrides, if they have been registered. */
3645 gdbarch_init_osabi (info, gdbarch);
3646
16461d7d
KB
3647 return gdbarch;
3648}
3649
a78f21af
AC
3650extern initialize_file_ftype _initialize_ia64_tdep; /* -Wmissing-prototypes */
3651
16461d7d
KB
3652void
3653_initialize_ia64_tdep (void)
3654{
b33e8514 3655 gdbarch_register (bfd_arch_ia64, ia64_gdbarch_init, NULL);
16461d7d 3656}
This page took 0.79777 seconds and 4 git commands to generate.