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