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