]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ppc-linux-tdep.c
* rs6000-tdep.c: Do not include "rs6000-tdep.h".
[thirdparty/binutils-gdb.git] / gdb / ppc-linux-tdep.c
CommitLineData
c877c8e6 1/* Target-dependent code for GDB, the GNU debugger.
4e052eda 2
6aba47ca 3 Copyright (C) 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
9b254dd1 4 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c877c8e6
KB
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
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c877c8e6
KB
21
22#include "defs.h"
23#include "frame.h"
24#include "inferior.h"
25#include "symtab.h"
26#include "target.h"
27#include "gdbcore.h"
28#include "gdbcmd.h"
29#include "symfile.h"
30#include "objfiles.h"
4e052eda 31#include "regcache.h"
fd0407d6 32#include "value.h"
4be87837 33#include "osabi.h"
f9be684a 34#include "regset.h"
6ded7999 35#include "solib-svr4.h"
9aa1e687 36#include "ppc-tdep.h"
7284e1be 37#include "ppc-linux-tdep.h"
61a65099
KB
38#include "trad-frame.h"
39#include "frame-unwind.h"
a8f60bfc 40#include "tramp-frame.h"
9aa1e687 41
7284e1be
UW
42#include "features/rs6000/powerpc-32l.c"
43#include "features/rs6000/powerpc-altivec32l.c"
44#include "features/rs6000/powerpc-64l.c"
45#include "features/rs6000/powerpc-altivec64l.c"
46#include "features/rs6000/powerpc-e500l.c"
47
6974274f 48static CORE_ADDR
52f729a7 49ppc_linux_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
c877c8e6 50{
50fd1280 51 gdb_byte buf[4];
c877c8e6
KB
52 struct obj_section *sect;
53 struct objfile *objfile;
54 unsigned long insn;
55 CORE_ADDR plt_start = 0;
56 CORE_ADDR symtab = 0;
57 CORE_ADDR strtab = 0;
58 int num_slots = -1;
59 int reloc_index = -1;
60 CORE_ADDR plt_table;
61 CORE_ADDR reloc;
62 CORE_ADDR sym;
63 long symidx;
64 char symname[1024];
65 struct minimal_symbol *msymbol;
66
82233d87 67 /* Find the section pc is in; if not in .plt, try the default method. */
c877c8e6
KB
68 sect = find_pc_section (pc);
69 if (!sect || strcmp (sect->the_bfd_section->name, ".plt") != 0)
82233d87 70 return find_solib_trampoline_target (frame, pc);
c877c8e6
KB
71
72 objfile = sect->objfile;
73
74 /* Pick up the instruction at pc. It had better be of the
75 form
76 li r11, IDX
77
78 where IDX is an index into the plt_table. */
79
80 if (target_read_memory (pc, buf, 4) != 0)
81 return 0;
82 insn = extract_unsigned_integer (buf, 4);
83
84 if ((insn & 0xffff0000) != 0x39600000 /* li r11, VAL */ )
85 return 0;
86
87 reloc_index = (insn << 16) >> 16;
88
89 /* Find the objfile that pc is in and obtain the information
90 necessary for finding the symbol name. */
91 for (sect = objfile->sections; sect < objfile->sections_end; ++sect)
92 {
93 const char *secname = sect->the_bfd_section->name;
94 if (strcmp (secname, ".plt") == 0)
95 plt_start = sect->addr;
96 else if (strcmp (secname, ".rela.plt") == 0)
97 num_slots = ((int) sect->endaddr - (int) sect->addr) / 12;
98 else if (strcmp (secname, ".dynsym") == 0)
99 symtab = sect->addr;
100 else if (strcmp (secname, ".dynstr") == 0)
101 strtab = sect->addr;
102 }
103
104 /* Make sure we have all the information we need. */
105 if (plt_start == 0 || num_slots == -1 || symtab == 0 || strtab == 0)
106 return 0;
107
108 /* Compute the value of the plt table */
109 plt_table = plt_start + 72 + 8 * num_slots;
110
111 /* Get address of the relocation entry (Elf32_Rela) */
112 if (target_read_memory (plt_table + reloc_index, buf, 4) != 0)
113 return 0;
7c0b4a20 114 reloc = extract_unsigned_integer (buf, 4);
c877c8e6
KB
115
116 sect = find_pc_section (reloc);
117 if (!sect)
118 return 0;
119
120 if (strcmp (sect->the_bfd_section->name, ".text") == 0)
121 return reloc;
122
123 /* Now get the r_info field which is the relocation type and symbol
124 index. */
125 if (target_read_memory (reloc + 4, buf, 4) != 0)
126 return 0;
127 symidx = extract_unsigned_integer (buf, 4);
128
129 /* Shift out the relocation type leaving just the symbol index */
130 /* symidx = ELF32_R_SYM(symidx); */
131 symidx = symidx >> 8;
132
133 /* compute the address of the symbol */
134 sym = symtab + symidx * 4;
135
136 /* Fetch the string table index */
137 if (target_read_memory (sym, buf, 4) != 0)
138 return 0;
139 symidx = extract_unsigned_integer (buf, 4);
140
141 /* Fetch the string; we don't know how long it is. Is it possible
142 that the following will fail because we're trying to fetch too
143 much? */
50fd1280
AC
144 if (target_read_memory (strtab + symidx, (gdb_byte *) symname,
145 sizeof (symname)) != 0)
c877c8e6
KB
146 return 0;
147
148 /* This might not work right if we have multiple symbols with the
149 same name; the only way to really get it right is to perform
150 the same sort of lookup as the dynamic linker. */
5520a790 151 msymbol = lookup_minimal_symbol_text (symname, NULL);
c877c8e6
KB
152 if (!msymbol)
153 return 0;
154
155 return SYMBOL_VALUE_ADDRESS (msymbol);
156}
157
122a33de
KB
158/* ppc_linux_memory_remove_breakpoints attempts to remove a breakpoint
159 in much the same fashion as memory_remove_breakpoint in mem-break.c,
160 but is careful not to write back the previous contents if the code
161 in question has changed in between inserting the breakpoint and
162 removing it.
163
164 Here is the problem that we're trying to solve...
165
166 Once upon a time, before introducing this function to remove
167 breakpoints from the inferior, setting a breakpoint on a shared
168 library function prior to running the program would not work
169 properly. In order to understand the problem, it is first
170 necessary to understand a little bit about dynamic linking on
171 this platform.
172
173 A call to a shared library function is accomplished via a bl
174 (branch-and-link) instruction whose branch target is an entry
175 in the procedure linkage table (PLT). The PLT in the object
176 file is uninitialized. To gdb, prior to running the program, the
177 entries in the PLT are all zeros.
178
179 Once the program starts running, the shared libraries are loaded
180 and the procedure linkage table is initialized, but the entries in
181 the table are not (necessarily) resolved. Once a function is
182 actually called, the code in the PLT is hit and the function is
183 resolved. In order to better illustrate this, an example is in
184 order; the following example is from the gdb testsuite.
185
186 We start the program shmain.
187
188 [kev@arroyo testsuite]$ ../gdb gdb.base/shmain
189 [...]
190
191 We place two breakpoints, one on shr1 and the other on main.
192
193 (gdb) b shr1
194 Breakpoint 1 at 0x100409d4
195 (gdb) b main
196 Breakpoint 2 at 0x100006a0: file gdb.base/shmain.c, line 44.
197
198 Examine the instruction (and the immediatly following instruction)
199 upon which the breakpoint was placed. Note that the PLT entry
200 for shr1 contains zeros.
201
202 (gdb) x/2i 0x100409d4
203 0x100409d4 <shr1>: .long 0x0
204 0x100409d8 <shr1+4>: .long 0x0
205
206 Now run 'til main.
207
208 (gdb) r
209 Starting program: gdb.base/shmain
210 Breakpoint 1 at 0xffaf790: file gdb.base/shr1.c, line 19.
211
212 Breakpoint 2, main ()
213 at gdb.base/shmain.c:44
214 44 g = 1;
215
216 Examine the PLT again. Note that the loading of the shared
217 library has initialized the PLT to code which loads a constant
218 (which I think is an index into the GOT) into r11 and then
219 branchs a short distance to the code which actually does the
220 resolving.
221
222 (gdb) x/2i 0x100409d4
223 0x100409d4 <shr1>: li r11,4
224 0x100409d8 <shr1+4>: b 0x10040984 <sg+4>
225 (gdb) c
226 Continuing.
227
228 Breakpoint 1, shr1 (x=1)
229 at gdb.base/shr1.c:19
230 19 l = 1;
231
232 Now we've hit the breakpoint at shr1. (The breakpoint was
233 reset from the PLT entry to the actual shr1 function after the
234 shared library was loaded.) Note that the PLT entry has been
235 resolved to contain a branch that takes us directly to shr1.
236 (The real one, not the PLT entry.)
237
238 (gdb) x/2i 0x100409d4
239 0x100409d4 <shr1>: b 0xffaf76c <shr1>
240 0x100409d8 <shr1+4>: b 0x10040984 <sg+4>
241
242 The thing to note here is that the PLT entry for shr1 has been
243 changed twice.
244
245 Now the problem should be obvious. GDB places a breakpoint (a
246 trap instruction) on the zero value of the PLT entry for shr1.
247 Later on, after the shared library had been loaded and the PLT
248 initialized, GDB gets a signal indicating this fact and attempts
249 (as it always does when it stops) to remove all the breakpoints.
250
251 The breakpoint removal was causing the former contents (a zero
252 word) to be written back to the now initialized PLT entry thus
253 destroying a portion of the initialization that had occurred only a
254 short time ago. When execution continued, the zero word would be
255 executed as an instruction an an illegal instruction trap was
256 generated instead. (0 is not a legal instruction.)
257
258 The fix for this problem was fairly straightforward. The function
259 memory_remove_breakpoint from mem-break.c was copied to this file,
260 modified slightly, and renamed to ppc_linux_memory_remove_breakpoint.
261 In tm-linux.h, MEMORY_REMOVE_BREAKPOINT is defined to call this new
262 function.
263
264 The differences between ppc_linux_memory_remove_breakpoint () and
265 memory_remove_breakpoint () are minor. All that the former does
266 that the latter does not is check to make sure that the breakpoint
267 location actually contains a breakpoint (trap instruction) prior
268 to attempting to write back the old contents. If it does contain
269 a trap instruction, we allow the old contents to be written back.
270 Otherwise, we silently do nothing.
271
272 The big question is whether memory_remove_breakpoint () should be
273 changed to have the same functionality. The downside is that more
274 traffic is generated for remote targets since we'll have an extra
275 fetch of a memory word each time a breakpoint is removed.
276
277 For the time being, we'll leave this self-modifying-code-friendly
278 version in ppc-linux-tdep.c, but it ought to be migrated somewhere
279 else in the event that some other platform has similar needs with
280 regard to removing breakpoints in some potentially self modifying
281 code. */
482ca3f5 282int
ae4b2284
MD
283ppc_linux_memory_remove_breakpoint (struct gdbarch *gdbarch,
284 struct bp_target_info *bp_tgt)
482ca3f5 285{
8181d85f 286 CORE_ADDR addr = bp_tgt->placed_address;
f4f9705a 287 const unsigned char *bp;
482ca3f5
KB
288 int val;
289 int bplen;
50fd1280 290 gdb_byte old_contents[BREAKPOINT_MAX];
8defab1a 291 struct cleanup *cleanup;
482ca3f5
KB
292
293 /* Determine appropriate breakpoint contents and size for this address. */
ae4b2284 294 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen);
482ca3f5 295 if (bp == NULL)
8a3fe4f8 296 error (_("Software breakpoints not implemented for this target."));
482ca3f5 297
8defab1a
DJ
298 /* Make sure we see the memory breakpoints. */
299 cleanup = make_show_memory_breakpoints_cleanup (1);
482ca3f5
KB
300 val = target_read_memory (addr, old_contents, bplen);
301
302 /* If our breakpoint is no longer at the address, this means that the
303 program modified the code on us, so it is wrong to put back the
304 old value */
305 if (val == 0 && memcmp (bp, old_contents, bplen) == 0)
8181d85f 306 val = target_write_memory (addr, bp_tgt->shadow_contents, bplen);
482ca3f5 307
8defab1a 308 do_cleanups (cleanup);
482ca3f5
KB
309 return val;
310}
6ded7999 311
b9ff3018
AC
312/* For historic reasons, PPC 32 GNU/Linux follows PowerOpen rather
313 than the 32 bit SYSV R4 ABI structure return convention - all
314 structures, no matter their size, are put in memory. Vectors,
315 which were added later, do get returned in a register though. */
316
05580c65 317static enum return_value_convention
c055b101
CV
318ppc_linux_return_value (struct gdbarch *gdbarch, struct type *func_type,
319 struct type *valtype, struct regcache *regcache,
320 gdb_byte *readbuf, const gdb_byte *writebuf)
b9ff3018 321{
05580c65
AC
322 if ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
323 || TYPE_CODE (valtype) == TYPE_CODE_UNION)
324 && !((TYPE_LENGTH (valtype) == 16 || TYPE_LENGTH (valtype) == 8)
325 && TYPE_VECTOR (valtype)))
326 return RETURN_VALUE_STRUCT_CONVENTION;
327 else
c055b101
CV
328 return ppc_sysv_abi_return_value (gdbarch, func_type, valtype, regcache,
329 readbuf, writebuf);
b9ff3018
AC
330}
331
f470a70a
JB
332/* Macros for matching instructions. Note that, since all the
333 operands are masked off before they're or-ed into the instruction,
334 you can use -1 to make masks. */
335
336#define insn_d(opcd, rts, ra, d) \
337 ((((opcd) & 0x3f) << 26) \
338 | (((rts) & 0x1f) << 21) \
339 | (((ra) & 0x1f) << 16) \
340 | ((d) & 0xffff))
341
342#define insn_ds(opcd, rts, ra, d, xo) \
343 ((((opcd) & 0x3f) << 26) \
344 | (((rts) & 0x1f) << 21) \
345 | (((ra) & 0x1f) << 16) \
346 | ((d) & 0xfffc) \
347 | ((xo) & 0x3))
348
349#define insn_xfx(opcd, rts, spr, xo) \
350 ((((opcd) & 0x3f) << 26) \
351 | (((rts) & 0x1f) << 21) \
352 | (((spr) & 0x1f) << 16) \
353 | (((spr) & 0x3e0) << 6) \
354 | (((xo) & 0x3ff) << 1))
355
356/* Read a PPC instruction from memory. PPC instructions are always
357 big-endian, no matter what endianness the program is running in, so
358 we can't use read_memory_integer or one of its friends here. */
359static unsigned int
360read_insn (CORE_ADDR pc)
361{
362 unsigned char buf[4];
363
364 read_memory (pc, buf, 4);
365 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
366}
367
368
369/* An instruction to match. */
370struct insn_pattern
371{
372 unsigned int mask; /* mask the insn with this... */
373 unsigned int data; /* ...and see if it matches this. */
374 int optional; /* If non-zero, this insn may be absent. */
375};
376
377/* Return non-zero if the instructions at PC match the series
378 described in PATTERN, or zero otherwise. PATTERN is an array of
379 'struct insn_pattern' objects, terminated by an entry whose mask is
380 zero.
381
382 When the match is successful, fill INSN[i] with what PATTERN[i]
383 matched. If PATTERN[i] is optional, and the instruction wasn't
384 present, set INSN[i] to 0 (which is not a valid PPC instruction).
385 INSN should have as many elements as PATTERN. Note that, if
386 PATTERN contains optional instructions which aren't present in
387 memory, then INSN will have holes, so INSN[i] isn't necessarily the
388 i'th instruction in memory. */
389static int
390insns_match_pattern (CORE_ADDR pc,
391 struct insn_pattern *pattern,
392 unsigned int *insn)
393{
394 int i;
395
396 for (i = 0; pattern[i].mask; i++)
397 {
398 insn[i] = read_insn (pc);
399 if ((insn[i] & pattern[i].mask) == pattern[i].data)
400 pc += 4;
401 else if (pattern[i].optional)
402 insn[i] = 0;
403 else
404 return 0;
405 }
406
407 return 1;
408}
409
410
411/* Return the 'd' field of the d-form instruction INSN, properly
412 sign-extended. */
413static CORE_ADDR
414insn_d_field (unsigned int insn)
415{
416 return ((((CORE_ADDR) insn & 0xffff) ^ 0x8000) - 0x8000);
417}
418
419
420/* Return the 'ds' field of the ds-form instruction INSN, with the two
421 zero bits concatenated at the right, and properly
422 sign-extended. */
423static CORE_ADDR
424insn_ds_field (unsigned int insn)
425{
426 return ((((CORE_ADDR) insn & 0xfffc) ^ 0x8000) - 0x8000);
427}
428
429
e538d2d7 430/* If DESC is the address of a 64-bit PowerPC GNU/Linux function
d64558a5
JB
431 descriptor, return the descriptor's entry point. */
432static CORE_ADDR
433ppc64_desc_entry_point (CORE_ADDR desc)
434{
435 /* The first word of the descriptor is the entry point. */
436 return (CORE_ADDR) read_memory_unsigned_integer (desc, 8);
437}
438
439
f470a70a
JB
440/* Pattern for the standard linkage function. These are built by
441 build_plt_stub in elf64-ppc.c, whose GLINK argument is always
442 zero. */
443static struct insn_pattern ppc64_standard_linkage[] =
444 {
445 /* addis r12, r2, <any> */
446 { insn_d (-1, -1, -1, 0), insn_d (15, 12, 2, 0), 0 },
447
448 /* std r2, 40(r1) */
449 { -1, insn_ds (62, 2, 1, 40, 0), 0 },
450
451 /* ld r11, <any>(r12) */
452 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 },
453
454 /* addis r12, r12, 1 <optional> */
455 { insn_d (-1, -1, -1, -1), insn_d (15, 12, 2, 1), 1 },
456
457 /* ld r2, <any>(r12) */
458 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 2, 12, 0, 0), 0 },
459
460 /* addis r12, r12, 1 <optional> */
461 { insn_d (-1, -1, -1, -1), insn_d (15, 12, 2, 1), 1 },
462
463 /* mtctr r11 */
464 { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 11, 9, 467),
465 0 },
466
467 /* ld r11, <any>(r12) */
468 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 },
469
470 /* bctr */
471 { -1, 0x4e800420, 0 },
472
473 { 0, 0, 0 }
474 };
475#define PPC64_STANDARD_LINKAGE_LEN \
476 (sizeof (ppc64_standard_linkage) / sizeof (ppc64_standard_linkage[0]))
477
f470a70a
JB
478/* When the dynamic linker is doing lazy symbol resolution, the first
479 call to a function in another object will go like this:
480
481 - The user's function calls the linkage function:
482
483 100007c4: 4b ff fc d5 bl 10000498
484 100007c8: e8 41 00 28 ld r2,40(r1)
485
486 - The linkage function loads the entry point (and other stuff) from
487 the function descriptor in the PLT, and jumps to it:
488
489 10000498: 3d 82 00 00 addis r12,r2,0
490 1000049c: f8 41 00 28 std r2,40(r1)
491 100004a0: e9 6c 80 98 ld r11,-32616(r12)
492 100004a4: e8 4c 80 a0 ld r2,-32608(r12)
493 100004a8: 7d 69 03 a6 mtctr r11
494 100004ac: e9 6c 80 a8 ld r11,-32600(r12)
495 100004b0: 4e 80 04 20 bctr
496
497 - But since this is the first time that PLT entry has been used, it
498 sends control to its glink entry. That loads the number of the
499 PLT entry and jumps to the common glink0 code:
500
501 10000c98: 38 00 00 00 li r0,0
502 10000c9c: 4b ff ff dc b 10000c78
503
504 - The common glink0 code then transfers control to the dynamic
505 linker's fixup code:
506
507 10000c78: e8 41 00 28 ld r2,40(r1)
508 10000c7c: 3d 82 00 00 addis r12,r2,0
509 10000c80: e9 6c 80 80 ld r11,-32640(r12)
510 10000c84: e8 4c 80 88 ld r2,-32632(r12)
511 10000c88: 7d 69 03 a6 mtctr r11
512 10000c8c: e9 6c 80 90 ld r11,-32624(r12)
513 10000c90: 4e 80 04 20 bctr
514
515 Eventually, this code will figure out how to skip all of this,
516 including the dynamic linker. At the moment, we just get through
517 the linkage function. */
518
519/* If the current thread is about to execute a series of instructions
520 at PC matching the ppc64_standard_linkage pattern, and INSN is the result
521 from that pattern match, return the code address to which the
522 standard linkage function will send them. (This doesn't deal with
523 dynamic linker lazy symbol resolution stubs.) */
524static CORE_ADDR
52f729a7
UW
525ppc64_standard_linkage_target (struct frame_info *frame,
526 CORE_ADDR pc, unsigned int *insn)
f470a70a 527{
52f729a7 528 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (frame));
f470a70a
JB
529
530 /* The address of the function descriptor this linkage function
531 references. */
532 CORE_ADDR desc
52f729a7
UW
533 = ((CORE_ADDR) get_frame_register_unsigned (frame,
534 tdep->ppc_gp0_regnum + 2)
f470a70a
JB
535 + (insn_d_field (insn[0]) << 16)
536 + insn_ds_field (insn[2]));
537
538 /* The first word of the descriptor is the entry point. Return that. */
d64558a5 539 return ppc64_desc_entry_point (desc);
f470a70a
JB
540}
541
542
543/* Given that we've begun executing a call trampoline at PC, return
544 the entry point of the function the trampoline will go to. */
545static CORE_ADDR
52f729a7 546ppc64_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
f470a70a
JB
547{
548 unsigned int ppc64_standard_linkage_insn[PPC64_STANDARD_LINKAGE_LEN];
549
550 if (insns_match_pattern (pc, ppc64_standard_linkage,
551 ppc64_standard_linkage_insn))
52f729a7
UW
552 return ppc64_standard_linkage_target (frame, pc,
553 ppc64_standard_linkage_insn);
f470a70a
JB
554 else
555 return 0;
556}
557
558
2bbe3cc1 559/* Support for convert_from_func_ptr_addr (ARCH, ADDR, TARG) on PPC
e2d0e7eb 560 GNU/Linux.
02631ec0
JB
561
562 Usually a function pointer's representation is simply the address
2bbe3cc1
DJ
563 of the function. On GNU/Linux on the PowerPC however, a function
564 pointer may be a pointer to a function descriptor.
565
566 For PPC64, a function descriptor is a TOC entry, in a data section,
567 which contains three words: the first word is the address of the
568 function, the second word is the TOC pointer (r2), and the third word
569 is the static chain value.
570
571 For PPC32, there are two kinds of function pointers: non-secure and
572 secure. Non-secure function pointers point directly to the
573 function in a code section and thus need no translation. Secure
574 ones (from GCC's -msecure-plt option) are in a data section and
575 contain one word: the address of the function.
576
577 Throughout GDB it is currently assumed that a function pointer contains
578 the address of the function, which is not easy to fix. In addition, the
e538d2d7
JB
579 conversion of a function address to a function pointer would
580 require allocation of a TOC entry in the inferior's memory space,
581 with all its drawbacks. To be able to call C++ virtual methods in
582 the inferior (which are called via function pointers),
583 find_function_addr uses this function to get the function address
2bbe3cc1 584 from a function pointer.
02631ec0 585
2bbe3cc1
DJ
586 If ADDR points at what is clearly a function descriptor, transform
587 it into the address of the corresponding function, if needed. Be
588 conservative, otherwise GDB will do the transformation on any
589 random addresses such as occur when there is no symbol table. */
02631ec0
JB
590
591static CORE_ADDR
2bbe3cc1
DJ
592ppc_linux_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
593 CORE_ADDR addr,
594 struct target_ops *targ)
02631ec0 595{
2bbe3cc1 596 struct gdbarch_tdep *tdep;
b6591e8b 597 struct section_table *s = target_section_by_addr (targ, addr);
2bbe3cc1
DJ
598 char *sect_name = NULL;
599
600 if (!s)
601 return addr;
602
603 tdep = gdbarch_tdep (gdbarch);
604
605 switch (tdep->wordsize)
606 {
607 case 4:
608 sect_name = ".plt";
609 break;
610 case 8:
611 sect_name = ".opd";
612 break;
613 default:
614 internal_error (__FILE__, __LINE__,
615 _("failed internal consistency check"));
616 }
02631ec0 617
9b540880 618 /* Check if ADDR points to a function descriptor. */
2bbe3cc1
DJ
619
620 /* NOTE: this depends on the coincidence that the address of a functions
621 entry point is contained in the first word of its function descriptor
622 for both PPC-64 and for PPC-32 with secure PLTs. */
623 if ((strcmp (s->the_bfd_section->name, sect_name) == 0)
624 && s->the_bfd_section->flags & SEC_DATA)
625 return get_target_memory_unsigned (targ, addr, tdep->wordsize);
9b540880
AC
626
627 return addr;
02631ec0
JB
628}
629
7284e1be
UW
630/* Wrappers to handle Linux-only registers. */
631
632static void
633ppc_linux_supply_gregset (const struct regset *regset,
634 struct regcache *regcache,
635 int regnum, const void *gregs, size_t len)
636{
637 const struct ppc_reg_offsets *offsets = regset->descr;
638
639 ppc_supply_gregset (regset, regcache, regnum, gregs, len);
640
641 if (ppc_linux_trap_reg_p (get_regcache_arch (regcache)))
642 {
643 /* "orig_r3" is stored 2 slots after "pc". */
644 if (regnum == -1 || regnum == PPC_ORIG_R3_REGNUM)
645 ppc_supply_reg (regcache, PPC_ORIG_R3_REGNUM, gregs,
646 offsets->pc_offset + 2 * offsets->gpr_size,
647 offsets->gpr_size);
648
649 /* "trap" is stored 8 slots after "pc". */
650 if (regnum == -1 || regnum == PPC_TRAP_REGNUM)
651 ppc_supply_reg (regcache, PPC_TRAP_REGNUM, gregs,
652 offsets->pc_offset + 8 * offsets->gpr_size,
653 offsets->gpr_size);
654 }
655}
f2db237a 656
f9be684a 657static void
f2db237a
AM
658ppc_linux_collect_gregset (const struct regset *regset,
659 const struct regcache *regcache,
660 int regnum, void *gregs, size_t len)
f9be684a 661{
7284e1be
UW
662 const struct ppc_reg_offsets *offsets = regset->descr;
663
664 /* Clear areas in the linux gregset not written elsewhere. */
f2db237a
AM
665 if (regnum == -1)
666 memset (gregs, 0, len);
7284e1be 667
f2db237a 668 ppc_collect_gregset (regset, regcache, regnum, gregs, len);
7284e1be
UW
669
670 if (ppc_linux_trap_reg_p (get_regcache_arch (regcache)))
671 {
672 /* "orig_r3" is stored 2 slots after "pc". */
673 if (regnum == -1 || regnum == PPC_ORIG_R3_REGNUM)
674 ppc_collect_reg (regcache, PPC_ORIG_R3_REGNUM, gregs,
675 offsets->pc_offset + 2 * offsets->gpr_size,
676 offsets->gpr_size);
677
678 /* "trap" is stored 8 slots after "pc". */
679 if (regnum == -1 || regnum == PPC_TRAP_REGNUM)
680 ppc_collect_reg (regcache, PPC_TRAP_REGNUM, gregs,
681 offsets->pc_offset + 8 * offsets->gpr_size,
682 offsets->gpr_size);
683 }
f9be684a
AC
684}
685
f2db237a
AM
686/* Regset descriptions. */
687static const struct ppc_reg_offsets ppc32_linux_reg_offsets =
688 {
689 /* General-purpose registers. */
690 /* .r0_offset = */ 0,
691 /* .gpr_size = */ 4,
692 /* .xr_size = */ 4,
693 /* .pc_offset = */ 128,
694 /* .ps_offset = */ 132,
695 /* .cr_offset = */ 152,
696 /* .lr_offset = */ 144,
697 /* .ctr_offset = */ 140,
698 /* .xer_offset = */ 148,
699 /* .mq_offset = */ 156,
700
701 /* Floating-point registers. */
702 /* .f0_offset = */ 0,
703 /* .fpscr_offset = */ 256,
704 /* .fpscr_size = */ 8,
705
706 /* AltiVec registers. */
707 /* .vr0_offset = */ 0,
06caf7d2
CES
708 /* .vscr_offset = */ 512 + 12,
709 /* .vrsave_offset = */ 528
f2db237a 710 };
f9be684a 711
f2db237a
AM
712static const struct ppc_reg_offsets ppc64_linux_reg_offsets =
713 {
714 /* General-purpose registers. */
715 /* .r0_offset = */ 0,
716 /* .gpr_size = */ 8,
717 /* .xr_size = */ 8,
718 /* .pc_offset = */ 256,
719 /* .ps_offset = */ 264,
720 /* .cr_offset = */ 304,
721 /* .lr_offset = */ 288,
722 /* .ctr_offset = */ 280,
723 /* .xer_offset = */ 296,
724 /* .mq_offset = */ 312,
725
726 /* Floating-point registers. */
727 /* .f0_offset = */ 0,
728 /* .fpscr_offset = */ 256,
729 /* .fpscr_size = */ 8,
730
731 /* AltiVec registers. */
732 /* .vr0_offset = */ 0,
06caf7d2
CES
733 /* .vscr_offset = */ 512 + 12,
734 /* .vrsave_offset = */ 528
f2db237a 735 };
2fda4977 736
f2db237a
AM
737static const struct regset ppc32_linux_gregset = {
738 &ppc32_linux_reg_offsets,
7284e1be 739 ppc_linux_supply_gregset,
f2db237a
AM
740 ppc_linux_collect_gregset,
741 NULL
f9be684a
AC
742};
743
f2db237a
AM
744static const struct regset ppc64_linux_gregset = {
745 &ppc64_linux_reg_offsets,
7284e1be 746 ppc_linux_supply_gregset,
f2db237a
AM
747 ppc_linux_collect_gregset,
748 NULL
749};
f9be684a 750
f2db237a
AM
751static const struct regset ppc32_linux_fpregset = {
752 &ppc32_linux_reg_offsets,
753 ppc_supply_fpregset,
754 ppc_collect_fpregset,
755 NULL
f9be684a
AC
756};
757
06caf7d2
CES
758static const struct regset ppc32_linux_vrregset = {
759 &ppc32_linux_reg_offsets,
760 ppc_supply_vrregset,
761 ppc_collect_vrregset,
762 NULL
763};
764
f2db237a
AM
765const struct regset *
766ppc_linux_gregset (int wordsize)
2fda4977 767{
f2db237a 768 return wordsize == 8 ? &ppc64_linux_gregset : &ppc32_linux_gregset;
2fda4977
DJ
769}
770
f2db237a
AM
771const struct regset *
772ppc_linux_fpregset (void)
773{
774 return &ppc32_linux_fpregset;
775}
2fda4977 776
f9be684a
AC
777static const struct regset *
778ppc_linux_regset_from_core_section (struct gdbarch *core_arch,
779 const char *sect_name, size_t sect_size)
2fda4977 780{
f9be684a
AC
781 struct gdbarch_tdep *tdep = gdbarch_tdep (core_arch);
782 if (strcmp (sect_name, ".reg") == 0)
2fda4977 783 {
f9be684a
AC
784 if (tdep->wordsize == 4)
785 return &ppc32_linux_gregset;
2fda4977 786 else
f9be684a 787 return &ppc64_linux_gregset;
2fda4977 788 }
f9be684a 789 if (strcmp (sect_name, ".reg2") == 0)
f2db237a 790 return &ppc32_linux_fpregset;
06caf7d2
CES
791 if (strcmp (sect_name, ".reg-ppc-vmx") == 0)
792 return &ppc32_linux_vrregset;
f9be684a 793 return NULL;
2fda4977
DJ
794}
795
a8f60bfc 796static void
5366653e 797ppc_linux_sigtramp_cache (struct frame_info *this_frame,
a8f60bfc
AC
798 struct trad_frame_cache *this_cache,
799 CORE_ADDR func, LONGEST offset,
800 int bias)
801{
802 CORE_ADDR base;
803 CORE_ADDR regs;
804 CORE_ADDR gpregs;
805 CORE_ADDR fpregs;
806 int i;
5366653e 807 struct gdbarch *gdbarch = get_frame_arch (this_frame);
a8f60bfc
AC
808 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
809
5366653e
DJ
810 base = get_frame_register_unsigned (this_frame,
811 gdbarch_sp_regnum (gdbarch));
812 if (bias > 0 && get_frame_pc (this_frame) != func)
a8f60bfc
AC
813 /* See below, some signal trampolines increment the stack as their
814 first instruction, need to compensate for that. */
815 base -= bias;
816
817 /* Find the address of the register buffer pointer. */
818 regs = base + offset;
819 /* Use that to find the address of the corresponding register
820 buffers. */
821 gpregs = read_memory_unsigned_integer (regs, tdep->wordsize);
822 fpregs = gpregs + 48 * tdep->wordsize;
823
824 /* General purpose. */
825 for (i = 0; i < 32; i++)
826 {
827 int regnum = i + tdep->ppc_gp0_regnum;
828 trad_frame_set_reg_addr (this_cache, regnum, gpregs + i * tdep->wordsize);
829 }
3e8c568d 830 trad_frame_set_reg_addr (this_cache,
40a6adc1 831 gdbarch_pc_regnum (gdbarch),
3e8c568d 832 gpregs + 32 * tdep->wordsize);
a8f60bfc
AC
833 trad_frame_set_reg_addr (this_cache, tdep->ppc_ctr_regnum,
834 gpregs + 35 * tdep->wordsize);
835 trad_frame_set_reg_addr (this_cache, tdep->ppc_lr_regnum,
836 gpregs + 36 * tdep->wordsize);
837 trad_frame_set_reg_addr (this_cache, tdep->ppc_xer_regnum,
838 gpregs + 37 * tdep->wordsize);
839 trad_frame_set_reg_addr (this_cache, tdep->ppc_cr_regnum,
840 gpregs + 38 * tdep->wordsize);
841
7284e1be
UW
842 if (ppc_linux_trap_reg_p (gdbarch))
843 {
844 trad_frame_set_reg_addr (this_cache, PPC_ORIG_R3_REGNUM,
845 gpregs + 34 * tdep->wordsize);
846 trad_frame_set_reg_addr (this_cache, PPC_TRAP_REGNUM,
847 gpregs + 40 * tdep->wordsize);
848 }
849
60f140f9
PG
850 if (ppc_floating_point_unit_p (gdbarch))
851 {
852 /* Floating point registers. */
853 for (i = 0; i < 32; i++)
854 {
40a6adc1 855 int regnum = i + gdbarch_fp0_regnum (gdbarch);
60f140f9
PG
856 trad_frame_set_reg_addr (this_cache, regnum,
857 fpregs + i * tdep->wordsize);
858 }
859 trad_frame_set_reg_addr (this_cache, tdep->ppc_fpscr_regnum,
4019046a 860 fpregs + 32 * tdep->wordsize);
60f140f9 861 }
a8f60bfc
AC
862 trad_frame_set_id (this_cache, frame_id_build (base, func));
863}
864
865static void
866ppc32_linux_sigaction_cache_init (const struct tramp_frame *self,
5366653e 867 struct frame_info *this_frame,
a8f60bfc
AC
868 struct trad_frame_cache *this_cache,
869 CORE_ADDR func)
870{
5366653e 871 ppc_linux_sigtramp_cache (this_frame, this_cache, func,
a8f60bfc
AC
872 0xd0 /* Offset to ucontext_t. */
873 + 0x30 /* Offset to .reg. */,
874 0);
875}
876
877static void
878ppc64_linux_sigaction_cache_init (const struct tramp_frame *self,
5366653e 879 struct frame_info *this_frame,
a8f60bfc
AC
880 struct trad_frame_cache *this_cache,
881 CORE_ADDR func)
882{
5366653e 883 ppc_linux_sigtramp_cache (this_frame, this_cache, func,
a8f60bfc
AC
884 0x80 /* Offset to ucontext_t. */
885 + 0xe0 /* Offset to .reg. */,
886 128);
887}
888
889static void
890ppc32_linux_sighandler_cache_init (const struct tramp_frame *self,
5366653e 891 struct frame_info *this_frame,
a8f60bfc
AC
892 struct trad_frame_cache *this_cache,
893 CORE_ADDR func)
894{
5366653e 895 ppc_linux_sigtramp_cache (this_frame, this_cache, func,
a8f60bfc
AC
896 0x40 /* Offset to ucontext_t. */
897 + 0x1c /* Offset to .reg. */,
898 0);
899}
900
901static void
902ppc64_linux_sighandler_cache_init (const struct tramp_frame *self,
5366653e 903 struct frame_info *this_frame,
a8f60bfc
AC
904 struct trad_frame_cache *this_cache,
905 CORE_ADDR func)
906{
5366653e 907 ppc_linux_sigtramp_cache (this_frame, this_cache, func,
a8f60bfc
AC
908 0x80 /* Offset to struct sigcontext. */
909 + 0x38 /* Offset to .reg. */,
910 128);
911}
912
913static struct tramp_frame ppc32_linux_sigaction_tramp_frame = {
914 SIGTRAMP_FRAME,
915 4,
916 {
917 { 0x380000ac, -1 }, /* li r0, 172 */
918 { 0x44000002, -1 }, /* sc */
919 { TRAMP_SENTINEL_INSN },
920 },
921 ppc32_linux_sigaction_cache_init
922};
923static struct tramp_frame ppc64_linux_sigaction_tramp_frame = {
924 SIGTRAMP_FRAME,
925 4,
926 {
927 { 0x38210080, -1 }, /* addi r1,r1,128 */
928 { 0x380000ac, -1 }, /* li r0, 172 */
929 { 0x44000002, -1 }, /* sc */
930 { TRAMP_SENTINEL_INSN },
931 },
932 ppc64_linux_sigaction_cache_init
933};
934static struct tramp_frame ppc32_linux_sighandler_tramp_frame = {
935 SIGTRAMP_FRAME,
936 4,
937 {
938 { 0x38000077, -1 }, /* li r0,119 */
939 { 0x44000002, -1 }, /* sc */
940 { TRAMP_SENTINEL_INSN },
941 },
942 ppc32_linux_sighandler_cache_init
943};
944static struct tramp_frame ppc64_linux_sighandler_tramp_frame = {
945 SIGTRAMP_FRAME,
946 4,
947 {
948 { 0x38210080, -1 }, /* addi r1,r1,128 */
949 { 0x38000077, -1 }, /* li r0,119 */
950 { 0x44000002, -1 }, /* sc */
951 { TRAMP_SENTINEL_INSN },
952 },
953 ppc64_linux_sighandler_cache_init
954};
955
7284e1be
UW
956
957/* Return 1 if PPC_ORIG_R3_REGNUM and PPC_TRAP_REGNUM are usable. */
958int
959ppc_linux_trap_reg_p (struct gdbarch *gdbarch)
960{
961 /* If we do not have a target description with registers, then
962 the special registers will not be included in the register set. */
963 if (!tdesc_has_registers (gdbarch_target_desc (gdbarch)))
964 return 0;
965
966 /* If we do, then it is safe to check the size. */
967 return register_size (gdbarch, PPC_ORIG_R3_REGNUM) > 0
968 && register_size (gdbarch, PPC_TRAP_REGNUM) > 0;
969}
970
971static void
972ppc_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
973{
974 struct gdbarch *gdbarch = get_regcache_arch (regcache);
975
976 regcache_cooked_write_unsigned (regcache, gdbarch_pc_regnum (gdbarch), pc);
977
978 /* Set special TRAP register to -1 to prevent the kernel from
979 messing with the PC we just installed, if we happen to be
980 within an interrupted system call that the kernel wants to
981 restart.
982
983 Note that after we return from the dummy call, the TRAP and
984 ORIG_R3 registers will be automatically restored, and the
985 kernel continues to restart the system call at this point. */
986 if (ppc_linux_trap_reg_p (gdbarch))
987 regcache_cooked_write_unsigned (regcache, PPC_TRAP_REGNUM, -1);
988}
989
990static const struct target_desc *
991ppc_linux_core_read_description (struct gdbarch *gdbarch,
992 struct target_ops *target,
993 bfd *abfd)
994{
995 asection *altivec = bfd_get_section_by_name (abfd, ".reg-ppc-vmx");
996 asection *section = bfd_get_section_by_name (abfd, ".reg");
997 if (! section)
998 return NULL;
999
1000 switch (bfd_section_size (abfd, section))
1001 {
1002 case 48 * 4:
1003 return altivec? tdesc_powerpc_altivec32l : tdesc_powerpc_32l;
1004
1005 case 48 * 8:
1006 return altivec? tdesc_powerpc_altivec64l : tdesc_powerpc_64l;
1007
1008 default:
1009 return NULL;
1010 }
1011}
1012
7b112f9c
JT
1013static void
1014ppc_linux_init_abi (struct gdbarch_info info,
1015 struct gdbarch *gdbarch)
1016{
1017 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7284e1be 1018 struct tdesc_arch_data *tdesc_data = (void *) info.tdep_info;
7b112f9c 1019
b14d30e1
JM
1020 /* PPC GNU/Linux uses either 64-bit or 128-bit long doubles; where
1021 128-bit, they are IBM long double, not IEEE quad long double as
1022 in the System V ABI PowerPC Processor Supplement. We can safely
1023 let them default to 128-bit, since the debug info will give the
1024 size of type actually used in each case. */
1025 set_gdbarch_long_double_bit (gdbarch, 16 * TARGET_CHAR_BIT);
1026 set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);
0598a43c 1027
2bbe3cc1
DJ
1028 /* Handle PPC GNU/Linux 64-bit function pointers (which are really
1029 function descriptors) and 32-bit secure PLT entries. */
1030 set_gdbarch_convert_from_func_ptr_addr
1031 (gdbarch, ppc_linux_convert_from_func_ptr_addr);
1032
7284e1be
UW
1033 /* Handle inferior calls during interrupted system calls. */
1034 set_gdbarch_write_pc (gdbarch, ppc_linux_write_pc);
1035
7b112f9c
JT
1036 if (tdep->wordsize == 4)
1037 {
b9ff3018
AC
1038 /* Until November 2001, gcc did not comply with the 32 bit SysV
1039 R4 ABI requirement that structures less than or equal to 8
1040 bytes should be returned in registers. Instead GCC was using
1041 the the AIX/PowerOpen ABI - everything returned in memory
1042 (well ignoring vectors that is). When this was corrected, it
1043 wasn't fixed for GNU/Linux native platform. Use the
1044 PowerOpen struct convention. */
05580c65 1045 set_gdbarch_return_value (gdbarch, ppc_linux_return_value);
b9ff3018 1046
7b112f9c
JT
1047 set_gdbarch_memory_remove_breakpoint (gdbarch,
1048 ppc_linux_memory_remove_breakpoint);
61a65099 1049
f470a70a 1050 /* Shared library handling. */
f470a70a
JB
1051 set_gdbarch_skip_trampoline_code (gdbarch,
1052 ppc_linux_skip_trampoline_code);
7b112f9c 1053 set_solib_svr4_fetch_link_map_offsets
76a9d10f 1054 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
a8f60bfc
AC
1055
1056 /* Trampolines. */
1057 tramp_frame_prepend_unwinder (gdbarch, &ppc32_linux_sigaction_tramp_frame);
1058 tramp_frame_prepend_unwinder (gdbarch, &ppc32_linux_sighandler_tramp_frame);
7b112f9c 1059 }
f470a70a
JB
1060
1061 if (tdep->wordsize == 8)
1062 {
4a7622d1
UW
1063 /* Handle the 64-bit SVR4 minimal-symbol convention of using "FN"
1064 for the descriptor and ".FN" for the entry-point -- a user
1065 specifying "break FN" will unexpectedly end up with a breakpoint
1066 on the descriptor and not the function. This architecture method
1067 transforms any breakpoints on descriptors into breakpoints on the
1068 corresponding entry point. */
1069 set_gdbarch_adjust_breakpoint_address
1070 (gdbarch, ppc64_sysv_abi_adjust_breakpoint_address);
1071
fb318ff7 1072 /* Shared library handling. */
2bbe3cc1 1073 set_gdbarch_skip_trampoline_code (gdbarch, ppc64_skip_trampoline_code);
fb318ff7
DJ
1074 set_solib_svr4_fetch_link_map_offsets
1075 (gdbarch, svr4_lp64_fetch_link_map_offsets);
1076
a8f60bfc
AC
1077 /* Trampolines. */
1078 tramp_frame_prepend_unwinder (gdbarch, &ppc64_linux_sigaction_tramp_frame);
1079 tramp_frame_prepend_unwinder (gdbarch, &ppc64_linux_sighandler_tramp_frame);
f470a70a 1080 }
f9be684a 1081 set_gdbarch_regset_from_core_section (gdbarch, ppc_linux_regset_from_core_section);
7284e1be 1082 set_gdbarch_core_read_description (gdbarch, ppc_linux_core_read_description);
b2756930
KB
1083
1084 /* Enable TLS support. */
1085 set_gdbarch_fetch_tls_load_module_address (gdbarch,
1086 svr4_fetch_objfile_link_map);
7284e1be
UW
1087
1088 if (tdesc_data)
1089 {
1090 const struct tdesc_feature *feature;
1091
1092 /* If we have target-described registers, then we can safely
1093 reserve a number for PPC_ORIG_R3_REGNUM and PPC_TRAP_REGNUM
1094 (whether they are described or not). */
1095 gdb_assert (gdbarch_num_regs (gdbarch) <= PPC_ORIG_R3_REGNUM);
1096 set_gdbarch_num_regs (gdbarch, PPC_TRAP_REGNUM + 1);
1097
1098 /* If they are present, then assign them to the reserved number. */
1099 feature = tdesc_find_feature (info.target_desc,
1100 "org.gnu.gdb.power.linux");
1101 if (feature != NULL)
1102 {
1103 tdesc_numbered_register (feature, tdesc_data,
1104 PPC_ORIG_R3_REGNUM, "orig_r3");
1105 tdesc_numbered_register (feature, tdesc_data,
1106 PPC_TRAP_REGNUM, "trap");
1107 }
1108 }
7b112f9c
JT
1109}
1110
1111void
1112_initialize_ppc_linux_tdep (void)
1113{
0a0a4ac3
AC
1114 /* Register for all sub-familes of the POWER/PowerPC: 32-bit and
1115 64-bit PowerPC, and the older rs6k. */
1116 gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc, GDB_OSABI_LINUX,
1117 ppc_linux_init_abi);
1118 gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc64, GDB_OSABI_LINUX,
1119 ppc_linux_init_abi);
1120 gdbarch_register_osabi (bfd_arch_rs6000, bfd_mach_rs6k, GDB_OSABI_LINUX,
1121 ppc_linux_init_abi);
7284e1be
UW
1122
1123 /* Initialize the Linux target descriptions. */
1124 initialize_tdesc_powerpc_32l ();
1125 initialize_tdesc_powerpc_altivec32l ();
1126 initialize_tdesc_powerpc_64l ();
1127 initialize_tdesc_powerpc_altivec64l ();
1128 initialize_tdesc_powerpc_e500l ();
7b112f9c 1129}