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