blob: 97edcfe171fe40d084b648ccc5251cd5a8087c8a [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"
12
Petr Machata37d368e2012-03-24 04:58:08 +010013/* There are two PLT types on 32-bit PPC: old-style, BSS PLT, and
14 * new-style "secure" PLT. We can tell one from the other by the
15 * flags on the .plt section. If it's +X (executable), it's BSS PLT,
16 * otherwise it's secure.
17 *
18 * BSS PLT works the same way as most architectures: the .plt section
19 * contains trampolines and we put breakpoints to those. With secure
20 * PLT, the .plt section doesn't contain instructions but addresses.
21 * The real PLT table is stored in .text. Addresses of those PLT
22 * entries can be computed, and it fact that's what the glink deal
23 * below does.
24 *
25 * If not prelinked, BSS PLT entries in the .plt section contain
26 * zeroes that are overwritten by the dynamic linker during start-up.
27 * For that reason, ltrace realizes those breakpoints only after
28 * .start is hit.
29 *
30 * 64-bit PPC is more involved. Program linker creates for each
31 * library call a _stub_ symbol named xxxxxxxx.plt_call.<callee>
32 * (where xxxxxxxx is a hexadecimal number). That stub does the call
33 * dispatch: it loads an address of a function to call from the
34 * section .plt, and branches. PLT entries themselves are essentially
35 * a curried call to the resolver. When the symbol is resolved, the
36 * resolver updates the value stored in .plt, and the next time
37 * around, the stub calls the library function directly. So we make
38 * at most one trip (none if the binary is prelinked) through each PLT
39 * entry, and correspondingly that is useless as a breakpoint site.
40 *
41 * Note the three confusing terms: stubs (that play the role of PLT
42 * entries), PLT entries, .plt section.
43 *
44 * We first check symbol tables and see if we happen to have stub
45 * symbols available. If yes we just put breakpoints to those, and
46 * treat them as usual breakpoints. The only tricky part is realizing
47 * that there can be more than one breakpoint per symbol.
48 *
49 * The case that we don't have the stub symbols available is harder.
50 * The following scheme uses two kinds of PLT breakpoints: unresolved
51 * and resolved (to some address). When the process starts (or when
52 * we attach), we distribute unresolved PLT breakpoints to the PLT
53 * entries (not stubs). Then we look in .plt, and for each entry
54 * whose value is different than the corresponding PLT entry address,
55 * we assume it was already resolved, and convert the breakpoint to
56 * resolved. We also rewrite the resolved value in .plt back to the
57 * PLT address.
58 *
59 * When a PLT entry hits a resolved breakpoint (which happens because
60 * we put back the unresolved addresses to .plt), we move the
61 * instruction pointer to the corresponding address and continue the
62 * process as if nothing happened.
63 *
64 * When unresolved PLT entry is called for the first time, we need to
65 * catch the new value that the resolver will write to a .plt slot.
66 * We also need to prevent another thread from racing through and
67 * taking the branch without ltrace noticing. So when unresolved PLT
68 * entry hits, we have to stop all threads. We then single-step
69 * through the resolver, until the .plt slot changes. When it does,
70 * we treat it the same way as above: convert the PLT breakpoint to
71 * resolved, and rewrite the .plt value back to PLT address. We then
72 * start all threads again.
73 *
74 * In theory we might find the exact instruction that will update the
75 * .plt slot, and emulate it, updating the PLT breakpoint immediately,
76 * and then just skip it. But that's even messier than the thread
77 * stopping business and single stepping that needs to be done.
78 */
79
Petr Machatae67635d2012-03-21 03:37:39 +010080#define PPC_PLT_STUB_SIZE 16
81
82static inline int
Petr Machata4e2073f2012-03-21 05:15:44 +010083host_powerpc64()
Petr Machatae67635d2012-03-21 03:37:39 +010084{
85#ifdef __powerpc64__
86 return 1;
87#else
88 return 0;
89#endif
90}
91
Juan Cespedesf1350522008-12-16 18:19:58 +010092GElf_Addr
Petr Machata4e2073f2012-03-21 05:15:44 +010093arch_plt_sym_val(struct ltelf *lte, size_t ndx, GElf_Rela *rela)
94{
95 if (lte->ehdr.e_machine == EM_PPC && lte->arch.secure_plt) {
96 assert(lte->arch.plt_stub_vma != 0);
Petr Machatae67635d2012-03-21 03:37:39 +010097 return lte->arch.plt_stub_vma + PPC_PLT_STUB_SIZE * ndx;
Petr Machata4e2073f2012-03-21 05:15:44 +010098
99 } else if (lte->ehdr.e_machine == EM_PPC) {
Petr Machatae67635d2012-03-21 03:37:39 +0100100 return rela->r_offset;
Petr Machata4e2073f2012-03-21 05:15:44 +0100101
102 } else {
103 assert(lte->ehdr.e_machine == EM_PPC64);
104 fprintf(stderr, "PPC64\n");
105 abort();
106 return rela->r_offset;
107 }
Petr Machatae67635d2012-03-21 03:37:39 +0100108}
109
110int
111arch_translate_address(struct Process *proc,
112 target_address_t addr, target_address_t *ret)
113{
Petr Machata4e2073f2012-03-21 05:15:44 +0100114 if (host_powerpc64() && proc->e_machine == EM_PPC64) {
Petr Machatae67635d2012-03-21 03:37:39 +0100115 long l = ptrace(PTRACE_PEEKTEXT, proc->pid, addr, 0);
116 fprintf(stderr, "arch_translate_address %p->%#lx\n",
117 addr, l);
118 if (l == -1 && errno) {
119 error(0, errno, ".opd translation of %p", addr);
120 return -1;
121 }
122 *ret = (target_address_t)l;
123 return 0;
124 }
125
126 *ret = addr;
127 return 0;
Juan Cespedesd914a202004-11-10 00:15:33 +0100128}
Ian Wienand9a2ad352006-02-20 22:44:45 +0100129
Petr Machata2b46cfc2012-02-18 11:17:29 +0100130/* XXX Apparently PPC64 doesn't support PLT breakpoints. */
Juan Cespedesf1350522008-12-16 18:19:58 +0100131void *
Juan Cespedesa8909f72009-04-28 20:02:41 +0200132sym2addr(Process *proc, struct library_symbol *sym) {
Olaf Heringa841f652006-09-15 01:57:49 +0200133 void *addr = sym->enter_addr;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200134 long pt_ret;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100135
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100136 debug(3, 0);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100137
Paul Gilliam76c61f12006-06-14 06:55:21 +0200138 if (sym->plt_type != LS_TOPLT_POINT) {
139 return addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100140 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100141
Paul Gilliam76c61f12006-06-14 06:55:21 +0200142 if (proc->pid == 0) {
143 return 0;
144 }
145
Juan Cespedesda9b9532009-04-07 15:33:50 +0200146 if (options.debug >= 3) {
Paul Gilliam76c61f12006-06-14 06:55:21 +0200147 xinfdump(proc->pid, (void *)(((long)addr-32)&0xfffffff0),
148 sizeof(void*)*8);
149 }
150
151 // On a PowerPC-64 system, a plt is three 64-bit words: the first is the
152 // 64-bit address of the routine. Before the PLT has been initialized,
153 // this will be 0x0. In fact, the symbol table won't have the plt's
154 // address even. Ater the PLT has been initialized, but before it has
155 // been resolved, the first word will be the address of the function in
156 // the dynamic linker that will reslove the PLT. After the PLT is
157 // resolved, this will will be the address of the routine whose symbol
158 // is in the symbol table.
159
160 // On a PowerPC-32 system, there are two types of PLTs: secure (new) and
161 // non-secure (old). For the secure case, the PLT is simply a pointer
162 // and we can treat it much as we do for the PowerPC-64 case. For the
163 // non-secure case, the PLT is executable code and we can put the
164 // break-point right in the PLT.
165
166 pt_ret = ptrace(PTRACE_PEEKTEXT, proc->pid, addr, 0);
167
Michael K. Edwards9bc4a9b2011-03-06 17:20:11 +0000168#if SIZEOF_LONG == 8
Paul Gilliam76c61f12006-06-14 06:55:21 +0200169 if (proc->mask_32bit) {
170 // Assume big-endian.
171 addr = (void *)((pt_ret >> 32) & 0xffffffff);
172 } else {
173 addr = (void *)pt_ret;
174 }
Michael K. Edwards9bc4a9b2011-03-06 17:20:11 +0000175#else
Petr Machata2b46cfc2012-02-18 11:17:29 +0100176 /* XXX Um, so where exactly are we dealing with the non-secure
177 PLT thing? */
Michael K. Edwards9bc4a9b2011-03-06 17:20:11 +0000178 addr = (void *)pt_ret;
179#endif
Paul Gilliam76c61f12006-06-14 06:55:21 +0200180
181 return addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100182}
Petr Machatae67635d2012-03-21 03:37:39 +0100183
184static GElf_Addr
185get_glink_vma(struct ltelf *lte, GElf_Addr ppcgot, Elf_Data *plt_data)
186{
187 Elf_Scn *ppcgot_sec = NULL;
188 GElf_Shdr ppcgot_shdr;
189 if (ppcgot != 0
190 && elf_get_section_covering(lte, ppcgot,
191 &ppcgot_sec, &ppcgot_shdr) < 0)
192 // xxx should be the log out
193 fprintf(stderr,
194 "DT_PPC_GOT=%#" PRIx64 ", but no such section found.\n",
195 ppcgot);
196
197 if (ppcgot_sec != NULL) {
198 Elf_Data *data = elf_loaddata(ppcgot_sec, &ppcgot_shdr);
199 if (data == NULL || data->d_size < 8 ) {
200 fprintf(stderr, "Couldn't read GOT data.\n");
201 } else {
202 // where PPCGOT begins in .got
203 size_t offset = ppcgot - ppcgot_shdr.sh_addr;
204 assert(offset % 4 == 0);
205 uint32_t glink_vma;
206 if (elf_read_u32(data, offset + 4, &glink_vma) < 0) {
207 fprintf(stderr,
208 "Couldn't read glink VMA address"
209 " at %zd@GOT\n", offset);
210 return 0;
211 }
212 if (glink_vma != 0) {
213 debug(1, "PPC GOT glink_vma address: %#" PRIx32,
214 glink_vma);
215 fprintf(stderr, "PPC GOT glink_vma "
216 "address: %#"PRIx32"\n", glink_vma);
217 return (GElf_Addr)glink_vma;
218 }
219 }
220 }
221
222 if (plt_data != NULL) {
223 uint32_t glink_vma;
224 if (elf_read_u32(plt_data, 0, &glink_vma) < 0) {
225 fprintf(stderr,
226 "Couldn't read glink VMA address at 0@.plt\n");
227 return 0;
228 }
229 debug(1, ".plt glink_vma address: %#" PRIx32, glink_vma);
230 fprintf(stderr, ".plt glink_vma address: "
231 "%#"PRIx32"\n", glink_vma);
232 return (GElf_Addr)glink_vma;
233 }
234
235 return 0;
236}
237
Petr Machata644d6692012-03-24 02:06:48 +0100238static int
239load_ppcgot(struct ltelf *lte, GElf_Addr *ppcgotp)
Petr Machatae67635d2012-03-21 03:37:39 +0100240{
Petr Machata644d6692012-03-24 02:06:48 +0100241 Elf_Scn *scn;
242 GElf_Shdr shdr;
243 if (elf_get_section_type(lte, SHT_DYNAMIC, &scn, &shdr) < 0
244 || scn == NULL) {
245 fail:
246 error(0, 0, "Couldn't get SHT_DYNAMIC: %s",
247 elf_errmsg(-1));
248 return -1;
Petr Machatae67635d2012-03-21 03:37:39 +0100249 }
Petr Machata644d6692012-03-24 02:06:48 +0100250
251 Elf_Data *data = elf_loaddata(scn, &shdr);
252 if (data == NULL)
253 goto fail;
254
255 size_t j;
256 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
257 GElf_Dyn dyn;
258 if (gelf_getdyn(data, j, &dyn) == NULL)
259 goto fail;
260
261 if(dyn.d_tag == DT_PPC_GOT) {
262 *ppcgotp = dyn.d_un.d_ptr;
263 return 0;
264 }
265 }
266
267 return -1;
Petr Machatae67635d2012-03-21 03:37:39 +0100268}
269
270int
271arch_elf_init(struct ltelf *lte)
272{
Petr Machata4e2073f2012-03-21 05:15:44 +0100273 lte->arch.secure_plt = !(lte->lte_flags & LTE_PLT_EXECUTABLE);
274 if (lte->ehdr.e_machine == EM_PPC && lte->arch.secure_plt) {
Petr Machata644d6692012-03-24 02:06:48 +0100275 GElf_Addr ppcgot;
276 if (load_ppcgot(lte, &ppcgot) < 0) {
277 fprintf(stderr, "Couldn't find DT_PPC_GOT.\n");
278 return -1;
279 }
280 GElf_Addr glink_vma = get_glink_vma(lte, ppcgot, lte->plt_data);
Petr Machatae67635d2012-03-21 03:37:39 +0100281
282 assert (lte->relplt_size % 12 == 0);
283 size_t count = lte->relplt_size / 12; // size of RELA entry
284 lte->arch.plt_stub_vma = glink_vma
285 - (GElf_Addr)count * PPC_PLT_STUB_SIZE;
286 debug(1, "stub_vma is %#" PRIx64, lte->arch.plt_stub_vma);
287 }
288
289 /* Override the value that we gleaned from flags on the .plt
290 * section. The PLT entries are in fact executable, they are
291 * just not in .plt. */
292 lte->lte_flags |= LTE_PLT_EXECUTABLE;
Petr Machata37d368e2012-03-24 04:58:08 +0100293
294 /* On PPC64, look for stub symbols in symbol table. These are
295 * called: xxxxxxxx.plt_call.callee_name@version+addend. */
296 if (lte->ehdr.e_machine == EM_PPC64
297 && lte->symtab != NULL && lte->strtab != NULL) {
298
299 /* N.B. We can't simply skip the symbols that we fail
300 * to read or malloc. There may be more than one stub
301 * per symbol name, and if we failed in one but
302 * succeeded in another, the PLT enabling code would
303 * have no way to tell that something is missing. We
304 * could work around that, of course, but it doesn't
305 * seem worth the trouble. We just fail right away in
306 * face of errors. */
307
308 size_t i;
309 for (i = 0; i < lte->symtab_count; ++i) {
310 GElf_Sym sym;
311 if (gelf_getsym(lte->symtab, i, &sym) == NULL)
312 break;
313
314 const char *name = lte->strtab + sym.st_name;
315
316#define STUBN ".plt_call."
317 if ((name = strstr(name, STUBN)) == NULL)
318 continue;
319 name += sizeof(STUBN) - 1;
320#undef STUBN
321
322 size_t len;
323 const char *ver = strchr(name, '@');
324 if (ver != NULL) {
325 len = ver - name;
326
327 } else {
328 /* If there is "+" at all, check that
329 * the symbol name ends in "+0". */
330 const char *add = strrchr(name, '+');
331 if (add != NULL) {
332 assert(strcmp(add, "+0") == 0);
333 len = add - name;
334 } else {
335 len = strlen(name);
336 }
337 }
338
339 char *sym_name = strndup(name, len);
340 if (sym_name == NULL) {
341 fail:
342 free(sym_name);
343 break;
344 }
345
346 struct library_symbol *libsym = malloc(sizeof(*libsym));
347 if (libsym == NULL)
348 goto fail;
349 target_address_t addr
350 = (target_address_t)sym.st_value + lte->bias;
351 library_symbol_init(libsym, addr, sym_name, 1,
352 LS_TOPLT_EXEC);
353 libsym->next = lte->arch.stubs;
354 lte->arch.stubs = libsym;
355 }
356 }
357
Petr Machatae67635d2012-03-21 03:37:39 +0100358 return 0;
359}
Petr Machata37d368e2012-03-24 04:58:08 +0100360
361enum plt_status
362arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
363 const char *a_name, GElf_Rela *rela, size_t i,
364 struct library_symbol **ret)
365{
366 if (lte->ehdr.e_machine == EM_PPC)
367 return plt_default;
368
369 /* PPC64. If we have stubs, we return a chain of breakpoint
370 * sites, one for each stub that corresponds to this PLT
371 * entry. */
372 struct library_symbol *chain = NULL;
373 struct library_symbol **symp;
374 for (symp = &lte->arch.stubs; *symp != NULL; ) {
375 struct library_symbol *sym = *symp;
376 if (strcmp(sym->name, a_name) != 0) {
377 symp = &(*symp)->next;
378 continue;
379 }
380
381 /* Re-chain the symbol from stubs to CHAIN. */
382 *symp = sym->next;
383 sym->next = chain;
384 chain = sym;
385 }
386
387 if (chain != NULL) {
388 struct library_symbol *sym;
389 for (sym = chain; sym != NULL; sym = sym->next)
390 fprintf(stderr, "match %s --> %p\n",
391 sym->name, sym->enter_addr);
392 for (sym = lte->arch.stubs; sym != NULL; sym = sym->next)
393 fprintf(stderr, "remains %s --> %p\n",
394 sym->name, sym->enter_addr);
395
396 *ret = chain;
397 return plt_ok;
398 }
399
400 fprintf(stderr, "NO STUBS!\n");
401 abort();
402}
403
Petr Machata4d9a91c2012-03-24 04:55:03 +0100404void
405arch_elf_destroy(struct ltelf *lte)
406{
Petr Machata37d368e2012-03-24 04:58:08 +0100407 struct library_symbol *sym;
408 for (sym = lte->arch.stubs; sym != NULL; ) {
409 struct library_symbol *next = sym->next;
410 library_symbol_destroy(sym);
411 free(sym);
412 sym = next;
413 }
Petr Machata4d9a91c2012-03-24 04:55:03 +0100414}