1 /* Target-dependent code for GDB, the GNU debugger.
3 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
36 #include "solib-svr4.h"
38 #include "trad-frame.h"
39 #include "frame-unwind.h"
41 /* The following instructions are used in the signal trampoline code
42 on GNU/Linux PPC. The kernel used to use magic syscalls 0x6666 and
43 0x7777 but now uses the sigreturn syscalls. We check for both. */
44 #define INSTR_LI_R0_0x6666 0x38006666
45 #define INSTR_LI_R0_0x7777 0x38007777
46 #define INSTR_LI_R0_NR_sigreturn 0x38000077
47 #define INSTR_LI_R0_NR_rt_sigreturn 0x380000AC
49 #define INSTR_SC 0x44000002
51 /* Since the *-tdep.c files are platform independent (i.e, they may be
52 used to build cross platform debuggers), we can't include system
53 headers. Therefore, details concerning the sigcontext structure
54 must be painstakingly rerecorded. What's worse, if these details
55 ever change in the header files, they'll have to be changed here
58 /* __SIGNAL_FRAMESIZE from <asm/ptrace.h> */
59 #define PPC_LINUX_SIGNAL_FRAMESIZE 64
61 /* From <asm/sigcontext.h>, offsetof(struct sigcontext_struct, regs) == 0x1c */
62 #define PPC_LINUX_REGS_PTR_OFFSET (PPC_LINUX_SIGNAL_FRAMESIZE + 0x1c)
64 /* From <asm/sigcontext.h>,
65 offsetof(struct sigcontext_struct, handler) == 0x14 */
66 #define PPC_LINUX_HANDLER_PTR_OFFSET (PPC_LINUX_SIGNAL_FRAMESIZE + 0x14)
68 /* From <asm/ptrace.h>, values for PT_NIP, PT_R1, and PT_LNK */
69 #define PPC_LINUX_PT_R0 0
70 #define PPC_LINUX_PT_R1 1
71 #define PPC_LINUX_PT_R2 2
72 #define PPC_LINUX_PT_R3 3
73 #define PPC_LINUX_PT_R4 4
74 #define PPC_LINUX_PT_R5 5
75 #define PPC_LINUX_PT_R6 6
76 #define PPC_LINUX_PT_R7 7
77 #define PPC_LINUX_PT_R8 8
78 #define PPC_LINUX_PT_R9 9
79 #define PPC_LINUX_PT_R10 10
80 #define PPC_LINUX_PT_R11 11
81 #define PPC_LINUX_PT_R12 12
82 #define PPC_LINUX_PT_R13 13
83 #define PPC_LINUX_PT_R14 14
84 #define PPC_LINUX_PT_R15 15
85 #define PPC_LINUX_PT_R16 16
86 #define PPC_LINUX_PT_R17 17
87 #define PPC_LINUX_PT_R18 18
88 #define PPC_LINUX_PT_R19 19
89 #define PPC_LINUX_PT_R20 20
90 #define PPC_LINUX_PT_R21 21
91 #define PPC_LINUX_PT_R22 22
92 #define PPC_LINUX_PT_R23 23
93 #define PPC_LINUX_PT_R24 24
94 #define PPC_LINUX_PT_R25 25
95 #define PPC_LINUX_PT_R26 26
96 #define PPC_LINUX_PT_R27 27
97 #define PPC_LINUX_PT_R28 28
98 #define PPC_LINUX_PT_R29 29
99 #define PPC_LINUX_PT_R30 30
100 #define PPC_LINUX_PT_R31 31
101 #define PPC_LINUX_PT_NIP 32
102 #define PPC_LINUX_PT_MSR 33
103 #define PPC_LINUX_PT_CTR 35
104 #define PPC_LINUX_PT_LNK 36
105 #define PPC_LINUX_PT_XER 37
106 #define PPC_LINUX_PT_CCR 38
107 #define PPC_LINUX_PT_MQ 39
108 #define PPC_LINUX_PT_FPR0 48 /* each FP reg occupies 2 slots in this space */
109 #define PPC_LINUX_PT_FPR31 (PPC_LINUX_PT_FPR0 + 2*31)
110 #define PPC_LINUX_PT_FPSCR (PPC_LINUX_PT_FPR0 + 2*32 + 1)
112 static int ppc_linux_at_sigtramp_return_path (CORE_ADDR pc);
114 /* Determine if pc is in a signal trampoline...
116 Ha! That's not what this does at all. wait_for_inferior in
117 infrun.c calls PC_IN_SIGTRAMP in order to detect entry into a
118 signal trampoline just after delivery of a signal. But on
119 GNU/Linux, signal trampolines are used for the return path only.
120 The kernel sets things up so that the signal handler is called
123 If we use in_sigtramp2() in place of in_sigtramp() (see below)
124 we'll (often) end up with stop_pc in the trampoline and prev_pc in
125 the (now exited) handler. The code there will cause a temporary
126 breakpoint to be set on prev_pc which is not very likely to get hit
129 If this is confusing, think of it this way... the code in
130 wait_for_inferior() needs to be able to detect entry into a signal
131 trampoline just after a signal is delivered, not after the handler
134 So, we define in_sigtramp() below to return 1 if the following is
137 1) The previous frame is a real signal trampoline.
141 2) pc is at the first or second instruction of the corresponding
144 Why the second instruction? It seems that wait_for_inferior()
145 never sees the first instruction when single stepping. When a
146 signal is delivered while stepping, the next instruction that
147 would've been stepped over isn't, instead a signal is delivered and
148 the first instruction of the handler is stepped over instead. That
149 puts us on the second instruction. (I added the test for the
150 first instruction long after the fact, just in case the observed
151 behavior is ever fixed.)
153 PC_IN_SIGTRAMP is called from blockframe.c as well in order to set
154 the frame's type (if a SIGTRAMP_FRAME). Because of our strange
155 definition of in_sigtramp below, we can't rely on the frame's type
156 getting set correctly from within blockframe.c. This is why we
157 take pains to set it in init_extra_frame_info().
159 NOTE: cagney/2002-11-10: I suspect the real problem here is that
160 the get_prev_frame() only initializes the frame's type after the
161 call to INIT_FRAME_INFO. get_prev_frame() should be fixed, this
162 code shouldn't be working its way around a bug :-(. */
165 ppc_linux_in_sigtramp (CORE_ADDR pc, char *func_name)
173 lr = read_register (gdbarch_tdep (current_gdbarch)->ppc_lr_regnum);
174 if (!ppc_linux_at_sigtramp_return_path (lr))
177 sp = read_register (SP_REGNUM);
179 if (target_read_memory (sp, buf, sizeof (buf)) != 0)
182 tramp_sp = extract_unsigned_integer (buf, 4);
184 if (target_read_memory (tramp_sp + PPC_LINUX_HANDLER_PTR_OFFSET, buf,
188 handler = extract_unsigned_integer (buf, 4);
190 return (pc == handler || pc == handler + 4);
194 insn_is_sigreturn (unsigned long pcinsn)
198 case INSTR_LI_R0_0x6666:
199 case INSTR_LI_R0_0x7777:
200 case INSTR_LI_R0_NR_sigreturn:
201 case INSTR_LI_R0_NR_rt_sigreturn:
209 * The signal handler trampoline is on the stack and consists of exactly
210 * two instructions. The easiest and most accurate way of determining
211 * whether the pc is in one of these trampolines is by inspecting the
212 * instructions. It'd be faster though if we could find a way to do this
213 * via some simple address comparisons.
216 ppc_linux_at_sigtramp_return_path (CORE_ADDR pc)
219 unsigned long pcinsn;
220 if (target_read_memory (pc - 4, buf, sizeof (buf)) != 0)
223 /* extract the instruction at the pc */
224 pcinsn = extract_unsigned_integer (buf + 4, 4);
227 (insn_is_sigreturn (pcinsn)
228 && extract_unsigned_integer (buf + 8, 4) == INSTR_SC)
231 && insn_is_sigreturn (extract_unsigned_integer (buf, 4))));
235 ppc_linux_skip_trampoline_code (CORE_ADDR pc)
238 struct obj_section *sect;
239 struct objfile *objfile;
241 CORE_ADDR plt_start = 0;
242 CORE_ADDR symtab = 0;
243 CORE_ADDR strtab = 0;
245 int reloc_index = -1;
251 struct minimal_symbol *msymbol;
253 /* Find the section pc is in; return if not in .plt */
254 sect = find_pc_section (pc);
255 if (!sect || strcmp (sect->the_bfd_section->name, ".plt") != 0)
258 objfile = sect->objfile;
260 /* Pick up the instruction at pc. It had better be of the
264 where IDX is an index into the plt_table. */
266 if (target_read_memory (pc, buf, 4) != 0)
268 insn = extract_unsigned_integer (buf, 4);
270 if ((insn & 0xffff0000) != 0x39600000 /* li r11, VAL */ )
273 reloc_index = (insn << 16) >> 16;
275 /* Find the objfile that pc is in and obtain the information
276 necessary for finding the symbol name. */
277 for (sect = objfile->sections; sect < objfile->sections_end; ++sect)
279 const char *secname = sect->the_bfd_section->name;
280 if (strcmp (secname, ".plt") == 0)
281 plt_start = sect->addr;
282 else if (strcmp (secname, ".rela.plt") == 0)
283 num_slots = ((int) sect->endaddr - (int) sect->addr) / 12;
284 else if (strcmp (secname, ".dynsym") == 0)
286 else if (strcmp (secname, ".dynstr") == 0)
290 /* Make sure we have all the information we need. */
291 if (plt_start == 0 || num_slots == -1 || symtab == 0 || strtab == 0)
294 /* Compute the value of the plt table */
295 plt_table = plt_start + 72 + 8 * num_slots;
297 /* Get address of the relocation entry (Elf32_Rela) */
298 if (target_read_memory (plt_table + reloc_index, buf, 4) != 0)
300 reloc = extract_unsigned_integer (buf, 4);
302 sect = find_pc_section (reloc);
306 if (strcmp (sect->the_bfd_section->name, ".text") == 0)
309 /* Now get the r_info field which is the relocation type and symbol
311 if (target_read_memory (reloc + 4, buf, 4) != 0)
313 symidx = extract_unsigned_integer (buf, 4);
315 /* Shift out the relocation type leaving just the symbol index */
316 /* symidx = ELF32_R_SYM(symidx); */
317 symidx = symidx >> 8;
319 /* compute the address of the symbol */
320 sym = symtab + symidx * 4;
322 /* Fetch the string table index */
323 if (target_read_memory (sym, buf, 4) != 0)
325 symidx = extract_unsigned_integer (buf, 4);
327 /* Fetch the string; we don't know how long it is. Is it possible
328 that the following will fail because we're trying to fetch too
330 if (target_read_memory (strtab + symidx, symname, sizeof (symname)) != 0)
333 /* This might not work right if we have multiple symbols with the
334 same name; the only way to really get it right is to perform
335 the same sort of lookup as the dynamic linker. */
336 msymbol = lookup_minimal_symbol_text (symname, NULL);
340 return SYMBOL_VALUE_ADDRESS (msymbol);
343 /* The rs6000 version of FRAME_SAVED_PC will almost work for us. The
344 signal handler details are different, so we'll handle those here
345 and call the rs6000 version to do the rest. */
347 ppc_linux_frame_saved_pc (struct frame_info *fi)
349 if ((get_frame_type (fi) == SIGTRAMP_FRAME))
351 CORE_ADDR regs_addr =
352 read_memory_integer (get_frame_base (fi)
353 + PPC_LINUX_REGS_PTR_OFFSET, 4);
354 /* return the NIP in the regs array */
355 return read_memory_integer (regs_addr + 4 * PPC_LINUX_PT_NIP, 4);
357 else if (get_next_frame (fi)
358 && (get_frame_type (get_next_frame (fi)) == SIGTRAMP_FRAME))
360 CORE_ADDR regs_addr =
361 read_memory_integer (get_frame_base (get_next_frame (fi))
362 + PPC_LINUX_REGS_PTR_OFFSET, 4);
363 /* return LNK in the regs array */
364 return read_memory_integer (regs_addr + 4 * PPC_LINUX_PT_LNK, 4);
367 return rs6000_frame_saved_pc (fi);
371 ppc_linux_init_extra_frame_info (int fromleaf, struct frame_info *fi)
373 rs6000_init_extra_frame_info (fromleaf, fi);
375 if (get_next_frame (fi) != 0)
377 /* We're called from get_prev_frame_info; check to see if
378 this is a signal frame by looking to see if the pc points
379 at trampoline code */
380 if (ppc_linux_at_sigtramp_return_path (get_frame_pc (fi)))
381 deprecated_set_frame_type (fi, SIGTRAMP_FRAME);
383 /* FIXME: cagney/2002-11-10: Is this double bogus? What
384 happens if the frame has previously been marked as a dummy? */
385 deprecated_set_frame_type (fi, NORMAL_FRAME);
390 ppc_linux_frameless_function_invocation (struct frame_info *fi)
392 /* We'll find the wrong thing if we let
393 rs6000_frameless_function_invocation () search for a signal trampoline */
394 if (ppc_linux_at_sigtramp_return_path (get_frame_pc (fi)))
397 return rs6000_frameless_function_invocation (fi);
401 ppc_linux_frame_init_saved_regs (struct frame_info *fi)
403 if ((get_frame_type (fi) == SIGTRAMP_FRAME))
407 if (deprecated_get_frame_saved_regs (fi))
410 frame_saved_regs_zalloc (fi);
413 read_memory_integer (get_frame_base (fi)
414 + PPC_LINUX_REGS_PTR_OFFSET, 4);
415 deprecated_get_frame_saved_regs (fi)[PC_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_NIP;
416 deprecated_get_frame_saved_regs (fi)[gdbarch_tdep (current_gdbarch)->ppc_ps_regnum] =
417 regs_addr + 4 * PPC_LINUX_PT_MSR;
418 deprecated_get_frame_saved_regs (fi)[gdbarch_tdep (current_gdbarch)->ppc_cr_regnum] =
419 regs_addr + 4 * PPC_LINUX_PT_CCR;
420 deprecated_get_frame_saved_regs (fi)[gdbarch_tdep (current_gdbarch)->ppc_lr_regnum] =
421 regs_addr + 4 * PPC_LINUX_PT_LNK;
422 deprecated_get_frame_saved_regs (fi)[gdbarch_tdep (current_gdbarch)->ppc_ctr_regnum] =
423 regs_addr + 4 * PPC_LINUX_PT_CTR;
424 deprecated_get_frame_saved_regs (fi)[gdbarch_tdep (current_gdbarch)->ppc_xer_regnum] =
425 regs_addr + 4 * PPC_LINUX_PT_XER;
426 deprecated_get_frame_saved_regs (fi)[gdbarch_tdep (current_gdbarch)->ppc_mq_regnum] =
427 regs_addr + 4 * PPC_LINUX_PT_MQ;
428 for (i = 0; i < 32; i++)
429 deprecated_get_frame_saved_regs (fi)[gdbarch_tdep (current_gdbarch)->ppc_gp0_regnum + i] =
430 regs_addr + 4 * PPC_LINUX_PT_R0 + 4 * i;
431 for (i = 0; i < 32; i++)
432 deprecated_get_frame_saved_regs (fi)[FP0_REGNUM + i] = regs_addr + 4 * PPC_LINUX_PT_FPR0 + 8 * i;
435 rs6000_frame_init_saved_regs (fi);
439 ppc_linux_frame_chain (struct frame_info *thisframe)
441 /* Kernel properly constructs the frame chain for the handler */
442 if ((get_frame_type (thisframe) == SIGTRAMP_FRAME))
443 return read_memory_integer (get_frame_base (thisframe), 4);
445 return rs6000_frame_chain (thisframe);
448 /* ppc_linux_memory_remove_breakpoints attempts to remove a breakpoint
449 in much the same fashion as memory_remove_breakpoint in mem-break.c,
450 but is careful not to write back the previous contents if the code
451 in question has changed in between inserting the breakpoint and
454 Here is the problem that we're trying to solve...
456 Once upon a time, before introducing this function to remove
457 breakpoints from the inferior, setting a breakpoint on a shared
458 library function prior to running the program would not work
459 properly. In order to understand the problem, it is first
460 necessary to understand a little bit about dynamic linking on
463 A call to a shared library function is accomplished via a bl
464 (branch-and-link) instruction whose branch target is an entry
465 in the procedure linkage table (PLT). The PLT in the object
466 file is uninitialized. To gdb, prior to running the program, the
467 entries in the PLT are all zeros.
469 Once the program starts running, the shared libraries are loaded
470 and the procedure linkage table is initialized, but the entries in
471 the table are not (necessarily) resolved. Once a function is
472 actually called, the code in the PLT is hit and the function is
473 resolved. In order to better illustrate this, an example is in
474 order; the following example is from the gdb testsuite.
476 We start the program shmain.
478 [kev@arroyo testsuite]$ ../gdb gdb.base/shmain
481 We place two breakpoints, one on shr1 and the other on main.
484 Breakpoint 1 at 0x100409d4
486 Breakpoint 2 at 0x100006a0: file gdb.base/shmain.c, line 44.
488 Examine the instruction (and the immediatly following instruction)
489 upon which the breakpoint was placed. Note that the PLT entry
490 for shr1 contains zeros.
492 (gdb) x/2i 0x100409d4
493 0x100409d4 <shr1>: .long 0x0
494 0x100409d8 <shr1+4>: .long 0x0
499 Starting program: gdb.base/shmain
500 Breakpoint 1 at 0xffaf790: file gdb.base/shr1.c, line 19.
502 Breakpoint 2, main ()
503 at gdb.base/shmain.c:44
506 Examine the PLT again. Note that the loading of the shared
507 library has initialized the PLT to code which loads a constant
508 (which I think is an index into the GOT) into r11 and then
509 branchs a short distance to the code which actually does the
512 (gdb) x/2i 0x100409d4
513 0x100409d4 <shr1>: li r11,4
514 0x100409d8 <shr1+4>: b 0x10040984 <sg+4>
518 Breakpoint 1, shr1 (x=1)
519 at gdb.base/shr1.c:19
522 Now we've hit the breakpoint at shr1. (The breakpoint was
523 reset from the PLT entry to the actual shr1 function after the
524 shared library was loaded.) Note that the PLT entry has been
525 resolved to contain a branch that takes us directly to shr1.
526 (The real one, not the PLT entry.)
528 (gdb) x/2i 0x100409d4
529 0x100409d4 <shr1>: b 0xffaf76c <shr1>
530 0x100409d8 <shr1+4>: b 0x10040984 <sg+4>
532 The thing to note here is that the PLT entry for shr1 has been
535 Now the problem should be obvious. GDB places a breakpoint (a
536 trap instruction) on the zero value of the PLT entry for shr1.
537 Later on, after the shared library had been loaded and the PLT
538 initialized, GDB gets a signal indicating this fact and attempts
539 (as it always does when it stops) to remove all the breakpoints.
541 The breakpoint removal was causing the former contents (a zero
542 word) to be written back to the now initialized PLT entry thus
543 destroying a portion of the initialization that had occurred only a
544 short time ago. When execution continued, the zero word would be
545 executed as an instruction an an illegal instruction trap was
546 generated instead. (0 is not a legal instruction.)
548 The fix for this problem was fairly straightforward. The function
549 memory_remove_breakpoint from mem-break.c was copied to this file,
550 modified slightly, and renamed to ppc_linux_memory_remove_breakpoint.
551 In tm-linux.h, MEMORY_REMOVE_BREAKPOINT is defined to call this new
554 The differences between ppc_linux_memory_remove_breakpoint () and
555 memory_remove_breakpoint () are minor. All that the former does
556 that the latter does not is check to make sure that the breakpoint
557 location actually contains a breakpoint (trap instruction) prior
558 to attempting to write back the old contents. If it does contain
559 a trap instruction, we allow the old contents to be written back.
560 Otherwise, we silently do nothing.
562 The big question is whether memory_remove_breakpoint () should be
563 changed to have the same functionality. The downside is that more
564 traffic is generated for remote targets since we'll have an extra
565 fetch of a memory word each time a breakpoint is removed.
567 For the time being, we'll leave this self-modifying-code-friendly
568 version in ppc-linux-tdep.c, but it ought to be migrated somewhere
569 else in the event that some other platform has similar needs with
570 regard to removing breakpoints in some potentially self modifying
573 ppc_linux_memory_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
575 const unsigned char *bp;
578 char old_contents[BREAKPOINT_MAX];
580 /* Determine appropriate breakpoint contents and size for this address. */
581 bp = BREAKPOINT_FROM_PC (&addr, &bplen);
583 error ("Software breakpoints not implemented for this target.");
585 val = target_read_memory (addr, old_contents, bplen);
587 /* If our breakpoint is no longer at the address, this means that the
588 program modified the code on us, so it is wrong to put back the
590 if (val == 0 && memcmp (bp, old_contents, bplen) == 0)
591 val = target_write_memory (addr, contents_cache, bplen);
596 /* For historic reasons, PPC 32 GNU/Linux follows PowerOpen rather
597 than the 32 bit SYSV R4 ABI structure return convention - all
598 structures, no matter their size, are put in memory. Vectors,
599 which were added later, do get returned in a register though. */
601 static enum return_value_convention
602 ppc_linux_return_value (struct gdbarch *gdbarch, struct type *valtype,
603 struct regcache *regcache, void *readbuf,
604 const void *writebuf)
606 if ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
607 || TYPE_CODE (valtype) == TYPE_CODE_UNION)
608 && !((TYPE_LENGTH (valtype) == 16 || TYPE_LENGTH (valtype) == 8)
609 && TYPE_VECTOR (valtype)))
610 return RETURN_VALUE_STRUCT_CONVENTION;
612 return ppc_sysv_abi_return_value (gdbarch, valtype, regcache, readbuf,
616 /* Fetch (and possibly build) an appropriate link_map_offsets
617 structure for GNU/Linux PPC targets using the struct offsets
618 defined in link.h (but without actual reference to that file).
620 This makes it possible to access GNU/Linux PPC shared libraries
621 from a GDB that was not built on an GNU/Linux PPC host (for cross
624 struct link_map_offsets *
625 ppc_linux_svr4_fetch_link_map_offsets (void)
627 static struct link_map_offsets lmo;
628 static struct link_map_offsets *lmp = NULL;
634 lmo.r_debug_size = 8; /* The actual size is 20 bytes, but
635 this is all we need. */
636 lmo.r_map_offset = 4;
639 lmo.link_map_size = 20; /* The actual size is 560 bytes, but
640 this is all we need. */
641 lmo.l_addr_offset = 0;
644 lmo.l_name_offset = 4;
647 lmo.l_next_offset = 12;
650 lmo.l_prev_offset = 16;
658 /* Macros for matching instructions. Note that, since all the
659 operands are masked off before they're or-ed into the instruction,
660 you can use -1 to make masks. */
662 #define insn_d(opcd, rts, ra, d) \
663 ((((opcd) & 0x3f) << 26) \
664 | (((rts) & 0x1f) << 21) \
665 | (((ra) & 0x1f) << 16) \
668 #define insn_ds(opcd, rts, ra, d, xo) \
669 ((((opcd) & 0x3f) << 26) \
670 | (((rts) & 0x1f) << 21) \
671 | (((ra) & 0x1f) << 16) \
675 #define insn_xfx(opcd, rts, spr, xo) \
676 ((((opcd) & 0x3f) << 26) \
677 | (((rts) & 0x1f) << 21) \
678 | (((spr) & 0x1f) << 16) \
679 | (((spr) & 0x3e0) << 6) \
680 | (((xo) & 0x3ff) << 1))
682 /* Read a PPC instruction from memory. PPC instructions are always
683 big-endian, no matter what endianness the program is running in, so
684 we can't use read_memory_integer or one of its friends here. */
686 read_insn (CORE_ADDR pc)
688 unsigned char buf[4];
690 read_memory (pc, buf, 4);
691 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
695 /* An instruction to match. */
698 unsigned int mask; /* mask the insn with this... */
699 unsigned int data; /* ...and see if it matches this. */
700 int optional; /* If non-zero, this insn may be absent. */
703 /* Return non-zero if the instructions at PC match the series
704 described in PATTERN, or zero otherwise. PATTERN is an array of
705 'struct insn_pattern' objects, terminated by an entry whose mask is
708 When the match is successful, fill INSN[i] with what PATTERN[i]
709 matched. If PATTERN[i] is optional, and the instruction wasn't
710 present, set INSN[i] to 0 (which is not a valid PPC instruction).
711 INSN should have as many elements as PATTERN. Note that, if
712 PATTERN contains optional instructions which aren't present in
713 memory, then INSN will have holes, so INSN[i] isn't necessarily the
714 i'th instruction in memory. */
716 insns_match_pattern (CORE_ADDR pc,
717 struct insn_pattern *pattern,
722 for (i = 0; pattern[i].mask; i++)
724 insn[i] = read_insn (pc);
725 if ((insn[i] & pattern[i].mask) == pattern[i].data)
727 else if (pattern[i].optional)
737 /* Return the 'd' field of the d-form instruction INSN, properly
740 insn_d_field (unsigned int insn)
742 return ((((CORE_ADDR) insn & 0xffff) ^ 0x8000) - 0x8000);
746 /* Return the 'ds' field of the ds-form instruction INSN, with the two
747 zero bits concatenated at the right, and properly
750 insn_ds_field (unsigned int insn)
752 return ((((CORE_ADDR) insn & 0xfffc) ^ 0x8000) - 0x8000);
756 /* If DESC is the address of a 64-bit PowerPC GNU/Linux function
757 descriptor, return the descriptor's entry point. */
759 ppc64_desc_entry_point (CORE_ADDR desc)
761 /* The first word of the descriptor is the entry point. */
762 return (CORE_ADDR) read_memory_unsigned_integer (desc, 8);
766 /* Pattern for the standard linkage function. These are built by
767 build_plt_stub in elf64-ppc.c, whose GLINK argument is always
769 static struct insn_pattern ppc64_standard_linkage[] =
771 /* addis r12, r2, <any> */
772 { insn_d (-1, -1, -1, 0), insn_d (15, 12, 2, 0), 0 },
775 { -1, insn_ds (62, 2, 1, 40, 0), 0 },
777 /* ld r11, <any>(r12) */
778 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 },
780 /* addis r12, r12, 1 <optional> */
781 { insn_d (-1, -1, -1, -1), insn_d (15, 12, 2, 1), 1 },
783 /* ld r2, <any>(r12) */
784 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 2, 12, 0, 0), 0 },
786 /* addis r12, r12, 1 <optional> */
787 { insn_d (-1, -1, -1, -1), insn_d (15, 12, 2, 1), 1 },
790 { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 11, 9, 467),
793 /* ld r11, <any>(r12) */
794 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 },
797 { -1, 0x4e800420, 0 },
801 #define PPC64_STANDARD_LINKAGE_LEN \
802 (sizeof (ppc64_standard_linkage) / sizeof (ppc64_standard_linkage[0]))
805 /* Recognize a 64-bit PowerPC GNU/Linux linkage function --- what GDB
806 calls a "solib trampoline". */
808 ppc64_in_solib_call_trampoline (CORE_ADDR pc, char *name)
810 /* Detecting solib call trampolines on PPC64 GNU/Linux is a pain.
812 It's not specifically solib call trampolines that are the issue.
813 Any call from one function to another function that uses a
814 different TOC requires a trampoline, to save the caller's TOC
815 pointer and then load the callee's TOC. An executable or shared
816 library may have more than one TOC, so even intra-object calls
817 may require a trampoline. Since executable and shared libraries
818 will all have their own distinct TOCs, every inter-object call is
819 also an inter-TOC call, and requires a trampoline --- so "solib
820 call trampolines" are just a special case.
822 The 64-bit PowerPC GNU/Linux ABI calls these call trampolines
823 "linkage functions". Since they need to be near the functions
824 that call them, they all appear in .text, not in any special
825 section. The .plt section just contains an array of function
826 descriptors, from which the linkage functions load the callee's
827 entry point, TOC value, and environment pointer. So
828 in_plt_section is useless. The linkage functions don't have any
829 special linker symbols to name them, either.
831 The only way I can see to recognize them is to actually look at
832 their code. They're generated by ppc_build_one_stub and some
833 other functions in bfd/elf64-ppc.c, so that should show us all
834 the instruction sequences we need to recognize. */
835 unsigned int insn[PPC64_STANDARD_LINKAGE_LEN];
837 return insns_match_pattern (pc, ppc64_standard_linkage, insn);
841 /* When the dynamic linker is doing lazy symbol resolution, the first
842 call to a function in another object will go like this:
844 - The user's function calls the linkage function:
846 100007c4: 4b ff fc d5 bl 10000498
847 100007c8: e8 41 00 28 ld r2,40(r1)
849 - The linkage function loads the entry point (and other stuff) from
850 the function descriptor in the PLT, and jumps to it:
852 10000498: 3d 82 00 00 addis r12,r2,0
853 1000049c: f8 41 00 28 std r2,40(r1)
854 100004a0: e9 6c 80 98 ld r11,-32616(r12)
855 100004a4: e8 4c 80 a0 ld r2,-32608(r12)
856 100004a8: 7d 69 03 a6 mtctr r11
857 100004ac: e9 6c 80 a8 ld r11,-32600(r12)
858 100004b0: 4e 80 04 20 bctr
860 - But since this is the first time that PLT entry has been used, it
861 sends control to its glink entry. That loads the number of the
862 PLT entry and jumps to the common glink0 code:
864 10000c98: 38 00 00 00 li r0,0
865 10000c9c: 4b ff ff dc b 10000c78
867 - The common glink0 code then transfers control to the dynamic
870 10000c78: e8 41 00 28 ld r2,40(r1)
871 10000c7c: 3d 82 00 00 addis r12,r2,0
872 10000c80: e9 6c 80 80 ld r11,-32640(r12)
873 10000c84: e8 4c 80 88 ld r2,-32632(r12)
874 10000c88: 7d 69 03 a6 mtctr r11
875 10000c8c: e9 6c 80 90 ld r11,-32624(r12)
876 10000c90: 4e 80 04 20 bctr
878 Eventually, this code will figure out how to skip all of this,
879 including the dynamic linker. At the moment, we just get through
880 the linkage function. */
882 /* If the current thread is about to execute a series of instructions
883 at PC matching the ppc64_standard_linkage pattern, and INSN is the result
884 from that pattern match, return the code address to which the
885 standard linkage function will send them. (This doesn't deal with
886 dynamic linker lazy symbol resolution stubs.) */
888 ppc64_standard_linkage_target (CORE_ADDR pc, unsigned int *insn)
890 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
892 /* The address of the function descriptor this linkage function
895 = ((CORE_ADDR) read_register (tdep->ppc_gp0_regnum + 2)
896 + (insn_d_field (insn[0]) << 16)
897 + insn_ds_field (insn[2]));
899 /* The first word of the descriptor is the entry point. Return that. */
900 return ppc64_desc_entry_point (desc);
904 /* Given that we've begun executing a call trampoline at PC, return
905 the entry point of the function the trampoline will go to. */
907 ppc64_skip_trampoline_code (CORE_ADDR pc)
909 unsigned int ppc64_standard_linkage_insn[PPC64_STANDARD_LINKAGE_LEN];
911 if (insns_match_pattern (pc, ppc64_standard_linkage,
912 ppc64_standard_linkage_insn))
913 return ppc64_standard_linkage_target (pc, ppc64_standard_linkage_insn);
919 /* Support for CONVERT_FROM_FUNC_PTR_ADDR (ARCH, ADDR, TARG) on PPC64
922 Usually a function pointer's representation is simply the address
923 of the function. On GNU/Linux on the 64-bit PowerPC however, a
924 function pointer is represented by a pointer to a TOC entry. This
925 TOC entry contains three words, the first word is the address of
926 the function, the second word is the TOC pointer (r2), and the
927 third word is the static chain value. Throughout GDB it is
928 currently assumed that a function pointer contains the address of
929 the function, which is not easy to fix. In addition, the
930 conversion of a function address to a function pointer would
931 require allocation of a TOC entry in the inferior's memory space,
932 with all its drawbacks. To be able to call C++ virtual methods in
933 the inferior (which are called via function pointers),
934 find_function_addr uses this function to get the function address
935 from a function pointer. */
937 /* If ADDR points at what is clearly a function descriptor, transform
938 it into the address of the corresponding function. Be
939 conservative, otherwize GDB will do the transformation on any
940 random addresses such as occures when there is no symbol table. */
943 ppc64_linux_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
945 struct target_ops *targ)
947 struct section_table *s = target_section_by_addr (targ, addr);
949 /* Check if ADDR points to a function descriptor. */
950 if (s && strcmp (s->the_bfd_section->name, ".opd") == 0)
951 return get_target_memory_unsigned (targ, addr, 8);
964 ELF_FPREGSET_SIZE = (ELF_NFPREG * 8)
968 right_supply_register (struct regcache *regcache, int wordsize, int regnum,
971 regcache_raw_supply (regcache, regnum,
973 - register_size (current_gdbarch, regnum)));
976 /* Extract the register values found in the WORDSIZED ABI GREGSET,
977 storing their values in REGCACHE. Note that some are left-aligned,
978 while others are right aligned. */
981 ppc_linux_supply_gregset (struct regcache *regcache,
982 int regnum, const void *gregs, size_t size,
986 struct gdbarch *regcache_arch = get_regcache_arch (regcache);
987 struct gdbarch_tdep *regcache_tdep = gdbarch_tdep (regcache_arch);
988 const bfd_byte *buf = gregs;
990 for (regi = 0; regi < 32; regi++)
991 right_supply_register (regcache, wordsize, regi, buf + wordsize * regi);
993 right_supply_register (regcache, wordsize, gdbarch_pc_regnum (regcache_arch),
994 buf + wordsize * PPC_LINUX_PT_NIP);
995 right_supply_register (regcache, wordsize, regcache_tdep->ppc_lr_regnum,
996 buf + wordsize * PPC_LINUX_PT_LNK);
997 regcache_raw_supply (regcache, regcache_tdep->ppc_cr_regnum,
998 buf + wordsize * PPC_LINUX_PT_CCR);
999 regcache_raw_supply (regcache, regcache_tdep->ppc_xer_regnum,
1000 buf + wordsize * PPC_LINUX_PT_XER);
1001 regcache_raw_supply (regcache, regcache_tdep->ppc_ctr_regnum,
1002 buf + wordsize * PPC_LINUX_PT_CTR);
1003 if (regcache_tdep->ppc_mq_regnum != -1)
1004 right_supply_register (regcache, wordsize, regcache_tdep->ppc_mq_regnum,
1005 buf + wordsize * PPC_LINUX_PT_MQ);
1006 right_supply_register (regcache, wordsize, regcache_tdep->ppc_ps_regnum,
1007 buf + wordsize * PPC_LINUX_PT_MSR);
1011 ppc32_linux_supply_gregset (const struct regset *regset,
1012 struct regcache *regcache,
1013 int regnum, const void *gregs, size_t size)
1015 ppc_linux_supply_gregset (regcache, regnum, gregs, size, 4);
1018 static struct regset ppc32_linux_gregset = {
1019 NULL, ppc32_linux_supply_gregset
1022 struct ppc_linux_sigtramp_cache
1025 struct trad_frame_saved_reg *saved_regs;
1028 static struct ppc_linux_sigtramp_cache *
1029 ppc_linux_sigtramp_cache (struct frame_info *next_frame, void **this_cache)
1035 struct ppc_linux_sigtramp_cache *cache;
1036 struct gdbarch *gdbarch = get_frame_arch (next_frame);
1037 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1039 if ((*this_cache) != NULL)
1040 return (*this_cache);
1041 cache = FRAME_OBSTACK_ZALLOC (struct ppc_linux_sigtramp_cache);
1042 (*this_cache) = cache;
1043 cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
1045 cache->base = frame_unwind_register_unsigned (next_frame, SP_REGNUM);
1047 /* Find the register pointer, which gives the address of the
1048 register buffers. */
1049 if (tdep->wordsize == 4)
1051 + 0xd0 /* Offset to ucontext_t. */
1052 + 0x30 /* Offset to .reg. */);
1055 + 0x80 /* Offset to ucontext_t. */
1056 + 0xe0 /* Offset to .reg. */);
1057 /* And the corresponding register buffers. */
1058 gpregs = read_memory_unsigned_integer (regs, tdep->wordsize);
1059 fpregs = gpregs + 48 * tdep->wordsize;
1061 /* General purpose. */
1062 for (i = 0; i < 32; i++)
1064 int regnum = i + tdep->ppc_gp0_regnum;
1065 cache->saved_regs[regnum].addr = gpregs + i * tdep->wordsize;
1067 cache->saved_regs[PC_REGNUM].addr = gpregs + 32 * tdep->wordsize;
1068 cache->saved_regs[tdep->ppc_ctr_regnum].addr = gpregs + 35 * tdep->wordsize;
1069 cache->saved_regs[tdep->ppc_lr_regnum].addr = gpregs + 36 * tdep->wordsize;
1070 cache->saved_regs[tdep->ppc_xer_regnum].addr = gpregs + 37 * tdep->wordsize;
1071 cache->saved_regs[tdep->ppc_cr_regnum].addr = gpregs + 38 * tdep->wordsize;
1073 /* Floating point registers. */
1074 for (i = 0; i < 32; i++)
1076 int regnum = i + FP0_REGNUM;
1077 cache->saved_regs[regnum].addr = fpregs + i * tdep->wordsize;
1079 cache->saved_regs[tdep->ppc_fpscr_regnum].addr = fpregs + 32 * tdep->wordsize;
1085 ppc_linux_sigtramp_this_id (struct frame_info *next_frame, void **this_cache,
1086 struct frame_id *this_id)
1088 struct ppc_linux_sigtramp_cache *info
1089 = ppc_linux_sigtramp_cache (next_frame, this_cache);
1090 (*this_id) = frame_id_build (info->base, frame_pc_unwind (next_frame));
1094 ppc_linux_sigtramp_prev_register (struct frame_info *next_frame,
1096 int regnum, int *optimizedp,
1097 enum lval_type *lvalp, CORE_ADDR *addrp,
1098 int *realnump, void *valuep)
1100 struct ppc_linux_sigtramp_cache *info
1101 = ppc_linux_sigtramp_cache (next_frame, this_cache);
1102 trad_frame_prev_register (next_frame, info->saved_regs, regnum,
1103 optimizedp, lvalp, addrp, realnump, valuep);
1106 static const struct frame_unwind ppc_linux_sigtramp_unwind =
1109 ppc_linux_sigtramp_this_id,
1110 ppc_linux_sigtramp_prev_register
1113 static const struct frame_unwind *
1114 ppc_linux_sigtramp_sniffer (struct frame_info *next_frame)
1116 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (next_frame));
1117 if (frame_pc_unwind (next_frame)
1118 > frame_unwind_register_unsigned (next_frame, SP_REGNUM))
1119 /* Assume anything that is vaguely on the stack is a signal
1121 return &ppc_linux_sigtramp_unwind;
1127 ppc64_linux_supply_gregset (const struct regset *regset,
1128 struct regcache * regcache,
1129 int regnum, const void *gregs, size_t size)
1131 ppc_linux_supply_gregset (regcache, regnum, gregs, size, 8);
1134 static struct regset ppc64_linux_gregset = {
1135 NULL, ppc64_linux_supply_gregset
1139 ppc_linux_supply_fpregset (const struct regset *regset,
1140 struct regcache * regcache,
1141 int regnum, const void *fpset, size_t size)
1144 struct gdbarch *regcache_arch = get_regcache_arch (regcache);
1145 struct gdbarch_tdep *regcache_tdep = gdbarch_tdep (regcache_arch);
1146 const bfd_byte *buf = fpset;
1148 for (regi = 0; regi < 32; regi++)
1149 regcache_raw_supply (regcache, FP0_REGNUM + regi, buf + 8 * regi);
1151 /* The FPSCR is stored in the low order word of the last doubleword in the
1153 regcache_raw_supply (regcache, regcache_tdep->ppc_fpscr_regnum,
1157 static struct regset ppc_linux_fpregset = { NULL, ppc_linux_supply_fpregset };
1159 static const struct regset *
1160 ppc_linux_regset_from_core_section (struct gdbarch *core_arch,
1161 const char *sect_name, size_t sect_size)
1163 struct gdbarch_tdep *tdep = gdbarch_tdep (core_arch);
1164 if (strcmp (sect_name, ".reg") == 0)
1166 if (tdep->wordsize == 4)
1167 return &ppc32_linux_gregset;
1169 return &ppc64_linux_gregset;
1171 if (strcmp (sect_name, ".reg2") == 0)
1172 return &ppc_linux_fpregset;
1177 ppc_linux_init_abi (struct gdbarch_info info,
1178 struct gdbarch *gdbarch)
1180 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1182 if (tdep->wordsize == 4)
1184 /* Until November 2001, gcc did not comply with the 32 bit SysV
1185 R4 ABI requirement that structures less than or equal to 8
1186 bytes should be returned in registers. Instead GCC was using
1187 the the AIX/PowerOpen ABI - everything returned in memory
1188 (well ignoring vectors that is). When this was corrected, it
1189 wasn't fixed for GNU/Linux native platform. Use the
1190 PowerOpen struct convention. */
1191 set_gdbarch_return_value (gdbarch, ppc_linux_return_value);
1194 /* Note: kevinb/2002-04-12: See note in rs6000_gdbarch_init regarding
1195 *_push_arguments(). The same remarks hold for the methods below. */
1196 set_gdbarch_deprecated_frameless_function_invocation (gdbarch, ppc_linux_frameless_function_invocation);
1197 set_gdbarch_deprecated_frame_chain (gdbarch, ppc_linux_frame_chain);
1198 set_gdbarch_deprecated_frame_saved_pc (gdbarch, ppc_linux_frame_saved_pc);
1200 set_gdbarch_deprecated_frame_init_saved_regs (gdbarch,
1201 ppc_linux_frame_init_saved_regs);
1202 set_gdbarch_deprecated_init_extra_frame_info (gdbarch,
1203 ppc_linux_init_extra_frame_info);
1206 set_gdbarch_memory_remove_breakpoint (gdbarch,
1207 ppc_linux_memory_remove_breakpoint);
1209 /* Shared library handling. */
1210 set_gdbarch_in_solib_call_trampoline (gdbarch, in_plt_section);
1211 set_gdbarch_skip_trampoline_code (gdbarch,
1212 ppc_linux_skip_trampoline_code);
1213 set_solib_svr4_fetch_link_map_offsets
1214 (gdbarch, ppc_linux_svr4_fetch_link_map_offsets);
1217 if (tdep->wordsize == 8)
1219 /* Handle PPC64 GNU/Linux function pointers (which are really
1220 function descriptors). */
1221 set_gdbarch_convert_from_func_ptr_addr
1222 (gdbarch, ppc64_linux_convert_from_func_ptr_addr);
1224 set_gdbarch_in_solib_call_trampoline
1225 (gdbarch, ppc64_in_solib_call_trampoline);
1226 set_gdbarch_skip_trampoline_code (gdbarch, ppc64_skip_trampoline_code);
1228 /* PPC64 malloc's entry-point is called ".malloc". */
1229 set_gdbarch_name_of_malloc (gdbarch, ".malloc");
1231 set_gdbarch_regset_from_core_section (gdbarch, ppc_linux_regset_from_core_section);
1232 frame_unwind_append_sniffer (gdbarch, ppc_linux_sigtramp_sniffer);
1236 _initialize_ppc_linux_tdep (void)
1238 /* Register for all sub-familes of the POWER/PowerPC: 32-bit and
1239 64-bit PowerPC, and the older rs6k. */
1240 gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc, GDB_OSABI_LINUX,
1241 ppc_linux_init_abi);
1242 gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc64, GDB_OSABI_LINUX,
1243 ppc_linux_init_abi);
1244 gdbarch_register_osabi (bfd_arch_rs6000, bfd_mach_rs6k, GDB_OSABI_LINUX,
1245 ppc_linux_init_abi);