ltrace-elf.c revision 7287166e8fd5949ffcf8eb1f3d378b5ea538915e
1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2006,2010,2011,2012,2013 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2010 Zachary T Welch, CodeSourcery
5 * Copyright (C) 2010 Joe Damato
6 * Copyright (C) 1997,1998,2001,2004,2007,2008,2009 Juan Cespedes
7 * Copyright (C) 2006 Olaf Hering, SUSE Linux GmbH
8 * Copyright (C) 2006 Eric Vaitl, Cisco Systems, Inc.
9 * Copyright (C) 2006 Paul Gilliam, IBM Corporation
10 * Copyright (C) 2006 Ian Wienand
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 * 02110-1301 USA
26 */
27
28#include "config.h"
29
30#include <assert.h>
31#ifdef	__linux__
32#include <endian.h>
33#endif
34#include <errno.h>
35#include <fcntl.h>
36#include <gelf.h>
37#include <inttypes.h>
38#include <search.h>
39#include <stdint.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <strings.h>
44#include <unistd.h>
45
46#include "backend.h"
47#include "filter.h"
48#include "library.h"
49#include "ltrace-elf.h"
50#include "proc.h"
51#include "debug.h"
52#include "options.h"
53
54#ifndef ARCH_HAVE_LTELF_DATA
55int
56arch_elf_init(struct ltelf *lte, struct library *lib)
57{
58	return 0;
59}
60
61void
62arch_elf_destroy(struct ltelf *lte)
63{
64}
65#endif
66
67int
68default_elf_add_plt_entry(struct process *proc, struct ltelf *lte,
69			  const char *a_name, GElf_Rela *rela, size_t ndx,
70			  struct library_symbol **ret)
71{
72	char *name = strdup(a_name);
73	if (name == NULL) {
74	fail_message:
75		fprintf(stderr, "Couldn't create symbol for PLT entry: %s\n",
76			strerror(errno));
77	fail:
78		free(name);
79		return -1;
80	}
81
82	GElf_Addr addr = arch_plt_sym_val(lte, ndx, rela);
83
84	struct library_symbol *libsym = malloc(sizeof(*libsym));
85	if (libsym == NULL)
86		goto fail_message;
87
88	/* XXX The double cast should be removed when
89	 * arch_addr_t becomes integral type.  */
90	arch_addr_t taddr = (arch_addr_t)
91		(uintptr_t)(addr + lte->bias);
92
93	if (library_symbol_init(libsym, taddr, name, 1, LS_TOPLT_EXEC) < 0) {
94		free(libsym);
95		goto fail;
96	}
97
98	libsym->next = *ret;
99	*ret = libsym;
100	return 0;
101}
102
103#ifndef ARCH_HAVE_ADD_PLT_ENTRY
104enum plt_status
105arch_elf_add_plt_entry(struct process *proc, struct ltelf *lte,
106		       const char *a_name, GElf_Rela *rela, size_t ndx,
107		       struct library_symbol **ret)
108{
109	return PLT_DEFAULT;
110}
111#endif
112
113Elf_Data *
114elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
115{
116	Elf_Data *data = elf_getdata(scn, NULL);
117	if (data == NULL || elf_getdata(scn, data) != NULL
118	    || data->d_off || data->d_size != shdr->sh_size)
119		return NULL;
120	return data;
121}
122
123static int
124elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
125		   int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
126		   void *data)
127{
128	int i;
129	for (i = 1; i < lte->ehdr.e_shnum; ++i) {
130		Elf_Scn *scn;
131		GElf_Shdr shdr;
132
133		scn = elf_getscn(lte->elf, i);
134		if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
135			debug(1, "Couldn't read section or header.");
136			return -1;
137		}
138		if (predicate(scn, &shdr, data)) {
139			*tgt_sec = scn;
140			*tgt_shdr = shdr;
141			return 0;
142		}
143	}
144
145	*tgt_sec = NULL;
146	return 0;
147}
148
149static int
150inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
151{
152	GElf_Addr addr = *(GElf_Addr *)data;
153	return addr >= shdr->sh_addr
154		&& addr < shdr->sh_addr + shdr->sh_size;
155}
156
157int
158elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
159			 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
160{
161	return elf_get_section_if(lte, tgt_sec, tgt_shdr,
162				  &inside_p, &addr);
163}
164
165static int
166type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
167{
168	GElf_Word type = *(GElf_Word *)data;
169	return shdr->sh_type == type;
170}
171
172int
173elf_get_section_type(struct ltelf *lte, GElf_Word type,
174		     Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
175{
176	return elf_get_section_if(lte, tgt_sec, tgt_shdr,
177				  &type_p, &type);
178}
179
180struct section_named_data {
181	struct ltelf *lte;
182	const char *name;
183};
184
185static int
186name_p(Elf_Scn *scn, GElf_Shdr *shdr, void *d)
187{
188	struct section_named_data *data = d;
189	const char *name = elf_strptr(data->lte->elf,
190				      data->lte->ehdr.e_shstrndx,
191				      shdr->sh_name);
192	return strcmp(name, data->name) == 0;
193}
194
195int
196elf_get_section_named(struct ltelf *lte, const char *name,
197		     Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
198{
199	struct section_named_data data = {
200		.lte = lte,
201		.name = name,
202	};
203	return elf_get_section_if(lte, tgt_sec, tgt_shdr,
204				  &name_p, &data);
205}
206
207int
208elf_can_read_next(Elf_Data *data, GElf_Xword offset, GElf_Xword size)
209{
210	assert(data != NULL);
211	if (data->d_size < size || offset > data->d_size - size) {
212		debug(1, "Not enough data to read %"PRId64"-byte value"
213		      " at offset %"PRId64".", size, offset);
214		return 0;
215	}
216	return 1;
217}
218
219#define DEF_READER(NAME, SIZE)						\
220	int								\
221	NAME(Elf_Data *data, GElf_Xword offset, uint##SIZE##_t *retp)	\
222	{								\
223		if (!elf_can_read_next(data, offset, SIZE / 8))		\
224			return -1;					\
225									\
226		if (data->d_buf == NULL) /* NODATA section */ {		\
227			*retp = 0;					\
228			return 0;					\
229		}							\
230									\
231		union {							\
232			uint##SIZE##_t dst;				\
233			char buf[0];					\
234		} u;							\
235		memcpy(u.buf, data->d_buf + offset, sizeof(u.dst));	\
236		*retp = u.dst;						\
237		return 0;						\
238	}
239
240DEF_READER(elf_read_u8, 8)
241DEF_READER(elf_read_u16, 16)
242DEF_READER(elf_read_u32, 32)
243DEF_READER(elf_read_u64, 64)
244
245#undef DEF_READER
246
247#define DEF_READER(NAME, SIZE)						\
248	int								\
249	NAME(Elf_Data *data, GElf_Xword *offset, uint##SIZE##_t *retp)	\
250	{								\
251		int rc = elf_read_u##SIZE(data, *offset, retp);		\
252		if (rc < 0)						\
253			return rc;					\
254		*offset += SIZE / 8;					\
255		return 0;						\
256	}
257
258DEF_READER(elf_read_next_u8, 8)
259DEF_READER(elf_read_next_u16, 16)
260DEF_READER(elf_read_next_u32, 32)
261DEF_READER(elf_read_next_u64, 64)
262
263#undef DEF_READER
264
265int
266elf_read_next_uleb128(Elf_Data *data, GElf_Xword *offset, uint64_t *retp)
267{
268	uint64_t result = 0;
269	int shift = 0;
270	int size = 8 * sizeof result;
271
272	while (1) {
273		uint8_t byte;
274		if (elf_read_next_u8(data, offset, &byte) < 0)
275			return -1;
276
277		uint8_t payload = byte & 0x7f;
278		result |= (uint64_t)payload << shift;
279		shift += 7;
280		if (shift > size && byte != 0x1)
281			return -1;
282		if ((byte & 0x80) == 0)
283			break;
284	}
285
286	if (retp != NULL)
287		*retp = result;
288	return 0;
289}
290
291int
292elf_read_uleb128(Elf_Data *data, GElf_Xword offset, uint64_t *retp)
293{
294	return elf_read_next_uleb128(data, &offset, retp);
295}
296
297int
298open_elf(struct ltelf *lte, const char *filename)
299{
300	lte->fd = open(filename, O_RDONLY);
301	if (lte->fd == -1)
302		return 1;
303
304	elf_version(EV_CURRENT);
305
306#ifdef HAVE_ELF_C_READ_MMAP
307	lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
308#else
309	lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
310#endif
311
312	if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) {
313		fprintf(stderr, "\"%s\" is not an ELF file\n", filename);
314		exit(EXIT_FAILURE);
315	}
316
317	if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL) {
318		fprintf(stderr, "can't read ELF header of \"%s\": %s\n",
319			filename, elf_errmsg(-1));
320		exit(EXIT_FAILURE);
321	}
322
323	if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) {
324		fprintf(stderr, "\"%s\" is neither an ELF executable"
325			" nor a shared library\n", filename);
326		exit(EXIT_FAILURE);
327	}
328
329	if (1
330#ifdef LT_ELF_MACHINE
331	    && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
332		|| lte->ehdr.e_machine != LT_ELF_MACHINE)
333#endif
334#ifdef LT_ELF_MACHINE2
335	    && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
336		|| lte->ehdr.e_machine != LT_ELF_MACHINE2)
337#endif
338#ifdef LT_ELF_MACHINE3
339	    && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
340		|| lte->ehdr.e_machine != LT_ELF_MACHINE3)
341#endif
342		) {
343		fprintf(stderr,
344			"\"%s\" is ELF from incompatible architecture\n",
345			filename);
346		exit(EXIT_FAILURE);
347	}
348
349	return 0;
350}
351
352static void
353read_symbol_table(struct ltelf *lte, const char *filename,
354		  Elf_Scn *scn, GElf_Shdr *shdr, const char *name,
355		  Elf_Data **datap, size_t *countp, const char **strsp)
356{
357	*datap = elf_getdata(scn, NULL);
358	*countp = shdr->sh_size / shdr->sh_entsize;
359	if ((*datap == NULL || elf_getdata(scn, *datap) != NULL)
360	    && options.static_filter != NULL) {
361		fprintf(stderr, "Couldn't get data of section"
362			" %s from \"%s\": %s\n",
363			name, filename, elf_errmsg(-1));
364		exit(EXIT_FAILURE);
365	}
366
367	scn = elf_getscn(lte->elf, shdr->sh_link);
368	GElf_Shdr shdr2;
369	if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) {
370		fprintf(stderr, "Couldn't get header of section"
371			" #%d from \"%s\": %s\n",
372			shdr->sh_link, filename, elf_errmsg(-1));
373		exit(EXIT_FAILURE);
374	}
375
376	Elf_Data *data = elf_getdata(scn, NULL);
377	if (data == NULL || elf_getdata(scn, data) != NULL
378	    || shdr2.sh_size != data->d_size || data->d_off) {
379		fprintf(stderr, "Couldn't get data of section"
380			" #%d from \"%s\": %s\n",
381			shdr2.sh_link, filename, elf_errmsg(-1));
382		exit(EXIT_FAILURE);
383	}
384
385	*strsp = data->d_buf;
386}
387
388static int
389do_init_elf(struct ltelf *lte, const char *filename)
390{
391	int i;
392	GElf_Addr relplt_addr = 0;
393	GElf_Addr soname_offset = 0;
394
395	debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
396	debug(1, "Reading ELF from %s...", filename);
397
398	for (i = 1; i < lte->ehdr.e_shnum; ++i) {
399		Elf_Scn *scn;
400		GElf_Shdr shdr;
401		const char *name;
402
403		scn = elf_getscn(lte->elf, i);
404		if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
405			fprintf(stderr,	"Couldn't get section #%d from"
406				" \"%s\": %s\n", i, filename, elf_errmsg(-1));
407			exit(EXIT_FAILURE);
408		}
409
410		name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
411		if (name == NULL) {
412			fprintf(stderr,	"Couldn't get name of section #%d from"
413				" \"%s\": %s\n", i, filename, elf_errmsg(-1));
414			exit(EXIT_FAILURE);
415		}
416
417		if (shdr.sh_type == SHT_SYMTAB) {
418			read_symbol_table(lte, filename,
419					  scn, &shdr, name, &lte->symtab,
420					  &lte->symtab_count, &lte->strtab);
421
422		} else if (shdr.sh_type == SHT_DYNSYM) {
423			read_symbol_table(lte, filename,
424					  scn, &shdr, name, &lte->dynsym,
425					  &lte->dynsym_count, &lte->dynstr);
426
427		} else if (shdr.sh_type == SHT_DYNAMIC) {
428			Elf_Data *data;
429			size_t j;
430
431			lte->dyn_addr = shdr.sh_addr + lte->bias;
432			lte->dyn_sz = shdr.sh_size;
433
434			data = elf_getdata(scn, NULL);
435			if (data == NULL || elf_getdata(scn, data) != NULL) {
436				fprintf(stderr, "Couldn't get .dynamic data"
437					" from \"%s\": %s\n",
438					filename, strerror(errno));
439				exit(EXIT_FAILURE);
440			}
441
442			for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
443				GElf_Dyn dyn;
444
445				if (gelf_getdyn(data, j, &dyn) == NULL) {
446					fprintf(stderr, "Couldn't get .dynamic"
447						" data from \"%s\": %s\n",
448						filename, strerror(errno));
449					exit(EXIT_FAILURE);
450				}
451				if (dyn.d_tag == DT_JMPREL)
452					relplt_addr = dyn.d_un.d_ptr;
453				else if (dyn.d_tag == DT_PLTRELSZ)
454					lte->relplt_size = dyn.d_un.d_val;
455				else if (dyn.d_tag == DT_SONAME)
456					soname_offset = dyn.d_un.d_val;
457			}
458		} else if (shdr.sh_type == SHT_PROGBITS
459			   || shdr.sh_type == SHT_NOBITS) {
460			if (strcmp(name, ".plt") == 0) {
461				lte->plt_addr = shdr.sh_addr;
462				lte->plt_size = shdr.sh_size;
463				lte->plt_data = elf_loaddata(scn, &shdr);
464				if (lte->plt_data == NULL)
465					fprintf(stderr,
466						"Can't load .plt data\n");
467				lte->plt_flags = shdr.sh_flags;
468			}
469#ifdef ARCH_SUPPORTS_OPD
470			else if (strcmp(name, ".opd") == 0) {
471				lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
472				lte->opd_size = shdr.sh_size;
473				lte->opd = elf_rawdata(scn, NULL);
474			}
475#endif
476		}
477	}
478
479	if (lte->dynsym == NULL || lte->dynstr == NULL) {
480		fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n",
481			filename);
482		exit(EXIT_FAILURE);
483	}
484
485	if (!relplt_addr || !lte->plt_addr) {
486		debug(1, "%s has no PLT relocations", filename);
487		lte->relplt = NULL;
488		lte->relplt_count = 0;
489	} else if (lte->relplt_size == 0) {
490		debug(1, "%s has unknown PLT size", filename);
491		lte->relplt = NULL;
492		lte->relplt_count = 0;
493	} else {
494
495		for (i = 1; i < lte->ehdr.e_shnum; ++i) {
496			Elf_Scn *scn;
497			GElf_Shdr shdr;
498
499			scn = elf_getscn(lte->elf, i);
500			if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
501				fprintf(stderr, "Couldn't get section header"
502					" from \"%s\": %s\n",
503					filename, elf_errmsg(-1));
504				exit(EXIT_FAILURE);
505			}
506			if (shdr.sh_addr == relplt_addr
507			    && shdr.sh_size == lte->relplt_size) {
508				lte->relplt = elf_getdata(scn, NULL);
509				lte->relplt_count =
510				    shdr.sh_size / shdr.sh_entsize;
511				if (lte->relplt == NULL
512				    || elf_getdata(scn, lte->relplt) != NULL) {
513					fprintf(stderr, "Couldn't get .rel*.plt"
514						" data from \"%s\": %s\n",
515						filename, elf_errmsg(-1));
516					exit(EXIT_FAILURE);
517				}
518				break;
519			}
520		}
521
522		if (i == lte->ehdr.e_shnum) {
523			fprintf(stderr,
524				"Couldn't find .rel*.plt section in \"%s\"\n",
525				filename);
526			exit(EXIT_FAILURE);
527		}
528
529		debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
530	}
531
532	if (soname_offset != 0)
533		lte->soname = lte->dynstr + soname_offset;
534
535	return 0;
536}
537
538void
539do_close_elf(struct ltelf *lte)
540{
541	debug(DEBUG_FUNCTION, "do_close_elf()");
542	arch_elf_destroy(lte);
543	elf_end(lte->elf);
544	close(lte->fd);
545}
546
547int
548elf_get_sym_info(struct ltelf *lte, const char *filename,
549		 size_t sym_index, GElf_Rela *rela, GElf_Sym *sym)
550{
551	int i = sym_index;
552	GElf_Rel rel;
553	void *ret;
554
555	if (lte->relplt->d_type == ELF_T_REL) {
556		ret = gelf_getrel(lte->relplt, i, &rel);
557		rela->r_offset = rel.r_offset;
558		rela->r_info = rel.r_info;
559		rela->r_addend = 0;
560	} else {
561		ret = gelf_getrela(lte->relplt, i, rela);
562	}
563
564	if (ret == NULL
565	    || ELF64_R_SYM(rela->r_info) >= lte->dynsym_count
566	    || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela->r_info),
567			   sym) == NULL) {
568		fprintf(stderr,
569			"Couldn't get relocation from \"%s\": %s\n",
570			filename, elf_errmsg(-1));
571		exit(EXIT_FAILURE);
572	}
573
574	return 0;
575}
576
577#ifndef ARCH_HAVE_GET_SYMINFO
578int
579arch_get_sym_info(struct ltelf *lte, const char *filename,
580		  size_t sym_index, GElf_Rela *rela, GElf_Sym *sym)
581{
582	return elf_get_sym_info(lte, filename, sym_index, rela, sym);
583}
584#endif
585
586static void
587mark_chain_latent(struct library_symbol *libsym)
588{
589	for (; libsym != NULL; libsym = libsym->next) {
590		debug(DEBUG_FUNCTION, "marking %s latent", libsym->name);
591		libsym->latent = 1;
592	}
593}
594
595static int
596populate_plt(struct process *proc, const char *filename,
597	     struct ltelf *lte, struct library *lib,
598	     int latent_plts)
599{
600	size_t i;
601	for (i = 0; i < lte->relplt_count; ++i) {
602		GElf_Rela rela;
603		GElf_Sym sym;
604
605		if (arch_get_sym_info(lte, filename, i, &rela, &sym) < 0)
606			continue; /* Skip this entry.  */
607
608		char const *name = lte->dynstr + sym.st_name;
609
610		/* If the symbol wasn't matched, reject it, unless we
611		 * need to keep latent PLT breakpoints for tracing
612		 * exports.  */
613		int matched = filter_matches_symbol(options.plt_filter,
614						    name, lib);
615		if (!matched && !latent_plts)
616			continue;
617
618		struct library_symbol *libsym = NULL;
619		switch (arch_elf_add_plt_entry(proc, lte, name,
620					       &rela, i, &libsym)) {
621		case PLT_DEFAULT:
622			if (default_elf_add_plt_entry(proc, lte, name,
623						      &rela, i, &libsym) < 0)
624			/* fall-through */
625		case PLT_FAIL:
626				return -1;
627			/* fall-through */
628		case PLT_OK:
629			if (libsym != NULL) {
630				/* If we are adding those symbols just
631				 * for tracing exports, mark them all
632				 * latent.  */
633				if (!matched)
634					mark_chain_latent(libsym);
635				library_add_symbol(lib, libsym);
636			}
637		}
638	}
639	return 0;
640}
641
642/* When -x rules result in request to trace several aliases, we only
643 * want to add such symbol once.  The only way that those symbols
644 * differ in is their name, e.g. in glibc you have __GI___libc_free,
645 * __cfree, __free, __libc_free, cfree and free all defined on the
646 * same address.  So instead we keep this unique symbol struct for
647 * each address, and replace name in libsym with a shorter variant if
648 * we find it.  */
649struct unique_symbol {
650	arch_addr_t addr;
651	struct library_symbol *libsym;
652};
653
654static int
655unique_symbol_cmp(const void *key, const void *val)
656{
657	const struct unique_symbol *sym_key = key;
658	const struct unique_symbol *sym_val = val;
659	return sym_key->addr != sym_val->addr;
660}
661
662static enum callback_status
663symbol_with_address(struct library_symbol *sym, void *addrptr)
664{
665	return sym->enter_addr == *(arch_addr_t *)addrptr
666		? CBS_STOP : CBS_CONT;
667}
668
669static int
670populate_this_symtab(struct process *proc, const char *filename,
671		     struct ltelf *lte, struct library *lib,
672		     Elf_Data *symtab, const char *strtab, size_t size,
673		     struct library_exported_name **names)
674{
675	/* If a valid NAMES is passed, we pass in *NAMES a list of
676	 * symbol names that this library exports.  */
677	if (names != NULL)
678		*names = NULL;
679
680	/* Using sorted array would be arguably better, but this
681	 * should be well enough for the number of symbols that we
682	 * typically deal with.  */
683	size_t num_symbols = 0;
684	struct unique_symbol *symbols = malloc(sizeof(*symbols) * size);
685	if (symbols == NULL) {
686		fprintf(stderr, "couldn't insert symbols for -x: %s\n",
687			strerror(errno));
688		return -1;
689	}
690
691	GElf_Word secflags[lte->ehdr.e_shnum];
692	size_t i;
693	for (i = 1; i < lte->ehdr.e_shnum; ++i) {
694		Elf_Scn *scn = elf_getscn(lte->elf, i);
695		GElf_Shdr shdr;
696		if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
697			secflags[i] = 0;
698		else
699			secflags[i] = shdr.sh_flags;
700	}
701
702	for (i = 0; i < size; ++i) {
703		GElf_Sym sym;
704		if (gelf_getsym(symtab, i, &sym) == NULL) {
705		fail:
706			fprintf(stderr,
707				"couldn't get symbol #%zd from %s: %s\n",
708				i, filename, elf_errmsg(-1));
709			continue;
710		}
711
712		/* XXX support IFUNC as well.  */
713		if (GELF_ST_TYPE(sym.st_info) != STT_FUNC
714		    || sym.st_value == 0
715		    || sym.st_shndx == STN_UNDEF)
716			continue;
717
718		/* Find symbol name and snip version.  */
719		const char *orig_name = strtab + sym.st_name;
720		const char *version = strchr(orig_name, '@');
721		size_t len = version != NULL ? (assert(version > orig_name),
722						(size_t)(version - orig_name))
723			: strlen(orig_name);
724		char name[len + 1];
725		memcpy(name, orig_name, len);
726		name[len] = 0;
727
728		/* If we are interested in exports, store this name.  */
729		char *name_copy = NULL;
730		if (names != NULL) {
731			struct library_exported_name *export = NULL;
732			name_copy = strdup(name);
733
734			if (name_copy == NULL
735			    || (export = malloc(sizeof(*export))) == NULL) {
736				free(name_copy);
737				fprintf(stderr, "Couldn't store symbol %s.  "
738					"Tracing may be incomplete.\n", name);
739			} else {
740				export->name = name_copy;
741				export->own_name = 1;
742				export->next = *names;
743				*names = export;
744			}
745		}
746
747		/* If the symbol is not matched, skip it.  We already
748		 * stored it to export list above.  */
749		if (!filter_matches_symbol(options.static_filter, name, lib))
750			continue;
751
752		arch_addr_t addr = (arch_addr_t)
753			(uintptr_t)(sym.st_value + lte->bias);
754		arch_addr_t naddr;
755
756		/* On arches that support OPD, the value of typical
757		 * function symbol will be a pointer to .opd, but some
758		 * will point directly to .text.  We don't want to
759		 * translate those.  */
760		if (secflags[sym.st_shndx] & SHF_EXECINSTR) {
761			naddr = addr;
762		} else if (arch_translate_address(lte, addr, &naddr) < 0) {
763			fprintf(stderr,
764				"couldn't translate address of %s@%s: %s\n",
765				name, lib->soname, strerror(errno));
766			continue;
767		}
768
769		char *full_name;
770		int own_full_name = 1;
771		if (name_copy == NULL) {
772			full_name = strdup(name);
773			if (full_name == NULL)
774				goto fail;
775		} else {
776			full_name = name_copy;
777			own_full_name = 0;
778		}
779
780		/* Look whether we already have a symbol for this
781		 * address.  If not, add this one.  */
782		struct unique_symbol key = { naddr, NULL };
783		struct unique_symbol *unique
784			= lsearch(&key, symbols, &num_symbols,
785				  sizeof(*symbols), &unique_symbol_cmp);
786
787		if (unique->libsym == NULL) {
788			struct library_symbol *libsym = malloc(sizeof(*libsym));
789			if (libsym == NULL
790			    || library_symbol_init(libsym, naddr,
791						   full_name, own_full_name,
792						   LS_TOPLT_NONE) < 0) {
793				--num_symbols;
794				goto fail;
795			}
796			unique->libsym = libsym;
797			unique->addr = naddr;
798
799		} else if (strlen(full_name) < strlen(unique->libsym->name)) {
800			library_symbol_set_name(unique->libsym,
801						full_name, own_full_name);
802
803		} else if (own_full_name) {
804			free(full_name);
805		}
806	}
807
808	/* Now we do the union of this set of unique symbols with
809	 * what's already in the library.  */
810	for (i = 0; i < num_symbols; ++i) {
811		struct library_symbol *this_sym = symbols[i].libsym;
812		assert(this_sym != NULL);
813		struct library_symbol *other
814			= library_each_symbol(lib, NULL, symbol_with_address,
815					      &this_sym->enter_addr);
816		if (other != NULL) {
817			library_symbol_destroy(this_sym);
818			free(this_sym);
819			symbols[i].libsym = NULL;
820		}
821	}
822
823	for (i = 0; i < num_symbols; ++i)
824		if (symbols[i].libsym != NULL)
825			library_add_symbol(lib, symbols[i].libsym);
826
827	free(symbols);
828	return 0;
829}
830
831static int
832populate_symtab(struct process *proc, const char *filename,
833		struct ltelf *lte, struct library *lib,
834		int symtabs, int exports)
835{
836	int status;
837	if (symtabs && lte->symtab != NULL && lte->strtab != NULL
838	    && (status = populate_this_symtab(proc, filename, lte, lib,
839					      lte->symtab, lte->strtab,
840					      lte->symtab_count, NULL)) < 0)
841		return status;
842
843	/* Check whether we want to trace symbols implemented by this
844	 * library (-l).  */
845	struct library_exported_name **names = NULL;
846	if (exports) {
847		debug(DEBUG_FUNCTION, "-l matches %s", lib->soname);
848		names = &lib->exported_names;
849	}
850
851	return populate_this_symtab(proc, filename, lte, lib,
852				    lte->dynsym, lte->dynstr,
853				    lte->dynsym_count, names);
854}
855
856static int
857read_module(struct library *lib, struct process *proc,
858	    const char *filename, GElf_Addr bias, int main)
859{
860	struct ltelf lte = {};
861	if (open_elf(&lte, filename) < 0)
862		return -1;
863
864	/* XXX When we abstract ABI into a module, this should instead
865	 * become something like
866	 *
867	 *    proc->abi = arch_get_abi(lte.ehdr);
868	 *
869	 * The code in open_elf needs to be replaced by this logic.
870	 * Be warned that libltrace.c calls open_elf as well to
871	 * determine whether ABI is supported.  This is to get
872	 * reasonable error messages when trying to run 64-bit binary
873	 * with 32-bit ltrace.  It is desirable to preserve this.  */
874	proc->e_machine = lte.ehdr.e_machine;
875	proc->e_class = lte.ehdr.e_ident[EI_CLASS];
876	get_arch_dep(proc);
877
878	/* Find out the base address.  For PIE main binaries we look
879	 * into auxv, otherwise we scan phdrs.  */
880	if (main && lte.ehdr.e_type == ET_DYN) {
881		arch_addr_t entry;
882		if (process_get_entry(proc, &entry, NULL) < 0) {
883			fprintf(stderr, "Couldn't find entry of PIE %s\n",
884				filename);
885			return -1;
886		}
887		/* XXX The double cast should be removed when
888		 * arch_addr_t becomes integral type.  */
889		lte.entry_addr = (GElf_Addr)(uintptr_t)entry;
890		lte.bias = (GElf_Addr)(uintptr_t)entry - lte.ehdr.e_entry;
891
892	} else {
893		GElf_Phdr phdr;
894		size_t i;
895		for (i = 0; gelf_getphdr (lte.elf, i, &phdr) != NULL; ++i) {
896			if (phdr.p_type == PT_LOAD) {
897				lte.base_addr = phdr.p_vaddr + bias;
898				break;
899			}
900		}
901
902		lte.bias = bias;
903		lte.entry_addr = lte.ehdr.e_entry + lte.bias;
904
905		if (lte.base_addr == 0) {
906			fprintf(stderr,
907				"Couldn't determine base address of %s\n",
908				filename);
909			return -1;
910		}
911	}
912
913	if (do_init_elf(&lte, filename) < 0)
914		return -1;
915
916	if (arch_elf_init(&lte, lib) < 0) {
917		fprintf(stderr, "Backend initialization failed.\n");
918		return -1;
919	}
920
921	int status = 0;
922	if (lib == NULL)
923		goto fail;
924
925	/* Note that we set soname and pathname as soon as they are
926	 * allocated, so in case of further errors, this get released
927	 * when LIB is released, which should happen in the caller
928	 * when we return error.  */
929
930	if (lib->pathname == NULL) {
931		char *pathname = strdup(filename);
932		if (pathname == NULL)
933			goto fail;
934		library_set_pathname(lib, pathname, 1);
935	}
936
937	if (lte.soname != NULL) {
938		char *soname = strdup(lte.soname);
939		if (soname == NULL)
940			goto fail;
941		library_set_soname(lib, soname, 1);
942	} else {
943		const char *soname = rindex(lib->pathname, '/');
944		if (soname != NULL)
945			soname += 1;
946		else
947			soname = lib->pathname;
948		library_set_soname(lib, soname, 0);
949	}
950
951	/* XXX The double cast should be removed when
952	 * arch_addr_t becomes integral type.  */
953	arch_addr_t entry = (arch_addr_t)(uintptr_t)lte.entry_addr;
954	if (arch_translate_address(&lte, entry, &entry) < 0)
955		goto fail;
956
957	/* XXX The double cast should be removed when
958	 * arch_addr_t becomes integral type.  */
959	lib->base = (arch_addr_t)(uintptr_t)lte.base_addr;
960	lib->entry = entry;
961	/* XXX The double cast should be removed when
962	 * arch_addr_t becomes integral type.  */
963	lib->dyn_addr = (arch_addr_t)(uintptr_t)lte.dyn_addr;
964
965	/* There are two reasons that we need to inspect symbol tables
966	 * or populate PLT entries.  Either the user requested
967	 * corresponding tracing features (respectively -x and -e), or
968	 * they requested tracing exported symbols (-l).
969	 *
970	 * In the latter case we need to keep even those PLT slots
971	 * that are not requested by -e (but we keep them latent).  We
972	 * also need to inspect .dynsym to find what exports this
973	 * library provide, to turn on existing latent PLT
974	 * entries.  */
975
976	int plts = filter_matches_library(options.plt_filter, lib);
977	if ((plts || options.export_filter != NULL)
978	    && populate_plt(proc, filename, &lte, lib,
979			    options.export_filter != NULL) < 0)
980		goto fail;
981
982	int exports = filter_matches_library(options.export_filter, lib);
983	int symtabs = filter_matches_library(options.static_filter, lib);
984	if ((symtabs || exports)
985	    && populate_symtab(proc, filename, &lte, lib,
986			       symtabs, exports) < 0)
987		goto fail;
988
989done:
990	do_close_elf(&lte);
991	return status;
992
993fail:
994	status = -1;
995	goto done;
996}
997
998int
999ltelf_read_library(struct library *lib, struct process *proc,
1000		   const char *filename, GElf_Addr bias)
1001{
1002	return read_module(lib, proc, filename, bias, 0);
1003}
1004
1005
1006struct library *
1007ltelf_read_main_binary(struct process *proc, const char *path)
1008{
1009	struct library *lib = malloc(sizeof(*lib));
1010	if (lib == NULL || library_init(lib, LT_LIBTYPE_MAIN) < 0) {
1011		free(lib);
1012		return NULL;
1013	}
1014	library_set_pathname(lib, path, 0);
1015
1016	/* There is a race between running the process and reading its
1017	 * binary for internal consumption.  So open the binary from
1018	 * the /proc filesystem.  XXX Note that there is similar race
1019	 * for libraries, but there we don't have a nice answer like
1020	 * that.  Presumably we could read the DSOs from the process
1021	 * memory image, but that's not currently done.  */
1022	char *fname = pid2name(proc->pid);
1023	if (fname == NULL
1024	    || read_module(lib, proc, fname, 0, 1) < 0) {
1025		library_destroy(lib);
1026		free(lib);
1027		lib = NULL;
1028	}
1029
1030	free(fname);
1031	return lib;
1032}
1033