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