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