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