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