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