ltrace-elf.c revision 4d4e1b853db0c97b7c7f023bc301e3c3eee58ce4
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#ifdef PLT_REINITALISATION_BP
54extern char *PLTs_initialized_by_here;
55#endif
56
57#ifndef ARCH_HAVE_LTELF_DATA
58int
59arch_elf_init(struct ltelf *lte, struct library *lib)
60{
61	return 0;
62}
63
64void
65arch_elf_destroy(struct ltelf *lte)
66{
67}
68#endif
69
70int
71default_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
72			  const char *a_name, GElf_Rela *rela, size_t ndx,
73			  struct library_symbol **ret)
74{
75	char *name = strdup(a_name);
76	if (name == NULL) {
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;
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	*ret = libsym;
99	return 0;
100}
101
102#ifndef ARCH_HAVE_ADD_PLT_ENTRY
103enum plt_status
104arch_elf_add_plt_entry(struct Process *proc, struct ltelf *lte,
105		       const char *a_name, GElf_Rela *rela, size_t ndx,
106		       struct library_symbol **ret)
107{
108	return plt_default;
109}
110#endif
111
112Elf_Data *
113elf_loaddata(Elf_Scn *scn, GElf_Shdr *shdr)
114{
115	Elf_Data *data = elf_getdata(scn, NULL);
116	if (data == NULL || elf_getdata(scn, data) != NULL
117	    || data->d_off || data->d_size != shdr->sh_size)
118		return NULL;
119	return data;
120}
121
122static int
123elf_get_section_if(struct ltelf *lte, Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr,
124		   int (*predicate)(Elf_Scn *, GElf_Shdr *, void *data),
125		   void *data)
126{
127	int i;
128	for (i = 1; i < lte->ehdr.e_shnum; ++i) {
129		Elf_Scn *scn;
130		GElf_Shdr shdr;
131
132		scn = elf_getscn(lte->elf, i);
133		if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
134			debug(1, "Couldn't read section or header.");
135			return -1;
136		}
137		if (predicate(scn, &shdr, data)) {
138			*tgt_sec = scn;
139			*tgt_shdr = shdr;
140			return 0;
141		}
142	}
143	return -1;
144
145}
146
147static int
148inside_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
149{
150	GElf_Addr addr = *(GElf_Addr *)data;
151	return addr >= shdr->sh_addr
152		&& addr < shdr->sh_addr + shdr->sh_size;
153}
154
155int
156elf_get_section_covering(struct ltelf *lte, GElf_Addr addr,
157			 Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
158{
159	return elf_get_section_if(lte, tgt_sec, tgt_shdr,
160				  &inside_p, &addr);
161}
162
163static int
164type_p(Elf_Scn *scn, GElf_Shdr *shdr, void *data)
165{
166	GElf_Word type = *(GElf_Word *)data;
167	return shdr->sh_type == type;
168}
169
170int
171elf_get_section_type(struct ltelf *lte, GElf_Word type,
172		     Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
173{
174	return elf_get_section_if(lte, tgt_sec, tgt_shdr,
175				  &type_p, &type);
176}
177
178struct section_named_data {
179	struct ltelf *lte;
180	const char *name;
181};
182
183static int
184name_p(Elf_Scn *scn, GElf_Shdr *shdr, void *d)
185{
186	struct section_named_data *data = d;
187	const char *name = elf_strptr(data->lte->elf,
188				      data->lte->ehdr.e_shstrndx,
189				      shdr->sh_name);
190	return strcmp(name, data->name) == 0;
191}
192
193int
194elf_get_section_named(struct ltelf *lte, const char *name,
195		     Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr)
196{
197	struct section_named_data data = {
198		.lte = lte,
199		.name = name,
200	};
201	return elf_get_section_if(lte, tgt_sec, tgt_shdr,
202				  &name_p, &data);
203}
204
205static int
206need_data(Elf_Data *data, GElf_Xword offset, GElf_Xword size)
207{
208	assert(data != NULL);
209	if (data->d_size < size || offset > data->d_size - size) {
210		debug(1, "Not enough data to read %"PRId64"-byte value"
211		      " at offset %"PRId64".", size, offset);
212		return -1;
213	}
214	return 0;
215}
216
217#define DEF_READER(NAME, SIZE)						\
218	int								\
219	NAME(Elf_Data *data, GElf_Xword offset, uint##SIZE##_t *retp)	\
220	{								\
221		if (!need_data(data, offset, SIZE / 8) < 0)		\
222			return -1;					\
223									\
224		if (data->d_buf == NULL) /* NODATA section */ {		\
225			*retp = 0;					\
226			return 0;					\
227		}							\
228									\
229		union {							\
230			uint##SIZE##_t dst;				\
231			char buf[0];					\
232		} u;							\
233		memcpy(u.buf, data->d_buf + offset, sizeof(u.dst));	\
234		*retp = u.dst;						\
235		return 0;						\
236	}
237
238DEF_READER(elf_read_u16, 16)
239DEF_READER(elf_read_u32, 32)
240DEF_READER(elf_read_u64, 64)
241
242#undef DEF_READER
243
244int
245open_elf(struct ltelf *lte, const char *filename)
246{
247	lte->fd = open(filename, O_RDONLY);
248	if (lte->fd == -1)
249		return 1;
250
251	elf_version(EV_CURRENT);
252
253#ifdef HAVE_ELF_C_READ_MMAP
254	lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
255#else
256	lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
257#endif
258
259	if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) {
260		fprintf(stderr, "\"%s\" is not an ELF file\n", filename);
261		exit(EXIT_FAILURE);
262	}
263
264	if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL) {
265		fprintf(stderr, "can't read ELF header of \"%s\": %s\n",
266			filename, elf_errmsg(-1));
267		exit(EXIT_FAILURE);
268	}
269
270	if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) {
271		fprintf(stderr, "\"%s\" is neither an ELF executable"
272			" nor a shared library\n", filename);
273		exit(EXIT_FAILURE);
274	}
275
276	if (1
277#ifdef LT_ELF_MACHINE
278	    && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
279		|| lte->ehdr.e_machine != LT_ELF_MACHINE)
280#endif
281#ifdef LT_ELF_MACHINE2
282	    && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
283		|| lte->ehdr.e_machine != LT_ELF_MACHINE2)
284#endif
285#ifdef LT_ELF_MACHINE3
286	    && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
287		|| lte->ehdr.e_machine != LT_ELF_MACHINE3)
288#endif
289		) {
290		fprintf(stderr,
291			"\"%s\" is ELF from incompatible architecture\n",
292			filename);
293		exit(EXIT_FAILURE);
294	}
295
296	return 0;
297}
298
299static void
300read_symbol_table(struct ltelf *lte, const char *filename,
301		  Elf_Scn *scn, GElf_Shdr *shdr, const char *name,
302		  Elf_Data **datap, size_t *countp, const char **strsp)
303{
304	*datap = elf_getdata(scn, NULL);
305	*countp = shdr->sh_size / shdr->sh_entsize;
306	if ((*datap == NULL || elf_getdata(scn, *datap) != NULL)
307	    && options.static_filter != NULL) {
308		fprintf(stderr, "Couldn't get data of section"
309			" %s from \"%s\": %s\n",
310			name, filename, elf_errmsg(-1));
311		exit(EXIT_FAILURE);
312	}
313
314	scn = elf_getscn(lte->elf, shdr->sh_link);
315	GElf_Shdr shdr2;
316	if (scn == NULL || gelf_getshdr(scn, &shdr2) == NULL) {
317		fprintf(stderr, "Couldn't get header of section"
318			" #%d from \"%s\": %s\n",
319			shdr2.sh_link, filename, elf_errmsg(-1));
320		exit(EXIT_FAILURE);
321	}
322
323	Elf_Data *data = elf_getdata(scn, NULL);
324	if (data == NULL || elf_getdata(scn, data) != NULL
325	    || shdr2.sh_size != data->d_size || data->d_off) {
326		fprintf(stderr, "Couldn't get data of section"
327			" #%d from \"%s\": %s\n",
328			shdr2.sh_link, filename, elf_errmsg(-1));
329		exit(EXIT_FAILURE);
330	}
331
332	*strsp = data->d_buf;
333}
334
335static int
336do_init_elf(struct ltelf *lte, const char *filename, GElf_Addr bias)
337{
338	int i;
339	GElf_Addr relplt_addr = 0;
340	GElf_Addr soname_offset = 0;
341
342	debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename);
343	debug(1, "Reading ELF from %s...", filename);
344
345	if (open_elf(lte, filename) < 0)
346		return -1;
347
348	/* Find out the base address.  */
349	{
350		GElf_Phdr phdr;
351		for (i = 0; gelf_getphdr (lte->elf, i, &phdr) != NULL; ++i) {
352			if (phdr.p_type == PT_LOAD) {
353				lte->base_addr = phdr.p_vaddr + bias;
354				break;
355			}
356		}
357	}
358
359	if (lte->base_addr == 0) {
360		fprintf(stderr, "Couldn't determine base address of %s\n",
361			filename);
362		return -1;
363	}
364
365	lte->bias = bias;
366	lte->entry_addr = lte->ehdr.e_entry + lte->bias;
367
368	for (i = 1; i < lte->ehdr.e_shnum; ++i) {
369		Elf_Scn *scn;
370		GElf_Shdr shdr;
371		const char *name;
372
373		scn = elf_getscn(lte->elf, i);
374		if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
375			fprintf(stderr,	"Couldn't get section #%d from"
376				" \"%s\": %s\n", i, filename, elf_errmsg(-1));
377			exit(EXIT_FAILURE);
378		}
379
380		name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
381		if (name == NULL) {
382			fprintf(stderr,	"Couldn't get name of section #%d from"
383				" \"%s\": %s\n", i, filename, elf_errmsg(-1));
384			exit(EXIT_FAILURE);
385		}
386
387		if (shdr.sh_type == SHT_SYMTAB) {
388			read_symbol_table(lte, filename,
389					  scn, &shdr, name, &lte->symtab,
390					  &lte->symtab_count, &lte->strtab);
391
392		} else if (shdr.sh_type == SHT_DYNSYM) {
393			read_symbol_table(lte, filename,
394					  scn, &shdr, name, &lte->dynsym,
395					  &lte->dynsym_count, &lte->dynstr);
396
397		} else if (shdr.sh_type == SHT_DYNAMIC) {
398			Elf_Data *data;
399			size_t j;
400
401			lte->dyn_addr = shdr.sh_addr;
402			lte->dyn_sz = shdr.sh_size;
403
404			data = elf_getdata(scn, NULL);
405			if (data == NULL || elf_getdata(scn, data) != NULL) {
406				fprintf(stderr, "Couldn't get .dynamic data"
407					" from \"%s\": %s\n",
408					filename, strerror(errno));
409				exit(EXIT_FAILURE);
410			}
411
412			for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
413				GElf_Dyn dyn;
414
415				if (gelf_getdyn(data, j, &dyn) == NULL) {
416					fprintf(stderr, "Couldn't get .dynamic"
417						" data from \"%s\": %s\n",
418						filename, strerror(errno));
419					exit(EXIT_FAILURE);
420				}
421				if (dyn.d_tag == DT_JMPREL)
422					relplt_addr = dyn.d_un.d_ptr;
423				else if (dyn.d_tag == DT_PLTRELSZ)
424					lte->relplt_size = dyn.d_un.d_val;
425				else if (dyn.d_tag == DT_SONAME)
426					soname_offset = dyn.d_un.d_val;
427			}
428		} else if (shdr.sh_type == SHT_PROGBITS
429			   || shdr.sh_type == SHT_NOBITS) {
430			if (strcmp(name, ".plt") == 0) {
431				lte->plt_addr = shdr.sh_addr;
432				lte->plt_size = shdr.sh_size;
433				lte->plt_data = elf_loaddata(scn, &shdr);
434				if (lte->plt_data == NULL)
435					fprintf(stderr,
436						"Can't load .plt data\n");
437				lte->plt_flags = shdr.sh_flags;
438			}
439#ifdef ARCH_SUPPORTS_OPD
440			else if (strcmp(name, ".opd") == 0) {
441				lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
442				lte->opd_size = shdr.sh_size;
443				lte->opd = elf_rawdata(scn, NULL);
444			}
445#endif
446		}
447	}
448
449	if (lte->dynsym == NULL || lte->dynstr == NULL) {
450		fprintf(stderr, "Couldn't find .dynsym or .dynstr in \"%s\"\n",
451			filename);
452		exit(EXIT_FAILURE);
453	}
454
455	if (!relplt_addr || !lte->plt_addr) {
456		debug(1, "%s has no PLT relocations", filename);
457		lte->relplt = NULL;
458		lte->relplt_count = 0;
459	} else if (lte->relplt_size == 0) {
460		debug(1, "%s has unknown PLT size", filename);
461		lte->relplt = NULL;
462		lte->relplt_count = 0;
463	} else {
464
465		for (i = 1; i < lte->ehdr.e_shnum; ++i) {
466			Elf_Scn *scn;
467			GElf_Shdr shdr;
468
469			scn = elf_getscn(lte->elf, i);
470			if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) {
471				fprintf(stderr, "Couldn't get section header"
472					" from \"%s\": %s\n",
473					filename, elf_errmsg(-1));
474				exit(EXIT_FAILURE);
475			}
476			if (shdr.sh_addr == relplt_addr
477			    && shdr.sh_size == lte->relplt_size) {
478				lte->relplt = elf_getdata(scn, NULL);
479				lte->relplt_count =
480				    shdr.sh_size / shdr.sh_entsize;
481				if (lte->relplt == NULL
482				    || elf_getdata(scn, lte->relplt) != NULL) {
483					fprintf(stderr, "Couldn't get .rel*.plt"
484						" data from \"%s\": %s\n",
485						filename, elf_errmsg(-1));
486					exit(EXIT_FAILURE);
487				}
488				break;
489			}
490		}
491
492		if (i == lte->ehdr.e_shnum) {
493			fprintf(stderr,
494				"Couldn't find .rel*.plt section in \"%s\"\n",
495				filename);
496			exit(EXIT_FAILURE);
497		}
498
499		debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
500	}
501
502	if (soname_offset != 0)
503		lte->soname = lte->dynstr + soname_offset;
504
505	return 0;
506}
507
508/* XXX temporarily non-static */
509void
510do_close_elf(struct ltelf *lte) {
511	debug(DEBUG_FUNCTION, "do_close_elf()");
512	arch_elf_destroy(lte);
513	elf_end(lte->elf);
514	close(lte->fd);
515}
516
517static int
518populate_plt(struct Process *proc, const char *filename,
519	     struct ltelf *lte, struct library *lib)
520{
521	size_t i;
522	for (i = 0; i < lte->relplt_count; ++i) {
523		GElf_Rel rel;
524		GElf_Rela rela;
525		GElf_Sym sym;
526		void *ret;
527
528		if (lte->relplt->d_type == ELF_T_REL) {
529			ret = gelf_getrel(lte->relplt, i, &rel);
530			rela.r_offset = rel.r_offset;
531			rela.r_info = rel.r_info;
532			rela.r_addend = 0;
533		} else {
534			ret = gelf_getrela(lte->relplt, i, &rela);
535		}
536
537		if (ret == NULL
538		    || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count
539		    || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info),
540				   &sym) == NULL) {
541			fprintf(stderr,
542				"Couldn't get relocation from \"%s\": %s\n",
543				filename, elf_errmsg(-1));
544			exit(EXIT_FAILURE);
545		}
546
547		char const *name = lte->dynstr + sym.st_name;
548
549		if (!filter_matches_symbol(options.plt_filter, name, lib))
550			continue;
551
552		struct library_symbol *libsym = NULL;
553		switch (arch_elf_add_plt_entry(proc, lte, name,
554					       &rela, i, &libsym)) {
555		case plt_default:
556			if (default_elf_add_plt_entry(proc, lte, name,
557						      &rela, i, &libsym) < 0)
558			/* fall-through */
559		case plt_fail:
560				return -1;
561			/* fall-through */
562		case plt_ok:
563			if (libsym != NULL)
564				library_add_symbol(lib, libsym);
565		}
566	}
567	return 0;
568}
569
570/* When -x rules result in request to trace several aliases, we only
571 * want to add such symbol once.  The only way that those symbols
572 * differ in is their name, e.g. in glibc you have __GI___libc_free,
573 * __cfree, __free, __libc_free, cfree and free all defined on the
574 * same address.  So instead we keep this unique symbol struct for
575 * each address, and replace name in libsym with a shorter variant if
576 * we find it.  */
577struct unique_symbol {
578	arch_addr_t addr;
579	struct library_symbol *libsym;
580};
581
582static int
583unique_symbol_cmp(const void *key, const void *val)
584{
585	const struct unique_symbol *sym_key = key;
586	const struct unique_symbol *sym_val = val;
587	return sym_key->addr != sym_val->addr;
588}
589
590static int
591populate_this_symtab(struct Process *proc, const char *filename,
592		     struct ltelf *lte, struct library *lib,
593		     Elf_Data *symtab, const char *strtab, size_t size)
594{
595	/* Using sorted array would be arguably better, but this
596	 * should be well enough for the number of symbols that we
597	 * typically deal with.  */
598	size_t num_symbols = 0;
599	struct unique_symbol *symbols = malloc(sizeof(*symbols) * size);
600	if (symbols == NULL) {
601		fprintf(stderr, "couldn't insert symbols for -x: %s\n",
602			strerror(errno));
603		return -1;
604	}
605
606	GElf_Word secflags[lte->ehdr.e_shnum];
607	size_t i;
608	for (i = 1; i < lte->ehdr.e_shnum; ++i) {
609		Elf_Scn *scn = elf_getscn(lte->elf, i);
610		if (scn == NULL)
611			continue;
612		GElf_Shdr shdr;
613		if (gelf_getshdr(scn, &shdr) == NULL)
614			continue;
615		secflags[i] = shdr.sh_flags;
616	}
617
618	size_t lib_len = strlen(lib->soname);
619	for (i = 0; i < size; ++i) {
620		GElf_Sym sym;
621		if (gelf_getsym(symtab, i, &sym) == NULL) {
622		fail:
623			fprintf(stderr,
624				"couldn't get symbol #%zd from %s: %s\n",
625				i, filename, elf_errmsg(-1));
626			continue;
627		}
628
629		/* XXX support IFUNC as well.  */
630		if (GELF_ST_TYPE(sym.st_info) != STT_FUNC
631		    || sym.st_value == 0
632		    || sym.st_shndx == STN_UNDEF)
633			continue;
634
635		const char *orig_name = strtab + sym.st_name;
636		const char *version = strchr(orig_name, '@');
637		size_t len = version != NULL ? (assert(version > orig_name),
638						(size_t)(version - orig_name))
639			: strlen(orig_name);
640		char name[len + 1];
641		memcpy(name, orig_name, len);
642		name[len] = 0;
643
644		if (!filter_matches_symbol(options.static_filter, name, lib))
645			continue;
646
647		arch_addr_t addr = (arch_addr_t)
648			(uintptr_t)(sym.st_value + lte->bias);
649		arch_addr_t naddr;
650
651		/* On arches that support OPD, the value of typical
652		 * function symbol will be a pointer to .opd, but some
653		 * will point directly to .text.  We don't want to
654		 * translate those.  */
655		if (secflags[sym.st_shndx] & SHF_EXECINSTR) {
656			naddr = addr;
657		} else if (arch_translate_address(lte, addr, &naddr) < 0) {
658			fprintf(stderr,
659				"couldn't translate address of %s@%s: %s\n",
660				name, lib->soname, strerror(errno));
661			continue;
662		}
663
664		char *full_name;
665		if (lib->type != LT_LIBTYPE_MAIN) {
666			full_name = malloc(strlen(name) + 1 + lib_len + 1);
667			if (full_name == NULL)
668				goto fail;
669			sprintf(full_name, "%s@%s", name, lib->soname);
670		} else {
671			full_name = strdup(name);
672			if (full_name == NULL)
673				goto fail;
674		}
675
676		/* Look whether we already have a symbol for this
677		 * address.  If not, add this one.  */
678		struct unique_symbol key = { naddr, NULL };
679		struct unique_symbol *unique
680			= lsearch(&key, symbols, &num_symbols,
681				  sizeof(*symbols), &unique_symbol_cmp);
682
683		if (unique->libsym == NULL) {
684			struct library_symbol *libsym = malloc(sizeof(*libsym));
685			if (libsym == NULL
686			    || library_symbol_init(libsym, naddr, full_name,
687						   1, LS_TOPLT_NONE) < 0) {
688				--num_symbols;
689				goto fail;
690			}
691			unique->libsym = libsym;
692			unique->addr = naddr;
693
694		} else if (strlen(full_name) < strlen(unique->libsym->name)) {
695			library_symbol_set_name(unique->libsym, full_name, 1);
696
697		} else {
698			free(full_name);
699		}
700	}
701
702	for (i = 0; i < num_symbols; ++i) {
703		assert(symbols[i].libsym != NULL);
704		library_add_symbol(lib, symbols[i].libsym);
705	}
706
707	free(symbols);
708
709	return 0;
710}
711
712static int
713populate_symtab(struct Process *proc, const char *filename,
714		struct ltelf *lte, struct library *lib)
715{
716	if (lte->symtab != NULL && lte->strtab != NULL)
717		return populate_this_symtab(proc, filename, lte, lib,
718					    lte->symtab, lte->strtab,
719					    lte->symtab_count);
720	else
721		return populate_this_symtab(proc, filename, lte, lib,
722					    lte->dynsym, lte->dynstr,
723					    lte->dynsym_count);
724}
725
726int
727ltelf_read_library(struct library *lib, struct Process *proc,
728		   const char *filename, GElf_Addr bias)
729{
730	struct ltelf lte = {};
731	if (do_init_elf(&lte, filename, bias) < 0)
732		return -1;
733	if (arch_elf_init(&lte, lib) < 0) {
734		fprintf(stderr, "Backend initialization failed.\n");
735		return -1;
736	}
737
738	proc->e_machine = lte.ehdr.e_machine;
739	proc->e_class = lte.ehdr.e_ident[EI_CLASS];
740
741	int status = 0;
742	if (lib == NULL)
743		goto fail;
744
745	/* Note that we set soname and pathname as soon as they are
746	 * allocated, so in case of further errors, this get released
747	 * when LIB is release, which should happen in the caller when
748	 * we return error.  */
749
750	if (lib->pathname == NULL) {
751		char *pathname = strdup(filename);
752		if (pathname == NULL)
753			goto fail;
754		library_set_pathname(lib, pathname, 1);
755	}
756
757	if (lte.soname != NULL) {
758		char *soname = strdup(lte.soname);
759		if (soname == NULL)
760			goto fail;
761		library_set_soname(lib, soname, 1);
762	} else {
763		const char *soname = rindex(lib->pathname, '/') + 1;
764		if (soname == NULL)
765			soname = lib->pathname;
766		library_set_soname(lib, soname, 0);
767	}
768
769	/* XXX The double cast should be removed when
770	 * arch_addr_t becomes integral type.  */
771	arch_addr_t entry = (arch_addr_t)(uintptr_t)lte.entry_addr;
772	if (arch_translate_address(&lte, entry, &entry) < 0)
773		goto fail;
774
775	/* XXX The double cast should be removed when
776	 * arch_addr_t becomes integral type.  */
777	lib->base = (arch_addr_t)(uintptr_t)lte.base_addr;
778	lib->entry = entry;
779	/* XXX The double cast should be removed when
780	 * arch_addr_t becomes integral type.  */
781	lib->dyn_addr = (arch_addr_t)(uintptr_t)lte.dyn_addr;
782
783	if (filter_matches_library(options.plt_filter, lib)
784	    && populate_plt(proc, filename, &lte, lib) < 0)
785		goto fail;
786
787	if (filter_matches_library(options.static_filter, lib)
788	    && populate_symtab(proc, filename, &lte, lib) < 0)
789		goto fail;
790
791done:
792	do_close_elf(&lte);
793	return status;
794
795fail:
796	status = -1;
797	goto done;
798}
799
800struct library *
801ltelf_read_main_binary(struct Process *proc, const char *path)
802{
803	struct library *lib = malloc(sizeof(*lib));
804	if (lib == NULL)
805		return NULL;
806	library_init(lib, LT_LIBTYPE_MAIN);
807	library_set_pathname(lib, path, 0);
808
809	/* There is a race between running the process and reading its
810	 * binary for internal consumption.  So open the binary from
811	 * the /proc filesystem.  XXX Note that there is similar race
812	 * for libraries, but there we don't have a nice answer like
813	 * that.  Presumably we could read the DSOs from the process
814	 * memory image, but that's not currently done.  */
815	char *fname = pid2name(proc->pid);
816	if (ltelf_read_library(lib, proc, fname, 0) < 0) {
817		library_destroy(lib);
818		free(lib);
819		return NULL;
820	}
821
822	return lib;
823}
824