ltrace-elf.c revision 2b46cfc1127d390eddd9593fe5ce5399c1f68130
1#include "config.h"
2
3#include <endian.h>
4#include <errno.h>
5#include <error.h>
6#include <fcntl.h>
7#include <gelf.h>
8#include <inttypes.h>
9#include <stdint.h>
10#include <stdlib.h>
11#include <string.h>
12#include <unistd.h>
13#include <assert.h>
14
15#include "common.h"
16#include "proc.h"
17#include "library.h"
18
19#ifdef PLT_REINITALISATION_BP
20extern char *PLTs_initialized_by_here;
21#endif
22
23#ifndef DT_PPC_GOT
24# define DT_PPC_GOT		(DT_LOPROC + 0)
25#endif
26
27#define PPC_PLT_STUB_SIZE 16
28
29static Elf_Data *loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
30{
31	Elf_Data *data = elf_getdata(scn, NULL);
32	if (data == NULL || elf_getdata(scn, data) != NULL
33	    || data->d_off || data->d_size != shdr->sh_size)
34		return NULL;
35	return data;
36}
37
38static int inside(GElf_Addr addr, GElf_Shdr *shdr)
39{
40	return addr >= shdr->sh_addr
41		&& addr < shdr->sh_addr + shdr->sh_size;
42}
43
44static int maybe_pick_section(GElf_Addr addr,
45			      Elf_Scn *in_sec, GElf_Shdr *in_shdr,
46			      Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
47{
48	if (inside(addr, in_shdr)) {
49		*tgt_sec = in_sec;
50		*tgt_shdr = *in_shdr;
51		return 1;
52	}
53	return 0;
54}
55
56static int get_section_covering(struct ltelf *lte, GElf_Addr addr,
57				Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
58{
59	int i;
60	for (i = 1; i < lte->ehdr.e_shnum; ++i) {
61		Elf_Scn *scn;
62		GElf_Shdr shdr;
63
64		scn = elf_getscn(lte->elf, i);
65		if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
66			debug(1, "Couldn't read section or header.");
67			return 0;
68		}
69
70		if (maybe_pick_section(addr, scn, &shdr, tgt_sec, tgt_shdr))
71			return 1;
72	}
73
74	return 0;
75}
76
77static GElf_Addr read32be(Elf_Data *data, size_t offset)
78{
79	if (data->d_size < offset + 4) {
80		debug(1, "Not enough data to read 32bit value at offset %zd.",
81		      offset);
82		return 0;
83	}
84
85	unsigned char const *buf = data->d_buf + offset;
86	return ((Elf32_Word)buf[0] << 24)
87		| ((Elf32_Word)buf[1] << 16)
88		| ((Elf32_Word)buf[2] << 8)
89		| ((Elf32_Word)buf[3]);
90}
91
92static GElf_Addr get_glink_vma(struct ltelf *lte, GElf_Addr ppcgot,
93			       Elf_Data *plt_data)
94{
95	Elf_Scn *ppcgot_sec = NULL;
96	GElf_Shdr ppcgot_shdr;
97	if (ppcgot != 0
98	    && !get_section_covering(lte, ppcgot, &ppcgot_sec, &ppcgot_shdr))
99		// xxx should be the log out
100		fprintf(stderr,
101			"DT_PPC_GOT=%#" PRIx64 ", but no such section found.\n",
102			ppcgot);
103
104	if (ppcgot_sec != NULL) {
105		Elf_Data *data = loaddata(ppcgot_sec, &ppcgot_shdr);
106		if (data == NULL
107		    || data->d_size < 8 )
108			debug(1, "Couldn't read GOT data.");
109		else {
110			// where PPCGOT begins in .got
111			size_t offset = ppcgot - ppcgot_shdr.sh_addr;
112			GElf_Addr glink_vma = read32be(data, offset + 4);
113			if (glink_vma != 0) {
114				debug(1, "PPC GOT glink_vma address: %#" PRIx64,
115				      glink_vma);
116				return glink_vma;
117			}
118		}
119	}
120
121	if (plt_data != NULL) {
122		GElf_Addr glink_vma = read32be(plt_data, 0);
123		debug(1, ".plt glink_vma address: %#" PRIx64, glink_vma);
124		return glink_vma;
125	}
126
127	return 0;
128}
129
130int
131open_elf(struct ltelf *lte, const char *filename)
132{
133	lte->fd = open(filename, O_RDONLY);
134	if (lte->fd == -1)
135		return 1;
136
137	elf_version(EV_CURRENT);
138
139#ifdef HAVE_ELF_C_READ_MMAP
140	lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
141#else
142	lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
143#endif
144
145	if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF)
146		error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename);
147
148	if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL)
149		error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"",
150		      filename);
151
152	if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN)
153		error(EXIT_FAILURE, 0,
154		      "\"%s\" is not an ELF executable nor shared library",
155		      filename);
156
157	if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
158	     || lte->ehdr.e_machine != LT_ELF_MACHINE)
159#ifdef LT_ELF_MACHINE2
160	    && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
161		|| lte->ehdr.e_machine != LT_ELF_MACHINE2)
162#endif
163#ifdef LT_ELF_MACHINE3
164	    && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
165		|| lte->ehdr.e_machine != LT_ELF_MACHINE3)
166#endif
167	    )
168		error(EXIT_FAILURE, 0,
169		      "\"%s\" is ELF from incompatible architecture", filename);
170
171	return 0;
172}
173
174/* XXX temporarily non-static */
175int
176do_init_elf(struct ltelf *lte, const char *filename)
177{
178	int i;
179	GElf_Addr relplt_addr = 0;
180	GElf_Addr soname_offset = 0;
181	size_t relplt_size = 0;
182
183	debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
184	debug(1, "Reading ELF from %s...", filename);
185
186	if (open_elf(lte, filename) < 0)
187		return -1;
188
189	Elf_Data *plt_data = NULL;
190	GElf_Addr ppcgot = 0;
191
192	/* For non-DSO objects, find out the base address.  */
193	if (lte->ehdr.e_type == ET_EXEC) {
194		GElf_Phdr phdr;
195		for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
196			if (phdr.p_type == PT_LOAD) {
197				fprintf(stderr, " + loadable segment at %#lx\n",
198					phdr.p_vaddr);
199				lte->base_addr = phdr.p_vaddr;
200				break;
201			}
202		}
203	}
204
205	lte->entry_addr = lte->ehdr.e_entry;
206
207	for (i = 1; i < lte->ehdr.e_shnum; ++i) {
208		Elf_Scn *scn;
209		GElf_Shdr shdr;
210		const char *name;
211
212		scn = elf_getscn(lte->elf, i);
213		if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
214			error(EXIT_FAILURE, 0,
215			      "Couldn't get section header from \"%s\"",
216			      filename);
217
218		name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
219		if (name == NULL)
220			error(EXIT_FAILURE, 0,
221			      "Couldn't get section header from \"%s\"",
222			      filename);
223
224		if (shdr.sh_type == SHT_SYMTAB) {
225			Elf_Data *data;
226
227			lte->symtab = elf_getdata(scn, NULL);
228			lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
229			if ((lte->symtab == NULL
230			     || elf_getdata(scn, lte->symtab) != NULL)
231			    && opt_x != NULL)
232				error(EXIT_FAILURE, 0,
233				      "Couldn't get .symtab data from \"%s\"",
234				      filename);
235
236			scn = elf_getscn(lte->elf, shdr.sh_link);
237			if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
238				error(EXIT_FAILURE, 0,
239				      "Couldn't get section header from \"%s\"",
240				      filename);
241
242			data = elf_getdata(scn, NULL);
243			if (data == NULL || elf_getdata(scn, data) != NULL
244			    || shdr.sh_size != data->d_size || data->d_off)
245				error(EXIT_FAILURE, 0,
246				      "Couldn't get .strtab data from \"%s\"",
247				      filename);
248
249			lte->strtab = data->d_buf;
250		} else if (shdr.sh_type == SHT_DYNSYM) {
251			Elf_Data *data;
252
253			lte->dynsym = elf_getdata(scn, NULL);
254			lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
255			if (lte->dynsym == NULL
256			    || elf_getdata(scn, lte->dynsym) != NULL)
257				error(EXIT_FAILURE, 0,
258				      "Couldn't get .dynsym data from \"%s\"",
259				      filename);
260
261			scn = elf_getscn(lte->elf, shdr.sh_link);
262			if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
263				error(EXIT_FAILURE, 0,
264				      "Couldn't get section header from \"%s\"",
265				      filename);
266
267			data = elf_getdata(scn, NULL);
268			if (data == NULL || elf_getdata(scn, data) != NULL
269			    || shdr.sh_size != data->d_size || data->d_off)
270				error(EXIT_FAILURE, 0,
271				      "Couldn't get .dynstr data from \"%s\"",
272				      filename);
273
274			lte->dynstr = data->d_buf;
275		} else if (shdr.sh_type == SHT_DYNAMIC) {
276			Elf_Data *data;
277			size_t j;
278
279			lte->dyn_addr = shdr.sh_addr;
280			fprintf(stderr, "dyn_addr = %#lx\n", lte->dyn_addr);
281			extern void *dyn_addr;
282			dyn_addr = (void *)lte->dyn_addr;
283			lte->dyn_sz = shdr.sh_size;
284
285			data = elf_getdata(scn, NULL);
286			if (data == NULL || elf_getdata(scn, data) != NULL)
287				error(EXIT_FAILURE, 0,
288				      "Couldn't get .dynamic data from \"%s\"",
289				      filename);
290
291			for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
292				GElf_Dyn dyn;
293
294				if (gelf_getdyn(data, j, &dyn) == NULL)
295					error(EXIT_FAILURE, 0,
296					      "Couldn't get .dynamic data from \"%s\"",
297					      filename);
298#ifdef __mips__
299/**
300  MIPS ABI Supplement:
301
302  DT_PLTGOT This member holds the address of the .got section.
303
304  DT_MIPS_SYMTABNO This member holds the number of entries in the
305  .dynsym section.
306
307  DT_MIPS_LOCAL_GOTNO This member holds the number of local global
308  offset table entries.
309
310  DT_MIPS_GOTSYM This member holds the index of the first dyamic
311  symbol table entry that corresponds to an entry in the gobal offset
312  table.
313
314 */
315				if(dyn.d_tag==DT_PLTGOT){
316					lte->pltgot_addr=dyn.d_un.d_ptr;
317				}
318				if(dyn.d_tag==DT_MIPS_LOCAL_GOTNO){
319					lte->mips_local_gotno=dyn.d_un.d_val;
320				}
321				if(dyn.d_tag==DT_MIPS_GOTSYM){
322					lte->mips_gotsym=dyn.d_un.d_val;
323				}
324#endif // __mips__
325				if (dyn.d_tag == DT_JMPREL)
326					relplt_addr = dyn.d_un.d_ptr;
327				else if (dyn.d_tag == DT_PLTRELSZ)
328					relplt_size = dyn.d_un.d_val;
329				else if (dyn.d_tag == DT_PPC_GOT) {
330					ppcgot = dyn.d_un.d_val;
331					debug(1, "ppcgot %#" PRIx64, ppcgot);
332				} else if (dyn.d_tag == DT_SONAME) {
333					soname_offset = dyn.d_un.d_val;
334				}
335			}
336		} else if (shdr.sh_type == SHT_PROGBITS
337			   || shdr.sh_type == SHT_NOBITS) {
338			if (strcmp(name, ".plt") == 0) {
339				lte->plt_addr = shdr.sh_addr;
340				lte->plt_size = shdr.sh_size;
341				if (shdr.sh_flags & SHF_EXECINSTR) {
342					lte->lte_flags |= LTE_PLT_EXECUTABLE;
343				}
344				if (lte->ehdr.e_machine == EM_PPC) {
345					plt_data = loaddata(scn, &shdr);
346					if (plt_data == NULL)
347						fprintf(stderr,
348							"Can't load .plt data\n");
349				}
350			}
351#ifdef ARCH_SUPPORTS_OPD
352			else if (strcmp(name, ".opd") == 0) {
353				lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
354				lte->opd_size = shdr.sh_size;
355				lte->opd = elf_rawdata(scn, NULL);
356			}
357#endif
358		}
359	}
360
361	if (lte->dynsym == NULL || lte->dynstr == NULL)
362		error(EXIT_FAILURE, 0,
363		      "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
364
365	if (!relplt_addr || !lte->plt_addr) {
366		debug(1, "%s has no PLT relocations", filename);
367		lte->relplt = NULL;
368		lte->relplt_count = 0;
369	} else if (relplt_size == 0) {
370		debug(1, "%s has unknown PLT size", filename);
371		lte->relplt = NULL;
372		lte->relplt_count = 0;
373	} else {
374		if (lte->ehdr.e_machine == EM_PPC) {
375			GElf_Addr glink_vma
376				= get_glink_vma(lte, ppcgot, plt_data);
377
378			assert (relplt_size % 12 == 0);
379			size_t count = relplt_size / 12; // size of RELA entry
380			lte->plt_stub_vma = glink_vma
381				- (GElf_Addr)count * PPC_PLT_STUB_SIZE;
382			debug(1, "stub_vma is %#" PRIx64, lte->plt_stub_vma);
383		}
384
385		for (i = 1; i < lte->ehdr.e_shnum; ++i) {
386			Elf_Scn *scn;
387			GElf_Shdr shdr;
388
389			scn = elf_getscn(lte->elf, i);
390			if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
391				error(EXIT_FAILURE, 0,
392				      "Couldn't get section header from \"%s\"",
393				      filename);
394			if (shdr.sh_addr == relplt_addr
395			    && shdr.sh_size == relplt_size) {
396				lte->relplt = elf_getdata(scn, NULL);
397				lte->relplt_count =
398				    shdr.sh_size / shdr.sh_entsize;
399				if (lte->relplt == NULL
400				    || elf_getdata(scn, lte->relplt) != NULL)
401					error(EXIT_FAILURE, 0,
402					      "Couldn't get .rel*.plt data from \"%s\"",
403					      filename);
404				break;
405			}
406		}
407
408		if (i == lte->ehdr.e_shnum)
409			error(EXIT_FAILURE, 0,
410			      "Couldn't find .rel*.plt section in \"%s\"",
411			      filename);
412
413		debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
414	}
415
416	if (soname_offset != 0)
417		lte->soname = lte->dynstr + soname_offset;
418
419	return 0;
420}
421
422/* XXX temporarily non-static */
423void
424do_close_elf(struct ltelf *lte) {
425	debug(DEBUG_FUNCTION, "do_close_elf()");
426	elf_end(lte->elf);
427	close(lte->fd);
428}
429
430/* XXX non-static for now, as it's not called anywhere.  But we want
431 * this code around.  */
432GElf_Addr
433opd2addr(struct ltelf *lte, GElf_Addr addr) {
434#ifdef ARCH_SUPPORTS_OPD
435	unsigned long base, offset;
436
437	if (!lte->opd)
438		return addr;
439
440	base = (unsigned long)lte->opd->d_buf;
441	offset = (unsigned long)addr - (unsigned long)lte->opd_addr;
442	if (offset > lte->opd_size)
443		error(EXIT_FAILURE, 0, "static plt not in .opd");
444
445	return *(GElf_Addr*)(base + offset);
446#else //!ARCH_SUPPORTS_OPD
447	return addr;
448#endif
449}
450
451struct library *
452ltelf_read_library(const char *filename, GElf_Addr base)
453{
454	 // XXX we leak LTE contents
455	struct ltelf lte = {
456		.base_addr = base,
457	};
458	if (do_init_elf(&lte, filename) < 0)
459		return NULL;
460
461	struct library *lib = malloc(sizeof(*lib));
462	char *soname = NULL;
463	if (lib == NULL) {
464	fail:
465		free(soname);
466		library_destroy(lib);
467		free(lib);
468		lib = NULL;
469		goto done;
470	}
471
472	if (lte.soname != NULL) {
473		soname = strdup(lte.soname);
474		if (soname == NULL)
475			goto fail;
476	}
477
478	library_init(lib, soname, soname != NULL);
479	lib->entry = (target_address_t)lte.entry_addr;
480	lib->base = (target_address_t)lte.base_addr;
481
482	size_t i;
483	for (i = 0; i < lte.relplt_count; ++i) {
484		GElf_Rel rel;
485		GElf_Rela rela;
486		GElf_Sym sym;
487		GElf_Addr addr;
488		void *ret;
489
490		if (lte.relplt->d_type == ELF_T_REL) {
491			ret = gelf_getrel(lte.relplt, i, &rel);
492			rela.r_offset = rel.r_offset;
493			rela.r_info = rel.r_info;
494			rela.r_addend = 0;
495		} else {
496			ret = gelf_getrela(lte.relplt, i, &rela);
497		}
498
499		if (ret == NULL
500		    || ELF64_R_SYM(rela.r_info) >= lte.dynsym_count
501		    || gelf_getsym(lte.dynsym, ELF64_R_SYM(rela.r_info),
502				   &sym) == NULL)
503			error(EXIT_FAILURE, 0,
504			      "Couldn't get relocation from \"%s\"",
505			      filename);
506
507		/* We will destroy the ELF object at the end of the
508		 * scope.  We need to copy the name for our purposes.
509		 * XXX consider just keeping the ELF around.  */
510		char *name = strdup(lte.dynstr + sym.st_name);
511		if (name == NULL) {
512		fail2:
513			free(name);
514			goto fail;
515		}
516
517		enum toplt pltt;
518		if (sym.st_value == 0 && lte.plt_stub_vma != 0) {
519			pltt = LS_TOPLT_EXEC;
520			addr = lte.plt_stub_vma + PPC_PLT_STUB_SIZE * i;
521		} else {
522			pltt = PLTS_ARE_EXECUTABLE(&lte)
523				?  LS_TOPLT_EXEC : LS_TOPLT_POINT;
524			addr = arch_plt_sym_val(&lte, i, &rela);
525		}
526
527		struct library_symbol *libsym = malloc(sizeof(*libsym));
528		if (libsym == NULL)
529			goto fail2;
530		library_symbol_init(libsym, addr, name, 1, pltt,
531				    ELF64_ST_BIND(sym.st_info) == STB_WEAK);
532		library_add_symbol(lib, libsym);
533	}
534
535done:
536	do_close_elf(&lte);
537	return lib;
538}
539
540struct library *
541ltelf_read_main_binary(struct Process *proc, const char *path)
542{
543	fprintf(stderr, "ltelf_read_main_binary %d %s\n", proc->pid, path);
544	char *fname = pid2name(proc->pid);
545	struct library *lib = ltelf_read_library(fname, 0);
546	library_set_name(lib, path, 0);
547	return lib;
548}
549