1 /* Target-dependent code for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
32 /* The following two instructions are used in the signal trampoline
34 #define INSTR_LI_R0_0x7777 0x38007777
35 #define INSTR_SC 0x44000002
37 /* Since the *-tdep.c files are platform independent (i.e, they may be
38 used to build cross platform debuggers), we can't include system
39 headers. Therefore, details concerning the sigcontext structure
40 must be painstakingly rerecorded. What's worse, if these details
41 ever change in the header files, they'll have to be changed here
44 /* __SIGNAL_FRAMESIZE from <asm/ptrace.h> */
45 #define PPC_LINUX_SIGNAL_FRAMESIZE 64
47 /* From <asm/sigcontext.h>, offsetof(struct sigcontext_struct, regs) == 0x1c */
48 #define PPC_LINUX_REGS_PTR_OFFSET (PPC_LINUX_SIGNAL_FRAMESIZE + 0x1c)
50 /* From <asm/sigcontext.h>,
51 offsetof(struct sigcontext_struct, handler) == 0x14 */
52 #define PPC_LINUX_HANDLER_PTR_OFFSET (PPC_LINUX_SIGNAL_FRAMESIZE + 0x14)
54 /* From <asm/ptrace.h>, values for PT_NIP, PT_R1, and PT_LNK */
55 #define PPC_LINUX_PT_R0 0
56 #define PPC_LINUX_PT_R1 1
57 #define PPC_LINUX_PT_R2 2
58 #define PPC_LINUX_PT_R3 3
59 #define PPC_LINUX_PT_R4 4
60 #define PPC_LINUX_PT_R5 5
61 #define PPC_LINUX_PT_R6 6
62 #define PPC_LINUX_PT_R7 7
63 #define PPC_LINUX_PT_R8 8
64 #define PPC_LINUX_PT_R9 9
65 #define PPC_LINUX_PT_R10 10
66 #define PPC_LINUX_PT_R11 11
67 #define PPC_LINUX_PT_R12 12
68 #define PPC_LINUX_PT_R13 13
69 #define PPC_LINUX_PT_R14 14
70 #define PPC_LINUX_PT_R15 15
71 #define PPC_LINUX_PT_R16 16
72 #define PPC_LINUX_PT_R17 17
73 #define PPC_LINUX_PT_R18 18
74 #define PPC_LINUX_PT_R19 19
75 #define PPC_LINUX_PT_R20 20
76 #define PPC_LINUX_PT_R21 21
77 #define PPC_LINUX_PT_R22 22
78 #define PPC_LINUX_PT_R23 23
79 #define PPC_LINUX_PT_R24 24
80 #define PPC_LINUX_PT_R25 25
81 #define PPC_LINUX_PT_R26 26
82 #define PPC_LINUX_PT_R27 27
83 #define PPC_LINUX_PT_R28 28
84 #define PPC_LINUX_PT_R29 29
85 #define PPC_LINUX_PT_R30 30
86 #define PPC_LINUX_PT_R31 31
87 #define PPC_LINUX_PT_NIP 32
88 #define PPC_LINUX_PT_MSR 33
89 #define PPC_LINUX_PT_CTR 35
90 #define PPC_LINUX_PT_LNK 36
91 #define PPC_LINUX_PT_XER 37
92 #define PPC_LINUX_PT_CCR 38
93 #define PPC_LINUX_PT_MQ 39
94 #define PPC_LINUX_PT_FPR0 48 /* each FP reg occupies 2 slots in this space */
95 #define PPC_LINUX_PT_FPR31 (PPC_LINUX_PT_FPR0 + 2*31)
96 #define PPC_LINUX_PT_FPSCR (PPC_LINUX_PT_FPR0 + 2*32 + 1)
98 /* Determine if pc is in a signal trampoline...
100 Ha! That's not what this does at all. wait_for_inferior in infrun.c
101 calls IN_SIGTRAMP in order to detect entry into a signal trampoline
102 just after delivery of a signal. But on linux, signal trampolines
103 are used for the return path only. The kernel sets things up so that
104 the signal handler is called directly.
106 If we use in_sigtramp2() in place of in_sigtramp() (see below)
107 we'll (often) end up with stop_pc in the trampoline and prev_pc in
108 the (now exited) handler. The code there will cause a temporary
109 breakpoint to be set on prev_pc which is not very likely to get hit
112 If this is confusing, think of it this way... the code in
113 wait_for_inferior() needs to be able to detect entry into a signal
114 trampoline just after a signal is delivered, not after the handler
117 So, we define in_sigtramp() below to return 1 if the following is
120 1) The previous frame is a real signal trampoline.
124 2) pc is at the first or second instruction of the corresponding
127 Why the second instruction? It seems that wait_for_inferior()
128 never sees the first instruction when single stepping. When a
129 signal is delivered while stepping, the next instruction that
130 would've been stepped over isn't, instead a signal is delivered and
131 the first instruction of the handler is stepped over instead. That
132 puts us on the second instruction. (I added the test for the
133 first instruction long after the fact, just in case the observed
134 behavior is ever fixed.)
136 IN_SIGTRAMP is called from blockframe.c as well in order to set
137 the signal_handler_caller flag. Because of our strange definition
138 of in_sigtramp below, we can't rely on signal_handler_caller getting
139 set correctly from within blockframe.c. This is why we take pains
140 to set it in init_extra_frame_info(). */
143 ppc_linux_in_sigtramp (CORE_ADDR pc, char *func_name)
151 lr = read_register (LR_REGNUM);
152 if (!ppc_linux_at_sigtramp_return_path (lr))
155 sp = read_register (SP_REGNUM);
157 if (target_read_memory (sp, buf, sizeof (buf)) != 0)
160 tramp_sp = extract_unsigned_integer (buf, 4);
162 if (target_read_memory (tramp_sp + PPC_LINUX_HANDLER_PTR_OFFSET, buf,
166 handler = extract_unsigned_integer (buf, 4);
168 return (pc == handler || pc == handler + 4);
172 * The signal handler trampoline is on the stack and consists of exactly
173 * two instructions. The easiest and most accurate way of determining
174 * whether the pc is in one of these trampolines is by inspecting the
175 * instructions. It'd be faster though if we could find a way to do this
176 * via some simple address comparisons.
179 ppc_linux_at_sigtramp_return_path (CORE_ADDR pc)
182 unsigned long pcinsn;
183 if (target_read_memory (pc - 4, buf, sizeof (buf)) != 0)
186 /* extract the instruction at the pc */
187 pcinsn = extract_unsigned_integer (buf + 4, 4);
190 (pcinsn == INSTR_LI_R0_0x7777
191 && extract_unsigned_integer (buf + 8, 4) == INSTR_SC)
194 && extract_unsigned_integer (buf, 4) == INSTR_LI_R0_0x7777));
198 ppc_linux_skip_trampoline_code (CORE_ADDR pc)
201 struct obj_section *sect;
202 struct objfile *objfile;
204 CORE_ADDR plt_start = 0;
205 CORE_ADDR symtab = 0;
206 CORE_ADDR strtab = 0;
208 int reloc_index = -1;
214 struct minimal_symbol *msymbol;
216 /* Find the section pc is in; return if not in .plt */
217 sect = find_pc_section (pc);
218 if (!sect || strcmp (sect->the_bfd_section->name, ".plt") != 0)
221 objfile = sect->objfile;
223 /* Pick up the instruction at pc. It had better be of the
227 where IDX is an index into the plt_table. */
229 if (target_read_memory (pc, buf, 4) != 0)
231 insn = extract_unsigned_integer (buf, 4);
233 if ((insn & 0xffff0000) != 0x39600000 /* li r11, VAL */ )
236 reloc_index = (insn << 16) >> 16;
238 /* Find the objfile that pc is in and obtain the information
239 necessary for finding the symbol name. */
240 for (sect = objfile->sections; sect < objfile->sections_end; ++sect)
242 const char *secname = sect->the_bfd_section->name;
243 if (strcmp (secname, ".plt") == 0)
244 plt_start = sect->addr;
245 else if (strcmp (secname, ".rela.plt") == 0)
246 num_slots = ((int) sect->endaddr - (int) sect->addr) / 12;
247 else if (strcmp (secname, ".dynsym") == 0)
249 else if (strcmp (secname, ".dynstr") == 0)
253 /* Make sure we have all the information we need. */
254 if (plt_start == 0 || num_slots == -1 || symtab == 0 || strtab == 0)
257 /* Compute the value of the plt table */
258 plt_table = plt_start + 72 + 8 * num_slots;
260 /* Get address of the relocation entry (Elf32_Rela) */
261 if (target_read_memory (plt_table + reloc_index, buf, 4) != 0)
263 reloc = extract_address (buf, 4);
265 sect = find_pc_section (reloc);
269 if (strcmp (sect->the_bfd_section->name, ".text") == 0)
272 /* Now get the r_info field which is the relocation type and symbol
274 if (target_read_memory (reloc + 4, buf, 4) != 0)
276 symidx = extract_unsigned_integer (buf, 4);
278 /* Shift out the relocation type leaving just the symbol index */
279 /* symidx = ELF32_R_SYM(symidx); */
280 symidx = symidx >> 8;
282 /* compute the address of the symbol */
283 sym = symtab + symidx * 4;
285 /* Fetch the string table index */
286 if (target_read_memory (sym, buf, 4) != 0)
288 symidx = extract_unsigned_integer (buf, 4);
290 /* Fetch the string; we don't know how long it is. Is it possible
291 that the following will fail because we're trying to fetch too
293 if (target_read_memory (strtab + symidx, symname, sizeof (symname)) != 0)
296 /* This might not work right if we have multiple symbols with the
297 same name; the only way to really get it right is to perform
298 the same sort of lookup as the dynamic linker. */
299 msymbol = lookup_minimal_symbol_text (symname, NULL, NULL);
303 return SYMBOL_VALUE_ADDRESS (msymbol);
306 /* The rs6000 version of FRAME_SAVED_PC will almost work for us. The
307 signal handler details are different, so we'll handle those here
308 and call the rs6000 version to do the rest. */
310 ppc_linux_frame_saved_pc (struct frame_info *fi)
312 if (fi->signal_handler_caller)
314 CORE_ADDR regs_addr =
315 read_memory_integer (fi->frame + PPC_LINUX_REGS_PTR_OFFSET, 4);
316 /* return the NIP in the regs array */
317 return read_memory_integer (regs_addr + 4 * PPC_LINUX_PT_NIP, 4);
320 return rs6000_frame_saved_pc (fi);
324 ppc_linux_init_extra_frame_info (int fromleaf, struct frame_info *fi)
326 rs6000_init_extra_frame_info (fromleaf, fi);
330 /* We're called from get_prev_frame_info; check to see if
331 this is a signal frame by looking to see if the pc points
332 at trampoline code */
333 if (ppc_linux_at_sigtramp_return_path (fi->pc))
334 fi->signal_handler_caller = 1;
336 fi->signal_handler_caller = 0;
341 ppc_linux_frameless_function_invocation (struct frame_info *fi)
343 /* We'll find the wrong thing if we let
344 rs6000_frameless_function_invocation () search for a signal trampoline */
345 if (ppc_linux_at_sigtramp_return_path (fi->pc))
348 return rs6000_frameless_function_invocation (fi);
352 ppc_linux_frame_init_saved_regs (struct frame_info *fi)
354 if (fi->signal_handler_caller)
361 frame_saved_regs_zalloc (fi);
364 read_memory_integer (fi->frame + PPC_LINUX_REGS_PTR_OFFSET, 4);
365 fi->saved_regs[PC_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_NIP;
366 fi->saved_regs[PS_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_MSR;
367 fi->saved_regs[CR_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_CCR;
368 fi->saved_regs[LR_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_LNK;
369 fi->saved_regs[CTR_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_CTR;
370 fi->saved_regs[XER_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_XER;
371 fi->saved_regs[MQ_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_MQ;
372 for (i = 0; i < 32; i++)
373 fi->saved_regs[GP0_REGNUM + i] = regs_addr + 4 * PPC_LINUX_PT_R0 + 4 * i;
374 for (i = 0; i < 32; i++)
375 fi->saved_regs[FP0_REGNUM + i] = regs_addr + 4 * PPC_LINUX_PT_FPR0 + 8 * i;
378 rs6000_frame_init_saved_regs (fi);
382 ppc_linux_frame_chain (struct frame_info *thisframe)
384 /* Kernel properly constructs the frame chain for the handler */
385 if (thisframe->signal_handler_caller)
386 return read_memory_integer ((thisframe)->frame, 4);
388 return rs6000_frame_chain (thisframe);
391 /* FIXME: Move the following to rs6000-tdep.c (or some other file where
392 it may be used generically by ports which use either the SysV ABI or
395 /* round2 rounds x up to the nearest multiple of s assuming that s is a
399 #define round2(x,s) ((((long) (x) - 1) & ~(long)((s)-1)) + (s))
401 /* Pass the arguments in either registers, or in the stack. Using the
402 ppc sysv ABI, the first eight words of the argument list (that might
403 be less than eight parameters if some parameters occupy more than one
404 word) are passed in r3..r10 registers. float and double parameters are
405 passed in fpr's, in addition to that. Rest of the parameters if any
406 are passed in user stack.
408 If the function is returning a structure, then the return address is passed
409 in r3, then the first 7 words of the parametes can be passed in registers,
413 ppc_sysv_abi_push_arguments (nargs, args, sp, struct_return, struct_addr)
418 CORE_ADDR struct_addr;
432 greg = struct_return ? 4 : 3;
437 /* Figure out how much new stack space is required for arguments
438 which don't fit in registers. Unlike the PowerOpen ABI, the
439 SysV ABI doesn't reserve any extra space for parameters which
440 are put in registers. */
441 for (argno = 0; argno < nargs; argno++)
444 type = check_typedef (VALUE_TYPE (arg));
445 len = TYPE_LENGTH (type);
447 if (TYPE_CODE (type) == TYPE_CODE_FLT)
453 /* SysV ABI converts floats to doubles when placed in
454 memory and requires 8 byte alignment */
455 if (argstkspace & 0x4)
460 else if (TYPE_CODE (type) == TYPE_CODE_INT && len == 8) /* long long */
465 if (argstkspace & 0x4)
479 || TYPE_CODE (type) == TYPE_CODE_STRUCT
480 || TYPE_CODE (type) == TYPE_CODE_UNION)
482 /* Rounding to the nearest multiple of 8 may not be necessary,
483 but it is safe. Particularly since we don't know the
484 field types of the structure */
485 structstkspace += round2 (len, 8);
494 /* Get current SP location */
495 saved_sp = read_sp ();
497 sp -= argstkspace + structstkspace;
499 /* Allocate space for backchain and callee's saved lr */
502 /* Make sure that we maintain 16 byte alignment */
505 /* Update %sp before proceeding any further */
506 write_register (SP_REGNUM, sp);
508 /* write the backchain */
509 store_address (old_sp_buf, 4, saved_sp);
510 write_memory (sp, old_sp_buf, 4);
513 structoffset = argoffset + argstkspace;
516 /* Now fill in the registers and stack... */
517 for (argno = 0; argno < nargs; argno++)
520 type = check_typedef (VALUE_TYPE (arg));
521 len = TYPE_LENGTH (type);
523 if (TYPE_CODE (type) == TYPE_CODE_FLT)
529 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
530 memcpy (®isters[REGISTER_BYTE (FP0_REGNUM + freg)],
531 VALUE_CONTENTS (arg), len);
536 /* SysV ABI converts floats to doubles when placed in
537 memory and requires 8 byte alignment */
538 /* FIXME: Convert floats to doubles */
541 write_memory (sp + argoffset, (char *) VALUE_CONTENTS (arg), len);
545 else if (TYPE_CODE (type) == TYPE_CODE_INT && len == 8) /* long long */
552 write_memory (sp + argoffset, (char *) VALUE_CONTENTS (arg), len);
560 memcpy (®isters[REGISTER_BYTE (greg)],
561 VALUE_CONTENTS (arg), 4);
562 memcpy (®isters[REGISTER_BYTE (greg + 1)],
563 VALUE_CONTENTS (arg) + 4, 4);
571 || TYPE_CODE (type) == TYPE_CODE_STRUCT
572 || TYPE_CODE (type) == TYPE_CODE_UNION)
574 write_memory (sp + structoffset, VALUE_CONTENTS (arg), len);
575 store_address (val_buf, 4, sp + structoffset);
576 structoffset += round2 (len, 8);
580 memset (val_buf, 0, 4);
581 memcpy (val_buf, VALUE_CONTENTS (arg), len);
585 *(int *) ®isters[REGISTER_BYTE (greg)] = 0;
586 memcpy (®isters[REGISTER_BYTE (greg)], val_buf, 4);
591 write_memory (sp + argoffset, val_buf, 4);
597 target_store_registers (-1);