blob: 88ea90a068111260832820c2ae835aee08a74455 [file] [log] [blame]
Juan Cespedesd914a202004-11-10 00:15:33 +01001#include <gelf.h>
Juan Cespedesa7af00d2009-07-26 13:23:18 +02002#include <sys/ptrace.h>
Petr Machatae67635d2012-03-21 03:37:39 +01003#include <errno.h>
4#include <error.h>
5#include <inttypes.h>
6#include <assert.h>
Petr Machata37d368e2012-03-24 04:58:08 +01007#include <string.h>
Petr Machatae67635d2012-03-21 03:37:39 +01008
Petr Machata366c2f42012-02-09 19:34:36 +01009#include "proc.h"
Juan Cespedesf7281232009-06-25 16:11:21 +020010#include "common.h"
Petr Machatae67635d2012-03-21 03:37:39 +010011#include "library.h"
Petr Machatab64b5c72012-03-27 03:19:42 +020012#include "breakpoint.h"
Petr Machata58b2d0f2012-03-28 02:19:20 +020013#include "linux-gnu/trace.h"
Petr Machatae67635d2012-03-21 03:37:39 +010014
Petr Machata37d368e2012-03-24 04:58:08 +010015/* 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
21 * contains trampolines and we put breakpoints to those. With secure
22 * PLT, the .plt section doesn't contain instructions but addresses.
23 * The real PLT table is stored in .text. Addresses of those PLT
24 * entries can be computed, and it fact that's what the glink deal
25 * below does.
26 *
27 * If not prelinked, BSS PLT entries in the .plt section contain
28 * zeroes that are overwritten by the dynamic linker during start-up.
29 * For that reason, ltrace realizes those breakpoints only after
30 * .start is hit.
31 *
32 * 64-bit PPC is more involved. Program linker creates for each
33 * library call a _stub_ symbol named xxxxxxxx.plt_call.<callee>
34 * (where xxxxxxxx is a hexadecimal number). That stub does the call
35 * dispatch: it loads an address of a function to call from the
36 * section .plt, and branches. PLT entries themselves are essentially
37 * a curried call to the resolver. When the symbol is resolved, the
38 * resolver updates the value stored in .plt, and the next time
39 * around, the stub calls the library function directly. So we make
40 * at most one trip (none if the binary is prelinked) through each PLT
41 * entry, and correspondingly that is useless as a breakpoint site.
42 *
43 * Note the three confusing terms: stubs (that play the role of PLT
44 * entries), PLT entries, .plt section.
45 *
46 * We first check symbol tables and see if we happen to have stub
47 * symbols available. If yes we just put breakpoints to those, and
48 * treat them as usual breakpoints. The only tricky part is realizing
49 * that there can be more than one breakpoint per symbol.
50 *
51 * The case that we don't have the stub symbols available is harder.
52 * The following scheme uses two kinds of PLT breakpoints: unresolved
53 * and resolved (to some address). When the process starts (or when
54 * we attach), we distribute unresolved PLT breakpoints to the PLT
55 * entries (not stubs). Then we look in .plt, and for each entry
56 * whose value is different than the corresponding PLT entry address,
57 * we assume it was already resolved, and convert the breakpoint to
58 * resolved. We also rewrite the resolved value in .plt back to the
59 * PLT address.
60 *
61 * When a PLT entry hits a resolved breakpoint (which happens because
62 * we put back the unresolved addresses to .plt), we move the
63 * instruction pointer to the corresponding address and continue the
64 * process as if nothing happened.
65 *
66 * When unresolved PLT entry is called for the first time, we need to
67 * catch the new value that the resolver will write to a .plt slot.
68 * We also need to prevent another thread from racing through and
69 * taking the branch without ltrace noticing. So when unresolved PLT
70 * entry hits, we have to stop all threads. We then single-step
71 * through the resolver, until the .plt slot changes. When it does,
72 * we treat it the same way as above: convert the PLT breakpoint to
73 * resolved, and rewrite the .plt value back to PLT address. We then
74 * start all threads again.
75 *
76 * In theory we might find the exact instruction that will update the
77 * .plt slot, and emulate it, updating the PLT breakpoint immediately,
78 * and then just skip it. But that's even messier than the thread
79 * stopping business and single stepping that needs to be done.
Petr Machata58b2d0f2012-03-28 02:19:20 +020080 *
81 * Short of doing this we really have to stop everyone. There is no
82 * way around that. Unless we know where the stubs are, we don't have
83 * a way to catch a thread that would use the window of opportunity
84 * between updating .plt and notifying ltrace about the singlestep.
Petr Machata37d368e2012-03-24 04:58:08 +010085 */
86
Petr Machatae67635d2012-03-21 03:37:39 +010087#define PPC_PLT_STUB_SIZE 16
Petr Machatab64b5c72012-03-27 03:19:42 +020088#define PPC64_PLT_STUB_SIZE 8 //xxx
Petr Machatae67635d2012-03-21 03:37:39 +010089
90static inline int
Petr Machata4e2073f2012-03-21 05:15:44 +010091host_powerpc64()
Petr Machatae67635d2012-03-21 03:37:39 +010092{
93#ifdef __powerpc64__
94 return 1;
95#else
96 return 0;
97#endif
98}
99
Juan Cespedesf1350522008-12-16 18:19:58 +0100100GElf_Addr
Petr Machata4e2073f2012-03-21 05:15:44 +0100101arch_plt_sym_val(struct ltelf *lte, size_t ndx, GElf_Rela *rela)
102{
103 if (lte->ehdr.e_machine == EM_PPC && lte->arch.secure_plt) {
104 assert(lte->arch.plt_stub_vma != 0);
Petr Machatae67635d2012-03-21 03:37:39 +0100105 return lte->arch.plt_stub_vma + PPC_PLT_STUB_SIZE * ndx;
Petr Machata4e2073f2012-03-21 05:15:44 +0100106
107 } else if (lte->ehdr.e_machine == EM_PPC) {
Petr Machatae67635d2012-03-21 03:37:39 +0100108 return rela->r_offset;
Petr Machata4e2073f2012-03-21 05:15:44 +0100109
110 } else {
Petr Machatab64b5c72012-03-27 03:19:42 +0200111 /* If we get here, we don't have stub symbols. In
112 * that case we put brakpoints to PLT entries the same
113 * as the PPC32 secure PLT case does. */
114 assert(lte->arch.plt_stub_vma != 0);
115 return lte->arch.plt_stub_vma + PPC64_PLT_STUB_SIZE * ndx;
Petr Machata4e2073f2012-03-21 05:15:44 +0100116 }
Petr Machatae67635d2012-03-21 03:37:39 +0100117}
118
119int
120arch_translate_address(struct Process *proc,
121 target_address_t addr, target_address_t *ret)
122{
Petr Machatab64b5c72012-03-27 03:19:42 +0200123 if (proc->e_machine == EM_PPC64) {
124 assert(host_powerpc64());
Petr Machatae67635d2012-03-21 03:37:39 +0100125 long l = ptrace(PTRACE_PEEKTEXT, proc->pid, addr, 0);
126 fprintf(stderr, "arch_translate_address %p->%#lx\n",
127 addr, l);
128 if (l == -1 && errno) {
129 error(0, errno, ".opd translation of %p", addr);
130 return -1;
131 }
132 *ret = (target_address_t)l;
133 return 0;
134 }
135
136 *ret = addr;
137 return 0;
Juan Cespedesd914a202004-11-10 00:15:33 +0100138}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100139
Petr Machata2b46cfc2012-02-18 11:17:29 +0100140/* XXX Apparently PPC64 doesn't support PLT breakpoints. */
Juan Cespedesf1350522008-12-16 18:19:58 +0100141void *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200142sym2addr(Process *proc, struct library_symbol *sym) {
Olaf Heringa841f652006-09-15 01:57:49 +0200143 void *addr = sym->enter_addr;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200144 long pt_ret;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100145
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100146 debug(3, 0);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100147
Paul Gilliam76c61f12006-06-14 06:55:21 +0200148 if (sym->plt_type != LS_TOPLT_POINT) {
149 return addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100150 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100151
Paul Gilliam76c61f12006-06-14 06:55:21 +0200152 if (proc->pid == 0) {
153 return 0;
154 }
155
Juan Cespedesda9b9532009-04-07 15:33:50 +0200156 if (options.debug >= 3) {
Paul Gilliam76c61f12006-06-14 06:55:21 +0200157 xinfdump(proc->pid, (void *)(((long)addr-32)&0xfffffff0),
158 sizeof(void*)*8);
159 }
160
161 // On a PowerPC-64 system, a plt is three 64-bit words: the first is the
162 // 64-bit address of the routine. Before the PLT has been initialized,
163 // this will be 0x0. In fact, the symbol table won't have the plt's
164 // address even. Ater the PLT has been initialized, but before it has
165 // been resolved, the first word will be the address of the function in
166 // the dynamic linker that will reslove the PLT. After the PLT is
167 // resolved, this will will be the address of the routine whose symbol
168 // is in the symbol table.
169
170 // On a PowerPC-32 system, there are two types of PLTs: secure (new) and
171 // non-secure (old). For the secure case, the PLT is simply a pointer
172 // and we can treat it much as we do for the PowerPC-64 case. For the
173 // non-secure case, the PLT is executable code and we can put the
174 // break-point right in the PLT.
175
176 pt_ret = ptrace(PTRACE_PEEKTEXT, proc->pid, addr, 0);
177
Michael K. Edwards9bc4a9b2011-03-06 17:20:11 +0000178#if SIZEOF_LONG == 8
Paul Gilliam76c61f12006-06-14 06:55:21 +0200179 if (proc->mask_32bit) {
180 // Assume big-endian.
181 addr = (void *)((pt_ret >> 32) & 0xffffffff);
182 } else {
183 addr = (void *)pt_ret;
184 }
Michael K. Edwards9bc4a9b2011-03-06 17:20:11 +0000185#else
Petr Machata2b46cfc2012-02-18 11:17:29 +0100186 /* XXX Um, so where exactly are we dealing with the non-secure
187 PLT thing? */
Michael K. Edwards9bc4a9b2011-03-06 17:20:11 +0000188 addr = (void *)pt_ret;
189#endif
Paul Gilliam76c61f12006-06-14 06:55:21 +0200190
191 return addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100192}
Petr Machatae67635d2012-03-21 03:37:39 +0100193
194static GElf_Addr
195get_glink_vma(struct ltelf *lte, GElf_Addr ppcgot, Elf_Data *plt_data)
196{
197 Elf_Scn *ppcgot_sec = NULL;
198 GElf_Shdr ppcgot_shdr;
199 if (ppcgot != 0
200 && elf_get_section_covering(lte, ppcgot,
201 &ppcgot_sec, &ppcgot_shdr) < 0)
202 // xxx should be the log out
203 fprintf(stderr,
204 "DT_PPC_GOT=%#" PRIx64 ", but no such section found.\n",
205 ppcgot);
206
207 if (ppcgot_sec != NULL) {
208 Elf_Data *data = elf_loaddata(ppcgot_sec, &ppcgot_shdr);
209 if (data == NULL || data->d_size < 8 ) {
210 fprintf(stderr, "Couldn't read GOT data.\n");
211 } else {
212 // where PPCGOT begins in .got
213 size_t offset = ppcgot - ppcgot_shdr.sh_addr;
214 assert(offset % 4 == 0);
215 uint32_t glink_vma;
216 if (elf_read_u32(data, offset + 4, &glink_vma) < 0) {
217 fprintf(stderr,
218 "Couldn't read glink VMA address"
219 " at %zd@GOT\n", offset);
220 return 0;
221 }
222 if (glink_vma != 0) {
223 debug(1, "PPC GOT glink_vma address: %#" PRIx32,
224 glink_vma);
225 fprintf(stderr, "PPC GOT glink_vma "
226 "address: %#"PRIx32"\n", glink_vma);
227 return (GElf_Addr)glink_vma;
228 }
229 }
230 }
231
232 if (plt_data != NULL) {
233 uint32_t glink_vma;
234 if (elf_read_u32(plt_data, 0, &glink_vma) < 0) {
235 fprintf(stderr,
236 "Couldn't read glink VMA address at 0@.plt\n");
237 return 0;
238 }
239 debug(1, ".plt glink_vma address: %#" PRIx32, glink_vma);
240 fprintf(stderr, ".plt glink_vma address: "
241 "%#"PRIx32"\n", glink_vma);
242 return (GElf_Addr)glink_vma;
243 }
244
245 return 0;
246}
247
Petr Machata644d6692012-03-24 02:06:48 +0100248static int
Petr Machatad1746d12012-03-27 03:14:14 +0200249load_dynamic_entry(struct ltelf *lte, int tag, GElf_Addr *valuep)
Petr Machatae67635d2012-03-21 03:37:39 +0100250{
Petr Machata644d6692012-03-24 02:06:48 +0100251 Elf_Scn *scn;
252 GElf_Shdr shdr;
253 if (elf_get_section_type(lte, SHT_DYNAMIC, &scn, &shdr) < 0
254 || scn == NULL) {
255 fail:
256 error(0, 0, "Couldn't get SHT_DYNAMIC: %s",
257 elf_errmsg(-1));
258 return -1;
Petr Machatae67635d2012-03-21 03:37:39 +0100259 }
Petr Machata644d6692012-03-24 02:06:48 +0100260
261 Elf_Data *data = elf_loaddata(scn, &shdr);
262 if (data == NULL)
263 goto fail;
264
265 size_t j;
266 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
267 GElf_Dyn dyn;
268 if (gelf_getdyn(data, j, &dyn) == NULL)
269 goto fail;
270
Petr Machatad1746d12012-03-27 03:14:14 +0200271 if(dyn.d_tag == tag) {
272 *valuep = dyn.d_un.d_ptr;
Petr Machata644d6692012-03-24 02:06:48 +0100273 return 0;
274 }
275 }
276
277 return -1;
Petr Machatae67635d2012-03-21 03:37:39 +0100278}
279
Petr Machatad1746d12012-03-27 03:14:14 +0200280static int
281load_ppcgot(struct ltelf *lte, GElf_Addr *ppcgotp)
282{
283 return load_dynamic_entry(lte, DT_PPC_GOT, ppcgotp);
284}
285
Petr Machatab64b5c72012-03-27 03:19:42 +0200286static int
287load_ppc64_glink(struct ltelf *lte, GElf_Addr *glinkp)
288{
289 return load_dynamic_entry(lte, DT_PPC64_GLINK, glinkp);
290}
291
Petr Machatae67635d2012-03-21 03:37:39 +0100292int
293arch_elf_init(struct ltelf *lte)
294{
Petr Machata4e2073f2012-03-21 05:15:44 +0100295 lte->arch.secure_plt = !(lte->lte_flags & LTE_PLT_EXECUTABLE);
296 if (lte->ehdr.e_machine == EM_PPC && lte->arch.secure_plt) {
Petr Machata644d6692012-03-24 02:06:48 +0100297 GElf_Addr ppcgot;
298 if (load_ppcgot(lte, &ppcgot) < 0) {
299 fprintf(stderr, "Couldn't find DT_PPC_GOT.\n");
300 return -1;
301 }
302 GElf_Addr glink_vma = get_glink_vma(lte, ppcgot, lte->plt_data);
Petr Machatae67635d2012-03-21 03:37:39 +0100303
304 assert (lte->relplt_size % 12 == 0);
305 size_t count = lte->relplt_size / 12; // size of RELA entry
306 lte->arch.plt_stub_vma = glink_vma
307 - (GElf_Addr)count * PPC_PLT_STUB_SIZE;
308 debug(1, "stub_vma is %#" PRIx64, lte->arch.plt_stub_vma);
Petr Machatab64b5c72012-03-27 03:19:42 +0200309
310 } else if (lte->ehdr.e_machine == EM_PPC64) {
311 GElf_Addr glink_vma;
312 if (load_ppc64_glink(lte, &glink_vma) < 0) {
313 fprintf(stderr, "Couldn't find DT_PPC64_GLINK.\n");
314 return -1;
315 }
316
317 /* The first glink stub starts at offset 32. */
318 lte->arch.plt_stub_vma = glink_vma + 32;
Petr Machatae67635d2012-03-21 03:37:39 +0100319 }
320
321 /* Override the value that we gleaned from flags on the .plt
322 * section. The PLT entries are in fact executable, they are
323 * just not in .plt. */
324 lte->lte_flags |= LTE_PLT_EXECUTABLE;
Petr Machata37d368e2012-03-24 04:58:08 +0100325
326 /* On PPC64, look for stub symbols in symbol table. These are
327 * called: xxxxxxxx.plt_call.callee_name@version+addend. */
328 if (lte->ehdr.e_machine == EM_PPC64
329 && lte->symtab != NULL && lte->strtab != NULL) {
330
331 /* N.B. We can't simply skip the symbols that we fail
332 * to read or malloc. There may be more than one stub
333 * per symbol name, and if we failed in one but
334 * succeeded in another, the PLT enabling code would
335 * have no way to tell that something is missing. We
336 * could work around that, of course, but it doesn't
Petr Machata7b361142012-03-24 14:27:01 +0100337 * seem worth the trouble. So if anything fails, we
338 * just pretend that we don't have stub symbols at
339 * all, as if the binary is stripped. */
Petr Machata37d368e2012-03-24 04:58:08 +0100340
341 size_t i;
342 for (i = 0; i < lte->symtab_count; ++i) {
343 GElf_Sym sym;
Petr Machata7b361142012-03-24 14:27:01 +0100344 if (gelf_getsym(lte->symtab, i, &sym) == NULL) {
345 struct library_symbol *sym, *next;
346 fail:
347 for (sym = lte->arch.stubs; sym != NULL; ) {
348 next = sym->next;
349 library_symbol_destroy(sym);
350 free(sym);
351 sym = next;
352 }
353 lte->arch.stubs = NULL;
Petr Machata37d368e2012-03-24 04:58:08 +0100354 break;
Petr Machata7b361142012-03-24 14:27:01 +0100355 }
Petr Machata37d368e2012-03-24 04:58:08 +0100356
357 const char *name = lte->strtab + sym.st_name;
358
359#define STUBN ".plt_call."
360 if ((name = strstr(name, STUBN)) == NULL)
361 continue;
362 name += sizeof(STUBN) - 1;
363#undef STUBN
364
365 size_t len;
366 const char *ver = strchr(name, '@');
367 if (ver != NULL) {
368 len = ver - name;
369
370 } else {
371 /* If there is "+" at all, check that
372 * the symbol name ends in "+0". */
373 const char *add = strrchr(name, '+');
374 if (add != NULL) {
375 assert(strcmp(add, "+0") == 0);
376 len = add - name;
377 } else {
378 len = strlen(name);
379 }
380 }
381
382 char *sym_name = strndup(name, len);
Petr Machata7b361142012-03-24 14:27:01 +0100383 struct library_symbol *libsym = malloc(sizeof(*libsym));
384 if (sym_name == NULL || libsym == NULL) {
Petr Machata37d368e2012-03-24 04:58:08 +0100385 free(sym_name);
Petr Machata7b361142012-03-24 14:27:01 +0100386 free(libsym);
387 goto fail;
Petr Machata37d368e2012-03-24 04:58:08 +0100388 }
389
Petr Machata37d368e2012-03-24 04:58:08 +0100390 target_address_t addr
391 = (target_address_t)sym.st_value + lte->bias;
392 library_symbol_init(libsym, addr, sym_name, 1,
393 LS_TOPLT_EXEC);
Petr Machatab64b5c72012-03-27 03:19:42 +0200394 libsym->arch.type = PPC64PLT_STUB;
Petr Machata37d368e2012-03-24 04:58:08 +0100395 libsym->next = lte->arch.stubs;
396 lte->arch.stubs = libsym;
397 }
398 }
399
Petr Machatae67635d2012-03-21 03:37:39 +0100400 return 0;
401}
Petr Machata37d368e2012-03-24 04:58:08 +0100402
Petr Machata58b2d0f2012-03-28 02:19:20 +0200403static int
404read_plt_slot_value(struct Process *proc, GElf_Addr addr, GElf_Addr *valp)
405{
406 /* on PPC32 we need to do things differently, but PPC64/PPC32
407 * is currently not supported anyway. */
408 assert(host_powerpc64());
409
410 long l = ptrace(PTRACE_PEEKTEXT, proc->pid, addr, 0);
411 if (l == -1 && errno != 0) {
412 error(0, errno, "ptrace .plt slot value @%#" PRIx64, addr);
413 return -1;
414 }
415
416 *valp = (GElf_Addr)l;
417 return 0;
418}
419
420static int
421unresolve_plt_slot(struct Process *proc, GElf_Addr addr, GElf_Addr value)
422{
423 /* We only modify plt_entry[0], which holds the resolved
424 * address of the routine. We keep the TOC and environment
425 * pointers intact. Hence the only adjustment that we need to
426 * do is to IP. */
427 if (ptrace(PTRACE_POKETEXT, proc->pid, addr, value) < 0) {
428 error(0, errno, "unresolve .plt slot");
429 return -1;
430 }
431 return 0;
432}
433
Petr Machata37d368e2012-03-24 04:58:08 +0100434enum plt_status
435arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machatad1746d12012-03-27 03:14:14 +0200436 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machata37d368e2012-03-24 04:58:08 +0100437 struct library_symbol **ret)
438{
439 if (lte->ehdr.e_machine == EM_PPC)
440 return plt_default;
441
442 /* PPC64. If we have stubs, we return a chain of breakpoint
443 * sites, one for each stub that corresponds to this PLT
444 * entry. */
445 struct library_symbol *chain = NULL;
446 struct library_symbol **symp;
447 for (symp = &lte->arch.stubs; *symp != NULL; ) {
448 struct library_symbol *sym = *symp;
449 if (strcmp(sym->name, a_name) != 0) {
450 symp = &(*symp)->next;
451 continue;
452 }
453
454 /* Re-chain the symbol from stubs to CHAIN. */
455 *symp = sym->next;
456 sym->next = chain;
457 chain = sym;
458 }
459
460 if (chain != NULL) {
Petr Machata37d368e2012-03-24 04:58:08 +0100461 *ret = chain;
462 return plt_ok;
463 }
464
Petr Machatab64b5c72012-03-27 03:19:42 +0200465 /* We don't have stub symbols. Find corresponding .plt slot,
466 * and check whether it contains the corresponding PLT address
467 * (or 0 if the dynamic linker hasn't run yet). N.B. we don't
468 * want read this from ELF file, but from process image. That
469 * makes a difference if we are attaching to a running
470 * process. */
471
472 GElf_Addr plt_entry_addr = arch_plt_sym_val(lte, ndx, rela);
473 GElf_Addr plt_slot_addr = rela->r_offset;
474 assert(plt_slot_addr >= lte->plt_addr
475 || plt_slot_addr < lte->plt_addr + lte->plt_size);
476
Petr Machata58b2d0f2012-03-28 02:19:20 +0200477 GElf_Addr plt_slot_value;
478 if (read_plt_slot_value(proc, plt_slot_addr, &plt_slot_value) < 0)
Petr Machatab64b5c72012-03-27 03:19:42 +0200479 return plt_fail;
Petr Machatab64b5c72012-03-27 03:19:42 +0200480
481 char *name = strdup(a_name);
482 struct library_symbol *libsym = malloc(sizeof(*libsym));
483 if (name == NULL || libsym == NULL) {
484 error(0, errno, "allocation for .plt slot");
485 fail:
486 free(name);
487 free(libsym);
488 return plt_fail;
489 }
490
491 library_symbol_init(libsym, (target_address_t)plt_entry_addr,
492 name, 1, LS_TOPLT_EXEC);
Petr Machata58b2d0f2012-03-28 02:19:20 +0200493 libsym->arch.plt_slot_addr = plt_slot_addr;
494
495 if (plt_slot_value == plt_entry_addr || plt_slot_value == 0) {
Petr Machatab64b5c72012-03-27 03:19:42 +0200496 libsym->arch.type = PPC64PLT_UNRESOLVED;
Petr Machata58b2d0f2012-03-28 02:19:20 +0200497 libsym->arch.resolved_value = plt_entry_addr;
498
Petr Machatab64b5c72012-03-27 03:19:42 +0200499 } else {
500 /* Unresolve the .plt slot. If the binary was
501 * prelinked, this makes the code invalid, because in
502 * case of prelinked binary, the dynamic linker
503 * doesn't update .plt[0] and .plt[1] with addresses
504 * of the resover. But we don't care, we will never
505 * need to enter the resolver. That just means that
506 * we have to un-un-resolve this back before we
507 * detach, which is nothing new: we already need to
508 * retract breakpoints. */
Petr Machata58b2d0f2012-03-28 02:19:20 +0200509
510 if (unresolve_plt_slot(proc, plt_slot_addr, plt_entry_addr) < 0)
Petr Machatab64b5c72012-03-27 03:19:42 +0200511 goto fail;
Petr Machatab64b5c72012-03-27 03:19:42 +0200512 libsym->arch.type = PPC64PLT_RESOLVED;
Petr Machata58b2d0f2012-03-28 02:19:20 +0200513 libsym->arch.resolved_value = plt_slot_value;
Petr Machatab64b5c72012-03-27 03:19:42 +0200514 }
515
516 *ret = libsym;
517 return plt_ok;
Petr Machata37d368e2012-03-24 04:58:08 +0100518}
519
Petr Machata4d9a91c2012-03-24 04:55:03 +0100520void
521arch_elf_destroy(struct ltelf *lte)
522{
Petr Machata37d368e2012-03-24 04:58:08 +0100523 struct library_symbol *sym;
524 for (sym = lte->arch.stubs; sym != NULL; ) {
525 struct library_symbol *next = sym->next;
526 library_symbol_destroy(sym);
527 free(sym);
528 sym = next;
529 }
Petr Machata4d9a91c2012-03-24 04:55:03 +0100530}
Petr Machatab64b5c72012-03-27 03:19:42 +0200531
Petr Machata58b2d0f2012-03-28 02:19:20 +0200532static enum callback_status
533keep_stepping_p(struct process_stopping_handler *self)
Petr Machatab64b5c72012-03-27 03:19:42 +0200534{
Petr Machata58b2d0f2012-03-28 02:19:20 +0200535 struct Process *proc = self->task_enabling_breakpoint;
536 struct library_symbol *libsym = self->breakpoint_being_enabled->libsym;
537 GElf_Addr value;
538 if (read_plt_slot_value(proc, libsym->arch.plt_slot_addr, &value) < 0)
539 return CBS_FAIL;
540
541 /* In UNRESOLVED state, the RESOLVED_VALUE in fact contains
542 * the PLT entry value. */
543 if (value == libsym->arch.resolved_value)
544 return CBS_CONT;
545
546 /* The .plt slot got resolved! We can migrate the breakpoint
547 * to RESOLVED and stop single-stepping. */
548 if (unresolve_plt_slot(proc, libsym->arch.plt_slot_addr,
549 libsym->arch.resolved_value) < 0)
550 return CBS_FAIL;
551 libsym->arch.type = PPC64PLT_RESOLVED;
552 libsym->arch.resolved_value = value;
553
554 return CBS_STOP;
Petr Machatab64b5c72012-03-27 03:19:42 +0200555}
556
Petr Machata58b2d0f2012-03-28 02:19:20 +0200557static void
558ppc64_plt_bp_continue(struct breakpoint *bp, struct Process *proc)
559{
Petr Machata58b2d0f2012-03-28 02:19:20 +0200560 switch (bp->libsym->arch.type) {
561 target_address_t rv;
562
563 case PPC64PLT_STUB:
564 /* We should never get here. */
565 abort();
566
567 case PPC64PLT_UNRESOLVED:
568 if (process_install_stopping_handler(proc, bp, NULL,
Petr Machatacb9a28d2012-03-28 11:11:32 +0200569 &keep_stepping_p,
Petr Machata949a56a2012-04-03 00:32:03 +0200570 NULL) < 0) {
Petr Machata58b2d0f2012-03-28 02:19:20 +0200571 perror("ppc64_unresolved_bp_continue: couldn't install"
572 " event handler");
573 continue_after_breakpoint(proc, bp);
574 }
575 return;
576
577 case PPC64PLT_RESOLVED:
Petr Machata58b2d0f2012-03-28 02:19:20 +0200578 rv = (target_address_t)bp->libsym->arch.resolved_value;
579 set_instruction_pointer(proc, rv);
580 continue_process(proc->pid);
581 }
582}
583
Petr Machata52dbfb12012-03-29 16:38:26 +0200584/* For some symbol types, we need to set up custom callbacks. XXX we
585 * don't need PROC here, we can store the data in BP if it is of
586 * interest to us. */
Petr Machatab64b5c72012-03-27 03:19:42 +0200587int
588arch_breakpoint_init(struct Process *proc, struct breakpoint *bp)
589{
590 if (proc->e_machine == EM_PPC
Petr Machata052b5f12012-04-06 14:53:07 +0200591 || bp->libsym == NULL)
592 return 0;
593
594 /* We could see LS_TOPLT_EXEC or LS_TOPLT_NONE (the latter
595 * when we trace entry points), but not LS_TOPLT_POINT
596 * anywhere on PPC. */
597 assert(bp->libsym->plt_type != LS_TOPLT_POINT);
598 if (bp->libsym->plt_type != LS_TOPLT_EXEC
Petr Machatab64b5c72012-03-27 03:19:42 +0200599 || bp->libsym->arch.type == PPC64PLT_STUB)
600 return 0;
601
Petr Machata58b2d0f2012-03-28 02:19:20 +0200602 static struct bp_callbacks cbs = {
603 .on_continue = ppc64_plt_bp_continue,
604 };
605 breakpoint_set_callbacks(bp, &cbs);
Petr Machatab64b5c72012-03-27 03:19:42 +0200606 return 0;
607}
608
609void
610arch_breakpoint_destroy(struct breakpoint *bp)
611{
612}