ChangeLog change only.
[deliverable/binutils-gdb.git] / gdb / ppc-linux-tdep.c
CommitLineData
c877c8e6
KB
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.
4
5 This file is part of GDB.
6
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.
11
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.
16
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. */
21
22#include "defs.h"
23#include "frame.h"
24#include "inferior.h"
25#include "symtab.h"
26#include "target.h"
27#include "gdbcore.h"
28#include "gdbcmd.h"
29#include "symfile.h"
30#include "objfiles.h"
31
32/* The following two instructions are used in the signal trampoline
33 code on linux/ppc */
34#define INSTR_LI_R0_0x7777 0x38007777
35#define INSTR_SC 0x44000002
36
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
42 as well. */
43
44/* __SIGNAL_FRAMESIZE from <asm/ptrace.h> */
45#define PPC_LINUX_SIGNAL_FRAMESIZE 64
46
47/* From <asm/sigcontext.h>, offsetof(struct sigcontext_struct, regs) == 0x1c */
48#define PPC_LINUX_REGS_PTR_OFFSET (PPC_LINUX_SIGNAL_FRAMESIZE + 0x1c)
49
50/* From <asm/sigcontext.h>,
51 offsetof(struct sigcontext_struct, handler) == 0x14 */
52#define PPC_LINUX_HANDLER_PTR_OFFSET (PPC_LINUX_SIGNAL_FRAMESIZE + 0x14)
53
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)
97
98/* Determine if pc is in a signal trampoline...
99
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.
105
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
110 again.
111
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
115 has been run.
116
117 So, we define in_sigtramp() below to return 1 if the following is
118 true:
119
120 1) The previous frame is a real signal trampoline.
121
122 - and -
123
124 2) pc is at the first or second instruction of the corresponding
125 handler.
126
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.)
135
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(). */
141
142int
143ppc_linux_in_sigtramp (CORE_ADDR pc, char *func_name)
144{
145 CORE_ADDR lr;
146 CORE_ADDR sp;
147 CORE_ADDR tramp_sp;
148 char buf[4];
149 CORE_ADDR handler;
150
151 lr = read_register (LR_REGNUM);
152 if (!ppc_linux_at_sigtramp_return_path (lr))
153 return 0;
154
155 sp = read_register (SP_REGNUM);
156
157 if (target_read_memory (sp, buf, sizeof (buf)) != 0)
158 return 0;
159
160 tramp_sp = extract_unsigned_integer (buf, 4);
161
162 if (target_read_memory (tramp_sp + PPC_LINUX_HANDLER_PTR_OFFSET, buf,
163 sizeof (buf)) != 0)
164 return 0;
165
166 handler = extract_unsigned_integer (buf, 4);
167
168 return (pc == handler || pc == handler + 4);
169}
170
171/*
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.
177 */
178int
179ppc_linux_at_sigtramp_return_path (CORE_ADDR pc)
180{
181 char buf[12];
182 unsigned long pcinsn;
183 if (target_read_memory (pc - 4, buf, sizeof (buf)) != 0)
184 return 0;
185
186 /* extract the instruction at the pc */
187 pcinsn = extract_unsigned_integer (buf + 4, 4);
188
189 return (
190 (pcinsn == INSTR_LI_R0_0x7777
191 && extract_unsigned_integer (buf + 8, 4) == INSTR_SC)
192 ||
193 (pcinsn == INSTR_SC
194 && extract_unsigned_integer (buf, 4) == INSTR_LI_R0_0x7777));
195}
196
197CORE_ADDR
198ppc_linux_skip_trampoline_code (CORE_ADDR pc)
199{
200 char buf[4];
201 struct obj_section *sect;
202 struct objfile *objfile;
203 unsigned long insn;
204 CORE_ADDR plt_start = 0;
205 CORE_ADDR symtab = 0;
206 CORE_ADDR strtab = 0;
207 int num_slots = -1;
208 int reloc_index = -1;
209 CORE_ADDR plt_table;
210 CORE_ADDR reloc;
211 CORE_ADDR sym;
212 long symidx;
213 char symname[1024];
214 struct minimal_symbol *msymbol;
215
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)
219 return 0;
220
221 objfile = sect->objfile;
222
223 /* Pick up the instruction at pc. It had better be of the
224 form
225 li r11, IDX
226
227 where IDX is an index into the plt_table. */
228
229 if (target_read_memory (pc, buf, 4) != 0)
230 return 0;
231 insn = extract_unsigned_integer (buf, 4);
232
233 if ((insn & 0xffff0000) != 0x39600000 /* li r11, VAL */ )
234 return 0;
235
236 reloc_index = (insn << 16) >> 16;
237
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)
241 {
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)
248 symtab = sect->addr;
249 else if (strcmp (secname, ".dynstr") == 0)
250 strtab = sect->addr;
251 }
252
253 /* Make sure we have all the information we need. */
254 if (plt_start == 0 || num_slots == -1 || symtab == 0 || strtab == 0)
255 return 0;
256
257 /* Compute the value of the plt table */
258 plt_table = plt_start + 72 + 8 * num_slots;
259
260 /* Get address of the relocation entry (Elf32_Rela) */
261 if (target_read_memory (plt_table + reloc_index, buf, 4) != 0)
262 return 0;
263 reloc = extract_address (buf, 4);
264
265 sect = find_pc_section (reloc);
266 if (!sect)
267 return 0;
268
269 if (strcmp (sect->the_bfd_section->name, ".text") == 0)
270 return reloc;
271
272 /* Now get the r_info field which is the relocation type and symbol
273 index. */
274 if (target_read_memory (reloc + 4, buf, 4) != 0)
275 return 0;
276 symidx = extract_unsigned_integer (buf, 4);
277
278 /* Shift out the relocation type leaving just the symbol index */
279 /* symidx = ELF32_R_SYM(symidx); */
280 symidx = symidx >> 8;
281
282 /* compute the address of the symbol */
283 sym = symtab + symidx * 4;
284
285 /* Fetch the string table index */
286 if (target_read_memory (sym, buf, 4) != 0)
287 return 0;
288 symidx = extract_unsigned_integer (buf, 4);
289
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
292 much? */
293 if (target_read_memory (strtab + symidx, symname, sizeof (symname)) != 0)
294 return 0;
295
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);
300 if (!msymbol)
301 return 0;
302
303 return SYMBOL_VALUE_ADDRESS (msymbol);
304}
305
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. */
309unsigned long
310ppc_linux_frame_saved_pc (struct frame_info *fi)
311{
312 if (fi->signal_handler_caller)
313 {
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);
318 }
319
320 return rs6000_frame_saved_pc (fi);
321}
322
323void
324ppc_linux_init_extra_frame_info (int fromleaf, struct frame_info *fi)
325{
326 rs6000_init_extra_frame_info (fromleaf, fi);
327
328 if (fi->next != 0)
329 {
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;
335 else
336 fi->signal_handler_caller = 0;
337 }
338}
339
340int
341ppc_linux_frameless_function_invocation (struct frame_info *fi)
342{
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))
346 return 0;
347 else
348 return rs6000_frameless_function_invocation (fi);
349}
350
351void
352ppc_linux_frame_init_saved_regs (struct frame_info *fi)
353{
354 if (fi->signal_handler_caller)
355 {
356 CORE_ADDR regs_addr;
357 int i;
358 if (fi->saved_regs)
359 return;
360
361 frame_saved_regs_zalloc (fi);
362
363 regs_addr =
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;
376 }
377 else
378 rs6000_frame_init_saved_regs (fi);
379}
380
381CORE_ADDR
382ppc_linux_frame_chain (struct frame_info *thisframe)
383{
384 /* Kernel properly constructs the frame chain for the handler */
385 if (thisframe->signal_handler_caller)
386 return read_memory_integer ((thisframe)->frame, 4);
387 else
388 return rs6000_frame_chain (thisframe);
389}
390
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
393 the EABI */
394
395/* round2 rounds x up to the nearest multiple of s assuming that s is a
396 power of 2 */
397
398#undef round2
399#define round2(x,s) ((((long) (x) - 1) & ~(long)((s)-1)) + (s))
400
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.
407
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,
410 starting from r4. */
411
412CORE_ADDR
413ppc_sysv_abi_push_arguments (nargs, args, sp, struct_return, struct_addr)
414 int nargs;
415 value_ptr *args;
416 CORE_ADDR sp;
417 int struct_return;
418 CORE_ADDR struct_addr;
419{
420 int argno;
421 int greg, freg;
422 int argstkspace;
423 int structstkspace;
424 int argoffset;
425 int structoffset;
426 value_ptr arg;
427 struct type *type;
428 int len;
429 char old_sp_buf[4];
430 CORE_ADDR saved_sp;
431
432 greg = struct_return ? 4 : 3;
433 freg = 1;
434 argstkspace = 0;
435 structstkspace = 0;
436
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++)
442 {
443 arg = args[argno];
444 type = check_typedef (VALUE_TYPE (arg));
445 len = TYPE_LENGTH (type);
446
447 if (TYPE_CODE (type) == TYPE_CODE_FLT)
448 {
449 if (freg <= 8)
450 freg++;
451 else
452 {
453 /* SysV ABI converts floats to doubles when placed in
454 memory and requires 8 byte alignment */
455 if (argstkspace & 0x4)
456 argstkspace += 4;
457 argstkspace += 8;
458 }
459 }
460 else if (TYPE_CODE (type) == TYPE_CODE_INT && len == 8) /* long long */
461 {
462 if (greg > 9)
463 {
464 greg = 11;
465 if (argstkspace & 0x4)
466 argstkspace += 4;
467 argstkspace += 8;
468 }
469 else
470 {
471 if ((greg & 1) == 0)
472 greg++;
473 greg += 2;
474 }
475 }
476 else
477 {
478 if (len > 4
479 || TYPE_CODE (type) == TYPE_CODE_STRUCT
480 || TYPE_CODE (type) == TYPE_CODE_UNION)
481 {
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);
486 }
487 if (greg <= 10)
488 greg++;
489 else
490 argstkspace += 4;
491 }
492 }
493
494 /* Get current SP location */
495 saved_sp = read_sp ();
496
497 sp -= argstkspace + structstkspace;
498
499 /* Allocate space for backchain and callee's saved lr */
500 sp -= 8;
501
502 /* Make sure that we maintain 16 byte alignment */
503 sp &= ~0x0f;
504
505 /* Update %sp before proceeding any further */
506 write_register (SP_REGNUM, sp);
507
508 /* write the backchain */
509 store_address (old_sp_buf, 4, saved_sp);
510 write_memory (sp, old_sp_buf, 4);
511
512 argoffset = 8;
513 structoffset = argoffset + argstkspace;
514 freg = 1;
515 greg = 3;
516 /* Now fill in the registers and stack... */
517 for (argno = 0; argno < nargs; argno++)
518 {
519 arg = args[argno];
520 type = check_typedef (VALUE_TYPE (arg));
521 len = TYPE_LENGTH (type);
522
523 if (TYPE_CODE (type) == TYPE_CODE_FLT)
524 {
525 if (freg <= 8)
526 {
527 if (len > 8)
528 printf_unfiltered (
529 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
530 memcpy (&registers[REGISTER_BYTE (FP0_REGNUM + freg)],
531 VALUE_CONTENTS (arg), len);
532 freg++;
533 }
534 else
535 {
536 /* SysV ABI converts floats to doubles when placed in
537 memory and requires 8 byte alignment */
538 /* FIXME: Convert floats to doubles */
539 if (argoffset & 0x4)
540 argoffset += 4;
541 write_memory (sp + argoffset, (char *) VALUE_CONTENTS (arg), len);
542 argoffset += 8;
543 }
544 }
545 else if (TYPE_CODE (type) == TYPE_CODE_INT && len == 8) /* long long */
546 {
547 if (greg > 9)
548 {
549 greg = 11;
550 if (argoffset & 0x4)
551 argoffset += 4;
552 write_memory (sp + argoffset, (char *) VALUE_CONTENTS (arg), len);
553 argoffset += 8;
554 }
555 else
556 {
557 if ((greg & 1) == 0)
558 greg++;
559
560 memcpy (&registers[REGISTER_BYTE (greg)],
561 VALUE_CONTENTS (arg), 4);
562 memcpy (&registers[REGISTER_BYTE (greg + 1)],
563 VALUE_CONTENTS (arg) + 4, 4);
564 greg += 2;
565 }
566 }
567 else
568 {
569 char val_buf[4];
570 if (len > 4
571 || TYPE_CODE (type) == TYPE_CODE_STRUCT
572 || TYPE_CODE (type) == TYPE_CODE_UNION)
573 {
574 write_memory (sp + structoffset, VALUE_CONTENTS (arg), len);
575 store_address (val_buf, 4, sp + structoffset);
576 structoffset += round2 (len, 8);
577 }
578 else
579 {
580 memset (val_buf, 0, 4);
581 memcpy (val_buf, VALUE_CONTENTS (arg), len);
582 }
583 if (greg <= 10)
584 {
585 *(int *) &registers[REGISTER_BYTE (greg)] = 0;
586 memcpy (&registers[REGISTER_BYTE (greg)], val_buf, 4);
587 greg++;
588 }
589 else
590 {
591 write_memory (sp + argoffset, val_buf, 4);
592 argoffset += 4;
593 }
594 }
595 }
596
597 target_store_registers (-1);
598 return sp;
599}
This page took 0.043773 seconds and 4 git commands to generate.