ltrace-elf.c revision 552d75e2a226782cc9ecf966e6e343af8f51031f
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	GElf_Rel rel;
552
553	if (lte->relplt->d_type == ELF_T_REL) {
554		if (gelf_getrel(lte->relplt, sym_index, &rel) == NULL)
555			return -1;
556		rela->r_offset = rel.r_offset;
557		rela->r_info = rel.r_info;
558		rela->r_addend = 0;
559
560	} else if (gelf_getrela(lte->relplt, sym_index, rela) == NULL) {
561		return -1;
562	}
563
564	if (ELF64_R_SYM(rela->r_info) >= lte->dynsym_count
565	    || gelf_getsym(lte->dynsym,
566			   ELF64_R_SYM(rela->r_info), sym) == NULL)
567		return -1;
568
569	return 0;
570}
571
572#ifndef ARCH_HAVE_GET_SYMINFO
573int
574arch_get_sym_info(struct ltelf *lte, const char *filename,
575		  size_t sym_index, GElf_Rela *rela, GElf_Sym *sym)
576{
577	return elf_get_sym_info(lte, filename, sym_index, rela, sym);
578}
579#endif
580
581static void
582mark_chain_latent(struct library_symbol *libsym)
583{
584	for (; libsym != NULL; libsym = libsym->next) {
585		debug(DEBUG_FUNCTION, "marking %s latent", libsym->name);
586		libsym->latent = 1;
587	}
588}
589
590static int
591populate_plt(struct process *proc, const char *filename,
592	     struct ltelf *lte, struct library *lib,
593	     int latent_plts)
594{
595	size_t i;
596	for (i = 0; i < lte->relplt_count; ++i) {
597		GElf_Rela rela;
598		GElf_Sym sym;
599
600		switch (arch_get_sym_info(lte, filename, i, &rela, &sym)) {
601		default:
602			fprintf(stderr,
603				"Couldn't get relocation for symbol #%zd"
604				" from \"%s\": %s\n",
605				i, filename, elf_errmsg(-1));
606			/* Fall through.  */
607		case 1:
608			continue; /* Skip this entry.  */
609		case 0:
610			break;
611		}
612
613		char const *name = lte->dynstr + sym.st_name;
614
615		/* If the symbol wasn't matched, reject it, unless we
616		 * need to keep latent PLT breakpoints for tracing
617		 * exports.  */
618		int matched = filter_matches_symbol(options.plt_filter,
619						    name, lib);
620		if (!matched && !latent_plts)
621			continue;
622
623		struct library_symbol *libsym = NULL;
624		switch (arch_elf_add_plt_entry(proc, lte, name,
625					       &rela, i, &libsym)) {
626		case PLT_DEFAULT:
627			if (default_elf_add_plt_entry(proc, lte, name,
628						      &rela, i, &libsym) < 0)
629			/* Fall through.  */
630		case PLT_FAIL:
631				return -1;
632			/* Fall through.  */
633		case PLT_OK:
634			if (libsym != NULL) {
635				/* If we are adding those symbols just
636				 * for tracing exports, mark them all
637				 * latent.  */
638				if (!matched)
639					mark_chain_latent(libsym);
640				library_add_symbol(lib, libsym);
641			}
642		}
643	}
644	return 0;
645}
646
647/* When -x rules result in request to trace several aliases, we only
648 * want to add such symbol once.  The only way that those symbols
649 * differ in is their name, e.g. in glibc you have __GI___libc_free,
650 * __cfree, __free, __libc_free, cfree and free all defined on the
651 * same address.  So instead we keep this unique symbol struct for
652 * each address, and replace name in libsym with a shorter variant if
653 * we find it.  */
654struct unique_symbol {
655	arch_addr_t addr;
656	struct library_symbol *libsym;
657};
658
659static int
660unique_symbol_cmp(const void *key, const void *val)
661{
662	const struct unique_symbol *sym_key = key;
663	const struct unique_symbol *sym_val = val;
664	return sym_key->addr != sym_val->addr;
665}
666
667static enum callback_status
668symbol_with_address(struct library_symbol *sym, void *addrptr)
669{
670	return sym->enter_addr == *(arch_addr_t *)addrptr
671		? CBS_STOP : CBS_CONT;
672}
673
674static int
675populate_this_symtab(struct process *proc, const char *filename,
676		     struct ltelf *lte, struct library *lib,
677		     Elf_Data *symtab, const char *strtab, size_t count,
678		     struct library_exported_name **names)
679{
680	/* If a valid NAMES is passed, we pass in *NAMES a list of
681	 * symbol names that this library exports.  */
682	if (names != NULL)
683		*names = NULL;
684
685	/* Using sorted array would be arguably better, but this
686	 * should be well enough for the number of symbols that we
687	 * typically deal with.  */
688	size_t num_symbols = 0;
689	struct unique_symbol *symbols = malloc(sizeof(*symbols) * count);
690	if (symbols == NULL) {
691		fprintf(stderr, "couldn't insert symbols for -x: %s\n",
692			strerror(errno));
693		return -1;
694	}
695
696	GElf_Word secflags[lte->ehdr.e_shnum];
697	size_t i;
698	for (i = 1; i < lte->ehdr.e_shnum; ++i) {
699		Elf_Scn *scn = elf_getscn(lte->elf, i);
700		GElf_Shdr shdr;
701		if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
702			secflags[i] = 0;
703		else
704			secflags[i] = shdr.sh_flags;
705	}
706
707	for (i = 0; i < count; ++i) {
708		GElf_Sym sym;
709		if (gelf_getsym(symtab, i, &sym) == NULL) {
710		fail:
711			fprintf(stderr,
712				"couldn't get symbol #%zd from %s: %s\n",
713				i, filename, elf_errmsg(-1));
714			continue;
715		}
716
717		/* XXX support IFUNC as well.  */
718		if (GELF_ST_TYPE(sym.st_info) != STT_FUNC
719		    || sym.st_value == 0
720		    || sym.st_shndx == STN_UNDEF)
721			continue;
722
723		/* Find symbol name and snip version.  */
724		const char *orig_name = strtab + sym.st_name;
725		const char *version = strchr(orig_name, '@');
726		size_t len = version != NULL ? (assert(version > orig_name),
727						(size_t)(version - orig_name))
728			: strlen(orig_name);
729		char name[len + 1];
730		memcpy(name, orig_name, len);
731		name[len] = 0;
732
733		/* If we are interested in exports, store this name.  */
734		char *name_copy = NULL;
735		if (names != NULL) {
736			struct library_exported_name *export = NULL;
737			name_copy = strdup(name);
738
739			if (name_copy == NULL
740			    || (export = malloc(sizeof(*export))) == NULL) {
741				free(name_copy);
742				fprintf(stderr, "Couldn't store symbol %s.  "
743					"Tracing may be incomplete.\n", name);
744			} else {
745				export->name = name_copy;
746				export->own_name = 1;
747				export->next = *names;
748				*names = export;
749			}
750		}
751
752		/* If the symbol is not matched, skip it.  We already
753		 * stored it to export list above.  */
754		if (!filter_matches_symbol(options.static_filter, name, lib))
755			continue;
756
757		arch_addr_t addr = (arch_addr_t)
758			(uintptr_t)(sym.st_value + lte->bias);
759		arch_addr_t naddr;
760
761		/* On arches that support OPD, the value of typical
762		 * function symbol will be a pointer to .opd, but some
763		 * will point directly to .text.  We don't want to
764		 * translate those.  */
765		if (secflags[sym.st_shndx] & SHF_EXECINSTR) {
766			naddr = addr;
767		} else if (arch_translate_address(lte, addr, &naddr) < 0) {
768			fprintf(stderr,
769				"couldn't translate address of %s@%s: %s\n",
770				name, lib->soname, strerror(errno));
771			continue;
772		}
773
774		char *full_name;
775		int own_full_name = 1;
776		if (name_copy == NULL) {
777			full_name = strdup(name);
778			if (full_name == NULL)
779				goto fail;
780		} else {
781			full_name = name_copy;
782			own_full_name = 0;
783		}
784
785		/* Look whether we already have a symbol for this
786		 * address.  If not, add this one.  */
787		struct unique_symbol key = { naddr, NULL };
788		struct unique_symbol *unique
789			= lsearch(&key, symbols, &num_symbols,
790				  sizeof(*symbols), &unique_symbol_cmp);
791
792		if (unique->libsym == NULL) {
793			struct library_symbol *libsym = malloc(sizeof(*libsym));
794			if (libsym == NULL
795			    || library_symbol_init(libsym, naddr,
796						   full_name, own_full_name,
797						   LS_TOPLT_NONE) < 0) {
798				--num_symbols;
799				goto fail;
800			}
801			unique->libsym = libsym;
802			unique->addr = naddr;
803
804		} else if (strlen(full_name) < strlen(unique->libsym->name)) {
805			library_symbol_set_name(unique->libsym,
806						full_name, own_full_name);
807
808		} else if (own_full_name) {
809			free(full_name);
810		}
811	}
812
813	/* Now we do the union of this set of unique symbols with
814	 * what's already in the library.  */
815	for (i = 0; i < num_symbols; ++i) {
816		struct library_symbol *this_sym = symbols[i].libsym;
817		assert(this_sym != NULL);
818		struct library_symbol *other
819			= library_each_symbol(lib, NULL, symbol_with_address,
820					      &this_sym->enter_addr);
821		if (other != NULL) {
822			library_symbol_destroy(this_sym);
823			free(this_sym);
824			symbols[i].libsym = NULL;
825		}
826	}
827
828	for (i = 0; i < num_symbols; ++i)
829		if (symbols[i].libsym != NULL)
830			library_add_symbol(lib, symbols[i].libsym);
831
832	free(symbols);
833	return 0;
834}
835
836static int
837populate_symtab(struct process *proc, const char *filename,
838		struct ltelf *lte, struct library *lib,
839		int symtabs, int exports)
840{
841	int status;
842	if (symtabs && lte->symtab != NULL && lte->strtab != NULL
843	    && (status = populate_this_symtab(proc, filename, lte, lib,
844					      lte->symtab, lte->strtab,
845					      lte->symtab_count, NULL)) < 0)
846		return status;
847
848	/* Check whether we want to trace symbols implemented by this
849	 * library (-l).  */
850	struct library_exported_name **names = NULL;
851	if (exports) {
852		debug(DEBUG_FUNCTION, "-l matches %s", lib->soname);
853		names = &lib->exported_names;
854	}
855
856	return populate_this_symtab(proc, filename, lte, lib,
857				    lte->dynsym, lte->dynstr,
858				    lte->dynsym_count, names);
859}
860
861static int
862read_module(struct library *lib, struct process *proc,
863	    const char *filename, GElf_Addr bias, int main)
864{
865	struct ltelf lte = {};
866	if (open_elf(&lte, filename) < 0)
867		return -1;
868
869	/* XXX When we abstract ABI into a module, this should instead
870	 * become something like
871	 *
872	 *    proc->abi = arch_get_abi(lte.ehdr);
873	 *
874	 * The code in open_elf needs to be replaced by this logic.
875	 * Be warned that libltrace.c calls open_elf as well to
876	 * determine whether ABI is supported.  This is to get
877	 * reasonable error messages when trying to run 64-bit binary
878	 * with 32-bit ltrace.  It is desirable to preserve this.  */
879	proc->e_machine = lte.ehdr.e_machine;
880	proc->e_class = lte.ehdr.e_ident[EI_CLASS];
881	get_arch_dep(proc);
882
883	/* Find out the base address.  For PIE main binaries we look
884	 * into auxv, otherwise we scan phdrs.  */
885	if (main && lte.ehdr.e_type == ET_DYN) {
886		arch_addr_t entry;
887		if (process_get_entry(proc, &entry, NULL) < 0) {
888			fprintf(stderr, "Couldn't find entry of PIE %s\n",
889				filename);
890			return -1;
891		}
892		/* XXX The double cast should be removed when
893		 * arch_addr_t becomes integral type.  */
894		lte.entry_addr = (GElf_Addr)(uintptr_t)entry;
895		lte.bias = (GElf_Addr)(uintptr_t)entry - lte.ehdr.e_entry;
896
897	} else {
898		GElf_Phdr phdr;
899		size_t i;
900		for (i = 0; gelf_getphdr (lte.elf, i, &phdr) != NULL; ++i) {
901			if (phdr.p_type == PT_LOAD) {
902				lte.base_addr = phdr.p_vaddr + bias;
903				break;
904			}
905		}
906
907		lte.bias = bias;
908		lte.entry_addr = lte.ehdr.e_entry + lte.bias;
909
910		if (lte.base_addr == 0) {
911			fprintf(stderr,
912				"Couldn't determine base address of %s\n",
913				filename);
914			return -1;
915		}
916	}
917
918	if (do_init_elf(&lte, filename) < 0)
919		return -1;
920
921	if (arch_elf_init(&lte, lib) < 0) {
922		fprintf(stderr, "Backend initialization failed.\n");
923		return -1;
924	}
925
926	int status = 0;
927	if (lib == NULL)
928		goto fail;
929
930	/* Note that we set soname and pathname as soon as they are
931	 * allocated, so in case of further errors, this get released
932	 * when LIB is released, which should happen in the caller
933	 * when we return error.  */
934
935	if (lib->pathname == NULL) {
936		char *pathname = strdup(filename);
937		if (pathname == NULL)
938			goto fail;
939		library_set_pathname(lib, pathname, 1);
940	}
941
942	if (lte.soname != NULL) {
943		char *soname = strdup(lte.soname);
944		if (soname == NULL)
945			goto fail;
946		library_set_soname(lib, soname, 1);
947	} else {
948		const char *soname = rindex(lib->pathname, '/');
949		if (soname != NULL)
950			soname += 1;
951		else
952			soname = lib->pathname;
953		library_set_soname(lib, soname, 0);
954	}
955
956	/* XXX The double cast should be removed when
957	 * arch_addr_t becomes integral type.  */
958	arch_addr_t entry = (arch_addr_t)(uintptr_t)lte.entry_addr;
959	if (arch_translate_address(&lte, entry, &entry) < 0)
960		goto fail;
961
962	/* XXX The double cast should be removed when
963	 * arch_addr_t becomes integral type.  */
964	lib->base = (arch_addr_t)(uintptr_t)lte.base_addr;
965	lib->entry = entry;
966	/* XXX The double cast should be removed when
967	 * arch_addr_t becomes integral type.  */
968	lib->dyn_addr = (arch_addr_t)(uintptr_t)lte.dyn_addr;
969
970	/* There are two reasons that we need to inspect symbol tables
971	 * or populate PLT entries.  Either the user requested
972	 * corresponding tracing features (respectively -x and -e), or
973	 * they requested tracing exported symbols (-l).
974	 *
975	 * In the latter case we need to keep even those PLT slots
976	 * that are not requested by -e (but we keep them latent).  We
977	 * also need to inspect .dynsym to find what exports this
978	 * library provide, to turn on existing latent PLT
979	 * entries.  */
980
981	int plts = filter_matches_library(options.plt_filter, lib);
982	if ((plts || options.export_filter != NULL)
983	    && populate_plt(proc, filename, &lte, lib,
984			    options.export_filter != NULL) < 0)
985		goto fail;
986
987	int exports = filter_matches_library(options.export_filter, lib);
988	int symtabs = filter_matches_library(options.static_filter, lib);
989	if ((symtabs || exports)
990	    && populate_symtab(proc, filename, &lte, lib,
991			       symtabs, exports) < 0)
992		goto fail;
993
994done:
995	do_close_elf(&lte);
996	return status;
997
998fail:
999	status = -1;
1000	goto done;
1001}
1002
1003int
1004ltelf_read_library(struct library *lib, struct process *proc,
1005		   const char *filename, GElf_Addr bias)
1006{
1007	return read_module(lib, proc, filename, bias, 0);
1008}
1009
1010
1011struct library *
1012ltelf_read_main_binary(struct process *proc, const char *path)
1013{
1014	struct library *lib = malloc(sizeof(*lib));
1015	if (lib == NULL || library_init(lib, LT_LIBTYPE_MAIN) < 0) {
1016		free(lib);
1017		return NULL;
1018	}
1019	library_set_pathname(lib, path, 0);
1020
1021	/* There is a race between running the process and reading its
1022	 * binary for internal consumption.  So open the binary from
1023	 * the /proc filesystem.  XXX Note that there is similar race
1024	 * for libraries, but there we don't have a nice answer like
1025	 * that.  Presumably we could read the DSOs from the process
1026	 * memory image, but that's not currently done.  */
1027	char *fname = pid2name(proc->pid);
1028	if (fname == NULL
1029	    || read_module(lib, proc, fname, 0, 1) < 0) {
1030		library_destroy(lib);
1031		free(lib);
1032		lib = NULL;
1033	}
1034
1035	free(fname);
1036	return lib;
1037}
1038