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, 2004 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 #include "regset.h" 36 #include "solib-svr4.h" 37 #include "ppc-tdep.h" 38 #include "trad-frame.h" 39 #include "frame-unwind.h" 40 41 /* The following instructions are used in the signal trampoline code 42 on GNU/Linux PPC. The kernel used to use magic syscalls 0x6666 and 43 0x7777 but now uses the sigreturn syscalls. We check for both. */ 44 #define INSTR_LI_R0_0x6666 0x38006666 45 #define INSTR_LI_R0_0x7777 0x38007777 46 #define INSTR_LI_R0_NR_sigreturn 0x38000077 47 #define INSTR_LI_R0_NR_rt_sigreturn 0x380000AC 48 49 #define INSTR_SC 0x44000002 50 51 /* Since the *-tdep.c files are platform independent (i.e, they may be 52 used to build cross platform debuggers), we can't include system 53 headers. Therefore, details concerning the sigcontext structure 54 must be painstakingly rerecorded. What's worse, if these details 55 ever change in the header files, they'll have to be changed here 56 as well. */ 57 58 /* __SIGNAL_FRAMESIZE from <asm/ptrace.h> */ 59 #define PPC_LINUX_SIGNAL_FRAMESIZE 64 60 61 /* From <asm/sigcontext.h>, offsetof(struct sigcontext_struct, regs) == 0x1c */ 62 #define PPC_LINUX_REGS_PTR_OFFSET (PPC_LINUX_SIGNAL_FRAMESIZE + 0x1c) 63 64 /* From <asm/sigcontext.h>, 65 offsetof(struct sigcontext_struct, handler) == 0x14 */ 66 #define PPC_LINUX_HANDLER_PTR_OFFSET (PPC_LINUX_SIGNAL_FRAMESIZE + 0x14) 67 68 /* From <asm/ptrace.h>, values for PT_NIP, PT_R1, and PT_LNK */ 69 #define PPC_LINUX_PT_R0 0 70 #define PPC_LINUX_PT_R1 1 71 #define PPC_LINUX_PT_R2 2 72 #define PPC_LINUX_PT_R3 3 73 #define PPC_LINUX_PT_R4 4 74 #define PPC_LINUX_PT_R5 5 75 #define PPC_LINUX_PT_R6 6 76 #define PPC_LINUX_PT_R7 7 77 #define PPC_LINUX_PT_R8 8 78 #define PPC_LINUX_PT_R9 9 79 #define PPC_LINUX_PT_R10 10 80 #define PPC_LINUX_PT_R11 11 81 #define PPC_LINUX_PT_R12 12 82 #define PPC_LINUX_PT_R13 13 83 #define PPC_LINUX_PT_R14 14 84 #define PPC_LINUX_PT_R15 15 85 #define PPC_LINUX_PT_R16 16 86 #define PPC_LINUX_PT_R17 17 87 #define PPC_LINUX_PT_R18 18 88 #define PPC_LINUX_PT_R19 19 89 #define PPC_LINUX_PT_R20 20 90 #define PPC_LINUX_PT_R21 21 91 #define PPC_LINUX_PT_R22 22 92 #define PPC_LINUX_PT_R23 23 93 #define PPC_LINUX_PT_R24 24 94 #define PPC_LINUX_PT_R25 25 95 #define PPC_LINUX_PT_R26 26 96 #define PPC_LINUX_PT_R27 27 97 #define PPC_LINUX_PT_R28 28 98 #define PPC_LINUX_PT_R29 29 99 #define PPC_LINUX_PT_R30 30 100 #define PPC_LINUX_PT_R31 31 101 #define PPC_LINUX_PT_NIP 32 102 #define PPC_LINUX_PT_MSR 33 103 #define PPC_LINUX_PT_CTR 35 104 #define PPC_LINUX_PT_LNK 36 105 #define PPC_LINUX_PT_XER 37 106 #define PPC_LINUX_PT_CCR 38 107 #define PPC_LINUX_PT_MQ 39 108 #define PPC_LINUX_PT_FPR0 48 /* each FP reg occupies 2 slots in this space */ 109 #define PPC_LINUX_PT_FPR31 (PPC_LINUX_PT_FPR0 + 2*31) 110 #define PPC_LINUX_PT_FPSCR (PPC_LINUX_PT_FPR0 + 2*32 + 1) 111 112 static int ppc_linux_at_sigtramp_return_path (CORE_ADDR pc); 113 114 /* Determine if pc is in a signal trampoline... 115 116 Ha! That's not what this does at all. wait_for_inferior in 117 infrun.c calls get_frame_type() in order to detect entry into a 118 signal trampoline just after delivery of a signal. But on 119 GNU/Linux, signal trampolines are used for the return path only. 120 The kernel sets things up so that the signal handler is called 121 directly. 122 123 If we use in_sigtramp2() in place of in_sigtramp() (see below) 124 we'll (often) end up with stop_pc in the trampoline and prev_pc in 125 the (now exited) handler. The code there will cause a temporary 126 breakpoint to be set on prev_pc which is not very likely to get hit 127 again. 128 129 If this is confusing, think of it this way... the code in 130 wait_for_inferior() needs to be able to detect entry into a signal 131 trampoline just after a signal is delivered, not after the handler 132 has been run. 133 134 So, we define in_sigtramp() below to return 1 if the following is 135 true: 136 137 1) The previous frame is a real signal trampoline. 138 139 - and - 140 141 2) pc is at the first or second instruction of the corresponding 142 handler. 143 144 Why the second instruction? It seems that wait_for_inferior() 145 never sees the first instruction when single stepping. When a 146 signal is delivered while stepping, the next instruction that 147 would've been stepped over isn't, instead a signal is delivered and 148 the first instruction of the handler is stepped over instead. That 149 puts us on the second instruction. (I added the test for the first 150 instruction long after the fact, just in case the observed behavior 151 is ever fixed.) */ 152 153 int 154 ppc_linux_in_sigtramp (CORE_ADDR pc, char *func_name) 155 { 156 CORE_ADDR lr; 157 CORE_ADDR sp; 158 CORE_ADDR tramp_sp; 159 char buf[4]; 160 CORE_ADDR handler; 161 162 lr = read_register (gdbarch_tdep (current_gdbarch)->ppc_lr_regnum); 163 if (!ppc_linux_at_sigtramp_return_path (lr)) 164 return 0; 165 166 sp = read_register (SP_REGNUM); 167 168 if (target_read_memory (sp, buf, sizeof (buf)) != 0) 169 return 0; 170 171 tramp_sp = extract_unsigned_integer (buf, 4); 172 173 if (target_read_memory (tramp_sp + PPC_LINUX_HANDLER_PTR_OFFSET, buf, 174 sizeof (buf)) != 0) 175 return 0; 176 177 handler = extract_unsigned_integer (buf, 4); 178 179 return (pc == handler || pc == handler + 4); 180 } 181 182 static int 183 insn_is_sigreturn (unsigned long pcinsn) 184 { 185 switch(pcinsn) 186 { 187 case INSTR_LI_R0_0x6666: 188 case INSTR_LI_R0_0x7777: 189 case INSTR_LI_R0_NR_sigreturn: 190 case INSTR_LI_R0_NR_rt_sigreturn: 191 return 1; 192 default: 193 return 0; 194 } 195 } 196 197 /* 198 * The signal handler trampoline is on the stack and consists of exactly 199 * two instructions. The easiest and most accurate way of determining 200 * whether the pc is in one of these trampolines is by inspecting the 201 * instructions. It'd be faster though if we could find a way to do this 202 * via some simple address comparisons. 203 */ 204 static int 205 ppc_linux_at_sigtramp_return_path (CORE_ADDR pc) 206 { 207 char buf[12]; 208 unsigned long pcinsn; 209 if (target_read_memory (pc - 4, buf, sizeof (buf)) != 0) 210 return 0; 211 212 /* extract the instruction at the pc */ 213 pcinsn = extract_unsigned_integer (buf + 4, 4); 214 215 return ( 216 (insn_is_sigreturn (pcinsn) 217 && extract_unsigned_integer (buf + 8, 4) == INSTR_SC) 218 || 219 (pcinsn == INSTR_SC 220 && insn_is_sigreturn (extract_unsigned_integer (buf, 4)))); 221 } 222 223 static CORE_ADDR 224 ppc_linux_skip_trampoline_code (CORE_ADDR pc) 225 { 226 char buf[4]; 227 struct obj_section *sect; 228 struct objfile *objfile; 229 unsigned long insn; 230 CORE_ADDR plt_start = 0; 231 CORE_ADDR symtab = 0; 232 CORE_ADDR strtab = 0; 233 int num_slots = -1; 234 int reloc_index = -1; 235 CORE_ADDR plt_table; 236 CORE_ADDR reloc; 237 CORE_ADDR sym; 238 long symidx; 239 char symname[1024]; 240 struct minimal_symbol *msymbol; 241 242 /* Find the section pc is in; return if not in .plt */ 243 sect = find_pc_section (pc); 244 if (!sect || strcmp (sect->the_bfd_section->name, ".plt") != 0) 245 return 0; 246 247 objfile = sect->objfile; 248 249 /* Pick up the instruction at pc. It had better be of the 250 form 251 li r11, IDX 252 253 where IDX is an index into the plt_table. */ 254 255 if (target_read_memory (pc, buf, 4) != 0) 256 return 0; 257 insn = extract_unsigned_integer (buf, 4); 258 259 if ((insn & 0xffff0000) != 0x39600000 /* li r11, VAL */ ) 260 return 0; 261 262 reloc_index = (insn << 16) >> 16; 263 264 /* Find the objfile that pc is in and obtain the information 265 necessary for finding the symbol name. */ 266 for (sect = objfile->sections; sect < objfile->sections_end; ++sect) 267 { 268 const char *secname = sect->the_bfd_section->name; 269 if (strcmp (secname, ".plt") == 0) 270 plt_start = sect->addr; 271 else if (strcmp (secname, ".rela.plt") == 0) 272 num_slots = ((int) sect->endaddr - (int) sect->addr) / 12; 273 else if (strcmp (secname, ".dynsym") == 0) 274 symtab = sect->addr; 275 else if (strcmp (secname, ".dynstr") == 0) 276 strtab = sect->addr; 277 } 278 279 /* Make sure we have all the information we need. */ 280 if (plt_start == 0 || num_slots == -1 || symtab == 0 || strtab == 0) 281 return 0; 282 283 /* Compute the value of the plt table */ 284 plt_table = plt_start + 72 + 8 * num_slots; 285 286 /* Get address of the relocation entry (Elf32_Rela) */ 287 if (target_read_memory (plt_table + reloc_index, buf, 4) != 0) 288 return 0; 289 reloc = extract_unsigned_integer (buf, 4); 290 291 sect = find_pc_section (reloc); 292 if (!sect) 293 return 0; 294 295 if (strcmp (sect->the_bfd_section->name, ".text") == 0) 296 return reloc; 297 298 /* Now get the r_info field which is the relocation type and symbol 299 index. */ 300 if (target_read_memory (reloc + 4, buf, 4) != 0) 301 return 0; 302 symidx = extract_unsigned_integer (buf, 4); 303 304 /* Shift out the relocation type leaving just the symbol index */ 305 /* symidx = ELF32_R_SYM(symidx); */ 306 symidx = symidx >> 8; 307 308 /* compute the address of the symbol */ 309 sym = symtab + symidx * 4; 310 311 /* Fetch the string table index */ 312 if (target_read_memory (sym, buf, 4) != 0) 313 return 0; 314 symidx = extract_unsigned_integer (buf, 4); 315 316 /* Fetch the string; we don't know how long it is. Is it possible 317 that the following will fail because we're trying to fetch too 318 much? */ 319 if (target_read_memory (strtab + symidx, symname, sizeof (symname)) != 0) 320 return 0; 321 322 /* This might not work right if we have multiple symbols with the 323 same name; the only way to really get it right is to perform 324 the same sort of lookup as the dynamic linker. */ 325 msymbol = lookup_minimal_symbol_text (symname, NULL); 326 if (!msymbol) 327 return 0; 328 329 return SYMBOL_VALUE_ADDRESS (msymbol); 330 } 331 332 /* ppc_linux_memory_remove_breakpoints attempts to remove a breakpoint 333 in much the same fashion as memory_remove_breakpoint in mem-break.c, 334 but is careful not to write back the previous contents if the code 335 in question has changed in between inserting the breakpoint and 336 removing it. 337 338 Here is the problem that we're trying to solve... 339 340 Once upon a time, before introducing this function to remove 341 breakpoints from the inferior, setting a breakpoint on a shared 342 library function prior to running the program would not work 343 properly. In order to understand the problem, it is first 344 necessary to understand a little bit about dynamic linking on 345 this platform. 346 347 A call to a shared library function is accomplished via a bl 348 (branch-and-link) instruction whose branch target is an entry 349 in the procedure linkage table (PLT). The PLT in the object 350 file is uninitialized. To gdb, prior to running the program, the 351 entries in the PLT are all zeros. 352 353 Once the program starts running, the shared libraries are loaded 354 and the procedure linkage table is initialized, but the entries in 355 the table are not (necessarily) resolved. Once a function is 356 actually called, the code in the PLT is hit and the function is 357 resolved. In order to better illustrate this, an example is in 358 order; the following example is from the gdb testsuite. 359 360 We start the program shmain. 361 362 [kev@arroyo testsuite]$ ../gdb gdb.base/shmain 363 [...] 364 365 We place two breakpoints, one on shr1 and the other on main. 366 367 (gdb) b shr1 368 Breakpoint 1 at 0x100409d4 369 (gdb) b main 370 Breakpoint 2 at 0x100006a0: file gdb.base/shmain.c, line 44. 371 372 Examine the instruction (and the immediatly following instruction) 373 upon which the breakpoint was placed. Note that the PLT entry 374 for shr1 contains zeros. 375 376 (gdb) x/2i 0x100409d4 377 0x100409d4 <shr1>: .long 0x0 378 0x100409d8 <shr1+4>: .long 0x0 379 380 Now run 'til main. 381 382 (gdb) r 383 Starting program: gdb.base/shmain 384 Breakpoint 1 at 0xffaf790: file gdb.base/shr1.c, line 19. 385 386 Breakpoint 2, main () 387 at gdb.base/shmain.c:44 388 44 g = 1; 389 390 Examine the PLT again. Note that the loading of the shared 391 library has initialized the PLT to code which loads a constant 392 (which I think is an index into the GOT) into r11 and then 393 branchs a short distance to the code which actually does the 394 resolving. 395 396 (gdb) x/2i 0x100409d4 397 0x100409d4 <shr1>: li r11,4 398 0x100409d8 <shr1+4>: b 0x10040984 <sg+4> 399 (gdb) c 400 Continuing. 401 402 Breakpoint 1, shr1 (x=1) 403 at gdb.base/shr1.c:19 404 19 l = 1; 405 406 Now we've hit the breakpoint at shr1. (The breakpoint was 407 reset from the PLT entry to the actual shr1 function after the 408 shared library was loaded.) Note that the PLT entry has been 409 resolved to contain a branch that takes us directly to shr1. 410 (The real one, not the PLT entry.) 411 412 (gdb) x/2i 0x100409d4 413 0x100409d4 <shr1>: b 0xffaf76c <shr1> 414 0x100409d8 <shr1+4>: b 0x10040984 <sg+4> 415 416 The thing to note here is that the PLT entry for shr1 has been 417 changed twice. 418 419 Now the problem should be obvious. GDB places a breakpoint (a 420 trap instruction) on the zero value of the PLT entry for shr1. 421 Later on, after the shared library had been loaded and the PLT 422 initialized, GDB gets a signal indicating this fact and attempts 423 (as it always does when it stops) to remove all the breakpoints. 424 425 The breakpoint removal was causing the former contents (a zero 426 word) to be written back to the now initialized PLT entry thus 427 destroying a portion of the initialization that had occurred only a 428 short time ago. When execution continued, the zero word would be 429 executed as an instruction an an illegal instruction trap was 430 generated instead. (0 is not a legal instruction.) 431 432 The fix for this problem was fairly straightforward. The function 433 memory_remove_breakpoint from mem-break.c was copied to this file, 434 modified slightly, and renamed to ppc_linux_memory_remove_breakpoint. 435 In tm-linux.h, MEMORY_REMOVE_BREAKPOINT is defined to call this new 436 function. 437 438 The differences between ppc_linux_memory_remove_breakpoint () and 439 memory_remove_breakpoint () are minor. All that the former does 440 that the latter does not is check to make sure that the breakpoint 441 location actually contains a breakpoint (trap instruction) prior 442 to attempting to write back the old contents. If it does contain 443 a trap instruction, we allow the old contents to be written back. 444 Otherwise, we silently do nothing. 445 446 The big question is whether memory_remove_breakpoint () should be 447 changed to have the same functionality. The downside is that more 448 traffic is generated for remote targets since we'll have an extra 449 fetch of a memory word each time a breakpoint is removed. 450 451 For the time being, we'll leave this self-modifying-code-friendly 452 version in ppc-linux-tdep.c, but it ought to be migrated somewhere 453 else in the event that some other platform has similar needs with 454 regard to removing breakpoints in some potentially self modifying 455 code. */ 456 int 457 ppc_linux_memory_remove_breakpoint (CORE_ADDR addr, char *contents_cache) 458 { 459 const unsigned char *bp; 460 int val; 461 int bplen; 462 char old_contents[BREAKPOINT_MAX]; 463 464 /* Determine appropriate breakpoint contents and size for this address. */ 465 bp = BREAKPOINT_FROM_PC (&addr, &bplen); 466 if (bp == NULL) 467 error ("Software breakpoints not implemented for this target."); 468 469 val = target_read_memory (addr, old_contents, bplen); 470 471 /* If our breakpoint is no longer at the address, this means that the 472 program modified the code on us, so it is wrong to put back the 473 old value */ 474 if (val == 0 && memcmp (bp, old_contents, bplen) == 0) 475 val = target_write_memory (addr, contents_cache, bplen); 476 477 return val; 478 } 479 480 /* For historic reasons, PPC 32 GNU/Linux follows PowerOpen rather 481 than the 32 bit SYSV R4 ABI structure return convention - all 482 structures, no matter their size, are put in memory. Vectors, 483 which were added later, do get returned in a register though. */ 484 485 static enum return_value_convention 486 ppc_linux_return_value (struct gdbarch *gdbarch, struct type *valtype, 487 struct regcache *regcache, void *readbuf, 488 const void *writebuf) 489 { 490 if ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT 491 || TYPE_CODE (valtype) == TYPE_CODE_UNION) 492 && !((TYPE_LENGTH (valtype) == 16 || TYPE_LENGTH (valtype) == 8) 493 && TYPE_VECTOR (valtype))) 494 return RETURN_VALUE_STRUCT_CONVENTION; 495 else 496 return ppc_sysv_abi_return_value (gdbarch, valtype, regcache, readbuf, 497 writebuf); 498 } 499 500 /* Fetch (and possibly build) an appropriate link_map_offsets 501 structure for GNU/Linux PPC targets using the struct offsets 502 defined in link.h (but without actual reference to that file). 503 504 This makes it possible to access GNU/Linux PPC shared libraries 505 from a GDB that was not built on an GNU/Linux PPC host (for cross 506 debugging). */ 507 508 struct link_map_offsets * 509 ppc_linux_svr4_fetch_link_map_offsets (void) 510 { 511 static struct link_map_offsets lmo; 512 static struct link_map_offsets *lmp = NULL; 513 514 if (lmp == NULL) 515 { 516 lmp = &lmo; 517 518 lmo.r_debug_size = 8; /* The actual size is 20 bytes, but 519 this is all we need. */ 520 lmo.r_map_offset = 4; 521 lmo.r_map_size = 4; 522 523 lmo.link_map_size = 20; /* The actual size is 560 bytes, but 524 this is all we need. */ 525 lmo.l_addr_offset = 0; 526 lmo.l_addr_size = 4; 527 528 lmo.l_name_offset = 4; 529 lmo.l_name_size = 4; 530 531 lmo.l_next_offset = 12; 532 lmo.l_next_size = 4; 533 534 lmo.l_prev_offset = 16; 535 lmo.l_prev_size = 4; 536 } 537 538 return lmp; 539 } 540 541 542 /* Macros for matching instructions. Note that, since all the 543 operands are masked off before they're or-ed into the instruction, 544 you can use -1 to make masks. */ 545 546 #define insn_d(opcd, rts, ra, d) \ 547 ((((opcd) & 0x3f) << 26) \ 548 | (((rts) & 0x1f) << 21) \ 549 | (((ra) & 0x1f) << 16) \ 550 | ((d) & 0xffff)) 551 552 #define insn_ds(opcd, rts, ra, d, xo) \ 553 ((((opcd) & 0x3f) << 26) \ 554 | (((rts) & 0x1f) << 21) \ 555 | (((ra) & 0x1f) << 16) \ 556 | ((d) & 0xfffc) \ 557 | ((xo) & 0x3)) 558 559 #define insn_xfx(opcd, rts, spr, xo) \ 560 ((((opcd) & 0x3f) << 26) \ 561 | (((rts) & 0x1f) << 21) \ 562 | (((spr) & 0x1f) << 16) \ 563 | (((spr) & 0x3e0) << 6) \ 564 | (((xo) & 0x3ff) << 1)) 565 566 /* Read a PPC instruction from memory. PPC instructions are always 567 big-endian, no matter what endianness the program is running in, so 568 we can't use read_memory_integer or one of its friends here. */ 569 static unsigned int 570 read_insn (CORE_ADDR pc) 571 { 572 unsigned char buf[4]; 573 574 read_memory (pc, buf, 4); 575 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; 576 } 577 578 579 /* An instruction to match. */ 580 struct insn_pattern 581 { 582 unsigned int mask; /* mask the insn with this... */ 583 unsigned int data; /* ...and see if it matches this. */ 584 int optional; /* If non-zero, this insn may be absent. */ 585 }; 586 587 /* Return non-zero if the instructions at PC match the series 588 described in PATTERN, or zero otherwise. PATTERN is an array of 589 'struct insn_pattern' objects, terminated by an entry whose mask is 590 zero. 591 592 When the match is successful, fill INSN[i] with what PATTERN[i] 593 matched. If PATTERN[i] is optional, and the instruction wasn't 594 present, set INSN[i] to 0 (which is not a valid PPC instruction). 595 INSN should have as many elements as PATTERN. Note that, if 596 PATTERN contains optional instructions which aren't present in 597 memory, then INSN will have holes, so INSN[i] isn't necessarily the 598 i'th instruction in memory. */ 599 static int 600 insns_match_pattern (CORE_ADDR pc, 601 struct insn_pattern *pattern, 602 unsigned int *insn) 603 { 604 int i; 605 606 for (i = 0; pattern[i].mask; i++) 607 { 608 insn[i] = read_insn (pc); 609 if ((insn[i] & pattern[i].mask) == pattern[i].data) 610 pc += 4; 611 else if (pattern[i].optional) 612 insn[i] = 0; 613 else 614 return 0; 615 } 616 617 return 1; 618 } 619 620 621 /* Return the 'd' field of the d-form instruction INSN, properly 622 sign-extended. */ 623 static CORE_ADDR 624 insn_d_field (unsigned int insn) 625 { 626 return ((((CORE_ADDR) insn & 0xffff) ^ 0x8000) - 0x8000); 627 } 628 629 630 /* Return the 'ds' field of the ds-form instruction INSN, with the two 631 zero bits concatenated at the right, and properly 632 sign-extended. */ 633 static CORE_ADDR 634 insn_ds_field (unsigned int insn) 635 { 636 return ((((CORE_ADDR) insn & 0xfffc) ^ 0x8000) - 0x8000); 637 } 638 639 640 /* If DESC is the address of a 64-bit PowerPC GNU/Linux function 641 descriptor, return the descriptor's entry point. */ 642 static CORE_ADDR 643 ppc64_desc_entry_point (CORE_ADDR desc) 644 { 645 /* The first word of the descriptor is the entry point. */ 646 return (CORE_ADDR) read_memory_unsigned_integer (desc, 8); 647 } 648 649 650 /* Pattern for the standard linkage function. These are built by 651 build_plt_stub in elf64-ppc.c, whose GLINK argument is always 652 zero. */ 653 static struct insn_pattern ppc64_standard_linkage[] = 654 { 655 /* addis r12, r2, <any> */ 656 { insn_d (-1, -1, -1, 0), insn_d (15, 12, 2, 0), 0 }, 657 658 /* std r2, 40(r1) */ 659 { -1, insn_ds (62, 2, 1, 40, 0), 0 }, 660 661 /* ld r11, <any>(r12) */ 662 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 }, 663 664 /* addis r12, r12, 1 <optional> */ 665 { insn_d (-1, -1, -1, -1), insn_d (15, 12, 2, 1), 1 }, 666 667 /* ld r2, <any>(r12) */ 668 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 2, 12, 0, 0), 0 }, 669 670 /* addis r12, r12, 1 <optional> */ 671 { insn_d (-1, -1, -1, -1), insn_d (15, 12, 2, 1), 1 }, 672 673 /* mtctr r11 */ 674 { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 11, 9, 467), 675 0 }, 676 677 /* ld r11, <any>(r12) */ 678 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 }, 679 680 /* bctr */ 681 { -1, 0x4e800420, 0 }, 682 683 { 0, 0, 0 } 684 }; 685 #define PPC64_STANDARD_LINKAGE_LEN \ 686 (sizeof (ppc64_standard_linkage) / sizeof (ppc64_standard_linkage[0])) 687 688 689 /* Recognize a 64-bit PowerPC GNU/Linux linkage function --- what GDB 690 calls a "solib trampoline". */ 691 static int 692 ppc64_in_solib_call_trampoline (CORE_ADDR pc, char *name) 693 { 694 /* Detecting solib call trampolines on PPC64 GNU/Linux is a pain. 695 696 It's not specifically solib call trampolines that are the issue. 697 Any call from one function to another function that uses a 698 different TOC requires a trampoline, to save the caller's TOC 699 pointer and then load the callee's TOC. An executable or shared 700 library may have more than one TOC, so even intra-object calls 701 may require a trampoline. Since executable and shared libraries 702 will all have their own distinct TOCs, every inter-object call is 703 also an inter-TOC call, and requires a trampoline --- so "solib 704 call trampolines" are just a special case. 705 706 The 64-bit PowerPC GNU/Linux ABI calls these call trampolines 707 "linkage functions". Since they need to be near the functions 708 that call them, they all appear in .text, not in any special 709 section. The .plt section just contains an array of function 710 descriptors, from which the linkage functions load the callee's 711 entry point, TOC value, and environment pointer. So 712 in_plt_section is useless. The linkage functions don't have any 713 special linker symbols to name them, either. 714 715 The only way I can see to recognize them is to actually look at 716 their code. They're generated by ppc_build_one_stub and some 717 other functions in bfd/elf64-ppc.c, so that should show us all 718 the instruction sequences we need to recognize. */ 719 unsigned int insn[PPC64_STANDARD_LINKAGE_LEN]; 720 721 return insns_match_pattern (pc, ppc64_standard_linkage, insn); 722 } 723 724 725 /* When the dynamic linker is doing lazy symbol resolution, the first 726 call to a function in another object will go like this: 727 728 - The user's function calls the linkage function: 729 730 100007c4: 4b ff fc d5 bl 10000498 731 100007c8: e8 41 00 28 ld r2,40(r1) 732 733 - The linkage function loads the entry point (and other stuff) from 734 the function descriptor in the PLT, and jumps to it: 735 736 10000498: 3d 82 00 00 addis r12,r2,0 737 1000049c: f8 41 00 28 std r2,40(r1) 738 100004a0: e9 6c 80 98 ld r11,-32616(r12) 739 100004a4: e8 4c 80 a0 ld r2,-32608(r12) 740 100004a8: 7d 69 03 a6 mtctr r11 741 100004ac: e9 6c 80 a8 ld r11,-32600(r12) 742 100004b0: 4e 80 04 20 bctr 743 744 - But since this is the first time that PLT entry has been used, it 745 sends control to its glink entry. That loads the number of the 746 PLT entry and jumps to the common glink0 code: 747 748 10000c98: 38 00 00 00 li r0,0 749 10000c9c: 4b ff ff dc b 10000c78 750 751 - The common glink0 code then transfers control to the dynamic 752 linker's fixup code: 753 754 10000c78: e8 41 00 28 ld r2,40(r1) 755 10000c7c: 3d 82 00 00 addis r12,r2,0 756 10000c80: e9 6c 80 80 ld r11,-32640(r12) 757 10000c84: e8 4c 80 88 ld r2,-32632(r12) 758 10000c88: 7d 69 03 a6 mtctr r11 759 10000c8c: e9 6c 80 90 ld r11,-32624(r12) 760 10000c90: 4e 80 04 20 bctr 761 762 Eventually, this code will figure out how to skip all of this, 763 including the dynamic linker. At the moment, we just get through 764 the linkage function. */ 765 766 /* If the current thread is about to execute a series of instructions 767 at PC matching the ppc64_standard_linkage pattern, and INSN is the result 768 from that pattern match, return the code address to which the 769 standard linkage function will send them. (This doesn't deal with 770 dynamic linker lazy symbol resolution stubs.) */ 771 static CORE_ADDR 772 ppc64_standard_linkage_target (CORE_ADDR pc, unsigned int *insn) 773 { 774 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch); 775 776 /* The address of the function descriptor this linkage function 777 references. */ 778 CORE_ADDR desc 779 = ((CORE_ADDR) read_register (tdep->ppc_gp0_regnum + 2) 780 + (insn_d_field (insn[0]) << 16) 781 + insn_ds_field (insn[2])); 782 783 /* The first word of the descriptor is the entry point. Return that. */ 784 return ppc64_desc_entry_point (desc); 785 } 786 787 788 /* Given that we've begun executing a call trampoline at PC, return 789 the entry point of the function the trampoline will go to. */ 790 static CORE_ADDR 791 ppc64_skip_trampoline_code (CORE_ADDR pc) 792 { 793 unsigned int ppc64_standard_linkage_insn[PPC64_STANDARD_LINKAGE_LEN]; 794 795 if (insns_match_pattern (pc, ppc64_standard_linkage, 796 ppc64_standard_linkage_insn)) 797 return ppc64_standard_linkage_target (pc, ppc64_standard_linkage_insn); 798 else 799 return 0; 800 } 801 802 803 /* Support for CONVERT_FROM_FUNC_PTR_ADDR (ARCH, ADDR, TARG) on PPC64 804 GNU/Linux. 805 806 Usually a function pointer's representation is simply the address 807 of the function. On GNU/Linux on the 64-bit PowerPC however, a 808 function pointer is represented by a pointer to a TOC entry. This 809 TOC entry contains three words, the first word is the address of 810 the function, the second word is the TOC pointer (r2), and the 811 third word is the static chain value. Throughout GDB it is 812 currently assumed that a function pointer contains the address of 813 the function, which is not easy to fix. In addition, the 814 conversion of a function address to a function pointer would 815 require allocation of a TOC entry in the inferior's memory space, 816 with all its drawbacks. To be able to call C++ virtual methods in 817 the inferior (which are called via function pointers), 818 find_function_addr uses this function to get the function address 819 from a function pointer. */ 820 821 /* If ADDR points at what is clearly a function descriptor, transform 822 it into the address of the corresponding function. Be 823 conservative, otherwize GDB will do the transformation on any 824 random addresses such as occures when there is no symbol table. */ 825 826 static CORE_ADDR 827 ppc64_linux_convert_from_func_ptr_addr (struct gdbarch *gdbarch, 828 CORE_ADDR addr, 829 struct target_ops *targ) 830 { 831 struct section_table *s = target_section_by_addr (targ, addr); 832 833 /* Check if ADDR points to a function descriptor. */ 834 if (s && strcmp (s->the_bfd_section->name, ".opd") == 0) 835 return get_target_memory_unsigned (targ, addr, 8); 836 837 return addr; 838 } 839 840 static void 841 right_supply_register (struct regcache *regcache, int wordsize, int regnum, 842 const bfd_byte *buf) 843 { 844 regcache_raw_supply (regcache, regnum, 845 (buf + wordsize - register_size (current_gdbarch, regnum))); 846 } 847 848 /* Extract the register values found in the WORDSIZED ABI GREGSET, 849 storing their values in REGCACHE. Note that some are left-aligned, 850 while others are right aligned. */ 851 852 void 853 ppc_linux_supply_gregset (struct regcache *regcache, 854 int regnum, const void *gregs, size_t size, 855 int wordsize) 856 { 857 int regi; 858 struct gdbarch *regcache_arch = get_regcache_arch (regcache); 859 struct gdbarch_tdep *regcache_tdep = gdbarch_tdep (regcache_arch); 860 const bfd_byte *buf = gregs; 861 862 for (regi = 0; regi < ppc_num_gprs; regi++) 863 right_supply_register (regcache, wordsize, 864 regcache_tdep->ppc_gp0_regnum + regi, 865 buf + wordsize * regi); 866 867 right_supply_register (regcache, wordsize, gdbarch_pc_regnum (regcache_arch), 868 buf + wordsize * PPC_LINUX_PT_NIP); 869 right_supply_register (regcache, wordsize, regcache_tdep->ppc_lr_regnum, 870 buf + wordsize * PPC_LINUX_PT_LNK); 871 regcache_raw_supply (regcache, regcache_tdep->ppc_cr_regnum, 872 buf + wordsize * PPC_LINUX_PT_CCR); 873 regcache_raw_supply (regcache, regcache_tdep->ppc_xer_regnum, 874 buf + wordsize * PPC_LINUX_PT_XER); 875 regcache_raw_supply (regcache, regcache_tdep->ppc_ctr_regnum, 876 buf + wordsize * PPC_LINUX_PT_CTR); 877 if (regcache_tdep->ppc_mq_regnum != -1) 878 right_supply_register (regcache, wordsize, regcache_tdep->ppc_mq_regnum, 879 buf + wordsize * PPC_LINUX_PT_MQ); 880 right_supply_register (regcache, wordsize, regcache_tdep->ppc_ps_regnum, 881 buf + wordsize * PPC_LINUX_PT_MSR); 882 } 883 884 static void 885 ppc32_linux_supply_gregset (const struct regset *regset, 886 struct regcache *regcache, 887 int regnum, const void *gregs, size_t size) 888 { 889 ppc_linux_supply_gregset (regcache, regnum, gregs, size, 4); 890 } 891 892 static struct regset ppc32_linux_gregset = { 893 NULL, ppc32_linux_supply_gregset 894 }; 895 896 struct ppc_linux_sigtramp_cache 897 { 898 CORE_ADDR base; 899 struct trad_frame_saved_reg *saved_regs; 900 }; 901 902 static struct ppc_linux_sigtramp_cache * 903 ppc_linux_sigtramp_cache (struct frame_info *next_frame, void **this_cache) 904 { 905 CORE_ADDR regs; 906 CORE_ADDR gpregs; 907 CORE_ADDR fpregs; 908 int i; 909 struct ppc_linux_sigtramp_cache *cache; 910 struct gdbarch *gdbarch = get_frame_arch (next_frame); 911 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); 912 913 if ((*this_cache) != NULL) 914 return (*this_cache); 915 cache = FRAME_OBSTACK_ZALLOC (struct ppc_linux_sigtramp_cache); 916 (*this_cache) = cache; 917 cache->saved_regs = trad_frame_alloc_saved_regs (next_frame); 918 919 cache->base = frame_unwind_register_unsigned (next_frame, SP_REGNUM); 920 921 /* Find the register pointer, which gives the address of the 922 register buffers. */ 923 if (tdep->wordsize == 4) 924 regs = (cache->base 925 + 0xd0 /* Offset to ucontext_t. */ 926 + 0x30 /* Offset to .reg. */); 927 else 928 regs = (cache->base 929 + 0x80 /* Offset to ucontext_t. */ 930 + 0xe0 /* Offset to .reg. */); 931 /* And the corresponding register buffers. */ 932 gpregs = read_memory_unsigned_integer (regs, tdep->wordsize); 933 fpregs = gpregs + 48 * tdep->wordsize; 934 935 /* General purpose. */ 936 for (i = 0; i < ppc_num_gprs; i++) 937 { 938 int regnum = i + tdep->ppc_gp0_regnum; 939 cache->saved_regs[regnum].addr = gpregs + i * tdep->wordsize; 940 } 941 cache->saved_regs[PC_REGNUM].addr = gpregs + 32 * tdep->wordsize; 942 cache->saved_regs[tdep->ppc_ctr_regnum].addr = gpregs + 35 * tdep->wordsize; 943 cache->saved_regs[tdep->ppc_lr_regnum].addr = gpregs + 36 * tdep->wordsize; 944 cache->saved_regs[tdep->ppc_xer_regnum].addr = gpregs + 37 * tdep->wordsize; 945 cache->saved_regs[tdep->ppc_cr_regnum].addr = gpregs + 38 * tdep->wordsize; 946 947 /* Floating point registers. */ 948 if (ppc_floating_point_unit_p (gdbarch)) 949 { 950 for (i = 0; i < ppc_num_fprs; i++) 951 { 952 int regnum = i + tdep->ppc_fp0_regnum; 953 cache->saved_regs[regnum].addr = fpregs + i * tdep->wordsize; 954 } 955 cache->saved_regs[tdep->ppc_fpscr_regnum].addr 956 = fpregs + 32 * tdep->wordsize; 957 } 958 959 return cache; 960 } 961 962 static void 963 ppc_linux_sigtramp_this_id (struct frame_info *next_frame, void **this_cache, 964 struct frame_id *this_id) 965 { 966 struct ppc_linux_sigtramp_cache *info 967 = ppc_linux_sigtramp_cache (next_frame, this_cache); 968 (*this_id) = frame_id_build (info->base, frame_pc_unwind (next_frame)); 969 } 970 971 static void 972 ppc_linux_sigtramp_prev_register (struct frame_info *next_frame, 973 void **this_cache, 974 int regnum, int *optimizedp, 975 enum lval_type *lvalp, CORE_ADDR *addrp, 976 int *realnump, void *valuep) 977 { 978 struct ppc_linux_sigtramp_cache *info 979 = ppc_linux_sigtramp_cache (next_frame, this_cache); 980 trad_frame_get_prev_register (next_frame, info->saved_regs, regnum, 981 optimizedp, lvalp, addrp, realnump, valuep); 982 } 983 984 static const struct frame_unwind ppc_linux_sigtramp_unwind = 985 { 986 SIGTRAMP_FRAME, 987 ppc_linux_sigtramp_this_id, 988 ppc_linux_sigtramp_prev_register 989 }; 990 991 static const struct frame_unwind * 992 ppc_linux_sigtramp_sniffer (struct frame_info *next_frame) 993 { 994 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (next_frame)); 995 if (frame_pc_unwind (next_frame) 996 > frame_unwind_register_unsigned (next_frame, SP_REGNUM)) 997 /* Assume anything that is vaguely on the stack is a signal 998 trampoline. */ 999 return &ppc_linux_sigtramp_unwind; 1000 else 1001 return NULL; 1002 } 1003 1004 static void 1005 ppc64_linux_supply_gregset (const struct regset *regset, 1006 struct regcache * regcache, 1007 int regnum, const void *gregs, size_t size) 1008 { 1009 ppc_linux_supply_gregset (regcache, regnum, gregs, size, 8); 1010 } 1011 1012 static struct regset ppc64_linux_gregset = { 1013 NULL, ppc64_linux_supply_gregset 1014 }; 1015 1016 void 1017 ppc_linux_supply_fpregset (const struct regset *regset, 1018 struct regcache * regcache, 1019 int regnum, const void *fpset, size_t size) 1020 { 1021 int regi; 1022 struct gdbarch *regcache_arch = get_regcache_arch (regcache); 1023 struct gdbarch_tdep *regcache_tdep = gdbarch_tdep (regcache_arch); 1024 const bfd_byte *buf = fpset; 1025 1026 if (! ppc_floating_point_unit_p (regcache_arch)) 1027 return; 1028 1029 for (regi = 0; regi < ppc_num_fprs; regi++) 1030 regcache_raw_supply (regcache, 1031 regcache_tdep->ppc_fp0_regnum + regi, 1032 buf + 8 * regi); 1033 1034 /* The FPSCR is stored in the low order word of the last 1035 doubleword in the fpregset. */ 1036 regcache_raw_supply (regcache, regcache_tdep->ppc_fpscr_regnum, 1037 buf + 8 * 32 + 4); 1038 } 1039 1040 static struct regset ppc_linux_fpregset = { NULL, ppc_linux_supply_fpregset }; 1041 1042 static const struct regset * 1043 ppc_linux_regset_from_core_section (struct gdbarch *core_arch, 1044 const char *sect_name, size_t sect_size) 1045 { 1046 struct gdbarch_tdep *tdep = gdbarch_tdep (core_arch); 1047 if (strcmp (sect_name, ".reg") == 0) 1048 { 1049 if (tdep->wordsize == 4) 1050 return &ppc32_linux_gregset; 1051 else 1052 return &ppc64_linux_gregset; 1053 } 1054 if (strcmp (sect_name, ".reg2") == 0) 1055 return &ppc_linux_fpregset; 1056 return NULL; 1057 } 1058 1059 static void 1060 ppc_linux_init_abi (struct gdbarch_info info, 1061 struct gdbarch *gdbarch) 1062 { 1063 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); 1064 1065 if (tdep->wordsize == 4) 1066 { 1067 /* NOTE: jimb/2004-03-26: The System V ABI PowerPC Processor 1068 Supplement says that long doubles are sixteen bytes long. 1069 However, as one of the known warts of its ABI, PPC GNU/Linux 1070 uses eight-byte long doubles. GCC only recently got 128-bit 1071 long double support on PPC, so it may be changing soon. The 1072 Linux[sic] Standards Base says that programs that use 'long 1073 double' on PPC GNU/Linux are non-conformant. */ 1074 set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT); 1075 1076 /* Until November 2001, gcc did not comply with the 32 bit SysV 1077 R4 ABI requirement that structures less than or equal to 8 1078 bytes should be returned in registers. Instead GCC was using 1079 the the AIX/PowerOpen ABI - everything returned in memory 1080 (well ignoring vectors that is). When this was corrected, it 1081 wasn't fixed for GNU/Linux native platform. Use the 1082 PowerOpen struct convention. */ 1083 set_gdbarch_return_value (gdbarch, ppc_linux_return_value); 1084 1085 set_gdbarch_memory_remove_breakpoint (gdbarch, 1086 ppc_linux_memory_remove_breakpoint); 1087 1088 /* Shared library handling. */ 1089 set_gdbarch_in_solib_call_trampoline (gdbarch, in_plt_section); 1090 set_gdbarch_skip_trampoline_code (gdbarch, 1091 ppc_linux_skip_trampoline_code); 1092 set_solib_svr4_fetch_link_map_offsets 1093 (gdbarch, ppc_linux_svr4_fetch_link_map_offsets); 1094 } 1095 1096 if (tdep->wordsize == 8) 1097 { 1098 /* Handle PPC64 GNU/Linux function pointers (which are really 1099 function descriptors). */ 1100 set_gdbarch_convert_from_func_ptr_addr 1101 (gdbarch, ppc64_linux_convert_from_func_ptr_addr); 1102 1103 set_gdbarch_in_solib_call_trampoline 1104 (gdbarch, ppc64_in_solib_call_trampoline); 1105 set_gdbarch_skip_trampoline_code (gdbarch, ppc64_skip_trampoline_code); 1106 1107 /* PPC64 malloc's entry-point is called ".malloc". */ 1108 set_gdbarch_name_of_malloc (gdbarch, ".malloc"); 1109 } 1110 set_gdbarch_regset_from_core_section (gdbarch, ppc_linux_regset_from_core_section); 1111 frame_unwind_append_sniffer (gdbarch, ppc_linux_sigtramp_sniffer); 1112 } 1113 1114 void 1115 _initialize_ppc_linux_tdep (void) 1116 { 1117 /* Register for all sub-familes of the POWER/PowerPC: 32-bit and 1118 64-bit PowerPC, and the older rs6k. */ 1119 gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc, GDB_OSABI_LINUX, 1120 ppc_linux_init_abi); 1121 gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc64, GDB_OSABI_LINUX, 1122 ppc_linux_init_abi); 1123 gdbarch_register_osabi (bfd_arch_rs6000, bfd_mach_rs6k, GDB_OSABI_LINUX, 1124 ppc_linux_init_abi); 1125 } 1126