OSDN Git Service

Switch the license of all .c files to GPLv3.
[pf3gnuchains/pf3gnuchains4x.git] / gdb / ppc-linux-tdep.c
1 /* Target-dependent code for GDB, the GNU debugger.
2
3    Copyright (C) 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
5    Free Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "frame.h"
24 #include "inferior.h"
25 #include "symtab.h"
26 #include "target.h"
27 #include "gdbcore.h"
28 #include "gdbcmd.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "regcache.h"
32 #include "value.h"
33 #include "osabi.h"
34 #include "regset.h"
35 #include "solib-svr4.h"
36 #include "ppc-tdep.h"
37 #include "trad-frame.h"
38 #include "frame-unwind.h"
39 #include "tramp-frame.h"
40
41 /* From <asm/ptrace.h>, values for PT_NIP, PT_R1, and PT_LNK */
42 #define PPC_LINUX_PT_R0         0
43 #define PPC_LINUX_PT_R1         1
44 #define PPC_LINUX_PT_R2         2
45 #define PPC_LINUX_PT_R3         3
46 #define PPC_LINUX_PT_R4         4
47 #define PPC_LINUX_PT_R5         5
48 #define PPC_LINUX_PT_R6         6
49 #define PPC_LINUX_PT_R7         7
50 #define PPC_LINUX_PT_R8         8
51 #define PPC_LINUX_PT_R9         9
52 #define PPC_LINUX_PT_R10        10
53 #define PPC_LINUX_PT_R11        11
54 #define PPC_LINUX_PT_R12        12
55 #define PPC_LINUX_PT_R13        13
56 #define PPC_LINUX_PT_R14        14
57 #define PPC_LINUX_PT_R15        15
58 #define PPC_LINUX_PT_R16        16
59 #define PPC_LINUX_PT_R17        17
60 #define PPC_LINUX_PT_R18        18
61 #define PPC_LINUX_PT_R19        19
62 #define PPC_LINUX_PT_R20        20
63 #define PPC_LINUX_PT_R21        21
64 #define PPC_LINUX_PT_R22        22
65 #define PPC_LINUX_PT_R23        23
66 #define PPC_LINUX_PT_R24        24
67 #define PPC_LINUX_PT_R25        25
68 #define PPC_LINUX_PT_R26        26
69 #define PPC_LINUX_PT_R27        27
70 #define PPC_LINUX_PT_R28        28
71 #define PPC_LINUX_PT_R29        29
72 #define PPC_LINUX_PT_R30        30
73 #define PPC_LINUX_PT_R31        31
74 #define PPC_LINUX_PT_NIP        32
75 #define PPC_LINUX_PT_MSR        33
76 #define PPC_LINUX_PT_CTR        35
77 #define PPC_LINUX_PT_LNK        36
78 #define PPC_LINUX_PT_XER        37
79 #define PPC_LINUX_PT_CCR        38
80 #define PPC_LINUX_PT_MQ         39
81 #define PPC_LINUX_PT_FPR0       48      /* each FP reg occupies 2 slots in this space */
82 #define PPC_LINUX_PT_FPR31 (PPC_LINUX_PT_FPR0 + 2*31)
83 #define PPC_LINUX_PT_FPSCR (PPC_LINUX_PT_FPR0 + 2*32 + 1)
84
85
86 static CORE_ADDR
87 ppc_linux_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
88 {
89   gdb_byte buf[4];
90   struct obj_section *sect;
91   struct objfile *objfile;
92   unsigned long insn;
93   CORE_ADDR plt_start = 0;
94   CORE_ADDR symtab = 0;
95   CORE_ADDR strtab = 0;
96   int num_slots = -1;
97   int reloc_index = -1;
98   CORE_ADDR plt_table;
99   CORE_ADDR reloc;
100   CORE_ADDR sym;
101   long symidx;
102   char symname[1024];
103   struct minimal_symbol *msymbol;
104
105   /* Find the section pc is in; return if not in .plt */
106   sect = find_pc_section (pc);
107   if (!sect || strcmp (sect->the_bfd_section->name, ".plt") != 0)
108     return 0;
109
110   objfile = sect->objfile;
111
112   /* Pick up the instruction at pc.  It had better be of the
113      form
114      li r11, IDX
115
116      where IDX is an index into the plt_table.  */
117
118   if (target_read_memory (pc, buf, 4) != 0)
119     return 0;
120   insn = extract_unsigned_integer (buf, 4);
121
122   if ((insn & 0xffff0000) != 0x39600000 /* li r11, VAL */ )
123     return 0;
124
125   reloc_index = (insn << 16) >> 16;
126
127   /* Find the objfile that pc is in and obtain the information
128      necessary for finding the symbol name. */
129   for (sect = objfile->sections; sect < objfile->sections_end; ++sect)
130     {
131       const char *secname = sect->the_bfd_section->name;
132       if (strcmp (secname, ".plt") == 0)
133         plt_start = sect->addr;
134       else if (strcmp (secname, ".rela.plt") == 0)
135         num_slots = ((int) sect->endaddr - (int) sect->addr) / 12;
136       else if (strcmp (secname, ".dynsym") == 0)
137         symtab = sect->addr;
138       else if (strcmp (secname, ".dynstr") == 0)
139         strtab = sect->addr;
140     }
141
142   /* Make sure we have all the information we need. */
143   if (plt_start == 0 || num_slots == -1 || symtab == 0 || strtab == 0)
144     return 0;
145
146   /* Compute the value of the plt table */
147   plt_table = plt_start + 72 + 8 * num_slots;
148
149   /* Get address of the relocation entry (Elf32_Rela) */
150   if (target_read_memory (plt_table + reloc_index, buf, 4) != 0)
151     return 0;
152   reloc = extract_unsigned_integer (buf, 4);
153
154   sect = find_pc_section (reloc);
155   if (!sect)
156     return 0;
157
158   if (strcmp (sect->the_bfd_section->name, ".text") == 0)
159     return reloc;
160
161   /* Now get the r_info field which is the relocation type and symbol
162      index. */
163   if (target_read_memory (reloc + 4, buf, 4) != 0)
164     return 0;
165   symidx = extract_unsigned_integer (buf, 4);
166
167   /* Shift out the relocation type leaving just the symbol index */
168   /* symidx = ELF32_R_SYM(symidx); */
169   symidx = symidx >> 8;
170
171   /* compute the address of the symbol */
172   sym = symtab + symidx * 4;
173
174   /* Fetch the string table index */
175   if (target_read_memory (sym, buf, 4) != 0)
176     return 0;
177   symidx = extract_unsigned_integer (buf, 4);
178
179   /* Fetch the string; we don't know how long it is.  Is it possible
180      that the following will fail because we're trying to fetch too
181      much? */
182   if (target_read_memory (strtab + symidx, (gdb_byte *) symname,
183                           sizeof (symname)) != 0)
184     return 0;
185
186   /* This might not work right if we have multiple symbols with the
187      same name; the only way to really get it right is to perform
188      the same sort of lookup as the dynamic linker. */
189   msymbol = lookup_minimal_symbol_text (symname, NULL);
190   if (!msymbol)
191     return 0;
192
193   return SYMBOL_VALUE_ADDRESS (msymbol);
194 }
195
196 /* ppc_linux_memory_remove_breakpoints attempts to remove a breakpoint
197    in much the same fashion as memory_remove_breakpoint in mem-break.c,
198    but is careful not to write back the previous contents if the code
199    in question has changed in between inserting the breakpoint and
200    removing it.
201
202    Here is the problem that we're trying to solve...
203
204    Once upon a time, before introducing this function to remove
205    breakpoints from the inferior, setting a breakpoint on a shared
206    library function prior to running the program would not work
207    properly.  In order to understand the problem, it is first
208    necessary to understand a little bit about dynamic linking on
209    this platform.
210
211    A call to a shared library function is accomplished via a bl
212    (branch-and-link) instruction whose branch target is an entry
213    in the procedure linkage table (PLT).  The PLT in the object
214    file is uninitialized.  To gdb, prior to running the program, the
215    entries in the PLT are all zeros.
216
217    Once the program starts running, the shared libraries are loaded
218    and the procedure linkage table is initialized, but the entries in
219    the table are not (necessarily) resolved.  Once a function is
220    actually called, the code in the PLT is hit and the function is
221    resolved.  In order to better illustrate this, an example is in
222    order; the following example is from the gdb testsuite.
223             
224         We start the program shmain.
225
226             [kev@arroyo testsuite]$ ../gdb gdb.base/shmain
227             [...]
228
229         We place two breakpoints, one on shr1 and the other on main.
230
231             (gdb) b shr1
232             Breakpoint 1 at 0x100409d4
233             (gdb) b main
234             Breakpoint 2 at 0x100006a0: file gdb.base/shmain.c, line 44.
235
236         Examine the instruction (and the immediatly following instruction)
237         upon which the breakpoint was placed.  Note that the PLT entry
238         for shr1 contains zeros.
239
240             (gdb) x/2i 0x100409d4
241             0x100409d4 <shr1>:      .long 0x0
242             0x100409d8 <shr1+4>:    .long 0x0
243
244         Now run 'til main.
245
246             (gdb) r
247             Starting program: gdb.base/shmain 
248             Breakpoint 1 at 0xffaf790: file gdb.base/shr1.c, line 19.
249
250             Breakpoint 2, main ()
251                 at gdb.base/shmain.c:44
252             44        g = 1;
253
254         Examine the PLT again.  Note that the loading of the shared
255         library has initialized the PLT to code which loads a constant
256         (which I think is an index into the GOT) into r11 and then
257         branchs a short distance to the code which actually does the
258         resolving.
259
260             (gdb) x/2i 0x100409d4
261             0x100409d4 <shr1>:      li      r11,4
262             0x100409d8 <shr1+4>:    b       0x10040984 <sg+4>
263             (gdb) c
264             Continuing.
265
266             Breakpoint 1, shr1 (x=1)
267                 at gdb.base/shr1.c:19
268             19        l = 1;
269
270         Now we've hit the breakpoint at shr1.  (The breakpoint was
271         reset from the PLT entry to the actual shr1 function after the
272         shared library was loaded.) Note that the PLT entry has been
273         resolved to contain a branch that takes us directly to shr1. 
274         (The real one, not the PLT entry.)
275
276             (gdb) x/2i 0x100409d4
277             0x100409d4 <shr1>:      b       0xffaf76c <shr1>
278             0x100409d8 <shr1+4>:    b       0x10040984 <sg+4>
279
280    The thing to note here is that the PLT entry for shr1 has been
281    changed twice.
282
283    Now the problem should be obvious.  GDB places a breakpoint (a
284    trap instruction) on the zero value of the PLT entry for shr1. 
285    Later on, after the shared library had been loaded and the PLT
286    initialized, GDB gets a signal indicating this fact and attempts
287    (as it always does when it stops) to remove all the breakpoints.
288
289    The breakpoint removal was causing the former contents (a zero
290    word) to be written back to the now initialized PLT entry thus
291    destroying a portion of the initialization that had occurred only a
292    short time ago.  When execution continued, the zero word would be
293    executed as an instruction an an illegal instruction trap was
294    generated instead.  (0 is not a legal instruction.)
295
296    The fix for this problem was fairly straightforward.  The function
297    memory_remove_breakpoint from mem-break.c was copied to this file,
298    modified slightly, and renamed to ppc_linux_memory_remove_breakpoint.
299    In tm-linux.h, MEMORY_REMOVE_BREAKPOINT is defined to call this new
300    function.
301
302    The differences between ppc_linux_memory_remove_breakpoint () and
303    memory_remove_breakpoint () are minor.  All that the former does
304    that the latter does not is check to make sure that the breakpoint
305    location actually contains a breakpoint (trap instruction) prior
306    to attempting to write back the old contents.  If it does contain
307    a trap instruction, we allow the old contents to be written back. 
308    Otherwise, we silently do nothing.
309
310    The big question is whether memory_remove_breakpoint () should be
311    changed to have the same functionality.  The downside is that more
312    traffic is generated for remote targets since we'll have an extra
313    fetch of a memory word each time a breakpoint is removed.
314
315    For the time being, we'll leave this self-modifying-code-friendly
316    version in ppc-linux-tdep.c, but it ought to be migrated somewhere
317    else in the event that some other platform has similar needs with
318    regard to removing breakpoints in some potentially self modifying
319    code.  */
320 int
321 ppc_linux_memory_remove_breakpoint (struct bp_target_info *bp_tgt)
322 {
323   CORE_ADDR addr = bp_tgt->placed_address;
324   const unsigned char *bp;
325   int val;
326   int bplen;
327   gdb_byte old_contents[BREAKPOINT_MAX];
328
329   /* Determine appropriate breakpoint contents and size for this address.  */
330   bp = gdbarch_breakpoint_from_pc (current_gdbarch, &addr, &bplen);
331   if (bp == NULL)
332     error (_("Software breakpoints not implemented for this target."));
333
334   val = target_read_memory (addr, old_contents, bplen);
335
336   /* If our breakpoint is no longer at the address, this means that the
337      program modified the code on us, so it is wrong to put back the
338      old value */
339   if (val == 0 && memcmp (bp, old_contents, bplen) == 0)
340     val = target_write_memory (addr, bp_tgt->shadow_contents, bplen);
341
342   return val;
343 }
344
345 /* For historic reasons, PPC 32 GNU/Linux follows PowerOpen rather
346    than the 32 bit SYSV R4 ABI structure return convention - all
347    structures, no matter their size, are put in memory.  Vectors,
348    which were added later, do get returned in a register though.  */
349
350 static enum return_value_convention
351 ppc_linux_return_value (struct gdbarch *gdbarch, struct type *valtype,
352                         struct regcache *regcache, gdb_byte *readbuf,
353                         const gdb_byte *writebuf)
354 {  
355   if ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
356        || TYPE_CODE (valtype) == TYPE_CODE_UNION)
357       && !((TYPE_LENGTH (valtype) == 16 || TYPE_LENGTH (valtype) == 8)
358            && TYPE_VECTOR (valtype)))
359     return RETURN_VALUE_STRUCT_CONVENTION;
360   else
361     return ppc_sysv_abi_return_value (gdbarch, valtype, regcache, readbuf,
362                                       writebuf);
363 }
364
365 /* Macros for matching instructions.  Note that, since all the
366    operands are masked off before they're or-ed into the instruction,
367    you can use -1 to make masks.  */
368
369 #define insn_d(opcd, rts, ra, d)                \
370   ((((opcd) & 0x3f) << 26)                      \
371    | (((rts) & 0x1f) << 21)                     \
372    | (((ra) & 0x1f) << 16)                      \
373    | ((d) & 0xffff))
374
375 #define insn_ds(opcd, rts, ra, d, xo)           \
376   ((((opcd) & 0x3f) << 26)                      \
377    | (((rts) & 0x1f) << 21)                     \
378    | (((ra) & 0x1f) << 16)                      \
379    | ((d) & 0xfffc)                             \
380    | ((xo) & 0x3))
381
382 #define insn_xfx(opcd, rts, spr, xo)            \
383   ((((opcd) & 0x3f) << 26)                      \
384    | (((rts) & 0x1f) << 21)                     \
385    | (((spr) & 0x1f) << 16)                     \
386    | (((spr) & 0x3e0) << 6)                     \
387    | (((xo) & 0x3ff) << 1))
388
389 /* Read a PPC instruction from memory.  PPC instructions are always
390    big-endian, no matter what endianness the program is running in, so
391    we can't use read_memory_integer or one of its friends here.  */
392 static unsigned int
393 read_insn (CORE_ADDR pc)
394 {
395   unsigned char buf[4];
396
397   read_memory (pc, buf, 4);
398   return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
399 }
400
401
402 /* An instruction to match.  */
403 struct insn_pattern
404 {
405   unsigned int mask;            /* mask the insn with this... */
406   unsigned int data;            /* ...and see if it matches this. */
407   int optional;                 /* If non-zero, this insn may be absent.  */
408 };
409
410 /* Return non-zero if the instructions at PC match the series
411    described in PATTERN, or zero otherwise.  PATTERN is an array of
412    'struct insn_pattern' objects, terminated by an entry whose mask is
413    zero.
414
415    When the match is successful, fill INSN[i] with what PATTERN[i]
416    matched.  If PATTERN[i] is optional, and the instruction wasn't
417    present, set INSN[i] to 0 (which is not a valid PPC instruction).
418    INSN should have as many elements as PATTERN.  Note that, if
419    PATTERN contains optional instructions which aren't present in
420    memory, then INSN will have holes, so INSN[i] isn't necessarily the
421    i'th instruction in memory.  */
422 static int
423 insns_match_pattern (CORE_ADDR pc,
424                      struct insn_pattern *pattern,
425                      unsigned int *insn)
426 {
427   int i;
428
429   for (i = 0; pattern[i].mask; i++)
430     {
431       insn[i] = read_insn (pc);
432       if ((insn[i] & pattern[i].mask) == pattern[i].data)
433         pc += 4;
434       else if (pattern[i].optional)
435         insn[i] = 0;
436       else
437         return 0;
438     }
439
440   return 1;
441 }
442
443
444 /* Return the 'd' field of the d-form instruction INSN, properly
445    sign-extended.  */
446 static CORE_ADDR
447 insn_d_field (unsigned int insn)
448 {
449   return ((((CORE_ADDR) insn & 0xffff) ^ 0x8000) - 0x8000);
450 }
451
452
453 /* Return the 'ds' field of the ds-form instruction INSN, with the two
454    zero bits concatenated at the right, and properly
455    sign-extended.  */
456 static CORE_ADDR
457 insn_ds_field (unsigned int insn)
458 {
459   return ((((CORE_ADDR) insn & 0xfffc) ^ 0x8000) - 0x8000);
460 }
461
462
463 /* If DESC is the address of a 64-bit PowerPC GNU/Linux function
464    descriptor, return the descriptor's entry point.  */
465 static CORE_ADDR
466 ppc64_desc_entry_point (CORE_ADDR desc)
467 {
468   /* The first word of the descriptor is the entry point.  */
469   return (CORE_ADDR) read_memory_unsigned_integer (desc, 8);
470 }
471
472
473 /* Pattern for the standard linkage function.  These are built by
474    build_plt_stub in elf64-ppc.c, whose GLINK argument is always
475    zero.  */
476 static struct insn_pattern ppc64_standard_linkage[] =
477   {
478     /* addis r12, r2, <any> */
479     { insn_d (-1, -1, -1, 0), insn_d (15, 12, 2, 0), 0 },
480
481     /* std r2, 40(r1) */
482     { -1, insn_ds (62, 2, 1, 40, 0), 0 },
483
484     /* ld r11, <any>(r12) */
485     { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 },
486
487     /* addis r12, r12, 1 <optional> */
488     { insn_d (-1, -1, -1, -1), insn_d (15, 12, 2, 1), 1 },
489
490     /* ld r2, <any>(r12) */
491     { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 2, 12, 0, 0), 0 },
492
493     /* addis r12, r12, 1 <optional> */
494     { insn_d (-1, -1, -1, -1), insn_d (15, 12, 2, 1), 1 },
495
496     /* mtctr r11 */
497     { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 11, 9, 467),
498       0 },
499
500     /* ld r11, <any>(r12) */
501     { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 },
502       
503     /* bctr */
504     { -1, 0x4e800420, 0 },
505
506     { 0, 0, 0 }
507   };
508 #define PPC64_STANDARD_LINKAGE_LEN \
509   (sizeof (ppc64_standard_linkage) / sizeof (ppc64_standard_linkage[0]))
510
511 /* When the dynamic linker is doing lazy symbol resolution, the first
512    call to a function in another object will go like this:
513
514    - The user's function calls the linkage function:
515
516      100007c4:  4b ff fc d5     bl      10000498
517      100007c8:  e8 41 00 28     ld      r2,40(r1)
518
519    - The linkage function loads the entry point (and other stuff) from
520      the function descriptor in the PLT, and jumps to it:
521
522      10000498:  3d 82 00 00     addis   r12,r2,0
523      1000049c:  f8 41 00 28     std     r2,40(r1)
524      100004a0:  e9 6c 80 98     ld      r11,-32616(r12)
525      100004a4:  e8 4c 80 a0     ld      r2,-32608(r12)
526      100004a8:  7d 69 03 a6     mtctr   r11
527      100004ac:  e9 6c 80 a8     ld      r11,-32600(r12)
528      100004b0:  4e 80 04 20     bctr
529
530    - But since this is the first time that PLT entry has been used, it
531      sends control to its glink entry.  That loads the number of the
532      PLT entry and jumps to the common glink0 code:
533
534      10000c98:  38 00 00 00     li      r0,0
535      10000c9c:  4b ff ff dc     b       10000c78
536
537    - The common glink0 code then transfers control to the dynamic
538      linker's fixup code:
539
540      10000c78:  e8 41 00 28     ld      r2,40(r1)
541      10000c7c:  3d 82 00 00     addis   r12,r2,0
542      10000c80:  e9 6c 80 80     ld      r11,-32640(r12)
543      10000c84:  e8 4c 80 88     ld      r2,-32632(r12)
544      10000c88:  7d 69 03 a6     mtctr   r11
545      10000c8c:  e9 6c 80 90     ld      r11,-32624(r12)
546      10000c90:  4e 80 04 20     bctr
547
548    Eventually, this code will figure out how to skip all of this,
549    including the dynamic linker.  At the moment, we just get through
550    the linkage function.  */
551
552 /* If the current thread is about to execute a series of instructions
553    at PC matching the ppc64_standard_linkage pattern, and INSN is the result
554    from that pattern match, return the code address to which the
555    standard linkage function will send them.  (This doesn't deal with
556    dynamic linker lazy symbol resolution stubs.)  */
557 static CORE_ADDR
558 ppc64_standard_linkage_target (struct frame_info *frame,
559                                CORE_ADDR pc, unsigned int *insn)
560 {
561   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (frame));
562
563   /* The address of the function descriptor this linkage function
564      references.  */
565   CORE_ADDR desc
566     = ((CORE_ADDR) get_frame_register_unsigned (frame,
567                                                 tdep->ppc_gp0_regnum + 2)
568        + (insn_d_field (insn[0]) << 16)
569        + insn_ds_field (insn[2]));
570
571   /* The first word of the descriptor is the entry point.  Return that.  */
572   return ppc64_desc_entry_point (desc);
573 }
574
575
576 /* Given that we've begun executing a call trampoline at PC, return
577    the entry point of the function the trampoline will go to.  */
578 static CORE_ADDR
579 ppc64_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
580 {
581   unsigned int ppc64_standard_linkage_insn[PPC64_STANDARD_LINKAGE_LEN];
582
583   if (insns_match_pattern (pc, ppc64_standard_linkage,
584                            ppc64_standard_linkage_insn))
585     return ppc64_standard_linkage_target (frame, pc,
586                                           ppc64_standard_linkage_insn);
587   else
588     return 0;
589 }
590
591
592 /* Support for convert_from_func_ptr_addr (ARCH, ADDR, TARG) on PPC
593    GNU/Linux.
594
595    Usually a function pointer's representation is simply the address
596    of the function.  On GNU/Linux on the PowerPC however, a function
597    pointer may be a pointer to a function descriptor.
598
599    For PPC64, a function descriptor is a TOC entry, in a data section,
600    which contains three words: the first word is the address of the
601    function, the second word is the TOC pointer (r2), and the third word
602    is the static chain value.
603
604    For PPC32, there are two kinds of function pointers: non-secure and
605    secure.  Non-secure function pointers point directly to the
606    function in a code section and thus need no translation.  Secure
607    ones (from GCC's -msecure-plt option) are in a data section and
608    contain one word: the address of the function.
609
610    Throughout GDB it is currently assumed that a function pointer contains
611    the address of the function, which is not easy to fix.  In addition, the
612    conversion of a function address to a function pointer would
613    require allocation of a TOC entry in the inferior's memory space,
614    with all its drawbacks.  To be able to call C++ virtual methods in
615    the inferior (which are called via function pointers),
616    find_function_addr uses this function to get the function address
617    from a function pointer.
618
619    If ADDR points at what is clearly a function descriptor, transform
620    it into the address of the corresponding function, if needed.  Be
621    conservative, otherwise GDB will do the transformation on any
622    random addresses such as occur when there is no symbol table.  */
623
624 static CORE_ADDR
625 ppc_linux_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
626                                       CORE_ADDR addr,
627                                       struct target_ops *targ)
628 {
629   struct gdbarch_tdep *tdep;
630   struct section_table *s = target_section_by_addr (targ, addr);
631   char *sect_name = NULL;
632
633   if (!s)
634     return addr;
635
636   tdep = gdbarch_tdep (gdbarch);
637
638   switch (tdep->wordsize)
639     {
640       case 4:
641         sect_name = ".plt";
642         break;
643       case 8:
644         sect_name = ".opd";
645         break;
646       default:
647         internal_error (__FILE__, __LINE__,
648                         _("failed internal consistency check"));
649     }
650
651   /* Check if ADDR points to a function descriptor.  */
652
653   /* NOTE: this depends on the coincidence that the address of a functions
654      entry point is contained in the first word of its function descriptor
655      for both PPC-64 and for PPC-32 with secure PLTs.  */
656   if ((strcmp (s->the_bfd_section->name, sect_name) == 0)
657       && s->the_bfd_section->flags & SEC_DATA)
658     return get_target_memory_unsigned (targ, addr, tdep->wordsize);
659
660   return addr;
661 }
662
663 static void
664 right_supply_register (struct regcache *regcache, int wordsize, int regnum,
665                        const bfd_byte *buf)
666 {
667   regcache_raw_supply (regcache, regnum,
668                        (buf + wordsize - register_size (current_gdbarch, regnum)));
669 }
670
671 /* Extract the register values found in the WORDSIZED ABI GREGSET,
672    storing their values in REGCACHE.  Note that some are left-aligned,
673    while others are right aligned.  */
674
675 void
676 ppc_linux_supply_gregset (struct regcache *regcache,
677                           int regnum, const void *gregs, size_t size,
678                           int wordsize)
679 {
680   int regi;
681   struct gdbarch *regcache_arch = get_regcache_arch (regcache); 
682   struct gdbarch_tdep *regcache_tdep = gdbarch_tdep (regcache_arch);
683   const bfd_byte *buf = gregs;
684
685   for (regi = 0; regi < ppc_num_gprs; regi++)
686     right_supply_register (regcache, wordsize,
687                            regcache_tdep->ppc_gp0_regnum + regi,
688                            buf + wordsize * regi);
689
690   right_supply_register (regcache, wordsize, gdbarch_pc_regnum (regcache_arch),
691                          buf + wordsize * PPC_LINUX_PT_NIP);
692   right_supply_register (regcache, wordsize, regcache_tdep->ppc_lr_regnum,
693                          buf + wordsize * PPC_LINUX_PT_LNK);
694   regcache_raw_supply (regcache, regcache_tdep->ppc_cr_regnum,
695                        buf + wordsize * PPC_LINUX_PT_CCR);
696   regcache_raw_supply (regcache, regcache_tdep->ppc_xer_regnum,
697                        buf + wordsize * PPC_LINUX_PT_XER);
698   regcache_raw_supply (regcache, regcache_tdep->ppc_ctr_regnum,
699                        buf + wordsize * PPC_LINUX_PT_CTR);
700   if (regcache_tdep->ppc_mq_regnum != -1)
701     right_supply_register (regcache, wordsize, regcache_tdep->ppc_mq_regnum,
702                            buf + wordsize * PPC_LINUX_PT_MQ);
703   right_supply_register (regcache, wordsize, regcache_tdep->ppc_ps_regnum,
704                          buf + wordsize * PPC_LINUX_PT_MSR);
705 }
706
707 static void
708 ppc32_linux_supply_gregset (const struct regset *regset,
709                             struct regcache *regcache,
710                             int regnum, const void *gregs, size_t size)
711 {
712   ppc_linux_supply_gregset (regcache, regnum, gregs, size, 4);
713 }
714
715 static struct regset ppc32_linux_gregset = {
716   NULL, ppc32_linux_supply_gregset
717 };
718
719 static void
720 ppc64_linux_supply_gregset (const struct regset *regset,
721                             struct regcache * regcache,
722                             int regnum, const void *gregs, size_t size)
723 {
724   ppc_linux_supply_gregset (regcache, regnum, gregs, size, 8);
725 }
726
727 static struct regset ppc64_linux_gregset = {
728   NULL, ppc64_linux_supply_gregset
729 };
730
731 void
732 ppc_linux_supply_fpregset (const struct regset *regset,
733                            struct regcache * regcache,
734                            int regnum, const void *fpset, size_t size)
735 {
736   int regi;
737   struct gdbarch *regcache_arch = get_regcache_arch (regcache); 
738   struct gdbarch_tdep *regcache_tdep = gdbarch_tdep (regcache_arch);
739   const bfd_byte *buf = fpset;
740
741   if (! ppc_floating_point_unit_p (regcache_arch))
742     return;
743
744   for (regi = 0; regi < ppc_num_fprs; regi++)
745     regcache_raw_supply (regcache, 
746                          regcache_tdep->ppc_fp0_regnum + regi,
747                          buf + 8 * regi);
748
749   /* The FPSCR is stored in the low order word of the last
750      doubleword in the fpregset.  */
751   regcache_raw_supply (regcache, regcache_tdep->ppc_fpscr_regnum,
752                        buf + 8 * 32 + 4);
753 }
754
755 static struct regset ppc_linux_fpregset = { NULL, ppc_linux_supply_fpregset };
756
757 static const struct regset *
758 ppc_linux_regset_from_core_section (struct gdbarch *core_arch,
759                                     const char *sect_name, size_t sect_size)
760 {
761   struct gdbarch_tdep *tdep = gdbarch_tdep (core_arch);
762   if (strcmp (sect_name, ".reg") == 0)
763     {
764       if (tdep->wordsize == 4)
765         return &ppc32_linux_gregset;
766       else
767         return &ppc64_linux_gregset;
768     }
769   if (strcmp (sect_name, ".reg2") == 0)
770     return &ppc_linux_fpregset;
771   return NULL;
772 }
773
774 static void
775 ppc_linux_sigtramp_cache (struct frame_info *next_frame,
776                           struct trad_frame_cache *this_cache,
777                           CORE_ADDR func, LONGEST offset,
778                           int bias)
779 {
780   CORE_ADDR base;
781   CORE_ADDR regs;
782   CORE_ADDR gpregs;
783   CORE_ADDR fpregs;
784   int i;
785   struct gdbarch *gdbarch = get_frame_arch (next_frame);
786   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
787
788   base = frame_unwind_register_unsigned (next_frame,
789                                          gdbarch_sp_regnum (current_gdbarch));
790   if (bias > 0 && frame_pc_unwind (next_frame) != func)
791     /* See below, some signal trampolines increment the stack as their
792        first instruction, need to compensate for that.  */
793     base -= bias;
794
795   /* Find the address of the register buffer pointer.  */
796   regs = base + offset;
797   /* Use that to find the address of the corresponding register
798      buffers.  */
799   gpregs = read_memory_unsigned_integer (regs, tdep->wordsize);
800   fpregs = gpregs + 48 * tdep->wordsize;
801
802   /* General purpose.  */
803   for (i = 0; i < 32; i++)
804     {
805       int regnum = i + tdep->ppc_gp0_regnum;
806       trad_frame_set_reg_addr (this_cache, regnum, gpregs + i * tdep->wordsize);
807     }
808   trad_frame_set_reg_addr (this_cache,
809                            gdbarch_pc_regnum (current_gdbarch),
810                            gpregs + 32 * tdep->wordsize);
811   trad_frame_set_reg_addr (this_cache, tdep->ppc_ctr_regnum,
812                            gpregs + 35 * tdep->wordsize);
813   trad_frame_set_reg_addr (this_cache, tdep->ppc_lr_regnum,
814                            gpregs + 36 * tdep->wordsize);
815   trad_frame_set_reg_addr (this_cache, tdep->ppc_xer_regnum,
816                            gpregs + 37 * tdep->wordsize);
817   trad_frame_set_reg_addr (this_cache, tdep->ppc_cr_regnum,
818                            gpregs + 38 * tdep->wordsize);
819
820   if (ppc_floating_point_unit_p (gdbarch))
821     {
822       /* Floating point registers.  */
823       for (i = 0; i < 32; i++)
824         {
825           int regnum = i + gdbarch_fp0_regnum (current_gdbarch);
826           trad_frame_set_reg_addr (this_cache, regnum,
827                                    fpregs + i * tdep->wordsize);
828         }
829       trad_frame_set_reg_addr (this_cache, tdep->ppc_fpscr_regnum,
830                          fpregs + 32 * tdep->wordsize);
831     }
832   trad_frame_set_id (this_cache, frame_id_build (base, func));
833 }
834
835 static void
836 ppc32_linux_sigaction_cache_init (const struct tramp_frame *self,
837                                   struct frame_info *next_frame,
838                                   struct trad_frame_cache *this_cache,
839                                   CORE_ADDR func)
840 {
841   ppc_linux_sigtramp_cache (next_frame, this_cache, func,
842                             0xd0 /* Offset to ucontext_t.  */
843                             + 0x30 /* Offset to .reg.  */,
844                             0);
845 }
846
847 static void
848 ppc64_linux_sigaction_cache_init (const struct tramp_frame *self,
849                                   struct frame_info *next_frame,
850                                   struct trad_frame_cache *this_cache,
851                                   CORE_ADDR func)
852 {
853   ppc_linux_sigtramp_cache (next_frame, this_cache, func,
854                             0x80 /* Offset to ucontext_t.  */
855                             + 0xe0 /* Offset to .reg.  */,
856                             128);
857 }
858
859 static void
860 ppc32_linux_sighandler_cache_init (const struct tramp_frame *self,
861                                    struct frame_info *next_frame,
862                                    struct trad_frame_cache *this_cache,
863                                    CORE_ADDR func)
864 {
865   ppc_linux_sigtramp_cache (next_frame, this_cache, func,
866                             0x40 /* Offset to ucontext_t.  */
867                             + 0x1c /* Offset to .reg.  */,
868                             0);
869 }
870
871 static void
872 ppc64_linux_sighandler_cache_init (const struct tramp_frame *self,
873                                    struct frame_info *next_frame,
874                                    struct trad_frame_cache *this_cache,
875                                    CORE_ADDR func)
876 {
877   ppc_linux_sigtramp_cache (next_frame, this_cache, func,
878                             0x80 /* Offset to struct sigcontext.  */
879                             + 0x38 /* Offset to .reg.  */,
880                             128);
881 }
882
883 static struct tramp_frame ppc32_linux_sigaction_tramp_frame = {
884   SIGTRAMP_FRAME,
885   4,
886   { 
887     { 0x380000ac, -1 }, /* li r0, 172 */
888     { 0x44000002, -1 }, /* sc */
889     { TRAMP_SENTINEL_INSN },
890   },
891   ppc32_linux_sigaction_cache_init
892 };
893 static struct tramp_frame ppc64_linux_sigaction_tramp_frame = {
894   SIGTRAMP_FRAME,
895   4,
896   {
897     { 0x38210080, -1 }, /* addi r1,r1,128 */
898     { 0x380000ac, -1 }, /* li r0, 172 */
899     { 0x44000002, -1 }, /* sc */
900     { TRAMP_SENTINEL_INSN },
901   },
902   ppc64_linux_sigaction_cache_init
903 };
904 static struct tramp_frame ppc32_linux_sighandler_tramp_frame = {
905   SIGTRAMP_FRAME,
906   4,
907   { 
908     { 0x38000077, -1 }, /* li r0,119 */
909     { 0x44000002, -1 }, /* sc */
910     { TRAMP_SENTINEL_INSN },
911   },
912   ppc32_linux_sighandler_cache_init
913 };
914 static struct tramp_frame ppc64_linux_sighandler_tramp_frame = {
915   SIGTRAMP_FRAME,
916   4,
917   { 
918     { 0x38210080, -1 }, /* addi r1,r1,128 */
919     { 0x38000077, -1 }, /* li r0,119 */
920     { 0x44000002, -1 }, /* sc */
921     { TRAMP_SENTINEL_INSN },
922   },
923   ppc64_linux_sighandler_cache_init
924 };
925
926 static void
927 ppc_linux_init_abi (struct gdbarch_info info,
928                     struct gdbarch *gdbarch)
929 {
930   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
931
932   /* NOTE: jimb/2004-03-26: The System V ABI PowerPC Processor
933      Supplement says that long doubles are sixteen bytes long.
934      However, as one of the known warts of its ABI, PPC GNU/Linux uses
935      eight-byte long doubles.  GCC only recently got 128-bit long
936      double support on PPC, so it may be changing soon.  The
937      Linux[sic] Standards Base says that programs that use 'long
938      double' on PPC GNU/Linux are non-conformant.  */
939   /* NOTE: cagney/2005-01-25: True for both 32- and 64-bit.  */
940   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
941
942   /* Handle PPC GNU/Linux 64-bit function pointers (which are really
943      function descriptors) and 32-bit secure PLT entries.  */
944   set_gdbarch_convert_from_func_ptr_addr
945     (gdbarch, ppc_linux_convert_from_func_ptr_addr);
946
947   if (tdep->wordsize == 4)
948     {
949       /* Until November 2001, gcc did not comply with the 32 bit SysV
950          R4 ABI requirement that structures less than or equal to 8
951          bytes should be returned in registers.  Instead GCC was using
952          the the AIX/PowerOpen ABI - everything returned in memory
953          (well ignoring vectors that is).  When this was corrected, it
954          wasn't fixed for GNU/Linux native platform.  Use the
955          PowerOpen struct convention.  */
956       set_gdbarch_return_value (gdbarch, ppc_linux_return_value);
957
958       set_gdbarch_memory_remove_breakpoint (gdbarch,
959                                             ppc_linux_memory_remove_breakpoint);
960
961       /* Shared library handling.  */
962       set_gdbarch_skip_trampoline_code (gdbarch,
963                                         ppc_linux_skip_trampoline_code);
964       set_solib_svr4_fetch_link_map_offsets
965         (gdbarch, svr4_ilp32_fetch_link_map_offsets);
966
967       /* Trampolines.  */
968       tramp_frame_prepend_unwinder (gdbarch, &ppc32_linux_sigaction_tramp_frame);
969       tramp_frame_prepend_unwinder (gdbarch, &ppc32_linux_sighandler_tramp_frame);
970     }
971   
972   if (tdep->wordsize == 8)
973     {
974       /* Shared library handling.  */
975       set_gdbarch_skip_trampoline_code (gdbarch, ppc64_skip_trampoline_code);
976       set_solib_svr4_fetch_link_map_offsets
977         (gdbarch, svr4_lp64_fetch_link_map_offsets);
978
979       /* Trampolines.  */
980       tramp_frame_prepend_unwinder (gdbarch, &ppc64_linux_sigaction_tramp_frame);
981       tramp_frame_prepend_unwinder (gdbarch, &ppc64_linux_sighandler_tramp_frame);
982     }
983   set_gdbarch_regset_from_core_section (gdbarch, ppc_linux_regset_from_core_section);
984
985   /* Enable TLS support.  */
986   set_gdbarch_fetch_tls_load_module_address (gdbarch,
987                                              svr4_fetch_objfile_link_map);
988 }
989
990 void
991 _initialize_ppc_linux_tdep (void)
992 {
993   /* Register for all sub-familes of the POWER/PowerPC: 32-bit and
994      64-bit PowerPC, and the older rs6k.  */
995   gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc, GDB_OSABI_LINUX,
996                          ppc_linux_init_abi);
997   gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc64, GDB_OSABI_LINUX,
998                          ppc_linux_init_abi);
999   gdbarch_register_osabi (bfd_arch_rs6000, bfd_mach_rs6k, GDB_OSABI_LINUX,
1000                          ppc_linux_init_abi);
1001 }