IA64: Migrate from 'regset_from_core_section' to 'iterate_over_regset_sections'
[platform/upstream/binutils.git] / gdb / ppc-linux-tdep.c
1 /* Target-dependent code for GDB, the GNU debugger.
2
3    Copyright (C) 1986-2014 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "symtab.h"
24 #include "target.h"
25 #include "gdbcore.h"
26 #include "gdbcmd.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29 #include "regcache.h"
30 #include "value.h"
31 #include "osabi.h"
32 #include "regset.h"
33 #include "solib-svr4.h"
34 #include "solib-spu.h"
35 #include "solib.h"
36 #include "solist.h"
37 #include "ppc-tdep.h"
38 #include "ppc64-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 "elf/ppc64.h"
48 #include "exceptions.h"
49 #include "arch-utils.h"
50 #include "spu-tdep.h"
51 #include "xml-syscall.h"
52 #include "linux-tdep.h"
53
54 #include "stap-probe.h"
55 #include "ax.h"
56 #include "ax-gdb.h"
57 #include "cli/cli-utils.h"
58 #include "parser-defs.h"
59 #include "user-regs.h"
60 #include <ctype.h>
61 #include "elf-bfd.h"            /* for elfcore_write_* */
62
63 #include "features/rs6000/powerpc-32l.c"
64 #include "features/rs6000/powerpc-altivec32l.c"
65 #include "features/rs6000/powerpc-cell32l.c"
66 #include "features/rs6000/powerpc-vsx32l.c"
67 #include "features/rs6000/powerpc-isa205-32l.c"
68 #include "features/rs6000/powerpc-isa205-altivec32l.c"
69 #include "features/rs6000/powerpc-isa205-vsx32l.c"
70 #include "features/rs6000/powerpc-64l.c"
71 #include "features/rs6000/powerpc-altivec64l.c"
72 #include "features/rs6000/powerpc-cell64l.c"
73 #include "features/rs6000/powerpc-vsx64l.c"
74 #include "features/rs6000/powerpc-isa205-64l.c"
75 #include "features/rs6000/powerpc-isa205-altivec64l.c"
76 #include "features/rs6000/powerpc-isa205-vsx64l.c"
77 #include "features/rs6000/powerpc-e500l.c"
78
79 /* Shared library operations for PowerPC-Linux.  */
80 static struct target_so_ops powerpc_so_ops;
81
82 /* The syscall's XML filename for PPC and PPC64.  */
83 #define XML_SYSCALL_FILENAME_PPC "syscalls/ppc-linux.xml"
84 #define XML_SYSCALL_FILENAME_PPC64 "syscalls/ppc64-linux.xml"
85
86 /* ppc_linux_memory_remove_breakpoints attempts to remove a breakpoint
87    in much the same fashion as memory_remove_breakpoint in mem-break.c,
88    but is careful not to write back the previous contents if the code
89    in question has changed in between inserting the breakpoint and
90    removing it.
91
92    Here is the problem that we're trying to solve...
93
94    Once upon a time, before introducing this function to remove
95    breakpoints from the inferior, setting a breakpoint on a shared
96    library function prior to running the program would not work
97    properly.  In order to understand the problem, it is first
98    necessary to understand a little bit about dynamic linking on
99    this platform.
100
101    A call to a shared library function is accomplished via a bl
102    (branch-and-link) instruction whose branch target is an entry
103    in the procedure linkage table (PLT).  The PLT in the object
104    file is uninitialized.  To gdb, prior to running the program, the
105    entries in the PLT are all zeros.
106
107    Once the program starts running, the shared libraries are loaded
108    and the procedure linkage table is initialized, but the entries in
109    the table are not (necessarily) resolved.  Once a function is
110    actually called, the code in the PLT is hit and the function is
111    resolved.  In order to better illustrate this, an example is in
112    order; the following example is from the gdb testsuite.
113             
114         We start the program shmain.
115
116             [kev@arroyo testsuite]$ ../gdb gdb.base/shmain
117             [...]
118
119         We place two breakpoints, one on shr1 and the other on main.
120
121             (gdb) b shr1
122             Breakpoint 1 at 0x100409d4
123             (gdb) b main
124             Breakpoint 2 at 0x100006a0: file gdb.base/shmain.c, line 44.
125
126         Examine the instruction (and the immediatly following instruction)
127         upon which the breakpoint was placed.  Note that the PLT entry
128         for shr1 contains zeros.
129
130             (gdb) x/2i 0x100409d4
131             0x100409d4 <shr1>:      .long 0x0
132             0x100409d8 <shr1+4>:    .long 0x0
133
134         Now run 'til main.
135
136             (gdb) r
137             Starting program: gdb.base/shmain 
138             Breakpoint 1 at 0xffaf790: file gdb.base/shr1.c, line 19.
139
140             Breakpoint 2, main ()
141                 at gdb.base/shmain.c:44
142             44        g = 1;
143
144         Examine the PLT again.  Note that the loading of the shared
145         library has initialized the PLT to code which loads a constant
146         (which I think is an index into the GOT) into r11 and then
147         branchs a short distance to the code which actually does the
148         resolving.
149
150             (gdb) x/2i 0x100409d4
151             0x100409d4 <shr1>:      li      r11,4
152             0x100409d8 <shr1+4>:    b       0x10040984 <sg+4>
153             (gdb) c
154             Continuing.
155
156             Breakpoint 1, shr1 (x=1)
157                 at gdb.base/shr1.c:19
158             19        l = 1;
159
160         Now we've hit the breakpoint at shr1.  (The breakpoint was
161         reset from the PLT entry to the actual shr1 function after the
162         shared library was loaded.) Note that the PLT entry has been
163         resolved to contain a branch that takes us directly to shr1.
164         (The real one, not the PLT entry.)
165
166             (gdb) x/2i 0x100409d4
167             0x100409d4 <shr1>:      b       0xffaf76c <shr1>
168             0x100409d8 <shr1+4>:    b       0x10040984 <sg+4>
169
170    The thing to note here is that the PLT entry for shr1 has been
171    changed twice.
172
173    Now the problem should be obvious.  GDB places a breakpoint (a
174    trap instruction) on the zero value of the PLT entry for shr1.
175    Later on, after the shared library had been loaded and the PLT
176    initialized, GDB gets a signal indicating this fact and attempts
177    (as it always does when it stops) to remove all the breakpoints.
178
179    The breakpoint removal was causing the former contents (a zero
180    word) to be written back to the now initialized PLT entry thus
181    destroying a portion of the initialization that had occurred only a
182    short time ago.  When execution continued, the zero word would be
183    executed as an instruction an illegal instruction trap was
184    generated instead.  (0 is not a legal instruction.)
185
186    The fix for this problem was fairly straightforward.  The function
187    memory_remove_breakpoint from mem-break.c was copied to this file,
188    modified slightly, and renamed to ppc_linux_memory_remove_breakpoint.
189    In tm-linux.h, MEMORY_REMOVE_BREAKPOINT is defined to call this new
190    function.
191
192    The differences between ppc_linux_memory_remove_breakpoint () and
193    memory_remove_breakpoint () are minor.  All that the former does
194    that the latter does not is check to make sure that the breakpoint
195    location actually contains a breakpoint (trap instruction) prior
196    to attempting to write back the old contents.  If it does contain
197    a trap instruction, we allow the old contents to be written back.
198    Otherwise, we silently do nothing.
199
200    The big question is whether memory_remove_breakpoint () should be
201    changed to have the same functionality.  The downside is that more
202    traffic is generated for remote targets since we'll have an extra
203    fetch of a memory word each time a breakpoint is removed.
204
205    For the time being, we'll leave this self-modifying-code-friendly
206    version in ppc-linux-tdep.c, but it ought to be migrated somewhere
207    else in the event that some other platform has similar needs with
208    regard to removing breakpoints in some potentially self modifying
209    code.  */
210 static int
211 ppc_linux_memory_remove_breakpoint (struct gdbarch *gdbarch,
212                                     struct bp_target_info *bp_tgt)
213 {
214   CORE_ADDR addr = bp_tgt->placed_address;
215   const unsigned char *bp;
216   int val;
217   int bplen;
218   gdb_byte old_contents[BREAKPOINT_MAX];
219   struct cleanup *cleanup;
220
221   /* Determine appropriate breakpoint contents and size for this address.  */
222   bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen);
223   if (bp == NULL)
224     error (_("Software breakpoints not implemented for this target."));
225
226   /* Make sure we see the memory breakpoints.  */
227   cleanup = make_show_memory_breakpoints_cleanup (1);
228   val = target_read_memory (addr, old_contents, bplen);
229
230   /* If our breakpoint is no longer at the address, this means that the
231      program modified the code on us, so it is wrong to put back the
232      old value.  */
233   if (val == 0 && memcmp (bp, old_contents, bplen) == 0)
234     val = target_write_raw_memory (addr, bp_tgt->shadow_contents, bplen);
235
236   do_cleanups (cleanup);
237   return val;
238 }
239
240 /* For historic reasons, PPC 32 GNU/Linux follows PowerOpen rather
241    than the 32 bit SYSV R4 ABI structure return convention - all
242    structures, no matter their size, are put in memory.  Vectors,
243    which were added later, do get returned in a register though.  */
244
245 static enum return_value_convention
246 ppc_linux_return_value (struct gdbarch *gdbarch, struct value *function,
247                         struct type *valtype, struct regcache *regcache,
248                         gdb_byte *readbuf, const gdb_byte *writebuf)
249 {  
250   if ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
251        || TYPE_CODE (valtype) == TYPE_CODE_UNION)
252       && !((TYPE_LENGTH (valtype) == 16 || TYPE_LENGTH (valtype) == 8)
253            && TYPE_VECTOR (valtype)))
254     return RETURN_VALUE_STRUCT_CONVENTION;
255   else
256     return ppc_sysv_abi_return_value (gdbarch, function, valtype, regcache,
257                                       readbuf, writebuf);
258 }
259
260 /* PLT stub in executable.  */
261 static struct ppc_insn_pattern powerpc32_plt_stub[] =
262   {
263     { 0xffff0000, 0x3d600000, 0 },      /* lis   r11, xxxx       */
264     { 0xffff0000, 0x816b0000, 0 },      /* lwz   r11, xxxx(r11)  */
265     { 0xffffffff, 0x7d6903a6, 0 },      /* mtctr r11             */
266     { 0xffffffff, 0x4e800420, 0 },      /* bctr                  */
267     {          0,          0, 0 }
268   };
269
270 /* PLT stub in shared library.  */
271 static struct ppc_insn_pattern powerpc32_plt_stub_so[] =
272   {
273     { 0xffff0000, 0x817e0000, 0 },      /* lwz   r11, xxxx(r30)  */
274     { 0xffffffff, 0x7d6903a6, 0 },      /* mtctr r11             */
275     { 0xffffffff, 0x4e800420, 0 },      /* bctr                  */
276     { 0xffffffff, 0x60000000, 0 },      /* nop                   */
277     {          0,          0, 0 }
278   };
279 #define POWERPC32_PLT_STUB_LEN  ARRAY_SIZE (powerpc32_plt_stub)
280
281 /* Check if PC is in PLT stub.  For non-secure PLT, stub is in .plt
282    section.  For secure PLT, stub is in .text and we need to check
283    instruction patterns.  */
284
285 static int
286 powerpc_linux_in_dynsym_resolve_code (CORE_ADDR pc)
287 {
288   struct bound_minimal_symbol sym;
289
290   /* Check whether PC is in the dynamic linker.  This also checks
291      whether it is in the .plt section, used by non-PIC executables.  */
292   if (svr4_in_dynsym_resolve_code (pc))
293     return 1;
294
295   /* Check if we are in the resolver.  */
296   sym = lookup_minimal_symbol_by_pc (pc);
297   if (sym.minsym != NULL
298       && (strcmp (MSYMBOL_LINKAGE_NAME (sym.minsym), "__glink") == 0
299           || strcmp (MSYMBOL_LINKAGE_NAME (sym.minsym),
300                      "__glink_PLTresolve") == 0))
301     return 1;
302
303   return 0;
304 }
305
306 /* Follow PLT stub to actual routine.  */
307
308 static CORE_ADDR
309 ppc_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
310 {
311   unsigned int insnbuf[POWERPC32_PLT_STUB_LEN];
312   struct gdbarch *gdbarch = get_frame_arch (frame);
313   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
314   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
315   CORE_ADDR target = 0;
316
317   if (ppc_insns_match_pattern (frame, pc, powerpc32_plt_stub, insnbuf))
318     {
319       /* Insn pattern is
320                 lis   r11, xxxx
321                 lwz   r11, xxxx(r11)
322          Branch target is in r11.  */
323
324       target = (ppc_insn_d_field (insnbuf[0]) << 16)
325         | ppc_insn_d_field (insnbuf[1]);
326       target = read_memory_unsigned_integer (target, 4, byte_order);
327     }
328
329   if (ppc_insns_match_pattern (frame, pc, powerpc32_plt_stub_so, insnbuf))
330     {
331       /* Insn pattern is
332                 lwz   r11, xxxx(r30)
333          Branch target is in r11.  */
334
335       target = get_frame_register_unsigned (frame, tdep->ppc_gp0_regnum + 30)
336                + ppc_insn_d_field (insnbuf[0]);
337       target = read_memory_unsigned_integer (target, 4, byte_order);
338     }
339
340   return target;
341 }
342
343 /* Wrappers to handle Linux-only registers.  */
344
345 static void
346 ppc_linux_supply_gregset (const struct regset *regset,
347                           struct regcache *regcache,
348                           int regnum, const void *gregs, size_t len)
349 {
350   const struct ppc_reg_offsets *offsets = regset->regmap;
351
352   ppc_supply_gregset (regset, regcache, regnum, gregs, len);
353
354   if (ppc_linux_trap_reg_p (get_regcache_arch (regcache)))
355     {
356       /* "orig_r3" is stored 2 slots after "pc".  */
357       if (regnum == -1 || regnum == PPC_ORIG_R3_REGNUM)
358         ppc_supply_reg (regcache, PPC_ORIG_R3_REGNUM, gregs,
359                         offsets->pc_offset + 2 * offsets->gpr_size,
360                         offsets->gpr_size);
361
362       /* "trap" is stored 8 slots after "pc".  */
363       if (regnum == -1 || regnum == PPC_TRAP_REGNUM)
364         ppc_supply_reg (regcache, PPC_TRAP_REGNUM, gregs,
365                         offsets->pc_offset + 8 * offsets->gpr_size,
366                         offsets->gpr_size);
367     }
368 }
369
370 static void
371 ppc_linux_collect_gregset (const struct regset *regset,
372                            const struct regcache *regcache,
373                            int regnum, void *gregs, size_t len)
374 {
375   const struct ppc_reg_offsets *offsets = regset->regmap;
376
377   /* Clear areas in the linux gregset not written elsewhere.  */
378   if (regnum == -1)
379     memset (gregs, 0, len);
380
381   ppc_collect_gregset (regset, regcache, regnum, gregs, len);
382
383   if (ppc_linux_trap_reg_p (get_regcache_arch (regcache)))
384     {
385       /* "orig_r3" is stored 2 slots after "pc".  */
386       if (regnum == -1 || regnum == PPC_ORIG_R3_REGNUM)
387         ppc_collect_reg (regcache, PPC_ORIG_R3_REGNUM, gregs,
388                          offsets->pc_offset + 2 * offsets->gpr_size,
389                          offsets->gpr_size);
390
391       /* "trap" is stored 8 slots after "pc".  */
392       if (regnum == -1 || regnum == PPC_TRAP_REGNUM)
393         ppc_collect_reg (regcache, PPC_TRAP_REGNUM, gregs,
394                          offsets->pc_offset + 8 * offsets->gpr_size,
395                          offsets->gpr_size);
396     }
397 }
398
399 /* Regset descriptions.  */
400 static const struct ppc_reg_offsets ppc32_linux_reg_offsets =
401   {
402     /* General-purpose registers.  */
403     /* .r0_offset = */ 0,
404     /* .gpr_size = */ 4,
405     /* .xr_size = */ 4,
406     /* .pc_offset = */ 128,
407     /* .ps_offset = */ 132,
408     /* .cr_offset = */ 152,
409     /* .lr_offset = */ 144,
410     /* .ctr_offset = */ 140,
411     /* .xer_offset = */ 148,
412     /* .mq_offset = */ 156,
413
414     /* Floating-point registers.  */
415     /* .f0_offset = */ 0,
416     /* .fpscr_offset = */ 256,
417     /* .fpscr_size = */ 8,
418
419     /* AltiVec registers.  */
420     /* .vr0_offset = */ 0,
421     /* .vscr_offset = */ 512 + 12,
422     /* .vrsave_offset = */ 528
423   };
424
425 static const struct ppc_reg_offsets ppc64_linux_reg_offsets =
426   {
427     /* General-purpose registers.  */
428     /* .r0_offset = */ 0,
429     /* .gpr_size = */ 8,
430     /* .xr_size = */ 8,
431     /* .pc_offset = */ 256,
432     /* .ps_offset = */ 264,
433     /* .cr_offset = */ 304,
434     /* .lr_offset = */ 288,
435     /* .ctr_offset = */ 280,
436     /* .xer_offset = */ 296,
437     /* .mq_offset = */ 312,
438
439     /* Floating-point registers.  */
440     /* .f0_offset = */ 0,
441     /* .fpscr_offset = */ 256,
442     /* .fpscr_size = */ 8,
443
444     /* AltiVec registers.  */
445     /* .vr0_offset = */ 0,
446     /* .vscr_offset = */ 512 + 12,
447     /* .vrsave_offset = */ 528
448   };
449
450 static const struct regset ppc32_linux_gregset = {
451   &ppc32_linux_reg_offsets,
452   ppc_linux_supply_gregset,
453   ppc_linux_collect_gregset
454 };
455
456 static const struct regset ppc64_linux_gregset = {
457   &ppc64_linux_reg_offsets,
458   ppc_linux_supply_gregset,
459   ppc_linux_collect_gregset
460 };
461
462 static const struct regset ppc32_linux_fpregset = {
463   &ppc32_linux_reg_offsets,
464   ppc_supply_fpregset,
465   ppc_collect_fpregset
466 };
467
468 static const struct regset ppc32_linux_vrregset = {
469   &ppc32_linux_reg_offsets,
470   ppc_supply_vrregset,
471   ppc_collect_vrregset
472 };
473
474 static const struct regset ppc32_linux_vsxregset = {
475   &ppc32_linux_reg_offsets,
476   ppc_supply_vsxregset,
477   ppc_collect_vsxregset
478 };
479
480 const struct regset *
481 ppc_linux_gregset (int wordsize)
482 {
483   return wordsize == 8 ? &ppc64_linux_gregset : &ppc32_linux_gregset;
484 }
485
486 const struct regset *
487 ppc_linux_fpregset (void)
488 {
489   return &ppc32_linux_fpregset;
490 }
491
492 /* Iterate over supported core file register note sections. */
493
494 static void
495 ppc_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
496                                         iterate_over_regset_sections_cb *cb,
497                                         void *cb_data,
498                                         const struct regcache *regcache)
499 {
500   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
501   int have_altivec = tdep->ppc_vr0_regnum != -1;
502   int have_vsx = tdep->ppc_vsr0_upper_regnum != -1;
503
504   if (tdep->wordsize == 4)
505     cb (".reg", 48 * 4, &ppc32_linux_gregset, NULL, cb_data);
506   else
507     cb (".reg", 48 * 8, &ppc64_linux_gregset, NULL, cb_data);
508
509   cb (".reg2", 264, &ppc32_linux_fpregset, NULL, cb_data);
510
511   if (have_altivec)
512     cb (".reg-ppc-vmx", 544, &ppc32_linux_vrregset, "ppc Altivec", cb_data);
513
514   if (have_vsx)
515     cb (".reg-ppc-vsx", 256, &ppc32_linux_vsxregset, "POWER7 VSX", cb_data);
516 }
517
518 static void
519 ppc_linux_sigtramp_cache (struct frame_info *this_frame,
520                           struct trad_frame_cache *this_cache,
521                           CORE_ADDR func, LONGEST offset,
522                           int bias)
523 {
524   CORE_ADDR base;
525   CORE_ADDR regs;
526   CORE_ADDR gpregs;
527   CORE_ADDR fpregs;
528   int i;
529   struct gdbarch *gdbarch = get_frame_arch (this_frame);
530   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
531   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
532
533   base = get_frame_register_unsigned (this_frame,
534                                       gdbarch_sp_regnum (gdbarch));
535   if (bias > 0 && get_frame_pc (this_frame) != func)
536     /* See below, some signal trampolines increment the stack as their
537        first instruction, need to compensate for that.  */
538     base -= bias;
539
540   /* Find the address of the register buffer pointer.  */
541   regs = base + offset;
542   /* Use that to find the address of the corresponding register
543      buffers.  */
544   gpregs = read_memory_unsigned_integer (regs, tdep->wordsize, byte_order);
545   fpregs = gpregs + 48 * tdep->wordsize;
546
547   /* General purpose.  */
548   for (i = 0; i < 32; i++)
549     {
550       int regnum = i + tdep->ppc_gp0_regnum;
551       trad_frame_set_reg_addr (this_cache,
552                                regnum, gpregs + i * tdep->wordsize);
553     }
554   trad_frame_set_reg_addr (this_cache,
555                            gdbarch_pc_regnum (gdbarch),
556                            gpregs + 32 * tdep->wordsize);
557   trad_frame_set_reg_addr (this_cache, tdep->ppc_ctr_regnum,
558                            gpregs + 35 * tdep->wordsize);
559   trad_frame_set_reg_addr (this_cache, tdep->ppc_lr_regnum,
560                            gpregs + 36 * tdep->wordsize);
561   trad_frame_set_reg_addr (this_cache, tdep->ppc_xer_regnum,
562                            gpregs + 37 * tdep->wordsize);
563   trad_frame_set_reg_addr (this_cache, tdep->ppc_cr_regnum,
564                            gpregs + 38 * tdep->wordsize);
565
566   if (ppc_linux_trap_reg_p (gdbarch))
567     {
568       trad_frame_set_reg_addr (this_cache, PPC_ORIG_R3_REGNUM,
569                                gpregs + 34 * tdep->wordsize);
570       trad_frame_set_reg_addr (this_cache, PPC_TRAP_REGNUM,
571                                gpregs + 40 * tdep->wordsize);
572     }
573
574   if (ppc_floating_point_unit_p (gdbarch))
575     {
576       /* Floating point registers.  */
577       for (i = 0; i < 32; i++)
578         {
579           int regnum = i + gdbarch_fp0_regnum (gdbarch);
580           trad_frame_set_reg_addr (this_cache, regnum,
581                                    fpregs + i * tdep->wordsize);
582         }
583       trad_frame_set_reg_addr (this_cache, tdep->ppc_fpscr_regnum,
584                          fpregs + 32 * tdep->wordsize);
585     }
586   trad_frame_set_id (this_cache, frame_id_build (base, func));
587 }
588
589 static void
590 ppc32_linux_sigaction_cache_init (const struct tramp_frame *self,
591                                   struct frame_info *this_frame,
592                                   struct trad_frame_cache *this_cache,
593                                   CORE_ADDR func)
594 {
595   ppc_linux_sigtramp_cache (this_frame, this_cache, func,
596                             0xd0 /* Offset to ucontext_t.  */
597                             + 0x30 /* Offset to .reg.  */,
598                             0);
599 }
600
601 static void
602 ppc64_linux_sigaction_cache_init (const struct tramp_frame *self,
603                                   struct frame_info *this_frame,
604                                   struct trad_frame_cache *this_cache,
605                                   CORE_ADDR func)
606 {
607   ppc_linux_sigtramp_cache (this_frame, this_cache, func,
608                             0x80 /* Offset to ucontext_t.  */
609                             + 0xe0 /* Offset to .reg.  */,
610                             128);
611 }
612
613 static void
614 ppc32_linux_sighandler_cache_init (const struct tramp_frame *self,
615                                    struct frame_info *this_frame,
616                                    struct trad_frame_cache *this_cache,
617                                    CORE_ADDR func)
618 {
619   ppc_linux_sigtramp_cache (this_frame, this_cache, func,
620                             0x40 /* Offset to ucontext_t.  */
621                             + 0x1c /* Offset to .reg.  */,
622                             0);
623 }
624
625 static void
626 ppc64_linux_sighandler_cache_init (const struct tramp_frame *self,
627                                    struct frame_info *this_frame,
628                                    struct trad_frame_cache *this_cache,
629                                    CORE_ADDR func)
630 {
631   ppc_linux_sigtramp_cache (this_frame, this_cache, func,
632                             0x80 /* Offset to struct sigcontext.  */
633                             + 0x38 /* Offset to .reg.  */,
634                             128);
635 }
636
637 static struct tramp_frame ppc32_linux_sigaction_tramp_frame = {
638   SIGTRAMP_FRAME,
639   4,
640   { 
641     { 0x380000ac, -1 }, /* li r0, 172 */
642     { 0x44000002, -1 }, /* sc */
643     { TRAMP_SENTINEL_INSN },
644   },
645   ppc32_linux_sigaction_cache_init
646 };
647 static struct tramp_frame ppc64_linux_sigaction_tramp_frame = {
648   SIGTRAMP_FRAME,
649   4,
650   {
651     { 0x38210080, -1 }, /* addi r1,r1,128 */
652     { 0x380000ac, -1 }, /* li r0, 172 */
653     { 0x44000002, -1 }, /* sc */
654     { TRAMP_SENTINEL_INSN },
655   },
656   ppc64_linux_sigaction_cache_init
657 };
658 static struct tramp_frame ppc32_linux_sighandler_tramp_frame = {
659   SIGTRAMP_FRAME,
660   4,
661   { 
662     { 0x38000077, -1 }, /* li r0,119 */
663     { 0x44000002, -1 }, /* sc */
664     { TRAMP_SENTINEL_INSN },
665   },
666   ppc32_linux_sighandler_cache_init
667 };
668 static struct tramp_frame ppc64_linux_sighandler_tramp_frame = {
669   SIGTRAMP_FRAME,
670   4,
671   { 
672     { 0x38210080, -1 }, /* addi r1,r1,128 */
673     { 0x38000077, -1 }, /* li r0,119 */
674     { 0x44000002, -1 }, /* sc */
675     { TRAMP_SENTINEL_INSN },
676   },
677   ppc64_linux_sighandler_cache_init
678 };
679
680
681 /* Address to use for displaced stepping.  When debugging a stand-alone
682    SPU executable, entry_point_address () will point to an SPU local-store
683    address and is thus not usable as displaced stepping location.  We use
684    the auxiliary vector to determine the PowerPC-side entry point address
685    instead.  */
686
687 static CORE_ADDR ppc_linux_entry_point_addr = 0;
688
689 static void
690 ppc_linux_inferior_created (struct target_ops *target, int from_tty)
691 {
692   ppc_linux_entry_point_addr = 0;
693 }
694
695 static CORE_ADDR
696 ppc_linux_displaced_step_location (struct gdbarch *gdbarch)
697 {
698   if (ppc_linux_entry_point_addr == 0)
699     {
700       CORE_ADDR addr;
701
702       /* Determine entry point from target auxiliary vector.  */
703       if (target_auxv_search (&current_target, AT_ENTRY, &addr) <= 0)
704         error (_("Cannot find AT_ENTRY auxiliary vector entry."));
705
706       /* Make certain that the address points at real code, and not a
707          function descriptor.  */
708       addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
709                                                  &current_target);
710
711       /* Inferior calls also use the entry point as a breakpoint location.
712          We don't want displaced stepping to interfere with those
713          breakpoints, so leave space.  */
714       ppc_linux_entry_point_addr = addr + 2 * PPC_INSN_SIZE;
715     }
716
717   return ppc_linux_entry_point_addr;
718 }
719
720
721 /* Return 1 if PPC_ORIG_R3_REGNUM and PPC_TRAP_REGNUM are usable.  */
722 int
723 ppc_linux_trap_reg_p (struct gdbarch *gdbarch)
724 {
725   /* If we do not have a target description with registers, then
726      the special registers will not be included in the register set.  */
727   if (!tdesc_has_registers (gdbarch_target_desc (gdbarch)))
728     return 0;
729
730   /* If we do, then it is safe to check the size.  */
731   return register_size (gdbarch, PPC_ORIG_R3_REGNUM) > 0
732          && register_size (gdbarch, PPC_TRAP_REGNUM) > 0;
733 }
734
735 /* Return the current system call's number present in the
736    r0 register.  When the function fails, it returns -1.  */
737 static LONGEST
738 ppc_linux_get_syscall_number (struct gdbarch *gdbarch,
739                               ptid_t ptid)
740 {
741   struct regcache *regcache = get_thread_regcache (ptid);
742   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
743   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
744   struct cleanup *cleanbuf;
745   /* The content of a register */
746   gdb_byte *buf;
747   /* The result */
748   LONGEST ret;
749
750   /* Make sure we're in a 32- or 64-bit machine */
751   gdb_assert (tdep->wordsize == 4 || tdep->wordsize == 8);
752
753   buf = (gdb_byte *) xmalloc (tdep->wordsize * sizeof (gdb_byte));
754
755   cleanbuf = make_cleanup (xfree, buf);
756
757   /* Getting the system call number from the register.
758      When dealing with PowerPC architecture, this information
759      is stored at 0th register.  */
760   regcache_cooked_read (regcache, tdep->ppc_gp0_regnum, buf);
761
762   ret = extract_signed_integer (buf, tdep->wordsize, byte_order);
763   do_cleanups (cleanbuf);
764
765   return ret;
766 }
767
768 static void
769 ppc_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
770 {
771   struct gdbarch *gdbarch = get_regcache_arch (regcache);
772
773   regcache_cooked_write_unsigned (regcache, gdbarch_pc_regnum (gdbarch), pc);
774
775   /* Set special TRAP register to -1 to prevent the kernel from
776      messing with the PC we just installed, if we happen to be
777      within an interrupted system call that the kernel wants to
778      restart.
779
780      Note that after we return from the dummy call, the TRAP and
781      ORIG_R3 registers will be automatically restored, and the
782      kernel continues to restart the system call at this point.  */
783   if (ppc_linux_trap_reg_p (gdbarch))
784     regcache_cooked_write_unsigned (regcache, PPC_TRAP_REGNUM, -1);
785 }
786
787 static int
788 ppc_linux_spu_section (bfd *abfd, asection *asect, void *user_data)
789 {
790   return strncmp (bfd_section_name (abfd, asect), "SPU/", 4) == 0;
791 }
792
793 static const struct target_desc *
794 ppc_linux_core_read_description (struct gdbarch *gdbarch,
795                                  struct target_ops *target,
796                                  bfd *abfd)
797 {
798   asection *cell = bfd_sections_find_if (abfd, ppc_linux_spu_section, NULL);
799   asection *altivec = bfd_get_section_by_name (abfd, ".reg-ppc-vmx");
800   asection *vsx = bfd_get_section_by_name (abfd, ".reg-ppc-vsx");
801   asection *section = bfd_get_section_by_name (abfd, ".reg");
802   if (! section)
803     return NULL;
804
805   switch (bfd_section_size (abfd, section))
806     {
807     case 48 * 4:
808       if (cell)
809         return tdesc_powerpc_cell32l;
810       else if (vsx)
811         return tdesc_powerpc_vsx32l;
812       else if (altivec)
813         return tdesc_powerpc_altivec32l;
814       else
815         return tdesc_powerpc_32l;
816
817     case 48 * 8:
818       if (cell)
819         return tdesc_powerpc_cell64l;
820       else if (vsx)
821         return tdesc_powerpc_vsx64l;
822       else if (altivec)
823         return tdesc_powerpc_altivec64l;
824       else
825         return tdesc_powerpc_64l;
826
827     default:
828       return NULL;
829     }
830 }
831
832
833 /* Implementation of `gdbarch_elf_make_msymbol_special', as defined in
834    gdbarch.h.  This implementation is used for the ELFv2 ABI only.  */
835
836 static void
837 ppc_elfv2_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
838 {
839   elf_symbol_type *elf_sym = (elf_symbol_type *)sym;
840
841   /* If the symbol is marked as having a local entry point, set a target
842      flag in the msymbol.  We currently only support local entry point
843      offsets of 8 bytes, which is the only entry point offset ever used
844      by current compilers.  If/when other offsets are ever used, we will
845      have to use additional target flag bits to store them.  */
846   switch (PPC64_LOCAL_ENTRY_OFFSET (elf_sym->internal_elf_sym.st_other))
847     {
848     default:
849       break;
850     case 8:
851       MSYMBOL_TARGET_FLAG_1 (msym) = 1;
852       break;
853     }
854 }
855
856 /* Implementation of `gdbarch_skip_entrypoint', as defined in
857    gdbarch.h.  This implementation is used for the ELFv2 ABI only.  */
858
859 static CORE_ADDR
860 ppc_elfv2_skip_entrypoint (struct gdbarch *gdbarch, CORE_ADDR pc)
861 {
862   struct bound_minimal_symbol fun;
863   int local_entry_offset = 0;
864
865   fun = lookup_minimal_symbol_by_pc (pc);
866   if (fun.minsym == NULL)
867     return pc;
868
869   /* See ppc_elfv2_elf_make_msymbol_special for how local entry point
870      offset values are encoded.  */
871   if (MSYMBOL_TARGET_FLAG_1 (fun.minsym))
872     local_entry_offset = 8;
873
874   if (BMSYMBOL_VALUE_ADDRESS (fun) <= pc
875       && pc < BMSYMBOL_VALUE_ADDRESS (fun) + local_entry_offset)
876     return BMSYMBOL_VALUE_ADDRESS (fun) + local_entry_offset;
877
878   return pc;
879 }
880
881 /* Implementation of `gdbarch_stap_is_single_operand', as defined in
882    gdbarch.h.  */
883
884 static int
885 ppc_stap_is_single_operand (struct gdbarch *gdbarch, const char *s)
886 {
887   return (*s == 'i' /* Literal number.  */
888           || (isdigit (*s) && s[1] == '('
889               && isdigit (s[2])) /* Displacement.  */
890           || (*s == '(' && isdigit (s[1])) /* Register indirection.  */
891           || isdigit (*s)); /* Register value.  */
892 }
893
894 /* Implementation of `gdbarch_stap_parse_special_token', as defined in
895    gdbarch.h.  */
896
897 static int
898 ppc_stap_parse_special_token (struct gdbarch *gdbarch,
899                               struct stap_parse_info *p)
900 {
901   if (isdigit (*p->arg))
902     {
903       /* This temporary pointer is needed because we have to do a lookahead.
904           We could be dealing with a register displacement, and in such case
905           we would not need to do anything.  */
906       const char *s = p->arg;
907       char *regname;
908       int len;
909       struct stoken str;
910
911       while (isdigit (*s))
912         ++s;
913
914       if (*s == '(')
915         {
916           /* It is a register displacement indeed.  Returning 0 means we are
917              deferring the treatment of this case to the generic parser.  */
918           return 0;
919         }
920
921       len = s - p->arg;
922       regname = alloca (len + 2);
923       regname[0] = 'r';
924
925       strncpy (regname + 1, p->arg, len);
926       ++len;
927       regname[len] = '\0';
928
929       if (user_reg_map_name_to_regnum (gdbarch, regname, len) == -1)
930         error (_("Invalid register name `%s' on expression `%s'."),
931                regname, p->saved_arg);
932
933       write_exp_elt_opcode (&p->pstate, OP_REGISTER);
934       str.ptr = regname;
935       str.length = len;
936       write_exp_string (&p->pstate, str);
937       write_exp_elt_opcode (&p->pstate, OP_REGISTER);
938
939       p->arg = s;
940     }
941   else
942     {
943       /* All the other tokens should be handled correctly by the generic
944          parser.  */
945       return 0;
946     }
947
948   return 1;
949 }
950
951 /* Cell/B.E. active SPE context tracking support.  */
952
953 static struct objfile *spe_context_objfile = NULL;
954 static CORE_ADDR spe_context_lm_addr = 0;
955 static CORE_ADDR spe_context_offset = 0;
956
957 static ptid_t spe_context_cache_ptid;
958 static CORE_ADDR spe_context_cache_address;
959
960 /* Hook into inferior_created, solib_loaded, and solib_unloaded observers
961    to track whether we've loaded a version of libspe2 (as static or dynamic
962    library) that provides the __spe_current_active_context variable.  */
963 static void
964 ppc_linux_spe_context_lookup (struct objfile *objfile)
965 {
966   struct bound_minimal_symbol sym;
967
968   if (!objfile)
969     {
970       spe_context_objfile = NULL;
971       spe_context_lm_addr = 0;
972       spe_context_offset = 0;
973       spe_context_cache_ptid = minus_one_ptid;
974       spe_context_cache_address = 0;
975       return;
976     }
977
978   sym = lookup_minimal_symbol ("__spe_current_active_context", NULL, objfile);
979   if (sym.minsym)
980     {
981       spe_context_objfile = objfile;
982       spe_context_lm_addr = svr4_fetch_objfile_link_map (objfile);
983       spe_context_offset = BMSYMBOL_VALUE_ADDRESS (sym);
984       spe_context_cache_ptid = minus_one_ptid;
985       spe_context_cache_address = 0;
986       return;
987     }
988 }
989
990 static void
991 ppc_linux_spe_context_inferior_created (struct target_ops *t, int from_tty)
992 {
993   struct objfile *objfile;
994
995   ppc_linux_spe_context_lookup (NULL);
996   ALL_OBJFILES (objfile)
997     ppc_linux_spe_context_lookup (objfile);
998 }
999
1000 static void
1001 ppc_linux_spe_context_solib_loaded (struct so_list *so)
1002 {
1003   if (strstr (so->so_original_name, "/libspe") != NULL)
1004     {
1005       solib_read_symbols (so, 0);
1006       ppc_linux_spe_context_lookup (so->objfile);
1007     }
1008 }
1009
1010 static void
1011 ppc_linux_spe_context_solib_unloaded (struct so_list *so)
1012 {
1013   if (so->objfile == spe_context_objfile)
1014     ppc_linux_spe_context_lookup (NULL);
1015 }
1016
1017 /* Retrieve contents of the N'th element in the current thread's
1018    linked SPE context list into ID and NPC.  Return the address of
1019    said context element, or 0 if not found.  */
1020 static CORE_ADDR
1021 ppc_linux_spe_context (int wordsize, enum bfd_endian byte_order,
1022                        int n, int *id, unsigned int *npc)
1023 {
1024   CORE_ADDR spe_context = 0;
1025   gdb_byte buf[16];
1026   int i;
1027
1028   /* Quick exit if we have not found __spe_current_active_context.  */
1029   if (!spe_context_objfile)
1030     return 0;
1031
1032   /* Look up cached address of thread-local variable.  */
1033   if (!ptid_equal (spe_context_cache_ptid, inferior_ptid))
1034     {
1035       struct target_ops *target = &current_target;
1036       volatile struct gdb_exception ex;
1037
1038       TRY_CATCH (ex, RETURN_MASK_ERROR)
1039         {
1040           /* We do not call target_translate_tls_address here, because
1041              svr4_fetch_objfile_link_map may invalidate the frame chain,
1042              which must not do while inside a frame sniffer.
1043
1044              Instead, we have cached the lm_addr value, and use that to
1045              directly call the target's to_get_thread_local_address.  */
1046           spe_context_cache_address
1047             = target->to_get_thread_local_address (target, inferior_ptid,
1048                                                    spe_context_lm_addr,
1049                                                    spe_context_offset);
1050           spe_context_cache_ptid = inferior_ptid;
1051         }
1052
1053       if (ex.reason < 0)
1054         return 0;
1055     }
1056
1057   /* Read variable value.  */
1058   if (target_read_memory (spe_context_cache_address, buf, wordsize) == 0)
1059     spe_context = extract_unsigned_integer (buf, wordsize, byte_order);
1060
1061   /* Cyle through to N'th linked list element.  */
1062   for (i = 0; i < n && spe_context; i++)
1063     if (target_read_memory (spe_context + align_up (12, wordsize),
1064                             buf, wordsize) == 0)
1065       spe_context = extract_unsigned_integer (buf, wordsize, byte_order);
1066     else
1067       spe_context = 0;
1068
1069   /* Read current context.  */
1070   if (spe_context
1071       && target_read_memory (spe_context, buf, 12) != 0)
1072     spe_context = 0;
1073
1074   /* Extract data elements.  */
1075   if (spe_context)
1076     {
1077       if (id)
1078         *id = extract_signed_integer (buf, 4, byte_order);
1079       if (npc)
1080         *npc = extract_unsigned_integer (buf + 4, 4, byte_order);
1081     }
1082
1083   return spe_context;
1084 }
1085
1086
1087 /* Cell/B.E. cross-architecture unwinder support.  */
1088
1089 struct ppu2spu_cache
1090 {
1091   struct frame_id frame_id;
1092   struct regcache *regcache;
1093 };
1094
1095 static struct gdbarch *
1096 ppu2spu_prev_arch (struct frame_info *this_frame, void **this_cache)
1097 {
1098   struct ppu2spu_cache *cache = *this_cache;
1099   return get_regcache_arch (cache->regcache);
1100 }
1101
1102 static void
1103 ppu2spu_this_id (struct frame_info *this_frame,
1104                  void **this_cache, struct frame_id *this_id)
1105 {
1106   struct ppu2spu_cache *cache = *this_cache;
1107   *this_id = cache->frame_id;
1108 }
1109
1110 static struct value *
1111 ppu2spu_prev_register (struct frame_info *this_frame,
1112                        void **this_cache, int regnum)
1113 {
1114   struct ppu2spu_cache *cache = *this_cache;
1115   struct gdbarch *gdbarch = get_regcache_arch (cache->regcache);
1116   gdb_byte *buf;
1117
1118   buf = alloca (register_size (gdbarch, regnum));
1119
1120   if (regnum < gdbarch_num_regs (gdbarch))
1121     regcache_raw_read (cache->regcache, regnum, buf);
1122   else
1123     gdbarch_pseudo_register_read (gdbarch, cache->regcache, regnum, buf);
1124
1125   return frame_unwind_got_bytes (this_frame, regnum, buf);
1126 }
1127
1128 struct ppu2spu_data
1129 {
1130   struct gdbarch *gdbarch;
1131   int id;
1132   unsigned int npc;
1133   gdb_byte gprs[128*16];
1134 };
1135
1136 static int
1137 ppu2spu_unwind_register (void *src, int regnum, gdb_byte *buf)
1138 {
1139   struct ppu2spu_data *data = src;
1140   enum bfd_endian byte_order = gdbarch_byte_order (data->gdbarch);
1141
1142   if (regnum >= 0 && regnum < SPU_NUM_GPRS)
1143     memcpy (buf, data->gprs + 16*regnum, 16);
1144   else if (regnum == SPU_ID_REGNUM)
1145     store_unsigned_integer (buf, 4, byte_order, data->id);
1146   else if (regnum == SPU_PC_REGNUM)
1147     store_unsigned_integer (buf, 4, byte_order, data->npc);
1148   else
1149     return REG_UNAVAILABLE;
1150
1151   return REG_VALID;
1152 }
1153
1154 static int
1155 ppu2spu_sniffer (const struct frame_unwind *self,
1156                  struct frame_info *this_frame, void **this_prologue_cache)
1157 {
1158   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1159   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1160   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1161   struct ppu2spu_data data;
1162   struct frame_info *fi;
1163   CORE_ADDR base, func, backchain, spe_context;
1164   gdb_byte buf[8];
1165   int n = 0;
1166
1167   /* Count the number of SPU contexts already in the frame chain.  */
1168   for (fi = get_next_frame (this_frame); fi; fi = get_next_frame (fi))
1169     if (get_frame_type (fi) == ARCH_FRAME
1170         && gdbarch_bfd_arch_info (get_frame_arch (fi))->arch == bfd_arch_spu)
1171       n++;
1172
1173   base = get_frame_sp (this_frame);
1174   func = get_frame_pc (this_frame);
1175   if (target_read_memory (base, buf, tdep->wordsize))
1176     return 0;
1177   backchain = extract_unsigned_integer (buf, tdep->wordsize, byte_order);
1178
1179   spe_context = ppc_linux_spe_context (tdep->wordsize, byte_order,
1180                                        n, &data.id, &data.npc);
1181   if (spe_context && base <= spe_context && spe_context < backchain)
1182     {
1183       char annex[32];
1184
1185       /* Find gdbarch for SPU.  */
1186       struct gdbarch_info info;
1187       gdbarch_info_init (&info);
1188       info.bfd_arch_info = bfd_lookup_arch (bfd_arch_spu, bfd_mach_spu);
1189       info.byte_order = BFD_ENDIAN_BIG;
1190       info.osabi = GDB_OSABI_LINUX;
1191       info.tdep_info = (void *) &data.id;
1192       data.gdbarch = gdbarch_find_by_info (info);
1193       if (!data.gdbarch)
1194         return 0;
1195
1196       xsnprintf (annex, sizeof annex, "%d/regs", data.id);
1197       if (target_read (&current_target, TARGET_OBJECT_SPU, annex,
1198                        data.gprs, 0, sizeof data.gprs)
1199           == sizeof data.gprs)
1200         {
1201           struct ppu2spu_cache *cache
1202             = FRAME_OBSTACK_CALLOC (1, struct ppu2spu_cache);
1203
1204           struct address_space *aspace = get_frame_address_space (this_frame);
1205           struct regcache *regcache = regcache_xmalloc (data.gdbarch, aspace);
1206           struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache);
1207           regcache_save (regcache, ppu2spu_unwind_register, &data);
1208           discard_cleanups (cleanups);
1209
1210           cache->frame_id = frame_id_build (base, func);
1211           cache->regcache = regcache;
1212           *this_prologue_cache = cache;
1213           return 1;
1214         }
1215     }
1216
1217   return 0;
1218 }
1219
1220 static void
1221 ppu2spu_dealloc_cache (struct frame_info *self, void *this_cache)
1222 {
1223   struct ppu2spu_cache *cache = this_cache;
1224   regcache_xfree (cache->regcache);
1225 }
1226
1227 static const struct frame_unwind ppu2spu_unwind = {
1228   ARCH_FRAME,
1229   default_frame_unwind_stop_reason,
1230   ppu2spu_this_id,
1231   ppu2spu_prev_register,
1232   NULL,
1233   ppu2spu_sniffer,
1234   ppu2spu_dealloc_cache,
1235   ppu2spu_prev_arch,
1236 };
1237
1238
1239 static void
1240 ppc_linux_init_abi (struct gdbarch_info info,
1241                     struct gdbarch *gdbarch)
1242 {
1243   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1244   struct tdesc_arch_data *tdesc_data = (void *) info.tdep_info;
1245   static const char *const stap_integer_prefixes[] = { "i", NULL };
1246   static const char *const stap_register_indirection_prefixes[] = { "(",
1247                                                                     NULL };
1248   static const char *const stap_register_indirection_suffixes[] = { ")",
1249                                                                     NULL };
1250
1251   linux_init_abi (info, gdbarch);
1252
1253   /* PPC GNU/Linux uses either 64-bit or 128-bit long doubles; where
1254      128-bit, they are IBM long double, not IEEE quad long double as
1255      in the System V ABI PowerPC Processor Supplement.  We can safely
1256      let them default to 128-bit, since the debug info will give the
1257      size of type actually used in each case.  */
1258   set_gdbarch_long_double_bit (gdbarch, 16 * TARGET_CHAR_BIT);
1259   set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);
1260
1261   /* Handle inferior calls during interrupted system calls.  */
1262   set_gdbarch_write_pc (gdbarch, ppc_linux_write_pc);
1263
1264   /* Get the syscall number from the arch's register.  */
1265   set_gdbarch_get_syscall_number (gdbarch, ppc_linux_get_syscall_number);
1266
1267   /* SystemTap functions.  */
1268   set_gdbarch_stap_integer_prefixes (gdbarch, stap_integer_prefixes);
1269   set_gdbarch_stap_register_indirection_prefixes (gdbarch,
1270                                           stap_register_indirection_prefixes);
1271   set_gdbarch_stap_register_indirection_suffixes (gdbarch,
1272                                           stap_register_indirection_suffixes);
1273   set_gdbarch_stap_gdb_register_prefix (gdbarch, "r");
1274   set_gdbarch_stap_is_single_operand (gdbarch, ppc_stap_is_single_operand);
1275   set_gdbarch_stap_parse_special_token (gdbarch,
1276                                         ppc_stap_parse_special_token);
1277
1278   if (tdep->wordsize == 4)
1279     {
1280       /* Until November 2001, gcc did not comply with the 32 bit SysV
1281          R4 ABI requirement that structures less than or equal to 8
1282          bytes should be returned in registers.  Instead GCC was using
1283          the AIX/PowerOpen ABI - everything returned in memory
1284          (well ignoring vectors that is).  When this was corrected, it
1285          wasn't fixed for GNU/Linux native platform.  Use the
1286          PowerOpen struct convention.  */
1287       set_gdbarch_return_value (gdbarch, ppc_linux_return_value);
1288
1289       set_gdbarch_memory_remove_breakpoint (gdbarch,
1290                                             ppc_linux_memory_remove_breakpoint);
1291
1292       /* Shared library handling.  */
1293       set_gdbarch_skip_trampoline_code (gdbarch, ppc_skip_trampoline_code);
1294       set_solib_svr4_fetch_link_map_offsets
1295         (gdbarch, svr4_ilp32_fetch_link_map_offsets);
1296
1297       /* Setting the correct XML syscall filename.  */
1298       set_xml_syscall_file_name (XML_SYSCALL_FILENAME_PPC);
1299
1300       /* Trampolines.  */
1301       tramp_frame_prepend_unwinder (gdbarch,
1302                                     &ppc32_linux_sigaction_tramp_frame);
1303       tramp_frame_prepend_unwinder (gdbarch,
1304                                     &ppc32_linux_sighandler_tramp_frame);
1305
1306       /* BFD target for core files.  */
1307       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
1308         set_gdbarch_gcore_bfd_target (gdbarch, "elf32-powerpcle");
1309       else
1310         set_gdbarch_gcore_bfd_target (gdbarch, "elf32-powerpc");
1311
1312       if (powerpc_so_ops.in_dynsym_resolve_code == NULL)
1313         {
1314           powerpc_so_ops = svr4_so_ops;
1315           /* Override dynamic resolve function.  */
1316           powerpc_so_ops.in_dynsym_resolve_code =
1317             powerpc_linux_in_dynsym_resolve_code;
1318         }
1319       set_solib_ops (gdbarch, &powerpc_so_ops);
1320
1321       set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
1322     }
1323   
1324   if (tdep->wordsize == 8)
1325     {
1326       if (tdep->elf_abi == POWERPC_ELF_V1)
1327         {
1328           /* Handle PPC GNU/Linux 64-bit function pointers (which are really
1329              function descriptors).  */
1330           set_gdbarch_convert_from_func_ptr_addr
1331             (gdbarch, ppc64_convert_from_func_ptr_addr);
1332
1333           set_gdbarch_elf_make_msymbol_special
1334             (gdbarch, ppc64_elf_make_msymbol_special);
1335         }
1336       else
1337         {
1338           set_gdbarch_elf_make_msymbol_special
1339             (gdbarch, ppc_elfv2_elf_make_msymbol_special);
1340
1341           set_gdbarch_skip_entrypoint (gdbarch, ppc_elfv2_skip_entrypoint);
1342         }
1343
1344       /* Shared library handling.  */
1345       set_gdbarch_skip_trampoline_code (gdbarch, ppc64_skip_trampoline_code);
1346       set_solib_svr4_fetch_link_map_offsets
1347         (gdbarch, svr4_lp64_fetch_link_map_offsets);
1348
1349       /* Setting the correct XML syscall filename.  */
1350       set_xml_syscall_file_name (XML_SYSCALL_FILENAME_PPC64);
1351
1352       /* Trampolines.  */
1353       tramp_frame_prepend_unwinder (gdbarch,
1354                                     &ppc64_linux_sigaction_tramp_frame);
1355       tramp_frame_prepend_unwinder (gdbarch,
1356                                     &ppc64_linux_sighandler_tramp_frame);
1357
1358       /* BFD target for core files.  */
1359       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
1360         set_gdbarch_gcore_bfd_target (gdbarch, "elf64-powerpcle");
1361       else
1362         set_gdbarch_gcore_bfd_target (gdbarch, "elf64-powerpc");
1363     }
1364
1365   /* PPC32 uses a different prpsinfo32 compared to most other Linux
1366      archs.  */
1367   if (tdep->wordsize == 4)
1368     set_gdbarch_elfcore_write_linux_prpsinfo (gdbarch,
1369                                               elfcore_write_ppc_linux_prpsinfo32);
1370
1371   set_gdbarch_core_read_description (gdbarch, ppc_linux_core_read_description);
1372   set_gdbarch_iterate_over_regset_sections (gdbarch,
1373                                             ppc_linux_iterate_over_regset_sections);
1374
1375   /* Enable TLS support.  */
1376   set_gdbarch_fetch_tls_load_module_address (gdbarch,
1377                                              svr4_fetch_objfile_link_map);
1378
1379   if (tdesc_data)
1380     {
1381       const struct tdesc_feature *feature;
1382
1383       /* If we have target-described registers, then we can safely
1384          reserve a number for PPC_ORIG_R3_REGNUM and PPC_TRAP_REGNUM
1385          (whether they are described or not).  */
1386       gdb_assert (gdbarch_num_regs (gdbarch) <= PPC_ORIG_R3_REGNUM);
1387       set_gdbarch_num_regs (gdbarch, PPC_TRAP_REGNUM + 1);
1388
1389       /* If they are present, then assign them to the reserved number.  */
1390       feature = tdesc_find_feature (info.target_desc,
1391                                     "org.gnu.gdb.power.linux");
1392       if (feature != NULL)
1393         {
1394           tdesc_numbered_register (feature, tdesc_data,
1395                                    PPC_ORIG_R3_REGNUM, "orig_r3");
1396           tdesc_numbered_register (feature, tdesc_data,
1397                                    PPC_TRAP_REGNUM, "trap");
1398         }
1399     }
1400
1401   /* Enable Cell/B.E. if supported by the target.  */
1402   if (tdesc_compatible_p (info.target_desc,
1403                           bfd_lookup_arch (bfd_arch_spu, bfd_mach_spu)))
1404     {
1405       /* Cell/B.E. multi-architecture support.  */
1406       set_spu_solib_ops (gdbarch);
1407
1408       /* Cell/B.E. cross-architecture unwinder support.  */
1409       frame_unwind_prepend_unwinder (gdbarch, &ppu2spu_unwind);
1410
1411       /* The default displaced_step_at_entry_point doesn't work for
1412          SPU stand-alone executables.  */
1413       set_gdbarch_displaced_step_location (gdbarch,
1414                                            ppc_linux_displaced_step_location);
1415     }
1416
1417   set_gdbarch_get_siginfo_type (gdbarch, linux_get_siginfo_type);
1418 }
1419
1420 /* Provide a prototype to silence -Wmissing-prototypes.  */
1421 extern initialize_file_ftype _initialize_ppc_linux_tdep;
1422
1423 void
1424 _initialize_ppc_linux_tdep (void)
1425 {
1426   /* Register for all sub-familes of the POWER/PowerPC: 32-bit and
1427      64-bit PowerPC, and the older rs6k.  */
1428   gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc, GDB_OSABI_LINUX,
1429                          ppc_linux_init_abi);
1430   gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc64, GDB_OSABI_LINUX,
1431                          ppc_linux_init_abi);
1432   gdbarch_register_osabi (bfd_arch_rs6000, bfd_mach_rs6k, GDB_OSABI_LINUX,
1433                          ppc_linux_init_abi);
1434
1435   /* Attach to inferior_created observer.  */
1436   observer_attach_inferior_created (ppc_linux_inferior_created);
1437
1438   /* Attach to observers to track __spe_current_active_context.  */
1439   observer_attach_inferior_created (ppc_linux_spe_context_inferior_created);
1440   observer_attach_solib_loaded (ppc_linux_spe_context_solib_loaded);
1441   observer_attach_solib_unloaded (ppc_linux_spe_context_solib_unloaded);
1442
1443   /* Initialize the Linux target descriptions.  */
1444   initialize_tdesc_powerpc_32l ();
1445   initialize_tdesc_powerpc_altivec32l ();
1446   initialize_tdesc_powerpc_cell32l ();
1447   initialize_tdesc_powerpc_vsx32l ();
1448   initialize_tdesc_powerpc_isa205_32l ();
1449   initialize_tdesc_powerpc_isa205_altivec32l ();
1450   initialize_tdesc_powerpc_isa205_vsx32l ();
1451   initialize_tdesc_powerpc_64l ();
1452   initialize_tdesc_powerpc_altivec64l ();
1453   initialize_tdesc_powerpc_cell64l ();
1454   initialize_tdesc_powerpc_vsx64l ();
1455   initialize_tdesc_powerpc_isa205_64l ();
1456   initialize_tdesc_powerpc_isa205_altivec64l ();
1457   initialize_tdesc_powerpc_isa205_vsx64l ();
1458   initialize_tdesc_powerpc_e500l ();
1459 }