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