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