* Makefile.in (ALLDEPFILES): Add nbsd-tdep.c.
[deliverable/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
c877c8e6 146 the signal_handler_caller flag. Because of our strange definition
d7bd68ca
AC
147 of in_sigtramp below, we can't rely on signal_handler_caller
148 getting set correctly from within blockframe.c. This is why we
149 take pains to set it in init_extra_frame_info(). */
c877c8e6
KB
150
151int
152ppc_linux_in_sigtramp (CORE_ADDR pc, char *func_name)
153{
154 CORE_ADDR lr;
155 CORE_ADDR sp;
156 CORE_ADDR tramp_sp;
157 char buf[4];
158 CORE_ADDR handler;
159
2188cbdd 160 lr = read_register (gdbarch_tdep (current_gdbarch)->ppc_lr_regnum);
c877c8e6
KB
161 if (!ppc_linux_at_sigtramp_return_path (lr))
162 return 0;
163
164 sp = read_register (SP_REGNUM);
165
166 if (target_read_memory (sp, buf, sizeof (buf)) != 0)
167 return 0;
168
169 tramp_sp = extract_unsigned_integer (buf, 4);
170
171 if (target_read_memory (tramp_sp + PPC_LINUX_HANDLER_PTR_OFFSET, buf,
172 sizeof (buf)) != 0)
173 return 0;
174
175 handler = extract_unsigned_integer (buf, 4);
176
177 return (pc == handler || pc == handler + 4);
178}
179
180/*
181 * The signal handler trampoline is on the stack and consists of exactly
182 * two instructions. The easiest and most accurate way of determining
183 * whether the pc is in one of these trampolines is by inspecting the
184 * instructions. It'd be faster though if we could find a way to do this
185 * via some simple address comparisons.
186 */
9aa1e687 187static int
c877c8e6
KB
188ppc_linux_at_sigtramp_return_path (CORE_ADDR pc)
189{
190 char buf[12];
191 unsigned long pcinsn;
192 if (target_read_memory (pc - 4, buf, sizeof (buf)) != 0)
193 return 0;
194
195 /* extract the instruction at the pc */
196 pcinsn = extract_unsigned_integer (buf + 4, 4);
197
198 return (
199 (pcinsn == INSTR_LI_R0_0x7777
200 && extract_unsigned_integer (buf + 8, 4) == INSTR_SC)
201 ||
202 (pcinsn == INSTR_SC
203 && extract_unsigned_integer (buf, 4) == INSTR_LI_R0_0x7777));
204}
205
206CORE_ADDR
207ppc_linux_skip_trampoline_code (CORE_ADDR pc)
208{
209 char buf[4];
210 struct obj_section *sect;
211 struct objfile *objfile;
212 unsigned long insn;
213 CORE_ADDR plt_start = 0;
214 CORE_ADDR symtab = 0;
215 CORE_ADDR strtab = 0;
216 int num_slots = -1;
217 int reloc_index = -1;
218 CORE_ADDR plt_table;
219 CORE_ADDR reloc;
220 CORE_ADDR sym;
221 long symidx;
222 char symname[1024];
223 struct minimal_symbol *msymbol;
224
225 /* Find the section pc is in; return if not in .plt */
226 sect = find_pc_section (pc);
227 if (!sect || strcmp (sect->the_bfd_section->name, ".plt") != 0)
228 return 0;
229
230 objfile = sect->objfile;
231
232 /* Pick up the instruction at pc. It had better be of the
233 form
234 li r11, IDX
235
236 where IDX is an index into the plt_table. */
237
238 if (target_read_memory (pc, buf, 4) != 0)
239 return 0;
240 insn = extract_unsigned_integer (buf, 4);
241
242 if ((insn & 0xffff0000) != 0x39600000 /* li r11, VAL */ )
243 return 0;
244
245 reloc_index = (insn << 16) >> 16;
246
247 /* Find the objfile that pc is in and obtain the information
248 necessary for finding the symbol name. */
249 for (sect = objfile->sections; sect < objfile->sections_end; ++sect)
250 {
251 const char *secname = sect->the_bfd_section->name;
252 if (strcmp (secname, ".plt") == 0)
253 plt_start = sect->addr;
254 else if (strcmp (secname, ".rela.plt") == 0)
255 num_slots = ((int) sect->endaddr - (int) sect->addr) / 12;
256 else if (strcmp (secname, ".dynsym") == 0)
257 symtab = sect->addr;
258 else if (strcmp (secname, ".dynstr") == 0)
259 strtab = sect->addr;
260 }
261
262 /* Make sure we have all the information we need. */
263 if (plt_start == 0 || num_slots == -1 || symtab == 0 || strtab == 0)
264 return 0;
265
266 /* Compute the value of the plt table */
267 plt_table = plt_start + 72 + 8 * num_slots;
268
269 /* Get address of the relocation entry (Elf32_Rela) */
270 if (target_read_memory (plt_table + reloc_index, buf, 4) != 0)
271 return 0;
272 reloc = extract_address (buf, 4);
273
274 sect = find_pc_section (reloc);
275 if (!sect)
276 return 0;
277
278 if (strcmp (sect->the_bfd_section->name, ".text") == 0)
279 return reloc;
280
281 /* Now get the r_info field which is the relocation type and symbol
282 index. */
283 if (target_read_memory (reloc + 4, buf, 4) != 0)
284 return 0;
285 symidx = extract_unsigned_integer (buf, 4);
286
287 /* Shift out the relocation type leaving just the symbol index */
288 /* symidx = ELF32_R_SYM(symidx); */
289 symidx = symidx >> 8;
290
291 /* compute the address of the symbol */
292 sym = symtab + symidx * 4;
293
294 /* Fetch the string table index */
295 if (target_read_memory (sym, buf, 4) != 0)
296 return 0;
297 symidx = extract_unsigned_integer (buf, 4);
298
299 /* Fetch the string; we don't know how long it is. Is it possible
300 that the following will fail because we're trying to fetch too
301 much? */
302 if (target_read_memory (strtab + symidx, symname, sizeof (symname)) != 0)
303 return 0;
304
305 /* This might not work right if we have multiple symbols with the
306 same name; the only way to really get it right is to perform
307 the same sort of lookup as the dynamic linker. */
308 msymbol = lookup_minimal_symbol_text (symname, NULL, NULL);
309 if (!msymbol)
310 return 0;
311
312 return SYMBOL_VALUE_ADDRESS (msymbol);
313}
314
315/* The rs6000 version of FRAME_SAVED_PC will almost work for us. The
316 signal handler details are different, so we'll handle those here
317 and call the rs6000 version to do the rest. */
9aa1e687 318CORE_ADDR
c877c8e6
KB
319ppc_linux_frame_saved_pc (struct frame_info *fi)
320{
321 if (fi->signal_handler_caller)
322 {
323 CORE_ADDR regs_addr =
50c9bd31 324 read_memory_integer (fi->frame + PPC_LINUX_REGS_PTR_OFFSET, 4);
c877c8e6
KB
325 /* return the NIP in the regs array */
326 return read_memory_integer (regs_addr + 4 * PPC_LINUX_PT_NIP, 4);
327 }
50c9bd31
KB
328 else if (fi->next && fi->next->signal_handler_caller)
329 {
330 CORE_ADDR regs_addr =
331 read_memory_integer (fi->next->frame + PPC_LINUX_REGS_PTR_OFFSET, 4);
332 /* return LNK in the regs array */
333 return read_memory_integer (regs_addr + 4 * PPC_LINUX_PT_LNK, 4);
334 }
335 else
336 return rs6000_frame_saved_pc (fi);
c877c8e6
KB
337}
338
339void
340ppc_linux_init_extra_frame_info (int fromleaf, struct frame_info *fi)
341{
342 rs6000_init_extra_frame_info (fromleaf, fi);
343
344 if (fi->next != 0)
345 {
346 /* We're called from get_prev_frame_info; check to see if
347 this is a signal frame by looking to see if the pc points
348 at trampoline code */
349 if (ppc_linux_at_sigtramp_return_path (fi->pc))
350 fi->signal_handler_caller = 1;
351 else
352 fi->signal_handler_caller = 0;
353 }
354}
355
356int
357ppc_linux_frameless_function_invocation (struct frame_info *fi)
358{
359 /* We'll find the wrong thing if we let
360 rs6000_frameless_function_invocation () search for a signal trampoline */
361 if (ppc_linux_at_sigtramp_return_path (fi->pc))
362 return 0;
363 else
364 return rs6000_frameless_function_invocation (fi);
365}
366
367void
368ppc_linux_frame_init_saved_regs (struct frame_info *fi)
369{
370 if (fi->signal_handler_caller)
371 {
372 CORE_ADDR regs_addr;
373 int i;
374 if (fi->saved_regs)
375 return;
376
377 frame_saved_regs_zalloc (fi);
378
379 regs_addr =
380 read_memory_integer (fi->frame + PPC_LINUX_REGS_PTR_OFFSET, 4);
381 fi->saved_regs[PC_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_NIP;
2188cbdd
EZ
382 fi->saved_regs[gdbarch_tdep (current_gdbarch)->ppc_ps_regnum] =
383 regs_addr + 4 * PPC_LINUX_PT_MSR;
384 fi->saved_regs[gdbarch_tdep (current_gdbarch)->ppc_cr_regnum] =
385 regs_addr + 4 * PPC_LINUX_PT_CCR;
386 fi->saved_regs[gdbarch_tdep (current_gdbarch)->ppc_lr_regnum] =
387 regs_addr + 4 * PPC_LINUX_PT_LNK;
388 fi->saved_regs[gdbarch_tdep (current_gdbarch)->ppc_ctr_regnum] =
389 regs_addr + 4 * PPC_LINUX_PT_CTR;
390 fi->saved_regs[gdbarch_tdep (current_gdbarch)->ppc_xer_regnum] =
391 regs_addr + 4 * PPC_LINUX_PT_XER;
392 fi->saved_regs[gdbarch_tdep (current_gdbarch)->ppc_mq_regnum] =
393 regs_addr + 4 * PPC_LINUX_PT_MQ;
c877c8e6 394 for (i = 0; i < 32; i++)
2188cbdd
EZ
395 fi->saved_regs[gdbarch_tdep (current_gdbarch)->ppc_gp0_regnum + i] =
396 regs_addr + 4 * PPC_LINUX_PT_R0 + 4 * i;
c877c8e6
KB
397 for (i = 0; i < 32; i++)
398 fi->saved_regs[FP0_REGNUM + i] = regs_addr + 4 * PPC_LINUX_PT_FPR0 + 8 * i;
399 }
400 else
401 rs6000_frame_init_saved_regs (fi);
402}
403
404CORE_ADDR
405ppc_linux_frame_chain (struct frame_info *thisframe)
406{
407 /* Kernel properly constructs the frame chain for the handler */
408 if (thisframe->signal_handler_caller)
409 return read_memory_integer ((thisframe)->frame, 4);
410 else
411 return rs6000_frame_chain (thisframe);
412}
413
414/* FIXME: Move the following to rs6000-tdep.c (or some other file where
415 it may be used generically by ports which use either the SysV ABI or
416 the EABI */
417
ace1378a
EZ
418/* Until November 2001, gcc was not complying to the SYSV ABI for
419 returning structures less than or equal to 8 bytes in size. It was
420 returning everything in memory. When this was corrected, it wasn't
421 fixed for native platforms. */
422int
423ppc_sysv_abi_broken_use_struct_convention (int gcc_p, struct type *value_type)
424{
425 if (TYPE_LENGTH (value_type) == 16
426 && TYPE_VECTOR (value_type))
427 return 0;
428
429 return generic_use_struct_convention (gcc_p, value_type);
430}
431
8e0662df
EZ
432/* Structures 8 bytes or less long are returned in the r3 & r4
433 registers, according to the SYSV ABI. */
434int
435ppc_sysv_abi_use_struct_convention (int gcc_p, struct type *value_type)
436{
ace1378a
EZ
437 if (TYPE_LENGTH (value_type) == 16
438 && TYPE_VECTOR (value_type))
439 return 0;
440
8e0662df
EZ
441 return (TYPE_LENGTH (value_type) > 8);
442}
443
c877c8e6
KB
444/* round2 rounds x up to the nearest multiple of s assuming that s is a
445 power of 2 */
446
447#undef round2
448#define round2(x,s) ((((long) (x) - 1) & ~(long)((s)-1)) + (s))
449
450/* Pass the arguments in either registers, or in the stack. Using the
451 ppc sysv ABI, the first eight words of the argument list (that might
452 be less than eight parameters if some parameters occupy more than one
453 word) are passed in r3..r10 registers. float and double parameters are
454 passed in fpr's, in addition to that. Rest of the parameters if any
455 are passed in user stack.
456
457 If the function is returning a structure, then the return address is passed
458 in r3, then the first 7 words of the parametes can be passed in registers,
459 starting from r4. */
460
461CORE_ADDR
ea7c478f 462ppc_sysv_abi_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
fba45db2 463 int struct_return, CORE_ADDR struct_addr)
c877c8e6
KB
464{
465 int argno;
ace1378a
EZ
466 /* Next available general register for non-float, non-vector arguments. */
467 int greg;
468 /* Next available floating point register for float arguments. */
469 int freg;
470 /* Next available vector register for vector arguments. */
471 int vreg;
c877c8e6
KB
472 int argstkspace;
473 int structstkspace;
474 int argoffset;
475 int structoffset;
ea7c478f 476 struct value *arg;
c877c8e6
KB
477 struct type *type;
478 int len;
479 char old_sp_buf[4];
480 CORE_ADDR saved_sp;
481
482 greg = struct_return ? 4 : 3;
483 freg = 1;
ace1378a 484 vreg = 2;
c877c8e6
KB
485 argstkspace = 0;
486 structstkspace = 0;
487
488 /* Figure out how much new stack space is required for arguments
489 which don't fit in registers. Unlike the PowerOpen ABI, the
490 SysV ABI doesn't reserve any extra space for parameters which
491 are put in registers. */
492 for (argno = 0; argno < nargs; argno++)
493 {
494 arg = args[argno];
495 type = check_typedef (VALUE_TYPE (arg));
496 len = TYPE_LENGTH (type);
497
498 if (TYPE_CODE (type) == TYPE_CODE_FLT)
499 {
500 if (freg <= 8)
501 freg++;
502 else
503 {
504 /* SysV ABI converts floats to doubles when placed in
505 memory and requires 8 byte alignment */
506 if (argstkspace & 0x4)
507 argstkspace += 4;
508 argstkspace += 8;
509 }
510 }
511 else if (TYPE_CODE (type) == TYPE_CODE_INT && len == 8) /* long long */
512 {
513 if (greg > 9)
514 {
515 greg = 11;
516 if (argstkspace & 0x4)
517 argstkspace += 4;
518 argstkspace += 8;
519 }
520 else
521 {
522 if ((greg & 1) == 0)
523 greg++;
524 greg += 2;
525 }
526 }
ace1378a
EZ
527 else if (!TYPE_VECTOR (type))
528 {
c877c8e6
KB
529 if (len > 4
530 || TYPE_CODE (type) == TYPE_CODE_STRUCT
531 || TYPE_CODE (type) == TYPE_CODE_UNION)
532 {
533 /* Rounding to the nearest multiple of 8 may not be necessary,
ace1378a
EZ
534 but it is safe. Particularly since we don't know the
535 field types of the structure */
c877c8e6
KB
536 structstkspace += round2 (len, 8);
537 }
538 if (greg <= 10)
539 greg++;
540 else
541 argstkspace += 4;
ace1378a
EZ
542 }
543 else
544 {
545 if (len == 16
546 && TYPE_CODE (type) == TYPE_CODE_ARRAY
547 && TYPE_VECTOR (type))
548 {
549 if (vreg <= 13)
550 vreg++;
551 else
552 {
553 /* Vector arguments must be aligned to 16 bytes on
554 the stack. */
555 argstkspace += round2 (argstkspace, 16);
556 argstkspace += 16;
557 }
558 }
c877c8e6
KB
559 }
560 }
561
562 /* Get current SP location */
563 saved_sp = read_sp ();
564
565 sp -= argstkspace + structstkspace;
566
567 /* Allocate space for backchain and callee's saved lr */
568 sp -= 8;
569
570 /* Make sure that we maintain 16 byte alignment */
571 sp &= ~0x0f;
572
573 /* Update %sp before proceeding any further */
574 write_register (SP_REGNUM, sp);
575
576 /* write the backchain */
577 store_address (old_sp_buf, 4, saved_sp);
578 write_memory (sp, old_sp_buf, 4);
579
580 argoffset = 8;
581 structoffset = argoffset + argstkspace;
582 freg = 1;
583 greg = 3;
ace1378a 584 vreg = 2;
482ca3f5
KB
585 /* Fill in r3 with the return structure, if any */
586 if (struct_return)
587 {
588 char val_buf[4];
589 store_address (val_buf, 4, struct_addr);
590 memcpy (&registers[REGISTER_BYTE (greg)], val_buf, 4);
591 greg++;
592 }
c877c8e6
KB
593 /* Now fill in the registers and stack... */
594 for (argno = 0; argno < nargs; argno++)
595 {
596 arg = args[argno];
597 type = check_typedef (VALUE_TYPE (arg));
598 len = TYPE_LENGTH (type);
599
600 if (TYPE_CODE (type) == TYPE_CODE_FLT)
601 {
602 if (freg <= 8)
603 {
604 if (len > 8)
605 printf_unfiltered (
ace1378a 606 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
c877c8e6
KB
607 memcpy (&registers[REGISTER_BYTE (FP0_REGNUM + freg)],
608 VALUE_CONTENTS (arg), len);
609 freg++;
610 }
611 else
612 {
613 /* SysV ABI converts floats to doubles when placed in
614 memory and requires 8 byte alignment */
615 /* FIXME: Convert floats to doubles */
616 if (argoffset & 0x4)
617 argoffset += 4;
618 write_memory (sp + argoffset, (char *) VALUE_CONTENTS (arg), len);
619 argoffset += 8;
620 }
621 }
622 else if (TYPE_CODE (type) == TYPE_CODE_INT && len == 8) /* long long */
623 {
624 if (greg > 9)
625 {
626 greg = 11;
627 if (argoffset & 0x4)
628 argoffset += 4;
629 write_memory (sp + argoffset, (char *) VALUE_CONTENTS (arg), len);
630 argoffset += 8;
631 }
632 else
633 {
634 if ((greg & 1) == 0)
635 greg++;
636
637 memcpy (&registers[REGISTER_BYTE (greg)],
638 VALUE_CONTENTS (arg), 4);
639 memcpy (&registers[REGISTER_BYTE (greg + 1)],
640 VALUE_CONTENTS (arg) + 4, 4);
641 greg += 2;
642 }
643 }
ace1378a 644 else if (!TYPE_VECTOR (type))
c877c8e6
KB
645 {
646 char val_buf[4];
647 if (len > 4
648 || TYPE_CODE (type) == TYPE_CODE_STRUCT
649 || TYPE_CODE (type) == TYPE_CODE_UNION)
650 {
651 write_memory (sp + structoffset, VALUE_CONTENTS (arg), len);
652 store_address (val_buf, 4, sp + structoffset);
653 structoffset += round2 (len, 8);
654 }
655 else
656 {
657 memset (val_buf, 0, 4);
658 memcpy (val_buf, VALUE_CONTENTS (arg), len);
659 }
660 if (greg <= 10)
661 {
c877c8e6
KB
662 memcpy (&registers[REGISTER_BYTE (greg)], val_buf, 4);
663 greg++;
664 }
665 else
666 {
667 write_memory (sp + argoffset, val_buf, 4);
668 argoffset += 4;
669 }
670 }
ace1378a
EZ
671 else
672 {
673 if (len == 16
674 && TYPE_CODE (type) == TYPE_CODE_ARRAY
675 && TYPE_VECTOR (type))
676 {
677 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
678 char *v_val_buf = alloca (16);
679 memset (v_val_buf, 0, 16);
680 memcpy (v_val_buf, VALUE_CONTENTS (arg), len);
681 if (vreg <= 13)
682 {
683 memcpy (&registers[REGISTER_BYTE (tdep->ppc_vr0_regnum
684 + vreg)],
685 v_val_buf, 16);
686 vreg++;
687 }
688 else
689 {
690 write_memory (sp + argoffset, v_val_buf, 16);
691 argoffset += 16;
692 }
693 }
694 }
c877c8e6
KB
695 }
696
697 target_store_registers (-1);
698 return sp;
699}
482ca3f5 700
122a33de
KB
701/* ppc_linux_memory_remove_breakpoints attempts to remove a breakpoint
702 in much the same fashion as memory_remove_breakpoint in mem-break.c,
703 but is careful not to write back the previous contents if the code
704 in question has changed in between inserting the breakpoint and
705 removing it.
706
707 Here is the problem that we're trying to solve...
708
709 Once upon a time, before introducing this function to remove
710 breakpoints from the inferior, setting a breakpoint on a shared
711 library function prior to running the program would not work
712 properly. In order to understand the problem, it is first
713 necessary to understand a little bit about dynamic linking on
714 this platform.
715
716 A call to a shared library function is accomplished via a bl
717 (branch-and-link) instruction whose branch target is an entry
718 in the procedure linkage table (PLT). The PLT in the object
719 file is uninitialized. To gdb, prior to running the program, the
720 entries in the PLT are all zeros.
721
722 Once the program starts running, the shared libraries are loaded
723 and the procedure linkage table is initialized, but the entries in
724 the table are not (necessarily) resolved. Once a function is
725 actually called, the code in the PLT is hit and the function is
726 resolved. In order to better illustrate this, an example is in
727 order; the following example is from the gdb testsuite.
728
729 We start the program shmain.
730
731 [kev@arroyo testsuite]$ ../gdb gdb.base/shmain
732 [...]
733
734 We place two breakpoints, one on shr1 and the other on main.
735
736 (gdb) b shr1
737 Breakpoint 1 at 0x100409d4
738 (gdb) b main
739 Breakpoint 2 at 0x100006a0: file gdb.base/shmain.c, line 44.
740
741 Examine the instruction (and the immediatly following instruction)
742 upon which the breakpoint was placed. Note that the PLT entry
743 for shr1 contains zeros.
744
745 (gdb) x/2i 0x100409d4
746 0x100409d4 <shr1>: .long 0x0
747 0x100409d8 <shr1+4>: .long 0x0
748
749 Now run 'til main.
750
751 (gdb) r
752 Starting program: gdb.base/shmain
753 Breakpoint 1 at 0xffaf790: file gdb.base/shr1.c, line 19.
754
755 Breakpoint 2, main ()
756 at gdb.base/shmain.c:44
757 44 g = 1;
758
759 Examine the PLT again. Note that the loading of the shared
760 library has initialized the PLT to code which loads a constant
761 (which I think is an index into the GOT) into r11 and then
762 branchs a short distance to the code which actually does the
763 resolving.
764
765 (gdb) x/2i 0x100409d4
766 0x100409d4 <shr1>: li r11,4
767 0x100409d8 <shr1+4>: b 0x10040984 <sg+4>
768 (gdb) c
769 Continuing.
770
771 Breakpoint 1, shr1 (x=1)
772 at gdb.base/shr1.c:19
773 19 l = 1;
774
775 Now we've hit the breakpoint at shr1. (The breakpoint was
776 reset from the PLT entry to the actual shr1 function after the
777 shared library was loaded.) Note that the PLT entry has been
778 resolved to contain a branch that takes us directly to shr1.
779 (The real one, not the PLT entry.)
780
781 (gdb) x/2i 0x100409d4
782 0x100409d4 <shr1>: b 0xffaf76c <shr1>
783 0x100409d8 <shr1+4>: b 0x10040984 <sg+4>
784
785 The thing to note here is that the PLT entry for shr1 has been
786 changed twice.
787
788 Now the problem should be obvious. GDB places a breakpoint (a
789 trap instruction) on the zero value of the PLT entry for shr1.
790 Later on, after the shared library had been loaded and the PLT
791 initialized, GDB gets a signal indicating this fact and attempts
792 (as it always does when it stops) to remove all the breakpoints.
793
794 The breakpoint removal was causing the former contents (a zero
795 word) to be written back to the now initialized PLT entry thus
796 destroying a portion of the initialization that had occurred only a
797 short time ago. When execution continued, the zero word would be
798 executed as an instruction an an illegal instruction trap was
799 generated instead. (0 is not a legal instruction.)
800
801 The fix for this problem was fairly straightforward. The function
802 memory_remove_breakpoint from mem-break.c was copied to this file,
803 modified slightly, and renamed to ppc_linux_memory_remove_breakpoint.
804 In tm-linux.h, MEMORY_REMOVE_BREAKPOINT is defined to call this new
805 function.
806
807 The differences between ppc_linux_memory_remove_breakpoint () and
808 memory_remove_breakpoint () are minor. All that the former does
809 that the latter does not is check to make sure that the breakpoint
810 location actually contains a breakpoint (trap instruction) prior
811 to attempting to write back the old contents. If it does contain
812 a trap instruction, we allow the old contents to be written back.
813 Otherwise, we silently do nothing.
814
815 The big question is whether memory_remove_breakpoint () should be
816 changed to have the same functionality. The downside is that more
817 traffic is generated for remote targets since we'll have an extra
818 fetch of a memory word each time a breakpoint is removed.
819
820 For the time being, we'll leave this self-modifying-code-friendly
821 version in ppc-linux-tdep.c, but it ought to be migrated somewhere
822 else in the event that some other platform has similar needs with
823 regard to removing breakpoints in some potentially self modifying
824 code. */
482ca3f5
KB
825int
826ppc_linux_memory_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
827{
f4f9705a 828 const unsigned char *bp;
482ca3f5
KB
829 int val;
830 int bplen;
831 char old_contents[BREAKPOINT_MAX];
832
833 /* Determine appropriate breakpoint contents and size for this address. */
834 bp = BREAKPOINT_FROM_PC (&addr, &bplen);
835 if (bp == NULL)
836 error ("Software breakpoints not implemented for this target.");
837
838 val = target_read_memory (addr, old_contents, bplen);
839
840 /* If our breakpoint is no longer at the address, this means that the
841 program modified the code on us, so it is wrong to put back the
842 old value */
843 if (val == 0 && memcmp (bp, old_contents, bplen) == 0)
844 val = target_write_memory (addr, contents_cache, bplen);
845
846 return val;
847}
6ded7999
KB
848
849/* Fetch (and possibly build) an appropriate link_map_offsets
ca557f44 850 structure for GNU/Linux PPC targets using the struct offsets
6ded7999
KB
851 defined in link.h (but without actual reference to that file).
852
ca557f44
AC
853 This makes it possible to access GNU/Linux PPC shared libraries
854 from a GDB that was not built on an GNU/Linux PPC host (for cross
855 debugging). */
6ded7999
KB
856
857struct link_map_offsets *
858ppc_linux_svr4_fetch_link_map_offsets (void)
859{
860 static struct link_map_offsets lmo;
861 static struct link_map_offsets *lmp = NULL;
862
863 if (lmp == NULL)
864 {
865 lmp = &lmo;
866
867 lmo.r_debug_size = 8; /* The actual size is 20 bytes, but
868 this is all we need. */
869 lmo.r_map_offset = 4;
870 lmo.r_map_size = 4;
871
872 lmo.link_map_size = 20; /* The actual size is 560 bytes, but
873 this is all we need. */
874 lmo.l_addr_offset = 0;
875 lmo.l_addr_size = 4;
876
877 lmo.l_name_offset = 4;
878 lmo.l_name_size = 4;
879
880 lmo.l_next_offset = 12;
881 lmo.l_next_size = 4;
882
883 lmo.l_prev_offset = 16;
884 lmo.l_prev_size = 4;
885 }
886
887 return lmp;
888}
This page took 0.208594 seconds and 4 git commands to generate.