]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/ppc-linux-tdep.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / ppc-linux-tdep.c
1 /* Target-dependent code for GDB, the GNU debugger.
2
3 Copyright (C) 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
5 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
23
24 #include "defs.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "symtab.h"
28 #include "target.h"
29 #include "gdbcore.h"
30 #include "gdbcmd.h"
31 #include "symfile.h"
32 #include "objfiles.h"
33 #include "regcache.h"
34 #include "value.h"
35 #include "osabi.h"
36 #include "regset.h"
37 #include "solib-svr4.h"
38 #include "ppc-tdep.h"
39 #include "trad-frame.h"
40 #include "frame-unwind.h"
41 #include "tramp-frame.h"
42
43 /* The following instructions are used in the signal trampoline code
44 on GNU/Linux PPC. The kernel used to use magic syscalls 0x6666 and
45 0x7777 but now uses the sigreturn syscalls. We check for both. */
46 #define INSTR_LI_R0_0x6666 0x38006666
47 #define INSTR_LI_R0_0x7777 0x38007777
48 #define INSTR_LI_R0_NR_sigreturn 0x38000077
49 #define INSTR_LI_R0_NR_rt_sigreturn 0x380000AC
50
51 #define INSTR_SC 0x44000002
52
53 /* Since the *-tdep.c files are platform independent (i.e, they may be
54 used to build cross platform debuggers), we can't include system
55 headers. Therefore, details concerning the sigcontext structure
56 must be painstakingly rerecorded. What's worse, if these details
57 ever change in the header files, they'll have to be changed here
58 as well. */
59
60 /* __SIGNAL_FRAMESIZE from <asm/ptrace.h> */
61 #define PPC_LINUX_SIGNAL_FRAMESIZE 64
62
63 /* From <asm/sigcontext.h>, offsetof(struct sigcontext_struct, regs) == 0x1c */
64 #define PPC_LINUX_REGS_PTR_OFFSET (PPC_LINUX_SIGNAL_FRAMESIZE + 0x1c)
65
66 /* From <asm/sigcontext.h>,
67 offsetof(struct sigcontext_struct, handler) == 0x14 */
68 #define PPC_LINUX_HANDLER_PTR_OFFSET (PPC_LINUX_SIGNAL_FRAMESIZE + 0x14)
69
70 /* From <asm/ptrace.h>, values for PT_NIP, PT_R1, and PT_LNK */
71 #define PPC_LINUX_PT_R0 0
72 #define PPC_LINUX_PT_R1 1
73 #define PPC_LINUX_PT_R2 2
74 #define PPC_LINUX_PT_R3 3
75 #define PPC_LINUX_PT_R4 4
76 #define PPC_LINUX_PT_R5 5
77 #define PPC_LINUX_PT_R6 6
78 #define PPC_LINUX_PT_R7 7
79 #define PPC_LINUX_PT_R8 8
80 #define PPC_LINUX_PT_R9 9
81 #define PPC_LINUX_PT_R10 10
82 #define PPC_LINUX_PT_R11 11
83 #define PPC_LINUX_PT_R12 12
84 #define PPC_LINUX_PT_R13 13
85 #define PPC_LINUX_PT_R14 14
86 #define PPC_LINUX_PT_R15 15
87 #define PPC_LINUX_PT_R16 16
88 #define PPC_LINUX_PT_R17 17
89 #define PPC_LINUX_PT_R18 18
90 #define PPC_LINUX_PT_R19 19
91 #define PPC_LINUX_PT_R20 20
92 #define PPC_LINUX_PT_R21 21
93 #define PPC_LINUX_PT_R22 22
94 #define PPC_LINUX_PT_R23 23
95 #define PPC_LINUX_PT_R24 24
96 #define PPC_LINUX_PT_R25 25
97 #define PPC_LINUX_PT_R26 26
98 #define PPC_LINUX_PT_R27 27
99 #define PPC_LINUX_PT_R28 28
100 #define PPC_LINUX_PT_R29 29
101 #define PPC_LINUX_PT_R30 30
102 #define PPC_LINUX_PT_R31 31
103 #define PPC_LINUX_PT_NIP 32
104 #define PPC_LINUX_PT_MSR 33
105 #define PPC_LINUX_PT_CTR 35
106 #define PPC_LINUX_PT_LNK 36
107 #define PPC_LINUX_PT_XER 37
108 #define PPC_LINUX_PT_CCR 38
109 #define PPC_LINUX_PT_MQ 39
110 #define PPC_LINUX_PT_FPR0 48 /* each FP reg occupies 2 slots in this space */
111 #define PPC_LINUX_PT_FPR31 (PPC_LINUX_PT_FPR0 + 2*31)
112 #define PPC_LINUX_PT_FPSCR (PPC_LINUX_PT_FPR0 + 2*32 + 1)
113
114 static int ppc_linux_at_sigtramp_return_path (CORE_ADDR pc);
115
116 /* Determine if pc is in a signal trampoline...
117
118 Ha! That's not what this does at all. wait_for_inferior in
119 infrun.c calls get_frame_type() in order to detect entry into a
120 signal trampoline just after delivery of a signal. But on
121 GNU/Linux, signal trampolines are used for the return path only.
122 The kernel sets things up so that the signal handler is called
123 directly.
124
125 If we use in_sigtramp2() in place of in_sigtramp() (see below)
126 we'll (often) end up with stop_pc in the trampoline and prev_pc in
127 the (now exited) handler. The code there will cause a temporary
128 breakpoint to be set on prev_pc which is not very likely to get hit
129 again.
130
131 If this is confusing, think of it this way... the code in
132 wait_for_inferior() needs to be able to detect entry into a signal
133 trampoline just after a signal is delivered, not after the handler
134 has been run.
135
136 So, we define in_sigtramp() below to return 1 if the following is
137 true:
138
139 1) The previous frame is a real signal trampoline.
140
141 - and -
142
143 2) pc is at the first or second instruction of the corresponding
144 handler.
145
146 Why the second instruction? It seems that wait_for_inferior()
147 never sees the first instruction when single stepping. When a
148 signal is delivered while stepping, the next instruction that
149 would've been stepped over isn't, instead a signal is delivered and
150 the first instruction of the handler is stepped over instead. That
151 puts us on the second instruction. (I added the test for the first
152 instruction long after the fact, just in case the observed behavior
153 is ever fixed.) */
154
155 int
156 ppc_linux_in_sigtramp (CORE_ADDR pc, char *func_name)
157 {
158 CORE_ADDR lr;
159 CORE_ADDR sp;
160 CORE_ADDR tramp_sp;
161 gdb_byte buf[4];
162 CORE_ADDR handler;
163
164 lr = read_register (gdbarch_tdep (current_gdbarch)->ppc_lr_regnum);
165 if (!ppc_linux_at_sigtramp_return_path (lr))
166 return 0;
167
168 sp = read_register (SP_REGNUM);
169
170 if (target_read_memory (sp, buf, sizeof (buf)) != 0)
171 return 0;
172
173 tramp_sp = extract_unsigned_integer (buf, 4);
174
175 if (target_read_memory (tramp_sp + PPC_LINUX_HANDLER_PTR_OFFSET, buf,
176 sizeof (buf)) != 0)
177 return 0;
178
179 handler = extract_unsigned_integer (buf, 4);
180
181 return (pc == handler || pc == handler + 4);
182 }
183
184 static int
185 insn_is_sigreturn (unsigned long pcinsn)
186 {
187 switch(pcinsn)
188 {
189 case INSTR_LI_R0_0x6666:
190 case INSTR_LI_R0_0x7777:
191 case INSTR_LI_R0_NR_sigreturn:
192 case INSTR_LI_R0_NR_rt_sigreturn:
193 return 1;
194 default:
195 return 0;
196 }
197 }
198
199 /*
200 * The signal handler trampoline is on the stack and consists of exactly
201 * two instructions. The easiest and most accurate way of determining
202 * whether the pc is in one of these trampolines is by inspecting the
203 * instructions. It'd be faster though if we could find a way to do this
204 * via some simple address comparisons.
205 */
206 static int
207 ppc_linux_at_sigtramp_return_path (CORE_ADDR pc)
208 {
209 gdb_byte buf[12];
210 unsigned long pcinsn;
211 if (target_read_memory (pc - 4, buf, sizeof (buf)) != 0)
212 return 0;
213
214 /* extract the instruction at the pc */
215 pcinsn = extract_unsigned_integer (buf + 4, 4);
216
217 return (
218 (insn_is_sigreturn (pcinsn)
219 && extract_unsigned_integer (buf + 8, 4) == INSTR_SC)
220 ||
221 (pcinsn == INSTR_SC
222 && insn_is_sigreturn (extract_unsigned_integer (buf, 4))));
223 }
224
225 static CORE_ADDR
226 ppc_linux_skip_trampoline_code (CORE_ADDR pc)
227 {
228 gdb_byte buf[4];
229 struct obj_section *sect;
230 struct objfile *objfile;
231 unsigned long insn;
232 CORE_ADDR plt_start = 0;
233 CORE_ADDR symtab = 0;
234 CORE_ADDR strtab = 0;
235 int num_slots = -1;
236 int reloc_index = -1;
237 CORE_ADDR plt_table;
238 CORE_ADDR reloc;
239 CORE_ADDR sym;
240 long symidx;
241 char symname[1024];
242 struct minimal_symbol *msymbol;
243
244 /* Find the section pc is in; return if not in .plt */
245 sect = find_pc_section (pc);
246 if (!sect || strcmp (sect->the_bfd_section->name, ".plt") != 0)
247 return 0;
248
249 objfile = sect->objfile;
250
251 /* Pick up the instruction at pc. It had better be of the
252 form
253 li r11, IDX
254
255 where IDX is an index into the plt_table. */
256
257 if (target_read_memory (pc, buf, 4) != 0)
258 return 0;
259 insn = extract_unsigned_integer (buf, 4);
260
261 if ((insn & 0xffff0000) != 0x39600000 /* li r11, VAL */ )
262 return 0;
263
264 reloc_index = (insn << 16) >> 16;
265
266 /* Find the objfile that pc is in and obtain the information
267 necessary for finding the symbol name. */
268 for (sect = objfile->sections; sect < objfile->sections_end; ++sect)
269 {
270 const char *secname = sect->the_bfd_section->name;
271 if (strcmp (secname, ".plt") == 0)
272 plt_start = sect->addr;
273 else if (strcmp (secname, ".rela.plt") == 0)
274 num_slots = ((int) sect->endaddr - (int) sect->addr) / 12;
275 else if (strcmp (secname, ".dynsym") == 0)
276 symtab = sect->addr;
277 else if (strcmp (secname, ".dynstr") == 0)
278 strtab = sect->addr;
279 }
280
281 /* Make sure we have all the information we need. */
282 if (plt_start == 0 || num_slots == -1 || symtab == 0 || strtab == 0)
283 return 0;
284
285 /* Compute the value of the plt table */
286 plt_table = plt_start + 72 + 8 * num_slots;
287
288 /* Get address of the relocation entry (Elf32_Rela) */
289 if (target_read_memory (plt_table + reloc_index, buf, 4) != 0)
290 return 0;
291 reloc = extract_unsigned_integer (buf, 4);
292
293 sect = find_pc_section (reloc);
294 if (!sect)
295 return 0;
296
297 if (strcmp (sect->the_bfd_section->name, ".text") == 0)
298 return reloc;
299
300 /* Now get the r_info field which is the relocation type and symbol
301 index. */
302 if (target_read_memory (reloc + 4, buf, 4) != 0)
303 return 0;
304 symidx = extract_unsigned_integer (buf, 4);
305
306 /* Shift out the relocation type leaving just the symbol index */
307 /* symidx = ELF32_R_SYM(symidx); */
308 symidx = symidx >> 8;
309
310 /* compute the address of the symbol */
311 sym = symtab + symidx * 4;
312
313 /* Fetch the string table index */
314 if (target_read_memory (sym, buf, 4) != 0)
315 return 0;
316 symidx = extract_unsigned_integer (buf, 4);
317
318 /* Fetch the string; we don't know how long it is. Is it possible
319 that the following will fail because we're trying to fetch too
320 much? */
321 if (target_read_memory (strtab + symidx, (gdb_byte *) symname,
322 sizeof (symname)) != 0)
323 return 0;
324
325 /* This might not work right if we have multiple symbols with the
326 same name; the only way to really get it right is to perform
327 the same sort of lookup as the dynamic linker. */
328 msymbol = lookup_minimal_symbol_text (symname, NULL);
329 if (!msymbol)
330 return 0;
331
332 return SYMBOL_VALUE_ADDRESS (msymbol);
333 }
334
335 /* ppc_linux_memory_remove_breakpoints attempts to remove a breakpoint
336 in much the same fashion as memory_remove_breakpoint in mem-break.c,
337 but is careful not to write back the previous contents if the code
338 in question has changed in between inserting the breakpoint and
339 removing it.
340
341 Here is the problem that we're trying to solve...
342
343 Once upon a time, before introducing this function to remove
344 breakpoints from the inferior, setting a breakpoint on a shared
345 library function prior to running the program would not work
346 properly. In order to understand the problem, it is first
347 necessary to understand a little bit about dynamic linking on
348 this platform.
349
350 A call to a shared library function is accomplished via a bl
351 (branch-and-link) instruction whose branch target is an entry
352 in the procedure linkage table (PLT). The PLT in the object
353 file is uninitialized. To gdb, prior to running the program, the
354 entries in the PLT are all zeros.
355
356 Once the program starts running, the shared libraries are loaded
357 and the procedure linkage table is initialized, but the entries in
358 the table are not (necessarily) resolved. Once a function is
359 actually called, the code in the PLT is hit and the function is
360 resolved. In order to better illustrate this, an example is in
361 order; the following example is from the gdb testsuite.
362
363 We start the program shmain.
364
365 [kev@arroyo testsuite]$ ../gdb gdb.base/shmain
366 [...]
367
368 We place two breakpoints, one on shr1 and the other on main.
369
370 (gdb) b shr1
371 Breakpoint 1 at 0x100409d4
372 (gdb) b main
373 Breakpoint 2 at 0x100006a0: file gdb.base/shmain.c, line 44.
374
375 Examine the instruction (and the immediatly following instruction)
376 upon which the breakpoint was placed. Note that the PLT entry
377 for shr1 contains zeros.
378
379 (gdb) x/2i 0x100409d4
380 0x100409d4 <shr1>: .long 0x0
381 0x100409d8 <shr1+4>: .long 0x0
382
383 Now run 'til main.
384
385 (gdb) r
386 Starting program: gdb.base/shmain
387 Breakpoint 1 at 0xffaf790: file gdb.base/shr1.c, line 19.
388
389 Breakpoint 2, main ()
390 at gdb.base/shmain.c:44
391 44 g = 1;
392
393 Examine the PLT again. Note that the loading of the shared
394 library has initialized the PLT to code which loads a constant
395 (which I think is an index into the GOT) into r11 and then
396 branchs a short distance to the code which actually does the
397 resolving.
398
399 (gdb) x/2i 0x100409d4
400 0x100409d4 <shr1>: li r11,4
401 0x100409d8 <shr1+4>: b 0x10040984 <sg+4>
402 (gdb) c
403 Continuing.
404
405 Breakpoint 1, shr1 (x=1)
406 at gdb.base/shr1.c:19
407 19 l = 1;
408
409 Now we've hit the breakpoint at shr1. (The breakpoint was
410 reset from the PLT entry to the actual shr1 function after the
411 shared library was loaded.) Note that the PLT entry has been
412 resolved to contain a branch that takes us directly to shr1.
413 (The real one, not the PLT entry.)
414
415 (gdb) x/2i 0x100409d4
416 0x100409d4 <shr1>: b 0xffaf76c <shr1>
417 0x100409d8 <shr1+4>: b 0x10040984 <sg+4>
418
419 The thing to note here is that the PLT entry for shr1 has been
420 changed twice.
421
422 Now the problem should be obvious. GDB places a breakpoint (a
423 trap instruction) on the zero value of the PLT entry for shr1.
424 Later on, after the shared library had been loaded and the PLT
425 initialized, GDB gets a signal indicating this fact and attempts
426 (as it always does when it stops) to remove all the breakpoints.
427
428 The breakpoint removal was causing the former contents (a zero
429 word) to be written back to the now initialized PLT entry thus
430 destroying a portion of the initialization that had occurred only a
431 short time ago. When execution continued, the zero word would be
432 executed as an instruction an an illegal instruction trap was
433 generated instead. (0 is not a legal instruction.)
434
435 The fix for this problem was fairly straightforward. The function
436 memory_remove_breakpoint from mem-break.c was copied to this file,
437 modified slightly, and renamed to ppc_linux_memory_remove_breakpoint.
438 In tm-linux.h, MEMORY_REMOVE_BREAKPOINT is defined to call this new
439 function.
440
441 The differences between ppc_linux_memory_remove_breakpoint () and
442 memory_remove_breakpoint () are minor. All that the former does
443 that the latter does not is check to make sure that the breakpoint
444 location actually contains a breakpoint (trap instruction) prior
445 to attempting to write back the old contents. If it does contain
446 a trap instruction, we allow the old contents to be written back.
447 Otherwise, we silently do nothing.
448
449 The big question is whether memory_remove_breakpoint () should be
450 changed to have the same functionality. The downside is that more
451 traffic is generated for remote targets since we'll have an extra
452 fetch of a memory word each time a breakpoint is removed.
453
454 For the time being, we'll leave this self-modifying-code-friendly
455 version in ppc-linux-tdep.c, but it ought to be migrated somewhere
456 else in the event that some other platform has similar needs with
457 regard to removing breakpoints in some potentially self modifying
458 code. */
459 int
460 ppc_linux_memory_remove_breakpoint (struct bp_target_info *bp_tgt)
461 {
462 CORE_ADDR addr = bp_tgt->placed_address;
463 const unsigned char *bp;
464 int val;
465 int bplen;
466 gdb_byte old_contents[BREAKPOINT_MAX];
467
468 /* Determine appropriate breakpoint contents and size for this address. */
469 bp = BREAKPOINT_FROM_PC (&addr, &bplen);
470 if (bp == NULL)
471 error (_("Software breakpoints not implemented for this target."));
472
473 val = target_read_memory (addr, old_contents, bplen);
474
475 /* If our breakpoint is no longer at the address, this means that the
476 program modified the code on us, so it is wrong to put back the
477 old value */
478 if (val == 0 && memcmp (bp, old_contents, bplen) == 0)
479 val = target_write_memory (addr, bp_tgt->shadow_contents, bplen);
480
481 return val;
482 }
483
484 /* For historic reasons, PPC 32 GNU/Linux follows PowerOpen rather
485 than the 32 bit SYSV R4 ABI structure return convention - all
486 structures, no matter their size, are put in memory. Vectors,
487 which were added later, do get returned in a register though. */
488
489 static enum return_value_convention
490 ppc_linux_return_value (struct gdbarch *gdbarch, struct type *valtype,
491 struct regcache *regcache, gdb_byte *readbuf,
492 const gdb_byte *writebuf)
493 {
494 if ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
495 || TYPE_CODE (valtype) == TYPE_CODE_UNION)
496 && !((TYPE_LENGTH (valtype) == 16 || TYPE_LENGTH (valtype) == 8)
497 && TYPE_VECTOR (valtype)))
498 return RETURN_VALUE_STRUCT_CONVENTION;
499 else
500 return ppc_sysv_abi_return_value (gdbarch, valtype, regcache, readbuf,
501 writebuf);
502 }
503
504 /* Macros for matching instructions. Note that, since all the
505 operands are masked off before they're or-ed into the instruction,
506 you can use -1 to make masks. */
507
508 #define insn_d(opcd, rts, ra, d) \
509 ((((opcd) & 0x3f) << 26) \
510 | (((rts) & 0x1f) << 21) \
511 | (((ra) & 0x1f) << 16) \
512 | ((d) & 0xffff))
513
514 #define insn_ds(opcd, rts, ra, d, xo) \
515 ((((opcd) & 0x3f) << 26) \
516 | (((rts) & 0x1f) << 21) \
517 | (((ra) & 0x1f) << 16) \
518 | ((d) & 0xfffc) \
519 | ((xo) & 0x3))
520
521 #define insn_xfx(opcd, rts, spr, xo) \
522 ((((opcd) & 0x3f) << 26) \
523 | (((rts) & 0x1f) << 21) \
524 | (((spr) & 0x1f) << 16) \
525 | (((spr) & 0x3e0) << 6) \
526 | (((xo) & 0x3ff) << 1))
527
528 /* Read a PPC instruction from memory. PPC instructions are always
529 big-endian, no matter what endianness the program is running in, so
530 we can't use read_memory_integer or one of its friends here. */
531 static unsigned int
532 read_insn (CORE_ADDR pc)
533 {
534 unsigned char buf[4];
535
536 read_memory (pc, buf, 4);
537 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
538 }
539
540
541 /* An instruction to match. */
542 struct insn_pattern
543 {
544 unsigned int mask; /* mask the insn with this... */
545 unsigned int data; /* ...and see if it matches this. */
546 int optional; /* If non-zero, this insn may be absent. */
547 };
548
549 /* Return non-zero if the instructions at PC match the series
550 described in PATTERN, or zero otherwise. PATTERN is an array of
551 'struct insn_pattern' objects, terminated by an entry whose mask is
552 zero.
553
554 When the match is successful, fill INSN[i] with what PATTERN[i]
555 matched. If PATTERN[i] is optional, and the instruction wasn't
556 present, set INSN[i] to 0 (which is not a valid PPC instruction).
557 INSN should have as many elements as PATTERN. Note that, if
558 PATTERN contains optional instructions which aren't present in
559 memory, then INSN will have holes, so INSN[i] isn't necessarily the
560 i'th instruction in memory. */
561 static int
562 insns_match_pattern (CORE_ADDR pc,
563 struct insn_pattern *pattern,
564 unsigned int *insn)
565 {
566 int i;
567
568 for (i = 0; pattern[i].mask; i++)
569 {
570 insn[i] = read_insn (pc);
571 if ((insn[i] & pattern[i].mask) == pattern[i].data)
572 pc += 4;
573 else if (pattern[i].optional)
574 insn[i] = 0;
575 else
576 return 0;
577 }
578
579 return 1;
580 }
581
582
583 /* Return the 'd' field of the d-form instruction INSN, properly
584 sign-extended. */
585 static CORE_ADDR
586 insn_d_field (unsigned int insn)
587 {
588 return ((((CORE_ADDR) insn & 0xffff) ^ 0x8000) - 0x8000);
589 }
590
591
592 /* Return the 'ds' field of the ds-form instruction INSN, with the two
593 zero bits concatenated at the right, and properly
594 sign-extended. */
595 static CORE_ADDR
596 insn_ds_field (unsigned int insn)
597 {
598 return ((((CORE_ADDR) insn & 0xfffc) ^ 0x8000) - 0x8000);
599 }
600
601
602 /* If DESC is the address of a 64-bit PowerPC GNU/Linux function
603 descriptor, return the descriptor's entry point. */
604 static CORE_ADDR
605 ppc64_desc_entry_point (CORE_ADDR desc)
606 {
607 /* The first word of the descriptor is the entry point. */
608 return (CORE_ADDR) read_memory_unsigned_integer (desc, 8);
609 }
610
611
612 /* Pattern for the standard linkage function. These are built by
613 build_plt_stub in elf64-ppc.c, whose GLINK argument is always
614 zero. */
615 static struct insn_pattern ppc64_standard_linkage[] =
616 {
617 /* addis r12, r2, <any> */
618 { insn_d (-1, -1, -1, 0), insn_d (15, 12, 2, 0), 0 },
619
620 /* std r2, 40(r1) */
621 { -1, insn_ds (62, 2, 1, 40, 0), 0 },
622
623 /* ld r11, <any>(r12) */
624 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 },
625
626 /* addis r12, r12, 1 <optional> */
627 { insn_d (-1, -1, -1, -1), insn_d (15, 12, 2, 1), 1 },
628
629 /* ld r2, <any>(r12) */
630 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 2, 12, 0, 0), 0 },
631
632 /* addis r12, r12, 1 <optional> */
633 { insn_d (-1, -1, -1, -1), insn_d (15, 12, 2, 1), 1 },
634
635 /* mtctr r11 */
636 { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 11, 9, 467),
637 0 },
638
639 /* ld r11, <any>(r12) */
640 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 },
641
642 /* bctr */
643 { -1, 0x4e800420, 0 },
644
645 { 0, 0, 0 }
646 };
647 #define PPC64_STANDARD_LINKAGE_LEN \
648 (sizeof (ppc64_standard_linkage) / sizeof (ppc64_standard_linkage[0]))
649
650 /* When the dynamic linker is doing lazy symbol resolution, the first
651 call to a function in another object will go like this:
652
653 - The user's function calls the linkage function:
654
655 100007c4: 4b ff fc d5 bl 10000498
656 100007c8: e8 41 00 28 ld r2,40(r1)
657
658 - The linkage function loads the entry point (and other stuff) from
659 the function descriptor in the PLT, and jumps to it:
660
661 10000498: 3d 82 00 00 addis r12,r2,0
662 1000049c: f8 41 00 28 std r2,40(r1)
663 100004a0: e9 6c 80 98 ld r11,-32616(r12)
664 100004a4: e8 4c 80 a0 ld r2,-32608(r12)
665 100004a8: 7d 69 03 a6 mtctr r11
666 100004ac: e9 6c 80 a8 ld r11,-32600(r12)
667 100004b0: 4e 80 04 20 bctr
668
669 - But since this is the first time that PLT entry has been used, it
670 sends control to its glink entry. That loads the number of the
671 PLT entry and jumps to the common glink0 code:
672
673 10000c98: 38 00 00 00 li r0,0
674 10000c9c: 4b ff ff dc b 10000c78
675
676 - The common glink0 code then transfers control to the dynamic
677 linker's fixup code:
678
679 10000c78: e8 41 00 28 ld r2,40(r1)
680 10000c7c: 3d 82 00 00 addis r12,r2,0
681 10000c80: e9 6c 80 80 ld r11,-32640(r12)
682 10000c84: e8 4c 80 88 ld r2,-32632(r12)
683 10000c88: 7d 69 03 a6 mtctr r11
684 10000c8c: e9 6c 80 90 ld r11,-32624(r12)
685 10000c90: 4e 80 04 20 bctr
686
687 Eventually, this code will figure out how to skip all of this,
688 including the dynamic linker. At the moment, we just get through
689 the linkage function. */
690
691 /* If the current thread is about to execute a series of instructions
692 at PC matching the ppc64_standard_linkage pattern, and INSN is the result
693 from that pattern match, return the code address to which the
694 standard linkage function will send them. (This doesn't deal with
695 dynamic linker lazy symbol resolution stubs.) */
696 static CORE_ADDR
697 ppc64_standard_linkage_target (CORE_ADDR pc, unsigned int *insn)
698 {
699 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
700
701 /* The address of the function descriptor this linkage function
702 references. */
703 CORE_ADDR desc
704 = ((CORE_ADDR) read_register (tdep->ppc_gp0_regnum + 2)
705 + (insn_d_field (insn[0]) << 16)
706 + insn_ds_field (insn[2]));
707
708 /* The first word of the descriptor is the entry point. Return that. */
709 return ppc64_desc_entry_point (desc);
710 }
711
712
713 /* Given that we've begun executing a call trampoline at PC, return
714 the entry point of the function the trampoline will go to. */
715 static CORE_ADDR
716 ppc64_skip_trampoline_code (CORE_ADDR pc)
717 {
718 unsigned int ppc64_standard_linkage_insn[PPC64_STANDARD_LINKAGE_LEN];
719
720 if (insns_match_pattern (pc, ppc64_standard_linkage,
721 ppc64_standard_linkage_insn))
722 return ppc64_standard_linkage_target (pc, ppc64_standard_linkage_insn);
723 else
724 return 0;
725 }
726
727
728 /* Support for CONVERT_FROM_FUNC_PTR_ADDR (ARCH, ADDR, TARG) on PPC64
729 GNU/Linux.
730
731 Usually a function pointer's representation is simply the address
732 of the function. On GNU/Linux on the 64-bit PowerPC however, a
733 function pointer is represented by a pointer to a TOC entry. This
734 TOC entry contains three words, the first word is the address of
735 the function, the second word is the TOC pointer (r2), and the
736 third word is the static chain value. Throughout GDB it is
737 currently assumed that a function pointer contains the address of
738 the function, which is not easy to fix. In addition, the
739 conversion of a function address to a function pointer would
740 require allocation of a TOC entry in the inferior's memory space,
741 with all its drawbacks. To be able to call C++ virtual methods in
742 the inferior (which are called via function pointers),
743 find_function_addr uses this function to get the function address
744 from a function pointer. */
745
746 /* If ADDR points at what is clearly a function descriptor, transform
747 it into the address of the corresponding function. Be
748 conservative, otherwize GDB will do the transformation on any
749 random addresses such as occures when there is no symbol table. */
750
751 static CORE_ADDR
752 ppc64_linux_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
753 CORE_ADDR addr,
754 struct target_ops *targ)
755 {
756 struct section_table *s = target_section_by_addr (targ, addr);
757
758 /* Check if ADDR points to a function descriptor. */
759 if (s && strcmp (s->the_bfd_section->name, ".opd") == 0)
760 return get_target_memory_unsigned (targ, addr, 8);
761
762 return addr;
763 }
764
765 static void
766 right_supply_register (struct regcache *regcache, int wordsize, int regnum,
767 const bfd_byte *buf)
768 {
769 regcache_raw_supply (regcache, regnum,
770 (buf + wordsize - register_size (current_gdbarch, regnum)));
771 }
772
773 /* Extract the register values found in the WORDSIZED ABI GREGSET,
774 storing their values in REGCACHE. Note that some are left-aligned,
775 while others are right aligned. */
776
777 void
778 ppc_linux_supply_gregset (struct regcache *regcache,
779 int regnum, const void *gregs, size_t size,
780 int wordsize)
781 {
782 int regi;
783 struct gdbarch *regcache_arch = get_regcache_arch (regcache);
784 struct gdbarch_tdep *regcache_tdep = gdbarch_tdep (regcache_arch);
785 const bfd_byte *buf = gregs;
786
787 for (regi = 0; regi < ppc_num_gprs; regi++)
788 right_supply_register (regcache, wordsize,
789 regcache_tdep->ppc_gp0_regnum + regi,
790 buf + wordsize * regi);
791
792 right_supply_register (regcache, wordsize, gdbarch_pc_regnum (regcache_arch),
793 buf + wordsize * PPC_LINUX_PT_NIP);
794 right_supply_register (regcache, wordsize, regcache_tdep->ppc_lr_regnum,
795 buf + wordsize * PPC_LINUX_PT_LNK);
796 regcache_raw_supply (regcache, regcache_tdep->ppc_cr_regnum,
797 buf + wordsize * PPC_LINUX_PT_CCR);
798 regcache_raw_supply (regcache, regcache_tdep->ppc_xer_regnum,
799 buf + wordsize * PPC_LINUX_PT_XER);
800 regcache_raw_supply (regcache, regcache_tdep->ppc_ctr_regnum,
801 buf + wordsize * PPC_LINUX_PT_CTR);
802 if (regcache_tdep->ppc_mq_regnum != -1)
803 right_supply_register (regcache, wordsize, regcache_tdep->ppc_mq_regnum,
804 buf + wordsize * PPC_LINUX_PT_MQ);
805 right_supply_register (regcache, wordsize, regcache_tdep->ppc_ps_regnum,
806 buf + wordsize * PPC_LINUX_PT_MSR);
807 }
808
809 static void
810 ppc32_linux_supply_gregset (const struct regset *regset,
811 struct regcache *regcache,
812 int regnum, const void *gregs, size_t size)
813 {
814 ppc_linux_supply_gregset (regcache, regnum, gregs, size, 4);
815 }
816
817 static struct regset ppc32_linux_gregset = {
818 NULL, ppc32_linux_supply_gregset
819 };
820
821 static void
822 ppc64_linux_supply_gregset (const struct regset *regset,
823 struct regcache * regcache,
824 int regnum, const void *gregs, size_t size)
825 {
826 ppc_linux_supply_gregset (regcache, regnum, gregs, size, 8);
827 }
828
829 static struct regset ppc64_linux_gregset = {
830 NULL, ppc64_linux_supply_gregset
831 };
832
833 void
834 ppc_linux_supply_fpregset (const struct regset *regset,
835 struct regcache * regcache,
836 int regnum, const void *fpset, size_t size)
837 {
838 int regi;
839 struct gdbarch *regcache_arch = get_regcache_arch (regcache);
840 struct gdbarch_tdep *regcache_tdep = gdbarch_tdep (regcache_arch);
841 const bfd_byte *buf = fpset;
842
843 if (! ppc_floating_point_unit_p (regcache_arch))
844 return;
845
846 for (regi = 0; regi < ppc_num_fprs; regi++)
847 regcache_raw_supply (regcache,
848 regcache_tdep->ppc_fp0_regnum + regi,
849 buf + 8 * regi);
850
851 /* The FPSCR is stored in the low order word of the last
852 doubleword in the fpregset. */
853 regcache_raw_supply (regcache, regcache_tdep->ppc_fpscr_regnum,
854 buf + 8 * 32 + 4);
855 }
856
857 static struct regset ppc_linux_fpregset = { NULL, ppc_linux_supply_fpregset };
858
859 static const struct regset *
860 ppc_linux_regset_from_core_section (struct gdbarch *core_arch,
861 const char *sect_name, size_t sect_size)
862 {
863 struct gdbarch_tdep *tdep = gdbarch_tdep (core_arch);
864 if (strcmp (sect_name, ".reg") == 0)
865 {
866 if (tdep->wordsize == 4)
867 return &ppc32_linux_gregset;
868 else
869 return &ppc64_linux_gregset;
870 }
871 if (strcmp (sect_name, ".reg2") == 0)
872 return &ppc_linux_fpregset;
873 return NULL;
874 }
875
876 static void
877 ppc_linux_sigtramp_cache (struct frame_info *next_frame,
878 struct trad_frame_cache *this_cache,
879 CORE_ADDR func, LONGEST offset,
880 int bias)
881 {
882 CORE_ADDR base;
883 CORE_ADDR regs;
884 CORE_ADDR gpregs;
885 CORE_ADDR fpregs;
886 int i;
887 struct gdbarch *gdbarch = get_frame_arch (next_frame);
888 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
889
890 base = frame_unwind_register_unsigned (next_frame, SP_REGNUM);
891 if (bias > 0 && frame_pc_unwind (next_frame) != func)
892 /* See below, some signal trampolines increment the stack as their
893 first instruction, need to compensate for that. */
894 base -= bias;
895
896 /* Find the address of the register buffer pointer. */
897 regs = base + offset;
898 /* Use that to find the address of the corresponding register
899 buffers. */
900 gpregs = read_memory_unsigned_integer (regs, tdep->wordsize);
901 fpregs = gpregs + 48 * tdep->wordsize;
902
903 /* General purpose. */
904 for (i = 0; i < 32; i++)
905 {
906 int regnum = i + tdep->ppc_gp0_regnum;
907 trad_frame_set_reg_addr (this_cache, regnum, gpregs + i * tdep->wordsize);
908 }
909 trad_frame_set_reg_addr (this_cache, PC_REGNUM, gpregs + 32 * tdep->wordsize);
910 trad_frame_set_reg_addr (this_cache, tdep->ppc_ctr_regnum,
911 gpregs + 35 * tdep->wordsize);
912 trad_frame_set_reg_addr (this_cache, tdep->ppc_lr_regnum,
913 gpregs + 36 * tdep->wordsize);
914 trad_frame_set_reg_addr (this_cache, tdep->ppc_xer_regnum,
915 gpregs + 37 * tdep->wordsize);
916 trad_frame_set_reg_addr (this_cache, tdep->ppc_cr_regnum,
917 gpregs + 38 * tdep->wordsize);
918
919 if (ppc_floating_point_unit_p (gdbarch))
920 {
921 /* Floating point registers. */
922 for (i = 0; i < 32; i++)
923 {
924 int regnum = i + FP0_REGNUM;
925 trad_frame_set_reg_addr (this_cache, regnum,
926 fpregs + i * tdep->wordsize);
927 }
928 trad_frame_set_reg_addr (this_cache, tdep->ppc_fpscr_regnum,
929 fpregs + 32 * tdep->wordsize);
930 }
931 trad_frame_set_id (this_cache, frame_id_build (base, func));
932 }
933
934 static void
935 ppc32_linux_sigaction_cache_init (const struct tramp_frame *self,
936 struct frame_info *next_frame,
937 struct trad_frame_cache *this_cache,
938 CORE_ADDR func)
939 {
940 ppc_linux_sigtramp_cache (next_frame, this_cache, func,
941 0xd0 /* Offset to ucontext_t. */
942 + 0x30 /* Offset to .reg. */,
943 0);
944 }
945
946 static void
947 ppc64_linux_sigaction_cache_init (const struct tramp_frame *self,
948 struct frame_info *next_frame,
949 struct trad_frame_cache *this_cache,
950 CORE_ADDR func)
951 {
952 ppc_linux_sigtramp_cache (next_frame, this_cache, func,
953 0x80 /* Offset to ucontext_t. */
954 + 0xe0 /* Offset to .reg. */,
955 128);
956 }
957
958 static void
959 ppc32_linux_sighandler_cache_init (const struct tramp_frame *self,
960 struct frame_info *next_frame,
961 struct trad_frame_cache *this_cache,
962 CORE_ADDR func)
963 {
964 ppc_linux_sigtramp_cache (next_frame, this_cache, func,
965 0x40 /* Offset to ucontext_t. */
966 + 0x1c /* Offset to .reg. */,
967 0);
968 }
969
970 static void
971 ppc64_linux_sighandler_cache_init (const struct tramp_frame *self,
972 struct frame_info *next_frame,
973 struct trad_frame_cache *this_cache,
974 CORE_ADDR func)
975 {
976 ppc_linux_sigtramp_cache (next_frame, this_cache, func,
977 0x80 /* Offset to struct sigcontext. */
978 + 0x38 /* Offset to .reg. */,
979 128);
980 }
981
982 static struct tramp_frame ppc32_linux_sigaction_tramp_frame = {
983 SIGTRAMP_FRAME,
984 4,
985 {
986 { 0x380000ac, -1 }, /* li r0, 172 */
987 { 0x44000002, -1 }, /* sc */
988 { TRAMP_SENTINEL_INSN },
989 },
990 ppc32_linux_sigaction_cache_init
991 };
992 static struct tramp_frame ppc64_linux_sigaction_tramp_frame = {
993 SIGTRAMP_FRAME,
994 4,
995 {
996 { 0x38210080, -1 }, /* addi r1,r1,128 */
997 { 0x380000ac, -1 }, /* li r0, 172 */
998 { 0x44000002, -1 }, /* sc */
999 { TRAMP_SENTINEL_INSN },
1000 },
1001 ppc64_linux_sigaction_cache_init
1002 };
1003 static struct tramp_frame ppc32_linux_sighandler_tramp_frame = {
1004 SIGTRAMP_FRAME,
1005 4,
1006 {
1007 { 0x38000077, -1 }, /* li r0,119 */
1008 { 0x44000002, -1 }, /* sc */
1009 { TRAMP_SENTINEL_INSN },
1010 },
1011 ppc32_linux_sighandler_cache_init
1012 };
1013 static struct tramp_frame ppc64_linux_sighandler_tramp_frame = {
1014 SIGTRAMP_FRAME,
1015 4,
1016 {
1017 { 0x38210080, -1 }, /* addi r1,r1,128 */
1018 { 0x38000077, -1 }, /* li r0,119 */
1019 { 0x44000002, -1 }, /* sc */
1020 { TRAMP_SENTINEL_INSN },
1021 },
1022 ppc64_linux_sighandler_cache_init
1023 };
1024
1025 static void
1026 ppc_linux_init_abi (struct gdbarch_info info,
1027 struct gdbarch *gdbarch)
1028 {
1029 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1030
1031 /* NOTE: jimb/2004-03-26: The System V ABI PowerPC Processor
1032 Supplement says that long doubles are sixteen bytes long.
1033 However, as one of the known warts of its ABI, PPC GNU/Linux uses
1034 eight-byte long doubles. GCC only recently got 128-bit long
1035 double support on PPC, so it may be changing soon. The
1036 Linux[sic] Standards Base says that programs that use 'long
1037 double' on PPC GNU/Linux are non-conformant. */
1038 /* NOTE: cagney/2005-01-25: True for both 32- and 64-bit. */
1039 set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1040
1041 if (tdep->wordsize == 4)
1042 {
1043 /* Until November 2001, gcc did not comply with the 32 bit SysV
1044 R4 ABI requirement that structures less than or equal to 8
1045 bytes should be returned in registers. Instead GCC was using
1046 the the AIX/PowerOpen ABI - everything returned in memory
1047 (well ignoring vectors that is). When this was corrected, it
1048 wasn't fixed for GNU/Linux native platform. Use the
1049 PowerOpen struct convention. */
1050 set_gdbarch_return_value (gdbarch, ppc_linux_return_value);
1051
1052 set_gdbarch_memory_remove_breakpoint (gdbarch,
1053 ppc_linux_memory_remove_breakpoint);
1054
1055 /* Shared library handling. */
1056 set_gdbarch_skip_trampoline_code (gdbarch,
1057 ppc_linux_skip_trampoline_code);
1058 set_solib_svr4_fetch_link_map_offsets
1059 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
1060
1061 /* Trampolines. */
1062 tramp_frame_prepend_unwinder (gdbarch, &ppc32_linux_sigaction_tramp_frame);
1063 tramp_frame_prepend_unwinder (gdbarch, &ppc32_linux_sighandler_tramp_frame);
1064 }
1065
1066 if (tdep->wordsize == 8)
1067 {
1068 /* Handle PPC64 GNU/Linux function pointers (which are really
1069 function descriptors). */
1070 set_gdbarch_convert_from_func_ptr_addr
1071 (gdbarch, ppc64_linux_convert_from_func_ptr_addr);
1072 set_gdbarch_skip_trampoline_code (gdbarch, ppc64_skip_trampoline_code);
1073
1074 /* Shared library handling. */
1075 set_solib_svr4_fetch_link_map_offsets
1076 (gdbarch, svr4_lp64_fetch_link_map_offsets);
1077
1078 /* Trampolines. */
1079 tramp_frame_prepend_unwinder (gdbarch, &ppc64_linux_sigaction_tramp_frame);
1080 tramp_frame_prepend_unwinder (gdbarch, &ppc64_linux_sighandler_tramp_frame);
1081 }
1082 set_gdbarch_regset_from_core_section (gdbarch, ppc_linux_regset_from_core_section);
1083
1084 /* Enable TLS support. */
1085 set_gdbarch_fetch_tls_load_module_address (gdbarch,
1086 svr4_fetch_objfile_link_map);
1087 }
1088
1089 void
1090 _initialize_ppc_linux_tdep (void)
1091 {
1092 /* Register for all sub-familes of the POWER/PowerPC: 32-bit and
1093 64-bit PowerPC, and the older rs6k. */
1094 gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc, GDB_OSABI_LINUX,
1095 ppc_linux_init_abi);
1096 gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc64, GDB_OSABI_LINUX,
1097 ppc_linux_init_abi);
1098 gdbarch_register_osabi (bfd_arch_rs6000, bfd_mach_rs6k, GDB_OSABI_LINUX,
1099 ppc_linux_init_abi);
1100 }