blob: fa43e3ec1ba345853101f31b23db9bd1df8f8e9d [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
Petr Machata9a45d222012-04-17 13:48:58 +020021 * 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 Machata37d368e2012-03-24 04:58:08 +010028 *
Petr Machata9a45d222012-04-17 13:48:58 +020029 * 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 Machata37d368e2012-03-24 04:58:08 +010033 *
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 Machata19c0f292012-04-15 19:09:02 +020064 * 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 Machata37d368e2012-03-24 04:58:08 +010067 *
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 Machata19c0f292012-04-15 19:09:02 +020078 * 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 Machata58b2d0f2012-03-28 02:19:20 +020083 *
Petr Machata9a45d222012-04-17 13:48:58 +020084 * N.B. It's tempting to try to emulate the instruction that updates
85 * .plt. We would compute the resolved address, and instead of
86 * letting the dynamic linker put it in .plt, we would resolve the
87 * breakpoint to that address. This way we wouldn't need to stop
88 * other threads. However that instruction may turn out to be a sync,
89 * and in general, may be any instruction between the actual write and
90 * the following sync. XXX TODO that means that we need to put the
91 * post-enable breakpoint at the following sync, not to the
92 * instruction itself (unless it's a sync already).
Petr Machata19c0f292012-04-15 19:09:02 +020093 *
94 * XXX TODO If we have hardware watch point, we might put a read watch
95 * on .plt slot, and discover the offenders this way. I don't know
96 * the details, but I assume at most a handful (like, one or two, if
97 * available at all) addresses may be watched at a time, and thus this
98 * would be used as an amendment of the above rather than full-on
99 * solution to PLT tracing on PPC.
Petr Machata37d368e2012-03-24 04:58:08 +0100100 */
101
Petr Machatae67635d2012-03-21 03:37:39 +0100102#define PPC_PLT_STUB_SIZE 16
Petr Machatab64b5c72012-03-27 03:19:42 +0200103#define PPC64_PLT_STUB_SIZE 8 //xxx
Petr Machatae67635d2012-03-21 03:37:39 +0100104
105static inline int
Petr Machata4e2073f2012-03-21 05:15:44 +0100106host_powerpc64()
Petr Machatae67635d2012-03-21 03:37:39 +0100107{
108#ifdef __powerpc64__
109 return 1;
110#else
111 return 0;
112#endif
113}
114
Petr Machata9a45d222012-04-17 13:48:58 +0200115static int
116read_target_4(struct Process *proc, target_address_t addr, uint32_t *lp)
117{
118 unsigned long l = ptrace(PTRACE_PEEKTEXT, proc->pid, addr, 0);
119 if (l == -1UL && errno)
120 return -1;
121 if (host_powerpc64())
122 l >>= 32;
123 *lp = l;
124 return 0;
125}
126
127static int
128read_target_8(struct Process *proc, target_address_t addr, uint64_t *lp)
129{
130 assert(host_powerpc64());
131 unsigned long l = ptrace(PTRACE_PEEKTEXT, proc->pid, addr, 0);
132 if (l == -1UL && errno)
133 return -1;
134 *lp = l;
135 return 0;
136}
137
138static int
139read_target_long(struct Process *proc, target_address_t addr, uint64_t *lp)
140{
141 if (proc->e_machine == EM_PPC) {
142 uint32_t w;
143 int ret = read_target_4(proc, addr, &w);
144 if (ret >= 0)
145 *lp = (uint64_t)w;
146 return ret;
147 } else {
148 return read_target_8(proc, addr, lp);
149 }
150}
151
Petr Machatad9573322012-04-17 05:21:02 +0200152static enum callback_status
153reenable_breakpoint(struct Process *proc, struct breakpoint *bp, void *data)
154{
155 /* We don't need to re-enable non-PLT breakpoints and
156 * breakpoints that are not PPC32 BSS unprelinked. */
157 if (bp->libsym == NULL
158 || bp->libsym->plt_type == LS_TOPLT_NONE
159 || bp->libsym->lib->arch.bss_plt_prelinked != 0)
160 return CBS_CONT;
161
162 debug(DEBUG_PROCESS, "pid=%d reenable_breakpoint %s",
163 proc->pid, breakpoint_name(bp));
164
Petr Machata9a45d222012-04-17 13:48:58 +0200165 assert(proc->e_machine == EM_PPC);
166 uint32_t l;
167 if (read_target_4(proc, bp->addr, &l) < 0) {
168 error(0, errno, "couldn't read PLT value for %s(%p)",
169 breakpoint_name(bp), bp->addr);
170 return CBS_CONT;
171 }
172 bp->libsym->arch.plt_slot_addr = (GElf_Addr)bp->addr;
173 bp->libsym->arch.resolved_value = l;
174
Petr Machatad9573322012-04-17 05:21:02 +0200175 /* Re-enable the breakpoint that was overwritten by the
Petr Machataf685a3d2012-04-17 17:06:32 +0200176 * dynamic linker. */
Petr Machatad9573322012-04-17 05:21:02 +0200177 enable_breakpoint(proc, bp);
178
179 return CBS_CONT;
180}
181
182void
183arch_dynlink_done(struct Process *proc)
184{
185 /* On PPC32, .plt of objects that use BSS PLT are overwritten
186 * by the dynamic linker (unless that object was prelinked).
187 * We need to re-enable breakpoints in those objects. */
188 proc_each_breakpoint(proc, NULL, reenable_breakpoint, NULL);
189}
190
Juan Cespedesf1350522008-12-16 18:19:58 +0100191GElf_Addr
Petr Machata4e2073f2012-03-21 05:15:44 +0100192arch_plt_sym_val(struct ltelf *lte, size_t ndx, GElf_Rela *rela)
193{
194 if (lte->ehdr.e_machine == EM_PPC && lte->arch.secure_plt) {
195 assert(lte->arch.plt_stub_vma != 0);
Petr Machatae67635d2012-03-21 03:37:39 +0100196 return lte->arch.plt_stub_vma + PPC_PLT_STUB_SIZE * ndx;
Petr Machata4e2073f2012-03-21 05:15:44 +0100197
198 } else if (lte->ehdr.e_machine == EM_PPC) {
Petr Machatae67635d2012-03-21 03:37:39 +0100199 return rela->r_offset;
Petr Machata4e2073f2012-03-21 05:15:44 +0100200
201 } else {
Petr Machatab64b5c72012-03-27 03:19:42 +0200202 /* If we get here, we don't have stub symbols. In
203 * that case we put brakpoints to PLT entries the same
204 * as the PPC32 secure PLT case does. */
205 assert(lte->arch.plt_stub_vma != 0);
206 return lte->arch.plt_stub_vma + PPC64_PLT_STUB_SIZE * ndx;
Petr Machata4e2073f2012-03-21 05:15:44 +0100207 }
Petr Machatae67635d2012-03-21 03:37:39 +0100208}
209
210int
211arch_translate_address(struct Process *proc,
212 target_address_t addr, target_address_t *ret)
213{
Petr Machatab64b5c72012-03-27 03:19:42 +0200214 if (proc->e_machine == EM_PPC64) {
215 assert(host_powerpc64());
Petr Machatae67635d2012-03-21 03:37:39 +0100216 long l = ptrace(PTRACE_PEEKTEXT, proc->pid, addr, 0);
Petr Machatae67635d2012-03-21 03:37:39 +0100217 if (l == -1 && errno) {
218 error(0, errno, ".opd translation of %p", addr);
219 return -1;
220 }
221 *ret = (target_address_t)l;
222 return 0;
223 }
224
225 *ret = addr;
226 return 0;
Juan Cespedesd914a202004-11-10 00:15:33 +0100227}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100228
Juan Cespedesf1350522008-12-16 18:19:58 +0100229void *
Petr Machata18c801c2012-04-07 01:24:08 +0200230sym2addr(struct Process *proc, struct library_symbol *sym)
231{
232 return sym->enter_addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100233}
Petr Machatae67635d2012-03-21 03:37:39 +0100234
235static GElf_Addr
236get_glink_vma(struct ltelf *lte, GElf_Addr ppcgot, Elf_Data *plt_data)
237{
238 Elf_Scn *ppcgot_sec = NULL;
239 GElf_Shdr ppcgot_shdr;
240 if (ppcgot != 0
241 && elf_get_section_covering(lte, ppcgot,
242 &ppcgot_sec, &ppcgot_shdr) < 0)
Petr Machata8b00d5b2012-04-06 16:05:10 +0200243 error(0, 0, "DT_PPC_GOT=%#"PRIx64", but no such section found",
244 ppcgot);
Petr Machatae67635d2012-03-21 03:37:39 +0100245
246 if (ppcgot_sec != NULL) {
247 Elf_Data *data = elf_loaddata(ppcgot_sec, &ppcgot_shdr);
248 if (data == NULL || data->d_size < 8 ) {
Petr Machata8b00d5b2012-04-06 16:05:10 +0200249 error(0, 0, "couldn't read GOT data");
Petr Machatae67635d2012-03-21 03:37:39 +0100250 } else {
251 // where PPCGOT begins in .got
252 size_t offset = ppcgot - ppcgot_shdr.sh_addr;
253 assert(offset % 4 == 0);
254 uint32_t glink_vma;
255 if (elf_read_u32(data, offset + 4, &glink_vma) < 0) {
Petr Machata8b00d5b2012-04-06 16:05:10 +0200256 error(0, 0, "couldn't read glink VMA address"
257 " at %zd@GOT", offset);
Petr Machatae67635d2012-03-21 03:37:39 +0100258 return 0;
259 }
260 if (glink_vma != 0) {
261 debug(1, "PPC GOT glink_vma address: %#" PRIx32,
262 glink_vma);
Petr Machatae67635d2012-03-21 03:37:39 +0100263 return (GElf_Addr)glink_vma;
264 }
265 }
266 }
267
268 if (plt_data != NULL) {
269 uint32_t glink_vma;
270 if (elf_read_u32(plt_data, 0, &glink_vma) < 0) {
Petr Machata8b00d5b2012-04-06 16:05:10 +0200271 error(0, 0, "couldn't read glink VMA address");
Petr Machatae67635d2012-03-21 03:37:39 +0100272 return 0;
273 }
274 debug(1, ".plt glink_vma address: %#" PRIx32, glink_vma);
Petr Machatae67635d2012-03-21 03:37:39 +0100275 return (GElf_Addr)glink_vma;
276 }
277
278 return 0;
279}
280
Petr Machata644d6692012-03-24 02:06:48 +0100281static int
Petr Machatad1746d12012-03-27 03:14:14 +0200282load_dynamic_entry(struct ltelf *lte, int tag, GElf_Addr *valuep)
Petr Machatae67635d2012-03-21 03:37:39 +0100283{
Petr Machata644d6692012-03-24 02:06:48 +0100284 Elf_Scn *scn;
285 GElf_Shdr shdr;
286 if (elf_get_section_type(lte, SHT_DYNAMIC, &scn, &shdr) < 0
287 || scn == NULL) {
288 fail:
289 error(0, 0, "Couldn't get SHT_DYNAMIC: %s",
290 elf_errmsg(-1));
291 return -1;
Petr Machatae67635d2012-03-21 03:37:39 +0100292 }
Petr Machata644d6692012-03-24 02:06:48 +0100293
294 Elf_Data *data = elf_loaddata(scn, &shdr);
295 if (data == NULL)
296 goto fail;
297
298 size_t j;
299 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
300 GElf_Dyn dyn;
301 if (gelf_getdyn(data, j, &dyn) == NULL)
302 goto fail;
303
Petr Machatad1746d12012-03-27 03:14:14 +0200304 if(dyn.d_tag == tag) {
305 *valuep = dyn.d_un.d_ptr;
Petr Machata644d6692012-03-24 02:06:48 +0100306 return 0;
307 }
308 }
309
310 return -1;
Petr Machatae67635d2012-03-21 03:37:39 +0100311}
312
Petr Machatad1746d12012-03-27 03:14:14 +0200313static int
314load_ppcgot(struct ltelf *lte, GElf_Addr *ppcgotp)
315{
316 return load_dynamic_entry(lte, DT_PPC_GOT, ppcgotp);
317}
318
Petr Machatab64b5c72012-03-27 03:19:42 +0200319static int
320load_ppc64_glink(struct ltelf *lte, GElf_Addr *glinkp)
321{
322 return load_dynamic_entry(lte, DT_PPC64_GLINK, glinkp);
323}
324
Petr Machatad9573322012-04-17 05:21:02 +0200325static int
326nonzero_data(Elf_Data *data)
327{
Petr Machata9a45d222012-04-17 13:48:58 +0200328 /* We are not supposed to get here if there's no PLT. */
Petr Machatad9573322012-04-17 05:21:02 +0200329 assert(data != NULL);
330
331 unsigned char *buf = data->d_buf;
332 if (buf == NULL)
333 return 0;
334
335 size_t i;
336 for (i = 0; i < data->d_size; ++i)
337 if (buf[i] != 0)
338 return 1;
339 return 0;
340}
341
Petr Machatae67635d2012-03-21 03:37:39 +0100342int
Petr Machatad9573322012-04-17 05:21:02 +0200343arch_elf_init(struct ltelf *lte, struct library *lib)
Petr Machatae67635d2012-03-21 03:37:39 +0100344{
Petr Machata18c801c2012-04-07 01:24:08 +0200345 lte->arch.secure_plt = !(lte->plt_flags & SHF_EXECINSTR);
Petr Machatad9573322012-04-17 05:21:02 +0200346
347 /* For PPC32 BSS, it is important whether the binary was
348 * prelinked. If .plt section is NODATA, or if it contains
349 * zeroes, then this library is not prelinked, and we need to
350 * delay breakpoints. */
351 if (lte->ehdr.e_machine == EM_PPC && !lte->arch.secure_plt)
352 lib->arch.bss_plt_prelinked = nonzero_data(lte->plt_data);
353 else
354 /* For cases where it's irrelevant, initialize the
355 * value to something conspicuous. */
356 lib->arch.bss_plt_prelinked = -1;
357
Petr Machata4e2073f2012-03-21 05:15:44 +0100358 if (lte->ehdr.e_machine == EM_PPC && lte->arch.secure_plt) {
Petr Machata644d6692012-03-24 02:06:48 +0100359 GElf_Addr ppcgot;
360 if (load_ppcgot(lte, &ppcgot) < 0) {
Petr Machata8b00d5b2012-04-06 16:05:10 +0200361 error(0, 0, "couldn't find DT_PPC_GOT");
Petr Machata644d6692012-03-24 02:06:48 +0100362 return -1;
363 }
364 GElf_Addr glink_vma = get_glink_vma(lte, ppcgot, lte->plt_data);
Petr Machatae67635d2012-03-21 03:37:39 +0100365
366 assert (lte->relplt_size % 12 == 0);
367 size_t count = lte->relplt_size / 12; // size of RELA entry
368 lte->arch.plt_stub_vma = glink_vma
369 - (GElf_Addr)count * PPC_PLT_STUB_SIZE;
370 debug(1, "stub_vma is %#" PRIx64, lte->arch.plt_stub_vma);
Petr Machatab64b5c72012-03-27 03:19:42 +0200371
372 } else if (lte->ehdr.e_machine == EM_PPC64) {
373 GElf_Addr glink_vma;
374 if (load_ppc64_glink(lte, &glink_vma) < 0) {
Petr Machata8b00d5b2012-04-06 16:05:10 +0200375 error(0, 0, "couldn't find DT_PPC64_GLINK");
Petr Machatab64b5c72012-03-27 03:19:42 +0200376 return -1;
377 }
378
379 /* The first glink stub starts at offset 32. */
380 lte->arch.plt_stub_vma = glink_vma + 32;
Petr Machatae67635d2012-03-21 03:37:39 +0100381 }
382
Petr Machata37d368e2012-03-24 04:58:08 +0100383 /* On PPC64, look for stub symbols in symbol table. These are
384 * called: xxxxxxxx.plt_call.callee_name@version+addend. */
385 if (lte->ehdr.e_machine == EM_PPC64
386 && lte->symtab != NULL && lte->strtab != NULL) {
387
388 /* N.B. We can't simply skip the symbols that we fail
389 * to read or malloc. There may be more than one stub
390 * per symbol name, and if we failed in one but
391 * succeeded in another, the PLT enabling code would
392 * have no way to tell that something is missing. We
393 * could work around that, of course, but it doesn't
Petr Machata7b361142012-03-24 14:27:01 +0100394 * seem worth the trouble. So if anything fails, we
395 * just pretend that we don't have stub symbols at
396 * all, as if the binary is stripped. */
Petr Machata37d368e2012-03-24 04:58:08 +0100397
398 size_t i;
399 for (i = 0; i < lte->symtab_count; ++i) {
400 GElf_Sym sym;
Petr Machata7b361142012-03-24 14:27:01 +0100401 if (gelf_getsym(lte->symtab, i, &sym) == NULL) {
402 struct library_symbol *sym, *next;
403 fail:
404 for (sym = lte->arch.stubs; sym != NULL; ) {
405 next = sym->next;
406 library_symbol_destroy(sym);
407 free(sym);
408 sym = next;
409 }
410 lte->arch.stubs = NULL;
Petr Machata37d368e2012-03-24 04:58:08 +0100411 break;
Petr Machata7b361142012-03-24 14:27:01 +0100412 }
Petr Machata37d368e2012-03-24 04:58:08 +0100413
414 const char *name = lte->strtab + sym.st_name;
415
416#define STUBN ".plt_call."
417 if ((name = strstr(name, STUBN)) == NULL)
418 continue;
419 name += sizeof(STUBN) - 1;
420#undef STUBN
421
422 size_t len;
423 const char *ver = strchr(name, '@');
424 if (ver != NULL) {
425 len = ver - name;
426
427 } else {
428 /* If there is "+" at all, check that
429 * the symbol name ends in "+0". */
430 const char *add = strrchr(name, '+');
431 if (add != NULL) {
432 assert(strcmp(add, "+0") == 0);
433 len = add - name;
434 } else {
435 len = strlen(name);
436 }
437 }
438
439 char *sym_name = strndup(name, len);
Petr Machata7b361142012-03-24 14:27:01 +0100440 struct library_symbol *libsym = malloc(sizeof(*libsym));
441 if (sym_name == NULL || libsym == NULL) {
Petr Machatae8d90762012-04-15 04:28:31 +0200442 fail2:
Petr Machata37d368e2012-03-24 04:58:08 +0100443 free(sym_name);
Petr Machata7b361142012-03-24 14:27:01 +0100444 free(libsym);
445 goto fail;
Petr Machata37d368e2012-03-24 04:58:08 +0100446 }
447
Petr Machataea8eb9a2012-04-17 01:32:07 +0200448 /* XXX The double cast should be removed when
449 * target_address_t becomes integral type. */
450 target_address_t addr = (target_address_t)
451 (uintptr_t)sym.st_value + lte->bias;
Petr Machatae8d90762012-04-15 04:28:31 +0200452 if (library_symbol_init(libsym, addr, sym_name, 1,
453 LS_TOPLT_EXEC) < 0)
454 goto fail2;
Petr Machata585f60f2012-04-17 17:05:12 +0200455 libsym->arch.type = PPC64_PLT_STUB;
Petr Machata37d368e2012-03-24 04:58:08 +0100456 libsym->next = lte->arch.stubs;
457 lte->arch.stubs = libsym;
458 }
459 }
460
Petr Machatae67635d2012-03-21 03:37:39 +0100461 return 0;
462}
Petr Machata37d368e2012-03-24 04:58:08 +0100463
Petr Machata58b2d0f2012-03-28 02:19:20 +0200464static int
465read_plt_slot_value(struct Process *proc, GElf_Addr addr, GElf_Addr *valp)
466{
Petr Machata9a45d222012-04-17 13:48:58 +0200467 /* On PPC64, we read from .plt, which contains 8 byte
468 * addresses. On PPC32 we read from .plt, which contains 4
469 * byte instructions. So read_target_long is appropriate. */
470 uint64_t l;
471 if (read_target_long(proc, (target_address_t)addr, &l) < 0) {
Petr Machata58b2d0f2012-03-28 02:19:20 +0200472 error(0, errno, "ptrace .plt slot value @%#" PRIx64, addr);
473 return -1;
474 }
475
476 *valp = (GElf_Addr)l;
477 return 0;
478}
479
480static int
481unresolve_plt_slot(struct Process *proc, GElf_Addr addr, GElf_Addr value)
482{
483 /* We only modify plt_entry[0], which holds the resolved
484 * address of the routine. We keep the TOC and environment
485 * pointers intact. Hence the only adjustment that we need to
486 * do is to IP. */
487 if (ptrace(PTRACE_POKETEXT, proc->pid, addr, value) < 0) {
488 error(0, errno, "unresolve .plt slot");
489 return -1;
490 }
491 return 0;
492}
493
Petr Machata8557b4a2012-04-17 17:02:11 +0200494static void
495mark_as_resolved(struct library_symbol *libsym, GElf_Addr value)
496{
497 libsym->arch.type = PPC_PLT_RESOLVED;
498 libsym->arch.resolved_value = value;
499}
500
Petr Machata37d368e2012-03-24 04:58:08 +0100501enum plt_status
502arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
Petr Machatad1746d12012-03-27 03:14:14 +0200503 const char *a_name, GElf_Rela *rela, size_t ndx,
Petr Machata37d368e2012-03-24 04:58:08 +0100504 struct library_symbol **ret)
505{
506 if (lte->ehdr.e_machine == EM_PPC)
507 return plt_default;
508
509 /* PPC64. If we have stubs, we return a chain of breakpoint
510 * sites, one for each stub that corresponds to this PLT
511 * entry. */
512 struct library_symbol *chain = NULL;
513 struct library_symbol **symp;
514 for (symp = &lte->arch.stubs; *symp != NULL; ) {
515 struct library_symbol *sym = *symp;
516 if (strcmp(sym->name, a_name) != 0) {
517 symp = &(*symp)->next;
518 continue;
519 }
520
521 /* Re-chain the symbol from stubs to CHAIN. */
522 *symp = sym->next;
523 sym->next = chain;
524 chain = sym;
525 }
526
527 if (chain != NULL) {
Petr Machata37d368e2012-03-24 04:58:08 +0100528 *ret = chain;
529 return plt_ok;
530 }
531
Petr Machatab64b5c72012-03-27 03:19:42 +0200532 /* We don't have stub symbols. Find corresponding .plt slot,
533 * and check whether it contains the corresponding PLT address
534 * (or 0 if the dynamic linker hasn't run yet). N.B. we don't
535 * want read this from ELF file, but from process image. That
536 * makes a difference if we are attaching to a running
537 * process. */
538
539 GElf_Addr plt_entry_addr = arch_plt_sym_val(lte, ndx, rela);
540 GElf_Addr plt_slot_addr = rela->r_offset;
541 assert(plt_slot_addr >= lte->plt_addr
542 || plt_slot_addr < lte->plt_addr + lte->plt_size);
543
Petr Machata58b2d0f2012-03-28 02:19:20 +0200544 GElf_Addr plt_slot_value;
545 if (read_plt_slot_value(proc, plt_slot_addr, &plt_slot_value) < 0)
Petr Machatab64b5c72012-03-27 03:19:42 +0200546 return plt_fail;
Petr Machatab64b5c72012-03-27 03:19:42 +0200547
548 char *name = strdup(a_name);
549 struct library_symbol *libsym = malloc(sizeof(*libsym));
550 if (name == NULL || libsym == NULL) {
551 error(0, errno, "allocation for .plt slot");
552 fail:
553 free(name);
554 free(libsym);
555 return plt_fail;
556 }
557
Petr Machataea8eb9a2012-04-17 01:32:07 +0200558 /* XXX The double cast should be removed when
559 * target_address_t becomes integral type. */
560 if (library_symbol_init(libsym,
561 (target_address_t)(uintptr_t)plt_entry_addr,
Petr Machatae8d90762012-04-15 04:28:31 +0200562 name, 1, LS_TOPLT_EXEC) < 0)
563 goto fail;
Petr Machata58b2d0f2012-03-28 02:19:20 +0200564 libsym->arch.plt_slot_addr = plt_slot_addr;
565
566 if (plt_slot_value == plt_entry_addr || plt_slot_value == 0) {
Petr Machata585f60f2012-04-17 17:05:12 +0200567 libsym->arch.type = PPC_PLT_UNRESOLVED;
Petr Machata58b2d0f2012-03-28 02:19:20 +0200568 libsym->arch.resolved_value = plt_entry_addr;
569
Petr Machatab64b5c72012-03-27 03:19:42 +0200570 } else {
571 /* Unresolve the .plt slot. If the binary was
572 * prelinked, this makes the code invalid, because in
573 * case of prelinked binary, the dynamic linker
574 * doesn't update .plt[0] and .plt[1] with addresses
575 * of the resover. But we don't care, we will never
576 * need to enter the resolver. That just means that
577 * we have to un-un-resolve this back before we
Petr Machata19c0f292012-04-15 19:09:02 +0200578 * detach. */
Petr Machata58b2d0f2012-03-28 02:19:20 +0200579
Petr Machatae5ebe212012-04-15 04:41:13 +0200580 if (unresolve_plt_slot(proc, plt_slot_addr, plt_entry_addr) < 0) {
581 library_symbol_destroy(libsym);
Petr Machatab64b5c72012-03-27 03:19:42 +0200582 goto fail;
Petr Machatae5ebe212012-04-15 04:41:13 +0200583 }
Petr Machata8557b4a2012-04-17 17:02:11 +0200584 mark_as_resolved(libsym, plt_slot_value);
Petr Machatab64b5c72012-03-27 03:19:42 +0200585 }
586
587 *ret = libsym;
588 return plt_ok;
Petr Machata37d368e2012-03-24 04:58:08 +0100589}
590
Petr Machata4d9a91c2012-03-24 04:55:03 +0100591void
592arch_elf_destroy(struct ltelf *lte)
593{
Petr Machata37d368e2012-03-24 04:58:08 +0100594 struct library_symbol *sym;
595 for (sym = lte->arch.stubs; sym != NULL; ) {
596 struct library_symbol *next = sym->next;
597 library_symbol_destroy(sym);
598 free(sym);
599 sym = next;
600 }
Petr Machata4d9a91c2012-03-24 04:55:03 +0100601}
Petr Machatab64b5c72012-03-27 03:19:42 +0200602
Petr Machata6b314182012-04-15 04:40:45 +0200603static void
604dl_plt_update_bp_on_hit(struct breakpoint *bp, struct Process *proc)
605{
Petr Machatab04b64b2012-04-17 17:05:37 +0200606 debug(DEBUG_PROCESS, "pid=%d dl_plt_update_bp_on_hit %s(%p)",
607 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machata6b314182012-04-15 04:40:45 +0200608 struct process_stopping_handler *self = proc->arch.handler;
609 assert(self != NULL);
610
611 struct library_symbol *libsym = self->breakpoint_being_enabled->libsym;
612 GElf_Addr value;
613 if (read_plt_slot_value(proc, libsym->arch.plt_slot_addr, &value) < 0)
614 return;
615
Petr Machataf685a3d2012-04-17 17:06:32 +0200616 /* On PPC64, we rewrite the slot value. */
617 if (proc->e_machine == EM_PPC64)
618 unresolve_plt_slot(proc, libsym->arch.plt_slot_addr,
619 libsym->arch.resolved_value);
620 /* We mark the breakpoint as resolved on both arches. */
Petr Machata8d930e82012-04-17 17:03:01 +0200621 mark_as_resolved(libsym, value);
Petr Machata72b5ee82012-04-17 13:44:06 +0200622
Petr Machata6b314182012-04-15 04:40:45 +0200623 /* cb_on_all_stopped looks if HANDLER is set to NULL as a way
624 * to check that this was run. It's an error if it
625 * wasn't. */
626 breakpoint_turn_off(bp, proc);
627 proc->arch.handler = NULL;
628}
629
630static void
631cb_on_all_stopped(struct process_stopping_handler *self)
632{
633 /* Put that in for dl_plt_update_bp_on_hit to see. */
634 assert(self->task_enabling_breakpoint->arch.handler == NULL);
635 self->task_enabling_breakpoint->arch.handler = self;
636
637 linux_ptrace_disable_and_continue(self);
638}
639
Petr Machata58b2d0f2012-03-28 02:19:20 +0200640static enum callback_status
Petr Machata6b314182012-04-15 04:40:45 +0200641cb_keep_stepping_p(struct process_stopping_handler *self)
Petr Machatab64b5c72012-03-27 03:19:42 +0200642{
Petr Machata58b2d0f2012-03-28 02:19:20 +0200643 struct Process *proc = self->task_enabling_breakpoint;
644 struct library_symbol *libsym = self->breakpoint_being_enabled->libsym;
645 GElf_Addr value;
646 if (read_plt_slot_value(proc, libsym->arch.plt_slot_addr, &value) < 0)
647 return CBS_FAIL;
648
649 /* In UNRESOLVED state, the RESOLVED_VALUE in fact contains
650 * the PLT entry value. */
651 if (value == libsym->arch.resolved_value)
652 return CBS_CONT;
653
Petr Machatab04b64b2012-04-17 17:05:37 +0200654 debug(DEBUG_PROCESS, "pid=%d PLT got resolved to value %#"PRIx64,
655 proc->pid, value);
656
Petr Machata58b2d0f2012-03-28 02:19:20 +0200657 /* The .plt slot got resolved! We can migrate the breakpoint
658 * to RESOLVED and stop single-stepping. */
659 if (unresolve_plt_slot(proc, libsym->arch.plt_slot_addr,
660 libsym->arch.resolved_value) < 0)
661 return CBS_FAIL;
Petr Machata6b314182012-04-15 04:40:45 +0200662
663 /* Install breakpoint to the address where the change takes
664 * place. If we fail, then that just means that we'll have to
665 * singlestep the next time around as well. */
666 struct Process *leader = proc->leader;
667 if (leader == NULL || leader->arch.dl_plt_update_bp != NULL)
Petr Machata8557b4a2012-04-17 17:02:11 +0200668 goto done;
Petr Machata6b314182012-04-15 04:40:45 +0200669
670 /* We need to install to the next instruction. ADDR points to
671 * a store instruction, so moving the breakpoint one
672 * instruction forward is safe. */
673 target_address_t addr = get_instruction_pointer(proc) + 4;
674 leader->arch.dl_plt_update_bp = insert_breakpoint(proc, addr, NULL);
Petr Machata8557b4a2012-04-17 17:02:11 +0200675 if (leader->arch.dl_plt_update_bp == NULL)
676 goto done;
Petr Machata6b314182012-04-15 04:40:45 +0200677
678 /* Turn it off for now. We will turn it on again when we hit
679 * the PLT entry that needs this. */
680 breakpoint_turn_off(leader->arch.dl_plt_update_bp, proc);
681
682 if (leader->arch.dl_plt_update_bp != NULL) {
683 static struct bp_callbacks dl_plt_update_cbs = {
684 .on_hit = dl_plt_update_bp_on_hit,
685 };
686 leader->arch.dl_plt_update_bp->cbs = &dl_plt_update_cbs;
687 }
688
Petr Machata8557b4a2012-04-17 17:02:11 +0200689done:
690 mark_as_resolved(libsym, value);
Petr Machata58b2d0f2012-03-28 02:19:20 +0200691
692 return CBS_STOP;
Petr Machatab64b5c72012-03-27 03:19:42 +0200693}
694
Petr Machata58b2d0f2012-03-28 02:19:20 +0200695static void
Petr Machata9a45d222012-04-17 13:48:58 +0200696ppc_plt_bp_continue(struct breakpoint *bp, struct Process *proc)
Petr Machata58b2d0f2012-03-28 02:19:20 +0200697{
Petr Machata58b2d0f2012-03-28 02:19:20 +0200698 switch (bp->libsym->arch.type) {
699 target_address_t rv;
Petr Machata6b314182012-04-15 04:40:45 +0200700 struct Process *leader;
701 void (*on_all_stopped)(struct process_stopping_handler *);
702 enum callback_status (*keep_stepping_p)
703 (struct process_stopping_handler *);
704
Petr Machata9a45d222012-04-17 13:48:58 +0200705 case PPC_DEFAULT:
706 assert(proc->e_machine == EM_PPC);
707 assert(bp->libsym != NULL);
708 assert(bp->libsym->lib->arch.bss_plt_prelinked == 0);
709 /* fall-through */
710
Petr Machata585f60f2012-04-17 17:05:12 +0200711 case PPC_PLT_UNRESOLVED:
Petr Machata6b314182012-04-15 04:40:45 +0200712 on_all_stopped = NULL;
713 keep_stepping_p = NULL;
714 leader = proc->leader;
715
Petr Machata05058b72012-04-17 01:33:03 +0200716 if (leader != NULL && leader->arch.dl_plt_update_bp != NULL
717 && breakpoint_turn_on(leader->arch.dl_plt_update_bp,
718 proc) >= 0)
Petr Machata6b314182012-04-15 04:40:45 +0200719 on_all_stopped = cb_on_all_stopped;
Petr Machata05058b72012-04-17 01:33:03 +0200720 else
Petr Machata6b314182012-04-15 04:40:45 +0200721 keep_stepping_p = cb_keep_stepping_p;
Petr Machata6b314182012-04-15 04:40:45 +0200722
723 if (process_install_stopping_handler
724 (proc, bp, on_all_stopped, keep_stepping_p, NULL) < 0) {
Petr Machata9a45d222012-04-17 13:48:58 +0200725 error(0, 0, "ppc_plt_bp_continue: couldn't install"
726 " event handler");
Petr Machata58b2d0f2012-03-28 02:19:20 +0200727 continue_after_breakpoint(proc, bp);
728 }
729 return;
730
Petr Machata585f60f2012-04-17 17:05:12 +0200731 case PPC_PLT_RESOLVED:
Petr Machataf685a3d2012-04-17 17:06:32 +0200732 if (proc->e_machine == EM_PPC) {
733 continue_after_breakpoint(proc, bp);
734 return;
735 }
736
Petr Machataea8eb9a2012-04-17 01:32:07 +0200737 /* XXX The double cast should be removed when
738 * target_address_t becomes integral type. */
739 rv = (target_address_t)
740 (uintptr_t)bp->libsym->arch.resolved_value;
Petr Machata58b2d0f2012-03-28 02:19:20 +0200741 set_instruction_pointer(proc, rv);
742 continue_process(proc->pid);
Petr Machata50969622012-04-06 16:06:26 +0200743 return;
744
Petr Machata585f60f2012-04-17 17:05:12 +0200745 case PPC64_PLT_STUB:
Petr Machatafbd97422012-04-16 21:09:18 +0200746 /* These should never hit here. */
Petr Machata50969622012-04-06 16:06:26 +0200747 break;
Petr Machata58b2d0f2012-03-28 02:19:20 +0200748 }
Petr Machata50969622012-04-06 16:06:26 +0200749
750 assert(bp->libsym->arch.type != bp->libsym->arch.type);
751 abort();
Petr Machata58b2d0f2012-03-28 02:19:20 +0200752}
753
Petr Machatad9573322012-04-17 05:21:02 +0200754void
755arch_library_init(struct library *lib)
756{
757}
758
759void
760arch_library_destroy(struct library *lib)
761{
762}
763
764void
765arch_library_clone(struct library *retp, struct library *lib)
766{
767}
768
Petr Machata24c6e9d2012-04-15 04:31:34 +0200769int
770arch_library_symbol_init(struct library_symbol *libsym)
771{
772 /* We set type explicitly in the code above, where we have the
773 * necessary context. This is for calls from ltrace-elf.c and
774 * such. */
Petr Machatafbd97422012-04-16 21:09:18 +0200775 libsym->arch.type = PPC_DEFAULT;
Petr Machata24c6e9d2012-04-15 04:31:34 +0200776 return 0;
777}
778
779void
780arch_library_symbol_destroy(struct library_symbol *libsym)
781{
782}
783
784int
785arch_library_symbol_clone(struct library_symbol *retp,
786 struct library_symbol *libsym)
787{
788 retp->arch = libsym->arch;
789 return 0;
790}
791
Petr Machata52dbfb12012-03-29 16:38:26 +0200792/* For some symbol types, we need to set up custom callbacks. XXX we
793 * don't need PROC here, we can store the data in BP if it is of
794 * interest to us. */
Petr Machatab64b5c72012-03-27 03:19:42 +0200795int
796arch_breakpoint_init(struct Process *proc, struct breakpoint *bp)
797{
Petr Machata9a45d222012-04-17 13:48:58 +0200798 /* Artificial and entry-point breakpoints are plain. */
799 if (bp->libsym == NULL || bp->libsym->plt_type != LS_TOPLT_EXEC)
Petr Machata052b5f12012-04-06 14:53:07 +0200800 return 0;
801
Petr Machata9a45d222012-04-17 13:48:58 +0200802 /* On PPC, secure PLT and prelinked BSS PLT are plain. */
803 if (proc->e_machine == EM_PPC
804 && bp->libsym->lib->arch.bss_plt_prelinked != 0)
805 return 0;
806
807 /* On PPC64, stub PLT breakpoints are plain. */
808 if (proc->e_machine == EM_PPC64
Petr Machata585f60f2012-04-17 17:05:12 +0200809 && bp->libsym->arch.type == PPC64_PLT_STUB)
Petr Machatab64b5c72012-03-27 03:19:42 +0200810 return 0;
811
Petr Machata58b2d0f2012-03-28 02:19:20 +0200812 static struct bp_callbacks cbs = {
Petr Machata9a45d222012-04-17 13:48:58 +0200813 .on_continue = ppc_plt_bp_continue,
Petr Machata58b2d0f2012-03-28 02:19:20 +0200814 };
815 breakpoint_set_callbacks(bp, &cbs);
Petr Machatab64b5c72012-03-27 03:19:42 +0200816 return 0;
817}
818
819void
820arch_breakpoint_destroy(struct breakpoint *bp)
821{
822}
Petr Machatad3cc9882012-04-13 21:40:23 +0200823
824int
825arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp)
826{
827 retp->arch = sbp->arch;
828 return 0;
829}
Petr Machata6b314182012-04-15 04:40:45 +0200830
831int
832arch_process_init(struct Process *proc)
833{
834 proc->arch.dl_plt_update_bp = NULL;
835 proc->arch.handler = NULL;
836 return 0;
837}
838
839void
840arch_process_destroy(struct Process *proc)
841{
842}
843
844int
845arch_process_clone(struct Process *retp, struct Process *proc)
846{
847 retp->arch = proc->arch;
848 return 0;
849}
850
851int
852arch_process_exec(struct Process *proc)
853{
854 return arch_process_init(proc);
855}