plt.c revision 929bd57ca202fd2f2e8485ebf65d683e664f67b5
1/* 2 * This file is part of ltrace. 3 * Copyright (C) 2012 Petr Machata, Red Hat Inc. 4 * Copyright (C) 2004,2008,2009 Juan Cespedes 5 * Copyright (C) 2006 Paul Gilliam 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as 9 * published by the Free Software Foundation; either version 2 of the 10 * License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 20 * 02110-1301 USA 21 */ 22 23#include <gelf.h> 24#include <sys/ptrace.h> 25#include <errno.h> 26#include <inttypes.h> 27#include <assert.h> 28#include <string.h> 29 30#include "proc.h" 31#include "common.h" 32#include "insn.h" 33#include "library.h" 34#include "breakpoint.h" 35#include "linux-gnu/trace.h" 36#include "backend.h" 37 38/* There are two PLT types on 32-bit PPC: old-style, BSS PLT, and 39 * new-style "secure" PLT. We can tell one from the other by the 40 * flags on the .plt section. If it's +X (executable), it's BSS PLT, 41 * otherwise it's secure. 42 * 43 * BSS PLT works the same way as most architectures: the .plt section 44 * contains trampolines and we put breakpoints to those. If not 45 * prelinked, .plt contains zeroes, and dynamic linker fills in the 46 * initial set of trampolines, which means that we need to delay 47 * enabling breakpoints until after binary entry point is hit. 48 * Additionally, after first call, dynamic linker updates .plt with 49 * branch to resolved address. That means that on first hit, we must 50 * do something similar to the PPC64 gambit described below. 51 * 52 * With secure PLT, the .plt section doesn't contain instructions but 53 * addresses. The real PLT table is stored in .text. Addresses of 54 * those PLT entries can be computed, and apart from the fact that 55 * they are in .text, they are ordinary PLT entries. 56 * 57 * 64-bit PPC is more involved. Program linker creates for each 58 * library call a _stub_ symbol named xxxxxxxx.plt_call.<callee> 59 * (where xxxxxxxx is a hexadecimal number). That stub does the call 60 * dispatch: it loads an address of a function to call from the 61 * section .plt, and branches. PLT entries themselves are essentially 62 * a curried call to the resolver. When the symbol is resolved, the 63 * resolver updates the value stored in .plt, and the next time 64 * around, the stub calls the library function directly. So we make 65 * at most one trip (none if the binary is prelinked) through each PLT 66 * entry, and correspondingly that is useless as a breakpoint site. 67 * 68 * Note the three confusing terms: stubs (that play the role of PLT 69 * entries), PLT entries, .plt section. 70 * 71 * We first check symbol tables and see if we happen to have stub 72 * symbols available. If yes we just put breakpoints to those, and 73 * treat them as usual breakpoints. The only tricky part is realizing 74 * that there can be more than one breakpoint per symbol. 75 * 76 * The case that we don't have the stub symbols available is harder. 77 * The following scheme uses two kinds of PLT breakpoints: unresolved 78 * and resolved (to some address). When the process starts (or when 79 * we attach), we distribute unresolved PLT breakpoints to the PLT 80 * entries (not stubs). Then we look in .plt, and for each entry 81 * whose value is different than the corresponding PLT entry address, 82 * we assume it was already resolved, and convert the breakpoint to 83 * resolved. We also rewrite the resolved value in .plt back to the 84 * PLT address. 85 * 86 * When a PLT entry hits a resolved breakpoint (which happens because 87 * we rewrite .plt with the original unresolved addresses), we move 88 * the instruction pointer to the corresponding address and continue 89 * the process as if nothing happened. 90 * 91 * When unresolved PLT entry is called for the first time, we need to 92 * catch the new value that the resolver will write to a .plt slot. 93 * We also need to prevent another thread from racing through and 94 * taking the branch without ltrace noticing. So when unresolved PLT 95 * entry hits, we have to stop all threads. We then single-step 96 * through the resolver, until the .plt slot changes. When it does, 97 * we treat it the same way as above: convert the PLT breakpoint to 98 * resolved, and rewrite the .plt value back to PLT address. We then 99 * start all threads again. 100 * 101 * As an optimization, we remember the address where the address was 102 * resolved, and put a breakpoint there. The next time around (when 103 * the next PLT entry is to be resolved), instead of single-stepping 104 * through half the dynamic linker, we just let the thread run and hit 105 * this breakpoint. When it hits, we know the PLT entry was resolved. 106 * 107 * XXX TODO If we have hardware watch point, we might put a read watch 108 * on .plt slot, and discover the offenders this way. I don't know 109 * the details, but I assume at most a handful (like, one or two, if 110 * available at all) addresses may be watched at a time, and thus this 111 * would be used as an amendment of the above rather than full-on 112 * solution to PLT tracing on PPC. 113 */ 114 115#define PPC_PLT_STUB_SIZE 16 116#define PPC64_PLT_STUB_SIZE 8 //xxx 117 118static inline int 119host_powerpc64() 120{ 121#ifdef __powerpc64__ 122 return 1; 123#else 124 return 0; 125#endif 126} 127 128int 129read_target_4(struct process *proc, arch_addr_t addr, uint32_t *lp) 130{ 131 unsigned long l = ptrace(PTRACE_PEEKTEXT, proc->pid, addr, 0); 132 if (l == -1UL && errno) 133 return -1; 134#ifdef __powerpc64__ 135 l >>= 32; 136#endif 137 *lp = l; 138 return 0; 139} 140 141static int 142read_target_8(struct process *proc, arch_addr_t addr, uint64_t *lp) 143{ 144 unsigned long l = ptrace(PTRACE_PEEKTEXT, proc->pid, addr, 0); 145 if (l == -1UL && errno) 146 return -1; 147 if (host_powerpc64()) { 148 *lp = l; 149 } else { 150 unsigned long l2 = ptrace(PTRACE_PEEKTEXT, proc->pid, 151 addr + 4, 0); 152 if (l2 == -1UL && errno) 153 return -1; 154 *lp = ((uint64_t)l << 32) | l2; 155 } 156 return 0; 157} 158 159int 160read_target_long(struct process *proc, arch_addr_t addr, uint64_t *lp) 161{ 162 if (proc->e_machine == EM_PPC) { 163 uint32_t w; 164 int ret = read_target_4(proc, addr, &w); 165 if (ret >= 0) 166 *lp = (uint64_t)w; 167 return ret; 168 } else { 169 return read_target_8(proc, addr, lp); 170 } 171} 172 173static void 174mark_as_resolved(struct library_symbol *libsym, GElf_Addr value) 175{ 176 libsym->arch.type = PPC_PLT_RESOLVED; 177 libsym->arch.resolved_value = value; 178} 179 180void 181arch_dynlink_done(struct process *proc) 182{ 183 /* On PPC32 with BSS PLT, we need to enable delayed symbols. */ 184 struct library_symbol *libsym = NULL; 185 while ((libsym = proc_each_symbol(proc, libsym, 186 library_symbol_delayed_cb, NULL))) { 187 if (read_target_8(proc, libsym->enter_addr, 188 &libsym->arch.resolved_value) < 0) { 189 fprintf(stderr, 190 "couldn't read PLT value for %s(%p): %s\n", 191 libsym->name, libsym->enter_addr, 192 strerror(errno)); 193 return; 194 } 195 196 /* arch_dynlink_done is called on attach as well. In 197 * that case some slots will have been resolved 198 * already. Unresolved PLT looks like this: 199 * 200 * <sleep@plt>: li r11,0 201 * <sleep@plt+4>: b "resolve" 202 * 203 * "resolve" is another address in PLTGOT (the same 204 * block that all the PLT slots are it). When 205 * resolved, it looks either this way: 206 * 207 * <sleep@plt>: b 0xfea88d0 <sleep> 208 * 209 * Which is easy to detect. It can also look this 210 * way: 211 * 212 * <sleep@plt>: li r11,0 213 * <sleep@plt+4>: b "dispatch" 214 * 215 * The "dispatch" address lies in PLTGOT as well. In 216 * current GNU toolchain, "dispatch" address is the 217 * same as PLTGOT address. We rely on this to figure 218 * out whether the address is resolved or not. */ 219 uint32_t insn1 = libsym->arch.resolved_value >> 32; 220 uint32_t insn2 = (uint32_t)libsym->arch.resolved_value; 221 if ((insn1 & BRANCH_MASK) == B_INSN 222 || ((insn2 & BRANCH_MASK) == B_INSN 223 /* XXX double cast */ 224 && (ppc_branch_dest(libsym->enter_addr + 4, insn2) 225 == (void*)(long)libsym->lib->arch.pltgot_addr))) 226 mark_as_resolved(libsym, libsym->arch.resolved_value); 227 228 if (proc_activate_delayed_symbol(proc, libsym) < 0) 229 return; 230 231 /* XXX double cast */ 232 libsym->arch.plt_slot_addr 233 = (GElf_Addr)(uintptr_t)libsym->enter_addr; 234 } 235} 236 237GElf_Addr 238arch_plt_sym_val(struct ltelf *lte, size_t ndx, GElf_Rela *rela) 239{ 240 if (lte->ehdr.e_machine == EM_PPC && lte->arch.secure_plt) { 241 assert(lte->arch.plt_stub_vma != 0); 242 return lte->arch.plt_stub_vma + PPC_PLT_STUB_SIZE * ndx; 243 244 } else if (lte->ehdr.e_machine == EM_PPC) { 245 return rela->r_offset; 246 247 } else { 248 /* If we get here, we don't have stub symbols. In 249 * that case we put brakpoints to PLT entries the same 250 * as the PPC32 secure PLT case does. */ 251 assert(lte->arch.plt_stub_vma != 0); 252 return lte->arch.plt_stub_vma + PPC64_PLT_STUB_SIZE * ndx; 253 } 254} 255 256/* This entry point is called when ltelf is not available 257 * anymore--during runtime. At that point we don't have to concern 258 * ourselves with bias, as the values in OPD have been resolved 259 * already. */ 260int 261arch_translate_address_dyn(struct process *proc, 262 arch_addr_t addr, arch_addr_t *ret) 263{ 264 if (proc->e_machine == EM_PPC64) { 265 uint64_t value; 266 if (read_target_8(proc, addr, &value) < 0) { 267 fprintf(stderr, 268 "dynamic .opd translation of %p: %s\n", 269 addr, strerror(errno)); 270 return -1; 271 } 272 /* XXX The double cast should be removed when 273 * arch_addr_t becomes integral type. */ 274 *ret = (arch_addr_t)(uintptr_t)value; 275 return 0; 276 } 277 278 *ret = addr; 279 return 0; 280} 281 282int 283arch_translate_address(struct ltelf *lte, 284 arch_addr_t addr, arch_addr_t *ret) 285{ 286 if (lte->ehdr.e_machine == EM_PPC64) { 287 /* XXX The double cast should be removed when 288 * arch_addr_t becomes integral type. */ 289 GElf_Xword offset 290 = (GElf_Addr)(uintptr_t)addr - lte->arch.opd_base; 291 uint64_t value; 292 if (elf_read_u64(lte->arch.opd_data, offset, &value) < 0) { 293 fprintf(stderr, "static .opd translation of %p: %s\n", 294 addr, elf_errmsg(-1)); 295 return -1; 296 } 297 *ret = (arch_addr_t)(uintptr_t)(value + lte->bias); 298 return 0; 299 } 300 301 *ret = addr; 302 return 0; 303} 304 305static int 306load_opd_data(struct ltelf *lte, struct library *lib) 307{ 308 Elf_Scn *sec; 309 GElf_Shdr shdr; 310 if (elf_get_section_named(lte, ".opd", &sec, &shdr) < 0) { 311 fail: 312 fprintf(stderr, "couldn't find .opd data\n"); 313 return -1; 314 } 315 316 lte->arch.opd_data = elf_rawdata(sec, NULL); 317 if (lte->arch.opd_data == NULL) 318 goto fail; 319 320 lte->arch.opd_base = shdr.sh_addr + lte->bias; 321 lte->arch.opd_size = shdr.sh_size; 322 323 return 0; 324} 325 326void * 327sym2addr(struct process *proc, struct library_symbol *sym) 328{ 329 return sym->enter_addr; 330} 331 332static GElf_Addr 333get_glink_vma(struct ltelf *lte, GElf_Addr ppcgot, Elf_Data *plt_data) 334{ 335 Elf_Scn *ppcgot_sec = NULL; 336 GElf_Shdr ppcgot_shdr; 337 if (ppcgot != 0 338 && elf_get_section_covering(lte, ppcgot, 339 &ppcgot_sec, &ppcgot_shdr) < 0) 340 fprintf(stderr, 341 "DT_PPC_GOT=%#"PRIx64", but no such section found\n", 342 ppcgot); 343 344 if (ppcgot_sec != NULL) { 345 Elf_Data *data = elf_loaddata(ppcgot_sec, &ppcgot_shdr); 346 if (data == NULL || data->d_size < 8 ) { 347 fprintf(stderr, "couldn't read GOT data\n"); 348 } else { 349 // where PPCGOT begins in .got 350 size_t offset = ppcgot - ppcgot_shdr.sh_addr; 351 assert(offset % 4 == 0); 352 uint32_t glink_vma; 353 if (elf_read_u32(data, offset + 4, &glink_vma) < 0) { 354 fprintf(stderr, "couldn't read glink VMA" 355 " address at %zd@GOT\n", offset); 356 return 0; 357 } 358 if (glink_vma != 0) { 359 debug(1, "PPC GOT glink_vma address: %#" PRIx32, 360 glink_vma); 361 return (GElf_Addr)glink_vma; 362 } 363 } 364 } 365 366 if (plt_data != NULL) { 367 uint32_t glink_vma; 368 if (elf_read_u32(plt_data, 0, &glink_vma) < 0) { 369 fprintf(stderr, "couldn't read glink VMA address\n"); 370 return 0; 371 } 372 debug(1, ".plt glink_vma address: %#" PRIx32, glink_vma); 373 return (GElf_Addr)glink_vma; 374 } 375 376 return 0; 377} 378 379static int 380load_dynamic_entry(struct ltelf *lte, int tag, GElf_Addr *valuep) 381{ 382 Elf_Scn *scn; 383 GElf_Shdr shdr; 384 if (elf_get_section_type(lte, SHT_DYNAMIC, &scn, &shdr) < 0 385 || scn == NULL) { 386 fail: 387 fprintf(stderr, "Couldn't get SHT_DYNAMIC: %s\n", 388 elf_errmsg(-1)); 389 return -1; 390 } 391 392 Elf_Data *data = elf_loaddata(scn, &shdr); 393 if (data == NULL) 394 goto fail; 395 396 size_t j; 397 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) { 398 GElf_Dyn dyn; 399 if (gelf_getdyn(data, j, &dyn) == NULL) 400 goto fail; 401 402 if(dyn.d_tag == tag) { 403 *valuep = dyn.d_un.d_ptr; 404 return 0; 405 } 406 } 407 408 return -1; 409} 410 411static int 412nonzero_data(Elf_Data *data) 413{ 414 /* We are not supposed to get here if there's no PLT. */ 415 assert(data != NULL); 416 417 unsigned char *buf = data->d_buf; 418 if (buf == NULL) 419 return 0; 420 421 size_t i; 422 for (i = 0; i < data->d_size; ++i) 423 if (buf[i] != 0) 424 return 1; 425 return 0; 426} 427 428int 429arch_elf_init(struct ltelf *lte, struct library *lib) 430{ 431 if (lte->ehdr.e_machine == EM_PPC64 432 && load_opd_data(lte, lib) < 0) 433 return -1; 434 435 lte->arch.secure_plt = !(lte->plt_flags & SHF_EXECINSTR); 436 437 /* For PPC32 BSS, it is important whether the binary was 438 * prelinked. If .plt section is NODATA, or if it contains 439 * zeroes, then this library is not prelinked, and we need to 440 * delay breakpoints. */ 441 if (lte->ehdr.e_machine == EM_PPC && !lte->arch.secure_plt) 442 lib->arch.bss_plt_prelinked = nonzero_data(lte->plt_data); 443 else 444 /* For cases where it's irrelevant, initialize the 445 * value to something conspicuous. */ 446 lib->arch.bss_plt_prelinked = -1; 447 448 if (lte->ehdr.e_machine == EM_PPC && lte->arch.secure_plt) { 449 GElf_Addr ppcgot; 450 if (load_dynamic_entry(lte, DT_PPC_GOT, &ppcgot) < 0) { 451 fprintf(stderr, "couldn't find DT_PPC_GOT\n"); 452 return -1; 453 } 454 GElf_Addr glink_vma = get_glink_vma(lte, ppcgot, lte->plt_data); 455 456 assert(lte->relplt_size % 12 == 0); 457 size_t count = lte->relplt_size / 12; // size of RELA entry 458 lte->arch.plt_stub_vma = glink_vma 459 - (GElf_Addr)count * PPC_PLT_STUB_SIZE; 460 debug(1, "stub_vma is %#" PRIx64, lte->arch.plt_stub_vma); 461 462 } else if (lte->ehdr.e_machine == EM_PPC64) { 463 GElf_Addr glink_vma; 464 if (load_dynamic_entry(lte, DT_PPC64_GLINK, &glink_vma) < 0) { 465 fprintf(stderr, "couldn't find DT_PPC64_GLINK\n"); 466 return -1; 467 } 468 469 /* The first glink stub starts at offset 32. */ 470 lte->arch.plt_stub_vma = glink_vma + 32; 471 472 } else { 473 /* By exhaustion--PPC32 BSS. */ 474 if (load_dynamic_entry(lte, DT_PLTGOT, 475 &lib->arch.pltgot_addr) < 0) { 476 fprintf(stderr, "couldn't find DT_PLTGOT\n"); 477 return -1; 478 } 479 } 480 481 /* On PPC64, look for stub symbols in symbol table. These are 482 * called: xxxxxxxx.plt_call.callee_name@version+addend. */ 483 if (lte->ehdr.e_machine == EM_PPC64 484 && lte->symtab != NULL && lte->strtab != NULL) { 485 486 /* N.B. We can't simply skip the symbols that we fail 487 * to read or malloc. There may be more than one stub 488 * per symbol name, and if we failed in one but 489 * succeeded in another, the PLT enabling code would 490 * have no way to tell that something is missing. We 491 * could work around that, of course, but it doesn't 492 * seem worth the trouble. So if anything fails, we 493 * just pretend that we don't have stub symbols at 494 * all, as if the binary is stripped. */ 495 496 size_t i; 497 for (i = 0; i < lte->symtab_count; ++i) { 498 GElf_Sym sym; 499 if (gelf_getsym(lte->symtab, i, &sym) == NULL) { 500 struct library_symbol *sym, *next; 501 fail: 502 for (sym = lte->arch.stubs; sym != NULL; ) { 503 next = sym->next; 504 library_symbol_destroy(sym); 505 free(sym); 506 sym = next; 507 } 508 lte->arch.stubs = NULL; 509 break; 510 } 511 512 const char *name = lte->strtab + sym.st_name; 513 514#define STUBN ".plt_call." 515 if ((name = strstr(name, STUBN)) == NULL) 516 continue; 517 name += sizeof(STUBN) - 1; 518#undef STUBN 519 520 size_t len; 521 const char *ver = strchr(name, '@'); 522 if (ver != NULL) { 523 len = ver - name; 524 525 } else { 526 /* If there is "+" at all, check that 527 * the symbol name ends in "+0". */ 528 const char *add = strrchr(name, '+'); 529 if (add != NULL) { 530 assert(strcmp(add, "+0") == 0); 531 len = add - name; 532 } else { 533 len = strlen(name); 534 } 535 } 536 537 char *sym_name = strndup(name, len); 538 struct library_symbol *libsym = malloc(sizeof(*libsym)); 539 if (sym_name == NULL || libsym == NULL) { 540 fail2: 541 free(sym_name); 542 free(libsym); 543 goto fail; 544 } 545 546 /* XXX The double cast should be removed when 547 * arch_addr_t becomes integral type. */ 548 arch_addr_t addr = (arch_addr_t) 549 (uintptr_t)sym.st_value + lte->bias; 550 if (library_symbol_init(libsym, addr, sym_name, 1, 551 LS_TOPLT_EXEC) < 0) 552 goto fail2; 553 libsym->arch.type = PPC64_PLT_STUB; 554 libsym->next = lte->arch.stubs; 555 lte->arch.stubs = libsym; 556 } 557 } 558 559 return 0; 560} 561 562static int 563read_plt_slot_value(struct process *proc, GElf_Addr addr, GElf_Addr *valp) 564{ 565 /* On PPC64, we read from .plt, which contains 8 byte 566 * addresses. On PPC32 we read from .plt, which contains 4 567 * byte instructions, but the PLT is two instructions, and 568 * either can change. */ 569 uint64_t l; 570 /* XXX double cast. */ 571 if (read_target_8(proc, (arch_addr_t)(uintptr_t)addr, &l) < 0) { 572 fprintf(stderr, "ptrace .plt slot value @%#" PRIx64": %s\n", 573 addr, strerror(errno)); 574 return -1; 575 } 576 577 *valp = (GElf_Addr)l; 578 return 0; 579} 580 581static int 582unresolve_plt_slot(struct process *proc, GElf_Addr addr, GElf_Addr value) 583{ 584 /* We only modify plt_entry[0], which holds the resolved 585 * address of the routine. We keep the TOC and environment 586 * pointers intact. Hence the only adjustment that we need to 587 * do is to IP. */ 588 if (ptrace(PTRACE_POKETEXT, proc->pid, addr, value) < 0) { 589 fprintf(stderr, "failed to unresolve .plt slot: %s\n", 590 strerror(errno)); 591 return -1; 592 } 593 return 0; 594} 595 596enum plt_status 597arch_elf_add_plt_entry(struct process *proc, struct ltelf *lte, 598 const char *a_name, GElf_Rela *rela, size_t ndx, 599 struct library_symbol **ret) 600{ 601 if (lte->ehdr.e_machine == EM_PPC) { 602 if (lte->arch.secure_plt) 603 return plt_default; 604 605 struct library_symbol *libsym = NULL; 606 if (default_elf_add_plt_entry(proc, lte, a_name, rela, ndx, 607 &libsym) < 0) 608 return plt_fail; 609 610 /* On PPC32 with BSS PLT, delay the symbol until 611 * dynamic linker is done. */ 612 assert(!libsym->delayed); 613 libsym->delayed = 1; 614 615 *ret = libsym; 616 return plt_ok; 617 } 618 619 /* PPC64. If we have stubs, we return a chain of breakpoint 620 * sites, one for each stub that corresponds to this PLT 621 * entry. */ 622 struct library_symbol *chain = NULL; 623 struct library_symbol **symp; 624 for (symp = <e->arch.stubs; *symp != NULL; ) { 625 struct library_symbol *sym = *symp; 626 if (strcmp(sym->name, a_name) != 0) { 627 symp = &(*symp)->next; 628 continue; 629 } 630 631 /* Re-chain the symbol from stubs to CHAIN. */ 632 *symp = sym->next; 633 sym->next = chain; 634 chain = sym; 635 } 636 637 if (chain != NULL) { 638 *ret = chain; 639 return plt_ok; 640 } 641 642 /* We don't have stub symbols. Find corresponding .plt slot, 643 * and check whether it contains the corresponding PLT address 644 * (or 0 if the dynamic linker hasn't run yet). N.B. we don't 645 * want read this from ELF file, but from process image. That 646 * makes a difference if we are attaching to a running 647 * process. */ 648 649 GElf_Addr plt_entry_addr = arch_plt_sym_val(lte, ndx, rela); 650 GElf_Addr plt_slot_addr = rela->r_offset; 651 assert(plt_slot_addr >= lte->plt_addr 652 || plt_slot_addr < lte->plt_addr + lte->plt_size); 653 654 GElf_Addr plt_slot_value; 655 if (read_plt_slot_value(proc, plt_slot_addr, &plt_slot_value) < 0) 656 return plt_fail; 657 658 char *name = strdup(a_name); 659 struct library_symbol *libsym = malloc(sizeof(*libsym)); 660 if (name == NULL || libsym == NULL) { 661 fprintf(stderr, "allocation for .plt slot: %s\n", 662 strerror(errno)); 663 fail: 664 free(name); 665 free(libsym); 666 return plt_fail; 667 } 668 669 /* XXX The double cast should be removed when 670 * arch_addr_t becomes integral type. */ 671 if (library_symbol_init(libsym, 672 (arch_addr_t)(uintptr_t)plt_entry_addr, 673 name, 1, LS_TOPLT_EXEC) < 0) 674 goto fail; 675 libsym->arch.plt_slot_addr = plt_slot_addr; 676 677 if (plt_slot_value == plt_entry_addr || plt_slot_value == 0) { 678 libsym->arch.type = PPC_PLT_UNRESOLVED; 679 libsym->arch.resolved_value = plt_entry_addr; 680 681 } else { 682 /* Unresolve the .plt slot. If the binary was 683 * prelinked, this makes the code invalid, because in 684 * case of prelinked binary, the dynamic linker 685 * doesn't update .plt[0] and .plt[1] with addresses 686 * of the resover. But we don't care, we will never 687 * need to enter the resolver. That just means that 688 * we have to un-un-resolve this back before we 689 * detach. */ 690 691 if (unresolve_plt_slot(proc, plt_slot_addr, plt_entry_addr) < 0) { 692 library_symbol_destroy(libsym); 693 goto fail; 694 } 695 mark_as_resolved(libsym, plt_slot_value); 696 } 697 698 *ret = libsym; 699 return plt_ok; 700} 701 702void 703arch_elf_destroy(struct ltelf *lte) 704{ 705 struct library_symbol *sym; 706 for (sym = lte->arch.stubs; sym != NULL; ) { 707 struct library_symbol *next = sym->next; 708 library_symbol_destroy(sym); 709 free(sym); 710 sym = next; 711 } 712} 713 714static void 715dl_plt_update_bp_on_hit(struct breakpoint *bp, struct process *proc) 716{ 717 debug(DEBUG_PROCESS, "pid=%d dl_plt_update_bp_on_hit %s(%p)", 718 proc->pid, breakpoint_name(bp), bp->addr); 719 struct process_stopping_handler *self = proc->arch.handler; 720 assert(self != NULL); 721 722 struct library_symbol *libsym = self->breakpoint_being_enabled->libsym; 723 GElf_Addr value; 724 if (read_plt_slot_value(proc, libsym->arch.plt_slot_addr, &value) < 0) 725 return; 726 727 /* On PPC64, we rewrite the slot value. */ 728 if (proc->e_machine == EM_PPC64) 729 unresolve_plt_slot(proc, libsym->arch.plt_slot_addr, 730 libsym->arch.resolved_value); 731 /* We mark the breakpoint as resolved on both arches. */ 732 mark_as_resolved(libsym, value); 733 734 /* cb_on_all_stopped looks if HANDLER is set to NULL as a way 735 * to check that this was run. It's an error if it 736 * wasn't. */ 737 proc->arch.handler = NULL; 738 739 breakpoint_turn_off(bp, proc); 740} 741 742static void 743cb_on_all_stopped(struct process_stopping_handler *self) 744{ 745 /* Put that in for dl_plt_update_bp_on_hit to see. */ 746 assert(self->task_enabling_breakpoint->arch.handler == NULL); 747 self->task_enabling_breakpoint->arch.handler = self; 748 749 linux_ptrace_disable_and_continue(self); 750} 751 752static enum callback_status 753cb_keep_stepping_p(struct process_stopping_handler *self) 754{ 755 struct process *proc = self->task_enabling_breakpoint; 756 struct library_symbol *libsym = self->breakpoint_being_enabled->libsym; 757 758 GElf_Addr value; 759 if (read_plt_slot_value(proc, libsym->arch.plt_slot_addr, &value) < 0) 760 return CBS_FAIL; 761 762 /* In UNRESOLVED state, the RESOLVED_VALUE in fact contains 763 * the PLT entry value. */ 764 if (value == libsym->arch.resolved_value) 765 return CBS_CONT; 766 767 debug(DEBUG_PROCESS, "pid=%d PLT got resolved to value %#"PRIx64, 768 proc->pid, value); 769 770 /* The .plt slot got resolved! We can migrate the breakpoint 771 * to RESOLVED and stop single-stepping. */ 772 if (proc->e_machine == EM_PPC64 773 && unresolve_plt_slot(proc, libsym->arch.plt_slot_addr, 774 libsym->arch.resolved_value) < 0) 775 return CBS_FAIL; 776 777 /* Resolving on PPC64 consists of overwriting a doubleword in 778 * .plt. That doubleword is than read back by a stub, and 779 * jumped on. Hopefully we can assume that double word update 780 * is done on a single place only, as it contains a final 781 * address. We still need to look around for any sync 782 * instruction, but essentially it is safe to optimize away 783 * the single stepping next time and install a post-update 784 * breakpoint. 785 * 786 * The situation on PPC32 BSS is more complicated. The 787 * dynamic linker here updates potentially several 788 * instructions (XXX currently we assume two) and the rules 789 * are more complicated. Sometimes it's enough to adjust just 790 * one of the addresses--the logic for generating optimal 791 * dispatch depends on relative addresses of the .plt entry 792 * and the jump destination. We can't assume that the some 793 * instruction block does the update every time. So on PPC32, 794 * we turn the optimization off and just step through it each 795 * time. */ 796 if (proc->e_machine == EM_PPC) 797 goto done; 798 799 /* Install breakpoint to the address where the change takes 800 * place. If we fail, then that just means that we'll have to 801 * singlestep the next time around as well. */ 802 struct process *leader = proc->leader; 803 if (leader == NULL || leader->arch.dl_plt_update_bp != NULL) 804 goto done; 805 806 /* We need to install to the next instruction. ADDR points to 807 * a store instruction, so moving the breakpoint one 808 * instruction forward is safe. */ 809 arch_addr_t addr = get_instruction_pointer(proc) + 4; 810 leader->arch.dl_plt_update_bp = insert_breakpoint(proc, addr, NULL); 811 if (leader->arch.dl_plt_update_bp == NULL) 812 goto done; 813 814 static struct bp_callbacks dl_plt_update_cbs = { 815 .on_hit = dl_plt_update_bp_on_hit, 816 }; 817 leader->arch.dl_plt_update_bp->cbs = &dl_plt_update_cbs; 818 819 /* Turn it off for now. We will turn it on again when we hit 820 * the PLT entry that needs this. */ 821 breakpoint_turn_off(leader->arch.dl_plt_update_bp, proc); 822 823done: 824 mark_as_resolved(libsym, value); 825 826 return CBS_STOP; 827} 828 829static void 830jump_to_entry_point(struct process *proc, struct breakpoint *bp) 831{ 832 /* XXX The double cast should be removed when 833 * arch_addr_t becomes integral type. */ 834 arch_addr_t rv = (arch_addr_t) 835 (uintptr_t)bp->libsym->arch.resolved_value; 836 set_instruction_pointer(proc, rv); 837} 838 839static void 840ppc_plt_bp_continue(struct breakpoint *bp, struct process *proc) 841{ 842 switch (bp->libsym->arch.type) { 843 struct process *leader; 844 void (*on_all_stopped)(struct process_stopping_handler *); 845 enum callback_status (*keep_stepping_p) 846 (struct process_stopping_handler *); 847 848 case PPC_DEFAULT: 849 assert(proc->e_machine == EM_PPC); 850 assert(bp->libsym != NULL); 851 assert(bp->libsym->lib->arch.bss_plt_prelinked == 0); 852 /* Fall through. */ 853 854 case PPC_PLT_UNRESOLVED: 855 on_all_stopped = NULL; 856 keep_stepping_p = NULL; 857 leader = proc->leader; 858 859 if (leader != NULL && leader->arch.dl_plt_update_bp != NULL 860 && breakpoint_turn_on(leader->arch.dl_plt_update_bp, 861 proc) >= 0) 862 on_all_stopped = cb_on_all_stopped; 863 else 864 keep_stepping_p = cb_keep_stepping_p; 865 866 if (process_install_stopping_handler 867 (proc, bp, on_all_stopped, keep_stepping_p, NULL) < 0) { 868 fprintf(stderr, "ppc_plt_bp_continue: " 869 "couldn't install event handler\n"); 870 continue_after_breakpoint(proc, bp); 871 } 872 return; 873 874 case PPC_PLT_RESOLVED: 875 if (proc->e_machine == EM_PPC) { 876 continue_after_breakpoint(proc, bp); 877 return; 878 } 879 880 jump_to_entry_point(proc, bp); 881 continue_process(proc->pid); 882 return; 883 884 case PPC64_PLT_STUB: 885 /* These should never hit here. */ 886 break; 887 } 888 889 assert(bp->libsym->arch.type != bp->libsym->arch.type); 890 abort(); 891} 892 893/* When a process is in a PLT stub, it may have already read the data 894 * in .plt that we changed. If we detach now, it will jump to PLT 895 * entry and continue to the dynamic linker, where it will SIGSEGV, 896 * because zeroth .plt slot is not filled in prelinked binaries, and 897 * the dynamic linker needs that data. Moreover, the process may 898 * actually have hit the breakpoint already. This functions tries to 899 * detect both cases and do any fix-ups necessary to mend this 900 * situation. */ 901static enum callback_status 902detach_task_cb(struct process *task, void *data) 903{ 904 struct breakpoint *bp = data; 905 906 if (get_instruction_pointer(task) == bp->addr) { 907 debug(DEBUG_PROCESS, "%d at %p, which is PLT slot", 908 task->pid, bp->addr); 909 jump_to_entry_point(task, bp); 910 return CBS_CONT; 911 } 912 913 /* XXX There's still a window of several instructions where we 914 * might catch the task inside a stub such that it has already 915 * read destination address from .plt, but hasn't jumped yet, 916 * thus avoiding the breakpoint. */ 917 918 return CBS_CONT; 919} 920 921static void 922ppc_plt_bp_retract(struct breakpoint *bp, struct process *proc) 923{ 924 /* On PPC64, we rewrite .plt with PLT entry addresses. This 925 * needs to be undone. Unfortunately, the program may have 926 * made decisions based on that value */ 927 if (proc->e_machine == EM_PPC64 928 && bp->libsym != NULL 929 && bp->libsym->arch.type == PPC_PLT_RESOLVED) { 930 each_task(proc->leader, NULL, detach_task_cb, bp); 931 unresolve_plt_slot(proc, bp->libsym->arch.plt_slot_addr, 932 bp->libsym->arch.resolved_value); 933 } 934} 935 936void 937arch_library_init(struct library *lib) 938{ 939} 940 941void 942arch_library_destroy(struct library *lib) 943{ 944} 945 946void 947arch_library_clone(struct library *retp, struct library *lib) 948{ 949} 950 951int 952arch_library_symbol_init(struct library_symbol *libsym) 953{ 954 /* We set type explicitly in the code above, where we have the 955 * necessary context. This is for calls from ltrace-elf.c and 956 * such. */ 957 libsym->arch.type = PPC_DEFAULT; 958 return 0; 959} 960 961void 962arch_library_symbol_destroy(struct library_symbol *libsym) 963{ 964} 965 966int 967arch_library_symbol_clone(struct library_symbol *retp, 968 struct library_symbol *libsym) 969{ 970 retp->arch = libsym->arch; 971 return 0; 972} 973 974/* For some symbol types, we need to set up custom callbacks. XXX we 975 * don't need PROC here, we can store the data in BP if it is of 976 * interest to us. */ 977int 978arch_breakpoint_init(struct process *proc, struct breakpoint *bp) 979{ 980 /* Artificial and entry-point breakpoints are plain. */ 981 if (bp->libsym == NULL || bp->libsym->plt_type != LS_TOPLT_EXEC) 982 return 0; 983 984 /* On PPC, secure PLT and prelinked BSS PLT are plain. */ 985 if (proc->e_machine == EM_PPC 986 && bp->libsym->lib->arch.bss_plt_prelinked != 0) 987 return 0; 988 989 /* On PPC64, stub PLT breakpoints are plain. */ 990 if (proc->e_machine == EM_PPC64 991 && bp->libsym->arch.type == PPC64_PLT_STUB) 992 return 0; 993 994 static struct bp_callbacks cbs = { 995 .on_continue = ppc_plt_bp_continue, 996 .on_retract = ppc_plt_bp_retract, 997 }; 998 breakpoint_set_callbacks(bp, &cbs); 999 return 0; 1000} 1001 1002void 1003arch_breakpoint_destroy(struct breakpoint *bp) 1004{ 1005} 1006 1007int 1008arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp) 1009{ 1010 retp->arch = sbp->arch; 1011 return 0; 1012} 1013 1014int 1015arch_process_init(struct process *proc) 1016{ 1017 proc->arch.dl_plt_update_bp = NULL; 1018 proc->arch.handler = NULL; 1019 return 0; 1020} 1021 1022void 1023arch_process_destroy(struct process *proc) 1024{ 1025} 1026 1027int 1028arch_process_clone(struct process *retp, struct process *proc) 1029{ 1030 retp->arch = proc->arch; 1031 return 0; 1032} 1033 1034int 1035arch_process_exec(struct process *proc) 1036{ 1037 return arch_process_init(proc); 1038} 1039