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