OSDN Git Service

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