2 * This file is part of ltrace.
3 * Copyright (C) 2012,2013 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2004,2008,2009 Juan Cespedes
5 * Copyright (C) 2006 Paul Gilliam
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.
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.
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
24 #include <sys/ptrace.h>
35 #include "breakpoint.h"
36 #include "linux-gnu/trace.h"
39 /* There are two PLT types on 32-bit PPC: old-style, BSS PLT, and
40 * new-style "secure" PLT. We can tell one from the other by the
41 * flags on the .plt section. If it's +X (executable), it's BSS PLT,
42 * otherwise it's secure.
44 * BSS PLT works the same way as most architectures: the .plt section
45 * contains trampolines and we put breakpoints to those. If not
46 * prelinked, .plt contains zeroes, and dynamic linker fills in the
47 * initial set of trampolines, which means that we need to delay
48 * enabling breakpoints until after binary entry point is hit.
49 * Additionally, after first call, dynamic linker updates .plt with
50 * branch to resolved address. That means that on first hit, we must
51 * do something similar to the PPC64 gambit described below.
53 * With secure PLT, the .plt section doesn't contain instructions but
54 * addresses. The real PLT table is stored in .text. Addresses of
55 * those PLT entries can be computed, and apart from the fact that
56 * they are in .text, they are ordinary PLT entries.
58 * 64-bit PPC is more involved. Program linker creates for each
59 * library call a _stub_ symbol named xxxxxxxx.plt_call.<callee>
60 * (where xxxxxxxx is a hexadecimal number). That stub does the call
61 * dispatch: it loads an address of a function to call from the
62 * section .plt, and branches. PLT entries themselves are essentially
63 * a curried call to the resolver. When the symbol is resolved, the
64 * resolver updates the value stored in .plt, and the next time
65 * around, the stub calls the library function directly. So we make
66 * at most one trip (none if the binary is prelinked) through each PLT
67 * entry, and correspondingly that is useless as a breakpoint site.
69 * Note the three confusing terms: stubs (that play the role of PLT
70 * entries), PLT entries, .plt section.
72 * We first check symbol tables and see if we happen to have stub
73 * symbols available. If yes we just put breakpoints to those, and
74 * treat them as usual breakpoints. The only tricky part is realizing
75 * that there can be more than one breakpoint per symbol.
77 * The case that we don't have the stub symbols available is harder.
78 * The following scheme uses two kinds of PLT breakpoints: unresolved
79 * and resolved (to some address). When the process starts (or when
80 * we attach), we distribute unresolved PLT breakpoints to the PLT
81 * entries (not stubs). Then we look in .plt, and for each entry
82 * whose value is different than the corresponding PLT entry address,
83 * we assume it was already resolved, and convert the breakpoint to
84 * resolved. We also rewrite the resolved value in .plt back to the
87 * When a PLT entry hits a resolved breakpoint (which happens because
88 * we rewrite .plt with the original unresolved addresses), we move
89 * the instruction pointer to the corresponding address and continue
90 * the process as if nothing happened.
92 * When unresolved PLT entry is called for the first time, we need to
93 * catch the new value that the resolver will write to a .plt slot.
94 * We also need to prevent another thread from racing through and
95 * taking the branch without ltrace noticing. So when unresolved PLT
96 * entry hits, we have to stop all threads. We then single-step
97 * through the resolver, until the .plt slot changes. When it does,
98 * we treat it the same way as above: convert the PLT breakpoint to
99 * resolved, and rewrite the .plt value back to PLT address. We then
100 * start all threads again.
102 * As an optimization, we remember the address where the address was
103 * resolved, and put a breakpoint there. The next time around (when
104 * the next PLT entry is to be resolved), instead of single-stepping
105 * through half the dynamic linker, we just let the thread run and hit
106 * this breakpoint. When it hits, we know the PLT entry was resolved.
108 * Another twist comes from tracing slots corresponding to
109 * R_PPC64_JMP_IREL relocations. These have no dedicated PLT entry.
110 * The calls are done directly from stubs, and the .plt entry
111 * (actually .iplt entry, these live in a special section) is resolved
112 * in advance before the binary starts. Because there's no PLT entry,
113 * we put the PLT breakpoints directly to the IFUNC resolver code, and
114 * then would like them to behave like ordinary PLT slots, including
115 * catching the point where these get resolved to unresolve them. So
116 * for the first call (which is the actual resolver call), we pretend
117 * that this breakpoint is artificial and has no associated symbol,
118 * and turn it on fully only after the first hit. Ideally we would
119 * trace that first call as well, but then the stepper, which tries to
120 * catch the point where the slot is resolved, would hit the return
121 * breakpoint and that's not currently handled well.
123 * On PPC32 with secure PLT, the address of IFUNC symbols in main
124 * binary actually isn't of the resolver, but of a PLT slot. We
125 * therefore have to locate the corresponding PLT relocation (which is
126 * of type R_PPC_IRELATIVE) and request that it be traced. The addend
127 * of that relocation is an address of resolver, and we request
128 * tracing of the xyz.IFUNC symbol there.
130 * XXX TODO If we have hardware watch point, we might put a read watch
131 * on .plt slot, and discover the offenders this way. I don't know
132 * the details, but I assume at most a handful (like, one or two, if
133 * available at all) addresses may be watched at a time, and thus this
134 * would be used as an amendment of the above rather than full-on
135 * solution to PLT tracing on PPC.
138 #define PPC_PLT_STUB_SIZE 16
139 #define PPC64_PLT_STUB_SIZE 8 //xxx
152 mark_as_resolved(struct library_symbol *libsym, GElf_Addr value)
154 libsym->arch.type = PPC_PLT_RESOLVED;
155 libsym->arch.resolved_value = value;
159 ppc32_delayed_symbol(struct library_symbol *libsym)
161 /* arch_dynlink_done is called on attach as well. In that
162 * case some slots will have been resolved already.
163 * Unresolved PLT looks like this:
165 * <sleep@plt>: li r11,0
166 * <sleep@plt+4>: b "resolve"
168 * "resolve" is another address in PLTGOT (the same block that
169 * all the PLT slots are it). When resolved, it looks either
172 * <sleep@plt>: b 0xfea88d0 <sleep>
174 * Which is easy to detect. It can also look this way:
176 * <sleep@plt>: li r11,0
177 * <sleep@plt+4>: b "dispatch"
179 * The "dispatch" address lies in PLTGOT as well. In current
180 * GNU toolchain, "dispatch" address is the same as PLTGOT
181 * address. We rely on this to figure out whether the address
182 * is resolved or not. */
184 uint32_t insn1 = libsym->arch.resolved_value >> 32;
185 uint32_t insn2 = (uint32_t) libsym->arch.resolved_value;
186 if ((insn1 & BRANCH_MASK) == B_INSN
187 || ((insn2 & BRANCH_MASK) == B_INSN
188 /* XXX double cast */
189 && (ppc_branch_dest(libsym->enter_addr + 4, insn2)
190 == (arch_addr_t) (long) libsym->lib->arch.pltgot_addr)))
192 mark_as_resolved(libsym, libsym->arch.resolved_value);
197 arch_dynlink_done(struct process *proc)
199 /* We may need to activate delayed symbols. */
200 struct library_symbol *libsym = NULL;
201 while ((libsym = proc_each_symbol(proc, libsym,
202 library_symbol_delayed_cb, NULL))) {
203 if (proc_read_64(proc, libsym->enter_addr,
204 &libsym->arch.resolved_value) < 0) {
206 "couldn't read PLT value for %s(%p): %s\n",
207 libsym->name, libsym->enter_addr,
212 if (proc->e_machine == EM_PPC)
213 ppc32_delayed_symbol(libsym);
215 if (proc_activate_delayed_symbol(proc, libsym) < 0)
218 if (proc->e_machine == EM_PPC)
219 /* XXX double cast */
220 libsym->arch.plt_slot_addr
221 = (GElf_Addr) (uintptr_t) libsym->enter_addr;
226 reloc_is_irelative(int machine, GElf_Rela *rela)
228 bool irelative = false;
229 if (machine == EM_PPC64) {
230 #ifdef R_PPC64_JMP_IREL
231 irelative = GELF_R_TYPE(rela->r_info) == R_PPC64_JMP_IREL;
234 assert(machine == EM_PPC);
235 #ifdef R_PPC_IRELATIVE
236 irelative = GELF_R_TYPE(rela->r_info) == R_PPC_IRELATIVE;
243 arch_plt_sym_val(struct ltelf *lte, size_t ndx, GElf_Rela *rela)
245 if (lte->ehdr.e_machine == EM_PPC && lte->arch.secure_plt) {
246 assert(lte->arch.plt_stub_vma != 0);
247 return lte->arch.plt_stub_vma + PPC_PLT_STUB_SIZE * ndx;
249 } else if (lte->ehdr.e_machine == EM_PPC) {
250 return rela->r_offset;
252 /* Beyond this point, we are on PPC64, but don't have stub
255 } else if (reloc_is_irelative(lte->ehdr.e_machine, rela)) {
257 /* Put JMP_IREL breakpoint to resolver, since there's
258 * no dedicated PLT entry. */
260 assert(rela->r_addend != 0);
261 /* XXX double cast */
262 arch_addr_t res_addr = (arch_addr_t) (uintptr_t) rela->r_addend;
263 if (arch_translate_address(lte, res_addr, &res_addr) < 0) {
264 fprintf(stderr, "Couldn't OPD-translate IRELATIVE "
265 "resolver address.\n");
268 /* XXX double cast */
269 return (GElf_Addr) (uintptr_t) res_addr;
272 /* We put brakpoints to PLT entries the same as the
273 * PPC32 secure PLT case does. */
274 assert(lte->arch.plt_stub_vma != 0);
275 return lte->arch.plt_stub_vma + PPC64_PLT_STUB_SIZE * ndx;
279 /* This entry point is called when ltelf is not available
280 * anymore--during runtime. At that point we don't have to concern
281 * ourselves with bias, as the values in OPD have been resolved
284 arch_translate_address_dyn(struct process *proc,
285 arch_addr_t addr, arch_addr_t *ret)
287 if (proc->e_machine == EM_PPC64) {
289 if (proc_read_64(proc, addr, &value) < 0) {
291 "dynamic .opd translation of %p: %s\n",
292 addr, strerror(errno));
295 /* XXX The double cast should be removed when
296 * arch_addr_t becomes integral type. */
297 *ret = (arch_addr_t)(uintptr_t)value;
306 arch_translate_address(struct ltelf *lte,
307 arch_addr_t addr, arch_addr_t *ret)
309 if (lte->ehdr.e_machine == EM_PPC64) {
310 /* XXX The double cast should be removed when
311 * arch_addr_t becomes integral type. */
313 = (GElf_Addr)(uintptr_t)addr - lte->arch.opd_base;
315 if (elf_read_u64(lte->arch.opd_data, offset, &value) < 0) {
316 fprintf(stderr, "static .opd translation of %p: %s\n",
317 addr, elf_errmsg(-1));
320 *ret = (arch_addr_t)(uintptr_t)(value + lte->bias);
329 load_opd_data(struct ltelf *lte, struct library *lib)
333 if (elf_get_section_named(lte, ".opd", &sec, &shdr) < 0
336 fprintf(stderr, "couldn't find .opd data\n");
340 lte->arch.opd_data = elf_rawdata(sec, NULL);
341 if (lte->arch.opd_data == NULL)
344 lte->arch.opd_base = shdr.sh_addr + lte->bias;
345 lte->arch.opd_size = shdr.sh_size;
351 sym2addr(struct process *proc, struct library_symbol *sym)
353 return sym->enter_addr;
357 get_glink_vma(struct ltelf *lte, GElf_Addr ppcgot, Elf_Data *plt_data)
359 Elf_Scn *ppcgot_sec = NULL;
360 GElf_Shdr ppcgot_shdr;
362 && (elf_get_section_covering(lte, ppcgot,
363 &ppcgot_sec, &ppcgot_shdr) < 0
364 || ppcgot_sec == NULL))
366 "DT_PPC_GOT=%#"PRIx64", but no such section found\n",
369 if (ppcgot_sec != NULL) {
370 Elf_Data *data = elf_loaddata(ppcgot_sec, &ppcgot_shdr);
371 if (data == NULL || data->d_size < 8 ) {
372 fprintf(stderr, "couldn't read GOT data\n");
374 // where PPCGOT begins in .got
375 size_t offset = ppcgot - ppcgot_shdr.sh_addr;
376 assert(offset % 4 == 0);
378 if (elf_read_u32(data, offset + 4, &glink_vma) < 0) {
379 fprintf(stderr, "couldn't read glink VMA"
380 " address at %zd@GOT\n", offset);
383 if (glink_vma != 0) {
384 debug(1, "PPC GOT glink_vma address: %#" PRIx32,
386 return (GElf_Addr)glink_vma;
391 if (plt_data != NULL) {
393 if (elf_read_u32(plt_data, 0, &glink_vma) < 0) {
394 fprintf(stderr, "couldn't read glink VMA address\n");
397 debug(1, ".plt glink_vma address: %#" PRIx32, glink_vma);
398 return (GElf_Addr)glink_vma;
405 nonzero_data(Elf_Data *data)
407 /* We are not supposed to get here if there's no PLT. */
408 assert(data != NULL);
410 unsigned char *buf = data->d_buf;
415 for (i = 0; i < data->d_size; ++i)
421 static enum callback_status
422 reloc_copy_if_irelative(GElf_Rela *rela, void *data)
424 struct ltelf *lte = data;
426 return CBS_STOP_IF(reloc_is_irelative(lte->ehdr.e_machine, rela)
427 && VECT_PUSHBACK(<e->plt_relocs, rela) < 0);
431 arch_elf_init(struct ltelf *lte, struct library *lib)
433 if (lte->ehdr.e_machine == EM_PPC64
434 && load_opd_data(lte, lib) < 0)
437 lte->arch.secure_plt = !(lte->plt_flags & SHF_EXECINSTR);
439 /* For PPC32 BSS, it is important whether the binary was
440 * prelinked. If .plt section is NODATA, or if it contains
441 * zeroes, then this library is not prelinked, and we need to
442 * delay breakpoints. */
443 if (lte->ehdr.e_machine == EM_PPC && !lte->arch.secure_plt)
444 lib->arch.bss_plt_prelinked = nonzero_data(lte->plt_data);
446 /* For cases where it's irrelevant, initialize the
447 * value to something conspicuous. */
448 lib->arch.bss_plt_prelinked = -1;
450 /* On PPC64 and PPC32 secure, IRELATIVE relocations actually
451 * relocate .iplt section, and as such are stored in .rela.dyn
452 * (where all non-PLT relocations are stored) instead of
453 * .rela.plt. Add these to lte->plt_relocs. */
455 GElf_Addr rela, relasz;
458 if ((lte->ehdr.e_machine == EM_PPC64 || lte->arch.secure_plt)
459 && elf_load_dynamic_entry(lte, DT_RELA, &rela) == 0
460 && elf_load_dynamic_entry(lte, DT_RELASZ, &relasz) == 0
461 && elf_get_section_covering(lte, rela, &rela_sec, &rela_shdr) == 0
462 && rela_sec != NULL) {
465 VECT_INIT(&v, GElf_Rela);
466 int ret = elf_read_relocs(lte, rela_sec, &rela_shdr, &v);
468 && VECT_EACH(&v, GElf_Rela, NULL,
469 reloc_copy_if_irelative, lte) != NULL)
472 VECT_DESTROY(&v, GElf_Rela, NULL, NULL);
478 if (lte->ehdr.e_machine == EM_PPC && lte->arch.secure_plt) {
480 if (elf_load_dynamic_entry(lte, DT_PPC_GOT, &ppcgot) < 0) {
481 fprintf(stderr, "couldn't find DT_PPC_GOT\n");
484 GElf_Addr glink_vma = get_glink_vma(lte, ppcgot, lte->plt_data);
486 size_t count = vect_size(<e->plt_relocs);
487 lte->arch.plt_stub_vma = glink_vma
488 - (GElf_Addr) count * PPC_PLT_STUB_SIZE;
489 debug(1, "stub_vma is %#" PRIx64, lte->arch.plt_stub_vma);
491 } else if (lte->ehdr.e_machine == EM_PPC64) {
493 if (elf_load_dynamic_entry(lte, DT_PPC64_GLINK,
495 fprintf(stderr, "couldn't find DT_PPC64_GLINK\n");
499 /* The first glink stub starts at offset 32. */
500 lte->arch.plt_stub_vma = glink_vma + 32;
503 /* By exhaustion--PPC32 BSS. */
504 if (elf_load_dynamic_entry(lte, DT_PLTGOT,
505 &lib->arch.pltgot_addr) < 0) {
506 fprintf(stderr, "couldn't find DT_PLTGOT\n");
511 /* On PPC64, look for stub symbols in symbol table. These are
512 * called: xxxxxxxx.plt_call.callee_name@version+addend. */
513 if (lte->ehdr.e_machine == EM_PPC64
514 && lte->symtab != NULL && lte->strtab != NULL) {
516 /* N.B. We can't simply skip the symbols that we fail
517 * to read or malloc. There may be more than one stub
518 * per symbol name, and if we failed in one but
519 * succeeded in another, the PLT enabling code would
520 * have no way to tell that something is missing. We
521 * could work around that, of course, but it doesn't
522 * seem worth the trouble. So if anything fails, we
523 * just pretend that we don't have stub symbols at
524 * all, as if the binary is stripped. */
527 for (i = 0; i < lte->symtab_count; ++i) {
529 if (gelf_getsym(lte->symtab, i, &sym) == NULL) {
530 struct library_symbol *sym, *next;
532 for (sym = lte->arch.stubs; sym != NULL; ) {
534 library_symbol_destroy(sym);
538 lte->arch.stubs = NULL;
542 const char *name = lte->strtab + sym.st_name;
544 #define STUBN ".plt_call."
545 if ((name = strstr(name, STUBN)) == NULL)
547 name += sizeof(STUBN) - 1;
551 const char *ver = strchr(name, '@');
556 /* If there is "+" at all, check that
557 * the symbol name ends in "+0". */
558 const char *add = strrchr(name, '+');
560 assert(strcmp(add, "+0") == 0);
567 char *sym_name = strndup(name, len);
568 struct library_symbol *libsym = malloc(sizeof(*libsym));
569 if (sym_name == NULL || libsym == NULL) {
576 /* XXX The double cast should be removed when
577 * arch_addr_t becomes integral type. */
578 arch_addr_t addr = (arch_addr_t)
579 (uintptr_t)sym.st_value + lte->bias;
580 if (library_symbol_init(libsym, addr, sym_name, 1,
583 libsym->arch.type = PPC64_PLT_STUB;
584 libsym->next = lte->arch.stubs;
585 lte->arch.stubs = libsym;
593 read_plt_slot_value(struct process *proc, GElf_Addr addr, GElf_Addr *valp)
595 /* On PPC64, we read from .plt, which contains 8 byte
596 * addresses. On PPC32 we read from .plt, which contains 4
597 * byte instructions, but the PLT is two instructions, and
598 * either can change. */
600 /* XXX double cast. */
601 if (proc_read_64(proc, (arch_addr_t)(uintptr_t)addr, &l) < 0) {
602 fprintf(stderr, "ptrace .plt slot value @%#" PRIx64": %s\n",
603 addr, strerror(errno));
607 *valp = (GElf_Addr)l;
612 unresolve_plt_slot(struct process *proc, GElf_Addr addr, GElf_Addr value)
614 /* We only modify plt_entry[0], which holds the resolved
615 * address of the routine. We keep the TOC and environment
616 * pointers intact. Hence the only adjustment that we need to
618 if (ptrace(PTRACE_POKETEXT, proc->pid, addr, value) < 0) {
619 fprintf(stderr, "failed to unresolve .plt slot: %s\n",
627 arch_elf_add_func_entry(struct process *proc, struct ltelf *lte,
629 arch_addr_t addr, const char *name,
630 struct library_symbol **ret)
632 if (lte->ehdr.e_machine != EM_PPC || lte->ehdr.e_type == ET_DYN)
637 ifunc = GELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC;
642 size_t len = vect_size(<e->plt_relocs);
644 for (i = 0; i < len; ++i) {
645 GElf_Rela *rela = VECT_ELEMENT(<e->plt_relocs, GElf_Rela, i);
646 if (sym->st_value == arch_plt_sym_val(lte, i, rela)) {
648 char *tmp_name = linux_append_IFUNC_to_name(name);
649 struct library_symbol *libsym = malloc(sizeof *libsym);
651 /* XXX double cast. */
652 arch_addr_t resolver_addr
653 = (arch_addr_t) (uintptr_t) rela->r_addend;
655 if (tmp_name == NULL || libsym == NULL
656 || library_symbol_init(libsym, resolver_addr,
658 LS_TOPLT_EXEC) < 0) {
665 if (elf_add_plt_entry(proc, lte, name, rela,
667 library_symbol_destroy(libsym);
671 libsym->proto = linux_IFUNC_prototype();
683 arch_elf_add_plt_entry(struct process *proc, struct ltelf *lte,
684 const char *a_name, GElf_Rela *rela, size_t ndx,
685 struct library_symbol **ret)
687 bool is_irelative = reloc_is_irelative(lte->ehdr.e_machine, rela);
689 if (! is_irelative) {
690 name = strdup(a_name);
692 GElf_Addr addr = lte->ehdr.e_machine == EM_PPC64
693 ? (GElf_Addr) rela->r_addend
694 : arch_plt_sym_val(lte, ndx, rela);
695 name = linux_elf_find_irelative_name(lte, addr);
704 struct library_symbol *chain = NULL;
705 if (lte->ehdr.e_machine == EM_PPC) {
706 if (default_elf_add_plt_entry(proc, lte, name, rela, ndx,
710 if (! lte->arch.secure_plt) {
711 /* On PPC32 with BSS PLT, delay the symbol
712 * until dynamic linker is done. */
713 assert(!chain->delayed);
723 /* PPC64. If we have stubs, we return a chain of breakpoint
724 * sites, one for each stub that corresponds to this PLT
726 struct library_symbol **symp;
727 for (symp = <e->arch.stubs; *symp != NULL; ) {
728 struct library_symbol *sym = *symp;
729 if (strcmp(sym->name, name) != 0) {
730 symp = &(*symp)->next;
734 /* Re-chain the symbol from stubs to CHAIN. */
743 /* We don't have stub symbols. Find corresponding .plt slot,
744 * and check whether it contains the corresponding PLT address
745 * (or 0 if the dynamic linker hasn't run yet). N.B. we don't
746 * want read this from ELF file, but from process image. That
747 * makes a difference if we are attaching to a running
750 GElf_Addr plt_entry_addr = arch_plt_sym_val(lte, ndx, rela);
751 GElf_Addr plt_slot_addr = rela->r_offset;
753 assert(plt_slot_addr >= lte->plt_addr
754 || plt_slot_addr < lte->plt_addr + lte->plt_size);
756 GElf_Addr plt_slot_value;
757 if (read_plt_slot_value(proc, plt_slot_addr, &plt_slot_value) < 0)
760 struct library_symbol *libsym = malloc(sizeof(*libsym));
761 if (libsym == NULL) {
762 fprintf(stderr, "allocation for .plt slot: %s\n",
769 /* XXX The double cast should be removed when
770 * arch_addr_t becomes integral type. */
771 if (library_symbol_init(libsym,
772 (arch_addr_t) (uintptr_t) plt_entry_addr,
773 name, 1, LS_TOPLT_EXEC) < 0)
775 libsym->arch.plt_slot_addr = plt_slot_addr;
778 && (plt_slot_value == plt_entry_addr || plt_slot_value == 0)) {
779 libsym->arch.type = PPC_PLT_UNRESOLVED;
780 libsym->arch.resolved_value = plt_entry_addr;
783 /* Unresolve the .plt slot. If the binary was
784 * prelinked, this makes the code invalid, because in
785 * case of prelinked binary, the dynamic linker
786 * doesn't update .plt[0] and .plt[1] with addresses
787 * of the resover. But we don't care, we will never
788 * need to enter the resolver. That just means that
789 * we have to un-un-resolve this back before we
792 if (unresolve_plt_slot(proc, plt_slot_addr, plt_entry_addr) < 0) {
793 library_symbol_destroy(libsym);
797 if (! is_irelative) {
798 mark_as_resolved(libsym, plt_slot_value);
800 libsym->arch.type = PPC_PLT_IRELATIVE;
801 libsym->arch.resolved_value = plt_entry_addr;
810 arch_elf_destroy(struct ltelf *lte)
812 struct library_symbol *sym;
813 for (sym = lte->arch.stubs; sym != NULL; ) {
814 struct library_symbol *next = sym->next;
815 library_symbol_destroy(sym);
822 dl_plt_update_bp_on_hit(struct breakpoint *bp, struct process *proc)
824 debug(DEBUG_PROCESS, "pid=%d dl_plt_update_bp_on_hit %s(%p)",
825 proc->pid, breakpoint_name(bp), bp->addr);
826 struct process_stopping_handler *self = proc->arch.handler;
827 assert(self != NULL);
829 struct library_symbol *libsym = self->breakpoint_being_enabled->libsym;
831 if (read_plt_slot_value(proc, libsym->arch.plt_slot_addr, &value) < 0)
834 /* On PPC64, we rewrite the slot value. */
835 if (proc->e_machine == EM_PPC64)
836 unresolve_plt_slot(proc, libsym->arch.plt_slot_addr,
837 libsym->arch.resolved_value);
838 /* We mark the breakpoint as resolved on both arches. */
839 mark_as_resolved(libsym, value);
841 /* cb_on_all_stopped looks if HANDLER is set to NULL as a way
842 * to check that this was run. It's an error if it
844 proc->arch.handler = NULL;
846 breakpoint_turn_off(bp, proc);
850 cb_on_all_stopped(struct process_stopping_handler *self)
852 /* Put that in for dl_plt_update_bp_on_hit to see. */
853 assert(self->task_enabling_breakpoint->arch.handler == NULL);
854 self->task_enabling_breakpoint->arch.handler = self;
856 linux_ptrace_disable_and_continue(self);
859 static enum callback_status
860 cb_keep_stepping_p(struct process_stopping_handler *self)
862 struct process *proc = self->task_enabling_breakpoint;
863 struct library_symbol *libsym = self->breakpoint_being_enabled->libsym;
866 if (read_plt_slot_value(proc, libsym->arch.plt_slot_addr, &value) < 0)
869 /* In UNRESOLVED state, the RESOLVED_VALUE in fact contains
870 * the PLT entry value. */
871 if (value == libsym->arch.resolved_value)
874 debug(DEBUG_PROCESS, "pid=%d PLT got resolved to value %#"PRIx64,
877 /* The .plt slot got resolved! We can migrate the breakpoint
878 * to RESOLVED and stop single-stepping. */
879 if (proc->e_machine == EM_PPC64
880 && unresolve_plt_slot(proc, libsym->arch.plt_slot_addr,
881 libsym->arch.resolved_value) < 0)
884 /* Resolving on PPC64 consists of overwriting a doubleword in
885 * .plt. That doubleword is than read back by a stub, and
886 * jumped on. Hopefully we can assume that double word update
887 * is done on a single place only, as it contains a final
888 * address. We still need to look around for any sync
889 * instruction, but essentially it is safe to optimize away
890 * the single stepping next time and install a post-update
893 * The situation on PPC32 BSS is more complicated. The
894 * dynamic linker here updates potentially several
895 * instructions (XXX currently we assume two) and the rules
896 * are more complicated. Sometimes it's enough to adjust just
897 * one of the addresses--the logic for generating optimal
898 * dispatch depends on relative addresses of the .plt entry
899 * and the jump destination. We can't assume that the some
900 * instruction block does the update every time. So on PPC32,
901 * we turn the optimization off and just step through it each
903 if (proc->e_machine == EM_PPC)
906 /* Install breakpoint to the address where the change takes
907 * place. If we fail, then that just means that we'll have to
908 * singlestep the next time around as well. */
909 struct process *leader = proc->leader;
910 if (leader == NULL || leader->arch.dl_plt_update_bp != NULL)
913 /* We need to install to the next instruction. ADDR points to
914 * a store instruction, so moving the breakpoint one
915 * instruction forward is safe. */
916 arch_addr_t addr = get_instruction_pointer(proc) + 4;
917 leader->arch.dl_plt_update_bp = insert_breakpoint_at(proc, addr, NULL);
918 if (leader->arch.dl_plt_update_bp == NULL)
921 static struct bp_callbacks dl_plt_update_cbs = {
922 .on_hit = dl_plt_update_bp_on_hit,
924 leader->arch.dl_plt_update_bp->cbs = &dl_plt_update_cbs;
926 /* Turn it off for now. We will turn it on again when we hit
927 * the PLT entry that needs this. */
928 breakpoint_turn_off(leader->arch.dl_plt_update_bp, proc);
931 mark_as_resolved(libsym, value);
937 jump_to_entry_point(struct process *proc, struct breakpoint *bp)
939 /* XXX The double cast should be removed when
940 * arch_addr_t becomes integral type. */
941 arch_addr_t rv = (arch_addr_t)
942 (uintptr_t)bp->libsym->arch.resolved_value;
943 set_instruction_pointer(proc, rv);
947 ppc_plt_bp_continue(struct breakpoint *bp, struct process *proc)
949 /* If this is a first call through IREL breakpoint, enable the
950 * symbol so that it doesn't look like an artificial
951 * breakpoint anymore. */
952 if (bp->libsym == NULL) {
953 assert(bp->arch.irel_libsym != NULL);
954 bp->libsym = bp->arch.irel_libsym;
955 bp->arch.irel_libsym = NULL;
958 switch (bp->libsym->arch.type) {
959 struct process *leader;
960 void (*on_all_stopped)(struct process_stopping_handler *);
961 enum callback_status (*keep_stepping_p)
962 (struct process_stopping_handler *);
965 assert(proc->e_machine == EM_PPC);
966 assert(bp->libsym != NULL);
967 assert(bp->libsym->lib->arch.bss_plt_prelinked == 0);
970 case PPC_PLT_IRELATIVE:
971 case PPC_PLT_UNRESOLVED:
972 on_all_stopped = NULL;
973 keep_stepping_p = NULL;
974 leader = proc->leader;
976 if (leader != NULL && leader->arch.dl_plt_update_bp != NULL
977 && breakpoint_turn_on(leader->arch.dl_plt_update_bp,
979 on_all_stopped = cb_on_all_stopped;
981 keep_stepping_p = cb_keep_stepping_p;
983 if (process_install_stopping_handler
984 (proc, bp, on_all_stopped, keep_stepping_p, NULL) < 0) {
985 fprintf(stderr, "ppc_plt_bp_continue: "
986 "couldn't install event handler\n");
987 continue_after_breakpoint(proc, bp);
991 case PPC_PLT_RESOLVED:
992 if (proc->e_machine == EM_PPC) {
993 continue_after_breakpoint(proc, bp);
997 jump_to_entry_point(proc, bp);
998 continue_process(proc->pid);
1001 case PPC64_PLT_STUB:
1002 /* These should never hit here. */
1006 assert(bp->libsym->arch.type != bp->libsym->arch.type);
1010 /* When a process is in a PLT stub, it may have already read the data
1011 * in .plt that we changed. If we detach now, it will jump to PLT
1012 * entry and continue to the dynamic linker, where it will SIGSEGV,
1013 * because zeroth .plt slot is not filled in prelinked binaries, and
1014 * the dynamic linker needs that data. Moreover, the process may
1015 * actually have hit the breakpoint already. This functions tries to
1016 * detect both cases and do any fix-ups necessary to mend this
1018 static enum callback_status
1019 detach_task_cb(struct process *task, void *data)
1021 struct breakpoint *bp = data;
1023 if (get_instruction_pointer(task) == bp->addr) {
1024 debug(DEBUG_PROCESS, "%d at %p, which is PLT slot",
1025 task->pid, bp->addr);
1026 jump_to_entry_point(task, bp);
1030 /* XXX There's still a window of several instructions where we
1031 * might catch the task inside a stub such that it has already
1032 * read destination address from .plt, but hasn't jumped yet,
1033 * thus avoiding the breakpoint. */
1039 ppc_plt_bp_retract(struct breakpoint *bp, struct process *proc)
1041 /* On PPC64, we rewrite .plt with PLT entry addresses. This
1042 * needs to be undone. Unfortunately, the program may have
1043 * made decisions based on that value */
1044 if (proc->e_machine == EM_PPC64
1045 && bp->libsym != NULL
1046 && bp->libsym->arch.type == PPC_PLT_RESOLVED) {
1047 each_task(proc->leader, NULL, detach_task_cb, bp);
1048 unresolve_plt_slot(proc, bp->libsym->arch.plt_slot_addr,
1049 bp->libsym->arch.resolved_value);
1054 arch_library_init(struct library *lib)
1060 arch_library_destroy(struct library *lib)
1065 arch_library_clone(struct library *retp, struct library *lib)
1071 arch_library_symbol_init(struct library_symbol *libsym)
1073 /* We set type explicitly in the code above, where we have the
1074 * necessary context. This is for calls from ltrace-elf.c and
1076 libsym->arch.type = PPC_DEFAULT;
1081 arch_library_symbol_destroy(struct library_symbol *libsym)
1086 arch_library_symbol_clone(struct library_symbol *retp,
1087 struct library_symbol *libsym)
1089 retp->arch = libsym->arch;
1093 /* For some symbol types, we need to set up custom callbacks. XXX we
1094 * don't need PROC here, we can store the data in BP if it is of
1095 * interest to us. */
1097 arch_breakpoint_init(struct process *proc, struct breakpoint *bp)
1099 bp->arch.irel_libsym = NULL;
1101 /* Artificial and entry-point breakpoints are plain. */
1102 if (bp->libsym == NULL || bp->libsym->plt_type != LS_TOPLT_EXEC)
1105 /* On PPC, secure PLT and prelinked BSS PLT are plain. */
1106 if (proc->e_machine == EM_PPC
1107 && bp->libsym->lib->arch.bss_plt_prelinked != 0)
1110 /* On PPC64, stub PLT breakpoints are plain. */
1111 if (proc->e_machine == EM_PPC64
1112 && bp->libsym->arch.type == PPC64_PLT_STUB)
1115 static struct bp_callbacks cbs = {
1116 .on_continue = ppc_plt_bp_continue,
1117 .on_retract = ppc_plt_bp_retract,
1119 breakpoint_set_callbacks(bp, &cbs);
1121 /* For JMP_IREL breakpoints, make the breakpoint look
1122 * artificial by hiding the symbol. */
1123 if (bp->libsym->arch.type == PPC_PLT_IRELATIVE) {
1124 bp->arch.irel_libsym = bp->libsym;
1132 arch_breakpoint_destroy(struct breakpoint *bp)
1137 arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp)
1139 retp->arch = sbp->arch;
1144 arch_process_init(struct process *proc)
1146 proc->arch.dl_plt_update_bp = NULL;
1147 proc->arch.handler = NULL;
1152 arch_process_destroy(struct process *proc)
1157 arch_process_clone(struct process *retp, struct process *proc)
1159 retp->arch = proc->arch;
1164 arch_process_exec(struct process *proc)
1166 return arch_process_init(proc);