1*f357c00bSEd Maste /* $NetBSD: symtab.c,v 1.2 2013/08/29 15:01:57 christos Exp $ */ 2287472b3SEd Maste 3287472b3SEd Maste /*- 4287472b3SEd Maste * Copyright (c) 2012 The NetBSD Foundation, Inc. 5287472b3SEd Maste * All rights reserved. 6287472b3SEd Maste * 7287472b3SEd Maste * This code is derived from software contributed to The NetBSD Foundation 8287472b3SEd Maste * by Christos Zoulas. 9287472b3SEd Maste * 10287472b3SEd Maste * Redistribution and use in source and binary forms, with or without 11287472b3SEd Maste * modification, are permitted provided that the following conditions 12287472b3SEd Maste * are met: 13287472b3SEd Maste * 1. Redistributions of source code must retain the above copyright 14287472b3SEd Maste * notice, this list of conditions and the following disclaimer. 15287472b3SEd Maste * 2. Redistributions in binary form must reproduce the above copyright 16287472b3SEd Maste * notice, this list of conditions and the following disclaimer in the 17287472b3SEd Maste * documentation and/or other materials provided with the distribution. 18287472b3SEd Maste * 19287472b3SEd Maste * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20287472b3SEd Maste * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21287472b3SEd Maste * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22287472b3SEd Maste * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23287472b3SEd Maste * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24287472b3SEd Maste * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25287472b3SEd Maste * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26287472b3SEd Maste * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27287472b3SEd Maste * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28287472b3SEd Maste * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29287472b3SEd Maste * POSSIBILITY OF SUCH DAMAGE. 30287472b3SEd Maste */ 31287472b3SEd Maste #include <sys/cdefs.h> 32*f357c00bSEd Maste __RCSID("$NetBSD: symtab.c,v 1.2 2013/08/29 15:01:57 christos Exp $"); 33287472b3SEd Maste 34287472b3SEd Maste #include <stdlib.h> 35287472b3SEd Maste #include <stdio.h> 36287472b3SEd Maste #include <string.h> 37*f357c00bSEd Maste #include <stdint.h> 38287472b3SEd Maste #include <err.h> 39287472b3SEd Maste #include <dlfcn.h> 40287472b3SEd Maste 41287472b3SEd Maste #include <libelf.h> 42287472b3SEd Maste #include <gelf.h> 43287472b3SEd Maste #ifndef ELF_ST_BIND 44287472b3SEd Maste #define ELF_ST_BIND(x) ((x) >> 4) 45287472b3SEd Maste #endif 46287472b3SEd Maste #ifndef ELF_ST_TYPE 47287472b3SEd Maste #define ELF_ST_TYPE(x) (((unsigned int)x) & 0xf) 48287472b3SEd Maste #endif 49287472b3SEd Maste 50287472b3SEd Maste 51287472b3SEd Maste #include "symtab.h" 52287472b3SEd Maste 53287472b3SEd Maste struct symbol { 54287472b3SEd Maste char *st_name; 55287472b3SEd Maste uintptr_t st_value; 56287472b3SEd Maste uintptr_t st_info; 57287472b3SEd Maste }; 58287472b3SEd Maste 59287472b3SEd Maste struct symtab { 60287472b3SEd Maste size_t nsymbols; 61287472b3SEd Maste struct symbol *symbols; 62287472b3SEd Maste }; 63287472b3SEd Maste 64287472b3SEd Maste static int 65287472b3SEd Maste address_compare(const void *a, const void *b) 66287472b3SEd Maste { 67287472b3SEd Maste const struct symbol *sa = a; 68287472b3SEd Maste const struct symbol *sb = b; 69287472b3SEd Maste return (int)(intmax_t)(sa->st_value - sb->st_value); 70287472b3SEd Maste } 71287472b3SEd Maste 72287472b3SEd Maste void 73287472b3SEd Maste symtab_destroy(symtab_t *s) 74287472b3SEd Maste { 75287472b3SEd Maste if (s == NULL) 76287472b3SEd Maste return; 77287472b3SEd Maste for (size_t i = 0; i < s->nsymbols; i++) 78287472b3SEd Maste free(s->symbols[i].st_name); 79287472b3SEd Maste free(s->symbols); 80287472b3SEd Maste free(s); 81287472b3SEd Maste } 82287472b3SEd Maste 83287472b3SEd Maste symtab_t * 84287472b3SEd Maste symtab_create(int fd, int bind, int type) 85287472b3SEd Maste { 86287472b3SEd Maste Elf *elf; 87287472b3SEd Maste symtab_t *st; 88287472b3SEd Maste Elf_Scn *scn = NULL; 89287472b3SEd Maste 90287472b3SEd Maste if (elf_version(EV_CURRENT) == EV_NONE) { 91287472b3SEd Maste warnx("Elf Library is out of date."); 92287472b3SEd Maste return NULL; 93287472b3SEd Maste } 94287472b3SEd Maste 95287472b3SEd Maste elf = elf_begin(fd, ELF_C_READ, NULL); 96287472b3SEd Maste if (elf == NULL) { 97287472b3SEd Maste warnx("Error opening elf file: %s", elf_errmsg(elf_errno())); 98287472b3SEd Maste return NULL; 99287472b3SEd Maste } 100287472b3SEd Maste st = calloc(1, sizeof(*st)); 101287472b3SEd Maste if (st == NULL) { 102287472b3SEd Maste warnx("Error allocating symbol table"); 103287472b3SEd Maste elf_end(elf); 104287472b3SEd Maste return NULL; 105287472b3SEd Maste } 106287472b3SEd Maste 107287472b3SEd Maste while ((scn = elf_nextscn(elf, scn)) != NULL) { 108287472b3SEd Maste GElf_Shdr shdr; 109287472b3SEd Maste Elf_Data *edata; 110287472b3SEd Maste size_t ns; 111287472b3SEd Maste struct symbol *s; 112287472b3SEd Maste 113287472b3SEd Maste gelf_getshdr(scn, &shdr); 114287472b3SEd Maste if(shdr.sh_type != SHT_SYMTAB) 115287472b3SEd Maste continue; 116287472b3SEd Maste 117287472b3SEd Maste edata = elf_getdata(scn, NULL); 118287472b3SEd Maste ns = shdr.sh_size / shdr.sh_entsize; 119287472b3SEd Maste s = calloc(ns, sizeof(*s)); 120287472b3SEd Maste if (s == NULL) { 121287472b3SEd Maste warn("Cannot allocate %zu symbols", ns); 122287472b3SEd Maste goto out; 123287472b3SEd Maste } 124287472b3SEd Maste st->symbols = s; 125287472b3SEd Maste 126287472b3SEd Maste for (size_t i = 0; i < ns; i++) { 127287472b3SEd Maste GElf_Sym sym; 128287472b3SEd Maste gelf_getsym(edata, (int)i, &sym); 129287472b3SEd Maste 130287472b3SEd Maste if (bind != -1 && 131287472b3SEd Maste (unsigned)bind != ELF_ST_BIND(sym.st_info)) 132287472b3SEd Maste continue; 133287472b3SEd Maste 134287472b3SEd Maste if (type != -1 && 135287472b3SEd Maste (unsigned)type != ELF_ST_TYPE(sym.st_info)) 136287472b3SEd Maste continue; 137287472b3SEd Maste 138287472b3SEd Maste s->st_value = sym.st_value; 139287472b3SEd Maste s->st_info = sym.st_info; 140287472b3SEd Maste s->st_name = strdup( 141287472b3SEd Maste elf_strptr(elf, shdr.sh_link, sym.st_name)); 142287472b3SEd Maste if (s->st_name == NULL) 143287472b3SEd Maste goto out; 144287472b3SEd Maste s++; 145287472b3SEd Maste } 146287472b3SEd Maste st->nsymbols = s - st->symbols; 147287472b3SEd Maste if (st->nsymbols == 0) { 148287472b3SEd Maste warnx("No symbols found"); 149287472b3SEd Maste goto out; 150287472b3SEd Maste } 151287472b3SEd Maste qsort(st->symbols, st->nsymbols, sizeof(*st->symbols), 152287472b3SEd Maste address_compare); 153287472b3SEd Maste elf_end(elf); 154287472b3SEd Maste return st; 155287472b3SEd Maste } 156287472b3SEd Maste out: 157287472b3SEd Maste symtab_destroy(st); 158287472b3SEd Maste elf_end(elf); 159287472b3SEd Maste return NULL; 160287472b3SEd Maste } 161287472b3SEd Maste 162287472b3SEd Maste 163287472b3SEd Maste int 164287472b3SEd Maste symtab_find(const symtab_t *st, const void *p, Dl_info *dli) 165287472b3SEd Maste { 166287472b3SEd Maste struct symbol *s = st->symbols; 167287472b3SEd Maste size_t ns = st->nsymbols; 168287472b3SEd Maste size_t hi = ns; 169287472b3SEd Maste size_t lo = 0; 170287472b3SEd Maste size_t mid = ns / 2; 171287472b3SEd Maste uintptr_t dd, sd, me = (uintptr_t)p; 172287472b3SEd Maste 173287472b3SEd Maste for (;;) { 174287472b3SEd Maste if (s[mid].st_value < me) 175287472b3SEd Maste lo = mid; 176287472b3SEd Maste else if (s[mid].st_value > me) 177287472b3SEd Maste hi = mid; 178287472b3SEd Maste else 179287472b3SEd Maste break; 180287472b3SEd Maste if (hi - lo == 1) { 181287472b3SEd Maste mid = lo; 182287472b3SEd Maste break; 183287472b3SEd Maste } 184287472b3SEd Maste mid = (hi + lo) / 2; 185287472b3SEd Maste } 186287472b3SEd Maste dd = me - (uintptr_t)dli->dli_saddr; 187287472b3SEd Maste sd = me - s[mid].st_value; 188287472b3SEd Maste if (dd > sd) { 189287472b3SEd Maste dli->dli_saddr = (void *)s[mid].st_value; 190287472b3SEd Maste dli->dli_sname = s[mid].st_name; 191287472b3SEd Maste } 192287472b3SEd Maste return 1; 193287472b3SEd Maste } 194