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