Home | History | Annotate | Line # | Download | only in libexecinfo
symtab.c revision 1.5
      1 /*	$NetBSD: symtab.c,v 1.5 2016/04/20 14:00:16 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: symtab.c,v 1.5 2016/04/20 14:00:16 christos Exp $");
     33 
     34 #include <stdlib.h>
     35 #include <stdio.h>
     36 #include <string.h>
     37 #include <stdint.h>
     38 #include <stdbool.h>
     39 #include <err.h>
     40 #include <dlfcn.h>
     41 
     42 #include <libelf.h>
     43 #include <gelf.h>
     44 #ifndef ELF_ST_BIND
     45 #define ELF_ST_BIND(x)          ((x) >> 4)
     46 #endif
     47 #ifndef ELF_ST_TYPE
     48 #define ELF_ST_TYPE(x)          (((unsigned int)x) & 0xf)
     49 #endif
     50 
     51 #include "symtab.h"
     52 
     53 struct symbol {
     54 	char *st_name;
     55 	uintptr_t st_value;
     56 	uintptr_t st_info;
     57 };
     58 
     59 struct symtab {
     60 	size_t nsymbols;
     61 	struct symbol *symbols;
     62 	bool ispie;
     63 };
     64 
     65 static int
     66 address_compare(const void *a, const void *b)
     67 {
     68 	const struct symbol *sa = a;
     69 	const struct symbol *sb = b;
     70 	return (int)(intmax_t)(sa->st_value - sb->st_value);
     71 }
     72 
     73 void
     74 symtab_destroy(symtab_t *s)
     75 {
     76 	if (s == NULL)
     77 		return;
     78 	for (size_t i = 0; i < s->nsymbols; i++)
     79 		free(s->symbols[i].st_name);
     80 	free(s->symbols);
     81 	free(s);
     82 }
     83 
     84 symtab_t *
     85 symtab_create(int fd, int bind, int type)
     86 {
     87 	Elf *elf;
     88 	symtab_t *st;
     89 	Elf_Scn *scn = NULL;
     90 	GElf_Ehdr ehdr;
     91 
     92 	if (elf_version(EV_CURRENT) == EV_NONE) {
     93 		warnx("Elf Library is out of date.");
     94 		return NULL;
     95 	}
     96 
     97 	elf = elf_begin(fd, ELF_C_READ, NULL);
     98 	if (elf == NULL) {
     99 		warnx("Error opening elf file: %s", elf_errmsg(elf_errno()));
    100 		return NULL;
    101 	}
    102 	st = calloc(1, sizeof(*st));
    103 	if (st == NULL) {
    104 		warnx("Error allocating symbol table");
    105 		elf_end(elf);
    106 		return NULL;
    107 	}
    108 	if (gelf_getehdr(elf, &ehdr) == NULL) {
    109 		warnx("Error getting ELF Ehdr");
    110 		elf_end(elf);
    111 		return NULL;
    112 	}
    113 
    114 	st->ispie = ehdr.e_type == ET_DYN;
    115 
    116 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
    117 		GElf_Shdr shdr;
    118 		Elf_Data *edata;
    119 		size_t ns;
    120 		struct symbol *s;
    121 
    122 		gelf_getshdr(scn, &shdr);
    123 		if(shdr.sh_type != SHT_SYMTAB)
    124 			continue;
    125 
    126 		edata = elf_getdata(scn, NULL);
    127 		ns = shdr.sh_size / shdr.sh_entsize;
    128 		s = calloc(ns, sizeof(*s));
    129 		if (s == NULL) {
    130 			warn("Cannot allocate %zu symbols", ns);
    131 			goto out;
    132 		}
    133 		st->symbols = s;
    134 
    135 		for (size_t i = 0; i < ns; i++) {
    136 			GElf_Sym sym;
    137                         gelf_getsym(edata, (int)i, &sym);
    138 
    139 #ifdef SYMTAB_DEBUG
    140 			fprintf(stderr, "%s: %s@%#jx=%d,%d\n", __func__,
    141 			    elf_strptr(elf, shdr.sh_link, sym.st_name),
    142 			    (uintmax_t)sym.st_value, ELF_ST_BIND(sym.st_info),
    143 			    ELF_ST_TYPE(sym.st_info));
    144 #endif
    145 
    146 			if (bind != -1 &&
    147 			    (unsigned)bind != ELF_ST_BIND(sym.st_info))
    148 				continue;
    149 
    150 			if (type != -1 &&
    151 			    (unsigned)type != ELF_ST_TYPE(sym.st_info))
    152 				continue;
    153 
    154 			s->st_value = sym.st_value;
    155 			s->st_info = sym.st_info;
    156 			s->st_name = strdup(
    157 			    elf_strptr(elf, shdr.sh_link, sym.st_name));
    158 			if (s->st_name == NULL) {
    159 				warn("Cannot allocate symbol");
    160 				goto out;
    161 			}
    162 			s++;
    163                 }
    164 		st->nsymbols = s - st->symbols;
    165 		if (st->nsymbols == 0) {
    166 			warnx("No symbols found");
    167 			goto out;
    168 		}
    169 		qsort(st->symbols, st->nsymbols, sizeof(*st->symbols),
    170 		    address_compare);
    171 		elf_end(elf);
    172 		return st;
    173 	}
    174 out:
    175 	symtab_destroy(st);
    176 	elf_end(elf);
    177 	return NULL;
    178 }
    179 
    180 
    181 int
    182 symtab_find(const symtab_t *st, const void *p, Dl_info *dli)
    183 {
    184 	struct symbol *s = st->symbols;
    185 	size_t ns = st->nsymbols;
    186 	size_t hi = ns;
    187 	size_t lo = 0;
    188 	size_t mid = ns / 2;
    189 	uintptr_t fbase = st->ispie ? (uintptr_t)dli->dli_fbase : 0;
    190 	uintptr_t dd, sd, me = (uintptr_t)p - fbase;
    191 	uintptr_t ad = (uintptr_t)dli->dli_saddr - fbase;
    192 
    193 #ifdef SYMTAB_DEBUG
    194 	fprintf(stderr, "%s: [fbase=%#jx, saddr=%p, me=%#jx ad=%#jx]\n",
    195 	    __func__, (uintmax_t)fbase, dli->dli_saddr, (uintmax_t)me,
    196 	    (uintmax_t)ad);
    197 #endif
    198 	for (;;) {
    199 		if (s[mid].st_value < me)
    200 			lo = mid;
    201 		else if (s[mid].st_value > me)
    202 			hi = mid;
    203 		else
    204 			break;
    205 		if (hi - lo == 1) {
    206 			mid = lo;
    207 			break;
    208 		}
    209 		mid = (hi + lo) / 2;
    210 	}
    211 	dd = me - ad;
    212 	sd = me - s[mid].st_value;
    213 	if (dd > sd) {
    214 		dli->dli_saddr = (void *)s[mid].st_value;
    215 		dli->dli_sname = s[mid].st_name;
    216 #ifdef SYMTAB_DEBUG
    217 		fprintf(stderr, "%s: me=%#jx -> [%#jx, %s]\n", __func__,
    218 		    (uintmax_t)me, (uintmax_t)sd, dli->dli_sname);
    219 #endif
    220 	}
    221 #ifdef SYMTAB_DEBUG
    222 	else
    223 		fprintf(stderr, "%s: %#jx -> [%#jx, ***]\n", __func__,
    224 		    (uintmax_t)me, (uintmax_t)sd);
    225 #endif
    226 	return 1;
    227 }
    228