2000-07-31 Elena Zannoni <ezannoni@kwikemart.cygnus.com>
[deliverable/binutils-gdb.git] / gdb / ppc-linux-tdep.c
CommitLineData
c877c8e6
KB
1/* Target-dependent code for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include "defs.h"
23#include "frame.h"
24#include "inferior.h"
25#include "symtab.h"
26#include "target.h"
27#include "gdbcore.h"
28#include "gdbcmd.h"
29#include "symfile.h"
30#include "objfiles.h"
31
32/* The following two instructions are used in the signal trampoline
33 code on linux/ppc */
34#define INSTR_LI_R0_0x7777 0x38007777
35#define INSTR_SC 0x44000002
36
37/* Since the *-tdep.c files are platform independent (i.e, they may be
38 used to build cross platform debuggers), we can't include system
39 headers. Therefore, details concerning the sigcontext structure
40 must be painstakingly rerecorded. What's worse, if these details
41 ever change in the header files, they'll have to be changed here
42 as well. */
43
44/* __SIGNAL_FRAMESIZE from <asm/ptrace.h> */
45#define PPC_LINUX_SIGNAL_FRAMESIZE 64
46
47/* From <asm/sigcontext.h>, offsetof(struct sigcontext_struct, regs) == 0x1c */
48#define PPC_LINUX_REGS_PTR_OFFSET (PPC_LINUX_SIGNAL_FRAMESIZE + 0x1c)
49
50/* From <asm/sigcontext.h>,
51 offsetof(struct sigcontext_struct, handler) == 0x14 */
52#define PPC_LINUX_HANDLER_PTR_OFFSET (PPC_LINUX_SIGNAL_FRAMESIZE + 0x14)
53
54/* From <asm/ptrace.h>, values for PT_NIP, PT_R1, and PT_LNK */
55#define PPC_LINUX_PT_R0 0
56#define PPC_LINUX_PT_R1 1
57#define PPC_LINUX_PT_R2 2
58#define PPC_LINUX_PT_R3 3
59#define PPC_LINUX_PT_R4 4
60#define PPC_LINUX_PT_R5 5
61#define PPC_LINUX_PT_R6 6
62#define PPC_LINUX_PT_R7 7
63#define PPC_LINUX_PT_R8 8
64#define PPC_LINUX_PT_R9 9
65#define PPC_LINUX_PT_R10 10
66#define PPC_LINUX_PT_R11 11
67#define PPC_LINUX_PT_R12 12
68#define PPC_LINUX_PT_R13 13
69#define PPC_LINUX_PT_R14 14
70#define PPC_LINUX_PT_R15 15
71#define PPC_LINUX_PT_R16 16
72#define PPC_LINUX_PT_R17 17
73#define PPC_LINUX_PT_R18 18
74#define PPC_LINUX_PT_R19 19
75#define PPC_LINUX_PT_R20 20
76#define PPC_LINUX_PT_R21 21
77#define PPC_LINUX_PT_R22 22
78#define PPC_LINUX_PT_R23 23
79#define PPC_LINUX_PT_R24 24
80#define PPC_LINUX_PT_R25 25
81#define PPC_LINUX_PT_R26 26
82#define PPC_LINUX_PT_R27 27
83#define PPC_LINUX_PT_R28 28
84#define PPC_LINUX_PT_R29 29
85#define PPC_LINUX_PT_R30 30
86#define PPC_LINUX_PT_R31 31
87#define PPC_LINUX_PT_NIP 32
88#define PPC_LINUX_PT_MSR 33
89#define PPC_LINUX_PT_CTR 35
90#define PPC_LINUX_PT_LNK 36
91#define PPC_LINUX_PT_XER 37
92#define PPC_LINUX_PT_CCR 38
93#define PPC_LINUX_PT_MQ 39
94#define PPC_LINUX_PT_FPR0 48 /* each FP reg occupies 2 slots in this space */
95#define PPC_LINUX_PT_FPR31 (PPC_LINUX_PT_FPR0 + 2*31)
96#define PPC_LINUX_PT_FPSCR (PPC_LINUX_PT_FPR0 + 2*32 + 1)
97
50c9bd31
KB
98int ppc_linux_at_sigtramp_return_path (CORE_ADDR pc);
99
c877c8e6
KB
100/* Determine if pc is in a signal trampoline...
101
102 Ha! That's not what this does at all. wait_for_inferior in infrun.c
103 calls IN_SIGTRAMP in order to detect entry into a signal trampoline
104 just after delivery of a signal. But on linux, signal trampolines
105 are used for the return path only. The kernel sets things up so that
106 the signal handler is called directly.
107
108 If we use in_sigtramp2() in place of in_sigtramp() (see below)
109 we'll (often) end up with stop_pc in the trampoline and prev_pc in
110 the (now exited) handler. The code there will cause a temporary
111 breakpoint to be set on prev_pc which is not very likely to get hit
112 again.
113
114 If this is confusing, think of it this way... the code in
115 wait_for_inferior() needs to be able to detect entry into a signal
116 trampoline just after a signal is delivered, not after the handler
117 has been run.
118
119 So, we define in_sigtramp() below to return 1 if the following is
120 true:
121
122 1) The previous frame is a real signal trampoline.
123
124 - and -
125
126 2) pc is at the first or second instruction of the corresponding
127 handler.
128
129 Why the second instruction? It seems that wait_for_inferior()
130 never sees the first instruction when single stepping. When a
131 signal is delivered while stepping, the next instruction that
132 would've been stepped over isn't, instead a signal is delivered and
133 the first instruction of the handler is stepped over instead. That
134 puts us on the second instruction. (I added the test for the
135 first instruction long after the fact, just in case the observed
136 behavior is ever fixed.)
137
138 IN_SIGTRAMP is called from blockframe.c as well in order to set
139 the signal_handler_caller flag. Because of our strange definition
140 of in_sigtramp below, we can't rely on signal_handler_caller getting
141 set correctly from within blockframe.c. This is why we take pains
142 to set it in init_extra_frame_info(). */
143
144int
145ppc_linux_in_sigtramp (CORE_ADDR pc, char *func_name)
146{
147 CORE_ADDR lr;
148 CORE_ADDR sp;
149 CORE_ADDR tramp_sp;
150 char buf[4];
151 CORE_ADDR handler;
152
153 lr = read_register (LR_REGNUM);
154 if (!ppc_linux_at_sigtramp_return_path (lr))
155 return 0;
156
157 sp = read_register (SP_REGNUM);
158
159 if (target_read_memory (sp, buf, sizeof (buf)) != 0)
160 return 0;
161
162 tramp_sp = extract_unsigned_integer (buf, 4);
163
164 if (target_read_memory (tramp_sp + PPC_LINUX_HANDLER_PTR_OFFSET, buf,
165 sizeof (buf)) != 0)
166 return 0;
167
168 handler = extract_unsigned_integer (buf, 4);
169
170 return (pc == handler || pc == handler + 4);
171}
172
173/*
174 * The signal handler trampoline is on the stack and consists of exactly
175 * two instructions. The easiest and most accurate way of determining
176 * whether the pc is in one of these trampolines is by inspecting the
177 * instructions. It'd be faster though if we could find a way to do this
178 * via some simple address comparisons.
179 */
180int
181ppc_linux_at_sigtramp_return_path (CORE_ADDR pc)
182{
183 char buf[12];
184 unsigned long pcinsn;
185 if (target_read_memory (pc - 4, buf, sizeof (buf)) != 0)
186 return 0;
187
188 /* extract the instruction at the pc */
189 pcinsn = extract_unsigned_integer (buf + 4, 4);
190
191 return (
192 (pcinsn == INSTR_LI_R0_0x7777
193 && extract_unsigned_integer (buf + 8, 4) == INSTR_SC)
194 ||
195 (pcinsn == INSTR_SC
196 && extract_unsigned_integer (buf, 4) == INSTR_LI_R0_0x7777));
197}
198
199CORE_ADDR
200ppc_linux_skip_trampoline_code (CORE_ADDR pc)
201{
202 char buf[4];
203 struct obj_section *sect;
204 struct objfile *objfile;
205 unsigned long insn;
206 CORE_ADDR plt_start = 0;
207 CORE_ADDR symtab = 0;
208 CORE_ADDR strtab = 0;
209 int num_slots = -1;
210 int reloc_index = -1;
211 CORE_ADDR plt_table;
212 CORE_ADDR reloc;
213 CORE_ADDR sym;
214 long symidx;
215 char symname[1024];
216 struct minimal_symbol *msymbol;
217
218 /* Find the section pc is in; return if not in .plt */
219 sect = find_pc_section (pc);
220 if (!sect || strcmp (sect->the_bfd_section->name, ".plt") != 0)
221 return 0;
222
223 objfile = sect->objfile;
224
225 /* Pick up the instruction at pc. It had better be of the
226 form
227 li r11, IDX
228
229 where IDX is an index into the plt_table. */
230
231 if (target_read_memory (pc, buf, 4) != 0)
232 return 0;
233 insn = extract_unsigned_integer (buf, 4);
234
235 if ((insn & 0xffff0000) != 0x39600000 /* li r11, VAL */ )
236 return 0;
237
238 reloc_index = (insn << 16) >> 16;
239
240 /* Find the objfile that pc is in and obtain the information
241 necessary for finding the symbol name. */
242 for (sect = objfile->sections; sect < objfile->sections_end; ++sect)
243 {
244 const char *secname = sect->the_bfd_section->name;
245 if (strcmp (secname, ".plt") == 0)
246 plt_start = sect->addr;
247 else if (strcmp (secname, ".rela.plt") == 0)
248 num_slots = ((int) sect->endaddr - (int) sect->addr) / 12;
249 else if (strcmp (secname, ".dynsym") == 0)
250 symtab = sect->addr;
251 else if (strcmp (secname, ".dynstr") == 0)
252 strtab = sect->addr;
253 }
254
255 /* Make sure we have all the information we need. */
256 if (plt_start == 0 || num_slots == -1 || symtab == 0 || strtab == 0)
257 return 0;
258
259 /* Compute the value of the plt table */
260 plt_table = plt_start + 72 + 8 * num_slots;
261
262 /* Get address of the relocation entry (Elf32_Rela) */
263 if (target_read_memory (plt_table + reloc_index, buf, 4) != 0)
264 return 0;
265 reloc = extract_address (buf, 4);
266
267 sect = find_pc_section (reloc);
268 if (!sect)
269 return 0;
270
271 if (strcmp (sect->the_bfd_section->name, ".text") == 0)
272 return reloc;
273
274 /* Now get the r_info field which is the relocation type and symbol
275 index. */
276 if (target_read_memory (reloc + 4, buf, 4) != 0)
277 return 0;
278 symidx = extract_unsigned_integer (buf, 4);
279
280 /* Shift out the relocation type leaving just the symbol index */
281 /* symidx = ELF32_R_SYM(symidx); */
282 symidx = symidx >> 8;
283
284 /* compute the address of the symbol */
285 sym = symtab + symidx * 4;
286
287 /* Fetch the string table index */
288 if (target_read_memory (sym, buf, 4) != 0)
289 return 0;
290 symidx = extract_unsigned_integer (buf, 4);
291
292 /* Fetch the string; we don't know how long it is. Is it possible
293 that the following will fail because we're trying to fetch too
294 much? */
295 if (target_read_memory (strtab + symidx, symname, sizeof (symname)) != 0)
296 return 0;
297
298 /* This might not work right if we have multiple symbols with the
299 same name; the only way to really get it right is to perform
300 the same sort of lookup as the dynamic linker. */
301 msymbol = lookup_minimal_symbol_text (symname, NULL, NULL);
302 if (!msymbol)
303 return 0;
304
305 return SYMBOL_VALUE_ADDRESS (msymbol);
306}
307
308/* The rs6000 version of FRAME_SAVED_PC will almost work for us. The
309 signal handler details are different, so we'll handle those here
310 and call the rs6000 version to do the rest. */
311unsigned long
312ppc_linux_frame_saved_pc (struct frame_info *fi)
313{
314 if (fi->signal_handler_caller)
315 {
316 CORE_ADDR regs_addr =
50c9bd31 317 read_memory_integer (fi->frame + PPC_LINUX_REGS_PTR_OFFSET, 4);
c877c8e6
KB
318 /* return the NIP in the regs array */
319 return read_memory_integer (regs_addr + 4 * PPC_LINUX_PT_NIP, 4);
320 }
50c9bd31
KB
321 else if (fi->next && fi->next->signal_handler_caller)
322 {
323 CORE_ADDR regs_addr =
324 read_memory_integer (fi->next->frame + PPC_LINUX_REGS_PTR_OFFSET, 4);
325 /* return LNK in the regs array */
326 return read_memory_integer (regs_addr + 4 * PPC_LINUX_PT_LNK, 4);
327 }
328 else
329 return rs6000_frame_saved_pc (fi);
c877c8e6
KB
330}
331
332void
333ppc_linux_init_extra_frame_info (int fromleaf, struct frame_info *fi)
334{
335 rs6000_init_extra_frame_info (fromleaf, fi);
336
337 if (fi->next != 0)
338 {
339 /* We're called from get_prev_frame_info; check to see if
340 this is a signal frame by looking to see if the pc points
341 at trampoline code */
342 if (ppc_linux_at_sigtramp_return_path (fi->pc))
343 fi->signal_handler_caller = 1;
344 else
345 fi->signal_handler_caller = 0;
346 }
347}
348
349int
350ppc_linux_frameless_function_invocation (struct frame_info *fi)
351{
352 /* We'll find the wrong thing if we let
353 rs6000_frameless_function_invocation () search for a signal trampoline */
354 if (ppc_linux_at_sigtramp_return_path (fi->pc))
355 return 0;
356 else
357 return rs6000_frameless_function_invocation (fi);
358}
359
360void
361ppc_linux_frame_init_saved_regs (struct frame_info *fi)
362{
363 if (fi->signal_handler_caller)
364 {
365 CORE_ADDR regs_addr;
366 int i;
367 if (fi->saved_regs)
368 return;
369
370 frame_saved_regs_zalloc (fi);
371
372 regs_addr =
373 read_memory_integer (fi->frame + PPC_LINUX_REGS_PTR_OFFSET, 4);
374 fi->saved_regs[PC_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_NIP;
375 fi->saved_regs[PS_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_MSR;
376 fi->saved_regs[CR_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_CCR;
377 fi->saved_regs[LR_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_LNK;
378 fi->saved_regs[CTR_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_CTR;
379 fi->saved_regs[XER_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_XER;
380 fi->saved_regs[MQ_REGNUM] = regs_addr + 4 * PPC_LINUX_PT_MQ;
381 for (i = 0; i < 32; i++)
382 fi->saved_regs[GP0_REGNUM + i] = regs_addr + 4 * PPC_LINUX_PT_R0 + 4 * i;
383 for (i = 0; i < 32; i++)
384 fi->saved_regs[FP0_REGNUM + i] = regs_addr + 4 * PPC_LINUX_PT_FPR0 + 8 * i;
385 }
386 else
387 rs6000_frame_init_saved_regs (fi);
388}
389
390CORE_ADDR
391ppc_linux_frame_chain (struct frame_info *thisframe)
392{
393 /* Kernel properly constructs the frame chain for the handler */
394 if (thisframe->signal_handler_caller)
395 return read_memory_integer ((thisframe)->frame, 4);
396 else
397 return rs6000_frame_chain (thisframe);
398}
399
400/* FIXME: Move the following to rs6000-tdep.c (or some other file where
401 it may be used generically by ports which use either the SysV ABI or
402 the EABI */
403
404/* round2 rounds x up to the nearest multiple of s assuming that s is a
405 power of 2 */
406
407#undef round2
408#define round2(x,s) ((((long) (x) - 1) & ~(long)((s)-1)) + (s))
409
410/* Pass the arguments in either registers, or in the stack. Using the
411 ppc sysv ABI, the first eight words of the argument list (that might
412 be less than eight parameters if some parameters occupy more than one
413 word) are passed in r3..r10 registers. float and double parameters are
414 passed in fpr's, in addition to that. Rest of the parameters if any
415 are passed in user stack.
416
417 If the function is returning a structure, then the return address is passed
418 in r3, then the first 7 words of the parametes can be passed in registers,
419 starting from r4. */
420
421CORE_ADDR
fba45db2
KB
422ppc_sysv_abi_push_arguments (int nargs, value_ptr *args, CORE_ADDR sp,
423 int struct_return, CORE_ADDR struct_addr)
c877c8e6
KB
424{
425 int argno;
426 int greg, freg;
427 int argstkspace;
428 int structstkspace;
429 int argoffset;
430 int structoffset;
431 value_ptr arg;
432 struct type *type;
433 int len;
434 char old_sp_buf[4];
435 CORE_ADDR saved_sp;
436
437 greg = struct_return ? 4 : 3;
438 freg = 1;
439 argstkspace = 0;
440 structstkspace = 0;
441
442 /* Figure out how much new stack space is required for arguments
443 which don't fit in registers. Unlike the PowerOpen ABI, the
444 SysV ABI doesn't reserve any extra space for parameters which
445 are put in registers. */
446 for (argno = 0; argno < nargs; argno++)
447 {
448 arg = args[argno];
449 type = check_typedef (VALUE_TYPE (arg));
450 len = TYPE_LENGTH (type);
451
452 if (TYPE_CODE (type) == TYPE_CODE_FLT)
453 {
454 if (freg <= 8)
455 freg++;
456 else
457 {
458 /* SysV ABI converts floats to doubles when placed in
459 memory and requires 8 byte alignment */
460 if (argstkspace & 0x4)
461 argstkspace += 4;
462 argstkspace += 8;
463 }
464 }
465 else if (TYPE_CODE (type) == TYPE_CODE_INT && len == 8) /* long long */
466 {
467 if (greg > 9)
468 {
469 greg = 11;
470 if (argstkspace & 0x4)
471 argstkspace += 4;
472 argstkspace += 8;
473 }
474 else
475 {
476 if ((greg & 1) == 0)
477 greg++;
478 greg += 2;
479 }
480 }
481 else
482 {
483 if (len > 4
484 || TYPE_CODE (type) == TYPE_CODE_STRUCT
485 || TYPE_CODE (type) == TYPE_CODE_UNION)
486 {
487 /* Rounding to the nearest multiple of 8 may not be necessary,
488 but it is safe. Particularly since we don't know the
489 field types of the structure */
490 structstkspace += round2 (len, 8);
491 }
492 if (greg <= 10)
493 greg++;
494 else
495 argstkspace += 4;
496 }
497 }
498
499 /* Get current SP location */
500 saved_sp = read_sp ();
501
502 sp -= argstkspace + structstkspace;
503
504 /* Allocate space for backchain and callee's saved lr */
505 sp -= 8;
506
507 /* Make sure that we maintain 16 byte alignment */
508 sp &= ~0x0f;
509
510 /* Update %sp before proceeding any further */
511 write_register (SP_REGNUM, sp);
512
513 /* write the backchain */
514 store_address (old_sp_buf, 4, saved_sp);
515 write_memory (sp, old_sp_buf, 4);
516
517 argoffset = 8;
518 structoffset = argoffset + argstkspace;
519 freg = 1;
520 greg = 3;
482ca3f5
KB
521 /* Fill in r3 with the return structure, if any */
522 if (struct_return)
523 {
524 char val_buf[4];
525 store_address (val_buf, 4, struct_addr);
526 memcpy (&registers[REGISTER_BYTE (greg)], val_buf, 4);
527 greg++;
528 }
c877c8e6
KB
529 /* Now fill in the registers and stack... */
530 for (argno = 0; argno < nargs; argno++)
531 {
532 arg = args[argno];
533 type = check_typedef (VALUE_TYPE (arg));
534 len = TYPE_LENGTH (type);
535
536 if (TYPE_CODE (type) == TYPE_CODE_FLT)
537 {
538 if (freg <= 8)
539 {
540 if (len > 8)
541 printf_unfiltered (
542 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
543 memcpy (&registers[REGISTER_BYTE (FP0_REGNUM + freg)],
544 VALUE_CONTENTS (arg), len);
545 freg++;
546 }
547 else
548 {
549 /* SysV ABI converts floats to doubles when placed in
550 memory and requires 8 byte alignment */
551 /* FIXME: Convert floats to doubles */
552 if (argoffset & 0x4)
553 argoffset += 4;
554 write_memory (sp + argoffset, (char *) VALUE_CONTENTS (arg), len);
555 argoffset += 8;
556 }
557 }
558 else if (TYPE_CODE (type) == TYPE_CODE_INT && len == 8) /* long long */
559 {
560 if (greg > 9)
561 {
562 greg = 11;
563 if (argoffset & 0x4)
564 argoffset += 4;
565 write_memory (sp + argoffset, (char *) VALUE_CONTENTS (arg), len);
566 argoffset += 8;
567 }
568 else
569 {
570 if ((greg & 1) == 0)
571 greg++;
572
573 memcpy (&registers[REGISTER_BYTE (greg)],
574 VALUE_CONTENTS (arg), 4);
575 memcpy (&registers[REGISTER_BYTE (greg + 1)],
576 VALUE_CONTENTS (arg) + 4, 4);
577 greg += 2;
578 }
579 }
580 else
581 {
582 char val_buf[4];
583 if (len > 4
584 || TYPE_CODE (type) == TYPE_CODE_STRUCT
585 || TYPE_CODE (type) == TYPE_CODE_UNION)
586 {
587 write_memory (sp + structoffset, VALUE_CONTENTS (arg), len);
588 store_address (val_buf, 4, sp + structoffset);
589 structoffset += round2 (len, 8);
590 }
591 else
592 {
593 memset (val_buf, 0, 4);
594 memcpy (val_buf, VALUE_CONTENTS (arg), len);
595 }
596 if (greg <= 10)
597 {
598 *(int *) &registers[REGISTER_BYTE (greg)] = 0;
599 memcpy (&registers[REGISTER_BYTE (greg)], val_buf, 4);
600 greg++;
601 }
602 else
603 {
604 write_memory (sp + argoffset, val_buf, 4);
605 argoffset += 4;
606 }
607 }
608 }
609
610 target_store_registers (-1);
611 return sp;
612}
482ca3f5 613
122a33de
KB
614/* ppc_linux_memory_remove_breakpoints attempts to remove a breakpoint
615 in much the same fashion as memory_remove_breakpoint in mem-break.c,
616 but is careful not to write back the previous contents if the code
617 in question has changed in between inserting the breakpoint and
618 removing it.
619
620 Here is the problem that we're trying to solve...
621
622 Once upon a time, before introducing this function to remove
623 breakpoints from the inferior, setting a breakpoint on a shared
624 library function prior to running the program would not work
625 properly. In order to understand the problem, it is first
626 necessary to understand a little bit about dynamic linking on
627 this platform.
628
629 A call to a shared library function is accomplished via a bl
630 (branch-and-link) instruction whose branch target is an entry
631 in the procedure linkage table (PLT). The PLT in the object
632 file is uninitialized. To gdb, prior to running the program, the
633 entries in the PLT are all zeros.
634
635 Once the program starts running, the shared libraries are loaded
636 and the procedure linkage table is initialized, but the entries in
637 the table are not (necessarily) resolved. Once a function is
638 actually called, the code in the PLT is hit and the function is
639 resolved. In order to better illustrate this, an example is in
640 order; the following example is from the gdb testsuite.
641
642 We start the program shmain.
643
644 [kev@arroyo testsuite]$ ../gdb gdb.base/shmain
645 [...]
646
647 We place two breakpoints, one on shr1 and the other on main.
648
649 (gdb) b shr1
650 Breakpoint 1 at 0x100409d4
651 (gdb) b main
652 Breakpoint 2 at 0x100006a0: file gdb.base/shmain.c, line 44.
653
654 Examine the instruction (and the immediatly following instruction)
655 upon which the breakpoint was placed. Note that the PLT entry
656 for shr1 contains zeros.
657
658 (gdb) x/2i 0x100409d4
659 0x100409d4 <shr1>: .long 0x0
660 0x100409d8 <shr1+4>: .long 0x0
661
662 Now run 'til main.
663
664 (gdb) r
665 Starting program: gdb.base/shmain
666 Breakpoint 1 at 0xffaf790: file gdb.base/shr1.c, line 19.
667
668 Breakpoint 2, main ()
669 at gdb.base/shmain.c:44
670 44 g = 1;
671
672 Examine the PLT again. Note that the loading of the shared
673 library has initialized the PLT to code which loads a constant
674 (which I think is an index into the GOT) into r11 and then
675 branchs a short distance to the code which actually does the
676 resolving.
677
678 (gdb) x/2i 0x100409d4
679 0x100409d4 <shr1>: li r11,4
680 0x100409d8 <shr1+4>: b 0x10040984 <sg+4>
681 (gdb) c
682 Continuing.
683
684 Breakpoint 1, shr1 (x=1)
685 at gdb.base/shr1.c:19
686 19 l = 1;
687
688 Now we've hit the breakpoint at shr1. (The breakpoint was
689 reset from the PLT entry to the actual shr1 function after the
690 shared library was loaded.) Note that the PLT entry has been
691 resolved to contain a branch that takes us directly to shr1.
692 (The real one, not the PLT entry.)
693
694 (gdb) x/2i 0x100409d4
695 0x100409d4 <shr1>: b 0xffaf76c <shr1>
696 0x100409d8 <shr1+4>: b 0x10040984 <sg+4>
697
698 The thing to note here is that the PLT entry for shr1 has been
699 changed twice.
700
701 Now the problem should be obvious. GDB places a breakpoint (a
702 trap instruction) on the zero value of the PLT entry for shr1.
703 Later on, after the shared library had been loaded and the PLT
704 initialized, GDB gets a signal indicating this fact and attempts
705 (as it always does when it stops) to remove all the breakpoints.
706
707 The breakpoint removal was causing the former contents (a zero
708 word) to be written back to the now initialized PLT entry thus
709 destroying a portion of the initialization that had occurred only a
710 short time ago. When execution continued, the zero word would be
711 executed as an instruction an an illegal instruction trap was
712 generated instead. (0 is not a legal instruction.)
713
714 The fix for this problem was fairly straightforward. The function
715 memory_remove_breakpoint from mem-break.c was copied to this file,
716 modified slightly, and renamed to ppc_linux_memory_remove_breakpoint.
717 In tm-linux.h, MEMORY_REMOVE_BREAKPOINT is defined to call this new
718 function.
719
720 The differences between ppc_linux_memory_remove_breakpoint () and
721 memory_remove_breakpoint () are minor. All that the former does
722 that the latter does not is check to make sure that the breakpoint
723 location actually contains a breakpoint (trap instruction) prior
724 to attempting to write back the old contents. If it does contain
725 a trap instruction, we allow the old contents to be written back.
726 Otherwise, we silently do nothing.
727
728 The big question is whether memory_remove_breakpoint () should be
729 changed to have the same functionality. The downside is that more
730 traffic is generated for remote targets since we'll have an extra
731 fetch of a memory word each time a breakpoint is removed.
732
733 For the time being, we'll leave this self-modifying-code-friendly
734 version in ppc-linux-tdep.c, but it ought to be migrated somewhere
735 else in the event that some other platform has similar needs with
736 regard to removing breakpoints in some potentially self modifying
737 code. */
482ca3f5
KB
738int
739ppc_linux_memory_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
740{
741 unsigned char *bp;
742 int val;
743 int bplen;
744 char old_contents[BREAKPOINT_MAX];
745
746 /* Determine appropriate breakpoint contents and size for this address. */
747 bp = BREAKPOINT_FROM_PC (&addr, &bplen);
748 if (bp == NULL)
749 error ("Software breakpoints not implemented for this target.");
750
751 val = target_read_memory (addr, old_contents, bplen);
752
753 /* If our breakpoint is no longer at the address, this means that the
754 program modified the code on us, so it is wrong to put back the
755 old value */
756 if (val == 0 && memcmp (bp, old_contents, bplen) == 0)
757 val = target_write_memory (addr, contents_cache, bplen);
758
759 return val;
760}
This page took 0.081206 seconds and 4 git commands to generate.