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