2003-01-27 Andrew Cagney <ac131313@redhat.com>
[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
c877c8e6 39/* The following two instructions are used in the signal trampoline
ca557f44 40 code on GNU/Linux PPC. */
c877c8e6
KB
41#define INSTR_LI_R0_0x7777 0x38007777
42#define INSTR_SC 0x44000002
43
44/* Since the *-tdep.c files are platform independent (i.e, they may be
45 used to build cross platform debuggers), we can't include system
46 headers. Therefore, details concerning the sigcontext structure
47 must be painstakingly rerecorded. What's worse, if these details
48 ever change in the header files, they'll have to be changed here
49 as well. */
50
51/* __SIGNAL_FRAMESIZE from <asm/ptrace.h> */
52#define PPC_LINUX_SIGNAL_FRAMESIZE 64
53
54/* From <asm/sigcontext.h>, offsetof(struct sigcontext_struct, regs) == 0x1c */
55#define PPC_LINUX_REGS_PTR_OFFSET (PPC_LINUX_SIGNAL_FRAMESIZE + 0x1c)
56
57/* From <asm/sigcontext.h>,
58 offsetof(struct sigcontext_struct, handler) == 0x14 */
59#define PPC_LINUX_HANDLER_PTR_OFFSET (PPC_LINUX_SIGNAL_FRAMESIZE + 0x14)
60
61/* From <asm/ptrace.h>, values for PT_NIP, PT_R1, and PT_LNK */
62#define PPC_LINUX_PT_R0 0
63#define PPC_LINUX_PT_R1 1
64#define PPC_LINUX_PT_R2 2
65#define PPC_LINUX_PT_R3 3
66#define PPC_LINUX_PT_R4 4
67#define PPC_LINUX_PT_R5 5
68#define PPC_LINUX_PT_R6 6
69#define PPC_LINUX_PT_R7 7
70#define PPC_LINUX_PT_R8 8
71#define PPC_LINUX_PT_R9 9
72#define PPC_LINUX_PT_R10 10
73#define PPC_LINUX_PT_R11 11
74#define PPC_LINUX_PT_R12 12
75#define PPC_LINUX_PT_R13 13
76#define PPC_LINUX_PT_R14 14
77#define PPC_LINUX_PT_R15 15
78#define PPC_LINUX_PT_R16 16
79#define PPC_LINUX_PT_R17 17
80#define PPC_LINUX_PT_R18 18
81#define PPC_LINUX_PT_R19 19
82#define PPC_LINUX_PT_R20 20
83#define PPC_LINUX_PT_R21 21
84#define PPC_LINUX_PT_R22 22
85#define PPC_LINUX_PT_R23 23
86#define PPC_LINUX_PT_R24 24
87#define PPC_LINUX_PT_R25 25
88#define PPC_LINUX_PT_R26 26
89#define PPC_LINUX_PT_R27 27
90#define PPC_LINUX_PT_R28 28
91#define PPC_LINUX_PT_R29 29
92#define PPC_LINUX_PT_R30 30
93#define PPC_LINUX_PT_R31 31
94#define PPC_LINUX_PT_NIP 32
95#define PPC_LINUX_PT_MSR 33
96#define PPC_LINUX_PT_CTR 35
97#define PPC_LINUX_PT_LNK 36
98#define PPC_LINUX_PT_XER 37
99#define PPC_LINUX_PT_CCR 38
100#define PPC_LINUX_PT_MQ 39
101#define PPC_LINUX_PT_FPR0 48 /* each FP reg occupies 2 slots in this space */
102#define PPC_LINUX_PT_FPR31 (PPC_LINUX_PT_FPR0 + 2*31)
103#define PPC_LINUX_PT_FPSCR (PPC_LINUX_PT_FPR0 + 2*32 + 1)
104
9aa1e687 105static int ppc_linux_at_sigtramp_return_path (CORE_ADDR pc);
50c9bd31 106
c877c8e6
KB
107/* Determine if pc is in a signal trampoline...
108
ca557f44 109 Ha! That's not what this does at all. wait_for_inferior in
d7bd68ca
AC
110 infrun.c calls PC_IN_SIGTRAMP in order to detect entry into a
111 signal trampoline just after delivery of a signal. But on
112 GNU/Linux, signal trampolines are used for the return path only.
113 The kernel sets things up so that the signal handler is called
114 directly.
c877c8e6
KB
115
116 If we use in_sigtramp2() in place of in_sigtramp() (see below)
117 we'll (often) end up with stop_pc in the trampoline and prev_pc in
118 the (now exited) handler. The code there will cause a temporary
119 breakpoint to be set on prev_pc which is not very likely to get hit
120 again.
121
122 If this is confusing, think of it this way... the code in
123 wait_for_inferior() needs to be able to detect entry into a signal
124 trampoline just after a signal is delivered, not after the handler
125 has been run.
126
127 So, we define in_sigtramp() below to return 1 if the following is
128 true:
129
130 1) The previous frame is a real signal trampoline.
131
132 - and -
133
134 2) pc is at the first or second instruction of the corresponding
135 handler.
136
137 Why the second instruction? It seems that wait_for_inferior()
138 never sees the first instruction when single stepping. When a
139 signal is delivered while stepping, the next instruction that
140 would've been stepped over isn't, instead a signal is delivered and
141 the first instruction of the handler is stepped over instead. That
142 puts us on the second instruction. (I added the test for the
143 first instruction long after the fact, just in case the observed
144 behavior is ever fixed.)
145
d7bd68ca 146 PC_IN_SIGTRAMP is called from blockframe.c as well in order to set
5a203e44
AC
147 the frame's type (if a SIGTRAMP_FRAME). Because of our strange
148 definition of in_sigtramp below, we can't rely on the frame's type
d7bd68ca 149 getting set correctly from within blockframe.c. This is why we
5a203e44
AC
150 take pains to set it in init_extra_frame_info().
151
152 NOTE: cagney/2002-11-10: I suspect the real problem here is that
153 the get_prev_frame() only initializes the frame's type after the
154 call to INIT_FRAME_INFO. get_prev_frame() should be fixed, this
155 code shouldn't be working its way around a bug :-(. */
c877c8e6
KB
156
157int
158ppc_linux_in_sigtramp (CORE_ADDR pc, char *func_name)
159{
160 CORE_ADDR lr;
161 CORE_ADDR sp;
162 CORE_ADDR tramp_sp;
163 char buf[4];
164 CORE_ADDR handler;
165
2188cbdd 166 lr = read_register (gdbarch_tdep (current_gdbarch)->ppc_lr_regnum);
c877c8e6
KB
167 if (!ppc_linux_at_sigtramp_return_path (lr))
168 return 0;
169
170 sp = read_register (SP_REGNUM);
171
172 if (target_read_memory (sp, buf, sizeof (buf)) != 0)
173 return 0;
174
175 tramp_sp = extract_unsigned_integer (buf, 4);
176
177 if (target_read_memory (tramp_sp + PPC_LINUX_HANDLER_PTR_OFFSET, buf,
178 sizeof (buf)) != 0)
179 return 0;
180
181 handler = extract_unsigned_integer (buf, 4);
182
183 return (pc == handler || pc == handler + 4);
184}
185
186/*
187 * The signal handler trampoline is on the stack and consists of exactly
188 * two instructions. The easiest and most accurate way of determining
189 * whether the pc is in one of these trampolines is by inspecting the
190 * instructions. It'd be faster though if we could find a way to do this
191 * via some simple address comparisons.
192 */
9aa1e687 193static int
c877c8e6
KB
194ppc_linux_at_sigtramp_return_path (CORE_ADDR pc)
195{
196 char buf[12];
197 unsigned long pcinsn;
198 if (target_read_memory (pc - 4, buf, sizeof (buf)) != 0)
199 return 0;
200
201 /* extract the instruction at the pc */
202 pcinsn = extract_unsigned_integer (buf + 4, 4);
203
204 return (
205 (pcinsn == INSTR_LI_R0_0x7777
206 && extract_unsigned_integer (buf + 8, 4) == INSTR_SC)
207 ||
208 (pcinsn == INSTR_SC
209 && extract_unsigned_integer (buf, 4) == INSTR_LI_R0_0x7777));
210}
211
212CORE_ADDR
213ppc_linux_skip_trampoline_code (CORE_ADDR pc)
214{
215 char buf[4];
216 struct obj_section *sect;
217 struct objfile *objfile;
218 unsigned long insn;
219 CORE_ADDR plt_start = 0;
220 CORE_ADDR symtab = 0;
221 CORE_ADDR strtab = 0;
222 int num_slots = -1;
223 int reloc_index = -1;
224 CORE_ADDR plt_table;
225 CORE_ADDR reloc;
226 CORE_ADDR sym;
227 long symidx;
228 char symname[1024];
229 struct minimal_symbol *msymbol;
230
231 /* Find the section pc is in; return if not in .plt */
232 sect = find_pc_section (pc);
233 if (!sect || strcmp (sect->the_bfd_section->name, ".plt") != 0)
234 return 0;
235
236 objfile = sect->objfile;
237
238 /* Pick up the instruction at pc. It had better be of the
239 form
240 li r11, IDX
241
242 where IDX is an index into the plt_table. */
243
244 if (target_read_memory (pc, buf, 4) != 0)
245 return 0;
246 insn = extract_unsigned_integer (buf, 4);
247
248 if ((insn & 0xffff0000) != 0x39600000 /* li r11, VAL */ )
249 return 0;
250
251 reloc_index = (insn << 16) >> 16;
252
253 /* Find the objfile that pc is in and obtain the information
254 necessary for finding the symbol name. */
255 for (sect = objfile->sections; sect < objfile->sections_end; ++sect)
256 {
257 const char *secname = sect->the_bfd_section->name;
258 if (strcmp (secname, ".plt") == 0)
259 plt_start = sect->addr;
260 else if (strcmp (secname, ".rela.plt") == 0)
261 num_slots = ((int) sect->endaddr - (int) sect->addr) / 12;
262 else if (strcmp (secname, ".dynsym") == 0)
263 symtab = sect->addr;
264 else if (strcmp (secname, ".dynstr") == 0)
265 strtab = sect->addr;
266 }
267
268 /* Make sure we have all the information we need. */
269 if (plt_start == 0 || num_slots == -1 || symtab == 0 || strtab == 0)
270 return 0;
271
272 /* Compute the value of the plt table */
273 plt_table = plt_start + 72 + 8 * num_slots;
274
275 /* Get address of the relocation entry (Elf32_Rela) */
276 if (target_read_memory (plt_table + reloc_index, buf, 4) != 0)
277 return 0;
278 reloc = extract_address (buf, 4);
279
280 sect = find_pc_section (reloc);
281 if (!sect)
282 return 0;
283
284 if (strcmp (sect->the_bfd_section->name, ".text") == 0)
285 return reloc;
286
287 /* Now get the r_info field which is the relocation type and symbol
288 index. */
289 if (target_read_memory (reloc + 4, buf, 4) != 0)
290 return 0;
291 symidx = extract_unsigned_integer (buf, 4);
292
293 /* Shift out the relocation type leaving just the symbol index */
294 /* symidx = ELF32_R_SYM(symidx); */
295 symidx = symidx >> 8;
296
297 /* compute the address of the symbol */
298 sym = symtab + symidx * 4;
299
300 /* Fetch the string table index */
301 if (target_read_memory (sym, buf, 4) != 0)
302 return 0;
303 symidx = extract_unsigned_integer (buf, 4);
304
305 /* Fetch the string; we don't know how long it is. Is it possible
306 that the following will fail because we're trying to fetch too
307 much? */
308 if (target_read_memory (strtab + symidx, symname, sizeof (symname)) != 0)
309 return 0;
310
311 /* This might not work right if we have multiple symbols with the
312 same name; the only way to really get it right is to perform
313 the same sort of lookup as the dynamic linker. */
314 msymbol = lookup_minimal_symbol_text (symname, NULL, NULL);
315 if (!msymbol)
316 return 0;
317
318 return SYMBOL_VALUE_ADDRESS (msymbol);
319}
320
321/* The rs6000 version of FRAME_SAVED_PC will almost work for us. The
322 signal handler details are different, so we'll handle those here
323 and call the rs6000 version to do the rest. */
9aa1e687 324CORE_ADDR
c877c8e6
KB
325ppc_linux_frame_saved_pc (struct frame_info *fi)
326{
5a203e44 327 if ((get_frame_type (fi) == SIGTRAMP_FRAME))
c877c8e6
KB
328 {
329 CORE_ADDR regs_addr =
50c9bd31 330 read_memory_integer (fi->frame + PPC_LINUX_REGS_PTR_OFFSET, 4);
c877c8e6
KB
331 /* return the NIP in the regs array */
332 return read_memory_integer (regs_addr + 4 * PPC_LINUX_PT_NIP, 4);
333 }
5a203e44 334 else if (fi->next && (get_frame_type (fi->next) == SIGTRAMP_FRAME))
50c9bd31
KB
335 {
336 CORE_ADDR regs_addr =
337 read_memory_integer (fi->next->frame + PPC_LINUX_REGS_PTR_OFFSET, 4);
338 /* return LNK in the regs array */
339 return read_memory_integer (regs_addr + 4 * PPC_LINUX_PT_LNK, 4);
340 }
341 else
342 return rs6000_frame_saved_pc (fi);
c877c8e6
KB
343}
344
345void
346ppc_linux_init_extra_frame_info (int fromleaf, struct frame_info *fi)
347{
348 rs6000_init_extra_frame_info (fromleaf, fi);
349
350 if (fi->next != 0)
351 {
352 /* We're called from get_prev_frame_info; check to see if
353 this is a signal frame by looking to see if the pc points
354 at trampoline code */
355 if (ppc_linux_at_sigtramp_return_path (fi->pc))
5a203e44 356 deprecated_set_frame_type (fi, SIGTRAMP_FRAME);
c877c8e6 357 else
5a203e44
AC
358 /* FIXME: cagney/2002-11-10: Is this double bogus? What
359 happens if the frame has previously been marked as a dummy? */
360 deprecated_set_frame_type (fi, NORMAL_FRAME);
c877c8e6
KB
361 }
362}
363
364int
365ppc_linux_frameless_function_invocation (struct frame_info *fi)
366{
367 /* We'll find the wrong thing if we let
368 rs6000_frameless_function_invocation () search for a signal trampoline */
369 if (ppc_linux_at_sigtramp_return_path (fi->pc))
370 return 0;
371 else
372 return rs6000_frameless_function_invocation (fi);
373}
374
375void
376ppc_linux_frame_init_saved_regs (struct frame_info *fi)
377{
5a203e44 378 if ((get_frame_type (fi) == SIGTRAMP_FRAME))
c877c8e6
KB
379 {
380 CORE_ADDR regs_addr;
381 int i;
382 if (fi->saved_regs)
383 return;
384
385 frame_saved_regs_zalloc (fi);
386
387 regs_addr =
388 read_memory_integer (fi->frame + PPC_LINUX_REGS_PTR_OFFSET, 4);
389 fi->saved_regs[PC_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_NIP;
2188cbdd
EZ
390 fi->saved_regs[gdbarch_tdep (current_gdbarch)->ppc_ps_regnum] =
391 regs_addr + 4 * PPC_LINUX_PT_MSR;
392 fi->saved_regs[gdbarch_tdep (current_gdbarch)->ppc_cr_regnum] =
393 regs_addr + 4 * PPC_LINUX_PT_CCR;
394 fi->saved_regs[gdbarch_tdep (current_gdbarch)->ppc_lr_regnum] =
395 regs_addr + 4 * PPC_LINUX_PT_LNK;
396 fi->saved_regs[gdbarch_tdep (current_gdbarch)->ppc_ctr_regnum] =
397 regs_addr + 4 * PPC_LINUX_PT_CTR;
398 fi->saved_regs[gdbarch_tdep (current_gdbarch)->ppc_xer_regnum] =
399 regs_addr + 4 * PPC_LINUX_PT_XER;
400 fi->saved_regs[gdbarch_tdep (current_gdbarch)->ppc_mq_regnum] =
401 regs_addr + 4 * PPC_LINUX_PT_MQ;
c877c8e6 402 for (i = 0; i < 32; i++)
2188cbdd
EZ
403 fi->saved_regs[gdbarch_tdep (current_gdbarch)->ppc_gp0_regnum + i] =
404 regs_addr + 4 * PPC_LINUX_PT_R0 + 4 * i;
c877c8e6
KB
405 for (i = 0; i < 32; i++)
406 fi->saved_regs[FP0_REGNUM + i] = regs_addr + 4 * PPC_LINUX_PT_FPR0 + 8 * i;
407 }
408 else
409 rs6000_frame_init_saved_regs (fi);
410}
411
412CORE_ADDR
413ppc_linux_frame_chain (struct frame_info *thisframe)
414{
415 /* Kernel properly constructs the frame chain for the handler */
5a203e44 416 if ((get_frame_type (thisframe) == SIGTRAMP_FRAME))
c877c8e6
KB
417 return read_memory_integer ((thisframe)->frame, 4);
418 else
419 return rs6000_frame_chain (thisframe);
420}
421
122a33de
KB
422/* ppc_linux_memory_remove_breakpoints attempts to remove a breakpoint
423 in much the same fashion as memory_remove_breakpoint in mem-break.c,
424 but is careful not to write back the previous contents if the code
425 in question has changed in between inserting the breakpoint and
426 removing it.
427
428 Here is the problem that we're trying to solve...
429
430 Once upon a time, before introducing this function to remove
431 breakpoints from the inferior, setting a breakpoint on a shared
432 library function prior to running the program would not work
433 properly. In order to understand the problem, it is first
434 necessary to understand a little bit about dynamic linking on
435 this platform.
436
437 A call to a shared library function is accomplished via a bl
438 (branch-and-link) instruction whose branch target is an entry
439 in the procedure linkage table (PLT). The PLT in the object
440 file is uninitialized. To gdb, prior to running the program, the
441 entries in the PLT are all zeros.
442
443 Once the program starts running, the shared libraries are loaded
444 and the procedure linkage table is initialized, but the entries in
445 the table are not (necessarily) resolved. Once a function is
446 actually called, the code in the PLT is hit and the function is
447 resolved. In order to better illustrate this, an example is in
448 order; the following example is from the gdb testsuite.
449
450 We start the program shmain.
451
452 [kev@arroyo testsuite]$ ../gdb gdb.base/shmain
453 [...]
454
455 We place two breakpoints, one on shr1 and the other on main.
456
457 (gdb) b shr1
458 Breakpoint 1 at 0x100409d4
459 (gdb) b main
460 Breakpoint 2 at 0x100006a0: file gdb.base/shmain.c, line 44.
461
462 Examine the instruction (and the immediatly following instruction)
463 upon which the breakpoint was placed. Note that the PLT entry
464 for shr1 contains zeros.
465
466 (gdb) x/2i 0x100409d4
467 0x100409d4 <shr1>: .long 0x0
468 0x100409d8 <shr1+4>: .long 0x0
469
470 Now run 'til main.
471
472 (gdb) r
473 Starting program: gdb.base/shmain
474 Breakpoint 1 at 0xffaf790: file gdb.base/shr1.c, line 19.
475
476 Breakpoint 2, main ()
477 at gdb.base/shmain.c:44
478 44 g = 1;
479
480 Examine the PLT again. Note that the loading of the shared
481 library has initialized the PLT to code which loads a constant
482 (which I think is an index into the GOT) into r11 and then
483 branchs a short distance to the code which actually does the
484 resolving.
485
486 (gdb) x/2i 0x100409d4
487 0x100409d4 <shr1>: li r11,4
488 0x100409d8 <shr1+4>: b 0x10040984 <sg+4>
489 (gdb) c
490 Continuing.
491
492 Breakpoint 1, shr1 (x=1)
493 at gdb.base/shr1.c:19
494 19 l = 1;
495
496 Now we've hit the breakpoint at shr1. (The breakpoint was
497 reset from the PLT entry to the actual shr1 function after the
498 shared library was loaded.) Note that the PLT entry has been
499 resolved to contain a branch that takes us directly to shr1.
500 (The real one, not the PLT entry.)
501
502 (gdb) x/2i 0x100409d4
503 0x100409d4 <shr1>: b 0xffaf76c <shr1>
504 0x100409d8 <shr1+4>: b 0x10040984 <sg+4>
505
506 The thing to note here is that the PLT entry for shr1 has been
507 changed twice.
508
509 Now the problem should be obvious. GDB places a breakpoint (a
510 trap instruction) on the zero value of the PLT entry for shr1.
511 Later on, after the shared library had been loaded and the PLT
512 initialized, GDB gets a signal indicating this fact and attempts
513 (as it always does when it stops) to remove all the breakpoints.
514
515 The breakpoint removal was causing the former contents (a zero
516 word) to be written back to the now initialized PLT entry thus
517 destroying a portion of the initialization that had occurred only a
518 short time ago. When execution continued, the zero word would be
519 executed as an instruction an an illegal instruction trap was
520 generated instead. (0 is not a legal instruction.)
521
522 The fix for this problem was fairly straightforward. The function
523 memory_remove_breakpoint from mem-break.c was copied to this file,
524 modified slightly, and renamed to ppc_linux_memory_remove_breakpoint.
525 In tm-linux.h, MEMORY_REMOVE_BREAKPOINT is defined to call this new
526 function.
527
528 The differences between ppc_linux_memory_remove_breakpoint () and
529 memory_remove_breakpoint () are minor. All that the former does
530 that the latter does not is check to make sure that the breakpoint
531 location actually contains a breakpoint (trap instruction) prior
532 to attempting to write back the old contents. If it does contain
533 a trap instruction, we allow the old contents to be written back.
534 Otherwise, we silently do nothing.
535
536 The big question is whether memory_remove_breakpoint () should be
537 changed to have the same functionality. The downside is that more
538 traffic is generated for remote targets since we'll have an extra
539 fetch of a memory word each time a breakpoint is removed.
540
541 For the time being, we'll leave this self-modifying-code-friendly
542 version in ppc-linux-tdep.c, but it ought to be migrated somewhere
543 else in the event that some other platform has similar needs with
544 regard to removing breakpoints in some potentially self modifying
545 code. */
482ca3f5
KB
546int
547ppc_linux_memory_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
548{
f4f9705a 549 const unsigned char *bp;
482ca3f5
KB
550 int val;
551 int bplen;
552 char old_contents[BREAKPOINT_MAX];
553
554 /* Determine appropriate breakpoint contents and size for this address. */
555 bp = BREAKPOINT_FROM_PC (&addr, &bplen);
556 if (bp == NULL)
557 error ("Software breakpoints not implemented for this target.");
558
559 val = target_read_memory (addr, old_contents, bplen);
560
561 /* If our breakpoint is no longer at the address, this means that the
562 program modified the code on us, so it is wrong to put back the
563 old value */
564 if (val == 0 && memcmp (bp, old_contents, bplen) == 0)
565 val = target_write_memory (addr, contents_cache, bplen);
566
567 return val;
568}
6ded7999
KB
569
570/* Fetch (and possibly build) an appropriate link_map_offsets
ca557f44 571 structure for GNU/Linux PPC targets using the struct offsets
6ded7999
KB
572 defined in link.h (but without actual reference to that file).
573
ca557f44
AC
574 This makes it possible to access GNU/Linux PPC shared libraries
575 from a GDB that was not built on an GNU/Linux PPC host (for cross
576 debugging). */
6ded7999
KB
577
578struct link_map_offsets *
579ppc_linux_svr4_fetch_link_map_offsets (void)
580{
581 static struct link_map_offsets lmo;
582 static struct link_map_offsets *lmp = NULL;
583
584 if (lmp == NULL)
585 {
586 lmp = &lmo;
587
588 lmo.r_debug_size = 8; /* The actual size is 20 bytes, but
589 this is all we need. */
590 lmo.r_map_offset = 4;
591 lmo.r_map_size = 4;
592
593 lmo.link_map_size = 20; /* The actual size is 560 bytes, but
594 this is all we need. */
595 lmo.l_addr_offset = 0;
596 lmo.l_addr_size = 4;
597
598 lmo.l_name_offset = 4;
599 lmo.l_name_size = 4;
600
601 lmo.l_next_offset = 12;
602 lmo.l_next_size = 4;
603
604 lmo.l_prev_offset = 16;
605 lmo.l_prev_size = 4;
606 }
607
608 return lmp;
609}
7b112f9c 610
2fda4977
DJ
611enum {
612 ELF_NGREG = 48,
613 ELF_NFPREG = 33,
614 ELF_NVRREG = 33
615};
616
617enum {
618 ELF_GREGSET_SIZE = (ELF_NGREG * 4),
619 ELF_FPREGSET_SIZE = (ELF_NFPREG * 8)
620};
621
622void
623ppc_linux_supply_gregset (char *buf)
624{
625 int regi;
626 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
627
628 for (regi = 0; regi < 32; regi++)
629 supply_register (regi, buf + 4 * regi);
630
631 supply_register (PC_REGNUM, buf + 4 * PPC_LINUX_PT_NIP);
632 supply_register (tdep->ppc_lr_regnum, buf + 4 * PPC_LINUX_PT_LNK);
633 supply_register (tdep->ppc_cr_regnum, buf + 4 * PPC_LINUX_PT_CCR);
634 supply_register (tdep->ppc_xer_regnum, buf + 4 * PPC_LINUX_PT_XER);
635 supply_register (tdep->ppc_ctr_regnum, buf + 4 * PPC_LINUX_PT_CTR);
636 if (tdep->ppc_mq_regnum != -1)
637 supply_register (tdep->ppc_mq_regnum, buf + 4 * PPC_LINUX_PT_MQ);
638 supply_register (tdep->ppc_ps_regnum, buf + 4 * PPC_LINUX_PT_MSR);
639}
640
641void
642ppc_linux_supply_fpregset (char *buf)
643{
644 int regi;
645 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
646
647 for (regi = 0; regi < 32; regi++)
648 supply_register (FP0_REGNUM + regi, buf + 8 * regi);
649
650 /* The FPSCR is stored in the low order word of the last doubleword in the
651 fpregset. */
652 supply_register (tdep->ppc_fpscr_regnum, buf + 8 * 32 + 4);
653}
654
655/*
656 Use a local version of this function to get the correct types for regsets.
657*/
658
659static void
660fetch_core_registers (char *core_reg_sect,
661 unsigned core_reg_size,
662 int which,
663 CORE_ADDR reg_addr)
664{
665 if (which == 0)
666 {
667 if (core_reg_size == ELF_GREGSET_SIZE)
668 ppc_linux_supply_gregset (core_reg_sect);
669 else
670 warning ("wrong size gregset struct in core file");
671 }
672 else if (which == 2)
673 {
674 if (core_reg_size == ELF_FPREGSET_SIZE)
675 ppc_linux_supply_fpregset (core_reg_sect);
676 else
677 warning ("wrong size fpregset struct in core file");
678 }
679}
680
681/* Register that we are able to handle ELF file formats using standard
682 procfs "regset" structures. */
683
684static struct core_fns ppc_linux_regset_core_fns =
685{
686 bfd_target_elf_flavour, /* core_flavour */
687 default_check_format, /* check_format */
688 default_core_sniffer, /* core_sniffer */
689 fetch_core_registers, /* core_read_registers */
690 NULL /* next */
691};
692
7b112f9c
JT
693static void
694ppc_linux_init_abi (struct gdbarch_info info,
695 struct gdbarch *gdbarch)
696{
697 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
698
699 /* Until November 2001, gcc was not complying to the SYSV ABI for
700 returning structures less than or equal to 8 bytes in size. It was
701 returning everything in memory. When this was corrected, it wasn't
702 fixed for native platforms. */
703 set_gdbarch_use_struct_convention (gdbarch,
704 ppc_sysv_abi_broken_use_struct_convention);
705
706 if (tdep->wordsize == 4)
707 {
708 /* Note: kevinb/2002-04-12: See note in rs6000_gdbarch_init regarding
709 *_push_arguments(). The same remarks hold for the methods below. */
710 set_gdbarch_frameless_function_invocation (gdbarch,
711 ppc_linux_frameless_function_invocation);
712 set_gdbarch_frame_chain (gdbarch, ppc_linux_frame_chain);
713 set_gdbarch_frame_saved_pc (gdbarch, ppc_linux_frame_saved_pc);
714
715 set_gdbarch_frame_init_saved_regs (gdbarch,
716 ppc_linux_frame_init_saved_regs);
717 set_gdbarch_init_extra_frame_info (gdbarch,
718 ppc_linux_init_extra_frame_info);
719
720 set_gdbarch_memory_remove_breakpoint (gdbarch,
721 ppc_linux_memory_remove_breakpoint);
722 set_solib_svr4_fetch_link_map_offsets
723 (gdbarch, ppc_linux_svr4_fetch_link_map_offsets);
724 }
725}
726
727void
728_initialize_ppc_linux_tdep (void)
729{
05816f70 730 gdbarch_register_osabi (bfd_arch_powerpc, 0, GDB_OSABI_LINUX,
7b112f9c 731 ppc_linux_init_abi);
2fda4977 732 add_core_fns (&ppc_linux_regset_core_fns);
7b112f9c 733}
This page took 0.274262 seconds and 4 git commands to generate.