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