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