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