* ppc-linux-tdep.c: Include "solib.h", "solist.h", "exceptions.h",
[deliverable/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, 2008, 2009
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 3 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, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "frame.h"
24 #include "inferior.h"
25 #include "symtab.h"
26 #include "target.h"
27 #include "gdbcore.h"
28 #include "gdbcmd.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "regcache.h"
32 #include "value.h"
33 #include "osabi.h"
34 #include "regset.h"
35 #include "solib-svr4.h"
36 #include "solib-spu.h"
37 #include "solib.h"
38 #include "solist.h"
39 #include "ppc-tdep.h"
40 #include "ppc-linux-tdep.h"
41 #include "trad-frame.h"
42 #include "frame-unwind.h"
43 #include "tramp-frame.h"
44 #include "observer.h"
45 #include "auxv.h"
46 #include "elf/common.h"
47 #include "exceptions.h"
48 #include "arch-utils.h"
49 #include "spu-tdep.h"
50
51 #include "features/rs6000/powerpc-32l.c"
52 #include "features/rs6000/powerpc-altivec32l.c"
53 #include "features/rs6000/powerpc-cell32l.c"
54 #include "features/rs6000/powerpc-vsx32l.c"
55 #include "features/rs6000/powerpc-isa205-32l.c"
56 #include "features/rs6000/powerpc-isa205-altivec32l.c"
57 #include "features/rs6000/powerpc-isa205-vsx32l.c"
58 #include "features/rs6000/powerpc-64l.c"
59 #include "features/rs6000/powerpc-altivec64l.c"
60 #include "features/rs6000/powerpc-cell64l.c"
61 #include "features/rs6000/powerpc-vsx64l.c"
62 #include "features/rs6000/powerpc-isa205-64l.c"
63 #include "features/rs6000/powerpc-isa205-altivec64l.c"
64 #include "features/rs6000/powerpc-isa205-vsx64l.c"
65 #include "features/rs6000/powerpc-e500l.c"
66
67
68 /* ppc_linux_memory_remove_breakpoints attempts to remove a breakpoint
69 in much the same fashion as memory_remove_breakpoint in mem-break.c,
70 but is careful not to write back the previous contents if the code
71 in question has changed in between inserting the breakpoint and
72 removing it.
73
74 Here is the problem that we're trying to solve...
75
76 Once upon a time, before introducing this function to remove
77 breakpoints from the inferior, setting a breakpoint on a shared
78 library function prior to running the program would not work
79 properly. In order to understand the problem, it is first
80 necessary to understand a little bit about dynamic linking on
81 this platform.
82
83 A call to a shared library function is accomplished via a bl
84 (branch-and-link) instruction whose branch target is an entry
85 in the procedure linkage table (PLT). The PLT in the object
86 file is uninitialized. To gdb, prior to running the program, the
87 entries in the PLT are all zeros.
88
89 Once the program starts running, the shared libraries are loaded
90 and the procedure linkage table is initialized, but the entries in
91 the table are not (necessarily) resolved. Once a function is
92 actually called, the code in the PLT is hit and the function is
93 resolved. In order to better illustrate this, an example is in
94 order; the following example is from the gdb testsuite.
95
96 We start the program shmain.
97
98 [kev@arroyo testsuite]$ ../gdb gdb.base/shmain
99 [...]
100
101 We place two breakpoints, one on shr1 and the other on main.
102
103 (gdb) b shr1
104 Breakpoint 1 at 0x100409d4
105 (gdb) b main
106 Breakpoint 2 at 0x100006a0: file gdb.base/shmain.c, line 44.
107
108 Examine the instruction (and the immediatly following instruction)
109 upon which the breakpoint was placed. Note that the PLT entry
110 for shr1 contains zeros.
111
112 (gdb) x/2i 0x100409d4
113 0x100409d4 <shr1>: .long 0x0
114 0x100409d8 <shr1+4>: .long 0x0
115
116 Now run 'til main.
117
118 (gdb) r
119 Starting program: gdb.base/shmain
120 Breakpoint 1 at 0xffaf790: file gdb.base/shr1.c, line 19.
121
122 Breakpoint 2, main ()
123 at gdb.base/shmain.c:44
124 44 g = 1;
125
126 Examine the PLT again. Note that the loading of the shared
127 library has initialized the PLT to code which loads a constant
128 (which I think is an index into the GOT) into r11 and then
129 branchs a short distance to the code which actually does the
130 resolving.
131
132 (gdb) x/2i 0x100409d4
133 0x100409d4 <shr1>: li r11,4
134 0x100409d8 <shr1+4>: b 0x10040984 <sg+4>
135 (gdb) c
136 Continuing.
137
138 Breakpoint 1, shr1 (x=1)
139 at gdb.base/shr1.c:19
140 19 l = 1;
141
142 Now we've hit the breakpoint at shr1. (The breakpoint was
143 reset from the PLT entry to the actual shr1 function after the
144 shared library was loaded.) Note that the PLT entry has been
145 resolved to contain a branch that takes us directly to shr1.
146 (The real one, not the PLT entry.)
147
148 (gdb) x/2i 0x100409d4
149 0x100409d4 <shr1>: b 0xffaf76c <shr1>
150 0x100409d8 <shr1+4>: b 0x10040984 <sg+4>
151
152 The thing to note here is that the PLT entry for shr1 has been
153 changed twice.
154
155 Now the problem should be obvious. GDB places a breakpoint (a
156 trap instruction) on the zero value of the PLT entry for shr1.
157 Later on, after the shared library had been loaded and the PLT
158 initialized, GDB gets a signal indicating this fact and attempts
159 (as it always does when it stops) to remove all the breakpoints.
160
161 The breakpoint removal was causing the former contents (a zero
162 word) to be written back to the now initialized PLT entry thus
163 destroying a portion of the initialization that had occurred only a
164 short time ago. When execution continued, the zero word would be
165 executed as an instruction an an illegal instruction trap was
166 generated instead. (0 is not a legal instruction.)
167
168 The fix for this problem was fairly straightforward. The function
169 memory_remove_breakpoint from mem-break.c was copied to this file,
170 modified slightly, and renamed to ppc_linux_memory_remove_breakpoint.
171 In tm-linux.h, MEMORY_REMOVE_BREAKPOINT is defined to call this new
172 function.
173
174 The differences between ppc_linux_memory_remove_breakpoint () and
175 memory_remove_breakpoint () are minor. All that the former does
176 that the latter does not is check to make sure that the breakpoint
177 location actually contains a breakpoint (trap instruction) prior
178 to attempting to write back the old contents. If it does contain
179 a trap instruction, we allow the old contents to be written back.
180 Otherwise, we silently do nothing.
181
182 The big question is whether memory_remove_breakpoint () should be
183 changed to have the same functionality. The downside is that more
184 traffic is generated for remote targets since we'll have an extra
185 fetch of a memory word each time a breakpoint is removed.
186
187 For the time being, we'll leave this self-modifying-code-friendly
188 version in ppc-linux-tdep.c, but it ought to be migrated somewhere
189 else in the event that some other platform has similar needs with
190 regard to removing breakpoints in some potentially self modifying
191 code. */
192 static int
193 ppc_linux_memory_remove_breakpoint (struct gdbarch *gdbarch,
194 struct bp_target_info *bp_tgt)
195 {
196 CORE_ADDR addr = bp_tgt->placed_address;
197 const unsigned char *bp;
198 int val;
199 int bplen;
200 gdb_byte old_contents[BREAKPOINT_MAX];
201 struct cleanup *cleanup;
202
203 /* Determine appropriate breakpoint contents and size for this address. */
204 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen);
205 if (bp == NULL)
206 error (_("Software breakpoints not implemented for this target."));
207
208 /* Make sure we see the memory breakpoints. */
209 cleanup = make_show_memory_breakpoints_cleanup (1);
210 val = target_read_memory (addr, old_contents, bplen);
211
212 /* If our breakpoint is no longer at the address, this means that the
213 program modified the code on us, so it is wrong to put back the
214 old value */
215 if (val == 0 && memcmp (bp, old_contents, bplen) == 0)
216 val = target_write_memory (addr, bp_tgt->shadow_contents, bplen);
217
218 do_cleanups (cleanup);
219 return val;
220 }
221
222 /* For historic reasons, PPC 32 GNU/Linux follows PowerOpen rather
223 than the 32 bit SYSV R4 ABI structure return convention - all
224 structures, no matter their size, are put in memory. Vectors,
225 which were added later, do get returned in a register though. */
226
227 static enum return_value_convention
228 ppc_linux_return_value (struct gdbarch *gdbarch, struct type *func_type,
229 struct type *valtype, struct regcache *regcache,
230 gdb_byte *readbuf, const gdb_byte *writebuf)
231 {
232 if ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
233 || TYPE_CODE (valtype) == TYPE_CODE_UNION)
234 && !((TYPE_LENGTH (valtype) == 16 || TYPE_LENGTH (valtype) == 8)
235 && TYPE_VECTOR (valtype)))
236 return RETURN_VALUE_STRUCT_CONVENTION;
237 else
238 return ppc_sysv_abi_return_value (gdbarch, func_type, valtype, regcache,
239 readbuf, writebuf);
240 }
241
242 /* Macros for matching instructions. Note that, since all the
243 operands are masked off before they're or-ed into the instruction,
244 you can use -1 to make masks. */
245
246 #define insn_d(opcd, rts, ra, d) \
247 ((((opcd) & 0x3f) << 26) \
248 | (((rts) & 0x1f) << 21) \
249 | (((ra) & 0x1f) << 16) \
250 | ((d) & 0xffff))
251
252 #define insn_ds(opcd, rts, ra, d, xo) \
253 ((((opcd) & 0x3f) << 26) \
254 | (((rts) & 0x1f) << 21) \
255 | (((ra) & 0x1f) << 16) \
256 | ((d) & 0xfffc) \
257 | ((xo) & 0x3))
258
259 #define insn_xfx(opcd, rts, spr, xo) \
260 ((((opcd) & 0x3f) << 26) \
261 | (((rts) & 0x1f) << 21) \
262 | (((spr) & 0x1f) << 16) \
263 | (((spr) & 0x3e0) << 6) \
264 | (((xo) & 0x3ff) << 1))
265
266 /* Read a PPC instruction from memory. PPC instructions are always
267 big-endian, no matter what endianness the program is running in, so
268 we can't use read_memory_integer or one of its friends here. */
269 static unsigned int
270 read_insn (CORE_ADDR pc)
271 {
272 unsigned char buf[4];
273
274 read_memory (pc, buf, 4);
275 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
276 }
277
278
279 /* An instruction to match. */
280 struct insn_pattern
281 {
282 unsigned int mask; /* mask the insn with this... */
283 unsigned int data; /* ...and see if it matches this. */
284 int optional; /* If non-zero, this insn may be absent. */
285 };
286
287 /* Return non-zero if the instructions at PC match the series
288 described in PATTERN, or zero otherwise. PATTERN is an array of
289 'struct insn_pattern' objects, terminated by an entry whose mask is
290 zero.
291
292 When the match is successful, fill INSN[i] with what PATTERN[i]
293 matched. If PATTERN[i] is optional, and the instruction wasn't
294 present, set INSN[i] to 0 (which is not a valid PPC instruction).
295 INSN should have as many elements as PATTERN. Note that, if
296 PATTERN contains optional instructions which aren't present in
297 memory, then INSN will have holes, so INSN[i] isn't necessarily the
298 i'th instruction in memory. */
299 static int
300 insns_match_pattern (CORE_ADDR pc,
301 struct insn_pattern *pattern,
302 unsigned int *insn)
303 {
304 int i;
305
306 for (i = 0; pattern[i].mask; i++)
307 {
308 insn[i] = read_insn (pc);
309 if ((insn[i] & pattern[i].mask) == pattern[i].data)
310 pc += 4;
311 else if (pattern[i].optional)
312 insn[i] = 0;
313 else
314 return 0;
315 }
316
317 return 1;
318 }
319
320
321 /* Return the 'd' field of the d-form instruction INSN, properly
322 sign-extended. */
323 static CORE_ADDR
324 insn_d_field (unsigned int insn)
325 {
326 return ((((CORE_ADDR) insn & 0xffff) ^ 0x8000) - 0x8000);
327 }
328
329
330 /* Return the 'ds' field of the ds-form instruction INSN, with the two
331 zero bits concatenated at the right, and properly
332 sign-extended. */
333 static CORE_ADDR
334 insn_ds_field (unsigned int insn)
335 {
336 return ((((CORE_ADDR) insn & 0xfffc) ^ 0x8000) - 0x8000);
337 }
338
339
340 /* If DESC is the address of a 64-bit PowerPC GNU/Linux function
341 descriptor, return the descriptor's entry point. */
342 static CORE_ADDR
343 ppc64_desc_entry_point (struct gdbarch *gdbarch, CORE_ADDR desc)
344 {
345 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
346 /* The first word of the descriptor is the entry point. */
347 return (CORE_ADDR) read_memory_unsigned_integer (desc, 8, byte_order);
348 }
349
350
351 /* Pattern for the standard linkage function. These are built by
352 build_plt_stub in elf64-ppc.c, whose GLINK argument is always
353 zero. */
354 static struct insn_pattern ppc64_standard_linkage1[] =
355 {
356 /* addis r12, r2, <any> */
357 { insn_d (-1, -1, -1, 0), insn_d (15, 12, 2, 0), 0 },
358
359 /* std r2, 40(r1) */
360 { -1, insn_ds (62, 2, 1, 40, 0), 0 },
361
362 /* ld r11, <any>(r12) */
363 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 },
364
365 /* addis r12, r12, 1 <optional> */
366 { insn_d (-1, -1, -1, -1), insn_d (15, 12, 12, 1), 1 },
367
368 /* ld r2, <any>(r12) */
369 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 2, 12, 0, 0), 0 },
370
371 /* addis r12, r12, 1 <optional> */
372 { insn_d (-1, -1, -1, -1), insn_d (15, 12, 12, 1), 1 },
373
374 /* mtctr r11 */
375 { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 11, 9, 467), 0 },
376
377 /* ld r11, <any>(r12) */
378 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 },
379
380 /* bctr */
381 { -1, 0x4e800420, 0 },
382
383 { 0, 0, 0 }
384 };
385 #define PPC64_STANDARD_LINKAGE1_LEN \
386 (sizeof (ppc64_standard_linkage1) / sizeof (ppc64_standard_linkage1[0]))
387
388 static struct insn_pattern ppc64_standard_linkage2[] =
389 {
390 /* addis r12, r2, <any> */
391 { insn_d (-1, -1, -1, 0), insn_d (15, 12, 2, 0), 0 },
392
393 /* std r2, 40(r1) */
394 { -1, insn_ds (62, 2, 1, 40, 0), 0 },
395
396 /* ld r11, <any>(r12) */
397 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 },
398
399 /* addi r12, r12, <any> <optional> */
400 { insn_d (-1, -1, -1, 0), insn_d (14, 12, 12, 0), 1 },
401
402 /* mtctr r11 */
403 { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 11, 9, 467), 0 },
404
405 /* ld r2, <any>(r12) */
406 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 2, 12, 0, 0), 0 },
407
408 /* ld r11, <any>(r12) */
409 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 },
410
411 /* bctr */
412 { -1, 0x4e800420, 0 },
413
414 { 0, 0, 0 }
415 };
416 #define PPC64_STANDARD_LINKAGE2_LEN \
417 (sizeof (ppc64_standard_linkage2) / sizeof (ppc64_standard_linkage2[0]))
418
419 static struct insn_pattern ppc64_standard_linkage3[] =
420 {
421 /* std r2, 40(r1) */
422 { -1, insn_ds (62, 2, 1, 40, 0), 0 },
423
424 /* ld r11, <any>(r2) */
425 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 2, 0, 0), 0 },
426
427 /* addi r2, r2, <any> <optional> */
428 { insn_d (-1, -1, -1, 0), insn_d (14, 2, 2, 0), 1 },
429
430 /* mtctr r11 */
431 { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 11, 9, 467), 0 },
432
433 /* ld r11, <any>(r2) */
434 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 2, 0, 0), 0 },
435
436 /* ld r2, <any>(r2) */
437 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 2, 2, 0, 0), 0 },
438
439 /* bctr */
440 { -1, 0x4e800420, 0 },
441
442 { 0, 0, 0 }
443 };
444 #define PPC64_STANDARD_LINKAGE3_LEN \
445 (sizeof (ppc64_standard_linkage3) / sizeof (ppc64_standard_linkage3[0]))
446
447
448 /* When the dynamic linker is doing lazy symbol resolution, the first
449 call to a function in another object will go like this:
450
451 - The user's function calls the linkage function:
452
453 100007c4: 4b ff fc d5 bl 10000498
454 100007c8: e8 41 00 28 ld r2,40(r1)
455
456 - The linkage function loads the entry point (and other stuff) from
457 the function descriptor in the PLT, and jumps to it:
458
459 10000498: 3d 82 00 00 addis r12,r2,0
460 1000049c: f8 41 00 28 std r2,40(r1)
461 100004a0: e9 6c 80 98 ld r11,-32616(r12)
462 100004a4: e8 4c 80 a0 ld r2,-32608(r12)
463 100004a8: 7d 69 03 a6 mtctr r11
464 100004ac: e9 6c 80 a8 ld r11,-32600(r12)
465 100004b0: 4e 80 04 20 bctr
466
467 - But since this is the first time that PLT entry has been used, it
468 sends control to its glink entry. That loads the number of the
469 PLT entry and jumps to the common glink0 code:
470
471 10000c98: 38 00 00 00 li r0,0
472 10000c9c: 4b ff ff dc b 10000c78
473
474 - The common glink0 code then transfers control to the dynamic
475 linker's fixup code:
476
477 10000c78: e8 41 00 28 ld r2,40(r1)
478 10000c7c: 3d 82 00 00 addis r12,r2,0
479 10000c80: e9 6c 80 80 ld r11,-32640(r12)
480 10000c84: e8 4c 80 88 ld r2,-32632(r12)
481 10000c88: 7d 69 03 a6 mtctr r11
482 10000c8c: e9 6c 80 90 ld r11,-32624(r12)
483 10000c90: 4e 80 04 20 bctr
484
485 Eventually, this code will figure out how to skip all of this,
486 including the dynamic linker. At the moment, we just get through
487 the linkage function. */
488
489 /* If the current thread is about to execute a series of instructions
490 at PC matching the ppc64_standard_linkage pattern, and INSN is the result
491 from that pattern match, return the code address to which the
492 standard linkage function will send them. (This doesn't deal with
493 dynamic linker lazy symbol resolution stubs.) */
494 static CORE_ADDR
495 ppc64_standard_linkage1_target (struct frame_info *frame,
496 CORE_ADDR pc, unsigned int *insn)
497 {
498 struct gdbarch *gdbarch = get_frame_arch (frame);
499 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
500
501 /* The address of the function descriptor this linkage function
502 references. */
503 CORE_ADDR desc
504 = ((CORE_ADDR) get_frame_register_unsigned (frame,
505 tdep->ppc_gp0_regnum + 2)
506 + (insn_d_field (insn[0]) << 16)
507 + insn_ds_field (insn[2]));
508
509 /* The first word of the descriptor is the entry point. Return that. */
510 return ppc64_desc_entry_point (gdbarch, desc);
511 }
512
513 static struct core_regset_section ppc_linux_vsx_regset_sections[] =
514 {
515 { ".reg", 268 },
516 { ".reg2", 264 },
517 { ".reg-ppc-vmx", 544 },
518 { ".reg-ppc-vsx", 256 },
519 { NULL, 0}
520 };
521
522 static struct core_regset_section ppc_linux_vmx_regset_sections[] =
523 {
524 { ".reg", 268 },
525 { ".reg2", 264 },
526 { ".reg-ppc-vmx", 544 },
527 { NULL, 0}
528 };
529
530 static struct core_regset_section ppc_linux_fp_regset_sections[] =
531 {
532 { ".reg", 268 },
533 { ".reg2", 264 },
534 { NULL, 0}
535 };
536
537 static CORE_ADDR
538 ppc64_standard_linkage2_target (struct frame_info *frame,
539 CORE_ADDR pc, unsigned int *insn)
540 {
541 struct gdbarch *gdbarch = get_frame_arch (frame);
542 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
543
544 /* The address of the function descriptor this linkage function
545 references. */
546 CORE_ADDR desc
547 = ((CORE_ADDR) get_frame_register_unsigned (frame,
548 tdep->ppc_gp0_regnum + 2)
549 + (insn_d_field (insn[0]) << 16)
550 + insn_ds_field (insn[2]));
551
552 /* The first word of the descriptor is the entry point. Return that. */
553 return ppc64_desc_entry_point (gdbarch, desc);
554 }
555
556 static CORE_ADDR
557 ppc64_standard_linkage3_target (struct frame_info *frame,
558 CORE_ADDR pc, unsigned int *insn)
559 {
560 struct gdbarch *gdbarch = get_frame_arch (frame);
561 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
562
563 /* The address of the function descriptor this linkage function
564 references. */
565 CORE_ADDR desc
566 = ((CORE_ADDR) get_frame_register_unsigned (frame,
567 tdep->ppc_gp0_regnum + 2)
568 + insn_ds_field (insn[1]));
569
570 /* The first word of the descriptor is the entry point. Return that. */
571 return ppc64_desc_entry_point (gdbarch, desc);
572 }
573
574
575 /* Given that we've begun executing a call trampoline at PC, return
576 the entry point of the function the trampoline will go to. */
577 static CORE_ADDR
578 ppc64_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
579 {
580 unsigned int ppc64_standard_linkage1_insn[PPC64_STANDARD_LINKAGE1_LEN];
581 unsigned int ppc64_standard_linkage2_insn[PPC64_STANDARD_LINKAGE2_LEN];
582 unsigned int ppc64_standard_linkage3_insn[PPC64_STANDARD_LINKAGE3_LEN];
583 CORE_ADDR target;
584
585 if (insns_match_pattern (pc, ppc64_standard_linkage1,
586 ppc64_standard_linkage1_insn))
587 pc = ppc64_standard_linkage1_target (frame, pc,
588 ppc64_standard_linkage1_insn);
589 else if (insns_match_pattern (pc, ppc64_standard_linkage2,
590 ppc64_standard_linkage2_insn))
591 pc = ppc64_standard_linkage2_target (frame, pc,
592 ppc64_standard_linkage2_insn);
593 else if (insns_match_pattern (pc, ppc64_standard_linkage3,
594 ppc64_standard_linkage3_insn))
595 pc = ppc64_standard_linkage3_target (frame, pc,
596 ppc64_standard_linkage3_insn);
597 else
598 return 0;
599
600 /* The PLT descriptor will either point to the already resolved target
601 address, or else to a glink stub. As the latter carry synthetic @plt
602 symbols, find_solib_trampoline_target should be able to resolve them. */
603 target = find_solib_trampoline_target (frame, pc);
604 return target? target : pc;
605 }
606
607
608 /* Support for convert_from_func_ptr_addr (ARCH, ADDR, TARG) on PPC64
609 GNU/Linux.
610
611 Usually a function pointer's representation is simply the address
612 of the function. On GNU/Linux on the PowerPC however, a function
613 pointer may be a pointer to a function descriptor.
614
615 For PPC64, a function descriptor is a TOC entry, in a data section,
616 which contains three words: the first word is the address of the
617 function, the second word is the TOC pointer (r2), and the third word
618 is the static chain value.
619
620 Throughout GDB it is currently assumed that a function pointer contains
621 the address of the function, which is not easy to fix. In addition, the
622 conversion of a function address to a function pointer would
623 require allocation of a TOC entry in the inferior's memory space,
624 with all its drawbacks. To be able to call C++ virtual methods in
625 the inferior (which are called via function pointers),
626 find_function_addr uses this function to get the function address
627 from a function pointer.
628
629 If ADDR points at what is clearly a function descriptor, transform
630 it into the address of the corresponding function, if needed. Be
631 conservative, otherwise GDB will do the transformation on any
632 random addresses such as occur when there is no symbol table. */
633
634 static CORE_ADDR
635 ppc64_linux_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
636 CORE_ADDR addr,
637 struct target_ops *targ)
638 {
639 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
640 struct target_section *s = target_section_by_addr (targ, addr);
641
642 /* Check if ADDR points to a function descriptor. */
643 if (s && strcmp (s->the_bfd_section->name, ".opd") == 0)
644 {
645 /* There may be relocations that need to be applied to the .opd
646 section. Unfortunately, this function may be called at a time
647 where these relocations have not yet been performed -- this can
648 happen for example shortly after a library has been loaded with
649 dlopen, but ld.so has not yet applied the relocations.
650
651 To cope with both the case where the relocation has been applied,
652 and the case where it has not yet been applied, we do *not* read
653 the (maybe) relocated value from target memory, but we instead
654 read the non-relocated value from the BFD, and apply the relocation
655 offset manually.
656
657 This makes the assumption that all .opd entries are always relocated
658 by the same offset the section itself was relocated. This should
659 always be the case for GNU/Linux executables and shared libraries.
660 Note that other kind of object files (e.g. those added via
661 add-symbol-files) will currently never end up here anyway, as this
662 function accesses *target* sections only; only the main exec and
663 shared libraries are ever added to the target. */
664
665 gdb_byte buf[8];
666 int res;
667
668 res = bfd_get_section_contents (s->bfd, s->the_bfd_section,
669 &buf, addr - s->addr, 8);
670 if (res != 0)
671 return extract_unsigned_integer (buf, 8, byte_order)
672 - bfd_section_vma (s->bfd, s->the_bfd_section) + s->addr;
673 }
674
675 return addr;
676 }
677
678 /* Wrappers to handle Linux-only registers. */
679
680 static void
681 ppc_linux_supply_gregset (const struct regset *regset,
682 struct regcache *regcache,
683 int regnum, const void *gregs, size_t len)
684 {
685 const struct ppc_reg_offsets *offsets = regset->descr;
686
687 ppc_supply_gregset (regset, regcache, regnum, gregs, len);
688
689 if (ppc_linux_trap_reg_p (get_regcache_arch (regcache)))
690 {
691 /* "orig_r3" is stored 2 slots after "pc". */
692 if (regnum == -1 || regnum == PPC_ORIG_R3_REGNUM)
693 ppc_supply_reg (regcache, PPC_ORIG_R3_REGNUM, gregs,
694 offsets->pc_offset + 2 * offsets->gpr_size,
695 offsets->gpr_size);
696
697 /* "trap" is stored 8 slots after "pc". */
698 if (regnum == -1 || regnum == PPC_TRAP_REGNUM)
699 ppc_supply_reg (regcache, PPC_TRAP_REGNUM, gregs,
700 offsets->pc_offset + 8 * offsets->gpr_size,
701 offsets->gpr_size);
702 }
703 }
704
705 static void
706 ppc_linux_collect_gregset (const struct regset *regset,
707 const struct regcache *regcache,
708 int regnum, void *gregs, size_t len)
709 {
710 const struct ppc_reg_offsets *offsets = regset->descr;
711
712 /* Clear areas in the linux gregset not written elsewhere. */
713 if (regnum == -1)
714 memset (gregs, 0, len);
715
716 ppc_collect_gregset (regset, regcache, regnum, gregs, len);
717
718 if (ppc_linux_trap_reg_p (get_regcache_arch (regcache)))
719 {
720 /* "orig_r3" is stored 2 slots after "pc". */
721 if (regnum == -1 || regnum == PPC_ORIG_R3_REGNUM)
722 ppc_collect_reg (regcache, PPC_ORIG_R3_REGNUM, gregs,
723 offsets->pc_offset + 2 * offsets->gpr_size,
724 offsets->gpr_size);
725
726 /* "trap" is stored 8 slots after "pc". */
727 if (regnum == -1 || regnum == PPC_TRAP_REGNUM)
728 ppc_collect_reg (regcache, PPC_TRAP_REGNUM, gregs,
729 offsets->pc_offset + 8 * offsets->gpr_size,
730 offsets->gpr_size);
731 }
732 }
733
734 /* Regset descriptions. */
735 static const struct ppc_reg_offsets ppc32_linux_reg_offsets =
736 {
737 /* General-purpose registers. */
738 /* .r0_offset = */ 0,
739 /* .gpr_size = */ 4,
740 /* .xr_size = */ 4,
741 /* .pc_offset = */ 128,
742 /* .ps_offset = */ 132,
743 /* .cr_offset = */ 152,
744 /* .lr_offset = */ 144,
745 /* .ctr_offset = */ 140,
746 /* .xer_offset = */ 148,
747 /* .mq_offset = */ 156,
748
749 /* Floating-point registers. */
750 /* .f0_offset = */ 0,
751 /* .fpscr_offset = */ 256,
752 /* .fpscr_size = */ 8,
753
754 /* AltiVec registers. */
755 /* .vr0_offset = */ 0,
756 /* .vscr_offset = */ 512 + 12,
757 /* .vrsave_offset = */ 528
758 };
759
760 static const struct ppc_reg_offsets ppc64_linux_reg_offsets =
761 {
762 /* General-purpose registers. */
763 /* .r0_offset = */ 0,
764 /* .gpr_size = */ 8,
765 /* .xr_size = */ 8,
766 /* .pc_offset = */ 256,
767 /* .ps_offset = */ 264,
768 /* .cr_offset = */ 304,
769 /* .lr_offset = */ 288,
770 /* .ctr_offset = */ 280,
771 /* .xer_offset = */ 296,
772 /* .mq_offset = */ 312,
773
774 /* Floating-point registers. */
775 /* .f0_offset = */ 0,
776 /* .fpscr_offset = */ 256,
777 /* .fpscr_size = */ 8,
778
779 /* AltiVec registers. */
780 /* .vr0_offset = */ 0,
781 /* .vscr_offset = */ 512 + 12,
782 /* .vrsave_offset = */ 528
783 };
784
785 static const struct regset ppc32_linux_gregset = {
786 &ppc32_linux_reg_offsets,
787 ppc_linux_supply_gregset,
788 ppc_linux_collect_gregset,
789 NULL
790 };
791
792 static const struct regset ppc64_linux_gregset = {
793 &ppc64_linux_reg_offsets,
794 ppc_linux_supply_gregset,
795 ppc_linux_collect_gregset,
796 NULL
797 };
798
799 static const struct regset ppc32_linux_fpregset = {
800 &ppc32_linux_reg_offsets,
801 ppc_supply_fpregset,
802 ppc_collect_fpregset,
803 NULL
804 };
805
806 static const struct regset ppc32_linux_vrregset = {
807 &ppc32_linux_reg_offsets,
808 ppc_supply_vrregset,
809 ppc_collect_vrregset,
810 NULL
811 };
812
813 static const struct regset ppc32_linux_vsxregset = {
814 &ppc32_linux_reg_offsets,
815 ppc_supply_vsxregset,
816 ppc_collect_vsxregset,
817 NULL
818 };
819
820 const struct regset *
821 ppc_linux_gregset (int wordsize)
822 {
823 return wordsize == 8 ? &ppc64_linux_gregset : &ppc32_linux_gregset;
824 }
825
826 const struct regset *
827 ppc_linux_fpregset (void)
828 {
829 return &ppc32_linux_fpregset;
830 }
831
832 static const struct regset *
833 ppc_linux_regset_from_core_section (struct gdbarch *core_arch,
834 const char *sect_name, size_t sect_size)
835 {
836 struct gdbarch_tdep *tdep = gdbarch_tdep (core_arch);
837 if (strcmp (sect_name, ".reg") == 0)
838 {
839 if (tdep->wordsize == 4)
840 return &ppc32_linux_gregset;
841 else
842 return &ppc64_linux_gregset;
843 }
844 if (strcmp (sect_name, ".reg2") == 0)
845 return &ppc32_linux_fpregset;
846 if (strcmp (sect_name, ".reg-ppc-vmx") == 0)
847 return &ppc32_linux_vrregset;
848 if (strcmp (sect_name, ".reg-ppc-vsx") == 0)
849 return &ppc32_linux_vsxregset;
850 return NULL;
851 }
852
853 static void
854 ppc_linux_sigtramp_cache (struct frame_info *this_frame,
855 struct trad_frame_cache *this_cache,
856 CORE_ADDR func, LONGEST offset,
857 int bias)
858 {
859 CORE_ADDR base;
860 CORE_ADDR regs;
861 CORE_ADDR gpregs;
862 CORE_ADDR fpregs;
863 int i;
864 struct gdbarch *gdbarch = get_frame_arch (this_frame);
865 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
866 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
867
868 base = get_frame_register_unsigned (this_frame,
869 gdbarch_sp_regnum (gdbarch));
870 if (bias > 0 && get_frame_pc (this_frame) != func)
871 /* See below, some signal trampolines increment the stack as their
872 first instruction, need to compensate for that. */
873 base -= bias;
874
875 /* Find the address of the register buffer pointer. */
876 regs = base + offset;
877 /* Use that to find the address of the corresponding register
878 buffers. */
879 gpregs = read_memory_unsigned_integer (regs, tdep->wordsize, byte_order);
880 fpregs = gpregs + 48 * tdep->wordsize;
881
882 /* General purpose. */
883 for (i = 0; i < 32; i++)
884 {
885 int regnum = i + tdep->ppc_gp0_regnum;
886 trad_frame_set_reg_addr (this_cache, regnum, gpregs + i * tdep->wordsize);
887 }
888 trad_frame_set_reg_addr (this_cache,
889 gdbarch_pc_regnum (gdbarch),
890 gpregs + 32 * tdep->wordsize);
891 trad_frame_set_reg_addr (this_cache, tdep->ppc_ctr_regnum,
892 gpregs + 35 * tdep->wordsize);
893 trad_frame_set_reg_addr (this_cache, tdep->ppc_lr_regnum,
894 gpregs + 36 * tdep->wordsize);
895 trad_frame_set_reg_addr (this_cache, tdep->ppc_xer_regnum,
896 gpregs + 37 * tdep->wordsize);
897 trad_frame_set_reg_addr (this_cache, tdep->ppc_cr_regnum,
898 gpregs + 38 * tdep->wordsize);
899
900 if (ppc_linux_trap_reg_p (gdbarch))
901 {
902 trad_frame_set_reg_addr (this_cache, PPC_ORIG_R3_REGNUM,
903 gpregs + 34 * tdep->wordsize);
904 trad_frame_set_reg_addr (this_cache, PPC_TRAP_REGNUM,
905 gpregs + 40 * tdep->wordsize);
906 }
907
908 if (ppc_floating_point_unit_p (gdbarch))
909 {
910 /* Floating point registers. */
911 for (i = 0; i < 32; i++)
912 {
913 int regnum = i + gdbarch_fp0_regnum (gdbarch);
914 trad_frame_set_reg_addr (this_cache, regnum,
915 fpregs + i * tdep->wordsize);
916 }
917 trad_frame_set_reg_addr (this_cache, tdep->ppc_fpscr_regnum,
918 fpregs + 32 * tdep->wordsize);
919 }
920 trad_frame_set_id (this_cache, frame_id_build (base, func));
921 }
922
923 static void
924 ppc32_linux_sigaction_cache_init (const struct tramp_frame *self,
925 struct frame_info *this_frame,
926 struct trad_frame_cache *this_cache,
927 CORE_ADDR func)
928 {
929 ppc_linux_sigtramp_cache (this_frame, this_cache, func,
930 0xd0 /* Offset to ucontext_t. */
931 + 0x30 /* Offset to .reg. */,
932 0);
933 }
934
935 static void
936 ppc64_linux_sigaction_cache_init (const struct tramp_frame *self,
937 struct frame_info *this_frame,
938 struct trad_frame_cache *this_cache,
939 CORE_ADDR func)
940 {
941 ppc_linux_sigtramp_cache (this_frame, this_cache, func,
942 0x80 /* Offset to ucontext_t. */
943 + 0xe0 /* Offset to .reg. */,
944 128);
945 }
946
947 static void
948 ppc32_linux_sighandler_cache_init (const struct tramp_frame *self,
949 struct frame_info *this_frame,
950 struct trad_frame_cache *this_cache,
951 CORE_ADDR func)
952 {
953 ppc_linux_sigtramp_cache (this_frame, this_cache, func,
954 0x40 /* Offset to ucontext_t. */
955 + 0x1c /* Offset to .reg. */,
956 0);
957 }
958
959 static void
960 ppc64_linux_sighandler_cache_init (const struct tramp_frame *self,
961 struct frame_info *this_frame,
962 struct trad_frame_cache *this_cache,
963 CORE_ADDR func)
964 {
965 ppc_linux_sigtramp_cache (this_frame, this_cache, func,
966 0x80 /* Offset to struct sigcontext. */
967 + 0x38 /* Offset to .reg. */,
968 128);
969 }
970
971 static struct tramp_frame ppc32_linux_sigaction_tramp_frame = {
972 SIGTRAMP_FRAME,
973 4,
974 {
975 { 0x380000ac, -1 }, /* li r0, 172 */
976 { 0x44000002, -1 }, /* sc */
977 { TRAMP_SENTINEL_INSN },
978 },
979 ppc32_linux_sigaction_cache_init
980 };
981 static struct tramp_frame ppc64_linux_sigaction_tramp_frame = {
982 SIGTRAMP_FRAME,
983 4,
984 {
985 { 0x38210080, -1 }, /* addi r1,r1,128 */
986 { 0x380000ac, -1 }, /* li r0, 172 */
987 { 0x44000002, -1 }, /* sc */
988 { TRAMP_SENTINEL_INSN },
989 },
990 ppc64_linux_sigaction_cache_init
991 };
992 static struct tramp_frame ppc32_linux_sighandler_tramp_frame = {
993 SIGTRAMP_FRAME,
994 4,
995 {
996 { 0x38000077, -1 }, /* li r0,119 */
997 { 0x44000002, -1 }, /* sc */
998 { TRAMP_SENTINEL_INSN },
999 },
1000 ppc32_linux_sighandler_cache_init
1001 };
1002 static struct tramp_frame ppc64_linux_sighandler_tramp_frame = {
1003 SIGTRAMP_FRAME,
1004 4,
1005 {
1006 { 0x38210080, -1 }, /* addi r1,r1,128 */
1007 { 0x38000077, -1 }, /* li r0,119 */
1008 { 0x44000002, -1 }, /* sc */
1009 { TRAMP_SENTINEL_INSN },
1010 },
1011 ppc64_linux_sighandler_cache_init
1012 };
1013
1014
1015 /* Address to use for displaced stepping. When debugging a stand-alone
1016 SPU executable, entry_point_address () will point to an SPU local-store
1017 address and is thus not usable as displaced stepping location. We use
1018 the auxiliary vector to determine the PowerPC-side entry point address
1019 instead. */
1020
1021 static CORE_ADDR ppc_linux_entry_point_addr = 0;
1022
1023 static void
1024 ppc_linux_inferior_created (struct target_ops *target, int from_tty)
1025 {
1026 ppc_linux_entry_point_addr = 0;
1027 }
1028
1029 static CORE_ADDR
1030 ppc_linux_displaced_step_location (struct gdbarch *gdbarch)
1031 {
1032 if (ppc_linux_entry_point_addr == 0)
1033 {
1034 CORE_ADDR addr;
1035
1036 /* Determine entry point from target auxiliary vector. */
1037 if (target_auxv_search (&current_target, AT_ENTRY, &addr) <= 0)
1038 error (_("Cannot find AT_ENTRY auxiliary vector entry."));
1039
1040 /* Make certain that the address points at real code, and not a
1041 function descriptor. */
1042 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
1043 &current_target);
1044
1045 /* Inferior calls also use the entry point as a breakpoint location.
1046 We don't want displaced stepping to interfere with those
1047 breakpoints, so leave space. */
1048 ppc_linux_entry_point_addr = addr + 2 * PPC_INSN_SIZE;
1049 }
1050
1051 return ppc_linux_entry_point_addr;
1052 }
1053
1054
1055 /* Return 1 if PPC_ORIG_R3_REGNUM and PPC_TRAP_REGNUM are usable. */
1056 int
1057 ppc_linux_trap_reg_p (struct gdbarch *gdbarch)
1058 {
1059 /* If we do not have a target description with registers, then
1060 the special registers will not be included in the register set. */
1061 if (!tdesc_has_registers (gdbarch_target_desc (gdbarch)))
1062 return 0;
1063
1064 /* If we do, then it is safe to check the size. */
1065 return register_size (gdbarch, PPC_ORIG_R3_REGNUM) > 0
1066 && register_size (gdbarch, PPC_TRAP_REGNUM) > 0;
1067 }
1068
1069 static void
1070 ppc_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
1071 {
1072 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1073
1074 regcache_cooked_write_unsigned (regcache, gdbarch_pc_regnum (gdbarch), pc);
1075
1076 /* Set special TRAP register to -1 to prevent the kernel from
1077 messing with the PC we just installed, if we happen to be
1078 within an interrupted system call that the kernel wants to
1079 restart.
1080
1081 Note that after we return from the dummy call, the TRAP and
1082 ORIG_R3 registers will be automatically restored, and the
1083 kernel continues to restart the system call at this point. */
1084 if (ppc_linux_trap_reg_p (gdbarch))
1085 regcache_cooked_write_unsigned (regcache, PPC_TRAP_REGNUM, -1);
1086 }
1087
1088 static int
1089 ppc_linux_spu_section (bfd *abfd, asection *asect, void *user_data)
1090 {
1091 return strncmp (bfd_section_name (abfd, asect), "SPU/", 4) == 0;
1092 }
1093
1094 static const struct target_desc *
1095 ppc_linux_core_read_description (struct gdbarch *gdbarch,
1096 struct target_ops *target,
1097 bfd *abfd)
1098 {
1099 asection *cell = bfd_sections_find_if (abfd, ppc_linux_spu_section, NULL);
1100 asection *altivec = bfd_get_section_by_name (abfd, ".reg-ppc-vmx");
1101 asection *vsx = bfd_get_section_by_name (abfd, ".reg-ppc-vsx");
1102 asection *section = bfd_get_section_by_name (abfd, ".reg");
1103 if (! section)
1104 return NULL;
1105
1106 switch (bfd_section_size (abfd, section))
1107 {
1108 case 48 * 4:
1109 if (cell)
1110 return tdesc_powerpc_cell32l;
1111 else if (vsx)
1112 return tdesc_powerpc_vsx32l;
1113 else if (altivec)
1114 return tdesc_powerpc_altivec32l;
1115 else
1116 return tdesc_powerpc_32l;
1117
1118 case 48 * 8:
1119 if (cell)
1120 return tdesc_powerpc_cell64l;
1121 else if (vsx)
1122 return tdesc_powerpc_vsx64l;
1123 else if (altivec)
1124 return tdesc_powerpc_altivec64l;
1125 else
1126 return tdesc_powerpc_64l;
1127
1128 default:
1129 return NULL;
1130 }
1131 }
1132
1133
1134 /* Cell/B.E. active SPE context tracking support. */
1135
1136 static struct objfile *spe_context_objfile = NULL;
1137 static CORE_ADDR spe_context_lm_addr = 0;
1138 static CORE_ADDR spe_context_offset = 0;
1139
1140 static ptid_t spe_context_cache_ptid;
1141 static CORE_ADDR spe_context_cache_address;
1142
1143 /* Hook into inferior_created, solib_loaded, and solib_unloaded observers
1144 to track whether we've loaded a version of libspe2 (as static or dynamic
1145 library) that provides the __spe_current_active_context variable. */
1146 static void
1147 ppc_linux_spe_context_lookup (struct objfile *objfile)
1148 {
1149 struct minimal_symbol *sym;
1150
1151 if (!objfile)
1152 {
1153 spe_context_objfile = NULL;
1154 spe_context_lm_addr = 0;
1155 spe_context_offset = 0;
1156 spe_context_cache_ptid = minus_one_ptid;
1157 spe_context_cache_address = 0;
1158 return;
1159 }
1160
1161 sym = lookup_minimal_symbol ("__spe_current_active_context", NULL, objfile);
1162 if (sym)
1163 {
1164 spe_context_objfile = objfile;
1165 spe_context_lm_addr = svr4_fetch_objfile_link_map (objfile);
1166 spe_context_offset = SYMBOL_VALUE_ADDRESS (sym);
1167 spe_context_cache_ptid = minus_one_ptid;
1168 spe_context_cache_address = 0;
1169 return;
1170 }
1171 }
1172
1173 static void
1174 ppc_linux_spe_context_inferior_created (struct target_ops *t, int from_tty)
1175 {
1176 struct objfile *objfile;
1177
1178 ppc_linux_spe_context_lookup (NULL);
1179 ALL_OBJFILES (objfile)
1180 ppc_linux_spe_context_lookup (objfile);
1181 }
1182
1183 static void
1184 ppc_linux_spe_context_solib_loaded (struct so_list *so)
1185 {
1186 if (strstr (so->so_original_name, "/libspe") != NULL)
1187 {
1188 solib_read_symbols (so, so->from_tty ? SYMFILE_VERBOSE : 0);
1189 ppc_linux_spe_context_lookup (so->objfile);
1190 }
1191 }
1192
1193 static void
1194 ppc_linux_spe_context_solib_unloaded (struct so_list *so)
1195 {
1196 if (so->objfile == spe_context_objfile)
1197 ppc_linux_spe_context_lookup (NULL);
1198 }
1199
1200 /* Retrieve contents of the N'th element in the current thread's
1201 linked SPE context list into ID and NPC. Return the address of
1202 said context element, or 0 if not found. */
1203 static CORE_ADDR
1204 ppc_linux_spe_context (int wordsize, enum bfd_endian byte_order,
1205 int n, int *id, unsigned int *npc)
1206 {
1207 CORE_ADDR spe_context = 0;
1208 gdb_byte buf[16];
1209 int i;
1210
1211 /* Quick exit if we have not found __spe_current_active_context. */
1212 if (!spe_context_objfile)
1213 return 0;
1214
1215 /* Look up cached address of thread-local variable. */
1216 if (!ptid_equal (spe_context_cache_ptid, inferior_ptid))
1217 {
1218 struct target_ops *target = &current_target;
1219 volatile struct gdb_exception ex;
1220
1221 while (target && !target->to_get_thread_local_address)
1222 target = find_target_beneath (target);
1223 if (!target)
1224 return 0;
1225
1226 TRY_CATCH (ex, RETURN_MASK_ERROR)
1227 {
1228 /* We do not call target_translate_tls_address here, because
1229 svr4_fetch_objfile_link_map may invalidate the frame chain,
1230 which must not do while inside a frame sniffer.
1231
1232 Instead, we have cached the lm_addr value, and use that to
1233 directly call the target's to_get_thread_local_address. */
1234 spe_context_cache_address
1235 = target->to_get_thread_local_address (target, inferior_ptid,
1236 spe_context_lm_addr,
1237 spe_context_offset);
1238 spe_context_cache_ptid = inferior_ptid;
1239 }
1240
1241 if (ex.reason < 0)
1242 return 0;
1243 }
1244
1245 /* Read variable value. */
1246 if (target_read_memory (spe_context_cache_address, buf, wordsize) == 0)
1247 spe_context = extract_unsigned_integer (buf, wordsize, byte_order);
1248
1249 /* Cyle through to N'th linked list element. */
1250 for (i = 0; i < n && spe_context; i++)
1251 if (target_read_memory (spe_context + align_up (12, wordsize),
1252 buf, wordsize) == 0)
1253 spe_context = extract_unsigned_integer (buf, wordsize, byte_order);
1254 else
1255 spe_context = 0;
1256
1257 /* Read current context. */
1258 if (spe_context
1259 && target_read_memory (spe_context, buf, 12) != 0)
1260 spe_context = 0;
1261
1262 /* Extract data elements. */
1263 if (spe_context)
1264 {
1265 if (id)
1266 *id = extract_signed_integer (buf, 4, byte_order);
1267 if (npc)
1268 *npc = extract_unsigned_integer (buf + 4, 4, byte_order);
1269 }
1270
1271 return spe_context;
1272 }
1273
1274
1275 /* Cell/B.E. cross-architecture unwinder support. */
1276
1277 struct ppu2spu_cache
1278 {
1279 struct frame_id frame_id;
1280 struct regcache *regcache;
1281 };
1282
1283 static struct gdbarch *
1284 ppu2spu_prev_arch (struct frame_info *this_frame, void **this_cache)
1285 {
1286 struct ppu2spu_cache *cache = *this_cache;
1287 return get_regcache_arch (cache->regcache);
1288 }
1289
1290 static void
1291 ppu2spu_this_id (struct frame_info *this_frame,
1292 void **this_cache, struct frame_id *this_id)
1293 {
1294 struct ppu2spu_cache *cache = *this_cache;
1295 *this_id = cache->frame_id;
1296 }
1297
1298 static struct value *
1299 ppu2spu_prev_register (struct frame_info *this_frame,
1300 void **this_cache, int regnum)
1301 {
1302 struct ppu2spu_cache *cache = *this_cache;
1303 struct gdbarch *gdbarch = get_regcache_arch (cache->regcache);
1304 gdb_byte *buf;
1305
1306 buf = alloca (register_size (gdbarch, regnum));
1307 regcache_cooked_read (cache->regcache, regnum, buf);
1308 return frame_unwind_got_bytes (this_frame, regnum, buf);
1309 }
1310
1311 struct ppu2spu_data
1312 {
1313 struct gdbarch *gdbarch;
1314 int id;
1315 unsigned int npc;
1316 gdb_byte gprs[128*16];
1317 };
1318
1319 static int
1320 ppu2spu_unwind_register (void *src, int regnum, gdb_byte *buf)
1321 {
1322 struct ppu2spu_data *data = src;
1323 enum bfd_endian byte_order = gdbarch_byte_order (data->gdbarch);
1324
1325 if (regnum >= 0 && regnum < SPU_NUM_GPRS)
1326 memcpy (buf, data->gprs + 16*regnum, 16);
1327 else if (regnum == SPU_ID_REGNUM)
1328 store_unsigned_integer (buf, 4, byte_order, data->id);
1329 else if (regnum == SPU_PC_REGNUM)
1330 store_unsigned_integer (buf, 4, byte_order, data->npc);
1331 else
1332 return 0;
1333
1334 return 1;
1335 }
1336
1337 static int
1338 ppu2spu_sniffer (const struct frame_unwind *self,
1339 struct frame_info *this_frame, void **this_prologue_cache)
1340 {
1341 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1342 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1343 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1344 struct ppu2spu_data data;
1345 struct frame_info *fi;
1346 CORE_ADDR base, func, backchain, spe_context;
1347 gdb_byte buf[8];
1348 int n = 0;
1349
1350 /* Count the number of SPU contexts already in the frame chain. */
1351 for (fi = get_next_frame (this_frame); fi; fi = get_next_frame (fi))
1352 if (get_frame_type (fi) == ARCH_FRAME
1353 && gdbarch_bfd_arch_info (get_frame_arch (fi))->arch == bfd_arch_spu)
1354 n++;
1355
1356 base = get_frame_sp (this_frame);
1357 func = get_frame_pc (this_frame);
1358 if (target_read_memory (base, buf, tdep->wordsize))
1359 return 0;
1360 backchain = extract_unsigned_integer (buf, tdep->wordsize, byte_order);
1361
1362 spe_context = ppc_linux_spe_context (tdep->wordsize, byte_order,
1363 n, &data.id, &data.npc);
1364 if (spe_context && base <= spe_context && spe_context < backchain)
1365 {
1366 char annex[32];
1367
1368 /* Find gdbarch for SPU. */
1369 struct gdbarch_info info;
1370 gdbarch_info_init (&info);
1371 info.bfd_arch_info = bfd_lookup_arch (bfd_arch_spu, bfd_mach_spu);
1372 info.byte_order = BFD_ENDIAN_BIG;
1373 info.osabi = GDB_OSABI_LINUX;
1374 info.tdep_info = (void *) &data.id;
1375 data.gdbarch = gdbarch_find_by_info (info);
1376 if (!data.gdbarch)
1377 return 0;
1378
1379 xsnprintf (annex, sizeof annex, "%d/regs", data.id);
1380 if (target_read (&current_target, TARGET_OBJECT_SPU, annex,
1381 data.gprs, 0, sizeof data.gprs)
1382 == sizeof data.gprs)
1383 {
1384 struct ppu2spu_cache *cache
1385 = FRAME_OBSTACK_CALLOC (1, struct ppu2spu_cache);
1386
1387 struct regcache *regcache = regcache_xmalloc (data.gdbarch);
1388 struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache);
1389 regcache_save (regcache, ppu2spu_unwind_register, &data);
1390 discard_cleanups (cleanups);
1391
1392 cache->frame_id = frame_id_build (base, func);
1393 cache->regcache = regcache;
1394 *this_prologue_cache = cache;
1395 return 1;
1396 }
1397 }
1398
1399 return 0;
1400 }
1401
1402 static void
1403 ppu2spu_dealloc_cache (struct frame_info *self, void *this_cache)
1404 {
1405 struct ppu2spu_cache *cache = this_cache;
1406 regcache_xfree (cache->regcache);
1407 }
1408
1409 static const struct frame_unwind ppu2spu_unwind = {
1410 ARCH_FRAME,
1411 ppu2spu_this_id,
1412 ppu2spu_prev_register,
1413 NULL,
1414 ppu2spu_sniffer,
1415 ppu2spu_dealloc_cache,
1416 ppu2spu_prev_arch,
1417 };
1418
1419
1420 static void
1421 ppc_linux_init_abi (struct gdbarch_info info,
1422 struct gdbarch *gdbarch)
1423 {
1424 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1425 struct tdesc_arch_data *tdesc_data = (void *) info.tdep_info;
1426
1427 /* PPC GNU/Linux uses either 64-bit or 128-bit long doubles; where
1428 128-bit, they are IBM long double, not IEEE quad long double as
1429 in the System V ABI PowerPC Processor Supplement. We can safely
1430 let them default to 128-bit, since the debug info will give the
1431 size of type actually used in each case. */
1432 set_gdbarch_long_double_bit (gdbarch, 16 * TARGET_CHAR_BIT);
1433 set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);
1434
1435 /* Handle inferior calls during interrupted system calls. */
1436 set_gdbarch_write_pc (gdbarch, ppc_linux_write_pc);
1437
1438 if (tdep->wordsize == 4)
1439 {
1440 /* Until November 2001, gcc did not comply with the 32 bit SysV
1441 R4 ABI requirement that structures less than or equal to 8
1442 bytes should be returned in registers. Instead GCC was using
1443 the the AIX/PowerOpen ABI - everything returned in memory
1444 (well ignoring vectors that is). When this was corrected, it
1445 wasn't fixed for GNU/Linux native platform. Use the
1446 PowerOpen struct convention. */
1447 set_gdbarch_return_value (gdbarch, ppc_linux_return_value);
1448
1449 set_gdbarch_memory_remove_breakpoint (gdbarch,
1450 ppc_linux_memory_remove_breakpoint);
1451
1452 /* Shared library handling. */
1453 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
1454 set_solib_svr4_fetch_link_map_offsets
1455 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
1456
1457 /* Trampolines. */
1458 tramp_frame_prepend_unwinder (gdbarch, &ppc32_linux_sigaction_tramp_frame);
1459 tramp_frame_prepend_unwinder (gdbarch, &ppc32_linux_sighandler_tramp_frame);
1460
1461 /* BFD target for core files. */
1462 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
1463 set_gdbarch_gcore_bfd_target (gdbarch, "elf32-powerpcle");
1464 else
1465 set_gdbarch_gcore_bfd_target (gdbarch, "elf32-powerpc");
1466 }
1467
1468 if (tdep->wordsize == 8)
1469 {
1470 /* Handle PPC GNU/Linux 64-bit function pointers (which are really
1471 function descriptors). */
1472 set_gdbarch_convert_from_func_ptr_addr
1473 (gdbarch, ppc64_linux_convert_from_func_ptr_addr);
1474
1475 /* Shared library handling. */
1476 set_gdbarch_skip_trampoline_code (gdbarch, ppc64_skip_trampoline_code);
1477 set_solib_svr4_fetch_link_map_offsets
1478 (gdbarch, svr4_lp64_fetch_link_map_offsets);
1479
1480 /* Trampolines. */
1481 tramp_frame_prepend_unwinder (gdbarch, &ppc64_linux_sigaction_tramp_frame);
1482 tramp_frame_prepend_unwinder (gdbarch, &ppc64_linux_sighandler_tramp_frame);
1483
1484 /* BFD target for core files. */
1485 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
1486 set_gdbarch_gcore_bfd_target (gdbarch, "elf64-powerpcle");
1487 else
1488 set_gdbarch_gcore_bfd_target (gdbarch, "elf64-powerpc");
1489 }
1490 set_gdbarch_regset_from_core_section (gdbarch, ppc_linux_regset_from_core_section);
1491 set_gdbarch_core_read_description (gdbarch, ppc_linux_core_read_description);
1492
1493 /* Supported register sections. */
1494 if (tdesc_find_feature (info.target_desc,
1495 "org.gnu.gdb.power.vsx"))
1496 set_gdbarch_core_regset_sections (gdbarch, ppc_linux_vsx_regset_sections);
1497 else if (tdesc_find_feature (info.target_desc,
1498 "org.gnu.gdb.power.altivec"))
1499 set_gdbarch_core_regset_sections (gdbarch, ppc_linux_vmx_regset_sections);
1500 else
1501 set_gdbarch_core_regset_sections (gdbarch, ppc_linux_fp_regset_sections);
1502
1503 /* Enable TLS support. */
1504 set_gdbarch_fetch_tls_load_module_address (gdbarch,
1505 svr4_fetch_objfile_link_map);
1506
1507 if (tdesc_data)
1508 {
1509 const struct tdesc_feature *feature;
1510
1511 /* If we have target-described registers, then we can safely
1512 reserve a number for PPC_ORIG_R3_REGNUM and PPC_TRAP_REGNUM
1513 (whether they are described or not). */
1514 gdb_assert (gdbarch_num_regs (gdbarch) <= PPC_ORIG_R3_REGNUM);
1515 set_gdbarch_num_regs (gdbarch, PPC_TRAP_REGNUM + 1);
1516
1517 /* If they are present, then assign them to the reserved number. */
1518 feature = tdesc_find_feature (info.target_desc,
1519 "org.gnu.gdb.power.linux");
1520 if (feature != NULL)
1521 {
1522 tdesc_numbered_register (feature, tdesc_data,
1523 PPC_ORIG_R3_REGNUM, "orig_r3");
1524 tdesc_numbered_register (feature, tdesc_data,
1525 PPC_TRAP_REGNUM, "trap");
1526 }
1527 }
1528
1529 /* Enable Cell/B.E. if supported by the target. */
1530 if (tdesc_compatible_p (info.target_desc,
1531 bfd_lookup_arch (bfd_arch_spu, bfd_mach_spu)))
1532 {
1533 /* Cell/B.E. multi-architecture support. */
1534 set_spu_solib_ops (gdbarch);
1535
1536 /* Cell/B.E. cross-architecture unwinder support. */
1537 frame_unwind_prepend_unwinder (gdbarch, &ppu2spu_unwind);
1538
1539 /* The default displaced_step_at_entry_point doesn't work for
1540 SPU stand-alone executables. */
1541 set_gdbarch_displaced_step_location (gdbarch,
1542 ppc_linux_displaced_step_location);
1543 }
1544 }
1545
1546 /* Provide a prototype to silence -Wmissing-prototypes. */
1547 extern initialize_file_ftype _initialize_ppc_linux_tdep;
1548
1549 void
1550 _initialize_ppc_linux_tdep (void)
1551 {
1552 /* Register for all sub-familes of the POWER/PowerPC: 32-bit and
1553 64-bit PowerPC, and the older rs6k. */
1554 gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc, GDB_OSABI_LINUX,
1555 ppc_linux_init_abi);
1556 gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc64, GDB_OSABI_LINUX,
1557 ppc_linux_init_abi);
1558 gdbarch_register_osabi (bfd_arch_rs6000, bfd_mach_rs6k, GDB_OSABI_LINUX,
1559 ppc_linux_init_abi);
1560
1561 /* Attach to inferior_created observer. */
1562 observer_attach_inferior_created (ppc_linux_inferior_created);
1563
1564 /* Attach to observers to track __spe_current_active_context. */
1565 observer_attach_inferior_created (ppc_linux_spe_context_inferior_created);
1566 observer_attach_solib_loaded (ppc_linux_spe_context_solib_loaded);
1567 observer_attach_solib_unloaded (ppc_linux_spe_context_solib_unloaded);
1568
1569 /* Initialize the Linux target descriptions. */
1570 initialize_tdesc_powerpc_32l ();
1571 initialize_tdesc_powerpc_altivec32l ();
1572 initialize_tdesc_powerpc_cell32l ();
1573 initialize_tdesc_powerpc_vsx32l ();
1574 initialize_tdesc_powerpc_isa205_32l ();
1575 initialize_tdesc_powerpc_isa205_altivec32l ();
1576 initialize_tdesc_powerpc_isa205_vsx32l ();
1577 initialize_tdesc_powerpc_64l ();
1578 initialize_tdesc_powerpc_altivec64l ();
1579 initialize_tdesc_powerpc_cell64l ();
1580 initialize_tdesc_powerpc_vsx64l ();
1581 initialize_tdesc_powerpc_isa205_64l ();
1582 initialize_tdesc_powerpc_isa205_altivec64l ();
1583 initialize_tdesc_powerpc_isa205_vsx64l ();
1584 initialize_tdesc_powerpc_e500l ();
1585 }
This page took 0.06272 seconds and 5 git commands to generate.