| 1 | /* Target-dependent code for GDB, the GNU debugger. |
| 2 | |
| 3 | Copyright (C) 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, |
| 4 | 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 |
| 5 | Free Software Foundation, Inc. |
| 6 | |
| 7 | This file is part of GDB. |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation; either version 3 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 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 | #include "regcache.h" |
| 32 | #include "value.h" |
| 33 | #include "osabi.h" |
| 34 | #include "regset.h" |
| 35 | #include "solib-svr4.h" |
| 36 | #include "ppc-tdep.h" |
| 37 | #include "trad-frame.h" |
| 38 | #include "frame-unwind.h" |
| 39 | #include "tramp-frame.h" |
| 40 | |
| 41 | static CORE_ADDR |
| 42 | ppc_linux_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc) |
| 43 | { |
| 44 | gdb_byte buf[4]; |
| 45 | struct obj_section *sect; |
| 46 | struct objfile *objfile; |
| 47 | unsigned long insn; |
| 48 | CORE_ADDR plt_start = 0; |
| 49 | CORE_ADDR symtab = 0; |
| 50 | CORE_ADDR strtab = 0; |
| 51 | int num_slots = -1; |
| 52 | int reloc_index = -1; |
| 53 | CORE_ADDR plt_table; |
| 54 | CORE_ADDR reloc; |
| 55 | CORE_ADDR sym; |
| 56 | long symidx; |
| 57 | char symname[1024]; |
| 58 | struct minimal_symbol *msymbol; |
| 59 | |
| 60 | /* Find the section pc is in; return if not in .plt */ |
| 61 | sect = find_pc_section (pc); |
| 62 | if (!sect || strcmp (sect->the_bfd_section->name, ".plt") != 0) |
| 63 | return 0; |
| 64 | |
| 65 | objfile = sect->objfile; |
| 66 | |
| 67 | /* Pick up the instruction at pc. It had better be of the |
| 68 | form |
| 69 | li r11, IDX |
| 70 | |
| 71 | where IDX is an index into the plt_table. */ |
| 72 | |
| 73 | if (target_read_memory (pc, buf, 4) != 0) |
| 74 | return 0; |
| 75 | insn = extract_unsigned_integer (buf, 4); |
| 76 | |
| 77 | if ((insn & 0xffff0000) != 0x39600000 /* li r11, VAL */ ) |
| 78 | return 0; |
| 79 | |
| 80 | reloc_index = (insn << 16) >> 16; |
| 81 | |
| 82 | /* Find the objfile that pc is in and obtain the information |
| 83 | necessary for finding the symbol name. */ |
| 84 | for (sect = objfile->sections; sect < objfile->sections_end; ++sect) |
| 85 | { |
| 86 | const char *secname = sect->the_bfd_section->name; |
| 87 | if (strcmp (secname, ".plt") == 0) |
| 88 | plt_start = sect->addr; |
| 89 | else if (strcmp (secname, ".rela.plt") == 0) |
| 90 | num_slots = ((int) sect->endaddr - (int) sect->addr) / 12; |
| 91 | else if (strcmp (secname, ".dynsym") == 0) |
| 92 | symtab = sect->addr; |
| 93 | else if (strcmp (secname, ".dynstr") == 0) |
| 94 | strtab = sect->addr; |
| 95 | } |
| 96 | |
| 97 | /* Make sure we have all the information we need. */ |
| 98 | if (plt_start == 0 || num_slots == -1 || symtab == 0 || strtab == 0) |
| 99 | return 0; |
| 100 | |
| 101 | /* Compute the value of the plt table */ |
| 102 | plt_table = plt_start + 72 + 8 * num_slots; |
| 103 | |
| 104 | /* Get address of the relocation entry (Elf32_Rela) */ |
| 105 | if (target_read_memory (plt_table + reloc_index, buf, 4) != 0) |
| 106 | return 0; |
| 107 | reloc = extract_unsigned_integer (buf, 4); |
| 108 | |
| 109 | sect = find_pc_section (reloc); |
| 110 | if (!sect) |
| 111 | return 0; |
| 112 | |
| 113 | if (strcmp (sect->the_bfd_section->name, ".text") == 0) |
| 114 | return reloc; |
| 115 | |
| 116 | /* Now get the r_info field which is the relocation type and symbol |
| 117 | index. */ |
| 118 | if (target_read_memory (reloc + 4, buf, 4) != 0) |
| 119 | return 0; |
| 120 | symidx = extract_unsigned_integer (buf, 4); |
| 121 | |
| 122 | /* Shift out the relocation type leaving just the symbol index */ |
| 123 | /* symidx = ELF32_R_SYM(symidx); */ |
| 124 | symidx = symidx >> 8; |
| 125 | |
| 126 | /* compute the address of the symbol */ |
| 127 | sym = symtab + symidx * 4; |
| 128 | |
| 129 | /* Fetch the string table index */ |
| 130 | if (target_read_memory (sym, buf, 4) != 0) |
| 131 | return 0; |
| 132 | symidx = extract_unsigned_integer (buf, 4); |
| 133 | |
| 134 | /* Fetch the string; we don't know how long it is. Is it possible |
| 135 | that the following will fail because we're trying to fetch too |
| 136 | much? */ |
| 137 | if (target_read_memory (strtab + symidx, (gdb_byte *) symname, |
| 138 | sizeof (symname)) != 0) |
| 139 | return 0; |
| 140 | |
| 141 | /* This might not work right if we have multiple symbols with the |
| 142 | same name; the only way to really get it right is to perform |
| 143 | the same sort of lookup as the dynamic linker. */ |
| 144 | msymbol = lookup_minimal_symbol_text (symname, NULL); |
| 145 | if (!msymbol) |
| 146 | return 0; |
| 147 | |
| 148 | return SYMBOL_VALUE_ADDRESS (msymbol); |
| 149 | } |
| 150 | |
| 151 | /* ppc_linux_memory_remove_breakpoints attempts to remove a breakpoint |
| 152 | in much the same fashion as memory_remove_breakpoint in mem-break.c, |
| 153 | but is careful not to write back the previous contents if the code |
| 154 | in question has changed in between inserting the breakpoint and |
| 155 | removing it. |
| 156 | |
| 157 | Here is the problem that we're trying to solve... |
| 158 | |
| 159 | Once upon a time, before introducing this function to remove |
| 160 | breakpoints from the inferior, setting a breakpoint on a shared |
| 161 | library function prior to running the program would not work |
| 162 | properly. In order to understand the problem, it is first |
| 163 | necessary to understand a little bit about dynamic linking on |
| 164 | this platform. |
| 165 | |
| 166 | A call to a shared library function is accomplished via a bl |
| 167 | (branch-and-link) instruction whose branch target is an entry |
| 168 | in the procedure linkage table (PLT). The PLT in the object |
| 169 | file is uninitialized. To gdb, prior to running the program, the |
| 170 | entries in the PLT are all zeros. |
| 171 | |
| 172 | Once the program starts running, the shared libraries are loaded |
| 173 | and the procedure linkage table is initialized, but the entries in |
| 174 | the table are not (necessarily) resolved. Once a function is |
| 175 | actually called, the code in the PLT is hit and the function is |
| 176 | resolved. In order to better illustrate this, an example is in |
| 177 | order; the following example is from the gdb testsuite. |
| 178 | |
| 179 | We start the program shmain. |
| 180 | |
| 181 | [kev@arroyo testsuite]$ ../gdb gdb.base/shmain |
| 182 | [...] |
| 183 | |
| 184 | We place two breakpoints, one on shr1 and the other on main. |
| 185 | |
| 186 | (gdb) b shr1 |
| 187 | Breakpoint 1 at 0x100409d4 |
| 188 | (gdb) b main |
| 189 | Breakpoint 2 at 0x100006a0: file gdb.base/shmain.c, line 44. |
| 190 | |
| 191 | Examine the instruction (and the immediatly following instruction) |
| 192 | upon which the breakpoint was placed. Note that the PLT entry |
| 193 | for shr1 contains zeros. |
| 194 | |
| 195 | (gdb) x/2i 0x100409d4 |
| 196 | 0x100409d4 <shr1>: .long 0x0 |
| 197 | 0x100409d8 <shr1+4>: .long 0x0 |
| 198 | |
| 199 | Now run 'til main. |
| 200 | |
| 201 | (gdb) r |
| 202 | Starting program: gdb.base/shmain |
| 203 | Breakpoint 1 at 0xffaf790: file gdb.base/shr1.c, line 19. |
| 204 | |
| 205 | Breakpoint 2, main () |
| 206 | at gdb.base/shmain.c:44 |
| 207 | 44 g = 1; |
| 208 | |
| 209 | Examine the PLT again. Note that the loading of the shared |
| 210 | library has initialized the PLT to code which loads a constant |
| 211 | (which I think is an index into the GOT) into r11 and then |
| 212 | branchs a short distance to the code which actually does the |
| 213 | resolving. |
| 214 | |
| 215 | (gdb) x/2i 0x100409d4 |
| 216 | 0x100409d4 <shr1>: li r11,4 |
| 217 | 0x100409d8 <shr1+4>: b 0x10040984 <sg+4> |
| 218 | (gdb) c |
| 219 | Continuing. |
| 220 | |
| 221 | Breakpoint 1, shr1 (x=1) |
| 222 | at gdb.base/shr1.c:19 |
| 223 | 19 l = 1; |
| 224 | |
| 225 | Now we've hit the breakpoint at shr1. (The breakpoint was |
| 226 | reset from the PLT entry to the actual shr1 function after the |
| 227 | shared library was loaded.) Note that the PLT entry has been |
| 228 | resolved to contain a branch that takes us directly to shr1. |
| 229 | (The real one, not the PLT entry.) |
| 230 | |
| 231 | (gdb) x/2i 0x100409d4 |
| 232 | 0x100409d4 <shr1>: b 0xffaf76c <shr1> |
| 233 | 0x100409d8 <shr1+4>: b 0x10040984 <sg+4> |
| 234 | |
| 235 | The thing to note here is that the PLT entry for shr1 has been |
| 236 | changed twice. |
| 237 | |
| 238 | Now the problem should be obvious. GDB places a breakpoint (a |
| 239 | trap instruction) on the zero value of the PLT entry for shr1. |
| 240 | Later on, after the shared library had been loaded and the PLT |
| 241 | initialized, GDB gets a signal indicating this fact and attempts |
| 242 | (as it always does when it stops) to remove all the breakpoints. |
| 243 | |
| 244 | The breakpoint removal was causing the former contents (a zero |
| 245 | word) to be written back to the now initialized PLT entry thus |
| 246 | destroying a portion of the initialization that had occurred only a |
| 247 | short time ago. When execution continued, the zero word would be |
| 248 | executed as an instruction an an illegal instruction trap was |
| 249 | generated instead. (0 is not a legal instruction.) |
| 250 | |
| 251 | The fix for this problem was fairly straightforward. The function |
| 252 | memory_remove_breakpoint from mem-break.c was copied to this file, |
| 253 | modified slightly, and renamed to ppc_linux_memory_remove_breakpoint. |
| 254 | In tm-linux.h, MEMORY_REMOVE_BREAKPOINT is defined to call this new |
| 255 | function. |
| 256 | |
| 257 | The differences between ppc_linux_memory_remove_breakpoint () and |
| 258 | memory_remove_breakpoint () are minor. All that the former does |
| 259 | that the latter does not is check to make sure that the breakpoint |
| 260 | location actually contains a breakpoint (trap instruction) prior |
| 261 | to attempting to write back the old contents. If it does contain |
| 262 | a trap instruction, we allow the old contents to be written back. |
| 263 | Otherwise, we silently do nothing. |
| 264 | |
| 265 | The big question is whether memory_remove_breakpoint () should be |
| 266 | changed to have the same functionality. The downside is that more |
| 267 | traffic is generated for remote targets since we'll have an extra |
| 268 | fetch of a memory word each time a breakpoint is removed. |
| 269 | |
| 270 | For the time being, we'll leave this self-modifying-code-friendly |
| 271 | version in ppc-linux-tdep.c, but it ought to be migrated somewhere |
| 272 | else in the event that some other platform has similar needs with |
| 273 | regard to removing breakpoints in some potentially self modifying |
| 274 | code. */ |
| 275 | int |
| 276 | ppc_linux_memory_remove_breakpoint (struct bp_target_info *bp_tgt) |
| 277 | { |
| 278 | CORE_ADDR addr = bp_tgt->placed_address; |
| 279 | const unsigned char *bp; |
| 280 | int val; |
| 281 | int bplen; |
| 282 | gdb_byte old_contents[BREAKPOINT_MAX]; |
| 283 | |
| 284 | /* Determine appropriate breakpoint contents and size for this address. */ |
| 285 | bp = gdbarch_breakpoint_from_pc (current_gdbarch, &addr, &bplen); |
| 286 | if (bp == NULL) |
| 287 | error (_("Software breakpoints not implemented for this target.")); |
| 288 | |
| 289 | val = target_read_memory (addr, old_contents, bplen); |
| 290 | |
| 291 | /* If our breakpoint is no longer at the address, this means that the |
| 292 | program modified the code on us, so it is wrong to put back the |
| 293 | old value */ |
| 294 | if (val == 0 && memcmp (bp, old_contents, bplen) == 0) |
| 295 | val = target_write_memory (addr, bp_tgt->shadow_contents, bplen); |
| 296 | |
| 297 | return val; |
| 298 | } |
| 299 | |
| 300 | /* For historic reasons, PPC 32 GNU/Linux follows PowerOpen rather |
| 301 | than the 32 bit SYSV R4 ABI structure return convention - all |
| 302 | structures, no matter their size, are put in memory. Vectors, |
| 303 | which were added later, do get returned in a register though. */ |
| 304 | |
| 305 | static enum return_value_convention |
| 306 | ppc_linux_return_value (struct gdbarch *gdbarch, struct type *valtype, |
| 307 | struct regcache *regcache, gdb_byte *readbuf, |
| 308 | const gdb_byte *writebuf) |
| 309 | { |
| 310 | if ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT |
| 311 | || TYPE_CODE (valtype) == TYPE_CODE_UNION) |
| 312 | && !((TYPE_LENGTH (valtype) == 16 || TYPE_LENGTH (valtype) == 8) |
| 313 | && TYPE_VECTOR (valtype))) |
| 314 | return RETURN_VALUE_STRUCT_CONVENTION; |
| 315 | else |
| 316 | return ppc_sysv_abi_return_value (gdbarch, valtype, regcache, readbuf, |
| 317 | writebuf); |
| 318 | } |
| 319 | |
| 320 | /* Macros for matching instructions. Note that, since all the |
| 321 | operands are masked off before they're or-ed into the instruction, |
| 322 | you can use -1 to make masks. */ |
| 323 | |
| 324 | #define insn_d(opcd, rts, ra, d) \ |
| 325 | ((((opcd) & 0x3f) << 26) \ |
| 326 | | (((rts) & 0x1f) << 21) \ |
| 327 | | (((ra) & 0x1f) << 16) \ |
| 328 | | ((d) & 0xffff)) |
| 329 | |
| 330 | #define insn_ds(opcd, rts, ra, d, xo) \ |
| 331 | ((((opcd) & 0x3f) << 26) \ |
| 332 | | (((rts) & 0x1f) << 21) \ |
| 333 | | (((ra) & 0x1f) << 16) \ |
| 334 | | ((d) & 0xfffc) \ |
| 335 | | ((xo) & 0x3)) |
| 336 | |
| 337 | #define insn_xfx(opcd, rts, spr, xo) \ |
| 338 | ((((opcd) & 0x3f) << 26) \ |
| 339 | | (((rts) & 0x1f) << 21) \ |
| 340 | | (((spr) & 0x1f) << 16) \ |
| 341 | | (((spr) & 0x3e0) << 6) \ |
| 342 | | (((xo) & 0x3ff) << 1)) |
| 343 | |
| 344 | /* Read a PPC instruction from memory. PPC instructions are always |
| 345 | big-endian, no matter what endianness the program is running in, so |
| 346 | we can't use read_memory_integer or one of its friends here. */ |
| 347 | static unsigned int |
| 348 | read_insn (CORE_ADDR pc) |
| 349 | { |
| 350 | unsigned char buf[4]; |
| 351 | |
| 352 | read_memory (pc, buf, 4); |
| 353 | return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | /* An instruction to match. */ |
| 358 | struct insn_pattern |
| 359 | { |
| 360 | unsigned int mask; /* mask the insn with this... */ |
| 361 | unsigned int data; /* ...and see if it matches this. */ |
| 362 | int optional; /* If non-zero, this insn may be absent. */ |
| 363 | }; |
| 364 | |
| 365 | /* Return non-zero if the instructions at PC match the series |
| 366 | described in PATTERN, or zero otherwise. PATTERN is an array of |
| 367 | 'struct insn_pattern' objects, terminated by an entry whose mask is |
| 368 | zero. |
| 369 | |
| 370 | When the match is successful, fill INSN[i] with what PATTERN[i] |
| 371 | matched. If PATTERN[i] is optional, and the instruction wasn't |
| 372 | present, set INSN[i] to 0 (which is not a valid PPC instruction). |
| 373 | INSN should have as many elements as PATTERN. Note that, if |
| 374 | PATTERN contains optional instructions which aren't present in |
| 375 | memory, then INSN will have holes, so INSN[i] isn't necessarily the |
| 376 | i'th instruction in memory. */ |
| 377 | static int |
| 378 | insns_match_pattern (CORE_ADDR pc, |
| 379 | struct insn_pattern *pattern, |
| 380 | unsigned int *insn) |
| 381 | { |
| 382 | int i; |
| 383 | |
| 384 | for (i = 0; pattern[i].mask; i++) |
| 385 | { |
| 386 | insn[i] = read_insn (pc); |
| 387 | if ((insn[i] & pattern[i].mask) == pattern[i].data) |
| 388 | pc += 4; |
| 389 | else if (pattern[i].optional) |
| 390 | insn[i] = 0; |
| 391 | else |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | return 1; |
| 396 | } |
| 397 | |
| 398 | |
| 399 | /* Return the 'd' field of the d-form instruction INSN, properly |
| 400 | sign-extended. */ |
| 401 | static CORE_ADDR |
| 402 | insn_d_field (unsigned int insn) |
| 403 | { |
| 404 | return ((((CORE_ADDR) insn & 0xffff) ^ 0x8000) - 0x8000); |
| 405 | } |
| 406 | |
| 407 | |
| 408 | /* Return the 'ds' field of the ds-form instruction INSN, with the two |
| 409 | zero bits concatenated at the right, and properly |
| 410 | sign-extended. */ |
| 411 | static CORE_ADDR |
| 412 | insn_ds_field (unsigned int insn) |
| 413 | { |
| 414 | return ((((CORE_ADDR) insn & 0xfffc) ^ 0x8000) - 0x8000); |
| 415 | } |
| 416 | |
| 417 | |
| 418 | /* If DESC is the address of a 64-bit PowerPC GNU/Linux function |
| 419 | descriptor, return the descriptor's entry point. */ |
| 420 | static CORE_ADDR |
| 421 | ppc64_desc_entry_point (CORE_ADDR desc) |
| 422 | { |
| 423 | /* The first word of the descriptor is the entry point. */ |
| 424 | return (CORE_ADDR) read_memory_unsigned_integer (desc, 8); |
| 425 | } |
| 426 | |
| 427 | |
| 428 | /* Pattern for the standard linkage function. These are built by |
| 429 | build_plt_stub in elf64-ppc.c, whose GLINK argument is always |
| 430 | zero. */ |
| 431 | static struct insn_pattern ppc64_standard_linkage[] = |
| 432 | { |
| 433 | /* addis r12, r2, <any> */ |
| 434 | { insn_d (-1, -1, -1, 0), insn_d (15, 12, 2, 0), 0 }, |
| 435 | |
| 436 | /* std r2, 40(r1) */ |
| 437 | { -1, insn_ds (62, 2, 1, 40, 0), 0 }, |
| 438 | |
| 439 | /* ld r11, <any>(r12) */ |
| 440 | { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 }, |
| 441 | |
| 442 | /* addis r12, r12, 1 <optional> */ |
| 443 | { insn_d (-1, -1, -1, -1), insn_d (15, 12, 2, 1), 1 }, |
| 444 | |
| 445 | /* ld r2, <any>(r12) */ |
| 446 | { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 2, 12, 0, 0), 0 }, |
| 447 | |
| 448 | /* addis r12, r12, 1 <optional> */ |
| 449 | { insn_d (-1, -1, -1, -1), insn_d (15, 12, 2, 1), 1 }, |
| 450 | |
| 451 | /* mtctr r11 */ |
| 452 | { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 11, 9, 467), |
| 453 | 0 }, |
| 454 | |
| 455 | /* ld r11, <any>(r12) */ |
| 456 | { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 }, |
| 457 | |
| 458 | /* bctr */ |
| 459 | { -1, 0x4e800420, 0 }, |
| 460 | |
| 461 | { 0, 0, 0 } |
| 462 | }; |
| 463 | #define PPC64_STANDARD_LINKAGE_LEN \ |
| 464 | (sizeof (ppc64_standard_linkage) / sizeof (ppc64_standard_linkage[0])) |
| 465 | |
| 466 | /* When the dynamic linker is doing lazy symbol resolution, the first |
| 467 | call to a function in another object will go like this: |
| 468 | |
| 469 | - The user's function calls the linkage function: |
| 470 | |
| 471 | 100007c4: 4b ff fc d5 bl 10000498 |
| 472 | 100007c8: e8 41 00 28 ld r2,40(r1) |
| 473 | |
| 474 | - The linkage function loads the entry point (and other stuff) from |
| 475 | the function descriptor in the PLT, and jumps to it: |
| 476 | |
| 477 | 10000498: 3d 82 00 00 addis r12,r2,0 |
| 478 | 1000049c: f8 41 00 28 std r2,40(r1) |
| 479 | 100004a0: e9 6c 80 98 ld r11,-32616(r12) |
| 480 | 100004a4: e8 4c 80 a0 ld r2,-32608(r12) |
| 481 | 100004a8: 7d 69 03 a6 mtctr r11 |
| 482 | 100004ac: e9 6c 80 a8 ld r11,-32600(r12) |
| 483 | 100004b0: 4e 80 04 20 bctr |
| 484 | |
| 485 | - But since this is the first time that PLT entry has been used, it |
| 486 | sends control to its glink entry. That loads the number of the |
| 487 | PLT entry and jumps to the common glink0 code: |
| 488 | |
| 489 | 10000c98: 38 00 00 00 li r0,0 |
| 490 | 10000c9c: 4b ff ff dc b 10000c78 |
| 491 | |
| 492 | - The common glink0 code then transfers control to the dynamic |
| 493 | linker's fixup code: |
| 494 | |
| 495 | 10000c78: e8 41 00 28 ld r2,40(r1) |
| 496 | 10000c7c: 3d 82 00 00 addis r12,r2,0 |
| 497 | 10000c80: e9 6c 80 80 ld r11,-32640(r12) |
| 498 | 10000c84: e8 4c 80 88 ld r2,-32632(r12) |
| 499 | 10000c88: 7d 69 03 a6 mtctr r11 |
| 500 | 10000c8c: e9 6c 80 90 ld r11,-32624(r12) |
| 501 | 10000c90: 4e 80 04 20 bctr |
| 502 | |
| 503 | Eventually, this code will figure out how to skip all of this, |
| 504 | including the dynamic linker. At the moment, we just get through |
| 505 | the linkage function. */ |
| 506 | |
| 507 | /* If the current thread is about to execute a series of instructions |
| 508 | at PC matching the ppc64_standard_linkage pattern, and INSN is the result |
| 509 | from that pattern match, return the code address to which the |
| 510 | standard linkage function will send them. (This doesn't deal with |
| 511 | dynamic linker lazy symbol resolution stubs.) */ |
| 512 | static CORE_ADDR |
| 513 | ppc64_standard_linkage_target (struct frame_info *frame, |
| 514 | CORE_ADDR pc, unsigned int *insn) |
| 515 | { |
| 516 | struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (frame)); |
| 517 | |
| 518 | /* The address of the function descriptor this linkage function |
| 519 | references. */ |
| 520 | CORE_ADDR desc |
| 521 | = ((CORE_ADDR) get_frame_register_unsigned (frame, |
| 522 | tdep->ppc_gp0_regnum + 2) |
| 523 | + (insn_d_field (insn[0]) << 16) |
| 524 | + insn_ds_field (insn[2])); |
| 525 | |
| 526 | /* The first word of the descriptor is the entry point. Return that. */ |
| 527 | return ppc64_desc_entry_point (desc); |
| 528 | } |
| 529 | |
| 530 | |
| 531 | /* Given that we've begun executing a call trampoline at PC, return |
| 532 | the entry point of the function the trampoline will go to. */ |
| 533 | static CORE_ADDR |
| 534 | ppc64_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc) |
| 535 | { |
| 536 | unsigned int ppc64_standard_linkage_insn[PPC64_STANDARD_LINKAGE_LEN]; |
| 537 | |
| 538 | if (insns_match_pattern (pc, ppc64_standard_linkage, |
| 539 | ppc64_standard_linkage_insn)) |
| 540 | return ppc64_standard_linkage_target (frame, pc, |
| 541 | ppc64_standard_linkage_insn); |
| 542 | else |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | |
| 547 | /* Support for convert_from_func_ptr_addr (ARCH, ADDR, TARG) on PPC |
| 548 | GNU/Linux. |
| 549 | |
| 550 | Usually a function pointer's representation is simply the address |
| 551 | of the function. On GNU/Linux on the PowerPC however, a function |
| 552 | pointer may be a pointer to a function descriptor. |
| 553 | |
| 554 | For PPC64, a function descriptor is a TOC entry, in a data section, |
| 555 | which contains three words: the first word is the address of the |
| 556 | function, the second word is the TOC pointer (r2), and the third word |
| 557 | is the static chain value. |
| 558 | |
| 559 | For PPC32, there are two kinds of function pointers: non-secure and |
| 560 | secure. Non-secure function pointers point directly to the |
| 561 | function in a code section and thus need no translation. Secure |
| 562 | ones (from GCC's -msecure-plt option) are in a data section and |
| 563 | contain one word: the address of the function. |
| 564 | |
| 565 | Throughout GDB it is currently assumed that a function pointer contains |
| 566 | the address of the function, which is not easy to fix. In addition, the |
| 567 | conversion of a function address to a function pointer would |
| 568 | require allocation of a TOC entry in the inferior's memory space, |
| 569 | with all its drawbacks. To be able to call C++ virtual methods in |
| 570 | the inferior (which are called via function pointers), |
| 571 | find_function_addr uses this function to get the function address |
| 572 | from a function pointer. |
| 573 | |
| 574 | If ADDR points at what is clearly a function descriptor, transform |
| 575 | it into the address of the corresponding function, if needed. Be |
| 576 | conservative, otherwise GDB will do the transformation on any |
| 577 | random addresses such as occur when there is no symbol table. */ |
| 578 | |
| 579 | static CORE_ADDR |
| 580 | ppc_linux_convert_from_func_ptr_addr (struct gdbarch *gdbarch, |
| 581 | CORE_ADDR addr, |
| 582 | struct target_ops *targ) |
| 583 | { |
| 584 | struct gdbarch_tdep *tdep; |
| 585 | struct section_table *s = target_section_by_addr (targ, addr); |
| 586 | char *sect_name = NULL; |
| 587 | |
| 588 | if (!s) |
| 589 | return addr; |
| 590 | |
| 591 | tdep = gdbarch_tdep (gdbarch); |
| 592 | |
| 593 | switch (tdep->wordsize) |
| 594 | { |
| 595 | case 4: |
| 596 | sect_name = ".plt"; |
| 597 | break; |
| 598 | case 8: |
| 599 | sect_name = ".opd"; |
| 600 | break; |
| 601 | default: |
| 602 | internal_error (__FILE__, __LINE__, |
| 603 | _("failed internal consistency check")); |
| 604 | } |
| 605 | |
| 606 | /* Check if ADDR points to a function descriptor. */ |
| 607 | |
| 608 | /* NOTE: this depends on the coincidence that the address of a functions |
| 609 | entry point is contained in the first word of its function descriptor |
| 610 | for both PPC-64 and for PPC-32 with secure PLTs. */ |
| 611 | if ((strcmp (s->the_bfd_section->name, sect_name) == 0) |
| 612 | && s->the_bfd_section->flags & SEC_DATA) |
| 613 | return get_target_memory_unsigned (targ, addr, tdep->wordsize); |
| 614 | |
| 615 | return addr; |
| 616 | } |
| 617 | |
| 618 | /* This wrapper clears areas in the linux gregset not written by |
| 619 | ppc_collect_gregset. */ |
| 620 | |
| 621 | static void |
| 622 | ppc_linux_collect_gregset (const struct regset *regset, |
| 623 | const struct regcache *regcache, |
| 624 | int regnum, void *gregs, size_t len) |
| 625 | { |
| 626 | if (regnum == -1) |
| 627 | memset (gregs, 0, len); |
| 628 | ppc_collect_gregset (regset, regcache, regnum, gregs, len); |
| 629 | } |
| 630 | |
| 631 | /* Regset descriptions. */ |
| 632 | static const struct ppc_reg_offsets ppc32_linux_reg_offsets = |
| 633 | { |
| 634 | /* General-purpose registers. */ |
| 635 | /* .r0_offset = */ 0, |
| 636 | /* .gpr_size = */ 4, |
| 637 | /* .xr_size = */ 4, |
| 638 | /* .pc_offset = */ 128, |
| 639 | /* .ps_offset = */ 132, |
| 640 | /* .cr_offset = */ 152, |
| 641 | /* .lr_offset = */ 144, |
| 642 | /* .ctr_offset = */ 140, |
| 643 | /* .xer_offset = */ 148, |
| 644 | /* .mq_offset = */ 156, |
| 645 | |
| 646 | /* Floating-point registers. */ |
| 647 | /* .f0_offset = */ 0, |
| 648 | /* .fpscr_offset = */ 256, |
| 649 | /* .fpscr_size = */ 8, |
| 650 | |
| 651 | /* AltiVec registers. */ |
| 652 | /* .vr0_offset = */ 0, |
| 653 | /* .vrsave_offset = */ 512, |
| 654 | /* .vscr_offset = */ 512 + 12 |
| 655 | }; |
| 656 | |
| 657 | static const struct ppc_reg_offsets ppc64_linux_reg_offsets = |
| 658 | { |
| 659 | /* General-purpose registers. */ |
| 660 | /* .r0_offset = */ 0, |
| 661 | /* .gpr_size = */ 8, |
| 662 | /* .xr_size = */ 8, |
| 663 | /* .pc_offset = */ 256, |
| 664 | /* .ps_offset = */ 264, |
| 665 | /* .cr_offset = */ 304, |
| 666 | /* .lr_offset = */ 288, |
| 667 | /* .ctr_offset = */ 280, |
| 668 | /* .xer_offset = */ 296, |
| 669 | /* .mq_offset = */ 312, |
| 670 | |
| 671 | /* Floating-point registers. */ |
| 672 | /* .f0_offset = */ 0, |
| 673 | /* .fpscr_offset = */ 256, |
| 674 | /* .fpscr_size = */ 8, |
| 675 | |
| 676 | /* AltiVec registers. */ |
| 677 | /* .vr0_offset = */ 0, |
| 678 | /* .vrsave_offset = */ 528, |
| 679 | /* .vscr_offset = */ 512 + 12 |
| 680 | }; |
| 681 | |
| 682 | static const struct regset ppc32_linux_gregset = { |
| 683 | &ppc32_linux_reg_offsets, |
| 684 | ppc_supply_gregset, |
| 685 | ppc_linux_collect_gregset, |
| 686 | NULL |
| 687 | }; |
| 688 | |
| 689 | static const struct regset ppc64_linux_gregset = { |
| 690 | &ppc64_linux_reg_offsets, |
| 691 | ppc_supply_gregset, |
| 692 | ppc_linux_collect_gregset, |
| 693 | NULL |
| 694 | }; |
| 695 | |
| 696 | static const struct regset ppc32_linux_fpregset = { |
| 697 | &ppc32_linux_reg_offsets, |
| 698 | ppc_supply_fpregset, |
| 699 | ppc_collect_fpregset, |
| 700 | NULL |
| 701 | }; |
| 702 | |
| 703 | const struct regset * |
| 704 | ppc_linux_gregset (int wordsize) |
| 705 | { |
| 706 | return wordsize == 8 ? &ppc64_linux_gregset : &ppc32_linux_gregset; |
| 707 | } |
| 708 | |
| 709 | const struct regset * |
| 710 | ppc_linux_fpregset (void) |
| 711 | { |
| 712 | return &ppc32_linux_fpregset; |
| 713 | } |
| 714 | |
| 715 | static const struct regset * |
| 716 | ppc_linux_regset_from_core_section (struct gdbarch *core_arch, |
| 717 | const char *sect_name, size_t sect_size) |
| 718 | { |
| 719 | struct gdbarch_tdep *tdep = gdbarch_tdep (core_arch); |
| 720 | if (strcmp (sect_name, ".reg") == 0) |
| 721 | { |
| 722 | if (tdep->wordsize == 4) |
| 723 | return &ppc32_linux_gregset; |
| 724 | else |
| 725 | return &ppc64_linux_gregset; |
| 726 | } |
| 727 | if (strcmp (sect_name, ".reg2") == 0) |
| 728 | return &ppc32_linux_fpregset; |
| 729 | return NULL; |
| 730 | } |
| 731 | |
| 732 | static void |
| 733 | ppc_linux_sigtramp_cache (struct frame_info *next_frame, |
| 734 | struct trad_frame_cache *this_cache, |
| 735 | CORE_ADDR func, LONGEST offset, |
| 736 | int bias) |
| 737 | { |
| 738 | CORE_ADDR base; |
| 739 | CORE_ADDR regs; |
| 740 | CORE_ADDR gpregs; |
| 741 | CORE_ADDR fpregs; |
| 742 | int i; |
| 743 | struct gdbarch *gdbarch = get_frame_arch (next_frame); |
| 744 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
| 745 | |
| 746 | base = frame_unwind_register_unsigned (next_frame, |
| 747 | gdbarch_sp_regnum (current_gdbarch)); |
| 748 | if (bias > 0 && frame_pc_unwind (next_frame) != func) |
| 749 | /* See below, some signal trampolines increment the stack as their |
| 750 | first instruction, need to compensate for that. */ |
| 751 | base -= bias; |
| 752 | |
| 753 | /* Find the address of the register buffer pointer. */ |
| 754 | regs = base + offset; |
| 755 | /* Use that to find the address of the corresponding register |
| 756 | buffers. */ |
| 757 | gpregs = read_memory_unsigned_integer (regs, tdep->wordsize); |
| 758 | fpregs = gpregs + 48 * tdep->wordsize; |
| 759 | |
| 760 | /* General purpose. */ |
| 761 | for (i = 0; i < 32; i++) |
| 762 | { |
| 763 | int regnum = i + tdep->ppc_gp0_regnum; |
| 764 | trad_frame_set_reg_addr (this_cache, regnum, gpregs + i * tdep->wordsize); |
| 765 | } |
| 766 | trad_frame_set_reg_addr (this_cache, |
| 767 | gdbarch_pc_regnum (current_gdbarch), |
| 768 | gpregs + 32 * tdep->wordsize); |
| 769 | trad_frame_set_reg_addr (this_cache, tdep->ppc_ctr_regnum, |
| 770 | gpregs + 35 * tdep->wordsize); |
| 771 | trad_frame_set_reg_addr (this_cache, tdep->ppc_lr_regnum, |
| 772 | gpregs + 36 * tdep->wordsize); |
| 773 | trad_frame_set_reg_addr (this_cache, tdep->ppc_xer_regnum, |
| 774 | gpregs + 37 * tdep->wordsize); |
| 775 | trad_frame_set_reg_addr (this_cache, tdep->ppc_cr_regnum, |
| 776 | gpregs + 38 * tdep->wordsize); |
| 777 | |
| 778 | if (ppc_floating_point_unit_p (gdbarch)) |
| 779 | { |
| 780 | /* Floating point registers. */ |
| 781 | for (i = 0; i < 32; i++) |
| 782 | { |
| 783 | int regnum = i + gdbarch_fp0_regnum (current_gdbarch); |
| 784 | trad_frame_set_reg_addr (this_cache, regnum, |
| 785 | fpregs + i * tdep->wordsize); |
| 786 | } |
| 787 | trad_frame_set_reg_addr (this_cache, tdep->ppc_fpscr_regnum, |
| 788 | fpregs + 32 * tdep->wordsize); |
| 789 | } |
| 790 | trad_frame_set_id (this_cache, frame_id_build (base, func)); |
| 791 | } |
| 792 | |
| 793 | static void |
| 794 | ppc32_linux_sigaction_cache_init (const struct tramp_frame *self, |
| 795 | struct frame_info *next_frame, |
| 796 | struct trad_frame_cache *this_cache, |
| 797 | CORE_ADDR func) |
| 798 | { |
| 799 | ppc_linux_sigtramp_cache (next_frame, this_cache, func, |
| 800 | 0xd0 /* Offset to ucontext_t. */ |
| 801 | + 0x30 /* Offset to .reg. */, |
| 802 | 0); |
| 803 | } |
| 804 | |
| 805 | static void |
| 806 | ppc64_linux_sigaction_cache_init (const struct tramp_frame *self, |
| 807 | struct frame_info *next_frame, |
| 808 | struct trad_frame_cache *this_cache, |
| 809 | CORE_ADDR func) |
| 810 | { |
| 811 | ppc_linux_sigtramp_cache (next_frame, this_cache, func, |
| 812 | 0x80 /* Offset to ucontext_t. */ |
| 813 | + 0xe0 /* Offset to .reg. */, |
| 814 | 128); |
| 815 | } |
| 816 | |
| 817 | static void |
| 818 | ppc32_linux_sighandler_cache_init (const struct tramp_frame *self, |
| 819 | struct frame_info *next_frame, |
| 820 | struct trad_frame_cache *this_cache, |
| 821 | CORE_ADDR func) |
| 822 | { |
| 823 | ppc_linux_sigtramp_cache (next_frame, this_cache, func, |
| 824 | 0x40 /* Offset to ucontext_t. */ |
| 825 | + 0x1c /* Offset to .reg. */, |
| 826 | 0); |
| 827 | } |
| 828 | |
| 829 | static void |
| 830 | ppc64_linux_sighandler_cache_init (const struct tramp_frame *self, |
| 831 | struct frame_info *next_frame, |
| 832 | struct trad_frame_cache *this_cache, |
| 833 | CORE_ADDR func) |
| 834 | { |
| 835 | ppc_linux_sigtramp_cache (next_frame, this_cache, func, |
| 836 | 0x80 /* Offset to struct sigcontext. */ |
| 837 | + 0x38 /* Offset to .reg. */, |
| 838 | 128); |
| 839 | } |
| 840 | |
| 841 | static struct tramp_frame ppc32_linux_sigaction_tramp_frame = { |
| 842 | SIGTRAMP_FRAME, |
| 843 | 4, |
| 844 | { |
| 845 | { 0x380000ac, -1 }, /* li r0, 172 */ |
| 846 | { 0x44000002, -1 }, /* sc */ |
| 847 | { TRAMP_SENTINEL_INSN }, |
| 848 | }, |
| 849 | ppc32_linux_sigaction_cache_init |
| 850 | }; |
| 851 | static struct tramp_frame ppc64_linux_sigaction_tramp_frame = { |
| 852 | SIGTRAMP_FRAME, |
| 853 | 4, |
| 854 | { |
| 855 | { 0x38210080, -1 }, /* addi r1,r1,128 */ |
| 856 | { 0x380000ac, -1 }, /* li r0, 172 */ |
| 857 | { 0x44000002, -1 }, /* sc */ |
| 858 | { TRAMP_SENTINEL_INSN }, |
| 859 | }, |
| 860 | ppc64_linux_sigaction_cache_init |
| 861 | }; |
| 862 | static struct tramp_frame ppc32_linux_sighandler_tramp_frame = { |
| 863 | SIGTRAMP_FRAME, |
| 864 | 4, |
| 865 | { |
| 866 | { 0x38000077, -1 }, /* li r0,119 */ |
| 867 | { 0x44000002, -1 }, /* sc */ |
| 868 | { TRAMP_SENTINEL_INSN }, |
| 869 | }, |
| 870 | ppc32_linux_sighandler_cache_init |
| 871 | }; |
| 872 | static struct tramp_frame ppc64_linux_sighandler_tramp_frame = { |
| 873 | SIGTRAMP_FRAME, |
| 874 | 4, |
| 875 | { |
| 876 | { 0x38210080, -1 }, /* addi r1,r1,128 */ |
| 877 | { 0x38000077, -1 }, /* li r0,119 */ |
| 878 | { 0x44000002, -1 }, /* sc */ |
| 879 | { TRAMP_SENTINEL_INSN }, |
| 880 | }, |
| 881 | ppc64_linux_sighandler_cache_init |
| 882 | }; |
| 883 | |
| 884 | static void |
| 885 | ppc_linux_init_abi (struct gdbarch_info info, |
| 886 | struct gdbarch *gdbarch) |
| 887 | { |
| 888 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
| 889 | |
| 890 | /* NOTE: jimb/2004-03-26: The System V ABI PowerPC Processor |
| 891 | Supplement says that long doubles are sixteen bytes long. |
| 892 | However, as one of the known warts of its ABI, PPC GNU/Linux uses |
| 893 | eight-byte long doubles. GCC only recently got 128-bit long |
| 894 | double support on PPC, so it may be changing soon. The |
| 895 | Linux[sic] Standards Base says that programs that use 'long |
| 896 | double' on PPC GNU/Linux are non-conformant. */ |
| 897 | /* NOTE: cagney/2005-01-25: True for both 32- and 64-bit. */ |
| 898 | set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT); |
| 899 | |
| 900 | /* Handle PPC GNU/Linux 64-bit function pointers (which are really |
| 901 | function descriptors) and 32-bit secure PLT entries. */ |
| 902 | set_gdbarch_convert_from_func_ptr_addr |
| 903 | (gdbarch, ppc_linux_convert_from_func_ptr_addr); |
| 904 | |
| 905 | if (tdep->wordsize == 4) |
| 906 | { |
| 907 | /* Until November 2001, gcc did not comply with the 32 bit SysV |
| 908 | R4 ABI requirement that structures less than or equal to 8 |
| 909 | bytes should be returned in registers. Instead GCC was using |
| 910 | the the AIX/PowerOpen ABI - everything returned in memory |
| 911 | (well ignoring vectors that is). When this was corrected, it |
| 912 | wasn't fixed for GNU/Linux native platform. Use the |
| 913 | PowerOpen struct convention. */ |
| 914 | set_gdbarch_return_value (gdbarch, ppc_linux_return_value); |
| 915 | |
| 916 | set_gdbarch_memory_remove_breakpoint (gdbarch, |
| 917 | ppc_linux_memory_remove_breakpoint); |
| 918 | |
| 919 | /* Shared library handling. */ |
| 920 | set_gdbarch_skip_trampoline_code (gdbarch, |
| 921 | ppc_linux_skip_trampoline_code); |
| 922 | set_solib_svr4_fetch_link_map_offsets |
| 923 | (gdbarch, svr4_ilp32_fetch_link_map_offsets); |
| 924 | |
| 925 | /* Trampolines. */ |
| 926 | tramp_frame_prepend_unwinder (gdbarch, &ppc32_linux_sigaction_tramp_frame); |
| 927 | tramp_frame_prepend_unwinder (gdbarch, &ppc32_linux_sighandler_tramp_frame); |
| 928 | } |
| 929 | |
| 930 | if (tdep->wordsize == 8) |
| 931 | { |
| 932 | /* Shared library handling. */ |
| 933 | set_gdbarch_skip_trampoline_code (gdbarch, ppc64_skip_trampoline_code); |
| 934 | set_solib_svr4_fetch_link_map_offsets |
| 935 | (gdbarch, svr4_lp64_fetch_link_map_offsets); |
| 936 | |
| 937 | /* Trampolines. */ |
| 938 | tramp_frame_prepend_unwinder (gdbarch, &ppc64_linux_sigaction_tramp_frame); |
| 939 | tramp_frame_prepend_unwinder (gdbarch, &ppc64_linux_sighandler_tramp_frame); |
| 940 | } |
| 941 | set_gdbarch_regset_from_core_section (gdbarch, ppc_linux_regset_from_core_section); |
| 942 | |
| 943 | /* Enable TLS support. */ |
| 944 | set_gdbarch_fetch_tls_load_module_address (gdbarch, |
| 945 | svr4_fetch_objfile_link_map); |
| 946 | } |
| 947 | |
| 948 | void |
| 949 | _initialize_ppc_linux_tdep (void) |
| 950 | { |
| 951 | /* Register for all sub-familes of the POWER/PowerPC: 32-bit and |
| 952 | 64-bit PowerPC, and the older rs6k. */ |
| 953 | gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc, GDB_OSABI_LINUX, |
| 954 | ppc_linux_init_abi); |
| 955 | gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc64, GDB_OSABI_LINUX, |
| 956 | ppc_linux_init_abi); |
| 957 | gdbarch_register_osabi (bfd_arch_rs6000, bfd_mach_rs6k, GDB_OSABI_LINUX, |
| 958 | ppc_linux_init_abi); |
| 959 | } |